blob: c0a4a9abd43febe4997b32e014dda707764f73be [file] [log] [blame]
Manupa Karunaratnebef228b2020-07-29 18:06:28 +01001# 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 Verhaardaeae5672020-11-02 18:04:27 +010017# Contains unit tests for npu_encode_bias API for an external consumer
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010018import random
19
20import numpy as np
21
Louis Verhaardaeae5672020-11-02 18:04:27 +010022from ethosu.vela.api import npu_encode_bias
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010023
24
25def 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 Verhaardaeae5672020-11-02 18:04:27 +010037 biases_enc = npu_encode_bias(bias, scale, shift)
Manupa Karunaratnebef228b2020-07-29 18:06:28 +010038 assert isinstance(biases_enc, bytearray)
39 assert len(biases_enc) == 10