blob: 0009f6cfe35c646553416744da449b8669a456cd [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
37 def transform_with_strides_and_skirt(
Tim Hallc30f4952020-06-15 20:47:35 +010038 self,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000039 strides: List[int],
40 skirt: List[int],
41 ifm_shape: Shape4D,
42 npu_block_type: NpuBlockType,
Louis Verhaardc822d622021-03-11 14:59:06 +010043 concat_offsets: List[int],
Rickard Bolin1c08afa2022-01-07 14:22:52 +000044 k_dilated_height: int,
Jonas Ohlsson845e2322022-03-01 12:39:55 +010045 split_offset: Optional[Shape4D] = None,
46 split_shape: Optional[Shape4D] = None,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000047 upscaling_factor: int = 1,
Tim Hall79d07d22020-04-27 18:20:16 +010048 ):
49 new_start_coord = list(self.start_coord)
50 new_end_coord = list(self.end_coord)
51
Louis Verhaardc822d622021-03-11 14:59:06 +010052 new_start_coord = np.subtract(new_start_coord, concat_offsets)
53 new_end_coord = np.subtract(new_end_coord, concat_offsets)
Tim Hall79d07d22020-04-27 18:20:16 +010054
Diego Russoea6111a2020-04-14 18:41:58 +010055 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010056 for idx in range(len(split_offset)):
57 new_start_coord[idx] += split_offset[idx]
58 new_end_coord[idx] += split_offset[idx]
59
Tim Halld8339a72021-05-27 18:49:40 +010060 if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020061 # these types of operations do a "dot product" or sum over the entire IFM
Tim Halld8339a72021-05-27 18:49:40 +010062 if split_offset is None:
63 new_start_coord[-1] = 0
64 new_end_coord[-1] = ifm_shape.depth
65 else:
66 new_start_coord[-1] = split_offset[-1]
67 new_end_coord[-1] = new_start_coord[-1] + split_shape[-1]
Tim Hall79d07d22020-04-27 18:20:16 +010068
Patrik Gustavssone6b94bb2021-05-05 08:30:41 +020069 if len(new_end_coord) >= 1:
patrik.gustavssoneeb85152020-12-21 17:10:40 +000070 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth)
71 if len(new_end_coord) >= 2:
72 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor)
73 if len(new_end_coord) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020074 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000075 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010076
77 pad_top = 0
78 pad_bottom = 0
79 if strides is not None and skirt is not None:
80 if len(new_start_coord) >= 2:
81 stride = strides[2]
Tim Hall3751aa42021-12-16 13:17:29 +000082 # 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 +000083 # of the split op (which is defined by the read offset and the read shape)
Tim Hall3751aa42021-12-16 13:17:29 +000084 if split_offset is None:
85 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
86 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
87 else:
88 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], split_offset[-2])
Tim Hall51a8dce2021-12-20 16:49:27 +000089 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 +010090
91 if len(new_start_coord) >= 3:
92 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020093 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +010094
95 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020096 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010097
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020098 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010099 new_start_coord[-3] = max(new_start_coord[-3], 0)
100
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000101 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +0100102 # pad_bottom is calculated based the diff between the end position of the weight kernel,
103 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000104 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200105 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000106 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200107 else:
108 k_start = new_start_coord[-3] - pad_top
Rickard Bolin1c08afa2022-01-07 14:22:52 +0000109 pad_bottom = max(
110 0, k_start + total_stride + k_dilated_height - (ifm_shape.height * upscaling_factor)
111 )
Tim Hall79d07d22020-04-27 18:20:16 +0100112
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200113 # Adjust for upscaling
114 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
115 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000116 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100117
118 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
119
120 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
121 start = [0] * len(weight_shape)
122 end = list(weight_shape)
123 if oc_range_start is not None and oc_range_end is not None:
124 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
125 # input range is output range divided by channel multiplier
126 if weights_transposed:
127 start[-1] = oc_range_start // weight_shape[-2]
128 end[-1] = oc_range_end // weight_shape[-2]
129 else:
130 start[-2] = oc_range_start // weight_shape[-1]
131 end[-2] = oc_range_end // weight_shape[-1]
132 else:
133 start[-1] = oc_range_start
134 end[-1] = oc_range_end
135 for i in range(len(end)):
136 assert 0 <= start[i] < weight_shape[i]
137 assert 0 < end[i] <= weight_shape[i]
138
139 return Box(start, end)
140
Tim Halld8339a72021-05-27 18:49:40 +0100141 def is_subbox_of(self, other):
142 if self.start_coord and self.end_coord:
143 assert len(self.start_coord) == len(other.start_coord)
144 assert len(self.end_coord) == len(other.end_coord)
145 return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all(
146 a <= b for (a, b) in zip(self.end_coord, other.end_coord)
147 )
148
Tim Hall79d07d22020-04-27 18:20:16 +0100149 def get_size_shape(self):
150 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
151
152 def get_size(self):
153 return int(np.prod(self.get_size_shape()))
154
Louis Verhaard69b31762020-11-17 09:45:20 +0100155 def get_block(self) -> Block:
156 return Block.from_shape(self.get_size_shape())
157
Tim Hall79d07d22020-04-27 18:20:16 +0100158 def __str__(self):
159 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
160
161 __repr__ = __str__
162
163
Tim Hall79d07d22020-04-27 18:20:16 +0100164class Command:
Tim Hall79d07d22020-04-27 18:20:16 +0100165 def is_npu_pass_command(self):
166 return False
167
Tim Hall79d07d22020-04-27 18:20:16 +0100168 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100169 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100170 return np.array((0, 0))
171
172
173class NpuStripe(Command):
174 def __init__(
175 self,
176 ps,
177 block_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100178 is_first_h_stripe,
179 is_last_h_stripe,
180 ifm_tensor,
181 ifm_box,
182 ofm_tensor,
183 ofm_box,
184 weight_tensor=None,
185 weight_box=None,
Tim Halld784af72021-06-08 21:25:57 +0100186 scale_tensor=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100187 ifm2_tensor=None,
188 ifm2_box=None,
189 pad_top=0,
190 pad_bottom=0,
191 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100192 self.ps = ps
193 self.block_config = block_config
Tim Hall79d07d22020-04-27 18:20:16 +0100194 self.is_first_h_stripe = is_first_h_stripe
195 self.is_last_h_stripe = is_last_h_stripe
196 self.ifm_tensor = ifm_tensor
197 self.ifm_box = ifm_box
198 self.ifm2_tensor = ifm2_tensor
199 self.ifm2_box = ifm2_box
200 self.ofm_tensor = ofm_tensor
201 self.ofm_box = ofm_box
202 self.weight_tensor = weight_tensor
Tim Halld784af72021-06-08 21:25:57 +0100203 self.scale_tensor = scale_tensor
Tim Hall79d07d22020-04-27 18:20:16 +0100204 self.weight_box = weight_box
Tim Hall79d07d22020-04-27 18:20:16 +0100205 self.pad_top = pad_top
206 self.pad_bottom = pad_bottom
207 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000208 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100209
Tim Hall79d07d22020-04-27 18:20:16 +0100210 def is_npu_pass_command(self):
211 return True
212
213 def __str__(self):
214 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
215 self.ps.name,
216 self.ifm_box,
217 self.ifm2_box,
218 self.ofm_box,
219 self.weight_box,
220 self.block_config,
221 )
222
223 __repr__ = __str__
224
Tim Hall79d07d22020-04-27 18:20:16 +0100225 def get_block_dimensions(self):
226 ofm_box = self.ofm_box
227 block_config = self.block_config
228
229 out_height = 1
230 out_width = 1
231 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
232 if len(ofm_box.end_coord) >= 4:
233 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
234 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
235
236 assert out_height >= 0
237 assert out_width >= 0
238 assert out_depth >= 0
239 return (
240 round_up_divide(out_height, block_config[0]),
241 round_up_divide(out_width, block_config[1]),
242 round_up_divide(out_depth, block_config[3]),
243 )
244
245 def get_operation_count(self):
246 # returns numpy array of (DPU blocks, dma_ops)
247 return np.array((self.get_n_blocks(), 0))
248
249 def get_n_blocks(self):
250 h, w, d = self.get_block_dimensions()
251 res = h * w * d
252 assert res >= 0
253 return res
254
Tim Hall79d07d22020-04-27 18:20:16 +0100255
256class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200257 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200258 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100259 self.in_tensor = in_tensor
260 self.out_tensor = out_tensor
261 self.box = box
262
263 def __str__(self):
264 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
265
266 __repr__ = __str__
267
Tim Hall79d07d22020-04-27 18:20:16 +0100268 def get_operation_count(self):
269 # returns numpy array of (DPU blocks, dma_ops)
270 return np.array((0, 1))