math-skill 2.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en-US.md +159 -200
- package/README.md +159 -200
- package/agents/math-critic.en.md +2 -2
- package/agents/math-critic.md +8 -8
- package/commands/ask.md +3 -10
- package/design-patterns/attention/equivariant-attention.en.md +72 -0
- package/design-patterns/attention/equivariant-attention.md +72 -0
- package/design-patterns/attention/geometry-aware-attention.en.md +73 -0
- package/design-patterns/attention/geometry-aware-attention.md +72 -0
- package/design-patterns/attention/information-bottleneck-attention.en.md +72 -0
- package/design-patterns/attention/information-bottleneck-attention.md +72 -0
- package/design-patterns/attention/projection-attention.en.md +68 -0
- package/design-patterns/attention/projection-attention.md +68 -0
- package/design-patterns/attention/spectral-attention.en.md +71 -0
- package/design-patterns/attention/spectral-attention.md +71 -0
- package/design-patterns/compression/leverage-score-selection.en.md +71 -0
- package/design-patterns/compression/leverage-score-selection.md +71 -0
- package/design-patterns/compression/low-rank-kv-cache.en.md +60 -0
- package/design-patterns/compression/low-rank-kv-cache.md +60 -0
- package/design-patterns/compression/spectral-token-pruning.en.md +61 -0
- package/design-patterns/compression/spectral-token-pruning.md +61 -0
- package/design-patterns/compression/topology-preserving-compression.en.md +64 -0
- package/design-patterns/compression/topology-preserving-compression.md +64 -0
- package/design-patterns/loss/constraint-penalty.en.md +67 -0
- package/design-patterns/loss/constraint-penalty.md +78 -0
- package/design-patterns/loss/contrastive-loss.en.md +59 -0
- package/design-patterns/loss/contrastive-loss.md +68 -0
- package/design-patterns/loss/information-bottleneck-loss.en.md +69 -0
- package/design-patterns/loss/information-bottleneck-loss.md +74 -0
- package/design-patterns/loss/orthogonality-loss.en.md +56 -0
- package/design-patterns/loss/orthogonality-loss.md +60 -0
- package/design-patterns/loss/variational-loss.en.md +61 -0
- package/design-patterns/loss/variational-loss.md +71 -0
- package/design-patterns/representation/equivariant-split.en.md +87 -0
- package/design-patterns/representation/equivariant-split.md +89 -0
- package/design-patterns/representation/manifold-representation.en.md +85 -0
- package/design-patterns/representation/manifold-representation.md +87 -0
- package/design-patterns/representation/shared-private-decomposition.en.md +84 -0
- package/design-patterns/representation/shared-private-decomposition.md +86 -0
- package/design-patterns/representation/subspace-alignment.en.md +90 -0
- package/design-patterns/representation/subspace-alignment.md +92 -0
- package/design-patterns/routing/graph-routing.en.md +79 -0
- package/design-patterns/routing/graph-routing.md +81 -0
- package/design-patterns/routing/moe-routing.en.md +85 -0
- package/design-patterns/routing/moe-routing.md +87 -0
- package/design-patterns/routing/optimal-transport-routing.en.md +68 -0
- package/design-patterns/routing/optimal-transport-routing.md +72 -0
- package/design-patterns/routing/spectral-clustering-routing.en.md +88 -0
- package/design-patterns/routing/spectral-clustering-routing.md +90 -0
- package/knowledge-base/differential-geometry/connection.en.md +51 -0
- package/knowledge-base/differential-geometry/connection.md +51 -0
- package/knowledge-base/differential-geometry/curvature.en.md +49 -0
- package/knowledge-base/differential-geometry/curvature.md +49 -0
- package/knowledge-base/differential-geometry/geodesic.en.md +51 -0
- package/knowledge-base/differential-geometry/geodesic.md +51 -0
- package/knowledge-base/differential-geometry/manifold.en.md +47 -0
- package/knowledge-base/differential-geometry/manifold.md +47 -0
- package/knowledge-base/differential-geometry/metric-tensor.en.md +50 -0
- package/knowledge-base/differential-geometry/metric-tensor.md +50 -0
- package/knowledge-base/differential-geometry/tangent-space.en.md +48 -0
- package/knowledge-base/differential-geometry/tangent-space.md +48 -0
- package/knowledge-base/information-geometry/fisher-metric.en.md +56 -0
- package/knowledge-base/information-geometry/fisher-metric.md +56 -0
- package/knowledge-base/information-geometry/natural-gradient.en.md +54 -0
- package/knowledge-base/information-geometry/natural-gradient.md +54 -0
- package/knowledge-base/lie-theory/equivariance.en.md +54 -0
- package/knowledge-base/lie-theory/equivariance.md +54 -0
- package/knowledge-base/lie-theory/group-action.en.md +51 -0
- package/knowledge-base/lie-theory/group-action.md +51 -0
- package/knowledge-base/lie-theory/lie-algebra.en.md +51 -0
- package/knowledge-base/lie-theory/lie-algebra.md +51 -0
- package/knowledge-base/lie-theory/lie-group.en.md +51 -0
- package/knowledge-base/lie-theory/lie-group.md +51 -0
- package/knowledge-base/lie-theory/representation.en.md +52 -0
- package/knowledge-base/lie-theory/representation.md +52 -0
- package/knowledge-base/matrix-analysis/low-rank-approximation.en.md +49 -0
- package/knowledge-base/matrix-analysis/low-rank-approximation.md +49 -0
- package/knowledge-base/matrix-analysis/matrix-perturbation.en.md +49 -0
- package/knowledge-base/matrix-analysis/matrix-perturbation.md +49 -0
- package/knowledge-base/matrix-analysis/positive-semidefinite.en.md +50 -0
- package/knowledge-base/matrix-analysis/positive-semidefinite.md +50 -0
- package/knowledge-base/matrix-analysis/projection.en.md +47 -0
- package/knowledge-base/matrix-analysis/projection.md +47 -0
- package/knowledge-base/matrix-analysis/spectral-decomposition.en.md +48 -0
- package/knowledge-base/matrix-analysis/spectral-decomposition.md +48 -0
- package/knowledge-base/optimization/constrained-optimization.en.md +53 -0
- package/knowledge-base/optimization/constrained-optimization.md +53 -0
- package/knowledge-base/optimization/convex-optimization.en.md +50 -0
- package/knowledge-base/optimization/convex-optimization.md +50 -0
- package/knowledge-base/optimization/lagrangian-duality.en.md +50 -0
- package/knowledge-base/optimization/lagrangian-duality.md +50 -0
- package/knowledge-base/optimization/proximal-method.en.md +52 -0
- package/knowledge-base/optimization/proximal-method.md +52 -0
- package/knowledge-base/optimization/riemannian-optimization.en.md +51 -0
- package/knowledge-base/optimization/riemannian-optimization.md +51 -0
- package/knowledge-base/overview.en.md +29 -218
- package/knowledge-base/overview.md +29 -220
- package/knowledge-base/probability/concentration-inequality.en.md +45 -0
- package/knowledge-base/probability/concentration-inequality.md +45 -0
- package/knowledge-base/probability/entropy.en.md +47 -0
- package/knowledge-base/probability/entropy.md +47 -0
- package/knowledge-base/probability/fisher-information.en.md +48 -0
- package/knowledge-base/probability/fisher-information.md +48 -0
- package/knowledge-base/probability/information-bottleneck.en.md +49 -0
- package/knowledge-base/probability/information-bottleneck.md +49 -0
- package/knowledge-base/probability/kl-divergence.en.md +51 -0
- package/knowledge-base/probability/kl-divergence.md +51 -0
- package/knowledge-base/topology/euler-characteristic.en.md +54 -0
- package/knowledge-base/topology/euler-characteristic.md +54 -0
- package/knowledge-base/topology/fundamental-group.en.md +52 -0
- package/knowledge-base/topology/fundamental-group.md +52 -0
- package/knowledge-base/topology/persistent-homology.en.md +54 -0
- package/knowledge-base/topology/persistent-homology.md +54 -0
- package/lenses/algorithmic.en.md +49 -0
- package/lenses/algorithmic.md +49 -0
- package/lenses/axiomatization.en.md +48 -0
- package/lenses/axiomatization.md +48 -0
- package/lenses/categorical.en.md +48 -0
- package/lenses/categorical.md +48 -0
- package/lenses/causal.en.md +49 -0
- package/lenses/causal.md +49 -0
- package/lenses/duality.en.md +47 -0
- package/lenses/duality.md +47 -0
- package/lenses/game.en.md +50 -0
- package/lenses/game.md +50 -0
- package/lenses/geometric.en.md +53 -0
- package/lenses/geometric.md +53 -0
- package/lenses/local-to-global.en.md +52 -0
- package/lenses/local-to-global.md +52 -0
- package/lenses/perturbation.en.md +51 -0
- package/lenses/perturbation.md +51 -0
- package/lenses/probabilistic.en.md +48 -0
- package/lenses/probabilistic.md +48 -0
- package/lenses/projection.en.md +50 -0
- package/lenses/projection.md +50 -0
- package/lenses/spectral.en.md +52 -0
- package/lenses/spectral.md +52 -0
- package/lenses/symmetry.en.md +49 -0
- package/lenses/symmetry.md +49 -0
- package/lenses/topological.en.md +48 -0
- package/lenses/topological.md +48 -0
- package/lenses/variational.en.md +48 -0
- package/lenses/variational.md +48 -0
- package/package.json +9 -3
- package/references/books/abstract-algebra.md +5 -5
- package/references/books/algebraic-geometry-rising-sea.md +5 -5
- package/references/books/differential-geometry.md +3 -3
- package/references/books/matrix-analysis.md +4 -4
- package/references/books/micro-lie-theory.md +3 -3
- package/references/books/optimization-ml.md +1 -1
- package/references/books/smooth-manifolds.md +3 -3
- package/references/gpu-friendly-math.en.md +8 -8
- package/references/gpu-friendly-math.md +2 -2
- package/references/inspiration.en.md +1 -1
- package/references/inspiration.md +2 -2
- package/skills/math-research-activator/SKILL.en.md +161 -106
- package/skills/math-research-activator/SKILL.md +162 -110
- package/skills/math-research-activator/original-texts.en.md +9 -9
- package/skills/math-research-activator/original-texts.md +11 -11
- package/commands/abstraction.md +0 -11
- package/commands/algorithmic-thinking.md +0 -11
- package/commands/axiomatization.md +0 -11
- package/commands/causal-inference.md +0 -11
- package/commands/discrete-combinatorial.md +0 -11
- package/commands/game-theory.md +0 -11
- package/commands/induction-analogy.md +0 -11
- package/commands/information-theory.md +0 -11
- package/commands/logic-deduction.md +0 -11
- package/commands/modeling.md +0 -11
- package/commands/optimization.md +0 -11
- package/commands/probability-statistics.md +0 -11
- package/commands/symmetry-invariance.md +0 -11
- package/commands/topological-thinking.md +0 -11
- package/commands/transformation.md +0 -11
- package/skills/abstraction/SKILL.en.md +0 -117
- package/skills/abstraction/SKILL.md +0 -121
- package/skills/abstraction/original-texts.en.md +0 -163
- package/skills/abstraction/original-texts.md +0 -175
- package/skills/algorithmic-thinking/SKILL.en.md +0 -132
- package/skills/algorithmic-thinking/SKILL.md +0 -138
- package/skills/algorithmic-thinking/original-texts.en.md +0 -253
- package/skills/algorithmic-thinking/original-texts.md +0 -256
- package/skills/axiomatization/SKILL.en.md +0 -144
- package/skills/axiomatization/SKILL.md +0 -151
- package/skills/axiomatization/original-texts.en.md +0 -154
- package/skills/axiomatization/original-texts.md +0 -160
- package/skills/causal-inference/SKILL.en.md +0 -147
- package/skills/causal-inference/SKILL.md +0 -151
- package/skills/causal-inference/original-texts.en.md +0 -136
- package/skills/causal-inference/original-texts.md +0 -167
- package/skills/discrete-combinatorial/SKILL.en.md +0 -124
- package/skills/discrete-combinatorial/SKILL.md +0 -131
- package/skills/discrete-combinatorial/original-texts.en.md +0 -184
- package/skills/discrete-combinatorial/original-texts.md +0 -185
- package/skills/game-theory/SKILL.en.md +0 -117
- package/skills/game-theory/SKILL.md +0 -123
- package/skills/game-theory/original-texts.en.md +0 -131
- package/skills/game-theory/original-texts.md +0 -131
- package/skills/induction-analogy/SKILL.en.md +0 -145
- package/skills/induction-analogy/SKILL.md +0 -152
- package/skills/induction-analogy/original-texts.en.md +0 -140
- package/skills/induction-analogy/original-texts.md +0 -154
- package/skills/information-theory/SKILL.en.md +0 -134
- package/skills/information-theory/SKILL.md +0 -140
- package/skills/information-theory/original-texts.en.md +0 -127
- package/skills/information-theory/original-texts.md +0 -127
- package/skills/logic-deduction/SKILL.en.md +0 -130
- package/skills/logic-deduction/SKILL.md +0 -135
- package/skills/logic-deduction/original-texts.en.md +0 -160
- package/skills/logic-deduction/original-texts.md +0 -173
- package/skills/modeling/SKILL.en.md +0 -135
- package/skills/modeling/SKILL.md +0 -139
- package/skills/modeling/original-texts.en.md +0 -162
- package/skills/modeling/original-texts.md +0 -165
- package/skills/optimization/SKILL.en.md +0 -129
- package/skills/optimization/SKILL.md +0 -135
- package/skills/optimization/original-texts.en.md +0 -167
- package/skills/optimization/original-texts.md +0 -168
- package/skills/probability-statistics/SKILL.en.md +0 -146
- package/skills/probability-statistics/SKILL.md +0 -151
- package/skills/probability-statistics/original-texts.en.md +0 -191
- package/skills/probability-statistics/original-texts.md +0 -193
- package/skills/symmetry-invariance/SKILL.en.md +0 -135
- package/skills/symmetry-invariance/SKILL.md +0 -139
- package/skills/symmetry-invariance/original-texts.en.md +0 -206
- package/skills/symmetry-invariance/original-texts.md +0 -221
- package/skills/topological-thinking/SKILL.en.md +0 -124
- package/skills/topological-thinking/SKILL.md +0 -128
- package/skills/topological-thinking/original-texts.en.md +0 -134
- package/skills/topological-thinking/original-texts.md +0 -164
- package/skills/transformation/SKILL.en.md +0 -120
- package/skills/transformation/SKILL.md +0 -124
- package/skills/transformation/original-texts.en.md +0 -204
- package/skills/transformation/original-texts.md +0 -216
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Spectral Attention
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as ✅ verified / ⚠️ retrofittable (needs validation) / ❌ infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
|
+
|
|
4
|
+
## Applicable Problems
|
|
5
|
+
When the input signal exhibits **frequency-domain/spectral structure** (periodicity, cyclic symmetry, graph structure), computing attention in the spectral domain rather than the spatial domain can dramatically reduce complexity while exploiting the signal's intrinsic structure. Typical scenarios include: time series forecasting (periodic signals), graph neural networks (graph Laplacian spectral decomposition), positional encoding (frequency-domain interpretation of RoPE/ALiBi), and $O(n \log n)$ acceleration of long-sequence attention.
|
|
6
|
+
|
|
7
|
+
## Mathematical Inspiration
|
|
8
|
+
- Lenses: [duality (frequency-domain transform), symmetry (cyclic/translation invariance)]
|
|
9
|
+
- Knowledge: [`../../knowledge-base/probability/entropy.md` (spectral entropy for measuring signal complexity), `../../knowledge-base/probability/concentration-inequality.md` (frequency-domain concentration inequalities)]
|
|
10
|
+
|
|
11
|
+
## Required Mathematical Knowledge
|
|
12
|
+
- **Discrete Fourier Transform (DFT/FFT)**: $O(n \log n)$ frequency-domain transform and the cyclic convolution theorem
|
|
13
|
+
- **Graph Laplacian Spectral Decomposition**: $L = U \Lambda U^T$, where $U$ is the graph Fourier basis
|
|
14
|
+
- **Irreducible Representations of the Cyclic Group $\mathbb{Z}_n$**: The DFT matrix is precisely the representation matrix of the cyclic group (see `references/books/abstract-algebra.md` Ch.4, Ch.11)
|
|
15
|
+
|
|
16
|
+
## AI Module Form
|
|
17
|
+
|
|
18
|
+
**Core Idea**: Transform attention from the spatial-domain $Q K^T$ into diagonal/sparse operations in the spectral domain:
|
|
19
|
+
|
|
20
|
+
**Scheme A: FFT-Accelerated Attention (Time Series)**:
|
|
21
|
+
```python
|
|
22
|
+
# Express cyclic convolution as element-wise multiplication in the frequency domain
|
|
23
|
+
Q_hat = fft(Q, dim=seq) # (n, d) -> (n, d) frequency domain
|
|
24
|
+
K_hat = fft(K, dim=seq)
|
|
25
|
+
# Attention ~ frequency-domain filtering: independent weighting per frequency component
|
|
26
|
+
attn_hat = Q_hat * conj(K_hat) # element-wise multiply = cyclic convolution
|
|
27
|
+
attn = ifft(attn_hat, dim=seq)
|
|
28
|
+
# Complexity: O(n log n * d) vs. standard O(n^2 * d)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Scheme B: Graph Spectral Attention (GNN)**:
|
|
32
|
+
```python
|
|
33
|
+
# Precompute graph Laplacian spectral decomposition L = U Lambda U^T (offline)
|
|
34
|
+
U = eigenvectors(L) # (n, k), take top-k low-frequency eigenvectors
|
|
35
|
+
# Spectral-domain attention: compute in the low-frequency subspace
|
|
36
|
+
Q_spec = U^T @ Q # (k, d) project to spectral domain
|
|
37
|
+
K_spec = U^T @ K
|
|
38
|
+
scores = (Q_spec @ W_q) @ (K_spec @ W_k).T / sqrt(d)
|
|
39
|
+
attn_spec = softmax(scores) @ (U^T @ V)
|
|
40
|
+
output = U @ attn_spec # back-project to spatial domain
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Scheme C: Frequency-Adaptive Attention Weights**:
|
|
44
|
+
```python
|
|
45
|
+
freq_weights = learnable_parameter(num_freq_bands) # learnable spectral weights
|
|
46
|
+
Q_hat, K_hat = fft(Q), fft(K)
|
|
47
|
+
scores_freq = freq_weights.unsqueeze(-1) * (Q_hat * conj(K_hat))
|
|
48
|
+
attn = ifft(scores_freq)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Implementable Architectures
|
|
52
|
+
- **Spectral Transformer**: Replace $O(n^2)$ attention with FFT, suitable for periodic sequence data (meteorological, financial, audio)
|
|
53
|
+
- **Graph Spectral Attention**: Leverage the first $k$ eigenvectors of the graph Laplacian for low-dimensional attention, suitable for large-scale graphs ($n > 10^5$)
|
|
54
|
+
- **Frequency-Aware Positional Encoding**: The essence of RoPE is the unitary representation of the cyclic group $\mathbb{Z}$ (see Abstract Algebra Ch.4), generalizable to other groups
|
|
55
|
+
|
|
56
|
+
## GPU Feasibility
|
|
57
|
+
- **Dimension 1 Tensorization**: FFT and matrix multiplication are both standard tensor operations
|
|
58
|
+
- **Dimension 2 GEMM-mappability**: Spectral projection $U^T Q$ is a standard GEMM; although FFT is not GEMM, highly optimized cuFFT implementations are available
|
|
59
|
+
- **Dimension 3 Complexity**: FFT attention $O(n \log n \cdot d)$, far superior to $O(n^2 d)$
|
|
60
|
+
- **Dimension 4 Memory**: Frequency-domain representation introduces no extra dimensions; spectral projection can reduce to $k \ll n$ dimensions
|
|
61
|
+
- **Dimension 5 Low Precision**: Complex-valued FFT suffers precision loss under fp16; fp32 or real-valued FFT (RFFT) is required
|
|
62
|
+
- **Dimension 6 Parallelism**: FFT can be parallelized across batch/head; cuFFT supports multi-stream execution
|
|
63
|
+
- **Dimension 7 Sparsity**: High-frequency components can be truncated in the spectral domain (structured sparsity), retaining only top-k frequencies
|
|
64
|
+
- **Dimension 8 Operator Fusion**: Fusing FFT with attention requires custom kernels; no ready-made fusion exists in standard libraries
|
|
65
|
+
|
|
66
|
+
## Paper Phrasing
|
|
67
|
+
"We propose a spectral-domain attention mechanism that transforms attention computation into the Fourier/Laplacian spectral domain, leveraging the cyclic convolution theorem to reduce sequence attention complexity from $O(n^2)$ to $O(n \log n)$ while preserving the ability to model dependencies at multiple scales through frequency-adaptive weights."
|
|
68
|
+
|
|
69
|
+
## Risks
|
|
70
|
+
- **Violation of Periodicity Assumption**: FFT implicitly assumes periodic boundary conditions, causing spectral leakage for non-periodic signals such as natural language. Windowing functions or zero-padding are needed.
|
|
71
|
+
- **Graph Laplacian Precomputation Cost**: Eigendecomposition $O(n^3)$ is infeasible for large-scale graphs, requiring approximations (Nystrom/Lanczos), and dynamic graphs necessitate recomputation.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# 谱注意力 / Spectral Attention
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
|
+
|
|
4
|
+
## 适用问题
|
|
5
|
+
当输入信号具有**频域/谱结构**(周期性、循环对称性、图结构)时,在谱域而非空域计算注意力可大幅降低复杂度并利用信号的内在结构。典型场景:时间序列预测(周期性信号)、图神经网络(图 Laplacian 谱分解)、位置编码(RoPE/ALiBi 的频率解释)、长序列注意力的 $O(n \log n)$ 加速。
|
|
6
|
+
|
|
7
|
+
## 数学思想来源
|
|
8
|
+
- 透镜:[duality(对偶透镜 — 频域变换), symmetry(对称透镜 — 循环/平移不变性)]
|
|
9
|
+
- 知识:[`../../knowledge-base/probability/entropy.md`(谱熵度量信号复杂度), `../../knowledge-base/probability/concentration-inequality.md`(频域浓度不等式)]
|
|
10
|
+
|
|
11
|
+
## 需要的数学知识
|
|
12
|
+
- **离散傅里叶变换 DFT / FFT**:$O(n \log n)$ 频域变换,循环卷积定理
|
|
13
|
+
- **图 Laplacian 谱分解**:$L = U \Lambda U^T$,$U$ 为图 Fourier 基
|
|
14
|
+
- **循环群 $\mathbb{Z}_n$ 的不可约表示**:DFT 矩阵即循环群的表示矩阵(参见 `references/books/abstract-algebra.md` Ch.4, Ch.11)
|
|
15
|
+
|
|
16
|
+
## AI 模块形式
|
|
17
|
+
|
|
18
|
+
**核心思路**:将注意力从空域的 $Q K^T$ 转换到谱域的对角/稀疏运算:
|
|
19
|
+
|
|
20
|
+
**方案 A:FFT 加速注意力(时间序列)**:
|
|
21
|
+
```python
|
|
22
|
+
# 将循环卷积写成谱域逐元素乘
|
|
23
|
+
Q_hat = fft(Q, dim=seq) # (n, d) -> (n, d) 频域
|
|
24
|
+
K_hat = fft(K, dim=seq)
|
|
25
|
+
# 注意力 ≈ 频域滤波:每个频率分量独立加权
|
|
26
|
+
attn_hat = Q_hat * conj(K_hat) # 逐元素乘 = 循环卷积
|
|
27
|
+
attn = ifft(attn_hat, dim=seq)
|
|
28
|
+
# 复杂度:O(n log n * d) vs 标准 O(n^2 * d)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**方案 B:图谱注意力(GNN)**:
|
|
32
|
+
```python
|
|
33
|
+
# 预计算图 Laplacian 谱分解 L = U Λ U^T(离线)
|
|
34
|
+
U = eigenvectors(L) # (n, k), 取 top-k 低频特征向量
|
|
35
|
+
# 谱域注意力:在低频子空间中计算
|
|
36
|
+
Q_spec = U^T @ Q # (k, d) 投影到谱域
|
|
37
|
+
K_spec = U^T @ K
|
|
38
|
+
scores = (Q_spec @ W_q) @ (K_spec @ W_k).T / sqrt(d)
|
|
39
|
+
attn_spec = softmax(scores) @ (U^T @ V)
|
|
40
|
+
output = U @ attn_spec # 反投影回空域
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**方案 C:频率自适应注意力权重**:
|
|
44
|
+
```python
|
|
45
|
+
freq_weights = learnable_parameter(num_freq_bands) # 可学习频谱权重
|
|
46
|
+
Q_hat, K_hat = fft(Q), fft(K)
|
|
47
|
+
scores_freq = freq_weights.unsqueeze(-1) * (Q_hat * conj(K_hat))
|
|
48
|
+
attn = ifft(scores_freq)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 可实现结构
|
|
52
|
+
- **Spectral Transformer**:用 FFT 替代 $O(n^2)$ 注意力,适合周期性序列数据(气象、金融、音频)
|
|
53
|
+
- **Graph Spectral Attention**:利用图 Laplacian 的前 $k$ 个特征向量做低维注意力,适合大规模图($n > 10^5$)
|
|
54
|
+
- **频率感知位置编码**:RoPE 的本质即循环群 $\mathbb{Z}$ 的酉表示(参见抽象代数 Ch.4),可推广到其他群
|
|
55
|
+
|
|
56
|
+
## GPU 可行性
|
|
57
|
+
- **维度 1 张量化 ✅**:FFT 和矩阵乘均为标准张量运算
|
|
58
|
+
- **维度 2 GEMM 可映射 ✅**:谱投影 $U^T Q$ 为标准 GEMM;FFT 虽非 GEMM 但有高度优化的 cuFFT 实现
|
|
59
|
+
- **维度 3 复杂度 ✅**:FFT 注意力 $O(n \log n \cdot d)$,远优于 $O(n^2 d)$
|
|
60
|
+
- **维度 4 显存 ✅**:频域表示不增加额外维度,谱投影可降低到 $k \ll n$ 维
|
|
61
|
+
- **维度 5 低精度 ⚠️**:复数 FFT 在 fp16 下精度损失,需 fp32 或使用实数 FFT (RFFT)
|
|
62
|
+
- **维度 6 并行 ✅**:FFT 可跨 batch/head 并行,cuFFT 支持多流
|
|
63
|
+
- **维度 7 稀疏 ✅**:谱域中高频分量可截断(结构化稀疏),只保留 top-k 频率
|
|
64
|
+
- **维度 8 算子融合 ⚠️**:FFT 与 attention 融合需自定义 kernel,标准库无现成融合
|
|
65
|
+
|
|
66
|
+
## 论文表述方式
|
|
67
|
+
"我们提出谱域注意力机制,通过将注意力计算转换到 Fourier/Laplacian 谱域,利用循环卷积定理将序列注意力复杂度从 $O(n^2)$ 降至 $O(n \log n)$,同时通过频率自适应权重保留对不同尺度依赖关系的建模能力。"
|
|
68
|
+
|
|
69
|
+
## 风险
|
|
70
|
+
- **非周期性假设违反**:FFT 隐式假设周期性边界条件,对自然语言等非周期信号产生谱泄漏,需加窗函数或零填充。
|
|
71
|
+
- **图 Laplacian 预计算成本**:大规模图的特征分解 $O(n^3)$ 不可行,需近似(Nyström/Lanczos),且动态图需重算。
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Leverage Score Selection
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as ✅ verified / ⚠️ retrofittable (needs validation) / ❌ infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
|
+
|
|
4
|
+
## Target Problem
|
|
5
|
+
Use when selecting the most representative rows / columns / tokens from a large-scale matrix while guaranteeing the precision of downstream linear algebra operations: KV-Cache token selection, data coreset construction, Nystrom landmark sampling, distributed gradient compression. Core objective: **sample based on statistical leverage scores derived from subspace projections, with probabilistic guarantees of approximating full-computation accuracy**.
|
|
6
|
+
|
|
7
|
+
## Mathematical Foundations
|
|
8
|
+
- Lenses: lenses/spectral.md (leverage scores = projection energy of row vectors onto the principal subspace), lenses/probabilistic.md (probabilistic sampling and concentration inequality guarantees), lenses/algorithmic.md (complexity--accuracy trade-offs of randomized algorithms)
|
|
9
|
+
- Knowledge: knowledge-base/matrix-analysis/low-rank-approximation.md (randomized SVD, nuclear norm), knowledge-base/matrix-analysis/projection.md (diagonal entries of the projection matrix = leverage scores), knowledge-base/probability/concentration-inequality.md (Bernstein matrix concentration bound)
|
|
10
|
+
|
|
11
|
+
## Required Mathematical Background
|
|
12
|
+
- **Statistical Leverage Scores**: $\ell_i = \|(V_k V_k^T)_i\|^2 = (V_k V_k^T)_{ii}$, the projection energy of the $i$-th row onto the rank-$k$ subspace; $\sum_i \ell_i = k$
|
|
13
|
+
- **Leverage Score Sampling Guarantee**: sampling $s = O(k \log k / \epsilon^2)$ rows with probabilities $p_i = \ell_i / k$ yields a $(1+\epsilon)$ approximation to full least squares (Drineas--Mahoney)
|
|
14
|
+
- **Bernstein Matrix Bound**: after sampling, $\|\hat{A}^T \hat{A} - A^T A\|_2 \leq \epsilon \|A\|_F^2$ with probability $\geq 1-\delta$
|
|
15
|
+
- **Fast Approximation**: $\tilde{\ell}_i = \|(A\Omega)_i\|^2$ ($\Omega$ random Gaussian), avoids full SVD, $O(Ndk)$
|
|
16
|
+
- **DPP Extension**: Determinantal Point Process $P(S) \propto \det(L_S)$ adds a diversity guarantee on top of leverage scores
|
|
17
|
+
|
|
18
|
+
## AI Module Specification
|
|
19
|
+
```
|
|
20
|
+
Module: LeverageScoreSelector
|
|
21
|
+
Input: A ∈ R^{N×d} Parameters: sample size s << N, rank parameter k
|
|
22
|
+
|
|
23
|
+
Method 1 - Random projection leverage scores (online / large-scale):
|
|
24
|
+
Omega = randn(d, k+p) // random matrix, p=5 oversampling
|
|
25
|
+
Q = qr(A @ Omega)[0] // N×(k+p), GEMM + QR
|
|
26
|
+
leverage = sum(Q ** 2, dim=1) // N-dim, row-wise sum of squares
|
|
27
|
+
indices = topk(leverage, s) // deterministic top-s selection
|
|
28
|
+
A_selected = A[indices]
|
|
29
|
+
|
|
30
|
+
Method 2 - Exact leverage scores (offline / small matrices):
|
|
31
|
+
U_k = svd(A)[:k][0] // truncated SVD left singular vectors
|
|
32
|
+
leverage = sum(U_k ** 2, dim=1) // exact rank-k leverage scores
|
|
33
|
+
probs = leverage / leverage.sum()
|
|
34
|
+
indices = multinomial_sample(N, s, probs) // probabilistic sampling + reweighting
|
|
35
|
+
weights = 1 / sqrt(s * probs[indices])
|
|
36
|
+
|
|
37
|
+
Method 3 - KV-Cache sliding-window eviction:
|
|
38
|
+
Every M steps update Q = qr(K_cache @ Omega)[0]
|
|
39
|
+
leverage = sum(Q ** 2, dim=1)
|
|
40
|
+
Evict tokens with lowest leverage (least contribution to the subspace)
|
|
41
|
+
|
|
42
|
+
Method 4 - DPP greedy diverse selection:
|
|
43
|
+
scores = leverage.clone(); L = A @ A^T // PSD kernel matrix
|
|
44
|
+
for _ in range(s):
|
|
45
|
+
idx = argmax(scores); selected.append(idx)
|
|
46
|
+
scores -= α * |L[:, idx]| // penalize neighbors of already selected tokens
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Implementable Architectures
|
|
50
|
+
- **Random projection leverage layer**: 1 GEMM + 1 QR yields approximate leverage scores in $O(Ndk)$
|
|
51
|
+
- **KV-Cache eviction policy**: evict by leverage score ranking, with stronger theoretical guarantees than attention-score-based eviction
|
|
52
|
+
- **Coreset constructor**: leverage score sampling + importance reweighting, ensuring empirical risk approximates the full-data risk
|
|
53
|
+
- **DPP greedy extension**: leverage scores + exclusion penalty, balancing importance and diversity
|
|
54
|
+
|
|
55
|
+
## GPU Feasibility
|
|
56
|
+
- Tensorization / GEMM: $A\Omega$ is a GEMM; QR via cuSOLVER; leverage scores = elementwise row-wise sum of squares
|
|
57
|
+
- Complexity: $O(Ndk)$ is far superior to $O(Nd^2)$ full SVD; top-s selection in $O(N \log s)$
|
|
58
|
+
- Memory: $\Omega$ is only $d \times k$ (KB-scale); $Q$ is the same size as $A$ but can be computed in batches
|
|
59
|
+
- Low precision: QR recommended in fp32 (small matrix with $k+p$ columns, negligible overhead); leverage scores can be cast back to bf16
|
|
60
|
+
- Parallelism: the $A\Omega$ GEMM is highly parallelizable; top-s can use parallel radix sort
|
|
61
|
+
- Operator fusion: $A\Omega$ + QR + row-norm² can be fused to avoid materializing intermediate matrices
|
|
62
|
+
|
|
63
|
+
## Paper-Worthy Formulation
|
|
64
|
+
"We adopt statistical leverage scores as the importance metric for token selection: approximating rank-$k$ subspace leverage scores via random projection in $O(Ndk)$, with the Drineas--Mahoney theory guaranteeing that $O(k \log k / \epsilon^2)$ samples suffice for a $(1+\epsilon)$ approximation of the full subspace."
|
|
65
|
+
|
|
66
|
+
## Risks
|
|
67
|
+
- **Rank parameter $k$ selection**: leverage scores depend on the rank-$k$ subspace; an incorrect $k$ leads to biased sampling -- the effective rank must be diagnosed first
|
|
68
|
+
- **Sampling variance**: probabilistic sampling introduces variance; low-probability rows occasionally selected produce large weight noise -- deterministic top-s can be used as an alternative
|
|
69
|
+
- **Leverage scores vs. semantic importance mismatch**: the metric measures subspace contribution, which does not necessarily reflect semantics; it can be combined with attention scores via weighted fusion
|
|
70
|
+
- **DPP greedy suboptimality**: exact DPP sampling is $O(N^3)$; the greedy approximation may miss the globally optimal subset
|
|
71
|
+
- **Stale scores under dynamic data**: in streaming scenarios leverage scores drift as data evolves, requiring periodic recomputation or incremental updates
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Leverage Score Selection(杠杆分数选择)
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
|
+
|
|
4
|
+
## 适用问题
|
|
5
|
+
当需要从大规模矩阵中选取最有代表性的行/列/token,且需保证下游线性代数运算精度时使用:KV-Cache token 选择、数据 coreset 构建、Nyström landmark 采样、分布式梯度压缩。核心诉求:**基于子空间投影的统计杠杆分数做采样,以概率保证逼近全量计算精度**。
|
|
6
|
+
|
|
7
|
+
## 数学思想来源
|
|
8
|
+
- 透镜:lenses/spectral.md(杠杆分数 = 行向量在主子空间上的投影能量)、lenses/probabilistic.md(概率采样与浓度不等式保证)、lenses/algorithmic.md(随机化算法的复杂度与精度权衡)
|
|
9
|
+
- 知识:knowledge-base/matrix-analysis/low-rank-approximation.md(随机化 SVD、核范数)、knowledge-base/matrix-analysis/projection.md(投影矩阵对角元 = 杠杆分数)、knowledge-base/probability/concentration-inequality.md(Bernstein 矩阵浓度界)
|
|
10
|
+
|
|
11
|
+
## 需要的数学知识
|
|
12
|
+
- **统计杠杆分数**:$\ell_i = \|(V_k V_k^T)_i\|^2 = (V_k V_k^T)_{ii}$,第 $i$ 行在 rank-$k$ 子空间的投影能量;$\sum_i \ell_i = k$
|
|
13
|
+
- **杠杆分数采样保证**:以 $p_i = \ell_i / k$ 采样 $s = O(k \log k / \epsilon^2)$ 行,$(1+\epsilon)$ 近似全量最小二乘(Drineas-Mahoney)
|
|
14
|
+
- **Bernstein 矩阵界**:采样后 $\|\hat{A}^T \hat{A} - A^T A\|_2 \leq \epsilon \|A\|_F^2$,概率 $\geq 1-\delta$
|
|
15
|
+
- **快速近似**:$\tilde{\ell}_i = \|(A\Omega)_i\|^2$($\Omega$ 随机高斯),避免完整 SVD,$O(Ndk)$
|
|
16
|
+
- **DPP 扩展**:行列式点过程 $P(S) \propto \det(L_S)$ 在杠杆分数基础上增加多样性保证
|
|
17
|
+
|
|
18
|
+
## AI 模块形式
|
|
19
|
+
```
|
|
20
|
+
模块:LeverageScoreSelector
|
|
21
|
+
输入:A ∈ R^{N×d} 参数:采样数 s << N,秩参数 k
|
|
22
|
+
|
|
23
|
+
方法1 - 随机投影杠杆分数(在线/大规模):
|
|
24
|
+
Omega = randn(d, k+p) // 随机矩阵,p=5 过采样
|
|
25
|
+
Q = qr(A @ Omega)[0] // N×(k+p),GEMM + QR
|
|
26
|
+
leverage = sum(Q ** 2, dim=1) // N 维,逐行平方和
|
|
27
|
+
indices = topk(leverage, s) // 确定性选 top-s
|
|
28
|
+
A_selected = A[indices]
|
|
29
|
+
|
|
30
|
+
方法2 - 精确杠杆分数(离线/小矩阵):
|
|
31
|
+
U_k = svd(A)[:k][0] // 截断 SVD 左奇异向量
|
|
32
|
+
leverage = sum(U_k ** 2, dim=1) // 精确 rank-k 杠杆分数
|
|
33
|
+
probs = leverage / leverage.sum()
|
|
34
|
+
indices = multinomial_sample(N, s, probs) // 概率采样 + 重加权
|
|
35
|
+
weights = 1 / sqrt(s * probs[indices])
|
|
36
|
+
|
|
37
|
+
方法3 - KV-Cache 滑动窗口驱逐:
|
|
38
|
+
每 M 步更新 Q = qr(K_cache @ Omega)[0]
|
|
39
|
+
leverage = sum(Q ** 2, dim=1)
|
|
40
|
+
驱逐 leverage 最低的 token(对子空间贡献最小)
|
|
41
|
+
|
|
42
|
+
方法4 - DPP 贪心多样化选择:
|
|
43
|
+
scores = leverage.clone(); L = A @ A^T // PSD 核矩阵
|
|
44
|
+
for _ in range(s):
|
|
45
|
+
idx = argmax(scores); selected.append(idx)
|
|
46
|
+
scores -= α * |L[:, idx]| // 惩罚已选 token 的邻居
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 可实现结构
|
|
50
|
+
- **随机投影杠杆层**:1 次 GEMM + 1 次 QR 即得近似杠杆分数,$O(Ndk)$
|
|
51
|
+
- **KV-Cache 驱逐策略**:按杠杆分数排序驱逐,比 attention score 驱逐更有理论保证
|
|
52
|
+
- **Coreset 构建器**:杠杆分数采样 + 重要性重加权,保证经验风险逼近全量
|
|
53
|
+
- **DPP 贪心扩展**:杠杆分数 + 互斥惩罚,兼顾重要性与多样性
|
|
54
|
+
|
|
55
|
+
## GPU 可行性
|
|
56
|
+
- 张量化/GEMM:$A\Omega$ 为 GEMM;QR 有 cuSOLVER;杠杆分数 = elementwise 逐行平方和
|
|
57
|
+
- 复杂度:$O(Ndk)$ 远优于 $O(Nd^2)$ 完整 SVD;top-s 选择 $O(N \log s)$
|
|
58
|
+
- 显存:$\Omega$ 仅 $d \times k$(KB 级);$Q$ 与 $A$ 同尺寸但可分批计算
|
|
59
|
+
- 低精度:QR 建议 fp32(小矩阵 $k+p$ 列,开销可忽略);杠杆分数可回 bf16
|
|
60
|
+
- 并行:$A\Omega$ 的 GEMM 高度并行;top-s 可用 radix sort 并行
|
|
61
|
+
- 算子融合:$A\Omega$ + QR + row-norm² 可融合避免物化中间矩阵
|
|
62
|
+
|
|
63
|
+
## 论文表述方式
|
|
64
|
+
"采用统计杠杆分数作为 token 选择的重要性度量:通过随机投影在 $O(Ndk)$ 内近似 rank-$k$ 子空间杠杆分数,Drineas-Mahoney 理论保证 $O(k \log k / \epsilon^2)$ 次采样即可 $(1+\epsilon)$ 近似全量子空间。"
|
|
65
|
+
|
|
66
|
+
## 风险
|
|
67
|
+
- **秩参数 $k$ 选择**:杠杆分数依赖 rank-$k$ 子空间,$k$ 选错导致采样偏差;需先诊断有效秩
|
|
68
|
+
- **采样方差**:概率采样引入方差,低概率行偶尔被选中产生大权重噪声;可改用确定性 top-s
|
|
69
|
+
- **杠杆分数 vs. 语义重要性不一致**:度量的是子空间贡献,不一定反映语义;可与 attention score 加权融合
|
|
70
|
+
- **DPP 贪心次优**:精确 DPP 采样 $O(N^3)$,贪心近似可能漏掉全局最优子集
|
|
71
|
+
- **动态数据过时**:streaming 场景下杠杆分数随数据漂移,需定期重算或增量更新
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Low-Rank KV-Cache
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as ✅ verified / ⚠️ retrofittable (needs validation) / ❌ infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
|
+
|
|
4
|
+
## Target Problem
|
|
5
|
+
Use when KV-Cache memory consumption becomes the bottleneck during LLM inference: long-context inference ($L > 8K$), multi-turn conversation accumulation, edge deployment, speculative decoding / beam search. Core objective: **compress the KV-Cache from $O(Ld)$ to $O(kd)$ with $k \ll L$, under controlled information loss**.
|
|
6
|
+
|
|
7
|
+
## Mathematical Foundations
|
|
8
|
+
- Lenses: lenses/spectral.md (spectral component identification and truncation), lenses/variational.md (Pareto trade-off between compression ratio and reconstruction error), lenses/duality.md (nuclear norm--spectral norm duality)
|
|
9
|
+
- Knowledge: knowledge-base/matrix-analysis/low-rank-approximation.md (Eckart--Young optimal approximation, randomized SVD), knowledge-base/matrix-analysis/projection.md (orthogonal projection onto principal subspace), knowledge-base/matrix-analysis/matrix-perturbation.md (Weyl perturbation bound)
|
|
10
|
+
|
|
11
|
+
## Required Mathematical Background
|
|
12
|
+
- **Eckart--Young--Mirsky Theorem**: $A_k = U_k \Sigma_k V_k^H$, $\|A - A_k\|_F = \sqrt{\sum_{i>k} \sigma_i^2}$; truncated SVD = optimal rank-$k$ approximation
|
|
13
|
+
- **Randomized SVD**: $Y = A\Omega$ ($\Omega$ random Gaussian), $Y = QR$, $B = Q^H A$, perform SVD on $B$; complexity $O(Ldk)$, dominated by matrix multiplications
|
|
14
|
+
- **Weyl Perturbation Bound**: $\|A - A_k\|_2 = \sigma_{k+1}$; maximum deviation of attention scores after compression $\leq \sigma_{k+1}$
|
|
15
|
+
- **Effective Rank**: $r_{\text{eff}}(K) = \|K\|_F^2 / \|K\|_2^2$, guides adaptive selection of $k$
|
|
16
|
+
|
|
17
|
+
## AI Module Specification
|
|
18
|
+
```
|
|
19
|
+
Module: LowRankKVCompressor
|
|
20
|
+
Input: K ∈ R^{L×d}, V ∈ R^{L×d} Parameters: target rank k << L, update frequency M
|
|
21
|
+
|
|
22
|
+
Method 1 - Offline periodic compression (most practical):
|
|
23
|
+
Omega = randn(d, k+p) // random projection, p=5 oversampling
|
|
24
|
+
Q_k = qr(K @ Omega)[0] // L×(k+p) orthonormal basis (GEMM + QR)
|
|
25
|
+
B_k = Q_k^T @ K // (k+p)×d small matrix GEMM
|
|
26
|
+
U_r, S_r, Vt_r = svd(B_k) // small matrix SVD
|
|
27
|
+
K_comp = S_r[:k] * Vt_r[:k, :] // k×d compressed Key
|
|
28
|
+
// Attention: softmax(Q @ K_comp^T / √d) @ V_comp, sequence dimension L → k
|
|
29
|
+
|
|
30
|
+
Method 2 - Streaming incremental compression (low latency):
|
|
31
|
+
Maintain basis (U_basis ∈ R^{k×d}), on new token arrival:
|
|
32
|
+
residual p = k_new - U_basis^T @ (U_basis @ k_new)
|
|
33
|
+
if ‖p‖ > τ: brand_update + truncate_to_rank(k) // expand basis
|
|
34
|
+
else: coeff = U_basis @ k_new // project onto existing basis
|
|
35
|
+
|
|
36
|
+
Method 3 - Layer-wise adaptive: allocate per-layer, per-head k based on effective rank r_eff[l,h]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Implementable Architectures
|
|
40
|
+
- **Periodic compression layer**: trigger randomized SVD every $M=64$ steps, $L \times d \to k \times d$
|
|
41
|
+
- **Double buffering**: compressed basis + recent $w$ raw tokens, balancing accuracy and compression ratio
|
|
42
|
+
- **Shared basis**: multi-head sharing of the Key column-space basis, each head stores only coefficients
|
|
43
|
+
- **Quantized basis**: further INT8/FP8 quantization after compression, achieving dual compression
|
|
44
|
+
|
|
45
|
+
## GPU Feasibility
|
|
46
|
+
- Tensorization / GEMM: randomized SVD = 3 GEMMs + 1 small SVD, maps perfectly onto Tensor Cores
|
|
47
|
+
- Complexity: $O(Ldk)$ is far superior to $O(Ld^2)$ full SVD; overhead negligible for $k \sim 256$
|
|
48
|
+
- Memory: KV-Cache reduced from $O(Ld)$ to $O(kd)$; $k/L \sim 1/16$ yields 16x compression
|
|
49
|
+
- Low precision: SVD recommended in fp32 (acceptable for small matrices); compressed KV can be stored back in bf16
|
|
50
|
+
- Parallelism: compression across layers / heads is fully independent; incremental update $O(kd)$ with very low latency
|
|
51
|
+
- Operator fusion: $K\Omega$ + QR can be partially fused; the QK^T dimension in attention is already reduced
|
|
52
|
+
|
|
53
|
+
## Paper-Worthy Formulation
|
|
54
|
+
"Building on the Eckart--Young--Mirsky theorem, we employ randomized SVD to project the KV-Cache onto the optimal rank-$k$ subspace, achieving $L/k$-fold memory compression at $O(Ldk)$ complexity, with the Weyl perturbation bound guaranteeing that attention score deviations do not exceed $\sigma_{k+1}$."
|
|
55
|
+
|
|
56
|
+
## Risks
|
|
57
|
+
- **Improper rank selection**: $k$ too small causes $\sigma_{k+1}$ to be non-negligible, degrading long-range recall; singular value decay curves must be monitored
|
|
58
|
+
- **Incremental SVD error accumulation**: repeated Brand updates drift away from the true SVD, requiring periodic re-compression for correction
|
|
59
|
+
- **Positional encoding distortion**: RoPE is coupled with Keys, and compression may disrupt relative positional information; separate handling or re-injection is needed
|
|
60
|
+
- **Double-buffer seam**: attention score scales differ between the compressed region and the raw region, requiring unified normalization
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Low-Rank KV-Cache(低秩 KV 缓存压缩)
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
|
+
|
|
4
|
+
## 适用问题
|
|
5
|
+
当 LLM 推理的 KV-Cache 显存占用成为瓶颈时使用:长上下文推理($L > 8K$)、多轮对话累积、边缘部署、投机解码/束搜索。核心诉求:**以可控信息损失将 KV-Cache 从 $O(Ld)$ 压缩到 $O(kd)$,$k \ll L$**。
|
|
6
|
+
|
|
7
|
+
## 数学思想来源
|
|
8
|
+
- 透镜:lenses/spectral.md(谱分量识别与截断)、lenses/variational.md(压缩率与重构误差的帕累托取舍)、lenses/duality.md(核范数-谱范数对偶)
|
|
9
|
+
- 知识:knowledge-base/matrix-analysis/low-rank-approximation.md(Eckart-Young 最优逼近、随机化 SVD)、knowledge-base/matrix-analysis/projection.md(正交投影到主子空间)、knowledge-base/matrix-analysis/matrix-perturbation.md(Weyl 扰动界)
|
|
10
|
+
|
|
11
|
+
## 需要的数学知识
|
|
12
|
+
- **Eckart-Young-Mirsky 定理**:$A_k = U_k \Sigma_k V_k^H$,$\|A - A_k\|_F = \sqrt{\sum_{i>k} \sigma_i^2}$,截断 SVD = 最优秩-$k$ 逼近
|
|
13
|
+
- **随机化 SVD**:$Y = A\Omega$($\Omega$ 随机高斯),$Y = QR$,$B = Q^H A$,对 $B$ 做 SVD;复杂度 $O(Ldk)$,核心全是 matmul
|
|
14
|
+
- **Weyl 扰动界**:$\|A - A_k\|_2 = \sigma_{k+1}$,压缩后 attention score 最大偏差 $\leq \sigma_{k+1}$
|
|
15
|
+
- **有效秩**:$r_{\text{eff}}(K) = \|K\|_F^2 / \|K\|_2^2$,指导 $k$ 的自适应选取
|
|
16
|
+
|
|
17
|
+
## AI 模块形式
|
|
18
|
+
```
|
|
19
|
+
模块:LowRankKVCompressor
|
|
20
|
+
输入:K ∈ R^{L×d}, V ∈ R^{L×d} 参数:目标秩 k << L,更新频率 M
|
|
21
|
+
|
|
22
|
+
方法1 - 离线周期压缩(最实用):
|
|
23
|
+
Omega = randn(d, k+p) // 随机投影,p=5 过采样
|
|
24
|
+
Q_k = qr(K @ Omega)[0] // L×(k+p) 正交基底(GEMM + QR)
|
|
25
|
+
B_k = Q_k^T @ K // (k+p)×d 小矩阵 GEMM
|
|
26
|
+
U_r, S_r, Vt_r = svd(B_k) // 小矩阵 SVD
|
|
27
|
+
K_comp = S_r[:k] * Vt_r[:k, :] // k×d 压缩 Key
|
|
28
|
+
// Attention: softmax(Q @ K_comp^T / √d) @ V_comp,序列维度 L → k
|
|
29
|
+
|
|
30
|
+
方法2 - 流式增量压缩(低延迟):
|
|
31
|
+
维护基底 (U_basis ∈ R^{k×d}),新 token 到来:
|
|
32
|
+
残差 p = k_new - U_basis^T @ (U_basis @ k_new)
|
|
33
|
+
if ‖p‖ > τ: brand_update + truncate_to_rank(k) // 扩展基底
|
|
34
|
+
else: coeff = U_basis @ k_new // 投影到现有基底
|
|
35
|
+
|
|
36
|
+
方法3 - 分层自适应:按有效秩 r_eff[l,h] 分配各层各头的 k
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 可实现结构
|
|
40
|
+
- **周期压缩层**:每 M=64 步触发随机化 SVD,$L \times d \to k \times d$
|
|
41
|
+
- **双缓冲**:压缩基底 + 近期 $w$ 个原始 token,兼顾精度与压缩率
|
|
42
|
+
- **共享基底**:多头共享 Key 列空间基底,各头只存系数
|
|
43
|
+
- **量化基底**:压缩后进一步 INT8/FP8 量化,双重压缩
|
|
44
|
+
|
|
45
|
+
## GPU 可行性
|
|
46
|
+
- 张量化/GEMM:随机化 SVD = 3 次 GEMM + 1 次小 SVD,完美映射 Tensor Core
|
|
47
|
+
- 复杂度:$O(Ldk)$ 远优于 $O(Ld^2)$ 完整 SVD;$k \sim 256$ 时开销可忽略
|
|
48
|
+
- 显存:KV-Cache 从 $O(Ld)$ 到 $O(kd)$,$k/L \sim 1/16$ 可达 16x 压缩
|
|
49
|
+
- 低精度:SVD 建议 fp32(小矩阵可接受);压缩后 KV 可回 bf16 存储
|
|
50
|
+
- 并行:多层/多头压缩完全独立;增量更新 $O(kd)$ 极低延迟
|
|
51
|
+
- 算子融合:$K\Omega$ + QR 可部分融合;attention 的 QK^T 维度已缩小
|
|
52
|
+
|
|
53
|
+
## 论文表述方式
|
|
54
|
+
"基于 Eckart-Young-Mirsky 定理,采用随机化 SVD 将 KV-Cache 投影到最优秩-$k$ 子空间,以 $O(Ldk)$ 复杂度实现 $L/k$ 倍显存压缩,Weyl 扰动界保证 attention score 偏差不超过 $\sigma_{k+1}$。"
|
|
55
|
+
|
|
56
|
+
## 风险
|
|
57
|
+
- **秩选取不当**:$k$ 过小导致 $\sigma_{k+1}$ 不可忽略,长距离 recall 下降;需监控奇异值衰减曲线
|
|
58
|
+
- **增量 SVD 误差累积**:Brand 更新多次后偏离真实 SVD,需定期重压缩校正
|
|
59
|
+
- **位置编码失真**:RoPE 与 Key 耦合,压缩可能破坏相对位置;需单独处理或重新注入
|
|
60
|
+
- **双缓冲接缝**:压缩区与原始区的 attention score 尺度不一致,需统一归一化
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Spectral Token Pruning
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as ✅ verified / ⚠️ retrofittable (needs validation) / ❌ infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
|
+
|
|
4
|
+
## Target Problem
|
|
5
|
+
Use when pruning must be based on the structural importance of tokens (rather than raw attention scores alone): KV-Cache eviction, long-document summarization, inference acceleration (reducing $O(L^2)$), multimodal vision token compression. Core objective: **quantify the structural importance of each token via spectral methods, achieving pruning with minimal information loss**.
|
|
6
|
+
|
|
7
|
+
## Mathematical Foundations
|
|
8
|
+
- Lenses: lenses/spectral.md (identifying dominant spectral components, discarding redundant ones), lenses/algorithmic.md (complexity classification and approximation algorithms), lenses/perturbation.md (pruning = sparse perturbation, Weyl bound for spectral drift estimation)
|
|
9
|
+
- Knowledge: knowledge-base/matrix-analysis/spectral-decomposition.md (spectral radius, eigenvector centrality), knowledge-base/matrix-analysis/matrix-perturbation.md (Geršgorin discs, perturbation bounds), knowledge-base/matrix-analysis/positive-semidefinite.md (Gram matrix PSD structure)
|
|
10
|
+
|
|
11
|
+
## Required Mathematical Background
|
|
12
|
+
- **Eigenvector Centrality**: the principal eigenvector of the attention matrix $A$ satisfies $Ax = \lambda_1 x$; component $x_i$ quantifies the global influence of token $i$ (Perron--Frobenius guarantees non-negativity)
|
|
13
|
+
- **Spectral Gap**: $\Delta = \lambda_1 - \lambda_2$ governs the rate of information diffusion; large $\Delta \Rightarrow$ a few tokens dominate $\Rightarrow$ safe to prune
|
|
14
|
+
- **Fiedler Vector**: the second-smallest eigenvector of the Laplacian $L_{\text{sym}}$ yields the optimal bipartition; small magnitude = partition boundary = important
|
|
15
|
+
- **Weyl Perturbation Bound**: pruning = removing rows/columns = rank-1 perturbation $E$, $|\lambda_i(A) - \lambda_i(A_{\text{pruned}})| \leq \|E\|_2$
|
|
16
|
+
|
|
17
|
+
## AI Module Specification
|
|
18
|
+
```
|
|
19
|
+
Module: SpectralTokenPruner
|
|
20
|
+
Input: K ∈ R^{L×d} Parameters: retention ratio ρ ∈ (0,1]
|
|
21
|
+
|
|
22
|
+
Method 1 - Spectral centrality pruning (power iteration):
|
|
23
|
+
A = softmax(K @ K^T / √d) // L×L similarity graph
|
|
24
|
+
v = ones(L) / √L
|
|
25
|
+
for t in range(5): v = A @ v; v = v / ‖v‖ // power iteration O(L²·T)
|
|
26
|
+
indices = topk(v, ceil(ρ * L)) // retain tokens with highest centrality
|
|
27
|
+
|
|
28
|
+
Method 2 - Geršgorin cheap pruning (zero iterations):
|
|
29
|
+
A = softmax(K @ K^T / √d)
|
|
30
|
+
gersh_score = |diag(A)| + sum(|A|, dim=1) // disc upper bound, O(L²) elementwise
|
|
31
|
+
indices = topk(gersh_score, ceil(ρ * L))
|
|
32
|
+
|
|
33
|
+
Method 3 - Differentiable spectral pruning (end-to-end):
|
|
34
|
+
v = power_iteration(A, T=5)
|
|
35
|
+
gate = sigmoid(v @ W_gate / τ) // soft gating, τ annealing
|
|
36
|
+
K_gated = gate * K // per-token scaling
|
|
37
|
+
L_sparse = ‖gate‖_1 / L // sparsity regularization
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Implementable Architectures
|
|
41
|
+
- **Power iteration centrality**: 5-step matvec to estimate the principal eigenvector, $O(L^2 \cdot 5)$, suitable for moderate-length sequences
|
|
42
|
+
- **Sampling approximation**: for $L > 4096$, sample $m$ anchor points and construct an $m \times m$ submatrix for spectral analysis
|
|
43
|
+
- **Multi-head fusion**: average the attention graphs across different heads before performing spectral analysis
|
|
44
|
+
- **Progressive pruning**: incrementally increase the pruning ratio across layers (light pruning in shallow layers, heavy pruning in deep layers)
|
|
45
|
+
|
|
46
|
+
## GPU Feasibility
|
|
47
|
+
- Tensorization / GEMM: $A = KK^T$ is a GEMM; power iteration is a chain of matvecs; Geršgorin is elementwise
|
|
48
|
+
- Complexity: power iteration $O(L^2 T)$, $T \leq 10$; Geršgorin $O(L^2)$ elementwise, zero iterations
|
|
49
|
+
- Memory: the $L \times L$ similarity matrix exceeds 256 MB for $L > 8K$, requiring chunking or sampling
|
|
50
|
+
- Low precision: power iteration is stable in bf16 (normalization prevents overflow); Geršgorin is purely elementwise with no precision concerns
|
|
51
|
+
- Parallelism: spectral analysis across heads / layers is independently parallel; matvec is highly parallelizable
|
|
52
|
+
- Operator fusion: $KK^T$ + row-sum + topk can be fused into a single kernel
|
|
53
|
+
|
|
54
|
+
## Paper-Worthy Formulation
|
|
55
|
+
"We cast token pruning as spectral sparsification of a directed graph: leveraging the Perron--Frobenius principal eigenvector of the attention matrix to quantify global centrality, with the Weyl perturbation bound guaranteeing that post-pruning spectral drift does not exceed the $\ell_2$ norm of the removed tokens, while Geršgorin discs provide an $O(L^2)$ inexpensive alternative."
|
|
56
|
+
|
|
57
|
+
## Risks
|
|
58
|
+
- **$L \times L$ matrix memory bottleneck**: for long sequences the similarity matrix itself may exceed available memory, necessitating sampling or chunking
|
|
59
|
+
- **Slow power iteration convergence**: when $\lambda_1 / \lambda_2 \approx 1$, $O(1/\Delta)$ iterations are required, reducing efficiency
|
|
60
|
+
- **Semantics $\neq$ spectral importance**: certain tokens (e.g., punctuation) have low spectral centrality yet are semantically critical; purely spectral methods may prune them erroneously
|
|
61
|
+
- **Hard pruning is non-differentiable**: top-k blocks gradients; end-to-end training requires softmax relaxation or Gumbel-topk
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Spectral Token Pruning(谱 Token 剪枝)
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
|
+
|
|
4
|
+
## 适用问题
|
|
5
|
+
当需要基于 token 的结构性重要性(而非单纯 attention score)进行剪枝时使用:KV-Cache 驱逐、长文档摘要、推理加速(降低 $O(L^2)$)、多模态视觉 token 压缩。核心诉求:**用谱方法量化每个 token 的结构性重要性,实现信息损失最小的剪枝**。
|
|
6
|
+
|
|
7
|
+
## 数学思想来源
|
|
8
|
+
- 透镜:lenses/spectral.md(识别主导谱分量、丢弃冗余分量)、lenses/algorithmic.md(复杂度分类与近似算法)、lenses/perturbation.md(剪枝 = 稀疏扰动,Weyl 界估谱漂移)
|
|
9
|
+
- 知识:knowledge-base/matrix-analysis/spectral-decomposition.md(谱半径、特征向量中心性)、knowledge-base/matrix-analysis/matrix-perturbation.md(Geršgorin 圆盘、扰动界)、knowledge-base/matrix-analysis/positive-semidefinite.md(Gram 矩阵 PSD 结构)
|
|
10
|
+
|
|
11
|
+
## 需要的数学知识
|
|
12
|
+
- **特征向量中心性**:attention 矩阵 $A$ 的主特征向量 $Ax = \lambda_1 x$,分量 $x_i$ 量化 token $i$ 的全局影响力(Perron-Frobenius 保证非负)
|
|
13
|
+
- **谱间隙**:$\Delta = \lambda_1 - \lambda_2$ 决定信息扩散速度,$\Delta$ 大 $\Rightarrow$ 少数 token 主导 $\Rightarrow$ 安全剪枝
|
|
14
|
+
- **Fiedler 向量**:Laplacian $L_{\text{sym}}$ 的第二小特征向量给出最优二分割,幅值小 = 分割边界 = 重要
|
|
15
|
+
- **Weyl 扰动界**:剪枝 = 删除行/列 = 秩-1 扰动 $E$,$|\lambda_i(A) - \lambda_i(A_{\text{pruned}})| \leq \|E\|_2$
|
|
16
|
+
|
|
17
|
+
## AI 模块形式
|
|
18
|
+
```
|
|
19
|
+
模块:SpectralTokenPruner
|
|
20
|
+
输入:K ∈ R^{L×d} 参数:保留比例 ρ ∈ (0,1]
|
|
21
|
+
|
|
22
|
+
方法1 - 谱中心性剪枝(幂迭代):
|
|
23
|
+
A = softmax(K @ K^T / √d) // L×L 相似度图
|
|
24
|
+
v = ones(L) / √L
|
|
25
|
+
for t in range(5): v = A @ v; v = v / ‖v‖ // 幂迭代 O(L²·T)
|
|
26
|
+
indices = topk(v, ceil(ρ * L)) // 保留中心性最高的 token
|
|
27
|
+
|
|
28
|
+
方法2 - Geršgorin 廉价剪枝(零迭代):
|
|
29
|
+
A = softmax(K @ K^T / √d)
|
|
30
|
+
gersh_score = |diag(A)| + sum(|A|, dim=1) // 圆盘上界,O(L²) elementwise
|
|
31
|
+
indices = topk(gersh_score, ceil(ρ * L))
|
|
32
|
+
|
|
33
|
+
方法3 - 可微谱剪枝(端到端):
|
|
34
|
+
v = power_iteration(A, T=5)
|
|
35
|
+
gate = sigmoid(v @ W_gate / τ) // 软门控,τ 退火
|
|
36
|
+
K_gated = gate * K // 逐 token 缩放
|
|
37
|
+
L_sparse = ‖gate‖_1 / L // 稀疏正则
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 可实现结构
|
|
41
|
+
- **幂迭代中心性**:5 步 matvec 估计主特征向量,$O(L^2 \cdot 5)$,适合中等序列
|
|
42
|
+
- **采样近似**:$L > 4096$ 时采样 $m$ 个锚点,构造 $m \times m$ 子矩阵做谱分析
|
|
43
|
+
- **多头融合**:不同头的 attention 图取平均后再做谱分析
|
|
44
|
+
- **渐进式剪枝**:逐层递增剪枝比例(浅层少剪、深层多剪)
|
|
45
|
+
|
|
46
|
+
## GPU 可行性
|
|
47
|
+
- 张量化/GEMM:$A = KK^T$ 为 GEMM;幂迭代为 matvec 链;Geršgorin 为 elementwise
|
|
48
|
+
- 复杂度:幂迭代 $O(L^2 T)$,$T \leq 10$;Geršgorin $O(L^2)$ elementwise,零迭代
|
|
49
|
+
- 显存:$L \times L$ 相似度矩阵在 $L > 8K$ 时超 256MB,需分块或采样
|
|
50
|
+
- 低精度:幂迭代在 bf16 下稳定(归一化防溢出);Geršgorin 纯 elementwise 无精度问题
|
|
51
|
+
- 并行:多头/多层谱分析独立并行;matvec 高度并行
|
|
52
|
+
- 算子融合:$KK^T$ + row-sum + topk 可融合为单一 kernel
|
|
53
|
+
|
|
54
|
+
## 论文表述方式
|
|
55
|
+
"将 token 剪枝建模为有向图的谱稀疏化:利用 attention 矩阵的 Perron-Frobenius 主特征向量量化全局中心性,Weyl 扰动界保证剪枝后谱漂移不超过被移除 token 的 $\ell_2$ 范数,Geršgorin 圆盘提供 $O(L^2)$ 廉价替代。"
|
|
56
|
+
|
|
57
|
+
## 风险
|
|
58
|
+
- **$L \times L$ 矩阵显存瓶颈**:长序列下相似度矩阵本身可能超出显存,必须采样或分块
|
|
59
|
+
- **幂迭代收敛慢**:$\lambda_1 / \lambda_2 \approx 1$ 时需 $O(1/\Delta)$ 步,效率下降
|
|
60
|
+
- **语义 ≠ 谱重要性**:某些 token(如标点)谱中心性低但语义关键,纯谱方法可能误剪
|
|
61
|
+
- **硬剪枝不可微**:top-k 阻断梯度,端到端训练需 softmax 松弛或 Gumbel-topk
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Topology-Preserving Compression
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as ✅ verified / ⚠️ retrofittable (needs validation) / ❌ infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
|
+
|
|
4
|
+
## Target Problem
|
|
5
|
+
Use when compressing representations while homologically preserving the intrinsic topological structure of the data (connected components, loops, cavities): latent-space compression (a toroidal manifold must not collapse into a line segment), knowledge distillation (student--teacher homological equivalence), 3D mesh simplification (genus invariance), KV-Cache semantic preservation (cluster structure must not collapse). Core objective: **compress dimensions or counts while ensuring that changes in the persistence diagram of persistent homology remain controlled**.
|
|
6
|
+
|
|
7
|
+
## Mathematical Foundations
|
|
8
|
+
- Lenses: lenses/topological.md (topological invariants -- connected components and hole counts are invariant under continuous deformation), lenses/spectral.md (Gauss--Bonnet linking curvature and Euler characteristic), lenses/variational.md (constrained variational of compression ratio vs. topological fidelity)
|
|
9
|
+
- Knowledge: knowledge-base/topology/persistent-homology.md (persistent homology, Vietoris--Rips filtration, bottleneck distance), knowledge-base/topology/euler-characteristic.md (Euler characteristic for rapid topological diagnostics), knowledge-base/matrix-analysis/matrix-perturbation.md (Davis--Kahan subspace perturbation bound)
|
|
10
|
+
|
|
11
|
+
## Required Mathematical Background
|
|
12
|
+
- **Stability Theorem of Persistent Homology**: $d_B(D(X), D(Y)) \leq d_{GH}(X, Y)$; the Hausdorff distance bounds the change in persistence diagrams
|
|
13
|
+
- **Euler Characteristic Curve**: $\chi(\epsilon) = \sum_k (-1)^k \beta_k(\epsilon)$, richer in information than a single $\chi$, computable in $O(N^2)$
|
|
14
|
+
- **Mapping Cylinder Isomorphism**: if $f: X \to Y$ is an $\epsilon$-isometry, then $f_*$ induces an isomorphism on features with persistence intervals $> 2\epsilon$
|
|
15
|
+
- **Landmark Approximation**: build a witness complex on $m$ landmarks, $O(m^3)$ replacing $O(N^3)$
|
|
16
|
+
|
|
17
|
+
## AI Module Specification
|
|
18
|
+
```
|
|
19
|
+
Module: TopologyPreservingCompressor
|
|
20
|
+
Input: X ∈ R^{N×d} Parameters: topological weight λ_topo, target dimension r < d
|
|
21
|
+
|
|
22
|
+
Method 1 - Euler curve matching (most practical, differentiable):
|
|
23
|
+
Z = encoder(X) // R^{N×r}
|
|
24
|
+
D_orig = cdist(X, X); D_comp = cdist(Z, Z) // N×N distance matrices
|
|
25
|
+
chi_orig = euler_curve(D_orig, eps_grid) // |eps_grid|-dimensional vector
|
|
26
|
+
chi_comp = euler_curve(D_comp, eps_grid)
|
|
27
|
+
L_topo = ‖chi_orig - chi_comp‖_2² // topological matching loss (differentiable)
|
|
28
|
+
L_total = L_recon + λ_topo · L_topo
|
|
29
|
+
|
|
30
|
+
Method 2 - Persistent homology regularization (exact but expensive):
|
|
31
|
+
D_orig = persistent_homology(X, max_dim=1) // H_0 + H_1 barcode
|
|
32
|
+
D_comp = persistent_homology(Z, max_dim=1)
|
|
33
|
+
// differentiable surrogate: persistence landscape/image
|
|
34
|
+
L_topo = ‖landscape(D_orig) - landscape(D_comp)‖_2²
|
|
35
|
+
|
|
36
|
+
Method 3 - Topology monitoring + adaptive compression ratio (at inference time):
|
|
37
|
+
Z = compress(X, ratio=ρ)
|
|
38
|
+
if count_components(Z, τ) < 0.8 * count_components(X, τ):
|
|
39
|
+
ρ *= 1.2; Z = compress(X, ratio=ρ) // topological collapse → reduce compression ratio
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Implementable Architectures
|
|
43
|
+
- **Euler curve matching layer**: $\chi(\epsilon)$ requires only a distance matrix + threshold counting; the $\epsilon$ sweep is parallelizable
|
|
44
|
+
- **Landmark sampler**: FPS (Farthest Point Sampling) in $O(Nm)$, guaranteeing coverage
|
|
45
|
+
- **Topology-aware distillation**: the persistence image discrepancy between teacher and student representations serves as an auxiliary distillation objective
|
|
46
|
+
- **Topological diagnostics dashboard**: real-time plotting of $\beta_0(\epsilon), \beta_1(\epsilon)$ curves during training
|
|
47
|
+
|
|
48
|
+
## GPU Feasibility
|
|
49
|
+
- Tensorization / GEMM: the distance matrix `cdist` is GEMM-dominated; $\chi(\epsilon)$ involves threshold counting + cumulative sum
|
|
50
|
+
- Complexity: full persistent homology $O(N^3)$ is infeasible; Euler curve $O(N^2 |\epsilon|)$ is feasible; Landmark $O(m^3)$
|
|
51
|
+
- Memory: the $N \times N$ distance matrix requires chunking or landmark reduction for $N > 8K$
|
|
52
|
+
- Low precision: distance computation is stable in bf16 (positive-number addition); $\chi$ involves integer arithmetic with no precision concerns
|
|
53
|
+
- Parallelism: each threshold in the $\epsilon$ sweep is independently parallel; landmark selection can be batch-parallelized
|
|
54
|
+
- Operator fusion: cdist + threshold + count can be fused to avoid materializing the large distance matrix
|
|
55
|
+
|
|
56
|
+
## Paper-Worthy Formulation
|
|
57
|
+
"Grounded in the Bottleneck stability theorem of persistent homology, we achieve $O(N^2)$-complexity topology-preserving regularization via Euler characteristic curve matching, ensuring that post-compression deviations in Betti numbers over persistence intervals are controlled by the Hausdorff distance."
|
|
58
|
+
|
|
59
|
+
## Risks
|
|
60
|
+
- **Persistent homology computational bottleneck**: exact boundary matrix reduction is highly serial ($O(N^3)$), necessitating reliance on Euler curve or landmark approximations
|
|
61
|
+
- **Information loss in the Euler curve**: $\chi = \sum(-1)^k \beta_k$ collapses multi-order Betti numbers into a single scalar; distinct topologies may share the same $\chi$
|
|
62
|
+
- **Topology $\neq$ semantics**: topological preservation does not imply semantic preservation -- two semantically distinct spaces may be topologically isomorphic
|
|
63
|
+
- **Scale parameter sensitivity**: the $\epsilon$ range for the filtration must be set manually and varies significantly across datasets
|
|
64
|
+
- **Landmark sampling bias**: FPS may produce non-uniform coverage in high-dimensional spaces, leading to biased topological estimates
|