-
Notifications
You must be signed in to change notification settings - Fork 109
Implements repeat_interleave #2477
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
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7424f69
Implements repeat_interleave
xadupre 05c9062
remove mistake
xadupre 9560baa
lint
xadupre 689a456
lint
xadupre 9881f6e
fixed
xadupre c442d38
Merge branch 'main' of https://github.com/microsoft/onnxscript into r…
xadupre 84e297e
lint
xadupre edd67f1
fix merge conflict
xadupre 0e590a4
restore one test worngly merged
xadupre 00ca91a
Merge branch 'main' of https://github.com/microsoft/onnxscript into r…
xadupre 9586e57
merge with main
xadupre 7cc457a
fix repeat_interleave
xadupre b343329
Update onnxscript/function_libs/torch_lib/ops/core.py
xadupre 1343c54
Merge branch 'main' into repeati
justinchuby 7043030
Merge branch 'main' into repeati
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7292,12 +7292,108 @@ | |||||
| return op.Tile(self_expanded, repeats) | ||||||
|
|
||||||
|
|
||||||
| def aten_repeat_interleave( | ||||||
| repeats: TensorType, output_size: Optional[int] = None | ||||||
| @torch_op("aten::repeat_interleave.Scalar", trace_only=True) | ||||||
| def aten_repeat_interleave_int( | ||||||
| self: TensorType, repeats: int, dim: Optional[int] | ||||||
| ) -> TensorType: | ||||||
| """repeat_interleave.Tensor(Tensor repeats, *, int? output_size=None) -> Tensor""" | ||||||
| """repeat_interleave.Tensor(Tensor repeats, *, int? output_size=None) -> Tensor | ||||||
|
xadupre marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| raise NotImplementedError() | ||||||
| The trick is to repeat in one direction orthogonal to reshape. | ||||||
|
|
||||||
| .. code-block:: python | ||||||
|
|
||||||
| x = torch.tensor([[0, 1, 2], [3, 4, 5]]) | ||||||
| x.repeat_interleave(2, dim=0) | ||||||
|
|
||||||
| is equivalent to: | ||||||
|
|
||||||
| .. code-block:: python | ||||||
|
|
||||||
| x = torch.tensor([[0, 1, 2], [3, 4, 5]]) | ||||||
| x.repeat((1, 2)).reshape((-1, t.shape[1])) | ||||||
| """ | ||||||
| if dim is None: | ||||||
| raise NotImplementedError("No conversion available yet when dim is None.") | ||||||
|
|
||||||
| self_rank = len(self.shape) | ||||||
| pos_dim = (dim + self_rank) % self_rank | ||||||
| unsqueezed = op.Unsqueeze(self, [pos_dim + 1]) | ||||||
| onehot = op.Concat(op.ConstantOfShape((self_rank,), value=[1]), repeats, axis=0) | ||||||
|
xadupre marked this conversation as resolved.
Outdated
|
||||||
| tiled = op.Tile(unsqueezed, onehot) | ||||||
|
xadupre marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| if dim < -1: | ||||||
|
xadupre marked this conversation as resolved.
Outdated
|
||||||
| dim += self_rank | ||||||
| return aten_flatten(tiled, -2 if dim == -1 else dim, -1 if dim == -1 else (dim + 1)) | ||||||
|
xadupre marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
|
|
||||||
| @torch_op("aten::repeat_interleave.Tensor", trace_only=True) | ||||||
| def aten_repeat_interleave_Tensor( | ||||||
| self: TensorType, repeats: Optional[TensorType] = None, dim: Optional[int] = None | ||||||
| ) -> TensorType: | ||||||
| """repeat_interleave.Tensor(Tensor repeats, *, int? output_size=None) -> Tensor | ||||||
|
|
||||||
| When `repeats` is a tensor, each line is multiplied | ||||||
| by a different number. | ||||||
| There are multiple strategies. Here is one. | ||||||
|
|
||||||
| .. code-block:: python | ||||||
|
|
||||||
| import torch | ||||||
|
|
||||||
| x = torch.tensor([[0, 1, 2], [3, 4, 5]]) | ||||||
| times = torch.tensor([2, 3], dtype=torch.int64) | ||||||
| y = x.repeat_interleave(times, dim=0) | ||||||
| print("repeat_interleave") | ||||||
| print(y) | ||||||
|
|
||||||
| ci = times.cumsum(dim=0) | ||||||
| rows = torch.arange(ci[-1], dtype=torch.int64) < ci.reshape((-1, 1)) | ||||||
| srows = times.shape[0] - rows.to(torch.int64).sum(axis=0) | ||||||
| indices = srows.reshape((-1, )) | ||||||
| print("decomposed") | ||||||
| print(x[indices, :]) | ||||||
| """ | ||||||
| if repeats is None: | ||||||
| repeats = self | ||||||
| self = op.Range(0, op.Squeeze(op.Shape(repeats, start=-1), [0]), 1) | ||||||
| if dim is None: | ||||||
| # flatten | ||||||
| self = op.Reshape(self, [-1]) | ||||||
| rk = 1 | ||||||
|
Collaborator
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.
Suggested change
Member
Author
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. done |
||||||
| else: | ||||||
| rk = len(self.shape) | ||||||
|
|
||||||
| if rk > 2: | ||||||
| shape_x0 = op.Shape(self, start=0, end=1) | ||||||
| shape_x = op.Shape(self, start=1) | ||||||
| self = op.Reshape(self, op.Concat(shape_x0, [-1], axis=0)) | ||||||
| elif rk == 1: | ||||||
| shape_x0 = None | ||||||
|
|
||||||
| shape_x = None | ||||||
| self = op.Reshape(self, [-1, 1]) | ||||||
| else: | ||||||
| if rk != 2: | ||||||
| raise NotImplementedError(f"rank(self)={rk} not implemented for repeat_interleave") | ||||||
|
xadupre marked this conversation as resolved.
|
||||||
| shape_x = None | ||||||
|
|
||||||
| ci = op.CumSum(repeats, [0]) | ||||||
| last_ci = op.Gather(ci, [-1]) | ||||||
| trange = op.Range(0, op.Squeeze(last_ci, [0]), 1) | ||||||
| rows = op.Less(trange, op.Unsqueeze(ci, [-1])) | ||||||
| srows = op.Sub( | ||||||
| op.Shape(self, start=0, end=1), | ||||||
| op.ReduceSum(op.Cast(rows, to=INT64.dtype), [0]), | ||||||
| ) | ||||||
| indices = op.Reshape(srows, [-1]) | ||||||
| values = op.GatherND(self, op.Unsqueeze(indices, [-1])) | ||||||
| if rk == 2: | ||||||
| return values | ||||||
| # shape_x cannot be None at this stage. | ||||||
| assert shape_x is not None # for mypy | ||||||
| return op.Reshape( | ||||||
| values, | ||||||
| op.Concat([-1], shape_x, axis=0) if shape_x else [-1], | ||||||
|
|
||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| @torch_op("aten::reshape") | ||||||
|
|
||||||
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.
Uh oh!
There was an error while loading. Please reload this page.