-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Arith] linear system and equation solver #5171
Conversation
NOTE: please try to rewrite to directly use the auxiliary class arith::Analyzer instead of the Simplify function, so that the temp data structure can be created only once |
@tqchen changed to use arith::Analyzer |
include/tvm/arith/linear_system.h
Outdated
|
||
/*! | ||
* \brief Represent a linear system including variables, their ranges and | ||
* the linear relations between them (either equations or inequalities) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth mentioning that the system is integer.
Also I'm not sure if LinearSystem is a good name, because it may be useful to have non-linear (in)equalities in relations
(which doesn't break the algorithms that work on the linear part).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good suggestion. how about Formulas, Constraints, RelationSet, IntRelationSet, or InequalitySet (if we make term "inequality" to also include equations)? @sergei-grechanik
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntegerSet or IntegerSystem maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good. There's IntSet in tvm, so I'll do IntegerSystem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think it a bit more, should we use a more general name? as in solver, we will check the relation and ignore the non-integer equations, which implies it is valid to contain non-integer relations inside. @sergei-grechanik
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If variables are integer, I think it's still ok to call it IntegerSystem. But if we allow non-integer variables (I doubt it is a good idea), then I don't know, Set and System sound too unspecific, Constraints may be ok.
About |
@sergei-grechanik do you have example that we have to end with rewrite? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, although i have essentially zero expertise in numerical algorithm, so I didnt really understand what is going on.
src/arith/linear_system.cc
Outdated
node->variables = std::move(variables); | ||
node->ranges = std::move(ranges); | ||
node->relations = std::move(relations); | ||
data_ = std::move(node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is data_?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's ObjectRef->data_ whose type is ObjectPtr
cc @Hzfengsy @spectrometerHBH @merrymercy please also take a look if you have time, should be relevant to your areas |
@yzhliu Unfortunately I don't have an example. It's written in my notes that "we should end with rw because it factors multipliers out". If it's only that, it's probably not that important. |
|
||
// Transform formulas into rows of the matrix | ||
// S_{mxn} V^{-1}_{nxn} x_{nx1} = U y, in which n is # of variables | ||
// here we initialize S_{mxn} to be A, U to be identity matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
V to be identity matrix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
V was described above. here what I mean is
U^{-1} S V^{-1} x = y = [coefs] => S V^{-1} x = U [coefs]
which implies U is identity.
c58057b
to
c2678f8
Compare
@sergei-grechanik I rename it to IntConstraints, and also add checker to ensure the variables to be integers. please take a review again. |
* \param V an identity matrix, it will be modified to V_{nxn} | ||
* \param x the x in A x = y. it will be modified to V^{-1}_{nxn} x_{nx1} | ||
* \param y the y in A x = y. it will be modified to U_{mxm} y_{mx1} | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this and came to conclusion that computing the proper Smith normal form may improve stability of automatic differentiation (I mean generating the same gradient code for slightly different but equivalent input code). However, I'm not sure if it's worth it (and not in this PR of course).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I'll put a todo here.
3f7424e
to
0010a64
Compare
Co-authored-by: Sergei Grechanik <[email protected]>
Co-authored-by: Sergei Grechanik <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, looks good to me
* [arith] linear system and equation solver Co-authored-by: Sergei Grechanik <[email protected]> * avoid constructing analyzer every time * generate random test cases and address comments Co-authored-by: Sergei Grechanik <[email protected]> * rename linear_system to int_constraints * add comments and use random seed * message for reporting failure with seed * add SEqualReduce to IntConstraints; allow variables & ranges to be None Co-authored-by: Sergei Grechanik <[email protected]> Co-authored-by: Sergei Grechanik <[email protected]>
* [arith] linear system and equation solver Co-authored-by: Sergei Grechanik <[email protected]> * avoid constructing analyzer every time * generate random test cases and address comments Co-authored-by: Sergei Grechanik <[email protected]> * rename linear_system to int_constraints * add comments and use random seed * message for reporting failure with seed * add SEqualReduce to IntConstraints; allow variables & ranges to be None Co-authored-by: Sergei Grechanik <[email protected]> Co-authored-by: Sergei Grechanik <[email protected]>
* [arith] linear system and equation solver Co-authored-by: Sergei Grechanik <[email protected]> * avoid constructing analyzer every time * generate random test cases and address comments Co-authored-by: Sergei Grechanik <[email protected]> * rename linear_system to int_constraints * add comments and use random seed * message for reporting failure with seed * add SEqualReduce to IntConstraints; allow variables & ranges to be None Co-authored-by: Sergei Grechanik <[email protected]> Co-authored-by: Sergei Grechanik <[email protected]>
…m_data:master to master * commit 'cd0d52daa6942bdafa9363ff6cfa3d25fcd5b8d6': (824 commits) [Intrinsic] Add log1p, ldexp, atan2, hypot, nextafter, copysign (apache#5312) [Rust][CI] Restore Rust CI (apache#5137) Remove PrimExpr from String (apache#5311) [Requantize] Cleanup and Optimize Lowering (apache#5286) [IR][TRANSFORM] Enable CopyOnWrite for passes. (apache#5309) [PYTORCH]Abs, Arange, Softplus ops (apache#5295) [LLVM] Fix generation of LLVM intrinsics (apache#5282) [BYOC] Add example of Composite + Annotate for DNNL fused op (apache#5272) [Frontend][TensorFlow]Improve TensorFlow Static Shape Tensor Array (apache#5243) [RUNTIME] Introduce RValue reference(move) support to TypedPackedFunc (apache#5271) [RELAY][FRONTEND][CAFFE2] add Mul and ConvTranspose operator (apache#5302) [BYOC] Refine AnnotateTarget and MergeCompilerRegion Passes (apache#5277) [CI] Fix the hexagon string (apache#5304) [Arith] linear system and equation solver (apache#5171) [PYTORCH]Repeat, Reciprocal & Reshape Op support (apache#5280) [FRONTEND][TENSORFLOW] Fix gather_nd indices (apache#5279) Update device_annotation.cc (apache#5291) [REFACTOR][IR] Move to runtime::String (apache#5276) [NDArray] Set shape_ in NDArray::FromDLPack (apache#5301) [RUNTIME] Initial implementation of Hexagon runtime support (apache#5252) ...
Co-authored-by: Sergei Grechanik [email protected]
Linear system data structures and linear equations solver (via Smith Normal Form)
this is part of tensor level autodiff. will be used by #5121
Originally implemented by @sgrechanik-h in #2634 I modified a bit to make it more mathematical. @sgrechanik-h please help to check if the math is correct and whether
SuperSimplify
is required (I usetir::Simplify
instead in this PR, I think it is only required in condition lift)@sgrechanik-h @tqchen @MarisaKirisame @junrushao1994 @hzfan Please review