Lucas Kabela
abcb24f4de
[Dynamo][Better Engineering] Add typing annotations to guard and source ( #158397 )
...
As part of better engineering week, we would like to improve out type support to improve dev experience in dynamo
This PR adds strict typing support to a critical set of files for dynamo, `source.py` and the base `_guards.py`
Running
```
mypy torch/_dynamo/source.py torch/_guards.py --linecount-report /tmp/coverage_log
```
| -------- | Lines Unannotated | Lines Total | % lines covered | Funcs Unannotated | Funcs Total | % funcs covered |
| -------- | ------- | -------- | ------- | ------- | ------- | ------- |
| Main | 1227 | 2208 | 55.57% | 207 | 362 | 57.18% |
| This PR | 2217 | 2217 | 100.00% | 362 | 362 | 100.00% |
| Delta | +990 | +9 | +44.43% | +155 | 0 | +42.82% |
Pull Request resolved: https://github.com/pytorch/pytorch/pull/158397
Approved by: https://github.com/anijain2305
2025-07-24 15:55:18 +00:00
Animesh Jain
9b4d938f04
[dynamo][fsdp] Consistent behavior of int attributes ( #157262 )
...
Reimpl of https://github.com/pytorch/pytorch/pull/150954
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157262
Approved by: https://github.com/bdhirsh
2025-07-22 11:26:54 +00:00
Lucas Kabela
9498d95b9c
[Dynamo][BetterEngineering] Type trace_rules.py ( #158679 )
...
As part of better engineering week, we would like to improve out type support to improve dev experience in dynamo
This PR adds strict typing support to a core file, `trace_rules.py`
Running
```
mypy torch/_dynamo/trace_rules.py --linecount-report /tmp/coverage_log
```
| -------- | Lines Unannotated | Lines Total | % lines covered | Funcs Unannotated | Funcs Total | % funcs covered |
| -------- | ------- | -------- | ------- | ------- | ------- | ------- |
| Main | 2564 | 3997 | 64.15% | 34 | 53 | 64.15% |
| This PR | 4022 | 4022 | 100.00% | 53 | 53 | 100.00% |
| Delta | +1458 | +25 | +35.85% | +19 | 0 | +35.85% |
Pull Request resolved: https://github.com/pytorch/pytorch/pull/158679
Approved by: https://github.com/williamwen42
2025-07-21 22:12:59 +00:00
Ruben Rodriguez Buchillon
b1a0c34dd3
[pt2 event logging] add configurable prefix ( #157678 )
...
Summary:
# Why
make experiments easier to find
# What
- dynamo config to provide a prefix
- use the prefix when sending data to scuba through the self.id_ field
Test Plan:
```
# code edited to set the prefix as `coconutruben-02`
buck2 run mode/opt scripts/coconutruben/torchmm:experiment 2>&1 | tee /tmp/epx040
```
on scuba
```
| autotune_dtypes | autotune_offset | autotune_shape | autotune_strides | event | run_id |
| -----| -----| -----| -----| -----| ----- |
| "torch.float16, torch.float16" | "0, 0" | "4096x3008, 3008x2048" | "[3008, 1], [2048, 1]" | "mm_template_autotuning" | "coconutruben-02-e6bdccc5-6dcf-4d68-9a04-b34f2c6d94fd" |
| "torch.float16, torch.float16" | "0, 0" | "4096x3008, 3008x2048" | "[3008, 1], [2048, 1]" | "mm_template_autotuning" | "coconutruben-02-14165153-5842-4eaa-9e6c-3b0cbc016375" |
```
Rollback Plan:
Differential Revision: D77837550
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157678
Approved by: https://github.com/stashuk-olek
2025-07-21 20:41:03 +00:00
Benjamin Glass
22920c9138
Grab bag of (mostly) typing improvements ( #158075 )
...
Collects some scattershot improvements made while attempting to enable training for AOTInductor. Non-typing changes are:
1. Swapping a few custom searches for the output node in an FX graph for calling `graph.output_node()`.
2. Removing two unused parameters from `torch.export._unlift._unlift`.
3. Switching handles to constants in `cpp_wrapper_cpu` to use C++ references for memory efficiency.
4. Cleaning out unused, unexported imports from `torch/export/__init__.py`, and adding one missing export to `__all__`.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/158075
Approved by: https://github.com/Skylion007
2025-07-21 19:17:01 +00:00
Michael Lazos
89850bbc07
[Dynamo] Use proper sources for constructing dataclass defaults ( #157993 )
...
Partially fixes https://github.com/pytorch/pytorch/issues/154009
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157993
Approved by: https://github.com/williamwen42 , https://github.com/anijain2305
2025-07-18 21:51:40 +00:00
Boyuan Feng
94995eba07
[Log] add a hook for recompile user context ( #157961 )
...
Users may want compile-related but customized logging info to dynamo_compile. One example is to logging the current training iteration index when recompilation happens. In general, current training iteration index is not available to compiler, since the same compiled function may be called multiple times in the same training iteration. The user could provide the training iteration index in a user hook where torch.compile logs it when recompilation happens.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157961
Approved by: https://github.com/masnesral
2025-07-11 03:41:33 +00:00
PyTorch MergeBot
e517066f41
Revert "[dynamo][fsdp] Consistent behavior of int attributes ( #157262 )"
...
This reverts commit 178fe7aa98 .
Reverted https://github.com/pytorch/pytorch/pull/157262 on behalf of https://github.com/huydhn due to This fails some internal tests and needs to be relanded ([comment](https://github.com/pytorch/pytorch/pull/157262#issuecomment-3059463896 ))
2025-07-10 23:11:18 +00:00
Raymond Li
82765dad16
Fix logging of config_suppress_errors and config_inline_inbuilt_nn_modules ( #157947 )
...
Currently ~50% of the time we fail or crash before logging metrics, so moving where this is logged will let us have more comprehensive (less-null) data.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157947
Approved by: https://github.com/masnesral , https://github.com/jovianjaison
2025-07-10 12:05:43 +00:00
Animesh Jain
178fe7aa98
[dynamo][fsdp] Consistent behavior of int attributes ( #157262 )
...
Reimpl of https://github.com/pytorch/pytorch/pull/150954
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157262
Approved by: https://github.com/bdhirsh
2025-07-08 22:11:33 +00:00
Guilherme Leobas
e49acfc5c5
[list] Raise exception in invalid list method call ( #156148 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156148
Approved by: https://github.com/zou3519
ghstack dependencies: #153969
2025-07-07 14:51:10 +00:00
Guilherme Leobas
0e7f02fe2e
[Dynamo] [FrozensetSubclass] Add support for user defined frozensets ( #154263 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154263
Approved by: https://github.com/williamwen42
ghstack dependencies: #153150 , #152991 , #154539 , #153553 , #154063 , #154064 , #154065 , #154066
2025-07-04 00:46:05 +00:00
Guilherme Leobas
22abe6ded4
[Dynamo] [SetSubclass] Add support for user defined sets ( #153553 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153553
Approved by: https://github.com/williamwen42 , https://github.com/zou3519
ghstack dependencies: #153150 , #152991 , #154539
2025-07-04 00:45:25 +00:00
Guilherme Leobas
e7167dbacf
[Set] Support sets in VariableBuilder ( #153150 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153150
Approved by: https://github.com/zou3519
2025-07-04 00:45:03 +00:00
PyTorch MergeBot
8c0df6fe17
Revert "[dynamo][fsdp] Consistent behavior of int attributes ( #157262 )"
...
This reverts commit 42b48ee672 .
Reverted https://github.com/pytorch/pytorch/pull/157262 on behalf of https://github.com/jeanschmidt due to Newly introduced tests are red in internal runs, check D77593713 ([comment](https://github.com/pytorch/pytorch/pull/157262#issuecomment-3026944993 ))
2025-07-02 08:30:39 +00:00
Animesh Jain
42b48ee672
[dynamo][fsdp] Consistent behavior of int attributes ( #157262 )
...
Reimpl of https://github.com/pytorch/pytorch/pull/150954
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157262
Approved by: https://github.com/bdhirsh
2025-06-30 22:32:52 +00:00
William Wen
dcb8982969
[dynamo] move error_on_graph_break out of config ( #156762 )
...
error_on_graph_break doesn't need to be in config, so we move it out. It should make the functorch_maml_omniglot regression less severe.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156762
Approved by: https://github.com/jansel
ghstack dependencies: #154283 , #154289 , #154782
2025-06-26 21:40:38 +00:00
Xuehai Pan
1b2146fc6d
[BE][4/16] fix typos in torch/ (torch/_dynamo/) ( #156314 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156314
Approved by: https://github.com/jingsh
ghstack dependencies: #156313
2025-06-23 02:57:19 +00:00
PyTorch MergeBot
5b427c92a8
Revert "[BE][4/16] fix typos in torch/ (torch/_dynamo/) ( #156314 )"
...
This reverts commit ead741c5fb .
Reverted https://github.com/pytorch/pytorch/pull/156314 on behalf of https://github.com/atalman due to export/test_torchbind.py::TestCompileTorchbind::test_compile_error_on_input_aliasing_contents_backend_aot_eager [GH job link](https://github.com/pytorch/pytorch/actions/runs/15804799771/job/44548489912 ) [HUD commit link](c95f7fa874 ) ([comment](https://github.com/pytorch/pytorch/pull/156313#issuecomment-2994171213 ))
2025-06-22 12:31:57 +00:00
Xuehai Pan
ead741c5fb
[BE][4/16] fix typos in torch/ (torch/_dynamo/) ( #156314 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156314
Approved by: https://github.com/jingsh
ghstack dependencies: #156313
2025-06-22 08:43:18 +00:00
soulitzer
554b568040
Add internal use only utility to allow externally visible side effects within HOPs ( #155715 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155715
Approved by: https://github.com/zou3519
2025-06-21 03:55:28 +00:00
James Wu
b2fc9cfea1
[precompile] Add CompilePackage to serialize dynamo states. ( #155118 )
...
Adding a per torch.compile() object CompilePackage which tracks dynamo artifact. CompilePackage is considered a low level component and should not be directly exposed to end users. It has the following interface:
1. `CompilePackage.__init__()` which optionally takes previously serialized dynamo states.
a. when `dynamo` argument is None, it will contruct a brand new CompilePackage object.
b. when `dynamo` argument is not None, it will load a pre-compiled dynamo state.
2. `package.save()` which dumps the dynamo states into _DynamoCacheEntry.
3. `package.install(backends)` which will handle all the side-effectful global scope updates with compiled functions and resume functions.
This diff focus on making the low level mechanism for precompile. It will be left to upper level interface to use these API to build more user-facing frontend.
Differential Revision: [D75956538](https://our.internmc.facebook.com/intern/diff/D75956538/ )
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155118
Approved by: https://github.com/jamesjwu
Co-authored-by: James Wu <jjwu@meta.com>
2025-06-13 13:54:10 +00:00
Oguz Ulgen
d1947a8707
Migrate from lru_cache to cache ( #155613 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155613
Approved by: https://github.com/ezyang
ghstack dependencies: #155612
2025-06-11 19:44:18 +00:00
Joel Schlosser
c4b93e6579
Replace frame_traced_fn hook with get_traced_code() util ( #155249 )
...
#153622 introduced a hook for getting the relevant code objects after frame tracing. The idea is to have vLLM use this instead of monkey-patching `inline_call_()` to determine the source code files to hash. Unfortunately, the hook runs too late; the vLLM backend needs access to the set of source code filenames while it's running.
This PR replaces the newly-added hook with a utility function that a backend can call to get this information. I've made the change in vLLM and can verify that this allows the information to be queried at the right time.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155249
Approved by: https://github.com/zou3519
2025-06-10 22:40:58 +00:00
Animesh Jain
067fd0b3ab
[dynamo][cleanup] Simplify disabling of the helper functions on tensor properties ( #155259 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155259
Approved by: https://github.com/zhxchen17
2025-06-06 19:44:40 +00:00
Animesh Jain
271ca679a8
[reland][dynamo] Record the pre-graph bytecode using fast record function event ( #154974 )
...
reland of https://github.com/pytorch/pytorch/pull/154769
@diff-train-skip-merge
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154974
Approved by: https://github.com/Lucaskabela , https://github.com/jansel
2025-06-06 13:11:03 +00:00
PyTorch MergeBot
e01fde8213
Revert "[reland][dynamo] Record the pre-graph bytecode using fast record function event ( #154974 )"
...
This reverts commit bee9c70c5d .
Reverted https://github.com/pytorch/pytorch/pull/154974 on behalf of https://github.com/malfet due to Broke inductor tests, see 3c72b9fd8f/1 ([comment](https://github.com/pytorch/pytorch/pull/154974#issuecomment-2944370617 ))
2025-06-05 13:36:21 +00:00
Animesh Jain
bee9c70c5d
[reland][dynamo] Record the pre-graph bytecode using fast record function event ( #154974 )
...
reland of https://github.com/pytorch/pytorch/pull/154769
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154974
Approved by: https://github.com/Lucaskabela , https://github.com/jansel
2025-06-05 07:25:04 +00:00
Animesh Jain
c881f2ddf3
[reland][dynamo] Mark a vt unspecialized nn module variable source earlier ( #155099 )
...
Reland of https://github.com/pytorch/pytorch/pull/154780
Fixes #ISSUE_NUMBER
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155099
Approved by: https://github.com/williamwen42
2025-06-04 23:05:36 +00:00
PyTorch MergeBot
a99a01a677
Revert "[dynamo] Mark a vt unspecialized nn module variable source earlier ( #154780 )"
...
This reverts commit cc96febb97 .
Reverted https://github.com/pytorch/pytorch/pull/154780 on behalf of https://github.com/seemethere due to This fails internal testing see, https://fburl.com/diff/b0yuxk4w ([comment](https://github.com/pytorch/pytorch/pull/154780#issuecomment-2940381691 ))
2025-06-04 15:03:34 +00:00
Animesh Jain
cc96febb97
[dynamo] Mark a vt unspecialized nn module variable source earlier ( #154780 )
...
I am working on providing some skip guard helper functions to allow users to reduce guard overhead. This is a refactor to allow that.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154780
Approved by: https://github.com/StrongerXi , https://github.com/jansel
2025-06-03 19:19:47 +00:00
PyTorch MergeBot
a7e496a896
Revert "[dynamo] Record the pre-graph bytecode using fast record function event ( #154769 )"
...
This reverts commit 409c396a48 .
Reverted https://github.com/pytorch/pytorch/pull/154769 on behalf of https://github.com/seemethere due to This fails internal tests see [fburl.com/diff/67gyp7gp](https://fburl.com/diff/67gyp7gp ) ([comment](https://github.com/pytorch/pytorch/pull/154769#issuecomment-2933629894 ))
2025-06-03 06:13:49 +00:00
Animesh Jain
409c396a48
[dynamo] Record the pre-graph bytecode using fast record function event ( #154769 )
...

Adds another event in the profiler traces. This can help us find models where pre-graph bytecode is very expensive.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154769
Approved by: https://github.com/zou3519 , https://github.com/williamwen42 , https://github.com/StrongerXi , https://github.com/jansel
2025-06-02 22:33:27 +00:00
Aaron Gokaslan
b6b9311f4f
[BE][Ez]: Fix typo in dynamo utils #154639 ( #154748 )
...
Fixes a typo in #154639
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154748
Approved by: https://github.com/ngimel
2025-05-30 18:39:01 +00:00
Aaron Gokaslan
2120eeb8de
[BE][Ez]: Improve dynamo utils typing with TypeIs and TypeGuard ( #154639 )
...
Adds some additional TypeIs and TypeGuard to some _dynamo utils for additional type narrowing
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154639
Approved by: https://github.com/jansel
2025-05-30 18:09:50 +00:00
Ryan Guo
8002d22ce3
[dynamo] Trace into descriptor with __set__ ( #154176 )
...
As title, this patch basically implements
https://github.com/python/cpython/blob/3.11/Objects/object.c#L1371-L1452 ,
and make the `__get__` handling more robust.
I ran into this while fixing #133762 .
Differential Revision: [D75488090](https://our.internmc.facebook.com/intern/diff/D75488090 )
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154176
Approved by: https://github.com/jansel
2025-05-30 16:14:37 +00:00
James Wu
bb17f9c98b
[AOTAutogradCache] Fix CHROMIUM_EVENT_LOG being none ( #154258 )
...
It turns out if you import something that's None at import time in python, and later update the value, the one you imported stays none:
```
import torch
from torch._dynamo.utils import CHROMIUM_EVENT_LOG
class Foo:
pass
torch._dynamo.utils.CHROMIUM_EVENT_LOG = Foo()
print(CHROMIUM_EVENT_LOG) # None
```
This fixes teh bug so we get AOTAUtogradCache instant events again
Differential Revision: [D75305770](https://our.internmc.facebook.com/intern/diff/D75305770/ )
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154258
Approved by: https://github.com/oulgen
2025-05-23 21:53:31 +00:00
PyTorch MergeBot
3443627e07
Revert "[BE]: Enable RUFF TRY400 rule - log.exception ( #153473 )"
...
This reverts commit 4f4ecc583e .
Reverted https://github.com/pytorch/pytorch/pull/153473 on behalf of https://github.com/jeanschmidt due to seems to have broken internal signals, @albanD may I count on you to help the author merge his PR? D74837988 ([comment](https://github.com/pytorch/pytorch/pull/153473#issuecomment-2886017075 ))
2025-05-16 08:29:26 +00:00
Aaron Gokaslan
4f4ecc583e
[BE]: Enable RUFF TRY400 rule - log.exception ( #153473 )
...
Change logging.error to logging.exception to log additional information when relevant. A few places have slipped in logging.errors in try except since I last did a clean up here and the rule is stabilized so I am enabling it codebase wide. I have NOQA'd much of our custom exception stack trace handling for RPC calls and distributed and tried to a fix a few errors based on whether we immediately reraised it or if we didn't print any exception handling where it could be useful.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153473
Approved by: https://github.com/albanD , https://github.com/cyyever
2025-05-15 13:36:59 +00:00
Sam Larsen
dde705864a
Fix test broken by D73809989 ( #153413 )
...
Summary: I forgot to remove this unused field in D73809989.
Test Plan: `buck test 'fbcode//mode/opt' fbcode//caffe2/test:fbonly -- --exact 'caffe2/test:fbonly - test_compilation_metrics_logger_in_sync (caffe2.test.fb.test_fb.TestFBOnly)'`
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153413
Approved by: https://github.com/c00w
2025-05-13 16:44:30 +00:00
Michael Lazos
ff039d39ec
[Dynamo] Optimize dedupe region ancestor tracking ( #152589 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/152589
Approved by: https://github.com/anijain2305
ghstack dependencies: #152389 , #152505 , #152410 , #152506 , #152570 , #152572
2025-05-13 12:17:59 +00:00
Michael Lazos
3592cb52d9
[Hierarchical Compilation] Use universal flatten APIs ( #152505 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/152505
Approved by: https://github.com/anijain2305
ghstack dependencies: #152389
2025-05-13 12:17:59 +00:00
Michael Lazos
023a3dc69f
[Hierarchical Compilation] Track node mutations ( #152389 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/152389
Approved by: https://github.com/anijain2305
2025-05-13 12:17:59 +00:00
Aaron Gokaslan
3555ebb63d
[BE]: Update ruff to 0.11.8 ( #153249 )
...
Fixes a ton of false negatives throughout the codebase. RUFF also properly validates NOQA comments now and most of the changes are fixing typos there or removing filewide flake8 suppressions that were also silencing ruff issues.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153249
Approved by: https://github.com/cyyever , https://github.com/albanD , https://github.com/seemethere
2025-05-12 18:30:52 +00:00
PyTorch MergeBot
5c3fddb9cc
Revert "[Hierarchical Compilation] Track node mutations ( #152389 )"
...
This reverts commit c2936ebfd5 .
Reverted https://github.com/pytorch/pytorch/pull/152389 on behalf of https://github.com/jeanschmidt due to Humm, interesting, there seems to be a bug in stack PRs, as it should be part of the stack and be reverted with the other ones ([comment](https://github.com/pytorch/pytorch/pull/152389#issuecomment-2873540451 ))
2025-05-12 18:18:44 +00:00
PyTorch MergeBot
78d752e96a
Revert "[Hierarchical Compilation] Use universal flatten APIs ( #152505 )"
...
This reverts commit f9e3a9058e .
Reverted https://github.com/pytorch/pytorch/pull/152505 on behalf of https://github.com/jeanschmidt due to [TENTATIVE] reverting to check if reverting this stack partially caused the introduction of https://github.com/pytorch/pytorch/actions/runs/14966121510/job/42049638969#step:22:875 ([comment](https://github.com/pytorch/pytorch/pull/152505#issuecomment-2872869990 ))
2025-05-12 14:48:08 +00:00
PyTorch MergeBot
aa7fe6af41
Revert "[Dynamo] Optimize dedupe region ancestor tracking ( #152589 )"
...
This reverts commit b5f1345f72 .
Reverted https://github.com/pytorch/pytorch/pull/152589 on behalf of https://github.com/jeanschmidt due to Breaking internal signal citadel-fbcode-test-mode-opt-for-pt2_stack_for_internal-linux-0 please see diff [D74531503](https://www.internalfb.com/diff/D74531503 ) for more details ([comment](https://github.com/pytorch/pytorch/pull/152410#issuecomment-2871168679 ))
2025-05-12 07:15:09 +00:00
PyTorch MergeBot
01bb249978
Revert "has_triton: Use the device interface for detecting Triton availability ( #139171 )"
...
This reverts commit 48bfe9afc7 .
Reverted https://github.com/pytorch/pytorch/pull/139171 on behalf of https://github.com/masnesral due to Performance regression for huggingface ([comment](https://github.com/pytorch/pytorch/pull/139171#issuecomment-2868939790 ))
2025-05-10 14:46:23 +00:00
Michael Lazos
b5f1345f72
[Dynamo] Optimize dedupe region ancestor tracking ( #152589 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/152589
Approved by: https://github.com/anijain2305
ghstack dependencies: #152389 , #152505 , #152410 , #152506 , #152570 , #152572
2025-05-10 08:27:56 +00:00
Michael Lazos
f9e3a9058e
[Hierarchical Compilation] Use universal flatten APIs ( #152505 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/152505
Approved by: https://github.com/anijain2305
ghstack dependencies: #152389
2025-05-10 08:27:07 +00:00