xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/toco/graph_transformations/resolve_reorder_axes.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <algorithm>
16 #include <memory>
17 #include <string>
18 #include <unordered_map>
19 #include <utility>
20 #include <vector>
21 
22 #include "tensorflow/core/platform/logging.h"
23 #include "tensorflow/lite/toco/graph_transformations/graph_transformations.h"
24 #include "tensorflow/lite/toco/model.h"
25 #include "tensorflow/lite/toco/tooling_util.h"
26 
27 namespace toco {
28 
29 namespace {
30 
RenameArray(Model * model,const std::string & oldname,const std::string & desired_newname)31 void RenameArray(Model* model, const std::string& oldname,
32                  const std::string& desired_newname) {
33   const std::string& newname = AvailableArrayName(*model, desired_newname);
34   auto& arrays = model->GetMutableArrayMap();
35   arrays[newname] = std::move(arrays[oldname]);
36   arrays.erase(oldname);
37   for (const auto& op : model->operators) {
38     for (std::string& input : op->inputs) {
39       if (input == oldname) {
40         input = newname;
41       }
42     }
43     for (std::string& output : op->outputs) {
44       if (output == oldname) {
45         output = newname;
46       }
47     }
48   }
49 }
50 
51 }  // namespace
52 
53 // Reorder the elements of an input_array according to the input_axes_order and
54 // output_axes_order. Then adjust the shapes of the input and output arrays
55 // accordingly. Note that input_array must have a buffer (that is, it is a
56 // constant array).
57 template <typename T, ArrayDataType DataType>
ReorderAxes(AxesOrder input_axes_order,AxesOrder output_axes_order,const Array & input_array,Array * output_array)58 void ReorderAxes(AxesOrder input_axes_order, AxesOrder output_axes_order,
59                  const Array& input_array, Array* output_array) {
60   DCHECK(input_array.buffer->type == DataType);
61   DCHECK(!output_array->buffer);
62   const auto& input_data = input_array.GetBuffer<DataType>().data;
63   auto& output_data = output_array->GetMutableBuffer<DataType>().data;
64   output_data.resize(RequiredBufferSizeForShape(output_array->shape()));
65   // TODO(b/62904716) Shapes should be used directly.
66   Shape input_shape = input_array.shape();
67   Shape output_shape = output_array->shape();
68   if (AxesCount(input_axes_order) == 2) {
69     UnextendShape(&input_shape, 2);
70     UnextendShape(&output_shape, 2);
71   }
72   ShuffleArray(input_shape, input_axes_order, output_axes_order, output_shape,
73                input_data.data(), output_data.data());
74   if (input_array.minmax) {
75     output_array->GetOrCreateMinMax() = input_array.GetMinMax();
76   }
77   if (input_array.narrow_range) {
78     output_array->narrow_range = true;
79   }
80 }
81 
Run(Model * model,std::size_t op_index,bool * modified)82 ::tensorflow::Status ResolveReorderAxes::Run(Model* model, std::size_t op_index,
83                                              bool* modified) {
84   *modified = false;
85   auto it = model->operators.begin() + op_index;
86   auto* op = it->get();
87   if (op->type != OperatorType::kReorderAxes) {
88     return ::tensorflow::OkStatus();
89   }
90   auto* reorder_op = static_cast<ReorderAxesOperator*>(op);
91 
92   // Intentionally copies, not references.
93   const std::string input_array_name = reorder_op->inputs[0];
94   const std::string output_array_name = reorder_op->outputs[0];
95 
96   auto& input_array = model->GetArray(input_array_name);
97   auto& output_array = model->GetArray(output_array_name);
98   if (!input_array.buffer) {
99     return ::tensorflow::OkStatus();
100   }
101   // Yield until output dims have been resolved.
102   if (!output_array.has_shape()) {
103     return ::tensorflow::OkStatus();
104   }
105   // Reorder the input array dims and buffer data
106   if (input_array.buffer->type == ArrayDataType::kFloat) {
107     ReorderAxes<float, ArrayDataType::kFloat>(reorder_op->input_axes_order,
108                                               reorder_op->output_axes_order,
109                                               input_array, &output_array);
110   } else if (input_array.buffer->type == ArrayDataType::kUint8) {
111     // TODO(benoitjacob): This path seems unused.
112     // ReorderAxes is only used when importing from
113     // TensorFlow GraphDef, which does not support quantized nodes.
114     ReorderAxes<uint8, ArrayDataType::kUint8>(reorder_op->input_axes_order,
115                                               reorder_op->output_axes_order,
116                                               input_array, &output_array);
117   } else {
118     LOG(FATAL) << "Cannot ReorderAxes unless input buffer is float or uint8.";
119   }
120 
121   AddMessageF("Reordered axes for array %s", input_array_name);
122 
123   DeleteOpAndArrays(model, op);
124   RenameArray(model, output_array_name, input_array_name);
125 
126   *modified = true;
127   return ::tensorflow::OkStatus();
128 }
129 
130 }  // namespace toco
131