[bug] Custom MoE + fp32 master weights: bf16/fp32 dtype mismatch crash in forward
Problem
Custom MoE models trained with fp32 master weights (--fsdp.param_dtype bf16
with fp32 parameter storage) crash in the first forward pass with:
RuntimeError: expected mat1 and mat2 to have the same dtype, but got:
c10::BFloat16 != float
The failing matmul is the Qwen3.5-MoE linear-attention in_proj_qkv (nn.Linear,
text hidden 2048 -> 8192): the incoming activation is bf16 but the weight computes
in fp32. Reproduced with LoRA SFT on Qwen3.6-35B-A3B
(Qwen3_5MoeForConditionalGeneration), ep_size=4, and reproduces under every gradient_checkpoint mode (none/selective/full) — it is a plain forward
crash, not an activation-checkpoint issue.
This is distinct from NVIDIA-NeMo/Automodel#3325: NVIDIA-NeMo/Automodel#3325 is the post-dispatch CheckpointError
that appears only with gradient_checkpoint full after this crash is resolved.
With full, this dtype crash fires first; both must be fixed for the shipped
flagship MoE recipes to run.
Root cause
_make_compute_dtype_fn in nemo_automodel/components/distributed/parallelizer_utils.py resolves each
parameter's compute dtype by precedence: (1) pinned fp32
(_keep_in_fp32_modules_strict), (2) tensor._hf_compute_dtype recorded at
checkpoint load, (3) a fallback.
-
Custom models never populate precedence #2. Registered architectures are
built through the custom-model path in_transformers/model_init.py, which
returns the constructed module before weight loading and never calls_restore_loaded_model_dtype. Sotensor._hf_compute_dtypeis never set and
every parameter of a custom model falls through to the #3 fallback. -
The fallback used a module-global uniformity flag. It applied the policy
compute dtype (bf16) to unhinted fp32 storage only when all floating params
in the module shared one storage dtype (storage_is_uniform). -
A single bf16 sibling flips that flag. In a Qwen3.5-MoE decoder layer with
fp32 master weights, every floating param is fp32 storage exceptmlp.shared_expert_gate.weight, which is constructed bf16. So the module's
storage set is{float32, bfloat16}andstorage_is_uniform=False.Runtime probe of the layer holding
in_proj_qkv:storage_is_uniform=False floating_storage_dtypes={torch.float32, torch.bfloat16} linear_attn.in_proj_qkv.weight storage=torch.float32 pinned=False hf=None mlp.shared_expert_gate.weight storage=torch.bfloat16 pinned=False hf=None -
With the flag
False, the fallback returns each param's own storage dtype, so
the fp32 master weights (in_proj_qkv,out_proj, ...) are classified as
fp32 compute instead of bf16. -
NVIDIA-NeMo/Automodel#3255 (
preserve ERNIE router and dense Qwen3.5 SSM precision) setcast_forward_inputs=Falseon fp32 FSDP units. The misclassified-fp32in_proj_qkvunit therefore no longer casts its incoming bf16 activation, and
thebf16 @ fp32matmul crashes. Before NVIDIA-NeMo/Automodel#3255 this misclassification only
caused harmless extra fp32 compute; NVIDIA-NeMo/Automodel#3255 turned it into a hard failure.
Suggested fix
Resolve the #3 fallback per parameter instead of via a module-global flag: an
unhinted fp32 storage under a lower-precision policy is an fp32 master weight and
should compute in the policy dtype; any other storage keeps its own dtype. A
single genuinely-bf16 sibling (shared_expert_gate) then no longer forces the
layer's fp32 master weights into fp32 compute. The (storage, compute) grouping
already keeps each FSDP unit storage-uniform, so _init_mp_dtypes is unaffected.
A PR implementing this follows.