Home | History | Annotate | Download | only in utils
      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 
     16 #include "tensorflow/core/grappler/utils/topological_sort.h"
     17 #include <deque>
     18 #include <unordered_map>
     19 #include "tensorflow/core/framework/node_def.pb.h"
     20 #include "tensorflow/core/grappler/op_types.h"
     21 #include "tensorflow/core/grappler/utils.h"
     22 #include "tensorflow/core/lib/core/status.h"
     23 
     24 namespace tensorflow {
     25 namespace grappler {
     26 
     27 // Kahn's algorithm is implemented.
     28 // For details, see https://en.wikipedia.org/wiki/Topological_sorting
     29 Status TopologicalSort(GraphDef* graph) {
     30   SimpleGraphView graph_view;
     31   TF_RETURN_IF_ERROR(graph_view.Initialize(*graph));
     32 
     33   std::vector<int> ready_nodes;
     34   ready_nodes.reserve(graph_view.num_nodes());
     35 
     36   int front = 0;
     37   int back = 0;
     38   std::vector<int> num_ready_inputs(graph_view.num_nodes(), 0);
     39   for (int i = 0; i < graph_view.num_nodes(); i++) {
     40     if (graph_view.inputs(i).empty()) {
     41       ready_nodes.push_back(i);
     42       back++;
     43     }
     44     if (IsMerge(graph->node(i))) {
     45       for (int input : graph_view.inputs(i)) {
     46         if (IsNextIteration(graph->node(input))) {
     47           num_ready_inputs[i]++;
     48         }
     49       }
     50     }
     51   }
     52 
     53   while (front != back) {
     54     int ready_node = ready_nodes[front];
     55     for (int fanout : graph_view.outputs(ready_node)) {
     56       ++num_ready_inputs[fanout];
     57       if (num_ready_inputs[fanout] == graph_view.inputs(fanout).size()) {
     58         ready_nodes.push_back(fanout);
     59         ++back;
     60       }
     61     }
     62     ++front;
     63   }
     64 
     65   if (back != graph_view.num_nodes()) {
     66     return errors::InvalidArgument(
     67         "The graph couldn't be sorted in topological order.");
     68   }
     69 
     70   PermuteNodesInPlace(graph, &ready_nodes, /*invert_permutation=*/true);
     71   return Status::OK();
     72 }
     73 
     74 }  // namespace grappler
     75 }  // namespace tensorflow
     76