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