aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/reorder_elementwise_unary.cc
blob: 3c8d41108918ad6b4232fcc0168c9e0cd65100e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <iterator>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>

#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
#include "tensorflow/contrib/lite/toco/model.h"
#include "tensorflow/contrib/lite/toco/tooling_util.h"
#include "tensorflow/core/platform/logging.h"

namespace toco {

namespace {

bool IsElementwiseOperator(OperatorType optype) {
  switch (optype) {
    case OperatorType::kCast:
    case OperatorType::kExp:
    case OperatorType::kFloor:
    case OperatorType::kNeg:
    case OperatorType::kRelu:
    case OperatorType::kRelu1:
    case OperatorType::kRelu6:
    case OperatorType::kTanh:
    case OperatorType::kSqrt:
    case OperatorType::kSquare:
      return true;
    default:
      return false;
  }
}

bool IsMoveOperator(OperatorType optype) {
  switch (optype) {
    case OperatorType::kDepthToSpace:
    case OperatorType::kExpandDims:
    case OperatorType::kSpaceToDepth:
    case OperatorType::kSqueeze:
    case OperatorType::kReshape:
    case OperatorType::kTranspose:
      return true;
    default:
      return false;
  }
}

}  // namespace

// Swap elementwise operators such that all value operators occur before all
// element move operators, e.g. negation then transpose.
::tensorflow::Status ReorderElementwiseUnary::Run(Model* model,
                                                  std::size_t op_index,
                                                  bool* modified) {
  *modified = false;
  const auto element_op_it = model->operators.begin() + op_index;
  std::unique_ptr<Operator>& element_op = *element_op_it;
  if (!IsElementwiseOperator(element_op->type)) {
    return ::tensorflow::Status::OK();
  }

  const string intermediate_name = element_op->inputs[0];
  auto it = FindOpWithOutput(*model, intermediate_name);
  if (it == model->operators.end()) {
    AddMessageF("No preceding operator");
    return ::tensorflow::Status::OK();
  }

  std::unique_ptr<Operator>& move_op = *it;
  if (!IsMoveOperator(move_op->type)) {
    AddMessageF("Preceding operator is not a move operator");
    return ::tensorflow::Status::OK();
  }

  if (CountOpsWithInput(*model, intermediate_name) != 1) {
    AddMessageF("Input %s used elsewhere", intermediate_name);
    return ::tensorflow::Status::OK();
  }

  // Check that the intermediate is discardable.
  if (!IsDiscardableArray(*model, intermediate_name)) {
    AddMessageF(
        "Cannot swap elementwise as it would invalidate %s which is "
        "an output array.",
        intermediate_name);
    return ::tensorflow::Status::OK();
  }

  // op->inputs may change so we need to keep a value by copy.
  const string input_name = move_op->inputs[0];
  const string output_name = element_op->outputs[0];

  AddMessageF("Swapping around operators with %s and %s", LogName(*element_op),
              LogName(*move_op));

  // If the output array is an exit node for the graph then we need to retain
  // the name as an output node. This makes the naming scheme a little confusing
  // but is required in this rare case.
  if (!IsDiscardableArray(*model, output_name)) {
    // The output name of the sequence needs to stay static, so create a new
    // array new use for the intermediate.
    const auto new_intermediate_name =
        AvailableArrayName(*model, element_op->outputs[0] + "_reorder");
    AddMessageF("Adding new array %s to preserve output array name %s",
                new_intermediate_name, output_name);

    element_op->inputs[0] = input_name;
    element_op->outputs[0] = new_intermediate_name;
    model->EraseArray(intermediate_name);
    move_op->inputs[0] = new_intermediate_name;
    move_op->outputs[0] = output_name;
  } else {
    // The intermediate array is now the output array.
    for (int i = 0; i < model->operators.size(); i++) {
      Operator* consumer = model->operators[i].get();
      for (int j = 0; j < consumer->inputs.size(); j++) {
        if (consumer->inputs[j] == output_name) {
          consumer->inputs[j] = intermediate_name;
        }
      }
    }

    element_op->inputs[0] = input_name;
    move_op->inputs[0] = output_name;
  }

  // Reset both arrays as shape, type, min/max, etc can all change because of
  // the position swap.
  model->EraseArray(element_op->outputs[0]);
  model->EraseArray(move_op->outputs[0]);

  // Reconstruct.
  model->GetOrCreateArray(element_op->outputs[0]);
  model->GetOrCreateArray(move_op->outputs[0]);

  // Swap the order of the operators.
  element_op.swap(move_op);

  *modified = true;
  return ::tensorflow::Status::OK();
}

}  // namespace toco