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_KERNELS_REDUCTION_OPS_H_ 17 #define TENSORFLOW_KERNELS_REDUCTION_OPS_H_ 18 19 // Functor definitions for Reduction ops, must be compilable by nvcc. 20 21 #include <iostream> 22 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/tensor_types.h" 25 26 namespace tensorflow { 27 namespace functor { 28 29 template <typename Device, typename OUT_T, typename IN_T, 30 typename ReductionAxes, typename Reducer> 31 void ReduceEigenImpl(const Device& d, OUT_T out, IN_T in, 32 const ReductionAxes& reduction_axes, 33 const Reducer& reducer) { 34 out.device(d) = in.reduce(reduction_axes, reducer); 35 } 36 37 // For most reducers, the identity is Reducer::initialize() 38 template <typename Reducer> 39 struct Identity { 40 static auto identity(const Reducer& reducer) 41 -> decltype(reducer.initialize()) { 42 return reducer.initialize(); 43 } 44 }; 45 46 // MeanReducer is a special case, since it doesn't technically have an identity. 47 // Thus, ideally we'd return nan. However, mean is instantiated for integer 48 // types as well, so we do the nan override only for floating point types. 49 #define FIX_MEAN_IDENTITY(T) \ 50 template <> \ 51 struct Identity<Eigen::internal::MeanReducer<T>> { \ 52 static T identity(const Eigen::internal::MeanReducer<T>&) { \ 53 return Eigen::NumTraits<T>::quiet_NaN(); \ 54 } \ 55 }; 56 FIX_MEAN_IDENTITY(Eigen::half) 57 FIX_MEAN_IDENTITY(float) 58 FIX_MEAN_IDENTITY(double) 59 FIX_MEAN_IDENTITY(complex64) 60 FIX_MEAN_IDENTITY(complex128) 61 #undef FIX_MEAN_IDENTITY 62 63 template <typename Device, typename OUT_T, typename Reducer> 64 void FillIdentityEigenImpl(const Device& d, OUT_T out, const Reducer& reducer) { 65 out.device(d) = out.constant(Identity<Reducer>::identity(reducer)); 66 } 67 68 template <typename Device, typename Reducer> 69 struct ReduceFunctor { 70 template <typename OUT_T, typename IN_T, typename ReductionAxes> 71 static void Reduce(OpKernelContext* ctx, OUT_T out, IN_T in, 72 const ReductionAxes& reduction_axes, 73 const Reducer& reducer); 74 75 template <typename OUT_T> 76 static void FillIdentity(const Device& d, OUT_T out, const Reducer& reducer); 77 }; 78 79 } // namespace functor 80 } // namespace tensorflow 81 82 #endif // TENSORFLOW_KERNELS_REDUCTION_OPS_H_ 83