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: Ulysses evidence for gpt-oss (attention sinks, MoE, aux off) (#17183)

Daniel Han400288df0af3
diff --git a/tests/test_parallel_capabilities_evidence.py b/tests/test_parallel_capabilities_evidence.py
index e67410cb146..f39d2e93e93 100644
--- a/tests/test_parallel_capabilities_evidence.py
+++ b/tests/test_parallel_capabilities_evidence.py
@@ -463,6 +463,64 @@ def test_gemma4_evidence_ids_are_real_parity_cases():
assert named >= 3
+ "mode, expert_lora",
+ [("full", False), ("lora", False), ("lora", True)],
+ ids = ["full", "lora", "lora+experts"],
+)
+def test_gpt_oss_moe_under_cp_follows_its_gloo_evidence(mode, expert_lora):
+ kw = dict(
+ trainer = "sft",
+ family = "gpt_oss",
+ moe = True,
+ windowed = True,
+ direct_params = True,
+ mode = mode,
+ expert_lora = expert_lora,
+ cp = 2,
+ cp_backend = "ulysses",
+ torch = (2, 13),
+ attention = "eager",
+ )
+ for extra in ({}, {"memory": "gc"}, {"dp": 2}, {"cp": 4}):
+ query = caps.Query(**{**kw, **extra})
+ status, cell = caps.cell_status(query)
+ assert status == "supported" and "test_gpt_oss_cp_matches_fp64_reference" in cell.parity, (
+ extra,
+ cell,
+ )
+ caps.check(query)
+ for extra, match in (
+ ({"memory": "unsloth_offload"}, "Unsloth checkpoint"),
+ ({"fsdp2": True}, "fsdp2_cp"),
+ ({"router_aux_loss": True}, "router auxiliary loss"),
+ ({"compile": "block"}, "no numeric parity"),
+ ({"attention": "sdpa"}, "no numeric parity"),
+ ({"mode": "qlora"}, "4-bit experts"),
+ ({"cp": 1, "tp": 2}, "sinks"),
+ ):
+ with pytest.raises(NotImplementedError, match = match):
+ caps.check(caps.Query(**{**kw, **extra}))
+
+
+def test_gpt_oss_evidence_ids_are_real_parity_cases():
+ import re
+
+ parity = pytest.importorskip("tests.test_ulysses_gpt_oss")
+ named = 0
+ for cell in caps.CELLS:
+ if cell.parity and "test_gpt_oss_cp_matches_fp64_reference[" in cell.parity:
+ ids = re.search(r"\[(.*)\]", cell.parity).group(1).split("|")
+ assert set(ids) <= set(parity.CASES), (cell.name, ids)
+ lora = {parity.CASES[i].get("lora") for i in ids}
+ assert len(lora) == 1, (cell.name, ids)
+ assert {"lora": "dense", "lora+experts": "experts"}.get(
+ cell.name.split(" x ")[1]
+ ) in lora
+ named += 1
+ assert named == 3
+
+
def test_unsloth_checkpoint_keeps_an_unverified_moe_row_refused_under_strict(
clean_env, monkeypatch
):
diff --git a/tests/test_ulysses_gpt_oss.py b/tests/test_ulysses_gpt_oss.py
new file mode 100644
index 00000000000..ecb68b6ff7d
--- /dev/null
+++ b/tests/test_ulysses_gpt_oss.py
@@ -0,0 +1,375 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Ulysses CP for gpt-oss on CPU gloo: fp64 parity vs one process, plus controls that must fail."""
+
+import copy
+import inspect
+import os
+import tempfile
+import types
+
+import pytest
+import torch
+import torch.distributed as dist
+import torch.multiprocessing as mp
+import torch.nn.functional as F
+
+import unsloth # noqa: F401
+from unsloth import context_parallel as cp
+from unsloth.utils import ulysses_hybrid as uh
+
+transformers = pytest.importorskip("transformers")
+gpt_oss = pytest.importorskip("transformers.models.gpt_oss.modeling_gpt_oss")
+LAYER_TYPES = ["sliding_attention", "full_attention", "sliding_attention", "full_attention"]
+SEQ = 13
+
+
+def _config(window = 4, **overrides):
+ common = dict(
+ vocab_size = 64,
+ hidden_size = 32,
+ intermediate_size = 16,
+ num_hidden_layers = 4,
+ num_attention_heads = 4,
+ num_key_value_heads = 2,
+ head_dim = 8,
+ num_local_experts = 4,
+ num_experts_per_tok = 2,
+ sliding_window = window,
+ layer_types = LAYER_TYPES,
+ router_aux_loss_coef = 0.0,
+ output_router_logits = False,
+ pad_token_id = 0,
+ )
+ return gpt_oss.GptOssConfig(**{**common, **overrides})
+
+
+def _model(
+ *,
+ window = 4,
+ dtype = torch.float64,
+ checkpoint = False,
+ lora = None,
+):
+ os.environ.setdefault("UNSLOTH_MOE_BACKEND", "native_torch")
+ torch.manual_seed(0)
+ model = gpt_oss.GptOssForCausalLM(_config(window))
+ with torch.no_grad():
+ g = torch.Generator().manual_seed(3)
+ for module in model.modules():
+ if isinstance(module, gpt_oss.GptOssAttention):
+ # The default init is N(0, 0.02): near-zero sinks would hide a wrong head slice.
+ module.sinks.copy_(1.5 * torch.randn(module.sinks.shape, generator = g))
+ model = model.to(dtype).train()
+ # gpt-oss has no SDPA path (sdpa ignores the sinks): eager is the attention it trains with.
+ model.config._attn_implementation = "eager"
+ model.config.use_cache = False
+ for module in model.modules():
+ if hasattr(module, "set_experts_implementation"):
+ module.set_experts_implementation("eager")
+ if checkpoint:
+ model.gradient_checkpointing_enable(gradient_checkpointing_kwargs = {"use_reentrant": False})
+ if lora:
+ from peft import LoraConfig, get_peft_model
+
+ extra = {}
+ if lora == "experts":
+ extra["target_parameters"] = ["experts.gate_up_proj", "experts.down_proj"]
+ targets = ["q_proj", "k_proj", "v_proj", "o_proj"]
+ model = get_peft_model(
+ model, LoraConfig(r = 4, lora_alpha = 8, target_modules = targets, **extra)
+ )
+ g = torch.Generator().manual_seed(5)
+ with torch.no_grad():
+ for name, p in model.named_parameters():
+ if "lora_B" in name: # nonzero B: the adapter changes the forward
+ p.copy_(0.05 * torch.randn(p.shape, generator = g, dtype = p.dtype))
+ return model
+
+
+def _manager(size, group):
+ manager = object.__new__(cp.ContextParallelManager)
+ manager.size = size
+ manager.backend = "ulysses"
+ local = dist.get_rank(group)
+ manager.mesh = types.SimpleNamespace(get_group = lambda: group, get_local_rank = lambda: local)
+ return manager
+
+
+def _loss(logits, shift_labels, count):
+ return (
+ F.cross_entropy(
+ logits.to(torch.promote_types(logits.dtype, torch.float32)).flatten(0, 1),
+ shift_labels.flatten(),
+ ignore_index = -100,
+ reduction = "sum",
+ )
+ / count
+ )
+
+
+def _batch(labels_scheme):
+ ids = torch.randint(1, 64, (2, SEQ), generator = torch.Generator().manual_seed(1))
+ mask = torch.ones_like(ids)
+ mask[1, 6:] = 0 # row 1: 6 valid tokens, right padded (unequal lengths, odd total)
+ labels = ids.masked_fill(mask == 0, -100)
+ if labels_scheme == "prompt_masked":
+ # Completion-only labels: the first shard(s) carry no labels at all.
+ labels[:, :9] = -100
+ return ids, mask, labels
+
+
+def _grad_status(grad):
+ return 0 if grad is None else (1 if not grad.any() else 2)
+
+
+def _spawn(fn, world, *args):
+ if "fork" not in mp.get_all_start_methods():
+ pytest.skip("needs the fork start method")
+ with tempfile.TemporaryDirectory() as d:
+ mp.start_processes(
+ _entry,
+ args = (world, os.path.join(d, "init"), fn, args),
+ nprocs = world,
+ join = True,
+ start_method = "fork",
+ )
+
+
+def _entry(rank, world, init_file, fn, args):
+ torch.set_num_threads(1)
+ dist.init_process_group("gloo", init_method = f"file://{init_file}", rank = rank, world_size = world)
+ try:
+ fn(rank, world, *args)
+ finally:
+ dist.destroy_process_group()
+
+
+def _cp_groups(world, size):
+ mine = None
+ for start in range(0, world, size):
+ ranks = list(range(start, start + size))
+ group = dist.new_group(ranks)
+ if dist.get_rank() in ranks:
+ mine = group
+ return mine
+
+
+class _FirstHeadsSinks(uh._HeadShard):
+ """Control: every rank gets the first heads' sinks (rank 0's), as a slice that ignores the rank would."""
+
+ def __init__(self, module, **overrides):
+ if "sinks" in overrides:
+ overrides["sinks"] = module.sinks[: overrides["sinks"].shape[0]]
+ super().__init__(module, **overrides)
+
+
+class _UnslicedSinks(uh._HeadShard):
+ """Control: the shard keeps all heads' sinks (no slicing at all)."""
+
+ def __init__(self, module, **overrides):
+ overrides.pop("sinks", None)
+ super().__init__(module, **overrides)
+
+
+def _install_zoo_attention_forward():
+ """Install zoo's GptOssAttention forward (worker only); under Ulysses the stashed HF forward must run.
+ On CPU zoo installs nothing, so a must-not-run forward goes in via the same patch_function_past_key_values."""
+ from unsloth_zoo.temporary_patches import gpt_oss as zoo_gpt_oss
+ from unsloth_zoo.temporary_patches.utils import patch_function_past_key_values
+
+ os.environ["UNSLOTH_MODEL_NAME"] = "unsloth/gpt-oss-20b"
+ hf_forward = gpt_oss.GptOssAttention.forward
+ zoo_gpt_oss.patch_GptOssAttention()
+ if gpt_oss.GptOssAttention.forward is hf_forward:
+
+ def forward(
+ self,
+ hidden_states,
+ position_embeddings,
+ attention_mask,
+ past_key_values = None,
+ **kwargs,
+ ):
+ raise AssertionError("the zoo forward ran under Ulysses")
+
+ forward.__signature__ = inspect.signature(hf_forward) # zoo's strict signature match
+ assert patch_function_past_key_values(gpt_oss.GptOssAttention, "forward", [forward])
+ assert gpt_oss.GptOssAttention.forward is not hf_forward
+ assert (
+ vars(gpt_oss.GptOssAttention)["_original_modeling_gpt_oss_GptOssAttention_forward"]
+ is hf_forward
+ )
+ assert not uh._calls_attention_registry(gpt_oss.GptOssAttention.forward)
+
+
+def _model_case(
+ rank,
+ world,
+ case,
+ control = None,
+):
+ size = case["cp"]
+ dp = world // size
+ group = _cp_groups(world, size)
+ window = case.get("window", 4)
+ model = _model(window = window, checkpoint = case.get("checkpoint", False), lora = case.get("lora"))
+ reference = copy.deepcopy(model)
+ ids, mask, labels = _batch(case.get("labels", "pad"))
+ shift = F.pad(labels, (0, 1), value = -100)[:, 1:]
+ count = shift.ne(-100).sum()
+ _loss(
+ reference(input_ids = ids, attention_mask = mask, use_cache = False).logits, shift, count
+ ).backward()
+
+ if case.get("zoo_forward"):
+ _install_zoo_attention_forward()
+ rows = list(range(rank // size, ids.shape[0], dp))
+ manager = _manager(size, group)
+ cp._check_cp_model(model, size, "ulysses")
+ real_route = uh._route_hf_forward
+ if control == "zoo_unrouted":
+ uh._route_hf_forward = lambda module, get_cp_manager: None
+ try:
+ manager.attach_attention_hooks(model)
+ finally:
+ uh._route_hf_forward = real_route
+ attention = [m for n, m in model.named_modules() if n.endswith("self_attn")]
+ assert len(attention) == 4 and all(isinstance(m, gpt_oss.GptOssAttention) for m in attention)
+ if control == "shard_local":
+ for module in attention:
+ module._unsloth_cp_ulysses_attention = False
+ elif control == "dropped_window":
+ for module in attention:
+ module.sliding_window = None
+ calls = []
+ real = uh._ulysses_hf_attention
+ real_shard = uh._HeadShard
+
+ def counted(*args, **kwargs):
+ calls.append((args[1].shape[1], args[2].shape[1], args[1].shape[-1]))
+ return real(*args, **kwargs)
+
+ uh._ulysses_hf_attention = counted
+ if control == "first_heads_sinks":
+ uh._HeadShard = _FirstHeadsSinks
+ elif control == "unsliced_sinks":
+ uh._HeadShard = _UnslicedSinks
+ try:
+ inputs = {
+ "input_ids": ids[rows].clone(),
+ "attention_mask": mask[rows].clone(),
+ "labels": labels[rows].clone(),
+ }
+ with manager.apply(inputs):
+ out = model(
+ input_ids = inputs["input_ids"],
+ attention_mask = inputs["attention_mask"],
+ position_ids = inputs["position_ids"],
+ use_cache = False,
+ )
+ _loss(out.logits, inputs["shift_labels"], count).backward()
+ finally:
+ uh._ulysses_hf_attention = real
+ uh._HeadShard = real_shard
+ text = _config()
+ if control is None:
+ runs = 2 if case.get("checkpoint") else 1
+ assert len(calls) == runs * text.num_hidden_layers, (len(calls), runs)
+ assert set(calls) == {
+ (text.num_attention_heads, text.num_key_value_heads, text.head_dim)
+ }, calls
+ elif control == "shard_local":
+ assert not calls, calls
+
+ ref_params = dict(reference.named_parameters())
+ sinks_checked = 0
+ for name, param in model.named_parameters():
+ statuses = [None] * world
+ dist.all_gather_object(statuses, _grad_status(param.grad))
+ expected = ref_params[name].grad
+ if expected is None:
+ assert statuses == [0] * world, (name, statuses)
+ continue
+ # Every rank holds a gradient (a rank without labels too), or the reduction desyncs.
+ assert 0 not in statuses, (name, statuses)
+ grad = param.grad
+ dist.all_reduce(grad)
+ torch.testing.assert_close(
+ grad, expected, rtol = 1e-5, atol = 1e-6, msg = lambda m: f"{name}: {m}"
+ )
+ assert _grad_status(grad) == _grad_status(expected), name
+ sinks_checked += name.endswith("sinks")
+ if case.get("lora") is None:
+ assert sinks_checked == text.num_hidden_layers, sinks_checked
+ # Large eps: GptOssRMSNorm runs in float32 even under float64, so gradients carry float32 rounding.
+ for net in (reference, model):
+ trainable = [p for p in net.parameters() if p.requires_grad]
+ torch.optim.AdamW(trainable, lr = 1e-3, eps = 1e-3, weight_decay = 0.1).step()
+ for (name, mine), theirs in zip(model.named_parameters(), reference.parameters()):
+ torch.testing.assert_close(
+ mine, theirs, rtol = 1e-5, atol = 1e-6, msg = lambda m: f"after AdamW {name}: {m}"
+ )
+
+
+def _case_worker(rank, world, case):
+ _model_case(rank, world, case)
+
+
+CASES = {
+ "cp2": dict(cp = 2, world = 2, window = 3),
+ "cp2_window9_prompt_masked": dict(cp = 2, world = 2, window = 9, labels = "prompt_masked"),
+ "cp4": dict(cp = 4, world = 4, window = 3),
+ "dp2cp2": dict(cp = 2, world = 4, window = 5, labels = "prompt_masked"),
+ "cp2_checkpoint": dict(cp = 2, world = 2, window = 3, checkpoint = True),
+ "cp2_lora": dict(cp = 2, world = 2, window = 3, lora = "dense"),
+ "cp4_lora": dict(cp = 4, world = 4, window = 5, lora = "dense"),
+ "cp2_lora_experts": dict(cp = 2, world = 2, window = 3, lora = "experts"),
+ "cp2_lora_experts_checkpoint": dict(cp = 2, world = 2, window = 3, lora = "experts", checkpoint = True),
+ "cp4_lora_experts": dict(cp = 4, world = 4, window = 5, lora = "experts"),
+ "cp2_zoo_forward": dict(cp = 2, world = 2, window = 3, zoo_forward = True),
+ "cp2_lora_experts_zoo_forward": dict(cp = 2, world = 2, window = 3, lora = "experts", zoo_forward = True),
+}
+
+
[email protected]("name", list(CASES))
+def test_gpt_oss_cp_matches_fp64_reference(name):
+ case = CASES[name]
+ _spawn(_case_worker, case["world"], case)
+
+
+# Expected failure per control (unsliced sinks: shape error).
+CONTROLS = {
+ "shard_local": AssertionError,
+ "dropped_window": AssertionError,
+ "first_heads_sinks": AssertionError,
+ "unsliced_sinks": RuntimeError,
+ "zoo_unrouted": AssertionError,
+}
+
+
+def _controls_worker(rank, world):
+ wrong = []
+ for control, expected in CONTROLS.items():
+ try:
+ case = dict(cp = 2, window = 3, zoo_forward = control.startswith("zoo"))
+ _model_case(rank, world, case, control = control)
+ wrong.append((control, "matched the reference"))
+ except Exception as error:
+ if type(error) is not expected:
+ wrong.append((control, repr(error)))
+ assert not wrong, f"negative controls that did not fail as expected: {wrong}"
+
+
+def test_gpt_oss_negative_controls_fail():
+ _spawn(_controls_worker, 2)
+
+
+def test_gpt_oss_attention_is_generic_hf_route():
+ model = _model(dtype = torch.float32)
+ assert cp._generic_hf_attention(model)
+ assert cp._supports_context_parallel(model, "ulysses")
+ cp._check_cp_model(model, 2, "ulysses")
+ cp._check_cp_model(model, 4, "ulysses")
+ assert cp.resolve_cp_backend("auto", 2, model) == "ulysses"
diff --git a/unsloth/parallel_capabilities.py b/unsloth/parallel_capabilities.py
index 6dc5b0c4bc1..51caafc48cd 100644
--- a/unsloth/parallel_capabilities.py
+++ b/unsloth/parallel_capabilities.py
@@ -921,8 +921,16 @@ _GEMMA4_CASES = {
"lora": "moe_cp2_lora",
"lora+experts": "moe_cp2_lora_experts|moe_cp2_lora_experts_eager_checkpoint",
}
+# gpt-oss: eager only (no SDPA path honours the sinks); HF checkpointing only.
+_GPT_OSS = "tests/test_ulysses_gpt_oss.py::test_gpt_oss_cp_matches_fp64_reference"
+_GPT_OSS_EAGER = "eager: block compile not measured for this family"
+_GPT_OSS_CASES = {
+ "full": "cp2|cp2_window9_prompt_masked|cp4|dp2cp2|cp2_checkpoint|cp2_zoo_forward",
+ "lora": "cp2_lora|cp4_lora",
+ "lora+experts": "cp2_lora_experts|cp2_lora_experts_checkpoint|cp4_lora_experts|cp2_lora_experts_zoo_forward",
+}
# Attention implementations a family's evidence ran (default: _MOE_COMMON's sdpa and eager).
-_MOE_ATTENTION = {"deepseek_v4": ("eager",)}
+_MOE_ATTENTION = {"deepseek_v4": ("eager",), "gpt_oss": ("eager",)}
_MOE_TEXT_ONLY = {
"gemma4": "multimodal Gemma 4 refuses under Ulysses (its vision attention is bidirectional); only the "
"text-only Gemma4ForCausalLM ran",
@@ -1006,6 +1014,10 @@ _MOE_EVIDENCE = {
("gemma4", precision, False, "cp"): (f"{_GEMMA4}[{ids}]",) * 2 + (_GEMMA4_EAGER,)
for precision, ids in _GEMMA4_CASES.items()
},
+ **{
+ ("gpt_oss", precision, False, "cp"): (f"{_GPT_OSS}[{ids}]",) * 2 + (_GPT_OSS_EAGER,)
+ for precision, ids in _GPT_OSS_CASES.items()
+ },
}