Repository navigation
BUG: torch: accept Python scalars in binary elementwise functions - #478
Open
raashish1601 wants to merge 1 commit into
Open
raashish1601 wants to merge 1 commit into
raashish1601 wants to merge 1 commit into
Conversation
torch.maximum, minimum, atan2, hypot, logaddexp, nextafter and the logical functions reject Python scalars, and the comparisons and copysign reject a scalar as the first argument. Convert the scalar to a 0-D tensor of the result dtype in these positions. Closes data-apis#271
This branch has not been deployed
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.
Closes #271 (also covers #410).
torch.maximum,minimum,atan2,hypot,logaddexp,nextafterandlogical_and/or/xorreject Python scalars, andequal,not_equal,less,less_equal,greater,greater_equalandcopysignreject a scalar as the first argument. The 2024.12 standard allows a scalar in either position.Following the idea in the issue,
_two_argnow takesscalar_x1/scalar_x2flags. When set, a Python scalar in that position is converted to a 0-D tensor withresult_type(scalar, other)on the other argument's device. The flags are only set where torch rejects the scalar, so calls that already work (e.g.add(x, 1)) take the same path as before.nextafterand the logical functions are now wrapped too and added to__all__.Testing: removed the matching
test_binary_with_scalars_*entries fromtorch-xfails.txt; those 28 tests pass locally against array-api-tests, along with the rest oftest_operators_and_elementwise_functions.pyandtest_signatures.py. Added tests totests/test_torch.py; they fail on main and pass with this change.ruff check .passes.