Repository navigation
Run relation attention on packed tokens with an out-of-place ragged bias - #916
Open
kmontemayor2-sc wants to merge 8 commits into
Open
kmontemayor2-sc wants to merge 8 commits into
kmontemayor2-sc wants to merge 8 commits into
Conversation
relation_attention_mode edge_type_bilinear and edge_type_hgt now run on the packed path. Each layer scores its relation edges per relation segment and adds the scores to its own copy of the length-bucketed per-pair bias with an out-of-place index_add, so the bias shared by the layers is never written and the per-layer recompute stays valid; attention then runs SDPA per bucket. The last first-token layer adds the scores to a per-key bias instead and runs the varlen kernel. The padded path shares the score computation, which no longer reads one host value per relation. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
yliu2-sc
reviewed
Nov 24, 2026
| layout.position[target].unsqueeze(1), | ||
| layout.position[source].unsqueeze(1), | ||
| ) # [num_edges, num_heads] | ||
| if per_pair is None: |
Collaborator
There was a problem hiding this comment.
When there is no incoming per-pair bias, could we use index_add_ on the freshly allocated zero buffer? It is not shared, so this avoids another full bias allocation
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
The per-pair and first-token per-key buffers are zeros this method just allocated and nobody else holds, so index_add_ avoids a second full bias-sized allocation. The incoming shared bias stays out of place because a write would leak this layer's scores into other layers and the recompute. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ntion # Conflicts: # tests/unit/nn/packed_graph_transformer_test.py
_relation_attention_scores scored each relation on selected_query[start:end] / selected_key[start:end]. Each slice's backward is a zero tensor the size of all the edges plus a copy, summed once per relation. Tensor.split's backward is one concatenation. Same change as for the relation messages; outputs are unchanged. 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
relation_attention_modeedge_type_bilinearandedge_type_hgtnow run on packed tokens on CUDA. Each layer addsits per-edge relation scores to its own copy of the length-bucketed per-pair bias. The old version wrote the scores
into a new dense grid per layer through a permuted view, which cost 19 ms per write on an L4.
Approach
forward_packedneedslayout.relation_edgesfor relation attention or messages; the score computationis shared with the padded path (
_relation_attention_scores: one batched matmul per relation segment, then onescatter). That removes the padded path's per-relation
.item()reads._relation_attention_pair_bias(the name is kept). With everytoken queried, the scores go into the ragged
per_pairwith an out-of-placeindex_addatper_pair_linear_index(sequence, head, query position, key position). The layer starts from the incoming pairwise biaswhen there is one and from zeros otherwise. It never writes into the incoming buffer: the layers share it, and an
in-place write gives wrong outputs and gradients with no autograd error. It would also break the recompute contract of
_run_packed_layer(per-layer checkpointing).anchor_onlylayer only query row 0 is read, which is a per-key bias. There the scores are added to aper-key bias at the edge's source token, and no per-pair buffer is built.
User-facing changes
GraphTransformerEncoderLayer.forward_packedaccepts relation attention; it raisesValueErroronly whenlayout.relation_edgesis missing for a relation attention or message mode.Verification
packed_graph_transformer_test.py180 ran / 33 skipped (CUDA-only);graph_transformer_test.py(nn +transforms) 125 / 1;
packed_attention_test.py37 / 13;static_khop_test.py24; all OK.ruff,ty,dprintclean.
length buckets. Cases: bilinear, HGT, bilinear + pairwise, bilinear + anchor bias, HGT + pairwise + anchor bias.
gt2-pr4-unit, commit fea1d95e): 366 tests ran, 0 skipped.packed_attention_testpassed 37 of 37,which includes the earlier bf16 fix. There was 1 error, in the new
test_cuda_relation_attention_with_pairwise_bias_matches_cpu_in_eval_and_training: the test passedhid_dim=128without a matching
hop_distanceembedding. That was a test bug, and the fix is in commit c66dc919 on this branch.The re-run of the merged-up tip in job
gt2-pr6-unitpassed: 463 tests, 0 skipped, all OK, including this test.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. Times are ms per batch
or step, with peak MiB in parentheses. Old tip = stack tip
f93cdf2c. New = this PR (jobgt2-pr4-perf). L4 run-to-runnoise is about 5%.
with a 4-index write on the dense grid).
varlen kernel per bucket, which brings bilinear inference to 101.4 ms. That matches the dense-grid write fix (about
101 ms).
🤖 Generated with Claude Code