tvm
Loading...
Searching...
No Matches
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
33#include <utility>
34
35namespace tvm {
36namespace sym {
37class Analyzer;
38} // namespace sym
39} // namespace tvm
40
41namespace tvm {
42namespace tirx {
43
51class IndexMapNode : public ffi::Object {
52 public:
58 ffi::Array<PrimVar> initial_indices;
59
70 ffi::Array<PrimExpr> final_indices;
71
82 ffi::Optional<ffi::ObjectRef> inverse_index_map;
83
91
100 ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices) const;
110 ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices,
111 const sym::Analyzer& analyzer) const;
112
125 ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges) const;
139 ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges, const sym::Analyzer& analyzer) const;
140
148 ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape) const;
157 ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape,
158 const sym::Analyzer& analyzer) const;
159
160 /* \brief Map an Tensor according to this index map
161 *
162 * \param arr_src The Tensor whose layout is transformed by this index map.
163 *
164 * \returns The transformed Tensor.
165 */
166 ffi::Tensor MapTensor(ffi::Tensor arr_src) const;
167
173 ffi::String ToPythonString(
174 const std::function<ffi::Optional<ffi::String>(const Var& var)>& f_name_map = nullptr) const;
175
176 static void RegisterReflection() {
177 namespace refl = tvm::ffi::reflection;
178 refl::ObjectDef<IndexMapNode>()
179 .def_ro("initial_indices", &IndexMapNode::initial_indices,
180 refl::AttachFieldFlag::SEqHashDefPattern())
181 .def_ro("final_indices", &IndexMapNode::final_indices)
182 .def_ro("inverse_index_map", &IndexMapNode::inverse_index_map,
183 refl::AttachFieldFlag::SEqHashIgnore());
184 }
185
186 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
187 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapNode, ffi::Object);
188};
189
190class IndexMap : public ffi::ObjectRef {
191 public:
198 IndexMap(ffi::Array<PrimVar> initial_indices, ffi::Array<PrimExpr> final_indices,
199 ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
200
208 static IndexMap FromFunc(int ndim, ffi::TypedFunction<ffi::Array<PrimExpr>(ffi::Array<Var>)> func,
209 ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
210
220 IndexMap Inverse(ffi::Array<Range> initial_ranges) const;
232 IndexMap Inverse(ffi::Array<Range> initial_ranges, const sym::Analyzer& analyzer) const;
233
243 const std::function<ffi::Optional<ffi::String>(const Var& var)>& f_name_map = nullptr) const;
244
255 std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(ffi::Array<Range> initial_ranges) const;
268 std::pair<IndexMap, PrimExpr> NonSurjectiveInverse(ffi::Array<Range> initial_ranges,
269 const sym::Analyzer& analyzer) const;
270
272};
273
274} // namespace tirx
275} // namespace tvm
276
277#endif // TVM_TIR_INDEX_MAP_H_
Managed reference to VarNode.
Definition expr.h:714
Managed reference to AnalyzerObj.
Definition analyzer.h:916
Defines a mapping between two representations of indices into a tensor.
Definition index_map.h:51
ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices, const sym::Analyzer &analyzer) const
Map indices to the output space using an existing analyzer.
ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices) const
Map indices to the output space using a fresh analyzer.
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition index_map.h:186
ffi::Tensor MapTensor(ffi::Tensor arr_src) const
ffi::Array< PrimVar > initial_indices
Variables representing the indices prior to remapping.
Definition index_map.h:58
ffi::Optional< ffi::ObjectRef > inverse_index_map
The inverse index map.
Definition index_map.h:82
ffi::Array< Range > MapRanges(const ffi::Array< Range > &ranges, const sym::Analyzer &analyzer) const
Map a memory range to the output space using an existing analyzer.
IndexMapNode()
Default constructor.
Definition index_map.h:90
ffi::String ToPythonString(const std::function< ffi::Optional< ffi::String >(const Var &var)> &f_name_map=nullptr) const
Convert to string representation in Python.
ffi::Array< Range > MapRanges(const ffi::Array< Range > &ranges) const
Map a memory range to the output space using a fresh analyzer.
ffi::Array< PrimExpr > MapShape(const ffi::Array< PrimExpr > &shape, const sym::Analyzer &analyzer) const
Map a tensor shape to the output space using an existing analyzer.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapNode, ffi::Object)
ffi::Array< PrimExpr > MapShape(const ffi::Array< PrimExpr > &shape) const
Map a tensor shape to the output space using a fresh analyzer.
static void RegisterReflection()
Definition index_map.h:176
ffi::Array< PrimExpr > final_indices
Expressions defining the indices after remapping.
Definition index_map.h:70
Definition index_map.h:190
IndexMap Inverse(ffi::Array< Range > initial_ranges, const sym::Analyzer &analyzer) const
Generate the inverse mapping using an existing analyzer.
std::pair< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges, const sym::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.
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.
std::pair< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges) const
Generate the inverse mapping using a fresh 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.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IndexMap, ffi::ObjectRef, IndexMapNode)
Base expr nodes in TVM.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Device-independent tensor allocation, copies, views and serialization.