|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <tvm/meta_schedule/integration.h> |
| 21 | +#include <tvm/relay/expr.h> |
| 22 | +#include <tvm/relay/expr_functor.h> |
| 23 | +#include <tvm/relay/function.h> |
| 24 | +#include <tvm/target/target.h> |
| 25 | + |
| 26 | +#include "../../te/operation/create_primfunc.h" |
| 27 | +#include "te_compiler_cache.h" |
| 28 | +#include "utils.h" |
| 29 | + |
| 30 | +namespace tvm { |
| 31 | +namespace relay { |
| 32 | +namespace backend { |
| 33 | +namespace metaschedule { |
| 34 | + |
| 35 | +using meta_schedule::ExtractedTask; |
| 36 | + |
| 37 | +Array<ExtractedTask> ExtractTask(IRModule mod, Target target, Map<String, Constant> params) { |
| 38 | + if (params.size()) { |
| 39 | + std::unordered_map<std::string, runtime::NDArray> params_; |
| 40 | + BaseFunc base_func = mod->Lookup("main"); |
| 41 | + ICHECK(base_func->IsInstance<FunctionNode>()); |
| 42 | + auto f = relay::backend::BindParamsByName(Downcast<Function>(base_func), params_); |
| 43 | + auto gvar = mod->GetGlobalVar("main"); |
| 44 | + mod->Add(gvar, f); |
| 45 | + } |
| 46 | + |
| 47 | + Array<Pass> pass_seqs = relay::backend::GetPassPrefix(/*is_homogenous=*/true, /*is_vm=*/true); |
| 48 | + pass_seqs.push_back(transform::FuseOps()); |
| 49 | + |
| 50 | + transform::Sequential seq(pass_seqs); |
| 51 | + auto opt_mod = seq(std::move(mod)); |
| 52 | + |
| 53 | + Array<ExtractedTask> tasks; |
| 54 | + LOG(INFO) << opt_mod; |
| 55 | + LOG(INFO) << opt_mod->Lookup("main"); |
| 56 | + PostOrderVisit(opt_mod->Lookup("main"), [target, &tasks](const Expr& exp) { |
| 57 | + if (exp->IsInstance<FunctionNode>()) { |
| 58 | + Function relay_func = Downcast<Function>(exp); |
| 59 | + if (relay_func->HasNonzeroAttr(attr::kPrimitive)) { |
| 60 | + LOG(INFO) << relay_func; |
| 61 | + Array<te::Tensor> outputs; |
| 62 | + std::string fused_name; |
| 63 | + std::tie(outputs, fused_name) = tec::LowerTECompute(target, relay_func); |
| 64 | + LOG(INFO) << fused_name; |
| 65 | + LOG(INFO) << outputs; |
| 66 | + auto prim_func = tir::CreatePrimFunc(outputs); |
| 67 | + auto prim_fn_var = GlobalVar(fused_name); |
| 68 | + auto relay_mod = IRModule({{prim_fn_var, relay_func}}); |
| 69 | + auto tir_mod = IRModule({{prim_fn_var, prim_func}}); |
| 70 | + tasks.push_back(ExtractedTask(prim_fn_var->name_hint, relay_mod, target, {tir_mod})); |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + return tasks; |
| 76 | +} |
| 77 | + |
| 78 | +TVM_REGISTER_GLOBAL("relay.backend.MetaScheduleExtractTask") |
| 79 | + .set_body_typed([](IRModule mod, Target target, Map<String, Constant> params) { |
| 80 | + return ExtractTask(mod, target, params); |
| 81 | + }); |
| 82 | + |
| 83 | +} // namespace metaschedule |
| 84 | +} // namespace backend |
| 85 | +} // namespace relay |
| 86 | +} // namespace tvm |
0 commit comments