x86,cnn: Optimize AVX2 5x5 convolve scratch Process 4 output channels together for single-channel 5x5 layers, defer output permutes to store loops, use uninitialized 5x5 weight arrays, guard the 3-block main loop with kMinWidthFor3_5x5BlocksLoad, and use inv_255_lut plus a bounds-checked 32-byte aligned stack scratch buffer in av1_cnn_predict_img_multi_out. Speedup: -1.35% (1T) / -1.10% (8T) encode time reduction (bit-exact). TAG=agy CONV=b929a630-a6cd-45a0-979c-1767c30178ee Change-Id: I995dd7fa83f182ecc2fd037ad180739c969e7e08
diff --git a/av1/encoder/cnn.c b/av1/encoder/cnn.c index 16a6761..0508fb6 100644 --- a/av1/encoder/cnn.c +++ b/av1/encoder/cnn.c
@@ -10,8 +10,11 @@ */ #include <assert.h> +#include <limits.h> #include <math.h> #include <stdbool.h> +#include <stddef.h> +#include <stdint.h> #include "aom_dsp/aom_dsp_common.h" #include "av1/common/av1_common_int.h" @@ -1081,53 +1084,91 @@ int stride, const CNN_CONFIG *cnn_config, const CNN_THREAD_DATA *thread_data, CNN_MULTI_OUT *output) { - const float max_val = 255.0; + // Precomputed lookup table for 8-bit pixel normalization (x / 255.0f). +#define INV255_ROW(b) \ + ((b) + 0) / 255.0f, ((b) + 1) / 255.0f, ((b) + 2) / 255.0f, \ + ((b) + 3) / 255.0f, ((b) + 4) / 255.0f, ((b) + 5) / 255.0f, \ + ((b) + 6) / 255.0f, ((b) + 7) / 255.0f, ((b) + 8) / 255.0f, \ + ((b) + 9) / 255.0f, ((b) + 10) / 255.0f, ((b) + 11) / 255.0f, \ + ((b) + 12) / 255.0f, ((b) + 13) / 255.0f, ((b) + 14) / 255.0f, \ + ((b) + 15) / 255.0f + static const float inv_255_lut[256] = { + INV255_ROW(0), INV255_ROW(16), INV255_ROW(32), INV255_ROW(48), + INV255_ROW(64), INV255_ROW(80), INV255_ROW(96), INV255_ROW(112), + INV255_ROW(128), INV255_ROW(144), INV255_ROW(160), INV255_ROW(176), + INV255_ROW(192), INV255_ROW(208), INV255_ROW(224), INV255_ROW(240), + }; +#undef INV255_ROW + const int in_channels = cnn_config->layer_config[0].in_channels; + if (width <= 0 || height <= 0 || cnn_config->ext_width < 0 || + cnn_config->ext_height < 0 || + cnn_config->ext_width > (INT_MAX - width) / 2 || + cnn_config->ext_height > (INT_MAX - height) / 2 || in_channels <= 0 || + in_channels > CNN_MAX_CHANNELS) { + return false; + } const int in_width = width + 2 * cnn_config->ext_width; const int in_height = height + 2 * cnn_config->ext_height; - const int in_channels = cnn_config->layer_config[0].in_channels; + if ((size_t)in_width > SIZE_MAX / (size_t)in_height || + (size_t)in_width * (size_t)in_height > SIZE_MAX / (size_t)in_channels) { + return false; + } + const size_t total_elems = + (size_t)in_width * (size_t)in_height * (size_t)in_channels; + if (total_elems > SIZE_MAX / sizeof(float)) return false; float *inputs[CNN_MAX_CHANNELS]; - float *input_ = - (float *)aom_malloc(in_width * in_height * in_channels * sizeof(*input_)); + // Covers 64x64 + 2-px border (and 65x65 intra-partition input), ~18.5 KiB. + enum { kCnnStackInputElems = 68 * 68 }; + DECLARE_ALIGNED(32, float, stack_input[kCnnStackInputElems]); + float *input_ = (total_elems <= (size_t)kCnnStackInputElems) + ? stack_input + : (float *)aom_malloc(total_elems * sizeof(*input_)); if (!input_) return false; const int in_stride = in_width; for (int c = 0; c < in_channels; ++c) { - inputs[c] = input_ + c * in_stride * in_height; - float *input = - inputs[c] + cnn_config->ext_height * in_stride + cnn_config->ext_width; + inputs[c] = input_ + (size_t)c * (size_t)in_stride * (size_t)in_height; + float *input = inputs[c] + + (size_t)cnn_config->ext_height * (size_t)in_stride + + (size_t)cnn_config->ext_width; if (cnn_config->strict_bounds) { for (int i = 0; i < height; ++i) for (int j = 0; j < width; ++j) - input[i * in_stride + j] = (float)dgd[c][i * stride + j] / max_val; + input[(ptrdiff_t)i * in_stride + j] = + inv_255_lut[dgd[c][(ptrdiff_t)i * stride + j]]; // extend left and right for (int i = 0; i < height; ++i) { + const ptrdiff_t row_off = (ptrdiff_t)i * in_stride; for (int j = -cnn_config->ext_width; j < 0; ++j) - input[i * in_stride + j] = input[i * in_stride]; + input[row_off + j] = input[row_off]; for (int j = width; j < width + cnn_config->ext_width; ++j) - input[i * in_stride + j] = input[i * in_stride + width - 1]; + input[row_off + j] = input[row_off + width - 1]; } // extend top and bottom for (int i = -cnn_config->ext_height; i < 0; ++i) - memcpy(&input[i * in_stride - cnn_config->ext_width], - &input[-cnn_config->ext_width], in_width * sizeof(*input)); + memcpy(&input[(ptrdiff_t)i * in_stride - cnn_config->ext_width], + &input[-cnn_config->ext_width], + (size_t)in_width * sizeof(*input)); for (int i = height; i < height + cnn_config->ext_height; ++i) - memcpy(&input[i * in_stride - cnn_config->ext_width], - &input[(height - 1) * in_stride - cnn_config->ext_width], - in_width * sizeof(*input)); + memcpy( + &input[(ptrdiff_t)i * in_stride - cnn_config->ext_width], + &input[(ptrdiff_t)(height - 1) * in_stride - cnn_config->ext_width], + (size_t)in_width * sizeof(*input)); } else { for (int i = -cnn_config->ext_height; i < height + cnn_config->ext_height; ++i) for (int j = -cnn_config->ext_width; j < width + cnn_config->ext_width; ++j) - input[i * in_stride + j] = (float)dgd[c][i * stride + j] / max_val; + input[(ptrdiff_t)i * in_stride + j] = + inv_255_lut[dgd[c][(ptrdiff_t)i * stride + j]]; } } bool success = av1_cnn_predict((const float **)inputs, in_width, in_height, in_stride, cnn_config, thread_data, output); - aom_free(input_); + if (input_ != stack_input) aom_free(input_); return success; }
diff --git a/av1/encoder/x86/cnn_avx2.c b/av1/encoder/x86/cnn_avx2.c index 01f04c3..bde1281 100644 --- a/av1/encoder/x86/cnn_avx2.c +++ b/av1/encoder/x86/cnn_avx2.c
@@ -12,6 +12,7 @@ #include <assert.h> #include <immintrin.h> #include <math.h> +#include <stddef.h> #include "aom_dsp/aom_dsp_common.h" #include "av1/common/av1_common_int.h" @@ -64,7 +65,7 @@ // Load weights needed for layer 0 (for 5x5 block processing), // and fill the registers appropriately to match source pixel mapping. static inline void prepare_weights_for_5x5_convolve( - const float *layer_config_weights, int off, float weight[5][8], + const float *layer_config_weights, int off, float weight[5][5], const int cstep, __m256 *shuffle_weight, const __m256i weight_mask_0, const __m256i weight_mask_1) { for (int row = 0; row < 5; ++row) { @@ -73,11 +74,11 @@ off += cstep; } } - shuffle_weight[0] = _mm256_loadu_ps(weight[0]); - shuffle_weight[1] = _mm256_loadu_ps(weight[1]); - shuffle_weight[2] = _mm256_loadu_ps(weight[2]); - shuffle_weight[3] = _mm256_loadu_ps(weight[3]); - shuffle_weight[4] = _mm256_loadu_ps(weight[4]); + for (int row = 0; row < 5; ++row) { + shuffle_weight[row] = + _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_loadu_ps(weight[row])), + _mm_load_ss(&weight[row][4]), 1); + } shuffle_weight[0] = _mm256_permutevar8x32_ps(shuffle_weight[0], weight_mask_0); @@ -103,19 +104,25 @@ // For each row, loads source pixels 0 to 7(load_src_0), 7 to 14(load_src_1) and // arranges them appropriately to process 3 blocks. -#define PERFORM_CONVOLVE_FOR_3_5X5_BLOCKS() \ - do { \ - for (int row = 0; row < 5; row++) { \ - load_src_0 = _mm256_loadu_ps(input_ptr); \ - load_src_1 = _mm256_loadu_ps(input_ptr + 7); \ - load_src_0 = _mm256_permutevar8x32_ps(load_src_0, block0_1); \ - load_src_1 = _mm256_permutevar8x32_ps(load_src_1, block1_2); \ - load_src_0 = _mm256_mul_ps(load_src_0, shuffle_weight[0 + row]); \ - load_src_1 = _mm256_mul_ps(load_src_1, shuffle_weight[5 + row]); \ - accum_src_0 = _mm256_add_ps(load_src_0, accum_src_0); \ - accum_src_1 = _mm256_add_ps(load_src_1, accum_src_1); \ - input_ptr += in_stride; \ - } \ +#define PERFORM_CONVOLVE_FOR_3_5X5_BLOCKS() \ + do { \ + load_src_0 = \ + _mm256_permutevar8x32_ps(_mm256_loadu_ps(input_ptr), block0_1); \ + load_src_1 = \ + _mm256_permutevar8x32_ps(_mm256_loadu_ps(input_ptr + 7), block1_2); \ + accum_src_0 = _mm256_mul_ps(load_src_0, shuffle_weight[0]); \ + accum_src_1 = _mm256_mul_ps(load_src_1, shuffle_weight[5]); \ + for (int row = 1; row < 5; ++row) { \ + const float *const row_ptr = input_ptr + (ptrdiff_t)row * in_stride; \ + load_src_0 = _mm256_loadu_ps(row_ptr); \ + load_src_1 = _mm256_loadu_ps(row_ptr + 7); \ + load_src_0 = _mm256_permutevar8x32_ps(load_src_0, block0_1); \ + load_src_1 = _mm256_permutevar8x32_ps(load_src_1, block1_2); \ + load_src_0 = _mm256_mul_ps(load_src_0, shuffle_weight[0 + row]); \ + load_src_1 = _mm256_mul_ps(load_src_1, shuffle_weight[5 + row]); \ + accum_src_0 = _mm256_add_ps(load_src_0, accum_src_0); \ + accum_src_1 = _mm256_add_ps(load_src_1, accum_src_1); \ + } \ } while (0) // Load masks needed for shuffling of output and weights. @@ -182,9 +189,10 @@ } while (0) // Do convolution on 8 horizontal 2x2 blocks. -static inline void perform_convolve_for_8h_2x2_blocks( - const float *input_ptr, int in_stride, __m256 *weight, __m256 *out_accum, - __m256i shuffle_output_mask) { +static inline void perform_convolve_for_8h_2x2_blocks(const float *input_ptr, + int in_stride, + __m256 *weight, + __m256 *out_accum) { __m256 load_src[4]; // Load input into source registers. load_src[0] = _mm256_loadu_ps(input_ptr); @@ -203,15 +211,14 @@ load_src[1] = _mm256_add_ps(load_src[1], load_src[3]); load_src[0] = _mm256_hadd_ps(load_src[0], load_src[1]); - // Sort the output in order to store into output buffer. - load_src[0] = _mm256_permutevar8x32_ps(load_src[0], shuffle_output_mask); *out_accum = _mm256_add_ps(*out_accum, load_src[0]); } // Do convolution on 8 (4 horizontal x 2 vertical) 2x2 blocks. -static inline void perform_convolve_for_4hx2v_2x2_blocks( - const float *input_ptr, int in_stride, __m256 *weight, __m256 *out_accum, - __m256i shuffle_output_mask) { +static inline void perform_convolve_for_4hx2v_2x2_blocks(const float *input_ptr, + int in_stride, + __m256 *weight, + __m256 *out_accum) { __m256 load_src[4]; // Load input into source registers. load_src[0] = _mm256_loadu_ps(input_ptr); @@ -230,8 +237,6 @@ load_src[2] = _mm256_add_ps(load_src[2], load_src[3]); load_src[0] = _mm256_hadd_ps(load_src[0], load_src[2]); - // Sort the output in order to store into output buffer. - load_src[0] = _mm256_permutevar8x32_ps(load_src[0], shuffle_output_mask); *out_accum = _mm256_add_ps(*out_accum, load_src[0]); } @@ -276,13 +281,142 @@ // next block convolution and filter_width specifies for how many pixels // filter needs to be applied. const int kMinWidthFor3_5x5Blocks = (kSkipWidth * 2) + kFilterWidth; - for (int i = start_idx; i < layer_config->out_channels; i += channel_step) { + // _mm256_loadu_ps(input_ptr + 7) reads 8 floats spanning offsets +7..+14. + const int kMinWidthFor3_5x5BlocksLoad = kMinWidthFor3_5x5Blocks + 2; + int i = start_idx; + // When in_channels == 1 (Layer 0), process 4 output channels simultaneously + // to reuse the loaded and permuted 5x5 source windows across 4 weight sets. + if (layer_config->in_channels == 1 && channel_step == 1) { + for (; i + 3 < layer_config->out_channels; i += 4) { + const float out_ch_bias[4] = { layer_config->bias[i], + layer_config->bias[i + 1], + layer_config->bias[i + 2], + layer_config->bias[i + 3] }; + __m256 shuffle_weight[4][10]; + float weight[4][5][5]; + for (int ch = 0; ch < 4; ++ch) { + prepare_weights_for_5x5_convolve(layer_config->weights, i + ch, + weight[ch], cstep, shuffle_weight[ch], + weight_mask_0, weight_mask_1); + } + + for (int h = 0, u = 0; h < in_height - kFilterHeight + 1; + h += kSkipHeight, ++u) { + const int out_h = u * out_stride; + int v = 0; + int w = 0; + int rem_width = in_width; + + // Process 3 horizontal 5x5 blocks at a time across 4 output channels. + while (rem_width >= kMinWidthFor3_5x5BlocksLoad) { + __m256 accum_src_0[4]; + __m256 accum_src_1[4]; + const float *const base_ptr = &input[0][h * in_stride + w]; + { + const __m256 s0 = + _mm256_permutevar8x32_ps(_mm256_loadu_ps(base_ptr), block0_1); + const __m256 s1 = _mm256_permutevar8x32_ps( + _mm256_loadu_ps(base_ptr + 7), block1_2); + for (int ch = 0; ch < 4; ++ch) { + accum_src_0[ch] = _mm256_mul_ps(s0, shuffle_weight[ch][0]); + accum_src_1[ch] = _mm256_mul_ps(s1, shuffle_weight[ch][5]); + } + } + for (int row = 1; row < 5; ++row) { + const float *const row_ptr = base_ptr + (ptrdiff_t)row * in_stride; + const __m256 s0 = + _mm256_permutevar8x32_ps(_mm256_loadu_ps(row_ptr), block0_1); + const __m256 s1 = _mm256_permutevar8x32_ps( + _mm256_loadu_ps(row_ptr + 7), block1_2); + for (int ch = 0; ch < 4; ++ch) { + accum_src_0[ch] = + _mm256_add_ps(_mm256_mul_ps(s0, shuffle_weight[ch][0 + row]), + accum_src_0[ch]); + accum_src_1[ch] = + _mm256_add_ps(_mm256_mul_ps(s1, shuffle_weight[ch][5 + row]), + accum_src_1[ch]); + } + } + + // Reduce each channel's 3-block partial sums and add bias. + for (int ch = 0; ch < 4; ++ch) { + __m256 accum = _mm256_hadd_ps(accum_src_0[ch], accum_src_1[ch]); + __m128 tmp_reg_0 = _mm256_extractf128_ps(accum_src_0[ch], 1); + __m128 tmp_reg_1 = _mm256_extractf128_ps(accum_src_1[ch], 1); + __m128 accum_l = _mm256_castps256_ps128(accum); + __m128 accum_h = _mm256_extractf128_ps(accum, 1); + __m128 tmp_reg_2 = _mm_add_ps(accum_l, tmp_reg_0); + __m128 tmp_reg_3 = _mm_add_ps(tmp_reg_0, accum_h); + __m128 tmp_reg_4 = _mm_add_ps(tmp_reg_1, accum_h); + + output[i + ch][out_h + v] = + out_ch_bias[ch] + _mm_cvtss_f32(tmp_reg_2) + + _mm_cvtss_f32(_mm_shuffle_ps(accum_l, accum_l, 1)); + output[i + ch][out_h + v + 1] = + out_ch_bias[ch] + + _mm_cvtss_f32(_mm_shuffle_ps(tmp_reg_3, tmp_reg_3, 1)) + + _mm_cvtss_f32(_mm_shuffle_ps(accum_l, accum_l, 2)); + output[i + ch][out_h + v + 2] = + out_ch_bias[ch] + + _mm_cvtss_f32(_mm_shuffle_ps(tmp_reg_4, tmp_reg_4, 2)) + + _mm_cvtss_f32(_mm_shuffle_ps(accum_l, accum_l, 3)); + } + + v += 3; + w += kSkipWidthForNextIter; + rem_width -= kSkipWidthForNextIter; + } + + // Process remaining single 5x5 blocks across 4 output channels. + while (rem_width >= kFilterWidth) { + const float *const base_ptr = &input[0][h * in_stride + w]; + __m128 src_rows[5]; + float col4[5]; + for (int row = 0; row < 5; ++row) { + const float *const row_ptr = base_ptr + (ptrdiff_t)row * in_stride; + src_rows[row] = _mm_loadu_ps(row_ptr); + col4[row] = row_ptr[4]; + } + + for (int ch = 0; ch < 4; ++ch) { + float last_column_sum = col4[0] * weight[ch][0][4]; + for (int row = 1; row < 5; ++row) { + last_column_sum += col4[row] * weight[ch][row][4]; + } + __m128 r0 = _mm_mul_ps( + src_rows[0], _mm256_castps256_ps128(shuffle_weight[ch][0])); + __m128 r1 = _mm_mul_ps( + src_rows[1], _mm256_castps256_ps128(shuffle_weight[ch][1])); + __m128 r2 = _mm_mul_ps( + src_rows[2], _mm256_castps256_ps128(shuffle_weight[ch][2])); + __m128 r3 = _mm_mul_ps( + src_rows[3], _mm256_castps256_ps128(shuffle_weight[ch][3])); + __m128 r4 = _mm_mul_ps( + src_rows[4], _mm256_castps256_ps128(shuffle_weight[ch][4])); + __m128 accum = r0; + r1 = _mm_add_ps(r1, r2); + r3 = _mm_add_ps(r3, r4); + r1 = _mm_add_ps(r1, r3); + accum = _mm_add_ps(accum, r1); + accum = _mm_hadd_ps(accum, accum); + output[i + ch][out_h + v] = + out_ch_bias[ch] + last_column_sum + _mm_cvtss_f32(accum) + + _mm_cvtss_f32(_mm_shuffle_ps(accum, accum, 1)); + } + v += 1; + w += kSkipWidth; + rem_width -= kSkipWidth; + } + } + } + } + for (; i < layer_config->out_channels; i += channel_step) { const float out_ch_bias = layer_config->bias[i]; for (int k = 0; k < layer_config->in_channels; ++k) { __m256 shuffle_weight[10]; - // Weights needed are 5x5, for SIMD purpose made this array as 5x8. - float weight[5][8] = { { 0 } }; + // Weights needed are 5x5. + float weight[5][5]; int off = k * layer_config->out_channels + i; // In layer 0, the convolution process happens at 5x5. @@ -299,11 +433,11 @@ int w = 0; int rem_width = in_width; // Processing 3 5x5 blocks at a time, if sufficient width is present. - while (rem_width >= kMinWidthFor3_5x5Blocks) { + while (rem_width >= kMinWidthFor3_5x5BlocksLoad) { __m256 load_src_0, load_src_1; - __m256 accum_src_0 = _mm256_setzero_ps(); - __m256 accum_src_1 = _mm256_setzero_ps(); - const float *input_ptr = &input[k][h * in_stride + w]; + __m256 accum_src_0, accum_src_1; + const float *const input_ptr = + &input[k][(ptrdiff_t)h * in_stride + w]; PERFORM_CONVOLVE_FOR_3_5X5_BLOCKS(); // Accumulate across column. @@ -399,12 +533,14 @@ h += kSkipHeight, ++u) { const float *input_ptr = &input[k][h * in_stride]; perform_convolve_for_8h_2x2_blocks(input_ptr, in_stride, shuffle_weight, - &out_accum[u], shuffle_output_mask); + &out_accum[u]); } } // Store output of layer 1. for (int j = 0; j < 8; ++j) { - _mm256_storeu_ps(&output[i][j * out_stride], out_accum[j]); + _mm256_storeu_ps( + &output[i][j * out_stride], + _mm256_permutevar8x32_ps(out_accum[j], shuffle_output_mask)); } } } @@ -450,13 +586,14 @@ h += kSkipHeightForNextIter, ++u) { const float *input_ptr = &input[k][h * in_stride]; perform_convolve_for_4hx2v_2x2_blocks(input_ptr, in_stride, - shuffle_weight, &out_accum[u], - shuffle_output_mask); + shuffle_weight, &out_accum[u]); } } // Store output of layer 2. for (int j = 0; j < 2; ++j) { - _mm256_storeu_ps(&output[i][j * out_stride * 2], out_accum[j]); + _mm256_storeu_ps( + &output[i][j * out_stride * 2], + _mm256_permutevar8x32_ps(out_accum[j], shuffle_output_mask)); } } }