Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 1 | # Copyright (C) 2021 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. |
| 16 | # Description: |
| 17 | # Functions used to read from a TOSA format file. |
| 18 | import os.path |
| 19 | import struct |
| 20 | import sys |
| 21 | |
| 22 | import numpy as np |
| 23 | |
| 24 | from .nn_graph import Graph |
| 25 | from .nn_graph import Subgraph |
| 26 | from .operation import Op |
| 27 | from .operation import Operation |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 28 | from .reader_util import align_tensor_indices_to_nng |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 29 | from .reader_util import clone_and_reshape_tensor |
| 30 | from .reader_util import decode_str |
| 31 | from .reader_util import fixup_tensors |
| 32 | from .tensor import QuantizationParameters |
| 33 | from .tensor import Tensor |
| 34 | from .tflite_mapping import DataType |
| 35 | from .tosa.TosaGraph import TosaGraph as TG |
| 36 | from .tosa_mapping import datatype_map |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 37 | from .tosa_mapping import datatype_map_numpy |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 38 | from .tosa_mapping import tosa_operator_map |
| 39 | from .tosa_mapping import unsupported_tosa_operators |
| 40 | |
| 41 | |
| 42 | class TosaSubgraph: |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 43 | def __init__(self, graph, block): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 44 | self.graph = graph |
| 45 | self.name = decode_str(block.Name()) |
| 46 | |
| 47 | self.tensors = [] |
| 48 | for idx in range(block.TensorsLength()): |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 49 | self.tensors.append(self.parse_tensor(block.Tensors(idx))) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 50 | |
| 51 | for idx in range(block.OperatorsLength()): |
| 52 | self.parse_operator(idx, block.Operators(idx)) |
| 53 | |
| 54 | # Get the subgraph inputs and outputs |
| 55 | self.inputs = self.get_sg_inputs_remove_duplicates(block) |
| 56 | self.outputs = self.get_sg_outputs_remove_duplicates(block) |
| 57 | fixup_tensors(self.inputs, self.tensors) |
| 58 | |
| 59 | def get_sg_inputs_remove_duplicates(self, block): |
| 60 | inputs = [] |
| 61 | for idx in range(block.InputsLength()): |
| 62 | tens_data = block.Inputs(idx) |
| 63 | self.add_not_duplicate(tens_data, inputs, "input") |
| 64 | return inputs |
| 65 | |
| 66 | def get_sg_outputs_remove_duplicates(self, block): |
| 67 | outputs = [] |
| 68 | for idx in range(block.OutputsLength()): |
| 69 | tens_data = block.Outputs(idx) |
| 70 | self.add_not_duplicate(tens_data, outputs, "output") |
| 71 | return outputs |
| 72 | |
| 73 | def add_not_duplicate(self, tens_data, tensors, warning_str): |
| 74 | name = decode_str(tens_data) |
| 75 | tensor = self.get_tensor_by_name(name) |
| 76 | if tensor not in tensors: |
| 77 | tensors.append(tensor) |
| 78 | else: |
| 79 | print(f"Warning: Subgraph {warning_str} tensor ({tensor}) already seen. Removing the duplicate.") |
| 80 | |
| 81 | def get_tensor_by_name(self, name): |
| 82 | for tens in self.tensors: |
| 83 | if tens.name == name: |
| 84 | return tens |
| 85 | return None |
| 86 | |
| 87 | def parse_operator(self, op_index, op_data): |
| 88 | op_code = op_data.Op() |
| 89 | if op_code in unsupported_tosa_operators: |
| 90 | print("Unsupported Operator", op_code) |
| 91 | assert False |
| 92 | |
| 93 | op_type, attr_serializer, quant_serializer, indices = tosa_operator_map[op_code] |
| 94 | inputs = [] |
| 95 | outputs = [] |
| 96 | for idx in range(op_data.InputsLength()): |
| 97 | input_tens = self.get_tensor_by_name(decode_str(op_data.Inputs(idx))) |
| 98 | inputs.append(input_tens) |
| 99 | assert input_tens is not None |
| 100 | |
| 101 | for idx in range(op_data.OutputsLength()): |
| 102 | output_tens = self.get_tensor_by_name(decode_str(op_data.Outputs(idx))) |
| 103 | outputs.append(output_tens) |
| 104 | assert output_tens is not None |
| 105 | |
| 106 | name = "unknown_op_name" |
| 107 | if len(outputs): |
| 108 | name = outputs[0].name |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 109 | inputs = align_tensor_indices_to_nng(op_type, indices, inputs) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 110 | op = Operation(op_type, name) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 111 | op.op_index = op_index |
| 112 | op.inputs = inputs |
| 113 | op.outputs = outputs |
| 114 | |
| 115 | for out in op.outputs: |
| 116 | out.ops = [op] |
| 117 | |
| 118 | # TODO Transpose_conv and conv3d |
| 119 | if op.type.is_depthwise_conv2d_op() or op.type.is_conv2d_op() or op.type == Op.FullyConnected: |
| 120 | if inputs[1].values is not None: |
| 121 | if op.type == Op.FullyConnected: |
| 122 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 0), False) |
| 123 | elif op.type.is_conv2d_op(): |
| 124 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 2, 3, 0), False) |
| 125 | elif op.type.is_depthwise_conv2d_op(): |
| 126 | inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 2, 0, 3), False) |
| 127 | if op.type.needs_bias() and len(inputs) <= op_type.info.indices.biases[0]: |
| 128 | # No Bias tensor |
| 129 | inputs.append(None) |
| 130 | if inputs[-1] and inputs[-1].values is not None: |
| 131 | # Since bias tensor is used for both bias and scale, |
| 132 | # a clone with a unique equivalence_id is needed |
| 133 | inputs[-1] = clone_and_reshape_tensor(inputs[-1], (0,), True) |
| 134 | |
| 135 | if attr_serializer is not None: |
| 136 | op.attrs = attr_serializer.deserialize(op_data) |
| 137 | |
| 138 | if "dilation" in op.attrs: |
| 139 | dilation = op.attrs["dilation"] |
| 140 | if len(dilation) == 2: |
| 141 | op.attrs["dilation"] = (1, dilation[0], dilation[1], 1) |
| 142 | elif len(dilation) == 3: |
| 143 | # TODO CONV3D more to be done.... |
| 144 | op.attrs["dilation"] = (dilation[0], dilation[1], dilation[2], 1) |
| 145 | if "kernel" in op.attrs: |
| 146 | kernel = op.attrs["kernel"] |
| 147 | if len(kernel) == 2: |
| 148 | op.attrs["ksize"] = (1, kernel[0], kernel[1], 1) |
| 149 | else: |
| 150 | # TODO CONV3D more to be done.... |
| 151 | print("Unsupported kernel dimensions: ", len(kernel)) |
| 152 | assert False |
| 153 | |
| 154 | if quant_serializer is not None: |
| 155 | quant_info = quant_serializer.deserialize(op_data) |
| 156 | |
| 157 | # TODO tensor zero points currently set here |
| 158 | # zero points part of Rescale operation, handled in tosa_graph_optimizer |
| 159 | if "input_zp" in quant_info: |
| 160 | self.set_tensor_zp(op.ifm, quant_info["input_zp"]) |
| 161 | if "weight_zp" in quant_info: |
| 162 | self.set_tensor_zp(op.weights, quant_info["weight_zp"]) |
| 163 | if "ouput_zp" in quant_info: |
| 164 | self.set_tensor_zp(op.ofm, quant_info["output_zp"]) |
| 165 | if "a_zp" in quant_info: |
| 166 | self.set_tensor_zp(op.ifm, quant_info["a_zp"]) |
| 167 | if "b_zp" in quant_info: |
| 168 | self.set_tensor_zp(op.ifm2, quant_info["b_zp"]) |
| 169 | |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 170 | def parse_tensor(self, tens_data): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 171 | name = decode_str(tens_data.Name()) |
| 172 | np_shape = tens_data.ShapeAsNumpy() |
| 173 | shape = list(np_shape) if type(np_shape) is np.ndarray else [] |
| 174 | tens_dtype = tens_data.Type() |
| 175 | dtype = datatype_map[tens_dtype] |
| 176 | |
| 177 | tens = Tensor(shape, dtype, name) |
| 178 | |
| 179 | # Initialize quantization parameters |
| 180 | tens.quantization = QuantizationParameters() |
| 181 | |
| 182 | tens.quantization.scale_f32 = 1.0 |
| 183 | if dtype == DataType.uint8: |
| 184 | tens.quantization.quant_min = 0 |
| 185 | tens.quantization.quant_max = (1 << dtype.bits) - 1 |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 186 | elif dtype in (DataType.int8, DataType.int16, DataType.int32, DataType.int48): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 187 | tens.quantization.quant_min = -(1 << (dtype.bits - 1)) |
| 188 | tens.quantization.quant_max = (1 << (dtype.bits - 1)) - 1 |
| 189 | |
| 190 | tens.values = None |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 191 | |
| 192 | data_length = tens_data.DataLength() |
| 193 | if data_length != 0: |
| 194 | data_as_numpy = tens_data.DataAsNumpy() |
| 195 | if tens_dtype in datatype_map_numpy: |
| 196 | np_dtype = datatype_map_numpy[tens_dtype] |
| 197 | tens.values = np.array(data_as_numpy.view(np_dtype).reshape(shape)) |
| 198 | else: |
| 199 | # int48 is only expected as an accumulated data/output format, int4 not supported |
| 200 | print(f"Error: unsupported/unexpected Tensor type {dtype}, with data") |
| 201 | assert False |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 202 | |
| 203 | return tens |
| 204 | |
| 205 | def set_tensor_zp(self, tens, zp): |
| 206 | if tens.quantization.zero_point is None: |
| 207 | tens.quantization.zero_point = zp |
| 208 | elif tens.quantization.zero_point != zp: |
| 209 | print(f"Error: Setting tensor zp not possible, tensor already has different zero point") |
| 210 | assert False |
| 211 | |
| 212 | |
| 213 | class TosaGraph: |
| 214 | def __init__(self, filename, batch_size, feed_dict, output_node_names, initialisation_nodes): |
| 215 | |
| 216 | self.op_times = {} |
| 217 | if batch_size is None: |
| 218 | batch_size = 1 |
| 219 | self.batch_size = batch_size |
| 220 | self.name = os.path.splitext(os.path.basename(filename))[0] |
| 221 | self.initialisation_nodes = initialisation_nodes |
| 222 | |
| 223 | with open(filename, "rb") as f: |
| 224 | buf = bytearray(f.read()) |
| 225 | |
| 226 | try: |
| 227 | parsing_step = "parsing root" |
| 228 | tosa_graph = TG.GetRootAsTosaGraph(buf, 0) |
| 229 | |
| 230 | parsing_step = "parsing version" |
| 231 | self.check_version(tosa_graph) |
| 232 | |
| 233 | parsing_step = "parsing blocks length" |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 234 | self.subgraphs = [] |
| 235 | for b_idx in range(tosa_graph.BlocksLength()): |
| 236 | parsing_step = f"parsing block {b_idx}" |
Patrik Gustavsson | d15866c | 2021-08-10 13:56:34 +0200 | [diff] [blame] | 237 | self.subgraphs.append(TosaSubgraph(self, tosa_graph.Blocks(b_idx))) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 238 | |
| 239 | self.nng = Graph(self.name, self.batch_size) |
| 240 | for tosa_sg in self.subgraphs: |
| 241 | sg = Subgraph(tosa_sg.name) |
| 242 | sg.original_inputs = tosa_sg.inputs # Preserve the original input order |
| 243 | sg.output_tensors = tosa_sg.outputs |
| 244 | self.nng.subgraphs.append(sg) |
| 245 | |
| 246 | except (struct.error, TypeError, RuntimeError) as e: |
| 247 | print(f'Error: Invalid .tosa file. Got "{e}" while {parsing_step}.') |
| 248 | sys.exit(1) |
| 249 | |
| 250 | def check_version(self, tosa_graph): |
| 251 | version = tosa_graph.Version() |
| 252 | version_str = f"{version._major()}.{version._minor()}.{version._patch()}" |
| 253 | if version_str != "0.22.0": |
| 254 | print(f"Unsupported TOSA version: {version_str}") |
| 255 | assert False |
| 256 | |
| 257 | |
| 258 | def read_tosa(filename, batch_size, feed_dict, output_node_names, initialisation_nodes): |
| 259 | tosa_graph = TosaGraph(filename, batch_size, feed_dict, output_node_names, initialisation_nodes) |
| 260 | nng = tosa_graph.nng |
| 261 | nng.refresh_after_modification() |
| 262 | return nng |