linhaifeng
695cb0d342
[2/N][Fix] Fix typo in test folder ( #166374 )
...
Fix typo in test folder.
_typos.toml
```bash
[default.extend-words]
nd = "nd"
arange = "arange"
Nd = "Nd"
GLOBALs = "GLOBALs"
hte = "hte"
iy = "iy"
PN = "PN"
Dout = "Dout"
optin = "optin"
gam = "gam"
PTD = "PTD"
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/166374
Approved by: https://github.com/cyyever , https://github.com/ezyang
2025-10-29 03:02:07 +00:00
PyTorch MergeBot
75b8295868
Revert "Warn if AccumulateGrad stream does not match producer node stream ( #165065 )"
...
This reverts commit 12f742941d .
Reverted https://github.com/pytorch/pytorch/pull/165065 on behalf of https://github.com/clee2000 due to broke internal builds D85273204 usages of TORCH_API void add need to be updated? ([comment](https://github.com/pytorch/pytorch/pull/165065#issuecomment-3438061854 ))
2025-10-23 17:02:49 +00:00
soulitzer
12f742941d
Warn if AccumulateGrad stream does not match producer node stream ( #165065 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165065
Approved by: https://github.com/ngimel
2025-10-22 17:33:27 +00:00
Yuanyuan Chen
e925dfcc6b
Enable all SIM rules except disabled ones ( #164645 )
...
`SIM` rules are useful for simplifying boolean expressions and enhances code readability.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164645
Approved by: https://github.com/ezyang , https://github.com/mlazos
2025-10-17 07:27:11 +00:00
Scott Wolchok
331b7cc054
Fix double dispatch to Python for detach ( #163671 )
...
This fixes #71725 .
Differential Revision: [D83857880](https://our.internmc.facebook.com/intern/diff/D83857880 )
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163671
Approved by: https://github.com/ezyang , https://github.com/albanD
2025-10-15 17:24:50 +00:00
PyTorch MergeBot
267348fe7f
Revert "Fix double dispatch to Python for detach ( #163671 )"
...
This reverts commit a3e3efe474 .
Reverted https://github.com/pytorch/pytorch/pull/163671 on behalf of https://github.com/seemethere due to We should've reverted this when we decided to revert https://github.com/pytorch/pytorch/pull/164691 since they were actually stacked ([comment](https://github.com/pytorch/pytorch/pull/163671#issuecomment-3400009953 ))
2025-10-14 03:55:36 +00:00
Scott Wolchok
a3e3efe474
Fix double dispatch to Python for detach ( #163671 )
...
This fixes #71725 .
Differential Revision: [D83857880](https://our.internmc.facebook.com/intern/diff/D83857880 )
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163671
Approved by: https://github.com/ezyang , https://github.com/albanD
2025-10-13 16:10:17 +00:00
PyTorch MergeBot
f975bd58af
Revert "Warn if AccumulateGrad stream does not match producer node stream ( #165065 )"
...
This reverts commit a70ef954b9 .
Reverted https://github.com/pytorch/pytorch/pull/165065 on behalf of https://github.com/izaitsevfb due to breaks lint ([comment](https://github.com/pytorch/pytorch/pull/165065#issuecomment-3391387386 ))
2025-10-10 17:29:29 +00:00
soulitzer
a70ef954b9
Warn if AccumulateGrad stream does not match producer node stream ( #165065 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165065
Approved by: https://github.com/ngimel
ghstack dependencies: #162815
2025-10-10 16:46:01 +00:00
soulitzer
71aefd5595
[reland] Allow setting grad_dtype on leaf tensors ( #164751 )
...
ghstack-source-id: e44b3941530be83a630ec93f1478eec741ffca2e
Pull-Request-resolved: https://github.com/pytorch/pytorch/pull/162815
Fixes #ISSUE_NUMBER
Relanding due to internal weirdness. Separate PR to codev w/o ghstack.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164751
Approved by: https://github.com/albanD
2025-10-08 20:23:13 +00:00
PyTorch MergeBot
97463d4cf3
Revert "Fix double dispatch to Python for detach ( #163671 )"
...
This reverts commit c32118dc3e .
Reverted https://github.com/pytorch/pytorch/pull/163671 on behalf of https://github.com/izaitsevfb due to breaks export tests ([comment](https://github.com/pytorch/pytorch/pull/163671#issuecomment-3379281422 ))
2025-10-08 01:46:45 +00:00
Scott Wolchok
c32118dc3e
Fix double dispatch to Python for detach ( #163671 )
...
This fixes #71725 .
Differential Revision: [D83857880](https://our.internmc.facebook.com/intern/diff/D83857880 )
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163671
Approved by: https://github.com/ezyang , https://github.com/albanD
2025-10-07 23:34:37 +00:00
albanD
af32d16a71
Add pure view support in autograd Function ( #164736 )
...
This is the same as https://github.com/pytorch/pytorch/pull/164467
But it needs to be co-deved due to internal insanity.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164736
Approved by: https://github.com/soulitzer
2025-10-06 18:21:05 +00:00
PyTorch MergeBot
5d7360bb03
Revert "Enable all SIM rules except disabled ones ( #164645 )"
...
This reverts commit 321e602692 .
Reverted https://github.com/pytorch/pytorch/pull/164645 on behalf of https://github.com/izaitsevfb due to causes lint failures ([comment](https://github.com/pytorch/pytorch/pull/164645#issuecomment-3369274351 ))
2025-10-05 19:32:21 +00:00
Yuanyuan Chen
321e602692
Enable all SIM rules except disabled ones ( #164645 )
...
`SIM` rules are useful for simplifying boolean expressions and enhances code readability.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164645
Approved by: https://github.com/ezyang
2025-10-05 07:38:25 +00:00
PyTorch MergeBot
f46bb04dcc
Revert "Add pure view support in autograd Function ( #164467 )"
...
This reverts commit 10335ffb2c .
Reverted https://github.com/pytorch/pytorch/pull/164467 on behalf of https://github.com/facebook-github-bot due to Diff reverted internally ([comment](https://github.com/pytorch/pytorch/pull/164467#issuecomment-3368152304 ))
2025-10-04 11:42:46 +00:00
soulitzer
7d570129e0
Fix custom autograd Function memory leak when saving mutated view ( #164407 )
...
Fixes https://github.com/pytorch/pytorch/issues/160317
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164407
Approved by: https://github.com/albanD
2025-10-04 00:47:12 +00:00
PyTorch MergeBot
3ddf2018d0
Revert "Support setting grad_dtype on leaf tensors ( #162815 )"
...
This reverts commit dca73982c5 .
Reverted https://github.com/pytorch/pytorch/pull/162815 on behalf of https://github.com/yangw-dev due to break internal test D83850533, see more details below ([comment](https://github.com/pytorch/pytorch/pull/162815#issuecomment-3367498501 ))
2025-10-03 23:14:28 +00:00
albanD
10335ffb2c
Add pure view support in autograd Function ( #164467 )
...
Fix https://github.com/pytorch/pytorch/issues/73604
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164467
Approved by: https://github.com/ezyang , https://github.com/soulitzer
2025-10-03 21:42:11 +00:00
soulitzer
dca73982c5
Support setting grad_dtype on leaf tensors ( #162815 )
...
`grad_dtype` is a new attribute on Tensor to control gradient dtype:
- Access/setting is leaf-only.
- grad_dtype is respected when (1) when assigning to .grad, and (2) in the engine after the previous node produces incoming gradients for AccumulateGrad. (See table below for details)
- Not setting grad_dtype preserves the current behavior. Accessing it returns `t.dtype`
- `grad_dtype` cannot be set when there is already a `.grad` present and the dtypes conflict.
| `grad_dtype` setting | Setting `.grad` manually | Incoming gradient from autograd engine |
|-----------------------|--------------------------|-----------------------------------------|
| **Default (tensor’s dtype)** | `.grad` must match tensor’s dtype | Engine casts incoming grad to tensor’s dtype |
| **Set to specific dtype** | `.grad` must match that dtype | Engine casts incoming grad to the specified dtype |
| **Set to `None`** | `.grad` may be any dtype | Engine does not cast; accepts incoming grad dtype as-is |
Pull Request resolved: https://github.com/pytorch/pytorch/pull/162815
Approved by: https://github.com/albanD
2025-10-02 23:09:07 +00:00
soulitzer
bac0f289a3
Add methods to access data and unpack_hook on SavedVariable ( #164358 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164358
Approved by: https://github.com/albanD
2025-10-02 13:05:16 +00:00
soulitzer
8171d6052e
Clear custom autograd Function ctx.to_save earlier ( #161171 )
...
Fixes https://github.com/pytorch/pytorch/issues/161186
Pull Request resolved: https://github.com/pytorch/pytorch/pull/161171
Approved by: https://github.com/albanD
2025-09-02 03:26:31 +00:00
PyTorch MergeBot
049c08eda8
Revert "[dynamo] [guard] Add caching for inside torch.compile.disable function to avoid unnecessary recompilation. ( #160934 )"
...
This reverts commit 8f31aa97a3 .
Reverted https://github.com/pytorch/pytorch/pull/160934 on behalf of https://github.com/anijain2305 due to causes memory leak leading to OOMs ([comment](https://github.com/pytorch/pytorch/pull/160934#issuecomment-3234426359 ))
2025-08-28 17:56:36 +00:00
soulitzer
1e4dfeeb06
Add early_stop kwarg to torch.utils.checkpoint ( #160781 )
...
We already have a context manager "set_checkpoint_early_stop". This PR adds a kwarg that toggles the same setting.
It is also useful to have a kwarg version of the setting in addition to the context manager because is annoying to apply a context manager when the AC is being applied via CheckpointWrapper.
Similar to the "debug" kwarg and the corresponding "set_checkpoint_debug_enabled" context manager, the context manager defaults to None and overrides the local setting when non-None.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160781
Approved by: https://github.com/tianyu-l
2025-08-26 22:32:35 +00:00
thenumberouscode
8f31aa97a3
[dynamo] [guard] Add caching for inside torch.compile.disable function to avoid unnecessary recompilation. ( #160934 )
...
Fixes #157399
cherry pick of d6a5c03
@mlazos
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160934
Approved by: https://github.com/mlazos
2025-08-19 06:01:26 +00:00
albanD
8e6a313858
Add ownership token when needed on GradientEdge ( #160098 )
...
We can avoid the token by introducing PyObject preservation for THPFunction. But I think it will be too much complexity given that this kind of issue is very rare.
Happy to be talked into doing it though if someone really wants to.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160098
Approved by: https://github.com/ezyang , https://github.com/soulitzer
2025-08-12 20:14:18 +00:00
Simon Fan
c8205cb354
[autograd] match 0-dim gradients device type regardless of subclassness ( #160165 )
...
Not sure if there some subclasses where the outer.dim() == 0 but you wouldn't want to move it?
FIXES https://github.com/pytorch/pytorch/issues/160084
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160165
Approved by: https://github.com/ezyang , https://github.com/albanD
2025-08-11 17:57:32 +00:00
Simon Fan
8085edc8f9
[autograd] torch._C._set_view_replay_enabled state leaking into other tests ( #159840 )
...
This was causing view_fns to pop up in tests that ran after `TestAutograd.test_view_replay_enabled` where it isn't used as a context manager. It is unclear to me why we would want `_force_original_view_tracking` to mutate global state on __init__ rather than on __enter__, that could be an alternative fix.
FIXES https://github.com/pytorch/pytorch/issues/156306 https://github.com/pytorch/pytorch/issues/156289 https://github.com/pytorch/pytorch/issues/156265 https://github.com/pytorch/pytorch/issues/156209
Pull Request resolved: https://github.com/pytorch/pytorch/pull/159840
Approved by: https://github.com/albanD
2025-08-05 21:57:49 +00:00
PyTorch MergeBot
cb4f41e125
Revert "[dynamo] [guard] Add caching for inside torch.compile.disable function to avoid unnecessary recompilation. ( #157566 )"
...
This reverts commit 8e07c9870d .
Reverted https://github.com/pytorch/pytorch/pull/157566 on behalf of https://github.com/yangw-dev due to failed an odd internal test, please reach out to metamate to fix it, D79112610 ([comment](https://github.com/pytorch/pytorch/pull/157566#issuecomment-3141840110 ))
2025-08-01 01:27:45 +00:00
thenumberouscode
8e07c9870d
[dynamo] [guard] Add caching for inside torch.compile.disable function to avoid unnecessary recompilation. ( #157566 )
...
inside torch.compile.disable function always triggers recompilation. because a user inside function decorated with torch._dynamo.disable would be used as an argument in the resume_in_xx function. In the current implementation, it will always be a new object, resulting in the ID_MATCH guard always failing and triggering recompilation.
Fixes https://github.com/pytorch/pytorch/issues/157399
@xmfan
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157566
Approved by: https://github.com/mlazos , https://github.com/anijain2305
2025-07-28 12:44:22 +00:00
Xuehai Pan
fc0376e8b1
[BE][2/6] fix typos in test/ (test/test_*.py) ( #157636 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157636
Approved by: https://github.com/yewentao256 , https://github.com/mlazos
ghstack dependencies: #156311 , #156609
2025-07-09 11:02:23 +00:00
soulitzer
1ed243f01c
Add missing attr access check for legacy autograd.Function ( #155055 )
...
Fixes https://github.com/pytorch/pytorch/issues/154981
Pull Request resolved: https://github.com/pytorch/pytorch/pull/155055
Approved by: https://github.com/albanD
ghstack dependencies: #154509 , #154852
2025-06-11 01:03:49 +00:00
Natalia Gimelshein
34e3930401
fix numpy compatibility for 2d small list indices ( #154806 )
...
Will fix #119548 and linked issues once we switch from warning to the new behavior,
but for now, given how much this syntax was used in our test suite, we suspect a silent change will be disruptive.
We will change the behavior after 2.8 branch is cut.
Numpy behavior was changed at least in numpy 1.24 (more than 2 years ago)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/154806
Approved by: https://github.com/cyyever , https://github.com/Skylion007 , https://github.com/albanD
2025-06-04 01:58:52 +00:00
soulitzer
3aa95b252a
Fix test_side_stream_backward_overlap flakiness ( #153963 )
...
Fixes https://github.com/pytorch/pytorch/issues/153927
Although the autograd backward should always execute SideBw before MainBw, there is still a small chance the recorded events won't be in that order.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153963
Approved by: https://github.com/janeyx99 , https://github.com/Skylion007
ghstack dependencies: #151079 , #153412
2025-05-20 21:02:56 +00:00
soulitzer
a060f3d272
Rewrite autograd producer consumer stream sync logic ( #151079 )
...
Also see previous work https://github.com/pytorch/pytorch/pull/142097
Pull Request resolved: https://github.com/pytorch/pytorch/pull/151079
Approved by: https://github.com/albanD
2025-05-16 15:42:22 +00:00
xinan.lin
8ca985b365
[Break XPU] Skip newly added test case on XPU that failed because torch._C._scatter not implemented. ( #153685 )
...
Fixes #153608
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153685
Approved by: https://github.com/malfet
2025-05-16 14:15:50 +00:00
Simon Fan
5aea57d653
[ca][dynamo] always run eager checkpoint region's recomputation in eager ( #153300 )
...
I slap disable on the recomputation hook, otherwise the partitioner may save less/more activations and mismatch with the expected eager count in checkpoint. See code comment `Note: [compiled autograd and checkpoint unpack hook]`.
This fixes all non-nested checkpointing tests. I also wrap nested checkpointing tests, and a few of them still fail.
This also seems to fix all PYTORCH_TEST_WITH_DYNAMO checkpointing tests except for `TestAutograd.test_checkpointing_without_reentrant_custom_function_works`. For those tests, it looks like we fail to HOPify the checkpointed region and when the backward executes the unpack hooks, dynamo tried to trace them. This messed up the internal state tracking of checkpointing, some raising the _StopRecomputationError and others raising the same count mismatch error as CA.
FIXES https://github.com/pytorch/pytorch/issues/127115
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153300
Approved by: https://github.com/jansel
2025-05-16 01:37:48 +00:00
PyTorch MergeBot
236b08cbf8
Revert "[ca][dynamo] always run eager checkpoint region's recomputation in eager ( #153300 )"
...
This reverts commit 4863e5c843 .
Reverted https://github.com/pytorch/pytorch/pull/153300 on behalf of https://github.com/malfet due to Looks like it breaks rocm, see fa8543454a/1 ([comment](https://github.com/pytorch/pytorch/pull/153300#issuecomment-2884489459 ))
2025-05-15 16:58:52 +00:00
Simon Fan
4863e5c843
[ca][dynamo] always run eager checkpoint region's recomputation in eager ( #153300 )
...
I slap disable on the recomputation hook, otherwise the partitioner may save less/more activations and mismatch with the expected eager count in checkpoint. See code comment `Note: [compiled autograd and checkpoint unpack hook]`.
This fixes all non-nested checkpointing tests. I also wrap nested checkpointing tests, and a few of them still fail.
This also seems to fix all PYTORCH_TEST_WITH_DYNAMO checkpointing tests except for `TestAutograd.test_checkpointing_without_reentrant_custom_function_works`. For those tests, it looks like we fail to HOPify the checkpointed region and when the backward executes the unpack hooks, dynamo tried to trace them. This messed up the internal state tracking of checkpointing, some raising the _StopRecomputationError and others raising the same count mismatch error as CA.
FIXES https://github.com/pytorch/pytorch/issues/127115
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153300
Approved by: https://github.com/jansel
2025-05-15 08:10:35 +00:00
PyTorch MergeBot
2c1912452d
Revert "Rewrite autograd producer consumer stream sync logic ( #151079 )"
...
This reverts commit f78e4529a9 .
Reverted https://github.com/pytorch/pytorch/pull/151079 on behalf of https://github.com/jeanschmidt due to Seems to have introduced regressions in internal signals, see [D74648937](https://www.internalfb.com/diff/D74648937 ) ([comment](https://github.com/pytorch/pytorch/pull/151079#issuecomment-2880176879 ))
2025-05-14 13:07:12 +00:00
Simon Fan
a80eb84a5f
[ca] support higher order gradients (create_graph=True) ( #153222 )
...
Adds create_graph support if you don't compile or compile only with torch.compile(backend="eager").
Using a backend that uses AOTDispatch produces a post-dispatch AOT backward, where its double backward will be silently incorrect if the forward trace involved any ops that are not composite implicit.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153222
Approved by: https://github.com/jansel
ghstack dependencies: #153193
2025-05-13 16:42:09 +00:00
soulitzer
f78e4529a9
Rewrite autograd producer consumer stream sync logic ( #151079 )
...
Also see previous work https://github.com/pytorch/pytorch/pull/142097
Pull Request resolved: https://github.com/pytorch/pytorch/pull/151079
Approved by: https://github.com/albanD
2025-05-12 21:07:16 +00:00
soulitzer
cb35a2b15d
Add missing in-place on view check to custom autograd.Function ( #153094 )
...
Fixes https://github.com/pytorch/pytorch/issues/152773
Pull Request resolved: https://github.com/pytorch/pytorch/pull/153094
Approved by: https://github.com/albanD
ghstack dependencies: #153005
2025-05-12 14:42:46 +00:00
Valérian Rey
f5851efed9
Fix torch.autograd.backward inputs validation ( #150975 )
...
- Fixes #150883
- Fixes #70504
This is my first PR to pytorch, so please tell me if I'm forgetting anything.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/150975
Approved by: https://github.com/soulitzer
2025-04-17 02:11:13 +00:00
soulitzer
32f0f414ab
Add some autograd producer consumer stream sync tests ( #150952 )
...
Thanks @ngimel and @albanD for some ideas on test cases
Pull Request resolved: https://github.com/pytorch/pytorch/pull/150952
Approved by: https://github.com/albanD
2025-04-12 02:44:09 +00:00
soulitzer
a2bba53f87
Improve error message when view of intermediate is returned from autograd.Function and marked dirty ( #149543 )
...
Fixes https://github.com/pytorch/pytorch/issues/149252
Pull Request resolved: https://github.com/pytorch/pytorch/pull/149543
Approved by: https://github.com/zou3519
ghstack dependencies: #149220
2025-03-25 14:44:11 +00:00
Animesh Jain
a3c286677b
[compile] Switch off inference mode during compilation ( #149321 )
...
PR does following
* Turns `inference_mode` to False and `no_grad` for `convert_frame`, if the inference_mode is on globally.
* Turns off inference_mode for fake tensor prop. This ensures that converting from real inference tensor to a fake tensor removes the inference-ness.
* Graph breaks on is_inference and is_inference_mode_enabled.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/149321
Approved by: https://github.com/jansel , https://github.com/zou3519
2025-03-19 02:45:27 +00:00
Aaron Gokaslan
292af3cc89
[BE][Ez]: ISC001 Auto concatenate implicit one line strings ( #146408 )
...
Apply ruff rule about implicit string concatenation, this autofixes strings that are all the same type and on the same line. These lines are broken up likely as the result of autoformatters in the past. All fixes are automated using the autofixes in ISC001.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/146408
Approved by: https://github.com/justinchuby , https://github.com/janeyx99
2025-02-04 19:07:04 +00:00
Brian Hirsh
e68f5087d8
update _unsafe_set_version_counter to accept lists of tensors ( #137921 )
...
See the comment [here](https://github.com/pytorch/pytorch/issues/132014#issuecomment-2379547400 ) (cc @H-Huang @awgu @kwen2501 @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang @aakhundov @XilunWu @rec) - this PR updates `_unsafe_set_version_counter` to accept a list of tensors, for overhead-sensitive users (e.g. distributed) who need to hide VC bumps from autograd on a large list of tensors without wanting to suffer the overhead of going from python->C++ separately for every tensor in the list.
I left the binding in pybind, and used a `std::vector`. if we **really** need to optimize overhead even further, we could write a manual cpython binding.
I use this updated API in the next PR to fix FSDP2, so that it properly hides the VC of all `all_gather_buffer` tensors in its call to `split_with_sizes_copy.out(all_gather_buffers)`.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/137921
Approved by: https://github.com/awgu , https://github.com/albanD
2025-02-04 04:51:11 +00:00
soulitzer
9e0ee152e5
Fix allow_mutation_on_saved_tensors for inplace foreach ( #145520 )
...
Pull Request resolved: https://github.com/pytorch/pytorch/pull/145520
Approved by: https://github.com/albanD
2025-01-25 00:58:03 +00:00