aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/graph_transformations/remove_trivial_fake_quant.cc
blob: 5448a816bc43ac803cd2022c6659584683907280 (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
/* 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_map>
#include <vector>

#include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
#include "tensorflow/contrib/lite/toco/graph_transformations/remove_trivial_passthrough.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 IsFakeQuantTrivial(GraphTransformation* transformation, const Model& model,
                        const FakeQuantOperator& fakequant_op) {
  CHECK(fakequant_op.type == OperatorType::kFakeQuant);

  if (!fakequant_op.minmax) {
    // Require ReadFakeQuantMinMax to have run.
    return false;
  }

  // FakeQuants are trivial if they are taking input from another identical
  // FakeQuant op.
  auto* producing_op = GetOpWithOutput(model, fakequant_op.inputs[0]);
  if (!producing_op || producing_op->type != OperatorType::kFakeQuant) {
    return false;
  }
  const auto& producing_fakequant_op =
      *static_cast<FakeQuantOperator*>(producing_op);
  if (!producing_fakequant_op.minmax) {
    // Require ReadFakeQuantMinMax to have run.
    return false;
  }

  if (*fakequant_op.minmax == *producing_fakequant_op.minmax &&
      fakequant_op.num_bits == producing_fakequant_op.num_bits) {
    transformation->AddMessageF(
        "%s is trivial because it is preceded by an identical FakeQuant %s",
        LogName(fakequant_op), LogName(producing_fakequant_op));
    return true;
  }

  return false;
}

}  // namespace

// Removes FakeQuant ops that are trivial (have no effect, are redundant, etc).
::tensorflow::Status RemoveTrivialFakeQuant::Run(Model* model,
                                                 std::size_t op_index,
                                                 bool* modified) {
  *modified = false;
  const auto op_it = model->operators.begin() + op_index;
  auto* op = op_it->get();
  if (op->type != OperatorType::kFakeQuant) {
    return ::tensorflow::Status::OK();
  }
  auto* fakequant_op = static_cast<FakeQuantOperator*>(op);

  if (!IsFakeQuantTrivial(this, *model, *fakequant_op)) {
    AddMessageF("%s is not trivial", LogName(*fakequant_op));
    return ::tensorflow::Status::OK();
  }

  AddMessageF("Removing trivial %s", LogName(*fakequant_op));

  CHECK_EQ(fakequant_op->inputs.size(), 1);
  *modified = RemoveTrivialPassthroughOp(this, model, op_index);
  return ::tensorflow::Status::OK();
}

}  // namespace toco