Lightning-AI / Lightning-AI/lightning-thunder
behavior to `torch.tensor` and `torch.Tensor` looks unclear
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 121
- PR merge metrics
- No merged PRs in 30d
Description
*Note*: If you have a model or program that is not supported yet but should be, please use the program coverage template.
## 🐛 Bug
If a callable uses `torch.tensor` or `torch.Tensor` (while the latter is not that recommended as a tensor constructor, https://pytorch.org/docs/stable/tensors.html#torch-tensor), Thunder's interpreter does not seem to cope with them. `examine` seems to show some clumsiness.
### To Reproduce
Run the snippet attached below.
```
### h=
/home/mkozuki/ghq/github.com/Lightning-AI/lightning-thunder/d.py:9: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
return torch.tensor(h)
Failed to run the unmodified function. Please verify that your code runs without thunder
The code failed with exception - sin(): argument 'input' (position 1) must be Tensor, not tuple
name 'n' is not defined
### h=
Failed to run the unmodified function. Please verify that your code runs without thunder
The code failed with exception - sin(): argument 'input' (position 1) must be Tensor, not tuple
len() of a 0-d tensor
```
#### Code sample
```python
import torch
import thunder
from thunder.examine import examine
def f(x):
h = torch.sin(x)
return torch.tensor(h)
def g(x):
h = torch.sin(x)
return torch.Tensor(h)
for h in (f, g):
print(f"### {h=}")
x = torch.randn((2, 2), device="cuda", requires_grad=True)
y = h(x)
x = torch.randn((2, 2), device="cuda", requires_grad=True)
try:
examine(h, (x,))
jitted = thunder.jit(h)
y = jitted(x)
except Exception as e:
print(e)
```
### Expected behavior
Clear error, or handle them
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the provided Python snippet, focusing on examine(h, (x,)) and thunder.jit(h) for functions using torch.tensor and torch.Tensor. Start by tracing how the interpreter handles these constructors; done means both cases are either supported correctly or produce a clear, accurate error instead of the reported failures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100