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.
Files changed (183) hide show
  1. package/README.en-US.md +50 -30
  2. package/README.md +26 -6
  3. package/agents/math-critic.en.md +30 -28
  4. package/agents/math-critic.md +30 -30
  5. package/commands/ask.en.md +15 -0
  6. package/commands/ask.md +6 -1
  7. package/design-patterns/attention/equivariant-attention.en.md +17 -11
  8. package/design-patterns/attention/equivariant-attention.md +16 -10
  9. package/design-patterns/attention/geometry-aware-attention.en.md +11 -11
  10. package/design-patterns/attention/geometry-aware-attention.md +10 -10
  11. package/design-patterns/attention/information-bottleneck-attention.en.md +40 -20
  12. package/design-patterns/attention/information-bottleneck-attention.md +40 -20
  13. package/design-patterns/attention/projection-attention.en.md +12 -12
  14. package/design-patterns/attention/projection-attention.md +11 -11
  15. package/design-patterns/attention/spectral-attention.en.md +27 -13
  16. package/design-patterns/attention/spectral-attention.md +26 -12
  17. package/design-patterns/compression/leverage-score-selection.en.md +14 -14
  18. package/design-patterns/compression/leverage-score-selection.md +14 -14
  19. package/design-patterns/compression/low-rank-kv-cache.en.md +40 -13
  20. package/design-patterns/compression/low-rank-kv-cache.md +39 -13
  21. package/design-patterns/compression/spectral-token-pruning.en.md +17 -15
  22. package/design-patterns/compression/spectral-token-pruning.md +17 -15
  23. package/design-patterns/compression/topology-preserving-compression.en.md +11 -11
  24. package/design-patterns/compression/topology-preserving-compression.md +11 -11
  25. package/design-patterns/loss/constraint-penalty.en.md +9 -5
  26. package/design-patterns/loss/constraint-penalty.md +10 -7
  27. package/design-patterns/loss/contrastive-loss.en.md +38 -12
  28. package/design-patterns/loss/contrastive-loss.md +39 -13
  29. package/design-patterns/loss/information-bottleneck-loss.en.md +6 -6
  30. package/design-patterns/loss/information-bottleneck-loss.md +7 -7
  31. package/design-patterns/loss/orthogonality-loss.en.md +25 -18
  32. package/design-patterns/loss/orthogonality-loss.md +25 -19
  33. package/design-patterns/loss/variational-loss.en.md +5 -5
  34. package/design-patterns/loss/variational-loss.md +6 -6
  35. package/design-patterns/overview.en.md +36 -0
  36. package/design-patterns/overview.md +36 -0
  37. package/design-patterns/representation/equivariant-split.en.md +5 -5
  38. package/design-patterns/representation/equivariant-split.md +5 -6
  39. package/design-patterns/representation/manifold-representation.en.md +13 -13
  40. package/design-patterns/representation/manifold-representation.md +14 -15
  41. package/design-patterns/representation/shared-private-decomposition.en.md +13 -8
  42. package/design-patterns/representation/shared-private-decomposition.md +13 -10
  43. package/design-patterns/representation/subspace-alignment.en.md +13 -13
  44. package/design-patterns/representation/subspace-alignment.md +13 -13
  45. package/design-patterns/routing/graph-routing.en.md +12 -12
  46. package/design-patterns/routing/graph-routing.md +12 -12
  47. package/design-patterns/routing/moe-routing.en.md +13 -13
  48. package/design-patterns/routing/moe-routing.md +13 -15
  49. package/design-patterns/routing/optimal-transport-routing.en.md +6 -6
  50. package/design-patterns/routing/optimal-transport-routing.md +7 -7
  51. package/design-patterns/routing/spectral-clustering-routing.en.md +27 -16
  52. package/design-patterns/routing/spectral-clustering-routing.md +27 -18
  53. package/knowledge-base/differential-geometry/connection.en.md +17 -2
  54. package/knowledge-base/differential-geometry/connection.md +17 -2
  55. package/knowledge-base/differential-geometry/curvature.en.md +14 -1
  56. package/knowledge-base/differential-geometry/curvature.md +14 -1
  57. package/knowledge-base/differential-geometry/geodesic.en.md +16 -2
  58. package/knowledge-base/differential-geometry/geodesic.md +16 -2
  59. package/knowledge-base/differential-geometry/index.en.md +52 -0
  60. package/knowledge-base/differential-geometry/index.md +52 -0
  61. package/knowledge-base/differential-geometry/manifold.en.md +15 -2
  62. package/knowledge-base/differential-geometry/manifold.md +15 -2
  63. package/knowledge-base/differential-geometry/metric-tensor.en.md +16 -2
  64. package/knowledge-base/differential-geometry/metric-tensor.md +17 -3
  65. package/knowledge-base/differential-geometry/tangent-space.en.md +17 -3
  66. package/knowledge-base/differential-geometry/tangent-space.md +17 -3
  67. package/knowledge-base/information-geometry/fisher-metric.en.md +32 -13
  68. package/knowledge-base/information-geometry/fisher-metric.md +32 -13
  69. package/knowledge-base/information-geometry/index.en.md +45 -0
  70. package/knowledge-base/information-geometry/index.md +45 -0
  71. package/knowledge-base/information-geometry/natural-gradient.en.md +24 -10
  72. package/knowledge-base/information-geometry/natural-gradient.md +24 -10
  73. package/knowledge-base/lie-theory/equivariance.en.md +18 -3
  74. package/knowledge-base/lie-theory/equivariance.md +18 -3
  75. package/knowledge-base/lie-theory/group-action.en.md +16 -2
  76. package/knowledge-base/lie-theory/group-action.md +16 -2
  77. package/knowledge-base/lie-theory/index.en.md +52 -0
  78. package/knowledge-base/lie-theory/index.md +52 -0
  79. package/knowledge-base/lie-theory/lie-algebra.en.md +17 -2
  80. package/knowledge-base/lie-theory/lie-algebra.md +17 -2
  81. package/knowledge-base/lie-theory/lie-group.en.md +17 -3
  82. package/knowledge-base/lie-theory/lie-group.md +17 -3
  83. package/knowledge-base/lie-theory/representation.en.md +17 -2
  84. package/knowledge-base/lie-theory/representation.md +17 -2
  85. package/knowledge-base/matrix-analysis/index.en.md +49 -0
  86. package/knowledge-base/matrix-analysis/index.md +49 -0
  87. package/knowledge-base/matrix-analysis/low-rank-approximation.en.md +20 -7
  88. package/knowledge-base/matrix-analysis/low-rank-approximation.md +20 -7
  89. package/knowledge-base/matrix-analysis/matrix-perturbation.en.md +13 -1
  90. package/knowledge-base/matrix-analysis/matrix-perturbation.md +13 -1
  91. package/knowledge-base/matrix-analysis/positive-semidefinite.en.md +20 -6
  92. package/knowledge-base/matrix-analysis/positive-semidefinite.md +20 -6
  93. package/knowledge-base/matrix-analysis/projection.en.md +14 -1
  94. package/knowledge-base/matrix-analysis/projection.md +14 -1
  95. package/knowledge-base/matrix-analysis/spectral-decomposition.en.md +14 -1
  96. package/knowledge-base/matrix-analysis/spectral-decomposition.md +14 -1
  97. package/knowledge-base/optimization/constrained-optimization.en.md +17 -3
  98. package/knowledge-base/optimization/constrained-optimization.md +17 -3
  99. package/knowledge-base/optimization/convex-optimization.en.md +15 -2
  100. package/knowledge-base/optimization/convex-optimization.md +15 -2
  101. package/knowledge-base/optimization/index.en.md +50 -0
  102. package/knowledge-base/optimization/index.md +50 -0
  103. package/knowledge-base/optimization/lagrangian-duality.en.md +18 -4
  104. package/knowledge-base/optimization/lagrangian-duality.md +18 -4
  105. package/knowledge-base/optimization/proximal-method.en.md +15 -1
  106. package/knowledge-base/optimization/proximal-method.md +15 -1
  107. package/knowledge-base/optimization/riemannian-optimization.en.md +18 -4
  108. package/knowledge-base/optimization/riemannian-optimization.md +18 -4
  109. package/knowledge-base/overview.en.md +20 -8
  110. package/knowledge-base/overview.md +20 -8
  111. package/knowledge-base/probability/concentration-inequality.en.md +21 -7
  112. package/knowledge-base/probability/concentration-inequality.md +21 -7
  113. package/knowledge-base/probability/entropy.en.md +24 -8
  114. package/knowledge-base/probability/entropy.md +23 -7
  115. package/knowledge-base/probability/fisher-information.en.md +24 -10
  116. package/knowledge-base/probability/fisher-information.md +24 -10
  117. package/knowledge-base/probability/index.en.md +51 -0
  118. package/knowledge-base/probability/index.md +51 -0
  119. package/knowledge-base/probability/information-bottleneck.en.md +24 -8
  120. package/knowledge-base/probability/information-bottleneck.md +24 -8
  121. package/knowledge-base/probability/kl-divergence.en.md +21 -8
  122. package/knowledge-base/probability/kl-divergence.md +21 -8
  123. package/knowledge-base/topology/euler-characteristic.en.md +15 -2
  124. package/knowledge-base/topology/euler-characteristic.md +15 -2
  125. package/knowledge-base/topology/fundamental-group.en.md +16 -2
  126. package/knowledge-base/topology/fundamental-group.md +16 -2
  127. package/knowledge-base/topology/index.en.md +48 -0
  128. package/knowledge-base/topology/index.md +48 -0
  129. package/knowledge-base/topology/persistent-homology.en.md +17 -2
  130. package/knowledge-base/topology/persistent-homology.md +17 -2
  131. package/lenses/algorithmic.en.md +1 -1
  132. package/lenses/algorithmic.md +1 -1
  133. package/lenses/axiomatization.en.md +1 -1
  134. package/lenses/axiomatization.md +1 -1
  135. package/lenses/categorical.en.md +1 -1
  136. package/lenses/categorical.md +1 -1
  137. package/lenses/causal.en.md +1 -1
  138. package/lenses/causal.md +1 -1
  139. package/lenses/duality.en.md +1 -1
  140. package/lenses/duality.md +1 -1
  141. package/lenses/game.en.md +2 -2
  142. package/lenses/game.md +2 -2
  143. package/lenses/geometric.en.md +1 -1
  144. package/lenses/geometric.md +1 -1
  145. package/lenses/local-to-global.en.md +1 -1
  146. package/lenses/local-to-global.md +1 -1
  147. package/lenses/perturbation.en.md +1 -1
  148. package/lenses/perturbation.md +1 -1
  149. package/lenses/probabilistic.en.md +1 -1
  150. package/lenses/probabilistic.md +1 -1
  151. package/lenses/projection.en.md +1 -1
  152. package/lenses/projection.md +1 -1
  153. package/lenses/spectral.en.md +2 -2
  154. package/lenses/spectral.md +2 -2
  155. package/lenses/symmetry.en.md +1 -1
  156. package/lenses/symmetry.md +1 -1
  157. package/lenses/topological.en.md +2 -2
  158. package/lenses/topological.md +2 -2
  159. package/lenses/variational.en.md +1 -1
  160. package/lenses/variational.md +1 -1
  161. package/package.json +5 -5
  162. package/references/agentic-workflow.en.md +3 -3
  163. package/references/agentic-workflow.md +1 -1
  164. package/references/books/abstract-algebra.en.md +124 -0
  165. package/references/books/abstract-algebra.md +17 -17
  166. package/references/books/algebraic-geometry-rising-sea.en.md +171 -0
  167. package/references/books/algebraic-geometry-rising-sea.md +7 -7
  168. package/references/books/differential-geometry.en.md +140 -0
  169. package/references/books/differential-geometry.md +23 -23
  170. package/references/books/matrix-analysis.en.md +146 -0
  171. package/references/books/matrix-analysis.md +32 -32
  172. package/references/books/micro-lie-theory.en.md +116 -0
  173. package/references/books/micro-lie-theory.md +16 -16
  174. package/references/books/optimization-ml.en.md +163 -0
  175. package/references/books/optimization-ml.md +20 -21
  176. package/references/books/smooth-manifolds.en.md +105 -0
  177. package/references/books/smooth-manifolds.md +15 -15
  178. package/references/gpu-friendly-math.en.md +35 -9
  179. package/references/gpu-friendly-math.md +33 -5
  180. package/references/skill-index.en.md +105 -0
  181. package/references/skill-index.md +105 -0
  182. package/skills/math-research-activator/SKILL.en.md +72 -88
  183. package/skills/math-research-activator/SKILL.md +61 -78
@@ -1,12 +1,12 @@
1
1
  # Topology-Preserving Compression
2
- > **Rigor disclaimer**: Claims about complexity, memory, FlashAttention fusion, Tensor Core, and KV-Cache compression are marked as verified / ⚠️ retrofittable (needs validation) / infeasible. Unmarked claims are theoretically possible but require engineering validation.
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
  ## Target Problem
5
- Use when compressing representations while homologically preserving the intrinsic topological structure of the data (connected components, loops, cavities): latent-space compression (a toroidal manifold must not collapse into a line segment), knowledge distillation (student--teacher homological equivalence), 3D mesh simplification (genus invariance), KV-Cache semantic preservation (cluster structure must not collapse). Core objective: **compress dimensions or counts while ensuring that changes in the persistence diagram of persistent homology remain controlled**.
5
+ Use when compressing representations while homologically preserving the intrinsic topological structure of the data (connected components, loops, cavities): latent-space compression (a toroidal manifold must not collapse into a line segment), knowledge distillation (student--teacher homological equivalence), 3D mesh simplification (genus invariance), KV-Cache semantic preservation (cluster structure must not collapse). Core objective: **compress dimensions or counts while constraining and measuring changes in the persistence diagram of persistent homology**.
6
6
 
7
7
  ## Mathematical Foundations
8
- - Lenses: lenses/topological.md (topological invariants -- connected components and hole counts are invariant under continuous deformation), lenses/spectral.md (Gauss--Bonnet linking curvature and Euler characteristic), lenses/variational.md (constrained variational of compression ratio vs. topological fidelity)
9
- - Knowledge: knowledge-base/topology/persistent-homology.md (persistent homology, Vietoris--Rips filtration, bottleneck distance), knowledge-base/topology/euler-characteristic.md (Euler characteristic for rapid topological diagnostics), knowledge-base/matrix-analysis/matrix-perturbation.md (Davis--Kahan subspace perturbation bound)
8
+ - Lenses: ../../lenses/topological.en.md (topological invariants -- connected components and hole counts are invariant under continuous deformation), ../../lenses/spectral.en.md (Gauss--Bonnet linking curvature and Euler characteristic), ../../lenses/variational.en.md (constrained variational of compression ratio vs. topological fidelity)
9
+ - Knowledge: ../../knowledge-base/topology/persistent-homology.en.md (persistent homology, Vietoris--Rips filtration, bottleneck distance), ../../knowledge-base/topology/euler-characteristic.en.md (Euler characteristic for rapid topological diagnostics), ../../knowledge-base/matrix-analysis/matrix-perturbation.en.md (Davis--Kahan subspace perturbation bound)
10
10
 
11
11
  ## Required Mathematical Background
12
12
  - **Stability Theorem of Persistent Homology**: $d_B(D(X), D(Y)) \leq d_{GH}(X, Y)$; the Hausdorff distance bounds the change in persistence diagrams
@@ -46,15 +46,15 @@ Method 3 - Topology monitoring + adaptive compression ratio (at inference time):
46
46
  - **Topological diagnostics dashboard**: real-time plotting of $\beta_0(\epsilon), \beta_1(\epsilon)$ curves during training
47
47
 
48
48
  ## GPU Feasibility
49
- - Tensorization / GEMM: the distance matrix `cdist` is GEMM-dominated; $\chi(\epsilon)$ involves threshold counting + cumulative sum
50
- - Complexity: full persistent homology $O(N^3)$ is infeasible; Euler curve $O(N^2 |\epsilon|)$ is feasible; Landmark $O(m^3)$
51
- - Memory: the $N \times N$ distance matrix requires chunking or landmark reduction for $N > 8K$
52
- - Low precision: distance computation is stable in bf16 (positive-number addition); $\chi$ involves integer arithmetic with no precision concerns
53
- - Parallelism: each threshold in the $\epsilon$ sweep is independently parallel; landmark selection can be batch-parallelized
54
- - Operator fusion: cdist + threshold + count can be fused to avoid materializing the large distance matrix
49
+ - **D1[v]/D2[v]**: the distance matrix `cdist` is GEMM-dominated; $\chi(\epsilon)$ involves threshold counting + cumulative sum
50
+ - **D3[~]**: full persistent homology $O(N^3)$ is infeasible; Euler curve $O(N^2 |\epsilon|)$ is feasible; Landmark $O(m^3)$
51
+ - **D4[~]**: the $N \times N$ distance matrix requires chunking or landmark reduction for $N > 8K$
52
+ - **D5[v]**: distance computation is stable in bf16 (positive-number addition); $\chi$ involves integer arithmetic with no precision concerns
53
+ - **D6[v]**: each threshold in the $\epsilon$ sweep is independently parallel; landmark selection can be batch-parallelized
54
+ - **D8[v]**: cdist + threshold + count can be fused to avoid materializing the large distance matrix
55
55
 
56
56
  ## Paper-Worthy Formulation
57
- "Grounded in the Bottleneck stability theorem of persistent homology, we achieve $O(N^2)$-complexity topology-preserving regularization via Euler characteristic curve matching, ensuring that post-compression deviations in Betti numbers over persistence intervals are controlled by the Hausdorff distance."
57
+ "Grounded in the Bottleneck stability theorem of persistent homology, we build a computable topology-preserving regularizer via Euler-characteristic curves or landmark approximations. Bottleneck distance can bound persistence-diagram changes when the filtration-function perturbation is controlled; Euler curves are incomplete proxies and do not by themselves guarantee per-degree Betti-number deviations, so persistence / Betti-curve deviations should be measured."
58
58
 
59
59
  ## Risks
60
60
  - **Persistent homology computational bottleneck**: exact boundary matrix reduction is highly serial ($O(N^3)$), necessitating reliance on Euler curve or landmark approximations
@@ -1,12 +1,12 @@
1
1
  # Topology-Preserving Compression(拓扑保持压缩)
2
- > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / 不可行」。未标注的视为理论可行,需工程验证。
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
3
3
 
4
4
  ## 适用问题
5
- 当压缩表示时需要同构保持数据的本质拓扑结构(连通性、环、空腔)时使用:隐空间压缩(环形流形不能压成线段)、知识蒸馏(学生-教师同调等价)、3D 网格简化(genus 不变)、KV-Cache 语义保持(聚类结构不坍缩)。核心诉求:**压缩维度或数量,保证持续同调的 persistence diagram 变化可控**。
5
+ 当压缩表示时需要同构保持数据的本质拓扑结构(连通性、环、空腔)时使用:隐空间压缩(环形流形不能压成线段)、知识蒸馏(学生-教师同调等价)、3D 网格简化(genus 不变)、KV-Cache 语义保持(聚类结构不坍缩)。核心诉求:**压缩维度或数量,同时约束并实测持续同调的 persistence diagram 变化**。
6
6
 
7
7
  ## 数学思想来源
8
- - 透镜:lenses/topological.md(拓扑不变量——连通性、洞数在连续变形下不变)、lenses/spectral.md(Gauss-Bonnet 连接曲率与欧拉示性数)、lenses/variational.md(压缩率 vs. 拓扑保真度的约束优化)
9
- - 知识:knowledge-base/topology/persistent-homology.md(持续同调、Vietoris-Rips 滤流、Bottleneck 距离)、knowledge-base/topology/euler-characteristic.md(欧拉示性数快速拓扑诊断)、knowledge-base/matrix-analysis/matrix-perturbation.md(Davis-Kahan 子空间扰动界)
8
+ - 透镜:../../lenses/topological.md(拓扑不变量——连通性、洞数在连续变形下不变)、../../lenses/spectral.md(Gauss-Bonnet 连接曲率与欧拉示性数)、../../lenses/variational.md(压缩率 vs. 拓扑保真度的约束优化)
9
+ - 知识:../../knowledge-base/topology/persistent-homology.md(持续同调、Vietoris-Rips 滤流、Bottleneck 距离)、../../knowledge-base/topology/euler-characteristic.md(欧拉示性数快速拓扑诊断)、../../knowledge-base/matrix-analysis/matrix-perturbation.md(Davis-Kahan 子空间扰动界)
10
10
 
11
11
  ## 需要的数学知识
12
12
  - **持续同调稳定性定理**:$d_B(D(X), D(Y)) \leq d_{GH}(X, Y)$,Hausdorff 距离界定 persistence diagram 变化
@@ -46,15 +46,15 @@
46
46
  - **拓扑诊断仪表板**:训练中实时绘制 $\beta_0(\epsilon), \beta_1(\epsilon)$ 曲线
47
47
 
48
48
  ## GPU 可行性
49
- - 张量化/GEMM:距离矩阵 `cdist` 核心为 GEMM;$\chi(\epsilon)$ 为阈值计数 + 累积和
50
- - 复杂度:完整持续同调 $O(N^3)$ 不可行;Euler curve $O(N^2 |\epsilon|)$ 可行;Landmark $O(m^3)$
51
- - 显存:$N \times N$ 距离矩阵在 $N > 8K$ 时需分块或 Landmark 降维
52
- - 低精度:距离计算在 bf16 下稳定(正数加法);$\chi$ 为整数运算无精度问题
53
- - 并行:$\epsilon$ 扫描的每个阈值独立并行;Landmark 选择可批并行
54
- - 算子融合:cdist + threshold + count 可融合避免物化大距离矩阵
49
+ - **D1[v]/D2[v]**:距离矩阵 `cdist` 核心为 GEMM;$\chi(\epsilon)$ 为阈值计数 + 累积和
50
+ - **D3[~]**:完整持续同调 $O(N^3)$ 不可行;Euler curve $O(N^2 |\epsilon|)$ 可行;Landmark $O(m^3)$
51
+ - **D4[~]**:$N \times N$ 距离矩阵在 $N > 8K$ 时需分块或 Landmark 降维
52
+ - **D5[v]**:距离计算在 bf16 下稳定(正数加法);$\chi$ 为整数运算无精度问题
53
+ - **D6[v]**:$\epsilon$ 扫描的每个阈值独立并行;Landmark 选择可批并行
54
+ - **D8[v]**:cdist + threshold + count 可融合避免物化大距离矩阵
55
55
 
56
56
  ## 论文表述方式
57
- "以持续同调的 Bottleneck 稳定性定理为理论基础,通过 Euler characteristic curve 匹配实现 $O(N^2)$ 复杂度的拓扑保真正则化,保证压缩后 Betti 数在持续区间上的偏差受 Hausdorff 距离控制。"
57
+ "以持续同调的 Bottleneck 稳定性定理为理论基础,通过 Euler characteristic curve landmark 近似构造可计算的拓扑正则。Bottleneck 距离可在滤流函数扰动受控时界定 persistence diagram 的变化;Euler curve 只是不完整代理,不能单独保证每阶 Betti 数偏差受控,需报告 persistence/Betti 曲线的实测偏差。"
58
58
 
59
59
  ## 风险
60
60
  - **持续同调计算瓶颈**:精确边界矩阵约化高度串行($O(N^3)$),必须依赖 Euler curve 或 Landmark 近似
@@ -1,12 +1,12 @@
1
1
  # Constraint Penalty
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.
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 design involves hard constraints (e.g., probability simplex, orthogonality, capacity limits, load balancing) but end-to-end training is required. Typical scenarios: (1) MoE routing probabilities must lie on the $K$-simplex with load balancing; (2) Expert activation count is constrained (top-k); (3) Subspace projection matrices must satisfy orthogonality $W^T W = I$; (4) Feature norms are bounded $\|z\| \leq R$. Core objective: **transform mathematical constraints into differentiable penalty terms integrated into gradient-based variational**.
6
6
 
7
7
  ## Mathematical Inspiration
8
- - Lenses: lenses/variational.md (constrained variational, Lagrangian duality, KKT conditions), lenses/geometric.md (manifold projection)
9
- - Knowledge: knowledge-base/optimization/lagrangian-duality.md (augmented Lagrangian method, penalty function method), knowledge-base/matrix-analysis/projection.md (projection operators, constraint sets)
8
+ - Lenses: ../../lenses/variational.en.md (constrained variational, Lagrangian duality, KKT conditions), ../../lenses/geometric.en.md (manifold projection)
9
+ - Knowledge: ../../knowledge-base/optimization/lagrangian-duality.en.md (augmented Lagrangian method, penalty function method), ../../knowledge-base/matrix-analysis/projection.en.md (projection operators, constraint sets)
10
10
 
11
11
  ## Required Mathematical Knowledge
12
12
  - **Penalty Function Method**: $\min f(x)$ s.t. $g(x)=0 \to \min f(x) + \rho/2 \cdot \|g(x)\|^2$ -- $\rho$ is gradually increased (exterior point method), driving constraint violation $\|g(x)\| \to 0$
@@ -29,6 +29,10 @@ Method 2 - Augmented Lagrangian Method (ALM):
29
29
  // lambda is a learnable parameter (nn.Parameter), updated via gradient ascent:
30
30
  lambda.data += rho * g(x).detach() // dual ascent step
31
31
  // Converges faster than pure penalty, avoids rho -> infinity
32
+ For inequality constraints h(x) <= 0:
33
+ L_ALM = Sum_j 1/(2*rho) * [max(0, lambda_j + rho*h_j(x))^2 - lambda_j^2]
34
+ // lambda update: lambda_j <- max(0, lambda_j + rho*h_j(x))
35
+ // Strictly feasible constraints (lambda_j + rho*h_j(x) < 0) are not penalized
32
36
 
33
37
  Method 3 - Softmax Projection onto Simplex (load balancing special case):
34
38
  p = softmax(logits / tau) // project onto Delta^{K-1}
@@ -43,7 +47,7 @@ Method 4 - Orthogonal Constraint Projection:
43
47
 
44
48
  ## Implementable Architectures
45
49
  - **Loss Wrapper**: ConstraintLoss(base_loss, constraints, rho_schedule) -- forward computes base_loss + Sum constraint.penalty()
46
- - **Dual Variable Management**: nn.Parameter stores lambda; negative learning rate set in the optimizer to achieve gradient ascent
50
+ - **Dual Variable Management**: equality multipliers can use nn.Parameter with a negative learning rate for gradient ascent; inequality multipliers must satisfy $\lambda \geq 0$, so use explicit projected updates `lambda <- max(0, lambda + rho*h(x))` or clamp after optimizer steps
47
51
  - **Warm-up Strategy**: Optimize only base_loss for the first $N$ steps, then progressively activate constraint penalties
48
52
  - **Constraint Monitoring**: Record $\|g(x)\|$ at each step for visualization and adaptive rho adjustment
49
53
 
@@ -58,7 +62,7 @@ Method 4 - Orthogonal Constraint Projection:
58
62
  - **Operator Fusion**: Constraint computation + weighted summation + merging with base_loss can be fused into a single kernel
59
63
 
60
64
  ## Paper Phrasing
61
- "We employ the augmented Lagrangian method to convert hard constraints $g(x)=0$ into differentiable penalty terms $\lambda^T g(x) + \rho/2 \|g(x)\|^2$. Through alternating dual ascent updates of $\lambda$, constraint satisfaction is guaranteed to converge at rate $O(1/\rho)$ without requiring $\rho \to \infty$, reducing constraint violations by 3x compared to pure penalty methods in experiments."
65
+ "We employ the augmented Lagrangian method to convert hard constraints $g(x)=0$ into differentiable penalty terms $\lambda^T g(x) + \rho/2 \|g(x)\|^2$, using alternating dual ascent updates of $\lambda$ rather than relying only on $\rho \to \infty$. Under convexity, constraint qualifications such as LICQ/MFCQ, and sufficiently accurate inner solves, ALM can converge to KKT solutions; in non-convex training, report the measured constraint-violation curve instead of claiming a universal $O(1/\rho)$ rate."
62
66
 
63
67
  ## Risks
64
68
  - Excessively fast rho growth leads to ill-conditioned variational landscapes (deteriorating condition numbers), causing gradient vanishing or explosion
@@ -1,5 +1,5 @@
1
1
  # Constraint Penalty(约束惩罚损失)
2
- > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / 不可行」。未标注的视为理论可行,需工程验证。
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
3
3
 
4
4
  ## 适用问题
5
5
  当设计中有硬约束(如概率单纯形、正交性、容量限制、负载均衡)但需要端到端训练时使用。
@@ -8,9 +8,9 @@
8
8
  核心诉求:**将数学约束转化为可微惩罚项,融入梯度优化流程**。
9
9
 
10
10
  ## 数学思想来源
11
- - 透镜:lenses/variational.md(约束优化、拉格朗日对偶、KKT 条件)、lenses/geometric.md(流形投影)
12
- - 知识:knowledge-base/optimization/lagrangian-duality.md(增广拉格朗日法、惩罚函数法)、
13
- knowledge-base/matrix-analysis/projection.md(投影算子、约束集)
11
+ - 透镜:../../lenses/variational.md(约束优化、拉格朗日对偶、KKT 条件)、../../lenses/geometric.md(流形投影)
12
+ - 知识:../../knowledge-base/optimization/lagrangian-duality.md(增广拉格朗日法、惩罚函数法)、
13
+ ../../knowledge-base/matrix-analysis/projection.md(投影算子、约束集)
14
14
 
15
15
  ## 需要的数学知识
16
16
  - **罚函数法**:min f(x) s.t. g(x)=0 → min f(x) + ρ/2 · ‖g(x)‖²
@@ -37,6 +37,10 @@
37
37
  // λ 为可学习参数(nn.Parameter),通过梯度上升更新:
38
38
  λ.data += ρ * g(x).detach() // 对偶上升步
39
39
  // 比纯罚函数收敛快,避免 ρ → ∞
40
+ 对不等式约束 h(x) ≤ 0:
41
+ L_ALM = Σ_j 1/(2ρ) · [max(0, λ_j + ρ·h_j(x))² - λ_j²]
42
+ // λ 更新:λ_j ← max(0, λ_j + ρ·h_j(x))
43
+ // 严格可行(λ_j + ρ·h_j(x) < 0)的约束不被惩罚
40
44
 
41
45
  方法3 - Softmax 投影到单纯形(负载均衡特例):
42
46
  p = softmax(logits / τ) // 投影到 Δ^{K-1}
@@ -52,7 +56,7 @@
52
56
  ## 可实现结构
53
57
  - **Loss Wrapper**:ConstraintLoss(base_loss, constraints, ρ_schedule)
54
58
  forward 时计算 base_loss + Σ constraint.penalty()
55
- - **对偶变量管理**:nn.Parameter 存储 λ,optimizer 中为 λ 设置负学习率实现梯度上升
59
+ - **对偶变量管理**:等式乘子 λ 可用 nn.Parameter + 负学习率做梯度上升;不等式乘子必须保持 λ ≥ 0,建议用显式投影更新 `λ ← max(0, λ + ρ·h(x))` 或在 optimizer step 后 clamp
56
60
  - **warm-up 策略**:前 N 步只优化 base_loss,之后逐步激活约束惩罚
57
61
  - **约束监控**:每 step 记录 ‖g(x)‖ 用于可视化和自适应 ρ 调整
58
62
 
@@ -68,8 +72,7 @@
68
72
 
69
73
  ## 论文表述方式
70
74
  "采用增广拉格朗日法将硬约束 g(x)=0 转化为可微惩罚项 λ^Tg(x) + ρ/2‖g(x)‖²,
71
- 通过对偶变量 λ 的交替上升更新,在无需 ρ→∞ 的条件下保证约束满足度以 O(1/ρ) 收敛,
72
- 相比纯罚函数法在实验中减少 3× 的约束违反。"
75
+ 并通过对偶变量 λ 的交替上升更新避免单纯依赖 ρ→∞。在凸性、约束规格(如 LICQ/MFCQ)和内层子问题求解精度满足条件时,ALM 可收敛到 KKT 解;非凸训练中应报告实际约束违反曲线,而不能宣称通用 O(1/ρ) 速率。"
73
76
 
74
77
  ## 风险
75
78
  - ρ 增大过快导致优化 landscape 病态(条件数恶化),梯度消失或爆炸
@@ -1,12 +1,12 @@
1
1
  # Contrastive 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.
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 model needs to learn "what is similar to what and what is different from what." Typical scenarios: (1) Different augmented views of the same input should be pulled closer (positive pairs), while different inputs should be pushed apart (negative pairs); (2) Shared representations should capture cross-task commonalities, while Private representations should distinguish task-specific features; (3) In the expert embedding space, similar inputs should be routed to the same expert. Core objective: **learn relative relationships rather than absolute values**.
6
6
 
7
7
  ## Mathematical Inspiration
8
- - Lenses: lenses/geometric.md (metric spaces and distance functions), lenses/probabilistic.md (mutual information maximization)
9
- - Knowledge: knowledge-base/probability/entropy.md (conditional distributions and likelihood), knowledge-base/differential-geometry/manifold.md (geodesics and curvature)
8
+ - Lenses: ../../lenses/geometric.en.md (metric spaces and distance functions), ../../lenses/probabilistic.en.md (mutual information maximization)
9
+ - Knowledge: ../../knowledge-base/probability/entropy.en.md (conditional distributions and likelihood), ../../knowledge-base/differential-geometry/manifold.en.md (geodesics and curvature)
10
10
 
11
11
  ## Required Mathematical Knowledge
12
12
  - **InfoNCE Loss**: $L = -\log[\exp(\text{sim}(q,k^+)/\tau) / \sum_j \exp(\text{sim}(q,k_j)/\tau)]$ -- essentially a lower-bound estimate of mutual information in contrastive learning; $\tau$ is the temperature parameter controlling distribution sharpness
@@ -14,6 +14,32 @@ When the model needs to learn "what is similar to what and what is different fro
14
14
  - **NT-Xent (Normalized Temperature-scaled Cross Entropy)**: Softmax contrastive loss on the unit sphere $S^{d-1}$, with normalization eliminating scale effects
15
15
  - **Debiased Contrastive Learning**: Corrects for false negatives in negative samples, using prior $\tau^+$ to estimate the true negative sample distribution
16
16
 
17
+ ## Alignment-Uniformity Framework
18
+
19
+ Representation quality in contrastive learning decomposes into two independent objectives (Wang & Isola, 2020):
20
+
21
+ - **Alignment**: Positive pairs should have similar representations
22
+ $L_{\text{align}} = \mathbb{E}_{(x, x^+)}[\|f(x) - f(x^+)\|^2]$
23
+ - **Uniformity**: Representations should be uniformly distributed on the unit hypersphere $S^{d-1}$
24
+ $L_{\text{uniform}} = \log \mathbb{E}_{x, x'}[\exp(-2\|f(x) - f(x')\|^2)]$
25
+
26
+ **InfoNCE and alignment-uniformity**: As $N \to \infty$ with appropriate temperature $\tau$, the InfoNCE loss asymptotically decomposes into alignment + uniformity. For finite $N$, InfoNCE provides a lower bound on $I(X;Z)$, with tightness increasing in $N$.
27
+
28
+ **Conditions for uniformity to hold**:
29
+ - Sufficiently large negative sample count $N$ (theory requires $N \to \infty$; in practice $N \geq 1024$ is typically sufficient)
30
+ - Temperature $\tau$ not too large ($\tau \to \infty$ causes loss to degenerate to a constant, eliminating the uniformity-driving force)
31
+ - Representation dimension $d$ sufficient to support the intrinsic dimension of the data
32
+
33
+ **Conditions for uniformity to fail**:
34
+ - Insufficient negatives $\rightarrow$ weak uniformity pressure; representations may cluster on a local region of the sphere
35
+ - Representation collapse: all inputs map to the same (or few) points, trivially minimizing alignment but completely destroying uniformity
36
+ - Temperature $\tau$ too large $\rightarrow$ softmax degenerates to uniform distribution, gradients vanish, no uniformity guarantee
37
+ - Severe positive/negative imbalance within the batch without queue compensation
38
+
39
+ **What can be guaranteed at most**: Under ideal conditions (sufficiently large $N$, appropriate $\tau$, no collapse), minimizing contrastive loss is equivalent to jointly maximizing positive-pair alignment and representation uniformity.
40
+
41
+ **What cannot be guaranteed**: Optimality of learned representations for downstream tasks (uniformity $\neq$ task relevance); semantic-level alignment (only geometric-level positive-pair proximity is guaranteed).
42
+
17
43
  ## AI Module Form
18
44
  ```
19
45
  Module: ContrastiveLoss
@@ -40,17 +66,17 @@ Hard Negative Mining:
40
66
  - **Multi-Granularity Contrast**: Apply contrastive objectives simultaneously at token-level, sequence-level, and expert-level
41
67
 
42
68
  ## GPU Feasibility
43
- - **Tensorization**: Similarity computation is $z_a @ z_n^T$ -- standard GEMM $(B \times d) @ (d \times N) = B \times N$
44
- - **GEMM-mappability**: Core computation is 1-2 matrix multiplications, perfectly mapped to cuBLAS
45
- - **Complexity**: $O(B \cdot N \cdot d)$ computation + $O(B \cdot N)$ storage for the logits matrix; approximately 64MB when B=256, N=65536
46
- - **Memory & KV-Cache**: Negative sample queue occupies $N \cdot d \cdot 4$ bytes, approximately 65536 * 256 * 4 = 64MB, fixed overhead
47
- - **Low Precision Stability**: Cosine similarity + softmax under fp16 requires caution for exp overflow; use log-sum-exp trick
48
- - **Parallelism & Communication**: On multiple GPUs, use all-gather to collect negatives from other GPUs to enlarge $N$ (MoCo v3 strategy)
49
- - **Sparse Structure**: After hard negative mining, only $k \ll N$ negatives are retained, effectively sparsifying the logits
50
- - **Operator Fusion**: L2-norm -> matmul -> scale -> log-softmax -> nll_loss can be fused
69
+ - **D1[v]**: Similarity computation is $z_a @ z_n^T$ -- standard GEMM $(B \times d) @ (d \times N) = B \times N$
70
+ - **D2[v]**: Core computation is 1-2 matrix multiplications, perfectly mapped to cuBLAS
71
+ - **D3[v]**: $O(B \cdot N \cdot d)$ computation + $O(B \cdot N)$ storage for the logits matrix; approximately 64MB when B=256, N=65536
72
+ - **D4[v]**: Negative sample queue occupies $N \cdot d \cdot 4$ bytes, approximately 65536 * 256 * 4 = 64MB, fixed overhead
73
+ - **D5[v]**: Cosine similarity + softmax under fp16 requires caution for exp overflow; use log-sum-exp trick
74
+ - **D6[v]**: On multiple GPUs, use all-gather to collect negatives from other GPUs to enlarge $N$ (MoCo v3 strategy)
75
+ - **D7[v]**: After hard negative mining, only $k \ll N$ negatives are retained, effectively sparsifying the logits
76
+ - **D8[v]**: L2-norm -> matmul -> scale -> log-softmax -> nll_loss can be fused
51
77
 
52
78
  ## Paper Phrasing
53
- "We employ temperature-scaled InfoNCE contrastive loss with a momentum encoder maintaining a negative sample queue of N=65536, maximizing the lower bound of positive-pair mutual information on the unit sphere. Theoretical analysis shows this lower bound converges at rate $O(1/\sqrt{N})$."
79
+ "We employ temperature-scaled InfoNCE contrastive loss with a momentum encoder maintaining a negative-sample queue, optimizing proxies for positive-pair alignment and representation uniformity on the unit sphere. Mutual-information lower bounds and sampling-error rates depend on the negative-sample distribution, independence assumptions, and queue staleness; report ablations over queue size, temperature, negative-mining strategy, and downstream metrics."
54
80
 
55
81
  ## Risks
56
82
  - Too small $\tau$ causes training instability (excessively large gradients); too large $\tau$ makes all samples indistinguishable (degenerates to uniform distribution)
@@ -1,5 +1,5 @@
1
1
  # Contrastive Loss(对比损失)
2
- > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / 不可行」。未标注的视为理论可行,需工程验证。
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
3
3
 
4
4
  ## 适用问题
5
5
  当需要让模型学会"什么和什么相似、什么和什么不同"时使用。典型场景:
@@ -8,9 +8,9 @@
8
8
  (3) 专家嵌入空间中相似输入应路由到同一专家。核心诉求:**学习相对关系而非绝对值**。
9
9
 
10
10
  ## 数学思想来源
11
- - 透镜:lenses/geometric.md(度量空间与距离函数)、lenses/probabilistic.md(互信息最大化)
12
- - 知识:knowledge-base/probability/entropy.md(条件分布与似然)、
13
- knowledge-base/differential-geometry/manifold.md(测地线与曲率)
11
+ - 透镜:../../lenses/geometric.md(度量空间与距离函数)、../../lenses/probabilistic.md(互信息最大化)
12
+ - 知识:../../knowledge-base/probability/entropy.md(条件分布与似然)、
13
+ ../../knowledge-base/differential-geometry/manifold.md(测地线与曲率)
14
14
 
15
15
  ## 需要的数学知识
16
16
  - **InfoNCE 损失**:L = -log[exp(sim(q,k⁺)/τ) / Σ_j exp(sim(q,k_j)/τ)]
@@ -22,6 +22,32 @@
22
22
  - **去偏对比学习 (Debiased Contrastive)**:
23
23
  修正负样本中的假阴性问题,使用先验 τ⁺ 估计真实负样本分布
24
24
 
25
+ ## 对齐-均匀性理论 (Alignment-Uniformity Framework)
26
+
27
+ 对比学习的表示质量可分解为两个独立目标(Wang & Isola, 2020):
28
+
29
+ - **对齐 (Alignment)**:正对的表示应接近
30
+ $L_{\text{align}} = \mathbb{E}_{(x, x^+)}[\|f(x) - f(x^+)\|^2]$
31
+ - **均匀性 (Uniformity)**:表示应在单位超球面 $S^{d-1}$ 上均匀分布
32
+ $L_{\text{uniform}} = \log \mathbb{E}_{x, x'}[\exp(-2\|f(x) - f(x')\|^2)]$
33
+
34
+ **InfoNCE 与对齐-均匀性的关系**:当 $N \to \infty$ 且温度 $\tau$ 适当时,InfoNCE 损失渐近分解为 alignment + uniformity 之和。有限 $N$ 下,InfoNCE 给出 $I(X;Z)$ 的下界,下界紧度随 $N$ 增大。
35
+
36
+ **均匀性成立的条件**:
37
+ - 负样本数 $N$ 充分大(理论要求 $N \to \infty$,实践中 $N \geq 1024$ 通常足够)
38
+ - 温度 $\tau$ 不过大($\tau \to \infty$ 时 loss 退化为常数,丧失均匀性驱动力)
39
+ - 表示维度 $d$ 足够支撑数据的本征维度
40
+
41
+ **均匀性不成立的条件**:
42
+ - 负样本不足 → 均匀性驱动力弱,表示可能聚集在球面局部
43
+ - 表示坍塌 (representation collapse):所有输入映射到同一/少数点,trivially 最小化 alignment 但完全丧失 uniformity
44
+ - 温度 $\tau$ 过大 → softmax 趋于均匀分布,梯度消失,无均匀性保证
45
+ - batch 内正负对比例严重失衡且未通过队列补偿
46
+
47
+ **最多能保证什么**:在理想条件下($N$ 充分大、$\tau$ 合适、无坍塌),对比损失最小化等价于同时最大化正对对齐度和表示均匀性。
48
+
49
+ **不能保证什么**:不能保证学到的表示对下游任务最优(均匀性 ≠ 任务相关性);不能保证语义层级的对齐(仅保证几何层面的正对接近)。
50
+
25
51
  ## AI 模块形式
26
52
  ```
27
53
  模块:ContrastiveLoss
@@ -48,18 +74,18 @@ Hard Negative Mining:
48
74
  - **多粒度对比**:同时在 token-level、sequence-level、expert-level 施加对比
49
75
 
50
76
  ## 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 可融合
77
+ - **D1[v]**:sim 计算为 z_a @ z_n^T → 标准 GEMM (B×d) @ (d×N) = B×N
78
+ - **D2[v]**:核心就是 1-2 次矩阵乘法,完美映射 cuBLAS
79
+ - **D3[v]**:O(B·N·d) 计算 + O(B·N) 存储 logits 矩阵,B=256,N=65536 时约 64MB
80
+ - **D4[v]**:负样本队列占 N·d·4 bytes ≈ 65536·256·4 = 64MB,固定开销
81
+ - **D5[v]**:cosine 相似度 + softmax 在 fp16 下需注意 exp 溢出,用 log-sum-exp trick
82
+ - **D6[v]**:多 GPU 时用 all-gather 收集其他 GPU 的负样本扩大 N(MoCo v3 策略)
83
+ - **D7[v]**:hard negative mining 后只保留 k<<N 个负样本,有效稀疏化 logits
84
+ - **D8[v]**:L2-norm → matmul → scale → log-softmax → nll_loss 可融合
59
85
 
60
86
  ## 论文表述方式
61
87
  "采用温度缩放的 InfoNCE 对比损失,通过动量编码器维护 N=65536 的负样本队列,
62
- 在单位球面上最大化正对互信息的下界,理论分析表明该下界以 O(1/√N) 收敛。"
88
+ 在单位球面上优化正对齐与表示均匀性代理。互信息下界和采样误差界依赖负样本分布、独立性与队列陈旧度假设,论文中应报告队列大小、温度、负样本策略和下游指标的消融。"
63
89
 
64
90
  ## 风险
65
91
  - τ 过小导致训练不稳定(梯度过大),τ 过大导致所有样本不分难易(退化均匀分布)
@@ -1,12 +1,12 @@
1
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.
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 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
6
 
7
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)
8
+ - Lenses: ../../lenses/probabilistic.en.md (information bottleneck principle, mutual information variational), ../../lenses/variational.en.md (Lagrangian duality)
9
+ - Knowledge: ../../knowledge-base/probability/kl-divergence.en.md (IB theory, rate-distortion function), ../../knowledge-base/probability/entropy.en.md (mutual information and conditional entropy)
10
10
 
11
11
  ## Required Mathematical Knowledge
12
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$
@@ -15,7 +15,7 @@ When a representation $Z$ must achieve an optimal balance between "retaining tas
15
15
  $I(Z;Y) \geq \mathbb{E}_{p(z,y)}[\log q(y|z)] + H(Y)$ (lower bound for prediction term)
16
16
  - **CPC (Contrastive Predictive Coding)**: InfoNCE lower bound on $I(Z_t; Z_{t+k})$
17
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')] \}$
18
+ $I(X;Z) = \sup_\phi \{ \mathbb{E}_{p(x,z)}[T_\phi(x,z)] - \log \mathbb{E}_{p(x)p(z)}[\exp(T_\phi(x,z))] \}$ (Donsker-Varadhan representation, $T_\phi$ is a neural network critic)
19
19
 
20
20
  ## AI Module Form
21
21
  ```
@@ -32,7 +32,7 @@ Method 1 - VIB (Variational Information Bottleneck):
32
32
 
33
33
  Method 2 - Contrastive Mutual Information Estimation (no distributional assumptions):
34
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
35
+ I_nwj(x;z) = E_{p(x,z)}[f(x,z)] - e^{-1} * E_{p(x)p(z)}[exp(f(x,z))] // f is a discriminator network, expectations over joint and product of marginals respectively
36
36
  L_IB_contrast = I_nwj(x;z) - beta * InfoNCE(z, y) // both terms differentiable
37
37
 
38
38
  Method 3 - Shared/Private IB Decomposition:
@@ -60,7 +60,7 @@ Method 3 - Shared/Private IB Decomposition:
60
60
  - **Operator Fusion**: Encoder forward + KL computation + discriminator forward can be partially fused
61
61
 
62
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."
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 optimization. IB theory provides upper bounds on generalization error, but actual generalization also depends on optimization dynamics, data distribution, and model capacity."
64
64
 
65
65
  ## Risks
66
66
  - Mutual information estimators (MINE/NWJ) have high variance, causing training instability; large batches or moving averages are needed
@@ -1,5 +1,5 @@
1
1
  # Information Bottleneck Loss(信息瓶颈损失)
2
- > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / 不可行」。未标注的视为理论可行,需工程验证。
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
3
3
 
4
4
  ## 适用问题
5
5
  当需要让表示 Z 在"保留任务相关信息"与"压缩输入冗余信息"之间取得最优平衡时使用。
@@ -8,9 +8,9 @@
8
8
  核心诉求:**信息最优压缩——不多不少,只保留有用的**。
9
9
 
10
10
  ## 数学思想来源
11
- - 透镜:lenses/probabilistic.md(信息瓶颈原理、互信息优化)、lenses/variational.md(拉格朗日对偶)
12
- - 知识:knowledge-base/probability/kl-divergence.md(IB 理论、率失真函数)、
13
- knowledge-base/probability/entropy.md(互信息与条件熵)
11
+ - 透镜:../../lenses/probabilistic.md(信息瓶颈原理、互信息优化)、../../lenses/variational.md(拉格朗日对偶)
12
+ - 知识:../../knowledge-base/probability/kl-divergence.md(IB 理论、率失真函数)、
13
+ ../../knowledge-base/probability/entropy.md(互信息与条件熵)
14
14
 
15
15
  ## 需要的数学知识
16
16
  - **信息瓶颈目标**:min I(X;Z) - β·I(Z;Y),压缩 X→Z 同时保留 Z 对 Y 的预测力
@@ -19,7 +19,7 @@
19
19
  I(Z;Y) ≥ E_{p(z,y)}[log q(y|z)] + H(Y) (用下界估计预测项)
20
20
  - **CPC (Contrastive Predictive Coding)**:I(Z_t; Z_{t+k}) 的 InfoNCE 下界
21
21
  - **MINE (Mutual Information Neural Estimation)**:
22
- I(X;Z) = sup_θ { E[log T_θ(x,z)] - log E[T_θ(x,z')] }
22
+ I(X;Z) = sup_φ { E_{p(x,z)}[T_φ(x,z)] - log E_{p(x)p(z)}[exp(T_φ(x,z))] } (Donsker-Varadhan 表示,T_φ 为神经网络 critic)
23
23
 
24
24
  ## AI 模块形式
25
25
  ```
@@ -36,7 +36,7 @@
36
36
 
37
37
  方法2 - 对比式互信息估计(无需分布假设):
38
38
  // 用 NWJ 估计器替代 KL
39
- I_nwj(x;z) = E[f(x,z)] - exp(E[f(x,z')] - 1) // f 为判别网络
39
+ I_nwj(x;z) = E_{p(x,z)}[f(x,z)] - e^{-1} · E_{p(x)p(z)}[exp(f(x,z))] // f 为判别网络,期望分别取 joint 和 product of marginals
40
40
  L_IB_contrast = I_nwj(x;z) - β · InfoNCE(z, y) // 两项均可微
41
41
 
42
42
  方法3 - Shared/Private IB 分解:
@@ -65,7 +65,7 @@
65
65
 
66
66
  ## 论文表述方式
67
67
  "基于信息瓶颈理论,将 Shared/Private 分解形式化为 min I(X;Z_s)+I(X;Z_p)-β₁I(Z_s;Y_c)-β₂I(Z_p;Y_s),
68
- 通过变分上下界替代互信息项实现端到端优化,理论上保证压缩-预测 Pareto 前沿的 β-最优性。"
68
+ 通过变分上下界替代互信息项实现端到端优化。IB 理论给出泛化误差的上界,但实际泛化还依赖于优化动态、数据分布、模型容量等因素。"
69
69
 
70
70
  ## 风险
71
71
  - 互信息估计(MINE/NWJ)方差大,训练不稳定,需要大 batch 或 moving average
@@ -1,12 +1,12 @@
1
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.
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
  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
6
 
7
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)
8
+ - Lenses: ../../lenses/projection.en.md (orthogonal projection and subspace decomposition), ../../lenses/variational.en.md (regularization and saddle points)
9
+ - Knowledge: ../../knowledge-base/matrix-analysis/projection.en.md (spectral theorem, SVD, Schur decomposition), ../../knowledge-base/probability/kl-divergence.en.md (redundancy and mutual information)
10
10
 
11
11
  ## Required Mathematical Knowledge
12
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$
@@ -18,17 +18,24 @@ In multi-expert / multi-task settings, representations learned by submodules are
18
18
  ```
19
19
  Module: OrthogonalDiversityLoss
20
20
  Input: K feature matrices {W_k in R^{d x r}}_{k=1}^K (weights or features of K submodules)
21
+ // Note: Grassmann distance requires QR decomposition of W_i first to obtain orthonormal bases Q_i
21
22
 
22
23
  Method 1 - Frobenius Orthogonal Regularization:
23
24
  L_orth = Sum_{i<j} ||W_i^T W_j||_F^2
24
25
  // Computation: O(K^2 * d * r^2), K typically <16 so cost is manageable
25
26
 
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
27
+ Method 2 - Subspace-overlap log barrier (based on principal angles):
28
+ sigma_k = singular values of SVD(Q_i^T Q_j) (= cos(theta_k), theta_k are principal angles)
29
+ // Must first orthonormalize W_i, W_j: Q_i = qr(W_i).Q, Q_j = qr(W_j).Q
30
+ // Otherwise singular values may exceed 1, making -log(1-sigma^2+eps) undefined
31
+ // Original formula sigma^2*(1-sigma^2) is wrong: penalty is 0 at BOTH sigma=0 (orthogonal) AND sigma=1 (complete overlap)!
32
+ // Fully overlapping subspaces receive zero penalty, defeating the orthogonality objective.
33
+ // Correct formula: log-barrier, 0 at sigma=0 and → +∞ as sigma→1
34
+ L_grass = Sum_{i<j} Sum_k -log(1 - sigma_k^2 + eps) // = -Sum log(sin^2(theta_k)), 0 when orthogonal (theta=pi/2), →∞ when overlapping (theta→0)
35
+
36
+ Method 3 - Efficient Normalized-Gram Decorrelation:
37
+ W_norm = column_normalize(concat([W_1,...,W_K])) // without normalization this also penalizes norms, not only angles
38
+ G = W_norm^T * W_norm // single GEMM
32
39
  L_corr = ||G * (1 - I)||_F^2 // mask out diagonal, penalize off-diagonal elements
33
40
  ```
34
41
 
@@ -38,17 +45,17 @@ Method 3 - Efficient Cosine Decorrelation:
38
45
  - **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
46
 
40
47
  ## 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
48
+ - **D1[v]**: Core operation is matmul ($W^T W$) -- standard GEMM, perfectly mapped to Tensor Cores
49
+ - **D2[v]**: Method 3 requires only 1 GEMM + 1 element-wise mask + Frobenius norm
50
+ - **D3[v]**: $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$
51
+ - **D4[v]**: Intermediate matrices are on the order of $d \times r \cdot K$, adding no KV-Cache overhead
52
+ - **D5[v]**: Frobenius norm is a sum of squares, safe under fp16; Grassmann SVD is recommended in fp32
53
+ - **D6[v]**: The $K$ pairs are embarrassingly parallel; can be distributed across GPUs with all-reduce
54
+ - **D7[v]**: If $W_k$ is itself sparse (e.g., MoE gate), masking further increases sparsity
55
+ - **D8[v]**: matmul -> mask -> square -> sum can be fused into a single CUDA kernel
49
56
 
50
57
  ## 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})$."
58
+ "We introduce an orthogonality regularizer L_orth = Sum_{i<j} ||Q_i^T Q_j||_F^2, where Q_i is an orthonormal basis for W_i, to penalize overlap between feature subspaces of different submodules. This can reduce linear redundancy, but any redundancy-decay rate requires random-subspace or data-distribution assumptions and should be reported through principal angles, mutual-information estimates, or downstream ablations."
52
59
 
53
60
  ## Risks
54
61
  - Excessively large lambda causes variational difficulties (orthogonal constraint conflicts with task objective); requires careful tuning or adaptive lambda
@@ -1,5 +1,5 @@
1
1
  # Orthogonality Loss(正交性损失)
2
- > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / 不可行」。未标注的视为理论可行,需工程验证。
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「[v] 已验证 / [~] 可改造需验证 / [x] 不可行」。未标注的视为理论可行,需工程验证。
3
3
 
4
4
  ## 适用问题
5
5
  多专家/多任务场景中,各子模块学习到的表示高度重叠、冗余,导致参数利用率低下。
@@ -7,9 +7,9 @@
7
7
  MoE 专家差异化、多任务 head 去相关。核心诉求:**让不同模块看到不同的东西**。
8
8
 
9
9
  ## 数学思想来源
10
- - 透镜:lenses/projection.md(正交投影与子空间分解)、lenses/variational.md(正则化与鞍点)
11
- - 知识:knowledge-base/matrix-analysis/projection.md(谱定理、SVD、Schur 分解)、
12
- knowledge-base/probability/kl-divergence.md(冗余度与互信息)
10
+ - 透镜:../../lenses/projection.md(正交投影与子空间分解)、../../lenses/variational.md(正则化与鞍点)
11
+ - 知识:../../knowledge-base/matrix-analysis/projection.md(谱定理、SVD、Schur 分解)、
12
+ ../../knowledge-base/probability/kl-divergence.md(冗余度与互信息)
13
13
 
14
14
  ## 需要的数学知识
15
15
  - **Frobenius 内积与正交性**:⟨A, B⟩_F = tr(A^T B),当 ⟨A, B⟩_F = 0 时 A⊥B
@@ -21,17 +21,24 @@ MoE 专家差异化、多任务 head 去相关。核心诉求:**让不同模
21
21
  ```
22
22
  模块:OrthogonalDiversityLoss
23
23
  输入:K 个特征矩阵 {W_k ∈ R^{d×r}}_{k=1}^K(K 个子模块的权重或特征)
24
+ // 注意:Grassmann 距离需先对 W_i 做 QR 分解取正交基 Q_i
24
25
 
25
26
  方法1 - Frobenius 正交正则:
26
27
  L_orth = Σ_{i<j} ‖W_i^T W_j‖_F²
27
28
  // 计算量:O(K² · d · r²),K 一般 <16 所以可控
28
29
 
29
- 方法2 - Grassmann 距离(基于主角度):
30
- σ_k = SVD(W_i^T W_j) 的奇异值
31
- L_grass = Σ_{i<j} Σ_k σ_k²(1 - σ_k²) // 惩罚非 0 非 1 的奇异值
30
+ 方法2 - 子空间重叠对数障碍(基于主角度):
31
+ σ_k = SVD(Q_i^T Q_j) 的奇异值(= cos(θ_k),θ_k 为主角度)
32
+ // 必须先将 W_i, W_j 正交归一化:Q_i = qr(W_i).Q, Q_j = qr(W_j).Q
33
+ // 否则奇异值可能 > 1,导致 -log(1-σ²+ε) 未定义
34
+ // ⚠ 原公式 σ²(1-σ²) 错误:σ=0(正交)和 σ=1(完全重叠)时惩罚均为 0!
35
+ // 完全重叠的子空间获得零惩罚,违背正交性目标。
36
+ // 正确公式:对数障碍,σ=0 时为 0,σ→1 时 → +∞
37
+ L_grass = Σ_{i<j} Σ_k -log(1 - σ_k² + ε) // = -Σ log(sin²θ_k),正交时 θ=π/2 → 0,重叠时 θ→0 → ∞
32
38
 
33
- 方法3 - 高效 cosine 去相关:
34
- G = concat([W_1,...,W_K])^T · concat([W_1,...,W_K]) // 一次 GEMM
39
+ 方法3 - 高效归一化 Gram 去相关:
40
+ W_norm = column_normalize(concat([W_1,...,W_K])) // 若不归一化,会同时惩罚范数而非只惩罚角度
41
+ G = W_norm^T · W_norm // 一次 GEMM
35
42
  L_corr = ‖G ⊙ (1 - I)‖_F² // mask 掉对角线,惩罚非对角元素
36
43
  ```
37
44
 
@@ -41,18 +48,17 @@ MoE 专家差异化、多任务 head 去相关。核心诉求:**让不同模
41
48
  - **分块计算**:当 K 很大时,对 (i,j) 对做 mini-batch 采样,每步只算 C(K,2) 中的 B 对
42
49
 
43
50
  ## 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
51
+ - **D1[v]**:核心操作为 matmul(W^T W)→ 标准 GEMM,完美映射 Tensor Core
52
+ - **D2[v]**:方法3 只需 1 次 GEMM + 1 次 element-wise mask + Frobenius 范数
53
+ - **D3[v]**:O(K·d·r) 存储 + O(d·r²·K) 或 O(K²·d·r²) 计算,K<16 时 negligible
54
+ - **D4[v]**:中间矩阵 d×r·K 量级,不增加 KV-Cache 负担
55
+ - **D5[v]**:Frobenius 范数为平方和,fp16 下 OK;Grassmann SVD 建议 fp32
56
+ - **D6[v]**:K 对之间 embarrassingly parallel,可分 GPU 计算后 all-reduce
57
+ - **D7[v]**:若 W_k 本身稀疏(如 MoE gate),mask 后稀疏度进一步提升
58
+ - **D8[v]**:matmul → mask → square → sum 可融合为单个 CUDA kernel
52
59
 
53
60
  ## 论文表述方式
54
- "我们引入正交性正则项 L_orth = Σ_{i<j}‖W_i^T W_j‖_F²,将各子模块的特征空间约束
55
- 到近似正交的 Grassmann 子流形上,理论上保证 K 个子空间的冗余度以 O(1/√d) 衰减。"
61
+ "我们引入正交性正则项 L_orth = Σ_{i<j}‖Q_i^T Q_j‖_F²(其中 Q_i 为 W_i 的正交基),惩罚不同子模块特征子空间的重叠。该项可降低线性相关冗余,但冗余衰减速率需要随机子空间或数据分布假设支撑,应通过主角度、互信息估计或下游消融实测报告。"
56
62
 
57
63
  ## 风险
58
64
  - λ 过大导致优化困难(正交约束与任务目标冲突),需 careful tuning 或 adaptive λ