-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[Bug] Fix infinite loop when exponent of integer pow is negative #5275
Conversation
✅ Deploy Preview for docsite-preview ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
After several attempts to deal with the issue of losing accuracy, I decide to change from converting all pow to return floats to throwing exceptions when pow receives negative exponents when lhs is an integer (a la torch). |
Emm.. Why cannot we distinguish pow with floats from those with ints? IMHO we only need to change the return type without changing the input type. For example, we produce |
Yes, this is what we are trying to do. For Vulkan backend, if we cast everything to float, we couldn't tell whether we need to add the bias for rounding during codegen because it looks exactly like an ordinary floating point pow to the codegen. Another thing we could try: instead of leveraging implicit type conversion, we pass the result to a round operator when both lhs and rhs are integer types (but |
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.
The API doc needs to be updated :-) FYI
taichi/python/taichi/lang/ops.py
Lines 731 to 760 in 0dfd166
@binary | |
def pow(x, a): # pylint: disable=W0622 | |
"""First array elements raised to powers from second array :math:`x^a`, element-wise. | |
Negative values raised to a non-integral value will return `nan`. | |
A zero value raised to a negative value will return `inf`. | |
Args: | |
x (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ | |
The bases. | |
a (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ | |
The exponents. | |
Returns: | |
The bases in `x1` raised to the exponents in `x2`. This is a scalar if both \ | |
`x1` and `x2` are scalars. | |
Example:: | |
>>> @ti.kernel | |
>>> def test(): | |
>>> x = ti.Matrix([-2.0, 0.0, 2.0]) | |
>>> y = -2.2 | |
>>> z = ti.pow(x, y) | |
>>> print(z) | |
>>> | |
>>> test() | |
[-nan, inf, 0.217638] | |
""" | |
return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, x, a) |
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!
Related issue = fix #4661