blob: 72caa1b6cc7691bc05cd719c10129592274783a6 [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# Shared buffer allocation works out how to allocate the Ethos-U55 shared buffer for a given pass.
Tim Hall79d07d22020-04-27 18:20:16 +010018import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010019
Diego Russoe8a10452020-04-21 17:39:10 +010020from .architecture_features import ArchitectureFeatures
21from .architecture_features import Block
22from .architecture_features import Kernel
23from .architecture_features import SharedBufferArea
24from .architecture_features import SHRAMElements
Louis Verhaard7db78962020-05-25 15:05:26 +020025from .errors import OptionError
Diego Russoea6111a2020-04-14 18:41:58 +010026from .operation import NpuBlockType
Tim Hall79d07d22020-04-27 18:20:16 +010027
28
29class SharedBufferAllocation:
30 def __init__(self, arch, ps):
31 self.arch = arch
32
33 self.bank_locations = np.zeros(SharedBufferArea.Size)
34 self.banks_required = np.zeros(SharedBufferArea.Size)
35
36 ifm_tensor, ifm2_tensor, weight_tensor, ofm_tensor = ps.get_primary_op_ifm_ifm2_weights_ofm()
37
38 strides = (1, 1, 1, 1)
39 dilation = (1, 1, 1, 1)
40 self.kernel = Kernel(1, 1)
41 is_elementwise = ps.npu_block_type == NpuBlockType.ElementWise
42
43 if ps.primary_op:
44 strides = ps.primary_op.attrs.get("strides", strides)
45 dilation = ps.primary_op.attrs.get("dilation", dilation)
46 k_h = 1
47 k_w = 1
48 if weight_tensor:
49 if ps.primary_op.type != "FullyConnectedAct":
50 k_h = weight_tensor.shape[0]
51 k_w = weight_tensor.shape[1]
52 else:
53 k_h = ps.primary_op.attrs.get("filter_height", 1)
54 k_w = ps.primary_op.attrs.get("filter_width", 1)
55
56 self.kernel = Kernel(k_w, k_h, strides[2], strides[1], dilation[2], dilation[1])
57
58 self.is_equal_depth_op = is_elementwise or ps.npu_block_type in (
59 NpuBlockType.ConvolutionDepthWise,
60 NpuBlockType.Pooling,
61 )
62 self.strides = strides
63
64 self.use_accumulator_element = SHRAMElements.Acc32
65 if is_elementwise:
66 self.use_ifm_element = SHRAMElements.IFM8_Elementwise
67 else:
68 self.use_ifm_element = SHRAMElements.IFM8
69
70 self.ifm_bits = 0
71 self.ifm_depth = 0
72 if ifm_tensor:
73 self.ifm_bits = ifm_tensor.dtype.size_in_bits()
74 if ifm_tensor.shape == [] and is_elementwise:
75 # Elementwise operator with scalar in ifm, use ifm2 depth
76 self.ifm_depth = ifm2_tensor.shape[-1]
77 else:
78 self.ifm_depth = ifm_tensor.shape[-1]
79 if self.ifm_bits == 16:
80 self.use_accumulator_element = SHRAMElements.Acc40
81 self.use_ifm_element = self.use_ifm_element + 1
82 assert (self.use_ifm_element == SHRAMElements.IFM16) or (
83 self.use_ifm_element == SHRAMElements.IFM16_Elementwise
84 )
85 else:
86 assert self.ifm_bits == 8, "Unexpected IFM bitdepth"
87
Dwight Lidmana9390f72020-05-13 12:00:08 +020088 self.ifm_resampling_mode = ifm_tensor.resampling_mode
Tim Hall79d07d22020-04-27 18:20:16 +010089 self.ifm_block_depth = arch.calc_ifm_block_depth(self.ifm_depth, self.ifm_bits)
90 self.ofm_tensor = ofm_tensor
91
92 self.banks_required[SharedBufferArea.Weights] = arch.shram_reserved_weight_banks
93 self.banks_required[SharedBufferArea.OFM] = arch.shram_reserved_output_banks
94
95 def is_valid(self):
96 # Assign zero-based bank starts (first element remains zero)
97 self.bank_locations[1:] = np.cumsum(self.banks_required)[:-1]
98
99 # Accumulator area is measured from the end of the buffer
100 self.bank_locations[SharedBufferArea.Accumulators] = (
101 self.arch.shram_total_banks - self.banks_required[SharedBufferArea.Accumulators]
102 )
103 ifm_end = self.bank_locations[SharedBufferArea.IFM] + self.banks_required[SharedBufferArea.IFM]
104 return ifm_end <= self.bank_locations[SharedBufferArea.Accumulators]
105
106 def try_block(self, ofm_block: Block):
107 # Get IFM block configuration
108 ifm_block_depth = ofm_block.depth if self.is_equal_depth_op else self.ifm_block_depth
Dwight Lidmana9390f72020-05-13 12:00:08 +0200109 ifm_block = self.arch.get_ifm_block_size(ifm_block_depth, ofm_block, self.kernel, ifm_resampling_mode=self.ifm_resampling_mode)
Tim Hall79d07d22020-04-27 18:20:16 +0100110 ifm_config = self.arch.get_block_config(ifm_block.width, ifm_block.height, ifm_block.depth)
111 if ifm_config is None:
112 return None
113
114 # Get OFM block configuration
115 ofm_config = self.arch.get_block_config(ofm_block.width, ofm_block.height, ofm_block.depth)
116 if ofm_config is None:
117 return None
118
119 # Update bank counts for IFM and Accumulator
120 self.banks_required[SharedBufferArea.IFM] = ifm_config.banks[self.use_ifm_element]
121 self.banks_required[SharedBufferArea.Accumulators] = ofm_config.banks[self.use_accumulator_element]
122
123 # Validating calculates bank layout and returns validity
124 if not self.is_valid():
125 return None
126
127 return (ofm_block.height, ofm_block.width, ifm_block.depth, ofm_block.depth)
128
129 def generate_used_mask(self, active_set):
130 res = np.zeros(self.arch.shram_total_banks, dtype=np.int64)
131 for kind in active_set:
132 start = int(self.bank_locations[kind])
133 end = start + int(self.banks_required[kind])
134 res[start:end] = 1
135 return res
136
137 def is_compatible(first, second):
138 """See if the bank allocations of two convolutions are compatible,
139 so that they can run back-to-back without a fence in between"""
140
141 first_set = set((SharedBufferArea.OFM, SharedBufferArea.Accumulators))
142 second_set = set((SharedBufferArea.IFM, SharedBufferArea.Weights))
143
144 first_mask = first.generate_used_mask(first_set)
145 second_mask = second.generate_used_mask(second_set)
146
147 if np.sum(first_mask & second_mask):
148 # overlap
149 return False
150
151 return True
152
153
154def shared_buffer_allocation_for_pass_and_block_config(arch, ps, block_config):
155 alloc = SharedBufferAllocation(arch, ps)
156 assert (alloc.ifm_block_depth == block_config[2]) or alloc.is_equal_depth_op
157 if alloc.try_block(Block(block_config[1], block_config[0], block_config[3])):
158 return alloc
159
160 return None
161
162
163def find_block_configs_suitable_for_pass_and_shared_buffer(arch, ps):
164 alloc = SharedBufferAllocation(arch, ps)
165
166 if arch.override_block_config:
167 config = alloc.try_block(arch.override_block_config)
Louis Verhaard7db78962020-05-25 15:05:26 +0200168 raise OptionError(
169 "--force-block-config",
170 str(arch.override_block_config),
171 "This forced block config value cannot be used; it is not compatible",
172 )
Tim Hall79d07d22020-04-27 18:20:16 +0100173
174 # Constrain the search space if the OFM is smaller than the max block size
175 # - Add other block search constraints here if required
176 if len(alloc.ofm_tensor.shape) == 2:
177 max_block_height = max_block_width = alloc.ofm_tensor.shape[0]
178 else:
179 max_block_width = alloc.ofm_tensor.shape[-2]
180 max_block_height = alloc.ofm_tensor.shape[-3]
181
182 # Common block depth
183 max_block_depth = alloc.ofm_tensor.shape[-1]
184
185 # Constrain to valid ranges before search
186 max_block_width = min(arch.ofm_block_max.width, max_block_width)
187 max_block_height = min(arch.ofm_block_max.height, max_block_height)
188 max_block_depth = min(arch.ofm_block_max.depth, max_block_depth)
189
190 valid_block_configs = []
191 # Try a range of block shapes against this pass
192 for w in range(arch.ofm_ublock.width, max_block_width + arch.ofm_ublock.width, arch.ofm_ublock.width):
193 for h in range(arch.ofm_ublock.height, max_block_height + arch.ofm_ublock.height, arch.ofm_ublock.height):
194 # Try valid OFM block depths
195 for c in range(arch.ofm_ublock.depth, max_block_depth + arch.ofm_ublock.depth, arch.ofm_ublock.depth):
196 # OFM block depth has the constraint that if it causes the OFM to be
197 # split, it must be a multiple of the OFM split size
198 if (c >= max_block_depth) or (c < max_block_depth and (c % ArchitectureFeatures.OFMSplitDepth) == 0):
199 config = alloc.try_block(Block(w, h, c))
200 if config:
201 valid_block_configs.append(config)
202
203 assert len(valid_block_configs) > 0
204 return valid_block_configs