From f606b5456e4876da5f90e2902b2dff07516a03dc Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Wed, 22 Aug 2018 14:52:48 +0000
Subject: [PATCH] XNOR-net 21 FPS on CPU yolov2-tiny.cfg
---
src/gemm.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 203 insertions(+), 13 deletions(-)
diff --git a/src/gemm.c b/src/gemm.c
index 7abfe25..6342252 100644
--- a/src/gemm.c
+++ b/src/gemm.c
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <math.h>
#include <float.h>
+#include <string.h>
#if defined(_OPENMP)
#include <omp.h>
@@ -595,7 +596,7 @@
static int max_num_threads = 0;
if (max_num_threads == 0) {
max_num_threads = omp_get_max_threads();
- omp_set_num_threads( max_num_threads / 2);
+ //omp_set_num_threads( max_num_threads / 2);
}
#endif
@@ -1037,27 +1038,212 @@
}
}
-void transpose_8x8_bits(unsigned char A[8], unsigned char B[8], int m, int n)
+
+//From Berkeley Vision's Caffe!
+//https://github.com/BVLC/caffe/blob/master/LICENSE
+void im2col_cpu_custom_bin(float* data_im,
+ int channels, int height, int width,
+ int ksize, int stride, int pad, float* data_col, int bit_align)
+{
+ int c, h, w;
+ int height_col = (height + 2 * pad - ksize) / stride + 1;
+ int width_col = (width + 2 * pad - ksize) / stride + 1;
+ int channels_col = channels * ksize * ksize;
+
+ // optimized version
+ if (height_col == height && width_col == width && stride == 1 && pad == 1 && is_fma_avx2())
+ {
+ __m256i all256_sing1 = _mm256_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000);
+
+ //int algin = 8;
+ //int ldb = width_col * height_col;
+ //int new_ldb = ldb + (algin - ldb % algin);
+ int new_ldb = bit_align;
+
+ #pragma omp parallel for
+ for (c = 0; c < channels_col; ++c) {
+ int w_offset = c % ksize;
+ int h_offset = (c / ksize) % ksize;
+ int c_im = c / ksize / ksize;
+ for (h = pad; h < height_col - pad; ++h) {
+ for (w = pad; w < width_col - pad - 8; w += 8) {
+ int im_row = h_offset + h - pad;
+ int im_col = w_offset + w - pad;
+ //int col_index = (c * height_col + h) * width_col + w;
+ int col_index = c * new_ldb + h * width_col + w;
+
+ //data_col[col_index] = data_im[im_col + width*(im_row + height*c_im)];
+ __m256 src256 = _mm256_loadu_ps((float *)(&data_im[im_col + width*(im_row + height*c_im)]));
+ _mm256_storeu_ps(&data_col[col_index], src256);
+
+ /*/
+ __m256i src256 = _mm256_loadu_si256((__m256i *)(&data_im[im_col + width*(im_row + height*c_im)]));
+ __m256i result256 = _mm256_and_si256(src256, all256_sing1); // check sign in 8 x 32-bit floats
+
+ uint32_t mask = _mm256_movemask_ps(_mm256_castsi256_ps(result256)); // (val >= 0) ? 0 : 1
+ mask = ~mask; // inverse mask, (val >= 0) ? 1 : 0
+
+ data_col[col_index / 8] = mask; // dst[i / 8] = mask;
+ */
+ }
+
+ for (; w < width_col - pad; ++w) {
+ int im_row = h_offset + h - pad;
+ int im_col = w_offset + w - pad;
+ //int col_index = (c * height_col + h) * width_col + w;
+ int col_index = c * new_ldb + h * width_col + w;
+
+ data_col[col_index] = data_im[im_col + width*(im_row + height*c_im)];
+ float val = data_im[im_col + width*(im_row + height*c_im)];
+ //if(val > 0) set_bit(data_col, col_index);
+ }
+ }
+
+ {
+ w = 0;
+ for (h = 0; h < height_col; ++h) {
+ int im_row = h_offset + h;
+ int im_col = w_offset + w;
+ //int col_index = (c * height_col + h) * width_col + w;
+ int col_index = c * new_ldb + h * width_col + w;
+
+ data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ float val = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ //if (val > 0) set_bit(data_col, col_index);
+ }
+ }
+
+ {
+ w = width_col - 1;
+ for (h = 0; h < height_col; ++h) {
+ int im_row = h_offset + h;
+ int im_col = w_offset + w;
+ //int col_index = (c * height_col + h) * width_col + w;
+ int col_index = c * new_ldb + h * width_col + w;
+
+ data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ float val = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ //if (val > 0) set_bit(data_col, col_index);
+ }
+ }
+
+ {
+ h = 0;
+ for (w = 0; w < width_col; ++w) {
+ int im_row = h_offset + h;
+ int im_col = w_offset + w;
+ //int col_index = (c * height_col + h) * width_col + w;
+ int col_index = c * new_ldb + h * width_col + w;
+
+ data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ float val = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ //if (val > 0) set_bit(data_col, col_index);
+ }
+ }
+
+ {
+ h = height_col - 1;
+ for (w = 0; w < width_col; ++w) {
+ int im_row = h_offset + h;
+ int im_col = w_offset + w;
+ //int col_index = (c * height_col + h) * width_col + w;
+ int col_index = c * new_ldb + h * width_col + w;
+
+ data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ float val = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad);
+ //if (val > 0) set_bit(data_col, col_index);
+ }
+ }
+ }
+
+ }
+ else {
+ printf("\n Error: is no non-optimized version \n");
+ //im2col_cpu(data_im, channels, height, width, ksize, stride, pad, data_col);
+ }
+
+ /*
+ int src_size = bit_align*channels_col;
+ char *bit_arr = calloc(src_size, sizeof(float));
+ float_to_bit(data_col, bit_arr, src_size);
+ memcpy(data_col, bit_arr, src_size * sizeof(float));
+ free(bit_arr);
+ */
+}
+
+
+void transpose_8x8_bits_my(unsigned char *A, unsigned char *B, int lda, int ldb)
+{
+ unsigned x, y, t;
+ for (y = 0; y < 8; ++y) {
+ for (x = 0; x < 8; ++x) {
+ if(A[y * lda] & (1 << x)) B[x * ldb] |= 1 << y;
+ }
+ }
+}
+
+unsigned char reverse_byte_1(char a)
+{
+ return ((a & 0x1) << 7) | ((a & 0x2) << 5) |
+ ((a & 0x4) << 3) | ((a & 0x8) << 1) |
+ ((a & 0x10) >> 1) | ((a & 0x20) >> 3) |
+ ((a & 0x40) >> 5) | ((a & 0x80) >> 7);
+}
+
+unsigned char reverse_byte_2(unsigned char a)
+{
+ return ((a * 0x0802LU & 0x22110LU) | (a * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
+}
+
+static unsigned char lookup[16] = {
+ 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
+ 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf, };
+
+unsigned char reverse_byte(unsigned char n) {
+ // Reverse the top and bottom nibble then swap them.
+ return (lookup[n & 0b1111] << 4) | lookup[n >> 4];
+}
+
+
+void transpose8rS32_reversed_diagonale(unsigned char* A, int m, int n, unsigned char* B)
{
unsigned x, y, t;
// Load the array and pack it into x and y.
-
x = (A[0] << 24) | (A[m] << 16) | (A[2 * m] << 8) | A[3 * m];
y = (A[4 * m] << 24) | (A[5 * m] << 16) | (A[6 * m] << 8) | A[7 * m];
- t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
- t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
+ t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
+ t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
- t = (x ^ (x >> 14)) & 0x0000CCCC; x = x ^ t ^ (t << 14);
- t = (y ^ (y >> 14)) & 0x0000CCCC; y = y ^ t ^ (t << 14);
+ t = (x ^ (x >> 14)) & 0x0000CCCC; x = x ^ t ^ (t << 14);
+ t = (y ^ (y >> 14)) & 0x0000CCCC; y = y ^ t ^ (t << 14);
t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
x = t;
- B[0] = x >> 24; B[n] = x >> 16; B[2 * n] = x >> 8; B[3 * n] = x;
- B[4 * n] = y >> 24; B[5 * n] = y >> 16; B[6 * n] = y >> 8; B[7 * n] = y;
+ B[7 * n] = reverse_byte(x >> 24); B[6 * n] = reverse_byte(x >> 16); B[5 * n] = reverse_byte(x >> 8); B[4 * n] = reverse_byte(x);
+ B[3 * n] = reverse_byte(y >> 24); B[2 * n] = reverse_byte(y >> 16); B[1 * n] = reverse_byte(y >> 8); B[0 * n] = reverse_byte(y);
+}
+
+void transpose_bin(char *A, char *B, const int n, const int m,
+ const int lda, const int ldb, const int block_size)
+{
+ int i;
+ #pragma omp parallel for
+ for (i = 0; i < n; i += 8) {
+ int j;
+ for (j = 0; j < m - 8; j += 8) {
+ int a_index = i*lda + j;
+ int b_index = j*ldb + i;
+ //transpose_8x8_bits_my(&A[a_index/8], &B[b_index/8], lda/8, ldb/8);
+ transpose8rS32_reversed_diagonale(&A[a_index / 8], lda / 8, ldb / 8, &B[b_index / 8]);
+ }
+ for (; j < m; ++j) {
+ if (get_bit(A, i*lda + j)) set_bit(B, j*ldb + i);
+ }
+ }
}
void activate_array_cpu_custom(float *x, const int n, const ACTIVATION a)
@@ -1102,14 +1288,18 @@
size_t i;
__m256i all256_sing1 = _mm256_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000);
+ __m256 float_zero256 = _mm256_set1_ps(0.00001);
for (i = 0; i < size; i+=8)
{
- __m256i src256 = _mm256_loadu_si256((__m256i *)(&src[i]));
- __m256i result256 = _mm256_and_si256(src256, all256_sing1); // check sign in 8 x 32-bit floats
+ //__m256i src256 = _mm256_loadu_si256((__m256i *)(&src[i]));
+ //__m256i result256 = _mm256_and_si256(src256, all256_sing1); // check sign in 8 x 32-bit floats
+ //uint32_t mask = _mm256_movemask_ps(_mm256_castsi256_ps(result256)); // (val >= 0) ? 0 : 1
+ //mask = ~mask; // inverse mask, (val >= 0) ? 1 : 0
- uint32_t mask = _mm256_movemask_ps(_mm256_castsi256_ps(result256)); // (val >= 0) ? 0 : 1
- mask = ~mask; // inverse mask, (val >= 0) ? 1 : 0
+ __m256 src256 = _mm256_loadu_ps((float *)(&src[i]));
+ __m256 result256 = _mm256_cmp_ps(src256, float_zero256, _CMP_GT_OS);
+ uint32_t mask = _mm256_movemask_ps(result256); // (val > 0) ? 0 : 1
dst[i / 8] = mask;
}
--
Gitblit v1.10.0