verl icon indicating copy to clipboard operation
verl copied to clipboard

[BUG] Model loading ignores _keep_in_fp32_modules — silent precision degradation for fp32-keep models (Inkling, Qwen3-Next)

Open SelfSL opened this issue 4 months ago • 4 comments

System Info

----------Python Info----------
Version      : 3.12.3
------------Pip Info-----------
vllm         : 0.24.1.dev0+gee0da84ab.d20260706.cu129
sglang       : 0.0.0.dev200+g6a60304af
ray          : 2.56.0
torch        : 2.12.0a0+git220ecc2
----------verl Info-----------
Version      : 0.9.0.dev
Commit Hash  : 9481350ec2cd7cf923a47d8d1e207edae25a1297
----------Environment----------
CUDA Runtime : 12.9
----------System Info----------
CPU Memory   : 4011.88 GB
GPU Count    : 8 x NVIDIA B300 SXM6 AC (268.59 GB)

Supplementary: transformers 5.14.1; zero-hit claim re-verified on main 2026-07-18 (grep -rn keep_in_fp32 verl/ on a fresh clone).

Information

  • [ ] The official example scripts
  • [x] My own modified scripts

Tasks

  • [ ] An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • [x] My own task or dataset (give details below)

Reproduction

No verl load path consults HF's _keep_in_fp32_modules / _keep_in_fp32_modules_strict (zero grep hits repo-wide). Minimal repro — a toy model declaring a keep-list, loaded through fsdp2_load_full_state_dict; the kept module comes back bf16:

# torchrun --nproc-per-node 1 repro.py
import torch, torch.distributed as dist
from torch.distributed.fsdp import fully_shard, MixedPrecisionPolicy
from transformers import PretrainedConfig, PreTrainedModel
from verl.utils.fsdp_utils import fsdp2_load_full_state_dict
 
class TinyCfg(PretrainedConfig):
    model_type = "tiny_fp32keep"
 
class TinyModel(PreTrainedModel):
    config_class = TinyCfg
    _keep_in_fp32_modules_strict = ["sensitive"]
    def __init__(self, config):
        super().__init__(config)
        self.sensitive = torch.nn.Linear(8, 8, bias=False)   # declared fp32-keep
        self.other = torch.nn.Linear(8, 8, bias=False)
 
dist.init_process_group("nccl")
torch.cuda.set_device(0)
 
ref = TinyModel(TinyCfg())                      # from_pretrained honors the list -> fp32
full_sd = ref.state_dict()
 
with torch.device("meta"):
    m = TinyModel(TinyCfg())
m = m.to(torch.bfloat16)                        # typical verl-style uniform dtype
fully_shard(m, mp_policy=MixedPrecisionPolicy(param_dtype=torch.bfloat16))
fsdp2_load_full_state_dict(m, full_sd)
 
print("sensitive dtype:", m.sensitive.weight.dtype)   # observed: torch.bfloat16
assert m.sensitive.weight.dtype == torch.float32, "keep_in_fp32 dropped"

Large-scale impact (measured, Inkling 975B — declares _keep_in_fp32_modules_strict = ["attn_sconv","mlp_sconv","k_sconv","v_sconv"], 264 depthwise convs on the K/V/residual streams): loaded bf16, each conv adds only ~0.2–0.5% relative error, but it compounds across 66 layers — per-layer divergence vs from_pretrained grows 0.0006 → 0.11 (layer 11) → ~0.65 mid-stack. Prompt "The capital of France is", greedy, bf16, FSDP2 x8: from_pretrained -> ' Paris'; keep-list-blind load of identical weights -> ','. Per-module value checks pass (values right, dtype wrong); only end-to-end differential tracing exposes it. Full layer-curve logs available.

Expected behavior

Modules named in _keep_in_fp32_modules(_strict) retain fp32 through verl's materialization, matching from_pretrained numerics (or, at minimum, a loud warning that the policy is being dropped).

Fix sketch (validated end-to-end at 975B; happy to PR):

  1. At build: resolve keys = _keep_in_fp32_modules ∪ _keep_in_fp32_modules_strict from the model class; cast matching submodules to fp32 before sharding.
  2. Wrap each matching module as its own fully_shard unit with a non-casting policy (param_dtype=None) — FSDP2 asserts uniform dtype per unit, so a dtype-only patch is insufficient; nest before the parent layer wrap (same pattern as MoE-expert child units).
  3. Loaders copy with tensor.to(param.dtype) so fp32-kept params upcast exactly. With this applied, our bare-built FSDP2 Inkling reproduces from_pretrained bit-exactly across all 66 layers (per-layer rel ≡ 0, identical greedy decoding).

SelfSL avatar Jul 18 '26 21:07 SelfSL

Hi @SelfSL, thanks for the detailed report and reproduction.

I checked the current main branch and confirmed that the FSDP2 path does not currently handle _keep_in_fp32_modules or _keep_in_fp32_modules_strict.

I’m interested in working on this issue, but I’d like to coordinate first to avoid duplicating your work. Are you already preparing a PR?

My proposed scope would be:

  • preserve the declared FP32 modules during FSDP2 wrapping and full-state-dict loading;
  • handle both regular and strict module matching, including nested modules and possible wrapping conflicts;
  • add a minimal distributed regression test verifying parameter dtypes, forward/backward correctness, and state-dict round-trip behavior;
  • compare loss and gradients against an unsharded reference, and validate the implementation on A100 GPUs.

I can first share a minimal reproduction and proposed design before changing the implementation. If you are already working on the fix, I’d also be happy to collaborate or help with distributed validation instead of creating a competing PR.

Would this scope align with what you had in mind?

chengcuiping avatar Jul 26 '26 01:07 chengcuiping

Hi, the fix sketch in this issue is very clear. I'd like to implement it if it's still unclaimed.

To summarize the approach from the issue:

  1. Resolve _keep_in_fp32_modules ∪ _keep_in_fp32_modules_strict from the model class after loading config
  2. Cast matching submodules to fp32 before sharding
  3. Nest each matching module as its own fully_shard unit with param_dtype=None to prevent FSDP2 from recasting it (same pattern as MoE-expert child units)
  4. Ensure loaders copy with tensor.to(param.dtype) so fp32-kept params upcast correctly

I work with FSDP2 and precision-aware training patterns — I've been contributing CUDA quantization work to vLLM (PR vllm-project/vllm#49828) and understand the dtype-sharding interaction well.

Could I be assigned? I'll validate end-to-end on a minimal model with declared _keep_in_fp32_modules and include a test verifying dtype preservation post-load.

priyanka25aug avatar Jul 26 '26 19:07 priyanka25aug

@SelfSL @chengcuiping — tagging to coordinate: @chengcuiping, are you actively working on a PR for this? Happy to collaborate or focus on a different part (e.g. the distributed validation suite) rather than duplicate the implementation work. @SelfSL, since you filed the issue with a full fix sketch and mentioned you're happy to PR — are you planning to open one?

priyanka25aug avatar Jul 26 '26 19:07 priyanka25aug

Yes — I’m actively working on this.

I now have a local implementation against main at 983cb0f, including a failing-before-fix toy reproduction and passing single-GPU and DP=2 validation on A100s. I’m completing a final audit of tied/shared-parameter handling, the FSDP2 child mixed-precision policy, and CI/pre-commit integration before opening a draft PR.

@priyanka25aug, thanks for coordinating. Once the draft branch is public, validation on a different PyTorch/Transformers stack would be very useful and would avoid duplicating the core implementation.

@SelfSL, I would especially appreciate your view on param_dtype=None versus torch.float32 for the child FSDP2 units, given your end-to-end Inkling validation. I’ll link the draft here after the remaining audit passes.

chengcuiping avatar Jul 27 '26 07:07 chengcuiping