pub struct PagedAdamWState {
pub exp_avg: HashMap<String, Tensor>,
pub exp_avg_sq: HashMap<String, Tensor>,
pub steps: HashMap<String, usize>,
pub page_size: usize,
pub max_gpu_memory: usize,
pub current_gpu_usage: usize,
/* private fields */
}Expand description
Paged optimizer state for CPU offloading.
Stores optimizer states (momentum, variance) on CPU and pages them to GPU as needed during parameter updates. This enables training large models on limited VRAM by trading off memory for compute.
Matches Python QLoRA’s --optim paged_adamw_32bit behavior.
Fields§
§exp_avg: HashMap<String, Tensor>First moment estimates (CPU tensors, paged to GPU on demand).
exp_avg_sq: HashMap<String, Tensor>Second moment estimates (CPU tensors, paged to GPU on demand).
steps: HashMap<String, usize>Step counts per parameter.
page_size: usizePage size in bytes.
max_gpu_memory: usizeMaximum GPU memory for optimizer states (0 = unlimited).
current_gpu_usage: usizeCurrent GPU memory usage (bytes).
Implementations§
Source§impl PagedAdamWState
impl PagedAdamWState
Sourcepub fn new(page_size: usize, max_gpu_memory: usize) -> PagedAdamWState
pub fn new(page_size: usize, max_gpu_memory: usize) -> PagedAdamWState
Create new paged optimizer state.
Sourcepub fn init_param(
&mut self,
name: &str,
shape: &[usize],
_device: &Device,
) -> Result<(), QLoraError>
pub fn init_param( &mut self, name: &str, shape: &[usize], _device: &Device, ) -> Result<(), QLoraError>
Sourcepub fn page_to_device(
&mut self,
name: &str,
device: &Device,
) -> Result<(Tensor, Tensor), QLoraError>
pub fn page_to_device( &mut self, name: &str, device: &Device, ) -> Result<(Tensor, Tensor), QLoraError>
Page state to GPU for update, returns (exp_avg, exp_avg_sq) on target device.
Updates LRU tracking and GPU memory usage.
§Errors
Returns error if device transfer fails.
Sourcepub fn page_to_cpu(
&mut self,
name: &str,
exp_avg: &Tensor,
exp_avg_sq: &Tensor,
) -> Result<(), QLoraError>
pub fn page_to_cpu( &mut self, name: &str, exp_avg: &Tensor, exp_avg_sq: &Tensor, ) -> Result<(), QLoraError>
Page state back to CPU after update.
Updates GPU memory tracking.
§Errors
Returns error if device transfer fails.
Sourcepub fn increment_step(&mut self, name: &str)
pub fn increment_step(&mut self, name: &str)
Increment step count for a parameter.
Sourcepub fn is_gpu_resident(&self, name: &str) -> bool
pub fn is_gpu_resident(&self, name: &str) -> bool
Check if a parameter’s optimizer state is currently GPU-resident.
Sourcepub fn gpu_resident_count(&self) -> usize
pub fn gpu_resident_count(&self) -> usize
Get the number of parameters currently GPU-resident.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PagedAdamWState
impl !RefUnwindSafe for PagedAdamWState
impl Send for PagedAdamWState
impl Sync for PagedAdamWState
impl Unpin for PagedAdamWState
impl !UnwindSafe for PagedAdamWState
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> 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