callensxavier commited on
Commit
52cc75f
·
verified ·
1 Parent(s): 90e48bd

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +61 -110
README.md CHANGED
@@ -2,134 +2,85 @@
2
  license: mit
3
  tags:
4
  - attention
 
5
  - efficient-attention
6
  - number-theory
7
  - calabi-yau
8
- - custom-kernel
9
- - pytorch
10
- - benchmark
11
- datasets: []
12
  ---
13
 
14
- # S20-Decay Attention Kernel
15
 
16
- A high-performance, mathematically exact attention bias derived from the Weight-5 Apéry-like binomial sum:
17
 
18
  $$S_{20}(n) = \sum_{k=0}^{n} \binom{n}{k}^4 \binom{n+k}{k}$$
19
 
20
- > **Paper**: [A Weight-5 Apéry-like Binomial Sum, its Calabi-Yau 4-fold Period, and Supercongruences](https://doi.org/10.5281/zenodo.20747943)
21
- > **Code**: [GitHub Mirror-Map-Sieve](https://github.com/xaviercallens/Mirror-Map-Sieve)
22
-
23
- ---
24
-
25
- ## 3 Core Hypotheses
26
-
27
- 1. **Exact Mathematical Rigidity**: Unlike ALiBi or learned positional embeddings, the S20 sequence provides a deterministic, integer-derived attention decay. Zero floating-point drift at any context length.
28
- 2. **O(1) Vectorized Toeplitz Construction**: The decay matrix is built as a 1D broadcast over a distance tensor — no nested loops, no learned parameters.
29
- 3. **Zero-Cost LLM Injection**: S20 decay can be injected into any SDPA-based model via global monkey-patching (`F.scaled_dot_product_attention`) with **zero measurable latency overhead** on GPU.
30
-
31
- ---
32
-
33
- ## GPU Benchmark: Tesla T4 (16GB, CUDA 12.9, PyTorch 2.9.1)
34
-
35
- ### Raw Kernel: SDPA ± S20 Bias
36
-
37
- | Seq Len | SDPA Baseline | SDPA + S20 | Overhead | Correct |
38
- |---------|--------------|------------|----------|---------|
39
- | 64 | 0.020 ms | 0.022 ms | 1.08× | ✅ |
40
- | 128 | 0.024 ms | 0.029 ms | 1.23× | ✅ |
41
- | 256 | 0.041 ms | 0.053 ms | 1.31× | ✅ |
42
- | 512 | 0.092 ms | 0.144 ms | 1.56× | ✅ |
43
- | 1024 | 0.199 ms | 0.529 ms | 2.65× | ✅ |
44
-
45
- ### Phi-3-mini-4k-instruct (3.8B) — Global SDPA Patching
46
-
47
- | Seq Len | Baseline | S20-SDPA | Overhead | Base tok/s | S20 tok/s | Energy (J) | Power (W) |
48
- |---------|----------|----------|----------|------------|-----------|------------|-----------|
49
- | 64 | 49.59 ms | 49.09 ms | **0.99×** | 1,290 | 1,304 | 67.9 | 69.2 |
50
- | 128 | 59.21 ms | 59.15 ms | **1.00×** | 2,162 | 2,164 | 82.3 | 69.8 |
51
- | 256 | 106.98 ms | 107.39 ms | **1.00×** | 2,393 | 2,384 | 115.5 | 54.4 |
52
- | 512 | 211.06 ms | 213.15 ms | **1.01×** | 2,426 | 2,402 | 297.3 | 69.9 |
53
- | 1024 | 488.51 ms | 487.11 ms | **1.00×** | 2,096 | 2,102 | 659.9 | 67.7 |
54
-
55
- > **Key finding**: On a real 3.8B-parameter model, S20 global SDPA patching adds **zero measurable overhead** (0.99–1.01×) across all sequence lengths. The integer-sequence bias is effectively free on GPU.
56
-
57
- ---
58
-
59
- ## CPU Benchmark: Open-Weights Model Injection
60
-
61
- S20 decay injected as post-logit positional mask on CPU (Apple Silicon):
62
-
63
- | Model | Params | Baseline | S20-Injected | Overhead | Avg PPL |
64
- |-------|--------|----------|--------------|----------|---------|
65
- | GPT-2 | 124M | 39.2 ms | 41.6 ms | 1.06× | 175.7 |
66
- | DistilGPT-2 | 82M | 21.6 ms | 21.3 ms | 0.99× | 302.0 |
67
- | OPT-125M | 125M | 29.0 ms | 29.4 ms | 1.01× | 199.7 |
68
- | BLOOM-560M | 559M | 122.4 ms | 118.0 ms | 0.96× | 139.0 |
69
-
70
- ---
71
-
72
- ## Method: Global SDPA Patching (Forward Hook Option A)
73
-
74
- ```python
75
- import torch
76
- import torch.nn.functional as F
77
- from math import comb
78
-
79
- # 1. Build S20 decay sequence
80
- def s20(n): return sum(comb(n, k)**4 * comb(n+k, k) for k in range(n+1))
81
- _S20 = [s20(d) for d in range(18)]
82
-
83
- # 2. Vectorized log-bias matrix
84
- def build_s20_log_bias(seq_len, device="cuda", dtype=torch.float16):
85
- base = float(_S20[0])
86
- weights = [base/float(x) if x > 0 else 0.0 for x in _S20] + [0.0]
87
- dv = torch.tensor(weights, dtype=torch.float32, device=device)
88
- idx = torch.arange(seq_len, device=device)
89
- dist = (idx.unsqueeze(0) - idx.unsqueeze(1)).abs().clamp(max=len(_S20))
90
- decay = dv[dist]
91
- log_bias = torch.log(decay.clamp(min=1e-30))
92
- causal = torch.tril(torch.ones(seq_len, seq_len, device=device))
93
- log_bias = log_bias * causal + (1 - causal) * (-1e9)
94
- return log_bias.unsqueeze(0).unsqueeze(0).to(dtype)
95
-
96
- # 3. Monkey-patch F.scaled_dot_product_attention
97
- _original_sdpa = F.scaled_dot_product_attention
98
- _bias_cache = {}
99
-
100
- def patched_sdpa(q, k, v, attn_mask=None, dropout_p=0.0, is_causal=False, **kw):
101
- L = q.shape[-2]
102
- if L not in _bias_cache:
103
- _bias_cache[L] = build_s20_log_bias(L, q.device, q.dtype)
104
- bias = _bias_cache[L][:, :, :L, :k.shape[-2]]
105
- if attn_mask is not None:
106
- attn_mask = attn_mask + bias
107
- else:
108
- attn_mask = bias
109
- return _original_sdpa(q, k, v, attn_mask=attn_mask, dropout_p=dropout_p, **kw)
110
-
111
- F.scaled_dot_product_attention = patched_sdpa
112
- # Now ANY model using SDPA will have S20 decay injected automatically
113
- ```
114
-
115
- ---
116
-
117
- ## Reproducibility
118
 
119
  ```bash
120
- # Clone and run on any CUDA GPU
121
  git clone https://github.com/xaviercallens/Mirror-Map-Sieve.git
122
- cd Mirror-Map-Sieve/4_ai_hardware_attention
123
- pip install torch transformers accelerate
124
- python gpu_benchmark_s20.py --model microsoft/Phi-3-mini-4k-instruct --seq_lens 64 128 256 512 1024
125
  ```
126
 
127
  ## Citation
128
 
129
  ```bibtex
130
- @software{callens2026s20attn,
131
  author = {Callens, Xavier},
132
- title = {S20-Decay Attention Kernel: Vectorized Integer-Sequence Attention Bias},
133
  year = {2026},
134
  url = {https://huggingface.co/callensxavier/s20-attention-kernel},
135
  doi = {10.5281/zenodo.20747943}
 
2
  license: mit
3
  tags:
4
  - attention
5
+ - positional-encoding
6
  - efficient-attention
7
  - number-theory
8
  - calabi-yau
9
+ - negative-result
10
+ datasets:
11
+ - callensxavier/cy-sieve-attention-benchmark
 
12
  ---
13
 
14
+ # CY-Sieve Attention Kernel — a falsifiable experiment (NEGATIVE result)
15
 
16
+ A positional-attention bias derived from the weight-5 Apéry-like binomial sum
17
 
18
  $$S_{20}(n) = \sum_{k=0}^{n} \binom{n}{k}^4 \binom{n+k}{k}$$
19
 
20
+ whose holonomic structure is a CalabiYau **3-fold** period (an order-4 MUM block;
21
+ the geometry fixes the long-range decay slope $\log\lambda = 3.762$ and curvature
22
+ $\beta = 2$). The engineering bet: generate the positional bias *on the fly* from
23
+ the order-4 recurrence, costing **O(L)** HBM instead of an **O(L²)** table.
24
+
25
+ > **Code:** [GitHub — Mirror-Map-Sieve](https://github.com/xaviercallens/Mirror-Map-Sieve)
26
+ > · **Math paper:** [Zenodo 10.5281/zenodo.20747943](https://doi.org/10.5281/zenodo.20747943)
27
+ > · **Benchmark data:** [callensxavier/cy-sieve-attention-benchmark](https://huggingface.co/datasets/callensxavier/cy-sieve-attention-benchmark)
28
+
29
+ ## ⚠️ Honest result (2026-06-22, NVIDIA L4): the quality gate KILLED it
30
+
31
+ This card **corrects** an earlier version that claimed "zero overhead" and implied
32
+ a win. Those numbers came from an invalid method (monkey-patching the bias into a
33
+ *frozen* pretrained model, which collapses every alternative scheme equally). The
34
+ correct test — **training small GPTs from scratch**, one per positional scheme, on
35
+ real WikiText-2 gives:
36
+
37
+ | scheme | ppl @512 (train) | @1024 (2×) | @2048 (4×) |
38
+ |---|---|---|---|
39
+ | **learned-absolute** | **4.22** | 12.10 | 20.82 |
40
+ | ALiBi | 10.74 | 11.73 | 11.35 |
41
+ | **sliding-window** | **4.99** | **5.07** | **5.03** |
42
+ | CY-Sieve τ-ladder | 11.33 | 12.31 | 12.05 |
43
+ | CY-Sieve τ=128 | 6.80 | 7.12 | 7.00 |
44
+ | CY-Sieve τ=512 | 4.65 | 6.08 | 10.62 |
45
+
46
+ **Verdict: KILL (+10.15%).** Best CY-Sieve (4.65) vs best baseline (4.22) exceeds
47
+ the pre-committed >5% kill threshold; a plain **sliding window won outright**. The
48
+ geometry-fixed slope is too steep for a drop-in positional scheme.
49
+
50
+ ## What *did* hold up
51
+
52
+ - **§4 kernel correctness PASS.** The Triton kernel matches the NumPy reference
53
+ within FP16 tolerance (4/4 tests).
54
+ - **§6 memory claim — confirmed.** The on-the-fly bias reads **O(L)** bytes of HBM
55
+ vs **O(L²)** for a materialized table (**8192× less at L=16384**). *But* the
56
+ current unfused kernel is **~4–6× slower** than fused dense SDPA — a
57
+ memory-traffic win, **not** a latency win. Per the project's reporting rule,
58
+ with the quality gate failing, these numbers are **not** a contribution.
59
+
60
+ ## Why publish a negative result?
61
+
62
+ Because a fast, correct kernel that degrades model quality is a *failed* kernel,
63
+ and saying so is the point. The Calabi–Yau geometry is a sound **prior** for the
64
+ bias shape, not the right **value** — pinning the attention slope to the
65
+ sequence's growth rate was the mistake. Redesign directions (learnable
66
+ geometry-initialized slope; exact-local-window + gentle-tail hybrid; β=2 ablation)
67
+ are in the [findings writeup](https://github.com/xaviercallens/Mirror-Map-Sieve/blob/main/docs/PHASE3_CYSIEVE_GPU_FINDINGS.md).
68
+
69
+ ## Reproduce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  ```bash
 
72
  git clone https://github.com/xaviercallens/Mirror-Map-Sieve.git
73
+ cd Mirror-Map-Sieve
74
+ pip install -r 4_ai_hardware_attention/requirements-gpu.txt
75
+ python 4_ai_hardware_attention/run_gpu_phase.py # §4 parity + §5 quality + §6 perf
76
  ```
77
 
78
  ## Citation
79
 
80
  ```bibtex
81
+ @misc{callens2026cysieve,
82
  author = {Callens, Xavier},
83
+ title = {CY-Sieve Attention: a Calabi--Yau positional bias and its negative quality result},
84
  year = {2026},
85
  url = {https://huggingface.co/callensxavier/s20-attention-kernel},
86
  doi = {10.5281/zenodo.20747943}