Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +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. |
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). |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 18 | from enum import IntEnum |
| 19 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 20 | import numpy as np |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 21 | |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 22 | from .architecture_features import Block |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | from .numeric_util import round_up_divide |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 24 | from .operation import NpuBlockType |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class Box: |
| 28 | def __init__(self, start_coord, end_coord): |
| 29 | self.start_coord = list(start_coord) |
| 30 | self.end_coord = list(end_coord) |
| 31 | assert len(self.start_coord) == len(end_coord) |
| 32 | for i in range(len(self.start_coord)): |
| 33 | assert self.start_coord[i] <= self.end_coord[i] |
| 34 | |
| 35 | def transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 36 | self, |
| 37 | strides, |
| 38 | skirt, |
| 39 | ifm_shape, |
| 40 | npu_block_type, |
| 41 | concat_axis=0, |
| 42 | concat_offset=0, |
| 43 | split_offset=None, |
| 44 | k_height=1, |
| 45 | upscaling_factor=1, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 46 | ): |
| 47 | new_start_coord = list(self.start_coord) |
| 48 | new_end_coord = list(self.end_coord) |
| 49 | |
| 50 | new_start_coord[concat_axis] -= concat_offset |
| 51 | new_end_coord[concat_axis] -= concat_offset |
| 52 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 53 | if split_offset is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 54 | for idx in range(len(split_offset)): |
| 55 | new_start_coord[idx] += split_offset[idx] |
| 56 | new_end_coord[idx] += split_offset[idx] |
| 57 | |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 58 | if split_offset is None and npu_block_type in set( |
| 59 | (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum) |
| 60 | ): |
| 61 | # these types of operations do a "dot product" or sum over the entire IFM |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 62 | new_start_coord[-1] = 0 |
| 63 | new_end_coord[-1] = ifm_shape[-1] |
| 64 | |
Louis Verhaard | e0ef273 | 2020-06-03 08:56:44 +0200 | [diff] [blame] | 65 | if npu_block_type == NpuBlockType.ElementWise and min(len(new_end_coord), len(ifm_shape)) >= 1: |
| 66 | new_end_coord[-1] = min(new_end_coord[-1], ifm_shape[-1]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 67 | if min(len(new_end_coord), len(ifm_shape)) >= 2: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 68 | new_end_coord[-2] = min(new_end_coord[-2], ifm_shape[-2] * upscaling_factor) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 69 | if min(len(new_end_coord), len(ifm_shape)) >= 3: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 70 | original_end_coord = list(new_end_coord) |
| 71 | new_end_coord[-3] = min(new_end_coord[-3], ifm_shape[-3] * upscaling_factor) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 72 | |
| 73 | pad_top = 0 |
| 74 | pad_bottom = 0 |
| 75 | if strides is not None and skirt is not None: |
| 76 | if len(new_start_coord) >= 2: |
| 77 | stride = strides[2] |
| 78 | new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0) |
| 79 | new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape[-2]) |
| 80 | |
| 81 | if len(new_start_coord) >= 3: |
| 82 | stride = strides[1] |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 83 | skirt_top_remainder = skirt[0] % upscaling_factor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 84 | |
| 85 | total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 86 | 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] | 87 | |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 88 | pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 89 | new_start_coord[-3] = max(new_start_coord[-3], 0) |
| 90 | |
| 91 | while len(ifm_shape) < 3: |
| 92 | ifm_shape = [1] + ifm_shape |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 93 | |
| 94 | if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape[-3] * upscaling_factor): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 95 | # pad_bottom is calculated based the diff between the end position of the weight kernel, |
| 96 | # after last stride and the ifm height. |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 97 | if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape[-3] * upscaling_factor: |
| 98 | # Special case for Transpose Convolution with VALID padding. |
| 99 | pad_bottom = original_end_coord[-3] - (ifm_shape[-3] * upscaling_factor) |
| 100 | else: |
| 101 | k_start = new_start_coord[-3] - pad_top |
| 102 | pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape[-3] * upscaling_factor)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 103 | |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 104 | # Adjust for upscaling |
| 105 | new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0) |
| 106 | new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor) |
Patrik Gustavsson | 34b9dc1 | 2020-11-25 13:41:22 +0100 | [diff] [blame] | 107 | new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape[-3]), 1) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 108 | |
| 109 | return Box(new_start_coord, new_end_coord), pad_top, pad_bottom |
| 110 | |
| 111 | def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False): |
| 112 | start = [0] * len(weight_shape) |
| 113 | end = list(weight_shape) |
| 114 | if oc_range_start is not None and oc_range_end is not None: |
| 115 | if npu_block_type == NpuBlockType.ConvolutionDepthWise: |
| 116 | # input range is output range divided by channel multiplier |
| 117 | if weights_transposed: |
| 118 | start[-1] = oc_range_start // weight_shape[-2] |
| 119 | end[-1] = oc_range_end // weight_shape[-2] |
| 120 | else: |
| 121 | start[-2] = oc_range_start // weight_shape[-1] |
| 122 | end[-2] = oc_range_end // weight_shape[-1] |
| 123 | else: |
| 124 | start[-1] = oc_range_start |
| 125 | end[-1] = oc_range_end |
| 126 | for i in range(len(end)): |
| 127 | assert 0 <= start[i] < weight_shape[i] |
| 128 | assert 0 < end[i] <= weight_shape[i] |
| 129 | |
| 130 | return Box(start, end) |
| 131 | |
| 132 | def get_size_shape(self): |
| 133 | return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))] |
| 134 | |
| 135 | def get_size(self): |
| 136 | return int(np.prod(self.get_size_shape())) |
| 137 | |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 138 | def get_block(self) -> Block: |
| 139 | return Block.from_shape(self.get_size_shape()) |
| 140 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 141 | def __str__(self): |
| 142 | return "<Box %s - %s>" % (self.start_coord, self.end_coord) |
| 143 | |
| 144 | __repr__ = __str__ |
| 145 | |
| 146 | |
| 147 | class CommandType(IntEnum): |
| 148 | NpuStripe = 0 |
| 149 | DMA = 1 |
| 150 | Size = 2 |
| 151 | |
| 152 | |
| 153 | class Command: |
| 154 | def get_ofm_y_range_for_pass(self, ps_requested): |
| 155 | return None |
| 156 | |
| 157 | def is_npu_pass_command(self): |
| 158 | return False |
| 159 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 160 | def get_operation_count(self): |
| 161 | # returns numpy array of (DPU blocks, dma_ops). Should line up with the CommandType enum |
| 162 | return np.array((0, 0)) |
| 163 | |
| 164 | |
| 165 | class NpuStripe(Command): |
| 166 | def __init__( |
| 167 | self, |
| 168 | ps, |
| 169 | block_config, |
| 170 | is_first, |
| 171 | is_last, |
| 172 | is_first_h_stripe, |
| 173 | is_last_h_stripe, |
| 174 | ifm_tensor, |
| 175 | ifm_box, |
| 176 | ofm_tensor, |
| 177 | ofm_box, |
| 178 | weight_tensor=None, |
| 179 | weight_box=None, |
| 180 | scale_tensor=None, |
| 181 | concat_axis=0, |
| 182 | concat_offset=0, |
| 183 | ifm2_tensor=None, |
| 184 | ifm2_box=None, |
| 185 | pad_top=0, |
| 186 | pad_bottom=0, |
| 187 | ): |
| 188 | self.cmdtype = CommandType.NpuStripe |
| 189 | self.ps = ps |
| 190 | self.block_config = block_config |
| 191 | self.is_first = is_first |
| 192 | self.is_last = is_last |
| 193 | self.is_first_h_stripe = is_first_h_stripe |
| 194 | self.is_last_h_stripe = is_last_h_stripe |
| 195 | self.ifm_tensor = ifm_tensor |
| 196 | self.ifm_box = ifm_box |
| 197 | self.ifm2_tensor = ifm2_tensor |
| 198 | self.ifm2_box = ifm2_box |
| 199 | self.ofm_tensor = ofm_tensor |
| 200 | self.ofm_box = ofm_box |
| 201 | self.weight_tensor = weight_tensor |
| 202 | self.scale_tensor = scale_tensor |
| 203 | self.weight_box = weight_box |
| 204 | self.concat_axis = concat_axis |
| 205 | self.concat_offset = concat_offset |
| 206 | self.pad_top = pad_top |
| 207 | self.pad_bottom = pad_bottom |
| 208 | for i in range(len(self.ofm_box.end_coord)): |
| 209 | assert self.ofm_box.end_coord[i] <= self.ofm_tensor.shape[i] |
| 210 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 211 | def is_npu_pass_command(self): |
| 212 | return True |
| 213 | |
| 214 | def __str__(self): |
| 215 | return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % ( |
| 216 | self.ps.name, |
| 217 | self.ifm_box, |
| 218 | self.ifm2_box, |
| 219 | self.ofm_box, |
| 220 | self.weight_box, |
| 221 | self.block_config, |
| 222 | ) |
| 223 | |
| 224 | __repr__ = __str__ |
| 225 | |
| 226 | def get_ofm_y_range_for_pass(self, ps_requested): |
| 227 | if ps_requested != self.ps: |
| 228 | return None |
| 229 | if len(self.ofm_box.start_coord) >= 3: |
| 230 | return (self.ofm_box.start_coord[-3], self.ofm_box.end_coord[-3]) |
| 231 | return None |
| 232 | |
| 233 | def get_block_dimensions(self): |
| 234 | ofm_box = self.ofm_box |
| 235 | block_config = self.block_config |
| 236 | |
| 237 | out_height = 1 |
| 238 | out_width = 1 |
| 239 | out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1] |
| 240 | if len(ofm_box.end_coord) >= 4: |
| 241 | out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2] |
| 242 | out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3] |
| 243 | |
| 244 | assert out_height >= 0 |
| 245 | assert out_width >= 0 |
| 246 | assert out_depth >= 0 |
| 247 | return ( |
| 248 | round_up_divide(out_height, block_config[0]), |
| 249 | round_up_divide(out_width, block_config[1]), |
| 250 | round_up_divide(out_depth, block_config[3]), |
| 251 | ) |
| 252 | |
| 253 | def get_operation_count(self): |
| 254 | # returns numpy array of (DPU blocks, dma_ops) |
| 255 | return np.array((self.get_n_blocks(), 0)) |
| 256 | |
| 257 | def get_n_blocks(self): |
| 258 | h, w, d = self.get_block_dimensions() |
| 259 | res = h * w * d |
| 260 | assert res >= 0 |
| 261 | return res |
| 262 | |
| 263 | def get_single_block_command(self, block_idx): |
| 264 | block_cfg = (self.block_config[0], self.block_config[1], self.block_config[3]) |
| 265 | dims = self.get_block_dimensions() |
| 266 | strides = dims[1] * dims[2], dims[2], 1 |
| 267 | coord = [] |
| 268 | idx_left = block_idx |
| 269 | for s in strides: |
| 270 | c = idx_left // s |
| 271 | idx_left -= c * s |
| 272 | coord.append(c) |
| 273 | |
| 274 | assert idx_left == 0 |
| 275 | |
| 276 | # put in dummy height/widths in case we're dealing with FC layers |
| 277 | ofm_start = list(self.ofm_box.start_coord) |
| 278 | ofm_end = list(self.ofm_box.end_coord) |
| 279 | |
| 280 | # cut out a nice block shape |
| 281 | for idx in (-1, -2, -3): |
| 282 | if len(ofm_start) >= -idx: |
| 283 | ofm_start[idx] += block_cfg[idx] * coord[idx] |
| 284 | ofm_end[idx] = min(ofm_end[idx], ofm_start[idx] + block_cfg[idx]) |
| 285 | |
| 286 | ps = self.ps |
| 287 | strides = None |
| 288 | skirt = None |
| 289 | if ps.primary_op is not None: |
| 290 | strides = ps.primary_op.attrs.get("strides", None) |
| 291 | skirt = ps.primary_op.attrs.get("skirt", None) |
| 292 | npu_block_type = ps.npu_block_type |
| 293 | |
| 294 | ofm_box = Box(ofm_start, ofm_end) |
| 295 | ifm_box, _, _ = ofm_box.transform_with_strides_and_skirt( |
| 296 | strides, skirt, self.ifm_tensor.shape, npu_block_type, self.concat_axis, self.concat_offset |
| 297 | ) |
| 298 | |
| 299 | weight_box = None |
| 300 | if self.weight_tensor is not None: |
| 301 | weight_oc_start = ofm_start[-1] |
| 302 | weight_oc_end = ofm_end[-1] |
| 303 | if self.concat_axis - len(self.weight_tensor.shape) == -1: |
| 304 | weight_oc_start -= self.concat_offset |
| 305 | weight_oc_end -= self.concat_offset |
| 306 | |
| 307 | weight_box = Box.make_weight_box( |
| 308 | self.weight_tensor.shape, |
| 309 | npu_block_type, |
| 310 | weight_oc_start, |
| 311 | weight_oc_end, |
| 312 | self.weight_tensor.weight_transpose_depthwise, |
| 313 | ) |
| 314 | |
| 315 | return NpuStripe( |
| 316 | self.ps, |
| 317 | self.block_config, |
| 318 | self.is_first, |
| 319 | self.is_last, |
| 320 | self.is_first_h_stripe, |
| 321 | self.is_last_h_stripe, |
| 322 | self.ifm_tensor, |
| 323 | ifm_box, |
| 324 | self.ofm_tensor, |
| 325 | ofm_box, |
| 326 | self.weight_tensor, |
| 327 | weight_box, |
| 328 | self.scale_tensor, |
| 329 | self.concat_axis, |
| 330 | self.concat_offset, |
| 331 | ) |
| 332 | |
| 333 | |
| 334 | class DMA(Command): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 335 | def __init__(self, ps, in_tensor, out_tensor, box): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 336 | self.cmdtype = CommandType.DMA |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 337 | self.ps = ps |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 338 | self.in_tensor = in_tensor |
| 339 | self.out_tensor = out_tensor |
| 340 | self.box = box |
| 341 | |
| 342 | def __str__(self): |
| 343 | return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box) |
| 344 | |
| 345 | __repr__ = __str__ |
| 346 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 347 | def get_operation_count(self): |
| 348 | # returns numpy array of (DPU blocks, dma_ops) |
| 349 | return np.array((0, 1)) |