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 || TENSORFLOW_USE_ROCM
92 // Forward declarations of the functor specializations for GPU.
93 namespace functor {
94 #define DECLARE_SOFTSIGN_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 #define DECLARE_SOFTSIGN_GRAD_GPU_SPEC(T) \
102 template <> \
103 void SoftsignGrad<GPUDevice, T>::operator()( \
104 const GPUDevice& d, typename TTypes<T>::ConstTensor gradients, \
105 typename TTypes<T>::ConstTensor features, \
106 typename TTypes<T>::Tensor backprops); \
107 extern template struct SoftsignGrad<GPUDevice, T>;
108
109 #if !defined(MLIR_GENERATED_GPU_KERNELS_ENABLED)
110 TF_CALL_GPU_NUMBER_TYPES(DECLARE_SOFTSIGN_GPU_SPEC);
111 #endif
112 TF_CALL_GPU_NUMBER_TYPES(DECLARE_SOFTSIGN_GRAD_GPU_SPEC);
113 } // namespace functor
114
115 // Registration of the GPU implementations.
116 #define REGISTER_SOFTSIGN_GPU_KERNELS(type) \
117 REGISTER_KERNEL_BUILDER( \
118 Name("Softsign").Device(DEVICE_GPU).TypeConstraint<type>("T"), \
119 SoftsignOp<GPUDevice, type>);
120
121 #define REGISTER_SOFTSIGN_GRAD_GPU_KERNELS(type) \
122 REGISTER_KERNEL_BUILDER( \
123 Name("SoftsignGrad").Device(DEVICE_GPU).TypeConstraint<type>("T"), \
124 SoftsignGradOp<GPUDevice, type>);
125
126 #if !defined(MLIR_GENERATED_GPU_KERNELS_ENABLED)
127 TF_CALL_GPU_NUMBER_TYPES(REGISTER_SOFTSIGN_GPU_KERNELS);
128 #endif
129 TF_CALL_GPU_NUMBER_TYPES(REGISTER_SOFTSIGN_GRAD_GPU_KERNELS);
130
131 #undef REGISTER_SOFTSIGN_GPU_KERNELS
132 #undef REGISTER_SOFTSIGN_GRAD_GPU_KERNELS
133
134 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
135
136 } // namespace tensorflow
137