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/softsign_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/kernels/warn_about_ints.h" 27 #include "tensorflow/core/lib/core/errors.h" 28 29 namespace tensorflow { 30 31 typedef Eigen::ThreadPoolDevice CPUDevice; 32 typedef Eigen::GpuDevice GPUDevice; 33 34 template <typename Device, typename T> 35 class SoftsignOp : public UnaryElementWiseOp<T, SoftsignOp<Device, T>> { 36 public: 37 explicit SoftsignOp(OpKernelConstruction* context) 38 : UnaryElementWiseOp<T, SoftsignOp<Device, T>>(context) { 39 WarnAboutInts(context); 40 } 41 42 void Operate(OpKernelContext* context, const Tensor& input, Tensor* output) { 43 functor::Softsign<Device, T> functor; 44 functor(context->eigen_device<Device>(), input.flat<T>(), 45 output->flat<T>()); 46 } 47 }; 48 49 template <typename Device, typename T> 50 class SoftsignGradOp 51 : public BinaryElementWiseOp<T, SoftsignGradOp<Device, T>> { 52 public: 53 explicit SoftsignGradOp(OpKernelConstruction* context) 54 : BinaryElementWiseOp<T, SoftsignGradOp<Device, T>>(context) { 55 WarnAboutInts(context); 56 } 57 58 void OperateNoTemplate(OpKernelContext* context, const Tensor& g, 59 const Tensor& a, Tensor* output); 60 61 // INPUTS: 62 // g (gradients): backpropagated gradients 63 // a (inputs): inputs that were passed to SoftsignOp() 64 // OUTPUT: 65 // gradients to backprop 66 template <int NDIMS> 67 void Operate(OpKernelContext* context, const Tensor& g, const Tensor& a, 68 Tensor* output) { 69 OperateNoTemplate(context, g, a, output); 70 } 71 }; 72 73 template <typename Device, typename T> 74 void SoftsignGradOp<Device, T>::OperateNoTemplate(OpKernelContext* context, 75 const Tensor& g, 76 const Tensor& a, 77 Tensor* output) { 78 OP_REQUIRES(context, a.IsSameSize(g), 79 errors::InvalidArgument("g and a must be the same size")); 80 functor::SoftsignGrad<Device, T> functor; 81 functor(context->eigen_device<Device>(), g.flat<T>(), a.flat<T>(), 82 output->flat<T>()); 83 } 84 85 #define REGISTER_KERNELS(type) \ 86 REGISTER_KERNEL_BUILDER( \ 87 Name("Softsign").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 88 SoftsignOp<CPUDevice, type>); \ 89 REGISTER_KERNEL_BUILDER( \ 90 Name("SoftsignGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 91 SoftsignGradOp<CPUDevice, type>); 92 93 TF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNELS); 94 #undef REGISTER_KERNELS 95 96 #if GOOGLE_CUDA 97 // Forward declarations of the functor specializations for GPU. 98 namespace functor { 99 #define DECLARE_GPU_SPEC(T) \ 100 template <> \ 101 void Softsign<GPUDevice, T>::operator()( \ 102 const GPUDevice& d, typename TTypes<T>::ConstTensor features, \ 103 typename TTypes<T>::Tensor activations); \ 104 extern template struct Softsign<GPUDevice, T>; \ 105 \ 106 template <> \ 107 void SoftsignGrad<GPUDevice, T>::operator()( \ 108 const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \ 109 typename TTypes<T>::ConstTensor features, \ 110 typename TTypes<T>::Tensor backprops); \ 111 extern template struct SoftsignGrad<GPUDevice, T>; 112 113 TF_CALL_GPU_NUMBER_TYPES(DECLARE_GPU_SPEC); 114 } // namespace functor 115 116 // Registration of the GPU implementations. 117 #define REGISTER_GPU_KERNELS(type) \ 118 REGISTER_KERNEL_BUILDER( \ 119 Name("Softsign").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 120 SoftsignOp<GPUDevice, type>); \ 121 REGISTER_KERNEL_BUILDER( \ 122 Name("SoftsignGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \ 123 SoftsignGradOp<GPUDevice, type>); 124 125 TF_CALL_GPU_NUMBER_TYPES(REGISTER_GPU_KERNELS); 126 #undef REGISTER_GPU_KERNELS 127 128 #endif // GOOGLE_CUDA 129 130 } // namespace tensorflow 131