callensxavier commited on
Commit
5a152fc
·
verified ·
1 Parent(s): 73360ee

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +79 -48
README.md CHANGED
@@ -7,90 +7,121 @@ tags:
7
  - calabi-yau
8
  - custom-kernel
9
  - pytorch
 
 
10
  ---
11
 
12
- # S20-Decay Attention Kernel (Callens-ALIX)
13
 
14
- This repository hosts the artifacts, benchmarking data, and reference implementation for the **S20-Decay Attention Kernel**, a high-performance, mathematically exact attention bias derived from the Weight-5 Apéry-like binomial sum.
15
 
16
  $$S_{20}(n) = \sum_{k=0}^{n} \binom{n}{k}^4 \binom{n+k}{k}$$
17
 
18
- > **Related Academic Paper (Math Track)**: [Automated Classification of Calabi-Yau Periods and the Universal Diagonal Theorem via the Mirror Map Sieve](https://doi.org/10.5281/zenodo.20747943)
19
- > **Source Code**: [GitHub - Mirror-Map-Sieve](https://github.com/xaviercallens/Mirror-Map-Sieve)
20
 
21
- ## 3 Core Hypotheses & Findings
 
 
22
 
23
- 1. **Exact Mathematical Rigidity**: Unlike ALiBi or learned position embeddings that rely on floating-point parameters, the S20 sequence provides a deterministic, integer-derived attention decay. This entirely eliminates floating-point drift at long context horizons.
24
- 2. **O(1) Vectorized Toeplitz Performance**: The legacy O(L²) nested-loop construction was the bottleneck. By vectorizing the $S_{20}(|i-j|)$ decay matrix as a 1D sequence broadcast mapped over a distance tensor, the ALIX-v2 kernel runs **~21× faster** than legacy and **~3-5× faster** than standard FP16-SDPA on CPU.
25
- 3. **Plug-and-Play LLM Injection**: S20 decay can be seamlessly injected into open-weights models (GPT-2, OPT, BLOOM) as a post-logit positional mask, dramatically altering their attention footprint without requiring retraining.
26
 
27
  ---
28
 
29
- ## 1. Core Kernel Benchmarks (CPU, 1 Batch, 8 Heads, dim=64)
30
 
31
- The raw PyTorch kernel benchmarking shows that constructing and applying the S20 decay matrix is extraordinarily lightweight.
32
 
33
- ## S20 Attention Kernel Benchmark Results
 
 
 
 
 
 
34
 
35
- **Hardware**: CPU
36
- **Device**: cpu
37
- **Config**: batch=1, heads=8, head_dim=64
38
 
39
- | Seq Len | FP16-SDPA | ALIX-v1 (legacy) | ALIX-v2 (vectorized) | LIA-v2 (vectorized) | Speedup v1→v2 | Overhead vs SDPA |
40
- |---------|-----------|------------------|----------------------|---------------------|---------------|-----------------|
41
- | 64 | 0.30 ms | | 0.22 ms | 0.21 ms | | 0.74× |
42
- | 128 | 1.04 ms | | 0.37 ms | 0.36 ms | | 0.35× |
43
- | 256 | 3.64 ms | | 0.78 ms | 0.73 ms | | 0.21× |
44
- | 512 | 12.89 ms | | 2.11 ms | 2.08 ms | | 0.16× |
45
- | 1024 | 44.10 ms | | 8.38 ms | 8.42 ms | | 0.19× |
46
- | 2048 | 132.41 ms | — | 26.69 ms | 26.64 ms | — | 0.20× |
47
 
48
- > **Methodology**: 3 warmup runs, 20 timed runs. Decay matrix pre-built (not included in per-call timing).
49
- > **Correctness**: Verified by attention row-sum check (tol=1e-4) and NaN/Inf detection.
50
 
51
  ---
52
 
53
- ## 2. Open-Weights Model Injection Benchmarks
54
 
55
- We injected the S20 positional decay into standard open-weights architectures to measure latency overhead and test perplexity stability.
56
 
57
  | Model | Params | Baseline | S20-Injected | Overhead | Avg PPL |
58
  |-------|--------|----------|--------------|----------|---------|
59
- | GPT-2 (124M) | 124.4M | 39.2 ms | 41.6 ms | 1.06× | 175.7 |
60
- | DistilGPT-2 (82M) | 81.9M | 21.6 ms | 21.3 ms | 0.99× | 302.0 |
61
- | OPT-125M | 125.2M | 29.0 ms | 29.4 ms | 1.01× | 199.7 |
62
- | BLOOM-560M | 559.2M | 122.4 ms | 118.0 ms | 0.96× | 139.0 |
63
-
64
-
65
- *(Note: Perplexity is evaluated zero-shot on test prompts without fine-tuning. The baseline vs S20 injected metrics demonstrate the computational overhead of the decay matrix in a full LLM forward pass).*
66
 
67
  ---
68
 
69
- ## Usage (PyTorch)
70
 
71
  ```python
72
  import torch
 
73
  from math import comb
74
 
75
- # 1. Generate S20 sequence
76
- def s20(n: int) -> int:
77
- return sum(comb(n, k)**4 * comb(n + k, k) for k in range(n + 1))
78
-
79
- _S20 = [s20(d) for d in range(18)] # Decays to machine zero by dist=17
80
 
81
- # 2. Vectorized decay matrix construction
82
- def build_s20_decay(seq_len: int, device="cpu"):
83
  base = float(_S20[0])
84
- weights = [base / float(x) if x > 0 else 0.0 for x in _S20] + [0.0]
85
  dv = torch.tensor(weights, dtype=torch.float32, device=device)
86
-
87
  idx = torch.arange(seq_len, device=device)
88
  dist = (idx.unsqueeze(0) - idx.unsqueeze(1)).abs().clamp(max=len(_S20))
89
- return dv[dist]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- # 3. Apply to attention logits
92
- decay = build_s20_decay(L)
93
- attn_weights = torch.softmax(scores + torch.log(decay), dim=-1)
 
 
 
94
  ```
95
 
96
  ## Citation
@@ -101,6 +132,6 @@ attn_weights = torch.softmax(scores + torch.log(decay), dim=-1)
101
  title = {S20-Decay Attention Kernel: Vectorized Integer-Sequence Attention Bias},
102
  year = {2026},
103
  url = {https://huggingface.co/callensxavier/s20-attention-kernel},
104
- note = {Hugging Face Model Card}
105
  }
106
  ```
 
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
 
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}
136
  }
137
  ```