Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Export ACCELERATE_MIXED_PRECISION when bf16/fp16 is set explicitly (fixes #4891) (#7534)
Irving Ernestoa335c006052d
diff --git a/tests/python/test_v100_fullft_precision.py b/tests/python/test_v100_fullft_precision.pyindex 49abcb4ad63..02f2ec572e4 100644--- a/tests/python/test_v100_fullft_precision.py+++ b/tests/python/test_v100_fullft_precision.py@@ -191,3 +191,72 @@ def test_genuine_bf16_model_with_fp16_still_raises():bf16 = False,)assert raised == "TypeError"+++def test_explicit_bf16_exports_bf16_env():+ # Issue #4891: bfloat16 model, user explicitly sets bf16=True. The env var+ # must still be exported: downstream readers (unsloth_zoo/rl_replacements.py+ # and unsloth/models/rl_replacements.py) default ACCELERATE_MIXED_PRECISION+ # to 'fp16' when it is unset, which wraps a bfloat16 model in a float16+ # autocast and crashes GRPO inside matmul_lora.+ fp16, bf16, amp, raised = _decide(+ torch.bfloat16,+ bf16_supported = True,+ force_float32 = False,+ full_finetuning = False,+ mixed_precision = "float32",+ fp16 = False,+ bf16 = True,+ )+ assert raised is None+ assert (fp16, bf16) == (False, True)+ assert amp == "bf16"+++def test_explicit_fp16_exports_fp16_env():+ # float16 model with fp16 explicitly requested must export 'fp16'.+ fp16, bf16, amp, raised = _decide(+ torch.float16,+ bf16_supported = False,+ force_float32 = False,+ full_finetuning = False,+ mixed_precision = "float32",+ fp16 = True,+ bf16 = False,+ )+ assert raised is None+ assert (fp16, bf16) == (True, False)+ assert amp == "fp16"+++def test_force_float32_beats_explicit_bf16():+ # force_float32 must keep precedence over the explicit-flag export.+ fp16, bf16, amp, raised = _decide(+ torch.float32,+ bf16_supported = True,+ force_float32 = True,+ full_finetuning = False,+ mixed_precision = "float32",+ fp16 = False,+ bf16 = True,+ )+ assert raised is None+ assert (fp16, bf16) == (False, False)+ assert amp == "no"+++def test_unsloth_mixed_precision_bfloat16_beats_explicit_bf16():+ # UNSLOTH_MIXED_PRECISION='bfloat16' (pure bf16 full FT, no autocast) must+ # keep precedence over the explicit-flag export.+ fp16, bf16, amp, raised = _decide(+ torch.bfloat16,+ bf16_supported = True,+ force_float32 = False,+ full_finetuning = True,+ mixed_precision = "bfloat16",+ fp16 = False,+ bf16 = True,+ )+ assert raised is None+ assert (fp16, bf16) == (False, False)+ assert amp == "no"diff --git a/unsloth/models/rl.py b/unsloth/models/rl.pyindex eeab8fbaca4..e0636260f13 100644--- a/unsloth/models/rl.py+++ b/unsloth/models/rl.py@@ -1049,6 +1049,12 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):" os.environ['ACCELERATE_MIXED_PRECISION'] = 'no'\n"" if hasattr(args, 'mixed_precision'): args.mixed_precision = 'no'\n"" # args.mixed_precision is a new argument which needs to be set now\n"+ "elif use_bf16 or use_fp16:\n"+ " # transformers <5 exported this itself from fp16/bf16; 5.x dropped the write, so an\n"+ " # explicit flag left it unset and GRPO readers defaulted to 'fp16', wrapping a\n"+ " # bfloat16 model in a float16 autocast. See unslothai/unsloth#4891.\n"+ " os.environ['ACCELERATE_MIXED_PRECISION'] = 'bf16' if use_bf16 else 'fp16'\n"+ " if hasattr(args, 'mixed_precision'): args.mixed_precision = 'bf16' if use_bf16 else 'fp16'\n""\n")extra_args += mixed_precision