Skip to content

Add an all-gather K/V exchange to DistributedNeighborhoodAttentionS2 - #429

Merged
azrael417 merged 4 commits into
mainfrom
tkurth/neighborhood-allgather
Dec 3, 2026
Merged

azrael417 merged 4 commits into
mainfrom
tkurth/neighborhood-allgather

Conversation

@azrael417

Copy link
Copy Markdown
Collaborator

Summary

Adds an all-gather alternative to the azimuth ring exchange of DistributedNeighborhoodAttentionS2, selected with kv_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). 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 moves to a private base, _AzimuthSplitBackend, with an _attend helper: 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.
  • RingExchangeBackend keeps the ring exchange and its user-visible name "ring".
  • Code that actually uses ring communication or ring kernels keeps "ring" in its name.

2. gather_from_copy_to_azimuth_region. 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, like its polar counterpart.

  • Bug fixed along the way: its backward reduce-scattered by the natural split whatever sizes the forward gathered. With any other split the ranks disagreed on buffer sizes and the collective hung. No caller passed another split so far.
  • The shapes now travel to the backward, and reduce_from_scatter_to_azimuth_region takes optional shapes_ 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"
K/V held per rank own chunk + one in flight full-width band
backward dK/dV buffers (fp32) chunk-sized full-width
communication q - 1 hops each way, overlapped, then a ring reduce-scatter 1 all-gather, 1 reduce-scatter
kernel launches per pass q 1

Both 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:
    • 13 all-gather rows in the comparison with the serial layer: self-attention, downsampling, upsampling with pscale_out 2 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;
    • backend-state rows, the double-backward refusal for both backends, and kv_exchange validation.

Results:

  • GB10, 1 GPU, test_distributed_attention.py at 1x1: 128 passed, 6 skipped (22 new).
  • gloo, CPU: test_distributed_primitives.py on 1x1, 2x1, 1x2, 1x4, 2x2, 3x2, and test_distributed_attention.py on 2x1, 1x2, 2x2, all pass. The regular-grid attention cases need the CUDA ring kernels, so on CPU they reach the refusal only.
  • Local script, not in the PR: 2 and 4 ranks sharing the one GPU over gloo, so with a real azimuth split, run the all-gather backend against the serial layer on 1x2 and 1x4. Worst relative error 9.5e-5 over output, dQ, dK, dV, with TF32 enabled. The ring backend cannot run this way, since gloo has no point-to-point for CUDA tensors.
  • Docs build with -W --keep-going: succeeds.

To come: NCCL multi-GPU runs of test_distributed_attention.py and test_distributed_primitives.py on 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

  • A maintainer agreed to this change in the linked issue (not needed for small documentation or typo fixes).
  • I understand every change in this PR and can explain it in review.
  • Tests are added or updated for bug fixes and new behavior.
  • Changelog.md is updated for user-visible changes.

See CONTRIBUTING.md for the contribution policy.

🤖 Generated with Claude Code

azrael417 and others added 3 commits December 3, 2026 00:13
…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>
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; the previous documentation issue is fixed.

Summary

Adds kv_exchange="all-gather" to DistributedNeighborhoodAttentionS2, while keeping "ring" as the default.

  • Regular-grid attention can gather K/V across azimuth ranks in one pass.
  • Azimuth ranks can gather each other's longitude chunks.

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]
Loading

Reviews (2) · Last reviewed commit: "Count the K/V collectives of both exchan..." · Reviewed by Greptile

Comment thread docs/guide/distributed.md Resolved
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>
@azrael417
azrael417 merged commit e8afd1a into main Dec 3, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant