Repository navigation
Use F.rms_norm in LlamaRMSNorm and the norms derived from it - #42461
Conversation
|
Hey @mstojkovicTT, thanks for the PR! We definitely want the functions to be a drop-in replacement, so they should return exactly the same dtype as the old functions did. Also, in your tests you're initializing |
This may be problematic, because of the following scenario:
return self.weight * hidden_states.to(input_dtype)
Here is the pytorch default implementation of
I did the same testing just with the additional with torch.no_grad():
random_weight = torch.randn(hidden_size, device=device) * 0.05 + 1.0
hf_module.weight.copy_(random_weight)
new_module.weight.copy_(random_weight)and everything still works. And also @Rocketknight1, thank you for taking a time to review this! |
|
Yeah, I understand! My guess is that the original code was made to follow the original model implementation, even if it seems weird. Downcasting |
|
Ah, that makes sense. What do you think about trying CI run against the changes for now, and if it breaks something I will take a closer look? @Rocketknight1 |
|
Sure, let's see how it goes |
|
run-slow: llama |
|
This comment contains models: ["models/llama"] |
CI ResultsModel CI Report❌ Failed tests
|
|
Hmn, it does seem like this might shift logits slightly |
dd4bfbd to
5f8619a
Compare
|
Thanks for running this @Rocketknight1! For With my change (new behavior)I see a small drift in the expected mean/slice:
And for the slice (
[[ 6.2500e-02, 3.1250e-02, -7.8188e-03, 3.1250e-02, 5.0068e-05,
0.0000e+00, 0.0000e+00, 0.0000e+00, -5.0068e-05, 3.1300e-02,
-5.0068e-05, -5.0068e-05, -1.5638e-02, -2.3438e-02, 0.0000e+00]]
[-12.5625, -7.1250, -0.6289, -7.8750, -6.9688, -7.8125, -6.5000,
-7.4375, -7.6562, -6.9688, -6.0312, -7.0312, -1.8203, 1.8750,
-8.5000]
[-12.5000, -7.0938, -0.6367, -7.8438, -6.9688, -7.8125, -6.5000,
-7.4375, -7.6562, -6.9375, -6.0312, -7.0312, -1.8359, 1.8516,
-8.5000]Baseline / original implementation (before my change)For comparison, with the original implementation I see different drift:
And for the slice (
[-6.2500e-02, 6.2500e-02, 7.8119e-02, -9.3750e-02, -1.2495e-01,
-6.2500e-02, 3.1250e-02, -1.2500e-01, -3.1300e-02, 5.0068e-05,
-6.2550e-02, -3.1300e-02, -1.5638e-02, 2.3438e-02, 0.0000e+00]
[-12.5625, -7.1250, -0.6289, -7.8750, -6.9688, -7.8125, -6.5000,
-7.4375, -7.6562, -6.9688, -6.0312, -7.0312, -1.8203, 1.8750,
-8.5000]
[-12.6250, -7.0625, -0.5508, -7.9688, -7.0938, -7.8750, -6.4688,
-7.5625, -7.6875, -6.9688, -6.0938, -7.0625, -1.8359, 1.8984,
-8.5000]So from my side it looks like the change does alter numerics, but it’s not obvious that it strictly makes things “worse”, it seems to move the error pattern relative to the golden expectations. I dont know if you have some kind of way to check the actual performance of the model? |
5f8619a to
995f7d8
Compare
|
Hi @mstojkovicTT, just realized we might actually need to pause this PR! The reason is that I don't think Torch 2.3 (our current minimum supported version) has |
|
That makes sense. Thanks for the responses, putting this on a pause till then :) |
|
Yeah, and thanks for investigating! My suspicion is the numerics are similar enough that we might be able to just switch everything to using |
|
@Rocketknight1 could you take a look when you get a chance? This is waiting on a maintainer: it has been waiting for a maintainer review. |
The next commit edits LlamaRMSNorm and regenerates the norms derived from it with `make fix-repo`.
rms_norm normalizes in one fused kernel; the weight is applied after the cast back to the input dtype, so outputs and dtypes match the previous implementation (autocast runs rms_norm in fp32). Only LlamaRMSNorm is edited, the other 140 norms come from `make fix-repo`.
|
Taking this over, thanks @mstojkovicTT for starting it and for the investigation. It was paused on the torch 2.3 floor (comment), and main now requires What changed:
One H100, torch 2.13, bf16, forward + backward:
Tests for llama, qwen2, qwen3, qwen3_moe, mistral and olmo2 give the same results as main. Slow integration tests for llama, qwen2, qwen3 and mistral too, except @Rocketknight1 @ArthurZucker could you take a look? |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Rocketknight1
left a comment
There was a problem hiding this comment.
@qgallouedec LGTM! I worry someone might try to refactor this later though, can you make it clear in the comment why we keep the weight outside the rms_norm call?
|
[For maintainers] Suggested jobs to run (before merge) run-slow: aimv2, apertus, apertus1p5, arcee, aria, axk1, axk2, bailing_hybrid, bamba, bitnet, blt, chameleon, clvp, cohere2_moe, cosmos3_edge, csm |
|
Thanks, done in 78ea297: the comment now says not to pass the weight to |
706bd9e
Fixes #42398
LlamaRMSNormnormalizes withF.rms_norm(one fused kernel, torch >= 2.4) instead of several fp32 elementwise kernels. The weight is still applied after the cast back to the input dtype, so output dtypes are unchanged and bf16 outputs match the previous code on all but 3 elements per million. OnlyLlamaRMSNormis edited by hand:make fix-reporegenerates the 140 norms derived from it. Norms with other math (Gemma, T5) are unchanged.One H100, torch 2.13, bf16, forward + backward:
The script below prints 315 ms / 41.4 GiB on main, 250 ms / 38.3 GiB here.