Skip to content
fn

prepare_qat

→nn.Module
prepare_qat(model: nn.Module, qconfig: QConfig | QConfigMapping | None = None, inplace: bool = False)

Insert fake-quant modules for quantization-aware training.

prepare_qat is the entry point of the QAT workflow — prepare_qat → fine-tune → convert. Where static PTQ only observes a frozen float model, QAT makes the quantization grid part of the forward pass: this swaps every weighted layer for its lucid.nn.qat counterpart (which fake-quantizes both its weight and its output) and attaches a fake-quant to every QuantStub. During the subsequent fine-tuning the network feels the int8 grid on each step — via the straight-through estimator of fake_quantize — and learns weights that minimize the rounding error, typically recovering most of the accuracy a low-bit model would otherwise lose. Reach for it when plain static PTQ drops too much accuracy (aggressive bit widths, quantization-sensitive architectures).

Unlike prepare, the returned model is left in train mode: BN keeps updating its running stats and the fake-quant grids track the evolving ranges. Fuse Conv/BN with fuse_modules_qat first so the folded ConvBn* modules stay trainable and fold BN per forward under the STE. After fine-tuning, convert maps the nn.qat layers to the same int8 inference layers a static-PTQ convert produces.

Parameters

Float model with QuantStub / DeQuantStub boundaries (and, ideally, Conv/BN runs already fused for QAT).
qconfigQConfig or QConfigMapping= None
The QAT recipe — a single QConfig is promoted to a global mapping, and None (default) falls back to get_default_qat_qconfig_mapping (fused weight + activation fake-quant).
inplacebool= False
If True mutate and return model itself; if False (default) prepare a copy.deepcopy and leave the original untouched.

Returns

nn.Module

The QAT-ready model in train mode. Fine-tune it, then call convert to obtain the int8 inference model.

Notes

  • Returned in train mode (contrast prepare, which returns eval) — fake-quant and BN both need training-time behavior.
  • Weighted layers become trainable fake-quant modules; their float weights are kept and updated by the optimizer (nothing is baked to int8 until convert).
  • Fuse with fuse_modules_qat (not fuse_modules) beforehand: the eval-time BN fold would freeze BN, which QAT needs to keep learning.
  • Non-destructive by default (deep-copy).
  • A module's own .qconfig attribute picks its recipe (None keeps it and its subtree float) exactly as in prepare.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> model = nn.Sequential(nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 10))
>>> model.qconfig = Q.get_default_qat_qconfig()
>>> qat = Q.prepare_qat(model)              # fake-quant inserted, train mode
>>> qat.training
True
>>> # ... fine-tune qat for a few epochs with real labels ...
>>> qmodel = Q.convert(qat.eval())          # bake into the int8 inference model

See Also

Used by 1