Skip to main content

Matrix

Struct Matrix 

Source
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

Source

pub fn new(n_row: usize, n_col: usize) -> Matrix

Creates a new matrix initialised with zeros

§Params
  • rows - Number of rows in the matrix (typically vocabulary size)
  • n_col - Number of elements per row (embedding dimension)
§Returns

A new Matrix with all elements set to 0.0

Source

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.

Source

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

Source

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.
Source

pub fn norm(&self, i: usize) -> f32

Computes the L2 norm of a matrix row

§Params
  • i - Row index
§Returns

The L2 norm (Euclidean length) of the row vector

Source

pub fn zero(&mut self)

Sets all matrix elements to zero

Source

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 have n_col elements)
  • i - Row index
  • mul - Scaling factor for the vector
§Safety

The caller must ensure vec points to at least n_col valid f32 elements.

Source

pub unsafe fn dot_row(&self, vec: *const f32, i: usize) -> f32

Computes dot product between a vector and a matrix row

§Params
  • vec - Pointer to the vector (must have row_size elements)
  • i - Row index
§Returns

The dot product result

§Safety

The caller must ensure vec points to at least row_size valid f32 elements

Source

pub fn dot_two_row(&self, i: usize, j: usize) -> f32

Computes dot product between two matrix rows

§Params
  • i - First row index
  • j - Second row index
§Returns

The dot product of row i and row j

Source

pub fn get_row(&mut self, i: usize) -> *mut f32

Gets a mutable pointer to a matrix row

§Params
  • i - Row index
§Returns

Mutable pointer to the start of row i

§Safety

The caller must ensure the pointer is used correctly and doesn’t create aliasing issues. Primarily used for passing to SIMD functions.

Source

pub fn row_as_slice(&self, i: usize) -> &[f32]

Returns a shared slice of row i

§Params
  • i - Row index
§Returns

Slice of the row data

Source

pub fn get_row_unmod(&self, i: usize) -> *const f32

Gets a const pointer to a matrix row

§Params
  • i - Row index
§Returns

Const pointer to the start of row i

Source

pub fn n_col(&self) -> usize

Returns the number of elements per row

Source

pub fn n_rows(&self) -> usize

Returns the total number of rows

Source

pub fn to_faer(&self) -> Mat<f32>

Converts the matrix to a Faer matrix

§Returns

Faer matrix

Source

pub fn write_csv(&self, path: &str) -> Result<()>

Write the matrix rows to a CSV file

§Params
  • path - Path to the output CSV
Source

pub fn average_with(&self, other: &Matrix) -> Matrix

Compute the element-wise average of two matrices

§Params
  • other - The other matrix (must have same dimensions)
§Returns

A new matrix where each element is (self + other) / 2

§Panics

Panics if dimensions do not match.

Trait Implementations§

Source§

impl Debug for Matrix

Source§

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

Formats the value using the given formatter. Read more

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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

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

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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