From 729ce43e6ec45cfdb58e06e227428a0f81c5de0f Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Fri, 10 Jun 2016 00:20:31 +0000
Subject: [PATCH] stuff

---
 src/convolutional_layer.c |  127 ++++++++++++++++++++++++++----------------
 1 files changed, 78 insertions(+), 49 deletions(-)

diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index 5575aac..af867e5 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -8,6 +8,14 @@
 #include <stdio.h>
 #include <time.h>
 
+#ifdef AI2
+#include "xnor_layer.h"
+#endif
+
+#ifndef AI2
+#define AI2 0
+#endif
+
 void swap_binary(convolutional_layer *l)
 {
     float *swap = l->filters;
@@ -21,24 +29,6 @@
     #endif
 }
 
-void binarize_filters2(float *filters, int n, int size, char *binary, float *scales)
-{
-    int i, k, f;
-    for(f = 0; f < n; ++f){
-        float mean = 0;
-        for(i = 0; i < size; ++i){
-            mean += fabs(filters[f*size + i]);
-        }
-        mean = mean / size;
-        scales[f] = mean;
-        for(i = 0; i < size/8; ++i){
-            binary[f*size + i] = (filters[f*size + i] > 0) ? 1 : 0;
-            for(k = 0; k < 8; ++k){
-            }
-        }
-    }
-}
-
 void binarize_filters(float *filters, int n, int size, float *binary)
 {
     int i, f;
@@ -54,6 +44,21 @@
     }
 }
 
+void binarize_input(float *input, int n, int size, float *binary)
+{
+    int i, s;
+    for(s = 0; s < size; ++s){
+        float mean = 0;
+        for(i = 0; i < n; ++i){
+            mean += fabs(input[i*size + s]);
+        }
+        mean = mean / n;
+        for(i = 0; i < n; ++i){
+            binary[i*size + s] = (input[i*size + s] > 0) ? mean : -mean;
+        }
+    }
+}
+
 int convolutional_out_height(convolutional_layer l)
 {
     int h = l.h;
@@ -89,7 +94,7 @@
 }
 
 size_t get_workspace_size(layer l){
-    #ifdef CUDNN
+#ifdef CUDNN
     size_t most = 0;
     size_t s = 0;
     cudnnGetConvolutionForwardWorkspaceSize(cudnn_handle(),
@@ -117,9 +122,9 @@
             &s);
     if (s > most) most = s;
     return most;
-    #else
+#else
     return (size_t)l.out_h*l.out_w*l.size*l.size*l.c*sizeof(float);
-    #endif
+#endif
 }
 
 convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation, int batch_normalize, int binary, int xnor)
@@ -133,6 +138,7 @@
     l.c = c;
     l.n = n;
     l.binary = binary;
+    l.xnor = xnor;
     l.batch = batch;
     l.stride = stride;
     l.size = size;
@@ -164,6 +170,10 @@
         l.cfilters = calloc(c*n*size*size, sizeof(char));
         l.scales = calloc(n, sizeof(float));
     }
+    if(xnor){
+        l.binary_filters = calloc(c*n*size*size, sizeof(float));
+        l.binary_input = calloc(l.inputs*l.batch, sizeof(float));
+    }
 
     if(batch_normalize){
         l.scales = calloc(n, sizeof(float));
@@ -199,7 +209,6 @@
         l.binary_filters_gpu = cuda_make_array(l.filters, c*n*size*size);
         l.binary_input_gpu = cuda_make_array(0, l.inputs*l.batch);
     }
-    l.xnor = xnor;
 
     if(batch_normalize){
         l.mean_gpu = cuda_make_array(l.mean, n);
@@ -325,7 +334,7 @@
 
     l->delta_gpu =     cuda_make_array(l->delta, l->batch*out_h*out_w*l->n);
     l->output_gpu =    cuda_make_array(l->output, l->batch*out_h*out_w*l->n);
-    #ifdef CUDNN
+#ifdef CUDNN
     cudnnSetTensor4dDescriptor(l->dsrcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w); 
     cudnnSetTensor4dDescriptor(l->ddstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w); 
     cudnnSetFilter4dDescriptor(l->dfilterDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size); 
@@ -359,7 +368,7 @@
             CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST,
             0,
             &l->bf_algo);
-    #endif
+#endif
 #endif
     l->workspace_size = get_workspace_size(*l);
 }
@@ -404,7 +413,9 @@
     int out_w = convolutional_out_width(l);
     int i;
 
+
     fill_cpu(l.outputs*l.batch, 0, l.output, 1);
+
     /*
        if(l.binary){
        binarize_filters(l.filters, l.n, l.c*l.size*l.size, l.binary_filters);
@@ -413,42 +424,59 @@
        }
      */
 
-    if(l.binary){
-        int m = l.n;
-        int k = l.size*l.size*l.c;
-        int n = out_h*out_w;
+    /*
+       if(l.binary){
+       int m = l.n;
+       int k = l.size*l.size*l.c;
+       int n = out_h*out_w;
 
-        char  *a = l.cfilters;
-        float *b = state.workspace;
-        float *c = l.output;
+       char  *a = l.cfilters;
+       float *b = state.workspace;
+       float *c = l.output;
 
+       for(i = 0; i < l.batch; ++i){
+       im2col_cpu(state.input, l.c, l.h, l.w, 
+       l.size, l.stride, l.pad, b);
+       gemm_bin(m,n,k,1,a,k,b,n,c,n);
+       c += n*m;
+       state.input += l.c*l.h*l.w;
+       }
+       scale_bias(l.output, l.scales, l.batch, l.n, out_h*out_w);
+       add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);
+       activate_array(l.output, m*n*l.batch, l.activation);
+       return;
+       }
+     */
+
+    if(l.xnor && (l.c%32 != 0 || !AI2)){
+        binarize_filters(l.filters, l.n, l.c*l.size*l.size, l.binary_filters);
+        swap_binary(&l);
         for(i = 0; i < l.batch; ++i){
-            im2col_cpu(state.input, l.c, l.h, l.w, 
-                    l.size, l.stride, l.pad, b);
-            gemm_bin(m,n,k,1,a,k,b,n,c,n);
-            c += n*m;
-            state.input += l.c*l.h*l.w;
+            binarize_input(state.input + i*l.inputs, l.c, l.h*l.w, l.binary_input + i*l.inputs);
         }
-        scale_bias(l.output, l.scales, l.batch, l.n, out_h*out_w);
-        add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);
-        activate_array(l.output, m*n*l.batch, l.activation);
-        return;
+        state.input = l.binary_input;
     }
 
     int m = l.n;
     int k = l.size*l.size*l.c;
     int n = out_h*out_w;
 
-    float *a = l.filters;
-    float *b = state.workspace;
-    float *c = l.output;
+    if (l.xnor && l.c%32 == 0 && AI2) {
+        forward_xnor_layer(l, state);
+        printf("xnor\n");
+    } else {
 
-    for(i = 0; i < l.batch; ++i){
-        im2col_cpu(state.input, l.c, l.h, l.w, 
-                l.size, l.stride, l.pad, b);
-        gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
-        c += n*m;
-        state.input += l.c*l.h*l.w;
+        float *a = l.filters;
+        float *b = state.workspace;
+        float *c = l.output;
+
+        for(i = 0; i < l.batch; ++i){
+            im2col_cpu(state.input, l.c, l.h, l.w, 
+                    l.size, l.stride, l.pad, b);
+            gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
+            c += n*m;
+            state.input += l.c*l.h*l.w;
+        }
     }
 
     if(l.batch_normalize){
@@ -457,6 +485,7 @@
     add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);
 
     activate_array(l.output, m*n*l.batch, l.activation);
+    if(l.binary || l.xnor) swap_binary(&l);
 }
 
 void backward_convolutional_layer(convolutional_layer l, network_state state)

--
Gitblit v1.10.0