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

Propagates requires_grad over to AllReduce output #6326

Merged
merged 1 commit into from
Jan 19, 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
6 changes: 4 additions & 2 deletions test/pjrt/test_collective_ops_tpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ def _all_reduce(pin_layout):
device = xm.xla_device()
# Prevent 0 and 1 from being converted to constants
ordinal = xm.send_cpu_data_to_device(
torch.tensor(xm.get_ordinal()), device=device)
torch.tensor(xm.get_ordinal(), dtype=torch.float32, requires_grad=True),
device=device)
out = xm.all_reduce(xm.REDUCE_SUM, ordinal, pin_layout=pin_layout)[0]
assert out.requires_grad
xm.mark_step()

return out.cpu().numpy()
return out.cpu().detach().numpy()

@parameterized.named_parameters(('pinned', True), ('unpinned', False))
def test_all_reduce(self, pin_layout):
Expand Down
3 changes: 2 additions & 1 deletion torch_xla/csrc/init_python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,8 @@ void InitXlaModuleBindings(py::module m) {
NoGilSection nogil;
result = AllReduce(reduce_type, input, scale, replica_groups, pin_layout);
}
return result;
return torch::autograd::make_variable(
result, /*requires_grad=*/input.requires_grad());
});
m.def("_xla_quantize_tensor",
[](const at::Tensor& input, const std::vector<float>& scale_list,
Expand Down