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: no graph break in compiled Ulysses attention on packed rows (#16734)

Daniel Han4accec06a963
diff --git a/tests/test_cp_ulysses_generic_gloo.py b/tests/test_cp_ulysses_generic_gloo.py
index ae816b0a6dc..053f8fa6ba7 100644
--- a/tests/test_cp_ulysses_generic_gloo.py
+++ b/tests/test_cp_ulysses_generic_gloo.py
@@ -257,6 +257,77 @@ def test_generic_ulysses_matches_one_process(kind, implementation, packed):
_spawn(_parity_worker, 2, kind, implementation, packed, _OUT)
+def _fullgraph_packed_worker(rank, world, kind, implementation, out_dir):
+ """fullgraph-compiled layers: a packed row with a padding tail traces with no graph break and matches one process."""
+ ad.HAS_FLASH_ATTENTION = False
+ ad.HAS_XFORMERS = False
+ model = _model(kind, implementation)
+ for layer in model.model.layers:
+ layer.compile(backend = "eager", fullgraph = True)
+ ids, labels = _batch(True)
+ ids, labels = ids[:, :15], labels[:, :15] # 15 tokens: one padding token to the cp multiple
+ lengths = [6, 7, 2]
+ count = _reference(model, ids, labels, lengths)
+ reference = {n: p.grad.clone() for n, p in model.named_parameters() if p.grad is not None}
+ model.zero_grad(set_to_none = True)
+ manager = _manager(world, rank)
+ manager.attach_attention_hooks(model)
+ inputs = {
+ "input_ids": ids.clone(),
+ "labels": labels.clone(),
+ "packed_seq_lengths": torch.tensor(lengths, dtype = torch.int32),
+ }
+ with manager.apply(inputs):
+ out = model(
+ input_ids = inputs["input_ids"],
+ position_ids = inputs["position_ids"],
+ packed_seq_lengths = inputs["packed_seq_lengths"],
+ use_cache = False,
+ )
+ _loss(out.logits, inputs["shift_labels"], count).backward()
+ for name, param in model.named_parameters():
+ if not param.requires_grad:
+ continue
+ grad = torch.zeros_like(param) if param.grad is None else param.grad.clone()
+ dist.all_reduce(grad)
+ want = reference.get(name, torch.zeros_like(param))
+ torch.testing.assert_close(grad, want, rtol = 1e-4, atol = 1e-6, msg = name)
+
+
+# Qwen3's decoder layer traces whole in one process (Gemma3's does not: its own forward breaks the graph).
[email protected]("kind, implementation", [("qwen3", "sdpa"), ("qwen3", "eager")])
+def test_compiled_packed_ulysses_has_no_graph_break(kind, implementation):
+ _config(kind)
+ try:
+ _spawn(_fullgraph_packed_worker, 2, kind, implementation, _OUT)
+ except mp.ProcessRaisedException as error:
+ # torch 2.10's dynamo fails building a guard on transformers' sdpa path in the one-process
+ # reference itself (AttributeError on transformers.quantizers.auto), before any CP code runs.
+ text = str(error)
+ if (
+ tuple(int(x) for x in torch.__version__.split(".")[:2]) < (2, 13)
+ and "InternalTorchDynamoError" in text
+ and "transformers.quantizers.auto" in text
+ ):
+ pytest.skip(f"torch {torch.__version__} dynamo guard bug in the one-process reference")
+ raise
+
+
+def test_whole_sequence_mask_keeps_the_padding_tail_apart():
+ # Token 16 (padding past cu_seqlens[-1]) is its own segment.
+ mask = uh._whole_sequence_mask(
+ 16,
+ torch.device("cpu"),
+ torch.float32,
+ None,
+ torch.tensor([0, 6, 13, 15], dtype = torch.int32),
+ )[0, 0]
+ allowed = mask == 0
+ assert allowed[15].nonzero().flatten().tolist() == [15]
+ assert not allowed[13:15, 15].any() and allowed[14, 13]
+ assert allowed[12].nonzero().flatten().tolist() == list(range(6, 13))
+
+
def _compiled_rows_worker(rank, world, kind, implementation, out_dir):
"""Per-layer compiled model, two same-shape packed rows with different documents: each must match one process."""
ad.HAS_FLASH_ATTENTION = False
diff --git a/unsloth/utils/ulysses_hybrid.py b/unsloth/utils/ulysses_hybrid.py
index fd256e2c6a2..25d4313cb04 100644
--- a/unsloth/utils/ulysses_hybrid.py
+++ b/unsloth/utils/ulysses_hybrid.py
@@ -309,7 +309,9 @@ class _HeadShard:
# Plain attribute access only: torch.compile cannot trace object.__getattribute__ / object.__setattr__.
def __init__(self, module, **overrides):
self._unsloth_module = module
- self.__dict__.update(overrides)
+ # One attribute at a time: torch < 2.13 cannot trace self.__dict__.update (graph break).
+ for name, value in overrides.items():
+ setattr(self, name, value)
def __getattr__(self, name):
# Only called for names the shard does not hold itself.
@@ -344,7 +346,8 @@ def _refuse_non_causal(module, is_causal = None) -> None:
def _whole_sequence_mask(total, device, dtype, window, cu_seqlens):
"""Additive [1, 1, T, T] mask over the gathered sequence: causal, the sliding window (key > query - window,
- as transformers' masks), and packed documents closed off from one another."""
+ as transformers' masks), and packed documents closed off from one another (padding past
+ cu_seqlens[-1] is one more segment)."""
idx = torch.arange(total, device = device)
allowed = idx[None, :] <= idx[:, None]
if window is not None:
@@ -405,9 +408,6 @@ def _ulysses_hf_attention(
else get_packed_info_from_kwargs({"packed_seq_lengths": packed_seq_lengths}, query.device)
)
cu_seqlens = None if seq_info is None else seq_info[1]
- if cu_seqlens is not None and int(cu_seqlens[-1]) < total:
- # Padding to the cp multiple sits after the last document: its own segment.
- cu_seqlens = torch.cat([cu_seqlens, cu_seqlens.new_tensor([total])])
overrides = {"num_key_value_groups": plan.local_groups}
sinks = getattr(module, "sinks", None)
@@ -423,6 +423,9 @@ def _ulysses_hf_attention(
# Flash takes the window and the softcap as arguments, and documents as cu_seqlens.
mask = None
if cu_seqlens is not None:
+ if int(cu_seqlens[-1]) < total:
+ # Padding to the cp multiple sits after the last document: its own segment.
+ cu_seqlens = torch.cat([cu_seqlens, cu_seqlens.new_tensor([total])])
cu = cu_seqlens.to(device = query.device, dtype = torch.int32)
longest = int((cu[1:] - cu[:-1]).max())
kwargs.update(
@@ -431,6 +434,7 @@ def _ulysses_hf_attention(
elif implementation == "sdpa" and cu_seqlens is None and window is None:
mask = None # sdpa's is_causal path
else:
+ # No data-dependent padding check here: it graph-broke compiled packed attention.
mask = _whole_sequence_mask(total, query.device, query.dtype, window, cu_seqlens)
out, _ = attention_fn(shard, query, key, value, mask, **kwargs)
# [B, T, H / N, D] -> [B, T / N, H, D]