use wasm_bindgen::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct LoRAConfig {
pub dim: usize,
pub rank: usize,
pub alpha: f32,
pub learning_rate: f32,
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,
}
}
}
#[derive(Clone)]
pub struct LoRAPair {
a: Vec<[f32; 2]>,
b: [[f32; 256]; 2],
alpha: f32,
lr: f32,
dim: usize,
adapt_count: u64,
}
impl LoRAPair {
pub fn new(config: &LoRAConfig) -> Self {
let dim = config.dim.min(256); let rank = config.rank.min(2);
let mut a = Vec::with_capacity(dim);
let scale = (2.0 / dim as f32).sqrt() * 0.1;
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]);
}
let b = [[0.0f32; 256]; 2];
Self {
a,
b,
alpha: config.alpha,
lr: config.learning_rate,
dim,
adapt_count: 0,
}
}
#[inline]
pub fn forward(&self, input: &[f32]) -> Vec<f32> {
let n = input.len().min(self.dim);
let mut output = input.to_vec();
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];
}
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
}
#[inline]
pub fn forward_into(&self, input: &[f32], output: &mut [f32]) {
let n = input.len().min(self.dim).min(output.len());
output[..n].copy_from_slice(&input[..n]);
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];
}
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;
}
}
#[inline]
pub fn adapt(&mut self, gradient: &[f32]) {
let n = gradient.len().min(self.dim);
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 grad_norm = fast_sqrt(grad_norm_sq);
let inv_norm = 1.0 / grad_norm;
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];
}
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;
}
#[inline]
pub fn adapt_with_reward(&mut self, gradient: &[f32], improvement: f32) {
if improvement <= 0.0 {
return; }
let n = gradient.len().min(self.dim);
let scaled_lr = self.lr * improvement.min(2.0);
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);
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];
}
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;
}
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;
}
pub fn adapt_count(&self) -> u64 {
self.adapt_count
}
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
}
pub fn param_count(&self) -> usize {
self.a.len() * 2 + 256 * 2
}
}
pub struct MicroLoRAEngine {
default_lora: LoRAPair,
#[allow(dead_code)]
config: LoRAConfig,
forward_count: u64,
total_adapt_count: u64,
}
impl MicroLoRAEngine {
pub fn new(config: LoRAConfig) -> Self {
Self {
default_lora: LoRAPair::new(&config),
config,
forward_count: 0,
total_adapt_count: 0,
}
}
#[inline]
pub fn forward(&mut self, input: &[f32]) -> Vec<f32> {
self.forward_count += 1;
self.default_lora.forward(input)
}
#[inline]
pub fn adapt(&mut self, gradient: &[f32]) {
self.default_lora.adapt(gradient);
self.total_adapt_count += 1;
}
#[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;
}
pub fn reset(&mut self) {
self.default_lora.reset();
self.forward_count = 0;
self.total_adapt_count = 0;
}
pub fn stats(&self) -> (u64, u64, f32) {
(
self.forward_count,
self.total_adapt_count,
self.default_lora.delta_norm(),
)
}
pub fn lora(&self) -> &LoRAPair {
&self.default_lora
}
pub fn lora_mut(&mut self) -> &mut LoRAPair {
&mut self.default_lora
}
}
impl Default for MicroLoRAEngine {
fn default() -> Self {
Self::new(LoRAConfig::default())
}
}
#[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)
}
#[inline(always)]
fn pseudo_random(seed: u32) -> f32 {
let mut x = seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
(x as f32) / (u32::MAX as f32)
}
pub mod wasm_exports {
use super::*;
#[allow(unused_imports)]
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct WasmMicroLoRA {
engine: MicroLoRAEngine,
input_buffer: Vec<f32>,
output_buffer: Vec<f32>,
}
#[wasm_bindgen]
impl WasmMicroLoRA {
#[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],
}
}
#[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 dim(&self) -> usize {
self.input_buffer.len()
}
#[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;
}
#[wasm_bindgen]
pub fn forward_array(&mut self, input: &[f32]) -> Vec<f32> {
self.engine.forward(input)
}
#[wasm_bindgen]
pub fn adapt(&mut self) {
self.engine.adapt(&self.input_buffer.clone());
}
#[wasm_bindgen]
pub fn adapt_array(&mut self, gradient: &[f32]) {
self.engine.adapt(gradient);
}
#[wasm_bindgen]
pub fn adapt_with_reward(&mut self, improvement: f32) {
self.engine
.adapt_with_reward(&self.input_buffer.clone(), improvement);
}
#[wasm_bindgen]
pub fn reset(&mut self) {
self.engine.reset();
}
#[wasm_bindgen]
pub fn forward_count(&self) -> u64 {
self.engine.forward_count
}
#[wasm_bindgen]
pub fn adapt_count(&self) -> u64 {
self.engine.total_adapt_count
}
#[wasm_bindgen]
pub fn delta_norm(&self) -> f32 {
self.engine.default_lora.delta_norm()
}
#[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);
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);
let gradient = vec![0.1; 256];
lora.adapt(&gradient);
let input = vec![1.0; 256];
let output = lora.forward(&input);
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);
}
}