2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "mio_tflite2121/Helper.h"
19 #include <flatbuffers/flatbuffers.h>
20 #include <gtest/gtest.h>
24 class mio_tflite2121_helper_test : public ::testing::Test
27 void initialization_finish(void)
29 _fbb.Finish(tflite::CreateModelDirect(_fbb, 0, &_opcodes_vec));
33 void add_operator_code(int8_t deprecated_builtin_code, const char *custom_code,
34 tflite::BuiltinOperator builtin_code)
36 _opcodes_vec.push_back(tflite::CreateOperatorCodeDirect(
37 _fbb, deprecated_builtin_code, custom_code, 1 /* version */, builtin_code));
40 const tflite::OperatorCode *get_operator_code(uint8_t idx)
42 return tflite::GetModel(_fbb.GetBufferPointer())->operator_codes()->Get(idx);
46 flatbuffers::FlatBufferBuilder _fbb;
47 std::vector<flatbuffers::Offset<tflite::OperatorCode>> _opcodes_vec;
51 * Extended 'builtin_code' is not in TFLite schema v3.
53 * Thus it is filled with 0(BuiltinOperator_ADD) in schame v3. Please refer to
54 * https://github.com/tensorflow/tensorflow/blob/1ab788fa8d08430be239ab970980b891ad7af494/tensorflow/lite/schema/schema_utils.cc#L28-L31
56 TEST_F(mio_tflite2121_helper_test, v3)
58 // BuiltinOperator_ADD = 0
59 // BuiltinOperator_CONV_2D = 3
60 add_operator_code(3, "", tflite::BuiltinOperator_ADD);
61 initialization_finish();
63 ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
64 ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
65 tflite::BuiltinOperator_CONV_2D);
66 ASSERT_FALSE(mio::tflite::is_custom(get_operator_code(0)));
69 TEST_F(mio_tflite2121_helper_test, v3_custom)
71 // BuiltinOperator_ADD = 0
72 // BuiltinOperator_CUSTOM = 32
73 add_operator_code(32, "custom", tflite::BuiltinOperator_ADD);
74 initialization_finish();
76 ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
77 ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
78 tflite::BuiltinOperator_CUSTOM);
79 ASSERT_TRUE(mio::tflite::is_custom(get_operator_code(0)));
82 TEST_F(mio_tflite2121_helper_test, v3_NEG)
84 // BuiltinOperator_ADD = 0
85 // BuiltinOperator_CUMSUM = 128
86 // deprecated_builtin_code cannot be negative value
87 add_operator_code(128, "", tflite::BuiltinOperator_ADD);
88 initialization_finish();
90 ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
93 TEST_F(mio_tflite2121_helper_test, v3a_under127)
95 // BuiltinOperator_CONV_2D = 3
96 add_operator_code(3, "", tflite::BuiltinOperator_CONV_2D);
97 initialization_finish();
99 ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
100 ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
101 tflite::BuiltinOperator_CONV_2D);
102 ASSERT_FALSE(mio::tflite::is_custom(get_operator_code(0)));
105 TEST_F(mio_tflite2121_helper_test, v3a_under127_NEG)
107 // BuiltinOperator_CONV_2D = 3
108 // BuiltinOperator_CUMSUM = 128
109 // deprecated_builtin_code cannot be negative value
110 add_operator_code(128, "", tflite::BuiltinOperator_CONV_2D);
111 initialization_finish();
113 ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
116 TEST_F(mio_tflite2121_helper_test, v3a_custom)
118 // BuiltinOperator_CUSTOM = 32
119 add_operator_code(32, "custom", tflite::BuiltinOperator_CUSTOM);
120 initialization_finish();
122 ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
123 ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
124 tflite::BuiltinOperator_CUSTOM);
125 ASSERT_TRUE(mio::tflite::is_custom(get_operator_code(0)));
128 TEST_F(mio_tflite2121_helper_test, v3a_custom_NEG)
130 // BuiltinOperator_CUMSUM = 128
131 // deprecated_builtin_code cannot be negative value
132 add_operator_code(128, "custom", tflite::BuiltinOperator_CUSTOM);
133 initialization_finish();
135 ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
138 TEST_F(mio_tflite2121_helper_test, v3a_over127)
140 // BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES = 127
141 // BuiltinOperator_CUMSUM = 128
142 add_operator_code(127, "", tflite::BuiltinOperator_CUMSUM);
143 initialization_finish();
145 ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
146 ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
147 tflite::BuiltinOperator_CUMSUM);
148 ASSERT_FALSE(mio::tflite::is_custom(get_operator_code(0)));
151 TEST_F(mio_tflite2121_helper_test, v3a_over127_NEG)
153 // BuiltinOperator_CUMSUM = 128
154 // deprecated_builtin_code cannot be negative value
155 add_operator_code(128, "", tflite::BuiltinOperator_CUMSUM);
156 initialization_finish();
158 ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));