Patrik Gustavsson | e3b1b91 | 2021-02-09 15:38:46 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +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. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # Contains classes that hold commands for the high-level command stream (one command per DMA or NPU stripe). |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 18 | from typing import List |
Jonas Ohlsson | 845e232 | 2022-03-01 12:39:55 +0100 | [diff] [blame] | 19 | from typing import Optional |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 20 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 21 | import numpy as np |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 22 | |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 23 | from .architecture_features import Block |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 24 | from .numeric_util import round_up_divide |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 25 | from .operation import NpuBlockType |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 26 | from .shape4d import Shape4D |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class Box: |
| 30 | def __init__(self, start_coord, end_coord): |
| 31 | self.start_coord = list(start_coord) |
| 32 | self.end_coord = list(end_coord) |
| 33 | assert len(self.start_coord) == len(end_coord) |
| 34 | for i in range(len(self.start_coord)): |
| 35 | assert self.start_coord[i] <= self.end_coord[i] |
| 36 | |
erik.andersson@arm.com | 6b2a0b4 | 2022-03-22 15:35:30 +0100 | [diff] [blame] | 37 | @staticmethod |
| 38 | def wrap(a, b): |
| 39 | """Wrap broadcasted tensor boxes in order to |
| 40 | prevent out of bounds during box creation""" |
| 41 | tmp = [0, 0, 0, 0] |
| 42 | for i, val in enumerate(a): |
| 43 | if int(val) != 0: |
| 44 | tmp[i] = a[i] |
| 45 | if a[i] >= b[i] and b[i] != 0: |
| 46 | tmp[i] = a[i] % b[i] |
| 47 | return Shape4D(tmp) |
| 48 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 49 | def transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 50 | self, |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 51 | strides: List[int], |
| 52 | skirt: List[int], |
| 53 | ifm_shape: Shape4D, |
| 54 | npu_block_type: NpuBlockType, |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 55 | concat_offsets: List[int], |
Rickard Bolin | 1c08afa | 2022-01-07 14:22:52 +0000 | [diff] [blame] | 56 | k_dilated_height: int, |
Jonas Ohlsson | 845e232 | 2022-03-01 12:39:55 +0100 | [diff] [blame] | 57 | split_offset: Optional[Shape4D] = None, |
| 58 | split_shape: Optional[Shape4D] = None, |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 59 | upscaling_factor: int = 1, |
erik.andersson@arm.com | 6b2a0b4 | 2022-03-22 15:35:30 +0100 | [diff] [blame] | 60 | op_type=None, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 61 | ): |
| 62 | new_start_coord = list(self.start_coord) |
| 63 | new_end_coord = list(self.end_coord) |
| 64 | |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 65 | new_start_coord = np.subtract(new_start_coord, concat_offsets) |
| 66 | new_end_coord = np.subtract(new_end_coord, concat_offsets) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 67 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 68 | if split_offset is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 69 | for idx in range(len(split_offset)): |
| 70 | new_start_coord[idx] += split_offset[idx] |
| 71 | new_end_coord[idx] += split_offset[idx] |
| 72 | |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 73 | if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum): |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 74 | # these types of operations do a "dot product" or sum over the entire IFM |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 75 | if split_offset is None: |
| 76 | new_start_coord[-1] = 0 |
| 77 | new_end_coord[-1] = ifm_shape.depth |
| 78 | else: |
| 79 | new_start_coord[-1] = split_offset[-1] |
| 80 | new_end_coord[-1] = new_start_coord[-1] + split_shape[-1] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 81 | |
Patrik Gustavsson | e6b94bb | 2021-05-05 08:30:41 +0200 | [diff] [blame] | 82 | if len(new_end_coord) >= 1: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 83 | new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth) |
| 84 | if len(new_end_coord) >= 2: |
| 85 | new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor) |
| 86 | if len(new_end_coord) >= 3: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 87 | original_end_coord = list(new_end_coord) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 88 | new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 89 | |
| 90 | pad_top = 0 |
| 91 | pad_bottom = 0 |
| 92 | if strides is not None and skirt is not None: |
| 93 | if len(new_start_coord) >= 2: |
| 94 | stride = strides[2] |
Tim Hall | 3751aa4 | 2021-12-16 13:17:29 +0000 | [diff] [blame] | 95 | # if the current op was combined with a split slice read then the valid ifm range is given by the output |
Tim Hall | 51a8dce | 2021-12-20 16:49:27 +0000 | [diff] [blame] | 96 | # of the split op (which is defined by the read offset and the read shape) |
Tim Hall | 3751aa4 | 2021-12-16 13:17:29 +0000 | [diff] [blame] | 97 | if split_offset is None: |
| 98 | new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0) |
| 99 | new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width) |
| 100 | else: |
| 101 | new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], split_offset[-2]) |
Tim Hall | 51a8dce | 2021-12-20 16:49:27 +0000 | [diff] [blame] | 102 | new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], split_offset[-2] + split_shape[-2]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 103 | |
| 104 | if len(new_start_coord) >= 3: |
| 105 | stride = strides[1] |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 106 | skirt_top_remainder = skirt[0] % upscaling_factor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 107 | |
| 108 | total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 109 | new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 110 | |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 111 | pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 112 | new_start_coord[-3] = max(new_start_coord[-3], 0) |
| 113 | |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 114 | if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 115 | # pad_bottom is calculated based the diff between the end position of the weight kernel, |
| 116 | # after last stride and the ifm height. |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 117 | if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 118 | # Special case for Transpose Convolution with VALID padding. |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 119 | pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 120 | else: |
| 121 | k_start = new_start_coord[-3] - pad_top |
Rickard Bolin | 1c08afa | 2022-01-07 14:22:52 +0000 | [diff] [blame] | 122 | pad_bottom = max( |
| 123 | 0, k_start + total_stride + k_dilated_height - (ifm_shape.height * upscaling_factor) |
| 124 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 125 | |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 126 | # Adjust for upscaling |
| 127 | new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0) |
| 128 | new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 129 | new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 130 | |
erik.andersson@arm.com | 6b2a0b4 | 2022-03-22 15:35:30 +0100 | [diff] [blame] | 131 | # Wrap the IFMs of broadcasted binary elementwise ops |
| 132 | # at the limits of the non-broadcasted volumes |
| 133 | # Non-broadcasted ops aren't affected by the wrapping |
| 134 | if op_type is not None and op_type.is_binary_elementwise_op(): |
| 135 | tmp = list(ifm_shape) |
| 136 | one = Shape4D(1, 1, 1, 1) |
| 137 | new_start_coord = Box.wrap(new_start_coord, tmp) |
| 138 | new_end_coord = Box.wrap(Shape4D(list(new_end_coord)) - one, tmp) + one |
| 139 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 140 | return Box(new_start_coord, new_end_coord), pad_top, pad_bottom |
| 141 | |
| 142 | def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False): |
| 143 | start = [0] * len(weight_shape) |
| 144 | end = list(weight_shape) |
| 145 | if oc_range_start is not None and oc_range_end is not None: |
| 146 | if npu_block_type == NpuBlockType.ConvolutionDepthWise: |
| 147 | # input range is output range divided by channel multiplier |
| 148 | if weights_transposed: |
| 149 | start[-1] = oc_range_start // weight_shape[-2] |
| 150 | end[-1] = oc_range_end // weight_shape[-2] |
| 151 | else: |
| 152 | start[-2] = oc_range_start // weight_shape[-1] |
| 153 | end[-2] = oc_range_end // weight_shape[-1] |
| 154 | else: |
| 155 | start[-1] = oc_range_start |
| 156 | end[-1] = oc_range_end |
| 157 | for i in range(len(end)): |
| 158 | assert 0 <= start[i] < weight_shape[i] |
| 159 | assert 0 < end[i] <= weight_shape[i] |
| 160 | |
| 161 | return Box(start, end) |
| 162 | |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 163 | def is_subbox_of(self, other): |
| 164 | if self.start_coord and self.end_coord: |
| 165 | assert len(self.start_coord) == len(other.start_coord) |
| 166 | assert len(self.end_coord) == len(other.end_coord) |
| 167 | return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all( |
| 168 | a <= b for (a, b) in zip(self.end_coord, other.end_coord) |
| 169 | ) |
| 170 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 171 | def get_size_shape(self): |
| 172 | return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))] |
| 173 | |
| 174 | def get_size(self): |
| 175 | return int(np.prod(self.get_size_shape())) |
| 176 | |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 177 | def get_block(self) -> Block: |
| 178 | return Block.from_shape(self.get_size_shape()) |
| 179 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 180 | def __str__(self): |
| 181 | return "<Box %s - %s>" % (self.start_coord, self.end_coord) |
| 182 | |
| 183 | __repr__ = __str__ |
| 184 | |
| 185 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 186 | class Command: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 187 | def is_npu_pass_command(self): |
| 188 | return False |
| 189 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 190 | def get_operation_count(self): |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 191 | # returns numpy array of (DPU blocks, dma_ops). |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 192 | return np.array((0, 0)) |
| 193 | |
| 194 | |
| 195 | class NpuStripe(Command): |
| 196 | def __init__( |
| 197 | self, |
| 198 | ps, |
| 199 | block_config, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 200 | is_first_h_stripe, |
| 201 | is_last_h_stripe, |
| 202 | ifm_tensor, |
| 203 | ifm_box, |
| 204 | ofm_tensor, |
| 205 | ofm_box, |
| 206 | weight_tensor=None, |
| 207 | weight_box=None, |
Tim Hall | d784af7 | 2021-06-08 21:25:57 +0100 | [diff] [blame] | 208 | scale_tensor=None, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 209 | ifm2_tensor=None, |
| 210 | ifm2_box=None, |
| 211 | pad_top=0, |
| 212 | pad_bottom=0, |
Fredrik Svedberg | b81e1bb | 2022-10-11 21:50:51 +0200 | [diff] [blame] | 213 | reversed_operands=False, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 214 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 215 | self.ps = ps |
| 216 | self.block_config = block_config |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 217 | self.is_first_h_stripe = is_first_h_stripe |
| 218 | self.is_last_h_stripe = is_last_h_stripe |
| 219 | self.ifm_tensor = ifm_tensor |
| 220 | self.ifm_box = ifm_box |
| 221 | self.ifm2_tensor = ifm2_tensor |
| 222 | self.ifm2_box = ifm2_box |
| 223 | self.ofm_tensor = ofm_tensor |
| 224 | self.ofm_box = ofm_box |
| 225 | self.weight_tensor = weight_tensor |
Tim Hall | d784af7 | 2021-06-08 21:25:57 +0100 | [diff] [blame] | 226 | self.scale_tensor = scale_tensor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 227 | self.weight_box = weight_box |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 228 | self.pad_top = pad_top |
| 229 | self.pad_bottom = pad_bottom |
Fredrik Svedberg | b81e1bb | 2022-10-11 21:50:51 +0200 | [diff] [blame] | 230 | self.reversed_operands = reversed_operands |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 231 | for i in range(len(self.ofm_box.end_coord)): |
Tim Hall | 73e843f | 2021-02-04 22:47:46 +0000 | [diff] [blame] | 232 | assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 233 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 234 | def is_npu_pass_command(self): |
| 235 | return True |
| 236 | |
| 237 | def __str__(self): |
| 238 | return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % ( |
| 239 | self.ps.name, |
| 240 | self.ifm_box, |
| 241 | self.ifm2_box, |
| 242 | self.ofm_box, |
| 243 | self.weight_box, |
| 244 | self.block_config, |
| 245 | ) |
| 246 | |
| 247 | __repr__ = __str__ |
| 248 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 249 | def get_block_dimensions(self): |
| 250 | ofm_box = self.ofm_box |
| 251 | block_config = self.block_config |
| 252 | |
| 253 | out_height = 1 |
| 254 | out_width = 1 |
| 255 | out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1] |
| 256 | if len(ofm_box.end_coord) >= 4: |
| 257 | out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2] |
| 258 | out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3] |
| 259 | |
| 260 | assert out_height >= 0 |
| 261 | assert out_width >= 0 |
| 262 | assert out_depth >= 0 |
| 263 | return ( |
| 264 | round_up_divide(out_height, block_config[0]), |
| 265 | round_up_divide(out_width, block_config[1]), |
| 266 | round_up_divide(out_depth, block_config[3]), |
| 267 | ) |
| 268 | |
| 269 | def get_operation_count(self): |
| 270 | # returns numpy array of (DPU blocks, dma_ops) |
| 271 | return np.array((self.get_n_blocks(), 0)) |
| 272 | |
| 273 | def get_n_blocks(self): |
| 274 | h, w, d = self.get_block_dimensions() |
| 275 | res = h * w * d |
| 276 | assert res >= 0 |
| 277 | return res |
| 278 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 279 | |
| 280 | class DMA(Command): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 281 | def __init__(self, ps, in_tensor, out_tensor, box): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 282 | self.ps = ps |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 283 | self.in_tensor = in_tensor |
| 284 | self.out_tensor = out_tensor |
| 285 | self.box = box |
| 286 | |
| 287 | def __str__(self): |
| 288 | return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box) |
| 289 | |
| 290 | __repr__ = __str__ |
| 291 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 292 | def get_operation_count(self): |
| 293 | # returns numpy array of (DPU blocks, dma_ops) |
| 294 | return np.array((0, 1)) |