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