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