Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +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: |
| 17 | # Contains unit tests for encode_weights API for an external consumer |
| 18 | import numpy as np |
| 19 | import pytest |
| 20 | |
| 21 | from ethosu.vela import weight_compressor |
| 22 | from ethosu.vela.architecture_features import Accelerator |
| 23 | |
| 24 | |
| 25 | @pytest.mark.parametrize( |
| 26 | "arch", |
| 27 | [ |
| 28 | Accelerator.Ethos_U55_32, |
| 29 | Accelerator.Ethos_U55_64, |
| 30 | Accelerator.Ethos_U55_128, |
| 31 | Accelerator.Ethos_U55_256, |
| 32 | Accelerator.Yoda_256, |
| 33 | Accelerator.Yoda_512, |
| 34 | ], |
| 35 | ) |
| 36 | @pytest.mark.parametrize("dilation_x", [1, 2]) |
| 37 | @pytest.mark.parametrize("dilation_y", [1, 2]) |
| 38 | @pytest.mark.parametrize("ifm_bitdepth", [8, 16]) |
| 39 | @pytest.mark.parametrize("depth_control", [1, 2, 3]) |
| 40 | @pytest.mark.parametrize("weights_shape_and_block_depth", [((16, 16, 16, 16), 8), ((3, 3, 25, 16), 8)]) |
| 41 | def test_encode_weights( |
| 42 | arch, weights_shape_and_block_depth, dilation_x, dilation_y, ifm_bitdepth, depth_control, |
| 43 | ): |
| 44 | """ |
| 45 | This unit test checks the interface of the API function but not the functionality. |
| 46 | Functional correctness is tested at a system level. |
| 47 | """ |
| 48 | |
| 49 | weights_shape = weights_shape_and_block_depth[0] |
| 50 | ofm_block_depth = weights_shape_and_block_depth[1] |
| 51 | val_max = np.iinfo(np.uint8).max |
| 52 | weights_hwio = np.random.randint(val_max, size=weights_shape, dtype=np.uint8) |
| 53 | weights_ohwi = np.transpose(weights_hwio, (3, 0, 1, 2)) |
| 54 | is_depthwise = True if depth_control == 2 else False |
| 55 | is_partkernel = True if depth_control == 3 else False |
| 56 | dilation_xy = (dilation_x, dilation_y) |
| 57 | |
| 58 | encoded_stream = weight_compressor.encode_weights( |
| 59 | accelerator=arch, |
| 60 | weights_volume=weights_ohwi, |
| 61 | dilation_xy=dilation_xy, |
| 62 | ifm_bitdepth=ifm_bitdepth, |
| 63 | ofm_block_depth=ofm_block_depth, |
| 64 | is_depthwise=is_depthwise, |
| 65 | is_partkernel=is_partkernel, |
| 66 | ) |
| 67 | assert type(encoded_stream) == bytearray |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | # two test candidates for debugging purposes |
| 72 | test_encode_weights(Accelerator.Ethos_U55_256, ((3, 3, 25, 16), 8), 1, 1, 8, 0) |
| 73 | test_encode_weights(Accelerator.Ethos_U55_256, ((16, 16, 16, 16), 8), 1, 1, 8, 0) |