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] Operations on uint8 don't respect upcasting rules #649

Closed
s-ol opened this issue Dec 8, 2023 · 1 comment · Fixed by #650
Closed

[BUG] Operations on uint8 don't respect upcasting rules #649

s-ol opened this issue Dec 8, 2023 · 1 comment · Fixed by #650
Labels
bug Something isn't working

Comments

@s-ol
Copy link
Contributor

s-ol commented Dec 8, 2023

The documentation clearly states:

ulab observes the following upcasting rules:

  1. Operations on two ndarrays of the same dtype preserve their dtype, even when the results overflow.
  2. ...

However the implementation does not respect this rule for uint8 types, which are upcast to uint16 regardless:

To Reproduce

>>> import ulab.numpy as np
>>> x = np.array([1, 2], dtype=np.uint8)
>>> x + x
array([2, 4], dtype=uint16)

Expected behavior
The result should have a uint8 dtype.

Additional context
Since this problem only regards the uint8 dtype and seems to be implemented rather consistently in this way it seems likely that this was an intentional choice at some point. However it is completely undocumented and I don't understand the rationale behind it.

The current behaviour means if the user wants to remain in "uint8-space", that is not possible at all, even using explicit casting. If the implementation were changed to match the docs however, users could explicitly cast one argument to uint16 to obtain the wanted result if needed, without breaking the other use case.

This is related to but separate from #396, which was partially solved by changing the 'downcasting' implementation for scalar arguments in #398. However adding a small integer constant to an u8 array still results in an u16 result due to this upcasting exception.

@s-ol s-ol added the bug Something isn't working label Dec 8, 2023
@s-ol
Copy link
Contributor Author

s-ol commented Dec 8, 2023

Note that numpy implements upcasting as per current ulab docs as well:

>>> import numpy as np
>>> np.array([1,2,3], dtype=np.uint8)
array([1, 2, 3], dtype=uint8)
>>> x = np.array([1,2,3], dtype=np.uint8)
>>> x + x
array([2, 4, 6], dtype=uint8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant