SparseConv2d

Struct SparseConv2d 

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

Sparse 2D Convolution layer

Implements 2D convolution operations optimized for sparse inputs and/or sparse kernels. This can significantly reduce computation when either the input or kernel has many zeros.

§Mathematical Formulation

For standard convolution: y[i,j] = Σ_m Σ_n w[m,n] * x[i+m, j+n] For sparse convolution: Only compute terms where w[m,n] ≠ 0

§Use Cases

  • Computer vision with sparse activations (post-ReLU)
  • Medical imaging with sparse features
  • Pruned neural networks
  • Specialized domains with naturally sparse data

Implementations§

Source§

impl SparseConv2d

Source

pub fn new( in_channels: usize, out_channels: usize, kernel_size: (usize, usize), stride: Option<(usize, usize)>, padding: Option<(usize, usize)>, dilation: Option<(usize, usize)>, sparsity: f32, use_bias: bool, ) -> TorshResult<Self>

Create a new sparse 2D convolution layer

§Arguments
  • in_channels - Number of input channels
  • out_channels - Number of output channels
  • kernel_size - Convolution kernel size (height, width)
  • stride - Stride for convolution (default: (1, 1))
  • padding - Padding for convolution (default: (0, 0))
  • dilation - Dilation for convolution (default: (1, 1))
  • sparsity - Kernel sparsity level (0.0 = dense, 1.0 = fully sparse)
  • use_bias - Whether to include learnable bias
§Returns
  • TorshResult<Self> - New sparse 2D convolution layer or error
§Example
use torsh_sparse::nn::convolution::SparseConv2d;

// Create sparse conv: 3->64 channels, 3x3 kernel, 90% sparse
let conv = SparseConv2d::new(
    3, 64, (3, 3),
    Some((1, 1)), Some((1, 1)), None,
    0.9, true
).unwrap();
Source

pub fn forward(&self, input: &Tensor) -> TorshResult<Tensor>

Forward pass for sparse convolution

§Arguments
  • input - Input tensor (batch_size, in_channels, height, width)
§Returns
  • TorshResult<Tensor> - Output tensor (batch_size, out_channels, out_height, out_width)
Source

pub fn num_parameters(&self) -> usize

Get the number of parameters

Source

pub fn kernel_sparsity(&self) -> f32

Get kernel sparsity

Source

pub fn in_channels(&self) -> usize

Get input channels

Source

pub fn out_channels(&self) -> usize

Get output channels

Source

pub fn kernel_size(&self) -> (usize, usize)

Get kernel size

Source

pub fn stride(&self) -> (usize, usize)

Get stride

Source

pub fn padding(&self) -> (usize, usize)

Get padding

Source

pub fn dilation(&self) -> (usize, usize)

Get dilation

Trait Implementations§

Source§

impl Clone for SparseConv2d

Source§

fn clone(&self) -> SparseConv2d

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SparseConv2d

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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