Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cuDNN implementation for bias_add #3489

Merged
merged 2 commits into from
Aug 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions oneflow/user/kernels/bias_add_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
#include "oneflow/core/device/cuda_util.h"
#include "oneflow/core/framework/framework.h"
#include "oneflow/user/kernels/bias_add_kernel.h"
#include "oneflow/core/device/cudnn_util.h"

namespace oneflow {

Expand Down Expand Up @@ -44,6 +45,27 @@ __global__ void InplaceBiasAddGpu(const Index elem_cnt, const Index bias_size,
CUDA_1D_KERNEL_LOOP_T(Index, i, elem_cnt) { y[i] += bias[(i % block_size) / inner_size]; }
}

template<typename T, typename Index>
typename std::enable_if<IsFloating<T>::value || std::is_same<T, float16>::value>::type
InplaceBiasAdd(DeviceCtx* ctx, Index outer_size, Index bias_size, Index inner_size, const T* x,
const T* bias, T* y) {
CudnnTensorDesc c_desc(CUDNN_TENSOR_NCHW, GetDataType<T>::value, outer_size, bias_size,
inner_size, 1);
CudnnTensorDesc a_desc(CUDNN_TENSOR_NCHW, GetDataType<T>::value, 1, bias_size, 1, 1);
OF_CUDNN_CHECK(cudnnAddTensor(ctx->cudnn_handle(), CudnnSPOnePtr<float>(), a_desc.Get(), bias,
CudnnSPOnePtr<float>(), c_desc.Get(), y));
}

template<typename T, typename Index>
typename std::enable_if<IsIntegral<T>::value>::type InplaceBiasAdd(DeviceCtx* ctx, Index outer_size,
Index bias_size,
Index inner_size, const T* x,
const T* bias, T* y) {
const Index elem_cnt = outer_size * bias_size * inner_size;
RUN_CUDA_KERNEL((InplaceBiasAddGpu<T, Index>), ctx, elem_cnt, elem_cnt, bias_size, inner_size,
bias, y);
}

} // namespace

template<typename T, typename Index>
Expand All @@ -52,8 +74,7 @@ struct BiasAddCalculation<DeviceType::kGPU, T, Index> {
const T* x, const T* bias, T* y) {
const Index elem_cnt = outer_size * bias_size * inner_size;
if (x == y) {
RUN_CUDA_KERNEL((InplaceBiasAddGpu<T, Index>), ctx, elem_cnt, elem_cnt, bias_size, inner_size,
bias, y);
InplaceBiasAdd<T, Index>(ctx, outer_size, bias_size, inner_size, x, bias, y);
} else {
RUN_CUDA_KERNEL((BiasAddGpu<T, Index>), ctx, elem_cnt, elem_cnt, bias_size, inner_size, x,
bias, y);
Expand All @@ -65,10 +86,14 @@ template<typename Index>
struct BiasAddCalculation<DeviceType::kGPU, float16, Index> {
static void Invoke(DeviceCtx* ctx, Index outer_size, Index bias_size, Index inner_size,
const float16* x, const float16* bias, float16* y) {
const Index elem_cnt = outer_size * bias_size * inner_size;
RUN_CUDA_KERNEL((BiasAddGpuHalf<Index>), ctx, elem_cnt, elem_cnt, bias_size, inner_size,
reinterpret_cast<const half*>(x), reinterpret_cast<const half*>(bias),
reinterpret_cast<half*>(y));
if (x == y) {
InplaceBiasAdd<float16, Index>(ctx, outer_size, bias_size, inner_size, x, bias, y);
} else {
const Index elem_cnt = outer_size * bias_size * inner_size;
RUN_CUDA_KERNEL((BiasAddGpuHalf<Index>), ctx, elem_cnt, elem_cnt, bias_size, inner_size,
reinterpret_cast<const half*>(x), reinterpret_cast<const half*>(bias),
reinterpret_cast<half*>(y));
}
}
};

Expand Down