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 index_put with boolean index #2018

Merged
merged 22 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
43 changes: 8 additions & 35 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4261,7 +4261,7 @@
raise NotImplementedError()


@torch_op(("aten::index_put", "aten::_unsafe_index_put"))
@torch_op(("aten::index_put", "aten::_unsafe_index_put"), trace_only=True)
def aten_index_put(
self: TReal,
indices: Sequence[INT64],
Expand All @@ -4275,18 +4275,18 @@
"""

# TODO(justinchuby): Handle when indicies has more than one element
index = op.SequenceAt(indices, 0)
index = indices[0]
new_index = op.Unsqueeze(index, [-1])

if op.Cast(accumulate, to=BOOL.dtype):
if accumulate:
result = op.ScatterND(self, new_index, values, reduction="add")
else:
result = op.ScatterND(self, new_index, values)

return result


@torch_op("aten::index_put")
@torch_op("aten::index_put", trace_only=True)
def aten_index_put_bool(
self: TReal,
indices: Sequence[BOOL],
Expand All @@ -4295,37 +4295,10 @@
) -> TReal:
"""index_put(Tensor self, Tensor?[] indices, Tensor values, bool accumulate=False) -> Tensor"""

index = op.SequenceAt(indices, 0) # assume indices only have 1 element
# FIXME: ORT ArgMax fails on INT64 input even though ONNX allows it
index_int = op.Cast(index, to=INT32.dtype)
# if all False, return op.Identity(self)
if op.ReduceSum(index_int) == 0:
result = self
else:
# change array([F,F,T,F,F]) to array([2])
index = op.ArgMax(index_int) # assume index only have 1 True
# change array([2]) to array([2,2,2,2,2])
self_dim_1 = op.Shape(self, start=1, end=2)
index_dim_0 = op.Shape(index, start=0, end=1)
shape = op.Concat(self_dim_1, index_dim_0, axis=0)
new_ind = op.Expand(index, shape)
new_ind_t = op.Transpose(new_ind)

# values must have same rank with input(self)
if op.Size(op.Shape(values)) < op.Size(op.Shape(self)): # type: ignore[operator]
values = op.Unsqueeze(values, op.Constant(value_ints=[0]))

if op.Cast(accumulate, to=BOOL.dtype):
zeros = op.Expand(op.Constant(value_float=0.0), op.Shape(self))
zeros = op.CastLike(zeros, values)
result = op.ScatterElements(zeros, new_ind_t, values)
# FIXME: type promotion
result = op.CastLike(result, self)
result = op.Add(result, self)
else:
result = op.ScatterElements(self, new_ind_t, values)

return result
# TODO: Support indices with more than 1 elements
index = indices[0]

Check warning on line 4299 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#L4299

Added line #L4299 was not covered by tests
# accumulate should be always False, True does not make sense but an assert would be great
xadupre marked this conversation as resolved.
Show resolved Hide resolved
return op.Where(index, values, self)

Check warning on line 4301 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#L4301

Added line #L4301 was not covered by tests
justinchuby marked this conversation as resolved.
Show resolved Hide resolved
justinchuby marked this conversation as resolved.
Show resolved Hide resolved


def aten_index_reduce(
Expand Down
1 change: 0 additions & 1 deletion tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,6 @@ def _where_input_wrangler(
matcher=lambda sample: sample.args[0][0].dtype != torch.bool,
reason="this Aten overload only supports tensor(bool) as indices",
)
.skip(reason="FIXME: https://github.com/microsoft/onnxscript/issues/1749"),
TorchLibOpInfo(
"index_put",
core_ops.aten_index_put,
Expand Down
Loading