Skip to content
class

Tanh

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

Implementing kernel

C++ engine symbols that back this Python API.

Quantized lucid.nn.Tanh — tanh(x) then output fake-quant.

The inference-time replacement for a calibrated float nn.Tanh, installed by lucid.quantization.convert / from_float. Under the sidecar representation (design B) the quantized layer is the float tanh followed by a fake-quantize to the calibrated output grid, so a standalone tanh inside a quantized region is requantized rather than left at full precision.

Because tanh⁡(x)∈[−1,1]\tanh(x) \in [-1, 1] the range is known a priori, making this another fixed-qparams activation. Since the range is signed, the fixed grid centres the zero-point in the middle of the quint8 codes so that both the -1 and +1 extremes are representable.

y=fake_quant⁡(tanh⁡(x)),S=2256,Z=128y = \operatorname{fake\_quant}\bigl(\tanh(x)\bigr), \qquad S = \tfrac{2}{256},\quad Z = 128

so codes 0 .. 255 tile [−1,1][-1, 1] with the real value 0 landing on code 128. 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.

Parameters

scaleTensor
Per-tensor output scale — the fixed 2/256 under a lucid.quantization.FixedQParamsObserver, else calibrated. Supplied by from_float.
zero_pointTensor
Per-tensor output zero-point (128 for the signed [-1, 1] grid).
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.
  • Bounded to [-1, 1]; pairing with lucid.quantization.FixedQParamsObserver avoids a calibration pass.
  • The non-zero zero-point (128) is what lets a signed range live on the unsigned quint8 grid without clipping either sign.

Examples

>>> import lucid, lucid.nn as nn
>>> qtanh = nn.quantized.Tanh(lucid.tensor(2 / 256), lucid.tensor(128.0))
>>> y = qtanh(lucid.randn(8))
>>> bool(((y >= -1) & (y <= 1)).all().item())    # bounded to [-1, 1]
True

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.