blob: c45bc4e59c2450d6eca0409d9d17a5cf0775a4e7 [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).
Tim Hall79d07d22020-04-27 18:20:16 +010018import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010019
Louis Verhaard69b31762020-11-17 09:45:20 +010020from .architecture_features import Block
Tim Hall79d07d22020-04-27 18:20:16 +010021from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010022from .operation import NpuBlockType
Tim Hall79d07d22020-04-27 18:20:16 +010023
24
25class Box:
26 def __init__(self, start_coord, end_coord):
27 self.start_coord = list(start_coord)
28 self.end_coord = list(end_coord)
29 assert len(self.start_coord) == len(end_coord)
30 for i in range(len(self.start_coord)):
31 assert self.start_coord[i] <= self.end_coord[i]
32
33 def transform_with_strides_and_skirt(
Tim Hallc30f4952020-06-15 20:47:35 +010034 self,
35 strides,
36 skirt,
37 ifm_shape,
38 npu_block_type,
39 concat_axis=0,
40 concat_offset=0,
41 split_offset=None,
42 k_height=1,
43 upscaling_factor=1,
Tim Hall79d07d22020-04-27 18:20:16 +010044 ):
45 new_start_coord = list(self.start_coord)
46 new_end_coord = list(self.end_coord)
47
48 new_start_coord[concat_axis] -= concat_offset
49 new_end_coord[concat_axis] -= concat_offset
50
Diego Russoea6111a2020-04-14 18:41:58 +010051 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010052 for idx in range(len(split_offset)):
53 new_start_coord[idx] += split_offset[idx]
54 new_end_coord[idx] += split_offset[idx]
55
Michael McGeaghf3e3ad72020-12-02 12:39:03 +000056 if (split_offset is None) and (
57 npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum)
Fredrik Svedberga0c36242020-06-03 15:43:31 +020058 ):
59 # these types of operations do a "dot product" or sum over the entire IFM
Tim Hall79d07d22020-04-27 18:20:16 +010060 new_start_coord[-1] = 0
61 new_end_coord[-1] = ifm_shape[-1]
62
Louis Verhaarde0ef2732020-06-03 08:56:44 +020063 if npu_block_type == NpuBlockType.ElementWise and min(len(new_end_coord), len(ifm_shape)) >= 1:
64 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape[-1])
Tim Hall79d07d22020-04-27 18:20:16 +010065 if min(len(new_end_coord), len(ifm_shape)) >= 2:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020066 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape[-2] * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010067 if min(len(new_end_coord), len(ifm_shape)) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020068 original_end_coord = list(new_end_coord)
69 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape[-3] * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010070
71 pad_top = 0
72 pad_bottom = 0
73 if strides is not None and skirt is not None:
74 if len(new_start_coord) >= 2:
75 stride = strides[2]
76 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
77 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape[-2])
78
79 if len(new_start_coord) >= 3:
80 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020081 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +010082
83 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020084 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010085
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020086 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010087 new_start_coord[-3] = max(new_start_coord[-3], 0)
88
89 while len(ifm_shape) < 3:
90 ifm_shape = [1] + ifm_shape
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020091
92 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape[-3] * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +010093 # pad_bottom is calculated based the diff between the end position of the weight kernel,
94 # after last stride and the ifm height.
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020095 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape[-3] * upscaling_factor:
96 # Special case for Transpose Convolution with VALID padding.
97 pad_bottom = original_end_coord[-3] - (ifm_shape[-3] * upscaling_factor)
98 else:
99 k_start = new_start_coord[-3] - pad_top
100 pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape[-3] * upscaling_factor))
Tim Hall79d07d22020-04-27 18:20:16 +0100101
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200102 # Adjust for upscaling
103 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
104 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
Patrik Gustavsson34b9dc12020-11-25 13:41:22 +0100105 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape[-3]), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100106
107 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
108
109 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
110 start = [0] * len(weight_shape)
111 end = list(weight_shape)
112 if oc_range_start is not None and oc_range_end is not None:
113 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
114 # input range is output range divided by channel multiplier
115 if weights_transposed:
116 start[-1] = oc_range_start // weight_shape[-2]
117 end[-1] = oc_range_end // weight_shape[-2]
118 else:
119 start[-2] = oc_range_start // weight_shape[-1]
120 end[-2] = oc_range_end // weight_shape[-1]
121 else:
122 start[-1] = oc_range_start
123 end[-1] = oc_range_end
124 for i in range(len(end)):
125 assert 0 <= start[i] < weight_shape[i]
126 assert 0 < end[i] <= weight_shape[i]
127
128 return Box(start, end)
129
130 def get_size_shape(self):
131 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
132
133 def get_size(self):
134 return int(np.prod(self.get_size_shape()))
135
Louis Verhaard69b31762020-11-17 09:45:20 +0100136 def get_block(self) -> Block:
137 return Block.from_shape(self.get_size_shape())
138
Tim Hall79d07d22020-04-27 18:20:16 +0100139 def __str__(self):
140 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
141
142 __repr__ = __str__
143
144
Tim Hall79d07d22020-04-27 18:20:16 +0100145class Command:
146 def get_ofm_y_range_for_pass(self, ps_requested):
147 return None
148
149 def is_npu_pass_command(self):
150 return False
151
Tim Hall79d07d22020-04-27 18:20:16 +0100152 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100153 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100154 return np.array((0, 0))
155
156
157class NpuStripe(Command):
158 def __init__(
159 self,
160 ps,
161 block_config,
162 is_first,
163 is_last,
164 is_first_h_stripe,
165 is_last_h_stripe,
166 ifm_tensor,
167 ifm_box,
168 ofm_tensor,
169 ofm_box,
170 weight_tensor=None,
171 weight_box=None,
172 scale_tensor=None,
173 concat_axis=0,
174 concat_offset=0,
175 ifm2_tensor=None,
176 ifm2_box=None,
177 pad_top=0,
178 pad_bottom=0,
179 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100180 self.ps = ps
181 self.block_config = block_config
182 self.is_first = is_first
183 self.is_last = is_last
184 self.is_first_h_stripe = is_first_h_stripe
185 self.is_last_h_stripe = is_last_h_stripe
186 self.ifm_tensor = ifm_tensor
187 self.ifm_box = ifm_box
188 self.ifm2_tensor = ifm2_tensor
189 self.ifm2_box = ifm2_box
190 self.ofm_tensor = ofm_tensor
191 self.ofm_box = ofm_box
192 self.weight_tensor = weight_tensor
193 self.scale_tensor = scale_tensor
194 self.weight_box = weight_box
195 self.concat_axis = concat_axis
196 self.concat_offset = concat_offset
197 self.pad_top = pad_top
198 self.pad_bottom = pad_bottom
199 for i in range(len(self.ofm_box.end_coord)):
200 assert self.ofm_box.end_coord[i] <= self.ofm_tensor.shape[i]
201
Tim Hall79d07d22020-04-27 18:20:16 +0100202 def is_npu_pass_command(self):
203 return True
204
205 def __str__(self):
206 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
207 self.ps.name,
208 self.ifm_box,
209 self.ifm2_box,
210 self.ofm_box,
211 self.weight_box,
212 self.block_config,
213 )
214
215 __repr__ = __str__
216
217 def get_ofm_y_range_for_pass(self, ps_requested):
218 if ps_requested != self.ps:
219 return None
220 if len(self.ofm_box.start_coord) >= 3:
221 return (self.ofm_box.start_coord[-3], self.ofm_box.end_coord[-3])
222 return None
223
224 def get_block_dimensions(self):
225 ofm_box = self.ofm_box
226 block_config = self.block_config
227
228 out_height = 1
229 out_width = 1
230 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
231 if len(ofm_box.end_coord) >= 4:
232 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
233 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
234
235 assert out_height >= 0
236 assert out_width >= 0
237 assert out_depth >= 0
238 return (
239 round_up_divide(out_height, block_config[0]),
240 round_up_divide(out_width, block_config[1]),
241 round_up_divide(out_depth, block_config[3]),
242 )
243
244 def get_operation_count(self):
245 # returns numpy array of (DPU blocks, dma_ops)
246 return np.array((self.get_n_blocks(), 0))
247
248 def get_n_blocks(self):
249 h, w, d = self.get_block_dimensions()
250 res = h * w * d
251 assert res >= 0
252 return res
253
254 def get_single_block_command(self, block_idx):
255 block_cfg = (self.block_config[0], self.block_config[1], self.block_config[3])
256 dims = self.get_block_dimensions()
257 strides = dims[1] * dims[2], dims[2], 1
258 coord = []
259 idx_left = block_idx
260 for s in strides:
261 c = idx_left // s
262 idx_left -= c * s
263 coord.append(c)
264
265 assert idx_left == 0
266
267 # put in dummy height/widths in case we're dealing with FC layers
268 ofm_start = list(self.ofm_box.start_coord)
269 ofm_end = list(self.ofm_box.end_coord)
270
271 # cut out a nice block shape
272 for idx in (-1, -2, -3):
273 if len(ofm_start) >= -idx:
274 ofm_start[idx] += block_cfg[idx] * coord[idx]
275 ofm_end[idx] = min(ofm_end[idx], ofm_start[idx] + block_cfg[idx])
276
277 ps = self.ps
278 strides = None
279 skirt = None
280 if ps.primary_op is not None:
281 strides = ps.primary_op.attrs.get("strides", None)
282 skirt = ps.primary_op.attrs.get("skirt", None)
283 npu_block_type = ps.npu_block_type
284
285 ofm_box = Box(ofm_start, ofm_end)
286 ifm_box, _, _ = ofm_box.transform_with_strides_and_skirt(
287 strides, skirt, self.ifm_tensor.shape, npu_block_type, self.concat_axis, self.concat_offset
288 )
289
290 weight_box = None
291 if self.weight_tensor is not None:
292 weight_oc_start = ofm_start[-1]
293 weight_oc_end = ofm_end[-1]
294 if self.concat_axis - len(self.weight_tensor.shape) == -1:
295 weight_oc_start -= self.concat_offset
296 weight_oc_end -= self.concat_offset
297
298 weight_box = Box.make_weight_box(
299 self.weight_tensor.shape,
300 npu_block_type,
301 weight_oc_start,
302 weight_oc_end,
303 self.weight_tensor.weight_transpose_depthwise,
304 )
305
306 return NpuStripe(
307 self.ps,
308 self.block_config,
309 self.is_first,
310 self.is_last,
311 self.is_first_h_stripe,
312 self.is_last_h_stripe,
313 self.ifm_tensor,
314 ifm_box,
315 self.ofm_tensor,
316 ofm_box,
317 self.weight_tensor,
318 weight_box,
319 self.scale_tensor,
320 self.concat_axis,
321 self.concat_offset,
322 )
323
324
325class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200326 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200327 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100328 self.in_tensor = in_tensor
329 self.out_tensor = out_tensor
330 self.box = box
331
332 def __str__(self):
333 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
334
335 __repr__ = __str__
336
Tim Hall79d07d22020-04-27 18:20:16 +0100337 def get_operation_count(self):
338 # returns numpy array of (DPU blocks, dma_ops)
339 return np.array((0, 1))