pub struct SparseLinear {
pub weight: Parameter,
pub bias: Option<Parameter>,
pub threshold: Parameter,
/* private fields */
}Expand description
A linear layer with a differentiable magnitude pruning mask.
During the forward pass, a soft mask is computed via sigmoid soft thresholding:
mask = sigmoid((|weight| - threshold) * temperature)
effective_weight = weight * mask
y = x @ effective_weight^T + biasThe sigmoid makes the mask differentiable, so gradients flow through it and
the network learns which weights to prune. The threshold parameter is
learnable and included in parameters().
§Structured vs Unstructured
- Structured (
structured=true): One threshold per output neuron. Entire output channels can be pruned, yielding hardware-friendly sparsity. - Unstructured (
structured=false): One threshold per weight element. Finer-grained but less hardware-friendly.
§Example
let layer = SparseLinear::new(784, 256);
let output = layer.forward(&input);
println!("Density: {:.1}%", layer.density() * 100.0);Fields§
§weight: ParameterWeight matrix of shape (out_features, in_features).
bias: Option<Parameter>Optional bias vector of shape (out_features).
threshold: ParameterLearnable magnitude thresholds. Shape depends on structured:
- Structured: (out_features,)
- Unstructured: (out_features, in_features)
Implementations§
Source§impl SparseLinear
impl SparseLinear
Sourcepub fn new(in_features: usize, out_features: usize) -> Self
pub fn new(in_features: usize, out_features: usize) -> Self
Creates a new SparseLinear layer with structured pruning and bias.
§Arguments
in_features- Size of each input sampleout_features- Size of each output sample
Sourcepub fn unstructured(in_features: usize, out_features: usize) -> Self
pub fn unstructured(in_features: usize, out_features: usize) -> Self
Creates a new SparseLinear layer with unstructured (per-weight) pruning.
§Arguments
in_features- Size of each input sampleout_features- Size of each output sample
Sourcepub fn with_bias(in_features: usize, out_features: usize, bias: bool) -> Self
pub fn with_bias(in_features: usize, out_features: usize, bias: bool) -> Self
Creates a new SparseLinear layer with configurable bias.
§Arguments
in_features- Size of each input sampleout_features- Size of each output samplebias- Whether to include a learnable bias
Sourcepub fn in_features(&self) -> usize
pub fn in_features(&self) -> usize
Returns the input feature dimension.
Sourcepub fn out_features(&self) -> usize
pub fn out_features(&self) -> usize
Returns the output feature dimension.
Sourcepub fn is_structured(&self) -> bool
pub fn is_structured(&self) -> bool
Returns whether this layer uses structured pruning.
Sourcepub fn density(&self) -> f32
pub fn density(&self) -> f32
Returns the fraction of weights that are active (above threshold).
Uses hard thresholding at |weight| >= threshold.
Sourcepub fn sparsity(&self) -> f32
pub fn sparsity(&self) -> f32
Returns the fraction of weights that are pruned.
Equivalent to 1.0 - density().
Sourcepub fn num_active(&self) -> usize
pub fn num_active(&self) -> usize
Returns the number of active (non-pruned) weights.
Sourcepub fn hard_prune(&mut self)
pub fn hard_prune(&mut self)
Permanently applies the pruning mask to the weights.
After calling this, pruned weights are zeroed and the threshold is reset to zero. This is an irreversible optimization for inference — the zeroed weights will not be recovered.
Sourcepub fn reset_threshold(&mut self, value: f32)
pub fn reset_threshold(&mut self, value: f32)
Sourcepub fn effective_weight(&self) -> Tensor<f32>
pub fn effective_weight(&self) -> Tensor<f32>
Returns the effective weight (weight * hard_mask) for inspection.
This shows what the weight matrix looks like after hard pruning, without actually modifying the layer.
Trait Implementations§
Source§impl Debug for SparseLinear
impl Debug for SparseLinear
Source§impl Module for SparseLinear
impl Module for SparseLinear
Source§fn named_parameters(&self) -> HashMap<String, Parameter>
fn named_parameters(&self) -> HashMap<String, Parameter>
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Source§fn set_training(&mut self, _training: bool)
fn set_training(&mut self, _training: bool)
Source§fn is_training(&self) -> bool
fn is_training(&self) -> bool
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
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>
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>
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