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 | # 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 19 | from .high_level_command_stream_generator import calc_allowed_ofm_ifm_overlap_for_cascaded_pass |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 20 | from .nn_graph import PassPlacement |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 21 | from .tensor import MemType |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 22 | from .tensor import Tensor |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class LiveRange: |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 26 | def __init__(self, tens, alignment): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 27 | 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 Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 32 | self.alignment = alignment |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 33 | |
| 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 Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame^] | 87 | # Set address of all tensors in LiveRange |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 88 | for tens in self.tensors: |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame^] | 89 | tens.address = address |
| 90 | |
| 91 | return address |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 92 | |
| 93 | def get_alignment(self): |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 94 | return self.alignment |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 95 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 96 | def set_alignment(self, alignment): |
| 97 | self.alignment = max(self.alignment, alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 106 | 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 | |
| 112 | class 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 Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 120 | def get_or_create_range(self, tens, alignment=Tensor.AllocationQuantum): |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame^] | 121 | # 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 Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 124 | rng.set_alignment(alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 125 | return rng |
| 126 | |
| 127 | # No live range found for the tensor, create a new one |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 128 | rng = LiveRange(tens, alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 129 | 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 | |
| 140 | def 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 | |
| 203 | def extract_live_ranges_from_cascaded_passes( |
| 204 | sg, |
| 205 | target_mem_area, |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 206 | target_mem_type_set, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 207 | 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 Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 211 | allocation_alignment=Tensor.AllocationQuantum, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 212 | ): |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 213 | if lr_graph is None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 214 | 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 224 | 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 226 | 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 235 | 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 241 | 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 246 | # Merge only memory operations in the NPU subgraphs |
| 247 | if sg.placement == PassPlacement.Npu: |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 248 | merge_memory_op_ranges(sg, lr_graph, tensor_should_be_ignored, target_mem_area, target_mem_type_set) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 249 | |
| 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 258 | if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 259 | continue |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 260 | rng = lr_graph.get_or_create_range(tens, allocation_alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 261 | rng.mark_usage(time_for_pass) |
| 262 | |
| 263 | cps_primary_op = cps.passes[0].primary_op |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 264 | |
| 265 | if cps_primary_op and cps_primary_op.type == "NpuOp" and MemType.Permanent_CPU not in target_mem_type_set: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 266 | # 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 Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 268 | # Use default allocation alignment of 16 for Npu tensors |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 269 | npu_sg = cps_primary_op.attrs["subgraph"] |
| 270 | lr_graph = extract_live_ranges_from_cascaded_passes( |
| 271 | npu_sg, |
| 272 | target_mem_area, |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 273 | target_mem_type_set, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 274 | 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 284 | if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 285 | continue |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 286 | rng = lr_graph.get_or_create_range(tens, allocation_alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 287 | rng.mark_usage(time_for_pass) |
| 288 | |
| 289 | for tens in cps.outputs: |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 290 | if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 291 | continue |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 292 | rng = lr_graph.get_or_create_range(tens, allocation_alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 293 | 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 305 | 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 307 | ): |
| 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 Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 320 | if tensor_should_be_ignored(tens, target_mem_area, target_mem_type_set): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 321 | continue |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 322 | rng = lr_graph.get_or_create_range(tens, allocation_alignment) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 323 | 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 |