Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
pre-commit-ci[bot]
e264d42413 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-04-16 21:55:16 +00:00
Daniel Han
e5ee16b8a1 Add FORCE_BFLOAT16 for Gemma-4 to prevent fp16 NaN in RL training
Gemma-4 produces NaN during GRPO training with dtype=torch.float16
due to torch.compile backward graph overflowing fp16 intermediates.
FORCE_FLOAT32 does not fix this because it still runs fp16 compute
(loads bf16 then casts to fp16 with no autocast). Instead, silently
switch to bfloat16 which has sufficient range to avoid overflow.

Adds a new FORCE_BFLOAT16 list separate from FORCE_FLOAT32. When a
user requests fp16 for Gemma-4 on a bf16-capable device, the dtype is
transparently switched to bfloat16 with a warning message. No changes
to the training pipeline or mixed precision behavior are needed.

Verified: 12 GRPO steps, ga=4, num_generations=2, all NaN-free.
2026-04-16 21:55:00 +00:00
Daniel Han
d323b47805 Add Gemma-4 to FORCE_FLOAT32 to prevent fp16 NaN in RL training
Gemma-4 produces NaN during GRPO training with dtype=torch.float16
due to torch.compile + fp16 autocast interaction in the backward pass.
Same class of issue as Gemma-3. Forces bfloat16 + float32 mixed
precision when users request fp16.
2026-04-16 20:31:49 +00:00

View file

@ -109,6 +109,13 @@ FORCE_FLOAT32 = [
"qwen3_5", # Qwen3.5 GDN layers produce NaN grad norms in float16 training "qwen3_5", # Qwen3.5 GDN layers produce NaN grad norms in float16 training
] ]
# Models that must use bfloat16 instead of float16.
# torch.compile backward graphs overflow fp16 intermediates for these models.
FORCE_BFLOAT16 = [
"gemma4,", # Add comma bc gemma4 will match gemma4_text
"gemma4text", # Gemma4TextModel (standalone text-only Gemma4)
]
global DISABLE_COMPILE_MODEL_NAMES global DISABLE_COMPILE_MODEL_NAMES
# Must be alphabetically sorted for each entry # Must be alphabetically sorted for each entry
@ -1373,6 +1380,24 @@ class FastModel(FastBaseModel):
os.environ["UNSLOTH_FORCE_FLOAT32"] = "1" os.environ["UNSLOTH_FORCE_FLOAT32"] = "1"
dtype = torch.bfloat16 # Change to bfloat16 loading dtype = torch.bfloat16 # Change to bfloat16 loading
break break
# Switch fp16 to bf16 for models whose torch.compile backward overflows fp16
global FORCE_BFLOAT16
for disable_name in FORCE_BFLOAT16:
if (
(
disable_name.lower()
== model_type_arch.lower().replace("-", "").replace("_", "")
or disable_name.lower() in model_types_all
)
and dtype == torch.float16
and SUPPORTS_BFLOAT16
):
logger.warning_once(
f"Unsloth: {model_type_arch} does not support float16 training. "
f"Switching to bfloat16."
)
dtype = torch.bfloat16
break
# Apply gradient checkpointing with smart heuristics # Apply gradient checkpointing with smart heuristics
use_gradient_checkpointing = apply_unsloth_gradient_checkpointing( use_gradient_checkpointing = apply_unsloth_gradient_checkpointing(
use_gradient_checkpointing, max_seq_length, dtype use_gradient_checkpointing, max_seq_length, dtype