ruvector-learning-wasm 0.1.0

Ultra-fast MicroLoRA adaptation for WASM - rank-2 LoRA with <100us latency for per-operator learning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Trajectory tracking for reinforcement learning
//!
//! Records execution trajectories for post-hoc learning and pattern analysis.

use wasm_bindgen::prelude::*;

/// A single trajectory recording
#[derive(Clone)]
pub struct Trajectory {
    /// Embedding at query start
    pub embedding: Vec<f32>,
    /// Operator type that was executed (0-16)
    pub operator_type: u8,
    /// Attention mechanism used
    pub attention_type: u8,
    /// Execution time in milliseconds
    pub execution_ms: f32,
    /// Baseline execution time (for comparison)
    pub baseline_ms: f32,
    /// Improvement ratio (baseline / actual - 1.0)
    pub improvement: f32,
    /// Timestamp (simulation time or wall clock)
    pub timestamp: u64,
}

impl Trajectory {
    /// Create a new trajectory
    pub fn new(
        embedding: Vec<f32>,
        operator_type: u8,
        attention_type: u8,
        execution_ms: f32,
        baseline_ms: f32,
    ) -> Self {
        let improvement = if execution_ms > 0.0 {
            (baseline_ms / execution_ms) - 1.0
        } else {
            0.0
        };

        Self {
            embedding,
            operator_type,
            attention_type,
            execution_ms,
            baseline_ms,
            improvement,
            timestamp: 0,
        }
    }

    /// Get quality score (0.0 - 1.0)
    pub fn quality(&self) -> f32 {
        // Quality based on improvement, saturating at 2x speedup
        ((self.improvement + 1.0) / 2.0).clamp(0.0, 1.0)
    }

    /// Check if this trajectory represents a success
    pub fn is_success(&self) -> bool {
        self.improvement > 0.0
    }

    /// Get the gradient direction for learning
    pub fn gradient(&self) -> Vec<f32> {
        if self.is_success() {
            // Positive improvement: reinforce this direction
            self.embedding.clone()
        } else {
            // Negative improvement: push away from this direction
            self.embedding.iter().map(|x| -x).collect()
        }
    }
}

/// Statistics for a collection of trajectories
#[derive(Clone, Default)]
pub struct TrajectoryStats {
    /// Total trajectory count
    pub count: u64,
    /// Mean improvement ratio
    pub mean_improvement: f32,
    /// Variance of improvement
    pub variance: f32,
    /// Best improvement seen
    pub best_improvement: f32,
    /// Success rate (positive improvement)
    pub success_rate: f32,
    /// Most common attention type
    pub best_attention: u8,
}

impl TrajectoryStats {
    /// Update stats with a new trajectory
    pub fn update(&mut self, trajectory: &Trajectory) {
        let n = self.count as f32;
        let new_n = n + 1.0;

        // Welford's online algorithm for mean and variance
        let delta = trajectory.improvement - self.mean_improvement;
        self.mean_improvement += delta / new_n;
        let delta2 = trajectory.improvement - self.mean_improvement;
        self.variance += delta * delta2;

        // Update best
        if trajectory.improvement > self.best_improvement {
            self.best_improvement = trajectory.improvement;
            self.best_attention = trajectory.attention_type;
        }

        // Update success rate
        let successes = self.success_rate * n;
        let new_successes = if trajectory.is_success() {
            successes + 1.0
        } else {
            successes
        };
        self.success_rate = new_successes / new_n;

        self.count += 1;
    }

    /// Get variance (finalized)
    pub fn final_variance(&self) -> f32 {
        if self.count > 1 {
            self.variance / (self.count - 1) as f32
        } else {
            0.0
        }
    }
}

/// Ring buffer for trajectory storage
pub struct TrajectoryBuffer {
    /// Trajectories storage
    trajectories: Vec<Trajectory>,
    /// Maximum capacity
    capacity: usize,
    /// Write position
    write_pos: usize,
    /// Total count (may exceed capacity)
    total_count: u64,
    /// Running stats
    stats: TrajectoryStats,
}

impl TrajectoryBuffer {
    /// Create a new trajectory buffer
    pub fn new(capacity: usize) -> Self {
        Self {
            trajectories: Vec::with_capacity(capacity),
            capacity,
            write_pos: 0,
            total_count: 0,
            stats: TrajectoryStats::default(),
        }
    }

    /// Push a new trajectory
    pub fn push(&mut self, trajectory: Trajectory) {
        self.stats.update(&trajectory);

        if self.trajectories.len() < self.capacity {
            self.trajectories.push(trajectory);
        } else {
            self.trajectories[self.write_pos] = trajectory;
        }

        self.write_pos = (self.write_pos + 1) % self.capacity;
        self.total_count += 1;
    }

    /// Get current buffer contents
    pub fn trajectories(&self) -> &[Trajectory] {
        &self.trajectories
    }

    /// Drain all trajectories (returns ownership, clears buffer)
    pub fn drain(&mut self) -> Vec<Trajectory> {
        let result = std::mem::take(&mut self.trajectories);
        self.write_pos = 0;
        result
    }

    /// Get statistics
    pub fn stats(&self) -> &TrajectoryStats {
        &self.stats
    }

    /// Get total count (may exceed capacity)
    pub fn total_count(&self) -> u64 {
        self.total_count
    }

    /// Get current buffer size
    pub fn len(&self) -> usize {
        self.trajectories.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.trajectories.is_empty()
    }

    /// Get high-quality trajectories (quality > threshold)
    pub fn high_quality(&self, threshold: f32) -> Vec<&Trajectory> {
        self.trajectories
            .iter()
            .filter(|t| t.quality() > threshold)
            .collect()
    }

    /// Get trajectories for a specific operator type
    pub fn by_operator(&self, op_type: u8) -> Vec<&Trajectory> {
        self.trajectories
            .iter()
            .filter(|t| t.operator_type == op_type)
            .collect()
    }

    /// Reset buffer and stats
    pub fn reset(&mut self) {
        self.trajectories.clear();
        self.write_pos = 0;
        self.total_count = 0;
        self.stats = TrajectoryStats::default();
    }
}

impl Default for TrajectoryBuffer {
    fn default() -> Self {
        Self::new(1000)
    }
}

// ============ WASM Bindings ============

/// WASM-exposed trajectory buffer
#[wasm_bindgen]
pub struct WasmTrajectoryBuffer {
    buffer: TrajectoryBuffer,
    #[allow(dead_code)]
    embedding_dim: usize,
}

#[wasm_bindgen]
impl WasmTrajectoryBuffer {
    /// Create a new trajectory buffer
    ///
    /// @param capacity - Maximum number of trajectories to store
    /// @param embedding_dim - Dimension of embeddings (default 256)
    #[wasm_bindgen(constructor)]
    pub fn new(capacity: Option<usize>, embedding_dim: Option<usize>) -> Self {
        Self {
            buffer: TrajectoryBuffer::new(capacity.unwrap_or(1000)),
            embedding_dim: embedding_dim.unwrap_or(256),
        }
    }

    /// Record a trajectory
    ///
    /// @param embedding - Embedding vector (Float32Array)
    /// @param op_type - Operator type (0-16)
    /// @param attention_type - Attention mechanism used
    /// @param execution_ms - Actual execution time
    /// @param baseline_ms - Baseline execution time
    #[wasm_bindgen]
    pub fn record(
        &mut self,
        embedding: &[f32],
        op_type: u8,
        attention_type: u8,
        execution_ms: f32,
        baseline_ms: f32,
    ) {
        let traj = Trajectory::new(
            embedding.to_vec(),
            op_type,
            attention_type,
            execution_ms,
            baseline_ms,
        );
        self.buffer.push(traj);
    }

    /// Get total count
    #[wasm_bindgen]
    pub fn total_count(&self) -> u64 {
        self.buffer.total_count()
    }

    /// Get buffer length
    #[wasm_bindgen]
    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    /// Check if empty
    #[wasm_bindgen]
    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    /// Get mean improvement
    #[wasm_bindgen]
    pub fn mean_improvement(&self) -> f32 {
        self.buffer.stats().mean_improvement
    }

    /// Get best improvement
    #[wasm_bindgen]
    pub fn best_improvement(&self) -> f32 {
        self.buffer.stats().best_improvement
    }

    /// Get success rate
    #[wasm_bindgen]
    pub fn success_rate(&self) -> f32 {
        self.buffer.stats().success_rate
    }

    /// Get best attention type
    #[wasm_bindgen]
    pub fn best_attention(&self) -> u8 {
        self.buffer.stats().best_attention
    }

    /// Get variance
    #[wasm_bindgen]
    pub fn variance(&self) -> f32 {
        self.buffer.stats().final_variance()
    }

    /// Reset buffer
    #[wasm_bindgen]
    pub fn reset(&mut self) {
        self.buffer.reset();
    }

    /// Get high quality trajectory count
    #[wasm_bindgen]
    pub fn high_quality_count(&self, threshold: f32) -> usize {
        self.buffer.high_quality(threshold).len()
    }

    /// Get trajectory count for operator
    #[wasm_bindgen]
    pub fn count_by_operator(&self, op_type: u8) -> usize {
        self.buffer.by_operator(op_type).len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_trajectory_creation() {
        let embedding = vec![1.0; 256];
        let traj = Trajectory::new(embedding, 2, 0, 100.0, 150.0);

        assert_eq!(traj.operator_type, 2);
        assert!(traj.improvement > 0.0); // 150/100 - 1 = 0.5
        assert!(traj.is_success());
    }

    #[test]
    fn test_trajectory_quality() {
        let embedding = vec![1.0; 256];

        // 2x speedup should give quality close to 1.0
        let fast = Trajectory::new(embedding.clone(), 0, 0, 50.0, 100.0);
        assert!(fast.quality() > 0.5);

        // Slowdown should give lower quality
        let slow = Trajectory::new(embedding, 0, 0, 150.0, 100.0);
        assert!(slow.quality() < 0.5);
    }

    #[test]
    fn test_buffer_push() {
        let mut buffer = TrajectoryBuffer::new(10);
        let embedding = vec![1.0; 256];

        for i in 0..15 {
            let traj = Trajectory::new(embedding.clone(), 0, 0, 100.0, 100.0 + i as f32);
            buffer.push(traj);
        }

        // Buffer should be at capacity
        assert_eq!(buffer.len(), 10);
        // Total count should include all pushes
        assert_eq!(buffer.total_count(), 15);
    }

    #[test]
    fn test_stats_update() {
        let mut stats = TrajectoryStats::default();
        let embedding = vec![1.0; 256];

        let traj1 = Trajectory::new(embedding.clone(), 0, 0, 100.0, 150.0); // 50% improvement
        let traj2 = Trajectory::new(embedding.clone(), 0, 1, 100.0, 200.0); // 100% improvement
        let traj3 = Trajectory::new(embedding, 0, 0, 150.0, 100.0); // -33% (failure)

        stats.update(&traj1);
        stats.update(&traj2);
        stats.update(&traj3);

        assert_eq!(stats.count, 3);
        assert!(stats.success_rate > 0.6); // 2/3 success
        assert_eq!(stats.best_attention, 1); // Best was attention type 1
    }

    #[test]
    fn test_high_quality_filter() {
        let mut buffer = TrajectoryBuffer::new(100);
        let embedding = vec![1.0; 256];

        // Add some trajectories with varying quality
        for i in 0..10 {
            let baseline = 100.0 + (i as f32) * 20.0;
            let traj = Trajectory::new(embedding.clone(), 0, 0, 100.0, baseline);
            buffer.push(traj);
        }

        let high_quality = buffer.high_quality(0.5);
        assert!(!high_quality.is_empty());
    }
}