Skip to main content

LoraAdapter

Struct LoraAdapter 

Source
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: usize

LoRA rank.

§in_dim: usize

Input dimension.

§out_dim: usize

Output dimension.

§a: Vec<f32>

Down-projection: [rank × in_dim]

§b: Vec<f32>

Up-projection: [out_dim × rank]

§alpha: f32

Scaling factor (alpha / rank).

Implementations§

Source§

impl LoraAdapter

Source

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.

Source

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.

Source

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.

Source

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 it

Alpha defaults to rank * 2.

Trait Implementations§

Source§

impl Clone for LoraAdapter

Source§

fn clone(&self) -> LoraAdapter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.