Skip to content

Add test for TopologicalSortPass on functions #2198

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
Apr 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions onnxscript/ir/passes/common/topological_sort_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ def test_topological_sort_modified_false(self):
(self.node_a, self.node_b, self.node_c),
)

def test_topological_sort_on_functions(self):
"""Test that TopologicalSortPass works on functions in a model."""
# Create a function with unsorted nodes
func_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
)
function = ir.Function(
domain="test_domain",
name="test_function",
graph=func_graph,
attributes=[],
)

# Create a model with the function
graph = ir.Graph(
inputs=[],
outputs=[],
nodes=[],
name="test_graph",
)
model = ir.Model(graph, ir_version=10, functions=[function])

# Apply the TopologicalSortPass
result = topological_sort.TopologicalSortPass()(model)

# Verify that the nodes in the function are sorted
sorted_func_nodes = (self.node_a, self.node_b, self.node_c)
self.assertTrue(result.modified)
self.assertEqual(
tuple(result.model.functions[function.identifier()]),
sorted_func_nodes,
)


if __name__ == "__main__":
unittest.main()
Loading