Skip to content

Commit

Permalink
Propagates requires_grad over to AllReduce output (#6326)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeounoh authored and bhavya01 committed Apr 22, 2024
1 parent 1471e14 commit e954c42
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
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

0 comments on commit e954c42

Please sign in to comment.