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] Reduce pooling code duplication #13289

Merged
merged 1 commit into from
Jun 26, 2024
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
38 changes: 38 additions & 0 deletions onert-micro/onert-micro/include/import/helpers/OMPooingCommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

#ifndef ONERT_MICRO_IMPORT_HELPERS_CONFIGURE_POOLING_KERNEL_COMMON_H
#define ONERT_MICRO_IMPORT_HELPERS_CONFIGURE_POOLING_KERNEL_COMMON_H

#include "import/OMKernelConfigureBuilder.h"
#include "core/OMUtils.h"
#include "OMStatus.h"
#include "execute/OMRuntimeKernel.h"

namespace onert_micro
{
namespace import
{
namespace helpers
{

OMStatus configure_pooling_kernel_common(const OMConfigureArgs &config_args);

} // namespace helpers
} // namespace import
} // namespace onert_micro

#endif // ONERT_MICRO_IMPORT_HELPERS_CONFIGURE_POOLING_KERNEL_COMMON_H
1 change: 1 addition & 0 deletions onert-micro/onert-micro/src/import/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(SOURCES
helpers/OMConfigureSISOKernel.cpp
helpers/OMPadCommon.cpp
helpers/OMConfigureTISOKernel.cpp
helpers/OMPoolingCommon.cpp
)

# Add configure kernels
Expand Down
94 changes: 94 additions & 0 deletions onert-micro/onert-micro/src/import/helpers/OMPoolingCommon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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/helpers/OMPooingCommon.h"

using namespace onert_micro;
using namespace onert_micro::core;

namespace
{

constexpr uint32_t inputTensorIdx = 0;
constexpr uint32_t outputTensorIdx = 0;

} // namespace

OMStatus
onert_micro::import::helpers::configure_pooling_kernel_common(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 = runtime_kernel.inputs[inputTensorIdx];
const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];

assert(input != nullptr);
assert(output != nullptr);

status = utils::checkCondition(input->type() == output->type());
if (status != Ok)
return status;

OMRuntimeShape input_shape(input);
OMRuntimeShape output_shape(output);

status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
if (status != Ok)
return status;

status = utils::checkCondition(input_shape.dimensionsCount() == 4);

auto option = runtime_kernel.first_operator->builtin_options_as_Pool2DOptions();

if (option == nullptr)
return UnknownError;

assert(option != nullptr);

if (input->type() != circle::TensorType_INT8 and input->type() != circle::TensorType_INT16)
return status;

// Check quantization params
if (input->quantization() == nullptr)
{
return NoQuantization;
}

if (input->quantization()->scale()->size() != 1)
{
return UnsupportedType;
}

// Check quantization params
if (output->quantization() == nullptr)
{
return NoQuantization;
}

if (output->quantization()->scale()->size() != 1)
{
return UnsupportedType;
}

return status;
}
80 changes: 2 additions & 78 deletions onert-micro/onert-micro/src/import/kernels/AveragePool2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,89 +14,13 @@
* limitations under the License.
*/

#include "OMStatus.h"

#include "core/OMUtils.h"
#include "core/OMKernelData.h"

#include "import/OMKernelConfigureBuilder.h"

#include "execute/OMRuntimeKernel.h"
#include "execute/OMUtils.h"
#include "import/helpers/OMPooingCommon.h"

using namespace onert_micro;
using namespace onert_micro::core;

namespace
{

constexpr uint32_t inputTensorIdx = 0;
constexpr uint32_t outputTensorIdx = 0;

} // namespace

OMStatus
onert_micro::import::configure_kernel_CircleAveragePool2D(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 = runtime_kernel.inputs[inputTensorIdx];
const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];

assert(input != nullptr);
assert(output != nullptr);

status = utils::checkCondition(input->type() == output->type());
if (status != Ok)
return status;

OMRuntimeShape input_shape(input);
OMRuntimeShape output_shape(output);

status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
if (status != Ok)
return status;

status = utils::checkCondition(input_shape.dimensionsCount() == 4);

auto option = runtime_kernel.first_operator->builtin_options_as_Pool2DOptions();

if (option == nullptr)
return UnknownError;

assert(option != nullptr);

if (input->type() != circle::TensorType_INT8 and input->type() != circle::TensorType_INT16)
return status;

// Check quantization params
if (input->quantization() == nullptr)
{
return NoQuantization;
}

if (input->quantization()->scale()->size() != 1)
{
return UnsupportedType;
}

// Check quantization params
if (output->quantization() == nullptr)
{
return NoQuantization;
}

if (output->quantization()->scale()->size() != 1)
{
return UnsupportedType;
}

return status;
return import::helpers::configure_pooling_kernel_common(config_args);
}
68 changes: 2 additions & 66 deletions onert-micro/onert-micro/src/import/kernels/L2Pool2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,12 @@
* limitations under the License.
*/

#include "OMStatus.h"

#include "core/OMUtils.h"
#include "core/OMKernelData.h"

#include "import/OMKernelConfigureBuilder.h"

#include "execute/OMRuntimeKernel.h"
#include "import/helpers/OMPooingCommon.h"

using namespace onert_micro;
using namespace onert_micro::core;

namespace
{

constexpr uint32_t inputTensorIdx = 0;
constexpr uint32_t outputTensorIdx = 0;

} // namespace

OMStatus onert_micro::import::configure_kernel_CircleL2Pool2D(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 = runtime_kernel.inputs[inputTensorIdx];
const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];

assert(input != nullptr);
assert(output != nullptr);

status = utils::checkCondition(input->type() == output->type());
if (status != Ok)
return status;

OMRuntimeShape input_shape(input);
OMRuntimeShape output_shape(output);

status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
if (status != Ok)
return status;

status = utils::checkCondition(input_shape.dimensionsCount() == 4);

auto option = runtime_kernel.first_operator->builtin_options_as_Pool2DOptions();

if (option == nullptr)
return UnknownError;

assert(option != nullptr);

if (input->type() != circle::TensorType_INT8 and input->type() != circle::TensorType_INT16)
return status;

// Check quantization params
if (input->quantization() == nullptr)
{
return NoQuantization;
}

if (input->quantization()->scale()->size() != 1)
{
return UnsupportedType;
}

return status;
return helpers::configure_pooling_kernel_common(config_args);
}
Loading
Loading