1 /* Copyright 2020 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/utils/broadcast_utils.h"
17
18 #include <algorithm>
19
20 #include "llvm/ADT/Sequence.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "mlir/Dialect/Shape/IR/Shape.h"
23 #include "mlir/Dialect/StandardOps/IR/Ops.h"
24 #include "mlir/Dialect/Tensor/IR/Tensor.h"
25 #include "mlir/IR/BuiltinTypes.h"
26 #include "mlir/IR/Diagnostics.h"
27
28 namespace mlir {
29 namespace hlo {
30
IsLegalNumpyRankedBroadcast(Value lhs,Value rhs,DenseIntElementsAttr broadcast_dims)31 bool IsLegalNumpyRankedBroadcast(Value lhs, Value rhs,
32 DenseIntElementsAttr broadcast_dims) {
33 RankedTensorType lhs_type = lhs.getType().dyn_cast<RankedTensorType>();
34 RankedTensorType rhs_type = rhs.getType().dyn_cast<RankedTensorType>();
35 if (!lhs_type || !rhs_type) return false;
36 if (lhs_type.getRank() == rhs_type.getRank()) return true;
37
38 // Otherwise, verify that broadcast_dims strictly performs left-padding.
39 auto smaller_rank = std::min(lhs_type.getRank(), rhs_type.getRank());
40 auto larger_rank = std::max(lhs_type.getRank(), rhs_type.getRank());
41
42 if (smaller_rank != broadcast_dims.getNumElements()) {
43 return false;
44 }
45 auto expected_extents =
46 llvm::seq<int64_t>(larger_rank - smaller_rank, larger_rank);
47 return std::equal(expected_extents.begin(), expected_extents.end(),
48 broadcast_dims.getIntValues().begin());
49 }
50
ComputeBinaryElementwiseBroadcastingResultExtents(Location loc,Value lhs,Value rhs,OpBuilder & builder)51 Value ComputeBinaryElementwiseBroadcastingResultExtents(Location loc, Value lhs,
52 Value rhs,
53 OpBuilder& builder) {
54 return ComputeNaryElementwiseBroadcastingResultExtents(
55 loc, ValueRange{lhs, rhs}, builder);
56 }
57
ComputeNaryElementwiseBroadcastingResultExtents(Location loc,ValueRange operands,OpBuilder & builder)58 Value ComputeNaryElementwiseBroadcastingResultExtents(Location loc,
59 ValueRange operands,
60 OpBuilder& builder) {
61 auto shapes = llvm::to_vector<4>(llvm::map_range(operands, [&](Value v) {
62 return builder.createOrFold<shape::ShapeOfOp>(loc, v);
63 }));
64
65 int64_t result_rank = 0;
66 for (Value s : shapes) {
67 auto ty = s.getType().cast<RankedTensorType>();
68 assert(ty.getRank() == 1 && "expect extent tensor type");
69 if (ty.isDynamicDim(0)) {
70 result_rank = ShapedType::kDynamicSize;
71 break;
72 } else {
73 result_rank = std::max(result_rank, ty.getDimSize(0));
74 }
75 }
76 Type extent_tensor_ty =
77 shape::getExtentTensorType(builder.getContext(), result_rank);
78
79 return builder.createOrFold<shape::BroadcastOp>(loc, extent_tensor_ty, shapes,
80 /*error=*/nullptr);
81 }
82
83 } // namespace hlo
84 } // namespace mlir
85