pub struct RMSNorm { /* private fields */ }Expand description
Root Mean Square Layer Normalization (Zhang & Sennrich, 2019).
A simplified version of LayerNorm that only uses the root mean square
for normalization, without centering (no mean subtraction).
This is faster than LayerNorm while achieving similar results.
y = x / RMS(x) * gamma
RMS(x) = sqrt(mean(x^2) + eps)Used in LLaMA, Gemma, and other modern transformers.
§Example
use aprender::nn::{RMSNorm, Module};
use aprender::autograd::Tensor;
let norm = RMSNorm::new(&[256]); // Normalize over 256 features
let x = Tensor::randn(&[32, 10, 256]); // [batch, seq, features]
let y = norm.forward(&x); // Normalized§References
- Zhang, B., & Sennrich, R. (2019). Root Mean Square Layer Normalization.
NeurIPS.
Implementations§
Source§impl RMSNorm
impl RMSNorm
Sourcepub fn with_eps(normalized_shape: &[usize], eps: f32) -> Self
pub fn with_eps(normalized_shape: &[usize], eps: f32) -> Self
Create RMSNorm with custom epsilon.
Sourcepub fn without_affine(normalized_shape: &[usize]) -> Self
pub fn without_affine(normalized_shape: &[usize]) -> Self
Create RMSNorm without learnable parameters.
Sourcepub fn normalized_shape(&self) -> &[usize]
pub fn normalized_shape(&self) -> &[usize]
Get the normalized shape.
Sourcepub fn set_weight(&mut self, weight: Tensor)
pub fn set_weight(&mut self, weight: Tensor)
Set weight tensor from external data.
Used for loading pre-trained weights.
Sourcepub fn placeholder(normalized_shape: &[usize]) -> Self
pub fn placeholder(normalized_shape: &[usize]) -> Self
Create a placeholder RMSNorm layer with minimal memory allocation.
Used for lazy initialization when loading pre-trained weights. The placeholder uses 1-element tensors instead of full vectors, reducing memory from O(n) to O(1).
IMPORTANT: This layer will NOT work for inference until
set_weight() is called with real weights.
Trait Implementations§
Source§impl Module for RMSNorm
impl Module for RMSNorm
Source§fn parameters_mut(&mut self) -> Vec<&mut Tensor>
fn parameters_mut(&mut self) -> Vec<&mut Tensor>
Source§fn refresh_caches(&mut self)
fn refresh_caches(&mut self)
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Auto Trait Implementations§
impl Freeze for RMSNorm
impl !RefUnwindSafe for RMSNorm
impl Send for RMSNorm
impl Sync for RMSNorm
impl Unpin for RMSNorm
impl !UnwindSafe for RMSNorm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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