/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/types.hpp" |
5 | | #include <cstdint> |
6 | | #include <cstring> |
7 | | #include <stdexcept> |
8 | | |
9 | | namespace labneura { |
10 | | |
11 | 66 | void AVX2Backend::operation_fp16(const TensorBackend& other, OperationType op_type) { |
12 | 66 | const int16_t* other_data = other.data_fp16(); |
13 | 66 | int16_t* this_data = data_fp16(); |
14 | | |
15 | 7.15k | for (std::size_t i = 0; i < size_; ++i) { |
16 | 7.08k | uint16_t lhs_bits; |
17 | 7.08k | uint16_t rhs_bits; |
18 | 7.08k | std::memcpy(&lhs_bits, &this_data[i], sizeof(lhs_bits)); |
19 | 7.08k | std::memcpy(&rhs_bits, &other_data[i], sizeof(rhs_bits)); |
20 | | |
21 | 7.08k | const float lhs = dequantize_fp16(lhs_bits); |
22 | 7.08k | const float rhs = dequantize_fp16(rhs_bits); |
23 | 7.08k | float result; |
24 | 7.08k | switch (op_type) { |
25 | 1.27k | case OperationType::ADD: |
26 | 1.27k | result = lhs + rhs; |
27 | 1.27k | break; |
28 | 534 | case OperationType::SUB: |
29 | 534 | result = lhs - rhs; |
30 | 534 | break; |
31 | 529 | case OperationType::MUL: |
32 | 529 | result = lhs * rhs; |
33 | 529 | break; |
34 | 4.74k | case OperationType::DIV: |
35 | 4.74k | result = rhs == 0.0f ? 0.0f : lhs / rhs; |
36 | 4.74k | break; |
37 | 0 | default: |
38 | 0 | throw std::invalid_argument("Unsupported FP16 operation"); |
39 | 7.08k | } |
40 | | |
41 | 7.08k | const uint16_t result_bits = quantize_fp16(result); |
42 | 7.08k | std::memcpy(&this_data[i], &result_bits, sizeof(result_bits)); |
43 | 7.08k | } |
44 | 66 | } |
45 | | |
46 | | } // namespace labneura |
47 | | |
48 | | #endif // __AVX2__ |