Fixed fp32 residuals - #3504
Conversation
Signed-off-by: mikail <mkhona@nvidia.com>
duncanriach
left a comment
There was a problem hiding this comment.
Good stuff. Thank you
| # When fp32 residual connections are enabled, pipeline parallel communication must | ||
| # use fp32 to match the dtype of the residual stream between pipeline stages. | ||
| if self.fp32_residual_connection and self.pipeline_dtype is not None: | ||
| self.pipeline_dtype = torch.float |
There was a problem hiding this comment.
Could you add a warnings.warn here so users aren't confused when their pipeline dtype changes?
There was a problem hiding this comment.
I just realized the warning shouldn't be output when self.pipeline_dtype is already torch.float.
There was a problem hiding this comment.
good point, fixed
There was a problem hiding this comment.
Please only warn on a single rank if possible. We don't want output spam.
There was a problem hiding this comment.
there is a warnings.warn all over that config file, not just the change I made. Shall we make another PR that fixes this for all the others?
(made a change, please let me know if that is ok, it needs log_single_rank)
|
/ok to test e0e74fd |
…e dtype changes Signed-off-by: mikail <mkhona@nvidia.com>
Signed-off-by: mikail <mkhona@nvidia.com>
Signed-off-by: mikail <mkhona@nvidia.com>
|
/ok to test c15e1dd |
Signed-off-by: mikail <mkhona@nvidia.com>
|
/ok to test 5580aee |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/22373578757 |
Signed-off-by: mikail <mkhona@nvidia.com>
Signed-off-by: mikail <mkhona@nvidia.com>
Signed-off-by: mikail <mkhona@nvidia.com>
…ar to unblock fp32_residual_connection When fp32_residual_connection=True the residual stream is fp32 and bias_dropout_add propagates that dtype to hidden_states. In the TE GPT layer spec input_layernorm/pre_mlp_layernorm is IdentityOp (folded into the fused TELayerNormColumnParallelLinear), so nothing casts back to params_dtype before this op. TE's set_activation_dtype strictly rejects input dtype != param dtype outside autocast, so the forward dies on the very first layer of a bf16 model with --fp32-residual-connection. Downcast input to layer_norm_weight.dtype at the top of forward. Residual-accumulation precision is preserved upstream in bias_dropout_add (fixed by NVIDIA#3504); only the input to LN+GEMM is brought back to params_dtype, which is what the op is configured to run in anyway. Add TestFp32ResidualTELayerNormLinear covering the path: SelfAttention from the TE submodules with bf16 params, fed an fp32 hidden_states. Without this fix the forward raises in TE; with the fix it returns a bf16 output of the expected shape.
Signed-off-by: mikail <mkhona@nvidia.com>
What does this PR do ?
Fix FP32 residual stream activations in Megatron which currently silently break.
Problem
The existing fp32_residual_connection config flag was broken, it never actually kept the residual stream in fp32. The core issue was in
_bias_dropout_add_func(used by every layer type), which downcast the fp32 residual to match the computation dtype (bf16/fp16):Previously: defeated fp32 residuals by casting residual DOWN to x's dtype:
residual = residual if residual.dtype == x.dtype else residual.to(x.dtype)This meant:
The embedding layer correctly emitted fp32 tensors when
fp32_residual_connection=TrueBut the very first
bias_dropout_addcall cast the residual back to bf16. The fp32 residual stream was lost from layer 0 onwardsAdditionally, the transformer layer never cast its residuals to fp32 — only the Mamba layer had its own
residual_in_fp32flag, which also got defeated by the same downcast.Solution
Reverse the casting direction in
_bias_dropout_add_func: instead of downcasting the residual, upcast x (and bias) to match the residual's dtype. This preserves the fp32 residual stream through every bias-dropout-add call.megatron/core/fusions/fused_bias_dropout.pyUpcast x/bias to residual dtype instead of downcasting residual to x dtypemegatron/core/transformer/transformer_layer.pyCast residual to fp32 at all 4 residual sites (self-attn, cross-attn, MLP, MoE router) whenfp32_residual_connection=Truemegatron/core/ssm/mamba_layer.pyRespect config.fp32_residual_connection in addition to the existingresidual_in_fp32flagmegatron/core/transformer/transformer_config.pyAuto-override pipeline_dtype to torch.float whenfp32_residual_connection=Trueto prevent PP communication dtype mismatchesWhy the old approach was wrong
The original downcast was added to prevent pipeline parallel communication hangs (the residual would upcast the output to fp32, mismatching the bf16 PP recv buffers). The correct fix is to set
pipeline_dtype = torch.floatwhen fp32 residuals are enabled, which this PR does automatically inTransformerConfig.__post_init__. The inference wrapper (abstract_model_inference_wrapper.py) already handled this correctly.Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.