Skip to content

[dynamo] Add namespace for cell variables that collide with locals - #188029

Closed
rtimpe wants to merge 19 commits into
gh/rtimpe/78/basefrom
gh/rtimpe/78/head
Closed

rtimpe wants to merge 19 commits into
gh/rtimpe/78/basefrom
gh/rtimpe/78/head

Conversation

@rtimpe

@rtimpe rtimpe commented Jun 24, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Cell variables that share a name with a local variable are stored separately in localsplus in cpython. This
distinction is lost in dynamo, where all variables are stored in symbolic_locals. This doesn't matter for most
purposes, but becomes relevant with the combination of an inlined list comprehension graph break with nested graph breaks
enabled. In this case the shared entry in symbolic_locals gets set to NULL, which causes a segfault when dynamo tries
to pass the cell to the resume function.

Authored with an LLM

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (40 Unrelated Failures)

As of commit b39aa42 with merge base 12c0042 (image):

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

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

[ghstack-poisoned]
@rtimpe rtimpe added the topic: not user facing topic category label Jul 2, 2026
[ghstack-poisoned]
rtimpe added a commit that referenced this pull request Jul 3, 2026
Cell variables that share a name with a local variable are stored separately in `localsplus` in cpython.  This
distinction is lost in dynamo, where all variables are stored in `symbolic_locals`.  This doesn't matter for most
purposes, but becomes relevant with the combination of an inlined list comprehension graph break with nested graph breaks
enabled.  In this case the shared entry in `symbolic_locals` gets set to NULL, which causes a segfault when dynamo tries
to pass the cell to the resume function.

Authored with an LLM

ghstack-source-id: f957817
Pull-Request: #188029
rtimpe added a commit that referenced this pull request Jul 3, 2026
Cell variables that share a name with a local variable are stored separately in `localsplus` in cpython.  This
distinction is lost in dynamo, where all variables are stored in `symbolic_locals`.  This doesn't matter for most
purposes, but becomes relevant with the combination of an inlined list comprehension graph break with nested graph breaks
enabled.  In this case the shared entry in `symbolic_locals` gets set to NULL, which causes a segfault when dynamo tries
to pass the cell to the resume function.

Authored with an LLM

ghstack-source-id: f957817
Pull-Request: #188029
# in `symbolic_locals` would let fast-local ops (LOAD_FAST_AND_CLEAR) clobber
# the cell, so the colliding cell lives here instead. Non-colliding cells
# stay in `symbolic_locals`.
symbolic_cellvars: dict[str, VariableTracker]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could we name this symbolic_localsplus?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's not directly analogous to localsplus, since it only holds cells. I thought about having a single localsplus storage, but it would have to be an array rather than a dict keyed by variable name. That would actually be a pretty big change since we also need to reference variables by name for codegen. So I think we probably need separate storage for locals and cells, though I'm not sure if this is the best way to manage it

rtimpe added 4 commits July 7, 2026 23:25
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@rtimpe
rtimpe marked this pull request as ready for review July 8, 2026 18:35
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread torch/_dynamo/variables/builtin.py Outdated
# symbolic_cellvars registers all of the frame's cells; listing it
# second makes cell contents take precedence over a (shadowing)
# colliding fast local of the same name.
items = list(tx.symbolic_locals.items()) + list(tx.symbolic_cellvars.items())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Use itertools.chain

# when the slot doesn't hold a cell: either the name is a colliding
# free var that never lives in symbolic_locals, or the cell was
# temporarily evicted by LOAD_FAST_AND_CLEAR.
var = self.symbolic_locals.get(name)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it guaranteed that name is in symbolic_locals? on _register_cells you pop the intersection of (co_freevars, co_varnames).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No. If it's missing .get will return None and we'll fall back to symbolic_cellvars

rtimpe added 2 commits July 14, 2026 23:25
[ghstack-poisoned]
[ghstack-poisoned]
pytorchmergebot added a commit that referenced this pull request Jul 28, 2026
This reverts commit 7ac8666.

Reverted #189592 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](#188029 (comment)))
pytorchmergebot added a commit that referenced this pull request Jul 28, 2026
…ocals (#188029)"

This reverts commit 0c0aa24.

Reverted #188029 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](#188029 (comment)))
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@rtimpe your PR has been successfully reverted.

[ghstack-poisoned]
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#188029)

Cell variables that share a name with a local variable are stored separately in `localsplus` in cpython.  This
distinction is lost in dynamo, where all variables are stored in `symbolic_locals`.  This doesn't matter for most
purposes, but becomes relevant with the combination of an inlined list comprehension graph break with nested graph breaks
enabled.  In this case the shared entry in `symbolic_locals` gets set to NULL, which causes a segfault when dynamo tries
to pass the cell to the resume function.

Authored with an LLM

Pull Request resolved: pytorch#188029
Approved by: https://github.com/guilhermeleobas, https://github.com/williamwen42
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
This adds support for sentinels created outside of the compiled region and basic operations on them (identity comparsion
and attribute access).  Creating sentinels, as well as other operations, are not supported yet.  This is the minimal
amount of support needed for pytorch#185682

Generated with AI assistance

Pull Request resolved: pytorch#189592
Approved by: https://github.com/guilhermeleobas
ghstack dependencies: pytorch#188029
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Fix pygen_yf to account for virtual iterators in python 3.15

Pull Request resolved: pytorch#190312
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#188029, pytorch#189592
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
This reverts commit 0f32fe1.

Reverted pytorch#190312 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#188029 (comment)))
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
This reverts commit 7ac8666.

Reverted pytorch#189592 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#188029 (comment)))
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ocals (pytorch#188029)"

This reverts commit 0c0aa24.

Reverted pytorch#188029 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#188029 (comment)))
rtimpe added 3 commits July 29, 2026 18:09
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #190312

pytorchmergebot pushed a commit that referenced this pull request Jul 30, 2026
This adds support for sentinels created outside of the compiled region and basic operations on them (identity comparsion
and attribute access).  Creating sentinels, as well as other operations, are not supported yet.  This is the minimal
amount of support needed for #185682

Generated with AI assistance

Pull Request resolved: #189592
Approved by: https://github.com/guilhermeleobas
ghstack dependencies: #188029
pytorchmergebot pushed a commit that referenced this pull request Jul 30, 2026
Fix pygen_yf to account for virtual iterators in python 3.15

Pull Request resolved: #190312
Approved by: https://github.com/Skylion007
ghstack dependencies: #188029, #189592
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
…ytorch#188029)

Cell variables that share a name with a local variable are stored separately in `localsplus` in cpython.  This
distinction is lost in dynamo, where all variables are stored in `symbolic_locals`.  This doesn't matter for most
purposes, but becomes relevant with the combination of an inlined list comprehension graph break with nested graph breaks
enabled.  In this case the shared entry in `symbolic_locals` gets set to NULL, which causes a segfault when dynamo tries
to pass the cell to the resume function.

Authored with an LLM

Pull Request resolved: pytorch#188029
Approved by: https://github.com/guilhermeleobas, https://github.com/williamwen42
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
This adds support for sentinels created outside of the compiled region and basic operations on them (identity comparsion
and attribute access).  Creating sentinels, as well as other operations, are not supported yet.  This is the minimal
amount of support needed for pytorch#185682

Generated with AI assistance

Pull Request resolved: pytorch#189592
Approved by: https://github.com/guilhermeleobas
ghstack dependencies: pytorch#188029
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
Fix pygen_yf to account for virtual iterators in python 3.15

Pull Request resolved: pytorch#190312
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#188029, pytorch#189592
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
This reverts commit 0f32fe1.

Reverted pytorch#190312 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#188029 (comment)))
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
This reverts commit 7ac8666.

Reverted pytorch#189592 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#188029 (comment)))
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
…ocals (pytorch#188029)"

This reverts commit 0c0aa24.

Reverted pytorch#188029 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#188029 (comment)))
@github-actions
github-actions Bot deleted the gh/rtimpe/78/head branch October 28, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/dynamo Trigger jobs ran periodically on main for dynamo tests ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo open source Reverted topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants