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,bool unsafe_as_extent_tensor)51 Value ComputeBinaryElementwiseBroadcastingResultExtents(
52 Location loc, Value lhs, Value rhs, OpBuilder& builder,
53 bool unsafe_as_extent_tensor) {
54 auto lhs_type = lhs.getType().dyn_cast<RankedTensorType>();
55 auto rhs_type = rhs.getType().dyn_cast<RankedTensorType>();
56 if (!lhs_type || !rhs_type) {
57 emitError(loc) << "shape computation for broadcasting elementwise ops "
58 << "is only implemented for ranked tensors";
59 return nullptr;
60 }
61
62 Value lhs_shape_v = builder.createOrFold<shape::ShapeOfOp>(loc, lhs);
63 Value rhs_shape_v = builder.createOrFold<shape::ShapeOfOp>(loc, rhs);
64
65 if (unsafe_as_extent_tensor) {
66 int64_t result_rank = std::max(lhs_type.getRank(), rhs_type.getRank());
67 Value result_shape_v = builder.createOrFold<shape::BroadcastOp>(
68 loc, shape::getExtentTensorType(builder.getContext()), lhs_shape_v,
69 rhs_shape_v, nullptr /* error */);
70 return builder.createOrFold<tensor::CastOp>(
71 loc, RankedTensorType::get({result_rank}, builder.getIndexType()),
72 result_shape_v);
73 }
74
75 return builder.createOrFold<shape::BroadcastOp>(
76 loc, builder.getType<shape::ShapeType>(), lhs_shape_v, rhs_shape_v,
77 nullptr /* error */);
78 }
79
80 } // namespace hlo
81 } // namespace mlir
82