From 0d512c2eb65a8629a86c4e33cacee209c1a89f56 Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Mon, 30 Dec 2024 14:22:40 +0900 Subject: [PATCH] [onert-micro] Enable SelectV2 op Enable SelectV2 op on onert-micro ONE-DCO-1.0-Signed-off-by: Chunseok Lee --- .../include/pal/mcu/KernelsToBuild.lst | 2 +- .../onert-micro/include/pal/mcu/PALSelectV2.h | 60 ++++++++ .../select_v2/FloatSelectV2Kernel.h | 99 +++++++++++++ .../test_models/select_v2/NegSelectV2Kernel.h | 93 ++++++++++++ .../select_v2/TestDataSelectV2Base.h | 66 +++++++++ .../src/execute/kernels/SelectV2.cpp | 137 ++++++++++++++++++ .../execute/kernels/tests/SelectV2.test.cpp | 71 +++++++++ .../src/import/kernels/SelectV2.cpp | 71 +++++++++ 8 files changed, 598 insertions(+), 1 deletion(-) create mode 100644 onert-micro/onert-micro/include/pal/mcu/PALSelectV2.h create mode 100644 onert-micro/onert-micro/include/test_models/select_v2/FloatSelectV2Kernel.h create mode 100644 onert-micro/onert-micro/include/test_models/select_v2/NegSelectV2Kernel.h create mode 100644 onert-micro/onert-micro/include/test_models/select_v2/TestDataSelectV2Base.h create mode 100644 onert-micro/onert-micro/src/execute/kernels/SelectV2.cpp create mode 100644 onert-micro/onert-micro/src/execute/kernels/tests/SelectV2.test.cpp create mode 100644 onert-micro/onert-micro/src/import/kernels/SelectV2.cpp diff --git a/onert-micro/onert-micro/include/pal/mcu/KernelsToBuild.lst b/onert-micro/onert-micro/include/pal/mcu/KernelsToBuild.lst index 23dbddc7eb5..fe09b9d5a90 100644 --- a/onert-micro/onert-micro/include/pal/mcu/KernelsToBuild.lst +++ b/onert-micro/onert-micro/include/pal/mcu/KernelsToBuild.lst @@ -75,7 +75,7 @@ REGISTER_KERNEL(TRANSPOSE, Transpose) REGISTER_KERNEL(TRANSPOSE_CONV, TransposeConv) REGISTER_KERNEL(SOFTMAX, Softmax) #/*REGISTER_KERNEL(SUM, Sum)*/ -#/*REGISTER_KERNEL(SELECT_V2, SelectV2)*/ +REGISTER_KERNEL(SELECT_V2, SelectV2) REGISTER_KERNEL(SVDF, SVDF) REGISTER_KERNEL(WHILE, While) #/*REGISTER_KERNEL(UNIDIRECTIONAL_SEQUENCE_LSTM, UnidirectionalSequenceLSTM)*/ diff --git a/onert-micro/onert-micro/include/pal/mcu/PALSelectV2.h b/onert-micro/onert-micro/include/pal/mcu/PALSelectV2.h new file mode 100644 index 00000000000..e40e98fdb58 --- /dev/null +++ b/onert-micro/onert-micro/include/pal/mcu/PALSelectV2.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ONERT_MICRO_PAL_SELECT_V2_COMMON_H +#define ONERT_MICRO_PAL_SELECT_V2_COMMON_H + +#include "PALUtils.h" +#include "ProcessBroadcastShapes.h" +#include "core/OMRuntimeShape.h" +#include "core/OMKernelData.h" + +namespace onert_micro +{ +namespace execute +{ +namespace pal +{ + +template +void Select(const core::OMRuntimeShape &input_condition_shape, const D *input_condition_data, + const core::OMRuntimeShape &input_x_shape, const T *input_x_data, + const core::OMRuntimeShape &input_y_shape, const T *input_y_data, + const core::OMRuntimeShape &output_shape, T *output_data) +{ + int64_t flatsize; + // Allow select operator executions on mixed scalar tensors and one element + // tensors. + if (input_condition_shape.flatSize() == 1 && input_x_shape.flatSize() == 1 && + input_y_shape.flatSize() == 1 && output_shape.flatSize() == 1) + { + flatsize = 1; + } + else + { + flatsize = input_condition_shape.flatSize(); + } + for (int64_t i = 0; i < flatsize; ++i) + { + output_data[i] = input_condition_data[i] ? input_x_data[i] : input_y_data[i]; + } +} + +} // namespace pal +} // namespace execute +} // namespace onert_micro + +#endif // ONERT_MICRO_PAL_SELECT_V2_COMMON_H diff --git a/onert-micro/onert-micro/include/test_models/select_v2/FloatSelectV2Kernel.h b/onert-micro/onert-micro/include/test_models/select_v2/FloatSelectV2Kernel.h new file mode 100644 index 00000000000..610d6bb1427 --- /dev/null +++ b/onert-micro/onert-micro/include/test_models/select_v2/FloatSelectV2Kernel.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ONERT_MICRO_TEST_MODELS_FLOAT_SELECT_V2_KERNEL_H +#define ONERT_MICRO_TEST_MODELS_FLOAT_SELECT_V2_KERNEL_H + +#include "TestDataSelectV2Base.h" + +namespace onert_micro +{ +namespace test_model +{ +namespace select_v2_float +{ +/* + * SelectV2 Kernel: + * + * InputCond(1, 3) X(1, 3) Y(1, 3) + * | | | + * SelectV2 + * | + * Output(1, 3) + */ +const unsigned char test_kernel_model_circle[] = { + 0x18, 0x00, 0x00, 0x00, 0x43, 0x49, 0x52, 0x30, 0x00, 0x00, 0x0e, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x08, 0x00, 0x10, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x38, 0x00, 0x00, 0x00, 0x98, 0x01, 0x00, 0x00, 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x24, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x84, 0xff, 0xff, 0xff, 0x88, 0xff, 0xff, 0xff, 0x8c, 0xff, 0xff, 0xff, + 0x90, 0xff, 0xff, 0xff, 0x94, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x0e, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x04, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, + 0x68, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x07, 0x00, 0x08, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x62, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, + 0x60, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xb8, 0xff, 0xff, 0xff, + 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x6f, 0x66, 0x6d, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0xdc, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0f, 0x00, 0x08, 0x00, 0x04, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, + 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6e, 0x64, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x11, 0x00, 0x00, 0x00, + 0x4f, 0x4e, 0x45, 0x2d, 0x74, 0x66, 0x6c, 0x69, 0x74, 0x65, 0x32, 0x63, 0x69, 0x72, 0x63, 0x6c, + 0x65, 0x00, 0x00, 0x00}; + +const std::vector input_data_1 = {true, false, false}; + +const std::vector input_data_2 = {1.1, 1.2, 1.3}; + +const std::vector input_data_3 = {2.1, 2.2, 2.3}; + +const std::vector reference_output_data = {1.1, 2.2, 2.3}; + +} // namespace select_v2_float + +class TestDataFloatSelectV2 : public TestDataSelectV2Base +{ +public: + TestDataFloatSelectV2() + { + _input_data_1 = select_v2_float::input_data_1; + _input_data_2 = select_v2_float::input_data_2; + _input_data_3 = select_v2_float::input_data_3; + _reference_output_data = select_v2_float::reference_output_data; + _test_kernel_model_circle = select_v2_float::test_kernel_model_circle; + } + + ~TestDataFloatSelectV2() override = default; +}; + +} // namespace test_model +} // namespace onert_micro + +#endif // ONERT_MICRO_TEST_MODELS_FLOAT_SELECT_V2_KERNEL_H diff --git a/onert-micro/onert-micro/include/test_models/select_v2/NegSelectV2Kernel.h b/onert-micro/onert-micro/include/test_models/select_v2/NegSelectV2Kernel.h new file mode 100644 index 00000000000..ff6bcc08082 --- /dev/null +++ b/onert-micro/onert-micro/include/test_models/select_v2/NegSelectV2Kernel.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ONERT_MICRO_TEST_MODELS_NEG_SELECT_V2_KERNEL_H +#define ONERT_MICRO_TEST_MODELS_NEG_SELECT_V2_KERNEL_H + +#include "TestDataSelectV2Base.h" + +namespace onert_micro +{ +namespace test_model +{ +namespace neg_select_v2_input_type_mismatch +{ + +/* + * SelectV2 Kernel with input type mismatch (input_x_type should be equal to input_y_type): + * + * Input_conv(1, 3) - Bool input_x(1, 3) - Int32 input_y(1, 3)- Float32 + * \ | / + * \ | / + * SelectV2 + * | + * Output(1, 4, 4, 1) + */ +const unsigned char test_kernel_model_circle[] = { + 0x18, 0x00, 0x00, 0x00, 0x43, 0x49, 0x52, 0x30, 0x00, 0x00, 0x0e, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x08, 0x00, 0x10, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x38, 0x00, 0x00, 0x00, 0x9c, 0x01, 0x00, 0x00, 0xb8, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x24, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x84, 0xff, 0xff, 0xff, 0x88, 0xff, 0xff, 0xff, 0x8c, 0xff, 0xff, 0xff, + 0x90, 0xff, 0xff, 0xff, 0x94, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x0e, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x04, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, + 0x68, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x07, 0x00, 0x08, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x62, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, + 0x60, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xdc, 0xff, 0xff, 0xff, + 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x6f, 0x66, 0x6d, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x65, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0xd8, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, + 0x0c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0f, 0x00, + 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6e, 0x64, + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, + 0x11, 0x00, 0x00, 0x00, 0x4f, 0x4e, 0x45, 0x2d, 0x74, 0x66, 0x6c, 0x69, 0x74, 0x65, 0x32, 0x63, + 0x69, 0x72, 0x63, 0x6c, 0x65, 0x00, 0x00, 0x00}; +} // namespace neg_select_v2_input_type_mismatch + +class NegTestDataInputMismatchSelectV2Kernel : public NegTestDataBase +{ +public: + NegTestDataInputMismatchSelectV2Kernel() + { + _test_kernel_model_circle = neg_select_v2_input_type_mismatch::test_kernel_model_circle; + } + + ~NegTestDataInputMismatchSelectV2Kernel() override = default; + + const unsigned char *get_model_ptr() override final { return _test_kernel_model_circle; } + +protected: + const unsigned char *_test_kernel_model_circle; +}; + +} // namespace test_model +} // namespace onert_micro + +#endif // ONERT_MICRO_TEST_MODELS_NEG_TRANSPOSE_CONV_KERNEL_H diff --git a/onert-micro/onert-micro/include/test_models/select_v2/TestDataSelectV2Base.h b/onert-micro/onert-micro/include/test_models/select_v2/TestDataSelectV2Base.h new file mode 100644 index 00000000000..325503e454d --- /dev/null +++ b/onert-micro/onert-micro/include/test_models/select_v2/TestDataSelectV2Base.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ONERT_MICRO_TEST_MODELS_SELECT_V2_KERNEL_BASE_H +#define ONERT_MICRO_TEST_MODELS_SELECT_V2_KERNEL_BASE_H + +#include "test_models/TestDataBase.h" + +namespace onert_micro +{ +namespace test_model +{ + +template class TestDataSelectV2Base : public TestDataBase +{ +public: + TestDataSelectV2Base() = default; + + const unsigned char *get_model_ptr() override final { return _test_kernel_model_circle; } + + const std::vector &get_input_data_by_index(int i) override final + { + switch (i) + { + case 1: + return _input_data_2; + case 2: + return _input_data_3; + default: + assert(false && "Wrong input index"); + } + } + + const std::vector &get_cond_input() { return _input_data_1; } + + const std::vector &get_output_data_by_index(int i) override final + { + assert(i == 0); + return _reference_output_data; + } + +protected: + std::vector _input_data_1; + std::vector _input_data_2; + std::vector _input_data_3; + std::vector _reference_output_data; + const unsigned char *_test_kernel_model_circle; +}; + +} // namespace test_model +} // namespace onert_micro + +#endif // ONERT_MICRO_TEST_MODELS_SELECT_V2_KERNEL_BASE_H diff --git a/onert-micro/onert-micro/src/execute/kernels/SelectV2.cpp b/onert-micro/onert-micro/src/execute/kernels/SelectV2.cpp new file mode 100644 index 00000000000..b9214727866 --- /dev/null +++ b/onert-micro/onert-micro/src/execute/kernels/SelectV2.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "OMStatus.h" + +#include "core/OMUtils.h" +#include "core/OMRuntimeShape.h" + +#include "execute/OMUtils.h" +#include "execute/OMKernelExecutionBuilder.h" +#include "execute/OMRuntimeKernel.h" +#include "PALSelectV2.h" + +using namespace onert_micro; +using namespace onert_micro::execute; + +namespace +{ + +constexpr int inputCond = 0; +constexpr int inputX = 1; +constexpr int inputY = 2; +constexpr int outputIndex = 0; + +template +void CallSelect(const core::OMRuntimeShape &input_cond_shape, const bool *input_cond_data, + const core::OMRuntimeShape &input_x_shape, const T *input_x_data, + const core::OMRuntimeShape &input_y_shape, const T *input_y_data, + const core::OMRuntimeShape &output_shape, T *output_data) +{ + using Func = decltype(onert_micro::execute::pal::Select) *; + Func select_func; + select_func = onert_micro::execute::pal::Select; + + select_func(input_cond_shape, input_cond_data, input_x_shape, input_x_data, input_y_shape, + input_y_data, output_shape, output_data); +} + +} // namespace + +// NOTE: doesnt currently support dynamic shapes +namespace onert_micro +{ +namespace execute +{ + +OMStatus execute_kernel_CircleSelectV2(const OMExecuteArgs &execute_args) +{ + core::OMRuntimeContext &runtime_context = execute_args.runtime_context; + core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage; + uint16_t op_index = execute_args.kernel_index; + + const circle::Tensor *input_cond; + const circle::Tensor *input_x; + const circle::Tensor *input_y; + const circle::Tensor *output; + + uint8_t *input_cond_data; + uint8_t *input_x_data; + uint8_t *input_y_data; + uint8_t *output_data; + + OMStatus status = Ok; + + // Read kernel + { + execute::OMRuntimeKernel runtime_kernel; + runtime_kernel.readKernel(op_index, runtime_context); + + input_cond = runtime_kernel.inputs[inputCond]; + input_x = runtime_kernel.inputs[inputX]; + input_y = runtime_kernel.inputs[inputY]; + output = runtime_kernel.outputs[outputIndex]; + + assert(input_cond != nullptr); + assert(input_x != nullptr); + assert(input_y != nullptr); + assert(output != nullptr); + + status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context); + if (status != Ok) + return status; + + input_cond_data = runtime_kernel.inputs_data[inputCond]; + input_x_data = runtime_kernel.inputs_data[inputX]; + input_y_data = runtime_kernel.inputs_data[inputY]; + output_data = runtime_kernel.outputs_data[outputIndex]; + + assert(input_cond_data != nullptr); + assert(input_x_data != nullptr); + assert(input_y_data != nullptr); + assert(output_data != nullptr); + } + + const core::OMRuntimeShape input_cond_shape(input_cond); + assert(input_cond_shape.flatSize() > 0); + const core::OMRuntimeShape input_x_shape(input_x); + const core::OMRuntimeShape input_y_shape(input_y); + const core::OMRuntimeShape output_shape(output); + + switch (input_x->type()) + { +#ifndef DIS_FLOAT + case circle::TensorType_FLOAT32: + { + CallSelect(input_cond_shape, core::utils::castInputData(input_cond_data), + input_x_shape, core::utils::castInputData(input_x_data), + input_y_shape, core::utils::castInputData(input_y_data), + output_shape, core::utils::castOutputData(output_data)); + } + break; +#endif + default: + { + status = UnsupportedType; + assert(false && "Unsupported type."); + } + } + + return status; +} + +} // namespace execute +} // namespace onert_micro diff --git a/onert-micro/onert-micro/src/execute/kernels/tests/SelectV2.test.cpp b/onert-micro/onert-micro/src/execute/kernels/tests/SelectV2.test.cpp new file mode 100644 index 00000000000..ab4660dc705 --- /dev/null +++ b/onert-micro/onert-micro/src/execute/kernels/tests/SelectV2.test.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "execute/OMTestUtils.h" +#include "test_models/select_v2/FloatSelectV2Kernel.h" +#include "test_models/select_v2/NegSelectV2Kernel.h" + +namespace onert_micro +{ +namespace +{ + +using namespace testing; + +class SelectV2Test : public ::testing::Test +{ + // Do nothing +}; + +template +std::vector checkSelectV2Kernel(test_model::TestDataSelectV2Base *test_data_base) +{ + onert_micro::OMInterpreter interpreter; + onert_micro::OMConfig config; + + interpreter.importModel(reinterpret_cast(test_data_base->get_model_ptr()), config); + + interpreter.reset(); + interpreter.allocateInputs(); + + bool *input_cond_data = reinterpret_cast(interpreter.getInputDataAt(0)); + T *input_x_data = reinterpret_cast(interpreter.getInputDataAt(1)); + T *input_y_data = reinterpret_cast(interpreter.getInputDataAt(2)); + + std::copy(test_data_base->get_cond_input().begin(), test_data_base->get_cond_input().end(), + input_cond_data); + std::copy(test_data_base->get_input_data_by_index(1).begin(), + test_data_base->get_input_data_by_index(1).end(), input_x_data); + std::copy(test_data_base->get_input_data_by_index(2).begin(), + test_data_base->get_input_data_by_index(2).end(), input_y_data); + + interpreter.run(config); + + T *output_data = reinterpret_cast(interpreter.getOutputDataAt(0)); + const size_t num_elements = interpreter.getOutputSizeAt(0); + std::vector output_data_vector(output_data, output_data + num_elements); + return output_data_vector; +} + +TEST_F(SelectV2Test, Float_P) +{ + test_model::TestDataFloatSelectV2 test_data_kernel; + std::vector output_data_vector = checkSelectV2Kernel(&test_data_kernel); + EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0)); +} + +} // namespace +} // namespace onert_micro diff --git a/onert-micro/onert-micro/src/import/kernels/SelectV2.cpp b/onert-micro/onert-micro/src/import/kernels/SelectV2.cpp new file mode 100644 index 00000000000..668de19462b --- /dev/null +++ b/onert-micro/onert-micro/src/import/kernels/SelectV2.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "import/OMKernelConfigureBuilder.h" +#include "core/OMUtils.h" +#include "OMStatus.h" +#include "execute/OMRuntimeKernel.h" + +using namespace onert_micro; +using namespace onert_micro::core; + +namespace +{ + +constexpr int inputCond = 0; +constexpr int inputX = 1; +constexpr int inputY = 2; +constexpr int outputIndex = 0; + +} // namespace + +namespace onert_micro +{ +namespace import +{ + +OMStatus configure_kernel_CircleSelectV2(const OMConfigureArgs &config_args) +{ + OMRuntimeContext &runtime_context = config_args.runtime_context; + uint16_t op_index = config_args.kernel_index; + + onert_micro::execute::OMRuntimeKernel runtime_kernel; + + OMStatus status = runtime_kernel.readKernel(op_index, runtime_context); + if (status != Ok) + return status; + + const circle::Tensor *input_cond = runtime_kernel.inputs[inputCond]; + const circle::Tensor *input_x = runtime_kernel.inputs[inputX]; + const circle::Tensor *input_y = runtime_kernel.inputs[inputY]; + const circle::Tensor *output = runtime_kernel.outputs[outputIndex]; + + assert(input_cond != nullptr); + assert(input_x != nullptr); + assert(input_y != nullptr); + assert(output != nullptr); + + if (input_cond->type() != circle::TensorType_BOOL) + return status; + + if (input_x->type() != input_y->type()) + return status; + + return status; +} + +} // namespace import +} // namespace onert_micro