Home | History | Annotate | Download | only in kernels
      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/softplus_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 SoftplusOp : public UnaryElementWiseOp<T, SoftplusOp<Device, T>> {
     36  public:
     37   explicit SoftplusOp(OpKernelConstruction* context)
     38       : UnaryElementWiseOp<T, SoftplusOp<Device, T>>(context) {
     39     WarnAboutInts(context);
     40   }
     41 
     42   void Operate(OpKernelContext* context, const Tensor& input, Tensor* output) {
     43     functor::Softplus<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 SoftplusGradOp
     51     : public BinaryElementWiseOp<T, SoftplusGradOp<Device, T>> {
     52  public:
     53   explicit SoftplusGradOp(OpKernelConstruction* context)
     54       : BinaryElementWiseOp<T, SoftplusGradOp<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 SoftplusOp()
     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 template <typename Device, typename T>
     73 void SoftplusGradOp<Device, T>::OperateNoTemplate(OpKernelContext* context,
     74                                                   const Tensor& g,
     75                                                   const Tensor& a,
     76                                                   Tensor* output) {
     77   OP_REQUIRES(context, a.IsSameSize(g),
     78               errors::InvalidArgument("g and a must be the same size"));
     79   functor::SoftplusGrad<Device, T> functor;
     80   functor(context->eigen_device<Device>(), g.flat<T>(), a.flat<T>(),
     81           output->flat<T>());
     82 }
     83 
     84 #define REGISTER_KERNELS(type)                                           \
     85   REGISTER_KERNEL_BUILDER(                                               \
     86       Name("Softplus").Device(DEVICE_CPU).TypeConstraint<type>("T"),     \
     87       SoftplusOp<CPUDevice, type>);                                      \
     88   REGISTER_KERNEL_BUILDER(                                               \
     89       Name("SoftplusGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \
     90       SoftplusGradOp<CPUDevice, type>);
     91 
     92 TF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNELS);
     93 #undef REGISTER_KERNELS
     94 
     95 #if GOOGLE_CUDA
     96 // Forward declarations of the functor specializations for GPU.
     97 namespace functor {
     98 #define DECLARE_GPU_SPEC(T)                                          \
     99   template <>                                                        \
    100   void Softplus<GPUDevice, T>::operator()(                           \
    101       const GPUDevice& d, typename TTypes<T>::ConstTensor features,  \
    102       typename TTypes<T>::Tensor activations);                       \
    103   extern template struct Softplus<GPUDevice, T>;                     \
    104                                                                      \
    105   template <>                                                        \
    106   void SoftplusGrad<GPUDevice, T>::operator()(                       \
    107       const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \
    108       typename TTypes<T>::ConstTensor features,                      \
    109       typename TTypes<T>::Tensor backprops);                         \
    110   extern template struct SoftplusGrad<GPUDevice, T>;
    111 
    112 TF_CALL_GPU_NUMBER_TYPES(DECLARE_GPU_SPEC);
    113 }  // namespace functor
    114 
    115 // Registration of the GPU implementations.
    116 #define REGISTER_GPU_KERNELS(type)                                       \
    117   REGISTER_KERNEL_BUILDER(                                               \
    118       Name("Softplus").Device(DEVICE_GPU).TypeConstraint<type>("T"),     \
    119       SoftplusOp<GPUDevice, type>);                                      \
    120   REGISTER_KERNEL_BUILDER(                                               \
    121       Name("SoftplusGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \
    122       SoftplusGradOp<GPUDevice, type>);
    123 
    124 TF_CALL_GPU_NUMBER_TYPES(REGISTER_GPU_KERNELS);
    125 #undef REGISTER_GPU_KERNELS
    126 
    127 #endif  // GOOGLE_CUDA
    128 
    129 }  // namespace tensorflow
    130