pub struct LoraLinear {
pub frozen_weight: NodeId,
pub lora_a: NodeId,
pub lora_b: NodeId,
pub bias: Option<NodeId>,
pub scaling: f32,
pub in_features: usize,
pub out_features: usize,
pub rank: usize,
}Expand description
A LoRA adapter for a linear layer.
Wraps a frozen weight matrix with trainable low-rank A and B matrices.
The effective weight is W + A @ B * scaling where scaling = alpha / rank.
In this crate, weight is stored as [in_features, out_features] (matching
LinearLayer), so:
lora_ahas shape[in_features, rank]lora_bhas shape[rank, out_features]- Forward:
y = x @ W + x @ A @ B * scaling + bias
Fields§
§frozen_weight: NodeIdOriginal frozen weight [in_features, out_features].
lora_a: NodeIdLow-rank matrix A [in_features, rank] — initialized with small random values.
lora_b: NodeIdLow-rank matrix B [rank, out_features] — initialized to zeros.
bias: Option<NodeId>Optional bias (frozen).
scaling: f32Scaling factor = alpha / rank.
in_features: usize§out_features: usize§rank: usizeImplementations§
Source§impl LoraLinear
impl LoraLinear
Sourcepub fn new(
graph: &mut Graph,
in_features: usize,
out_features: usize,
config: &LoraConfig,
) -> Result<Self, ModelError>
pub fn new( graph: &mut Graph, in_features: usize, out_features: usize, config: &LoraConfig, ) -> Result<Self, ModelError>
Creates a LoRA adapter from dimensions.
Initializes frozen_weight and bias as graph constants (requires_grad=false),
lora_a with small Gaussian-like values (requires_grad=true),
lora_b with zeros (requires_grad=true).
Sourcepub fn from_linear(
graph: &mut Graph,
weight_node: NodeId,
bias_node: NodeId,
in_features: usize,
out_features: usize,
config: &LoraConfig,
) -> Result<Self, ModelError>
pub fn from_linear( graph: &mut Graph, weight_node: NodeId, bias_node: NodeId, in_features: usize, out_features: usize, config: &LoraConfig, ) -> Result<Self, ModelError>
Creates a LoRA adapter from an existing LinearLayer’s weights.
The original weight and bias are frozen (stored as constants).
New trainable lora_a and lora_b matrices are created.
Sourcepub fn with_bias(
self,
graph: &mut Graph,
bias_tensor: Tensor,
) -> Result<Self, ModelError>
pub fn with_bias( self, graph: &mut Graph, bias_tensor: Tensor, ) -> Result<Self, ModelError>
Creates a LoRA adapter with a frozen bias term.
Sourcepub fn forward(
&self,
graph: &mut Graph,
input: NodeId,
) -> Result<NodeId, ModelError>
pub fn forward( &self, graph: &mut Graph, input: NodeId, ) -> Result<NodeId, ModelError>
Forward pass: y = x @ W + x @ A @ B * scaling + bias.
Gradients flow through A and B but not through the frozen weight.
Sourcepub fn trainable_params(&self) -> Vec<NodeId>
pub fn trainable_params(&self) -> Vec<NodeId>
Returns the trainable parameter NodeIds (only lora_a and lora_b).
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> 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