blob: 19a363c3fe899e6cf11a0d59dd735136c569db91 [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,
patrik.gustavssoneeb85152020-12-21 17:10:40 +000044 k_height: int = 1,
45 upscaling_factor: int = 1,
Tim Hall79d07d22020-04-27 18:20:16 +010046 ):
47 new_start_coord = list(self.start_coord)
48 new_end_coord = list(self.end_coord)
49
Louis Verhaardc822d622021-03-11 14:59:06 +010050 new_start_coord = np.subtract(new_start_coord, concat_offsets)
51 new_end_coord = np.subtract(new_end_coord, concat_offsets)
Tim Hall79d07d22020-04-27 18:20:16 +010052
Diego Russoea6111a2020-04-14 18:41:58 +010053 if split_offset is not None:
Tim Hall79d07d22020-04-27 18:20:16 +010054 for idx in range(len(split_offset)):
55 new_start_coord[idx] += split_offset[idx]
56 new_end_coord[idx] += split_offset[idx]
57
Michael McGeaghf3e3ad72020-12-02 12:39:03 +000058 if (split_offset is None) and (
59 npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum)
Fredrik Svedberga0c36242020-06-03 15:43:31 +020060 ):
61 # these types of operations do a "dot product" or sum over the entire IFM
Tim Hall79d07d22020-04-27 18:20:16 +010062 new_start_coord[-1] = 0
patrik.gustavssoneeb85152020-12-21 17:10:40 +000063 new_end_coord[-1] = ifm_shape.depth
Tim Hall79d07d22020-04-27 18:20:16 +010064
Patrik Gustavssone6b94bb2021-05-05 08:30:41 +020065 if len(new_end_coord) >= 1:
patrik.gustavssoneeb85152020-12-21 17:10:40 +000066 new_end_coord[-1] = min(new_end_coord[-1], ifm_shape.depth)
67 if len(new_end_coord) >= 2:
68 new_end_coord[-2] = min(new_end_coord[-2], ifm_shape.width * upscaling_factor)
69 if len(new_end_coord) >= 3:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020070 original_end_coord = list(new_end_coord)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000071 new_end_coord[-3] = min(new_end_coord[-3], ifm_shape.height * upscaling_factor)
Tim Hall79d07d22020-04-27 18:20:16 +010072
73 pad_top = 0
74 pad_bottom = 0
75 if strides is not None and skirt is not None:
76 if len(new_start_coord) >= 2:
77 stride = strides[2]
78 new_start_coord[-2] = max(new_start_coord[-2] * stride - skirt[1], 0)
patrik.gustavssoneeb85152020-12-21 17:10:40 +000079 new_end_coord[-2] = min(new_end_coord[-2] * stride + skirt[3], ifm_shape.width)
Tim Hall79d07d22020-04-27 18:20:16 +010080
81 if len(new_start_coord) >= 3:
82 stride = strides[1]
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020083 skirt_top_remainder = skirt[0] % upscaling_factor
Tim Hall79d07d22020-04-27 18:20:16 +010084
85 total_stride = stride * (new_end_coord[-3] - new_start_coord[-3] - 1)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020086 new_start_coord[-3] = new_start_coord[-3] * stride - skirt[0] + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010087
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020088 pad_top = max(0, 0 - new_start_coord[-3]) + skirt_top_remainder
Tim Hall79d07d22020-04-27 18:20:16 +010089 new_start_coord[-3] = max(new_start_coord[-3], 0)
90
patrik.gustavssoneeb85152020-12-21 17:10:40 +000091 if (new_end_coord[-3] * stride + skirt[2]) > (ifm_shape.height * upscaling_factor):
Tim Hall79d07d22020-04-27 18:20:16 +010092 # pad_bottom is calculated based the diff between the end position of the weight kernel,
93 # after last stride and the ifm height.
patrik.gustavssoneeb85152020-12-21 17:10:40 +000094 if upscaling_factor != 1 and original_end_coord[-3] > ifm_shape.height * upscaling_factor:
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020095 # Special case for Transpose Convolution with VALID padding.
patrik.gustavssoneeb85152020-12-21 17:10:40 +000096 pad_bottom = original_end_coord[-3] - (ifm_shape.height * upscaling_factor)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +020097 else:
98 k_start = new_start_coord[-3] - pad_top
patrik.gustavssoneeb85152020-12-21 17:10:40 +000099 pad_bottom = max(0, k_start + total_stride + k_height - (ifm_shape.height * upscaling_factor))
Tim Hall79d07d22020-04-27 18:20:16 +0100100
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200101 # Adjust for upscaling
102 new_start_coord[-3] = max(new_start_coord[-3] // upscaling_factor, 0)
103 new_end_coord[-3] = new_end_coord[-3] * stride + skirt[2] + (skirt[2] % upscaling_factor)
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000104 new_end_coord[-3] = max(min(new_end_coord[-3] // upscaling_factor, ifm_shape.height), 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100105
106 return Box(new_start_coord, new_end_coord), pad_top, pad_bottom
107
108 def make_weight_box(weight_shape, npu_block_type, oc_range_start=None, oc_range_end=None, weights_transposed=False):
109 start = [0] * len(weight_shape)
110 end = list(weight_shape)
111 if oc_range_start is not None and oc_range_end is not None:
112 if npu_block_type == NpuBlockType.ConvolutionDepthWise:
113 # input range is output range divided by channel multiplier
114 if weights_transposed:
115 start[-1] = oc_range_start // weight_shape[-2]
116 end[-1] = oc_range_end // weight_shape[-2]
117 else:
118 start[-2] = oc_range_start // weight_shape[-1]
119 end[-2] = oc_range_end // weight_shape[-1]
120 else:
121 start[-1] = oc_range_start
122 end[-1] = oc_range_end
123 for i in range(len(end)):
124 assert 0 <= start[i] < weight_shape[i]
125 assert 0 < end[i] <= weight_shape[i]
126
127 return Box(start, end)
128
129 def get_size_shape(self):
130 return [int(self.end_coord[i] - self.start_coord[i]) for i in range(len(self.end_coord))]
131
132 def get_size(self):
133 return int(np.prod(self.get_size_shape()))
134
Louis Verhaard69b31762020-11-17 09:45:20 +0100135 def get_block(self) -> Block:
136 return Block.from_shape(self.get_size_shape())
137
Tim Hall79d07d22020-04-27 18:20:16 +0100138 def __str__(self):
139 return "<Box %s - %s>" % (self.start_coord, self.end_coord)
140
141 __repr__ = __str__
142
143
Tim Hall79d07d22020-04-27 18:20:16 +0100144class Command:
145 def get_ofm_y_range_for_pass(self, ps_requested):
146 return None
147
148 def is_npu_pass_command(self):
149 return False
150
Tim Hall79d07d22020-04-27 18:20:16 +0100151 def get_operation_count(self):
Dwight Lidman9b43f842020-12-08 17:56:44 +0100152 # returns numpy array of (DPU blocks, dma_ops).
Tim Hall79d07d22020-04-27 18:20:16 +0100153 return np.array((0, 0))
154
155
156class NpuStripe(Command):
157 def __init__(
158 self,
159 ps,
160 block_config,
161 is_first,
162 is_last,
163 is_first_h_stripe,
164 is_last_h_stripe,
165 ifm_tensor,
166 ifm_box,
167 ofm_tensor,
168 ofm_box,
169 weight_tensor=None,
170 weight_box=None,
171 scale_tensor=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100172 ifm2_tensor=None,
173 ifm2_box=None,
174 pad_top=0,
175 pad_bottom=0,
176 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100177 self.ps = ps
178 self.block_config = block_config
179 self.is_first = is_first
180 self.is_last = is_last
181 self.is_first_h_stripe = is_first_h_stripe
182 self.is_last_h_stripe = is_last_h_stripe
183 self.ifm_tensor = ifm_tensor
184 self.ifm_box = ifm_box
185 self.ifm2_tensor = ifm2_tensor
186 self.ifm2_box = ifm2_box
187 self.ofm_tensor = ofm_tensor
188 self.ofm_box = ofm_box
189 self.weight_tensor = weight_tensor
190 self.scale_tensor = scale_tensor
191 self.weight_box = weight_box
Tim Hall79d07d22020-04-27 18:20:16 +0100192 self.pad_top = pad_top
193 self.pad_bottom = pad_bottom
194 for i in range(len(self.ofm_box.end_coord)):
Tim Hall73e843f2021-02-04 22:47:46 +0000195 assert self.ofm_box.end_coord[i] <= ps.ofm_shapes[0][i]
Tim Hall79d07d22020-04-27 18:20:16 +0100196
Tim Hall79d07d22020-04-27 18:20:16 +0100197 def is_npu_pass_command(self):
198 return True
199
200 def __str__(self):
201 return "<NPUStripe: ps=%s, ifm_box=%s, ifm2_box=%s, ofm_box=%s, weight_box=%s, block_config=%s>" % (
202 self.ps.name,
203 self.ifm_box,
204 self.ifm2_box,
205 self.ofm_box,
206 self.weight_box,
207 self.block_config,
208 )
209
210 __repr__ = __str__
211
212 def get_ofm_y_range_for_pass(self, ps_requested):
213 if ps_requested != self.ps:
214 return None
215 if len(self.ofm_box.start_coord) >= 3:
216 return (self.ofm_box.start_coord[-3], self.ofm_box.end_coord[-3])
217 return None
218
219 def get_block_dimensions(self):
220 ofm_box = self.ofm_box
221 block_config = self.block_config
222
223 out_height = 1
224 out_width = 1
225 out_depth = ofm_box.end_coord[-1] - ofm_box.start_coord[-1]
226 if len(ofm_box.end_coord) >= 4:
227 out_width = ofm_box.end_coord[-2] - ofm_box.start_coord[-2]
228 out_height = ofm_box.end_coord[-3] - ofm_box.start_coord[-3]
229
230 assert out_height >= 0
231 assert out_width >= 0
232 assert out_depth >= 0
233 return (
234 round_up_divide(out_height, block_config[0]),
235 round_up_divide(out_width, block_config[1]),
236 round_up_divide(out_depth, block_config[3]),
237 )
238
239 def get_operation_count(self):
240 # returns numpy array of (DPU blocks, dma_ops)
241 return np.array((self.get_n_blocks(), 0))
242
243 def get_n_blocks(self):
244 h, w, d = self.get_block_dimensions()
245 res = h * w * d
246 assert res >= 0
247 return res
248
Tim Hall79d07d22020-04-27 18:20:16 +0100249
250class DMA(Command):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200251 def __init__(self, ps, in_tensor, out_tensor, box):
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200252 self.ps = ps
Tim Hall79d07d22020-04-27 18:20:16 +0100253 self.in_tensor = in_tensor
254 self.out_tensor = out_tensor
255 self.box = box
256
257 def __str__(self):
258 return "<DMA: in=%s, out=%s, box=%s>" % (self.in_tensor.name, self.out_tensor.name, self.box)
259
260 __repr__ = __str__
261
Tim Hall79d07d22020-04-27 18:20:16 +0100262 def get_operation_count(self):
263 # returns numpy array of (DPU blocks, dma_ops)
264 return np.array((0, 1))