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 <vector>
     18 
     19 #include "tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.h"
     20 #include "tensorflow/contrib/lite/toco/model.h"
     21 #include "tensorflow/contrib/lite/toco/tooling_util.h"
     22 #include "tensorflow/core/platform/logging.h"
     23 
     24 namespace toco {
     25 
     26 bool RemoveTensorFlowAssert::Run(Model* model, std::size_t op_index) {
     27   const auto assert_it = model->operators.begin() + op_index;
     28   const auto* assert_op = assert_it->get();
     29   if (assert_op->type != OperatorType::kTensorFlowAssert) {
     30     return false;
     31   }
     32 
     33   bool changed = false;
     34   // Remove any other node's dependency on this assert node
     35   for (const auto& op : model->operators) {
     36     auto it = op->inputs.begin();
     37     while (it != op->inputs.end()) {
     38       if (*it == assert_op->outputs[0]) {
     39         op->inputs.erase(it);
     40         changed = true;
     41       } else {
     42         ++it;
     43       }
     44     }
     45   }
     46   CHECK(!CountOpsWithInput(*model, assert_op->outputs[0]));
     47 
     48   if (changed) {
     49     AddMessageF(
     50         "Prepared for the removal of %s by removing any other op's dependency "
     51         "on it",
     52         LogName(*assert_op));
     53   }
     54 
     55   // That's it. We can stop here, no need to duplicate the work that
     56   // RemoveUnusedOp will do removing this now-unused node.
     57   return changed;
     58 }
     59 
     60 }  // namespace toco
     61