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