From eb46166f1130d6c6fb5d41d6090a86b35d1542fc Mon Sep 17 00:00:00 2001
From: Edmond Yoo <hj3yoo@uwaterloo.ca>
Date: Thu, 06 Sep 2018 17:31:12 +0000
Subject: [PATCH] merge resolution
---
src/convolutional_layer.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 104 insertions(+), 15 deletions(-)
diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index bbc4807..16e6d5f 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -44,7 +44,7 @@
}
mean = mean / size;
for(i = 0; i < size; ++i){
- binary[f*size + i] = (weights[f*size + i] > 0) ? mean : -mean;
+ binary[f*size + i] = (weights[f*size + i] > 0) ? mean: -mean;
}
}
}
@@ -132,6 +132,7 @@
return most;
}
#endif
+ if(l.xnor) return (size_t)l.bit_align*l.size*l.size*l.c * sizeof(float);
return (size_t)l.out_h*l.out_w*l.size*l.size*l.c*sizeof(float);
}
@@ -305,6 +306,10 @@
if(xnor){
l.binary_weights = calloc(c*n*size*size, sizeof(float));
l.binary_input = calloc(l.inputs*l.batch, sizeof(float));
+
+ int align = 8;
+ int src_align = l.out_h*l.out_w;
+ l.bit_align = src_align + (align - src_align % align);
}
if(batch_normalize){
@@ -399,7 +404,9 @@
//fprintf(stderr, "conv %5d %2d x%2d /%2d %4d x%4d x%4d -> %4d x%4d x%4d\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c);
l.bflops = (2.0 * l.n * l.size*l.size*l.c * l.out_h*l.out_w) / 1000000000.;
- fprintf(stderr, "conv %5d %2d x%2d /%2d %4d x%4d x%4d -> %4d x%4d x%4d %5.3f BF\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c, l.bflops);
+ if (l.xnor) fprintf(stderr, "convX ");
+ else fprintf(stderr, "conv ");
+ fprintf(stderr, "%5d %2d x%2d /%2d %4d x%4d x%4d -> %4d x%4d x%4d %5.3f BF\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c, l.bflops);
return l;
}
@@ -593,15 +600,15 @@
}
}
-void binary_transpose_align_weights(convolutional_layer *l, size_t ldb_align)
+void binary_align_weights(convolutional_layer *l)
{
int m = l->n;
int k = l->size*l->size*l->c;
- size_t new_ldb = k + (ldb_align - k%ldb_align); // (k / 8 + 1) * 8;
+ size_t new_lda = k + (l->lda_align - k % l->lda_align); // (k / 8 + 1) * 8;
binarize_weights(l->weights, m, k, l->binary_weights);
- size_t align_weights_size = new_ldb * m;
+ size_t align_weights_size = new_lda * m;
size_t align_bit_weights_size = align_weights_size / 8;// +1;
float *align_weights = calloc(align_weights_size, sizeof(float));
l->align_bit_weights = calloc(align_bit_weights_size, sizeof(char));
@@ -610,7 +617,7 @@
// align A without transpose
for (i = 0; i < m; ++i) {
for (j = 0; j < k; ++j) {
- align_weights[i*new_ldb + j] = l->binary_weights[i*k + j];
+ align_weights[i*new_lda + j] = l->binary_weights[i*k + j];
}
}
float_to_bit(align_weights, l->align_bit_weights, align_weights_size);
@@ -621,6 +628,39 @@
free(align_weights);
}
+// further optimizations: im2col_bin() for XNOR, and then transpose_aling_bin()
+size_t binary_transpose_align_input(int k, int n, float *b, char **t_bit_input, size_t ldb_align, int bit_align)
+{
+ size_t new_ldb = k + (ldb_align - k%ldb_align); // (k / 8 + 1) * 8;
+ size_t t_intput_size = new_ldb * n;
+ size_t t_bit_input_size = t_intput_size / 8;// +1;
+ //float *t_input = calloc(t_intput_size, sizeof(float));
+ //char *
+ *t_bit_input = calloc(t_bit_input_size, sizeof(char));
+
+ //printf("\n bit_input_size = %d, n = %d, k = %d, ldb = %d \n", bit_input_size, n, k, n);
+ //printf("\n t_bit_input_size = %d, k = %d, n = %d, new_ldb = %d \n", t_bit_input_size, k, n, new_ldb);
+
+ //printf("\n align_weights_size = %d, k = %d, m = %d, lda = %d \n", align_weights_size, k, m, k);
+ //printf("\n align_bit_weights_size = %d, k = %d, m = %d, new_lda = %d \n", align_bit_weights_size, k, m, new_ldb);
+
+ int src_size = k * bit_align;
+ //printf("\n src_size = %d \n", src_size);
+
+ //float_to_bit(b, t_input, src_size);
+
+ // b - [bit_align, k] - [l.bit_align, l.size*l.size*l.c] = src_size
+ // t_input - [bit_align, k] - [n', k]
+ // t_bit_input - [new_ldb, n] - [k', n]
+
+ //transpose_bin(t_input, *t_bit_input, k, n, bit_align, new_ldb, 8);
+ transpose_bin(b, *t_bit_input, k, n, bit_align, new_ldb, 8);
+
+ //free(t_input);
+
+ return t_intput_size;
+}
+
void forward_convolutional_layer(convolutional_layer l, network_state state)
{
@@ -652,11 +692,28 @@
u++;
for(i = 0; i < l.batch; ++i){
- im2col_cpu(state.input, l.c, l.h, l.w,
- l.size, l.stride, l.pad, b);
+ //im2col_cpu(state.input, l.c, l.h, l.w, l.size, l.stride, l.pad, b);
+
+ //float *t_input = NULL;
+ //if (l.xnor) {
+ // size_t new_ldb = k + (l.lda_align - k%l.lda_align);
+ // size_t t_intput_size = new_ldb * n;
+ // t_input = calloc(t_intput_size, sizeof(float));
+ // im2col_cpu_custom_transpose(state.input, l.c, l.h, l.w, l.size, l.stride, l.pad, t_input, new_ldb);
+ //}
+ //if (l.xnor && l.size == 3 && l.stride == 1 && l.pad == 1) {}
+ //else
+ // further optimizations: im2col_bin() for XNOR, and then transpose_aling_bin()
+ //im2col_cpu_custom(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);
//gemm_nn_custom(m, n, k, 1, a, k, b, n, c, n);
- if (l.xnor) {
+ if (l.xnor && (l.stride == 1 && l.pad == 1)) {
+ memset(b, 0, l.bit_align*l.size*l.size*l.c * sizeof(float));
+ //im2col_cpu_custom_align(state.input, l.c, l.h, l.w, l.size, l.stride, l.pad, b, l.bit_align);
+ im2col_cpu_custom_bin(state.input, l.c, l.h, l.w, l.size, l.stride, l.pad, b, l.bit_align);
+
size_t output_size = l.outputs;
//float *count_output = calloc(output_size, sizeof(float));
//size_t bit_output_size = output_size / 8 + 1;
@@ -683,7 +740,9 @@
// transpose B from NxK to KxN (x-axis (ldb = l.size*l.size*l.c) - should be multiple of 8 bits)
{
+ /*
size_t ldb_align = 256;// 8;
+
size_t new_ldb = k + (ldb_align - k%ldb_align); // (k / 8 + 1) * 8;
size_t t_intput_size = new_ldb * n;
size_t t_bit_input_size = t_intput_size / 8;// +1;
@@ -707,6 +766,8 @@
}
float_to_bit(t_input, t_bit_input, t_intput_size);
+
+
if (!l.align_bit_weights)
{
size_t align_weights_size = new_ldb * m;
@@ -727,15 +788,39 @@
free(align_weights);
}
+ */
- gemm_nn_custom_bin_mean_transposed(m, n, k, 1, l.align_bit_weights, new_ldb, t_bit_input, new_ldb, c, n, l.mean_arr);
+ /*
+ if (l.size == 3 && l.stride == 1 && l.pad == 1)
+ {
+ //binarize_weights(l.weights, l.n, l.c*l.size*l.size, l.binary_weights);
+ //printf("\n mean = %f \n", l.mean_arr[0]);
- //gemm_nn_custom_bin_mean_transposed(m, n, k, 1, bit_weights, k, t_bit_input, new_ldb, c, n, mean_arr);
+ convolution_2d(l.w, l.h, l.size, l.n, l.c, l.pad, l.stride,
+ //l.weights, state.input, l.output, l.mean_arr);
+ l.binary_weights, state.input, l.output, l.mean_arr);
+ }
+ else {
+ */
- free(t_input);
- free(t_bit_input);
+ //size_t ldb_align = 256; // 256 bit for AVX2
+ int ldb_align = l.lda_align;
+ size_t new_ldb = k + (ldb_align - k%ldb_align);
+ char *t_bit_input = NULL;
+ size_t t_intput_size = binary_transpose_align_input(k, n, b, &t_bit_input, ldb_align, l.bit_align);
+ //char *t_bit_input = calloc(new_ldb * n, sizeof(char)); // for im2col_cpu_custom_transpose() only
+ //float_to_bit(t_input, t_bit_input, new_ldb * n); // for im2col_cpu_custom_transpose() only
- //free(align_bit_weights);
+ // 5x times faster than gemm()-float32
+ // further optimizations: accelerate maxpool-layer with OpenMP/AVX
+ gemm_nn_custom_bin_mean_transposed(m, n, k, 1, l.align_bit_weights, new_ldb, t_bit_input, new_ldb, c, n, l.mean_arr);
+
+ //gemm_nn_custom_bin_mean_transposed(m, n, k, 1, bit_weights, k, t_bit_input, new_ldb, c, n, mean_arr);
+
+ //free(t_input);
+ free(t_bit_input);
+ //}
+
}
// for bit_input: (k * n)
@@ -757,6 +842,8 @@
//free(mean_arr);
}
else {
+ im2col_cpu_custom(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);
// bit-count to float
}
@@ -769,7 +856,9 @@
}
add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w);
- activate_array(l.output, m*n*l.batch, l.activation);
+ //activate_array(l.output, m*n*l.batch, l.activation);
+ activate_array_cpu_custom(l.output, m*n*l.batch, l.activation);
+
if(l.binary || l.xnor) swap_binary(&l);
}
--
Gitblit v1.10.0