Skip to content

Commit

Permalink
Add data-type promotion to stack. (#7091)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysiraichi authored May 23, 2024
1 parent 7350b70 commit a299f33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions test/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,22 @@ def foo(grad, inp):

self.assertEqual(r, Xr.cpu())

def test_stack_different_types(self):

def foo(t0, t1):
return torch.stack([t0, t1])

t0 = torch.rand(10, 10, dtype=torch.bfloat16)
t1 = torch.rand(10, 10)

Xt0 = t0.to(xm.xla_device())
Xt1 = t1.to(xm.xla_device())

r = foo(t0, t1)
Xr = foo(Xt0, Xt1)

self.assertEqual(r, Xr.cpu())


class MNISTComparator(nn.Module):

Expand Down
6 changes: 5 additions & 1 deletion torch_xla/csrc/aten_xla_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3160,8 +3160,12 @@ at::Tensor XLANativeFunctions::squeeze_copy(const at::Tensor& self,

at::Tensor XLANativeFunctions::stack(at::TensorList tensors, int64_t dim) {
TORCH_LAZY_FN_COUNTER_TIMED_TRACING("xla::");
at::ScalarType result_type = at::native::result_type(tensors);
std::vector<at::Tensor> c_tensors(tensors.size());
std::transform(tensors.begin(), tensors.end(), c_tensors.begin(),
[=](const at::Tensor& t) { return t.to(result_type); });
return bridge::AtenFromXlaTensor(
tensor_methods::stack(bridge::GetXlaTensors(tensors), dim));
tensor_methods::stack(bridge::GetXlaTensors(c_tensors), dim));
}

at::Tensor XLANativeFunctions::std(const at::Tensor& self, bool unbiased) {
Expand Down

0 comments on commit a299f33

Please sign in to comment.