|
| 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 | +#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_common.h" |
| 17 | + |
| 18 | +#include "mlir/IR/BuiltinTypes.h" |
| 19 | + |
| 20 | +namespace mlir { |
| 21 | +namespace hlo { |
| 22 | + |
| 23 | +// Verifies the source target pairs attached to collective permute. |
| 24 | +LogicalResult VerifyCollectivePermuteSourceTargetPairs( |
| 25 | + Operation *op, DenseIntElementsAttr attr) { |
| 26 | + auto type = attr.getType().dyn_cast<RankedTensorType>(); |
| 27 | + if (type.getRank() != 2) |
| 28 | + return op->emitError() << "expect source_target_pairs attribute to be of " |
| 29 | + "rank 2, but got rank " |
| 30 | + << type.getRank(); |
| 31 | + if (type.getShape()[1] != 2) |
| 32 | + return op->emitError() |
| 33 | + << "expect source_target_pairs attribute of shape (N, 2), but got (" |
| 34 | + << type.getShape() << ")"; |
| 35 | + // Check source target pairs for duplicate sources or targets. |
| 36 | + llvm::DenseSet<int64_t> sources; |
| 37 | + llvm::DenseSet<int64_t> targets; |
| 38 | + for (auto i = attr.begin(), e = attr.end(); i != e; ++i) { |
| 39 | + auto val = (*i).getSExtValue(); |
| 40 | + if (i.getIndex() % 2 == 0) { |
| 41 | + bool is_unique = sources.insert(val).second; |
| 42 | + if (!is_unique) |
| 43 | + return op->emitError() << "duplicate sources not allowed."; |
| 44 | + } else { |
| 45 | + bool is_unique = targets.insert(val).second; |
| 46 | + if (!is_unique) |
| 47 | + return op->emitError() << "duplicate targets not allowed."; |
| 48 | + } |
| 49 | + } |
| 50 | + return success(); |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace hlo |
| 54 | +} // namespace mlir |
0 commit comments