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
QGruCellis calibrated from (C++QGRUScales). Gates are in the order r, z, n. - Lstm
Params - The multipliers and shifts of a
QLstmCell(C++QLSTMParams), with the same field names as the cell.output_multiplierandoutput_shiftgo in the cell’soutput_requantizer, with the hidden zero point and bounds. - Lstm
Scales - The float scales a
QLstmCellis calibrated from (C++QLSTMScales). Gates are in the order i, f, g, o. - Range
Observer - 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 withbuild_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 withbuild_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 toqmax(C++computeAffineParamsSymmetric). A range of zeros, or aqmaxof 0 or below, gives scale 1.0. - build_
add - Build a
QAddfor inputs(scale, zero point)input_aandinput_band outputoutput(C++buildQAddParamswith its default left shift of 20, TFLite’s). Both inputs are rescaled to twice the larger input scale; ratios are formed inf64from thef32scales, as the C++ does. Zero points and bounds are narrowed toi8; raiseqminto 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)andsin(-2πk/N)forkin0..N/2, times 32768, rounded to nearest (ties away from zero) and clamped toi16(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 forbuild_lstm_params. - build_
lstm_ params - Build a
QLstmCell’s multipliers and shifts from its float scales (C++buildQLSTMParams). The ratios are formed inf64from thef32scales, as the C++ does. - build_
mul_ requantizer - The requantizer of a
QMul:input_a_scale * input_b_scale / output_scale, inf64from thef32scales (C++buildQMulRequantizer; the same ratio asbuild_requantizerwith the second input’s scale as the weight scale). - build_
requantizer - Build a
Requantizerfor(input_scale * weight_scale) / output_scale(C++buildRequantizer<int8_t>). The zero point and bounds are narrowed toi8as the C++static_castdoes. - build_
rescaler - A requantizer for a pure rescale,
input_scale / output_scale, with no weight: forQConcatinputs and anything else that copies values from one grid to another (C++buildRescaler, which isbuildRequantizerwith 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 QSoftmaxparameters for an input tensor of scaleinput_scaleand a softmax ofbeta(1.0 for a plain softmax), computed as TFLite Micro does for an int8 softmax (PreprocessSoftmaxScalingwith 5 integer bits, thenCalculateInputRadius; 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| / qmaxover each channel’s weights (C++computePerChannelSymmetricScales), as TFLite quantizes depthwise and convolution weights. A channel of zeros, or aqmaxof 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)inf64, ties away from zero (C++quantizeQLSTMBiaseswithG= 4,quantizeQGRUBiaseswithG= 3). A value beyond thei32range wraps, as the C++ conversion fromlongdoes. - quantize_
multiplier - Decompose a positive real scale ratio into a Q0.31 multiplier and a shift
(C++
quantizeMultiplier). Returns(multiplier, shift):shift < 0for a ratio above 1,shift > 0below 1. A zero ratio gives(0, 0).