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_TIRX_TYPE_H_
25#define TVM_TIRX_TYPE_H_
26
27#include <tvm/ir/expr.h>
28#include <tvm/ir/type.h>
29#include <tvm/tirx/layout.h>
30
31#include <optional>
32
33namespace tvm::tirx {
34
35#ifndef TVM_INDEX_DEFAULT_I64
36#define TVM_INDEX_DEFAULT_I64 1
37#endif
40#if TVM_INDEX_DEFAULT_I64
41 static const PrimType default_index_ty = PrimType::Int(64);
42#else
43 static const PrimType default_index_ty = PrimType::Int(32);
44#endif
45 return default_index_ty;
46}
47
48inline DLDataType DefaultIndexType() {
49#if TVM_INDEX_DEFAULT_I64
50 return DLDataType{kDLInt, 64, 1};
51#else
52 return DLDataType{kDLInt, 32, 1};
53#endif
54}
55
64class TensorTypeNode : public TypeNode {
65 public:
66 explicit TensorTypeNode(ffi::UnsafeInit tag) : elem_offset(tag) {}
67
69
73 ffi::String storage_scope;
80 ffi::Array<PrimExpr> shape;
85 ffi::Array<PrimExpr> strides;
96 ffi::Optional<Layout> layout;
97
98 static void RegisterReflection() {
99 namespace refl = tvm::ffi::reflection;
100 refl::ObjectDef<TensorTypeNode>()
101 .def_ro("dtype", &TensorTypeNode::dtype)
102 .def_ro("storage_scope", &TensorTypeNode::storage_scope)
103 .def_ro("shape", &TensorTypeNode::shape, refl::AttachFieldFlag::SEqHashDefSimple())
104 .def_ro("strides", &TensorTypeNode::strides, refl::AttachFieldFlag::SEqHashDefSimple())
105 .def_ro("elem_offset", &TensorTypeNode::elem_offset,
106 refl::AttachFieldFlag::SEqHashDefSimple())
107 .def_ro("data_alignment", &TensorTypeNode::data_alignment)
108 .def_ro("offset_factor", &TensorTypeNode::offset_factor)
109 .def_ro("layout", &TensorTypeNode::layout);
110 }
111
113 DLDataType DefaultIndexType() const {
114 return shape.size() != 0 ? shape[0].ty()->dtype : tvm::tirx::DefaultIndexType();
115 }
116
118 PrimType ElementType() const { return dtype; }
119
122
124 TVM_TIRX_DLL bool IsScalar(bool alloc_or_decl = true) const;
125
127 TVM_TIRX_DLL std::optional<int64_t> ConstantAllocationSize() const;
128
138 ffi::Array<PrimExpr> ElemOffset(ffi::Array<PrimExpr> index, bool inner = false) const;
139
141};
142
146class TensorType : public Type {
147 public:
148 TVM_TIRX_DLL TensorType(ffi::String storage_scope, PrimType dtype, ffi::Array<PrimExpr> shape,
149 ffi::Array<PrimExpr> strides, ffi::Optional<PrimExpr> elem_offset,
150 int data_alignment, int offset_factor,
151 ffi::Optional<Layout> layout = std::nullopt, Location loc = UnknownLoc());
152
154
155 explicit TensorType(ffi::ObjectPtr<TensorTypeNode> n) : Type(ffi::UnsafeInit{}) {
156 TVM_FFI_ICHECK(n != nullptr);
157 data_ = std::move(n);
158 }
159};
160
166 public:
167 static void RegisterReflection() {
168 namespace refl = tvm::ffi::reflection;
169 refl::ObjectDef<TensorMapTypeNode>();
170 }
172};
173
184
185} // namespace tvm::tirx
186#endif // TVM_TIRX_TYPE_H_
Non-nullable source-location metadata.
Definition location.h:91
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Definition base_expr.h:161
static TVM_TIRX_DLL PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
static TVM_TIRX_DLL PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Definition type.h:127
Type is the base type of all types.
Definition base_expr.h:54
Managed reference to TypeNode.
Definition base_expr.h:80
Reference to the shared immutable unknown location.
Definition location.h:105
The type of tensor map.
Definition type.h:165
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TensorMapType", TensorMapTypeNode, TypeNode)
static void RegisterReflection()
Definition type.h:167
Managed reference to TensorMapTypeNode.
Definition type.h:178
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TensorMapType, Type, TensorMapTypeNode)
TVM_TIRX_DLL TensorMapType(Location loc=UnknownLoc())
Structural type of a TIRx tensor variable.
Definition type.h:64
PtrType DataPtrType() const
Definition type.h:121
ffi::Optional< Layout > layout
The layout of the tensor.
Definition type.h:96
DLDataType DefaultIndexType() const
Definition type.h:113
ffi::Array< PrimExpr > ElemOffset(ffi::Array< PrimExpr > index, bool inner=false) const
Determine the offset in the tensor of the given index.
PrimExpr elem_offset
The offset in terms of number of dtype elements (including lanes)
Definition type.h:87
int data_alignment
Alignment requirement of data pointer in bytes.
Definition type.h:89
ffi::Array< PrimExpr > shape
The type of the tensor prior to flattening.
Definition type.h:80
static void RegisterReflection()
Definition type.h:98
TensorTypeNode(ffi::UnsafeInit tag)
Definition type.h:66
int offset_factor
Factor of elem_offset field, elem_offset is guaranteed to be multiple of offset_factor.
Definition type.h:94
PrimType dtype
dtype in the content of the tensor
Definition type.h:71
ffi::Array< PrimExpr > strides
The strides of each dimension This can be an empty array, indicating array is contiguous.
Definition type.h:85
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TensorType", TensorTypeNode, TypeNode)
PrimType ElementType() const
Definition type.h:118
TVM_TIRX_DLL bool IsScalar(bool alloc_or_decl=true) const
Whether this type supports scalar tensor syntax.
ffi::String storage_scope
Storage scope/address space of the tensor.
Definition type.h:73
TensorTypeNode(PrimExpr elem_offset)
Definition type.h:68
TVM_TIRX_DLL std::optional< int64_t > ConstantAllocationSize() const
Return the constant element count, or nullopt for symbolic extents or overflow.
Managed reference to TensorTypeNode.
Definition type.h:146
TensorType(ffi::ObjectPtr< TensorTypeNode > n)
Definition type.h:155
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TensorType, Type, TensorTypeNode)
TVM_TIRX_DLL TensorType(ffi::String storage_scope, PrimType dtype, ffi::Array< PrimExpr > shape, ffi::Array< PrimExpr > strides, ffi::Optional< PrimExpr > elem_offset, int data_alignment, int offset_factor, ffi::Optional< Layout > layout=std::nullopt, Location loc=UnknownLoc())
Base expr nodes in TVM.
IR/AST nodes for TVM types shared across IR variants.
Definition of layout.
Backend-defined execution hierarchy nodes shared by layouts and tile calls.
Definition axis_group_graph.h:41
PrimType DefaultIndexPrimType()
if TVM_INDEX_DEFAULT_I64 is set, return int64, otherwise return int32
Definition type.h:39
DLDataType DefaultIndexType()
Definition type.h:48
#define TVM_TIRX_DLL
Definition base.h:83