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.
- package/LICENSE +21 -0
- package/README.md +2821 -225
- package/dist/src/backends/VectorBackend.d.ts +31 -6
- package/dist/src/backends/VectorBackend.d.ts.map +1 -1
- package/dist/src/backends/detector.js.map +1 -1
- package/dist/src/backends/factory.d.ts +16 -33
- package/dist/src/backends/factory.d.ts.map +1 -1
- package/dist/src/backends/factory.js +138 -108
- package/dist/src/backends/factory.js.map +1 -1
- package/dist/src/backends/hnswlib/HNSWLibBackend.d.ts.map +1 -1
- package/dist/src/backends/hnswlib/HNSWLibBackend.js +29 -2
- package/dist/src/backends/hnswlib/HNSWLibBackend.js.map +1 -1
- package/dist/src/backends/ruvector/RuVectorBackend.d.ts +198 -45
- package/dist/src/backends/ruvector/RuVectorBackend.d.ts.map +1 -1
- package/dist/src/backends/ruvector/RuVectorBackend.js +612 -277
- package/dist/src/backends/ruvector/RuVectorBackend.js.map +1 -1
- package/dist/src/backends/ruvector/RuVectorLearning.d.ts +0 -4
- package/dist/src/backends/ruvector/RuVectorLearning.d.ts.map +1 -1
- package/dist/src/backends/ruvector/RuVectorLearning.js +0 -6
- package/dist/src/backends/ruvector/RuVectorLearning.js.map +1 -1
- package/dist/src/backends/rvf/AdaptiveIndexTuner.d.ts +175 -0
- package/dist/src/backends/rvf/AdaptiveIndexTuner.d.ts.map +1 -0
- package/dist/src/backends/rvf/AdaptiveIndexTuner.js +559 -0
- package/dist/src/backends/rvf/AdaptiveIndexTuner.js.map +1 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.d.ts +148 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.d.ts.map +1 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.js +438 -0
- package/dist/src/backends/rvf/ContrastiveTrainer.js.map +1 -0
- package/dist/src/backends/rvf/FederatedSessionManager.d.ts +211 -0
- package/dist/src/backends/rvf/FederatedSessionManager.d.ts.map +1 -0
- package/dist/src/backends/rvf/FederatedSessionManager.js +389 -0
- package/dist/src/backends/rvf/FederatedSessionManager.js.map +1 -0
- package/dist/src/backends/rvf/FilterBuilder.d.ts +136 -0
- package/dist/src/backends/rvf/FilterBuilder.d.ts.map +1 -0
- package/dist/src/backends/rvf/FilterBuilder.js +157 -0
- package/dist/src/backends/rvf/FilterBuilder.js.map +1 -0
- package/dist/src/backends/rvf/NativeAccelerator.d.ts +140 -0
- package/dist/src/backends/rvf/NativeAccelerator.d.ts.map +1 -0
- package/dist/src/backends/rvf/NativeAccelerator.js +630 -0
- package/dist/src/backends/rvf/NativeAccelerator.js.map +1 -0
- package/dist/src/backends/rvf/RvfBackend.d.ts +194 -0
- package/dist/src/backends/rvf/RvfBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/RvfBackend.js +607 -0
- package/dist/src/backends/rvf/RvfBackend.js.map +1 -0
- package/dist/src/backends/rvf/RvfSolver.d.ts +175 -0
- package/dist/src/backends/rvf/RvfSolver.d.ts.map +1 -0
- package/dist/src/backends/rvf/RvfSolver.js +176 -0
- package/dist/src/backends/rvf/RvfSolver.js.map +1 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.d.ts +143 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.js +569 -0
- package/dist/src/backends/rvf/SelfLearningRvfBackend.js.map +1 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.d.ts +128 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.d.ts.map +1 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.js +415 -0
- package/dist/src/backends/rvf/SemanticQueryRouter.js.map +1 -0
- package/dist/src/backends/rvf/SimdFallbacks.d.ts +25 -0
- package/dist/src/backends/rvf/SimdFallbacks.d.ts.map +1 -0
- package/dist/src/backends/rvf/SimdFallbacks.js +287 -0
- package/dist/src/backends/rvf/SimdFallbacks.js.map +1 -0
- package/dist/src/backends/rvf/SolverBandit.d.ts +100 -0
- package/dist/src/backends/rvf/SolverBandit.d.ts.map +1 -0
- package/dist/src/backends/rvf/SolverBandit.js +216 -0
- package/dist/src/backends/rvf/SolverBandit.js.map +1 -0
- package/dist/src/backends/rvf/SonaLearningBackend.d.ts +157 -0
- package/dist/src/backends/rvf/SonaLearningBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/SonaLearningBackend.js +283 -0
- package/dist/src/backends/rvf/SonaLearningBackend.js.map +1 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.d.ts +77 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.d.ts.map +1 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.js +382 -0
- package/dist/src/backends/rvf/SqlJsRvfBackend.js.map +1 -0
- package/dist/src/backends/rvf/WasmStoreBridge.d.ts +27 -0
- package/dist/src/backends/rvf/WasmStoreBridge.d.ts.map +1 -0
- package/dist/src/backends/rvf/WasmStoreBridge.js +87 -0
- package/dist/src/backends/rvf/WasmStoreBridge.js.map +1 -0
- package/dist/src/backends/rvf/validation.d.ts +20 -0
- package/dist/src/backends/rvf/validation.d.ts.map +1 -0
- package/dist/src/backends/rvf/validation.js +78 -0
- package/dist/src/backends/rvf/validation.js.map +1 -0
- package/dist/src/benchmark/BenchmarkSuite.d.ts +318 -0
- package/dist/src/benchmark/BenchmarkSuite.d.ts.map +1 -0
- package/dist/src/benchmark/BenchmarkSuite.js +984 -0
- package/dist/src/benchmark/BenchmarkSuite.js.map +1 -0
- package/dist/src/benchmark/index.d.ts +29 -0
- package/dist/src/benchmark/index.d.ts.map +1 -0
- package/dist/src/benchmark/index.js +39 -0
- package/dist/src/benchmark/index.js.map +1 -0
- package/dist/src/browser/AttentionBrowser.d.ts.map +1 -1
- package/dist/src/browser/AttentionBrowser.js +1 -0
- package/dist/src/browser/AttentionBrowser.js.map +1 -1
- package/dist/src/cli/agentdb-cli.d.ts +13 -0
- package/dist/src/cli/agentdb-cli.d.ts.map +1 -1
- package/dist/src/cli/agentdb-cli.js +803 -94
- package/dist/src/cli/agentdb-cli.js.map +1 -1
- package/dist/src/cli/commands/hyperbolic.d.ts +11 -0
- package/dist/src/cli/commands/hyperbolic.d.ts.map +1 -0
- package/dist/src/cli/commands/hyperbolic.js +428 -0
- package/dist/src/cli/commands/hyperbolic.js.map +1 -0
- package/dist/src/cli/commands/learn.d.ts +11 -0
- package/dist/src/cli/commands/learn.d.ts.map +1 -0
- package/dist/src/cli/commands/learn.js +404 -0
- package/dist/src/cli/commands/learn.js.map +1 -0
- package/dist/src/cli/commands/migrate.d.ts +15 -0
- package/dist/src/cli/commands/migrate.d.ts.map +1 -1
- package/dist/src/cli/commands/migrate.js +176 -8
- package/dist/src/cli/commands/migrate.js.map +1 -1
- package/dist/src/cli/commands/route.d.ts +11 -0
- package/dist/src/cli/commands/route.d.ts.map +1 -0
- package/dist/src/cli/commands/route.js +414 -0
- package/dist/src/cli/commands/route.js.map +1 -0
- package/dist/src/cli/commands/rvf.d.ts +21 -0
- package/dist/src/cli/commands/rvf.d.ts.map +1 -0
- package/dist/src/cli/commands/rvf.js +448 -0
- package/dist/src/cli/commands/rvf.js.map +1 -0
- package/dist/src/cli/lib/report-store.d.ts +2 -1
- package/dist/src/cli/lib/report-store.d.ts.map +1 -1
- package/dist/src/cli/lib/report-store.js +85 -65
- package/dist/src/cli/lib/report-store.js.map +1 -1
- package/dist/src/cli/lib/simulation-runner.d.ts +40 -3
- package/dist/src/cli/lib/simulation-runner.d.ts.map +1 -1
- package/dist/src/cli/lib/simulation-runner.js +248 -6
- package/dist/src/cli/lib/simulation-runner.js.map +1 -1
- package/dist/src/compatibility/DeprecationWarnings.d.ts +54 -0
- package/dist/src/compatibility/DeprecationWarnings.d.ts.map +1 -0
- package/dist/src/compatibility/DeprecationWarnings.js +131 -0
- package/dist/src/compatibility/DeprecationWarnings.js.map +1 -0
- package/dist/src/compatibility/MigrationUtilities.d.ts +30 -0
- package/dist/src/compatibility/MigrationUtilities.d.ts.map +1 -0
- package/dist/src/compatibility/MigrationUtilities.js +184 -0
- package/dist/src/compatibility/MigrationUtilities.js.map +1 -0
- package/dist/src/compatibility/V1toV2Adapter.d.ts +76 -0
- package/dist/src/compatibility/V1toV2Adapter.d.ts.map +1 -0
- package/dist/src/compatibility/V1toV2Adapter.js +185 -0
- package/dist/src/compatibility/V1toV2Adapter.js.map +1 -0
- package/dist/src/compatibility/VersionDetector.d.ts +32 -0
- package/dist/src/compatibility/VersionDetector.d.ts.map +1 -0
- package/dist/src/compatibility/VersionDetector.js +144 -0
- package/dist/src/compatibility/VersionDetector.js.map +1 -0
- package/dist/src/compatibility/index.d.ts +14 -0
- package/dist/src/compatibility/index.d.ts.map +1 -0
- package/dist/src/compatibility/index.js +21 -0
- package/dist/src/compatibility/index.js.map +1 -0
- package/dist/src/compatibility/types.d.ts +103 -0
- package/dist/src/compatibility/types.d.ts.map +1 -0
- package/dist/src/compatibility/types.js +5 -0
- package/dist/src/compatibility/types.js.map +1 -0
- package/dist/src/controllers/AttentionService.d.ts +99 -202
- package/dist/src/controllers/AttentionService.d.ts.map +1 -1
- package/dist/src/controllers/AttentionService.js +450 -558
- package/dist/src/controllers/AttentionService.js.map +1 -1
- package/dist/src/controllers/CausalMemoryGraph.d.ts +30 -5
- package/dist/src/controllers/CausalMemoryGraph.d.ts.map +1 -1
- package/dist/src/controllers/CausalMemoryGraph.js +89 -13
- package/dist/src/controllers/CausalMemoryGraph.js.map +1 -1
- package/dist/src/controllers/CausalRecall.d.ts +4 -0
- package/dist/src/controllers/CausalRecall.d.ts.map +1 -1
- package/dist/src/controllers/CausalRecall.js +19 -2
- package/dist/src/controllers/CausalRecall.js.map +1 -1
- package/dist/src/controllers/EmbeddingService.d.ts.map +1 -1
- package/dist/src/controllers/EmbeddingService.js +28 -11
- package/dist/src/controllers/EmbeddingService.js.map +1 -1
- package/dist/src/controllers/EnhancedEmbeddingService.d.ts +4 -0
- package/dist/src/controllers/EnhancedEmbeddingService.d.ts.map +1 -1
- package/dist/src/controllers/EnhancedEmbeddingService.js +17 -3
- package/dist/src/controllers/EnhancedEmbeddingService.js.map +1 -1
- package/dist/src/controllers/ExplainableRecall.d.ts +1 -1
- package/dist/src/controllers/ExplainableRecall.d.ts.map +1 -1
- package/dist/src/controllers/ExplainableRecall.js +1 -1
- package/dist/src/controllers/ExplainableRecall.js.map +1 -1
- package/dist/src/controllers/HNSWIndex.d.ts +20 -2
- package/dist/src/controllers/HNSWIndex.d.ts.map +1 -1
- package/dist/src/controllers/HNSWIndex.js +82 -6
- package/dist/src/controllers/HNSWIndex.js.map +1 -1
- package/dist/src/controllers/LearningSystem.d.ts +1 -17
- package/dist/src/controllers/LearningSystem.d.ts.map +1 -1
- package/dist/src/controllers/LearningSystem.js +18 -135
- package/dist/src/controllers/LearningSystem.js.map +1 -1
- package/dist/src/controllers/MemoryController.d.ts +161 -0
- package/dist/src/controllers/MemoryController.d.ts.map +1 -0
- package/dist/src/controllers/MemoryController.js +303 -0
- package/dist/src/controllers/MemoryController.js.map +1 -0
- package/dist/src/controllers/MincutService.d.ts +92 -0
- package/dist/src/controllers/MincutService.d.ts.map +1 -0
- package/dist/src/controllers/MincutService.js +336 -0
- package/dist/src/controllers/MincutService.js.map +1 -0
- package/dist/src/controllers/NightlyLearner.d.ts +5 -1
- package/dist/src/controllers/NightlyLearner.d.ts.map +1 -1
- package/dist/src/controllers/NightlyLearner.js +17 -3
- package/dist/src/controllers/NightlyLearner.js.map +1 -1
- package/dist/src/controllers/QUICClient.d.ts +45 -0
- package/dist/src/controllers/QUICClient.d.ts.map +1 -1
- package/dist/src/controllers/QUICClient.js +190 -0
- package/dist/src/controllers/QUICClient.js.map +1 -1
- package/dist/src/controllers/ReasoningBank.d.ts +6 -3
- package/dist/src/controllers/ReasoningBank.d.ts.map +1 -1
- package/dist/src/controllers/ReasoningBank.js +47 -41
- package/dist/src/controllers/ReasoningBank.js.map +1 -1
- package/dist/src/controllers/ReflexionMemory.d.ts +115 -6
- package/dist/src/controllers/ReflexionMemory.d.ts.map +1 -1
- package/dist/src/controllers/ReflexionMemory.js +541 -213
- package/dist/src/controllers/ReflexionMemory.js.map +1 -1
- package/dist/src/controllers/SkillLibrary.d.ts +27 -3
- package/dist/src/controllers/SkillLibrary.d.ts.map +1 -1
- package/dist/src/controllers/SkillLibrary.js +187 -91
- package/dist/src/controllers/SkillLibrary.js.map +1 -1
- package/dist/src/controllers/SparsificationService.d.ts +151 -0
- package/dist/src/controllers/SparsificationService.d.ts.map +1 -0
- package/dist/src/controllers/SparsificationService.js +375 -0
- package/dist/src/controllers/SparsificationService.js.map +1 -0
- package/dist/src/controllers/SyncCoordinator.d.ts.map +1 -1
- package/dist/src/controllers/SyncCoordinator.js +115 -3
- package/dist/src/controllers/SyncCoordinator.js.map +1 -1
- package/dist/src/controllers/WASMVectorSearch.d.ts +12 -30
- package/dist/src/controllers/WASMVectorSearch.d.ts.map +1 -1
- package/dist/src/controllers/WASMVectorSearch.js +146 -93
- package/dist/src/controllers/WASMVectorSearch.js.map +1 -1
- package/dist/src/controllers/attention/AttentionCache.d.ts +38 -0
- package/dist/src/controllers/attention/AttentionCache.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionCache.js +80 -0
- package/dist/src/controllers/attention/AttentionCache.js.map +1 -0
- package/dist/src/controllers/attention/AttentionConfig.d.ts +127 -0
- package/dist/src/controllers/attention/AttentionConfig.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionConfig.js +124 -0
- package/dist/src/controllers/attention/AttentionConfig.js.map +1 -0
- package/dist/src/controllers/attention/AttentionCore.d.ts +89 -0
- package/dist/src/controllers/attention/AttentionCore.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionCore.js +289 -0
- package/dist/src/controllers/attention/AttentionCore.js.map +1 -0
- package/dist/src/controllers/attention/AttentionHelpers.d.ts +55 -0
- package/dist/src/controllers/attention/AttentionHelpers.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionHelpers.js +148 -0
- package/dist/src/controllers/attention/AttentionHelpers.js.map +1 -0
- package/dist/src/controllers/attention/AttentionMetrics.d.ts +55 -0
- package/dist/src/controllers/attention/AttentionMetrics.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionMetrics.js +72 -0
- package/dist/src/controllers/attention/AttentionMetrics.js.map +1 -0
- package/dist/src/controllers/attention/AttentionWASM.d.ts +98 -0
- package/dist/src/controllers/attention/AttentionWASM.d.ts.map +1 -0
- package/dist/src/controllers/attention/AttentionWASM.js +147 -0
- package/dist/src/controllers/attention/AttentionWASM.js.map +1 -0
- package/dist/src/controllers/attention/CrossAttentionController.d.ts +141 -0
- package/dist/src/controllers/attention/CrossAttentionController.d.ts.map +1 -0
- package/dist/src/controllers/attention/CrossAttentionController.js +325 -0
- package/dist/src/controllers/attention/CrossAttentionController.js.map +1 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.d.ts +137 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.d.ts.map +1 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.js +336 -0
- package/dist/src/controllers/attention/MultiHeadAttentionController.js.map +1 -0
- package/dist/src/controllers/attention/SelfAttentionController.d.ts +117 -0
- package/dist/src/controllers/attention/SelfAttentionController.d.ts.map +1 -0
- package/dist/src/controllers/attention/SelfAttentionController.js +205 -0
- package/dist/src/controllers/attention/SelfAttentionController.js.map +1 -0
- package/dist/src/controllers/attention/index.d.ts +24 -0
- package/dist/src/controllers/attention/index.d.ts.map +1 -0
- package/dist/src/controllers/attention/index.js +20 -0
- package/dist/src/controllers/attention/index.js.map +1 -0
- package/dist/src/controllers/index.d.ts +14 -27
- package/dist/src/controllers/index.d.ts.map +1 -1
- package/dist/src/controllers/index.js +10 -18
- package/dist/src/controllers/index.js.map +1 -1
- package/dist/src/controllers/prerequisites.d.ts +76 -0
- package/dist/src/controllers/prerequisites.d.ts.map +1 -0
- package/dist/src/controllers/prerequisites.js +235 -0
- package/dist/src/controllers/prerequisites.js.map +1 -0
- package/dist/src/core/AgentDB.d.ts +27 -19
- package/dist/src/core/AgentDB.d.ts.map +1 -1
- package/dist/src/core/AgentDB.js +76 -124
- package/dist/src/core/AgentDB.js.map +1 -1
- package/dist/src/core/QueryCache.d.ts +138 -0
- package/dist/src/core/QueryCache.d.ts.map +1 -0
- package/dist/src/core/QueryCache.js +309 -0
- package/dist/src/core/QueryCache.js.map +1 -0
- package/dist/src/db-fallback.d.ts +10 -1
- package/dist/src/db-fallback.d.ts.map +1 -1
- package/dist/src/db-fallback.js +205 -28
- package/dist/src/db-fallback.js.map +1 -1
- package/dist/src/examples/quic-sync-example.js +2 -2
- package/dist/src/examples/quic-sync-example.js.map +1 -1
- package/dist/src/index.d.ts +15 -15
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +39 -11
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/agentdb-mcp-server.js +47 -2
- package/dist/src/mcp/agentdb-mcp-server.js.map +1 -1
- package/dist/src/model/ModelCacheLoader.d.ts +24 -0
- package/dist/src/model/ModelCacheLoader.d.ts.map +1 -0
- package/dist/src/model/ModelCacheLoader.js +121 -0
- package/dist/src/model/ModelCacheLoader.js.map +1 -0
- package/dist/src/observability/index.d.ts +8 -0
- package/dist/src/observability/index.d.ts.map +1 -0
- package/dist/src/observability/index.js +8 -0
- package/dist/src/observability/index.js.map +1 -0
- package/dist/src/observability/integration.d.ts +32 -0
- package/dist/src/observability/integration.d.ts.map +1 -0
- package/dist/src/observability/integration.js +159 -0
- package/dist/src/observability/integration.js.map +1 -0
- package/dist/src/observability/telemetry.d.ts +137 -0
- package/dist/src/observability/telemetry.d.ts.map +1 -0
- package/dist/src/observability/telemetry.js +416 -0
- package/dist/src/observability/telemetry.js.map +1 -0
- package/dist/src/optimizations/BatchOperations.d.ts +47 -0
- package/dist/src/optimizations/BatchOperations.d.ts.map +1 -1
- package/dist/src/optimizations/BatchOperations.js +183 -31
- package/dist/src/optimizations/BatchOperations.js.map +1 -1
- package/dist/src/optimizations/Quantization.d.ts +330 -0
- package/dist/src/optimizations/Quantization.d.ts.map +1 -0
- package/dist/src/optimizations/Quantization.js +793 -0
- package/dist/src/optimizations/Quantization.js.map +1 -0
- package/dist/src/optimizations/index.d.ts +1 -3
- package/dist/src/optimizations/index.d.ts.map +1 -1
- package/dist/src/optimizations/index.js +0 -1
- package/dist/src/optimizations/index.js.map +1 -1
- package/dist/src/quantization/index.d.ts +7 -0
- package/dist/src/quantization/index.d.ts.map +1 -0
- package/dist/src/quantization/index.js +15 -0
- package/dist/src/quantization/index.js.map +1 -0
- package/dist/src/quantization/vector-quantization.d.ts +388 -0
- package/dist/src/quantization/vector-quantization.d.ts.map +1 -0
- package/dist/src/quantization/vector-quantization.js +1132 -0
- package/dist/src/quantization/vector-quantization.js.map +1 -0
- package/dist/src/search/HybridSearch.d.ts +311 -0
- package/dist/src/search/HybridSearch.d.ts.map +1 -0
- package/dist/src/search/HybridSearch.js +712 -0
- package/dist/src/search/HybridSearch.js.map +1 -0
- package/dist/src/search/index.d.ts +12 -0
- package/dist/src/search/index.d.ts.map +1 -0
- package/dist/src/search/index.js +16 -0
- package/dist/src/search/index.js.map +1 -0
- package/dist/src/services/AttentionService.d.ts +303 -0
- package/dist/src/services/AttentionService.d.ts.map +1 -0
- package/dist/src/services/AttentionService.js +1165 -0
- package/dist/src/services/AttentionService.js.map +1 -0
- package/dist/src/services/LLMRouter.d.ts +61 -7
- package/dist/src/services/LLMRouter.d.ts.map +1 -1
- package/dist/src/services/LLMRouter.js +286 -22
- package/dist/src/services/LLMRouter.js.map +1 -1
- package/dist/src/services/audit-logger.service.d.ts +168 -0
- package/dist/src/services/audit-logger.service.d.ts.map +1 -0
- package/dist/src/services/audit-logger.service.js +331 -0
- package/dist/src/services/audit-logger.service.js.map +1 -0
- package/dist/src/services/enhanced-embeddings.d.ts +271 -0
- package/dist/src/services/enhanced-embeddings.d.ts.map +1 -0
- package/dist/src/services/enhanced-embeddings.js +1035 -0
- package/dist/src/services/enhanced-embeddings.js.map +1 -0
- package/dist/src/services/federated-learning.d.ts +172 -0
- package/dist/src/services/federated-learning.d.ts.map +1 -0
- package/dist/src/services/federated-learning.js +342 -0
- package/dist/src/services/federated-learning.js.map +1 -0
- package/dist/src/simd/index.d.ts +9 -0
- package/dist/src/simd/index.d.ts.map +1 -0
- package/dist/src/simd/index.js +27 -0
- package/dist/src/simd/index.js.map +1 -0
- package/dist/src/simd/simd-vector-ops.d.ts +332 -0
- package/dist/src/simd/simd-vector-ops.d.ts.map +1 -0
- package/dist/src/simd/simd-vector-ops.js +945 -0
- package/dist/src/simd/simd-vector-ops.js.map +1 -0
- package/dist/src/types/database.types.d.ts +275 -0
- package/dist/src/types/database.types.d.ts.map +1 -0
- package/dist/src/types/database.types.js +55 -0
- package/dist/src/types/database.types.js.map +1 -0
- package/dist/src/types/graph.d.ts +29 -0
- package/dist/src/types/graph.d.ts.map +1 -0
- package/dist/src/types/graph.js +8 -0
- package/dist/src/types/graph.js.map +1 -0
- package/dist/src/utils/chalk-fallback.d.ts +3 -0
- package/dist/src/utils/chalk-fallback.d.ts.map +1 -0
- package/dist/src/utils/chalk-fallback.js +20 -0
- package/dist/src/utils/chalk-fallback.js.map +1 -0
- package/dist/src/utils/similarity.d.ts +11 -0
- package/dist/src/utils/similarity.d.ts.map +1 -0
- package/dist/src/utils/similarity.js +25 -0
- package/dist/src/utils/similarity.js.map +1 -0
- package/dist/src/wasm-loader.d.ts +66 -0
- package/dist/src/wasm-loader.d.ts.map +1 -0
- package/dist/src/wasm-loader.js +76 -0
- package/dist/src/wasm-loader.js.map +1 -0
- package/dist/src/wrappers/agentdb-fast.d.ts +139 -0
- package/dist/src/wrappers/agentdb-fast.d.ts.map +1 -0
- package/dist/src/wrappers/agentdb-fast.js +307 -0
- package/dist/src/wrappers/agentdb-fast.js.map +1 -0
- package/dist/src/wrappers/attention-fallbacks.d.ts +343 -0
- package/dist/src/wrappers/attention-fallbacks.d.ts.map +1 -0
- package/dist/src/wrappers/attention-fallbacks.js +1484 -0
- package/dist/src/wrappers/attention-fallbacks.js.map +1 -0
- package/dist/src/wrappers/attention-native.d.ts +111 -0
- package/dist/src/wrappers/attention-native.d.ts.map +1 -0
- package/dist/src/wrappers/attention-native.js +273 -0
- package/dist/src/wrappers/attention-native.js.map +1 -0
- package/dist/src/wrappers/embedding-service.d.ts +117 -0
- package/dist/src/wrappers/embedding-service.d.ts.map +1 -0
- package/dist/src/wrappers/embedding-service.js +370 -0
- package/dist/src/wrappers/embedding-service.js.map +1 -0
- package/dist/src/wrappers/gnn-wrapper.d.ts +71 -0
- package/dist/src/wrappers/gnn-wrapper.d.ts.map +1 -0
- package/dist/src/wrappers/gnn-wrapper.js +236 -0
- package/dist/src/wrappers/gnn-wrapper.js.map +1 -0
- package/dist/src/wrappers/index.d.ts +48 -0
- package/dist/src/wrappers/index.d.ts.map +1 -0
- package/dist/src/wrappers/index.js +83 -0
- package/dist/src/wrappers/index.js.map +1 -0
- package/package.json +77 -76
- package/scripts/postinstall.cjs +13 -144
- package/dist/agentdb.browser.js +0 -1685
- package/dist/agentdb.browser.js.map +0 -7
- package/dist/agentdb.browser.min.js +0 -3
- package/dist/agentdb.browser.min.js.map +0 -7
- package/dist/agentdb.wasm-loader.js +0 -207
- package/dist/backends/VectorBackend.d.ts +0 -120
- package/dist/backends/VectorBackend.js +0 -14
- package/dist/backends/VectorBackend.js.map +0 -1
- package/dist/optimizations/RVFOptimizer.d.ts +0 -225
- package/dist/optimizations/RVFOptimizer.js +0 -541
- package/dist/optimizations/RVFOptimizer.js.map +0 -1
- package/dist/schemas/frontier-schema.sql +0 -378
- package/dist/schemas/schema.sql +0 -382
- package/dist/simulation/cli.d.ts +0 -9
- package/dist/simulation/cli.d.ts.map +0 -1
- package/dist/simulation/cli.js +0 -68
- package/dist/simulation/cli.js.map +0 -1
- package/dist/simulation/runner.d.ts +0 -21
- package/dist/simulation/runner.d.ts.map +0 -1
- package/dist/simulation/runner.js +0 -241
- package/dist/simulation/runner.js.map +0 -1
- package/dist/simulation/scenarios/aidefence-integration.d.ts +0 -24
- package/dist/simulation/scenarios/aidefence-integration.d.ts.map +0 -1
- package/dist/simulation/scenarios/aidefence-integration.js +0 -124
- package/dist/simulation/scenarios/aidefence-integration.js.map +0 -1
- package/dist/simulation/scenarios/bmssp-integration.d.ts +0 -23
- package/dist/simulation/scenarios/bmssp-integration.d.ts.map +0 -1
- package/dist/simulation/scenarios/bmssp-integration.js +0 -104
- package/dist/simulation/scenarios/bmssp-integration.js.map +0 -1
- package/dist/simulation/scenarios/causal-reasoning.d.ts +0 -16
- package/dist/simulation/scenarios/causal-reasoning.d.ts.map +0 -1
- package/dist/simulation/scenarios/causal-reasoning.js +0 -108
- package/dist/simulation/scenarios/causal-reasoning.js.map +0 -1
- package/dist/simulation/scenarios/consciousness-explorer.d.ts +0 -25
- package/dist/simulation/scenarios/consciousness-explorer.d.ts.map +0 -1
- package/dist/simulation/scenarios/consciousness-explorer.js +0 -108
- package/dist/simulation/scenarios/consciousness-explorer.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.d.ts +0 -135
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.js +0 -158
- package/dist/simulation/scenarios/domain-examples/e-commerce-recommendations.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/index.d.ts +0 -14
- package/dist/simulation/scenarios/domain-examples/index.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/index.js +0 -14
- package/dist/simulation/scenarios/domain-examples/index.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.d.ts +0 -178
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.js +0 -218
- package/dist/simulation/scenarios/domain-examples/iot-sensor-networks.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/medical-imaging.d.ts +0 -122
- package/dist/simulation/scenarios/domain-examples/medical-imaging.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/medical-imaging.js +0 -129
- package/dist/simulation/scenarios/domain-examples/medical-imaging.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.d.ts +0 -166
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.js +0 -161
- package/dist/simulation/scenarios/domain-examples/robotics-navigation.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/scientific-research.d.ts +0 -151
- package/dist/simulation/scenarios/domain-examples/scientific-research.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/scientific-research.js +0 -187
- package/dist/simulation/scenarios/domain-examples/scientific-research.js.map +0 -1
- package/dist/simulation/scenarios/domain-examples/trading-systems.d.ts +0 -102
- package/dist/simulation/scenarios/domain-examples/trading-systems.d.ts.map +0 -1
- package/dist/simulation/scenarios/domain-examples/trading-systems.js +0 -100
- package/dist/simulation/scenarios/domain-examples/trading-systems.js.map +0 -1
- package/dist/simulation/scenarios/goalie-integration.d.ts +0 -24
- package/dist/simulation/scenarios/goalie-integration.d.ts.map +0 -1
- package/dist/simulation/scenarios/goalie-integration.js +0 -121
- package/dist/simulation/scenarios/goalie-integration.js.map +0 -1
- package/dist/simulation/scenarios/graph-traversal.d.ts +0 -17
- package/dist/simulation/scenarios/graph-traversal.d.ts.map +0 -1
- package/dist/simulation/scenarios/graph-traversal.js +0 -101
- package/dist/simulation/scenarios/graph-traversal.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/attention-analysis.d.ts +0 -45
- package/dist/simulation/scenarios/latent-space/attention-analysis.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/attention-analysis.js +0 -435
- package/dist/simulation/scenarios/latent-space/attention-analysis.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/clustering-analysis.d.ts +0 -55
- package/dist/simulation/scenarios/latent-space/clustering-analysis.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/clustering-analysis.js +0 -606
- package/dist/simulation/scenarios/latent-space/clustering-analysis.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.d.ts +0 -74
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.js +0 -391
- package/dist/simulation/scenarios/latent-space/hnsw-exploration.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.d.ts +0 -51
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.js +0 -536
- package/dist/simulation/scenarios/latent-space/hypergraph-exploration.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/index.d.ts +0 -35
- package/dist/simulation/scenarios/latent-space/index.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/index.js +0 -35
- package/dist/simulation/scenarios/latent-space/index.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/neural-augmentation.d.ts +0 -52
- package/dist/simulation/scenarios/latent-space/neural-augmentation.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/neural-augmentation.js +0 -472
- package/dist/simulation/scenarios/latent-space/neural-augmentation.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.d.ts +0 -53
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.js +0 -391
- package/dist/simulation/scenarios/latent-space/quantum-hybrid.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.d.ts +0 -52
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.js +0 -514
- package/dist/simulation/scenarios/latent-space/self-organizing-hnsw.js.map +0 -1
- package/dist/simulation/scenarios/latent-space/traversal-optimization.d.ts +0 -53
- package/dist/simulation/scenarios/latent-space/traversal-optimization.d.ts.map +0 -1
- package/dist/simulation/scenarios/latent-space/traversal-optimization.js +0 -595
- package/dist/simulation/scenarios/latent-space/traversal-optimization.js.map +0 -1
- package/dist/simulation/scenarios/lean-agentic-swarm.d.ts +0 -18
- package/dist/simulation/scenarios/lean-agentic-swarm.d.ts.map +0 -1
- package/dist/simulation/scenarios/lean-agentic-swarm.js +0 -132
- package/dist/simulation/scenarios/lean-agentic-swarm.js.map +0 -1
- package/dist/simulation/scenarios/multi-agent-swarm.d.ts +0 -17
- package/dist/simulation/scenarios/multi-agent-swarm.d.ts.map +0 -1
- package/dist/simulation/scenarios/multi-agent-swarm.js +0 -111
- package/dist/simulation/scenarios/multi-agent-swarm.js.map +0 -1
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.d.ts +0 -23
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.d.ts.map +0 -1
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.js +0 -98
- package/dist/simulation/scenarios/psycho-symbolic-reasoner.js.map +0 -1
- package/dist/simulation/scenarios/reflexion-learning.d.ts +0 -16
- package/dist/simulation/scenarios/reflexion-learning.d.ts.map +0 -1
- package/dist/simulation/scenarios/reflexion-learning.js +0 -98
- package/dist/simulation/scenarios/reflexion-learning.js.map +0 -1
- package/dist/simulation/scenarios/research-swarm.d.ts +0 -24
- package/dist/simulation/scenarios/research-swarm.d.ts.map +0 -1
- package/dist/simulation/scenarios/research-swarm.js +0 -146
- package/dist/simulation/scenarios/research-swarm.js.map +0 -1
- package/dist/simulation/scenarios/skill-evolution.d.ts +0 -16
- package/dist/simulation/scenarios/skill-evolution.d.ts.map +0 -1
- package/dist/simulation/scenarios/skill-evolution.js +0 -109
- package/dist/simulation/scenarios/skill-evolution.js.map +0 -1
- package/dist/simulation/scenarios/stock-market-emergence.d.ts +0 -33
- package/dist/simulation/scenarios/stock-market-emergence.d.ts.map +0 -1
- package/dist/simulation/scenarios/stock-market-emergence.js +0 -246
- package/dist/simulation/scenarios/stock-market-emergence.js.map +0 -1
- package/dist/simulation/scenarios/strange-loops.d.ts +0 -18
- package/dist/simulation/scenarios/strange-loops.d.ts.map +0 -1
- package/dist/simulation/scenarios/strange-loops.js +0 -133
- package/dist/simulation/scenarios/strange-loops.js.map +0 -1
- package/dist/simulation/scenarios/sublinear-solver.d.ts +0 -22
- package/dist/simulation/scenarios/sublinear-solver.d.ts.map +0 -1
- package/dist/simulation/scenarios/sublinear-solver.js +0 -82
- package/dist/simulation/scenarios/sublinear-solver.js.map +0 -1
- package/dist/simulation/scenarios/temporal-lead-solver.d.ts +0 -23
- package/dist/simulation/scenarios/temporal-lead-solver.d.ts.map +0 -1
- package/dist/simulation/scenarios/temporal-lead-solver.js +0 -90
- package/dist/simulation/scenarios/temporal-lead-solver.js.map +0 -1
- package/dist/simulation/scenarios/voting-system-consensus.d.ts +0 -27
- package/dist/simulation/scenarios/voting-system-consensus.d.ts.map +0 -1
- package/dist/simulation/scenarios/voting-system-consensus.js +0 -175
- package/dist/simulation/scenarios/voting-system-consensus.js.map +0 -1
- package/dist/simulation/types.d.ts +0 -162
- package/dist/simulation/types.d.ts.map +0 -1
- package/dist/simulation/types.js +0 -5
- package/dist/simulation/types.js.map +0 -1
- package/dist/simulation/utils/PerformanceOptimizer.d.ts +0 -115
- package/dist/simulation/utils/PerformanceOptimizer.d.ts.map +0 -1
- package/dist/simulation/utils/PerformanceOptimizer.js +0 -223
- package/dist/simulation/utils/PerformanceOptimizer.js.map +0 -1
- package/dist/src/backends/index.cjs +0 -6
- package/dist/src/backends/ruvector/GuardedVectorBackend.d.ts +0 -93
- package/dist/src/backends/ruvector/GuardedVectorBackend.d.ts.map +0 -1
- package/dist/src/backends/ruvector/GuardedVectorBackend.js +0 -182
- package/dist/src/backends/ruvector/GuardedVectorBackend.js.map +0 -1
- package/dist/src/consensus/RaftConsensus.d.ts +0 -220
- package/dist/src/consensus/RaftConsensus.d.ts.map +0 -1
- package/dist/src/consensus/RaftConsensus.js +0 -762
- package/dist/src/consensus/RaftConsensus.js.map +0 -1
- package/dist/src/controllers/HierarchicalMemory.d.ts +0 -197
- package/dist/src/controllers/HierarchicalMemory.d.ts.map +0 -1
- package/dist/src/controllers/HierarchicalMemory.js +0 -519
- package/dist/src/controllers/HierarchicalMemory.js.map +0 -1
- package/dist/src/controllers/MemoryConsolidation.d.ts +0 -142
- package/dist/src/controllers/MemoryConsolidation.d.ts.map +0 -1
- package/dist/src/controllers/MemoryConsolidation.js +0 -479
- package/dist/src/controllers/MemoryConsolidation.js.map +0 -1
- package/dist/src/controllers/QUICConnection.d.ts +0 -122
- package/dist/src/controllers/QUICConnection.d.ts.map +0 -1
- package/dist/src/controllers/QUICConnection.js +0 -329
- package/dist/src/controllers/QUICConnection.js.map +0 -1
- package/dist/src/controllers/QUICConnectionPool.d.ts +0 -83
- package/dist/src/controllers/QUICConnectionPool.d.ts.map +0 -1
- package/dist/src/controllers/QUICConnectionPool.js +0 -256
- package/dist/src/controllers/QUICConnectionPool.js.map +0 -1
- package/dist/src/controllers/QUICStreamManager.d.ts +0 -114
- package/dist/src/controllers/QUICStreamManager.d.ts.map +0 -1
- package/dist/src/controllers/QUICStreamManager.js +0 -267
- package/dist/src/controllers/QUICStreamManager.js.map +0 -1
- package/dist/src/controllers/StreamingEmbeddingService.d.ts +0 -82
- package/dist/src/controllers/StreamingEmbeddingService.d.ts.map +0 -1
- package/dist/src/controllers/StreamingEmbeddingService.js +0 -243
- package/dist/src/controllers/StreamingEmbeddingService.js.map +0 -1
- package/dist/src/controllers/index.cjs +0 -6
- package/dist/src/coordination/MultiDatabaseCoordinator.d.ts +0 -348
- package/dist/src/coordination/MultiDatabaseCoordinator.d.ts.map +0 -1
- package/dist/src/coordination/MultiDatabaseCoordinator.js +0 -803
- package/dist/src/coordination/MultiDatabaseCoordinator.js.map +0 -1
- package/dist/src/coordination/index.d.ts +0 -10
- package/dist/src/coordination/index.d.ts.map +0 -1
- package/dist/src/coordination/index.js +0 -10
- package/dist/src/coordination/index.js.map +0 -1
- package/dist/src/index.cjs +0 -6
- package/dist/src/optimizations/RVFOptimizer.d.ts +0 -226
- package/dist/src/optimizations/RVFOptimizer.d.ts.map +0 -1
- package/dist/src/optimizations/RVFOptimizer.js +0 -541
- package/dist/src/optimizations/RVFOptimizer.js.map +0 -1
- package/dist/src/security/AttestationLog.d.ts +0 -70
- package/dist/src/security/AttestationLog.d.ts.map +0 -1
- package/dist/src/security/AttestationLog.js +0 -174
- package/dist/src/security/AttestationLog.js.map +0 -1
- package/dist/src/security/MutationGuard.d.ts +0 -83
- package/dist/src/security/MutationGuard.d.ts.map +0 -1
- package/dist/src/security/MutationGuard.js +0 -364
- package/dist/src/security/MutationGuard.js.map +0 -1
- package/dist/src/security/index.cjs +0 -6
- package/dist/src/security/index.d.ts +0 -15
- package/dist/src/security/index.d.ts.map +0 -1
- package/dist/src/security/index.js +0 -18
- package/dist/src/security/index.js.map +0 -1
- package/dist/src/services/GNNService.d.ts +0 -173
- package/dist/src/services/GNNService.d.ts.map +0 -1
- package/dist/src/services/GNNService.js +0 -639
- package/dist/src/services/GNNService.js.map +0 -1
- package/dist/src/services/GraphTransformerService.d.ts +0 -80
- package/dist/src/services/GraphTransformerService.d.ts.map +0 -1
- package/dist/src/services/GraphTransformerService.js +0 -369
- package/dist/src/services/GraphTransformerService.js.map +0 -1
- package/dist/src/services/SemanticRouter.d.ts +0 -83
- package/dist/src/services/SemanticRouter.d.ts.map +0 -1
- package/dist/src/services/SemanticRouter.js +0 -160
- package/dist/src/services/SemanticRouter.js.map +0 -1
- package/dist/src/services/SonaTrajectoryService.d.ts +0 -224
- package/dist/src/services/SonaTrajectoryService.d.ts.map +0 -1
- package/dist/src/services/SonaTrajectoryService.js +0 -539
- package/dist/src/services/SonaTrajectoryService.js.map +0 -1
- package/dist/src/utils/LegacyAttentionAdapter.d.ts +0 -93
- package/dist/src/utils/LegacyAttentionAdapter.d.ts.map +0 -1
- package/dist/src/utils/LegacyAttentionAdapter.js +0 -241
- package/dist/src/utils/LegacyAttentionAdapter.js.map +0 -1
- package/dist/src/utils/vector-math.d.ts +0 -29
- package/dist/src/utils/vector-math.d.ts.map +0 -1
- package/dist/src/utils/vector-math.js +0 -66
- package/dist/src/utils/vector-math.js.map +0 -1
|
@@ -3,15 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Provides state-of-the-art attention mechanisms with runtime detection:
|
|
5
5
|
* - MultiHeadAttention (standard transformer attention)
|
|
6
|
-
* - FlashAttention (memory-efficient attention
|
|
6
|
+
* - FlashAttention (memory-efficient attention)
|
|
7
7
|
* - HyperbolicAttention (hyperbolic space attention)
|
|
8
8
|
* - MoEAttention (Mixture-of-Experts attention)
|
|
9
9
|
* - LinearAttention (linear complexity attention)
|
|
10
10
|
*
|
|
11
|
-
* ADR-064 Phase 1: Native Flash Attention integration with @ruvector/attention
|
|
12
|
-
* bindings for 7.47x speedup. High-level API (applyFlashAttention, applyMultiHeadAttention,
|
|
13
|
-
* applyMoE) works with number[] arrays for ergonomic MCP tool usage.
|
|
14
|
-
*
|
|
15
11
|
* Features:
|
|
16
12
|
* - Automatic runtime detection (Node.js NAPI vs Browser WASM)
|
|
17
13
|
* - Zero-copy Float32Array processing
|
|
@@ -19,157 +15,101 @@
|
|
|
19
15
|
* - Performance monitoring hooks
|
|
20
16
|
* - Type-safe interfaces
|
|
21
17
|
*/
|
|
18
|
+
import { AttentionConfigManager } from './attention/AttentionConfig.js';
|
|
19
|
+
import { AttentionMetricsTracker } from './attention/AttentionMetrics.js';
|
|
20
|
+
import { AttentionCacheManager } from './attention/AttentionCache.js';
|
|
21
|
+
import { AttentionWASMManager } from './attention/AttentionWASM.js';
|
|
22
|
+
import { AttentionCoreCompute } from './attention/AttentionCore.js';
|
|
23
|
+
import { SparsificationService } from './SparsificationService.js';
|
|
24
|
+
import { MincutService } from './MincutService.js';
|
|
22
25
|
/**
|
|
23
|
-
*
|
|
24
|
-
*/
|
|
25
|
-
function detectRuntime() {
|
|
26
|
-
// Check for Node.js
|
|
27
|
-
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
|
|
28
|
-
return 'nodejs';
|
|
29
|
-
}
|
|
30
|
-
// Check for browser (with proper type guards)
|
|
31
|
-
if (typeof globalThis !== 'undefined') {
|
|
32
|
-
const global = globalThis;
|
|
33
|
-
if (typeof global.window !== 'undefined' && typeof global.document !== 'undefined') {
|
|
34
|
-
return 'browser';
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return 'unknown';
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* AttentionService - Main controller for attention mechanisms
|
|
26
|
+
* AttentionService - Main orchestration layer for attention mechanisms
|
|
41
27
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
28
|
+
* Delegates to specialized classes:
|
|
29
|
+
* - AttentionConfigManager: Configuration and constants
|
|
30
|
+
* - AttentionMetricsTracker: Performance monitoring
|
|
31
|
+
* - AttentionCacheManager: Buffer pooling and mask caching
|
|
32
|
+
* - AttentionWASMManager: WASM/NAPI module loading
|
|
33
|
+
* - AttentionCoreCompute: Core computation algorithms
|
|
44
34
|
*/
|
|
45
35
|
export class AttentionService {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
36
|
+
configManager;
|
|
37
|
+
metricsTracker;
|
|
38
|
+
cacheManager;
|
|
39
|
+
wasmManager;
|
|
40
|
+
coreCompute;
|
|
41
|
+
sparsificationService;
|
|
42
|
+
mincutService;
|
|
50
43
|
initialized = false;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
stats = {
|
|
54
|
-
totalOps: 0,
|
|
55
|
-
avgExecutionTimeMs: 0,
|
|
56
|
-
peakMemoryBytes: 0,
|
|
57
|
-
mechanismCounts: {},
|
|
58
|
-
runtimeCounts: {}
|
|
59
|
-
};
|
|
44
|
+
initPromise = null;
|
|
45
|
+
warmedUp = false;
|
|
60
46
|
constructor(config) {
|
|
61
|
-
this.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
47
|
+
this.configManager = new AttentionConfigManager(config);
|
|
48
|
+
this.metricsTracker = new AttentionMetricsTracker();
|
|
49
|
+
this.cacheManager = new AttentionCacheManager();
|
|
50
|
+
this.wasmManager = new AttentionWASMManager();
|
|
51
|
+
this.coreCompute = new AttentionCoreCompute(this.configManager, this.cacheManager);
|
|
52
|
+
// Initialize sparse attention services if configured
|
|
53
|
+
const cfg = this.configManager.getConfig();
|
|
54
|
+
if (cfg.sparsification?.enabled) {
|
|
55
|
+
this.sparsificationService = new SparsificationService({
|
|
56
|
+
method: cfg.sparsification.method,
|
|
57
|
+
topK: cfg.sparsification.topK
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (cfg.partitioning?.enabled) {
|
|
61
|
+
this.mincutService = new MincutService({
|
|
62
|
+
algorithm: cfg.partitioning.method,
|
|
63
|
+
maxPartitionSize: cfg.partitioning.maxPartitionSize
|
|
64
|
+
});
|
|
65
|
+
}
|
|
79
66
|
}
|
|
80
67
|
/**
|
|
81
68
|
* Initialize the attention service
|
|
82
69
|
* Automatically detects and loads the appropriate backend (NAPI or WASM)
|
|
70
|
+
* Thread-safe with promise guard to prevent concurrent initialization
|
|
83
71
|
*/
|
|
84
72
|
async initialize() {
|
|
73
|
+
// Already initialized
|
|
85
74
|
if (this.initialized) {
|
|
86
75
|
return;
|
|
87
76
|
}
|
|
77
|
+
// Initialization in progress - wait for it
|
|
78
|
+
if (this.initPromise) {
|
|
79
|
+
return this.initPromise;
|
|
80
|
+
}
|
|
81
|
+
// Start new initialization
|
|
82
|
+
this.initPromise = this._doInitialize();
|
|
83
|
+
await this.initPromise;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Internal initialization implementation
|
|
87
|
+
*/
|
|
88
|
+
async _doInitialize() {
|
|
88
89
|
performance.mark('attention-service-init-start');
|
|
89
90
|
try {
|
|
90
|
-
|
|
91
|
-
// Try to load NAPI module for Node.js
|
|
92
|
-
await this.loadNAPIModule();
|
|
93
|
-
}
|
|
94
|
-
else if (this.runtime === 'browser') {
|
|
95
|
-
// Load WASM module for browsers
|
|
96
|
-
await this.loadWASMModule();
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
console.warn('⚠️ Unknown runtime environment, using fallback implementation');
|
|
100
|
-
}
|
|
91
|
+
await this.wasmManager.initialize();
|
|
101
92
|
this.initialized = true;
|
|
102
93
|
performance.mark('attention-service-init-end');
|
|
103
94
|
performance.measure('attention-service-init', 'attention-service-init-start', 'attention-service-init-end');
|
|
104
95
|
const measure = performance.getEntriesByName('attention-service-init')[0];
|
|
105
|
-
console.log(`✅ AttentionService initialized in ${measure.duration.toFixed(2)}ms (${this.
|
|
96
|
+
console.log(`✅ AttentionService initialized in ${measure.duration.toFixed(2)}ms (${this.wasmManager.getRuntime()})`);
|
|
97
|
+
// Clear performance entries to prevent memory leak
|
|
98
|
+
this.metricsTracker.clearPerformanceEntries('attention-service-init');
|
|
99
|
+
// Warm up JIT with small computation
|
|
100
|
+
if (!this.warmedUp) {
|
|
101
|
+
await this.warmUp();
|
|
102
|
+
this.warmedUp = true;
|
|
103
|
+
}
|
|
106
104
|
}
|
|
107
105
|
catch (error) {
|
|
108
106
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
109
107
|
console.error(`❌ AttentionService initialization failed: ${errorMessage}`);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Load NAPI module for Node.js runtime
|
|
115
|
-
*
|
|
116
|
-
* ADR-062: Tries native @ruvector/attention first, then
|
|
117
|
-
* @ruvector/graph-transformer sublinear attention as a secondary path.
|
|
118
|
-
*/
|
|
119
|
-
async loadNAPIModule() {
|
|
120
|
-
// Strategy 1: Try @ruvector/attention (direct NAPI-RS bindings)
|
|
121
|
-
try {
|
|
122
|
-
// @ts-ignore - Optional dependency
|
|
123
|
-
const mod = await import('@ruvector/attention');
|
|
124
|
-
if (mod && (typeof mod.multiHeadAttention === 'function' || typeof mod.flashAttention === 'function' || mod.default)) {
|
|
125
|
-
this.napiModule = mod;
|
|
126
|
-
this.engineType = 'napi';
|
|
127
|
-
console.log('[AttentionService] Using native @ruvector/attention NAPI-RS (2.49x-7.47x speedup)');
|
|
128
|
-
return;
|
|
108
|
+
// Preserve original error stack trace
|
|
109
|
+
if (error instanceof Error) {
|
|
110
|
+
throw error;
|
|
129
111
|
}
|
|
130
|
-
|
|
131
|
-
catch {
|
|
132
|
-
// Not available, try next
|
|
133
|
-
}
|
|
134
|
-
// Strategy 2: Try @ruvector/graph-transformer for sublinear attention
|
|
135
|
-
try {
|
|
136
|
-
// @ts-ignore - Optional dependency
|
|
137
|
-
const { GraphTransformer } = await import('@ruvector/graph-transformer');
|
|
138
|
-
if (GraphTransformer) {
|
|
139
|
-
const gt = new GraphTransformer();
|
|
140
|
-
// Wrap graph-transformer as a partial NAPI module
|
|
141
|
-
this.napiModule = {
|
|
142
|
-
_graphTransformer: gt,
|
|
143
|
-
_isGraphTransformerShim: true,
|
|
144
|
-
};
|
|
145
|
-
this.engineType = 'napi';
|
|
146
|
-
console.log('[AttentionService] Using @ruvector/graph-transformer native attention');
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
// Not available
|
|
152
|
-
}
|
|
153
|
-
console.warn('[AttentionService] No native bindings available, using JS fallback');
|
|
154
|
-
this.napiModule = null;
|
|
155
|
-
this.engineType = 'fallback';
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Load WASM module for browser runtime
|
|
159
|
-
*/
|
|
160
|
-
async loadWASMModule() {
|
|
161
|
-
try {
|
|
162
|
-
// @ts-ignore - Optional dependency
|
|
163
|
-
this.wasmModule = await import('ruvector-attention-wasm');
|
|
164
|
-
await this.wasmModule.default(); // Initialize WASM
|
|
165
|
-
this.engineType = 'wasm';
|
|
166
|
-
console.log('[AttentionService] Using ruvector-attention-wasm');
|
|
167
|
-
}
|
|
168
|
-
catch (error) {
|
|
169
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
170
|
-
console.warn(`[AttentionService] WASM not available: ${errorMessage}`);
|
|
171
|
-
this.wasmModule = null;
|
|
172
|
-
this.engineType = 'fallback';
|
|
112
|
+
throw new Error(`Failed to initialize AttentionService: ${errorMessage}`);
|
|
173
113
|
}
|
|
174
114
|
}
|
|
175
115
|
/**
|
|
@@ -186,28 +126,29 @@ export class AttentionService {
|
|
|
186
126
|
await this.initialize();
|
|
187
127
|
}
|
|
188
128
|
performance.mark('mha-start');
|
|
189
|
-
const startTime = Date.now();
|
|
190
129
|
try {
|
|
191
130
|
let output;
|
|
192
131
|
let weights;
|
|
193
132
|
let runtime = 'fallback';
|
|
133
|
+
const napiModule = this.wasmManager.getNAPIModule();
|
|
134
|
+
const wasmModule = this.wasmManager.getWASMModule();
|
|
194
135
|
// Try NAPI first (fastest for Node.js)
|
|
195
|
-
if (
|
|
196
|
-
const result =
|
|
136
|
+
if (napiModule && napiModule.multiHeadAttention) {
|
|
137
|
+
const result = napiModule.multiHeadAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), mask);
|
|
197
138
|
output = result.output;
|
|
198
139
|
weights = result.weights;
|
|
199
140
|
runtime = 'napi';
|
|
200
141
|
}
|
|
201
142
|
// Try WASM (for browsers)
|
|
202
|
-
else if (
|
|
203
|
-
const result =
|
|
143
|
+
else if (wasmModule && wasmModule.multiHeadAttention) {
|
|
144
|
+
const result = wasmModule.multiHeadAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), mask);
|
|
204
145
|
output = result.output;
|
|
205
146
|
weights = result.weights;
|
|
206
147
|
runtime = 'wasm';
|
|
207
148
|
}
|
|
208
149
|
// Fallback to JavaScript implementation
|
|
209
150
|
else {
|
|
210
|
-
const result = this.multiHeadAttentionFallback(query, key, value, mask);
|
|
151
|
+
const result = this.coreCompute.multiHeadAttentionFallback(query, key, value, mask);
|
|
211
152
|
output = result.output;
|
|
212
153
|
weights = result.weights;
|
|
213
154
|
runtime = 'fallback';
|
|
@@ -217,7 +158,7 @@ export class AttentionService {
|
|
|
217
158
|
const measure = performance.getEntriesByName('mha')[0];
|
|
218
159
|
const executionTimeMs = measure.duration;
|
|
219
160
|
// Update statistics
|
|
220
|
-
this.updateStats('multi-head', runtime, executionTimeMs, output.length * 4);
|
|
161
|
+
this.metricsTracker.updateStats('multi-head', runtime, executionTimeMs, output.length * 4);
|
|
221
162
|
return {
|
|
222
163
|
output,
|
|
223
164
|
weights,
|
|
@@ -233,14 +174,6 @@ export class AttentionService {
|
|
|
233
174
|
}
|
|
234
175
|
/**
|
|
235
176
|
* Compute Flash Attention (memory-efficient)
|
|
236
|
-
*
|
|
237
|
-
* Flash Attention reduces memory usage from O(n²) to O(n) for sequence length n
|
|
238
|
-
*
|
|
239
|
-
* @param query - Query vectors
|
|
240
|
-
* @param key - Key vectors
|
|
241
|
-
* @param value - Value vectors
|
|
242
|
-
* @param mask - Optional attention mask
|
|
243
|
-
* @returns Attention output and metadata
|
|
244
177
|
*/
|
|
245
178
|
async flashAttention(query, key, value, mask) {
|
|
246
179
|
if (!this.initialized) {
|
|
@@ -250,19 +183,21 @@ export class AttentionService {
|
|
|
250
183
|
try {
|
|
251
184
|
let output;
|
|
252
185
|
let runtime = 'fallback';
|
|
186
|
+
const napiModule = this.wasmManager.getNAPIModule();
|
|
187
|
+
const wasmModule = this.wasmManager.getWASMModule();
|
|
253
188
|
// Try NAPI first
|
|
254
|
-
if (
|
|
255
|
-
output =
|
|
189
|
+
if (napiModule && napiModule.flashAttention) {
|
|
190
|
+
output = napiModule.flashAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), mask);
|
|
256
191
|
runtime = 'napi';
|
|
257
192
|
}
|
|
258
193
|
// Try WASM
|
|
259
|
-
else if (
|
|
260
|
-
output =
|
|
194
|
+
else if (wasmModule && wasmModule.flashAttention) {
|
|
195
|
+
output = wasmModule.flashAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), mask);
|
|
261
196
|
runtime = 'wasm';
|
|
262
197
|
}
|
|
263
198
|
// Fallback (same as multi-head for now)
|
|
264
199
|
else {
|
|
265
|
-
const result = this.multiHeadAttentionFallback(query, key, value, mask);
|
|
200
|
+
const result = this.coreCompute.multiHeadAttentionFallback(query, key, value, mask);
|
|
266
201
|
output = result.output;
|
|
267
202
|
runtime = 'fallback';
|
|
268
203
|
}
|
|
@@ -271,7 +206,7 @@ export class AttentionService {
|
|
|
271
206
|
const measure = performance.getEntriesByName('flash')[0];
|
|
272
207
|
const executionTimeMs = measure.duration;
|
|
273
208
|
// Update statistics
|
|
274
|
-
this.updateStats('flash', runtime, executionTimeMs, output.length * 4);
|
|
209
|
+
this.metricsTracker.updateStats('flash', runtime, executionTimeMs, output.length * 4);
|
|
275
210
|
return {
|
|
276
211
|
output,
|
|
277
212
|
executionTimeMs,
|
|
@@ -284,15 +219,100 @@ export class AttentionService {
|
|
|
284
219
|
throw new Error(`Flash attention failed: ${errorMessage}`);
|
|
285
220
|
}
|
|
286
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Compute Flash Attention v2 (optimized memory-efficient attention)
|
|
224
|
+
*/
|
|
225
|
+
async flashAttentionV2(query, key, value, options) {
|
|
226
|
+
if (!this.initialized) {
|
|
227
|
+
await this.initialize();
|
|
228
|
+
}
|
|
229
|
+
performance.mark('flash-v2-start');
|
|
230
|
+
try {
|
|
231
|
+
let output;
|
|
232
|
+
let runtime = 'fallback';
|
|
233
|
+
let speedup;
|
|
234
|
+
let baselineTimeMs;
|
|
235
|
+
const napiModule = this.wasmManager.getNAPIModule();
|
|
236
|
+
const wasmModule = this.wasmManager.getWASMModule();
|
|
237
|
+
// Try NAPI first (fastest)
|
|
238
|
+
if (napiModule && napiModule.flashAttentionV2) {
|
|
239
|
+
const result = napiModule.flashAttentionV2(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), {
|
|
240
|
+
mask: options?.mask,
|
|
241
|
+
causal: options?.causal ?? false,
|
|
242
|
+
windowSize: options?.windowSize,
|
|
243
|
+
dropout: options?.dropout ?? this.configManager.getDropout(),
|
|
244
|
+
});
|
|
245
|
+
output = result.output;
|
|
246
|
+
speedup = result.speedup;
|
|
247
|
+
baselineTimeMs = result.baselineTimeMs;
|
|
248
|
+
runtime = 'napi';
|
|
249
|
+
}
|
|
250
|
+
// Try WASM (ADR-071 Phase 3 target)
|
|
251
|
+
else if (wasmModule && wasmModule.flashAttentionV2) {
|
|
252
|
+
const result = wasmModule.flashAttentionV2(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), {
|
|
253
|
+
mask: options?.mask,
|
|
254
|
+
causal: options?.causal ?? false,
|
|
255
|
+
windowSize: options?.windowSize,
|
|
256
|
+
dropout: options?.dropout ?? this.configManager.getDropout(),
|
|
257
|
+
});
|
|
258
|
+
output = result.output;
|
|
259
|
+
speedup = result.speedup;
|
|
260
|
+
baselineTimeMs = result.baselineTimeMs;
|
|
261
|
+
runtime = 'wasm';
|
|
262
|
+
}
|
|
263
|
+
// Fallback to Flash Attention v1 or standard attention
|
|
264
|
+
else {
|
|
265
|
+
console.warn('⚠️ Flash Attention v2 not available, falling back to v1');
|
|
266
|
+
// Benchmark baseline for comparison
|
|
267
|
+
const baselineStart = performance.now();
|
|
268
|
+
const fallbackResult = this.coreCompute.multiHeadAttentionFallback(query, key, value, options?.mask);
|
|
269
|
+
baselineTimeMs = performance.now() - baselineStart;
|
|
270
|
+
// Use v1 Flash Attention if available
|
|
271
|
+
if (wasmModule?.flashAttention || napiModule?.flashAttention) {
|
|
272
|
+
const flashStart = performance.now();
|
|
273
|
+
const flashResult = await this.flashAttention(query, key, value, options?.mask);
|
|
274
|
+
const flashTimeMs = performance.now() - flashStart;
|
|
275
|
+
output = flashResult.output;
|
|
276
|
+
speedup = baselineTimeMs / flashTimeMs;
|
|
277
|
+
runtime = flashResult.runtime;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
output = fallbackResult.output;
|
|
281
|
+
speedup = 1.0; // No speedup in pure fallback
|
|
282
|
+
runtime = 'fallback';
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
performance.mark('flash-v2-end');
|
|
286
|
+
performance.measure('flash-v2', 'flash-v2-start', 'flash-v2-end');
|
|
287
|
+
const measure = performance.getEntriesByName('flash-v2')[0];
|
|
288
|
+
const executionTimeMs = measure.duration;
|
|
289
|
+
// Update statistics
|
|
290
|
+
this.metricsTracker.updateStats('flash-v2', runtime, executionTimeMs, output.length * 4);
|
|
291
|
+
// Log performance metrics for ADR-071 verification
|
|
292
|
+
if (speedup && speedup >= AttentionConfigManager.FLASH_V2_MIN_SPEEDUP) {
|
|
293
|
+
console.log(`✅ Flash Attention v2 achieved ${speedup.toFixed(2)}x speedup ` +
|
|
294
|
+
`(target: ${AttentionConfigManager.FLASH_V2_MIN_SPEEDUP}x-${AttentionConfigManager.FLASH_V2_MAX_SPEEDUP}x)`);
|
|
295
|
+
}
|
|
296
|
+
else if (speedup) {
|
|
297
|
+
console.warn(`⚠️ Flash Attention v2 speedup ${speedup.toFixed(2)}x below target ` +
|
|
298
|
+
`(${AttentionConfigManager.FLASH_V2_MIN_SPEEDUP}x-${AttentionConfigManager.FLASH_V2_MAX_SPEEDUP}x)`);
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
output,
|
|
302
|
+
executionTimeMs,
|
|
303
|
+
mechanism: 'flash',
|
|
304
|
+
runtime,
|
|
305
|
+
speedup,
|
|
306
|
+
baselineTimeMs,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
311
|
+
throw new Error(`Flash Attention v2 failed: ${errorMessage}`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
287
314
|
/**
|
|
288
315
|
* Compute Linear Attention (O(n) complexity)
|
|
289
|
-
*
|
|
290
|
-
* Linear attention approximates standard attention with linear complexity
|
|
291
|
-
*
|
|
292
|
-
* @param query - Query vectors
|
|
293
|
-
* @param key - Key vectors
|
|
294
|
-
* @param value - Value vectors
|
|
295
|
-
* @returns Attention output and metadata
|
|
296
316
|
*/
|
|
297
317
|
async linearAttention(query, key, value) {
|
|
298
318
|
if (!this.initialized) {
|
|
@@ -302,19 +322,21 @@ export class AttentionService {
|
|
|
302
322
|
try {
|
|
303
323
|
let output;
|
|
304
324
|
let runtime = 'fallback';
|
|
325
|
+
const napiModule = this.wasmManager.getNAPIModule();
|
|
326
|
+
const wasmModule = this.wasmManager.getWASMModule();
|
|
305
327
|
// Try NAPI first
|
|
306
|
-
if (
|
|
307
|
-
output =
|
|
328
|
+
if (napiModule && napiModule.linearAttention) {
|
|
329
|
+
output = napiModule.linearAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim());
|
|
308
330
|
runtime = 'napi';
|
|
309
331
|
}
|
|
310
332
|
// Try WASM
|
|
311
|
-
else if (
|
|
312
|
-
output =
|
|
333
|
+
else if (wasmModule && wasmModule.linearAttention) {
|
|
334
|
+
output = wasmModule.linearAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim());
|
|
313
335
|
runtime = 'wasm';
|
|
314
336
|
}
|
|
315
337
|
// Fallback
|
|
316
338
|
else {
|
|
317
|
-
output = this.linearAttentionFallback(query, key, value);
|
|
339
|
+
output = this.coreCompute.linearAttentionFallback(query, key, value);
|
|
318
340
|
runtime = 'fallback';
|
|
319
341
|
}
|
|
320
342
|
performance.mark('linear-end');
|
|
@@ -322,7 +344,7 @@ export class AttentionService {
|
|
|
322
344
|
const measure = performance.getEntriesByName('linear')[0];
|
|
323
345
|
const executionTimeMs = measure.duration;
|
|
324
346
|
// Update statistics
|
|
325
|
-
this.updateStats('linear', runtime, executionTimeMs, output.length * 4);
|
|
347
|
+
this.metricsTracker.updateStats('linear', runtime, executionTimeMs, output.length * 4);
|
|
326
348
|
return {
|
|
327
349
|
output,
|
|
328
350
|
executionTimeMs,
|
|
@@ -337,14 +359,6 @@ export class AttentionService {
|
|
|
337
359
|
}
|
|
338
360
|
/**
|
|
339
361
|
* Compute Hyperbolic Attention (for hierarchical data)
|
|
340
|
-
*
|
|
341
|
-
* Hyperbolic attention operates in hyperbolic space, suitable for tree-like structures
|
|
342
|
-
*
|
|
343
|
-
* @param query - Query vectors
|
|
344
|
-
* @param key - Key vectors
|
|
345
|
-
* @param value - Value vectors
|
|
346
|
-
* @param curvature - Hyperbolic space curvature (default: -1.0)
|
|
347
|
-
* @returns Attention output and metadata
|
|
348
362
|
*/
|
|
349
363
|
async hyperbolicAttention(query, key, value, curvature = -1.0) {
|
|
350
364
|
if (!this.initialized) {
|
|
@@ -354,19 +368,21 @@ export class AttentionService {
|
|
|
354
368
|
try {
|
|
355
369
|
let output;
|
|
356
370
|
let runtime = 'fallback';
|
|
371
|
+
const napiModule = this.wasmManager.getNAPIModule();
|
|
372
|
+
const wasmModule = this.wasmManager.getWASMModule();
|
|
357
373
|
// Try NAPI first
|
|
358
|
-
if (
|
|
359
|
-
output =
|
|
374
|
+
if (napiModule && napiModule.hyperbolicAttention) {
|
|
375
|
+
output = napiModule.hyperbolicAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), curvature);
|
|
360
376
|
runtime = 'napi';
|
|
361
377
|
}
|
|
362
378
|
// Try WASM
|
|
363
|
-
else if (
|
|
364
|
-
output =
|
|
379
|
+
else if (wasmModule && wasmModule.hyperbolicAttention) {
|
|
380
|
+
output = wasmModule.hyperbolicAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), curvature);
|
|
365
381
|
runtime = 'wasm';
|
|
366
382
|
}
|
|
367
383
|
// Fallback (use standard attention)
|
|
368
384
|
else {
|
|
369
|
-
const result = this.multiHeadAttentionFallback(query, key, value);
|
|
385
|
+
const result = this.coreCompute.multiHeadAttentionFallback(query, key, value);
|
|
370
386
|
output = result.output;
|
|
371
387
|
runtime = 'fallback';
|
|
372
388
|
}
|
|
@@ -375,7 +391,7 @@ export class AttentionService {
|
|
|
375
391
|
const measure = performance.getEntriesByName('hyperbolic')[0];
|
|
376
392
|
const executionTimeMs = measure.duration;
|
|
377
393
|
// Update statistics
|
|
378
|
-
this.updateStats('hyperbolic', runtime, executionTimeMs, output.length * 4);
|
|
394
|
+
this.metricsTracker.updateStats('hyperbolic', runtime, executionTimeMs, output.length * 4);
|
|
379
395
|
return {
|
|
380
396
|
output,
|
|
381
397
|
executionTimeMs,
|
|
@@ -389,15 +405,25 @@ export class AttentionService {
|
|
|
389
405
|
}
|
|
390
406
|
}
|
|
391
407
|
/**
|
|
392
|
-
* Compute
|
|
408
|
+
* Compute Fused Attention (optimized single-pass attention)
|
|
393
409
|
*
|
|
394
|
-
*
|
|
410
|
+
* Fused attention combines softmax and weighted sum in a single pass
|
|
411
|
+
* for 20-25% performance improvement through better cache locality.
|
|
395
412
|
*
|
|
396
|
-
* @param query - Query vectors
|
|
397
|
-
* @param key - Key vectors
|
|
398
|
-
* @param value - Value vectors
|
|
399
|
-
* @param
|
|
400
|
-
* @returns Attention output and
|
|
413
|
+
* @param query - Query vectors [seqLen * embedDim]
|
|
414
|
+
* @param key - Key vectors [seqLen * embedDim]
|
|
415
|
+
* @param value - Value vectors [seqLen * embedDim]
|
|
416
|
+
* @param options - Fused attention options
|
|
417
|
+
* @returns Attention output and performance metrics
|
|
418
|
+
*/
|
|
419
|
+
async fusedAttention(query, key, value, options) {
|
|
420
|
+
if (!this.initialized) {
|
|
421
|
+
await this.initialize();
|
|
422
|
+
}
|
|
423
|
+
return this.coreCompute.fusedAttention(query, key, value, options);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Compute Mixture-of-Experts (MoE) Attention
|
|
401
427
|
*/
|
|
402
428
|
async moeAttention(query, key, value, mask) {
|
|
403
429
|
if (!this.initialized) {
|
|
@@ -407,21 +433,23 @@ export class AttentionService {
|
|
|
407
433
|
try {
|
|
408
434
|
let output;
|
|
409
435
|
let runtime = 'fallback';
|
|
410
|
-
const numExperts = this.
|
|
411
|
-
const topK = this.
|
|
436
|
+
const numExperts = this.configManager.getNumExperts();
|
|
437
|
+
const topK = this.configManager.getTopK();
|
|
438
|
+
const napiModule = this.wasmManager.getNAPIModule();
|
|
439
|
+
const wasmModule = this.wasmManager.getWASMModule();
|
|
412
440
|
// Try NAPI first
|
|
413
|
-
if (
|
|
414
|
-
output =
|
|
441
|
+
if (napiModule && napiModule.moeAttention) {
|
|
442
|
+
output = napiModule.moeAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), numExperts, topK, mask);
|
|
415
443
|
runtime = 'napi';
|
|
416
444
|
}
|
|
417
445
|
// Try WASM
|
|
418
|
-
else if (
|
|
419
|
-
output =
|
|
446
|
+
else if (wasmModule && wasmModule.moeAttention) {
|
|
447
|
+
output = wasmModule.moeAttention(query, key, value, this.configManager.getNumHeads(), this.configManager.getHeadDim(), numExperts, topK, mask);
|
|
420
448
|
runtime = 'wasm';
|
|
421
449
|
}
|
|
422
450
|
// Fallback (use standard attention)
|
|
423
451
|
else {
|
|
424
|
-
const result = this.multiHeadAttentionFallback(query, key, value, mask);
|
|
452
|
+
const result = this.coreCompute.multiHeadAttentionFallback(query, key, value, mask);
|
|
425
453
|
output = result.output;
|
|
426
454
|
runtime = 'fallback';
|
|
427
455
|
}
|
|
@@ -430,7 +458,7 @@ export class AttentionService {
|
|
|
430
458
|
const measure = performance.getEntriesByName('moe')[0];
|
|
431
459
|
const executionTimeMs = measure.duration;
|
|
432
460
|
// Update statistics
|
|
433
|
-
this.updateStats('moe', runtime, executionTimeMs, output.length * 4);
|
|
461
|
+
this.metricsTracker.updateStats('moe', runtime, executionTimeMs, output.length * 4);
|
|
434
462
|
return {
|
|
435
463
|
output,
|
|
436
464
|
executionTimeMs,
|
|
@@ -444,406 +472,270 @@ export class AttentionService {
|
|
|
444
472
|
}
|
|
445
473
|
}
|
|
446
474
|
/**
|
|
447
|
-
*
|
|
448
|
-
*
|
|
475
|
+
* Warm up JIT with small dummy computation
|
|
476
|
+
* Eliminates first-call JIT spikes (50-100ms → 5-10ms)
|
|
449
477
|
*/
|
|
450
|
-
|
|
451
|
-
const
|
|
452
|
-
const
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
const
|
|
456
|
-
|
|
457
|
-
for (let i = 0; i <
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
for (let k = 0; k < headDim; k++) {
|
|
465
|
-
const qIdx = i * embedDim + k;
|
|
466
|
-
const kIdx = j * embedDim + k;
|
|
467
|
-
score += query[qIdx] * key[kIdx];
|
|
468
|
-
}
|
|
469
|
-
score *= scale;
|
|
470
|
-
// Apply mask if provided
|
|
471
|
-
if (mask && mask[i * seqLen + j] === 0) {
|
|
472
|
-
score = -Infinity;
|
|
473
|
-
}
|
|
474
|
-
// Softmax (simplified)
|
|
475
|
-
const weight = Math.exp(score);
|
|
476
|
-
const vIdx = j * embedDim + d;
|
|
477
|
-
sum += weight * value[vIdx];
|
|
478
|
-
weightSum += weight;
|
|
479
|
-
}
|
|
480
|
-
output[i * embedDim + d] = weightSum > 0 ? sum / weightSum : 0;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
return { output };
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Fallback JavaScript implementation of linear attention
|
|
487
|
-
*/
|
|
488
|
-
linearAttentionFallback(query, key, value) {
|
|
489
|
-
// Simplified linear attention using feature maps
|
|
490
|
-
const { embedDim } = this.config;
|
|
491
|
-
const seqLen = Math.floor(query.length / embedDim);
|
|
492
|
-
const output = new Float32Array(query.length);
|
|
493
|
-
// Apply feature map (elu + 1)
|
|
494
|
-
const featureMap = (x) => x > 0 ? x + 1 : Math.exp(x);
|
|
495
|
-
for (let i = 0; i < seqLen; i++) {
|
|
496
|
-
for (let d = 0; d < embedDim; d++) {
|
|
497
|
-
let numerator = 0;
|
|
498
|
-
let denominator = 0;
|
|
499
|
-
for (let j = 0; j < seqLen; j++) {
|
|
500
|
-
const qVal = featureMap(query[i * embedDim + d]);
|
|
501
|
-
const kVal = featureMap(key[j * embedDim + d]);
|
|
502
|
-
const vVal = value[j * embedDim + d];
|
|
503
|
-
numerator += qVal * kVal * vVal;
|
|
504
|
-
denominator += qVal * kVal;
|
|
505
|
-
}
|
|
506
|
-
output[i * embedDim + d] = denominator > 0 ? numerator / denominator : 0;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
return output;
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* Update performance statistics
|
|
513
|
-
*/
|
|
514
|
-
updateStats(mechanism, runtime, executionTimeMs, memoryBytes) {
|
|
515
|
-
this.stats.totalOps++;
|
|
516
|
-
// Update average execution time
|
|
517
|
-
const prevTotal = this.stats.avgExecutionTimeMs * (this.stats.totalOps - 1);
|
|
518
|
-
this.stats.avgExecutionTimeMs = (prevTotal + executionTimeMs) / this.stats.totalOps;
|
|
519
|
-
// Update peak memory
|
|
520
|
-
if (memoryBytes > this.stats.peakMemoryBytes) {
|
|
521
|
-
this.stats.peakMemoryBytes = memoryBytes;
|
|
522
|
-
}
|
|
523
|
-
// Update mechanism counts
|
|
524
|
-
this.stats.mechanismCounts[mechanism] = (this.stats.mechanismCounts[mechanism] || 0) + 1;
|
|
525
|
-
// Update runtime counts
|
|
526
|
-
this.stats.runtimeCounts[runtime] = (this.stats.runtimeCounts[runtime] || 0) + 1;
|
|
478
|
+
async warmUp() {
|
|
479
|
+
const dummySize = 16; // Small size for warm-up
|
|
480
|
+
const embedDim = this.configManager.getEmbedDim();
|
|
481
|
+
const dummyQ = new Float32Array(dummySize * embedDim);
|
|
482
|
+
const dummyK = new Float32Array(dummySize * embedDim);
|
|
483
|
+
const dummyV = new Float32Array(dummySize * embedDim);
|
|
484
|
+
// Fill with random values
|
|
485
|
+
for (let i = 0; i < dummyQ.length; i++) {
|
|
486
|
+
dummyQ[i] = Math.random();
|
|
487
|
+
dummyK[i] = Math.random();
|
|
488
|
+
dummyV[i] = Math.random();
|
|
489
|
+
}
|
|
490
|
+
// Run once to warm up JIT (result discarded)
|
|
491
|
+
await this.multiHeadAttention(dummyQ, dummyK, dummyV);
|
|
527
492
|
}
|
|
528
493
|
/**
|
|
529
494
|
* Get performance statistics
|
|
530
495
|
*/
|
|
531
496
|
getStats() {
|
|
532
|
-
return
|
|
497
|
+
return this.metricsTracker.getStats();
|
|
533
498
|
}
|
|
534
499
|
/**
|
|
535
500
|
* Reset performance statistics
|
|
536
501
|
*/
|
|
537
502
|
resetStats() {
|
|
538
|
-
this.
|
|
539
|
-
totalOps: 0,
|
|
540
|
-
avgExecutionTimeMs: 0,
|
|
541
|
-
peakMemoryBytes: 0,
|
|
542
|
-
mechanismCounts: {},
|
|
543
|
-
runtimeCounts: {}
|
|
544
|
-
};
|
|
503
|
+
this.metricsTracker.resetStats();
|
|
545
504
|
}
|
|
546
505
|
/**
|
|
547
|
-
*
|
|
506
|
+
* Dispose of resources and clean up
|
|
507
|
+
* Call this when AttentionService is no longer needed
|
|
548
508
|
*/
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
509
|
+
async dispose() {
|
|
510
|
+
// Clean up WASM modules
|
|
511
|
+
await this.wasmManager.dispose();
|
|
512
|
+
// Clear all performance entries
|
|
513
|
+
this.metricsTracker.clearAllPerformanceEntries();
|
|
514
|
+
// Clear caches
|
|
515
|
+
this.cacheManager.clear();
|
|
516
|
+
// Reset state
|
|
517
|
+
this.initialized = false;
|
|
518
|
+
this.warmedUp = false;
|
|
519
|
+
this.initPromise = null;
|
|
520
|
+
// Reset stats
|
|
521
|
+
this.metricsTracker.resetStats();
|
|
522
|
+
console.log('✅ AttentionService disposed');
|
|
557
523
|
}
|
|
558
|
-
// -- ADR-064 Phase 1: High-level API for MCP tools --------------------------
|
|
559
524
|
/**
|
|
560
|
-
*
|
|
561
|
-
* Returns attention-weighted output (7.47x faster with native bindings).
|
|
525
|
+
* Compute Sparse Attention
|
|
562
526
|
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
* @param values - Value vectors (number[][])
|
|
566
|
-
* @param options - Optional head count and dropout rate
|
|
567
|
-
* @returns Attention-weighted output vector
|
|
568
|
-
*/
|
|
569
|
-
async applyFlashAttention(query, keys, values, options) {
|
|
570
|
-
if (!this.initialized) {
|
|
571
|
-
await this.initialize();
|
|
572
|
-
}
|
|
573
|
-
const heads = options?.headCount ?? this.config.numHeads;
|
|
574
|
-
const dimPerHead = Math.max(1, Math.floor(query.length / heads));
|
|
575
|
-
const queryBuf = new Float32Array(query);
|
|
576
|
-
const keysBuf = new Float32Array(keys.flat());
|
|
577
|
-
const valuesBuf = new Float32Array(values.flat());
|
|
578
|
-
// Try native Flash Attention
|
|
579
|
-
if (this.napiModule && typeof this.napiModule.flashAttention === 'function') {
|
|
580
|
-
const startNs = performance.now();
|
|
581
|
-
const result = this.napiModule.flashAttention(queryBuf, keysBuf, valuesBuf, heads, dimPerHead);
|
|
582
|
-
const elapsed = performance.now() - startNs;
|
|
583
|
-
this.updateStats('flash', 'napi', elapsed, queryBuf.byteLength);
|
|
584
|
-
const output = result instanceof Float32Array ? result : new Float32Array(result);
|
|
585
|
-
return Array.from(output);
|
|
586
|
-
}
|
|
587
|
-
// Try WASM
|
|
588
|
-
if (this.wasmModule && typeof this.wasmModule.flashAttention === 'function') {
|
|
589
|
-
const startNs = performance.now();
|
|
590
|
-
const result = this.wasmModule.flashAttention(queryBuf, keysBuf, valuesBuf, heads, dimPerHead);
|
|
591
|
-
const elapsed = performance.now() - startNs;
|
|
592
|
-
this.updateStats('flash', 'wasm', elapsed, queryBuf.byteLength);
|
|
593
|
-
const output = result instanceof Float32Array ? result : new Float32Array(result);
|
|
594
|
-
return Array.from(output);
|
|
595
|
-
}
|
|
596
|
-
// JS fallback
|
|
597
|
-
return this.applyAttentionJS(query, keys, values);
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* Apply Multi-Head Attention for 5x better relevance scoring.
|
|
527
|
+
* Uses graph sparsification to reduce the number of attention edges,
|
|
528
|
+
* achieving 10-100x speedup for large graphs (N > 10K nodes).
|
|
601
529
|
*
|
|
602
|
-
* @param query
|
|
603
|
-
* @param
|
|
604
|
-
* @param
|
|
605
|
-
* @returns Attention
|
|
530
|
+
* @param query - Query vector for attention scoring
|
|
531
|
+
* @param graphEdges - Graph adjacency list (node -> neighbors)
|
|
532
|
+
* @param options - Sparse attention options
|
|
533
|
+
* @returns Attention result with sparsity metadata
|
|
606
534
|
*/
|
|
607
|
-
async
|
|
535
|
+
async sparseAttention(query, graphEdges, options) {
|
|
608
536
|
if (!this.initialized) {
|
|
609
537
|
await this.initialize();
|
|
610
538
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
const
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
539
|
+
performance.mark('sparse-attention-start');
|
|
540
|
+
try {
|
|
541
|
+
const numNodes = graphEdges.length;
|
|
542
|
+
// For small graphs (N < 1000), fallback to dense attention
|
|
543
|
+
if (numNodes < 1000) {
|
|
544
|
+
console.warn(`⚠️ Graph size ${numNodes} < 1000, using dense attention`);
|
|
545
|
+
const dummyKey = new Float32Array(query.length);
|
|
546
|
+
const dummyValue = new Float32Array(query.length);
|
|
547
|
+
return this.multiHeadAttention(query, dummyKey, dummyValue);
|
|
548
|
+
}
|
|
549
|
+
// Initialize or reconfigure sparsification service
|
|
550
|
+
const cfg = this.configManager.getConfig();
|
|
551
|
+
const sparsificationMethod = options?.method || cfg.sparsification?.method || 'ppr';
|
|
552
|
+
const sparsificationTopK = options?.topK || cfg.sparsification?.topK || Math.floor(numNodes * 0.1);
|
|
553
|
+
if (!this.sparsificationService) {
|
|
554
|
+
this.sparsificationService = new SparsificationService({
|
|
555
|
+
method: sparsificationMethod,
|
|
556
|
+
topK: sparsificationTopK
|
|
557
|
+
});
|
|
558
|
+
await this.sparsificationService.initialize();
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
// Update config if method or topK changed
|
|
562
|
+
this.sparsificationService.updateConfig({
|
|
563
|
+
method: sparsificationMethod,
|
|
564
|
+
topK: sparsificationTopK
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
// Determine source node (first non-zero element in query)
|
|
568
|
+
let sourceNode = 0;
|
|
569
|
+
for (let i = 0; i < query.length && i < numNodes; i++) {
|
|
570
|
+
if (query[i] !== 0) {
|
|
571
|
+
sourceNode = i;
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
// Sparsify the graph
|
|
576
|
+
const sparsificationResult = await this.sparsificationService.sparsify(sourceNode, graphEdges);
|
|
577
|
+
// Build sparse graph with only top-K nodes
|
|
578
|
+
const sparseEdges = [];
|
|
579
|
+
const nodeMap = new Map(); // original -> sparse index
|
|
580
|
+
sparsificationResult.topKIndices.forEach((originalNode, sparseIdx) => {
|
|
581
|
+
nodeMap.set(originalNode, sparseIdx);
|
|
582
|
+
});
|
|
583
|
+
for (const originalNode of sparsificationResult.topKIndices) {
|
|
584
|
+
const neighbors = graphEdges[originalNode] || [];
|
|
585
|
+
const sparseNeighbors = [];
|
|
586
|
+
for (const neighbor of neighbors) {
|
|
587
|
+
const sparseNeighborIdx = nodeMap.get(neighbor);
|
|
588
|
+
if (sparseNeighborIdx !== undefined) {
|
|
589
|
+
sparseNeighbors.push(sparseNeighborIdx);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
sparseEdges.push(sparseNeighbors);
|
|
593
|
+
}
|
|
594
|
+
// Build sparse query/key/value matrices
|
|
595
|
+
const topK = sparsificationResult.topKIndices.length;
|
|
596
|
+
const embedDim = this.configManager.getEmbedDim();
|
|
597
|
+
const sparseQuery = new Float32Array(topK * embedDim);
|
|
598
|
+
const sparseKey = new Float32Array(topK * embedDim);
|
|
599
|
+
const sparseValue = new Float32Array(topK * embedDim);
|
|
600
|
+
for (let i = 0; i < topK; i++) {
|
|
601
|
+
const originalNode = sparsificationResult.topKIndices[i];
|
|
602
|
+
const score = sparsificationResult.scores[originalNode];
|
|
603
|
+
// Use score as query embedding (weighted by importance)
|
|
604
|
+
for (let d = 0; d < embedDim; d++) {
|
|
605
|
+
sparseQuery[i * embedDim + d] = score;
|
|
606
|
+
sparseKey[i * embedDim + d] = score;
|
|
607
|
+
sparseValue[i * embedDim + d] = score;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
// Run attention on sparse graph
|
|
611
|
+
const attentionResult = await this.multiHeadAttention(sparseQuery, sparseKey, sparseValue);
|
|
612
|
+
performance.mark('sparse-attention-end');
|
|
613
|
+
performance.measure('sparse-attention', 'sparse-attention-start', 'sparse-attention-end');
|
|
614
|
+
const measure = performance.getEntriesByName('sparse-attention')[0];
|
|
615
|
+
const executionTimeMs = measure.duration;
|
|
616
|
+
// Update statistics
|
|
617
|
+
this.metricsTracker.updateStats('sparse', attentionResult.runtime, executionTimeMs, attentionResult.output.length * 4);
|
|
618
|
+
return {
|
|
619
|
+
output: attentionResult.output,
|
|
620
|
+
weights: attentionResult.weights,
|
|
621
|
+
executionTimeMs,
|
|
622
|
+
mechanism: 'sparse',
|
|
623
|
+
runtime: attentionResult.runtime,
|
|
624
|
+
sparsityMetadata: {
|
|
625
|
+
method: sparsificationResult.method,
|
|
626
|
+
topKNodes: topK,
|
|
627
|
+
sparsityRatio: sparsificationResult.sparsityRatio
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
catch (error) {
|
|
632
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
633
|
+
throw new Error(`Sparse attention failed: ${errorMessage}`);
|
|
634
|
+
}
|
|
631
635
|
}
|
|
632
636
|
/**
|
|
633
|
-
*
|
|
637
|
+
* Compute Partitioned Attention
|
|
634
638
|
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
+
* Uses graph mincut algorithms to partition the graph, then applies
|
|
640
|
+
* attention within each partition independently. Achieves 50-80% memory
|
|
641
|
+
* reduction through intelligent clustering.
|
|
642
|
+
*
|
|
643
|
+
* @param query - Query vector for attention scoring
|
|
644
|
+
* @param graphEdges - Graph adjacency list
|
|
645
|
+
* @param options - Partitioned attention options
|
|
646
|
+
* @returns Attention result with partitioning metadata
|
|
639
647
|
*/
|
|
640
|
-
async
|
|
648
|
+
async partitionedAttention(query, graphEdges, options) {
|
|
641
649
|
if (!this.initialized) {
|
|
642
650
|
await this.initialize();
|
|
643
651
|
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
// Softmax
|
|
685
|
-
let expSum = 0;
|
|
686
|
-
for (let j = 0; j < seqLen; j++) {
|
|
687
|
-
scores[j] = Math.exp(scores[j] - maxScore);
|
|
688
|
-
expSum += scores[j];
|
|
689
|
-
}
|
|
690
|
-
for (let j = 0; j < seqLen; j++) {
|
|
691
|
-
scores[j] /= expSum;
|
|
692
|
-
}
|
|
693
|
-
// Weighted sum of values
|
|
694
|
-
const output = new Array(dim).fill(0);
|
|
695
|
-
for (let j = 0; j < seqLen; j++) {
|
|
696
|
-
const vj = values[j];
|
|
697
|
-
const w = scores[j];
|
|
698
|
-
for (let d = 0; d < dim; d++) {
|
|
699
|
-
output[d] += w * vj[d];
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
this.updateStats('flash', 'fallback', 0, dim * 4);
|
|
703
|
-
return output;
|
|
704
|
-
}
|
|
705
|
-
/**
|
|
706
|
-
* JS fallback for Multi-Head Attention.
|
|
707
|
-
*/
|
|
708
|
-
applyMultiHeadJS(query, context, numHeads) {
|
|
709
|
-
const dim = query.length;
|
|
710
|
-
const seqLen = context.length;
|
|
711
|
-
const headDim = Math.max(1, Math.floor(dim / numHeads));
|
|
712
|
-
const scale = 1.0 / Math.sqrt(headDim);
|
|
713
|
-
// Per-head attention
|
|
714
|
-
const output = new Array(dim).fill(0);
|
|
715
|
-
const allWeights = [];
|
|
716
|
-
for (let h = 0; h < numHeads; h++) {
|
|
717
|
-
const hStart = h * headDim;
|
|
718
|
-
const hEnd = Math.min(hStart + headDim, dim);
|
|
719
|
-
const headWeights = [];
|
|
720
|
-
// Compute scores for this head
|
|
721
|
-
const scores = [];
|
|
722
|
-
let maxScore = -Infinity;
|
|
723
|
-
for (let j = 0; j < seqLen; j++) {
|
|
724
|
-
let dot = 0;
|
|
725
|
-
for (let d = hStart; d < hEnd; d++) {
|
|
726
|
-
dot += query[d] * context[j][d];
|
|
652
|
+
performance.mark('partitioned-attention-start');
|
|
653
|
+
try {
|
|
654
|
+
const numNodes = graphEdges.length;
|
|
655
|
+
// For small graphs, fallback to dense attention
|
|
656
|
+
if (numNodes < 1000) {
|
|
657
|
+
console.warn(`⚠️ Graph size ${numNodes} < 1000, using dense attention`);
|
|
658
|
+
const dummyKey = new Float32Array(query.length);
|
|
659
|
+
const dummyValue = new Float32Array(query.length);
|
|
660
|
+
return this.multiHeadAttention(query, dummyKey, dummyValue);
|
|
661
|
+
}
|
|
662
|
+
// Initialize mincut service if not already
|
|
663
|
+
if (!this.mincutService) {
|
|
664
|
+
const cfg = this.configManager.getConfig();
|
|
665
|
+
this.mincutService = new MincutService({
|
|
666
|
+
algorithm: options?.method || cfg.partitioning?.method || 'stoer-wagner',
|
|
667
|
+
maxPartitionSize: options?.maxPartitionSize || cfg.partitioning?.maxPartitionSize || 1000
|
|
668
|
+
});
|
|
669
|
+
await this.mincutService.initialize();
|
|
670
|
+
}
|
|
671
|
+
// Partition the graph
|
|
672
|
+
const partitionResult = await this.mincutService.partition(graphEdges);
|
|
673
|
+
// Get partition statistics
|
|
674
|
+
const stats = this.mincutService.getPartitionStats(partitionResult, graphEdges);
|
|
675
|
+
// Compute attention within each partition
|
|
676
|
+
const embedDim = this.configManager.getEmbedDim();
|
|
677
|
+
const partitionOutputs = [];
|
|
678
|
+
for (const partition of partitionResult.partitions) {
|
|
679
|
+
const partitionSize = partition.length;
|
|
680
|
+
const partitionQuery = new Float32Array(partitionSize * embedDim);
|
|
681
|
+
const partitionKey = new Float32Array(partitionSize * embedDim);
|
|
682
|
+
const partitionValue = new Float32Array(partitionSize * embedDim);
|
|
683
|
+
// Build partition matrices (simple: use node indices as embeddings)
|
|
684
|
+
for (let i = 0; i < partitionSize; i++) {
|
|
685
|
+
const nodeId = partition[i];
|
|
686
|
+
const value = nodeId < query.length ? query[nodeId] : 0;
|
|
687
|
+
for (let d = 0; d < embedDim; d++) {
|
|
688
|
+
partitionQuery[i * embedDim + d] = value;
|
|
689
|
+
partitionKey[i * embedDim + d] = value;
|
|
690
|
+
partitionValue[i * embedDim + d] = value;
|
|
691
|
+
}
|
|
727
692
|
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
693
|
+
// Run attention on this partition
|
|
694
|
+
const partitionResult = await this.multiHeadAttention(partitionQuery, partitionKey, partitionValue);
|
|
695
|
+
partitionOutputs.push(partitionResult.output);
|
|
696
|
+
}
|
|
697
|
+
// Merge partition outputs (simple concatenation)
|
|
698
|
+
const totalOutputSize = partitionOutputs.reduce((sum, output) => sum + output.length, 0);
|
|
699
|
+
const mergedOutput = new Float32Array(totalOutputSize);
|
|
700
|
+
let offset = 0;
|
|
701
|
+
for (const output of partitionOutputs) {
|
|
702
|
+
mergedOutput.set(output, offset);
|
|
703
|
+
offset += output.length;
|
|
704
|
+
}
|
|
705
|
+
performance.mark('partitioned-attention-end');
|
|
706
|
+
performance.measure('partitioned-attention', 'partitioned-attention-start', 'partitioned-attention-end');
|
|
707
|
+
const measure = performance.getEntriesByName('partitioned-attention')[0];
|
|
708
|
+
const executionTimeMs = measure.duration;
|
|
709
|
+
// Update statistics
|
|
710
|
+
this.metricsTracker.updateStats('partitioned', 'fallback', executionTimeMs, mergedOutput.length * 4);
|
|
711
|
+
return {
|
|
712
|
+
output: mergedOutput,
|
|
713
|
+
executionTimeMs,
|
|
714
|
+
mechanism: 'partitioned',
|
|
715
|
+
runtime: 'fallback',
|
|
716
|
+
partitioningMetadata: {
|
|
717
|
+
numPartitions: stats.numPartitions,
|
|
718
|
+
cutSize: partitionResult.cutSize,
|
|
719
|
+
avgPartitionSize: stats.avgPartitionSize
|
|
748
720
|
}
|
|
749
|
-
}
|
|
750
|
-
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
catch (error) {
|
|
724
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
725
|
+
throw new Error(`Partitioned attention failed: ${errorMessage}`);
|
|
751
726
|
}
|
|
752
|
-
this.updateStats('multi-head', 'fallback', 0, dim * 4);
|
|
753
|
-
return { attention: output, weights: allWeights };
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* JS fallback for Mixture of Experts.
|
|
757
|
-
*/
|
|
758
|
-
applyMoEJS(input, experts, topK) {
|
|
759
|
-
const dim = input.length;
|
|
760
|
-
const gating = this.computeGatingWeights(input, experts, topK);
|
|
761
|
-
// Simulate expert outputs (each expert applies a simple transform)
|
|
762
|
-
const output = new Array(dim).fill(0);
|
|
763
|
-
for (let e = 0; e < experts; e++) {
|
|
764
|
-
if (gating[e] === 0)
|
|
765
|
-
continue;
|
|
766
|
-
const w = gating[e];
|
|
767
|
-
for (let d = 0; d < dim; d++) {
|
|
768
|
-
// Expert transform: scaled rotation based on expert index
|
|
769
|
-
const angle = (e * Math.PI) / experts;
|
|
770
|
-
output[d] += w * (input[d] * Math.cos(angle) + (input[(d + 1) % dim] || 0) * Math.sin(angle));
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
this.updateStats('moe', 'fallback', 0, dim * 4);
|
|
774
|
-
return { output, expertWeights: gating };
|
|
775
|
-
}
|
|
776
|
-
/**
|
|
777
|
-
* Compute gating weights for MoE (top-K selection with softmax).
|
|
778
|
-
*/
|
|
779
|
-
computeGatingWeights(input, experts, topK) {
|
|
780
|
-
// Simple gating: hash-based expert scores
|
|
781
|
-
const scores = [];
|
|
782
|
-
let sum = 0;
|
|
783
|
-
for (let e = 0; e < experts; e++) {
|
|
784
|
-
let s = 0;
|
|
785
|
-
for (let d = 0; d < Math.min(input.length, 16); d++) {
|
|
786
|
-
s += input[d] * Math.sin((e + 1) * (d + 1));
|
|
787
|
-
}
|
|
788
|
-
scores.push(Math.exp(s));
|
|
789
|
-
sum += Math.exp(s);
|
|
790
|
-
}
|
|
791
|
-
// Normalize
|
|
792
|
-
for (let e = 0; e < experts; e++) {
|
|
793
|
-
scores[e] /= sum;
|
|
794
|
-
}
|
|
795
|
-
// Zero out non-top-K
|
|
796
|
-
const indexed = scores.map((s, i) => ({ s, i }));
|
|
797
|
-
indexed.sort((a, b) => b.s - a.s);
|
|
798
|
-
const topKSet = new Set(indexed.slice(0, topK).map(x => x.i));
|
|
799
|
-
const result = new Array(experts).fill(0);
|
|
800
|
-
let topSum = 0;
|
|
801
|
-
for (const idx of topKSet) {
|
|
802
|
-
result[idx] = scores[idx];
|
|
803
|
-
topSum += scores[idx];
|
|
804
|
-
}
|
|
805
|
-
// Renormalize top-K
|
|
806
|
-
if (topSum > 0) {
|
|
807
|
-
for (const idx of topKSet) {
|
|
808
|
-
result[idx] /= topSum;
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
return result;
|
|
812
|
-
}
|
|
813
|
-
/**
|
|
814
|
-
* Reshape flat weight buffer into per-context weight matrix.
|
|
815
|
-
*/
|
|
816
|
-
reshapeWeights(weights, contextLen, numHeads) {
|
|
817
|
-
const result = [];
|
|
818
|
-
const w = weights instanceof Float32Array ? weights : new Float32Array(weights);
|
|
819
|
-
const stride = Math.max(1, Math.floor(w.length / numHeads));
|
|
820
|
-
for (let h = 0; h < numHeads; h++) {
|
|
821
|
-
const headWeights = [];
|
|
822
|
-
for (let j = 0; j < contextLen; j++) {
|
|
823
|
-
const idx = h * stride + j;
|
|
824
|
-
headWeights.push(idx < w.length ? w[idx] : 0);
|
|
825
|
-
}
|
|
826
|
-
result.push(headWeights);
|
|
827
|
-
}
|
|
828
|
-
return result;
|
|
829
727
|
}
|
|
830
728
|
/**
|
|
831
|
-
*
|
|
729
|
+
* Get service information
|
|
832
730
|
*/
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
mc += ctx[d] * ctx[d];
|
|
842
|
-
}
|
|
843
|
-
const denom = Math.sqrt(mq) * Math.sqrt(mc);
|
|
844
|
-
weights[0].push(denom > 0 ? dot / denom : 0);
|
|
845
|
-
}
|
|
846
|
-
return weights;
|
|
731
|
+
getInfo() {
|
|
732
|
+
return {
|
|
733
|
+
initialized: this.initialized,
|
|
734
|
+
runtime: this.wasmManager.getRuntime(),
|
|
735
|
+
hasNAPI: this.wasmManager.hasNAPI(),
|
|
736
|
+
hasWASM: this.wasmManager.hasWASM(),
|
|
737
|
+
config: this.configManager.getConfig()
|
|
738
|
+
};
|
|
847
739
|
}
|
|
848
740
|
}
|
|
849
741
|
//# sourceMappingURL=AttentionService.js.map
|