blob: 9a8ee580c0a4f007a35015293cdbf99b98a2a22c [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# Build a live range graph for tensors in one or more subgraphs. Used for tensor allocation as well as in the scheduler.
18# Can work with either a pass packed subgraph or a scheduled subgraph.
Tim Hall79d07d22020-04-27 18:20:16 +010019from .high_level_command_stream_generator import calc_allowed_ofm_ifm_overlap_for_cascaded_pass
Diego Russoe8a10452020-04-21 17:39:10 +010020from .nn_graph import PassPlacement
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020021from .tensor import MemType
Diego Russoe8a10452020-04-21 17:39:10 +010022from .tensor import Tensor
Tim Hall79d07d22020-04-27 18:20:16 +010023
24
25class LiveRange:
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020026 def __init__(self, tens, alignment):
Tim Hall79d07d22020-04-27 18:20:16 +010027 self.tensors = [] # Tensors that are assigned to the same LiveRange will be allocated to the same address
28 self.start_time = 99999999999
29 self.end_time = -1
30 self.size = 0
31 self.name = ""
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020032 self.alignment = alignment
Tim Hall79d07d22020-04-27 18:20:16 +010033
34 if tens:
35 self.add_tensor(tens)
36
37 def __str__(self):
38 return "<live_range.LiveRange: '%s' start_time=%s, end_time=%s>" % (self.name, self.start_time, self.end_time)
39
40 __repr__ = __str__
41
42 def add_tensor(self, tens):
43 if self.size == 0:
44 self.size = tens.storage_size()
45 self.name = tens.name # LiveRange will be named after the first tensor added
46 else:
47 assert (
48 self.size >= tens.storage_size()
49 ), "Tensors assigned to the same LiveRange need to fit the size of the LiveRange."
50
51 self.tensors.append(tens)
52
53 def mark_usage(self, op_time):
54 if op_time == -1:
55 return
56 op_time_start = op_time
57 op_time_end = op_time + 1
58
59 self.start_time = min(self.start_time, op_time_start)
60 self.end_time = max(self.end_time, op_time_end)
61
62 def overlaps_ranges(self, other):
63 return max(self.start_time, other.start_time) < min(self.end_time, other.end_time)
64
65 def overlaps_address(self, other):
66 # Returns the first pair of tensors in this LiveRange and 'other' which have
67 # overlapping addresses
68 for tens in self.tensors:
69 for other_tens in other.tensors:
70 if max(tens.address, other_tens.address) < min(
71 tens.address + self.size, other_tens.address + other.size
72 ):
73 return True, tens, other_tens
74
75 return False, None, None
76
77 def __lt__(self, other):
78 if self.start_time != other.start_time:
79 return self.start_time < other.start_time
80 if self.end_time != other.end_time:
81 return self.end_time < other.end_time
82 if self.size != other.size:
83 return self.size < other.size
84 return self.name < other.name
85
86 def set_address(self, address):
Jacob Bohlin1a666972020-09-11 10:04:15 +020087 # Set address of all tensors in LiveRange
Tim Hall79d07d22020-04-27 18:20:16 +010088 for tens in self.tensors:
Jacob Bohlin1a666972020-09-11 10:04:15 +020089 tens.address = address
90
91 return address
Tim Hall79d07d22020-04-27 18:20:16 +010092
93 def get_alignment(self):
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020094 return self.alignment
Tim Hall79d07d22020-04-27 18:20:16 +010095
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020096 def set_alignment(self, alignment):
97 self.alignment = max(self.alignment, alignment)
Tim Hall79d07d22020-04-27 18:20:16 +010098
99
100def merge_memory_op_ranges(sg, lr_graph, tensor_should_be_ignored, target_mem_area):
101 for ps in sg.passes:
102 if ps.placement == PassPlacement.MemoryOnly:
103 # For memory only passes, e.g. Reshape. Add input and output tensor to the same LiveRange
104 input_tensor = ps.inputs[0]
105 output_tensor = ps.outputs[0]
Tim Hall79d07d22020-04-27 18:20:16 +0100106 if not tensor_should_be_ignored(input_tensor, target_mem_area) and not tensor_should_be_ignored(
107 output_tensor, target_mem_area
108 ):
109 lr_graph.fuse_ranges(input_tensor, output_tensor)
110
111
112class LiveRangeGraph:
113 def __init__(self):
114 self.ranges = {} # tens -> range
115 self.allowed_overlaps = {} # (tens,tens) -> overlap_int
116 self.ignore_tensors = set()
117 self.processed_subgraphs = set()
118 self.current_time = 0
119
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200120 def get_or_create_range(self, tens, alignment=Tensor.AllocationQuantum):
Jacob Bohlin1a666972020-09-11 10:04:15 +0200121 # Return the live range of the tensor (or any of its clones)
122 for existing_tensor, rng in self.ranges.items():
123 if tens.equivalent(existing_tensor):
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200124 rng.set_alignment(alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100125 return rng
126
127 # No live range found for the tensor, create a new one
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200128 rng = LiveRange(tens, alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100129 self.ranges[tens] = rng
130 return rng
131
132 def fuse_ranges(self, in_tens, out_tens):
133 live_range = self.get_or_create_range(in_tens)
134 assert out_tens not in self.ranges, out_tens
135 live_range.add_tensor(out_tens)
136 self.ranges[out_tens] = live_range
137 return live_range
138
139
140def extract_live_ranges_from_passes(
141 sg,
142 target_mem_area,
143 mark_output_tensors_overlapping_with_input_tensors=False,
144 ignore_subgraph_input_output_tensors=False,
145):
146 lr_graph = LiveRangeGraph()
147
148 if ignore_subgraph_input_output_tensors:
149 lr_graph.ignore_tensors.update(sg.input_tensors)
150 lr_graph.ignore_tensors.update(sg.output_tensors)
151
152 def tensor_should_be_ignored(tens, target_mem_area):
153 if tens.mem_area != target_mem_area:
154 return True
155 if tens in lr_graph.ignore_tensors:
156 return True
157 if tens.name.endswith("reshape_shape_npu"):
158 # Reshape tensor, no need to allocate
159 lr_graph.ignore_tensors.add(tens)
160 return True
161 return False
162
163 # Merge only memory operations in the NPU subgraphs
164 if sg.placement == PassPlacement.Npu:
165 merge_memory_op_ranges(sg, lr_graph, tensor_should_be_ignored, target_mem_area)
166
167 for idx, ps in enumerate(sg.passes):
168 ps.time = 2 * idx
169
170 time_for_pass = ps.time
171
172 for tens in ps.inputs:
173 if tensor_should_be_ignored(tens, target_mem_area):
174 continue
175 rng = lr_graph.get_or_create_range(tens)
176 rng.mark_usage(time_for_pass)
177
178 for tens in ps.intermediates:
179 if tensor_should_be_ignored(tens, target_mem_area):
180 continue
181 rng = lr_graph.get_or_create_range(tens)
182 rng.mark_usage(time_for_pass)
183
184 for tens in ps.outputs:
185 if tensor_should_be_ignored(tens, target_mem_area):
186 continue
187 rng = lr_graph.get_or_create_range(tens)
188 output_time = time_for_pass
189 if not mark_output_tensors_overlapping_with_input_tensors and ps.is_element_wise:
190 output_time += 1
191 rng.mark_usage(output_time)
192
193 end_time = len(sg.passes) * 2
194 for tens in sg.output_tensors:
195 if tensor_should_be_ignored(tens, target_mem_area):
196 continue
197 rng = lr_graph.get_or_create_range(tens)
198 rng.mark_usage(end_time)
199
200 return lr_graph
201
202
203def extract_live_ranges_from_cascaded_passes(
204 sg,
205 target_mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200206 target_mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100207 mark_output_tensors_overlapping_with_input_tensors=False,
208 use_ifm_ofm_overlap=True,
209 ignore_subgraph_input_output_tensors=False,
210 lr_graph=None,
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200211 allocation_alignment=Tensor.AllocationQuantum,
Tim Hall79d07d22020-04-27 18:20:16 +0100212):
Diego Russoea6111a2020-04-14 18:41:58 +0100213 if lr_graph is None:
Tim Hall79d07d22020-04-27 18:20:16 +0100214 lr_graph = LiveRangeGraph()
215
216 if sg in lr_graph.processed_subgraphs:
217 # if subgraph has been processed already, return the lr_graph as is
218 return lr_graph
219
220 if ignore_subgraph_input_output_tensors:
221 lr_graph.ignore_tensors.update(sg.input_tensors)
222 lr_graph.ignore_tensors.update(sg.output_tensors)
223
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200224 def tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set):
225 if tens.mem_area != target_mem_area or tens.mem_type not in target_mem_type_set:
Tim Hall79d07d22020-04-27 18:20:16 +0100226 return True
227 if tens in lr_graph.ignore_tensors:
228 return True
229 if tens.name.endswith("reshape_shape_npu"):
230 # Reshape tensor, no need to allocate
231 lr_graph.ignore_tensors.add(tens)
232 return True
233 return False
234
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200235 def merge_memory_op_ranges(sg, lr_graph, tensor_should_be_ignored, target_mem_area, target_mem_type_set):
236 for ps in sg.passes:
237 if ps.placement == PassPlacement.MemoryOnly:
238 # For memory only passes, e.g. Reshape. Add input and output tensor to the same LiveRange
239 input_tensor = ps.inputs[0]
240 output_tensor = ps.outputs[0]
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200241 if not tensor_should_be_ignored(input_tensor, target_mem_area, target_mem_type_set) and not (
242 tensor_should_be_ignored(output_tensor, target_mem_area, target_mem_type_set)
243 ):
244 lr_graph.fuse_ranges(input_tensor, output_tensor)
245
Tim Hall79d07d22020-04-27 18:20:16 +0100246 # Merge only memory operations in the NPU subgraphs
247 if sg.placement == PassPlacement.Npu:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200248 merge_memory_op_ranges(sg, lr_graph, tensor_should_be_ignored, target_mem_area, target_mem_type_set)
Tim Hall79d07d22020-04-27 18:20:16 +0100249
250 for cps in sg.cascaded_passes:
251 cps.time = lr_graph.current_time
252
253 time_for_pass = cps.time
254
255 is_element_wise = cps.is_element_wise
256
257 for tens in cps.inputs:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200258 if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set):
Tim Hall79d07d22020-04-27 18:20:16 +0100259 continue
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200260 rng = lr_graph.get_or_create_range(tens, allocation_alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100261 rng.mark_usage(time_for_pass)
262
263 cps_primary_op = cps.passes[0].primary_op
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200264
265 if cps_primary_op and cps_primary_op.type == "NpuOp" and MemType.Permanent_CPU not in target_mem_type_set:
Tim Hall79d07d22020-04-27 18:20:16 +0100266 # If the primary-op is an NpuOp that means this is where an Npu subgraph
267 # is called. Go into said subgraph and extract live ranges before continuing.
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200268 # Use default allocation alignment of 16 for Npu tensors
Tim Hall79d07d22020-04-27 18:20:16 +0100269 npu_sg = cps_primary_op.attrs["subgraph"]
270 lr_graph = extract_live_ranges_from_cascaded_passes(
271 npu_sg,
272 target_mem_area,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200273 target_mem_type_set,
Tim Hall79d07d22020-04-27 18:20:16 +0100274 mark_output_tensors_overlapping_with_input_tensors,
275 use_ifm_ofm_overlap,
276 False,
277 lr_graph,
278 )
279 # Set the new time after handling the Npu subgraph
280 time_for_pass = lr_graph.current_time
281 cps.time = time_for_pass
282
283 for tens in cps.intermediates:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200284 if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set):
Tim Hall79d07d22020-04-27 18:20:16 +0100285 continue
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200286 rng = lr_graph.get_or_create_range(tens, allocation_alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100287 rng.mark_usage(time_for_pass)
288
289 for tens in cps.outputs:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200290 if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set):
Tim Hall79d07d22020-04-27 18:20:16 +0100291 continue
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200292 rng = lr_graph.get_or_create_range(tens, allocation_alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100293 output_time = time_for_pass
294 if not mark_output_tensors_overlapping_with_input_tensors and is_element_wise:
295 output_time += 1
296 rng.mark_usage(output_time)
297
298 if use_ifm_ofm_overlap:
299 # fill allowed overlap for ifm and ofm tensor
300 ifm_tensor = cps.passes[0].ifm_tensor
301 ofm_tensor = cps.passes[-1].ofm_tensor
302 if (
303 ifm_tensor is not None
304 and ofm_tensor is not None
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200305 and not tensor_should_be_ignored(ifm_tensor, target_mem_area, target_mem_type_set)
306 and not tensor_should_be_ignored(ofm_tensor, target_mem_area, target_mem_type_set)
Tim Hall79d07d22020-04-27 18:20:16 +0100307 ):
308 lr_graph.allowed_overlaps[(ifm_tensor, ofm_tensor)] = calc_allowed_ofm_ifm_overlap_for_cascaded_pass(
309 cps
310 )
311
312 lr_graph.current_time += 2
313
314 end_time = 0
315 for rng in lr_graph.ranges.values():
316 # Find the maximum end time of all live-ranges in the graph
317 end_time = max(end_time, rng.end_time)
318
319 for tens in sg.output_tensors:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200320 if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set):
Tim Hall79d07d22020-04-27 18:20:16 +0100321 continue
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200322 rng = lr_graph.get_or_create_range(tens, allocation_alignment)
Tim Hall79d07d22020-04-27 18:20:16 +0100323 rng.mark_usage(end_time)
324
325 # Add subgraph to set of processed subgraphs
326 lr_graph.processed_subgraphs.add(sg)
327 return lr_graph