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