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 #ifdef TENSORFLOW_USE_SYCL 33 typedef Eigen::SyclDevice SYCLDevice; 34 #endif // TENSORFLOW_USE_SYCL 35 36 // Partial specialization for a CPUDevice, that uses the Eigen implementation 37 // from SoftmaxEigenImpl. 38 namespace functor { 39 template <typename Device, typename T> 40 struct SoftmaxFunctorBase { operator ()tensorflow::functor::SoftmaxFunctorBase41 void operator()(const Device& d, typename TTypes<T>::ConstMatrix logits, 42 typename TTypes<T>::Matrix softmax, const bool log) { 43 SoftmaxEigenImpl<Device, T>::Compute(d, logits, softmax, log); 44 } 45 }; 46 template <typename T> 47 struct SoftmaxFunctor<CPUDevice, T> : SoftmaxFunctorBase<CPUDevice, T> {}; 48 49 #ifdef TENSORFLOW_USE_SYCL 50 template <typename T> 51 struct SoftmaxFunctor<SYCLDevice, T> : SoftmaxFunctorBase<SYCLDevice, T> {}; 52 #endif // TENSORFLOW_USE_SYCL 53 } // namespace functor 54 55 template <typename Device, typename T> 56 class SoftmaxOp : public OpKernel { 57 public: SoftmaxOp(OpKernelConstruction * context)58 explicit SoftmaxOp(OpKernelConstruction* context) : OpKernel(context) { 59 log_ = str_util::StartsWith(type_string(), "Log"); 60 } 61 Compute(OpKernelContext * context)62 void Compute(OpKernelContext* context) override { 63 const Tensor& logits_in = context->input(0); 64 OP_REQUIRES(context, TensorShapeUtils::IsVectorOrHigher(logits_in.shape()), 65 errors::InvalidArgument("logits must have >= 1 dimension, got ", 66 logits_in.shape().DebugString())); 67 Tensor* softmax_out = nullptr; 68 OP_REQUIRES_OK(context, context->forward_input_or_allocate_output( 69 {0}, 0, logits_in.shape(), &softmax_out)); 70 if (logits_in.NumElements() > 0) { 71 functor::SoftmaxFunctor<Device, T> functor; 72 functor(context->eigen_device<Device>(), logits_in.flat_inner_dims<T>(), 73 softmax_out->flat_inner_dims<T>(), log_); 74 } 75 } 76 77 private: 78 bool log_; 79 }; 80 81 #define REGISTER_CPU(T) \ 82 REGISTER_KERNEL_BUILDER( \ 83 Name("Softmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ 84 SoftmaxOp<CPUDevice, T>); 85 TF_CALL_half(REGISTER_CPU); 86 TF_CALL_float(REGISTER_CPU); 87 TF_CALL_double(REGISTER_CPU); 88 89 #undef REGISTER_CPU 90 #define REGISTER_CPU(T) \ 91 REGISTER_KERNEL_BUILDER( \ 92 Name("LogSoftmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ 93 SoftmaxOp<CPUDevice, T>); 94 TF_CALL_half(REGISTER_CPU); 95 TF_CALL_float(REGISTER_CPU); 96 TF_CALL_double(REGISTER_CPU); 97 98 #ifdef TENSORFLOW_USE_SYCL 99 REGISTER_KERNEL_BUILDER( 100 Name("Softmax").Device(DEVICE_SYCL).TypeConstraint<float>("T"), 101 SoftmaxOp<SYCLDevice, float>); 102 REGISTER_KERNEL_BUILDER( 103 Name("Softmax").Device(DEVICE_SYCL).TypeConstraint<double>("T"), 104 SoftmaxOp<SYCLDevice, double>); 105 #endif // TENSORFLOW_USE_SYCL 106 } // namespace tensorflow 107