Skip to content
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

[Autodiff] Optimize and eliminate the Jacobian tensor for te.autodiff #6078

Merged
merged 25 commits into from
Aug 18, 2020

Conversation

yzhliu
Copy link
Member

@yzhliu yzhliu commented Jul 16, 2020

Co-authored-by: Sergei Grechanik sergei.grechanik+h@gmail.com

This is the PR that aim to remove the intermediate large Jacobian tensor in te.autodiff, as well as to do some optimizations such as inline. We have made a series of PRs for https://discuss.tvm.ai/t/rfc-bring-in-tensor-expression-autodiff and this will be the last one.

@sgrechanik-h @MarisaKirisame @junrushao1994 @xqdan @tqchen Please take a look.

src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
include/tvm/arith/int_solver.h Outdated Show resolved Hide resolved
@MarisaKirisame
Copy link
Contributor

I have read the paper and roughly understand how it work. will continue reviewing shortly after.

src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved

// For Div we simply use the condition of the numerator.

if (nz_a.value.same_as(op->a)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor this into a function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean provide a function bool value_equals(const PrimExpr&) in NonzeroConditionResult?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ReuseNZ(nz_a, op);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the only place to use the function, do we really need to create a new function? correct me if I misunderstood.

return NonzeroConditionFunctor().NonzeroCondition(expr);
}

struct FactorOutAtomicFormulasResult {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps you should store them in conjunctive normal form.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean to change the struct & member name to mention CNF?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your approach is pure conjunction. I was suggesting maybe a disjunction of conjunction will allow more optimization ability. it is fine if you dont do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I'll keep in mind and maybe change it later.

src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
return NonzeroConditionFunctor().NonzeroCondition(expr);
}

struct FactorOutAtomicFormulasResult {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your approach is pure conjunction. I was suggesting maybe a disjunction of conjunction will allow more optimization ability. it is fine if you dont do it.

src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
src/te/autodiff/ad_simplify.cc Outdated Show resolved Hide resolved
@yzhliu
Copy link
Member Author

yzhliu commented Aug 7, 2020

@MarisaKirisame @sergei-grechanik @tqchen I addressed most of the comments, please take a look again.

Copy link
Contributor

@sergei-grechanik sergei-grechanik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @yzhliu, looks good to me. My only concern is that without performance testing the quality of the generated code may deteriorate with subsequent changes of the simplifiers, but that problem probably shouldn't be addressed in this PR.

@yzhliu
Copy link
Member Author

yzhliu commented Aug 10, 2020

@sergei-grechanik I agree. Perhaps we need performance integration test run periodically as mentioned in https://discuss.tvm.ai/t/efforts-on-benchmarking-for-tvm/

@tqchen could you also take a look?

* \param other Map to be merged.
* @return The merged Array. Original Map is kept unchanged.
*/
Map<K, V> Merge(const Map<K, V>& other) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we make it as a global function instead of member? so it is not ambiguitous (that the result is a new map)

* \param other Array to be concatenated.
* @return The concatenated Array. Original Array is kept unchanged.
*/
Array<T> Concat(const Array<T>& other) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider make it as a global function, which also enables copy on write on the lhs(this)

@yzhliu
Copy link
Member Author

yzhliu commented Aug 12, 2020

@tqchen I make them static now.

* \param lhs second Array to be concatenated.
* \return The concatenated Array. Original Arrays are kept unchanged.
*/
static Array<T> Concat(Array<T> lhs, const Array<T>& rhs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it a global template fucntion, so you can do Concat(lhs, rhs) without having to refer to the original type. Note that the type signature should be able to only run the function for the specific type so we won't risk over generalization

template <typename K, typename V,
typename = typename std::enable_if<std::is_base_of<ObjectRef, K>::value>::type,
typename = typename std::enable_if<std::is_base_of<ObjectRef, V>::value>::type>
static Map<K, V> Merge(Map<K, V> lhs, const Map<K, V>& rhs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static->inline as it is in the header

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tqchen my bad I forgot to when copy-pasting. modified.

@yzhliu
Copy link
Member Author

yzhliu commented Aug 14, 2020

@tqchen it's ready

@yzhliu
Copy link
Member Author

yzhliu commented Aug 18, 2020

@tqchen kindly ping.

@tqchen tqchen merged commit ff2a76f into apache:master Aug 18, 2020
@tqchen
Copy link
Member

tqchen commented Aug 18, 2020

Thanks @yzhliu @sergei-grechanik @MarisaKirisame !

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…apache#6078)

* [Autodiff] Optimize and eliminate the Jacobian tensor for te.autodiff

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>

* fix lint

* fix clang-format

* add comments and magic number

* clang-lint

* address some comments

* remove FreeVarsVisitor

* fix constexpr lint

* fix lint

* fix lint

* add Map.Merge

* lint

* change Array::Concat & Map::Merge to global functions

* fix lint

* move functions to global

* static -> inline

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…apache#6078)

* [Autodiff] Optimize and eliminate the Jacobian tensor for te.autodiff

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>

* fix lint

* fix clang-format

* add comments and magic number

* clang-lint

* address some comments

* remove FreeVarsVisitor

* fix constexpr lint

* fix lint

* fix lint

* add Map.Merge

* lint

* change Array::Concat & Map::Merge to global functions

* fix lint

* move functions to global

* static -> inline

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…apache#6078)

* [Autodiff] Optimize and eliminate the Jacobian tensor for te.autodiff

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>

* fix lint

* fix clang-format

* add comments and magic number

* clang-lint

* address some comments

* remove FreeVarsVisitor

* fix constexpr lint

* fix lint

* fix lint

* add Map.Merge

* lint

* change Array::Concat & Map::Merge to global functions

* fix lint

* move functions to global

* static -> inline

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Sep 2, 2020
…apache#6078)

* [Autodiff] Optimize and eliminate the Jacobian tensor for te.autodiff

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>

* fix lint

* fix clang-format

* add comments and magic number

* clang-lint

* address some comments

* remove FreeVarsVisitor

* fix constexpr lint

* fix lint

* fix lint

* add Map.Merge

* lint

* change Array::Concat & Map::Merge to global functions

* fix lint

* move functions to global

* static -> inline

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Sep 3, 2020
…apache#6078)

* [Autodiff] Optimize and eliminate the Jacobian tensor for te.autodiff

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>

* fix lint

* fix clang-format

* add comments and magic number

* clang-lint

* address some comments

* remove FreeVarsVisitor

* fix constexpr lint

* fix lint

* fix lint

* add Map.Merge

* lint

* change Array::Concat & Map::Merge to global functions

* fix lint

* move functions to global

* static -> inline

Co-authored-by: Sergei Grechanik <sergei.grechanik+h@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants