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/arith/bound.h>
28#include <tvm/ffi/reflection/registry.h>
29#include <tvm/ir/prim/expr.h>
30#include <tvm/tirx/op.h>
31
32#include <string>
33#include <type_traits>
34#include <utility>
35#include <vector>
36
37namespace tvm {
38namespace te {
39
40using arith::IntSet;
41using namespace tvm::tirx;
42
43// internal node container for Operation
44class OperationNode;
45class Tensor;
46
48class Operation : public ffi::ObjectRef {
49 public:
52 explicit Operation(ffi::ObjectPtr<ffi::Object> n) : ffi::ObjectRef(n) {}
53 explicit Operation(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
58 inline const OperationNode* operator->() const;
64 TVM_DLL Tensor output(size_t i) const;
67};
68
70class TensorNode : public OpaqueExprNode {
71 public:
73 ffi::Array<PrimExpr> shape;
80
81 static void RegisterReflection();
82
83 ffi::Array<PrimExpr> GetShape() const { return shape; }
84
85 PrimType GetDataType() const { return dtype; }
86
87 TVM_DLL ffi::String GetNameHint() const;
88
90
92};
93
98class Tensor : public OpaqueExpr {
99 private:
106 inline PrimExpr IndexTensor(ffi::Array<PrimExpr> indices, bool support_negative_indices) const;
107
108 public:
109 TVM_DLL Tensor(ffi::Array<PrimExpr> shape, PrimType dtype, Operation op, int value_index);
110
116 inline bool operator==(const Tensor& other) const;
122 inline bool operator!=(const Tensor& other) const;
124 inline size_t ndim() const;
130 template <typename... Args>
131 inline PrimExpr operator()(Args&&... args) const {
132 ffi::Array<PrimExpr> indices{std::forward<Args>(args)...};
133 return operator()(indices);
134 }
140 TVM_DLL PrimExpr operator()(ffi::Array<PrimExpr> indices) const;
146 TVM_DLL PrimExpr operator()(ffi::Array<PrimVar> indices) const;
152 template <typename... Args>
154 ffi::Array<PrimExpr> indices{std::forward<Args>(args)...};
155 return IndexWithNegativeIndices(indices);
156 }
162 TVM_DLL PrimExpr IndexWithNegativeIndices(ffi::Array<PrimExpr> indices) const;
168 TVM_DLL PrimExpr IndexWithNegativeIndices(ffi::Array<PrimVar> indices) const;
169
174 class Slice {
175 public:
176 // construct via tensor and indices
177 Slice(const Tensor& tensor, std::vector<PrimExpr> indices)
178 : tensor_(tensor), indices_(indices) {}
185 std::vector<PrimExpr> other = indices_;
186 other.emplace_back(i);
187 return Slice(tensor_, other);
188 }
194 inline operator PrimExpr() const { return tensor_(indices_); }
195
196 private:
197 const Tensor& tensor_;
198 std::vector<PrimExpr> indices_;
199 };
205 inline Slice operator[](PrimExpr i) const { return Slice(*this, {i}); }
206
208};
209
211TVM_DLL bool IsTensorLoad(const Expr& expr);
212
215
217TVM_DLL ffi::Array<PrimExpr> GetTensorLoadIndices(const Call& call);
218
219// Implementations of inline functions
220inline size_t Tensor::ndim() const { return (*this)->shape.size(); }
221
222inline bool Tensor::operator==(const Tensor& other) const {
223 if (get() == other.get()) return true;
224 if (get() == nullptr || other.get() == nullptr) return false;
225 if ((*this)->op.defined() || other->op.defined()) {
226 return (*this)->op == other->op && (*this)->value_index == other->value_index;
227 } else {
228 return false;
229 }
230}
231
232inline bool Tensor::operator!=(const Tensor& other) const { return !(*this == other); }
233
234// macro to turn every operation of slice to expression
235#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op) \
236 inline PrimExpr operator Op(const Tensor::Slice& a) { return Op a.operator PrimExpr(); }
237
238#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op) \
239 template <typename T> \
240 inline PrimExpr operator Op(const Tensor::Slice& a, const T& b) { \
241 return a.operator PrimExpr() Op b; \
242 } \
243 template <typename T> \
244 inline PrimExpr operator Op(const T& a, const Tensor::Slice& b) { \
245 return a Op b.operator PrimExpr(); \
246 } \
247 inline PrimExpr operator Op(const Tensor::Slice& a, const Tensor::Slice& b) { \
248 return a.operator PrimExpr() Op b.operator PrimExpr(); \
249 }
250
266
267} // namespace te
268} // namespace tvm
269
270namespace std {
271template <>
272struct hash<::tvm::te::Operation> : public ::tvm::ffi::ObjectPtrHash {};
273
274template <>
275struct hash<::tvm::te::Tensor> {
276 std::size_t operator()(const ::tvm::te::Tensor& k) const {
277 ::tvm::ffi::ObjectPtrHash hasher;
278 if (k.defined() && k->op.defined()) {
279 return hasher(k->op);
280 } else {
281 return hasher(k);
282 }
283 }
284};
285} // namespace std
286#endif // TVM_TE_TENSOR_H_
Bound deducers.
Managed reference to CallNode.
Definition expr.h:474
Managed reference to ExprNode.
Definition base_expr.h:335
Base node for opaque construction-time expressions.
Definition base_expr.h:352
Managed reference to OpaqueExprNode.
Definition base_expr.h:364
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
static PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
ContextType * get()
Definition with_context.h:81
Base class of all operation nodes.
Definition operation.h:157
Operation that produces tensors.
Definition tensor.h:48
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:507
Operation()
default constructor
Definition tensor.h:51
Operation(ffi::UnsafeInit tag)
Definition tensor.h:53
Operation(ffi::ObjectPtr< ffi::Object > n)
Definition tensor.h:52
Opaque construction-time node that represents a tensor.
Definition tensor.h:70
ffi::Array< PrimExpr > GetShape() const
Definition tensor.h:83
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("te.Tensor", TensorNode, OpaqueExprNode)
ffi::Array< PrimExpr > shape
The shape of the tensor.
Definition tensor.h:73
static void RegisterReflection()
Operation op
the source operation, can be None
Definition tensor.h:77
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition tensor.h:89
ffi::String GetNameHint() const
PrimType dtype
dtype in the content of the tensor
Definition tensor.h:75
int value_index
the output index from source operation
Definition tensor.h:79
PrimType GetDataType() const
Definition tensor.h:85
data structure to represent a slice that fixes first k coordinates. This is used to enable syntax sug...
Definition tensor.h:174
Slice operator[](PrimExpr i)
get i-th slice from the current slice.
Definition tensor.h:184
Slice(const Tensor &tensor, std::vector< PrimExpr > indices)
Definition tensor.h:177
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:98
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:222
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:153
Slice operator[](PrimExpr i) const
get i-th slice from the current Tensor.
Definition tensor.h:205
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Tensor, OpaqueExpr, TensorNode)
size_t ndim() const
Definition tensor.h:220
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:232
PrimExpr operator()(Args &&... args) const
Take elements from the tensor.
Definition tensor.h:131
Tensor(ffi::Array< PrimExpr > shape, PrimType dtype, Operation op, int value_index)
TIR expressions.
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 is a Call whose callee is a TE Tensor.
Tensor GetTensorFromLoad(const Call &call)
Recover and validate the Tensor callee of a tensor-load Call.
Definition axis_group_graph.h:39
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op)
Definition tensor.h:235
#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op)
Definition tensor.h:238
Common operators defined for Expr.