Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 13 additions & 2 deletions onnxscript/function_libs/torch_aten/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

from typing import Any, Optional, Sequence, Union

import onnx.helper

from onnxscript import BOOL, DOUBLE, FLOAT, INT64
from onnxscript.function_libs.torch_aten.registration import torch_op
from onnxscript.function_libs.torch_aten.typing import (
Expand Down Expand Up @@ -2174,10 +2176,19 @@ def aten_index_reduce(
raise NotImplementedError()


def aten_index_select(self: TensorType, dim: int, index: TensorType) -> TensorType:
@torch_op("aten::index_select")
def aten_index_select(self: TTensor, dim: int, index: TInt) -> TTensor:
# index_select(Tensor self, int dim, Tensor index) -> Tensor

raise NotImplementedError()
if op.Size(op.Shape(index)) == 0:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Otherwise, is index expected to be 1-dimensional? Or, can it have any other rank?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it should be 1d I think.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

should I do op.Reshape(index, [-1]) instead?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, if we expect it to be 0d or 1d, I think reshape, as you mention, is a better option.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done. thanks!

# Index is a scalar. Reshape it to a size 1 tensor.
index = op.Expand(
index,
op.Constant(value=onnx.helper.make_tensor("size_one", INT64.dtype, [1], [1])),
)

index = op.Cast(index, to=INT64.dtype)
return op.Gather(self, index, axis=dim)


def aten_index_select_backward(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def wrapped(fn):
"exp2": core_ops.aten_exp2,
"fmod": core_ops.aten_fmod,
"gt": core_ops.aten_gt,
"index_select": core_ops.aten_index_select,
"isinf": core_ops.aten_isinf,
"lt": core_ops.aten_lt,
"matmul": core_ops.aten_matmul,
Expand Down