tvm
expr.h
Go to the documentation of this file.
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_RELAX_EXPR_H_
20 #define TVM_RELAX_EXPR_H_
21 
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/container/map.h>
24 #include <tvm/ffi/reflection/registry.h>
25 #include <tvm/ir/cow.h>
26 #include <tvm/ir/expr.h>
27 #include <tvm/ir/function.h>
28 #include <tvm/ir/source_map.h>
29 #include <tvm/relax/type.h>
30 #include <tvm/runtime/tensor.h>
31 #include <tvm/tirx/expr.h>
32 #include <tvm/tirx/op.h>
33 
34 #include <functional>
35 
36 namespace tvm {
37 namespace relax {
38 
40 class TupleNode : public ExprNode {
41  public:
43  tvm::ffi::Array<Expr> fields;
44 
45  static void RegisterReflection() {
46  namespace refl = tvm::ffi::reflection;
47  refl::ObjectDef<TupleNode>().def_ro("fields", &TupleNode::fields);
48  }
50 };
51 
52 class Tuple : public Expr {
53  public:
59  TVM_DLL explicit Tuple(tvm::ffi::Array<Expr> fields, Span span = Span());
60 
75  template <typename ExprType, typename = std::enable_if_t<std::is_base_of_v<Expr, ExprType>>>
76  TVM_DLL explicit Tuple(tvm::ffi::Array<ExprType> fields, Span span = Span())
77  : Tuple(fields.Map([](const ExprType& expr) -> Expr { return expr; }), span) {}
78 
81 };
82 
84 class TupleGetItemNode : public ExprNode {
85  public:
89  int index;
90 
91  static void RegisterReflection() {
92  namespace refl = tvm::ffi::reflection;
93  refl::ObjectDef<TupleGetItemNode>()
94  .def_ro("tuple_value", &TupleGetItemNode::tuple)
95  .def_ro("index", &TupleGetItemNode::index);
96  }
98 };
99 
100 class TupleGetItem : public Expr {
101  public:
108  TVM_DLL TupleGetItem(Expr tuple, int index, Span span = Span());
109 
112 };
113 
116 class ShapeExprNode : public ExprNode {
117  public:
119  ffi::Array<PrimExpr> values;
120 
121  static void RegisterReflection() {
122  namespace refl = tvm::ffi::reflection;
123  refl::ObjectDef<ShapeExprNode>().def_ro("values", &ShapeExprNode::values);
124  }
126 };
127 
128 class ShapeExpr : public Expr {
129  public:
130  TVM_DLL explicit ShapeExpr(ffi::Array<PrimExpr> values, Span span = Span());
133 };
134 
136 class VarNode : public ExprNode {
137  public:
142  ffi::String name_hint;
143 
144  static void RegisterReflection() {
145  namespace refl = tvm::ffi::reflection;
146  refl::ObjectDef<VarNode>().def_ro("name_hint", &VarNode::name_hint,
147  refl::AttachFieldFlag::SEqHashIgnore());
148  }
149 
150  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
151  static constexpr const uint32_t _type_child_slots = 1;
153 };
154 
155 class Var : public Expr {
156  public:
157  TVM_DLL explicit Var(ffi::String name_hint, ffi::Optional<Type> ty_annotation,
158  Span span = Span());
160 };
161 
165 class DataflowVarNode : public VarNode {
166  public:
167  static void RegisterReflection() {
168  namespace refl = tvm::ffi::reflection;
169  refl::ObjectDef<DataflowVarNode>();
170  }
171 
172  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
174 };
175 
176 class DataflowVar : public Var {
177  public:
178  TVM_DLL explicit DataflowVar(ffi::String name_hint, ffi::Optional<Type> ty_annotation,
179  Span span = Span());
180 
182 };
183 
189 class ConstantNode : public ExprNode {
190  public:
193 
196 
198  bool is_scalar() const { return data->ndim == 0; }
199 
200  static void RegisterReflection() {
201  namespace refl = tvm::ffi::reflection;
202  refl::ObjectDef<ConstantNode>().def_ro("data", &ConstantNode::data);
203  }
205 };
206 
207 class Constant : public Expr {
208  public:
216  TVM_DLL explicit Constant(runtime::Tensor data, ffi::Optional<Type> ty_annotation = std::nullopt,
217  Span span = Span());
218 
221 };
222 
226 class StringImmNode : public ExprNode {
227  public:
229  ffi::String value;
230 
231  static void RegisterReflection() {
232  namespace refl = tvm::ffi::reflection;
233  refl::ObjectDef<StringImmNode>().def_ro("value", &StringImmNode::value);
234  }
236 };
237 
242 class StringImm : public Expr {
243  public:
249  TVM_DLL explicit StringImm(ffi::String value, Span span = Span());
250 
253 };
254 
258 class DataTypeImmNode : public ExprNode {
259  public:
261  DLDataType value;
262 
263  static void RegisterReflection() {
264  namespace refl = tvm::ffi::reflection;
265  refl::ObjectDef<DataTypeImmNode>().def_ro("value", &DataTypeImmNode::value);
266  }
268 };
269 
274 class DataTypeImm : public Expr {
275  public:
281  TVM_DLL explicit DataTypeImm(DLDataType value, Span span = Span());
282 
285 };
286 
288 class BindingNode : public ffi::Object {
289  public:
290  mutable Span span;
293 
294  static void RegisterReflection() {
295  namespace refl = tvm::ffi::reflection;
296  refl::ObjectDef<BindingNode>()
297  .def_ro("span", &BindingNode::span, refl::AttachFieldFlag::SEqHashIgnore())
298  // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
299  .def_ro("var", &BindingNode::var, refl::AttachFieldFlag::SEqHashDefRecursive());
300  }
301 
302  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
303 
304  TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.Binding", BindingNode, ffi::Object);
305 };
306 
307 class Binding : public ffi::ObjectRef {
308  protected:
309  Binding() = default;
310 
311  public:
312  explicit Binding(ffi::ObjectPtr<BindingNode> n) : ffi::ObjectRef(n) {}
313  explicit Binding(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
314  Binding(const Binding&) = default;
315  Binding(Binding&&) = default;
316  Binding& operator=(const Binding&) = default;
317  Binding& operator=(Binding&&) = default;
318  const BindingNode* operator->() const { return static_cast<const BindingNode*>(data_.get()); }
319  const BindingNode* get() const { return operator->(); }
321 };
322 
330 class MatchCastNode : public BindingNode {
331  public:
336 
337  static void RegisterReflection() {
338  namespace refl = tvm::ffi::reflection;
339  refl::ObjectDef<MatchCastNode>()
340  .def_ro("value", &MatchCastNode::value)
341  // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
342  .def_ro("ty", &MatchCastNode::ty, refl::AttachFieldFlag::SEqHashDefRecursive());
343  }
345 };
346 
351 class MatchCast : public Binding {
352  public:
353  TVM_DLL explicit MatchCast(Var var, Expr value, Type ty, Span span = Span());
354 
357 };
358 
359 class VarBindingNode : public BindingNode {
360  public:
363 
364  static void RegisterReflection() {
365  namespace refl = tvm::ffi::reflection;
366  refl::ObjectDef<VarBindingNode>().def_ro("value", &VarBindingNode::value);
367  // customize the SEqual and SHash methods for better error messages
368  refl::TypeAttrDef<VarBindingNode>()
369  .def("__s_equal__", &VarBindingNode::SEqual)
370  .def("__s_hash__", &VarBindingNode::SHash);
371  }
372 
373  bool SEqual(const VarBindingNode* other,
374  ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const;
375  int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const;
377 };
378 
379 class VarBinding : public Binding {
380  public:
381  TVM_DLL explicit VarBinding(Var var, Expr value, Span span = Span());
384 };
385 
386 class BindingBlockNode : public ffi::Object {
387  public:
388  ffi::Array<Binding> bindings;
389  mutable Span span;
390 
391  static void RegisterReflection() {
392  namespace refl = tvm::ffi::reflection;
393  refl::ObjectDef<BindingBlockNode>()
394  .def_ro("bindings", &BindingBlockNode::bindings)
395  .def_ro("span", &BindingBlockNode::span, refl::AttachFieldFlag::SEqHashIgnore(),
396  refl::DefaultValue(Span()));
397  }
398 
399  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
400  TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.BindingBlock", BindingBlockNode, ffi::Object);
401 };
402 
403 class BindingBlock : public ffi::ObjectRef {
404  public:
405  TVM_DLL explicit BindingBlock(ffi::Array<Binding> bindings, Span span = Span());
407 
409 };
410 
412  public:
413  static void RegisterReflection() {
414  namespace refl = tvm::ffi::reflection;
415  refl::ObjectDef<DataflowBlockNode>();
416  }
419 };
420 
421 class DataflowBlock : public BindingBlock {
422  public:
423  TVM_DLL explicit DataflowBlock(ffi::Array<Binding> bindings, Span span = Span());
426 };
427 
432 class SeqExprNode : public ExprNode {
433  public:
434  ffi::Array<BindingBlock> blocks;
436 
437  static void RegisterReflection() {
438  namespace refl = tvm::ffi::reflection;
439  refl::ObjectDef<SeqExprNode>()
440  .def_ro("blocks", &SeqExprNode::blocks)
441  .def_ro("body", &SeqExprNode::body);
442  refl::TypeAttrDef<SeqExprNode>()
443  .def("__s_equal__", &SeqExprNode::SEqual)
444  .def("__s_hash__", &SeqExprNode::SHash);
445  }
446 
447  bool SEqual(const SeqExprNode* other,
448  ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const {
449  // Establish mappings for symbolic variables defined by bindings before
450  // comparing their uses in the SeqExpr result type and body.
451  return equal(blocks, other->blocks, false, "blocks") && equal(ty, other->ty, false, "ty") &&
452  equal(body, other->body, false, "body");
453  }
454 
455  int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const {
456  int64_t hash_value = init_hash;
457  hash_value = hash(blocks, hash_value, false);
458  hash_value = hash(ty, hash_value, false);
459  hash_value = hash(body, hash_value, false);
460  return hash_value;
461  }
463 };
464 
465 class SeqExpr : public Expr {
466  public:
467  /* \brief Implicit conversion constructor
468  *
469  * Relax nodes that introduce a new scope (e.g. `relax::Function`)
470  * are required to be held as SeqExpr. This implicit conversion
471  * provides allows callsites to use these member variables when the
472  * C++ compile-time type is a `relax::Expr`. For example,
473  * a transform may use `func.CopyOnWrite()->body = expr;`.
474  *
475  * If the expression is already a `relax::SeqExpr`, the same
476  * underlying `relax::SeqExprNode` is used, and no copies are made.
477  */
478  TVM_DLL SeqExpr(Expr body); // NOLINT(*)
479 
480  TVM_DLL explicit SeqExpr(ffi::Array<BindingBlock> blocks, Expr body, Span span = Span());
483 };
484 
496 class IfNode : public ExprNode {
497  public:
504 
505  static void RegisterReflection() {
506  namespace refl = tvm::ffi::reflection;
507  refl::ObjectDef<IfNode>()
508  .def_ro("cond", &IfNode::cond)
509  .def_ro("true_branch", &IfNode::true_branch)
510  .def_ro("false_branch", &IfNode::false_branch);
511  }
512 
513  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
515 };
516 
517 class If : public Expr {
518  public:
536  TVM_DLL If(Expr cond, Expr true_branch, Expr false_branch, Span span = Span());
537 
540 };
541 
543 class FunctionNode : public BaseFuncNode {
544  public:
546  ffi::Array<Var> params;
550  Type ret_ty = Type::Missing();
552  bool is_pure;
553 
554  static void RegisterReflection() {
555  namespace refl = tvm::ffi::reflection;
556  refl::ObjectDef<FunctionNode>()
557  .def_ro("params", &FunctionNode::params, refl::AttachFieldFlag::SEqHashDefRecursive())
558  .def_ro("body", &FunctionNode::body)
559  .def_ro("ret_ty", &FunctionNode::ret_ty)
560  .def_ro("is_pure", &FunctionNode::is_pure);
561  }
562 
563  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
565 };
566 
567 class Function : public BaseFunc {
568  public:
590  TVM_DLL explicit Function(ffi::Array<Var> params, Expr body, ffi::Optional<Type> ret_ty,
591  bool is_pure = true, DictAttrs attrs = DictAttrs(), Span span = Span());
592 
597  TVM_DLL static Function CreateEmpty(ffi::Array<Var> params, Type ret_ty, bool is_pure = true,
598  DictAttrs attrs = DictAttrs(), Span span = Span());
599 
602 };
603 
604 // TODO(@sunggg): Investigate the exact usage of kComposite, kPartitionedFromPattern, and
605 // kPrimitive.
606 namespace attr {
608 constexpr const char* kPrimitive = "Primitive";
613 constexpr const char* kCodegen = "Codegen";
615 constexpr const char* kComposite = "Composite";
617 constexpr const char* kPartitionedFromPattern = "PartitionedFromPattern";
619 constexpr const char* kWorkspaceSize = "WorkspaceSize";
620 
621 // Note: in the future, we prefer snake_case instead of CamelCase for attributes.
622 // Past ones will be kept for backwards compatibility.
625 constexpr const char* kForcePure = "relax.force_pure";
626 
632 constexpr const char* kNumInput = "num_input";
633 } // namespace attr
634 
636 class ExternFuncNode : public BaseFuncNode {
637  public:
639  ffi::String global_symbol;
640 
641  static void RegisterReflection() {
642  namespace refl = tvm::ffi::reflection;
643  refl::ObjectDef<ExternFuncNode>().def_ro("global_symbol", &ExternFuncNode::global_symbol);
644  }
646 };
647 
648 class ExternFunc : public BaseFunc {
649  public:
650  TVM_DLL ExternFunc(ffi::String global_symbol, Span span = Span());
651  TVM_DLL ExternFunc(ffi::String global_symbol, Type ty, Span span = Span());
652 
655 };
656 
668 TVM_DLL Expr GetShapeOf(const Expr& expr);
669 
670 } // namespace relax
671 } // namespace tvm
672 
673 /* \brief Allow relax.Var as key in STL tables
674  *
675  * For most Relax expressions, it would be ambiguous whether the
676  * expression should follow reference equality or structural equality.
677  * This is not the case for variables, which do not contain nested
678  * internal structure, and are frequently used as keys in lookup
679  * tables.
680  *
681  * Providing `std::hash` and `std::equal_to` specializations for
682  * `relax::Var` allows it to be used as a key in STL tables. For
683  * `relax::Expr`, the user must specify the type of equality used
684  * (e.g. `std::unordered_set<T, StructuralHash, StructuralEqual>` or
685  * `std::unordered_set<T, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>`).
686  */
687 template <>
688 struct std::hash<tvm::relax::Var> {
689  std::size_t operator()(const tvm::relax::Var& var) const {
690  return tvm::ffi::ObjectPtrHash()(var);
691  }
692 };
693 
694 template <>
695 struct std::equal_to<tvm::relax::Var> {
696  bool operator()(const tvm::relax::Var& var_a, const tvm::relax::Var& var_b) const {
697  return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
698  }
699 };
700 
701 #endif // TVM_RELAX_EXPR_H_
Base node of all functions.
Definition: function.h:156
Managed reference to BaseFuncNode.
Definition: function.h:250
Managed reference to DictAttrsNode.
Definition: attrs.h:102
Base type of all the expressions.
Definition: base_expr.h:276
Type ty
The deduced or annotated type of the expression.
Definition: base_expr.h:290
Managed reference to ExprNode.
Definition: base_expr.h:311
Definition: source_map.h:111
Managed reference to TypeNode.
Definition: base_expr.h:77
static Type Missing()
Sentinel for a type that has not been populated yet.
Definition: expr.h:386
ffi::Array< Binding > bindings
Definition: expr.h:388
Span span
Definition: expr.h:389
static void RegisterReflection()
Definition: expr.h:391
TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.BindingBlock", BindingBlockNode, ffi::Object)
Definition: expr.h:403
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(BindingBlock, ffi::ObjectRef, BindingBlockNode)
BindingBlockNode * CopyOnWrite()
BindingBlock(ffi::Array< Binding > bindings, Span span=Span())
The base class of a variable binding in Relax.
Definition: expr.h:288
Var var
The return variable to bound to.
Definition: expr.h:292
static void RegisterReflection()
Definition: expr.h:294
Span span
Definition: expr.h:290
TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.Binding", BindingNode, ffi::Object)
Definition: expr.h:307
const BindingNode * operator->() const
Definition: expr.h:318
Binding & operator=(const Binding &)=default
Binding(const Binding &)=default
Binding & operator=(Binding &&)=default
Binding(Binding &&)=default
const BindingNode * get() const
Definition: expr.h:319
Binding(ffi::ObjectPtr< BindingNode > n)
Definition: expr.h:312
Binding(ffi::UnsafeInit tag)
Definition: expr.h:313
Constant tensor.
Definition: expr.h:189
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.Constant", ConstantNode, ExprNode)
static void RegisterReflection()
Definition: expr.h:200
bool is_scalar() const
Definition: expr.h:198
runtime::Tensor data
The data of the tensor.
Definition: expr.h:192
TensorType tensor_type() const
Definition: expr.h:207
TVM_DEFINE_OBJECT_REF_COW_METHOD(ConstantNode)
Constant(runtime::Tensor data, ffi::Optional< Type > ty_annotation=std::nullopt, Span span=Span())
The constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Constant, Expr, ConstantNode)
Represent a data type constant.
Definition: expr.h:258
DLDataType value
The data value.
Definition: expr.h:261
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.DataTypeImm", DataTypeImmNode, ExprNode)
static void RegisterReflection()
Definition: expr.h:263
Managed reference to DataTypeImm.
Definition: expr.h:274
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataTypeImm, Expr, DataTypeImmNode)
DataTypeImm(DLDataType value, Span span=Span())
The constructor.
TVM_DEFINE_OBJECT_REF_COW_METHOD(DataTypeImmNode)
Definition: expr.h:411
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.DataflowBlock", DataflowBlockNode, BindingBlockNode)
static void RegisterReflection()
Definition: expr.h:413
Definition: expr.h:421
DataflowBlock(ffi::Array< Binding > bindings, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(DataflowBlockNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataflowBlock, BindingBlock, DataflowBlockNode)
A sub-type of the variable node used to mark dataflow variables from normal visible "function local" ...
Definition: expr.h:165
static void RegisterReflection()
Definition: expr.h:167
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.DataflowVar", DataflowVarNode, VarNode)
Definition: expr.h:176
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataflowVar, Var, DataflowVarNode)
DataflowVar(ffi::String name_hint, ffi::Optional< Type > ty_annotation, Span span=Span())
The extern function, which can represent packed function.
Definition: expr.h:636
static void RegisterReflection()
Definition: expr.h:641
ffi::String global_symbol
The name of global symbol.
Definition: expr.h:639
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.ExternFunc", ExternFuncNode, BaseFuncNode)
Definition: expr.h:648
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ExternFunc, BaseFunc, ExternFuncNode)
ExternFunc(ffi::String global_symbol, Type ty, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(ExternFuncNode)
ExternFunc(ffi::String global_symbol, Span span=Span())
A Relax function.
Definition: expr.h:543
static void RegisterReflection()
Definition: expr.h:554
SeqExpr body
The body of the function.
Definition: expr.h:548
ffi::Array< Var > params
The parameters to the function.
Definition: expr.h:546
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.Function", FunctionNode, BaseFuncNode)
bool is_pure
Whether the function is annotated as pure or not.
Definition: expr.h:552
Definition: expr.h:567
static Function CreateEmpty(ffi::Array< Var > params, Type ret_ty, bool is_pure=true, DictAttrs attrs=DictAttrs(), Span span=Span())
Mimics the constructor but without body Expr.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Function, BaseFunc, FunctionNode)
Function(ffi::Array< Var > params, Expr body, ffi::Optional< Type > ret_ty, bool is_pure=true, DictAttrs attrs=DictAttrs(), Span span=Span())
Construct a Relax Function.
TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionNode)
Condition expression.
Definition: expr.h:496
static void RegisterReflection()
Definition: expr.h:505
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.If", IfNode, ExprNode)
SeqExpr true_branch
The expression evaluated when condition is true.
Definition: expr.h:501
Expr cond
The condition.
Definition: expr.h:499
SeqExpr false_branch
The expression evaluated when condition is false.
Definition: expr.h:503
Definition: expr.h:517
TVM_DEFINE_OBJECT_REF_COW_METHOD(IfNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(If, Expr, IfNode)
If(Expr cond, Expr true_branch, Expr false_branch, Span span=Span())
The constructor.
Runtime-match the value to the type.
Definition: expr.h:330
Expr value
The input value to match cast.
Definition: expr.h:333
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.MatchCast", MatchCastNode, BindingNode)
static void RegisterReflection()
Definition: expr.h:337
Managed reference to MatchCastNode.
Definition: expr.h:351
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(MatchCast, Binding, MatchCastNode)
MatchCast(Var var, Expr value, Type ty, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(MatchCastNode)
A sequence of blocks followed by an expression.
Definition: expr.h:432
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.SeqExpr", SeqExprNode, ExprNode)
Expr body
Definition: expr.h:435
bool SEqual(const SeqExprNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition: expr.h:447
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition: expr.h:455
static void RegisterReflection()
Definition: expr.h:437
ffi::Array< BindingBlock > blocks
Definition: expr.h:434
Definition: expr.h:465
SeqExpr(ffi::Array< BindingBlock > blocks, Expr body, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SeqExpr, Expr, SeqExprNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(SeqExprNode)
A shape expression which allows users to construct a shape containing PrimExpr.
Definition: expr.h:116
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.ShapeExpr", ShapeExprNode, ExprNode)
ffi::Array< PrimExpr > values
Definition: expr.h:119
static void RegisterReflection()
Definition: expr.h:121
Definition: expr.h:128
ShapeExpr(ffi::Array< PrimExpr > values, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(ShapeExprNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ShapeExpr, Expr, ShapeExprNode)
Represent a string literal constant.
Definition: expr.h:226
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.StringImm", StringImmNode, ExprNode)
ffi::String value
The data value.
Definition: expr.h:229
static void RegisterReflection()
Definition: expr.h:231
Managed reference to StringImm.
Definition: expr.h:242
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(StringImm, Expr, StringImmNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(StringImmNode)
StringImm(ffi::String value, Span span=Span())
The constructor.
Managed reference to TensorTypeNode.
Definition: type.h:220
Get index-th field out of a tuple.
Definition: expr.h:84
static void RegisterReflection()
Definition: expr.h:91
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.TupleGetItem", TupleGetItemNode, ExprNode)
int index
which value to get
Definition: expr.h:89
Expr tuple
The tuple Expression.
Definition: expr.h:87
Definition: expr.h:100
TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleGetItemNode)
TupleGetItem(Expr tuple, int index, Span span=Span())
The constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TupleGetItem, Expr, TupleGetItemNode)
Tuple container.
Definition: expr.h:40
static void RegisterReflection()
Definition: expr.h:45
tvm::ffi::Array< Expr > fields
the fields of the tuple
Definition: expr.h:43
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.Tuple", TupleNode, ExprNode)
Definition: expr.h:52
Tuple(tvm::ffi::Array< Expr > fields, Span span=Span())
The constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Tuple, Expr, TupleNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(TupleNode)
Tuple(tvm::ffi::Array< ExprType > fields, Span span=Span())
Utility constructor to handle conversion to relax::Expr.
Definition: expr.h:76
Definition: expr.h:359
static void RegisterReflection()
Definition: expr.h:364
Expr value
The binding value.
Definition: expr.h:362
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
bool SEqual(const VarBindingNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.VarBinding", VarBindingNode, BindingNode)
Definition: expr.h:379
TVM_DEFINE_OBJECT_REF_COW_METHOD(VarBindingNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(VarBinding, Binding, VarBindingNode)
VarBinding(Var var, Expr value, Span span=Span())
The variable class for all Relax bindings.
Definition: expr.h:136
ffi::String name_hint
The hint to the variable name.
Definition: expr.h:142
TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.Var", VarNode, ExprNode)
static void RegisterReflection()
Definition: expr.h:144
Definition: expr.h:155
Var(ffi::String name_hint, ffi::Optional< Type > ty_annotation, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Var, Expr, VarNode)
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:49
Copy-on-write helper macro for IR ffi::ObjectRef types.
Base expr nodes in TVM.
Function nodes.
constexpr const char * kForcePure
Override checking purity for this function and treat as pure (is_pure must be set to true)
Definition: expr.h:625
constexpr const char * kWorkspaceSize
The required workspace for an external function.
Definition: expr.h:619
constexpr const char * kNumInput
The number of inputs of a function. If a function has the num_input attribute, the last func->params....
Definition: expr.h:632
constexpr const char * kComposite
Treat the function as a composite operator.
Definition: expr.h:615
constexpr const char * kCodegen
Indicate the codegen that should be used for building this function. When this is unset or set to "de...
Definition: expr.h:613
constexpr const char * kPrimitive
Mark the function as a primitive function.
Definition: expr.h:608
constexpr const char * kPartitionedFromPattern
Indicate the function was created by the Pattern Partitioning Pass.
Definition: expr.h:617
Expr GetShapeOf(const Expr &expr)
Get the shape of Expr.
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Relax types, including the richer dependent Relax type nodes.
A device-independent managed Tensor abstraction.
A map from source names to source code.
TIR expressions.
Common operators defined for Expr.