|
| 1 | +/*++ |
| 2 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +Licensed under the MIT License. |
| 4 | +Module Name: |
| 5 | + sconv_nchw_depthwise_multiplier_greater_than_1_avx512f.cpp |
| 6 | +Abstract: |
| 7 | + This module implements the AVX512F kernel for the exact MobileClip grouped |
| 8 | + projection case: |
| 9 | +
|
| 10 | + - CHW input/output layout per group slice |
| 11 | + - input channels per group = 1 |
| 12 | + - output channels per group = 2 |
| 13 | + - kernel = 7x7 |
| 14 | + - stride = 2x2 |
| 15 | + - padding = 3,3,3,3 |
| 16 | + - dilation = 1x1 |
| 17 | +
|
| 18 | + The outer dispatch is expected to guarantee these constraints. |
| 19 | +--*/ |
| 20 | + |
| 21 | +#include "mlasi.h" |
| 22 | + |
| 23 | +#if defined(MLAS_TARGET_AMD64) |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +MLAS_FORCEINLINE |
| 28 | +void |
| 29 | +MlasConv2dSingleChannelCHWKernel7x7Pad3Stride2Dilation1DepthMultiplier2Scalar( |
| 30 | + const float* Input, |
| 31 | + size_t InputHeight, |
| 32 | + size_t InputWidth, |
| 33 | + const float* Filter0, |
| 34 | + const float* Filter1, |
| 35 | + float* Output0, |
| 36 | + float* Output1, |
| 37 | + size_t OutputWidth, |
| 38 | + size_t oh, |
| 39 | + size_t ow, |
| 40 | + float Beta |
| 41 | + ) |
| 42 | +/*++ |
| 43 | +
|
| 44 | +Routine Description: |
| 45 | +
|
| 46 | + Computes one border output point for the exact MobileClip |
| 47 | + 7x7/pad-3/stride-2/dilation-1, multiplier-2 case. |
| 48 | +
|
| 49 | + This helper is only used by the AVX512 implementation for border handling; |
| 50 | + it is not a generic fallback dispatch path despite the scalar |
| 51 | + implementation. |
| 52 | +
|
| 53 | +--*/ |
| 54 | +{ |
| 55 | + const ptrdiff_t input_origin_y = static_cast<ptrdiff_t>(oh * 2) - 3; |
| 56 | + const ptrdiff_t input_origin_x = static_cast<ptrdiff_t>(ow * 2) - 3; |
| 57 | + const size_t output_index = oh * OutputWidth + ow; |
| 58 | + |
| 59 | + float acc0 = (Beta == 0.0f) ? 0.0f : Output0[output_index] * Beta; |
| 60 | + float acc1 = (Beta == 0.0f) ? 0.0f : Output1[output_index] * Beta; |
| 61 | + |
| 62 | + for (size_t kh = 0; kh < 7; ++kh) { |
| 63 | + const ptrdiff_t ih = input_origin_y + static_cast<ptrdiff_t>(kh); |
| 64 | + if (ih < 0 || ih >= static_cast<ptrdiff_t>(InputHeight)) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + const float* input_row = Input + static_cast<size_t>(ih) * InputWidth; |
| 69 | + const float* filter0_row = Filter0 + kh * 7; |
| 70 | + const float* filter1_row = Filter1 + kh * 7; |
| 71 | + |
| 72 | + for (size_t kw = 0; kw < 7; ++kw) { |
| 73 | + const ptrdiff_t iw = input_origin_x + static_cast<ptrdiff_t>(kw); |
| 74 | + if (iw < 0 || iw >= static_cast<ptrdiff_t>(InputWidth)) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + const float input_value = input_row[static_cast<size_t>(iw)]; |
| 79 | + acc0 += input_value * filter0_row[kw]; |
| 80 | + acc1 += input_value * filter1_row[kw]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Output0[output_index] = acc0; |
| 85 | + Output1[output_index] = acc1; |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace |
| 89 | + |
| 90 | +void |
| 91 | +MlasConvDepthwiseMultiplier2CHWKernel7x7S2Avx512F( |
| 92 | + const float* Input, |
| 93 | + size_t InputHeight, |
| 94 | + size_t InputWidth, |
| 95 | + const float* Filter, |
| 96 | + float* Output, |
| 97 | + size_t OutputHeight, |
| 98 | + size_t OutputWidth, |
| 99 | + float Beta |
| 100 | + ) |
| 101 | +/*++ |
| 102 | +
|
| 103 | +Routine Description: |
| 104 | +
|
| 105 | + Computes one group slice of the exact MobileClip grouped projection case. |
| 106 | +
|
| 107 | +Assumptions: |
| 108 | +
|
| 109 | + - Input and output are CHW tensors for a single group slice. |
| 110 | + - Filter is OIHW for a single group slice with exactly two output channels. |
| 111 | + - Kernel = 7x7, stride = 2, padding = 3, dilation = 1. |
| 112 | + - OutputHeight and OutputWidth match the supplied input geometry. |
| 113 | +
|
| 114 | +Return Value: |
| 115 | +
|
| 116 | + None. |
| 117 | +
|
| 118 | +--*/ |
| 119 | +{ |
| 120 | + constexpr size_t KernelSize = 7; |
| 121 | + constexpr __mmask16 ValidKernelMask = 0x007F; |
| 122 | + |
| 123 | + const float* Filter0 = Filter; |
| 124 | + const float* Filter1 = Filter + KernelSize * KernelSize; |
| 125 | + float* Output0 = Output; |
| 126 | + float* Output1 = Output + (OutputHeight * OutputWidth); |
| 127 | + |
| 128 | + for (size_t oh = 0; oh < OutputHeight; ++oh) { |
| 129 | + const ptrdiff_t input_origin_y = static_cast<ptrdiff_t>(oh * 2) - 3; |
| 130 | + const bool interior_y = input_origin_y >= 0 && |
| 131 | + (input_origin_y + static_cast<ptrdiff_t>(KernelSize)) <= static_cast<ptrdiff_t>(InputHeight); |
| 132 | + |
| 133 | + for (size_t ow = 0; ow < OutputWidth; ++ow) { |
| 134 | + const ptrdiff_t input_origin_x = static_cast<ptrdiff_t>(ow * 2) - 3; |
| 135 | + const bool interior_x = input_origin_x >= 0 && |
| 136 | + (input_origin_x + static_cast<ptrdiff_t>(KernelSize)) <= static_cast<ptrdiff_t>(InputWidth); |
| 137 | + |
| 138 | + if (!(interior_y && interior_x)) { |
| 139 | + MlasConv2dSingleChannelCHWKernel7x7Pad3Stride2Dilation1DepthMultiplier2Scalar( |
| 140 | + Input, InputHeight, InputWidth, Filter0, Filter1, Output0, Output1, OutputWidth, oh, ow, Beta); |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + __m512 acc0 = _mm512_setzero_ps(); |
| 145 | + __m512 acc1 = _mm512_setzero_ps(); |
| 146 | + |
| 147 | + for (size_t kh = 0; kh < KernelSize; ++kh) { |
| 148 | + const float* input_row = Input + (static_cast<size_t>(input_origin_y) + kh) * InputWidth + static_cast<size_t>(input_origin_x); |
| 149 | + const __m512 input_vec = _mm512_maskz_loadu_ps(ValidKernelMask, input_row); |
| 150 | + const __m512 filter0_vec = _mm512_maskz_loadu_ps(ValidKernelMask, Filter0 + kh * KernelSize); |
| 151 | + const __m512 filter1_vec = _mm512_maskz_loadu_ps(ValidKernelMask, Filter1 + kh * KernelSize); |
| 152 | + |
| 153 | + acc0 = _mm512_fmadd_ps(input_vec, filter0_vec, acc0); |
| 154 | + acc1 = _mm512_fmadd_ps(input_vec, filter1_vec, acc1); |
| 155 | + } |
| 156 | + |
| 157 | + const size_t output_index = oh * OutputWidth + ow; |
| 158 | + float acc0_scalar = _mm512_reduce_add_ps(acc0); |
| 159 | + float acc1_scalar = _mm512_reduce_add_ps(acc1); |
| 160 | + |
| 161 | + if (Beta != 0.0f) { |
| 162 | + acc0_scalar += Output0[output_index] * Beta; |
| 163 | + acc1_scalar += Output1[output_index] * Beta; |
| 164 | + } |
| 165 | + |
| 166 | + Output0[output_index] = acc0_scalar; |
| 167 | + Output1[output_index] = acc1_scalar; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +#endif |
0 commit comments