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
impl SparseEmbedding
Sourcepub fn new(
vocab_size: usize,
embedding_dim: usize,
sparsity: f32,
) -> TorshResult<Self>
pub fn new( vocab_size: usize, embedding_dim: usize, sparsity: f32, ) -> TorshResult<Self>
Create a new sparse embedding layer
Sourcepub fn with_config(
vocab_size: usize,
embedding_dim: usize,
sparsity: f32,
config: SparseLayerConfig,
) -> TorshResult<Self>
pub fn with_config( vocab_size: usize, embedding_dim: usize, sparsity: f32, config: SparseLayerConfig, ) -> TorshResult<Self>
Create sparse embedding layer with configuration
Sourcepub fn from_weight(weight: CsrTensor) -> TorshResult<Self>
pub fn from_weight(weight: CsrTensor) -> TorshResult<Self>
Create from existing sparse weight matrix
Sourcepub fn forward(&self, indices: &[usize]) -> TorshResult<Tensor>
pub fn forward(&self, indices: &[usize]) -> TorshResult<Tensor>
Forward pass - lookup embeddings for given indices
Sourcepub fn forward_tensor(&self, indices: &Tensor) -> TorshResult<Tensor>
pub fn forward_tensor(&self, indices: &Tensor) -> TorshResult<Tensor>
Forward pass with tensor input
Sourcepub fn get_embedding(&self, index: usize) -> TorshResult<Tensor>
pub fn get_embedding(&self, index: usize) -> TorshResult<Tensor>
Get embedding for a single index
Sourcepub fn weight_mut(&mut self) -> &mut CsrTensor
pub fn weight_mut(&mut self) -> &mut CsrTensor
Get mutable weight matrix
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Get vocabulary size
Sourcepub fn embedding_dim(&self) -> usize
pub fn embedding_dim(&self) -> usize
Get embedding dimension
Sourcepub fn num_parameters(&self) -> usize
pub fn num_parameters(&self) -> usize
Get number of parameters (non-zero weights)
Sourcepub fn prune_to_sparsity(&mut self, target_sparsity: f32) -> TorshResult<()>
pub fn prune_to_sparsity(&mut self, target_sparsity: f32) -> TorshResult<()>
Update sparsity level by pruning weights
Sourcepub fn config(&self) -> &SparseLayerConfig
pub fn config(&self) -> &SparseLayerConfig
Get configuration
Sourcepub fn output_dimensions(&self, input_dims: &[usize]) -> Vec<usize>
pub fn output_dimensions(&self, input_dims: &[usize]) -> Vec<usize>
Calculate output dimensions for given input dimensions
Source§impl SparseEmbedding
impl SparseEmbedding
Sourcepub fn memory_stats(&self) -> SparseEmbeddingStats
pub fn memory_stats(&self) -> SparseEmbeddingStats
Get memory usage statistics
Trait Implementations§
Source§impl SparseLayer for SparseEmbedding
impl SparseLayer for SparseEmbedding
Source§fn forward(
&self,
input: &dyn SparseTensor,
) -> TorshResult<Box<dyn SparseTensor>>
fn forward( &self, input: &dyn SparseTensor, ) -> TorshResult<Box<dyn SparseTensor>>
Forward pass through the layer
Source§fn parameters(&self) -> Vec<&CsrTensor>
fn parameters(&self) -> Vec<&CsrTensor>
Get layer parameters in sparse format
Source§fn parameters_mut(&mut self) -> Vec<&mut CsrTensor>
fn parameters_mut(&mut self) -> Vec<&mut CsrTensor>
Get mutable layer parameters
Source§fn layer_type(&self) -> &'static str
fn layer_type(&self) -> &'static str
Get layer name/type
Source§fn sparsity_stats(&self) -> SparseStats
fn sparsity_stats(&self) -> SparseStats
Get sparsity statistics
Auto Trait Implementations§
impl Freeze for SparseEmbedding
impl RefUnwindSafe for SparseEmbedding
impl Send for SparseEmbedding
impl Sync for SparseEmbedding
impl Unpin for SparseEmbedding
impl UnwindSafe for SparseEmbedding
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> 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