SparseEmbedding

Struct SparseEmbedding 

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

Sparse embedding layer

This layer implements an embedding lookup table using sparse matrices, which can significantly reduce memory usage for large vocabularies with sparse word distributions.

Implementations§

Source§

impl SparseEmbedding

Source

pub fn new( vocab_size: usize, embedding_dim: usize, sparsity: f32, ) -> TorshResult<Self>

Create a new sparse embedding layer

Source

pub fn with_config( vocab_size: usize, embedding_dim: usize, sparsity: f32, config: SparseLayerConfig, ) -> TorshResult<Self>

Create sparse embedding layer with configuration

Source

pub fn from_weight(weight: CsrTensor) -> TorshResult<Self>

Create from existing sparse weight matrix

Source

pub fn forward(&self, indices: &[usize]) -> TorshResult<Tensor>

Forward pass - lookup embeddings for given indices

Source

pub fn forward_tensor(&self, indices: &Tensor) -> TorshResult<Tensor>

Forward pass with tensor input

Source

pub fn get_embedding(&self, index: usize) -> TorshResult<Tensor>

Get embedding for a single index

Source

pub fn weight(&self) -> &CsrTensor

Get weight matrix

Source

pub fn weight_mut(&mut self) -> &mut CsrTensor

Get mutable weight matrix

Source

pub fn vocab_size(&self) -> usize

Get vocabulary size

Source

pub fn embedding_dim(&self) -> usize

Get embedding dimension

Source

pub fn sparsity(&self) -> f32

Get current sparsity level

Source

pub fn num_parameters(&self) -> usize

Get number of parameters (non-zero weights)

Source

pub fn prune_to_sparsity(&mut self, target_sparsity: f32) -> TorshResult<()>

Update sparsity level by pruning weights

Source

pub fn config(&self) -> &SparseLayerConfig

Get configuration

Source

pub fn output_dimensions(&self, input_dims: &[usize]) -> Vec<usize>

Calculate output dimensions for given input dimensions

Source§

impl SparseEmbedding

Source

pub fn memory_stats(&self) -> SparseEmbeddingStats

Get memory usage statistics

Trait Implementations§

Source§

impl SparseLayer for SparseEmbedding

Source§

fn forward( &self, input: &dyn SparseTensor, ) -> TorshResult<Box<dyn SparseTensor>>

Forward pass through the layer
Source§

fn parameters(&self) -> Vec<&CsrTensor>

Get layer parameters in sparse format
Source§

fn parameters_mut(&mut self) -> Vec<&mut CsrTensor>

Get mutable layer parameters
Source§

fn layer_type(&self) -> &'static str

Get layer name/type
Source§

fn dimensions(&self) -> (Vec<usize>, Vec<usize>)

Get input and output dimensions
Source§

fn sparsity_stats(&self) -> SparseStats

Get sparsity statistics
Source§

fn train(&mut self, training: bool)

Set training mode
Source§

fn training(&self) -> bool

Check if layer is in training mode

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