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-internal.c
6 : * @date 19 October 2021
7 : * @brief ML-API Internal Utility Functions for inference implementations
8 : * @see https://github.com/nnstreamer/api
9 : * @author MyungJoo Ham <myungjoo.ham@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 :
13 : #include <string.h>
14 :
15 : #include <nnstreamer_plugin_api_util.h>
16 : #include <tensor_typedef.h>
17 : #include "ml-api-inference-internal.h"
18 : #include "ml-api-internal.h"
19 :
20 : /**
21 : * @brief Check tensor-info has extended rank value.
22 : */
23 : static gboolean
24 26016 : gst_info_is_extended (const GstTensorsInfo * gst_info)
25 : {
26 : GstTensorInfo *_info;
27 : guint i;
28 :
29 53334 : for (i = 0; i < gst_info->num_tensors; i++) {
30 27342 : _info = gst_tensors_info_get_nth_info ((GstTensorsInfo *) gst_info, i);
31 27342 : if (!_info)
32 0 : _ml_error_report_return (FALSE,
33 : "The parameter, gst_info, has invalid number of tensors. The max number of tensors is "
34 : NNS_TENSOR_SIZE_LIMIT_STR);
35 :
36 27342 : if (_info->dimension[ML_TENSOR_RANK_LIMIT_PREV] > 0)
37 24 : return TRUE;
38 : }
39 :
40 25992 : return FALSE;
41 : }
42 :
43 : /**
44 : * @brief Allocates a tensors information handle from gst info.
45 : */
46 : int
47 12986 : _ml_tensors_info_create_from_gst (ml_tensors_info_h * ml_info,
48 : const GstTensorsInfo * gst_info)
49 : {
50 : gboolean is_extended;
51 :
52 12986 : if (!ml_info)
53 1 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
54 : "The parameter, ml_info, is NULL. It should be a valid ml_tensors_info_h instance usually created by ml_tensors_info_create(). This could be an internal bug of ML API.");
55 :
56 12985 : if (!gst_info)
57 1 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
58 : "The parameter, gst_info, is NULL. It should be a valid GstTensorsInfo instance. This could be an internal bug of ML API.");
59 :
60 12984 : is_extended = gst_info_is_extended (gst_info);
61 12984 : if (is_extended)
62 7 : _ml_error_report_return_continue_iferr
63 : (ml_tensors_info_create_extended (ml_info),
64 : "The call to ml_tensors_info_create_extended has failed with %d.",
65 : _ERRNO);
66 : else
67 12977 : _ml_error_report_return_continue_iferr (ml_tensors_info_create (ml_info),
68 : "The call to ml_tensors_info_create has failed with %d.", _ERRNO);
69 :
70 12984 : _ml_tensors_info_copy_from_gst (*ml_info, gst_info);
71 12984 : return ML_ERROR_NONE;
72 : }
73 :
74 : /**
75 : * @brief Copies tensor meta info from gst tensors info.
76 : * @bug Thread safety required. Check its internal users first!
77 : */
78 : int
79 13034 : _ml_tensors_info_copy_from_gst (ml_tensors_info_h ml_info,
80 : const GstTensorsInfo * gst_info)
81 : {
82 : ml_tensors_info_s *_info;
83 :
84 13034 : if (!ml_info)
85 1 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
86 : "The parmater, ml_info, is NULL. It should be a valid ml_tensors_info_s instance, usually created by ml_tensors_info_create(). This is probably an internal bug of ML API.");
87 13033 : if (!gst_info)
88 1 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
89 : "The parmater, gst_info, is NULL. It should be a valid GstTensorsInfo instance. This is probably an internal bug of ML API.");
90 :
91 13032 : _info = (ml_tensors_info_s *) ml_info;
92 :
93 13032 : G_LOCK_UNLESS_NOLOCK (*_info);
94 13032 : _info->is_extended = gst_info_is_extended (gst_info);
95 13032 : gst_tensors_info_copy (&_info->info, gst_info);
96 13032 : G_UNLOCK_UNLESS_NOLOCK (*_info);
97 :
98 13032 : return ML_ERROR_NONE;
99 : }
100 :
101 : /**
102 : * @brief Copies tensor meta info from gst tensors info.
103 : * @bug Thread safety required. Check its internal users first!
104 : */
105 : int
106 6481 : _ml_tensors_info_copy_from_ml (GstTensorsInfo * gst_info,
107 : const ml_tensors_info_h ml_info)
108 : {
109 : ml_tensors_info_s *_info;
110 :
111 6481 : if (!ml_info)
112 1 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
113 : "The parmater, ml_info, is NULL. It should be a valid ml_tensors_info_s instance, usually created by ml_tensors_info_create(). This is probably an internal bug of ML API.");
114 6480 : if (!gst_info)
115 1 : _ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
116 : "The parmater, gst_info, is NULL. It should be a valid GstTensorsInfo instance. This is probably an internal bug of ML API.");
117 :
118 6479 : _info = (ml_tensors_info_s *) ml_info;
119 :
120 6479 : G_LOCK_UNLESS_NOLOCK (*_info);
121 6479 : gst_tensors_info_copy (gst_info, &_info->info);
122 6479 : G_UNLOCK_UNLESS_NOLOCK (*_info);
123 :
124 6479 : return ML_ERROR_NONE;
125 : }
|