math-skill 3.0.0 → 3.1.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 +50 -30
- package/README.md +26 -6
- package/agents/math-critic.en.md +30 -28
- package/agents/math-critic.md +30 -30
- package/commands/ask.en.md +15 -0
- package/commands/ask.md +6 -1
- package/design-patterns/attention/equivariant-attention.en.md +17 -11
- package/design-patterns/attention/equivariant-attention.md +16 -10
- package/design-patterns/attention/geometry-aware-attention.en.md +11 -11
- package/design-patterns/attention/geometry-aware-attention.md +10 -10
- package/design-patterns/attention/information-bottleneck-attention.en.md +40 -20
- package/design-patterns/attention/information-bottleneck-attention.md +40 -20
- package/design-patterns/attention/projection-attention.en.md +12 -12
- package/design-patterns/attention/projection-attention.md +11 -11
- package/design-patterns/attention/spectral-attention.en.md +27 -13
- package/design-patterns/attention/spectral-attention.md +26 -12
- package/design-patterns/compression/leverage-score-selection.en.md +14 -14
- package/design-patterns/compression/leverage-score-selection.md +14 -14
- package/design-patterns/compression/low-rank-kv-cache.en.md +40 -13
- package/design-patterns/compression/low-rank-kv-cache.md +39 -13
- package/design-patterns/compression/spectral-token-pruning.en.md +17 -15
- package/design-patterns/compression/spectral-token-pruning.md +17 -15
- package/design-patterns/compression/topology-preserving-compression.en.md +11 -11
- package/design-patterns/compression/topology-preserving-compression.md +11 -11
- package/design-patterns/loss/constraint-penalty.en.md +9 -5
- package/design-patterns/loss/constraint-penalty.md +10 -7
- package/design-patterns/loss/contrastive-loss.en.md +38 -12
- package/design-patterns/loss/contrastive-loss.md +39 -13
- package/design-patterns/loss/information-bottleneck-loss.en.md +6 -6
- package/design-patterns/loss/information-bottleneck-loss.md +7 -7
- package/design-patterns/loss/orthogonality-loss.en.md +25 -18
- package/design-patterns/loss/orthogonality-loss.md +25 -19
- package/design-patterns/loss/variational-loss.en.md +5 -5
- package/design-patterns/loss/variational-loss.md +6 -6
- package/design-patterns/overview.en.md +36 -0
- package/design-patterns/overview.md +36 -0
- package/design-patterns/representation/equivariant-split.en.md +5 -5
- package/design-patterns/representation/equivariant-split.md +5 -6
- package/design-patterns/representation/manifold-representation.en.md +13 -13
- package/design-patterns/representation/manifold-representation.md +14 -15
- package/design-patterns/representation/shared-private-decomposition.en.md +13 -8
- package/design-patterns/representation/shared-private-decomposition.md +13 -10
- package/design-patterns/representation/subspace-alignment.en.md +13 -13
- package/design-patterns/representation/subspace-alignment.md +13 -13
- package/design-patterns/routing/graph-routing.en.md +12 -12
- package/design-patterns/routing/graph-routing.md +12 -12
- package/design-patterns/routing/moe-routing.en.md +13 -13
- package/design-patterns/routing/moe-routing.md +13 -15
- package/design-patterns/routing/optimal-transport-routing.en.md +6 -6
- package/design-patterns/routing/optimal-transport-routing.md +7 -7
- package/design-patterns/routing/spectral-clustering-routing.en.md +27 -16
- package/design-patterns/routing/spectral-clustering-routing.md +27 -18
- package/knowledge-base/differential-geometry/connection.en.md +17 -2
- package/knowledge-base/differential-geometry/connection.md +17 -2
- package/knowledge-base/differential-geometry/curvature.en.md +14 -1
- package/knowledge-base/differential-geometry/curvature.md +14 -1
- package/knowledge-base/differential-geometry/geodesic.en.md +16 -2
- package/knowledge-base/differential-geometry/geodesic.md +16 -2
- package/knowledge-base/differential-geometry/index.en.md +52 -0
- package/knowledge-base/differential-geometry/index.md +52 -0
- package/knowledge-base/differential-geometry/manifold.en.md +15 -2
- package/knowledge-base/differential-geometry/manifold.md +15 -2
- package/knowledge-base/differential-geometry/metric-tensor.en.md +16 -2
- package/knowledge-base/differential-geometry/metric-tensor.md +17 -3
- package/knowledge-base/differential-geometry/tangent-space.en.md +17 -3
- package/knowledge-base/differential-geometry/tangent-space.md +17 -3
- package/knowledge-base/information-geometry/fisher-metric.en.md +32 -13
- package/knowledge-base/information-geometry/fisher-metric.md +32 -13
- package/knowledge-base/information-geometry/index.en.md +45 -0
- package/knowledge-base/information-geometry/index.md +45 -0
- package/knowledge-base/information-geometry/natural-gradient.en.md +24 -10
- package/knowledge-base/information-geometry/natural-gradient.md +24 -10
- package/knowledge-base/lie-theory/equivariance.en.md +18 -3
- package/knowledge-base/lie-theory/equivariance.md +18 -3
- package/knowledge-base/lie-theory/group-action.en.md +16 -2
- package/knowledge-base/lie-theory/group-action.md +16 -2
- package/knowledge-base/lie-theory/index.en.md +52 -0
- package/knowledge-base/lie-theory/index.md +52 -0
- package/knowledge-base/lie-theory/lie-algebra.en.md +17 -2
- package/knowledge-base/lie-theory/lie-algebra.md +17 -2
- package/knowledge-base/lie-theory/lie-group.en.md +17 -3
- package/knowledge-base/lie-theory/lie-group.md +17 -3
- package/knowledge-base/lie-theory/representation.en.md +17 -2
- package/knowledge-base/lie-theory/representation.md +17 -2
- package/knowledge-base/matrix-analysis/index.en.md +49 -0
- package/knowledge-base/matrix-analysis/index.md +49 -0
- package/knowledge-base/matrix-analysis/low-rank-approximation.en.md +20 -7
- package/knowledge-base/matrix-analysis/low-rank-approximation.md +20 -7
- package/knowledge-base/matrix-analysis/matrix-perturbation.en.md +13 -1
- package/knowledge-base/matrix-analysis/matrix-perturbation.md +13 -1
- package/knowledge-base/matrix-analysis/positive-semidefinite.en.md +20 -6
- package/knowledge-base/matrix-analysis/positive-semidefinite.md +20 -6
- package/knowledge-base/matrix-analysis/projection.en.md +14 -1
- package/knowledge-base/matrix-analysis/projection.md +14 -1
- package/knowledge-base/matrix-analysis/spectral-decomposition.en.md +14 -1
- package/knowledge-base/matrix-analysis/spectral-decomposition.md +14 -1
- package/knowledge-base/optimization/constrained-optimization.en.md +17 -3
- package/knowledge-base/optimization/constrained-optimization.md +17 -3
- package/knowledge-base/optimization/convex-optimization.en.md +15 -2
- package/knowledge-base/optimization/convex-optimization.md +15 -2
- package/knowledge-base/optimization/index.en.md +50 -0
- package/knowledge-base/optimization/index.md +50 -0
- package/knowledge-base/optimization/lagrangian-duality.en.md +18 -4
- package/knowledge-base/optimization/lagrangian-duality.md +18 -4
- package/knowledge-base/optimization/proximal-method.en.md +15 -1
- package/knowledge-base/optimization/proximal-method.md +15 -1
- package/knowledge-base/optimization/riemannian-optimization.en.md +18 -4
- package/knowledge-base/optimization/riemannian-optimization.md +18 -4
- package/knowledge-base/overview.en.md +20 -8
- package/knowledge-base/overview.md +20 -8
- package/knowledge-base/probability/concentration-inequality.en.md +21 -7
- package/knowledge-base/probability/concentration-inequality.md +21 -7
- package/knowledge-base/probability/entropy.en.md +24 -8
- package/knowledge-base/probability/entropy.md +23 -7
- package/knowledge-base/probability/fisher-information.en.md +24 -10
- package/knowledge-base/probability/fisher-information.md +24 -10
- package/knowledge-base/probability/index.en.md +51 -0
- package/knowledge-base/probability/index.md +51 -0
- package/knowledge-base/probability/information-bottleneck.en.md +24 -8
- package/knowledge-base/probability/information-bottleneck.md +24 -8
- package/knowledge-base/probability/kl-divergence.en.md +21 -8
- package/knowledge-base/probability/kl-divergence.md +21 -8
- package/knowledge-base/topology/euler-characteristic.en.md +15 -2
- package/knowledge-base/topology/euler-characteristic.md +15 -2
- package/knowledge-base/topology/fundamental-group.en.md +16 -2
- package/knowledge-base/topology/fundamental-group.md +16 -2
- package/knowledge-base/topology/index.en.md +48 -0
- package/knowledge-base/topology/index.md +48 -0
- package/knowledge-base/topology/persistent-homology.en.md +17 -2
- package/knowledge-base/topology/persistent-homology.md +17 -2
- package/lenses/algorithmic.en.md +1 -1
- package/lenses/algorithmic.md +1 -1
- package/lenses/axiomatization.en.md +1 -1
- package/lenses/axiomatization.md +1 -1
- package/lenses/categorical.en.md +1 -1
- package/lenses/categorical.md +1 -1
- package/lenses/causal.en.md +1 -1
- package/lenses/causal.md +1 -1
- package/lenses/duality.en.md +1 -1
- package/lenses/duality.md +1 -1
- package/lenses/game.en.md +2 -2
- package/lenses/game.md +2 -2
- package/lenses/geometric.en.md +1 -1
- package/lenses/geometric.md +1 -1
- package/lenses/local-to-global.en.md +1 -1
- package/lenses/local-to-global.md +1 -1
- package/lenses/perturbation.en.md +1 -1
- package/lenses/perturbation.md +1 -1
- package/lenses/probabilistic.en.md +1 -1
- package/lenses/probabilistic.md +1 -1
- package/lenses/projection.en.md +1 -1
- package/lenses/projection.md +1 -1
- package/lenses/spectral.en.md +2 -2
- package/lenses/spectral.md +2 -2
- package/lenses/symmetry.en.md +1 -1
- package/lenses/symmetry.md +1 -1
- package/lenses/topological.en.md +2 -2
- package/lenses/topological.md +2 -2
- package/lenses/variational.en.md +1 -1
- package/lenses/variational.md +1 -1
- package/package.json +5 -5
- package/references/agentic-workflow.en.md +3 -3
- package/references/agentic-workflow.md +1 -1
- package/references/books/abstract-algebra.en.md +124 -0
- package/references/books/abstract-algebra.md +17 -17
- package/references/books/algebraic-geometry-rising-sea.en.md +171 -0
- package/references/books/algebraic-geometry-rising-sea.md +7 -7
- package/references/books/differential-geometry.en.md +140 -0
- package/references/books/differential-geometry.md +23 -23
- package/references/books/matrix-analysis.en.md +146 -0
- package/references/books/matrix-analysis.md +32 -32
- package/references/books/micro-lie-theory.en.md +116 -0
- package/references/books/micro-lie-theory.md +16 -16
- package/references/books/optimization-ml.en.md +163 -0
- package/references/books/optimization-ml.md +20 -21
- package/references/books/smooth-manifolds.en.md +105 -0
- package/references/books/smooth-manifolds.md +15 -15
- package/references/gpu-friendly-math.en.md +35 -9
- package/references/gpu-friendly-math.md +33 -5
- package/references/skill-index.en.md +105 -0
- package/references/skill-index.md +105 -0
- package/skills/math-research-activator/SKILL.en.md +72 -88
- package/skills/math-research-activator/SKILL.md +61 -78
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# Equivariant Attention
|
|
2
|
-
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as [v] verified / [~] retrofittable (needs validation) / [x] infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
3
|
|
|
4
4
|
## Applicable Problems
|
|
5
5
|
When the input possesses an **explicit symmetry group $G$ action** (rotation, translation, permutation, reflection, etc.), and the desired model output should be **equivariant** (covariant) rather than invariant under the same transformations, equivariant constraints must be directly encoded into the attention mechanism. Typical scenarios include: 3D point clouds / molecules ($E(3)$ rigid-body group), image classification ($D_n$ rotation/reflection group), set data ($S_n$ permutation group), and multi-view / multi-sensor fusion.
|
|
6
6
|
|
|
7
7
|
## Mathematical Inspiration
|
|
8
8
|
- Lenses: [symmetry, categorical (unified framework for group actions)]
|
|
9
|
-
- Knowledge: [`../../knowledge-base/probability/concentration-inequality.md` (sample efficiency gains under equivariant constraints -- data equivalence along orbits), `../../knowledge-base/probability/entropy.md` (equivariant constraints reduce output distribution entropy, yielding stronger inductive bias)]
|
|
9
|
+
- Knowledge: [`../../knowledge-base/probability/concentration-inequality.en.md` (sample efficiency gains under equivariant constraints -- data equivalence along orbits), `../../knowledge-base/probability/entropy.en.md` (equivariant constraints reduce output distribution entropy, yielding stronger inductive bias)]
|
|
10
10
|
|
|
11
11
|
## Required Mathematical Knowledge
|
|
12
12
|
- **Group Representation Theory Basics**: Linear representation of a group $G$, $\rho: G \to GL(V)$, and irreducible representation decomposition
|
|
13
13
|
- **Equivariant Map Definition**: $f(g \cdot x) = \rho_{\text{out}}(g) \cdot f(x)$ for all $g \in G$
|
|
14
|
-
- **Orbit-Stabilizer Theorem** (see
|
|
14
|
+
- **Orbit-Stabilizer Theorem** (see `../../references/books/abstract-algebra.en.md` Ch.5): $|orbit| = |G|/|stab|$, giving the parameter sharing multiplier
|
|
15
15
|
- **Schur's Lemma**: An equivariant linear map between irreducible representations is either zero or a scalar multiple
|
|
16
16
|
|
|
17
17
|
## AI Module Form
|
|
@@ -55,14 +55,20 @@ output = mean(softmax((rho(g)@X@W_q) @ (rho(g)@X@W_k).T/sqrt(d)) @ (rho(g)@X@W_v
|
|
|
55
55
|
- **G-CNN Attention**: $D_n$ rotation/reflection equivariance for remote sensing imagery and medical imaging
|
|
56
56
|
|
|
57
57
|
## GPU Feasibility
|
|
58
|
-
- **
|
|
59
|
-
- **
|
|
60
|
-
- **
|
|
61
|
-
- **
|
|
62
|
-
- **
|
|
63
|
-
- **
|
|
64
|
-
- **
|
|
65
|
-
- **
|
|
58
|
+
- **D1**: Group actions implemented as $\rho(g)$ matrix multiplications; equivariant features stored as batched tensors
|
|
59
|
+
- **D2**: $\rho(g) X$ and $Q K^T$ are both GEMM operations; $|G|$ group elements map to batched GEMM
|
|
60
|
+
- **D3**: $|G|$-fold computation overhead; acceptable for small groups ($|D_4|=8$), infeasible for large groups ($|S_n|=n!$). Remedy: use generators + Cayley graph propagation instead of full group enumeration
|
|
61
|
+
- **D4**: Requires storing $|G|$ copies of intermediate features; mitigated by chunking + gradient checkpointing
|
|
62
|
+
- **D5**: Orthogonal representation matrices are numerically stable under bf16
|
|
63
|
+
- **D6**: $|G|$ group elements are naturally parallelizable (along the batch dimension)
|
|
64
|
+
- **D7**: Permutation $\rho(g)$ is extremely sparse and can be encoded as gather indices
|
|
65
|
+
- **D8**: Group action + linear duality can be fused into a single batched GEMM
|
|
66
|
+
|
|
67
|
+
**Quantitative assessment example** (E(3) equivariant, |G|=24 rotation group, d=64, n=512):
|
|
68
|
+
- D3: |G|x standard attention FLOPs = 24 · 2·512²·64 ≈ 805M (vs standard 33.6M)
|
|
69
|
+
- D4: Requires storing |G|=24 copies of intermediate features, 24 · 512 · 64 · 2B ≈ 1.5MB extra memory
|
|
70
|
+
- D6: 24 group elements parallelizable along the batch dimension, ideal speedup 24x
|
|
71
|
+
- D8: ρ(g)·X·W_q three-step fusion into a single batched GEMM
|
|
66
72
|
|
|
67
73
|
## Paper Phrasing
|
|
68
74
|
"We propose an equivariant attention mechanism that constrains attention weights to be group invariants and attention outputs to be group equivariants, directly encoding the inductive bias of symmetry group $G$ into the model architecture without additional data augmentation, achieving a $|G|/|stab|$-fold improvement in parameter efficiency."
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# 等变注意力 / Equivariant Attention
|
|
2
|
-
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
3
|
|
|
4
4
|
## 适用问题
|
|
5
5
|
当输入具有**明确的对称群 $G$ 作用**(旋转、平移、置换、反射等),且期望模型输出在相同变换下**协变**(equivariant)而非不变时,需要将等变约束直接编入注意力机制。典型场景:3D 点云/分子($E(3)$ 刚体群)、图像分类($D_n$ 旋转/镜像群)、集合数据($S_n$ 置换群)、多视角/多传感器融合。
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
## 需要的数学知识
|
|
12
12
|
- **群表示论基础**:群 $G$ 的线性表示 $\rho: G \to GL(V)$,不可约表示分解
|
|
13
13
|
- **等变映射定义**:$f(g \cdot x) = \rho_{\text{out}}(g) \cdot f(x)$,对所有 $g \in G$
|
|
14
|
-
- **轨道-稳定子定理**(参见
|
|
14
|
+
- **轨道-稳定子定理**(参见 `../../references/books/abstract-algebra.md` Ch.5):$|orbit| = |G|/|stab|$,参数共享倍率
|
|
15
15
|
- **Schur 引理**:不可约表示间的等变线性映射要么为零要么为标量乘
|
|
16
16
|
|
|
17
17
|
## AI 模块形式
|
|
@@ -55,14 +55,20 @@ output = mean(softmax((rho(g)@X@W_q) @ (rho(g)@X@W_k).T/sqrt(d)) @ (rho(g)@X@W_v
|
|
|
55
55
|
- **G-CNN 注意力**:$D_n$ 旋转/镜像等变,用于遥感图像和医学影像
|
|
56
56
|
|
|
57
57
|
## GPU 可行性
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
58
|
+
- **D1[v]**:群作用实现为 $\rho(g)$ 矩阵乘,等变特征为 batched 张量
|
|
59
|
+
- **D2[v]**:$\rho(g) X$ 和 $Q K^T$ 均为 GEMM;$|G|$ 个群元素 → batched GEMM
|
|
60
|
+
- **D3[~]**:$|G|$ 倍计算量,小群($|D_4|=8$)可接受,大群($|S_n|=n!$)不可。改造:用生成元 + Cayley 图传播替代整群枚举
|
|
61
|
+
- **D4[~]**:需存储 $|G|$ 份中间特征,可分块 + gradient checkpointing
|
|
62
|
+
- **D5[v]**:正交表示矩阵在 bf16 下数值稳定
|
|
63
|
+
- **D6[v]**:$|G|$ 个群元素天然并行(batch 维)
|
|
64
|
+
- **D7[~]**:置换 $\rho(g)$ 极度稀疏,可编码为 gather 索引
|
|
65
|
+
- **D8[v]**:群作用 + 线性变换可融合为单次 batched GEMM
|
|
66
|
+
|
|
67
|
+
**量化评估示例**(E(3) 等变, |G|=24 旋转群, d=64, n=512):
|
|
68
|
+
- D3: |G|× 标准 attention FLOPs = 24 · 2·512²·64 ≈ 805M(vs 标准 33.6M)
|
|
69
|
+
- D4: 需存储 |G|=24 份中间特征,24 · 512 · 64 · 2B ≈ 1.5MB 额外显存
|
|
70
|
+
- D6: 24 个群元素沿 batch 维并行,理想加速比 24x
|
|
71
|
+
- D8: ρ(g)·X·W_q 三步融合为单次 batched GEMM
|
|
66
72
|
|
|
67
73
|
## 论文表述方式
|
|
68
74
|
"我们提出等变注意力机制,通过将注意力权重约束为群不变量、注意力输出约束为群等变量,在不增加数据增强的情况下将对称群 $G$ 的归纳偏置直接编码进模型结构,参数效率提升 $|G|/|stab|$ 倍。"
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Geometry-Aware Attention
|
|
2
|
-
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as [v] verified / [~] retrofittable (needs validation) / [x] infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
3
|
|
|
4
4
|
## Applicable Problems
|
|
5
5
|
When **known geometric relationships** exist between tokens/keys (spatial distance, manifold geodesic distance, hierarchical structure, temporal interval), the position-agnostic inner product of standard attention cannot exploit this structural information. Geometry-aware attention **directly injects geometric priors into attention weight computation**, enabling the model to naturally respect the metric structure of the underlying space. Typical scenarios include: 3D scene understanding, molecular conformation, temporal forecasting, hierarchical text structure (paragraph-sentence-word), and knowledge graphs.
|
|
6
6
|
|
|
7
7
|
## Mathematical Inspiration
|
|
8
8
|
- Lenses: [symmetry (metric invariance), duality (coordinate-system-independent representation)]
|
|
9
|
-
- Knowledge: [`../../knowledge-base/information-geometry/fisher-metric.md` (geometric metrics on distribution spaces), `../../knowledge-base/probability/concentration-inequality.md` (concentration behavior under geometric constraints)]
|
|
9
|
+
- Knowledge: [`../../knowledge-base/information-geometry/fisher-metric.en.md` (geometric metrics on distribution spaces), `../../knowledge-base/probability/concentration-inequality.en.md` (concentration behavior under geometric constraints)]
|
|
10
10
|
|
|
11
11
|
## Required Mathematical Knowledge
|
|
12
12
|
- **Metric Spaces and Distance Functions**: Euclidean distance, geodesic distance, tree distance, Wasserstein distance
|
|
@@ -56,17 +56,17 @@ def manifold_attention(Q, K, V, manifold):
|
|
|
56
56
|
- **Molecular Conformation Attention**: 3D atomic coordinates -> distance matrix -> geometric bias, used in molecular GNNs and protein structure prediction
|
|
57
57
|
|
|
58
58
|
## GPU Feasibility
|
|
59
|
-
- **
|
|
60
|
-
- **
|
|
61
|
-
- **
|
|
62
|
-
- **
|
|
63
|
-
- **
|
|
64
|
-
- **
|
|
65
|
-
- **
|
|
66
|
-
- **
|
|
59
|
+
- **D1**: Distance matrices and bias matrices are dense tensors with element-wise operations
|
|
60
|
+
- **D2**: The main body $Q K^T$ is standard GEMM; geometric bias is additive and does not interrupt GEMM
|
|
61
|
+
- **D3**: Pairwise distance matrix construction and storage are $O(n^2)$; however, block-wise computation + online softmax can avoid materializing the full matrix
|
|
62
|
+
- **D4**: The $n \times n$ distance matrix occupies GPU memory; mitigated by block/streaming computation (compatible with FlashAttention)
|
|
63
|
+
- **D5**: Distance computation and bias addition are stable under bf16; the exp in RBF requires overflow caution (clamp distances)
|
|
64
|
+
- **D6**: Distance computation and attention can be pipelined; pairwise distances can be computed in parallel blocks
|
|
65
|
+
- **D7**: Distant biases tend toward $-\infty$ (approaching zero after softmax), naturally inducing structured sparsity (local attention windows)
|
|
66
|
+
- **D8**: Geometric bias can be fused into FlashAttention's online softmax loop (added after $QK^T$, before softmax)
|
|
67
67
|
|
|
68
68
|
## Paper Phrasing
|
|
69
|
-
"We propose geometry-aware attention, which explicitly injects
|
|
69
|
+
"We propose geometry-aware attention, which explicitly injects geometric distance bias terms into attention scores, enabling the model to naturally respect the metric structure of the input space. This achieves exponential decay of attention to distant tokens with only a small number of additional geometric bias parameters (zero for fixed bias variants such as ALiBi), while maintaining compatibility with FlashAttention."
|
|
70
70
|
|
|
71
71
|
## Risks
|
|
72
72
|
- **Geometric Prior Conflicting with Data**: If geometric distance is inconsistent with semantic relevance (e.g., distant but semantically related tokens in text), strong geometric bias will impair model expressiveness. The bias should be learnable and overridable by content-based attention.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# 几何感知注意力 / Geometry-Aware Attention
|
|
2
|
-
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
3
|
|
|
4
4
|
## 适用问题
|
|
5
5
|
当 token/key 之间存在**已知的几何关系**(空间距离、流形测地距、层级结构、时序间隔)时,标准注意力的位置无关内积无法利用这些结构信息。几何感知注意力将**几何先验直接注入注意力权重计算**,使模型天然尊重底空间的度量结构。典型场景:3D 场景理解、分子构象、时序预测、层级文本结构(段落-句子-词)、知识图谱。
|
|
@@ -55,17 +55,17 @@ def manifold_attention(Q, K, V, manifold):
|
|
|
55
55
|
- **分子构象注意力**:3D 原子坐标 → 距离矩阵 → 几何偏置,用于分子 GNN 和蛋白质结构预测
|
|
56
56
|
|
|
57
57
|
## GPU 可行性
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
58
|
+
- **D1[v]**:距离矩阵和偏置矩阵均为稠密张量,逐元素运算
|
|
59
|
+
- **D2[v]**:主体 $Q K^T$ 为标准 GEMM;几何偏置为加法,不阻断 GEMM
|
|
60
|
+
- **D3[~]**:成对距离矩阵 $O(n^2)$ 构建和存储;但可用分块计算 + online softmax 避免物化完整矩阵
|
|
61
|
+
- **D4[~]**:$n \times n$ 距离矩阵占用显存;可分块/流式计算(与 FlashAttention 兼容)
|
|
62
|
+
- **D5[v]**:距离计算和偏置加法在 bf16 下稳定;RBF 的 exp 需注意上溢(clamp 距离)
|
|
63
|
+
- **D6[v]**:距离计算和注意力可流水线并行,成对距离可分块并行
|
|
64
|
+
- **D7[v]**:远距离偏置趋于 $-\infty$(softmax 后趋零),天然诱导结构化稀疏(局部注意力窗口)
|
|
65
|
+
- **D8[v]**:几何偏置可融入 FlashAttention 的 online softmax 循环中(加在 $QK^T$ 之后、softmax 之前)
|
|
66
66
|
|
|
67
67
|
## 论文表述方式
|
|
68
|
-
"
|
|
68
|
+
"我们提出几何感知注意力,通过在注意力分数中显式注入几何距离偏置项,使模型天然尊重输入空间的度量结构,在仅增加少量几何偏置参数(固定偏置如 ALiBi 则零额外参数)的情况下将远距离 token 的注意力指数衰减,同时保持与 FlashAttention 的兼容性。"
|
|
69
69
|
|
|
70
70
|
## 风险
|
|
71
71
|
- **几何先验与数据冲突**:若几何距离与语义相关性不一致(如文本中远距离但语义相关的 token),强几何偏置会伤害模型表达。需让偏置可学习且可被内容注意力覆盖。
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Information Bottleneck Attention
|
|
2
|
-
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as [v] verified / [~] retrofittable (needs validation) / [x] infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
3
|
|
|
4
4
|
## Applicable Problems
|
|
5
5
|
When the attention mechanism needs to **selectively transmit useful information while suppressing redundant/noisy information**, information bottleneck theory can guide the learning of attention weights -- maximizing the mutual information $I(Z;Y)$ of the attention distribution with respect to the target $Y$, while minimizing the mutual information $I(X;Z)$ with respect to the input $X$. Typical scenarios include: long-document summarization (filtering large numbers of irrelevant tokens), multimodal alignment (cross-modal noise suppression), and interpretability (attention weights as visualization of information flow).
|
|
6
6
|
|
|
7
7
|
## Mathematical Inspiration
|
|
8
8
|
- Lenses: [categorical (information-theoretic framework unifying attention design), variational (constrained variational and Lagrangian duality)]
|
|
9
|
-
- Knowledge: [`../../knowledge-base/probability/information-bottleneck.md` (IB objective and variational lower bound), `../../knowledge-base/probability/kl-divergence.md` (implementation of KL regularization), `../../knowledge-base/probability/entropy.md` (mutual information estimation)]
|
|
9
|
+
- Knowledge: [`../../knowledge-base/probability/information-bottleneck.en.md` (IB objective and variational lower bound), `../../knowledge-base/probability/kl-divergence.en.md` (implementation of KL regularization), `../../knowledge-base/probability/entropy.en.md` (mutual information estimation)]
|
|
10
10
|
|
|
11
11
|
## Required Mathematical Knowledge
|
|
12
12
|
- **Information Bottleneck Objective**: $\min I(X;Z) - \beta I(Z;Y)$, balancing compression and prediction
|
|
@@ -19,27 +19,47 @@ When the attention mechanism needs to **selectively transmit useful information
|
|
|
19
19
|
|
|
20
20
|
**Scheme A: KL-Regularized Attention (Simplest IB Attention)**:
|
|
21
21
|
```python
|
|
22
|
-
#
|
|
22
|
+
# ⚠ Note the semantic direction of KL:
|
|
23
|
+
# KL(attn || uniform) = log(n) - H(attn)
|
|
24
|
+
# Minimizing +beta * KL(attn || uniform) = maximizing H(attn) → pushes toward uniform (max entropy / max compression)
|
|
25
|
+
# This does NOT induce sparsity by itself! Sparsity emerges from the tension with task_loss.
|
|
26
|
+
# For explicit sparsity induction, use an entropy penalty +beta * H(attn) (minimize entropy).
|
|
27
|
+
|
|
23
28
|
scores = (Q @ K.T) / sqrt(d)
|
|
24
29
|
attn = softmax(scores)
|
|
25
30
|
uniform_prior = ones_like(attn) / n
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
|
|
32
|
+
# Correct IB compression term: KL(q(z|x) || p(z)), where p(z) is a fixed prior
|
|
33
|
+
# Minimizing this term → compresses information (pushes toward prior); tension with task_loss yields useful attention
|
|
34
|
+
kl_compression = (attn * (log(attn + eps) - log(uniform_prior))).sum(dim=-1).mean()
|
|
35
|
+
# = log(n) - H(attn)
|
|
36
|
+
|
|
37
|
+
# IB objective: min task_loss + beta * I(X;Z), where I(X;Z) ≈ KL(attn || prior)
|
|
38
|
+
loss_ib = task_loss + beta * kl_compression
|
|
39
|
+
# Note: this term alone pushes toward uniform; sparsity arises from the counter-pull of task_loss
|
|
40
|
+
|
|
41
|
+
# Alternative: explicit sparsity induction (not IB compression, but entropy penalty)
|
|
42
|
+
entropy = -(attn * log(attn + eps)).sum(dim=-1).mean()
|
|
43
|
+
loss_sparse = task_loss + beta * entropy # minimize entropy → concentrated attention → implicit Top-K
|
|
44
|
+
|
|
28
45
|
output = attn @ V
|
|
29
46
|
```
|
|
30
47
|
|
|
31
48
|
**Scheme B: Variational Information Bottleneck Attention (VIB-Attention)**:
|
|
32
49
|
```python
|
|
33
|
-
# Introduce stochastic bottleneck Z ~ q(Z|
|
|
50
|
+
# Introduce a continuous stochastic bottleneck Z ~ q(Z|context) after value aggregation
|
|
34
51
|
scores = (Q @ K.T) / sqrt(d)
|
|
35
52
|
attn = softmax(scores)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
53
|
+
context = attn @ V
|
|
54
|
+
mu_z, log_var_z = linear_mu(context), linear_logvar(context)
|
|
55
|
+
z = mu_z + exp(0.5 * log_var_z) * randn_like(mu_z) # Gaussian reparameterization
|
|
56
|
+
kl = 0.5 * (mu_z^2 + exp(log_var_z) - log_var_z - 1).sum(-1).mean() # KL[q(z|context)||N(0,I)]
|
|
57
|
+
output = linear_out(z)
|
|
40
58
|
loss = task_loss + beta * kl
|
|
41
59
|
```
|
|
42
60
|
|
|
61
|
+
If the bottleneck is applied directly on the attention simplex (e.g., noisy logits followed by softmax), the random variable is logistic-normal / Concrete-like. The closed-form Gaussian KL above no longer applies; use Monte Carlo KL, a Concrete KL approximation, or place the Gaussian KL on pre-softmax logits.
|
|
62
|
+
|
|
43
63
|
**Scheme C: Mutual Information Maximization Attention (DIM Style)**:
|
|
44
64
|
```python
|
|
45
65
|
# Directly maximize I(Z;Y) via InfoNCE, with KL constraining I(X;Z)
|
|
@@ -50,22 +70,22 @@ loss = -info_nce + beta * kl_bottleneck
|
|
|
50
70
|
```
|
|
51
71
|
|
|
52
72
|
## Implementable Architectures
|
|
53
|
-
- **IB-Sparse Attention**: KL
|
|
73
|
+
- **IB-Sparse Attention**: The IB compression term KL(attn || uniform) by itself pushes toward uniform (maximum entropy), but its tension with task_loss forces the attention to balance between "compressing all information" and "selectively transmitting useful information," implicitly producing non-uniform attention. For **explicit** sparsity induction (Top-K selection), use an entropy penalty `+beta * H(attn)` (minimize attention entropy → concentration), rather than relying on the IB compression term alone
|
|
54
74
|
- **IB Interpretation of Dropout**: Dropout is a form of stochastic information bottleneck -- randomly blocking information channels forces the model to learn robust representations. The dropout rate corresponds to the $\beta$ parameter
|
|
55
75
|
- **Multi-Head Information Allocation**: Different heads learn different information bottlenecks (different $\beta$); some heads transmit global information, others only local information
|
|
56
76
|
|
|
57
77
|
## GPU Feasibility
|
|
58
|
-
- **
|
|
59
|
-
- **
|
|
60
|
-
- **
|
|
61
|
-
- **
|
|
62
|
-
- **
|
|
63
|
-
- **
|
|
64
|
-
- **
|
|
65
|
-
- **
|
|
78
|
+
- **D1**: KL regularization involves element-wise operations; VIB reparameterization sampling is element-wise
|
|
79
|
+
- **D2**: The main body $QK^T$ and $attn \cdot V$ are standard GEMM; regularization introduces no new GEMM operations
|
|
80
|
+
- **D3**: KL regularization is $O(n)$ per token, adding no asymptotic complexity
|
|
81
|
+
- **D4**: VIB requires additional $\mu_z$ and $\log\sigma_z$, approximately doubling attention weight memory
|
|
82
|
+
- **D5**: log/exp in KL computations are stable under bf16 (standard log-softmax tricks)
|
|
83
|
+
- **D6**: Regularization can be computed in parallel with forward propagation, introducing no serial dependencies
|
|
84
|
+
- **D7[~]**: KL regularization by itself does not induce sparsity (pushes toward uniform distribution); for sparsity, use an entropy penalty +beta * H(attn), which then enables block-sparse acceleration
|
|
85
|
+
- **D8**: KL can be fused into the softmax kernel (FusedSoftmaxKL)
|
|
66
86
|
|
|
67
87
|
## Paper Phrasing
|
|
68
|
-
"We propose information bottleneck attention, which models attention as an information bottleneck
|
|
88
|
+
"We propose information bottleneck attention, which models attention as an information bottleneck optimization problem. By maximizing the mutual information between output and target while constraining redundant information transmission from the input, the IB compression term by itself pushes toward a uniform distribution (maximum entropy), and its tension with the task loss produces non-uniform attention; explicit sparsity induction requires an additional entropy penalty term."
|
|
69
89
|
|
|
70
90
|
## Risks
|
|
71
91
|
- **High Variance of Mutual Information Estimators**: The MINE/NWJ/InfoNCE estimators in Scheme C exhibit high variance in high-dimensional spaces, potentially causing training instability. It is recommended to first validate the basic effect of IB attention with Scheme A (KL regularization) before attempting the full IB objective.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# 信息瓶颈注意力 / Information Bottleneck Attention
|
|
2
|
-
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
3
|
|
|
4
4
|
## 适用问题
|
|
5
5
|
当注意力机制需要**选择性传递有用信息、抑制冗余/噪声信息**时,用信息瓶颈理论指导注意力权重的学习——使注意力分布最大化关于目标 $Y$ 的互信息 $I(Z;Y)$,同时最小化关于输入 $X$ 的互信息 $I(X;Z)$。典型场景:长文档摘要(大量无关 token 需被过滤)、多模态对齐(跨模态噪声抑制)、可解释性(注意力权重作为信息流的可视化)。
|
|
@@ -19,27 +19,47 @@
|
|
|
19
19
|
|
|
20
20
|
**方案 A:KL 正则化注意力(最简 IB 注意力)**:
|
|
21
21
|
```python
|
|
22
|
-
#
|
|
22
|
+
# ⚠ 注意 KL 方向的语义:
|
|
23
|
+
# KL(attn || uniform) = log(n) - H(attn)
|
|
24
|
+
# 最小化 +beta * KL(attn || uniform) = 最大化 H(attn) → 推向均匀分布(最大熵/最大压缩)
|
|
25
|
+
# 这本身不诱导稀疏性!稀疏性来自压缩项与 task_loss 的张力。
|
|
26
|
+
# 若需要显式诱导稀疏注意力,应使用熵惩罚 +beta * H(attn)(最小化熵)。
|
|
27
|
+
|
|
23
28
|
scores = (Q @ K.T) / sqrt(d)
|
|
24
29
|
attn = softmax(scores)
|
|
25
30
|
uniform_prior = ones_like(attn) / n
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
|
|
32
|
+
# 正确的 IB 压缩项:KL(q(z|x) || p(z)),p(z) 为固定先验
|
|
33
|
+
# 最小化此项 → 压缩信息(推向先验),与 task_loss 的张力产生有用注意力
|
|
34
|
+
kl_compression = (attn * (log(attn + eps) - log(uniform_prior))).sum(dim=-1).mean()
|
|
35
|
+
# = log(n) - H(attn)
|
|
36
|
+
|
|
37
|
+
# IB 目标:min task_loss + beta * I(X;Z),其中 I(X;Z) ≈ KL(attn || prior)
|
|
38
|
+
loss_ib = task_loss + beta * kl_compression
|
|
39
|
+
# 注意:此项单独作用会推向均匀;稀疏性来自 task_loss 的反向拉力
|
|
40
|
+
|
|
41
|
+
# 替代方案:显式稀疏性诱导(非 IB 压缩,而是熵惩罚)
|
|
42
|
+
entropy = -(attn * log(attn + eps)).sum(dim=-1).mean()
|
|
43
|
+
loss_sparse = task_loss + beta * entropy # 最小化熵 → 注意力集中 → 隐式 Top-K
|
|
44
|
+
|
|
28
45
|
output = attn @ V
|
|
29
46
|
```
|
|
30
47
|
|
|
31
48
|
**方案 B:变分信息瓶颈注意力(VIB-Attention)**:
|
|
32
49
|
```python
|
|
33
|
-
#
|
|
50
|
+
# 引入随机连续瓶颈 Z ~ q(Z|context),限制 value 聚合后的信息通过量
|
|
34
51
|
scores = (Q @ K.T) / sqrt(d)
|
|
35
52
|
attn = softmax(scores)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
53
|
+
context = attn @ V
|
|
54
|
+
mu_z, log_var_z = linear_mu(context), linear_logvar(context)
|
|
55
|
+
z = mu_z + exp(0.5 * log_var_z) * randn_like(mu_z) # 高斯重参数化
|
|
56
|
+
kl = 0.5 * (mu_z^2 + exp(log_var_z) - log_var_z - 1).sum(-1).mean() # KL[q(z|context)||N(0,I)]
|
|
57
|
+
output = linear_out(z)
|
|
40
58
|
loss = task_loss + beta * kl
|
|
41
59
|
```
|
|
42
60
|
|
|
61
|
+
若瓶颈直接作用在 attention simplex 上(如对 logits 加噪后 softmax),变量分布是 logistic-normal / Concrete 类,不能再使用普通高斯到标准正态的闭式 KL;应使用 Monte Carlo KL、Concrete KL 近似,或把 KL 放在 softmax 前的 logits 高斯变量上。
|
|
62
|
+
|
|
43
63
|
**方案 C:互信息最大化注意力(DIM 风格)**:
|
|
44
64
|
```python
|
|
45
65
|
# 用 InfoNCE 直接最大化 I(Z;Y),配合 KL 约束 I(X;Z)
|
|
@@ -50,23 +70,23 @@ loss = -info_nce + beta * kl_bottleneck
|
|
|
50
70
|
```
|
|
51
71
|
|
|
52
72
|
## 可实现结构
|
|
53
|
-
- **IB-Sparse Attention
|
|
73
|
+
- **IB-Sparse Attention**:IB 压缩项 KL(attn || uniform) 本身推向均匀分布(最大熵),但与 task_loss 的张力迫使注意力在"压缩所有信息"与"选择性传递有用信息"之间取舍,从而隐式产生非均匀注意力。若要**显式**诱导稀疏(Top-K 选择),应使用熵惩罚 `+beta * H(attn)`(最小化注意力熵 → 集中化),而非依赖 IB 压缩项
|
|
54
74
|
- **Dropout 的 IB 解释**:Dropout 是一种随机信息瓶颈——随机阻断部分信息通道,迫使模型学习鲁棒表示。Dropout rate 对应 $\beta$ 参数
|
|
55
75
|
- **Multi-Head 信息分配**:不同 head 学习不同的信息瓶颈(不同 $\beta$),有的 head 传递全局信息,有的只传递局部信息
|
|
56
76
|
|
|
57
77
|
## GPU 可行性
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
78
|
+
- **D1[v]**:KL 正则项为逐元素运算,VIB 重参数化采样为逐元素
|
|
79
|
+
- **D2[v]**:主体 $QK^T$ 和 $attn \cdot V$ 为标准 GEMM,正则项不引入新 GEMM
|
|
80
|
+
- **D3[v]**:KL 正则 $O(n)$ 逐 token,不增加渐近复杂度
|
|
81
|
+
- **D4[~]**:VIB 需额外 $\mu_z$ 和 $\log\sigma_z$,约 2x 注意力权重显存
|
|
82
|
+
- **D5[v]**:KL 中 log/exp 在 bf16 下稳定(标准 log-softmax 技巧)
|
|
83
|
+
- **D6[v]**:正则项可与前向传播并行,不引入串行依赖
|
|
84
|
+
- **D7[~]**:KL 正则本身不诱导稀疏性(推向均匀分布);若需稀疏,应改用熵惩罚 +beta * H(attn),此时可 block-sparse 加速
|
|
85
|
+
- **D8[v]**:KL 可融入 softmax kernel(FusedSoftmaxKL)
|
|
66
86
|
|
|
67
87
|
## 论文表述方式
|
|
68
|
-
"
|
|
88
|
+
"我们提出信息瓶颈注意力机制,通过将注意力建模为信息瓶颈优化问题,在最大化输出与目标的互信息的同时约束输入信息的冗余传递。IB 压缩项本身推向均匀分布(最大熵),与任务损失的张力产生非均匀注意力;若要显式诱导稀疏性,需额外使用熵惩罚项。"
|
|
69
89
|
|
|
70
90
|
## 风险
|
|
71
91
|
- **互信息估计的方差问题**:方案 C 中的 MINE/NWJ/InfoNCE 估计器在高维空间中方差大,可能导致训练不稳定。建议先用方案 A(KL 正则)验证 IB 注意力的基本效果,再尝试完整 IB 目标。
|
|
72
|
-
- **$\beta$ 调参困难**:$\beta$ 控制压缩-预测权衡,不同任务最优 $\beta$ 差异大。$\beta$ 过小退化为标准注意力,$\beta$ 过大导致欠拟合。建议自适应 $\beta$ 调度或信息平面监控。
|
|
92
|
+
- **$\beta$ 调参困难**:$\beta$ 控制压缩-预测权衡,不同任务最优 $\beta$ 差异大。$\beta$ 过小退化为标准注意力,$\beta$ 过大导致欠拟合。建议自适应 $\beta$ 调度或信息平面监控。
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Projection Attention
|
|
2
|
-
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as [v] verified / [~] retrofittable (needs validation) / [x] infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
3
|
|
|
4
4
|
## Applicable Problems
|
|
5
5
|
When the token/key space dimensionality is too high, the standard attention $Q K^T$ inner product tends to become uniform in high-dimensional spaces ("attention collapse"). In such cases, key/query vectors must first be projected onto a **subspace with superior geometric structure** before computing attention. Typical scenarios include: KV-Cache compression for long-context LLMs, attention sparsification in high-dimensional embedding spaces, and attention alignment of heterogeneous features in multimodal fusion.
|
|
6
6
|
|
|
7
7
|
## Mathematical Inspiration
|
|
8
8
|
- Lenses: [projection, spectral, probabilistic]
|
|
9
|
-
- Knowledge: [`../../knowledge-base/probability/concentration-inequality.md` (high-dimensional concentration inequalities provide theoretical explanation for attention collapse), `../../knowledge-base/probability/entropy.md` (entropy of attention distributions as a quality metric)]
|
|
9
|
+
- Knowledge: [`../../knowledge-base/probability/concentration-inequality.en.md` (high-dimensional concentration inequalities provide theoretical explanation for attention collapse), `../../knowledge-base/probability/entropy.en.md` (entropy of attention distributions as a quality metric)]
|
|
10
10
|
|
|
11
11
|
## Required Mathematical Knowledge
|
|
12
12
|
- **Johnson-Lindenstrauss Lemma**: High-dimensional point sets can be projected onto an $O(\log n / \epsilon^2)$-dimensional subspace while approximately preserving distances
|
|
@@ -31,7 +31,7 @@ scores = (Q @ P_Q) @ (K @ P_K).T / sqrt(r)
|
|
|
31
31
|
attn = softmax(scores) @ V
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
2. **Random Fixed Projection** (
|
|
34
|
+
2. **Random Fixed Projection** (distance-preservation bound when JL conditions hold):
|
|
35
35
|
```
|
|
36
36
|
P = random_gaussian(d, r) / sqrt(r) # fixed, not trained
|
|
37
37
|
scores = (Q @ P) @ (K @ P).T / sqrt(r)
|
|
@@ -51,17 +51,17 @@ scores = (Q @ eigenvecs) @ (K @ eigenvecs).T / sqrt(r)
|
|
|
51
51
|
- **Hierarchical Projection**: Shallow layers use small $r$ (coarse filtering), while deep layers use large $r$ (fine ranking)
|
|
52
52
|
|
|
53
53
|
## GPU Feasibility
|
|
54
|
-
- **
|
|
55
|
-
- **
|
|
56
|
-
- **
|
|
57
|
-
- **
|
|
58
|
-
- **
|
|
59
|
-
- **
|
|
60
|
-
- **
|
|
61
|
-
- **
|
|
54
|
+
- **D1**: Projection = matrix multiplication, attention = matrix multiplication chain; entirely tensor operations
|
|
55
|
+
- **D2**: $Q P_Q$ and $K P_K$ are both standard GEMM operations, fully utilizing Tensor Cores
|
|
56
|
+
- **D3**: Projection cost $O(ndr)$ is far below attention cost $O(n^2 d)$, and after projection $r \ll d$ reduces attention to $O(n^2 r)$
|
|
57
|
+
- **D4**: Key-cache compressed by $d/r$; V-cache must be handled separately
|
|
58
|
+
- **D5**: Projection matrices are orthogonal or near-orthogonal, yielding numerical stability; bf16 is acceptable
|
|
59
|
+
- **D6**: Projection can be pipelined with attention; Multi-Head is naturally parallel across heads
|
|
60
|
+
- **D7**: Projection matrices are inherently dense; sparse projections (CountSketch) may introduce gather/scatter operations
|
|
61
|
+
- **D8[~] Retrofittable, needs kernel-level validation**: Projection may be integrated into a FlashAttention-style kernel, but requires kernel-level verification
|
|
62
62
|
|
|
63
63
|
## Paper Phrasing
|
|
64
|
-
"We decompose attention computation into
|
|
64
|
+
"We decompose attention computation into low-dimensional subspace projection and projected-space attention to reduce Key dimensionality; V-cache must be handled separately. With an independent random projection and the sample-size / target-dimension conditions of the Johnson-Lindenstrauss lemma, Euclidean distances of the projected objects can be approximately preserved. This does not automatically guarantee softmax-attention quality, so attention/output error and task metrics must be reported."
|
|
65
65
|
|
|
66
66
|
## Risks
|
|
67
67
|
- **Projection Direction Degeneracy**: Learnable projections may collapse onto a few directions (deterioration of the condition number of $P^T P$), causing attention distributions to degenerate. Orthogonal regularization $\|P^T P - I\|_F^2$ is required.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# 投影注意力 / Projection Attention
|
|
2
|
-
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache
|
|
2
|
+
> **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
|
|
3
3
|
|
|
4
4
|
## 适用问题
|
|
5
5
|
当输入的 token/key 空间维度过高,标准注意力的 $Q K^T$ 内积在高维空间中趋于均匀("注意力坍缩")时,需要先将 key/query 投影到一个**几何结构更优的子空间**再做注意力计算。典型场景:长上下文 LLM 的 KV-Cache 压缩、高维嵌入空间的注意力稀疏化、多模态融合中异构特征的注意力对齐。
|
|
@@ -31,7 +31,7 @@ scores = (Q @ P_Q) @ (K @ P_K).T / sqrt(r)
|
|
|
31
31
|
attn = softmax(scores) @ V
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
2.
|
|
34
|
+
2. **随机固定投影**(满足 JL 条件时可给距离保持界):
|
|
35
35
|
```
|
|
36
36
|
P = random_gaussian(d, r) / sqrt(r) # 固定不训练
|
|
37
37
|
scores = (Q @ P) @ (K @ P).T / sqrt(r)
|
|
@@ -51,17 +51,17 @@ scores = (Q @ eigenvecs) @ (K @ eigenvecs).T / sqrt(r)
|
|
|
51
51
|
- **分层投影**:浅层用小 $r$(粗筛),深层用大 $r$(精排)
|
|
52
52
|
|
|
53
53
|
## GPU 可行性
|
|
54
|
-
-
|
|
55
|
-
-
|
|
56
|
-
-
|
|
57
|
-
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
54
|
+
- **D1[v]**:投影 = 矩阵乘,注意力 = 矩阵乘链,全程张量运算
|
|
55
|
+
- **D2[v]**:$Q P_Q$ 和 $K P_K$ 均为标准 GEMM,吃满 Tensor Core
|
|
56
|
+
- **D3[v]**:投影 $O(ndr)$ 远低于注意力 $O(n^2 d)$,且投影后 $r \ll d$ 使注意力 $O(n^2 r)$
|
|
57
|
+
- **D4[v]**:Key-cache 压缩 $d/r$ 倍(V-cache 需独立处理),直接降低显存峰值
|
|
58
|
+
- **D5[v]**:投影矩阵正交/近正交时数值稳定,bf16 可接受
|
|
59
|
+
- **D6[v]**:投影可与注意力流水线并行,Multi-Head 天然跨头并行
|
|
60
|
+
- **D7[~]**:投影矩阵本身稠密;若用稀疏投影(CountSketch),可能引入 gather/scatter
|
|
61
|
+
- **D8[~]可改造需验证**:投影可融入 FlashAttention 的 online softmax 循环中
|
|
62
62
|
|
|
63
63
|
## 论文表述方式
|
|
64
|
-
"
|
|
64
|
+
"我们将注意力计算分解为低维子空间投影与投影空间注意力两步,以降低 Key 表示维度;V-cache 需独立处理。若使用独立随机投影并满足 Johnson-Lindenstrauss 的样本规模与维度条件,可近似保持投影对象的欧氏距离;这不自动保证 softmax attention 质量,需报告 attention/output 误差与任务指标。"
|
|
65
65
|
|
|
66
66
|
## 风险
|
|
67
67
|
- **投影方向退化**:可学习投影可能坍缩到少数方向($P^T P$ 条件数恶化),导致注意力分布退化。需加正交正则化 $\|P^T P - I\|_F^2$。
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
# Spectral Attention
|
|
2
|
-
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as
|
|
2
|
+
> **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as [v] verified / [~] retrofittable (needs validation) / [x] infeasible. Unmarked claims are theoretically possible but require engineering validation.
|
|
3
3
|
|
|
4
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.
|
|
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 **position-dependent** attention (requires circulant/Toeplitz structure assumption; does not apply to general content-dependent softmax attention).
|
|
6
6
|
|
|
7
7
|
## Mathematical Inspiration
|
|
8
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)]
|
|
9
|
+
- Knowledge: [`../../knowledge-base/probability/entropy.en.md` (spectral entropy for measuring signal complexity), `../../knowledge-base/probability/concentration-inequality.en.md` (frequency-domain concentration inequalities)]
|
|
10
10
|
|
|
11
11
|
## Required Mathematical Knowledge
|
|
12
12
|
- **Discrete Fourier Transform (DFT/FFT)**: $O(n \log n)$ frequency-domain transform and the cyclic convolution theorem
|
|
13
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
|
|
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.en.md` Ch.4, Ch.11)
|
|
15
15
|
|
|
16
16
|
## AI Module Form
|
|
17
17
|
|
|
18
18
|
**Core Idea**: Transform attention from the spatial-domain $Q K^T$ into diagonal/sparse operations in the spectral domain:
|
|
19
19
|
|
|
20
|
+
> **Critical Prerequisite**: The equivalence "spectral-domain diagonalization = attention" **holds only when the attention matrix is circulant or Toeplitz**, i.e., attention weights depend solely on relative position $a_{ij} = f(i-j)$ and are independent of token content. In this case, the cyclic convolution theorem guarantees that FFT diagonalizes the convolution kernel. For standard content-dependent softmax attention $\text{softmax}(QK^T/\sqrt{d})$, the attention matrix is determined by query/key content and is generally **not** circulant or Toeplitz; therefore FFT element-wise multiplication **cannot** equivalently replace the $QK^T$ computation. Schemes A/C are essentially position-dependent convolutional attention approximations, not exact substitutes for general softmax attention.
|
|
21
|
+
|
|
20
22
|
**Scheme A: FFT-Accelerated Attention (Time Series)**:
|
|
21
23
|
```python
|
|
22
24
|
# Express cyclic convolution as element-wise multiplication in the frequency domain
|
|
@@ -54,18 +56,30 @@ attn = ifft(scores_freq)
|
|
|
54
56
|
- **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
57
|
|
|
56
58
|
## GPU Feasibility
|
|
57
|
-
- **
|
|
58
|
-
- **
|
|
59
|
-
- **
|
|
60
|
-
- **
|
|
61
|
-
- **
|
|
62
|
-
- **
|
|
63
|
-
- **
|
|
64
|
-
- **
|
|
59
|
+
- **D1**: FFT and matrix multiplication are both standard tensor operations
|
|
60
|
+
- **D2**: Spectral projection $U^T Q$ is a standard GEMM; although FFT is not GEMM, highly optimized cuFFT implementations are available
|
|
61
|
+
- **D3[~]**: FFT attention $O(n \log n \cdot d)$, far superior to $O(n^2 d)$ -- **but only when attention has translation-invariant / position-dependent structure** (e.g., convolutional attention). General content-dependent softmax attention remains $O(n^2 d)$; linear attention approximations can achieve $O(n d^2)$, but via a different mechanism unrelated to FFT spectral methods.
|
|
62
|
+
- **D4**: Frequency-domain representation introduces no extra dimensions; spectral projection can reduce to $k \ll n$ dimensions
|
|
63
|
+
- **D5**: Complex-valued FFT suffers precision loss under fp16; fp32 or real-valued FFT (RFFT) is required
|
|
64
|
+
- **D6**: FFT can be parallelized across batch/head; cuFFT supports multi-stream execution
|
|
65
|
+
- **D7**: High-frequency components can be truncated in the spectral domain (structured sparsity), retaining only top-k frequencies
|
|
66
|
+
- **D8**: Fusing FFT with attention requires custom kernels; no ready-made fusion exists in standard libraries
|
|
67
|
+
|
|
68
|
+
**Quantitative assessment example** (standard transformer, d=128, n=2048, h=16):
|
|
69
|
+
- D3: FFT path FLOPs ≈ 2·n·log₂(n)·d ≈ 2·2048·11·128 ≈ 5.8M vs standard attention 2·n²·d ≈ 1.1G (only when attention is convolutional)
|
|
70
|
+
- D4: No n×n attention matrix materialization; FFT intermediates O(n·d) ≈ 1MB
|
|
71
|
+
- D5: FFT under bf16 has twiddle factor error ~10⁻³, acceptable
|
|
72
|
+
- D8: FFT + pointwise can be fused into a single CUDA kernel
|
|
65
73
|
|
|
66
74
|
## 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."
|
|
75
|
+
"We propose a spectral-domain attention mechanism that transforms attention computation into the Fourier/Laplacian spectral domain, leveraging the cyclic convolution theorem to reduce **translation-invariant** 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. Note: this acceleration **requires position-dependent (not content-dependent) attention structure**; for general content-dependent softmax attention, the spectral-domain equivalence does not hold."
|
|
76
|
+
|
|
77
|
+
## Applicability Conditions
|
|
78
|
+
- **Core restriction of FFT spectral attention**: The FFT acceleration in Schemes A/C **requires the attention matrix to be circulant or Toeplitz**, i.e., attention weights depend only on relative position $a_{ij} = f(i-j)$ and are independent of the specific token content (query/key vectors).
|
|
79
|
+
- **When the condition is NOT satisfied**: Standard softmax attention $\text{softmax}(QK^T/\sqrt{d})$ in most NLP tasks is content-dependent -- attention weights are jointly determined by query and key content, and the resulting attention matrix generally lacks circulant/Toeplitz structure. In this case, FFT element-wise multiplication **cannot** replace the $QK^T$ operation, and the $O(n \log n)$ complexity advantage does not hold.
|
|
80
|
+
- **When the condition IS satisfied**: Positional encoding (e.g., frequency components of RoPE/ALiBi), periodic convolutional kernels for time series, fixed-pattern attention with translation-invariant priors. Scheme B (graph spectral attention) is not subject to this restriction, as it projects onto graph Laplacian eigenbases rather than exploiting the cyclic convolution theorem.
|
|
68
81
|
|
|
69
82
|
## Risks
|
|
83
|
+
- **[x] Content-dependent attention incompatibility**: The core assumption of FFT spectral methods (circulant/Toeplitz structure) is **fundamentally incompatible** with the mainstream content-dependent softmax attention used in NLP. Applying FFT methods indiscriminately to general attention mechanisms constitutes a mathematical error -- they compute different quantities. Any usage must explicitly declare the position-dependence assumption being made.
|
|
70
84
|
- **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
85
|
- **Graph Laplacian Precomputation Cost**: Eigendecomposition $O(n^3)$ is infeasible for large-scale graphs, requiring approximations (Nystrom/Lanczos), and dynamic graphs necessitate recomputation.
|