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

[fix] masked_scatter_: non-contiguous self #100232

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions aten/src/ATen/native/TensorAdvancedIndexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,8 @@ Tensor & masked_scatter__cpu(Tensor& self, const Tensor & mask, const Tensor & s
.set_check_mem_overlap(false)
.check_all_same_dtype(false)
.resize_outputs(false)
// order of indexing matters
.enforce_linear_iteration()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think this should affect the perf for contiguous cases.

.add_output(self)
.add_input(*b_mask)
.build();
Expand Down
34 changes: 34 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5392,6 +5392,40 @@ def test_item(self, device, dtype):
t = torch.ones((), device=device, dtype=dtype)
self.assertEqual(1, t.item())

@onlyNativeDeviceTypes
def test_masked_scatter_inplace_noncontiguous(self, device):
t = torch.zeros(5, 2, dtype=torch.long, device=device)
t_non_contig = t.transpose(0, 1)
t_contig = t_non_contig.contiguous()

assert t_contig.is_contiguous()
assert not t_non_contig.is_contiguous()

mask = torch.tensor([[False, True], [False, True], [False, False], [True, True], [True, True]], device=device)
mask_non_contig = mask.transpose(0, 1)
mask_contig = mask_non_contig.contiguous()

assert mask_contig.is_contiguous()
assert not mask_non_contig.is_contiguous()

# source is always converted to contiguous by the op.
source = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 9]], device=device)

# t: contig, mask: contig
expected = t_contig.masked_scatter_(mask_contig, source)

# t: non-contig, mask: non-contig
actual = t_non_contig.masked_scatter_(mask_non_contig, source)
self.assertEqual(actual, expected)

# t: contig, mask: non-contig
actual = t_contig.masked_scatter_(mask_non_contig, source)
self.assertEqual(actual, expected)

# t: non-contig, mask: contig
actual = t_non_contig.masked_scatter_(mask_contig, source)
self.assertEqual(actual, expected)


# Tests that compare a device's computation with the (gold-standard) CPU's.
class TestDevicePrecision(TestCase):
Expand Down