BUG: torch.asarray device propagation#299
Merged
lucascolley merged 1 commit intodata-apis:mainfrom Apr 4, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR implements a workaround for a known PyTorch issue with torch.asarray device propagation and improves device handling across several functions. The changes include:
- Updating imports and all ordering in torch/_typing.py to source the Device type directly from torch.
- Revising type annotations and docstrings across torch, numpy, dask, and cupy _info.py files to reflect Device rather than a string.
- Introducing an asarray helper in torch/_aliases.py and removing redundant explicit conversion calls in prod, sum, any, and all.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| array_api_compat/torch/_typing.py | Adjusted import order and all for device type propagation. |
| array_api_compat/torch/_info.py | Updated docstrings for default_device and default_dtypes to use Device. |
| array_api_compat/torch/_aliases.py | Added asarray helper and removed explicit torch.asarray conversions in several functions. |
| array_api_compat/numpy/_info.py | Updated device type annotation in function docstrings. |
| array_api_compat/dask/array/_info.py | Updated device type annotation in function docstrings. |
| array_api_compat/cupy/_info.py | Updated device type annotation and added Notes for default_device. |
| array_api_compat/common/_aliases.py | Updated a comment on creation functions to reflect support for Dask. |
Comments suppressed due to low confidence (4)
array_api_compat/torch/_aliases.py:283
- In function 'prod', the explicit conversion to a tensor was removed. Ensure that 'x' is already a torch.Tensor or add conversion logic to prevent potential runtime errors.
- x = torch.asarray(x)
array_api_compat/torch/_aliases.py:319
- In function 'sum', the removal of the torch.asarray conversion may lead to errors if 'x' is not already a tensor. Consider validating or converting 'x' as needed.
- x = torch.asarray(x)
array_api_compat/torch/_aliases.py:349
- In function 'any', the removal of the torch.asarray conversion assumes that 'x' is already a tensor. Verify that this holds for all call sites to prevent possible runtime issues.
- x = torch.asarray(x)
array_api_compat/torch/_aliases.py:374
- In function 'all', ensure that removing the explicit torch.asarray conversion will not cause issues when 'x' is not a tensor. It may be necessary to handle non-tensor inputs safely.
- x = torch.asarray(x)
torch.asarray device propagationtorch.asarray device propagation
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.
asarray: device does not propagate from input to output afterset_default_devicepytorch/pytorch#150199prod,sum,anyandall, which had the same issue induced by array-api-compat