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/runtime/types.h"
     23 #include "tensorflow/contrib/lite/toco/tooling_util.h"
     24 #include "tensorflow/core/platform/logging.h"
     25 
     26 namespace toco {
     27 
     28 bool FuseActivationFunctions::Run(Model* model, std::size_t op_index) {
     29   const auto ac_it = model->operators.begin() + op_index;
     30   const auto* ac_op = ac_it->get();
     31 
     32   if (ac_op->type != OperatorType::kRelu6 &&
     33       ac_op->type != OperatorType::kRelu1 &&
     34       ac_op->type != OperatorType::kRelu) {
     35     return false;
     36   }
     37 
     38   // Find the op producing the array passed to this activation function
     39   Operator* op = GetOpWithOutput(*model, ac_op->inputs[0]);
     40 
     41   if (!op) return false;
     42 
     43   if (CountTrueOutputs(*model, *op) > 1) {
     44     AddMessageF(
     45         "Not fusing activation function into %s because it has more than one "
     46         " consumed output",
     47         LogName(*op));
     48     return false;
     49   }
     50 
     51   CHECK_EQ(op->outputs[0], ac_op->inputs[0]);
     52 
     53   int count_ops_consuming_output = CountOpsWithInput(*model, ac_op->inputs[0]);
     54   DCHECK_GE(count_ops_consuming_output, 1);
     55   if (count_ops_consuming_output > 1) {
     56     AddMessageF(
     57         "Not fusing activation function into %s because it is consumed by more "
     58         "than 1 other operator",
     59         LogName(*op));
     60     return false;
     61   }
     62 
     63   if (op->fused_activation_function != FusedActivationFunctionType::kNone) {
     64     AddMessageF(
     65         "Not fusing activation function into %s because it already has a fused "
     66         "activation function",
     67         LogName(*op));
     68     return false;
     69   }
     70 
     71   if (!OperatorSupportsFusedActivation(op->type)) {
     72     AddMessageF(
     73         "Not fusing activation function because the %s op doesn't support it",
     74         LogName(*op));
     75     return false;
     76   }
     77 
     78   AddMessageF("Fusing activation function %s into the preceding %s",
     79               LogName(*ac_op), LogName(*op));
     80   if (ac_op->type == OperatorType::kRelu6) {
     81     op->fused_activation_function = FusedActivationFunctionType::kRelu6;
     82   } else if (ac_op->type == OperatorType::kRelu1) {
     83     op->fused_activation_function = FusedActivationFunctionType::kRelu1;
     84   } else if (ac_op->type == OperatorType::kRelu) {
     85     op->fused_activation_function = FusedActivationFunctionType::kRelu;
     86   } else {
     87     LOG(FATAL) << "Unhandled activation function type";
     88   }
     89   model->EraseArray(ac_op->inputs[0]);
     90   op->outputs[0] = ac_op->outputs[0];
     91   model->operators.erase(ac_it);
     92   return true;
     93 }
     94 
     95 }  // namespace toco
     96