Rob Elliott | 78b9412 | 2024-01-25 13:05:16 +0000 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2021-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [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. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 16 | # |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 17 | # Description: |
| 18 | # Functions used to read from a TOSA format file. |
| 19 | import os.path |
| 20 | import struct |
| 21 | import sys |
| 22 | |
| 23 | import numpy as np |
| 24 | |
| 25 | from .nn_graph import Graph |
| 26 | from .nn_graph import Subgraph |
Fredrik Svedberg | 4a434cb | 2022-09-27 14:13:01 +0200 | [diff] [blame] | 27 | from .operation import ExplicitScaling |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 28 | from .operation import Op |
| 29 | from .operation import Operation |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 30 | from .reader_util import align_tensor_indices_to_nng |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 31 | from .reader_util import clone_and_reshape_tensor |
| 32 | from .reader_util import decode_str |
| 33 | from .reader_util import fixup_tensors |
Patrik Gustavsson | f1580f0 | 2021-09-01 12:43:02 +0200 | [diff] [blame] | 34 | from .shape4d import Shape4D |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 35 | from .tensor import QuantizationParameters |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 36 | from .tensor import shape_num_elements |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 37 | from .tensor import Tensor |
| 38 | from .tflite_mapping import DataType |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 39 | from .tosa.Op import Op as TosaOp |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 40 | from .tosa.TosaGraph import TosaGraph as TG |
| 41 | from .tosa_mapping import datatype_map |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 42 | from .tosa_mapping import datatype_map_numpy |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 43 | from .tosa_mapping import tosa_operator_map |
| 44 | from .tosa_mapping import unsupported_tosa_operators |
| 45 | |
| 46 | |
| 47 | class TosaSubgraph: |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 48 | def __init__(self, graph, block): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 49 | self.graph = graph |
| 50 | self.name = decode_str(block.Name()) |
| 51 | |
| 52 | self.tensors = [] |
| 53 | for idx in range(block.TensorsLength()): |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 54 | self.tensors.append(self.parse_tensor(block.Tensors(idx))) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 55 | |
| 56 | for idx in range(block.OperatorsLength()): |
| 57 | self.parse_operator(idx, block.Operators(idx)) |
| 58 | |
| 59 | # Get the subgraph inputs and outputs |
| 60 | self.inputs = self.get_sg_inputs_remove_duplicates(block) |
| 61 | self.outputs = self.get_sg_outputs_remove_duplicates(block) |
| 62 | fixup_tensors(self.inputs, self.tensors) |
| 63 | |
| 64 | def get_sg_inputs_remove_duplicates(self, block): |
| 65 | inputs = [] |
| 66 | for idx in range(block.InputsLength()): |
| 67 | tens_data = block.Inputs(idx) |
| 68 | self.add_not_duplicate(tens_data, inputs, "input") |
| 69 | return inputs |
| 70 | |
| 71 | def get_sg_outputs_remove_duplicates(self, block): |
| 72 | outputs = [] |
| 73 | for idx in range(block.OutputsLength()): |
| 74 | tens_data = block.Outputs(idx) |
| 75 | self.add_not_duplicate(tens_data, outputs, "output") |
| 76 | return outputs |
| 77 | |
| 78 | def add_not_duplicate(self, tens_data, tensors, warning_str): |
| 79 | name = decode_str(tens_data) |
| 80 | tensor = self.get_tensor_by_name(name) |
| 81 | if tensor not in tensors: |
| 82 | tensors.append(tensor) |
| 83 | else: |
| 84 | print(f"Warning: Subgraph {warning_str} tensor ({tensor}) already seen. Removing the duplicate.") |
| 85 | |
| 86 | def get_tensor_by_name(self, name): |
| 87 | for tens in self.tensors: |
| 88 | if tens.name == name: |
| 89 | return tens |
| 90 | return None |
| 91 | |
| 92 | def parse_operator(self, op_index, op_data): |
| 93 | op_code = op_data.Op() |
| 94 | if op_code in unsupported_tosa_operators: |
| 95 | print("Unsupported Operator", op_code) |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 96 | for opname in dir(TosaOp): |
| 97 | if op_code == getattr(TosaOp, opname): |
| 98 | print(f" {opname}") |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 99 | return |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 100 | |
| 101 | op_type, attr_serializer, quant_serializer, indices = tosa_operator_map[op_code] |
| 102 | inputs = [] |
| 103 | outputs = [] |
| 104 | for idx in range(op_data.InputsLength()): |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 105 | input = decode_str(op_data.Inputs(idx)) |
| 106 | input_tens = self.get_tensor_by_name(input) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 107 | inputs.append(input_tens) |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 108 | if input_tens is None: |
| 109 | print(f"could not find named input tensor {input}::{input_tens}") |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 110 | assert input_tens is not None |
| 111 | |
| 112 | for idx in range(op_data.OutputsLength()): |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 113 | output = decode_str(op_data.Outputs(idx)) |
| 114 | output_tens = self.get_tensor_by_name(output) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 115 | outputs.append(output_tens) |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 116 | if output_tens is None: |
| 117 | print(f"could not find named output tensor {output}::{output_tens}") |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 118 | assert output_tens is not None |
| 119 | |
| 120 | name = "unknown_op_name" |
| 121 | if len(outputs): |
| 122 | name = outputs[0].name |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 123 | inputs = align_tensor_indices_to_nng(op_type, indices, inputs) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 124 | op = Operation(op_type, name) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 125 | op.op_index = op_index |
| 126 | op.inputs = inputs |
| 127 | op.outputs = outputs |
| 128 | |
| 129 | for out in op.outputs: |
| 130 | out.ops = [op] |
| 131 | |
| 132 | # TODO Transpose_conv and conv3d |
| 133 | if op.type.is_depthwise_conv2d_op() or op.type.is_conv2d_op() or op.type == Op.FullyConnected: |
Oscar Andersson | b90666d | 2024-02-29 14:35:58 +0100 | [diff] [blame] | 134 | |
| 135 | def _remove_producing_identity_op(prod_op): |
| 136 | # find the producing op that is not an identity op and return it |
| 137 | while prod_op.type == Op.Identity: |
| 138 | prod_op = prod_op.inputs[0].ops[0] # get previous op |
| 139 | return prod_op |
| 140 | |
| 141 | def _check_and_get_connection(prod_op, tens): |
| 142 | # check weight producing op can be connected to the weight tensor |
| 143 | assert len(prod_op.outputs) == 1 |
| 144 | assert tens.shape == prod_op.outputs[0].shape |
| 145 | # only need to connect the current op connection as the tensor consuming connections haven't been |
| 146 | # initialised yet |
| 147 | return prod_op.outputs[0] |
| 148 | |
| 149 | # remove identity ops directly connected to the weight input of conv like ops |
| 150 | weights_producer_op = _remove_producing_identity_op(inputs[1].ops[0]) |
| 151 | inputs[1] = _check_and_get_connection(weights_producer_op, inputs[1]) # update connection |
| 152 | |
| 153 | if weights_producer_op.type == Op.Transpose: |
| 154 | # remove transpose op such that the weight op will a const op |
| 155 | transpose_op = weights_producer_op |
| 156 | # remove identity ops directly connected to the input of the transpose op |
| 157 | transpose_producer_op = _remove_producing_identity_op(transpose_op.inputs[0].ops[0]) |
| 158 | transpose_op.inputs[0] = _check_and_get_connection( |
| 159 | transpose_producer_op, transpose_op.inputs[0] |
| 160 | ) # update connection |
| 161 | |
| 162 | perms = transpose_op.attrs["perms"] |
| 163 | inputs[1] = clone_and_reshape_tensor(transpose_op.inputs[0], perms, False) |
| 164 | |
| 165 | if weights_producer_op.type == Op.Reshape: |
| 166 | # remove reshape op such that the weight op will a const op |
| 167 | reshape_op = weights_producer_op |
| 168 | # remove identity ops directly connected to the input of the reshape op |
| 169 | reshape_producer_op = _remove_producing_identity_op(reshape_op.inputs[0].ops[0]) |
| 170 | reshape_op.inputs[0] = _check_and_get_connection( |
| 171 | reshape_producer_op, reshape_op.inputs[0] |
| 172 | ) # update connection |
| 173 | |
| 174 | tens = reshape_op.inputs[0].clone("_reshape", False) |
| 175 | tens.values = np.reshape(tens.values, reshape_op.ofm.shape) |
| 176 | tens.shape = reshape_op.ofm.shape |
| 177 | tens._original_shape = tens.shape |
| 178 | tens.bandwidth_shape = tens.shape |
| 179 | tens.storage_shape = tens.shape |
| 180 | |
| 181 | tmp_op = Operation(Op.Const, tens.name) |
| 182 | tmp_op.set_output_tensor(tens) |
| 183 | inputs[1] = tens |
| 184 | |
| 185 | assert inputs[1].values is not None |
| 186 | |
| 187 | if op.type == Op.FullyConnected: |
| 188 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 0), False) |
| 189 | elif op.type.is_conv2d_op(): |
| 190 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 2, 3, 0), False) |
| 191 | elif op.type.is_depthwise_conv2d_op(): |
Per Ã…strand | 92240e7 | 2024-03-25 22:30:12 +0100 | [diff] [blame] | 192 | HWCM_to_HWOI = (0, 1, 3, 2) |
| 193 | inputs[1] = clone_and_reshape_tensor(inputs[1], HWCM_to_HWOI, False) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 194 | if op.type.needs_bias() and len(inputs) <= op_type.info.indices.biases[0]: |
| 195 | # No Bias tensor |
| 196 | inputs.append(None) |
| 197 | if inputs[-1] and inputs[-1].values is not None: |
| 198 | # Since bias tensor is used for both bias and scale, |
| 199 | # a clone with a unique equivalence_id is needed |
| 200 | inputs[-1] = clone_and_reshape_tensor(inputs[-1], (0,), True) |
| 201 | |
Oscar Andersson | b90666d | 2024-02-29 14:35:58 +0100 | [diff] [blame] | 202 | op.explicit_scaling = ExplicitScaling(False, [0], [1]) # no scaling |
| 203 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 204 | if attr_serializer is not None: |
| 205 | op.attrs = attr_serializer.deserialize(op_data) |
| 206 | |
Oscar Andersson | b90666d | 2024-02-29 14:35:58 +0100 | [diff] [blame] | 207 | if "pad" in op.attrs: |
| 208 | op.attrs["padding"] = op.attrs["pad"] # attribute was renamed to padding |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 209 | padding = op.attrs["padding"] # [top, bottom, left, right] |
| 210 | op.attrs["explicit_padding"] = ( |
| 211 | padding[0], |
| 212 | padding[2], |
| 213 | padding[1], |
| 214 | padding[3], |
| 215 | ) # [top, left, bottom, right] |
| 216 | if "stride" in op.attrs: |
| 217 | stride = op.attrs["stride"] |
| 218 | if len(stride) == 2: |
| 219 | op.attrs["strides"] = (1, stride[0], stride[1], 1) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 220 | del op.attrs["stride"] |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 221 | else: |
| 222 | # TODO CONV3D more to be done.... |
| 223 | print("Unsupported kernel dimensions: ", len(stride)) |
| 224 | assert False |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 225 | if "dilation" in op.attrs: |
| 226 | dilation = op.attrs["dilation"] |
| 227 | if len(dilation) == 2: |
| 228 | op.attrs["dilation"] = (1, dilation[0], dilation[1], 1) |
| 229 | elif len(dilation) == 3: |
| 230 | # TODO CONV3D more to be done.... |
| 231 | op.attrs["dilation"] = (dilation[0], dilation[1], dilation[2], 1) |
| 232 | if "kernel" in op.attrs: |
| 233 | kernel = op.attrs["kernel"] |
| 234 | if len(kernel) == 2: |
| 235 | op.attrs["ksize"] = (1, kernel[0], kernel[1], 1) |
| 236 | else: |
| 237 | # TODO CONV3D more to be done.... |
| 238 | print("Unsupported kernel dimensions: ", len(kernel)) |
| 239 | assert False |
Patrik Gustavsson | b081d67 | 2021-08-25 13:49:25 +0200 | [diff] [blame] | 240 | if "shift" in op.attrs and op.type == Op.Mul: |
| 241 | shift = op.attrs["shift"] |
| 242 | if shift != 0: |
Fredrik Svedberg | 4a434cb | 2022-09-27 14:13:01 +0200 | [diff] [blame] | 243 | op.explicit_scaling = ExplicitScaling(False, [shift], [1]) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 244 | if op.type.is_depthwise_conv2d_op(): |
Per Ã…strand | 92240e7 | 2024-03-25 22:30:12 +0100 | [diff] [blame] | 245 | assert op.weights.shape[-1] % op.ifm.shape[-1] == 0 |
| 246 | depth_multiplier = op.weights.shape[-1] / op.ifm.shape[-1] |
| 247 | if depth_multiplier > 1: |
| 248 | assert op.ifm.shape[-1] == 1 and op.ofm.shape[-1] == depth_multiplier, ( |
| 249 | "For depth multipliers > 1, IFM channels must be 1 and " |
Per Ã…strand | bab7f28 | 2024-04-22 11:48:09 +0200 | [diff] [blame] | 250 | "OFM channels must be equal to the depth multiplier" |
| 251 | ) |
Per Ã…strand | 92240e7 | 2024-03-25 22:30:12 +0100 | [diff] [blame] | 252 | op.attrs["depth_multiplier"] = depth_multiplier |
Patrik Gustavsson | f1580f0 | 2021-09-01 12:43:02 +0200 | [diff] [blame] | 253 | if op.type == Op.SplitSliceRead: |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 254 | op.read_offsets[0] = Shape4D.from_list(list(op.attrs["start"]), 0) |
Patrik Gustavsson | f1580f0 | 2021-09-01 12:43:02 +0200 | [diff] [blame] | 255 | op.read_shapes[0] = op.attrs["size"] |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 256 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 257 | # TODO tensor zero points currently set here |
| 258 | # zero points part of Rescale operation, handled in tosa_graph_optimizer |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 259 | if "input_zp" in op.attrs: |
| 260 | self.set_tensor_zp(op.ifm, op.attrs["input_zp"]) |
| 261 | if "weight_zp" in op.attrs: |
| 262 | self.set_tensor_zp(op.weights, op.attrs["weight_zp"]) |
| 263 | if "output_zp" in op.attrs: |
| 264 | self.set_tensor_zp(op.ofm, op.attrs["output_zp"]) |
| 265 | if "a_zp" in op.attrs: |
| 266 | self.set_tensor_zp(op.ifm, op.attrs["a_zp"]) |
| 267 | if "b_zp" in op.attrs: |
| 268 | self.set_tensor_zp(op.ifm2, op.attrs["b_zp"]) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 269 | |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 270 | def parse_tensor(self, tens_data): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 271 | name = decode_str(tens_data.Name()) |
| 272 | np_shape = tens_data.ShapeAsNumpy() |
| 273 | shape = list(np_shape) if type(np_shape) is np.ndarray else [] |
| 274 | tens_dtype = tens_data.Type() |
| 275 | dtype = datatype_map[tens_dtype] |
| 276 | |
| 277 | tens = Tensor(shape, dtype, name) |
| 278 | |
| 279 | # Initialize quantization parameters |
| 280 | tens.quantization = QuantizationParameters() |
| 281 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 282 | if dtype == DataType.uint8: |
| 283 | tens.quantization.quant_min = 0 |
| 284 | tens.quantization.quant_max = (1 << dtype.bits) - 1 |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 285 | elif dtype in (DataType.int8, DataType.int16, DataType.int32, DataType.int48): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 286 | tens.quantization.quant_min = -(1 << (dtype.bits - 1)) |
| 287 | tens.quantization.quant_max = (1 << (dtype.bits - 1)) - 1 |
| 288 | |
| 289 | tens.values = None |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 290 | |
| 291 | data_length = tens_data.DataLength() |
| 292 | if data_length != 0: |
| 293 | data_as_numpy = tens_data.DataAsNumpy() |
| 294 | if tens_dtype in datatype_map_numpy: |
| 295 | np_dtype = datatype_map_numpy[tens_dtype] |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 296 | |
| 297 | # TOSA pads the tensor data |
| 298 | shape_elements = shape_num_elements(shape) |
| 299 | values = np.array(data_as_numpy.view(np_dtype)) |
| 300 | values = values[0:shape_elements] |
| 301 | tens.values = values.reshape(shape) |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 302 | else: |
| 303 | # int48 is only expected as an accumulated data/output format, int4 not supported |
| 304 | print(f"Error: unsupported/unexpected Tensor type {dtype}, with data") |
| 305 | assert False |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 306 | |
| 307 | return tens |
| 308 | |
| 309 | def set_tensor_zp(self, tens, zp): |
| 310 | if tens.quantization.zero_point is None: |
| 311 | tens.quantization.zero_point = zp |
| 312 | elif tens.quantization.zero_point != zp: |
Jonas Ohlsson | 25e700c | 2022-03-04 14:58:56 +0100 | [diff] [blame] | 313 | print("Error: Setting tensor zp not possible, tensor already has different zero point") |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 314 | assert False |
| 315 | |
| 316 | |
| 317 | class TosaGraph: |
| 318 | def __init__(self, filename, batch_size, feed_dict, output_node_names, initialisation_nodes): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 319 | self.op_times = {} |
| 320 | if batch_size is None: |
| 321 | batch_size = 1 |
| 322 | self.batch_size = batch_size |
| 323 | self.name = os.path.splitext(os.path.basename(filename))[0] |
| 324 | self.initialisation_nodes = initialisation_nodes |
| 325 | |
| 326 | with open(filename, "rb") as f: |
| 327 | buf = bytearray(f.read()) |
| 328 | |
| 329 | try: |
| 330 | parsing_step = "parsing root" |
| 331 | tosa_graph = TG.GetRootAsTosaGraph(buf, 0) |
| 332 | |
| 333 | parsing_step = "parsing version" |
| 334 | self.check_version(tosa_graph) |
| 335 | |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 336 | parsing_step = "parsing single main region" |
| 337 | assert 1 == tosa_graph.RegionsLength() |
| 338 | assert b"main" == tosa_graph.Regions(0).Name() |
| 339 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 340 | parsing_step = "parsing blocks length" |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 341 | self.subgraphs = [] |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 342 | for b_idx in range(tosa_graph.Regions(0).BlocksLength()): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 343 | parsing_step = f"parsing block {b_idx}" |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 344 | self.subgraphs.append(TosaSubgraph(self, tosa_graph.Regions(0).Blocks(b_idx))) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 345 | |
| 346 | self.nng = Graph(self.name, self.batch_size) |
| 347 | for tosa_sg in self.subgraphs: |
| 348 | sg = Subgraph(tosa_sg.name) |
| 349 | sg.original_inputs = tosa_sg.inputs # Preserve the original input order |
| 350 | sg.output_tensors = tosa_sg.outputs |
| 351 | self.nng.subgraphs.append(sg) |
| 352 | |
| 353 | except (struct.error, TypeError, RuntimeError) as e: |
| 354 | print(f'Error: Invalid .tosa file. Got "{e}" while {parsing_step}.') |
| 355 | sys.exit(1) |
| 356 | |
| 357 | def check_version(self, tosa_graph): |
| 358 | version = tosa_graph.Version() |
Rob Elliott | 00a15db | 2023-08-17 14:27:06 +0000 | [diff] [blame] | 359 | version_str = f"{version._Major()}.{version._Minor()}.{version._Patch()}" |
Johan Alfven | 31947ad | 2024-04-04 15:50:08 +0200 | [diff] [blame] | 360 | if version_str not in ("0.80.0", "0.80.1"): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 361 | print(f"Unsupported TOSA version: {version_str}") |
| 362 | assert False |
| 363 | |
| 364 | |
| 365 | def read_tosa(filename, batch_size, feed_dict, output_node_names, initialisation_nodes): |
| 366 | tosa_graph = TosaGraph(filename, batch_size, feed_dict, output_node_names, initialisation_nodes) |
| 367 | nng = tosa_graph.nng |
| 368 | nng.refresh_after_modification() |
| 369 | return nng |