Skip to content
class

FixedQParamsObserver

extendsObserverBase
FixedQParamsObserver(scale: float, zero_point: int, qscheme: QScheme = per_tensor_affine, qdtype: QDtype = quint8, eps: float = 1e-08)

Observer with a fixed (scale, zero_point) — no statistics collected.

For ops whose output range is known a priori, calibration is pointless: the grid can be fixed once and for all. The canonical cases are bounded activations — sigmoid → [0, 1], softmax → [0, 1], tanh → [-1, 1] — where the ideal (scale, zero_point) is a closed-form function of the dtype grid, not something the data can improve on. FixedQParamsObserver carries that fixed pair, makes forward a pure identity (no statistics gathered), and returns the supplied qparams verbatim from calculate_qparams.

When to pick it. Use it on the output of a range-bounded op so calibration does not waste effort (or worse, under-shoot the true bound) estimating a range you already know. For a sigmoid/softmax output on the unsigned quint8 grid [0, 255] the natural choice is scale = 1/256, zero_point = 0 (mapping [0, 1] onto the full grid); a tanh output [-1, 1] uses scale = 2/256 with a mid-grid zero_point. For every unbounded tensor, use a statistics-collecting observer (MinMaxObserver, HistogramObserver, …) instead.

There is no statistic and no reduction — the qparams are the constants themselves:

(scale, zero_point)=(sfixed, zfixed),x^=(clip⁡(round⁡(x/sfixed)+zfixed, qmin⁡, qmax⁡)−zfixed) sfixed(\text{scale},\ \text{zero\_point}) = (s_{\text{fixed}},\ z_{\text{fixed}}), \qquad \hat{x} = \bigl(\operatorname{clip}(\operatorname{round}(x/s_{\text{fixed}}) + z_{\text{fixed}},\ q_{\min},\ q_{\max}) - z_{\text{fixed}}\bigr)\, s_{\text{fixed}}

independent of any observed data.

Parameters

scalefloat
The fixed quantization step size sfixeds_{\text{fixed}}.
zero_pointint
The fixed integer code that real value 0 maps to.
qschemeQScheme= per_tensor_affine
Target quantization scheme — metadata only; the qparams are fixed regardless.
qdtypeQDtype= quint8
Target quantized dtype recorded for downstream consumers (grid bounds).
epsfloat= 1e-8
Retained for interface symmetry with ObserverBase; unused because the scale is fixed (never derived, so never floored).

Attributes

scaleTensor
Scalar (shape ()) fixed quantization step.
zero_pointTensor
Scalar (shape ()) fixed zero-point (stored as an integer-valued float).

Notes

  • Identity forward. forward returns x unchanged and collects nothing — the two buffers are set once at construction and never move.
  • No calibration needed. calculate_qparams never raises (unlike PlaceholderObserver); it simply returns the stored (scale, zero_point), so it is valid to call immediately after construction with no forward passes.
  • Buffers, not stats. scale / zero_point are registered buffers, so they persist through state_dict and move with .to(device) like any other buffer.
  • Metadata fields. qscheme / eps are stored for interface symmetry but do not affect the returned qparams.

Examples

A fixed observer for a sigmoid output on the quint8 grid — the [0, 1]
range maps onto [0, 255] with scale = 1/256:
>>> import lucid.quantization as Q
>>> obs = Q.FixedQParamsObserver(scale=1.0 / 256, zero_point=0)
>>> scale, zero_point = obs.calculate_qparams()   # valid with no calibration
>>> round(scale.item(), 6), int(zero_point.item())
(0.003906, 0)
The forward is a pure identity — feeding data changes nothing:
>>> import lucid
>>> before = obs.scale.item()
>>> _ = obs(lucid.randn(4, 4))
>>> obs.scale.item() == before
True

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(scale: float, zero_point: int, qscheme: QScheme = per_tensor_affine, qdtype: QDtype = quint8, 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)

Identity — the qparams are fixed, so no statistics are gathered.