|
| 1 | +// Copyright (c) 2025 PaddlePaddle 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 | +#pragma once |
| 16 | + |
| 17 | +#include "paddle/phi/kernels/conv_kernel.h" |
| 18 | + |
| 19 | +#include "paddle/phi/backends/context_pool.h" |
| 20 | +#include "paddle/phi/backends/gpu/gpu_context.h" |
| 21 | +#include "paddle/phi/core/dense_tensor.h" |
| 22 | +#include "paddle/phi/core/kernel_registry.h" |
| 23 | + |
| 24 | +#ifdef PADDLE_WITH_HIP |
| 25 | +#include "paddle/phi/kernels/gpudnn/conv_miopen_helper.h" |
| 26 | +#else |
| 27 | +#include "paddle/phi/kernels/gpudnn/conv_cudnn_v7.h" |
| 28 | +#endif |
| 29 | + |
| 30 | +#include "paddle/phi/common/bfloat16.h" |
| 31 | +#include "paddle/phi/common/float16.h" |
| 32 | + |
| 33 | +#ifdef PADDLE_WITH_CUDNN_FRONTEND |
| 34 | +// clang-format off |
| 35 | +#include "paddle/phi/backends/dynload/cudnn_frontend.h" |
| 36 | +#include "paddle/phi/kernels/autotune/cache.h" |
| 37 | +#include "paddle/phi/kernels/gpudnn/conv_cudnn_frontend.h" |
| 38 | +// clang-format on |
| 39 | +#endif |
| 40 | + |
| 41 | +namespace phi { |
| 42 | + |
| 43 | +template <typename T, typename Context> |
| 44 | +void ConvCudnnKernel(const Context& ctx, |
| 45 | + const DenseTensor& input, |
| 46 | + const DenseTensor& filter, |
| 47 | + const std::vector<int>& strides, |
| 48 | + const std::vector<int>& paddings_t, |
| 49 | + const std::string& padding_algorithm, |
| 50 | + const std::vector<int>& dilations_t, |
| 51 | + int groups, |
| 52 | + const std::string& data_format, |
| 53 | + DenseTensor* output); |
| 54 | + |
| 55 | +template <typename T, typename Context> |
| 56 | +void DepthwiseConvCudnnKernel(const Context& dev_ctx, |
| 57 | + const DenseTensor& input, |
| 58 | + const DenseTensor& filter, |
| 59 | + const std::vector<int>& strides, |
| 60 | + const std::vector<int>& paddings, |
| 61 | + const std::string& padding_algorithm, |
| 62 | + int groups, |
| 63 | + const std::vector<int>& dilations, |
| 64 | + const std::string& data_format, |
| 65 | + DenseTensor* out) { |
| 66 | + ConvCudnnKernel<T>(dev_ctx, |
| 67 | + input, |
| 68 | + filter, |
| 69 | + strides, |
| 70 | + paddings, |
| 71 | + padding_algorithm, |
| 72 | + dilations, |
| 73 | + groups, |
| 74 | + data_format, |
| 75 | + out); |
| 76 | +} |
| 77 | + |
| 78 | +template <typename T, typename Context> |
| 79 | +void ConvCudnnGradKernel(const Context& ctx, |
| 80 | + const DenseTensor& input, |
| 81 | + const DenseTensor& filter, |
| 82 | + const DenseTensor& output_grad, |
| 83 | + const std::vector<int>& strides_t, |
| 84 | + const std::vector<int>& paddings_t, |
| 85 | + const std::string& padding_algorithm, |
| 86 | + const std::vector<int>& dilations_t, |
| 87 | + int groups, |
| 88 | + const std::string& data_format, |
| 89 | + DenseTensor* input_grad, |
| 90 | + DenseTensor* filter_grad); |
| 91 | + |
| 92 | +template <typename T, typename Context> |
| 93 | +void DepthwiseConvCudnnGradKernel(const Context& dev_ctx, |
| 94 | + const DenseTensor& input, |
| 95 | + const DenseTensor& filter, |
| 96 | + const DenseTensor& out_grad, |
| 97 | + const std::vector<int>& strides, |
| 98 | + const std::vector<int>& paddings, |
| 99 | + const std::string& padding_algorithm, |
| 100 | + int groups, |
| 101 | + const std::vector<int>& dilations, |
| 102 | + const std::string& data_format, |
| 103 | + DenseTensor* input_grad, |
| 104 | + DenseTensor* filter_grad) { |
| 105 | + ConvCudnnGradKernel<T>(dev_ctx, |
| 106 | + input, |
| 107 | + filter, |
| 108 | + out_grad, |
| 109 | + strides, |
| 110 | + paddings, |
| 111 | + padding_algorithm, |
| 112 | + dilations, |
| 113 | + groups, |
| 114 | + data_format, |
| 115 | + input_grad, |
| 116 | + filter_grad); |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace phi |
0 commit comments