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

[ARITH] Simplify casts of constants 0 and 1 #3758

Merged
merged 4 commits into from
Aug 13, 2019

Conversation

sgrechanik-h
Copy link
Contributor

This PR implements simplification of some casts of constants in the rewrite simplifier. The use case I have in mind is conversion from boolean expressions to floats (float32(i < j) instead of select(i < k, 1.0, 0.0)). I restricted consts to be just 0 and 1 because I'm not sure about usefulness and safety of other values (0 and 1 should be safe for all types, except maybe for some custom types).

This PR also introduces the is_const_value functions which is like is_const_int, but works for float expressions too. Besides this PR, it is also used in the tensor expression autodiff implementation.

return false;
}
}

inline bool is_const_int(const Expr& x, int64_t value) {
Copy link
Member

Choose a reason for hiding this comment

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

keep this impl, as the is_const_value involves recursions and could be more complicated than this one.

@@ -1757,6 +1757,20 @@ Mutate_(const Variable* op, const Expr& self) {
return self;
}

Expr RewriteSimplifier::Impl::
Mutate_(const Cast* op, const Expr& self) {
Copy link
Member

Choose a reason for hiding this comment

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

Let us just add cast between (uint/float/int here), ignore vector types for now as they are not as common.

@@ -551,18 +561,31 @@ inline bool is_negative_const(const Expr& a) {
}
}

template <typename ValueType>
inline bool is_const_value(const Expr& e, ValueType value) {
Copy link
Member

Choose a reason for hiding this comment

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

Let us not introduce this for now(see comments in the Mutator).

@tqchen tqchen added the status: need update need update based on feedbacks label Aug 12, 2019
}
if (is_const_value(op->value, 1)) {
return make_const(op->type, 1);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

could you support float(16) --> 16.000, int32(16.000) --> 16 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the implementation via tvm::cast cases like these should work now.

@sgrechanik-h
Copy link
Contributor Author

Turned out there is the cast function in expr_operator.cc which I overlooked. I think it can be used here, although it is much more permissive.

@tqchen tqchen merged commit bbc5d15 into apache:master Aug 13, 2019
wweic pushed a commit to neo-ai/tvm that referenced this pull request Aug 16, 2019
* [ARITH] Simplify casts of constants 0 and 1

* [EXPR] is_const_value to check whether non-ints are consts

* Revert "[EXPR] is_const_value to check whether non-ints are consts"

This reverts commit 7e1b346.

* Use tvm::cast
anijain2305 pushed a commit to anijain2305/tvm that referenced this pull request Aug 22, 2019
* [ARITH] Simplify casts of constants 0 and 1

* [EXPR] is_const_value to check whether non-ints are consts

* Revert "[EXPR] is_const_value to check whether non-ints are consts"

This reverts commit 7e1b346.

* Use tvm::cast
wweic pushed a commit to neo-ai/tvm that referenced this pull request Sep 6, 2019
* [ARITH] Simplify casts of constants 0 and 1

* [EXPR] is_const_value to check whether non-ints are consts

* Revert "[EXPR] is_const_value to check whether non-ints are consts"

This reverts commit 7e1b346.

* Use tvm::cast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: need update need update based on feedbacks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants