/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/avx2/avx2_ops_fp16_f16c.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #if defined(__AVX2__) && defined(__F16C__) |
2 | | |
3 | | #include "avx2_fp16_f16c.h" |
4 | | #include <immintrin.h> |
5 | | #include <stdexcept> |
6 | | |
7 | | namespace labneura { |
8 | | |
9 | | namespace { |
10 | | |
11 | | constexpr int kRoundNearestEven = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC; |
12 | | |
13 | | struct AddOp { |
14 | 275 | static __m256 vec(__m256 a, __m256 b) { return _mm256_add_ps(a, b); } |
15 | 158 | static float scalar(float a, float b) { return a + b; } |
16 | | }; |
17 | | |
18 | | struct SubOp { |
19 | 187 | static __m256 vec(__m256 a, __m256 b) { return _mm256_sub_ps(a, b); } |
20 | 117 | static float scalar(float a, float b) { return a - b; } |
21 | | }; |
22 | | |
23 | | struct MulOp { |
24 | 187 | static __m256 vec(__m256 a, __m256 b) { return _mm256_mul_ps(a, b); } |
25 | 112 | static float scalar(float a, float b) { return a * b; } |
26 | | }; |
27 | | |
28 | | struct DivOp { |
29 | 706 | static __m256 vec(__m256 a, __m256 b) { return _mm256_div_ps(a, b); } |
30 | 181 | static float scalar(float a, float b) { return a / b; } |
31 | | }; |
32 | | |
33 | | // FP32 arithmetic on FP16 operands followed by one round-to-nearest-even |
34 | | // conversion is correctly rounded for these four operations. |
35 | | template <typename Op> |
36 | 220 | void run(int16_t* dst, const int16_t* src, std::size_t size) { |
37 | 220 | std::size_t i = 0; |
38 | 1.57k | for (; i + 8 <= size; i += 8) { |
39 | 1.35k | const __m256 a = _mm256_cvtph_ps( |
40 | 1.35k | _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst + i))); |
41 | 1.35k | const __m256 b = _mm256_cvtph_ps( |
42 | 1.35k | _mm_loadu_si128(reinterpret_cast<const __m128i*>(src + i))); |
43 | 1.35k | _mm_storeu_si128(reinterpret_cast<__m128i*>(dst + i), |
44 | 1.35k | _mm256_cvtps_ph(Op::vec(a, b), kRoundNearestEven)); |
45 | 1.35k | } |
46 | 788 | for (; i < size; ++i) { |
47 | 568 | const float a = _cvtsh_ss(static_cast<uint16_t>(dst[i])); |
48 | 568 | const float b = _cvtsh_ss(static_cast<uint16_t>(src[i])); |
49 | 568 | dst[i] = static_cast<int16_t>( |
50 | 568 | _cvtss_sh(Op::scalar(a, b), kRoundNearestEven)); |
51 | 568 | } |
52 | 220 | } avx2_ops_fp16_f16c.cpp:_ZN8labneura12_GLOBAL__N_13runINS0_5AddOpEEEvPsPKsm Line | Count | Source | 36 | 63 | void run(int16_t* dst, const int16_t* src, std::size_t size) { | 37 | 63 | std::size_t i = 0; | 38 | 338 | for (; i + 8 <= size; i += 8) { | 39 | 275 | const __m256 a = _mm256_cvtph_ps( | 40 | 275 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst + i))); | 41 | 275 | const __m256 b = _mm256_cvtph_ps( | 42 | 275 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(src + i))); | 43 | 275 | _mm_storeu_si128(reinterpret_cast<__m128i*>(dst + i), | 44 | 275 | _mm256_cvtps_ph(Op::vec(a, b), kRoundNearestEven)); | 45 | 275 | } | 46 | 221 | for (; i < size; ++i) { | 47 | 158 | const float a = _cvtsh_ss(static_cast<uint16_t>(dst[i])); | 48 | 158 | const float b = _cvtsh_ss(static_cast<uint16_t>(src[i])); | 49 | 158 | dst[i] = static_cast<int16_t>( | 50 | 158 | _cvtss_sh(Op::scalar(a, b), kRoundNearestEven)); | 51 | 158 | } | 52 | 63 | } |
avx2_ops_fp16_f16c.cpp:_ZN8labneura12_GLOBAL__N_13runINS0_5SubOpEEEvPsPKsm Line | Count | Source | 36 | 47 | void run(int16_t* dst, const int16_t* src, std::size_t size) { | 37 | 47 | std::size_t i = 0; | 38 | 234 | for (; i + 8 <= size; i += 8) { | 39 | 187 | const __m256 a = _mm256_cvtph_ps( | 40 | 187 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst + i))); | 41 | 187 | const __m256 b = _mm256_cvtph_ps( | 42 | 187 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(src + i))); | 43 | 187 | _mm_storeu_si128(reinterpret_cast<__m128i*>(dst + i), | 44 | 187 | _mm256_cvtps_ph(Op::vec(a, b), kRoundNearestEven)); | 45 | 187 | } | 46 | 164 | for (; i < size; ++i) { | 47 | 117 | const float a = _cvtsh_ss(static_cast<uint16_t>(dst[i])); | 48 | 117 | const float b = _cvtsh_ss(static_cast<uint16_t>(src[i])); | 49 | 117 | dst[i] = static_cast<int16_t>( | 50 | 117 | _cvtss_sh(Op::scalar(a, b), kRoundNearestEven)); | 51 | 117 | } | 52 | 47 | } |
avx2_ops_fp16_f16c.cpp:_ZN8labneura12_GLOBAL__N_13runINS0_5MulOpEEEvPsPKsm Line | Count | Source | 36 | 45 | void run(int16_t* dst, const int16_t* src, std::size_t size) { | 37 | 45 | std::size_t i = 0; | 38 | 232 | for (; i + 8 <= size; i += 8) { | 39 | 187 | const __m256 a = _mm256_cvtph_ps( | 40 | 187 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst + i))); | 41 | 187 | const __m256 b = _mm256_cvtph_ps( | 42 | 187 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(src + i))); | 43 | 187 | _mm_storeu_si128(reinterpret_cast<__m128i*>(dst + i), | 44 | 187 | _mm256_cvtps_ph(Op::vec(a, b), kRoundNearestEven)); | 45 | 187 | } | 46 | 157 | for (; i < size; ++i) { | 47 | 112 | const float a = _cvtsh_ss(static_cast<uint16_t>(dst[i])); | 48 | 112 | const float b = _cvtsh_ss(static_cast<uint16_t>(src[i])); | 49 | 112 | dst[i] = static_cast<int16_t>( | 50 | 112 | _cvtss_sh(Op::scalar(a, b), kRoundNearestEven)); | 51 | 112 | } | 52 | 45 | } |
avx2_ops_fp16_f16c.cpp:_ZN8labneura12_GLOBAL__N_13runINS0_5DivOpEEEvPsPKsm Line | Count | Source | 36 | 65 | void run(int16_t* dst, const int16_t* src, std::size_t size) { | 37 | 65 | std::size_t i = 0; | 38 | 771 | for (; i + 8 <= size; i += 8) { | 39 | 706 | const __m256 a = _mm256_cvtph_ps( | 40 | 706 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst + i))); | 41 | 706 | const __m256 b = _mm256_cvtph_ps( | 42 | 706 | _mm_loadu_si128(reinterpret_cast<const __m128i*>(src + i))); | 43 | 706 | _mm_storeu_si128(reinterpret_cast<__m128i*>(dst + i), | 44 | 706 | _mm256_cvtps_ph(Op::vec(a, b), kRoundNearestEven)); | 45 | 706 | } | 46 | 246 | for (; i < size; ++i) { | 47 | 181 | const float a = _cvtsh_ss(static_cast<uint16_t>(dst[i])); | 48 | 181 | const float b = _cvtsh_ss(static_cast<uint16_t>(src[i])); | 49 | 181 | dst[i] = static_cast<int16_t>( | 50 | 181 | _cvtss_sh(Op::scalar(a, b), kRoundNearestEven)); | 51 | 181 | } | 52 | 65 | } |
|
53 | | |
54 | | } // namespace |
55 | | |
56 | | void avx2_fp16_f16c_kernel(int16_t* dst, const int16_t* src, std::size_t size, |
57 | 220 | OperationType op_type) { |
58 | 220 | switch (op_type) { |
59 | 63 | case ADD: run<AddOp>(dst, src, size); break; |
60 | 47 | case SUB: run<SubOp>(dst, src, size); break; |
61 | 45 | case MUL: run<MulOp>(dst, src, size); break; |
62 | 65 | case DIV: run<DivOp>(dst, src, size); break; |
63 | 0 | default: throw std::invalid_argument("Unsupported FP16 operation"); |
64 | 220 | } |
65 | 220 | } |
66 | | |
67 | | } // namespace labneura |
68 | | |
69 | | #endif // __AVX2__ && __F16C__ |