blob: 59b85873185ed791cb220b2e50df9161773d8c88 [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:
17# Contains unit tests for encode_biases API for an external consumer
18import random
19
20import numpy as np
21
22from ethosu.vela.weight_compressor import encode_bias
23
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))
37 biases_enc = encode_bias(bias, scale, shift)
38 assert isinstance(biases_enc, bytearray)
39 assert len(biases_enc) == 10
40
41
42if __name__ == "__main__":
43 test_encode_bias()