|
| 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 | +#include <algorithm> |
| 20 | + |
| 21 | +#include "../utils.h" |
| 22 | +#include "tvm/runtime/container/base.h" |
| 23 | + |
| 24 | +namespace tvm { |
| 25 | +namespace meta_schedule { |
| 26 | + |
| 27 | +using tir::BlockRV; |
| 28 | +using tir::LoopRV; |
| 29 | + |
| 30 | +void ApplyTensorization(const tir::Schedule& sch, const String& func_name, |
| 31 | + const tir::PrimFuncNode* func, bool vectorize_init_loop) { |
| 32 | + std::vector<std::pair<std::string, std::function<void(tir::BlockRV)>>> jobs; |
| 33 | + |
| 34 | + tir::PostOrderVisit(func->body, [=, &jobs](const ObjectRef& obj) -> bool { |
| 35 | + if (const auto* block = obj.as<tir::BlockNode>()) { |
| 36 | + tir::StmtSRef block_sref = sch->GetSRef(block); |
| 37 | + if (Optional<String> intrin_name = |
| 38 | + tir::GetAnn<String>(block_sref, tir::attr::meta_schedule_auto_tensorize)) { |
| 39 | + std::string block_name = block_sref->StmtAs<tir::BlockNode>()->name_hint; |
| 40 | + if (block_name.find("init") == std::string::npos) { |
| 41 | + jobs.emplace_back(block_name, [sch, intrin_name](tir::BlockRV block) { |
| 42 | + try { |
| 43 | + sch->Tensorize(block, intrin_name.value()); |
| 44 | + } catch (const std::exception& e) { |
| 45 | + LOG(WARNING) << "Tensorize failed with error " << e.what(); |
| 46 | + } |
| 47 | + }); |
| 48 | + } else if (vectorize_init_loop) { |
| 49 | + jobs.emplace_back(block_name, [sch](tir::BlockRV block) { |
| 50 | + Array<BlockRV> child_blocks = sch->GetChildBlocks(block); |
| 51 | + ICHECK(child_blocks.size() == 1); |
| 52 | + Array<LoopRV> init_loops = sch->GetLoops(child_blocks[0]); |
| 53 | + ICHECK(init_loops.size() == 1); |
| 54 | + sch->Vectorize(init_loops[0]); |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + }); |
| 61 | + |
| 62 | + for (auto kv : jobs) { |
| 63 | + tir::BlockRV block = sch->GetBlock(kv.first, func_name); |
| 64 | + sch->Unannotate(block, tir::attr::meta_schedule_auto_tensorize); |
| 65 | + kv.second(block); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class RewriteTensorizeNode : public PostprocNode { |
| 70 | + public: |
| 71 | + void InitializeWithTuneContext(const TuneContext& context) final {} |
| 72 | + |
| 73 | + bool Apply(const tir::Schedule& sch) final; |
| 74 | + |
| 75 | + void VisitAttrs(tvm::AttrVisitor* v) {} |
| 76 | + |
| 77 | + bool vectorize_init_loop = false; |
| 78 | + |
| 79 | + static constexpr const char* _type_key = "meta_schedule.RewriteTensorize"; |
| 80 | + TVM_DECLARE_FINAL_OBJECT_INFO(RewriteTensorizeNode, PostprocNode); |
| 81 | +}; |
| 82 | + |
| 83 | +bool RewriteTensorizeNode::Apply(const tir::Schedule& sch) { |
| 84 | + for (const auto& kv : sch->mod()->functions) { |
| 85 | + GlobalVar g_var = kv.first; |
| 86 | + BaseFunc base_func = kv.second; |
| 87 | + if (const tir::PrimFuncNode* prim_func = base_func.as<tir::PrimFuncNode>()) { |
| 88 | + ApplyTensorization(sch, g_var->name_hint, prim_func, vectorize_init_loop); |
| 89 | + } |
| 90 | + } |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +Postproc RewriteTensorize(bool vectorize_init_loop) { |
| 95 | + ObjectPtr<RewriteTensorizeNode> n = make_object<RewriteTensorizeNode>(); |
| 96 | + n->vectorize_init_loop = vectorize_init_loop; |
| 97 | + return Postproc(n); |
| 98 | +} |
| 99 | + |
| 100 | +TVM_REGISTER_NODE_TYPE(RewriteTensorizeNode); |
| 101 | +TVM_REGISTER_GLOBAL("meta_schedule.PostprocRewriteTensorize").set_body_typed(RewriteTensorize); |
| 102 | + |
| 103 | +} // namespace meta_schedule |
| 104 | +} // namespace tvm |
0 commit comments