Skip to content

Commit c3d4dcb

Browse files
committed
[single] Refactor: setting tensors info in ml_single_open_custom
Improve ml_single_open_custom's framework-specific in/out tensors info settings handling to a table-based approach. Improve code readability. Signed-off-by: hyunil park <hyunil46.park@samsung.com>
1 parent 3651763 commit c3d4dcb

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

c/src/ml-api-inference-single.c

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,33 @@ _ml_nnfw_to_str_prop (const ml_nnfw_hw_e hw)
10151015
return str_prop;
10161016
}
10171017

1018+
/**
1019+
* @brief Specifies whether input/output tensor information is required for each neural network framework
1020+
* @param nnfw_type The neural network framework type (e.g., TensorFlow, PyTorch)
1021+
* @param requires_in_info TRUE if the framework requires input tensors information
1022+
* @param requires_out_info TRUE if the framework requires output tensors information
1023+
*/
1024+
typedef struct
1025+
{
1026+
ml_nnfw_type_e nnfw_type;
1027+
gboolean requires_in_info;
1028+
gboolean requires_out_info;
1029+
} nnfw_tensors_info;
1030+
1031+
/**
1032+
* @brief A table defining frameworks that require tensor info setting.
1033+
* @details This table-driven approach simplifies adding or modifying framework-specific logic.
1034+
*/
1035+
static const nnfw_tensors_info nnfw_tensors_info_table[] = {
1036+
{ML_NNFW_TYPE_TENSORFLOW, TRUE, TRUE},
1037+
{ML_NNFW_TYPE_SNAP, TRUE, TRUE},
1038+
{ML_NNFW_TYPE_PYTORCH, TRUE, TRUE},
1039+
{ML_NNFW_TYPE_TRIX_ENGINE, TRUE, TRUE},
1040+
{ML_NNFW_TYPE_NCNN, TRUE, TRUE},
1041+
{ML_NNFW_TYPE_ARMNN, FALSE, FALSE}
1042+
};
1043+
1044+
10181045
/**
10191046
* @brief Opens an ML model with the custom options and returns the instance as a handle.
10201047
*/
@@ -1031,6 +1058,9 @@ ml_single_open_custom (ml_single_h * single, ml_single_preset * info)
10311058
g_autofree gchar *converted_models = NULL;
10321059
gchar **list_models;
10331060
guint i, num_models;
1061+
gboolean requires_in_info = FALSE;
1062+
gboolean requires_out_info = FALSE;
1063+
gboolean found_in_table = FALSE;
10341064
char *hw_name;
10351065

10361066
check_feature_state (ML_FEATURE_INFERENCE);
@@ -1097,56 +1127,45 @@ ml_single_open_custom (ml_single_h * single, ml_single_preset * info)
10971127
* 3. Construct a direct connection with the nnfw.
10981128
* Note that we do not construct a pipeline since 2019.12.
10991129
*/
1100-
if (nnfw == ML_NNFW_TYPE_TENSORFLOW || nnfw == ML_NNFW_TYPE_SNAP ||
1101-
nnfw == ML_NNFW_TYPE_PYTORCH || nnfw == ML_NNFW_TYPE_TRIX_ENGINE ||
1102-
nnfw == ML_NNFW_TYPE_NCNN) {
1103-
/* set input and output tensors information */
1104-
if (in_tensors_info && out_tensors_info) {
1105-
status =
1106-
ml_single_set_inout_tensors_info (filter_obj, TRUE, in_tensors_info);
1107-
if (status != ML_ERROR_NONE) {
1108-
_ml_error_report_continue
1109-
("Input tensors info is given; however, failed to set input tensors info. Error code: %d",
1110-
status);
1111-
goto error;
1112-
}
1130+
/* Look up framework properties from the table */
1131+
for (i = 0; i < G_N_ELEMENTS (nnfw_tensors_info_table); i++) {
1132+
if (nnfw_tensors_info_table[i].nnfw_type == nnfw) {
1133+
requires_in_info = nnfw_tensors_info_table[i].requires_in_info;
1134+
requires_out_info = nnfw_tensors_info_table[i].requires_out_info;
1135+
found_in_table = TRUE;
1136+
break;
1137+
}
1138+
}
11131139

1114-
status =
1115-
ml_single_set_inout_tensors_info (filter_obj, FALSE,
1116-
out_tensors_info);
1117-
if (status != ML_ERROR_NONE) {
1118-
_ml_error_report_continue
1119-
("Output tensors info is given; however, failed to set output tensors info. Error code: %d",
1120-
status);
1121-
goto error;
1122-
}
1123-
} else {
1124-
_ml_error_report
1125-
("To run the given nnfw, '%s', with a neural network model, both input and output information should be provided.",
1126-
fw_name);
1140+
if (found_in_table) {
1141+
if (requires_in_info && !in_tensors_info) {
1142+
_ml_error_report ("Framework '%s' requires input information.", fw_name);
1143+
status = ML_ERROR_INVALID_PARAMETER;
1144+
goto error;
1145+
}
1146+
1147+
if (requires_out_info && !out_tensors_info) {
1148+
_ml_error_report ("Framework '%s' requires output information.", fw_name);
11271149
status = ML_ERROR_INVALID_PARAMETER;
11281150
goto error;
11291151
}
1130-
} else if (nnfw == ML_NNFW_TYPE_ARMNN) {
1131-
/* set input and output tensors information, if available */
1152+
11321153
if (in_tensors_info) {
1133-
status =
1134-
ml_single_set_inout_tensors_info (filter_obj, TRUE, in_tensors_info);
1154+
status = ml_single_set_inout_tensors_info (filter_obj, TRUE, in_tensors_info);
11351155
if (status != ML_ERROR_NONE) {
11361156
_ml_error_report_continue
1137-
("With nnfw '%s', input tensors info is optional. However, the user has provided an invalid input tensors info. Error code: %d",
1138-
fw_name, status);
1157+
("Input tensors info is given; however, failed to set input tensors info. Error code: %d",
1158+
status);
11391159
goto error;
11401160
}
11411161
}
1162+
11421163
if (out_tensors_info) {
1143-
status =
1144-
ml_single_set_inout_tensors_info (filter_obj, FALSE,
1145-
out_tensors_info);
1164+
status = ml_single_set_inout_tensors_info (filter_obj, FALSE, out_tensors_info);
11461165
if (status != ML_ERROR_NONE) {
1147-
_ml_error_report_continue
1148-
("With nnfw '%s', output tensors info is optional. However, the user has provided an invalid output tensors info. Error code: %d",
1149-
fw_name, status);
1166+
_ml_error_report_continue
1167+
("Output tensors info is given; however, failed to set output tensors info. Error code: %d",
1168+
status);
11501169
goto error;
11511170
}
11521171
}

0 commit comments

Comments
 (0)