Skip to content
class

QuantWrapper

extendsModule
QuantWrapper(module: nn.Module)

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.Module
The float model to wrap. It is stored verbatim as self.module and is itself quantized in place when the wrapper is prepared and converted.

Attributes

The entry-side boundary marker inserted before module.
modulenn.Module
The wrapped float (later quantized) model.
The exit-side boundary marker inserted after module.

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) before prepare can attach observers.
  • Prefer explicit in-model QuantStub / DeQuantStub placement 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

Used by 1

Constructors

1
dunder

__init__

→None
__init__(module: nn.Module)

Instance methods

1
fn

forward

→Tensor
forward(x: Tensor)

Quantize the input, run the wrapped model, dequantize the output.