From a723e1c62a27aeb39aaf7fcdeb3beb4e89fba32d Mon Sep 17 00:00:00 2001
From: Alexey <AlexeyAB@users.noreply.github.com>
Date: Wed, 15 Aug 2018 20:52:09 +0000
Subject: [PATCH] Merge pull request #766 from HotChick91/AlexeyAB-mask
---
src/gemm.c | 250 +++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 218 insertions(+), 32 deletions(-)
diff --git a/src/gemm.c b/src/gemm.c
index 75ce59c..2fd7f50 100644
--- a/src/gemm.c
+++ b/src/gemm.c
@@ -318,6 +318,25 @@
#include <immintrin.h>
#include <smmintrin.h>
+#if defined(_MSC_VER) && _MSC_VER <= 1900
+static inline __int32 _mm256_extract_epi64(__m256i a, const int index) {
+ return a.m256i_i64[index];
+}
+
+static inline __int32 _mm256_extract_epi32(__m256i a, const int index) {
+ return a.m256i_i32[index];
+}
+
+#endif
+
+static inline float _castu32_f32(uint32_t a) {
+ return *((float *)&a);
+}
+
+static inline float _mm256_extract_float32(__m256 a, const int index) {
+ return a.m256_f32[index];
+}
+
#else // Linux GCC/Clang
#include <x86intrin.h>
#include <ammintrin.h>
@@ -325,6 +344,14 @@
#include <smmintrin.h>
#include <cpuid.h>
+static inline float _castu32_f32(uint32_t a) {
+ return *((float *)&a);
+}
+
+static inline float _mm256_extract_float32(__m256 a, const int index) {
+ return _castu32_f32(_mm256_extract_epi32(_mm256_castps_si256(a), index));
+}
+
void asm_cpuid(uint32_t* abcd, uint32_t eax)
{
uint32_t ebx = 0, edx = 0, ecx = 0;
@@ -429,7 +456,7 @@
}
-void convolution_2d(int w, int h, int ksize, int n, int c, int pad, int stride,
+void convolution_2d_old(int w, int h, int ksize, int n, int c, int pad, int stride,
float *weights, float *input, float *output)
{
int out_h = (h + 2 * pad - ksize) / stride + 1; // output_height=input_height for stride=1 and pad=1
@@ -477,6 +504,130 @@
}
}
+void convolution_2d(int w, int h, int ksize, int n, int c, int pad, int stride,
+ float *weights, float *input, float *output, float *mean)
+{
+ int out_h = (h + 2 * pad - ksize) / stride + 1; // output_height=input_height for stride=1 and pad=1
+ int out_w = (w + 2 * pad - ksize) / stride + 1; // output_width=input_width for stride=1 and pad=1
+ int i, f, j;
+
+#if defined(_OPENMP)
+ static int max_num_threads = 0;
+ if (max_num_threads == 0) {
+ max_num_threads = omp_get_max_threads();
+ omp_set_num_threads(4);// max_num_threads / 2);
+ }
+#endif
+
+ //convolution_2d_old(w, h, ksize, n, c, pad, stride, weights, input, output);
+
+ __m256i all256_sing1 = _mm256_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000);
+ for (i = 0; i < ksize*ksize*n*c; i+=8) {
+ *((__m256*)&weights[i]) = _mm256_and_ps(*((__m256*)&weights[i]), _mm256_castsi256_ps(all256_sing1));
+ }
+
+ for (i = 0; i < w*h*c; i += 8) {
+ //*((__m256*)&input[i]) = _mm256_and_ps(*((__m256*)&input[i]), _mm256_castsi256_ps(all256_sing1));
+ }
+
+
+ //__m256i all256_last_zero = _mm256_set1_epi32(0xFFFFFFFF);
+ //all256_last_zero.m256i_i32[7] = 0;
+ __m256i all256_last_zero =
+ _mm256_set_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0);
+
+ __m256i idx256 = _mm256_set_epi32(0, 7, 6, 5, 4, 3, 2, 1);
+ //__m256 all256_sing1 = _mm256_set1_ps(0x80000000);
+ __m256 all256_one = _mm256_set1_ps(1);
+ __m256i all256i_one = _mm256_set1_epi32(1);
+
+ ///__m256i src256 = _mm256_loadu_si256((__m256i *)(&src[i]));
+ ///__m256i result256 = _mm256_and_si256(src256, all256_sing1); // check sign in 8 x 32-bit floats
+
+ int fil;
+ // filter index
+#pragma omp parallel for // "omp parallel for" - automatic parallelization of loop by using OpenMP
+ for (fil = 0; fil < n; ++fil) {
+ int chan, y, x, f_y, f_x;
+ float cur_mean = fabs(mean[fil]);
+ __m256 mean256 = _mm256_set1_ps(cur_mean);
+ // channel index
+ //for (chan = 0; chan < c; ++chan)
+ // input - y
+ for (y = 0; y < h; ++y)
+ // input - x
+ for (x = 0; x < w-8; x+=8)
+ {
+ int const output_index = fil*w*h + y*w + x;
+ float sum = 0;
+ __m256 sum256 = _mm256_set1_ps(0);
+
+ for (chan = 0; chan < c; ++chan) {
+ int const weights_pre_index = fil*c*ksize*ksize + chan*ksize*ksize;
+ int const input_pre_index = chan*w*h;
+
+
+ // filter - y
+ for (f_y = 0; f_y < ksize; ++f_y)
+ {
+ int input_y = y + f_y - pad;
+ //__m256 in = *((__m256*)&input[input_pre_index + input_y*w]);
+ if (input_y < 0 || input_y >= h) continue;
+ //__m256 in = _mm256_loadu_ps(&input[input_pre_index + input_y*w + x - pad]);
+
+ // filter - x
+ for (f_x = 0; f_x < ksize; ++f_x)
+ {
+ int input_x = x + f_x - pad;
+ //if (input_y < 0 || input_x < 0 || input_y >= h || input_x >= w) continue;
+
+ int input_index = input_pre_index + input_y*w + input_x;
+ int weights_index = weights_pre_index + f_y*ksize + f_x;
+ //if (input_y < 0 || input_y >= h) continue;
+
+ //sum += input[input_index] * weights[weights_index];
+
+ __m256 in = *((__m256*)&input[input_index]);
+ __m256 w = _mm256_set1_ps(weights[weights_index]);
+ //__m256 w_sign = _mm256_and_ps(w, _mm256_castsi256_ps(all256_sing1)); // check sign in 8 x 32-bit floats
+ __m256 xor256 = _mm256_xor_ps(w, in);
+ //printf("\n xor256_1 = %f, xor256_2 = %f \n", xor256.m256_f32[0], xor256.m256_f32[1]);
+ //printf("\n in = %f, w = %f, xor256 = %f \n", in.m256_f32[0], w_sign.m256_f32[0], xor256.m256_f32[0]);
+
+ //__m256 pn1 = _mm256_and_ps(_mm256_castsi256_ps(all256i_one), xor256);
+
+
+ //sum256 = xor256;
+ sum256 = _mm256_add_ps(xor256, sum256);
+ //printf("\n --- \n");
+ //printf("\n 0 = %f, 1 = %f, 2 = %f, 3 = %f, 4 = %f, 5 = %f, 6 = %f, 7 = %f \n", in.m256_f32[0], in.m256_f32[1], in.m256_f32[2], in.m256_f32[3], in.m256_f32[4], in.m256_f32[5], in.m256_f32[6], in.m256_f32[7]);
+
+ if (f_x < ksize-1) {
+ //in = _mm256_permutevar8x32_ps(in, idx256);
+ //in = _mm256_and_ps(in, _mm256_castsi256_ps(all256_last_zero));
+ }
+ }
+ }
+ }
+ // l.output[filters][width][height] +=
+ // state.input[channels][width][height] *
+ // l.weights[filters][channels][filter_width][filter_height];
+ //output[output_index] += sum;
+
+ sum256 = _mm256_mul_ps(sum256, mean256);
+ //printf("\n cur_mean = %f, sum256 = %f, sum256 = %f, in = %f \n",
+ // cur_mean, sum256.m256_f32[0], sum256.m256_f32[1], input[input_pre_index]);
+
+ //__m256 out = *((__m256*)&output[output_index]);
+ //out = _mm256_add_ps(out, sum256);
+ //*((__m256*)&output[output_index]) = out;
+ *((__m256*)&output[output_index]) = sum256;
+
+ //_mm256_storeu_ps(&C[i*ldc + j], result256);
+ }
+ }
+}
+
// http://graphics.stanford.edu/~seander/bithacks.html
@@ -516,12 +667,18 @@
static inline int popcnt256_custom(__m256i n) {
__m256i val = count256(n);
- return val.m256i_i64[0] +
- val.m256i_i64[1] +
- val.m256i_i64[2] +
- val.m256i_i64[3];
+ //return val.m256i_i64[0] +
+ //val.m256i_i64[1] +
+ //val.m256i_i64[2] +
+ //val.m256i_i64[3];
+ return _mm256_extract_epi64(val, 0)
+ + _mm256_extract_epi64(val, 1)
+ + _mm256_extract_epi64(val, 2)
+ + _mm256_extract_epi64(val, 3);
}
+// 5x times faster than gemm()-float32
+// further optimizations: do mean-mult only for the last layer
void gemm_nn_custom_bin_mean_transposed(int M, int N, int K, float ALPHA_UNUSED,
unsigned char *A, int lda,
unsigned char *B, int ldb,
@@ -533,7 +690,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
@@ -564,10 +721,14 @@
}
// count of 1 bits
- count = count_sum.m256i_i64[0] +
- count_sum.m256i_i64[1] +
- count_sum.m256i_i64[2] +
- count_sum.m256i_i64[3];
+ //count = count_sum.m256i_i64[0] +
+ // count_sum.m256i_i64[1] +
+ // count_sum.m256i_i64[2] +
+ // count_sum.m256i_i64[3];
+ count = _mm256_extract_epi64(count_sum, 0)
+ + _mm256_extract_epi64(count_sum, 1)
+ + _mm256_extract_epi64(count_sum, 2)
+ + _mm256_extract_epi64(count_sum, 3);
int f1 = (K % bit_step == 0) ? 0 : (bit_step - (K % bit_step));
count = count - f1; // remove extra bits (from empty space for align only)
@@ -616,15 +777,15 @@
int col_index = (h * width_col + w)*ldb_align + c; // transposed & aligned
//data_col[col_index] = data_im[im_col + width*(im_row + height*c_im)];
- __m256 src256 = _mm256_loadu_ps((__m256i *)(&data_im[im_col + width*(im_row + height*c_im)]));
- data_col[col_index + ldb_align * 0] = src256.m256_f32[0];
- data_col[col_index + ldb_align * 1] = src256.m256_f32[1];
- data_col[col_index + ldb_align * 2] = src256.m256_f32[2];
- data_col[col_index + ldb_align * 3] = src256.m256_f32[3];
- data_col[col_index + ldb_align * 4] = src256.m256_f32[4];
- data_col[col_index + ldb_align * 5] = src256.m256_f32[5];
- data_col[col_index + ldb_align * 6] = src256.m256_f32[6];
- data_col[col_index + ldb_align * 7] = src256.m256_f32[7];
+ __m256 src256 = _mm256_loadu_ps((float *)(&data_im[im_col + width*(im_row + height*c_im)]));
+ data_col[col_index + ldb_align * 0] = _mm256_extract_float32(src256, 0);// src256.m256_f32[0];
+ data_col[col_index + ldb_align * 1] = _mm256_extract_float32(src256, 1);// src256.m256_f32[1];
+ data_col[col_index + ldb_align * 2] = _mm256_extract_float32(src256, 2);// src256.m256_f32[2];
+ data_col[col_index + ldb_align * 3] = _mm256_extract_float32(src256, 3);// src256.m256_f32[3];
+ data_col[col_index + ldb_align * 4] = _mm256_extract_float32(src256, 4);// src256.m256_f32[4];
+ data_col[col_index + ldb_align * 5] = _mm256_extract_float32(src256, 5);// src256.m256_f32[5];
+ data_col[col_index + ldb_align * 6] = _mm256_extract_float32(src256, 6);// src256.m256_f32[6];
+ data_col[col_index + ldb_align * 7] = _mm256_extract_float32(src256, 7);// src256.m256_f32[7];
//_mm256_storeu_ps(&data_col[col_index], src256);
}
@@ -717,7 +878,7 @@
int channels_col = channels * ksize * ksize;
// optimized version
- if (height_col == height && width_col == width && stride == 1 && pad == 1)
+ if (height_col == height && width_col == width && stride == 1 && pad == 1 && is_fma_avx())
{
#pragma omp parallel for
for (c = 0; c < channels_col; ++c) {
@@ -731,7 +892,7 @@
int col_index = (c * height_col + h) * width_col + w;
//data_col[col_index] = data_im[im_col + width*(im_row + height*c_im)];
- __m256 src256 = _mm256_loadu_ps((__m256i *)(&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);
}
@@ -796,26 +957,51 @@
}
}
+void transpose_8x8_bits(unsigned char A[8], unsigned char B[8], int m, int n)
+{
+ 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 >> 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;
+}
+
void activate_array_cpu_custom(float *x, const int n, const ACTIVATION a)
{
- int i;
+ int i = 0;
if (a == LINEAR)
{}
else if (a == LEAKY)
{
- __m256i all256_sing1 = _mm256_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000);
- __m256 all256_01 = _mm256_set1_ps(0.1F);
+ if (is_fma_avx()) {
+ __m256i all256_sing1 = _mm256_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0x80000000);
+ __m256 all256_01 = _mm256_set1_ps(0.1F);
- for (i = 0; i < n-8; i += 8) {
- //x[i] = (x[i]>0) ? x[i] : .1*x[i];
+ for (i = 0; i < n - 8; i += 8) {
+ //x[i] = (x[i]>0) ? x[i] : .1*x[i];
- __m256 src256 = _mm256_loadu_ps((__m256 *)(&x[i]));
- __m256 mult256 = _mm256_mul_ps((src256), all256_01); // mult * 0.1
+ __m256 src256 = _mm256_loadu_ps(&x[i]);
+ __m256 mult256 = _mm256_mul_ps((src256), all256_01); // mult * 0.1
- __m256i sign256 = _mm256_and_si256(_mm256_castps_si256(src256), all256_sing1); // check sign in 8 x 32-bit floats
+ __m256i sign256 = _mm256_and_si256(_mm256_castps_si256(src256), all256_sing1); // check sign in 8 x 32-bit floats
- __m256 result256 = _mm256_blendv_ps(src256, mult256, _mm256_castsi256_ps(sign256)); // (sign>0) ? src : mult;
- _mm256_storeu_ps((__m256 *)(&x[i]), result256);
+ __m256 result256 = _mm256_blendv_ps(src256, mult256, _mm256_castsi256_ps(sign256)); // (sign>0) ? src : mult;
+ _mm256_storeu_ps(&x[i], result256);
+ }
}
for (; i < n; ++i) {
@@ -922,7 +1108,7 @@
void convolution_2d(int w, int h, int ksize, int n, int c, int pad, int stride,
- float *weights, float *input, float *output)
+ float *weights, float *input, float *output, float *mean)
{
int out_h = (h + 2 * pad - ksize) / stride + 1; // output_height=input_height for stride=1 and pad=1
int out_w = (w + 2 * pad - ksize) / stride + 1; // output_width=input_width for stride=1 and pad=1
--
Gitblit v1.10.0