pub struct LoRAOptimizer { /* private fields */ }Expand description
LoRA-specific optimizer that only updates adapter parameters.
Implementations§
Source§impl LoRAOptimizer
impl LoRAOptimizer
Sourcepub fn new(
base_optimizer: Box<dyn OptimizerState>,
config: LoRAConfig,
learning_rate: f32,
) -> Self
pub fn new( base_optimizer: Box<dyn OptimizerState>, config: LoRAConfig, learning_rate: f32, ) -> Self
Creates a new LoRA optimizer.
Sourcepub fn add_adapter(
&mut self,
module_name: &str,
input_dim: usize,
output_dim: usize,
) -> Result<()>
pub fn add_adapter( &mut self, module_name: &str, input_dim: usize, output_dim: usize, ) -> Result<()>
Add a LoRA adapter for a specific module.
Sourcepub fn remove_adapter(&mut self, module_name: &str) -> Option<LoRAAdapter>
pub fn remove_adapter(&mut self, module_name: &str) -> Option<LoRAAdapter>
Remove a LoRA adapter.
Sourcepub fn get_adapter(&self, module_name: &str) -> Option<&LoRAAdapter>
pub fn get_adapter(&self, module_name: &str) -> Option<&LoRAAdapter>
Get a reference to an adapter.
Sourcepub fn get_adapter_mut(&mut self, module_name: &str) -> Option<&mut LoRAAdapter>
pub fn get_adapter_mut(&mut self, module_name: &str) -> Option<&mut LoRAAdapter>
Get a mutable reference to an adapter.
Sourcepub fn set_adapter_active(
&mut self,
module_name: &str,
active: bool,
) -> Result<()>
pub fn set_adapter_active( &mut self, module_name: &str, active: bool, ) -> Result<()>
Enable or disable an adapter.
Sourcepub fn set_all_adapters_active(&mut self, active: bool)
pub fn set_all_adapters_active(&mut self, active: bool)
Enable or disable all adapters.
Sourcepub fn num_trainable_parameters(&self) -> usize
pub fn num_trainable_parameters(&self) -> usize
Get the total number of trainable parameters.
Sourcepub fn freeze_base_parameters(&mut self, parameters: HashMap<String, Tensor>)
pub fn freeze_base_parameters(&mut self, parameters: HashMap<String, Tensor>)
Freeze base model parameters.
Sourcepub fn merge_adapters_into_base(&mut self) -> Result<()>
pub fn merge_adapters_into_base(&mut self) -> Result<()>
Merge all active adapters into their respective base weights.
Sourcepub fn save_adapters(&self) -> HashMap<String, (Tensor, Tensor, f32)>
pub fn save_adapters(&self) -> HashMap<String, (Tensor, Tensor, f32)>
Save adapter weights (for efficient storage/sharing).
Sourcepub fn load_adapters(
&mut self,
adapters: HashMap<String, (Tensor, Tensor, f32)>,
) -> Result<()>
pub fn load_adapters( &mut self, adapters: HashMap<String, (Tensor, Tensor, f32)>, ) -> Result<()>
Load adapter weights.
Sourcepub fn get_config(&self) -> &LoRAConfig
pub fn get_config(&self) -> &LoRAConfig
Get configuration.
Trait Implementations§
Source§impl Debug for LoRAOptimizer
impl Debug for LoRAOptimizer
Source§impl OptimizerState for LoRAOptimizer
impl OptimizerState for LoRAOptimizer
Source§fn zero_grad(&mut self) -> AnyhowResult<()>
fn zero_grad(&mut self) -> AnyhowResult<()>
Zero out gradients
Source§fn state_dict(&self) -> AnyhowResult<HashMap<String, Tensor>>
fn state_dict(&self) -> AnyhowResult<HashMap<String, Tensor>>
Save optimizer state to dictionary
Source§fn load_state_dict(
&mut self,
state: HashMap<String, Tensor>,
) -> AnyhowResult<()>
fn load_state_dict( &mut self, state: HashMap<String, Tensor>, ) -> AnyhowResult<()>
Load optimizer state from dictionary
Auto Trait Implementations§
impl Freeze for LoRAOptimizer
impl !RefUnwindSafe for LoRAOptimizer
impl !Send for LoRAOptimizer
impl !Sync for LoRAOptimizer
impl Unpin for LoRAOptimizer
impl !UnwindSafe for LoRAOptimizer
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
Mutably borrows from an owned value. Read more
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>
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 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>
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