math-skill 2.0.1 → 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.
Files changed (206) hide show
  1. package/README.en-US.md +167 -175
  2. package/README.md +167 -175
  3. package/agents/math-critic.en.md +235 -0
  4. package/agents/math-critic.md +10 -8
  5. package/commands/ask.md +3 -12
  6. package/design-patterns/attention/equivariant-attention.en.md +72 -0
  7. package/design-patterns/attention/equivariant-attention.md +72 -0
  8. package/design-patterns/attention/geometry-aware-attention.en.md +73 -0
  9. package/design-patterns/attention/geometry-aware-attention.md +72 -0
  10. package/design-patterns/attention/information-bottleneck-attention.en.md +72 -0
  11. package/design-patterns/attention/information-bottleneck-attention.md +72 -0
  12. package/design-patterns/attention/projection-attention.en.md +68 -0
  13. package/design-patterns/attention/projection-attention.md +68 -0
  14. package/design-patterns/attention/spectral-attention.en.md +71 -0
  15. package/design-patterns/attention/spectral-attention.md +71 -0
  16. package/design-patterns/compression/leverage-score-selection.en.md +71 -0
  17. package/design-patterns/compression/leverage-score-selection.md +71 -0
  18. package/design-patterns/compression/low-rank-kv-cache.en.md +60 -0
  19. package/design-patterns/compression/low-rank-kv-cache.md +60 -0
  20. package/design-patterns/compression/spectral-token-pruning.en.md +61 -0
  21. package/design-patterns/compression/spectral-token-pruning.md +61 -0
  22. package/design-patterns/compression/topology-preserving-compression.en.md +64 -0
  23. package/design-patterns/compression/topology-preserving-compression.md +64 -0
  24. package/design-patterns/loss/constraint-penalty.en.md +67 -0
  25. package/design-patterns/loss/constraint-penalty.md +78 -0
  26. package/design-patterns/loss/contrastive-loss.en.md +59 -0
  27. package/design-patterns/loss/contrastive-loss.md +68 -0
  28. package/design-patterns/loss/information-bottleneck-loss.en.md +69 -0
  29. package/design-patterns/loss/information-bottleneck-loss.md +74 -0
  30. package/design-patterns/loss/orthogonality-loss.en.md +56 -0
  31. package/design-patterns/loss/orthogonality-loss.md +60 -0
  32. package/design-patterns/loss/variational-loss.en.md +61 -0
  33. package/design-patterns/loss/variational-loss.md +71 -0
  34. package/design-patterns/representation/equivariant-split.en.md +87 -0
  35. package/design-patterns/representation/equivariant-split.md +89 -0
  36. package/design-patterns/representation/manifold-representation.en.md +85 -0
  37. package/design-patterns/representation/manifold-representation.md +87 -0
  38. package/design-patterns/representation/shared-private-decomposition.en.md +84 -0
  39. package/design-patterns/representation/shared-private-decomposition.md +86 -0
  40. package/design-patterns/representation/subspace-alignment.en.md +90 -0
  41. package/design-patterns/representation/subspace-alignment.md +92 -0
  42. package/design-patterns/routing/graph-routing.en.md +79 -0
  43. package/design-patterns/routing/graph-routing.md +81 -0
  44. package/design-patterns/routing/moe-routing.en.md +85 -0
  45. package/design-patterns/routing/moe-routing.md +87 -0
  46. package/design-patterns/routing/optimal-transport-routing.en.md +68 -0
  47. package/design-patterns/routing/optimal-transport-routing.md +72 -0
  48. package/design-patterns/routing/spectral-clustering-routing.en.md +88 -0
  49. package/design-patterns/routing/spectral-clustering-routing.md +90 -0
  50. package/knowledge-base/differential-geometry/connection.en.md +51 -0
  51. package/knowledge-base/differential-geometry/connection.md +51 -0
  52. package/knowledge-base/differential-geometry/curvature.en.md +49 -0
  53. package/knowledge-base/differential-geometry/curvature.md +49 -0
  54. package/knowledge-base/differential-geometry/geodesic.en.md +51 -0
  55. package/knowledge-base/differential-geometry/geodesic.md +51 -0
  56. package/knowledge-base/differential-geometry/manifold.en.md +47 -0
  57. package/knowledge-base/differential-geometry/manifold.md +47 -0
  58. package/knowledge-base/differential-geometry/metric-tensor.en.md +50 -0
  59. package/knowledge-base/differential-geometry/metric-tensor.md +50 -0
  60. package/knowledge-base/differential-geometry/tangent-space.en.md +48 -0
  61. package/knowledge-base/differential-geometry/tangent-space.md +48 -0
  62. package/knowledge-base/information-geometry/fisher-metric.en.md +56 -0
  63. package/knowledge-base/information-geometry/fisher-metric.md +56 -0
  64. package/knowledge-base/information-geometry/natural-gradient.en.md +54 -0
  65. package/knowledge-base/information-geometry/natural-gradient.md +54 -0
  66. package/knowledge-base/lie-theory/equivariance.en.md +54 -0
  67. package/knowledge-base/lie-theory/equivariance.md +54 -0
  68. package/knowledge-base/lie-theory/group-action.en.md +51 -0
  69. package/knowledge-base/lie-theory/group-action.md +51 -0
  70. package/knowledge-base/lie-theory/lie-algebra.en.md +51 -0
  71. package/knowledge-base/lie-theory/lie-algebra.md +51 -0
  72. package/knowledge-base/lie-theory/lie-group.en.md +51 -0
  73. package/knowledge-base/lie-theory/lie-group.md +51 -0
  74. package/knowledge-base/lie-theory/representation.en.md +52 -0
  75. package/knowledge-base/lie-theory/representation.md +52 -0
  76. package/knowledge-base/matrix-analysis/low-rank-approximation.en.md +49 -0
  77. package/knowledge-base/matrix-analysis/low-rank-approximation.md +49 -0
  78. package/knowledge-base/matrix-analysis/matrix-perturbation.en.md +49 -0
  79. package/knowledge-base/matrix-analysis/matrix-perturbation.md +49 -0
  80. package/knowledge-base/matrix-analysis/positive-semidefinite.en.md +50 -0
  81. package/knowledge-base/matrix-analysis/positive-semidefinite.md +50 -0
  82. package/knowledge-base/matrix-analysis/projection.en.md +47 -0
  83. package/knowledge-base/matrix-analysis/projection.md +47 -0
  84. package/knowledge-base/matrix-analysis/spectral-decomposition.en.md +48 -0
  85. package/knowledge-base/matrix-analysis/spectral-decomposition.md +48 -0
  86. package/knowledge-base/optimization/constrained-optimization.en.md +53 -0
  87. package/knowledge-base/optimization/constrained-optimization.md +53 -0
  88. package/knowledge-base/optimization/convex-optimization.en.md +50 -0
  89. package/knowledge-base/optimization/convex-optimization.md +50 -0
  90. package/knowledge-base/optimization/lagrangian-duality.en.md +50 -0
  91. package/knowledge-base/optimization/lagrangian-duality.md +50 -0
  92. package/knowledge-base/optimization/proximal-method.en.md +52 -0
  93. package/knowledge-base/optimization/proximal-method.md +52 -0
  94. package/knowledge-base/optimization/riemannian-optimization.en.md +51 -0
  95. package/knowledge-base/optimization/riemannian-optimization.md +51 -0
  96. package/knowledge-base/overview.en.md +39 -0
  97. package/knowledge-base/overview.md +29 -218
  98. package/knowledge-base/probability/concentration-inequality.en.md +45 -0
  99. package/knowledge-base/probability/concentration-inequality.md +45 -0
  100. package/knowledge-base/probability/entropy.en.md +47 -0
  101. package/knowledge-base/probability/entropy.md +47 -0
  102. package/knowledge-base/probability/fisher-information.en.md +48 -0
  103. package/knowledge-base/probability/fisher-information.md +48 -0
  104. package/knowledge-base/probability/information-bottleneck.en.md +49 -0
  105. package/knowledge-base/probability/information-bottleneck.md +49 -0
  106. package/knowledge-base/probability/kl-divergence.en.md +51 -0
  107. package/knowledge-base/probability/kl-divergence.md +51 -0
  108. package/knowledge-base/topology/euler-characteristic.en.md +54 -0
  109. package/knowledge-base/topology/euler-characteristic.md +54 -0
  110. package/knowledge-base/topology/fundamental-group.en.md +52 -0
  111. package/knowledge-base/topology/fundamental-group.md +52 -0
  112. package/knowledge-base/topology/persistent-homology.en.md +54 -0
  113. package/knowledge-base/topology/persistent-homology.md +54 -0
  114. package/lenses/algorithmic.en.md +49 -0
  115. package/lenses/algorithmic.md +49 -0
  116. package/lenses/axiomatization.en.md +48 -0
  117. package/lenses/axiomatization.md +48 -0
  118. package/lenses/categorical.en.md +48 -0
  119. package/lenses/categorical.md +48 -0
  120. package/lenses/causal.en.md +49 -0
  121. package/lenses/causal.md +49 -0
  122. package/lenses/duality.en.md +47 -0
  123. package/lenses/duality.md +47 -0
  124. package/lenses/game.en.md +50 -0
  125. package/lenses/game.md +50 -0
  126. package/lenses/geometric.en.md +53 -0
  127. package/lenses/geometric.md +53 -0
  128. package/lenses/local-to-global.en.md +52 -0
  129. package/lenses/local-to-global.md +52 -0
  130. package/lenses/perturbation.en.md +51 -0
  131. package/lenses/perturbation.md +51 -0
  132. package/lenses/probabilistic.en.md +48 -0
  133. package/lenses/probabilistic.md +48 -0
  134. package/lenses/projection.en.md +50 -0
  135. package/lenses/projection.md +50 -0
  136. package/lenses/spectral.en.md +52 -0
  137. package/lenses/spectral.md +52 -0
  138. package/lenses/symmetry.en.md +49 -0
  139. package/lenses/symmetry.md +49 -0
  140. package/lenses/topological.en.md +48 -0
  141. package/lenses/topological.md +48 -0
  142. package/lenses/variational.en.md +48 -0
  143. package/lenses/variational.md +48 -0
  144. package/package.json +11 -3
  145. package/references/agentic-workflow.en.md +53 -0
  146. package/references/agentic-workflow.md +2 -0
  147. package/references/books/abstract-algebra.md +9 -5
  148. package/references/books/algebraic-geometry-rising-sea.md +9 -5
  149. package/references/books/differential-geometry.md +7 -3
  150. package/references/books/matrix-analysis.md +8 -4
  151. package/references/books/micro-lie-theory.md +7 -3
  152. package/references/books/optimization-ml.md +5 -1
  153. package/references/books/smooth-manifolds.md +7 -3
  154. package/references/gpu-friendly-math.en.md +65 -0
  155. package/references/gpu-friendly-math.md +4 -2
  156. package/references/inspiration.en.md +113 -0
  157. package/references/inspiration.md +4 -2
  158. package/skills/math-research-activator/SKILL.en.md +187 -0
  159. package/skills/math-research-activator/SKILL.md +162 -108
  160. package/skills/math-research-activator/original-texts.en.md +105 -0
  161. package/skills/math-research-activator/original-texts.md +11 -11
  162. package/commands/abstraction.md +0 -13
  163. package/commands/algorithmic-thinking.md +0 -13
  164. package/commands/axiomatization.md +0 -13
  165. package/commands/causal-inference.md +0 -13
  166. package/commands/discrete-combinatorial.md +0 -13
  167. package/commands/game-theory.md +0 -13
  168. package/commands/induction-analogy.md +0 -13
  169. package/commands/information-theory.md +0 -13
  170. package/commands/logic-deduction.md +0 -13
  171. package/commands/modeling.md +0 -13
  172. package/commands/optimization.md +0 -13
  173. package/commands/probability-statistics.md +0 -13
  174. package/commands/symmetry-invariance.md +0 -13
  175. package/commands/topological-thinking.md +0 -13
  176. package/commands/transformation.md +0 -13
  177. package/skills/abstraction/SKILL.md +0 -119
  178. package/skills/abstraction/original-texts.md +0 -175
  179. package/skills/algorithmic-thinking/SKILL.md +0 -136
  180. package/skills/algorithmic-thinking/original-texts.md +0 -256
  181. package/skills/axiomatization/SKILL.md +0 -149
  182. package/skills/axiomatization/original-texts.md +0 -160
  183. package/skills/causal-inference/SKILL.md +0 -149
  184. package/skills/causal-inference/original-texts.md +0 -167
  185. package/skills/discrete-combinatorial/SKILL.md +0 -129
  186. package/skills/discrete-combinatorial/original-texts.md +0 -185
  187. package/skills/game-theory/SKILL.md +0 -121
  188. package/skills/game-theory/original-texts.md +0 -131
  189. package/skills/induction-analogy/SKILL.md +0 -150
  190. package/skills/induction-analogy/original-texts.md +0 -154
  191. package/skills/information-theory/SKILL.md +0 -138
  192. package/skills/information-theory/original-texts.md +0 -127
  193. package/skills/logic-deduction/SKILL.md +0 -133
  194. package/skills/logic-deduction/original-texts.md +0 -173
  195. package/skills/modeling/SKILL.md +0 -137
  196. package/skills/modeling/original-texts.md +0 -165
  197. package/skills/optimization/SKILL.md +0 -133
  198. package/skills/optimization/original-texts.md +0 -168
  199. package/skills/probability-statistics/SKILL.md +0 -149
  200. package/skills/probability-statistics/original-texts.md +0 -193
  201. package/skills/symmetry-invariance/SKILL.md +0 -137
  202. package/skills/symmetry-invariance/original-texts.md +0 -221
  203. package/skills/topological-thinking/SKILL.md +0 -126
  204. package/skills/topological-thinking/original-texts.md +0 -164
  205. package/skills/transformation/SKILL.md +0 -122
  206. package/skills/transformation/original-texts.md +0 -216
@@ -0,0 +1,68 @@
1
+ # Contrastive Loss(对比损失)
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
3
+
4
+ ## 适用问题
5
+ 当需要让模型学会"什么和什么相似、什么和什么不同"时使用。典型场景:
6
+ (1) 同一输入的不同增强视图应拉近(正对),不同输入应推远(负对);
7
+ (2) Shared 表示应包含跨任务共性,Private 表示应区分任务特异性;
8
+ (3) 专家嵌入空间中相似输入应路由到同一专家。核心诉求:**学习相对关系而非绝对值**。
9
+
10
+ ## 数学思想来源
11
+ - 透镜:lenses/geometric.md(度量空间与距离函数)、lenses/probabilistic.md(互信息最大化)
12
+ - 知识:knowledge-base/probability/entropy.md(条件分布与似然)、
13
+ knowledge-base/differential-geometry/manifold.md(测地线与曲率)
14
+
15
+ ## 需要的数学知识
16
+ - **InfoNCE 损失**:L = -log[exp(sim(q,k⁺)/τ) / Σ_j exp(sim(q,k_j)/τ)]
17
+ 本质是对比学习中的互信息下界估计,τ 为温度参数控制分布锐度
18
+ - **Margin-based 度量学习**:Triplet Loss = max(0, d(a,p) - d(a,n) + margin)
19
+ 在度量空间中显式拉开正负对距离差
20
+ - **NT-Xent (Normalized Temperature-scaled Cross Entropy)**:
21
+ 在单位球面 S^{d-1} 上的 softmax 对比,归一化消除尺度影响
22
+ - **去偏对比学习 (Debiased Contrastive)**:
23
+ 修正负样本中的假阴性问题,使用先验 τ⁺ 估计真实负样本分布
24
+
25
+ ## AI 模块形式
26
+ ```
27
+ 模块:ContrastiveLoss
28
+ 输入:锚点 z_a ∈ R^{B×d},正样本 z_p ∈ R^{B×d},负样本库 z_n ∈ R^{N×d}
29
+
30
+ 核心公式 (InfoNCE + 温度缩放):
31
+ sim(q, k) = q^T k / (‖q‖ · ‖k‖) // cosine 相似度
32
+ logits_i = [sim(z_a_i, z_p_i)] ⊕ [sim(z_a_i, z_n_j)]_{j=1}^N // 拼接
33
+ L_contrast = -1/B · Σ_i log( exp(logits_i[0]/τ) / Σ_j exp(logits_i[j]/τ) )
34
+
35
+ Queue 机制(MoCo 风格):
36
+ z_n = FIFO_queue.enqueue(z_p.detach()) // 负样本队列,容量 N >> B
37
+ // 队列中存储的是历史 batch 的编码,增大负样本数而不增加显存
38
+
39
+ Hard Negative Mining:
40
+ top-k indices = argsort(sim(z_a, z_n), descending=True)[:k]
41
+ z_n_hard = z_n[top-k indices] // 只保留最难的 k 个负样本
42
+ ```
43
+
44
+ ## 可实现结构
45
+ - **双塔编码器 + 投影头**:encoder → projection_head(MLP 2层) → 归一化 → loss
46
+ - **负样本队列**:维护 momentum encoder 输出的 FIFO queue,容量 N=65536
47
+ - **对称损失**:L = L(a→p) + L(p→a),正负角色互换,增强训练稳定性
48
+ - **多粒度对比**:同时在 token-level、sequence-level、expert-level 施加对比
49
+
50
+ ## GPU 可行性
51
+ - **张量化**:sim 计算为 z_a @ z_n^T → 标准 GEMM (B×d) @ (d×N) = B×N
52
+ - **GEMM 可映射**:核心就是 1-2 次矩阵乘法,完美映射 cuBLAS
53
+ - **复杂度**:O(B·N·d) 计算 + O(B·N) 存储 logits 矩阵,B=256,N=65536 时约 64MB
54
+ - **显存与 KV-Cache**:负样本队列占 N·d·4 bytes ≈ 65536·256·4 = 64MB,固定开销
55
+ - **低精度稳定**:cosine 相似度 + softmax 在 fp16 下需注意 exp 溢出,用 log-sum-exp trick
56
+ - **并行与通信**:多 GPU 时用 all-gather 收集其他 GPU 的负样本扩大 N(MoCo v3 策略)
57
+ - **稀疏结构**:hard negative mining 后只保留 k<<N 个负样本,有效稀疏化 logits
58
+ - **算子融合**:L2-norm → matmul → scale → log-softmax → nll_loss 可融合
59
+
60
+ ## 论文表述方式
61
+ "采用温度缩放的 InfoNCE 对比损失,通过动量编码器维护 N=65536 的负样本队列,
62
+ 在单位球面上最大化正对互信息的下界,理论分析表明该下界以 O(1/√N) 收敛。"
63
+
64
+ ## 风险
65
+ - τ 过小导致训练不稳定(梯度过大),τ 过大导致所有样本不分难易(退化均匀分布)
66
+ - 负样本队列中的过期编码引入 stale representation 偏差
67
+ - 假阴性问题:无监督负采样可能采到语义相似但标注不同的样本
68
+ - B 过小时 batch 内正负对不平衡,需依赖队列补偿
@@ -0,0 +1,69 @@
1
+ # Information Bottleneck Loss
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 a representation $Z$ must achieve an optimal balance between "retaining task-relevant information" and "compressing input redundancy." Typical scenarios: (1) Shared representations should retain only cross-task common information, discarding task-specific noise; (2) Private representations should retain only single-task unique information; (3) Routing features should maximize expert-task matching information. Core objective: **optimal information compression -- nothing more, nothing less, retaining only what is useful**.
6
+
7
+ ## Mathematical Inspiration
8
+ - Lenses: lenses/probabilistic.md (information bottleneck principle, mutual information variational), lenses/variational.md (Lagrangian duality)
9
+ - Knowledge: knowledge-base/probability/kl-divergence.md (IB theory, rate-distortion function), knowledge-base/probability/entropy.md (mutual information and conditional entropy)
10
+
11
+ ## Required Mathematical Knowledge
12
+ - **Information Bottleneck Objective**: $\min I(X;Z) - \beta \cdot I(Z;Y)$, compressing $X \to Z$ while preserving the predictive power of $Z$ for $Y$
13
+ - **Variational Bounds on Mutual Information**:
14
+ $I(X;Z) \leq \mathbb{E}_{p(x,z)}[\log q(z|x)] - \mathbb{E}_{p(z)}[\log q(z)]$ (upper bound for compression term)
15
+ $I(Z;Y) \geq \mathbb{E}_{p(z,y)}[\log q(y|z)] + H(Y)$ (lower bound for prediction term)
16
+ - **CPC (Contrastive Predictive Coding)**: InfoNCE lower bound on $I(Z_t; Z_{t+k})$
17
+ - **MINE (Mutual Information Neural Estimation)**:
18
+ $I(X;Z) = \sup_\theta \{ \mathbb{E}[\log T_\theta(x,z)] - \log \mathbb{E}[T_\theta(x,z')] \}$
19
+
20
+ ## AI Module Form
21
+ ```
22
+ Module: InformationBottleneckLoss
23
+ Input: representation Z in R^{B x d}, input X (or its encoding), label Y
24
+
25
+ Method 1 - VIB (Variational Information Bottleneck):
26
+ // Compression upper bound: variational approximation q(z) = N(0, I)
27
+ I_upper = KL(q(z|x) || p(z)) // standard VAE KL term
28
+ // Prediction lower bound: classifier/regressor q(y|z)
29
+ I_lower = CE(q(y|z), y) // cross-entropy = estimate of -H(Y|Z)
30
+ L_IB = I_upper + beta * I_lower
31
+ // beta controls compression-prediction trade-off: larger beta = more aggressive compression
32
+
33
+ Method 2 - Contrastive Mutual Information Estimation (no distributional assumptions):
34
+ // NWJ estimator instead of KL
35
+ I_nwj(x;z) = E[f(x,z)] - exp(E[f(x,z')] - 1) // f is a discriminator network
36
+ L_IB_contrast = I_nwj(x;z) - beta * InfoNCE(z, y) // both terms differentiable
37
+
38
+ Method 3 - Shared/Private IB Decomposition:
39
+ Z_s = enc_shared(x), Z_p = enc_private(x)
40
+ L = I(Z_s; X) + I(Z_p; X) // total compression
41
+ - beta_1 * I(Z_s; Y_common) // Shared retains common information
42
+ - beta_2 * I(Z_p; Y_specific) // Private retains specific information
43
+ + gamma * OrthLoss(Z_s, Z_p) // orthogonality ensures decomposition
44
+ ```
45
+
46
+ ## Implementable Architectures
47
+ - **Dual-Encoder Architecture**: enc_shared and enc_private share a base trunk, branching into separate heads
48
+ - **Mutual Information Estimator**: Small MLP discriminator $T(x,z) \to$ scalar, alternating variational with the main network
49
+ - **Beta Scheduling**: Set $\beta = 0$ at training start (no compression), gradually increase to target value during training
50
+ - **Gradient Reversal**: Gradient of $I(X;Z)$ is reversed via z.flip_gradient(), implementing adversarial compression
51
+
52
+ ## GPU Feasibility
53
+ - **Tensorization**: Mutual information estimator is a standard MLP -> GEMM chain; KL is element-wise
54
+ - **GEMM-mappability**: VIB method requires only encoder GEMM + KL computation; contrastive method adds 1 GEMM for shuffled negatives
55
+ - **Complexity**: One additional KL term $O(B \cdot d)$ or one discriminator forward pass $O(B \cdot d^2)$ beyond the standard network; acceptable
56
+ - **Memory & KV-Cache**: Requires additional storage for discriminator parameters (small MLP) and intermediate activations, <10MB
57
+ - **Low Precision Stability**: The exp operation in the MINE estimator requires clipping under fp16; VIB KL is recommended in fp32
58
+ - **Parallelism & Communication**: Discriminator and main network forward passes can run in parallel; gradients are synchronized through the shared representation layer
59
+ - **Sparse Structure**: Compressed $Z$ dimensions can be dynamically pruned (Automatic Relevance Determination, ARD)
60
+ - **Operator Fusion**: Encoder forward + KL computation + discriminator forward can be partially fused
61
+
62
+ ## Paper Phrasing
63
+ "Based on information bottleneck theory, we formalize Shared/Private decomposition as $\min I(X;Z_s) + I(X;Z_p) - \beta_1 I(Z_s;Y_c) - \beta_2 I(Z_p;Y_s)$, replacing mutual information terms with variational upper and lower bounds for end-to-end variational, theoretically guaranteeing beta-optimality on the compression-prediction Pareto frontier."
64
+
65
+ ## Risks
66
+ - Mutual information estimators (MINE/NWJ) have high variance, causing training instability; large batches or moving averages are needed
67
+ - Improper beta selection leads to over-compression (underfitting) or insufficient compression (overfitting)
68
+ - VIB assumes Gaussian posteriors, which may be inadequate for complex posterior distributions
69
+ - When optimizing multiple IB objectives jointly, the relative ratio of $\beta_1$ and $\beta_2$ is sensitive
@@ -0,0 +1,74 @@
1
+ # Information Bottleneck Loss(信息瓶颈损失)
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
3
+
4
+ ## 适用问题
5
+ 当需要让表示 Z 在"保留任务相关信息"与"压缩输入冗余信息"之间取得最优平衡时使用。
6
+ 典型场景:(1) Shared 表示应只保留跨任务共性信息,丢弃任务特异噪声;
7
+ (2) Private 表示应只保留单任务独有信息;(3) 路由特征应最大化专家-任务匹配信息。
8
+ 核心诉求:**信息最优压缩——不多不少,只保留有用的**。
9
+
10
+ ## 数学思想来源
11
+ - 透镜:lenses/probabilistic.md(信息瓶颈原理、互信息优化)、lenses/variational.md(拉格朗日对偶)
12
+ - 知识:knowledge-base/probability/kl-divergence.md(IB 理论、率失真函数)、
13
+ knowledge-base/probability/entropy.md(互信息与条件熵)
14
+
15
+ ## 需要的数学知识
16
+ - **信息瓶颈目标**:min I(X;Z) - β·I(Z;Y),压缩 X→Z 同时保留 Z 对 Y 的预测力
17
+ - **互信息变分下界/上界**:
18
+ I(X;Z) ≤ E_{p(x,z)}[log q(z|x)] - E_{p(z)}[log q(z)] (用上界估计压缩项)
19
+ I(Z;Y) ≥ E_{p(z,y)}[log q(y|z)] + H(Y) (用下界估计预测项)
20
+ - **CPC (Contrastive Predictive Coding)**:I(Z_t; Z_{t+k}) 的 InfoNCE 下界
21
+ - **MINE (Mutual Information Neural Estimation)**:
22
+ I(X;Z) = sup_θ { E[log T_θ(x,z)] - log E[T_θ(x,z')] }
23
+
24
+ ## AI 模块形式
25
+ ```
26
+ 模块:InformationBottleneckLoss
27
+ 输入:表示 Z ∈ R^{B×d},输入 X(或其编码),标签 Y
28
+
29
+ 方法1 - VIB (Variational Information Bottleneck):
30
+ // 压缩项上界:用变分近似 q(z) = N(0, I)
31
+ I_upper = KL(q(z|x) ‖ p(z)) // 标准 VAE 的 KL 项
32
+ // 预测项下界:用分类器/回归器 q(y|z)
33
+ I_lower = CE(q(y|z), y) // 交叉熵 = -H(Y|Z) 的估计
34
+ L_IB = I_upper + β · I_lower
35
+ // β 控制压缩-预测权衡:β↑ 更激进压缩,β↓ 保留更多预测信息
36
+
37
+ 方法2 - 对比式互信息估计(无需分布假设):
38
+ // 用 NWJ 估计器替代 KL
39
+ I_nwj(x;z) = E[f(x,z)] - exp(E[f(x,z')] - 1) // f 为判别网络
40
+ L_IB_contrast = I_nwj(x;z) - β · InfoNCE(z, y) // 两项均可微
41
+
42
+ 方法3 - Shared/Private IB 分解:
43
+ Z_s = enc_shared(x), Z_p = enc_private(x)
44
+ L = I(Z_s; X) + I(Z_p; X) // 总压缩
45
+ - β₁ · I(Z_s; Y_common) // Shared 保留公共信息
46
+ - β₂ · I(Z_p; Y_specific) // Private 保留特异信息
47
+ + γ · OrthLoss(Z_s, Z_p) // 正交性确保分解
48
+ ```
49
+
50
+ ## 可实现结构
51
+ - **双编码器架构**:enc_shared 和 enc_private 共享底层 trunk,分叉出各自 head
52
+ - **互信息估计器**:小型 MLP 判别器 T(x,z) → scalar,与主网络交替优化
53
+ - **β 调度**:训练初期 β=0(不压缩),随训练进行逐步增大到目标值
54
+ - **梯度反转**:I(X;Z) 的梯度通过 z.flip_gradient() 反转,实现对抗式压缩
55
+
56
+ ## GPU 可行性
57
+ - **张量化**:互信息估计器为标准 MLP → GEMM 链;KL 为 element-wise
58
+ - **GEMM 可映射**:VIB 方法仅需 encoder GEMM + KL 计算;对比方法额外 1 次 GEMM 做 shuffle 负样本
59
+ - **复杂度**:比标准网络多 1 个 KL 项 O(B·d) 或 1 个判别器前向 O(B·d²),可接受
60
+ - **显存与 KV-Cache**:需额外存储判别器参数(小型 MLP)和中间激活,<10MB
61
+ - **低精度稳定**:MINE 估计器的 exp 运算在 fp16 下需 clip;VIB 的 KL 建议 fp32
62
+ - **并行与通信**:判别器与主网络可并行前向,梯度通过共享表示层同步
63
+ - **稀疏结构**:压缩后的 Z 维度可动态裁剪(自动相关性确定 ARD)
64
+ - **算子融合**:encoder 前向 + KL 计算 + 判别器前向可部分融合
65
+
66
+ ## 论文表述方式
67
+ "基于信息瓶颈理论,将 Shared/Private 分解形式化为 min I(X;Z_s)+I(X;Z_p)-β₁I(Z_s;Y_c)-β₂I(Z_p;Y_s),
68
+ 通过变分上下界替代互信息项实现端到端优化,理论上保证压缩-预测 Pareto 前沿的 β-最优性。"
69
+
70
+ ## 风险
71
+ - 互信息估计(MINE/NWJ)方差大,训练不稳定,需要大 batch 或 moving average
72
+ - β 选择不当导致过度压缩(欠拟合)或压缩不足(过拟合)
73
+ - VIB 假设高斯后验,对复杂后验分布近似不足
74
+ - 多 IB 联合优化时 β₁, β₂ 的相对比例敏感
@@ -0,0 +1,56 @@
1
+ # Orthogonality Loss
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
+ In multi-expert / multi-task settings, representations learned by submodules are highly overlapping and redundant, leading to poor parameter utilization. This loss is used when the $d$-dimensional feature space needs to be partitioned into $K$ non-interfering subspaces -- such as Shared-Private separation, MoE expert differentiation, and multi-task head decorrelation. Core objective: **ensure different modules see different things**.
6
+
7
+ ## Mathematical Inspiration
8
+ - Lenses: lenses/projection.md (orthogonal projection and subspace decomposition), lenses/variational.md (regularization and saddle points)
9
+ - Knowledge: knowledge-base/matrix-analysis/projection.md (spectral theorem, SVD, Schur decomposition), knowledge-base/probability/kl-divergence.md (redundancy and mutual information)
10
+
11
+ ## Required Mathematical Knowledge
12
+ - **Frobenius Inner Product and Orthogonality**: $\langle A, B \rangle_F = \text{tr}(A^T B)$; when $\langle A, B \rangle_F = 0$, $A \perp B$
13
+ - **Stiefel Manifold Constraint**: $W \in \text{St}(d, k)$, i.e., $W^T W = I_k$, projecting onto the set of orthogonal matrices
14
+ - **DPP (Determinantal Point Process)**: $\det(W^T W)$ increases as column vectors become more spread out, serving as a diversity proxy
15
+ - **Off-Diagonal Elements of the Cosine Similarity Matrix**: $C_{ij} = |\langle w_i, w_j \rangle| / (\|w_i\|\|w_j\|)$, minimizing $\sum_{i \neq j} C_{ij}^2$
16
+
17
+ ## AI Module Form
18
+ ```
19
+ Module: OrthogonalDiversityLoss
20
+ Input: K feature matrices {W_k in R^{d x r}}_{k=1}^K (weights or features of K submodules)
21
+
22
+ Method 1 - Frobenius Orthogonal Regularization:
23
+ L_orth = Sum_{i<j} ||W_i^T W_j||_F^2
24
+ // Computation: O(K^2 * d * r^2), K typically <16 so cost is manageable
25
+
26
+ Method 2 - Grassmann Distance (based on principal angles):
27
+ sigma_k = singular values of SVD(W_i^T W_j)
28
+ L_grass = Sum_{i<j} Sum_k sigma_k^2 * (1 - sigma_k^2) // penalizes singular values that are neither 0 nor 1
29
+
30
+ Method 3 - Efficient Cosine Decorrelation:
31
+ G = concat([W_1,...,W_K])^T * concat([W_1,...,W_K]) // single GEMM
32
+ L_corr = ||G * (1 - I)||_F^2 // mask out diagonal, penalize off-diagonal elements
33
+ ```
34
+
35
+ ## Implementable Architectures
36
+ - **Embedded as nn.Module**: forward receives $K$ tensors and returns a scalar loss; supports direct .backward()
37
+ - **Weighted Combination with Main Loss**: L_total = L_task + lambda * L_orth; lambda can use cosine annealing or warm-up
38
+ - **Block Computation**: When $K$ is large, perform mini-batch sampling over $(i,j)$ pairs, computing only $B$ out of $\binom{K}{2}$ pairs per step
39
+
40
+ ## GPU Feasibility
41
+ - **Tensorization**: Core operation is matmul ($W^T W$) -- standard GEMM, perfectly mapped to Tensor Cores
42
+ - **GEMM-mappability**: Method 3 requires only 1 GEMM + 1 element-wise mask + Frobenius norm
43
+ - **Complexity**: $O(K \cdot d \cdot r)$ storage + $O(d \cdot r^2 \cdot K)$ or $O(K^2 \cdot d \cdot r^2)$ computation; negligible when $K < 16$
44
+ - **Memory & KV-Cache**: Intermediate matrices are on the order of $d \times r \cdot K$, adding no KV-Cache overhead
45
+ - **Low Precision Stability**: Frobenius norm is a sum of squares, safe under fp16; Grassmann SVD is recommended in fp32
46
+ - **Parallelism & Communication**: The $K$ pairs are embarrassingly parallel; can be distributed across GPUs with all-reduce
47
+ - **Sparse Structure**: If $W_k$ is itself sparse (e.g., MoE gate), masking further increases sparsity
48
+ - **Operator Fusion**: matmul -> mask -> square -> sum can be fused into a single CUDA kernel
49
+
50
+ ## Paper Phrasing
51
+ "We introduce an orthogonality regularizer L_orth = Sum_{i<j} ||W_i^T W_j||_F^2 that constrains the feature spaces of submodules to the approximately orthogonal Grassmann sub-manifold, theoretically guaranteeing that redundancy across $K$ subspaces decays as $O(1/\sqrt{d})$."
52
+
53
+ ## Risks
54
+ - Excessively large lambda causes variational difficulties (orthogonal constraint conflicts with task objective); requires careful tuning or adaptive lambda
55
+ - The SVD in Method 2 produces unstable gradients during backpropagation; epsilon-regularization on singular values is needed
56
+ - When $K \cdot r > d$, strict orthogonality is impossible; dimensionality reduction or acceptance of approximate orthogonality is required
@@ -0,0 +1,60 @@
1
+ # Orthogonality Loss(正交性损失)
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
3
+
4
+ ## 适用问题
5
+ 多专家/多任务场景中,各子模块学习到的表示高度重叠、冗余,导致参数利用率低下。
6
+ 当需要将 d 维特征空间分割为 K 个互不干扰的子空间时使用——如 Shared-Private 分离、
7
+ MoE 专家差异化、多任务 head 去相关。核心诉求:**让不同模块看到不同的东西**。
8
+
9
+ ## 数学思想来源
10
+ - 透镜:lenses/projection.md(正交投影与子空间分解)、lenses/variational.md(正则化与鞍点)
11
+ - 知识:knowledge-base/matrix-analysis/projection.md(谱定理、SVD、Schur 分解)、
12
+ knowledge-base/probability/kl-divergence.md(冗余度与互信息)
13
+
14
+ ## 需要的数学知识
15
+ - **Frobenius 内积与正交性**:⟨A, B⟩_F = tr(A^T B),当 ⟨A, B⟩_F = 0 时 A⊥B
16
+ - **Stiefel 流形约束**:W ∈ St(d, k) 即 W^T W = I_k,投影到正交矩阵集合
17
+ - **DPP 行列式点过程**:det(W^T W) 越大表示列向量越分散,可作为多样性代理
18
+ - **cosine 相似度矩阵去对角外元素**:C_ij = |⟨w_i, w_j⟩| / (‖w_i‖‖w_j‖),最小化 Σ_{i≠j} C_ij²
19
+
20
+ ## AI 模块形式
21
+ ```
22
+ 模块:OrthogonalDiversityLoss
23
+ 输入:K 个特征矩阵 {W_k ∈ R^{d×r}}_{k=1}^K(K 个子模块的权重或特征)
24
+
25
+ 方法1 - Frobenius 正交正则:
26
+ L_orth = Σ_{i<j} ‖W_i^T W_j‖_F²
27
+ // 计算量:O(K² · d · r²),K 一般 <16 所以可控
28
+
29
+ 方法2 - Grassmann 距离(基于主角度):
30
+ σ_k = SVD(W_i^T W_j) 的奇异值
31
+ L_grass = Σ_{i<j} Σ_k σ_k²(1 - σ_k²) // 惩罚非 0 非 1 的奇异值
32
+
33
+ 方法3 - 高效 cosine 去相关:
34
+ G = concat([W_1,...,W_K])^T · concat([W_1,...,W_K]) // 一次 GEMM
35
+ L_corr = ‖G ⊙ (1 - I)‖_F² // mask 掉对角线,惩罚非对角元素
36
+ ```
37
+
38
+ ## 可实现结构
39
+ - **嵌入为 nn.Module**:forward 接收 K 个 tensor,返回标量 loss,可直接 .backward()
40
+ - **与主 loss 加权组合**:L_total = L_task + λ · L_orth,λ 可用 cosine annealing 或 warm-up
41
+ - **分块计算**:当 K 很大时,对 (i,j) 对做 mini-batch 采样,每步只算 C(K,2) 中的 B 对
42
+
43
+ ## GPU 可行性
44
+ - **张量化**:核心操作为 matmul(W^T W)→ 标准 GEMM,完美映射 Tensor Core
45
+ - **GEMM 可映射**:方法3 只需 1 次 GEMM + 1 次 element-wise mask + Frobenius 范数
46
+ - **复杂度**:O(K·d·r) 存储 + O(d·r²·K) 或 O(K²·d·r²) 计算,K<16 时 negligible
47
+ - **显存与 KV-Cache**:中间矩阵 d×r·K 量级,不增加 KV-Cache 负担
48
+ - **低精度稳定**:Frobenius 范数为平方和,fp16 下 OK;Grassmann SVD 建议 fp32
49
+ - **并行与通信**:K 对之间 embarrassingly parallel,可分 GPU 计算后 all-reduce
50
+ - **稀疏结构**:若 W_k 本身稀疏(如 MoE gate),mask 后稀疏度进一步提升
51
+ - **算子融合**:matmul → mask → square → sum 可融合为单个 CUDA kernel
52
+
53
+ ## 论文表述方式
54
+ "我们引入正交性正则项 L_orth = Σ_{i<j}‖W_i^T W_j‖_F²,将各子模块的特征空间约束
55
+ 到近似正交的 Grassmann 子流形上,理论上保证 K 个子空间的冗余度以 O(1/√d) 衰减。"
56
+
57
+ ## 风险
58
+ - λ 过大导致优化困难(正交约束与任务目标冲突),需 careful tuning 或 adaptive λ
59
+ - 方法2 的 SVD 在反向传播时梯度不稳定,需添加 ε-正则化到奇异值
60
+ - 当 K·r > d 时正交性不可能严格满足,需降维或接受近似正交
@@ -0,0 +1,61 @@
1
+ # Variational Loss
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 sampling from latent variable distributions is required to generate diverse outputs. Typical scenarios: (1) Expert selection introduces discrete latent variables $z$ that need end-to-end variational; (2) Representation spaces need to model uncertainty; (3) Generative routing requires sampling from posterior distributions $p(z|x)$; (4) Bayesian mixture of experts. Core objective: **model distributions rather than point estimates in latent space, enabling uncertainty awareness and diversity**.
6
+
7
+ ## Mathematical Inspiration
8
+ - Lenses: lenses/probabilistic.md (variational inference and ELBO), lenses/probabilistic.md (posterior and prior)
9
+ - Knowledge: knowledge-base/probability/entropy.md (KL divergence, variational families), knowledge-base/probability/kl-divergence.md (ELBO derivation)
10
+
11
+ ## Required Mathematical Knowledge
12
+ - **ELBO (Evidence Lower Bound)**: $\log p(x) \geq \mathbb{E}_{q(z|x)}[\log p(x|z)] - \text{KL}(q(z|x) \| p(z))$ -- the first term is reconstruction likelihood, the second regularizes the posterior toward the prior
13
+ - **Reparameterization Trick**: $z = \mu + \sigma \odot \varepsilon$, $\varepsilon \sim \mathcal{N}(0, I)$ -- enables gradient backpropagation through sampling operations
14
+ - **Closed-Form KL Divergence**: When both $q$ and $p$ are Gaussian, $\text{KL}(\mathcal{N}(\mu,\sigma^2) \| \mathcal{N}(0,1)) = -\frac{1}{2} \sum(1 + \log \sigma^2 - \mu^2 - \sigma^2)$
15
+ - **Gumbel-Softmax (Discrete Latent Variables)**: $z = \text{softmax}((\log \pi + g) / \tau)$, $g \sim \text{Gumbel}(0,1)$ -- continuous relaxation of discrete sampling
16
+
17
+ ## AI Module Form
18
+ ```
19
+ Module: VariationalLoss
20
+ Input: encoder outputs (mu, log_sigma^2) in R^{B x d_z}, reconstruction output x_hat in R^{B x d_x}, original input x
21
+
22
+ Method 1 - Gaussian VAE Loss:
23
+ KL = -0.5 * sum(1 + log_sigma^2 - mu^2 - exp(log_sigma^2), dim=-1) // closed form
24
+ recon = -log p(x|x_hat) // MSE or BCE depending on data distribution
25
+ L_vae = recon + beta * KL // beta-VAE controls disentanglement
26
+
27
+ Method 2 - Gumbel-Softmax (discrete expert selection):
28
+ logits = encoder(x) in R^{B x K} // logits for K experts
29
+ g = -log(-log(uniform(B x K) + eps) + eps) // Gumbel noise sampling
30
+ z_soft = softmax((logits + g) / tau) // temperature tau annealed from 1.0 to 0.1
31
+ L_gumbel = CE(task_head(z_soft * features), y) // straight-through estimator backprop
32
+
33
+ Method 3 - Variational Information Bottleneck:
34
+ L_IB = -I(X; Z) + beta * I(Z; Y) // maximize MI between Z and X, minimize redundancy between Z and Y
35
+ ~ E[-log q(x|z)] + beta * KL(q(z|x) || p(z)) // variational approximation
36
+ ```
37
+
38
+ ## Implementable Architectures
39
+ - **Dual-Head Encoder Output**: Linear(d, 2 * d_z) -> split -> (mu, log_sigma^2), sharing base parameters
40
+ - **Beta Annealing Strategy**: Linearly increase beta from 0 to the target value to prevent KL collapse (posterior collapse)
41
+ - **Free Bits**: Set a KL lower bound lambda per dimension, penalizing only the excess: $\sum \max(\text{KL}_j, \lambda)$
42
+ - **IWAE Multi-Particle**: Use log-mean-exp over $K$ samples instead of single-sample ELBO for a tighter lower bound
43
+
44
+ ## GPU Feasibility
45
+ - **Tensorization**: Computation of mu and sigma^2 is a Linear layer (GEMM); KL involves element-wise operations
46
+ - **GEMM-mappability**: Encoder 1 GEMM -> split -> reparameterization -> decoder 1 GEMM
47
+ - **Complexity**: Same order as standard feedforward networks $O(B \cdot d^2)$; KL computation $O(B \cdot d_z)$ is negligible
48
+ - **Memory & KV-Cache**: Additional storage of mu and sigma^2, two $B \times d_z$ matrices; minimal overhead
49
+ - **Low Precision Stability**: log/exp operations in KL are recommended in fp32; Gumbel softmax log-log requires fp32
50
+ - **Parallelism & Communication**: The $K$ samples in multi-particle IWAE can be sampled and computed in parallel
51
+ - **Sparse Structure**: Discrete latent variables (Gumbel) degenerate to one-hot as $\tau \to 0$, naturally sparse
52
+ - **Operator Fusion**: The Linear layers for mu and sigma^2 can share a single GEMM followed by split; KL exp/sub/add can be fused
53
+
54
+ ## Paper Phrasing
55
+ "We adopt the variational inference framework, geometric expert selection as posterior inference over discrete latent variables $z$. End-to-end variational is achieved through Gumbel-Softmax relaxation, combined with a beta annealing strategy that effectively prevents posterior collapse. The ELBO lower bound converges at rate $O(\sqrt{d/n})$ with respect to latent variable dimensionality."
56
+
57
+ ## Risks
58
+ - Posterior collapse: The KL term converges to 0 prematurely, causing latent variables to degenerate into prior samples and lose information
59
+ - Gumbel-Softmax temperature $\tau$ annealing requires careful scheduling; too fast causes gradient vanishing, too slow loses discreteness
60
+ - IWAE multi-particle log-mean-exp is numerically unstable in high dimensions; log-sum-exp trick is needed
61
+ - Excessively large beta in beta-VAE degrades reconstruction quality; task-specific balancing is required
@@ -0,0 +1,71 @@
1
+ # Variational Loss(变分损失)
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
3
+
4
+ ## 适用问题
5
+ 当需要从隐变量分布中采样并生成多样化输出时使用。典型场景:
6
+ (1) 专家选择引入离散隐变量 z,需要端到端优化;(2) 表示空间需要建模不确定性;
7
+ (3) 生成式路由中需要从后验分布 p(z|x) 采样;(4) 贝叶斯专家混合。
8
+ 核心诉求:**在潜空间中建模分布而非点估计,获得不确定性感知与多样性**。
9
+
10
+ ## 数学思想来源
11
+ - 透镜:lenses/probabilistic.md(变分推断与 ELBO)、lenses/probabilistic.md(后验与先验)
12
+ - 知识:knowledge-base/probability/entropy.md(KL 散度、变分族)、
13
+ knowledge-base/probability/kl-divergence.md(ELBO 推导)
14
+
15
+ ## 需要的数学知识
16
+ - **ELBO(证据下界)**:log p(x) ≥ E_{q(z|x)}[log p(x|z)] - KL(q(z|x) ‖ p(z))
17
+ 第一项为重构似然,第二项为正则项将后验推近先验
18
+ - **重参数化技巧 (Reparameterization Trick)**:z = μ + σ ⊙ ε, ε ~ N(0, I)
19
+ 使梯度可通过采样操作反向传播
20
+ - **KL 散度闭合形式**:当 q 和 p 均为高斯时,
21
+ KL(N(μ,σ²) ‖ N(0,1)) = -½ Σ(1 + log σ² - μ² - σ²)
22
+ - **Gumbel-Softmax(离散隐变量)**:
23
+ z = softmax((log π + g) / τ), g ~ Gumbel(0,1),连续松弛离散采样
24
+
25
+ ## AI 模块形式
26
+ ```
27
+ 模块:VariationalLoss
28
+ 输入:编码器输出 (μ, log_σ²) ∈ R^{B×d_z},重构输出 x̂ ∈ R^{B×d_x},原始输入 x
29
+
30
+ 方法1 - 高斯 VAE 损失:
31
+ KL = -0.5 * sum(1 + log_σ² - μ² - exp(log_σ²), dim=-1) // 闭合形式
32
+ recon = -log p(x|x̂) // MSE 或 BCE 取决于数据分布
33
+ L_vae = recon + β · KL // β-VAE 控制解耦程度
34
+
35
+ 方法2 - Gumbel-Softmax(离散专家选择):
36
+ logits = encoder(x) ∈ R^{B×K} // K 个专家的 logits
37
+ g = -log(-log(uniform(B×K) + ε) + ε) // Gumbel 噪声采样
38
+ z_soft = softmax((logits + g) / τ) // 温度 τ 从 1.0 退火到 0.1
39
+ L_gumbel = CE(task_head(z_soft ⊙ features), y) // 直通估计器反向传播
40
+
41
+ 方法3 - 信息瓶颈变分:
42
+ L_IB = -I(X; Z) + β · I(Z; Y) // 最大化 Z 与 X 的互信息,最小化 Z 与 Y 的冗余
43
+ ≈ E[-log q(x|z)] + β · KL(q(z|x) ‖ p(z)) // 变分近似
44
+ ```
45
+
46
+ ## 可实现结构
47
+ - **编码器双头输出**:Linear(d, 2·d_z) → split → (μ, log_σ²),共享底层参数
48
+ - **β 退火策略**:β 从 0 线性增长到目标值,防止 KL 坍塌(posterior collapse)
49
+ - **Free Bits**:每个维度设定 KL 下界 λ,只惩罚超出部分:Σ max(KL_j, λ)
50
+ - **IWAE 多粒子**:用 K 个样本的 log-mean-exp 替代单样本 ELBO,获得更紧下界
51
+
52
+ ## GPU 可行性
53
+ - **张量化**:μ,σ² 的计算为 Linear 层(GEMM),KL 为 element-wise 运算
54
+ - **GEMM 可映射**:编码器 1 次 GEMM → split → 重参数化 → 解码器 1 次 GEMM
55
+ - **复杂度**:与标准前向网络同阶 O(B·d²),KL 计算 O(B·d_z) 可忽略
56
+ - **显存与 KV-Cache**:额外存储 μ, σ² 两个 B×d_z 矩阵,开销极小
57
+ - **低精度稳定**:KL 的 log/exp 运算建议 fp32;Gumbel softmax 的 log-log 需 fp32
58
+ - **并行与通信**:多粒子 IWAE 的 K 个样本可并行采样和计算
59
+ - **稀疏结构**:离散隐变量 (Gumbel) 在 τ→0 时退化为 one-hot,天然稀疏
60
+ - **算子融合**:μ 和 σ² 的 Linear 可共享一次 GEMM 后 split;KL 的 exp/sub/add 可融合
61
+
62
+ ## 论文表述方式
63
+ "采用变分推断框架,将专家选择建模为离散隐变量 z 的后验推断,通过 Gumbel-Softmax
64
+ 松弛实现端到端优化,配合 β 退火策略有效避免 posterior collapse,
65
+ ELBO 下界随隐变量维度以 O(√d/n) 收敛。"
66
+
67
+ ## 风险
68
+ - Posterior collapse:KL 项过早收敛到 0,隐变量退化为先验采样,丧失信息
69
+ - Gumbel-Softmax 的 τ 退火需要精心调度,过快导致梯度消失,过慢丧失离散性
70
+ - IWAE 多粒子在高维下 log-mean-exp 数值不稳定,需 log-sum-exp trick
71
+ - β-VAE 的 β 过大导致重构质量下降,需根据任务平衡
@@ -0,0 +1,87 @@
1
+ # Equivariant Split
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
+ Use when the input possesses symmetries (e.g., permutations, rotations, translations) and the representation should preserve or reflect those symmetries.
6
+ Typical scenarios: (1) Token permutation equivariance -- representation should change accordingly when token order in a sentence changes (positional encoding);
7
+ (2) Symmetry-based grouping of feature dimensions -- certain feature dimensions are invariant under specific transformations, while others are equivariant;
8
+ (3) Multi-expert symmetry specialization -- different experts process different symmetry subspaces;
9
+ (4) Geometric deep learning -- SE(3) equivariance in 3D molecular/protein structures.
10
+ Core requirement: **encode symmetry priors into network architecture to reduce learning burden and improve generalization**.
11
+
12
+ ## Mathematical Inspiration
13
+ - Lenses: lenses/geometric.md (group actions, invariant/equivariant maps), lenses/probabilistic.md (symmetry and information redundancy)
14
+ - Knowledge: knowledge-base/matrix-analysis/projection.md (group representation theory, irreducible representations),
15
+ knowledge-base/differential-geometry/manifold.md (Lie groups, homogeneous spaces)
16
+
17
+ ## Required Mathematical Background
18
+ - **Group Action and Equivariance**: A map f is equivariant with respect to group G if and only if f(g * x) = g * f(x) for all g in G
19
+ Invariance is a special case of equivariance (g * f(x) = f(x), i.e., the trivial representation)
20
+ - **Schur's Lemma and Irreducible Representation Decomposition**:
21
+ Any finite group representation decomposes as a direct sum of irreducible representations: V = direct_sum_i m_i * V_i
22
+ Equivariant linear maps are diagonal/block-diagonal between irreducible components
23
+ - **Peter-Weyl Theorem**: Functions on a compact group decompose as a series of irreducible representation matrix elements
24
+ f(x) = sum_rho sum_{ij} c_{rho,ij} * rho_{ij}(g) (generalized Fourier expansion)
25
+ - **Steerable Feature Spaces**: Features are organized according to irreducible representations of the group;
26
+ under the action of duality g, each component transforms via the corresponding representation matrix: f_i -> sum_j rho_{ij}(g) f_j
27
+
28
+ ## AI Module Form
29
+ ```
30
+ Module: EquivariantSplit
31
+ Input: X in R^{N x d}, symmetry group G (e.g., S_n permutation group, Z_n cyclic group, SO(3) rotation group)
32
+
33
+ Method 1 - Split feature dimensions by irreducible representations:
34
+ // Decompose d-dimensional features by irreducible representations of the group
35
+ irreps = decompose(G, d) // [(d_1, rho_1), (d_2, rho_2), ...] where sum d_i = d
36
+ X_split = split(X, [d_1, d_2, ...], dim=-1) // split by irreducible components
37
+ // Process each component independently with equivariant layers:
38
+ for (X_i, rho_i) in zip(X_split, irreps):
39
+ Y_i = EquivariantLinear(X_i, rho_i) // weights constrained by Schur's lemma
40
+ Y = concat(Y_i, dim=-1) // reassemble
41
+
42
+ Method 2 - Positional equivariant split (Token permutation group S_n):
43
+ // Transformer self-attention is naturally permutation equivariant (without positional encoding)
44
+ // Explicitly introduce controllable permutation equivariance:
45
+ X_content = X[:, :d_content] // permutation-invariant content part
46
+ X_position = X[:, d_content:] // position-dependent part
47
+ // Content part uses permutation-invariant pooling:
48
+ z_inv = mean(X_content, dim=1) // global invariant features
49
+ // Position part uses equivariant operations:
50
+ z_equiv = Attention(X_position, X_position, X_position) // permutation equivariant
51
+ output = z_equiv + MLP(z_inv).unsqueeze(1) // broadcast invariant signal back
52
+
53
+ Method 3 - Group convolution / group pooling:
54
+ // Features defined on group G: f: G -> R^c
55
+ // Group convolution: (f * psi)(g) = sum_{h in G} f(h) * psi(h^{-1} g)
56
+ // Group pooling: pool over orbits of subgroup H < G
57
+ // Implemented as matrix multiplication (group multiplication table -> sparse permutation matrices)
58
+ for g in generators(G):
59
+ X_g = permutation_matrix(g) @ X // group generator action
60
+ features_g = Linear(X_g) // equivariant processing with shared weights
61
+ output = aggregate(features_g) // aggregate along group dimension
62
+ ```
63
+
64
+ ## Implementable Structures
65
+ - **e3nn / lie_learn integration**: Use existing libraries for SO(3)/SE(3) irreducible representations and spherical harmonics
66
+ - **Blocked feature storage**: Organize feature dimensions by irreducible representations; each block independently normalized and processed
67
+ - **Precomputed group operations**: Group multiplication tables, Clebsch-Gordan coefficients, etc., computed once and cached
68
+ - **Symmetry augmentation**: Apply random group elements g in G to inputs during training (data augmentation) to encourage equivariance
69
+
70
+ ## GPU Feasibility
71
+ - **Tensorization**: Processing irreducible components is batched GEMM; group convolution is sparse GEMM or batched matmul
72
+ - **GEMM-mappable**: Each block of EquivariantLinear is an independent GEMM (N x d_i) @ (d_i x d_i_out), batchable
73
+ - **Complexity**: Same order as standard networks (Schur constraints actually reduce parameters); group convolution incurs an extra |G| factor
74
+ - **Memory & KV-Cache**: Group convolution requires storing |G| copies of features; significant memory pressure when |G| is large
75
+ - **Low-precision stability**: Spherical harmonics Y_l^m computations involve factorials and square roots; fp32 recommended
76
+ - **Parallelism & Communication**: Irreducible components are independent, perfectly parallel; different g in group convolution can be parallelized
77
+ - **Sparse structure**: Permutation matrices in group convolution are extremely sparse (exactly one nonzero per row/column); SpMM is efficient
78
+ - **Operator fusion**: Split -> batched matmul -> concat can be fused; group pooling scatter + reduce can be fused
79
+
80
+ ## Paper-Worthy Formulation
81
+ "Leveraging Schur's lemma from group representation theory, we decompose the d-dimensional feature space into a direct sum of irreducible representations of symmetry group G, with each component independently processed by equivariance-constrained linear layers. This achieves a |G| / sum(m_i^2) improvement in parameter efficiency while guaranteeing network equivariance under group actions, reducing generalization error by O(1/sqrt(|G|)) on symmetric data."
82
+
83
+ ## Risks
84
+ - Improper group selection (too large constrains cause underfitting, too small fails to capture symmetries)
85
+ - Irreducible representation decomposition requires domain knowledge; implementation is complex for non-standard groups
86
+ - The |G|-fold feature storage in group convolution becomes infeasible for large groups (e.g., S_10 has 3.6M elements)
87
+ - The trade-off between approximate equivariance (soft equivariance) and strict equivariance is difficult to control