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 | # Contains the main sequencing of the compiler. |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 18 | import time |
| 19 | |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 20 | from . import extract_npu_subgraphs |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 21 | from . import graph_optimiser |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 22 | from . import high_level_command_stream_generator |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | from . import insert_dma |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 24 | from . import live_range |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 25 | from . import lut |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 26 | from . import mark_tensors |
| 27 | from . import npu_performance |
| 28 | from . import npu_serialisation |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 29 | from . import pass_packing |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 30 | from . import register_command_stream_generator |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 31 | from . import scheduler |
| 32 | from . import tensor_allocation |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 33 | from . import weight_compressor |
Patrik Gustavsson | c0bb899 | 2020-08-11 16:45:35 +0200 | [diff] [blame] | 34 | from .errors import VelaError |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 35 | from .nn_graph import PassPlacement |
| 36 | from .nn_graph import TensorAllocator |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 37 | from .rewrite_graph import verify_graph_health |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 38 | from .tensor import MemType |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 39 | from .tensor import Tensor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class CompilerOptions: |
| 43 | """Set of options to change compiler behaviour - verbosity, targets, turning off passes. |
| 44 | |
| 45 | Note the difference between ArchitectureFeatures and CompilerOptions |
| 46 | - ArchitectureFeatures is for changing the Ethos-U55 and system architecture |
| 47 | - CompilerOptions is for changing the behaviour of the compiler |
| 48 | """ |
| 49 | |
| 50 | def __init__( |
| 51 | self, |
| 52 | verbose_graph=False, |
| 53 | verbose_quantization=False, |
| 54 | verbose_packing=False, |
| 55 | verbose_tensor_purpose=False, |
| 56 | verbose_tensor_format=False, |
| 57 | verbose_allocation=False, |
| 58 | verbose_high_level_command_stream=False, |
| 59 | verbose_register_command_stream=False, |
| 60 | verbose_operators=False, |
| 61 | show_minimum_possible_allocation=False, |
| 62 | show_cpu_operations=False, |
| 63 | tensor_allocator=TensorAllocator.Greedy, |
| 64 | timing=False, |
| 65 | output_dir="outputs", |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 66 | allocation_alignment=Tensor.AllocationQuantum, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 67 | ): |
| 68 | |
| 69 | self.verbose_graph = verbose_graph |
| 70 | self.verbose_quantization = verbose_quantization |
| 71 | self.verbose_packing = verbose_packing |
| 72 | self.verbose_tensor_purpose = verbose_tensor_purpose |
| 73 | self.verbose_tensor_format = verbose_tensor_format |
| 74 | self.verbose_allocation = verbose_allocation |
| 75 | self.verbose_high_level_command_stream = verbose_high_level_command_stream |
| 76 | self.verbose_register_command_stream = verbose_register_command_stream |
| 77 | self.verbose_operators = verbose_operators |
| 78 | self.show_minimum_possible_allocation = show_minimum_possible_allocation |
| 79 | self.show_cpu_operations = show_cpu_operations |
| 80 | self.tensor_allocator = tensor_allocator |
| 81 | self.timing = timing |
| 82 | self.output_dir = output_dir |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 83 | self.allocation_alignment = allocation_alignment |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 84 | |
| 85 | def __str__(self): |
| 86 | return type(self).__name__ + ": " + str(self.__dict__) |
| 87 | |
| 88 | __repr__ = __str__ |
| 89 | |
| 90 | |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 91 | def next_sram_factor(alloc_results): |
| 92 | # Bisects to find the max SRAM usage that successfully can be fitted with the tensor allocator. |
| 93 | # Returns tuple (factor, dry_test), with factor is None (stop) or 0 <= factor <= 1 (next SRAM factor to try), |
| 94 | # dry_test is True while still bisecting. |
| 95 | upper = 1.0 |
| 96 | lower = 0.7 |
| 97 | MAX_ITERATIONS = 8 |
| 98 | if len(alloc_results) == 0: |
| 99 | # First iteration, try max SRAM, keep the result if it succeeds |
| 100 | return (upper, False) |
| 101 | elif len(alloc_results) == 1: |
| 102 | if alloc_results[0]: |
| 103 | # The allocator succeeded at first try; stop |
| 104 | return (None, False) |
| 105 | else: |
| 106 | # Start bisecting, try lowerbound SRAM |
| 107 | return (lower, True) |
| 108 | elif len(alloc_results) > MAX_ITERATIONS: |
| 109 | # Stop |
| 110 | return (None, False) |
| 111 | if not alloc_results[1]: |
| 112 | # Allocation at lower failed; search interval 0 - lower |
| 113 | upper = lower |
| 114 | lower = 0 |
| 115 | best = lower |
| 116 | for success in alloc_results[2:]: |
| 117 | middle = (lower + upper) / 2 |
| 118 | if success: |
| 119 | best = max(best, middle) |
| 120 | lower = middle |
| 121 | else: |
| 122 | upper = middle |
| 123 | if len(alloc_results) == MAX_ITERATIONS: |
| 124 | # Done bisecting; repeat the best match, but not as dry test |
| 125 | return (best, False) |
| 126 | # Next try; run only as dry test |
| 127 | return ((lower + upper) / 2, True) |
| 128 | |
| 129 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 130 | def compiler_driver(nng, arch, options, scheduler_options): |
| 131 | assert verify_graph_health(nng) |
| 132 | nng = graph_optimiser.optimise_graph_a(nng, arch, options.verbose_graph) |
| 133 | assert verify_graph_health(nng) |
| 134 | |
| 135 | if options.verbose_quantization: |
| 136 | nng.print_graph_with_tensor_quantization() |
| 137 | |
| 138 | nng = graph_optimiser.optimise_graph_b(nng, arch, options.verbose_graph) |
| 139 | assert verify_graph_health(nng) |
| 140 | |
| 141 | nng = mark_tensors.mark_tensor_purpose(nng, arch, options.verbose_tensor_purpose) |
| 142 | assert verify_graph_health(nng) |
| 143 | nng = insert_dma.insert_dma_commands(nng, arch, options.verbose_graph) |
| 144 | assert verify_graph_health(nng) |
| 145 | pass_packing.pack_into_passes(nng, arch, options.verbose_packing) |
| 146 | assert verify_graph_health(nng) |
| 147 | |
| 148 | extract_npu_subgraphs.extract_npu_subgraphs(nng, arch) |
| 149 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 150 | assert verify_graph_health(nng) |
| 151 | if options.timing: |
| 152 | start = time.time() |
| 153 | |
| 154 | # Run the scheduler |
| 155 | scheduler.schedule_passes(nng, arch, scheduler_options) |
| 156 | |
| 157 | if options.timing: |
| 158 | stop = time.time() |
| 159 | print("Scheduling took %f s" % (stop - start)) |
| 160 | start = time.time() |
| 161 | |
| 162 | # Update the compressed weights now that we have determined the |
| 163 | # block config, and calc and pack the scales and biases |
| 164 | weight_compressor.update_pass_weight_and_scale_tensors(nng, arch) |
| 165 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 166 | # LiveRanges for constant tensors for all Npu subgraphs |
| 167 | permanent_storage = arch.permanent_storage_mem_area |
| 168 | lr_graph_flash = live_range.LiveRangeGraph() |
| 169 | |
| 170 | # Placeholders for scratch and flash tensors that are common for all Npu subgraphs |
| 171 | scratch_tens = None |
Patrik Gustavsson | 3ab9452 | 2020-06-29 17:36:55 +0200 | [diff] [blame] | 172 | scratch_fast_tens = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 173 | flash_tens = None |
| 174 | |
| 175 | # Calculate live ranges for all constant Npu tensors, in permanent storage |
| 176 | for sg in nng.subgraphs: |
| 177 | if sg.placement == PassPlacement.Npu: |
| 178 | lr_graph_flash = live_range.extract_live_ranges_from_cascaded_passes( |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 179 | sg, |
| 180 | permanent_storage, |
| 181 | MemType.Permanent_NPU, |
| 182 | ignore_subgraph_input_output_tensors=True, |
| 183 | lr_graph=lr_graph_flash, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 184 | ) |
| 185 | |
Tim Hall | 25f605c | 2020-05-18 18:04:26 +0100 | [diff] [blame] | 186 | if len(nng.subgraphs) > 1: |
| 187 | # Allocate all Npu constant tensors to the first Npu subgraph since it is |
| 188 | # processed first during serialization into tensors |
| 189 | first_npu_sg = nng.subgraphs[1] |
| 190 | assert first_npu_sg.placement == PassPlacement.Npu |
Tim Hall | 25f605c | 2020-05-18 18:04:26 +0100 | [diff] [blame] | 191 | tensor_allocation.allocate_tensors( |
| 192 | nng, |
| 193 | first_npu_sg, |
| 194 | arch, |
| 195 | permanent_storage, |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 196 | set((MemType.Permanent_NPU,)), |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 197 | tensor_allocator=TensorAllocator.LinearAlloc, |
| 198 | verbose_allocation=options.verbose_allocation, |
| 199 | show_minimum_possible_allocation=options.show_minimum_possible_allocation, |
| 200 | lr_graph=lr_graph_flash, |
Tim Hall | 25f605c | 2020-05-18 18:04:26 +0100 | [diff] [blame] | 201 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 202 | |
| 203 | # Allocate all non-constant tensors to the root, i.e. Cpu, subgraph. This step |
| 204 | # will start at the root subgraph's input and traverse from top to bottom. When |
| 205 | # it comes across an Npu-op it will extract live ranges for it's corresponding |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 206 | # Npu subgraph and add them to the root's live range graph. |
| 207 | # The non-constant tensors are stored either in arch.feature_map_storage_mem_area or |
| 208 | # arch.fast_storage_mem_area. |
| 209 | # When these memory areas are the same, all non-constant tensors are allocated together. |
| 210 | # Otherwise they are allocated separately. |
| 211 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 212 | root_sg = nng.get_root_subgraph() |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 213 | |
| 214 | alloc_list = [] |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 215 | feature_maps_in_fast_storage = arch.feature_map_storage_mem_area == arch.fast_storage_mem_area |
| 216 | if feature_maps_in_fast_storage: |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 217 | mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast))) |
| 218 | alloc_list.append(mem_alloc_scratch) |
| 219 | else: |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 220 | mem_alloc_scratch_fast = (arch.fast_storage_mem_area, set((MemType.Scratch_fast,))) |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 221 | mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch,))) |
| 222 | # Order is important |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 223 | alloc_list.append(mem_alloc_scratch_fast) |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 224 | alloc_list.append(mem_alloc_scratch) |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 225 | |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 226 | for mem_area, mem_type_set in alloc_list: |
| 227 | if feature_maps_in_fast_storage or mem_area != arch.fast_storage_mem_area: |
| 228 | tensor_allocation.allocate_tensors( |
| 229 | nng, |
| 230 | root_sg, |
| 231 | arch, |
| 232 | mem_area, |
| 233 | mem_type_set, |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 234 | tensor_allocator=options.tensor_allocator, |
| 235 | verbose_allocation=options.verbose_allocation, |
| 236 | show_minimum_possible_allocation=options.show_minimum_possible_allocation, |
| 237 | allocation_alignment=options.allocation_alignment, |
| 238 | ) |
| 239 | else: |
| 240 | # For the case where scratch_fast != scratch: attempt to place feature maps used between |
| 241 | # cascaded passes in fast storage. Bisection is used to find the max possible usage of SRAM. |
| 242 | alloc_results = [] |
| 243 | while True: |
| 244 | assert len(alloc_results) < 10, "Infinite allocator loop" |
| 245 | sram_factor, dry_test = next_sram_factor(alloc_results) |
| 246 | if sram_factor is None: |
| 247 | break |
| 248 | # Try to move as many feature maps as possible to SRAM before allocating |
| 249 | sram_limit = sram_factor * arch.sram_size |
| 250 | for sg in nng.subgraphs: |
| 251 | scheduler.use_fast_storage_for_feature_maps(sg, sram_limit, arch) |
| 252 | alloc_success = tensor_allocation.allocate_tensors( |
| 253 | nng, |
| 254 | root_sg, |
| 255 | arch, |
| 256 | mem_area, |
| 257 | mem_type_set, |
| 258 | max_size=arch.sram_size, |
| 259 | dry_test=dry_test, |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 260 | tensor_allocator=options.tensor_allocator, |
| 261 | verbose_allocation=options.verbose_allocation, |
| 262 | show_minimum_possible_allocation=options.show_minimum_possible_allocation, |
| 263 | allocation_alignment=options.allocation_alignment, |
| 264 | ) |
| 265 | if dry_test or not alloc_success: |
| 266 | for sg in nng.subgraphs: |
| 267 | scheduler.undo_use_fast_storage(sg, arch) |
| 268 | alloc_results.append(alloc_success) |
| 269 | if not alloc_results[-1]: |
| 270 | raise VelaError( |
| 271 | "Sram limit {} bytes, has been exceeded by the scratch fast tensor. " |
| 272 | "Increasing the value of --weight-estimation-scaling may help to resolve the issue. " |
| 273 | "See OPTIONS.md for more information.".format(arch.sram_size) |
| 274 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 275 | |
| 276 | # Generate command streams and serialise Npu-ops into tensors |
| 277 | for sg in nng.subgraphs: |
| 278 | high_level_command_stream_generator.generate_high_level_command_stream( |
| 279 | nng, sg, arch, options.verbose_high_level_command_stream |
| 280 | ) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 281 | lut.optimize_high_level_cmd_stream(sg, arch) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 282 | register_command_stream_generator.generate_register_command_stream( |
| 283 | nng, sg, arch, options.verbose_register_command_stream |
| 284 | ) |
Patrik Gustavsson | 3ab9452 | 2020-06-29 17:36:55 +0200 | [diff] [blame] | 285 | scratch_tens, scratch_fast_tens, flash_tens = npu_serialisation.serialise_npu_subgraph_into_tensors( |
| 286 | nng, sg, arch, scratch_tens, scratch_fast_tens, flash_tens |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 287 | ) |
| 288 | |
| 289 | npu_serialisation.rewrite_npu_call_ops(nng, root_sg, arch) |
| 290 | |
Jacob Bohlin | 268394d | 2020-08-13 13:24:59 +0200 | [diff] [blame] | 291 | # Set Scratch and Fast_scratch Tensor size |
| 292 | if scratch_tens is not None: |
| 293 | scratch_tens.set_all_shapes([root_sg.memory_used_per_type.get(MemType.Scratch, 0)]) |
| 294 | if scratch_fast_tens is not None: |
| 295 | scratch_fast_tens.set_all_shapes([root_sg.memory_used_per_type.get(MemType.Scratch_fast, 0)]) |
| 296 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 297 | # Allocate all Cpu constant tensors, this is done last because the Npu-ops |
| 298 | # have to be serialized into flash and scratch tensors first |
| 299 | tensor_allocation.allocate_tensors( |
| 300 | nng, |
| 301 | root_sg, |
| 302 | arch, |
| 303 | permanent_storage, |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 304 | set((MemType.Permanent_CPU,)), |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 305 | tensor_allocator=TensorAllocator.LinearAlloc, |
| 306 | verbose_allocation=options.verbose_allocation, |
| 307 | show_minimum_possible_allocation=options.show_minimum_possible_allocation, |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 308 | allocation_alignment=options.allocation_alignment, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 309 | ) |
| 310 | |
| 311 | npu_performance.calc_performance_for_network(nng, arch) |