blob: b5a6c42d2d8865bb0513d60b60060c2e51397eda [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
25from . import mark_tensors
26from . import npu_performance
27from . import npu_serialisation
Tim Hall79d07d22020-04-27 18:20:16 +010028from . import pass_packing
Diego Russoe8a10452020-04-21 17:39:10 +010029from . import register_command_stream_generator
Tim Hall79d07d22020-04-27 18:20:16 +010030from . import scheduler
31from . import tensor_allocation
Tim Hall79d07d22020-04-27 18:20:16 +010032from . import weight_compressor
Diego Russoe8a10452020-04-21 17:39:10 +010033from .nn_graph import PassPlacement
34from .nn_graph import TensorAllocator
Diego Russoea6111a2020-04-14 18:41:58 +010035from .rewrite_graph import verify_graph_health
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020036from .tensor import MemType
Tim Hall79d07d22020-04-27 18:20:16 +010037
38
39class CompilerOptions:
40 """Set of options to change compiler behaviour - verbosity, targets, turning off passes.
41
42Note the difference between ArchitectureFeatures and CompilerOptions
43- ArchitectureFeatures is for changing the Ethos-U55 and system architecture
44- CompilerOptions is for changing the behaviour of the compiler
45"""
46
47 def __init__(
48 self,
49 verbose_graph=False,
50 verbose_quantization=False,
51 verbose_packing=False,
52 verbose_tensor_purpose=False,
53 verbose_tensor_format=False,
54 verbose_allocation=False,
55 verbose_high_level_command_stream=False,
56 verbose_register_command_stream=False,
57 verbose_operators=False,
58 show_minimum_possible_allocation=False,
59 show_cpu_operations=False,
60 tensor_allocator=TensorAllocator.Greedy,
61 timing=False,
62 output_dir="outputs",
63 ):
64
65 self.verbose_graph = verbose_graph
66 self.verbose_quantization = verbose_quantization
67 self.verbose_packing = verbose_packing
68 self.verbose_tensor_purpose = verbose_tensor_purpose
69 self.verbose_tensor_format = verbose_tensor_format
70 self.verbose_allocation = verbose_allocation
71 self.verbose_high_level_command_stream = verbose_high_level_command_stream
72 self.verbose_register_command_stream = verbose_register_command_stream
73 self.verbose_operators = verbose_operators
74 self.show_minimum_possible_allocation = show_minimum_possible_allocation
75 self.show_cpu_operations = show_cpu_operations
76 self.tensor_allocator = tensor_allocator
77 self.timing = timing
78 self.output_dir = output_dir
79
80 def __str__(self):
81 return type(self).__name__ + ": " + str(self.__dict__)
82
83 __repr__ = __str__
84
85
86def compiler_driver(nng, arch, options, scheduler_options):
87 assert verify_graph_health(nng)
88 nng = graph_optimiser.optimise_graph_a(nng, arch, options.verbose_graph)
89 assert verify_graph_health(nng)
90
91 if options.verbose_quantization:
92 nng.print_graph_with_tensor_quantization()
93
94 nng = graph_optimiser.optimise_graph_b(nng, arch, options.verbose_graph)
95 assert verify_graph_health(nng)
96
97 nng = mark_tensors.mark_tensor_purpose(nng, arch, options.verbose_tensor_purpose)
98 assert verify_graph_health(nng)
99 nng = insert_dma.insert_dma_commands(nng, arch, options.verbose_graph)
100 assert verify_graph_health(nng)
101 pass_packing.pack_into_passes(nng, arch, options.verbose_packing)
102 assert verify_graph_health(nng)
103
104 extract_npu_subgraphs.extract_npu_subgraphs(nng, arch)
105
106 mark_tensors.mark_tensor_format(nng, arch, options.verbose_tensor_format)
107 assert verify_graph_health(nng)
108 if options.timing:
109 start = time.time()
110
111 # Run the scheduler
112 scheduler.schedule_passes(nng, arch, scheduler_options)
113
114 if options.timing:
115 stop = time.time()
116 print("Scheduling took %f s" % (stop - start))
117 start = time.time()
118
119 # Update the compressed weights now that we have determined the
120 # block config, and calc and pack the scales and biases
121 weight_compressor.update_pass_weight_and_scale_tensors(nng, arch)
122
Tim Hall79d07d22020-04-27 18:20:16 +0100123 # LiveRanges for constant tensors for all Npu subgraphs
124 permanent_storage = arch.permanent_storage_mem_area
125 lr_graph_flash = live_range.LiveRangeGraph()
126
127 # Placeholders for scratch and flash tensors that are common for all Npu subgraphs
128 scratch_tens = None
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200129 scratch_fast_tens = None
Tim Hall79d07d22020-04-27 18:20:16 +0100130 flash_tens = None
131
132 # Calculate live ranges for all constant Npu tensors, in permanent storage
133 for sg in nng.subgraphs:
134 if sg.placement == PassPlacement.Npu:
135 lr_graph_flash = live_range.extract_live_ranges_from_cascaded_passes(
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200136 sg,
137 permanent_storage,
138 MemType.Permanent_NPU,
139 ignore_subgraph_input_output_tensors=True,
140 lr_graph=lr_graph_flash,
Tim Hall79d07d22020-04-27 18:20:16 +0100141 )
142
Tim Hall25f605c2020-05-18 18:04:26 +0100143 if len(nng.subgraphs) > 1:
144 # Allocate all Npu constant tensors to the first Npu subgraph since it is
145 # processed first during serialization into tensors
146 first_npu_sg = nng.subgraphs[1]
147 assert first_npu_sg.placement == PassPlacement.Npu
Tim Hall25f605c2020-05-18 18:04:26 +0100148 tensor_allocation.allocate_tensors(
149 nng,
150 first_npu_sg,
151 arch,
152 permanent_storage,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200153 set((MemType.Permanent_NPU,)),
Tim Hall25f605c2020-05-18 18:04:26 +0100154 scheduler_options.use_ifm_ofm_overlap,
155 TensorAllocator.LinearAlloc,
156 options.verbose_allocation,
157 options.show_minimum_possible_allocation,
158 lr_graph_flash,
159 )
Tim Hall79d07d22020-04-27 18:20:16 +0100160
161 # Allocate all non-constant tensors to the root, i.e. Cpu, subgraph. This step
162 # will start at the root subgraph's input and traverse from top to bottom. When
163 # it comes across an Npu-op it will extract live ranges for it's corresponding
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200164 # Npu subgraph and add them to the root's live range graph.
165 # The non-constant tensors are stored either in arch.feature_map_storage_mem_area or
166 # arch.fast_storage_mem_area.
167 # When these memory areas are the same, all non-constant tensors are allocated together.
168 # Otherwise they are allocated separately.
169
Tim Hall79d07d22020-04-27 18:20:16 +0100170 root_sg = nng.get_root_subgraph()
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200171
172 alloc_list = []
173 if arch.feature_map_storage_mem_area == arch.fast_storage_mem_area:
174 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))
175 alloc_list.append(mem_alloc_scratch)
176 else:
177 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch,)))
178 mem_alloc_scratch_fast = (arch.fast_storage_mem_area, set((MemType.Scratch_fast,)))
179 alloc_list.append(mem_alloc_scratch)
180 alloc_list.append(mem_alloc_scratch_fast)
181
182 for alloc in alloc_list:
183 tensor_allocation.allocate_tensors(
184 nng,
185 root_sg,
186 arch,
187 alloc[0],
188 alloc[1],
189 scheduler_options.use_ifm_ofm_overlap,
190 options.tensor_allocator,
191 options.verbose_allocation,
192 options.show_minimum_possible_allocation,
193 )
Tim Hall79d07d22020-04-27 18:20:16 +0100194
195 # Generate command streams and serialise Npu-ops into tensors
196 for sg in nng.subgraphs:
197 high_level_command_stream_generator.generate_high_level_command_stream(
198 nng, sg, arch, options.verbose_high_level_command_stream
199 )
200 register_command_stream_generator.generate_register_command_stream(
201 nng, sg, arch, options.verbose_register_command_stream
202 )
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200203 scratch_tens, scratch_fast_tens, flash_tens = npu_serialisation.serialise_npu_subgraph_into_tensors(
204 nng, sg, arch, scratch_tens, scratch_fast_tens, flash_tens
Tim Hall79d07d22020-04-27 18:20:16 +0100205 )
206
207 npu_serialisation.rewrite_npu_call_ops(nng, root_sg, arch)
208
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200209 if root_sg is not None and (arch.feature_map_storage_mem_area != arch.fast_storage_mem_area):
210 if root_sg.memory_used_per_type.get(MemType.Scratch_fast, 0) > arch.sram_size:
211 print("Warning: Sram limit has been exceeded, by the scratch fast tensor")
212
Tim Hall79d07d22020-04-27 18:20:16 +0100213 # Allocate all Cpu constant tensors, this is done last because the Npu-ops
214 # have to be serialized into flash and scratch tensors first
215 tensor_allocation.allocate_tensors(
216 nng,
217 root_sg,
218 arch,
219 permanent_storage,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200220 set((MemType.Permanent_CPU,)),
Tim Hall79d07d22020-04-27 18:20:16 +0100221 scheduler_options.use_ifm_ofm_overlap,
Louis Verhaard3c07c972020-05-07 08:12:58 +0200222 TensorAllocator.LinearAlloc,
Tim Hall79d07d22020-04-27 18:20:16 +0100223 options.verbose_allocation,
224 options.show_minimum_possible_allocation,
225 )
226
227 npu_performance.calc_performance_for_network(nng, arch)