Fredrik Svedberg | 6ef0230 | 2022-09-29 13:29:37 +0200 | [diff] [blame] | 1 | # Copyright (C) 2022 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 | # Description: |
| 17 | # Unit tests for scaling |
| 18 | from ethosu.vela.scaling import quantise_scale |
| 19 | from ethosu.vela.scaling import reduced_quantise_scale |
| 20 | |
| 21 | |
| 22 | def test_scaling(): |
| 23 | multiplier, shift = quantise_scale(1) |
| 24 | assert multiplier == 1073741824 and shift == 30 |
| 25 | multiplier, shift = quantise_scale(0.5) |
| 26 | assert multiplier == 1073741824 and shift == 31 |
| 27 | multiplier, shift = quantise_scale(0.001) |
| 28 | assert multiplier == 1099511628 and shift == 40 |
| 29 | multiplier, shift = quantise_scale(0.008) |
| 30 | assert multiplier == 1099511628 and shift == 37 |
| 31 | multiplier, shift = quantise_scale(0.00097652) |
| 32 | assert multiplier == 2147390190 and shift == 41 |
| 33 | multiplier, shift = quantise_scale(0.0009765615959827986) |
| 34 | assert multiplier == 2147481660 and shift == 41 |
| 35 | |
| 36 | |
| 37 | def test_reduced_scaling(): |
| 38 | multiplier, shift = reduced_quantise_scale(1) |
| 39 | assert multiplier == 16384 and shift == 14 |
| 40 | multiplier, shift = reduced_quantise_scale(0.5) |
| 41 | assert multiplier == 16384 and shift == 15 |
| 42 | multiplier, shift = reduced_quantise_scale(0.001) |
| 43 | assert multiplier == 16777 and shift == 24 |
| 44 | multiplier, shift = reduced_quantise_scale(0.008) |
| 45 | assert multiplier == 16777 and shift == 21 |
| 46 | multiplier, shift = reduced_quantise_scale(0.00097652) |
| 47 | assert multiplier == 32767 and shift == 25 |
| 48 | # multiplier saturated |
| 49 | multiplier, shift = reduced_quantise_scale(0.0009765615959827986) |
| 50 | assert multiplier == 32767 and shift == 25 |