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 #include "tensorflow/compiler/tf2xla/sharding_util.h"
     16 
     17 #include "tensorflow/core/framework/node_def.pb.h"
     18 #include "tensorflow/core/lib/core/errors.h"
     19 #include "tensorflow/core/lib/strings/strcat.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 {
     29 xla::StatusOr<tensorflow::gtl::optional<xla::OpSharding>>
     30 GetShardingFromNodeDef(const NodeDef& node_def) {
     31   if (!HasNodeAttr(node_def, kShardingAttribute)) {
     32     return tensorflow::gtl::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 tensorflow::gtl::optional<xla::OpSharding>(sharding);
     43 }
     44 
     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 
     52 xla::StatusOr<tensorflow::gtl::optional<xla::OpSharding>>
     53 ParseShardingFromDevice(
     54     const string& device_name, int num_cores_per_replica,
     55     tensorflow::gtl::optional<xla::OpSharding> explicit_sharding) {
     56   if (device_name.empty()) {
     57     return tensorflow::gtl::optional<xla::OpSharding>();
     58   }
     59   DeviceNameUtils::ParsedName parsed_device;
     60   if (!DeviceNameUtils::ParseFullName(device_name, &parsed_device)) {
     61     return errors::InvalidArgument("Malformed assigned device '", device_name,
     62                                    "'");
     63   }
     64 
     65   if (explicit_sharding.has_value()) {
     66     return explicit_sharding;
     67   } else if (!parsed_device.has_type || !parsed_device.has_id ||
     68              !StringPiece(parsed_device.type)
     69                   .contains(kDeviceSuffixReplicatedCore)) {
     70     return tensorflow::gtl::optional<xla::OpSharding>();
     71   } else {
     72     const int core = parsed_device.id;
     73     if (core < 0 || core >= num_cores_per_replica) {
     74       return CoreOutOfRangeError(core, num_cores_per_replica);
     75     }
     76     return tensorflow::gtl::optional<xla::OpSharding>(
     77         xla::sharding_builder::AssignDevice(core));
     78   }
     79 }
     80 
     81 xla::StatusOr<tensorflow::gtl::optional<xla::OpSharding>>
     82 ParseShardingFromDevice(const NodeDef& node_def, int num_cores_per_replica) {
     83   const string& device_name = node_def.device();
     84   TF_ASSIGN_OR_RETURN(tensorflow::gtl::optional<xla::OpSharding> sharding,
     85                       GetShardingFromNodeDef(node_def));
     86   return ParseShardingFromDevice(device_name, num_cores_per_replica, sharding);
     87 }
     88 
     89 xla::StatusOr<tensorflow::gtl::optional<xla::OpSharding>>
     90 ParseShardingFromDevice(const Node& node, int num_cores_per_replica) {
     91   string device_name = node.assigned_device_name();
     92   if (device_name.empty()) {
     93     device_name = node.requested_device();
     94   }
     95   TF_ASSIGN_OR_RETURN(tensorflow::gtl::optional<xla::OpSharding> sharding,
     96                       GetShardingFromNodeDef(node.def()));
     97   return ParseShardingFromDevice(device_name, num_cores_per_replica, sharding);
     98 }
     99 
    100 void SetShardingDeviceAssignmentFromNode(const Node& src, Node* dst) {
    101   string device_name = src.assigned_device_name();
    102   if (device_name.empty()) {
    103     device_name = src.requested_device();
    104   }
    105   dst->set_assigned_device_name(device_name);
    106   if (const AttrValue* attr = src.attrs().Find(kShardingAttribute)) {
    107     dst->AddAttr(kShardingAttribute, *attr);
    108   }
    109 }
    110 
    111 }  // namespace tensorflow
    112