Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: Copyright 2021-2022 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 | # Utlity function for reading .tosa and .tflite files |
| 19 | from .operation import Op |
| 20 | from .operation import Operation |
| 21 | |
| 22 | |
| 23 | def decode_str(s): |
| 24 | if s is None: |
| 25 | return "" |
| 26 | return s.decode("utf-8") |
| 27 | |
| 28 | |
| 29 | def clone_and_reshape_tensor(src_tens, reorder, set_unique): |
| 30 | tens = src_tens.clone("_reshape", set_unique) |
Johan Alfvén | 53605be | 2022-10-26 12:52:17 +0200 | [diff] [blame] | 31 | if reorder is None: |
Tim Hall | 92cd33b | 2022-11-03 12:25:33 +0000 | [diff] [blame] | 32 | # reorder of None is a special case meaning 1D shape requested |
| 33 | tens.as_1D() |
Johan Alfvén | 53605be | 2022-10-26 12:52:17 +0200 | [diff] [blame] | 34 | else: |
Tim Hall | 92cd33b | 2022-11-03 12:25:33 +0000 | [diff] [blame] | 35 | tens.transpose(reorder) |
| 36 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 37 | tens.bandwidth_shape = tens.shape |
| 38 | tens.storage_shape = tens.shape |
| 39 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 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) |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def align_inputs_indices(from_indices, to_indices, inputs): |
| 64 | to_list = to_indices.ifms + to_indices.weights + to_indices.biases |
| 65 | from_list = from_indices.ifms + from_indices.weights + from_indices.biases |
| 66 | |
| 67 | assert len(to_list) == len(from_list) |
| 68 | if to_list != from_list: |
| 69 | for idx, t_idx in enumerate(to_list): |
| 70 | if t_idx >= len(inputs): |
| 71 | # Biases are allowed to be left out |
| 72 | assert t_idx in from_indices.biases and t_idx in to_indices.biases |
| 73 | continue |
| 74 | if to_list[idx] != from_list[idx]: |
| 75 | # find t_idx in from list and swap. |
| 76 | for jdx in from_list[idx:]: |
| 77 | if from_list[jdx] == t_idx: |
| 78 | inputs[idx], inputs[jdx] = inputs[jdx], inputs[idx] |
| 79 | from_list[idx], from_list[jdx] = from_list[jdx], from_list[idx] |
| 80 | break |
| 81 | assert from_list == to_list |
| 82 | return inputs |
| 83 | |
| 84 | |
| 85 | def align_tensor_indices_to_nng(op_type, indices, inputs): |
| 86 | nng_op = Op(op_type) |
| 87 | return align_inputs_indices(indices, nng_op.info.indices, inputs) |