Skip to Content
OchreML
Layers & Activations

DepthwiseConv2d

Direct depthwise 2D convolution over NCHW tensors without im2col matrix expansion overhead.

ochreml.nn.DepthwiseConv2d applies an independent spatial filter per input channel (producing in_channels * depth_multiplier output channels) directly in Rust without im2col expansion overhead. Pair with a 1x1 Conv2d for MobileNet-style depthwise separable blocks.

Import & Signature

import ochreml.nn as nn

dw = nn.DepthwiseConv2d(
    in_channels,
    depth_multiplier=1,
    kernel_size=3,
    stride=1,
    padding=0,
    dilation=1,
    bias=True,
    dtype="float32",
    device=None,
    seed=42,
)

Parameters

ParameterTypeDefaultDescription
in_channelsintrequiredNumber of input channels.
depth_multiplierint1Number of depthwise filters per input channel (out_channels = in_channels * depth_multiplier).
kernel_sizeint | tuple[int, int]3Spatial kernel size (kH, kW).
strideint | tuple[int, int]1Convolution stride.
paddingint | tuple[int, int]0Spatial zero-padding.
dilationint | tuple[int, int]1Kernel dilation.
biasboolTrueLearnable per-channel bias.
dtypestr"float32"Tensor dtype.
devicestr | NoneNoneTarget device.
seedint42Initialization seed.

Usage Example

import ochreml.nn as nn

dw = nn.DepthwiseConv2d(in_channels=16, depth_multiplier=1, kernel_size=3, padding=1)
pw = nn.Conv2d(16, 32, kernel_size=1)

x = nn.Tensor.randn([4, 16, 16, 16])
out = pw(dw(x))  # shape: (4, 32, 16, 16)

On this page