1 /* Copyright 2018 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/tf2xla/lib/broadcast.h"
17
18 #include <vector>
19
20 #include "absl/algorithm/container.h"
21 #include "absl/strings/str_join.h"
22 #include "tensorflow/compiler/tf2xla/shape_util.h"
23 #include "tensorflow/compiler/xla/shape_util.h"
24 #include "tensorflow/compiler/xla/status_macros.h"
25 #include "tensorflow/compiler/xla/util.h"
26 #include "tensorflow/core/framework/tensor_shape.h"
27 #include "tensorflow/core/util/bcast.h"
28
29 namespace tensorflow {
30
BroadcastTo(xla::XlaOp input,absl::Span<int64 const> output_dims)31 xla::StatusOr<xla::XlaOp> BroadcastTo(xla::XlaOp input,
32 absl::Span<int64 const> output_dims) {
33 xla::XlaBuilder* builder = input.builder();
34 TF_ASSIGN_OR_RETURN(xla::Shape input_shape, builder->GetShape(input));
35 absl::Span<int64 const> input_dims =
36 xla::AsInt64Slice(input_shape.dimensions());
37
38 if (input_dims == output_dims) {
39 return input;
40 }
41
42 if (input_dims.size() > output_dims.size()) {
43 return errors::InvalidArgument(
44 "Input shape (", xla::ShapeUtil::HumanString(input_shape),
45 ") must have rank less than or equal to the output shape [",
46 absl::StrJoin(output_dims, ","), "]");
47 }
48
49 std::vector<int64> broadcast_dims;
50 std::vector<int64> broadcast_shape;
51 auto input_it = input_dims.rbegin();
52 for (auto output_it = output_dims.rbegin(); output_it != output_dims.rend();
53 ++output_it) {
54 if (input_it != input_dims.rend()) {
55 if (!(*output_it == 0 && *input_it == 0) &&
56 !(*input_it != 0 && *output_it % *input_it == 0)) {
57 return errors::InvalidArgument("Invalid shape broadcast from ",
58 xla::ShapeUtil::HumanString(input_shape),
59 " to [", absl::StrJoin(output_dims, ","),
60 "]");
61 }
62
63 broadcast_dims.push_back(broadcast_shape.size());
64 if (*output_it == *input_it || *input_it == 1) {
65 broadcast_shape.push_back(*output_it);
66 } else if (*output_it != *input_it) {
67 // Add dimensions [I, O/I], which we will later flatten to just
68 // [O]. We must do this in two phases since XLA broadcasting does not
69 // support tiling.
70 broadcast_shape.push_back(*input_it);
71 broadcast_shape.push_back(*output_it / *input_it);
72 }
73 ++input_it;
74 } else {
75 broadcast_shape.push_back(*output_it);
76 }
77 }
78 TF_RET_CHECK(input_it == input_dims.rend());
79
80 absl::c_reverse(broadcast_dims);
81 int broadcast_shape_size = broadcast_shape.size();
82 for (int64& broadcast_dim : broadcast_dims) {
83 broadcast_dim = broadcast_shape_size - broadcast_dim - 1;
84 }
85 absl::c_reverse(broadcast_shape);
86 xla::XlaOp output =
87 xla::BroadcastInDim(input, broadcast_shape, broadcast_dims);
88 if (broadcast_shape != output_dims) {
89 output = xla::Reshape(output, output_dims);
90 }
91 return output;
92 }
93
BroadcastOpsToSame(xla::XlaOp * lhs,xla::XlaOp * rhs)94 Status BroadcastOpsToSame(xla::XlaOp* lhs, xla::XlaOp* rhs) {
95 TF_ASSIGN_OR_RETURN(auto lhs_xla_shape, lhs->builder()->GetShape(*lhs));
96 TF_ASSIGN_OR_RETURN(auto rhs_xla_shape, rhs->builder()->GetShape(*rhs));
97 TensorShape lhs_tf_shape;
98 TensorShape rhs_tf_shape;
99 TF_RETURN_IF_ERROR(XLAShapeToTensorShape(lhs_xla_shape, &lhs_tf_shape));
100 TF_RETURN_IF_ERROR(XLAShapeToTensorShape(rhs_xla_shape, &rhs_tf_shape));
101 if (!lhs_tf_shape.IsSameSize(rhs_tf_shape)) {
102 BCast bcast(BCast::FromShape(lhs_tf_shape), BCast::FromShape(rhs_tf_shape));
103 if (!bcast.IsValid()) {
104 return errors::InvalidArgument(
105 "Dimensions cannot be made to match through broadcasting");
106 }
107 TF_ASSIGN_OR_RETURN(*lhs, BroadcastTo(*lhs, bcast.output_shape()));
108 TF_ASSIGN_OR_RETURN(*rhs, BroadcastTo(*rhs, bcast.output_shape()));
109 }
110 return Status::OK();
111 }
112
113 } // namespace tensorflow
114