File: elementwise_sum_dnnlowp_op_avx2.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (217 lines) | stat: -rw-r--r-- 8,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <algorithm>
#include <cmath>
#include <cstdint>

#include <immintrin.h>

namespace caffe2 {

namespace internal {

using namespace std;

constexpr int VLEN = 8;

template <typename T, bool ReluFused>
void ElementWiseSumAVX2(
    const T* input0,
    const T* input1,
    T* output,
    int len,
    float a_scale,
    int32_t a_zero_point,
    float b_scale,
    int32_t b_zero_point,
    float c_scale,
    int32_t c_zero_point) {
  __m256i permute_mask_v =
      _mm256_set_epi32(0x07, 0x03, 0x06, 0x02, 0x05, 0x01, 0x04, 0x00);

  int len_aligned = len / (VLEN * 4) * (VLEN * 4);
  int j = 0;
  for (; j < len_aligned; j += VLEN * 4) {
    // Input is uint8_t but cvtepi8_epi32 assumes the input is int8_t,
    // so we subtract 0x80, cvtepi8_epi32, and then add 0x80
    // x
    __m256 in_v0 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(reinterpret_cast<const __m128i*>(input0 + j)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    in_v0 = _mm256_fmadd_ps(
        in_v0,
        _mm256_set1_ps(a_scale),
        _mm256_set1_ps(-a_zero_point * a_scale - b_zero_point * b_scale));

    __m256 in_v1 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(reinterpret_cast<const __m128i*>(input1 + j)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    __m256 acc_v = _mm256_fmadd_ps(in_v1, _mm256_set1_ps(b_scale), in_v0);

    __m256 x_transformed_v = _mm256_fmadd_ps(
        // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
        acc_v, _mm256_set1_ps(1.0 / c_scale), _mm256_set1_ps(c_zero_point));

    // y
    in_v0 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(
                reinterpret_cast<const __m128i*>(input0 + j + VLEN)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    in_v0 = _mm256_fmadd_ps(
        in_v0,
        _mm256_set1_ps(a_scale),
        _mm256_set1_ps(-a_zero_point * a_scale - b_zero_point * b_scale));

    in_v1 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(
                reinterpret_cast<const __m128i*>(input1 + j + VLEN)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    acc_v = _mm256_fmadd_ps(in_v1, _mm256_set1_ps(b_scale), in_v0);

    __m256 y_transformed_v = _mm256_fmadd_ps(
        // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
        acc_v, _mm256_set1_ps(1.0 / c_scale), _mm256_set1_ps(c_zero_point));

    // z
    in_v0 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(
                reinterpret_cast<const __m128i*>(input0 + j + 2 * VLEN)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    in_v0 = _mm256_fmadd_ps(
        in_v0,
        _mm256_set1_ps(a_scale),
        _mm256_set1_ps(-a_zero_point * a_scale - b_zero_point * b_scale));

    in_v1 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(
                reinterpret_cast<const __m128i*>(input1 + j + 2 * VLEN)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    acc_v = _mm256_fmadd_ps(in_v1, _mm256_set1_ps(b_scale), in_v0);

    __m256 z_transformed_v = _mm256_fmadd_ps(
        // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
        acc_v, _mm256_set1_ps(1.0 / c_scale), _mm256_set1_ps(c_zero_point));

    // w
    in_v0 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(
                reinterpret_cast<const __m128i*>(input0 + j + 3 * VLEN)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    in_v0 = _mm256_fmadd_ps(
        in_v0,
        _mm256_set1_ps(a_scale),
        _mm256_set1_ps(-a_zero_point * a_scale - b_zero_point * b_scale));

    in_v1 = _mm256_cvtepi32_ps(_mm256_add_epi32(
        _mm256_cvtepi8_epi32(_mm_sub_epi8(
            _mm_loadl_epi64(
                reinterpret_cast<const __m128i*>(input1 + j + 3 * VLEN)),
            // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions)
            _mm_set1_epi8(0x80))),
        _mm256_set1_epi32(0x80)));
    acc_v = _mm256_fmadd_ps(in_v1, _mm256_set1_ps(b_scale), in_v0);

    __m256 w_transformed_v = _mm256_fmadd_ps(
        // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
        acc_v, _mm256_set1_ps(1.0 / c_scale), _mm256_set1_ps(c_zero_point));

    // See fbgemm/src/QuantUtilsAvx2.cc requantizeOutputProcessingAvx2 function
    // for more details on this instruction sequence
    __m256i x_rounded_v = _mm256_cvtps_epi32(x_transformed_v);
    __m256i y_rounded_v = _mm256_cvtps_epi32(y_transformed_v);
    __m256i z_rounded_v = _mm256_cvtps_epi32(z_transformed_v);
    __m256i w_rounded_v = _mm256_cvtps_epi32(w_transformed_v);

    __m256i xy_packed_v = _mm256_packs_epi32(x_rounded_v, y_rounded_v);
    __m256i zw_packed_v = _mm256_packs_epi32(z_rounded_v, w_rounded_v);
    __m256i xyzw_packed_v = _mm256_packus_epi16(xy_packed_v, zw_packed_v);
    __m256i xyzw_clamped_v = _mm256_max_epu8(
        ReluFused ? _mm256_set1_epi8(c_zero_point) : _mm256_setzero_si256(),
        _mm256_min_epu8(
            xyzw_packed_v, _mm256_set1_epi8(static_cast<uint8_t>(255))));

    xyzw_clamped_v =
        _mm256_permutevar8x32_epi32(xyzw_clamped_v, permute_mask_v);
    _mm256_storeu_si256(reinterpret_cast<__m256i*>(output + j), xyzw_clamped_v);
  }
  for (; j < len; ++j) {
    float acc = 0;
    acc += (input0[j] - a_zero_point) * a_scale;
    acc += (input1[j] - b_zero_point) * b_scale;
    float transformed_val = c_zero_point + acc / c_scale;
    output[j] = std::max(
        ReluFused ? c_zero_point : 0.0f,
        std::min(255.0f, nearbyint(transformed_val)));
  }
}

template void ElementWiseSumAVX2<uint8_t, false>(
    const uint8_t* input0,
    const uint8_t* input1,
    uint8_t* output,
    int len,
    float a_scale,
    int32_t a_zero_point,
    float b_scale,
    int32_t b_zero_point,
    float c_scale,
    int32_t c_zero_point);

template void ElementWiseSumAVX2<uint8_t, true>(
    const uint8_t* input0,
    const uint8_t* input1,
    uint8_t* output,
    int len,
    float a_scale,
    int32_t a_zero_point,
    float b_scale,
    int32_t b_zero_point,
    float c_scale,
    int32_t c_zero_point);

template void ElementWiseSumAVX2<uint16_t, false>(
    const uint16_t* input0,
    const uint16_t* input1,
    uint16_t* output,
    int len,
    float a_scale,
    int32_t a_zero_point,
    float b_scale,
    int32_t b_zero_point,
    float c_scale,
    int32_t c_zero_point);

template void ElementWiseSumAVX2<uint16_t, true>(
    const uint16_t* input0,
    const uint16_t* input1,
    uint16_t* output,
    int len,
    float a_scale,
    int32_t a_zero_point,
    float b_scale,
    int32_t b_zero_point,
    float c_scale,
    int32_t c_zero_point);

} // namespace internal

} // namespace caffe2