Skip to content

Add Op (aten::masked_scatter) | feat (torchlib) #2112

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

Merged
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
22 changes: 19 additions & 3 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5202,10 +5202,26 @@
return op.Where(mask, value_cast, self)


def aten_masked_scatter(self: TensorType, mask: TensorType, source: TensorType) -> TensorType:
@torch_op(("aten::masked_scatter"), trace_only=True)
def aten_masked_scatter(self: TTensor, mask: TTensor, source: TTensor) -> TTensor:
"""masked_scatter(Tensor self, Tensor mask, Tensor source) -> Tensor"""

raise NotImplementedError()
if len(mask.shape) < len(self.shape):
mask = op.Expand(mask, op.Shape(self))

Check warning on line 5210 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L5210

Added line #L5210 was not covered by tests
else:
self = op.Expand(self, op.Shape(mask))
index = op.Transpose(op.NonZero(mask), perm=[1, 0])

Check warning on line 5213 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L5212-L5213

Added lines #L5212 - L5213 were not covered by tests

# NOTE: source can have more elements than needed.
# It could also have arbitrary shape.
# This is not supported by ONNX::ScatterND, so we need to flatten and slice source tensor.
source = op.Reshape(source, op.Constant(value_ints=[-1]))
axes = op.Constant(value_ints=[0])
starts = op.Constant(value_ints=[0])
ends = op.Gather(op.Shape(index), op.Constant(value_ints=[0]), axis=0)
source = op.Slice(source, starts, ends, axes)

Check warning on line 5222 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L5218-L5222

Added lines #L5218 - L5222 were not covered by tests

return op.ScatterND(self, index, source)

Check warning on line 5224 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L5224

Added line #L5224 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

What about self * (1-mask) + mask * source or mask * (source - self) + self ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The implementation is following torchscript converter. If we find it's too slow, we can change it later. I am unblocking Gemma3 for now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe create a follow up issue for tracking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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



def aten_masked_select(self: TensorType, mask: TensorType) -> TensorType:
Expand Down Expand Up @@ -6429,7 +6445,7 @@
raise NotImplementedError()


@torch_op("aten::nonzero")
@torch_op("aten::nonzero", trace_only=True)
def aten_nonzero(self: TTensor) -> INT64:
"""nonzero(Tensor self) -> Tensor"""
# NOTE: In torch the return shape is [n, d], while in onnx [d, n],
Expand Down
1 change: 1 addition & 0 deletions tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ def _where_input_wrangler(
dtypes=(torch.bool,),
reason="fixme: ORT does not have an implementation for Where with bool inputs.",
),
TorchLibOpInfo("masked_scatter", core_ops.aten_masked_scatter),
TorchLibOpInfo(
"matmul",
core_ops.aten_matmul,
Expand Down
Loading