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