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 | #include "tensorflow/core/lib/strings/str_util.h" |
19 | #define EIGEN_USE_THREADS |
20 | |
21 | #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" |
22 | #include "tensorflow/core/framework/op_kernel.h" |
23 | #include "tensorflow/core/framework/register_types.h" |
24 | #include "tensorflow/core/framework/tensor.h" |
25 | #include "tensorflow/core/framework/tensor_shape.h" |
26 | #include "tensorflow/core/kernels/softmax_op_functor.h" |
27 | |
28 | namespace tensorflow { |
29 | |
30 | typedef Eigen::ThreadPoolDevice CPUDevice; |
31 | typedef Eigen::GpuDevice GPUDevice; |
32 | |
33 | // Partial specialization for a CPUDevice, that uses the Eigen implementation |
34 | // from SoftmaxEigenImpl. |
35 | namespace functor { |
36 | template <typename Device, typename T> |
37 | struct SoftmaxFunctorBase { |
38 | void operator()(const Device& d, typename TTypes<T>::ConstMatrix logits, |
39 | typename TTypes<T>::Matrix softmax, const bool log) { |
40 | SoftmaxEigenImpl<Device, T>::Compute(d, logits, softmax, log); |
41 | } |
42 | }; |
43 | template <typename T> |
44 | struct SoftmaxFunctor<CPUDevice, T> : SoftmaxFunctorBase<CPUDevice, T> {}; |
45 | |
46 | } // namespace functor |
47 | |
48 | template <typename Device, typename T> |
49 | class SoftmaxOp : public OpKernel { |
50 | public: |
51 | explicit SoftmaxOp(OpKernelConstruction* context) : OpKernel(context) { |
52 | log_ = absl::StartsWith(type_string(), "Log" ); |
53 | } |
54 | |
55 | void Compute(OpKernelContext* context) override { |
56 | const Tensor& logits_in = context->input(0); |
57 | OP_REQUIRES(context, TensorShapeUtils::IsVectorOrHigher(logits_in.shape()), |
58 | errors::InvalidArgument("logits must have >= 1 dimension, got " , |
59 | logits_in.shape().DebugString())); |
60 | Tensor* softmax_out = nullptr; |
61 | OP_REQUIRES_OK(context, context->forward_input_or_allocate_output( |
62 | {0}, 0, logits_in.shape(), &softmax_out)); |
63 | if (logits_in.NumElements() > 0) { |
64 | functor::SoftmaxFunctor<Device, T> functor; |
65 | functor(context->eigen_device<Device>(), logits_in.flat_inner_dims<T>(), |
66 | softmax_out->flat_inner_dims<T>(), log_); |
67 | } |
68 | } |
69 | |
70 | private: |
71 | bool log_; |
72 | }; |
73 | |
74 | #define REGISTER_CPU(T) \ |
75 | REGISTER_KERNEL_BUILDER( \ |
76 | Name("Softmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ |
77 | SoftmaxOp<CPUDevice, T>); |
78 | TF_CALL_FLOAT_TYPES(REGISTER_CPU); |
79 | |
80 | #undef REGISTER_CPU |
81 | #define REGISTER_CPU(T) \ |
82 | REGISTER_KERNEL_BUILDER( \ |
83 | Name("LogSoftmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ |
84 | SoftmaxOp<CPUDevice, T>); |
85 | TF_CALL_FLOAT_TYPES(REGISTER_CPU); |
86 | |
87 | #undef REGISTER_CPU |
88 | |
89 | } // namespace tensorflow |
90 | |