Louis Verhaard | 933f55e | 2020-11-25 14:10:30 +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 | # |
| 17 | # Description: |
| 18 | # Contains unit tests for npu_find_block_configs API for an external consumer |
| 19 | from ethosu.vela.api import npu_find_block_configs |
| 20 | from ethosu.vela.api import npu_generate_register_command_stream |
| 21 | from ethosu.vela.api import NpuAccelerator |
| 22 | from ethosu.vela.api import NpuAddressRange |
| 23 | from ethosu.vela.api import NpuBlockTraversal |
| 24 | from ethosu.vela.api import NpuConv2DOperation |
| 25 | from ethosu.vela.api import NpuKernel |
| 26 | from ethosu.vela.api import NpuPadding |
| 27 | from ethosu.vela.api import NpuQuantization |
| 28 | from ethosu.vela.api import NpuShape3D |
| 29 | from ethosu.vela.ethos_u55_regs.ethos_u55_regs import cmd0 |
| 30 | from ethosu.vela.test.extapi.test_extapi_generate_commands import check_cmd0 |
| 31 | from ethosu.vela.test.extapi.test_extapi_generate_commands import create_feature_map |
| 32 | |
| 33 | |
| 34 | def test_find_block_configs(): |
| 35 | """Tests npu_find_block_configs""" |
| 36 | # Create a Conv2D operation |
| 37 | op = NpuConv2DOperation() |
| 38 | op.ifm = create_feature_map( |
| 39 | NpuShape3D(height=30, width=62, depth=46), 1, 512, quant=NpuQuantization(scale_f32=0.007843138, zero_point=128) |
| 40 | ) |
| 41 | op.ofm = create_feature_map( |
| 42 | NpuShape3D(height=30, width=31, depth=46), |
| 43 | 1, |
| 44 | 0x14E40, |
| 45 | quant=NpuQuantization(scale_f32=0.20392157, zero_point=128), |
| 46 | ) |
| 47 | op.kernel = NpuKernel(3, 2, 2, 1) |
| 48 | op.biases = [NpuAddressRange(region=0, address=32000, length=464)] |
| 49 | op.padding = NpuPadding(top=0, left=0, right=1, bottom=1) |
| 50 | op.block_traversal = NpuBlockTraversal.PART_KERNEL_FIRST |
| 51 | # Find valid block configs |
| 52 | accelerator = NpuAccelerator.Ethos_U55_256 |
| 53 | block_configs = npu_find_block_configs(op, accelerator) |
| 54 | # Select the last one |
| 55 | op.block_config = block_configs[-1] |
| 56 | # Note: the weights should be encoded with op.block_config.depth (not shown here) |
| 57 | op.weights = [NpuAddressRange(region=0, address=0, length=7696)] |
| 58 | # Check that generating register commands succeeds |
| 59 | cmds = npu_generate_register_command_stream([op], accelerator) |
| 60 | # Check that the selected block config was used |
| 61 | check_cmd0(cmds, cmd0.NPU_SET_OFM_BLK_HEIGHT_M1, op.block_config.height - 1) |
| 62 | check_cmd0(cmds, cmd0.NPU_SET_OFM_BLK_WIDTH_M1, op.block_config.width - 1) |
| 63 | check_cmd0(cmds, cmd0.NPU_SET_OFM_BLK_DEPTH_M1, op.block_config.depth - 1) |
James Ward | 399c4a2 | 2021-10-20 11:04:46 +0100 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def test_conv2d_block_height_1(): |
| 67 | """Test npu_find_block_configs returns valid config in the special case of reduced ublock height (H256).""" |
| 68 | # Create a Conv2D operation |
| 69 | op = NpuConv2DOperation() |
| 70 | op.ifm = create_feature_map( |
| 71 | NpuShape3D(height=1, width=1, depth=1024), |
| 72 | 1, |
| 73 | 512, |
| 74 | quant=NpuQuantization(scale_f32=0.023528477177023888, zero_point=0), |
| 75 | ) |
| 76 | op.ofm = create_feature_map( |
| 77 | NpuShape3D(height=1, width=1, depth=1001), |
| 78 | 1, |
| 79 | 0x14E40, |
| 80 | quant=NpuQuantization(scale_f32=0.16609922051429749, zero_point=66), |
| 81 | ) |
| 82 | op.kernel = NpuKernel(1, 1, 1, 1, 1, 1) |
| 83 | op.padding = NpuPadding(top=0, left=0, right=0, bottom=0) |
| 84 | op.block_traversal = NpuBlockTraversal.PART_KERNEL_FIRST |
| 85 | |
| 86 | # Find valid block configs |
| 87 | accelerator = NpuAccelerator.Ethos_U55_256 |
| 88 | block_configs = npu_find_block_configs(op, accelerator) |
| 89 | # Select the last one |
| 90 | op.block_config = block_configs[-1] |
| 91 | # Note: the weights should be encoded with op.block_config.depth (not shown here) |
| 92 | op.weights = [NpuAddressRange(region=0, address=0, length=7696)] |
| 93 | |
| 94 | # Check that generating register commands succeeds |
| 95 | cmds = npu_generate_register_command_stream([op], accelerator) |
| 96 | # Check that the selected block config was used |
| 97 | check_cmd0(cmds, cmd0.NPU_SET_OFM_BLK_HEIGHT_M1, op.block_config.height - 1) |
| 98 | check_cmd0(cmds, cmd0.NPU_SET_OFM_BLK_WIDTH_M1, op.block_config.width - 1) |
| 99 | check_cmd0(cmds, cmd0.NPU_SET_OFM_BLK_DEPTH_M1, op.block_config.depth - 1) |