Skip to content
class

PerChannelMinMaxObserver

extendsObserverBase
PerChannelMinMaxObserver(ch_axis: int = 0, qscheme: QScheme = per_channel_symmetric, qdtype: QDtype = qint8, eps: float = 1e-08)

Per-channel observer tracking a running min / max along ch_axis.

The standard weight observer. Rather than one range for the whole tensor, it keeps an independent [min, max] for every slice along ch_axis — reducing over all other axes — so calculate_qparams returns a length-C vector of (scale, zero_point), one per output channel. Because different output neurons / filters routinely span wildly different dynamic ranges, a single per-tensor scale is dominated by the widest channel and quantizes the rest coarsely; a per-channel scale tracks each one tightly, which is why per-channel symmetric qint8 is the standard accuracy-preserving choice for convolution and linear weights.

When to pick it. Use it for weights — it is the weight observer in both lucid.quantization.get_default_qconfig (static PTQ) and lucid.quantization.get_default_qat_qconfig (QAT, wrapped in FakeQuantize). It is not used for activations, whose channel layout varies per op and which are cheaper to quantize per-tensor. For a smoothed per-channel range over noisy weight calibration, use MovingAveragePerChannelMinMaxObserver.

Writing the input with ch_axis moved to the front and the rest flattened, x(c)x^{(c)} denotes the values of channel cc; each channel keeps its own cumulative range:

min_valc←min⁡ ⁣(min_valc, min⁡x(c)),max_valc←max⁡ ⁣(max_valc, max⁡x(c))\text{min\_val}_c \leftarrow \min\!\bigl(\text{min\_val}_c,\ \min x^{(c)}\bigr), \qquad \text{max\_val}_c \leftarrow \max\!\bigl(\text{max\_val}_c,\ \max x^{(c)}\bigr)

then the (default symmetric, signed) reduction pins zero_point to 0 and scales each channel by its own peak magnitude:

sc=max⁡(∣min_valc∣, ∣max_valc∣)(qmax⁡−qmin⁡)/2,zc=0s_c = \frac{\max(|\text{min\_val}_c|,\ |\text{max\_val}_c|)} {(q_{\max} - q_{\min})/2},\qquad z_c = 0

(an affine qscheme instead yields per-channel (s_c, z_c) via the base-class affine map).

Parameters

ch_axisint= 0
Axis whose entries are quantized independently — the output-channel axis, which is 0 for both conv weights (out, in, kh, kw) and linear weights (out, in).
qschemeQScheme= per_channel_symmetric
Target quantization scheme; the per-channel default pins zero_point per channel.
qdtypeQDtype= qint8
Target quantized dtype; qint8 gives the signed grid [-128, 127].
epsfloat= 1e-8
Lower floor on each derived per-channel scale.

Attributes

min_valTensor
Per-channel running minimum, shape (C,) after the first batch (scalar +inf seed before it).
max_valTensor
Per-channel running maximum, shape (C,) after the first batch (scalar -inf seed before it).

Notes

  • Scalar seed → per-channel vector. The buffers start as scalar ±inf; the first forward broadcasts that seed against the length-C batch range, so min_val / max_val acquire shape (C,) on the first observation; that one write re-registers the buffers, and every later one lands in place.
  • Cumulative per channel — like MinMaxObserver, the ranges only widen, so the observer is outlier-sensitive; smooth with the moving-average variant.
  • Symmetric vs affine. The default per-channel-symmetric scheme fixes zero_point = 0 (signed weights map 0 → 0 exactly); an affine scheme gives each channel a free zero-point.
  • Reduction axes. Every axis except ch_axis is reduced, so the number of qparams equals x.shape[ch_axis].
  • Weight observer of both default QConfigs (lucid.quantization.get_default_qconfig and lucid.quantization.get_default_qat_qconfig).

Examples

>>> import lucid
>>> import lucid.quantization as Q
>>> obs = Q.PerChannelMinMaxObserver(ch_axis=0)
>>> _ = obs(lucid.randn(16, 8))              # 16 output channels
>>> scale, zero_point = obs.calculate_qparams()
>>> scale.shape                              # one scale per output channel
(16,)
>>> bool((zero_point == 0).all().item())     # symmetric ⇒ zero_point pinned to 0
True
Per-channel is far tighter than per-tensor when channels differ in range — here one
row is ~1000x larger, yet each row keeps its own scale:
>>> import lucid
>>> w = lucid.stack([lucid.ones(4) * 0.001, lucid.ones(4) * 1.0])
>>> obs = Q.PerChannelMinMaxObserver(ch_axis=0)
>>> _ = obs(w)
>>> s, _ = obs.calculate_qparams()
>>> bool(s[0].item() < s[1].item())          # small-range row ⇒ smaller step
True

See Also

Used by 8

Constructors

1
dunder

__init__

→None
__init__(ch_axis: int = 0, qscheme: QScheme = per_channel_symmetric, qdtype: QDtype = qint8, eps: float = 1e-08)

Instance methods

2
fn

calculate_qparams

→tuple[Tensor, Tensor]
calculate_qparams()

Return (scale, zero_point) from the accumulated statistics.

fn

forward

→Tensor
forward(x: Tensor)

Fold per-channel min/max (reduced over all non-channel axes).