Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add dynamo config to HOP-ify context managers #152159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: gh/soulitzer/359/base
Choose a base branch
from

Conversation

soulitzer
Copy link
Contributor

@soulitzer soulitzer commented Apr 25, 2025

Stack from ghstack (oldest at bottom):

# Note [Hopifying Context Managers]
#
# If the context manager class has been added to a opt-in dynamo config,
# we will convert it into a generic context manager HOP. When the
# HOP is later called in AOTAutograd, it will run the captured
# graph under the ctx.
#
# How does this work?
# - On enter:
#   - Enter into a new subtracer
# - The subtracer traces the body of the context manager into a graph
# - On exit:
#   - Exit the subtracer
#   - Grab the inner fx graph from the subtracer and install it onto
#     the outer fx graph
#   - Create node on the outer fx graph that calls this subgraph
#
# Some notes:
#
# 1. Determining the inputs and outputs
#
# One trickiness here is that a HOP requires a function as input,
# but, unlike functions, context managers don't have explicit inputs and
# outputs. For inputs, we rely on lifted_freevars, which the subtracer
# already tracks for ordinary HOPs. For outputs, ideally we might only
# return the outputs that are referenceable later, e.g. not temporaries,
# but doing that is hard, so instead we just return all intermediates
# and rely on AOTAutograd to trace through the HOP and DCE any
# unnecessary ops.
#
# 2. Fixing VariableTracker proxies
#
# Inserting the call to the subgraph into the outer fx graph creates
# fresh variable trackers and proxies, but the instruction translator
# still refers the original VTs, e.g. in its symbolic locals and these
# VTs still hold the inner fx graph's proxies, which is wrong.
#
# We fix this by updating the VTs to hold the new outer fx graph
# proxies (e.g. corresponding to the getitems of the call to the
# subgraph). To know what those VTs are, we maintain a mapping from
# fx graph node name to the VTs which is updated everytime a new
# TensorVariable is created.

From discussion in HOP meeting:

  • Why do we need to put the body of the ctx in a HOP? Why not just insert two nodes into the graph?
    • There are pregrad passes and we don't want to match across the boundary
  • Could there be issues for caching?
    • Yes, we store the args to reconstruct a ctx onto the GraphModule, but those args aren't necessarily hashable. In this PR we limit to allowing only constants args, though we leave an exception for AC to pass in policy_fn (we'll deal with that later).

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

Copy link

pytorch-bot bot commented Apr 25, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit 7c77df3 with merge base e2c7ae5 (image):
💚 Looks good so far! There are no failures yet. 💚

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

soulitzer added a commit that referenced this pull request Apr 25, 2025
ghstack-source-id: 20ad11e
Pull Request resolved: #152159
@soulitzer soulitzer marked this pull request as draft April 25, 2025 03:17
@soulitzer soulitzer changed the title Add dynamo config to HOPify context managers" Add dynamo config to HOPify context managers Apr 25, 2025
@pytorch-bot pytorch-bot bot temporarily deployed to upload-benchmark-results April 25, 2025 04:52 Inactive
@pytorch-bot pytorch-bot bot deployed to upload-benchmark-results April 25, 2025 04:52 Active
See large code comment for details

TODO:
- tests


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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 25, 2025
ghstack-source-id: a1f7e65
Pull Request resolved: #152159
See large code comment for details

TODO:
- tests


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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 25, 2025
ghstack-source-id: 746aa38
Pull Request resolved: #152159
See large code comment for details

TODO:
- tests


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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 25, 2025
ghstack-source-id: e06b904
Pull Request resolved: #152159
See large code comment for details

TODO:
- tests


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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 25, 2025
ghstack-source-id: 8718d10
Pull Request resolved: #152159
@soulitzer soulitzer added the topic: not user facing topic category label Apr 25, 2025
See large code comment for details

TODO:
- tests


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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 25, 2025
ghstack-source-id: 3e91e44
Pull Request resolved: #152159
See large code comment for details

TODO:
- tests


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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 30, 2025
ghstack-source-id: 2688105
Pull Request resolved: #152159
@soulitzer soulitzer marked this pull request as ready for review April 30, 2025 16:56
@soulitzer soulitzer changed the title Add dynamo config to HOPify context managers Add dynamo config to HOP-ify context managers Apr 30, 2025

```
# Note [Hopifying Context Managers]
#
# If the context manager class has been added to a opt-in dynamo config,
# we will convert it into a generic context manager HOP. When the
# HOP is later called in AOTAutograd, it will run the captured
# graph under the ctx.
#
# How does this work?
# - On enter:
#   - Enter into a new subtracer
# - The subtracer traces the body of the context manager into a graph
# - On exit:
#   - Exit the subtracer
#   - Grab the inner fx graph from the subtracer and install it onto
#     the outer fx graph
#   - Create node on the outer fx graph that calls this subgraph
#
# Some notes:
#
# 1. Determining the inputs and outputs
#
# One trickiness here is that a HOP requires a function as input,
# but, unlike functions, context managers don't have explicit inputs and
# outputs. For inputs, we rely on lifted_freevars, which the subtracer
# already tracks for ordinary HOPs. For outputs, ideally we might only
# return the outputs that are referenceable later, e.g. not temporaries,
# but doing that is hard, so instead we just return all intermediates
# and rely on AOTAutograd to trace through the HOP and DCE any
# unnecessary ops.
#
# 2. Fixing VariableTracker proxies
#
# Inserting the call to the subgraph into the outer fx graph creates
# fresh variable trackers and proxies, but the instruction translator
# still refers the original VTs, e.g. in its symbolic locals and these
# VTs still hold the inner fx graph's proxies, which is wrong.
#
# We fix this by updating the VTs to hold the new outer fx graph
# proxies (e.g. corresponding to the getitems of the call to the
# subgraph). To know what those VTs are, we maintain a mapping from
# fx graph node name to the VTs which is updated everytime a new
# TensorVariable is created.
 ```

From discussion in HOP meeting:
- Why do we need to put the body of the ctx in a HOP? Why not just insert two nodes into the graph?
   - There are pregrad passes and we don't want to match across the boundary
- Could there be issues for caching?
   - Yes, we store the args to reconstruct a ctx onto the GraphModule, but those args aren't necessarily hashable. In this PR we limit to allowing only constants args, though we leave an exception for AC to pass in policy_fn (we'll deal with that later).
 

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

[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Apr 30, 2025
ghstack-source-id: 50a6ba0
Pull Request resolved: #152159
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant