Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved.
4 : *
5 : * @file ml-api-service-extension.c
6 : * @date 1 September 2023
7 : * @brief ML service extension C-API.
8 : * @see https://github.com/nnstreamer/api
9 : * @author Jaeyun Jung <jy1210.jung@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 :
13 : #include "ml-api-service-extension.h"
14 :
15 : /**
16 : * @brief The time to wait for new input data in message thread, in millisecond.
17 : */
18 : #define DEFAULT_TIMEOUT 200
19 :
20 : /**
21 : * @brief The max number of input data in message queue (0 for no limit).
22 : */
23 : #define DEFAULT_MAX_INPUT 5
24 :
25 : /**
26 : * @brief Internal enumeration for ml-service extension types.
27 : */
28 : typedef enum
29 : {
30 : ML_EXTENSION_TYPE_UNKNOWN = 0,
31 : ML_EXTENSION_TYPE_SINGLE = 1,
32 : ML_EXTENSION_TYPE_PIPELINE = 2,
33 :
34 : ML_EXTENSION_TYPE_MAX
35 : } ml_extension_type_e;
36 :
37 : /**
38 : * @brief Internal structure of the message in ml-service extension handle.
39 : */
40 : typedef struct
41 : {
42 : gchar *name;
43 : ml_tensors_data_h input;
44 : ml_tensors_data_h output;
45 : } ml_extension_msg_s;
46 :
47 : /**
48 : * @brief Internal structure for ml-service extension handle.
49 : */
50 : typedef struct
51 : {
52 : ml_extension_type_e type;
53 : gboolean running;
54 : guint timeout; /**< The time to wait for new input data in message thread, in millisecond (see DEFAULT_TIMEOUT). */
55 : guint max_input; /**< The max number of input data in message queue (see DEFAULT_MAX_INPUT). */
56 : GThread *msg_thread;
57 : GAsyncQueue *msg_queue;
58 :
59 : /**
60 : * Handles for each ml-service extension type.
61 : * - single : Default. Open model file and prepare invoke. The configuration should include model information.
62 : * - pipeline : Construct a pipeline from configuration. The configuration should include pipeline description.
63 : */
64 : ml_single_h single;
65 :
66 : ml_pipeline_h pipeline;
67 : GHashTable *node_table;
68 : } ml_extension_s;
69 :
70 : /**
71 : * @brief Internal function to handle the asynchronous invoke.
72 : */
73 : static int
74 0 : _ml_extension_async_cb (const ml_tensors_data_h data, void *user_data)
75 : {
76 0 : ml_service_s *mls = (ml_service_s *) user_data;
77 :
78 0 : return _ml_service_invoke_event_new_data (mls, NULL, data);
79 : }
80 :
81 : /**
82 : * @brief Internal function to create node info in pipeline.
83 : */
84 : static ml_service_node_info_s *
85 0 : _ml_extension_node_info_new (ml_service_s * mls, const gchar * name,
86 : ml_service_node_type_e type)
87 : {
88 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
89 : ml_service_node_info_s *node_info;
90 :
91 0 : if (!STR_IS_VALID (name)) {
92 0 : _ml_error_report_return (NULL,
93 : "Cannot add new node info, invalid node name '%s'.", name);
94 : }
95 :
96 0 : if (g_hash_table_lookup (ext->node_table, name)) {
97 0 : _ml_error_report_return (NULL,
98 : "Cannot add duplicated node '%s' in ml-service pipeline.", name);
99 : }
100 :
101 0 : node_info = g_try_new0 (ml_service_node_info_s, 1);
102 0 : if (!node_info) {
103 0 : _ml_error_report_return (NULL,
104 : "Failed to allocate new memory for node info in ml-service pipeline. Out of memory?");
105 : }
106 :
107 0 : node_info->name = g_strdup (name);
108 0 : node_info->type = type;
109 0 : node_info->mls = mls;
110 :
111 0 : g_hash_table_insert (ext->node_table, g_strdup (name), node_info);
112 :
113 0 : return node_info;
114 : }
115 :
116 : /**
117 : * @brief Internal function to release pipeline node info.
118 : */
119 : static void
120 0 : _ml_extension_node_info_free (gpointer data)
121 : {
122 0 : ml_service_node_info_s *node_info = (ml_service_node_info_s *) data;
123 :
124 0 : if (!node_info)
125 0 : return;
126 :
127 0 : if (node_info->info)
128 0 : ml_tensors_info_destroy (node_info->info);
129 :
130 0 : g_free (node_info->name);
131 0 : g_free (node_info);
132 : }
133 :
134 : /**
135 : * @brief Internal function to get the node info in ml-service extension.
136 : */
137 : static ml_service_node_info_s *
138 0 : _ml_extension_node_info_get (ml_extension_s * ext, const gchar * name)
139 : {
140 0 : if (!STR_IS_VALID (name))
141 0 : return NULL;
142 :
143 0 : return g_hash_table_lookup (ext->node_table, name);
144 : }
145 :
146 : /**
147 : * @brief Internal function to release ml-service extension message.
148 : */
149 : static void
150 0 : _ml_extension_msg_free (gpointer data)
151 : {
152 0 : ml_extension_msg_s *msg = (ml_extension_msg_s *) data;
153 :
154 0 : if (!msg)
155 0 : return;
156 :
157 0 : if (msg->input)
158 0 : ml_tensors_data_destroy (msg->input);
159 0 : if (msg->output)
160 0 : ml_tensors_data_destroy (msg->output);
161 :
162 0 : g_free (msg->name);
163 0 : g_free (msg);
164 : }
165 :
166 : /**
167 : * @brief Internal function to process ml-service extension message.
168 : */
169 : static gpointer
170 0 : _ml_extension_msg_thread (gpointer data)
171 : {
172 0 : ml_service_s *mls = (ml_service_s *) data;
173 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
174 : int status;
175 :
176 0 : g_mutex_lock (&mls->lock);
177 0 : ext->running = TRUE;
178 0 : g_cond_signal (&mls->cond);
179 0 : g_mutex_unlock (&mls->lock);
180 :
181 0 : while (ext->running) {
182 : ml_extension_msg_s *msg;
183 :
184 0 : msg = g_async_queue_timeout_pop (ext->msg_queue,
185 0 : ext->timeout * G_TIME_SPAN_MILLISECOND);
186 :
187 0 : if (msg) {
188 0 : switch (ext->type) {
189 0 : case ML_EXTENSION_TYPE_SINGLE:
190 : {
191 0 : status = ml_single_invoke (ext->single, msg->input, &msg->output);
192 :
193 0 : if (status == ML_ERROR_NONE) {
194 0 : _ml_service_invoke_event_new_data (mls, NULL, msg->output);
195 : } else {
196 0 : _ml_error_report
197 : ("Failed to invoke the model in ml-service extension thread.");
198 : }
199 0 : break;
200 : }
201 0 : case ML_EXTENSION_TYPE_PIPELINE:
202 : {
203 : ml_service_node_info_s *node_info;
204 :
205 0 : node_info = _ml_extension_node_info_get (ext, msg->name);
206 :
207 0 : if (node_info && node_info->type == ML_SERVICE_NODE_TYPE_INPUT) {
208 : /* The input data will be released in the pipeline. */
209 0 : status = ml_pipeline_src_input_data (node_info->handle, msg->input,
210 : ML_PIPELINE_BUF_POLICY_AUTO_FREE);
211 0 : msg->input = NULL;
212 :
213 0 : if (status != ML_ERROR_NONE) {
214 0 : _ml_error_report
215 : ("Failed to push input data into the pipeline in ml-service extension thread.");
216 : }
217 : } else {
218 0 : _ml_error_report
219 : ("Failed to push input data into the pipeline, cannot find input node '%s'.",
220 : msg->name);
221 : }
222 0 : break;
223 : }
224 0 : default:
225 : /* Unknown ml-service extension type, skip this. */
226 0 : break;
227 : }
228 :
229 0 : _ml_extension_msg_free (msg);
230 : }
231 : }
232 :
233 0 : return NULL;
234 : }
235 :
236 : /**
237 : * @brief Wrapper to release tensors-info handle.
238 : */
239 : static void
240 0 : _ml_extension_destroy_tensors_info (void *data)
241 : {
242 0 : ml_tensors_info_h info = (ml_tensors_info_h) data;
243 :
244 0 : if (info)
245 0 : ml_tensors_info_destroy (info);
246 0 : }
247 :
248 : /**
249 : * @brief Internal function to parse single-shot info from json.
250 : */
251 : static int
252 0 : _ml_extension_conf_parse_single (ml_service_s * mls, JsonObject * single)
253 : {
254 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
255 : ml_option_h option;
256 : int status;
257 :
258 0 : status = ml_option_create (&option);
259 0 : if (status != ML_ERROR_NONE) {
260 0 : _ml_error_report_return (status,
261 : "Failed to parse configuration file, cannot create ml-option handle.");
262 : }
263 :
264 : /**
265 : * 1. "key" : load model info from ml-service agent.
266 : * 2. "model" : configuration file includes model path.
267 : */
268 0 : if (json_object_has_member (single, "key")) {
269 0 : const gchar *key = json_object_get_string_member (single, "key");
270 :
271 0 : if (STR_IS_VALID (key)) {
272 : ml_information_h model_info;
273 :
274 0 : status = ml_service_model_get_activated (key, &model_info);
275 0 : if (status == ML_ERROR_NONE) {
276 0 : gchar *paths = NULL;
277 :
278 : /** @todo parse desc and other information if necessary. */
279 0 : ml_information_get (model_info, "path", (void **) (&paths));
280 0 : ml_option_set (option, "models", g_strdup (paths), g_free);
281 :
282 0 : ml_information_destroy (model_info);
283 : } else {
284 0 : _ml_error_report
285 : ("Failed to parse configuration file, cannot get the model of '%s'.",
286 : key);
287 0 : goto error;
288 : }
289 : }
290 0 : } else if (json_object_has_member (single, "model")) {
291 0 : JsonNode *file_node = json_object_get_member (single, "model");
292 0 : gchar *paths = NULL;
293 :
294 0 : status = _ml_service_conf_parse_string (file_node, ",", &paths);
295 0 : if (status != ML_ERROR_NONE) {
296 0 : _ml_error_report
297 : ("Failed to parse configuration file, it should have valid model path.");
298 0 : goto error;
299 : }
300 :
301 0 : ml_option_set (option, "models", paths, g_free);
302 : } else {
303 0 : status = ML_ERROR_INVALID_PARAMETER;
304 0 : _ml_error_report
305 : ("Failed to parse configuration file, cannot get the model path.");
306 0 : goto error;
307 : }
308 :
309 0 : if (json_object_has_member (single, "framework")) {
310 0 : const gchar *fw = json_object_get_string_member (single, "framework");
311 :
312 0 : if (STR_IS_VALID (fw))
313 0 : ml_option_set (option, "framework_name", g_strdup (fw), g_free);
314 : }
315 :
316 0 : if (json_object_has_member (single, "input_info")) {
317 0 : JsonNode *info_node = json_object_get_member (single, "input_info");
318 : ml_tensors_info_h in_info;
319 :
320 0 : status = _ml_service_conf_parse_tensors_info (info_node, &in_info);
321 0 : if (status != ML_ERROR_NONE) {
322 0 : _ml_error_report
323 : ("Failed to parse configuration file, cannot parse input information.");
324 0 : goto error;
325 : }
326 :
327 0 : ml_option_set (option, "input_info", in_info,
328 : _ml_extension_destroy_tensors_info);
329 : }
330 :
331 0 : if (json_object_has_member (single, "output_info")) {
332 0 : JsonNode *info_node = json_object_get_member (single, "output_info");
333 : ml_tensors_info_h out_info;
334 :
335 0 : status = _ml_service_conf_parse_tensors_info (info_node, &out_info);
336 0 : if (status != ML_ERROR_NONE) {
337 0 : _ml_error_report
338 : ("Failed to parse configuration file, cannot parse output information.");
339 0 : goto error;
340 : }
341 :
342 0 : ml_option_set (option, "output_info", out_info,
343 : _ml_extension_destroy_tensors_info);
344 : }
345 :
346 0 : if (json_object_has_member (single, "custom")) {
347 0 : const gchar *custom = json_object_get_string_member (single, "custom");
348 :
349 0 : if (STR_IS_VALID (custom))
350 0 : ml_option_set (option, "custom", g_strdup (custom), g_free);
351 : }
352 :
353 0 : if (json_object_has_member (single, "invoke_dynamic")) {
354 : const gchar *invoke_dynamic =
355 0 : json_object_get_string_member (single, "invoke_dynamic");
356 :
357 0 : if (STR_IS_VALID (invoke_dynamic)) {
358 0 : ml_option_set (option, "invoke_dynamic", g_strdup (invoke_dynamic),
359 : g_free);
360 : }
361 : }
362 :
363 0 : if (json_object_has_member (single, "invoke_async")) {
364 : const gchar *invoke_async =
365 0 : json_object_get_string_member (single, "invoke_async");
366 :
367 0 : if (STR_IS_VALID (invoke_async)) {
368 0 : ml_option_set (option, "invoke_async", g_strdup (invoke_async), g_free);
369 :
370 0 : if (g_ascii_strcasecmp (invoke_async, "true") == 0) {
371 0 : ml_option_set (option, "async_callback", _ml_extension_async_cb, NULL);
372 0 : ml_option_set (option, "async_data", mls, NULL);
373 : }
374 : }
375 : }
376 :
377 0 : error:
378 0 : if (status == ML_ERROR_NONE)
379 0 : status = ml_single_open_with_option (&ext->single, option);
380 :
381 0 : ml_option_destroy (option);
382 0 : return status;
383 : }
384 :
385 : /**
386 : * @brief Internal function to parse the node info in pipeline.
387 : */
388 : static int
389 0 : _ml_extension_conf_parse_pipeline_node (ml_service_s * mls, JsonNode * node,
390 : ml_service_node_type_e type)
391 : {
392 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
393 0 : JsonArray *array = NULL;
394 : JsonObject *object;
395 : guint i, n;
396 : int status;
397 :
398 0 : n = 1;
399 0 : if (JSON_NODE_HOLDS_ARRAY (node)) {
400 0 : array = json_node_get_array (node);
401 0 : n = json_array_get_length (array);
402 : }
403 :
404 0 : for (i = 0; i < n; i++) {
405 0 : const gchar *name = NULL;
406 : ml_service_node_info_s *node_info;
407 :
408 0 : if (array)
409 0 : object = json_array_get_object_element (array, i);
410 : else
411 0 : object = json_node_get_object (node);
412 :
413 0 : name = _ml_service_get_json_string_member (object, "name");
414 :
415 0 : node_info = _ml_extension_node_info_new (mls, name, type);
416 0 : if (!node_info) {
417 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
418 : "Failed to parse configuration file, cannot add new node information.");
419 : }
420 :
421 0 : if (json_object_has_member (object, "info")) {
422 0 : JsonNode *info_node = json_object_get_member (object, "info");
423 :
424 0 : status = _ml_service_conf_parse_tensors_info (info_node,
425 : &node_info->info);
426 0 : if (status != ML_ERROR_NONE) {
427 0 : _ml_error_report_return (status,
428 : "Failed to parse configuration file, cannot parse the information.");
429 : }
430 : } else {
431 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
432 : "Failed to parse configuration file, cannot find node information.");
433 : }
434 :
435 0 : switch (type) {
436 0 : case ML_SERVICE_NODE_TYPE_INPUT:
437 0 : status = ml_pipeline_src_get_handle (ext->pipeline, name,
438 0 : &node_info->handle);
439 0 : break;
440 0 : case ML_SERVICE_NODE_TYPE_OUTPUT:
441 0 : status = ml_pipeline_sink_register (ext->pipeline, name,
442 0 : _ml_service_pipeline_sink_cb, node_info, &node_info->handle);
443 0 : break;
444 0 : default:
445 0 : status = ML_ERROR_INVALID_PARAMETER;
446 0 : break;
447 : }
448 :
449 0 : if (status != ML_ERROR_NONE) {
450 0 : _ml_error_report_return (status,
451 : "Failed to parse configuration file, cannot get the handle for pipeline node.");
452 : }
453 : }
454 :
455 0 : return ML_ERROR_NONE;
456 : }
457 :
458 : /**
459 : * @brief Internal function to parse pipeline info from json.
460 : */
461 : static int
462 0 : _ml_extension_conf_parse_pipeline (ml_service_s * mls, JsonObject * pipe)
463 : {
464 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
465 0 : g_autofree gchar *desc = NULL;
466 : int status;
467 :
468 : /**
469 : * 1. "key" : load pipeline from ml-service agent.
470 : * 2. "description" : configuration file includes pipeline description.
471 : */
472 0 : if (json_object_has_member (pipe, "key")) {
473 0 : const gchar *key = json_object_get_string_member (pipe, "key");
474 :
475 0 : if (STR_IS_VALID (key)) {
476 0 : status = ml_service_pipeline_get (key, &desc);
477 0 : if (status != ML_ERROR_NONE) {
478 0 : _ml_error_report_return (status,
479 : "Failed to parse configuration file, cannot get the pipeline of '%s'.",
480 : key);
481 : }
482 : }
483 0 : } else if (json_object_has_member (pipe, "description")) {
484 0 : desc = g_strdup (json_object_get_string_member (pipe, "description"));
485 : } else {
486 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
487 : "Failed to parse configuration file, cannot get the pipeline description.");
488 : }
489 :
490 0 : status = ml_pipeline_construct (desc, NULL, NULL, &ext->pipeline);
491 0 : if (status != ML_ERROR_NONE) {
492 0 : _ml_error_report_return (status,
493 : "Failed to parse configuration file, cannot construct the pipeline.");
494 : }
495 :
496 0 : if (json_object_has_member (pipe, "input_node")) {
497 0 : JsonNode *node = json_object_get_member (pipe, "input_node");
498 :
499 0 : status = _ml_extension_conf_parse_pipeline_node (mls, node,
500 : ML_SERVICE_NODE_TYPE_INPUT);
501 0 : if (status != ML_ERROR_NONE) {
502 0 : _ml_error_report_return (status,
503 : "Failed to parse configuration file, cannot get the input node.");
504 : }
505 : } else {
506 0 : _ml_logw
507 : ("No input node is defined in the pipeline. Might Non-appsrc be used?");
508 : }
509 :
510 0 : if (json_object_has_member (pipe, "output_node")) {
511 0 : JsonNode *node = json_object_get_member (pipe, "output_node");
512 :
513 0 : status = _ml_extension_conf_parse_pipeline_node (mls, node,
514 : ML_SERVICE_NODE_TYPE_OUTPUT);
515 0 : if (status != ML_ERROR_NONE) {
516 0 : _ml_error_report_return (status,
517 : "Failed to parse configuration file, cannot get the output node.");
518 : }
519 : } else {
520 0 : _ml_logw ("No output node is defined in the pipeline.");
521 : }
522 :
523 : /* Start pipeline when creating ml-service handle to check pipeline description. */
524 0 : status = ml_pipeline_start (ext->pipeline);
525 0 : if (status != ML_ERROR_NONE) {
526 0 : _ml_error_report_return (status,
527 : "Failed to parse configuration file, cannot start the pipeline.");
528 : }
529 :
530 0 : return ML_ERROR_NONE;
531 : }
532 :
533 : /**
534 : * @brief Internal function to parse configuration file.
535 : */
536 : static int
537 0 : _ml_extension_conf_parse_json (ml_service_s * mls, JsonObject * object)
538 : {
539 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
540 : int status;
541 :
542 0 : if (json_object_has_member (object, "single")) {
543 0 : JsonObject *single = json_object_get_object_member (object, "single");
544 :
545 0 : status = _ml_extension_conf_parse_single (mls, single);
546 0 : if (status != ML_ERROR_NONE)
547 0 : return status;
548 :
549 0 : ext->type = ML_EXTENSION_TYPE_SINGLE;
550 0 : } else if (json_object_has_member (object, "pipeline")) {
551 0 : JsonObject *pipe = json_object_get_object_member (object, "pipeline");
552 :
553 0 : status = _ml_extension_conf_parse_pipeline (mls, pipe);
554 0 : if (status != ML_ERROR_NONE)
555 0 : return status;
556 :
557 0 : ext->type = ML_EXTENSION_TYPE_PIPELINE;
558 : } else {
559 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
560 : "Failed to parse configuration file, cannot get the valid type from configuration.");
561 : }
562 :
563 0 : return ML_ERROR_NONE;
564 : }
565 :
566 : /**
567 : * @brief Internal function to create ml-service extension.
568 : */
569 : int
570 0 : _ml_service_extension_create (ml_service_s * mls, JsonObject * object)
571 : {
572 : ml_extension_s *ext;
573 0 : g_autofree gchar *thread_name = g_strdup_printf ("ml-ext-msg-%d", getpid ());
574 : int status;
575 :
576 0 : mls->priv = ext = g_try_new0 (ml_extension_s, 1);
577 0 : if (ext == NULL) {
578 0 : _ml_error_report_return (ML_ERROR_OUT_OF_MEMORY,
579 : "Failed to allocate memory for ml-service extension. Out of memory?");
580 : }
581 :
582 0 : ext->type = ML_EXTENSION_TYPE_UNKNOWN;
583 0 : ext->running = FALSE;
584 0 : ext->timeout = DEFAULT_TIMEOUT;
585 0 : ext->max_input = DEFAULT_MAX_INPUT;
586 0 : ext->node_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
587 : _ml_extension_node_info_free);
588 :
589 0 : status = _ml_extension_conf_parse_json (mls, object);
590 0 : if (status != ML_ERROR_NONE) {
591 0 : _ml_error_report_return (status,
592 : "Failed to parse the ml-service extension configuration.");
593 : }
594 :
595 0 : g_mutex_lock (&mls->lock);
596 :
597 0 : ext->msg_queue = g_async_queue_new_full (_ml_extension_msg_free);
598 0 : ext->msg_thread = g_thread_new (thread_name, _ml_extension_msg_thread, mls);
599 :
600 : /* Wait until the message thread has been initialized. */
601 0 : g_cond_wait (&mls->cond, &mls->lock);
602 0 : g_mutex_unlock (&mls->lock);
603 :
604 0 : return ML_ERROR_NONE;
605 : }
606 :
607 : /**
608 : * @brief Internal function to release ml-service extension.
609 : */
610 : int
611 0 : _ml_service_extension_destroy (ml_service_s * mls)
612 : {
613 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
614 :
615 : /* Supposed internal function call to release handle. */
616 0 : if (!ext)
617 0 : return ML_ERROR_NONE;
618 :
619 : /**
620 : * Close message thread.
621 : * If model inference is running, it may wait for the result in message thread.
622 : * This takes time, so do not call join with extension lock.
623 : */
624 0 : ext->running = FALSE;
625 0 : if (ext->msg_thread) {
626 0 : g_thread_join (ext->msg_thread);
627 0 : ext->msg_thread = NULL;
628 : }
629 :
630 0 : if (ext->msg_queue) {
631 0 : g_async_queue_unref (ext->msg_queue);
632 0 : ext->msg_queue = NULL;
633 : }
634 :
635 0 : if (ext->single) {
636 0 : ml_single_close (ext->single);
637 0 : ext->single = NULL;
638 : }
639 :
640 0 : if (ext->pipeline) {
641 0 : ml_pipeline_stop (ext->pipeline);
642 0 : ml_pipeline_destroy (ext->pipeline);
643 0 : ext->pipeline = NULL;
644 : }
645 :
646 0 : if (ext->node_table) {
647 0 : g_hash_table_destroy (ext->node_table);
648 0 : ext->node_table = NULL;
649 : }
650 :
651 0 : g_free (ext);
652 0 : mls->priv = NULL;
653 :
654 0 : return ML_ERROR_NONE;
655 : }
656 :
657 : /**
658 : * @brief Internal function to start ml-service extension.
659 : */
660 : int
661 0 : _ml_service_extension_start (ml_service_s * mls)
662 : {
663 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
664 0 : int status = ML_ERROR_NONE;
665 :
666 0 : switch (ext->type) {
667 0 : case ML_EXTENSION_TYPE_PIPELINE:
668 0 : status = ml_pipeline_start (ext->pipeline);
669 0 : break;
670 0 : case ML_EXTENSION_TYPE_SINGLE:
671 : /* Do nothing. */
672 0 : break;
673 0 : default:
674 0 : status = ML_ERROR_NOT_SUPPORTED;
675 0 : break;
676 : }
677 :
678 0 : return status;
679 : }
680 :
681 : /**
682 : * @brief Internal function to stop ml-service extension.
683 : */
684 : int
685 0 : _ml_service_extension_stop (ml_service_s * mls)
686 : {
687 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
688 0 : int status = ML_ERROR_NONE;
689 :
690 0 : switch (ext->type) {
691 0 : case ML_EXTENSION_TYPE_PIPELINE:
692 0 : status = ml_pipeline_stop (ext->pipeline);
693 0 : break;
694 0 : case ML_EXTENSION_TYPE_SINGLE:
695 : /* Do nothing. */
696 0 : break;
697 0 : default:
698 0 : status = ML_ERROR_NOT_SUPPORTED;
699 0 : break;
700 : }
701 :
702 0 : return status;
703 : }
704 :
705 : /**
706 : * @brief Internal function to get the information of required input data.
707 : */
708 : int
709 0 : _ml_service_extension_get_input_information (ml_service_s * mls,
710 : const char *name, ml_tensors_info_h * info)
711 : {
712 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
713 : int status;
714 :
715 0 : switch (ext->type) {
716 0 : case ML_EXTENSION_TYPE_SINGLE:
717 0 : status = ml_single_get_input_info (ext->single, info);
718 0 : break;
719 0 : case ML_EXTENSION_TYPE_PIPELINE:
720 : {
721 : ml_service_node_info_s *node_info;
722 :
723 0 : node_info = _ml_extension_node_info_get (ext, name);
724 :
725 0 : if (node_info && node_info->type == ML_SERVICE_NODE_TYPE_INPUT) {
726 0 : status = _ml_tensors_info_create_from (node_info->info, info);
727 : } else {
728 0 : status = ML_ERROR_INVALID_PARAMETER;
729 : }
730 0 : break;
731 : }
732 0 : default:
733 0 : status = ML_ERROR_NOT_SUPPORTED;
734 0 : break;
735 : }
736 :
737 0 : return status;
738 : }
739 :
740 : /**
741 : * @brief Internal function to get the information of output data.
742 : */
743 : int
744 0 : _ml_service_extension_get_output_information (ml_service_s * mls,
745 : const char *name, ml_tensors_info_h * info)
746 : {
747 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
748 : int status;
749 :
750 0 : switch (ext->type) {
751 0 : case ML_EXTENSION_TYPE_SINGLE:
752 0 : status = ml_single_get_output_info (ext->single, info);
753 0 : break;
754 0 : case ML_EXTENSION_TYPE_PIPELINE:
755 : {
756 : ml_service_node_info_s *node_info;
757 :
758 0 : node_info = _ml_extension_node_info_get (ext, name);
759 :
760 0 : if (node_info && node_info->type == ML_SERVICE_NODE_TYPE_OUTPUT) {
761 0 : status = _ml_tensors_info_create_from (node_info->info, info);
762 : } else {
763 0 : status = ML_ERROR_INVALID_PARAMETER;
764 : }
765 0 : break;
766 : }
767 0 : default:
768 0 : status = ML_ERROR_NOT_SUPPORTED;
769 0 : break;
770 : }
771 :
772 0 : if (status != ML_ERROR_NONE) {
773 0 : if (*info) {
774 0 : ml_tensors_info_destroy (*info);
775 0 : *info = NULL;
776 : }
777 : }
778 :
779 0 : return status;
780 : }
781 :
782 : /**
783 : * @brief Internal function to set the information for ml-service extension.
784 : */
785 : int
786 0 : _ml_service_extension_set_information (ml_service_s * mls, const char *name,
787 : const char *value)
788 : {
789 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
790 :
791 : /* Check limitation of message queue and other options. */
792 0 : if (g_ascii_strcasecmp (name, "input_queue_size") == 0 ||
793 0 : g_ascii_strcasecmp (name, "max_input") == 0) {
794 0 : ext->max_input = (guint) g_ascii_strtoull (value, NULL, 10);
795 0 : } else if (g_ascii_strcasecmp (name, "timeout") == 0) {
796 0 : ext->timeout = (guint) g_ascii_strtoull (value, NULL, 10);
797 : }
798 :
799 0 : return ML_ERROR_NONE;
800 : }
801 :
802 : /**
803 : * @brief Internal function to add an input data to process the model in ml-service extension handle.
804 : */
805 : int
806 0 : _ml_service_extension_request (ml_service_s * mls, const char *name,
807 : const ml_tensors_data_h data)
808 : {
809 0 : ml_extension_s *ext = (ml_extension_s *) mls->priv;
810 : ml_extension_msg_s *msg;
811 : int status, len;
812 :
813 0 : if (ext->type == ML_EXTENSION_TYPE_PIPELINE) {
814 : ml_service_node_info_s *node_info;
815 :
816 0 : if (!STR_IS_VALID (name)) {
817 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
818 : "The parameter, name '%s', is invalid.", name);
819 : }
820 :
821 0 : node_info = _ml_extension_node_info_get (ext, name);
822 :
823 0 : if (!node_info || node_info->type != ML_SERVICE_NODE_TYPE_INPUT) {
824 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
825 : "The parameter, name '%s', is invalid, cannot find the input node from pipeline.",
826 : name);
827 : }
828 : }
829 :
830 0 : len = g_async_queue_length (ext->msg_queue);
831 :
832 0 : if (ext->max_input > 0 && len > 0 && ext->max_input <= len) {
833 0 : _ml_error_report_return (ML_ERROR_STREAMS_PIPE,
834 : "Failed to push input data into the queue, the max number of input is %u.",
835 : ext->max_input);
836 : }
837 :
838 0 : msg = g_try_new0 (ml_extension_msg_s, 1);
839 0 : if (!msg) {
840 0 : _ml_error_report_return (ML_ERROR_OUT_OF_MEMORY,
841 : "Failed to allocate the ml-service extension message. Out of memory?");
842 : }
843 :
844 0 : msg->name = g_strdup (name);
845 0 : status = ml_tensors_data_clone (data, &msg->input);
846 :
847 0 : if (status != ML_ERROR_NONE) {
848 0 : _ml_extension_msg_free (msg);
849 0 : _ml_error_report_return (status, "Failed to clone input data.");
850 : }
851 :
852 0 : g_async_queue_push (ext->msg_queue, msg);
853 :
854 0 : return ML_ERROR_NONE;
855 : }
|