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: log the clip_ratio metrics for loss_type luspo (#17537)

Vineeth Sai Varikuntlaeb3491890725
diff --git a/tests/test_grpo_luspo_clip_metrics.py b/tests/test_grpo_luspo_clip_metrics.py
new file mode 100644
index 00000000000..a8b40006549
--- /dev/null
+++ b/tests/test_grpo_luspo_clip_metrics.py
@@ -0,0 +1,30 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""GRPO compute_loss logs the clip_ratio metrics for loss_type luspo like TRL does."""
+
+from __future__ import annotations
+
+import pytest
+import torch
+
+from tests.test_grpo_metrics_bucket import BATCH, _compute_loss, _inputs, _trainer
+
+
[email protected]("loss_type", ["grpo", "bnpo", "dr_grpo", "dapo", "luspo"])
+def test_clip_ratio_metrics_logged(loss_type):
+ trainer = _trainer(training = True, should_evaluate = False)
+ trainer.loss_type = trainer.args.loss_type = loss_type
+ # luspo is sequence-level: zoo returns coef_1 as (B, 1). Row 0 low-clipped, row 1 high-clipped.
+ coef_1 = torch.tensor([[0.5], [1.5]])
+ inputs = _inputs()
+ inputs["advantages"] = torch.tensor([-1.0, 1.0])
+ assert coef_1.shape[0] == BATCH
+ _compute_loss(4.0, coef_1)(trainer, trainer.model, inputs)
+ trainer.log({})
+ metrics = trainer._metrics["train"]
+ # TRL global_masked_mean on a (B, 1) tensor: sum / B.
+ assert metrics["clip_ratio/low_mean"] == [0.5]
+ assert metrics["clip_ratio/high_mean"] == [0.5]
+ assert metrics["clip_ratio/region_mean"] == [1.0]
+ assert {"clip_ratio/low_min", "clip_ratio/high_max"} <= set(metrics)
diff --git a/tests/test_grpo_metrics_bucket.py b/tests/test_grpo_metrics_bucket.py
index ef87e974936..b703f9a7983 100644
--- a/tests/test_grpo_metrics_bucket.py
+++ b/tests/test_grpo_metrics_bucket.py
@@ -34,7 +34,7 @@ HELPERS = {
BATCH, PROMPT, COMPLETION = 2, 3, 4
-def _compute_loss(completion_length):
+def _compute_loss(completion_length, coef_1 = None):
tree = ast.parse(SOURCE_PATH.read_text(encoding = "utf-8"), filename = str(SOURCE_PATH))
nodes = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name in HELPERS]
namespace = {"torch": torch, "inspect": inspect}
@@ -51,7 +51,7 @@ def _compute_loss(completion_length):
torch.tensor(0.25),
None,
None,
- ones,
+ ones if coef_1 is None else coef_1,
ones,
)
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index babf7789b8e..0bad1ef6190 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -5654,7 +5654,7 @@ def grpo_trainer_compute_loss(function_name, function):
if advantages.dim() == 1:
advantages = advantages.unsqueeze(1)
- if self.loss_type in ["grpo", "bnpo", "dr_grpo", "dapo"]:
+ if self.loss_type in ["grpo", "bnpo", "dr_grpo", "dapo", "luspo"]:
is_low_clipped = (coef_1 < 1 - self.epsilon_low) & (advantages < 0)
is_high_clipped = (coef_1 > 1 + self.epsilon_high) & (advantages > 0)
is_region_clipped = is_low_clipped | is_high_clipped