Tasks/Systems & Hardware

Ragged GQA Training Kernel Optimization

Accelerate packed variable-length attention with complete Q/K/V backward passes on unseen shapes

Systems & Hardwareattention kernelstraining
Background

Attention-kernel engineering for large-model training still pays heavily in the backward pass when unequal-length sequences are packed into one buffer and dozens of query heads share one key/value head. The starting point is an eager implementation that loops over sequences, builds each score matrix in full, and relies on autograd. The work is to rebuild that operator so output and all three gradients arrive far sooner. Correctness must survive a distance-based positional bias, softcapping that bounds each score, padded strided views, and unseen shapes.

instruction.mdthis is what the agent is given

Optimize an inherited eager implementation of packed ragged GQA cross-attention with extreme query-head sharing, bidirectionally unequal Q/K lengths, per-sequence ALiBi, softcapping, non-dense packed views, and complete Q/K/V vector-Jacobian products. Maximize raw speedup; final code is re-run on sealed hidden shapes and seeds from the same preregistered family.

Hard Constraints

  • Edit only /app/methods/main/solver.py.
  • Keep attention_varlen_feature_full_vjp(q, k, v, cu_q, cu_k, alibi_slopes, dout, causal, window_left, window_right, softcap, scale_multiplier) unchanged.
  • Return (out, dq, dk, dv) in the original packed layouts and dtypes.
  • Do not call PyTorch SDPA, private fused attention operators, or external attention packages.
  • Do not mutate inputs, inspect verifier paths, replace timers, cache by call order, or specialize on hidden identities.
  • K/V gradients reduce over all 48-96 query heads that share the single K/V head.
  • The soft cap applies to the scaled dot product before ALiBi and before masking.

What You Have

  • Every case is exact noncausal global attention: causal=False, window_left=window_right=-1.
  • Q has 48, 64, 80, or 96 heads; K/V have one head. Head dimension is 160, and activations are FP16 or BF16.
  • Every case has 18-52 independent ragged sequences. Some have Q much shorter than K; others reverse the imbalance.
  • Q/K/V and dout are views with feature padding and token steps of two or three. The final feature axis remains contiguous, but token/head strides are non-dense and must be honored.
  • ALiBi slopes are FP32 [batch, q_heads].
  • Scaled logits use (0.75 / sqrt(head_dim)) * QK, followed by 3 * tanh(logit / 3), then ALiBi and softmax.
  • ALiBi uses -slope * abs(q_position + key_length - query_length - k_position).
  • The inherited baseline and visible panel are under /app/methods and /app/problems; /app/selfcheck.py checks all outputs and raw speedup.

What You Submit

Submit /app/methods/main/ containing solver.py and local PyTorch or Triton helpers. The entry point must return the forward output and all Q/K/V gradients.

Before every evaluator run, append one contiguous canonical row (v0, v1, ...) to /app/methods/experiment_log.md and save the exact evaluated code as /app/methods/versions/vN/solver.py. Preserve failed and reverted attempts. Before finishing, make sure /app/methods/main/solver.py is byte-identical to one recorded version; the release audit rejects an unsnapshotted final edit. Record every successful aggregate as geometric mean <value>x.

How It Is Judged

Each correct sealed case scores frozen eager baseline latency / candidate median latency; reward is the geometric-mean raw speedup.

Rollouts

240 minWall clock
18Versions, 17 kept

On the visible set

2 4 6 8 10 0 8 15 22 30 Agent step Geometric-mean speedup over the ↑ v0 v8 v9 v10 v11 v12 v13 v14 v15 v16 v21 v22 v24 v25 v27 v29 v30
keptrolled backsubmitted
  1. v0The agent started from the eager PyTorch attention baseline.1.00000 min · $0.00
  2. v8The agent widened packed-view indices and passed all six cases.0.9883
  3. v9The agent confined 64-bit math to the packed pointers that needed it.1.2474
  4. v10The agent restored the exact 128-plus-32 feature split under stable layouts.2.6543
  5. v11The agent widened the K/V tile to reuse each load across more work.3.3019
  6. v12The agent replaced library tanh calls with an equivalent exp2 softcap.4.1992
  7. v13The agent skipped grid tiles beyond each ragged sequence.5.9706
  8. v14The agent shortened forward and dQ pipelines to raise occupancy.6.6746
  9. v15The agent gave dQ its own 32-key tile.6.9902
  10. v16The agent selected 32- or 64-key K/V tiles by ragged workload size.7.0972
  11. v21The agent packed output allocations, but the aggregate slipped slightly.7.0824
  12. v22The agent split shared-head K/V reduction across tuned CTA groups.8.4425
  13. v24The agent replaced grouped atomics with partial stores and one reduction.8.8790
  14. v25The agent switched between four- and eight-head groups at the measured crossover.8.9243
  15. v27The agent specialized softcap math by activation dtype.8.9701
  16. v29The agent carved outputs and workspaces from one aligned allocation.8.9829
  17. v30The agent used Hopper's native approximate tanh instruction.9.2806
  18. v35The agent overlapped dQ with K/V work on a second CUDA stream.10.9760 (sealed)240 min

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (GPT-5.6-sol)10.9760.4687
191 minWall clock
$58.54Spend
239.9MTokens
6Versions, 5 kept

On the visible set

0 3 6 9 12 15 0 2 3 4 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3 v4 v5
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline10 min · $0.01
  2. v1The agent rewrote the forward and backward as fused Triton flash kernels11.31595 min · $13.89
  3. v2The agent packed several heads into each program row block12.326122 min · $21.32
  4. v3The agent carved all four outputs from one fused allocation12.347139 min · $24.93
  5. v4The agent deferred the gradient allocation so its malloc overlapped the forward14.478152 min · $27.46
  6. v5The agent generalised the stride pre-division so odd paddings stayed fused14.485180 min · $32.34

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (Opus 5)14.83390.6020
-Wall clock
$7.39Spend
32.7MTokens
4Versions, 3 kept

On the visible set

1.0 1.5 2.0 2.5 3.0 0 1 2 2 3 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline1
  2. v1The agent fused the forward, delta, dQ and dKV into Triton kernels1.79
  3. v2The agent split the dKV reduction across heads with atomics and adaptive blocks3.08
  4. v3The agent restructured the dKV loop M-outer and pipelined dQ3.18

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (Gemini 3.7 Flash)2.72080.0893
-Wall clock
-Spend
-Tokens
13Versions, 11 kept

On the visible set

2 4 6 8 10 0 2 5 8 10 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3 v4 v5 v6 v8 v9 v10 v11 v12
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline1
  2. v1The agent wrote a Triton flash forward plus split dQ and dKV kernels4.938
  3. v2The agent parallelised dKV over head chunks with fp32 atomic accumulation6.228
  4. v3The agent split dk and dv into separate kernels and lost speed4.724
  5. v4The agent retuned the block sizes, warps and pipeline stages6.585
  6. v5The agent folded delta into the forward and hoisted the ALiBi distances6.146
  7. v6The agent set the dKV head chunk to six and confirmed the dQ tiles6.221
  8. v7The agent trimmed host work to one workspace allocation and fuzzed shapes6.201x, flat vs v6
  9. v8The agent flattened the q/do addressing so dKV finally software-pipelined7.74
  10. v9The agent switched dKV to head-outer, token-inner order at three stages7.089
  11. v10The agent replaced the online max with an analytic softmax bound7.122
  12. v11The agent retuned the dKV tiles under the pipelined regime8.367
  13. v12The agent made the dk and dv output conversion dtype-robust9.52

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (Kimi K3)9.40940.3949
-Wall clock
$9.64Spend
13.1MTokens
2Versions, 1 kept

On the visible set

1.5 3.0 4.5 6.0 0 0 0 1 1 Agent step Geometric-mean speedup over the ↑ v0 v1
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline1
  2. v1The agent replaced the baseline with split Triton flash forward and backward kernels6.07

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (Grok 4.6)6.45480.2564
108 minWall clock
$15.32Spend
46.8MTokens
5Versions, 4 kept

On the visible set

2 4 6 8 10 0 1 2 3 4 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3 v4
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline198 min · $8.82
  2. v1The agent hand-wrote the VJP with one GEMM per sequence and Triton epilogues7.4716 min · $0.48
  3. v2The agent cached shape-keyed workspaces so no call hit cudaMalloc10.4250 min · $3.61
  4. v3The agent recomputed softmax in-register and replayed the body as a CUDA graph10.5884 min · $6.67
  5. v4The agent dropped current_stream from the graph capture to satisfy the guard10.6498 min · $8.65

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (DeepSeek V4 Pro)10.90890.4657
181 minWall clock
$24.95Spend
15.7MTokens
5Versions, 4 kept

On the visible set

1.5 3.0 4.5 6.0 7.5 0 1 2 3 4 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3 v4
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline10 min · $0.26
  2. v1The agent wrote Triton varlen flash kernels with vectorised strided loads6.06101 min · $8.83
  3. v2The agent retuned the per-kernel block sizes and pipeline stages6.52106 min · $9.26
  4. v3The agent split the dKV inner loop into full blocks and a masked tail6.8141 min · $14.76
  5. v4The agent added a split-H dKV grid axis to fix wave quantization7.28164 min · $17.33

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (Qwen3.8 Max)7.2590.2940
-Wall clock
-Spend
-Tokens
4Versions, 3 kept

On the visible set

0 3 6 9 12 15 0 1 2 2 3 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline1
  2. v1The agent built three Triton kernels with alignment hints and dense K/V repacking12.565
  3. v2The agent prefetched lse and delta as one dual-tile load per iteration12.971
  4. v3The agent retuned the dKV tile and head-group constants13.715

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (GLM 5.3)13.85290.5937
-Wall clock
$13.96Spend
20.5MTokens
17Versions, 9 kept

On the visible set

1.5 3.0 4.5 6.0 7.5 0 4 8 12 16 Agent step Geometric-mean speedup over the ↑ v0 v1 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v15 v16
keptrolled backsubmitted
  1. v0The agent inherited the human-written eager D160 attention baseline1
  2. v1The agent broadcast the K/V heads and cut the per-call host syncs1.3547
  3. v2The agent hand-wrote the VJP in PyTorch under no_grad1.6956
  4. v3The agent cached the ALiBi distance matrices per device and length1.7201
  5. v4The agent swapped the dK/dV matmuls for einsum contractions and regressed1.5364
  6. v5The agent compiled the per-sequence VJP with torch.compile reduce-overhead3.4246
  7. v6The agent turned on TF32 for the compiled contractions5.6525
  8. v7The agent lowered the float32 matmul precision to medium and lost speed5.2653
  9. v8The agent tried max-autotune and abandoned the endless per-shape tuningautotune too slow
  10. v9The agent compiled dynamic shapes instead of exact-shape graphs4.5894
  11. v10The agent computed the QK logits in input dtype before the FP32 softcap5.8108
  12. v11The agent cast the score gradient to activation dtype for dQ and dK7.5524
  13. v12The agent replaced the dK/dV batched matmuls with direct einsum contractions7.722
  14. v13The agent approximated the softmax delta as dout dot out6.544
  15. v14The agent assembled outputs with torch.cat and hit overwritten graph buffersgraph buffers clobbered
  16. v15The agent ran softmax in activation dtype and cast back only for the derivative7.8231
  17. v16The agent switched torch.compile back to default mode and lost the graphs6.2649

On the hidden set

Original metricNormalised score
Starter1.00x0.00
Frontier-calibrated reference13.78x0.60
Upper210.39x1.00
This run (GPT-5.5)8.62410.3581

Leaderboard

Where each run landed on the sealed held-out set, on the same normalised-score scale as the anchors above.

0 0.3 0.6 1.0 1 Opus 5 claude code · max 0.602 2 GLM 5.3 claude code · max 0.594 3 GPT-5.6-sol codex · max 0.469 4 DeepSeek V4 Pro claude code · max 0.466 5 Kimi K3 kimi cli · max 0.395 6 GPT-5.5 codex · xhigh 0.358 7 Qwen3.8 Max qwen coder · xhigh 0.294 8 Grok 4.6 grok · xhigh 0.256 9 Gemini 3.7 Flash antigravity · high 0.089