Repository navigation
Build pairwise attention biases per length bucket on the packed path - #915
Open
kmontemayor2-sc wants to merge 7 commits into
Open
kmontemayor2-sc wants to merge 7 commits into
kmontemayor2-sc wants to merge 7 commits into
Conversation
Pairwise attention biases (and the learned non-missing bias) now run on packed tokens. _packed_pairwise_bias looks up the pairwise features on each length bucket's [sequences, bucket_len] node grid, projects them, and concatenates the head-major blocks into the ragged per_pair bias; the non-missing bias is added out of place on the flat block. Layers that query all tokens run SDPA per bucket; the last anchor_only layer folds row 0 into a per-key bias on the varlen kernel. lookup_pairwise_relative_features becomes public. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
yliu2-sc
reviewed
Nov 24, 2026
| .flatten(), | ||
| ) | ||
| blocks.append(block) | ||
| per_pair = torch.cat(blocks) |
Collaborator
There was a problem hiding this comment.
Could we return blocks[0] for a single bucket? torch.cat([block]) copies the full bias
kmontemayor2-sc
marked this pull request as ready for review
November 25, 2026 00:14
kmontemayor2-sc
requested review from
mkolodner-sc,
svij-sc,
xgao4-sc and
zfan3-sc
as code owners
November 25, 2026 00:14
torch.cat over one block copies the full per-pair bias for no benefit. Every block is already flat, so use it directly when there is only one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mkolodner-sc
approved these changes
Nov 25, 2026
mkolodner-sc
left a comment
Collaborator
There was a problem hiding this comment.
Conditioned on @yliu2-sc 's approval
This branch has not been deployed
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
GraphTransformerEncoderconfigs withpairwise_attention_bias_attr_names(including the learned non-missing bias) nowrun on packed tokens on CUDA. The pairwise bias is built per length bucket as the ragged
per_pairbias from theprevious PR, so it never exists as a dense
[sequences, heads, max_seq_len, max_seq_len]grid.Approach
_packed_pairwise_biasbuilds the packed-order[sequences, max_seq_len]node grid once (_layout_grids, taken fromRun pairwise attention biases on packed tokens in GraphTransformerEncoder #856), then for each length bucket looks up the pairwise CSR features on the bucket's
[sequences, bucket_len]slice (
lookup_pairwise_relative_features, now public), projects them to one value per head, and lays them outhead-major. The non-missing bias is added with an out-of-place
index_addon the flat block (the old code wrote intoa permuted view of the dense grid, which is slow on CUDA).
PackedAttentionBias.per_pair. Layers that query every token runSDPA per bucket; the last
anchor_onlylayer gathers row 0 of each block into a per-key bias and runs the varlenkernel.
casts it.
User-facing changes
gigl.transforms.graph_transformer.lookup_pairwise_relative_featuresis public (it was_lookup_pairwise_relative_features).pairwise config does not whole-encoder compile and has no fixed host-sync count.
Verification
packed_graph_transformer_test.py139 ran / 20 skipped (CUDA-only);packed_attention_test.py37 / 13;graph_transformer_test.py(nn + transforms) 125 / 1;static_khop_test.py24; all OK.ruff,ty,dprintclean.
each change the output, missing attribute raises). New: parity with the padded path (outputs and training gradients,
both readouts, pairwise alone and with a per-key bias) on a batch that fills all four length buckets; each packed
sequence's block equals the padded path's dense bias for that anchor; bias dtype under bf16 autocast; CUDA parity and a
constant host-sync count across batches. Mutation checks (reversed bucket blocks, no non-missing bias, caller-order
node grid) each fail tests.
gt2-pr3-unit, commit dfb1a5bb): 325 ran, 0 skipped; all pairwise tests pass. One failure was theprevious PR's bf16 gradient check (see that PR), fixed by comparing relative L2 error; the re-run is in job
gt2-pr4-unit.Performance
One L4, torch 2.8.0+cu128, BAGL
phase_profile.py --parts 1 --random_init, production A3-MAU user-to-user model(3 layers, hidden 512, 8 heads,
max_seq_len256,anchor_only). Inference: 512 anchors per batch (45,170 tokens),fp32 with TF32, eager. Training: bf16 autocast, 128 query + 256 random anchors per step, eager. Old tip = stack tip
f93cdf2c; new = this PR (jobgt2-pr3-perf). L4 run-to-run noise is about 5%.Inference is slower than the old tip at this PR, as planned: the old tip ran a per-pair bias at inference through the
varlen kernel with a dense bias (#858), and here inference attention runs SDPA per bucket (35.4 ms of attention against
10.9 ms). The inference-routes PR brings the kernel back, once per bucket. The pairwise bias build is 20.9 ms against
24.5 ms at the old tip, and peak memory falls by 675 MiB (inference) and 1404 MiB (training).
🤖 Generated with Claude Code