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

Skip to content

Implement thread-local config with contextvars#173568

Closed
rtimpe wants to merge 12 commits into
gh/rtimpe/44/basefrom
gh/rtimpe/44/head
Closed

Implement thread-local config with contextvars#173568
rtimpe wants to merge 12 commits into
gh/rtimpe/44/basefrom
gh/rtimpe/44/head

Conversation

@rtimpe

@rtimpe rtimpe commented Jan 27, 2026

Copy link
Copy Markdown
Collaborator

[ghstack-poisoned]
rtimpe added a commit that referenced this pull request Jan 27, 2026
This is an alternative approach to #118387 using config vars


ghstack-source-id: 43801c9
Pull-Request: #173568
@pytorch-bot

pytorch-bot Bot commented Jan 27, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 6e3c08b with merge base 4026771 (image):
💚 Looks good so far! There are no failures yet. 💚

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

[ghstack-poisoned]
@github-actions github-actions Bot deleted a comment from pytorch-bot Bot Jan 27, 2026
rtimpe added a commit that referenced this pull request Jan 27, 2026
This is an alternative approach to #118387 using config vars

ghstack-source-id: 5fe67b8
Pull-Request: #173568
[ghstack-poisoned]
rtimpe added a commit that referenced this pull request Jan 28, 2026
This is an alternative approach to #118387 using context vars

ghstack-source-id: 244c0d0
Pull-Request: #173568
[ghstack-poisoned]
rtimpe added a commit that referenced this pull request Jan 28, 2026
This is an alternative approach to #118387 using context vars

ghstack-source-id: 1e7fc7c
Pull-Request: #173568



speech_transformer,pass,16

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.

I'm not sure why this is passing now, but I was getting CI failures before since the expected result was missing

return cls._instance

def submit(self, fn, *args, **kwargs):
ctx = contextvars.copy_context()

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.

This is needed since the spawned threads need access to the config. If we want to avoid doing this manually, another option would be to only use contextvars in python 3.14 (or even 3.14t), where PYTHON_THREAD_INHERIT_CONTEXT is available

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like a good idea to me. Even with PYTHON_THREAD_INHERIT_CONTEXT, ThreadPoolExecutor tasks don't inherit the context from their submitter -- the threads inherit their context from the the thread that created the pool. (This is something we are considering changing for 3.15)

@rtimpe rtimpe marked this pull request as ready for review January 29, 2026 01:12
@colesbury

Copy link
Copy Markdown
Contributor

This looks like a good approach to me, but I'm not very familiar with the code

@ezyang

ezyang commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

Whoever accepts and shepherds this, please note the last time we switched config to thread local it caused a SEV, so you're going to have to watch deployment very carefully.

@rtimpe

rtimpe commented Feb 10, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased gh/rtimpe/44/orig onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/173568)

pytorchmergebot pushed a commit that referenced this pull request Feb 10, 2026
This is an alternative approach to #118387 using context vars

ghstack-source-id: a190dd3
Pull-Request: #173568
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
This reverts commit c904bdc.

Reverted pytorch#173568 on behalf of https://github.com/georgehong due to tests failing with torch._dynamo hit config.recompile_limit (8) ([comment](pytorch#173568 (comment)))
[ghstack-poisoned]
rtimpe added a commit that referenced this pull request Apr 1, 2026
This is an alternative approach to #118387 using context vars

ghstack-source-id: 4215140
Pull-Request: #173568
rtimpe added a commit that referenced this pull request Apr 1, 2026
This is an alternative approach to #118387 using context vars

ghstack-source-id: 4215140
Pull-Request: #173568
rtimpe added a commit that referenced this pull request Apr 1, 2026
This is an alternative approach to #118387 using context vars

ghstack-source-id: 4215140
Pull-Request: #173568
@rtimpe

rtimpe commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

pytorchmergebot pushed a commit that referenced this pull request Apr 1, 2026
This was triggered by an internal test failure on #173568 (thanks to @williamwen42
for help debugging).  See https://gist.github.com/williamwen42/a31bee4f3175c0f540d4170c2dd7ceae for more context/detail

Authored with Claude.

Differential Revision: [D99142314](https://our.internmc.facebook.com/intern/diff/D99142314)
Pull Request resolved: #178950
Approved by: https://github.com/williamwen42
xuhancn pushed a commit to xuhancn/pytorch that referenced this pull request Apr 2, 2026
This is an alternative approach to pytorch#118387 using context vars

Pull Request resolved: pytorch#173568
Approved by: https://github.com/williamwen42
pytorch-bot Bot pushed a commit that referenced this pull request Apr 2, 2026
This reverts commit c904bdc.

Reverted #173568 on behalf of https://github.com/georgehong due to tests failing with torch._dynamo hit config.recompile_limit (8) ([comment](#173568 (comment)))
IvanKobzarev pushed a commit to IvanKobzarev/pytorch that referenced this pull request Apr 3, 2026
This was triggered by an internal test failure on pytorch#173568 (thanks to @williamwen42
for help debugging).  See https://gist.github.com/williamwen42/a31bee4f3175c0f540d4170c2dd7ceae for more context/detail

Authored with Claude.

Differential Revision: [D99142314](https://our.internmc.facebook.com/intern/diff/D99142314)
Pull Request resolved: pytorch#178950
Approved by: https://github.com/williamwen42
IvanKobzarev pushed a commit to IvanKobzarev/pytorch that referenced this pull request Apr 3, 2026
This is an alternative approach to pytorch#118387 using context vars

Pull Request resolved: pytorch#173568
Approved by: https://github.com/williamwen42
ghstack dependencies: pytorch#178950
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
This is an alternative approach to pytorch#118387 using context vars

Pull Request resolved: pytorch#173568
Approved by: https://github.com/williamwen42
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
This reverts commit c904bdc.

Reverted pytorch#173568 on behalf of https://github.com/georgehong due to tests failing with torch._dynamo hit config.recompile_limit (8) ([comment](pytorch#173568 (comment)))
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
This was triggered by an internal test failure on pytorch#173568 (thanks to @williamwen42
for help debugging).  See https://gist.github.com/williamwen42/a31bee4f3175c0f540d4170c2dd7ceae for more context/detail

Authored with Claude.

Differential Revision: [D99142314](https://our.internmc.facebook.com/intern/diff/D99142314)
Pull Request resolved: pytorch#178950
Approved by: https://github.com/williamwen42
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
This is an alternative approach to pytorch#118387 using context vars

Pull Request resolved: pytorch#173568
Approved by: https://github.com/williamwen42
ghstack dependencies: pytorch#178950
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 9, 2026
This was triggered by an internal test failure on pytorch#173568 (thanks to @williamwen42
for help debugging).  See https://gist.github.com/williamwen42/a31bee4f3175c0f540d4170c2dd7ceae for more context/detail

Authored with Claude.

Differential Revision: [D99142314](https://our.internmc.facebook.com/intern/diff/D99142314)
Pull Request resolved: pytorch#178950
Approved by: https://github.com/williamwen42
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 9, 2026
This is an alternative approach to pytorch#118387 using context vars

Pull Request resolved: pytorch#173568
Approved by: https://github.com/williamwen42
ghstack dependencies: pytorch#178950
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 10, 2026
This was triggered by an internal test failure on pytorch#173568 (thanks to @williamwen42
for help debugging).  See https://gist.github.com/williamwen42/a31bee4f3175c0f540d4170c2dd7ceae for more context/detail

Authored with Claude.

Differential Revision: [D99142314](https://our.internmc.facebook.com/intern/diff/D99142314)
Pull Request resolved: pytorch#178950
Approved by: https://github.com/williamwen42
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 10, 2026
This is an alternative approach to pytorch#118387 using context vars

Pull Request resolved: pytorch#173568
Approved by: https://github.com/williamwen42
ghstack dependencies: pytorch#178950
rtimpe added a commit that referenced this pull request Apr 17, 2026
There's potential deadlock related to _stash_obj_in_tls.  As of #173568 the
context object is stored in the calling thread TLS during _engine_run_backward.  Normally this object deallocated after
the backward pass completes, but there's a race where the autograd device thread can end up holding the last reference.
If this happens the context object gets deallocated by the device thread, which needs to acquire the GIL.  If another
thread happens to be holding the GIL while blocked on the device thread the result is a deadlock.  See
#173568 for more detail

The fix here avoids aquiring the GIL in the device thread by clearing the GraphTask's shared_ptr to the context object
during cleanup.  Since the TLS in the calling thread also has a shared_ptr, this avoids calling the SafePyObject
destructor (and therefore acquiring the GIL).  I'm not a huge fan of this approach since it introduces a manual cleanup
step, but I wasn't able to come up with something better.  Another option would be to use SafePyHandle instead of
SafePyObject and introduce a `_remove_obj_from_tls` python API.  But that makes the python side responsible for
refcounting, which feels wrong.

I'm also not sure exactly how we end up with a thread holding the GIL and also blocked on an autograd device thread.
That feels like a bug, but it seems to be triggered by Meta-internal code, so I'm not able to debug it.  All of which is
to say that this is the best approach I can come up with, but I'm very open to other suggestions.


ghstack-source-id: 81c9703
Pull-Request: #180700
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.

9 participants