erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 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 | # Neural network graph classes and enums. |
| 18 | # Pass - A packed pass containing one or more Operations. |
| 19 | # CascadedPass - A scheduled pass containing one or more Passes, as well as a scheduling strategy and block |
| 20 | # configurations. |
| 21 | # Subgraph - Holds a neural network subgraph, pointing at Tensors, Operations, Passes, and CascadedPasses. |
| 22 | # Graph - A full neural network graph with one or more Subgraphs. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | import enum |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 24 | from typing import List |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 25 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 26 | from .operation import Op |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 27 | from .shape4d import Shape4D |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 28 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 29 | |
| 30 | class PassPlacement(enum.Enum): |
| 31 | Unknown = 0 |
| 32 | Cpu = 1 |
| 33 | Npu = 2 |
| 34 | MemoryOnly = 3 |
| 35 | StartupInit = 4 |
| 36 | |
| 37 | |
| 38 | class TensorAllocator(enum.Enum): |
| 39 | LinearAlloc = 1 |
| 40 | Greedy = 2 |
Louis Verhaard | d700252 | 2021-01-20 17:23:54 +0100 | [diff] [blame] | 41 | HillClimb = 3 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 42 | |
| 43 | def __str__(self): |
| 44 | return self.name |
| 45 | |
| 46 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 47 | class NetworkType(enum.Enum): |
| 48 | TFLite = 1 |
| 49 | TOSA = 2 |
| 50 | |
| 51 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 52 | class Pass: |
| 53 | def __init__(self, name, placement, is_element_wise, npu_block_type): |
| 54 | self.inputs = [] |
| 55 | self.intermediates = [] |
| 56 | self.outputs = [] |
| 57 | self.ops = [] |
| 58 | self.primary_op = None |
| 59 | self.ifm_tensor = None |
| 60 | self.ifm2_tensor = None |
| 61 | self.ofm_tensor = None |
| 62 | self.weight_tensor = None |
| 63 | self.scale_tensor = None |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 64 | self.lut_tensor = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 65 | self.name = name |
| 66 | self.cascade = None |
| 67 | self.placement = placement |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 68 | self.ifm_shapes: List[Shape4D] = [] |
| 69 | self.ofm_shapes: List[Shape4D] = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 70 | |
| 71 | # TODO: rename is_element_wise because it is not the same as an ElementWise operator. It is used by the tensor |
| 72 | # allocation and requires that the OFM and IFM has the exact same address. Essentially complete overlap. |
| 73 | self.is_element_wise = is_element_wise |
| 74 | self.npu_block_type = npu_block_type |
| 75 | self.block_config = None # will be filled in by scheduler |
| 76 | self.shared_buffer = None # will be filled in by scheduler |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 77 | self.scheduling_info = None # will be filled in by scheduler |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 78 | |
| 79 | self.predecessors = [] |
| 80 | self.successors = [] |
| 81 | |
| 82 | def __str__(self): |
| 83 | return "<nng.Pass '%s', %s, ops=%s>" % (self.name, self.placement, [op.type for op in self.ops]) |
| 84 | |
| 85 | __repr__ = __str__ |
| 86 | |
| 87 | def get_primary_op_ifm_weights(self): |
| 88 | if not self.primary_op: |
| 89 | return None, None |
| 90 | return self.primary_op.get_ifm_ifm2_weights_ofm()[::2] |
| 91 | |
| 92 | def get_primary_op_ifm_ifm2_weights_ofm(self): |
| 93 | if not self.primary_op: |
| 94 | return None, None, None, None |
| 95 | return self.primary_op.get_ifm_ifm2_weights_ofm() |
| 96 | |
| 97 | def get_primary_op_ifm_weights_biases_ofm(self): |
| 98 | if not self.primary_op: |
| 99 | return None, None, None, None |
| 100 | return self.primary_op.get_ifm_weights_biases_ofm() |
| 101 | |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 102 | def get_primary_op_lut(self): |
| 103 | if not self.primary_op: |
| 104 | return None |
| 105 | return self.primary_op.activation_lut |
| 106 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 107 | |
| 108 | class SchedulingStrategy(enum.Enum): |
| 109 | Unknown = -1 |
| 110 | IfmStream = 0 |
| 111 | WeightStream = 1 |
| 112 | |
| 113 | |
| 114 | class SchedulerRewrite(enum.Enum): |
| 115 | Nop = 0 |
| 116 | ChangeTensorSubPurpose = 1 |
| 117 | |
| 118 | |
| 119 | class CascadedPass: |
| 120 | def __init__(self, name, strat, inputs, intermediates, outputs, passes, placement, is_element_wise): |
| 121 | self.name = name |
| 122 | self.strategy = strat |
| 123 | self.inputs = inputs |
| 124 | self.intermediates = intermediates |
| 125 | self.outputs = outputs |
| 126 | self.passes = passes |
| 127 | self.placement = placement |
| 128 | self.is_element_wise = is_element_wise |
| 129 | |
| 130 | self.predecessors = [] |
| 131 | self.successors = [] |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 132 | self.sram_used = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 133 | |
| 134 | def __str__(self): |
| 135 | return "<nng.CascadedPass strategy=%s x %s '%s', passes=%s, block_configs=%s>" % ( |
| 136 | self.strategy, |
| 137 | len(self.passes), |
| 138 | self.name, |
| 139 | [ps.name for ps in self.passes], |
| 140 | [ps.block_config for ps in self.passes], |
| 141 | ) |
| 142 | |
| 143 | __repr__ = __str__ |
| 144 | |
| 145 | |
| 146 | class Subgraph: |
| 147 | def __init__(self, name="<unnamed>", placement=PassPlacement.Cpu): |
| 148 | self.output_tensors = [] |
| 149 | self.input_tensors = [] |
| 150 | self.original_inputs = [] # Preserve the original input order |
| 151 | self.passes = [] |
| 152 | self.cascaded_passes = [] |
| 153 | self.name = name |
| 154 | self.high_level_command_stream = [] |
| 155 | self.placement = placement |
| 156 | self.command_stream_tensor = None |
| 157 | self.flash_tensor = None |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 158 | # Scratch information locally used in the scheduler |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 159 | self.schedule = None |
| 160 | self.sched_ops = [] |
| 161 | |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 162 | self.generated_stream_id = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 163 | |
| 164 | self.memory_used = {} |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 165 | self.memory_used_per_type = {} |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 166 | |
| 167 | def __str__(self): |
| 168 | return "<nng.Subgraph '%s', n_passes=%d, n_cascaded_passes=%d>" % ( |
| 169 | self.name, |
| 170 | len(self.passes), |
| 171 | len(self.cascaded_passes), |
| 172 | ) |
| 173 | |
| 174 | __repr__ = __str__ |
| 175 | |
| 176 | def update_consumers(self): |
| 177 | visit_op_set = set() |
| 178 | visit_tensor_set = set() |
| 179 | self.input_tensors = [] |
| 180 | |
| 181 | print_visit = False |
| 182 | |
| 183 | def visit_op(op): |
| 184 | if op in visit_op_set: |
| 185 | return |
| 186 | |
| 187 | visit_op_set.add(op) |
| 188 | for inp in op.inputs: |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 189 | if not inp: |
| 190 | continue |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 191 | if print_visit: |
| 192 | print(inp, "adding consumer", op) |
| 193 | visit_tensor(inp) |
| 194 | inp.consumer_list.append(op) |
| 195 | |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 196 | if op.type in (Op.Placeholder, Op.SubgraphInput): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 197 | assert len(op.outputs) == 1 |
| 198 | self.input_tensors.append(op.outputs[0]) |
| 199 | |
| 200 | for out in op.outputs: |
| 201 | if out not in visit_tensor_set: |
| 202 | out.consumer_list = [] # reset unvisited output, just in case |
| 203 | |
| 204 | def visit_tensor(tens): |
| 205 | if tens in visit_tensor_set: |
| 206 | return |
| 207 | visit_tensor_set.add(tens) |
| 208 | tens.consumer_list = [] |
| 209 | for op in tens.ops: |
| 210 | visit_op(op) |
| 211 | |
| 212 | for ps in self.passes: |
| 213 | for tens in ps.outputs + ps.inputs: |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 214 | if not tens: |
| 215 | continue |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 216 | tens.consumer_list = [] # reset unvisited tensors to start with |
| 217 | |
| 218 | for tens in self.output_tensors: |
| 219 | visit_tensor(tens) |
| 220 | tens.consumer_list.append(None) # special op to indicate that the graph consumes the result |
| 221 | |
| 222 | print_visit = True |
| 223 | for ps in self.passes: |
| 224 | for op in ps.ops: |
| 225 | visit_op(op) |
| 226 | for tens in ps.inputs: |
| 227 | visit_tensor(tens) |
| 228 | |
| 229 | def build_pass_links(self): |
| 230 | for idx, ps in enumerate(self.passes): |
| 231 | ps.time = 2 * idx |
| 232 | ps.predecessors = [] |
| 233 | ps.successors = [] |
| 234 | |
| 235 | for ps in self.passes: |
| 236 | for tens in ps.inputs: |
| 237 | for op in tens.ops: |
| 238 | pred_pass = op.scheduled_pass |
| 239 | assert pred_pass.time < ps.time |
| 240 | if ps not in pred_pass.successors: |
| 241 | pred_pass.successors.append(ps) |
| 242 | |
| 243 | if pred_pass not in ps.predecessors: |
| 244 | ps.predecessors.append(pred_pass) |
| 245 | |
| 246 | assert tens in pred_pass.outputs |
| 247 | |
| 248 | def build_pass_dag_predecessors(self): |
| 249 | for ps in self.passes: |
| 250 | ps.dag_predecessors = [] |
| 251 | |
| 252 | class State(enum.Enum): |
| 253 | NotVisited = 0 |
| 254 | BeingVisited = 1 |
| 255 | Visited = 2 |
| 256 | |
| 257 | pass_visit_dict = {} |
| 258 | |
| 259 | def visit_pass(ps): |
| 260 | state = pass_visit_dict.get(ps, State.NotVisited) |
| 261 | if state == State.Visited: |
| 262 | return True |
| 263 | elif state == State.BeingVisited: |
| 264 | return False # this is a loop, need to remove this link |
| 265 | elif state == State.NotVisited: |
| 266 | pass_visit_dict[ps] = State.BeingVisited |
| 267 | |
| 268 | ps.dag_predecessors = [] |
| 269 | for pred in ps.predecessors: |
| 270 | if visit_pass(pred): |
| 271 | ps.dag_predecessors.append(pred) |
| 272 | |
| 273 | pass_visit_dict[ps] = State.Visited |
| 274 | return True |
| 275 | |
| 276 | for ps in self.passes: |
| 277 | if not ps.successors: |
| 278 | visit_pass(ps) |
| 279 | |
| 280 | def build_cascaded_pass_links(self): |
| 281 | for cps in self.cascaded_passes: |
| 282 | cps.predecessors = [] |
| 283 | cps.successors = [] |
| 284 | |
| 285 | for cps in self.cascaded_passes: |
| 286 | for tens in cps.inputs: |
| 287 | for op in tens.ops: |
| 288 | pred_cpass = op.scheduled_pass.cascade |
| 289 | if cps not in pred_cpass.successors: |
| 290 | pred_cpass.successors.append(cps) |
| 291 | |
| 292 | if pred_cpass not in cps.predecessors: |
| 293 | cps.predecessors.append(pred_cpass) |
| 294 | |
| 295 | assert tens in pred_cpass.outputs |
| 296 | |
| 297 | def refresh_after_modification(self): |
| 298 | self.update_consumers() |
| 299 | |
| 300 | def prune_startup_init_pass(self): |
| 301 | assert len(self.passes) >= 1 |
| 302 | ps = self.passes[0] |
| 303 | assert ps.placement == PassPlacement.StartupInit |
| 304 | |
| 305 | ps.outputs = [out_tens for out_tens in ps.outputs if len(out_tens.consumers()) > 0] |
| 306 | ps.ops = [op for op in ps.ops if op.outputs[0] in ps.outputs] |
| 307 | |
| 308 | def get_all_ops(self): |
| 309 | all_ops = [] |
| 310 | visit_op_set = set() |
| 311 | visit_tensor_set = set() |
| 312 | |
| 313 | def visit_op(op): |
| 314 | if op in visit_op_set: |
| 315 | return |
| 316 | visit_op_set.add(op) |
| 317 | for inp in op.inputs: |
| 318 | visit_tensor(inp) |
| 319 | |
| 320 | all_ops.append(op) |
| 321 | |
| 322 | def visit_tensor(tens): |
Andreas Nevalainen | e1cc3de | 2020-09-08 15:31:02 +0200 | [diff] [blame] | 323 | if tens is None or tens in visit_tensor_set: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 324 | return |
| 325 | visit_tensor_set.add(tens) |
| 326 | for op in tens.ops: |
| 327 | visit_op(op) |
| 328 | |
| 329 | for tens in self.output_tensors: |
| 330 | visit_tensor(tens) |
| 331 | |
| 332 | return all_ops |
| 333 | |
| 334 | def print_operators(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 335 | print("print_operators()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 336 | all_ops = self.get_all_ops() |
| 337 | unique_ops = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 338 | for op in all_ops: |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 339 | if op.type in (Op.Const, Op.Identity, Op.Placeholder): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 340 | continue |
| 341 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 342 | attrs = op.attrs.copy() |
| 343 | if op.type in (Op.Conv2D, Op.Conv2DBias, Op.DepthwiseConv2DBias): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 344 | kshape = op.inputs[1].shape |
| 345 | attrs["kshape"] = [kshape[0], kshape[1]] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 346 | attrs["type"] = op.type.name |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 347 | attrs.pop("use_cudnn_on_gpu", None) |
Fredrik Svedberg | 1634305 | 2021-04-16 14:36:22 +0200 | [diff] [blame] | 348 | custom_options = attrs.pop("custom_options", None) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 349 | if attrs not in unique_ops: |
| 350 | unique_ops.append(attrs) |
| 351 | # print attributes in human readable format |
| 352 | a = attrs.copy() |
Fredrik Svedberg | 1634305 | 2021-04-16 14:36:22 +0200 | [diff] [blame] | 353 | if custom_options is not None: |
| 354 | a["custom_options"] = custom_options |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 355 | s = a.pop("type") |
| 356 | data_format = a.pop("data_format", None) |
| 357 | if data_format and data_format != b"NHWC": |
| 358 | s += " " + str(data_format) |
| 359 | t = a.pop("T", None) |
| 360 | if t: |
| 361 | s += " " + str(t)[9:-2] |
| 362 | srct = a.pop("SrcT", None) |
| 363 | if srct: |
| 364 | s += " " + str(srct)[9:-2] |
| 365 | dstt = a.pop("DstT", None) |
| 366 | if dstt: |
| 367 | s += "->" + str(dstt)[9:-2] |
| 368 | print(s + " " + str(a)) |
| 369 | |
Fredrik Svedberg | c875aa6 | 2021-05-06 09:53:31 +0200 | [diff] [blame] | 370 | def print_graph(self, label=None): |
| 371 | if label: |
| 372 | print(f"\n[ {label} ]") |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 373 | print("print_graph()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 374 | all_ops = self.get_all_ops() |
| 375 | for idx, op in enumerate(all_ops): |
| 376 | print(idx, op.type, op.name) |
| 377 | |
| 378 | def print_graph_with_tensors(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 379 | print("print_graph_with_tensors()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 380 | all_ops = self.get_all_ops() |
| 381 | for idx, op in enumerate(all_ops): |
| 382 | print(idx, op.type, op.name) |
| 383 | for idx, tens in enumerate(op.inputs): |
Fredrik Svedberg | b3d941e | 2021-10-13 14:06:03 +0200 | [diff] [blame^] | 384 | if tens: |
| 385 | print( |
| 386 | f" Input {idx:02d}" |
| 387 | f" {tens.purpose.name:>20} {tens.mem_area.name:>20} {tens.mem_type.name:>20} {tens}" |
| 388 | ) |
| 389 | else: |
| 390 | print(f" Input {idx:02d} {'-':>20} {'-':>20} {'-':>20} {tens}") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 391 | for idx, tens in enumerate(op.outputs): |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 392 | print( |
Fredrik Svedberg | b3d941e | 2021-10-13 14:06:03 +0200 | [diff] [blame^] | 393 | f" Output {idx:02d}" |
| 394 | f" {tens.purpose.name:>20} {tens.mem_area.name:>20} {tens.mem_type.name:>20} {tens}" |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 395 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 396 | print() |
| 397 | |
| 398 | def print_graph_with_tensor_quantization(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 399 | print("print_graph_with_tensor_quantization()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 400 | all_ops = self.get_all_ops() |
| 401 | for idx, op in enumerate(all_ops): |
| 402 | print(idx, op.type, op.name) |
| 403 | for idx, tens in enumerate(op.inputs): |
Fredrik Svedberg | b3d941e | 2021-10-13 14:06:03 +0200 | [diff] [blame^] | 404 | if tens: |
| 405 | q = tens.quantization |
| 406 | if q is None: |
| 407 | print(f" Input {idx:02d} {tens.dtype!s:>10} NO QUANTIZATION INFO {tens.name}") |
| 408 | else: |
| 409 | print( |
| 410 | f" Input {idx:02d} {tens.dtype!s:>10}" |
| 411 | f" min={q.min} max={q.max} scale={q.scale_f32!s} zero_point={q.zero_point} {tens.name}" |
| 412 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 413 | else: |
Fredrik Svedberg | b3d941e | 2021-10-13 14:06:03 +0200 | [diff] [blame^] | 414 | print(f" Input {idx:02d} {'-':>10} {tens}") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 415 | for idx, tens in enumerate(op.outputs): |
| 416 | q = tens.quantization |
| 417 | if q is None: |
Fredrik Svedberg | b3d941e | 2021-10-13 14:06:03 +0200 | [diff] [blame^] | 418 | print(f" Output {idx:02d} {tens.dtype!s:>10} NO QUANTIZATION INFO {tens.name}") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 419 | else: |
| 420 | print( |
Fredrik Svedberg | b3d941e | 2021-10-13 14:06:03 +0200 | [diff] [blame^] | 421 | f" Output {idx:02d} {tens.dtype!s:>10}" |
| 422 | f" min={q.min} max={q.max} scale={q.scale_f32!s} zero_point={q.zero_point} {tens.name}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 423 | ) |
| 424 | print() |
| 425 | |
| 426 | def print_passes(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 427 | print("print_passes()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 428 | for idx, ps in enumerate(self.passes): |
| 429 | print("%03d %s" % (idx * 2, ps)) |
| 430 | |
| 431 | def print_passes_with_tensors(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 432 | print("print_passes_with_tensors()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 433 | for idx, ps in enumerate(self.passes): |
| 434 | print("%3d %s" % (idx * 2, ps)) |
| 435 | for idx, tens in enumerate(ps.inputs): |
| 436 | print( |
| 437 | " Input %2d %-15s %-15s %-15s %s" |
| 438 | % (idx, tens.purpose.name, tens.mem_area.name, tens.format.name, tens.name) |
| 439 | ) |
| 440 | for idx, tens in enumerate(ps.intermediates): |
| 441 | print( |
| 442 | " Intermediate %2d %-15s %-15s %-15s %s" |
| 443 | % (idx, tens.purpose.name, tens.mem_area.name, tens.format.name, tens.name) |
| 444 | ) |
| 445 | for idx, tens in enumerate(ps.outputs): |
| 446 | print( |
| 447 | " Output %2d %-15s %-15s %-15s %s" |
| 448 | % (idx, tens.purpose.name, tens.mem_area.name, tens.format.name, tens.name) |
| 449 | ) |
| 450 | print() |
| 451 | |
| 452 | def print_cascaded_passes(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 453 | print("print_cascaded_passes()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 454 | for idx, ps in enumerate(self.cascaded_passes): |
| 455 | print("%3d %s SRAM used %.1f KB" % (idx * 2, ps, ps.sram_used / 1024)) |
| 456 | |
| 457 | def print_cascaded_passes_with_tensors(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 458 | print("print_cascaded_passes_with_tensors()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 459 | for idx, ps in enumerate(self.cascaded_passes): |
| 460 | print("%3d %s SRAM used %.1f KB" % (idx * 2, ps, ps.sram_used / 1024)) |
| 461 | for idx, tens in enumerate(ps.inputs): |
| 462 | print( |
| 463 | " Input %2d %-15s %-15s %-15s %s" |
| 464 | % (idx, tens.purpose.name, tens.mem_area.name, tens.format.name, tens.name) |
| 465 | ) |
| 466 | for idx, tens in enumerate(ps.intermediates): |
| 467 | print( |
| 468 | " Intermediate %2d %-15s %-15s %-15s %s" |
| 469 | % (idx, tens.purpose.name, tens.mem_area.name, tens.format.name, tens.name) |
| 470 | ) |
| 471 | for idx, tens in enumerate(ps.outputs): |
| 472 | print( |
| 473 | " Output %2d %-15s %-15s %-15s %s" |
| 474 | % (idx, tens.purpose.name, tens.mem_area.name, tens.format.name, tens.name) |
| 475 | ) |
| 476 | print() |
| 477 | |
| 478 | def print_cascaded_passes_with_tensor_sizes(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 479 | print("print_cascaded_passes_with_tensor_sizes()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 480 | for idx, ps in enumerate(self.cascaded_passes): |
| 481 | print("%3d %s SRAM used %.1f KB" % (idx * 2, ps, ps.sram_used / 1024)) |
| 482 | for idx, tens in enumerate(ps.inputs): |
| 483 | print( |
| 484 | " Input %2d %7.1f KB %-24s %-15s %-15s %-20s %s" |
| 485 | % ( |
| 486 | idx, |
| 487 | tens.storage_size() / 1024, |
| 488 | tens.storage_shape, |
| 489 | tens.mem_area.name, |
| 490 | tens.purpose.name, |
| 491 | tens.format.name, |
| 492 | tens.name, |
| 493 | ) |
| 494 | ) |
| 495 | for idx, tens in enumerate(ps.intermediates): |
| 496 | print( |
| 497 | " Intermediate %2d %7.1f KB %-24s %-15s %-15s %-20s %s" |
| 498 | % ( |
| 499 | idx, |
| 500 | tens.storage_size() / 1024, |
| 501 | tens.storage_shape, |
| 502 | tens.mem_area.name, |
| 503 | tens.purpose.name, |
| 504 | tens.format.name, |
| 505 | tens.name, |
| 506 | ) |
| 507 | ) |
| 508 | for idx, tens in enumerate(ps.outputs): |
| 509 | print( |
| 510 | " Output %2d %7.1f KB %-24s %-15s %-15s %-20s %s" |
| 511 | % ( |
| 512 | idx, |
| 513 | tens.storage_size() / 1024, |
| 514 | tens.storage_shape, |
| 515 | tens.mem_area.name, |
| 516 | tens.purpose.name, |
| 517 | tens.format.name, |
| 518 | tens.name, |
| 519 | ) |
| 520 | ) |
| 521 | print() |
| 522 | |
| 523 | def print_high_level_command_stream(self): |
Michael McGeagh | 775e396 | 2020-07-28 11:44:22 +0100 | [diff] [blame] | 524 | print("print_high_level_command_stream()", self.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 525 | for idx, cmd in enumerate(self.high_level_command_stream): |
| 526 | print("%3d %s" % (idx, cmd)) |
| 527 | |
| 528 | |
| 529 | class Graph: |
| 530 | def __init__(self, name="<unnamed>", batch_size=1): |
| 531 | self.name = name |
| 532 | self.batch_size = batch_size |
| 533 | self.subgraphs = [] |
Michael McGeagh | 22f74e1 | 2020-08-07 16:21:03 +0100 | [diff] [blame] | 534 | self.metadata = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 535 | self.memory_used = {} |
Diqing Zhong | db5124c | 2021-01-11 12:52:48 +0100 | [diff] [blame] | 536 | self.total_original_weights = 0 |
Fredrik Svedberg | f5c07c4 | 2021-04-23 14:36:42 +0200 | [diff] [blame] | 537 | self.total_npu_weights = 0 |
| 538 | self.total_npu_encoded_weights = 0 |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 539 | self.weight_cache = None # See CompressedWeightCache |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 540 | |
| 541 | def get_root_subgraph(self): |
| 542 | return self.subgraphs[0] |
| 543 | |
| 544 | def prune_startup_init_pass(self): |
| 545 | for sg in self.subgraphs: |
| 546 | sg.prune_startup_init_pass() |
| 547 | |
| 548 | def update_consumers(self): |
| 549 | for sg in self.subgraphs: |
| 550 | sg.update_consumers() |
| 551 | |
| 552 | def refresh_after_modification(self): |
| 553 | for sg in self.subgraphs: |
| 554 | sg.refresh_after_modification() |
| 555 | |
| 556 | def print_operators(self): |
| 557 | for sg in self.subgraphs: |
| 558 | sg.print_operators() |
| 559 | |
Fredrik Svedberg | c875aa6 | 2021-05-06 09:53:31 +0200 | [diff] [blame] | 560 | def print_graph(self, label=None): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 561 | for sg in self.subgraphs: |
Fredrik Svedberg | c875aa6 | 2021-05-06 09:53:31 +0200 | [diff] [blame] | 562 | sg.print_graph(label) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 563 | |
| 564 | def print_graph_with_tensors(self): |
| 565 | for sg in self.subgraphs: |
| 566 | sg.print_graph_with_tensors() |
| 567 | |
| 568 | def print_graph_with_tensor_quantization(self): |
| 569 | for sg in self.subgraphs: |
| 570 | sg.print_graph_with_tensor_quantization() |
| 571 | |
| 572 | def print_passes(self): |
| 573 | for sg in self.subgraphs: |
| 574 | sg.print_passes() |
| 575 | |
| 576 | def print_passes_with_tensors(self): |
| 577 | for sg in self.subgraphs: |
| 578 | sg.print_passes_with_tensors() |
| 579 | |
| 580 | def print_cascaded_passes(self): |
| 581 | for sg in self.subgraphs: |
| 582 | sg.print_cascaded_passes() |
| 583 | |
| 584 | def print_cascaded_passes_with_tensors(self): |
| 585 | for sg in self.subgraphs: |
| 586 | sg.print_cascaded_passes_with_tensors() |
| 587 | |
| 588 | def print_cascaded_passes_with_tensor_sizes(self): |
| 589 | for sg in self.subgraphs: |
| 590 | sg.print_cascaded_passes_with_tensor_sizes() |
| 591 | |
| 592 | def print_high_level_command_stream(self): |
| 593 | for sg in self.subgraphs: |
| 594 | sg.print_high_level_command_stream() |