Skip to content

Build pairwise attention biases per length bucket on the packed path - #915

Open
kmontemayor2-sc wants to merge 7 commits into
kmonte/gt2-bucket-attentionfrom
kmonte/gt2-pairwise-bias
Open

kmontemayor2-sc wants to merge 7 commits into
kmonte/gt2-bucket-attentionfrom
kmonte/gt2-pairwise-bias

Conversation

@kmontemayor2-sc

Copy link
Copy Markdown
Collaborator

Summary

GraphTransformerEncoder configs with pairwise_attention_bias_attr_names (including the learned non-missing bias) now
run on packed tokens on CUDA. The pairwise bias is built per length bucket as the ragged per_pair bias from the
previous PR, so it never exists as a dense [sequences, heads, max_seq_len, max_seq_len] grid.

Approach

  • _packed_pairwise_bias builds the packed-order [sequences, max_seq_len] node grid once (_layout_grids, taken from
    Run 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 out
    head-major. The non-missing bias is added with an out-of-place index_add on the flat block (the old code wrote into
    a permuted view of the dense grid, which is slow on CUDA).
  • The blocks are concatenated in bucket order into PackedAttentionBias.per_pair. Layers that query every token run
    SDPA per bucket; the last anchor_only layer gathers row 0 of each block into a per-key bias and runs the varlen
    kernel.
  • The bias keeps the projection output's dtype; under bf16 autocast that is already the attention dtype, so no layer
    casts it.
  • The padded path is unchanged.

User-facing changes

  • Pairwise attention biases no longer send a config to the padded path on CUDA.
  • gigl.transforms.graph_transformer.lookup_pairwise_relative_features is public (it was
    _lookup_pairwise_relative_features).
  • The pairwise CSR lookups read values back from the device and have data-dependent shapes (once per bucket), so a
    pairwise config does not whole-encoder compile and has no fixed host-sync count.

Verification

  • CPU: packed_graph_transformer_test.py 139 ran / 20 skipped (CUDA-only); packed_attention_test.py 37 / 13;
    graph_transformer_test.py (nn + transforms) 125 / 1; static_khop_test.py 24; all OK. ruff, ty, dprint
    clean.
  • Tests reused from Run pairwise attention biases on packed tokens in GraphTransformerEncoder #856 (supported-config and parity parameters, explicit anchors, projection and non-missing bias
    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.
  • CUDA, one L4 (job gt2-pr3-unit, commit dfb1a5bb): 325 ran, 0 skipped; all pairwise tests pass. One failure was the
    previous 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_len 256, 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 (job gt2-pr3-perf). L4 run-to-run noise is about 5%.

Config Old inference ms (MiB) New inference ms (MiB) Old training ms (MiB) New training ms (MiB)
pairwise bias 120.0 (4113) 137.9 (3438) 366.9 (6512) 275.4 (5108), -25%
per-key bias (unchanged path) 96.0 94.9 (3085) 199.9 (4131) 199.5 (4131)

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

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>
Comment thread gigl/nn/graph_transformer.py Outdated
.flatten(),
)
blocks.append(block)
per_pair = torch.cat(blocks)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we return blocks[0] for a single bucket? torch.cat([block]) copies the full bias

@kmontemayor2-sc
kmontemayor2-sc marked this pull request as ready for review November 25, 2026 00:14

@mkolodner-sc mkolodner-sc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditioned on @yliu2-sc 's approval

This branch has not been deployed

No deployments
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.

4 participants