aboutsummaryrefslogtreecommitdiff
path: root/ethosu/vela/test/extapi/test_extapi_encode_weights.py
blob: 87c504f46291442e97d6db1f839f2251e2e777c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the License); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Description:
# Contains unit tests for npu_encode_weights API for an external consumer
import numpy as np
import pytest

from ethosu.vela.api import npu_encode_weights
from ethosu.vela.api import NpuAccelerator
from ethosu.vela.api import NpuBlockTraversal


@pytest.mark.parametrize(
    "arch",
    list(NpuAccelerator),
)
@pytest.mark.parametrize("dilation_x", [1, 2])
@pytest.mark.parametrize("dilation_y", [1, 2])
@pytest.mark.parametrize("ifm_bitdepth", [8, 16])
@pytest.mark.parametrize("depth_control", [1, 2, 3])
@pytest.mark.parametrize("weights_shape_and_block_depth", [((16, 16, 16, 16), 8), ((3, 3, 25, 16), 8)])
def test_encode_weights(
    arch,
    weights_shape_and_block_depth,
    dilation_x,
    dilation_y,
    ifm_bitdepth,
    depth_control,
):
    """
    This unit test checks the interface of the API function but not the functionality.
    Functional correctness is tested at a system level.
    """

    weights_shape = weights_shape_and_block_depth[0]
    ofm_block_depth = weights_shape_and_block_depth[1]
    val_max = np.iinfo(np.uint8).max
    weights_hwio = np.random.randint(val_max, size=weights_shape, dtype=np.uint8)
    weights_ohwi = np.transpose(weights_hwio, (3, 0, 1, 2))
    is_depthwise = True if depth_control == 2 else False
    block_traversal = NpuBlockTraversal.PART_KERNEL_FIRST if depth_control == 3 else NpuBlockTraversal.DEPTH_FIRST
    dilation_xy = (dilation_x, dilation_y)

    encoded_stream = npu_encode_weights(
        accelerator=arch,
        weights_volume=weights_ohwi,
        dilation_xy=dilation_xy,
        ifm_bitdepth=ifm_bitdepth,
        ofm_block_depth=ofm_block_depth,
        is_depthwise=is_depthwise,
        block_traversal=block_traversal,
    )
    assert type(encoded_stream) == bytearray