[BUG] FSDP engine forward_step hardcodes bf16 autocast and ignores configured mixed-precision dtype
The new FSDP engine path hardcodes torch.autocast(..., dtype=torch.bfloat16) inside forward_step(), which makes the actual forward execution dtype diverge from the configured FSDP precision settings.
This appears to be a separate issue from #3243. That issue is about model_dtype override not being accepted from CLI/config; this one is about the execution path ignoring the resolved precision even after the engine has parsed mixed-precision config.
Relevant code:
verl/workers/engine/fsdp/transformer_impl.py
Current implementation:
with torch.autocast(device_type=device_name, dtype=torch.bfloat16):
raw_output = self.module(...)
with torch.autocast(device_type=device_name, dtype=torch.bfloat16):
model_output = self.prepare_model_outputs(...)
However, earlier in the same engine, the code already resolves mixed-precision dtype and passes it into FSDP/FSDP2:
mixed_precision_config = self.engine_config.mixed_precision
if mixed_precision_config is not None:
param_dtype = PrecisionType.to_dtype(mixed_precision_config.get("param_dtype", "bf16"))
reduce_dtype = PrecisionType.to_dtype(mixed_precision_config.get("reduce_dtype", "fp32"))
buffer_dtype = PrecisionType.to_dtype(mixed_precision_config.get("buffer_dtype", "fp32"))
else:
param_dtype = torch.bfloat16
reduce_dtype = torch.float32
buffer_dtype = torch.float32
mixed_precision = MixedPrecision(param_dtype=param_dtype, reduce_dtype=reduce_dtype, buffer_dtype=buffer_dtype)
...
mp_policy = MixedPrecisionPolicy(param_dtype=param_dtype, reduce_dtype=reduce_dtype, cast_forward_inputs=True)
So the engine correctly resolves param_dtype for FSDP setup, but forward_step() still forces bf16 autocast regardless of that resolved value.
Impact:
- setting
mixed_precision.param_dtype=fp16is ignored byforward_step() - setting
model_dtype=fp32or disabling mixed precision can still result in bf16 autocast in this path - the effective forward dtype is inconsistent with the configured FSDP precision policy
For comparison, the older actor path uses the configured dtype for autocast instead of hardcoding bf16:
with torch.autocast(device_type=self.device_name, dtype=self.param_dtype):
Expected behavior:
forward_step()should derive its autocast dtype from the same resolvedparam_dtypeused to construct FSDP/FSDP2- if the effective dtype is
torch.float32, it should avoid autocast entirely and usenullcontext()
Suggested fix:
- store the resolved
param_dtypeon the engine during_build_fsdp_module() - use that stored dtype in
forward_step()instead of hardcodingtorch.bfloat16 - skip autocast when the resolved dtype is fp32
Thank you for the detailed bug report and root cause analysis.
This is a clear inconsistency issue in the FSDP engine implementation. You've correctly identified that:
- The engine properly resolves
param_dtypefrommixed_precisionconfig during FSDP/FSDP2 setup - But
forward_step()hardcodestorch.bfloat16in autocast, ignoring the resolved dtype
Impact confirmation:
- Setting
mixed_precision.param_dtype=fp16or disabling mixed precision has no effect on the forward path - The older actor path correctly uses
self.param_dtypefor autocast - This can cause unexpected dtype mismatches during training
Suggested fix alignment:
Your proposed solution is correct:
# Instead of:
with torch.autocast(device_type=device_name, dtype=torch.bfloat16):
# Should be:
with torch.autocast(device_type=device_name, dtype=self.param_dtype):
# Or skip autocast when param_dtype is fp32
Additional consideration:
When param_dtype is torch.float32, autocast should be skipped entirely:
if self.param_dtype == torch.float32:
context = nullcontext()
else:
context = torch.autocast(device_type=device_name, dtype=self.param_dtype)
with context:
raw_output = self.module(...)
This ensures consistent behavior with the FSDP precision policy.
Would you be interested in submitting a PR for this fix?
Given your clear understanding of the code paths, a PR would be welcome. PR #5933 appears to have addressed a similar issue (param_dtype usage in autocast), so you may want to check if that PR covers the FSDP engine path or if additional changes are needed here.