Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: Copyright 2020, 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 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. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 16 | # |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 17 | # Description: |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 18 | # Contains unit tests for npu_encode_weights API for an external consumer |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 19 | import numpy as np |
| 20 | import pytest |
| 21 | |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 22 | from ethosu.vela.api import npu_encode_weights |
| 23 | from ethosu.vela.api import NpuAccelerator |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 24 | from ethosu.vela.api import NpuBlockTraversal |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 25 | |
| 26 | |
| 27 | @pytest.mark.parametrize( |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 28 | "arch", |
| 29 | list(NpuAccelerator), |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 30 | ) |
| 31 | @pytest.mark.parametrize("dilation_x", [1, 2]) |
| 32 | @pytest.mark.parametrize("dilation_y", [1, 2]) |
| 33 | @pytest.mark.parametrize("ifm_bitdepth", [8, 16]) |
| 34 | @pytest.mark.parametrize("depth_control", [1, 2, 3]) |
| 35 | @pytest.mark.parametrize("weights_shape_and_block_depth", [((16, 16, 16, 16), 8), ((3, 3, 25, 16), 8)]) |
| 36 | def test_encode_weights( |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 37 | arch, |
| 38 | weights_shape_and_block_depth, |
| 39 | dilation_x, |
| 40 | dilation_y, |
| 41 | ifm_bitdepth, |
| 42 | depth_control, |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 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 |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 55 | block_traversal = NpuBlockTraversal.PART_KERNEL_FIRST if depth_control == 3 else NpuBlockTraversal.DEPTH_FIRST |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 56 | dilation_xy = (dilation_x, dilation_y) |
| 57 | |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 58 | encoded_stream = npu_encode_weights( |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 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, |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 65 | block_traversal=block_traversal, |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 66 | ) |
| 67 | assert type(encoded_stream) == bytearray |