Modular collision-free sparse attention
Implementation & benchmark of arXiv:2608.27432 — Closing the gap and settling the problem of queens on an $n\times n$ board, each attacking at most one other
Source paper: Closing the gap and settling the problem of queens on an $n\times n$ board, each attacking at most one other arXiv:2608.27432 ⓘ · analyzed Aug 29, 2026
AI-generated research hypothesis, automatically tested. Not peer-reviewed.
Idea description
Replace dense attention between grid-arranged tokens by a deterministic block-sparse pattern generated from modular permutations. In each block, connect row token i to column token p(i)=2i modulo B; because i, i-p(i), and i+p(i) are injective modulo B when gcd(B,6)=1, the pattern avoids repeated horizontal, vertical, and diagonal projections. Use shifted permutations across heads to increase receptive-field coverage while retaining structured sparsity.
Formulas
Mathematical statement
The construction decomposes an AB by AB grid into A squared blocks of size B by B. For block coordinates R,C in {1,...,A} and within-block row i in {1,...,B}, the selected column is p(i) congruent to 2i modulo B, with residues represented in {1,...,B}. The global cell has row r=B(R-1)+i and column c=B(C-1)+p(i). Its difference and sum coordinates are B(R-C)+i-p(i) and B(R+C-2)+i+p(i). For two distinct within-block indices i1 and i2, the projections i, p(i), i-p(i), and i+p(i) are distinct modulo B when gcd(B,6)=1: p(i)=2i, i-p(i)=-i, and i+p(i)=3i modulo B, and multiplication by 1, 2, and 3 is invertible modulo B. We transfer this injective-projection property to a sparse attention edge pattern so that different local source indices do not repeatedly collide under the corresponding grid directions.
Implementation notes
Integrate the construction at the attention-mask stage, before computing attention logits. Reshape N=A times B tokens into an A by A grid of blocks, each block containing B within-block positions. For each head h, choose a shift s_h in {0,...,B-1} and define p_h(i)=1+((2(i-1)+s_h) modulo B). For every block row R, block column C, and within-block query position i, add one edge from r=B(R-1)+i to c=B(C-1)+p_h(i). Store the edge list once as a compressed sparse row structure. At runtime, gather Q[r], K[c], and V[c] for all edges, compute z[r,c]=Q[r] dot K[c] divided by the square root of the head dimension, apply a segmented softmax over keys sharing the same query r, and scatter-add the weighted values into each query output. Use different shifts across heads, for example s_h=h modulo B, and optionally add a small fixed local-window edge set so nearby tokens remain directly connected. The modular formulas and injectivity claims are computed exactly; attention quality, useful shift assignments, and the benefit of local edges must be measured empirically. Start with ViT-Tiny on CIFAR-100 or ImageNet-100, using N=120 tokens, A=8, B=15, and four shifted heads. Compare dense attention, standard window attention, and the modular mask at equal hidden size and batch size. The first success criterion is at least 2 times lower attention memory and FLOPs with no more than one percentage point top-1 accuracy loss. A stronger result is matching dense-attention accuracy at larger token counts or outperforming random sparse attention at the same number of edges.
Verification
Stage 1 · Toy mechanism gate: Passed ✓
Stage 2 · Mechanism transferred to benchmark: Not tested
Stage 2 · Practical benchmark result: Not run
Methodology: Toy-system gate first; the benchmark stage runs only after a pass. How verification works
Stage 1 — Mechanism check agent confidence 7/10
Built modular sparse attention with shifted modular permutations, segmented softmax, and scatter-add aggregation. The corrected configuration B=17 passed all injectivity checks and reduced attention score elements by 17x; on the fixed synthetic task it achieved 64.06% accuracy versus 60.94% dense and 63.57% random sparse, although the unoptimized CPU implementation was slower than dense. The originally suggested B=15 is invalid because gcd(15,6)=3 and the i+p projection is not injective.
- Agent confidence
- 7/10
- Baseline
- Dense: test_loss=2.4334, accuracy=60.94%, train_time=7.46s, attention_elements=56,644
- Idea
- Modular B=17: test_loss=1.7336, accuracy=64.06%, train_time=8.75s, attention_elements=3,332, nominal reduction=17x; random sparse accuracy=63.57%
Limitations: Only a small synthetic global-mean classification task was tested, with one seed and equal training steps. Actual optimized sparse-kernel FLOPs, memory, batching scalability, vision accuracy, local-window variants, and GPU performance were not tested; the Python gather/scatter implementation did not produce wall-clock speedup on CPU.
How to run: OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 /home/maxwelhelp/main/bin/python3 -u benchmark.py
Verdict computed by deterministic test code from paired-seed statistics — not by the language model.
Artifacts
- benchmark.py 4.9 KB View Raw
- report.md 1.4 KB View