Skip to content

Add support for float mask to aten::masked_fill #1337

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/Conversion/TorchToLinalg/Uncategorized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,8 @@ static Value createLinalgPayloadCalculationForElementwiseOp(

Value input = payloadArgs[0];
Value mask = payloadArgs[1];
if (mask.getType().isa<mlir::FloatType>())
mask = b.create<arith::ConstantOp>(loc, b.getBoolAttr(false));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I missed this the first time I reviewed your changes. Why is mask being set to false here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That seems to be expected behavior. I was casting to Int1 at first, but further testing shows that it seems to treat all floats as false. I haven't found anything in the documentation about it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems to be a bug upstream. I would actually expect float mask to result in a runtime error, since this is the behavior that aten.masked_select has:

https://github.com/pytorch/pytorch/blob/5b58140d1a471b144baf66cc61a45a86746f0215/aten/src/ATen/native/TensorAdvancedIndexing.cpp#L1720-L1721

Can you file a bug upstream for this?

Copy link
Collaborator Author

@gpetters94 gpetters94 Sep 15, 2022

Choose a reason for hiding this comment

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

Okay, the bug report is up here. I'm going to leave this as-is for now in case it's expected behavior but I'll add an assert if it isn't.

Value fillValue = convertScalarToDtype(b, loc, adaptor.value(), dtype);

return b.create<arith::SelectOp>(loc, mask, fillValue, input);
Expand Down
22 changes: 22 additions & 0 deletions python/torch_mlir_e2e_test/test_suite/constant_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,3 +1462,25 @@ def forward(self, x, mask, value):
def MaskedFillTensorFloatValueModule_basic(module, tu: TestUtils):
module.forward(tu.randint(2, 3, low=-10, high=10),
tu.randint(2, 3, high=2).to(dtype=torch.bool), tu.rand())


class MaskedFillTensorFloatMaskModule(torch.nn.Module):

def __init__(self):
super().__init__()

@export
@annotate_args([
None,
([-1, -1], torch.int64, True),
([-1, -1], torch.float, True),
])
def forward(self, x, mask):
return torch.ops.aten.masked_fill(x, mask, value=0.1)


@register_test_case(module_factory=lambda: MaskedFillTensorFloatMaskModule())
def MaskedFillTensorFloatMaskModule_basic(module, tu: TestUtils):
mask = tu.randint(2, 3, low=0, high=2).to(torch.float)
module.forward(tu.randint(2, 3, low=-10, high=10),
mask)