blob: 4a41edd0150f98af271515b36cf9268b6948df45 [file] [log] [blame]
Patrik Gustavssone3b1b912021-02-09 15:38:46 +01001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
Tim Hall79d07d22020-04-27 18:20:16 +01002#
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).
patrik.gustavssoneeb85152020-12-21 17:10:40 +000018from typing import List
Jonas Ohlsson845e2322022-03-01 12:39:55 +010019from typing import Optional
patrik.gustavssoneeb85152020-12-21 17:10:40 +000020
Tim Hall79d07d22020-04-27 18:20:16 +010021import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010022
Louis Verhaard69b31762020-11-17 09:45:20 +010023from .architecture_features import Block
Tim Hall79d07d22020-04-27 18:20:16 +010024from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010025from .operation import NpuBlockType
patrik.gustavssoneeb85152020-12-21 17:10:40 +000026from .shape4d import Shape4D
Tim Hall79d07d22020-04-27 18:20:16 +010027
28
29class Box:
30 def __init__(self, start_coord, end_coord):
31 self.start_coord = list(start_coord)
32 self.end_coord = list(end_coord)
33 assert len(self.start_coord) == len(end_coord)
34 for i in range(len(self.start_coord)):
35 assert self.start_coord[i] <= self.end_coord[i]
36
erik.andersson@arm.com6b2a0b42022-03-22 15:35:30 +010037 @staticmethod
38 def wrap(a, b):
39 """Wrap broadcasted tensor boxes in order to
40 prevent out of bounds during box creation"""
41 tmp = [0, 0, 0, 0]
42 for i, val in enumerate(a):
43 if int(val) != 0:
44 tmp[i] = a[i]
45 if a[i] >= b[i] and b[i] != 0:
46 tmp[i] = a[i] % b[i]
47 return Shape4D(tmp)
48
Tim Hall79d07d22020-04-27 18:20:16 +010049 def transform_with_strides_and_skirt(
Tim Hallc30f4952020-06-15 20:47:35 +010050 self,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000051 strides: List[int],
52 skirt: List[int],
53 ifm_shape: Shape4D,
54 npu_block_type: NpuBlockType,
Louis Verhaardc822d622021-03-11 14:59:06 +010055 concat_offsets: List[int],
Rickard Bolin1c08afa2022-01-07 14:22:52 +000056 k_dilated_height: int,
Jonas Ohlsson845e2322022-03-01 12:39:55 +010057 split_offset: Optional[Shape4D] = None,
58 split_shape: Optional[Shape4D] = None,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000059 upscaling_factor: int = 1,
erik.andersson@arm.com6b2a0b42022-03-22 15:35:30 +010060 op_type=None,
Tim Hall79d07d22020-04-27 18:20:16 +010061 ):
62 new_start_coord = list(self.start_coord)
63 new_end_coord = list(self.end_coord)
64
Louis Verhaardc822d622021-03-11 14:59:06 +010065 new_start_coord = np.subtract(new_start_coord, concat_offsets)
66 new_end_coord = np.subtract(new_end_coord, concat_offsets)
Tim Hall79d07d22020-04-27 18:20:16 +010067
Diego Russoea6111a2020-04-14 18:41:58 +010068 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010069 for idx in range(len(split_offset)):
70 new_start_coord[idx] += split_offset[idx]
71 new_end_coord[idx] += split_offset[idx]
72
Tim Halld8339a72021-05-27 18:49:40 +010073 if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020074 # these types of operations do a "dot product" or sum over the entire IFM
Tim Halld8339a72021-05-27 18:49:40 +010075 if split_offset is None:
76 new_start_coord[-1] = 0
77 new_end_coord[-1] = ifm_shape.depth
78 else:
79 new_start_coord[-1] = split_offset[-1]
80 new_end_coord[-1] = new_start_coord[-1] + split_shape[-1]
Tim Hall79d07d22020-04-27 18:20:16 +010081
Patrik Gustavssone6b94bb2021-05-05 08:30:41 +020082 if len(new_end_coord) >= 1:
patrik.gustavssoneeb85152020-12-21 17:10:40 +000083 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth)
84 if len(new_end_coord) >= 2:
85 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor)
86 if len(new_end_coord) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020087 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000088 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010089
90 pad_top = 0
91 pad_bottom = 0
92 if strides is not None and skirt is not None:
93 if len(new_start_coord) >= 2:
94 stride = strides[2]
Tim Hall3751aa42021-12-16 13:17:29 +000095 # if the current op was combined with a split slice read then the valid ifm range is given by the output
Tim Hall51a8dce2021-12-20 16:49:27 +000096 # of the split op (which is defined by the read offset and the read shape)
Tim Hall3751aa42021-12-16 13:17:29 +000097 if split_offset is None:
98 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
99 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
100 else:
101 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], split_offset[-2])
Tim Hall51a8dce2021-12-20 16:49:27 +0000102 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], split_offset[-2] + split_shape[-2])
Tim Hall79d07d22020-04-27 18:20:16 +0100103
104 if len(new_start_coord) >= 3:
105 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200106 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +0100107
108 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200109 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +0100110
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200111 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +0100112 new_start_coord[-3] = max(new_start_coord[-3], 0)
113
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000114 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +0100115 # pad_bottom is calculated based the diff between the end position of the weight kernel,
116 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000117 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200118 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000119 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200120 else:
121 k_start = new_start_coord[-3] - pad_top
Rickard Bolin1c08afa2022-01-07 14:22:52 +0000122 pad_bottom = max(
123 0, k_start + total_stride + k_dilated_height - (ifm_shape.height * upscaling_factor)
124 )
Tim Hall79d07d22020-04-27 18:20:16 +0100125
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200126 # Adjust for upscaling
127 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
128 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000129 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100130
erik.andersson@arm.com6b2a0b42022-03-22 15:35:30 +0100131 # Wrap the IFMs of broadcasted binary elementwise ops
132 # at the limits of the non-broadcasted volumes
133 # Non-broadcasted ops aren't affected by the wrapping
134 if op_type is not None and op_type.is_binary_elementwise_op():
135 tmp = list(ifm_shape)
136 one = Shape4D(1, 1, 1, 1)
137 new_start_coord = Box.wrap(new_start_coord, tmp)
138 new_end_coord = Box.wrap(Shape4D(list(new_end_coord)) - one, tmp) + one
139
Tim Hall79d07d22020-04-27 18:20:16 +0100140 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
141
142 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
143 start = [0] * len(weight_shape)
144 end = list(weight_shape)
145 if oc_range_start is not None and oc_range_end is not None:
146 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
147 # input range is output range divided by channel multiplier
148 if weights_transposed:
149 start[-1] = oc_range_start // weight_shape[-2]
150 end[-1] = oc_range_end // weight_shape[-2]
151 else:
152 start[-2] = oc_range_start // weight_shape[-1]
153 end[-2] = oc_range_end // weight_shape[-1]
154 else:
155 start[-1] = oc_range_start
156 end[-1] = oc_range_end
157 for i in range(len(end)):
158 assert 0 <= start[i] < weight_shape[i]
159 assert 0 < end[i] <= weight_shape[i]
160
161 return Box(start, end)
162
Tim Halld8339a72021-05-27 18:49:40 +0100163 def is_subbox_of(self, other):
164 if self.start_coord and self.end_coord:
165 assert len(self.start_coord) == len(other.start_coord)
166 assert len(self.end_coord) == len(other.end_coord)
167 return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all(
168 a <= b for (a, b) in zip(self.end_coord, other.end_coord)
169 )
170
Tim Hall79d07d22020-04-27 18:20:16 +0100171 def get_size_shape(self):
172 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
173
174 def get_size(self):
175 return int(np.prod(self.get_size_shape()))
176
Louis Verhaard69b31762020-11-17 09:45:20 +0100177 def get_block(self) -> Block:
178 return Block.from_shape(self.get_size_shape())
179
Tim Hall79d07d22020-04-27 18:20:16 +0100180 def __str__(self):
181 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
182
183 __repr__ = __str__
184
185
Tim Hall79d07d22020-04-27 18:20:16 +0100186class Command:
Tim Hall79d07d22020-04-27 18:20:16 +0100187 def is_npu_pass_command(self):
188 return False
189
Tim Hall79d07d22020-04-27 18:20:16 +0100190 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100191 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100192 return np.array((0, 0))
193
194
195class NpuStripe(Command):
196 def __init__(
197 self,
198 ps,
199 block_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100200 is_first_h_stripe,
201 is_last_h_stripe,
202 ifm_tensor,
203 ifm_box,
204 ofm_tensor,
205 ofm_box,
206 weight_tensor=None,
207 weight_box=None,
Tim Halld784af72021-06-08 21:25:57 +0100208 scale_tensor=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100209 ifm2_tensor=None,
210 ifm2_box=None,
211 pad_top=0,
212 pad_bottom=0,
213 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100214 self.ps = ps
215 self.block_config = block_config
Tim Hall79d07d22020-04-27 18:20:16 +0100216 self.is_first_h_stripe = is_first_h_stripe
217 self.is_last_h_stripe = is_last_h_stripe
218 self.ifm_tensor = ifm_tensor
219 self.ifm_box = ifm_box
220 self.ifm2_tensor = ifm2_tensor
221 self.ifm2_box = ifm2_box
222 self.ofm_tensor = ofm_tensor
223 self.ofm_box = ofm_box
224 self.weight_tensor = weight_tensor
Tim Halld784af72021-06-08 21:25:57 +0100225 self.scale_tensor = scale_tensor
Tim Hall79d07d22020-04-27 18:20:16 +0100226 self.weight_box = weight_box
Tim Hall79d07d22020-04-27 18:20:16 +0100227 self.pad_top = pad_top
228 self.pad_bottom = pad_bottom
229 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000230 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100231
Tim Hall79d07d22020-04-27 18:20:16 +0100232 def is_npu_pass_command(self):
233 return True
234
235 def __str__(self):
236 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
237 self.ps.name,
238 self.ifm_box,
239 self.ifm2_box,
240 self.ofm_box,
241 self.weight_box,
242 self.block_config,
243 )
244
245 __repr__ = __str__
246
Tim Hall79d07d22020-04-27 18:20:16 +0100247 def get_block_dimensions(self):
248 ofm_box = self.ofm_box
249 block_config = self.block_config
250
251 out_height = 1
252 out_width = 1
253 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
254 if len(ofm_box.end_coord) >= 4:
255 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
256 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
257
258 assert out_height >= 0
259 assert out_width >= 0
260 assert out_depth >= 0
261 return (
262 round_up_divide(out_height, block_config[0]),
263 round_up_divide(out_width, block_config[1]),
264 round_up_divide(out_depth, block_config[3]),
265 )
266
267 def get_operation_count(self):
268 # returns numpy array of (DPU blocks, dma_ops)
269 return np.array((self.get_n_blocks(), 0))
270
271 def get_n_blocks(self):
272 h, w, d = self.get_block_dimensions()
273 res = h * w * d
274 assert res >= 0
275 return res
276
Tim Hall79d07d22020-04-27 18:20:16 +0100277
278class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200279 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200280 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100281 self.in_tensor = in_tensor
282 self.out_tensor = out_tensor
283 self.box = box
284
285 def __str__(self):
286 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
287
288 __repr__ = __str__
289
Tim Hall79d07d22020-04-27 18:20:16 +0100290 def get_operation_count(self):
291 # returns numpy array of (DPU blocks, dma_ops)
292 return np.array((0, 1))