pub struct SparseLinear { /* private fields */ }Expand description
Sparse linear layer (also known as sparse fully connected layer)
This layer implements a linear transformation using sparse weight matrices, which can significantly reduce memory usage and computational cost for networks with high sparsity levels.
Implementations§
Source§impl SparseLinear
impl SparseLinear
Sourcepub fn new(
in_features: usize,
out_features: usize,
sparsity: f32,
use_bias: bool,
) -> TorshResult<Self>
pub fn new( in_features: usize, out_features: usize, sparsity: f32, use_bias: bool, ) -> TorshResult<Self>
Create a new sparse linear layer
Sourcepub fn with_config(
in_features: usize,
out_features: usize,
config: SparseLayerConfig,
use_bias: bool,
) -> TorshResult<Self>
pub fn with_config( in_features: usize, out_features: usize, config: SparseLayerConfig, use_bias: bool, ) -> TorshResult<Self>
Create sparse linear layer with configuration
Sourcepub fn from_weight(weight: CsrTensor, bias: Option<Tensor>) -> TorshResult<Self>
pub fn from_weight(weight: CsrTensor, bias: Option<Tensor>) -> TorshResult<Self>
Create from existing sparse weight matrix
Sourcepub fn forward(&self, input: &Tensor) -> TorshResult<Tensor>
pub fn forward(&self, input: &Tensor) -> TorshResult<Tensor>
Forward pass
Sourcepub fn weight_mut(&mut self) -> &mut CsrTensor
pub fn weight_mut(&mut self) -> &mut CsrTensor
Get mutable weight matrix
Sourcepub fn in_features(&self) -> usize
pub fn in_features(&self) -> usize
Get input features count
Sourcepub fn out_features(&self) -> usize
pub fn out_features(&self) -> usize
Get output features count
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
Sourcepub fn memory_stats(&self) -> SparseMemoryStats
pub fn memory_stats(&self) -> SparseMemoryStats
Get memory usage statistics
Trait Implementations§
Source§impl SparseLayer for SparseLinear
impl SparseLayer for SparseLinear
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 SparseLinear
impl RefUnwindSafe for SparseLinear
impl Send for SparseLinear
impl Sync for SparseLinear
impl Unpin for SparseLinear
impl UnsafeUnpin for SparseLinear
impl UnwindSafe for SparseLinear
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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