Repository navigation
Add DistributedAttentionS2 (all-gather), and split the attention modules - #431
Merged
Merged
Conversation
…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>
Contributor
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; no new actionable issue remains. SummaryAdds
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]
Reviews (3) · Last reviewed commit: "Rename test_distributed_attention.py to ..." · Reviewed by Greptile |
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>
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
DistributedAttentionS2, the distributed counterpart of the globalAttentionS2, 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:
spatial_group()along the point axis, stacked into a single collective.The output is split like the queries.
kv_exchange="all-gather"is the only exchange so far and the default. The ring (online softmax overring_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:_field_in/_field_out: the grids here, the shards in the subclass._exchange_kvhook, the identity, sits right after QK norm and before SDPA.New primitives:
gather_from_copy_to_spatial_regionandreduce_from_scatter_to_spatial_region, each other's backward, for chunks of any sizes in spatial-group rank order.DistributedAttentionS2carries the legacy-signature guard of the serial layer, which the repository-wide constructor check requires.2. Module split, no change in behaviour
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:k_channels/out_channels, QK norm, a trainable scale;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):
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.-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
Changelog.mdis updated for user-visible changes.See CONTRIBUTING.md for the contribution policy.
🤖 Generated with Claude Code