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 // See docs in ../ops/nn_ops.cc. 17 18 #define EIGEN_USE_THREADS 19 20 #include "tensorflow/core/kernels/relu_op.h" 21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 22 #include "tensorflow/core/framework/numeric_op.h" 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/register_types.h" 25 #include "tensorflow/core/framework/tensor.h" 26 #include "tensorflow/core/lib/core/errors.h" 27 28 namespace tensorflow { 29 30 typedef Eigen::ThreadPoolDevice CPUDevice; 31 typedef Eigen::GpuDevice GPUDevice; 32 #ifdef TENSORFLOW_USE_SYCL 33 typedef Eigen::SyclDevice SYCLDevice; 34 #endif // TENSORFLOW_USE_SYCL 35 36 #define REGISTER_RELU_KERNELS(type) \ 37 REGISTER_KERNEL_BUILDER( \ 38 Name("Relu").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 39 ReluOp<CPUDevice, type>); \ 40 REGISTER_KERNEL_BUILDER( \ 41 Name("ReluGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 42 ReluGradOp<CPUDevice, type>); \ 43 REGISTER_KERNEL_BUILDER( \ 44 Name("Relu6").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 45 Relu6Op<CPUDevice, type>); \ 46 REGISTER_KERNEL_BUILDER( \ 47 Name("Relu6Grad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 48 Relu6GradOp<CPUDevice, type>) 49 50 TF_CALL_REAL_NUMBER_TYPES(REGISTER_RELU_KERNELS); 51 #undef REGISTER_RELU_KERNELS 52 53 #define REGISTER_ELU_KERNELS(type) \ 54 REGISTER_KERNEL_BUILDER( \ 55 Name("Elu").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 56 EluOp<CPUDevice, type>); \ 57 REGISTER_KERNEL_BUILDER( \ 58 Name("EluGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 59 EluGradOp<CPUDevice, type>); \ 60 REGISTER_KERNEL_BUILDER( \ 61 Name("Selu").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 62 SeluOp<CPUDevice, type>); \ 63 REGISTER_KERNEL_BUILDER( \ 64 Name("SeluGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 65 SeluGradOp<CPUDevice, type>) 66 67 // Elu and Selu only make sense with float or double. 68 TF_CALL_GPU_NUMBER_TYPES(REGISTER_ELU_KERNELS); 69 #undef REGISTER_ELU_KERNELS 70 71 #if GOOGLE_CUDA 72 // Forward declarations of the functor specializations for GPU. 73 namespace functor { 74 #define DECLARE_GPU_SPEC(T) \ 75 template <> \ 76 void Relu<GPUDevice, T>::operator()( \ 77 const GPUDevice& d, typename TTypes<T>::ConstTensor features, \ 78 typename TTypes<T>::Tensor activations); \ 79 extern template struct Relu<GPUDevice, T>; \ 80 \ 81 template <> \ 82 void ReluGrad<GPUDevice, T>::operator()( \ 83 const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \ 84 typename TTypes<T>::ConstTensor features, \ 85 typename TTypes<T>::Tensor backprops); \ 86 extern template struct ReluGrad<GPUDevice, T>; \ 87 \ 88 template <> \ 89 void Relu6<GPUDevice, T>::operator()( \ 90 const GPUDevice& d, typename TTypes<T>::ConstTensor features, \ 91 typename TTypes<T>::Tensor activations); \ 92 extern template struct Relu6<GPUDevice, T>; \ 93 \ 94 template <> \ 95 void Relu6Grad<GPUDevice, T>::operator()( \ 96 const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \ 97 typename TTypes<T>::ConstTensor features, \ 98 typename TTypes<T>::Tensor backprops); \ 99 extern template struct Relu6Grad<GPUDevice, T>; \ 100 \ 101 template <> \ 102 void Elu<GPUDevice, T>::operator()(const GPUDevice& d, \ 103 typename TTypes<T>::ConstTensor features, \ 104 typename TTypes<T>::Tensor activations); \ 105 extern template struct Elu<GPUDevice, T>; \ 106 \ 107 template <> \ 108 void EluGrad<GPUDevice, T>::operator()( \ 109 const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \ 110 typename TTypes<T>::ConstTensor activations, \ 111 typename TTypes<T>::Tensor backprops); \ 112 extern template struct EluGrad<GPUDevice, T>; \ 113 \ 114 template <> \ 115 void Selu<GPUDevice, T>::operator()( \ 116 const GPUDevice& d, typename TTypes<T>::ConstTensor features, \ 117 typename TTypes<T>::Tensor activations); \ 118 extern template struct Selu<GPUDevice, T>; \ 119 \ 120 template <> \ 121 void SeluGrad<GPUDevice, T>::operator()( \ 122 const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \ 123 typename TTypes<T>::ConstTensor activations, \ 124 typename TTypes<T>::Tensor backprops); \ 125 extern template struct SeluGrad<GPUDevice, T>; 126 127 TF_CALL_GPU_NUMBER_TYPES(DECLARE_GPU_SPEC); 128 } // namespace functor 129 130 // Registration of the GPU implementations. 131 #define REGISTER_GPU_KERNELS(type) \ 132 REGISTER_KERNEL_BUILDER( \ 133 Name("Relu").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 134 ReluOp<GPUDevice, type>); \ 135 REGISTER_KERNEL_BUILDER( \ 136 Name("ReluGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 137 ReluGradOp<GPUDevice, type>); \ 138 REGISTER_KERNEL_BUILDER( \ 139 Name("Relu6").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 140 Relu6Op<GPUDevice, type>); \ 141 REGISTER_KERNEL_BUILDER( \ 142 Name("Relu6Grad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 143 Relu6GradOp<GPUDevice, type>); \ 144 REGISTER_KERNEL_BUILDER( \ 145 Name("Elu").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 146 EluOp<GPUDevice, type>); \ 147 REGISTER_KERNEL_BUILDER( \ 148 Name("EluGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 149 EluGradOp<GPUDevice, type>); \ 150 REGISTER_KERNEL_BUILDER( \ 151 Name("Selu").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 152 SeluOp<GPUDevice, type>); \ 153 REGISTER_KERNEL_BUILDER( \ 154 Name("SeluGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 155 SeluGradOp<GPUDevice, type>) 156 157 TF_CALL_GPU_NUMBER_TYPES(REGISTER_GPU_KERNELS); 158 #undef REGISTER_GPU_KERNELS 159 160 #endif // GOOGLE_CUDA 161 162 #ifdef TENSORFLOW_USE_SYCL 163 // Registration of the GPU implementations. 164 #define REGISTER_SYCL_KERNELS(type) \ 165 REGISTER_KERNEL_BUILDER( \ 166 Name("Relu").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 167 ReluOp<SYCLDevice, type>); \ 168 REGISTER_KERNEL_BUILDER( \ 169 Name("ReluGrad").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 170 ReluGradOp<SYCLDevice, type>); \ 171 REGISTER_KERNEL_BUILDER( \ 172 Name("Relu6").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 173 Relu6Op<SYCLDevice, type>); \ 174 REGISTER_KERNEL_BUILDER( \ 175 Name("Relu6Grad").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 176 Relu6GradOp<SYCLDevice, type>); \ 177 REGISTER_KERNEL_BUILDER( \ 178 Name("Elu").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 179 EluOp<SYCLDevice, type>); \ 180 REGISTER_KERNEL_BUILDER( \ 181 Name("EluGrad").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 182 EluGradOp<SYCLDevice, type>); \ 183 REGISTER_KERNEL_BUILDER( \ 184 Name("Selu").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 185 SeluOp<SYCLDevice, type>); \ 186 REGISTER_KERNEL_BUILDER( \ 187 Name("SeluGrad").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \ 188 SeluGradOp<SYCLDevice, type>) 189 190 TF_CALL_GPU_NUMBER_TYPES_NO_HALF(REGISTER_SYCL_KERNELS); 191 #undef REGISTER_SYCL_KERNELS 192 #endif // TENSORFLOW_USE_SYCL 193 194 } // namespace tensorflow 195