From e0976bcb30fa50e6e33c701fc057a4e93935bccf Mon Sep 17 00:00:00 2001
From: Edmond Yoo <hj3yoo@uwaterloo.ca>
Date: Sat, 13 Oct 2018 06:17:09 +0000
Subject: [PATCH] Update README
---
src/convolutional_layer.c | 57 +++++++++++++++++++++++++++++++++++++++++----------------
1 files changed, 41 insertions(+), 16 deletions(-)
diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index d236ec2..16e6d5f 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -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;
}
@@ -621,13 +628,13 @@
free(align_weights);
}
-
-size_t binary_transpose_align_input(int k, int n, float *b, char **t_bit_input, size_t ldb_align)
+// 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));
+ //float *t_input = calloc(t_intput_size, sizeof(float));
//char *
*t_bit_input = calloc(t_bit_input_size, sizeof(char));
@@ -637,13 +644,19 @@
//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 blocksize = 64;
- transpose_block_SSE4x4(b, t_input, k, n, n, new_ldb, blocksize);
+ int src_size = k * bit_align;
+ //printf("\n src_size = %d \n", src_size);
- //printf("\n blocksize = %d \n", blocksize);
+ //float_to_bit(b, t_input, src_size);
- float_to_bit(t_input, *t_bit_input, t_intput_size);
- free(t_input);
+ // 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;
}
@@ -688,14 +701,19 @@
// 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
- im2col_cpu_custom(state.input, l.c, l.h, l.w, l.size, l.stride, l.pad, b);
+ //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;
@@ -772,7 +790,7 @@
}
*/
-
+ /*
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);
@@ -783,20 +801,25 @@
l.binary_weights, state.input, l.output, l.mean_arr);
}
else {
+ */
//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);
+ 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
+ // 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);
- }
+ //}
}
@@ -819,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
}
--
Gitblit v1.10.0