blob: f29296d1a68f334cae541cab5abcdd319c33c5df [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
Tim Hall79d07d22020-04-27 18:20:16 +010029
30
Louis Verhaard3c07c972020-05-07 08:12:58 +020031def linear_allocate_live_ranges(live_ranges, alloc_granularity=16):
32 # Allocates using increasing addresses. Duplicate constant tensors will be allocated to the same address
Tim Hall79d07d22020-04-27 18:20:16 +010033 total_sz = 0
34 allocated_tensors = []
35
Louis Verhaard3c07c972020-05-07 08:12:58 +020036 # just assign increasing addresses, except for duplicates
Tim Hall79d07d22020-04-27 18:20:16 +010037 for tens, lr in live_ranges.ranges.items():
38 if tens in allocated_tensors:
39 continue
40
Louis Verhaard3c07c972020-05-07 08:12:58 +020041 address = total_sz
42 if tens.weight_compression_config is not None:
43 for allocated_tens in allocated_tensors:
44 if allocated_tens.weight_compression_config == tens.weight_compression_config:
45 address = allocated_tens.address
46 break
47 lr.set_address(address)
Tim Hall79d07d22020-04-27 18:20:16 +010048 allocated_tensors += lr.tensors
Louis Verhaard3c07c972020-05-07 08:12:58 +020049 if address == total_sz:
50 total_sz += numeric_util.round_up(int(math.ceil(lr.size)), alloc_granularity)
Tim Hall79d07d22020-04-27 18:20:16 +010051
52 return total_sz
53
54
55def mark_sram_used_for_cascaded_passes(sg, lrs):
56 end_pos = max(ps.time for ps in sg.cascaded_passes) + 2
57 mem_usage = np.zeros(end_pos, dtype=np.int64)
58
59 for tens, rng in lrs.ranges.items():
60 storage_size = tens.storage_size()
61 mem_usage[rng.start_time : rng.end_time] += storage_size
62
63 for cps in sg.cascaded_passes:
64 sram_used = max(mem_usage[cps.time], mem_usage[cps.time + 1])
65 cps.sram_used = sram_used
66 for ps in cps.passes:
67 ps.sram_used = sram_used
68
69
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020070def print_allocation(lrs, mem_area, mem_type_set, sg, verbose_allocation, show_minimum_possible_allocation):
Tim Hall79d07d22020-04-27 18:20:16 +010071 if verbose_allocation:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020072 if mem_type_set == set((MemType.Permanent_NPU,)) or mem_type_set == set((MemType.Permanent_CPU,)):
Tim Hall79d07d22020-04-27 18:20:16 +010073 print("allocation for", mem_area, "- constant tensors in", sg.placement.name, "subgraph(s)")
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020074 else:
75 print("allocation for", mem_area, "- non-constant tensors in Cpu and Npu subgraphs")
76
Tim Hall79d07d22020-04-27 18:20:16 +010077 for start_time, start, end, name, end_time in sorted(
78 (
79 lr.start_time,
80 tens.address,
81 tens.address + int(math.ceil(tens.storage_size())),
82 tens.name + " " + str(tens.purpose),
83 lr.end_time,
84 )
85 for tens, lr in lrs.ranges.items()
86 ):
87 name = name.replace("\x00", "")
88 print("%9d: %#12x - %#12x: %3d - %3d %s" % ((end - start), start, end, start_time, end_time, name))
89 print()
90
91 if show_minimum_possible_allocation and mem_area == MemArea.Sram:
92 min_possible_allocation = max(cps.sram_used for cps in sg.cascaded_passes)
93 print(
94 "Min possible allocation %d bytes / %.1f KB / %.1f MB"
95 % (min_possible_allocation, min_possible_allocation / 1024, min_possible_allocation / 1024 / 1024)
96 )
97
98
99def allocate_tensors(
100 nng,
101 sg,
102 arch,
103 mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200104 mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100105 use_ifm_ofm_overlap=True,
106 tensor_allocator=TensorAllocator.Greedy,
107 verbose_allocation=False,
108 show_minimum_possible_allocation=False,
109 lr_graph=None,
110):
111 ignore_subgraph_input_output_tensors = False
112 lrs = live_range.extract_live_ranges_from_cascaded_passes(
113 sg,
114 mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200115 mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100116 mark_output_tensors_overlapping_with_input_tensors=False,
117 use_ifm_ofm_overlap=use_ifm_ofm_overlap,
118 ignore_subgraph_input_output_tensors=ignore_subgraph_input_output_tensors,
119 lr_graph=lr_graph,
120 )
121
122 if lrs.ranges:
123 tens_alloc = tensor_allocator
124 if tens_alloc == TensorAllocator.Greedy:
125 total_sz = greedy_allocate_live_ranges(sg, arch, lrs, mem_area, verbose_allocation)
126 elif tens_alloc == TensorAllocator.LinearAlloc:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200127 total_sz = linear_allocate_live_ranges(lrs, 16)
Tim Hall79d07d22020-04-27 18:20:16 +0100128 else:
129 assert 0
130
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200131 if sg.memory_used.get(mem_area, 0) == 0:
132 sg.memory_used[mem_area] = total_sz
133 else:
134 sg.memory_used[mem_area] += total_sz
135
136 # Keep track of how much should be used for scratch or permanent storage for NPU
137 for mem_type in mem_type_set:
138 if sg.memory_used_per_type.get(mem_type, 0) == 0:
139 sg.memory_used_per_type[mem_type] = total_sz
140 else:
141 sg.memory_used_per_type[mem_type] += total_sz
Tim Hall79d07d22020-04-27 18:20:16 +0100142
143 nng.total_size[mem_area] = nng.total_size.get(mem_area, 0) + sum(tens.storage_size() for tens in lrs.ranges)
144 nng.total_elements[mem_area] = nng.total_elements.get(mem_area, 0) + sum(tens.elements() for tens in lrs.ranges)
145
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200146 print_allocation(lrs, mem_area, mem_type_set, sg, verbose_allocation, show_minimum_possible_allocation)
Tim Hall79d07d22020-04-27 18:20:16 +0100147
148 if mem_area == MemArea.Sram:
149 # Mark Sram usage for all subgraphs
150 for sg_ in nng.subgraphs:
151 mark_sram_used_for_cascaded_passes(sg_, lrs)
152
153 if sg == nng.get_root_subgraph():
154 nng.memory_used = sg.memory_used
155 for mem_area in nng.total_elements.keys():
156 try:
157 nng.bits_per_element[mem_area] = nng.total_size[mem_area] * 8 / nng.total_elements[mem_area]
158 except ZeroDivisionError:
159 nng.bits_per_element[mem_area] = 0.0