Skip to content

fix: wrap AdamWCustom/AdamWMini update in no_grad to fix leaf tensor inplace error - #4792

Open
szluyu99 wants to merge 1 commit into
PaddlePaddle:developfrom
szluyu99:fix/adamw-leaf-tensor-inplace-nograd
Open

fix: wrap AdamWCustom/AdamWMini update in no_grad to fix leaf tensor inplace error#4792
szluyu99 wants to merge 1 commit into
PaddlePaddle:developfrom
szluyu99:fix/adamw-leaf-tensor-inplace-nograd

Conversation

@szluyu99

Copy link
Copy Markdown

Problem

Training with AdamWCustom raises:

ValueError: (InvalidArgument) Leaf Tensor (embedding_0.w_0_fp32_master_0)
that doesn't stop gradient can't use inplace strategy.

Root cause

master_weight is created via paddle.cast(param, "float32") /
dequantize(...).astype("float32"), inheriting stop_gradient=False from the
trainable param, and it is a leaf tensor. Paddle forbids inplace ops
(p *= ..., master_weight[:] = ..., param[:] = ...) on leaf tensors that
don't stop gradient.

Fix

Run the parameter update under paddle.no_grad() (as Paddle's built-in
optimizers do) and set master_weight.stop_gradient = True. This only affects
autograd bookkeeping, not the optimizer math. Applied to both
AdamWCustom.adamw_custom and AdamWMini.adamw_python.

…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.
@paddle-bot

paddle-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

Thanks for your contribution!

@CLAassistant

CLAassistant commented Jul 21, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants