/home/runner/work/LabNeura/LabNeura/src/labneura/tensor.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/tensor.h" |
2 | | #include "labneura/backends/cpu/generic.h" |
3 | | #include "labneura/backends/cpu/neon.h" |
4 | | #include "labneura/backends/cpu/avx2.h" |
5 | | #include "labneura/backends/common/backend_factory.h" |
6 | | #include "labneura/types.hpp" |
7 | | #include <cstring> |
8 | | #include <algorithm> |
9 | | #include <cmath> |
10 | | #include <iostream> |
11 | | |
12 | | namespace labneura { |
13 | | |
14 | | namespace { |
15 | 3 | std::vector<float> flatten_rows(const std::vector<std::vector<float>>& rows) { |
16 | 3 | std::vector<float> flat; |
17 | 3 | if (rows.empty()) return flat; |
18 | 3 | const std::size_t cols = rows.front().size(); |
19 | 3 | flat.reserve(rows.size() * cols); |
20 | 6 | for (const auto& row : rows) { |
21 | 6 | if (row.size() != cols) { |
22 | 1 | throw std::invalid_argument( |
23 | 1 | "2-D Tensor rows must all have the same length"); |
24 | 1 | } |
25 | 5 | flat.insert(flat.end(), row.begin(), row.end()); |
26 | 5 | } |
27 | 2 | return flat; |
28 | 3 | } |
29 | | } // namespace |
30 | | |
31 | | // Tensor implementation |
32 | 29 | Tensor::Tensor() : quantization_mode_(QuantizationMode::FP32), shape_{0} { |
33 | 29 | init_backend(0); |
34 | 29 | } |
35 | | |
36 | | Tensor::Tensor(const std::vector<std::vector<float>>& rows, QuantizationMode mode) |
37 | 3 | : Tensor(flatten_rows(rows), mode) { |
38 | 3 | shape_ = {rows.size(), rows.empty() ? std::size_t{0} : rows.front().size()}; |
39 | 3 | } |
40 | | |
41 | | Tensor::Tensor(const std::vector<float>& data, QuantizationMode mode) |
42 | 1.75k | : quantization_mode_(mode), shape_{data.size()} { |
43 | 1.75k | init_backend(data.size()); |
44 | 1.75k | if (!data.empty()) { |
45 | 1.74k | if (mode == QuantizationMode::FP32) { |
46 | 906 | float* fp32_data = data_fp32(); |
47 | 205M | for (std::size_t i = 0; i < data.size(); ++i) { |
48 | 205M | fp32_data[i] = data[i]; |
49 | 205M | } |
50 | 906 | } else if (mode == QuantizationMode::FP16) { |
51 | 142 | int16_t* fp16_data = data_fp16(); |
52 | 12.2k | for (std::size_t i = 0; i < data.size(); ++i) { |
53 | | // Proper FP16 encoding (stored in int16_t for NEON compatibility) |
54 | 12.1k | uint16_t encoded = quantize_fp16(data[i]); |
55 | 12.1k | std::memcpy(&fp16_data[i], &encoded, sizeof(uint16_t)); |
56 | 12.1k | } |
57 | 699 | } else if (mode == QuantizationMode::INT8) { |
58 | 490 | int8_t* int8_data = data_int8(); |
59 | 200M | for (std::size_t i = 0; i < data.size(); ++i) { |
60 | 200M | int v = static_cast<int>(std::lrintf(data[i])); |
61 | 200M | int8_data[i] = static_cast<int8_t>(std::max(-128, std::min(127, v))); |
62 | 200M | } |
63 | 490 | } else if (mode == QuantizationMode::INT16) { |
64 | 209 | int16_t* int16_data = data_int16(); |
65 | 23.7k | for (std::size_t i = 0; i < data.size(); ++i) { |
66 | 23.4k | int v = static_cast<int>(std::lrintf(data[i])); |
67 | 23.4k | int16_data[i] = static_cast<int16_t>(std::max(-32768, std::min(32767, v))); |
68 | 23.4k | } |
69 | 209 | } else { |
70 | | // Other modes: default to FP32 |
71 | 0 | float* fp32_data = data_fp32(); |
72 | 0 | for (std::size_t i = 0; i < data.size(); ++i) { |
73 | 0 | fp32_data[i] = data[i]; |
74 | 0 | } |
75 | 0 | } |
76 | 1.74k | } |
77 | 1.75k | } |
78 | | |
79 | | Tensor::Tensor(const std::vector<int>& data, QuantizationMode mode) |
80 | 0 | : quantization_mode_(mode), shape_{data.size()} { |
81 | 0 | init_backend(data.size()); |
82 | 0 | if (mode == QuantizationMode::FP32) { |
83 | 0 | float* fp32_data = data_fp32(); |
84 | 0 | for (std::size_t i = 0; i < data.size(); ++i) { |
85 | 0 | fp32_data[i] = static_cast<float>(data[i]); |
86 | 0 | } |
87 | 0 | } else if (mode == QuantizationMode::FP16) { |
88 | 0 | int16_t* fp16_data = data_fp16(); |
89 | 0 | for (std::size_t i = 0; i < data.size(); ++i) { |
90 | | // Proper FP16 encoding from int (convert to float first, then quantize) |
91 | 0 | uint16_t encoded = quantize_fp16(static_cast<float>(data[i])); |
92 | 0 | std::memcpy(&fp16_data[i], &encoded, sizeof(uint16_t)); |
93 | 0 | } |
94 | 0 | } else if (mode == QuantizationMode::INT8) { |
95 | 0 | int8_t* int8_data = data_int8(); |
96 | 0 | for (std::size_t i = 0; i < data.size(); ++i) { |
97 | 0 | int8_data[i] = static_cast<int8_t>( |
98 | 0 | std::max(-128, std::min(127, data[i])) |
99 | 0 | ); |
100 | 0 | } |
101 | 0 | } else if (mode == QuantizationMode::INT16) { |
102 | 0 | int16_t* int16_data = data_int16(); |
103 | 0 | for (std::size_t i = 0; i < data.size(); ++i) { |
104 | 0 | int16_data[i] = static_cast<int16_t>( |
105 | 0 | std::max(-32768, std::min(32767, data[i])) |
106 | 0 | ); |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | Tensor::Tensor(float scalar, QuantizationMode mode) |
112 | 52 | : quantization_mode_(mode), shape_{1} { |
113 | 52 | init_backend(1); |
114 | 52 | if (mode == QuantizationMode::FP32) { |
115 | 26 | data_fp32()[0] = scalar; |
116 | 26 | } else if (mode == QuantizationMode::FP16) { |
117 | 5 | uint16_t encoded = quantize_fp16(scalar); |
118 | 5 | std::memcpy(&data_fp16()[0], &encoded, sizeof(uint16_t)); |
119 | 21 | } else if (mode == QuantizationMode::INT8) { |
120 | 11 | int v = static_cast<int>(std::lrintf(scalar)); |
121 | 11 | data_int8()[0] = static_cast<int8_t>(std::max(-128, std::min(127, v))); |
122 | 11 | } else if (mode == QuantizationMode::INT16) { |
123 | 10 | int v = static_cast<int>(std::lrintf(scalar)); |
124 | 10 | data_int16()[0] = static_cast<int16_t>(std::max(-32768, std::min(32767, v))); |
125 | 10 | } else { |
126 | 0 | data_fp32()[0] = scalar; |
127 | 0 | } |
128 | 52 | } |
129 | | |
130 | | Tensor::Tensor(int scalar, QuantizationMode mode) |
131 | 0 | : quantization_mode_(mode), shape_{1} { |
132 | 0 | init_backend(1); |
133 | 0 | if (mode == QuantizationMode::FP32) { |
134 | 0 | data_fp32()[0] = static_cast<float>(scalar); |
135 | 0 | } else if (mode == QuantizationMode::FP16) { |
136 | 0 | uint16_t encoded = quantize_fp16(static_cast<float>(scalar)); |
137 | 0 | std::memcpy(&data_fp16()[0], &encoded, sizeof(uint16_t)); |
138 | 0 | } else if (mode == QuantizationMode::INT8) { |
139 | 0 | data_int8()[0] = static_cast<int8_t>( |
140 | 0 | std::max(-128, std::min(127, scalar)) |
141 | 0 | ); |
142 | 0 | } else if (mode == QuantizationMode::INT16) { |
143 | 0 | data_int16()[0] = static_cast<int16_t>( |
144 | 0 | std::max(-32768, std::min(32767, scalar)) |
145 | 0 | ); |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | 2.33k | Tensor::~Tensor() = default; |
150 | | |
151 | | Tensor::Tensor(const Tensor& other) |
152 | 253 | : quantization_mode_(other.quantization_mode_), |
153 | 253 | backend_(other.backend_ ? other.backend_->clone() : nullptr), |
154 | 253 | shape_(other.shape_) {} |
155 | | |
156 | 0 | Tensor& Tensor::operator=(const Tensor& other) { |
157 | 0 | if (this != &other) { |
158 | 0 | quantization_mode_ = other.quantization_mode_; |
159 | 0 | backend_ = other.backend_ ? other.backend_->clone() : nullptr; |
160 | 0 | shape_ = other.shape_; |
161 | 0 | } |
162 | 0 | return *this; |
163 | 0 | } |
164 | | |
165 | | Tensor::Tensor(Tensor&& other) noexcept |
166 | 253 | : quantization_mode_(other.quantization_mode_), |
167 | 253 | backend_(std::move(other.backend_)), |
168 | 253 | shape_(std::move(other.shape_)) {} |
169 | | |
170 | 0 | Tensor& Tensor::operator=(Tensor&& other) noexcept { |
171 | 0 | if (this != &other) { |
172 | 0 | quantization_mode_ = other.quantization_mode_; |
173 | 0 | backend_ = std::move(other.backend_); |
174 | 0 | shape_ = std::move(other.shape_); |
175 | 0 | } |
176 | 0 | return *this; |
177 | 0 | } |
178 | | |
179 | 155k | std::size_t Tensor::size() const { |
180 | 155k | return backend_ ? backend_->size() : 0; |
181 | 155k | } |
182 | | |
183 | 14.9k | float* Tensor::data_fp32() { |
184 | 14.9k | return backend_ ? backend_->data_fp32() : nullptr; |
185 | 14.9k | } |
186 | | |
187 | 0 | const float* Tensor::data_fp32() const { |
188 | 0 | return backend_ ? backend_->data_fp32() : nullptr; |
189 | 0 | } |
190 | | |
191 | 3.06k | int8_t* Tensor::data_int8() { |
192 | 3.06k | return backend_ ? backend_->data_int8() : nullptr; |
193 | 3.06k | } |
194 | | |
195 | 0 | const int8_t* Tensor::data_int8() const { |
196 | 0 | return backend_ ? backend_->data_int8() : nullptr; |
197 | 0 | } |
198 | | |
199 | 931 | int16_t* Tensor::data_fp16() { |
200 | 931 | return backend_ ? backend_->data_fp16() : nullptr; |
201 | 931 | } |
202 | | |
203 | 0 | const int16_t* Tensor::data_fp16() const { |
204 | 0 | return backend_ ? backend_->data_fp16() : nullptr; |
205 | 0 | } |
206 | | |
207 | 1.06k | int16_t* Tensor::data_int16() { |
208 | 1.06k | return backend_ ? backend_->data_int16() : nullptr; |
209 | 1.06k | } |
210 | | |
211 | 0 | const int16_t* Tensor::data_int16() const { |
212 | 0 | return backend_ ? backend_->data_int16() : nullptr; |
213 | 0 | } |
214 | | |
215 | 63 | Tensor Tensor::add(const Tensor& other) const { |
216 | 63 | Tensor result(*this); |
217 | 63 | result.add_inplace(other); |
218 | 63 | return result; |
219 | 63 | } |
220 | | |
221 | 27 | Tensor Tensor::mul(const Tensor& other) const { |
222 | 27 | Tensor result(*this); |
223 | 27 | result.mul_inplace(other); |
224 | 27 | return result; |
225 | 27 | } |
226 | | |
227 | 26 | Tensor Tensor::sub(const Tensor& other) const { |
228 | 26 | Tensor result(*this); |
229 | 26 | result.sub_inplace(other); |
230 | 26 | return result; |
231 | 26 | } |
232 | | |
233 | 107 | Tensor Tensor::div(const Tensor& other) const { |
234 | 107 | Tensor result(*this); |
235 | 107 | result.div_inplace(other); |
236 | 107 | return result; |
237 | 107 | } |
238 | | |
239 | 454 | void Tensor::add_inplace(const Tensor& other) { |
240 | 454 | if (!backend_) { |
241 | 0 | throw std::runtime_error("Tensor backend not initialized"); |
242 | 0 | } |
243 | 454 | check_same_shape(other); |
244 | 454 | backend_->add_inplace(*other.backend_); |
245 | 454 | } |
246 | | |
247 | 196 | void Tensor::mul_inplace(const Tensor& other) { |
248 | 196 | if (!backend_) { |
249 | 0 | throw std::runtime_error("Tensor backend not initialized"); |
250 | 0 | } |
251 | 196 | check_same_shape(other); |
252 | 196 | backend_->mul_inplace(*other.backend_); |
253 | 196 | } |
254 | | |
255 | 142 | void Tensor::sub_inplace(const Tensor& other) { |
256 | 142 | if (!backend_) { |
257 | 0 | throw std::runtime_error("Tensor backend not initialized"); |
258 | 0 | } |
259 | 142 | check_same_shape(other); |
260 | 142 | backend_->sub_inplace(*other.backend_); |
261 | 142 | } |
262 | | |
263 | 154 | void Tensor::div_inplace(const Tensor& other) { |
264 | 154 | if (!backend_) { |
265 | 0 | throw std::runtime_error("Tensor backend not initialized"); |
266 | 0 | } |
267 | 154 | check_same_shape(other); |
268 | 154 | backend_->div_inplace(*other.backend_); |
269 | 154 | } |
270 | | |
271 | 946 | void Tensor::check_same_shape(const Tensor& other) const { |
272 | | // Differing sizes are reported by the backend ("size mismatch"). |
273 | 946 | if (size() == other.size() && shape_ != other.shape_) { |
274 | 1 | throw std::runtime_error("Tensor shape mismatch"); |
275 | 1 | } |
276 | 946 | } |
277 | | |
278 | 1.83k | void Tensor::init_backend(std::size_t size) { |
279 | 1.83k | backend_ = create_best_backend(size, quantization_mode_); |
280 | 1.83k | } |
281 | | |
282 | | } // namespace labneura |