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
27bool ResolveConstantFakeQuant::Run(Model* model, std::size_t op_index) {
28  const auto fakequant_it = model->operators.begin() + op_index;
29  const auto* fakequant_base_op = fakequant_it->get();
30  if (fakequant_base_op->type != OperatorType::kFakeQuant) {
31    return false;
32  }
33
34  const auto* fakequant_op =
35      static_cast<const FakeQuantOperator*>(fakequant_base_op);
36
37  // Yield until the fakequant MinMax has been resolved.
38  if (!fakequant_op->minmax) {
39    return false;
40  }
41
42  // This transformation only applies when the input array is constant.
43  if (!IsConstantParameterArray(*model, fakequant_op->inputs[0])) {
44    return false;
45  }
46
47  const auto& input_array = model->GetArray(fakequant_op->inputs[0]);
48  auto& output_array = model->GetArray(fakequant_op->outputs[0]);
49  CHECK(input_array.data_type == ArrayDataType::kFloat);
50  output_array.data_type = ArrayDataType::kFloat;
51  CHECK(!output_array.buffer);
52  const auto& input_buffer = input_array.GetBuffer<ArrayDataType::kFloat>();
53  output_array.GetOrCreateMinMax() = *fakequant_op->minmax;
54  auto& output_buffer = output_array.GetMutableBuffer<ArrayDataType::kFloat>();
55  const int size = input_buffer.data.size();
56  output_buffer.data.resize(size);
57  QuantizationParams qparams;
58  GetQuantizationParamsFromMinMax<ArrayDataType::kUint8>(
59      model->flags, *fakequant_op->minmax, &qparams);
60  for (int i = 0; i < size; i++) {
61    const double src_val = input_buffer.data[i];
62    const double unclamped_quantized_val =
63        std::round(qparams.zero_point + src_val / qparams.scale);
64    const double quantized_val =
65        std::min(255., std::max(0., unclamped_quantized_val));
66    const double dst_val = qparams.scale * (quantized_val - qparams.zero_point);
67    output_buffer.data[i] = dst_val;
68  }
69  if (CountOpsWithInput(*model, fakequant_op->inputs[0]) == 1) {
70    model->EraseArray(fakequant_op->inputs[0]);
71  }
72  model->operators.erase(fakequant_it);
73
74  return true;
75}
76
77}  // namespace toco
78