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

xm.save() should not set sync_xla_data=True when sync'ing. #8484

Merged
merged 1 commit into from
Dec 17, 2024
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
23 changes: 23 additions & 0 deletions test/test_input_output_aliases.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys

import torch
Expand Down Expand Up @@ -162,6 +163,28 @@ def test_separate_graphs(self):

self.assertEqual(t1.item(), 3)

def test_xm_save_no_aliasing(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this test is added in https://github.com/pytorch/xla/pull/8467/files already

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rebased.

"""
Test that xm.save() does not perform aliasing.
"""
xla_device = xm.xla_device()
t0 = torch.tensor([1], device=xla_device)
t1 = torch.tensor([2], device=xla_device)
xm.mark_step()

t2 = t0 + t1
t1.add_(1)

# Save the new value of t1 should not result in the old value
# being donated...
xm.save(t1, os.devnull)

# otherwise this mark_step could crash, or compute the wrong value
# for t2.
xm.mark_step()

self.assertEqual(t2.item(), 3)


if __name__ == '__main__':
test = unittest.main()
Expand Down
2 changes: 1 addition & 1 deletion torch_xla/core/xla_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ def _maybe_convert_to_cpu(data: Any, convert: bool = True) -> ToXlaTensorArena:

def convert_fn(tensors):
torch_xla._XLAC._xla_sync_multi(
tensors, devices=[], wait=True, sync_xla_data=True)
tensors, devices=[], wait=True, sync_xla_data=False)
if not convert:
return tensors
return torch_xla._XLAC._xla_get_cpu_tensors(tensors)
Expand Down
2 changes: 1 addition & 1 deletion torch_xla/utils/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _rewrite_data(path, data, save_tensors):

def convert_fn(tensors):
torch_xla._XLAC._xla_sync_multi(
tensors, devices=[], wait=True, sync_xla_data=True)
tensors, devices=[], wait=True, sync_xla_data=False)
rewritten_tensors = []
for i, t in enumerate(tensors):
if save_tensors:
Expand Down
Loading