-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Initial Implementation of TIRToRuntime Target hook #9190
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
Merged
Changes from all commits
Commits
Show all changes
3 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 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 |
|---|---|---|
|
|
@@ -401,12 +401,21 @@ std::pair<IRModule, IRModule> SplitDevHostFuncs(IRModule mod_mixed, const Target | |
| auto opt_mixed = transform::Sequential(mixed_pass_list); | ||
| mod_mixed = opt_mixed(std::move(mod_mixed)); | ||
|
|
||
| // We make an assumption here that the overriden host target | ||
| // can be used alongside the default host codegen based on device type | ||
| // this is so the correct code generator is used later instead of overriding the target. | ||
| // We need better support for inserting multiple kDLCPU targets as our current options | ||
| // are kDeviceKernelLaunch or not | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sigh. we'll get there. |
||
| Target overriden_host_target = target_host; | ||
| if (target->kind->device_type == target_host->kind->device_type) { | ||
| overriden_host_target = target; | ||
| } | ||
| auto host_pass_list = { | ||
| Filter([](const tir::PrimFunc& f) { | ||
| return f->GetAttr<Integer>(tvm::attr::kCallingConv, Integer(CallingConv::kDefault)) != | ||
| CallingConv::kDeviceKernelLaunch; | ||
| }), | ||
| BindTarget(target_host), | ||
| BindTarget(overriden_host_target), | ||
| tir::transform::LowerTVMBuiltin(), | ||
| tir::transform::LowerCustomDatatypes(), | ||
| tir::transform::LowerIntrin(), | ||
|
|
@@ -487,15 +496,27 @@ runtime::Module build(const Map<Target, IRModule>& inputs_arg, const Target& tar | |
|
|
||
| for (const auto& it : inputs) { | ||
| if (it.second.defined()) { | ||
| auto pair = SplitDevHostFuncs(it.second, it.first, target_host, pass_ctx); | ||
| const Target& target = it.first; | ||
| const IRModule& ir_module = it.second; | ||
| auto pair = SplitDevHostFuncs(ir_module, target, target_host, pass_ctx); | ||
| auto& mhost = pair.first; | ||
| auto& mdevice = pair.second; | ||
|
|
||
| ICHECK(mhost.defined()) << "The split host module must be defined"; | ||
|
|
||
| ICHECK(mhost_all.defined()) << "The host module must be defined"; | ||
|
|
||
| mhost_all->Update(mhost); | ||
| // We don't want library modules going back into host codegen | ||
| // unless they're supposed to. Here if we overrode the target host | ||
| // to allow lowering previously we check that it's meant to be placed | ||
| // back into the host Module. | ||
| bool overrides_host_target = target->kind->device_type == target_host->kind->device_type; | ||
| bool non_host_target_kind = target->kind != target_host->kind; | ||
| if (overrides_host_target && non_host_target_kind) { | ||
| device_modules.push_back(codegen::Build(mhost, it.first)); | ||
| } else { | ||
| mhost_all->Update(mhost); | ||
| } | ||
|
|
||
| if (mdevice->functions.size() != 0) { | ||
| device_modules.push_back(codegen::Build(mdevice, it.first)); | ||
|
|
@@ -510,6 +531,7 @@ runtime::Module build(const Map<Target, IRModule>& inputs_arg, const Target& tar | |
| mhost.Import(it); | ||
| } | ||
| } | ||
|
|
||
| return mhost; | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <tvm/relay/transform.h> | ||
| #include <tvm/target/target.h> | ||
|
|
||
| namespace tvm { | ||
|
|
||
| namespace relay { | ||
| namespace contrib { | ||
| namespace example_target_hooks { | ||
| tvm::transform::Pass RelayToTIR(); | ||
| runtime::Module TIRToRuntime(IRModule mod, Target target); | ||
| } // namespace example_target_hooks | ||
| } // namespace contrib | ||
| } // namespace relay | ||
|
|
||
| TVM_REGISTER_TARGET_KIND("example_target_hook", kDLCPU) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: example_target_kind |
||
| .set_attr<FTVMRelayToTIR>("RelayToTIR", relay::contrib::example_target_hooks::RelayToTIR()) | ||
| .set_attr<FTVMTIRToRuntime>("TIRToRuntime", relay::contrib::example_target_hooks::TIRToRuntime); | ||
|
|
||
| } // namespace tvm | ||
64 changes: 64 additions & 0 deletions
64
src/relay/backend/contrib/example_target_hooks/tir_to_runtime.cc
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <sstream> | ||
| #include <string> | ||
|
|
||
| #include "../../../../target/source/codegen_c_host.h" | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
| namespace contrib { | ||
| namespace example_target_hooks { | ||
|
|
||
| using namespace tir; | ||
|
|
||
| class CodeGenExampleTargetHook : public codegen::CodeGenCHost { | ||
| public: | ||
| /*! | ||
| * \brief Emit code that changes adds to multiplies for testing | ||
| */ | ||
| void VisitExpr_(const SubNode* op, std::ostream& os) final { | ||
| os << '('; | ||
| PrintExpr(op->a, os); | ||
| os << " * "; | ||
| PrintExpr(op->b, os); | ||
| os << ')'; | ||
| } | ||
| }; | ||
|
|
||
| runtime::Module TIRToRuntime(IRModule mod, Target target) { | ||
| bool output_ssa = false; | ||
| bool emit_asserts = false; | ||
| CodeGenExampleTargetHook codegen; | ||
| Array<String> function_names; | ||
| codegen.Init(output_ssa, emit_asserts, target->str()); | ||
| for (auto kv : mod->functions) { | ||
| auto prim_func = Downcast<PrimFunc>(kv.second); | ||
| auto global_symbol = prim_func->GetAttr<String>(tvm::attr::kGlobalSymbol); | ||
| function_names.push_back(global_symbol.value()); | ||
| codegen.AddFunction(prim_func); | ||
| } | ||
| std::string code = codegen.Finish(); | ||
| return codegen::CSourceModuleCreate(code, "c", function_names); | ||
| } | ||
|
|
||
| } // namespace example_target_hooks | ||
| } // namespace contrib | ||
| } // namespace relay | ||
| } // namespace tvm |
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
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.
Heads up that @mikepapadim is working here in #9103