1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include <algorithm>
16#include <memory>
17#include <string>
18#include <unordered_map>
19#include <vector>
20
21#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
22#include "tensorflow/contrib/lite/toco/model.h"
23#include "tensorflow/contrib/lite/toco/tooling_util.h"
24#include "tensorflow/core/platform/logging.h"
25
26namespace toco {
27
28// Reorder the elements of an input_array according to the input_axes_order and
29// output_axes_order. Then adjust the shapes of the input and output arrays
30// accordingly. Note that input_array must have a buffer (that is, it is a
31// constant array).
32template <typename T, ArrayDataType DataType>
33void ReorderAxes(AxesOrder input_axes_order, AxesOrder output_axes_order,
34                 Array* input_array, Array* output_array) {
35  CHECK(input_array->buffer->type == DataType);
36  CHECK(!output_array->buffer);
37  auto& input_data = input_array->GetMutableBuffer<DataType>().data;
38  std::vector<T> reordered_data;
39  reordered_data.resize(RequiredBufferSizeForShape(output_array->shape()));
40  // TODO(b/62904716) Shapes should be used directly.
41  Shape input_shape = input_array->shape();
42  Shape output_shape = output_array->shape();
43  if (AxesCount(input_axes_order) == 2) {
44    UnextendShape(&input_shape, 2);
45    UnextendShape(&output_shape, 2);
46  }
47  ShuffleArray(input_shape, input_axes_order, output_axes_order, output_shape,
48               input_data.data(), reordered_data.data());
49  input_data = reordered_data;
50  input_array->copy_shape(output_array->shape());
51}
52
53bool ResolveReorderAxes::Run(Model* model, std::size_t op_index) {
54  auto reorder_it = model->operators.begin() + op_index;
55  auto* reorder_op = static_cast<ReorderAxesOperator*>(reorder_it->get());
56  if (reorder_op->type != OperatorType::kReorderAxes) {
57    return false;
58  }
59  const auto& input_array_name = reorder_op->inputs[0];
60  const auto& output_array_name = reorder_op->outputs[0];
61  auto& input_array = model->GetArray(input_array_name);
62  auto& output_array = model->GetArray(output_array_name);
63  if (!input_array.buffer) {
64    return false;
65  }
66  // Yield until output dims have been resolved.
67  if (!output_array.has_shape()) {
68    return false;
69  }
70  // Reorder the input array dims and buffer data
71  if (input_array.buffer->type == ArrayDataType::kFloat) {
72    ReorderAxes<float, ArrayDataType::kFloat>(reorder_op->input_axes_order,
73                                              reorder_op->output_axes_order,
74                                              &input_array, &output_array);
75  } else if (input_array.buffer->type == ArrayDataType::kInt32) {
76    ReorderAxes<uint8, ArrayDataType::kUint8>(reorder_op->input_axes_order,
77                                              reorder_op->output_axes_order,
78                                              &input_array, &output_array);
79  } else {
80    LOG(FATAL) << "Cannot ReorderAxes unless input buffer is float or uint8.";
81  }
82
83  input_array.copy_shape(output_array.shape());
84
85  // Update the edges of the graph to point to the input array
86  for (const auto& other_op : model->operators) {
87    for (auto& input : other_op->inputs) {
88      if (input == output_array_name) {
89        input = input_array_name;
90      }
91    }
92  }
93
94  AddMessageF("Reordered axes for array %s", input_array_name);
95
96  // Remove the op and output array.
97  model->EraseArray(output_array_name);
98  model->operators.erase(reorder_it);
99  return true;
100}
101
102}  // namespace toco
103