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 #ifndef TENSORFLOW_KERNELS_AVGPOOLING_OP_H_
     17 #define TENSORFLOW_KERNELS_AVGPOOLING_OP_H_
     18 // Functor definition for AvgPoolingOp, must be compilable by nvcc.
     19 
     20 #include "tensorflow/core/framework/tensor_types.h"
     21 #include "tensorflow/core/kernels/eigen_pooling.h"
     22 #include "tensorflow/core/platform/types.h"
     23 
     24 namespace tensorflow {
     25 namespace functor {
     26 
     27 template <typename Device, typename T>
     28 struct SpatialAvgPooling {
     29   void operator()(const Device& d, typename TTypes<T, 4>::Tensor output,
     30                   typename TTypes<T, 4>::ConstTensor input, int window_rows,
     31                   int window_cols, int row_stride, int col_stride,
     32                   const Eigen::PaddingType& padding) {
     33     if (Eigen::internal::is_same<Device, Eigen::GpuDevice>::value) {
     34       // Use 32bit indexing to speed up the computations
     35       To32Bit(output).swap_layout().device(d) = Eigen::SpatialAvgPooling(
     36           To32Bit(input).swap_layout(), window_cols, window_rows, col_stride,
     37           row_stride, padding);
     38     } else {
     39       // Because we swap the layout, we swap the row/cols as well
     40       output.swap_layout().device(d) = Eigen::SpatialAvgPooling(
     41           input.swap_layout(), window_cols, window_rows, col_stride, row_stride,
     42           padding);
     43     }
     44   }
     45 };
     46 
     47 }  // namespace functor
     48 
     49 typedef Eigen::GpuDevice GPUDevice;
     50 
     51 // Launch a custom GPU kernels from Yanqing for the avgpooling backward
     52 // operation that works NHWC data formats. Arguments:
     53 //   top_diff: backprop to the output of the pooling layer
     54 //   num: number of input batches
     55 //   height: input height
     56 //   width: input width
     57 //   channels: number of input channels
     58 //   pooled_height: the height of the output to the pooling layer
     59 //   pooled_width: the width of the output to the pooling layer
     60 //   kernel_h: the height of the pooling kernel
     61 //   kernel_w: the width of the pooling kernel
     62 //   stride_h: the height of the vertical stride
     63 //   stride_w: the width of the horizontal stride
     64 //   pad_t: padding size to the top side
     65 //   pad_l: padding size to the left side
     66 //   bottom_diff: backprop to the input of the pooling layer.
     67 template <typename T>
     68 bool RunAvePoolBackwardNHWC(const T* const top_diff, const int num,
     69                             const int height, const int width,
     70                             const int channels, const int pooled_height,
     71                             const int pooled_width, const int kernel_h,
     72                             const int kernel_w, const int stride_h,
     73                             const int stride_w, const int pad_t,
     74                             const int pad_l, T* const bottom_diff,
     75                             const GPUDevice& d);
     76 
     77 }  // namespace tensorflow
     78 
     79 #endif  // TENSORFLOW_KERNELS_AVGPOOLING_OP_H_
     80