Repository navigation
Add opt-in TP2 FP32 reduce-scatter before BF16 gathers #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Elevatormusic
merged 10 commits into
Elevatormusic:deepseek-v41-tp2
from
imD-5:perf/dsv41-reduce-scatter
Nov 6, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2d49e25
perf(dsv41): prompt-chunk partials over a reduce-scatter, the same bi…
imD-5 abd10cf
Exercise reducer contracts with CPU collectives without Triton
Elevatormusic 1b3b26a
Reproduce mismatched reducer flags in the startup agreement
Elevatormusic 3fe7f86
Use the DeepSeek engine class in the startup regression
Elevatormusic 23d3163
Agree the prefill reducer mode before either rank warms up
Elevatormusic 36de949
Merge remote-tracking branch 'refs/remotes/coverage49/current-base' i…
Elevatormusic 46493eb
Merge commit '92fc1f2b5aa8a14d0a532054eabee3de2b2c4674' into codex/en…
Elevatormusic 40c431a
Merge commit 'b3aa5031a733423b4e68239a6350ea3772bffc56' into codex/en…
Elevatormusic b3d4529
Merge commit '8559c41c5c91f1f7181503c45a13dd72bb5c83b5' into codex/en…
Elevatormusic e2eb15b
Keep local benchmark communicators on gather
Elevatormusic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """Comm.reduced (TF_DS_RS): two ranks get bf16(p0 + p1), the y the mHC post kernels form from the fp32 gather, and | ||
| the gather itself where the reduce-scatter does not apply.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
|
|
||
| import pytest | ||
|
|
||
| torch = pytest.importorskip("torch") | ||
| pytest.importorskip("triton") | ||
|
|
||
| from tensorfold.families.deepseek_v41.cuda import model as M | ||
|
|
||
|
|
||
| class Link: | ||
| """Two ranks' collectives on the host: each call waits for both ranks' buffers, as NCCL does.""" | ||
|
|
||
| def __init__(self): | ||
| self.barrier, self.sends, self.calls = threading.Barrier(2), [None, None], [] | ||
|
|
||
| def swap(self, rank, send): | ||
| self.sends[rank] = send.clone() | ||
| self.barrier.wait() | ||
| both = list(self.sends) | ||
| self.barrier.wait() | ||
| return both | ||
|
|
||
|
|
||
| class Rank: | ||
| def __init__(self, link, rank, rdma=None): | ||
| self.link, self.rank, self.world = link, rank, 2 | ||
| if rdma is not None: | ||
| self.rdma = rdma | ||
|
|
||
| def reduce_scatter(self, send, recv): | ||
| a, b = self.link.swap(self.rank, send) | ||
| self.link.calls.append(("reduce_scatter", self.rank)) | ||
| recv.copy_((a + b).view(2, -1)[self.rank]) # ncclSum of two operands | ||
|
|
||
| def all_gather(self, send, recv): | ||
| a, b = self.link.swap(self.rank, send) | ||
| self.link.calls.append(("all_gather", send.dtype)) | ||
| recv.copy_(torch.cat([a, b])) | ||
|
|
||
|
|
||
| class Fits: | ||
| def fits(self, x): | ||
| return True | ||
|
|
||
|
|
||
| def both_ranks(p0, p1, rdma=None): | ||
| """Comm.reduced on two threads, one a rank: (rank 0's result, rank 1's result, the calls).""" | ||
|
|
||
| link, out = Link(), [None, None] | ||
|
|
||
| def run(rank, mine): | ||
| c = M.Comm.__new__(M.Comm) | ||
| c.nccl, c.world, c._buf = Rank(link, rank, rdma), 2, {} | ||
| out[rank] = c.reduced(mine) | ||
|
|
||
| ts = [threading.Thread(target=run, args=(r, p)) for r, p in ((0, p0), (1, p1))] | ||
| for t in ts: | ||
| t.start() | ||
| for t in ts: | ||
| t.join() | ||
| return out[0], out[1], link.calls | ||
|
|
||
|
|
||
| def parts(n=8, d=64, seed=0): | ||
| g = torch.Generator().manual_seed(seed) | ||
| return torch.randn((n, d), generator=g) * 3, torch.randn((n, d), generator=g) * 1e-3 | ||
|
|
||
|
|
||
| def test_both_ranks_get_the_posts_y(monkeypatch): | ||
| monkeypatch.setattr(M, "RS", True) | ||
| p0, p1 = parts() | ||
| y0, y1, calls = both_ranks(p0, p1) | ||
| want = (p0 + p1).to(torch.bfloat16)[None] # what _hc_post forms from the gather, before the mix | ||
| for y in (y0, y1): | ||
| assert y.dtype == torch.bfloat16 and torch.equal(y.view(torch.int16), want.view(torch.int16)) | ||
| assert sorted(c[0] for c in calls) == ["all_gather", "all_gather", "reduce_scatter", "reduce_scatter"] | ||
| assert all(c[1] == torch.bfloat16 for c in calls if c[0] == "all_gather") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("case", ["off", "odd rows", "rdma holds it"]) | ||
| def test_falls_back_to_the_gather(monkeypatch, case): | ||
| monkeypatch.setattr(M, "RS", case != "off") | ||
| p0, p1 = parts(n=7 if case == "odd rows" else 8) | ||
| y0, y1, calls = both_ranks(p0, p1, Fits() if case == "rdma holds it" else None) | ||
| for y in (y0, y1): | ||
| assert y.dtype == torch.float32 and torch.equal(y, torch.stack([p0, p1])) | ||
| assert [c[0] for c in calls] == ["all_gather", "all_gather"] | ||
|
|
||
|
|
||
| def test_one_rank_is_the_gather(monkeypatch): | ||
| monkeypatch.setattr(M, "RS", True) | ||
| c = M.Comm.__new__(M.Comm) | ||
| c.nccl, c.world, c._buf = None, 1, {} | ||
| p0, _ = parts() | ||
| assert torch.equal(c.reduced(p0), p0[None]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.