Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/190852
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 74feb0d with merge base 83296c3 ( CRCR (non-blocking) - The following CRCR downstream CI job failed but is non-blocking:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
34c0d05 to
fc80536
Compare
|
|
|
Hi @soulitzer , based on our previous discussion around the setter API, I’ve reworked the getter API to return a tuple. This keeps its interface consistent with set_output_grad_dtype while also simplifying the implementation. When you have a chance, could you please take another look? Thanks! |
|
@claude review these changes |
|
Claude finished @jansel's task in 43m 7s —— View job Reviewed — the main finding is that the getter silently returns all-
|
|
Should we just make reading t.grad_dtype work on non-leafs too? 🤔 |
|
Thanks! I think there are two possible interpretations of making
Did you mean the second approach, or simply lifting the current leaf-only restriction? For our target DTensor propagation path between |
|
@SongyuanZhao Yep, checking the input_metadata here for reading t.grad_dtype sounds good! |
9388e63 to
0957da0
Compare
|
@soulitzer Thanks for the suggestion! I've reworked the PR accordingly. Could you please take another look when you have a chance? |
|
Thanks for the update! I think one thing we need to validate here is whether setting .grad on non-leaf is also consistent with the non-leaf grad-dtype |
Non-leaf grad_dtype is stored in the output edge metadata, but assigning .grad only consulted the tensor AutogradMeta. This made the public grad_dtype value disagree with the dtype accepted by the .grad setter. Use one effective grad dtype lookup for both the getter and setter, and cover successful and rejected assignments. Keeping the lookup shared avoids duplicating the non-leaf edge handling. Test Plan: Remote CPU-only build: ``` /home/hadoop-scale-llm/ptbuild/bin/python -m pip install -e . -v --no-build-isolation ``` Targeted remote test (14 passed): ``` env -u LD_PRELOAD -u PYTHONPATH /home/hadoop-scale-llm/ptbuild/bin/python /home/hadoop-scale-llm/codex-build/pytorch-pr190852/test/test_autograd.py -v -k grad_dtype ``` Lint attempted locally: ``` .venv/bin/lintrunner -a ``` Lint could not complete because the local build directory is absent; the all-files Pyrefly checks also reported pre-existing missing torch attributes from the unbuilt checkout. Authored with assistance from Codex, an AI coding assistant.
0957da0 to
5fe7d6f
Compare
|
Thanks for pointing this out! This was indeed a gap: assigning .grad on a non-leaf tensor was not validated against its effective grad_dtype. I’ve fixed the inconsistency and added tests. |
| ``t.grad_dtype == t.dtype``. When not None, the autograd engine casts | ||
| incoming gradients to this dtype. This attribute is only accessible and | ||
| settable for leaf tensors. | ||
| incoming gradients to this dtype. This attribute is readable for all tensors, |
There was a problem hiding this comment.
minor nit: "this attribute is readable for both leaf and non-leaf tensors, but can only be set for leaf tensors"
Suggesting this wording as "all tensors" might too broad and could imply other properties like whether a tensor is differnetiable. Although it is possible to access grad_dtype for tensors of non-differentiable dtype it is not useful to do so.
soulitzer
left a comment
There was a problem hiding this comment.
Looks great, thanks for the update! Minor comment on wording of doc
Replace "all tensors" with "both leaf and non-leaf tensors" to describe the supported tensor categories precisely. Test Plan: Documentation-only wording change. Authored with assistance from Codex, an AI coding assistant.
Bind the producing node shared pointer by const reference when reading the effective gradient dtype. Test Plan: ``` spin quicklint -- --take CLANGFORMAT ``` Authored with assistance from Codex, an AI coding assistant.
|
@soulitzer Thanks! I’ve updated the wording as suggested~ |
|
@pytorchbot merge |
Merge startedYour 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 |
…h#190852) ## Summary This PR makes `Tensor.grad_dtype` readable on non-leaf tensors. For a non-leaf tensor, the getter reads the gradient dtype from the producing autograd node's output metadata: ```cpp t.grad_fn()->input_metadata(t.output_nr()).grad_dtype() ``` The existing behavior for leaf tensors is unchanged. Setting `Tensor.grad_dtype` also remains restricted to leaf tensors. ## Motivation The gradient dtype declared for a custom autograd Function output is stored in the producing node's `InputMetadata` by `ctx.set_output_grad_dtype(...)`. Simply removing the leaf-only check while continuing to read the Tensor's `AutogradMeta` would therefore not expose that declaration on a non-leaf output. Reading the metadata associated with the tensor's current gradient edge allows a downstream Function to observe the gradient dtype contract declared by its producer. One motivating example is the DTensor path: ```text param (DTensor) -> redistribute -> to_local ``` Here, `redistribute` can declare its output gradient dtype with `ctx.set_output_grad_dtype(...)`, and `to_local` can read that declaration from its non-leaf input through `Tensor.grad_dtype` during forward. ## Testing Added coverage for: - reading the default gradient dtype from a non-leaf tensor; - reading gradient dtypes declared by `ctx.set_output_grad_dtype(...)` both inside a downstream Function's `forward()` and outside the Function; - explicit dtype declarations, `None`, and the unset/default behavior; - selecting the correct metadata entry for each tensor returned by a multi-output Function; - keeping the setter restricted to leaf tensors. Pull Request resolved: pytorch#190852 Approved by: https://github.com/soulitzer
Summary
This PR makes
Tensor.grad_dtypereadable on non-leaf tensors.For a non-leaf tensor, the getter reads the gradient dtype from the producing autograd node's output metadata:
t.grad_fn()->input_metadata(t.output_nr()).grad_dtype()The existing behavior for leaf tensors is unchanged. Setting
Tensor.grad_dtypealso remains restricted to leaf tensors.Motivation
The gradient dtype declared for a custom autograd Function output is stored in the producing node's
InputMetadatabyctx.set_output_grad_dtype(...). Simply removing the leaf-only check while continuing to read the Tensor'sAutogradMetawould therefore not expose that declaration on a non-leaf output.Reading the metadata associated with the tensor's current gradient edge allows a downstream Function to observe the gradient dtype contract declared by its producer.
One motivating example is the DTensor path:
Here,
redistributecan declare its output gradient dtype withctx.set_output_grad_dtype(...), andto_localcan read that declaration from its non-leaf input throughTensor.grad_dtypeduring forward.Testing
Added coverage for:
ctx.set_output_grad_dtype(...)both inside a downstream Function'sforward()and outside the Function;None, and the unset/default behavior;cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo