blob: 92fe58407d2257914bd1410d16ee84cccec872e2 [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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Contains the main sequencing of the compiler.
Diego Russoea6111a2020-04-14 18:41:58 +010018import time
19
Diego Russoe8a10452020-04-21 17:39:10 +010020from . import extract_npu_subgraphs
Tim Hall79d07d22020-04-27 18:20:16 +010021from . import graph_optimiser
Diego Russoe8a10452020-04-21 17:39:10 +010022from . import high_level_command_stream_generator
Tim Hall79d07d22020-04-27 18:20:16 +010023from . import insert_dma
Diego Russoe8a10452020-04-21 17:39:10 +010024from . import live_range
Louis Verhaard0b8268a2020-08-05 16:11:29 +020025from . import lut
Diego Russoe8a10452020-04-21 17:39:10 +010026from . import mark_tensors
27from . import npu_performance
28from . import npu_serialisation
Tim Hall79d07d22020-04-27 18:20:16 +010029from . import pass_packing
Diego Russoe8a10452020-04-21 17:39:10 +010030from . import register_command_stream_generator
Tim Hall79d07d22020-04-27 18:20:16 +010031from . import scheduler
32from . import tensor_allocation
Tim Hall79d07d22020-04-27 18:20:16 +010033from . import weight_compressor
Patrik Gustavssonc0bb8992020-08-11 16:45:35 +020034from .errors import VelaError
Diego Russoe8a10452020-04-21 17:39:10 +010035from .nn_graph import PassPlacement
36from .nn_graph import TensorAllocator
Diego Russoea6111a2020-04-14 18:41:58 +010037from .rewrite_graph import verify_graph_health
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020038from .tensor import MemType
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020039from .tensor import Tensor
Tim Hall79d07d22020-04-27 18:20:16 +010040
41
42class CompilerOptions:
43 """Set of options to change compiler behaviour - verbosity, targets, turning off passes.
44
45Note 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 Bohlin0628a8c2020-08-28 13:25:14 +020066 allocation_alignment=Tensor.AllocationQuantum,
Tim Hall79d07d22020-04-27 18:20:16 +010067 ):
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 Bohlin0628a8c2020-08-28 13:25:14 +020083 self.allocation_alignment = allocation_alignment
Tim Hall79d07d22020-04-27 18:20:16 +010084
85 def __str__(self):
86 return type(self).__name__ + ": " + str(self.__dict__)
87
88 __repr__ = __str__
89
90
91def compiler_driver(nng, arch, options, scheduler_options):
92 assert verify_graph_health(nng)
93 nng = graph_optimiser.optimise_graph_a(nng, arch, options.verbose_graph)
94 assert verify_graph_health(nng)
95
96 if options.verbose_quantization:
97 nng.print_graph_with_tensor_quantization()
98
99 nng = graph_optimiser.optimise_graph_b(nng, arch, options.verbose_graph)
100 assert verify_graph_health(nng)
101
102 nng = mark_tensors.mark_tensor_purpose(nng, arch, options.verbose_tensor_purpose)
103 assert verify_graph_health(nng)
104 nng = insert_dma.insert_dma_commands(nng, arch, options.verbose_graph)
105 assert verify_graph_health(nng)
106 pass_packing.pack_into_passes(nng, arch, options.verbose_packing)
107 assert verify_graph_health(nng)
108
109 extract_npu_subgraphs.extract_npu_subgraphs(nng, arch)
110
111 mark_tensors.mark_tensor_format(nng, arch, options.verbose_tensor_format)
112 assert verify_graph_health(nng)
113 if options.timing:
114 start = time.time()
115
116 # Run the scheduler
117 scheduler.schedule_passes(nng, arch, scheduler_options)
118
119 if options.timing:
120 stop = time.time()
121 print("Scheduling took %f s" % (stop - start))
122 start = time.time()
123
124 # Update the compressed weights now that we have determined the
125 # block config, and calc and pack the scales and biases
126 weight_compressor.update_pass_weight_and_scale_tensors(nng, arch)
127
Tim Hall79d07d22020-04-27 18:20:16 +0100128 # LiveRanges for constant tensors for all Npu subgraphs
129 permanent_storage = arch.permanent_storage_mem_area
130 lr_graph_flash = live_range.LiveRangeGraph()
131
132 # Placeholders for scratch and flash tensors that are common for all Npu subgraphs
133 scratch_tens = None
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200134 scratch_fast_tens = None
Tim Hall79d07d22020-04-27 18:20:16 +0100135 flash_tens = None
136
137 # Calculate live ranges for all constant Npu tensors, in permanent storage
138 for sg in nng.subgraphs:
139 if sg.placement == PassPlacement.Npu:
140 lr_graph_flash = live_range.extract_live_ranges_from_cascaded_passes(
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200141 sg,
142 permanent_storage,
143 MemType.Permanent_NPU,
144 ignore_subgraph_input_output_tensors=True,
145 lr_graph=lr_graph_flash,
Tim Hall79d07d22020-04-27 18:20:16 +0100146 )
147
Tim Hall25f605c2020-05-18 18:04:26 +0100148 if len(nng.subgraphs) > 1:
149 # Allocate all Npu constant tensors to the first Npu subgraph since it is
150 # processed first during serialization into tensors
151 first_npu_sg = nng.subgraphs[1]
152 assert first_npu_sg.placement == PassPlacement.Npu
Tim Hall25f605c2020-05-18 18:04:26 +0100153 tensor_allocation.allocate_tensors(
154 nng,
155 first_npu_sg,
156 arch,
157 permanent_storage,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200158 set((MemType.Permanent_NPU,)),
Tim Hall25f605c2020-05-18 18:04:26 +0100159 scheduler_options.use_ifm_ofm_overlap,
160 TensorAllocator.LinearAlloc,
161 options.verbose_allocation,
162 options.show_minimum_possible_allocation,
163 lr_graph_flash,
164 )
Tim Hall79d07d22020-04-27 18:20:16 +0100165
166 # Allocate all non-constant tensors to the root, i.e. Cpu, subgraph. This step
167 # will start at the root subgraph's input and traverse from top to bottom. When
168 # it comes across an Npu-op it will extract live ranges for it's corresponding
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200169 # Npu subgraph and add them to the root's live range graph.
170 # The non-constant tensors are stored either in arch.feature_map_storage_mem_area or
171 # arch.fast_storage_mem_area.
172 # When these memory areas are the same, all non-constant tensors are allocated together.
173 # Otherwise they are allocated separately.
174
Tim Hall79d07d22020-04-27 18:20:16 +0100175 root_sg = nng.get_root_subgraph()
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200176
177 alloc_list = []
178 if arch.feature_map_storage_mem_area == arch.fast_storage_mem_area:
179 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))
180 alloc_list.append(mem_alloc_scratch)
181 else:
182 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch,)))
183 mem_alloc_scratch_fast = (arch.fast_storage_mem_area, set((MemType.Scratch_fast,)))
184 alloc_list.append(mem_alloc_scratch)
185 alloc_list.append(mem_alloc_scratch_fast)
186
187 for alloc in alloc_list:
188 tensor_allocation.allocate_tensors(
189 nng,
190 root_sg,
191 arch,
192 alloc[0],
193 alloc[1],
194 scheduler_options.use_ifm_ofm_overlap,
195 options.tensor_allocator,
196 options.verbose_allocation,
197 options.show_minimum_possible_allocation,
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200198 allocation_alignment=options.allocation_alignment,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200199 )
Tim Hall79d07d22020-04-27 18:20:16 +0100200
201 # Generate command streams and serialise Npu-ops into tensors
202 for sg in nng.subgraphs:
203 high_level_command_stream_generator.generate_high_level_command_stream(
204 nng, sg, arch, options.verbose_high_level_command_stream
205 )
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200206 lut.optimize_high_level_cmd_stream(sg, arch)
Tim Hall79d07d22020-04-27 18:20:16 +0100207 register_command_stream_generator.generate_register_command_stream(
208 nng, sg, arch, options.verbose_register_command_stream
209 )
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200210 scratch_tens, scratch_fast_tens, flash_tens = npu_serialisation.serialise_npu_subgraph_into_tensors(
211 nng, sg, arch, scratch_tens, scratch_fast_tens, flash_tens
Tim Hall79d07d22020-04-27 18:20:16 +0100212 )
213
214 npu_serialisation.rewrite_npu_call_ops(nng, root_sg, arch)
215
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200216 if root_sg is not None and (arch.feature_map_storage_mem_area != arch.fast_storage_mem_area):
217 if root_sg.memory_used_per_type.get(MemType.Scratch_fast, 0) > arch.sram_size:
Patrik Gustavssonc0bb8992020-08-11 16:45:35 +0200218 raise VelaError(
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200219 "Sram limit {} bytes, has been exceeded by the scratch fast tensor {} bytes. "
220 "Increasing the value of --weight-estimation-scaling may help to resolve the issue. "
221 "See OPTIONS.md for more information.".format(
Patrik Gustavssonc0bb8992020-08-11 16:45:35 +0200222 arch.sram_size, root_sg.memory_used_per_type.get(MemType.Scratch_fast, 0)
223 )
224 )
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200225
Tim Hall79d07d22020-04-27 18:20:16 +0100226 # Allocate all Cpu constant tensors, this is done last because the Npu-ops
227 # have to be serialized into flash and scratch tensors first
228 tensor_allocation.allocate_tensors(
229 nng,
230 root_sg,
231 arch,
232 permanent_storage,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200233 set((MemType.Permanent_CPU,)),
Tim Hall79d07d22020-04-27 18:20:16 +0100234 scheduler_options.use_ifm_ofm_overlap,
Louis Verhaard3c07c972020-05-07 08:12:58 +0200235 TensorAllocator.LinearAlloc,
Tim Hall79d07d22020-04-27 18:20:16 +0100236 options.verbose_allocation,
237 options.show_minimum_possible_allocation,
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200238 allocation_alignment=options.allocation_alignment,
Tim Hall79d07d22020-04-27 18:20:16 +0100239 )
240
241 npu_performance.calc_performance_for_network(nng, arch)