Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'
This commit is contained in:
44
vendor/ruvector/crates/ruvector-learning-wasm/Cargo.toml
vendored
Normal file
44
vendor/ruvector/crates/ruvector-learning-wasm/Cargo.toml
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
[package]
|
||||
name = "ruvector-learning-wasm"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "Ultra-fast MicroLoRA adaptation for WASM - rank-2 LoRA with <100us latency for per-operator learning"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/ruvnet/ruvector"
|
||||
homepage = "https://ruv.io"
|
||||
documentation = "https://docs.rs/ruvector-learning-wasm"
|
||||
authors = ["rUv <ruvnet@users.noreply.github.com>"]
|
||||
keywords = ["lora", "machine-learning", "wasm", "neural-network", "adaptation"]
|
||||
categories = ["algorithms", "wasm", "science", "no-std"]
|
||||
rust-version = "1.70"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
wasm-bindgen = "0.2"
|
||||
js-sys = "0.3"
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
serde-wasm-bindgen = { version = "0.6", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
serde = ["dep:serde", "dep:serde-wasm-bindgen"]
|
||||
simd = [] # Enable SIMD optimizations when available
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
panic = "abort"
|
||||
strip = true
|
||||
|
||||
[profile.release.package."*"]
|
||||
opt-level = "z"
|
||||
|
||||
[package.metadata.wasm-pack.profile.release]
|
||||
wasm-opt = false
|
||||
313
vendor/ruvector/crates/ruvector-learning-wasm/README.md
vendored
Normal file
313
vendor/ruvector/crates/ruvector-learning-wasm/README.md
vendored
Normal file
@@ -0,0 +1,313 @@
|
||||
# ruvector-learning-wasm
|
||||
|
||||
Ultra-fast MicroLoRA adaptation for WASM - rank-2 LoRA with <100us latency for per-operator learning.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install ruvector-learning-wasm
|
||||
```
|
||||
|
||||
## Overview
|
||||
|
||||
This package provides Low-Rank Adaptation (LoRA) matrices optimized for WebAssembly execution. It enables real-time per-operator-type learning in query optimization systems with minimal latency overhead.
|
||||
|
||||
### Key Features
|
||||
|
||||
- **Rank-2 LoRA**: Minimal parameter count (2d parameters per adapter)
|
||||
- **Per-Operator Scoping**: Separate adapters for 17 different operator types
|
||||
- **<100us Adaptation**: Instant weight updates for real-time learning
|
||||
- **WASM-Optimized**: Compiled to WebAssembly for near-native performance
|
||||
- **Zero-Allocation Hot Paths**: Pre-allocated buffers for performance-critical operations
|
||||
|
||||
## JavaScript API
|
||||
|
||||
### WasmMicroLoRA
|
||||
|
||||
The main LoRA engine for single-adapter use cases.
|
||||
|
||||
```typescript
|
||||
import init, { WasmMicroLoRA } from 'ruvector-learning-wasm';
|
||||
|
||||
// Initialize WASM module
|
||||
await init();
|
||||
|
||||
// Create a new MicroLoRA engine
|
||||
const lora = new WasmMicroLoRA(
|
||||
256, // dim: Embedding dimension (max 256)
|
||||
0.1, // alpha: Scaling factor
|
||||
0.01 // learning_rate: Learning rate for adaptation
|
||||
);
|
||||
|
||||
// Forward pass with typed array
|
||||
const input = new Float32Array(256).fill(1.0);
|
||||
const output = lora.forward_array(input);
|
||||
|
||||
// Adapt with gradient
|
||||
const gradient = new Float32Array(256).fill(0.1);
|
||||
lora.adapt_array(gradient);
|
||||
|
||||
// Get statistics
|
||||
console.log('Forward count:', lora.forward_count());
|
||||
console.log('Adapt count:', lora.adapt_count());
|
||||
console.log('Delta norm:', lora.delta_norm());
|
||||
console.log('Parameter count:', lora.param_count());
|
||||
|
||||
// Reset engine
|
||||
lora.reset();
|
||||
```
|
||||
|
||||
#### Zero-Allocation API
|
||||
|
||||
For performance-critical loops, use the buffer-based API:
|
||||
|
||||
```typescript
|
||||
const lora = new WasmMicroLoRA(256, 0.1, 0.01);
|
||||
|
||||
// Get buffer pointers
|
||||
const inputPtr = lora.get_input_ptr();
|
||||
const outputPtr = lora.get_output_ptr();
|
||||
const dim = lora.dim();
|
||||
|
||||
// Create views into WASM memory
|
||||
const memory = new Float32Array(lora.memory.buffer);
|
||||
const inputView = new Float32Array(memory.buffer, inputPtr, dim);
|
||||
const outputView = new Float32Array(memory.buffer, outputPtr, dim);
|
||||
|
||||
// Write input directly
|
||||
inputView.set(myInputData);
|
||||
|
||||
// Forward pass (zero allocation)
|
||||
lora.forward();
|
||||
|
||||
// Read output directly
|
||||
const result = outputView.slice();
|
||||
|
||||
// Adapt using input buffer as gradient
|
||||
lora.adapt();
|
||||
|
||||
// Adapt with reward (for RL)
|
||||
lora.adapt_with_reward(0.5); // improvement ratio
|
||||
```
|
||||
|
||||
### WasmScopedLoRA
|
||||
|
||||
Per-operator-type LoRA manager with 17 specialized adapters plus category fallback.
|
||||
|
||||
```typescript
|
||||
import init, { WasmScopedLoRA } from 'ruvector-learning-wasm';
|
||||
|
||||
await init();
|
||||
|
||||
const scopedLora = new WasmScopedLoRA(
|
||||
256, // dim
|
||||
0.1, // alpha
|
||||
0.01 // learning_rate
|
||||
);
|
||||
|
||||
// Operator types (0-16)
|
||||
const HNSW_SCAN = 2;
|
||||
const HASH_JOIN = 5;
|
||||
const FILTER = 9;
|
||||
|
||||
// Forward for specific operator
|
||||
const input = new Float32Array(256).fill(1.0);
|
||||
const output = scopedLora.forward_array(HNSW_SCAN, input);
|
||||
|
||||
// Adapt for specific operator
|
||||
const gradient = new Float32Array(256).fill(0.1);
|
||||
scopedLora.adapt_array(FILTER, gradient);
|
||||
|
||||
// Per-operator statistics
|
||||
console.log('HNSW forward count:', scopedLora.forward_count(HNSW_SCAN));
|
||||
console.log('Filter adapt count:', scopedLora.adapt_count(FILTER));
|
||||
console.log('Filter delta norm:', scopedLora.delta_norm(FILTER));
|
||||
|
||||
// Total statistics
|
||||
console.log('Total forwards:', scopedLora.total_forward_count());
|
||||
console.log('Total adapts:', scopedLora.total_adapt_count());
|
||||
|
||||
// Get operator name
|
||||
console.log(WasmScopedLoRA.scope_name(HNSW_SCAN)); // "HnswScan"
|
||||
|
||||
// Enable/disable category fallback (default: enabled)
|
||||
scopedLora.set_category_fallback(true);
|
||||
|
||||
// Reset specific operator or all
|
||||
scopedLora.reset_scope(FILTER);
|
||||
scopedLora.reset_all();
|
||||
```
|
||||
|
||||
#### Operator Types
|
||||
|
||||
| Value | Name | Category |
|
||||
|-------|------|----------|
|
||||
| 0 | SeqScan | Scan |
|
||||
| 1 | IndexScan | Scan |
|
||||
| 2 | HnswScan | Scan |
|
||||
| 3 | IvfFlatScan | Scan |
|
||||
| 4 | NestedLoopJoin | Join |
|
||||
| 5 | HashJoin | Join |
|
||||
| 6 | MergeJoin | Join |
|
||||
| 7 | Aggregate | Aggregation |
|
||||
| 8 | GroupBy | Aggregation |
|
||||
| 9 | Filter | Transform |
|
||||
| 10 | Project | Transform |
|
||||
| 11 | Sort | Order |
|
||||
| 12 | Limit | Order |
|
||||
| 13 | VectorDistance | Vector |
|
||||
| 14 | Rerank | Vector |
|
||||
| 15 | Materialize | Utility |
|
||||
| 16 | Result | Utility |
|
||||
|
||||
### WasmTrajectoryBuffer
|
||||
|
||||
Trajectory recording for reinforcement learning and pattern analysis.
|
||||
|
||||
```typescript
|
||||
import init, { WasmTrajectoryBuffer } from 'ruvector-learning-wasm';
|
||||
|
||||
await init();
|
||||
|
||||
const buffer = new WasmTrajectoryBuffer(
|
||||
1000, // capacity: max trajectories to store
|
||||
256 // embedding_dim
|
||||
);
|
||||
|
||||
// Record a trajectory
|
||||
const embedding = new Float32Array(256).fill(1.0);
|
||||
buffer.record(
|
||||
embedding,
|
||||
2, // op_type: HnswScan
|
||||
0, // attention_type
|
||||
100.0, // execution_ms
|
||||
150.0 // baseline_ms (improvement = 150/100 - 1 = 0.5)
|
||||
);
|
||||
|
||||
// Get statistics
|
||||
console.log('Total count:', buffer.total_count());
|
||||
console.log('Buffer length:', buffer.len());
|
||||
console.log('Mean improvement:', buffer.mean_improvement());
|
||||
console.log('Best improvement:', buffer.best_improvement());
|
||||
console.log('Success rate:', buffer.success_rate());
|
||||
console.log('Best attention type:', buffer.best_attention());
|
||||
console.log('Variance:', buffer.variance());
|
||||
|
||||
// Filter by quality
|
||||
console.log('High quality count:', buffer.high_quality_count(0.5));
|
||||
|
||||
// Filter by operator
|
||||
console.log('HnswScan trajectories:', buffer.count_by_operator(2));
|
||||
|
||||
// Reset buffer
|
||||
buffer.reset();
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Input Embedding (d-dim)
|
||||
|
|
||||
v
|
||||
+---------+
|
||||
| A: d x 2 | Down projection
|
||||
+---------+
|
||||
|
|
||||
v
|
||||
+---------+
|
||||
| B: 2 x d | Up projection (initialized to zero)
|
||||
+---------+
|
||||
|
|
||||
v
|
||||
Delta W = alpha * (A @ B)
|
||||
|
|
||||
v
|
||||
Output = Input + Delta W
|
||||
```
|
||||
|
||||
### Category Fallback
|
||||
|
||||
When an operator has fewer than 10 adaptations, the output is blended with the category adapter based on relative experience:
|
||||
|
||||
```
|
||||
weight = min(operator_adapt_count / 10, 1.0)
|
||||
output = operator_output * weight + category_output * (1 - weight)
|
||||
```
|
||||
|
||||
This enables transfer learning between similar operators (e.g., all scan types share Scan category knowledge).
|
||||
|
||||
## Performance
|
||||
|
||||
- **Forward pass**: ~50us for 256-dim embeddings
|
||||
- **Adaptation**: ~30us for gradient update
|
||||
- **Memory**: ~2KB per LoRA pair (A + B matrices)
|
||||
- **WASM size**: ~39KB (release build)
|
||||
|
||||
## TypeScript Types
|
||||
|
||||
Full TypeScript definitions are included in the package:
|
||||
|
||||
```typescript
|
||||
export class WasmMicroLoRA {
|
||||
constructor(dim?: number, alpha?: number, learning_rate?: number);
|
||||
get_input_ptr(): number;
|
||||
get_output_ptr(): number;
|
||||
dim(): number;
|
||||
forward(): void;
|
||||
forward_array(input: Float32Array): Float32Array;
|
||||
adapt(): void;
|
||||
adapt_array(gradient: Float32Array): void;
|
||||
adapt_with_reward(improvement: number): void;
|
||||
reset(): void;
|
||||
forward_count(): bigint;
|
||||
adapt_count(): bigint;
|
||||
delta_norm(): number;
|
||||
param_count(): number;
|
||||
}
|
||||
|
||||
export class WasmScopedLoRA {
|
||||
constructor(dim?: number, alpha?: number, learning_rate?: number);
|
||||
get_input_ptr(): number;
|
||||
get_output_ptr(): number;
|
||||
forward(op_type: number): void;
|
||||
forward_array(op_type: number, input: Float32Array): Float32Array;
|
||||
adapt(op_type: number): void;
|
||||
adapt_array(op_type: number, gradient: Float32Array): void;
|
||||
adapt_with_reward(op_type: number, improvement: number): void;
|
||||
reset_scope(op_type: number): void;
|
||||
reset_all(): void;
|
||||
forward_count(op_type: number): bigint;
|
||||
adapt_count(op_type: number): bigint;
|
||||
delta_norm(op_type: number): number;
|
||||
total_forward_count(): bigint;
|
||||
total_adapt_count(): bigint;
|
||||
set_category_fallback(enabled: boolean): void;
|
||||
static scope_name(op_type: number): string;
|
||||
}
|
||||
|
||||
export class WasmTrajectoryBuffer {
|
||||
constructor(capacity?: number, embedding_dim?: number);
|
||||
record(
|
||||
embedding: Float32Array,
|
||||
op_type: number,
|
||||
attention_type: number,
|
||||
execution_ms: number,
|
||||
baseline_ms: number
|
||||
): void;
|
||||
total_count(): bigint;
|
||||
len(): number;
|
||||
is_empty(): boolean;
|
||||
mean_improvement(): number;
|
||||
best_improvement(): number;
|
||||
success_rate(): number;
|
||||
best_attention(): number;
|
||||
variance(): number;
|
||||
reset(): void;
|
||||
high_quality_count(threshold: number): number;
|
||||
count_by_operator(op_type: number): number;
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT OR Apache-2.0
|
||||
232
vendor/ruvector/crates/ruvector-learning-wasm/pkg/README.md
vendored
Normal file
232
vendor/ruvector/crates/ruvector-learning-wasm/pkg/README.md
vendored
Normal file
@@ -0,0 +1,232 @@
|
||||
# @ruvector/learning-wasm - Ultra-Fast MicroLoRA for WebAssembly
|
||||
|
||||
[](https://www.npmjs.com/package/ruvector-learning-wasm)
|
||||
[](https://github.com/ruvnet/ruvector)
|
||||
[](https://www.npmjs.com/package/ruvector-learning-wasm)
|
||||
[](https://webassembly.org/)
|
||||
|
||||
Ultra-fast **Low-Rank Adaptation (LoRA)** for WebAssembly with sub-100 microsecond adaptation latency. Designed for real-time per-operator-type learning in query optimization systems, edge AI, and browser-based machine learning applications.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Rank-2 LoRA Architecture**: Minimal parameter count (2d parameters per adapter) for efficient edge deployment
|
||||
- **Sub-100us Adaptation Latency**: Instant weight updates enabling real-time learning
|
||||
- **Per-Operator Scoping**: Separate adapters for 17 different operator types (scan, filter, join, aggregate, etc.)
|
||||
- **Zero-Allocation Forward Pass**: Direct memory access for maximum performance
|
||||
- **Trajectory Buffer**: Track learning history with success rate analytics
|
||||
- **WASM-Optimized**: no_std compatible with minimal allocations
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install ruvector-learning-wasm
|
||||
# or
|
||||
yarn add ruvector-learning-wasm
|
||||
# or
|
||||
pnpm add ruvector-learning-wasm
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### TypeScript/JavaScript
|
||||
|
||||
```typescript
|
||||
import init, { WasmMicroLoRA, WasmScopedLoRA, WasmTrajectoryBuffer } from 'ruvector-learning-wasm';
|
||||
|
||||
// Initialize WASM module
|
||||
await init();
|
||||
|
||||
// Create a MicroLoRA engine (256-dim embeddings)
|
||||
const lora = new WasmMicroLoRA(256, 0.1, 0.01);
|
||||
|
||||
// Forward pass with typed arrays
|
||||
const input = new Float32Array(256).fill(0.1);
|
||||
const output = lora.forward_array(input);
|
||||
|
||||
// Adapt based on gradient
|
||||
const gradient = new Float32Array(256);
|
||||
gradient.fill(0.05);
|
||||
lora.adapt_array(gradient);
|
||||
|
||||
// Or use reward-based adaptation
|
||||
lora.adapt_with_reward(0.15); // 15% improvement
|
||||
|
||||
console.log(`Adaptations: ${lora.adapt_count()}`);
|
||||
console.log(`Delta norm: ${lora.delta_norm()}`);
|
||||
```
|
||||
|
||||
### Zero-Allocation Forward Pass
|
||||
|
||||
For maximum performance, use direct memory access:
|
||||
|
||||
```typescript
|
||||
// Get buffer pointers
|
||||
const inputPtr = lora.get_input_ptr();
|
||||
const outputPtr = lora.get_output_ptr();
|
||||
|
||||
// Write directly to WASM memory
|
||||
const memory = new Float32Array(wasmInstance.memory.buffer, inputPtr, 256);
|
||||
memory.set(inputData);
|
||||
|
||||
// Execute forward pass (zero allocation)
|
||||
lora.forward();
|
||||
|
||||
// Read output directly from WASM memory
|
||||
const result = new Float32Array(wasmInstance.memory.buffer, outputPtr, 256);
|
||||
```
|
||||
|
||||
### Per-Operator Scoped LoRA
|
||||
|
||||
```typescript
|
||||
import { WasmScopedLoRA } from 'ruvector-learning-wasm';
|
||||
|
||||
const scopedLora = new WasmScopedLoRA(256, 0.1, 0.01);
|
||||
|
||||
// Operator types: 0=Scan, 1=Filter, 2=Join, 3=Aggregate, 4=Project, 5=Sort, ...
|
||||
const SCAN_OP = 0;
|
||||
const JOIN_OP = 2;
|
||||
|
||||
// Forward pass for specific operator
|
||||
const scanOutput = scopedLora.forward_array(SCAN_OP, input);
|
||||
|
||||
// Adapt specific operator based on improvement
|
||||
scopedLora.adapt_with_reward(JOIN_OP, 0.25);
|
||||
|
||||
// Get operator name
|
||||
console.log(WasmScopedLoRA.scope_name(SCAN_OP)); // "Scan"
|
||||
|
||||
// Check per-operator statistics
|
||||
console.log(`Scan adaptations: ${scopedLora.adapt_count(SCAN_OP)}`);
|
||||
console.log(`Total adaptations: ${scopedLora.total_adapt_count()}`);
|
||||
```
|
||||
|
||||
### Trajectory Tracking
|
||||
|
||||
```typescript
|
||||
import { WasmTrajectoryBuffer } from 'ruvector-learning-wasm';
|
||||
|
||||
const buffer = new WasmTrajectoryBuffer(1000, 256);
|
||||
|
||||
// Record trajectories
|
||||
buffer.record(
|
||||
embedding, // Float32Array
|
||||
2, // operator type (JOIN)
|
||||
5, // attention mechanism used
|
||||
45.2, // actual execution time (ms)
|
||||
120.5 // baseline execution time (ms)
|
||||
);
|
||||
|
||||
// Analyze learning progress
|
||||
console.log(`Success rate: ${(buffer.success_rate() * 100).toFixed(1)}%`);
|
||||
console.log(`Best improvement: ${buffer.best_improvement()}x`);
|
||||
console.log(`Mean improvement: ${buffer.mean_improvement()}x`);
|
||||
console.log(`Best attention mechanism: ${buffer.best_attention()}`);
|
||||
|
||||
// Filter high-quality trajectories
|
||||
const topTrajectories = buffer.high_quality_count(0.5); // >50% improvement
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Input Embedding (d-dim)
|
||||
|
|
||||
v
|
||||
+---------+
|
||||
| A: d x 2 | Down projection (d -> 2)
|
||||
+---------+
|
||||
|
|
||||
v
|
||||
+---------+
|
||||
| B: 2 x d | Up projection (2 -> d)
|
||||
+---------+
|
||||
|
|
||||
v
|
||||
Delta W = alpha * (A @ B)
|
||||
|
|
||||
v
|
||||
Output = Input + Delta W
|
||||
```
|
||||
|
||||
## Performance Benchmarks
|
||||
|
||||
| Operation | Latency | Throughput |
|
||||
|-----------|---------|------------|
|
||||
| Forward (256-dim) | ~15us | 66K ops/sec |
|
||||
| Adapt (gradient) | ~25us | 40K ops/sec |
|
||||
| Forward (zero-alloc) | ~8us | 125K ops/sec |
|
||||
| Scoped forward | ~20us | 50K ops/sec |
|
||||
| Trajectory record | ~5us | 200K ops/sec |
|
||||
|
||||
Tested on Chrome 120+ / Node.js 20+ with WASM SIMD support.
|
||||
|
||||
## API Reference
|
||||
|
||||
### WasmMicroLoRA
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `new(dim?, alpha?, learning_rate?)` | Create engine (defaults: 256, 0.1, 0.01) |
|
||||
| `forward_array(input)` | Forward pass with Float32Array |
|
||||
| `forward()` | Zero-allocation forward using buffers |
|
||||
| `adapt_array(gradient)` | Adapt with gradient vector |
|
||||
| `adapt_with_reward(improvement)` | Reward-based adaptation |
|
||||
| `delta_norm()` | Get weight change magnitude |
|
||||
| `adapt_count()` | Number of adaptations |
|
||||
| `reset()` | Reset to initial state |
|
||||
|
||||
### WasmScopedLoRA
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `new(dim?, alpha?, learning_rate?)` | Create scoped manager |
|
||||
| `forward_array(op_type, input)` | Forward for operator |
|
||||
| `adapt_with_reward(op_type, improvement)` | Operator-specific adaptation |
|
||||
| `scope_name(op_type)` | Get operator name (static) |
|
||||
| `total_adapt_count()` | Total adaptations across all operators |
|
||||
| `set_category_fallback(enabled)` | Enable category fallback |
|
||||
|
||||
### WasmTrajectoryBuffer
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `new(capacity?, embedding_dim?)` | Create buffer |
|
||||
| `record(embedding, op_type, attention_type, exec_ms, baseline_ms)` | Record trajectory |
|
||||
| `success_rate()` | Get success rate (0.0-1.0) |
|
||||
| `best_improvement()` | Get best improvement ratio |
|
||||
| `mean_improvement()` | Get mean improvement ratio |
|
||||
| `high_quality_count(threshold)` | Count trajectories above threshold |
|
||||
|
||||
## Use Cases
|
||||
|
||||
- **Query Optimization**: Learn optimal attention mechanisms per SQL operator
|
||||
- **Edge AI Personalization**: Real-time model adaptation on user devices
|
||||
- **Browser ML**: In-browser fine-tuning without server round-trips
|
||||
- **Federated Learning**: Lightweight local adaptation for aggregation
|
||||
- **Reinforcement Learning**: Fast policy adaptation from rewards
|
||||
|
||||
## Bundle Size
|
||||
|
||||
- **WASM binary**: ~39KB (uncompressed)
|
||||
- **Gzip compressed**: ~15KB
|
||||
- **JavaScript glue**: ~5KB
|
||||
|
||||
## Related Packages
|
||||
|
||||
- [ruvector-attention-unified-wasm](https://www.npmjs.com/package/ruvector-attention-unified-wasm) - 18+ attention mechanisms
|
||||
- [ruvector-nervous-system-wasm](https://www.npmjs.com/package/ruvector-nervous-system-wasm) - Bio-inspired neural components
|
||||
- [ruvector-economy-wasm](https://www.npmjs.com/package/ruvector-economy-wasm) - CRDT credit economy
|
||||
|
||||
## License
|
||||
|
||||
MIT OR Apache-2.0
|
||||
|
||||
## Links
|
||||
|
||||
- [GitHub Repository](https://github.com/ruvnet/ruvector)
|
||||
- [Full Documentation](https://ruv.io)
|
||||
- [Bug Reports](https://github.com/ruvnet/ruvector/issues)
|
||||
|
||||
---
|
||||
|
||||
**Keywords**: LoRA, Low-Rank Adaptation, machine learning, WASM, WebAssembly, neural network, edge AI, adaptation, fine-tuning, query optimization, real-time learning, micro LoRA, rank-2, browser ML
|
||||
43
vendor/ruvector/crates/ruvector-learning-wasm/pkg/package.json
vendored
Normal file
43
vendor/ruvector/crates/ruvector-learning-wasm/pkg/package.json
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "@ruvector/learning-wasm",
|
||||
"type": "module",
|
||||
"collaborators": [
|
||||
"rUv <ruvnet@users.noreply.github.com>"
|
||||
],
|
||||
"author": "RuVector Team <ruvnet@users.noreply.github.com>",
|
||||
"description": "Ultra-fast MicroLoRA adaptation for WASM - rank-2 LoRA with <100us latency for per-operator learning",
|
||||
"version": "0.1.29",
|
||||
"license": "MIT OR Apache-2.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ruvnet/ruvector"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/ruvnet/ruvector/issues"
|
||||
},
|
||||
"files": [
|
||||
"ruvector_learning_wasm_bg.wasm",
|
||||
"ruvector_learning_wasm.js",
|
||||
"ruvector_learning_wasm.d.ts",
|
||||
"ruvector_learning_wasm_bg.wasm.d.ts",
|
||||
"README.md"
|
||||
],
|
||||
"main": "ruvector_learning_wasm.js",
|
||||
"homepage": "https://ruv.io",
|
||||
"types": "ruvector_learning_wasm.d.ts",
|
||||
"sideEffects": [
|
||||
"./snippets/*"
|
||||
],
|
||||
"keywords": [
|
||||
"lora",
|
||||
"machine-learning",
|
||||
"wasm",
|
||||
"neural-network",
|
||||
"adaptation",
|
||||
"ruvector",
|
||||
"webassembly",
|
||||
"ai",
|
||||
"deep-learning",
|
||||
"micro-lora"
|
||||
]
|
||||
}
|
||||
292
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm.d.ts
vendored
Normal file
292
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm.d.ts
vendored
Normal file
@@ -0,0 +1,292 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
export class WasmMicroLoRA {
|
||||
free(): void;
|
||||
[Symbol.dispose](): void;
|
||||
/**
|
||||
* Get delta norm (weight change magnitude)
|
||||
*/
|
||||
delta_norm(): number;
|
||||
/**
|
||||
* Adapt with typed array gradient
|
||||
*/
|
||||
adapt_array(gradient: Float32Array): void;
|
||||
/**
|
||||
* Get adaptation count
|
||||
*/
|
||||
adapt_count(): bigint;
|
||||
/**
|
||||
* Get parameter count
|
||||
*/
|
||||
param_count(): number;
|
||||
/**
|
||||
* Forward pass with typed array input (allocates output)
|
||||
*/
|
||||
forward_array(input: Float32Array): Float32Array;
|
||||
/**
|
||||
* Get forward pass count
|
||||
*/
|
||||
forward_count(): bigint;
|
||||
/**
|
||||
* Get pointer to input buffer for direct memory access
|
||||
*/
|
||||
get_input_ptr(): number;
|
||||
/**
|
||||
* Get pointer to output buffer for direct memory access
|
||||
*/
|
||||
get_output_ptr(): number;
|
||||
/**
|
||||
* Adapt with improvement reward using input buffer as gradient
|
||||
*/
|
||||
adapt_with_reward(improvement: number): void;
|
||||
/**
|
||||
* Get embedding dimension
|
||||
*/
|
||||
dim(): number;
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
constructor(dim?: number | null, alpha?: number | null, learning_rate?: number | null);
|
||||
/**
|
||||
* Adapt using input buffer as gradient
|
||||
*/
|
||||
adapt(): void;
|
||||
/**
|
||||
* Reset the engine
|
||||
*/
|
||||
reset(): void;
|
||||
/**
|
||||
* Forward pass using internal buffers (zero-allocation)
|
||||
*
|
||||
* Write input to get_input_ptr(), call forward(), read from get_output_ptr()
|
||||
*/
|
||||
forward(): void;
|
||||
}
|
||||
|
||||
export class WasmScopedLoRA {
|
||||
free(): void;
|
||||
[Symbol.dispose](): void;
|
||||
/**
|
||||
* Get delta norm for operator
|
||||
*/
|
||||
delta_norm(op_type: number): number;
|
||||
/**
|
||||
* Get operator scope name
|
||||
*/
|
||||
static scope_name(op_type: number): string;
|
||||
/**
|
||||
* Adapt with typed array
|
||||
*/
|
||||
adapt_array(op_type: number, gradient: Float32Array): void;
|
||||
/**
|
||||
* Get adapt count for operator
|
||||
*/
|
||||
adapt_count(op_type: number): bigint;
|
||||
/**
|
||||
* Reset specific operator adapter
|
||||
*/
|
||||
reset_scope(op_type: number): void;
|
||||
/**
|
||||
* Forward pass with typed array
|
||||
*/
|
||||
forward_array(op_type: number, input: Float32Array): Float32Array;
|
||||
/**
|
||||
* Get forward count for operator
|
||||
*/
|
||||
forward_count(op_type: number): bigint;
|
||||
/**
|
||||
* Get input buffer pointer
|
||||
*/
|
||||
get_input_ptr(): number;
|
||||
/**
|
||||
* Get output buffer pointer
|
||||
*/
|
||||
get_output_ptr(): number;
|
||||
/**
|
||||
* Adapt with improvement reward
|
||||
*/
|
||||
adapt_with_reward(op_type: number, improvement: number): void;
|
||||
/**
|
||||
* Get total adapt count
|
||||
*/
|
||||
total_adapt_count(): bigint;
|
||||
/**
|
||||
* Get total forward count
|
||||
*/
|
||||
total_forward_count(): bigint;
|
||||
/**
|
||||
* Enable/disable category fallback
|
||||
*/
|
||||
set_category_fallback(enabled: boolean): void;
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
constructor(dim?: number | null, alpha?: number | null, learning_rate?: number | null);
|
||||
/**
|
||||
* Adapt for operator type using input buffer as gradient
|
||||
*/
|
||||
adapt(op_type: number): void;
|
||||
/**
|
||||
* Forward pass for operator type (uses internal buffers)
|
||||
*
|
||||
* @param op_type - Operator type (0-16)
|
||||
*/
|
||||
forward(op_type: number): void;
|
||||
/**
|
||||
* Reset all adapters
|
||||
*/
|
||||
reset_all(): void;
|
||||
}
|
||||
|
||||
export class WasmTrajectoryBuffer {
|
||||
free(): void;
|
||||
[Symbol.dispose](): void;
|
||||
/**
|
||||
* Get total count
|
||||
*/
|
||||
total_count(): bigint;
|
||||
/**
|
||||
* Get success rate
|
||||
*/
|
||||
success_rate(): number;
|
||||
/**
|
||||
* Get best attention type
|
||||
*/
|
||||
best_attention(): number;
|
||||
/**
|
||||
* Get best improvement
|
||||
*/
|
||||
best_improvement(): number;
|
||||
/**
|
||||
* Get mean improvement
|
||||
*/
|
||||
mean_improvement(): number;
|
||||
/**
|
||||
* Get trajectory count for operator
|
||||
*/
|
||||
count_by_operator(op_type: number): number;
|
||||
/**
|
||||
* Get high quality trajectory count
|
||||
*/
|
||||
high_quality_count(threshold: number): number;
|
||||
/**
|
||||
* Get buffer length
|
||||
*/
|
||||
len(): number;
|
||||
/**
|
||||
* Create a new trajectory buffer
|
||||
*
|
||||
* @param capacity - Maximum number of trajectories to store
|
||||
* @param embedding_dim - Dimension of embeddings (default 256)
|
||||
*/
|
||||
constructor(capacity?: number | null, embedding_dim?: number | null);
|
||||
/**
|
||||
* Reset buffer
|
||||
*/
|
||||
reset(): void;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
record(embedding: Float32Array, op_type: number, attention_type: number, execution_ms: number, baseline_ms: number): void;
|
||||
/**
|
||||
* Check if empty
|
||||
*/
|
||||
is_empty(): boolean;
|
||||
/**
|
||||
* Get variance
|
||||
*/
|
||||
variance(): number;
|
||||
}
|
||||
|
||||
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
||||
|
||||
export interface InitOutput {
|
||||
readonly memory: WebAssembly.Memory;
|
||||
readonly __wbg_wasmmicrolora_free: (a: number, b: number) => void;
|
||||
readonly __wbg_wasmscopedlora_free: (a: number, b: number) => void;
|
||||
readonly __wbg_wasmtrajectorybuffer_free: (a: number, b: number) => void;
|
||||
readonly wasmmicrolora_adapt: (a: number) => void;
|
||||
readonly wasmmicrolora_adapt_array: (a: number, b: number, c: number) => void;
|
||||
readonly wasmmicrolora_adapt_count: (a: number) => bigint;
|
||||
readonly wasmmicrolora_adapt_with_reward: (a: number, b: number) => void;
|
||||
readonly wasmmicrolora_delta_norm: (a: number) => number;
|
||||
readonly wasmmicrolora_dim: (a: number) => number;
|
||||
readonly wasmmicrolora_forward: (a: number) => void;
|
||||
readonly wasmmicrolora_forward_array: (a: number, b: number, c: number, d: number) => void;
|
||||
readonly wasmmicrolora_forward_count: (a: number) => bigint;
|
||||
readonly wasmmicrolora_get_input_ptr: (a: number) => number;
|
||||
readonly wasmmicrolora_get_output_ptr: (a: number) => number;
|
||||
readonly wasmmicrolora_new: (a: number, b: number, c: number) => number;
|
||||
readonly wasmmicrolora_param_count: (a: number) => number;
|
||||
readonly wasmmicrolora_reset: (a: number) => void;
|
||||
readonly wasmscopedlora_adapt: (a: number, b: number) => void;
|
||||
readonly wasmscopedlora_adapt_array: (a: number, b: number, c: number, d: number) => void;
|
||||
readonly wasmscopedlora_adapt_count: (a: number, b: number) => bigint;
|
||||
readonly wasmscopedlora_adapt_with_reward: (a: number, b: number, c: number) => void;
|
||||
readonly wasmscopedlora_delta_norm: (a: number, b: number) => number;
|
||||
readonly wasmscopedlora_forward: (a: number, b: number) => void;
|
||||
readonly wasmscopedlora_forward_array: (a: number, b: number, c: number, d: number, e: number) => void;
|
||||
readonly wasmscopedlora_forward_count: (a: number, b: number) => bigint;
|
||||
readonly wasmscopedlora_get_input_ptr: (a: number) => number;
|
||||
readonly wasmscopedlora_get_output_ptr: (a: number) => number;
|
||||
readonly wasmscopedlora_new: (a: number, b: number, c: number) => number;
|
||||
readonly wasmscopedlora_reset_all: (a: number) => void;
|
||||
readonly wasmscopedlora_reset_scope: (a: number, b: number) => void;
|
||||
readonly wasmscopedlora_scope_name: (a: number, b: number) => void;
|
||||
readonly wasmscopedlora_set_category_fallback: (a: number, b: number) => void;
|
||||
readonly wasmscopedlora_total_adapt_count: (a: number) => bigint;
|
||||
readonly wasmscopedlora_total_forward_count: (a: number) => bigint;
|
||||
readonly wasmtrajectorybuffer_best_attention: (a: number) => number;
|
||||
readonly wasmtrajectorybuffer_best_improvement: (a: number) => number;
|
||||
readonly wasmtrajectorybuffer_count_by_operator: (a: number, b: number) => number;
|
||||
readonly wasmtrajectorybuffer_high_quality_count: (a: number, b: number) => number;
|
||||
readonly wasmtrajectorybuffer_is_empty: (a: number) => number;
|
||||
readonly wasmtrajectorybuffer_len: (a: number) => number;
|
||||
readonly wasmtrajectorybuffer_mean_improvement: (a: number) => number;
|
||||
readonly wasmtrajectorybuffer_new: (a: number, b: number) => number;
|
||||
readonly wasmtrajectorybuffer_record: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
|
||||
readonly wasmtrajectorybuffer_reset: (a: number) => void;
|
||||
readonly wasmtrajectorybuffer_success_rate: (a: number) => number;
|
||||
readonly wasmtrajectorybuffer_total_count: (a: number) => bigint;
|
||||
readonly wasmtrajectorybuffer_variance: (a: number) => number;
|
||||
readonly __wbindgen_export: (a: number, b: number) => number;
|
||||
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
|
||||
readonly __wbindgen_export2: (a: number, b: number, c: number) => void;
|
||||
}
|
||||
|
||||
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
||||
|
||||
/**
|
||||
* Instantiates the given `module`, which can either be bytes or
|
||||
* a precompiled `WebAssembly.Module`.
|
||||
*
|
||||
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
||||
*
|
||||
* @returns {InitOutput}
|
||||
*/
|
||||
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
||||
|
||||
/**
|
||||
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
||||
* for everything else, calls `WebAssembly.instantiate` directly.
|
||||
*
|
||||
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
||||
*
|
||||
* @returns {Promise<InitOutput>}
|
||||
*/
|
||||
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
||||
648
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm.js
vendored
Normal file
648
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm.js
vendored
Normal file
@@ -0,0 +1,648 @@
|
||||
let wasm;
|
||||
|
||||
function getArrayF32FromWasm0(ptr, len) {
|
||||
ptr = ptr >>> 0;
|
||||
return getFloat32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
|
||||
}
|
||||
|
||||
let cachedDataViewMemory0 = null;
|
||||
function getDataViewMemory0() {
|
||||
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
||||
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
||||
}
|
||||
return cachedDataViewMemory0;
|
||||
}
|
||||
|
||||
let cachedFloat32ArrayMemory0 = null;
|
||||
function getFloat32ArrayMemory0() {
|
||||
if (cachedFloat32ArrayMemory0 === null || cachedFloat32ArrayMemory0.byteLength === 0) {
|
||||
cachedFloat32ArrayMemory0 = new Float32Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachedFloat32ArrayMemory0;
|
||||
}
|
||||
|
||||
function getStringFromWasm0(ptr, len) {
|
||||
ptr = ptr >>> 0;
|
||||
return decodeText(ptr, len);
|
||||
}
|
||||
|
||||
let cachedUint8ArrayMemory0 = null;
|
||||
function getUint8ArrayMemory0() {
|
||||
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
||||
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachedUint8ArrayMemory0;
|
||||
}
|
||||
|
||||
function isLikeNone(x) {
|
||||
return x === undefined || x === null;
|
||||
}
|
||||
|
||||
function passArrayF32ToWasm0(arg, malloc) {
|
||||
const ptr = malloc(arg.length * 4, 4) >>> 0;
|
||||
getFloat32ArrayMemory0().set(arg, ptr / 4);
|
||||
WASM_VECTOR_LEN = arg.length;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
||||
cachedTextDecoder.decode();
|
||||
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
||||
let numBytesDecoded = 0;
|
||||
function decodeText(ptr, len) {
|
||||
numBytesDecoded += len;
|
||||
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
||||
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
||||
cachedTextDecoder.decode();
|
||||
numBytesDecoded = len;
|
||||
}
|
||||
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
||||
}
|
||||
|
||||
let WASM_VECTOR_LEN = 0;
|
||||
|
||||
const WasmMicroLoRAFinalization = (typeof FinalizationRegistry === 'undefined')
|
||||
? { register: () => {}, unregister: () => {} }
|
||||
: new FinalizationRegistry(ptr => wasm.__wbg_wasmmicrolora_free(ptr >>> 0, 1));
|
||||
|
||||
const WasmScopedLoRAFinalization = (typeof FinalizationRegistry === 'undefined')
|
||||
? { register: () => {}, unregister: () => {} }
|
||||
: new FinalizationRegistry(ptr => wasm.__wbg_wasmscopedlora_free(ptr >>> 0, 1));
|
||||
|
||||
const WasmTrajectoryBufferFinalization = (typeof FinalizationRegistry === 'undefined')
|
||||
? { register: () => {}, unregister: () => {} }
|
||||
: new FinalizationRegistry(ptr => wasm.__wbg_wasmtrajectorybuffer_free(ptr >>> 0, 1));
|
||||
|
||||
/**
|
||||
* WASM-exposed MicroLoRA engine
|
||||
*/
|
||||
export class WasmMicroLoRA {
|
||||
__destroy_into_raw() {
|
||||
const ptr = this.__wbg_ptr;
|
||||
this.__wbg_ptr = 0;
|
||||
WasmMicroLoRAFinalization.unregister(this);
|
||||
return ptr;
|
||||
}
|
||||
free() {
|
||||
const ptr = this.__destroy_into_raw();
|
||||
wasm.__wbg_wasmmicrolora_free(ptr, 0);
|
||||
}
|
||||
/**
|
||||
* Get delta norm (weight change magnitude)
|
||||
* @returns {number}
|
||||
*/
|
||||
delta_norm() {
|
||||
const ret = wasm.wasmmicrolora_delta_norm(this.__wbg_ptr);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Adapt with typed array gradient
|
||||
* @param {Float32Array} gradient
|
||||
*/
|
||||
adapt_array(gradient) {
|
||||
const ptr0 = passArrayF32ToWasm0(gradient, wasm.__wbindgen_export);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
wasm.wasmmicrolora_adapt_array(this.__wbg_ptr, ptr0, len0);
|
||||
}
|
||||
/**
|
||||
* Get adaptation count
|
||||
* @returns {bigint}
|
||||
*/
|
||||
adapt_count() {
|
||||
const ret = wasm.wasmmicrolora_adapt_count(this.__wbg_ptr);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Get parameter count
|
||||
* @returns {number}
|
||||
*/
|
||||
param_count() {
|
||||
const ret = wasm.wasmmicrolora_param_count(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Forward pass with typed array input (allocates output)
|
||||
* @param {Float32Array} input
|
||||
* @returns {Float32Array}
|
||||
*/
|
||||
forward_array(input) {
|
||||
try {
|
||||
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
||||
const ptr0 = passArrayF32ToWasm0(input, wasm.__wbindgen_export);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
wasm.wasmmicrolora_forward_array(retptr, this.__wbg_ptr, ptr0, len0);
|
||||
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
||||
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
||||
var v2 = getArrayF32FromWasm0(r0, r1).slice();
|
||||
wasm.__wbindgen_export2(r0, r1 * 4, 4);
|
||||
return v2;
|
||||
} finally {
|
||||
wasm.__wbindgen_add_to_stack_pointer(16);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Get forward pass count
|
||||
* @returns {bigint}
|
||||
*/
|
||||
forward_count() {
|
||||
const ret = wasm.wasmmicrolora_forward_count(this.__wbg_ptr);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Get pointer to input buffer for direct memory access
|
||||
* @returns {number}
|
||||
*/
|
||||
get_input_ptr() {
|
||||
const ret = wasm.wasmmicrolora_get_input_ptr(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Get pointer to output buffer for direct memory access
|
||||
* @returns {number}
|
||||
*/
|
||||
get_output_ptr() {
|
||||
const ret = wasm.wasmmicrolora_get_output_ptr(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Adapt with improvement reward using input buffer as gradient
|
||||
* @param {number} improvement
|
||||
*/
|
||||
adapt_with_reward(improvement) {
|
||||
wasm.wasmmicrolora_adapt_with_reward(this.__wbg_ptr, improvement);
|
||||
}
|
||||
/**
|
||||
* Get embedding dimension
|
||||
* @returns {number}
|
||||
*/
|
||||
dim() {
|
||||
const ret = wasm.wasmmicrolora_dim(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* 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)
|
||||
* @param {number | null} [dim]
|
||||
* @param {number | null} [alpha]
|
||||
* @param {number | null} [learning_rate]
|
||||
*/
|
||||
constructor(dim, alpha, learning_rate) {
|
||||
const ret = wasm.wasmmicrolora_new(isLikeNone(dim) ? 0x100000001 : (dim) >>> 0, isLikeNone(alpha) ? 0x100000001 : Math.fround(alpha), isLikeNone(learning_rate) ? 0x100000001 : Math.fround(learning_rate));
|
||||
this.__wbg_ptr = ret >>> 0;
|
||||
WasmMicroLoRAFinalization.register(this, this.__wbg_ptr, this);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Adapt using input buffer as gradient
|
||||
*/
|
||||
adapt() {
|
||||
wasm.wasmmicrolora_adapt(this.__wbg_ptr);
|
||||
}
|
||||
/**
|
||||
* Reset the engine
|
||||
*/
|
||||
reset() {
|
||||
wasm.wasmmicrolora_reset(this.__wbg_ptr);
|
||||
}
|
||||
/**
|
||||
* Forward pass using internal buffers (zero-allocation)
|
||||
*
|
||||
* Write input to get_input_ptr(), call forward(), read from get_output_ptr()
|
||||
*/
|
||||
forward() {
|
||||
wasm.wasmmicrolora_forward(this.__wbg_ptr);
|
||||
}
|
||||
}
|
||||
if (Symbol.dispose) WasmMicroLoRA.prototype[Symbol.dispose] = WasmMicroLoRA.prototype.free;
|
||||
|
||||
/**
|
||||
* WASM-exposed Scoped LoRA manager
|
||||
*/
|
||||
export class WasmScopedLoRA {
|
||||
__destroy_into_raw() {
|
||||
const ptr = this.__wbg_ptr;
|
||||
this.__wbg_ptr = 0;
|
||||
WasmScopedLoRAFinalization.unregister(this);
|
||||
return ptr;
|
||||
}
|
||||
free() {
|
||||
const ptr = this.__destroy_into_raw();
|
||||
wasm.__wbg_wasmscopedlora_free(ptr, 0);
|
||||
}
|
||||
/**
|
||||
* Get delta norm for operator
|
||||
* @param {number} op_type
|
||||
* @returns {number}
|
||||
*/
|
||||
delta_norm(op_type) {
|
||||
const ret = wasm.wasmscopedlora_delta_norm(this.__wbg_ptr, op_type);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Get operator scope name
|
||||
* @param {number} op_type
|
||||
* @returns {string}
|
||||
*/
|
||||
static scope_name(op_type) {
|
||||
let deferred1_0;
|
||||
let deferred1_1;
|
||||
try {
|
||||
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
||||
wasm.wasmscopedlora_scope_name(retptr, op_type);
|
||||
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
||||
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
||||
deferred1_0 = r0;
|
||||
deferred1_1 = r1;
|
||||
return getStringFromWasm0(r0, r1);
|
||||
} finally {
|
||||
wasm.__wbindgen_add_to_stack_pointer(16);
|
||||
wasm.__wbindgen_export2(deferred1_0, deferred1_1, 1);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adapt with typed array
|
||||
* @param {number} op_type
|
||||
* @param {Float32Array} gradient
|
||||
*/
|
||||
adapt_array(op_type, gradient) {
|
||||
const ptr0 = passArrayF32ToWasm0(gradient, wasm.__wbindgen_export);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
wasm.wasmscopedlora_adapt_array(this.__wbg_ptr, op_type, ptr0, len0);
|
||||
}
|
||||
/**
|
||||
* Get adapt count for operator
|
||||
* @param {number} op_type
|
||||
* @returns {bigint}
|
||||
*/
|
||||
adapt_count(op_type) {
|
||||
const ret = wasm.wasmscopedlora_adapt_count(this.__wbg_ptr, op_type);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Reset specific operator adapter
|
||||
* @param {number} op_type
|
||||
*/
|
||||
reset_scope(op_type) {
|
||||
wasm.wasmscopedlora_reset_scope(this.__wbg_ptr, op_type);
|
||||
}
|
||||
/**
|
||||
* Forward pass with typed array
|
||||
* @param {number} op_type
|
||||
* @param {Float32Array} input
|
||||
* @returns {Float32Array}
|
||||
*/
|
||||
forward_array(op_type, input) {
|
||||
try {
|
||||
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
||||
const ptr0 = passArrayF32ToWasm0(input, wasm.__wbindgen_export);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
wasm.wasmscopedlora_forward_array(retptr, this.__wbg_ptr, op_type, ptr0, len0);
|
||||
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
||||
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
||||
var v2 = getArrayF32FromWasm0(r0, r1).slice();
|
||||
wasm.__wbindgen_export2(r0, r1 * 4, 4);
|
||||
return v2;
|
||||
} finally {
|
||||
wasm.__wbindgen_add_to_stack_pointer(16);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Get forward count for operator
|
||||
* @param {number} op_type
|
||||
* @returns {bigint}
|
||||
*/
|
||||
forward_count(op_type) {
|
||||
const ret = wasm.wasmscopedlora_forward_count(this.__wbg_ptr, op_type);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Get input buffer pointer
|
||||
* @returns {number}
|
||||
*/
|
||||
get_input_ptr() {
|
||||
const ret = wasm.wasmscopedlora_get_input_ptr(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Get output buffer pointer
|
||||
* @returns {number}
|
||||
*/
|
||||
get_output_ptr() {
|
||||
const ret = wasm.wasmscopedlora_get_output_ptr(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Adapt with improvement reward
|
||||
* @param {number} op_type
|
||||
* @param {number} improvement
|
||||
*/
|
||||
adapt_with_reward(op_type, improvement) {
|
||||
wasm.wasmscopedlora_adapt_with_reward(this.__wbg_ptr, op_type, improvement);
|
||||
}
|
||||
/**
|
||||
* Get total adapt count
|
||||
* @returns {bigint}
|
||||
*/
|
||||
total_adapt_count() {
|
||||
const ret = wasm.wasmscopedlora_total_adapt_count(this.__wbg_ptr);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Get total forward count
|
||||
* @returns {bigint}
|
||||
*/
|
||||
total_forward_count() {
|
||||
const ret = wasm.wasmscopedlora_total_forward_count(this.__wbg_ptr);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Enable/disable category fallback
|
||||
* @param {boolean} enabled
|
||||
*/
|
||||
set_category_fallback(enabled) {
|
||||
wasm.wasmscopedlora_set_category_fallback(this.__wbg_ptr, enabled);
|
||||
}
|
||||
/**
|
||||
* 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)
|
||||
* @param {number | null} [dim]
|
||||
* @param {number | null} [alpha]
|
||||
* @param {number | null} [learning_rate]
|
||||
*/
|
||||
constructor(dim, alpha, learning_rate) {
|
||||
const ret = wasm.wasmscopedlora_new(isLikeNone(dim) ? 0x100000001 : (dim) >>> 0, isLikeNone(alpha) ? 0x100000001 : Math.fround(alpha), isLikeNone(learning_rate) ? 0x100000001 : Math.fround(learning_rate));
|
||||
this.__wbg_ptr = ret >>> 0;
|
||||
WasmScopedLoRAFinalization.register(this, this.__wbg_ptr, this);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Adapt for operator type using input buffer as gradient
|
||||
* @param {number} op_type
|
||||
*/
|
||||
adapt(op_type) {
|
||||
wasm.wasmscopedlora_adapt(this.__wbg_ptr, op_type);
|
||||
}
|
||||
/**
|
||||
* Forward pass for operator type (uses internal buffers)
|
||||
*
|
||||
* @param op_type - Operator type (0-16)
|
||||
* @param {number} op_type
|
||||
*/
|
||||
forward(op_type) {
|
||||
wasm.wasmscopedlora_forward(this.__wbg_ptr, op_type);
|
||||
}
|
||||
/**
|
||||
* Reset all adapters
|
||||
*/
|
||||
reset_all() {
|
||||
wasm.wasmscopedlora_reset_all(this.__wbg_ptr);
|
||||
}
|
||||
}
|
||||
if (Symbol.dispose) WasmScopedLoRA.prototype[Symbol.dispose] = WasmScopedLoRA.prototype.free;
|
||||
|
||||
/**
|
||||
* WASM-exposed trajectory buffer
|
||||
*/
|
||||
export class WasmTrajectoryBuffer {
|
||||
__destroy_into_raw() {
|
||||
const ptr = this.__wbg_ptr;
|
||||
this.__wbg_ptr = 0;
|
||||
WasmTrajectoryBufferFinalization.unregister(this);
|
||||
return ptr;
|
||||
}
|
||||
free() {
|
||||
const ptr = this.__destroy_into_raw();
|
||||
wasm.__wbg_wasmtrajectorybuffer_free(ptr, 0);
|
||||
}
|
||||
/**
|
||||
* Get total count
|
||||
* @returns {bigint}
|
||||
*/
|
||||
total_count() {
|
||||
const ret = wasm.wasmtrajectorybuffer_total_count(this.__wbg_ptr);
|
||||
return BigInt.asUintN(64, ret);
|
||||
}
|
||||
/**
|
||||
* Get success rate
|
||||
* @returns {number}
|
||||
*/
|
||||
success_rate() {
|
||||
const ret = wasm.wasmtrajectorybuffer_success_rate(this.__wbg_ptr);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Get best attention type
|
||||
* @returns {number}
|
||||
*/
|
||||
best_attention() {
|
||||
const ret = wasm.wasmtrajectorybuffer_best_attention(this.__wbg_ptr);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Get best improvement
|
||||
* @returns {number}
|
||||
*/
|
||||
best_improvement() {
|
||||
const ret = wasm.wasmtrajectorybuffer_best_improvement(this.__wbg_ptr);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Get mean improvement
|
||||
* @returns {number}
|
||||
*/
|
||||
mean_improvement() {
|
||||
const ret = wasm.wasmtrajectorybuffer_mean_improvement(this.__wbg_ptr);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Get trajectory count for operator
|
||||
* @param {number} op_type
|
||||
* @returns {number}
|
||||
*/
|
||||
count_by_operator(op_type) {
|
||||
const ret = wasm.wasmtrajectorybuffer_count_by_operator(this.__wbg_ptr, op_type);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Get high quality trajectory count
|
||||
* @param {number} threshold
|
||||
* @returns {number}
|
||||
*/
|
||||
high_quality_count(threshold) {
|
||||
const ret = wasm.wasmtrajectorybuffer_high_quality_count(this.__wbg_ptr, threshold);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Get buffer length
|
||||
* @returns {number}
|
||||
*/
|
||||
len() {
|
||||
const ret = wasm.wasmtrajectorybuffer_len(this.__wbg_ptr);
|
||||
return ret >>> 0;
|
||||
}
|
||||
/**
|
||||
* Create a new trajectory buffer
|
||||
*
|
||||
* @param capacity - Maximum number of trajectories to store
|
||||
* @param embedding_dim - Dimension of embeddings (default 256)
|
||||
* @param {number | null} [capacity]
|
||||
* @param {number | null} [embedding_dim]
|
||||
*/
|
||||
constructor(capacity, embedding_dim) {
|
||||
const ret = wasm.wasmtrajectorybuffer_new(isLikeNone(capacity) ? 0x100000001 : (capacity) >>> 0, isLikeNone(embedding_dim) ? 0x100000001 : (embedding_dim) >>> 0);
|
||||
this.__wbg_ptr = ret >>> 0;
|
||||
WasmTrajectoryBufferFinalization.register(this, this.__wbg_ptr, this);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Reset buffer
|
||||
*/
|
||||
reset() {
|
||||
wasm.wasmtrajectorybuffer_reset(this.__wbg_ptr);
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
* @param {Float32Array} embedding
|
||||
* @param {number} op_type
|
||||
* @param {number} attention_type
|
||||
* @param {number} execution_ms
|
||||
* @param {number} baseline_ms
|
||||
*/
|
||||
record(embedding, op_type, attention_type, execution_ms, baseline_ms) {
|
||||
const ptr0 = passArrayF32ToWasm0(embedding, wasm.__wbindgen_export);
|
||||
const len0 = WASM_VECTOR_LEN;
|
||||
wasm.wasmtrajectorybuffer_record(this.__wbg_ptr, ptr0, len0, op_type, attention_type, execution_ms, baseline_ms);
|
||||
}
|
||||
/**
|
||||
* Check if empty
|
||||
* @returns {boolean}
|
||||
*/
|
||||
is_empty() {
|
||||
const ret = wasm.wasmtrajectorybuffer_is_empty(this.__wbg_ptr);
|
||||
return ret !== 0;
|
||||
}
|
||||
/**
|
||||
* Get variance
|
||||
* @returns {number}
|
||||
*/
|
||||
variance() {
|
||||
const ret = wasm.wasmtrajectorybuffer_variance(this.__wbg_ptr);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (Symbol.dispose) WasmTrajectoryBuffer.prototype[Symbol.dispose] = WasmTrajectoryBuffer.prototype.free;
|
||||
|
||||
const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
|
||||
|
||||
async function __wbg_load(module, imports) {
|
||||
if (typeof Response === 'function' && module instanceof Response) {
|
||||
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
||||
try {
|
||||
return await WebAssembly.instantiateStreaming(module, imports);
|
||||
} catch (e) {
|
||||
const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
|
||||
|
||||
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
||||
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
||||
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const bytes = await module.arrayBuffer();
|
||||
return await WebAssembly.instantiate(bytes, imports);
|
||||
} else {
|
||||
const instance = await WebAssembly.instantiate(module, imports);
|
||||
|
||||
if (instance instanceof WebAssembly.Instance) {
|
||||
return { instance, module };
|
||||
} else {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function __wbg_get_imports() {
|
||||
const imports = {};
|
||||
imports.wbg = {};
|
||||
imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) {
|
||||
throw new Error(getStringFromWasm0(arg0, arg1));
|
||||
};
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
function __wbg_finalize_init(instance, module) {
|
||||
wasm = instance.exports;
|
||||
__wbg_init.__wbindgen_wasm_module = module;
|
||||
cachedDataViewMemory0 = null;
|
||||
cachedFloat32ArrayMemory0 = null;
|
||||
cachedUint8ArrayMemory0 = null;
|
||||
|
||||
|
||||
|
||||
return wasm;
|
||||
}
|
||||
|
||||
function initSync(module) {
|
||||
if (wasm !== undefined) return wasm;
|
||||
|
||||
|
||||
if (typeof module !== 'undefined') {
|
||||
if (Object.getPrototypeOf(module) === Object.prototype) {
|
||||
({module} = module)
|
||||
} else {
|
||||
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
||||
}
|
||||
}
|
||||
|
||||
const imports = __wbg_get_imports();
|
||||
if (!(module instanceof WebAssembly.Module)) {
|
||||
module = new WebAssembly.Module(module);
|
||||
}
|
||||
const instance = new WebAssembly.Instance(module, imports);
|
||||
return __wbg_finalize_init(instance, module);
|
||||
}
|
||||
|
||||
async function __wbg_init(module_or_path) {
|
||||
if (wasm !== undefined) return wasm;
|
||||
|
||||
|
||||
if (typeof module_or_path !== 'undefined') {
|
||||
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
||||
({module_or_path} = module_or_path)
|
||||
} else {
|
||||
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof module_or_path === 'undefined') {
|
||||
module_or_path = new URL('ruvector_learning_wasm_bg.wasm', import.meta.url);
|
||||
}
|
||||
const imports = __wbg_get_imports();
|
||||
|
||||
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
||||
module_or_path = fetch(module_or_path);
|
||||
}
|
||||
|
||||
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
||||
|
||||
return __wbg_finalize_init(instance, module);
|
||||
}
|
||||
|
||||
export { initSync };
|
||||
export default __wbg_init;
|
||||
BIN
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm_bg.wasm
vendored
Normal file
BIN
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm_bg.wasm
vendored
Normal file
Binary file not shown.
53
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm_bg.wasm.d.ts
vendored
Normal file
53
vendor/ruvector/crates/ruvector-learning-wasm/pkg/ruvector_learning_wasm_bg.wasm.d.ts
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
export const memory: WebAssembly.Memory;
|
||||
export const __wbg_wasmmicrolora_free: (a: number, b: number) => void;
|
||||
export const __wbg_wasmscopedlora_free: (a: number, b: number) => void;
|
||||
export const __wbg_wasmtrajectorybuffer_free: (a: number, b: number) => void;
|
||||
export const wasmmicrolora_adapt: (a: number) => void;
|
||||
export const wasmmicrolora_adapt_array: (a: number, b: number, c: number) => void;
|
||||
export const wasmmicrolora_adapt_count: (a: number) => bigint;
|
||||
export const wasmmicrolora_adapt_with_reward: (a: number, b: number) => void;
|
||||
export const wasmmicrolora_delta_norm: (a: number) => number;
|
||||
export const wasmmicrolora_dim: (a: number) => number;
|
||||
export const wasmmicrolora_forward: (a: number) => void;
|
||||
export const wasmmicrolora_forward_array: (a: number, b: number, c: number, d: number) => void;
|
||||
export const wasmmicrolora_forward_count: (a: number) => bigint;
|
||||
export const wasmmicrolora_get_input_ptr: (a: number) => number;
|
||||
export const wasmmicrolora_get_output_ptr: (a: number) => number;
|
||||
export const wasmmicrolora_new: (a: number, b: number, c: number) => number;
|
||||
export const wasmmicrolora_param_count: (a: number) => number;
|
||||
export const wasmmicrolora_reset: (a: number) => void;
|
||||
export const wasmscopedlora_adapt: (a: number, b: number) => void;
|
||||
export const wasmscopedlora_adapt_array: (a: number, b: number, c: number, d: number) => void;
|
||||
export const wasmscopedlora_adapt_count: (a: number, b: number) => bigint;
|
||||
export const wasmscopedlora_adapt_with_reward: (a: number, b: number, c: number) => void;
|
||||
export const wasmscopedlora_delta_norm: (a: number, b: number) => number;
|
||||
export const wasmscopedlora_forward: (a: number, b: number) => void;
|
||||
export const wasmscopedlora_forward_array: (a: number, b: number, c: number, d: number, e: number) => void;
|
||||
export const wasmscopedlora_forward_count: (a: number, b: number) => bigint;
|
||||
export const wasmscopedlora_get_input_ptr: (a: number) => number;
|
||||
export const wasmscopedlora_get_output_ptr: (a: number) => number;
|
||||
export const wasmscopedlora_new: (a: number, b: number, c: number) => number;
|
||||
export const wasmscopedlora_reset_all: (a: number) => void;
|
||||
export const wasmscopedlora_reset_scope: (a: number, b: number) => void;
|
||||
export const wasmscopedlora_scope_name: (a: number, b: number) => void;
|
||||
export const wasmscopedlora_set_category_fallback: (a: number, b: number) => void;
|
||||
export const wasmscopedlora_total_adapt_count: (a: number) => bigint;
|
||||
export const wasmscopedlora_total_forward_count: (a: number) => bigint;
|
||||
export const wasmtrajectorybuffer_best_attention: (a: number) => number;
|
||||
export const wasmtrajectorybuffer_best_improvement: (a: number) => number;
|
||||
export const wasmtrajectorybuffer_count_by_operator: (a: number, b: number) => number;
|
||||
export const wasmtrajectorybuffer_high_quality_count: (a: number, b: number) => number;
|
||||
export const wasmtrajectorybuffer_is_empty: (a: number) => number;
|
||||
export const wasmtrajectorybuffer_len: (a: number) => number;
|
||||
export const wasmtrajectorybuffer_mean_improvement: (a: number) => number;
|
||||
export const wasmtrajectorybuffer_new: (a: number, b: number) => number;
|
||||
export const wasmtrajectorybuffer_record: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
|
||||
export const wasmtrajectorybuffer_reset: (a: number) => void;
|
||||
export const wasmtrajectorybuffer_success_rate: (a: number) => number;
|
||||
export const wasmtrajectorybuffer_total_count: (a: number) => bigint;
|
||||
export const wasmtrajectorybuffer_variance: (a: number) => number;
|
||||
export const __wbindgen_export: (a: number, b: number) => number;
|
||||
export const __wbindgen_add_to_stack_pointer: (a: number) => number;
|
||||
export const __wbindgen_export2: (a: number, b: number, c: number) => void;
|
||||
46
vendor/ruvector/crates/ruvector-learning-wasm/src/lib.rs
vendored
Normal file
46
vendor/ruvector/crates/ruvector-learning-wasm/src/lib.rs
vendored
Normal 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::*;
|
||||
559
vendor/ruvector/crates/ruvector-learning-wasm/src/lora.rs
vendored
Normal file
559
vendor/ruvector/crates/ruvector-learning-wasm/src/lora.rs
vendored
Normal 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);
|
||||
}
|
||||
}
|
||||
523
vendor/ruvector/crates/ruvector-learning-wasm/src/operator_scope.rs
vendored
Normal file
523
vendor/ruvector/crates/ruvector-learning-wasm/src/operator_scope.rs
vendored
Normal 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());
|
||||
}
|
||||
}
|
||||
428
vendor/ruvector/crates/ruvector-learning-wasm/src/trajectory.rs
vendored
Normal file
428
vendor/ruvector/crates/ruvector-learning-wasm/src/trajectory.rs
vendored
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user