blob: 87c504f46291442e97d6db1f839f2251e2e777c5 [file] [log] [blame]
Manupa Karunaratned83d2e12020-07-20 12:05:32 +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_weights API for an external consumer
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010018import numpy as np
19import pytest
20
Louis Verhaardaeae5672020-11-02 18:04:27 +010021from ethosu.vela.api import npu_encode_weights
22from ethosu.vela.api import NpuAccelerator
Louis Verhaarde8a5a782020-11-02 18:04:27 +010023from ethosu.vela.api import NpuBlockTraversal
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010024
25
26@pytest.mark.parametrize(
Jonas Ohlssond8575072022-03-30 10:30:25 +020027 "arch",
28 list(NpuAccelerator),
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010029)
30@pytest.mark.parametrize("dilation_x", [1, 2])
31@pytest.mark.parametrize("dilation_y", [1, 2])
32@pytest.mark.parametrize("ifm_bitdepth", [8, 16])
33@pytest.mark.parametrize("depth_control", [1, 2, 3])
34@pytest.mark.parametrize("weights_shape_and_block_depth", [((16, 16, 16, 16), 8), ((3, 3, 25, 16), 8)])
35def test_encode_weights(
Jonas Ohlssond8575072022-03-30 10:30:25 +020036 arch,
37 weights_shape_and_block_depth,
38 dilation_x,
39 dilation_y,
40 ifm_bitdepth,
41 depth_control,
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010042):
43 """
44 This unit test checks the interface of the API function but not the functionality.
45 Functional correctness is tested at a system level.
46 """
47
48 weights_shape = weights_shape_and_block_depth[0]
49 ofm_block_depth = weights_shape_and_block_depth[1]
50 val_max = np.iinfo(np.uint8).max
51 weights_hwio = np.random.randint(val_max, size=weights_shape, dtype=np.uint8)
52 weights_ohwi = np.transpose(weights_hwio, (3, 0, 1, 2))
53 is_depthwise = True if depth_control == 2 else False
Louis Verhaarde8a5a782020-11-02 18:04:27 +010054 block_traversal = NpuBlockTraversal.PART_KERNEL_FIRST if depth_control == 3 else NpuBlockTraversal.DEPTH_FIRST
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010055 dilation_xy = (dilation_x, dilation_y)
56
Louis Verhaardaeae5672020-11-02 18:04:27 +010057 encoded_stream = npu_encode_weights(
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010058 accelerator=arch,
59 weights_volume=weights_ohwi,
60 dilation_xy=dilation_xy,
61 ifm_bitdepth=ifm_bitdepth,
62 ofm_block_depth=ofm_block_depth,
63 is_depthwise=is_depthwise,
Louis Verhaarde8a5a782020-11-02 18:04:27 +010064 block_traversal=block_traversal,
Manupa Karunaratned83d2e12020-07-20 12:05:32 +010065 )
66 assert type(encoded_stream) == bytearray