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 execute part #12629

Merged
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
41 changes: 41 additions & 0 deletions onert-micro/onert-micro/include/execute/OMExecuteArgs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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_EXECUTE_EXECUTE_ARGS_H
#define ONERT_MICRO_EXECUTE_EXECUTE_ARGS_H

#include "OMStatus.h"
#include "core/OMRuntimeContext.h"
#include "core/OMRuntimeStorage.h"
#include "core/OMRuntimeModule.h"

namespace onert_micro
{
namespace execute
{

struct OMExecuteArgs
{
core::OMRuntimeStorage &runtime_storage;
core::OMRuntimeContext &runtime_context;
uint16_t kernel_index;
core::OMRuntimeModule &runtime_module;
};

} // namespace execute
} // namespace onert_micro

#endif // ONERT_MICRO_EXECUTE_EXECUTE_ARGS_H
39 changes: 39 additions & 0 deletions onert-micro/onert-micro/include/execute/OMKernelExecute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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_EXECUTE_KERNEL_EXECUTE_H
#define ONERT_MICRO_EXECUTE_KERNEL_EXECUTE_H

#include "OMStatus.h"
#include "core/OMRuntimeContext.h"
#include "core/OMRuntimeStorage.h"
#include "execute/OMExecuteArgs.h"
#include "core/memory/OMRuntimeAllocator.h"

namespace onert_micro
{
namespace execute
{

struct OMKernelExecute
{
static OMStatus executeKernel(OMExecuteArgs &, core::memory::OMRuntimeAllocator &allocator);
};

} // namespace execute
} // namespace onert_micro

#endif // ONERT_MICRO_EXECUTE_KERNEL_EXECUTE_H
136 changes: 136 additions & 0 deletions onert-micro/onert-micro/include/execute/OMKernelExecutionBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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_EXECUTE_KERNEL_EXECUTION_BUILDER_H
#define ONERT_MICRO_EXECUTE_KERNEL_EXECUTION_BUILDER_H

#include "core/reader/OMCircleReader.h"
#include "core/OMKernelType.h"
#include "core/OMRuntimeStorage.h"
#include "core/OMRuntimeContext.h"
#include "execute/OMExecuteArgs.h"

namespace onert_micro
{
namespace execute
{

using KernelExecuteFunc = OMStatus(const OMExecuteArgs &);

#define REGISTER_KERNEL(builtin_operator, name) \
OMStatus execute_kernel_Circle##name(const OMExecuteArgs &);
#include "KernelsToBuild.lst"
#undef REGISTER_KERNEL

#define REGISTER_CUSTOM_KERNEL(name, string_name) \
OMStatus execute_kernel_Circle##name(const OMExecuteArgs &);
#include "CustomKernelsToBuild.lst"
#undef REGISTER_CUSTOM_KERNEL

class KernelBuiltinExecuteRegistry
{
public:
constexpr KernelBuiltinExecuteRegistry() : _operator_execute()
{
#define REGISTER_KERNEL(builtin_operator, name) \
registerKernelExecute(core::OMBuilderID::BuiltinOperator_##builtin_operator, \
execute_kernel_Circle##name);

#include "KernelsToBuild.lst"

#undef REGISTER_KERNEL
}

public:
OMStatus getKernelExecuteFunc(core::OMBuilderID builderID, KernelExecuteFunc **execute_func) const
{
const auto builder_id_opcode = size_t(builderID);
assert(builder_id_opcode < size_t(core::OMBuilderID::BuiltinOperatorsSize));
if (builder_id_opcode >= size_t(core::OMBuilderID::BuiltinOperatorsSize))
{
*execute_func = nullptr;
return UnknownError;
}
*execute_func = _operator_execute[builder_id_opcode];
return Ok;
}

private:
constexpr void registerKernelExecute(core::OMBuilderID id, KernelExecuteFunc *func)
{
assert(size_t(id) < size_t(core::OMBuilderID::BuiltinOperatorsSize));
_operator_execute[size_t(id)] = func;
}

private:
KernelExecuteFunc *_operator_execute[size_t(core::OMBuilderID::BuiltinOperatorsSize)];
};

class KernelCustomExecuteRegistry
{
public:
constexpr KernelCustomExecuteRegistry() : _operator_execute()
{
#define REGISTER_CUSTOM_KERNEL(name, string_name) \
registerKernelExecute(core::OMBuilderID::CUSTOM_##name, execute_kernel_Circle##name);

#include "CustomKernelsToBuild.lst"

#undef REGISTER_CUSTOM_KERNEL
}

public:
OMStatus getKernelExecuteFunc(core::OMBuilderID builderID, KernelExecuteFunc **execute_func) const
{
auto builder_id_opcode = size_t(builderID);
if (builder_id_opcode >= size_t(core::OMBuilderID::Size))
{
*execute_func = nullptr;
return UnknownError;
}
const auto builder_id_offset = size_t(core::OMBuilderID::BuiltinOperatorsSize);
builder_id_opcode -= builder_id_offset - 1;
if (builder_id_opcode < 0)
{
*execute_func = nullptr;
return UnknownError;
}
*execute_func = _operator_execute[builder_id_opcode];
return Ok;
}

private:
constexpr void registerKernelExecute(core::OMBuilderID id, KernelExecuteFunc *func)
{
auto builder_id_opcode = size_t(id);
const auto builder_id_offset = size_t(core::OMBuilderID::BuiltinOperatorsSize);
builder_id_opcode = builder_id_opcode - builder_id_offset - 1;
_operator_execute[builder_id_opcode] = func;
}

private:
KernelExecuteFunc *_operator_execute[size_t(core::OMBuilderID::Size) -
size_t(core::OMBuilderID::BuiltinOperatorsSize) - 1];
};

// Global constexpr kernel builtin and custom execute
constexpr KernelBuiltinExecuteRegistry kernel_builtin_execute;
constexpr KernelCustomExecuteRegistry kernel_custom_execute;

} // namespace execute
} // namespace onert_micro

#endif // ONERT_MICRO_EXECUTE_KERNEL_EXECUTION_BUILDER_H
69 changes: 69 additions & 0 deletions onert-micro/onert-micro/include/execute/OMRuntimeKernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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_EXECUTE_RUNTIME_KERNEL_H
#define ONERT_MICRO_EXECUTE_RUNTIME_KERNEL_H

#include "core/reader/OMCircleReader.h"
#include "core/OMRuntimeContext.h"
#include "core/OMRuntimeStorage.h"

#include <cstdint>

constexpr static uint32_t maxInputSize = 5;
constexpr static uint32_t maxOutputSize = 5;

namespace onert_micro
{
namespace execute
{

class OMRuntimeKernel
{
public:
OMRuntimeKernel() = default;
OMRuntimeKernel(const OMRuntimeKernel &) = delete;
OMRuntimeKernel(OMRuntimeKernel &&) = delete;
~OMRuntimeKernel() = default;
OMRuntimeKernel &operator=(const OMRuntimeKernel &) = delete;
OMRuntimeKernel &&operator=(const OMRuntimeKernel &&) = delete;

public:
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context);

OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage,
core::OMRuntimeContext &context);

public:
const circle::Tensor *inputs[maxInputSize] = {nullptr};
const circle::Tensor *outputs[maxOutputSize] = {nullptr};

uint8_t *inputs_data[maxInputSize] = {nullptr};
uint8_t *outputs_data[maxOutputSize] = {nullptr};

int32_t inputs_index[maxInputSize] = {-1};
int32_t outputs_index[maxOutputSize] = {-1};

uint32_t outputs_num = -1;
uint32_t inputs_num = -1;

const circle::Operator *first_operator = nullptr;
};

} // namespace execute
} // namespace onert_micro

#endif // ONERT_MICRO_EXECUTE_RUNTIME_KERNEL_H
75 changes: 75 additions & 0 deletions onert-micro/onert-micro/include/execute/OMTestUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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_EXECUTE_TESTUTILS_H
#define ONERT_MICRO_EXECUTE_TESTUTILS_H

#include "test_models/TestDataBase.h"
#include "OMInterpreter.h"

#include <type_traits>

#include <gtest/gtest.h>
#include <gmock/gmock.h>

namespace onert_micro
{
namespace execute
{
namespace testing
{

// Array version of `::testing::FloatNear` matcher.
::testing::Matcher<std::vector<float>> FloatArrayNear(const std::vector<float> &values,
float max_abs_error = 1.0e-5f);

template <typename T, typename U = T>
std::vector<U> checkKernel(uint32_t num_inputs,
onert_micro::test_model::TestDataBase<T, U> *test_data_base)
{
onert_micro::OMInterpreter interpreter;
onert_micro::OMConfig config;

interpreter.importModel(reinterpret_cast<const char *>(test_data_base->get_model_ptr()), config);

assert(num_inputs == interpreter.getNumberOfInputs());

for (uint32_t i = 0; i < num_inputs; ++i)
{
T *input_data = reinterpret_cast<T *>(interpreter.getInputDataAt(i));

// Set input data
{
std::copy(test_data_base->get_input_data_by_index(i).begin(),
test_data_base->get_input_data_by_index(i).end(), input_data);
}
}

interpreter.run();

U *output_data = reinterpret_cast<U *>(interpreter.getOutputDataAt(0));
const size_t num_elements = interpreter.getOutputSizeAt(0);
std::vector<U> output_data_vector(output_data, output_data + num_elements);
return output_data_vector;
}

void checkNEGSISOKernel(onert_micro::test_model::NegTestDataBase *test_data_base);

} // namespace testing
} // namespace execute
} // namespace onert_micro

#endif // ONERT_MICRO_EXECUTE_TESTUTILS_H
Loading
Loading