Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses a dtype mismatch error that occurs with FSDP2 when LoRA adapters are used with a base model loaded in a precision other than fp32. The core of the change is in verl/utils/fsdp_utils.py, where the apply_fsdp2 function is updated to differentiate between LoRA modules and base model transformer layers. By introducing an is_lora flag, the logic now separates trainable leaf modules (like LoRA adapters) and shards them independently before sharding the main transformer blocks. This prevents parameters with different data types from being grouped under the same FSDP wrapper, which was the source of the error. The changes are logical, well-targeted, and consistently applied across all call sites. The fix appears to be correct and should resolve the reported issue.
What does this PR do?
Fixes #3470
Currently, when the base model is loaded in a dtype other than fp32, its parameters end up in a different dtype than the LoRA adapters (since we do not set
autocast_adapter_dtype=Falseand the adapter parameters are cast to fp32 by default). With FSDP1, the wrapping policies for base model modules and LoRA modules are handled separately, so this mismatch does not cause issues. For FSDP2, however, there is no equivalent handling, and base model parameters and LoRA parameters can end up under the same FSDP wrapper, leading to dtype mismatch errors. This PR fixes this issue.Checklist Before Starting
[{modules}] {type}: {description}(This will be checked by the CI){modules}includefsdp,megatron,sglang,vllm,rollout,trainer,ci,training_utils,recipe,hardware,deployment,ray,worker,single_controller,misc,perf,model,algo,env,tool,ckpt,doc,data,like[megatron, fsdp, doc]{type}is infeat,fix,refactor,chore,test[BREAKING]to the beginning of the title.[BREAKING][fsdp, megatron] feat: dynamic batchingTest
Running
python -m verl.trainer.main_ppowithactor_rollout_ref.actor.strategy=fsdp2andactor_rollout_ref.actor.fsdp_config.model_dtype=bfloat16in the current main branch gives the error:but it runs well in this branch.
API and Usage Example
No API changes.
Design & Code Changes
This change updates the FSDP2 wrapping logic to explicitly identify LoRA modules and shard them separately from the base model modules.
Checklist Before Submitting
Important
Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.
pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=alwaysci-requestchannel in theverlSlack workspace. (If not accessible, please try the Feishu group (飞书群).)