1 | /* Copyright 2015 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_CORE_KERNELS_BROADCAST_TO_OP_H_ |
17 | #define TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_ |
18 | |
19 | #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" |
20 | #include "tensorflow/core/framework/op_kernel.h" |
21 | #include "tensorflow/core/framework/tensor.h" |
22 | #include "tensorflow/core/framework/tensor_shape.h" |
23 | #include "tensorflow/core/framework/tensor_types.h" |
24 | #include "tensorflow/core/framework/types.h" |
25 | #include "tensorflow/core/kernels/fill_functor.h" |
26 | #include "tensorflow/core/util/bcast.h" |
27 | |
28 | namespace tensorflow { |
29 | |
30 | namespace functor { |
31 | |
32 | template <typename Device, typename T> |
33 | struct BroadcastTo { |
34 | template <int NDIMS> |
35 | void DoBCast( |
36 | const Device &device, typename TTypes<T, NDIMS>::Tensor out, |
37 | typename TTypes<T, NDIMS>::ConstTensor in, |
38 | const typename Eigen::array<Eigen::DenseIndex, NDIMS> &bcast) const { |
39 | MaybeWith32BitIndexing<Device>( |
40 | [&](auto out32, auto in32, const auto &bcast32) { |
41 | out32.device(device) = in32.broadcast(bcast32); |
42 | }, |
43 | out, in, bcast); |
44 | } |
45 | |
46 | template <int NDIMS> |
47 | void ReshapeAndBCast(const Device &device, Tensor &output_tensor, |
48 | const Tensor &input_tensor, const BCast &bcast) const { |
49 | DoBCast<NDIMS>( |
50 | device, output_tensor.template shaped<T, NDIMS>(bcast.result_shape()), |
51 | input_tensor.template shaped<T, NDIMS>(bcast.x_reshape()), |
52 | BCast::ToIndexArrayType<Eigen::DenseIndex, NDIMS>(bcast.x_bcast())); |
53 | } |
54 | |
55 | // PRECONDITION: rank(input_shape) > 0 && |
56 | // rank(input_shape) <= rank(output_shape) && |
57 | // output_shape.num_elements() > 0. |
58 | void operator()(const Device &device, OpKernelContext *ctx, |
59 | Tensor &output_tensor, const TensorShape &output_shape, |
60 | const Tensor &input_tensor, const TensorShape &input_shape, |
61 | const BCast &bcast) const { |
62 | const int ndims = bcast.y_reshape().size(); |
63 | switch (ndims) { |
64 | case 1: |
65 | ReshapeAndBCast<1>(device, output_tensor, input_tensor, bcast); |
66 | break; |
67 | case 2: |
68 | ReshapeAndBCast<2>(device, output_tensor, input_tensor, bcast); |
69 | break; |
70 | case 3: |
71 | ReshapeAndBCast<3>(device, output_tensor, input_tensor, bcast); |
72 | break; |
73 | case 4: |
74 | ReshapeAndBCast<4>(device, output_tensor, input_tensor, bcast); |
75 | break; |
76 | case 5: |
77 | ReshapeAndBCast<5>(device, output_tensor, input_tensor, bcast); |
78 | break; |
79 | default: |
80 | ctx->SetStatus(errors::Unimplemented( |
81 | "Broadcast between " , input_shape.DebugString(), " and " , |
82 | output_shape.DebugString(), " is not supported yet." )); |
83 | break; |
84 | } |
85 | } |
86 | }; |
87 | |
88 | } // namespace functor |
89 | } // namespace tensorflow |
90 | |
91 | #endif // TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_ |
92 | |