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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Context parallel RL: single-tensor all-gather for hidden states and reduce-scatter in backward (#15132)
Daniel Hane2ba4af64de9
diff --git a/tests/test_context_parallel_rl.py b/tests/test_context_parallel_rl.pyindex e7c83179075..d830c425893 100644--- a/tests/test_context_parallel_rl.py+++ b/tests/test_context_parallel_rl.py@@ -716,3 +716,107 @@ def test_grpo_dapo_token_count_follows_the_adopted_completions():for p in procs:p.join(30)assert results == {0: "ok", 1: "ok"}, results+++def _gather_both_paths(x, weight, group, rank, size):+ results = {}+ for fused in (False, True):+ calls = []+ original = cp._fused_gather_ok+ cp._fused_gather_ok = lambda group, fused = fused: fused+ rs, ag = cp._reduce_scatter, cp._all_gather_into++ def _count(name, fn):+ def wrapper(*args, **kwargs):+ calls.append(name)+ return fn(*args, **kwargs)++ return wrapper++ cp._reduce_scatter = _count("reduce_scatter", rs)+ cp._all_gather_into = _count("all_gather_into_tensor", ag)+ try:+ leaf = x.clone().requires_grad_(True)+ full = cp._GatherSequence.apply(leaf, group, rank, size)+ # Rank-dependent upstream grad: the reduction must really sum the peers.+ (full.float() * (weight + rank)).sum().backward()+ finally:+ cp._fused_gather_ok, cp._reduce_scatter = original, rs+ cp._all_gather_into = ag+ results[fused] = (full.detach(), leaf.grad, calls)+ return results+++def _gather_worker(rank, world, init_file, queue):+ dist.init_process_group("gloo", init_method = f"file://{init_file}", rank = rank, world_size = world)+ try:+ group = dist.group.WORLD+ assert cp._fused_gather_ok(group)+ for dtype in (torch.float32, torch.float16, torch.bfloat16):+ for shape in ((1, 3, 4), (3, 2, 5), (2, 4, 2, 3)):+ torch.manual_seed(100 + rank)+ x = torch.randn(shape).to(dtype)+ full_shape = (shape[0], shape[1] * world, *shape[2:])+ torch.manual_seed(7)+ weight = torch.randn(full_shape)+ got = _gather_both_paths(x, weight, group, rank, world)+ (old, old_grad, old_calls), (new, new_grad, new_calls) = got[False], got[True]+ assert old_calls == [] and new_calls == ["all_gather_into_tensor", "reduce_scatter"]+ assert new.shape == full_shape and new.dtype == dtype and new.is_contiguous()+ assert new._base is None+ assert torch.equal(old, new), (dtype, shape)+ assert new_grad.shape == shape and new_grad.dtype == dtype+ assert torch.equal(old_grad, new_grad), (dtype, shape)+ # And against a closed form: this rank's slice of sum_r (weight + r).+ local = shape[1]+ expect = sum((weight + r).to(dtype) for r in range(world)).narrow(+ 1, rank * local, local+ )+ torch.testing.assert_close(new_grad, expect)+ # A backend without the single-tensor collectives (MPI) keeps the list path.+ dist_get_backend = dist.get_backend+ try:+ dist.get_backend = lambda group = None: "mpi"+ assert not cp._fused_gather_ok(group)+ dist.get_backend = lambda group = None: "cpu:gloo,cuda:nccl"+ assert cp._fused_gather_ok(group)+ finally:+ dist.get_backend = dist_get_backend+ x = torch.randn(2, 3, 4)+ leaf = x.clone().requires_grad_(True)+ calls = []+ ag = cp._all_gather_into+ cp._all_gather_into = lambda *a, **k: calls.append(1)+ original = cp._fused_gather_ok+ cp._fused_gather_ok = lambda group: False+ try:+ full = cp._GatherSequence.apply(leaf, group, rank, world)+ full.sum().backward()+ finally:+ cp._all_gather_into, cp._fused_gather_ok = ag, original+ assert calls == [] and full.shape == (2, 6, 4)+ assert torch.equal(leaf.grad, torch.full_like(x, float(world)))+ queue.put((rank, "ok"))+ except BaseException: # pragma: no cover - reported to the parent+ import traceback+ queue.put((rank, traceback.format_exc()))+ finally:+ dist.destroy_process_group()+++def test_gather_sequence_fused_collectives_match_list_path_bitwise():+ if "fork" not in mp.get_all_start_methods():+ pytest.skip("needs fork")+ ctx = mp.get_context("fork")+ queue = ctx.Queue()+ with tempfile.TemporaryDirectory() as tmp:+ init_file = os.path.join(tmp, "init")+ procs = [+ ctx.Process(target = _gather_worker, args = (r, 2, init_file, queue)) for r in range(2)+ ]+ for p in procs:+ p.start()+ results = dict(queue.get(timeout = 120) for _ in procs)+ for p in procs:+ p.join(30)+ assert results == {0: "ok", 1: "ok"}, resultsdiff --git a/unsloth/context_parallel.py b/unsloth/context_parallel.pyindex c1da4d31727..dfcfb63c2a6 100644--- a/unsloth/context_parallel.py+++ b/unsloth/context_parallel.py@@ -997,24 +997,70 @@ def patch_sft_trainer(install_cp_methods: bool = False) -> None:install_methods()+# Backends with single-tensor collectives; others (MPI) keep the list path.+_FUSED_GATHER_BACKENDS = ("nccl", "gloo", "xccl", "hccl", "ucc")+++def _fused_gather_ok(group) -> bool:+ try:+ backend = str(dist.get_backend(group)).lower()+ except Exception:+ return False+ return "mpi" not in backend and any(name in backend for name in _FUSED_GATHER_BACKENDS)+++# Newer torch deprecates *_tensor for *_single (same signatures).+_reduce_scatter = getattr(dist, "reduce_scatter_single", None) or getattr(+ dist, "reduce_scatter_tensor", None+)+_all_gather_into = getattr(dist, "all_gather_single", None) or getattr(+ dist, "all_gather_into_tensor", None+)++# DPO / GRPO: the loss reads per-token logps of whole sequences (sums over completions, group masks),# so only the decoder is sharded; its last hidden states are gathered back to full length before lm_head.class _GatherSequence(torch.autograd.Function):@staticmethoddef forward(ctx, x, group, rank, size):ctx.group, ctx.rank, ctx.local = group, rank, x.shape[1]- x = x.contiguous()- parts = [torch.empty_like(x) for _ in range(size)]- dist.all_gather(parts, x, group = group)- return torch.cat(parts, dim = 1)+ # Peers hold equal shards: the manager pads the sequence to a multiple of the group size.+ ctx.fused = (+ _reduce_scatter is not None and _all_gather_into is not None and _fused_gather_ok(group)+ )+ if not ctx.fused:+ x = x.contiguous()+ parts = [torch.empty_like(x) for _ in range(size)]+ dist.all_gather(parts, x, group = group)+ return torch.cat(parts, dim = 1)+ batch, local, rest = x.shape[0], x.shape[1], x.shape[2:]+ # Gather concatenates on dim 0, so sequence goes first; fresh output like torch.cat.+ out = x.new_empty((batch, size * local, *rest))+ if batch == 1:+ _all_gather_into(+ out.view(size * local, *rest), x.reshape(local, *rest).contiguous(), group = group+ )+ return out+ buffer = x.new_empty((size * local, batch, *rest))+ _all_gather_into(buffer, x.movedim(1, 0).contiguous(), group = group)+ return out.copy_(buffer.movedim(0, 1))@staticmethoddef backward(ctx, grad):# Every peer computes the same loss on the gathered sequence: summing their gradients makes each# shard's parameter grads N x its share, and DDP's average over the group then yields one copy.- grad = grad.clone(memory_format = torch.contiguous_format)- dist.all_reduce(grad, group = ctx.group)- return grad.narrow(1, ctx.rank * ctx.local, ctx.local), None, None, None+ if not ctx.fused:+ grad = grad.clone(memory_format = torch.contiguous_format)+ dist.all_reduce(grad, group = ctx.group)+ return grad.narrow(1, ctx.rank * ctx.local, ctx.local), None, None, None+ # Only this rank's slice of the sum is needed: a reduce-scatter sends half an all-reduce's bytes.+ batch, rest = grad.shape[0], grad.shape[2:]+ source = grad.movedim(1, 0).clone(memory_format = torch.contiguous_format)+ shard = grad.new_empty((ctx.local, batch, *rest))+ _reduce_scatter(shard, source, group = ctx.group)+ if batch == 1:+ return shard.view(1, ctx.local, *rest), None, None, None+ return shard.movedim(0, 1).contiguous(), None, None, Nonedef gather_sequence(x, manager):