@@ -40,57 +40,104 @@ const std::string PYTHON_SESSION_SIDE_PACKET_NAME = "py";
40
40
const std::string LLM_SESSION_SIDE_PACKET_NAME = " llm" ;
41
41
} // namespace
42
42
namespace ovms {
43
+
44
+ std::shared_ptr<GraphHelper> constructGraphHelper (const ::mediapipe::CalculatorGraphConfig& config, PythonNodeResourcesMap& pythonNodeResourcesMap, GenAiServableMap& genAiServableMap) {
45
+ auto gh = std::make_shared<GraphHelper>();
46
+ SPDLOG_ERROR (" ER GraphHelper():{}" , (void *)gh.get ());
47
+ gh->graph = std::make_shared<::mediapipe::CalculatorGraph>();
48
+ gh->currentTimestamp = ::mediapipe::Timestamp (0 );
49
+
50
+ auto absStatus = gh->graph ->Initialize (config);
51
+ if (!absStatus.ok ()) {
52
+ SPDLOG_ERROR (" ER issue:{}" , absStatus.ToString ());
53
+ throw 42 ;
54
+ }
55
+ for (auto & name : config.output_stream ()) {
56
+ std::string streamName = getStreamName (name);
57
+ gh->outStreamObservers [streamName] = std::shared_ptr<OutputStreamObserverI>(new NullOutputStreamObserver ()); // TODO use at() FIXME
58
+ auto & perGraphObserverFunctor = gh->outStreamObservers [streamName];
59
+ absStatus = gh->graph ->ObserveOutputStream (streamName, [&perGraphObserverFunctor](const ::mediapipe::Packet& packet) -> absl::Status { return perGraphObserverFunctor->handlePacket (packet); }); // TODO FIXME throw?
60
+ if (!absStatus.ok ()) {
61
+ SPDLOG_ERROR (" ER issue:{}" , absStatus.ToString ());
62
+ throw 42 ;
63
+ }
64
+ }
65
+ std::map<std::string, mediapipe::Packet> inputSidePackets;
66
+ inputSidePackets[PYTHON_SESSION_SIDE_PACKET_NAME] = mediapipe::MakePacket<PythonNodeResourcesMap>(pythonNodeResourcesMap)
67
+ .At (STARTING_TIMESTAMP);
68
+ inputSidePackets[LLM_SESSION_SIDE_PACKET_NAME] = mediapipe::MakePacket<GenAiServableMap>(genAiServableMap).At (STARTING_TIMESTAMP);
69
+ for (auto [k, v] : inputSidePackets) {
70
+ SPDLOG_ERROR (" k:{} v" , k);
71
+ }
72
+ SPDLOG_ERROR (" ER" );
73
+ absStatus = gh->graph ->StartRun (inputSidePackets);
74
+ SPDLOG_ERROR (" ER" );
75
+ if (!absStatus.ok ()) {
76
+ SPDLOG_ERROR (" Input sidePackets size:{}, python map size:{} key:{} side packet name:{}" , inputSidePackets.size (), pythonNodeResourcesMap.size (), pythonNodeResourcesMap.begin ()->first , PYTHON_SESSION_SIDE_PACKET_NAME);
77
+ SPDLOG_ERROR (" ER issue:{}" , absStatus.ToString ());
78
+ throw 42 ;
79
+ }
80
+ SPDLOG_ERROR (" ER" );
81
+ return gh;
82
+ }
83
+ void GraphQueue::restoreStream (int streamId) {
84
+ if (streamId < inferRequests.size ()) {
85
+ SPDLOG_ERROR (" Cannot restore stream id > queue length" );
86
+ assert (streamId < inferRequests.size ());
87
+ }
88
+ inferRequests[streamId] = constructGraphHelper (*this ->config , *this ->pythonNodeResourcesMap , *this ->genAiServableMap );
89
+ }
90
+
43
91
GraphQueue::GraphQueue (const ::mediapipe::CalculatorGraphConfig& config, std::shared_ptr<PythonNodeResourcesMap> pythonNodeResourcesMap, std::shared_ptr<GenAiServableMap> genAiServableMap, int streamsLength) :
44
92
Queue (streamsLength),
93
+ config (std::make_shared<const ::mediapipe::CalculatorGraphConfig>(config)),
45
94
pythonNodeResourcesMap (pythonNodeResourcesMap),
46
95
genAiServableMap (genAiServableMap) {
47
- SPDLOG_ERROR (" ER Constr graph queue :{}" , (void *)this );
96
+ SPDLOG_ERROR (" ER GraphQueue() :{}" , (void *)this );
48
97
inferRequests.reserve (streamsLength);
49
98
// TODO FIXME split constructor to init to handle retCodes?
50
99
for (auto i = 0 ; i < streamsLength; ++i) {
51
- auto gh = std::make_shared<GraphHelper>();
52
- gh->graph = std::make_shared<::mediapipe::CalculatorGraph>();
53
- gh->currentTimestamp = ::mediapipe::Timestamp (0 );
54
-
55
- auto absStatus = gh->graph ->Initialize (config);
56
- if (!absStatus.ok ()) {
57
- SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
58
- throw 42 ;
59
- }
60
- for (auto & name : config.output_stream ()) {
61
- std::string streamName = getStreamName (name);
62
- gh->outStreamObservers [streamName] = std::shared_ptr<OutputStreamObserverI>(new NullOutputStreamObserver ()); // TODO use at() FIXME
63
- auto & perGraphObserverFunctor = gh->outStreamObservers [streamName];
64
- absStatus = gh->graph ->ObserveOutputStream (streamName, [&perGraphObserverFunctor](const ::mediapipe::Packet& packet) -> absl::Status { return perGraphObserverFunctor->handlePacket (packet); }); // TODO FIXME throw?
65
- if (!absStatus.ok ()) {
66
- SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
67
- throw 42 ;
68
- }
69
- }
70
- std::map<std::string, mediapipe::Packet> inputSidePackets;
71
- inputSidePackets[PYTHON_SESSION_SIDE_PACKET_NAME] = mediapipe::MakePacket<PythonNodeResourcesMap>(*pythonNodeResourcesMap)
72
- .At (STARTING_TIMESTAMP);
73
- inputSidePackets[LLM_SESSION_SIDE_PACKET_NAME] = mediapipe::MakePacket<GenAiServableMap>(*genAiServableMap).At (STARTING_TIMESTAMP);
74
- for (auto [k, v] : inputSidePackets) {
75
- SPDLOG_ERROR (" k:{} v" , k);
76
- }
77
100
SPDLOG_ERROR (" ER" );
78
- absStatus = gh-> graph -> StartRun (inputSidePackets );
101
+ inferRequests. emplace_back ( std::move ( constructGraphHelper (* this -> config , *pythonNodeResourcesMap, *genAiServableMap)) );
79
102
SPDLOG_ERROR (" ER" );
80
- if (!absStatus.ok ()) {
81
- SPDLOG_ERROR (" Input sidePackets size:{}, python map size:{} key:{} side packet name:{}" , inputSidePackets.size (), pythonNodeResourcesMap->size (), pythonNodeResourcesMap->begin ()->first , PYTHON_SESSION_SIDE_PACKET_NAME);
82
- SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
83
- throw 42 ;
84
- }
103
+ }
104
+ }
85
105
86
- SPDLOG_ERROR (" ER" );
87
- inferRequests.emplace_back (std::move (gh));
88
- SPDLOG_ERROR (" ER" );
106
+ GraphHelper::~GraphHelper () {
107
+ SPDLOG_TRACE (" GraphHelper wait until idle graph" );
108
+ auto absStatus = this ->graph ->WaitUntilIdle ();
109
+ if (!absStatus.ok ()) {
110
+ SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
111
+ // throw 42.2;
112
+ }
113
+ absStatus = this ->graph ->CloseAllPacketSources ();
114
+ if (!absStatus.ok ()) {
115
+ SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
116
+ // throw "as";
89
117
}
118
+ SPDLOG_TRACE (" GraphQueue wait until done graph" );
119
+ absStatus = this ->graph ->WaitUntilDone ();
120
+ if (!absStatus.ok ()) {
121
+ SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
122
+ // throw 42.2;
123
+ }
124
+ this ->graph ->Cancel ();
125
+ if (!absStatus.ok ()) {
126
+ SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
127
+ // throw 42.2;
128
+ }
129
+ SPDLOG_ERROR (" ER" );
130
+ this ->graph .reset ();
131
+ SPDLOG_ERROR (" ER ~GraphHelper:{}" , (void *) this );
90
132
}
133
+
91
134
GraphQueue::~GraphQueue () {
92
- SPDLOG_ERROR (" ER Destroy graph queue :{}" , (void *)this );
135
+ SPDLOG_ERROR (" ER ~GraphQueue :{}" , (void *)this );
93
136
for (auto & graphHelper : inferRequests) {
137
+ SPDLOG_TRACE (" GraphQueue wait until idle graph" );
138
+ graphHelper.reset ();
139
+ SPDLOG_ERROR (" ER" );
140
+ continue ;
94
141
auto absStatus = graphHelper->graph ->WaitUntilIdle ();
95
142
if (!absStatus.ok ()) {
96
143
SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
@@ -101,6 +148,7 @@ GraphQueue::~GraphQueue() {
101
148
SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
102
149
// throw "as";
103
150
}
151
+ SPDLOG_TRACE (" GraphQueue wait until done graph" );
104
152
absStatus = graphHelper->graph ->WaitUntilDone ();
105
153
if (!absStatus.ok ()) {
106
154
SPDLOG_ERROR (" ER issue:{} {}" , absStatus.ToString (), (void *)this );
@@ -115,6 +163,7 @@ GraphQueue::~GraphQueue() {
115
163
graphHelper->graph .reset ();
116
164
SPDLOG_ERROR (" ER" );
117
165
}
118
- SPDLOG_ERROR (" ER Destroy graph queue :{}" , (void *)this );
166
+ SPDLOG_ERROR (" ER ~GraphQueue :{}" , (void *)this );
119
167
}
168
+ // TODO FIXME @atobisze move to destructor
120
169
} // namespace ovms
0 commit comments