MAINT: simplify torch dtype promotion#303
Merged
ev-br merged 3 commits intodata-apis:mainfrom Apr 15, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
array_api_compat/torch/_aliases.py:140
- The try/except block in _result_type silently swallows a KeyError when a dtype pair is not found in the promotion table. Consider handling the error explicitly or adding a clarifying comment to document the intended fallback behavior.
return _promotion_table[xdt, ydt]
array_api_compat/torch/_aliases.py:296
- Consider adding tests for _sum_prod_no_axis to verify that the dtype conversion and deep copy behavior work as expected, especially for edge cases involving small integer types like uint8.
return x.clone() if dtype == x.dtype else x.to(dtype)
crusaderky
commented
Apr 10, 2025
| (torch.float32, torch.complex64): torch.complex64, | ||
| (torch.float32, torch.complex128): torch.complex128, | ||
| (torch.float64, torch.complex64): torch.complex128, | ||
| (torch.float64, torch.complex128): torch.complex128, |
Contributor
Author
There was a problem hiding this comment.
(complex, float) use cases were missing
ev-br
approved these changes
Apr 15, 2025
| if x.dtype in (torch.uint8, torch.int8, torch.int16, torch.int32): | ||
| return x.to(torch.int64) | ||
|
|
||
| return x.clone() |
Member
There was a problem hiding this comment.
Note to self: this looks scary, but is in fact just a refactoring. Previously this stanza was duplicated in sum and prod.
Returning a copy looks reasonable, too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Salvaged from data-apis/array-api#298
sum(x, axis=(), dtype=x.dtype)andprodwith the same parameters, which were previously returningxitself, to return a deep copy instead. While this is not part of the Array API, it's what both numpy an base torch do, and probably the most healthy thing to do xref https://data-apis.org/array-api/latest/design_topics/copies_views_and_mutation.html.Discussion here: Spell out where views are allowed #331