Skip to content

Commit f9f2fa2

Browse files
authored
Add support for aten:index op when index is boolean | feat(torchlib) (#1285)
Implement index.Tensor(Tensor self, Tensor?[] indices) -> Tensor when indices is boolean. Fix #1141
1 parent 9943026 commit f9f2fa2

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

onnxscript/function_libs/torch_lib/ops/core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,6 +4035,13 @@ def aten_index(self: TensorType, indices: Sequence[Optional[INT64]]) -> TensorTy
40354035
return op.Transpose(self, perm=perm)
40364036

40374037

4038+
@torch_op(("aten::index.Tensor", "aten::_unsafe_index.Tensor"), trace_only=True)
4039+
def aten_index_bool(self: TensorType, indices: Sequence[Optional[BOOL]]) -> TensorType:
4040+
new_indices = op.Transpose(op.NonZero(indices[0]), perm=[1, 0])
4041+
new_indices = op.Squeeze(new_indices, axes=[1])
4042+
return op.Gather(self, new_indices, axis=0)
4043+
4044+
40384045
def aten_index_add(
40394046
self: TensorType, dim: int, index: TensorType, source: TensorType, alpha: float = 1
40404047
) -> TensorType:

onnxscript/tests/function_libs/torch_lib/extra_opinfo.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,31 @@ def sample_inputs__fft_c2r(self, device, dtype, requires_grad=False, **_):
692692
)
693693

694694

695+
def _index_variable_bool(shape, max_indices, device):
696+
if not isinstance(shape, tuple):
697+
shape = (shape,)
698+
index = (
699+
torch.rand(*shape, dtype=torch.double, device=device).mul_(max_indices).floor_().bool()
700+
)
701+
return index
702+
703+
704+
def sample_inputs_index_bool(op_info, device, dtype, requires_grad, **kwargs):
705+
del op_info # Unused
706+
del kwargs # Unused
707+
make_arg = functools.partial(
708+
torch_testing.make_tensor, dtype=dtype, device=device, requires_grad=requires_grad
709+
)
710+
s = 5
711+
index_bool = _index_variable_bool(s, s, device=device)
712+
test_args = [
713+
([index_bool],),
714+
]
715+
716+
for args in test_args:
717+
yield opinfo_core.SampleInput(make_arg((s, s, s, s)), args=args)
718+
719+
695720
def sample_inputs_index(op_info, device, dtype, requires_grad, **kwargs):
696721
del op_info # Unused
697722
del kwargs # Unused
@@ -1933,6 +1958,15 @@ def __init__(self):
19331958
),
19341959
sample_inputs_func=sample_inputs_index,
19351960
),
1961+
opinfo_core.OpInfo(
1962+
"ops.aten.index.Tensor.bool",
1963+
aten_name="index.Tensor",
1964+
dtypes=common_dtype.all_types_and_complex_and(
1965+
torch.bool, torch.float16, torch.bfloat16, torch.chalf
1966+
),
1967+
sample_inputs_func=sample_inputs_index_bool,
1968+
op=torch.ops.aten.index.Tensor,
1969+
),
19361970
opinfo_core.OpInfo(
19371971
"ops.aten.layer_norm",
19381972
aten_name="layer_norm",

onnxscript/tests/function_libs/torch_lib/ops_test_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ def _where_input_wrangler(
833833
# TorchLibOpInfo("is_same_size", core_ops.aten_is_same_size), # no test case in OPS_DB
834834
# TorchLibOpInfo("is_nonzero", core_ops.aten_is_nonzero), # no test case in OPS_DB
835835
TorchLibOpInfo("ops.aten.index.Tensor", core_ops.aten_index, trace_only=True),
836+
TorchLibOpInfo("ops.aten.index.Tensor.bool", core_ops.aten_index_bool, trace_only=True),
836837
TorchLibOpInfo(
837838
"index_put_bool",
838839
core_ops.aten_index_put_bool,

0 commit comments

Comments
 (0)