tvm
Loading...
Searching...
No Matches
tensor.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_TE_TENSOR_H_
25#define TVM_TE_TENSOR_H_
26
27#include <tvm/base.h>
28#include <tvm/ffi/reflection/registry.h>
29#include <tvm/ir/prim/expr.h>
30#include <tvm/ir/prim/op.h>
31#include <tvm/s_tir/iter_var.h>
32#include <tvm/sym/int_set.h>
33#include <tvm/tirx/expr.h>
34
35#include <string>
36#include <type_traits>
37#include <utility>
38#include <vector>
39
40namespace tvm {
41namespace te {
42using s_tir::IterVar;
43using s_tir::IterVarObj;
47using s_tir::kDataPar;
48using s_tir::kOpaque;
49using s_tir::kOrdered;
55using sym::IntSet;
56
57using namespace tvm::tirx;
58
59// internal node container for Operation
60class OperationObj;
61class Tensor;
62
64class Operation : public ffi::ObjectRef {
65 public:
66 static constexpr bool _type_is_nullable = false;
67 explicit Operation(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
72 inline const OperationObj* operator->() const;
78 TVM_DLL Tensor output(size_t i) const;
81};
82
84class TensorObj : public OpaqueExprObj {
85 public:
87 ffi::Array<PrimExpr> shape;
91 ffi::Optional<Operation> op;
94
95 static void RegisterReflection();
96
97 ffi::Array<PrimExpr> GetShape() const { return shape; }
98
99 PrimType GetDataType() const { return dtype; }
100
101 TVM_DLL ffi::String GetNameHint() const;
102
103 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindConstTreeNode;
104
106};
107
112class Tensor : public OpaqueExpr {
113 private:
120 inline PrimExpr IndexTensor(ffi::Array<PrimExpr> indices, bool support_negative_indices) const;
121
122 public:
123 TVM_DLL Tensor(ffi::Array<PrimExpr> shape, PrimType dtype, ffi::Optional<Operation> op,
124 int value_index);
125
131 inline bool operator==(const Tensor& other) const;
137 inline bool operator!=(const Tensor& other) const;
139 inline size_t ndim() const;
145 template <typename... Args>
146 inline PrimExpr operator()(Args&&... args) const {
147 ffi::Array<PrimExpr> indices{std::forward<Args>(args)...};
148 return operator()(indices);
149 }
155 TVM_DLL PrimExpr operator()(ffi::Array<PrimExpr> indices) const;
161 TVM_DLL PrimExpr operator()(ffi::Array<PrimVar> indices) const;
167 template <typename... Args>
168 TVM_DLL PrimExpr IndexWithNegativeIndices(Args&&... args) const {
169 ffi::Array<PrimExpr> indices{std::forward<Args>(args)...};
170 return IndexWithNegativeIndices(indices);
171 }
177 TVM_DLL PrimExpr IndexWithNegativeIndices(ffi::Array<PrimExpr> indices) const;
183 TVM_DLL PrimExpr IndexWithNegativeIndices(ffi::Array<PrimVar> indices) const;
184
189 class Slice {
190 public:
191 // construct via tensor and indices
192 Slice(const Tensor& tensor, std::vector<PrimExpr> indices)
193 : tensor_(tensor), indices_(indices) {}
200 std::vector<PrimExpr> other = indices_;
201 other.emplace_back(i);
202 return Slice(tensor_, other);
203 }
209 inline operator PrimExpr() const { return tensor_(indices_); }
210
211 private:
212 const Tensor& tensor_;
213 std::vector<PrimExpr> indices_;
214 };
220 inline Slice operator[](PrimExpr i) const { return Slice(*this, {i}); }
221
222 explicit Tensor(ffi::ObjectPtr<TensorObj> node) : OpaqueExpr(std::move(node)) {}
223
225};
226
228TVM_DLL bool IsTensorLoad(const Expr& expr);
229
231TVM_DLL Tensor GetTensorFromLoad(const Call& call);
232
234TVM_DLL ffi::Array<PrimExpr> GetTensorLoadIndices(const Call& call);
235
236// Implementations of inline functions
237inline size_t Tensor::ndim() const { return (*this)->shape.size(); }
238
239inline bool Tensor::operator==(const Tensor& other) const {
240 if (get() == other.get()) return true;
241 if (get() == nullptr || other.get() == nullptr) return false;
242 if ((*this)->op.has_value() || other->op.has_value()) {
243 return (*this)->op == other->op && (*this)->value_index == other->value_index;
244 } else {
245 return false;
246 }
247}
248
249inline bool Tensor::operator!=(const Tensor& other) const { return !(*this == other); }
250
251// macro to turn every operation of slice to expression
252#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op) \
253 inline PrimExpr operator Op(const Tensor::Slice& a) { return Op a.operator PrimExpr(); }
254
255#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op) \
256 template <typename T> \
257 inline PrimExpr operator Op(const Tensor::Slice& a, const T& b) { \
258 return a.operator PrimExpr() Op b; \
259 } \
260 template <typename T> \
261 inline PrimExpr operator Op(const T& a, const Tensor::Slice& b) { \
262 return a Op b.operator PrimExpr(); \
263 } \
264 inline PrimExpr operator Op(const Tensor::Slice& a, const Tensor::Slice& b) { \
265 return a.operator PrimExpr() Op b.operator PrimExpr(); \
266 }
267
283
284} // namespace te
285} // namespace tvm
286
287namespace std {
288template <>
289struct hash<::tvm::te::Operation> : public ::tvm::ffi::ObjectPtrHash {};
290
291template <>
292struct hash<::tvm::te::Tensor> {
293 std::size_t operator()(const ::tvm::te::Tensor& k) const {
294 ::tvm::ffi::ObjectPtrHash hasher;
295 if (k.defined() && k->op.has_value()) {
296 return hasher(k->op.value());
297 } else {
298 return hasher(k);
299 }
300 }
301};
302} // namespace std
303#endif // TVM_TE_TENSOR_H_
TIR expressions.
Shared primitive construction and constant helpers.
TIRx tensor expressions and construction helpers.
Managed reference to CallObj.
Definition expr.h:825
Managed reference to ExprObj.
Definition expr.h:101
Base node for opaque construction-time expressions.
Definition expr.h:136
Managed reference to OpaqueExprObj.
Definition expr.h:148
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Definition type.h:182
static TVM_TIRX_DLL PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
Base class of all operation nodes.
Definition operation.h:165
Operation that produces tensors.
Definition tensor.h:64
Tensor output(size_t i) const
get the i-th output of the operation.
static constexpr bool _type_is_nullable
Definition tensor.h:66
const OperationObj * operator->() const
access the internal node container
Definition operation.h:521
Operation(ffi::UnsafeInit tag)
Definition tensor.h:67
Opaque construction-time node that represents a tensor.
Definition tensor.h:84
ffi::Array< PrimExpr > shape
The shape of the tensor.
Definition tensor.h:87
int value_index
the output index from source operation
Definition tensor.h:93
ffi::Array< PrimExpr > GetShape() const
Definition tensor.h:97
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition tensor.h:103
PrimType dtype
dtype in the content of the tensor
Definition tensor.h:89
ffi::String GetNameHint() const
ffi::Optional< Operation > op
the source operation, can be None
Definition tensor.h:91
PrimType GetDataType() const
Definition tensor.h:99
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("te.Tensor", TensorObj, OpaqueExprObj)
static void RegisterReflection()
data structure to represent a slice that fixes first k coordinates. This is used to enable syntax sug...
Definition tensor.h:189
Slice operator[](PrimExpr i)
get i-th slice from the current slice.
Definition tensor.h:199
Slice(const Tensor &tensor, std::vector< PrimExpr > indices)
Definition tensor.h:192
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:112
PrimExpr IndexWithNegativeIndices(ffi::Array< PrimExpr > indices) const
Take elements from the tensor with support for negative indices.
PrimExpr IndexWithNegativeIndices(ffi::Array< PrimVar > indices) const
Take elements from the tensor with support for negative indices.
bool operator==(const Tensor &other) const
check if two tensors equals each other.
Definition tensor.h:239
PrimExpr operator()(ffi::Array< PrimVar > indices) const
Take elements from the tensor.
PrimExpr IndexWithNegativeIndices(Args &&... args) const
Take elements from the tensor with support for negative indices.
Definition tensor.h:168
Slice operator[](PrimExpr i) const
get i-th slice from the current Tensor.
Definition tensor.h:220
size_t ndim() const
Definition tensor.h:237
PrimExpr operator()(ffi::Array< PrimExpr > indices) const
Take elements from the tensor.
bool operator!=(const Tensor &other) const
check if two tensors are different.
Definition tensor.h:249
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Tensor, OpaqueExpr, TensorObj)
PrimExpr operator()(Args &&... args) const
Take elements from the tensor.
Definition tensor.h:146
Tensor(ffi::ObjectPtr< TensorObj > node)
Definition tensor.h:222
Tensor(ffi::Array< PrimExpr > shape, PrimType dtype, ffi::Optional< Operation > op, int value_index)
Parent compiler symbol visibility.
#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op)
Definition tensor.h:252
#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op)
Definition tensor.h:255
Integer set.
Axis metadata for schedulable tensor computations.
const char * IterVarType2String(IterVarType t)
Definition iter_var.h:160
IterVarType
Type of iteration variable. Each IterVar have a specific type.
Definition iter_var.h:40
@ kOrdered
Serial loops with loop carry dependency, the iteration must execute in order. Cannot be re-ordered.
Definition iter_var.h:72
@ kTensorized
Marks boundary of tensorization intrinsic.
Definition iter_var.h:100
@ kThreadIndex
The IterVar itself is a thread-index of a fixed thread launching group. Note that this is already ass...
Definition iter_var.h:57
@ kParallelized
The loop is parallelized.
Definition iter_var.h:96
@ kCommReduce
Communicative reduction. Cannot be directly parallelized.
Definition iter_var.h:64
@ kOpaque
IterVar is opaque,.
Definition iter_var.h:82
@ kDataPar
Data parallel iteration. This normally corresponds to axis of Tensor. Allow all IterVar manipulations...
Definition iter_var.h:49
@ kUnrolled
The execution is unrolled.
Definition iter_var.h:88
@ kVectorized
The loop is vectorized.
Definition iter_var.h:92
ffi::Array< PrimExpr > GetTensorLoadIndices(const Call &call)
Recover and validate the primitive indices of a tensor-load Call.
bool IsTensorLoad(const Expr &expr)
Return whether an expression calls the registered te.tensor_load operator.
Tensor GetTensorFromLoad(const Call &call)
Recover and validate the first Tensor argument of a tensor-load Call.
Definition axis_group_graph.h:41
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41