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