tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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,
106  arith::Analyzer* analyzer = nullptr) const;
107 
124  Array<Range> MapRanges(const Array<Range>& ranges, arith::Analyzer* analyzer = nullptr) const;
125 
137  Array<PrimExpr> MapShape(const Array<PrimExpr>& shape, arith::Analyzer* analyzer = nullptr) const;
138 
139  /* \brief Map an NDArray according to this index map
140  *
141  * \param arr_src The NDArray whose layout is transformed by this index map.
142  *
143  * \returns The transformed NDArray.
144  */
145  runtime::NDArray MapNDArray(runtime::NDArray arr_src) const;
146 
152  String ToPythonString(
153  const std::function<Optional<String>(const Var& var)>& f_name_map = nullptr) const;
154 
156  v->Visit("initial_indices", &initial_indices);
157  v->Visit("final_indices", &final_indices);
158  v->Visit("inverse_index_map", &inverse_index_map);
159  }
160 
161  bool SEqualReduce(const IndexMapNode* other, SEqualReducer equal) const {
162  return equal.DefEqual(initial_indices, other->initial_indices) &&
163  equal(final_indices, other->final_indices);
164  }
165 
166  void SHashReduce(SHashReducer hash_reduce) const {
167  hash_reduce.DefHash(initial_indices);
168  hash_reduce(final_indices);
169  }
170 
171  static constexpr const char* _type_key = "tir.IndexMap";
172  static constexpr const bool _type_has_method_sequal_reduce = true;
173  static constexpr const bool _type_has_method_shash_reduce = true;
175 };
176 
177 class IndexMap : public ObjectRef {
178  public:
185  IndexMap(Array<Var> initial_indices, Array<PrimExpr> final_indices,
186  Optional<IndexMap> inverse_index_map = NullOpt);
187 
195  static IndexMap FromFunc(int ndim, runtime::TypedPackedFunc<Array<PrimExpr>(Array<Var>)> func,
196  Optional<IndexMap> inverse_index_map = NullOpt);
197 
206  IndexMap Inverse(Array<Range> initial_ranges) const;
207 
216  IndexMap RenameVariables(
217  const std::function<Optional<String>(const Var& var)>& f_name_map = nullptr) const;
218 
228  std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(Array<Range> initial_ranges) 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_
void SHashReduce(SHashReducer hash_reduce) const
Definition: index_map.h:166
bool DefEqual(const ObjectRef &lhs, const ObjectRef &rhs)
Reduce condition to comparison of two definitions, where free vars can be mapped. ...
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
Base expr nodes in TVM.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:110
Array< PrimExpr > final_indices
Expressions defining the indices after remapping.
Definition: index_map.h:71
Variables in the TIR.
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
a named variable in TIR
Definition: var.h:88
Array< Var > initial_indices
Variables representing the indices prior to remapping.
Definition: index_map.h:59
Optional< ObjectRef > inverse_index_map
The inverse index map.
Definition: index_map.h:83
base class of all object containers.
Definition: object.h:167
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
IndexMap Substitute(const IndexMap &index_map, std::function< Optional< PrimExpr >(const Var &var)> f_subst)
Substitute variables in an index map.
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
void VisitAttrs(AttrVisitor *v)
Definition: index_map.h:155
IndexMapNode()
Default constructor.
Definition: index_map.h:91
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Definition: index_map.h:177
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
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:1768
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Base class of all object reference.
Definition: object.h:511
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
bool SEqualReduce(const IndexMapNode *other, SEqualReducer equal) const
Definition: index_map.h:161
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:579
Defines a mapping between two representations of indices into a buffer.
Definition: index_map.h:52
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:187