blob: 1b9a0e2838c58120f6ec791409e3eaa5ce765282 [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020 Arm Limited and/or its affiliates <open-source-office@arm.com>
Manupa Karunaratnebef228b2020-07-29 18:06:28 +01002#
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 Bolinbc6ee582022-11-04 08:24:29 +000016#
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010017# Description:
Louis Verhaardaeae5672020-11-02 18:04:27 +010018# Contains unit tests for npu_encode_bias API for an external consumer
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010019import random
20
21import numpy as np
22
Louis Verhaardaeae5672020-11-02 18:04:27 +010023from ethosu.vela.api import npu_encode_bias
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010024
25
26def 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 Verhaardaeae5672020-11-02 18:04:27 +010038 biases_enc = npu_encode_bias(bias, scale, shift)
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010039 assert isinstance(biases_enc, bytearray)
40 assert len(biases_enc) == 10