|
| 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 "../utils.h" |
| 20 | + |
| 21 | +namespace tvm { |
| 22 | +namespace meta_schedule { |
| 23 | + |
| 24 | +/*! \brief The type of inline to be performed on a specific block */ |
| 25 | +enum class InlineType : int32_t { |
| 26 | + /*! \brief No inline opportunity */ |
| 27 | + kNoInline = 0, |
| 28 | + /*! \brief Inline the block into its consumer */ |
| 29 | + kInlineIntoConsumer = 1, |
| 30 | + /*! \brief Inline the block into its producer */ |
| 31 | + kInlineIntoProducer = 2, |
| 32 | +}; |
| 33 | + |
| 34 | +/*! \brief The rule that inlines spatial blocks if it satisfies some conditions. */ |
| 35 | +class AutoInlineNode : public ScheduleRuleNode { |
| 36 | + public: |
| 37 | + /*! \brief Checks if the specific block should be inlined */ |
| 38 | + inline InlineType CheckInline(const tir::Schedule& sch, const tir::BlockRV& block_rv); |
| 39 | + |
| 40 | + // Inherited from ScheduleRuleNode |
| 41 | + void InitializeWithTuneContext(const TuneContext& context) final {} |
| 42 | + |
| 43 | + // Inherited from ScheduleRuleNode |
| 44 | + Array<tir::Schedule> Apply(const tir::Schedule& sch, const tir::BlockRV& block_rv) final { |
| 45 | + InlineType inline_type = CheckInline(sch, block_rv); |
| 46 | + if (inline_type == InlineType::kInlineIntoConsumer) { |
| 47 | + sch->ComputeInline(block_rv); |
| 48 | + } else if (inline_type == InlineType::kInlineIntoProducer) { |
| 49 | + sch->ReverseComputeInline(block_rv); |
| 50 | + } |
| 51 | + return {sch}; |
| 52 | + } |
| 53 | + |
| 54 | + public: |
| 55 | + /*! \brief If allows to inline a block into its producer */ |
| 56 | + bool into_producer; |
| 57 | + /*! \brief If allows to inline a block into its consumer */ |
| 58 | + bool into_consumer; |
| 59 | + /*! \brief Always inline constant tensors */ |
| 60 | + bool inline_const_tensor; |
| 61 | + /*! \brief Always disallow if-then-else-like constructs */ |
| 62 | + bool disallow_if_then_else; |
| 63 | + /*! \brief Always require the read-to-write mapping to be injective to do auto inline */ |
| 64 | + bool require_injective; |
| 65 | + /*! \brief Always require the read-to-write mapping to be ordered to do auto inline */ |
| 66 | + bool require_ordered; |
| 67 | + /*! \brief The operators that are disallowed in auto inline */ |
| 68 | + Array<Op> disallow_op; |
| 69 | + |
| 70 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 71 | + v->Visit("into_producer", &into_producer); |
| 72 | + v->Visit("into_consumer", &into_consumer); |
| 73 | + v->Visit("inline_const_tensor", &inline_const_tensor); |
| 74 | + v->Visit("disallow_if_then_else", &disallow_if_then_else); |
| 75 | + v->Visit("require_injective", &require_injective); |
| 76 | + v->Visit("require_ordered", &require_ordered); |
| 77 | + v->Visit("disallow_op", &disallow_op); |
| 78 | + } |
| 79 | + |
| 80 | + static constexpr const char* _type_key = "meta_schedule.AutoInline"; |
| 81 | + TVM_DECLARE_FINAL_OBJECT_INFO(AutoInlineNode, ScheduleRuleNode); |
| 82 | +}; |
| 83 | + |
| 84 | +inline InlineType AutoInlineNode::CheckInline(const tir::Schedule& sch, |
| 85 | + const tir::BlockRV& block_rv) { |
| 86 | + using namespace tvm::tir; |
| 87 | + StmtSRef block_sref = sch->GetSRef(block_rv); |
| 88 | + ScheduleState state = sch->state(); |
| 89 | + const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref); |
| 90 | + BlockRealize realize = GetBlockRealize(state, block_sref); |
| 91 | + // Cond 1. The block has only one write buffer |
| 92 | + if (block->writes.size() != 1) { |
| 93 | + return InlineType::kNoInline; |
| 94 | + } |
| 95 | + // Cond 2. For a block that generates a constant tensor, ignore all other conditions |
| 96 | + if (inline_const_tensor && block->reads.empty()) { |
| 97 | + return InlineType::kInlineIntoConsumer; |
| 98 | + } |
| 99 | + // Cond 3. The block doesn't contain any disallowed operators |
| 100 | + if (!disallow_op.empty() && HasOp(realize, disallow_op)) { |
| 101 | + return InlineType::kNoInline; |
| 102 | + } |
| 103 | + // Cond 4. The block doesn't have any if-then-else-like constructs |
| 104 | + if (disallow_if_then_else && HasIfThenElse(realize)) { |
| 105 | + return InlineType::kNoInline; |
| 106 | + } |
| 107 | + // Cond 5. The mapping from read indices to write indices are injective and ordered |
| 108 | + if (require_injective || require_ordered) { |
| 109 | + const BufferRegion& write_region = block->writes[0]; |
| 110 | + for (const BufferRegion& read_region : block->reads) { |
| 111 | + bool injective, ordered; |
| 112 | + auto _ = std::ignore; |
| 113 | + std::tie(/*exists=*/_, /*surjective=*/_, injective, ordered, /*no_const_read=*/_, |
| 114 | + /*no_shift_read=*/_) = AnalyzeReadWritePattern(read_region, write_region); |
| 115 | + if (require_injective && injective == false) { |
| 116 | + return InlineType::kNoInline; |
| 117 | + } |
| 118 | + if (require_ordered && ordered == false) { |
| 119 | + return InlineType::kNoInline; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + // Last cond: Check inline into the consumers or the spatial producer |
| 124 | + tir::StmtSRef scope_block = tir::GetScopeRoot(sch->state(), block_sref, // |
| 125 | + /*require_stage_pipeline=*/false, // |
| 126 | + /*require_subtree_compact_dataflow=*/false); |
| 127 | + if (into_consumer) { |
| 128 | + Array<tir::StmtSRef> consumer_srefs = GetConsumers(state, block_sref); |
| 129 | + if (!consumer_srefs.empty() && CanComputeInline(state, block_sref)) { |
| 130 | + return InlineType::kInlineIntoConsumer; |
| 131 | + } |
| 132 | + } |
| 133 | + if (into_producer) { |
| 134 | + Array<tir::StmtSRef> producer_srefs = GetProducers(state, block_sref); |
| 135 | + if (producer_srefs.size() == 1 && |
| 136 | + tir::IsCompleteBlock(sch->state(), producer_srefs[0], scope_block) && |
| 137 | + CanReverseComputeInline(state, block_sref)) { |
| 138 | + return InlineType::kInlineIntoProducer; |
| 139 | + } |
| 140 | + } |
| 141 | + return InlineType::kNoInline; |
| 142 | +} |
| 143 | + |
| 144 | +ScheduleRule ScheduleRule::AutoInline(bool into_producer, // |
| 145 | + bool into_consumer, // |
| 146 | + bool inline_const_tensor, // |
| 147 | + bool disallow_if_then_else, // |
| 148 | + bool require_injective, // |
| 149 | + bool require_ordered, // |
| 150 | + Optional<Array<String>> disallow_op) { |
| 151 | + ObjectPtr<AutoInlineNode> n = make_object<AutoInlineNode>(); |
| 152 | + n->into_producer = into_producer; |
| 153 | + n->into_consumer = into_consumer; |
| 154 | + n->inline_const_tensor = inline_const_tensor; |
| 155 | + n->disallow_if_then_else = disallow_if_then_else; |
| 156 | + n->require_injective = require_injective; |
| 157 | + n->require_ordered = require_ordered; |
| 158 | + n->disallow_op.clear(); |
| 159 | + if (disallow_op.defined()) { |
| 160 | + Array<String> op_names = disallow_op.value(); |
| 161 | + n->disallow_op.reserve(op_names.size()); |
| 162 | + for (const String& op_name : op_names) { |
| 163 | + n->disallow_op.push_back(Op::Get(op_name)); |
| 164 | + } |
| 165 | + } |
| 166 | + return ScheduleRule(n); |
| 167 | +} |
| 168 | + |
| 169 | +TVM_REGISTER_NODE_TYPE(AutoInlineNode); |
| 170 | +TVM_REGISTER_GLOBAL("meta_schedule.ScheduleRuleAutoInline") |
| 171 | + .set_body_typed(ScheduleRule::AutoInline); |
| 172 | + |
| 173 | +} // namespace meta_schedule |
| 174 | +} // namespace tvm |
0 commit comments