pub struct LoraLinear { /* private fields */ }Expand description
A single LoRA-adapted linear layer.
Computes: output = W @ x + (alpha/rank) * B @ (A @ x)
lora_ahas shape (rank, in_features) — initialized with Kaiming uniformlora_bhas shape (out_features, rank) — initialized with zeros
Because B starts at zero, the initial LoRA contribution is zero and the model produces the same output as the original frozen weights.
Implementations§
Source§impl LoraLinear
impl LoraLinear
Sourcepub fn new(weight: Array2<f32>, rank: usize, alpha: f32) -> ModelResult<Self>
pub fn new(weight: Array2<f32>, rank: usize, alpha: f32) -> ModelResult<Self>
Create a new LoRA-adapted linear layer.
The weight matrix should have shape (out_features, in_features). LoRA A is initialized with Kaiming-uniform values, B with zeros.
Sourcepub fn forward(&self, input: &Array1<f32>) -> ModelResult<Array1<f32>>
pub fn forward(&self, input: &Array1<f32>) -> ModelResult<Array1<f32>>
Forward pass for a single input vector.
Computes: output = W @ x + scaling * B @ (A @ x)
Sourcepub fn forward_batch(&self, input: &Array2<f32>) -> ModelResult<Array2<f32>>
pub fn forward_batch(&self, input: &Array2<f32>) -> ModelResult<Array2<f32>>
Forward pass for a batch of inputs.
Input shape: (batch_size, in_features) Output shape: (batch_size, out_features)
Sourcepub fn merge(&mut self) -> ModelResult<()>
pub fn merge(&mut self) -> ModelResult<()>
Merge LoRA weights into the original weight matrix for inference.
After merging, forward passes use only the modified W with no extra computation. W = W + scaling * B @ A
Sourcepub fn unmerge(&mut self) -> ModelResult<()>
pub fn unmerge(&mut self) -> ModelResult<()>
Unmerge LoRA weights from the original weight matrix.
Restores W to its original values for continued training. W = W - scaling * B @ A
Sourcepub fn trainable_params(&self) -> usize
pub fn trainable_params(&self) -> usize
Number of trainable parameters: rank * (in_features + out_features)
Sourcepub fn total_params(&self) -> usize
pub fn total_params(&self) -> usize
Total parameters including frozen weights
Sourcepub fn compression_ratio(&self) -> f32
pub fn compression_ratio(&self) -> f32
Ratio of trainable to total parameters
Sourcepub fn set_lora_a(&mut self, a: Array2<f32>) -> ModelResult<()>
pub fn set_lora_a(&mut self, a: Array2<f32>) -> ModelResult<()>
Set the LoRA A matrix, validating dimensions
Sourcepub fn set_lora_b(&mut self, b: Array2<f32>) -> ModelResult<()>
pub fn set_lora_b(&mut self, b: Array2<f32>) -> ModelResult<()>
Set the LoRA B matrix, validating dimensions
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Whether LoRA is currently enabled
Trait Implementations§
Source§impl Clone for LoraLinear
impl Clone for LoraLinear
Source§fn clone(&self) -> LoraLinear
fn clone(&self) -> LoraLinear
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for LoraLinear
impl RefUnwindSafe for LoraLinear
impl Send for LoraLinear
impl Sync for LoraLinear
impl Unpin for LoraLinear
impl UnsafeUnpin for LoraLinear
impl UnwindSafe for LoraLinear
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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