Skip to content

amp #

Mixed-precision helpers

vtl.amp provides backend-independent gradient-scaling primitives for #742. It does not change tensor dtype or dispatch kernels; it implements the scaling state machine that sits around a low-precision training step.

mut scaler := amp.new(amp.GradScalerConfig{init_scale: 65536.0})!
scaled_loss := scaler.scale_loss(loss)
// backprop scaled_loss, collect gradients
ok := scaler.unscale(mut gradients)!
if ok {
    optimizer.update()!
}
scaler.update(!ok)
  • scale_loss multiplies a loss by the current scale; unscale divides gradients in place and returns false if any value is non-finite.
  • Finite gradients are still unscaled when another gradient overflows; non-finite values are left untouched.
  • update backs off on overflow and grows after growth_interval consecutive good steps, using the configured factors.
  • state() / from_state() checkpoint the scale and good-step counter.
  • enabled: false makes every operation a no-op with scale 1.

Supported gradient dtypes are f32 and f64. Autocast policies, device-aware scaling, and distributed synchronization remain tracked in #742.

Run the focused tests with:

v test tests/amp/grad_scaler_test.v

fn eligible_ops #

fn eligible_ops() []string

eligible_ops lists operations with an autocast rule.

fn from_state #

fn from_state(state GradScalerState, config GradScalerConfig) !&GradScaler

from_state restores a scaler state created by state. The saved state.enabled flag wins: if the checkpoint was taken from a disabled scaler, the restored scaler stays disabled even when config.enabled is true.

fn new #

fn new(config GradScalerConfig) !&GradScaler

new validates a scaler configuration.

fn new_policy #

fn new_policy(enabled bool, dtype vtl.DType) !&AutocastPolicy

new_policy validates the requested dtype and creates a policy. Disabled policies keep the caller's dtype, but the dtype must still be a supported compute dtype so an unsupported value cannot hide in a disabled policy.

fn supported_compute_dtypes #

fn supported_compute_dtypes() []vtl.DType

supported_compute_dtypes lists the real VTL dtypes an autocast policy may select. VTL has no f16 element type, so half precision is deliberately absent.

struct AutocastPolicy #

struct AutocastPolicy {
pub:
	enabled bool
	dtype   vtl.DType
}

AutocastPolicy records the compute dtype for eligible operations. It holds a real vtl.DType and provides typed helpers that cast actual tensors and autograd variables, so callers run eligible ops at the selected dtype.

fn (AutocastPolicy) dtype_for #

fn (p &AutocastPolicy) dtype_for(operation string) !vtl.DType

dtype_for returns the compute dtype for an operation. Unknown operations fail explicitly rather than inheriting a cast implicitly.

fn (AutocastPolicy) compute_dtype_for #

fn (p &AutocastPolicy) compute_dtype_for[T](t &vtl.Tensor[T]) !vtl.DType

compute_dtype_for returns the dtype an eligible operation would run at for a real input tensor, combining the input dtype with the policy dtype through VTL's own promotion rules so no precision is silently lost.

fn (AutocastPolicy) should_cast #

fn (p &AutocastPolicy) should_cast[T](t &vtl.Tensor[T]) bool

should_cast reports whether a real tensor would be converted under the policy. Non-floating dtypes are left alone.

fn (AutocastPolicy) cast_f32 #

fn (p &AutocastPolicy) cast_f32[T](t &vtl.Tensor[T]) !&vtl.Tensor[f32]

cast_f32 casts a real tensor to f32 when the policy selects f32 autocast.

fn (AutocastPolicy) cast_f64 #

fn (p &AutocastPolicy) cast_f64[T](t &vtl.Tensor[T]) !&vtl.Tensor[f64]

cast_f64 casts a real tensor to f64 when the policy selects f64 autocast.

fn (AutocastPolicy) cast_variable_f32 #

fn (p &AutocastPolicy) cast_variable_f32[T](v &autograd.Variable[T]) !&autograd.Variable[f32]

cast_variable_f32 casts a real autograd variable to an f32 leaf variable, preserving requires_grad so downstream nn/la ops run at the compute dtype.

fn (AutocastPolicy) cast_variable_f64 #

fn (p &AutocastPolicy) cast_variable_f64[T](v &autograd.Variable[T]) !&autograd.Variable[f64]

cast_variable_f64 casts a real autograd variable to an f64 leaf variable.

fn (AutocastPolicy) matmul_f32 #

fn (p &AutocastPolicy) matmul_f32[T, U](a &vtl.Tensor[T], b &vtl.Tensor[U]) !&vtl.Tensor[f32]

matmul_f32 runs a real la.matmul after casting both operands to the policy's f32 compute dtype, so an nn/la call site is actually governed by autocast.

fn (AutocastPolicy) matmul_f64 #

fn (p &AutocastPolicy) matmul_f64[T, U](a &vtl.Tensor[T], b &vtl.Tensor[U]) !&vtl.Tensor[f64]

matmul_f64 runs a real la.matmul at the policy's f64 compute dtype.

struct GradScaler #

struct GradScaler {
mut:
	scale           f64
	good_steps      int
	growth_factor   f64
	backoff_factor  f64
	growth_interval int
	enabled         bool
}

GradScaler performs loss scaling, gradient unscaling, overflow detection, and scale updates.

fn (GradScaler) scale_loss #

fn (s &GradScaler) scale_loss(loss f64) f64

scale_loss multiplies a loss by the current scale.

fn (GradScaler) scale_factor #

fn (s &GradScaler) scale_factor() f64

scale_factor reports the current scale.

fn (GradScaler) unscale #

fn (mut s GradScaler) unscale[T](mut gradients []&vtl.Tensor[T]) !bool

unscale divides every gradient by the current scale in place. It returns false when any gradient is non-finite; those values are left untouched so the caller can skip the optimizer step and call update with overflow.

fn (GradScaler) update #

fn (mut s GradScaler) update(overflow bool)

update advances the scaler after a step. An overflow backs the scale off and resets the good-step counter; otherwise the scale grows after the configured number of consecutive good steps.

fn (GradScaler) state #

fn (s &GradScaler) state() GradScalerState

state returns the checkpointable scaler state.

struct GradScalerConfig #

@[params]
struct GradScalerConfig {
pub:
	init_scale      f64  = 65536.0
	growth_factor   f64  = 2.0
	backoff_factor  f64  = 0.5
	growth_interval int  = 2000
	enabled         bool = true
}

GradScalerConfig follows PyTorch's gradient-scaler defaults while staying backend-independent: callers apply the scale to a loss and pass gradients to unscale before an optimizer step.

struct GradScalerState #

struct GradScalerState {
pub:
	scale      f64
	good_steps int
	enabled    bool
}

GradScalerState is the data-only checkpoint form.