-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathml-api-inference-single.c
More file actions
2179 lines (1880 loc) · 73.8 KB
/
ml-api-inference-single.c
File metadata and controls
2179 lines (1880 loc) · 73.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: Apache-2.0 */
/**
* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
*
* @file ml-api-inference-single.c
* @date 29 Aug 2019
* @brief NNStreamer/Single C-API Wrapper.
* This allows to invoke individual input frame with NNStreamer.
* @see https://github.com/nnstreamer/nnstreamer
* @author MyungJoo Ham <myungjoo.ham@samsung.com>
* @author Parichay Kapoor <pk.kapoor@samsung.com>
* @bug No known bugs except for NYI items
*/
#include <string.h>
#include <nnstreamer-single.h>
#include <nnstreamer-tizen-internal.h> /* Tizen platform header */
#include <nnstreamer_internal.h>
#include <nnstreamer_plugin_api_util.h>
#include <tensor_filter_single.h>
#include "ml-api-inference-internal.h"
#include "ml-api-internal.h"
#include "ml-api-inference-single-internal.h"
#define ML_SINGLE_MAGIC 0xfeedfeed
/**
* @brief Default time to wait for an output in milliseconds (0 will wait for the output).
*/
#define SINGLE_DEFAULT_TIMEOUT 0
/**
* @brief Global lock for single shot API
* @detail This lock ensures that ml_single_close is thread safe. All other API
* functions use the mutex from the single handle. However for close,
* single handle mutex cannot be used as single handle is destroyed at
* close
* @note This mutex is automatically initialized as it is statically declared
*/
G_LOCK_DEFINE_STATIC (magic);
/**
* @brief Get valid handle after magic verification
* @note handle's mutex (single_h->mutex) is acquired after this
* @param[out] single_h The handle properly casted: (ml_single *).
* @param[in] single The handle to be validated: (void *).
* @param[in] reset Set TRUE if the handle is to be reset (magic = 0).
*/
#define ML_SINGLE_GET_VALID_HANDLE_LOCKED(single_h, single, reset) do { \
G_LOCK (magic); \
single_h = (ml_single *) single; \
if (G_UNLIKELY(single_h->magic != ML_SINGLE_MAGIC)) { \
_ml_error_report \
("The given param, %s (ml_single_h), is invalid. It is not a single_h instance or the user thread has modified it.", \
#single); \
G_UNLOCK (magic); \
return ML_ERROR_INVALID_PARAMETER; \
} \
if (G_UNLIKELY(reset)) \
single_h->magic = 0; \
g_mutex_lock (&single_h->mutex); \
G_UNLOCK (magic); \
} while (0)
/**
* @brief This is for the symmetricity with ML_SINGLE_GET_VALID_HANDLE_LOCKED
* @param[in] single_h The casted handle (ml_single *).
*/
#define ML_SINGLE_HANDLE_UNLOCK(single_h) g_mutex_unlock (&single_h->mutex);
/** define string names for input/output */
#define INPUT_STR "input"
#define OUTPUT_STR "output"
#define TYPE_STR "type"
#define NAME_STR "name"
/** concat string from #define */
#define CONCAT_MACRO_STR(STR1,STR2) STR1 STR2
/** States for invoke thread */
typedef enum
{
IDLE = 0, /**< ready to accept next input */
RUNNING, /**< running an input, cannot accept more input */
JOIN_REQUESTED /**< should join the thread, will exit soon */
} thread_state;
/**
* @brief The name of sub-plugin for defined neural net frameworks.
* @note The sub-plugin for Android is not declared (e.g., snap)
*/
static const char *ml_nnfw_subplugin_name[] = {
[ML_NNFW_TYPE_ANY] = "any", /* DO NOT use this name ('any') to get the sub-plugin */
[ML_NNFW_TYPE_CUSTOM_FILTER] = "custom",
[ML_NNFW_TYPE_TENSORFLOW_LITE] = "tensorflow-lite",
[ML_NNFW_TYPE_TENSORFLOW] = "tensorflow",
[ML_NNFW_TYPE_NNFW] = "nnfw",
[ML_NNFW_TYPE_MVNC] = "movidius-ncsdk2",
[ML_NNFW_TYPE_OPENVINO] = "openvino",
[ML_NNFW_TYPE_VIVANTE] = "vivante",
[ML_NNFW_TYPE_EDGE_TPU] = "edgetpu",
[ML_NNFW_TYPE_ARMNN] = "armnn",
[ML_NNFW_TYPE_SNPE] = "snpe",
[ML_NNFW_TYPE_PYTORCH] = "pytorch",
[ML_NNFW_TYPE_NNTR_INF] = "nntrainer",
[ML_NNFW_TYPE_VD_AIFW] = "vd_aifw",
[ML_NNFW_TYPE_TRIX_ENGINE] = "trix-engine",
[ML_NNFW_TYPE_MXNET] = "mxnet",
[ML_NNFW_TYPE_TVM] = "tvm",
[ML_NNFW_TYPE_ONNX_RUNTIME] = "onnxruntime",
[ML_NNFW_TYPE_NCNN] = "ncnn",
[ML_NNFW_TYPE_TENSORRT] = "tensorrt",
[ML_NNFW_TYPE_QNN] = "qnn",
[ML_NNFW_TYPE_LLAMACPP] = "llamacpp",
[ML_NNFW_TYPE_TIZEN_HAL] = "tizen-hal",
[ML_NNFW_TYPE_FLARE] = "flare",
NULL
};
/** ML single api data structure for handle */
typedef struct
{
GTensorFilterSingleClass *klass; /**< tensor filter class structure*/
GTensorFilterSingle *filter; /**< tensor filter element */
GstTensorsInfo in_info; /**< info about input */
GstTensorsInfo out_info; /**< info about output */
ml_nnfw_type_e nnfw; /**< nnfw type for this filter */
guint magic; /**< code to verify valid handle */
GThread *thread; /**< thread for invoking */
GMutex mutex; /**< mutex for synchronization */
GCond cond; /**< condition for synchronization */
ml_tensors_data_h input; /**< input received from user */
ml_tensors_data_h output; /**< output to be sent back to user */
guint timeout; /**< timeout for invoking */
thread_state state; /**< current state of the thread */
gboolean free_output; /**< true if output tensors are allocated in single-shot */
int status; /**< status of processing */
gboolean invoking; /**< invoke running flag */
ml_tensors_data_h in_tensors; /**< input tensor wrapper for processing */
ml_tensors_data_h out_tensors; /**< output tensor wrapper for processing */
GList *destroy_data_list; /**< data to be freed by filter */
gboolean invoke_dynamic; /**< true to invoke flexible tensor */
gboolean invoke_async; /**< true to invoke and return result asynchronously */
ml_tensors_data_cb invoke_async_cb; /**< Callback function to be called when the sub-plugin generates an output asynchronously. */
void *invoke_async_pdata; /**< Private data to be passed to async callback. */
} ml_single;
/**
* @brief Internal function to get the nnfw type.
*/
ml_nnfw_type_e
_ml_get_nnfw_type_by_subplugin_name (const char *name)
{
ml_nnfw_type_e nnfw_type = ML_NNFW_TYPE_ANY;
int idx = -1;
if (name == NULL)
return ML_NNFW_TYPE_ANY;
idx = find_key_strv (ml_nnfw_subplugin_name, name);
if (idx < 0) {
/* check sub-plugin for android */
if (g_ascii_strcasecmp (name, "snap") == 0)
nnfw_type = ML_NNFW_TYPE_SNAP;
else
_ml_error_report ("Cannot find nnfw, %s is an invalid name.",
_STR_NULL (name));
} else {
nnfw_type = (ml_nnfw_type_e) idx;
}
return nnfw_type;
}
/**
* @brief Internal function to get the sub-plugin name.
*/
const char *
_ml_get_nnfw_subplugin_name (ml_nnfw_type_e nnfw)
{
/* check sub-plugin for android */
if (nnfw == ML_NNFW_TYPE_SNAP)
return "snap";
return ml_nnfw_subplugin_name[nnfw];
}
/**
* @brief Convert c-api based hw to internal representation
*/
accl_hw
_ml_nnfw_to_accl_hw (const ml_nnfw_hw_e hw)
{
switch (hw) {
case ML_NNFW_HW_ANY:
return ACCL_DEFAULT;
case ML_NNFW_HW_AUTO:
return ACCL_AUTO;
case ML_NNFW_HW_CPU:
return ACCL_CPU;
#if defined (__aarch64__) || defined (__arm__)
case ML_NNFW_HW_CPU_NEON:
return ACCL_CPU_NEON;
#else
case ML_NNFW_HW_CPU_SIMD:
return ACCL_CPU_SIMD;
#endif
case ML_NNFW_HW_GPU:
return ACCL_GPU;
case ML_NNFW_HW_NPU:
return ACCL_NPU;
case ML_NNFW_HW_NPU_MOVIDIUS:
return ACCL_NPU_MOVIDIUS;
case ML_NNFW_HW_NPU_EDGE_TPU:
return ACCL_NPU_EDGE_TPU;
case ML_NNFW_HW_NPU_VIVANTE:
return ACCL_NPU_VIVANTE;
case ML_NNFW_HW_NPU_SLSI:
return ACCL_NPU_SLSI;
case ML_NNFW_HW_NPU_SR:
/** @todo how to get srcn npu */
return ACCL_NPU_SR;
default:
return ACCL_AUTO;
}
}
/**
* @brief Checks the availability of the given execution environments with custom option.
*/
int
ml_check_nnfw_availability_full (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw,
const char *custom, bool *available)
{
const char *fw_name = NULL;
check_feature_state (ML_FEATURE_INFERENCE);
if (!available)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, available (bool *), is NULL. It should be a valid pointer of bool. E.g., bool a; ml_check_nnfw_availability_full (..., &a);");
/* init false */
*available = false;
if (nnfw == ML_NNFW_TYPE_ANY)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, nnfw (ml_nnfw_type_e), is ML_NNFW_TYPE_ANY. It should specify the framework to be probed for the hardware availability.");
fw_name = _ml_get_nnfw_subplugin_name (nnfw);
if (fw_name) {
if (nnstreamer_filter_find (fw_name) != NULL) {
accl_hw accl = _ml_nnfw_to_accl_hw (hw);
if (gst_tensor_filter_check_hw_availability (fw_name, accl, custom)) {
*available = true;
} else {
_ml_logi ("%s is supported but not with the specified hardware.",
fw_name);
}
} else {
_ml_logi ("%s is not supported.", fw_name);
}
} else {
_ml_logw ("Cannot get the name of sub-plugin for given nnfw.");
}
return ML_ERROR_NONE;
}
/**
* @brief Checks the availability of the given execution environments.
*/
int
ml_check_nnfw_availability (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw,
bool *available)
{
return ml_check_nnfw_availability_full (nnfw, hw, NULL, available);
}
/**
* @brief setup input and output tensor memory to pass to the tensor_filter.
* @note this tensor memory wrapper will be reused for each invoke.
*/
static void
__setup_in_out_tensors (ml_single * single_h)
{
guint i;
ml_tensors_data_s *in_tensors = (ml_tensors_data_s *) single_h->in_tensors;
ml_tensors_data_s *out_tensors = (ml_tensors_data_s *) single_h->out_tensors;
/* Setup input buffer */
if (in_tensors) {
_ml_tensors_info_free (in_tensors->info);
_ml_tensors_info_copy_from_gst (in_tensors->info, &single_h->in_info);
} else {
ml_tensors_info_h info;
_ml_tensors_info_create_from_gst (&info, &single_h->in_info);
_ml_tensors_data_create_no_alloc (info, &single_h->in_tensors);
ml_tensors_info_destroy (info);
in_tensors = (ml_tensors_data_s *) single_h->in_tensors;
}
in_tensors->num_tensors = single_h->in_info.num_tensors;
for (i = 0; i < in_tensors->num_tensors; i++) {
/** memory will be allocated by tensor_filter_single */
in_tensors->tensors[i].data = NULL;
in_tensors->tensors[i].size =
gst_tensors_info_get_size (&single_h->in_info, i);
}
/* Setup output buffer */
if (out_tensors) {
_ml_tensors_info_free (out_tensors->info);
_ml_tensors_info_copy_from_gst (out_tensors->info, &single_h->out_info);
} else {
ml_tensors_info_h info;
_ml_tensors_info_create_from_gst (&info, &single_h->out_info);
_ml_tensors_data_create_no_alloc (info, &single_h->out_tensors);
ml_tensors_info_destroy (info);
out_tensors = (ml_tensors_data_s *) single_h->out_tensors;
}
out_tensors->num_tensors = single_h->out_info.num_tensors;
for (i = 0; i < out_tensors->num_tensors; i++) {
/** memory will be allocated by tensor_filter_single */
out_tensors->tensors[i].data = NULL;
out_tensors->tensors[i].size =
gst_tensors_info_get_size (&single_h->out_info, i);
}
}
/**
* @brief To call the framework to destroy the allocated output data
*/
static inline void
__destroy_notify (gpointer data_h, gpointer single_data)
{
ml_single *single_h;
ml_tensors_data_s *data;
data = (ml_tensors_data_s *) data_h;
single_h = (ml_single *) single_data;
if (G_LIKELY (single_h->filter)) {
if (single_h->klass->allocate_in_invoke (single_h->filter)) {
single_h->klass->destroy_notify (single_h->filter, data->tensors);
}
}
/* reset callback function */
data->destroy = NULL;
}
/**
* @brief Wrapper function for __destroy_notify
*/
static int
ml_single_destroy_notify_cb (void *handle, void *user_data)
{
ml_tensors_data_h data = (ml_tensors_data_h) handle;
ml_single_h single = (ml_single_h) user_data;
ml_single *single_h;
int status = ML_ERROR_NONE;
if (G_UNLIKELY (!single))
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"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.");
if (G_UNLIKELY (!data))
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"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.");
ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
if (G_UNLIKELY (!single_h->filter)) {
status = ML_ERROR_INVALID_PARAMETER;
_ml_error_report
("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.");
goto exit;
}
single_h->destroy_data_list =
g_list_remove (single_h->destroy_data_list, data);
__destroy_notify (data, single_h);
exit:
ML_SINGLE_HANDLE_UNLOCK (single_h);
return status;
}
/**
* @brief setup the destroy notify for the allocated output data.
* @note this stores the data entry in the single list.
* @note this has not overhead if the allocation of output is not performed by
* the framework but by tensor filter element.
*/
static void
set_destroy_notify (ml_single * single_h, ml_tensors_data_s * data,
gboolean add)
{
if (single_h->klass->allocate_in_invoke (single_h->filter)) {
data->destroy = ml_single_destroy_notify_cb;
data->user_data = single_h;
add = TRUE;
}
if (add) {
single_h->destroy_data_list = g_list_append (single_h->destroy_data_list,
(gpointer) data);
}
}
/**
* @brief Internal function to call subplugin's invoke
*/
static inline int
__invoke (ml_single * single_h, ml_tensors_data_h in, ml_tensors_data_h out,
gboolean alloc_output)
{
ml_tensors_data_s *in_data, *out_data;
int status = ML_ERROR_NONE;
in_data = (ml_tensors_data_s *) in;
out_data = (ml_tensors_data_s *) out;
/* Prevent error case when input or output is null in invoke thread. */
if (!in_data || !out_data) {
_ml_error_report ("Failed to invoke a model, invalid data handle.");
return ML_ERROR_STREAMS_PIPE;
}
/* Invoke the thread. */
if (!single_h->klass->invoke (single_h->filter, in_data->tensors,
out_data->tensors, alloc_output)) {
const char *fw_name = _ml_get_nnfw_subplugin_name (single_h->nnfw);
_ml_error_report
("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).",
fw_name, fw_name, fw_name);
status = ML_ERROR_STREAMS_PIPE;
}
return status;
}
/**
* @brief Internal function to post-process given output.
* @note Do not call this if single_h->free_output is false (output data is not allocated in single-shot).
*/
static inline void
__process_output (ml_single * single_h, ml_tensors_data_h output)
{
ml_tensors_data_s *out_data;
if (g_list_find (single_h->destroy_data_list, output)) {
/**
* Caller of the invoke thread has returned back with timeout.
* So, free the memory allocated by the invoke as their is no receiver.
*/
single_h->destroy_data_list =
g_list_remove (single_h->destroy_data_list, output);
ml_tensors_data_destroy (output);
} else {
out_data = (ml_tensors_data_s *) output;
set_destroy_notify (single_h, out_data, FALSE);
}
}
/**
* @brief thread to execute calls to invoke
*
* @details The thread behavior is detailed as below:
* - Starting with IDLE state, the thread waits for an input or change
* in state externally.
* - If state is not RUNNING, exit this thread, else process the
* request.
* - Process input, call invoke, process output. Any error in this
* state sets the status to be used by ml_single_invoke().
* - State is set back to IDLE and thread moves back to start.
*
* State changes performed by this function when:
* RUNNING -> IDLE - processing is finished.
* JOIN_REQUESTED -> IDLE - close is requested.
*
* @note Error while processing an input is provided back to requesting
* function, and further processing of invoke_thread is not affected.
*/
static void *
invoke_thread (void *arg)
{
ml_single *single_h;
ml_tensors_data_h input, output;
gboolean alloc_output = FALSE;
single_h = (ml_single *) arg;
g_mutex_lock (&single_h->mutex);
while (single_h->state <= RUNNING) {
int status = ML_ERROR_NONE;
/** wait for data */
while (single_h->state != RUNNING) {
g_cond_wait (&single_h->cond, &single_h->mutex);
if (single_h->state == JOIN_REQUESTED)
goto exit;
}
input = single_h->input;
output = single_h->output;
/* Set null to prevent double-free. */
single_h->input = single_h->output = NULL;
single_h->invoking = TRUE;
alloc_output = single_h->free_output;
g_mutex_unlock (&single_h->mutex);
status = __invoke (single_h, input, output, alloc_output);
g_mutex_lock (&single_h->mutex);
/* Clear input data after invoke is done. */
ml_tensors_data_destroy (input);
single_h->invoking = FALSE;
if (status != ML_ERROR_NONE || single_h->state == JOIN_REQUESTED) {
if (alloc_output) {
single_h->destroy_data_list =
g_list_remove (single_h->destroy_data_list, output);
ml_tensors_data_destroy (output);
}
if (single_h->state == JOIN_REQUESTED)
goto exit;
goto wait_for_next;
}
if (alloc_output)
__process_output (single_h, output);
/** loop over to wait for the next element */
wait_for_next:
single_h->status = status;
if (single_h->state == RUNNING)
single_h->state = IDLE;
g_cond_broadcast (&single_h->cond);
}
exit:
/* Do not set IDLE if JOIN_REQUESTED */
if (single_h->state == JOIN_REQUESTED) {
/* Release input and output data */
if (single_h->input)
ml_tensors_data_destroy (single_h->input);
if (alloc_output && single_h->output) {
single_h->destroy_data_list =
g_list_remove (single_h->destroy_data_list, single_h->output);
ml_tensors_data_destroy (single_h->output);
}
single_h->input = single_h->output = NULL;
} else if (single_h->state == RUNNING)
single_h->state = IDLE;
g_mutex_unlock (&single_h->mutex);
return NULL;
}
/**
* @brief Internal function to get the asynchronous invoke.
*/
static int
ml_single_async_cb (GstTensorMemory * data, GstTensorsInfo * info,
void *user_data)
{
ml_single_h single = (ml_single_h) user_data;
ml_single *single_h;
ml_tensors_info_h _info = NULL;
ml_tensors_data_h _data = NULL;
unsigned int i;
int ret = ML_ERROR_NONE;
ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
if (!single_h->invoke_async_cb) {
/* No callback, do nothing. Internal state changing? */
goto done;
}
ret = _ml_tensors_info_create_from_gst (&_info, info);
if (ret != ML_ERROR_NONE) {
_ml_error_report
("Cannot handle tensor data stream. Failed to create ml information.");
goto done;
}
ret = ml_tensors_data_create (_info, &_data);
if (ret != ML_ERROR_NONE) {
_ml_error_report
("Cannot handle tensor data stream. Failed to create ml data.");
goto done;
}
for (i = 0; i < info->num_tensors; ++i) {
ret = ml_tensors_data_set_tensor_data (_data, i,
data[i].data, data[i].size);
if (ret != ML_ERROR_NONE) {
_ml_error_report
("Cannot handle tensor data stream. Failed to update ml data of index %u, size is %zu.",
i, data[i].size);
goto done;
}
}
ret = single_h->invoke_async_cb (_data, single_h->invoke_async_pdata);
if (ret != ML_ERROR_NONE) {
_ml_error_report
("Cannot handle tensor data stream. The callback function returns error '%d'.",
ret);
}
done:
if (_info) {
ml_tensors_info_destroy (_info);
}
if (_data) {
ml_tensors_data_destroy (_data);
}
ML_SINGLE_HANDLE_UNLOCK (single_h);
return (ret == ML_ERROR_NONE) ? 0 : -1;
}
/**
* @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.
* @details Note that a model/framework may not support setting such information.
* @since_tizen 6.0
* @param[in] single The model handle.
* @param[in] in_info The handle of input tensors information.
* @param[out] out_info The handle of output tensors information. The caller is responsible for freeing the information with ml_tensors_info_destroy().
* @return @c 0 on success. Otherwise a negative error value.
* @retval #ML_ERROR_NONE Successful
* @retval #ML_ERROR_NOT_SUPPORTED This implies that the given framework does not support dynamic dimensions.
* Use ml_single_get_input_info() and ml_single_get_output_info() instead for this framework.
* @retval #ML_ERROR_INVALID_PARAMETER Fail. The parameter is invalid.
*/
static int
ml_single_update_info (ml_single_h single,
const ml_tensors_info_h in_info, ml_tensors_info_h * out_info)
{
if (!single)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, single (ml_single_h), is NULL. It should be a valid ml_single_h instance, usually created by ml_single_open().");
if (!in_info)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"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.");
if (!out_info)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"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.");
/* init null */
*out_info = NULL;
_ml_error_report_return_continue_iferr (ml_single_set_input_info (single,
in_info),
"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.",
_ERRNO);
__setup_in_out_tensors (single);
_ml_error_report_return_continue_iferr (ml_single_get_output_info (single,
out_info),
"Fetching output info after configuring input information has failed with %d error code.",
_ERRNO);
return ML_ERROR_NONE;
}
/**
* @brief Internal function to get the gst info from tensor-filter.
*/
static void
ml_single_get_gst_info (ml_single * single_h, gboolean is_input,
GstTensorsInfo * gst_info)
{
const gchar *prop_prefix, *prop_name, *prop_type;
gchar *val;
guint num;
if (is_input) {
prop_prefix = INPUT_STR;
prop_type = CONCAT_MACRO_STR (INPUT_STR, TYPE_STR);
prop_name = CONCAT_MACRO_STR (INPUT_STR, NAME_STR);
} else {
prop_prefix = OUTPUT_STR;
prop_type = CONCAT_MACRO_STR (OUTPUT_STR, TYPE_STR);
prop_name = CONCAT_MACRO_STR (OUTPUT_STR, NAME_STR);
}
gst_tensors_info_init (gst_info);
/* get dimensions */
g_object_get (single_h->filter, prop_prefix, &val, NULL);
num = gst_tensors_info_parse_dimensions_string (gst_info, val);
g_free (val);
/* set the number of tensors */
gst_info->num_tensors = num;
/* get types */
g_object_get (single_h->filter, prop_type, &val, NULL);
num = gst_tensors_info_parse_types_string (gst_info, val);
g_free (val);
if (gst_info->num_tensors != num) {
_ml_logw ("The number of tensor type is mismatched in filter.");
}
/* get names */
g_object_get (single_h->filter, prop_name, &val, NULL);
num = gst_tensors_info_parse_names_string (gst_info, val);
g_free (val);
if (gst_info->num_tensors != num) {
_ml_logw ("The number of tensor name is mismatched in filter.");
}
if (single_h->invoke_dynamic) {
/* flexible tensor stream */
gst_info->format = _NNS_TENSOR_FORMAT_FLEXIBLE;
/** @todo Consider multiple input tensors while invoking a model. */
if (gst_info->num_tensors == 0) {
gst_info->num_tensors = 1;
}
}
}
/**
* @brief Internal function to set the gst info in tensor-filter.
*/
static int
ml_single_set_gst_info (ml_single * single_h, const GstTensorsInfo * in_info)
{
GstTensorsInfo out_info;
int status = ML_ERROR_NONE;
int ret = -EINVAL;
gst_tensors_info_init (&out_info);
ret = single_h->klass->set_input_info (single_h->filter, in_info, &out_info);
if (ret == 0) {
gst_tensors_info_free (&single_h->in_info);
gst_tensors_info_free (&single_h->out_info);
gst_tensors_info_copy (&single_h->in_info, in_info);
gst_tensors_info_copy (&single_h->out_info, &out_info);
__setup_in_out_tensors (single_h);
} else if (ret == -ENOENT) {
status = ML_ERROR_NOT_SUPPORTED;
} else {
status = ML_ERROR_INVALID_PARAMETER;
}
gst_tensors_info_free (&out_info);
return status;
}
/**
* @brief Set the info for input/output tensors
*/
static int
ml_single_set_inout_tensors_info (GObject * object,
const gboolean is_input, ml_tensors_info_s * tensors_info)
{
int status = ML_ERROR_NONE;
GstTensorsInfo info;
gchar *str_dim, *str_type, *str_name;
const gchar *str_type_name, *str_name_name;
const gchar *prefix;
if (is_input) {
prefix = INPUT_STR;
str_type_name = CONCAT_MACRO_STR (INPUT_STR, TYPE_STR);
str_name_name = CONCAT_MACRO_STR (INPUT_STR, NAME_STR);
} else {
prefix = OUTPUT_STR;
str_type_name = CONCAT_MACRO_STR (OUTPUT_STR, TYPE_STR);
str_name_name = CONCAT_MACRO_STR (OUTPUT_STR, NAME_STR);
}
_ml_error_report_return_continue_iferr
(_ml_tensors_info_copy_from_ml (&info, tensors_info),
"Cannot fetch tensor-info from the given information. Error code: %d",
_ERRNO);
/* Set input option */
str_dim = gst_tensors_info_get_dimensions_string (&info);
str_type = gst_tensors_info_get_types_string (&info);
str_name = gst_tensors_info_get_names_string (&info);
if (!str_dim || !str_type || !str_name) {
if (!str_dim)
_ml_error_report
("Cannot fetch specific tensor-info from the given information: cannot fetch tensor dimension information.");
if (!str_type)
_ml_error_report
("Cannot fetch specific tensor-info from the given information: cannot fetch tensor type information.");
if (!str_name)
_ml_error_report
("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.");
status = ML_ERROR_INVALID_PARAMETER;
} else {
g_object_set (object, prefix, str_dim, str_type_name, str_type,
str_name_name, str_name, NULL);
}
g_free (str_dim);
g_free (str_type);
g_free (str_name);
gst_tensors_info_free (&info);
return status;
}
/**
* @brief Internal static function to set tensors info in the handle.
*/
static gboolean
ml_single_set_info_in_handle (ml_single_h single, gboolean is_input,
ml_tensors_info_s * tensors_info)
{
int status;
ml_single *single_h;
GstTensorsInfo *dest;
gboolean configured = FALSE;
gboolean is_valid = FALSE;
GObject *filter_obj;
single_h = (ml_single *) single;
filter_obj = G_OBJECT (single_h->filter);
if (is_input) {
dest = &single_h->in_info;
configured = single_h->klass->input_configured (single_h->filter);
} else {
dest = &single_h->out_info;
configured = single_h->klass->output_configured (single_h->filter);
}
if (configured) {
/* get configured info and compare with input info */
GstTensorsInfo gst_info;
ml_tensors_info_h info = NULL;
ml_single_get_gst_info (single_h, is_input, &gst_info);
_ml_tensors_info_create_from_gst (&info, &gst_info);
gst_tensors_info_free (&gst_info);
if (tensors_info && !ml_tensors_info_is_equal (tensors_info, info)) {
/* given input info is not matched with configured */
ml_tensors_info_destroy (info);
if (is_input) {
/* try to update tensors info */
status = ml_single_update_info (single, tensors_info, &info);
if (status != ML_ERROR_NONE)
goto done;
} else {
goto done;
}
}
gst_tensors_info_free (dest);
_ml_tensors_info_copy_from_ml (dest, info);
ml_tensors_info_destroy (info);
} else if (tensors_info) {
status =
ml_single_set_inout_tensors_info (filter_obj, is_input, tensors_info);
if (status != ML_ERROR_NONE)
goto done;
gst_tensors_info_free (dest);
_ml_tensors_info_copy_from_ml (dest, tensors_info);
}
is_valid = gst_tensors_info_validate (dest);
done:
return is_valid;
}
/**
* @brief Internal function to create and initialize the single handle.
*/
static ml_single *
ml_single_create_handle (ml_nnfw_type_e nnfw)
{
ml_single *single_h;
GError *error;
gboolean created = FALSE;
single_h = g_new0 (ml_single, 1);
if (single_h == NULL)
_ml_error_report_return (NULL,
"Failed to allocate memory for the single_h handle. Out of memory?");
single_h->filter = g_object_new (G_TYPE_TENSOR_FILTER_SINGLE, NULL);
if (single_h->filter == NULL) {
_ml_error_report
("Failed to create a new instance for filter. Out of memory?");
g_free (single_h);
return NULL;
}
single_h->magic = ML_SINGLE_MAGIC;
single_h->timeout = SINGLE_DEFAULT_TIMEOUT;
single_h->nnfw = nnfw;
single_h->state = IDLE;
single_h->thread = NULL;
single_h->input = NULL;
single_h->output = NULL;
single_h->destroy_data_list = NULL;
single_h->invoking = FALSE;
gst_tensors_info_init (&single_h->in_info);
gst_tensors_info_init (&single_h->out_info);
g_mutex_init (&single_h->mutex);
g_cond_init (&single_h->cond);
single_h->klass = g_type_class_ref (G_TYPE_TENSOR_FILTER_SINGLE);
if (single_h->klass == NULL) {
_ml_error_report
("Failed to get class of the tensor-filter of single API. This binary is not compiled properly or required libraries are not loaded.");
goto done;
}
single_h->thread =
g_thread_try_new (NULL, invoke_thread, (gpointer) single_h, &error);
if (single_h->thread == NULL) {
_ml_error_report
("Failed to create the invoke thread of single API, g_thread_try_new has reported an error: %s.",
error->message);
g_clear_error (&error);
goto done;
}
created = TRUE;
done:
if (!created) {
ml_single_close (single_h);
single_h = NULL;
}
return single_h;
}
/**
* @brief Validate arguments for open
*/
static int
_ml_single_open_custom_validate_arguments (ml_single_h * single,
ml_single_preset * info)
{
if (!single)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'single' (ml_single_h *), is NULL. It should be a valid pointer to an instance of ml_single_h.");
if (!info)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'info' (ml_single_preset *), is NULL. It should be a valid pointer to a valid instance of ml_single_preset.");
/* Validate input tensor info. */
if (info->input_info && !ml_tensors_info_is_valid (info->input_info))
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"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.");
/* Validate output tensor info. */
if (info->output_info && !ml_tensors_info_is_valid (info->output_info))
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"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.");
if (!info->models)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'info' (ml_single_preset *), is not valid. Its models entry if NULL (info->models is NULL).");
if (info->invoke_async && !info->invoke_async_cb)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'info' (ml_single_preset *), is not valid. It has 'invoke_async' entry but its callback 'invoke_async_cb' is NULL");
return ML_ERROR_NONE;
}