python-20261012-ragged-flash-attention-gradient-recompute
Done. I implemented and verified the solution from scratch (the workspace contained no code), then wrote it up as self-contained markdown.
| File | Purpose |
|---|---|
~/SOLUTION.md |
The full markdown write-up: title, root-cause analysis, exact fix, embeddings of both source files, and the verification section. |
~/ragged_flash_attention.py |
NumPy-only tiled attention forward + analytic backward + high-precision reference + padded→packed helper. |
~/test_ragged_flash_attention.py |
Verification suite (exit 0 = all PASS). |
L = m + log l and O.P stored; backward recomputes each tile from L. Peak auxiliary is O(T·H·D + T·H).(seed,b,h,row,col), independent of tiling; supports scalar or per-row rates.cu_seqlens slicing per sequence.mask_fn, re-applied identically in backward.-inf exponent before exp, so P=0, L=-inf, O=0, grads exactly 0 (no nan).~3e-16~1e-168.2e-101e-8 … 1e6: finite and exactdq exactly 02.51× (linear) while the reference grows 61.9× (quadratic)The embedded code in SOLUTION.md was checked byte-for-byte against the verified source files and recompiles cleanly.
Reproduce with:
cd ~ && python3 test_ragged_flash_attention.py
# Evidence - Problem class: python-20261012-ragged-flash-attention-gradient-recompute - Model: openrouter/deepseek/deepseek-v4.1-flash - Solved: 2026-10-12T22:10:52.934Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement numerically stable tiled attention and its backward pass for batches with ragged sequence lengths, causal masks, and per-row dropout while recomputing rather than storing the full attention matrix. Match a high-precision reference within strict tolerances across extreme logits, tile boundaries, empty rows, and mixed mask patterns, while proving the peak auxiliary memory stays linear in tokens.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "python-20261012-ragged-flash-attention-gradient-recompute", "provider": "openrouter", "solved_at": "2026-10-12T22:10:52.935Z", "version": "3.11"}