-
Couldn't load subscription status.
- Fork 87
Improves implementation of aten_index_put #2641
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
Open
xadupre
wants to merge
19
commits into
main
Choose a base branch
from
xadupre/input_put
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+277
−16
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3cb493f
Implements aten_index_put if inputs are SymbolicTensor
xadupre c9472f3
disable case with one index
xadupre 434fcfb
type constant
xadupre 5cf4882
Merge branch 'main' of https://github.com/microsoft/onnxscript into i…
xadupre ae6adca
another fix
xadupre 8510364
rename
xadupre e4d574a
style
xadupre 86d482d
lint
xadupre 8305cac
fix merge conflicts
xadupre 02dda0e
Merge branch 'main' of https://github.com/microsoft/onnxscript into i…
xadupre e6f7633
Merge branch 'main' of https://github.com/microsoft/onnxscript into i…
xadupre e108dc3
rename
xadupre 988e9f6
handle one more case for index_put
xadupre f4a1196
Merge branch 'indexput' of https://github.com/xadupre/onnxscript into…
xadupre f56ccc7
Merge branch 'main' of https://github.com/microsoft/onnxscript into x…
xadupre 1e30097
raise an exception
xadupre f3731ed
one more unittest
xadupre b6ebed8
fix reduction
xadupre 1c1fc1a
lint
xadupre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4223,6 +4223,30 @@ def aten_index_put( | |
| See implementation of `torch.onnx.symbolic_opset11.index_put | ||
| <https://github.com/pytorch/pytorch/blob/main/torch/onnx/symbolic_opset11.py#L212>`_. | ||
| """ | ||
| if ( | ||
| len(indices) > 1 | ||
| and any( | ||
| isinstance(index, torch.onnx._internal.exporter._tensors.SymbolicTensor) # pylint: disable=protected-access | ||
| for index in indices | ||
| ) | ||
| and len(values.shape) == 1 | ||
| ): | ||
| return _aten_index_put_dynamic(self, indices, values, accumulate=accumulate) | ||
|
|
||
| n_none = [i for i, ind in enumerate(indices) if ind is not None] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess |
||
| if ( | ||
| len(n_none) == 1 | ||
| and len(indices[n_none[0]].shape) == 1 | ||
| and len(self.shape) == len(values.shape) | ||
| ): | ||
| return _aten_index_put_scatter_nd(self, indices, values, accumulate) | ||
|
|
||
| if len(indices) == 1 and set(indices[0].shape[:-1]) == {1} and indices[0].shape[0] == 1: | ||
| # shape(self) = (5,5), shape(indices[0]) = (1,2), shape(values) = (2,5) | ||
| # This case was only found in ops_data test. | ||
| return _aten_index_put_scatter_nd( | ||
| self, [op.Reshape(indices[0], [-1])], values, accumulate | ||
| ) | ||
|
|
||
| def _make_reshape_list_broadcastable(reshape_list, values_shape): | ||
| # Remove ones until the rank of reshape_list matches values_shape. | ||
|
|
@@ -4235,7 +4259,13 @@ def _make_reshape_list_broadcastable(reshape_list, values_shape): | |
| # the reshape list should be : [[2, 1], [1, 3], [2, 1]] | ||
| for i, r in enumerate(reshape_list): | ||
| if r not in (1, values_shape[i]): | ||
| value_index = values_shape.index(r) | ||
| try: | ||
| value_index = values_shape.index(r) | ||
| except ValueError as e: | ||
| raise RuntimeError( | ||
| f"Unable to find element {r!r} in shape {values_shape}, " | ||
| f"reshape_list={reshape_list}" | ||
| ) from e | ||
| # Swap elements | ||
| # For the example above the current reshape list is [1, 2] for last dim, | ||
| # to make it broadcastable, we swap the elements | ||
|
|
@@ -4259,15 +4289,22 @@ def _make_reshape_list_broadcastable(reshape_list, values_shape): | |
| reshape_update = self.shape[i] | ||
| else: | ||
| idx = indices[i] | ||
| reshape_update = math.prod(idx.shape) | ||
| # when Index is more than 1D, flatten it and also the values shape | ||
| # Example: self shape: (10, 3), indices[i] shape: (2, 4), values shape: (2, 4, 3) | ||
| # Indices -> (2*4,) and values shape (2*4, 32) | ||
| if len(idx.shape) > 1: | ||
| values_shape = (reshape_update, *values_shape[len(idx.shape) :]) | ||
|
|
||
| # Flatten index (always working with 1D index in each dim) | ||
| idx = op.Reshape(idx, [-1]) | ||
| if all(isinstance(s, int) for s in idx.shape): | ||
| reshape_update = math.prod(idx.shape) | ||
| # when Index is more than 1D, flatten it and also the values shape | ||
| # Example: self shape: (10, 3), indices[i] shape: (2, 4), values shape: (2, 4, 3) | ||
| # Indices -> (2*4,) and values shape (2*4, 32) | ||
| if len(idx.shape) > 1: | ||
| values_shape = (reshape_update, *values_shape[len(idx.shape) :]) | ||
|
|
||
| # Flatten index (always working with 1D index in each dim) | ||
| idx = op.Reshape(idx, [-1]) | ||
| else: | ||
| raise RuntimeError( | ||
| f"Unable to handle index {indices[i]} for axis={i} " | ||
| f"because one of the dimension is not static as shape=" | ||
| f"{idx.shape}, indices={indices}" | ||
| ) | ||
|
|
||
| # Create a reshape pattern: one value per index dimension, | ||
| # with the current dimension set to the update size. | ||
|
|
@@ -4292,14 +4329,131 @@ def _make_reshape_list_broadcastable(reshape_list, values_shape): | |
| # Flatten values to match the indices | ||
| flat_values = op.Reshape(values, [-1]) | ||
|
|
||
| if accumulate: | ||
| result = op.ScatterND(self, new_index, flat_values, reduction="add") | ||
| else: | ||
| result = op.ScatterND(self, new_index, flat_values) | ||
|
|
||
| scatter_kwargs = dict(reduction="add") if accumulate else {} | ||
| result = op.ScatterND(self, new_index, flat_values, **scatter_kwargs) | ||
| return result | ||
|
|
||
|
|
||
| def _aten_index_put_scatter_nd( | ||
| x: TReal, | ||
| indices: Sequence[INT64], | ||
| values: TReal, | ||
| accumulate: bool = False, | ||
| ) -> TReal: | ||
| def _1dint(i: int): | ||
| return op.Constant(value_ints=ir.AttrInt64s("value_ints", [i])) | ||
|
|
||
| n_none = [i for i, ind in enumerate(indices) if ind is not None] | ||
| assert len(n_none) == 1, f"Unable to handle that case: n_none={n_none}" | ||
| unsq = op.Unsqueeze(indices[n_none[0]], _1dint(1)) | ||
| if n_none[0] == 0: | ||
| return op.ScatterND(x, unsq, values, reduction="add" if accumulate else "none") | ||
|
|
||
| perm = list(range(len(x.shape))) | ||
| perm[n_none[0]], perm[0] = perm[0], perm[n_none[0]] | ||
| return op.Transpose( | ||
| op.ScatterND( | ||
| op.Transpose(x, perm=perm), | ||
| unsq, | ||
| op.Transpose(values, perm=perm), | ||
| reduction="add" if accumulate else "none", | ||
| ), | ||
| perm=perm, | ||
| ) | ||
|
|
||
|
|
||
| def _aten_index_put_dynamic( | ||
| x: TReal, | ||
| indices: Sequence[INT64], | ||
| values: TReal, | ||
| accumulate: bool = False, | ||
| ) -> TReal: | ||
| def _1dint(i: int): | ||
| return op.Constant(value_ints=ir.AttrInt64s("value_ints", [i])) | ||
|
|
||
| def _0dint(i: int): | ||
| return op.Constant(value_int=ir.AttrInt64("value_int", i)) | ||
|
|
||
| def _make_range_or_cast(ind, shape_x, static_shape: bool, dim: int): | ||
| if ind is not None: | ||
| return op.Cast(ind, to=INT64.dtype), False | ||
| return ( | ||
| op.Cast( | ||
| op.Range( # Range does not return a typed result | ||
| _0dint(0), | ||
| op.Squeeze(op.Shape(x, start=dim, end=dim + 1)), | ||
| _0dint(1), | ||
| ), | ||
| to=INT64.dtype, | ||
| ), | ||
| True, | ||
| ) | ||
|
|
||
| shape_x = op.Shape(x) | ||
| exped = [] | ||
| fixed = [] | ||
| reshape_value_shape2 = [] | ||
| expand_value_shape = [] | ||
| for i, ind in enumerate(indices): | ||
| if isinstance(ind, torch.onnx._internal.exporter._tensors.SymbolicTensor): # pylint: disable=protected-access | ||
| ind.dtype = ir.DataType.INT64 | ||
| ind, expanded = _make_range_or_cast(ind, shape_x, False, i) | ||
| if expanded: | ||
| exped.append((i, ind)) | ||
| expand_value_shape.append(op.Shape(x, start=i, end=i + 1)) | ||
| reshape_value_shape2.append(_1dint(1)) | ||
| else: | ||
| expand_value_shape.append(_1dint(1)) | ||
| reshape_value_shape2.append(op.Shape(ind)) | ||
| fixed.append((i, ind)) | ||
|
|
||
| reshape_value_shape1 = [_1dint(1)] * len(indices) | ||
| if len(fixed) <= 1: | ||
| reshape_value_shape1 = None | ||
| elif fixed: | ||
| reshape_value_shape1[fixed[-1][0]] = _1dint(-1) | ||
|
|
||
| def _mkstride(x, i): | ||
| if i >= len(x.shape) - 1: | ||
| return _1dint(1) | ||
| if i == len(x.shape) - 2: | ||
| return op.Shape(x, start=i + 1) | ||
| return op.ReduceProd(op.Shape(x, start=i + 1), keepdims=1) | ||
|
|
||
| shape = [1] * (len(x.shape) + 1) | ||
| r_fixed = [] | ||
| if fixed: | ||
| new_shape = shape.copy() | ||
| new_shape[-1] = -1 | ||
| r_fixed = [op.Reshape(op.Mul(_mkstride(x, i), f), new_shape) for i, f in fixed] | ||
|
|
||
| r_exped = [] | ||
| for i, e in exped: | ||
| new_shape = shape.copy() | ||
| new_shape[i] = -1 | ||
| r_exped.append(op.Reshape(op.Mul(_mkstride(x, i), e), new_shape)) | ||
|
|
||
| # final sum | ||
| unflat = None | ||
| for a in [*r_fixed, *r_exped]: | ||
| if unflat is None: | ||
| unflat = a | ||
| continue | ||
| unflat = op.Add(unflat, a) | ||
|
|
||
| # value_shape | ||
| expanded_values = values | ||
| if reshape_value_shape1 is not None: | ||
| expanded_values = op.Reshape(expanded_values, op.Concat(*reshape_value_shape1, axis=0)) | ||
| expanded_values = op.Expand(expanded_values, op.Concat(*expand_value_shape, axis=0)) | ||
| flat_ind = op.Reshape(unflat, _1dint(-1)) | ||
| expanded_values = op.Reshape(expanded_values, _1dint(-1)) | ||
| flat_x = op.Reshape(x, _1dint(-1)) | ||
| scat_kwargs = {"reduction": "add"} if accumulate else {} | ||
| flat_up_x = op.ScatterElements(flat_x, flat_ind, expanded_values, **scat_kwargs) | ||
| return op.Reshape(flat_up_x, op.Shape(x)) | ||
|
|
||
|
|
||
| @torch_op("aten::index_put", trace_only=True) | ||
| def aten_index_put_bool( | ||
| self: TReal, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this condition for? I am just trying to understand the assumptions/conditions for this special case.