Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame^] | 1 | # 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. |
| 16 | |
| 17 | |
| 18 | # Description: |
| 19 | # Contains various scaling calculations for weights, elementwise operations, pooling etc. |
| 20 | |
| 21 | import math |
| 22 | from .numeric_util import round_away_zero |
| 23 | from enum import IntEnum |
| 24 | |
| 25 | |
| 26 | class OperandToScale(IntEnum): |
| 27 | OPa = 1 |
| 28 | OPb = 2 |
| 29 | |
| 30 | |
| 31 | # Quantise floating point scale value into 32-bit int scale and 6-bit shift |
| 32 | def quantise_scale(scale): |
| 33 | significand, exponent = math.frexp(scale) |
| 34 | significand_q31 = int(round_away_zero(significand * (1 << 31))) |
| 35 | exponent_q31 = exponent - 31 |
| 36 | shift = exponent_q31 * -1 |
| 37 | |
| 38 | if shift >= (1 << 6): |
| 39 | # Shift outside of valid range, set scale to 0 |
| 40 | return 0, 16 |
| 41 | |
| 42 | return significand_q31, shift |
| 43 | |
| 44 | |
| 45 | # Calculate global OFM scale for Average Pooling |
| 46 | def quantise_pooling_scale(nr_kernel_elements, rescale_bits=0): |
| 47 | _, k = math.frexp(nr_kernel_elements - 1) |
| 48 | N = 31 - rescale_bits |
| 49 | scale = ((1 << (N + k)) + (1 << k)) // nr_kernel_elements |
| 50 | shift = N + k |
| 51 | |
| 52 | assert shift < (1 << 6) |
| 53 | |
| 54 | return scale, shift |
| 55 | |
| 56 | |
| 57 | # Calculate elementwise Mul OFM scale+shift |
| 58 | def elementwise_mul_scale(input_scale, input2_scale, output_scale): |
| 59 | output_rescale = (input_scale * input2_scale) / output_scale |
| 60 | out_scale, out_shift = quantise_scale(output_rescale) |
| 61 | return out_scale, out_shift |
| 62 | |
| 63 | |
| 64 | # Simplified version of calculating elementwise Add/Sub scales |
| 65 | def simplified_elementwise_add_sub_scale(input1_scale, input2_scale, output_scale, input_shift=16): |
| 66 | max_input_scale = max(input1_scale, input2_scale) |
| 67 | |
| 68 | input1_rescale = input1_scale * (1 << input_shift) / (2 * max_input_scale) |
| 69 | input2_rescale = input2_scale * (1 << input_shift) / (2 * max_input_scale) |
| 70 | output_rescale = (2 * max_input_scale) / (output_scale * (1 << input_shift)) |
| 71 | |
| 72 | out_scale, out_shift = quantise_scale(output_rescale) |
| 73 | |
| 74 | return input1_rescale, input2_rescale, out_scale, out_shift |
| 75 | |
| 76 | |
| 77 | # Advanced version of calculating elementwise Add/Sub scales |
| 78 | def advanced_elementwise_add_sub_scale(input1_scale, input2_scale, output_scale, bitdepth): |
| 79 | # Always scale the smaller of the input scales |
| 80 | max_input_scale = max(input1_scale, input2_scale) |
| 81 | min_input_scale = min(input1_scale, input2_scale) |
| 82 | input_shift = 20 if bitdepth == 8 else 14 |
| 83 | op_to_scale = OperandToScale.OPa if input1_scale < input2_scale else OperandToScale.OPb |
| 84 | |
| 85 | input1_rescale, _, out_scale, out_shift = simplified_elementwise_add_sub_scale( |
| 86 | min_input_scale, max_input_scale, output_scale, input_shift |
| 87 | ) |
| 88 | |
| 89 | in_scale, in_shift = quantise_scale(input1_rescale) |
| 90 | |
| 91 | return in_scale, in_shift, out_scale, out_shift, op_to_scale |