Wrap an arbitrary float model with QuantStub / DeQuantStub.
A convenience container that bolts a quantization boundary pair onto a model
that was never written with quantization in mind (e.g. a stock zoo model
with no QuantStub of its own). The wrapper inserts a QuantStub
before the model and a DeQuantStub after it, so prepare can
attach an entry-side activation observer and convert can install a
runtime Quantize at the boundary — all without editing the wrapped
module's forward.
The wrapped module is stored as the module submodule, flanked by
quant and dequant. Each forward runs quant → module → dequant:
the input is quantized on entry, the model runs (its own weighted layers get
swapped to their quantized forms during convert), and the output passes
through the identity dequant marker on the way out.
Parameters
modulenn.Moduleself.module and is
itself quantized in place when the wrapper is prepared and converted.Attributes
quantQuantStubmodule.modulenn.ModuledequantDeQuantStubmodule.Notes
- Only the entry boundary is numerically active after
convert— the dequant side is an identity under the sidecar representation (design B). - The wrapped model still needs a
qconfig(set it on the wrapper or the inner module) beforepreparecan attach observers. - Prefer explicit in-model
QuantStub/DeQuantStubplacement when you want the boundary somewhere other than the model's outermost input/output.
Examples
>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> net = nn.Sequential(nn.Linear(16, 8))
>>> wrapped = nn.quantized.QuantWrapper(net)
>>> wrapped.qconfig = Q.get_default_qconfig()
>>> prepared = Q.prepare(wrapped)
>>> _ = prepared(lucid.randn(4, 16)) # calibrate
>>> qmodel = Q.convert(prepared)
>>> type(qmodel.quant).__name__ # stub swapped to runtime form
'Quantize'See Also
- lucid.nn.quantized.QuantStub—The entry marker it inserts.
- lucid.nn.quantized.DeQuantStub—The exit marker it inserts.
- lucid.quantization.prepare—Attaches observers to the wrapped boundaries.
- lucid.quantization.convert—Swaps the stubs for their runtime forms.