• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 <string>
16 #include <vector>
17 
18 #include "tensorflow/core/platform/logging.h"
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 
23 namespace toco {
24 
25 namespace {
26 
TransformsToIdentity(std::vector<int> const & perm1,std::vector<int> const & perm2)27 bool TransformsToIdentity(std::vector<int> const& perm1,
28                           std::vector<int> const& perm2) {
29   if (perm2.size() != perm1.size() || perm1.empty()) {
30     return false;
31   }
32   // perm1 is the order of the indices after first transpose. When perm1 is
33   // reordered according to perm2, if the result is simple increasing sequence
34   // i.e., range(0, perm1.size()), then the two transposes cancel each other.
35   for (size_t i = 0; i < perm1.size(); ++i) {
36     if (perm1[i] < 0 || perm1[i] >= static_cast<int>(perm1.size()) ||
37         perm2[i] < 0 || perm2[i] >= static_cast<int>(perm1.size())) {
38       return false;
39     }
40     if (perm1[perm2[i]] != static_cast<int>(i)) {
41       return false;
42     }
43   }
44   return true;
45 }
46 
ReplaceOpInputsWith(Model * model,const std::string & lookfor,const std::string & replacewith)47 void ReplaceOpInputsWith(Model* model, const std::string& lookfor,
48                          const std::string& replacewith) {
49   for (const auto& op : model->operators) {
50     for (size_t i = 0; i < op->inputs.size(); ++i) {
51       if (op->inputs[i] == lookfor) {
52         op->inputs[i] = replacewith;
53       }
54     }
55   }
56 }
57 
58 }  // namespace
59 
Run(Model * model,std::size_t op_index,bool * modified)60 ::tensorflow::Status RemoveSuccessiveTranspose::Run(Model* model,
61                                                     std::size_t op_index,
62                                                     bool* modified) {
63   *modified = false;
64   auto op = model->operators.begin() + op_index;
65   if (op->get()->type != OperatorType::kTranspose) {
66     return ::tensorflow::OkStatus();
67   }
68 
69   TransposeOperator* t_op = static_cast<TransposeOperator*>(op->get());
70   if (CountOpsWithInput(*model, t_op->outputs[0]) != 1) {
71     return ::tensorflow::OkStatus();
72   }
73   Operator* next = GetOpWithInput(*model, t_op->outputs[0]);
74   if (!next || next->type != OperatorType::kTranspose) {
75     return ::tensorflow::OkStatus();
76   }
77 
78   TransposeOperator* t_next = static_cast<TransposeOperator*>(next);
79   if (!CountOpsWithInput(*model, t_next->outputs[0])) {
80     return ::tensorflow::OkStatus();
81   }
82 
83   if (TransformsToIdentity(t_op->perm, t_next->perm)) {
84     // Find the input tensor that uses the results of transpose t_next, then
85     // make it point to the input of t_op, effectively isolating both the
86     // transposes from the graph.
87     ReplaceOpInputsWith(model, t_next->outputs[0], t_op->inputs[0]);
88     DeleteOpAndArrays(model, t_next);
89     DeleteOpAndArrays(model, t_op);
90     *modified = true;
91   }
92 
93   return ::tensorflow::OkStatus();
94 }
95 
96 }  // namespace toco
97