blob: fcf054e0e619c7e4b7eb377e4e5848f927ae7c9f [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Contains various scaling calculations for weights, elementwise operations, pooling etc.
Tim Hall79d07d22020-04-27 18:20:16 +010018import math
Tim Hall79d07d22020-04-27 18:20:16 +010019from enum import IntEnum
20
Diego Russoea6111a2020-04-14 18:41:58 +010021from .numeric_util import round_away_zero
22
Tim Hall79d07d22020-04-27 18:20:16 +010023
24class OperandToScale(IntEnum):
25 OPa = 1
26 OPb = 2
27
28
29# Quantise floating point scale value into 32-bit int scale and 6-bit shift
30def quantise_scale(scale):
31 significand, exponent = math.frexp(scale)
32 significand_q31 = int(round_away_zero(significand * (1 << 31)))
33 exponent_q31 = exponent - 31
34 shift = exponent_q31 * -1
35
Jacob Bohlin1cdc4672020-08-20 15:51:37 +020036 if not (0 <= shift < (1 << 6)):
Tim Hall79d07d22020-04-27 18:20:16 +010037 # Shift outside of valid range, set scale to 0
38 return 0, 16
39
40 return significand_q31, shift
41
42
Fredrik Svedbergd67c0aa2020-03-30 13:15:28 +020043# Reduced precision quantization for int16
44def reduced_quantise_scale(scale):
45 multiplier, shift = quantise_scale(scale)
46 reduced_multiplier = int((multiplier + (1 << 15)) >> 16)
47 reduced_shift = shift - 16
48
49 return reduced_multiplier, reduced_shift
50
51
Tim Hall79d07d22020-04-27 18:20:16 +010052# Calculate global OFM scale for Average Pooling
53def quantise_pooling_scale(nr_kernel_elements, rescale_bits=0):
54 _, k = math.frexp(nr_kernel_elements - 1)
55 N = 31 - rescale_bits
56 scale = ((1 << (N + k)) + (1 << k)) // nr_kernel_elements
57 shift = N + k
58
59 assert shift < (1 << 6)
60
61 return scale, shift
62
63
64# Calculate elementwise Mul OFM scale+shift
65def elementwise_mul_scale(input_scale, input2_scale, output_scale):
66 output_rescale = (input_scale * input2_scale) / output_scale
67 out_scale, out_shift = quantise_scale(output_rescale)
68 return out_scale, out_shift
69
70
71# Simplified version of calculating elementwise Add/Sub scales
72def simplified_elementwise_add_sub_scale(input1_scale, input2_scale, output_scale, input_shift=16):
73 max_input_scale = max(input1_scale, input2_scale)
74
75 input1_rescale = input1_scale * (1 << input_shift) / (2 * max_input_scale)
76 input2_rescale = input2_scale * (1 << input_shift) / (2 * max_input_scale)
77 output_rescale = (2 * max_input_scale) / (output_scale * (1 << input_shift))
78
79 out_scale, out_shift = quantise_scale(output_rescale)
80
81 return input1_rescale, input2_rescale, out_scale, out_shift
82
83
84# Advanced version of calculating elementwise Add/Sub scales
85def advanced_elementwise_add_sub_scale(input1_scale, input2_scale, output_scale, bitdepth):
86 # Always scale the smaller of the input scales
87 max_input_scale = max(input1_scale, input2_scale)
88 min_input_scale = min(input1_scale, input2_scale)
Fredrik Svedbergc91dd1c2020-05-04 15:40:04 +020089 input_shift = 20 if bitdepth == 8 else 15
Tim Hall79d07d22020-04-27 18:20:16 +010090 op_to_scale = OperandToScale.OPa if input1_scale < input2_scale else OperandToScale.OPb
91
92 input1_rescale, _, out_scale, out_shift = simplified_elementwise_add_sub_scale(
93 min_input_scale, max_input_scale, output_scale, input_shift
94 )
95
96 in_scale, in_shift = quantise_scale(input1_rescale)
97
98 return in_scale, in_shift, out_scale, out_shift, op_to_scale