blob: ddb2482409d1913c6a10b6684e7feb04b08c0445 [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
19
Tim Hall79d07d22020-04-27 18:20:16 +010020import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010021
Louis Verhaard69b31762020-11-17 09:45:20 +010022from .architecture_features import Block
Tim Hall79d07d22020-04-27 18:20:16 +010023from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010024from .operation import NpuBlockType
patrik.gustavssoneeb85152020-12-21 17:10:40 +000025from .shape4d import Shape4D
Tim Hall79d07d22020-04-27 18:20:16 +010026
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(
Tim Hallc30f4952020-06-15 20:47:35 +010037 self,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000038 strides: List[int],
39 skirt: List[int],
40 ifm_shape: Shape4D,
41 npu_block_type: NpuBlockType,
Louis Verhaardc822d622021-03-11 14:59:06 +010042 concat_offsets: List[int],
Patrik Gustavssone3b1b912021-02-09 15:38:46 +010043 split_offset: Shape4D = None,
Tim Halld8339a72021-05-27 18:49:40 +010044 split_shape: Shape4D = None,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000045 k_height: int = 1,
46 upscaling_factor: int = 1,
Tim Hall79d07d22020-04-27 18:20:16 +010047 ):
48 new_start_coord = list(self.start_coord)
49 new_end_coord = list(self.end_coord)
50
Louis Verhaardc822d622021-03-11 14:59:06 +010051 new_start_coord = np.subtract(new_start_coord, concat_offsets)
52 new_end_coord = np.subtract(new_end_coord, concat_offsets)
Tim Hall79d07d22020-04-27 18:20:16 +010053
Diego Russoea6111a2020-04-14 18:41:58 +010054 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010055 for idx in range(len(split_offset)):
56 new_start_coord[idx] += split_offset[idx]
57 new_end_coord[idx] += split_offset[idx]
58
Tim Halld8339a72021-05-27 18:49:40 +010059 if npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020060 # these types of operations do a "dot product" or sum over the entire IFM
Tim Halld8339a72021-05-27 18:49:40 +010061 if split_offset is None:
62 new_start_coord[-1] = 0
63 new_end_coord[-1] = ifm_shape.depth
64 else:
65 new_start_coord[-1] = split_offset[-1]
66 new_end_coord[-1] = new_start_coord[-1] + split_shape[-1]
Tim Hall79d07d22020-04-27 18:20:16 +010067
Patrik Gustavssone6b94bb2021-05-05 08:30:41 +020068 if len(new_end_coord) >= 1:
patrik.gustavssoneeb85152020-12-21 17:10:40 +000069 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth)
70 if len(new_end_coord) >= 2:
71 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor)
72 if len(new_end_coord) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020073 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000074 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010075
76 pad_top = 0
77 pad_bottom = 0
78 if strides is not None and skirt is not None:
79 if len(new_start_coord) >= 2:
80 stride = strides[2]
81 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000082 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
Tim Hall79d07d22020-04-27 18:20:16 +010083
84 if len(new_start_coord) >= 3:
85 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020086 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +010087
88 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020089 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010090
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020091 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010092 new_start_coord[-3] = max(new_start_coord[-3], 0)
93
patrik.gustavssoneeb85152020-12-21 17:10:40 +000094 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +010095 # pad_bottom is calculated based the diff between the end position of the weight kernel,
96 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +000097 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020098 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +000099 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200100 else:
101 k_start = new_start_coord[-3] - pad_top
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000102 pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape.height * upscaling_factor))
Tim Hall79d07d22020-04-27 18:20:16 +0100103
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200104 # Adjust for upscaling
105 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
106 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000107 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100108
109 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
110
111 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
112 start = [0] * len(weight_shape)
113 end = list(weight_shape)
114 if oc_range_start is not None and oc_range_end is not None:
115 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
116 # input range is output range divided by channel multiplier
117 if weights_transposed:
118 start[-1] = oc_range_start // weight_shape[-2]
119 end[-1] = oc_range_end // weight_shape[-2]
120 else:
121 start[-2] = oc_range_start // weight_shape[-1]
122 end[-2] = oc_range_end // weight_shape[-1]
123 else:
124 start[-1] = oc_range_start
125 end[-1] = oc_range_end
126 for i in range(len(end)):
127 assert 0 <= start[i] < weight_shape[i]
128 assert 0 < end[i] <= weight_shape[i]
129
130 return Box(start, end)
131
Tim Halld8339a72021-05-27 18:49:40 +0100132 def is_subbox_of(self, other):
133 if self.start_coord and self.end_coord:
134 assert len(self.start_coord) == len(other.start_coord)
135 assert len(self.end_coord) == len(other.end_coord)
136 return all(a >= b for (a, b) in zip(self.start_coord, other.start_coord)) and all(
137 a <= b for (a, b) in zip(self.end_coord, other.end_coord)
138 )
139
Tim Hall79d07d22020-04-27 18:20:16 +0100140 def get_size_shape(self):
141 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
142
143 def get_size(self):
144 return int(np.prod(self.get_size_shape()))
145
Louis Verhaard69b31762020-11-17 09:45:20 +0100146 def get_block(self) -> Block:
147 return Block.from_shape(self.get_size_shape())
148
Tim Hall79d07d22020-04-27 18:20:16 +0100149 def __str__(self):
150 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
151
152 __repr__ = __str__
153
154
Tim Hall79d07d22020-04-27 18:20:16 +0100155class Command:
Tim Hall79d07d22020-04-27 18:20:16 +0100156 def is_npu_pass_command(self):
157 return False
158
Tim Hall79d07d22020-04-27 18:20:16 +0100159 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100160 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100161 return np.array((0, 0))
162
163
164class NpuStripe(Command):
165 def __init__(
166 self,
167 ps,
168 block_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100169 is_first_h_stripe,
170 is_last_h_stripe,
171 ifm_tensor,
172 ifm_box,
173 ofm_tensor,
174 ofm_box,
175 weight_tensor=None,
176 weight_box=None,
Tim Halld784af72021-06-08 21:25:57 +0100177 scale_tensor=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100178 ifm2_tensor=None,
179 ifm2_box=None,
180 pad_top=0,
181 pad_bottom=0,
182 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100183 self.ps = ps
184 self.block_config = block_config
Tim Hall79d07d22020-04-27 18:20:16 +0100185 self.is_first_h_stripe = is_first_h_stripe
186 self.is_last_h_stripe = is_last_h_stripe
187 self.ifm_tensor = ifm_tensor
188 self.ifm_box = ifm_box
189 self.ifm2_tensor = ifm2_tensor
190 self.ifm2_box = ifm2_box
191 self.ofm_tensor = ofm_tensor
192 self.ofm_box = ofm_box
193 self.weight_tensor = weight_tensor
Tim Halld784af72021-06-08 21:25:57 +0100194 self.scale_tensor = scale_tensor
Tim Hall79d07d22020-04-27 18:20:16 +0100195 self.weight_box = weight_box
Tim Hall79d07d22020-04-27 18:20:16 +0100196 self.pad_top = pad_top
197 self.pad_bottom = pad_bottom
198 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000199 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100200
Tim Hall79d07d22020-04-27 18:20:16 +0100201 def is_npu_pass_command(self):
202 return True
203
204 def __str__(self):
205 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
206 self.ps.name,
207 self.ifm_box,
208 self.ifm2_box,
209 self.ofm_box,
210 self.weight_box,
211 self.block_config,
212 )
213
214 __repr__ = __str__
215
Tim Hall79d07d22020-04-27 18:20:16 +0100216 def get_block_dimensions(self):
217 ofm_box = self.ofm_box
218 block_config = self.block_config
219
220 out_height = 1
221 out_width = 1
222 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
223 if len(ofm_box.end_coord) >= 4:
224 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
225 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
226
227 assert out_height >= 0
228 assert out_width >= 0
229 assert out_depth >= 0
230 return (
231 round_up_divide(out_height, block_config[0]),
232 round_up_divide(out_width, block_config[1]),
233 round_up_divide(out_depth, block_config[3]),
234 )
235
236 def get_operation_count(self):
237 # returns numpy array of (DPU blocks, dma_ops)
238 return np.array((self.get_n_blocks(), 0))
239
240 def get_n_blocks(self):
241 h, w, d = self.get_block_dimensions()
242 res = h * w * d
243 assert res >= 0
244 return res
245
Tim Hall79d07d22020-04-27 18:20:16 +0100246
247class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200248 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200249 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100250 self.in_tensor = in_tensor
251 self.out_tensor = out_tensor
252 self.box = box
253
254 def __str__(self):
255 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
256
257 __repr__ = __str__
258
Tim Hall79d07d22020-04-27 18:20:16 +0100259 def get_operation_count(self):
260 # returns numpy array of (DPU blocks, dma_ops)
261 return np.array((0, 1))