blob: f9048a1e286806413231c3e8036a890c464d818f [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
24from .range_set import AccessDirection
25from .range_set import MemoryAccessSet
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(
Jacob Bohlin611fcdf2020-06-11 15:09:57 +020037 self, strides, skirt, ifm_shape, npu_block_type, concat_axis=0, concat_offset=0, split_offset=None, k_height=1,
38 upscaling_factor=1
Tim Hall79d07d22020-04-27 18:20:16 +010039 ):
40 new_start_coord = list(self.start_coord)
41 new_end_coord = list(self.end_coord)
42
Jacob Bohlin611fcdf2020-06-11 15:09:57 +020043 # Adjust for upscaling
44 if len(new_start_coord) == len(new_end_coord) == 4:
45 new_start_coord[1] = new_start_coord[1] // upscaling_factor
46 new_end_coord[1] = new_end_coord[1] // upscaling_factor
47
Tim Hall79d07d22020-04-27 18:20:16 +010048 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
Diego Russoea6111a2020-04-14 18:41:58 +010056 if split_offset is None and npu_block_type in set((NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct)):
Tim Hall79d07d22020-04-27 18:20:16 +010057 # these types of operations do a "dot product" over the entire IFM
58 new_start_coord[-1] = 0
59 new_end_coord[-1] = ifm_shape[-1]
60
Louis Verhaarde0ef2732020-06-03 08:56:44 +020061 if npu_block_type == NpuBlockType.ElementWise and min(len(new_end_coord), len(ifm_shape)) >= 1:
62 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape[-1])
Tim Hall79d07d22020-04-27 18:20:16 +010063 if min(len(new_end_coord), len(ifm_shape)) >= 2:
64 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape[-2])
65 if min(len(new_end_coord), len(ifm_shape)) >= 3:
66 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape[-3])
67
68 pad_top = 0
69 pad_bottom = 0
70 if strides is not None and skirt is not None:
71 if len(new_start_coord) >= 2:
72 stride = strides[2]
73 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
74 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape[-2])
75
76 if len(new_start_coord) >= 3:
77 stride = strides[1]
78
79 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
80 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0]
81
82 pad_top = max(0, 0 - new_start_coord[-3])
83 new_start_coord[-3] = max(new_start_coord[-3], 0)
84
85 while len(ifm_shape) < 3:
86 ifm_shape = [1] + ifm_shape
87 if (new_end_coord[-3] * stride + skirt[2]) > ifm_shape[-3]:
88 # pad_bottom is calculated based the diff between the end position of the weight kernel,
89 # after last stride and the ifm height.
90 k_start = new_start_coord[-3] - pad_top
91 pad_bottom = max(0, k_start + total_stride + k_height - ifm_shape[-3])
92
93 new_end_coord[-3] = min(new_end_coord[-3] * stride + skirt[2], ifm_shape[-3])
94
95 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
96
97 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
98 start = [0] * len(weight_shape)
99 end = list(weight_shape)
100 if oc_range_start is not None and oc_range_end is not None:
101 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
102 # input range is output range divided by channel multiplier
103 if weights_transposed:
104 start[-1] = oc_range_start // weight_shape[-2]
105 end[-1] = oc_range_end // weight_shape[-2]
106 else:
107 start[-2] = oc_range_start // weight_shape[-1]
108 end[-2] = oc_range_end // weight_shape[-1]
109 else:
110 start[-1] = oc_range_start
111 end[-1] = oc_range_end
112 for i in range(len(end)):
113 assert 0 <= start[i] < weight_shape[i]
114 assert 0 < end[i] <= weight_shape[i]
115
116 return Box(start, end)
117
118 def get_size_shape(self):
119 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
120
121 def get_size(self):
122 return int(np.prod(self.get_size_shape()))
123
124 def __str__(self):
125 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
126
127 __repr__ = __str__
128
129
130class CommandType(IntEnum):
131 NpuStripe = 0
132 DMA = 1
133 Size = 2
134
135
136class Command:
137 def get_ofm_y_range_for_pass(self, ps_requested):
138 return None
139
140 def is_npu_pass_command(self):
141 return False
142
143 def get_memory_accesses(self):
144 return None
145
146 def get_operation_count(self):
147 # returns numpy array of (DPU blocks, dma_ops). Should line up with the CommandType enum
148 return np.array((0, 0))
149
150
151class NpuStripe(Command):
152 def __init__(
153 self,
154 ps,
155 block_config,
156 is_first,
157 is_last,
158 is_first_h_stripe,
159 is_last_h_stripe,
160 ifm_tensor,
161 ifm_box,
162 ofm_tensor,
163 ofm_box,
164 weight_tensor=None,
165 weight_box=None,
166 scale_tensor=None,
167 concat_axis=0,
168 concat_offset=0,
169 ifm2_tensor=None,
170 ifm2_box=None,
171 pad_top=0,
172 pad_bottom=0,
173 ):
174 self.cmdtype = CommandType.NpuStripe
175 self.ps = ps
176 self.block_config = block_config
177 self.is_first = is_first
178 self.is_last = is_last
179 self.is_first_h_stripe = is_first_h_stripe
180 self.is_last_h_stripe = is_last_h_stripe
181 self.ifm_tensor = ifm_tensor
182 self.ifm_box = ifm_box
183 self.ifm2_tensor = ifm2_tensor
184 self.ifm2_box = ifm2_box
185 self.ofm_tensor = ofm_tensor
186 self.ofm_box = ofm_box
187 self.weight_tensor = weight_tensor
188 self.scale_tensor = scale_tensor
189 self.weight_box = weight_box
190 self.concat_axis = concat_axis
191 self.concat_offset = concat_offset
192 self.pad_top = pad_top
193 self.pad_bottom = pad_bottom
194 for i in range(len(self.ofm_box.end_coord)):
195 assert self.ofm_box.end_coord[i] <= self.ofm_tensor.shape[i]
196
197 def get_memory_accesses(self):
198 res = MemoryAccessSet()
199 if self.ifm_tensor is not None and self.ifm_tensor.shape != []:
200 res.add(
201 self.ifm_tensor.get_address_ranges_for_coordinates(self.ifm_box.start_coord, self.ifm_box.end_coord),
202 AccessDirection.Read,
203 )
204 if self.ifm2_tensor is not None and self.ifm2_tensor.shape != []:
205 res.add(
206 self.ifm2_tensor.get_address_ranges_for_coordinates(self.ifm2_box.start_coord, self.ifm2_box.end_coord),
207 AccessDirection.Read,
208 )
209 if self.ofm_tensor is not None:
210 res.add(
211 self.ofm_tensor.get_address_ranges_for_coordinates(self.ofm_box.start_coord, self.ofm_box.end_coord),
212 AccessDirection.Write,
213 )
214 if self.weight_tensor is not None:
215 res.add(
216 self.weight_tensor.get_address_ranges_for_coordinates(
217 self.weight_box.start_coord, self.weight_box.end_coord
218 ),
219 AccessDirection.Read,
220 )
221 return res
222
223 def is_npu_pass_command(self):
224 return True
225
226 def __str__(self):
227 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
228 self.ps.name,
229 self.ifm_box,
230 self.ifm2_box,
231 self.ofm_box,
232 self.weight_box,
233 self.block_config,
234 )
235
236 __repr__ = __str__
237
238 def get_ofm_y_range_for_pass(self, ps_requested):
239 if ps_requested != self.ps:
240 return None
241 if len(self.ofm_box.start_coord) >= 3:
242 return (self.ofm_box.start_coord[-3], self.ofm_box.end_coord[-3])
243 return None
244
245 def get_block_dimensions(self):
246 ofm_box = self.ofm_box
247 block_config = self.block_config
248
249 out_height = 1
250 out_width = 1
251 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
252 if len(ofm_box.end_coord) >= 4:
253 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
254 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
255
256 assert out_height >= 0
257 assert out_width >= 0
258 assert out_depth >= 0
259 return (
260 round_up_divide(out_height, block_config[0]),
261 round_up_divide(out_width, block_config[1]),
262 round_up_divide(out_depth, block_config[3]),
263 )
264
265 def get_operation_count(self):
266 # returns numpy array of (DPU blocks, dma_ops)
267 return np.array((self.get_n_blocks(), 0))
268
269 def get_n_blocks(self):
270 h, w, d = self.get_block_dimensions()
271 res = h * w * d
272 assert res >= 0
273 return res
274
275 def get_single_block_command(self, block_idx):
276 block_cfg = (self.block_config[0], self.block_config[1], self.block_config[3])
277 dims = self.get_block_dimensions()
278 strides = dims[1] * dims[2], dims[2], 1
279 coord = []
280 idx_left = block_idx
281 for s in strides:
282 c = idx_left // s
283 idx_left -= c * s
284 coord.append(c)
285
286 assert idx_left == 0
287
288 # put in dummy height/widths in case we're dealing with FC layers
289 ofm_start = list(self.ofm_box.start_coord)
290 ofm_end = list(self.ofm_box.end_coord)
291
292 # cut out a nice block shape
293 for idx in (-1, -2, -3):
294 if len(ofm_start) >= -idx:
295 ofm_start[idx] += block_cfg[idx] * coord[idx]
296 ofm_end[idx] = min(ofm_end[idx], ofm_start[idx] + block_cfg[idx])
297
298 ps = self.ps
299 strides = None
300 skirt = None
301 if ps.primary_op is not None:
302 strides = ps.primary_op.attrs.get("strides", None)
303 skirt = ps.primary_op.attrs.get("skirt", None)
304 npu_block_type = ps.npu_block_type
305
306 ofm_box = Box(ofm_start, ofm_end)
307 ifm_box, _, _ = ofm_box.transform_with_strides_and_skirt(
308 strides, skirt, self.ifm_tensor.shape, npu_block_type, self.concat_axis, self.concat_offset
309 )
310
311 weight_box = None
312 if self.weight_tensor is not None:
313 weight_oc_start = ofm_start[-1]
314 weight_oc_end = ofm_end[-1]
315 if self.concat_axis - len(self.weight_tensor.shape) == -1:
316 weight_oc_start -= self.concat_offset
317 weight_oc_end -= self.concat_offset
318
319 weight_box = Box.make_weight_box(
320 self.weight_tensor.shape,
321 npu_block_type,
322 weight_oc_start,
323 weight_oc_end,
324 self.weight_tensor.weight_transpose_depthwise,
325 )
326
327 return NpuStripe(
328 self.ps,
329 self.block_config,
330 self.is_first,
331 self.is_last,
332 self.is_first_h_stripe,
333 self.is_last_h_stripe,
334 self.ifm_tensor,
335 ifm_box,
336 self.ofm_tensor,
337 ofm_box,
338 self.weight_tensor,
339 weight_box,
340 self.scale_tensor,
341 self.concat_axis,
342 self.concat_offset,
343 )
344
345
346class DMA(Command):
347 def __init__(self, in_tensor, out_tensor, box):
348 self.cmdtype = CommandType.DMA
349 self.in_tensor = in_tensor
350 self.out_tensor = out_tensor
351 self.box = box
352
353 def __str__(self):
354 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
355
356 __repr__ = __str__
357
358 def get_memory_accesses(self):
359 res = MemoryAccessSet()
360
361 res.add(
362 self.in_tensor.get_address_ranges_for_coordinates(self.box.start_coord, self.box.end_coord),
363 AccessDirection.Read,
364 )
365 res.add(
366 self.out_tensor.get_address_ranges_for_coordinates(self.box.start_coord, self.box.end_coord),
367 AccessDirection.Write,
368 )
369 return res
370
371 def get_operation_count(self):
372 # returns numpy array of (DPU blocks, dma_ops)
373 return np.array((0, 1))