Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
4 : *
5 : * @file ml-api-inference-single.c
6 : * @date 29 Aug 2019
7 : * @brief NNStreamer/Single C-API Wrapper.
8 : * This allows to invoke individual input frame with NNStreamer.
9 : * @see https://github.com/nnstreamer/nnstreamer
10 : * @author MyungJoo Ham <myungjoo.ham@samsung.com>
11 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
12 : * @bug No known bugs except for NYI items
13 : */
14 :
15 : #include <string.h>
16 : #include <nnstreamer-single.h>
17 : #include <nnstreamer-tizen-internal.h> /* Tizen platform header */
18 : #include <nnstreamer_internal.h>
19 : #include <nnstreamer_plugin_api_util.h>
20 : #include <tensor_filter_single.h>
21 :
22 : #include "ml-api-inference-internal.h"
23 : #include "ml-api-internal.h"
24 : #include "ml-api-inference-single-internal.h"
25 :
26 : #define ML_SINGLE_MAGIC 0xfeedfeed
27 :
28 : /**
29 : * @brief Default time to wait for an output in milliseconds (0 will wait for the output).
30 : */
31 : #define SINGLE_DEFAULT_TIMEOUT 0
32 :
33 : /**
34 : * @brief Global lock for single shot API
35 : * @detail This lock ensures that ml_single_close is thread safe. All other API
36 : * functions use the mutex from the single handle. However for close,
37 : * single handle mutex cannot be used as single handle is destroyed at
38 : * close
39 : * @note This mutex is automatically initialized as it is statically declared
40 : */
41 : G_LOCK_DEFINE_STATIC (magic);
42 :
43 : /**
44 : * @brief Get valid handle after magic verification
45 : * @note handle's mutex (single_h->mutex) is acquired after this
46 : * @param[out] single_h The handle properly casted: (ml_single *).
47 : * @param[in] single The handle to be validated: (void *).
48 : * @param[in] reset Set TRUE if the handle is to be reset (magic = 0).
49 : */
50 : #define ML_SINGLE_GET_VALID_HANDLE_LOCKED(single_h, single, reset) do { \
51 : G_LOCK (magic); \
52 : single_h = (ml_single *) single; \
53 : if (G_UNLIKELY(single_h->magic != ML_SINGLE_MAGIC)) { \
54 : _ml_error_report \
55 : ("The given param, %s (ml_single_h), is invalid. It is not a single_h instance or the user thread has modified it.", \
56 : #single); \
57 : G_UNLOCK (magic); \
58 : return ML_ERROR_INVALID_PARAMETER; \
59 : } \
60 : if (G_UNLIKELY(reset)) \
61 : single_h->magic = 0; \
62 : g_mutex_lock (&single_h->mutex); \
63 : G_UNLOCK (magic); \
64 : } while (0)
65 :
66 : /**
67 : * @brief This is for the symmetricity with ML_SINGLE_GET_VALID_HANDLE_LOCKED
68 : * @param[in] single_h The casted handle (ml_single *).
69 : */
70 : #define ML_SINGLE_HANDLE_UNLOCK(single_h) g_mutex_unlock (&single_h->mutex);
71 :
72 : /** define string names for input/output */
73 : #define INPUT_STR "input"
74 : #define OUTPUT_STR "output"
75 : #define TYPE_STR "type"
76 : #define NAME_STR "name"
77 :
78 : /** concat string from #define */
79 : #define CONCAT_MACRO_STR(STR1,STR2) STR1 STR2
80 :
81 : /** States for invoke thread */
82 : typedef enum
83 : {
84 : IDLE = 0, /**< ready to accept next input */
85 : RUNNING, /**< running an input, cannot accept more input */
86 : JOIN_REQUESTED /**< should join the thread, will exit soon */
87 : } thread_state;
88 :
89 : /**
90 : * @brief The name of sub-plugin for defined neural net frameworks.
91 : * @note The sub-plugin for Android is not declared (e.g., snap)
92 : */
93 : static const char *ml_nnfw_subplugin_name[] = {
94 : [ML_NNFW_TYPE_ANY] = "any", /* DO NOT use this name ('any') to get the sub-plugin */
95 : [ML_NNFW_TYPE_CUSTOM_FILTER] = "custom",
96 : [ML_NNFW_TYPE_TENSORFLOW_LITE] = "tensorflow-lite",
97 : [ML_NNFW_TYPE_TENSORFLOW] = "tensorflow",
98 : [ML_NNFW_TYPE_NNFW] = "nnfw",
99 : [ML_NNFW_TYPE_MVNC] = "movidius-ncsdk2",
100 : [ML_NNFW_TYPE_OPENVINO] = "openvino",
101 : [ML_NNFW_TYPE_VIVANTE] = "vivante",
102 : [ML_NNFW_TYPE_EDGE_TPU] = "edgetpu",
103 : [ML_NNFW_TYPE_ARMNN] = "armnn",
104 : [ML_NNFW_TYPE_SNPE] = "snpe",
105 : [ML_NNFW_TYPE_PYTORCH] = "pytorch",
106 : [ML_NNFW_TYPE_NNTR_INF] = "nntrainer",
107 : [ML_NNFW_TYPE_VD_AIFW] = "vd_aifw",
108 : [ML_NNFW_TYPE_TRIX_ENGINE] = "trix-engine",
109 : [ML_NNFW_TYPE_MXNET] = "mxnet",
110 : [ML_NNFW_TYPE_TVM] = "tvm",
111 : [ML_NNFW_TYPE_ONNX_RUNTIME] = "onnxruntime",
112 : [ML_NNFW_TYPE_NCNN] = "ncnn",
113 : [ML_NNFW_TYPE_TENSORRT] = "tensorrt",
114 : [ML_NNFW_TYPE_QNN] = "qnn",
115 : NULL
116 : };
117 :
118 : /** ML single api data structure for handle */
119 : typedef struct
120 : {
121 : GTensorFilterSingleClass *klass; /**< tensor filter class structure*/
122 : GTensorFilterSingle *filter; /**< tensor filter element */
123 : GstTensorsInfo in_info; /**< info about input */
124 : GstTensorsInfo out_info; /**< info about output */
125 : ml_nnfw_type_e nnfw; /**< nnfw type for this filter */
126 : guint magic; /**< code to verify valid handle */
127 :
128 : GThread *thread; /**< thread for invoking */
129 : GMutex mutex; /**< mutex for synchronization */
130 : GCond cond; /**< condition for synchronization */
131 : ml_tensors_data_h input; /**< input received from user */
132 : ml_tensors_data_h output; /**< output to be sent back to user */
133 : guint timeout; /**< timeout for invoking */
134 : thread_state state; /**< current state of the thread */
135 : gboolean free_output; /**< true if output tensors are allocated in single-shot */
136 : int status; /**< status of processing */
137 : gboolean invoking; /**< invoke running flag */
138 : ml_tensors_data_h in_tensors; /**< input tensor wrapper for processing */
139 : ml_tensors_data_h out_tensors; /**< output tensor wrapper for processing */
140 :
141 : GList *destroy_data_list; /**< data to be freed by filter */
142 : } ml_single;
143 :
144 : /**
145 : * @brief Internal function to get the nnfw type.
146 : */
147 : ml_nnfw_type_e
148 0 : _ml_get_nnfw_type_by_subplugin_name (const char *name)
149 : {
150 0 : ml_nnfw_type_e nnfw_type = ML_NNFW_TYPE_ANY;
151 0 : int idx = -1;
152 :
153 0 : if (name == NULL)
154 0 : return ML_NNFW_TYPE_ANY;
155 :
156 0 : idx = find_key_strv (ml_nnfw_subplugin_name, name);
157 0 : if (idx < 0) {
158 : /* check sub-plugin for android */
159 0 : if (g_ascii_strcasecmp (name, "snap") == 0)
160 0 : nnfw_type = ML_NNFW_TYPE_SNAP;
161 : else
162 0 : _ml_error_report ("Cannot find nnfw, %s is an invalid name.",
163 : _STR_NULL (name));
164 : } else {
165 0 : nnfw_type = (ml_nnfw_type_e) idx;
166 : }
167 :
168 0 : return nnfw_type;
169 : }
170 :
171 : /**
172 : * @brief Internal function to get the sub-plugin name.
173 : */
174 : const char *
175 0 : _ml_get_nnfw_subplugin_name (ml_nnfw_type_e nnfw)
176 : {
177 : /* check sub-plugin for android */
178 0 : if (nnfw == ML_NNFW_TYPE_SNAP)
179 0 : return "snap";
180 :
181 0 : return ml_nnfw_subplugin_name[nnfw];
182 : }
183 :
184 : /**
185 : * @brief Convert c-api based hw to internal representation
186 : */
187 : accl_hw
188 0 : _ml_nnfw_to_accl_hw (const ml_nnfw_hw_e hw)
189 : {
190 0 : switch (hw) {
191 0 : case ML_NNFW_HW_ANY:
192 0 : return ACCL_DEFAULT;
193 0 : case ML_NNFW_HW_AUTO:
194 0 : return ACCL_AUTO;
195 0 : case ML_NNFW_HW_CPU:
196 0 : return ACCL_CPU;
197 : #if defined (__aarch64__) || defined (__arm__)
198 0 : case ML_NNFW_HW_CPU_NEON:
199 0 : return ACCL_CPU_NEON;
200 : #else
201 : case ML_NNFW_HW_CPU_SIMD:
202 : return ACCL_CPU_SIMD;
203 : #endif
204 0 : case ML_NNFW_HW_GPU:
205 0 : return ACCL_GPU;
206 0 : case ML_NNFW_HW_NPU:
207 0 : return ACCL_NPU;
208 0 : case ML_NNFW_HW_NPU_MOVIDIUS:
209 0 : return ACCL_NPU_MOVIDIUS;
210 0 : case ML_NNFW_HW_NPU_EDGE_TPU:
211 0 : return ACCL_NPU_EDGE_TPU;
212 0 : case ML_NNFW_HW_NPU_VIVANTE:
213 0 : return ACCL_NPU_VIVANTE;
214 0 : case ML_NNFW_HW_NPU_SLSI:
215 0 : return ACCL_NPU_SLSI;
216 0 : case ML_NNFW_HW_NPU_SR:
217 : /** @todo how to get srcn npu */
218 0 : return ACCL_NPU_SR;
219 0 : default:
220 0 : return ACCL_AUTO;
221 : }
222 : }
223 :
224 : /**
225 : * @brief Checks the availability of the given execution environments with custom option.
226 : */
227 : int
228 0 : ml_check_nnfw_availability_full (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw,
229 : const char *custom, bool *available)
230 : {
231 0 : const char *fw_name = NULL;
232 :
233 0 : check_feature_state (ML_FEATURE_INFERENCE);
234 :
235 0 : if (!available)
236 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
237 : "The parameter, available (bool *), is NULL. It should be a valid pointer of bool. E.g., bool a; ml_check_nnfw_availability_full (..., &a);");
238 :
239 : /* init false */
240 0 : *available = false;
241 :
242 0 : if (nnfw == ML_NNFW_TYPE_ANY)
243 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
244 : "The parameter, nnfw (ml_nnfw_type_e), is ML_NNFW_TYPE_ANY. It should specify the framework to be probed for the hardware availability.");
245 :
246 0 : fw_name = _ml_get_nnfw_subplugin_name (nnfw);
247 :
248 0 : if (fw_name) {
249 0 : if (nnstreamer_filter_find (fw_name) != NULL) {
250 0 : accl_hw accl = _ml_nnfw_to_accl_hw (hw);
251 :
252 0 : if (gst_tensor_filter_check_hw_availability (fw_name, accl, custom)) {
253 0 : *available = true;
254 : } else {
255 0 : _ml_logi ("%s is supported but not with the specified hardware.",
256 : fw_name);
257 : }
258 : } else {
259 0 : _ml_logi ("%s is not supported.", fw_name);
260 : }
261 : } else {
262 0 : _ml_logw ("Cannot get the name of sub-plugin for given nnfw.");
263 : }
264 :
265 0 : return ML_ERROR_NONE;
266 : }
267 :
268 : /**
269 : * @brief Checks the availability of the given execution environments.
270 : */
271 : int
272 0 : ml_check_nnfw_availability (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw,
273 : bool *available)
274 : {
275 0 : return ml_check_nnfw_availability_full (nnfw, hw, NULL, available);
276 : }
277 :
278 : /**
279 : * @brief setup input and output tensor memory to pass to the tensor_filter.
280 : * @note this tensor memory wrapper will be reused for each invoke.
281 : */
282 : static void
283 0 : __setup_in_out_tensors (ml_single * single_h)
284 : {
285 : guint i;
286 0 : ml_tensors_data_s *in_tensors = (ml_tensors_data_s *) single_h->in_tensors;
287 0 : ml_tensors_data_s *out_tensors = (ml_tensors_data_s *) single_h->out_tensors;
288 :
289 : /* Setup input buffer */
290 0 : if (in_tensors) {
291 0 : _ml_tensors_info_free (in_tensors->info);
292 0 : _ml_tensors_info_copy_from_gst (in_tensors->info, &single_h->in_info);
293 : } else {
294 : ml_tensors_info_h info;
295 :
296 0 : _ml_tensors_info_create_from_gst (&info, &single_h->in_info);
297 0 : _ml_tensors_data_create_no_alloc (info, &single_h->in_tensors);
298 :
299 0 : ml_tensors_info_destroy (info);
300 0 : in_tensors = (ml_tensors_data_s *) single_h->in_tensors;
301 : }
302 :
303 0 : in_tensors->num_tensors = single_h->in_info.num_tensors;
304 0 : for (i = 0; i < in_tensors->num_tensors; i++) {
305 : /** memory will be allocated by tensor_filter_single */
306 0 : in_tensors->tensors[i].data = NULL;
307 0 : in_tensors->tensors[i].size =
308 0 : gst_tensors_info_get_size (&single_h->in_info, i);
309 : }
310 :
311 : /* Setup output buffer */
312 0 : if (out_tensors) {
313 0 : _ml_tensors_info_free (out_tensors->info);
314 0 : _ml_tensors_info_copy_from_gst (out_tensors->info, &single_h->out_info);
315 : } else {
316 : ml_tensors_info_h info;
317 :
318 0 : _ml_tensors_info_create_from_gst (&info, &single_h->out_info);
319 0 : _ml_tensors_data_create_no_alloc (info, &single_h->out_tensors);
320 :
321 0 : ml_tensors_info_destroy (info);
322 0 : out_tensors = (ml_tensors_data_s *) single_h->out_tensors;
323 : }
324 :
325 0 : out_tensors->num_tensors = single_h->out_info.num_tensors;
326 0 : for (i = 0; i < out_tensors->num_tensors; i++) {
327 : /** memory will be allocated by tensor_filter_single */
328 0 : out_tensors->tensors[i].data = NULL;
329 0 : out_tensors->tensors[i].size =
330 0 : gst_tensors_info_get_size (&single_h->out_info, i);
331 : }
332 0 : }
333 :
334 : /**
335 : * @brief To call the framework to destroy the allocated output data
336 : */
337 : static inline void
338 0 : __destroy_notify (gpointer data_h, gpointer single_data)
339 : {
340 : ml_single *single_h;
341 : ml_tensors_data_s *data;
342 :
343 0 : data = (ml_tensors_data_s *) data_h;
344 0 : single_h = (ml_single *) single_data;
345 :
346 0 : if (G_LIKELY (single_h->filter)) {
347 0 : if (single_h->klass->allocate_in_invoke (single_h->filter)) {
348 0 : single_h->klass->destroy_notify (single_h->filter, data->tensors);
349 : }
350 : }
351 :
352 : /* reset callback function */
353 0 : data->destroy = NULL;
354 0 : }
355 :
356 : /**
357 : * @brief Wrapper function for __destroy_notify
358 : */
359 : static int
360 0 : ml_single_destroy_notify_cb (void *handle, void *user_data)
361 : {
362 0 : ml_tensors_data_h data = (ml_tensors_data_h) handle;
363 0 : ml_single_h single = (ml_single_h) user_data;
364 : ml_single *single_h;
365 0 : int status = ML_ERROR_NONE;
366 :
367 0 : if (G_UNLIKELY (!single))
368 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
369 : "Failed to destroy data buffer. Callback function argument from _ml_tensors_data_destroy_internal is invalid. The given 'user_data' is NULL. It appears to be an internal error of ML-API or the user thread has touched private data structure.");
370 0 : if (G_UNLIKELY (!data))
371 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
372 : "Failed to destroy data buffer. Callback function argument from _ml_tensors_data_destroy_internal is invalid. The given 'handle' is NULL. It appears to be an internal error of ML-API or the user thread has touched private data structure.");
373 :
374 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
375 :
376 0 : if (G_UNLIKELY (!single_h->filter)) {
377 0 : status = ML_ERROR_INVALID_PARAMETER;
378 0 : _ml_error_report
379 : ("Failed to destroy the data buffer. The handle instance (single_h) is invalid. It appears to be an internal error of ML-API of the user thread has touched private data structure.");
380 0 : goto exit;
381 : }
382 :
383 0 : single_h->destroy_data_list =
384 0 : g_list_remove (single_h->destroy_data_list, data);
385 0 : __destroy_notify (data, single_h);
386 :
387 0 : exit:
388 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
389 :
390 0 : return status;
391 : }
392 :
393 : /**
394 : * @brief setup the destroy notify for the allocated output data.
395 : * @note this stores the data entry in the single list.
396 : * @note this has not overhead if the allocation of output is not performed by
397 : * the framework but by tensor filter element.
398 : */
399 : static void
400 0 : set_destroy_notify (ml_single * single_h, ml_tensors_data_s * data,
401 : gboolean add)
402 : {
403 0 : if (single_h->klass->allocate_in_invoke (single_h->filter)) {
404 0 : data->destroy = ml_single_destroy_notify_cb;
405 0 : data->user_data = single_h;
406 0 : add = TRUE;
407 : }
408 :
409 0 : if (add) {
410 0 : single_h->destroy_data_list = g_list_append (single_h->destroy_data_list,
411 : (gpointer) data);
412 : }
413 0 : }
414 :
415 : /**
416 : * @brief Internal function to call subplugin's invoke
417 : */
418 : static inline int
419 0 : __invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out,
420 : gboolean alloc_output)
421 : {
422 : ml_tensors_data_s *in_data, *out_data;
423 0 : int status = ML_ERROR_NONE;
424 :
425 0 : in_data = (ml_tensors_data_s *) in;
426 0 : out_data = (ml_tensors_data_s *) out;
427 :
428 : /* Prevent error case when input or output is null in invoke thread. */
429 0 : if (!in_data || !out_data) {
430 0 : _ml_error_report ("Failed to invoke a model, invalid data handle.");
431 0 : return ML_ERROR_STREAMS_PIPE;
432 : }
433 :
434 : /* Invoke the thread. */
435 0 : if (!single_h->klass->invoke (single_h->filter, in_data->tensors,
436 0 : out_data->tensors, alloc_output)) {
437 0 : const char *fw_name = _ml_get_nnfw_subplugin_name (single_h->nnfw);
438 0 : _ml_error_report
439 : ("Failed to invoke the tensors. The invoke callback of the tensor-filter subplugin '%s' has failed. Please contact the author of tensor-filter-%s (nnstreamer-%s) or review its source code. Note that this usually happens when the designated framework does not support the given model (e.g., trying to run tf-lite 2.6 model with tf-lite 1.13).",
440 : fw_name, fw_name, fw_name);
441 0 : status = ML_ERROR_STREAMS_PIPE;
442 : }
443 :
444 0 : return status;
445 : }
446 :
447 : /**
448 : * @brief Internal function to post-process given output.
449 : * @note Do not call this if single_h->free_output is false (output data is not allocated in single-shot).
450 : */
451 : static inline void
452 0 : __process_output (ml_single * single_h, ml_tensors_data_h output)
453 : {
454 : ml_tensors_data_s *out_data;
455 :
456 0 : if (g_list_find (single_h->destroy_data_list, output)) {
457 : /**
458 : * Caller of the invoke thread has returned back with timeout.
459 : * So, free the memory allocated by the invoke as their is no receiver.
460 : */
461 0 : single_h->destroy_data_list =
462 0 : g_list_remove (single_h->destroy_data_list, output);
463 0 : ml_tensors_data_destroy (output);
464 : } else {
465 0 : out_data = (ml_tensors_data_s *) output;
466 0 : set_destroy_notify (single_h, out_data, FALSE);
467 : }
468 0 : }
469 :
470 : /**
471 : * @brief thread to execute calls to invoke
472 : *
473 : * @details The thread behavior is detailed as below:
474 : * - Starting with IDLE state, the thread waits for an input or change
475 : * in state externally.
476 : * - If state is not RUNNING, exit this thread, else process the
477 : * request.
478 : * - Process input, call invoke, process output. Any error in this
479 : * state sets the status to be used by ml_single_invoke().
480 : * - State is set back to IDLE and thread moves back to start.
481 : *
482 : * State changes performed by this function when:
483 : * RUNNING -> IDLE - processing is finished.
484 : * JOIN_REQUESTED -> IDLE - close is requested.
485 : *
486 : * @note Error while processing an input is provided back to requesting
487 : * function, and further processing of invoke_thread is not affected.
488 : */
489 : static void *
490 0 : invoke_thread (void *arg)
491 : {
492 : ml_single *single_h;
493 : ml_tensors_data_h input, output;
494 0 : gboolean alloc_output = FALSE;
495 :
496 0 : single_h = (ml_single *) arg;
497 :
498 0 : g_mutex_lock (&single_h->mutex);
499 :
500 0 : while (single_h->state <= RUNNING) {
501 0 : int status = ML_ERROR_NONE;
502 :
503 : /** wait for data */
504 0 : while (single_h->state != RUNNING) {
505 0 : g_cond_wait (&single_h->cond, &single_h->mutex);
506 0 : if (single_h->state == JOIN_REQUESTED)
507 0 : goto exit;
508 : }
509 :
510 0 : input = single_h->input;
511 0 : output = single_h->output;
512 : /* Set null to prevent double-free. */
513 0 : single_h->input = single_h->output = NULL;
514 :
515 0 : single_h->invoking = TRUE;
516 0 : alloc_output = single_h->free_output;
517 0 : g_mutex_unlock (&single_h->mutex);
518 0 : status = __invoke (single_h, input, output, alloc_output);
519 0 : g_mutex_lock (&single_h->mutex);
520 : /* Clear input data after invoke is done. */
521 0 : ml_tensors_data_destroy (input);
522 0 : single_h->invoking = FALSE;
523 :
524 0 : if (status != ML_ERROR_NONE || single_h->state == JOIN_REQUESTED) {
525 0 : if (alloc_output) {
526 0 : single_h->destroy_data_list =
527 0 : g_list_remove (single_h->destroy_data_list, output);
528 0 : ml_tensors_data_destroy (output);
529 : }
530 :
531 0 : if (single_h->state == JOIN_REQUESTED)
532 0 : goto exit;
533 0 : goto wait_for_next;
534 : }
535 :
536 0 : if (alloc_output)
537 0 : __process_output (single_h, output);
538 :
539 : /** loop over to wait for the next element */
540 0 : wait_for_next:
541 0 : single_h->status = status;
542 0 : if (single_h->state == RUNNING)
543 0 : single_h->state = IDLE;
544 0 : g_cond_broadcast (&single_h->cond);
545 : }
546 :
547 0 : exit:
548 : /* Do not set IDLE if JOIN_REQUESTED */
549 0 : if (single_h->state == JOIN_REQUESTED) {
550 : /* Release input and output data */
551 0 : if (single_h->input)
552 0 : ml_tensors_data_destroy (single_h->input);
553 :
554 0 : if (alloc_output && single_h->output) {
555 0 : single_h->destroy_data_list =
556 0 : g_list_remove (single_h->destroy_data_list, single_h->output);
557 0 : ml_tensors_data_destroy (single_h->output);
558 : }
559 :
560 0 : single_h->input = single_h->output = NULL;
561 0 : } else if (single_h->state == RUNNING)
562 0 : single_h->state = IDLE;
563 0 : g_mutex_unlock (&single_h->mutex);
564 0 : return NULL;
565 : }
566 :
567 : /**
568 : * @brief Sets the information (tensor dimension, type, name and so on) of required input data for the given model, and get updated output data information.
569 : * @details Note that a model/framework may not support setting such information.
570 : * @since_tizen 6.0
571 : * @param[in] single The model handle.
572 : * @param[in] in_info The handle of input tensors information.
573 : * @param[out] out_info The handle of output tensors information. The caller is responsible for freeing the information with ml_tensors_info_destroy().
574 : * @return @c 0 on success. Otherwise a negative error value.
575 : * @retval #ML_ERROR_NONE Successful
576 : * @retval #ML_ERROR_NOT_SUPPORTED This implies that the given framework does not support dynamic dimensions.
577 : * Use ml_single_get_input_info() and ml_single_get_output_info() instead for this framework.
578 : * @retval #ML_ERROR_INVALID_PARAMETER Fail. The parameter is invalid.
579 : */
580 : static int
581 0 : ml_single_update_info (ml_single_h single,
582 : const ml_tensors_info_h in_info, ml_tensors_info_h * out_info)
583 : {
584 0 : if (!single)
585 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
586 : "The parameter, single (ml_single_h), is NULL. It should be a valid ml_single_h instance, usually created by ml_single_open().");
587 0 : if (!in_info)
588 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
589 : "The parameter, in_info (const ml_tensors_info_h), is NULL. It should be a valid instance of ml_tensors_info_h, usually created by ml_tensors_info_create() and configured by the application.");
590 0 : if (!out_info)
591 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
592 : "The parameter, out_info (ml_tensors_info_h *), is NULL. It should be a valid pointer to an instance ml_tensors_info_h, usually created by ml_tensors_info_h(). Note that out_info is supposed to be overwritten by this API call.");
593 :
594 : /* init null */
595 0 : *out_info = NULL;
596 :
597 0 : _ml_error_report_return_continue_iferr (ml_single_set_input_info (single,
598 : in_info),
599 : "Configuring the neural network model with the given input information has failed with %d error code. The given input information ('in_info' parameter) might be invalid or the given neural network cannot accept it as its input data.",
600 : _ERRNO);
601 :
602 0 : __setup_in_out_tensors (single);
603 0 : _ml_error_report_return_continue_iferr (ml_single_get_output_info (single,
604 : out_info),
605 : "Fetching output info after configuring input information has failed with %d error code.",
606 : _ERRNO);
607 :
608 0 : return ML_ERROR_NONE;
609 : }
610 :
611 : /**
612 : * @brief Internal function to get the gst info from tensor-filter.
613 : */
614 : static void
615 0 : ml_single_get_gst_info (ml_single * single_h, gboolean is_input,
616 : GstTensorsInfo * gst_info)
617 : {
618 : const gchar *prop_prefix, *prop_name, *prop_type;
619 : gchar *val;
620 : guint num;
621 :
622 0 : if (is_input) {
623 0 : prop_prefix = INPUT_STR;
624 0 : prop_type = CONCAT_MACRO_STR (INPUT_STR, TYPE_STR);
625 0 : prop_name = CONCAT_MACRO_STR (INPUT_STR, NAME_STR);
626 : } else {
627 0 : prop_prefix = OUTPUT_STR;
628 0 : prop_type = CONCAT_MACRO_STR (OUTPUT_STR, TYPE_STR);
629 0 : prop_name = CONCAT_MACRO_STR (OUTPUT_STR, NAME_STR);
630 : }
631 :
632 0 : gst_tensors_info_init (gst_info);
633 :
634 : /* get dimensions */
635 0 : g_object_get (single_h->filter, prop_prefix, &val, NULL);
636 0 : num = gst_tensors_info_parse_dimensions_string (gst_info, val);
637 0 : g_free (val);
638 :
639 : /* set the number of tensors */
640 0 : gst_info->num_tensors = num;
641 :
642 : /* get types */
643 0 : g_object_get (single_h->filter, prop_type, &val, NULL);
644 0 : num = gst_tensors_info_parse_types_string (gst_info, val);
645 0 : g_free (val);
646 :
647 0 : if (gst_info->num_tensors != num) {
648 0 : _ml_logw ("The number of tensor type is mismatched in filter.");
649 : }
650 :
651 : /* get names */
652 0 : g_object_get (single_h->filter, prop_name, &val, NULL);
653 0 : num = gst_tensors_info_parse_names_string (gst_info, val);
654 0 : g_free (val);
655 :
656 0 : if (gst_info->num_tensors != num) {
657 0 : _ml_logw ("The number of tensor name is mismatched in filter.");
658 : }
659 0 : }
660 :
661 : /**
662 : * @brief Internal function to set the gst info in tensor-filter.
663 : */
664 : static int
665 0 : ml_single_set_gst_info (ml_single * single_h, const GstTensorsInfo * in_info)
666 : {
667 : GstTensorsInfo out_info;
668 0 : int status = ML_ERROR_NONE;
669 0 : int ret = -EINVAL;
670 :
671 0 : gst_tensors_info_init (&out_info);
672 0 : ret = single_h->klass->set_input_info (single_h->filter, in_info, &out_info);
673 0 : if (ret == 0) {
674 0 : gst_tensors_info_free (&single_h->in_info);
675 0 : gst_tensors_info_free (&single_h->out_info);
676 0 : gst_tensors_info_copy (&single_h->in_info, in_info);
677 0 : gst_tensors_info_copy (&single_h->out_info, &out_info);
678 :
679 0 : __setup_in_out_tensors (single_h);
680 0 : } else if (ret == -ENOENT) {
681 0 : status = ML_ERROR_NOT_SUPPORTED;
682 : } else {
683 0 : status = ML_ERROR_INVALID_PARAMETER;
684 : }
685 :
686 0 : gst_tensors_info_free (&out_info);
687 :
688 0 : return status;
689 : }
690 :
691 : /**
692 : * @brief Set the info for input/output tensors
693 : */
694 : static int
695 0 : ml_single_set_inout_tensors_info (GObject * object,
696 : const gboolean is_input, ml_tensors_info_s * tensors_info)
697 : {
698 0 : int status = ML_ERROR_NONE;
699 : GstTensorsInfo info;
700 : gchar *str_dim, *str_type, *str_name;
701 : const gchar *str_type_name, *str_name_name;
702 : const gchar *prefix;
703 :
704 0 : if (is_input) {
705 0 : prefix = INPUT_STR;
706 0 : str_type_name = CONCAT_MACRO_STR (INPUT_STR, TYPE_STR);
707 0 : str_name_name = CONCAT_MACRO_STR (INPUT_STR, NAME_STR);
708 : } else {
709 0 : prefix = OUTPUT_STR;
710 0 : str_type_name = CONCAT_MACRO_STR (OUTPUT_STR, TYPE_STR);
711 0 : str_name_name = CONCAT_MACRO_STR (OUTPUT_STR, NAME_STR);
712 : }
713 :
714 0 : _ml_error_report_return_continue_iferr
715 : (_ml_tensors_info_copy_from_ml (&info, tensors_info),
716 : "Cannot fetch tensor-info from the given information. Error code: %d",
717 : _ERRNO);
718 :
719 : /* Set input option */
720 0 : str_dim = gst_tensors_info_get_dimensions_string (&info);
721 0 : str_type = gst_tensors_info_get_types_string (&info);
722 0 : str_name = gst_tensors_info_get_names_string (&info);
723 :
724 0 : if (!str_dim || !str_type || !str_name) {
725 0 : if (!str_dim)
726 0 : _ml_error_report
727 : ("Cannot fetch specific tensor-info from the given information: cannot fetch tensor dimension information.");
728 0 : if (!str_type)
729 0 : _ml_error_report
730 : ("Cannot fetch specific tensor-info from the given information: cannot fetch tensor type information.");
731 0 : if (!str_name)
732 0 : _ml_error_report
733 : ("Cannot fetch specific tensor-info from the given information: cannot fetch tensor name information. Even if tensor names are not defined, this should be able to fetch a list of empty strings.");
734 :
735 0 : status = ML_ERROR_INVALID_PARAMETER;
736 : } else {
737 0 : g_object_set (object, prefix, str_dim, str_type_name, str_type,
738 : str_name_name, str_name, NULL);
739 : }
740 :
741 0 : g_free (str_dim);
742 0 : g_free (str_type);
743 0 : g_free (str_name);
744 :
745 0 : gst_tensors_info_free (&info);
746 :
747 0 : return status;
748 : }
749 :
750 : /**
751 : * @brief Internal static function to set tensors info in the handle.
752 : */
753 : static gboolean
754 0 : ml_single_set_info_in_handle (ml_single_h single, gboolean is_input,
755 : ml_tensors_info_s * tensors_info)
756 : {
757 : int status;
758 : ml_single *single_h;
759 : GstTensorsInfo *dest;
760 0 : gboolean configured = FALSE;
761 0 : gboolean is_valid = FALSE;
762 : GObject *filter_obj;
763 :
764 0 : single_h = (ml_single *) single;
765 0 : filter_obj = G_OBJECT (single_h->filter);
766 :
767 0 : if (is_input) {
768 0 : dest = &single_h->in_info;
769 0 : configured = single_h->klass->input_configured (single_h->filter);
770 : } else {
771 0 : dest = &single_h->out_info;
772 0 : configured = single_h->klass->output_configured (single_h->filter);
773 : }
774 :
775 0 : if (configured) {
776 : /* get configured info and compare with input info */
777 : GstTensorsInfo gst_info;
778 0 : ml_tensors_info_h info = NULL;
779 :
780 0 : ml_single_get_gst_info (single_h, is_input, &gst_info);
781 0 : _ml_tensors_info_create_from_gst (&info, &gst_info);
782 :
783 0 : gst_tensors_info_free (&gst_info);
784 :
785 0 : if (tensors_info && !ml_tensors_info_is_equal (tensors_info, info)) {
786 : /* given input info is not matched with configured */
787 0 : ml_tensors_info_destroy (info);
788 0 : if (is_input) {
789 : /* try to update tensors info */
790 0 : status = ml_single_update_info (single, tensors_info, &info);
791 0 : if (status != ML_ERROR_NONE)
792 0 : goto done;
793 : } else {
794 0 : goto done;
795 : }
796 : }
797 :
798 0 : gst_tensors_info_free (dest);
799 0 : _ml_tensors_info_copy_from_ml (dest, info);
800 0 : ml_tensors_info_destroy (info);
801 0 : } else if (tensors_info) {
802 : status =
803 0 : ml_single_set_inout_tensors_info (filter_obj, is_input, tensors_info);
804 0 : if (status != ML_ERROR_NONE)
805 0 : goto done;
806 :
807 0 : gst_tensors_info_free (dest);
808 0 : _ml_tensors_info_copy_from_ml (dest, tensors_info);
809 : }
810 :
811 0 : is_valid = gst_tensors_info_validate (dest);
812 :
813 0 : done:
814 0 : return is_valid;
815 : }
816 :
817 : /**
818 : * @brief Internal function to create and initialize the single handle.
819 : */
820 : static ml_single *
821 0 : ml_single_create_handle (ml_nnfw_type_e nnfw)
822 : {
823 : ml_single *single_h;
824 : GError *error;
825 0 : gboolean created = FALSE;
826 :
827 0 : single_h = g_new0 (ml_single, 1);
828 0 : if (single_h == NULL)
829 0 : _ml_error_report_return (NULL,
830 : "Failed to allocate memory for the single_h handle. Out of memory?");
831 :
832 0 : single_h->filter = g_object_new (G_TYPE_TENSOR_FILTER_SINGLE, NULL);
833 0 : if (single_h->filter == NULL) {
834 0 : _ml_error_report
835 : ("Failed to create a new instance for filter. Out of memory?");
836 0 : g_free (single_h);
837 0 : return NULL;
838 : }
839 :
840 0 : single_h->magic = ML_SINGLE_MAGIC;
841 0 : single_h->timeout = SINGLE_DEFAULT_TIMEOUT;
842 0 : single_h->nnfw = nnfw;
843 0 : single_h->state = IDLE;
844 0 : single_h->thread = NULL;
845 0 : single_h->input = NULL;
846 0 : single_h->output = NULL;
847 0 : single_h->destroy_data_list = NULL;
848 0 : single_h->invoking = FALSE;
849 :
850 0 : gst_tensors_info_init (&single_h->in_info);
851 0 : gst_tensors_info_init (&single_h->out_info);
852 0 : g_mutex_init (&single_h->mutex);
853 0 : g_cond_init (&single_h->cond);
854 :
855 0 : single_h->klass = g_type_class_ref (G_TYPE_TENSOR_FILTER_SINGLE);
856 0 : if (single_h->klass == NULL) {
857 0 : _ml_error_report
858 : ("Failed to get class of the tensor-filter of single API. This binary is not compiled properly or required libraries are not loaded.");
859 0 : goto done;
860 : }
861 :
862 0 : single_h->thread =
863 0 : g_thread_try_new (NULL, invoke_thread, (gpointer) single_h, &error);
864 0 : if (single_h->thread == NULL) {
865 0 : _ml_error_report
866 : ("Failed to create the invoke thread of single API, g_thread_try_new has reported an error: %s.",
867 : error->message);
868 0 : g_clear_error (&error);
869 0 : goto done;
870 : }
871 :
872 0 : created = TRUE;
873 :
874 0 : done:
875 0 : if (!created) {
876 0 : ml_single_close (single_h);
877 0 : single_h = NULL;
878 : }
879 :
880 0 : return single_h;
881 : }
882 :
883 : /**
884 : * @brief Validate arguments for open
885 : */
886 : static int
887 0 : _ml_single_open_custom_validate_arguments (ml_single_h * single,
888 : ml_single_preset * info)
889 : {
890 0 : if (!single)
891 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
892 : "The parameter, 'single' (ml_single_h *), is NULL. It should be a valid pointer to an instance of ml_single_h.");
893 0 : if (!info)
894 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
895 : "The parameter, 'info' (ml_single_preset *), is NULL. It should be a valid pointer to a valid instance of ml_single_preset.");
896 :
897 : /* Validate input tensor info. */
898 0 : if (info->input_info && !ml_tensors_info_is_valid (info->input_info))
899 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
900 : "The parameter, 'info' (ml_single_preset *), is not valid. It has 'input_info' entry that cannot be validated. ml_tensors_info_is_valid(info->input_info) has failed while info->input_info exists.");
901 :
902 : /* Validate output tensor info. */
903 0 : if (info->output_info && !ml_tensors_info_is_valid (info->output_info))
904 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
905 : "The parameter, 'info' (ml_single_preset *), is not valid. It has 'output_info' entry that cannot be validated. ml_tensors_info_is_valid(info->output_info) has failed while info->output_info exists.");
906 :
907 0 : if (!info->models)
908 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
909 : "The parameter, 'info' (ml_single_preset *), is not valid. Its models entry if NULL (info->models is NULL).");
910 :
911 0 : return ML_ERROR_NONE;
912 : }
913 :
914 : /**
915 : * @brief Internal function to convert accelerator as tensor_filter property format.
916 : * @note returned value must be freed by the caller
917 : * @note More details on format can be found in gst_tensor_filter_install_properties() in tensor_filter_common.c.
918 : */
919 : char *
920 0 : _ml_nnfw_to_str_prop (const ml_nnfw_hw_e hw)
921 : {
922 : const gchar *hw_name;
923 0 : const gchar *use_accl = "true:";
924 0 : gchar *str_prop = NULL;
925 :
926 0 : hw_name = get_accl_hw_str (_ml_nnfw_to_accl_hw (hw));
927 0 : str_prop = g_strdup_printf ("%s%s", use_accl, hw_name);
928 :
929 0 : return str_prop;
930 : }
931 :
932 : /**
933 : * @brief Opens an ML model with the custom options and returns the instance as a handle.
934 : */
935 : int
936 0 : ml_single_open_custom (ml_single_h * single, ml_single_preset * info)
937 : {
938 : ml_single *single_h;
939 : GObject *filter_obj;
940 0 : int status = ML_ERROR_NONE;
941 : ml_tensors_info_s *in_tensors_info, *out_tensors_info;
942 : ml_nnfw_type_e nnfw;
943 : ml_nnfw_hw_e hw;
944 : const gchar *fw_name;
945 : gchar **list_models;
946 : guint i, num_models;
947 : char *hw_name;
948 :
949 0 : check_feature_state (ML_FEATURE_INFERENCE);
950 :
951 : /* Validate the params */
952 0 : _ml_error_report_return_continue_iferr
953 : (_ml_single_open_custom_validate_arguments (single, info),
954 : "The parameter, 'info' (ml_single_preset *), cannot be validated. Please provide valid information for this object.");
955 :
956 : /* init null */
957 0 : *single = NULL;
958 :
959 0 : in_tensors_info = (ml_tensors_info_s *) info->input_info;
960 0 : out_tensors_info = (ml_tensors_info_s *) info->output_info;
961 0 : nnfw = info->nnfw;
962 0 : hw = info->hw;
963 0 : fw_name = _ml_get_nnfw_subplugin_name (nnfw);
964 :
965 : /**
966 : * 1. Determine nnfw and validate model file
967 : */
968 0 : list_models = g_strsplit (info->models, ",", -1);
969 0 : num_models = g_strv_length (list_models);
970 0 : for (i = 0; i < num_models; i++)
971 0 : g_strstrip (list_models[i]);
972 :
973 0 : status = _ml_validate_model_file ((const char **) list_models, num_models,
974 : &nnfw);
975 0 : if (status != ML_ERROR_NONE) {
976 0 : _ml_error_report_continue
977 : ("Cannot validate the model (1st model: %s. # models: %d). Error code: %d",
978 : list_models[0], num_models, status);
979 0 : g_strfreev (list_models);
980 0 : return status;
981 : }
982 :
983 0 : g_strfreev (list_models);
984 :
985 : /**
986 : * 2. Determine hw
987 : * (Supposed CPU only) Support others later.
988 : */
989 0 : if (!_ml_nnfw_is_available (nnfw, hw)) {
990 0 : _ml_error_report_return (ML_ERROR_NOT_SUPPORTED,
991 : "The given nnfw, '%s', is not supported. There is no corresponding tensor-filter subplugin available or the given hardware requirement is not supported for the given nnfw.",
992 : fw_name);
993 : }
994 :
995 : /* Create ml_single object */
996 0 : if ((single_h = ml_single_create_handle (nnfw)) == NULL) {
997 0 : _ml_error_report_return_continue (ML_ERROR_OUT_OF_MEMORY,
998 : "Cannot create handle for the given nnfw, %s", fw_name);
999 : }
1000 :
1001 0 : filter_obj = G_OBJECT (single_h->filter);
1002 :
1003 : /**
1004 : * 3. Construct a direct connection with the nnfw.
1005 : * Note that we do not construct a pipeline since 2019.12.
1006 : */
1007 0 : if (nnfw == ML_NNFW_TYPE_TENSORFLOW || nnfw == ML_NNFW_TYPE_SNAP ||
1008 0 : nnfw == ML_NNFW_TYPE_PYTORCH || nnfw == ML_NNFW_TYPE_TRIX_ENGINE ||
1009 0 : nnfw == ML_NNFW_TYPE_NCNN) {
1010 : /* set input and output tensors information */
1011 0 : if (in_tensors_info && out_tensors_info) {
1012 : status =
1013 0 : ml_single_set_inout_tensors_info (filter_obj, TRUE, in_tensors_info);
1014 0 : if (status != ML_ERROR_NONE) {
1015 0 : _ml_error_report_continue
1016 : ("Input tensors info is given; however, failed to set input tensors info. Error code: %d",
1017 : status);
1018 0 : goto error;
1019 : }
1020 :
1021 : status =
1022 0 : ml_single_set_inout_tensors_info (filter_obj, FALSE,
1023 : out_tensors_info);
1024 0 : if (status != ML_ERROR_NONE) {
1025 0 : _ml_error_report_continue
1026 : ("Output tensors info is given; however, failed to set output tensors info. Error code: %d",
1027 : status);
1028 0 : goto error;
1029 : }
1030 : } else {
1031 0 : _ml_error_report
1032 : ("To run the given nnfw, '%s', with a neural network model, both input and output information should be provided.",
1033 : fw_name);
1034 0 : status = ML_ERROR_INVALID_PARAMETER;
1035 0 : goto error;
1036 : }
1037 0 : } else if (nnfw == ML_NNFW_TYPE_ARMNN) {
1038 : /* set input and output tensors information, if available */
1039 0 : if (in_tensors_info) {
1040 : status =
1041 0 : ml_single_set_inout_tensors_info (filter_obj, TRUE, in_tensors_info);
1042 0 : if (status != ML_ERROR_NONE) {
1043 0 : _ml_error_report_continue
1044 : ("With nnfw '%s', input tensors info is optional. However, the user has provided an invalid input tensors info. Error code: %d",
1045 : fw_name, status);
1046 0 : goto error;
1047 : }
1048 : }
1049 0 : if (out_tensors_info) {
1050 : status =
1051 0 : ml_single_set_inout_tensors_info (filter_obj, FALSE,
1052 : out_tensors_info);
1053 0 : if (status != ML_ERROR_NONE) {
1054 0 : _ml_error_report_continue
1055 : ("With nnfw '%s', output tensors info is optional. However, the user has provided an invalid output tensors info. Error code: %d",
1056 : fw_name, status);
1057 0 : goto error;
1058 : }
1059 : }
1060 : }
1061 :
1062 : /* set accelerator, framework, model files and custom option */
1063 0 : if (info->fw_name) {
1064 0 : fw_name = (const char *) info->fw_name;
1065 : } else {
1066 0 : fw_name = _ml_get_nnfw_subplugin_name (nnfw); /* retry for "auto" */
1067 : }
1068 0 : hw_name = _ml_nnfw_to_str_prop (hw);
1069 0 : g_object_set (filter_obj, "framework", fw_name, "accelerator", hw_name,
1070 : "model", info->models, NULL);
1071 0 : g_free (hw_name);
1072 :
1073 0 : if (info->custom_option) {
1074 0 : g_object_set (filter_obj, "custom", info->custom_option, NULL);
1075 : }
1076 :
1077 : /* 4. Start the nnfw to get inout configurations if needed */
1078 0 : if (!single_h->klass->start (single_h->filter)) {
1079 0 : _ml_error_report
1080 : ("Failed to start NNFW, '%s', to get inout configurations. Subplugin class method has failed to start.",
1081 : fw_name);
1082 0 : status = ML_ERROR_STREAMS_PIPE;
1083 0 : goto error;
1084 : }
1085 :
1086 0 : if (nnfw == ML_NNFW_TYPE_NNTR_INF) {
1087 0 : if (!in_tensors_info || !out_tensors_info) {
1088 0 : if (!in_tensors_info) {
1089 : GstTensorsInfo in_info;
1090 :
1091 0 : gst_tensors_info_init (&in_info);
1092 :
1093 : /* ml_single_set_input_info() can't be done as it checks num_tensors */
1094 0 : status = ml_single_set_gst_info (single_h, &in_info);
1095 0 : if (status != ML_ERROR_NONE) {
1096 0 : _ml_error_report_continue
1097 : ("NNTrainer-inference-single cannot configure single_h handle instance with the given in_info. This might be an ML-API / NNTrainer internal error. Error Code: %d",
1098 : status);
1099 0 : goto error;
1100 : }
1101 : } else {
1102 0 : status = ml_single_set_input_info (single_h, in_tensors_info);
1103 0 : if (status != ML_ERROR_NONE) {
1104 0 : _ml_error_report_continue
1105 : ("NNTrainer-inference-single cannot configure single_h handle instance with the given in_info from the user. Error code: %d",
1106 : status);
1107 0 : goto error;
1108 : }
1109 : }
1110 : }
1111 : }
1112 :
1113 : /* 5. Set in/out configs and metadata */
1114 0 : if (!ml_single_set_info_in_handle (single_h, TRUE, in_tensors_info)) {
1115 0 : _ml_error_report
1116 : ("The input tensors info is invalid. Cannot configure single_h handle with the given input tensors info.");
1117 0 : status = ML_ERROR_INVALID_PARAMETER;
1118 0 : goto error;
1119 : }
1120 :
1121 0 : if (!ml_single_set_info_in_handle (single_h, FALSE, out_tensors_info)) {
1122 0 : _ml_error_report
1123 : ("The output tensors info is invalid. Cannot configure single_h handle with the given output tensors info.");
1124 0 : status = ML_ERROR_INVALID_PARAMETER;
1125 0 : goto error;
1126 : }
1127 :
1128 : /* Setup input and output memory buffers for invoke */
1129 0 : __setup_in_out_tensors (single_h);
1130 :
1131 0 : *single = single_h;
1132 0 : return ML_ERROR_NONE;
1133 :
1134 0 : error:
1135 0 : ml_single_close (single_h);
1136 0 : return status;
1137 : }
1138 :
1139 : /**
1140 : * @brief Opens an ML model and returns the instance as a handle.
1141 : */
1142 : int
1143 0 : ml_single_open (ml_single_h * single, const char *model,
1144 : const ml_tensors_info_h input_info, const ml_tensors_info_h output_info,
1145 : ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw)
1146 : {
1147 0 : return ml_single_open_full (single, model, input_info, output_info, nnfw, hw,
1148 : NULL);
1149 : }
1150 :
1151 : /**
1152 : * @brief Opens an ML model and returns the instance as a handle.
1153 : */
1154 : int
1155 0 : ml_single_open_full (ml_single_h * single, const char *model,
1156 : const ml_tensors_info_h input_info, const ml_tensors_info_h output_info,
1157 : ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw, const char *custom_option)
1158 : {
1159 0 : ml_single_preset info = { 0, };
1160 :
1161 0 : info.input_info = input_info;
1162 0 : info.output_info = output_info;
1163 0 : info.nnfw = nnfw;
1164 0 : info.hw = hw;
1165 0 : info.models = (char *) model;
1166 0 : info.custom_option = (char *) custom_option;
1167 :
1168 0 : return ml_single_open_custom (single, &info);
1169 : }
1170 :
1171 : /**
1172 : * @brief Open new single handle with given option.
1173 : */
1174 : int
1175 0 : ml_single_open_with_option (ml_single_h * single, const ml_option_h option)
1176 : {
1177 : void *value;
1178 0 : ml_single_preset info = { 0, };
1179 :
1180 0 : check_feature_state (ML_FEATURE_INFERENCE);
1181 :
1182 0 : if (!option) {
1183 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1184 : "The parameter, 'option' is NULL. It should be a valid ml_option_h, which should be created by ml_option_create().");
1185 : }
1186 :
1187 0 : if (!single)
1188 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1189 : "The parameter, 'single' (ml_single_h), is NULL. It should be a valid ml_single_h instance, usually created by ml_single_open().");
1190 :
1191 0 : if (ML_ERROR_NONE == ml_option_get (option, "input_info", &value))
1192 0 : info.input_info = value;
1193 0 : if (ML_ERROR_NONE == ml_option_get (option, "output_info", &value))
1194 0 : info.output_info = value;
1195 0 : if (ML_ERROR_NONE == ml_option_get (option, "nnfw", &value))
1196 0 : info.nnfw = *((ml_nnfw_type_e *) value);
1197 0 : if (ML_ERROR_NONE == ml_option_get (option, "hw", &value))
1198 0 : info.hw = *((ml_nnfw_hw_e *) value);
1199 0 : if (ML_ERROR_NONE == ml_option_get (option, "models", &value))
1200 0 : info.models = (gchar *) value;
1201 0 : if (ML_ERROR_NONE == ml_option_get (option, "custom", &value))
1202 0 : info.custom_option = (gchar *) value;
1203 0 : if (ML_ERROR_NONE == ml_option_get (option, "framework_name", &value) ||
1204 0 : ML_ERROR_NONE == ml_option_get (option, "framework", &value))
1205 0 : info.fw_name = (gchar *) value;
1206 :
1207 0 : return ml_single_open_custom (single, &info);
1208 : }
1209 :
1210 : /**
1211 : * @brief Closes the opened model handle.
1212 : *
1213 : * @details State changes performed by this function:
1214 : * ANY STATE -> JOIN REQUESTED - on receiving a request to close
1215 : *
1216 : * Once requested to close, invoke_thread() will exit after processing
1217 : * the current input (if any).
1218 : */
1219 : int
1220 0 : ml_single_close (ml_single_h single)
1221 : {
1222 : ml_single *single_h;
1223 : gboolean invoking;
1224 :
1225 0 : check_feature_state (ML_FEATURE_INFERENCE);
1226 :
1227 0 : if (!single)
1228 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1229 : "The parameter, 'single' (ml_single_h), is NULL. It should be a valid ml_single_h instance, usually created by ml_single_open().");
1230 :
1231 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 1);
1232 :
1233 0 : single_h->state = JOIN_REQUESTED;
1234 0 : g_cond_broadcast (&single_h->cond);
1235 0 : invoking = single_h->invoking;
1236 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1237 :
1238 : /** Wait until invoke process is finished */
1239 0 : while (invoking) {
1240 0 : _ml_logd ("Wait 1 ms until invoke is finished and close the handle.");
1241 0 : g_usleep (1000);
1242 0 : invoking = single_h->invoking;
1243 : /**
1244 : * single_h->invoking is the only protected value here and we are
1245 : * doing a read-only operation and do not need to project its value
1246 : * after the assignment.
1247 : * Thus, we do not need to lock single_h here.
1248 : */
1249 : }
1250 :
1251 0 : if (single_h->thread != NULL)
1252 0 : g_thread_join (single_h->thread);
1253 :
1254 : /** locking ensures correctness with parallel calls on close */
1255 0 : if (single_h->filter) {
1256 0 : g_list_foreach (single_h->destroy_data_list, __destroy_notify, single_h);
1257 0 : g_list_free (single_h->destroy_data_list);
1258 :
1259 0 : if (single_h->klass)
1260 0 : single_h->klass->stop (single_h->filter);
1261 :
1262 0 : g_object_unref (single_h->filter);
1263 0 : single_h->filter = NULL;
1264 : }
1265 :
1266 0 : if (single_h->klass) {
1267 0 : g_type_class_unref (single_h->klass);
1268 0 : single_h->klass = NULL;
1269 : }
1270 :
1271 0 : gst_tensors_info_free (&single_h->in_info);
1272 0 : gst_tensors_info_free (&single_h->out_info);
1273 :
1274 0 : ml_tensors_data_destroy (single_h->in_tensors);
1275 0 : ml_tensors_data_destroy (single_h->out_tensors);
1276 :
1277 0 : g_cond_clear (&single_h->cond);
1278 0 : g_mutex_clear (&single_h->mutex);
1279 :
1280 0 : g_free (single_h);
1281 0 : return ML_ERROR_NONE;
1282 : }
1283 :
1284 : /**
1285 : * @brief Internal function to validate input/output data.
1286 : */
1287 : static int
1288 0 : _ml_single_invoke_validate_data (ml_single_h single,
1289 : const ml_tensors_data_h data, const gboolean is_input)
1290 : {
1291 : ml_single *single_h;
1292 : ml_tensors_data_s *_data;
1293 : ml_tensors_data_s *_model;
1294 : guint i;
1295 : size_t raw_size;
1296 :
1297 0 : single_h = (ml_single *) single;
1298 0 : _data = (ml_tensors_data_s *) data;
1299 :
1300 0 : if (G_UNLIKELY (!_data))
1301 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1302 : "(internal function) The parameter, 'data' (const ml_tensors_data_h), is NULL. It should be a valid instance of ml_tensors_data_h.");
1303 :
1304 0 : if (is_input)
1305 0 : _model = (ml_tensors_data_s *) single_h->in_tensors;
1306 : else
1307 0 : _model = (ml_tensors_data_s *) single_h->out_tensors;
1308 :
1309 0 : if (G_UNLIKELY (_data->num_tensors != _model->num_tensors))
1310 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1311 : "(internal function) The number of %s tensors is not compatible with model. Given: %u, Expected: %u.",
1312 : (is_input) ? "input" : "output", _data->num_tensors,
1313 : _model->num_tensors);
1314 :
1315 0 : for (i = 0; i < _data->num_tensors; i++) {
1316 0 : if (G_UNLIKELY (!_data->tensors[i].data))
1317 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1318 : "The %d-th input tensor is not valid. There is no valid dimension metadata for this tensor.",
1319 : i);
1320 :
1321 0 : raw_size = _model->tensors[i].size;
1322 0 : if (G_UNLIKELY (_data->tensors[i].size != raw_size))
1323 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1324 : "The size of %d-th %s tensor is not compatible with model. Given: %zu, Expected: %zu.",
1325 : i, (is_input) ? "input" : "output", _data->tensors[i].size, raw_size);
1326 : }
1327 :
1328 0 : return ML_ERROR_NONE;
1329 : }
1330 :
1331 : /**
1332 : * @brief Internal function to invoke the model.
1333 : *
1334 : * @details State changes performed by this function:
1335 : * IDLE -> RUNNING - on receiving a valid request
1336 : *
1337 : * Invoke returns error if the current state is not IDLE.
1338 : * If IDLE, then invoke is requested to the thread.
1339 : * Invoke waits for the processing to be complete, and returns back
1340 : * the result once notified by the processing thread.
1341 : *
1342 : * @note IDLE is the valid thread state before and after this function call.
1343 : */
1344 : static int
1345 0 : _ml_single_invoke_internal (ml_single_h single,
1346 : const ml_tensors_data_h input, ml_tensors_data_h * output,
1347 : const gboolean need_alloc)
1348 : {
1349 : ml_single *single_h;
1350 : ml_tensors_data_h _in, _out;
1351 : gint64 end_time;
1352 0 : int status = ML_ERROR_NONE;
1353 :
1354 0 : check_feature_state (ML_FEATURE_INFERENCE);
1355 :
1356 0 : if (G_UNLIKELY (!single))
1357 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1358 : "(internal function) The parameter, single (ml_single_h), is NULL. It should be a valid instance of ml_single_h, usually created by ml_single_open().");
1359 :
1360 0 : if (G_UNLIKELY (!input))
1361 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1362 : "(internal function) The parameter, input (ml_tensors_data_h), is NULL. It should be a valid instance of ml_tensors_data_h.");
1363 :
1364 0 : if (G_UNLIKELY (!output))
1365 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1366 : "(internal function) The parameter, output (ml_tensors_data_h *), is NULL. It should be a valid pointer to an instance of ml_tensors_data_h to store the inference results.");
1367 :
1368 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
1369 :
1370 0 : if (G_UNLIKELY (!single_h->filter)) {
1371 0 : _ml_error_report
1372 : ("The tensor_filter element of this single handle (single_h) is not valid. It appears that the handle (ml_single_h single) is not appropriately created by ml_single_open(), user thread has touched its internal data, or the handle is already closed or freed by user.");
1373 0 : status = ML_ERROR_INVALID_PARAMETER;
1374 0 : goto exit;
1375 : }
1376 :
1377 : /* Validate input/output data */
1378 0 : status = _ml_single_invoke_validate_data (single, input, TRUE);
1379 0 : if (status != ML_ERROR_NONE) {
1380 0 : _ml_error_report_continue
1381 : ("The input data for the inference is not valid: error code %d. Please check the dimensions, type, number-of-tensors, and size information of the input data.",
1382 : status);
1383 0 : goto exit;
1384 : }
1385 :
1386 0 : if (!need_alloc) {
1387 0 : status = _ml_single_invoke_validate_data (single, *output, FALSE);
1388 0 : if (status != ML_ERROR_NONE) {
1389 0 : _ml_error_report_continue
1390 : ("The output data buffer provided by the user is not valid for the given neural network mode: error code %d. Please check the dimensions, type, number-of-tensors, and size information of the output data buffer.",
1391 : status);
1392 0 : goto exit;
1393 : }
1394 : }
1395 :
1396 0 : if (single_h->state != IDLE) {
1397 0 : if (G_UNLIKELY (single_h->state == JOIN_REQUESTED)) {
1398 0 : _ml_error_report
1399 : ("The handle (single_h single) is closed or being closed awaiting for the last ongoing invocation. Invoking with such a handle is not allowed. Please open another single_h handle to invoke.");
1400 0 : status = ML_ERROR_STREAMS_PIPE;
1401 0 : goto exit;
1402 : }
1403 0 : _ml_error_report
1404 : ("The handle (single_h single) is busy. There is another thread waiting for inference results with this handle. Please retry invoking again later when the handle becomes idle after completing the current inference task.");
1405 0 : status = ML_ERROR_TRY_AGAIN;
1406 0 : goto exit;
1407 : }
1408 :
1409 : /* prepare output data */
1410 0 : if (need_alloc) {
1411 0 : *output = NULL;
1412 :
1413 0 : status = _ml_tensors_data_clone_no_alloc (single_h->out_tensors, &_out);
1414 0 : if (status != ML_ERROR_NONE)
1415 0 : goto exit;
1416 : } else {
1417 0 : _out = *output;
1418 : }
1419 :
1420 : /**
1421 : * Clone input data here to prevent use-after-free case.
1422 : * We should release single_h->input after calling __invoke() function.
1423 : */
1424 0 : status = ml_tensors_data_clone (input, &_in);
1425 0 : if (status != ML_ERROR_NONE)
1426 0 : goto exit;
1427 :
1428 0 : single_h->state = RUNNING;
1429 0 : single_h->free_output = need_alloc;
1430 0 : single_h->input = _in;
1431 0 : single_h->output = _out;
1432 :
1433 0 : if (single_h->timeout > 0) {
1434 : /* Wake up "invoke_thread" */
1435 0 : g_cond_broadcast (&single_h->cond);
1436 :
1437 : /* set timeout */
1438 0 : end_time = g_get_monotonic_time () +
1439 0 : single_h->timeout * G_TIME_SPAN_MILLISECOND;
1440 :
1441 0 : if (g_cond_wait_until (&single_h->cond, &single_h->mutex, end_time)) {
1442 0 : status = single_h->status;
1443 : } else {
1444 0 : _ml_logw ("Wait for invoke has timed out");
1445 0 : status = ML_ERROR_TIMED_OUT;
1446 : /** This is set to notify invoke_thread to not process if timed out */
1447 0 : if (need_alloc)
1448 0 : set_destroy_notify (single_h, _out, TRUE);
1449 : }
1450 : } else {
1451 : /**
1452 : * Don't worry. We have locked single_h->mutex, thus there is no
1453 : * other thread with ml_single_invoke function on the same handle
1454 : * that are in this if-then-else block, which means that there is
1455 : * no other thread with active invoke-thread (calling __invoke())
1456 : * with the same handle. Thus we can call __invoke without
1457 : * having yet another mutex for __invoke.
1458 : */
1459 0 : single_h->invoking = TRUE;
1460 0 : status = __invoke (single_h, _in, _out, need_alloc);
1461 0 : ml_tensors_data_destroy (_in);
1462 0 : single_h->invoking = FALSE;
1463 0 : single_h->state = IDLE;
1464 :
1465 0 : if (status != ML_ERROR_NONE) {
1466 0 : if (need_alloc)
1467 0 : ml_tensors_data_destroy (_out);
1468 0 : goto exit;
1469 : }
1470 :
1471 0 : if (need_alloc)
1472 0 : __process_output (single_h, _out);
1473 : }
1474 :
1475 0 : exit:
1476 0 : if (status == ML_ERROR_NONE) {
1477 0 : if (need_alloc)
1478 0 : *output = _out;
1479 : }
1480 :
1481 0 : single_h->input = single_h->output = NULL;
1482 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1483 0 : return status;
1484 : }
1485 :
1486 : /**
1487 : * @brief Invokes the model with the given input data.
1488 : */
1489 : int
1490 0 : ml_single_invoke (ml_single_h single,
1491 : const ml_tensors_data_h input, ml_tensors_data_h * output)
1492 : {
1493 0 : return _ml_single_invoke_internal (single, input, output, TRUE);
1494 : }
1495 :
1496 : /**
1497 : * @brief Invokes the model with the given input data and fills the output data handle.
1498 : */
1499 : int
1500 0 : ml_single_invoke_fast (ml_single_h single,
1501 : const ml_tensors_data_h input, ml_tensors_data_h output)
1502 : {
1503 0 : return _ml_single_invoke_internal (single, input, &output, FALSE);
1504 : }
1505 :
1506 : /**
1507 : * @brief Gets the tensors info for the given handle.
1508 : * @param[out] info A pointer to a NULL (unallocated) instance.
1509 : */
1510 : static int
1511 0 : ml_single_get_tensors_info (ml_single_h single, gboolean is_input,
1512 : ml_tensors_info_h * info)
1513 : {
1514 : ml_single *single_h;
1515 0 : int status = ML_ERROR_NONE;
1516 :
1517 0 : check_feature_state (ML_FEATURE_INFERENCE);
1518 :
1519 0 : if (!single)
1520 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1521 : "(internal function) The parameter, 'single' (ml_single_h), is NULL. It should be a valid ml_single_h instance, usually created by ml_single_open().");
1522 0 : if (!info)
1523 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1524 : "(internal function) The parameter, 'info' (ml_tensors_info_h *) is NULL. It should be a valid pointer to an empty (NULL) instance of ml_tensor_info_h, which is supposed to be filled with the fetched info by this function.");
1525 :
1526 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
1527 :
1528 0 : if (is_input)
1529 0 : status = _ml_tensors_info_create_from_gst (info, &single_h->in_info);
1530 : else
1531 0 : status = _ml_tensors_info_create_from_gst (info, &single_h->out_info);
1532 :
1533 0 : if (status != ML_ERROR_NONE) {
1534 0 : _ml_error_report_continue
1535 : ("(internal function) Failed to create an entry for the ml_tensors_info_h instance. Error code: %d",
1536 : status);
1537 : }
1538 :
1539 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1540 0 : return status;
1541 : }
1542 :
1543 : /**
1544 : * @brief Gets the information of required input data for the given handle.
1545 : * @note information = (tensor dimension, type, name and so on)
1546 : */
1547 : int
1548 0 : ml_single_get_input_info (ml_single_h single, ml_tensors_info_h * info)
1549 : {
1550 0 : return ml_single_get_tensors_info (single, TRUE, info);
1551 : }
1552 :
1553 : /**
1554 : * @brief Gets the information of output data for the given handle.
1555 : * @note information = (tensor dimension, type, name and so on)
1556 : */
1557 : int
1558 0 : ml_single_get_output_info (ml_single_h single, ml_tensors_info_h * info)
1559 : {
1560 0 : return ml_single_get_tensors_info (single, FALSE, info);
1561 : }
1562 :
1563 : /**
1564 : * @brief Sets the maximum amount of time to wait for an output, in milliseconds.
1565 : */
1566 : int
1567 0 : ml_single_set_timeout (ml_single_h single, unsigned int timeout)
1568 : {
1569 : ml_single *single_h;
1570 :
1571 0 : check_feature_state (ML_FEATURE_INFERENCE);
1572 :
1573 0 : if (!single)
1574 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1575 : "The parameter, single (ml_single_h), is NULL. It should be a valid instance of ml_single_h, which is usually created by ml_single_open().");
1576 :
1577 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
1578 :
1579 0 : single_h->timeout = (guint) timeout;
1580 :
1581 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1582 0 : return ML_ERROR_NONE;
1583 : }
1584 :
1585 : /**
1586 : * @brief Sets the information (tensor dimension, type, name and so on) of required input data for the given model.
1587 : */
1588 : int
1589 0 : ml_single_set_input_info (ml_single_h single, const ml_tensors_info_h info)
1590 : {
1591 : ml_single *single_h;
1592 : GstTensorsInfo gst_info;
1593 0 : int status = ML_ERROR_NONE;
1594 :
1595 0 : check_feature_state (ML_FEATURE_INFERENCE);
1596 :
1597 0 : if (!single)
1598 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1599 : "The parameter, single (ml_single_h), is NULL. It should be a valid instance of ml_single_h, which is usually created by ml_single_open().");
1600 0 : if (!info)
1601 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1602 : "The parameter, info (const ml_tensors_info_h), is NULL. It should be a valid instance of ml_tensors_info_h, which is usually created by ml_tensors_info_create() or other APIs.");
1603 :
1604 0 : if (!ml_tensors_info_is_valid (info))
1605 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1606 : "The parameter, info (const ml_tensors_info_h), is not valid. Although it is not NULL, the content of 'info' is invalid. If it is created by ml_tensors_info_create(), which creates an empty instance, it should be filled by users afterwards. Please check if 'info' has all elements filled with valid values.");
1607 :
1608 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
1609 0 : _ml_tensors_info_copy_from_ml (&gst_info, info);
1610 0 : status = ml_single_set_gst_info (single_h, &gst_info);
1611 0 : gst_tensors_info_free (&gst_info);
1612 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1613 :
1614 0 : if (status != ML_ERROR_NONE)
1615 0 : _ml_error_report_continue
1616 : ("ml_single_set_gst_info() has failed to configure the single_h handle with the given info. Error code: %d",
1617 : status);
1618 :
1619 0 : return status;
1620 : }
1621 :
1622 : /**
1623 : * @brief Invokes the model with the given input data with the given info.
1624 : */
1625 : int
1626 0 : ml_single_invoke_dynamic (ml_single_h single,
1627 : const ml_tensors_data_h input, const ml_tensors_info_h in_info,
1628 : ml_tensors_data_h * output, ml_tensors_info_h * out_info)
1629 : {
1630 : int status;
1631 0 : ml_tensors_info_h cur_in_info = NULL;
1632 :
1633 0 : if (!single)
1634 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1635 : "The parameter, single (ml_single_h), is NULL. It should be a valid instance of ml_single_h, which is usually created by ml_single_open().");
1636 0 : if (!input)
1637 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1638 : "The parameter, input (const ml_tensors_data_h), is NULL. It should be a valid instance of ml_tensors_data_h with input data frame for inference.");
1639 0 : if (!in_info)
1640 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1641 : "The parameter, in_info (const ml_tensors_info_h), is NULL. It should be a valid instance of ml_tensor_info_h that describes metadata of the given input for inference (input).");
1642 0 : if (!output)
1643 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1644 : "The parameter, output (ml_tensors_data_h *), is NULL. It should be a pointer to an empty (NULL or do-not-care) instance of ml_tensors_data_h, which is filled by this API with the result of inference.");
1645 0 : if (!out_info)
1646 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1647 : "The parameter, out_info (ml_tensors_info_h *), is NULL. It should be a pointer to an empty (NULL or do-not-care) instance of ml_tensors_info_h, which is filled by this API with the neural network model info.");
1648 :
1649 : /* init null */
1650 0 : *output = NULL;
1651 0 : *out_info = NULL;
1652 :
1653 0 : status = ml_single_get_input_info (single, &cur_in_info);
1654 0 : if (status != ML_ERROR_NONE) {
1655 0 : _ml_error_report_continue
1656 : ("Failed to get input metadata configured by the opened single_h handle instance. Error code: %d.",
1657 : status);
1658 0 : goto exit;
1659 : }
1660 0 : status = ml_single_update_info (single, in_info, out_info);
1661 0 : if (status != ML_ERROR_NONE) {
1662 0 : _ml_error_report_continue
1663 : ("Failed to reconfigure the opened single_h handle instance with the updated input/output metadata. Error code: %d.",
1664 : status);
1665 0 : goto exit;
1666 : }
1667 :
1668 0 : status = ml_single_invoke (single, input, output);
1669 0 : if (status != ML_ERROR_NONE) {
1670 0 : ml_single_set_input_info (single, cur_in_info);
1671 0 : if (status != ML_ERROR_TRY_AGAIN) {
1672 : /* If it's TRY_AGAIN, ml_single_invoke() has already gave enough info. */
1673 0 : _ml_error_report_continue
1674 : ("Invoking the given neural network has failed. Error code: %d.",
1675 : status);
1676 : }
1677 : }
1678 :
1679 0 : exit:
1680 0 : if (cur_in_info)
1681 0 : ml_tensors_info_destroy (cur_in_info);
1682 :
1683 0 : if (status != ML_ERROR_NONE) {
1684 0 : if (*out_info) {
1685 0 : ml_tensors_info_destroy (*out_info);
1686 0 : *out_info = NULL;
1687 : }
1688 : }
1689 :
1690 0 : return status;
1691 : }
1692 :
1693 : /**
1694 : * @brief Sets the property value for the given model.
1695 : */
1696 : int
1697 0 : ml_single_set_property (ml_single_h single, const char *name, const char *value)
1698 : {
1699 : ml_single *single_h;
1700 0 : int status = ML_ERROR_NONE;
1701 0 : char *old_value = NULL;
1702 :
1703 0 : check_feature_state (ML_FEATURE_INFERENCE);
1704 :
1705 0 : if (!single)
1706 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1707 : "The parameter, single (ml_single_h), is NULL. It should be a valid instance of ml_single_h, which is usually created by ml_single_open().");
1708 0 : if (!name)
1709 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1710 : "The parameter, name (const char *), is NULL. It should be a valid string representing a property key.");
1711 :
1712 : /* get old value, also check the property is updatable. */
1713 0 : _ml_error_report_return_continue_iferr
1714 : (ml_single_get_property (single, name, &old_value),
1715 : "Cannot fetch the previous value for the given property name, '%s'. It appears that the property key, '%s', is invalid (not supported).",
1716 : name, name);
1717 :
1718 : /* if sets same value, do not change. */
1719 0 : if (old_value && value && g_ascii_strcasecmp (old_value, value) == 0) {
1720 0 : g_free (old_value);
1721 0 : return ML_ERROR_NONE;
1722 : }
1723 :
1724 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
1725 :
1726 : /* update property */
1727 0 : if (g_str_equal (name, "is-updatable")) {
1728 0 : if (!value)
1729 0 : goto error;
1730 :
1731 : /* boolean */
1732 0 : if (g_ascii_strcasecmp (value, "true") == 0) {
1733 0 : if (g_ascii_strcasecmp (old_value, "true") != 0)
1734 0 : g_object_set (G_OBJECT (single_h->filter), name, (gboolean) TRUE, NULL);
1735 0 : } else if (g_ascii_strcasecmp (value, "false") == 0) {
1736 0 : if (g_ascii_strcasecmp (old_value, "false") != 0)
1737 0 : g_object_set (G_OBJECT (single_h->filter), name, (gboolean) FALSE,
1738 : NULL);
1739 : } else {
1740 0 : _ml_error_report
1741 : ("The property value, '%s', is not appropriate for a boolean property 'is-updatable'. It should be either 'true' or 'false'.",
1742 : value);
1743 0 : status = ML_ERROR_INVALID_PARAMETER;
1744 : }
1745 0 : } else if (g_str_equal (name, "input") || g_str_equal (name, "inputtype")
1746 0 : || g_str_equal (name, "inputname") || g_str_equal (name, "output")
1747 0 : || g_str_equal (name, "outputtype") || g_str_equal (name, "outputname")) {
1748 : GstTensorsInfo gst_info;
1749 0 : gboolean is_input = g_str_has_prefix (name, "input");
1750 : guint num;
1751 :
1752 0 : if (!value)
1753 0 : goto error;
1754 :
1755 0 : ml_single_get_gst_info (single_h, is_input, &gst_info);
1756 :
1757 0 : if (g_str_has_suffix (name, "type"))
1758 0 : num = gst_tensors_info_parse_types_string (&gst_info, value);
1759 0 : else if (g_str_has_suffix (name, "name"))
1760 0 : num = gst_tensors_info_parse_names_string (&gst_info, value);
1761 : else
1762 0 : num = gst_tensors_info_parse_dimensions_string (&gst_info, value);
1763 :
1764 0 : if (num == gst_info.num_tensors) {
1765 : /* change configuration */
1766 0 : status = ml_single_set_gst_info (single_h, &gst_info);
1767 : } else {
1768 0 : _ml_error_report
1769 : ("The property value, '%s', is not appropriate for the given property key, '%s'. The API has failed to parse the given property value.",
1770 : value, name);
1771 0 : status = ML_ERROR_INVALID_PARAMETER;
1772 : }
1773 :
1774 0 : gst_tensors_info_free (&gst_info);
1775 : } else {
1776 0 : g_object_set (G_OBJECT (single_h->filter), name, value, NULL);
1777 : }
1778 0 : goto done;
1779 0 : error:
1780 0 : _ml_error_report
1781 : ("The parameter, value (const char *), is NULL. It should be a valid string representing the value to be set for the given property key, '%s'",
1782 : name);
1783 0 : status = ML_ERROR_INVALID_PARAMETER;
1784 0 : done:
1785 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1786 :
1787 0 : g_free (old_value);
1788 0 : return status;
1789 : }
1790 :
1791 : /**
1792 : * @brief Gets the property value for the given model.
1793 : */
1794 : int
1795 0 : ml_single_get_property (ml_single_h single, const char *name, char **value)
1796 : {
1797 : ml_single *single_h;
1798 0 : int status = ML_ERROR_NONE;
1799 :
1800 0 : check_feature_state (ML_FEATURE_INFERENCE);
1801 :
1802 0 : if (!single)
1803 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1804 : "The parameter, single (ml_single_h), is NULL. It should be a valid instance of ml_single_h, which is usually created by ml_single_open().");
1805 0 : if (!name)
1806 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1807 : "The parameter, name (const char *), is NULL. It should be a valid string representing a property key.");
1808 0 : if (!value)
1809 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1810 : "The parameter, value (const char *), is NULL. It should be a valid string representing the value to be set for the given property key, '%s'",
1811 : name);
1812 :
1813 : /* init null */
1814 0 : *value = NULL;
1815 :
1816 0 : ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
1817 :
1818 0 : if (g_str_equal (name, "input") || g_str_equal (name, "output") ||
1819 0 : g_str_equal (name, "inputtype") || g_str_equal (name, "inputname") ||
1820 0 : g_str_equal (name, "inputlayout") || g_str_equal (name, "outputtype") ||
1821 0 : g_str_equal (name, "outputname") || g_str_equal (name, "outputlayout") ||
1822 0 : g_str_equal (name, "accelerator") || g_str_equal (name, "custom")) {
1823 : /* string */
1824 0 : g_object_get (G_OBJECT (single_h->filter), name, value, NULL);
1825 0 : } else if (g_str_equal (name, "is-updatable")) {
1826 0 : gboolean bool_value = FALSE;
1827 :
1828 : /* boolean */
1829 0 : g_object_get (G_OBJECT (single_h->filter), name, &bool_value, NULL);
1830 0 : *value = (bool_value) ? g_strdup ("true") : g_strdup ("false");
1831 : } else {
1832 0 : _ml_error_report
1833 : ("The property key, '%s', is not available for get_property and not recognized by the API. It should be one of {input, inputtype, inputname, inputlayout, output, outputtype, outputname, outputlayout, accelerator, custom, is-updatable}.",
1834 : name);
1835 0 : status = ML_ERROR_NOT_SUPPORTED;
1836 : }
1837 :
1838 0 : ML_SINGLE_HANDLE_UNLOCK (single_h);
1839 0 : return status;
1840 : }
1841 :
1842 : /**
1843 : * @brief Internal helper function to validate model files.
1844 : */
1845 : static int
1846 0 : __ml_validate_model_file (const char *const *model,
1847 : const unsigned int num_models, gboolean * is_dir)
1848 : {
1849 : guint i;
1850 :
1851 0 : if (!model)
1852 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1853 : "The parameter, model, is NULL. It should be a valid array of strings, where each string is a valid file path for a neural network model file.");
1854 0 : if (num_models < 1)
1855 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1856 : "The parameter, num_models, is 0. It should be the number of files for the given neural network model.");
1857 :
1858 0 : if (g_file_test (model[0], G_FILE_TEST_IS_DIR)) {
1859 0 : *is_dir = TRUE;
1860 0 : return ML_ERROR_NONE;
1861 : }
1862 :
1863 0 : for (i = 0; i < num_models; i++) {
1864 0 : if (!model[i] ||
1865 0 : !g_file_test (model[i], G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
1866 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1867 : "The given param, model path [%d] = \"%s\" is invalid or the file is not found or accessible.",
1868 : i, _STR_NULL (model[i]));
1869 : }
1870 : }
1871 :
1872 0 : *is_dir = FALSE;
1873 :
1874 0 : return ML_ERROR_NONE;
1875 : }
1876 :
1877 : /**
1878 : * @brief Validates the nnfw model file.
1879 : * @since_tizen 5.5
1880 : * @param[in] model The path of model file.
1881 : * @param[in/out] nnfw The type of NNFW.
1882 : * @return @c 0 on success. Otherwise a negative error value.
1883 : * @retval #ML_ERROR_NONE Successful
1884 : * @retval #ML_ERROR_NOT_SUPPORTED Not supported, or framework to support this model file is unavailable in the environment.
1885 : * @retval #ML_ERROR_INVALID_PARAMETER Given parameter is invalid.
1886 : */
1887 : int
1888 0 : _ml_validate_model_file (const char *const *model,
1889 : const unsigned int num_models, ml_nnfw_type_e * nnfw)
1890 : {
1891 0 : int status = ML_ERROR_NONE;
1892 0 : ml_nnfw_type_e detected = ML_NNFW_TYPE_ANY;
1893 0 : gboolean is_dir = FALSE;
1894 : gchar *pos, *fw_name;
1895 0 : gchar **file_ext = NULL;
1896 : guint i;
1897 :
1898 0 : if (!nnfw)
1899 0 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
1900 : "The parameter, nnfw, is NULL. It should be a valid pointer of ml_nnfw_type_e.");
1901 :
1902 0 : _ml_error_report_return_continue_iferr (__ml_validate_model_file (model,
1903 : num_models, &is_dir),
1904 : "The parameters, model and num_models, are not valid.");
1905 :
1906 : /**
1907 : * @note detect-fw checks the file ext and returns proper fw name for given models.
1908 : * If detected fw and given nnfw are same, we don't need to check the file extension.
1909 : * If any condition for auto detection is added later, below code also should be updated.
1910 : */
1911 0 : fw_name = gst_tensor_filter_detect_framework (model, num_models, TRUE);
1912 0 : detected = _ml_get_nnfw_type_by_subplugin_name (fw_name);
1913 0 : g_free (fw_name);
1914 :
1915 0 : if (*nnfw == ML_NNFW_TYPE_ANY) {
1916 0 : if (detected == ML_NNFW_TYPE_ANY) {
1917 0 : _ml_error_report
1918 : ("The given neural network model (1st path is \"%s\", and there are %d paths declared) has unknown or unsupported extension. Please check its corresponding neural network framework and try to specify it instead of \"ML_NNFW_TYPE_ANY\".",
1919 : model[0], num_models);
1920 0 : status = ML_ERROR_INVALID_PARAMETER;
1921 : } else {
1922 0 : _ml_logi ("The given model is supposed a %s model.",
1923 : _ml_get_nnfw_subplugin_name (detected));
1924 0 : *nnfw = detected;
1925 : }
1926 :
1927 0 : goto done;
1928 0 : } else if (is_dir && *nnfw != ML_NNFW_TYPE_NNFW) {
1929 : /* supposed it is ONE if given model is directory */
1930 0 : _ml_error_report
1931 : ("The given model (1st path is \"%s\", and there are %d paths declared) is directory, which is allowed by \"NNFW (One Runtime)\" only, Please check the model and framework.",
1932 : model[0], num_models);
1933 0 : status = ML_ERROR_INVALID_PARAMETER;
1934 0 : goto done;
1935 0 : } else if (detected == *nnfw) {
1936 : /* Expected framework, nothing to do. */
1937 0 : goto done;
1938 : }
1939 :
1940 : /* Handle mismatched case, check file extension. */
1941 0 : file_ext = g_malloc0 (sizeof (char *) * (num_models + 1));
1942 0 : for (i = 0; i < num_models; i++) {
1943 0 : if ((pos = strrchr (model[i], '.')) == NULL) {
1944 0 : _ml_error_report ("The given model [%d]=\"%s\" has invalid extension.", i,
1945 : model[i]);
1946 0 : status = ML_ERROR_INVALID_PARAMETER;
1947 0 : goto done;
1948 : }
1949 :
1950 0 : file_ext[i] = g_ascii_strdown (pos, -1);
1951 : }
1952 :
1953 : /** @todo Make sure num_models is correct for each nnfw type */
1954 0 : switch (*nnfw) {
1955 0 : case ML_NNFW_TYPE_NNFW:
1956 : case ML_NNFW_TYPE_TVM:
1957 : case ML_NNFW_TYPE_ONNX_RUNTIME:
1958 : case ML_NNFW_TYPE_NCNN:
1959 : case ML_NNFW_TYPE_TENSORRT:
1960 : case ML_NNFW_TYPE_QNN:
1961 : /**
1962 : * We cannot check the file ext with NNFW.
1963 : * NNFW itself will validate metadata and model file.
1964 : */
1965 0 : break;
1966 0 : case ML_NNFW_TYPE_MVNC:
1967 : case ML_NNFW_TYPE_OPENVINO:
1968 : case ML_NNFW_TYPE_EDGE_TPU:
1969 : /**
1970 : * @todo Need to check method to validate model
1971 : * Although nnstreamer supports these frameworks,
1972 : * ML-API implementation is not ready.
1973 : */
1974 0 : _ml_error_report
1975 : ("Given NNFW is not supported by ML-API Inference.Single, yet, although it is supported by NNStreamer. If you have such NNFW integrated into your machine and want to access via ML-API, please update the corresponding implementation or report and discuss at github.com/nnstreamer/nnstreamer/issues.");
1976 0 : status = ML_ERROR_NOT_SUPPORTED;
1977 0 : break;
1978 0 : case ML_NNFW_TYPE_VD_AIFW:
1979 0 : if (!g_str_equal (file_ext[0], ".nb") &&
1980 0 : !g_str_equal (file_ext[0], ".ncp") &&
1981 0 : !g_str_equal (file_ext[0], ".tvn") &&
1982 0 : !g_str_equal (file_ext[0], ".bin")) {
1983 0 : status = ML_ERROR_INVALID_PARAMETER;
1984 : }
1985 0 : break;
1986 0 : case ML_NNFW_TYPE_SNAP:
1987 : #if !defined (__ANDROID__)
1988 0 : _ml_error_report ("SNAP is supported by Android/arm64-v8a devices only.");
1989 0 : status = ML_ERROR_NOT_SUPPORTED;
1990 : #endif
1991 : /* SNAP requires multiple files, set supported if model file exists. */
1992 0 : break;
1993 0 : case ML_NNFW_TYPE_ARMNN:
1994 0 : if (!g_str_equal (file_ext[0], ".caffemodel") &&
1995 0 : !g_str_equal (file_ext[0], ".tflite") &&
1996 0 : !g_str_equal (file_ext[0], ".pb") &&
1997 0 : !g_str_equal (file_ext[0], ".prototxt")) {
1998 0 : _ml_error_report
1999 : ("ARMNN accepts .caffemodel, .tflite, .pb, and .prototxt files only. Please support correct file extension. You have specified: \"%s\"",
2000 : file_ext[0]);
2001 0 : status = ML_ERROR_INVALID_PARAMETER;
2002 : }
2003 0 : break;
2004 0 : case ML_NNFW_TYPE_MXNET:
2005 0 : if (!g_str_equal (file_ext[0], ".params") &&
2006 0 : !g_str_equal (file_ext[0], ".json")) {
2007 0 : status = ML_ERROR_INVALID_PARAMETER;
2008 : }
2009 0 : break;
2010 0 : default:
2011 0 : _ml_error_report
2012 : ("You have designated an incorrect neural network framework (out of bound).");
2013 0 : status = ML_ERROR_INVALID_PARAMETER;
2014 0 : break;
2015 : }
2016 :
2017 0 : done:
2018 0 : if (status == ML_ERROR_NONE) {
2019 0 : if (!_ml_nnfw_is_available (*nnfw, ML_NNFW_HW_ANY)) {
2020 0 : status = ML_ERROR_NOT_SUPPORTED;
2021 0 : _ml_error_report
2022 : ("The subplugin for tensor-filter \"%s\" is not available. Please install the corresponding tensor-filter subplugin file (usually, \"libnnstreamer_filter_${NAME}.so\") at the correct path. Please use \"nnstreamer-check\" utility to check related configurations. If you do not have the utility ready, build and install \"confchk\", which is located at ${nnstreamer_source}/tools/development/confchk/ .",
2023 : _ml_get_nnfw_subplugin_name (*nnfw));
2024 : }
2025 : } else {
2026 0 : _ml_error_report
2027 : ("The given model file, \"%s\" (1st of %d files), is invalid.",
2028 : model[0], num_models);
2029 : }
2030 :
2031 0 : g_strfreev (file_ext);
2032 0 : return status;
2033 : }
|