blob: 94aa60884914a8f73f98eeafdc629732bad88084 [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# Wrapping function to do tensor address allocation. That is, assigning addresses to tensors based on what has been
20# worked out from the allowable overlaps that are calculated by the live range analysis.
21
22from . import live_range
23from .tensor import MemArea
24import math
25from . import numeric_util
26import numpy as np
27from .nn_graph import TensorAllocator, PassPlacement
28
29from .greedy_allocation import allocate_live_ranges as greedy_allocate_live_ranges
30
31
32def linear_allocate_live_ranges(live_ranges, alloc_granularity=256):
33 total_sz = 0
34 allocated_tensors = []
35
36 # just assign increasing addresses
37 for tens, lr in live_ranges.ranges.items():
38 if tens in allocated_tensors:
39 continue
40
41 lr.set_address(total_sz)
42 allocated_tensors += lr.tensors
43 total_sz += numeric_util.round_up(int(math.ceil(lr.size)), alloc_granularity)
44
45 return total_sz
46
47
48def mark_sram_used_for_cascaded_passes(sg, lrs):
49 end_pos = max(ps.time for ps in sg.cascaded_passes) + 2
50 mem_usage = np.zeros(end_pos, dtype=np.int64)
51
52 for tens, rng in lrs.ranges.items():
53 storage_size = tens.storage_size()
54 mem_usage[rng.start_time : rng.end_time] += storage_size
55
56 for cps in sg.cascaded_passes:
57 sram_used = max(mem_usage[cps.time], mem_usage[cps.time + 1])
58 cps.sram_used = sram_used
59 for ps in cps.passes:
60 ps.sram_used = sram_used
61
62
63def print_allocation(lrs, mem_area, sg, verbose_allocation, show_minimum_possible_allocation):
64 if verbose_allocation:
65 if mem_area == MemArea.Sram:
66 print("allocation for", mem_area, "- non-constant tensors in Cpu and Npu subgraphs")
67 else:
68 print("allocation for", mem_area, "- constant tensors in", sg.placement.name, "subgraph(s)")
69 for start_time, start, end, name, end_time in sorted(
70 (
71 lr.start_time,
72 tens.address,
73 tens.address + int(math.ceil(tens.storage_size())),
74 tens.name + " " + str(tens.purpose),
75 lr.end_time,
76 )
77 for tens, lr in lrs.ranges.items()
78 ):
79 name = name.replace("\x00", "")
80 print("%9d: %#12x - %#12x: %3d - %3d %s" % ((end - start), start, end, start_time, end_time, name))
81 print()
82
83 if show_minimum_possible_allocation and mem_area == MemArea.Sram:
84 min_possible_allocation = max(cps.sram_used for cps in sg.cascaded_passes)
85 print(
86 "Min possible allocation %d bytes / %.1f KB / %.1f MB"
87 % (min_possible_allocation, min_possible_allocation / 1024, min_possible_allocation / 1024 / 1024)
88 )
89
90
91def allocate_tensors(
92 nng,
93 sg,
94 arch,
95 mem_area,
96 use_ifm_ofm_overlap=True,
97 tensor_allocator=TensorAllocator.Greedy,
98 verbose_allocation=False,
99 show_minimum_possible_allocation=False,
100 lr_graph=None,
101):
102 ignore_subgraph_input_output_tensors = False
103 lrs = live_range.extract_live_ranges_from_cascaded_passes(
104 sg,
105 mem_area,
106 mark_output_tensors_overlapping_with_input_tensors=False,
107 use_ifm_ofm_overlap=use_ifm_ofm_overlap,
108 ignore_subgraph_input_output_tensors=ignore_subgraph_input_output_tensors,
109 lr_graph=lr_graph,
110 )
111
112 if lrs.ranges:
113 tens_alloc = tensor_allocator
114 if tens_alloc == TensorAllocator.Greedy:
115 total_sz = greedy_allocate_live_ranges(sg, arch, lrs, mem_area, verbose_allocation)
116 elif tens_alloc == TensorAllocator.LinearAlloc:
117 total_sz = linear_allocate_live_ranges(lrs)
118 else:
119 assert 0
120
121 sg.memory_used[mem_area] = total_sz
122
123 nng.total_size[mem_area] = nng.total_size.get(mem_area, 0) + sum(tens.storage_size() for tens in lrs.ranges)
124 nng.total_elements[mem_area] = nng.total_elements.get(mem_area, 0) + sum(tens.elements() for tens in lrs.ranges)
125
126 print_allocation(lrs, mem_area, sg, verbose_allocation, show_minimum_possible_allocation)
127
128 if mem_area == MemArea.Sram:
129 # Mark Sram usage for all subgraphs
130 for sg_ in nng.subgraphs:
131 mark_sram_used_for_cascaded_passes(sg_, lrs)
132
133 if sg == nng.get_root_subgraph():
134 nng.memory_used = sg.memory_used
135 for mem_area in nng.total_elements.keys():
136 try:
137 nng.bits_per_element[mem_area] = nng.total_size[mem_area] * 8 / nng.total_elements[mem_area]
138 except ZeroDivisionError:
139 nng.bits_per_element[mem_area] = 0.0