Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 1 | # Copyright (C) 2021 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 | # Unit tests for architecture_allocator.py |
| 19 | import pytest |
| 20 | |
| 21 | from ethosu.vela.architecture_allocator import find_block_config |
| 22 | from ethosu.vela.architecture_allocator import try_block_config |
| 23 | from ethosu.vela.architecture_features import Accelerator |
| 24 | from ethosu.vela.architecture_features import Block |
| 25 | from ethosu.vela.architecture_features import create_default_arch |
| 26 | from ethosu.vela.ethos_u55_regs.ethos_u55_regs import resampling_mode |
| 27 | from ethosu.vela.operation import Kernel |
| 28 | from ethosu.vela.operation import NpuBlockType |
| 29 | from ethosu.vela.shape4d import Shape4D |
| 30 | |
| 31 | test_data = [ |
| 32 | { |
| 33 | "block_type": NpuBlockType.ConvolutionDepthWise, |
| 34 | "kernel": Kernel(25, 5, 2, 2, 1, 1), |
| 35 | "ofm_shape": Shape4D(2, 11, 22), |
| 36 | "ifm_shape": Shape4D(27, 25, 22), |
| 37 | }, |
| 38 | { |
| 39 | "block_type": NpuBlockType.Pooling, |
| 40 | "kernel": Kernel(2, 2), |
| 41 | "ofm_shape": Shape4D(53, 49, 22), |
| 42 | "ifm_shape": Shape4D(27, 25, 22), |
| 43 | "ifm_resampling": resampling_mode.NEAREST, |
| 44 | }, |
| 45 | { |
| 46 | "block_type": NpuBlockType.ConvolutionMxN, |
| 47 | "accelerator": Accelerator.Ethos_U55_32, |
| 48 | "kernel": Kernel(2, 5), |
| 49 | "ofm_shape": Shape4D(48, 1, 17), |
| 50 | "ifm_shape": Shape4D(24, 5, 18), |
| 51 | "ifm_resampling": resampling_mode.TRANSPOSE, |
| 52 | }, |
| 53 | { |
| 54 | "block_type": NpuBlockType.ElementWise, |
| 55 | "ofm_shape": Shape4D(27, 2, 22), |
| 56 | "ifm_shape": Shape4D(27, 2, 1), |
| 57 | "ifm2_shape": Shape4D(27, 25, 22), |
| 58 | }, |
| 59 | { |
| 60 | "block_type": NpuBlockType.ElementWise, |
| 61 | "accelerator": Accelerator.Ethos_U55_32, |
| 62 | "ofm_shape": Shape4D(48, 37, 17), |
| 63 | "ifm_shape": Shape4D(48, 37, 17), |
| 64 | "uses_scalar": True, |
| 65 | "lut_banks": 2, |
| 66 | }, |
| 67 | { |
| 68 | "block_type": NpuBlockType.ElementWise, |
| 69 | "ofm_shape": Shape4D(27, 2, 22), |
| 70 | "ifm_shape": Shape4D(27, 2, 22), |
| 71 | "ifm_bits": 16, |
| 72 | }, |
| 73 | ] |
| 74 | |
| 75 | |
| 76 | @pytest.mark.parametrize("test_data", test_data) |
| 77 | def test_allocate(test_data): |
| 78 | """Tests that find_block_config and try_block_config produce consistent SHRAM layouts""" |
| 79 | accelerator = test_data.get("accelerator", Accelerator.Ethos_U55_128) |
| 80 | arch = create_default_arch(accelerator) |
| 81 | kernel = test_data.get("kernel", Kernel(1, 1)) |
| 82 | block_type = test_data["block_type"] |
| 83 | ofm_shape = test_data["ofm_shape"] |
| 84 | ifm_shape = test_data["ifm_shape"] |
| 85 | ifm2_shape = test_data.get("ifm2_shape") |
| 86 | uses_scalar = test_data.get("uses_scalar", False) |
| 87 | ifm_bits = test_data.get("ifm_bits", 8) |
| 88 | ifm_resampling = test_data.get("ifm_resampling", resampling_mode.NONE) |
| 89 | scaled = test_data.get("scaled", True) |
| 90 | lut_banks = test_data.get("lut_banks", 0) |
| 91 | config = find_block_config( |
| 92 | arch, |
| 93 | block_type, |
| 94 | ofm_shape, |
| 95 | ifm_shape, |
| 96 | ifm2_shape, |
| 97 | uses_scalar=uses_scalar, |
| 98 | ifm_bits=ifm_bits, |
| 99 | kernel=kernel, |
| 100 | lut_banks=lut_banks, |
| 101 | scaled=scaled, |
| 102 | ifm_resampling=ifm_resampling, |
| 103 | ) |
| 104 | assert config is not None |
| 105 | config2 = try_block_config( |
| 106 | Block.from_shape(config.ofm_block.as_list()), |
| 107 | arch, |
| 108 | block_type, |
Tim Hall | 3016157 | 2021-06-17 17:03:49 +0100 | [diff] [blame] | 109 | ofm_shape, |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 110 | ifm_shape, |
| 111 | ifm2_shape, |
| 112 | is_partkernel=config.is_partkernel, |
| 113 | uses_scalar=uses_scalar, |
| 114 | ifm_bits=ifm_bits, |
| 115 | kernel=kernel, |
| 116 | lut_banks=lut_banks, |
| 117 | scaled=scaled, |
| 118 | ifm_resampling=ifm_resampling, |
| 119 | ) |
| 120 | assert config2 is not None |
| 121 | assert config.layout.ib_end == config2.layout.ib_end |
| 122 | assert config.layout.ab_start == config2.layout.ab_start |
| 123 | assert config.layout.ib_start2 == config2.layout.ib_start2 |
| 124 | assert config.acc_type == config2.acc_type |