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 | # Utlity function for reading .tosa and .tflite files |
| 18 | from .operation import Op |
| 19 | from .operation import Operation |
| 20 | |
| 21 | |
| 22 | def decode_str(s): |
| 23 | if s is None: |
| 24 | return "" |
| 25 | return s.decode("utf-8") |
| 26 | |
| 27 | |
| 28 | def clone_and_reshape_tensor(src_tens, reorder, set_unique): |
| 29 | tens = src_tens.clone("_reshape", set_unique) |
| 30 | tens.shape = [src_tens.shape[idx] for idx in reorder] |
| 31 | tens.bandwidth_shape = tens.shape |
| 32 | tens.storage_shape = tens.shape |
| 33 | |
| 34 | if tens.values is not None: |
| 35 | tens.values = tens.values.transpose(reorder) |
| 36 | |
| 37 | if tens.quant_values is not None: |
| 38 | tens.quant_values = tens.quant_values.transpose(reorder) |
| 39 | |
| 40 | op = Operation(Op.Const, tens.name) |
| 41 | op.set_output_tensor(tens) |
| 42 | return tens |
| 43 | |
| 44 | |
| 45 | # Fix up tensors without operations. Generate either Placeholder or Constant ops |
| 46 | def fixup_tensors(input_tensors, tensors): |
| 47 | for tens in input_tensors: |
| 48 | if len(tens.ops) and tens.ops[0].type == Op.Const: |
| 49 | break |
| 50 | |
| 51 | if tens.ops != []: |
| 52 | tens.error("This subgraph input tensor has unexpected driving operators.") |
| 53 | |
| 54 | op = Operation(Op.Placeholder, tens.name) |
| 55 | op.set_output_tensor(tens) |
| 56 | |
| 57 | for tens in tensors: |
| 58 | if not tens.ops: |
| 59 | op = Operation(Op.Const, tens.name) |
| 60 | op.set_output_tensor(tens) |