From b5936b499abc94c0efffbcc99b5698574b59d860 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Sat, 05 Sep 2015 00:52:44 +0000
Subject: [PATCH] lots of stuff

---
 src/connected_layer.c |  214 +++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 151 insertions(+), 63 deletions(-)

diff --git a/src/connected_layer.c b/src/connected_layer.c
index d769e1f..4323505 100644
--- a/src/connected_layer.c
+++ b/src/connected_layer.c
@@ -1,90 +1,178 @@
 #include "connected_layer.h"
 #include "utils.h"
+#include "cuda.h"
+#include "blas.h"
+#include "gemm.h"
 
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-connected_layer *make_connected_layer(int inputs, int outputs, ACTIVATION activation)
+connected_layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation)
 {
-    fprintf(stderr, "Connected Layer: %d inputs, %d outputs\n", inputs, outputs);
     int i;
-    connected_layer *layer = calloc(1, sizeof(connected_layer));
-    layer->inputs = inputs;
-    layer->outputs = outputs;
+    connected_layer l = {0};
+    l.type = CONNECTED;
 
-    layer->output = calloc(outputs, sizeof(double*));
-    layer->delta = calloc(outputs, sizeof(double*));
+    l.inputs = inputs;
+    l.outputs = outputs;
+    l.batch=batch;
 
-    layer->weight_updates = calloc(inputs*outputs, sizeof(double));
-    layer->weight_momentum = calloc(inputs*outputs, sizeof(double));
-    layer->weights = calloc(inputs*outputs, sizeof(double));
-    double scale = 2./inputs;
-    for(i = 0; i < inputs*outputs; ++i)
-        layer->weights[i] = rand_normal()*scale;
+    l.output = calloc(batch*outputs, sizeof(float*));
+    l.delta = calloc(batch*outputs, sizeof(float*));
 
-    layer->bias_updates = calloc(outputs, sizeof(double));
-    layer->bias_momentum = calloc(outputs, sizeof(double));
-    layer->biases = calloc(outputs, sizeof(double));
-    for(i = 0; i < outputs; ++i)
-        //layer->biases[i] = rand_normal()*scale + scale;
-        layer->biases[i] = 0;
+    l.weight_updates = calloc(inputs*outputs, sizeof(float));
+    l.bias_updates = calloc(outputs, sizeof(float));
 
-    layer->activation = activation;
-    return layer;
-}
+    l.weights = calloc(inputs*outputs, sizeof(float));
+    l.biases = calloc(outputs, sizeof(float));
 
-void forward_connected_layer(connected_layer layer, double *input)
-{
-    int i, j;
-    for(i = 0; i < layer.outputs; ++i){
-        layer.output[i] = layer.biases[i];
-        for(j = 0; j < layer.inputs; ++j){
-            layer.output[i] += input[j]*layer.weights[i*layer.inputs + j];
-        }
-        layer.output[i] = activate(layer.output[i], layer.activation);
+
+    //float scale = 1./sqrt(inputs);
+    float scale = sqrt(2./inputs);
+    for(i = 0; i < inputs*outputs; ++i){
+        l.weights[i] = 2*scale*rand_uniform() - scale;
     }
-}
 
-void learn_connected_layer(connected_layer layer, double *input)
-{
-    int i, j;
-    for(i = 0; i < layer.outputs; ++i){
-        layer.delta[i] *= gradient(layer.output[i], layer.activation);
-        layer.bias_updates[i] += layer.delta[i];
-        for(j = 0; j < layer.inputs; ++j){
-            layer.weight_updates[i*layer.inputs + j] += layer.delta[i]*input[j];
-        }
+    for(i = 0; i < outputs; ++i){
+        l.biases[i] = scale;
     }
+
+#ifdef GPU
+    l.weights_gpu = cuda_make_array(l.weights, inputs*outputs);
+    l.biases_gpu = cuda_make_array(l.biases, outputs);
+
+    l.weight_updates_gpu = cuda_make_array(l.weight_updates, inputs*outputs);
+    l.bias_updates_gpu = cuda_make_array(l.bias_updates, outputs);
+
+    l.output_gpu = cuda_make_array(l.output, outputs*batch);
+    l.delta_gpu = cuda_make_array(l.delta, outputs*batch);
+#endif
+    l.activation = activation;
+    fprintf(stderr, "Connected Layer: %d inputs, %d outputs\n", inputs, outputs);
+    return l;
 }
 
-void update_connected_layer(connected_layer layer, double step, double momentum, double decay)
+void update_connected_layer(connected_layer l, int batch, float learning_rate, float momentum, float decay)
 {
-    int i,j;
-    for(i = 0; i < layer.outputs; ++i){
-        layer.bias_momentum[i] = step*(layer.bias_updates[i]) + momentum*layer.bias_momentum[i];
-        layer.biases[i] += layer.bias_momentum[i];
-        for(j = 0; j < layer.inputs; ++j){
-            int index = i*layer.inputs+j;
-            layer.weight_momentum[index] = step*(layer.weight_updates[index] - decay*layer.weights[index]) + momentum*layer.weight_momentum[index];
-            layer.weights[index] += layer.weight_momentum[index];
-            //layer.weights[index] = constrain(layer.weights[index], 100.);
-        }
-    }
-    memset(layer.bias_updates, 0, layer.outputs*sizeof(double));
-    memset(layer.weight_updates, 0, layer.outputs*layer.inputs*sizeof(double));
+    axpy_cpu(l.outputs, learning_rate/batch, l.bias_updates, 1, l.biases, 1);
+    scal_cpu(l.outputs, momentum, l.bias_updates, 1);
+
+    axpy_cpu(l.inputs*l.outputs, -decay*batch, l.weights, 1, l.weight_updates, 1);
+    axpy_cpu(l.inputs*l.outputs, learning_rate/batch, l.weight_updates, 1, l.weights, 1);
+    scal_cpu(l.inputs*l.outputs, momentum, l.weight_updates, 1);
 }
 
-void backward_connected_layer(connected_layer layer, double *input, double *delta)
+void forward_connected_layer(connected_layer l, network_state state)
 {
-    int i, j;
-
-    for(j = 0; j < layer.inputs; ++j){
-        delta[j] = 0;
-        for(i = 0; i < layer.outputs; ++i){
-            delta[j] += layer.delta[i]*layer.weights[i*layer.inputs + j];
-        }
+    int i;
+    for(i = 0; i < l.batch; ++i){
+        copy_cpu(l.outputs, l.biases, 1, l.output + i*l.outputs, 1);
     }
+    int m = l.batch;
+    int k = l.inputs;
+    int n = l.outputs;
+    float *a = state.input;
+    float *b = l.weights;
+    float *c = l.output;
+    gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
+    activate_array(l.output, l.outputs*l.batch, l.activation);
 }
 
+void backward_connected_layer(connected_layer l, network_state state)
+{
+    int i;
+    gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);
+    for(i = 0; i < l.batch; ++i){
+        axpy_cpu(l.outputs, 1, l.delta + i*l.outputs, 1, l.bias_updates, 1);
+    }
+    int m = l.inputs;
+    int k = l.batch;
+    int n = l.outputs;
+    float *a = state.input;
+    float *b = l.delta;
+    float *c = l.weight_updates;
+    gemm(1,0,m,n,k,1,a,m,b,n,1,c,n);
+
+    m = l.batch;
+    k = l.outputs;
+    n = l.inputs;
+
+    a = l.delta;
+    b = l.weights;
+    c = state.delta;
+
+    if(c) gemm(0,1,m,n,k,1,a,k,b,k,1,c,n);
+}
+
+#ifdef GPU
+
+void pull_connected_layer(connected_layer l)
+{
+    cuda_pull_array(l.weights_gpu, l.weights, l.inputs*l.outputs);
+    cuda_pull_array(l.biases_gpu, l.biases, l.outputs);
+    cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.inputs*l.outputs);
+    cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.outputs);
+}
+
+void push_connected_layer(connected_layer l)
+{
+    cuda_push_array(l.weights_gpu, l.weights, l.inputs*l.outputs);
+    cuda_push_array(l.biases_gpu, l.biases, l.outputs);
+    cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.inputs*l.outputs);
+    cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.outputs);
+}
+
+void update_connected_layer_gpu(connected_layer l, int batch, float learning_rate, float momentum, float decay)
+{
+    axpy_ongpu(l.outputs, learning_rate/batch, l.bias_updates_gpu, 1, l.biases_gpu, 1);
+    scal_ongpu(l.outputs, momentum, l.bias_updates_gpu, 1);
+
+    axpy_ongpu(l.inputs*l.outputs, -decay*batch, l.weights_gpu, 1, l.weight_updates_gpu, 1);
+    axpy_ongpu(l.inputs*l.outputs, learning_rate/batch, l.weight_updates_gpu, 1, l.weights_gpu, 1);
+    scal_ongpu(l.inputs*l.outputs, momentum, l.weight_updates_gpu, 1);
+}
+
+void forward_connected_layer_gpu(connected_layer l, network_state state)
+{
+    int i;
+    for(i = 0; i < l.batch; ++i){
+        copy_ongpu_offset(l.outputs, l.biases_gpu, 0, 1, l.output_gpu, i*l.outputs, 1);
+    }
+    int m = l.batch;
+    int k = l.inputs;
+    int n = l.outputs;
+    float * a = state.input;
+    float * b = l.weights_gpu;
+    float * c = l.output_gpu;
+    gemm_ongpu(0,0,m,n,k,1,a,k,b,n,1,c,n);
+    activate_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation);
+}
+
+void backward_connected_layer_gpu(connected_layer l, network_state state)
+{
+    int i;
+    gradient_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
+    for(i = 0; i < l.batch; ++i){
+        axpy_ongpu_offset(l.outputs, 1, l.delta_gpu, i*l.outputs, 1, l.bias_updates_gpu, 0, 1);
+    }
+    int m = l.inputs;
+    int k = l.batch;
+    int n = l.outputs;
+    float * a = state.input;
+    float * b = l.delta_gpu;
+    float * c = l.weight_updates_gpu;
+    gemm_ongpu(1,0,m,n,k,1,a,m,b,n,1,c,n);
+
+    m = l.batch;
+    k = l.outputs;
+    n = l.inputs;
+
+    a = l.delta_gpu;
+    b = l.weights_gpu;
+    c = state.delta;
+
+    if(c) gemm_ongpu(0,1,m,n,k,1,a,k,b,k,1,c,n);
+}
+#endif

--
Gitblit v1.10.0