• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/compiler/tf2xla/sharding_util.h"
16 
17 #include "absl/strings/match.h"
18 #include "tensorflow/core/framework/node_def.pb.h"
19 #include "tensorflow/core/lib/core/errors.h"
20 #include "tensorflow/core/util/device_name_utils.h"
21 
22 namespace tensorflow {
23 namespace {
24 const char kDeviceSuffixReplicatedCore[] = "REPLICATED_CORE";
25 const char kShardingAttribute[] = "_XlaSharding";
26 }  // namespace
27 
28 namespace {
GetShardingFromNodeDef(const NodeDef & node_def)29 xla::StatusOr<absl::optional<xla::OpSharding>> GetShardingFromNodeDef(
30     const NodeDef& node_def) {
31   if (!HasNodeAttr(node_def, kShardingAttribute)) {
32     return absl::optional<xla::OpSharding>();
33   }
34   string value;
35   xla::OpSharding sharding;
36   TF_RETURN_IF_ERROR(GetNodeAttr(node_def, kShardingAttribute, &value));
37   if (!sharding.ParseFromString(value)) {
38     return xla::InvalidArgument(
39         "Experimental _XlaSharding attribute was not a valid encoded "
40         "xla::OpSharding proto.");
41   }
42   return absl::optional<xla::OpSharding>(sharding);
43 }
44 
CoreOutOfRangeError(int core,int num_cores_per_replica)45 Status CoreOutOfRangeError(int core, int num_cores_per_replica) {
46   return errors::InvalidArgument(
47       "Invalid replicated core id: ", core,
48       "; num_cores_per_replica=", num_cores_per_replica);
49 }
50 }  // namespace
51 
ParseShardingFromDevice(const string & device_name,int num_cores_per_replica,absl::optional<xla::OpSharding> explicit_sharding)52 xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice(
53     const string& device_name, int num_cores_per_replica,
54     absl::optional<xla::OpSharding> explicit_sharding) {
55   if (device_name.empty()) {
56     return absl::optional<xla::OpSharding>();
57   }
58   DeviceNameUtils::ParsedName parsed_device;
59   if (!DeviceNameUtils::ParseFullName(device_name, &parsed_device)) {
60     return errors::InvalidArgument("Malformed assigned device '", device_name,
61                                    "'");
62   }
63 
64   if (explicit_sharding.has_value()) {
65     return explicit_sharding;
66   } else if (!parsed_device.has_type || !parsed_device.has_id ||
67              !absl::StrContains(parsed_device.type,
68                                 kDeviceSuffixReplicatedCore)) {
69     return absl::optional<xla::OpSharding>();
70   } else {
71     const int core = parsed_device.id;
72     if (core < 0 || core >= num_cores_per_replica) {
73       return CoreOutOfRangeError(core, num_cores_per_replica);
74     }
75     return absl::optional<xla::OpSharding>(
76         xla::sharding_builder::AssignDevice(core));
77   }
78 }
79 
ParseShardingFromDevice(const NodeDef & node_def,int num_cores_per_replica)80 xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice(
81     const NodeDef& node_def, int num_cores_per_replica) {
82   const string& device_name = node_def.device();
83   TF_ASSIGN_OR_RETURN(absl::optional<xla::OpSharding> sharding,
84                       GetShardingFromNodeDef(node_def));
85   return ParseShardingFromDevice(device_name, num_cores_per_replica, sharding);
86 }
87 
ParseShardingFromDevice(const Node & node,int num_cores_per_replica)88 xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice(
89     const Node& node, int num_cores_per_replica) {
90   string device_name = node.assigned_device_name();
91   if (device_name.empty()) {
92     device_name = node.requested_device();
93   }
94   TF_ASSIGN_OR_RETURN(absl::optional<xla::OpSharding> sharding,
95                       GetShardingFromNodeDef(node.def()));
96   return ParseShardingFromDevice(device_name, num_cores_per_replica, sharding);
97 }
98 
SetShardingDeviceAssignmentFromNode(const Node & src,Node * dst)99 void SetShardingDeviceAssignmentFromNode(const Node& src, Node* dst) {
100   string device_name = src.assigned_device_name();
101   if (device_name.empty()) {
102     device_name = src.requested_device();
103   }
104   dst->set_assigned_device_name(device_name);
105   if (const AttrValue* attr = src.attrs().Find(kShardingAttribute)) {
106     dst->AddAttr(kShardingAttribute, *attr);
107   }
108 }
109 
110 }  // namespace tensorflow
111