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 <vector>
16
17#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
18#include "tensorflow/contrib/lite/toco/model.h"
19#include "tensorflow/contrib/lite/toco/tooling_util.h"
20#include "tensorflow/core/platform/logging.h"
21
22namespace toco {
23
24namespace {
25
26template <ArrayDataType Type>
27void Stack(Model* model, StackOperator const& op) {
28  auto& output_array = model->GetArray(op.outputs[0]);
29  CHECK(output_array.data_type == Type);
30
31  // Create a buffer for the output array
32  std::vector<DataType<Type>>& output_data =
33      output_array.GetMutableBuffer<Type>().data;
34  output_data.resize(RequiredBufferSizeForShape(output_array.shape()));
35
36  // Stack inputs into buffer
37  CHECK_EQ(op.axis, 0) << "Stacking only supported along first axis";
38  int dst_offset = 0;
39  for (int i = 0; i < op.inputs.size(); i++) {
40    // Append array data to output for each input array
41    const auto& input_array = model->GetArray(op.inputs[i]);
42    int input_size = RequiredBufferSizeForShape(input_array.shape());
43    memcpy(&output_data[dst_offset], &input_array.GetBuffer<Type>().data[0],
44           input_size * sizeof(Type));
45    dst_offset += input_size;
46  }
47  CHECK_EQ(dst_offset, output_data.size());
48}
49
50}  // namespace
51
52bool ResolveConstantStack::Run(Model* model, std::size_t op_index) {
53  auto it = model->operators.begin() + op_index;
54  const auto* base_op = it->get();
55  if (base_op->type != OperatorType::kStack) {
56    return false;
57  }
58  const auto* op = static_cast<const StackOperator*>(base_op);
59
60  CHECK_GE(op->inputs.size(), 1);
61  CHECK_EQ(op->outputs.size(), 1);
62  auto& output_array = model->GetArray(op->outputs[0]);
63  if (output_array.data_type == ArrayDataType::kNone) {
64    // Yield until the output type has been set by PropagateArrayDataTypes
65    return false;
66  }
67
68  if (!output_array.has_shape()) {
69    // Yield until the output shape has been set by PropagateFixedShapes
70    return false;
71  }
72
73  for (const auto& input : op->inputs) {
74    if (!IsConstantParameterArray(*model, input)) {
75      // Yield if any input is mutable
76      return false;
77    }
78  }
79
80  CHECK(!output_array.buffer);
81  switch (output_array.data_type) {
82    case ArrayDataType::kFloat:
83      Stack<ArrayDataType::kFloat>(model, *op);
84      break;
85    case ArrayDataType::kUint8:
86      Stack<ArrayDataType::kUint8>(model, *op);
87      break;
88    case ArrayDataType::kInt32:
89      Stack<ArrayDataType::kInt32>(model, *op);
90      break;
91    case ArrayDataType::kInt64:
92      Stack<ArrayDataType::kInt64>(model, *op);
93      break;
94    default:
95      LOG(FATAL) << "Unsupported data type given to Stack op with output \""
96                 << op->outputs[0] << "\"";
97      break;
98  }
99
100  // Erase input arrays if no longer used
101  for (const auto& input : op->inputs) {
102    if (IsDiscardableArray(*model, input) &&
103        CountOpsWithInput(*model, input) == 1) {
104      model->EraseArray(input);
105    }
106  }
107
108  // Erase the operator
109  model->operators.erase(it);
110  return true;
111}
112
113}  // namespace toco
114