Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4k
Created CSourceMetaData module for model metadata#7002
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
acf414e
Created CSourceMetaData module for model metadata
manupak 15c8d90
Created CSourceMetaData module for model metadata
manupak 61444bf
Created CSourceMetaData module for model metadata
manupak 04b1a75
Created CSourceMetaData module for model metadata
manupak 4cc6f75
Created CSourceMetaData module for model metadata
manupak 7260b8a
Created CSourceMetaData module for model metadata
manupak 9bc4c88
Created CSourceMetaData module for model metadata
manupak 3ea831c
Created CSourceMetaData module for model metadata
manupak 46a09e1
Created CSourceMetaData module for model metadata
manupak 6fbfce1
Created CSourceMetaData module for model metadata
manupak 7d25e9e
Created CSourceMetaData module for model metadata
manupak 18428d6
Created CSourceMetaData module for model metadata
manupak 947edaa
Created CSourceMetaData module for model metadata
manupak 9480b2d
Created CSourceMetaData module for model metadata
manupak d622778
Created CSourceMetaData module for model metadata
manupak aa5578f
Created CSourceMetaData module for model metadata
manupak 30e787d
Created CSourceMetaData module for model metadata
manupak df16f22
Created CSourceMetaData module for model metadata
manupak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -15,7 +15,7 @@ | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # pylint: disable=invalid-name, unused-import, import-outside-toplevel | ||
| # pylint: disable=invalid-name, unused-import, import-outside-toplevel, inconsistent-return-statements | ||
| """Runtime Module namespace.""" | ||
| import os | ||
| import ctypes | ||
| @@ -252,7 +252,7 @@ def _collect_dso_modules(self): | ||
| def _dso_exportable(self): | ||
| return self.type_key == "llvm" or self.type_key == "c" | ||
| def export_library(self, file_name, fcompile=None, addons=None, **kwargs): | ||
| def export_library(self, file_name, fcompile=None, addons=None, workspace_dir=None, **kwargs): | ||
| """Export the module and its imported device code one library. | ||
| This function only works on host llvm modules. | ||
| @@ -268,8 +268,19 @@ def export_library(self, file_name, fcompile=None, addons=None, **kwargs): | ||
| If fcompile has attribute object_format, will compile host library | ||
| to that format. Otherwise, will use default format "o". | ||
| workspace_dir : str, optional | ||
| the path to a directory used to create intermediary | ||
| artifacts for the process exporting of the library. | ||
| If this is not provided a temporary dir will be created. | ||
| kwargs : dict, optional | ||
| Additional arguments passed to fcompile | ||
| Returns | ||
| ------- | ||
| result of fcompile() : unknown, optional | ||
| If the compilation function returns an artifact it would be returned via | ||
| export_library, if any. | ||
| """ | ||
| # NOTE: this function depends on contrib library features | ||
| # which are only available in when TVM function is available. | ||
| @@ -292,22 +303,28 @@ def export_library(self, file_name, fcompile=None, addons=None, **kwargs): | ||
| return | ||
| modules = self._collect_dso_modules() | ||
| temp = _utils.tempdir() | ||
| if workspace_dir is None: | ||
| temp = _utils.tempdir() | ||
| workspace_dir = temp.temp_dir | ||
| files = addons if addons else [] | ||
| is_system_lib = False | ||
| has_c_module = False | ||
| llvm_target_triple = None | ||
| for index, module in enumerate(modules): | ||
| if fcompile is not None and hasattr(fcompile, "object_format"): | ||
| object_format = fcompile.object_format | ||
| if module.type_key == "c": | ||
| object_format = "c" | ||
| has_c_module = True | ||
| else: | ||
| object_format = fcompile.object_format | ||
| else: | ||
| if module.type_key == "llvm": | ||
| object_format = "o" | ||
| else: | ||
| assert module.type_key == "c" | ||
| object_format = "cc" | ||
| object_format = "c" | ||
| has_c_module = True | ||
| path_obj = temp.relpath("lib" + str(index) + "." + object_format) | ||
| path_obj = os.path.join(workspace_dir, f"lib{index}.{object_format}") | ||
| module.save(path_obj) | ||
| files.append(path_obj) | ||
| is_system_lib = ( | ||
| @@ -330,25 +347,28 @@ def export_library(self, file_name, fcompile=None, addons=None, **kwargs): | ||
| if self.imported_modules: | ||
| if enabled("llvm") and llvm_target_triple: | ||
| path_obj = temp.relpath("devc." + object_format) | ||
| path_obj = os.path.join(workspace_dir, f"devc.{object_format}") | ||
| m = _ffi_api.ModulePackImportsToLLVM(self, is_system_lib, llvm_target_triple) | ||
| m.save(path_obj) | ||
| files.append(path_obj) | ||
| else: | ||
| path_cc = temp.relpath("devc.cc") | ||
| path_cc = os.path.join(workspace_dir, "devc.c") | ||
| with open(path_cc, "w") as f: | ||
| f.write(_ffi_api.ModulePackImportsToC(self, is_system_lib)) | ||
| files.append(path_cc) | ||
| if has_c_module: | ||
| # The imports could contain a c module but the object format could be tar | ||
| # Thus, it would not recognize the following include paths as options | ||
| # which are there assuming a c compiler is the fcompile. | ||
| if has_c_module and not file_name.endswith(".tar"): | ||
manupak marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| options = [] | ||
| if "options" in kwargs: | ||
| opts = kwargs["options"] | ||
| options = opts if isinstance(opts, (list, tuple)) else [opts] | ||
| opts = options + ["-I" + path for path in find_include_path()] | ||
| kwargs.update({"options": opts}) | ||
| fcompile(file_name, files, **kwargs) | ||
| return fcompile(file_name, files, **kwargs) | ||
| def system_lib(): | ||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -215,20 +215,19 @@ class CodegenC : public MemoizedExprTranslator<std::vector<Output>>, public Code | ||
| class CSourceCodegen : public CSourceModuleCodegenBase { | ||
| public: | ||
| std::pair<std::string, Array<String>> GenCFunc(const Function& func) { | ||
| std::tuple<Array<String>, String, String> GenCFunc(const Function& func) { | ||
| ICHECK(func.defined()) << "Input error: expect a Relay function."; | ||
| // Record the external symbol for runtime lookup. | ||
| auto sid = GetExtSymbol(func); | ||
| CodegenC builder(sid); | ||
| CodegenC builder(GetExtSymbol(func)); | ||
| auto out = builder.VisitExpr(func->body); | ||
| code_stream_ << builder.JIT(out); | ||
| return {sid, builder.const_vars_}; | ||
manupak marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return std::make_tuple(builder.const_vars_, builder.ext_func_id_, builder.JIT(out)); | ||
| } | ||
| runtime::Module CreateCSourceModule(const ObjectRef& ref) override { | ||
| ICHECK(ref->IsInstance<FunctionNode>()); | ||
| auto res = GenCFunc(Downcast<Function>(ref)); | ||
| Array<String> variables = std::get<0>(res); | ||
| String func_name = std::get<1>(res); | ||
| // Create headers | ||
| code_stream_ << "#include <cstring>\n"; | ||
| code_stream_ << "#include <vector>\n"; | ||
| @@ -259,18 +258,13 @@ class CSourceCodegen : public CSourceModuleCodegenBase { | ||
| )op_macro"; | ||
| code_stream_ << operator_macro << "\n\n"; | ||
| ICHECK(ref->IsInstance<FunctionNode>()); | ||
| auto res = GenCFunc(Downcast<Function>(ref)); | ||
| code_stream_ << std::get<2>(res); | ||
| std::string code = code_stream_.str(); | ||
| String sym = std::get<0>(res); | ||
| Array<String> variables = std::get<1>(res); | ||
| // Create a CSource module | ||
| const auto* pf = runtime::Registry::Get("runtime.CSourceModuleCreate"); | ||
| ICHECK(pf != nullptr) << "Cannot find csource module to create the external runtime module"; | ||
| return (*pf)(code, "c", sym, variables); | ||
| return (*pf)(code, "c", Array<String>{func_name}, variables); | ||
| } | ||
| private: | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.