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.

Context parallel and async GRPO: recognise gated fast forwards (#15559)

Daniel Han68fad07c24f5
diff --git a/tests/test_gated_fast_forward_checks.py b/tests/test_gated_fast_forward_checks.py
new file mode 100644
index 00000000000..844813e86ff
--- /dev/null
+++ b/tests/test_gated_fast_forward_checks.py
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Fast-forward checks must see through the pre_patch gate (class forward is a wrapper, so `is` fails)."""
+
+import torch
+
+
+def _gated_class(fast):
+ from unsloth.models import llama as unsloth_llama
+
+ def stock(self, *args, **kwargs):
+ return "stock"
+
+ cls = type("GatedAttention", (torch.nn.Module,), {"forward": fast})
+ cls.forward = unsloth_llama._gated_method(cls, "forward", fast, stock)
+ assert getattr(cls.forward, "_unsloth_gated", False)
+ return cls
+
+
+class _Layer(torch.nn.Module):
+ def __init__(self, attn):
+ super().__init__()
+ self.self_attn = attn
+
+
+class _Stack(torch.nn.Module):
+ def __init__(self, attn_cls, marked):
+ super().__init__()
+ self.layers = torch.nn.ModuleList([_Layer(attn_cls()) for _ in range(2)])
+ if marked:
+ from unsloth.models.llama import mark_unsloth_fast_path
+ mark_unsloth_fast_path(self)
+
+
+def test_context_parallel_accepts_gated_fast_attention():
+ from unsloth import context_parallel as cp
+ from unsloth.models.llama import LlamaAttention_fast_forward
+
+ cls = _gated_class(LlamaAttention_fast_forward)
+ assert cp._supports_context_parallel(_Stack(cls, marked = True), "ulysses")
+ assert cp._supports_context_parallel(_Stack(cls, marked = True), "ring")
+ assert not cp._supports_context_parallel(_Stack(cls, marked = False), "ulysses")
+
+
+def test_context_parallel_still_refuses_other_attention():
+ from unsloth import context_parallel as cp
+
+ def other(self, *args, **kwargs):
+ return "other"
+
+ cls = _gated_class(other)
+ assert not cp._supports_context_parallel(_Stack(cls, marked = True), "ulysses")
+ plain = type("PlainAttention", (torch.nn.Module,), {"forward": other})
+ assert not cp._supports_context_parallel(_Stack(plain, marked = True), "ulysses")
+
+
+def test_ungated_fast_attention_still_accepted():
+ from unsloth import context_parallel as cp
+ from unsloth.models.llama import LlamaAttention_fast_forward
+
+ cls = type("FastAttention", (torch.nn.Module,), {"forward": LlamaAttention_fast_forward})
+ assert cp._supports_context_parallel(_Stack(cls, marked = False), "ulysses")
+
+
+def test_async_grpo_sees_gated_fast_backbone():
+ from unsloth.models import rl
+ from unsloth.models.llama import LlamaModel_fast_forward, mark_unsloth_fast_path
+
+ cls = _gated_class(LlamaModel_fast_forward)
+ backbone = cls()
+ assert not rl._is_unsloth_fast_backbone(backbone)
+ mark_unsloth_fast_path(backbone)
+ assert rl._is_unsloth_fast_backbone(backbone)
+ assert not rl._is_unsloth_fast_backbone(None)
+ direct = type("FastBackbone", (torch.nn.Module,), {"forward": LlamaModel_fast_forward})()
+ assert rl._is_unsloth_fast_backbone(direct)
diff --git a/unsloth/context_parallel.py b/unsloth/context_parallel.py
index 43af45df0b0..7a62a58bcb5 100644
--- a/unsloth/context_parallel.py
+++ b/unsloth/context_parallel.py
@@ -209,11 +209,10 @@ def _hybrid_config_blocker(model) -> Optional[str]:
def _supports_context_parallel(model, backend = "ring") -> bool:
- from .models.llama import LlamaAttention_fast_forward
+ from .models.llama import LlamaAttention_fast_forward, runs_fast_forward
modules = [module for name, module in model.named_modules() if name.endswith("self_attn")]
- forwards = [getattr(type(m).forward, "__func__", type(m).forward) for m in modules]
- if bool(forwards) and all(f is LlamaAttention_fast_forward for f in forwards):
+ if bool(modules) and all(runs_fast_forward(m, LlamaAttention_fast_forward) for m in modules):
return not _stateful_mixers(model)
mixers = _stateful_mixers(model)
return (
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 030343fb856..45a3b498c27 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -3310,6 +3310,22 @@ def _gated_method(cls, name, fast, original):
return gated
+def runs_fast_forward(
+ module,
+ fast,
+ name = "forward",
+):
+ """True when `module` runs Unsloth's `fast` as its class `name`: directly, or through the gate that
+ pre_patch installs (which runs `fast` only on modules marked by mark_unsloth_fast_path)."""
+ method = getattr(type(module), name, None)
+ method = getattr(method, "__func__", method)
+ if method is fast:
+ return True
+ if getattr(method, "_unsloth_gated", False) and getattr(method, "__wrapped__", None) is fast:
+ return bool(getattr(module, "_unsloth_fast_path", False))
+ return False
+
+
def mark_unsloth_fast_path(model):
"""Opt every module of an Unsloth-loaded model into the patched forwards."""
for module in model.modules():
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index 658bd0e65e0..6a914b7f89b 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -4859,8 +4859,8 @@ def _without_instance_bound_methods(obj):
def _is_unsloth_fast_backbone(backbone):
- from .llama import LlamaModel_fast_forward
- return getattr(type(backbone), "forward", None) is LlamaModel_fast_forward
+ from .llama import LlamaModel_fast_forward, runs_fast_forward
+ return backbone is not None and runs_fast_forward(backbone, LlamaModel_fast_forward)
def _text_prepended_position_ids(position_ids):