• 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/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/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 SoftsignOp : public UnaryElementWiseOp<T, SoftsignOp<Device, T>> {
35  public:
SoftsignOp(OpKernelConstruction * context)36   explicit SoftsignOp(OpKernelConstruction* context)
37       : UnaryElementWiseOp<T, SoftsignOp<Device, T>>(context) {}
38 
Operate(OpKernelContext * context,const Tensor & input,Tensor * output)39   void Operate(OpKernelContext* context, const Tensor& input, Tensor* output) {
40     functor::Softsign<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 SoftsignGradOp
48     : public BinaryElementWiseOp<T, SoftsignGradOp<Device, T>> {
49  public:
SoftsignGradOp(OpKernelConstruction * context)50   explicit SoftsignGradOp(OpKernelConstruction* context)
51       : BinaryElementWiseOp<T, SoftsignGradOp<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 SoftsignOp()
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 
68 template <typename Device, typename T>
OperateNoTemplate(OpKernelContext * context,const Tensor & g,const Tensor & a,Tensor * output)69 void SoftsignGradOp<Device, T>::OperateNoTemplate(OpKernelContext* context,
70                                                   const Tensor& g,
71                                                   const Tensor& a,
72                                                   Tensor* output) {
73   OP_REQUIRES(context, a.IsSameSize(g),
74               errors::InvalidArgument("g and a must be the same size"));
75   functor::SoftsignGrad<Device, T> functor;
76   functor(context->eigen_device<Device>(), g.flat<T>(), a.flat<T>(),
77           output->flat<T>());
78 }
79 
80 #define REGISTER_KERNELS(type)                                           \
81   REGISTER_KERNEL_BUILDER(                                               \
82       Name("Softsign").Device(DEVICE_CPU).TypeConstraint<type>("T"),     \
83       SoftsignOp<CPUDevice, type>);                                      \
84   REGISTER_KERNEL_BUILDER(                                               \
85       Name("SoftsignGrad").Device(DEVICE_CPU).TypeConstraint<type>("T"), \
86       SoftsignGradOp<CPUDevice, type>);
87 
88 TF_CALL_FLOAT_TYPES(REGISTER_KERNELS);
89 #undef REGISTER_KERNELS
90 
91 #if GOOGLE_CUDA
92 // Forward declarations of the functor specializations for GPU.
93 namespace functor {
94 #define DECLARE_GPU_SPEC(T)                                          \
95   template <>                                                        \
96   void Softsign<GPUDevice, T>::operator()(                           \
97       const GPUDevice& d, typename TTypes<T>::ConstTensor features,  \
98       typename TTypes<T>::Tensor activations);                       \
99   extern template struct Softsign<GPUDevice, T>;                     \
100                                                                      \
101   template <>                                                        \
102   void SoftsignGrad<GPUDevice, T>::operator()(                       \
103       const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \
104       typename TTypes<T>::ConstTensor features,                      \
105       typename TTypes<T>::Tensor backprops);                         \
106   extern template struct SoftsignGrad<GPUDevice, T>;
107 
108 TF_CALL_GPU_NUMBER_TYPES(DECLARE_GPU_SPEC);
109 }  // namespace functor
110 
111 // Registration of the GPU implementations.
112 #define REGISTER_GPU_KERNELS(type)                                       \
113   REGISTER_KERNEL_BUILDER(                                               \
114       Name("Softsign").Device(DEVICE_GPU).TypeConstraint<type>("T"),     \
115       SoftsignOp<GPUDevice, type>);                                      \
116   REGISTER_KERNEL_BUILDER(                                               \
117       Name("SoftsignGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \
118       SoftsignGradOp<GPUDevice, type>);
119 
120 TF_CALL_GPU_NUMBER_TYPES(REGISTER_GPU_KERNELS);
121 #undef REGISTER_GPU_KERNELS
122 
123 #endif  // GOOGLE_CUDA
124 
125 }  // namespace tensorflow
126