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 <memory>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
21#include "tensorflow/contrib/lite/toco/model.h"
22#include "tensorflow/contrib/lite/toco/tooling_util.h"
23#include "tensorflow/core/platform/logging.h"
24
25namespace toco {
26
27namespace {
28
29std::vector<std::unique_ptr<Operator>>::iterator FindOperator(
30    Model* model, const Operator* op) {
31  auto it = model->operators.begin();
32  for (; it != model->operators.end(); ++it) {
33    if (it->get() == op) {
34      break;
35    }
36  }
37  return it;
38}
39
40bool CheckArrayIsScalarFloat(Model* model, const std::string& name, float val) {
41  const auto& op_array = model->GetArray(name);
42  if (!op_array.buffer || op_array.buffer->type != ArrayDataType::kFloat ||
43      RequiredBufferSizeForShape(op_array.shape()) != 1) {
44    return false;
45  }
46  const auto& op_data = op_array.GetBuffer<ArrayDataType::kFloat>().data;
47  return op_data[0] == val;
48}
49
50// Returns index of scalar input when there is exactly one scalar, -1 otherwise
51int GetSingleScalarInputIndexOfBinaryOp(Model* model, const Operator* op,
52                                        float val) {
53  bool input0_is_scalar = CheckArrayIsScalarFloat(model, op->inputs[0], val);
54  bool input1_is_scalar = CheckArrayIsScalarFloat(model, op->inputs[1], val);
55  return input0_is_scalar == input1_is_scalar ? -1 : input0_is_scalar ? 0 : 1;
56}
57}  // namespace
58
59bool IdentifyRelu1::Run(Model* model, std::size_t op_index) {
60  // Follow sequences of min+max and max+min. First get the leading op.
61  const auto op_it = model->operators.begin() + op_index;
62  const auto* op_0 = op_it->get();
63  if (op_0->type != OperatorType::kTensorFlowMinimum &&
64      op_0->type != OperatorType::kTensorFlowMaximum) {
65    return false;
66  }
67
68  // Get the paired op and ensure it's the counter to the first.
69  const auto* op_1 = GetOpWithInput(*model, op_0->outputs[0]);
70  if (!op_1 ||
71      (op_1->type != OperatorType::kTensorFlowMinimum &&
72       op_1->type != OperatorType::kTensorFlowMaximum) ||
73      op_0->type == op_1->type) {
74    return false;
75  }
76
77  const auto* min_op =
78      op_0->type == OperatorType::kTensorFlowMinimum ? op_0 : op_1;
79  const auto* max_op =
80      op_0->type == OperatorType::kTensorFlowMaximum ? op_0 : op_1;
81
82  CHECK_EQ(min_op->inputs.size(), 2);
83  CHECK_EQ(max_op->inputs.size(), 2);
84  if (min_op->outputs.size() != 1 || max_op->outputs.size() != 1) {
85    return false;
86  }
87
88  // Get the original input to the min+max pair.
89  int min_scalar_input_index =
90      GetSingleScalarInputIndexOfBinaryOp(model, min_op, 1.0f);
91  int max_scalar_input_index =
92      GetSingleScalarInputIndexOfBinaryOp(model, max_op, -1.0f);
93  if (min_scalar_input_index == -1 || max_scalar_input_index == -1) {
94    return false;
95  }
96  int op_0_scalar_input_index =
97      op_0 == min_op ? min_scalar_input_index : max_scalar_input_index;
98
99  // Create and emplace Relu1 node.
100  auto* relu1_op = new Relu1Operator;
101  relu1_op->inputs = {op_0->inputs[!op_0_scalar_input_index]};
102  relu1_op->outputs = op_1->outputs;
103  model->operators.emplace(op_it, relu1_op);
104
105  AddMessageF("Creating %s replacing equivalent subgraph", LogName(*relu1_op));
106
107  // Erase op scalar inputs & operators. Note that we preserve the non-scalar
108  // input to the first op as that's been redirected to the relu1_op.
109  DeleteArrayIfUsedOnce(op_0->inputs[op_0_scalar_input_index], model);
110  DeleteArrayIfUsedOnce(op_1->inputs[0], model);
111  DeleteArrayIfUsedOnce(op_1->inputs[1], model);
112  model->operators.erase(FindOperator(model, op_0));
113  model->operators.erase(FindOperator(model, op_1));
114
115  return true;
116}
117
118}  // namespace toco
119