Skip to main content

Module calibration

Module calibration 

Source
Expand description

Host-side calibration: turning float scales into the integer parameters the int8 layers use (C++ quantizeMultiplier, buildRequantizer, quantize, dequantize, buildQSigmoidLUT, buildQTanhLUT, buildQSoftmaxFixedPointParams, computeAffineParamsAsymmetric, computeAffineParamsSymmetric, computePerChannelSymmetricScales, the RangeObserver, buildQAddParams, buildRescaler, buildQMulRequantizer, buildQLSTMParams, buildQGRUParams, quantizeQLSTMBiases, quantizeQGRUBiases, buildQFFTTwiddles).

Needs the std feature, for exp, tanh, cos and sin; tinymind (C++) gates the same functions on TINYMIND_ENABLE_FLOAT && TINYMIND_ENABLE_STD. On a device, run these on the host and embed the results as constants. The float arithmetic follows the C++ step for step, so the tables and requantizers match the C++ ones on the same platform: exp and tanh come from the system math library in both.

Structs§

GruParams
The multipliers and shifts of a QGruCell (C++ QGRUParams), with the same field names as the cell.
GruScales
The float scales a QGruCell is calibrated from (C++ QGRUScales). Gates are in the order r, z, n.
LstmParams
The multipliers and shifts of a QLstmCell (C++ QLSTMParams), with the same field names as the cell. output_multiplier and output_shift go in the cell’s output_requantizer, with the hidden zero point and bounds.
LstmScales
The float scales a QLstmCell is calibrated from (C++ QLSTMScales). Gates are in the order i, f, g, o.
RangeObserver
The running minimum and maximum of the values it has observed, for fitting an activation’s scale from calibration inputs (C++ RangeObserver). Before any observation the range is [0, 0].

Constants§

SIGMOID_LUT_OUTPUT_SCALE
Output scale of the sigmoid LUTs the recurrent cells read: 1/256, with zero point -128 (C++ kQLstmSigmoidOutputScale). Build them with build_sigmoid_lut(lut_input_scale, 0, SIGMOID_LUT_OUTPUT_SCALE, -128).
TANH_LUT_OUTPUT_SCALE
Output scale of the tanh LUTs the recurrent cells read: 1/128, with zero point 0 (C++ kQLstmTanhOutputScale). Build them with build_tanh_lut(lut_input_scale, 0, TANH_LUT_OUTPUT_SCALE, 0).

Functions§

affine_params_asymmetric
Asymmetric per-tensor affine parameters (scale, zero_point) that map the real range [fmin, fmax], widened to include 0, onto [qmin, qmax] (C++ computeAffineParamsAsymmetric). An empty range gives (1.0, qmin).
affine_params_symmetric
Symmetric affine parameters (scale, 0) covering [fmin, fmax]: the scale maps the larger magnitude to qmax (C++ computeAffineParamsSymmetric). A range of zeros, or a qmax of 0 or below, gives scale 1.0.
build_add
Build a QAdd for inputs (scale, zero point) input_a and input_b and output output (C++ buildQAddParams with its default left shift of 20, TFLite’s). Both inputs are rescaled to twice the larger input scale; ratios are formed in f64 from the f32 scales, as the C++ does. Zero points and bounds are narrowed to i8; raise qmin to the output zero point to fuse a ReLU.
build_fft_twiddles
The Q1.15 twiddle tables of a QFft1D<N, HALF>: cos(-2πk/N) and sin(-2πk/N) for k in 0..N/2, times 32768, rounded to nearest (ties away from zero) and clamped to i16 (C++ buildQFFTTwiddles). Returns (cos, sin).
build_gru_params
Build a QGruCell’s multipliers and shifts from its float scales (C++ buildQGRUParams). Use the LUTs and biases as for build_lstm_params.
build_lstm_params
Build a QLstmCell’s multipliers and shifts from its float scales (C++ buildQLSTMParams). The ratios are formed in f64 from the f32 scales, as the C++ does.
build_mul_requantizer
The requantizer of a QMul: input_a_scale * input_b_scale / output_scale, in f64 from the f32 scales (C++ buildQMulRequantizer; the same ratio as build_requantizer with the second input’s scale as the weight scale).
build_requantizer
Build a Requantizer for (input_scale * weight_scale) / output_scale (C++ buildRequantizer<int8_t>). The zero point and bounds are narrowed to i8 as the C++ static_cast does.
build_rescaler
A requantizer for a pure rescale, input_scale / output_scale, with no weight: for QConcat inputs and anything else that copies values from one grid to another (C++ buildRescaler, which is buildRequantizer with a weight scale of 1).
build_sigmoid_lut
Sigmoid lookup table, 1 / (1 + exp(-x)) (C++ buildQSigmoidLUT). An output grid of scale 1/256 and zero point -128 uses the full int8 range.
build_softmax
QSoftmax parameters for an input tensor of scale input_scale and a softmax of beta (1.0 for a plain softmax), computed as TFLite Micro does for an int8 softmax (PreprocessSoftmaxScaling with 5 integer bits, then CalculateInputRadius; C++ buildQSoftmaxFixedPointParams).
build_tanh_lut
Tanh lookup table (C++ buildQTanhLUT). An output grid of scale 1/128 and zero point 0 uses the full int8 range.
dequantize
The real value an int8 stands for: scale * (q - zero_point) (C++ dequantize<int8_t>).
per_channel_symmetric_scales
One symmetric weight scale per output channel, max |w| / qmax over each channel’s weights (C++ computePerChannelSymmetricScales), as TFLite quantizes depthwise and convolution weights. A channel of zeros, or a qmax of 0 or below, gets scale 1.0.
quantize
Quantize a real value to int8: round(x / scale) + zero_point, ties away from zero (as TFLite), saturated to [qmin, qmax] (C++ quantize<int8_t>).
quantize_gate_biases
Quantize a recurrent cell’s float biases, biases[gate][unit], into the LUT input grid: round(b / lut_input_scale) in f64, ties away from zero (C++ quantizeQLSTMBiases with G = 4, quantizeQGRUBiases with G = 3). A value beyond the i32 range wraps, as the C++ conversion from long does.
quantize_multiplier
Decompose a positive real scale ratio into a Q0.31 multiplier and a shift (C++ quantizeMultiplier). Returns (multiplier, shift): shift < 0 for a ratio above 1, shift > 0 below 1. A zero ratio gives (0, 0).