blob: c25c023e7388e2122485b595b099caba85c2da2d [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
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,
42 concat_axis: int = 0,
43 concat_offset: int = 0,
44 split_offset: int = None,
45 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
51 new_start_coord[concat_axis] -= concat_offset
52 new_end_coord[concat_axis] -= concat_offset
53
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
Michael McGeaghf3e3ad72020-12-02 12:39:03 +000059 if (split_offset is None) and (
60 npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum)
Fredrik Svedberga0c36242020-06-03 15:43:31 +020061 ):
62 # these types of operations do a "dot product" or sum over the entire IFM
Tim Hall79d07d22020-04-27 18:20:16 +010063 new_start_coord[-1] = 0
patrik.gustavssoneeb85152020-12-21 17:10:40 +000064 new_end_coord[-1] = ifm_shape.depth
Tim Hall79d07d22020-04-27 18:20:16 +010065
patrik.gustavssoneeb85152020-12-21 17:10:40 +000066 if npu_block_type == NpuBlockType.ElementWise and len(new_end_coord) >= 1:
67 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth)
68 if len(new_end_coord) >= 2:
69 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor)
70 if len(new_end_coord) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020071 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000072 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010073
74 pad_top = 0
75 pad_bottom = 0
76 if strides is not None and skirt is not None:
77 if len(new_start_coord) >= 2:
78 stride = strides[2]
79 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000080 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
Tim Hall79d07d22020-04-27 18:20:16 +010081
82 if len(new_start_coord) >= 3:
83 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020084 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +010085
86 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020087 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010088
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020089 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010090 new_start_coord[-3] = max(new_start_coord[-3], 0)
91
patrik.gustavssoneeb85152020-12-21 17:10:40 +000092 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +010093 # pad_bottom is calculated based the diff between the end position of the weight kernel,
94 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +000095 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020096 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +000097 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020098 else:
99 k_start = new_start_coord[-3] - pad_top
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000100 pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape.height * upscaling_factor))
Tim Hall79d07d22020-04-27 18:20:16 +0100101
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200102 # Adjust for upscaling
103 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
104 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000105 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100106
107 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
108
109 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
110 start = [0] * len(weight_shape)
111 end = list(weight_shape)
112 if oc_range_start is not None and oc_range_end is not None:
113 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
114 # input range is output range divided by channel multiplier
115 if weights_transposed:
116 start[-1] = oc_range_start // weight_shape[-2]
117 end[-1] = oc_range_end // weight_shape[-2]
118 else:
119 start[-2] = oc_range_start // weight_shape[-1]
120 end[-2] = oc_range_end // weight_shape[-1]
121 else:
122 start[-1] = oc_range_start
123 end[-1] = oc_range_end
124 for i in range(len(end)):
125 assert 0 <= start[i] < weight_shape[i]
126 assert 0 < end[i] <= weight_shape[i]
127
128 return Box(start, end)
129
130 def get_size_shape(self):
131 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
132
133 def get_size(self):
134 return int(np.prod(self.get_size_shape()))
135
Louis Verhaard69b31762020-11-17 09:45:20 +0100136 def get_block(self) -> Block:
137 return Block.from_shape(self.get_size_shape())
138
Tim Hall79d07d22020-04-27 18:20:16 +0100139 def __str__(self):
140 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
141
142 __repr__ = __str__
143
144
Tim Hall79d07d22020-04-27 18:20:16 +0100145class Command:
146 def get_ofm_y_range_for_pass(self, ps_requested):
147 return None
148
149 def is_npu_pass_command(self):
150 return False
151
Tim Hall79d07d22020-04-27 18:20:16 +0100152 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100153 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100154 return np.array((0, 0))
155
156
157class NpuStripe(Command):
158 def __init__(
159 self,
160 ps,
161 block_config,
162 is_first,
163 is_last,
164 is_first_h_stripe,
165 is_last_h_stripe,
166 ifm_tensor,
167 ifm_box,
168 ofm_tensor,
169 ofm_box,
170 weight_tensor=None,
171 weight_box=None,
172 scale_tensor=None,
173 concat_axis=0,
174 concat_offset=0,
175 ifm2_tensor=None,
176 ifm2_box=None,
177 pad_top=0,
178 pad_bottom=0,
179 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100180 self.ps = ps
181 self.block_config = block_config
182 self.is_first = is_first
183 self.is_last = is_last
184 self.is_first_h_stripe = is_first_h_stripe
185 self.is_last_h_stripe = is_last_h_stripe
186 self.ifm_tensor = ifm_tensor
187 self.ifm_box = ifm_box
188 self.ifm2_tensor = ifm2_tensor
189 self.ifm2_box = ifm2_box
190 self.ofm_tensor = ofm_tensor
191 self.ofm_box = ofm_box
192 self.weight_tensor = weight_tensor
193 self.scale_tensor = scale_tensor
194 self.weight_box = weight_box
195 self.concat_axis = concat_axis
196 self.concat_offset = concat_offset
197 self.pad_top = pad_top
198 self.pad_bottom = pad_bottom
199 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000200 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100201
Tim Hall79d07d22020-04-27 18:20:16 +0100202 def is_npu_pass_command(self):
203 return True
204
205 def __str__(self):
206 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
207 self.ps.name,
208 self.ifm_box,
209 self.ifm2_box,
210 self.ofm_box,
211 self.weight_box,
212 self.block_config,
213 )
214
215 __repr__ = __str__
216
217 def get_ofm_y_range_for_pass(self, ps_requested):
218 if ps_requested != self.ps:
219 return None
220 if len(self.ofm_box.start_coord) >= 3:
221 return (self.ofm_box.start_coord[-3], self.ofm_box.end_coord[-3])
222 return None
223
224 def get_block_dimensions(self):
225 ofm_box = self.ofm_box
226 block_config = self.block_config
227
228 out_height = 1
229 out_width = 1
230 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
231 if len(ofm_box.end_coord) >= 4:
232 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
233 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
234
235 assert out_height >= 0
236 assert out_width >= 0
237 assert out_depth >= 0
238 return (
239 round_up_divide(out_height, block_config[0]),
240 round_up_divide(out_width, block_config[1]),
241 round_up_divide(out_depth, block_config[3]),
242 )
243
244 def get_operation_count(self):
245 # returns numpy array of (DPU blocks, dma_ops)
246 return np.array((self.get_n_blocks(), 0))
247
248 def get_n_blocks(self):
249 h, w, d = self.get_block_dimensions()
250 res = h * w * d
251 assert res >= 0
252 return res
253
Tim Hall79d07d22020-04-27 18:20:16 +0100254
255class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200256 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200257 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100258 self.in_tensor = in_tensor
259 self.out_tensor = out_tensor
260 self.box = box
261
262 def __str__(self):
263 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
264
265 __repr__ = __str__
266
Tim Hall79d07d22020-04-27 18:20:16 +0100267 def get_operation_count(self):
268 # returns numpy array of (DPU blocks, dma_ops)
269 return np.array((0, 1))