Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[onert-micro] Add float ResizeNearestNeighbor kernels #11682

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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_RESIZE_NEAREST_NEIGHBOR_KERNEL_H
#define LUCI_INTERPRETER_TEST_MODELS_NEG_RESIZE_NEAREST_NEIGHBOR_KERNEL_H

#include "luci_interpreter/test_models/TestDataBase.h"

namespace luci_interpreter
{
namespace test_kernel
{
namespace neg_input_output_type_mismatch_resize_nearest_neighbor_kernel
{
/*
* ResizeNearestNeighbor Kernel with input output type mismatch:
*
* Input(1, 4, 4, 8) - Float32
* |
* ResizeNearestNeighbor
* |
* Output(1, 16, 16, 8) - 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,
0x4c, 0x00, 0x00, 0x00, 0x90, 0x01, 0x00, 0x00, 0xac, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x38, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 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,
0x68, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x70, 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, 0x4a, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x07, 0x00, 0x06, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00,
0x44, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xd0, 0xff, 0xff, 0xff, 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, 0x10, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0f, 0x00,
0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x73, 0x69, 0x7a, 0x65,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 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,
0x08, 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, 0x61, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x61, 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_resize_nearest_neighbor_kernel

class NegTestDataInputOutputTypeMismatchResizeNearestNeighborKernel : public NegTestDataBase
{
public:
NegTestDataInputOutputTypeMismatchResizeNearestNeighborKernel()
{
_test_kernel_model_circle =
neg_input_output_type_mismatch_resize_nearest_neighbor_kernel::test_kernel_model_circle;
}

~NegTestDataInputOutputTypeMismatchResizeNearestNeighborKernel() 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_RESIZE_NEAREST_NEIGHBOR_KERNEL_H
Original file line number Diff line number Diff line change
@@ -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_RESIZE_NEAREST_NEIGHBOR_KERNEL_BASE_H
#define LUCI_INTERPRETER_TEST_MODELS_RESIZE_NEAREST_NEIGHBOR_KERNEL_BASE_H

#include "luci_interpreter/test_models/TestDataBase.h"

namespace luci_interpreter
{
namespace test_kernel
{

template <typename T> class TestDataResizeNearestNeighborBase : public TestDataBase<T>
{
public:
TestDataResizeNearestNeighborBase() = default;

const unsigned char *get_model_ptr() override final { return _test_kernel_model_circle; }

const std::vector<T> &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<T> &get_output_data_by_index(int i) override final
{
assert(i == 0);
return _reference_output_data;
}

protected:
std::vector<T> _input_data;
std::vector<T> _reference_output_data;
const unsigned char *_test_kernel_model_circle;
};

} // namespace test_kernel
} // namespace luci_interpreter

#endif // LUCI_INTERPRETER_TEST_MODELS_RESIZE_NEAREST_NEIGHBOR_KERNEL_BASE_H
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ REGISTER_KERNEL(SUM, Sum)
REGISTER_KERNEL(SELECT_V2, SelectV2)
REGISTER_KERNEL(WHILE, While)
REGISTER_KERNEL(RESIZE_BILINEAR, ResizeBilinear)
REGISTER_KERNEL(RESIZE_NEAREST_NEIGHBOR, ResizeNearestNeighbor)
REGISTER_KERNEL(RSQRT, Rsqrt)
REGISTER_KERNEL(NEG, Neg)
REGISTER_KERNEL(ZEROS_LIKE, ZerosLike)
94 changes: 94 additions & 0 deletions onert-micro/luci-interpreter/pal/common/PALResizeNearestNeighbor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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_RESIZE_NEAREST_NEIGHBOR_COMMON_H
#define LUCI_INTERPRETER_PAL_RESIZE_NEAREST_NEIGHBOR_COMMON_H

#include "Params.h"
#include "PALUtils.h"

namespace luci_interpreter_pal
{

inline int32_t getNearestNeighbor(const int input_value, const int32_t input_size,
const int32_t output_size, const bool align_corners,
const bool half_pixel_centers)
{
const float scale = (align_corners && output_size > 1)
? (input_size - 1) / static_cast<float>(output_size - 1)
: input_size / static_cast<float>(output_size);
const float offset = half_pixel_centers ? 0.5f : 0.0f;
int32_t output_value =
std::min(align_corners ? static_cast<int32_t>(std::round((input_value + offset) * scale))
: static_cast<int32_t>(std::floor((input_value + offset) * scale)),
input_size - 1);
if (half_pixel_centers)
{
output_value = std::max(static_cast<int32_t>(0), output_value);
}
return output_value;
}

template <typename T>
inline void
ResizeNearestNeighbor(const ResizeNearestNeighborParams &op_params,
const luci_interpreter::RuntimeShape &unextended_input_shape,
const T *input_data, const luci_interpreter::RuntimeShape &output_size_shape,
const int32_t *output_size_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);

int32_t batches = MatchingDim(input_shape, 0, output_shape, 0);
int32_t input_height = input_shape.dims(1);
int32_t input_width = input_shape.dims(2);
int32_t depth = MatchingDim(input_shape, 3, output_shape, 3);

int32_t output_height = output_size_data[0];
int32_t output_width = output_size_data[1];

const int col_offset = input_shape.dims(3);
const int row_offset = input_shape.dims(2) * col_offset;
const int batch_offset = input_shape.dims(1) * row_offset;

const T *input_ptr = input_data;
T *output_ptr = output_data;
for (int b = 0; b < batches; ++b)
{
for (int y = 0; y < output_height; ++y)
{
int32_t in_y = getNearestNeighbor(y, input_height, output_height, op_params.align_corners,
op_params.half_pixel_centers);
const T *y_input_ptr = input_ptr + in_y * row_offset;
for (int x = 0; x < output_width; ++x)
{
int32_t in_x = getNearestNeighbor(x, input_width, output_width, op_params.align_corners,
op_params.half_pixel_centers);
const T *x_input_ptr = y_input_ptr + in_x * col_offset;
memcpy(output_ptr, x_input_ptr, depth * sizeof(T));
output_ptr += depth;
}
}
input_ptr += batch_offset;
}
}

} // namespace luci_interpreter_pal

#endif // LUCI_INTERPRETER_PAL_RESIZE_NEAREST_NEIGHBOR_COMMON_H
6 changes: 6 additions & 0 deletions onert-micro/luci-interpreter/pal/common/Params.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ struct MeanParams
int16_t axis[4];
};

struct ResizeNearestNeighborParams
{
bool align_corners;
bool half_pixel_centers;
};

struct PadParams
{
int8_t left_padding_count;
Expand Down
1 change: 1 addition & 0 deletions onert-micro/luci-interpreter/pal/mcu/KernelsToBuild.lst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ REGISTER_KERNEL(SELECT_V2, SelectV2)
REGISTER_KERNEL(WHILE, While)
REGISTER_KERNEL(UNIDIRECTIONAL_SEQUENCE_LSTM, UnidirectionalSequenceLSTM)
REGISTER_KERNEL(RESIZE_BILINEAR, ResizeBilinear)
REGISTER_KERNEL(RESIZE_NEAREST_NEIGHBOR, ResizeNearestNeighbor)
REGISTER_KERNEL(RSQRT, Rsqrt)
REGISTER_KERNEL(NEG, Neg)
REGISTER_KERNEL(ZEROS_LIKE, ZerosLike)
45 changes: 0 additions & 45 deletions onert-micro/luci-interpreter/src/kernels/ResizeBilinear.h

This file was deleted.

Loading