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_TIRX_INDEX_MAP_H_
27#define TVM_TIRX_INDEX_MAP_H_
28
29#include <tvm/ffi/container/array.h>
30#include <tvm/ffi/container/tuple.h>
31#include <tvm/ir/expr.h>
32#include <tvm/runtime/tensor.h>
33
34#include <utility>
35
36namespace tvm {
37namespace sym {
38class Analyzer;
39} // namespace sym
40} // namespace tvm
41
42namespace tvm {
43namespace tirx {
44
52class IndexMapObj : 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 TVM_TIRX_DLL ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices) const;
111 TVM_TIRX_DLL ffi::Array<PrimExpr> MapIndices(const ffi::Array<PrimExpr>& indices,
112 const sym::Analyzer& analyzer) const;
113
126 TVM_TIRX_DLL ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges) const;
140 TVM_TIRX_DLL ffi::Array<Range> MapRanges(const ffi::Array<Range>& ranges,
141 const sym::Analyzer& analyzer) const;
142
150 TVM_TIRX_DLL ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape) const;
159 TVM_TIRX_DLL ffi::Array<PrimExpr> MapShape(const ffi::Array<PrimExpr>& shape,
160 const sym::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 */
168 TVM_TIRX_DLL ffi::Tensor MapTensor(ffi::Tensor arr_src) const;
169
176 const ffi::TypedFunction<ffi::Optional<ffi::String>(const Var& var)>& f_name_map =
177 nullptr) const;
178
179 static void RegisterReflection() {
180 namespace refl = tvm::ffi::reflection;
181 refl::ObjectDef<IndexMapObj>()
182 .def_ro("initial_indices", &IndexMapObj::initial_indices,
183 refl::AttachFieldFlag::SEqHashDefPattern())
184 .def_ro("final_indices", &IndexMapObj::final_indices)
185 .def_ro("inverse_index_map", &IndexMapObj::inverse_index_map,
186 refl::AttachFieldFlag::SEqHashIgnore());
187 }
188
189 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
190 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapObj, ffi::Object);
191};
192
193class IndexMap : public ffi::ObjectRef {
194 public:
201 TVM_TIRX_DLL IndexMap(ffi::Array<PrimVar> initial_indices, ffi::Array<PrimExpr> final_indices,
202 ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
203
212 int ndim, ffi::TypedFunction<ffi::Array<PrimExpr>(ffi::Array<Var>)> func,
213 ffi::Optional<IndexMap> inverse_index_map = std::nullopt);
214
224 TVM_TIRX_DLL IndexMap Inverse(ffi::Array<Range> initial_ranges) const;
236 TVM_TIRX_DLL IndexMap Inverse(ffi::Array<Range> initial_ranges,
237 const sym::Analyzer& analyzer) const;
238
248 RenameVariables(const ffi::TypedFunction<ffi::Optional<ffi::String>(const Var& var)>& f_name_map =
249 nullptr) const;
250
261 TVM_TIRX_DLL ffi::Tuple<IndexMap, PrimExpr> NonSurjectiveInverse(
262 ffi::Array<Range> initial_ranges) const;
275 TVM_TIRX_DLL ffi::Tuple<IndexMap, PrimExpr> NonSurjectiveInverse(
276 ffi::Array<Range> initial_ranges, const sym::Analyzer& analyzer) const;
277
279};
280
281} // namespace tirx
282} // namespace tvm
283
284#endif // TVM_TIRX_INDEX_MAP_H_
Base expr nodes in TVM.
#define TVM_TIRX_DLL
Definition base.h:55
Device-independent tensor allocation, copies, views and serialization.
Managed reference to VarObj.
Definition expr.h:571
Managed reference to AnalyzerObj.
Definition analyzer.h:750
Defines a mapping between two representations of indices into a tensor.
Definition index_map.h:52
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IndexMap", IndexMapObj, ffi::Object)
ffi::Array< PrimVar > initial_indices
Variables representing the indices prior to remapping.
Definition index_map.h:59
TVM_TIRX_DLL ffi::Tensor MapTensor(ffi::Tensor arr_src) const
ffi::Optional< ffi::ObjectRef > inverse_index_map
The inverse index map.
Definition index_map.h:83
TVM_TIRX_DLL ffi::String ToPythonString(const ffi::TypedFunction< ffi::Optional< ffi::String >(const Var &var)> &f_name_map=nullptr) const
Convert to string representation in Python.
IndexMapObj()
Default constructor.
Definition index_map.h:91
ffi::Array< PrimExpr > final_indices
Expressions defining the indices after remapping.
Definition index_map.h:71
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition index_map.h:189
TVM_TIRX_DLL 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.
TVM_TIRX_DLL 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.
static void RegisterReflection()
Definition index_map.h:179
TVM_TIRX_DLL ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices) const
Map indices to the output space using a fresh analyzer.
TVM_TIRX_DLL ffi::Array< Range > MapRanges(const ffi::Array< Range > &ranges) const
Map a memory range to the output space using a fresh analyzer.
TVM_TIRX_DLL ffi::Array< PrimExpr > MapShape(const ffi::Array< PrimExpr > &shape) const
Map a tensor shape to the output space using a fresh analyzer.
TVM_TIRX_DLL ffi::Array< PrimExpr > MapIndices(const ffi::Array< PrimExpr > &indices, const sym::Analyzer &analyzer) const
Map indices to the output space using an existing analyzer.
Definition index_map.h:193
static TVM_TIRX_DLL 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.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IndexMap, ffi::ObjectRef, IndexMapObj)
TVM_TIRX_DLL IndexMap(ffi::Array< PrimVar > initial_indices, ffi::Array< PrimExpr > final_indices, ffi::Optional< IndexMap > inverse_index_map=std::nullopt)
The constructor.
TVM_TIRX_DLL IndexMap Inverse(ffi::Array< Range > initial_ranges, const sym::Analyzer &analyzer) const
Generate the inverse mapping using an existing analyzer.
TVM_TIRX_DLL IndexMap RenameVariables(const ffi::TypedFunction< 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.
TVM_TIRX_DLL IndexMap Inverse(ffi::Array< Range > initial_ranges) const
Generate the inverse mapping using a fresh analyzer.
TVM_TIRX_DLL ffi::Tuple< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges) const
Generate the inverse mapping using a fresh analyzer.
TVM_TIRX_DLL ffi::Tuple< IndexMap, PrimExpr > NonSurjectiveInverse(ffi::Array< Range > initial_ranges, const sym::Analyzer &analyzer) const
Generate the inverse mapping using an existing analyzer.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41