• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #ifndef TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_UTILS_LHLO_UTILS_H_
17 #define TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_UTILS_LHLO_UTILS_H_
18 
19 #include "llvm/ADT/SmallSet.h"
20 #include "mlir/IR/BuiltinAttributes.h"
21 #include "mlir/IR/BuiltinTypes.h"
22 #include "mlir/IR/Types.h"
23 
24 namespace mlir {
25 namespace lmhlo {
26 
27 // Verifies replica groups attached to collective communication operations.
28 // If the attribute is not empty, it must be a rank 2 tensor, and each replica
29 // should appear exactly once. If `is_uniform_sized` is true, then we also check
30 // that each group is of the same size. If the operation has
31 // `use_global_device_ids` set, then replica group cannot be empty.
32 template <typename OpT>
VerifyReplicaGroups(OpT op,bool is_uniform_sized)33 LogicalResult VerifyReplicaGroups(OpT op, bool is_uniform_sized) {
34   DenseIntElementsAttr attr = op.replica_groups();
35   auto replica_group_type = attr.getType().dyn_cast<RankedTensorType>();
36   if (!replica_group_type || replica_group_type.getRank() != 2 ||
37       !replica_group_type.getElementType().isInteger(/*width=*/64))
38     return op.emitOpError(
39         "replica groups should be a rank 2 tensor of 64 bit integers");
40 
41   if (replica_group_type.getShape().equals(ArrayRef<int64_t>{0, 0})) {
42     if (op.use_global_device_ids()) {
43       return op.emitOpError(
44           "if `use_global_device_ids` is set, the replica groups cannot be "
45           "empty");
46     }
47     return success();
48   }
49 
50   int64_t max_replica_id_seen = 0;
51   llvm::SmallSet<int64_t, 8> replica_seen;
52   for (int64_t id : attr.getValues<int64_t>()) {
53     // Replica groups are stored in a 2D tensor. If the op supports non-uniform
54     // groups, null replica IDs are stored as -1.
55     if (id == -1) {
56       if (is_uniform_sized) {
57         return op.emitOpError("Invalid replica id -1");
58       }
59       continue;
60     }
61 
62     if (!replica_seen.insert(id).second) {
63       return op.emitOpError("replica id #") << id << " seen more than once";
64     }
65     max_replica_id_seen = std::max(max_replica_id_seen, id);
66   }
67 
68   for (int64_t id = 0; id <= max_replica_id_seen; id++) {
69     if (!replica_seen.contains(id)) {
70       return op.emitOpError("replica id #")
71              << id << " not seen in replica groups";
72     }
73   }
74   return success();
75 }
76 
77 template <typename OpT>
VerifyAllReduce(OpT op)78 static LogicalResult VerifyAllReduce(OpT op) {
79   if (failed(VerifyReplicaGroups(op, /*is_uniform_sized=*/false)))
80     return failure();
81 
82   // AllReduce has variadic operands and results that have the same size.
83   // Each member of the operand should have the same type as the corresponding
84   // member of the result.
85   for (auto it : llvm::enumerate(
86            llvm::zip(op.operands().getTypes(), op.results().getTypes()))) {
87     Type operandType = std::get<0>(it.value());
88     Type resultType = std::get<1>(it.value());
89     if (operandType != resultType)
90       return op.emitOpError("requires operand #")
91              << it.index() << " (type: " << operandType << ") and result #"
92              << it.index() << " (type: " << resultType << ") to have same type";
93   }
94   return success();
95 }
96 
97 }  // namespace lmhlo
98 }  // namespace mlir
99 
100 #endif  // TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_UTILS_LHLO_UTILS_H_
101