blob: 0ad30e5fa0d99328e88098a840b69fbcdd9f070d [file] [log] [blame]
Louis Verhaardd7002522021-01-20 17:23:54 +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# Wrapping function to do tensor address allocation. That is, assigning addresses to tensors based on what has been
18# worked out from the allowable overlaps that are calculated by the live range analysis.
Tim Hall79d07d22020-04-27 18:20:16 +010019import math
Louis Verhaard226ecaf2021-03-30 10:18:28 +020020from typing import List
Tim Hall79d07d22020-04-27 18:20:16 +010021
Diego Russoea6111a2020-04-14 18:41:58 +010022import numpy as np
23
Louis Verhaardd7002522021-01-20 17:23:54 +010024from . import hillclimb_allocation
Diego Russoea6111a2020-04-14 18:41:58 +010025from . import live_range
26from . import numeric_util
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020027from .errors import AllocationError
Tim Hall79d07d22020-04-27 18:20:16 +010028from .greedy_allocation import allocate_live_ranges as greedy_allocate_live_ranges
Louis Verhaard226ecaf2021-03-30 10:18:28 +020029from .live_range import LiveRange
Louis Verhaard9bfe0f82020-12-03 12:26:25 +010030from .live_range import LiveRangeGraph
Diego Russoe8a10452020-04-21 17:39:10 +010031from .nn_graph import TensorAllocator
32from .tensor import MemArea
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020033from .tensor import MemType
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020034from .tensor import Tensor
Louis Verhaard0b8268a2020-08-05 16:11:29 +020035from .tensor import TensorPurpose
Tim Hall79d07d22020-04-27 18:20:16 +010036
37
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020038def linear_allocate_live_ranges(live_ranges, alloc_granularity=Tensor.AllocationQuantum):
Louis Verhaard3c07c972020-05-07 08:12:58 +020039 # Allocates using increasing addresses. Duplicate constant tensors will be allocated to the same address
Tim Hall79d07d22020-04-27 18:20:16 +010040 total_sz = 0
41 allocated_tensors = []
42
Louis Verhaard3c07c972020-05-07 08:12:58 +020043 # just assign increasing addresses, except for duplicates
Tim Hall79d07d22020-04-27 18:20:16 +010044 for tens, lr in live_ranges.ranges.items():
45 if tens in allocated_tensors:
46 continue
47
Louis Verhaard3c07c972020-05-07 08:12:58 +020048 address = total_sz
49 if tens.weight_compression_config is not None:
50 for allocated_tens in allocated_tensors:
51 if allocated_tens.weight_compression_config == tens.weight_compression_config:
52 address = allocated_tens.address
53 break
Louis Verhaard0b8268a2020-08-05 16:11:29 +020054 if tens.purpose == TensorPurpose.LUT:
55 for allocated_tens in allocated_tensors:
56 if allocated_tens.equivalent(tens):
57 address = allocated_tens.address
58 break
Louis Verhaard3c07c972020-05-07 08:12:58 +020059 lr.set_address(address)
Tim Hall79d07d22020-04-27 18:20:16 +010060 allocated_tensors += lr.tensors
Louis Verhaard3c07c972020-05-07 08:12:58 +020061 if address == total_sz:
62 total_sz += numeric_util.round_up(int(math.ceil(lr.size)), alloc_granularity)
Tim Hall79d07d22020-04-27 18:20:16 +010063
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020064 verify_alignment(live_ranges, alloc_granularity)
Tim Hall79d07d22020-04-27 18:20:16 +010065 return total_sz
66
67
Louis Verhaardd7002522021-01-20 17:23:54 +010068def hillclimb_allocate_live_ranges(live_ranges: LiveRangeGraph, alloc_granularity: int) -> int:
69 # Allocates using the hill climb allocator
Louis Verhaard226ecaf2021-03-30 10:18:28 +020070 addresses = hillclimb_allocation.allocate_live_ranges(live_ranges.lrs)
Louis Verhaard9bfe0f82020-12-03 12:26:25 +010071 # The result is a list containing the allocated addresses
72 total_sz = 0
Louis Verhaard226ecaf2021-03-30 10:18:28 +020073 for lr, address in zip(live_ranges.lrs, addresses):
Louis Verhaard9bfe0f82020-12-03 12:26:25 +010074 total_sz = max(total_sz, address + lr.size)
75 lr.set_address(address)
76 verify_allocation(live_ranges, alloc_granularity)
77 return total_sz
78
79
80def verify_alignment(live_ranges: LiveRangeGraph, alignment: int):
Louis Verhaard226ecaf2021-03-30 10:18:28 +020081 for lr in live_ranges.lrs:
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020082 for tens in lr.tensors:
83 if not all(op and op.run_on_npu for op in tens.ops + tens.consumer_list):
84 # This is a CPU tensor, verify alignment
85 if tens.address % alignment != 0:
Michael McGeagh7a6f8432020-12-02 15:29:22 +000086 raise AllocationError(f"Tensor '{tens.name}' not aligned to {alignment} bytes")
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020087
88
Louis Verhaard9bfe0f82020-12-03 12:26:25 +010089def verify_allocation(live_ranges: LiveRangeGraph, alignment: int):
Louis Verhaard226ecaf2021-03-30 10:18:28 +020090 verify_alignment(live_ranges, alignment)
91 nr_time_slots = 1 + max(lr.end_time for lr in live_ranges.lrs)
92 # Contains active live ranges at each timestamp
93 lrs_at_time = [[] for i in range(nr_time_slots)]
94 for lr in live_ranges.lrs:
95 for t in range(lr.start_time, lr.end_time + 1):
96 lrs_at_time[t].append(lr)
97 for t in range(nr_time_slots):
98 for ix, n in enumerate(lrs_at_time[t]):
99 for m in lrs_at_time[t][ix + 1 :]:
Louis Verhaard9bfe0f82020-12-03 12:26:25 +0100100 overlap, tens_n, tens_m = n.overlaps_address(m)
101 if overlap and not (tens_n.equivalent(tens_m) and tens_n.address == tens_m.address):
102 raise AllocationError(
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000103 f"Overlapping buffers: {n.name}: {tens_n.address} -> {tens_n.address + n.size}"
104 f" and {m.name}: {tens_m.address} -> {tens_m.address + m.size}"
Louis Verhaard9bfe0f82020-12-03 12:26:25 +0100105 )
106
107
Tim Hall79d07d22020-04-27 18:20:16 +0100108def mark_sram_used_for_cascaded_passes(sg, lrs):
109 end_pos = max(ps.time for ps in sg.cascaded_passes) + 2
110 mem_usage = np.zeros(end_pos, dtype=np.int64)
111
112 for tens, rng in lrs.ranges.items():
113 storage_size = tens.storage_size()
114 mem_usage[rng.start_time : rng.end_time] += storage_size
115
116 for cps in sg.cascaded_passes:
117 sram_used = max(mem_usage[cps.time], mem_usage[cps.time + 1])
118 cps.sram_used = sram_used
119 for ps in cps.passes:
120 ps.sram_used = sram_used
121
122
Tim Hallb9b515c2020-11-01 21:27:19 +0000123def print_allocation(lrs, mem_area, mem_type_set, sg, verbose_allocation):
Tim Hall79d07d22020-04-27 18:20:16 +0100124 if verbose_allocation:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200125 if mem_type_set == set((MemType.Permanent_NPU,)) or mem_type_set == set((MemType.Permanent_CPU,)):
Tim Hall79d07d22020-04-27 18:20:16 +0100126 print("allocation for", mem_area, "- constant tensors in", sg.placement.name, "subgraph(s)")
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200127 else:
128 print("allocation for", mem_area, "- non-constant tensors in Cpu and Npu subgraphs")
Louis Verhaard1356c2a2020-09-16 10:25:28 +0200129 mem_usage = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100130 for start_time, start, end, name, end_time in sorted(
131 (
132 lr.start_time,
133 tens.address,
134 tens.address + int(math.ceil(tens.storage_size())),
135 tens.name + " " + str(tens.purpose),
136 lr.end_time,
137 )
138 for tens, lr in lrs.ranges.items()
139 ):
140 name = name.replace("\x00", "")
141 print("%9d: %#12x - %#12x: %3d - %3d %s" % ((end - start), start, end, start_time, end_time, name))
Louis Verhaard1356c2a2020-09-16 10:25:28 +0200142 mem_usage = max(mem_usage, end)
143 print("Memory usage: {} ({:#x}) bytes / {:.1f} KB".format(mem_usage, mem_usage, mem_usage / 1024))
Tim Hall79d07d22020-04-27 18:20:16 +0100144 print()
145
Tim Hall79d07d22020-04-27 18:20:16 +0100146
Louis Verhaard226ecaf2021-03-30 10:18:28 +0200147def calculate_allocation_efficiency(lrs: List[LiveRange]):
148 size_at_time = [0] * (1 + max(lr.end_time for lr in lrs))
149 for lr in lrs:
erik.andersson@arm.com3438c922021-03-24 10:32:09 +0100150 for t in range(lr.start_time, lr.end_time + 1):
151 size_at_time[t] += lr.size
152
153 return max(size_at_time)
154
155
Tim Hall79d07d22020-04-27 18:20:16 +0100156def allocate_tensors(
157 nng,
158 sg,
159 arch,
160 mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200161 mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100162 tensor_allocator=TensorAllocator.Greedy,
163 verbose_allocation=False,
Tim Hall79d07d22020-04-27 18:20:16 +0100164 lr_graph=None,
Tim Hallb9b515c2020-11-01 21:27:19 +0000165 cpu_tensor_alignment=Tensor.AllocationQuantum,
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200166 max_size=None,
167 dry_test=False,
Tim Hall79d07d22020-04-27 18:20:16 +0100168):
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200169 # Allocates addresses to tensors, returns False if tensors could not be fit within max_size
Tim Hall79d07d22020-04-27 18:20:16 +0100170 ignore_subgraph_input_output_tensors = False
171 lrs = live_range.extract_live_ranges_from_cascaded_passes(
172 sg,
173 mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200174 mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100175 ignore_subgraph_input_output_tensors=ignore_subgraph_input_output_tensors,
176 lr_graph=lr_graph,
Tim Hallb9b515c2020-11-01 21:27:19 +0000177 cpu_tensor_alignment=cpu_tensor_alignment,
Tim Hall79d07d22020-04-27 18:20:16 +0100178 )
179
180 if lrs.ranges:
181 tens_alloc = tensor_allocator
182 if tens_alloc == TensorAllocator.Greedy:
Tim Hallb9b515c2020-11-01 21:27:19 +0000183 total_sz = greedy_allocate_live_ranges(sg, arch, lrs, mem_area, cpu_tensor_alignment, verbose_allocation)
Louis Verhaard9bfe0f82020-12-03 12:26:25 +0100184 verify_allocation(lrs, cpu_tensor_alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100185 elif tens_alloc == TensorAllocator.LinearAlloc:
Tim Hallb9b515c2020-11-01 21:27:19 +0000186 total_sz = linear_allocate_live_ranges(lrs, cpu_tensor_alignment)
Louis Verhaardd7002522021-01-20 17:23:54 +0100187 elif tens_alloc == TensorAllocator.HillClimb:
188 total_sz = hillclimb_allocate_live_ranges(lrs, cpu_tensor_alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100189 else:
190 assert 0
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200191 alloc_ok = max_size is None or total_sz <= max_size
192 if dry_test or not alloc_ok:
193 # Dry test or allocation failed; undo allocation
194 for lr in lrs.ranges.values():
195 lr.set_address(None)
196 return alloc_ok
Tim Hall79d07d22020-04-27 18:20:16 +0100197
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200198 if sg.memory_used.get(mem_area, 0) == 0:
199 sg.memory_used[mem_area] = total_sz
200 else:
201 sg.memory_used[mem_area] += total_sz
202
203 # Keep track of how much should be used for scratch or permanent storage for NPU
204 for mem_type in mem_type_set:
205 if sg.memory_used_per_type.get(mem_type, 0) == 0:
206 sg.memory_used_per_type[mem_type] = total_sz
207 else:
208 sg.memory_used_per_type[mem_type] += total_sz
Tim Hall79d07d22020-04-27 18:20:16 +0100209
Tim Hallb9b515c2020-11-01 21:27:19 +0000210 print_allocation(lrs, mem_area, mem_type_set, sg, verbose_allocation)
Tim Hall79d07d22020-04-27 18:20:16 +0100211
212 if mem_area == MemArea.Sram:
Louis Verhaard226ecaf2021-03-30 10:18:28 +0200213 sg.min_mem_usage = calculate_allocation_efficiency(lrs.lrs)
Tim Hall79d07d22020-04-27 18:20:16 +0100214 # Mark Sram usage for all subgraphs
215 for sg_ in nng.subgraphs:
216 mark_sram_used_for_cascaded_passes(sg_, lrs)
217
218 if sg == nng.get_root_subgraph():
219 nng.memory_used = sg.memory_used
Diqing Zhong66d7ec02021-02-01 19:07:04 +0100220 try:
221 nng.weights_compression_ratio = nng.total_compressed_weights / nng.total_original_weights
222 except ZeroDivisionError:
223 nng.weights_compression_ratio = 0.0
Diqing Zhongdb5124c2021-01-11 12:52:48 +0100224
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200225 return True