1 /* Copyright 2019 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/xla/service/collective_ops_utils.h"
17
18 #include "tensorflow/compiler/xla/service/global_device_id.h"
19
20 namespace xla {
21
MatchReductionComputation(const HloComputation * computation)22 absl::optional<ReductionKind> MatchReductionComputation(
23 const HloComputation* computation) {
24 namespace m = match;
25 const HloInstruction* root = computation->root_instruction();
26
27 auto match_opcode = [&](HloOpcode opcode) {
28 return Match(
29 root, m::Op()
30 .WithOpcode(opcode)
31 .WithBinaryOperandsAnyOrder(m::Parameter(0), m::Parameter(1))
32 .WithShape(m::Shape().IsEffectiveScalar()));
33 };
34
35 // Match the operation to a reduction kind. We can represent and/or of pred as
36 // min/max. This works because pred is stored as an 8-bit int of value 0 or 1.
37 PrimitiveType type = computation->root_instruction()->shape().element_type();
38 if (match_opcode(HloOpcode::kAdd)) {
39 return ReductionKind::SUM;
40 } else if (match_opcode(HloOpcode::kMultiply)) {
41 return ReductionKind::PRODUCT;
42 } else if (match_opcode(HloOpcode::kMinimum) ||
43 (type == PRED && match_opcode(HloOpcode::kAnd))) {
44 return ReductionKind::MIN;
45 } else if (match_opcode(HloOpcode::kMaximum) ||
46 (type == PRED && match_opcode(HloOpcode::kOr))) {
47 return ReductionKind::MAX;
48 } else {
49 return absl::nullopt;
50 }
51 }
52
GetParticipatingReplicas(int replica_id,int total_replica_count,absl::Span<const ReplicaGroup> replica_groups)53 StatusOr<std::vector<int>> GetParticipatingReplicas(
54 int replica_id, int total_replica_count,
55 absl::Span<const ReplicaGroup> replica_groups) {
56 // Empty replica_groups() means that all replicas participate.
57 if (replica_groups.empty()) {
58 std::vector<int> all_replicas(total_replica_count);
59 absl::c_iota(all_replicas, 0);
60 return all_replicas;
61 }
62
63 // Figure out the other replicas that go together with this one.
64 absl::optional<ReplicaGroup> replica_group;
65 for (const ReplicaGroup& g : replica_groups) {
66 if (absl::c_linear_search(g.replica_ids(), replica_id)) {
67 TF_RET_CHECK(!replica_group.has_value())
68 << "Replica " << replica_id << " appears twice in replica groups";
69 replica_group = g;
70 }
71 }
72 TF_RET_CHECK(replica_group.has_value())
73 << "Replica " << replica_id << " doesn't appear in replica groups";
74 return std::vector<int>(replica_group->replica_ids().begin(),
75 replica_group->replica_ids().end());
76 }
77
GetParticipatingDevices(GlobalDeviceId device_id,const DeviceAssignment & device_assignment,int total_replica_count,absl::Span<const ReplicaGroup> replica_groups)78 StatusOr<std::vector<GlobalDeviceId>> GetParticipatingDevices(
79 GlobalDeviceId device_id, const DeviceAssignment& device_assignment,
80 int total_replica_count, absl::Span<const ReplicaGroup> replica_groups) {
81 std::vector<GlobalDeviceId> participants;
82 // Fast path for common case, avoiding logical IDs lookup.
83 if (replica_groups.empty() && device_assignment.computation_count() == 1) {
84 participants.reserve(total_replica_count);
85 for (int replica_id = 0; replica_id < total_replica_count; ++replica_id) {
86 participants.emplace_back(
87 device_assignment(replica_id, /*computation_id=*/0));
88 }
89 return participants;
90 }
91
92 std::pair<int, int> logical_ids;
93 TF_ASSIGN_OR_RETURN(logical_ids,
94 device_assignment.LogicalIdsForDevice(device_id));
95 int replica_id = logical_ids.first;
96 int computation_id = logical_ids.second;
97 TF_ASSIGN_OR_RETURN(std::vector<int> participating_replicas,
98 GetParticipatingReplicas(replica_id, total_replica_count,
99 replica_groups));
100
101 participants.reserve(participating_replicas.size());
102 for (int replica_id : participating_replicas) {
103 participants.emplace_back(device_assignment(replica_id, computation_id));
104 }
105 return participants;
106 }
107
108 } // end namespace xla
109