Skip to content
class

Conv3d

extends_QuantizedConvNd
Conv3d(in_channels: int, out_channels: int, kernel_size: _IntTuple, stride: _IntTuple, padding: _IntTuple, dilation: _IntTuple, groups: int, bias: bool)

Implementing kernel

C++ engine symbols that back this Python API.

Quantized 3-D convolution — int8 kernel, dequantize-to-float compute.

The volumetric member of the quantized conv family (video, 3-D medical imaging), and the inference-time replacement that lucid.quantization.convert installs for a calibrated float lucid.nn.Conv3d. Volumetric kernels are the largest weights in a 3-D net, so the int8 checkpoint / working-set savings matter most here.

Representation (sidecar design B). The learned float kernel is quantized once, at from_float time, into an int8 code tensor plus a per-output-channel scale / zero_point; the float kernel is then dropped. Only the int8 codes, the qparams, and the (still-float) bias live in the module, so the checkpoint is far smaller than the float layer's. Each forward dequantizes the kernel back to float, runs the ordinary F.conv3d, and finally fake-quantizes the output onto the activation grid that calibration observed. Keeping the convolution itself in float means the result matches a genuine int8 kernel to within a rounding step and the layer runs unchanged on any device, with no int8 conv kernel required.

Per-channel weights. The kernel (out_channels, in_channels/groups, kd, kh, kw) is quantized independently for every output channel (axis 0): each filter carries its own scale and zero-point. Because different output filters often span very different dynamic ranges, per-channel quantization tracks them far more tightly than a single per-tensor scale, and is the standard accuracy-preserving choice for convolution kernels. The bias is small and precision-sensitive, so it is left in float.

Encoding happens once (at from_float); decode + convolve run on every forward:

wo,c,d,i,jq=clamp⁡ ⁣(round⁡(wo,c,d,i,j/so)+zo, −128, 127),w^o,c,d,i,j=(wo,c,d,i,jq−zo) sow^{q}_{o,c,d,i,j} = \operatorname{clamp}\!\bigl( \operatorname{round}(w_{o,c,d,i,j}/s_o) + z_o,\ -128,\ 127\bigr), \qquad \hat{w}_{o,c,d,i,j} = (w^{q}_{o,c,d,i,j} - z_o)\, s_o y=fake_quant⁡ ⁣(conv3d⁡(x,w^)+b),fake_quant⁡(t)=(clamp⁡(round⁡(t/S)+Z, qmin⁡, qmax⁡)−Z) Sy = \operatorname{fake\_quant}\!\bigl( \operatorname{conv3d}(x, \hat{w}) + b\bigr), \qquad \operatorname{fake\_quant}(t) = \bigl( \operatorname{clamp}(\operatorname{round}(t/S) + Z,\ q_{\min},\ q_{\max}) - Z\bigr)\, S

where so,zos_o, z_o are the per-output-channel weight scale / zero-point (index oo runs over out_channels) and S,ZS, Z the scalar calibrated output (scale, zero_point) with grid bounds qmin⁡,qmax⁡q_{\min}, q_{\max} (0, 255 for the default quint8). The decode → float-conv → re-quantize round-trip is what makes the output bit-equivalent to a true int8 kernel without needing one.

Parameters

in_channelsint
Number of channels in the input volume.
out_channelsint
Number of channels produced by the convolution — quantized per-channel.
kernel_sizeint or tuple of int
Size of the convolving kernel.
strideint or tuple of int
Stride of the convolution.
paddingint or tuple of int
Implicit zero-padding added to both sides of each spatial dim.
dilationint or tuple of int
Spacing between kernel elements.
groupsint
Number of blocked connections from input to output channels.
biasbool
Whether a learned float bias term is added after the convolution.

Attributes

weight_int8Tensor
The int8 kernel codes, shape (out_channels, in_channels/groups, kd, kh, kw).
weight_scaleTensor
Per-output-channel weight scale, shape (out_channels,).
weight_zero_pointTensor
Per-output-channel weight zero-point, shape (out_channels,).
scaleTensor
Scalar output-activation scale, set from the calibrated observer.
zero_pointTensor
Scalar output-activation zero-point, set from the calibrated observer.
biasTensor or None
The float bias of shape (out_channels,), or None.

Notes

  • Instances are normally produced by lucid.quantization.convert (or from_float), not constructed directly: a freshly constructed layer has identity qparams (scale = 1, zero_point = 0) and zeroed kernel codes, and only becomes meaningful once from_float / load_state_dict fills the buffers.
  • Only int8 codes + qparams + float bias enter the state_dict; the float kernel never does. The weight payload shrinks ~3.55x (int8) and a whole model checkpoint ~3.97x (measured on resnet_18, a conv-heavy net).
  • The kernel is quantized per-output-channel on axis 0 — each filter gets its own scale / zero-point, far tighter than one per-tensor scale for wide kernels; the bias stays float.
  • This layer wins on memory, not compute — the convolution runs in float (CPU stream = Accelerate, GPU stream = MLX), so its speed tracks the float layer's. There is no weight-only int8 conv GEMM analogue of lucid.nn.quantized.QuantizedLinearMLX; the payoff is the smaller checkpoint and working set.
  • Device-transparent: because compute stays in float, the layer runs unchanged on CPU or Metal and follows the input tensor's device.
  • Calibration is required — the output (scale, zero_point) come from an activation observer that must see representative data between prepare and convert; an uncalibrated observer keeps its ±inf seed, collapsing scale toward eps and the output toward zero (from_float warns loudly in that case).
  • Only integer / tuple padding with padding_mode="zeros" is supported; string "same" / "valid" padding raises NotImplementedError at from_float.
  • _activation is the identity here; the fused lucid.nn.quantized.ConvReLU3d overrides it to apply ReLU before the output fake-quant so calibration and inference see the same post-ReLU range.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> model = nn.Sequential(nn.Conv3d(3, 8, 3))
>>> model.qconfig = Q.get_default_qconfig()
>>> prepared = Q.prepare(model)                  # insert observers
>>> _ = prepared(lucid.randn(1, 3, 8, 16, 16))   # calibrate on representative data
>>> qmodel = Q.convert(prepared)                 # float Conv3d -> quantized Conv3d
>>> type(qmodel[0]).__name__
'Conv3d'
>>> qmodel(lucid.randn(1, 3, 8, 16, 16)).shape   # int8-weight inference
(1, 8, 6, 14, 14)
Constructing the layer directly is **not** a substitute for that workflow — a
bare instance carries zeroed codes and identity qparams, so it returns garbage
until from_float / load_state_dict populates the buffers:
>>> broken = nn.quantized.Conv3d(3, 8, (3, 3, 3), (1, 1, 1), (0, 0, 0), (1, 1, 1), 1, True)
>>> bool((broken.weight_int8 == 0).all().item())
True

See Also

Used by 2

Class methods

1
cls

from_float

→_QuantizedConvNdfrom _QuantizedConvNd
from_float(mod: nn.Module)

Quantize a calibrated float convolution module.

Instance methods

2
fn

extra_repr

→strfrom _QuantizedConvNd
extra_repr()

Override to add extra repr info (e.g. Linear shows in_features, etc.).

fn

forward

→Tensorfrom _QuantizedConvNd
forward(input: Tensor)

Dequantize the kernel, convolve, fake-quantize the output.