Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'

This commit is contained in:
ruv
2026-02-28 14:39:40 -05:00
7854 changed files with 3522914 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
//! MicroLoRA WASM - Ultra-fast Low-Rank Adaptation for Edge AI
//!
//! This crate provides rank-2 LoRA (Low-Rank Adaptation) matrices optimized for
//! WASM execution with <100us adaptation latency. Designed for real-time
//! per-operator-type learning in query optimization systems.
//!
//! ## Key Features
//!
//! - **Rank-2 LoRA**: Minimal parameter count (2d parameters per adapter)
//! - **Per-Operator Scoping**: Separate adapters for different operator types
//! - **<100us Adaptation**: Instant weight updates for real-time learning
//! - **WASM-Optimized**: no_std compatible, minimal allocations
//!
//! ## Architecture
//!
//! ```text
//! Input Embedding (d-dim)
//! |
//! v
//! +---------+
//! | A: d x 2 | Down projection
//! +---------+
//! |
//! v
//! +---------+
//! | B: 2 x d | Up projection
//! +---------+
//! |
//! v
//! Delta W = alpha * (A @ B)
//! |
//! v
//! Output = Input + Delta W
//! ```
mod lora;
mod operator_scope;
mod trajectory;
pub use lora::{LoRAConfig, LoRAPair, MicroLoRAEngine};
pub use operator_scope::{OperatorScope, ScopedLoRA};
pub use trajectory::{Trajectory, TrajectoryBuffer, TrajectoryStats};
// Re-export core types for JS
pub use lora::wasm_exports::*;
pub use operator_scope::wasm_exports::*;

View File

@@ -0,0 +1,559 @@
//! MicroLoRA: Rank-2 Low-Rank Adaptation with <100us latency
//!
//! Implements the core LoRA algorithm: output = input + alpha * (input @ A @ B)
//! where A: [d x 2] and B: [2 x d] for rank-2 adaptation.
use wasm_bindgen::prelude::*;
/// Configuration for MicroLoRA
#[derive(Debug, Clone, Copy)]
pub struct LoRAConfig {
/// Embedding dimension (typically 256)
pub dim: usize,
/// LoRA rank (1-2 for micro, default 2)
pub rank: usize,
/// Scaling factor alpha (default 0.1)
pub alpha: f32,
/// Learning rate for adaptation (default 0.01)
pub learning_rate: f32,
/// Dropout rate (0.0 = no dropout)
pub dropout: f32,
}
impl Default for LoRAConfig {
fn default() -> Self {
Self {
dim: 256,
rank: 2,
alpha: 0.1,
learning_rate: 0.01,
dropout: 0.0,
}
}
}
/// A single LoRA adapter pair (A and B matrices)
///
/// For rank-2:
/// - A: [dim x 2] - Down projection
/// - B: [2 x dim] - Up projection (initialized to zero)
///
/// Forward: output = input + alpha * (input @ A @ B)
#[derive(Clone)]
pub struct LoRAPair {
/// Down projection matrix A: [dim][rank]
/// Stored as Vec<[f32; 2]> for rank-2
a: Vec<[f32; 2]>,
/// Up projection matrix B: [rank][dim]
/// Stored as [[f32; 256]; 2] for fixed 256-dim embeddings
b: [[f32; 256]; 2],
/// Scaling factor
alpha: f32,
/// Learning rate
lr: f32,
/// Embedding dimension
dim: usize,
/// Adaptation count for statistics
adapt_count: u64,
}
impl LoRAPair {
/// Create a new LoRA pair with Kaiming initialization for A, zeros for B
pub fn new(config: &LoRAConfig) -> Self {
let dim = config.dim.min(256); // Cap at 256 for fixed-size B
let rank = config.rank.min(2); // Cap at 2 for micro
// Initialize A with small random values (Kaiming-like)
// Using deterministic pseudo-random for reproducibility
let mut a = Vec::with_capacity(dim);
let scale = (2.0 / dim as f32).sqrt() * 0.1; // Small initialization
for i in 0..dim {
let seed = i as u32;
let r0 = pseudo_random(seed) * scale - scale / 2.0;
let r1 = if rank > 1 {
pseudo_random(seed.wrapping_add(1000)) * scale - scale / 2.0
} else {
0.0
};
a.push([r0, r1]);
}
// B initialized to zeros (LoRA standard practice)
let b = [[0.0f32; 256]; 2];
Self {
a,
b,
alpha: config.alpha,
lr: config.learning_rate,
dim,
adapt_count: 0,
}
}
/// Forward pass: output = input + alpha * (input @ A @ B)
///
/// Complexity: O(d * r + r * d) = O(2dr) for rank r
/// For rank-2, d=256: ~1024 ops = <100us
#[inline]
pub fn forward(&self, input: &[f32]) -> Vec<f32> {
let n = input.len().min(self.dim);
let mut output = input.to_vec();
// Compute low_rank = input @ A (result: [2])
let mut low_rank = [0.0f32; 2];
for i in 0..n {
low_rank[0] += input[i] * self.a[i][0];
low_rank[1] += input[i] * self.a[i][1];
}
// Compute delta = low_rank @ B (result: [dim])
// Output = input + alpha * delta
for i in 0..n {
let delta = low_rank[0] * self.b[0][i] + low_rank[1] * self.b[1][i];
output[i] += self.alpha * delta;
}
output
}
/// Forward pass into pre-allocated buffer (zero-allocation hot path)
#[inline]
pub fn forward_into(&self, input: &[f32], output: &mut [f32]) {
let n = input.len().min(self.dim).min(output.len());
// Copy input to output
output[..n].copy_from_slice(&input[..n]);
// Compute low_rank = input @ A
let mut low_rank = [0.0f32; 2];
for i in 0..n {
low_rank[0] += input[i] * self.a[i][0];
low_rank[1] += input[i] * self.a[i][1];
}
// Add delta to output
for i in 0..n {
let delta = low_rank[0] * self.b[0][i] + low_rank[1] * self.b[1][i];
output[i] += self.alpha * delta;
}
}
/// Adapt weights based on gradient signal
///
/// Uses rank-1 outer product update to B matrix for instant adaptation.
/// Target latency: <100us
#[inline]
pub fn adapt(&mut self, gradient: &[f32]) {
let n = gradient.len().min(self.dim);
// Compute gradient norm for normalization
let mut grad_norm_sq = 0.0f32;
for i in 0..n {
grad_norm_sq += gradient[i] * gradient[i];
}
if grad_norm_sq < 1e-16 {
return; // Skip if gradient is too small
}
let grad_norm = fast_sqrt(grad_norm_sq);
let inv_norm = 1.0 / grad_norm;
// Compute column sums of A for scaling
let mut a_col_sum = [0.0f32; 2];
for i in 0..n {
a_col_sum[0] += self.a[i][0];
a_col_sum[1] += self.a[i][1];
}
// Update B using outer product: B += lr * a_sum * normalized_grad^T
for j in 0..n {
let normalized_grad = gradient[j] * inv_norm;
self.b[0][j] += self.lr * a_col_sum[0] * normalized_grad;
self.b[1][j] += self.lr * a_col_sum[1] * normalized_grad;
}
self.adapt_count += 1;
}
/// Adapt with improvement signal (for reinforcement learning)
///
/// Uses the improvement ratio to scale the update magnitude.
#[inline]
pub fn adapt_with_reward(&mut self, gradient: &[f32], improvement: f32) {
if improvement <= 0.0 {
return; // Only learn from positive improvements
}
let n = gradient.len().min(self.dim);
// Scale learning rate by improvement (clamped)
let scaled_lr = self.lr * improvement.min(2.0);
// Compute gradient norm
let mut grad_norm_sq = 0.0f32;
for i in 0..n {
grad_norm_sq += gradient[i] * gradient[i];
}
if grad_norm_sq < 1e-16 {
return;
}
let inv_norm = 1.0 / fast_sqrt(grad_norm_sq);
// Compute A column sums
let mut a_col_sum = [0.0f32; 2];
for i in 0..n {
a_col_sum[0] += self.a[i][0];
a_col_sum[1] += self.a[i][1];
}
// Update B
for j in 0..n {
let normalized_grad = gradient[j] * inv_norm;
self.b[0][j] += scaled_lr * a_col_sum[0] * normalized_grad;
self.b[1][j] += scaled_lr * a_col_sum[1] * normalized_grad;
}
self.adapt_count += 1;
}
/// Reset B matrix to zeros (fresh start)
pub fn reset(&mut self) {
for i in 0..256 {
self.b[0][i] = 0.0;
self.b[1][i] = 0.0;
}
self.adapt_count = 0;
}
/// Get the number of adaptations performed
pub fn adapt_count(&self) -> u64 {
self.adapt_count
}
/// Get the effective weight delta norm (for monitoring)
pub fn delta_norm(&self) -> f32 {
let mut norm_sq = 0.0f32;
for i in 0..self.dim {
let delta = self.b[0][i] * self.b[0][i] + self.b[1][i] * self.b[1][i];
norm_sq += delta;
}
fast_sqrt(norm_sq) * self.alpha
}
/// Get parameter count
pub fn param_count(&self) -> usize {
self.a.len() * 2 + 256 * 2
}
}
/// Main MicroLoRA engine managing multiple LoRA pairs
pub struct MicroLoRAEngine {
/// Default LoRA pair for unscoped operations
default_lora: LoRAPair,
/// Configuration (kept for potential future use)
#[allow(dead_code)]
config: LoRAConfig,
/// Total forward passes
forward_count: u64,
/// Total adaptations
total_adapt_count: u64,
}
impl MicroLoRAEngine {
/// Create a new MicroLoRA engine
pub fn new(config: LoRAConfig) -> Self {
Self {
default_lora: LoRAPair::new(&config),
config,
forward_count: 0,
total_adapt_count: 0,
}
}
/// Forward pass through the default LoRA
#[inline]
pub fn forward(&mut self, input: &[f32]) -> Vec<f32> {
self.forward_count += 1;
self.default_lora.forward(input)
}
/// Adapt the default LoRA with gradient
#[inline]
pub fn adapt(&mut self, gradient: &[f32]) {
self.default_lora.adapt(gradient);
self.total_adapt_count += 1;
}
/// Adapt with improvement reward
#[inline]
pub fn adapt_with_reward(&mut self, gradient: &[f32], improvement: f32) {
self.default_lora.adapt_with_reward(gradient, improvement);
self.total_adapt_count += 1;
}
/// Reset the engine
pub fn reset(&mut self) {
self.default_lora.reset();
self.forward_count = 0;
self.total_adapt_count = 0;
}
/// Get statistics
pub fn stats(&self) -> (u64, u64, f32) {
(
self.forward_count,
self.total_adapt_count,
self.default_lora.delta_norm(),
)
}
/// Get the underlying LoRA pair for advanced use
pub fn lora(&self) -> &LoRAPair {
&self.default_lora
}
/// Get mutable reference to underlying LoRA
pub fn lora_mut(&mut self) -> &mut LoRAPair {
&mut self.default_lora
}
}
impl Default for MicroLoRAEngine {
fn default() -> Self {
Self::new(LoRAConfig::default())
}
}
// ============ Helper Functions ============
/// Fast inverse square root (Quake III style)
#[inline(always)]
fn fast_sqrt(x: f32) -> f32 {
if x <= 0.0 {
return 0.0;
}
let i = 0x5f3759df - (x.to_bits() >> 1);
let y = f32::from_bits(i);
x * y * (1.5 - 0.5 * x * y * y)
}
/// Deterministic pseudo-random number generator
#[inline(always)]
fn pseudo_random(seed: u32) -> f32 {
// Simple xorshift
let mut x = seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
(x as f32) / (u32::MAX as f32)
}
// ============ WASM Bindings ============
pub mod wasm_exports {
use super::*;
#[allow(unused_imports)]
use wasm_bindgen::prelude::*;
/// WASM-exposed MicroLoRA engine
#[wasm_bindgen]
pub struct WasmMicroLoRA {
engine: MicroLoRAEngine,
// Pre-allocated buffers for zero-allocation hot paths
input_buffer: Vec<f32>,
output_buffer: Vec<f32>,
}
#[wasm_bindgen]
impl WasmMicroLoRA {
/// Create a new MicroLoRA engine
///
/// @param dim - Embedding dimension (default 256, max 256)
/// @param alpha - Scaling factor (default 0.1)
/// @param learning_rate - Learning rate (default 0.01)
#[wasm_bindgen(constructor)]
pub fn new(dim: Option<usize>, alpha: Option<f32>, learning_rate: Option<f32>) -> Self {
let config = LoRAConfig {
dim: dim.unwrap_or(256).min(256),
rank: 2,
alpha: alpha.unwrap_or(0.1),
learning_rate: learning_rate.unwrap_or(0.01),
dropout: 0.0,
};
let actual_dim = config.dim;
Self {
engine: MicroLoRAEngine::new(config),
input_buffer: vec![0.0; actual_dim],
output_buffer: vec![0.0; actual_dim],
}
}
/// Get pointer to input buffer for direct memory access
#[wasm_bindgen]
pub fn get_input_ptr(&mut self) -> *mut f32 {
self.input_buffer.as_mut_ptr()
}
/// Get pointer to output buffer for direct memory access
#[wasm_bindgen]
pub fn get_output_ptr(&self) -> *const f32 {
self.output_buffer.as_ptr()
}
/// Get embedding dimension
#[wasm_bindgen]
pub fn dim(&self) -> usize {
self.input_buffer.len()
}
/// Forward pass using internal buffers (zero-allocation)
///
/// Write input to get_input_ptr(), call forward(), read from get_output_ptr()
#[wasm_bindgen]
pub fn forward(&mut self) {
self.engine
.default_lora
.forward_into(&self.input_buffer, &mut self.output_buffer);
self.engine.forward_count += 1;
}
/// Forward pass with typed array input (allocates output)
#[wasm_bindgen]
pub fn forward_array(&mut self, input: &[f32]) -> Vec<f32> {
self.engine.forward(input)
}
/// Adapt using input buffer as gradient
#[wasm_bindgen]
pub fn adapt(&mut self) {
self.engine.adapt(&self.input_buffer.clone());
}
/// Adapt with typed array gradient
#[wasm_bindgen]
pub fn adapt_array(&mut self, gradient: &[f32]) {
self.engine.adapt(gradient);
}
/// Adapt with improvement reward using input buffer as gradient
#[wasm_bindgen]
pub fn adapt_with_reward(&mut self, improvement: f32) {
self.engine
.adapt_with_reward(&self.input_buffer.clone(), improvement);
}
/// Reset the engine
#[wasm_bindgen]
pub fn reset(&mut self) {
self.engine.reset();
}
/// Get forward pass count
#[wasm_bindgen]
pub fn forward_count(&self) -> u64 {
self.engine.forward_count
}
/// Get adaptation count
#[wasm_bindgen]
pub fn adapt_count(&self) -> u64 {
self.engine.total_adapt_count
}
/// Get delta norm (weight change magnitude)
#[wasm_bindgen]
pub fn delta_norm(&self) -> f32 {
self.engine.default_lora.delta_norm()
}
/// Get parameter count
#[wasm_bindgen]
pub fn param_count(&self) -> usize {
self.engine.default_lora.param_count()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lora_pair_creation() {
let config = LoRAConfig::default();
let lora = LoRAPair::new(&config);
assert_eq!(lora.dim, 256);
assert_eq!(lora.adapt_count, 0);
}
#[test]
fn test_lora_forward() {
let config = LoRAConfig::default();
let lora = LoRAPair::new(&config);
let input = vec![1.0; 256];
let output = lora.forward(&input);
assert_eq!(output.len(), 256);
// Initially B is zeros, so output should equal input
for i in 0..256 {
assert!((output[i] - input[i]).abs() < 1e-6);
}
}
#[test]
fn test_lora_adapt() {
let config = LoRAConfig::default();
let mut lora = LoRAPair::new(&config);
let gradient = vec![0.1; 256];
lora.adapt(&gradient);
assert_eq!(lora.adapt_count, 1);
assert!(lora.delta_norm() > 0.0);
}
#[test]
fn test_lora_forward_after_adapt() {
let config = LoRAConfig::default();
let mut lora = LoRAPair::new(&config);
// Adapt
let gradient = vec![0.1; 256];
lora.adapt(&gradient);
// Forward should now produce different output
let input = vec![1.0; 256];
let output = lora.forward(&input);
// Output should differ from input after adaptation
let mut diff = 0.0f32;
for i in 0..256 {
diff += (output[i] - input[i]).abs();
}
assert!(
diff > 0.0,
"Output should differ from input after adaptation"
);
}
#[test]
fn test_engine_stats() {
let mut engine = MicroLoRAEngine::default();
let input = vec![1.0; 256];
let _ = engine.forward(&input);
engine.adapt(&input);
let (forwards, adapts, delta) = engine.stats();
assert_eq!(forwards, 1);
assert_eq!(adapts, 1);
assert!(delta >= 0.0);
}
}

View File

@@ -0,0 +1,523 @@
//! Per-Operator-Type Scoped LoRA
//!
//! Maintains separate LoRA adapters for different operator types,
//! enabling specialized learning for each query operator.
use crate::lora::{LoRAConfig, LoRAPair};
use wasm_bindgen::prelude::*;
/// Operator types for scoping (matches ruvector-dag OperatorType)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum OperatorScope {
// Scan operators (0-3)
SeqScan = 0,
IndexScan = 1,
HnswScan = 2,
IvfFlatScan = 3,
// Join operators (4-6)
NestedLoopJoin = 4,
HashJoin = 5,
MergeJoin = 6,
// Aggregation (7-8)
Aggregate = 7,
GroupBy = 8,
// Filter/Project (9-10)
Filter = 9,
Project = 10,
// Sort/Limit (11-12)
Sort = 11,
Limit = 12,
// Vector operations (13-14)
VectorDistance = 13,
Rerank = 14,
// Utility (15-16)
Materialize = 15,
Result = 16,
}
impl OperatorScope {
/// Convert from u8
pub fn from_u8(v: u8) -> Option<Self> {
match v {
0 => Some(Self::SeqScan),
1 => Some(Self::IndexScan),
2 => Some(Self::HnswScan),
3 => Some(Self::IvfFlatScan),
4 => Some(Self::NestedLoopJoin),
5 => Some(Self::HashJoin),
6 => Some(Self::MergeJoin),
7 => Some(Self::Aggregate),
8 => Some(Self::GroupBy),
9 => Some(Self::Filter),
10 => Some(Self::Project),
11 => Some(Self::Sort),
12 => Some(Self::Limit),
13 => Some(Self::VectorDistance),
14 => Some(Self::Rerank),
15 => Some(Self::Materialize),
16 => Some(Self::Result),
_ => None,
}
}
/// Get category for grouped learning
pub fn category(&self) -> OperatorCategory {
match self {
Self::SeqScan | Self::IndexScan | Self::HnswScan | Self::IvfFlatScan => {
OperatorCategory::Scan
}
Self::NestedLoopJoin | Self::HashJoin | Self::MergeJoin => OperatorCategory::Join,
Self::Aggregate | Self::GroupBy => OperatorCategory::Aggregation,
Self::Filter | Self::Project => OperatorCategory::Transform,
Self::Sort | Self::Limit => OperatorCategory::Order,
Self::VectorDistance | Self::Rerank => OperatorCategory::Vector,
Self::Materialize | Self::Result => OperatorCategory::Utility,
}
}
}
/// High-level operator categories for shared learning
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum OperatorCategory {
Scan = 0,
Join = 1,
Aggregation = 2,
Transform = 3,
Order = 4,
Vector = 5,
Utility = 6,
}
/// Scoped LoRA manager with per-operator-type adapters
///
/// Maintains 17 separate LoRA pairs (one per OperatorScope) for
/// specialized learning based on query operator type.
pub struct ScopedLoRA {
/// Per-operator-type LoRA pairs (17 total)
adapters: [LoRAPair; 17],
/// Per-category LoRA pairs for fallback (7 total)
category_adapters: [LoRAPair; 7],
/// Configuration (kept for potential future use)
#[allow(dead_code)]
config: LoRAConfig,
/// Whether to use category fallback when operator has no history
use_category_fallback: bool,
/// Per-operator forward counts
forward_counts: [u64; 17],
}
impl ScopedLoRA {
/// Create a new scoped LoRA manager
pub fn new(config: LoRAConfig) -> Self {
// Initialize all 17 operator adapters
let adapters = std::array::from_fn(|_| LoRAPair::new(&config));
let category_adapters = std::array::from_fn(|_| LoRAPair::new(&config));
Self {
adapters,
category_adapters,
config,
use_category_fallback: true,
forward_counts: [0; 17],
}
}
/// Forward pass for a specific operator type
#[inline]
pub fn forward(&mut self, scope: OperatorScope, input: &[f32]) -> Vec<f32> {
let idx = scope as usize;
self.forward_counts[idx] += 1;
// Use operator-specific adapter
let output = self.adapters[idx].forward(input);
// If using fallback and this operator has little history,
// blend with category adapter
if self.use_category_fallback && self.adapters[idx].adapt_count() < 10 {
let cat_idx = scope.category() as usize;
let cat_output = self.category_adapters[cat_idx].forward(input);
// Blend based on relative experience
let op_exp = self.adapters[idx].adapt_count() as f32;
let weight = (op_exp / 10.0).min(1.0);
let mut blended = output;
for i in 0..blended.len().min(cat_output.len()) {
blended[i] = blended[i] * weight + cat_output[i] * (1.0 - weight);
}
return blended;
}
output
}
/// Adapt the adapter for a specific operator type
#[inline]
pub fn adapt(&mut self, scope: OperatorScope, gradient: &[f32]) {
let idx = scope as usize;
self.adapters[idx].adapt(gradient);
// Also update category adapter for transfer learning
let cat_idx = scope.category() as usize;
self.category_adapters[cat_idx].adapt(gradient);
}
/// Adapt with improvement reward
#[inline]
pub fn adapt_with_reward(&mut self, scope: OperatorScope, gradient: &[f32], improvement: f32) {
let idx = scope as usize;
self.adapters[idx].adapt_with_reward(gradient, improvement);
// Also update category adapter
let cat_idx = scope.category() as usize;
self.category_adapters[cat_idx].adapt_with_reward(gradient, improvement);
}
/// Reset a specific operator's adapter
pub fn reset_scope(&mut self, scope: OperatorScope) {
let idx = scope as usize;
self.adapters[idx].reset();
self.forward_counts[idx] = 0;
}
/// Reset all adapters
pub fn reset_all(&mut self) {
for adapter in &mut self.adapters {
adapter.reset();
}
for adapter in &mut self.category_adapters {
adapter.reset();
}
self.forward_counts = [0; 17];
}
/// Get statistics for a specific operator
pub fn stats(&self, scope: OperatorScope) -> (u64, u64, f32) {
let idx = scope as usize;
(
self.forward_counts[idx],
self.adapters[idx].adapt_count(),
self.adapters[idx].delta_norm(),
)
}
/// Get total statistics across all operators
pub fn total_stats(&self) -> (u64, u64, f32) {
let total_forwards: u64 = self.forward_counts.iter().sum();
let total_adapts: u64 = self.adapters.iter().map(|a| a.adapt_count()).sum();
let max_delta: f32 = self
.adapters
.iter()
.map(|a| a.delta_norm())
.fold(0.0, f32::max);
(total_forwards, total_adapts, max_delta)
}
/// Get the most active operator scopes
pub fn most_active(&self, top_n: usize) -> Vec<(OperatorScope, u64)> {
let mut counts: Vec<(usize, u64)> = self
.forward_counts
.iter()
.enumerate()
.map(|(i, &c)| (i, c))
.collect();
counts.sort_by(|a, b| b.1.cmp(&a.1));
counts
.into_iter()
.take(top_n)
.filter_map(|(idx, count)| {
OperatorScope::from_u8(idx as u8).map(|scope| (scope, count))
})
.collect()
}
/// Set category fallback mode
pub fn set_category_fallback(&mut self, enabled: bool) {
self.use_category_fallback = enabled;
}
/// Get reference to operator adapter
pub fn adapter(&self, scope: OperatorScope) -> &LoRAPair {
&self.adapters[scope as usize]
}
/// Get mutable reference to operator adapter
pub fn adapter_mut(&mut self, scope: OperatorScope) -> &mut LoRAPair {
&mut self.adapters[scope as usize]
}
}
impl Default for ScopedLoRA {
fn default() -> Self {
Self::new(LoRAConfig::default())
}
}
// ============ WASM Bindings ============
pub mod wasm_exports {
use super::*;
#[allow(unused_imports)]
use wasm_bindgen::prelude::*;
/// WASM-exposed Scoped LoRA manager
#[wasm_bindgen]
pub struct WasmScopedLoRA {
inner: ScopedLoRA,
input_buffer: Vec<f32>,
output_buffer: Vec<f32>,
}
#[wasm_bindgen]
impl WasmScopedLoRA {
/// Create a new scoped LoRA manager
///
/// @param dim - Embedding dimension (max 256)
/// @param alpha - Scaling factor (default 0.1)
/// @param learning_rate - Learning rate (default 0.01)
#[wasm_bindgen(constructor)]
pub fn new(dim: Option<usize>, alpha: Option<f32>, learning_rate: Option<f32>) -> Self {
let config = LoRAConfig {
dim: dim.unwrap_or(256).min(256),
rank: 2,
alpha: alpha.unwrap_or(0.1),
learning_rate: learning_rate.unwrap_or(0.01),
dropout: 0.0,
};
let actual_dim = config.dim;
Self {
inner: ScopedLoRA::new(config),
input_buffer: vec![0.0; actual_dim],
output_buffer: vec![0.0; actual_dim],
}
}
/// Get input buffer pointer
#[wasm_bindgen]
pub fn get_input_ptr(&mut self) -> *mut f32 {
self.input_buffer.as_mut_ptr()
}
/// Get output buffer pointer
#[wasm_bindgen]
pub fn get_output_ptr(&self) -> *const f32 {
self.output_buffer.as_ptr()
}
/// Forward pass for operator type (uses internal buffers)
///
/// @param op_type - Operator type (0-16)
#[wasm_bindgen]
pub fn forward(&mut self, op_type: u8) {
if let Some(scope) = OperatorScope::from_u8(op_type) {
let output = self.inner.forward(scope, &self.input_buffer);
let n = output.len().min(self.output_buffer.len());
self.output_buffer[..n].copy_from_slice(&output[..n]);
}
}
/// Forward pass with typed array
#[wasm_bindgen]
pub fn forward_array(&mut self, op_type: u8, input: &[f32]) -> Vec<f32> {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.forward(scope, input)
} else {
input.to_vec()
}
}
/// Adapt for operator type using input buffer as gradient
#[wasm_bindgen]
pub fn adapt(&mut self, op_type: u8) {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.adapt(scope, &self.input_buffer.clone());
}
}
/// Adapt with typed array
#[wasm_bindgen]
pub fn adapt_array(&mut self, op_type: u8, gradient: &[f32]) {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.adapt(scope, gradient);
}
}
/// Adapt with improvement reward
#[wasm_bindgen]
pub fn adapt_with_reward(&mut self, op_type: u8, improvement: f32) {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner
.adapt_with_reward(scope, &self.input_buffer.clone(), improvement);
}
}
/// Reset specific operator adapter
#[wasm_bindgen]
pub fn reset_scope(&mut self, op_type: u8) {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.reset_scope(scope);
}
}
/// Reset all adapters
#[wasm_bindgen]
pub fn reset_all(&mut self) {
self.inner.reset_all();
}
/// Get forward count for operator
#[wasm_bindgen]
pub fn forward_count(&self, op_type: u8) -> u64 {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.stats(scope).0
} else {
0
}
}
/// Get adapt count for operator
#[wasm_bindgen]
pub fn adapt_count(&self, op_type: u8) -> u64 {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.stats(scope).1
} else {
0
}
}
/// Get delta norm for operator
#[wasm_bindgen]
pub fn delta_norm(&self, op_type: u8) -> f32 {
if let Some(scope) = OperatorScope::from_u8(op_type) {
self.inner.stats(scope).2
} else {
0.0
}
}
/// Get total forward count
#[wasm_bindgen]
pub fn total_forward_count(&self) -> u64 {
self.inner.total_stats().0
}
/// Get total adapt count
#[wasm_bindgen]
pub fn total_adapt_count(&self) -> u64 {
self.inner.total_stats().1
}
/// Enable/disable category fallback
#[wasm_bindgen]
pub fn set_category_fallback(&mut self, enabled: bool) {
self.inner.set_category_fallback(enabled);
}
/// Get operator scope name
#[wasm_bindgen]
pub fn scope_name(op_type: u8) -> String {
match op_type {
0 => "SeqScan".to_string(),
1 => "IndexScan".to_string(),
2 => "HnswScan".to_string(),
3 => "IvfFlatScan".to_string(),
4 => "NestedLoopJoin".to_string(),
5 => "HashJoin".to_string(),
6 => "MergeJoin".to_string(),
7 => "Aggregate".to_string(),
8 => "GroupBy".to_string(),
9 => "Filter".to_string(),
10 => "Project".to_string(),
11 => "Sort".to_string(),
12 => "Limit".to_string(),
13 => "VectorDistance".to_string(),
14 => "Rerank".to_string(),
15 => "Materialize".to_string(),
16 => "Result".to_string(),
_ => "Unknown".to_string(),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_scoped_lora_creation() {
let lora = ScopedLoRA::default();
let (forwards, adapts, delta) = lora.total_stats();
assert_eq!(forwards, 0);
assert_eq!(adapts, 0);
assert_eq!(delta, 0.0);
}
#[test]
fn test_scoped_forward() {
let mut lora = ScopedLoRA::default();
let input = vec![1.0; 256];
let output = lora.forward(OperatorScope::HnswScan, &input);
assert_eq!(output.len(), 256);
let (forwards, _, _) = lora.stats(OperatorScope::HnswScan);
assert_eq!(forwards, 1);
}
#[test]
fn test_scoped_adapt() {
let mut lora = ScopedLoRA::default();
let gradient = vec![0.1; 256];
lora.adapt(OperatorScope::Filter, &gradient);
let (_, adapts, delta) = lora.stats(OperatorScope::Filter);
assert_eq!(adapts, 1);
assert!(delta > 0.0);
}
#[test]
fn test_category_transfer() {
let mut lora = ScopedLoRA::default();
let gradient = vec![0.1; 256];
// Adapt HnswScan (category: Scan)
lora.adapt(OperatorScope::HnswScan, &gradient);
// SeqScan should benefit from category adapter via fallback
let input = vec![1.0; 256];
let output = lora.forward(OperatorScope::SeqScan, &input);
// With fallback enabled and SeqScan having no history,
// it should use the category adapter which was updated
// This is a behavioral test - output should differ from input
let mut diff = 0.0f32;
for i in 0..256 {
diff += (output[i] - input[i]).abs();
}
// Due to category transfer, there should be some difference
assert!(diff > 0.0, "Category transfer should affect output");
}
#[test]
fn test_operator_scope_conversion() {
for i in 0..=16u8 {
let scope = OperatorScope::from_u8(i);
assert!(scope.is_some(), "Scope {} should be valid", i);
}
assert!(OperatorScope::from_u8(17).is_none());
}
}

View File

@@ -0,0 +1,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());
}
}