pub struct LoraLinear {
pub base: LoraBase,
pub lora_a: Param,
pub lora_b: Param,
pub scale: f32,
pub dropout: Dropout,
}Expand description
A frozen linear layer plus a trainable low-rank update (mlx-lm’s LoRALinear):
y = base(x) + scale · (dropout(x) A) B, with A of shape [input, r] and B of shape
[r, output] (zeros at first, so the layer starts as its base). Names follow mlx-lm:
linear.weight, lora_a, lora_b.
Fields§
§base: LoraBaseThe adapted layer (frozen).
lora_a: Param[input_dims, r].
lora_b: Param[r, output_dims].
scale: f32The update’s scale (mlx-lm’s default 20).
dropout: DropoutDropout on the update’s input (mlx-lm’s LoRA dropout; 0 by default).
Implementations§
Source§impl LoraLinear
impl LoraLinear
Sourcepub fn from_base(base: impl Into<LoraBase>, r: i32) -> Self
pub fn from_base(base: impl Into<LoraBase>, r: i32) -> Self
Adapts base with rank r and mlx-lm’s default scale 20, as mlx-lm’s
LoRALinear.from_base (including its random draws: A is uniform in
[-1/√input, 1/√input)). The base is frozen.
Sourcepub fn dropout(self, p: f32) -> Self
pub fn dropout(self, p: f32) -> Self
Drops the update’s input with probability p in training mode (mlx-lm’s LoRA
dropout; the base sees the input unchanged).
Sourcepub fn rslora(self) -> Self
pub fn rslora(self) -> Self
Rank-stabilised scaling (rsLoRA): treating scale as α/r, the update is scaled
by α/√r = scale · √r instead.
Trait Implementations§
Source§impl Clone for LoraLinear
impl Clone for LoraLinear
Source§impl Debug for LoraLinear
impl Debug for LoraLinear
Source§impl Leaves for LoraLinear
impl Leaves for LoraLinear
Source§impl Tree for LoraLinearwhere
Self: Clone,
impl Tree for LoraLinearwhere
Self: Clone,
Source§fn visit_mut(&mut self, __v: &mut VisitMut<'_>)
fn visit_mut(&mut self, __v: &mut VisitMut<'_>)
v mutably, in the order of
Leaves::visit.Source§fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self
fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self
f(path, leaf) (same structure; parameters keep
their frozen flag).Source§fn flatten(&self) -> Vec<(String, Array)>
fn flatten(&self) -> Vec<(String, Array)>
tree_flatten).Source§fn unflatten(
&self,
leaves: impl IntoIterator<Item = Array>,
) -> Result<Self, Error>
fn unflatten( &self, leaves: impl IntoIterator<Item = Array>, ) -> Result<Self, Error>
self is the template) with leaves in order: exactly
Tree::len of them, else an error naming both counts.Source§fn load_flat(
&mut self,
named: impl IntoIterator<Item = (String, Array)>,
mode: LoadMode,
) -> Result<(), Error>
fn load_flat( &mut self, named: impl IntoIterator<Item = (String, Array)>, mode: LoadMode, ) -> Result<(), Error>
LoadMode::Strict requires exactly the tree’s paths with
matching shapes; LoadMode::Partial assigns the matching names and ignores
unknown ones (shapes must still match). Loaded dtypes are kept. On any problem the
error lists every one (missing, unknown and mismatched names, sorted) and the tree is
unchanged. Duplicate paths (in the tree or in named) are an error.Auto Trait Implementations§
impl !RefUnwindSafe for LoraLinear
impl !UnwindSafe for LoraLinear
impl Freeze for LoraLinear
impl Send for LoraLinear
impl Sync for LoraLinear
impl Unpin for LoraLinear
impl UnsafeUnpin for LoraLinear
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 moreSource§impl<T> IntoResult for Twhere
T: Tree,
impl<T> IntoResult for Twhere
T: Tree,
Source§impl<T> Module for Twhere
T: Tree,
impl<T> Module for Twhere
T: Tree,
Source§fn parameters(&self) -> Vec<(String, Array)>
fn parameters(&self) -> Vec<(String, Array)>
Source§fn trainable_parameters(&self) -> Vec<(String, Array)>
fn trainable_parameters(&self) -> Vec<(String, Array)>
Source§fn num_params(&self) -> usize
fn num_params(&self) -> usize
tree_flatten total).Source§fn num_trainable(&self) -> usize
fn num_trainable(&self) -> usize
Source§fn summary(&self) -> Summary
fn summary(&self) -> Summary
Source§fn display(&self) -> ModuleDisplay<'_, Self>
fn display(&self) -> ModuleDisplay<'_, Self>
println!("{}", model.display()).Source§fn freeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
fn freeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
filter, returning how many changed.
A matching plain Array leaf cannot be frozen (only Params can): that is an
error naming the paths, and nothing changes. Buffers are never trainable anyway.Source§fn unfreeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
fn unfreeze(&mut self, filter: impl Fn(&str) -> bool) -> Result<usize>
filter, returning how many changed.
Buffers (quantized weights) and integer parameters stay non-trainable.Source§fn train(&mut self, on: bool)
fn train(&mut self, on: bool)
true, the default) or evaluation
(false, where dropout is the identity), as MLX’s Module.train(mode). (There is
no eval(): it would read like x.eval(), which evaluates.)Source§fn training(&self) -> bool
fn training(&self) -> bool
true without any).Source§fn load_weights(&mut self, src: impl WeightSource) -> Result<()>
fn load_weights(&mut self, src: impl WeightSource) -> Result<()>
load_weights(strict=True)): every leaf
must be given with its shape and nothing else (loaded dtypes are kept). On any
problem the error lists every missing, unknown and mismatched name, and the model
is unchanged. src is a safetensors path or named arrays.Source§fn load_weights_partial(&mut self, src: impl WeightSource) -> Result<()>
fn load_weights_partial(&mut self, src: impl WeightSource) -> Result<()>
load_weights(strict=False)): unknown
names are ignored, missing leaves keep their values, shapes must match.Source§fn save_weights(&self, path: impl AsRef<Path>) -> Result<()>
fn save_weights(&self, path: impl AsRef<Path>) -> Result<()>
save_weights), evaluating them. Unloaded placeholders are an error naming them.Source§fn save_trainable(&self, path: impl AsRef<Path>) -> Result<()>
fn save_trainable(&self, path: impl AsRef<Path>) -> Result<()>
Source§fn map_modules<T: Tree>(
&mut self,
filter: impl Fn(&str) -> bool,
f: impl FnMut(&str, &mut T) -> Result<()>,
) -> Result<usize>
fn map_modules<T: Tree>( &mut self, filter: impl Fn(&str) -> bool, f: impl FnMut(&str, &mut T) -> Result<()>, ) -> Result<usize>
f(path, submodule) on every submodule of type T whose path matches
filter (f may replace it, as when applying LoRA), returning how many. It stops at
the first error from f; submodules changed before it keep their change. Matching
submodules are not searched for further matches.