Skip to content

Commit

Permalink
[onert] Introduce CodegenManager class (#12553)
Browse files Browse the repository at this point in the history
This commit introduces CodegenManager class.
This class runs codegen() function to load target backend library
and call the codegen function.

ONE-DCO-1.0-Signed-off-by: Jiyoung Yun <jy910.yun@samsung.com>
Co-authored-by: Sanggyu Lee <sg5.lee@samsung.com>
  • Loading branch information
jyoungyun and Sanggyu Lee committed Jan 31, 2024
1 parent fb04773 commit d2ce5d2
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
81 changes: 81 additions & 0 deletions runtime/onert/core/include/odc/CodegenManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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_ODC_CODEGEN_MANAGER_H__
#define __ONERT_ODC_CODEGEN_MANAGER_H__

#include <string>

namespace onert
{
namespace odc
{

enum class CodegenPreference
{
CODEGEN_PREF_DEFAULT,
// TODO Support Traffic and Cycle codegen preference
CODEGEN_PREF_PERFORMANCE_FIRST,
CODEGEN_PREF_MEMORY_FIRST,
CODEGEN_PREF_COMPILE_TIME_FIRST,
};

class CodegenManager
{
public:
// Non-copyable
CodegenManager(const std::string &model_path) : _model_path(model_path) {}
CodegenManager(CodegenManager const &) = delete;
CodegenManager &operator=(CodegenManager const &) = delete;

public:
/**
* @brief Set model path to export compiled model
*
* @param model_path Model path to export compiled model
*/
void exportModelPath(const std::string &model_path) { _export_model_path = model_path; }

/**
* @brief Get model path to export compiled model
*
* @return Model path to export compiled model
*/
const std::string &exportModelPath() const { return _export_model_path; }

/**
* @brief Execute code generator
*
* @param target Target backend name
* This target string will be used to find a backend library.
* The name of target backend library should follow the following rules:
* 'lib' + {backend extension} + '-gen' + {lib extension}
* And the target string should be a name except 'lib' and {lib extension}.
* For example, if the backend extension is 'aaa', the backend library name
* should be 'libaaa-gen.so', and the target string should be 'aaa-gen'.
* @param pref @c CodegenPreference Codegen preference
*/
bool codegen(const char *target, CodegenPreference pref);

private:
std::string _model_path = "";
std::string _export_model_path = "";
};

} // namespace odc
} // namespace onert

#endif // __ONERT_ODC_CODEGEN_MANAGER_H__
55 changes: 55 additions & 0 deletions runtime/onert/core/src/odc/CodegenManager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 "CodegenLoader.h"
#include "odc/CodegenManager.h"
#include "util/Utils.h"

#include <mutex>

namespace onert
{
namespace odc
{

bool CodegenManager::codegen(const char *target, CodegenPreference pref)
{
if (target == nullptr)
throw std::runtime_error("Target string is not set");

if (_export_model_path.empty())
throw std::runtime_error("Export model path is not set");

if (_model_path.empty())
throw std::runtime_error("Model path does not exist");

// codegen function is thread-unsafe
static std::mutex lock;
std::lock_guard<std::mutex> guard(lock);

auto &codegen_loader = CodegenLoader::instance();
codegen_loader.loadLibrary(target);
const auto code_generator = codegen_loader.get();
// TODO Use compile preference
UNUSED_RELEASE(pref);
const auto result = code_generator->codegen(_model_path.c_str(), _export_model_path.c_str());
codegen_loader.unloadLibrary();

return (result == 0);
}

} // namespace odc
} // namespace onert

0 comments on commit d2ce5d2

Please sign in to comment.