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: sequence packing for the no-grad old/ref logp path (default-on) (#6738)

Daniel Han9407d491933d
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index 3be614cf4a8..0573fd5fd8e 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -29,6 +29,7 @@ from collections import defaultdict
from unsloth_zoo.rl_replacements import (
RL_REPLACEMENTS,
left_pack_padding,
+ create_completion_attention_mask,
chunked_selective_log_softmax,
_unsloth_get_mm_token_id,
_unsloth_fix_mm_token_type_ids,
@@ -48,7 +49,22 @@ from ..device_type import (
ALLOW_PREQUANTIZED_MODELS,
)
import textwrap
-from ._utils import _get_inference_mode_context_manager
+from ._utils import _get_inference_mode_context_manager, UNSLOTH_ENABLE_LOGGING
+
+# One-time GRPO sequence-packing gates; mirrored into the generated trainer cache via RL_PRE_ITEMS.
+UNSLOTH_GRPO_SEQ_PACKING_ON = os.environ.get("UNSLOTH_GRPO_SEQ_PACKING", "1").lower() not in (
+ "0",
+ "false",
+ "no",
+ "off",
+)
+# Packing needs zoo#840's masked-column guard in grpo_compute_loss (installed zoo is fixed per-process).
+try:
+ UNSLOTH_ZOO_HAS_MASKED_COL_GUARD = "torch.where(_keep, new" in inspect.getsource(
+ RL_REPLACEMENTS["grpo_compute_loss"]
+ )
+except Exception:
+ UNSLOTH_ZOO_HAS_MASKED_COL_GUARD = False
RL_EXTRA_ARGS = defaultdict(list)
RL_FUNCTIONS = defaultdict(list)
@@ -1359,6 +1375,212 @@ def grpo_trainer__get_per_token_logps_and_entropies(function_name, function):
)
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "1"
+ # ---- Sequence packing (default-on; disable with UNSLOTH_GRPO_SEQ_PACKING=0) ----
+ # One varlen [1, sum L] forward replaces the padded [B, Lmax] loop (also fixes the
+ # left-pad RoPE error). Self-verified against the per-row forward, re-checked as T
+ # grows; falls back if a backend ignores packed_seq_lengths.
+ logprobs = None
+ _pk_result = None
+ _pk_use = False
+ _pk_enabled = UNSLOTH_GRPO_SEQ_PACKING_ON
+ # Without zoo#840's masked-column guard, zeroed prompt/pad columns turn NaN in exp().
+ _pk_enabled = _pk_enabled and UNSLOTH_ZOO_HAS_MASKED_COL_GUARD
+ _pk_ok = getattr(unwrapped_model, "_unsloth_seq_packing_nograd_ok", None)
+ if (
+ _pk_enabled
+ and pixel_values is None
+ and token_type_ids is None
+ and mm_token_type_ids is None
+ and _pk_ok is not False
+ ):
+ try:
+ _pk_pad = self.processing_class.pad_token_id
+ _pk_keep = input_ids != _pk_pad
+ _pk_len = _pk_keep.sum(dim = 1)
+ _pk_len_cpu = _pk_len.tolist() # single GPU->CPU sync, reused below
+ _pk_nz_cpu = [_n for _n in _pk_len_cpu if _n > 0]
+ _pk_flat = input_ids[_pk_keep].unsqueeze(0)
+ _pk_T = _pk_flat.shape[1]
+ _pk_L = input_ids.shape[1]
+ _pk_W = logits_to_keep + max_left_pad
+ _pk_maxseg = max(_pk_nz_cpu) if _pk_nz_cpu else 0
+ # sliding-window models lose the per-sequence local window in a packed stream
+ _pk_sw = getattr(
+ getattr(unwrapped_model, "config", None), "sliding_window", None
+ )
+ _pk_sw_ok = not (isinstance(_pk_sw, int) and _pk_sw > 0 and _pk_maxseg > _pk_sw)
+ # per-row completion mask (same as the loss); prompt-only rows count as inactive
+ _pk_cmask = create_completion_attention_mask(
+ input_ids[:, -_pk_W:], left_pad_tokens_per_prompt, max_left_pad, _pk_pad
+ )
+ _pk_active = int(_pk_cmask.any(dim = 1).sum())
+ # skip the packed forward entirely at known-unsafe lengths (avoids a wasted pass / OOM)
+ _pk_unsafe = getattr(
+ unwrapped_model, "_unsloth_seq_packing_nograd_unsafe_T", None
+ )
+ # cap the flattened forward at one padded [batch_size, seq_len] mini-batch's
+ # token budget; anything larger uses the chunked padded loop
+ _pk_cap = batch_size * seq_len
+ if (
+ _pk_T >= 2
+ and _pk_T <= _pk_cap
+ and len(_pk_nz_cpu) > 0
+ and _pk_sw_ok
+ and not (_pk_unsafe is not None and _pk_T >= _pk_unsafe)
+ and (_pk_ok is True or _pk_active >= 2)
+ ):
+ # reset 0-based position_ids per segment
+ _pk_pos = (_pk_keep.cumsum(dim = 1) - 1)[_pk_keep].unsqueeze(0)
+ _pk_chunks = max(1, total_rows * multiplier)
+ _pk_nz_idx = _pk_keep.nonzero(
+ as_tuple = False
+ ) # [T, 2] = (row, col), row-major
+ _pk_within = _pk_nz_idx[1:, 0] == _pk_nz_idx[:-1, 0] # [T-1]
+ # per-row completion start after left-packing (matches create_completion_attention_mask)
+ _pk_cstart = (_pk_L - logits_to_keep) - left_pad_tokens_per_prompt # [rows]
+ _pk_ctgt = (_pk_nz_idx[1:, 1] >= _pk_cstart[_pk_nz_idx[1:, 0]]) & _pk_within
+ with _get_inference_mode_context_manager(model):
+ with torch.amp.autocast(device_type = "cuda", dtype = self._autocast_dtype):
+ # use_cache=False: a KV cache silently disables varlen packing
+ _pk_hidden = unwrapped_model(
+ input_ids = _pk_flat,
+ position_ids = _pk_pos,
+ packed_seq_lengths = torch.tensor(
+ _pk_nz_cpu, dtype = torch.int32, device = input_ids.device
+ ),
+ use_cache = False,
+ ).logits
+ _pk_sel = chunked_hidden_states_selective_log_softmax(
+ _pk_hidden[0, :-1, :][_pk_ctgt].unsqueeze(0),
+ lm_head,
+ _pk_flat[0, 1:][_pk_ctgt].unsqueeze(0),
+ _pk_chunks,
+ logit_scale_multiply,
+ logit_scale_divide,
+ logit_softcapping,
+ temperature,
+ )[0]
+ # GPT-OSS offload race guard (matches the padded loop)
+ device_synchronize()
+ # scatter each completion logprob back to its (row, col) so [:, -_pk_W:] matches padded
+ _pk_tgt = (_pk_nz_idx[1:, 0] * _pk_L + _pk_nz_idx[1:, 1])[_pk_ctgt]
+ _pk_result = (
+ torch.zeros(
+ total_rows * _pk_L,
+ dtype = torch.float32,
+ device = input_ids.device,
+ )
+ .index_put((_pk_tgt,), _pk_sel.to(torch.float32))
+ .view(total_rows, _pk_L)[:, -_pk_W:]
+ )
+ # re-verify when T or the longest segment grows past what was verified
+ # (a LongRoPE cache switch can change the result)
+ _pk_vT = int(
+ getattr(unwrapped_model, "_unsloth_seq_packing_nograd_verified_T", 0)
+ )
+ _pk_vS = int(
+ getattr(unwrapped_model, "_unsloth_seq_packing_nograd_verified_seg", 0)
+ )
+ # debug: hand-edit this condition to force re-verify every step
+ if _pk_ok is True and _pk_T <= _pk_vT and _pk_maxseg <= _pk_vS:
+ _pk_use = True # already verified for this shape
+ else:
+ # verify against the per-row forward (ground truth)
+ _pk_ref = torch.zeros_like(_pk_result)
+ with _get_inference_mode_context_manager(model):
+ with torch.amp.autocast(
+ device_type = "cuda", dtype = self._autocast_dtype
+ ):
+ for _pk_i in range(total_rows):
+ _pk_ni = _pk_len_cpu[_pk_i]
+ if _pk_ni < 2:
+ continue
+ _pk_rmask = _pk_keep[_pk_i]
+ _pk_real = input_ids[_pk_i][_pk_rmask].unsqueeze(0)
+ _pk_rpos = torch.arange(
+ _pk_ni, device = input_ids.device
+ ).unsqueeze(0)
+ _pk_rh = unwrapped_model(
+ input_ids = _pk_real,
+ position_ids = _pk_rpos,
+ use_cache = False,
+ ).logits
+ _pk_rsel = chunked_hidden_states_selective_log_softmax(
+ _pk_rh[:, :-1, :],
+ lm_head,
+ _pk_real[:, 1:],
+ 1,
+ logit_scale_multiply,
+ logit_scale_divide,
+ logit_softcapping,
+ temperature,
+ )[0]
+ _pk_rcols = _pk_rmask.nonzero(as_tuple = False).squeeze(1)[
+ 1:
+ ] - (_pk_L - _pk_W)
+ _pk_rkeep = _pk_rcols >= 0
+ _pk_ref[_pk_i, _pk_rcols[_pk_rkeep]] = _pk_rsel[
+ _pk_rkeep
+ ].to(torch.float32)
+ device_synchronize()
+ # compare over the loss-mask region only
+ _pk_cm = _pk_cmask.float()
+ _pk_diff = float(((_pk_result - _pk_ref).abs() * _pk_cm).max())
+ if UNSLOTH_ENABLE_LOGGING:
+ print(
+ f"[Unsloth] GRPO seq-packing (no-grad) verify: T={_pk_T} maxseg={_pk_maxseg} packed-vs-perrow max|d|={_pk_diff:.4f}",
+ flush = True,
+ )
+ # kernel-noise floor ~0.25; cross-sample contamination is >= 2.4
+ if _pk_diff < 7e-1:
+ unwrapped_model._unsloth_seq_packing_nograd_ok = True
+ # widen the trusted shape only when >= 2 completion rows exercised
+ # cross-sample packing; single-row passes prove nothing
+ if _pk_active >= 2:
+ unwrapped_model._unsloth_seq_packing_nograd_verified_T = max(
+ _pk_vT, _pk_T
+ )
+ unwrapped_model._unsloth_seq_packing_nograd_verified_seg = max(
+ _pk_vS, _pk_maxseg
+ )
+ _pk_ok = True
+ _pk_use = True
+ else:
+ _pk_use = False
+ if _pk_diff >= 1.5:
+ # contamination (attention ignores the packed mask): disable packing
+ unwrapped_model._unsloth_seq_packing_nograd_ok = False
+ else:
+ # likely a length boundary (LongRoPE): mark unsafe, keep smaller shapes
+ unwrapped_model._unsloth_seq_packing_nograd_unsafe_T = (
+ _pk_T if _pk_unsafe is None else min(_pk_unsafe, _pk_T)
+ )
+ if UNSLOTH_ENABLE_LOGGING:
+ print(
+ f"[Unsloth] GRPO seq-packing (no-grad) fell back at T={_pk_T} (diff={_pk_diff:.3f})",
+ flush = True,
+ )
+ except Exception as _pk_err:
+ # any failure: drop intermediates, use the padded loop, do not retry
+ _pk_hidden = None
+ _pk_sel = None
+ _pk_result = None
+ _pk_use = False
+ if isinstance(_pk_err, torch.cuda.OutOfMemoryError):
+ torch.cuda.empty_cache()
+ unwrapped_model._unsloth_seq_packing_nograd_ok = False
+ if UNSLOTH_ENABLE_LOGGING:
+ print(
+ f"[Unsloth] GRPO sequence-packing (no-grad) disabled (fell back to padded): {_pk_err!r}",
+ flush = True,
+ )
+ if _pk_use and _pk_result is not None:
+ logprobs = _pk_result # verified -> skip the loop
+ zipped_inputs = []
+ else:
+ # free packed intermediates before running the padded loop
+ _pk_hidden = _pk_sel = _pk_result = _pk_ref = None
+
with _get_inference_mode_context_manager(model):
for (
input_ids_chunk,
@@ -1443,7 +1665,8 @@ def grpo_trainer__get_per_token_logps_and_entropies(function_name, function):
# However, it seems that this line does not slow down or disrupt models.
device_synchronize()
all_logprobs_list.append(logprobs_chunk)
- logprobs = torch.cat(all_logprobs_list, dim = 0)
+ if logprobs is None: # padded fallback when packing was not used
+ logprobs = torch.cat(all_logprobs_list, dim = 0)
entropies = None
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "0"
@@ -1523,6 +1746,24 @@ RL_PRE_ITEMS["grpo_trainer"].append(inspect.getsource(grpo_accumulated_loss))
RL_PRE_ITEMS["grpo_trainer"].append(grpo_compute_loss_slow)
RL_PRE_ITEMS["grpo_trainer"].append(inspect.getsource(grpo_update_SamplingParams))
RL_PRE_ITEMS["grpo_trainer"].append(inspect.getsource(_get_inference_mode_context_manager))
+# inspect.getsource inlines function bodies but not module imports, so constants the inlined
+# grpo functions reference (e.g. UNSLOTH_ENABLE_LOGGING) must be redefined in the generated cache.
+RL_PRE_ITEMS["grpo_trainer"].append(
+ "import os as _unsloth_os\n"
+ "UNSLOTH_ENABLE_LOGGING = _unsloth_os.environ.get('UNSLOTH_ENABLE_LOGGING', '0') in ('1', 'True', 'true')\n"
+)
+# One-time sequence-packing gates, same values as the module-top constants above.
+RL_PRE_ITEMS["grpo_trainer"].append(
+ "UNSLOTH_GRPO_SEQ_PACKING_ON = _unsloth_os.environ.get('UNSLOTH_GRPO_SEQ_PACKING', '1').lower() not in ('0', 'false', 'no', 'off')\n"
+)
+RL_PRE_ITEMS["grpo_trainer"].append(
+ "try:\n"
+ " import inspect as _unsloth_inspect\n"
+ " from unsloth_zoo.rl_replacements import RL_REPLACEMENTS as _unsloth_zoo_RL\n"
+ " UNSLOTH_ZOO_HAS_MASKED_COL_GUARD = 'torch.where(_keep, new' in _unsloth_inspect.getsource(_unsloth_zoo_RL['grpo_compute_loss'])\n"
+ "except Exception:\n"
+ " UNSLOTH_ZOO_HAS_MASKED_COL_GUARD = False\n"
+)
# Edit _get_per_token_logps to handle mixed precision