-
Notifications
You must be signed in to change notification settings - Fork 157
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
glistening
merged 2 commits into
Samsung:master
from
jyoungyun:onert/introduce_codegen_manager
Jan 31, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thetarget
is necessary, I think.There was a problem hiding this comment.
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
andtarget
. You may think it as the binary of codegen. I agree to add check.There was a problem hiding this comment.
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.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.