aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/remove_trivial_quantized_activation_func.cc
blob: 4133815285fdc500e6a112eeedff1bbf6bca66ab (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
/* 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 <limits>
#include <memory>
#include <string>
#include <vector>

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

namespace toco {

namespace {

bool IsTrivialUnfusedActivationFunc(GraphTransformation* transformation,
                                    const Model& model, OperatorType op_type,
                                    const string& input_array_name) {
  double clamp_min;
  double clamp_max;
  switch (op_type) {
    case OperatorType::kRelu:
      clamp_min = 0.0;
      clamp_max = std::numeric_limits<double>::infinity();
      break;
    case OperatorType::kRelu1:
      clamp_min = -1.0;
      clamp_max = 1.0;
      break;
    case OperatorType::kRelu6:
      clamp_min = 0.0;
      clamp_max = 6.0;
      break;
    default:
      return false;
  }

  const auto& input_array = model.GetArray(input_array_name);
  return IsArrayQuantizedRangeSubset(transformation, input_array, clamp_min,
                                     clamp_max);
}

bool IsTrivialFusedActivationFunc(
    GraphTransformation* transformation, const Model& model,
    FusedActivationFunctionType activation_function,
    const string& output_array_name) {
  double clamp_min;
  double clamp_max;
  switch (activation_function) {
    case FusedActivationFunctionType::kNone:
      return false;
    case FusedActivationFunctionType::kRelu:
      clamp_min = 0.0;
      clamp_max = std::numeric_limits<double>::infinity();
      break;
    case FusedActivationFunctionType::kRelu1:
      clamp_min = -1.0;
      clamp_max = 1.0;
      break;
    case FusedActivationFunctionType::kRelu6:
      clamp_min = 0.0;
      clamp_max = 6.0;
      break;
    default:
      LOG(FATAL) << "Unsupported fused activation type: "
                 << static_cast<int>(activation_function);
      return false;
  }

  const auto& output_array = model.GetArray(output_array_name);
  return IsArrayQuantizedRangeSubset(transformation, output_array, clamp_min,
                                     clamp_max);
}

}  // namespace

// Attempts to remove both fused and unfused activation functions if the
// quantization params indicate that the representable values fall inside the
// activation range.
::tensorflow::Status RemoveTrivialQuantizedActivationFunc::Run(
    Model* model, std::size_t op_index, bool* modified) {
  *modified = false;
  const auto it = model->operators.begin() + op_index;
  auto* op = it->get();
  if (op->inputs.empty()) {
    return ::tensorflow::Status::OK();
  }

  if (IsTrivialUnfusedActivationFunc(this, *model, op->type, op->inputs[0])) {
    AddMessageF(
        "Removing trivial unfused activation function %s because the input "
        "minmax imply at least as tight a clamp anyway.",
        LogName(*op));
    *modified = RemoveTrivialPassthroughOp(this, model, op_index);
    return ::tensorflow::Status::OK();
  }
  if (IsTrivialFusedActivationFunc(this, *model, op->fused_activation_function,
                                   op->outputs[0])) {
    op->fused_activation_function = FusedActivationFunctionType::kNone;
    AddMessageF(
        "Removing trivial quantized activation function on %s "
        "because the output quantization parameters imply at least as tight "
        "a clamp anyway.",
        LogName(*op));
    *modified = true;
    return ::tensorflow::Status::OK();
  }
  return ::tensorflow::Status::OK();
}

}  // namespace toco