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/ffi/container/array.h>
30 #include <tvm/ir/expr.h>
31 #include <tvm/runtime/tensor.h>
32 #include <tvm/tirx/var.h>
33 
34 #include <utility>
35 
36 namespace tvm {
37 namespace arith {
38 class Analyzer;
39 } // namespace arith
40 } // namespace tvm
41 
42 namespace tvm {
43 namespace tirx {
44 
52 class IndexMapNode : public ffi::Object {
53  public:
59  ffi::Array<PrimVar> initial_indices;
60 
71  ffi::Array<PrimExpr> final_indices;
72 
83  ffi::Optional<ffi::ObjectRef> inverse_index_map;
84 
92 
101  ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices) const;
111  ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices,
112  const arith::Analyzer& analyzer) const;
113 
126  ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges) const;
140  ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges,
141  const arith::Analyzer& analyzer) const;
142 
150  ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape) const;
159  ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape,
160  const arith::Analyzer& analyzer) const;
161 
162  /* \brief Map an Tensor according to this index map
163  *
164  * \param arr_src The Tensor whose layout is transformed by this index map.
165  *
166  * \returns The transformed Tensor.
167  */
169 
175  ffi::String ToPythonString(
176  const std::function<ffi::Optional<ffi::String>(const Var& var)>& f_name_map = nullptr) const;
177 
178  static void RegisterReflection() {
179  namespace refl = tvm::ffi::reflection;
180  refl::ObjectDef<IndexMapNode>()
181  .def_ro("initial_indices", &IndexMapNode::initial_indices,
182  refl::AttachFieldFlag::SEqHashDefRecursive())
183  .def_ro("final_indices", &IndexMapNode::final_indices)
184  .def_ro("inverse_index_map", &IndexMapNode::inverse_index_map,
185  refl::AttachFieldFlag::SEqHashIgnore());
186  }
187 
188  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
189  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapNode, ffi::Object);
190 };
191 
192 class IndexMap : public ffi::ObjectRef {
193  public:
200  IndexMap(ffi::Array<PrimVar> initial_indices, ffi::Array<PrimExpr> final_indices,
201  ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
202 
210  static IndexMap FromFunc(int ndim, ffi::TypedFunction<ffi::Array<PrimExpr>(ffi::Array<Var>)> func,
211  ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
212 
222  IndexMap Inverse(ffi::Array<Range> initial_ranges) const;
234  IndexMap Inverse(ffi::Array<Range> initial_ranges, const arith::Analyzer& analyzer) const;
235 
245  const std::function<ffi::Optional<ffi::String>(const Var& var)>& f_name_map = nullptr) const;
246 
257  std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(ffi::Array<Range> initial_ranges) const;
270  std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(ffi::Array<Range> initial_ranges,
271  const arith::Analyzer& analyzer) const;
272 
274 };
275 
281 IndexMap Substitute(const IndexMap& index_map,
282  std::function<ffi::Optional<PrimExpr>(const Var& var)> f_subst);
283 
284 } // namespace tirx
285 } // namespace tvm
286 
287 #endif // TVM_TIR_INDEX_MAP_H_
Managed reference to AnalyzerObj.
Definition: analyzer.h:913
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:49
Defines a mapping between two representations of indices into a buffer.
Definition: index_map.h:52
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: index_map.h:188
ffi::Array< Range > MapRanges(const ffi::Array< Range > &ranges, const arith::Analyzer &analyzer) const
Map a memory range to the output space using an existing analyzer.
ffi::Array< PrimExpr > MapShape(const ffi::Array< PrimExpr > &shape, const arith::Analyzer &analyzer) const
Map a buffer shape to the output space using an existing analyzer.
ffi::Array< PrimVar > initial_indices
Variables representing the indices prior to remapping.
Definition: index_map.h:59
ffi::Optional< ffi::ObjectRef > inverse_index_map
The inverse index map.
Definition: index_map.h:83
IndexMapNode()
Default constructor.
Definition: index_map.h:91
ffi::String ToPythonString(const std::function< ffi::Optional< ffi::String >(const Var &var)> &f_name_map=nullptr) const
Convert to string representation in Python.
runtime::Tensor MapTensor(runtime::Tensor arr_src) const
ffi::Array< Range > MapRanges(const ffi::Array< Range > &ranges) const
Map a memory range to the output space using a fresh analyzer.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapNode, ffi::Object)
ffi::Array< PrimExpr > MapShape(const ffi::Array< PrimExpr > &shape) const
Map a buffer shape to the output space using a fresh analyzer.
ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices) const
Map indices to the output space using a fresh analyzer.
static void RegisterReflection()
Definition: index_map.h:178
ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices, const arith::Analyzer &analyzer) const
Map indices to the output space using an existing analyzer.
ffi::Array< PrimExpr > final_indices
Expressions defining the indices after remapping.
Definition: index_map.h:71
Definition: index_map.h:192
std::pair< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges, const arith::Analyzer &analyzer) const
Generate the inverse mapping using an existing analyzer.
IndexMap(ffi::Array< PrimVar > initial_indices, ffi::Array< PrimExpr > final_indices, ffi::Optional< IndexMap > inverse_index_map=std::nullopt)
The constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IndexMap, ffi::ObjectRef, IndexMapNode)
static IndexMap FromFunc(int ndim, ffi::TypedFunction< ffi::Array< PrimExpr >(ffi::Array< Var >)> func, ffi::Optional< IndexMap > inverse_index_map=std::nullopt)
Create an index map from a packed function.
IndexMap Inverse(ffi::Array< Range > initial_ranges, const arith::Analyzer &analyzer) const
Generate the inverse mapping using an existing analyzer.
IndexMap RenameVariables(const std::function< ffi::Optional< ffi::String >(const Var &var)> &f_name_map=nullptr) const
Rename the variables in the index map and ensure the names are unique.
IndexMap Inverse(ffi::Array< Range > initial_ranges) const
Generate the inverse mapping using a fresh analyzer.
std::pair< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges) const
Generate the inverse mapping using a fresh analyzer.
a named variable in TIR
Definition: var.h:68
Base expr nodes in TVM.
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
IndexMap Substitute(const IndexMap &index_map, std::function< ffi::Optional< PrimExpr >(const Var &var)> f_subst)
Substitute variables in an index map.
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
A device-independent managed Tensor abstraction.
Variables in the TIR.