pub struct Matrix { /* private fields */ }Expand description
Dense matrix stored in row-major format
The matrix data is stored as a flat vector where each row is laid out contiguously in memory for cache efficiency. This layout enables efficient SIMD operations on individual rows.
§Fields
n_col- Number of elements per row (dimension of embeddings)data- Flat storage of matrix data (rows * ncol elements)
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn norm_self(&mut self)
pub fn norm_self(&mut self)
Normalises all rows in the matrix to unit length
Each row vector is divided by its L2 norm, making it a unit vector.
Sourcepub fn make_send(self) -> MatrixWrapper
pub fn make_send(self) -> MatrixWrapper
Wraps the matrix in a thread-safe wrapper
§Returns
A MatrixWrapper that can be safely shared across threads using Arc
Sourcepub fn uniform(&mut self, bound: f32, seed: usize)
pub fn uniform(&mut self, bound: f32, seed: usize)
Initialises matrix with uniform random values
§Params
bound- Values will be sampled uniformly from [-bound, bound]seed- Seed for reproducibility.
Sourcepub unsafe fn add_row(&mut self, vec: *const f32, i: usize, mul: f32)
pub unsafe fn add_row(&mut self, vec: *const f32, i: usize, mul: f32)
Adds a scaled vector to a matrix row (SAXPY operation)
Performs: row[i] = row[i] + mul * vec
This is used during gradient updates where we add scaled gradients to embedding vectors.
§Params
vec- Pointer to the vector to add (must haven_colelements)i- Row indexmul- Scaling factor for the vector
§Safety
The caller must ensure vec points to at least n_col valid f32
elements.
Sourcepub fn dot_two_row(&self, i: usize, j: usize) -> f32
pub fn dot_two_row(&self, i: usize, j: usize) -> f32
Sourcepub fn row_as_slice(&self, i: usize) -> &[f32]
pub fn row_as_slice(&self, i: usize) -> &[f32]
Sourcepub fn get_row_unmod(&self, i: usize) -> *const f32
pub fn get_row_unmod(&self, i: usize) -> *const f32
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Matrix
impl RefUnwindSafe for Matrix
impl Send for Matrix
impl Sync for Matrix
impl Unpin for Matrix
impl UnsafeUnpin for Matrix
impl UnwindSafe for Matrix
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
Mutably borrows from an owned value. Read more
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
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>
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 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>
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