kynjal-cli 3.1.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/agents/core/coder.md +1 -1
- package/.claude/agents/core/planner.md +2 -2
- package/.claude/agents/core/researcher.md +1 -1
- package/.claude/agents/core/reviewer.md +1 -1
- package/.claude/agents/core/tester.md +1 -1
- package/.claude/agents/data/data-ml-model.md +4 -4
- package/.claude/agents/development/dev-backend-api.md +4 -4
- package/.claude/agents/documentation/docs-api-openapi.md +4 -4
- package/.claude/agents/github/code-review-swarm.md +2 -2
- package/.claude/agents/github/issue-tracker.md +2 -2
- package/.claude/agents/github/pr-manager.md +2 -2
- package/.claude/agents/github/release-manager.md +2 -2
- package/.claude/agents/github/workflow-automation.md +2 -2
- package/.claude/agents/sparc/architecture.md +3 -3
- package/.claude/agents/sparc/pseudocode.md +2 -2
- package/.claude/agents/sparc/refinement.md +3 -3
- package/.claude/agents/sparc/specification.md +2 -2
- package/.claude/agents/swarm/adaptive-coordinator.md +1 -1
- package/.claude/agents/swarm/hierarchical-coordinator.md +1 -1
- package/.claude/agents/swarm/mesh-coordinator.md +1 -1
- package/.claude/agents/templates/base-template-generator.md +25 -4
- package/.claude/agents/templates/sparc-coordinator.md +3 -3
- package/.claude/helpers/auto-commit.sh +1 -1
- package/.claude/helpers/auto-memory-hook.mjs +27 -9
- package/.claude/helpers/hook-handler.cjs +58 -18
- package/.claude/helpers/statusline.cjs +14 -33
- package/.claude/helpers/statusline.js +3 -3
- package/.claude/settings.json +9 -9
- package/.claude/skills/reasoningbank-intelligence/SKILL.md +2 -2
- package/.claude/skills/swarm-orchestration/SKILL.md +1 -1
- package/README.md +383 -170
- package/bin/cli.js +6 -6
- package/bin/mcp-server.js +1 -1
- package/bin/preinstall.cjs +2 -0
- package/dist/src/appliance/gguf-engine.js +664 -0
- package/dist/src/appliance/gguf-engine.js.map +1 -0
- package/dist/src/appliance/ruvllm-bridge.js +492 -0
- package/dist/src/appliance/ruvllm-bridge.js.map +1 -0
- package/dist/src/appliance/rvfa-builder.js +383 -0
- package/dist/src/appliance/rvfa-builder.js.map +1 -0
- package/dist/src/appliance/rvfa-distribution.js +533 -0
- package/dist/src/appliance/rvfa-distribution.js.map +1 -0
- package/dist/src/appliance/rvfa-format.js +465 -0
- package/dist/src/appliance/rvfa-format.js.map +1 -0
- package/dist/src/appliance/rvfa-runner.js +373 -0
- package/dist/src/appliance/rvfa-runner.js.map +1 -0
- package/dist/src/appliance/rvfa-signing.js +469 -0
- package/dist/src/appliance/rvfa-signing.js.map +1 -0
- package/dist/src/benchmarks/pretrain/index.js +542 -331
- package/dist/src/benchmarks/pretrain/index.js.map +1 -1
- package/dist/src/commands/agent.d.ts.map +1 -1
- package/dist/src/commands/agent.js +725 -502
- package/dist/src/commands/agent.js.map +1 -1
- package/dist/src/commands/analyze.js +1548 -1218
- package/dist/src/commands/analyze.js.map +1 -1
- package/dist/src/commands/appliance-advanced.d.ts +9 -0
- package/dist/src/commands/appliance-advanced.d.ts.map +1 -0
- package/dist/src/commands/appliance-advanced.js +324 -0
- package/dist/src/commands/appliance-advanced.js.map +1 -0
- package/dist/src/commands/appliance.d.ts +8 -0
- package/dist/src/commands/appliance.d.ts.map +1 -0
- package/dist/src/commands/appliance.js +581 -0
- package/dist/src/commands/appliance.js.map +1 -0
- package/dist/src/commands/benchmark.js +523 -372
- package/dist/src/commands/benchmark.js.map +1 -1
- package/dist/src/commands/claims.js +364 -274
- package/dist/src/commands/claims.js.map +1 -1
- package/dist/src/commands/cleanup.d.ts +13 -0
- package/dist/src/commands/cleanup.d.ts.map +1 -0
- package/dist/src/commands/cleanup.js +262 -0
- package/dist/src/commands/cleanup.js.map +1 -0
- package/dist/src/commands/completions.js +118 -477
- package/dist/src/commands/completions.js.map +1 -1
- package/dist/src/commands/config.js +303 -237
- package/dist/src/commands/config.js.map +1 -1
- package/dist/src/commands/daemon.d.ts.map +1 -1
- package/dist/src/commands/daemon.js +597 -425
- package/dist/src/commands/daemon.js.map +1 -1
- package/dist/src/commands/deployment.js +275 -194
- package/dist/src/commands/deployment.js.map +1 -1
- package/dist/src/commands/doctor.d.ts.map +1 -1
- package/dist/src/commands/doctor.js +690 -460
- package/dist/src/commands/doctor.js.map +1 -1
- package/dist/src/commands/embeddings.js +1543 -1293
- package/dist/src/commands/embeddings.js.map +1 -1
- package/dist/src/commands/guidance.js +596 -449
- package/dist/src/commands/guidance.js.map +1 -1
- package/dist/src/commands/hive-mind.js +938 -854
- package/dist/src/commands/hive-mind.js.map +1 -1
- package/dist/src/commands/hooks.d.ts.map +1 -1
- package/dist/src/commands/hooks.js +3677 -2570
- package/dist/src/commands/hooks.js.map +1 -1
- package/dist/src/commands/index.js +322 -122
- package/dist/src/commands/index.js.map +1 -1
- package/dist/src/commands/init.d.ts +1 -1
- package/dist/src/commands/init.d.ts.map +1 -1
- package/dist/src/commands/init.js +943 -787
- package/dist/src/commands/init.js.map +1 -1
- package/dist/src/commands/issues.js +558 -383
- package/dist/src/commands/issues.js.map +1 -1
- package/dist/src/commands/mcp.d.ts.map +1 -1
- package/dist/src/commands/mcp.js +605 -475
- package/dist/src/commands/mcp.js.map +1 -1
- package/dist/src/commands/memory.d.ts.map +1 -1
- package/dist/src/commands/memory.js +1031 -814
- package/dist/src/commands/memory.js.map +1 -1
- package/dist/src/commands/migrate.js +347 -282
- package/dist/src/commands/migrate.js.map +1 -1
- package/dist/src/commands/neural.d.ts.map +1 -1
- package/dist/src/commands/neural.js +1563 -1283
- package/dist/src/commands/neural.js.map +1 -1
- package/dist/src/commands/performance.js +643 -497
- package/dist/src/commands/performance.js.map +1 -1
- package/dist/src/commands/plugins.js +841 -668
- package/dist/src/commands/plugins.js.map +1 -1
- package/dist/src/commands/process.js +447 -392
- package/dist/src/commands/process.js.map +1 -1
- package/dist/src/commands/progress.js +256 -162
- package/dist/src/commands/progress.js.map +1 -1
- package/dist/src/commands/providers.js +220 -150
- package/dist/src/commands/providers.js.map +1 -1
- package/dist/src/commands/route.js +665 -520
- package/dist/src/commands/route.js.map +1 -1
- package/dist/src/commands/ruvector/backup.js +651 -505
- package/dist/src/commands/ruvector/backup.js.map +1 -1
- package/dist/src/commands/ruvector/benchmark.js +401 -349
- package/dist/src/commands/ruvector/benchmark.js.map +1 -1
- package/dist/src/commands/ruvector/import.js +267 -225
- package/dist/src/commands/ruvector/import.js.map +1 -1
- package/dist/src/commands/ruvector/index.js +75 -37
- package/dist/src/commands/ruvector/index.js.map +1 -1
- package/dist/src/commands/ruvector/init.js +359 -336
- package/dist/src/commands/ruvector/init.js.map +1 -1
- package/dist/src/commands/ruvector/migrate.js +322 -335
- package/dist/src/commands/ruvector/migrate.js.map +1 -1
- package/dist/src/commands/ruvector/optimize.js +431 -375
- package/dist/src/commands/ruvector/optimize.js.map +1 -1
- package/dist/src/commands/ruvector/setup.js +117 -703
- package/dist/src/commands/ruvector/setup.js.map +1 -1
- package/dist/src/commands/ruvector/status.js +419 -364
- package/dist/src/commands/ruvector/status.js.map +1 -1
- package/dist/src/commands/security.d.ts.map +1 -1
- package/dist/src/commands/security.js +610 -456
- package/dist/src/commands/security.js.map +1 -1
- package/dist/src/commands/session.d.ts +1 -1
- package/dist/src/commands/session.js +627 -505
- package/dist/src/commands/session.js.map +1 -1
- package/dist/src/commands/start.d.ts +1 -1
- package/dist/src/commands/start.js +368 -271
- package/dist/src/commands/start.js.map +1 -1
- package/dist/src/commands/status.d.ts +1 -1
- package/dist/src/commands/status.d.ts.map +1 -1
- package/dist/src/commands/status.js +492 -379
- package/dist/src/commands/status.js.map +1 -1
- package/dist/src/commands/swarm.js +488 -408
- package/dist/src/commands/swarm.js.map +1 -1
- package/dist/src/commands/task.d.ts +1 -1
- package/dist/src/commands/task.js +539 -424
- package/dist/src/commands/task.js.map +1 -1
- package/dist/src/commands/transfer-store.js +412 -322
- package/dist/src/commands/transfer-store.js.map +1 -1
- package/dist/src/commands/update.js +291 -196
- package/dist/src/commands/update.js.map +1 -1
- package/dist/src/commands/workflow.js +486 -386
- package/dist/src/commands/workflow.js.map +1 -1
- package/dist/src/config-adapter.js +40 -39
- package/dist/src/config-adapter.js.map +1 -1
- package/dist/src/index.js +416 -312
- package/dist/src/index.js.map +1 -1
- package/dist/src/infrastructure/in-memory-repositories.js +507 -246
- package/dist/src/infrastructure/in-memory-repositories.js.map +1 -1
- package/dist/src/init/claudemd-generator.js +78 -368
- package/dist/src/init/claudemd-generator.js.map +1 -1
- package/dist/src/init/executor.js +1019 -1345
- package/dist/src/init/executor.js.map +1 -1
- package/dist/src/init/helpers-generator.js +60 -635
- package/dist/src/init/helpers-generator.js.map +1 -1
- package/dist/src/init/index.d.ts +1 -1
- package/dist/src/init/index.d.ts.map +1 -1
- package/dist/src/init/index.js +1 -1
- package/dist/src/init/index.js.map +1 -1
- package/dist/src/init/mcp-generator.d.ts +0 -1
- package/dist/src/init/mcp-generator.d.ts.map +1 -1
- package/dist/src/init/mcp-generator.js +62 -42
- package/dist/src/init/mcp-generator.js.map +1 -1
- package/dist/src/init/settings-generator.d.ts.map +1 -1
- package/dist/src/init/settings-generator.js +167 -100
- package/dist/src/init/settings-generator.js.map +1 -1
- package/dist/src/init/statusline-generator.d.ts +16 -8
- package/dist/src/init/statusline-generator.d.ts.map +1 -1
- package/dist/src/init/statusline-generator.js +20 -1300
- package/dist/src/init/statusline-generator.js.map +1 -1
- package/dist/src/init/types.d.ts +15 -5
- package/dist/src/init/types.d.ts.map +1 -1
- package/dist/src/init/types.js +66 -76
- package/dist/src/init/types.js.map +1 -1
- package/dist/src/mcp-client.js +130 -76
- package/dist/src/mcp-client.js.map +1 -1
- package/dist/src/mcp-server.js +758 -445
- package/dist/src/mcp-server.js.map +1 -1
- package/dist/src/mcp-tools/agent-tools.js +492 -391
- package/dist/src/mcp-tools/agent-tools.js.map +1 -1
- package/dist/src/mcp-tools/agentdb-tools.d.ts +30 -0
- package/dist/src/mcp-tools/agentdb-tools.d.ts.map +1 -0
- package/dist/src/mcp-tools/agentdb-tools.js +758 -0
- package/dist/src/mcp-tools/agentdb-tools.js.map +1 -0
- package/dist/src/mcp-tools/analyze-tools.js +236 -172
- package/dist/src/mcp-tools/analyze-tools.js.map +1 -1
- package/dist/src/mcp-tools/auto-install.js +142 -80
- package/dist/src/mcp-tools/auto-install.js.map +1 -1
- package/dist/src/mcp-tools/browser-tools.js +375 -252
- package/dist/src/mcp-tools/browser-tools.js.map +1 -1
- package/dist/src/mcp-tools/claims-tools.js +565 -473
- package/dist/src/mcp-tools/claims-tools.js.map +1 -1
- package/dist/src/mcp-tools/config-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/config-tools.js +284 -190
- package/dist/src/mcp-tools/config-tools.js.map +1 -1
- package/dist/src/mcp-tools/coordination-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/coordination-tools.js +600 -349
- package/dist/src/mcp-tools/coordination-tools.js.map +1 -1
- package/dist/src/mcp-tools/daa-tools.js +367 -289
- package/dist/src/mcp-tools/daa-tools.js.map +1 -1
- package/dist/src/mcp-tools/embeddings-tools.js +693 -582
- package/dist/src/mcp-tools/embeddings-tools.js.map +1 -1
- package/dist/src/mcp-tools/github-tools.js +312 -261
- package/dist/src/mcp-tools/github-tools.js.map +1 -1
- package/dist/src/mcp-tools/hive-mind-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/hive-mind-tools.js +718 -423
- package/dist/src/mcp-tools/hive-mind-tools.js.map +1 -1
- package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/hooks-tools.js +2726 -1978
- package/dist/src/mcp-tools/hooks-tools.js.map +1 -1
- package/dist/src/mcp-tools/index.d.ts +2 -0
- package/dist/src/mcp-tools/index.d.ts.map +1 -1
- package/dist/src/mcp-tools/index.js +2 -0
- package/dist/src/mcp-tools/index.js.map +1 -1
- package/dist/src/mcp-tools/memory-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/memory-tools.js +514 -329
- package/dist/src/mcp-tools/memory-tools.js.map +1 -1
- package/dist/src/mcp-tools/neural-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/neural-tools.js +428 -326
- package/dist/src/mcp-tools/neural-tools.js.map +1 -1
- package/dist/src/mcp-tools/performance-tools.js +480 -420
- package/dist/src/mcp-tools/performance-tools.js.map +1 -1
- package/dist/src/mcp-tools/progress-tools.js +278 -204
- package/dist/src/mcp-tools/progress-tools.js.map +1 -1
- package/dist/src/mcp-tools/ruvllm-tools.d.ts +9 -0
- package/dist/src/mcp-tools/ruvllm-tools.d.ts.map +1 -0
- package/dist/src/mcp-tools/ruvllm-tools.js +399 -0
- package/dist/src/mcp-tools/ruvllm-tools.js.map +1 -0
- package/dist/src/mcp-tools/security-tools.js +429 -297
- package/dist/src/mcp-tools/security-tools.js.map +1 -1
- package/dist/src/mcp-tools/session-tools.js +234 -185
- package/dist/src/mcp-tools/session-tools.js.map +1 -1
- package/dist/src/mcp-tools/swarm-tools.d.ts +2 -1
- package/dist/src/mcp-tools/swarm-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/swarm-tools.js +303 -64
- package/dist/src/mcp-tools/swarm-tools.js.map +1 -1
- package/dist/src/mcp-tools/system-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/system-tools.js +352 -200
- package/dist/src/mcp-tools/system-tools.js.map +1 -1
- package/dist/src/mcp-tools/task-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/task-tools.js +357 -189
- package/dist/src/mcp-tools/task-tools.js.map +1 -1
- package/dist/src/mcp-tools/terminal-tools.js +196 -148
- package/dist/src/mcp-tools/terminal-tools.js.map +1 -1
- package/dist/src/mcp-tools/transfer-tools.js +333 -186
- package/dist/src/mcp-tools/transfer-tools.js.map +1 -1
- package/dist/src/mcp-tools/wasm-agent-tools.d.ts +9 -0
- package/dist/src/mcp-tools/wasm-agent-tools.d.ts.map +1 -0
- package/dist/src/mcp-tools/wasm-agent-tools.js +377 -0
- package/dist/src/mcp-tools/wasm-agent-tools.js.map +1 -0
- package/dist/src/mcp-tools/workflow-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/workflow-tools.js +471 -335
- package/dist/src/mcp-tools/workflow-tools.js.map +1 -1
- package/dist/src/memory/ewc-consolidation.js +345 -173
- package/dist/src/memory/ewc-consolidation.js.map +1 -1
- package/dist/src/memory/intelligence.js +841 -359
- package/dist/src/memory/intelligence.js.map +1 -1
- package/dist/src/memory/memory-bridge.js +1964 -0
- package/dist/src/memory/memory-bridge.js.map +1 -0
- package/dist/src/memory/memory-initializer.js +1895 -1602
- package/dist/src/memory/memory-initializer.js.map +1 -1
- package/dist/src/memory/sona-optimizer.js +329 -199
- package/dist/src/memory/sona-optimizer.js.map +1 -1
- package/dist/src/output.d.ts +2 -2
- package/dist/src/output.d.ts.map +1 -1
- package/dist/src/output.js +273 -242
- package/dist/src/output.js.map +1 -1
- package/dist/src/parser.js +217 -124
- package/dist/src/parser.js.map +1 -1
- package/dist/src/plugins/manager.js +531 -278
- package/dist/src/plugins/manager.js.map +1 -1
- package/dist/src/plugins/store/discovery.js +362 -275
- package/dist/src/plugins/store/discovery.js.map +1 -1
- package/dist/src/plugins/store/index.js +105 -48
- package/dist/src/plugins/store/index.js.map +1 -1
- package/dist/src/plugins/store/search.js +107 -69
- package/dist/src/plugins/store/search.js.map +1 -1
- package/dist/src/plugins/tests/demo-plugin-store.js +160 -113
- package/dist/src/plugins/tests/demo-plugin-store.js.map +1 -1
- package/dist/src/plugins/tests/standalone-test.js +223 -172
- package/dist/src/plugins/tests/standalone-test.js.map +1 -1
- package/dist/src/plugins/tests/test-plugin-store.js +228 -190
- package/dist/src/plugins/tests/test-plugin-store.js.map +1 -1
- package/dist/src/production/circuit-breaker.js +126 -62
- package/dist/src/production/circuit-breaker.js.map +1 -1
- package/dist/src/production/error-handler.js +156 -86
- package/dist/src/production/error-handler.js.map +1 -1
- package/dist/src/production/monitoring.js +220 -139
- package/dist/src/production/monitoring.js.map +1 -1
- package/dist/src/production/rate-limiter.js +93 -74
- package/dist/src/production/rate-limiter.js.map +1 -1
- package/dist/src/production/retry.js +167 -75
- package/dist/src/production/retry.js.map +1 -1
- package/dist/src/prompt.js +560 -436
- package/dist/src/prompt.js.map +1 -1
- package/dist/src/runtime/headless.js +289 -200
- package/dist/src/runtime/headless.js.map +1 -1
- package/dist/src/ruvector/agent-wasm.js +511 -0
- package/dist/src/ruvector/agent-wasm.js.map +1 -0
- package/dist/src/ruvector/ast-analyzer.js +232 -145
- package/dist/src/ruvector/ast-analyzer.js.map +1 -1
- package/dist/src/ruvector/coverage-router.js +419 -287
- package/dist/src/ruvector/coverage-router.js.map +1 -1
- package/dist/src/ruvector/coverage-tools.js +101 -56
- package/dist/src/ruvector/coverage-tools.js.map +1 -1
- package/dist/src/ruvector/diff-classifier.js +451 -324
- package/dist/src/ruvector/diff-classifier.js.map +1 -1
- package/dist/src/ruvector/enhanced-model-router.js +337 -251
- package/dist/src/ruvector/enhanced-model-router.js.map +1 -1
- package/dist/src/ruvector/flash-attention.js +254 -223
- package/dist/src/ruvector/flash-attention.js.map +1 -1
- package/dist/src/ruvector/graph-analyzer.js +680 -486
- package/dist/src/ruvector/graph-analyzer.js.map +1 -1
- package/dist/src/ruvector/index.js +113 -27
- package/dist/src/ruvector/index.js.map +1 -1
- package/dist/src/ruvector/lora-adapter.js +248 -155
- package/dist/src/ruvector/lora-adapter.js.map +1 -1
- package/dist/src/ruvector/model-router.js +248 -175
- package/dist/src/ruvector/model-router.js.map +1 -1
- package/dist/src/ruvector/moe-router.js +286 -228
- package/dist/src/ruvector/moe-router.js.map +1 -1
- package/dist/src/ruvector/q-learning-router.js +338 -257
- package/dist/src/ruvector/q-learning-router.js.map +1 -1
- package/dist/src/ruvector/ruvllm-wasm.js +527 -0
- package/dist/src/ruvector/ruvllm-wasm.js.map +1 -0
- package/dist/src/ruvector/semantic-router.js +67 -60
- package/dist/src/ruvector/semantic-router.js.map +1 -1
- package/dist/src/ruvector/vector-db.js +205 -119
- package/dist/src/ruvector/vector-db.js.map +1 -1
- package/dist/src/services/agentic-flow-bridge.js +168 -0
- package/dist/src/services/agentic-flow-bridge.js.map +1 -0
- package/dist/src/services/claim-service.js +940 -615
- package/dist/src/services/claim-service.js.map +1 -1
- package/dist/src/services/container-worker-pool.js +669 -399
- package/dist/src/services/container-worker-pool.js.map +1 -1
- package/dist/src/services/headless-worker-executor.js +467 -441
- package/dist/src/services/headless-worker-executor.js.map +1 -1
- package/dist/src/services/index.d.ts +5 -5
- package/dist/src/services/index.d.ts.map +1 -1
- package/dist/src/services/index.js +4 -4
- package/dist/src/services/index.js.map +1 -1
- package/dist/src/services/registry-api.js +201 -93
- package/dist/src/services/registry-api.js.map +1 -1
- package/dist/src/services/ruvector-training.js +414 -144
- package/dist/src/services/ruvector-training.js.map +1 -1
- package/dist/src/services/worker-daemon.js +928 -531
- package/dist/src/services/worker-daemon.js.map +1 -1
- package/dist/src/services/worker-queue.js +550 -331
- package/dist/src/services/worker-queue.js.map +1 -1
- package/dist/src/suggest.js +55 -45
- package/dist/src/suggest.js.map +1 -1
- package/dist/src/transfer/anonymization/index.js +37 -29
- package/dist/src/transfer/anonymization/index.js.map +1 -1
- package/dist/src/transfer/deploy-seraphine.d.ts +1 -1
- package/dist/src/transfer/deploy-seraphine.js +156 -129
- package/dist/src/transfer/deploy-seraphine.js.map +1 -1
- package/dist/src/transfer/export.js +142 -84
- package/dist/src/transfer/export.js.map +1 -1
- package/dist/src/transfer/index.d.ts +1 -1
- package/dist/src/transfer/index.d.ts.map +1 -1
- package/dist/src/transfer/index.js +2 -0
- package/dist/src/transfer/index.js.map +1 -1
- package/dist/src/transfer/ipfs/client.js +337 -179
- package/dist/src/transfer/ipfs/client.js.map +1 -1
- package/dist/src/transfer/ipfs/upload.js +434 -290
- package/dist/src/transfer/ipfs/upload.js.map +1 -1
- package/dist/src/transfer/models/seraphine.js +58 -58
- package/dist/src/transfer/models/seraphine.js.map +1 -1
- package/dist/src/transfer/serialization/cfp.js +37 -33
- package/dist/src/transfer/serialization/cfp.js.map +1 -1
- package/dist/src/transfer/storage/gcs.js +248 -139
- package/dist/src/transfer/storage/gcs.js.map +1 -1
- package/dist/src/transfer/store/discovery.js +353 -243
- package/dist/src/transfer/store/discovery.js.map +1 -1
- package/dist/src/transfer/store/download.js +365 -243
- package/dist/src/transfer/store/download.js.map +1 -1
- package/dist/src/transfer/store/index.js +130 -63
- package/dist/src/transfer/store/index.js.map +1 -1
- package/dist/src/transfer/store/publish.js +258 -184
- package/dist/src/transfer/store/publish.js.map +1 -1
- package/dist/src/transfer/store/registry.js +73 -51
- package/dist/src/transfer/store/registry.js.map +1 -1
- package/dist/src/transfer/store/search.js +96 -64
- package/dist/src/transfer/store/search.js.map +1 -1
- package/dist/src/transfer/store/tests/standalone-test.js +231 -174
- package/dist/src/transfer/store/tests/standalone-test.js.map +1 -1
- package/dist/src/transfer/test-seraphine.js +130 -95
- package/dist/src/transfer/test-seraphine.js.map +1 -1
- package/dist/src/transfer/tests/test-store.js +239 -194
- package/dist/src/transfer/tests/test-store.js.map +1 -1
- package/dist/src/types.js +56 -27
- package/dist/src/types.js.map +1 -1
- package/dist/src/update/checker.js +183 -106
- package/dist/src/update/checker.js.map +1 -1
- package/dist/src/update/executor.js +198 -135
- package/dist/src/update/executor.js.map +1 -1
- package/dist/src/update/index.js +85 -38
- package/dist/src/update/index.js.map +1 -1
- package/dist/src/update/rate-limiter.js +31 -19
- package/dist/src/update/rate-limiter.js.map +1 -1
- package/dist/src/update/validator.js +64 -38
- package/dist/src/update/validator.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -10
- package/.claude/agents/custom/accessibility-auditor.yaml +0 -56
- package/.claude/agents/custom/design-architect.yaml +0 -48
- package/.claude/agents/custom/ui-developer.yaml +0 -46
- package/.claude/agents/custom/ux-researcher.yaml +0 -60
- package/dist/src/benchmarks/pretrain/index.d.ts +0 -58
- package/dist/src/benchmarks/pretrain/index.d.ts.map +0 -1
- package/dist/src/commands/index.d.ts +0 -108
- package/dist/src/commands/index.d.ts.map +0 -1
- package/dist/src/config-adapter.d.ts +0 -15
- package/dist/src/config-adapter.d.ts.map +0 -1
- package/dist/src/index.d.ts +0 -76
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/infrastructure/in-memory-repositories.d.ts +0 -68
- package/dist/src/infrastructure/in-memory-repositories.d.ts.map +0 -1
- package/dist/src/init/claudemd-generator.d.ts +0 -25
- package/dist/src/init/claudemd-generator.d.ts.map +0 -1
- package/dist/src/init/executor.d.ts +0 -41
- package/dist/src/init/executor.d.ts.map +0 -1
- package/dist/src/init/helpers-generator.d.ts +0 -60
- package/dist/src/init/helpers-generator.d.ts.map +0 -1
- package/dist/src/mcp-client.d.ts +0 -92
- package/dist/src/mcp-client.d.ts.map +0 -1
- package/dist/src/mcp-server.d.ts +0 -161
- package/dist/src/mcp-server.d.ts.map +0 -1
- package/dist/src/mcp-tools/auto-install.d.ts +0 -83
- package/dist/src/mcp-tools/auto-install.d.ts.map +0 -1
- package/dist/src/mcp-tools/types.d.ts +0 -31
- package/dist/src/mcp-tools/types.d.ts.map +0 -1
- package/dist/src/memory/ewc-consolidation.d.ts +0 -271
- package/dist/src/memory/ewc-consolidation.d.ts.map +0 -1
- package/dist/src/memory/intelligence.d.ts +0 -285
- package/dist/src/memory/intelligence.d.ts.map +0 -1
- package/dist/src/memory/memory-initializer.d.ts +0 -396
- package/dist/src/memory/memory-initializer.d.ts.map +0 -1
- package/dist/src/memory/sona-optimizer.d.ts +0 -227
- package/dist/src/memory/sona-optimizer.d.ts.map +0 -1
- package/dist/src/parser.d.ts +0 -41
- package/dist/src/parser.d.ts.map +0 -1
- package/dist/src/plugins/manager.d.ts +0 -133
- package/dist/src/plugins/manager.d.ts.map +0 -1
- package/dist/src/plugins/store/discovery.d.ts +0 -88
- package/dist/src/plugins/store/discovery.d.ts.map +0 -1
- package/dist/src/plugins/store/index.d.ts +0 -76
- package/dist/src/plugins/store/index.d.ts.map +0 -1
- package/dist/src/plugins/store/search.d.ts +0 -46
- package/dist/src/plugins/store/search.d.ts.map +0 -1
- package/dist/src/plugins/store/types.d.ts +0 -274
- package/dist/src/plugins/store/types.d.ts.map +0 -1
- package/dist/src/production/circuit-breaker.d.ts +0 -101
- package/dist/src/production/circuit-breaker.d.ts.map +0 -1
- package/dist/src/production/error-handler.d.ts +0 -92
- package/dist/src/production/error-handler.d.ts.map +0 -1
- package/dist/src/production/monitoring.d.ts +0 -161
- package/dist/src/production/monitoring.d.ts.map +0 -1
- package/dist/src/production/rate-limiter.d.ts +0 -80
- package/dist/src/production/rate-limiter.d.ts.map +0 -1
- package/dist/src/production/retry.d.ts +0 -48
- package/dist/src/production/retry.d.ts.map +0 -1
- package/dist/src/prompt.d.ts +0 -44
- package/dist/src/prompt.d.ts.map +0 -1
- package/dist/src/runtime/headless.d.ts +0 -60
- package/dist/src/runtime/headless.d.ts.map +0 -1
- package/dist/src/ruvector/ast-analyzer.d.ts +0 -67
- package/dist/src/ruvector/ast-analyzer.d.ts.map +0 -1
- package/dist/src/ruvector/coverage-router.d.ts +0 -160
- package/dist/src/ruvector/coverage-router.d.ts.map +0 -1
- package/dist/src/ruvector/diff-classifier.d.ts +0 -175
- package/dist/src/ruvector/diff-classifier.d.ts.map +0 -1
- package/dist/src/ruvector/enhanced-model-router.d.ts +0 -146
- package/dist/src/ruvector/enhanced-model-router.d.ts.map +0 -1
- package/dist/src/ruvector/flash-attention.d.ts +0 -195
- package/dist/src/ruvector/flash-attention.d.ts.map +0 -1
- package/dist/src/ruvector/graph-analyzer.d.ts +0 -187
- package/dist/src/ruvector/graph-analyzer.d.ts.map +0 -1
- package/dist/src/ruvector/index.d.ts +0 -34
- package/dist/src/ruvector/index.d.ts.map +0 -1
- package/dist/src/ruvector/lora-adapter.d.ts +0 -218
- package/dist/src/ruvector/lora-adapter.d.ts.map +0 -1
- package/dist/src/ruvector/model-router.d.ts +0 -220
- package/dist/src/ruvector/model-router.d.ts.map +0 -1
- package/dist/src/ruvector/moe-router.d.ts +0 -206
- package/dist/src/ruvector/moe-router.d.ts.map +0 -1
- package/dist/src/ruvector/q-learning-router.d.ts +0 -211
- package/dist/src/ruvector/q-learning-router.d.ts.map +0 -1
- package/dist/src/ruvector/semantic-router.d.ts +0 -77
- package/dist/src/ruvector/semantic-router.d.ts.map +0 -1
- package/dist/src/ruvector/vector-db.d.ts +0 -69
- package/dist/src/ruvector/vector-db.d.ts.map +0 -1
- package/dist/src/services/claim-service.d.ts +0 -204
- package/dist/src/services/claim-service.d.ts.map +0 -1
- package/dist/src/services/container-worker-pool.d.ts +0 -197
- package/dist/src/services/container-worker-pool.d.ts.map +0 -1
- package/dist/src/services/headless-worker-executor.d.ts +0 -304
- package/dist/src/services/headless-worker-executor.d.ts.map +0 -1
- package/dist/src/services/registry-api.d.ts +0 -58
- package/dist/src/services/registry-api.d.ts.map +0 -1
- package/dist/src/services/ruvector-training.d.ts +0 -213
- package/dist/src/services/ruvector-training.d.ts.map +0 -1
- package/dist/src/services/worker-daemon.d.ts +0 -203
- package/dist/src/services/worker-daemon.d.ts.map +0 -1
- package/dist/src/services/worker-queue.d.ts +0 -194
- package/dist/src/services/worker-queue.d.ts.map +0 -1
- package/dist/src/suggest.d.ts +0 -53
- package/dist/src/suggest.d.ts.map +0 -1
- package/dist/src/transfer/export.d.ts +0 -25
- package/dist/src/transfer/export.d.ts.map +0 -1
- package/dist/src/transfer/ipfs/client.d.ts +0 -109
- package/dist/src/transfer/ipfs/client.d.ts.map +0 -1
- package/dist/src/transfer/ipfs/upload.d.ts +0 -95
- package/dist/src/transfer/ipfs/upload.d.ts.map +0 -1
- package/dist/src/transfer/models/seraphine.d.ts +0 -72
- package/dist/src/transfer/models/seraphine.d.ts.map +0 -1
- package/dist/src/transfer/serialization/cfp.d.ts +0 -49
- package/dist/src/transfer/serialization/cfp.d.ts.map +0 -1
- package/dist/src/transfer/storage/gcs.d.ts +0 -82
- package/dist/src/transfer/storage/gcs.d.ts.map +0 -1
- package/dist/src/transfer/store/discovery.d.ts +0 -84
- package/dist/src/transfer/store/discovery.d.ts.map +0 -1
- package/dist/src/transfer/store/download.d.ts +0 -70
- package/dist/src/transfer/store/download.d.ts.map +0 -1
- package/dist/src/transfer/store/index.d.ts +0 -84
- package/dist/src/transfer/store/index.d.ts.map +0 -1
- package/dist/src/transfer/store/publish.d.ts +0 -76
- package/dist/src/transfer/store/publish.d.ts.map +0 -1
- package/dist/src/transfer/store/search.d.ts +0 -54
- package/dist/src/transfer/store/search.d.ts.map +0 -1
- package/dist/src/transfer/types.d.ts +0 -245
- package/dist/src/transfer/types.d.ts.map +0 -1
- package/dist/src/types.d.ts +0 -198
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/update/checker.d.ts +0 -34
- package/dist/src/update/checker.d.ts.map +0 -1
- package/dist/src/update/executor.d.ts +0 -32
- package/dist/src/update/executor.d.ts.map +0 -1
- package/dist/src/update/index.d.ts +0 -33
- package/dist/src/update/index.d.ts.map +0 -1
- package/dist/src/update/rate-limiter.d.ts +0 -20
- package/dist/src/update/rate-limiter.d.ts.map +0 -1
- package/dist/src/update/validator.d.ts +0 -17
- package/dist/src/update/validator.d.ts.map +0 -1
|
@@ -15,12 +15,68 @@
|
|
|
15
15
|
*
|
|
16
16
|
* @module moe-router
|
|
17
17
|
*/
|
|
18
|
+
var __assign = (this && this.__assign) || function () {
|
|
19
|
+
__assign = Object.assign || function(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
23
|
+
t[p] = s[p];
|
|
24
|
+
}
|
|
25
|
+
return t;
|
|
26
|
+
};
|
|
27
|
+
return __assign.apply(this, arguments);
|
|
28
|
+
};
|
|
29
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
30
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
31
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
32
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
33
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
34
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
35
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
39
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
40
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
41
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
42
|
+
function step(op) {
|
|
43
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
44
|
+
while (_) try {
|
|
45
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
46
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
47
|
+
switch (op[0]) {
|
|
48
|
+
case 0: case 1: t = op; break;
|
|
49
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
50
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
51
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
52
|
+
default:
|
|
53
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
54
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
55
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
56
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
57
|
+
if (t[2]) _.ops.pop();
|
|
58
|
+
_.trys.pop(); continue;
|
|
59
|
+
}
|
|
60
|
+
op = body.call(thisArg, _);
|
|
61
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
62
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
+
if (ar || !(i in from)) {
|
|
68
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
+
ar[i] = from[i];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
+
};
|
|
18
74
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
19
75
|
import { dirname } from 'path';
|
|
20
76
|
/**
|
|
21
77
|
* Expert names in order (index corresponds to expert slot)
|
|
22
78
|
*/
|
|
23
|
-
export
|
|
79
|
+
export var EXPERT_NAMES = [
|
|
24
80
|
'coder',
|
|
25
81
|
'tester',
|
|
26
82
|
'reviewer',
|
|
@@ -33,19 +89,19 @@ export const EXPERT_NAMES = [
|
|
|
33
89
|
/**
|
|
34
90
|
* Number of experts (fixed at 8)
|
|
35
91
|
*/
|
|
36
|
-
export
|
|
92
|
+
export var NUM_EXPERTS = 8;
|
|
37
93
|
/**
|
|
38
94
|
* Input dimension (384 from ONNX MiniLM-L6-v2)
|
|
39
95
|
*/
|
|
40
|
-
export
|
|
96
|
+
export var INPUT_DIM = 384;
|
|
41
97
|
/**
|
|
42
98
|
* Hidden layer dimension
|
|
43
99
|
*/
|
|
44
|
-
export
|
|
100
|
+
export var HIDDEN_DIM = 128;
|
|
45
101
|
/**
|
|
46
102
|
* Default configuration
|
|
47
103
|
*/
|
|
48
|
-
|
|
104
|
+
var DEFAULT_CONFIG = {
|
|
49
105
|
topK: 2,
|
|
50
106
|
learningRate: 0.01,
|
|
51
107
|
temperature: 1.0,
|
|
@@ -53,7 +109,7 @@ const DEFAULT_CONFIG = {
|
|
|
53
109
|
weightsPath: '.swarm/moe-weights.json',
|
|
54
110
|
autoSaveInterval: 50,
|
|
55
111
|
enableNoise: true,
|
|
56
|
-
noiseStd: 0.1
|
|
112
|
+
noiseStd: 0.1
|
|
57
113
|
};
|
|
58
114
|
// ============================================================================
|
|
59
115
|
// Matrix Operations
|
|
@@ -62,13 +118,13 @@ const DEFAULT_CONFIG = {
|
|
|
62
118
|
* Initialize weights using Xavier/Glorot initialization
|
|
63
119
|
*/
|
|
64
120
|
function xavierInit(fanIn, fanOut) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
for (
|
|
121
|
+
var std = Math.sqrt(2.0 / (fanIn + fanOut));
|
|
122
|
+
var weights = new Float32Array(fanIn * fanOut);
|
|
123
|
+
for (var i = 0; i < weights.length; i++) {
|
|
68
124
|
// Box-Muller transform for normal distribution
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
125
|
+
var u1 = Math.random();
|
|
126
|
+
var u2 = Math.random();
|
|
127
|
+
var z = Math.sqrt(-2 * Math.log(u1 + 1e-8)) * Math.cos(2 * Math.PI * u2);
|
|
72
128
|
weights[i] = z * std;
|
|
73
129
|
}
|
|
74
130
|
return weights;
|
|
@@ -78,11 +134,11 @@ function xavierInit(fanIn, fanOut) {
|
|
|
78
134
|
* W is stored row-major: [rows * cols]
|
|
79
135
|
*/
|
|
80
136
|
function matmul(W, x, rows, cols, out) {
|
|
81
|
-
for (
|
|
82
|
-
|
|
83
|
-
|
|
137
|
+
for (var i = 0; i < rows; i++) {
|
|
138
|
+
var sum = 0;
|
|
139
|
+
var rowOffset = i * cols;
|
|
84
140
|
// 4x loop unrolling for SIMD-friendly access
|
|
85
|
-
|
|
141
|
+
var j = 0;
|
|
86
142
|
for (; j + 3 < cols; j += 4) {
|
|
87
143
|
sum +=
|
|
88
144
|
W[rowOffset + j] * x[j] +
|
|
@@ -101,7 +157,7 @@ function matmul(W, x, rows, cols, out) {
|
|
|
101
157
|
* Vector addition: y = x + b
|
|
102
158
|
*/
|
|
103
159
|
function addBias(x, b, out) {
|
|
104
|
-
for (
|
|
160
|
+
for (var i = 0; i < x.length; i++) {
|
|
105
161
|
out[i] = x[i] + b[i];
|
|
106
162
|
}
|
|
107
163
|
}
|
|
@@ -109,7 +165,7 @@ function addBias(x, b, out) {
|
|
|
109
165
|
* ReLU activation: y = max(0, x)
|
|
110
166
|
*/
|
|
111
167
|
function relu(x, out) {
|
|
112
|
-
for (
|
|
168
|
+
for (var i = 0; i < x.length; i++) {
|
|
113
169
|
out[i] = x[i] > 0 ? x[i] : 0;
|
|
114
170
|
}
|
|
115
171
|
}
|
|
@@ -118,20 +174,20 @@ function relu(x, out) {
|
|
|
118
174
|
*/
|
|
119
175
|
function softmax(x, temperature, out) {
|
|
120
176
|
// Find max for numerical stability
|
|
121
|
-
|
|
122
|
-
for (
|
|
177
|
+
var maxVal = x[0];
|
|
178
|
+
for (var i = 1; i < x.length; i++) {
|
|
123
179
|
if (x[i] > maxVal)
|
|
124
180
|
maxVal = x[i];
|
|
125
181
|
}
|
|
126
182
|
// Compute exp and sum
|
|
127
|
-
|
|
128
|
-
for (
|
|
183
|
+
var sum = 0;
|
|
184
|
+
for (var i = 0; i < x.length; i++) {
|
|
129
185
|
out[i] = Math.exp((x[i] - maxVal) / temperature);
|
|
130
186
|
sum += out[i];
|
|
131
187
|
}
|
|
132
188
|
// Normalize
|
|
133
|
-
|
|
134
|
-
for (
|
|
189
|
+
var invSum = 1.0 / (sum + 1e-8);
|
|
190
|
+
for (var i = 0; i < x.length; i++) {
|
|
135
191
|
out[i] *= invSum;
|
|
136
192
|
}
|
|
137
193
|
}
|
|
@@ -139,8 +195,8 @@ function softmax(x, temperature, out) {
|
|
|
139
195
|
* Compute entropy of distribution: H = -sum(p * log(p))
|
|
140
196
|
*/
|
|
141
197
|
function entropy(p) {
|
|
142
|
-
|
|
143
|
-
for (
|
|
198
|
+
var h = 0;
|
|
199
|
+
for (var i = 0; i < p.length; i++) {
|
|
144
200
|
if (p[i] > 1e-8) {
|
|
145
201
|
h -= p[i] * Math.log(p[i]);
|
|
146
202
|
}
|
|
@@ -151,10 +207,10 @@ function entropy(p) {
|
|
|
151
207
|
* Add Gaussian noise for exploration
|
|
152
208
|
*/
|
|
153
209
|
function addNoise(x, std, out) {
|
|
154
|
-
for (
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
210
|
+
for (var i = 0; i < x.length; i++) {
|
|
211
|
+
var u1 = Math.random();
|
|
212
|
+
var u2 = Math.random();
|
|
213
|
+
var z = Math.sqrt(-2 * Math.log(u1 + 1e-8)) * Math.cos(2 * Math.PI * u2);
|
|
158
214
|
out[i] = x[i] + z * std;
|
|
159
215
|
}
|
|
160
216
|
}
|
|
@@ -170,39 +226,18 @@ function addNoise(x, std, out) {
|
|
|
170
226
|
*
|
|
171
227
|
* Uses top-k expert selection with load balancing.
|
|
172
228
|
*/
|
|
173
|
-
|
|
174
|
-
config
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
logitsWithBias; // [NUM_EXPERTS]
|
|
186
|
-
noisyLogits; // [NUM_EXPERTS]
|
|
187
|
-
probs; // [NUM_EXPERTS]
|
|
188
|
-
// Gradient buffers for online learning
|
|
189
|
-
gradW2; // [NUM_EXPERTS x HIDDEN_DIM]
|
|
190
|
-
gradb2; // [NUM_EXPERTS]
|
|
191
|
-
gradW1; // [HIDDEN_DIM x INPUT_DIM]
|
|
192
|
-
gradb1; // [HIDDEN_DIM]
|
|
193
|
-
gradHidden; // [HIDDEN_DIM]
|
|
194
|
-
// Statistics
|
|
195
|
-
routingCounts; // [NUM_EXPERTS]
|
|
196
|
-
totalRoutings = 0;
|
|
197
|
-
updateCount = 0;
|
|
198
|
-
avgReward = 0;
|
|
199
|
-
// Cache for last input (for gradient computation)
|
|
200
|
-
lastInput = null;
|
|
201
|
-
lastHiddenActivated = null;
|
|
202
|
-
lastProbs = null;
|
|
203
|
-
lastSelectedExperts = [];
|
|
204
|
-
constructor(config = {}) {
|
|
205
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
229
|
+
var MoERouter = /** @class */ (function () {
|
|
230
|
+
function MoERouter(config) {
|
|
231
|
+
if (config === void 0) { config = {}; }
|
|
232
|
+
this.totalRoutings = 0;
|
|
233
|
+
this.updateCount = 0;
|
|
234
|
+
this.avgReward = 0;
|
|
235
|
+
// Cache for last input (for gradient computation)
|
|
236
|
+
this.lastInput = null;
|
|
237
|
+
this.lastHiddenActivated = null;
|
|
238
|
+
this.lastProbs = null;
|
|
239
|
+
this.lastSelectedExperts = [];
|
|
240
|
+
this.config = __assign(__assign({}, DEFAULT_CONFIG), config);
|
|
206
241
|
// Initialize weights
|
|
207
242
|
this.W1 = xavierInit(INPUT_DIM, HIDDEN_DIM);
|
|
208
243
|
this.b1 = new Float32Array(HIDDEN_DIM);
|
|
@@ -228,23 +263,33 @@ export class MoERouter {
|
|
|
228
263
|
/**
|
|
229
264
|
* Initialize router, loading persisted weights if available
|
|
230
265
|
*/
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
266
|
+
MoERouter.prototype.initialize = function () {
|
|
267
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
268
|
+
return __generator(this, function (_a) {
|
|
269
|
+
switch (_a.label) {
|
|
270
|
+
case 0: return [4 /*yield*/, this.loadWeights()];
|
|
271
|
+
case 1:
|
|
272
|
+
_a.sent();
|
|
273
|
+
return [2 /*return*/];
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
};
|
|
234
278
|
/**
|
|
235
279
|
* Route task to top-k experts based on embedding
|
|
236
280
|
*
|
|
237
281
|
* @param taskEmbedding - 384-dim task embedding from ONNX
|
|
238
282
|
* @returns Routing result with selected experts and weights
|
|
239
283
|
*/
|
|
240
|
-
route(taskEmbedding) {
|
|
284
|
+
MoERouter.prototype.route = function (taskEmbedding) {
|
|
285
|
+
var _this = this;
|
|
241
286
|
// Convert to Float32Array if needed
|
|
242
|
-
|
|
287
|
+
var input = taskEmbedding instanceof Float32Array
|
|
243
288
|
? taskEmbedding
|
|
244
289
|
: new Float32Array(taskEmbedding);
|
|
245
290
|
// Validate input dimension
|
|
246
291
|
if (input.length !== INPUT_DIM) {
|
|
247
|
-
throw new Error(
|
|
292
|
+
throw new Error("Expected embedding dimension " + INPUT_DIM + ", got " + input.length);
|
|
248
293
|
}
|
|
249
294
|
// Forward pass through gating network
|
|
250
295
|
// Layer 1: Linear + ReLU
|
|
@@ -264,13 +309,14 @@ export class MoERouter {
|
|
|
264
309
|
// Softmax to get probabilities
|
|
265
310
|
softmax(this.noisyLogits, this.config.temperature, this.probs);
|
|
266
311
|
// Select top-k experts
|
|
267
|
-
|
|
312
|
+
var expertIndices = this.selectTopK(this.probs, this.config.topK);
|
|
268
313
|
// Compute load balance loss
|
|
269
|
-
|
|
314
|
+
var loadBalanceLoss = this.computeLoadBalanceLoss();
|
|
270
315
|
// Compute entropy
|
|
271
|
-
|
|
316
|
+
var routingEntropy = entropy(this.probs);
|
|
272
317
|
// Update statistics
|
|
273
|
-
for (
|
|
318
|
+
for (var _i = 0, expertIndices_1 = expertIndices; _i < expertIndices_1.length; _i++) {
|
|
319
|
+
var idx = expertIndices_1[_i];
|
|
274
320
|
this.routingCounts[idx]++;
|
|
275
321
|
}
|
|
276
322
|
this.totalRoutings++;
|
|
@@ -280,20 +326,20 @@ export class MoERouter {
|
|
|
280
326
|
this.lastProbs = new Float32Array(this.probs);
|
|
281
327
|
this.lastSelectedExperts = expertIndices;
|
|
282
328
|
// Build result
|
|
283
|
-
|
|
284
|
-
|
|
329
|
+
var totalWeight = expertIndices.reduce(function (sum, idx) { return sum + _this.probs[idx]; }, 0);
|
|
330
|
+
var experts = expertIndices.map(function (idx) { return ({
|
|
285
331
|
name: EXPERT_NAMES[idx],
|
|
286
332
|
index: idx,
|
|
287
|
-
weight:
|
|
288
|
-
score:
|
|
289
|
-
}));
|
|
333
|
+
weight: _this.probs[idx] / (totalWeight + 1e-8),
|
|
334
|
+
score: _this.probs[idx]
|
|
335
|
+
}); });
|
|
290
336
|
return {
|
|
291
|
-
experts,
|
|
337
|
+
experts: experts,
|
|
292
338
|
allScores: Array.from(this.probs),
|
|
293
|
-
loadBalanceLoss,
|
|
294
|
-
entropy: routingEntropy
|
|
339
|
+
loadBalanceLoss: loadBalanceLoss,
|
|
340
|
+
entropy: routingEntropy
|
|
295
341
|
};
|
|
296
|
-
}
|
|
342
|
+
};
|
|
297
343
|
/**
|
|
298
344
|
* Update expert weights based on reward signal
|
|
299
345
|
*
|
|
@@ -303,10 +349,10 @@ export class MoERouter {
|
|
|
303
349
|
* @param expert - Expert that received the reward
|
|
304
350
|
* @param reward - Reward signal (-1 to 1, positive = good)
|
|
305
351
|
*/
|
|
306
|
-
updateExpertWeights(expert, reward) {
|
|
307
|
-
|
|
352
|
+
MoERouter.prototype.updateExpertWeights = function (expert, reward) {
|
|
353
|
+
var expertIdx = typeof expert === 'number' ? expert : EXPERT_NAMES.indexOf(expert);
|
|
308
354
|
if (expertIdx < 0 || expertIdx >= NUM_EXPERTS) {
|
|
309
|
-
console.warn(
|
|
355
|
+
console.warn("[MoE] Invalid expert: " + expert);
|
|
310
356
|
return;
|
|
311
357
|
}
|
|
312
358
|
if (!this.lastInput || !this.lastHiddenActivated || !this.lastProbs) {
|
|
@@ -314,7 +360,7 @@ export class MoERouter {
|
|
|
314
360
|
return;
|
|
315
361
|
}
|
|
316
362
|
// Clamp reward to [-1, 1]
|
|
317
|
-
|
|
363
|
+
var clampedReward = Math.max(-1, Math.min(1, reward));
|
|
318
364
|
// Compute gradients using REINFORCE
|
|
319
365
|
// For softmax: d_log_p_i / d_logit_j = delta_ij - p_j
|
|
320
366
|
// gradient = reward * (1 - p_expert) for selected expert
|
|
@@ -326,7 +372,7 @@ export class MoERouter {
|
|
|
326
372
|
this.gradb1.fill(0);
|
|
327
373
|
this.gradHidden.fill(0);
|
|
328
374
|
// Gradient w.r.t. logits (before softmax)
|
|
329
|
-
for (
|
|
375
|
+
for (var i = 0; i < NUM_EXPERTS; i++) {
|
|
330
376
|
if (i === expertIdx) {
|
|
331
377
|
this.gradb2[i] = clampedReward * (1 - this.lastProbs[i]);
|
|
332
378
|
}
|
|
@@ -335,22 +381,22 @@ export class MoERouter {
|
|
|
335
381
|
}
|
|
336
382
|
}
|
|
337
383
|
// Gradient w.r.t. W2: outer product of gradb2 and hiddenActivated
|
|
338
|
-
for (
|
|
339
|
-
|
|
340
|
-
for (
|
|
384
|
+
for (var i = 0; i < NUM_EXPERTS; i++) {
|
|
385
|
+
var rowOffset = i * HIDDEN_DIM;
|
|
386
|
+
for (var j = 0; j < HIDDEN_DIM; j++) {
|
|
341
387
|
this.gradW2[rowOffset + j] = this.gradb2[i] * this.lastHiddenActivated[j];
|
|
342
388
|
}
|
|
343
389
|
}
|
|
344
390
|
// Backprop through W2 to get gradient w.r.t. hidden
|
|
345
|
-
for (
|
|
346
|
-
|
|
347
|
-
for (
|
|
391
|
+
for (var j = 0; j < HIDDEN_DIM; j++) {
|
|
392
|
+
var sum = 0;
|
|
393
|
+
for (var i = 0; i < NUM_EXPERTS; i++) {
|
|
348
394
|
sum += this.gradb2[i] * this.W2[i * HIDDEN_DIM + j];
|
|
349
395
|
}
|
|
350
396
|
this.gradHidden[j] = sum;
|
|
351
397
|
}
|
|
352
398
|
// Backprop through ReLU: gradient is 0 where activation was 0
|
|
353
|
-
for (
|
|
399
|
+
for (var j = 0; j < HIDDEN_DIM; j++) {
|
|
354
400
|
if (this.lastHiddenActivated[j] <= 0) {
|
|
355
401
|
this.gradHidden[j] = 0;
|
|
356
402
|
}
|
|
@@ -358,24 +404,24 @@ export class MoERouter {
|
|
|
358
404
|
// Gradient w.r.t. b1
|
|
359
405
|
this.gradb1.set(this.gradHidden);
|
|
360
406
|
// Gradient w.r.t. W1: outer product of gradHidden and input
|
|
361
|
-
for (
|
|
362
|
-
|
|
363
|
-
for (
|
|
407
|
+
for (var i = 0; i < HIDDEN_DIM; i++) {
|
|
408
|
+
var rowOffset = i * INPUT_DIM;
|
|
409
|
+
for (var j = 0; j < INPUT_DIM; j++) {
|
|
364
410
|
this.gradW1[rowOffset + j] = this.gradHidden[i] * this.lastInput[j];
|
|
365
411
|
}
|
|
366
412
|
}
|
|
367
413
|
// Apply gradients with learning rate
|
|
368
|
-
|
|
369
|
-
for (
|
|
414
|
+
var lr = this.config.learningRate;
|
|
415
|
+
for (var i = 0; i < this.W2.length; i++) {
|
|
370
416
|
this.W2[i] += lr * this.gradW2[i];
|
|
371
417
|
}
|
|
372
|
-
for (
|
|
418
|
+
for (var i = 0; i < this.b2.length; i++) {
|
|
373
419
|
this.b2[i] += lr * this.gradb2[i];
|
|
374
420
|
}
|
|
375
|
-
for (
|
|
421
|
+
for (var i = 0; i < this.W1.length; i++) {
|
|
376
422
|
this.W1[i] += lr * this.gradW1[i];
|
|
377
423
|
}
|
|
378
|
-
for (
|
|
424
|
+
for (var i = 0; i < this.b1.length; i++) {
|
|
379
425
|
this.b1[i] += lr * this.gradb1[i];
|
|
380
426
|
}
|
|
381
427
|
// Update statistics
|
|
@@ -384,44 +430,44 @@ export class MoERouter {
|
|
|
384
430
|
// Auto-save
|
|
385
431
|
if (this.config.autoSaveInterval > 0 &&
|
|
386
432
|
this.updateCount % this.config.autoSaveInterval === 0) {
|
|
387
|
-
this.saveWeights()
|
|
433
|
+
this.saveWeights()["catch"](function () { }); // Fire and forget
|
|
388
434
|
}
|
|
389
|
-
}
|
|
435
|
+
};
|
|
390
436
|
/**
|
|
391
437
|
* Get load balance statistics across all experts
|
|
392
438
|
*/
|
|
393
|
-
getLoadBalance() {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
for (
|
|
399
|
-
|
|
439
|
+
MoERouter.prototype.getLoadBalance = function () {
|
|
440
|
+
var counts = {};
|
|
441
|
+
var utilization = {};
|
|
442
|
+
var total = this.totalRoutings || 1;
|
|
443
|
+
var idealUtilization = 1 / NUM_EXPERTS;
|
|
444
|
+
for (var i = 0; i < NUM_EXPERTS; i++) {
|
|
445
|
+
var name = EXPERT_NAMES[i];
|
|
400
446
|
counts[name] = this.routingCounts[i];
|
|
401
447
|
utilization[name] = this.routingCounts[i] / total;
|
|
402
448
|
}
|
|
403
449
|
// Compute Gini coefficient
|
|
404
|
-
|
|
450
|
+
var gini = this.computeGiniCoefficient();
|
|
405
451
|
// Compute coefficient of variation
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
for (
|
|
452
|
+
var mean = total / NUM_EXPERTS;
|
|
453
|
+
var variance = 0;
|
|
454
|
+
for (var i = 0; i < NUM_EXPERTS; i++) {
|
|
409
455
|
variance += Math.pow(this.routingCounts[i] - mean, 2);
|
|
410
456
|
}
|
|
411
457
|
variance /= NUM_EXPERTS;
|
|
412
|
-
|
|
458
|
+
var cv = Math.sqrt(variance) / (mean + 1e-8);
|
|
413
459
|
return {
|
|
414
|
-
utilization,
|
|
460
|
+
utilization: utilization,
|
|
415
461
|
totalRoutings: this.totalRoutings,
|
|
416
462
|
routingCounts: counts,
|
|
417
463
|
giniCoefficient: gini,
|
|
418
|
-
coefficientOfVariation: cv
|
|
464
|
+
coefficientOfVariation: cv
|
|
419
465
|
};
|
|
420
|
-
}
|
|
466
|
+
};
|
|
421
467
|
/**
|
|
422
468
|
* Get router statistics
|
|
423
469
|
*/
|
|
424
|
-
getStats() {
|
|
470
|
+
MoERouter.prototype.getStats = function () {
|
|
425
471
|
return {
|
|
426
472
|
totalRoutings: this.totalRoutings,
|
|
427
473
|
updateCount: this.updateCount,
|
|
@@ -429,129 +475,139 @@ export class MoERouter {
|
|
|
429
475
|
topK: this.config.topK,
|
|
430
476
|
temperature: this.config.temperature,
|
|
431
477
|
learningRate: this.config.learningRate,
|
|
432
|
-
giniCoefficient: this.computeGiniCoefficient()
|
|
478
|
+
giniCoefficient: this.computeGiniCoefficient()
|
|
433
479
|
};
|
|
434
|
-
}
|
|
480
|
+
};
|
|
435
481
|
/**
|
|
436
482
|
* Reset all statistics and routing counts
|
|
437
483
|
*/
|
|
438
|
-
resetStats() {
|
|
484
|
+
MoERouter.prototype.resetStats = function () {
|
|
439
485
|
this.routingCounts.fill(0);
|
|
440
486
|
this.totalRoutings = 0;
|
|
441
487
|
this.updateCount = 0;
|
|
442
488
|
this.avgReward = 0;
|
|
443
|
-
}
|
|
489
|
+
};
|
|
444
490
|
/**
|
|
445
491
|
* Load weights from persistence file
|
|
446
492
|
*/
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
493
|
+
MoERouter.prototype.loadWeights = function (path) {
|
|
494
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
495
|
+
var weightsPath, data, model;
|
|
496
|
+
return __generator(this, function (_a) {
|
|
497
|
+
weightsPath = path || this.config.weightsPath;
|
|
498
|
+
try {
|
|
499
|
+
if (!existsSync(weightsPath)) {
|
|
500
|
+
return [2 /*return*/, false];
|
|
501
|
+
}
|
|
502
|
+
data = readFileSync(weightsPath, 'utf-8');
|
|
503
|
+
model = JSON.parse(data);
|
|
504
|
+
// Validate version
|
|
505
|
+
if (!model.version || !model.version.startsWith('1.')) {
|
|
506
|
+
console.warn("[MoE] Incompatible model version: " + model.version);
|
|
507
|
+
return [2 /*return*/, false];
|
|
508
|
+
}
|
|
509
|
+
// Load weights
|
|
510
|
+
this.W1 = new Float32Array(model.weights.W1.flat());
|
|
511
|
+
this.b1 = new Float32Array(model.weights.b1);
|
|
512
|
+
this.W2 = new Float32Array(model.weights.W2.flat());
|
|
513
|
+
this.b2 = new Float32Array(model.weights.b2);
|
|
514
|
+
// Load stats
|
|
515
|
+
this.updateCount = model.stats.updateCount || 0;
|
|
516
|
+
this.avgReward = model.stats.avgReward || 0;
|
|
517
|
+
this.routingCounts = new Float32Array(model.stats.routingCounts || new Array(NUM_EXPERTS).fill(0));
|
|
518
|
+
this.totalRoutings = this.routingCounts.reduce(function (a, b) { return a + b; }, 0);
|
|
519
|
+
return [2 /*return*/, true];
|
|
520
|
+
}
|
|
521
|
+
catch (err) {
|
|
522
|
+
console.warn("[MoE] Failed to load weights: " + err);
|
|
523
|
+
return [2 /*return*/, false];
|
|
524
|
+
}
|
|
525
|
+
return [2 /*return*/];
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
};
|
|
477
529
|
/**
|
|
478
530
|
* Save weights to persistence file
|
|
479
531
|
*/
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
532
|
+
MoERouter.prototype.saveWeights = function (path) {
|
|
533
|
+
return __awaiter(this, void 0, Promise, function () {
|
|
534
|
+
var weightsPath, dir, W1_2d, i, W2_2d, i, model;
|
|
535
|
+
return __generator(this, function (_a) {
|
|
536
|
+
weightsPath = path || this.config.weightsPath;
|
|
537
|
+
try {
|
|
538
|
+
dir = dirname(weightsPath);
|
|
539
|
+
if (!existsSync(dir)) {
|
|
540
|
+
mkdirSync(dir, { recursive: true });
|
|
541
|
+
}
|
|
542
|
+
W1_2d = [];
|
|
543
|
+
for (i = 0; i < HIDDEN_DIM; i++) {
|
|
544
|
+
W1_2d.push(Array.from(this.W1.slice(i * INPUT_DIM, (i + 1) * INPUT_DIM)));
|
|
545
|
+
}
|
|
546
|
+
W2_2d = [];
|
|
547
|
+
for (i = 0; i < NUM_EXPERTS; i++) {
|
|
548
|
+
W2_2d.push(Array.from(this.W2.slice(i * HIDDEN_DIM, (i + 1) * HIDDEN_DIM)));
|
|
549
|
+
}
|
|
550
|
+
model = {
|
|
551
|
+
version: '1.0.0',
|
|
552
|
+
config: {
|
|
553
|
+
topK: this.config.topK,
|
|
554
|
+
temperature: this.config.temperature,
|
|
555
|
+
learningRate: this.config.learningRate,
|
|
556
|
+
loadBalanceCoef: this.config.loadBalanceCoef
|
|
557
|
+
},
|
|
558
|
+
weights: {
|
|
559
|
+
W1: W1_2d,
|
|
560
|
+
b1: Array.from(this.b1),
|
|
561
|
+
W2: W2_2d,
|
|
562
|
+
b2: Array.from(this.b2)
|
|
563
|
+
},
|
|
564
|
+
stats: {
|
|
565
|
+
updateCount: this.updateCount,
|
|
566
|
+
routingCounts: Array.from(this.routingCounts),
|
|
567
|
+
avgReward: this.avgReward
|
|
568
|
+
},
|
|
569
|
+
metadata: {
|
|
570
|
+
savedAt: new Date().toISOString(),
|
|
571
|
+
expertNames: __spreadArray([], EXPERT_NAMES, true)
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
writeFileSync(weightsPath, JSON.stringify(model, null, 2));
|
|
575
|
+
return [2 /*return*/, true];
|
|
576
|
+
}
|
|
577
|
+
catch (err) {
|
|
578
|
+
console.warn("[MoE] Failed to save weights: " + err);
|
|
579
|
+
return [2 /*return*/, false];
|
|
580
|
+
}
|
|
581
|
+
return [2 /*return*/];
|
|
582
|
+
});
|
|
583
|
+
});
|
|
584
|
+
};
|
|
529
585
|
/**
|
|
530
586
|
* Reset weights to random initialization
|
|
531
587
|
*/
|
|
532
|
-
resetWeights() {
|
|
588
|
+
MoERouter.prototype.resetWeights = function () {
|
|
533
589
|
this.W1 = xavierInit(INPUT_DIM, HIDDEN_DIM);
|
|
534
590
|
this.b1.fill(0);
|
|
535
591
|
this.W2 = xavierInit(HIDDEN_DIM, NUM_EXPERTS);
|
|
536
592
|
this.b2.fill(0);
|
|
537
593
|
this.resetStats();
|
|
538
|
-
}
|
|
594
|
+
};
|
|
539
595
|
// ============================================================================
|
|
540
596
|
// Private Methods
|
|
541
597
|
// ============================================================================
|
|
542
598
|
/**
|
|
543
599
|
* Select top-k indices from probabilities
|
|
544
600
|
*/
|
|
545
|
-
selectTopK(probs, k) {
|
|
601
|
+
MoERouter.prototype.selectTopK = function (probs, k) {
|
|
546
602
|
// Create index-value pairs and sort by value descending
|
|
547
|
-
|
|
548
|
-
for (
|
|
603
|
+
var pairs = [];
|
|
604
|
+
for (var i = 0; i < probs.length; i++) {
|
|
549
605
|
pairs.push([i, probs[i]]);
|
|
550
606
|
}
|
|
551
|
-
pairs.sort((a, b)
|
|
607
|
+
pairs.sort(function (a, b) { return b[1] - a[1]; });
|
|
552
608
|
// Return top-k indices
|
|
553
|
-
return pairs.slice(0, k).map((p)
|
|
554
|
-
}
|
|
609
|
+
return pairs.slice(0, k).map(function (p) { return p[0]; });
|
|
610
|
+
};
|
|
555
611
|
/**
|
|
556
612
|
* Compute load balance loss for regularization
|
|
557
613
|
*
|
|
@@ -560,41 +616,43 @@ export class MoERouter {
|
|
|
560
616
|
* where f_i = fraction of tokens routed to expert i
|
|
561
617
|
* P_i = average routing probability to expert i
|
|
562
618
|
*/
|
|
563
|
-
computeLoadBalanceLoss() {
|
|
619
|
+
MoERouter.prototype.computeLoadBalanceLoss = function () {
|
|
564
620
|
if (this.totalRoutings === 0)
|
|
565
621
|
return 0;
|
|
566
|
-
|
|
567
|
-
for (
|
|
568
|
-
|
|
569
|
-
|
|
622
|
+
var loss = 0;
|
|
623
|
+
for (var i = 0; i < NUM_EXPERTS; i++) {
|
|
624
|
+
var fraction = this.routingCounts[i] / this.totalRoutings;
|
|
625
|
+
var avgProb = this.probs[i]; // Current routing prob
|
|
570
626
|
loss += fraction * avgProb;
|
|
571
627
|
}
|
|
572
628
|
return NUM_EXPERTS * loss * this.config.loadBalanceCoef;
|
|
573
|
-
}
|
|
629
|
+
};
|
|
574
630
|
/**
|
|
575
631
|
* Compute Gini coefficient for load distribution
|
|
576
632
|
*/
|
|
577
|
-
computeGiniCoefficient() {
|
|
633
|
+
MoERouter.prototype.computeGiniCoefficient = function () {
|
|
578
634
|
if (this.totalRoutings === 0)
|
|
579
635
|
return 0;
|
|
580
636
|
// Sort counts
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
637
|
+
var sorted = Array.from(this.routingCounts).sort(function (a, b) { return a - b; });
|
|
638
|
+
var n = sorted.length;
|
|
639
|
+
var mean = this.totalRoutings / n;
|
|
584
640
|
// Compute Gini using the formula: G = (2 * sum(i * x_i) - (n+1) * sum(x_i)) / (n * sum(x_i))
|
|
585
|
-
|
|
586
|
-
for (
|
|
641
|
+
var weightedSum = 0;
|
|
642
|
+
for (var i = 0; i < n; i++) {
|
|
587
643
|
weightedSum += (i + 1) * sorted[i];
|
|
588
644
|
}
|
|
589
|
-
|
|
645
|
+
var gini = (2 * weightedSum - (n + 1) * this.totalRoutings) /
|
|
590
646
|
(n * this.totalRoutings + 1e-8);
|
|
591
647
|
return Math.max(0, gini);
|
|
592
|
-
}
|
|
593
|
-
|
|
648
|
+
};
|
|
649
|
+
return MoERouter;
|
|
650
|
+
}());
|
|
651
|
+
export { MoERouter };
|
|
594
652
|
// ============================================================================
|
|
595
653
|
// Singleton Instance
|
|
596
654
|
// ============================================================================
|
|
597
|
-
|
|
655
|
+
var moeRouterInstance = null;
|
|
598
656
|
/**
|
|
599
657
|
* Get singleton MoE router instance
|
|
600
658
|
*
|
|
@@ -605,7 +663,7 @@ export function getMoERouter(config) {
|
|
|
605
663
|
if (!moeRouterInstance) {
|
|
606
664
|
moeRouterInstance = new MoERouter(config);
|
|
607
665
|
// Initialize in background (load weights)
|
|
608
|
-
moeRouterInstance.initialize()
|
|
666
|
+
moeRouterInstance.initialize()["catch"](function (err) {
|
|
609
667
|
console.warn('[MoE] Failed to initialize router:', err);
|
|
610
668
|
});
|
|
611
669
|
}
|