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