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