Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -7,90 +7,121 @@ tags:
|
|
| 7 |
- calabi-yau
|
| 8 |
- custom-kernel
|
| 9 |
- pytorch
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# S20-Decay Attention Kernel
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
$$S_{20}(n) = \sum_{k=0}^{n} \binom{n}{k}^4 \binom{n+k}{k}$$
|
| 17 |
|
| 18 |
-
> **
|
| 19 |
-
> **
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
1. **Exact Mathematical Rigidity**: Unlike ALiBi or learned
|
| 24 |
-
2. **O(1) Vectorized Toeplitz
|
| 25 |
-
3. **
|
| 26 |
|
| 27 |
---
|
| 28 |
|
| 29 |
-
##
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
**Device**: cpu
|
| 37 |
-
**Config**: batch=1, heads=8, head_dim=64
|
| 38 |
|
| 39 |
-
| Seq Len |
|
| 40 |
-
|---------|----------
|
| 41 |
-
|
|
| 42 |
-
|
|
| 43 |
-
|
|
| 44 |
-
|
|
| 45 |
-
|
|
| 46 |
-
| 2048 | 132.41 ms | — | 26.69 ms | 26.64 ms | — | 0.20× |
|
| 47 |
|
| 48 |
-
> **
|
| 49 |
-
> **Correctness**: Verified by attention row-sum check (tol=1e-4) and NaN/Inf detection.
|
| 50 |
|
| 51 |
---
|
| 52 |
|
| 53 |
-
##
|
| 54 |
|
| 55 |
-
|
| 56 |
|
| 57 |
| Model | Params | Baseline | S20-Injected | Overhead | Avg PPL |
|
| 58 |
|-------|--------|----------|--------------|----------|---------|
|
| 59 |
-
| GPT-2
|
| 60 |
-
| DistilGPT-2
|
| 61 |
-
| OPT-125M |
|
| 62 |
-
| BLOOM-560M |
|
| 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 |
-
##
|
| 70 |
|
| 71 |
```python
|
| 72 |
import torch
|
|
|
|
| 73 |
from math import comb
|
| 74 |
|
| 75 |
-
# 1.
|
| 76 |
-
def s20(n:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
_S20 = [s20(d) for d in range(18)] # Decays to machine zero by dist=17
|
| 80 |
|
| 81 |
-
# 2. Vectorized
|
| 82 |
-
def
|
| 83 |
base = float(_S20[0])
|
| 84 |
-
weights = [base
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
```
|