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

end, start and step argument of arange() work with a 0D tensor against error messages #129334

Closed
hyperkai opened this issue Jun 23, 2024 · 2 comments
Labels
low priority We're unlikely to get around to doing this in the near future module: tensor creation triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@hyperkai
Copy link

hyperkai commented Jun 23, 2024

πŸ› Describe the bug

end argument of arange() cannot have a tensor according to the error message as shown below:

import torch
                            # ↓↓↓↓↓↓↓↓↓
torch.arange(end=torch.tensor([0, 1, 2])) # Error

TypeError: arange(): argument 'end' must be Number, not Tensor

And, start argument of arange() cannot have a tensor according to the error message as shown below:

import torch
                              # ↓↓↓↓↓↓↓↓↓
torch.arange(start=torch.tensor([0, 1, 2]), end=5) # Error
TypeError: arange() received an invalid combination of arguments - got (end=int, start=Tensor, ), but expected one of:
 * (Number end, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (Number start, Number end, *, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (Number start, Number end, Number step, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

And, step argument of arange() cannot have a tensor according to the error message as shown below:

import torch
                                             # ↓↓↓↓↓↓↓↓↓
torch.arange(start=0, end=5, step=torch.tensor([0, 1, 2])) # Error
TypeError: arange() received an invalid combination of arguments - got (end=int, step=Tensor, start=int, ), but expected one of:
 * (Number end, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (Number start, Number end, *, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 * (Number start, Number end, Number step, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

But, end, start and step argument of arange() work with the 0D tensor of `an integer against the error messages as shown below:

import torch
                            # ↓
torch.arange(end=torch.tensor(2)) # tensor([0, 1])
                              # ↓
torch.arange(start=torch.tensor(2), end=5) # tensor([2, 3, 4])
                                             # ↓
torch.arange(start=0, end=5, step=torch.tensor(2)) # tensor([0, 2, 4])

In addition, , end, start and step argument of arange() work with the 0D tensor of a floating-point number, complex number or boolean value against the error messages as shown below:

import torch
                            # ↓↓
torch.arange(end=torch.tensor(2.)) # tensor([0., 1.])
                            # ↓↓↓↓↓↓
torch.arange(end=torch.tensor(2.+0.j)) # tensor([0., 1.])
                            # ↓↓↓↓
torch.arange(end=torch.tensor(True)) # tensor([0])

                              # ↓↓
torch.arange(start=torch.tensor(2.), end=5.) # tensor([2., 3., 4.])
                              # ↓↓↓↓↓↓
torch.arange(start=torch.tensor(2.+0.j), end=5.+0.j) # tensor([2., 3., 4.])
                              # ↓↓↓↓↓
torch.arange(start=torch.tensor(False), end=True) # tensor([0])

                                               # ↓↓
torch.arange(start=0., end=5., step=torch.tensor(2.)) # tensor([0., 2., 4.])
                                                       # ↓↓↓↓↓↓
torch.arange(start=0.+0.j, end=5.+0.j, step=torch.tensor(2.+0.j)) # tensor([0., 2., 4.])
                                                    # ↓↓↓↓
torch.arange(start=False, end=True, step=torch.tensor(True)) # tensor([0])

Versions

import torch

torch.__version__ # 2.3.0+cu121

cc @gchanan @mruberry

@janeyx99
Copy link
Contributor

This likely has to do with the fact that the dispatcher will reinterpret those Scalar Tensors as Scalars, which is a good feature for other operations in PyTorch. (cc @bdhirsh to confirm.)

The current behavior seems fine to me--is there an action item to pursue here?

@janeyx99 janeyx99 added triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: tensor creation labels Jun 24, 2024
@malfet malfet added the low priority We're unlikely to get around to doing this in the near future label Jun 26, 2024
@malfet
Copy link
Contributor

malfet commented Jun 26, 2024

Closing, as indeed it works as expected: ability to treat scalar tensors as numbers fits nicely with Python's duck-typing paradigm

@malfet malfet closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
low priority We're unlikely to get around to doing this in the near future module: tensor creation triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

3 participants