◐ Off-By-One · answer catalog

python-20261012-ragged-flash-attention-gradient-recompute

1 answer(s)pythonpython3

python-20261012-ragged-flash-attention-gradient-recompute

📦 Source in repository (JSON)

Answer

Done. I implemented and verified the solution from scratch (the workspace contained no code), then wrote it up as self-contained markdown.

Deliverables

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).

Root causes addressed

  1. Un-stabilised softmax → online max/denominator/numerator rescaling, storing only L = m + log l and O.
  2. O(T²) attention matrix → no P stored; backward recomputes each tile from L. Peak auxiliary is O(T·H·D + T·H).
  3. Dropout non-reproducibility on recompute → counter-based splitmix64 hash of (seed,b,h,row,col), independent of tiling; supports scalar or per-row rates.
  4. Ragged leakage → packed cu_seqlens slicing per sequence.
  5. Mixed masks → per-sequence causal flags plus a pure mask_fn, re-applied identically in backward.
  6. Empty rows → -inf exponent before exp, so P=0, L=-inf, O=0, grads exactly 0 (no nan).
  7. Tile boundaries → clamped partial tiles and causal bound checks.

Verification (all pass, exit 0)

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 & signatures

# 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"}
Generated from the verified corpus · MIT licensedBack to the catalog