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