pub struct GradScaler { /* private fields */ }Expand description
Dynamic gradient scaler for FP16 mixed precision training.
Scales the loss to prevent gradient underflow in FP16, then unscales gradients before the optimizer step. Automatically adjusts the scale based on whether gradients contain inf/nan.
§Example
let mut scaler = GradScaler::new();
// Scale the loss in place, then run the backward pass on it.
scaler.scale_loss(&loss, &ctx, &engine);
// ... backward pass fills `gradients` ...
// Unscale and skip the optimizer step if any gradient overflowed.
if scaler.unscale_and_check(&gradients, &ctx, &engine) {
// optimizer.step(...)
}
scaler.update();Implementations§
Source§impl GradScaler
impl GradScaler
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new GradScaler with default settings.
Default: scale=65536, growth_factor=2.0, backoff_factor=0.5, growth_interval=2000
Sourcepub fn with_params(
init_scale: f32,
growth_factor: f32,
backoff_factor: f32,
growth_interval: u32,
) -> Self
pub fn with_params( init_scale: f32, growth_factor: f32, backoff_factor: f32, growth_interval: u32, ) -> Self
Create a GradScaler with custom parameters.
Sourcepub fn scale_factor(&self) -> f32
pub fn scale_factor(&self) -> f32
Get current scale factor.
Sourcepub fn scale_loss(
&self,
loss: &UnifiedTensor,
ctx: &WgpuContext,
engine: &ComputeEngine,
)
pub fn scale_loss( &self, loss: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, )
Scale the loss value before backward pass.
Scales loss in place (no new tensor is allocated).
Sourcepub fn unscale_and_check(
&mut self,
gradients: &[UnifiedTensor],
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> bool
pub fn unscale_and_check( &mut self, gradients: &[UnifiedTensor], ctx: &WgpuContext, engine: &ComputeEngine, ) -> bool
Unscale gradients and check for inf/nan.
Returns true if gradients are valid (no overflow), false otherwise. If false, you should skip the optimizer step.
Sourcepub fn update(&mut self)
pub fn update(&mut self)
Update the scale factor based on whether overflow occurred.
Call this at the end of each training step.
Sourcepub fn had_overflow(&self) -> bool
pub fn had_overflow(&self) -> bool
Check if overflow was detected in the last step.
Sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enable or disable the scaler.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Check if scaler is enabled.
Sourcepub fn stats(&self) -> GradScalerStats
pub fn stats(&self) -> GradScalerStats
Get statistics for logging.
Sourcepub fn checkpoint(&self) -> GradScalerCheckpoint
pub fn checkpoint(&self) -> GradScalerCheckpoint
Snapshot the scaler for a checkpoint.
Sourcepub fn load_checkpoint(&mut self, checkpoint: &GradScalerCheckpoint)
pub fn load_checkpoint(&mut self, checkpoint: &GradScalerCheckpoint)
Restore a snapshot taken by checkpoint.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GradScaler
impl RefUnwindSafe for GradScaler
impl Send for GradScaler
impl Sync for GradScaler
impl Unpin for GradScaler
impl UnsafeUnpin for GradScaler
impl UnwindSafe for GradScaler
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> 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