|
| 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 | +#ifndef TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_ |
| 20 | +#define TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_ |
| 21 | + |
| 22 | +#include <tvm/relax/block_builder.h> |
| 23 | +#include <tvm/relax/expr.h> |
| 24 | +#include <tvm/script/ir_builder/base.h> |
| 25 | +#include <tvm/script/ir_builder/ir/frame.h> |
| 26 | +#include <tvm/script/ir_builder/ir/ir.h> |
| 27 | + |
| 28 | +namespace tvm { |
| 29 | +namespace script { |
| 30 | +namespace ir_builder { |
| 31 | +namespace relax { |
| 32 | + |
| 33 | +/*! \brief The base ir_builder frame for the relax dialect. */ |
| 34 | +class RelaxFrameNode : public IRBuilderFrameNode { |
| 35 | + public: |
| 36 | + void VisitAttrs(tvm::AttrVisitor* v) { IRBuilderFrameNode::VisitAttrs(v); } |
| 37 | + |
| 38 | + static constexpr const char* _type_key = "script.ir_builder.relax.RelaxFrame"; |
| 39 | + TVM_DECLARE_BASE_OBJECT_INFO(RelaxFrameNode, IRBuilderFrameNode); |
| 40 | +}; |
| 41 | + |
| 42 | +class RelaxFrame : public IRBuilderFrame { |
| 43 | + public: |
| 44 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(RelaxFrame, IRBuilderFrame, RelaxFrameNode); |
| 45 | + |
| 46 | + protected: |
| 47 | + RelaxFrame() = default; |
| 48 | +}; |
| 49 | + |
| 50 | +/*! \brief The base ir_builder frame for frames with SeqExpr |
| 51 | + i.e. Functions, If branches |
| 52 | + */ |
| 53 | +class SeqExprFrameNode : public RelaxFrameNode { |
| 54 | + public: |
| 55 | + /*! \brief The binding blocks inside the frame. */ |
| 56 | + Array<tvm::relax::BindingBlock> binding_blocks; |
| 57 | + /*! \brief The frame output expr. `NullOpt` when undefined. */ |
| 58 | + Optional<tvm::relax::Expr> output; |
| 59 | + |
| 60 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 61 | + RelaxFrameNode::VisitAttrs(v); |
| 62 | + v->Visit("binding_blocks", &binding_blocks); |
| 63 | + v->Visit("output", &output); |
| 64 | + } |
| 65 | + |
| 66 | + static constexpr const char* _type_key = "script.ir_builder.relax.SeqExprFrame"; |
| 67 | + TVM_DECLARE_BASE_OBJECT_INFO(SeqExprFrameNode, RelaxFrameNode); |
| 68 | + |
| 69 | + public: |
| 70 | + void ExitWithScope() override; |
| 71 | +}; |
| 72 | + |
| 73 | +class SeqExprFrame : public RelaxFrame { |
| 74 | + public: |
| 75 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(SeqExprFrame, RelaxFrame, SeqExprFrameNode); |
| 76 | +}; |
| 77 | + |
| 78 | +/*! \brief The ir_builder frame for the relax function. */ |
| 79 | +class FunctionFrameNode : public SeqExprFrameNode { |
| 80 | + public: |
| 81 | + /*! |
| 82 | + * \brief The function name. |
| 83 | + * \note The name will not be specified in constructor, so it is "Optional", |
| 84 | + * However, we must specify the name by `R.func_name` before exit this frame. |
| 85 | + */ |
| 86 | + Optional<String> name; |
| 87 | + /*! \brief The function params. */ |
| 88 | + Array<tvm::relax::Var> params; |
| 89 | + /*! |
| 90 | + * \brief The function return type. |
| 91 | + * \note Usually the function return type can be deduced by the function body. |
| 92 | + * But we can use this field to specify a more "accurate" return type. |
| 93 | + * i.e. If the `ret_type` is None, try to use the deduced type from body |
| 94 | + * If the `ret_type` is not None, check the deduced type is a base type of the given one. |
| 95 | + */ |
| 96 | + Optional<Type> ret_type; |
| 97 | + /*! \brief The function attributes. */ |
| 98 | + Map<String, ObjectRef> attrs; |
| 99 | + /*! \brief The block builder to create Relax function. */ |
| 100 | + tvm::relax::BlockBuilder block_builder; |
| 101 | + |
| 102 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 103 | + SeqExprFrameNode::VisitAttrs(v); |
| 104 | + v->Visit("name", &name); |
| 105 | + v->Visit("params", ¶ms); |
| 106 | + v->Visit("ret_type", &ret_type); |
| 107 | + v->Visit("attrs", &attrs); |
| 108 | + v->Visit("binding_blocks", &binding_blocks); |
| 109 | + v->Visit("output", &output); |
| 110 | + // `block_builder` is not visited. |
| 111 | + } |
| 112 | + |
| 113 | + static constexpr const char* _type_key = "script.ir_builder.relax.FunctionFrame"; |
| 114 | + TVM_DECLARE_FINAL_OBJECT_INFO(FunctionFrameNode, SeqExprFrameNode); |
| 115 | + |
| 116 | + public: |
| 117 | + void ExitWithScope() final; |
| 118 | +}; |
| 119 | + |
| 120 | +class FunctionFrame : public SeqExprFrame { |
| 121 | + public: |
| 122 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(FunctionFrame, SeqExprFrame, FunctionFrameNode); |
| 123 | +}; |
| 124 | + |
| 125 | +/*! \brief The ir_builder frame for relax binding blocks. */ |
| 126 | +class BlockFrameNode : public RelaxFrameNode { |
| 127 | + public: |
| 128 | + /*! \brief The flag that indicates whether the block is a dataflow block. */ |
| 129 | + bool is_dataflow; |
| 130 | + /*! \brief The variables emitted in this block. */ |
| 131 | + Array<tvm::relax::Var> emitted_vars; |
| 132 | + /*! |
| 133 | + * \brief (Only used for a dataflow block.) A boolean indicating if the dataflow block is ended of |
| 134 | + * construction. If it is true, any new binding trying to be emitted into this block will cause an |
| 135 | + * error. |
| 136 | + */ |
| 137 | + bool block_ended; |
| 138 | + |
| 139 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 140 | + RelaxFrameNode::VisitAttrs(v); |
| 141 | + v->Visit("is_dataflow", &is_dataflow); |
| 142 | + v->Visit("emitted_vars", &emitted_vars); |
| 143 | + v->Visit("block_ended", &block_ended); |
| 144 | + } |
| 145 | + |
| 146 | + static constexpr const char* _type_key = "script.ir_builder.relax.BlockFrame"; |
| 147 | + TVM_DECLARE_FINAL_OBJECT_INFO(BlockFrameNode, RelaxFrameNode); |
| 148 | + |
| 149 | + public: |
| 150 | + void EnterWithScope() final; |
| 151 | + void ExitWithScope() final; |
| 152 | +}; |
| 153 | + |
| 154 | +class BlockFrame : public RelaxFrame { |
| 155 | + public: |
| 156 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(BlockFrame, RelaxFrame, BlockFrameNode); |
| 157 | +}; |
| 158 | + |
| 159 | +/*! |
| 160 | + * \brief A frame that represents if statement. |
| 161 | + * |
| 162 | + * \sa IfFrame |
| 163 | + */ |
| 164 | +class IfFrameNode : public RelaxFrameNode { |
| 165 | + public: |
| 166 | + /*! \brief The condition of the if statement. */ |
| 167 | + tvm::relax::Expr condition; |
| 168 | + /*! \brief The Bindings in the true branch. */ |
| 169 | + Optional<tvm::relax::Expr> then_expr; |
| 170 | + /*! \brief The Bindings in the false branch. */ |
| 171 | + Optional<tvm::relax::Expr> else_expr; |
| 172 | + /*! \brief The Binding var. */ |
| 173 | + tvm::relax::Var var; |
| 174 | + /*! \brief The binding var name. */ |
| 175 | + String var_name; |
| 176 | + |
| 177 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 178 | + RelaxFrameNode::VisitAttrs(v); |
| 179 | + v->Visit("condition", &condition); |
| 180 | + v->Visit("then_expr", &then_expr); |
| 181 | + v->Visit("else_expr", &else_expr); |
| 182 | + v->Visit("var", &var); |
| 183 | + v->Visit("var_name", &var_name); |
| 184 | + } |
| 185 | + |
| 186 | + static constexpr const char* _type_key = "script.ir_builder.relax.IfFrame"; |
| 187 | + TVM_DECLARE_FINAL_OBJECT_INFO(IfFrameNode, RelaxFrameNode); |
| 188 | + |
| 189 | + public: |
| 190 | + /*! |
| 191 | + * \brief The method called when entering RAII scope. |
| 192 | + * \sa tvm::support::With |
| 193 | + */ |
| 194 | + void EnterWithScope() final; |
| 195 | + /*! |
| 196 | + * \brief The method called when exiting RAII scope. |
| 197 | + * \sa tvm::support::With |
| 198 | + */ |
| 199 | + void ExitWithScope() final; |
| 200 | +}; |
| 201 | + |
| 202 | +/*! |
| 203 | + * \brief Managed reference to IfFrameNode. |
| 204 | + * |
| 205 | + * \sa IfFrameNode |
| 206 | + */ |
| 207 | +class IfFrame : public RelaxFrame { |
| 208 | + public: |
| 209 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IfFrame, RelaxFrame, IfFrameNode); |
| 210 | +}; |
| 211 | + |
| 212 | +/*! |
| 213 | + * \brief A frame that represents then. |
| 214 | + * |
| 215 | + * \sa ThenFrame |
| 216 | + */ |
| 217 | +class ThenFrameNode : public SeqExprFrameNode { |
| 218 | + public: |
| 219 | + static constexpr const char* _type_key = "script.ir_builder.relax.ThenFrame"; |
| 220 | + TVM_DECLARE_FINAL_OBJECT_INFO(ThenFrameNode, SeqExprFrameNode); |
| 221 | + |
| 222 | + public: |
| 223 | + /*! |
| 224 | + * \brief The method called when entering RAII scope. |
| 225 | + * \sa tvm::support::With |
| 226 | + */ |
| 227 | + void EnterWithScope() final; |
| 228 | + /*! |
| 229 | + * \brief The method called when exiting RAII scope. |
| 230 | + * \sa tvm::support::With |
| 231 | + */ |
| 232 | + void ExitWithScope() final; |
| 233 | +}; |
| 234 | + |
| 235 | +/*! |
| 236 | + * \brief Managed reference to ThenFrameNode. |
| 237 | + * |
| 238 | + * \sa ThenFrameNode |
| 239 | + */ |
| 240 | +class ThenFrame : public SeqExprFrame { |
| 241 | + public: |
| 242 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ThenFrame, SeqExprFrame, ThenFrameNode); |
| 243 | +}; |
| 244 | + |
| 245 | +/*! |
| 246 | + * \brief A frame that represents else. |
| 247 | + * |
| 248 | + * \sa ElseFrame |
| 249 | + */ |
| 250 | +class ElseFrameNode : public SeqExprFrameNode { |
| 251 | + public: |
| 252 | + static constexpr const char* _type_key = "script.ir_builder.relax.ElseFrame"; |
| 253 | + TVM_DECLARE_FINAL_OBJECT_INFO(ElseFrameNode, SeqExprFrameNode); |
| 254 | + |
| 255 | + public: |
| 256 | + /*! |
| 257 | + * \brief The method called when entering RAII scope. |
| 258 | + * \sa tvm::support::With |
| 259 | + */ |
| 260 | + void EnterWithScope() final; |
| 261 | + /*! |
| 262 | + * \brief The method called when exiting RAII scope. |
| 263 | + * \sa tvm::support::With |
| 264 | + */ |
| 265 | + void ExitWithScope() final; |
| 266 | +}; |
| 267 | + |
| 268 | +/*! |
| 269 | + * \brief Managed reference to ElseFrameNode. |
| 270 | + * |
| 271 | + * \sa ElseFrameNode |
| 272 | + */ |
| 273 | +class ElseFrame : public SeqExprFrame { |
| 274 | + public: |
| 275 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ElseFrame, SeqExprFrame, ElseFrameNode); |
| 276 | +}; |
| 277 | + |
| 278 | +} // namespace relax |
| 279 | +} // namespace ir_builder |
| 280 | +} // namespace script |
| 281 | +} // namespace tvm |
| 282 | + |
| 283 | +#endif // TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_ |
0 commit comments