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