tvm
Loading...
Searching...
No Matches
int_solver.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
24#ifndef TVM_ARITH_INT_SOLVER_H_
25#define TVM_ARITH_INT_SOLVER_H_
26
27#include <tvm/ir/expr.h>
28#include <tvm/ir/prim/expr.h>
29#include <tvm/tirx/op.h>
30
31#include <unordered_map>
32#include <utility>
33#include <vector>
34
35#include "analyzer.h"
36
37namespace tvm {
38namespace arith {
39
40using tirx::IterVar;
41using tirx::PrimVar;
42using tirx::Var;
43using tirx::VarNode;
44
45// According to experiments two best simplifications orders were can->rw and rw->can->rw,
46// but rw->can->rw is better for a couple of cases.
47// Also we should end with rw because it factors multipliers out.
49
59class IntGroupBoundsNode : public ffi::Object {
60 public:
62 ffi::Array<PrimExpr> lower;
63 ffi::Array<PrimExpr> equal;
64 ffi::Array<PrimExpr> upper;
65
66 static void RegisterReflection() {
67 namespace refl = tvm::ffi::reflection;
68 refl::ObjectDef<IntGroupBoundsNode>()
69 .def_ro("coef", &IntGroupBoundsNode::coef)
70 .def_ro("lower", &IntGroupBoundsNode::lower)
71 .def_ro("equal", &IntGroupBoundsNode::equal)
72 .def_ro("upper", &IntGroupBoundsNode::upper);
73 }
74
76 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IntGroupBounds", IntGroupBoundsNode, ffi::Object);
77};
78
83class IntGroupBounds : public ffi::ObjectRef {
84 public:
95 TVM_DLL IntGroupBounds(PrimExpr coef, ffi::Array<PrimExpr> lower, ffi::Array<PrimExpr> equal,
96 ffi::Array<PrimExpr> upper);
97
104
108 IntGroupBounds Substitute(const ffi::Map<Var, PrimExpr>& subst) const;
109
116 Range FindBestRange(const ffi::Map<Var, Range>& vranges_addl = {}) const;
117
124
126};
127
133class IntConstraintsNode : public ffi::Object {
134 public:
135 // e.g., \alpha, \beta, must be integers
136 ffi::Array<PrimVar> variables;
137 // e.g., 1 <= \alpha <= N, etc.
138 // it is absolutely ok to include ranges for parameters
139 // (variables that are not in this->variables) in this map
140 ffi::Map<Var, Range> ranges;
141 // linear equalities or inequalities
142 // e.g., A \alpha = \beta or A \alpha <= \beta
143 ffi::Array<PrimExpr> relations;
144
145 static void RegisterReflection() {
146 namespace refl = tvm::ffi::reflection;
147 refl::ObjectDef<IntConstraintsNode>()
148 .def_ro("variables", &IntConstraintsNode::variables)
149 .def_ro("ranges", &IntConstraintsNode::ranges)
150 .def_ro("relations", &IntConstraintsNode::relations);
151 }
152
154 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IntConstraints", IntConstraintsNode, ffi::Object);
155};
156
161class IntConstraints : public ffi::ObjectRef {
162 public:
170 TVM_DLL IntConstraints(ffi::Array<PrimVar> variables, ffi::Map<Var, Range> ranges,
171 ffi::Array<PrimExpr> relations);
172
174};
175
190class IntConstraintsTransformNode : public ffi::Object {
191 public:
194 ffi::Map<Var, PrimExpr> src_to_dst;
195 ffi::Map<Var, PrimExpr> dst_to_src;
196
197 static void RegisterReflection() {
198 namespace refl = tvm::ffi::reflection;
199 refl::ObjectDef<IntConstraintsTransformNode>()
200 .def_ro("src", &IntConstraintsTransformNode::src)
201 .def_ro("dst", &IntConstraintsTransformNode::dst)
202 .def_ro("src_to_dst", &IntConstraintsTransformNode::src_to_dst)
203 .def_ro("dst_to_src", &IntConstraintsTransformNode::dst_to_src);
204 }
205
208 ffi::Object);
209};
210
215class IntConstraintsTransform : public ffi::ObjectRef {
216 public:
228 ffi::Map<Var, PrimExpr> src_to_dst,
229 ffi::Map<Var, PrimExpr> dst_to_src);
230
239
242};
243
244typedef std::pair<ffi::Map<Var, IntGroupBounds>, ffi::Array<PrimExpr>> PartialSolvedInequalities;
245
261void SmithNormalFormDiag(std::vector<std::vector<int64_t>>* S, std::vector<std::vector<int64_t>>* V,
262 std::vector<PrimExpr>* x, std::vector<PrimExpr>* y);
263
275
295
303ffi::Array<PrimExpr> AsConditions(const ffi::Array<PrimVar>& variables,
304 const ffi::Map<Var, IntGroupBounds>& bounds,
305 const ffi::Array<PrimExpr>& relations);
306
318
336
337} // namespace arith
338} // namespace tvm
339#endif // TVM_ARITH_INT_SOLVER_H_
Algebra expression simplifications.
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Range container
Definition expr.h:610
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Represent integer constrains including (integer) variables, their ranges and the relations between th...
Definition int_solver.h:133
ffi::Array< PrimExpr > relations
Definition int_solver.h:143
static void RegisterReflection()
Definition int_solver.h:145
ffi::Map< Var, Range > ranges
Definition int_solver.h:140
ffi::Array< PrimVar > variables
Definition int_solver.h:136
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IntConstraints", IntConstraintsNode, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition int_solver.h:153
We can have different set of variables to represent the same constraints. For example,...
Definition int_solver.h:190
ffi::Map< Var, PrimExpr > dst_to_src
Definition int_solver.h:195
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition int_solver.h:206
IntConstraints src
Definition int_solver.h:192
static void RegisterReflection()
Definition int_solver.h:197
ffi::Map< Var, PrimExpr > src_to_dst
Definition int_solver.h:194
IntConstraints dst
Definition int_solver.h:193
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IntConstraintsTransform", IntConstraintsTransformNode, ffi::Object)
Managed reference to IntConstraintsTransformNode.
Definition int_solver.h:215
IntConstraintsTransform operator+(const IntConstraintsTransform &other) const
Chain-compose two IntConstraintsTransform together. this->dst must be the same as other->src.
IntConstraintsTransform(IntConstraints src, IntConstraints dst, ffi::Map< Var, PrimExpr > src_to_dst, ffi::Map< Var, PrimExpr > dst_to_src)
Constructor by fields.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IntConstraintsTransform, ffi::ObjectRef, IntConstraintsTransformNode)
Managed reference to IntConstraintsNode.
Definition int_solver.h:161
IntConstraints(ffi::Array< PrimVar > variables, ffi::Map< Var, Range > ranges, ffi::Array< PrimExpr > relations)
Constructor by fields.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IntConstraints, ffi::ObjectRef, IntConstraintsNode)
Represent integer grouped bounds which are classified into lower bounds (inclusive),...
Definition int_solver.h:59
PrimExpr coef
Definition int_solver.h:61
static void RegisterReflection()
Definition int_solver.h:66
ffi::Array< PrimExpr > equal
Definition int_solver.h:63
ffi::Array< PrimExpr > upper
Definition int_solver.h:64
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IntGroupBounds", IntGroupBoundsNode, ffi::Object)
ffi::Array< PrimExpr > lower
Definition int_solver.h:62
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition int_solver.h:75
Managed reference to IntGroupBoundsNode.
Definition int_solver.h:83
static IntGroupBounds FromRange(const Range &r)
Construct bounds from a range.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IntGroupBounds, ffi::ObjectRef, IntGroupBoundsNode)
IntGroupBounds(PrimExpr coef, ffi::Array< PrimExpr > lower, ffi::Array< PrimExpr > equal, ffi::Array< PrimExpr > upper)
Constructor by fields.
Range FindBestRange(const ffi::Map< Var, Range > &vranges_addl={}) const
Find the best range from the grouped bounds.
IntGroupBounds operator+(const Range &r)
Combine the bounds with another range.
IntGroupBounds Substitute(const ffi::Map< Var, PrimExpr > &subst) const
Perform substitution on all components of the struct.
Base expr nodes in TVM.
TIR expressions.
std::pair< ffi::Map< Var, IntGroupBounds >, ffi::Array< PrimExpr > > PartialSolvedInequalities
Definition int_solver.h:244
IntConstraints SolveInequalitiesToRange(const IntConstraints &system_to_solve)
Solve linear inequalities and infer the range of each variable.
void SmithNormalFormDiag(std::vector< std::vector< int64_t > > *S, std::vector< std::vector< int64_t > > *V, std::vector< PrimExpr > *x, std::vector< PrimExpr > *y)
Obtain Smith Normal Form of linear equation A x = y. Smith Normal Form of matrix A_{mxn} is S_{mxn} =...
constexpr int kSimplifyRewriteCanonicalRewrite
Definition int_solver.h:48
IntConstraintsTransform SolveInequalitiesDeskewRange(const IntConstraints &system_to_solve)
Solve linear inequalities and deskew the ranges towards zero.
PartialSolvedInequalities SolveLinearInequalities(const IntConstraints &system_to_solve)
Solve linear inequalities.
ffi::Array< PrimExpr > AsConditions(const ffi::Array< PrimVar > &variables, const ffi::Map< Var, IntGroupBounds > &bounds, const ffi::Array< PrimExpr > &relations)
Combine the information into an array of (in)equalities.
IntConstraintsTransform SolveLinearEquations(const IntConstraints &system_to_solve)
Solve linear equations.
tvm::Var Var
Definition var.h:38
tvm::VarNode VarNode
Definition var.h:37
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Common operators defined for Expr.