-
Notifications
You must be signed in to change notification settings - Fork 66
[IR] introduce slice support on graph #2307
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
[IR] introduce slice support on graph #2307
Conversation
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.
Thanks!
Could you also update the following text to match the implementation?
in the class docstring above. also https://github.com/Johansmm/onnxscript/blob/862de43e60f51ee4833104a254da9d8db80420a6/onnxscript/ir/_core.py#L2319 on complexity? There are two type annotations to update: https://github.com/Johansmm/onnxscript/blob/862de43e60f51ee4833104a254da9d8db80420a6/onnxscript/ir/_core.py#L2715 and https://github.com/Johansmm/onnxscript/blob/862de43e60f51ee4833104a254da9d8db80420a6/onnxscript/ir/_core.py#L2285 . Please follow the recommendation below to create typing overloads for the methods |
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.
Some comments, thank you!
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2307 +/- ##
==========================================
- Coverage 73.76% 73.76% -0.01%
==========================================
Files 239 239
Lines 30906 30930 +24
Branches 3494 3503 +9
==========================================
+ Hits 22799 22815 +16
Misses 6907 6907
- Partials 1200 1208 +8 ☔ View full report in Codecov by Sentry. |
862de43
to
2937953
Compare
Last push force with suggestions |
2937953
to
1005aa7
Compare
The final thing I wanted to make sure before merge is that accessing nodes on either ends (graph.node[0], graph.node[-1]) is not much slower (better if it turns out to be faster) than before. Would you be able to run a small test? |
@justinchuby, the new complexity is O(n) for any case. import time
from onnxscript.ir import _linked_list
class _TestElement:
def __init__(self, value):
self.value = value
def __repr__(self) -> str:
return f"_TestElement({self.value})"
N = 100000
linked_list = _linked_list.DoublyLinkedSet()
elems = [_TestElement(i) for i in range(N)]
linked_list.extend(elems)
time_list_1 = []
idx = -1
for _ in range(1000):
t = time.time()
x = tuple(linked_list)[idx]
t = time.time() - t
time_list_1.append(t)
time_list_2 = []
for _ in range(1000):
t = time.time()
for i in reversed(linked_list):
x = i
break
t = time.time() - t
time_list_2.append(t)
print(sum(time_list_1) / 1000)
print(sum(time_list_2) / 1000) Results changing
Therefore:
|
It is in fact quite common to have models with more than 1000 nodes in the case of LLMs. I would prefer it to run faster. Could you do (1) as you suggested, and revert the comment changes? Thanks a lot! I think it makes sense to handle slices in an if branch. |
1005aa7
to
bb10b1c
Compare
@justinchuby, I have updated the code to produce a more optimal response. Now |
Thank you! The changes look very reasonable. Were you able to run the benchmark again with some examples? |
@justinchuby can you explain what benchmark you are talking about? If you are referring to the IR unit tests, I ran them and they passed without any problem. |
I was referring to #2307 (comment) this script in your comment |
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.
Pull Request Overview
This PR adds slice support for accessing elements from the graph‐related collections and linked list implementation. Key changes include:
- Addition of parameterized tests for slicing in _linked_list_test.py.
- Implementation of slice overloads and corresponding logic in DoublyLinkedSet in _linked_list.py.
- Similar overload enhancements for getitem in node collections in _core.py.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
onnxscript/ir/_linked_list_test.py | Added parameterized test to validate slice support |
onnxscript/ir/_linked_list.py | Updated getitem to support slicing with overloads and custom logic |
onnxscript/ir/_core.py | Added overload annotations for slice support in node collection access |
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.
Sorry after taking another look I wonder if the added complexity is worth it (line 160-162, 166-167). Would it be a good idea to (1) keep the original logic for int index, and (2) use tuple for slices?
bb10b1c
to
e29419f
Compare
@justinchuby, I see that you don't agree with my algorithm, so I have gone back to the initial solution. Note that this one is a bit more expensive. |
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.
Thank you! I think it’s a good trade off for simplicity
@@ -2282,7 +2282,12 @@ | |||
def opset_imports(self) -> dict[str, int]: | |||
return self._opset_imports | |||
|
|||
def __getitem__(self, index: int) -> Node: | |||
@typing.overload | |||
def __getitem__(self, index: int) -> Node: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@typing.overload | ||
def __getitem__(self, index: int) -> Node: ... | ||
@typing.overload | ||
def __getitem__(self, index: slice) -> Sequence[Node]: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@@ -2712,7 +2717,12 @@ | |||
self._metadata_props: dict[str, str] | None = metadata_props | |||
self._nodes: tuple[Node, ...] = tuple(nodes) | |||
|
|||
def __getitem__(self, index: int) -> Node: | |||
@typing.overload | |||
def __getitem__(self, index: int) -> Node: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@typing.overload | ||
def __getitem__(self, index: int) -> Node: ... | ||
@typing.overload | ||
def __getitem__(self, index: slice) -> Sequence[Node]: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@@ -2961,7 +2971,12 @@ | |||
def attributes(self) -> OrderedDict[str, Attr]: | |||
return self._attributes | |||
|
|||
def __getitem__(self, index: int) -> Node: | |||
@typing.overload | |||
def __getitem__(self, index: int) -> Node: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@typing.overload | ||
def __getitem__(self, index: int) -> Node: ... | ||
@typing.overload | ||
def __getitem__(self, index: slice) -> Sequence[Node]: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@@ -136,11 +136,18 @@ | |||
) | |||
return self._length | |||
|
|||
def __getitem__(self, index: int) -> T: | |||
@overload | |||
def __getitem__(self, index: int) -> T: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
@overload | ||
def __getitem__(self, index: int) -> T: ... | ||
@overload | ||
def __getitem__(self, index: slice) -> Sequence[T]: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
Hi @Johansmm, we are in the process of migrating the IR component to https://github.com/onnx/ir-py so that it becomes an official onnx project. Would you like to help us duplicate your PR to the new repository as well? We are looking to create an initial release within about a week or less. |
@justinchuby done: onnx/ir-py#25 |
Introduce slice support
Close #2302