Skip to content

[dynamo] skip tracing functions registered in sys.monitoring#158171

Closed
williamwen42 wants to merge 7 commits intogh/williamwen42/262/basefrom
gh/williamwen42/262/head
Closed

[dynamo] skip tracing functions registered in sys.monitoring#158171
williamwen42 wants to merge 7 commits intogh/williamwen42/262/basefrom
gh/williamwen42/262/head

Conversation

@williamwen42
Copy link
Member

@williamwen42 williamwen42 commented Jul 12, 2025

Stack from ghstack (oldest at bottom):

Fixes #158164

This was fixed by applying skip_code_recursive to any function registered to sys.monitoring (via PyThreadState_GET()->interp->monitoring_callables). This check is done whenever we attempt to set the eval frame callback from Python.

Microbenchmark: benchmarks/dynamo/microbenchmarks/overheads.py:

BEFORE:

requires_grad=False
eager    7.1us (warmup=0.0s)
compiled 24.6us (warmup=10.0s)

requires_grad=True
eager    8.9us (warmup=0.0s)
compiled 57.8us (warmup=0.1s)

inference_mode()
eager    6.5us (warmup=0.0s)
compiled 23.4us (warmup=0.1s)

AFTER:

requires_grad=False
eager    7.0us (warmup=0.0s)
compiled 23.2us (warmup=15.2s)

requires_grad=True
eager    9.0us (warmup=0.0s)
compiled 55.1us (warmup=0.1s)

inference_mode()
eager    6.4us (warmup=0.0s)
compiled 22.2us (warmup=0.1s)

Followup thought: how do we let users know that a frame is skipped because the code object is a callable registered to sys.monitoring? (or any other reason?)

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

Differential Revision: D78530528

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 12, 2025

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ You can merge normally! (2 Unrelated Failures)

As of commit 21ea843 with merge base 7b72e5b (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

williamwen42 added a commit that referenced this pull request Jul 12, 2025
ghstack-source-id: f2284d2
Pull Request resolved: #158171
Fixes #158164

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

[ghstack-poisoned]
williamwen42 added a commit that referenced this pull request Jul 18, 2025
ghstack-source-id: d515405
Pull Request resolved: #158171
Fixes #158164

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

[ghstack-poisoned]
williamwen42 added a commit that referenced this pull request Jul 18, 2025
ghstack-source-id: 0196781
Pull Request resolved: #158171
@williamwen42 williamwen42 added topic: not user facing topic category module: python version Issues related to specific Python versions labels Jul 18, 2025
@williamwen42
Copy link
Member Author

@williamwen42 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 18, 2025
@williamwen42 williamwen42 added the keep-going Don't stop on first failure, keep running tests until the end label Jul 18, 2025
@williamwen42 williamwen42 changed the title [dynamo] turn off sys.monitoring if eval_frame is set [dynamo] skip tracing functions registered in sys.monitoring Jul 21, 2025
Fixes #158164

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

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

[ghstack-poisoned]
williamwen42 added a commit that referenced this pull request Jul 21, 2025
ghstack-source-id: ef28467
Pull Request resolved: #158171
Fixes #158164

This was fixed by applying `skip_code_recursive` to any function registered to `sys.monitoring` (via PyThreadState_GET()->interp->monitoring_callables`). This check is done whenever we attempt to set the eval frame callback from Python.

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

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

[ghstack-poisoned]
williamwen42 added a commit that referenced this pull request Jul 21, 2025
ghstack-source-id: 09870cc
Pull Request resolved: #158171
@williamwen42
Copy link
Member Author

@williamwen42 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Fixes #158164

This was fixed by applying `skip_code_recursive` to any function registered to `sys.monitoring` (via `PyThreadState_GET()->interp->monitoring_callables`). This check is done whenever we attempt to set the eval frame callback from Python.

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

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

[ghstack-poisoned]
williamwen42 added a commit that referenced this pull request Jul 21, 2025
ghstack-source-id: 0cef2d6
Pull Request resolved: #158171
Fixes #158164

This was fixed by applying `skip_code_recursive` to any function registered to `sys.monitoring` (via `PyThreadState_GET()->interp->monitoring_callables`). This check is done whenever we attempt to set the eval frame callback from Python.

Microbenchmark: `benchmarks/dynamo/microbenchmarks/overheads.py`:

BEFORE:
```
requires_grad=False
eager    7.1us (warmup=0.0s)
compiled 24.6us (warmup=10.0s)

requires_grad=True
eager    8.9us (warmup=0.0s)
compiled 57.8us (warmup=0.1s)

inference_mode()
eager    6.5us (warmup=0.0s)
compiled 23.4us (warmup=0.1s)
```

AFTER:
```
requires_grad=False
eager    7.0us (warmup=0.0s)
compiled 23.2us (warmup=15.2s)

requires_grad=True
eager    9.0us (warmup=0.0s)
compiled 55.1us (warmup=0.1s)

inference_mode()
eager    6.4us (warmup=0.0s)
compiled 22.2us (warmup=0.1s)
```

Followup thought: how do we let users know that a frame is skipped because the code object is a callable registered to sys.monitoring? (or any other reason?)

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

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

[ghstack-poisoned]
williamwen42 added a commit that referenced this pull request Jul 21, 2025
ghstack-source-id: 89d4ab5
Pull Request resolved: #158171
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: This PR has internal changes and must be landed via Phabricator! Please try reimporting/rexporting the PR!

Details for Dev Infra team Raised by workflow job

@williamwen42
Copy link
Member Author

@pytorchbot merge

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request keep-going Don't stop on first failure, keep running tests until the end Merged module: dynamo module: python version Issues related to specific Python versions topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants