From c1e1365c75c9c80e0ec94e43c285ff5e5b9f6100 Mon Sep 17 00:00:00 2001 From: shaahji <96227573+shaahji@users.noreply.github.com> Date: Mon, 13 Jan 2025 22:28:50 -0800 Subject: [PATCH] Fix graph vertex ordering during topological ordering (#1546) ## Fix graph vertex ordering during topological ordering Previous implementation returned vertices in reverse order even when there are no dependencies in the graph. Iterate the vertices in reverse order so input order can be retained. ## Checklist before requesting a review - [ ] Add unit tests for this change. - [ ] Make sure all tests can pass. - [ ] Update documents if necessary. - [ ] Lint and apply fixes to your code by running `lintrunner -a` - [ ] Is this a user-facing change? If yes, give a description of this change to be included in the release notes. - [ ] Is this PR including examples changes? If yes, please remember to update [example documentation](https://github.com/microsoft/Olive/blob/main/docs/source/examples.md) in a follow-up PR. ## (Optional) Issue link --- olive/strategy/utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/olive/strategy/utils.py b/olive/strategy/utils.py index ab8bd82e9..4839b7701 100644 --- a/olive/strategy/utils.py +++ b/olive/strategy/utils.py @@ -54,7 +54,11 @@ def topological_sort(self): visited = set() order = [] - for v in self.vertices: + # Since the dependee vertex is inserted in front, iterate the vertices in + # reverse order to retain the relative order of vertices in the graph. + # Without it the graph vertices are reversed even in cases where no + # dependency exist. + for v in reversed(self.vertices): if v not in visited: self._topological_sort_util(v, visited, order)