pytorch
f73d9a79 - [torch][fx] Fix PassManager to not use a class variable mutable list (#89108)

Commit
2 years ago
[torch][fx] Fix PassManager to not use a class variable mutable list (#89108) Summary: I found a confusing bug in the PassManager that only happens when you instantiate one multiple times: it will use old passes and constraints! This occurs because the class-level declarations initialize it to an empty list, but the problem is that class initializers only run once, and are creating class variables. This means the same empty list was being reused every time, except after the first time it isn't empty. The empty list has to be created in `__init__` newly each time or else it'll be shared. Note that this is the same type of bug as using an empty list as a default parameter, where it'll reuse the same list pointer and not make it empty each time. The better way to do this is with either: * An immutable default parameter like an empty tuple, that you create a new list from: `self.passes = list(passes)` * Use None and then create the empty list inside `__init__` I chose the latter as it's less likely to cause a behavior change due to the changed default. Note that for immutable values like `False` and `1` this doesn't apply as you can't mutate that value for everyone. Test Plan: Added a test to ensure that the pass state is not saved. Without my change, this test would fail as it would run all of the `2 * x` passes first, then all of the `3 * x` passes. Differential Revision: D41327056 Pull Request resolved: https://github.com/pytorch/pytorch/pull/89108 Approved by: https://github.com/angelayi
Author
Committer
Parents
Loading