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 #if GOOGLE_CUDA 17 18 #define EIGEN_USE_GPU 19 20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 21 #include "tensorflow/core/kernels/cwise_ops_gpu_common.cu.h" 22 23 namespace tensorflow { 24 namespace functor { 25 26 template <typename T> 27 struct SelectFunctor<GPUDevice, T> { 28 void operator()(const GPUDevice& d, typename TTypes<T>::Flat out, 29 typename TTypes<bool>::ConstFlat cond_flat, 30 typename TTypes<T>::ConstFlat then_flat, 31 typename TTypes<T>::ConstFlat else_flat) { 32 To32Bit(out).device(d) = 33 To32Bit(cond_flat).select(To32Bit(then_flat), To32Bit(else_flat)); 34 } 35 }; 36 37 template <typename T> 38 struct SelectScalarFunctor<GPUDevice, T> { 39 void operator()(const GPUDevice& d, typename TTypes<T>::Flat out, 40 typename TTypes<bool>::ConstScalar cond, 41 typename TTypes<T>::ConstFlat then_flat, 42 typename TTypes<T>::ConstFlat else_flat) { 43 #if !defined(EIGEN_HAS_INDEX_LIST) 44 Eigen::array<int, 1> rank1{1}; 45 #else 46 Eigen::IndexList<Eigen::type2index<1> > rank1; 47 #endif 48 const int size = then_flat.dimension(0); 49 Eigen::array<int, 1> broadcast_dims{size}; 50 51 To32Bit(out).device(d) = cond.reshape(rank1) 52 .broadcast(broadcast_dims) 53 .select(then_flat, else_flat); 54 } 55 }; 56 57 template <typename T> 58 struct BatchSelectFunctor<GPUDevice, T> { 59 void operator()(const GPUDevice& d, 60 typename TTypes<T>::Matrix output_flat_outer_dims, 61 TTypes<bool>::ConstVec cond_vec, 62 typename TTypes<T>::ConstMatrix then_flat_outer_dims, 63 typename TTypes<T>::ConstMatrix else_flat_outer_dims) { 64 const int batch = cond_vec.size(); 65 const int all_but_batch = then_flat_outer_dims.dimension(1); 66 67 #if !defined(EIGEN_HAS_INDEX_LIST) 68 Eigen::array<int, 2> broadcast_dims{{ 1, all_but_batch }}; 69 Eigen::Tensor<int, 2>::Dimensions reshape_dims{{ batch, 1 }}; 70 #else 71 Eigen::IndexList<Eigen::type2index<1>, int> broadcast_dims; 72 broadcast_dims.set(1, all_but_batch); 73 Eigen::IndexList<int, Eigen::type2index<1> > reshape_dims; 74 reshape_dims.set(0, batch); 75 #endif 76 77 // TODO(ebrevdo): Figure out why this leads to erroneous memory access. 78 // 79 // To32Bit(output_flat_outer_dims).device(d) = 80 // To32Bit(cond_vec) 81 // .reshape(reshape_dims) 82 // .broadcast(broadcast_dims) 83 // .select(To32Bit(then_flat_outer_dims), 84 // To32Bit(else_flat_outer_dims)); 85 output_flat_outer_dims.device(d) = 86 cond_vec.reshape(reshape_dims) 87 .broadcast(broadcast_dims) 88 .select(then_flat_outer_dims, else_flat_outer_dims); 89 } 90 }; 91 92 #define SELECT_FUNCTOR(T) \ 93 template struct SelectFunctor<GPUDevice, T>; \ 94 template struct SelectScalarFunctor<GPUDevice, T>; \ 95 template struct BatchSelectFunctor<GPUDevice, T>; 96 97 SELECT_FUNCTOR(Eigen::half); 98 SELECT_FUNCTOR(float); 99 SELECT_FUNCTOR(double); 100 SELECT_FUNCTOR(int32); 101 SELECT_FUNCTOR(int64); 102 SELECT_FUNCTOR(complex64); 103 SELECT_FUNCTOR(complex128); 104 105 #undef SELECT_FUNCTOR 106 107 } // namespace functor 108 } // namespace tensorflow 109 110 #endif // GOOGLE_CUDA 111