Repository navigation
adamw_flash: keep the unquantized bf16 Adam variance in fp32 so it can decay - #14749
Conversation
…de the fused step
…es bf16 because its variance is fp32
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
optim = "adamw_flash"defaults toquantize = False, so flashoptim stores both Adam moments in the parameter dtype. Its fused kernel updates the variance in fp32 and writes it back with round-to-nearest (tl.store(var_ptr, var_f32.to(PARAM_DTYPE)), flashoptim 0.1.4optimizers.py:2529).At
beta2 = 0.999one step moves the variance by 0.1% of the gap tog**2. That is under half a bf16 ulp (0.2% to 0.4%) unlessg**2is several times the stored value, so the rounding throws the step away. Two things follow:lr / sqrt(v)keeps shrinking. Under noisy gradients it ratchets up on every large sample and never comes back.g**2, so steps are about 1.8x too large.Through the real fused flashoptim step (4096 elements, stored variance over an fp32 EMA of the same gradients):
With this PR every cell is 1.00.
This affects bf16 full finetuning with the default
optim_args. LoRA adapters (fp32 params),quantize=Trueand the other optimizers are not affected.Evidence (Qwen2.5-0.5B, bf16 full finetune, wikitext-103, batch 8 x 512, lr 2e-5 cosine with 50 warmup steps, grad clip 1.0, B200)
The stored variance was read from the optimizer state every 100 steps and compared with an fp32 EMA of the same clipped gradients, for 5 tensors.
With the current default:
With this PR: the ratio is 1.0 throughout, and the share of elements that decrease matches the fp32 EMA.
Eval loss, 6000 steps:
2000-step runs (mean train loss over the last 500 steps, relative to fp32 AdamW):
quantize=TrueFix
For bf16 params with unquantized moments,
UnslothFlashAdamWwidensexp_avg_sqto fp32 when the state is created or loaded.v * beta2 + g**2 * (1 - beta2)beside it, and that result replaces the variance after the step.set_datakeeps the fp32 storage.load_state_dict: torch casts floating state to the param dtype on load, so the saved fp32 variance is copied back after loading instead of being rounded to bf16. Checkpoints written before this PR (bf16 variance) still load.quantize=Falseis still refused: the variance would underflow.Cost
Tests
tests/utils/test_flash_adamw_optim.py:test_adamw_flash_bf16_variance_follows_fp32[_scaled_noise|_steady](CPU, unfused): the variance must match an fp32 EMA after a 10x gradient drop and under a steady gradient. On main it stays frozen at its peak in the first and stalls at 0.31x in the second.test_adamw_flash_bf16_variance_survives_a_state_dict_round_trip(CPU): the fp32 variance is unchanged afterstate_dict/load_state_dict.test_adamw_flash_fused_bf16_variance_follows_fp32[_scaled_noise|_steady](gpu): the same checks through flashoptim's fused kernel.test_adamw_flash_rejects_fp16_params_with_unquantized_momentsnow checks the new message.All of these fail on main and pass here. The rest of the file passes unchanged.