Skip to main content

Param

Struct Param 

Source
pub struct Param {
    pub value: Matrix,
    pub grad: Matrix,
    /* private fields */
}
Expand description

A weight matrix plus its gradient and Adam moments.

Only the value is serialized: a snapshot restores the weights and restarts the moment estimates from zero, which is what Network::load_json already does for dense networks.

Fields§

§value: Matrix§grad: Matrix

Implementations§

Source§

impl Param

Source

pub fn new(value: Matrix) -> Self

Source

pub fn zeros(rows: usize, cols: usize) -> Self

Source

pub fn filled(rows: usize, cols: usize, value: f32) -> Self

Every entry set to value. RMSNorm scales start at one.

Source

pub fn he_uniform( rows: usize, cols: usize, fan_in: usize, rng: &mut StdRng, ) -> Self

He-style uniform initialization, matching the dense network’s scheme so a transformer built with a given seed is as reproducible as a Network.

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn freeze(&mut self)

Stops the parameter training and releases its gradient and Adam moments, which is three quarters of what it costs to train.

The weights stay, and a forward pass is unaffected. Reversed by Param::unfreeze, which allocates the buffers again, empty.

Source

pub fn unfreeze(&mut self)

Gives the parameter its gradient and moment buffers back, zeroed.

Source

pub fn is_frozen(&self) -> bool

Source

pub fn set_fake_quantize(&mut self, enabled: bool)

Rounds this weight through the int8 grid in the forward pass, without changing what is stored or what the optimizer updates.

Source

pub fn is_fake_quantized(&self) -> bool

Whether the forward pass rounds this weight.

Source

pub fn zero_grad(&mut self)

Source

pub fn to_cuda( &mut self, device: usize, memory_budget_mib: usize, ) -> Result<(), NetworkError>

Moves the parameter onto device and keeps it there until Param::to_cpu.

While it is resident the device owns the weights: the host value, grad and moments are stale, and Linear::forward and Linear::backward run their GEMMs on the device.

A model with more than one parameter should open a CudaDevice once and use Param::to_cuda_on instead; this builds a context, and a context per weight is the thing that makes a two-hundred-parameter model slow to start.

Fails closed: built without the cuda feature this returns NetworkError::CudaFeatureDisabled rather than staying on the host and saying nothing.

Source

pub fn to_cuda_on(&mut self, device: &CudaDevice) -> Result<(), NetworkError>

Param::to_cuda onto a device a caller already opened.

Source

pub fn to_cpu(&mut self) -> Result<(), NetworkError>

Copies the weights and gradient back from the device and drops the mirror. A parameter that was never on a device is left alone.

Source

pub fn moments(&mut self) -> Result<(&Matrix, &Matrix), NetworkError>

The Adam moments, refreshed from the device first when the parameter is resident there.

A snapshot carries weights only, so a run that stops and resumes has to carry the moments separately; see TransformerLm::save_optimizer_state.

Source

pub fn set_moments( &mut self, first: Matrix, second: Matrix, ) -> Result<(), NetworkError>

Restores moments saved by an earlier run, uploading them when the parameter is already resident on a device.

Source

pub fn set_value(&mut self, value: Matrix) -> Result<(), NetworkError>

Replaces the weights, uploading them when the parameter is resident on a device.

The shape has to match: a weight is wired into a forward pass by its dimensions, and a caller that meant to reshape wants a new parameter, not this. Used by Ema::swap_in to stand an averaged weight in the live one’s place.

Source

pub fn grad_sum_squares(&self) -> Result<f64, NetworkError>

The sum of the squared gradient entries, for a caller building a global gradient norm across every parameter.

Accumulated in f64: a 100M-parameter model sums that many small squares into one number, and an f32 accumulator loses the tail of them.

Source

pub fn step(&mut self, optimizer: &Optimizer, step: usize, scale: f32)

Applies one optimizer update and clears the gradient.

step is the global Adam step count, already incremented: the first update is step 1, not 0 (while Schedule::rate counts from 0). Step 0 panics, because Adam’s bias correction 1 - beta^0 would divide by zero and turn every parameter NaN. SGD does not read it. scale divides the accumulated gradient, which lets a caller accumulate over a batch and average once here.

Weight decay applies to parameters with more than one row only. A one-row parameter is a bias or a norm gain, and shrinking those towards zero only fights the model (the nanoGPT rule).

Trait Implementations§

Source§

impl Clone for Param

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 Debug for Param

Source§

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

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

impl<'de> Deserialize<'de> for Param

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Matrix> for Param

Source§

fn from(value: Matrix) -> Self

Converts to this type from the input type.
Source§

impl From<Param> for Matrix

Source§

fn from(param: Param) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Param

Compares weights only. Gradients and moments are transient training state, and two models that predict identically are equal for every purpose a caller has.

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 Serialize for Param

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Forget for Param

§

impl Freeze for Param

§

impl Move for Param

§

impl RefUnwindSafe for Param

§

impl Send for Param

§

impl Sync for Param

§

impl Unpin for Param

§

impl UnsafeUnpin for Param

§

impl UnwindSafe for Param

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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