tvm
Loading...
Searching...
No Matches
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/prim/expr.h>
32#include <tvm/ir/type.h>
34
35#include <string>
36#include <utility>
37
38namespace tvm {
39namespace relax {
40
43
44class BlockBuilder;
45
47static constexpr int kUnknownNDim = -1;
48
49using tvm::AnyType;
51using tvm::TupleType;
53
54// Compatibility aliases for existing C++ callers. New code should use AnyType.
57
59 public:
60 static void RegisterReflection() {
61 namespace refl = tvm::ffi::reflection;
62 refl::ObjectDef<PackedFuncTypeNode>();
63 }
65};
66
73
102class ShapeTypeNode : public TypeNode {
103 public:
105 ffi::Optional<ffi::Array<PrimExpr>> values;
110 int ndim{kUnknownNDim};
111
113 bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
114
115 static void RegisterReflection() {
116 namespace refl = tvm::ffi::reflection;
117 refl::ObjectDef<ShapeTypeNode>()
118 .def_ro("values", &ShapeTypeNode::values)
119 .def_ro("ndim", &ShapeTypeNode::ndim);
120 }
122};
123
128class ShapeType : public Type {
129 public:
135 TVM_DLL ShapeType(ffi::Array<PrimExpr> values, Location loc = UnknownLoc());
141 TVM_DLL ShapeType(int ndim, Location loc = UnknownLoc());
142
144};
145
149class TensorTypeNode : public TypeNode {
150 public:
155 ffi::Optional<Expr> shape;
159 ffi::Optional<VDevice> vdevice;
161 ffi::Optional<tvm::PrimType> dtype{std::nullopt};
166 int ndim{kUnknownNDim};
167
169 bool IsUnknownNdim() const { return ndim == kUnknownNDim; }
170
172 bool IsUnknownDtype() const { return !dtype.has_value(); }
173
175 ffi::Optional<ffi::Array<PrimExpr>> GetShape() const {
176 if (!shape.has_value()) return {};
177 const Expr& shape_expr = this->shape.value();
178 if (shape_expr->ty.as<MissingType>().has_value()) return {};
179 if (const auto* shape_ty = shape_expr->ty.as<ShapeTypeNode>()) {
180 return shape_ty->values;
181 }
182 return {};
183 }
184
185 static void RegisterReflection() {
186 namespace refl = tvm::ffi::reflection;
187 refl::ObjectDef<TensorTypeNode>()
188 .def_ro("shape", &TensorTypeNode::shape)
189 .def_ro("dtype", &TensorTypeNode::dtype)
190 .def_ro("vdevice", &TensorTypeNode::vdevice)
191 .def_ro("ndim", &TensorTypeNode::ndim);
192 }
194};
195
200class TensorType : public Type {
201 public:
202 explicit TensorType(ffi::ObjectPtr<TensorTypeNode> data) : Type(ffi::UnsafeInit{}) {
203 TVM_FFI_ICHECK(data != nullptr);
204 data_ = std::move(data);
205 }
206
216 TVM_DLL TensorType(Expr shape, ffi::Optional<tvm::PrimType> dtype = std::nullopt,
217 ffi::Optional<VDevice> vdevice = std::nullopt, Location loc = UnknownLoc());
218
226 TVM_DLL TensorType(ffi::Optional<tvm::PrimType> dtype, int ndim,
227 ffi::Optional<VDevice> vdevice = std::nullopt, Location loc = UnknownLoc());
228
230};
231
238using TypeDeriveFunc = TypedEnvFunc<Type(const Call& call, const BlockBuilder& ctx)>;
239
246class FuncTypeNode : public TypeNode {
247 public:
253 ffi::Optional<ffi::Array<Type>> params;
263 ffi::Optional<TypeDeriveFunc> derive_func;
269 bool purity;
270
275 bool IsOpaque() const { return !params.has_value(); }
276
277 static void RegisterReflection() {
278 namespace refl = tvm::ffi::reflection;
279 refl::ObjectDef<FuncTypeNode>()
280 .def_ro("params", &FuncTypeNode::params, refl::AttachFieldFlag::SEqHashDefPattern())
281 .def_ro("ret", &FuncTypeNode::ret)
282 .def_ro("derive_func", &FuncTypeNode::derive_func)
283 .def_ro("purity", &FuncTypeNode::purity);
284 }
286};
287
292class FuncType : public Type {
293 public:
294 explicit FuncType(ffi::ObjectPtr<FuncTypeNode> data) : Type(ffi::UnsafeInit{}) {
295 TVM_FFI_ICHECK(data != nullptr);
296 data_ = std::move(data);
297 }
308 TVM_DLL FuncType(ffi::Array<Type> params, Type ret, bool purity = true,
309 Location loc = UnknownLoc());
310
322 TVM_DLL static FuncType OpaqueFunc(TypeDeriveFunc derive_func, bool purity = false,
323 Location loc = UnknownLoc());
324
336 TVM_DLL static FuncType OpaqueFunc(Type ret = AnyType(), bool purity = false,
337 Location loc = UnknownLoc());
338
340};
341
349template <typename T>
350inline ffi::Optional<T> MatchType(const Expr& expr) {
351 if (!expr.defined()) {
352 return std::nullopt;
353 }
354 using TNode = typename T::ContainerType;
355 if (const TNode* ptr = expr->ty.as<TNode>()) {
356 return ffi::GetRef<T>(ptr);
357 } else {
358 return std::nullopt;
359 }
360}
361
369template <typename T>
370inline const T* GetTypeAs(const Expr& expr) {
371 TVM_FFI_ICHECK(!expr->ty.as<MissingType>().has_value())
372 << "The type is not populated, check if you have normalized the expr";
373 return expr->ty.as<T>();
374}
375
382inline Type GetType(const Expr& expr) {
383 TVM_FFI_ICHECK(!expr->ty.as<MissingType>().has_value())
384 << "The type is not populated, check if you have normalized the expr";
385 return expr->ty;
386}
387
394inline bool HasVoidType(const Expr& expr) {
395 auto* ptr = expr->ty.as<TupleTypeNode>();
396 return ptr != nullptr && ptr->fields.size() == 0;
397}
398
406TVM_DLL void UpdateType(Expr expr, Type ty);
407
408} // namespace relax
409} // namespace tvm
410
411#endif // TVM_RELAX_TYPE_H_
Helpers for attribute objects.
The top type, which admits any value.
Definition type.h:43
Managed reference to AnyTypeNode.
Definition type.h:53
Managed reference to CallNode.
Definition expr.h:972
Base type of all the expressions.
Definition base_expr.h:324
Managed reference to ExprNode.
Definition base_expr.h:359
Non-nullable source-location metadata.
Definition location.h:91
Managed reference to the MissingTypeNode singleton.
Definition base_expr.h:105
The type of tuple values.
Definition type.h:146
ffi::Array< Type > fields
The type of each field in the tuple.
Definition type.h:149
Managed reference to TupleTypeNode.
Definition type.h:164
Type is the base type of all types.
Definition base_expr.h:54
Managed reference to TypeNode.
Definition base_expr.h:80
static TVM_TIRX_DLL Type Missing()
Construct a MissingType for type information not yet populated.
Please refer to TypedEnvFunc<R(Args..)>.
Definition env_func.h:107
Reference to the shared immutable unknown location.
Definition location.h:105
Definition block_builder.h:257
Function type information.
Definition type.h:246
Type ret
The type of the function's return value.
Definition type.h:257
bool IsOpaque() const
Definition type.h:275
bool purity
Whether the function is pure.
Definition type.h:269
ffi::Optional< ffi::Array< Type > > params
The parameter type of the function.
Definition type.h:253
static void RegisterReflection()
Definition type.h:277
ffi::Optional< TypeDeriveFunc > derive_func
Derivation function of opaque functions that may take any number of parameters.
Definition type.h:263
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.FuncType", FuncTypeNode, TypeNode)
Managed reference to FuncTypeNode.
Definition type.h:292
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FuncType, Type, FuncTypeNode)
static FuncType OpaqueFunc(TypeDeriveFunc derive_func, bool purity=false, Location loc=UnknownLoc())
Constructing an opaque function type using derive_func.
FuncType(ffi::Array< Type > params, Type ret, bool purity=true, Location loc=UnknownLoc())
Constructor from parameter type and return value type.
FuncType(ffi::ObjectPtr< FuncTypeNode > data)
Definition type.h:294
static FuncType OpaqueFunc(Type ret=AnyType(), bool purity=false, Location loc=UnknownLoc())
Construct an opaque function using from return type.
Definition type.h:58
static void RegisterReflection()
Definition type.h:60
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.PackedFuncType", PackedFuncTypeNode, TypeNode)
Definition type.h:67
PackedFuncType(Location loc=UnknownLoc())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PackedFuncType, Type, PackedFuncTypeNode)
Base type of all Relax type information.
Definition type.h:102
int ndim
The number of dimension of the shape, can be unknown.
Definition type.h:110
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:105
static void RegisterReflection()
Definition type.h:115
bool IsUnknownNdim() const
Definition type.h:113
Managed reference to ShapeTypeNode.
Definition type.h:128
ShapeType(int ndim, Location loc=UnknownLoc())
Construction with known unknown symbolic shape patterns.
ShapeType(ffi::Array< PrimExpr > values, Location loc=UnknownLoc())
Construction with known symbolic shape patterns.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ShapeType, Type, ShapeTypeNode)
Type of Tensor.
Definition type.h:149
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.TensorType", TensorTypeNode, TypeNode)
bool IsUnknownDtype() const
Definition type.h:172
ffi::Optional< ffi::Array< PrimExpr > > GetShape() const
Definition type.h:175
ffi::Optional< VDevice > vdevice
The virtual device, indicates where the tensor is expected to be executed.
Definition type.h:159
ffi::Optional< tvm::PrimType > dtype
The content dtype, or nullopt if the dtype is unknown.
Definition type.h:161
static void RegisterReflection()
Definition type.h:185
bool IsUnknownNdim() const
Definition type.h:169
ffi::Optional< Expr > shape
optionally store the shape expression of the tensor.
Definition type.h:155
int ndim
The number of dimension of the tensor, can be unknown.
Definition type.h:166
Managed reference to TensorTypeNode.
Definition type.h:200
TensorType(ffi::Optional< tvm::PrimType > dtype, int ndim, ffi::Optional< VDevice > vdevice=std::nullopt, Location loc=UnknownLoc())
Construction with an unknown shape expression.
TensorType(Expr shape, ffi::Optional< tvm::PrimType > dtype=std::nullopt, ffi::Optional< VDevice > vdevice=std::nullopt, Location loc=UnknownLoc())
Construction with a known shape expression.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TensorType, Type, TensorTypeNode)
TensorType(ffi::ObjectPtr< TensorTypeNode > data)
Definition type.h:202
Serializable global function used in IR.
TIR expressions.
IR/AST nodes for TVM types shared across IR variants.
bool HasVoidType(const Expr &expr)
Whether the expr has void type.
Definition type.h:394
void UpdateType(Expr expr, Type ty)
Update the type of an Expr.
Type GetType(const Expr &expr)
Get the underlying Relax type of expr.
Definition type.h:382
const T * GetTypeAs(const Expr &expr)
Get the type of a given expr and try to cast it as const T*.
Definition type.h:370
ffi::Optional< T > MatchType(const Expr &expr)
Match and check if expr has Relax type T and return it.
Definition type.h:350
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Global information used by Relax.