Home | History | Annotate | Download | only in graph_transformations
      1 /* Copyright 2017 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/contrib/lite/toco/graph_transformations/graph_transformations.h"
     21 #include "tensorflow/contrib/lite/toco/model.h"
     22 #include "tensorflow/contrib/lite/toco/model_flags.pb.h"
     23 #include "tensorflow/contrib/lite/toco/tooling_util.h"
     24 #include "tensorflow/core/platform/logging.h"
     25 
     26 namespace toco {
     27 
     28 // This inserts an operator whose output is a float array (name:
     29 // flags.input_array()).  It has to wait for any existing operators that
     30 // generate this output to be removed by graph transformations.  Note that there
     31 // may be more than one operator that takes the input_array as their input, and
     32 // that some of these may be removed by graph transformations.
     33 bool AddDequantizeOperatorToInput(const string& input_name, const Operator* op,
     34                                   GraphTransformation* transformation,
     35                                   Model* model) {
     36   // An operator with the required output may be a dequantize operator already
     37   // created.  Alternatively it may be an operator that needs to be removed
     38   // because it is unused, in which case we wait for RemoveUnusedOp to do its
     39   // work.
     40   if (GetOpWithOutput(*model, input_name)) {
     41     return false;
     42   }
     43 
     44   // We only apply for the first operator if there is more than one.  This is
     45   // not strictly necessary for ordering correctness, since we insert the
     46   // dequant operator at the beginning of the op sequence, but it makes the
     47   // insertion more predictable (eg forward vs backwards operator sweep).
     48   if (CountOpsWithInput(*model, input_name) > 1) {
     49     if (op != GetFirstOpWithInput(*model, input_name)) {
     50       return false;
     51     }
     52   }
     53 
     54   auto& input_array = model->GetArray(input_name);
     55   if (input_array.data_type != ArrayDataType::kFloat) {
     56     return false;
     57   }
     58 
     59   if (input_array.final_data_type == input_array.data_type ||
     60       input_array.final_data_type == ArrayDataType::kNone) {
     61     return false;
     62   }
     63 
     64   const auto& dequantized_input_name =
     65       AvailableArrayName(*model, input_name + "_dequantized");
     66   for (auto& other_op : model->operators) {
     67     for (string& other_op_input : other_op->inputs) {
     68       if (other_op_input == input_name) {
     69         other_op_input = dequantized_input_name;
     70       }
     71     }
     72   }
     73 
     74   auto& dequantized_input_array =
     75       model->GetOrCreateArray(dequantized_input_name);
     76   auto* image_input_op = new DequantizeOperator;
     77   image_input_op->inputs = {input_name};
     78   image_input_op->outputs = {dequantized_input_name};
     79   model->operators.emplace(model->operators.begin(), image_input_op);
     80 
     81   CHECK(input_array.final_data_type == ArrayDataType::kUint8);
     82   input_array.data_type = ArrayDataType::kUint8;
     83   dequantized_input_array.data_type = ArrayDataType::kFloat;
     84   const auto& input_minmax = input_array.GetMinMax();
     85   auto& dequantized_input_minmax = dequantized_input_array.GetOrCreateMinMax();
     86   dequantized_input_minmax = input_minmax;
     87   auto& input_qparams = input_array.GetOrCreateQuantizationParams();
     88   GetQuantizationParamsFromMinMax<ArrayDataType::kUint8>(
     89       model->flags, input_minmax, &input_qparams);
     90 
     91   transformation->AddMessageF(
     92       "Created %s"
     93       " to handle quantized input image data, taking over existing"
     94       " mean_value and std_value flags. Cleared those flags.",
     95       LogName(*image_input_op));
     96 
     97   return true;
     98 }
     99 
    100 bool MakeInitialDequantizeOperator::Run(Model* model, std::size_t op_index) {
    101   // This is effectively a transformation applied to edges.  We iterate over the
    102   // specified node (op) and proceed for input edges.
    103   const auto it = model->operators.begin() + op_index;
    104   const auto* op = it->get();
    105   bool change_made = false;
    106   for (auto& input : op->inputs) {
    107     for (auto& input_array : *model->flags.mutable_input_arrays()) {
    108       if (input_array.name() == input) {
    109         if (AddDequantizeOperatorToInput(input_array.name(), op, this, model)) {
    110           change_made = true;
    111           input_array.clear_mean_value();
    112           input_array.clear_std_value();
    113         }
    114       }
    115     }
    116   }
    117   return change_made;
    118 }
    119 
    120 }  // namespace toco
    121