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 #if GOOGLE_CUDA
     17 
     18 #define EIGEN_USE_GPU
     19 
     20 #include "tensorflow/core/kernels/l2loss_op.h"
     21 
     22 #include "tensorflow/core/framework/register_types.h"
     23 
     24 #include "tensorflow/core/kernels/reduction_gpu_kernels.cu.h"
     25 #include "tensorflow/core/kernels/reduction_ops_common.h"
     26 
     27 namespace tensorflow {
     28 
     29 typedef Eigen::GpuDevice GPUDevice;
     30 
     31 // TODO(eriche): can add specialization for half2
     32 template <typename T>
     33 struct squareHalf {
     34   __host__ __device__ T operator()(const T& x) const {
     35     return static_cast<T>(0.5) * x * x;
     36   }
     37 };
     38 
     39 template <typename T>
     40 class L2LossOp<GPUDevice, T> : public OpKernel {
     41  public:
     42   explicit L2LossOp(OpKernelConstruction* context) : OpKernel(context) {}
     43 
     44   void Compute(OpKernelContext* context) override {
     45     // The input tensor can be of any number of dimensions, even though it's
     46     // 2D in most typical applications.
     47     const Tensor& input = context->input(0);
     48     // The output is a single number.
     49     Tensor* output = nullptr;
     50     OP_REQUIRES_OK(context,
     51                    context->allocate_output(0, TensorShape({}), &output));
     52     typedef cub::TransformInputIterator<T, squareHalf<T>, T*> inputIterType;
     53     inputIterType input_itr((T*)input.flat<T>().data(), squareHalf<T>());
     54     typedef const Eigen::array<TTypes<float>::Tensor::Index, 1>& ReductionAxes;
     55 
     56     Constants<GPUDevice> constants;
     57     functor::ReduceImpl<T, cub::Sum, T*, inputIterType, ReductionAxes>(
     58         context, (T*)output->flat<T>().data(), input_itr, 1,
     59         input.flat<T>().size(), 1, 1, 0, constants.kZero, cub::Sum());
     60   }
     61 };
     62 
     63 // Registration of the GPU implementations.
     64 #define REGISTER_GPU_KERNEL(T)                                  \
     65   REGISTER_KERNEL_BUILDER(                                      \
     66       Name("L2Loss").Device(DEVICE_GPU).TypeConstraint<T>("T"), \
     67       L2LossOp<GPUDevice, T>);
     68 
     69 REGISTER_GPU_KERNEL(float);
     70 REGISTER_GPU_KERNEL(double);
     71 REGISTER_GPU_KERNEL(Eigen::half);
     72 #undef REGISTER_GPU_KERNEL
     73 
     74 }  // namespace tensorflow
     75 
     76 #endif  // GOOGLE_CUDA
     77