aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/fuse_activation_functions.cc
blob: c5ce3fcd95eb0aaf63dcc7f43b96d8a13ed93929 (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
/* Copyright 2017 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 <memory>
#include <string>
#include <unordered_map>
#include <vector>

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

namespace toco {

bool FuseActivationFunctions::Run(Model* model, std::size_t op_index) {
  const auto ac_it = model->operators.begin() + op_index;
  const auto* ac_op = ac_it->get();

  if (ac_op->type != OperatorType::kRelu6 &&
      ac_op->type != OperatorType::kRelu1 &&
      ac_op->type != OperatorType::kRelu) {
    return false;
  }

  // Find the op producing the array passed to this activation function
  Operator* op = GetOpWithOutput(*model, ac_op->inputs[0]);

  if (!op) return false;

  if (CountTrueOutputs(*model, *op) > 1) {
    AddMessageF(
        "Not fusing activation function %s into %s because it has more than "
        "one  consumed output",
        LogName(*ac_op), LogName(*op));
    return false;
  }

  CHECK_EQ(op->outputs[0], ac_op->inputs[0]);

  int count_ops_consuming_output = CountOpsWithInput(*model, ac_op->inputs[0]);
  DCHECK_GE(count_ops_consuming_output, 1);
  if (count_ops_consuming_output > 1) {
    AddMessageF(
        "Not fusing activation function into %s because it is consumed by more "
        "than 1 other operator",
        LogName(*ac_op), LogName(*op));
    return false;
  }

  if (!IsDiscardableArray(*model, op->outputs[0])) {
    AddMessageF(
        "Not fusing activation function %s into %s because output %s it is not "
        "discardable",
        LogName(*ac_op), LogName(*op), op->outputs[0]);
    return false;
  }

  if (op->fused_activation_function != FusedActivationFunctionType::kNone) {
    AddMessageF(
        "Not fusing activation function %s into %s because it already has a "
        "fused activation function",
        LogName(*ac_op), LogName(*op));
    return false;
  }

  if (!OperatorSupportsFusedActivation(op->type)) {
    AddMessageF(
        "Not fusing activation function %s because the %s op doesn't support "
        "it",
        LogName(*ac_op), LogName(*op));
    return false;
  }

  AddMessageF("Fusing activation function %s into the preceding %s",
              LogName(*ac_op), LogName(*op));
  if (ac_op->type == OperatorType::kRelu6) {
    op->fused_activation_function = FusedActivationFunctionType::kRelu6;
  } else if (ac_op->type == OperatorType::kRelu1) {
    op->fused_activation_function = FusedActivationFunctionType::kRelu1;
  } else if (ac_op->type == OperatorType::kRelu) {
    op->fused_activation_function = FusedActivationFunctionType::kRelu;
  } else {
    LOG(FATAL) << "Unhandled activation function type";
  }
  model->EraseArray(ac_op->inputs[0]);
  op->outputs[0] = ac_op->outputs[0];
  model->operators.erase(ac_it);
  return true;
}

}  // namespace toco