19 #ifndef TVM_RELAX_EXPR_H_
20 #define TVM_RELAX_EXPR_H_
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/container/map.h>
24 #include <tvm/ffi/reflection/registry.h>
46 namespace refl = tvm::ffi::reflection;
59 TVM_DLL
explicit Tuple(tvm::ffi::Array<Expr> fields,
Span span =
Span());
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) {}
92 namespace refl = tvm::ffi::reflection;
93 refl::ObjectDef<TupleGetItemNode>()
94 .def_ro(
"tuple_value", &TupleGetItemNode::tuple)
95 .def_ro(
"index", &TupleGetItemNode::index);
122 namespace refl = tvm::ffi::reflection;
123 refl::ObjectDef<ShapeExprNode>().def_ro(
"values", &ShapeExprNode::values);
145 namespace refl = tvm::ffi::reflection;
146 refl::ObjectDef<VarNode>().def_ro(
"name_hint", &VarNode::name_hint,
147 refl::AttachFieldFlag::SEqHashIgnore());
150 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
151 static constexpr
const uint32_t _type_child_slots = 1;
157 TVM_DLL
explicit Var(ffi::String name_hint, ffi::Optional<Type> ty_annotation,
168 namespace refl = tvm::ffi::reflection;
169 refl::ObjectDef<DataflowVarNode>();
172 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
178 TVM_DLL
explicit DataflowVar(ffi::String name_hint, ffi::Optional<Type> ty_annotation,
201 namespace refl = tvm::ffi::reflection;
202 refl::ObjectDef<ConstantNode>().def_ro(
"data", &ConstantNode::data);
232 namespace refl = tvm::ffi::reflection;
233 refl::ObjectDef<StringImmNode>().def_ro(
"value", &StringImmNode::value);
264 namespace refl = tvm::ffi::reflection;
265 refl::ObjectDef<DataTypeImmNode>().def_ro(
"value", &DataTypeImmNode::value);
295 namespace refl = tvm::ffi::reflection;
296 refl::ObjectDef<BindingNode>()
297 .def_ro(
"span", &BindingNode::span, refl::AttachFieldFlag::SEqHashIgnore())
299 .def_ro(
"var", &
BindingNode::var, refl::AttachFieldFlag::SEqHashDefRecursive());
302 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
312 explicit Binding(ffi::ObjectPtr<BindingNode> n) : ffi::ObjectRef(n) {}
313 explicit Binding(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
338 namespace refl = tvm::ffi::reflection;
339 refl::ObjectDef<MatchCastNode>()
340 .def_ro(
"value", &MatchCastNode::value)
342 .def_ro(
"ty", &MatchCastNode::ty, refl::AttachFieldFlag::SEqHashDefRecursive());
365 namespace refl = tvm::ffi::reflection;
366 refl::ObjectDef<VarBindingNode>().def_ro(
"value", &VarBindingNode::value);
368 refl::TypeAttrDef<VarBindingNode>()
369 .def(
"__s_equal__", &VarBindingNode::SEqual)
370 .def(
"__s_hash__", &VarBindingNode::SHash);
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;
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()));
399 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
414 namespace refl = tvm::ffi::reflection;
415 refl::ObjectDef<DataflowBlockNode>();
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);
448 ffi::TypedFunction<
bool(AnyView, AnyView,
bool, AnyView)>
equal)
const {
451 return equal(blocks, other->
blocks,
false,
"blocks") &&
equal(ty, other->
ty,
false,
"ty") &&
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);
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);
513 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
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);
563 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
590 TVM_DLL
explicit Function(ffi::Array<Var> params,
Expr body, ffi::Optional<Type> ret_ty,
642 namespace refl = tvm::ffi::reflection;
643 refl::ObjectDef<ExternFuncNode>().def_ro(
"global_symbol", &ExternFuncNode::global_symbol);
688 struct std::hash<
tvm::relax::Var> {
690 return tvm::ffi::ObjectPtrHash()(
var);
695 struct std::equal_to<
tvm::relax::Var> {
697 return tvm::ffi::ObjectPtrEqual()(var_a, var_b);
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.
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)
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)
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
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)
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.DataflowBlock", DataflowBlockNode, BindingBlockNode)
static void RegisterReflection()
Definition: expr.h:413
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)
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)
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
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
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
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
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
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)
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
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)
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
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.
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.
Common operators defined for Expr.