Skip to main content

qualia_core_db/lora/
mod.rs

1//! Zero-Copy LoRA Multiplexing — context-driven neural adaptation.
2//!
3//! Maintains a single quantized base model in memory and streams tiny LoRA
4//! (Low-Rank Adaptation) adapters based on context triggers encoded in the
5//! `NQuin` 5th metadata vector.  Context switching takes <10 ms; the
6//! extra memory footprint is ≤15 MB per cached adapter.
7//!
8//! # Architecture
9//!
10//! ```text
11//! Prompt / NQuin
12//!       │
13//!       ▼
14//!  ContextDetector  ──► ContextType (Medical / Legal / Chemical / …)
15//!       │
16//!       ▼
17//!  LoRAAdapterManager (LRU-10 cache)
18//!       ├── adapter_cache: HashMap<ContextType, LoRAAdapter>
19//!       │      LoRAAdapter { lora_a: [rank × n_in], lora_b: [n_out × rank] }
20//!       │
21//!       ▼
22//!  CPU apply: output += B @ (A @ x) * scaling
23//!  GPU apply: lora_apply.wgsl dispatch (additive delta on hidden state)
24//!       │
25//!       ▼
26//!  Modified hidden-state fed into fused_transformer.wgsl
27//! ```
28//!
29//! # NQuin metadata encoding (bits 60–48)
30//!
31//! | Bits  | Field          | Notes                  |
32//! |-------|----------------|------------------------|
33//! | 63–60 | ContextType    | 0=General … 5=Technical |
34//! | 59–56 | AdapterID      | 0–15 (4 bits)          |
35//! | 55–48 | Confidence     | 0–255 → 0.0–1.0        |
36
37pub mod adapter_manager;
38pub mod context_detector;
39
40#[cfg(not(target_arch = "wasm32"))]
41pub mod webgpu_lora;
42
43pub use adapter_manager::{LoRAAdapter, LoRAAdapterManager, LoRAError, LoRAMetadata, LoRATensor};
44pub use context_detector::{ContextDetector, ContextType};