use crate::lora::{LoRAConfig, LoRAPair};
use wasm_bindgen::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum OperatorScope {
SeqScan = 0,
IndexScan = 1,
HnswScan = 2,
IvfFlatScan = 3,
NestedLoopJoin = 4,
HashJoin = 5,
MergeJoin = 6,
Aggregate = 7,
GroupBy = 8,
Filter = 9,
Project = 10,
Sort = 11,
Limit = 12,
VectorDistance = 13,
Rerank = 14,
Materialize = 15,
Result = 16,
}
impl OperatorScope {
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,
}
}
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,
}
}
}
#[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,
}
pub struct ScopedLoRA {
adapters: [LoRAPair; 17],
category_adapters: [LoRAPair; 7],
#[allow(dead_code)]
config: LoRAConfig,
use_category_fallback: bool,
forward_counts: [u64; 17],
}
impl ScopedLoRA {
pub fn new(config: LoRAConfig) -> Self {
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],
}
}
#[inline]
pub fn forward(&mut self, scope: OperatorScope, input: &[f32]) -> Vec<f32> {
let idx = scope as usize;
self.forward_counts[idx] += 1;
let output = self.adapters[idx].forward(input);
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);
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
}
#[inline]
pub fn adapt(&mut self, scope: OperatorScope, gradient: &[f32]) {
let idx = scope as usize;
self.adapters[idx].adapt(gradient);
let cat_idx = scope.category() as usize;
self.category_adapters[cat_idx].adapt(gradient);
}
#[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);
let cat_idx = scope.category() as usize;
self.category_adapters[cat_idx].adapt_with_reward(gradient, improvement);
}
pub fn reset_scope(&mut self, scope: OperatorScope) {
let idx = scope as usize;
self.adapters[idx].reset();
self.forward_counts[idx] = 0;
}
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];
}
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(),
)
}
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)
}
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()
}
pub fn set_category_fallback(&mut self, enabled: bool) {
self.use_category_fallback = enabled;
}
pub fn adapter(&self, scope: OperatorScope) -> &LoRAPair {
&self.adapters[scope as usize]
}
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())
}
}
pub mod wasm_exports {
use super::*;
#[allow(unused_imports)]
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct WasmScopedLoRA {
inner: ScopedLoRA,
input_buffer: Vec<f32>,
output_buffer: Vec<f32>,
}
#[wasm_bindgen]
impl WasmScopedLoRA {
#[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],
}
}
#[wasm_bindgen]
pub fn get_input_ptr(&mut self) -> *mut f32 {
self.input_buffer.as_mut_ptr()
}
#[wasm_bindgen]
pub fn get_output_ptr(&self) -> *const f32 {
self.output_buffer.as_ptr()
}
#[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]);
}
}
#[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()
}
}
#[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());
}
}
#[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);
}
}
#[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);
}
}
#[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);
}
}
#[wasm_bindgen]
pub fn reset_all(&mut self) {
self.inner.reset_all();
}
#[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
}
}
#[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
}
}
#[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
}
}
#[wasm_bindgen]
pub fn total_forward_count(&self) -> u64 {
self.inner.total_stats().0
}
#[wasm_bindgen]
pub fn total_adapt_count(&self) -> u64 {
self.inner.total_stats().1
}
#[wasm_bindgen]
pub fn set_category_fallback(&mut self, enabled: bool) {
self.inner.set_category_fallback(enabled);
}
#[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];
lora.adapt(OperatorScope::HnswScan, &gradient);
let input = vec![1.0; 256];
let output = lora.forward(OperatorScope::SeqScan, &input);
let mut diff = 0.0f32;
for i in 0..256 {
diff += (output[i] - input[i]).abs();
}
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());
}
}