• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const char kXlaReplicaIdAttrName[] = "_xla_replica_id";
30 
31 const char kXlaIsPlaceholderForTailOcAttrName[] =
32     "_xla_is_placeholder_for_tail_oc";
33 
34 const char kXlaOriginalOutsideCompilationNodeName[] =
35     "_xla_original_oc_node_name";
36 
SetDeviceOrdinalAttributeForNode(Node * node,int device_ordinal)37 Status SetDeviceOrdinalAttributeForNode(Node* node, int device_ordinal) {
38   if (!HasNodeAttr(node->def(), kXlaHasHostTransferAttrName)) {
39     return errors::InvalidArgument("Node ", node->DebugString(),
40                                    " does not have attribute ",
41                                    kXlaHasHostTransferAttrName);
42   }
43 
44   if (node->type_string() == "_XlaRecvAtHost" ||
45       node->type_string() == "_XlaSendFromHost") {
46     node->ClearAttr("device_ordinal");
47     node->AddAttr("device_ordinal", device_ordinal);
48   } else if (node->IsIfNode()) {
49     AttrValue device_ordinal_value;
50     device_ordinal_value.set_i(device_ordinal);
51     for (const string& attr_name :
52          std::vector<string>{"then_branch", "else_branch"}) {
53       NameAttrList branch_func;
54       TF_RETURN_IF_ERROR(GetNodeAttr(node->attrs(), attr_name, &branch_func));
55       (*branch_func.mutable_attr())["_device_ordinal"] = device_ordinal_value;
56       node->ClearAttr(attr_name);
57       node->AddAttr(attr_name, branch_func);
58     }
59   } else if (node->IsWhileNode()) {
60     AttrValue device_ordinal_value;
61     device_ordinal_value.set_i(device_ordinal);
62     for (const string& attr_name : std::vector<string>{"cond", "body"}) {
63       NameAttrList branch_func;
64       TF_RETURN_IF_ERROR(GetNodeAttr(node->attrs(), attr_name, &branch_func));
65       (*branch_func.mutable_attr())["_device_ordinal"] = device_ordinal_value;
66       node->ClearAttr(attr_name);
67       node->AddAttr(attr_name, branch_func);
68     }
69   } else if (HasNodeAttr(node->def(), "_device_ordinal")) {
70     // Function call node containing outside compilation.
71     node->ClearAttr("_device_ordinal");
72     node->AddAttr("_device_ordinal", device_ordinal);
73   } else {
74     return errors::Internal("Unknown node type to set 'device_ordinal': ",
75                             node->DebugString());
76   }
77   return Status::OK();
78 }
79 
CalculateTokenInputsForOutputToken(const Graph & g)80 std::set<std::string> CalculateTokenInputsForOutputToken(const Graph& g) {
81   std::set<std::string> results;
82   Node* first_side_effecting_node_on_path = nullptr;
83   ReverseDFS(g,
84              [&](Node* n) {
85                std::vector<string> token_input_nodes;
86                if (!GetNodeAttr(n->attrs(), kXlaTokenInputNodesAttrName,
87                                 &token_input_nodes)
88                         .ok() ||
89                    token_input_nodes.empty()) {
90                  return;
91                }
92 
93                if (first_side_effecting_node_on_path != nullptr) {
94                  return;
95                }
96 
97                first_side_effecting_node_on_path = n;
98                string original_node_name;
99                TF_CHECK_OK(GetNodeAttr(n->def(),
100                                        kXlaOriginalOutsideCompilationNodeName,
101                                        &original_node_name));
102                results.insert(original_node_name);
103              },
104              [&](Node* n) {
105                if (first_side_effecting_node_on_path == n) {
106                  first_side_effecting_node_on_path = nullptr;
107                }
108              },
109              NodeComparatorName());
110   return results;
111 }
112 
HasSideEffectingNodes(const Graph & g)113 bool HasSideEffectingNodes(const Graph& g) {
114   for (Node* n : g.nodes()) {
115     std::vector<string> token_input_nodes;
116     if (GetNodeAttr(n->attrs(), kXlaTokenInputNodesAttrName, &token_input_nodes)
117             .ok() &&
118         !token_input_nodes.empty()) {
119       return true;
120     }
121   }
122   return false;
123 }
124 
ParseHostComputeCoreList(absl::Span<const string> list_from_attr,std::map<string,int> * host_compute_core)125 Status ParseHostComputeCoreList(absl::Span<const string> list_from_attr,
126                                 std::map<string, int>* host_compute_core) {
127   for (const auto& hc_core : list_from_attr) {
128     std::vector<string> parts = str_util::Split(hc_core, ":");
129     if (parts.size() != 2) {
130       return errors::InvalidArgument(
131           "Malformed host_compute_core entry ", hc_core,
132           " should be <cluster_name>:<core_number>.");
133     }
134     int core;
135     if (!absl::numbers_internal::safe_strto32_base(parts[1], &core, 10)) {
136       return errors::InvalidArgument("Malformed host_compute_core entry ",
137                                      hc_core,
138                                      " part after ':' should be an integer.");
139     }
140     if (host_compute_core->find(parts[0]) != host_compute_core->end()) {
141       return errors::InvalidArgument(
142           "Duplicate host_compute_core entry for cluster ", parts[0]);
143     }
144     (*host_compute_core)[parts[0]] = core;
145   }
146   return Status::OK();
147 }
148 
149 }  // namespace tensorflow
150