Coverage Report

Created: 2026-11-03 04:34

/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/avx2/avx2_backend.cpp
Line
Count
Source (jump to first uncovered line)
1
#if defined(__AVX2__)
2
3
#include "labneura/backends/cpu/avx2.h"
4
#include "avx2_internal.h"
5
#include "labneura/utils/alignment.hpp"
6
#include <memory>
7
#include <algorithm>
8
#include <stdexcept>
9
10
namespace labneura {
11
12
// =======================
13
// Constructor
14
// =======================
15
16
AVX2Backend::AVX2Backend(std::size_t size, QuantizationMode mode)
17
2.02k
    : size_(size), quantization_mode_(mode) {
18
19
    // AVX2: 256-bit registers → 8 FP32 elements
20
2.02k
    aligned_size_ = ((size + 7) / 8) * 8;
21
22
2.02k
    if (mode == QuantizationMode::FP32) {
23
1.05k
        float* p = labneura::util::allocate_aligned<float>(aligned_size_, labneura::util::AVX2_ALIGN);
24
1.05k
        data_fp32_ = std::unique_ptr<float, void(*)(float*)>(p, labneura::util::free_aligned_float);
25
1.05k
        std::fill(data_fp32_.get() + size_, data_fp32_.get() + aligned_size_, 0.0f);
26
1.05k
    } else if (mode == QuantizationMode::FP16) {
27
181
        int16_t* p = labneura::util::allocate_aligned<int16_t>(aligned_size_, labneura::util::AVX2_ALIGN);
28
181
        auto fp16_deleter = [](int16_t* ptr){ labneura::util::aligned_free(ptr); };
29
181
        data_fp16_ = std::unique_ptr<int16_t, void(*)(int16_t*)>(p, fp16_deleter);
30
181
        std::fill(data_fp16_.get() + size_, data_fp16_.get() + aligned_size_, static_cast<int16_t>(0));
31
785
    } else if (mode == QuantizationMode::INT16) {
32
255
        int16_t* p = labneura::util::allocate_aligned<int16_t>(aligned_size_, labneura::util::AVX2_ALIGN);
33
255
        auto int16_deleter = [](int16_t* ptr){ labneura::util::aligned_free(ptr); };
34
255
        data_int16_ = std::unique_ptr<int16_t, void(*)(int16_t*)>(p, int16_deleter);
35
255
        std::fill(data_int16_.get() + size_, data_int16_.get() + aligned_size_, static_cast<int16_t>(0));
36
530
    } else {
37
530
        int8_t* p = labneura::util::allocate_aligned<int8_t>(aligned_size_, labneura::util::AVX2_ALIGN);
38
530
        data_int8_ = std::unique_ptr<int8_t, void(*)(int8_t*)>(p, labneura::util::free_aligned_int8);
39
530
        std::fill(data_int8_.get() + size_, data_int8_.get() + aligned_size_, 0);
40
530
    }
41
2.02k
}
42
43
// =======================
44
// Metadata
45
// =======================
46
47
157k
std::size_t AVX2Backend::size() const {
48
157k
    return size_;
49
157k
}
50
51
3.38k
QuantizationMode AVX2Backend::quantization_mode() const {
52
3.38k
    return quantization_mode_;
53
3.38k
}
54
55
// =======================
56
// Data access
57
// =======================
58
59
15.5k
float* AVX2Backend::data_fp32() {
60
15.5k
    if (quantization_mode_ != QuantizationMode::FP32) {
61
0
        throw std::runtime_error("Tensor is not in FP32 mode");
62
0
    }
63
15.5k
    return data_fp32_.get();
64
15.5k
}
65
66
528
const float* AVX2Backend::data_fp32() const {
67
528
    if (quantization_mode_ != QuantizationMode::FP32) {
68
0
        throw std::runtime_error("Tensor is not in FP32 mode");
69
0
    }
70
528
    return data_fp32_.get();
71
528
}
72
73
1.03k
int16_t* AVX2Backend::data_fp16() {
74
1.03k
    if (quantization_mode_ != QuantizationMode::FP16) {
75
0
        throw std::runtime_error("Tensor is not in FP16 mode");
76
0
    }
77
1.03k
    return data_fp16_.get();
78
1.03k
}
79
80
66
const int16_t* AVX2Backend::data_fp16() const {
81
66
    if (quantization_mode_ != QuantizationMode::FP16) {
82
0
        throw std::runtime_error("Tensor is not in FP16 mode");
83
0
    }
84
66
    return data_fp16_.get();
85
66
}
86
87
1.19k
int16_t* AVX2Backend::data_int16() {
88
1.19k
    if (quantization_mode_ != QuantizationMode::INT16) {
89
0
        throw std::runtime_error("Tensor is not in INT16 mode");
90
0
    }
91
1.19k
    return data_int16_.get();
92
1.19k
}
93
94
98
const int16_t* AVX2Backend::data_int16() const {
95
98
    if (quantization_mode_ != QuantizationMode::INT16) {
96
0
        throw std::runtime_error("Tensor is not in INT16 mode");
97
0
    }
98
98
    return data_int16_.get();
99
98
}
100
101
3.26k
int8_t* AVX2Backend::data_int8() {
102
3.26k
    if (quantization_mode_ != QuantizationMode::INT8) {
103
0
        throw std::runtime_error("Tensor is not in INT8 mode");
104
0
    }
105
3.26k
    return data_int8_.get();
106
3.26k
}
107
108
202
const int8_t* AVX2Backend::data_int8() const {
109
202
    if (quantization_mode_ != QuantizationMode::INT8) {
110
0
        throw std::runtime_error("Tensor is not in INT8 mode");
111
0
    }
112
202
    return data_int8_.get();
113
202
}
114
115
// =======================
116
// Clone
117
// =======================
118
119
240
std::unique_ptr<TensorBackend> AVX2Backend::clone() const {
120
240
    auto backend = std::make_unique<AVX2Backend>(size_, quantization_mode_);
121
240
    if (quantization_mode_ == QuantizationMode::FP32) {
122
114
        std::copy(data_fp32_.get(), data_fp32_.get() + size_, backend->data_fp32());
123
126
    } else if (quantization_mode_ == QuantizationMode::FP16) {
124
37
        std::copy(data_fp16_.get(), data_fp16_.get() + size_, backend->data_fp16());
125
89
    } else if (quantization_mode_ == QuantizationMode::INT16) {
126
39
        std::copy(data_int16_.get(), data_int16_.get() + size_, backend->data_int16());
127
50
    } else {
128
50
        std::copy(data_int8_.get(), data_int8_.get() + size_, backend->data_int8());
129
50
    }
130
240
    return backend;
131
240
}
132
133
} // namespace labneura
134
135
#endif // __AVX2__