Tasks/Systems & Hardware

TPU v6e Masked GQA Kernel Optimization

Accelerate decode, prefill, and encoder attention across masks, head-sharing ratios, and unseen shapes

Systems & HardwareTPU v6eattention kernels
Background

Accelerator kernel engineering — hand-written code that drives a chip's matrix units — has no single attention routine that stays fast across mask shapes and head-sharing ratios production models use. The starting point is a compiler-generated routine on one TPU chip that builds the grid of position-to-position scores in memory before masking and normalizing it. The work is to replace it so masked-out regions are never computed. One design must cover short queries against long caches and unmasked blocks, without drifting from a full-precision reference.

instruction.mdthis is what the agent is given

You are given a working attention implementation in /app/methods/main/attention.py and a set of visible benchmark cases in /app/data/visible_strata.json. Make make_attention return the fastest callable you can for each configuration, on the single TPU v6e chip visible inside this container. The shipped implementation is the reward zero point: matching it scores 0. You may delete and rewrite everything under /app/methods/main/; only the entry signature and output contract are fixed. Grading runs your code on held-out configurations drawn from the same distribution as the visible ones (different shapes and seeds), each case gated on numerical correctness, and rewards the per-case speedup over the same baseline implementation measured on the same chip.

Hard Constraints

  • Submit an algorithm, not answers: no branching on case name or seed; your code must handle any configuration from the documented family.
  • Entry signature, verbatim: make_attention(cfg) in /app/methods/main/attention.py, returning a callable f(q, k, v).
  • Output contract: bfloat16, same shape as q; a case counts only if it matches the fp32 reference within rtol=2e-2, atol=2e-2.
  • Deterministic: same inputs, same output.
  • JAX only (any of jax/jnp/pallas as installed in the image); no additional packages.

What You Have

  • /app/methods/main/attention.py — the baseline (plain XLA, fp32 softmax). This is what your speedup is measured against.
  • /app/data/visible_strata.json — visible cases: decode/prefill/encoder shapes, GQA ratios 1:1 to 16:1, optional causal mask, optional sliding window, optional logit softcap, head_dim 128, bf16 inputs.
  • /app/eval/attn_eval.py — the exact correctness check and timing harness grading uses.
  • python /app/selfcheck.py — free, unlimited: correctness + speedup on every visible case against a frozen copy of the baseline.
  • One TPU v6e chip. jax.devices() shows exactly it.

What You Submit

# /app/methods/main/attention.py
def make_attention(cfg: dict):
    """cfg: batch, q_len, kv_len, num_q_heads, num_kv_heads, head_dim,
    causal, window, softcap, seed, name."""
    def f(q, k, v):  # bf16 in, bf16 out, q.shape
        ...
    return f
output: bfloat16 array of shape (batch, num_q_heads, q_len, head_dim)

How It Is Judged

Per held-out case: run make_attention(cfg), check correctness against the fp32 reference (fail -> that case scores 0), then time it (median over repeated rounds after warmup) and compute speedup = baseline_time / your_time where baseline_time is the frozen measurement of the shipped implementation on the same chip. Each case's speedup maps onto [0, 1] through the task's anchor band (log-linear within segments); the final reward is the arithmetic mean over all held-out cases. Compilation happens once per case before timing and is not counted.

Rollouts

109 minWall clock
$22.23Spend
46.6MTokens
7Versions, 6 kept

On the visible set

0 2 4 6 8 10 12 0 75 150 225 300 Agent step Visible mean speedup ↑ v0 v1 v2 v3 v4 v5 v6
keptrolled backsubmitted
  1. v0The agent snapshotted the plain XLA baseline before touching anything.1.0012 min · $0.21
  2. v1The agent tried the stock splash kernel at tiny tiles, and it ran slower than XLA.0.786 min · $0.71
  3. v2The agent gave each mask regime its own tile shape and the kernel took off.7.18115 min · $1.79
  4. v3The agent replaced the online softmax with a fixed shift and specialised the windows.10.10156 min · $9.57
  5. v4The agent trimmed leftover scratch work the kernel no longer needed.10.25865 min · $11.65
  6. v5The agent skipped the softcap entirely where it provably changes nothing.10.35787 min · $16.51
  7. v6The agent covered the one-sided window case its dispatch had been missing.10.388104 min · $21.08

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (GPT-5.6-sol)8.93890.6355
331 minWall clock
$20.98Spend
26.2MTokens
10Versions, 7 kept

On the visible set

0 2 4 6 8 10 12 0 2 4 6 8 Agent step Visible mean speedup ↑ v0 v1 v2 v3 v4 v5 v6 v7
keptrolled backsubmitted
  1. v0The agent inherited the shipped XLA attention as its zero point10 min · $0.21
  2. v1The agent reached for JAX's ready-made splash kernel first5.8157 min · $1.08
  3. v2The agent wrote its own kernel and dropped the running softmax max6.59657 min · $4.66
  4. v3The agent packed the query heads sharing a KV head into one matmul9.807200 min · $12.18
  5. v4The agent made mask cost scale with tile area in its cost model10.439223 min · $13.29
  6. v5The agent stress-tested odd shapes and fixed a silently wrong head-packing trick10.388247 min · $15.03
  7. v6The agent built the mask once per tile and shared it across heads10.975280 min · $16.84
  8. v7The agent re-allowed odd block sizes that divide the awkward sequence lengths10.956329 min · $20.53
  9. -- (K transposed)The agent tried storing K transposed and reverted it as mixed-to-worsemixed, mostly slower$20.76
  10. -- (numerics)The agent tested three numerics variants for the matmuls and adopted noneall negative, dropped$20.76

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (Opus 5)10.27030.6419
33 minWall clock
$5.71Spend
37.7MTokens
3Versions, 2 kept

On the visible set

0 2 4 6 8 10 12 0 0 1 2 2 Agent step Visible mean speedup ↑ v1 v2
keptrolled backsubmitted
  1. v0The agent profiled the plain XLA baseline's memory traffic1.62-11.5ms baseline
  2. v1The agent dropped in the splash kernel and swept its tile sizes6.47
  3. v2The agent let the mask shape pick the tile size6.688

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (Gemini 3.7 Flash)6.73070.5053
149 minWall clock
$6.74Spend
12.8MTokens
8Versions, 6 kept

On the visible set

0 2 4 6 8 10 12 0 2 3 4 6 Agent step Visible mean speedup ↑ v0 v1 v1+sweep v3 v4 v5 splash comparison
keptrolled backsubmitted
  1. v0The agent measured the shipped XLA attention as its starting point1$0.43
  2. v1The agent wrote a Pallas flash kernel that skips fully masked blocks3.06$0.73
  3. v1+sweepThe agent swept block sizes and found bigger tiles feed the matmuls better6.31$1.72
  4. v2The agent folded the winning 1024-wide tile sweep into the kernelno number logged$2.70
  5. v3The agent chose the loop form per mask shape so units overlap6.787$3.69
  6. v4The agent removed the online max after proving a static shift safe7.472$4.81
  7. v5The agent reused each K/V load across several query tiles7.638$6.03
  8. splash comparisonThe agent benchmarked the library splash kernel and dropped it as slower5.65$6.38

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (Kimi K3)7.34940.5325
44 minWall clock
$7.26Spend
11.3MTokens
6Versions, 3 kept

On the visible set

0 2 4 6 8 10 12 0 20 40 60 80 Agent step Visible mean speedup ↑ v1 v2 v3 v4 v5
keptrolled backsubmitted
  1. v1The agent fused batch into heads and ran the stock splash kernel6.171
  2. v2The agent patched the kernel's PV matmul to bf16 and dropped it6.701
  3. v3The agent picked tile sizes from the shape instead of one fixed setting6.941
  4. v4The agent added a short-query shortcut that mismatched the window tiling6.745
  5. v5The agent matched the tiles to the window width and kept the shortcut6.924
  6. v6The agent widened the query tile for long-KV prefill shapessame as v5, correct

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (Grok 4.6)6.75050.5136
135 minWall clock
$1.54Spend
9.5MTokens
13Versions, 12 kept

On the visible set

0 2 4 6 8 10 12 0 2 5 8 10 Agent step Visible mean speedup ↑ v0_baseline v1 v1_flash v2 v2_norescale v3 v3_regions v4 v4_exactwidth v5 v5_submission v6 v6_final
keptrolled backsubmitted
  1. v0_baselineThe agent snapshotted the shipped XLA attention as its baseline1$0.12
  2. v1The agent wrote flash attention in plain XLA so scores never reach memory5.05$0.25
  3. v1_flashThe agent wrote flash attention in plain XLA so scores never reach memory5.05$0.37
  4. v2The agent dropped the running-max rescale once it saw logits were bounded8.3$0.50
  5. v2_norescaleThe agent dropped the running-max rescale once it saw logits were bounded8.3$0.62
  6. v3The agent split each query block into masked edges and a maskless middle9.32$0.75
  7. v3_regionsThe agent split each query block into masked edges and a maskless middle9.32$0.87
  8. v4The agent shrank masked edges to exact width and skipped empty blocks10.9$1.00
  9. v4_exactwidthThe agent shrank masked edges to exact width and skipped empty blocks10.9$1.12
  10. v5The agent replaced hand-picked tiles with a shape-driven tile rule10.7$1.24
  11. v5_submissionThe agent replaced hand-picked tiles with a shape-driven tile rule10.7$1.37
  12. v6The agent retuned the tile rule on invented shapes for held-out coverage11.05134 min · $1.49
  13. v6_finalThe agent retuned the tile rule on invented shapes for held-out coverage11.05$1.52

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (DeepSeek V4 Pro)8.19280.6028
720 minWall clock
$19.71Spend
38.3MTokens
26Versions, 23 kept

On the visible set

0 2 4 6 8 10 12 0 100 200 300 Agent step Visible mean speedup ↑ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v14 v15 v16 v18 v19 v20 v22 v23 v24 v25 v26
keptrolled backsubmitted
  1. v1The agent snapshotted the shipped XLA attention as its reference16 min · $0.28
  2. v2The agent hand-wrote a Pallas flash kernel with tiles far too small0.4417 min · $0.77
  3. v3The agent widened the tiles and let interior tiles skip the mask math1.34525 min · $1.08
  4. v4The agent proved the logits bounded and threw away the running-max rescale5.244 min · $1.82
  5. v5The agent stacked a GQA group's query heads onto one K/V load5.2364 min · $2.62
  6. v6The agent removed the per-tile branch that wrecked the compiler's scheduling7.7574 min · $2.98
  7. v7The agent widened the inner tile and steered around a NaN-producing race8.55103 min · $3.86
  8. v8The agent aligned windowed query tiles so they never straddle head blocks8.61129 min · $4.96
  9. v9The agent extended the kernel down to single-token decode shapes8.65142 min · $5.50
  10. v10The agent fuzzed for nondeterminism and rebuilt the KV tiling race-free8.618210 min · $7.64
  11. v11The agent relaxed the tiling rules its padding fix had made safe8.624233 min · $8.11
  12. v12The agent stopped a ragged remainder tile from dragging long prefill shapes8.617253 min · $8.52
  13. v13The agent tried a polynomial softcap, and the serial chain ran slower2.1-2.2x slower260 min · $9.03
  14. v14The agent folded the softmax scale into the tanh argument8.621268 min · $9.24
  15. v15The agent merged the KV loop into one tile when nothing was skippable8.613284 min · $9.79
  16. v16The agent hoisted the mask branch so fully-valid tiles skip it8.68313 min · $10.26
  17. v17The agent tried a Taylor softcap, and the compiler scheduled it worse16-38% slower321 min · $10.45
  18. v18The agent pushed the merged tile out to 8k after diagnosing the VMEM limit8.694347 min · $11.08
  19. v19The agent added deep KV tiles for long sequences with little to skip8.678373 min · $11.82
  20. v20The agent loosened the alignment rule so more shapes reach the merged tile8.683408 min · $12.78
  21. v21The agent let non-causal shapes stack heads, since their masks ignore positionflat, 7.36x unchanged428 min · $13.46
  22. v22The agent pre-scaled the query once per program for windowed cases8.863538 min · $16.12
  23. v23The agent merged the KV tiles when the window leaves nothing skippable8.884604 min · $17.77
  24. v24The agent retired an old race guard that padding had made unnecessary8.876641 min · $18.83
  25. v25The agent gated the merge on tile alignment after a ragged shape exhausted VMEM8.908691 min · $19.34
  26. v26The agent switched off bounds checks it had already proven unnecessary8.925714 min · $19.58

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (Qwen3.8 Max)8.21780.5896
360 minWall clock
$15.88Spend
55.8MTokens
8Versions, 7 kept

On the visible set

0 2 4 6 8 10 12 0 50 100 150 200 Agent step Visible mean speedup ↑ v1 v2 v3 v4 v5 v6 v7 v8
keptrolled backsubmitted
  1. v1The agent dropped in a Pallas flash kernel that never writes scores out4.54527 min · $1.29
  2. v2The agent made the tiling follow each shape's divisors and split mask-free blocks5.59754 min · $2.36
  3. v3The agent let the kernel time a few geometries and keep the fastest6.13797 min · $4.19
  4. v4The agent cleaned up the masking and exploited the softcap's bounded logits6.185135 min · $6.22
  5. v5The agent dropped the running row max once a fixed shift proved safe7.263207 min · $10.29
  6. v6The agent widened query tiles across whole GQA groups7.296255 min · $12.18
  7. v7The agent reordered the autotune list so big tiles get tried first7.302303 min · $13.67
  8. v8The agent hoisted the scaled query out of the inner loop and shipped it7.25329 min · $14.66

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (GLM 5.3)7.77930.5569
44 minWall clock
$4.70Spend
5.9MTokens
9Versions, 8 kept

On the visible set

0 2 4 6 8 10 12 0 2 4 6 8 Agent step Visible mean speedup ↑ v0 v1 v2 v3 v4 v5 v5 validation v6 v6 final
keptrolled backsubmitted
  1. v0The agent started from the plain XLA attention baseline0.9993 min · $0.20
  2. v1The agent used a fast kernel only where sliding windows made it safe1.14810 min · $1.11
  3. v2The agent gave grouped-query attention its own cheaper path1.17114 min · $1.44
  4. v3The agent found far larger kernel tiles unlock most of the speedup4.43721 min · $2.03
  5. v4The agent routed plain full and causal cases to a fused attention call4.60526 min · $2.69
  6. v5The agent extended the fast kernel to every evenly-tiling shape6.84430 min · $3.13
  7. v5 validationThe agent checked its dispatcher on freshly generated configs5.02$3.61
  8. v6The agent tuned tile sizes per attention shape instead of one global choice7.24140 min · $4.08
  9. v6 finalThe agent reran the final dispatcher to confirm the speedup7.246$4.39

On the hidden set

Original metricNormalised score
Starter1.00.0
Upper43.76081.0
This run (GPT-5.5)6.67370.5267

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.642 2 GPT-5.6-sol codex · max 0.636 3 DeepSeek V4 Pro claude code · max 0.603 4 Qwen3.8 Max qwen coder · xhigh 0.590 5 GLM 5.3 claude code · max 0.557 6 Kimi K3 kimi cli · max 0.533 7 GPT-5.5 codex · xhigh 0.527 8 Grok 4.6 grok · xhigh 0.514 9 Gemini 3.7 Flash antigravity · high 0.505