verl icon indicating copy to clipboard operation
verl copied to clipboard

[BUG] FSDP engine forward_step hardcodes bf16 autocast and ignores configured mixed-precision dtype

Open ChangyiYang opened this issue 7 months ago • 1 comments

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=fp16 is ignored by forward_step()
  • setting model_dtype=fp32 or 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 resolved param_dtype used to construct FSDP/FSDP2
  • if the effective dtype is torch.float32, it should avoid autocast entirely and use nullcontext()

Suggested fix:

  • store the resolved param_dtype on the engine during _build_fsdp_module()
  • use that stored dtype in forward_step() instead of hardcoding torch.bfloat16
  • skip autocast when the resolved dtype is fp32

ChangyiYang avatar Apr 08 '26 21:04 ChangyiYang

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:

  1. The engine properly resolves param_dtype from mixed_precision config during FSDP/FSDP2 setup
  2. But forward_step() hardcodes torch.bfloat16 in autocast, ignoring the resolved dtype

Impact confirmation:

  • Setting mixed_precision.param_dtype=fp16 or disabling mixed precision has no effect on the forward path
  • The older actor path correctly uses self.param_dtype for 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.

Kirrito-k423 avatar Apr 09 '26 03:04 Kirrito-k423