Skip to content
class

Hardswish

extends_QuantizedActivation
Hardswish(scale: Tensor, zero_point: Tensor, qdtype: QDtype = quint8)

Quantized lucid.nn.Hardswish — hardswish(x) then output fake-quant.

The inference-time replacement for a calibrated float nn.Hardswish, installed by lucid.quantization.convert / from_float. Hardswish is the cheap piecewise-linear approximation of x·sigmoid(x) used throughout MobileNetV3 / EfficientNet-lite, chosen precisely because its clamp-and-multiply form quantizes cleanly. Under the sidecar representation (design B) the quantized layer is the float activation followed by a fake-quantize to the calibrated output grid.

Unlike Sigmoid / Tanh, hardswish is unbounded above (it grows like xx for large xx), so its output range is not known a priori — it must be calibrated with a real observer rather than a lucid.quantization.FixedQParamsObserver.

y=fake_quant⁡(hardswish⁡(x)),hardswish⁡(x)=x clamp⁡(x+3, 0, 6)6y = \operatorname{fake\_quant}\bigl(\operatorname{hardswish}(x)\bigr), \qquad \operatorname{hardswish}(x) = x\, \frac{\operatorname{clamp}(x + 3,\ 0,\ 6)}{6}

with fake_quant⁡(t)=(clamp⁡(round⁡(t/S)+Z,qmin⁡,qmax⁡)−Z) S\operatorname{fake\_quant}(t) = (\operatorname{clamp}( \operatorname{round}(t/S) + Z, q_{\min}, q_{\max}) - Z)\, S and the calibrated output (scale, zero_point) S,ZS, Z.

Parameters

scaleTensor
Per-tensor output scale from the calibrating observer. Supplied by from_float; construct directly only with qparams in hand.
zero_pointTensor
Per-tensor output zero-point from the calibrating observer.
qdtypeQDtype= quint8
Quantized dtype bounding the grid. Defaults to lucid.quantization.quint8 ([0, 255]).

Notes

  • Built via convert / from_float, which reads the source module's output observer.
  • Its range is data-dependent, so calibrate through the prepared model before convert — a fixed-qparams grid would clip the unbounded upper tail.
  • The forward is float hardswish → fake-quant; only the output is snapped.

Examples

>>> import lucid, lucid.nn as nn
>>> qhs = nn.quantized.Hardswish(lucid.tensor(0.05), lucid.tensor(64.0))
>>> qhs(lucid.randn(8)).shape
(8,)

See Also

Used by 1

Class methods

1
cls

from_float

→Selffrom _QuantizedActivation
from_float(mod: nn.Module)

Build from a calibrated float activation (reads its output observer).

Instance methods

1
fn

forward

→Tensorfrom _QuantizedActivation
forward(input: Tensor)

Activate, then fake-quantize to the calibrated grid.