Coverage Report

Created: 2026-11-07 16:24

/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
namespace {
13
14
std::shared_ptr<TensorStorage> make_avx2_storage(
15
3.14k
    std::size_t size, QuantizationMode mode) {
16
3.14k
    return TensorStorage::allocate(
17
3.14k
        size, util::checked_round_up(size, 8), mode, util::AVX2_ALIGN);
18
3.14k
}
19
20
}  // namespace
21
22
// =======================
23
// Constructor
24
// =======================
25
26
AVX2Backend::AVX2Backend(std::size_t size, QuantizationMode mode)
27
3.14k
    : TensorBackend(make_avx2_storage(size, mode)),
28
3.14k
      size_(size),
29
3.14k
      aligned_size_(storage_->allocation_capacity),
30
3.14k
      quantization_mode_(mode) {
31
3.14k
    if (mode == QuantizationMode::FP32) {
32
1.30k
        data_fp32_ = static_cast<float*>(storage_->data());
33
1.30k
        std::fill(data_fp32_ + size_, data_fp32_ + aligned_size_, 0.0f);
34
1.84k
    } else if (mode == QuantizationMode::FP16) {
35
611
        data_fp16_ = static_cast<int16_t*>(storage_->data());
36
611
        std::fill(data_fp16_ + size_, data_fp16_ + aligned_size_, static_cast<int16_t>(0));
37
1.23k
    } else if (mode == QuantizationMode::INT16) {
38
360
        data_int16_ = static_cast<int16_t*>(storage_->data());
39
360
        std::fill(data_int16_ + size_, data_int16_ + aligned_size_, static_cast<int16_t>(0));
40
877
    } else if (mode == QuantizationMode::BOOL) {
41
228
        data_bool_ = static_cast<std::uint8_t*>(storage_->data());
42
228
        std::fill(data_bool_, data_bool_ + aligned_size_, std::uint8_t{0});
43
649
    } else {
44
649
        data_int8_ = static_cast<int8_t*>(storage_->data());
45
649
        std::fill(data_int8_ + size_, data_int8_ + aligned_size_, 0);
46
649
    }
47
3.14k
}
48
49
AVX2Backend::AVX2Backend(std::shared_ptr<TensorStorage> storage,
50
                         std::size_t logical_size,
51
                         std::size_t storage_offset)
52
82
    : TensorBackend(std::move(storage), logical_size, storage_offset),
53
82
      size_(logical_size),
54
82
      aligned_size_(storage_->allocation_capacity),
55
82
      quantization_mode_(storage_->dtype) {
56
82
    if (quantization_mode_ == QuantizationMode::FP32) {
57
44
        data_fp32_ = data_at_storage_offset<float>();
58
44
    } else if (quantization_mode_ == QuantizationMode::FP16) {
59
12
        data_fp16_ = data_at_storage_offset<int16_t>();
60
26
    } else if (quantization_mode_ == QuantizationMode::INT16) {
61
12
        data_int16_ = data_at_storage_offset<int16_t>();
62
14
    } else if (quantization_mode_ == QuantizationMode::BOOL) {
63
0
        data_bool_ = data_at_storage_offset<std::uint8_t>();
64
14
    } else {
65
14
        data_int8_ = data_at_storage_offset<int8_t>();
66
14
    }
67
82
}
68
69
// =======================
70
// Metadata
71
// =======================
72
73
181k
std::size_t AVX2Backend::size() const {
74
181k
    return size_;
75
181k
}
76
77
5.79k
QuantizationMode AVX2Backend::quantization_mode() const {
78
5.79k
    return quantization_mode_;
79
5.79k
}
80
81
// =======================
82
// Data access
83
// =======================
84
85
16.1k
float* AVX2Backend::data_fp32() {
86
16.1k
    if (quantization_mode_ != QuantizationMode::FP32) {
87
0
        throw std::runtime_error("Tensor is not in FP32 mode");
88
0
    }
89
16.1k
    return data_fp32_;
90
16.1k
}
91
92
782
const float* AVX2Backend::data_fp32() const {
93
782
    if (quantization_mode_ != QuantizationMode::FP32) {
94
0
        throw std::runtime_error("Tensor is not in FP32 mode");
95
0
    }
96
782
    return data_fp32_;
97
782
}
98
99
1.90k
int16_t* AVX2Backend::data_fp16() {
100
1.90k
    if (quantization_mode_ != QuantizationMode::FP16) {
101
0
        throw std::runtime_error("Tensor is not in FP16 mode");
102
0
    }
103
1.90k
    return data_fp16_;
104
1.90k
}
105
106
328
const int16_t* AVX2Backend::data_fp16() const {
107
328
    if (quantization_mode_ != QuantizationMode::FP16) {
108
0
        throw std::runtime_error("Tensor is not in FP16 mode");
109
0
    }
110
328
    return data_fp16_;
111
328
}
112
113
1.42k
int16_t* AVX2Backend::data_int16() {
114
1.42k
    if (quantization_mode_ != QuantizationMode::INT16) {
115
0
        throw std::runtime_error("Tensor is not in INT16 mode");
116
0
    }
117
1.42k
    return data_int16_;
118
1.42k
}
119
120
191
const int16_t* AVX2Backend::data_int16() const {
121
191
    if (quantization_mode_ != QuantizationMode::INT16) {
122
0
        throw std::runtime_error("Tensor is not in INT16 mode");
123
0
    }
124
191
    return data_int16_;
125
191
}
126
127
3.66k
int8_t* AVX2Backend::data_int8() {
128
3.66k
    if (quantization_mode_ != QuantizationMode::INT8) {
129
0
        throw std::runtime_error("Tensor is not in INT8 mode");
130
0
    }
131
3.66k
    return data_int8_;
132
3.66k
}
133
134
300
const int8_t* AVX2Backend::data_int8() const {
135
300
    if (quantization_mode_ != QuantizationMode::INT8) {
136
0
        throw std::runtime_error("Tensor is not in INT8 mode");
137
0
    }
138
300
    return data_int8_;
139
300
}
140
141
612
std::uint8_t* AVX2Backend::data_bool() {
142
612
    if (quantization_mode_ != QuantizationMode::BOOL) {
143
0
        throw std::runtime_error("Tensor is not in BOOL mode");
144
0
    }
145
612
    return canonicalize_bool_data(data_bool_);
146
612
}
147
148
0
const std::uint8_t* AVX2Backend::data_bool() const {
149
0
    if (quantization_mode_ != QuantizationMode::BOOL) {
150
0
        throw std::runtime_error("Tensor is not in BOOL mode");
151
0
    }
152
0
    return validate_bool_data(data_bool_);
153
0
}
154
155
// =======================
156
// Clone
157
// =======================
158
159
261
std::unique_ptr<TensorBackend> AVX2Backend::clone() const {
160
261
    auto backend = std::make_unique<AVX2Backend>(size_, quantization_mode_);
161
261
    if (quantization_mode_ == QuantizationMode::FP32) {
162
33
        std::copy(data_fp32_, data_fp32_ + size_, backend->data_fp32());
163
228
    } else if (quantization_mode_ == QuantizationMode::FP16) {
164
140
        std::copy(data_fp16_, data_fp16_ + size_, backend->data_fp16());
165
140
    } else if (quantization_mode_ == QuantizationMode::INT16) {
166
37
        std::copy(data_int16_, data_int16_ + size_, backend->data_int16());
167
51
    } else if (quantization_mode_ == QuantizationMode::BOOL) {
168
0
        std::copy(data_bool_, data_bool_ + size_, backend->data_bool());
169
51
    } else {
170
51
        std::copy(data_int8_, data_int8_ + size_, backend->data_int8());
171
51
    }
172
261
    return backend;
173
261
}
174
175
std::unique_ptr<TensorBackend> AVX2Backend::create_storage_alias(
176
82
    std::size_t logical_size, std::size_t storage_offset) const {
177
82
    return std::unique_ptr<TensorBackend>(
178
82
        new AVX2Backend(storage_, logical_size, storage_offset));
179
82
}
180
181
} // namespace labneura
182
183
#endif // __AVX2__