Skip to content

Add DistributedAttentionS2 (all-gather), and split the attention modules - #431

Merged
azrael417 merged 5 commits into
mainfrom
tkurth/distributed-global-attention
Dec 3, 2026
Merged

azrael417 merged 5 commits into
mainfrom
tkurth/distributed-global-attention

Conversation

@azrael417

Copy link
Copy Markdown
Collaborator

Summary

Adds DistributedAttentionS2, the distributed counterpart of the global AttentionS2, and splits the attention modules into global and neighborhood ones behind facades. Two commits, each reviewable on its own.

1. DistributedAttentionS2 (all-gather)

The fields are split as for every distributed layer: polar x azimuth on regular grids, polar bands on HEALPix, either side or both, product grids too. The layer works as follows:

  1. Projections: the Q/K/V projections act point by point, so they run on every rank's own points.
  2. Gather: keys and values are all-gathered over spatial_group() along the point axis, stacked into a single collective.
  3. Attention: every rank attends with its own queries to all of them in one ordinary SDPA call, so the serial layer's fused kernels, cuDNN for the mask (Prefer cuDNN attention for the masked AttentionS2 SDPA call #425), dropout and autocast carry over.
  4. Backward: reduce-scatters dK/dV back to the owning ranks.

The output is split like the queries. kv_exchange="all-gather" is the only exchange so far and the default. The ring (online softmax over ring_chunks) follows, with "auto" then preferring it.

Mask: the shifted log-weight mask is assembled once, as a shared table keyed on the block layout, in the order the gathered keys arrive. That order is the spatial ranks in turn, each block in its field layout, mapped through spatial_rank_grid(). Key order does not matter for attention, only the consistency between keys and mask does. On an equal-area input grid (HEALPix) there is no mask, as in the serial layer.

Changes to AttentionS2, none of them visible:

  • Fields are checked and laid out against _field_in/_field_out: the grids here, the shards in the subclass.
  • The layout helpers accept regular shards.
  • An _exchange_kv hook, the identity, sits right after QK norm and before SDPA.

New primitives: gather_from_copy_to_spatial_region and reduce_from_scatter_to_spatial_region, each other's backward, for chunks of any sizes in spatial-group rank order.

DistributedAttentionS2 carries the legacy-signature guard of the serial layer, which the repository-wide constructor check requires.

2. Module split, no change in behaviour

attention/attention.py                 facade: AttentionS2, NeighborhoodAttentionS2
attention/global_attention.py          AttentionS2, its mask and SDPA dispatch
attention/neighborhood_attention.py    NeighborhoodAttentionS2              (git mv of attention.py)
attention/_attention_utils.py          + _resolve_scale, _drop_inert_key_bias (used by both)

distributed/distributed_attention.py                facade: DistributedAttentionS2, DistributedNeighborhoodAttentionS2
distributed/distributed_global_attention.py         DistributedAttentionS2
distributed/distributed_neighborhood_attention.py   ring Function, backends, layer   (git mv of distributed_attention.py)
  • Compatibility: the facades keep the established import paths, and models pickled with them, working.
  • Private names: imported from the defining modules.
  • Tests: the SDPA dispatch tests now patch global_attention, since patching the facade would not reach the code that uses the names.

No issue: maintainer-led, follows #426-#429.

Testing

New tests:

  • tests/test_distributed_global_attention.py, against the serial layer: output, input gradients and parameter gradients (summed over the spatial group). It runs on CPU too, since the layer is plain PyTorch. Cases:

    • self-attention, including the aliased single-input path;
    • downsampling and upsampling across quadratures;
    • regular->HEALPix (masked) and HEALPix->regular / HEALPix->HEALPix (unmasked);
    • product grids, distinct k_channels/out_channels, QK norm, a trainable scale;
    • bf16, and fp16 on CUDA.

    It also covers the mask order, dropout in eval, validation, and the HEALPix azimuth refusal.

    fp32 tolerance: 32 ULPs of max|ref|, documented in place. The layer sums over the keys in gathered rather than grid order. On one rank the result is exact (0 ULPs); measured up to 14.7 ULPs (dQ on 1x4) with 128-512 input points.

  • test_distributed_primitives.py: second-order tests of both spatial primitives, fp64/fp16/bf16, natural and skewed chunks.

Results (NGC PyTorch 26.06 container):

  • gloo, CPU:
    • global attention on 1x1, 2x1, 1x2, 2x2, 3x2, 1x4, 4x1: all pass;
    • full distributed suite on 2x1, 1x2, 2x2 after the restructure: all pass.
  • GB10, 1 GPU, 1x1: global attention 20 passed, 1 skipped; neighborhood attention 128 passed, 6 skipped.
  • Serial: test_attention.py test_attention_layout.py test_cache.py test_radial.py test_field_contract.py test_grid_assumptions.py, 2245 passed, 25 skipped.
  • Imports: the facades resolve to the same classes as the public names.
  • Docs build with -W --keep-going: succeeds.

To come: NCCL multi-GPU runs on GB200 / H100.

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 2 commits December 3, 2026 01:47
…her)

The projections run on every rank's own points; the keys and values are
then all-gathered over the spatial group, stacked into one collective, and
every rank attends with its own queries to all of them in one ordinary SDPA
call, so the serial layer's fused kernels, cuDNN for the mask, dropout and
autocast carry over; the backward reduce-scatters dK/dV. The log-weight mask
is assembled once, as a shared table, in the order the gathered keys arrive
(spatial ranks in turn, each block in its field layout). kv_exchange takes
"all-gather" only so far; "ring" follows.

AttentionS2 gains what the subclass needs and nothing visible: fields are
checked and laid out against _field_in/_field_out (the grids, or the
shards), the layout helpers accept regular shards, and an _exchange_kv hook,
the identity, sits before SDPA.

New primitives gather_from_copy_to_spatial_region and
reduce_from_scatter_to_spatial_region, each other's backward, for chunks of
any sizes. DistributedAttentionS2 is re-exported from distributed_attention.

Tests against the serial layer on every process grid, on CPU as well:
self-attention, cross-grid regular<->HEALPix (masked and unmasked), product
grids, distinct k/out channels, QK norm, a trainable scale, autocast; the
mask order, dropout in eval, validation; second-order tests of the
primitives with natural and skewed chunks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Serial: attention/attention.py becomes neighborhood_attention.py (git mv, so
history follows the bulk), AttentionS2 with its mask and SDPA dispatch moves
to global_attention.py, and the helpers both layers use (_resolve_scale,
_drop_inert_key_bias) to _attention_utils.py. Distributed: the same, with
distributed_attention.py becoming distributed_neighborhood_attention.py next
to distributed_global_attention.py.

attention.py and distributed_attention.py stay as facades re-exporting the
public classes, so the established import paths, and models pickled with
them, keep working. Private names, and the names tests patch, are imported
from the defining modules: the SDPA dispatch tests now patch
global_attention, where patching the facade would not reach the code.

No change in behaviour.

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; no new actionable issue remains.

Summary

Adds DistributedAttentionS2 with one all-gather for keys and values and a matching backward that returns their summed gradients to their owners.

  • Each spatial rank can attend to keys and values from the full input grid.
  • Global and neighborhood attention now live in separate modules.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Q[Local queries] --> P[Local projections]
  KV[Local keys and values] --> K[Local projections]
  K --> G[All-gather over spatial ranks]
  G --> A[Attention]
  P --> A
  M[Mask in gathered key order] --> A
  A --> O[Local output]
  A --> B[Backward sums and returns key/value gradients]
Loading

Reviews (3) · Last reviewed commit: "Rename test_distributed_attention.py to ..." · Reviewed by Greptile

Comment thread torch_harmonics/attention/attention.py Resolved
azrael417 and others added 3 commits December 3, 2026 02:29
The budget of 16 ULPs was calibrated on groups of at most 4 ranks. The
spatial primitives sum over every rank of the process grid, 8 on the 2x4
and 4x2 CI grids, where the float64 second-order gradient of
reduce_from_scatter_to_spatial_region exceeded 16 on the x86 runners (7.4
on aarch64; the BLAS sums the matmuls in another order). The rounding of a
reassociated sum grows with its number of terms, so the budget now grows in
proportion beyond 4 ranks; up to 4 it is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kv_point_shapes listed every input point only to count them, on every rank
and for every layer, also where there is no mask; the shards know their
npoints. Only the cached mask builder still lists points, for grids with a
mask.

The facades claimed that models pickled before the split keep loading;
they do not, since a pickle refers to the old locations of private helpers.
The docstrings and the Changelog now say so: imports are unchanged, and
state_dict checkpoints load as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_attention.py

It tests DistributedNeighborhoodAttentionS2, next to
test_distributed_global_attention.py, mirroring the module split.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@azrael417
azrael417 merged commit 8a03543 into main Dec 3, 2026
24 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