-
Notifications
You must be signed in to change notification settings - Fork 72
[pass] Create topological sort pass #2191
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86a1305
[passes] Create topological sort pass
justinchuby a25a8c7
Add unit tests for TopologicalSortPass
justinchuby df9b5bf
lint
justinchuby c104e76
test order
justinchuby bb9a501
naming
justinchuby 28044cb
test
justinchuby ea39dfc
Rename variables for clarity in topological sort
justinchuby 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
"""Pass for topologically sorting the graphs.""" | ||
|
||
from __future__ import annotations | ||
|
||
__all__ = [ | ||
"TopologicalSortPass", | ||
] | ||
|
||
|
||
from onnxscript import ir | ||
|
||
|
||
class TopologicalSortPass(ir.passes.InPlacePass): | ||
"""Topologically sort graphs and functions in a model.""" | ||
|
||
def call(self, model: ir.Model) -> ir.passes.PassResult: | ||
original_nodes = list(model.graph) | ||
model.graph.sort() | ||
sorted_nodes = list(model.graph) | ||
for function in model.functions.values(): | ||
original_nodes.extend(function) | ||
function.sort() | ||
sorted_nodes.extend(function) | ||
|
||
# Compare node orders to determine if any changes were made | ||
modified = False | ||
for node, new_node in zip(original_nodes, sorted_nodes): | ||
if node is not new_node: | ||
modified = True | ||
break | ||
return ir.passes.PassResult(model=model, modified=modified) |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
"""Unit tests for the TopologicalSortPass.""" | ||
|
||
import unittest | ||
|
||
from onnxscript import ir | ||
from onnxscript.ir.passes.common import topological_sort | ||
|
||
|
||
class TopologicalSortPassTest(unittest.TestCase): | ||
def setUp(self): | ||
self.node_a = ir.node("A", inputs=[], name="node_a") | ||
self.node_b = ir.node("B", inputs=self.node_a.outputs, name="node_b") | ||
self.node_c = ir.node("C", inputs=self.node_b.outputs, name="node_c") | ||
|
||
def test_topological_sort_modified_true(self): | ||
graph = ir.Graph( | ||
inputs=self.node_a.inputs, | ||
outputs=self.node_c.outputs, | ||
nodes=[self.node_c, self.node_b, self.node_a], # Unsorted nodes | ||
name="test_graph", | ||
) | ||
model = ir.Model(graph, ir_version=10) | ||
result = topological_sort.TopologicalSortPass()(model) | ||
self.assertTrue(result.modified) | ||
self.assertEqual( | ||
tuple(result.model.graph), | ||
(self.node_a, self.node_b, self.node_c), | ||
) | ||
|
||
def test_topological_sort_modified_false(self): | ||
"""Test that modified is False when the input model is already sorted.""" | ||
sorted_graph = ir.Graph( | ||
inputs=self.node_a.inputs, | ||
outputs=self.node_c.outputs, | ||
nodes=[self.node_a, self.node_b, self.node_c], # Sorted nodes | ||
name="test_graph", | ||
) | ||
sorted_model = ir.Model(sorted_graph, ir_version=10) | ||
result = topological_sort.TopologicalSortPass()(sorted_model) | ||
self.assertFalse(result.modified) | ||
self.assertEqual( | ||
tuple(result.model.graph), | ||
(self.node_a, self.node_b, self.node_c), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
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.