blob: 09c1805dfdd2fa6420503f7c16ba7fd412ef020f [file] [log] [blame]
Johan Alfven90724962023-02-02 09:07:48 +01001# SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Tim Hall79d07d22020-04-27 18:20:16 +010017# Description:
18# 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 +000019from typing import List
Jonas Ohlsson845e2322022-03-01 12:39:55 +010020from typing import Optional
patrik.gustavssoneeb85152020-12-21 17:10:40 +000021
Tim Hall79d07d22020-04-27 18:20:16 +010022import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010023
Louis Verhaard69b31762020-11-17 09:45:20 +010024from .architecture_features import Block
Tim Hall79d07d22020-04-27 18:20:16 +010025from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010026from .operation import NpuBlockType
patrik.gustavssoneeb85152020-12-21 17:10:40 +000027from .shape4d import Shape4D
Tim Hall79d07d22020-04-27 18:20:16 +010028
29
30class Box:
31 def __init__(self, start_coord, end_coord):
32 self.start_coord = list(start_coord)
33 self.end_coord = list(end_coord)
34 assert len(self.start_coord) == len(end_coord)
35 for i in range(len(self.start_coord)):
36 assert self.start_coord[i] <= self.end_coord[i]
37
erik.andersson@arm.com6b2a0b42022-03-22 15:35:30 +010038 @staticmethod
39 def wrap(a, b):
40 """Wrap broadcasted tensor boxes in order to
41 prevent out of bounds during box creation"""
42 tmp = [0, 0, 0, 0]
43 for i, val in enumerate(a):
44 if int(val) != 0:
45 tmp[i] = a[i]
46 if a[i] >= b[i] and b[i] != 0:
47 tmp[i] = a[i] % b[i]
48 return Shape4D(tmp)
49
Tim Hall79d07d22020-04-27 18:20:16 +010050 def transform_with_strides_and_skirt(
Tim Hallc30f4952020-06-15 20:47:35 +010051 self,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000052 strides: List[int],
53 skirt: List[int],
54 ifm_shape: Shape4D,
55 npu_block_type: NpuBlockType,
Louis Verhaardc822d622021-03-11 14:59:06 +010056 concat_offsets: List[int],
Rickard Bolin1c08afa2022-01-07 14:22:52 +000057 k_dilated_height: int,
Jonas Ohlsson845e2322022-03-01 12:39:55 +010058 split_offset: Optional[Shape4D] = None,
59 split_shape: Optional[Shape4D] = None,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000060 upscaling_factor: int = 1,
erik.andersson@arm.com6b2a0b42022-03-22 15:35:30 +010061 op_type=None,
Tim Hall79d07d22020-04-27 18:20:16 +010062 ):
63 new_start_coord = list(self.start_coord)
64 new_end_coord = list(self.end_coord)
65
Louis Verhaardc822d622021-03-11 14:59:06 +010066 new_start_coord = np.subtract(new_start_coord, concat_offsets)
67 new_end_coord = np.subtract(new_end_coord, concat_offsets)
Tim Hall79d07d22020-04-27 18:20:16 +010068
Diego Russoea6111a2020-04-14 18:41:58 +010069 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010070 for idx in range(len(split_offset)):
71 new_start_coord[idx] += split_offset[idx]
72 new_end_coord[idx] += split_offset[idx]
73
Tim Halld8339a72021-05-27 18:49:40 +010074 if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020075 # these types of operations do a "dot product" or sum over the entire IFM
Tim Halld8339a72021-05-27 18:49:40 +010076 if split_offset is None:
77 new_start_coord[-1] = 0
78 new_end_coord[-1] = ifm_shape.depth
79 else:
80 new_start_coord[-1] = split_offset[-1]
81 new_end_coord[-1] = new_start_coord[-1] + split_shape[-1]
Tim Hall79d07d22020-04-27 18:20:16 +010082
Patrik Gustavssone6b94bb2021-05-05 08:30:41 +020083 if len(new_end_coord) >= 1:
patrik.gustavssoneeb85152020-12-21 17:10:40 +000084 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth)
85 if len(new_end_coord) >= 2:
86 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor)
87 if len(new_end_coord) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020088 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000089 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010090
91 pad_top = 0
92 pad_bottom = 0
93 if strides is not None and skirt is not None:
94 if len(new_start_coord) >= 2:
95 stride = strides[2]
Tim Hall3751aa42021-12-16 13:17:29 +000096 # 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 +000097 # of the split op (which is defined by the read offset and the read shape)
Tim Hall3751aa42021-12-16 13:17:29 +000098 if split_offset is None:
99 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
100 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
101 else:
102 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], split_offset[-2])
Tim Hall51a8dce2021-12-20 16:49:27 +0000103 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 +0100104
105 if len(new_start_coord) >= 3:
106 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200107 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +0100108
109 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200110 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +0100111
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200112 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +0100113 new_start_coord[-3] = max(new_start_coord[-3], 0)
114
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000115 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +0100116 # pad_bottom is calculated based the diff between the end position of the weight kernel,
117 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000118 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200119 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000120 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200121 else:
122 k_start = new_start_coord[-3] - pad_top
Rickard Bolin1c08afa2022-01-07 14:22:52 +0000123 pad_bottom = max(
124 0, k_start + total_stride + k_dilated_height - (ifm_shape.height * upscaling_factor)
125 )
Tim Hall79d07d22020-04-27 18:20:16 +0100126
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200127 # Adjust for upscaling
128 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
129 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000130 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100131
erik.andersson@arm.com6b2a0b42022-03-22 15:35:30 +0100132 # Wrap the IFMs of broadcasted binary elementwise ops
133 # at the limits of the non-broadcasted volumes
134 # Non-broadcasted ops aren't affected by the wrapping
135 if op_type is not None and op_type.is_binary_elementwise_op():
136 tmp = list(ifm_shape)
137 one = Shape4D(1, 1, 1, 1)
138 new_start_coord = Box.wrap(new_start_coord, tmp)
139 new_end_coord = Box.wrap(Shape4D(list(new_end_coord)) - one, tmp) + one
140
Tim Hall79d07d22020-04-27 18:20:16 +0100141 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
142
143 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
144 start = [0] * len(weight_shape)
145 end = list(weight_shape)
146 if oc_range_start is not None and oc_range_end is not None:
147 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
148 # input range is output range divided by channel multiplier
149 if weights_transposed:
150 start[-1] = oc_range_start // weight_shape[-2]
151 end[-1] = oc_range_end // weight_shape[-2]
152 else:
153 start[-2] = oc_range_start // weight_shape[-1]
154 end[-2] = oc_range_end // weight_shape[-1]
155 else:
156 start[-1] = oc_range_start
157 end[-1] = oc_range_end
158 for i in range(len(end)):
159 assert 0 <= start[i] < weight_shape[i]
160 assert 0 < end[i] <= weight_shape[i]
161
162 return Box(start, end)
163
Tim Halld8339a72021-05-27 18:49:40 +0100164 def is_subbox_of(self, other):
165 if self.start_coord and self.end_coord:
166 assert len(self.start_coord) == len(other.start_coord)
167 assert len(self.end_coord) == len(other.end_coord)
168 return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all(
169 a <= b for (a, b) in zip(self.end_coord, other.end_coord)
170 )
171
Tim Hall79d07d22020-04-27 18:20:16 +0100172 def get_size_shape(self):
173 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
174
175 def get_size(self):
176 return int(np.prod(self.get_size_shape()))
177
Louis Verhaard69b31762020-11-17 09:45:20 +0100178 def get_block(self) -> Block:
179 return Block.from_shape(self.get_size_shape())
180
Tim Hall79d07d22020-04-27 18:20:16 +0100181 def __str__(self):
182 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
183
184 __repr__ = __str__
185
186
Tim Hall79d07d22020-04-27 18:20:16 +0100187class Command:
Tim Hall79d07d22020-04-27 18:20:16 +0100188 def is_npu_pass_command(self):
189 return False
190
Tim Hall79d07d22020-04-27 18:20:16 +0100191 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100192 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100193 return np.array((0, 0))
194
195
196class NpuStripe(Command):
197 def __init__(
198 self,
199 ps,
200 block_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100201 is_first_h_stripe,
202 is_last_h_stripe,
203 ifm_tensor,
204 ifm_box,
205 ofm_tensor,
206 ofm_box,
207 weight_tensor=None,
208 weight_box=None,
Tim Halld784af72021-06-08 21:25:57 +0100209 scale_tensor=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100210 ifm2_tensor=None,
211 ifm2_box=None,
212 pad_top=0,
213 pad_bottom=0,
Fredrik Svedbergb81e1bb2022-10-11 21:50:51 +0200214 reversed_operands=False,
Tim Hall79d07d22020-04-27 18:20:16 +0100215 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100216 self.ps = ps
217 self.block_config = block_config
Tim Hall79d07d22020-04-27 18:20:16 +0100218 self.is_first_h_stripe = is_first_h_stripe
219 self.is_last_h_stripe = is_last_h_stripe
220 self.ifm_tensor = ifm_tensor
221 self.ifm_box = ifm_box
222 self.ifm2_tensor = ifm2_tensor
223 self.ifm2_box = ifm2_box
224 self.ofm_tensor = ofm_tensor
225 self.ofm_box = ofm_box
226 self.weight_tensor = weight_tensor
Tim Halld784af72021-06-08 21:25:57 +0100227 self.scale_tensor = scale_tensor
Tim Hall79d07d22020-04-27 18:20:16 +0100228 self.weight_box = weight_box
Tim Hall79d07d22020-04-27 18:20:16 +0100229 self.pad_top = pad_top
230 self.pad_bottom = pad_bottom
Fredrik Svedbergb81e1bb2022-10-11 21:50:51 +0200231 self.reversed_operands = reversed_operands
Tim Hall79d07d22020-04-27 18:20:16 +0100232 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000233 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100234
Tim Hall79d07d22020-04-27 18:20:16 +0100235 def is_npu_pass_command(self):
236 return True
237
238 def __str__(self):
239 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
240 self.ps.name,
241 self.ifm_box,
242 self.ifm2_box,
243 self.ofm_box,
244 self.weight_box,
245 self.block_config,
246 )
247
248 __repr__ = __str__
249
Tim Hall79d07d22020-04-27 18:20:16 +0100250 def get_block_dimensions(self):
251 ofm_box = self.ofm_box
252 block_config = self.block_config
253
254 out_height = 1
255 out_width = 1
256 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
257 if len(ofm_box.end_coord) >= 4:
258 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
259 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
260
261 assert out_height >= 0
262 assert out_width >= 0
263 assert out_depth >= 0
264 return (
265 round_up_divide(out_height, block_config[0]),
266 round_up_divide(out_width, block_config[1]),
267 round_up_divide(out_depth, block_config[3]),
268 )
269
270 def get_operation_count(self):
271 # returns numpy array of (DPU blocks, dma_ops)
272 return np.array((self.get_n_blocks(), 0))
273
274 def get_n_blocks(self):
275 h, w, d = self.get_block_dimensions()
276 res = h * w * d
277 assert res >= 0
278 return res
279
Tim Hall79d07d22020-04-27 18:20:16 +0100280
281class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200282 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200283 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100284 self.in_tensor = in_tensor
285 self.out_tensor = out_tensor
286 self.box = box
287
288 def __str__(self):
289 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
290
291 __repr__ = __str__
292
Tim Hall79d07d22020-04-27 18:20:16 +0100293 def get_operation_count(self):
294 # returns numpy array of (DPU blocks, dma_ops)
295 return np.array((0, 1))
Johan Alfven90724962023-02-02 09:07:48 +0100296
297
298class NOP(Command):
299 def __init__(self, ps, in_tensor, out_tensor):
300 self.ps = ps
301 self.in_tensor = in_tensor
302 self.out_tensor = out_tensor
303
304 def __str__(self):
305 return f"<NOP: in={self.in_tensor.name}, out={self.out_tensor.name}>"
306
307 __repr__ = __str__
308
309 def get_operation_count(self):
310 # returns numpy array of (DPU blocks, dma_ops)
311 return np.array((0, 0))