fix: wrap AdamWCustom/AdamWMini update in no_grad to fix leaf tensor inplace error - #4792
Open
szluyu99 wants to merge 1 commit into
Open
fix: wrap AdamWCustom/AdamWMini update in no_grad to fix leaf tensor inplace error#4792szluyu99 wants to merge 1 commit into
szluyu99 wants to merge 1 commit into
Conversation
…inplace error Master weights created via cast/dequantize inherit stop_gradient=False and are leaf tensors, so inplace ops (p *= ..., master_weight[:] = ..., param[:] = ...) raise "Leaf Tensor ... that doesn't stop gradient can't use inplace strategy". Run the parameter update under paddle.no_grad() (as paddle's built-in optimizers do) and mark master_weight.stop_gradient = True. This only changes autograd bookkeeping, not the optimizer math.
|
Thanks for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Training with AdamWCustom raises:
Root cause
master_weightis created viapaddle.cast(param, "float32")/dequantize(...).astype("float32"), inheritingstop_gradient=Falsefrom thetrainable param, and it is a leaf tensor. Paddle forbids inplace ops
(
p *= ...,master_weight[:] = ...,param[:] = ...) on leaf tensors thatdon't stop gradient.
Fix
Run the parameter update under
paddle.no_grad()(as Paddle's built-inoptimizers do) and set
master_weight.stop_gradient = True. This only affectsautograd bookkeeping, not the optimizer math. Applied to both
AdamWCustom.adamw_customandAdamWMini.adamw_python.