This commit is contained in:
Joseph Redmon
2016-03-13 23:18:42 -07:00
parent 16d06ec0db
commit d1965bdb96
21 changed files with 1503 additions and 758 deletions

View File

@ -115,6 +115,46 @@ __global__ void backward_bias_kernel(float *bias_updates, float *delta, int batc
}
}
__global__ void dot_kernel(float *output, float scale, int batch, int n, int size, float *delta)
{
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
int f1 = index / n;
int f2 = index % n;
if (f2 <= f1) return;
float sum = 0;
float norm1 = 0;
float norm2 = 0;
int b, i;
for(b = 0; b < batch; ++b){
for(i = 0; i < size; ++i){
int i1 = b * size * n + f1 * size + i;
int i2 = b * size * n + f2 * size + i;
sum += output[i1] * output[i2];
norm1 += output[i1] * output[i1];
norm2 += output[i2] * output[i2];
}
}
norm1 = sqrt(norm1);
norm2 = sqrt(norm2);
float norm = norm1 * norm2;
sum = sum / norm;
for(b = 0; b < batch; ++b){
for(i = 0; i < size; ++i){
int i1 = b * size * n + f1 * size + i;
int i2 = b * size * n + f2 * size + i;
delta[i1] += - scale * sum * output[i2] / norm;
delta[i2] += - scale * sum * output[i1] / norm;
}
}
}
void dot_error_gpu(layer l)
{
dot_kernel<<<cuda_gridsize(l.n*l.n), BLOCK>>>(l.output_gpu, l.dot, l.batch, l.n, l.out_w * l.out_h, l.delta_gpu);
check_error(cudaPeekAtLastError());
}
void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size)
{
backward_bias_kernel<<<n, BLOCK>>>(bias_updates, delta, batch, n, size);
@ -123,9 +163,9 @@ void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int
void swap_binary(convolutional_layer *l)
{
float *swap = l->filters_gpu;
l->filters_gpu = l->binary_filters_gpu;
l->binary_filters_gpu = swap;
float *swap = l->filters_gpu;
l->filters_gpu = l->binary_filters_gpu;
l->binary_filters_gpu = swap;
}
void forward_convolutional_layer_gpu(convolutional_layer l, network_state state)
@ -150,8 +190,8 @@ void forward_convolutional_layer_gpu(convolutional_layer l, network_state state)
gemm_ongpu(0,0,m,n,k,1.,a,k,b,n,1.,c+i*m*n,n);
}
if(l.batch_normalize){
if(state.train){
if (l.batch_normalize) {
if (state.train) {
fast_mean_gpu(l.output_gpu, l.batch, l.n, l.out_h*l.out_w, l.mean_gpu);
fast_variance_gpu(l.output_gpu, l.mean_gpu, l.batch, l.n, l.out_h*l.out_w, l.variance_gpu);
@ -172,6 +212,7 @@ void forward_convolutional_layer_gpu(convolutional_layer l, network_state state)
add_bias_gpu(l.output_gpu, l.biases_gpu, l.batch, l.n, n);
activate_array_ongpu(l.output_gpu, m*n*l.batch, l.activation);
if(l.dot > 0) dot_error_gpu(l);
if(l.binary) swap_binary(&l);
}