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

cummax: fix 0-sized dimension reduction. #8653

Merged
merged 3 commits into from
Feb 3, 2025
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
14 changes: 14 additions & 0 deletions test/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,20 @@ def foo(x: torch.Tensor) -> torch.Tensor:
self.assertEqual(out.dtype, out_xla.dtype)
self.assertEqual(out.cpu(), out_xla.cpu(), prec=1e-4)

def test_cummax_0_sized_dimension(self):
# Test cummax on dim=2 (a 0-sized dimension).
#
# Make sure we are not crashing, here. Instead, we should return a tuple of
# empty tensors, just like PyTorch.

dim = 2
a = torch.rand(5, 5, 0, 5)

expected = torch.cummax(a, dim)
actual = torch.cummax(a.to(xm.xla_device()), dim)

self.assertEqual(actual, expected)


class MNISTComparator(nn.Module):

Expand Down
20 changes: 17 additions & 3 deletions torch_xla/csrc/tensor_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,9 +1314,23 @@ XLATensorPtr cross(const XLATensorPtr& input, const XLATensorPtr& other,

std::tuple<XLATensorPtr, XLATensorPtr> cummax(const XLATensorPtr& input,
int64_t dim) {
torch::lazy::NodePtr node = torch_xla::MakeNode<CumMax>(
input->GetIrValue(), torch::lazy::GetCanonicalDimensionIndex(
dim, input->shape().get().rank()));
xla::Shape shape = input->shape().get();
int64_t canonical_dim =
torch::lazy::GetCanonicalDimensionIndex(dim, shape.rank());

if (shape.dimensions(canonical_dim) == 0) {
// Handle edge-case where the size of `dim` is 0.
// The current lowering crashes, setting the padding to -1.
absl::Span<const int64_t> dimensions = shape.dimensions();
at::IntArrayRef shape_(dimensions.data(), dimensions.size());
at::Tensor val =
at::empty(shape_, at::TensorOptions().dtype(input->dtype()));
at::Tensor idx = at::empty(shape_, at::TensorOptions().dtype(at::kLong));
return std::make_tuple(input->Create(val, input->GetDevice()),
input->Create(idx, input->GetDevice()));
}
torch::lazy::NodePtr node =
torch_xla::MakeNode<CumMax>(input->GetIrValue(), canonical_dim);
XLATensorPtr t_value = input->CreateFrom(torch::lazy::Value(node, 0),
/*delay_eager_executation=*/true);
XLATensorPtr t_index =
Expand Down