[precompile] Add CompilePackage to serialize dynamo states. (#155118)

Adding a per torch.compile() object CompilePackage which tracks dynamo artifact. CompilePackage is considered a low level component and should not be directly exposed to end users. It has the following interface:

1. `CompilePackage.__init__()` which optionally takes previously serialized dynamo states.
     a. when `dynamo` argument is None, it will contruct a brand new CompilePackage object.
     b. when `dynamo` argument is not None, it will load a pre-compiled dynamo state.
2. `package.save()` which dumps the dynamo states into _DynamoCacheEntry.
3. `package.install(backends)` which will handle all the side-effectful global scope updates with compiled functions and resume functions.

This diff focus on making the low level mechanism for precompile. It will be left to upper level interface to use these API to build more user-facing frontend.

Differential Revision: [D75956538](https://our.internmc.facebook.com/intern/diff/D75956538/)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/155118
Approved by: https://github.com/jamesjwu

Co-authored-by: James Wu <jjwu@meta.com>
This commit is contained in:
James Wu
2025-06-12 09:46:38 -07:00
committed by PyTorch MergeBot
parent 670dab6c63
commit b2fc9cfea1
11 changed files with 618 additions and 14 deletions

View File

@ -213,6 +213,7 @@ def debug_insert_nops(
global_scope=globals(),
f_code=frame.f_code,
torch_function_mode_stack=[],
package=None,
)
return wrap_guarded_code(