-
Notifications
You must be signed in to change notification settings - Fork 254
Add graph reinit on failure #4237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atobiszei
wants to merge
14
commits into
main
Choose a base branch
from
atobisze_mp_reinit_graph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−52
Open
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1850989
Fix python build for mp_on_py_off config
atobiszei 537cc52
Turn off unstable tests
atobiszei 271f337
Add graph reinit on failure
atobiszei 271ee8e
Merge remote-tracking branch 'origin/main' into atobisze_mp_reinit_graph
atobiszei 28aadd6
Merge remote-tracking branch 'origin/main' into atobisze_mp_reinit_graph
atobiszei 10477df
Remove default queue switch off
atobiszei 6b96b84
Fix style
atobiszei 4806c6b
Fix
atobiszei 8d60096
Fix CI: add ErrorOnNegativeTestCalculator to whitelist, remove queue …
atobiszei d6c7ff0
Move ErrorOnNegativeTestCalculator to its own file
atobiszei 4aee6d6
Review fixes
atobiszei 3ac2926
Self-review
atobiszei 25e3f72
Self-review
atobiszei 66d3f19
Self-review: add try/catch in GraphReinitGuard dtor, use this-> consi…
atobiszei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,25 +85,82 @@ GraphQueue::GraphQueue(const ::mediapipe::CalculatorGraphConfig& config, std::sh | |
| SPDLOG_ERROR("Graph queue StartRun failed: {}", absStatus.ToString()); | ||
| throw std::runtime_error(absStatus.ToString()); | ||
| } | ||
| inferRequests.emplace_back(std::move(graphHelper)); | ||
| this->inferRequests.emplace_back(std::move(graphHelper)); | ||
| } | ||
| } | ||
| GraphQueue::~GraphQueue() { | ||
| for (auto& graphHelper : inferRequests) { | ||
| auto absStatus = graphHelper->graph->WaitUntilIdle(); | ||
| GraphHelper::~GraphHelper() { | ||
| if (!graph) { | ||
| return; | ||
| } | ||
| auto absStatus = graph->WaitUntilIdle(); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_DEBUG("GraphHelper WaitUntilIdle error: {}", absStatus.ToString()); | ||
| } | ||
| absStatus = graph->CloseAllPacketSources(); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_DEBUG("GraphHelper CloseAllPacketSources error: {}", absStatus.ToString()); | ||
| } | ||
| absStatus = graph->WaitUntilDone(); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_DEBUG("GraphHelper WaitUntilDone error: {}", absStatus.ToString()); | ||
| } | ||
| graph->Cancel(); | ||
| } | ||
|
|
||
| void GraphHelper::reinitialize(const ::mediapipe::CalculatorGraphConfig& config, const GraphSidePackets& sidePacketMaps) { | ||
| SPDLOG_DEBUG("Reinitializing graph after error"); | ||
| // Tear down the old graph (best-effort, errors expected since graph is in bad state) | ||
| if (this->graph) { | ||
| auto absStatus = this->graph->CloseAllPacketSources(); | ||
|
Comment on lines
+120
to
+124
|
||
| if (!absStatus.ok()) { | ||
| SPDLOG_DEBUG("Graph queue WaitUntilIdle error: {}", absStatus.ToString()); | ||
| SPDLOG_DEBUG("reinitialize: CloseAllPacketSources: {}", absStatus.ToString()); | ||
| } | ||
| absStatus = graphHelper->graph->CloseAllPacketSources(); | ||
| absStatus = this->graph->WaitUntilDone(); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_DEBUG("Graph queue CloseAllPacketSources error: {}", absStatus.ToString()); | ||
| SPDLOG_DEBUG("reinitialize: WaitUntilDone: {}", absStatus.ToString()); | ||
| } | ||
| absStatus = graphHelper->graph->WaitUntilDone(); | ||
| this->graph->Cancel(); | ||
| } | ||
| // Create fresh graph | ||
| graph = std::make_unique<::mediapipe::CalculatorGraph>(); | ||
| currentTimestamp = ::mediapipe::Timestamp(0); | ||
|
|
||
| auto absStatus = graph->Initialize(config); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_ERROR("Graph reinitialize: Initialize failed: {}", absStatus.ToString()); | ||
| graph.reset(); | ||
| return; | ||
|
|
||
| } | ||
|
|
||
| for (const auto& [streamName, holder] : outStreamObservers) { | ||
| absStatus = graph->ObserveOutputStream(streamName, [holder](const ::mediapipe::Packet& packet) -> absl::Status { | ||
| return holder->current->handlePacket(packet); | ||
| }); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_DEBUG("Graph queue WaitUntilDone error: {}", absStatus.ToString()); | ||
| SPDLOG_ERROR("Graph reinitialize: ObserveOutputStream failed: {}", absStatus.ToString()); | ||
| graph.reset(); | ||
| return; | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove graph.reset. |
||
| graphHelper->graph->Cancel(); | ||
| graphHelper->graph.reset(); | ||
| } | ||
| // Reset observers to null sentinel | ||
| for (const auto& [streamName, holder] : outStreamObservers) { | ||
| holder->current = std::make_shared<NullOutputStreamObserver>(); | ||
| } | ||
| // Reset execution contexts | ||
| for (auto& [nodeName, ctx] : genAiExecutionContextMap) { | ||
| ctx->reset(); | ||
| } | ||
| std::map<std::string, mediapipe::Packet> inputSidePackets; | ||
| buildInputSidePackets(inputSidePackets, sidePacketMaps); | ||
| inputSidePackets[LLM_EXECUTION_CONTEXT_SESSION_SIDE_PACKET_TAG] = | ||
| mediapipe::MakePacket<GenAiExecutionContextMap>(genAiExecutionContextMap) | ||
| .At(::mediapipe::Timestamp(STARTING_TIMESTAMP_VALUE)); | ||
| absStatus = graph->StartRun(inputSidePackets); | ||
| if (!absStatus.ok()) { | ||
| SPDLOG_ERROR("Graph reinitialize: StartRun failed: {}", absStatus.ToString()); | ||
| graph.reset(); | ||
| return; | ||
| } | ||
|
|
||
| SPDLOG_DEBUG("Graph reinitialized successfully"); | ||
| } | ||
| GraphQueue::~GraphQueue() = default; | ||
| } // namespace ovms | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,40 @@ struct GraphHelper { | |
| genAiExecutionContextMap(std::move(gh.genAiExecutionContextMap)), | ||
| currentTimestamp(gh.currentTimestamp) {} | ||
| GraphHelper& operator=(GraphHelper&&) = delete; | ||
| ~GraphHelper(); | ||
| // Tears down the current (errored) graph and rebuilds a fresh one | ||
| // with the same observers and side packets. Called when inference | ||
| // encounters a graph error to avoid returning a poisoned graph to the pool. | ||
| void reinitialize(const ::mediapipe::CalculatorGraphConfig& config, const GraphSidePackets& sidePacketMaps); | ||
| }; | ||
|
|
||
| // RAII guard that reinitializes the graph if inference exits with an error. | ||
| // Construct before the first graph interaction (packet push). Call dismiss() | ||
| // on the success path. If not dismissed, the destructor rebuilds the graph | ||
| // so the next request from the pool gets a clean graph. | ||
| class GraphReinitGuard { | ||
| GraphHelper& helper; | ||
| const ::mediapipe::CalculatorGraphConfig& config; | ||
| const GraphSidePackets& sidePacketMaps; | ||
| bool dismissed = false; | ||
|
|
||
| public: | ||
| GraphReinitGuard(GraphHelper& helper, | ||
| const ::mediapipe::CalculatorGraphConfig& config, | ||
| const GraphSidePackets& sidePacketMaps) : | ||
| helper(helper), | ||
| config(config), | ||
| sidePacketMaps(sidePacketMaps) {} | ||
| void dismiss() { dismissed = true; } | ||
| ~GraphReinitGuard() { | ||
| if (!dismissed) { | ||
| helper.reinitialize(config, sidePacketMaps); | ||
|
|
||
| } | ||
| } | ||
|
Comment on lines
+99
to
+109
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case we could only log error anyway. Will add that. |
||
| GraphReinitGuard(const GraphReinitGuard&) = delete; | ||
| GraphReinitGuard& operator=(const GraphReinitGuard&) = delete; | ||
| }; | ||
|
|
||
| // we need to keep Graph alive during MP reload hence shared_ptr | ||
| class GraphQueue : public Queue<std::shared_ptr<GraphHelper>> { | ||
| std::shared_ptr<GraphSidePackets> sidePacketMaps; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit test that will ensure failed graphs are reusable is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test