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 | # Allocate tensor addresses using a greedy algorithm. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 18 | from . import numeric_util |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 19 | from .errors import AllocationError |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class GreedyAllocator: |
| 23 | def __init__(self, nng, arch, live_ranges, mem_area): |
| 24 | self.nng = nng |
| 25 | self.arch = arch |
| 26 | self.mem_area = mem_area |
| 27 | |
| 28 | self.live_ranges = live_ranges |
| 29 | self.memory_required = 0 |
| 30 | |
| 31 | self.current_allocs = [] |
| 32 | |
| 33 | def alloc(self, new_lr): |
| 34 | size = new_lr.size |
| 35 | current_top = 0 |
| 36 | if self.current_allocs: |
| 37 | current_top = max(start_addr + lr.size for start_addr, lr in self.current_allocs) |
| 38 | best_offset = numeric_util.round_up(current_top, new_lr.get_alignment()) |
| 39 | best_offset_fit = (1 << 64) - 1 |
| 40 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 41 | aligned_size = numeric_util.round_up(size, new_lr.get_alignment()) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 42 | current_offset = 0 |
| 43 | for start_addr, lr in self.current_allocs: |
| 44 | aligned_current_offset = numeric_util.round_up(current_offset, new_lr.get_alignment()) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 45 | if aligned_current_offset + aligned_size <= start_addr and start_addr - current_offset < best_offset_fit: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 46 | best_offset = current_offset |
| 47 | best_offset_fit = start_addr - current_offset |
| 48 | |
| 49 | current_offset = start_addr + lr.size |
| 50 | |
Charles Xu | 5b3dcd7 | 2020-05-28 07:20:52 +0200 | [diff] [blame] | 51 | best_offset = new_lr.set_address(best_offset) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 52 | self.memory_required = max(self.memory_required, best_offset + aligned_size) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 53 | self.current_allocs.append((best_offset, new_lr)) |
| 54 | self.current_allocs = list(sorted(self.current_allocs)) |
| 55 | |
| 56 | def dealloc(self, lr_to_dealloc): |
| 57 | self.current_allocs = [(start_addr, lr) for start_addr, lr in self.current_allocs if lr != lr_to_dealloc] |
| 58 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 59 | def allocate_live_ranges(self, verbose_allocation, alignment): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 60 | lrs = set() |
| 61 | for lr in self.live_ranges.ranges.values(): |
| 62 | lrs.add((lr.start_time, lr.end_time, lr)) |
| 63 | |
| 64 | lrs = sorted(lrs) |
| 65 | |
| 66 | for curr_time, _, new_lr in lrs: |
| 67 | for _, lr in list(self.current_allocs): |
| 68 | if lr.end_time < curr_time: |
| 69 | self.dealloc(lr) |
| 70 | |
| 71 | self.alloc(new_lr) |
| 72 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 73 | self.verify_allocation(alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 74 | return self.memory_required |
| 75 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 76 | def verify_allocation(self, alignment): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 77 | lrs = list(self.live_ranges.ranges.values()) |
| 78 | for n in lrs: |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 79 | for tens in n.tensors: |
| 80 | if not all(op and op.run_on_npu for op in tens.ops + tens.consumer_list): |
| 81 | # This is a CPU tensor, verify alignment |
| 82 | if tens.address % alignment != 0: |
| 83 | raise AllocationError("Tensor {} not aligned to {} bytes".format(tens.name, alignment)) |
| 84 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 85 | for m in lrs: |
| 86 | if n != m and n.overlaps_ranges(m): |
| 87 | overlap, tens_n, tens_m = n.overlaps_address(m) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 88 | if overlap and not (tens_n.equivalent(tens_m) and tens_n.address == tens_m.address): |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 89 | raise AllocationError( |
| 90 | "Overlapping buffers: {}: {} -> {} and {}: {} -> {}".format( |
| 91 | n.name, |
| 92 | tens_n.address, |
| 93 | tens_n.address + n.size, |
| 94 | m.name, |
| 95 | tens_m.address, |
| 96 | tens_m.address + m.size, |
| 97 | ) |
| 98 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 99 | |
| 100 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 101 | def allocate_live_ranges(nng, arch, live_ranges, mem_area, alignment, verbose_allocation=False): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 102 | g = GreedyAllocator(nng, arch, live_ranges, mem_area) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame^] | 103 | return g.allocate_live_ranges(verbose_allocation, alignment) |