Skip to main content

ConvNextBlock

Struct ConvNextBlock 

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

A single ConvNeXt block.

All weight tensors are flat row-major Vec<f32>.

Implementations§

Source§

impl ConvNextBlock

Source

pub fn new(cfg: ConvNextConfig, rng: &mut LcgRng) -> VisionResult<Self>

Construct a new block with random depthwise / pointwise weights, zeroed depthwise bias, identity LayerNorm affine, and layer-scale gamma set to cfg.layer_scale_init.

Kernels and pointwise weights use a Kaiming-ish scaled normal initialisation drawn from the deterministic LCG RNG.

§Errors

Propagates configuration validation from ConvNextConfig::new.

Source

pub fn config(&self) -> &ConvNextConfig

Read-only access to the block configuration.

Source

pub fn dw_kernel_mut(&mut self) -> &mut [f32]

Mutable access to the depthwise kernel (used for tests that install an identity / delta kernel).

Source

pub fn dw_bias_mut(&mut self) -> &mut [f32]

Mutable access to the depthwise bias.

Source

pub fn depthwise_conv(&self, x: &[f32]) -> VisionResult<Vec<f32>>

Depthwise convolution: each channel c is convolved with its own kernel × kernel filter, zero-padded “same” (pad = (k-1)/2), stride 1, plus per-channel bias.

Input / output are (C, H, W) flat.

§Errors

DimensionMismatch if x.len() != C·H·W.

Source

pub fn channel_layernorm(&self, x: &[f32]) -> VisionResult<Vec<f32>>

Channel LayerNorm: at each spatial position (h, w), gather the C channel values, normalise to zero-mean / unit-variance (ε = 1e-6), then apply per-channel affine γ · x̂ + β.

Input / output are (C, H, W) flat.

§Errors

DimensionMismatch if x.len() != C·H·W.

Source

pub fn forward(&self, x: &[f32]) -> VisionResult<Vec<f32>>

Forward pass: (C·H·W) → (C·H·W).

See the module docs for the full pipeline. When layer_scale_init is 0.0, the residual branch is multiplied by zero so forward(x) == x.

§Errors
  • DimensionMismatch if x.len() != C·H·W.
  • NonFinite if the output contains non-finite values.
Source

pub fn n_params(&self) -> usize

Total number of learnable parameters in this block.

dw_kernel  : C · k²
dw_bias    : C
ln_gamma   : C
ln_beta    : C
pw1_weight : hidden · C
pw1_bias   : hidden
pw2_weight : C · hidden
pw2_bias   : C
layer_scale: C

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