/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/avx2/avx2_ops_fp16.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #if defined(__AVX2__) |
2 | | |
3 | | #include "labneura/backends/cpu/avx2.h" |
4 | | #include "labneura/backends/common/cpu_features.h" |
5 | | #include "labneura/backends/common/fp16_kernel_selection.h" |
6 | | #include "labneura/types.hpp" |
7 | | #include <atomic> |
8 | | #include <cstdint> |
9 | | #include <cstring> |
10 | | #include <stdexcept> |
11 | | |
12 | | #if defined(LABNEURA_ENABLE_AVX2_F16C) |
13 | | #include "avx2_fp16_f16c.h" |
14 | | #endif |
15 | | |
16 | | namespace labneura { |
17 | | |
18 | | namespace { |
19 | | |
20 | | // Software-conversion fallback for CPUs without F16C. IEEE semantics come from |
21 | | // quantize_fp16/dequantize_fp16; this file is built without -mf16c. |
22 | | template <typename Op> |
23 | 0 | void run_scalar(int16_t* dst, const int16_t* src, std::size_t size, Op op) { |
24 | 0 | for (std::size_t i = 0; i < size; ++i) { |
25 | 0 | uint16_t lhs_bits; |
26 | 0 | uint16_t rhs_bits; |
27 | 0 | std::memcpy(&lhs_bits, &dst[i], sizeof(lhs_bits)); |
28 | 0 | std::memcpy(&rhs_bits, &src[i], sizeof(rhs_bits)); |
29 | 0 | const uint16_t result_bits = |
30 | 0 | quantize_fp16(op(dequantize_fp16(lhs_bits), dequantize_fp16(rhs_bits))); |
31 | 0 | std::memcpy(&dst[i], &result_bits, sizeof(result_bits)); |
32 | 0 | } |
33 | 0 | } Unexecuted instantiation: avx2_ops_fp16.cpp:_ZN8labneura12_GLOBAL__N_110run_scalarIZNS_11AVX2Backend14operation_fp16ERKNS_13TensorBackendENS_13OperationTypeEE3$_0EEvPsPKsmT_ Unexecuted instantiation: avx2_ops_fp16.cpp:_ZN8labneura12_GLOBAL__N_110run_scalarIZNS_11AVX2Backend14operation_fp16ERKNS_13TensorBackendENS_13OperationTypeEE3$_1EEvPsPKsmT_ Unexecuted instantiation: avx2_ops_fp16.cpp:_ZN8labneura12_GLOBAL__N_110run_scalarIZNS_11AVX2Backend14operation_fp16ERKNS_13TensorBackendENS_13OperationTypeEE3$_2EEvPsPKsmT_ Unexecuted instantiation: avx2_ops_fp16.cpp:_ZN8labneura12_GLOBAL__N_110run_scalarIZNS_11AVX2Backend14operation_fp16ERKNS_13TensorBackendENS_13OperationTypeEE3$_3EEvPsPKsmT_ |
34 | | |
35 | | #if defined(LABNEURA_ENABLE_AVX2_F16C) |
36 | | constexpr bool kF16CCompiled = true; |
37 | | #else |
38 | | constexpr bool kF16CCompiled = false; |
39 | | #endif |
40 | | |
41 | | std::atomic<bool> g_force_scalar{false}; |
42 | | |
43 | | } // namespace |
44 | | |
45 | | namespace detail { |
46 | | |
47 | 0 | bool avx2_fp16_f16c_compiled() { return kF16CCompiled; } |
48 | | |
49 | 220 | AVX2FP16Kernel avx2_fp16_active_kernel() { |
50 | 220 | static const bool f16c_supported = cpu_supports_f16c(); |
51 | 220 | if (g_force_scalar.load(std::memory_order_relaxed)) { |
52 | 0 | return AVX2FP16Kernel::SCALAR; |
53 | 0 | } |
54 | 220 | return select_avx2_fp16_kernel(kF16CCompiled, f16c_supported); |
55 | 220 | } |
56 | | |
57 | 0 | void avx2_fp16_force_scalar_for_testing(bool force) { |
58 | 0 | g_force_scalar.store(force, std::memory_order_relaxed); |
59 | 0 | } |
60 | | |
61 | | } // namespace detail |
62 | | |
63 | 220 | void AVX2Backend::operation_fp16(const TensorBackend& other, OperationType op_type) { |
64 | 220 | const int16_t* other_data = other.data_fp16(); |
65 | 220 | int16_t* this_data = data_fp16(); |
66 | | |
67 | 220 | if (detail::avx2_fp16_active_kernel() == detail::AVX2FP16Kernel::F16C) { |
68 | 220 | #if defined(LABNEURA_ENABLE_AVX2_F16C) |
69 | 220 | avx2_fp16_f16c_kernel(this_data, other_data, size_, op_type); |
70 | 220 | return; |
71 | 220 | #endif |
72 | 220 | } |
73 | | |
74 | 0 | switch (op_type) { |
75 | 0 | case ADD: |
76 | 0 | run_scalar(this_data, other_data, size_, [](float a, float b) { return a + b; }); |
77 | 0 | break; |
78 | 0 | case SUB: |
79 | 0 | run_scalar(this_data, other_data, size_, [](float a, float b) { return a - b; }); |
80 | 0 | break; |
81 | 0 | case MUL: |
82 | 0 | run_scalar(this_data, other_data, size_, [](float a, float b) { return a * b; }); |
83 | 0 | break; |
84 | 0 | case DIV: |
85 | 0 | run_scalar(this_data, other_data, size_, [](float a, float b) { return a / b; }); |
86 | 0 | break; |
87 | 0 | default: |
88 | 0 | throw std::invalid_argument("Unsupported FP16 operation"); |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | | } // namespace labneura |
93 | | |
94 | | #endif // __AVX2__ |