Home | History | Annotate | Download | only in tf2xla
      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/compiler/tf2xla/const_analysis.h"
     17 
     18 #include <unordered_map>
     19 #include <unordered_set>
     20 
     21 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     22 #include "tensorflow/core/framework/node_def_util.h"
     23 #include "tensorflow/core/graph/algorithm.h"
     24 
     25 namespace tensorflow {
     26 
     27 // Backwards dataflow analysis that finds arguments to a graph that must be
     28 // compile-time constants.
     29 Status BackwardsConstAnalysis(const Graph& g,
     30                               std::vector<bool>* compile_time_const_args) {
     31   // Operators that don't look at the data of their inputs, just the shapes.
     32   const std::unordered_set<string> metadata_ops = {
     33       "Rank",
     34       "Shape",
     35       "ShapeN",
     36       "Size",
     37   };
     38 
     39   Status status;
     40   std::unordered_set<Node*> must_be_const;
     41   auto visit = [&status, &metadata_ops, &must_be_const,
     42                 compile_time_const_args](Node* node) {
     43     if (!status.ok()) return;
     44 
     45     // If this is a metadata-only op, don't propagate the const requirement.
     46     if (metadata_ops.find(node->type_string()) != metadata_ops.end()) return;
     47 
     48     // If this node must be const, and it isn't a metadata op, then all of its
     49     // parents must be const.
     50     if (must_be_const.find(node) != must_be_const.end()) {
     51       if (node->type_string() == "_Arg") {
     52         int index;
     53         status = GetNodeAttr(node->attrs(), "index", &index);
     54         if (!status.ok()) return;
     55         compile_time_const_args->at(index) = true;
     56         return;
     57       }
     58       for (Node* pred : node->in_nodes()) {
     59         must_be_const.insert(pred);
     60       }
     61       return;
     62     }
     63 
     64     // Mark any compile-time constant operator arguments as const.
     65     const std::unordered_set<string>* const_inputs =
     66         XlaOpRegistry::CompileTimeConstantInputs(node->type_string());
     67     if (!const_inputs || const_inputs->empty()) return;
     68 
     69     NameRangeMap input_name_ranges;
     70     status =
     71         NameRangesForNode(*node, node->op_def(), &input_name_ranges, nullptr);
     72     if (!status.ok()) return;
     73 
     74     for (const string& input : *const_inputs) {
     75       auto name_range = input_name_ranges.find(input);
     76       if (name_range == input_name_ranges.end()) continue;
     77 
     78       for (Edge const* edge : node->in_edges()) {
     79         if (edge->dst_input() >= name_range->second.first &&
     80             edge->dst_input() < name_range->second.second) {
     81           must_be_const.insert(edge->src());
     82         }
     83       }
     84     }
     85   };
     86 
     87   // Post-order traversal visits nodes in reverse topological order for an
     88   // acyclic graph.
     89   DFS(g, {}, visit);
     90   return status;
     91 }
     92 
     93 }  // namespace tensorflow
     94