pytorch
ecf3ca00 - [fx] Separate globals assignment from code generation (#51974)

Commit
3 years ago
[fx] Separate globals assignment from code generation (#51974) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/51974 Right now, when an FX `Graph` references an external object, we will emit code like: import foo def forward(input: foo.bar.baz): ... This is problematic in a world with `torch.package`, since then name `foo.bar.baz` may reference a name from any number of packages. This PR lays the groundwork for FX-package integration by separating the resolution of external references from the genration of the function code. When generating a Graph's Python source, we keep track of all external references and assign them unique names. At the end, we have a dictionary mapping names -> actual objects. This becomes the `globals` namespace we pass to `exec` when installing the forward function in a `GraphModule`. This is nice because we can always be sure that `exec` is seeing the same objects that were referenced from the `Graph`, no import statements needed. At serialization time, we use a `ModuleEnv` to resolve the globals dict to a set of import statements that can be run to reprodce the `global` namespace. This is only used on serialiation/deserialization, and those functions are expected to check that the import statements are producing the correct results. Concretely, the code above will now look like: from foo.bar import baz as foo_bar_baz def forward(input: foo_bar_baz): ... Test Plan: Imported from OSS Reviewed By: jamesr66a Differential Revision: D26340593 Pulled By: suo fbshipit-source-id: fe247f75205d0a03fd067bdd0f95491e8edf1436
Author
suo suo
Parents
Loading