Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: Copyright 2020 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [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 | # |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 17 | # Description: |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 18 | # Contains unit tests for npu_encode_bias API for an external consumer |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 19 | import random |
| 20 | |
| 21 | import numpy as np |
| 22 | |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 23 | from ethosu.vela.api import npu_encode_bias |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def test_encode_bias(): |
| 27 | bias_lower_limit = -(1 << (40 - 1)) |
| 28 | bias_upper_limit = (1 << (40 - 1)) - 1 |
| 29 | scale_lower_limit = 0 |
| 30 | scale_upper_limit = (1 << 32) - 1 |
| 31 | shift_lower_limit = 0 |
| 32 | shift_upper_limit = (1 << 6) - 1 |
| 33 | |
| 34 | for _ in range(30): |
| 35 | bias = np.int64(random.randint(bias_lower_limit, bias_upper_limit)) |
| 36 | scale = int(random.randint(scale_lower_limit, scale_upper_limit)) |
| 37 | shift = int(random.randint(shift_lower_limit, shift_upper_limit)) |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 38 | biases_enc = npu_encode_bias(bias, scale, shift) |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 39 | assert isinstance(biases_enc, bytearray) |
| 40 | assert len(biases_enc) == 10 |