Skip to content

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.

GRPO: latch the trainer's autocast precision on the efficient logprob path (#13968)

Daniel Han02d7f8ee3e32
diff --git a/tests/test_grpo_efficient_autocast_latch.py b/tests/test_grpo_efficient_autocast_latch.py
new file mode 100644
index 00000000000..8be9aab0d3f
--- /dev/null
+++ b/tests/test_grpo_efficient_autocast_latch.py
@@ -0,0 +1,91 @@
+# Unsloth Zoo - Utilities for Unsloth
+# Copyright 2023-present Daniel Han-Chen, Michael Han-Chen & the Unsloth team. All rights reserved.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+
+"""With no scoring forward (vLLM, beta = 0) the efficient call must still latch this trainer's precision."""
+
+import ast
+import os
+import textwrap
+import types
+from pathlib import Path
+
+import torch
+
+REPL_SRC = (
+ Path(__file__).resolve().parents[1] / "unsloth" / "models" / "rl_replacements.py"
+).read_text(encoding = "utf-8")
+
+
+def _function(name, parent = None):
+ tree = ast.parse(REPL_SRC)
+ scope = tree.body
+ if parent is not None:
+ scope = next(
+ n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == parent
+ ).body
+ node = next(n for n in scope if isinstance(n, ast.FunctionDef) and n.name == name)
+ return textwrap.dedent(ast.get_source_segment(REPL_SRC, node))
+
+
+def _namespace():
+ ns = {"torch": torch, "os": os}
+ exec(_function("_unsloth_grpo_autocast"), ns)
+ exec(
+ _function(
+ "_get_per_token_logps_and_entropies", "grpo_trainer__get_per_token_logps_and_entropies"
+ ),
+ ns,
+ )
+ return ns
+
+
+def test_efficient_call_latches_this_trainers_precision(monkeypatch):
+ ns = _namespace()
+ monkeypatch.setenv("ACCELERATE_MIXED_PRECISION", "fp16")
+ trainer = types.SimpleNamespace(
+ args = types.SimpleNamespace(mixed_precision = "bf16", bf16 = True, fp16 = False),
+ model = None,
+ )
+ out = ns["_get_per_token_logps_and_entropies"](
+ trainer, None, None, None, 4, compute_efficient = True
+ )
+ assert out == (None, None)
+ assert trainer._autocast_enabled is True
+ assert trainer._autocast_dtype is torch.bfloat16
+
+
+def test_efficient_call_keeps_an_existing_latch(monkeypatch):
+ ns = _namespace()
+ trainer = types.SimpleNamespace(
+ args = None, model = None, _autocast_enabled = False, _autocast_dtype = torch.float16
+ )
+ ns["_get_per_token_logps_and_entropies"](trainer, None, None, None, 4, compute_efficient = True)
+ assert trainer._autocast_enabled is False
+ assert trainer._autocast_dtype is torch.float16
+
+
+def test_legacy_logprob_replacement_latches_too(monkeypatch):
+ ns = {"torch": torch, "os": os}
+ exec(_function("_unsloth_grpo_autocast"), ns)
+ exec(_function("_get_per_token_logps", "grpo_trainer__get_per_token_logps"), ns)
+ monkeypatch.setenv("ACCELERATE_MIXED_PRECISION", "fp16")
+ trainer = types.SimpleNamespace(
+ args = types.SimpleNamespace(mixed_precision = "bf16", bf16 = True, fp16 = False),
+ model = None,
+ )
+ assert ns["_get_per_token_logps"](trainer, None, None, None, 4) is None
+ assert trainer._autocast_dtype is torch.bfloat16
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index c1fe4a7b6a0..55963ead6e9 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -2423,6 +2423,8 @@ def grpo_trainer__get_per_token_logps(function_name, function):
compute_efficient = False,
):
if True: # os.environ.get('UNSLOTH_USE_NEW_MODEL', '0') == '0':
+ # Older TRL calls this instead of _get_per_token_logps_and_entropies: latch here too.
+ _unsloth_grpo_autocast(self)
return None # Unsloth efficient GRPO
_unsloth_grpo_autocast(self)
@@ -2466,6 +2468,8 @@ def grpo_trainer__get_per_token_logps_and_entropies(function_name, function):
):
# All Unsloth code in this function is licensed under AGPL3.
if compute_efficient:
+ # No scoring forward may run (vLLM, beta = 0): latch precision here, not from the shared env.
+ _unsloth_grpo_autocast(self)
return None, None
else:
_unsloth_grpo_autocast(self)