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

Fix inconsistent python/cpp API behavior for if_then_else, power #3829

Merged
merged 6 commits into from
Aug 26, 2019

Conversation

sxjscience
Copy link
Member

@sxjscience sxjscience commented Aug 24, 2019

In the C++ side, the if_then_else will call BinaryOpMatchTypes, which will try to convert the dtypes of the inner expressions to be the same:
https://github.com/dmlc/tvm/blob/d05893b3aaddb098780a2fbaf2d2af81bb50d393/src/lang/expr_operator.cc#L242

However, in the python side, there is no such conversion, which makes the behavior inconsistent.
The same issue occurs for power. Thus, fix by directly registering the APIs inside _make.

To give an example of the inconsistent behavior, consider the following example:

import tvm
a = tvm.var('a', dtype='float32')
b = tvm.var('b', dtype='int32')
c = tvm.if_then_else(a > 0, a, b)
print(c)

In the original version, the result will be

tvm_if_then_else((a > 0f), a, b)

If we use the C++ API, it will be the following, in which b will be automatically casted to float32.

tvm_if_then_else((a > 0f), a, float32(b))

@sxjscience
Copy link
Member Author

@kevinthesun @icemelon9

@@ -419,7 +419,7 @@ def power(x, y):
z : Expr
The result.
"""
return call_pure_intrin(x.dtype, "pow", x, y)
return _make._OpPow(convert(x), convert(y))
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a test case to check the type conversion.

if cond.dtype != "bool":
raise TypeError("The condition's data type has to be bool")
return call_pure_intrin(t.dtype, "tvm_if_then_else", cond, t, f)
return _make._OpIfThenElse(convert(cond), convert(t), convert(f))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@yzhliu yzhliu added the status: need update need update based on feedbacks label Aug 25, 2019
@sxjscience
Copy link
Member Author

@kevinthesun I've added the checks.

@sxjscience
Copy link
Member Author

Also, I find that the behavior of BinaryOpMatchTypes is not the same as numpy.

For example:

import numpy as np
import tvm
print((tvm.var(dtype='int32') + tvm.var(dtype='float16')).dtype)
# result: float16
print((tvm.var(dtype='int32') + tvm.var(dtype='float32')).dtype)
# result: float32
print((np.array(1).astype('int32') + np.array(1).astype('float32')).dtype)
# result: float64
print((np.array(1).astype('int32') + np.array(1).astype('float16')).dtype)
# result: float64

Seems that numpy promotes the float to the highest precision whenever it meets the integer + float.

@kevinthesun kevinthesun merged commit 283b0c3 into apache:master Aug 26, 2019
@kevinthesun
Copy link
Contributor

Thanks!

@sxjscience sxjscience deleted the if_then_else_consistent branch August 26, 2019 18:45
wweic pushed a commit to wweic/tvm that referenced this pull request Sep 16, 2019
…che#3829)

* fix inconsistent python/cpp APIs for if_then_else

* fix error message

* fix power consistency

* fix

* fix bug

* add test
wweic pushed a commit to wweic/tvm that referenced this pull request Sep 16, 2019
…che#3829)

* fix inconsistent python/cpp APIs for if_then_else

* fix error message

* fix power consistency

* fix

* fix bug

* add test
wweic pushed a commit to neo-ai/tvm that referenced this pull request Sep 16, 2019
…che#3829)

* fix inconsistent python/cpp APIs for if_then_else

* fix error message

* fix power consistency

* fix

* fix bug

* add test
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