Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/onnx/test_pytorch_onnx_caffe2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,24 @@ def forward(self, x):
inputs = torch.randn(3, 2, 1)
self.run_model_test(model, train=False, input=(inputs, ), batch_size=BATCH_SIZE)


@skipIfUnsupportedMinOpsetVersion(9)
def test_masked_fill(self):
class MaskedFillModel(torch.nn.Module):
def forward(self, x):
mask = torch.tensor([[0, 0, 1], [1, 1, 0]], dtype=torch.uint8)
return x.masked_fill(mask, 2)

x = torch.zeros(4, 2, 3, requires_grad=True)
self.run_model_test(MaskedFillModel(), input=(x, ), train=False, batch_size=BATCH_SIZE)

class MaskedFillModel2(torch.nn.Module):
def forward(self, x):
return x.masked_fill(x > 3, -1)

x = torch.arange(16).view(2, 2, 4).to(torch.float32)
self.run_model_test(MaskedFillModel2(), input=(x, ), train=False, batch_size=BATCH_SIZE)

# a bit of metaprogramming to set up all the rnn tests


Expand Down
18 changes: 18 additions & 0 deletions test/onnx/test_pytorch_onnx_onnxruntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,23 @@ def forward(self, x):
x = torch.randn(2, 3, 4)
self.run_test(TensorFactory(), x)

@skipIfUnsupportedMinOpsetVersion(9)
def test_masked_fill(self):
class MaskedFillModel(torch.nn.Module):
def forward(self, x):
mask = torch.tensor([[0, 0, 1], [1, 1, 0]], dtype=torch.uint8)
return x.masked_fill(mask, 2)

x = torch.zeros(4, 2, 3, requires_grad=True)
self.run_test(MaskedFillModel(), x)

class MaskedFillModel2(torch.nn.Module):
def forward(self, x):
return x.masked_fill(x > 3, -1)

x = torch.arange(16).view(2, 2, 4).to(torch.float32)
self.run_test(MaskedFillModel2(), x)


# opset 7 tests
TestONNXRuntime_opset7 = type(str("TestONNXRuntime_opset7"),
Expand All @@ -375,6 +392,7 @@ def forward(self, x):
(unittest.TestCase,),
dict(TestONNXRuntime.__dict__, opset_version=8))


# opset 10 tests
TestONNXRuntime_opset10 = type(str("TestONNXRuntime_opset10"),
(unittest.TestCase,),
Expand Down
1 change: 1 addition & 0 deletions torch/onnx/symbolic_opset8.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

black_listed_operators = [
"nonzero", "where", "scatter", "scatter_add", "erf", "sign", "isnan", "gather",
"masked_fill"
]

for black_listed_op in black_listed_operators:
Expand Down
6 changes: 6 additions & 0 deletions torch/onnx/symbolic_opset9.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,3 +1664,9 @@ def gather(g, self, dim, index, sparse_grad=False):
@parse_args('v', 'is', 'i')
def logsumexp(g, input, dim, keepdim):
return g.op('ReduceLogSumExp', input, axes_i=dim, keepdims_i=keepdim)


def masked_fill(g, self, mask, value):
mask = _cast_Bool(g, mask, False)
value = sym_help._maybe_get_scalar(value)
return g.op('Where', mask, sym_help._if_scalar_type_as(g, value, self), self)