math-skill 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/README.en-US.md +159 -200
  2. package/README.md +159 -200
  3. package/agents/math-critic.en.md +2 -2
  4. package/agents/math-critic.md +8 -8
  5. package/commands/ask.md +3 -10
  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 +29 -218
  97. package/knowledge-base/overview.md +29 -220
  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 +9 -3
  145. package/references/books/abstract-algebra.md +5 -5
  146. package/references/books/algebraic-geometry-rising-sea.md +5 -5
  147. package/references/books/differential-geometry.md +3 -3
  148. package/references/books/matrix-analysis.md +4 -4
  149. package/references/books/micro-lie-theory.md +3 -3
  150. package/references/books/optimization-ml.md +1 -1
  151. package/references/books/smooth-manifolds.md +3 -3
  152. package/references/gpu-friendly-math.en.md +8 -8
  153. package/references/gpu-friendly-math.md +2 -2
  154. package/references/inspiration.en.md +1 -1
  155. package/references/inspiration.md +2 -2
  156. package/skills/math-research-activator/SKILL.en.md +161 -106
  157. package/skills/math-research-activator/SKILL.md +162 -110
  158. package/skills/math-research-activator/original-texts.en.md +9 -9
  159. package/skills/math-research-activator/original-texts.md +11 -11
  160. package/commands/abstraction.md +0 -11
  161. package/commands/algorithmic-thinking.md +0 -11
  162. package/commands/axiomatization.md +0 -11
  163. package/commands/causal-inference.md +0 -11
  164. package/commands/discrete-combinatorial.md +0 -11
  165. package/commands/game-theory.md +0 -11
  166. package/commands/induction-analogy.md +0 -11
  167. package/commands/information-theory.md +0 -11
  168. package/commands/logic-deduction.md +0 -11
  169. package/commands/modeling.md +0 -11
  170. package/commands/optimization.md +0 -11
  171. package/commands/probability-statistics.md +0 -11
  172. package/commands/symmetry-invariance.md +0 -11
  173. package/commands/topological-thinking.md +0 -11
  174. package/commands/transformation.md +0 -11
  175. package/skills/abstraction/SKILL.en.md +0 -117
  176. package/skills/abstraction/SKILL.md +0 -121
  177. package/skills/abstraction/original-texts.en.md +0 -163
  178. package/skills/abstraction/original-texts.md +0 -175
  179. package/skills/algorithmic-thinking/SKILL.en.md +0 -132
  180. package/skills/algorithmic-thinking/SKILL.md +0 -138
  181. package/skills/algorithmic-thinking/original-texts.en.md +0 -253
  182. package/skills/algorithmic-thinking/original-texts.md +0 -256
  183. package/skills/axiomatization/SKILL.en.md +0 -144
  184. package/skills/axiomatization/SKILL.md +0 -151
  185. package/skills/axiomatization/original-texts.en.md +0 -154
  186. package/skills/axiomatization/original-texts.md +0 -160
  187. package/skills/causal-inference/SKILL.en.md +0 -147
  188. package/skills/causal-inference/SKILL.md +0 -151
  189. package/skills/causal-inference/original-texts.en.md +0 -136
  190. package/skills/causal-inference/original-texts.md +0 -167
  191. package/skills/discrete-combinatorial/SKILL.en.md +0 -124
  192. package/skills/discrete-combinatorial/SKILL.md +0 -131
  193. package/skills/discrete-combinatorial/original-texts.en.md +0 -184
  194. package/skills/discrete-combinatorial/original-texts.md +0 -185
  195. package/skills/game-theory/SKILL.en.md +0 -117
  196. package/skills/game-theory/SKILL.md +0 -123
  197. package/skills/game-theory/original-texts.en.md +0 -131
  198. package/skills/game-theory/original-texts.md +0 -131
  199. package/skills/induction-analogy/SKILL.en.md +0 -145
  200. package/skills/induction-analogy/SKILL.md +0 -152
  201. package/skills/induction-analogy/original-texts.en.md +0 -140
  202. package/skills/induction-analogy/original-texts.md +0 -154
  203. package/skills/information-theory/SKILL.en.md +0 -134
  204. package/skills/information-theory/SKILL.md +0 -140
  205. package/skills/information-theory/original-texts.en.md +0 -127
  206. package/skills/information-theory/original-texts.md +0 -127
  207. package/skills/logic-deduction/SKILL.en.md +0 -130
  208. package/skills/logic-deduction/SKILL.md +0 -135
  209. package/skills/logic-deduction/original-texts.en.md +0 -160
  210. package/skills/logic-deduction/original-texts.md +0 -173
  211. package/skills/modeling/SKILL.en.md +0 -135
  212. package/skills/modeling/SKILL.md +0 -139
  213. package/skills/modeling/original-texts.en.md +0 -162
  214. package/skills/modeling/original-texts.md +0 -165
  215. package/skills/optimization/SKILL.en.md +0 -129
  216. package/skills/optimization/SKILL.md +0 -135
  217. package/skills/optimization/original-texts.en.md +0 -167
  218. package/skills/optimization/original-texts.md +0 -168
  219. package/skills/probability-statistics/SKILL.en.md +0 -146
  220. package/skills/probability-statistics/SKILL.md +0 -151
  221. package/skills/probability-statistics/original-texts.en.md +0 -191
  222. package/skills/probability-statistics/original-texts.md +0 -193
  223. package/skills/symmetry-invariance/SKILL.en.md +0 -135
  224. package/skills/symmetry-invariance/SKILL.md +0 -139
  225. package/skills/symmetry-invariance/original-texts.en.md +0 -206
  226. package/skills/symmetry-invariance/original-texts.md +0 -221
  227. package/skills/topological-thinking/SKILL.en.md +0 -124
  228. package/skills/topological-thinking/SKILL.md +0 -128
  229. package/skills/topological-thinking/original-texts.en.md +0 -134
  230. package/skills/topological-thinking/original-texts.md +0 -164
  231. package/skills/transformation/SKILL.en.md +0 -120
  232. package/skills/transformation/SKILL.md +0 -124
  233. package/skills/transformation/original-texts.en.md +0 -204
  234. package/skills/transformation/original-texts.md +0 -216
@@ -0,0 +1,68 @@
1
+ # Optimal Transport Routing
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 a set of input tokens/samples must be assigned to a set of experts/sub-modules while pursuing globally optimal matching cost.
6
+ Typical scenarios: (1) Load-balanced MoE routing -- assigning N tokens to K experts with a cost matrix defined as negative similarity; (2) Cross-layer feature alignment -- transporting layer-l features to the optimal subset of layer-(l+1); (3) Multi-task sample assignment -- assigning samples in a batch to the most appropriate task head.
7
+ Core requirement: **globally optimal assignment, rather than greedy per-point decisions**.
8
+
9
+ ## Mathematical Inspiration
10
+ - Lenses: lenses/variational.md (convex variational, duality theory), lenses/geometric.md (Wasserstein distance)
11
+ - Knowledge: knowledge-base/optimization/lagrangian-duality.md (duality theory, constrained optimization),
12
+ knowledge-base/probability/entropy.md (entropy regularization, marginal constraints)
13
+
14
+ ## Required Mathematical Background
15
+ - **Discrete Optimal Transport**: min_{P in Pi(mu,nu)} <C, P> = sum_{ij} C_{ij} P_{ij}
16
+ where Pi(mu,nu) = {P >= 0 : P * 1 = mu, P^T * 1 = nu} is the set of couplings with marginal constraints
17
+ - **Entropy-Regularized Sinkhorn**: min <C,P> - eps * H(P) => P* = diag(u) * exp(-C/eps) * diag(v)
18
+ Solved by alternating row/column scaling (Sinkhorn-Knopp), convergence rate O(1/eps^2)
19
+ - **Wasserstein-1 Distance**: W_1(mu,nu) = min_{pi in Pi} E_pi[||x-y||] = sup_{||f||_L <= 1} E_mu[f] - E_nu[f]
20
+ Kantorovich-Rubinstein duality, used for continuous distribution matching
21
+ - **Gromov-Wasserstein**: When source/target spaces have different dimensions, minimizes the structure-preserving transport cost
22
+
23
+ ## AI Module Form
24
+ ```
25
+ Module: OptimalTransportRouter
26
+ Input: token representations X in R^{N x d}, expert embeddings E in R^{K x d}, capacity constraint cap in R^K
27
+
28
+ Cost matrix: C_{ik} = -sim(X_i, E_k) or ||X_i - E_k||^2 (N x K)
29
+
30
+ Sinkhorn routing (entropy-regularized):
31
+ K_mat = exp(-C / eps) // Gibbs kernel, eps = 0.05 ~ 0.1
32
+ for t = 1..T: // T = 5 ~ 20 iterations
33
+ u = a / (K_mat @ v) // row scaling, a = 1/N
34
+ v = b / (K_mat^T @ u) // column scaling, b = cap / sum(cap)
35
+ P = diag(u) @ K_mat @ diag(v) // optimal transport plan, doubly stochastic matrix
36
+ assignment = argmax(P, dim=1) // hard assignment (at inference)
37
+ // At training: weighted_features = P @ E (soft assignment, differentiable)
38
+
39
+ Capacity constraint (b vector):
40
+ b_k = total_tokens / K // uniform allocation
41
+ b_k = alpha * uniform + (1 - alpha) * learned // learned non-uniform allocation
42
+ ```
43
+
44
+ ## Implementable Structures
45
+ - **Sinkhorn layer**: Custom autograd Function; forward pass performs Sinkhorn iterations, backward pass uses the implicit function theorem for gradients
46
+ - **Fixed iteration count**: T = 10 fixed iterations => can be unrolled into a computation graph (unrolled variational)
47
+ - **Log-domain stabilization**: Convert Sinkhorn to log domain to avoid exp overflow:
48
+ log_u = log_a - logsumexp(log_K + log_v, dim=1)
49
+ - **Batch OT**: Solve independently per micro-batch, parallelize Sinkhorn iterations
50
+
51
+ ## GPU Feasibility
52
+ - **Tensorization**: Sinkhorn core is matrix-vector multiplication K @ v of shape (N x K) * (K x 1), standard GEMV
53
+ - **GEMM-mappable**: Computation of C via X @ E^T is GEMM (N x d) @ (d x K); Sinkhorn iterations are GEMV
54
+ - **Complexity**: O(N * K * T) where T = 10 ~ 20; for N = 2048, K = 64 approximately 2.6M FLOPs, negligible
55
+ - **Memory & KV-Cache**: Storing C (N x K) and P (N x K); for N = 2048, K = 64 approximately 1 MB
56
+ - **Low-precision stability**: Sinkhorn in fp16 may cause exp(-C/eps) overflow; log-domain + fp32 recommended
57
+ - **Parallelism & Communication**: Batch dimension is independent; Sinkhorn iterations have sequential dependencies, but each iteration's matvec is highly parallel
58
+ - **Sparse structure**: As eps -> 0, P approaches sparsity (permutation matrix); top-k approximation can be used for acceleration
59
+ - **Operator fusion**: The exp -> matvec -> division pipeline in a single Sinkhorn step can be fused into a CUDA kernel
60
+
61
+ ## Paper-Worthy Formulation
62
+ "We formulate token-to-expert routing as an entropy-regularized optimal transport problem, obtaining an epsilon-approximate doubly stochastic assignment matrix via the Sinkhorn-Knopp algorithm within T = 10 iterations. The transport cost is guaranteed to converge to the global optimum within O(eps * log N), while marginal constraints b precisely control the load upper bound per expert."
63
+
64
+ ## Risks
65
+ - Excessively small eps causes numerical instability in Sinkhorn (exp overflow); requires log-domain or increasing eps
66
+ - Fixed unrolling of T iterations limits solution precision; too many iterations increase latency
67
+ - The N x K cost matrix creates significant memory pressure when both N and K are large (N = 32K, K = 256 => 32 MB)
68
+ - Training-inference discrepancy: performance gap between soft assignment and hard assignment
@@ -0,0 +1,72 @@
1
+ # Optimal Transport Routing(最优传输路由)
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
3
+
4
+ ## 适用问题
5
+ 当需要将一组输入 token/样本 分配到一组专家/子模块,且追求全局最优的匹配代价时使用。
6
+ 典型场景:(1) 负载均衡的 MoE 路由——将 N 个 token 分配到 K 个专家,代价矩阵为负相似度;
7
+ (2) 跨层特征对齐——将第 l 层特征传输到第 l+1 层的最优子集;
8
+ (3) 多任务样本分配——将 batch 中的样本分配到最合适的任务头。
9
+ 核心诉求:**全局最优分配,而非贪心逐点决策**。
10
+
11
+ ## 数学思想来源
12
+ - 透镜:lenses/variational.md(凸优化、对偶理论)、lenses/geometric.md(Wasserstein 距离)
13
+ - 知识:knowledge-base/optimization/lagrangian-duality.md(对偶理论、约束优化)、
14
+ knowledge-base/probability/entropy.md(熵正则化、边际约束)
15
+
16
+ ## 需要的数学知识
17
+ - **离散最优传输**:min_{P∈Π(μ,ν)} ⟨C, P⟩ = Σ_{ij} C_{ij} P_{ij}
18
+ 其中 Π(μ,ν) = {P ≥ 0 : P·1 = μ, P^T·1 = ν} 为边际约束耦合集
19
+ - **熵正则化 Sinkhorn**:min ⟨C,P⟩ - ε·H(P) → P* = diag(u)·exp(-C/ε)·diag(v)
20
+ 通过交替缩放行/列(Sinkhorn-Knopp)求解,收敛速度 O(1/ε²)
21
+ - **Wasserstein-1 距离**:W_1(μ,ν) = min_{π∈Π} E_π[‖x-y‖] = sup_{‖f‖_L≤1} E_μ[f] - E_ν[f]
22
+ Kantorovich-Rubinstein 对偶,用于连续分布匹配
23
+ - **Gromov-Wasserstein**:当源/目标空间维度不同时,min 结构保持的传输代价
24
+
25
+ ## AI 模块形式
26
+ ```
27
+ 模块:OptimalTransportRouter
28
+ 输入:token 表示 X ∈ R^{N×d},专家嵌入 E ∈ R^{K×d},容量约束 cap ∈ R^K
29
+
30
+ 代价矩阵:C_{ik} = -sim(X_i, E_k) 或 ‖X_i - E_k‖² (N×K)
31
+
32
+ Sinkhorn 路由(熵正则化):
33
+ K_mat = exp(-C / ε) // Gibbs kernel, ε=0.05~0.1
34
+ for t = 1..T: // T=5~20 次迭代
35
+ u = a / (K_mat @ v) // 行缩放,a = 1/N
36
+ v = b / (K_mat^T @ u) // 列缩放,b = cap/sum(cap)
37
+ P = diag(u) @ K_mat @ diag(v) // 最优传输计划,双随机矩阵
38
+ assignment = argmax(P, dim=1) // 硬分配(推理时)
39
+ // 训练时:weighted_features = P @ E (软分配,可微)
40
+
41
+ 容量约束(b 向量):
42
+ b_k = total_tokens / K // 均匀分配
43
+ b_k = α·uniform + (1-α)·learned // 学习非均匀分配
44
+ ```
45
+
46
+ ## 可实现结构
47
+ - **Sinkhorn 层**:自定义 autograd Function,前向做 Sinkhorn 迭代,反向用隐函数定理求梯度
48
+ - **迭代次数固定**:T=10 次固定迭代 → 可展开为计算图(unrolled variational)
49
+ - **log-domain 稳定化**:将 Sinkhorn 转换到 log 域避免 exp 溢出:
50
+ log_u = log_a - logsumexp(log_K + log_v, dim=1)
51
+ - **Batch OT**:每个 micro-batch 独立求解,并行化 Sinkhorn 迭代
52
+
53
+ ## GPU 可行性
54
+ - **张量化**:Sinkhorn 核心为矩阵向量乘法 K@v (N×K)·(K×1),标准 GEMV
55
+ - **GEMM 可映射**:C 的计算 X@E^T 为 GEMM (N×d)@(d×K);Sinkhorn 迭代为 GEMV
56
+ - **复杂度**:O(N·K·T) 其中 T=10~20,当 N=2048, K=64 时约 2.6M FLOPs,极小
57
+ - **显存与 KV-Cache**:存储 C(N×K) 和 P(N×K),N=2048,K=64 时约 1MB
58
+ - **低精度稳定**:Sinkhorn 在 fp16 下 exp(-C/ε) 可能溢出,建议 log-domain + fp32
59
+ - **并行与通信**:batch 维度独立;Sinkhorn 迭代为顺序依赖,但每次迭代的 matvec 高度并行
60
+ - **稀疏结构**:ε→0 时 P 趋于稀疏(permutation matrix),可用 top-k 近似加速
61
+ - **算子融合**:exp → matvec → division 的 Sinkhorn 单步可融合为 CUDA kernel
62
+
63
+ ## 论文表述方式
64
+ "将 token-to-expert 路由建模为熵正则化最优传输问题,通过 Sinkhorn-Knopp 算法在 T=10 次
65
+ 迭代内求得 ε-近似最优的双随机分配矩阵,理论保证传输代价在 O(ε·log N) 内收敛到全局最优,
66
+ 同时通过边际约束 b 精确控制各专家的负载上限。"
67
+
68
+ ## 风险
69
+ - ε 过小导致 Sinkhorn 数值不稳定(exp 溢出),需用 log-domain 或增大 ε
70
+ - T 次迭代的固定展开限制了解精度,过多 T 增加延迟
71
+ - N×K 代价矩阵在 N, K 都很大时内存压力显著(N=32K, K=256 → 32MB)
72
+ - 训练-推理不一致:软分配 vs 硬分配的性能 gap
@@ -0,0 +1,88 @@
1
+ # Spectral Clustering Routing
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 routing needs to be based on the intrinsic similarity structure of tokens/samples. Typical scenarios:
6
+ (1) Unsupervised expert assignment -- when no routing labels are available, spectral clustering automatically discovers natural token clusters;
7
+ (2) Adaptive expert initialization -- use spectral clustering results to initialize expert parameters at the beginning of training;
8
+ (3) Input-aware dynamic clustering -- different batches have different token distributions, requiring adaptive routing;
9
+ (4) Multi-granularity clustering -- different layers use spectral clustering at different granularities (coarse to fine).
10
+ Core requirement: **discover the intrinsic cluster structure of data for routing or expert initialization**.
11
+
12
+ ## Mathematical Inspiration
13
+ - Lenses: lenses/geometric.md (spectral graph theory, Laplacian eigenmaps), lenses/variational.md (relaxation and approximation)
14
+ - Knowledge: knowledge-base/matrix-analysis/projection.md (eigendecomposition, Rayleigh quotient),
15
+ knowledge-base/differential-geometry/manifold.md (manifold learning, graph cuts)
16
+
17
+ ## Required Mathematical Background
18
+ - **Spectral Clustering (Ng-Jordan-Weiss)**:
19
+ 1. Construct similarity graph W_{ij} = exp(-||x_i - x_j||^2 / (2 sigma^2))
20
+ 2. Compute normalized Laplacian L_sym = I - D^{-1/2} W D^{-1/2}
21
+ 3. Extract the k smallest eigenvectors U_k in R^{N x k}
22
+ 4. Apply k-means to the rows of U_k to obtain k clusters
23
+ - **Nystrom Approximation**: When N is too large for the full W matrix, sample m << N points
24
+ W approximately C * W_m^{-1} * C^T, reducing eigendecomposition to m x m
25
+ - **Spectral Relaxation Continuation**: Discrete cluster assignment => continuous eigenvectors => differentiable routing
26
+ Use softmax(U_k * W_proj) instead of hard k-means assignment
27
+ - **Power Iteration Acceleration**: Full eigendecomposition is unnecessary; only the top k eigenvectors are needed
28
+ Use Lanczos/Arnoldi iteration O(N^2 * k * iter) or randomized SVD O(N * k * log k)
29
+
30
+ ## AI Module Form
31
+ ```
32
+ Module: SpectralClusterRouter
33
+ Input: X in R^{N x d}, number of clusters K
34
+
35
+ Method 1 - Online Spectral Clustering Routing (periodic updates during training):
36
+ // Update cluster centers every M steps; use nearest neighbor at inference
37
+ W = exp(-cdist(X_sample, X_sample) / (2 sigma^2)) // m x m sampled similarity
38
+ L = I - D^{-1/2} W D^{-1/2} // normalized Laplacian
39
+ U_k = eigsh(L, k=K, which='SM') // K smallest eigenvectors
40
+ centers = kmeans(U_k, K) // K cluster centers
41
+ // Routing: project new tokens into spectral space and assign
42
+ proj = X @ W_proj // N -> K dimensional projection (learnable)
43
+ assignment = argmin(cdist(proj, centers)) // nearest center assignment
44
+
45
+ Method 2 - Differentiable Spectral Routing (end-to-end):
46
+ // Use softmax relaxation instead of hard assignment
47
+ sim_matrix = X @ X^T // N x N (or sampled m x m)
48
+ A = exp(sim_matrix / tau) // similarity graph (learnable tau)
49
+ D_inv_sqrt = diag(1 / sqrt(sum(A, dim=1) + eps))
50
+ L_norm = I - D_inv_sqrt @ A @ D_inv_sqrt // normalized Laplacian
51
+ // Approximate top K eigenvectors (power iteration + orthogonalization)
52
+ U_k = power_iteration_approx(L_norm, K, steps=5) // N x K
53
+ // Soft assignment
54
+ cluster_logits = U_k @ W_cluster // N x K (learnable projection)
55
+ route_probs = softmax(cluster_logits / tau_route) // soft routing probabilities
56
+
57
+ Method 3 - Anchor Spectral Clustering (large-scale):
58
+ anchors = kmeans_pp(X, m) // m anchor points, m << N
59
+ Z = exp(-cdist(X, anchors) / (2 sigma^2)) // N x m affinity matrix
60
+ L_anchor = I - D_z^{-1/2} Z^T Z D_z^{-1/2} // m x m Laplacian
61
+ U_k = eigsh(L_anchor, K) // m x K eigenvectors
62
+ route = Z @ U_k @ W_proj // N x K routing scores
63
+ ```
64
+
65
+ ## Implementable Structures
66
+ - **Periodic offline clustering**: Every N_step steps, collect token representations => offline spectral clustering => update routing table
67
+ - **Nystrom sampling**: Randomly sample m = 1024 representative points, reducing the N x N problem to m x m
68
+ - **Power iteration implementation**: 5-10 power iteration steps + Gram-Schmidt orthogonalization, GPU-friendly
69
+ - **Progressive training**: Early stage uses k-means coarse routing => mid-stage spectral clustering refinement => late-stage fine-tuning of routing network
70
+
71
+ ## GPU Feasibility
72
+ - **Tensorization**: Similarity matrix X @ X^T is GEMM; Laplacian construction is element-wise + diagonal matrix operations
73
+ - **GEMM-mappable**: Z^T @ Z in Method 3 is GEMM (m x N) @ (N x m); Z @ U_k is GEMM (N x m) @ (m x K)
74
+ - **Complexity**: Full spectral clustering O(N^2 * K) does not scale; Nystrom O(N * m * K + m^3); power iteration O(N^2 * K * T)
75
+ - **Memory & KV-Cache**: N x N similarity matrix exceeds 64 MB when N > 4096; sampling-based dimensionality reduction is essential
76
+ - **Low-precision stability**: Eigendecomposition recommended in fp32; exp(-dist/sigma^2) in fp16 requires distance clipping
77
+ - **Parallelism & Communication**: Power iteration matvec is highly parallel; k-means assign + update steps can be batch-parallelized
78
+ - **Sparse structure**: k-NN graph replaces fully connected graph; W sparsity > 95%, enabling SpMM acceleration
79
+ - **Operator fusion**: Diagonal scaling in D^{-1/2} @ A @ D^{-1/2} can be fused; cdist + exp + normalize can be fused
80
+
81
+ ## Paper-Worthy Formulation
82
+ "We achieve differentiable routing via continuous relaxation of spectral clustering: constructing the normalized Laplacian of the token similarity graph, reducing the O(N^2) eigendecomposition to O(Nm + K^3) through Nystrom approximation, and enabling GPU-friendly online spectral clustering via power iteration. Clustering quality, measured by Normalized Cut, guarantees an O(sqrt(log(N/K))) approximation ratio."
83
+
84
+ ## Risks
85
+ - The memory and computation cost of the N x N similarity matrix does not scale for long sequences; sampling or k-NN sparsification is mandatory
86
+ - Eigendecomposition is non-differentiable (gradients undefined when eigenvalues coincide); end-to-end training requires relaxation or stop-gradient
87
+ - The number of clusters K must be specified a priori, and re-clustering is needed when K changes
88
+ - The bandwidth parameter sigma is sensitive to clustering quality: too small causes isolated points, too large causes cluster merging
@@ -0,0 +1,90 @@
1
+ # Spectral Clustering Routing(谱聚类路由)
2
+ > **严谨性声明**:本文件中涉及复杂度、显存、FlashAttention 融合、Tensor Core、KV-Cache 压缩的结论均标注为「✅ 已验证 / ⚠️ 可改造需验证 / ❌ 不可行」。未标注的视为理论可行,需工程验证。
3
+
4
+ ## 适用问题
5
+ 当需要基于 token/样本的内在相似性结构进行分组路由时使用。典型场景:
6
+ (1) 无监督专家分配——没有路由标签时,用谱聚类自动发现 token 的自然分簇;
7
+ (2) 自适应专家初始化——训练初期用谱聚类结果初始化专家参数;
8
+ (3) 输入感知的动态聚类——不同 batch 的 token 分布不同,路由应自适应;
9
+ (4) 多粒度聚类——不同层使用不同粒度的谱聚类(粗→细)。
10
+ 核心诉求:**发现数据内在的簇结构,用于路由或专家初始化**。
11
+
12
+ ## 数学思想来源
13
+ - 透镜:lenses/geometric.md(谱图论、拉普拉斯特征映射)、lenses/variational.md(松弛与近似)
14
+ - 知识:knowledge-base/matrix-analysis/projection.md(特征值分解、Rayleigh 商)、
15
+ knowledge-base/differential-geometry/manifold.md(流形学习、图割)
16
+
17
+ ## 需要的数学知识
18
+ - **谱聚类 (Ng-Jordan-Weiss)**:
19
+ 1. 构造相似度图 W_{ij} = exp(-‖x_i - x_j‖² / 2σ²)
20
+ 2. 计算归一化拉普拉斯 L_sym = I - D^{-1/2} W D^{-1/2}
21
+ 3. 取前 k 个最小特征向量 U_k ∈ R^{N×k}
22
+ 4. 对 U_k 的行做 k-means 得到 k 个簇
23
+ - **Nyström 近似**:当 N 太大无法计算完整 W 时,采样 m<<N 个点
24
+ W ≈ C · W_m^{-1} · C^T,将特征分解降维到 m×m
25
+ - **谱松弛连续化**:离散聚类分配 → 连续特征向量 → 可微路由
26
+ 用 softmax(U_k · W_proj) 替代硬 k-means 分配
27
+ - **幂迭代加速**:不需完整特征分解,只需前 k 个特征向量
28
+ 用 Lanczos/Arnoldi 迭代 O(N²·k·iter) 或 randomized SVD O(N·k·log k)
29
+
30
+ ## AI 模块形式
31
+ ```
32
+ 模块:SpectralClusterRouter
33
+ 输入:X ∈ R^{N×d},簇数 K
34
+
35
+ 方法1 - 在线谱聚类路由(训练时周期性更新):
36
+ // 每 M 步更新一次聚类中心,推理时用最近邻
37
+ W = exp(-cdist(X_sample, X_sample) / (2σ²)) // m×m 采样相似度
38
+ L = I - D^{-1/2} W D^{-1/2} // 归一化拉普拉斯
39
+ U_k = eigsh(L, k=K, which='SM') // 前 K 个最小特征向量
40
+ centers = kmeans(U_k, K) // K 个聚类中心
41
+ // 路由:将新 token 投影到谱空间后分配
42
+ proj = X @ W_proj // N→K 维投影(可学习)
43
+ assignment = argmin(cdist(proj, centers)) // 最近中心分配
44
+
45
+ 方法2 - 可微谱路由(端到端):
46
+ // 用 softmax 松弛替代硬分配
47
+ sim_matrix = X @ X^T // N×N(或采样 m×m)
48
+ A = exp(sim_matrix / τ) // 相似度图(可学习 τ)
49
+ D_inv_sqrt = diag(1 / sqrt(sum(A, dim=1) + ε))
50
+ L_norm = I - D_inv_sqrt @ A @ D_inv_sqrt // 归一化拉普拉斯
51
+ // 近似前 K 个特征向量(幂迭代 + 正交化)
52
+ U_k = power_iteration_approx(L_norm, K, steps=5) // N×K
53
+ // 软分配
54
+ cluster_logits = U_k @ W_cluster // N×K(可学习投影)
55
+ route_probs = softmax(cluster_logits / τ_route) // 软路由概率
56
+
57
+ 方法3 - 锚点谱聚类(大规模):
58
+ anchors = kmeans_pp(X, m) // m 个锚点,m << N
59
+ Z = exp(-cdist(X, anchors) / (2σ²)) // N×m 亲和矩阵
60
+ L_anchor = I - D_z^{-1/2} Z^T Z D_z^{-1/2} // m×m 拉普拉斯
61
+ U_k = eigsh(L_anchor, K) // m×K 特征向量
62
+ route = Z @ U_k @ W_proj // N×K 路由分数
63
+ ```
64
+
65
+ ## 可实现结构
66
+ - **周期性离线聚类**:每 N_step 步收集 token 表示 → 离线谱聚类 → 更新路由表
67
+ - **Nyström 采样**:随机采样 m=1024 个代表点,将 N×N 问题降为 m×m
68
+ - **幂迭代实现**:5~10 步幂迭代 + Gram-Schmidt 正交化,GPU 友好
69
+ - **渐进式训练**:初期用 k-means 粗路由 → 中期谱聚类精化 → 后期可微调路由网络
70
+
71
+ ## GPU 可行性
72
+ - **张量化**:相似度矩阵 X@X^T 为 GEMM;拉普拉斯构造为 element-wise + 对角矩阵
73
+ - **GEMM 可映射**:方法3 的 Z^T@Z 为 GEMM (m×N)@(N×m);Z@U_k 为 GEMM (N×m)@(m×K)
74
+ - **复杂度**:完整谱聚类 O(N²·K) 不可扩展;Nyström O(N·m·K+m³);幂迭代 O(N²·K·T)
75
+ - **显存与 KV-Cache**:N×N 相似度矩阵在 N>4096 时 >64MB,必须采样降维
76
+ - **低精度稳定**:特征分解建议 fp32;exp(-dist/σ²) 在 fp16 下需 clip distance
77
+ - **并行与通信**:幂迭代的 matvec 高度并行;k-means 的 assign+update 可批并行
78
+ - **稀疏结构**:k-NN 图替代全连接图,W 稀疏度 >95%,SpMM 加速
79
+ - **算子融合**:D^{-1/2}@A@D^{-1/2} 的对角缩放可融合;cdist+exp+normalize 可融合
80
+
81
+ ## 论文表述方式
82
+ "利用谱聚类的连续松弛实现可微路由:构造 token 相似度图的归一化拉普拉斯,
83
+ 通过 Nyström 近似将 O(N²) 特征分解降为 O(Nm+K³),配合幂迭代实现 GPU 友好的
84
+ 在线谱聚类,聚类质量以 Normalized Cut 衡量保证 O(√(log N/K)) 的近似比。"
85
+
86
+ ## 风险
87
+ - N×N 相似度矩阵的显存和计算在长序列下不可扩展,必须采样或 k-NN 稀疏化
88
+ - 特征分解不可微(特征值重合时梯度未定义),端到端训练需松弛或 stop-gradient
89
+ - 簇数 K 需先验指定,且 K 变化时需重新聚类
90
+ - σ(带宽参数)对聚类质量敏感,过小导致孤立点,过大导致合并
@@ -0,0 +1,51 @@
1
+ # Connection
2
+
3
+ ## Minimal Definition
4
+
5
+ A connection $\nabla$ is a rule specifying "how to transport a vector along a curve from one point to another without extraneous rotation." Formally, $\nabla: \mathfrak{X}(M) \times \mathfrak{X}(M) \to \mathfrak{X}(M)$ satisfies the Leibniz rule. The Levi-Civita connection is the unique connection that is metric-compatible ($\nabla g = 0$) and torsion-free ($T = 0$).
6
+
7
+ ## Core Formulas
8
+
9
+ - Covariant derivative: $\nabla_X Y = \left(X^i \partial_i Y^k + X^i Y^j \Gamma^k_{ij}\right) \partial_k$
10
+ - Christoffel symbols (Levi-Civita): $\Gamma^k_{ij} = \frac{1}{2} g^{kl}(\partial_i g_{jl} + \partial_j g_{il} - \partial_l g_{ij})$
11
+ - Parallel transport equation: $\frac{D V^k}{dt} = \dot V^k + \Gamma^k_{ij} \dot\gamma^i V^j = 0$
12
+ - Connection form (on a principal bundle): $\omega \in \Omega^1(P, \mathfrak{g})$; the gauge field $A_\mu$ is a local connection form
13
+ - Curvature = non-commutativity of the connection: $R(X,Y) = [\nabla_X, \nabla_Y] - \nabla_{[X,Y]}$
14
+
15
+ ## Applicable Problems
16
+
17
+ - Cross-point vector comparison: tangent spaces at different points cannot be directly summed; a connection specifies the "transport rule"
18
+ - Momentum/state transport in optimization: historical gradients in Riemannian Adam must be carried across steps via parallel transport
19
+ - Gauge-equivariant networks: the freedom in choosing local coordinate frames (gauges) is aligned by the connection
20
+ - Physically constrained systems: the electromagnetic field equals the curvature of a U(1) connection; Yang-Mills equals the curvature of a non-Abelian connection
21
+
22
+ ## AI Design Translation
23
+
24
+ - **Vector transport module**: In Riemannian optimizers, transport the momentum $m_k \in T_{x_k}M$ to $T_{x_{k+1}}M$; closed-form transport (e.g., projection on the Stiefel manifold) can be expressed as GEMM
25
+ - **Gauge-equivariant CNN**: On manifolds/meshes, each edge carries a $G$-connection element that aligns the local frames of adjacent points, making convolution kernels invariant to local coordinate choices
26
+ - **Parallel transport regularization**: Penalize non-parallelism of the feature field under the connection $\|\nabla_X f\|^2$, enforcing smooth feature variation along the manifold
27
+ - **Connection learning parameterization**: Parameterize Christoffel symbols as neural network outputs, learning the "optimal transport rule" on the data manifold
28
+
29
+ ## Engineering Feasibility
30
+
31
+ GPU friendliness: the core challenge of connections is "serial ODE integration."
32
+ - **Closed-form parallel transport** (specific manifolds such as SO(3), Stiefel): single-step matrix operations, batchable, GPU-friendly
33
+ - **Parallel transport for general connections**: integrating $\dot{V} + \Gamma \dot\gamma V = 0$ along a curve is a serial ODE with poor parallelism
34
+ - **Christoffel symbol computation**: involves partial derivatives of the metric $g$ and $g^{-1}$; if $g$ has a closed form, $O(n^3)$; otherwise more expensive
35
+ - **Connections in gauge-equivariant CNNs**: one $G$-element action per edge (matrix-times-feature-vector), expressible as sparse matmul or batched small GEMM
36
+ - Key adaptation: use a single-step retraction/closed-form transport instead of step-by-step ODE integration
37
+
38
+ ## Risks and Failure Conditions
39
+
40
+ - **Step-by-step ODE integration for parallel transport**: Serial recurrence kills parallelism; closed-form transport or single-step approximations must be used
41
+ - **Inconsistent left/right connection conventions**: Mixing left-invariant and right-invariant connections leads to misaligned gradients
42
+ - **Improper handling of gauge freedom**: If the connection parameterization in gauge-equivariant networks is incomplete, equivariance silently breaks
43
+ - **Numerical derivatives of Christoffel symbols**: Finite-difference estimation of $\partial_i g_{jk}$ is noisy; analytical formulas or autodiff are preferable
44
+ - **Connection does not imply metric compatibility**: A connection need not admit a compatible metric (non-metric connections); incorrectly assuming compatibility leads to inconsistencies
45
+
46
+ ## Further References
47
+
48
+ - Distillation notes: references/books/differential-geometry.md (Ch 12 Connections and Covariant Derivatives, Section 12.2 Connection Forms, Section 12.4 Ehresmann, Section 12.12 G-Connections)
49
+ - Distillation notes: references/books/differential-geometry.md (Section 6.8 Principal Bundles, Section 9.8 Electromagnetism)
50
+ - Original text: Jeffrey M. Lee, *Manifolds and Differential Geometry*, Ch 12 (Section 12.1--Section 12.12, complete connection theory)
51
+ - Original text: Jeffrey M. Lee, *Manifolds and Differential Geometry*, Section 13.1 Levi-Civita Connection
@@ -0,0 +1,51 @@
1
+ # 联络 (Connection)
2
+
3
+ ## 最小定义
4
+
5
+ 联络 $\nabla$ 是流形上"如何把一个向量沿曲线搬到另一点而不额外旋转"的规则。形式化地,$\nabla: \mathfrak{X}(M) \times \mathfrak{X}(M) \to \mathfrak{X}(M)$ 满足 Leibniz 法则。Levi-Civita 联络是唯一与度量相容($\nabla g = 0$)且无挠($T = 0$)的联络。
6
+
7
+ ## 核心公式
8
+
9
+ - 协变导数:$\nabla_X Y = \left(X^i \partial_i Y^k + X^i Y^j \Gamma^k_{ij}\right) \partial_k$
10
+ - Christoffel 符号(Levi-Civita):$\Gamma^k_{ij} = \frac{1}{2} g^{kl}(\partial_i g_{jl} + \partial_j g_{il} - \partial_l g_{ij})$
11
+ - 平行移动方程:$\frac{D V^k}{dt} = \dot V^k + \Gamma^k_{ij} \dot\gamma^i V^j = 0$
12
+ - 联络形式(主丛):$\omega \in \Omega^1(P, \mathfrak{g})$,规范场 $A_\mu$ 即局部联络形式
13
+ - 曲率 = 联络的不交换性:$R(X,Y) = [\nabla_X, \nabla_Y] - \nabla_{[X,Y]}$
14
+
15
+ ## 适用问题
16
+
17
+ - 跨点向量比较:不同点的切空间无法直接相加,需要联络规定的"搬运规则"
18
+ - 优化中的动量/状态搬运:Riemannian Adam 中历史梯度需要通过 parallel transport 跨步搬运
19
+ - 规范等变网络:局部坐标系(gauge)的选择自由由联络对齐
20
+ - 物理约束系统:电磁场 = U(1) 联络曲率,Yang-Mills = 非阿贝尔联络曲率
21
+
22
+ ## AI 设计翻译
23
+
24
+ - **Vector transport 模块**:Riemannian 优化器中,将动量 $m_k \in T_{x_k}M$ 搬到 $T_{x_{k+1}}M$;闭式 transport(如 Stiefel 上的投影)可 GEMM 化
25
+ - **Gauge-equivariant CNN**:在流形/网格上,每条边携带一个 $G$-联络元素对齐相邻点的局部 frame,使卷积核对局部坐标选择不变
26
+ - **Parallel transport 正则化**:惩罚特征场在联络下的非平行性 $\|\nabla_X f\|^2$,强制特征沿流形平滑变化
27
+ - **联络学习的参数化**:将 Christoffel 符号参数化为神经网络输出,学习数据流形上的"最优搬运规则"
28
+
29
+ ## 工程可行性
30
+
31
+ GPU 友好度:联络的核心挑战是"串行 ODE 积分"。
32
+ - **闭式 parallel transport**(特定流形如 SO(3)、Stiefel):单步矩阵运算,可 batched,GPU 友好
33
+ - **一般联络的平行移动**:沿曲线积分 $\dot{V} + \Gamma \dot\gamma V = 0$ 是串行 ODE,并行性差
34
+ - **Christoffel 符号计算**:涉及度量 $g$ 的偏导和 $g^{-1}$,若 $g$ 有闭式则 $O(n^3)$,否则更贵
35
+ - **规范等变 CNN 中的联络**:每边一个 $G$-元素作用(矩阵乘特征向量),可表达为 sparse matmul 或 batched small GEMM
36
+ - 关键改造:用一步 retraction/closed-form transport 替代逐步 ODE 积分
37
+
38
+ ## 风险与失效条件
39
+
40
+ - **逐步 ODE 积分做平行移动**:串行递推杀死并行度,必须用闭式 transport 或一步近似
41
+ - **左右联络约定不统一**:左不变 vs 右不变联络的选择不一致导致梯度错位
42
+ - **规范自由度未正确处理**:gauge-equivariant 网络中若联络参数化不完备,等变性会悄悄破缺
43
+ - **Christoffel 符号的数值导数**:用有限差分估计 $\partial_i g_{jk}$ 时噪声大,最好有解析式或 autodiff
44
+ - **联络 ≠ 度量**:有联络不一定有相容度量(非度量联络),错误假设相容性会导致不一致
45
+
46
+ ## 深入参考
47
+
48
+ - 蒸馏稿:references/books/differential-geometry.md(Ch 12 Connections and Covariant Derivatives, §12.2 联络形式, §12.4 Ehresmann, §12.12 G-联络)
49
+ - 蒸馏稿:references/books/differential-geometry.md(§6.8 Principal Bundles, §9.8 Electromagnetism)
50
+ - 原书:Jeffrey M. Lee, *Manifolds and Differential Geometry*, Ch 12(§12.1-§12.12 完整联络理论)
51
+ - 原书:Jeffrey M. Lee, *Manifolds and Differential Geometry*, §13.1 Levi-Civita Connection
@@ -0,0 +1,49 @@
1
+ # Curvature
2
+
3
+ ## Minimal Definition
4
+
5
+ Curvature measures whether a manifold "bends" and whether "paths are path-dependent." The Riemann curvature tensor $R(X,Y)Z = \nabla_X\nabla_Y Z - \nabla_Y\nabla_X Z - \nabla_{[X,Y]}Z$ describes the deflection of a vector after parallel transport around an infinitesimal parallelogram. The sectional curvature $K(\sigma)$ is its simplest scalar extraction.
6
+
7
+ ## Core Formulas
8
+
9
+ - Riemann curvature tensor: $R^l_{ijk} = \partial_i \Gamma^l_{jk} - \partial_j \Gamma^l_{ik} + \Gamma^l_{im}\Gamma^m_{jk} - \Gamma^l_{jm}\Gamma^m_{ik}$
10
+ - Ricci curvature (contraction): $R_{ij} = \sum_k R^k_{ikj}$
11
+ - Scalar curvature: $S = \sum_{ij} g^{ij} R_{ij}$
12
+ - Sectional curvature: $K(X,Y) = \frac{\langle R(X,Y)Y, X\rangle}{\|X\|^2\|Y\|^2 - \langle X,Y\rangle^2}$
13
+ - Jacobi equation: $\frac{D^2 J}{dt^2} + R(J, \dot\gamma)\dot\gamma = 0$ (describes divergence/convergence of geodesics)
14
+ - Hessian-vector product: $Hv = \nabla(\nabla L \cdot v)$, $O(N)$ estimation of curvature information
15
+
16
+ ## Applicable Problems
17
+
18
+ - Loss landscape analysis: curvature determines conditioning and sharpness, distinguishing sharp minima from flat minima
19
+ - Optimization trajectory stability: Jacobi fields describe the divergence/convergence of neighboring optimization trajectories
20
+ - Generalization diagnostics: flat minima (low curvature) tend to generalize better
21
+ - Manifold learning: the curvature of the data manifold guides latent space dimension and metric selection
22
+
23
+ ## AI Design Translation
24
+
25
+ - **Curvature regularization (geometric perspective on SAM)**: Use Hessian-vector products to estimate $\max_v v^T H v$, penalizing sharp minima and preferring flat minima
26
+ - **HVP-based diagnostic**: $\|Hv\|/\|v\|$ as a cheap proxy for loss landscape curvature, used for learning rate adaptation and early stopping
27
+ - **Jacobi field trajectory monitoring**: Track the distance evolution between two neighboring optimization trajectories, the discrete analog of $J''(t) + R(J,\dot\gamma)\dot\gamma = 0$, to detect divergence/convergence
28
+ - **Ricci-flow-inspired graph rewiring**: Use discrete Ricci curvature to guide dynamic adjustment of graph/attention structure (negative-curvature edges indicate bottlenecks that need additional connections)
29
+
30
+ ## Engineering Feasibility
31
+
32
+ GPU friendliness: the core difficulty of curvature is that "the full tensor cannot be materialized."
33
+ - **Riemann tensor**: 4th order, $n^4$ components, materializing it exhausts memory -- **explicit computation is prohibited**
34
+ - **Hessian-vector product (HVP)**: Via Pearlmutter's algorithm, one forward pass + one backward pass yields $Hv$, $O(N)$ time and $O(N)$ memory, GPU-friendly
35
+ - **Monte Carlo estimation of Ricci/scalar curvature**: Randomly sample directions $v$, $\mathbb{E}[v^T H v] = \text{tr}(H)$, using Hutchinson's estimator, GPU-friendly
36
+ - **Jacobi fields**: Require integrating a second-order ODE along a trajectory, serial recurrence, GPU-unfriendly; in practice, discrete finite-difference approximations are used
37
+ - Low precision: second-order derivatives in HVP are noisy under fp16, requiring fp32 accumulation
38
+
39
+ ## Risks and Failure Conditions
40
+
41
+ - **Materializing the full Riemann/Hessian tensor**: $O(N^2)$ to $O(N^4)$ memory, impossible when $N \sim 10^9$
42
+ - **Low signal-to-noise ratio in curvature estimation**: Monte Carlo estimation of HVP has high variance; with small batches, the signal may be drowned in noise
43
+ - **Treating curvature regularization as a panacea**: Curvature estimation itself is expensive (requiring additional forward and backward passes); benefits should be validated at small scale first when uncertain
44
+ - **Discrete approximation errors**: When using finite differences to approximate Jacobi fields/HVP, the step size is sensitive -- too large causes truncation error, too small causes floating-point cancellation
45
+
46
+ ## Further References
47
+
48
+ - Distillation notes: references/books/differential-geometry.md (Ch 12 Section 12.5/Section 12.10 Curvature, Ch 13 Section 13.2 Riemann Curvature, Section 13.7 Jacobi Fields, Section 13.11 Rauch Comparison)
49
+ - Original text: Jeffrey M. Lee, *Manifolds and Differential Geometry*, Section 13.2 Riemann Curvature Tensor, Section 13.7 Jacobi Fields
@@ -0,0 +1,49 @@
1
+ # 曲率 (Curvature)
2
+
3
+ ## 最小定义
4
+
5
+ 曲率度量流形"弯不弯"以及"路径依不依赖"。Riemann 曲率张量 $R(X,Y)Z = \nabla_X\nabla_Y Z - \nabla_Y\nabla_X Z - \nabla_{[X,Y]}Z$ 描述向量绕无穷小平行四边形平行移动一圈后的偏转。截面曲率 $K(\sigma)$ 是其最简标量提取。
6
+
7
+ ## 核心公式
8
+
9
+ - Riemann 曲率张量:$R^l_{ijk} = \partial_i \Gamma^l_{jk} - \partial_j \Gamma^l_{ik} + \Gamma^l_{im}\Gamma^m_{jk} - \Gamma^l_{jm}\Gamma^m_{ik}$
10
+ - Ricci 曲率(缩并):$R_{ij} = \sum_k R^k_{ikj}$
11
+ - 标量曲率:$S = \sum_{ij} g^{ij} R_{ij}$
12
+ - 截面曲率:$K(X,Y) = \frac{\langle R(X,Y)Y, X\rangle}{\|X\|^2\|Y\|^2 - \langle X,Y\rangle^2}$
13
+ - Jacobi 方程:$\frac{D^2 J}{dt^2} + R(J, \dot\gamma)\dot\gamma = 0$(描述测地线的发散/汇聚)
14
+ - Hessian-vector product:$Hv = \nabla(\nabla L \cdot v)$,$O(N)$ 估计曲率信息
15
+
16
+ ## 适用问题
17
+
18
+ - 损失地形分析:曲率决定条件数与尖锐度,尖锐极小 vs 平坦极小的区分
19
+ - 优化轨迹稳定性:Jacobi 场描述相邻优化轨迹的发散/汇聚
20
+ - 泛化性诊断:平坦极小(低曲率)往往泛化更好
21
+ - 流形学习:数据流形的曲率指导隐空间维数和度量选择
22
+
23
+ ## AI 设计翻译
24
+
25
+ - **曲率正则化(SAM 的几何视角)**:用 Hessian-vector product 估计 $\max_v v^T H v$,惩罚尖锐极小,偏好 flat minima
26
+ - **HVP-based 诊断器**:$\|Hv\|/\|v\|$ 作为 loss landscape 曲率的廉价代理,用于学习率自适应和早停
27
+ - **Jacobi 场轨迹监控**:跟踪两条相邻优化轨迹的距离变化,$J''(t) + R(J,\dot\gamma)\dot\gamma = 0$ 的离散版,检测发散/收敛
28
+ - **Ricci-flow 启发式图重连**:用离散 Ricci 曲率指导图/注意力结构的动态调整(负曲率边=瓶颈,需增连)
29
+
30
+ ## 工程可行性
31
+
32
+ GPU 友好度:曲率的核心难点是"不能物化全张量"。
33
+ - **Riemann 张量**:4 阶,$n^4$ 个分量,物化即爆显存,**禁止显式计算**
34
+ - **Hessian-vector product (HVP)**:通过 Pearlmutter 算法,一次前向 + 一次反向即可得到 $Hv$,$O(N)$ 时间 $O(N)$ 显存,GPU 友好
35
+ - **Ricci/标量曲率的 Monte Carlo 估计**:随机采样方向 $v$,$\mathbb{E}[v^T H v] = \text{tr}(H)$,用 Hutchinson 估计,GPU 友好
36
+ - **Jacobi 场**:需要沿轨迹积分二阶 ODE,串行递推,GPU 不友好;工程上用离散差分近似
37
+ - 低精度:HVP 中的二阶导在 fp16 下噪声大,需 fp32 累加
38
+
39
+ ## 风险与失效条件
40
+
41
+ - **物化全 Riemann/Hessian 张量**:$O(N^2)$~$O(N^4)$ 显存,$N \sim 10^9$ 时不可能
42
+ - **曲率估计信噪比低**:HVP 的 Monte Carlo 估计方差大,小 batch 下信号可能被噪声淹没
43
+ - **把曲率正则当万能药**:曲率估计本身昂贵(每次需额外前向+反向),收益不确定时需先小规模验证
44
+ - **离散近似误差**:用有限差分近似 Jacobi 场/HVP 时,步长选择敏感——太大截断误差大,太小浮点抵消
45
+
46
+ ## 深入参考
47
+
48
+ - 蒸馏稿:references/books/differential-geometry.md(Ch 12 §12.5/§12.10 Curvature, Ch 13 §13.2 Riemann Curvature, §13.7 Jacobi Fields, §13.11 Rauch Comparison)
49
+ - 原书:Jeffrey M. Lee, *Manifolds and Differential Geometry*, §13.2 Riemann Curvature Tensor, §13.7 Jacobi Fields
@@ -0,0 +1,51 @@
1
+ # Geodesic
2
+
3
+ ## Minimal Definition
4
+
5
+ A geodesic is a locally length-minimizing (or extremal-length) path $\gamma: [0,1] \to M$ on a manifold, satisfying the auto-parallel condition $\nabla_{\dot\gamma} \dot\gamma = 0$ -- that is, the tangent vector is parallel-transported unchanged along itself. The exponential map $\exp_p(v)$ maps a tangent vector $v \in T_pM$ to the endpoint at $t=1$ of the geodesic starting at $p$ with initial velocity $v$.
6
+
7
+ ## Core Formulas
8
+
9
+ - Geodesic equation: $\ddot\gamma^k + \sum_{ij} \Gamma^k_{ij} \dot\gamma^i \dot\gamma^j = 0$
10
+ - Exponential map: $\exp_p(v) = \gamma_v(1)$, where $\gamma_v$ is the geodesic with $\gamma(0)=p, \dot\gamma(0)=v$
11
+ - Logarithmic map: $\log_p(q) = v \in T_pM$ such that $\exp_p(v) = q$ (the inverse of the exponential map)
12
+ - Retraction (engineering approximation): $R_p(v) \approx \exp_p(v)$, requiring only a first-order approximation for optimization
13
+ - Closed-form on the sphere: $\exp_p(v) = \cos(\|v\|) p + \sin(\|v\|) \frac{v}{\|v\|}$
14
+
15
+ ## Applicable Problems
16
+
17
+ - Constrained optimization: performing SGD on SPD/Stiefel/Grassmann/hyperbolic manifolds, where update steps must move along geodesics
18
+ - Latent space interpolation: geodesics between two points in the latent space respect the data manifold structure better than Euclidean straight lines
19
+ - Distance computation on manifolds: $d(p,q) = \|\log_p(q)\|_g$
20
+ - Data augmentation: sampling along geodesics to generate new training samples
21
+
22
+ ## AI Design Translation
23
+
24
+ - **Retraction-based optimizer**: Each step performs $x_{k+1} = R_{x_k}(-\eta \cdot \text{grad})$, using closed-form retractions instead of ODE integration; rotations for the sphere, QR/Cayley for Stiefel, Rodrigues for SO(3)
25
+ - **Geodesic interpolation layer**: Use closed-form geodesics in spherical/hyperbolic latent spaces for mixup and interpolation, $\gamma(t) = \exp_p(t \cdot \log_p(q))$
26
+ - **Momentum on manifolds**: Transport the momentum vector from $T_{x_k}M$ to $T_{x_{k+1}}M$ via vector transport (the discrete analog of parallel transport), then combine with the new gradient
27
+ - **Exponential map output head**: The network makes unconstrained predictions in the tangent space $\mathbb{R}^n$, then projects back to the valid manifold via $\exp_p$, naturally satisfying constraints
28
+
29
+ ## Engineering Feasibility
30
+
31
+ GPU friendliness depends on whether a closed-form retraction exists:
32
+ - **Manifolds with closed forms** (sphere, hyperbolic, SO(3), Stiefel-QR): $\exp_p(v)$ is a finite algebraic expression, $O(1)$/sample, tensorizable in batches, GPU-friendly
33
+ - **Manifolds without closed forms**: require numerical integration of the geodesic equation (second-order ODE), serial recurrence, GPU-unfriendly
34
+ - Closed-form retractions as substitutes for exact exp: QR decomposition, Cayley transform, and other first-order approximations trade a small amount of precision for significant speedup
35
+ - Small-matrix exp for 3x3/4x4 (SO(3)/SE(3)) can be fused into a single kernel, but cannot fully saturate Tensor Cores
36
+ - **Low-precision critical issue**: $\sin\theta/\theta$ and $(1-\cos\theta)/\theta^2$ produce division by zero as $\theta \to 0$; $\log$ is singular as $\theta \to \pi$; fp16 yields NaN directly
37
+
38
+ ## Risks and Failure Conditions
39
+
40
+ - **Step-by-step ODE integration for geodesics**: Serial recurrence kills GPU parallelism; closed-form retractions must be used instead
41
+ - **Small-angle / large-angle singularities**: Numerical instability at $\theta \to 0$ and $\theta \to \pi$ is catastrophically amplified at low precision; Taylor expansion fallbacks are essential
42
+ - **Retraction error accumulation**: First-order approximations may accumulate errors over multiple iterations, requiring occasional exact projection corrections
43
+ - **Cut locus problem**: Beyond the cut locus, the exponential map no longer yields the shortest path; $\log_p(q)$ may not exist or may not be unique
44
+ - **Forced manifold structure for geometric aesthetics**: Applying geodesics to tasks where Euclidean approximations suffice adds complexity and singularity risk
45
+
46
+ ## Further References
47
+
48
+ - Distillation notes: references/books/differential-geometry.md (Ch 13 Section 13.4 Geodesics, Section 13.11 Rauch Comparison)
49
+ - Distillation notes: references/books/smooth-manifolds.md (Ch 20 The Exponential Map)
50
+ - Original text: Jeffrey M. Lee, *Manifolds and Differential Geometry*, Section 13.4 Geodesics
51
+ - Original text: John M. Lee, *Introduction to Smooth Manifolds*, Ch 20 (exponential map, retraction prototype)