/* 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. ==============================================================================*/ // See docs in ../ops/nn_ops.cc. #define USE_EIGEN_TENSOR #define EIGEN_USE_THREADS #if GOOGLE_CUDA #define EIGEN_USE_GPU #endif // GOOGLE_CUDA #include "tensorflow/core/kernels/conv_ops.h" #include #include #include #include "tensorflow/core/framework/numeric_op.h" #include "tensorflow/core/framework/op_kernel.h" #include "tensorflow/core/framework/register_types.h" #include "tensorflow/core/framework/tensor.h" #include "tensorflow/core/framework/tensor_shape.h" #include "tensorflow/core/framework/tensor_slice.h" #include "tensorflow/core/kernels/bounds_check.h" #include "tensorflow/core/kernels/conv_2d.h" #include "tensorflow/core/kernels/deep_conv2d.h" #include "tensorflow/core/kernels/ops_util.h" #include "tensorflow/core/lib/core/errors.h" #include "tensorflow/core/lib/gtl/array_slice.h" #include "tensorflow/core/lib/strings/numbers.h" #include "tensorflow/core/lib/strings/str_util.h" #include "tensorflow/core/platform/logging.h" #include "tensorflow/core/platform/macros.h" #include "tensorflow/core/util/padding.h" #include "tensorflow/core/util/tensor_format.h" #include "tensorflow/core/util/use_cudnn.h" #ifdef TENSORFLOW_USE_LIBXSMM_CONVOLUTIONS #include "tensorflow/core/kernels/xsmm_conv2d.h" #endif #if GOOGLE_CUDA #include "tensorflow/core/kernels/conv_ops_gpu.h" #include "tensorflow/core/platform/stream_executor.h" #endif // GOOGLE_CUDA namespace tensorflow { typedef Eigen::ThreadPoolDevice CPUDevice; typedef Eigen::GpuDevice GPUDevice; namespace { template struct LaunchGeneric { void operator()(OpKernelContext* ctx, const Tensor& input, const Tensor& filter, int row_stride, int col_stride, int row_dilation, int col_dilation, const Padding& padding, Tensor* output, TensorFormat data_format) { CHECK(data_format == FORMAT_NHWC) << "Generic conv implementation only " "supports NHWC tensor format for now."; if (filter.dim_size(0) == 1 && filter.dim_size(1) == 1 && row_stride == 1 && col_stride == 1) { // For 1x1 kernel, the 2D convolution is reduced to matrix // multiplication. // // TODO(vrv): We should be able to call SpatialConvolution // and it will produce the same result, but doing so // led to NaNs during training. Using matmul instead for now. int conv_width = 1; // Width for the convolution step. for (int i = 0; i < 3; ++i) { conv_width *= output->dim_size(i); } Eigen::array, 1> dim_pair; dim_pair[0] = Eigen::IndexPair(1, 0); functor::MatMulConvFunctor()( ctx->eigen_device(), output->shaped({conv_width, filter.dim_size(3)}), input.shaped({conv_width, filter.dim_size(2)}), filter.shaped({filter.dim_size(2), filter.dim_size(3)}), dim_pair); } else if (filter.dim_size(0) == input.dim_size(1) && filter.dim_size(1) == input.dim_size(2) && row_dilation == 1 && col_dilation == 1 && padding == VALID) { // If the input data and filter have the same height/width, // the 2D convolution is reduced to matrix multiplication. const int k = // Length of reduction dimension. filter.dim_size(0) * filter.dim_size(1) * filter.dim_size(2); Eigen::array, 1> dim_pair; dim_pair[0] = Eigen::IndexPair(1, 0); functor::MatMulConvFunctor()( ctx->eigen_device(), output->shaped({input.dim_size(0), filter.dim_size(3)}), input.shaped({input.dim_size(0), k}), filter.shaped({k, filter.dim_size(3)}), dim_pair); } else { functor::SpatialConvolution()( ctx->eigen_device(), output->tensor(), input.tensor(), filter.tensor(), row_stride, col_stride, row_dilation, col_dilation, BrainPadding2EigenPadding(padding)); } } }; } // namespace template struct LaunchConv2DOp { void operator()(OpKernelContext* ctx, bool use_cudnn, bool cudnn_use_autotune, const Tensor& input, const Tensor& filter, int row_dilation, int col_dilation, int row_stride, int col_stride, const Padding& padding, Tensor* output, TensorFormat data_format) { if (data_format != FORMAT_NHWC) { ctx->SetStatus( errors::Unimplemented("Generic conv implementation only supports " "NHWC tensor format for now.")); return; } const int64 in_depth = GetTensorDim(input, data_format, 'C'); OP_REQUIRES(ctx, in_depth == filter.dim_size(2), errors::Unimplemented("Generic conv implementation does not " "support grouped convolutions for now.")); LaunchGeneric()(ctx, input, filter, row_stride, col_stride, row_dilation, col_dilation, padding, output, data_format); } }; template class LaunchDeepConvOp { public: static bool Run(OpKernelContext* ctx, const Tensor& input, const Tensor& filter, int batch, int input_rows, int input_cols, int in_depth, int filter_rows, int filter_cols, int pad_rows, int pad_cols, int out_rows, int /*out_cols*/, int /*out_depth*/, int /*dilation_rows*/, int /*dilation_cols*/, int /*stride_rows*/, int /*stride_cols*/, Tensor* /*output*/, TensorFormat /*data_format*/) { return false; } }; // Conditionally launches DeepConv operation based on convolution parameters. template <> class LaunchDeepConvOp { public: static bool Run(OpKernelContext* ctx, const Tensor& input, const Tensor& filter, int batch, int input_rows, int input_cols, int in_depth, int filter_rows, int filter_cols, int pad_rows, int pad_cols, int out_rows, int out_cols, int out_depth, int dilation_rows, int dilation_cols, int stride_rows, int stride_cols, Tensor* output, TensorFormat data_format) { if (data_format != FORMAT_NHWC || dilation_rows != 1 || dilation_cols != 1 || !CanUseDeepConv2D(stride_rows, stride_cols, filter_rows, filter_cols, in_depth, out_depth, out_rows, out_cols)) { return false; } Conv2DArgs args; args.batch = batch; args.in_rows = input_rows; args.in_cols = input_cols; args.in_depth = in_depth; args.filter_rows = filter_rows; args.filter_cols = filter_cols; args.pad_rows = pad_rows; args.pad_cols = pad_cols; args.out_rows = out_rows; args.out_cols = out_cols; args.out_depth = out_depth; auto input_ptr = input.template flat().data(); auto filter_ptr = filter.template flat().data(); auto output_ptr = output->template flat().data(); functor::DeepConv2D()(ctx, args, input_ptr, filter_ptr, output_ptr); return true; } }; #ifdef TENSORFLOW_USE_LIBXSMM_CONVOLUTIONS template class LaunchXsmmConvOp { public: static bool Run(OpKernelContext* ctx, const Tensor& input, const Tensor& filter, int batch, int input_rows, int input_cols, int in_depth, int filter_rows, int filter_cols, int pad_rows, int pad_cols, int out_rows, int out_cols, int out_depth, int stride_rows, int stride_cols, int dilation_rows, int dilation_cols, Tensor* output, TensorFormat data_format) { return false; } }; template <> class LaunchXsmmConvOp { public: static bool Run(OpKernelContext* ctx, const Tensor& input, const Tensor& filter, int batch, int input_rows, int input_cols, int in_depth, int filter_rows, int filter_cols, int pad_rows, int pad_cols, int out_rows, int out_cols, int out_depth, int dilation_rows, int dilation_cols, int stride_rows, int stride_cols, Tensor* output, TensorFormat data_format) { auto num_threads = ctx->device()->tensorflow_cpu_worker_threads()->num_threads; // See libxsmm_dnn.h for this struct definition. libxsmm_dnn_conv_desc desc; desc.N = batch; desc.C = in_depth; desc.H = input_rows; desc.W = input_cols; desc.K = out_depth; desc.R = filter_rows; desc.S = filter_cols; desc.u = stride_rows; desc.v = stride_cols; desc.pad_h = pad_rows; desc.pad_w = pad_cols; desc.pad_h_in = 0; desc.pad_w_in = 0; desc.pad_h_out = 0; desc.pad_w_out = 0; desc.threads = num_threads; desc.algo = LIBXSMM_DNN_CONV_ALGO_DIRECT; desc.buffer_format = LIBXSMM_DNN_TENSOR_FORMAT_NHWC; desc.filter_format = LIBXSMM_DNN_TENSOR_FORMAT_LIBXSMM; desc.fuse_ops = LIBXSMM_DNN_CONV_FUSE_NONE; desc.options = LIBXSMM_DNN_CONV_OPTION_WU_EXT_FILTER_REDUCE_OVERWRITE; desc.datatype = LIBXSMM_DNN_DATATYPE_F32; if (dilation_rows != 1 || dilation_cols != 1 || !CanUseXsmmConv2D(desc, data_format)) { return false; } auto input_ptr = input.template flat().data(); auto filter_ptr = filter.template flat().data(); auto output_ptr = output->template flat().data(); bool success = functor::XsmmFwdConv2D()( ctx, desc, input_ptr, filter_ptr, output_ptr); return success; } }; #endif #define TF_REQUIRES(EXP, STATUS) \ do { \ if (!TF_PREDICT_TRUE(EXP)) return (STATUS); \ } while (false) Status InitConv2DParameters(const OpKernelConstruction* context, Conv2DParameters* params) { TF_RETURN_IF_ERROR(context->GetAttr("dilations", ¶ms->dilations)); TF_RETURN_IF_ERROR(context->GetAttr("strides", ¶ms->strides)); TF_RETURN_IF_ERROR(context->GetAttr("padding", ¶ms->padding)); string data_format_string; TF_RETURN_IF_ERROR(context->GetAttr("data_format", &data_format_string)); TF_REQUIRES(FormatFromString(data_format_string, ¶ms->data_format), errors::InvalidArgument("Invalid data format")); const auto& strides = params->strides; const auto& dilations = params->dilations; const auto& data_format = params->data_format; TF_REQUIRES(dilations.size() == 4, errors::InvalidArgument("Sliding window dilations field must " "specify 4 dimensions")); TF_REQUIRES(strides.size() == 4, errors::InvalidArgument("Sliding window strides field must " "specify 4 dimensions")); const int64 stride_n = GetTensorDim(strides, data_format, 'N'); const int64 stride_c = GetTensorDim(strides, data_format, 'C'); const int64 stride_h = GetTensorDim(strides, data_format, 'H'); const int64 stride_w = GetTensorDim(strides, data_format, 'W'); TF_REQUIRES( stride_n == 1 && stride_c == 1, errors::InvalidArgument("Current implementation does not yet support " "strides in the batch and depth dimensions.")); TF_REQUIRES(stride_h > 0 && stride_w > 0, errors::InvalidArgument( "Row and column strides should be larger than 0.")); const int64 dilation_n = GetTensorDim(dilations, data_format, 'N'); const int64 dilation_c = GetTensorDim(dilations, data_format, 'C'); const int64 dilation_h = GetTensorDim(dilations, data_format, 'H'); const int64 dilation_w = GetTensorDim(dilations, data_format, 'W'); TF_REQUIRES( dilation_n == 1 && dilation_c == 1, errors::InvalidArgument("Current implementation does not yet support " "dilations in the batch and depth dimensions.")); TF_REQUIRES( dilation_h > 0 && dilation_w > 0, errors::InvalidArgument("Dilated rates should be larger than 0.")); return Status::OK(); } Status ComputeConv2DDimension(const Conv2DParameters& params, const Tensor& input, const Tensor& filter, Conv2DDimensions* dimensions) { // Check that 2D convolution input and filter have exactly 4 dimensions. TF_REQUIRES(input.dims() == 4, errors::InvalidArgument("input must be 4-dimensional", input.shape().DebugString())); TF_REQUIRES(filter.dims() == 4, errors::InvalidArgument("filter must be 4-dimensional: ", filter.shape().DebugString())); for (int i = 0; i < 3; i++) { TF_REQUIRES( FastBoundsCheck(filter.dim_size(i), std::numeric_limits::max()), errors::InvalidArgument("filter too large")); } // The last dimension for input is in_depth. Check that it is the same as the // filter's in_depth or it is evenly divisible by filter's in_depth. const int64 in_depth_raw = GetTensorDim(input, params.data_format, 'C'); const int64 patch_depth_raw = filter.dim_size(2); TF_REQUIRES(FastBoundsCheck(in_depth_raw, std::numeric_limits::max()), errors::InvalidArgument("Input depth too large")); TF_REQUIRES(FastBoundsCheck(patch_depth_raw, std::numeric_limits::max()), errors::InvalidArgument("Patch depth too large")); const int in_depth = static_cast(in_depth_raw); const int patch_depth = static_cast(patch_depth_raw); TF_REQUIRES(in_depth % patch_depth == 0, errors::InvalidArgument( "input depth must be evenly divisible by filter depth: ", in_depth, " vs ", patch_depth)); // The last dimension for filter is out_depth. const int out_depth = static_cast(filter.dim_size(3)); // The second dimension for input is rows/height. // The first dimension for filter is rows/height. const int64 input_rows_raw = GetTensorDim(input, params.data_format, 'H'); TF_REQUIRES(FastBoundsCheck(input_rows_raw, std::numeric_limits::max()), errors::InvalidArgument("Input rows too large")); const int input_rows = static_cast(input_rows_raw); const int filter_rows = static_cast(filter.dim_size(0)); // The third dimension for input is columns/width. // The second dimension for filter is columns/width. const int64 input_cols_raw = GetTensorDim(input, params.data_format, 'W'); TF_REQUIRES(FastBoundsCheck(input_cols_raw, std::numeric_limits::max()), errors::InvalidArgument("Input cols too large")); const int input_cols = static_cast(input_cols_raw); const int filter_cols = static_cast(filter.dim_size(1)); // The first dimension for input is batch. const int64 batch_raw = GetTensorDim(input, params.data_format, 'N'); TF_REQUIRES(FastBoundsCheck(batch_raw, std::numeric_limits::max()), errors::InvalidArgument("batch is too large")); const int batch = static_cast(batch_raw); // Take the stride and dilation from the second and third dimensions only (we // do not support striding or dilation on the batch or depth dimension). const int stride_rows = GetTensorDim(params.strides, params.data_format, 'H'); const int stride_cols = GetTensorDim(params.strides, params.data_format, 'W'); const int dilation_rows = GetTensorDim(params.dilations, params.data_format, 'H'); const int dilation_cols = GetTensorDim(params.dilations, params.data_format, 'W'); // Compute windowed output sizes for rows and columns. int64 out_rows = 0, out_cols = 0, pad_rows = 0, pad_cols = 0; TF_RETURN_IF_ERROR(GetWindowedOutputSizeV2( input_rows, filter_rows, dilation_rows, stride_rows, params.padding, &out_rows, &pad_rows)); TF_RETURN_IF_ERROR(GetWindowedOutputSizeV2( input_cols, filter_cols, dilation_cols, stride_cols, params.padding, &out_cols, &pad_cols)); dimensions->batch = batch; dimensions->input_rows = input_rows; dimensions->input_cols = input_cols; dimensions->in_depth = in_depth; dimensions->filter_rows = filter_rows; dimensions->filter_cols = filter_cols; dimensions->patch_depth = patch_depth; dimensions->out_depth = out_depth; dimensions->stride_rows = stride_rows; dimensions->stride_cols = stride_cols; dimensions->dilation_rows = dilation_rows; dimensions->dilation_cols = dilation_cols; dimensions->out_rows = out_rows; dimensions->out_cols = out_cols; dimensions->pad_rows = pad_rows; dimensions->pad_cols = pad_cols; return Status::OK(); } #undef TF_REQUIRES template class Conv2DOp : public BinaryOp { public: explicit Conv2DOp(OpKernelConstruction* context) : BinaryOp(context) { OP_REQUIRES_OK(context, InitConv2DParameters(context, ¶ms_)); OP_REQUIRES_OK(context, context->GetAttr("use_cudnn_on_gpu", &use_cudnn_)); use_cudnn_ &= CanUseCudnn(); cudnn_use_autotune_ = CudnnUseAutotune(); } void Compute(OpKernelContext* context) override { // Input tensor is of the following dimensions: // [ batch, in_rows, in_cols, in_depth ] const Tensor& input = context->input(0); // Input filter is of the following dimensions: // [ filter_rows, filter_cols, in_depth, out_depth] const Tensor& filter = context->input(1); Conv2DDimensions dimensions; OP_REQUIRES_OK(context, ComputeConv2DDimension(params_, input, filter, &dimensions)); TensorShape out_shape = ShapeFromFormat( params_.data_format, dimensions.batch, dimensions.out_rows, dimensions.out_cols, dimensions.out_depth); // Output tensor is of the following dimensions: // [ in_batch, out_rows, out_cols, out_depth ] Tensor* output = nullptr; OP_REQUIRES_OK(context, context->allocate_output(0, out_shape, &output)); VLOG(2) << "Conv2D: in_depth = " << dimensions.in_depth << ", patch_depth = " << dimensions.patch_depth << ", input_cols = " << dimensions.input_cols << ", filter_cols = " << dimensions.filter_cols << ", input_rows = " << dimensions.input_rows << ", filter_rows = " << dimensions.filter_rows << ", stride_rows = " << dimensions.stride_rows << ", stride_cols = " << dimensions.stride_cols << ", dilation_rows = " << dimensions.dilation_rows << ", dilation_cols = " << dimensions.dilation_cols << ", out_depth = " << dimensions.out_depth; // If there is nothing to compute, return. if (out_shape.num_elements() == 0) { return; } #ifdef TENSORFLOW_USE_LIBXSMM_CONVOLUTIONS if (LaunchXsmmConvOp::Run( context, input, filter, dimensions.batch, dimensions.input_rows, dimensions.input_cols, dimensions.in_depth, dimensions.filter_rows, dimensions.filter_cols, dimensions.pad_rows, dimensions.pad_cols, dimensions.out_rows, dimensions.out_cols, dimensions.out_depth, dimensions.dilation_rows, dimensions.dilation_cols, dimensions.stride_rows, dimensions.stride_cols, output, params_.data_format)) { return; } #endif if (LaunchDeepConvOp::Run( context, input, filter, dimensions.batch, dimensions.input_rows, dimensions.input_cols, dimensions.in_depth, dimensions.filter_rows, dimensions.filter_cols, dimensions.pad_rows, dimensions.pad_cols, dimensions.out_rows, dimensions.out_cols, dimensions.out_depth, dimensions.dilation_rows, dimensions.dilation_cols, dimensions.stride_rows, dimensions.stride_cols, output, params_.data_format)) { return; } launcher_(context, use_cudnn_, cudnn_use_autotune_, input, filter, dimensions.dilation_rows, dimensions.dilation_cols, dimensions.stride_rows, dimensions.stride_cols, params_.padding, output, params_.data_format); } private: Conv2DParameters params_; bool use_cudnn_; bool cudnn_use_autotune_; LaunchConv2DOp launcher_; TF_DISALLOW_COPY_AND_ASSIGN(Conv2DOp); }; #define REGISTER_CPU(T) \ REGISTER_KERNEL_BUILDER( \ Name("Conv2D").Device(DEVICE_CPU).TypeConstraint("T"), \ Conv2DOp); // If we're using the alternative GEMM-based implementation of Conv2D for the // CPU implementation, don't register this EigenTensor-based version. #if !defined(USE_GEMM_FOR_CONV) TF_CALL_half(REGISTER_CPU); TF_CALL_float(REGISTER_CPU); TF_CALL_double(REGISTER_CPU); #endif // USE_GEMM_FOR_CONV // To be used inside depthwise_conv_op.cc. template struct LaunchConv2DOp; template struct LaunchConv2DOp; template struct LaunchConv2DOp; #if GOOGLE_CUDA int64 GetCudnnWorkspaceLimit(const string& envvar_in_mb, int64 default_value_in_bytes) { const char* workspace_limit_in_mb_str = getenv(envvar_in_mb.c_str()); if (workspace_limit_in_mb_str != nullptr && strcmp(workspace_limit_in_mb_str, "") != 0) { int64 scratch_limit_in_mb = -1; if (strings::safe_strto64(workspace_limit_in_mb_str, &scratch_limit_in_mb)) { return scratch_limit_in_mb * (1 << 20); } else { LOG(WARNING) << "Invalid value for env-var " << envvar_in_mb << ": " << workspace_limit_in_mb_str; } } return default_value_in_bytes; } // A dummy type to group forward convolution autotune results together. struct ConvAutoTuneGroup { static string name() { return "Conv"; } }; typedef AutoTuneSingleton AutoTuneConv; template void LaunchConv2DOp::operator()( OpKernelContext* ctx, bool use_cudnn, bool cudnn_use_autotune, const Tensor& input_param, const Tensor& filter, int row_dilation, int col_dilation, int row_stride, int col_stride, const Padding& padding, Tensor* output, TensorFormat data_format) { using se::dnn::AlgorithmConfig; using se::dnn::AlgorithmDesc; using se::dnn::ProfileResult; auto* stream = ctx->op_device_context()->stream(); OP_REQUIRES(ctx, stream, errors::Internal("No GPU stream available.")); if (!use_cudnn) { ctx->SetStatus( errors::Unimplemented("Conv2D for GPU is not currently supported " "without cudnn")); return; } Tensor input = input_param; const int64 in_batch = GetTensorDim(input, data_format, 'N'); int64 in_rows = GetTensorDim(input, data_format, 'H'); int64 in_cols = GetTensorDim(input, data_format, 'W'); const int64 in_depths = GetTensorDim(input, data_format, 'C'); const int64 patch_rows = filter.dim_size(0); const int64 patch_cols = filter.dim_size(1); const int64 patch_depths = filter.dim_size(2); // If the filter in-depth (patch_depths) is 1 and smaller than the input // depth, it's a depthwise convolution. More generally, if the filter in-depth // divides but is smaller than the input depth, it is a grouped convolution. bool is_grouped_convolution = patch_depths != in_depths; if (patch_rows == 1 && patch_cols == 1 && !is_grouped_convolution && row_dilation == 1 && col_dilation == 1 && row_stride == 1 && col_stride == 1 && data_format == FORMAT_NHWC) { // 1x1 filter, so call cublas directly. const uint64 m = in_batch * in_rows * in_cols; const uint64 k = patch_depths; const uint64 n = filter.dim_size(3); auto a_ptr = AsDeviceMemory(input.template flat().data(), input.template flat().size()); auto b_ptr = AsDeviceMemory(filter.template flat().data(), filter.template flat().size()); auto c_ptr = AsDeviceMemory(output->template flat().data(), output->template flat().size()); auto no_transpose = se::blas::Transpose::kNoTranspose; bool blas_launch_status = stream ->ThenBlasGemm(no_transpose, no_transpose, n, m, k, 1.0f, b_ptr, n, a_ptr, k, 0.0f, &c_ptr, n) .ok(); if (!blas_launch_status) { ctx->SetStatus(errors::Internal("Blas SGEMM launch failed : m=", m, ", n=", n, ", k=", k)); } return; } else if (patch_rows == in_rows && patch_cols == in_cols && !is_grouped_convolution && row_dilation == 1 && col_dilation == 1 && padding == VALID && data_format == FORMAT_NHWC) { // The input data and filter have the same height/width, so call cublas // directly. const uint64 m = in_batch; const uint64 k = patch_rows * patch_cols * patch_depths; const uint64 n = filter.dim_size(3); auto a_ptr = AsDeviceMemory(input.template flat().data(), input.template flat().size()); auto b_ptr = AsDeviceMemory(filter.template flat().data(), filter.template flat().size()); auto c_ptr = AsDeviceMemory(output->template flat().data(), output->template flat().size()); auto no_transpose = se::blas::Transpose::kNoTranspose; bool blas_launch_status = stream ->ThenBlasGemm(no_transpose, no_transpose, n, m, k, 1.0f, b_ptr, n, a_ptr, k, 0.0f, &c_ptr, n) .ok(); if (!blas_launch_status) { ctx->SetStatus(errors::Internal("Blas SGEMM launch failed : m=", m, ", n=", n, ", k=", k)); } return; } int padding_rows = 0; int padding_cols = 0; const int64 out_batch = GetTensorDim(*output, data_format, 'N'); const int64 out_rows = GetTensorDim(*output, data_format, 'H'); const int64 out_cols = GetTensorDim(*output, data_format, 'W'); const int64 out_depths = GetTensorDim(*output, data_format, 'C'); if (padding == SAME) { // Total padding on rows and cols is // Pr = (R' - 1) * S + (Kr - 1) * Dr + 1 - R // Pc = (C' - 1) * S + (Kc - 1) * Dc + 1 - C // where (R', C') are output dimensions, (R, C) are input dimensions, S // is stride, (Dr, Dc) are dilations, (Kr, Kc) are filter dimensions. // We pad Pr/2 on the left and Pr - Pr/2 on the right, Pc/2 on the top // and Pc - Pc/2 on the bottom. When Pr or Pc is odd, this means // we pad more on the right and bottom than on the top and left. padding_rows = std::max(0, (out_rows - 1) * row_stride + (patch_rows - 1) * row_dilation + 1 - in_rows); padding_cols = std::max(0, (out_cols - 1) * col_stride + (patch_cols - 1) * col_dilation + 1 - in_cols); const bool rows_odd = (padding_rows % 2 != 0); const bool cols_odd = (padding_cols % 2 != 0); if (rows_odd || cols_odd) { Tensor transformed_input; int64 new_in_rows = in_rows + rows_odd; int64 new_in_cols = in_cols + cols_odd; OP_REQUIRES_OK( ctx, ctx->allocate_temp(DataTypeToEnum::value, ShapeFromFormat(data_format, in_batch, new_in_rows, new_in_cols, in_depths), &transformed_input)); functor::PadInput()( ctx->eigen_device(), To32Bit(input_param.tensor()), {{0, 0}}, {{rows_odd, cols_odd}}, To32Bit(transformed_input.tensor()), data_format); input = transformed_input; in_rows = new_in_rows; in_cols = new_in_cols; } } if (data_format == FORMAT_NHWC) { // Convert the input tensor from NHWC to NCHW. TensorShape nchw_shape = ShapeFromFormat(FORMAT_NCHW, in_batch, in_rows, in_cols, in_depths); if (in_depths > 1) { Tensor transformed_input; OP_REQUIRES_OK(ctx, ctx->allocate_temp(DataTypeToEnum::value, nchw_shape, &transformed_input)); functor::NHWCToNCHW()( ctx->eigen_device(), const_cast(input).tensor(), transformed_input.tensor()); input = transformed_input; } else { // If depth <= 1, then just reshape. CHECK(input.CopyFrom(input, nchw_shape)); } } CHECK(padding_rows >= 0 && padding_cols >= 0) << "Negative row or col paddings: (" << padding_rows << ", " << padding_cols << ")"; se::dnn::BatchDescriptor input_desc; input_desc.set_count(in_batch) .set_feature_map_count(in_depths) .set_height(in_rows) .set_width(in_cols) .set_layout(se::dnn::DataLayout::kBatchDepthYX); se::dnn::BatchDescriptor output_desc; output_desc.set_count(out_batch) .set_height(out_rows) .set_width(out_cols) .set_feature_map_count(out_depths) .set_layout(se::dnn::DataLayout::kBatchDepthYX); se::dnn::FilterDescriptor filter_desc; filter_desc.set_input_filter_height(patch_rows) .set_input_filter_width(patch_cols) .set_input_feature_map_count(patch_depths) .set_output_feature_map_count(filter.dim_size(3)); se::dnn::ConvolutionDescriptor conv_desc; conv_desc.set_vertical_dilation_rate(row_dilation) .set_horizontal_dilation_rate(col_dilation) .set_vertical_filter_stride(row_stride) .set_horizontal_filter_stride(col_stride) .set_zero_padding_height(padding_rows / 2) .set_zero_padding_width(padding_cols / 2) .set_group_count(in_depths / patch_depths); Tensor transformed_filter; OP_REQUIRES_OK(ctx, ctx->allocate_temp( DataTypeToEnum::value, TensorShape({filter.dim_size(3), filter.dim_size(2), filter.dim_size(0), filter.dim_size(1)}), &transformed_filter)); functor::TransformFilter()( ctx->eigen_device(), FORMAT_OIHW, To32Bit(filter.tensor()), To32Bit(transformed_filter.tensor())); Tensor transformed_output; OP_REQUIRES_OK( ctx, ctx->allocate_temp(DataTypeToEnum::value, ShapeFromFormat(FORMAT_NCHW, out_batch, out_rows, out_cols, out_depths), &transformed_output)); auto input_ptr = AsDeviceMemory(input.template flat().data(), input.template flat().size()); auto filter_ptr = AsDeviceMemory(transformed_filter.template flat().data(), transformed_filter.template flat().size()); auto output_ptr = AsDeviceMemory(transformed_output.template flat().data(), transformed_output.template flat().size()); static int64 ConvolveScratchSize = GetCudnnWorkspaceLimit( // default value is in bytes despite the name of the environment variable "TF_CUDNN_WORKSPACE_LIMIT_IN_MB", 1LL << 32 // 4GB ); int device_id = stream->parent()->device_ordinal(); DataType dtype = input.dtype(); ConvParameters conv_parameters = { in_batch, // batch in_depths, // in_depths {{in_rows, // in_rows in_cols}}, // in_cols FORMAT_NCHW, // compute_data_format out_depths, // out_depths {{patch_rows, // filter_rows patch_cols, // filter_cols patch_depths}}, // filter_depths {{row_dilation, // dilation_rows col_dilation}}, // dilation_cols {{row_stride, // stride_rows col_stride}}, // stride_cols {{padding_rows, // padding_rows padding_cols}}, // padding_cols dtype, // tensor datatype device_id, // device_id }; AlgorithmConfig algorithm_config; if (cudnn_use_autotune && !AutoTuneConv::GetInstance()->Find(conv_parameters, &algorithm_config)) { std::vector algorithms; OP_REQUIRES( ctx, stream->parent()->GetConvolveAlgorithms( conv_parameters.ShouldIncludeWinogradNonfusedAlgo( stream->parent()), &algorithms), errors::Unknown("Failed to get convolution algorithm. This is probably " "because cuDNN failed to initialize, so try looking to " "see if a warning log message was printed above.")); ProfileResult best_result; ProfileResult best_result_no_scratch; for (auto profile_algorithm : algorithms) { // TODO(zhengxq): profile each algorithm multiple times to better // accuracy. CudnnScratchAllocator scratch_allocator(ConvolveScratchSize, ctx); ProfileResult profile_result; bool cudnn_launch_status = stream ->ThenConvolveWithAlgorithm( input_desc, input_ptr, filter_desc, filter_ptr, conv_desc, output_desc, &output_ptr, &scratch_allocator, AlgorithmConfig(profile_algorithm), &profile_result) .ok(); if (cudnn_launch_status) { if (profile_result.is_valid()) { if (profile_result.elapsed_time_in_ms() < best_result.elapsed_time_in_ms()) { best_result = profile_result; } if (scratch_allocator.TotalByteSize() == 0 && profile_result.elapsed_time_in_ms() < best_result_no_scratch.elapsed_time_in_ms()) { best_result_no_scratch = profile_result; } } } } // TODO(yangzihao): refactor the profile result checking code into a common // utility function. OP_REQUIRES(ctx, best_result.is_valid() || best_result_no_scratch.is_valid(), errors::NotFound("No algorithm worked!")); if (best_result.is_valid()) { algorithm_config.set_algorithm(best_result.algorithm()); } if (best_result_no_scratch.is_valid()) { algorithm_config.set_algorithm_no_scratch( best_result_no_scratch.algorithm()); } AutoTuneConv::GetInstance()->Insert(conv_parameters, algorithm_config); } CudnnScratchAllocator scratch_allocator(ConvolveScratchSize, ctx); bool cudnn_launch_status = stream ->ThenConvolveWithAlgorithm(input_desc, input_ptr, filter_desc, filter_ptr, conv_desc, output_desc, &output_ptr, &scratch_allocator, algorithm_config, nullptr) .ok(); if (!cudnn_launch_status) { ctx->SetStatus(errors::Internal( "cuDNN launch failure : input shape(", input.shape().DebugString(), ") filter shape(", filter.shape().DebugString(), ")")); } // Convert the output tensor back from NHWC to NCHW. if (data_format == FORMAT_NHWC) { functor::NCHWToNHWC()( ctx->eigen_device(), const_cast(transformed_output).tensor(), output->tensor()); } else { *output = transformed_output; } } // Forward declarations of the functor specializations for GPU. namespace functor { #define DECLARE_GPU_SPEC(T) \ template <> \ void SpatialConvolution::operator()( \ const GPUDevice& d, typename TTypes::Tensor output, \ typename TTypes::ConstTensor input, \ typename TTypes::ConstTensor filter, int row_stride, \ int col_stride, int row_dilation, int col_dilation, \ const Eigen::PaddingType& padding); \ extern template struct SpatialConvolution; \ template <> \ void MatMulConvFunctor::operator()( \ const GPUDevice& d, typename TTypes::Tensor out, \ typename TTypes::ConstTensor in0, \ typename TTypes::ConstTensor in1, \ const Eigen::array, 1>& dim_pair); \ extern template struct MatMulConvFunctor; \ template <> \ void TransformFilter::operator()( \ const GPUDevice& d, FilterTensorFormat dst_filter_format, \ typename TTypes::ConstTensor in, \ typename TTypes::Tensor out); \ extern template struct TransformFilter; \ template <> \ void PadInput::operator()( \ const GPUDevice& d, typename TTypes::ConstTensor in, \ const std::array& padding_left, \ const std::array& padding_right, \ typename TTypes::Tensor out, TensorFormat data_format); \ extern template struct PadInput DECLARE_GPU_SPEC(float); DECLARE_GPU_SPEC(Eigen::half); DECLARE_GPU_SPEC(double); #undef DECLARE_GPU_SPEC } // namespace functor // Registration of the GPU implementations. REGISTER_KERNEL_BUILDER( Name("Conv2D").Device(DEVICE_GPU).TypeConstraint("T"), Conv2DOp); REGISTER_KERNEL_BUILDER( Name("Conv2D").Device(DEVICE_GPU).TypeConstraint("T"), Conv2DOp); REGISTER_KERNEL_BUILDER( Name("Conv2D").Device(DEVICE_GPU).TypeConstraint("T"), Conv2DOp); // To be used inside depthwise_conv_op.cc. template struct LaunchConv2DOp; template struct LaunchConv2DOp; template struct LaunchConv2DOp; #endif // GOOGLE_CUDA } // namespace tensorflow