Home | History | Annotate | Download | only in kernels
      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 "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 #include "tensorflow/core/framework/register_types.h"
     23 #include "tensorflow/core/framework/tensor.h"
     24 #include "tensorflow/core/framework/tensor_shape.h"
     25 #include "tensorflow/core/kernels/softmax_op_functor.h"
     26 
     27 namespace tensorflow {
     28 
     29 typedef Eigen::ThreadPoolDevice CPUDevice;
     30 typedef Eigen::GpuDevice GPUDevice;
     31 #ifdef TENSORFLOW_USE_SYCL
     32 typedef Eigen::SyclDevice SYCLDevice;
     33 #endif  // TENSORFLOW_USE_SYCL
     34 
     35 // Partial specialization for a CPUDevice, that uses the Eigen implementation
     36 // from SoftmaxEigenImpl.
     37 namespace functor {
     38 template <typename Device, typename T>
     39 struct SoftmaxFunctorBase {
     40   void operator()(const Device& d, typename TTypes<T>::ConstMatrix logits,
     41                   typename TTypes<T>::Matrix softmax, const bool log) {
     42     SoftmaxEigenImpl<Device, T>::Compute(d, logits, softmax, log);
     43   }
     44 };
     45 template <typename T>
     46 struct SoftmaxFunctor<CPUDevice, T> : SoftmaxFunctorBase<CPUDevice, T> {};
     47 
     48 #ifdef TENSORFLOW_USE_SYCL
     49 template <typename T>
     50 struct SoftmaxFunctor<SYCLDevice, T> : SoftmaxFunctorBase<SYCLDevice, T> {};
     51 #endif  // TENSORFLOW_USE_SYCL
     52 }  // namespace functor
     53 
     54 template <typename Device, typename T>
     55 class SoftmaxOp : public OpKernel {
     56  public:
     57   explicit SoftmaxOp(OpKernelConstruction* context) : OpKernel(context) {
     58     log_ = StringPiece(type_string()).starts_with("Log");
     59   }
     60 
     61   void Compute(OpKernelContext* context) override {
     62     const Tensor& logits_in = context->input(0);
     63     OP_REQUIRES(context, TensorShapeUtils::IsMatrix(logits_in.shape()),
     64                 errors::InvalidArgument("logits must be 2-dimensional"));
     65     Tensor* softmax_out = nullptr;
     66     OP_REQUIRES_OK(context, context->forward_input_or_allocate_output(
     67                                 {0}, 0, logits_in.shape(), &softmax_out));
     68     if (logits_in.NumElements() > 0) {
     69       functor::SoftmaxFunctor<Device, T> functor;
     70       functor(context->eigen_device<Device>(), logits_in.matrix<T>(),
     71               softmax_out->matrix<T>(), log_);
     72     }
     73   }
     74 
     75  private:
     76   bool log_;
     77 };
     78 
     79 #define REGISTER_CPU(T)                                          \
     80   REGISTER_KERNEL_BUILDER(                                       \
     81       Name("Softmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
     82       SoftmaxOp<CPUDevice, T>);
     83 TF_CALL_half(REGISTER_CPU);
     84 TF_CALL_float(REGISTER_CPU);
     85 TF_CALL_double(REGISTER_CPU);
     86 
     87 #undef REGISTER_CPU
     88 #define REGISTER_CPU(T)                                             \
     89   REGISTER_KERNEL_BUILDER(                                          \
     90       Name("LogSoftmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
     91       SoftmaxOp<CPUDevice, T>);
     92 TF_CALL_half(REGISTER_CPU);
     93 TF_CALL_float(REGISTER_CPU);
     94 TF_CALL_double(REGISTER_CPU);
     95 
     96 #ifdef TENSORFLOW_USE_SYCL
     97 REGISTER_KERNEL_BUILDER(
     98     Name("Softmax").Device(DEVICE_SYCL).TypeConstraint<float>("T"),
     99     SoftmaxOp<SYCLDevice, float>);
    100 REGISTER_KERNEL_BUILDER(
    101     Name("Softmax").Device(DEVICE_SYCL).TypeConstraint<double>("T"),
    102     SoftmaxOp<SYCLDevice, double>);
    103 #endif  // TENSORFLOW_USE_SYCL
    104 }  // namespace tensorflow
    105