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