Skip to content
class

Conv3d

extendsConv3d
Conv3d(args: object = (), qconfig: QConfig | None = None, kwargs: object = {})

Implementing kernel

C++ engine symbols that back this Python API.

Quantization-aware 3-D convolution — trainable float kernel, fake-quant per forward.

The training-time stand-in that lucid.quantization.prepare_qat installs in place of a float lucid.nn.Conv3d (volumetric / video backbones). It keeps a trainable float kernel but routes both the weight and the layer output through a lucid.quantization.FakeQuantize on every forward, so the network feels the int8 rounding error as it learns and adapts its kernel to the eventual int8 grid. This closes the train / inference accuracy gap: a network trained with the rounding baked in barely moves when its weights are finally frozen to int8, whereas plain post-training quantization perturbs a kernel that never saw rounding.

Straight-through estimator (STE). Rounding is a step function whose derivative is zero almost everywhere, so a literal round would block all gradient flow. The fake-quant rounds in the forward pass but, in the backward pass, pretends it was the identity — the incoming gradient passes straight through to the float kernel (clipped to the quantization range). The kernel thus stays fully trainable while every forward value it produces is the dequantized number an int8 conv kernel would have computed.

Where it sits. lucid.quantization.prepare_qat deep-copies the float model and swaps each lucid.nn.Conv3d for this class, attaching the weight and activation FakeQuantize modules from the layer's qconfig. After training, lucid.quantization.convert reads the trained kernel together with the activation observer's final (scale, zero_point) and bakes them into an inference lucid.nn.quantized.Conv3d whose weight is stored as (typically per-output-channel) int8 codes.

On each forward the kernel is fake-quantized, the ordinary F.conv3d runs, and the result is fake-quantized onto the observed activation grid:

fake_quant⁡(t)=(clamp⁡(round⁡(t/s)+z, qmin⁡, qmax⁡)−z) s,∂ fake_quant⁡(t)∂t=1\operatorname{fake\_quant}(t) = \bigl( \operatorname{clamp}(\operatorname{round}(t / s) + z,\ q_{\min},\ q_{\max}) - z\bigr)\, s, \qquad \frac{\partial\, \operatorname{fake\_quant}(t)}{\partial t} = 1 y=fake_quant⁡a ⁣(fake_quant⁡w(W)⋆x+b)y = \operatorname{fake\_quant}_{a}\!\bigl( \operatorname{fake\_quant}_{w}(W) \star x + b\bigr)

where ⋆\star is the 3-D cross-correlation, s,zs, z are the scale / zero-point the enclosing FakeQuantize derives from its observer (per-output-channel for the weight), and qmin⁡,qmax⁡q_{\min}, q_{\max} are the grid bounds. The straight-through unit derivative is what keeps the layer differentiable despite the non-differentiable round.

Parameters

qconfig(QConfig, keyword - only)= None
Quantization recipe supplying the weight and activation lucid.quantization.FakeQuantize modules applied during training. Required — constructing the layer without one raises ValueError.
*argsobject= ()
All remaining arguments are forwarded verbatim to the float lucid.nn.Conv3d constructor (in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias), which owns the actual trainable kernel and bias.
**kwargsobject= ()
All remaining arguments are forwarded verbatim to the float lucid.nn.Conv3d constructor (in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias), which owns the actual trainable kernel and bias.

Attributes

weight_fake_quantFakeQuantize
The weight observer + fake-quant built from qconfig.weight(); rounds the float kernel each forward and tracks its range so convert can pick the int8 qparams.
activation_post_processFakeQuantize
The output observer + fake-quant built from qconfig.activation(); calibrates the activation grid whose (scale, zero_point) convert later bakes in.

Notes

  • STE differentiability. round is applied forward but the gradient is passed through as the identity, so the float kernel trains normally.
  • Deferred string padding. Integer / tuple padding is supported; string padding ("same" / "valid") is deferred, matching the quantized conv.
  • Both directions are wired for you. lucid.quantization.prepare_qat swaps the float conv in; lucid.quantization.convert folds this layer out into the matching lucid.nn.quantized.Conv3d. Manual construction is rarely needed and requires an explicit qconfig.
  • Training is slower, not faster. Only the int8 numerics are simulated; the convolution itself still runs in float with two extra fake-quant ops, so the speed / memory win arrives only after convert.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> import lucid.nn.qat as nnqat
>>> model = nn.Sequential(nn.Conv3d(3, 8, 3, padding=1))
>>> qat = Q.prepare_qat(model)              # nn.Conv3d -> qat.Conv3d
>>> isinstance(qat[0], nnqat.Conv3d)
True
>>> loss = (qat(lucid.randn(2, 3, 4, 4, 4)) ** 2).mean()
>>> loss.backward()                         # STE routes grads to the float kernel
>>> _ = qat.eval()
>>> qc = Q.convert(qat)                     # -> quantized.Conv3d (int8 weight)
>>> type(qc[0]).__name__
'Conv3d'
A qconfig is mandatory — constructing the layer directly without one raises:
>>> nnqat.Conv3d(3, 8, 3)
Traceback (most recent call last):
    ...
ValueError: qat conv requires a qconfig

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(args: object = (), qconfig: QConfig | None = None, kwargs: object = {})

Class methods

1

Instance methods

1
fn

forward

→Tensor
forward(x: Tensor)

Apply the convolution to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,Cin,∗)(N, C_{\text{in}}, *).

Returns

Tensor

Output tensor of shape (N,Cout,∗)(N, C_{\text{out}}, *) with spatial dimensions determined by stride, padding, dilation, and kernel size.