Fixed multi-GPU training for Tensor Cores

This commit is contained in:
AlexeyAB 2018-03-09 19:44:46 +03:00
parent a6c51e3b75
commit 880cf187d8
5 changed files with 48 additions and 35 deletions

View File

@ -135,26 +135,24 @@ void forward_convolutional_layer_gpu(convolutional_layer l, network_state state)
// 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 (*state.net.max_input16_size < input16_size) {
//printf("\n input16_size: cur = %zu \t max = %zu \n", input16_size, *state.net.max_input16_size);
*state.net.max_input16_size = input16_size;
if (*state.net.input16_gpu) cuda_free(*state.net.input16_gpu);
*state.net.input16_gpu = (float *)cuda_make_f16_from_f32_array(NULL, *state.net.max_input16_size);
}
float *input16 = *state.net.input16_gpu;
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);
if (*state.net.max_output16_size < output16_size) {
*state.net.max_output16_size = output16_size;
if (*state.net.output16_gpu) cuda_free(*state.net.output16_gpu);
*state.net.output16_gpu = (float *)cuda_make_f16_from_f32_array(NULL, *state.net.max_output16_size);
}
float *output16 = *state.net.output16_gpu;
cuda_convert_f32_to_f16(state.input, input16_size, (float *)input16);
cuda_convert_f32_to_f16(state.input, input16_size, input16);
//fill_ongpu(output16_size / 2, 0, (float *)output16, 1);
cudnnConvolutionForward(cudnn_handle(),
@ -171,7 +169,7 @@ void forward_convolutional_layer_gpu(convolutional_layer l, network_state state)
l.dstTensorDesc,
output16);
cuda_convert_f16_to_f32((float *)output16, output16_size, l.output_gpu);
cuda_convert_f16_to_f32(output16, output16_size, l.output_gpu);
#else
@ -238,27 +236,24 @@ void backward_convolutional_layer_gpu(convolutional_layer l, network_state state
#ifdef CUDNN_HALF
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 delta16_size = l.batch*l.n*l.out_w*l.out_h;
static size_t max_delta16_size = delta16_size;
static half* delta16 = cuda_make_f16_from_f32_array(NULL, max_delta16_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 (*state.net.max_input16_size < input16_size) {
*state.net.max_input16_size = input16_size;
if(*state.net.input16_gpu) cuda_free(*state.net.input16_gpu);
*state.net.input16_gpu = (float *)cuda_make_f16_from_f32_array(NULL, *state.net.max_input16_size);
}
float *input16 = *state.net.input16_gpu;
if (max_delta16_size < delta16_size) {
max_delta16_size = delta16_size;
cuda_free((float *)delta16);
delta16 = cuda_make_f16_from_f32_array(NULL, max_delta16_size);
if (*state.net.max_output16_size < delta16_size) {
*state.net.max_output16_size = delta16_size;
if(*state.net.output16_gpu) cuda_free(*state.net.output16_gpu);
*state.net.output16_gpu = (float *)cuda_make_f16_from_f32_array(NULL, *state.net.max_output16_size);
}
float *delta16 = *state.net.output16_gpu;
cuda_convert_f32_to_f16(state.input, input16_size, (float *)input16);
cuda_convert_f32_to_f16(l.delta_gpu, delta16_size, (float *)delta16);
cuda_convert_f32_to_f16(state.input, input16_size, input16);
cuda_convert_f32_to_f16(l.delta_gpu, delta16_size, delta16);
// convert input: state.input (x), l.delta_gpu (y) from fp32 to fp16
// get output: l.weight_updates_gpu (dw) and convert it to fp32 (ONLY if it is fp16)
@ -305,7 +300,7 @@ void backward_convolutional_layer_gpu(convolutional_layer l, network_state state
l.dsrcTensorDesc,
input16); // state.delta);
cuda_convert_f16_to_f32((float *)input16, input16_size, state.delta);
cuda_convert_f16_to_f32(input16, input16_size, state.delta);
if (l.binary || l.xnor) swap_binary(&l);
if (l.xnor) gradient_array_ongpu(original_input, l.batch*l.c*l.h*l.w, HARDTAN, state.delta);

View File

@ -305,8 +305,8 @@ convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int
l.weights_gpu = cuda_make_array(l.weights, c*n*size*size);
#ifdef CUDNN_HALF
l.weights_gpu16 = cuda_make_array(l.weights, c*n*size*size / 2);
l.weight_updates_gpu16 = cuda_make_array(l.weight_updates, c*n*size*size / 2);
l.weights_gpu16 = cuda_make_array(NULL, c*n*size*size / 2); //cuda_make_array(l.weights, c*n*size*size / 2);
l.weight_updates_gpu16 = cuda_make_array(NULL, c*n*size*size / 2); //cuda_make_array(l.weight_updates, c*n*size*size / 2);
#endif
l.weight_updates_gpu = cuda_make_array(l.weight_updates, c*n*size*size);

View File

@ -140,6 +140,11 @@ network make_network(int n)
#ifdef GPU
net.input_gpu = calloc(1, sizeof(float *));
net.truth_gpu = calloc(1, sizeof(float *));
net.input16_gpu = calloc(1, sizeof(float *));
net.output16_gpu = calloc(1, sizeof(float *));
net.max_input16_size = calloc(1, sizeof(size_t));
net.max_output16_size = calloc(1, sizeof(size_t));
#endif
return net;
}
@ -622,6 +627,13 @@ void free_network(network net)
if (*net.truth_gpu) cuda_free(*net.truth_gpu);
if (net.input_gpu) free(net.input_gpu);
if (net.truth_gpu) free(net.truth_gpu);
if (*net.input16_gpu) cuda_free(*net.input16_gpu);
if (*net.output16_gpu) cuda_free(*net.output16_gpu);
if (net.input16_gpu) free(net.input16_gpu);
if (net.output16_gpu) free(net.output16_gpu);
if (net.max_input16_size) free(net.max_input16_size);
if (net.max_output16_size) free(net.max_output16_size);
#else
free(net.workspace);
#endif

View File

@ -64,6 +64,10 @@ typedef struct network{
#ifdef GPU
float **input_gpu;
float **truth_gpu;
float **input16_gpu;
float **output16_gpu;
size_t *max_input16_size;
size_t *max_output16_size;
int wait_stream;
#endif
} network;

View File

@ -26,17 +26,19 @@
#include "opencv2/videoio/videoio.hpp"
#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)""CVAUX_STR(CV_VERSION_MINOR)""CVAUX_STR(CV_VERSION_REVISION)
#pragma comment(lib, "opencv_world" OPENCV_VERSION ".lib")
#ifdef TRACK_OPTFLOW
#pragma comment(lib, "opencv_cudaoptflow" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_cudaimgproc" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_core" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_imgproc" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_highgui" OPENCV_VERSION ".lib")
#endif // TRACK_OPTFLOW
#else
#define OPENCV_VERSION CVAUX_STR(CV_VERSION_EPOCH)""CVAUX_STR(CV_VERSION_MAJOR)""CVAUX_STR(CV_VERSION_MINOR)
#pragma comment(lib, "opencv_core" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_imgproc" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_highgui" OPENCV_VERSION ".lib")
#endif
#endif // CV_VERSION_EPOCH
class track_kalman {
public: