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 |
| 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 |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 25 | from .shape4d import Shape4D |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class Box: |
| 29 | def __init__(self, start_coord, end_coord): |
| 30 | self.start_coord = list(start_coord) |
| 31 | self.end_coord = list(end_coord) |
| 32 | assert len(self.start_coord) == len(end_coord) |
| 33 | for i in range(len(self.start_coord)): |
| 34 | assert self.start_coord[i] <= self.end_coord[i] |
| 35 | |
| 36 | def transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 37 | self, |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 38 | strides: List[int], |
| 39 | skirt: List[int], |
| 40 | ifm_shape: Shape4D, |
| 41 | npu_block_type: NpuBlockType, |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 42 | concat_offsets: List[int], |
Patrik Gustavsson | e3b1b91 | 2021-02-09 15:38:46 +0100 | [diff] [blame] | 43 | split_offset: Shape4D = None, |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 44 | split_shape: Shape4D = None, |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 45 | k_height: int = 1, |
| 46 | upscaling_factor: int = 1, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 47 | ): |
| 48 | new_start_coord = list(self.start_coord) |
| 49 | new_end_coord = list(self.end_coord) |
| 50 | |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 51 | new_start_coord = np.subtract(new_start_coord, concat_offsets) |
| 52 | new_end_coord = np.subtract(new_end_coord, concat_offsets) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 53 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 54 | if split_offset is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 55 | for idx in range(len(split_offset)): |
| 56 | new_start_coord[idx] += split_offset[idx] |
| 57 | new_end_coord[idx] += split_offset[idx] |
| 58 | |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 59 | if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum): |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 60 | # 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] | 61 | if split_offset is None: |
| 62 | new_start_coord[-1] = 0 |
| 63 | new_end_coord[-1] = ifm_shape.depth |
| 64 | else: |
| 65 | new_start_coord[-1] = split_offset[-1] |
| 66 | new_end_coord[-1] = new_start_coord[-1] + split_shape[-1] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 67 | |
Patrik Gustavsson | e6b94bb | 2021-05-05 08:30:41 +0200 | [diff] [blame] | 68 | if len(new_end_coord) >= 1: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 69 | new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth) |
| 70 | if len(new_end_coord) >= 2: |
| 71 | new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor) |
| 72 | if len(new_end_coord) >= 3: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 73 | original_end_coord = list(new_end_coord) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | pad_top = 0 |
| 77 | pad_bottom = 0 |
| 78 | if strides is not None and skirt is not None: |
| 79 | if len(new_start_coord) >= 2: |
| 80 | stride = strides[2] |
| 81 | new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 82 | new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 83 | |
| 84 | if len(new_start_coord) >= 3: |
| 85 | stride = strides[1] |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 86 | skirt_top_remainder = skirt[0] % upscaling_factor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 87 | |
| 88 | total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 89 | 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] | 90 | |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 91 | pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 92 | new_start_coord[-3] = max(new_start_coord[-3], 0) |
| 93 | |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 94 | 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] | 95 | # pad_bottom is calculated based the diff between the end position of the weight kernel, |
| 96 | # after last stride and the ifm height. |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 97 | 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] | 98 | # Special case for Transpose Convolution with VALID padding. |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 99 | pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 100 | else: |
| 101 | k_start = new_start_coord[-3] - pad_top |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 102 | pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape.height * 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 | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 107 | 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] | 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 | |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 132 | def is_subbox_of(self, other): |
| 133 | if self.start_coord and self.end_coord: |
| 134 | assert len(self.start_coord) == len(other.start_coord) |
| 135 | assert len(self.end_coord) == len(other.end_coord) |
| 136 | return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all( |
| 137 | a <= b for (a, b) in zip(self.end_coord, other.end_coord) |
| 138 | ) |
| 139 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 140 | def get_size_shape(self): |
| 141 | return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))] |
| 142 | |
| 143 | def get_size(self): |
| 144 | return int(np.prod(self.get_size_shape())) |
| 145 | |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 146 | def get_block(self) -> Block: |
| 147 | return Block.from_shape(self.get_size_shape()) |
| 148 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 149 | def __str__(self): |
| 150 | return "<Box %s - %s>" % (self.start_coord, self.end_coord) |
| 151 | |
| 152 | __repr__ = __str__ |
| 153 | |
| 154 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 155 | class Command: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 156 | def is_npu_pass_command(self): |
| 157 | return False |
| 158 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 159 | def get_operation_count(self): |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 160 | # returns numpy array of (DPU blocks, dma_ops). |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 161 | return np.array((0, 0)) |
| 162 | |
| 163 | |
| 164 | class NpuStripe(Command): |
| 165 | def __init__( |
| 166 | self, |
| 167 | ps, |
| 168 | block_config, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 169 | is_first_h_stripe, |
| 170 | is_last_h_stripe, |
| 171 | ifm_tensor, |
| 172 | ifm_box, |
| 173 | ofm_tensor, |
| 174 | ofm_box, |
| 175 | weight_tensor=None, |
| 176 | weight_box=None, |
Tim Hall | d784af7 | 2021-06-08 21:25:57 +0100 | [diff] [blame] | 177 | scale_tensor=None, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 178 | ifm2_tensor=None, |
| 179 | ifm2_box=None, |
| 180 | pad_top=0, |
| 181 | pad_bottom=0, |
| 182 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 183 | self.ps = ps |
| 184 | self.block_config = block_config |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 185 | self.is_first_h_stripe = is_first_h_stripe |
| 186 | self.is_last_h_stripe = is_last_h_stripe |
| 187 | self.ifm_tensor = ifm_tensor |
| 188 | self.ifm_box = ifm_box |
| 189 | self.ifm2_tensor = ifm2_tensor |
| 190 | self.ifm2_box = ifm2_box |
| 191 | self.ofm_tensor = ofm_tensor |
| 192 | self.ofm_box = ofm_box |
| 193 | self.weight_tensor = weight_tensor |
Tim Hall | d784af7 | 2021-06-08 21:25:57 +0100 | [diff] [blame] | 194 | self.scale_tensor = scale_tensor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 195 | self.weight_box = weight_box |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 196 | self.pad_top = pad_top |
| 197 | self.pad_bottom = pad_bottom |
| 198 | for i in range(len(self.ofm_box.end_coord)): |
Tim Hall | 73e843f | 2021-02-04 22:47:46 +0000 | [diff] [blame] | 199 | assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 200 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 201 | def is_npu_pass_command(self): |
| 202 | return True |
| 203 | |
| 204 | def __str__(self): |
| 205 | return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % ( |
| 206 | self.ps.name, |
| 207 | self.ifm_box, |
| 208 | self.ifm2_box, |
| 209 | self.ofm_box, |
| 210 | self.weight_box, |
| 211 | self.block_config, |
| 212 | ) |
| 213 | |
| 214 | __repr__ = __str__ |
| 215 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 216 | def get_block_dimensions(self): |
| 217 | ofm_box = self.ofm_box |
| 218 | block_config = self.block_config |
| 219 | |
| 220 | out_height = 1 |
| 221 | out_width = 1 |
| 222 | out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1] |
| 223 | if len(ofm_box.end_coord) >= 4: |
| 224 | out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2] |
| 225 | out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3] |
| 226 | |
| 227 | assert out_height >= 0 |
| 228 | assert out_width >= 0 |
| 229 | assert out_depth >= 0 |
| 230 | return ( |
| 231 | round_up_divide(out_height, block_config[0]), |
| 232 | round_up_divide(out_width, block_config[1]), |
| 233 | round_up_divide(out_depth, block_config[3]), |
| 234 | ) |
| 235 | |
| 236 | def get_operation_count(self): |
| 237 | # returns numpy array of (DPU blocks, dma_ops) |
| 238 | return np.array((self.get_n_blocks(), 0)) |
| 239 | |
| 240 | def get_n_blocks(self): |
| 241 | h, w, d = self.get_block_dimensions() |
| 242 | res = h * w * d |
| 243 | assert res >= 0 |
| 244 | return res |
| 245 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 246 | |
| 247 | class DMA(Command): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 248 | def __init__(self, ps, in_tensor, out_tensor, box): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 249 | self.ps = ps |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 250 | self.in_tensor = in_tensor |
| 251 | self.out_tensor = out_tensor |
| 252 | self.box = box |
| 253 | |
| 254 | def __str__(self): |
| 255 | return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box) |
| 256 | |
| 257 | __repr__ = __str__ |
| 258 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 259 | def get_operation_count(self): |
| 260 | # returns numpy array of (DPU blocks, dma_ops) |
| 261 | return np.array((0, 1)) |