tvm
index_map.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 
26 #ifndef TVM_TIR_INDEX_MAP_H_
27 #define TVM_TIR_INDEX_MAP_H_
28 
29 #include <tvm/ir/expr.h>
31 #include <tvm/runtime/object.h>
32 #include <tvm/tir/var.h>
33 
34 #include <utility>
35 
36 namespace tvm {
37 namespace arith {
38 class Analyzer;
39 }
40 } // namespace tvm
41 
42 namespace tvm {
43 namespace tir {
44 
52 class IndexMapNode : public Object {
53  public:
60 
72 
84 
92 
105  Array<PrimExpr> MapIndices(const Array<PrimExpr>& indices, arith::Analyzer* analyzer) const;
106 
123  Array<Range> MapRanges(const Array<Range>& ranges, arith::Analyzer* analyzer) const;
124 
137 
138  /* \brief Map an NDArray according to this index map
139  *
140  * \param arr_src The NDArray whose layout is transformed by this index map.
141  *
142  * \returns The transformed NDArray.
143  */
145 
152  const std::function<Optional<String>(const Var& var)>& f_name_map = nullptr) const;
153 
155  v->Visit("initial_indices", &initial_indices);
156  v->Visit("final_indices", &final_indices);
157  v->Visit("inverse_index_map", &inverse_index_map);
158  }
159 
160  bool SEqualReduce(const IndexMapNode* other, SEqualReducer equal) const {
161  return equal.DefEqual(initial_indices, other->initial_indices) &&
163  }
164 
165  void SHashReduce(SHashReducer hash_reduce) const {
166  hash_reduce.DefHash(initial_indices);
167  hash_reduce(final_indices);
168  }
169 
170  static constexpr const char* _type_key = "tir.IndexMap";
171  static constexpr const bool _type_has_method_sequal_reduce = true;
172  static constexpr const bool _type_has_method_shash_reduce = true;
174 };
175 
176 class IndexMap : public ObjectRef {
177  public:
184  IndexMap(Array<Var> initial_indices, Array<PrimExpr> final_indices,
185  Optional<IndexMap> inverse_index_map = NullOpt);
186 
195  Optional<IndexMap> inverse_index_map = NullOpt);
196 
205  IndexMap Inverse(Array<Range> initial_ranges, arith::Analyzer* analyzer) const;
206 
216  const std::function<Optional<String>(const Var& var)>& f_name_map = nullptr) const;
217 
227  std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(Array<Range> initial_ranges,
228  arith::Analyzer* analyzer) const;
229 
231 };
232 
238 IndexMap Substitute(const IndexMap& index_map,
239  std::function<Optional<PrimExpr>(const Var& var)> f_subst);
240 
241 } // namespace tir
242 } // namespace tvm
243 
244 #endif // TVM_TIR_INDEX_MAP_H_
Runtime Array container types.
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:137
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:198
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:629
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:63
Defines a mapping between two representations of indices into a buffer.
Definition: index_map.h:52
Array< PrimExpr > MapIndices(const Array< PrimExpr > &indices, arith::Analyzer *analyzer) const
Map indices to the output space.
static constexpr const char * _type_key
Definition: index_map.h:170
Array< PrimExpr > final_indices
Expressions defining the indices after remapping.
Definition: index_map.h:71
Optional< ObjectRef > inverse_index_map
The inverse index map.
Definition: index_map.h:83
Array< PrimExpr > MapShape(const Array< PrimExpr > &shape, arith::Analyzer *analyzer) const
Map a buffer shape to the output space.
Array< Var > initial_indices
Variables representing the indices prior to remapping.
Definition: index_map.h:59
bool SEqualReduce(const IndexMapNode *other, SEqualReducer equal) const
Definition: index_map.h:160
IndexMapNode()
Default constructor.
Definition: index_map.h:91
Array< Range > MapRanges(const Array< Range > &ranges, arith::Analyzer *analyzer) const
Map a memory range to the output space.
TVM_DECLARE_FINAL_OBJECT_INFO(IndexMapNode, Object)
void SHashReduce(SHashReducer hash_reduce) const
Definition: index_map.h:165
String ToPythonString(const std::function< Optional< String >(const Var &var)> &f_name_map=nullptr) const
Convert to string representation in Python.
void VisitAttrs(AttrVisitor *v)
Definition: index_map.h:154
static constexpr const bool _type_has_method_shash_reduce
Definition: index_map.h:172
runtime::NDArray MapNDArray(runtime::NDArray arr_src) const
static constexpr const bool _type_has_method_sequal_reduce
Definition: index_map.h:171
Definition: index_map.h:176
TVM_DEFINE_OBJECT_REF_METHODS(IndexMap, ObjectRef, IndexMapNode)
IndexMap Inverse(Array< Range > initial_ranges, arith::Analyzer *analyzer) const
Generate the inverse mapping.
static IndexMap FromFunc(int ndim, runtime::TypedPackedFunc< Array< PrimExpr >(Array< Var >)> func, Optional< IndexMap > inverse_index_map=NullOpt)
Create an index map from a packed function.
IndexMap RenameVariables(const std::function< Optional< String >(const Var &var)> &f_name_map=nullptr) const
Rename the variables in the index map and ensure the names are unique.
std::pair< IndexMap, PrimExpr > NonSurjectiveInverse(Array< Range > initial_ranges, arith::Analyzer *analyzer) const
Generate the inverse mapping.
IndexMap(Array< Var > initial_indices, Array< PrimExpr > final_indices, Optional< IndexMap > inverse_index_map=NullOpt)
The constructor.
a named variable in TIR
Definition: var.h:89
Base expr nodes in TVM.
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
IndexMap Substitute(const IndexMap &index_map, std::function< Optional< PrimExpr >(const Var &var)> f_subst)
Substitute variables in an index map.
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1913
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
A managed object in the TVM runtime.
Variables in the TIR.