1 /* Copyright 2017 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_PARTIAL_REDUCTION_OPS_H_ 17 #define TENSORFLOW_CORE_KERNELS_PARTIAL_REDUCTION_OPS_H_ 18 19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 20 #include "tensorflow/core/framework/tensor.h" 21 #include "tensorflow/core/framework/tensor_shape.h" 22 #include "tensorflow/core/framework/tensor_types.h" 23 24 #define Sum(a, b) ((a) + (b)) 25 #define Prod(a, b) ((a) * (b)) 26 #define Max(a, b) ((a) > (b) ? (a) : (b)) 27 #define Min(a, b) ((a) < (b) ? (a) : (b)) 28 29 namespace tensorflow { 30 31 class OpKernelContext; 32 33 namespace functor { 34 35 namespace reduce_functions { 36 37 template <typename T> 38 inline T zero() { 39 return T(0); 40 } 41 42 template <typename T> 43 inline T one() { 44 return T(1); 45 } 46 47 template <typename T> 48 inline T infinity() { 49 return std::max<T>(std::numeric_limits<T>::max(), 50 std::numeric_limits<T>::infinity()); 51 } 52 53 template <typename T> 54 inline T negative_infinity() { 55 return std::min<T>(-std::numeric_limits<T>::infinity(), 56 std::numeric_limits<T>::min()); 57 } 58 59 } // namespace reduce_functions 60 61 #define CALL_ALL_REDUCEOPS(func, ...) \ 62 func(Sum, functor::reduce_functions::zero, ##__VA_ARGS__) \ 63 func(Prod, functor::reduce_functions::one, ##__VA_ARGS__) func( \ 64 Max, functor::reduce_functions::negative_infinity, ##__VA_ARGS__) \ 65 func(Min, functor::reduce_functions::infinity, ##__VA_ARGS__) 66 67 #define ReduceSliceFunctorReduceop(reduceop, dummy) \ 68 template <typename Device, typename T, typename Index> \ 69 struct ReduceSliceFunctor##reduceop { \ 70 virtual ~ReduceSliceFunctor##reduceop() {} \ 71 virtual void operator()(OpKernelContext* ctx, const Device& d, \ 72 Index indices_width, \ 73 typename TTypes<Index, 1>::ConstTensor indices, \ 74 typename TTypes<T, 3>::ConstTensor data, \ 75 typename TTypes<T, 3>::Tensor output); \ 76 }; 77 78 CALL_ALL_REDUCEOPS(ReduceSliceFunctorReduceop) 79 #undef ReduceSliceFunctorReduceop 80 81 } // namespace functor 82 } // namespace tensorflow 83 84 #endif // TENSORFLOW_CORE_KERNELS_PARTIAL_REDUCTION_OPS_H_ 85