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