Skip to content

Extend device data node binding API to not clone specified input tensors #9054

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

Conversation

rpsilva-aws
Copy link
Collaborator

@rpsilva-aws rpsilva-aws commented Apr 29, 2025

In this PR, we extend the _get_tensors_xla_device_data_node binding API to return the same tensor values for a given set of specified unmutated tensor inputs. It currently returns a list of tensors that capture the XLATensor values of the graph inputs. However, these tensors end up creating new ATen tensors, which are effectively clones of the original tensors.

torch_xla::XLATensor::Create(backend_data));

This makes it so that these are not eligible to be aliased at the step barrier. Instead, we extend the API to allow users to specify the list of known input tensors, such that if graph input matches one of those inputs, then the same ATen tensor is returned back to the user as instead of returning a clone.

>>> t4 = torch.tensor(50).to(device)
>>> t5 = t4 + 10
>>> t6 = t5 * 20
>>> [torch_xla._XLAC._xla_get_tensor_id(x) for x in [t4, t5, t6]]
[8, 10, 12]
>>> results_with_input = torch_xla._XLAC._get_tensors_xla_device_data_node([t4, t5, t6], [t4])
>>> [torch_xla._XLAC._xla_get_tensor_id(x) for x in results_with_input[1]]
[8, 18, 19]
>>> results_without_input = torch_xla._XLAC._get_tensors_xla_device_data_node([t4, t5, t6], [])
>>> [torch_xla._XLAC._xla_get_tensor_id(x) for x in results_without_input[1]]
[20, 21, 22]

The input tensors are kept as optional, ensuring that the API change is backwards compatible.

cc: @mcuiaws

@rpsilva-aws rpsilva-aws marked this pull request as ready for review April 29, 2025 17:04
@rpsilva-aws rpsilva-aws changed the title Extend device data node binding to not override tensor IDs Extend device data node binding API to not clone specified input tensors Apr 29, 2025
@rpsilva-aws rpsilva-aws requested review from tengyifei and qihqi April 29, 2025 23:29
@rpsilva-aws
Copy link
Collaborator Author

@tengyifei @qihqi PTAL, as this will help us move away from explicit aliasings for the gradient accumulation (needed for 2.7.1), and with some internal nightly experimental workstreams.

}
m.def(
"_get_tensors_xla_device_data_node",
[](const std::vector<at::Tensor>& output_tensors,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the argument order swapped? Should input_tensors come first?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants