2015-11-16 06:51:26 +03:00
|
|
|
#include "cuda_runtime.h"
|
|
|
|
#include "curand.h"
|
|
|
|
#include "cublas_v2.h"
|
|
|
|
|
2015-02-11 06:41:03 +03:00
|
|
|
extern "C" {
|
2017-06-02 06:31:13 +03:00
|
|
|
#include "convolutional_layer.h"
|
|
|
|
#include "deconvolutional_layer.h"
|
|
|
|
#include "batchnorm_layer.h"
|
|
|
|
#include "gemm.h"
|
|
|
|
#include "blas.h"
|
|
|
|
#include "im2col.h"
|
|
|
|
#include "col2im.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "cuda.h"
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
extern "C" void forward_deconvolutional_layer_gpu(layer l, network net)
|
2015-02-11 06:41:03 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
int m = l.size*l.size*l.n;
|
|
|
|
int n = l.h*l.w;
|
|
|
|
int k = l.c;
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
fill_gpu(l.outputs*l.batch, 0, l.output_gpu, 1);
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
for(i = 0; i < l.batch; ++i){
|
|
|
|
float *a = l.weights_gpu;
|
2017-04-10 05:56:42 +03:00
|
|
|
float *b = net.input_gpu + i*l.c*l.h*l.w;
|
|
|
|
float *c = net.workspace;
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
gemm_gpu(1,0,m,n,k,1,a,m,b,n,0,c,n);
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
col2im_gpu(net.workspace, l.out_c, l.out_h, l.out_w, l.size, l.stride, l.pad, l.output_gpu+i*l.outputs);
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
2017-03-27 09:42:30 +03:00
|
|
|
if (l.batch_normalize) {
|
2017-04-10 05:56:42 +03:00
|
|
|
forward_batchnorm_layer_gpu(l, net);
|
2017-03-27 09:42:30 +03:00
|
|
|
} else {
|
|
|
|
add_bias_gpu(l.output_gpu, l.biases_gpu, l.batch, l.n, l.out_w*l.out_h);
|
|
|
|
}
|
2017-06-18 23:05:37 +03:00
|
|
|
activate_array_gpu(l.output_gpu, l.batch*l.n*l.out_w*l.out_h, l.activation);
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
extern "C" void backward_deconvolutional_layer_gpu(layer l, network net)
|
2015-02-11 06:41:03 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-12-26 21:52:21 +03:00
|
|
|
//constrain_gpu(l.outputs*l.batch, 1, l.delta_gpu, 1);
|
2017-06-18 23:05:37 +03:00
|
|
|
gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
|
2017-03-27 09:42:30 +03:00
|
|
|
|
|
|
|
if(l.batch_normalize){
|
2017-04-10 05:56:42 +03:00
|
|
|
backward_batchnorm_layer_gpu(l, net);
|
2017-03-27 09:42:30 +03:00
|
|
|
} else {
|
|
|
|
backward_bias_gpu(l.bias_updates_gpu, l.delta_gpu, l.batch, l.n, l.out_w*l.out_h);
|
|
|
|
}
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
//if(net.delta_gpu) memset(net.delta_gpu, 0, l.batch*l.h*l.w*l.c*sizeof(float));
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
for(i = 0; i < l.batch; ++i){
|
|
|
|
int m = l.c;
|
|
|
|
int n = l.size*l.size*l.n;
|
|
|
|
int k = l.h*l.w;
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
float *a = net.input_gpu + i*m*k;
|
|
|
|
float *b = net.workspace;
|
2017-03-27 09:42:30 +03:00
|
|
|
float *c = l.weight_updates_gpu;
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
im2col_gpu(l.delta_gpu + i*l.outputs, l.out_c, l.out_h, l.out_w,
|
2017-03-27 09:42:30 +03:00
|
|
|
l.size, l.stride, l.pad, b);
|
2017-06-18 23:05:37 +03:00
|
|
|
gemm_gpu(0,1,m,n,k,1,a,k,b,k,1,c,n);
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
if(net.delta_gpu){
|
2017-03-27 09:42:30 +03:00
|
|
|
int m = l.c;
|
|
|
|
int n = l.h*l.w;
|
|
|
|
int k = l.size*l.size*l.n;
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
float *a = l.weights_gpu;
|
2017-04-10 05:56:42 +03:00
|
|
|
float *b = net.workspace;
|
|
|
|
float *c = net.delta_gpu + i*n*m;
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
gemm_gpu(0,0,m,n,k,1,a,k,b,n,1,c,n);
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
extern "C" void pull_deconvolutional_layer(layer l)
|
2015-02-11 06:41:03 +03:00
|
|
|
{
|
2017-03-27 09:42:30 +03:00
|
|
|
cuda_pull_array(l.weights_gpu, l.weights, l.c*l.n*l.size*l.size);
|
|
|
|
cuda_pull_array(l.biases_gpu, l.biases, l.n);
|
|
|
|
cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.c*l.n*l.size*l.size);
|
|
|
|
cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.n);
|
|
|
|
if (l.batch_normalize){
|
|
|
|
cuda_pull_array(l.scales_gpu, l.scales, l.n);
|
|
|
|
cuda_pull_array(l.rolling_mean_gpu, l.rolling_mean, l.n);
|
|
|
|
cuda_pull_array(l.rolling_variance_gpu, l.rolling_variance, l.n);
|
|
|
|
}
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
extern "C" void push_deconvolutional_layer(layer l)
|
2015-02-11 06:41:03 +03:00
|
|
|
{
|
2017-03-27 09:42:30 +03:00
|
|
|
cuda_push_array(l.weights_gpu, l.weights, l.c*l.n*l.size*l.size);
|
|
|
|
cuda_push_array(l.biases_gpu, l.biases, l.n);
|
|
|
|
cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.c*l.n*l.size*l.size);
|
|
|
|
cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.n);
|
|
|
|
if (l.batch_normalize){
|
|
|
|
cuda_push_array(l.scales_gpu, l.scales, l.n);
|
|
|
|
cuda_push_array(l.rolling_mean_gpu, l.rolling_mean, l.n);
|
|
|
|
cuda_push_array(l.rolling_variance_gpu, l.rolling_variance, l.n);
|
|
|
|
}
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:19:08 +03:00
|
|
|
void update_deconvolutional_layer_gpu(layer l, update_args a)
|
2015-02-11 06:41:03 +03:00
|
|
|
{
|
2017-06-13 02:19:08 +03:00
|
|
|
float learning_rate = a.learning_rate*l.learning_rate_scale;
|
|
|
|
float momentum = a.momentum;
|
|
|
|
float decay = a.decay;
|
|
|
|
int batch = a.batch;
|
|
|
|
|
|
|
|
if(a.adam){
|
2017-12-26 21:52:21 +03:00
|
|
|
adam_update_gpu(l.weights_gpu, l.weight_updates_gpu, l.m_gpu, l.v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.nweights, batch, a.t);
|
2017-06-13 02:19:08 +03:00
|
|
|
adam_update_gpu(l.biases_gpu, l.bias_updates_gpu, l.bias_m_gpu, l.bias_v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.n, batch, a.t);
|
2017-04-10 05:56:42 +03:00
|
|
|
if(l.scales_gpu){
|
2017-06-13 02:19:08 +03:00
|
|
|
adam_update_gpu(l.scales_gpu, l.scale_updates_gpu, l.scale_m_gpu, l.scale_v_gpu, a.B1, a.B2, a.eps, decay, learning_rate, l.n, batch, a.t);
|
2017-04-10 05:56:42 +03:00
|
|
|
}
|
|
|
|
}else{
|
2017-12-26 21:52:21 +03:00
|
|
|
axpy_gpu(l.nweights, -decay*batch, l.weights_gpu, 1, l.weight_updates_gpu, 1);
|
|
|
|
axpy_gpu(l.nweights, learning_rate/batch, l.weight_updates_gpu, 1, l.weights_gpu, 1);
|
|
|
|
scal_gpu(l.nweights, momentum, l.weight_updates_gpu, 1);
|
2017-04-10 05:56:42 +03:00
|
|
|
|
2017-06-18 23:05:37 +03:00
|
|
|
axpy_gpu(l.n, learning_rate/batch, l.bias_updates_gpu, 1, l.biases_gpu, 1);
|
|
|
|
scal_gpu(l.n, momentum, l.bias_updates_gpu, 1);
|
2015-02-11 06:41:03 +03:00
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
if(l.scales_gpu){
|
2017-06-18 23:05:37 +03:00
|
|
|
axpy_gpu(l.n, learning_rate/batch, l.scale_updates_gpu, 1, l.scales_gpu, 1);
|
|
|
|
scal_gpu(l.n, momentum, l.scale_updates_gpu, 1);
|
2017-04-10 05:56:42 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-11 06:41:03 +03:00
|
|
|
}
|
|
|
|
|