Home | History | Annotate | Download | only in tf2xla
      1 /* Copyright 2018 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/side_effect_util.h"
     17 
     18 #include "absl/strings/numbers.h"
     19 #include "tensorflow/core/graph/algorithm.h"
     20 
     21 namespace tensorflow {
     22 
     23 const char kXlaTokenInputNodesAttrName[] = "_xla_token_input_nodes";
     24 
     25 const char kXlaTokenArgNodeName[] = "_xla_token_arg_node";
     26 
     27 const char kXlaHasHostTransferAttrName[] = "_xla_has_host_transfer";
     28 
     29 Status SetDeviceOrdinalAttributeForNode(Node* node, int device_ordinal) {
     30   if (!HasNodeAttr(node->def(), kXlaHasHostTransferAttrName)) {
     31     return errors::InvalidArgument("Node ", node->DebugString(),
     32                                    " does not have attribute ",
     33                                    kXlaHasHostTransferAttrName);
     34   }
     35 
     36   if (node->type_string() == "_XlaRecvAtHost" ||
     37       node->type_string() == "_XlaSendFromHost") {
     38     node->ClearAttr("device_ordinal");
     39     node->AddAttr("device_ordinal", device_ordinal);
     40   } else if (node->type_string() == "If") {
     41     AttrValue device_ordinal_value;
     42     device_ordinal_value.set_i(device_ordinal);
     43     for (const string& attr_name :
     44          std::vector<string>{"then_branch", "else_branch"}) {
     45       NameAttrList branch_func;
     46       TF_RETURN_IF_ERROR(GetNodeAttr(node->attrs(), attr_name, &branch_func));
     47       (*branch_func.mutable_attr())["device_ordinal"] = device_ordinal_value;
     48       node->ClearAttr(attr_name);
     49       node->AddAttr(attr_name, branch_func);
     50     }
     51   } else if (node->type_string() == "While") {
     52     AttrValue device_ordinal_value;
     53     device_ordinal_value.set_i(device_ordinal);
     54     for (const string& attr_name : std::vector<string>{"cond", "body"}) {
     55       NameAttrList branch_func;
     56       TF_RETURN_IF_ERROR(GetNodeAttr(node->attrs(), attr_name, &branch_func));
     57       (*branch_func.mutable_attr())["device_ordinal"] = device_ordinal_value;
     58       node->ClearAttr(attr_name);
     59       node->AddAttr(attr_name, branch_func);
     60     }
     61   } else if (HasNodeAttr(node->def(), "device_ordinal")) {
     62     // Function call node containing outside compilation.
     63     node->ClearAttr("device_ordinal");
     64     node->AddAttr("device_ordinal", device_ordinal);
     65   } else {
     66     return errors::Internal("Unknown node type to set 'device_ordinal': ",
     67                             node->DebugString());
     68   }
     69   return Status::OK();
     70 }
     71 
     72 std::set<std::string> CalculateTokenInputsForOutputToken(const Graph& g) {
     73   std::set<std::string> results;
     74   Node* first_side_effecting_node_on_path = nullptr;
     75   ReverseDFS(g,
     76              [&](Node* n) {
     77                std::vector<string> token_input_nodes;
     78                if (!GetNodeAttr(n->attrs(), kXlaTokenInputNodesAttrName,
     79                                 &token_input_nodes)
     80                         .ok() ||
     81                    token_input_nodes.empty()) {
     82                  return;
     83                }
     84 
     85                if (first_side_effecting_node_on_path != nullptr) {
     86                  return;
     87                }
     88 
     89                first_side_effecting_node_on_path = n;
     90                results.insert(n->name());
     91              },
     92              [&](Node* n) {
     93                if (first_side_effecting_node_on_path == n) {
     94                  first_side_effecting_node_on_path = nullptr;
     95                }
     96              },
     97              NodeComparatorName());
     98   return results;
     99 }
    100 
    101 bool HasSideEffectingNodes(const Graph& g) {
    102   for (Node* n : g.nodes()) {
    103     std::vector<string> token_input_nodes;
    104     if (GetNodeAttr(n->attrs(), kXlaTokenInputNodesAttrName, &token_input_nodes)
    105             .ok() &&
    106         !token_input_nodes.empty()) {
    107       return true;
    108     }
    109   }
    110   return false;
    111 }
    112 
    113 Status ParseHostComputeCoreList(absl::Span<const string> list_from_attr,
    114                                 std::map<string, int>* host_compute_core) {
    115   for (const auto& hc_core : list_from_attr) {
    116     std::vector<string> parts = str_util::Split(hc_core, ":");
    117     if (parts.size() != 2) {
    118       return errors::InvalidArgument(
    119           "Malformed host_compute_core entry ", hc_core,
    120           " should be <cluster_name>:<core_number>.");
    121     }
    122     int core;
    123     if (!absl::numbers_internal::safe_strto32_base(parts[1], &core, 10)) {
    124       return errors::InvalidArgument("Malformed host_compute_core entry ",
    125                                      hc_core,
    126                                      " part after ':' should be an integer.");
    127     }
    128     if (host_compute_core->find(parts[0]) != host_compute_core->end()) {
    129       return errors::InvalidArgument(
    130           "Duplicate host_compute_core entry for cluster ", parts[0]);
    131     }
    132     (*host_compute_core)[parts[0]] = core;
    133   }
    134   return Status::OK();
    135 }
    136 
    137 }  // namespace tensorflow
    138