-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquantizeToFloat.m
46 lines (37 loc) · 1.48 KB
/
quantizeToFloat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function value = quantizeToFloat(value)
%% quantizeToFloat Quantizes a value and rescales back to floating point
%
% quantizedValue = quantizeToFloat(value) returns a floating point
% value that has been quantized using best precision scaling.
%
% Example:
% quantizedValue = quantizeToFloat(dlarray(single(365.247)))
% Copyright 2023 The Mathworks, Inc.
% Calculate the ideal scaling factor using the input range.
m = extractdata(gather(max(abs(value(:)))));
scalingFactor = double(floor(log2(m)));
% Adjust the scaling factor by 6. 8 bit wordlength - 1
% sign - 1 floor
scalingFactor = scalingFactor - 6;
% Scale the value using the calculated scaling factor.
value = scaleValue(value, scalingFactor);
% Saturate to int8 range.
value = saturateValue(value);
% Round values while bypassing the dlgradient calculation.
value = bypassdlgradients(@round, value);
% Rescale values to single range.
value = rescaleValue(value, scalingFactor);
end
function value = scaleValue(value, scalingFactor)
% Scale the value using the calculated scaling factor.
value = value*single((2^( -1*scalingFactor )));
end
function value = saturateValue(value)
% Saturate to int8 range
value = max(value,-128); % intmin('int8')
value = min(value, 127); % intmax('int8')
end
function value = rescaleValue(value, scalingFactor)
% Rescale values to single range.
value = value*single(2^scalingFactor);
end