tvm
iter_affine_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 
48 #ifndef TVM_ARITH_ITER_AFFINE_MAP_H_
49 #define TVM_ARITH_ITER_AFFINE_MAP_H_
50 
51 #include <tvm/arith/analyzer.h>
52 #include <tvm/ir/diagnostic.h>
53 #include <tvm/ir/expr.h>
54 #include <tvm/tir/var.h>
55 
56 namespace tvm {
57 namespace arith {
58 
66 class IterMapExprNode : public PrimExprNode {
67  public:
68  // overrides
70 
71  static constexpr const char* _type_key = "arith.IterMapExpr";
72  static constexpr const uint32_t _type_child_slots = 3;
74 };
75 
80 class IterMapExpr : public PrimExpr {
81  public:
83 };
84 
91 class IterMarkNode : public Object {
92  public:
102 
103  // overrides
105  v->Visit("source", &source);
106  v->Visit("extent", &extent);
107  }
108 
109  bool SEqualReduce(const IterMarkNode* other, SEqualReducer equal) const {
110  equal->MarkGraphNode();
111  return equal(source, other->source) && equal(extent, other->extent);
112  }
113 
114  void SHashReduce(SHashReducer hash_reduce) const {
115  hash_reduce->MarkGraphNode();
116  hash_reduce(source);
117  hash_reduce(extent);
118  }
119 
120  static constexpr const bool _type_has_method_sequal_reduce = true;
121  static constexpr const bool _type_has_method_shash_reduce = true;
122  static constexpr const char* _type_key = "arith.IterMark";
124 };
125 
130 class IterMark : public ObjectRef {
131  public:
137  TVM_DLL IterMark(PrimExpr source, PrimExpr extent);
138 
141 };
142 
149  public:
158 
159  // overrides
161  v->Visit("source", &source);
162  v->Visit("lower_factor", &lower_factor);
163  v->Visit("extent", &extent);
164  v->Visit("scale", &scale);
165  }
166 
167  bool SEqualReduce(const IterSplitExprNode* other, SEqualReducer equal) const {
168  return equal(source, other->source) && equal(lower_factor, other->lower_factor) &&
169  equal(extent, other->extent) && equal(scale, other->scale);
170  }
171 
172  void SHashReduce(SHashReducer hash_reduce) const {
173  hash_reduce(source);
174  hash_reduce(lower_factor);
175  hash_reduce(extent);
176  hash_reduce(scale);
177  }
178 
179  static constexpr const char* _type_key = "arith.IterSplitExpr";
181 };
182 
187 class IterSplitExpr : public IterMapExpr {
188  public:
193  TVM_DLL explicit IterSplitExpr(IterMark source);
199  TVM_DLL explicit IterSplitExpr(IterMark source, PrimExpr scale);
207  TVM_DLL explicit IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent,
208  PrimExpr scale);
209 
212 };
213 
220  public:
225 
226  // overrides
228  v->Visit("args", &args);
229  v->Visit("base", &base);
230  }
231 
232  bool SEqualReduce(const IterSumExprNode* other, SEqualReducer equal) const {
233  return equal(args, other->args) && equal(base, other->base);
234  }
235 
236  void SHashReduce(SHashReducer hash_reduce) const {
237  hash_reduce(args);
238  hash_reduce(base);
239  }
240 
241  static constexpr const char* _type_key = "arith.IterSumExpr";
243 };
244 
249 class IterSumExpr : public IterMapExpr {
250  public:
257 
260 };
261 
264  // Require the mapping to be bijective.
266  // Require the mapping to be surjective.
268  // No mapping safety check.
269  NoCheck = 3
270 };
271 
275 class IterMapResultNode : public Object {
276  public:
277  // The detected pattern if a match exists.
279 
280  // Any errors that occurred while converting the input indices. If
281  // the array is empty, the conversion was successful.
283 
293 
294  // overrides
296  v->Visit("errors", &errors);
297  v->Visit("indices", &indices);
298  v->Visit("padding_predicate", &padding_predicate);
299  }
300 
301  static constexpr const char* _type_key = "arith.IterMapResult";
303 };
304 
309 class IterMapResult : public ObjectRef {
310  public:
311  // constructor
312  IterMapResult() { data_ = make_object<IterMapResultNode>(); }
313 
315  IterMapResultNode* operator->() const { return static_cast<IterMapResultNode*>(get_mutable()); }
316 };
317 
341 IterMapResult DetectIterMap(const Array<PrimExpr>& indices, const Map<Var, Range>& input_iters,
342  const PrimExpr& predicate, IterMapLevel check_level,
343  arith::Analyzer* analyzer, bool simplify_trivial_iterators = true);
344 
357  const PrimExpr& input_pred, IterMapLevel check_level,
358  arith::Analyzer* analyzer, bool simplify_trivial_iterators = true);
359 
381  const Array<PrimExpr> outputs);
382 
411  const Map<Var, Range>& input_iters,
412  const Array<Var>& sub_iters, const PrimExpr& predicate,
413  IterMapLevel check_level, arith::Analyzer* analyzer,
414  bool simplify_trivial_iterators = true);
415 
422 
440  arith::Analyzer* analyzer);
441 
442 } // namespace arith
443 } // namespace tvm
444 #endif // TVM_ARITH_ITER_AFFINE_MAP_H_
Algebra expression simplifications.
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
Base node of all primitive expressions.
Definition: expr.h:86
Reference to PrimExprNode.
Definition: expr.h:115
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:137
virtual void MarkGraphNode()=0
Mark current comparison as graph node in hashing. Graph node hash will depends on the graph structure...
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:629
Base class of all iter map expressions.
Definition: iter_affine_map.h:66
TVM_DECLARE_BASE_OBJECT_INFO(IterMapExprNode, PrimExprNode)
static constexpr const char * _type_key
Definition: iter_affine_map.h:71
static constexpr const uint32_t _type_child_slots
Definition: iter_affine_map.h:72
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:69
Managed reference to IterMapExprNode.
Definition: iter_affine_map.h:80
TVM_DEFINE_OBJECT_REF_METHODS(IterMapExpr, PrimExpr, IterMapExprNode)
Result of DetectIterMap.
Definition: iter_affine_map.h:275
Array< IterSumExpr > indices
Definition: iter_affine_map.h:278
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:295
static constexpr const char * _type_key
Definition: iter_affine_map.h:301
TVM_DECLARE_FINAL_OBJECT_INFO(IterMapResultNode, Object)
Array< String > errors
Definition: iter_affine_map.h:282
PrimExpr padding_predicate
Boolean expression indicating if a specific value w.
Definition: iter_affine_map.h:292
Managed reference to IterMapResultNode.
Definition: iter_affine_map.h:309
IterMapResult()
Definition: iter_affine_map.h:312
IterMapResultNode * operator->() const
Definition: iter_affine_map.h:315
Mark the source as an iterator in [0, extent).
Definition: iter_affine_map.h:91
static constexpr const bool _type_has_method_shash_reduce
Definition: iter_affine_map.h:121
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:104
static constexpr const bool _type_has_method_sequal_reduce
Definition: iter_affine_map.h:120
bool SEqualReduce(const IterMarkNode *other, SEqualReducer equal) const
Definition: iter_affine_map.h:109
TVM_DECLARE_FINAL_OBJECT_INFO(IterMarkNode, Object)
PrimExpr extent
The extent of the iteration.
Definition: iter_affine_map.h:101
PrimExpr source
The source expression, can either be a IterSumExpr or a Var.
Definition: iter_affine_map.h:97
void SHashReduce(SHashReducer hash_reduce) const
Definition: iter_affine_map.h:114
static constexpr const char * _type_key
Definition: iter_affine_map.h:122
Managed reference to IterMarkExprNode.
Definition: iter_affine_map.h:130
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterMarkNode)
IterMark(PrimExpr source, PrimExpr extent)
constructor.
TVM_DEFINE_OBJECT_REF_METHODS(IterMark, ObjectRef, IterMarkNode)
Split of an iterator.
Definition: iter_affine_map.h:148
static constexpr const char * _type_key
Definition: iter_affine_map.h:179
PrimExpr lower_factor
The lower factor to split the source.
Definition: iter_affine_map.h:153
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:160
TVM_DECLARE_FINAL_OBJECT_INFO(IterSplitExprNode, IterMapExprNode)
IterMark source
The source marked iterator.
Definition: iter_affine_map.h:151
PrimExpr scale
Additional scale.
Definition: iter_affine_map.h:157
void SHashReduce(SHashReducer hash_reduce) const
Definition: iter_affine_map.h:172
bool SEqualReduce(const IterSplitExprNode *other, SEqualReducer equal) const
Definition: iter_affine_map.h:167
PrimExpr extent
The extent of the split.
Definition: iter_affine_map.h:155
Managed reference to IterSplitExprNode.
Definition: iter_affine_map.h:187
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSplitExprNode)
TVM_DEFINE_OBJECT_REF_METHODS(IterSplitExpr, IterMapExpr, IterSplitExprNode)
IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent, PrimExpr scale)
constructor
IterSplitExpr(IterMark source)
constructor from just source.
IterSplitExpr(IterMark source, PrimExpr scale)
constructor from just source.
Fuse multiple iterators by summing them with scaling.
Definition: iter_affine_map.h:219
void SHashReduce(SHashReducer hash_reduce) const
Definition: iter_affine_map.h:236
bool SEqualReduce(const IterSumExprNode *other, SEqualReducer equal) const
Definition: iter_affine_map.h:232
static constexpr const char * _type_key
Definition: iter_affine_map.h:241
Array< IterSplitExpr > args
The args to the sum.
Definition: iter_affine_map.h:222
TVM_DECLARE_FINAL_OBJECT_INFO(IterSumExprNode, IterMapExprNode)
PrimExpr base
The base offset.
Definition: iter_affine_map.h:224
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:227
Managed reference to IterSumExprNode.
Definition: iter_affine_map.h:249
IterSumExpr(Array< IterSplitExpr > args, PrimExpr base)
constructor.
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode)
TVM_DEFINE_OBJECT_REF_METHODS(IterSumExpr, IterMapExpr, IterSumExprNode)
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
Object * get_mutable() const
Definition: object.h:607
base class of all object containers.
Definition: object.h:171
A new diagnostic interface for TVM error reporting.
Base expr nodes in TVM.
Array< Array< IterMark > > SubspaceDivide(const Array< PrimExpr > &bindings, const Map< Var, Range > &input_iters, const Array< Var > &sub_iters, const PrimExpr &predicate, IterMapLevel check_level, arith::Analyzer *analyzer, bool simplify_trivial_iterators=true)
Detect if bindings can be written as [a_0*e_0 + b_0 + c_0, a_1*e_1 + b_1, ..., a_n*e_n + b_n].
IterMapLevel
Mapping level for iterators.
Definition: iter_affine_map.h:263
@ NoCheck
Definition: iter_affine_map.h:269
@ Bijective
Definition: iter_affine_map.h:265
@ Surjective
Definition: iter_affine_map.h:267
PrimExpr NormalizeIterMapToExpr(const PrimExpr &expr)
Given an expression that may contain IterMapExpr, transform it to normal PrimExpr.
Array< PrimExpr > IterMapSimplify(const Array< PrimExpr > &indices, const Map< Var, Range > &input_iters, const PrimExpr &input_pred, IterMapLevel check_level, arith::Analyzer *analyzer, bool simplify_trivial_iterators=true)
Use IterVarMap detector to rewrite and simplify the indices.
IterMapResult DetectIterMap(const Array< PrimExpr > &indices, const Map< Var, Range > &input_iters, const PrimExpr &predicate, IterMapLevel check_level, arith::Analyzer *analyzer, bool simplify_trivial_iterators=true)
Detect if indices can be written as [y_0 + c_0, y_1 + c_1, ..., y_n + c_n].
IterSumExpr NormalizeToIterSum(PrimExpr index, const Map< Var, Range > &input_iters, arith::Analyzer *analyzer)
Rewrite index as IterSumExpr.
Map< Var, PrimExpr > InverseAffineIterMap(const Array< IterSumExpr > &iter_map, const Array< PrimExpr > outputs)
Apply the inverse of the affine transformation to the outputs.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Variables in the TIR.