Skip to main content

SpikeGuard

Struct SpikeGuard 

Source
pub struct SpikeGuard { /* private fields */ }
Expand description

Flags a training step whose loss jumps well above the recent average, so the caller can drop it before the optimizer sees it.

The average is an exponential moving mean of the accepted losses. A loss that is not finite, or more than ratio times that mean once warmup losses have been accepted, is a spike: it is not folded into the mean, and SpikeGuard::consecutive counts how many came in a row. Skip a spike by clearing the gradients instead of stepping; after several in a row, roll back to the last checkpoint and its optimizer state.

use rusting_brain::SpikeGuard;

let mut guard = SpikeGuard::new(2.0, 0.99, 10);
for loss in [3.0, 2.9, 2.8, 2.9, 2.7, 2.8, 2.6, 2.7, 2.6, 2.5] {
    assert!(!guard.is_spike(loss));
}
assert!(guard.is_spike(9.0));
assert!(guard.is_spike(f32::NAN));
assert_eq!(guard.consecutive(), 2);
assert!(!guard.is_spike(2.6));
assert_eq!(guard.consecutive(), 0);

// In a training loop:
// let loss = model.accumulate_step(&batch)?;
// if guard.is_spike(loss.lm_loss) {
//     model.zero_grad();
//     if guard.consecutive() >= 3 {
//         model = TransformerLm::load_bin("last.bin")?;
//         model.load_optimizer_state("last.opt")?;
//     }
// } else {
//     model.step_clipped(1.0, 1.0)?;
// }

Implementations§

Source§

impl SpikeGuard

Source

pub fn new(ratio: f32, beta: f32, warmup: usize) -> Self

A loss above ratio times the moving mean is a spike. beta is the mean’s decay per accepted loss (0.99 averages about the last hundred), and no loss is a spike on size alone until warmup have been accepted.

Source

pub fn is_spike(&mut self, loss: f32) -> bool

Records loss and says whether it is a spike.

Source

pub fn consecutive(&self) -> usize

Spikes seen in a row since the last accepted loss.

Source

pub fn mean(&self) -> f32

The moving mean of the accepted losses, 0 before the first.

Trait Implementations§

Source§

impl Clone for SpikeGuard

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SpikeGuard

Source§

impl Debug for SpikeGuard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SpikeGuard

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SpikeGuard

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V