Skip to content

enable tensor.copy_ with torch native cpu tensor as source #8682

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions torchax/test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def test_flatten(self):
a = a.flatten(0, 1)
self.assertEqual(tuple(a.shape), (6, 4))

def test_copy_(self):
with self.env:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is because self.env.config.use_torch_native_for_cpu_tensor = False, then a is a jax tensor.

But I use a new env for test_copy_, still doesn't work. @qihqi

a = torch.zeros((2, 3), device="cpu")
b = torch.ones((2, 3))
b.copy_(a)
self.assertTrue(torch.allclose(a, b.cpu()))

def test_rnn(self):
model = SeqModel()
x = torch.randn((2, 100, 20))
Expand Down
2 changes: 2 additions & 0 deletions torchax/torchax/ops/jaten.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def _aten_add(x, y, *, alpha=1):

@op(torch.ops.aten.copy_, is_jax_function=False)
def _aten_copy(x, y, memory_format=None):
if y.device.type == "cpu":
y = y.to(x.device)
if x.ndim == 1 and y.ndim == 0:
# case of torch.empty((1,)).copy_(tensor(N))
# we need to return 0D tensor([N]) and not scalar tensor(N)
Expand Down
Loading