Skip to content

Use sets to improve performance of dfs search #6213

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 1 commit into from
Jul 5, 2024

Conversation

kylesayrs
Copy link
Contributor

@kylesayrs kylesayrs commented Jul 4, 2024

Description

This change modifies the DFS search used in model extraction to use sets rather than iterables

Motivation and Context

While shape inference is the most significant bottleneck, these changes are a step in the direction of being able to support model extraction for very large graphs.

Test Script

import onnx
from onnx.utils import Extractor

model = onnx.load("obertquant.onnx")
extractor = Extractor(model)
extracted_model = extractor.extract_model(
    input_names=["input_ids", "attention_mask", "token_type_ids"], output_names=["2058"]
)
onnx.save(extracted_model, "truncated.onnx")

Benchmarks were produced using pyinstrument and analyzing the Extractor.extract_model function

Model Name Num Nodes Previous New
obertquant.onnx 1271 0.158s 0.110s
ai-town-3B.onnx 3515 8.002s 3.725s

@kylesayrs kylesayrs requested a review from a team as a code owner July 4, 2024 20:47
@kylesayrs kylesayrs force-pushed the extractor-dfs-optimization branch from 5714577 to 61e7fb1 Compare July 4, 2024 20:50
Copy link

codecov bot commented Jul 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 57.11%. Comparing base (83194ed) to head (01d56d4).
Report is 61 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6213      +/-   ##
==========================================
+ Coverage   56.95%   57.11%   +0.15%     
==========================================
  Files         506      506              
  Lines       30467    31087     +620     
  Branches     4592     4624      +32     
==========================================
+ Hits        17353    17754     +401     
- Misses      12285    12505     +220     
+ Partials      829      828       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kylesayrs kylesayrs force-pushed the extractor-dfs-optimization branch from c1ca823 to e75c67a Compare July 4, 2024 21:11
Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
@kylesayrs kylesayrs force-pushed the extractor-dfs-optimization branch from e75c67a to 01d56d4 Compare July 4, 2024 21:22
@xadupre
Copy link
Contributor

xadupre commented Jul 4, 2024

It seems reachable + unreachable = set of nodes. Do you it is doable to remoave unreachable and simplify the function signature assuming performance stays the same?

@kylesayrs
Copy link
Contributor Author

@xadupre The reachable set must be maintained, as it is the ultimate purpose of the function. This leaves the unreachable set to be removed. Unfortunately, in order to iterate through the negative set of reachable, the function must iterate through all node indices.

nodes_to_search = [
    index
    for index in range(len(nodes))
    if node_output_name in nodes[index].output and index not in reachable
]

This leads to a longer runtime for larger models

Model Name Num Nodes Previous New One Set
obertquant.onnx 1271 0.158s 0.110s 0.118s
ai-town-3B.onnx 3515 8.002s 3.725s 6.094s

@kylesayrs kylesayrs changed the title Use sets to increase performance of dfs search Use sets to improve performance of dfs search Jul 5, 2024
@xadupre xadupre added this pull request to the merge queue Jul 5, 2024
Merged via the queue into onnx:main with commit c1dffb6 Jul 5, 2024
38 checks passed
andife pushed a commit to andife/onnx that referenced this pull request Jul 20, 2024
### Description
This change modifies the DFS search used in model extraction to use sets
rather than iterables

### Motivation and Context
While shape inference is the most significant bottleneck, these changes
are a step in the direction of being able to support model extraction
for very large graphs.

## Test Script ##
```python3
import onnx
from onnx.utils import Extractor

model = onnx.load("obertquant.onnx")
extractor = Extractor(model)
extracted_model = extractor.extract_model(
    input_names=["input_ids", "attention_mask", "token_type_ids"], output_names=["2058"]
)
onnx.save(extracted_model, "truncated.onnx")
```

Benchmarks were produced using pyinstrument and analyzing the
`Extractor.extract_model` function
| Model Name | Num Nodes | Previous | New |
| ---- | -------- | --------- | ----- |
| obertquant.onnx | 1271  | 0.158s   | 0.110s |
| ai-town-3B.onnx | 3515  | 8.002s  | 3.725s |

Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
Signed-off-by: Andreas Fehlner <fehlner@arcor.de>
linshokaku pushed a commit to linshokaku/onnx that referenced this pull request Oct 2, 2024
### Description
This change modifies the DFS search used in model extraction to use sets
rather than iterables

### Motivation and Context
While shape inference is the most significant bottleneck, these changes
are a step in the direction of being able to support model extraction
for very large graphs.

## Test Script ##
```python3
import onnx
from onnx.utils import Extractor

model = onnx.load("obertquant.onnx")
extractor = Extractor(model)
extracted_model = extractor.extract_model(
    input_names=["input_ids", "attention_mask", "token_type_ids"], output_names=["2058"]
)
onnx.save(extracted_model, "truncated.onnx")
```

Benchmarks were produced using pyinstrument and analyzing the
`Extractor.extract_model` function
| Model Name | Num Nodes | Previous | New |
| ---- | -------- | --------- | ----- |
| obertquant.onnx | 1271  | 0.158s   | 0.110s |
| ai-town-3B.onnx | 3515  | 8.002s  | 3.725s |

Signed-off-by: Kyle Sayers <kylesayrs@gmail.com>
Signed-off-by: Linsho Kaku <linsho@preferred.jp>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants