tvm
struct_info.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_STRUCT_INFO_H_
20 #define TVM_RELAX_STRUCT_INFO_H_
21 
22 #include <tvm/ffi/reflection/registry.h>
23 #include <tvm/ir/env_func.h>
24 #include <tvm/ir/source_map.h>
25 #include <tvm/node/node.h>
27 #include <tvm/relax/expr.h>
28 #include <tvm/relax/type.h>
29 
30 #include <utility>
31 
32 namespace tvm {
33 namespace relax {
34 
39  public:
40  static void RegisterReflection() {
41  namespace refl = tvm::ffi::reflection;
42  refl::ObjectDef<ObjectStructInfoNode>();
43  }
45 };
46 
51 class ObjectStructInfo : public StructInfo {
52  public:
53  TVM_DLL ObjectStructInfo(Span span = Span());
54 
56 };
57 
62  public:
64  ffi::Optional<PrimExpr> value;
65 
68 
69  static void RegisterReflection() {
70  namespace refl = tvm::ffi::reflection;
71  refl::ObjectDef<PrimStructInfoNode>()
72  .def_ro("value", &PrimStructInfoNode::value)
73  .def_ro("dtype", &PrimStructInfoNode::dtype);
74  }
76 };
77 
82 class PrimStructInfo : public StructInfo {
83  public:
84  /* Construct a PrimStructInfo with a known dtype, but unknown value */
85  TVM_DLL PrimStructInfo(DataType dtype, Span span = Span());
86 
87  /* Construct a PrimStructInfo with a known value */
88  TVM_DLL PrimStructInfo(PrimExpr value, Span span = Span());
89 
91 };
92 
97  public:
99  ffi::Optional<ffi::Array<PrimExpr>> values;
104  int ndim;
105 
107  bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
108 
109  static void RegisterReflection() {
110  namespace refl = tvm::ffi::reflection;
111  refl::ObjectDef<ShapeStructInfoNode>()
112  .def_ro("values", &ShapeStructInfoNode::values)
113  .def_ro("ndim", &ShapeStructInfoNode::ndim);
114  }
116 };
117 
122 class ShapeStructInfo : public StructInfo {
123  public:
129  TVM_DLL ShapeStructInfo(ffi::Array<PrimExpr> values, Span span = Span());
135  TVM_DLL ShapeStructInfo(int ndim, Span span = Span());
136 
138 };
139 
144  public:
149  ffi::Optional<Expr> shape;
153  ffi::Optional<VDevice> vdevice;
160  int ndim;
161 
163  bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
164 
166  bool IsUnknownDtype() const { return dtype.is_void(); }
167 
169  ffi::Optional<ffi::Array<PrimExpr>> GetShape() const {
170  if (!shape.defined()) return {};
171  ShapeStructInfo shape_sinfo = Downcast<ShapeStructInfo>(this->shape.value()->struct_info_);
172  return shape_sinfo->values;
173  }
174 
175  static void RegisterReflection() {
176  namespace refl = tvm::ffi::reflection;
177  refl::ObjectDef<TensorStructInfoNode>()
178  .def_ro("shape", &TensorStructInfoNode::shape)
179  .def_ro("dtype", &TensorStructInfoNode::dtype)
180  .def_ro("vdevice", &TensorStructInfoNode::vdevice)
181  .def_ro("ndim", &TensorStructInfoNode::ndim);
182  }
184 };
185 
190 class TensorStructInfo : public StructInfo {
191  public:
202  ffi::Optional<VDevice> vdevice = std::nullopt, Span span = Span());
203 
211  TVM_DLL TensorStructInfo(DataType dtype, int ndim, ffi::Optional<VDevice> vdevice = std::nullopt,
212  Span span = Span());
213 
215 };
216 
221  public:
223  ffi::Array<StructInfo> fields;
224 
225  static void RegisterReflection() {
226  namespace refl = tvm::ffi::reflection;
227  refl::ObjectDef<TupleStructInfoNode>().def_ro("fields", &TupleStructInfoNode::fields);
228  }
230 };
231 
236 class TupleStructInfo : public StructInfo {
237  public:
243  TVM_DLL TupleStructInfo(ffi::Array<StructInfo> fields, Span span = Span());
244 
246 };
247 
255 
263  public:
269  ffi::Optional<ffi::Array<StructInfo>> params;
279  ffi::Optional<StructInfoDeriveFunc> derive_func;
285  bool purity;
286 
291  bool IsOpaque() const { return !params.defined(); }
292 
293  static void RegisterReflection() {
294  namespace refl = tvm::ffi::reflection;
295  refl::ObjectDef<FuncStructInfoNode>()
296  .def_ro("params", &FuncStructInfoNode::params, refl::AttachFieldFlag::SEqHashDef())
297  .def_ro("ret", &FuncStructInfoNode::ret)
298  .def_ro("derive_func", &FuncStructInfoNode::derive_func)
299  .def_ro("purity", &FuncStructInfoNode::purity);
300  }
302 };
303 
308 class FuncStructInfo : public StructInfo {
309  public:
310  explicit FuncStructInfo(ObjectPtr<FuncStructInfoNode> data) : StructInfo(ffi::UnsafeInit{}) {
311  TVM_FFI_ICHECK(data != nullptr);
312  data_ = std::move(data);
313  }
324  TVM_DLL FuncStructInfo(ffi::Array<StructInfo> params, StructInfo ret, bool purity = true,
325  Span span = Span());
326 
338  TVM_DLL static FuncStructInfo OpaqueFunc(StructInfoDeriveFunc derive_func, bool purity = false,
339  Span span = Span());
340 
352  TVM_DLL static FuncStructInfo OpaqueFunc(StructInfo ret = ObjectStructInfo(), bool purity = false,
353  Span span = Span());
354 
356 };
357 
365 template <typename T>
366 inline ffi::Optional<T> MatchStructInfo(const Expr& expr) {
367  using TNode = typename T::ContainerType;
368  if (const TNode* ptr = expr->struct_info_.as<TNode>()) {
369  return ffi::GetRef<T>(ptr);
370  } else {
371  return std::nullopt;
372  }
373 }
374 
382 template <typename T>
383 inline const T* GetStructInfoAs(const Expr& expr) {
384  ICHECK(expr->struct_info_.defined())
385  << "The struct_info is not populated, check if you have normalized the expr";
386  return expr->struct_info_.as<T>();
387 }
388 
395 inline StructInfo GetStructInfo(const Expr& expr) {
396  auto* ptr = expr->struct_info_.as<StructInfoNode>();
397  ICHECK(ptr) << "The struct_info is not populated, check if you have normalized the expr";
398  return ffi::GetRef<StructInfo>(ptr);
399 }
400 
407 inline bool HasVoidStructInfo(const Expr& expr) {
408  auto* ptr = expr->struct_info_.as<TupleStructInfoNode>();
409  return ptr != nullptr && ptr->fields.size() == 0;
410 }
411 
419 TVM_DLL void UpdateStructInfo(Expr expr, StructInfo struct_info);
420 
421 } // namespace relax
422 } // namespace tvm
423 #endif // TVM_RELAX_STRUCT_INFO_H_
The utility for constructing Relax binding blocks.
Reference to PrimExprNode.
Definition: expr.h:124
Managed reference to RelaxExprNode.
Definition: expr.h:439
Definition: source_map.h:111
Please refer to TypedEnvFunc<R(Args..)>.
Definition: env_func.h:104
Definition: block_builder.h:264
Definition: expr.h:176
Structure information about function.
Definition: struct_info.h:262
bool IsOpaque() const
Definition: struct_info.h:291
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.FuncStructInfo", FuncStructInfoNode, StructInfoNode)
ffi::Optional< ffi::Array< StructInfo > > params
The parameter struct info of the function.
Definition: struct_info.h:269
StructInfo ret
The struct info of the function's return value.
Definition: struct_info.h:273
bool purity
Whether the function is pure.
Definition: struct_info.h:285
static void RegisterReflection()
Definition: struct_info.h:293
ffi::Optional< StructInfoDeriveFunc > derive_func
Derivation function of opaque functions that may take any number of parameters.
Definition: struct_info.h:279
Managed reference to FuncStructInfoNode.
Definition: struct_info.h:308
FuncStructInfo(ObjectPtr< FuncStructInfoNode > data)
Definition: struct_info.h:310
FuncStructInfo(ffi::Array< StructInfo > params, StructInfo ret, bool purity=true, Span span=Span())
Constructor from parameter struct info and return value struct info.
static FuncStructInfo OpaqueFunc(StructInfoDeriveFunc derive_func, bool purity=false, Span span=Span())
Constructing an opaque function struct info using derive_func.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FuncStructInfo, StructInfo, FuncStructInfoNode)
static FuncStructInfo OpaqueFunc(StructInfo ret=ObjectStructInfo(), bool purity=false, Span span=Span())
Construct an opaque function using from return struct info.
Opaque object.
Definition: struct_info.h:38
static void RegisterReflection()
Definition: struct_info.h:40
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.ObjectStructInfo", ObjectStructInfoNode, StructInfoNode)
Managed reference to ObjectStructInfoNode.
Definition: struct_info.h:51
ObjectStructInfo(Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ObjectStructInfo, StructInfo, ObjectStructInfoNode)
Primitive value.
Definition: struct_info.h:61
ffi::Optional< PrimExpr > value
Underlying primitive value, if known.
Definition: struct_info.h:64
DataType dtype
Underlying data type of the primitive value.
Definition: struct_info.h:67
static void RegisterReflection()
Definition: struct_info.h:69
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.PrimStructInfo", PrimStructInfoNode, StructInfoNode)
Managed reference to PrimStructInfoNode.
Definition: struct_info.h:82
PrimStructInfo(DataType dtype, Span span=Span())
PrimStructInfo(PrimExpr value, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrimStructInfo, StructInfo, PrimStructInfoNode)
StructInfo of shape value.
Definition: struct_info.h:96
static void RegisterReflection()
Definition: struct_info.h:109
bool IsUnknownNdim() const
Definition: struct_info.h:107
int ndim
The number of dimension of the shape, can be unknown.
Definition: struct_info.h:104
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.ShapeStructInfo", ShapeStructInfoNode, StructInfoNode)
ffi::Optional< ffi::Array< PrimExpr > > values
optionally stores the symbolic value patterns of the shape
Definition: struct_info.h:99
Managed reference to ShapeStructInfoNode.
Definition: struct_info.h:122
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ShapeStructInfo, StructInfo, ShapeStructInfoNode)
ShapeStructInfo(int ndim, Span span=Span())
Construction with known unknown symbolic shape patterns.
ShapeStructInfo(ffi::Array< PrimExpr > values, Span span=Span())
Construction with known symbolic shape patterns.
Base type of all structure information.
Definition: expr.h:108
Managed reference to StructInfoNode.
Definition: expr.h:132
StructInfo of Tensor.
Definition: struct_info.h:143
ffi::Optional< ffi::Array< PrimExpr > > GetShape() const
Definition: struct_info.h:169
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.TensorStructInfo", TensorStructInfoNode, StructInfoNode)
DataType dtype
The content data type, use void to denote the dtype is unknown.
Definition: struct_info.h:155
ffi::Optional< VDevice > vdevice
The virtual device, indicates where the tensor is expected to be executed.
Definition: struct_info.h:153
ffi::Optional< Expr > shape
optionally store the shape expression of the tensor.
Definition: struct_info.h:149
static void RegisterReflection()
Definition: struct_info.h:175
int ndim
The number of dimension of the tensor, can be unknown.
Definition: struct_info.h:160
bool IsUnknownDtype() const
Definition: struct_info.h:166
bool IsUnknownNdim() const
Definition: struct_info.h:163
Managed reference to TensorStructInfoNode.
Definition: struct_info.h:190
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TensorStructInfo, StructInfo, TensorStructInfoNode)
TensorStructInfo(Expr shape, DataType dtype, ffi::Optional< VDevice > vdevice=std::nullopt, Span span=Span())
Construction with a known shape expression.
TensorStructInfo(DataType dtype, int ndim, ffi::Optional< VDevice > vdevice=std::nullopt, Span span=Span())
Construction with an unknown shape expression.
StructInfo of Tuple.
Definition: struct_info.h:220
ffi::Array< StructInfo > fields
The struct info of tuple fields.
Definition: struct_info.h:223
static void RegisterReflection()
Definition: struct_info.h:225
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.TupleStructInfo", TupleStructInfoNode, StructInfoNode)
Managed reference to TupleStructInfoNode.
Definition: struct_info.h:236
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TupleStructInfo, StructInfo, TupleStructInfoNode)
TupleStructInfo(ffi::Array< StructInfo > fields, Span span=Span())
Constructor.
Runtime primitive data type.
Definition: data_type.h:47
bool is_void() const
Definition: data_type.h:209
Serializable global function used in IR.
Definition: repr_printer.h:91
const T * GetStructInfoAs(const Expr &expr)
Get the structure info of a given expr and try to cast it as const T*.
Definition: struct_info.h:383
void UpdateStructInfo(Expr expr, StructInfo struct_info)
Update the struct info of an Expr.
StructInfo GetStructInfo(const Expr &expr)
Get the underlying structure info of expr.
Definition: struct_info.h:395
bool HasVoidStructInfo(const Expr &expr)
Whether the expr has void struct info.
Definition: struct_info.h:407
ffi::Optional< T > MatchStructInfo(const Expr &expr)
Match and check if expr have StructInfo T and return it.
Definition: struct_info.h:366
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1960
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Definitions and helper macros for IR/AST nodes.
Relax Types.
A map from source names to source code.