From fc87d4674e912eb92c262a6defdb6af6ccd3dd74 Mon Sep 17 00:00:00 2001 From: Artem Balyshev Date: Thu, 28 Sep 2023 17:44:03 +0300 Subject: [PATCH] [onert-micro] Add float SpaceToDepth kernels This commit adds float SpaceToDepth kernels for onert-micro. ONE-DCO-1.0-Signed-off-by: Artem Balyshev --- .../space_to_depth/FloatSpaceToDepthKernel.h | 98 +++++++++++++++++++ .../space_to_depth/NegSpaceToDepthKernel.h | 87 ++++++++++++++++ .../space_to_depth/TestDataSpaceToDepthBase.h | 60 ++++++++++++ .../pal/cmsisnn/KernelsToBuild.lst | 1 + .../pal/common/PALSpaceToDepth.h | 69 +++++++++++++ .../pal/mcu/KernelsToBuild.lst | 1 + .../src/kernels/SpaceToDepth.cpp | 84 ++++++++-------- .../src/kernels/SpaceToDepth.h | 45 --------- .../src/kernels/SpaceToDepth.test.cpp | 78 +++++++++------ 9 files changed, 407 insertions(+), 116 deletions(-) create mode 100644 onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/FloatSpaceToDepthKernel.h create mode 100644 onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/NegSpaceToDepthKernel.h create mode 100644 onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/TestDataSpaceToDepthBase.h create mode 100644 onert-micro/luci-interpreter/pal/common/PALSpaceToDepth.h delete mode 100644 onert-micro/luci-interpreter/src/kernels/SpaceToDepth.h diff --git a/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/FloatSpaceToDepthKernel.h b/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/FloatSpaceToDepthKernel.h new file mode 100644 index 00000000000..4982d3e764c --- /dev/null +++ b/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/FloatSpaceToDepthKernel.h @@ -0,0 +1,98 @@ +/* + * 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 LUCI_INTERPRETER_TEST_MODELS_FLOAT_SPACE_TO_DEPTH_KERNEL_H +#define LUCI_INTERPRETER_TEST_MODELS_FLOAT_SPACE_TO_DEPTH_KERNEL_H + +#include "TestDataSpaceToDepthBase.h" + +namespace luci_interpreter +{ +namespace test_kernel +{ +namespace space_to_depth_float +{ +/* + * SpaceToDepth Kernel: + * + * Input(1, 4, 4, 3) + * | + * SpaceToDepth + * | + * Output(1, 2, 2, 12) + */ +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, + 0x2c, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x4c, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, + 0xfc, 0xff, 0xff, 0xff, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 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, 0x6c, 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, 0x16, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x07, 0x00, 0x08, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0xd4, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x6f, 0x66, 0x6d, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x10, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x69, 0x66, 0x6d, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 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, 0x1a, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1a, 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 = { + 30.991892, 47.0218, -96.94782, 41.75081, 13.22614, 60.732224, 55.300236, 4.7265563, + -96.22574, -60.987602, -12.473722, -92.82498, 37.063133, 53.67425, 74.06907, 14.154034, + -69.062805, 78.23639, 46.7026, 19.651571, -59.871742, -38.91334, 95.758804, 85.42076, + 6.2756233, 26.522488, 60.209923, 78.387436, 87.826294, -61.8489, -22.721363, 41.05879, + 76.31915, 17.438269, 54.071205, -27.286413, 5.965944, 20.973064, -3.8931062, 26.459524, + -22.902449, -75.43978, 11.68149, -78.51755, 10.050673, 86.07591, 5.0900855, -72.07779}; + +const std::vector reference_output_data = { + 30.991892, 47.0218, -96.94782, 41.75081, 13.22614, 60.732224, 37.063133, 53.67425, + 74.06907, 14.154034, -69.062805, 78.23639, 55.300236, 4.7265563, -96.22574, -60.987602, + -12.473722, -92.82498, 46.7026, 19.651571, -59.871742, -38.91334, 95.758804, 85.42076, + 6.2756233, 26.522488, 60.209923, 78.387436, 87.826294, -61.8489, 5.965944, 20.973064, + -3.8931062, 26.459524, -22.902449, -75.43978, -22.721363, 41.05879, 76.31915, 17.438269, + 54.071205, -27.286413, 11.68149, -78.51755, 10.050673, 86.07591, 5.0900855, -72.07779}; + +} // namespace space_to_depth_float + +class TestDataFloatSpaceToDepth : public TestDataSpaceToDepthBase +{ +public: + TestDataFloatSpaceToDepth() + { + _input_data = space_to_depth_float::input_data; + _reference_output_data = space_to_depth_float::reference_output_data; + _test_kernel_model_circle = space_to_depth_float::test_kernel_model_circle; + } + + ~TestDataFloatSpaceToDepth() override = default; +}; + +} // namespace test_kernel +} // namespace luci_interpreter + +#endif // LUCI_INTERPRETER_TEST_MODELS_FLOAT_SPACE_TO_DEPTH_KERNEL_H diff --git a/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/NegSpaceToDepthKernel.h b/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/NegSpaceToDepthKernel.h new file mode 100644 index 00000000000..09cfcb3d934 --- /dev/null +++ b/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/NegSpaceToDepthKernel.h @@ -0,0 +1,87 @@ +/* + * 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 LUCI_INTERPRETER_TEST_MODELS_NEG_SPACE_TO_DEPTH_KERNEL_H +#define LUCI_INTERPRETER_TEST_MODELS_NEG_SPACE_TO_DEPTH_KERNEL_H + +#include "luci_interpreter/test_models/TestDataBase.h" + +namespace luci_interpreter +{ +namespace test_kernel +{ +namespace neg_input_output_type_mismatch_space_to_depth_kernel +{ +/* + * SpaceToDepth kernel with input output type mismatch: + * + * Input(1, 4, 4, 3) - Float32 + * | + * SpaceToDepth + * | + * Output(1, 2, 2, 12) - Int32 + */ +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, + 0x2c, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x5c, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, + 0xfc, 0xff, 0xff, 0xff, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 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, 0x6c, 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, 0x16, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x07, 0x00, 0x08, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0f, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x6f, 0x66, 0x6d, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x10, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x69, 0x66, 0x6d, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 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, 0x1a, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1a, 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_input_output_type_mismatch_space_to_depth_kernel + +class NegTestDataInputOutputTypeMismatchSpaceToDepthKernel : public NegTestDataBase +{ +public: + NegTestDataInputOutputTypeMismatchSpaceToDepthKernel() + { + _test_kernel_model_circle = + neg_input_output_type_mismatch_space_to_depth_kernel::test_kernel_model_circle; + } + + ~NegTestDataInputOutputTypeMismatchSpaceToDepthKernel() 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_kernel +} // namespace luci_interpreter + +#endif // LUCI_INTERPRETER_TEST_MODELS_NEG_SPACE_TO_DEPTH_KERNEL_H diff --git a/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/TestDataSpaceToDepthBase.h b/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/TestDataSpaceToDepthBase.h new file mode 100644 index 00000000000..ef4c83dcf77 --- /dev/null +++ b/onert-micro/luci-interpreter/include/luci_interpreter/test_models/space_to_depth/TestDataSpaceToDepthBase.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 LUCI_INTERPRETER_TEST_MODELS_SPACE_TO_DEPTH_KERNEL_BASE_H +#define LUCI_INTERPRETER_TEST_MODELS_SPACE_TO_DEPTH_KERNEL_BASE_H + +#include "luci_interpreter/test_models/TestDataBase.h" + +namespace luci_interpreter +{ +namespace test_kernel +{ + +template class TestDataSpaceToDepthBase : public TestDataBase +{ +public: + TestDataSpaceToDepthBase() = 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 0: + return _input_data; + default: + assert(false && "Wrong input index"); + } + } + + const std::vector &get_output_data_by_index(int i) override final + { + assert(i == 0); + return _reference_output_data; + } + +protected: + std::vector _input_data; + std::vector _reference_output_data; + const unsigned char *_test_kernel_model_circle; +}; + +} // namespace test_kernel +} // namespace luci_interpreter + +#endif // LUCI_INTERPRETER_TEST_MODELS_SPACE_TO_DEPTH_KERNEL_BASE_H diff --git a/onert-micro/luci-interpreter/pal/cmsisnn/KernelsToBuild.lst b/onert-micro/luci-interpreter/pal/cmsisnn/KernelsToBuild.lst index 6e8ec181d56..f4cfc290f0f 100644 --- a/onert-micro/luci-interpreter/pal/cmsisnn/KernelsToBuild.lst +++ b/onert-micro/luci-interpreter/pal/cmsisnn/KernelsToBuild.lst @@ -44,6 +44,7 @@ REGISTER_KERNEL(SPACE_TO_BATCH_ND, SpaceToBatchND) REGISTER_KERNEL(STRIDED_SLICE, StridedSlice) REGISTER_KERNEL(SPLIT_V, SplitV) REGISTER_KERNEL(SQRT, Sqrt) +REGISTER_KERNEL(SPACE_TO_DEPTH, SpaceToDepth) REGISTER_KERNEL(QUANTIZE, Quantize) REGISTER_KERNEL(TANH, Tanh) REGISTER_KERNEL(TRANSPOSE, Transpose) diff --git a/onert-micro/luci-interpreter/pal/common/PALSpaceToDepth.h b/onert-micro/luci-interpreter/pal/common/PALSpaceToDepth.h new file mode 100644 index 00000000000..2b13a5a3e6d --- /dev/null +++ b/onert-micro/luci-interpreter/pal/common/PALSpaceToDepth.h @@ -0,0 +1,69 @@ +/* + * 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 LUCI_INTERPRETER_PAL_SPACE_TO_DEPTH_COMMON_H +#define LUCI_INTERPRETER_PAL_SPACE_TO_DEPTH_COMMON_H + +#include "PALUtils.h" + +#include + +namespace luci_interpreter_pal +{ + +template +inline void +SpaceToDepth(const int32_t block_size, const luci_interpreter::RuntimeShape &unextended_input_shape, + const T *input_data, const luci_interpreter::RuntimeShape &unextended_output_shape, + T *output_data) +{ + const luci_interpreter::RuntimeShape input_shape = + luci_interpreter::RuntimeShape::extendedShape(4, unextended_input_shape); + const luci_interpreter::RuntimeShape output_shape = + luci_interpreter::RuntimeShape::extendedShape(4, unextended_output_shape); + + const int input_depth = input_shape.dims(3); + const int input_width = input_shape.dims(2); + const int input_height = input_shape.dims(1); + const int input_batch = input_shape.dims(0); + + for (int in_b = 0; in_b < input_batch; ++in_b) + { + for (int in_h = 0; in_h < input_height; ++in_h) + { + for (int in_w = 0; in_w < input_width; ++in_w) + { + for (int in_d = 0; in_d < input_depth; ++in_d) + { + const int out_d = + in_d + ((in_h % block_size) * block_size + in_w % block_size) * input_depth; + const int out_w = in_w / block_size; + const int out_h = in_h / block_size; + const int out_b = in_b; + + const int input_index = offset(input_shape.dimsData(), in_b, in_h, in_w, in_d); + const int output_index = offset(output_shape.dimsData(), out_b, out_h, out_w, out_d); + + output_data[output_index] = input_data[input_index]; + } + } + } + } +} + +} // namespace luci_interpreter_pal + +#endif // LUCI_INTERPRETER_PAL_SPACE_TO_DEPTH_COMMON_H diff --git a/onert-micro/luci-interpreter/pal/mcu/KernelsToBuild.lst b/onert-micro/luci-interpreter/pal/mcu/KernelsToBuild.lst index 5bb80c76c6f..6f4d5eb6ca3 100644 --- a/onert-micro/luci-interpreter/pal/mcu/KernelsToBuild.lst +++ b/onert-micro/luci-interpreter/pal/mcu/KernelsToBuild.lst @@ -50,6 +50,7 @@ REGISTER_KERNEL(SPACE_TO_BATCH_ND, SpaceToBatchND) REGISTER_KERNEL(STRIDED_SLICE, StridedSlice) REGISTER_KERNEL(SPLIT_V, SplitV) REGISTER_KERNEL(SQRT, Sqrt) +REGISTER_KERNEL(SPACE_TO_DEPTH, SpaceToDepth) REGISTER_KERNEL(QUANTIZE, Quantize) REGISTER_KERNEL(TANH, Tanh) REGISTER_KERNEL(TRANSPOSE, Transpose) diff --git a/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.cpp b/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.cpp index 06cc5faaede..5e3f08dcdbb 100644 --- a/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.cpp +++ b/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.cpp @@ -14,66 +14,64 @@ * limitations under the License. */ -#include "SpaceToDepth.h" -#include "Utils.h" +#include "Builders.h" +#include "kernels/Utils.h" +#include "SISOKernel.h" + #include "PALSpaceToDepth.h" namespace luci_interpreter { -namespace kernels -{ -SpaceToDepth::SpaceToDepth(const Tensor *input, Tensor *output, const SpaceToDepthParams ¶ms) - : KernelWithParams({input}, {output}, params) +void configure_kernel_CircleSpaceToDepth(const circle::Operator *cur_op, + BaseRuntimeGraph *runtime_graph) { -} + kernels::SISOKernel kernel(cur_op, runtime_graph); -void SpaceToDepth::configure() -{ - assert(input()->shape().num_dims() == 4); - assert(output()->element_type() == DataType::FLOAT32 || - output()->element_type() == DataType::U8 || output()->element_type() == DataType::S8 || - output()->element_type() == DataType::S32 || output()->element_type() == DataType::S64); - assert(input()->element_type() == output()->element_type()); + LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input()) == + Tensor::element_type(kernel.output())); + + const auto *options = cur_op->builtin_options_as_SpaceToDepthOptions(); - const int block_size = params().block_size; - const int32_t input_height = input()->shape().dim(1); - const int32_t input_width = input()->shape().dim(2); - int32_t output_height = input_height / block_size; - int32_t output_width = input_width / block_size; + const int32_t block_size = options->block_size(); + LUCI_INTERPRETER_CHECK(block_size > 0); - assert(input_height == output_height * block_size); - assert(input_width == output_width * block_size); + constexpr int kHeightRank = 1; + constexpr int kWidthRank = 2; + constexpr int kDepthRank = 3; - Shape output_shape(4); - output_shape.dim(0) = input()->shape().dim(0); - output_shape.dim(1) = output_height; - output_shape.dim(2) = output_width; - output_shape.dim(3) = input()->shape().dim(3) * block_size * block_size; - // TODO: enable it only if kernel with dynamic shapes - output()->resize(output_shape); + const int input_height = Tensor::dim(kernel.input(), kHeightRank); + const int input_width = Tensor::dim(kernel.input(), kWidthRank); + int output_height = input_height / block_size; + int output_width = input_width / block_size; + + LUCI_INTERPRETER_CHECK(input_height == output_height * block_size); + LUCI_INTERPRETER_CHECK(input_width == output_width * block_size); } -void SpaceToDepth::execute() const +void execute_kernel_CircleSpaceToDepth(const circle::Operator *cur_op, + BaseRuntimeGraph *runtime_graph) { - tflite::SpaceToDepthParams op_params{}; - op_params.block_size = params().block_size; - switch (input()->element_type()) + kernels::SISOKernel kernel(cur_op, runtime_graph); + + const auto *options = cur_op->builtin_options_as_SpaceToDepthOptions(); + const int32_t block_size = options->block_size(); + + switch (Tensor::element_type(kernel.input())) { +#ifndef DIS_FLOAT case DataType::FLOAT32: - luci_interpreter_pal::SpaceToDepth(op_params, getTensorShape(input()), - getTensorData(input()), getTensorShape(output()), - getTensorData(output())); - break; - case DataType::U8: - luci_interpreter_pal::SpaceToDepth(op_params, getTensorShape(input()), - getTensorData(input()), getTensorShape(output()), - getTensorData(output())); + { + luci_interpreter_pal::SpaceToDepth( + block_size, kernels::getTensorRuntimeShape(kernel.input(), runtime_graph), + kernels::getTensorData(runtime_graph->getDataByTensor(kernel.input())), + kernels::getTensorRuntimeShape(kernel.output(), runtime_graph), + kernels::getTensorData(runtime_graph->getDataByTensor(kernel.output()))); break; + } +#endif // DIS_FLOAT default: - assert(false && "Unsupported type."); + assert(false && "Unsupported type"); } } - -} // namespace kernels } // namespace luci_interpreter diff --git a/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.h b/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.h deleted file mode 100644 index e66316b114c..00000000000 --- a/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2020 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 LUCI_INTERPRETER_KERNELS_SPACETODEPTH_H -#define LUCI_INTERPRETER_KERNELS_SPACETODEPTH_H - -#include "core/Kernel.h" -#include "core/KernelParams.h" - -#include - -namespace luci_interpreter -{ -namespace kernels -{ - -class SpaceToDepth : public KernelWithParams -{ -public: - SpaceToDepth(const Tensor *input, Tensor *output, const SpaceToDepthParams ¶ms); - - const Tensor *input() const { return _inputs[0]; } - Tensor *output() const { return _outputs[0]; } - - void configure() override; - void execute() const override; -}; - -} // namespace kernels -} // namespace luci_interpreter - -#endif // LUCI_INTERPRETER_KERNELS_SPACETODEPTH_H diff --git a/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.test.cpp b/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.test.cpp index 4af48861873..18a03507e8f 100644 --- a/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.test.cpp +++ b/onert-micro/luci-interpreter/src/kernels/SpaceToDepth.test.cpp @@ -14,52 +14,74 @@ * limitations under the License. */ -#include "kernels/SpaceToDepth.h" #include "kernels/TestUtils.h" -#include "luci_interpreter/TestMemoryManager.h" +#include "luci_interpreter/test_models/space_to_depth/FloatSpaceToDepthKernel.h" +#include "luci_interpreter/test_models/space_to_depth/NegSpaceToDepthKernel.h" + +#include "loader/ModuleLoader.h" namespace luci_interpreter { -namespace kernels -{ namespace { using namespace testing; -template class SpaceToDepthTest : public ::testing::Test +class SpaceToDepthTest : public ::testing::Test { + // Do nothing }; -using DataTypes = ::testing::Types; -TYPED_TEST_SUITE(SpaceToDepthTest, DataTypes); - -TYPED_TEST(SpaceToDepthTest, SimpleCase) +template +std::vector checkSpaceToDepthKernel(test_kernel::TestDataBase *test_data_base) { - std::unique_ptr memory_manager = std::make_unique(); + MemoryManager memory_manager{}; + RuntimeModule runtime_module{}; + bool dealloc_input = true; - constexpr DataType element_type = getElementType(); - std::vector input_data{1, 5, 6, 7, 2, 3, 4, 8}; - Shape input_shape{1, 2, 2, 2}; - Tensor input_tensor = - makeInputTensor(input_shape, input_data, memory_manager.get()); - std::vector output_data{1, 5, 6, 7, 2, 3, 4, 8}; - std::vector output_shape{1, 1, 1, 8}; - Tensor output_tensor = makeOutputTensor(element_type); + // Load model with single op + auto *model_data_raw = reinterpret_cast(test_data_base->get_model_ptr()); + ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input); - SpaceToDepthParams params{}; - params.block_size = 2; + auto *main_runtime_graph = runtime_module.getMainGraph(); + assert(main_runtime_graph->getNumOfInputTensors() == 1); - SpaceToDepth kernel(&input_tensor, &output_tensor, params); - kernel.configure(); - memory_manager->allocate_memory(output_tensor); - kernel.execute(); + // Set input data + { + auto *input_tensor_data = reinterpret_cast(main_runtime_graph->configureGraphInput(0)); + std::copy(test_data_base->get_input_data_by_index(0).begin(), + test_data_base->get_input_data_by_index(0).end(), input_tensor_data); + } - EXPECT_THAT(extractTensorData(output_tensor), - ::testing::ElementsAreArray(output_data)); - EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape)); + runtime_module.execute(); + + assert(main_runtime_graph->getNumOfOutputTensors() == 1); + + T *output_data = reinterpret_cast(main_runtime_graph->getOutputDataByIndex(0)); + const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T)); + std::vector output_data_vector(output_data, output_data + num_elements); + return output_data_vector; +} + +TEST_F(SpaceToDepthTest, Float_P) +{ + test_kernel::TestDataFloatSpaceToDepth test_data_kernel; + std::vector output_data_vector = checkSpaceToDepthKernel(&test_data_kernel); + EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear( + test_data_kernel.get_output_data_by_index(0), 0.0001f)); +} + +TEST_F(SpaceToDepthTest, Input_output_type_mismatch_NEG) +{ + test_kernel::NegTestDataInputOutputTypeMismatchSpaceToDepthKernel test_data_kernel; + MemoryManager memory_manager{}; + RuntimeModule runtime_module{}; + bool dealloc_input = true; + // Load model with single op + auto *model_data_raw = reinterpret_cast(test_data_kernel.get_model_ptr()); + EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input), + ""); } } // namespace -} // namespace kernels } // namespace luci_interpreter