agentdb 3.0.0-alpha.10 → 3.0.0-alpha.12

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 (648) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2821 -225
  3. package/dist/src/backends/VectorBackend.d.ts +31 -6
  4. package/dist/src/backends/VectorBackend.d.ts.map +1 -1
  5. package/dist/src/backends/detector.js.map +1 -1
  6. package/dist/src/backends/factory.d.ts +16 -33
  7. package/dist/src/backends/factory.d.ts.map +1 -1
  8. package/dist/src/backends/factory.js +138 -108
  9. package/dist/src/backends/factory.js.map +1 -1
  10. package/dist/src/backends/hnswlib/HNSWLibBackend.d.ts.map +1 -1
  11. package/dist/src/backends/hnswlib/HNSWLibBackend.js +29 -2
  12. package/dist/src/backends/hnswlib/HNSWLibBackend.js.map +1 -1
  13. package/dist/src/backends/ruvector/RuVectorBackend.d.ts +198 -45
  14. package/dist/src/backends/ruvector/RuVectorBackend.d.ts.map +1 -1
  15. package/dist/src/backends/ruvector/RuVectorBackend.js +612 -277
  16. package/dist/src/backends/ruvector/RuVectorBackend.js.map +1 -1
  17. package/dist/src/backends/ruvector/RuVectorLearning.d.ts +0 -4
  18. package/dist/src/backends/ruvector/RuVectorLearning.d.ts.map +1 -1
  19. package/dist/src/backends/ruvector/RuVectorLearning.js +0 -6
  20. package/dist/src/backends/ruvector/RuVectorLearning.js.map +1 -1
  21. package/dist/src/backends/rvf/AdaptiveIndexTuner.d.ts +175 -0
  22. package/dist/src/backends/rvf/AdaptiveIndexTuner.d.ts.map +1 -0
  23. package/dist/src/backends/rvf/AdaptiveIndexTuner.js +559 -0
  24. package/dist/src/backends/rvf/AdaptiveIndexTuner.js.map +1 -0
  25. package/dist/src/backends/rvf/ContrastiveTrainer.d.ts +148 -0
  26. package/dist/src/backends/rvf/ContrastiveTrainer.d.ts.map +1 -0
  27. package/dist/src/backends/rvf/ContrastiveTrainer.js +438 -0
  28. package/dist/src/backends/rvf/ContrastiveTrainer.js.map +1 -0
  29. package/dist/src/backends/rvf/FederatedSessionManager.d.ts +211 -0
  30. package/dist/src/backends/rvf/FederatedSessionManager.d.ts.map +1 -0
  31. package/dist/src/backends/rvf/FederatedSessionManager.js +389 -0
  32. package/dist/src/backends/rvf/FederatedSessionManager.js.map +1 -0
  33. package/dist/src/backends/rvf/FilterBuilder.d.ts +136 -0
  34. package/dist/src/backends/rvf/FilterBuilder.d.ts.map +1 -0
  35. package/dist/src/backends/rvf/FilterBuilder.js +157 -0
  36. package/dist/src/backends/rvf/FilterBuilder.js.map +1 -0
  37. package/dist/src/backends/rvf/NativeAccelerator.d.ts +140 -0
  38. package/dist/src/backends/rvf/NativeAccelerator.d.ts.map +1 -0
  39. package/dist/src/backends/rvf/NativeAccelerator.js +630 -0
  40. package/dist/src/backends/rvf/NativeAccelerator.js.map +1 -0
  41. package/dist/src/backends/rvf/RvfBackend.d.ts +194 -0
  42. package/dist/src/backends/rvf/RvfBackend.d.ts.map +1 -0
  43. package/dist/src/backends/rvf/RvfBackend.js +607 -0
  44. package/dist/src/backends/rvf/RvfBackend.js.map +1 -0
  45. package/dist/src/backends/rvf/RvfSolver.d.ts +175 -0
  46. package/dist/src/backends/rvf/RvfSolver.d.ts.map +1 -0
  47. package/dist/src/backends/rvf/RvfSolver.js +176 -0
  48. package/dist/src/backends/rvf/RvfSolver.js.map +1 -0
  49. package/dist/src/backends/rvf/SelfLearningRvfBackend.d.ts +143 -0
  50. package/dist/src/backends/rvf/SelfLearningRvfBackend.d.ts.map +1 -0
  51. package/dist/src/backends/rvf/SelfLearningRvfBackend.js +569 -0
  52. package/dist/src/backends/rvf/SelfLearningRvfBackend.js.map +1 -0
  53. package/dist/src/backends/rvf/SemanticQueryRouter.d.ts +128 -0
  54. package/dist/src/backends/rvf/SemanticQueryRouter.d.ts.map +1 -0
  55. package/dist/src/backends/rvf/SemanticQueryRouter.js +415 -0
  56. package/dist/src/backends/rvf/SemanticQueryRouter.js.map +1 -0
  57. package/dist/src/backends/rvf/SimdFallbacks.d.ts +25 -0
  58. package/dist/src/backends/rvf/SimdFallbacks.d.ts.map +1 -0
  59. package/dist/src/backends/rvf/SimdFallbacks.js +287 -0
  60. package/dist/src/backends/rvf/SimdFallbacks.js.map +1 -0
  61. package/dist/src/backends/rvf/SolverBandit.d.ts +100 -0
  62. package/dist/src/backends/rvf/SolverBandit.d.ts.map +1 -0
  63. package/dist/src/backends/rvf/SolverBandit.js +216 -0
  64. package/dist/src/backends/rvf/SolverBandit.js.map +1 -0
  65. package/dist/src/backends/rvf/SonaLearningBackend.d.ts +157 -0
  66. package/dist/src/backends/rvf/SonaLearningBackend.d.ts.map +1 -0
  67. package/dist/src/backends/rvf/SonaLearningBackend.js +283 -0
  68. package/dist/src/backends/rvf/SonaLearningBackend.js.map +1 -0
  69. package/dist/src/backends/rvf/SqlJsRvfBackend.d.ts +77 -0
  70. package/dist/src/backends/rvf/SqlJsRvfBackend.d.ts.map +1 -0
  71. package/dist/src/backends/rvf/SqlJsRvfBackend.js +382 -0
  72. package/dist/src/backends/rvf/SqlJsRvfBackend.js.map +1 -0
  73. package/dist/src/backends/rvf/WasmStoreBridge.d.ts +27 -0
  74. package/dist/src/backends/rvf/WasmStoreBridge.d.ts.map +1 -0
  75. package/dist/src/backends/rvf/WasmStoreBridge.js +87 -0
  76. package/dist/src/backends/rvf/WasmStoreBridge.js.map +1 -0
  77. package/dist/src/backends/rvf/validation.d.ts +20 -0
  78. package/dist/src/backends/rvf/validation.d.ts.map +1 -0
  79. package/dist/src/backends/rvf/validation.js +78 -0
  80. package/dist/src/backends/rvf/validation.js.map +1 -0
  81. package/dist/src/benchmark/BenchmarkSuite.d.ts +318 -0
  82. package/dist/src/benchmark/BenchmarkSuite.d.ts.map +1 -0
  83. package/dist/src/benchmark/BenchmarkSuite.js +984 -0
  84. package/dist/src/benchmark/BenchmarkSuite.js.map +1 -0
  85. package/dist/src/benchmark/index.d.ts +29 -0
  86. package/dist/src/benchmark/index.d.ts.map +1 -0
  87. package/dist/src/benchmark/index.js +39 -0
  88. package/dist/src/benchmark/index.js.map +1 -0
  89. package/dist/src/browser/AttentionBrowser.d.ts.map +1 -1
  90. package/dist/src/browser/AttentionBrowser.js +1 -0
  91. package/dist/src/browser/AttentionBrowser.js.map +1 -1
  92. package/dist/src/cli/agentdb-cli.d.ts +13 -0
  93. package/dist/src/cli/agentdb-cli.d.ts.map +1 -1
  94. package/dist/src/cli/agentdb-cli.js +803 -94
  95. package/dist/src/cli/agentdb-cli.js.map +1 -1
  96. package/dist/src/cli/commands/hyperbolic.d.ts +11 -0
  97. package/dist/src/cli/commands/hyperbolic.d.ts.map +1 -0
  98. package/dist/src/cli/commands/hyperbolic.js +428 -0
  99. package/dist/src/cli/commands/hyperbolic.js.map +1 -0
  100. package/dist/src/cli/commands/learn.d.ts +11 -0
  101. package/dist/src/cli/commands/learn.d.ts.map +1 -0
  102. package/dist/src/cli/commands/learn.js +404 -0
  103. package/dist/src/cli/commands/learn.js.map +1 -0
  104. package/dist/src/cli/commands/migrate.d.ts +15 -0
  105. package/dist/src/cli/commands/migrate.d.ts.map +1 -1
  106. package/dist/src/cli/commands/migrate.js +176 -8
  107. package/dist/src/cli/commands/migrate.js.map +1 -1
  108. package/dist/src/cli/commands/route.d.ts +11 -0
  109. package/dist/src/cli/commands/route.d.ts.map +1 -0
  110. package/dist/src/cli/commands/route.js +414 -0
  111. package/dist/src/cli/commands/route.js.map +1 -0
  112. package/dist/src/cli/commands/rvf.d.ts +21 -0
  113. package/dist/src/cli/commands/rvf.d.ts.map +1 -0
  114. package/dist/src/cli/commands/rvf.js +448 -0
  115. package/dist/src/cli/commands/rvf.js.map +1 -0
  116. package/dist/src/cli/lib/report-store.d.ts +2 -1
  117. package/dist/src/cli/lib/report-store.d.ts.map +1 -1
  118. package/dist/src/cli/lib/report-store.js +85 -65
  119. package/dist/src/cli/lib/report-store.js.map +1 -1
  120. package/dist/src/cli/lib/simulation-runner.d.ts +40 -3
  121. package/dist/src/cli/lib/simulation-runner.d.ts.map +1 -1
  122. package/dist/src/cli/lib/simulation-runner.js +248 -6
  123. package/dist/src/cli/lib/simulation-runner.js.map +1 -1
  124. package/dist/src/compatibility/DeprecationWarnings.d.ts +54 -0
  125. package/dist/src/compatibility/DeprecationWarnings.d.ts.map +1 -0
  126. package/dist/src/compatibility/DeprecationWarnings.js +131 -0
  127. package/dist/src/compatibility/DeprecationWarnings.js.map +1 -0
  128. package/dist/src/compatibility/MigrationUtilities.d.ts +30 -0
  129. package/dist/src/compatibility/MigrationUtilities.d.ts.map +1 -0
  130. package/dist/src/compatibility/MigrationUtilities.js +184 -0
  131. package/dist/src/compatibility/MigrationUtilities.js.map +1 -0
  132. package/dist/src/compatibility/V1toV2Adapter.d.ts +76 -0
  133. package/dist/src/compatibility/V1toV2Adapter.d.ts.map +1 -0
  134. package/dist/src/compatibility/V1toV2Adapter.js +185 -0
  135. package/dist/src/compatibility/V1toV2Adapter.js.map +1 -0
  136. package/dist/src/compatibility/VersionDetector.d.ts +32 -0
  137. package/dist/src/compatibility/VersionDetector.d.ts.map +1 -0
  138. package/dist/src/compatibility/VersionDetector.js +144 -0
  139. package/dist/src/compatibility/VersionDetector.js.map +1 -0
  140. package/dist/src/compatibility/index.d.ts +14 -0
  141. package/dist/src/compatibility/index.d.ts.map +1 -0
  142. package/dist/src/compatibility/index.js +21 -0
  143. package/dist/src/compatibility/index.js.map +1 -0
  144. package/dist/src/compatibility/types.d.ts +103 -0
  145. package/dist/src/compatibility/types.d.ts.map +1 -0
  146. package/dist/src/compatibility/types.js +5 -0
  147. package/dist/src/compatibility/types.js.map +1 -0
  148. package/dist/src/controllers/AttentionService.d.ts +99 -202
  149. package/dist/src/controllers/AttentionService.d.ts.map +1 -1
  150. package/dist/src/controllers/AttentionService.js +450 -558
  151. package/dist/src/controllers/AttentionService.js.map +1 -1
  152. package/dist/src/controllers/CausalMemoryGraph.d.ts +30 -5
  153. package/dist/src/controllers/CausalMemoryGraph.d.ts.map +1 -1
  154. package/dist/src/controllers/CausalMemoryGraph.js +89 -13
  155. package/dist/src/controllers/CausalMemoryGraph.js.map +1 -1
  156. package/dist/src/controllers/CausalRecall.d.ts +4 -0
  157. package/dist/src/controllers/CausalRecall.d.ts.map +1 -1
  158. package/dist/src/controllers/CausalRecall.js +19 -2
  159. package/dist/src/controllers/CausalRecall.js.map +1 -1
  160. package/dist/src/controllers/EmbeddingService.d.ts.map +1 -1
  161. package/dist/src/controllers/EmbeddingService.js +28 -11
  162. package/dist/src/controllers/EmbeddingService.js.map +1 -1
  163. package/dist/src/controllers/EnhancedEmbeddingService.d.ts +4 -0
  164. package/dist/src/controllers/EnhancedEmbeddingService.d.ts.map +1 -1
  165. package/dist/src/controllers/EnhancedEmbeddingService.js +17 -3
  166. package/dist/src/controllers/EnhancedEmbeddingService.js.map +1 -1
  167. package/dist/src/controllers/ExplainableRecall.d.ts +1 -1
  168. package/dist/src/controllers/ExplainableRecall.d.ts.map +1 -1
  169. package/dist/src/controllers/ExplainableRecall.js +1 -1
  170. package/dist/src/controllers/ExplainableRecall.js.map +1 -1
  171. package/dist/src/controllers/HNSWIndex.d.ts +20 -2
  172. package/dist/src/controllers/HNSWIndex.d.ts.map +1 -1
  173. package/dist/src/controllers/HNSWIndex.js +82 -6
  174. package/dist/src/controllers/HNSWIndex.js.map +1 -1
  175. package/dist/src/controllers/LearningSystem.d.ts +1 -17
  176. package/dist/src/controllers/LearningSystem.d.ts.map +1 -1
  177. package/dist/src/controllers/LearningSystem.js +18 -135
  178. package/dist/src/controllers/LearningSystem.js.map +1 -1
  179. package/dist/src/controllers/MemoryController.d.ts +161 -0
  180. package/dist/src/controllers/MemoryController.d.ts.map +1 -0
  181. package/dist/src/controllers/MemoryController.js +303 -0
  182. package/dist/src/controllers/MemoryController.js.map +1 -0
  183. package/dist/src/controllers/MincutService.d.ts +92 -0
  184. package/dist/src/controllers/MincutService.d.ts.map +1 -0
  185. package/dist/src/controllers/MincutService.js +336 -0
  186. package/dist/src/controllers/MincutService.js.map +1 -0
  187. package/dist/src/controllers/NightlyLearner.d.ts +5 -1
  188. package/dist/src/controllers/NightlyLearner.d.ts.map +1 -1
  189. package/dist/src/controllers/NightlyLearner.js +17 -3
  190. package/dist/src/controllers/NightlyLearner.js.map +1 -1
  191. package/dist/src/controllers/QUICClient.d.ts +45 -0
  192. package/dist/src/controllers/QUICClient.d.ts.map +1 -1
  193. package/dist/src/controllers/QUICClient.js +190 -0
  194. package/dist/src/controllers/QUICClient.js.map +1 -1
  195. package/dist/src/controllers/ReasoningBank.d.ts +6 -3
  196. package/dist/src/controllers/ReasoningBank.d.ts.map +1 -1
  197. package/dist/src/controllers/ReasoningBank.js +47 -41
  198. package/dist/src/controllers/ReasoningBank.js.map +1 -1
  199. package/dist/src/controllers/ReflexionMemory.d.ts +115 -6
  200. package/dist/src/controllers/ReflexionMemory.d.ts.map +1 -1
  201. package/dist/src/controllers/ReflexionMemory.js +541 -213
  202. package/dist/src/controllers/ReflexionMemory.js.map +1 -1
  203. package/dist/src/controllers/SkillLibrary.d.ts +27 -3
  204. package/dist/src/controllers/SkillLibrary.d.ts.map +1 -1
  205. package/dist/src/controllers/SkillLibrary.js +187 -91
  206. package/dist/src/controllers/SkillLibrary.js.map +1 -1
  207. package/dist/src/controllers/SparsificationService.d.ts +151 -0
  208. package/dist/src/controllers/SparsificationService.d.ts.map +1 -0
  209. package/dist/src/controllers/SparsificationService.js +375 -0
  210. package/dist/src/controllers/SparsificationService.js.map +1 -0
  211. package/dist/src/controllers/SyncCoordinator.d.ts.map +1 -1
  212. package/dist/src/controllers/SyncCoordinator.js +115 -3
  213. package/dist/src/controllers/SyncCoordinator.js.map +1 -1
  214. package/dist/src/controllers/WASMVectorSearch.d.ts +12 -30
  215. package/dist/src/controllers/WASMVectorSearch.d.ts.map +1 -1
  216. package/dist/src/controllers/WASMVectorSearch.js +146 -93
  217. package/dist/src/controllers/WASMVectorSearch.js.map +1 -1
  218. package/dist/src/controllers/attention/AttentionCache.d.ts +38 -0
  219. package/dist/src/controllers/attention/AttentionCache.d.ts.map +1 -0
  220. package/dist/src/controllers/attention/AttentionCache.js +80 -0
  221. package/dist/src/controllers/attention/AttentionCache.js.map +1 -0
  222. package/dist/src/controllers/attention/AttentionConfig.d.ts +127 -0
  223. package/dist/src/controllers/attention/AttentionConfig.d.ts.map +1 -0
  224. package/dist/src/controllers/attention/AttentionConfig.js +124 -0
  225. package/dist/src/controllers/attention/AttentionConfig.js.map +1 -0
  226. package/dist/src/controllers/attention/AttentionCore.d.ts +89 -0
  227. package/dist/src/controllers/attention/AttentionCore.d.ts.map +1 -0
  228. package/dist/src/controllers/attention/AttentionCore.js +289 -0
  229. package/dist/src/controllers/attention/AttentionCore.js.map +1 -0
  230. package/dist/src/controllers/attention/AttentionHelpers.d.ts +55 -0
  231. package/dist/src/controllers/attention/AttentionHelpers.d.ts.map +1 -0
  232. package/dist/src/controllers/attention/AttentionHelpers.js +148 -0
  233. package/dist/src/controllers/attention/AttentionHelpers.js.map +1 -0
  234. package/dist/src/controllers/attention/AttentionMetrics.d.ts +55 -0
  235. package/dist/src/controllers/attention/AttentionMetrics.d.ts.map +1 -0
  236. package/dist/src/controllers/attention/AttentionMetrics.js +72 -0
  237. package/dist/src/controllers/attention/AttentionMetrics.js.map +1 -0
  238. package/dist/src/controllers/attention/AttentionWASM.d.ts +98 -0
  239. package/dist/src/controllers/attention/AttentionWASM.d.ts.map +1 -0
  240. package/dist/src/controllers/attention/AttentionWASM.js +147 -0
  241. package/dist/src/controllers/attention/AttentionWASM.js.map +1 -0
  242. package/dist/src/controllers/attention/CrossAttentionController.d.ts +141 -0
  243. package/dist/src/controllers/attention/CrossAttentionController.d.ts.map +1 -0
  244. package/dist/src/controllers/attention/CrossAttentionController.js +325 -0
  245. package/dist/src/controllers/attention/CrossAttentionController.js.map +1 -0
  246. package/dist/src/controllers/attention/MultiHeadAttentionController.d.ts +137 -0
  247. package/dist/src/controllers/attention/MultiHeadAttentionController.d.ts.map +1 -0
  248. package/dist/src/controllers/attention/MultiHeadAttentionController.js +336 -0
  249. package/dist/src/controllers/attention/MultiHeadAttentionController.js.map +1 -0
  250. package/dist/src/controllers/attention/SelfAttentionController.d.ts +117 -0
  251. package/dist/src/controllers/attention/SelfAttentionController.d.ts.map +1 -0
  252. package/dist/src/controllers/attention/SelfAttentionController.js +205 -0
  253. package/dist/src/controllers/attention/SelfAttentionController.js.map +1 -0
  254. package/dist/src/controllers/attention/index.d.ts +24 -0
  255. package/dist/src/controllers/attention/index.d.ts.map +1 -0
  256. package/dist/src/controllers/attention/index.js +20 -0
  257. package/dist/src/controllers/attention/index.js.map +1 -0
  258. package/dist/src/controllers/index.d.ts +14 -27
  259. package/dist/src/controllers/index.d.ts.map +1 -1
  260. package/dist/src/controllers/index.js +10 -18
  261. package/dist/src/controllers/index.js.map +1 -1
  262. package/dist/src/controllers/prerequisites.d.ts +76 -0
  263. package/dist/src/controllers/prerequisites.d.ts.map +1 -0
  264. package/dist/src/controllers/prerequisites.js +235 -0
  265. package/dist/src/controllers/prerequisites.js.map +1 -0
  266. package/dist/src/core/AgentDB.d.ts +27 -19
  267. package/dist/src/core/AgentDB.d.ts.map +1 -1
  268. package/dist/src/core/AgentDB.js +76 -124
  269. package/dist/src/core/AgentDB.js.map +1 -1
  270. package/dist/src/core/QueryCache.d.ts +138 -0
  271. package/dist/src/core/QueryCache.d.ts.map +1 -0
  272. package/dist/src/core/QueryCache.js +309 -0
  273. package/dist/src/core/QueryCache.js.map +1 -0
  274. package/dist/src/db-fallback.d.ts +10 -1
  275. package/dist/src/db-fallback.d.ts.map +1 -1
  276. package/dist/src/db-fallback.js +205 -28
  277. package/dist/src/db-fallback.js.map +1 -1
  278. package/dist/src/examples/quic-sync-example.js +2 -2
  279. package/dist/src/examples/quic-sync-example.js.map +1 -1
  280. package/dist/src/index.d.ts +15 -15
  281. package/dist/src/index.d.ts.map +1 -1
  282. package/dist/src/index.js +39 -11
  283. package/dist/src/index.js.map +1 -1
  284. package/dist/src/mcp/agentdb-mcp-server.js +47 -2
  285. package/dist/src/mcp/agentdb-mcp-server.js.map +1 -1
  286. package/dist/src/model/ModelCacheLoader.d.ts +24 -0
  287. package/dist/src/model/ModelCacheLoader.d.ts.map +1 -0
  288. package/dist/src/model/ModelCacheLoader.js +121 -0
  289. package/dist/src/model/ModelCacheLoader.js.map +1 -0
  290. package/dist/src/observability/index.d.ts +8 -0
  291. package/dist/src/observability/index.d.ts.map +1 -0
  292. package/dist/src/observability/index.js +8 -0
  293. package/dist/src/observability/index.js.map +1 -0
  294. package/dist/src/observability/integration.d.ts +32 -0
  295. package/dist/src/observability/integration.d.ts.map +1 -0
  296. package/dist/src/observability/integration.js +159 -0
  297. package/dist/src/observability/integration.js.map +1 -0
  298. package/dist/src/observability/telemetry.d.ts +137 -0
  299. package/dist/src/observability/telemetry.d.ts.map +1 -0
  300. package/dist/src/observability/telemetry.js +416 -0
  301. package/dist/src/observability/telemetry.js.map +1 -0
  302. package/dist/src/optimizations/BatchOperations.d.ts +47 -0
  303. package/dist/src/optimizations/BatchOperations.d.ts.map +1 -1
  304. package/dist/src/optimizations/BatchOperations.js +183 -31
  305. package/dist/src/optimizations/BatchOperations.js.map +1 -1
  306. package/dist/src/optimizations/Quantization.d.ts +330 -0
  307. package/dist/src/optimizations/Quantization.d.ts.map +1 -0
  308. package/dist/src/optimizations/Quantization.js +793 -0
  309. package/dist/src/optimizations/Quantization.js.map +1 -0
  310. package/dist/src/optimizations/index.d.ts +1 -3
  311. package/dist/src/optimizations/index.d.ts.map +1 -1
  312. package/dist/src/optimizations/index.js +0 -1
  313. package/dist/src/optimizations/index.js.map +1 -1
  314. package/dist/src/quantization/index.d.ts +7 -0
  315. package/dist/src/quantization/index.d.ts.map +1 -0
  316. package/dist/src/quantization/index.js +15 -0
  317. package/dist/src/quantization/index.js.map +1 -0
  318. package/dist/src/quantization/vector-quantization.d.ts +388 -0
  319. package/dist/src/quantization/vector-quantization.d.ts.map +1 -0
  320. package/dist/src/quantization/vector-quantization.js +1132 -0
  321. package/dist/src/quantization/vector-quantization.js.map +1 -0
  322. package/dist/src/search/HybridSearch.d.ts +311 -0
  323. package/dist/src/search/HybridSearch.d.ts.map +1 -0
  324. package/dist/src/search/HybridSearch.js +712 -0
  325. package/dist/src/search/HybridSearch.js.map +1 -0
  326. package/dist/src/search/index.d.ts +12 -0
  327. package/dist/src/search/index.d.ts.map +1 -0
  328. package/dist/src/search/index.js +16 -0
  329. package/dist/src/search/index.js.map +1 -0
  330. package/dist/src/services/AttentionService.d.ts +303 -0
  331. package/dist/src/services/AttentionService.d.ts.map +1 -0
  332. package/dist/src/services/AttentionService.js +1165 -0
  333. package/dist/src/services/AttentionService.js.map +1 -0
  334. package/dist/src/services/LLMRouter.d.ts +61 -7
  335. package/dist/src/services/LLMRouter.d.ts.map +1 -1
  336. package/dist/src/services/LLMRouter.js +286 -22
  337. package/dist/src/services/LLMRouter.js.map +1 -1
  338. package/dist/src/services/audit-logger.service.d.ts +168 -0
  339. package/dist/src/services/audit-logger.service.d.ts.map +1 -0
  340. package/dist/src/services/audit-logger.service.js +331 -0
  341. package/dist/src/services/audit-logger.service.js.map +1 -0
  342. package/dist/src/services/enhanced-embeddings.d.ts +271 -0
  343. package/dist/src/services/enhanced-embeddings.d.ts.map +1 -0
  344. package/dist/src/services/enhanced-embeddings.js +1035 -0
  345. package/dist/src/services/enhanced-embeddings.js.map +1 -0
  346. package/dist/src/services/federated-learning.d.ts +172 -0
  347. package/dist/src/services/federated-learning.d.ts.map +1 -0
  348. package/dist/src/services/federated-learning.js +342 -0
  349. package/dist/src/services/federated-learning.js.map +1 -0
  350. package/dist/src/simd/index.d.ts +9 -0
  351. package/dist/src/simd/index.d.ts.map +1 -0
  352. package/dist/src/simd/index.js +27 -0
  353. package/dist/src/simd/index.js.map +1 -0
  354. package/dist/src/simd/simd-vector-ops.d.ts +332 -0
  355. package/dist/src/simd/simd-vector-ops.d.ts.map +1 -0
  356. package/dist/src/simd/simd-vector-ops.js +945 -0
  357. package/dist/src/simd/simd-vector-ops.js.map +1 -0
  358. package/dist/src/types/database.types.d.ts +275 -0
  359. package/dist/src/types/database.types.d.ts.map +1 -0
  360. package/dist/src/types/database.types.js +55 -0
  361. package/dist/src/types/database.types.js.map +1 -0
  362. package/dist/src/types/graph.d.ts +29 -0
  363. package/dist/src/types/graph.d.ts.map +1 -0
  364. package/dist/src/types/graph.js +8 -0
  365. package/dist/src/types/graph.js.map +1 -0
  366. package/dist/src/utils/chalk-fallback.d.ts +3 -0
  367. package/dist/src/utils/chalk-fallback.d.ts.map +1 -0
  368. package/dist/src/utils/chalk-fallback.js +20 -0
  369. package/dist/src/utils/chalk-fallback.js.map +1 -0
  370. package/dist/src/utils/similarity.d.ts +11 -0
  371. package/dist/src/utils/similarity.d.ts.map +1 -0
  372. package/dist/src/utils/similarity.js +25 -0
  373. package/dist/src/utils/similarity.js.map +1 -0
  374. package/dist/src/wasm-loader.d.ts +66 -0
  375. package/dist/src/wasm-loader.d.ts.map +1 -0
  376. package/dist/src/wasm-loader.js +76 -0
  377. package/dist/src/wasm-loader.js.map +1 -0
  378. package/dist/src/wrappers/agentdb-fast.d.ts +139 -0
  379. package/dist/src/wrappers/agentdb-fast.d.ts.map +1 -0
  380. package/dist/src/wrappers/agentdb-fast.js +307 -0
  381. package/dist/src/wrappers/agentdb-fast.js.map +1 -0
  382. package/dist/src/wrappers/attention-fallbacks.d.ts +343 -0
  383. package/dist/src/wrappers/attention-fallbacks.d.ts.map +1 -0
  384. package/dist/src/wrappers/attention-fallbacks.js +1484 -0
  385. package/dist/src/wrappers/attention-fallbacks.js.map +1 -0
  386. package/dist/src/wrappers/attention-native.d.ts +111 -0
  387. package/dist/src/wrappers/attention-native.d.ts.map +1 -0
  388. package/dist/src/wrappers/attention-native.js +273 -0
  389. package/dist/src/wrappers/attention-native.js.map +1 -0
  390. package/dist/src/wrappers/embedding-service.d.ts +117 -0
  391. package/dist/src/wrappers/embedding-service.d.ts.map +1 -0
  392. package/dist/src/wrappers/embedding-service.js +370 -0
  393. package/dist/src/wrappers/embedding-service.js.map +1 -0
  394. package/dist/src/wrappers/gnn-wrapper.d.ts +71 -0
  395. package/dist/src/wrappers/gnn-wrapper.d.ts.map +1 -0
  396. package/dist/src/wrappers/gnn-wrapper.js +236 -0
  397. package/dist/src/wrappers/gnn-wrapper.js.map +1 -0
  398. package/dist/src/wrappers/index.d.ts +48 -0
  399. package/dist/src/wrappers/index.d.ts.map +1 -0
  400. package/dist/src/wrappers/index.js +83 -0
  401. package/dist/src/wrappers/index.js.map +1 -0
  402. package/package.json +77 -76
  403. package/scripts/postinstall.cjs +13 -144
  404. package/dist/agentdb.browser.js +0 -1685
  405. package/dist/agentdb.browser.js.map +0 -7
  406. package/dist/agentdb.browser.min.js +0 -3
  407. package/dist/agentdb.browser.min.js.map +0 -7
  408. package/dist/agentdb.wasm-loader.js +0 -207
  409. package/dist/backends/VectorBackend.d.ts +0 -120
  410. package/dist/backends/VectorBackend.js +0 -14
  411. package/dist/backends/VectorBackend.js.map +0 -1
  412. package/dist/optimizations/RVFOptimizer.d.ts +0 -225
  413. package/dist/optimizations/RVFOptimizer.js +0 -541
  414. package/dist/optimizations/RVFOptimizer.js.map +0 -1
  415. package/dist/schemas/frontier-schema.sql +0 -378
  416. package/dist/schemas/schema.sql +0 -382
  417. package/dist/simulation/cli.d.ts +0 -9
  418. package/dist/simulation/cli.d.ts.map +0 -1
  419. package/dist/simulation/cli.js +0 -68
  420. package/dist/simulation/cli.js.map +0 -1
  421. package/dist/simulation/runner.d.ts +0 -21
  422. package/dist/simulation/runner.d.ts.map +0 -1
  423. package/dist/simulation/runner.js +0 -241
  424. package/dist/simulation/runner.js.map +0 -1
  425. package/dist/simulation/scenarios/aidefence-integration.d.ts +0 -24
  426. package/dist/simulation/scenarios/aidefence-integration.d.ts.map +0 -1
  427. package/dist/simulation/scenarios/aidefence-integration.js +0 -124
  428. package/dist/simulation/scenarios/aidefence-integration.js.map +0 -1
  429. package/dist/simulation/scenarios/bmssp-integration.d.ts +0 -23
  430. package/dist/simulation/scenarios/bmssp-integration.d.ts.map +0 -1
  431. package/dist/simulation/scenarios/bmssp-integration.js +0 -104
  432. package/dist/simulation/scenarios/bmssp-integration.js.map +0 -1
  433. package/dist/simulation/scenarios/causal-reasoning.d.ts +0 -16
  434. package/dist/simulation/scenarios/causal-reasoning.d.ts.map +0 -1
  435. package/dist/simulation/scenarios/causal-reasoning.js +0 -108
  436. package/dist/simulation/scenarios/causal-reasoning.js.map +0 -1
  437. package/dist/simulation/scenarios/consciousness-explorer.d.ts +0 -25
  438. package/dist/simulation/scenarios/consciousness-explorer.d.ts.map +0 -1
  439. package/dist/simulation/scenarios/consciousness-explorer.js +0 -108
  440. package/dist/simulation/scenarios/consciousness-explorer.js.map +0 -1
  441. package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.d.ts +0 -135
  442. package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.d.ts.map +0 -1
  443. package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.js +0 -158
  444. package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.js.map +0 -1
  445. package/dist/simulation/scenarios/domain-examples/index.d.ts +0 -14
  446. package/dist/simulation/scenarios/domain-examples/index.d.ts.map +0 -1
  447. package/dist/simulation/scenarios/domain-examples/index.js +0 -14
  448. package/dist/simulation/scenarios/domain-examples/index.js.map +0 -1
  449. package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.d.ts +0 -178
  450. package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.d.ts.map +0 -1
  451. package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.js +0 -218
  452. package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.js.map +0 -1
  453. package/dist/simulation/scenarios/domain-examples/medical-imaging.d.ts +0 -122
  454. package/dist/simulation/scenarios/domain-examples/medical-imaging.d.ts.map +0 -1
  455. package/dist/simulation/scenarios/domain-examples/medical-imaging.js +0 -129
  456. package/dist/simulation/scenarios/domain-examples/medical-imaging.js.map +0 -1
  457. package/dist/simulation/scenarios/domain-examples/robotics-navigation.d.ts +0 -166
  458. package/dist/simulation/scenarios/domain-examples/robotics-navigation.d.ts.map +0 -1
  459. package/dist/simulation/scenarios/domain-examples/robotics-navigation.js +0 -161
  460. package/dist/simulation/scenarios/domain-examples/robotics-navigation.js.map +0 -1
  461. package/dist/simulation/scenarios/domain-examples/scientific-research.d.ts +0 -151
  462. package/dist/simulation/scenarios/domain-examples/scientific-research.d.ts.map +0 -1
  463. package/dist/simulation/scenarios/domain-examples/scientific-research.js +0 -187
  464. package/dist/simulation/scenarios/domain-examples/scientific-research.js.map +0 -1
  465. package/dist/simulation/scenarios/domain-examples/trading-systems.d.ts +0 -102
  466. package/dist/simulation/scenarios/domain-examples/trading-systems.d.ts.map +0 -1
  467. package/dist/simulation/scenarios/domain-examples/trading-systems.js +0 -100
  468. package/dist/simulation/scenarios/domain-examples/trading-systems.js.map +0 -1
  469. package/dist/simulation/scenarios/goalie-integration.d.ts +0 -24
  470. package/dist/simulation/scenarios/goalie-integration.d.ts.map +0 -1
  471. package/dist/simulation/scenarios/goalie-integration.js +0 -121
  472. package/dist/simulation/scenarios/goalie-integration.js.map +0 -1
  473. package/dist/simulation/scenarios/graph-traversal.d.ts +0 -17
  474. package/dist/simulation/scenarios/graph-traversal.d.ts.map +0 -1
  475. package/dist/simulation/scenarios/graph-traversal.js +0 -101
  476. package/dist/simulation/scenarios/graph-traversal.js.map +0 -1
  477. package/dist/simulation/scenarios/latent-space/attention-analysis.d.ts +0 -45
  478. package/dist/simulation/scenarios/latent-space/attention-analysis.d.ts.map +0 -1
  479. package/dist/simulation/scenarios/latent-space/attention-analysis.js +0 -435
  480. package/dist/simulation/scenarios/latent-space/attention-analysis.js.map +0 -1
  481. package/dist/simulation/scenarios/latent-space/clustering-analysis.d.ts +0 -55
  482. package/dist/simulation/scenarios/latent-space/clustering-analysis.d.ts.map +0 -1
  483. package/dist/simulation/scenarios/latent-space/clustering-analysis.js +0 -606
  484. package/dist/simulation/scenarios/latent-space/clustering-analysis.js.map +0 -1
  485. package/dist/simulation/scenarios/latent-space/hnsw-exploration.d.ts +0 -74
  486. package/dist/simulation/scenarios/latent-space/hnsw-exploration.d.ts.map +0 -1
  487. package/dist/simulation/scenarios/latent-space/hnsw-exploration.js +0 -391
  488. package/dist/simulation/scenarios/latent-space/hnsw-exploration.js.map +0 -1
  489. package/dist/simulation/scenarios/latent-space/hypergraph-exploration.d.ts +0 -51
  490. package/dist/simulation/scenarios/latent-space/hypergraph-exploration.d.ts.map +0 -1
  491. package/dist/simulation/scenarios/latent-space/hypergraph-exploration.js +0 -536
  492. package/dist/simulation/scenarios/latent-space/hypergraph-exploration.js.map +0 -1
  493. package/dist/simulation/scenarios/latent-space/index.d.ts +0 -35
  494. package/dist/simulation/scenarios/latent-space/index.d.ts.map +0 -1
  495. package/dist/simulation/scenarios/latent-space/index.js +0 -35
  496. package/dist/simulation/scenarios/latent-space/index.js.map +0 -1
  497. package/dist/simulation/scenarios/latent-space/neural-augmentation.d.ts +0 -52
  498. package/dist/simulation/scenarios/latent-space/neural-augmentation.d.ts.map +0 -1
  499. package/dist/simulation/scenarios/latent-space/neural-augmentation.js +0 -472
  500. package/dist/simulation/scenarios/latent-space/neural-augmentation.js.map +0 -1
  501. package/dist/simulation/scenarios/latent-space/quantum-hybrid.d.ts +0 -53
  502. package/dist/simulation/scenarios/latent-space/quantum-hybrid.d.ts.map +0 -1
  503. package/dist/simulation/scenarios/latent-space/quantum-hybrid.js +0 -391
  504. package/dist/simulation/scenarios/latent-space/quantum-hybrid.js.map +0 -1
  505. package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.d.ts +0 -52
  506. package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.d.ts.map +0 -1
  507. package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.js +0 -514
  508. package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.js.map +0 -1
  509. package/dist/simulation/scenarios/latent-space/traversal-optimization.d.ts +0 -53
  510. package/dist/simulation/scenarios/latent-space/traversal-optimization.d.ts.map +0 -1
  511. package/dist/simulation/scenarios/latent-space/traversal-optimization.js +0 -595
  512. package/dist/simulation/scenarios/latent-space/traversal-optimization.js.map +0 -1
  513. package/dist/simulation/scenarios/lean-agentic-swarm.d.ts +0 -18
  514. package/dist/simulation/scenarios/lean-agentic-swarm.d.ts.map +0 -1
  515. package/dist/simulation/scenarios/lean-agentic-swarm.js +0 -132
  516. package/dist/simulation/scenarios/lean-agentic-swarm.js.map +0 -1
  517. package/dist/simulation/scenarios/multi-agent-swarm.d.ts +0 -17
  518. package/dist/simulation/scenarios/multi-agent-swarm.d.ts.map +0 -1
  519. package/dist/simulation/scenarios/multi-agent-swarm.js +0 -111
  520. package/dist/simulation/scenarios/multi-agent-swarm.js.map +0 -1
  521. package/dist/simulation/scenarios/psycho-symbolic-reasoner.d.ts +0 -23
  522. package/dist/simulation/scenarios/psycho-symbolic-reasoner.d.ts.map +0 -1
  523. package/dist/simulation/scenarios/psycho-symbolic-reasoner.js +0 -98
  524. package/dist/simulation/scenarios/psycho-symbolic-reasoner.js.map +0 -1
  525. package/dist/simulation/scenarios/reflexion-learning.d.ts +0 -16
  526. package/dist/simulation/scenarios/reflexion-learning.d.ts.map +0 -1
  527. package/dist/simulation/scenarios/reflexion-learning.js +0 -98
  528. package/dist/simulation/scenarios/reflexion-learning.js.map +0 -1
  529. package/dist/simulation/scenarios/research-swarm.d.ts +0 -24
  530. package/dist/simulation/scenarios/research-swarm.d.ts.map +0 -1
  531. package/dist/simulation/scenarios/research-swarm.js +0 -146
  532. package/dist/simulation/scenarios/research-swarm.js.map +0 -1
  533. package/dist/simulation/scenarios/skill-evolution.d.ts +0 -16
  534. package/dist/simulation/scenarios/skill-evolution.d.ts.map +0 -1
  535. package/dist/simulation/scenarios/skill-evolution.js +0 -109
  536. package/dist/simulation/scenarios/skill-evolution.js.map +0 -1
  537. package/dist/simulation/scenarios/stock-market-emergence.d.ts +0 -33
  538. package/dist/simulation/scenarios/stock-market-emergence.d.ts.map +0 -1
  539. package/dist/simulation/scenarios/stock-market-emergence.js +0 -246
  540. package/dist/simulation/scenarios/stock-market-emergence.js.map +0 -1
  541. package/dist/simulation/scenarios/strange-loops.d.ts +0 -18
  542. package/dist/simulation/scenarios/strange-loops.d.ts.map +0 -1
  543. package/dist/simulation/scenarios/strange-loops.js +0 -133
  544. package/dist/simulation/scenarios/strange-loops.js.map +0 -1
  545. package/dist/simulation/scenarios/sublinear-solver.d.ts +0 -22
  546. package/dist/simulation/scenarios/sublinear-solver.d.ts.map +0 -1
  547. package/dist/simulation/scenarios/sublinear-solver.js +0 -82
  548. package/dist/simulation/scenarios/sublinear-solver.js.map +0 -1
  549. package/dist/simulation/scenarios/temporal-lead-solver.d.ts +0 -23
  550. package/dist/simulation/scenarios/temporal-lead-solver.d.ts.map +0 -1
  551. package/dist/simulation/scenarios/temporal-lead-solver.js +0 -90
  552. package/dist/simulation/scenarios/temporal-lead-solver.js.map +0 -1
  553. package/dist/simulation/scenarios/voting-system-consensus.d.ts +0 -27
  554. package/dist/simulation/scenarios/voting-system-consensus.d.ts.map +0 -1
  555. package/dist/simulation/scenarios/voting-system-consensus.js +0 -175
  556. package/dist/simulation/scenarios/voting-system-consensus.js.map +0 -1
  557. package/dist/simulation/types.d.ts +0 -162
  558. package/dist/simulation/types.d.ts.map +0 -1
  559. package/dist/simulation/types.js +0 -5
  560. package/dist/simulation/types.js.map +0 -1
  561. package/dist/simulation/utils/PerformanceOptimizer.d.ts +0 -115
  562. package/dist/simulation/utils/PerformanceOptimizer.d.ts.map +0 -1
  563. package/dist/simulation/utils/PerformanceOptimizer.js +0 -223
  564. package/dist/simulation/utils/PerformanceOptimizer.js.map +0 -1
  565. package/dist/src/backends/index.cjs +0 -6
  566. package/dist/src/backends/ruvector/GuardedVectorBackend.d.ts +0 -93
  567. package/dist/src/backends/ruvector/GuardedVectorBackend.d.ts.map +0 -1
  568. package/dist/src/backends/ruvector/GuardedVectorBackend.js +0 -182
  569. package/dist/src/backends/ruvector/GuardedVectorBackend.js.map +0 -1
  570. package/dist/src/consensus/RaftConsensus.d.ts +0 -220
  571. package/dist/src/consensus/RaftConsensus.d.ts.map +0 -1
  572. package/dist/src/consensus/RaftConsensus.js +0 -762
  573. package/dist/src/consensus/RaftConsensus.js.map +0 -1
  574. package/dist/src/controllers/HierarchicalMemory.d.ts +0 -197
  575. package/dist/src/controllers/HierarchicalMemory.d.ts.map +0 -1
  576. package/dist/src/controllers/HierarchicalMemory.js +0 -519
  577. package/dist/src/controllers/HierarchicalMemory.js.map +0 -1
  578. package/dist/src/controllers/MemoryConsolidation.d.ts +0 -142
  579. package/dist/src/controllers/MemoryConsolidation.d.ts.map +0 -1
  580. package/dist/src/controllers/MemoryConsolidation.js +0 -479
  581. package/dist/src/controllers/MemoryConsolidation.js.map +0 -1
  582. package/dist/src/controllers/QUICConnection.d.ts +0 -122
  583. package/dist/src/controllers/QUICConnection.d.ts.map +0 -1
  584. package/dist/src/controllers/QUICConnection.js +0 -329
  585. package/dist/src/controllers/QUICConnection.js.map +0 -1
  586. package/dist/src/controllers/QUICConnectionPool.d.ts +0 -83
  587. package/dist/src/controllers/QUICConnectionPool.d.ts.map +0 -1
  588. package/dist/src/controllers/QUICConnectionPool.js +0 -256
  589. package/dist/src/controllers/QUICConnectionPool.js.map +0 -1
  590. package/dist/src/controllers/QUICStreamManager.d.ts +0 -114
  591. package/dist/src/controllers/QUICStreamManager.d.ts.map +0 -1
  592. package/dist/src/controllers/QUICStreamManager.js +0 -267
  593. package/dist/src/controllers/QUICStreamManager.js.map +0 -1
  594. package/dist/src/controllers/StreamingEmbeddingService.d.ts +0 -82
  595. package/dist/src/controllers/StreamingEmbeddingService.d.ts.map +0 -1
  596. package/dist/src/controllers/StreamingEmbeddingService.js +0 -243
  597. package/dist/src/controllers/StreamingEmbeddingService.js.map +0 -1
  598. package/dist/src/controllers/index.cjs +0 -6
  599. package/dist/src/coordination/MultiDatabaseCoordinator.d.ts +0 -348
  600. package/dist/src/coordination/MultiDatabaseCoordinator.d.ts.map +0 -1
  601. package/dist/src/coordination/MultiDatabaseCoordinator.js +0 -803
  602. package/dist/src/coordination/MultiDatabaseCoordinator.js.map +0 -1
  603. package/dist/src/coordination/index.d.ts +0 -10
  604. package/dist/src/coordination/index.d.ts.map +0 -1
  605. package/dist/src/coordination/index.js +0 -10
  606. package/dist/src/coordination/index.js.map +0 -1
  607. package/dist/src/index.cjs +0 -6
  608. package/dist/src/optimizations/RVFOptimizer.d.ts +0 -226
  609. package/dist/src/optimizations/RVFOptimizer.d.ts.map +0 -1
  610. package/dist/src/optimizations/RVFOptimizer.js +0 -541
  611. package/dist/src/optimizations/RVFOptimizer.js.map +0 -1
  612. package/dist/src/security/AttestationLog.d.ts +0 -70
  613. package/dist/src/security/AttestationLog.d.ts.map +0 -1
  614. package/dist/src/security/AttestationLog.js +0 -174
  615. package/dist/src/security/AttestationLog.js.map +0 -1
  616. package/dist/src/security/MutationGuard.d.ts +0 -83
  617. package/dist/src/security/MutationGuard.d.ts.map +0 -1
  618. package/dist/src/security/MutationGuard.js +0 -364
  619. package/dist/src/security/MutationGuard.js.map +0 -1
  620. package/dist/src/security/index.cjs +0 -6
  621. package/dist/src/security/index.d.ts +0 -15
  622. package/dist/src/security/index.d.ts.map +0 -1
  623. package/dist/src/security/index.js +0 -18
  624. package/dist/src/security/index.js.map +0 -1
  625. package/dist/src/services/GNNService.d.ts +0 -173
  626. package/dist/src/services/GNNService.d.ts.map +0 -1
  627. package/dist/src/services/GNNService.js +0 -639
  628. package/dist/src/services/GNNService.js.map +0 -1
  629. package/dist/src/services/GraphTransformerService.d.ts +0 -80
  630. package/dist/src/services/GraphTransformerService.d.ts.map +0 -1
  631. package/dist/src/services/GraphTransformerService.js +0 -369
  632. package/dist/src/services/GraphTransformerService.js.map +0 -1
  633. package/dist/src/services/SemanticRouter.d.ts +0 -83
  634. package/dist/src/services/SemanticRouter.d.ts.map +0 -1
  635. package/dist/src/services/SemanticRouter.js +0 -160
  636. package/dist/src/services/SemanticRouter.js.map +0 -1
  637. package/dist/src/services/SonaTrajectoryService.d.ts +0 -224
  638. package/dist/src/services/SonaTrajectoryService.d.ts.map +0 -1
  639. package/dist/src/services/SonaTrajectoryService.js +0 -539
  640. package/dist/src/services/SonaTrajectoryService.js.map +0 -1
  641. package/dist/src/utils/LegacyAttentionAdapter.d.ts +0 -93
  642. package/dist/src/utils/LegacyAttentionAdapter.d.ts.map +0 -1
  643. package/dist/src/utils/LegacyAttentionAdapter.js +0 -241
  644. package/dist/src/utils/LegacyAttentionAdapter.js.map +0 -1
  645. package/dist/src/utils/vector-math.d.ts +0 -29
  646. package/dist/src/utils/vector-math.d.ts.map +0 -1
  647. package/dist/src/utils/vector-math.js +0 -66
  648. package/dist/src/utils/vector-math.js.map +0 -1
@@ -0,0 +1,1484 @@
1
+ /**
2
+ * Attention Module Fallbacks
3
+ *
4
+ * Since @ruvector/attention is completely broken, provide JavaScript fallbacks
5
+ * Performance will be slower but functionality will work
6
+ *
7
+ * Optimized version includes:
8
+ * - SIMD-style loop unrolling (8x with separate accumulators)
9
+ * - Pre-allocated buffer pools for intermediate results
10
+ * - Numerically stable fused softmax operations
11
+ * - TypedArray optimizations (Float32Array)
12
+ * - Batch processing capabilities
13
+ * - Flash attention with improved tiling strategy
14
+ */
15
+ // ============================================================================
16
+ // Security Constants
17
+ // ============================================================================
18
+ /** Maximum hidden dimension to prevent memory exhaustion */
19
+ const MAX_HIDDEN_DIM = 16384;
20
+ /** Maximum sequence length for attention */
21
+ const MAX_SEQ_LENGTH = 32768;
22
+ /** Maximum buffer pool size per dimension */
23
+ const MAX_BUFFER_POOL_SIZE = 64;
24
+ /** Maximum total memory for buffer pools (64MB) */
25
+ const MAX_BUFFER_POOL_MEMORY = 64 * 1024 * 1024;
26
+ /** Small epsilon for numerical stability */
27
+ const EPSILON = 1e-8;
28
+ // ============================================================================
29
+ // Validation Helpers
30
+ // ============================================================================
31
+ /**
32
+ * Validate dimension is within safe limits
33
+ */
34
+ function validateDimension(dim, name) {
35
+ if (!Number.isFinite(dim) || dim < 1 || dim > MAX_HIDDEN_DIM) {
36
+ throw new Error(`${name} must be between 1 and ${MAX_HIDDEN_DIM}`);
37
+ }
38
+ }
39
+ /**
40
+ * Validate sequence length is within safe limits
41
+ */
42
+ function validateSeqLength(len, name) {
43
+ if (!Number.isFinite(len) || len < 1 || len > MAX_SEQ_LENGTH) {
44
+ throw new Error(`${name} must be between 1 and ${MAX_SEQ_LENGTH}`);
45
+ }
46
+ }
47
+ /**
48
+ * Validate Float32Array input
49
+ */
50
+ function validateFloat32Array(arr, name) {
51
+ if (!(arr instanceof Float32Array)) {
52
+ throw new Error(`${name} must be a Float32Array`);
53
+ }
54
+ }
55
+ // ============================================================================
56
+ // Buffer Pool Management - Pre-allocated buffers for hot paths
57
+ // ============================================================================
58
+ /**
59
+ * Buffer pool for reusing Float32Arrays to avoid GC pressure
60
+ */
61
+ class BufferPool {
62
+ pools = new Map();
63
+ maxPoolSize = MAX_BUFFER_POOL_SIZE;
64
+ totalMemory = 0;
65
+ acquire(size) {
66
+ // Validate size to prevent DoS
67
+ if (!Number.isFinite(size) || size < 0 || size > MAX_HIDDEN_DIM * MAX_SEQ_LENGTH) {
68
+ throw new Error(`Invalid buffer size: ${size}`);
69
+ }
70
+ const pool = this.pools.get(size);
71
+ if (pool && pool.length > 0) {
72
+ return pool.pop();
73
+ }
74
+ return new Float32Array(size);
75
+ }
76
+ release(buffer) {
77
+ const size = buffer.length;
78
+ const bufferBytes = size * 4; // Float32 = 4 bytes
79
+ // Check total memory limit before adding to pool
80
+ if (this.totalMemory + bufferBytes > MAX_BUFFER_POOL_MEMORY) {
81
+ return; // Don't pool, let GC handle it
82
+ }
83
+ let pool = this.pools.get(size);
84
+ if (!pool) {
85
+ pool = [];
86
+ this.pools.set(size, pool);
87
+ }
88
+ if (pool.length < this.maxPoolSize) {
89
+ buffer.fill(0); // Clear for reuse
90
+ pool.push(buffer);
91
+ this.totalMemory += bufferBytes;
92
+ }
93
+ }
94
+ clear() {
95
+ this.pools.clear();
96
+ this.totalMemory = 0;
97
+ }
98
+ }
99
+ // Global buffer pool instance
100
+ const globalBufferPool = new BufferPool();
101
+ // ============================================================================
102
+ // SIMD-Style Optimized Operations
103
+ // ============================================================================
104
+ /**
105
+ * 8x unrolled dot product with separate accumulators
106
+ * Mimics SIMD behavior for better CPU pipelining
107
+ */
108
+ function dotProduct8x(a, b, length) {
109
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
110
+ let sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
111
+ const unrolledLen = length - (length % 8);
112
+ // Main 8x unrolled loop
113
+ for (let i = 0; i < unrolledLen; i += 8) {
114
+ sum0 += a[i] * b[i];
115
+ sum1 += a[i + 1] * b[i + 1];
116
+ sum2 += a[i + 2] * b[i + 2];
117
+ sum3 += a[i + 3] * b[i + 3];
118
+ sum4 += a[i + 4] * b[i + 4];
119
+ sum5 += a[i + 5] * b[i + 5];
120
+ sum6 += a[i + 6] * b[i + 6];
121
+ sum7 += a[i + 7] * b[i + 7];
122
+ }
123
+ // Handle remainder
124
+ for (let i = unrolledLen; i < length; i++) {
125
+ sum0 += a[i] * b[i];
126
+ }
127
+ // Combine accumulators (in pairs to maintain precision)
128
+ return (sum0 + sum1) + (sum2 + sum3) + (sum4 + sum5) + (sum6 + sum7);
129
+ }
130
+ /**
131
+ * 8x unrolled matrix-vector multiplication
132
+ * output[i] = sum_j(matrix[i][j] * vector[j])
133
+ */
134
+ function matVecMul8x(matrix, vector, output, rows, cols) {
135
+ const unrolledCols = cols - (cols % 8);
136
+ for (let i = 0; i < rows; i++) {
137
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
138
+ let sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
139
+ const rowOffset = i * cols;
140
+ // 8x unrolled inner loop
141
+ for (let j = 0; j < unrolledCols; j += 8) {
142
+ const idx = rowOffset + j;
143
+ sum0 += matrix[idx] * vector[j];
144
+ sum1 += matrix[idx + 1] * vector[j + 1];
145
+ sum2 += matrix[idx + 2] * vector[j + 2];
146
+ sum3 += matrix[idx + 3] * vector[j + 3];
147
+ sum4 += matrix[idx + 4] * vector[j + 4];
148
+ sum5 += matrix[idx + 5] * vector[j + 5];
149
+ sum6 += matrix[idx + 6] * vector[j + 6];
150
+ sum7 += matrix[idx + 7] * vector[j + 7];
151
+ }
152
+ // Handle remainder
153
+ for (let j = unrolledCols; j < cols; j++) {
154
+ sum0 += matrix[rowOffset + j] * vector[j];
155
+ }
156
+ output[i] = (sum0 + sum1) + (sum2 + sum3) + (sum4 + sum5) + (sum6 + sum7);
157
+ }
158
+ }
159
+ /**
160
+ * 8x unrolled scaled add: output[i] = a[i] + scale * b[i]
161
+ */
162
+ function scaledAdd8x(output, a, b, scale, length) {
163
+ const unrolledLen = length - (length % 8);
164
+ for (let i = 0; i < unrolledLen; i += 8) {
165
+ output[i] = a[i] + scale * b[i];
166
+ output[i + 1] = a[i + 1] + scale * b[i + 1];
167
+ output[i + 2] = a[i + 2] + scale * b[i + 2];
168
+ output[i + 3] = a[i + 3] + scale * b[i + 3];
169
+ output[i + 4] = a[i + 4] + scale * b[i + 4];
170
+ output[i + 5] = a[i + 5] + scale * b[i + 5];
171
+ output[i + 6] = a[i + 6] + scale * b[i + 6];
172
+ output[i + 7] = a[i + 7] + scale * b[i + 7];
173
+ }
174
+ for (let i = unrolledLen; i < length; i++) {
175
+ output[i] = a[i] + scale * b[i];
176
+ }
177
+ }
178
+ // ============================================================================
179
+ // Optimized Softmax Operations
180
+ // ============================================================================
181
+ /**
182
+ * Numerically stable softmax with fused max-subtract-exp-sum operation
183
+ * Single pass for max finding, second pass for exp and sum
184
+ */
185
+ function softmaxInPlace(scores, length) {
186
+ // Validate length
187
+ if (length <= 0 || length > scores.length) {
188
+ throw new Error(`Invalid length: ${length}`);
189
+ }
190
+ // Find max (8x unrolled)
191
+ let max0 = -Infinity, max1 = -Infinity, max2 = -Infinity, max3 = -Infinity;
192
+ const unrolledLen = length - (length % 4);
193
+ for (let i = 0; i < unrolledLen; i += 4) {
194
+ if (scores[i] > max0)
195
+ max0 = scores[i];
196
+ if (scores[i + 1] > max1)
197
+ max1 = scores[i + 1];
198
+ if (scores[i + 2] > max2)
199
+ max2 = scores[i + 2];
200
+ if (scores[i + 3] > max3)
201
+ max3 = scores[i + 3];
202
+ }
203
+ let maxVal = Math.max(max0, max1, max2, max3);
204
+ for (let i = unrolledLen; i < length; i++) {
205
+ if (scores[i] > maxVal)
206
+ maxVal = scores[i];
207
+ }
208
+ // Handle edge case: all -Infinity scores
209
+ if (!Number.isFinite(maxVal)) {
210
+ // Uniform distribution when all scores are -Infinity
211
+ const uniformVal = 1.0 / length;
212
+ for (let i = 0; i < length; i++) {
213
+ scores[i] = uniformVal;
214
+ }
215
+ return;
216
+ }
217
+ // Fused subtract-exp-sum (4x unrolled for better cache usage)
218
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
219
+ for (let i = 0; i < unrolledLen; i += 4) {
220
+ const e0 = Math.exp(scores[i] - maxVal);
221
+ const e1 = Math.exp(scores[i + 1] - maxVal);
222
+ const e2 = Math.exp(scores[i + 2] - maxVal);
223
+ const e3 = Math.exp(scores[i + 3] - maxVal);
224
+ scores[i] = e0;
225
+ scores[i + 1] = e1;
226
+ scores[i + 2] = e2;
227
+ scores[i + 3] = e3;
228
+ sum0 += e0;
229
+ sum1 += e1;
230
+ sum2 += e2;
231
+ sum3 += e3;
232
+ }
233
+ for (let i = unrolledLen; i < length; i++) {
234
+ const e = Math.exp(scores[i] - maxVal);
235
+ scores[i] = e;
236
+ sum0 += e;
237
+ }
238
+ const sumTotal = sum0 + sum1 + sum2 + sum3;
239
+ // Guard against division by zero
240
+ if (sumTotal < EPSILON) {
241
+ // Uniform distribution when sum is effectively zero
242
+ const uniformVal = 1.0 / length;
243
+ for (let i = 0; i < length; i++) {
244
+ scores[i] = uniformVal;
245
+ }
246
+ return;
247
+ }
248
+ const invSum = 1.0 / sumTotal;
249
+ // Normalize (4x unrolled)
250
+ for (let i = 0; i < unrolledLen; i += 4) {
251
+ scores[i] *= invSum;
252
+ scores[i + 1] *= invSum;
253
+ scores[i + 2] *= invSum;
254
+ scores[i + 3] *= invSum;
255
+ }
256
+ for (let i = unrolledLen; i < length; i++) {
257
+ scores[i] *= invSum;
258
+ }
259
+ }
260
+ /**
261
+ * Softmax returning new array (when in-place not desired)
262
+ */
263
+ function softmaxOptimized(input) {
264
+ const output = globalBufferPool.acquire(input.length);
265
+ output.set(input);
266
+ softmaxInPlace(output, input.length);
267
+ return output;
268
+ }
269
+ // ============================================================================
270
+ // Original Scaled Dot-Product Attention (backward compatible)
271
+ // ============================================================================
272
+ /**
273
+ * Scaled Dot-Product Attention
274
+ * The core attention mechanism
275
+ */
276
+ export function scaledDotProductAttention(query, key, value, mask) {
277
+ const dk = query.length;
278
+ // Compute attention scores: Q · K^T / sqrt(dk)
279
+ let score = 0;
280
+ for (let i = 0; i < dk; i++) {
281
+ score += query[i] * key[i];
282
+ }
283
+ score /= Math.sqrt(dk);
284
+ // Apply mask if provided
285
+ if (mask && mask[0] === 0) {
286
+ score = -Infinity;
287
+ }
288
+ // Softmax (single score version)
289
+ const expScore = Math.exp(score);
290
+ const weight = expScore; // Simplified for single K,V pair
291
+ // Weighted value
292
+ const output = value.map(v => v * weight);
293
+ return { output, weights: [weight] };
294
+ }
295
+ // ============================================================================
296
+ // Optimized Scaled Dot-Product Attention with TypedArrays
297
+ // ============================================================================
298
+ /**
299
+ * Optimized scaled dot-product attention using TypedArrays and SIMD-style ops
300
+ */
301
+ export function scaledDotProductAttentionOptimized(query, key, value, mask) {
302
+ const dk = query.length;
303
+ const scale = 1.0 / Math.sqrt(dk);
304
+ // Compute attention score using 8x unrolled dot product
305
+ let score = dotProduct8x(query, key, dk) * scale;
306
+ // Apply mask if provided
307
+ if (mask && mask[0] === 0) {
308
+ score = -Infinity;
309
+ }
310
+ // Softmax (single score version)
311
+ const expScore = Math.exp(score);
312
+ const weight = expScore;
313
+ // Weighted value (using buffer pool)
314
+ const output = globalBufferPool.acquire(value.length);
315
+ const unrolledLen = value.length - (value.length % 8);
316
+ for (let i = 0; i < unrolledLen; i += 8) {
317
+ output[i] = value[i] * weight;
318
+ output[i + 1] = value[i + 1] * weight;
319
+ output[i + 2] = value[i + 2] * weight;
320
+ output[i + 3] = value[i + 3] * weight;
321
+ output[i + 4] = value[i + 4] * weight;
322
+ output[i + 5] = value[i + 5] * weight;
323
+ output[i + 6] = value[i + 6] * weight;
324
+ output[i + 7] = value[i + 7] * weight;
325
+ }
326
+ for (let i = unrolledLen; i < value.length; i++) {
327
+ output[i] = value[i] * weight;
328
+ }
329
+ return { output, weights: new Float32Array([weight]) };
330
+ }
331
+ /**
332
+ * Batch scaled dot-product attention for processing multiple queries at once
333
+ * Processes multiple Q-K-V triplets efficiently
334
+ */
335
+ export function batchScaledDotProductAttention(queries, keys, values, masks) {
336
+ const batchSize = queries.length;
337
+ const outputs = [];
338
+ const allWeights = [];
339
+ // Process in batches of 4 for cache efficiency
340
+ const batchOf4 = batchSize - (batchSize % 4);
341
+ for (let b = 0; b < batchOf4; b += 4) {
342
+ // Process 4 attention computations
343
+ for (let i = 0; i < 4; i++) {
344
+ const idx = b + i;
345
+ const mask = masks ? masks[idx] : null;
346
+ const { output, weights } = scaledDotProductAttentionOptimized(queries[idx], keys[idx], values[idx], mask);
347
+ outputs.push(output);
348
+ allWeights.push(weights);
349
+ }
350
+ }
351
+ // Handle remainder
352
+ for (let b = batchOf4; b < batchSize; b++) {
353
+ const mask = masks ? masks[b] : null;
354
+ const { output, weights } = scaledDotProductAttentionOptimized(queries[b], keys[b], values[b], mask);
355
+ outputs.push(output);
356
+ allWeights.push(weights);
357
+ }
358
+ return { outputs, weights: allWeights };
359
+ }
360
+ /**
361
+ * Full attention computation over sequences (Q, K, V matrices)
362
+ * Returns attention output and weights for all query positions
363
+ */
364
+ export function batchSequenceAttention(queries, // [seqLen * dim] flattened
365
+ keys, // [seqLen * dim] flattened
366
+ values, // [seqLen * dim] flattened
367
+ seqLen, dim, mask // [seqLen * seqLen] attention mask
368
+ ) {
369
+ const scale = 1.0 / Math.sqrt(dim);
370
+ const output = globalBufferPool.acquire(seqLen * dim);
371
+ const weights = globalBufferPool.acquire(seqLen * seqLen);
372
+ // Compute attention scores for each query position
373
+ for (let qi = 0; qi < seqLen; qi++) {
374
+ const qOffset = qi * dim;
375
+ const scoresOffset = qi * seqLen;
376
+ // Compute Q[qi] * K^T for all key positions
377
+ for (let ki = 0; ki < seqLen; ki++) {
378
+ const kOffset = ki * dim;
379
+ let score = 0;
380
+ // 8x unrolled dot product
381
+ const unrolledDim = dim - (dim % 8);
382
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
383
+ let sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
384
+ for (let d = 0; d < unrolledDim; d += 8) {
385
+ sum0 += queries[qOffset + d] * keys[kOffset + d];
386
+ sum1 += queries[qOffset + d + 1] * keys[kOffset + d + 1];
387
+ sum2 += queries[qOffset + d + 2] * keys[kOffset + d + 2];
388
+ sum3 += queries[qOffset + d + 3] * keys[kOffset + d + 3];
389
+ sum4 += queries[qOffset + d + 4] * keys[kOffset + d + 4];
390
+ sum5 += queries[qOffset + d + 5] * keys[kOffset + d + 5];
391
+ sum6 += queries[qOffset + d + 6] * keys[kOffset + d + 6];
392
+ sum7 += queries[qOffset + d + 7] * keys[kOffset + d + 7];
393
+ }
394
+ for (let d = unrolledDim; d < dim; d++) {
395
+ sum0 += queries[qOffset + d] * keys[kOffset + d];
396
+ }
397
+ score = ((sum0 + sum1) + (sum2 + sum3) + (sum4 + sum5) + (sum6 + sum7)) * scale;
398
+ // Apply mask if provided
399
+ if (mask && mask[scoresOffset + ki] === 0) {
400
+ score = -1e9; // Large negative instead of -Infinity for numerical stability
401
+ }
402
+ weights[scoresOffset + ki] = score;
403
+ }
404
+ // Softmax over the row
405
+ softmaxInPlace(new Float32Array(weights.buffer, scoresOffset * 4, seqLen), seqLen);
406
+ // Weighted sum of values
407
+ const outOffset = qi * dim;
408
+ for (let d = 0; d < dim; d++) {
409
+ output[outOffset + d] = 0;
410
+ }
411
+ for (let vi = 0; vi < seqLen; vi++) {
412
+ const vOffset = vi * dim;
413
+ const w = weights[scoresOffset + vi];
414
+ // 8x unrolled weighted add
415
+ const unrolledDim = dim - (dim % 8);
416
+ for (let d = 0; d < unrolledDim; d += 8) {
417
+ output[outOffset + d] += w * values[vOffset + d];
418
+ output[outOffset + d + 1] += w * values[vOffset + d + 1];
419
+ output[outOffset + d + 2] += w * values[vOffset + d + 2];
420
+ output[outOffset + d + 3] += w * values[vOffset + d + 3];
421
+ output[outOffset + d + 4] += w * values[vOffset + d + 4];
422
+ output[outOffset + d + 5] += w * values[vOffset + d + 5];
423
+ output[outOffset + d + 6] += w * values[vOffset + d + 6];
424
+ output[outOffset + d + 7] += w * values[vOffset + d + 7];
425
+ }
426
+ for (let d = unrolledDim; d < dim; d++) {
427
+ output[outOffset + d] += w * values[vOffset + d];
428
+ }
429
+ }
430
+ }
431
+ return { output, weights };
432
+ }
433
+ /**
434
+ * Multi-Head Attention (JavaScript fallback)
435
+ *
436
+ * Replaces broken @ruvector/attention.multiHeadAttention
437
+ */
438
+ export class MultiHeadAttention {
439
+ numHeads;
440
+ hiddenDim;
441
+ headDim;
442
+ queryWeights;
443
+ keyWeights;
444
+ valueWeights;
445
+ outputWeights;
446
+ constructor(config) {
447
+ // Validate config for security
448
+ validateDimension(config.hiddenDim, 'hiddenDim');
449
+ this.numHeads = Math.min(Math.max(1, config.numHeads || 8), 256);
450
+ this.hiddenDim = config.hiddenDim;
451
+ this.headDim = Math.floor(this.hiddenDim / this.numHeads);
452
+ if (this.headDim < 1) {
453
+ throw new Error('headDim must be at least 1 (hiddenDim / numHeads)');
454
+ }
455
+ // Initialize weights (random)
456
+ this.queryWeights = this.initializeWeights();
457
+ this.keyWeights = this.initializeWeights();
458
+ this.valueWeights = this.initializeWeights();
459
+ this.outputWeights = this.initializeOutputWeights();
460
+ }
461
+ initializeWeights() {
462
+ const weights = [];
463
+ for (let h = 0; h < this.numHeads; h++) {
464
+ const headWeights = [];
465
+ for (let i = 0; i < this.headDim; i++) {
466
+ const row = [];
467
+ for (let j = 0; j < this.hiddenDim; j++) {
468
+ row.push((Math.random() - 0.5) * 0.1);
469
+ }
470
+ headWeights.push(row);
471
+ }
472
+ weights.push(headWeights);
473
+ }
474
+ return weights;
475
+ }
476
+ initializeOutputWeights() {
477
+ const weights = [];
478
+ for (let i = 0; i < this.hiddenDim; i++) {
479
+ const row = [];
480
+ for (let j = 0; j < this.hiddenDim; j++) {
481
+ row.push((Math.random() - 0.5) * 0.1);
482
+ }
483
+ weights.push(row);
484
+ }
485
+ return weights;
486
+ }
487
+ forward(query, key, value, mask) {
488
+ const headOutputs = [];
489
+ const allWeights = [];
490
+ // Process each head
491
+ for (let h = 0; h < this.numHeads; h++) {
492
+ // Project to head dimension
493
+ const q = this.project(query, this.queryWeights[h]);
494
+ const k = this.project(key, this.keyWeights[h]);
495
+ const v = this.project(value, this.valueWeights[h]);
496
+ // Attention for this head
497
+ const { output, weights } = scaledDotProductAttention(q, k, v, mask);
498
+ headOutputs.push(output);
499
+ allWeights.push(weights);
500
+ }
501
+ // Concatenate heads
502
+ const concatenated = headOutputs.flat();
503
+ // Output projection
504
+ const output = this.project(concatenated, this.outputWeights);
505
+ return { output, attentionWeights: allWeights };
506
+ }
507
+ project(input, weights) {
508
+ const output = [];
509
+ for (let i = 0; i < weights.length; i++) {
510
+ let sum = 0;
511
+ for (let j = 0; j < input.length; j++) {
512
+ sum += input[j] * weights[i][j];
513
+ }
514
+ output.push(sum);
515
+ }
516
+ return output;
517
+ }
518
+ }
519
+ // ============================================================================
520
+ // Optimized Multi-Head Attention with TypedArrays and SIMD-style operations
521
+ // ============================================================================
522
+ /**
523
+ * Optimized Multi-Head Attention using TypedArrays and pre-allocated buffers
524
+ *
525
+ * Improvements over original:
526
+ * - Float32Array instead of number[]
527
+ * - Pre-allocated weight matrices in contiguous memory
528
+ * - 8x loop unrolling for projections
529
+ * - Buffer pool for intermediate results
530
+ * - Batch processing support
531
+ */
532
+ export class MultiHeadAttentionOptimized {
533
+ numHeads;
534
+ hiddenDim;
535
+ headDim;
536
+ // Contiguous weight storage for cache efficiency
537
+ // Shape: [numHeads * headDim * hiddenDim]
538
+ queryWeights;
539
+ keyWeights;
540
+ valueWeights;
541
+ // Shape: [hiddenDim * hiddenDim]
542
+ outputWeights;
543
+ // Pre-allocated buffers for intermediate results
544
+ headBuffer;
545
+ projBuffer;
546
+ concatBuffer;
547
+ constructor(config) {
548
+ // Validate config
549
+ validateDimension(config.hiddenDim, 'hiddenDim');
550
+ this.numHeads = Math.min(Math.max(1, config.numHeads || 8), 256);
551
+ this.hiddenDim = config.hiddenDim;
552
+ this.headDim = Math.floor(this.hiddenDim / this.numHeads);
553
+ if (this.headDim < 1) {
554
+ throw new Error('headDim must be at least 1 (hiddenDim / numHeads)');
555
+ }
556
+ const projSize = this.numHeads * this.headDim * this.hiddenDim;
557
+ // Initialize contiguous weight arrays
558
+ this.queryWeights = this.initializeProjectionWeights(projSize);
559
+ this.keyWeights = this.initializeProjectionWeights(projSize);
560
+ this.valueWeights = this.initializeProjectionWeights(projSize);
561
+ this.outputWeights = this.initializeOutputWeights();
562
+ // Pre-allocate buffers
563
+ this.headBuffer = new Float32Array(this.headDim);
564
+ this.projBuffer = new Float32Array(this.headDim * 3); // Q, K, V for one head
565
+ this.concatBuffer = new Float32Array(this.numHeads * this.headDim);
566
+ }
567
+ initializeProjectionWeights(size) {
568
+ const weights = new Float32Array(size);
569
+ const scale = 0.1;
570
+ for (let i = 0; i < size; i++) {
571
+ weights[i] = (Math.random() - 0.5) * scale;
572
+ }
573
+ return weights;
574
+ }
575
+ initializeOutputWeights() {
576
+ const size = this.hiddenDim * this.hiddenDim;
577
+ const weights = new Float32Array(size);
578
+ const scale = 0.1;
579
+ for (let i = 0; i < size; i++) {
580
+ weights[i] = (Math.random() - 0.5) * scale;
581
+ }
582
+ return weights;
583
+ }
584
+ /**
585
+ * Forward pass with optimized TypedArray operations
586
+ */
587
+ forward(query, key, value, mask) {
588
+ const allWeights = [];
589
+ const headOutputs = [];
590
+ // Process each head with optimized projections
591
+ for (let h = 0; h < this.numHeads; h++) {
592
+ const weightOffset = h * this.headDim * this.hiddenDim;
593
+ // Project Q, K, V for this head using 8x unrolled matmul
594
+ const q = this.projectOptimized(query, this.queryWeights, weightOffset);
595
+ const k = this.projectOptimized(key, this.keyWeights, weightOffset);
596
+ const v = this.projectOptimized(value, this.valueWeights, weightOffset);
597
+ // Scaled dot-product attention
598
+ const { output, weights } = scaledDotProductAttentionOptimized(q, k, v, mask);
599
+ headOutputs.push(output);
600
+ allWeights.push(weights);
601
+ }
602
+ // Concatenate heads into pre-allocated buffer
603
+ let offset = 0;
604
+ for (let h = 0; h < this.numHeads; h++) {
605
+ this.concatBuffer.set(headOutputs[h], offset);
606
+ offset += headOutputs[h].length;
607
+ }
608
+ // Output projection
609
+ const output = globalBufferPool.acquire(this.hiddenDim);
610
+ matVecMul8x(this.outputWeights, this.concatBuffer, output, this.hiddenDim, this.hiddenDim);
611
+ return { output, attentionWeights: allWeights };
612
+ }
613
+ /**
614
+ * Batch forward pass for multiple inputs
615
+ */
616
+ batchForward(queries, keys, values, masks) {
617
+ const outputs = [];
618
+ const allWeights = [];
619
+ for (let i = 0; i < queries.length; i++) {
620
+ const mask = masks ? masks[i] : null;
621
+ const { output, attentionWeights } = this.forward(queries[i], keys[i], values[i], mask);
622
+ outputs.push(output);
623
+ allWeights.push(attentionWeights);
624
+ }
625
+ return { outputs, attentionWeights: allWeights };
626
+ }
627
+ /**
628
+ * Optimized projection using contiguous weight storage and 8x unrolling
629
+ */
630
+ projectOptimized(input, weights, weightOffset) {
631
+ const output = globalBufferPool.acquire(this.headDim);
632
+ const inputLen = input.length;
633
+ const unrolledLen = inputLen - (inputLen % 8);
634
+ for (let i = 0; i < this.headDim; i++) {
635
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
636
+ let sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
637
+ const rowOffset = weightOffset + i * inputLen;
638
+ // 8x unrolled inner loop
639
+ for (let j = 0; j < unrolledLen; j += 8) {
640
+ sum0 += input[j] * weights[rowOffset + j];
641
+ sum1 += input[j + 1] * weights[rowOffset + j + 1];
642
+ sum2 += input[j + 2] * weights[rowOffset + j + 2];
643
+ sum3 += input[j + 3] * weights[rowOffset + j + 3];
644
+ sum4 += input[j + 4] * weights[rowOffset + j + 4];
645
+ sum5 += input[j + 5] * weights[rowOffset + j + 5];
646
+ sum6 += input[j + 6] * weights[rowOffset + j + 6];
647
+ sum7 += input[j + 7] * weights[rowOffset + j + 7];
648
+ }
649
+ // Handle remainder
650
+ for (let j = unrolledLen; j < inputLen; j++) {
651
+ sum0 += input[j] * weights[rowOffset + j];
652
+ }
653
+ output[i] = (sum0 + sum1) + (sum2 + sum3) + (sum4 + sum5) + (sum6 + sum7);
654
+ }
655
+ return output;
656
+ }
657
+ /**
658
+ * Release buffers back to pool (call when done with outputs)
659
+ */
660
+ releaseBuffer(buffer) {
661
+ globalBufferPool.release(buffer);
662
+ }
663
+ /**
664
+ * Get weight matrices (for serialization/loading)
665
+ */
666
+ getWeights() {
667
+ return {
668
+ query: this.queryWeights,
669
+ key: this.keyWeights,
670
+ value: this.valueWeights,
671
+ output: this.outputWeights,
672
+ };
673
+ }
674
+ /**
675
+ * Set weight matrices (for loading pre-trained weights)
676
+ */
677
+ setWeights(weights) {
678
+ this.queryWeights.set(weights.query);
679
+ this.keyWeights.set(weights.key);
680
+ this.valueWeights.set(weights.value);
681
+ this.outputWeights.set(weights.output);
682
+ }
683
+ }
684
+ /**
685
+ * Flash Attention (optimized fallback)
686
+ *
687
+ * Replaces broken @ruvector/attention.flashAttention
688
+ * Uses tiling/chunking for better memory efficiency
689
+ */
690
+ export class FlashAttention {
691
+ hiddenDim;
692
+ blockSize;
693
+ constructor(config) {
694
+ this.hiddenDim = config.hiddenDim;
695
+ this.blockSize = Math.min(64, this.hiddenDim); // Tile size
696
+ }
697
+ forward(query, key, value, numHeads = 8) {
698
+ const seqLen = query.length;
699
+ const headDim = this.hiddenDim / numHeads;
700
+ const output = [];
701
+ const attentionScores = [];
702
+ // Process in blocks for memory efficiency
703
+ for (let i = 0; i < seqLen; i += this.blockSize) {
704
+ const blockEnd = Math.min(i + this.blockSize, seqLen);
705
+ for (let qi = i; qi < blockEnd; qi++) {
706
+ const scores = [];
707
+ let maxScore = -Infinity;
708
+ // Compute attention scores for this query
709
+ for (let ki = 0; ki < seqLen; ki++) {
710
+ let score = 0;
711
+ for (let d = 0; d < query[qi].length; d++) {
712
+ score += query[qi][d] * key[ki][d];
713
+ }
714
+ score /= Math.sqrt(headDim);
715
+ scores.push(score);
716
+ maxScore = Math.max(maxScore, score);
717
+ }
718
+ // Numerically stable softmax
719
+ const expScores = scores.map(s => Math.exp(s - maxScore));
720
+ const sumExp = expScores.reduce((a, b) => a + b, 0);
721
+ const weights = expScores.map(e => e / sumExp);
722
+ // Weighted sum of values
723
+ const outputRow = new Array(value[0].length).fill(0);
724
+ for (let vi = 0; vi < seqLen; vi++) {
725
+ for (let d = 0; d < value[vi].length; d++) {
726
+ outputRow[d] += weights[vi] * value[vi][d];
727
+ }
728
+ }
729
+ output.push(outputRow);
730
+ attentionScores.push(weights);
731
+ }
732
+ }
733
+ return { output, attentionScores };
734
+ }
735
+ }
736
+ /**
737
+ * Optimized Flash Attention using TypedArrays and improved tiling strategy
738
+ *
739
+ * Improvements over original:
740
+ * - Separate Q and KV block sizes for better memory usage
741
+ * - Online softmax computation (memory efficient)
742
+ * - Pre-allocated buffers for all intermediate results
743
+ * - 8x loop unrolling for score computation
744
+ * - Causal masking support
745
+ * - Backward pass placeholder for gradient computation
746
+ */
747
+ export class FlashAttentionOptimized {
748
+ hiddenDim;
749
+ blockSizeQ;
750
+ blockSizeKV;
751
+ causal;
752
+ // Pre-allocated buffers
753
+ scoresBuffer;
754
+ expBuffer;
755
+ outputBuffer;
756
+ maxBuffer;
757
+ sumBuffer;
758
+ constructor(config) {
759
+ // Validate config
760
+ validateDimension(config.hiddenDim, 'hiddenDim');
761
+ this.hiddenDim = config.hiddenDim;
762
+ // Optimal block sizes for L1/L2 cache (typically 32KB-256KB)
763
+ // Bounded for security
764
+ this.blockSizeQ = Math.min(Math.max(8, config.blockSizeQ || 32), 256);
765
+ this.blockSizeKV = Math.min(Math.max(8, config.blockSizeKV || 64), 256);
766
+ this.causal = config.causal || false;
767
+ // Validate that block sizes don't cause excessive memory allocation
768
+ const maxBlockMemory = this.blockSizeQ * this.blockSizeKV * 4 * 2; // scores + exp buffers
769
+ const maxOutputMemory = this.blockSizeQ * this.hiddenDim * 4;
770
+ if (maxBlockMemory + maxOutputMemory > 64 * 1024 * 1024) { // 64MB limit
771
+ throw new Error('Block sizes would require excessive memory allocation');
772
+ }
773
+ // Pre-allocate buffers for maximum expected sizes
774
+ this.scoresBuffer = new Float32Array(this.blockSizeQ * this.blockSizeKV);
775
+ this.expBuffer = new Float32Array(this.blockSizeQ * this.blockSizeKV);
776
+ this.outputBuffer = new Float32Array(this.blockSizeQ * this.hiddenDim);
777
+ this.maxBuffer = new Float32Array(this.blockSizeQ);
778
+ this.sumBuffer = new Float32Array(this.blockSizeQ);
779
+ }
780
+ /**
781
+ * Forward pass with optimized tiling strategy
782
+ * Uses online softmax for memory efficiency
783
+ */
784
+ forward(query, // [seqLen * dim] flattened
785
+ key, // [seqLen * dim] flattened
786
+ value, // [seqLen * dim] flattened
787
+ seqLen, dim, numHeads = 8) {
788
+ // Validate inputs
789
+ validateFloat32Array(query, 'query');
790
+ validateFloat32Array(key, 'key');
791
+ validateFloat32Array(value, 'value');
792
+ validateSeqLength(seqLen, 'seqLen');
793
+ validateDimension(dim, 'dim');
794
+ // Validate array sizes match expected dimensions
795
+ const expectedSize = seqLen * dim;
796
+ if (query.length < expectedSize || key.length < expectedSize || value.length < expectedSize) {
797
+ throw new Error(`Input arrays too small for seqLen=${seqLen}, dim=${dim}`);
798
+ }
799
+ const headDim = dim / numHeads;
800
+ if (headDim < 1) {
801
+ throw new Error('Invalid numHeads: results in headDim < 1');
802
+ }
803
+ const scale = 1.0 / Math.sqrt(headDim);
804
+ // Allocate output arrays
805
+ const output = globalBufferPool.acquire(seqLen * dim);
806
+ const attentionScores = globalBufferPool.acquire(seqLen * seqLen);
807
+ // Initialize output to zero
808
+ output.fill(0);
809
+ // Process query blocks
810
+ for (let qBlockStart = 0; qBlockStart < seqLen; qBlockStart += this.blockSizeQ) {
811
+ const qBlockEnd = Math.min(qBlockStart + this.blockSizeQ, seqLen);
812
+ const qBlockSize = qBlockEnd - qBlockStart;
813
+ // Initialize running max and sum for online softmax
814
+ this.maxBuffer.fill(-Infinity);
815
+ this.sumBuffer.fill(0);
816
+ this.outputBuffer.fill(0);
817
+ // Process key-value blocks
818
+ const kvEnd = this.causal ? qBlockEnd : seqLen;
819
+ for (let kvBlockStart = 0; kvBlockStart < kvEnd; kvBlockStart += this.blockSizeKV) {
820
+ const kvBlockEnd = Math.min(kvBlockStart + this.blockSizeKV, kvEnd);
821
+ const kvBlockSize = kvBlockEnd - kvBlockStart;
822
+ // Compute attention scores for this block (Q_block @ K_block^T)
823
+ this.computeBlockScores(query, key, qBlockStart, qBlockEnd, kvBlockStart, kvBlockEnd, dim, scale);
824
+ // Apply causal mask if needed
825
+ if (this.causal) {
826
+ for (let qi = 0; qi < qBlockSize; qi++) {
827
+ const globalQi = qBlockStart + qi;
828
+ for (let ki = 0; ki < kvBlockSize; ki++) {
829
+ const globalKi = kvBlockStart + ki;
830
+ if (globalKi > globalQi) {
831
+ this.scoresBuffer[qi * kvBlockSize + ki] = -1e9;
832
+ }
833
+ }
834
+ }
835
+ }
836
+ // Online softmax update and output accumulation
837
+ this.updateOnlineSoftmax(value, qBlockSize, kvBlockSize, kvBlockStart, dim);
838
+ // Store attention scores
839
+ for (let qi = 0; qi < qBlockSize; qi++) {
840
+ const globalQi = qBlockStart + qi;
841
+ for (let ki = 0; ki < kvBlockSize; ki++) {
842
+ const globalKi = kvBlockStart + ki;
843
+ attentionScores[globalQi * seqLen + globalKi] = this.scoresBuffer[qi * kvBlockSize + ki];
844
+ }
845
+ }
846
+ }
847
+ // Normalize output by sum
848
+ for (let qi = 0; qi < qBlockSize; qi++) {
849
+ const globalQi = qBlockStart + qi;
850
+ const invSum = 1.0 / (this.sumBuffer[qi] + 1e-8);
851
+ const outOffset = globalQi * dim;
852
+ const localOffset = qi * dim;
853
+ // 8x unrolled normalization
854
+ const unrolledDim = dim - (dim % 8);
855
+ for (let d = 0; d < unrolledDim; d += 8) {
856
+ output[outOffset + d] = this.outputBuffer[localOffset + d] * invSum;
857
+ output[outOffset + d + 1] = this.outputBuffer[localOffset + d + 1] * invSum;
858
+ output[outOffset + d + 2] = this.outputBuffer[localOffset + d + 2] * invSum;
859
+ output[outOffset + d + 3] = this.outputBuffer[localOffset + d + 3] * invSum;
860
+ output[outOffset + d + 4] = this.outputBuffer[localOffset + d + 4] * invSum;
861
+ output[outOffset + d + 5] = this.outputBuffer[localOffset + d + 5] * invSum;
862
+ output[outOffset + d + 6] = this.outputBuffer[localOffset + d + 6] * invSum;
863
+ output[outOffset + d + 7] = this.outputBuffer[localOffset + d + 7] * invSum;
864
+ }
865
+ for (let d = unrolledDim; d < dim; d++) {
866
+ output[outOffset + d] = this.outputBuffer[localOffset + d] * invSum;
867
+ }
868
+ }
869
+ }
870
+ return { output, attentionScores };
871
+ }
872
+ /**
873
+ * Compute attention scores for a block pair with 8x unrolling
874
+ */
875
+ computeBlockScores(query, key, qStart, qEnd, kStart, kEnd, dim, scale) {
876
+ const qBlockSize = qEnd - qStart;
877
+ const kBlockSize = kEnd - kStart;
878
+ const unrolledDim = dim - (dim % 8);
879
+ for (let qi = 0; qi < qBlockSize; qi++) {
880
+ const qOffset = (qStart + qi) * dim;
881
+ for (let ki = 0; ki < kBlockSize; ki++) {
882
+ const kOffset = (kStart + ki) * dim;
883
+ // 8x unrolled dot product
884
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
885
+ let sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
886
+ for (let d = 0; d < unrolledDim; d += 8) {
887
+ sum0 += query[qOffset + d] * key[kOffset + d];
888
+ sum1 += query[qOffset + d + 1] * key[kOffset + d + 1];
889
+ sum2 += query[qOffset + d + 2] * key[kOffset + d + 2];
890
+ sum3 += query[qOffset + d + 3] * key[kOffset + d + 3];
891
+ sum4 += query[qOffset + d + 4] * key[kOffset + d + 4];
892
+ sum5 += query[qOffset + d + 5] * key[kOffset + d + 5];
893
+ sum6 += query[qOffset + d + 6] * key[kOffset + d + 6];
894
+ sum7 += query[qOffset + d + 7] * key[kOffset + d + 7];
895
+ }
896
+ for (let d = unrolledDim; d < dim; d++) {
897
+ sum0 += query[qOffset + d] * key[kOffset + d];
898
+ }
899
+ const score = ((sum0 + sum1) + (sum2 + sum3) + (sum4 + sum5) + (sum6 + sum7)) * scale;
900
+ this.scoresBuffer[qi * kBlockSize + ki] = score;
901
+ }
902
+ }
903
+ }
904
+ /**
905
+ * Online softmax update with output accumulation
906
+ * This is the core of flash attention's memory efficiency
907
+ */
908
+ updateOnlineSoftmax(value, qBlockSize, kvBlockSize, kvBlockStart, dim) {
909
+ for (let qi = 0; qi < qBlockSize; qi++) {
910
+ // Find new max
911
+ let newMax = this.maxBuffer[qi];
912
+ for (let ki = 0; ki < kvBlockSize; ki++) {
913
+ const score = this.scoresBuffer[qi * kvBlockSize + ki];
914
+ if (score > newMax)
915
+ newMax = score;
916
+ }
917
+ // Compute scaling factor for existing values
918
+ const oldMax = this.maxBuffer[qi];
919
+ const rescale = Math.exp(oldMax - newMax);
920
+ // Update running sum with rescaling
921
+ let newSum = this.sumBuffer[qi] * rescale;
922
+ // Compute exp scores and accumulate
923
+ const localOffset = qi * dim;
924
+ for (let ki = 0; ki < kvBlockSize; ki++) {
925
+ const expScore = Math.exp(this.scoresBuffer[qi * kvBlockSize + ki] - newMax);
926
+ this.expBuffer[qi * kvBlockSize + ki] = expScore;
927
+ newSum += expScore;
928
+ }
929
+ // Rescale existing output
930
+ if (rescale !== 1.0) {
931
+ const unrolledDim = dim - (dim % 8);
932
+ for (let d = 0; d < unrolledDim; d += 8) {
933
+ this.outputBuffer[localOffset + d] *= rescale;
934
+ this.outputBuffer[localOffset + d + 1] *= rescale;
935
+ this.outputBuffer[localOffset + d + 2] *= rescale;
936
+ this.outputBuffer[localOffset + d + 3] *= rescale;
937
+ this.outputBuffer[localOffset + d + 4] *= rescale;
938
+ this.outputBuffer[localOffset + d + 5] *= rescale;
939
+ this.outputBuffer[localOffset + d + 6] *= rescale;
940
+ this.outputBuffer[localOffset + d + 7] *= rescale;
941
+ }
942
+ for (let d = unrolledDim; d < dim; d++) {
943
+ this.outputBuffer[localOffset + d] *= rescale;
944
+ }
945
+ }
946
+ // Add new weighted values
947
+ for (let ki = 0; ki < kvBlockSize; ki++) {
948
+ const weight = this.expBuffer[qi * kvBlockSize + ki];
949
+ const vOffset = (kvBlockStart + ki) * dim;
950
+ // 8x unrolled weighted add
951
+ const unrolledDim = dim - (dim % 8);
952
+ for (let d = 0; d < unrolledDim; d += 8) {
953
+ this.outputBuffer[localOffset + d] += weight * value[vOffset + d];
954
+ this.outputBuffer[localOffset + d + 1] += weight * value[vOffset + d + 1];
955
+ this.outputBuffer[localOffset + d + 2] += weight * value[vOffset + d + 2];
956
+ this.outputBuffer[localOffset + d + 3] += weight * value[vOffset + d + 3];
957
+ this.outputBuffer[localOffset + d + 4] += weight * value[vOffset + d + 4];
958
+ this.outputBuffer[localOffset + d + 5] += weight * value[vOffset + d + 5];
959
+ this.outputBuffer[localOffset + d + 6] += weight * value[vOffset + d + 6];
960
+ this.outputBuffer[localOffset + d + 7] += weight * value[vOffset + d + 7];
961
+ }
962
+ for (let d = unrolledDim; d < dim; d++) {
963
+ this.outputBuffer[localOffset + d] += weight * value[vOffset + d];
964
+ }
965
+ }
966
+ // Update max and sum
967
+ this.maxBuffer[qi] = newMax;
968
+ this.sumBuffer[qi] = newSum;
969
+ }
970
+ }
971
+ /**
972
+ * Batch forward for processing multiple sequences
973
+ */
974
+ batchForward(queries, keys, values, seqLens, dim, numHeads = 8) {
975
+ const outputs = [];
976
+ const allScores = [];
977
+ for (let i = 0; i < queries.length; i++) {
978
+ const { output, attentionScores } = this.forward(queries[i], keys[i], values[i], seqLens[i], dim, numHeads);
979
+ outputs.push(output);
980
+ allScores.push(attentionScores);
981
+ }
982
+ return { outputs, attentionScores: allScores };
983
+ }
984
+ /**
985
+ * Backward pass for gradient computation
986
+ * Computes dQ, dK, dV from dOutput using the attention scores from forward pass
987
+ *
988
+ * Forward pass recap:
989
+ * S = Q @ K^T / sqrt(d) (scaled dot-product scores)
990
+ * A = softmax(S) (attention weights)
991
+ * O = A @ V (output)
992
+ *
993
+ * Backward pass:
994
+ * dV = A^T @ dO (gradient w.r.t. values)
995
+ * dA = dO @ V^T (gradient through output)
996
+ * dS = A * (dA - sum(dA * A, axis=-1, keepdims=True)) (softmax backward)
997
+ * dS = dS / sqrt(d) (apply scaling)
998
+ * dQ = dS @ K (gradient w.r.t. queries)
999
+ * dK = dS^T @ Q (gradient w.r.t. keys)
1000
+ */
1001
+ backward(dOutput, query, key, value, attentionScores, seqLen, dim) {
1002
+ // Validate inputs
1003
+ validateFloat32Array(dOutput, 'dOutput');
1004
+ validateFloat32Array(query, 'query');
1005
+ validateFloat32Array(key, 'key');
1006
+ validateFloat32Array(value, 'value');
1007
+ validateFloat32Array(attentionScores, 'attentionScores');
1008
+ validateSeqLength(seqLen, 'seqLen');
1009
+ validateDimension(dim, 'dim');
1010
+ const dQuery = globalBufferPool.acquire(seqLen * dim);
1011
+ const dKey = globalBufferPool.acquire(seqLen * dim);
1012
+ const dValue = globalBufferPool.acquire(seqLen * dim);
1013
+ // Initialize gradients to zero
1014
+ dQuery.fill(0);
1015
+ dKey.fill(0);
1016
+ dValue.fill(0);
1017
+ // Scaling factor (must match forward pass)
1018
+ const numHeads = 8; // Default from forward
1019
+ const headDim = dim / numHeads;
1020
+ const scale = 1.0 / Math.sqrt(headDim);
1021
+ // First: Normalize attention scores to get attention weights A
1022
+ // attentionScores contains the raw scores S, we need to apply softmax
1023
+ // In the forward pass, scores were stored after softmax, so use them directly
1024
+ // But the scores buffer may contain raw scores - we recompute softmax for numerical stability
1025
+ // Allocate temporary buffer for attention weights (normalized)
1026
+ const attentionWeights = globalBufferPool.acquire(seqLen * seqLen);
1027
+ // Apply softmax row-wise to get attention weights A
1028
+ for (let i = 0; i < seqLen; i++) {
1029
+ const rowOffset = i * seqLen;
1030
+ // Find max for numerical stability
1031
+ let maxScore = -Infinity;
1032
+ for (let j = 0; j < seqLen; j++) {
1033
+ const score = attentionScores[rowOffset + j];
1034
+ if (score > maxScore)
1035
+ maxScore = score;
1036
+ }
1037
+ // Compute exp and sum
1038
+ let sumExp = 0;
1039
+ for (let j = 0; j < seqLen; j++) {
1040
+ const expVal = Math.exp(attentionScores[rowOffset + j] - maxScore);
1041
+ attentionWeights[rowOffset + j] = expVal;
1042
+ sumExp += expVal;
1043
+ }
1044
+ // Normalize
1045
+ const invSum = 1.0 / (sumExp + EPSILON);
1046
+ for (let j = 0; j < seqLen; j++) {
1047
+ attentionWeights[rowOffset + j] *= invSum;
1048
+ }
1049
+ }
1050
+ // Step 1: Compute dV = A^T @ dO
1051
+ // dV[j, d] = sum_i A[i, j] * dO[i, d]
1052
+ for (let j = 0; j < seqLen; j++) {
1053
+ const dVOffset = j * dim;
1054
+ for (let i = 0; i < seqLen; i++) {
1055
+ const aWeight = attentionWeights[i * seqLen + j]; // A^T[j, i] = A[i, j]
1056
+ const dOOffset = i * dim;
1057
+ // 8x unrolled accumulation
1058
+ const unrolledDim = dim - (dim % 8);
1059
+ for (let d = 0; d < unrolledDim; d += 8) {
1060
+ dValue[dVOffset + d] += aWeight * dOutput[dOOffset + d];
1061
+ dValue[dVOffset + d + 1] += aWeight * dOutput[dOOffset + d + 1];
1062
+ dValue[dVOffset + d + 2] += aWeight * dOutput[dOOffset + d + 2];
1063
+ dValue[dVOffset + d + 3] += aWeight * dOutput[dOOffset + d + 3];
1064
+ dValue[dVOffset + d + 4] += aWeight * dOutput[dOOffset + d + 4];
1065
+ dValue[dVOffset + d + 5] += aWeight * dOutput[dOOffset + d + 5];
1066
+ dValue[dVOffset + d + 6] += aWeight * dOutput[dOOffset + d + 6];
1067
+ dValue[dVOffset + d + 7] += aWeight * dOutput[dOOffset + d + 7];
1068
+ }
1069
+ for (let d = unrolledDim; d < dim; d++) {
1070
+ dValue[dVOffset + d] += aWeight * dOutput[dOOffset + d];
1071
+ }
1072
+ }
1073
+ }
1074
+ // Step 2: Compute dA = dO @ V^T
1075
+ // dA[i, j] = sum_d dO[i, d] * V[j, d]
1076
+ const dAttention = globalBufferPool.acquire(seqLen * seqLen);
1077
+ dAttention.fill(0);
1078
+ for (let i = 0; i < seqLen; i++) {
1079
+ const dOOffset = i * dim;
1080
+ const dARowOffset = i * seqLen;
1081
+ for (let j = 0; j < seqLen; j++) {
1082
+ const vOffset = j * dim;
1083
+ let dotProduct = 0;
1084
+ // 8x unrolled dot product
1085
+ const unrolledDim = dim - (dim % 8);
1086
+ let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
1087
+ let sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
1088
+ for (let d = 0; d < unrolledDim; d += 8) {
1089
+ sum0 += dOutput[dOOffset + d] * value[vOffset + d];
1090
+ sum1 += dOutput[dOOffset + d + 1] * value[vOffset + d + 1];
1091
+ sum2 += dOutput[dOOffset + d + 2] * value[vOffset + d + 2];
1092
+ sum3 += dOutput[dOOffset + d + 3] * value[vOffset + d + 3];
1093
+ sum4 += dOutput[dOOffset + d + 4] * value[vOffset + d + 4];
1094
+ sum5 += dOutput[dOOffset + d + 5] * value[vOffset + d + 5];
1095
+ sum6 += dOutput[dOOffset + d + 6] * value[vOffset + d + 6];
1096
+ sum7 += dOutput[dOOffset + d + 7] * value[vOffset + d + 7];
1097
+ }
1098
+ dotProduct = (sum0 + sum1) + (sum2 + sum3) + (sum4 + sum5) + (sum6 + sum7);
1099
+ for (let d = unrolledDim; d < dim; d++) {
1100
+ dotProduct += dOutput[dOOffset + d] * value[vOffset + d];
1101
+ }
1102
+ dAttention[dARowOffset + j] = dotProduct;
1103
+ }
1104
+ }
1105
+ // Step 3: Compute dS = softmax_backward(dA, A)
1106
+ // For softmax: dS_ij = A_ij * (dA_ij - sum_k(A_ik * dA_ik))
1107
+ const dScores = globalBufferPool.acquire(seqLen * seqLen);
1108
+ for (let i = 0; i < seqLen; i++) {
1109
+ const rowOffset = i * seqLen;
1110
+ // Compute sum_k(A_ik * dA_ik) for this row
1111
+ let sumAdA = 0;
1112
+ for (let k = 0; k < seqLen; k++) {
1113
+ sumAdA += attentionWeights[rowOffset + k] * dAttention[rowOffset + k];
1114
+ }
1115
+ // Compute dS_ij = A_ij * (dA_ij - sumAdA) * scale
1116
+ for (let j = 0; j < seqLen; j++) {
1117
+ const idx = rowOffset + j;
1118
+ dScores[idx] = attentionWeights[idx] * (dAttention[idx] - sumAdA) * scale;
1119
+ }
1120
+ }
1121
+ // Step 4: Compute dQ = dS @ K
1122
+ // dQ[i, d] = sum_j dS[i, j] * K[j, d]
1123
+ for (let i = 0; i < seqLen; i++) {
1124
+ const dQOffset = i * dim;
1125
+ const dSRowOffset = i * seqLen;
1126
+ for (let j = 0; j < seqLen; j++) {
1127
+ const dSValue = dScores[dSRowOffset + j];
1128
+ const kOffset = j * dim;
1129
+ // 8x unrolled accumulation
1130
+ const unrolledDim = dim - (dim % 8);
1131
+ for (let d = 0; d < unrolledDim; d += 8) {
1132
+ dQuery[dQOffset + d] += dSValue * key[kOffset + d];
1133
+ dQuery[dQOffset + d + 1] += dSValue * key[kOffset + d + 1];
1134
+ dQuery[dQOffset + d + 2] += dSValue * key[kOffset + d + 2];
1135
+ dQuery[dQOffset + d + 3] += dSValue * key[kOffset + d + 3];
1136
+ dQuery[dQOffset + d + 4] += dSValue * key[kOffset + d + 4];
1137
+ dQuery[dQOffset + d + 5] += dSValue * key[kOffset + d + 5];
1138
+ dQuery[dQOffset + d + 6] += dSValue * key[kOffset + d + 6];
1139
+ dQuery[dQOffset + d + 7] += dSValue * key[kOffset + d + 7];
1140
+ }
1141
+ for (let d = unrolledDim; d < dim; d++) {
1142
+ dQuery[dQOffset + d] += dSValue * key[kOffset + d];
1143
+ }
1144
+ }
1145
+ }
1146
+ // Step 5: Compute dK = dS^T @ Q
1147
+ // dK[j, d] = sum_i dS[i, j] * Q[i, d]
1148
+ for (let j = 0; j < seqLen; j++) {
1149
+ const dKOffset = j * dim;
1150
+ for (let i = 0; i < seqLen; i++) {
1151
+ const dSValue = dScores[i * seqLen + j]; // dS^T[j, i] = dS[i, j]
1152
+ const qOffset = i * dim;
1153
+ // 8x unrolled accumulation
1154
+ const unrolledDim = dim - (dim % 8);
1155
+ for (let d = 0; d < unrolledDim; d += 8) {
1156
+ dKey[dKOffset + d] += dSValue * query[qOffset + d];
1157
+ dKey[dKOffset + d + 1] += dSValue * query[qOffset + d + 1];
1158
+ dKey[dKOffset + d + 2] += dSValue * query[qOffset + d + 2];
1159
+ dKey[dKOffset + d + 3] += dSValue * query[qOffset + d + 3];
1160
+ dKey[dKOffset + d + 4] += dSValue * query[qOffset + d + 4];
1161
+ dKey[dKOffset + d + 5] += dSValue * query[qOffset + d + 5];
1162
+ dKey[dKOffset + d + 6] += dSValue * query[qOffset + d + 6];
1163
+ dKey[dKOffset + d + 7] += dSValue * query[qOffset + d + 7];
1164
+ }
1165
+ for (let d = unrolledDim; d < dim; d++) {
1166
+ dKey[dKOffset + d] += dSValue * query[qOffset + d];
1167
+ }
1168
+ }
1169
+ }
1170
+ // Release temporary buffers
1171
+ globalBufferPool.release(attentionWeights);
1172
+ globalBufferPool.release(dAttention);
1173
+ globalBufferPool.release(dScores);
1174
+ return { dQuery, dKey, dValue };
1175
+ }
1176
+ /**
1177
+ * Release buffers back to pool
1178
+ */
1179
+ releaseBuffer(buffer) {
1180
+ globalBufferPool.release(buffer);
1181
+ }
1182
+ }
1183
+ // ============================================================================
1184
+ // Batch Multi-Head Attention
1185
+ // ============================================================================
1186
+ /**
1187
+ * Batch multi-head attention for processing multiple queries efficiently
1188
+ * Combines optimized MHA with batch processing
1189
+ */
1190
+ export function batchMultiHeadAttention(attention, queries, keys, values, masks) {
1191
+ return attention.batchForward(queries, keys, values, masks);
1192
+ }
1193
+ /**
1194
+ * Linear Attention (fallback)
1195
+ *
1196
+ * O(n) complexity approximation of attention
1197
+ */
1198
+ export class LinearAttention {
1199
+ hiddenDim;
1200
+ featureMap;
1201
+ constructor(config) {
1202
+ this.hiddenDim = config.hiddenDim;
1203
+ // ELU feature map
1204
+ this.featureMap = (x) => (x > 0 ? x : Math.exp(x) - 1);
1205
+ }
1206
+ forward(query, key, value) {
1207
+ const seqLen = query.length;
1208
+ const dim = value[0].length;
1209
+ // Apply feature map
1210
+ const queryMapped = query.map(q => q.map(this.featureMap));
1211
+ const keyMapped = key.map(k => k.map(this.featureMap));
1212
+ // Compute K^T V (dimension: [dim, valueDim])
1213
+ const ktv = Array.from({ length: this.hiddenDim }, () => Array(dim).fill(0));
1214
+ for (let i = 0; i < seqLen; i++) {
1215
+ for (let d1 = 0; d1 < this.hiddenDim; d1++) {
1216
+ for (let d2 = 0; d2 < dim; d2++) {
1217
+ ktv[d1][d2] += keyMapped[i][d1] * value[i][d2];
1218
+ }
1219
+ }
1220
+ }
1221
+ // Compute Q (K^T V)
1222
+ const output = [];
1223
+ for (let i = 0; i < seqLen; i++) {
1224
+ const row = [];
1225
+ for (let d2 = 0; d2 < dim; d2++) {
1226
+ let sum = 0;
1227
+ for (let d1 = 0; d1 < this.hiddenDim; d1++) {
1228
+ sum += queryMapped[i][d1] * ktv[d1][d2];
1229
+ }
1230
+ row.push(sum);
1231
+ }
1232
+ // Normalize
1233
+ const normSum = queryMapped[i].reduce((a, b) => a + b, 0);
1234
+ output.push(row.map(v => v / (normSum + 1e-8)));
1235
+ }
1236
+ return { output };
1237
+ }
1238
+ }
1239
+ /**
1240
+ * Hyperbolic Attention (simplified fallback)
1241
+ *
1242
+ * Approximation using hyperbolic geometry
1243
+ */
1244
+ export class HyperbolicAttention {
1245
+ hiddenDim;
1246
+ curvature;
1247
+ constructor(config) {
1248
+ this.hiddenDim = config.hiddenDim;
1249
+ this.curvature = -1.0; // Poincaré ball curvature
1250
+ }
1251
+ forward(query, key, value) {
1252
+ // Hyperbolic distance (simplified)
1253
+ const distance = this.hyperbolicDistance(query, key);
1254
+ // Attention weight based on hyperbolic distance
1255
+ const weight = Math.exp(-distance);
1256
+ // Weighted value
1257
+ const output = value.map(v => v * weight);
1258
+ return { output, distance };
1259
+ }
1260
+ hyperbolicDistance(a, b) {
1261
+ // Simplified hyperbolic distance in Poincare ball
1262
+ let normDiffSq = 0;
1263
+ for (let i = 0; i < a.length; i++) {
1264
+ const diff = a[i] - b[i];
1265
+ normDiffSq += diff * diff;
1266
+ }
1267
+ const normASq = a.reduce((sum, v) => sum + v * v, 0);
1268
+ const normBSq = b.reduce((sum, v) => sum + v * v, 0);
1269
+ const numerator = normDiffSq;
1270
+ const denominator = (1 - normASq) * (1 - normBSq);
1271
+ // Guard against division by zero and negative values
1272
+ if (denominator <= EPSILON) {
1273
+ return Infinity; // Points are on or outside the boundary
1274
+ }
1275
+ const acoshArg = 1 + (2 * numerator) / denominator;
1276
+ // Guard against invalid acosh argument (must be >= 1)
1277
+ if (!Number.isFinite(acoshArg) || acoshArg < 1) {
1278
+ return 0; // Return 0 for identical points or numerical issues
1279
+ }
1280
+ return Math.acosh(acoshArg);
1281
+ }
1282
+ }
1283
+ /**
1284
+ * MoE (Mixture of Experts) Attention (fallback)
1285
+ *
1286
+ * Routes to different expert attention modules
1287
+ */
1288
+ export class MoEAttention {
1289
+ experts;
1290
+ numExperts;
1291
+ gatingWeights;
1292
+ constructor(config) {
1293
+ this.numExperts = config.numExperts || 4;
1294
+ this.experts = Array.from({ length: this.numExperts }, () => new MultiHeadAttention(config));
1295
+ // Initialize gating network weights
1296
+ this.gatingWeights = Array.from({ length: this.numExperts }, () => Array.from({ length: config.hiddenDim }, () => (Math.random() - 0.5) * 0.1));
1297
+ }
1298
+ forward(query, key, value, topK = 2) {
1299
+ // Compute gating scores
1300
+ const gatingScores = this.gatingWeights.map(weights => {
1301
+ let score = 0;
1302
+ for (let i = 0; i < query.length; i++) {
1303
+ score += query[i] * weights[i];
1304
+ }
1305
+ return score;
1306
+ });
1307
+ // Softmax over top-K experts
1308
+ const expScores = gatingScores.map(s => Math.exp(s));
1309
+ const sumExp = expScores.reduce((a, b) => a + b, 0);
1310
+ const expertWeights = expScores.map(e => e / sumExp);
1311
+ // Get top-K experts
1312
+ const expertIndices = expertWeights
1313
+ .map((weight, idx) => ({ weight, idx }))
1314
+ .sort((a, b) => b.weight - a.weight)
1315
+ .slice(0, topK);
1316
+ // Weighted combination of expert outputs
1317
+ const output = new Array(query.length).fill(0);
1318
+ for (const { weight, idx } of expertIndices) {
1319
+ const expertOutput = this.experts[idx].forward(query, key, value).output;
1320
+ for (let i = 0; i < output.length; i++) {
1321
+ output[i] += weight * expertOutput[i];
1322
+ }
1323
+ }
1324
+ return { output, expertWeights };
1325
+ }
1326
+ }
1327
+ /**
1328
+ * Check if native attention is available
1329
+ */
1330
+ export function isNativeAttentionAvailable() {
1331
+ try {
1332
+ const attention = require('@ruvector/attention');
1333
+ // Try a simple operation
1334
+ const result = attention.flashAttention(new Float32Array([1, 0]), new Float32Array([1, 0]), new Float32Array([1, 0]), 1);
1335
+ return true;
1336
+ }
1337
+ catch {
1338
+ return false;
1339
+ }
1340
+ }
1341
+ /**
1342
+ * Factory function to create appropriate attention module
1343
+ */
1344
+ export function createAttention(type, config) {
1345
+ switch (type) {
1346
+ case 'multi-head':
1347
+ return new MultiHeadAttention(config);
1348
+ case 'flash':
1349
+ return new FlashAttention(config);
1350
+ case 'linear':
1351
+ return new LinearAttention(config);
1352
+ case 'hyperbolic':
1353
+ return new HyperbolicAttention(config);
1354
+ case 'moe':
1355
+ return new MoEAttention(config);
1356
+ default:
1357
+ return new MultiHeadAttention(config);
1358
+ }
1359
+ }
1360
+ /**
1361
+ * Factory function to create optimized attention modules
1362
+ */
1363
+ export function createAttentionOptimized(type, config) {
1364
+ switch (type) {
1365
+ case 'multi-head':
1366
+ return new MultiHeadAttentionOptimized(config);
1367
+ case 'flash':
1368
+ return new FlashAttentionOptimized(config);
1369
+ default:
1370
+ return new MultiHeadAttentionOptimized(config);
1371
+ }
1372
+ }
1373
+ // ============================================================================
1374
+ // Utility Functions for TypedArray Conversion
1375
+ // ============================================================================
1376
+ /**
1377
+ * Convert number[] to Float32Array
1378
+ */
1379
+ export function toFloat32Array(arr) {
1380
+ return new Float32Array(arr);
1381
+ }
1382
+ /**
1383
+ * Convert Float32Array to number[]
1384
+ */
1385
+ export function toNumberArray(arr) {
1386
+ return Array.from(arr);
1387
+ }
1388
+ /**
1389
+ * Convert 2D number[][] to flattened Float32Array
1390
+ */
1391
+ export function flatten2D(arr) {
1392
+ const totalLen = arr.reduce((sum, row) => sum + row.length, 0);
1393
+ const result = new Float32Array(totalLen);
1394
+ let offset = 0;
1395
+ for (const row of arr) {
1396
+ result.set(row, offset);
1397
+ offset += row.length;
1398
+ }
1399
+ return result;
1400
+ }
1401
+ /**
1402
+ * Unflatten Float32Array to 2D array
1403
+ */
1404
+ export function unflatten2D(arr, rows, cols) {
1405
+ const result = [];
1406
+ for (let i = 0; i < rows; i++) {
1407
+ result.push(Array.from(arr.slice(i * cols, (i + 1) * cols)));
1408
+ }
1409
+ return result;
1410
+ }
1411
+ // ============================================================================
1412
+ // Buffer Pool Export for External Memory Management
1413
+ // ============================================================================
1414
+ /**
1415
+ * Get the global buffer pool for external memory management
1416
+ */
1417
+ export function getBufferPool() {
1418
+ return {
1419
+ acquire: (size) => globalBufferPool.acquire(size),
1420
+ release: (buffer) => globalBufferPool.release(buffer),
1421
+ clear: () => globalBufferPool.clear(),
1422
+ };
1423
+ }
1424
+ // ============================================================================
1425
+ // Performance Benchmarking Utilities
1426
+ // ============================================================================
1427
+ /**
1428
+ * Get high-resolution time (cross-platform)
1429
+ */
1430
+ function getTime() {
1431
+ if (typeof performance !== 'undefined' && performance.now) {
1432
+ return performance.now();
1433
+ }
1434
+ return Date.now();
1435
+ }
1436
+ /**
1437
+ * Benchmark attention implementation
1438
+ */
1439
+ export function benchmarkAttention(attention, config) {
1440
+ const { seqLen, dim, iterations, numHeads = 8 } = config;
1441
+ // Create test data
1442
+ const query = new Float32Array(seqLen * dim);
1443
+ const key = new Float32Array(seqLen * dim);
1444
+ const value = new Float32Array(seqLen * dim);
1445
+ // Initialize with random values
1446
+ for (let i = 0; i < query.length; i++) {
1447
+ query[i] = Math.random() - 0.5;
1448
+ key[i] = Math.random() - 0.5;
1449
+ value[i] = Math.random() - 0.5;
1450
+ }
1451
+ // Warmup
1452
+ if (attention instanceof FlashAttentionOptimized) {
1453
+ attention.forward(query, key, value, seqLen, dim, numHeads);
1454
+ }
1455
+ else if (attention instanceof MultiHeadAttentionOptimized) {
1456
+ const q1 = query.slice(0, dim);
1457
+ const k1 = key.slice(0, dim);
1458
+ const v1 = value.slice(0, dim);
1459
+ attention.forward(q1, k1, v1);
1460
+ }
1461
+ // Benchmark
1462
+ const startTime = getTime();
1463
+ for (let i = 0; i < iterations; i++) {
1464
+ if (attention instanceof FlashAttentionOptimized) {
1465
+ const { output } = attention.forward(query, key, value, seqLen, dim, numHeads);
1466
+ attention.releaseBuffer(output);
1467
+ }
1468
+ else if (attention instanceof MultiHeadAttentionOptimized) {
1469
+ const q1 = query.slice(0, dim);
1470
+ const k1 = key.slice(0, dim);
1471
+ const v1 = value.slice(0, dim);
1472
+ const { output } = attention.forward(q1, k1, v1);
1473
+ attention.releaseBuffer(output);
1474
+ }
1475
+ }
1476
+ const endTime = getTime();
1477
+ const totalTimeMs = endTime - startTime;
1478
+ return {
1479
+ avgTimeMs: totalTimeMs / iterations,
1480
+ opsPerSecond: (iterations * 1000) / totalTimeMs,
1481
+ memoryUsed: query.byteLength + key.byteLength + value.byteLength,
1482
+ };
1483
+ }
1484
+ //# sourceMappingURL=attention-fallbacks.js.map