Welcome to mirror list, hosted at ThFree Co, Russian Federation.

postprocess.h - github.com/marian-nmt/intgemm/intgemm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2df946de1850dd20f2e7ebe23e8314cfe440914c (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#pragma once

#include "intrinsics.h"
#include "postprocess_pipeline.h"
#include "types.h"
#include "vec_utils.h"

namespace intgemm {

/*
 * Unquantize
 */
class Unquantize {
public:
  float unquantize_multiplier;

  Unquantize(float unquantize_multiplier) : unquantize_multiplier(unquantize_multiplier) {}
};

template <>
class PostprocessImpl<Unquantize, CPUType::SSE2> {
public:
  using InputRegister = RegisterPair128i;
  using OutputRegister = RegisterPair128;

  INTGEMM_SSE2 PostprocessImpl(const Unquantize& config) {
    unquantize_multiplier = set1_ps<__m128>(config.unquantize_multiplier);
  }

  INTGEMM_SSE2 inline OutputRegister run(InputRegister input, Index offset) {
    return {
      mul_ps(cvtepi32_ps(input.pack0123), unquantize_multiplier),
      mul_ps(cvtepi32_ps(input.pack4567), unquantize_multiplier),
    };
  }

private:
  __m128 unquantize_multiplier;
};

template <>
class PostprocessImpl<Unquantize, CPUType::AVX2> {
public:
  using InputRegister = __m256i;
  using OutputRegister = __m256;

  INTGEMM_AVX2 PostprocessImpl(const Unquantize& config) {
    unquantize_multiplier = set1_ps<__m256>(config.unquantize_multiplier);
  }

  INTGEMM_AVX2 inline OutputRegister run(InputRegister input, Index offset) {
    return mul_ps(cvtepi32_ps(input), unquantize_multiplier);
  }

private:
  __m256 unquantize_multiplier;
};

template <>
class PostprocessImpl<Unquantize, CPUType::AVX512BW> {
public:
  using InputRegister = __m512i;
  using OutputRegister = __m512;

  INTGEMM_AVX512BW PostprocessImpl(const Unquantize& config) {
    unquantize_multiplier = set1_ps<__m512>(config.unquantize_multiplier);
  }

  INTGEMM_AVX512BW inline OutputRegister run(InputRegister input, Index offset) {
    return mul_ps(cvtepi32_ps(input), unquantize_multiplier);
  }

private:
  __m512 unquantize_multiplier;
};

/*
 * Identity
 */
class Identity {};

template <>
class PostprocessImpl<Identity, CPUType::SSE2> {
public:
  using InputRegister = RegisterPair128i;
  using OutputRegister = RegisterPair128i;

  PostprocessImpl(const Identity& config) {}

  INTGEMM_SSE2 inline OutputRegister run(InputRegister input, Index offset) {
    return input;
  }
};

template <>
class PostprocessImpl<Identity, CPUType::AVX2> {
public:
  using InputRegister = __m256i;
  using OutputRegister = __m256i;

  PostprocessImpl(const Identity& config) {}

  INTGEMM_AVX2 inline OutputRegister run(InputRegister input, Index offset) {
    return input;
  }
};

template <>
class PostprocessImpl<Identity, CPUType::AVX512BW> {
public:
  using InputRegister = __m512i;
  using OutputRegister = __m512i;

  PostprocessImpl(const Identity& config) {}

  INTGEMM_AVX512BW inline OutputRegister run(InputRegister input, Index offset) {
    return input;
  }
};

/*
 * Add a bias term
 */
class AddBias {
public:
  const float* bias;
  const Index length;

  AddBias(const float* bias, Index length) : bias(bias), length(length) {}
};

template <>
class PostprocessImpl<AddBias, CPUType::SSE2> {
public:
  using InputRegister = RegisterPair128;
  using OutputRegister = RegisterPair128;

  PostprocessImpl(const AddBias& config) : config(config) {}

  INTGEMM_SSE2 inline OutputRegister run(InputRegister input, Index offset) {
    auto bias_term0123 = *reinterpret_cast<const __m128*>(config.bias + (offset % config.length));
    auto bias_term4567 = *reinterpret_cast<const __m128*>(config.bias + (offset % config.length) + 4);
    return {
      add_ps(input.pack0123, bias_term0123),
      add_ps(input.pack4567, bias_term4567),
    };
  }

private:
  const AddBias config;
};

template <>
class PostprocessImpl<AddBias, CPUType::AVX2> {
public:
  using InputRegister = __m256;
  using OutputRegister = __m256;

  PostprocessImpl(const AddBias& config) : config(config) {}

  INTGEMM_AVX2 inline OutputRegister run(InputRegister input, Index offset) {
    auto bias_term = *reinterpret_cast<const __m256*>(config.bias + (offset % config.length));
    return add_ps(input, bias_term);
  }

private:
  const AddBias config;
};

/*
 * ReLU
 */
class ReLU {};

template <>
class PostprocessImpl<ReLU, CPUType::SSE2> {
public:
  using InputRegister = RegisterPair128;
  using OutputRegister = RegisterPair128;

  PostprocessImpl(const ReLU& config) {}

  INTGEMM_SSE2 inline OutputRegister run(InputRegister input, Index offset) {
    static const auto const_zero = set1_ps<__m128>(0.f);
    return {
      max_ps(const_zero, input.pack0123),
      max_ps(const_zero, input.pack4567),
    };
  }
};

template <>
class PostprocessImpl<ReLU, CPUType::SSSE3> : public PostprocessImpl<ReLU, CPUType::SSE2> {};

template <>
class PostprocessImpl<ReLU, CPUType::AVX2> {
public:
  using InputRegister = __m256;
  using OutputRegister = __m256;

  PostprocessImpl(const ReLU& config) {}

  INTGEMM_AVX2 inline OutputRegister run(InputRegister input, Index offset) {
    static const auto const_zero = set1_ps<__m256>(0.f);
    return max_ps(const_zero, input);
  }
};

template <>
class PostprocessImpl<ReLU, CPUType::AVX512BW> {
public:
  using InputRegister = __m512;
  using OutputRegister = __m512;

  PostprocessImpl(const ReLU& config) {}

  INTGEMM_AVX512BW inline OutputRegister run(InputRegister input, Index offset) {
    static const auto const_zero = set1_ps<__m512>(0.f);
    return max_ps(const_zero, input);
  }
};

/*
 * Sigmoid (uses Taylor series approximation of e^x)
 */
class Sigmoid {};

template <>
class PostprocessImpl<Sigmoid, CPUType::AVX2> {
public:
  using InputRegister = __m256;
  using OutputRegister = __m256;

  PostprocessImpl(const Sigmoid& config) {}

  INTGEMM_AVX2 inline OutputRegister run(InputRegister input, Index offset) {
    static const auto const_zero = set1_ps<__m256>(0.f);
    static const auto const_one = set1_ps<__m256>(1.f);

    auto x = input;
    auto minus_x = sub_ps(const_zero, x);
    auto e_x = exp_approx_taylor(x);
    auto e_minus_x = exp_approx_taylor(minus_x);

    auto sigmoid_case1 = _mm256_rcp_ps(add_ps(const_one, e_minus_x));
    auto sigmoid_case2 = mul_ps(e_x, _mm256_rcp_ps(add_ps(const_one, e_x)));

    auto nonnegative_x_mask = _mm256_cmp_ps(const_zero, x, _CMP_LT_OS);
    return _mm256_blendv_ps(sigmoid_case1, sigmoid_case2, nonnegative_x_mask);
  }
};

}