/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #ifndef TENSORFLOW_KERNELS_CONV_2D_H_ #define TENSORFLOW_KERNELS_CONV_2D_H_ #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" #include "tensorflow/core/framework/tensor_types.h" #include "tensorflow/core/kernels/eigen_backward_spatial_convolutions.h" #include "tensorflow/core/kernels/eigen_spatial_convolutions.h" #include "tensorflow/core/util/tensor_format.h" namespace tensorflow { namespace functor { // TODO(yangke): revisit these operations and in particular, see if we can // combine all of them into just one operation without causing nvcc to // timeout. template struct ShuffleAndReverse { void operator()(const Device& d, typename TTypes::ConstTensor input, const Eigen::DSizes& order, const Eigen::array& reverse_dims, typename TTypes::Tensor output) { output.device(d) = input.shuffle(order).reverse(reverse_dims); } }; template struct InflatePadAndShuffle { void operator()( const Device& d, typename TTypes::ConstTensor input, const Eigen::DSizes& strides, const Eigen::array, Dims>& pad_dims, const Eigen::DSizes& order, typename TTypes::Tensor output) { output.device(d) = input.inflate(strides).pad(pad_dims).shuffle(order); } }; template void SpatialConvolutionFunc(const Device& d, Output output, Input input, Filter filter, int row_stride, int col_stride, const Eigen::PaddingType& padding) { // Need to swap row/col when calling Eigen. output.device(d) = Eigen::SpatialConvolution(input, filter, col_stride, row_stride, padding); } template struct SpatialConvolution { void operator()(const Device& d, typename TTypes::Tensor output, typename TTypes::ConstTensor input, typename TTypes::ConstTensor filter, int row_stride, int col_stride, const Eigen::PaddingType& padding) { SpatialConvolutionFunc(d, output, input, filter, row_stride, col_stride, padding); } }; template struct SpatialConvolution { void operator()(const Device& d, typename TTypes::Tensor output, typename TTypes::ConstTensor input, typename TTypes::ConstTensor filter, int row_stride, int col_stride, const Eigen::PaddingType& padding) { output.device(d) = Eigen::SpatialConvolution(input.cast(), filter.cast(), col_stride, row_stride, padding) .cast(); } }; template struct SpatialConvolutionBackwardInput { void operator()(const Device& d, typename TTypes::Tensor input_backward, typename TTypes::ConstTensor kernel, typename TTypes::ConstTensor output_backward, int input_rows, int input_cols, int row_stride, int col_stride) { // Need to swap row/col when calling Eigen. input_backward.device(d) = Eigen::SpatialConvolutionBackwardInput( kernel, output_backward, input_cols, input_rows, col_stride, row_stride); } }; template struct SpatialConvolutionBackwardKernel { void operator()(const Device& d, typename TTypes::Tensor kernel_backward, typename TTypes::ConstTensor input, typename TTypes::ConstTensor output_backward, int kernel_rows, int kernel_cols, int row_stride, int col_stride) { // Need to swap row/col when calling Eigen. kernel_backward.device(d) = Eigen::SpatialConvolutionBackwardKernel( input, output_backward, kernel_cols, kernel_rows, col_stride, row_stride); } }; // TODO(vrv): Figure out how to use the MatMulFunctor in matmul_op.h. // My initial attempt to do this compiled but failed in the pytest // due to a swigdeps error. template struct MatMulConvFunctor { // Computes on device "d": out = in0 * in1, where * is matrix // multiplication. void operator()( const Device& d, typename TTypes::Tensor out, typename TTypes::ConstTensor in0, typename TTypes::ConstTensor in1, const Eigen::array, 1>& dim_pair) { out.device(d) = in0.contract(in1, dim_pair); } }; // Shuffles a filter tensor from: // [, in, out] // to: // [out, in, ] template struct TransformFilter { void operator()(const Device& d, typename TTypes::ConstTensor in, typename TTypes::Tensor out) { // We want a 3, 2, 0, 1 shuffle. Merge the spatial dimensions together // to speed up the shuffle operation. Eigen::DSizes merged_dims; merged_dims[0] = in.dimension(0); // spatial dimensions for (int i = 1; i < NDIMS - 2; ++i) { merged_dims[0] *= in.dimension(i); } merged_dims[1] = in.dimension(NDIMS - 2); // input filters merged_dims[2] = in.dimension(NDIMS - 1); // output filters Eigen::DSizes expanded_dims; expanded_dims[0] = in.dimension(NDIMS - 1); // output filters expanded_dims[1] = in.dimension(NDIMS - 2); // input filters for (int i = 0; i < NDIMS; ++i) { // spatial dimensions expanded_dims[i + 2] = in.dimension(i); } out.device(d) = in.reshape(merged_dims) .shuffle(Eigen::DSizes(2, 1, 0)) .reshape(expanded_dims); } }; template struct TransformDepth { void operator()(const Device& d, typename TTypes::ConstTensor in, const Eigen::DSizes& shuffle, typename TTypes::Tensor out) { Eigen::DSizes merged_dims; Eigen::DSizes expanded_dims; Eigen::DSizes new_shuffle; // Merge dimensions that won't be shuffled together to speed things up. if (shuffle[1] == 2 && shuffle[2] == 3) { merged_dims[0] = in.dimension(0); merged_dims[1] = in.dimension(1); merged_dims[2] = in.dimension(2) * in.dimension(3); new_shuffle[0] = shuffle[0]; new_shuffle[1] = 2; new_shuffle[2] = shuffle[3]; expanded_dims[0] = in.dimension(shuffle[0]); expanded_dims[1] = in.dimension(2); expanded_dims[2] = in.dimension(3); expanded_dims[3] = in.dimension(shuffle[3]); } else if (shuffle[0] == 2 && shuffle[1] == 3) { merged_dims[0] = in.dimension(0); merged_dims[1] = in.dimension(1); merged_dims[2] = in.dimension(2) * in.dimension(3); new_shuffle[0] = 2; new_shuffle[1] = shuffle[2]; new_shuffle[2] = shuffle[3]; expanded_dims[0] = in.dimension(2); expanded_dims[1] = in.dimension(3); expanded_dims[2] = in.dimension(shuffle[2]); expanded_dims[3] = in.dimension(shuffle[3]); } else if (shuffle[0] == 0 && shuffle[1] == 3 && shuffle[2] == 1 && shuffle[3] == 2) { merged_dims[0] = in.dimension(0); merged_dims[1] = in.dimension(1) * in.dimension(2); merged_dims[2] = in.dimension(3); new_shuffle[0] = 0; new_shuffle[1] = 2; new_shuffle[2] = 1; expanded_dims[0] = in.dimension(0); expanded_dims[1] = in.dimension(3); expanded_dims[2] = in.dimension(1); expanded_dims[3] = in.dimension(2); } else { assert(false && "unexpected shuffle"); } out.device(d) = in.reshape(merged_dims).shuffle(new_shuffle).reshape(expanded_dims); } }; template struct PadInput { void operator()(const Device& d, typename TTypes::ConstTensor in, const std::array& padding_left, const std::array& padding_right, typename TTypes::Tensor out, TensorFormat format) { Eigen::array, NDIMS> padding; padding[GetTensorDimIndex(format, 'N')] = std::make_pair(0, 0); for (int i = 0; i < NDIMS - 2; ++i) { padding[GetTensorDimIndex(format, '0' + i)] = std::make_pair(padding_left[i], padding_right[i]); } padding[GetTensorDimIndex(format, 'C')] = std::make_pair(0, 0); out.device(d) = in.pad(padding); } }; // Converts a tensor from: // [batch, , filters] // to: // [batch, filters, ] template struct NHWCToNCHW { void operator()(const Device& d, typename TTypes::ConstTensor in, typename TTypes::Tensor out); }; // Converts a tensor from: // [batch, filters, ] // to: // [batch, , filters] template struct NCHWToNHWC { void operator()(const Device& d, typename TTypes::ConstTensor in, typename TTypes::Tensor out); }; // Reverses the effect of TransformFilter above. template struct ReverseTransformFilter { void operator()(const Device& d, typename TTypes::ConstTensor in, typename TTypes::Tensor out); }; } // namespace functor template class ConvAlgorithmMap; template <> class ConvAlgorithmMap {}; } // namespace tensorflow #endif // TENSORFLOW_KERNELS_CONV_2D_H_