| 50 | } // namespace |
| 51 | |
| 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 explicit_sharding; |
| 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 | |
| 80 | xla::StatusOr<absl::optional<xla::OpSharding>> ParseShardingFromDevice( |
| 81 | const NodeDef& node_def, int num_cores_per_replica) { |