Realm

oa::ml::optim

Module published by the oa::ml module.

MdRust public API

Public Types

Public Functions

pub fn clip_grad_norm(gradients: &[Matrix], max_norm: f32) -> Result<()>
pub fn clip_parameter_grad_norm( parameters: &[Parameter], max_norm: f32, ) -> Result<()>

Function Documentation

pub fn clip_grad_norm(gradients: &[Matrix], max_norm: f32) -> Result<()>
Clip the combined L2 norm of nonempty FP32 gradients in place. The operation records one GPU reduction followed by one GPU scaling pass. It does not read gradients back to the CPU or wait for completion. Empty matrices are ignored; at most sixteen nonempty gradients are admitted. Errors Returns an error when max_norm is negative or non-finite, more than sixteen nonempty gradients are supplied, gradients do not share one runtime, a gradient is not FP32, storage is duplicated, a size exceeds the shader ABI, or allocation and runtime recording fail.
pub fn clip_parameter_grad_norm( parameters: &[Parameter], max_norm: f32, ) -> Result<()>
Clip the combined L2 norm of accumulated Parameter gradients. This is the parameter-level form of clip_grad_norm: each parameter’s live gradient handle is collected, parameters without a gradient are skipped, and gradient values are rescaled so a following Optimizer::step observes clipped values. Up to sixteen nonempty gradients scale in place through the fused two-pass kernel; larger models take an equivalent device-resident reduction and broadcast-rescale path that stores a detached replacement gradient. Call between backward and the optimizer step. Inside crate::ml::Training::step the call belongs in the record closure so the rescale is captured into the training program; in eager steps it belongs before crate::ml::Training::complete_step. Errors Returns an error when max_norm is negative or non-finite, gradients do not share one runtime or are not FP32, storages are duplicated on the fused path, a size exceeds the shader ABI, or allocation and runtime recording fail.