forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New relay backend for meta schedule task extraction
- Loading branch information
Showing
4 changed files
with
124 additions
and
1 deletion.
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
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,86 @@ | ||
/* | ||
* 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/meta_schedule/integration.h> | ||
#include <tvm/relay/expr.h> | ||
#include <tvm/relay/expr_functor.h> | ||
#include <tvm/relay/function.h> | ||
#include <tvm/target/target.h> | ||
|
||
#include "../../te/operation/create_primfunc.h" | ||
#include "te_compiler_cache.h" | ||
#include "utils.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
namespace backend { | ||
namespace metaschedule { | ||
|
||
using meta_schedule::ExtractedTask; | ||
|
||
Array<ExtractedTask> ExtractTask(IRModule mod, Target target, Map<String, Constant> params) { | ||
if (params.size()) { | ||
std::unordered_map<std::string, runtime::NDArray> params_; | ||
BaseFunc base_func = mod->Lookup("main"); | ||
ICHECK(base_func->IsInstance<FunctionNode>()); | ||
auto f = relay::backend::BindParamsByName(Downcast<Function>(base_func), params_); | ||
auto gvar = mod->GetGlobalVar("main"); | ||
mod->Add(gvar, f); | ||
} | ||
|
||
Array<Pass> pass_seqs = relay::backend::GetPassPrefix(/*is_homogenous=*/true, /*is_vm=*/true); | ||
pass_seqs.push_back(transform::FuseOps()); | ||
|
||
transform::Sequential seq(pass_seqs); | ||
auto opt_mod = seq(std::move(mod)); | ||
|
||
Array<ExtractedTask> tasks; | ||
LOG(INFO) << opt_mod; | ||
LOG(INFO) << opt_mod->Lookup("main"); | ||
PostOrderVisit(opt_mod->Lookup("main"), [target, &tasks](const Expr& exp) { | ||
if (exp->IsInstance<FunctionNode>()) { | ||
Function relay_func = Downcast<Function>(exp); | ||
if (relay_func->HasNonzeroAttr(attr::kPrimitive)) { | ||
LOG(INFO) << relay_func; | ||
Array<te::Tensor> outputs; | ||
std::string fused_name; | ||
std::tie(outputs, fused_name) = tec::LowerTECompute(target, relay_func); | ||
LOG(INFO) << fused_name; | ||
LOG(INFO) << outputs; | ||
auto prim_func = tir::CreatePrimFunc(outputs); | ||
auto prim_fn_var = GlobalVar(fused_name); | ||
auto relay_mod = IRModule({{prim_fn_var, relay_func}}); | ||
auto tir_mod = IRModule({{prim_fn_var, prim_func}}); | ||
tasks.push_back(ExtractedTask(prim_fn_var->name_hint, relay_mod, target, {tir_mod})); | ||
} | ||
} | ||
}); | ||
|
||
return tasks; | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.backend.MetaScheduleExtractTask") | ||
.set_body_typed([](IRModule mod, Target target, Map<String, Constant> params) { | ||
return ExtractTask(mod, target, params); | ||
}); | ||
|
||
} // namespace metaschedule | ||
} // namespace backend | ||
} // namespace relay | ||
} // namespace tvm |
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
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