pub struct GradScaler { /* private fields */ }Implementations§
Source§impl GradScaler
impl GradScaler
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a scaler with the PyTorch default configuration
(init_scale=65536, growth 2.0, backoff 0.5, interval 2000).
Sourcepub fn with_config(
init_scale: f32,
growth_factor: f32,
backoff_factor: f32,
growth_interval: u64,
) -> Result<Self>
pub fn with_config( init_scale: f32, growth_factor: f32, backoff_factor: f32, growth_interval: u64, ) -> Result<Self>
Create a scaler with an explicit dynamic-scaling configuration.
§Errors
Returns an error when init_scale, growth_factor, or
backoff_factor is not positive and finite, or growth_interval is
zero.
Sourcepub fn capture_state(&self) -> Result<GradScalerState>
pub fn capture_state(&self) -> Result<GradScalerState>
Capture the resume-relevant scaler state.
In host mode this reads the struct fields; after prepare_captured
the device owns scale bookkeeping, so capture reads the device scale
and counter back (one scalar synchronization each — checkpoints are
infrequent). found_inf is transient within a step and deliberately
absent: a checkpoint commits at completed updates.
§Errors
Returns an error when device readback fails.
Sourcepub fn restore_state(&mut self, state: GradScalerState) -> Result<()>
pub fn restore_state(&mut self, state: GradScalerState) -> Result<()>
Restore scaler state captured by GradScaler::capture_state.
Host fields always update; when device state exists the scale and counter upload into the live device matrices rather than replacing them, because the optimizer’s finite-gate binding references those exact buffers.
§Errors
Returns an error when the scale is not positive and finite or the device upload fails.
Sourcepub fn prepare_captured(&mut self, runtime: &Runtime) -> Result<()>
pub fn prepare_captured(&mut self, runtime: &Runtime) -> Result<()>
Allocate device-resident scaler state on runtime for captured replay.
After this call, scale_loss, unscale_gradients, and
record_finite_gate all reference the device buffers instead of the
host scale, so a captured program replays scale adaptation and the
optimizer gate without a mid-step host decision. Bind
GradScaler::finite_flag to the optimizer once through
Optimizer::set_finite_gate.
§Errors
Returns an error when buffer allocation or upload fails.
Sourcepub fn finite_flag(&self) -> Result<Matrix>
pub fn finite_flag(&self) -> Result<Matrix>
Return the device-resident finite flag for optimizer binding.
The flag is a single-element F32 matrix written by
GradScaler::record_finite_gate; bind it once with
Optimizer::set_finite_gate before the first captured step.
§Errors
Returns an error when prepare_captured has not run.
Sourcepub fn record_finite_gate(&mut self, check: &Matrix) -> Result<()>
pub fn record_finite_gate(&mut self, check: &Matrix) -> Result<()>
Record the device-side finite gate update for the current step.
Dispatches MlGradScalerAdvanceF32: check decides the flag (finite
iff every unscaled gradient element is finite), the scale backs off on
overflow or grows after growth_interval finite steps, and the growth
counter advances — all in device buffers, so the record replays the
full dynamic-scaling contract inside a captured training program.
Must follow unscale_gradients in record order.
§Errors
Returns an error when prepare_captured has not run, check is not a
same-runtime single-element F32 matrix, or recording fails.
Sourcepub fn scale_loss(&self, loss: &Matrix) -> Result<Matrix>
pub fn scale_loss(&self, loss: &Matrix) -> Result<Matrix>
Multiply loss by the current scale as a recorded device operation.
Under prepare_captured the multiplier is the device scale buffer, so
replayed steps apply the scale the device-side advance produced for
that step rather than the value baked at capture time.
§Errors
Returns an error when runtime recording fails.
Sourcepub fn unscale_gradients(&self, parameters: &[Parameter]) -> Result<Matrix>
pub fn unscale_gradients(&self, parameters: &[Parameter]) -> Result<Matrix>
Divide every nonempty parameter gradient by the scale in place and
return a [1] accumulator that is finite iff every unscaled gradient
element is finite.
The unscale and the check are device-resident: each gradient is rescaled into detached replacement storage and its full sum is folded into the accumulator, so NaN or Inf propagates into the check value.
§Errors
Returns an error when runtime recording or allocation fails.
Sourcepub fn step_optimizer(
&mut self,
optimizer: &mut dyn Optimizer,
check: &Matrix,
) -> Result<bool>
pub fn step_optimizer( &mut self, optimizer: &mut dyn Optimizer, check: &Matrix, ) -> Result<bool>
Apply optimizer.step() when check is finite; skip the update and
flag the scale for backoff when it is not.
This is the one host boundary of the scaler contract: the check value is read back once per step, matching the CPU-side gate in torch.
§Errors
Returns an error when the check matrix is empty, readback fails, or
optimizer.step() fails.
Sourcepub fn update(&mut self)
pub fn update(&mut self)
Adjust the scale after step_optimizer: halve it on a skipped step,
otherwise double it after growth_interval consecutive finite steps.
This is the eager-path companion. Under prepare_captured the
device-side advance owns scale bookkeeping during replay and this is a
no-op; device_scale reads the current value back when needed.
Sourcepub fn device_scale(&self) -> Result<f32>
pub fn device_scale(&self) -> Result<f32>
Read the current device-resident scale back to the host.
Costs one scalar synchronization; intended for diagnostics and logging, not per-step control flow.
§Errors
Returns an error when prepare_captured has not run or the readback
fails.