blob: bb91145ee12ba36f83c3bdff4587dc5666fed870 [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# 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
Tim Hall79d07d22020-04-27 18:20:16 +010020
Diego Russoea6111a2020-04-14 18:41:58 +010021import numpy as np
22
23from . import live_range
24from . import numeric_util
Tim Hall79d07d22020-04-27 18:20:16 +010025from .greedy_allocation import allocate_live_ranges as greedy_allocate_live_ranges
Diego Russoe8a10452020-04-21 17:39:10 +010026from .nn_graph import TensorAllocator
27from .tensor import MemArea
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020028from .tensor import MemType
Louis Verhaard0b8268a2020-08-05 16:11:29 +020029from .tensor import TensorPurpose
Tim Hall79d07d22020-04-27 18:20:16 +010030
31
Louis Verhaard3c07c972020-05-07 08:12:58 +020032def linear_allocate_live_ranges(live_ranges, alloc_granularity=16):
33 # Allocates using increasing addresses. Duplicate constant tensors will be allocated to the same address
Tim Hall79d07d22020-04-27 18:20:16 +010034 total_sz = 0
35 allocated_tensors = []
36
Louis Verhaard3c07c972020-05-07 08:12:58 +020037 # just assign increasing addresses, except for duplicates
Tim Hall79d07d22020-04-27 18:20:16 +010038 for tens, lr in live_ranges.ranges.items():
39 if tens in allocated_tensors:
40 continue
41
Louis Verhaard3c07c972020-05-07 08:12:58 +020042 address = total_sz
43 if tens.weight_compression_config is not None:
44 for allocated_tens in allocated_tensors:
45 if allocated_tens.weight_compression_config == tens.weight_compression_config:
46 address = allocated_tens.address
47 break
Louis Verhaard0b8268a2020-08-05 16:11:29 +020048 if tens.purpose == TensorPurpose.LUT:
49 for allocated_tens in allocated_tensors:
50 if allocated_tens.equivalent(tens):
51 address = allocated_tens.address
52 break
Louis Verhaard3c07c972020-05-07 08:12:58 +020053 lr.set_address(address)
Tim Hall79d07d22020-04-27 18:20:16 +010054 allocated_tensors += lr.tensors
Louis Verhaard3c07c972020-05-07 08:12:58 +020055 if address == total_sz:
56 total_sz += numeric_util.round_up(int(math.ceil(lr.size)), alloc_granularity)
Tim Hall79d07d22020-04-27 18:20:16 +010057
58 return total_sz
59
60
61def mark_sram_used_for_cascaded_passes(sg, lrs):
62 end_pos = max(ps.time for ps in sg.cascaded_passes) + 2
63 mem_usage = np.zeros(end_pos, dtype=np.int64)
64
65 for tens, rng in lrs.ranges.items():
66 storage_size = tens.storage_size()
67 mem_usage[rng.start_time : rng.end_time] += storage_size
68
69 for cps in sg.cascaded_passes:
70 sram_used = max(mem_usage[cps.time], mem_usage[cps.time + 1])
71 cps.sram_used = sram_used
72 for ps in cps.passes:
73 ps.sram_used = sram_used
74
75
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020076def print_allocation(lrs, mem_area, mem_type_set, sg, verbose_allocation, show_minimum_possible_allocation):
Tim Hall79d07d22020-04-27 18:20:16 +010077 if verbose_allocation:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020078 if mem_type_set == set((MemType.Permanent_NPU,)) or mem_type_set == set((MemType.Permanent_CPU,)):
Tim Hall79d07d22020-04-27 18:20:16 +010079 print("allocation for", mem_area, "- constant tensors in", sg.placement.name, "subgraph(s)")
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020080 else:
81 print("allocation for", mem_area, "- non-constant tensors in Cpu and Npu subgraphs")
82
Tim Hall79d07d22020-04-27 18:20:16 +010083 for start_time, start, end, name, end_time in sorted(
84 (
85 lr.start_time,
86 tens.address,
87 tens.address + int(math.ceil(tens.storage_size())),
88 tens.name + " " + str(tens.purpose),
89 lr.end_time,
90 )
91 for tens, lr in lrs.ranges.items()
92 ):
93 name = name.replace("\x00", "")
94 print("%9d: %#12x - %#12x: %3d - %3d %s" % ((end - start), start, end, start_time, end_time, name))
95 print()
96
97 if show_minimum_possible_allocation and mem_area == MemArea.Sram:
98 min_possible_allocation = max(cps.sram_used for cps in sg.cascaded_passes)
99 print(
100 "Min possible allocation %d bytes / %.1f KB / %.1f MB"
101 % (min_possible_allocation, min_possible_allocation / 1024, min_possible_allocation / 1024 / 1024)
102 )
103
104
105def allocate_tensors(
106 nng,
107 sg,
108 arch,
109 mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200110 mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100111 use_ifm_ofm_overlap=True,
112 tensor_allocator=TensorAllocator.Greedy,
113 verbose_allocation=False,
114 show_minimum_possible_allocation=False,
115 lr_graph=None,
116):
117 ignore_subgraph_input_output_tensors = False
118 lrs = live_range.extract_live_ranges_from_cascaded_passes(
119 sg,
120 mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200121 mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100122 mark_output_tensors_overlapping_with_input_tensors=False,
123 use_ifm_ofm_overlap=use_ifm_ofm_overlap,
124 ignore_subgraph_input_output_tensors=ignore_subgraph_input_output_tensors,
125 lr_graph=lr_graph,
126 )
127
128 if lrs.ranges:
129 tens_alloc = tensor_allocator
130 if tens_alloc == TensorAllocator.Greedy:
131 total_sz = greedy_allocate_live_ranges(sg, arch, lrs, mem_area, verbose_allocation)
132 elif tens_alloc == TensorAllocator.LinearAlloc:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200133 total_sz = linear_allocate_live_ranges(lrs, 16)
Tim Hall79d07d22020-04-27 18:20:16 +0100134 else:
135 assert 0
136
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200137 if sg.memory_used.get(mem_area, 0) == 0:
138 sg.memory_used[mem_area] = total_sz
139 else:
140 sg.memory_used[mem_area] += total_sz
141
142 # Keep track of how much should be used for scratch or permanent storage for NPU
143 for mem_type in mem_type_set:
144 if sg.memory_used_per_type.get(mem_type, 0) == 0:
145 sg.memory_used_per_type[mem_type] = total_sz
146 else:
147 sg.memory_used_per_type[mem_type] += total_sz
Tim Hall79d07d22020-04-27 18:20:16 +0100148
149 nng.total_size[mem_area] = nng.total_size.get(mem_area, 0) + sum(tens.storage_size() for tens in lrs.ranges)
150 nng.total_elements[mem_area] = nng.total_elements.get(mem_area, 0) + sum(tens.elements() for tens in lrs.ranges)
151
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200152 print_allocation(lrs, mem_area, mem_type_set, sg, verbose_allocation, show_minimum_possible_allocation)
Tim Hall79d07d22020-04-27 18:20:16 +0100153
154 if mem_area == MemArea.Sram:
155 # Mark Sram usage for all subgraphs
156 for sg_ in nng.subgraphs:
157 mark_sram_used_for_cascaded_passes(sg_, lrs)
158
159 if sg == nng.get_root_subgraph():
160 nng.memory_used = sg.memory_used
161 for mem_area in nng.total_elements.keys():
162 try:
163 nng.bits_per_element[mem_area] = nng.total_size[mem_area] * 8 / nng.total_elements[mem_area]
164 except ZeroDivisionError:
165 nng.bits_per_element[mem_area] = 0.0