• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
DoBCast32BitBroadcastTo35   void DoBCast32Bit(const Device &device, typename TTypes<T, NDIMS>::Tensor out,
36                     typename TTypes<T, NDIMS>::ConstTensor in,
37                     const typename Eigen::array<int, NDIMS> &bcast) const {
38     To32Bit(out).device(device) = To32Bit(in).broadcast(bcast);
39   }
40 
41   template <int NDIMS>
DoBCastBroadcastTo42   void DoBCast(
43       const Device &device, typename TTypes<T, NDIMS>::Tensor out,
44       typename TTypes<T, NDIMS>::ConstTensor in,
45       const typename Eigen::array<Eigen::DenseIndex, NDIMS> &bcast) const {
46     out.device(device) = in.broadcast(bcast);
47   }
48 
49   template <int NDIMS>
ReshapeAndBCastBroadcastTo50   void ReshapeAndBCast(const Device &device, Tensor &output_tensor,
51                        const Tensor &input_tensor, const BCast &bcast) const {
52     const bool can_use_32bit = std::is_same<Eigen::GpuDevice, Device>::value &&
53                                output_tensor.NumElements() < kint32max &&
54                                input_tensor.NumElements() < kint32max;
55     if (can_use_32bit) {
56       DoBCast32Bit<NDIMS>(
57           device, output_tensor.template shaped<T, NDIMS>(bcast.result_shape()),
58           input_tensor.template shaped<T, NDIMS>(bcast.x_reshape()),
59           BCast::ToIndexArrayType<int, NDIMS>(bcast.x_bcast()));
60     } else {
61       DoBCast<NDIMS>(
62           device, output_tensor.template shaped<T, NDIMS>(bcast.result_shape()),
63           input_tensor.template shaped<T, NDIMS>(bcast.x_reshape()),
64           BCast::ToIndexArrayType<Eigen::DenseIndex, NDIMS>(bcast.x_bcast()));
65     }
66   }
67 
68   // PRECONDITION: rank(input_shape) > 0 &&
69   //               rank(input_shape) <= rank(output_shape)  &&
70   //               output_shape.num_elements() > 0.
operatorBroadcastTo71   void operator()(const Device &device, OpKernelContext *ctx,
72                   Tensor &output_tensor, const TensorShape &output_shape,
73                   const Tensor &input_tensor, const TensorShape &input_shape,
74                   const BCast &bcast) const {
75     const int ndims = bcast.y_reshape().size();
76     switch (ndims) {
77       case 1:
78         ReshapeAndBCast<1>(device, output_tensor, input_tensor, bcast);
79         break;
80       case 2:
81         ReshapeAndBCast<2>(device, output_tensor, input_tensor, bcast);
82         break;
83       case 3:
84         ReshapeAndBCast<3>(device, output_tensor, input_tensor, bcast);
85         break;
86       case 4:
87         ReshapeAndBCast<4>(device, output_tensor, input_tensor, bcast);
88         break;
89       case 5:
90         ReshapeAndBCast<5>(device, output_tensor, input_tensor, bcast);
91         break;
92       default:
93         ctx->SetStatus(errors::Unimplemented(
94             "Broadcast between ", input_shape.DebugString(), " and ",
95             output_shape.DebugString(), " is not supported yet."));
96         break;
97     }
98   }
99 };
100 
101 }  // namespace functor
102 }  // namespace tensorflow
103 
104 #endif  // TENSORFLOW_CORE_KERNELS_BROADCAST_TO_OP_H_
105