From cd2bdec09030edf7da79ecdeb38d908c106850b3 Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Fri, 23 Feb 2018 12:05:31 +0000
Subject: [PATCH] Updated to CUDA 9.1. And fixed no_gpu dependecies.
---
src/convolutional_kernels.cu | 95 ++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 92 insertions(+), 3 deletions(-)
diff --git a/src/convolutional_kernels.cu b/src/convolutional_kernels.cu
index 005269b..3b2a349 100644
--- a/src/convolutional_kernels.cu
+++ b/src/convolutional_kernels.cu
@@ -74,6 +74,40 @@
check_error(cudaPeekAtLastError());
}
+__global__ void cuda_f32_to_f16(float* input_f32, size_t size, half *output_f16)
+{
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
+ if (idx < size) output_f16[idx] = __float2half(input_f32[idx]);
+ //if (idx < size) *((unsigned short *)output_f16 + idx) = __float2half(input_f32[idx]);
+}
+
+void cuda_convert_f32_to_f16(float* input_f32, size_t size, half *output_f16) {
+ cuda_f32_to_f16 <<< size / BLOCK + 1, BLOCK, 0, get_cuda_stream() >>> (input_f32, size, output_f16);
+}
+
+__global__ void cuda_f16_to_f32(half* input_f16, size_t size, float *output_f32)
+{
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
+ if (idx < size) output_f32[idx] = __half2float(input_f16[idx]);
+ //if (idx < size) output_f32[idx] = __half2float(*((unsigned short *)input_f16 + idx));
+}
+
+void cuda_convert_f16_to_f32(half* input_f16, size_t size, float *output_f32) {
+ cuda_f16_to_f32 <<< size / BLOCK + 1, BLOCK, 0, get_cuda_stream() >>> (input_f16, size, output_f32);
+}
+
+half *cuda_make_f16_from_f32_array(float *src, size_t n)
+{
+ half *dst16;
+ size_t size = sizeof(half)*n;
+ check_error(cudaMalloc((void **)&dst16, size));
+ if (src) {
+ cuda_convert_f32_to_f16(src, n, dst16);
+ }
+ if (!dst16) error("Cuda malloc failed\n");
+ return dst16;
+}
+
void forward_convolutional_layer_gpu(convolutional_layer l, network_state state)
{
fill_ongpu(l.outputs*l.batch, 0, l.output_gpu, 1);
@@ -90,9 +124,57 @@
}
#ifdef CUDNN
- float one = 1;
+ //float one = 1; // alpha[0], beta[0] is float for HALF and FLOAT
+ float alpha = 1, beta = 0;
+
+#ifdef CUDNN_HALF
+ // Note: For improved performance it is advised to use beta[0] = 0.0.
+ // For Tensor Core: cudnnSetConvolutionMathType() where cudnnMathType_t mathType = CUDNN_TENSOR_OP_MATH;
+ // 1. or CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM and use CUDNN_DATA_HALF
+ // 2. or CUDNN_CONVOLUTION_FWD_ALGO_WINOGRAD_NONFUSED
+ // More: http://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index.html#tensor_ops
+
+ const size_t input16_size = l.batch*l.c*l.w*l.h;
+ static size_t max_input16_size = input16_size;
+ static half* input16 = cuda_make_f16_from_f32_array(NULL, max_input16_size);
+
+ const size_t output16_size = l.batch*l.out_c*l.out_h*l.out_w;
+ static size_t max_output16_size = output16_size;
+ static half* output16 = cuda_make_f16_from_f32_array(NULL, max_output16_size);
+
+ if (max_input16_size < input16_size) {
+ max_input16_size = input16_size;
+ cuda_free((float *)input16);
+ input16 = cuda_make_f16_from_f32_array(state.input, max_input16_size);
+ }
+
+ if (max_output16_size < output16_size) {
+ max_output16_size = output16_size;
+ cuda_free((float *)output16);
+ output16 = cuda_make_f16_from_f32_array(NULL, max_output16_size);
+ }
+
+ cuda_convert_f32_to_f16(state.input, input16_size, input16);
+
+ cudnnConvolutionForward(cudnn_handle(),
+ &alpha,
+ l.srcTensorDesc,
+ input16,
+ l.weightDesc,
+ l.weights_gpu16,
+ l.convDesc,
+ l.fw_algo,
+ state.workspace,
+ l.workspace_size,
+ &beta,
+ l.dstTensorDesc,
+ output16);
+
+ cuda_convert_f16_to_f32(output16, output16_size, l.output_gpu);
+#else
+
cudnnConvolutionForward(cudnn_handle(),
- &one,
+ &alpha,
l.srcTensorDesc,
state.input,
l.weightDesc,
@@ -101,9 +183,11 @@
l.fw_algo,
state.workspace,
l.workspace_size,
- &one,
+ &beta,
l.dstTensorDesc,
l.output_gpu);
+#endif
+
#else
int i;
@@ -127,6 +211,7 @@
activate_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation);
//if(l.dot > 0) dot_error_gpu(l);
if(l.binary || l.xnor) swap_binary(&l);
+ //cudaDeviceSynchronize(); // for correct profiling of performance
}
void backward_convolutional_layer_gpu(convolutional_layer l, network_state state)
@@ -162,6 +247,7 @@
if(state.delta){
if(l.binary || l.xnor) swap_binary(&l);
+ // http://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index.html#cudnnConvolutionBackwardData
cudnnConvolutionBackwardData(cudnn_handle(),
&one,
l.weightDesc,
@@ -231,6 +317,9 @@
void push_convolutional_layer(convolutional_layer layer)
{
cuda_push_array(layer.weights_gpu, layer.weights, layer.c*layer.n*layer.size*layer.size);
+#ifdef CUDNN_HALF
+ cuda_convert_f32_to_f16(layer.weights_gpu, layer.c*layer.n*layer.size*layer.size, (half *)layer.weights_gpu16);
+#endif
cuda_push_array(layer.biases_gpu, layer.biases, layer.n);
cuda_push_array(layer.weight_updates_gpu, layer.weight_updates, layer.c*layer.n*layer.size*layer.size);
cuda_push_array(layer.bias_updates_gpu, layer.bias_updates, layer.n);
--
Gitblit v1.10.0