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

[Bug] Fix infinite loop when exponent of integer pow is negative #5275

Merged
merged 21 commits into from
Jul 14, 2022

Conversation

AD1024
Copy link
Contributor

@AD1024 AD1024 commented Jun 27, 2022

Related issue = fix #4661

@netlify
Copy link

netlify bot commented Jun 27, 2022

Deploy Preview for docsite-preview ready!

Name Link
🔨 Latest commit 621236a
🔍 Latest deploy log https://app.netlify.com/sites/docsite-preview/deploys/62cf9e7acfd38800097d97d9
😎 Deploy Preview https://deploy-preview-5275--docsite-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@AD1024 AD1024 changed the title [bug] Fix infinite loop when exponent of integer pow is negative [Bug] Fix infinite loop when exponent of integer pow is negative Jun 28, 2022
@AD1024 AD1024 marked this pull request as ready for review June 28, 2022 22:22
@AD1024 AD1024 marked this pull request as draft June 29, 2022 01:21
@AD1024
Copy link
Contributor Author

AD1024 commented Jul 4, 2022

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).
For some backends such as Vulkan (correct me if I'm wrong), we could not tell from pow with floats from those with ints if we make all pows to return floats. However, those backends depend on this information to decide whether to insert precision correction (e.g. Adding biases). Thus throwing exception is probably the 'right' way to fix this issue.

@strongoier
Copy link
Contributor

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 round(pow(f32(x), f32(y))) when x and y are i32s.

@AD1024
Copy link
Contributor Author

AD1024 commented Jul 5, 2022

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 round(pow(f32(x), f32(y))) when x and y are i32s.

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 round doesn't work for negative exponents).

@AD1024 AD1024 marked this pull request as ready for review July 6, 2022 18:01
Copy link
Contributor

@strongoier strongoier left a 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

@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)

Copy link
Contributor

@strongoier strongoier left a comment

Choose a reason for hiding this comment

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

LGTM!

@strongoier strongoier merged commit 9e4c36f into taichi-dev:master Jul 14, 2022
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.

Infinite Loop with Exponentiation
3 participants