moflo 4.8.32 → 4.8.34
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/bin/generate-code-map.mjs +955 -955
- package/bin/index-guidance.mjs +905 -905
- package/bin/index-tests.mjs +728 -728
- package/bin/setup-project.mjs +252 -252
- package/package.json +10 -5
- package/src/@claude-flow/cli/dist/src/commands/doctor.js +1339 -1107
- package/src/@claude-flow/cli/dist/src/index.js +2 -18
- package/src/@claude-flow/cli/dist/src/mcp-tools/hooks-tools.js +17 -0
- package/src/@claude-flow/cli/dist/src/memory/memory-initializer.js +4 -7
- package/src/@claude-flow/cli/dist/src/version.js +6 -0
- package/src/@claude-flow/cli/package.json +1 -1
- package/src/@claude-flow/neural/README.md +260 -0
- package/src/@claude-flow/neural/dist/algorithms/a2c.js +361 -0
- package/src/@claude-flow/neural/dist/algorithms/curiosity.js +392 -0
- package/src/@claude-flow/neural/dist/algorithms/decision-transformer.js +415 -0
- package/src/@claude-flow/neural/dist/algorithms/dqn.js +303 -0
- package/src/@claude-flow/neural/dist/algorithms/index.js +74 -0
- package/src/@claude-flow/neural/dist/algorithms/ppo.js +331 -0
- package/src/@claude-flow/neural/dist/algorithms/q-learning.js +259 -0
- package/src/@claude-flow/neural/dist/algorithms/sarsa.js +297 -0
- package/src/@claude-flow/neural/dist/application/index.js +7 -0
- package/src/@claude-flow/neural/dist/application/services/neural-application-service.js +161 -0
- package/src/@claude-flow/neural/dist/domain/entities/pattern.js +134 -0
- package/src/@claude-flow/neural/dist/domain/index.js +8 -0
- package/src/@claude-flow/neural/dist/domain/services/learning-service.js +195 -0
- package/src/@claude-flow/neural/dist/index.js +201 -0
- package/src/@claude-flow/neural/dist/modes/balanced.js +234 -0
- package/src/@claude-flow/neural/dist/modes/base.js +77 -0
- package/src/@claude-flow/neural/dist/modes/batch.js +316 -0
- package/src/@claude-flow/neural/dist/modes/edge.js +310 -0
- package/src/@claude-flow/neural/dist/modes/index.js +13 -0
- package/src/@claude-flow/neural/dist/modes/real-time.js +196 -0
- package/src/@claude-flow/neural/dist/modes/research.js +389 -0
- package/src/@claude-flow/neural/dist/pattern-learner.js +603 -0
- package/src/@claude-flow/neural/dist/reasoning-bank.js +993 -0
- package/src/@claude-flow/neural/dist/reasoningbank-adapter.js +463 -0
- package/src/@claude-flow/neural/dist/sona-integration.js +326 -0
- package/src/@claude-flow/neural/dist/sona-manager.js +695 -0
- package/src/@claude-flow/neural/dist/types.js +11 -0
- package/src/@claude-flow/neural/package.json +26 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Balanced Mode Implementation
|
|
3
|
+
*
|
|
4
|
+
* General-purpose mode with:
|
|
5
|
+
* - +25% quality improvement
|
|
6
|
+
* - 18ms overhead
|
|
7
|
+
* - Rank-4 LoRA
|
|
8
|
+
* - Pattern caching
|
|
9
|
+
* - Standard learning pipeline
|
|
10
|
+
*/
|
|
11
|
+
import { BaseModeImplementation } from './base.js';
|
|
12
|
+
/**
|
|
13
|
+
* Balanced mode for general-purpose learning
|
|
14
|
+
*/
|
|
15
|
+
export class BalancedMode extends BaseModeImplementation {
|
|
16
|
+
mode = 'balanced';
|
|
17
|
+
// Pattern cache
|
|
18
|
+
patternCache = new Map();
|
|
19
|
+
cacheHits = 0;
|
|
20
|
+
cacheMisses = 0;
|
|
21
|
+
// Learning state
|
|
22
|
+
gradientAccumulator = new Map();
|
|
23
|
+
momentumBuffers = new Map();
|
|
24
|
+
// Stats
|
|
25
|
+
totalPatternMatches = 0;
|
|
26
|
+
totalPatternTime = 0;
|
|
27
|
+
totalLearnTime = 0;
|
|
28
|
+
learnIterations = 0;
|
|
29
|
+
qualityImprovements = [];
|
|
30
|
+
async initialize() {
|
|
31
|
+
await super.initialize();
|
|
32
|
+
this.patternCache.clear();
|
|
33
|
+
this.gradientAccumulator.clear();
|
|
34
|
+
this.momentumBuffers.clear();
|
|
35
|
+
}
|
|
36
|
+
async cleanup() {
|
|
37
|
+
this.patternCache.clear();
|
|
38
|
+
this.gradientAccumulator.clear();
|
|
39
|
+
this.momentumBuffers.clear();
|
|
40
|
+
await super.cleanup();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Find patterns using similarity search with caching
|
|
44
|
+
*/
|
|
45
|
+
async findPatterns(embedding, k, patterns) {
|
|
46
|
+
const startTime = performance.now();
|
|
47
|
+
// Check cache
|
|
48
|
+
const cacheKey = this.computeCacheKey(embedding);
|
|
49
|
+
const cached = this.patternCache.get(cacheKey);
|
|
50
|
+
if (cached && cached.length >= k) {
|
|
51
|
+
this.cacheHits++;
|
|
52
|
+
this.totalPatternTime += performance.now() - startTime;
|
|
53
|
+
this.totalPatternMatches++;
|
|
54
|
+
return cached.slice(0, k);
|
|
55
|
+
}
|
|
56
|
+
this.cacheMisses++;
|
|
57
|
+
// Compute similarities for all patterns
|
|
58
|
+
const matches = [];
|
|
59
|
+
for (const pattern of patterns) {
|
|
60
|
+
const similarity = this.cosineSimilarity(embedding, pattern.embedding);
|
|
61
|
+
matches.push({
|
|
62
|
+
pattern,
|
|
63
|
+
similarity,
|
|
64
|
+
confidence: similarity * pattern.successRate,
|
|
65
|
+
latencyMs: 0,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Sort by similarity descending
|
|
69
|
+
matches.sort((a, b) => b.similarity - a.similarity);
|
|
70
|
+
const topK = matches.slice(0, k);
|
|
71
|
+
// Cache result
|
|
72
|
+
if (this.patternCache.size > 500) {
|
|
73
|
+
const firstKey = this.patternCache.keys().next().value;
|
|
74
|
+
if (firstKey)
|
|
75
|
+
this.patternCache.delete(firstKey);
|
|
76
|
+
}
|
|
77
|
+
this.patternCache.set(cacheKey, topK);
|
|
78
|
+
this.totalPatternTime += performance.now() - startTime;
|
|
79
|
+
this.totalPatternMatches++;
|
|
80
|
+
return topK;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Learn from trajectories using standard gradient descent
|
|
84
|
+
*/
|
|
85
|
+
async learn(trajectories, config, ewcState) {
|
|
86
|
+
const startTime = performance.now();
|
|
87
|
+
if (trajectories.length === 0)
|
|
88
|
+
return 0;
|
|
89
|
+
const qualityThreshold = config.qualityThreshold;
|
|
90
|
+
const learningRate = config.learningRate;
|
|
91
|
+
// Separate positive and negative examples
|
|
92
|
+
const goodTrajectories = trajectories.filter(t => t.qualityScore >= qualityThreshold);
|
|
93
|
+
const badTrajectories = trajectories.filter(t => t.qualityScore < qualityThreshold);
|
|
94
|
+
if (goodTrajectories.length === 0)
|
|
95
|
+
return 0;
|
|
96
|
+
// Compute gradients from trajectory pairs
|
|
97
|
+
let totalGradientNorm = 0;
|
|
98
|
+
for (const good of goodTrajectories) {
|
|
99
|
+
// Use last step embedding as "goal state"
|
|
100
|
+
if (good.steps.length === 0)
|
|
101
|
+
continue;
|
|
102
|
+
const goalState = good.steps[good.steps.length - 1].stateAfter;
|
|
103
|
+
// Positive gradient: move toward good outcomes
|
|
104
|
+
const posGradient = this.computeGradient(goalState, good.qualityScore);
|
|
105
|
+
totalGradientNorm += this.accumulateGradient('positive', posGradient, learningRate);
|
|
106
|
+
// Negative gradient: move away from bad outcomes (contrastive)
|
|
107
|
+
for (const bad of badTrajectories.slice(0, 3)) {
|
|
108
|
+
if (bad.steps.length === 0)
|
|
109
|
+
continue;
|
|
110
|
+
const badState = bad.steps[bad.steps.length - 1].stateAfter;
|
|
111
|
+
const negGradient = this.computeGradient(badState, -bad.qualityScore);
|
|
112
|
+
totalGradientNorm += this.accumulateGradient('negative', negGradient, learningRate * 0.5);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Apply EWC regularization
|
|
116
|
+
const ewcPenalty = this.computeEWCPenalty(ewcState, config.ewcLambda);
|
|
117
|
+
totalGradientNorm += ewcPenalty;
|
|
118
|
+
// Compute improvement delta
|
|
119
|
+
const avgGoodQuality = goodTrajectories.reduce((s, t) => s + t.qualityScore, 0) / goodTrajectories.length;
|
|
120
|
+
const baselineQuality = 0.5;
|
|
121
|
+
const improvementDelta = avgGoodQuality - baselineQuality;
|
|
122
|
+
this.qualityImprovements.push(improvementDelta);
|
|
123
|
+
if (this.qualityImprovements.length > 100) {
|
|
124
|
+
this.qualityImprovements = this.qualityImprovements.slice(-100);
|
|
125
|
+
}
|
|
126
|
+
this.totalLearnTime += performance.now() - startTime;
|
|
127
|
+
this.learnIterations++;
|
|
128
|
+
return Math.max(0, improvementDelta);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Apply LoRA adaptations with rank-4
|
|
132
|
+
*/
|
|
133
|
+
async applyLoRA(input, weights) {
|
|
134
|
+
if (!weights) {
|
|
135
|
+
return input;
|
|
136
|
+
}
|
|
137
|
+
const output = new Float32Array(input.length);
|
|
138
|
+
output.set(input);
|
|
139
|
+
const rank = this.config.loraRank;
|
|
140
|
+
// Apply to all target modules
|
|
141
|
+
for (const module of ['q_proj', 'v_proj', 'k_proj', 'o_proj']) {
|
|
142
|
+
const A = weights.A.get(module);
|
|
143
|
+
const B = weights.B.get(module);
|
|
144
|
+
if (A && B) {
|
|
145
|
+
const adapted = this.applyLoRATransform(input, A, B, rank);
|
|
146
|
+
const alpha = 0.2; // Moderate blending
|
|
147
|
+
for (let i = 0; i < output.length; i++) {
|
|
148
|
+
output[i] = output[i] * (1 - alpha) + adapted[i] * alpha;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return output;
|
|
153
|
+
}
|
|
154
|
+
getStats() {
|
|
155
|
+
const avgImprovement = this.qualityImprovements.length > 0
|
|
156
|
+
? this.qualityImprovements.reduce((a, b) => a + b, 0) / this.qualityImprovements.length
|
|
157
|
+
: 0;
|
|
158
|
+
return {
|
|
159
|
+
cacheHitRate: this.cacheHits + this.cacheMisses > 0
|
|
160
|
+
? this.cacheHits / (this.cacheHits + this.cacheMisses)
|
|
161
|
+
: 0,
|
|
162
|
+
avgPatternMatchMs: this.totalPatternMatches > 0
|
|
163
|
+
? this.totalPatternTime / this.totalPatternMatches
|
|
164
|
+
: 0,
|
|
165
|
+
avgLearnMs: this.learnIterations > 0
|
|
166
|
+
? this.totalLearnTime / this.learnIterations
|
|
167
|
+
: 0,
|
|
168
|
+
avgImprovement,
|
|
169
|
+
patternCacheSize: this.patternCache.size,
|
|
170
|
+
learnIterations: this.learnIterations,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Compute cache key from embedding
|
|
175
|
+
*/
|
|
176
|
+
computeCacheKey(embedding) {
|
|
177
|
+
const keyParts = [];
|
|
178
|
+
for (let i = 0; i < Math.min(16, embedding.length); i++) {
|
|
179
|
+
keyParts.push(embedding[i].toFixed(2));
|
|
180
|
+
}
|
|
181
|
+
return keyParts.join(',');
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Compute gradient from state and reward
|
|
185
|
+
*/
|
|
186
|
+
computeGradient(state, reward) {
|
|
187
|
+
const gradient = new Float32Array(state.length);
|
|
188
|
+
for (let i = 0; i < state.length; i++) {
|
|
189
|
+
gradient[i] = state[i] * reward;
|
|
190
|
+
}
|
|
191
|
+
return gradient;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Accumulate gradient with momentum
|
|
195
|
+
*/
|
|
196
|
+
accumulateGradient(key, gradient, lr) {
|
|
197
|
+
let momentum = this.momentumBuffers.get(key);
|
|
198
|
+
if (!momentum) {
|
|
199
|
+
momentum = new Float32Array(gradient.length);
|
|
200
|
+
this.momentumBuffers.set(key, momentum);
|
|
201
|
+
}
|
|
202
|
+
let accumulator = this.gradientAccumulator.get(key);
|
|
203
|
+
if (!accumulator) {
|
|
204
|
+
accumulator = new Float32Array(gradient.length);
|
|
205
|
+
this.gradientAccumulator.set(key, accumulator);
|
|
206
|
+
}
|
|
207
|
+
const beta = 0.9; // Momentum coefficient
|
|
208
|
+
let norm = 0;
|
|
209
|
+
for (let i = 0; i < gradient.length; i++) {
|
|
210
|
+
momentum[i] = beta * momentum[i] + (1 - beta) * gradient[i];
|
|
211
|
+
accumulator[i] += lr * momentum[i];
|
|
212
|
+
norm += momentum[i] * momentum[i];
|
|
213
|
+
}
|
|
214
|
+
return Math.sqrt(norm);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Compute EWC penalty for continual learning
|
|
218
|
+
*/
|
|
219
|
+
computeEWCPenalty(ewcState, lambda) {
|
|
220
|
+
let penalty = 0;
|
|
221
|
+
for (const [key, fisher] of ewcState.fisher) {
|
|
222
|
+
const means = ewcState.means.get(key);
|
|
223
|
+
const current = this.gradientAccumulator.get(key);
|
|
224
|
+
if (means && current) {
|
|
225
|
+
for (let i = 0; i < Math.min(fisher.length, means.length, current.length); i++) {
|
|
226
|
+
const diff = current[i] - means[i];
|
|
227
|
+
penalty += fisher[i] * diff * diff;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return lambda * penalty * 0.5;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=balanced.js.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Mode Implementation
|
|
3
|
+
*
|
|
4
|
+
* Separated to avoid circular dependencies.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Base class for mode implementations
|
|
8
|
+
*/
|
|
9
|
+
export class BaseModeImplementation {
|
|
10
|
+
config;
|
|
11
|
+
optimizations;
|
|
12
|
+
isInitialized = false;
|
|
13
|
+
constructor(config, optimizations) {
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.optimizations = optimizations;
|
|
16
|
+
}
|
|
17
|
+
async initialize() {
|
|
18
|
+
this.isInitialized = true;
|
|
19
|
+
}
|
|
20
|
+
async cleanup() {
|
|
21
|
+
this.isInitialized = false;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compute cosine similarity between two vectors (SIMD-optimized)
|
|
25
|
+
*/
|
|
26
|
+
cosineSimilarity(a, b) {
|
|
27
|
+
if (a.length !== b.length)
|
|
28
|
+
return 0;
|
|
29
|
+
let dotProduct = 0;
|
|
30
|
+
let normA = 0;
|
|
31
|
+
let normB = 0;
|
|
32
|
+
// Process 4 elements at a time for SIMD-like behavior
|
|
33
|
+
const len = a.length;
|
|
34
|
+
const simdLen = len - (len % 4);
|
|
35
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
36
|
+
dotProduct += a[i] * b[i] + a[i + 1] * b[i + 1] + a[i + 2] * b[i + 2] + a[i + 3] * b[i + 3];
|
|
37
|
+
normA += a[i] * a[i] + a[i + 1] * a[i + 1] + a[i + 2] * a[i + 2] + a[i + 3] * a[i + 3];
|
|
38
|
+
normB += b[i] * b[i] + b[i + 1] * b[i + 1] + b[i + 2] * b[i + 2] + b[i + 3] * b[i + 3];
|
|
39
|
+
}
|
|
40
|
+
// Handle remaining elements
|
|
41
|
+
for (let i = simdLen; i < len; i++) {
|
|
42
|
+
dotProduct += a[i] * b[i];
|
|
43
|
+
normA += a[i] * a[i];
|
|
44
|
+
normB += b[i] * b[i];
|
|
45
|
+
}
|
|
46
|
+
const denom = Math.sqrt(normA) * Math.sqrt(normB);
|
|
47
|
+
return denom > 0 ? dotProduct / denom : 0;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Apply LoRA: output = input + BA * input (simplified)
|
|
51
|
+
*/
|
|
52
|
+
applyLoRATransform(input, A, B, rank) {
|
|
53
|
+
const dim = input.length;
|
|
54
|
+
const output = new Float32Array(dim);
|
|
55
|
+
// Copy input to output
|
|
56
|
+
output.set(input);
|
|
57
|
+
// Compute A * input -> intermediate (rank dimensions)
|
|
58
|
+
const intermediate = new Float32Array(rank);
|
|
59
|
+
for (let r = 0; r < rank; r++) {
|
|
60
|
+
let sum = 0;
|
|
61
|
+
for (let d = 0; d < dim; d++) {
|
|
62
|
+
sum += A[d * rank + r] * input[d];
|
|
63
|
+
}
|
|
64
|
+
intermediate[r] = sum;
|
|
65
|
+
}
|
|
66
|
+
// Compute B * intermediate -> delta (dim dimensions)
|
|
67
|
+
for (let d = 0; d < dim; d++) {
|
|
68
|
+
let sum = 0;
|
|
69
|
+
for (let r = 0; r < rank; r++) {
|
|
70
|
+
sum += B[r * dim + d] * intermediate[r];
|
|
71
|
+
}
|
|
72
|
+
output[d] += sum;
|
|
73
|
+
}
|
|
74
|
+
return output;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch Mode Implementation
|
|
3
|
+
*
|
|
4
|
+
* Optimized for high-throughput processing with:
|
|
5
|
+
* - Large batch sizes (128)
|
|
6
|
+
* - Rank-8 LoRA
|
|
7
|
+
* - Gradient accumulation
|
|
8
|
+
* - Async batch processing
|
|
9
|
+
* - 50ms latency budget
|
|
10
|
+
*/
|
|
11
|
+
import { BaseModeImplementation } from './base.js';
|
|
12
|
+
/**
|
|
13
|
+
* Batch mode for high-throughput processing
|
|
14
|
+
*/
|
|
15
|
+
export class BatchMode extends BaseModeImplementation {
|
|
16
|
+
mode = 'batch';
|
|
17
|
+
// Batch processing queues
|
|
18
|
+
patternQueue = [];
|
|
19
|
+
learningQueue = [];
|
|
20
|
+
// Batch buffers
|
|
21
|
+
embeddingBuffer = null;
|
|
22
|
+
batchEmbeddings = [];
|
|
23
|
+
// Gradient accumulation
|
|
24
|
+
accumulatedGradients = new Map();
|
|
25
|
+
gradientSteps = 0;
|
|
26
|
+
// Batch processing state
|
|
27
|
+
isBatchProcessing = false;
|
|
28
|
+
batchTimer = null;
|
|
29
|
+
// Stats
|
|
30
|
+
totalBatches = 0;
|
|
31
|
+
totalItems = 0;
|
|
32
|
+
totalBatchTime = 0;
|
|
33
|
+
learnIterations = 0;
|
|
34
|
+
async initialize() {
|
|
35
|
+
await super.initialize();
|
|
36
|
+
this.patternQueue = [];
|
|
37
|
+
this.learningQueue = [];
|
|
38
|
+
this.accumulatedGradients.clear();
|
|
39
|
+
this.gradientSteps = 0;
|
|
40
|
+
}
|
|
41
|
+
async cleanup() {
|
|
42
|
+
if (this.batchTimer) {
|
|
43
|
+
clearTimeout(this.batchTimer);
|
|
44
|
+
}
|
|
45
|
+
this.patternQueue = [];
|
|
46
|
+
this.learningQueue = [];
|
|
47
|
+
this.accumulatedGradients.clear();
|
|
48
|
+
await super.cleanup();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Find patterns - queues for batch processing
|
|
52
|
+
*/
|
|
53
|
+
async findPatterns(embedding, k, patterns) {
|
|
54
|
+
// For immediate needs, process synchronously
|
|
55
|
+
if (patterns.length < 100) {
|
|
56
|
+
return this.findPatternsDirect(embedding, k, patterns);
|
|
57
|
+
}
|
|
58
|
+
// Queue for batch processing
|
|
59
|
+
return new Promise(resolve => {
|
|
60
|
+
this.patternQueue.push({ embedding, k, resolve });
|
|
61
|
+
this.scheduleBatchProcessing(patterns);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Learn from trajectories - accumulates for batch
|
|
66
|
+
*/
|
|
67
|
+
async learn(trajectories, config, ewcState) {
|
|
68
|
+
const startTime = performance.now();
|
|
69
|
+
if (trajectories.length === 0)
|
|
70
|
+
return 0;
|
|
71
|
+
// Add to learning queue
|
|
72
|
+
this.learningQueue.push(...trajectories);
|
|
73
|
+
// Process when queue is full
|
|
74
|
+
if (this.learningQueue.length >= config.batchSize) {
|
|
75
|
+
return this.processBatchLearning(config, ewcState);
|
|
76
|
+
}
|
|
77
|
+
// Return estimated improvement
|
|
78
|
+
const avgQuality = trajectories.reduce((s, t) => s + t.qualityScore, 0) / trajectories.length;
|
|
79
|
+
this.totalBatchTime += performance.now() - startTime;
|
|
80
|
+
return Math.max(0, avgQuality - 0.5) * 0.5; // Partial estimate
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Apply LoRA with rank-8
|
|
84
|
+
*/
|
|
85
|
+
async applyLoRA(input, weights) {
|
|
86
|
+
if (!weights) {
|
|
87
|
+
return input;
|
|
88
|
+
}
|
|
89
|
+
// Batch mode can process multiple inputs efficiently
|
|
90
|
+
this.batchEmbeddings.push(new Float32Array(input));
|
|
91
|
+
// Process immediately for single requests
|
|
92
|
+
if (this.batchEmbeddings.length === 1) {
|
|
93
|
+
const output = await this.applyLoRADirect(input, weights);
|
|
94
|
+
this.batchEmbeddings = [];
|
|
95
|
+
return output;
|
|
96
|
+
}
|
|
97
|
+
// For multiple inputs, process as batch
|
|
98
|
+
const outputs = await this.applyLoRABatch(this.batchEmbeddings, weights);
|
|
99
|
+
this.batchEmbeddings = [];
|
|
100
|
+
return outputs[outputs.length - 1];
|
|
101
|
+
}
|
|
102
|
+
getStats() {
|
|
103
|
+
return {
|
|
104
|
+
totalBatches: this.totalBatches,
|
|
105
|
+
avgItemsPerBatch: this.totalBatches > 0 ? this.totalItems / this.totalBatches : 0,
|
|
106
|
+
avgBatchTimeMs: this.totalBatches > 0 ? this.totalBatchTime / this.totalBatches : 0,
|
|
107
|
+
pendingPatternRequests: this.patternQueue.length,
|
|
108
|
+
pendingTrajectories: this.learningQueue.length,
|
|
109
|
+
accumulatedGradientSteps: this.gradientSteps,
|
|
110
|
+
learnIterations: this.learnIterations,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// ========================================================================
|
|
114
|
+
// Direct processing (for small batches)
|
|
115
|
+
// ========================================================================
|
|
116
|
+
/**
|
|
117
|
+
* Direct pattern matching without batching
|
|
118
|
+
*/
|
|
119
|
+
findPatternsDirect(embedding, k, patterns) {
|
|
120
|
+
const matches = [];
|
|
121
|
+
for (const pattern of patterns) {
|
|
122
|
+
const similarity = this.cosineSimilarity(embedding, pattern.embedding);
|
|
123
|
+
matches.push({
|
|
124
|
+
pattern,
|
|
125
|
+
similarity,
|
|
126
|
+
confidence: similarity * pattern.successRate,
|
|
127
|
+
latencyMs: 0,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
matches.sort((a, b) => b.similarity - a.similarity);
|
|
131
|
+
return matches.slice(0, k);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Direct LoRA application
|
|
135
|
+
*/
|
|
136
|
+
async applyLoRADirect(input, weights) {
|
|
137
|
+
const output = new Float32Array(input.length);
|
|
138
|
+
output.set(input);
|
|
139
|
+
const rank = this.config.loraRank;
|
|
140
|
+
for (const module of ['q_proj', 'v_proj', 'k_proj', 'o_proj']) {
|
|
141
|
+
const A = weights.A.get(module);
|
|
142
|
+
const B = weights.B.get(module);
|
|
143
|
+
if (A && B) {
|
|
144
|
+
const adapted = this.applyLoRATransform(input, A, B, rank);
|
|
145
|
+
const alpha = 0.25;
|
|
146
|
+
for (let i = 0; i < output.length; i++) {
|
|
147
|
+
output[i] = output[i] * (1 - alpha) + adapted[i] * alpha;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return output;
|
|
152
|
+
}
|
|
153
|
+
// ========================================================================
|
|
154
|
+
// Batch processing
|
|
155
|
+
// ========================================================================
|
|
156
|
+
/**
|
|
157
|
+
* Schedule batch processing
|
|
158
|
+
*/
|
|
159
|
+
scheduleBatchProcessing(patterns) {
|
|
160
|
+
if (this.batchTimer)
|
|
161
|
+
return;
|
|
162
|
+
this.batchTimer = setTimeout(() => {
|
|
163
|
+
this.processBatchPatterns(patterns);
|
|
164
|
+
}, 10); // Wait 10ms to accumulate requests
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Process pattern requests in batch
|
|
168
|
+
*/
|
|
169
|
+
async processBatchPatterns(patterns) {
|
|
170
|
+
this.batchTimer = null;
|
|
171
|
+
if (this.patternQueue.length === 0)
|
|
172
|
+
return;
|
|
173
|
+
const startTime = performance.now();
|
|
174
|
+
this.isBatchProcessing = true;
|
|
175
|
+
const batch = this.patternQueue;
|
|
176
|
+
this.patternQueue = [];
|
|
177
|
+
// Pre-compute pattern embeddings matrix
|
|
178
|
+
const patternMatrix = patterns.map(p => p.embedding);
|
|
179
|
+
// Process all queries in batch
|
|
180
|
+
for (const request of batch) {
|
|
181
|
+
const matches = this.batchSimilaritySearch(request.embedding, request.k, patterns, patternMatrix);
|
|
182
|
+
request.resolve(matches);
|
|
183
|
+
}
|
|
184
|
+
this.totalBatches++;
|
|
185
|
+
this.totalItems += batch.length;
|
|
186
|
+
this.totalBatchTime += performance.now() - startTime;
|
|
187
|
+
this.isBatchProcessing = false;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Batch similarity search
|
|
191
|
+
*/
|
|
192
|
+
batchSimilaritySearch(query, k, patterns, patternMatrix) {
|
|
193
|
+
const similarities = [];
|
|
194
|
+
for (let i = 0; i < patternMatrix.length; i++) {
|
|
195
|
+
const sim = this.cosineSimilarity(query, patternMatrix[i]);
|
|
196
|
+
similarities.push({ idx: i, sim });
|
|
197
|
+
}
|
|
198
|
+
similarities.sort((a, b) => b.sim - a.sim);
|
|
199
|
+
const topK = similarities.slice(0, k);
|
|
200
|
+
return topK.map(s => ({
|
|
201
|
+
pattern: patterns[s.idx],
|
|
202
|
+
similarity: s.sim,
|
|
203
|
+
confidence: s.sim * patterns[s.idx].successRate,
|
|
204
|
+
latencyMs: 0,
|
|
205
|
+
}));
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Process batch learning
|
|
209
|
+
*/
|
|
210
|
+
async processBatchLearning(config, ewcState) {
|
|
211
|
+
const startTime = performance.now();
|
|
212
|
+
const batch = this.learningQueue.slice(0, config.batchSize);
|
|
213
|
+
this.learningQueue = this.learningQueue.slice(config.batchSize);
|
|
214
|
+
const qualityThreshold = config.qualityThreshold;
|
|
215
|
+
const learningRate = config.learningRate;
|
|
216
|
+
// Separate by quality
|
|
217
|
+
const good = batch.filter(t => t.qualityScore >= qualityThreshold);
|
|
218
|
+
const bad = batch.filter(t => t.qualityScore < qualityThreshold);
|
|
219
|
+
if (good.length === 0) {
|
|
220
|
+
this.totalBatchTime += performance.now() - startTime;
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
// Accumulate gradients
|
|
224
|
+
for (const trajectory of good) {
|
|
225
|
+
this.accumulateTrajectoryGradient(trajectory, learningRate);
|
|
226
|
+
}
|
|
227
|
+
// Contrastive learning from bad examples
|
|
228
|
+
for (const trajectory of bad.slice(0, good.length)) {
|
|
229
|
+
this.accumulateTrajectoryGradient(trajectory, -learningRate * 0.3);
|
|
230
|
+
}
|
|
231
|
+
this.gradientSteps++;
|
|
232
|
+
// Apply accumulated gradients every N steps
|
|
233
|
+
if (this.gradientSteps >= 4) {
|
|
234
|
+
await this.applyAccumulatedGradients(ewcState, config.ewcLambda);
|
|
235
|
+
this.gradientSteps = 0;
|
|
236
|
+
}
|
|
237
|
+
// Compute improvement
|
|
238
|
+
const avgQuality = good.reduce((s, t) => s + t.qualityScore, 0) / good.length;
|
|
239
|
+
const improvement = avgQuality - 0.5;
|
|
240
|
+
this.learnIterations++;
|
|
241
|
+
this.totalBatchTime += performance.now() - startTime;
|
|
242
|
+
return Math.max(0, improvement);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Accumulate gradient from trajectory
|
|
246
|
+
*/
|
|
247
|
+
accumulateTrajectoryGradient(trajectory, scale) {
|
|
248
|
+
if (trajectory.steps.length === 0)
|
|
249
|
+
return;
|
|
250
|
+
const key = trajectory.domain;
|
|
251
|
+
let gradient = this.accumulatedGradients.get(key);
|
|
252
|
+
if (!gradient) {
|
|
253
|
+
const dim = trajectory.steps[0].stateAfter.length;
|
|
254
|
+
gradient = new Float32Array(dim);
|
|
255
|
+
this.accumulatedGradients.set(key, gradient);
|
|
256
|
+
}
|
|
257
|
+
// Add trajectory contribution
|
|
258
|
+
const weight = trajectory.qualityScore * scale;
|
|
259
|
+
for (const step of trajectory.steps) {
|
|
260
|
+
for (let i = 0; i < Math.min(gradient.length, step.stateAfter.length); i++) {
|
|
261
|
+
gradient[i] += step.stateAfter[i] * weight * step.reward;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Apply accumulated gradients with EWC
|
|
267
|
+
*/
|
|
268
|
+
async applyAccumulatedGradients(ewcState, ewcLambda) {
|
|
269
|
+
for (const [key, gradient] of this.accumulatedGradients) {
|
|
270
|
+
// Normalize gradient
|
|
271
|
+
const norm = Math.sqrt(gradient.reduce((s, v) => s + v * v, 0));
|
|
272
|
+
if (norm > 0) {
|
|
273
|
+
for (let i = 0; i < gradient.length; i++) {
|
|
274
|
+
gradient[i] /= norm;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// Apply EWC penalty
|
|
278
|
+
const fisher = ewcState.fisher.get(key);
|
|
279
|
+
const means = ewcState.means.get(key);
|
|
280
|
+
if (fisher && means) {
|
|
281
|
+
for (let i = 0; i < gradient.length; i++) {
|
|
282
|
+
const penalty = ewcLambda * fisher[i] * (gradient[i] - means[i]);
|
|
283
|
+
gradient[i] -= penalty;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Clear gradient for next accumulation
|
|
287
|
+
gradient.fill(0);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Apply LoRA to batch of inputs
|
|
292
|
+
*/
|
|
293
|
+
async applyLoRABatch(inputs, weights) {
|
|
294
|
+
const outputs = [];
|
|
295
|
+
const rank = this.config.loraRank;
|
|
296
|
+
// Process all inputs together for cache efficiency
|
|
297
|
+
for (const input of inputs) {
|
|
298
|
+
const output = new Float32Array(input.length);
|
|
299
|
+
output.set(input);
|
|
300
|
+
for (const module of ['q_proj', 'v_proj', 'k_proj', 'o_proj']) {
|
|
301
|
+
const A = weights.A.get(module);
|
|
302
|
+
const B = weights.B.get(module);
|
|
303
|
+
if (A && B) {
|
|
304
|
+
const adapted = this.applyLoRATransform(input, A, B, rank);
|
|
305
|
+
const alpha = 0.25;
|
|
306
|
+
for (let i = 0; i < output.length; i++) {
|
|
307
|
+
output[i] = output[i] * (1 - alpha) + adapted[i] * alpha;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
outputs.push(output);
|
|
312
|
+
}
|
|
313
|
+
return outputs;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
//# sourceMappingURL=batch.js.map
|