Repository navigation
Add an all-gather K/V exchange to DistributedNeighborhoodAttentionS2 - #429
Merged
Merged
Conversation
…he ring exchange RingBackend named a communication pattern, while "ring" elsewhere mostly means a latitude ring of grid points, its own ring_weights included. What every backend of a longitude-split regular pair shares -- the arcs cut for this rank, the ring quadrature weights, and the call of the ring attention Function over a walk of K/V chunks -- moves to a private base, _AzimuthSplitBackend, with an _attend helper. RingExchangeBackend keeps the ring exchange and its user-visible name "ring". The double-backward refusal now speaks of the longitude-split backends, since an all-gather backend will run the same kernels. No change in behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…kward The azimuth all-gather whose backward is a reduce-scatter existed only as the private backward of reduce_from_scatter_to_azimuth_region. It is now public, as its polar counterpart is, for the all-gather backend of the neighborhood attention. Its backward reduce-scattered by the natural split, whatever sizes the forward gathered: with any other split the ranks disagreed on the buffer sizes and the collective hung. The shapes now travel to the backward, and reduce_from_scatter_to_azimuth_region takes optional shapes_ like the polar one. A second-order test covers the natural and a skewed split; the skewed one hangs with the old backward. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kv_exchange="all-gather" (on a pair of regular grids; "ring" stays the default) gathers the halo-padded K/V band over the azimuth group with gather_from_copy_to_azimuth_region and runs the ring kernels once, as a ring of one whose only chunk covers every longitude from 0: the arcs are in global longitudes and the kernels place a chunk by its first one, so that chunk is the union of the ring's. The gather's backward reduce-scatters the full-width dK/dV to their owners. One collective and one kernel launch per pass instead of a chain of hops, for the full-width band in memory and full-width fp32 gradients in the backward. AllGatherBackend sits next to RingExchangeBackend under the longitude-split base, selected by kv_exchange in available(). An unknown value, and "all-gather" on a ragged pair, are refused. Tests: all-gather rows in the comparison with the serial layer (both directions, QK norm, asymmetric channels, trainable scale, AMP), a direct comparison with the ring backend, backend state, the double-backward refusal, and the validation. The guide compares the two exchanges. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; the previous documentation issue is fixed. SummaryAdds
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
KV[Local keys and values] --> Halo[Latitude halo]
Halo --> Choice{kv_exchange}
Choice -->|ring| Ring[Rotate longitude chunks]
Choice -->|all-gather| Gather[Gather full longitude band]
Ring --> Attention[Run ring attention kernels]
Gather --> Attention
Attention --> Output[Local output]
Reviews (2) · Last reviewed commit: "Count the K/V collectives of both exchan..." · Reviewed by Greptile |
The all-gather backend gathers K and V separately, so it runs two all-gathers in the forward and two reduce-scatters in the backward, not one each. The ring row understated the backward as well: two K/V walks, one per backward pass, and q hops of the dK/dV sums. The guide's table, the layer and backend docstrings and the Changelog now say so, with kernel launches counted per direction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 tasks done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds an all-gather alternative to the azimuth ring exchange of
DistributedNeighborhoodAttentionS2, selected withkv_exchange="all-gather"on a pair of regular grids."ring"stays the default and is unchanged. It is the first of the two K/V exchanges planned for the distributed global attention too, where the same choice will exist over the spatial group.The PR has three commits, each reviewable on its own.
1. Restructure the backends (no change in behaviour).
RingBackendnamed a communication pattern, while "ring" elsewhere mostly means a latitude ring of grid points, its ownring_weightsincluded._AzimuthSplitBackend, with an_attendhelper: the arcs cut for this rank, the ring quadrature weights, and the call of the ring attention Function over a walk of K/V chunks.RingExchangeBackendkeeps the ring exchange and its user-visible name"ring".2.
gather_from_copy_to_azimuth_region. The azimuth all-gather whose backward is a reduce-scatter existed only as the private backward ofreduce_from_scatter_to_azimuth_region; it is now public, like its polar counterpart.reduce_from_scatter_to_azimuth_regiontakes optionalshapes_like the polar one.3.
AllGatherBackend. After the latitude halo, every rank all-gathers the K/V band along longitude. It then runs the existing ring kernels once, as a ring of one whose only chunk covers every longitude from 0. The arcs are in global longitudes and the kernels place a chunk by its first longitude, so that chunk is the union of the ring's chunks; no kernel or arc changes. The same single-chunk path already runs today on grids without an azimuth split. The gather's backward reduce-scatters the full-width dK/dV to their owners."ring"(default)"all-gather"q - 1hops each way, overlapped, then a ring reduce-scatterqBoth agree up to the order of summation. The distributed guide gets a short section comparing them. An unknown
kv_exchange, and"all-gather"on a ragged (HEALPix) pair, which is not split along longitude, are refused. Double backward stays refused for both backends; the message now names the longitude-split backends.No issue: maintainer-led, part of the distributed attention work (#426, #428).
Testing
New tests:
test_distributed_primitives.py:test_gather_from_copy_to_azimuth_region, first- and second-order gradients against a serial loss, fp64/fp16/bf16, with the natural and a skewed split. Run against the old backward, the skewed case hangs; with the fix it passes.test_distributed_attention.py:pscale_out2 and 3, longitude-only upsampling, a mixed grid, QK norm, asymmetric channels, a trainable scale, fp16/bf16 autocast;test_all_gather_matches_ring: outputs and input/parameter gradients of the two backends directly against each other;kv_exchangevalidation.Results:
test_distributed_attention.pyat 1x1: 128 passed, 6 skipped (22 new).test_distributed_primitives.pyon 1x1, 2x1, 1x2, 1x4, 2x2, 3x2, andtest_distributed_attention.pyon 2x1, 1x2, 2x2, all pass. The regular-grid attention cases need the CUDA ring kernels, so on CPU they reach the refusal only.-W --keep-going: succeeds.To come: NCCL multi-GPU runs of
test_distributed_attention.pyandtest_distributed_primitives.pyon GB200 / H100, in particular on the grids with an azimuth split.AI assistance
Claude Code (Claude Opus 5.5) wrote the change, the tests and this description, reviewed by the author.
Checklist
Changelog.mdis updated for user-visible changes.See CONTRIBUTING.md for the contribution policy.
🤖 Generated with Claude Code