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] Introduce CodegenManager class #12553

Merged
merged 2 commits into from
Jan 31, 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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what target is but code that is checking the target is necessary, I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target is the term in compiler. source and target. You may think it as the binary of codegen. I agree to add check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can find the detail explanation of target string in header file.

   * @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'.

This Codegen manager dynamically loads the target library. To find a shared library, a search policy must be defined. In this case, the library is searched for using this target string.

{
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();
ragmani marked this conversation as resolved.
Show resolved Hide resolved
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
Loading