1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <memory>
16 #include <string>
17 #include <unordered_map>
18 #include <vector>
19 
20 #include "tensorflow/lite/toco/graph_transformations/graph_transformations.h"
21 #include "tensorflow/lite/toco/graph_transformations/quantization_util.h"
22 #include "tensorflow/lite/toco/model.h"
23 #include "tensorflow/lite/toco/tooling_util.h"
24 #include "tensorflow/core/platform/logging.h"
25 
26 namespace toco {
27 
28 namespace {
29 
SupportsMinMax(const Array & array)30 bool SupportsMinMax(const Array& array) {
31   return array.data_type == ArrayDataType::kFloat;
32 }
33 
34 }  // namespace
35 
36 // Propagates default min/max values to any operator input/output array that
37 // is missing them.
38 //
39 // When provided a set of min/max values for uint8 arrays this will rescale
40 // the values for other data types as required and preserving the floating point
41 // range within the new type.
Run(Model * model,std::size_t op_index,bool * modified)42 ::tensorflow::Status PropagateDefaultMinMax::Run(Model* model,
43                                                  std::size_t op_index,
44                                                  bool* modified) {
45   *modified = false;
46   const auto it = model->operators.begin() + op_index;
47   const auto* op = it->get();
48 
49   bool did_change = false;
50 
51   for (const auto& input : op->inputs) {
52     auto& input_array = model->GetArray(input);
53     if (!input_array.minmax && !input_array.buffer &&
54         SupportsMinMax(input_array)) {
55       did_change |= SetArrayMinMax(input, &input_array);
56     }
57   }
58 
59   for (const auto& output : op->outputs) {
60     auto& output_array = model->GetArray(output);
61     if (!output_array.minmax && !output_array.buffer &&
62         SupportsMinMax(output_array)) {
63       did_change |= SetArrayMinMax(output, &output_array);
64     }
65   }
66 
67   *modified = did_change;
68   return ::tensorflow::OkStatus();
69 }
70 
71 // Sets the min/max on the given array, adjusting the reference_minmax for the
72 // final data type of the array if it is already specified.
SetArrayMinMax(const std::string & array_name,Array * array)73 bool PropagateDefaultMinMax::SetArrayMinMax(const std::string& array_name,
74                                             Array* array) {
75   CHECK(!array->minmax);
76 
77   ArrayDataType quantized_data_type =
78       GetQuantizedDataType(*array, ArrayDataType::kUint8);
79   for (const auto& type_range : type_ranges_) {
80     if (type_range.first == quantized_data_type) {
81       array->GetOrCreateMinMax() = type_range.second;
82       break;
83     }
84   }
85   if (!array->minmax) {
86     AddMessageF(
87         "No defaults specified for quantized data type %s of array %s, "
88         "skipping",
89         ArrayDataTypeName(quantized_data_type), array_name);
90     return false;
91   }
92 
93   AddMessageF("Adding default minmax %g,%g to array %s when quantized as %s",
94               array->GetMinMax().min, array->GetMinMax().max, array_name,
95               ArrayDataTypeName(quantized_data_type));
96 
97   return true;
98 }
99 
100 }  // namespace toco
101