pub struct LoraAdapter {
pub rank: usize,
pub in_dim: usize,
pub out_dim: usize,
pub a: Vec<f32>,
pub b: Vec<f32>,
pub alpha: f32,
}Expand description
CPU-side LoRA adapter for inference.
Loads from the same binary format as GpuLoraAdapter (Plan 008):
[LORA(4) | version(4) | blake3(32) | payload...]
where payload = [n_adapters(4) | rank(4) | alpha(4) | adapter_data...]
and adapter_data = [in_dim(4) | out_dim(4) | a_f32s | b_f32s]
Use LoraAdapter::load to read ALL adapters from a multi-adapter file
(correct for L2+ models), or LoraAdapter::load_first when only the
first adapter is needed (e.g., single-forward-pass heuristic players).
Zero-copy: loaded once per domain, reference-passed during inference.
Fields ordered by descending alignment to minimize padding: usize/Vec (8-byte) → f32 (4-byte).
Fields§
§rank: usizeLoRA rank.
in_dim: usizeInput dimension.
out_dim: usizeOutput dimension.
a: Vec<f32>Down-projection: [rank × in_dim]
b: Vec<f32>Up-projection: [out_dim × rank]
alpha: f32Scaling factor (alpha / rank).
Implementations§
Source§impl LoraAdapter
impl LoraAdapter
Sourcepub fn load(path: &Path) -> Result<Vec<Self>, String>
pub fn load(path: &Path) -> Result<Vec<Self>, String>
Load ALL adapters from a Plan 008 binary LoRA file.
Multi-adapter files (e.g., L2+ with 6 adapters/layer × n_layer) return every adapter in declaration order. Single-adapter files return a 1-element Vec.
Issue 299: previously this returned only the first adapter, silently discarding layers 1+ and invalidating L2+ arena benchmarks.
Sourcepub fn load_first(path: &Path) -> Result<Self, String>
pub fn load_first(path: &Path) -> Result<Self, String>
Load only the first adapter from a Plan 008 binary LoRA file.
Convenience for consumers that store a single LoraAdapter and only run
one forward pass (e.g., LoraPlayer, FullHLPlayer). Multi-adapter
files (L2+) have layers 1+ silently dropped — this is explicit about
that limitation so callers cannot accidentally regress on Issue 299.
For correct multi-adapter evaluation, use load and apply
each adapter to its target projection during the forward pass.
Sourcepub fn save(
adapters: &[&Self],
rank: usize,
alpha: f32,
path: &Path,
) -> Result<(), String>
pub fn save( adapters: &[&Self], rank: usize, alpha: f32, path: &Path, ) -> Result<(), String>
Save adapters to a Plan 008 binary LoRA file (the inverse of load).
All adapters MUST share the same rank and alpha — the file format stores
them once in the header. Per-adapter in_dim/out_dim are stored individually
(they can differ across targets, e.g. Q vs K in GQA).
Format: ["LORA" | version=1(u32) | blake3(payload)(32) | payload] where
payload = [n_adapters(u32) | rank(u32) | alpha(f32) | per-adapter: in_dim(u32) | out_dim(u32) | a_f32s | b_f32s].
This is the CPU-side counterpart to the private GPU LoRA exporter,
producing byte-identical files that load via either path. Used by
CpuLoraTrainer (Issue 018 CPU fallback) to produce arena-loadable
adapters without a GPU.
Sourcepub fn load_from_bin(path: &Path) -> Result<Vec<Self>, String>
pub fn load_from_bin(path: &Path) -> Result<Vec<Self>, String>
Load LoRA adapters from a compact binary format.
Format:
[MAGIC: "LORA" 4B]
[VERSION: 1B]
[RANK: 2B LE]
[N_LAYERS: 2B LE]
[N_TARGETS: 2B LE]
[TARGET_IDS: N_TARGETS × 2B LE] (0=q_proj, 1=k_proj, 2=v_proj, 3=o_proj,
4=gate_proj, 5=up_proj, 6=down_proj)
[LAYER_DATA: for each (layer, target):
[A_ROWS: 2B][A_COLS: 2B][A_DATA: A_ROWS×A_COLS × 4B f32]
[B_ROWS: 2B][B_COLS: 2B][B_DATA: B_ROWS×B_COLS × 4B f32]
]
[BLAKE3_HASH: 32B] — covers everything before itAlpha defaults to rank * 2.
Trait Implementations§
Source§impl Clone for LoraAdapter
impl Clone for LoraAdapter
Source§fn clone(&self) -> LoraAdapter
fn clone(&self) -> LoraAdapter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for LoraAdapter
impl RefUnwindSafe for LoraAdapter
impl Send for LoraAdapter
impl Sync for LoraAdapter
impl Unpin for LoraAdapter
impl UnsafeUnpin for LoraAdapter
impl UnwindSafe for LoraAdapter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more