Skip to content
class

MovingAveragePerChannelMinMaxObserver

extendsPerChannelMinMaxObserver
MovingAveragePerChannelMinMaxObserver(ch_axis: int = 0, qscheme: QScheme = per_channel_symmetric, qdtype: QDtype = qint8, averaging_constant: float = 0.01, eps: float = 1e-08)

Per-channel observer with an EMA of the per-channel min / max.

The per-channel analogue of MovingAverageMinMaxObserver, and the outlier-tolerant sibling of PerChannelMinMaxObserver. It keeps an independent range per channel along ch_axis (reducing over every other axis) but updates each channel's [min, max] with an exponential moving average instead of a hard cumulative min/max — so a spike in one calibration batch nudges that channel's range only slightly and is relaxed back by later batches. calculate_qparams still returns a length-C vector of (scale, zero_point).

When to pick it. Use it for weights when weight calibration is noisy (e.g. running statistics observed during QAT rather than on static weights) and you want per-channel tightness and per-batch robustness. For static weights the plain PerChannelMinMaxObserver is usually enough; for activations, use the per-tensor MovingAverageMinMaxObserver or HistogramObserver.

With x(c)x^{(c)} the values of channel cc in the current batch, the first batch seeds each channel directly and every later batch applies the per-channel EMA:

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

where cc is averaging_constant. The reduction to per-channel qparams is the (default symmetric, signed) map inherited from PerChannelMinMaxObserver, pinning each z_c = 0.

Parameters

ch_axisint= 0
Output-channel axis quantized independently (0 for conv / linear weights).
qschemeQScheme= per_channel_symmetric
Target quantization scheme.
qdtypeQDtype= qint8
Target quantized dtype (signed [-128, 127] grid).
averaging_constantfloat= 0.01
EMA weight c∈(0,1]c \in (0, 1] of each new batch's per-channel range; smaller is smoother / slower / more outlier-robust.
epsfloat= 1e-8
Lower floor on each derived per-channel scale.

Attributes

min_valTensor
Per-channel EMA minimum, shape (C,) after the first batch (scalar +inf seed before it).
max_valTensor
Per-channel EMA maximum, shape (C,) after the first batch (scalar -inf seed before it).
averaging_constantfloat
The stored EMA weight cc.

Notes

  • First-batch seeding is explicit. Because the scalar ±inf seed cannot be broadcast into an elementwise where against a length-C batch range, forward special-cases the first observation and copies the per-channel range in directly; the EMA recurrence runs only from the second batch on.
  • Not cumulative — each channel's range can shrink between batches (the whole point of the EMA), unlike PerChannelMinMaxObserver.
  • No autograd history. The scalar-seed → (C,) first write re-registers the buffers and every later EMA update lands in place under lucid.no_grad, per the ObserverBase contract.
  • Symmetric vs affine behaves as in the per-channel base class; the default pins each zero_point to 0.

Examples

>>> import lucid
>>> import lucid.quantization as Q
>>> obs = Q.MovingAveragePerChannelMinMaxObserver(ch_axis=0, averaging_constant=0.1)
>>> for _ in range(8):
...     _ = obs(lucid.randn(12, 32))         # 12 output channels
>>> scale, zero_point = obs.calculate_qparams()
>>> scale.shape
(12,)
>>> bool((zero_point == 0).all().item())     # per-channel symmetric ⇒ z = 0
True

See Also

Used by 1

Constructors

1
dunder

__init__

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

Instance methods

1
fn

forward

→Tensor
forward(x: Tensor)

EMA-update the per-channel min/max (seeded on the first batch).