tvm
type.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 
24 #ifndef TVM_RELAX_TYPE_H_
25 #define TVM_RELAX_TYPE_H_
26 
27 #include <tvm/ffi/function.h>
28 #include <tvm/ffi/reflection/registry.h>
29 #include <tvm/ir/attrs.h>
30 #include <tvm/ir/env_func.h>
31 #include <tvm/ir/global_info.h>
32 #include <tvm/ir/type.h>
33 #include <tvm/tirx/expr.h>
34 
35 #include <string>
36 #include <utility>
37 
38 namespace tvm {
39 namespace relax {
40 
41 using Expr = tvm::Expr;
43 
44 class BlockBuilder;
45 
47 static constexpr int kUnknownNDim = -1;
48 
49 using tvm::TupleType;
50 using tvm::TupleTypeNode;
51 
52 class PackedFuncTypeNode : public TypeNode {
53  public:
54  static void RegisterReflection() {
55  namespace refl = tvm::ffi::reflection;
56  refl::ObjectDef<PackedFuncTypeNode>();
57  }
59 };
60 
61 class PackedFuncType : public Type {
62  public:
63  TVM_DLL PackedFuncType(Span span = Span());
64 
66 };
67 
95 class AnyTypeNode : public TypeNode {
96  public:
97  static void RegisterReflection() {
98  namespace refl = tvm::ffi::reflection;
99  refl::ObjectDef<AnyTypeNode>();
100  }
102 };
103 
108 class AnyType : public Type {
109  public:
110  TVM_DLL AnyType(Span span = Span());
111 
113 };
114 
115 // Compatibility aliases for existing C++ callers. New code should use AnyType.
118 
122 class ShapeTypeNode : public TypeNode {
123  public:
125  ffi::Optional<ffi::Array<PrimExpr>> values;
130  int ndim{kUnknownNDim};
131 
133  bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
134 
135  static void RegisterReflection() {
136  namespace refl = tvm::ffi::reflection;
137  refl::ObjectDef<ShapeTypeNode>()
138  .def_ro("values", &ShapeTypeNode::values)
139  .def_ro("ndim", &ShapeTypeNode::ndim);
140  }
142 };
143 
148 class ShapeType : public Type {
149  public:
155  TVM_DLL ShapeType(ffi::Array<PrimExpr> values, Span span = Span());
161  TVM_DLL ShapeType(int ndim, Span span = Span());
162 
164 };
165 
169 class TensorTypeNode : public TypeNode {
170  public:
175  ffi::Optional<Expr> shape;
179  ffi::Optional<VDevice> vdevice;
181  ffi::Optional<tvm::PrimType> dtype{std::nullopt};
186  int ndim{kUnknownNDim};
187 
189  bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
190 
192  bool IsUnknownDtype() const { return !dtype.has_value(); }
193 
195  ffi::Optional<ffi::Array<PrimExpr>> GetShape() const {
196  if (!shape.has_value()) return {};
197  const Expr& shape_expr = this->shape.value();
198  if (shape_expr->ty.IsMissing()) return {};
199  if (const auto* shape_ty = shape_expr->ty.as<ShapeTypeNode>()) {
200  return shape_ty->values;
201  }
202  return {};
203  }
204 
205  static void RegisterReflection() {
206  namespace refl = tvm::ffi::reflection;
207  refl::ObjectDef<TensorTypeNode>()
208  .def_ro("shape", &TensorTypeNode::shape)
209  .def_ro("dtype", &TensorTypeNode::dtype)
210  .def_ro("vdevice", &TensorTypeNode::vdevice)
211  .def_ro("ndim", &TensorTypeNode::ndim);
212  }
214 };
215 
220 class TensorType : public Type {
221  public:
222  explicit TensorType(ffi::ObjectPtr<TensorTypeNode> data) : Type(ffi::UnsafeInit{}) {
223  TVM_FFI_ICHECK(data != nullptr);
224  data_ = std::move(data);
225  }
226 
236  TVM_DLL TensorType(Expr shape, ffi::Optional<tvm::PrimType> dtype = std::nullopt,
237  ffi::Optional<VDevice> vdevice = std::nullopt, Span span = Span());
238 
246  TVM_DLL TensorType(ffi::Optional<tvm::PrimType> dtype, int ndim,
247  ffi::Optional<VDevice> vdevice = std::nullopt, Span span = Span());
248 
250 };
251 
258 using TypeDeriveFunc = TypedEnvFunc<Type(const Call& call, const BlockBuilder& ctx)>;
259 
266 class FuncTypeNode : public TypeNode {
267  public:
273  ffi::Optional<ffi::Array<Type>> params;
283  ffi::Optional<TypeDeriveFunc> derive_func;
289  bool purity;
290 
295  bool IsOpaque() const { return !params.has_value(); }
296 
297  static void RegisterReflection() {
298  namespace refl = tvm::ffi::reflection;
299  refl::ObjectDef<FuncTypeNode>()
300  .def_ro("params", &FuncTypeNode::params, refl::AttachFieldFlag::SEqHashDefRecursive())
301  .def_ro("ret", &FuncTypeNode::ret)
302  .def_ro("derive_func", &FuncTypeNode::derive_func)
303  .def_ro("purity", &FuncTypeNode::purity);
304  }
306 };
307 
312 class FuncType : public Type {
313  public:
314  explicit FuncType(ffi::ObjectPtr<FuncTypeNode> data) : Type(ffi::UnsafeInit{}) {
315  TVM_FFI_ICHECK(data != nullptr);
316  data_ = std::move(data);
317  }
328  TVM_DLL FuncType(ffi::Array<Type> params, Type ret, bool purity = true, Span span = Span());
329 
341  TVM_DLL static FuncType OpaqueFunc(TypeDeriveFunc derive_func, bool purity = false,
342  Span span = Span());
343 
355  TVM_DLL static FuncType OpaqueFunc(Type ret = AnyType(), bool purity = false, Span span = Span());
356 
358 };
359 
367 template <typename T>
368 inline ffi::Optional<T> MatchType(const Expr& expr) {
369  if (!expr.defined()) {
370  return std::nullopt;
371  }
372  using TNode = typename T::ContainerType;
373  if (const TNode* ptr = expr->ty.as<TNode>()) {
374  return ffi::GetRef<T>(ptr);
375  } else {
376  return std::nullopt;
377  }
378 }
379 
387 template <typename T>
388 inline const T* GetTypeAs(const Expr& expr) {
389  TVM_FFI_ICHECK(!expr->ty.IsMissing())
390  << "The type is not populated, check if you have normalized the expr";
391  return expr->ty.as<T>();
392 }
393 
400 inline Type GetType(const Expr& expr) {
401  TVM_FFI_ICHECK(!expr->ty.IsMissing())
402  << "The type is not populated, check if you have normalized the expr";
403  return expr->ty;
404 }
405 
412 inline bool HasVoidType(const Expr& expr) {
413  auto* ptr = expr->ty.as<TupleTypeNode>();
414  return ptr != nullptr && ptr->fields.size() == 0;
415 }
416 
424 TVM_DLL void UpdateType(Expr expr, Type ty);
425 
426 } // namespace relax
427 } // namespace tvm
428 
429 #endif // TVM_RELAX_TYPE_H_
Helpers for attribute objects.
Managed reference to CallNode.
Definition: expr.h:348
Base type of all the expressions.
Definition: base_expr.h:276
Managed reference to ExprNode.
Definition: base_expr.h:311
Definition: source_map.h:111
The type of tuple values.
Definition: type.h:90
ffi::Array< Type > fields
The type of each field in the tuple.
Definition: type.h:93
Managed reference to TupleTypeNode.
Definition: type.h:108
Type is the base type of all types.
Definition: base_expr.h:52
Managed reference to TypeNode.
Definition: base_expr.h:77
static Type Missing()
Sentinel for a type that has not been populated yet.
Please refer to TypedEnvFunc<R(Args..)>.
Definition: env_func.h:105
Base type of all Relax type information.
Definition: type.h:95
static void RegisterReflection()
Definition: type.h:97
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.AnyType", AnyTypeNode, TypeNode)
Managed reference to AnyTypeNode.
Definition: type.h:108
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AnyType, Type, AnyTypeNode)
AnyType(Span span=Span())
Definition: block_builder.h:257
Function type information.
Definition: type.h:266
Type ret
The type of the function's return value.
Definition: type.h:277
bool IsOpaque() const
Definition: type.h:295
bool purity
Whether the function is pure.
Definition: type.h:289
ffi::Optional< ffi::Array< Type > > params
The parameter type of the function.
Definition: type.h:273
static void RegisterReflection()
Definition: type.h:297
ffi::Optional< TypeDeriveFunc > derive_func
Derivation function of opaque functions that may take any number of parameters.
Definition: type.h:283
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.FuncType", FuncTypeNode, TypeNode)
Managed reference to FuncTypeNode.
Definition: type.h:312
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FuncType, Type, FuncTypeNode)
static FuncType OpaqueFunc(Type ret=AnyType(), bool purity=false, Span span=Span())
Construct an opaque function using from return type.
static FuncType OpaqueFunc(TypeDeriveFunc derive_func, bool purity=false, Span span=Span())
Constructing an opaque function type using derive_func.
FuncType(ffi::ObjectPtr< FuncTypeNode > data)
Definition: type.h:314
FuncType(ffi::Array< Type > params, Type ret, bool purity=true, Span span=Span())
Constructor from parameter type and return value type.
Definition: type.h:52
static void RegisterReflection()
Definition: type.h:54
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.PackedFuncType", PackedFuncTypeNode, TypeNode)
Definition: type.h:61
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PackedFuncType, Type, PackedFuncTypeNode)
PackedFuncType(Span span=Span())
Type of shape value.
Definition: type.h:122
int ndim
The number of dimension of the shape, can be unknown.
Definition: type.h:130
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.ShapeType", ShapeTypeNode, TypeNode)
ffi::Optional< ffi::Array< PrimExpr > > values
optionally stores the symbolic value patterns of the shape
Definition: type.h:125
static void RegisterReflection()
Definition: type.h:135
bool IsUnknownNdim() const
Definition: type.h:133
Managed reference to ShapeTypeNode.
Definition: type.h:148
ShapeType(int ndim, Span span=Span())
Construction with known unknown symbolic shape patterns.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ShapeType, Type, ShapeTypeNode)
ShapeType(ffi::Array< PrimExpr > values, Span span=Span())
Construction with known symbolic shape patterns.
Type of Tensor.
Definition: type.h:169
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.TensorType", TensorTypeNode, TypeNode)
bool IsUnknownDtype() const
Definition: type.h:192
ffi::Optional< VDevice > vdevice
The virtual device, indicates where the tensor is expected to be executed.
Definition: type.h:179
ffi::Optional< ffi::Array< PrimExpr > > GetShape() const
Definition: type.h:195
ffi::Optional< tvm::PrimType > dtype
The content dtype, or nullopt if the dtype is unknown.
Definition: type.h:181
static void RegisterReflection()
Definition: type.h:205
bool IsUnknownNdim() const
Definition: type.h:189
ffi::Optional< Expr > shape
optionally store the shape expression of the tensor.
Definition: type.h:175
int ndim
The number of dimension of the tensor, can be unknown.
Definition: type.h:186
Managed reference to TensorTypeNode.
Definition: type.h:220
TensorType(ffi::Optional< tvm::PrimType > dtype, int ndim, ffi::Optional< VDevice > vdevice=std::nullopt, Span span=Span())
Construction with an unknown shape expression.
TensorType(Expr shape, ffi::Optional< tvm::PrimType > dtype=std::nullopt, ffi::Optional< VDevice > vdevice=std::nullopt, Span span=Span())
Construction with a known shape expression.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TensorType, Type, TensorTypeNode)
TensorType(ffi::ObjectPtr< TensorTypeNode > data)
Definition: type.h:222
Serializable global function used in IR.
GlobalInfo are globally static object that are referred by the IR itself.
IR/AST nodes for TVM types shared across IR variants.
bool HasVoidType(const Expr &expr)
Whether the expr has void type.
Definition: type.h:412
const T * GetTypeAs(const Expr &expr)
Get the type of a given expr and try to cast it as const T*.
Definition: type.h:388
void UpdateType(Expr expr, Type ty)
Update the type of an Expr.
tvm::ExprNode ExprNode
Definition: type.h:42
Type GetType(const Expr &expr)
Get the underlying Relax type of expr.
Definition: type.h:400
tvm::Expr Expr
Definition: type.h:41
ffi::Optional< T > MatchType(const Expr &expr)
Match and check if expr has Relax type T and return it.
Definition: type.h:368
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
TIR expressions.