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/ffi/reflection/registry.h>
28#include <tvm/ir/prim/expr.h>
29#include <tvm/ir/prim/op.h>
30#include <tvm/s_tir/iter_var.h>
31#include <tvm/sym/bound.h>
32#include <tvm/tirx/expr.h>
33
34#include <string>
35#include <type_traits>
36#include <utility>
37#include <vector>
38
39namespace tvm {
40namespace te {
41using s_tir::IterVar;
42using s_tir::IterVarNode;
46using s_tir::kDataPar;
47using s_tir::kOpaque;
48using s_tir::kOrdered;
54using sym::IntSet;
55
56using namespace tvm::tirx;
57
58// internal node container for Operation
59class OperationNode;
60class Tensor;
61
63class Operation : public ffi::ObjectRef {
64 public:
65 static constexpr bool _type_is_nullable = false;
66 explicit Operation(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
71 inline const OperationNode* operator->() const;
77 TVM_DLL Tensor output(size_t i) const;
80};
81
83class TensorNode : public OpaqueExprNode {
84 public:
86 ffi::Array<PrimExpr> shape;
90 ffi::Optional<Operation> op;
93
94 static void RegisterReflection();
95
96 ffi::Array<PrimExpr> GetShape() const { return shape; }
97
98 PrimType GetDataType() const { return dtype; }
99
100 TVM_DLL ffi::String GetNameHint() const;
101
102 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindConstTreeNode;
103
105};
106
111class Tensor : public OpaqueExpr {
112 private:
119 inline PrimExpr IndexTensor(ffi::Array<PrimExpr> indices, bool support_negative_indices) const;
120
121 public:
122 TVM_DLL Tensor(ffi::Array<PrimExpr> shape, PrimType dtype, ffi::Optional<Operation> op,
123 int value_index);
124
130 inline bool operator==(const Tensor& other) const;
136 inline bool operator!=(const Tensor& other) const;
138 inline size_t ndim() const;
144 template <typename... Args>
145 inline PrimExpr operator()(Args&&... args) const {
146 ffi::Array<PrimExpr> indices{std::forward<Args>(args)...};
147 return operator()(indices);
148 }
154 TVM_DLL PrimExpr operator()(ffi::Array<PrimExpr> indices) const;
160 TVM_DLL PrimExpr operator()(ffi::Array<PrimVar> indices) const;
166 template <typename... Args>
167 TVM_DLL PrimExpr IndexWithNegativeIndices(Args&&... args) const {
168 ffi::Array<PrimExpr> indices{std::forward<Args>(args)...};
169 return IndexWithNegativeIndices(indices);
170 }
176 TVM_DLL PrimExpr IndexWithNegativeIndices(ffi::Array<PrimExpr> indices) const;
182 TVM_DLL PrimExpr IndexWithNegativeIndices(ffi::Array<PrimVar> indices) const;
183
188 class Slice {
189 public:
190 // construct via tensor and indices
191 Slice(const Tensor& tensor, std::vector<PrimExpr> indices)
192 : tensor_(tensor), indices_(indices) {}
199 std::vector<PrimExpr> other = indices_;
200 other.emplace_back(i);
201 return Slice(tensor_, other);
202 }
208 inline operator PrimExpr() const { return tensor_(indices_); }
209
210 private:
211 const Tensor& tensor_;
212 std::vector<PrimExpr> indices_;
213 };
219 inline Slice operator[](PrimExpr i) const { return Slice(*this, {i}); }
220
221 explicit Tensor(ffi::ObjectPtr<TensorNode> node) : OpaqueExpr(std::move(node)) {}
222
224};
225
227TVM_DLL bool IsTensorLoad(const Expr& expr);
228
230TVM_DLL Tensor GetTensorFromLoad(const Call& call);
231
233TVM_DLL ffi::Array<PrimExpr> GetTensorLoadIndices(const Call& call);
234
235// Implementations of inline functions
236inline size_t Tensor::ndim() const { return (*this)->shape.size(); }
237
238inline bool Tensor::operator==(const Tensor& other) const {
239 if (get() == other.get()) return true;
240 if (get() == nullptr || other.get() == nullptr) return false;
241 if ((*this)->op.has_value() || other->op.has_value()) {
242 return (*this)->op == other->op && (*this)->value_index == other->value_index;
243 } else {
244 return false;
245 }
246}
247
248inline bool Tensor::operator!=(const Tensor& other) const { return !(*this == other); }
249
250// macro to turn every operation of slice to expression
251#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op) \
252 inline PrimExpr operator Op(const Tensor::Slice& a) { return Op a.operator PrimExpr(); }
253
254#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op) \
255 template <typename T> \
256 inline PrimExpr operator Op(const Tensor::Slice& a, const T& b) { \
257 return a.operator PrimExpr() Op b; \
258 } \
259 template <typename T> \
260 inline PrimExpr operator Op(const T& a, const Tensor::Slice& b) { \
261 return a Op b.operator PrimExpr(); \
262 } \
263 inline PrimExpr operator Op(const Tensor::Slice& a, const Tensor::Slice& b) { \
264 return a.operator PrimExpr() Op b.operator PrimExpr(); \
265 }
266
282
283} // namespace te
284} // namespace tvm
285
286namespace std {
287template <>
288struct hash<::tvm::te::Operation> : public ::tvm::ffi::ObjectPtrHash {};
289
290template <>
291struct hash<::tvm::te::Tensor> {
292 std::size_t operator()(const ::tvm::te::Tensor& k) const {
293 ::tvm::ffi::ObjectPtrHash hasher;
294 if (k.defined() && k->op.has_value()) {
295 return hasher(k->op.value());
296 } else {
297 return hasher(k);
298 }
299 }
300};
301} // namespace std
302#endif // TVM_TE_TENSOR_H_
Bound deducers.
Managed reference to CallNode.
Definition expr.h:972
Managed reference to ExprNode.
Definition base_expr.h:359
Base node for opaque construction-time expressions.
Definition base_expr.h:394
Managed reference to OpaqueExprNode.
Definition base_expr.h:406
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).
Base class of all operation nodes.
Definition operation.h:165
Operation that produces tensors.
Definition tensor.h:63
Tensor output(size_t i) const
get the i-th output of the operation.
const OperationNode * operator->() const
access the internal node container
Definition operation.h:521
static constexpr bool _type_is_nullable
Definition tensor.h:65
Operation(ffi::UnsafeInit tag)
Definition tensor.h:66
Opaque construction-time node that represents a tensor.
Definition tensor.h:83
ffi::Array< PrimExpr > GetShape() const
Definition tensor.h:96
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("te.Tensor", TensorNode, OpaqueExprNode)
ffi::Array< PrimExpr > shape
The shape of the tensor.
Definition tensor.h:86
ffi::Optional< Operation > op
the source operation, can be None
Definition tensor.h:90
static void RegisterReflection()
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition tensor.h:102
ffi::String GetNameHint() const
PrimType dtype
dtype in the content of the tensor
Definition tensor.h:88
int value_index
the output index from source operation
Definition tensor.h:92
PrimType GetDataType() const
Definition tensor.h:98
data structure to represent a slice that fixes first k coordinates. This is used to enable syntax sug...
Definition tensor.h:188
Slice operator[](PrimExpr i)
get i-th slice from the current slice.
Definition tensor.h:198
Slice(const Tensor &tensor, std::vector< PrimExpr > indices)
Definition tensor.h:191
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:111
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:238
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:167
Slice operator[](PrimExpr i) const
get i-th slice from the current Tensor.
Definition tensor.h:219
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Tensor, OpaqueExpr, TensorNode)
size_t ndim() const
Definition tensor.h:236
Tensor(ffi::ObjectPtr< TensorNode > node)
Definition tensor.h:221
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:248
PrimExpr operator()(Args &&... args) const
Take elements from the tensor.
Definition tensor.h:145
Tensor(ffi::Array< PrimExpr > shape, PrimType dtype, ffi::Optional< Operation > op, int value_index)
TIR expressions.
Shared primitive construction and constant helpers.
Axis metadata for schedulable tensor computations.
const char * IterVarType2String(IterVarType t)
Definition iter_var.h:159
IterVarType
Type of iteration variable. Each IterVar have a specific type.
Definition iter_var.h:39
@ kOrdered
Serial loops with loop carry dependency, the iteration must execute in order. Cannot be re-ordered.
Definition iter_var.h:71
@ kTensorized
Marks boundary of tensorization intrinsic.
Definition iter_var.h:99
@ kThreadIndex
The IterVar itself is a thread-index of a fixed thread launching group. Note that this is already ass...
Definition iter_var.h:56
@ kParallelized
The loop is parallelized.
Definition iter_var.h:95
@ kCommReduce
Communicative reduction. Cannot be directly parallelized.
Definition iter_var.h:63
@ kOpaque
IterVar is opaque,.
Definition iter_var.h:81
@ kDataPar
Data parallel iteration. This normally corresponds to axis of Tensor. Allow all IterVar manipulations...
Definition iter_var.h:48
@ kUnrolled
The execution is unrolled.
Definition iter_var.h:87
@ kVectorized
The loop is vectorized.
Definition iter_var.h:91
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.
Backend-defined execution hierarchy nodes shared by layouts and tile calls.
Definition axis_group_graph.h:41
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op)
Definition tensor.h:251
#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op)
Definition tensor.h:254
TIRx tensor expressions and construction helpers.