Skip to content

[easy] [Precompile] Refactor guards, improve typing#160530

Closed
jamesjwu wants to merge 3 commits intogh/jamesjwu/182/basefrom
gh/jamesjwu/182/head
Closed

[easy] [Precompile] Refactor guards, improve typing#160530
jamesjwu wants to merge 3 commits intogh/jamesjwu/182/basefrom
gh/jamesjwu/182/head

Conversation

@jamesjwu
Copy link
Contributor

@jamesjwu jamesjwu commented Aug 13, 2025

Stack from ghstack (oldest at bottom):

Purely a refactor, improve typing and get rid of some type errors. Make certain fields as nonnull, since in general it's not empty.

The goal of this stack of PRs is to move the save/load logic of guard serialization into separate, flat phases, instead of being embedded in guard creation. This way, we can put a try/catch around it and fail safely if certain guards are not serializable.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames @Lucaskabela

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Aug 13, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/160530

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 4f826dd with merge base ee1b041 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@jamesjwu jamesjwu changed the title [Precompile] Refactor guards, improve typing [easy] [Precompile] Refactor guards, improve typing Aug 13, 2025
[ghstack-poisoned]
@jamesjwu jamesjwu marked this pull request as ready for review August 13, 2025 17:19
@jamesjwu jamesjwu added the topic: not user facing topic category label Aug 13, 2025
current_device: Optional[torch.device]
global_state_guard: torch._C._dynamo.guards.GlobalStateGuard
name_of_builtins_dict_key_in_fglobals: Optional[str] = None
_guards: torch._guards.GuardsSet
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is basically never None. The only time it was None was in OutputGraph.init initially, and gets properly initialized within the same function. So we refactor it so it's always non null (saving an empty value initially in that one callsite)

Copy link
Contributor

@Lucaskabela Lucaskabela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working to clean up the typing and refactor the serialization; one minor suggestion which may or may not have already tried, otherwise LGTM!

for source in output_graph.guard_on_key_order:
prune_variable(source)

def normalize_create_fn(x: Any) -> Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least, this always takes in an returns a callable right?

[ghstack-poisoned]
@jamesjwu
Copy link
Contributor Author

Add better typing to create_fn, as well as normalize_create_fn

@jamesjwu
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 17, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

can-gaa-hou pushed a commit to can-gaa-hou/pytorch that referenced this pull request Aug 22, 2025
Purely a refactor, improve typing and get rid of some type errors. Make certain fields as nonnull, since in general it's not empty.

The goal of this stack of PRs is to move the save/load logic of guard serialization into separate, flat phases, instead of being embedded in guard creation. This way, we can put a try/catch around it and fail safely if certain guards are not serializable.

Pull Request resolved: pytorch#160530
Approved by: https://github.com/Lucaskabela, https://github.com/Skylion007
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
Purely a refactor, improve typing and get rid of some type errors. Make certain fields as nonnull, since in general it's not empty.

The goal of this stack of PRs is to move the save/load logic of guard serialization into separate, flat phases, instead of being embedded in guard creation. This way, we can put a try/catch around it and fail safely if certain guards are not serializable.

Pull Request resolved: pytorch#160530
Approved by: https://github.com/Lucaskabela, https://github.com/Skylion007
@github-actions github-actions bot deleted the gh/jamesjwu/182/head branch September 17, 2025 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants