blob: b5a058e3e37b3f101ac15ffa660eb6e40991cd85 [file] [log] [blame]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +02001# 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
Johan Alfvén53605be2022-10-26 12:52:17 +020018import numpy as np
19
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020020from .operation import Op
21from .operation import Operation
22
23
24def decode_str(s):
25 if s is None:
26 return ""
27 return s.decode("utf-8")
28
29
30def clone_and_reshape_tensor(src_tens, reorder, set_unique):
31 tens = src_tens.clone("_reshape", set_unique)
Johan Alfvén53605be2022-10-26 12:52:17 +020032 if reorder is None:
33 # 1D shape requested
34 tens.shape = [np.prod(src_tens.shape)]
35 else:
36 tens.shape = [src_tens.shape[idx] for idx in reorder]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020037 tens.bandwidth_shape = tens.shape
38 tens.storage_shape = tens.shape
39
40 if tens.values is not None:
Johan Alfvén53605be2022-10-26 12:52:17 +020041 if reorder is None:
42 # 1D shape requested
43 tens.values = tens.values.reshape(tens.shape)
44 else:
45 tens.values = tens.values.transpose(reorder)
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020046
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020047 op = Operation(Op.Const, tens.name)
48 op.set_output_tensor(tens)
49 return tens
50
51
52# Fix up tensors without operations. Generate either Placeholder or Constant ops
53def fixup_tensors(input_tensors, tensors):
54 for tens in input_tensors:
55 if len(tens.ops) and tens.ops[0].type == Op.Const:
56 break
57
58 if tens.ops != []:
59 tens.error("This subgraph input tensor has unexpected driving operators.")
60
61 op = Operation(Op.Placeholder, tens.name)
62 op.set_output_tensor(tens)
63
64 for tens in tensors:
65 if not tens.ops:
66 op = Operation(Op.Const, tens.name)
67 op.set_output_tensor(tens)
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020068
69
70def align_inputs_indices(from_indices, to_indices, inputs):
71 to_list = to_indices.ifms + to_indices.weights + to_indices.biases
72 from_list = from_indices.ifms + from_indices.weights + from_indices.biases
73
74 assert len(to_list) == len(from_list)
75 if to_list != from_list:
76 for idx, t_idx in enumerate(to_list):
77 if t_idx >= len(inputs):
78 # Biases are allowed to be left out
79 assert t_idx in from_indices.biases and t_idx in to_indices.biases
80 continue
81 if to_list[idx] != from_list[idx]:
82 # find t_idx in from list and swap.
83 for jdx in from_list[idx:]:
84 if from_list[jdx] == t_idx:
85 inputs[idx], inputs[jdx] = inputs[jdx], inputs[idx]
86 from_list[idx], from_list[jdx] = from_list[jdx], from_list[idx]
87 break
88 assert from_list == to_list
89 return inputs
90
91
92def align_tensor_indices_to_nng(op_type, indices, inputs):
93 nng_op = Op(op_type)
94 return align_inputs_indices(indices, nng_op.info.indices, inputs)