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/object.h>
32 #include <tvm/runtime/tensor.h>
33 #include <tvm/tirx/var.h>
34 
35 #include <utility>
36 
37 namespace tvm {
38 namespace arith {
39 class Analyzer;
40 }
41 } // namespace tvm
42 
43 namespace tvm {
44 namespace tirx {
45 
53 class IndexMapNode : public Object {
54  public:
60  ffi::Array<Var> initial_indices;
61 
72  ffi::Array<PrimExpr> final_indices;
73 
84  ffi::Optional<ObjectRef> inverse_index_map;
85 
93 
106  ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices,
107  arith::Analyzer* analyzer) const;
108 
125  ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges, arith::Analyzer* analyzer) const;
126 
138  ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape, arith::Analyzer* analyzer) const;
139 
140  /* \brief Map an Tensor according to this index map
141  *
142  * \param arr_src The Tensor whose layout is transformed by this index map.
143  *
144  * \returns The transformed Tensor.
145  */
147 
153  ffi::String ToPythonString(
154  const std::function<ffi::Optional<ffi::String>(const Var& var)>& f_name_map = nullptr) const;
155 
156  static void RegisterReflection() {
157  namespace refl = tvm::ffi::reflection;
158  refl::ObjectDef<IndexMapNode>()
159  .def_ro("initial_indices", &IndexMapNode::initial_indices,
160  refl::AttachFieldFlag::SEqHashDef())
161  .def_ro("final_indices", &IndexMapNode::final_indices)
162  .def_ro("inverse_index_map", &IndexMapNode::inverse_index_map,
163  refl::AttachFieldFlag::SEqHashIgnore());
164  }
165 
166  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
168 };
169 
170 class IndexMap : public ObjectRef {
171  public:
178  IndexMap(ffi::Array<Var> initial_indices, ffi::Array<PrimExpr> final_indices,
179  ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
180 
188  static IndexMap FromFunc(int ndim, ffi::TypedFunction<ffi::Array<PrimExpr>(ffi::Array<Var>)> func,
189  ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
190 
199  IndexMap Inverse(ffi::Array<Range> initial_ranges, arith::Analyzer* analyzer) const;
200 
210  const std::function<ffi::Optional<ffi::String>(const Var& var)>& f_name_map = nullptr) const;
211 
221  std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(ffi::Array<Range> initial_ranges,
222  arith::Analyzer* analyzer) const;
223 
225 };
226 
232 IndexMap Substitute(const IndexMap& index_map,
233  std::function<ffi::Optional<PrimExpr>(const Var& var)> f_subst);
234 
235 } // namespace tirx
236 } // namespace tvm
237 
238 #endif // TVM_TIR_INDEX_MAP_H_
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:634
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:54
Defines a mapping between two representations of indices into a buffer.
Definition: index_map.h:53
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: index_map.h:166
ffi::Array< PrimExpr > MapShape(const ffi::Array< PrimExpr > &shape, arith::Analyzer *analyzer) const
Map a buffer shape to the output space.
ffi::Array< Range > MapRanges(const ffi::Array< Range > &ranges, arith::Analyzer *analyzer) const
Map a memory range to the output space.
IndexMapNode()
Default constructor.
Definition: index_map.h:92
ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices, arith::Analyzer *analyzer) const
Map indices to the output space.
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::Optional< ObjectRef > inverse_index_map
The inverse index map.
Definition: index_map.h:84
static void RegisterReflection()
Definition: index_map.h:156
ffi::Array< Var > initial_indices
Variables representing the indices prior to remapping.
Definition: index_map.h:60
ffi::Array< PrimExpr > final_indices
Expressions defining the indices after remapping.
Definition: index_map.h:72
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapNode, Object)
Definition: index_map.h:170
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IndexMap, ObjectRef, IndexMapNode)
IndexMap(ffi::Array< Var > initial_indices, ffi::Array< PrimExpr > final_indices, ffi::Optional< IndexMap > inverse_index_map=std::nullopt)
The constructor.
std::pair< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges, arith::Analyzer *analyzer) const
Generate the inverse mapping.
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, arith::Analyzer *analyzer) const
Generate the inverse mapping.
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.
a named variable in TIR
Definition: var.h:76
Base expr nodes in TVM.
Definition: repr_printer.h:91
Var var(std::string name_hint, DataType t=DataType::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, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1981
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
A managed object in the TVM runtime.
A device-independent managed Tensor abstraction.
Variables in the TIR.