blob: cf31aa5f05062b4413e97b2997bb355e464fd6eb [file] [log] [blame]
Patrik Gustavssone3b1b912021-02-09 15:38:46 +01001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
Tim Hall79d07d22020-04-27 18:20:16 +01002#
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 Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Contains classes that hold commands for the high-level command stream (one command per DMA or NPU stripe).
patrik.gustavssoneeb85152020-12-21 17:10:40 +000018from typing import List
19
Tim Hall79d07d22020-04-27 18:20:16 +010020import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010021
Louis Verhaard69b31762020-11-17 09:45:20 +010022from .architecture_features import Block
Tim Hall79d07d22020-04-27 18:20:16 +010023from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010024from .operation import NpuBlockType
patrik.gustavssoneeb85152020-12-21 17:10:40 +000025from .shape4d import Shape4D
Tim Hall79d07d22020-04-27 18:20:16 +010026
27
28class 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 Hallc30f4952020-06-15 20:47:35 +010037 self,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000038 strides: List[int],
39 skirt: List[int],
40 ifm_shape: Shape4D,
41 npu_block_type: NpuBlockType,
Louis Verhaardc822d622021-03-11 14:59:06 +010042 concat_offsets: List[int],
Patrik Gustavssone3b1b912021-02-09 15:38:46 +010043 split_offset: Shape4D = None,
Tim Halld8339a72021-05-27 18:49:40 +010044 split_shape: Shape4D = None,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000045 k_height: int = 1,
46 upscaling_factor: int = 1,
Tim Hall79d07d22020-04-27 18:20:16 +010047 ):
48 new_start_coord = list(self.start_coord)
49 new_end_coord = list(self.end_coord)
50
Louis Verhaardc822d622021-03-11 14:59:06 +010051 new_start_coord = np.subtract(new_start_coord, concat_offsets)
52 new_end_coord = np.subtract(new_end_coord, concat_offsets)
Tim Hall79d07d22020-04-27 18:20:16 +010053
Diego Russoea6111a2020-04-14 18:41:58 +010054 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010055 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 Halld8339a72021-05-27 18:49:40 +010059 if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020060 # these types of operations do a "dot product" or sum over the entire IFM
Tim Halld8339a72021-05-27 18:49:40 +010061 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 Hall79d07d22020-04-27 18:20:16 +010067
Patrik Gustavssone6b94bb2021-05-05 08:30:41 +020068 if len(new_end_coord) >= 1:
patrik.gustavssoneeb85152020-12-21 17:10:40 +000069 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 Bohlin9b64ba02020-07-07 17:15:22 +020073 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000074 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010075
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]
Tim Hall3751aa42021-12-16 13:17:29 +000081 # if the current op was combined with a split slice read then the valid ifm range is given by the output
Tim Hall51a8dce2021-12-20 16:49:27 +000082 # of the split op (which is defined by the read offset and the read shape)
Tim Hall3751aa42021-12-16 13:17:29 +000083 if split_offset is None:
84 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
85 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
86 else:
87 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], split_offset[-2])
Tim Hall51a8dce2021-12-20 16:49:27 +000088 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], split_offset[-2] + split_shape[-2])
Tim Hall79d07d22020-04-27 18:20:16 +010089
90 if len(new_start_coord) >= 3:
91 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020092 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +010093
94 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020095 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010096
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020097 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010098 new_start_coord[-3] = max(new_start_coord[-3], 0)
99
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000100 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +0100101 # pad_bottom is calculated based the diff between the end position of the weight kernel,
102 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000103 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200104 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000105 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200106 else:
107 k_start = new_start_coord[-3] - pad_top
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000108 pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape.height * upscaling_factor))
Tim Hall79d07d22020-04-27 18:20:16 +0100109
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200110 # Adjust for upscaling
111 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
112 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000113 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100114
115 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
116
117 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
118 start = [0] * len(weight_shape)
119 end = list(weight_shape)
120 if oc_range_start is not None and oc_range_end is not None:
121 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
122 # input range is output range divided by channel multiplier
123 if weights_transposed:
124 start[-1] = oc_range_start // weight_shape[-2]
125 end[-1] = oc_range_end // weight_shape[-2]
126 else:
127 start[-2] = oc_range_start // weight_shape[-1]
128 end[-2] = oc_range_end // weight_shape[-1]
129 else:
130 start[-1] = oc_range_start
131 end[-1] = oc_range_end
132 for i in range(len(end)):
133 assert 0 <= start[i] < weight_shape[i]
134 assert 0 < end[i] <= weight_shape[i]
135
136 return Box(start, end)
137
Tim Halld8339a72021-05-27 18:49:40 +0100138 def is_subbox_of(self, other):
139 if self.start_coord and self.end_coord:
140 assert len(self.start_coord) == len(other.start_coord)
141 assert len(self.end_coord) == len(other.end_coord)
142 return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all(
143 a <= b for (a, b) in zip(self.end_coord, other.end_coord)
144 )
145
Tim Hall79d07d22020-04-27 18:20:16 +0100146 def get_size_shape(self):
147 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
148
149 def get_size(self):
150 return int(np.prod(self.get_size_shape()))
151
Louis Verhaard69b31762020-11-17 09:45:20 +0100152 def get_block(self) -> Block:
153 return Block.from_shape(self.get_size_shape())
154
Tim Hall79d07d22020-04-27 18:20:16 +0100155 def __str__(self):
156 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
157
158 __repr__ = __str__
159
160
Tim Hall79d07d22020-04-27 18:20:16 +0100161class Command:
Tim Hall79d07d22020-04-27 18:20:16 +0100162 def is_npu_pass_command(self):
163 return False
164
Tim Hall79d07d22020-04-27 18:20:16 +0100165 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100166 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100167 return np.array((0, 0))
168
169
170class NpuStripe(Command):
171 def __init__(
172 self,
173 ps,
174 block_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100175 is_first_h_stripe,
176 is_last_h_stripe,
177 ifm_tensor,
178 ifm_box,
179 ofm_tensor,
180 ofm_box,
181 weight_tensor=None,
182 weight_box=None,
Tim Halld784af72021-06-08 21:25:57 +0100183 scale_tensor=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100184 ifm2_tensor=None,
185 ifm2_box=None,
186 pad_top=0,
187 pad_bottom=0,
188 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100189 self.ps = ps
190 self.block_config = block_config
Tim Hall79d07d22020-04-27 18:20:16 +0100191 self.is_first_h_stripe = is_first_h_stripe
192 self.is_last_h_stripe = is_last_h_stripe
193 self.ifm_tensor = ifm_tensor
194 self.ifm_box = ifm_box
195 self.ifm2_tensor = ifm2_tensor
196 self.ifm2_box = ifm2_box
197 self.ofm_tensor = ofm_tensor
198 self.ofm_box = ofm_box
199 self.weight_tensor = weight_tensor
Tim Halld784af72021-06-08 21:25:57 +0100200 self.scale_tensor = scale_tensor
Tim Hall79d07d22020-04-27 18:20:16 +0100201 self.weight_box = weight_box
Tim Hall79d07d22020-04-27 18:20:16 +0100202 self.pad_top = pad_top
203 self.pad_bottom = pad_bottom
204 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000205 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100206
Tim Hall79d07d22020-04-27 18:20:16 +0100207 def is_npu_pass_command(self):
208 return True
209
210 def __str__(self):
211 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
212 self.ps.name,
213 self.ifm_box,
214 self.ifm2_box,
215 self.ofm_box,
216 self.weight_box,
217 self.block_config,
218 )
219
220 __repr__ = __str__
221
Tim Hall79d07d22020-04-27 18:20:16 +0100222 def get_block_dimensions(self):
223 ofm_box = self.ofm_box
224 block_config = self.block_config
225
226 out_height = 1
227 out_width = 1
228 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
229 if len(ofm_box.end_coord) >= 4:
230 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
231 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
232
233 assert out_height >= 0
234 assert out_width >= 0
235 assert out_depth >= 0
236 return (
237 round_up_divide(out_height, block_config[0]),
238 round_up_divide(out_width, block_config[1]),
239 round_up_divide(out_depth, block_config[3]),
240 )
241
242 def get_operation_count(self):
243 # returns numpy array of (DPU blocks, dma_ops)
244 return np.array((self.get_n_blocks(), 0))
245
246 def get_n_blocks(self):
247 h, w, d = self.get_block_dimensions()
248 res = h * w * d
249 assert res >= 0
250 return res
251
Tim Hall79d07d22020-04-27 18:20:16 +0100252
253class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200254 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200255 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100256 self.in_tensor = in_tensor
257 self.out_tensor = out_tensor
258 self.box = box
259
260 def __str__(self):
261 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
262
263 __repr__ = __str__
264
Tim Hall79d07d22020-04-27 18:20:16 +0100265 def get_operation_count(self):
266 # returns numpy array of (DPU blocks, dma_ops)
267 return np.array((0, 1))