pub struct ParallelOptimizer<O, A, D>where
O: Optimizer<A, D> + Clone + Send + Sync,
A: Float + ScalarOperand + Debug + Send + Sync,
D: Dimension,{ /* private fields */ }Expand description
Parallel optimizer wrapper for processing multiple parameter groups
This wrapper enables parallel processing of multiple parameter groups, providing significant speedup on multi-core systems.
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{SGD, Optimizer};
use optirs_core::parallel_optimizer::ParallelOptimizer;
// Create base optimizer
let optimizer = SGD::new(0.01);
// Wrap in parallel optimizer
let mut parallel_opt = ParallelOptimizer::new(optimizer);
// Process multiple parameter groups in parallel
let params_list = vec![
Array1::zeros(1000),
Array1::zeros(2000),
Array1::zeros(1500),
];
let grads_list = vec![
Array1::from_elem(1000, 0.1),
Array1::from_elem(2000, 0.1),
Array1::from_elem(1500, 0.1),
];
let updated = parallel_opt.step_parallel_groups(¶ms_list, &grads_list).expect("parallel_opt.step_parallel_groups succeeds");§State handling
Stateful optimizers (Adam, AdamW, LAMB, RAdam, Lion, …) keep per-parameter moment estimates. A dedicated optimizer instance is therefore materialized and retained for every parameter group, and each group’s instance is mutated in place across calls. Without this, every call would optimize with a freshly reset clone and, for example, Adam would degenerate into sign-SGD.
Implementations§
Source§impl<O, A, D> ParallelOptimizer<O, A, D>
impl<O, A, D> ParallelOptimizer<O, A, D>
Sourcepub fn new(base_optimizer: O) -> Self
pub fn new(base_optimizer: O) -> Self
Creates a new parallel optimizer wrapper
§Arguments
base_optimizer- The base optimizer to parallelize
Sourcepub fn num_groups(&self) -> usize
pub fn num_groups(&self) -> usize
Number of parameter groups for which persistent state is currently held
Sourcepub fn group_optimizer(&self, group: usize) -> Option<&O>
pub fn group_optimizer(&self, group: usize) -> Option<&O>
Access the persistent optimizer instance of a parameter group
Sourcepub fn group_optimizer_mut(&mut self, group: usize) -> Option<&mut O>
pub fn group_optimizer_mut(&mut self, group: usize) -> Option<&mut O>
Access the persistent optimizer instance of a parameter group mutably
Sourcepub fn reset_group_state(&mut self)
pub fn reset_group_state(&mut self)
Drop all per-group state, restarting every group from the base optimizer
Sourcepub fn step_parallel_groups(
&mut self,
params_list: &[Array<A, D>],
grads_list: &[Array<A, D>],
) -> Result<Vec<Array<A, D>>>
pub fn step_parallel_groups( &mut self, params_list: &[Array<A, D>], grads_list: &[Array<A, D>], ) -> Result<Vec<Array<A, D>>>
Sourcepub fn get_learning_rate(&self) -> A
pub fn get_learning_rate(&self) -> A
Get the current learning rate from the base optimizer
Sourcepub fn set_learning_rate(&mut self, learning_rate: A)
pub fn set_learning_rate(&mut self, learning_rate: A)
Set the learning rate on the base optimizer and on every per-group instance
Trait Implementations§
Auto Trait Implementations§
impl<O, A, D> Freeze for ParallelOptimizer<O, A, D>where
O: Freeze,
impl<O, A, D> RefUnwindSafe for ParallelOptimizer<O, A, D>
impl<O, A, D> Send for ParallelOptimizer<O, A, D>
impl<O, A, D> Sync for ParallelOptimizer<O, A, D>
impl<O, A, D> Unpin for ParallelOptimizer<O, A, D>
impl<O, A, D> UnsafeUnpin for ParallelOptimizer<O, A, D>where
O: UnsafeUnpin,
impl<O, A, D> UnwindSafe for ParallelOptimizer<O, A, D>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.