pytorch/torch/_C
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
..
_acc Add functions to setup PrivateUse1 as a python backend device. (#157859) 2025-10-01 21:32:59 +00:00
_dynamo [5/N] Apply ruff UP035 rule (#164423) 2025-10-02 07:31:11 +00:00
_export [serialization] Add pte file to archive (#162520) 2025-09-11 07:59:11 +00:00
__init__.pyi.in Support setting grad_dtype on leaf tensors (#162815) 2025-10-02 23:09:07 +00:00
_aoti.pyi [AOTInductor] Add class declarations to torch._C._aoti interface file (#155128) 2025-06-17 00:10:57 +00:00
_autograd.pyi [5/N] Apply ruff UP035 rule (#164423) 2025-10-02 07:31:11 +00:00
_cpu.pyi
_cudnn.pyi
_cusparselt.pyi
_distributed_autograd.pyi
_distributed_c10d.pyi Fake process group Direct construction error (#163665) 2025-10-02 22:19:26 +00:00
_distributed_rpc_testing.pyi
_distributed_rpc.pyi
_functionalization.pyi Reapply "Make functionalization ViewMeta serializable with pickle. (#143712)" (#163769) 2025-09-25 10:27:37 +00:00
_functions.pyi
_functorch.pyi torchdim Python port (#160236) 2025-09-21 03:01:04 +00:00
_instruction_counter.pyi
_itt.pyi
_jit_tree_views.pyi added stubs for jit tree views (#156504) 2025-06-25 06:15:17 +00:00
_lazy_ts_backend.pyi
_lazy.pyi
_monitor.pyi [5/N] Apply ruff UP035 rule (#164423) 2025-10-02 07:31:11 +00:00
_nn.pyi.in fix type hints for interpolation functions (#157202) 2025-07-09 03:11:37 +00:00
_nvtx.pyi
_onnx.pyi
_profiler.pyi [5/N] Apply ruff UP035 rule (#164423) 2025-10-02 07:31:11 +00:00
_VariableFunctions.pyi.in [BE][CI][Easy] Run lintrunner on generated .pyi stub files (#150732) 2025-05-27 14:58:02 +00:00
_verbose.pyi
build.bzl
return_types.pyi.in [BE][CI][Easy] Run lintrunner on generated .pyi stub files (#150732) 2025-05-27 14:58:02 +00:00