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

[fix] ltorch.to to return a copy, when copy=True #1775

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions thunder/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,20 @@ def torch_to(a, memory_format):
assert_close(torch_result, thunder_result, check_stride=True)


def test_torch_tensor_to_return_type():
a = torch.randn(2, 4, 5, 3)

def torch_to(a):
return a.to(copy=True)

jfoo = thunder.jit(torch_to)
thunder_result = jfoo(a)
torch_result = torch_to(a)

assert thunder_result is not a
assert_close(torch_result, thunder_result)


# TODO See issue "Add contiguous and clang.stride_order OpInfos that check stride
# consistency with PyTorch"
@instantiate(
Expand Down
7 changes: 7 additions & 0 deletions thunder/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,13 @@ def to(
copy: bool = False,
memory_format: None | torch.memory_format = None,
) -> TensorLike:

input_device = a.device
input_dtype = a.dtype
if copy and not _will_to_return_self(input_device, input_dtype, device, dtype, memory_format, copy):
b = prims.empty(a.shape, device=input_device, dtype=input_dtype)
return _copy_(b, a)

device, dtype = _parse_to_device_and_dtype(
tensor_dtype_or_device, optional_positional_dtype, device=device, dtype=dtype
)
Expand Down
Loading