/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/generic/generic_ops_fp32.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/backends/cpu/generic.h" |
2 | | #include "labneura/runtime.h" |
3 | | #include "labneura/types.hpp" |
4 | | #include <algorithm> |
5 | | #include <cstdint> |
6 | | #include <cstring> |
7 | | #include <limits> |
8 | | #include <type_traits> |
9 | | |
10 | | namespace labneura { |
11 | | |
12 | | namespace { |
13 | | |
14 | | constexpr std::size_t kParallelAddThreshold = 1000000; |
15 | | constexpr std::size_t kParallelAddGrainSize = 16384; |
16 | | |
17 | | void add_fp32_range(float* destination, const float* source, |
18 | 11 | std::size_t begin, std::size_t end) { |
19 | 10.0k | for (std::size_t i = begin; i < end; ++i) { |
20 | 10.0k | destination[i] += source[i]; |
21 | 10.0k | } |
22 | 11 | } |
23 | | |
24 | | std::size_t checked_stride_advance(std::size_t dimension, |
25 | 64 | std::size_t stride) { |
26 | 64 | if (dimension <= 1 || stride == 0) { |
27 | 24 | return 0; |
28 | 24 | } |
29 | 40 | if (dimension - 1 > |
30 | 40 | std::numeric_limits<std::size_t>::max() / stride) { |
31 | 0 | throw std::overflow_error("Broadcast input offset overflows size_t"); |
32 | 0 | } |
33 | 40 | return (dimension - 1) * stride; |
34 | 40 | } |
35 | | |
36 | 64 | std::size_t checked_offset_add(std::size_t offset, std::size_t advance) { |
37 | 64 | if (offset > std::numeric_limits<std::size_t>::max() - advance) { |
38 | 0 | throw std::overflow_error("Broadcast input offset overflows size_t"); |
39 | 0 | } |
40 | 64 | return offset + advance; |
41 | 64 | } |
42 | | |
43 | | std::size_t maximum_broadcast_offset( |
44 | | const std::vector<std::size_t>& shape, |
45 | 32 | const std::vector<std::size_t>& strides) { |
46 | 32 | if (shape.size() != strides.size()) { |
47 | 0 | throw std::invalid_argument("Broadcast output shape/stride rank mismatch"); |
48 | 0 | } |
49 | 32 | std::size_t maximum = 0; |
50 | 96 | for (std::size_t axis = 0; axis < shape.size(); ++axis) { |
51 | 64 | maximum = checked_offset_add( |
52 | 64 | maximum, checked_stride_advance(shape[axis], strides[axis])); |
53 | 64 | } |
54 | 32 | return maximum; |
55 | 32 | } |
56 | | |
57 | | void validate_broadcast_range(const TensorStorage& storage, |
58 | | std::size_t storage_offset, |
59 | 48 | std::size_t maximum_relative_offset) { |
60 | 48 | if (storage_offset >= storage.logical_size || |
61 | 48 | maximum_relative_offset >= storage.logical_size - storage_offset) { |
62 | 0 | throw std::out_of_range( |
63 | 0 | "Broadcast input accesses beyond storage logical elements"); |
64 | 0 | } |
65 | 48 | } |
66 | | |
67 | | template <typename Element> |
68 | | Element apply_broadcast_operation(OperationType op_type, Element lhs, |
69 | 0 | Element rhs) { |
70 | | if constexpr (std::is_same_v<Element, int8_t> || |
71 | 0 | std::is_same_v<Element, int16_t>) { |
72 | 0 | using Wide = std::conditional_t<std::is_same_v<Element, int8_t>, |
73 | 0 | int16_t, int32_t>; |
74 | 0 | Wide result = 0; |
75 | 0 | switch (op_type) { |
76 | 0 | case OperationType::ADD: |
77 | 0 | result = static_cast<Wide>(lhs) + static_cast<Wide>(rhs); |
78 | 0 | break; |
79 | 0 | case OperationType::SUB: |
80 | 0 | result = static_cast<Wide>(lhs) - static_cast<Wide>(rhs); |
81 | 0 | break; |
82 | 0 | case OperationType::MUL: |
83 | 0 | result = static_cast<Wide>(lhs) * static_cast<Wide>(rhs); |
84 | 0 | break; |
85 | 0 | case OperationType::DIV: |
86 | 0 | if (rhs == 0) { |
87 | 0 | return 0; |
88 | 0 | } |
89 | 0 | result = static_cast<Wide>(lhs) / static_cast<Wide>(rhs); |
90 | 0 | break; |
91 | 0 | default: |
92 | 0 | throw std::invalid_argument( |
93 | 0 | "Unsupported broadcast arithmetic operation"); |
94 | 0 | } |
95 | 0 | const Wide minimum = static_cast<Wide>( |
96 | 0 | std::numeric_limits<Element>::min()); |
97 | 0 | const Wide maximum = static_cast<Wide>( |
98 | 0 | std::numeric_limits<Element>::max()); |
99 | 0 | return static_cast<Element>(std::max(minimum, std::min(maximum, result))); |
100 | 0 | } else { |
101 | 0 | switch (op_type) { |
102 | 0 | case OperationType::ADD: return lhs + rhs; |
103 | 0 | case OperationType::SUB: return lhs - rhs; |
104 | 0 | case OperationType::MUL: return lhs * rhs; |
105 | 0 | case OperationType::DIV: return lhs / rhs; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | throw std::invalid_argument("Unsupported broadcast arithmetic operation"); |
109 | 0 | } Unexecuted instantiation: generic_ops_fp32.cpp:_ZN8labneura12_GLOBAL__N_125apply_broadcast_operationIfEET_NS_13OperationTypeES2_S2_ Unexecuted instantiation: generic_ops_fp32.cpp:_ZN8labneura12_GLOBAL__N_125apply_broadcast_operationIaEET_NS_13OperationTypeES2_S2_ Unexecuted instantiation: generic_ops_fp32.cpp:_ZN8labneura12_GLOBAL__N_125apply_broadcast_operationIsEET_NS_13OperationTypeES2_S2_ |
110 | | |
111 | | template <typename Element> |
112 | | void execute_broadcast(const Element* lhs_data, std::size_t lhs_storage_offset, |
113 | | const Element* rhs_data, std::size_t rhs_storage_offset, |
114 | | Element* output_data, |
115 | | std::size_t output_storage_offset, |
116 | | std::size_t output_numel, |
117 | | const detail::BroadcastPlan& plan, |
118 | 0 | OperationType op_type) { |
119 | 0 | for (std::size_t linear_index = 0; linear_index < output_numel; |
120 | 0 | ++linear_index) { |
121 | 0 | std::size_t remainder = linear_index; |
122 | 0 | std::size_t lhs_offset = lhs_storage_offset; |
123 | 0 | std::size_t rhs_offset = rhs_storage_offset; |
124 | 0 | for (std::size_t axis = plan.shape.size(); axis > 0; --axis) { |
125 | 0 | const std::size_t dimension = plan.shape[axis - 1]; |
126 | 0 | const std::size_t coordinate = remainder % dimension; |
127 | 0 | remainder /= dimension; |
128 | 0 | lhs_offset += coordinate * plan.lhs_strides[axis - 1]; |
129 | 0 | rhs_offset += coordinate * plan.rhs_strides[axis - 1]; |
130 | 0 | } |
131 | 0 | output_data[output_storage_offset + linear_index] = |
132 | 0 | apply_broadcast_operation(op_type, lhs_data[lhs_offset], |
133 | 0 | rhs_data[rhs_offset]); |
134 | 0 | } |
135 | 0 | } Unexecuted instantiation: generic_ops_fp32.cpp:_ZN8labneura12_GLOBAL__N_117execute_broadcastIfEEvPKT_mS4_mPS2_mmRKNS_6detail13BroadcastPlanENS_13OperationTypeE Unexecuted instantiation: generic_ops_fp32.cpp:_ZN8labneura12_GLOBAL__N_117execute_broadcastIaEEvPKT_mS4_mPS2_mmRKNS_6detail13BroadcastPlanENS_13OperationTypeE Unexecuted instantiation: generic_ops_fp32.cpp:_ZN8labneura12_GLOBAL__N_117execute_broadcastIsEEvPKT_mS4_mPS2_mmRKNS_6detail13BroadcastPlanENS_13OperationTypeE |
136 | | |
137 | 216 | inline float bit_pattern_to_fp16_float(uint16_t bits) { |
138 | 216 | return dequantize_fp16(bits); |
139 | 216 | } |
140 | | |
141 | 108 | inline uint16_t fp16_float_to_bit_pattern(float value) { |
142 | 108 | return quantize_fp16(value); |
143 | 108 | } |
144 | | |
145 | | inline void apply_fp16_broadcast_element(OperationType op_type, |
146 | | const int16_t* lhs_data, |
147 | | std::size_t lhs_offset, |
148 | | const int16_t* rhs_data, |
149 | | std::size_t rhs_offset, |
150 | | int16_t* output_data, |
151 | 108 | std::size_t output_offset) { |
152 | 108 | uint16_t lhs_bits = static_cast<uint16_t>(lhs_data[lhs_offset]); |
153 | 108 | uint16_t rhs_bits = static_cast<uint16_t>(rhs_data[rhs_offset]); |
154 | 108 | const float lhs_value = bit_pattern_to_fp16_float(lhs_bits); |
155 | 108 | const float rhs_value = bit_pattern_to_fp16_float(rhs_bits); |
156 | 108 | float result = 0.0f; |
157 | 108 | switch (op_type) { |
158 | 27 | case OperationType::ADD: result = lhs_value + rhs_value; break; |
159 | 27 | case OperationType::SUB: result = lhs_value - rhs_value; break; |
160 | 27 | case OperationType::MUL: result = lhs_value * rhs_value; break; |
161 | 27 | case OperationType::DIV: result = lhs_value / rhs_value; break; |
162 | 0 | default: throw std::invalid_argument("Unsupported FP16 broadcast arithmetic operation"); |
163 | 108 | } |
164 | 108 | uint16_t out_bits = fp16_float_to_bit_pattern(result); |
165 | 108 | std::memcpy(&output_data[output_offset], &out_bits, sizeof(out_bits)); |
166 | 108 | } |
167 | | |
168 | | void execute_fp16_broadcast(const int16_t* lhs_data, std::size_t lhs_storage_offset, |
169 | | const int16_t* rhs_data, std::size_t rhs_storage_offset, |
170 | | int16_t* output_data, |
171 | | std::size_t output_storage_offset, |
172 | | std::size_t output_numel, |
173 | | const detail::BroadcastPlan& plan, |
174 | 16 | OperationType op_type) { |
175 | 124 | for (std::size_t linear_index = 0; linear_index < output_numel; ++linear_index) { |
176 | 108 | std::size_t remainder = linear_index; |
177 | 108 | std::size_t lhs_offset = lhs_storage_offset; |
178 | 108 | std::size_t rhs_offset = rhs_storage_offset; |
179 | 360 | for (std::size_t axis = plan.shape.size(); axis > 0; --axis) { |
180 | 252 | const std::size_t dimension = plan.shape[axis - 1]; |
181 | 252 | const std::size_t coordinate = remainder % dimension; |
182 | 252 | remainder /= dimension; |
183 | 252 | lhs_offset += coordinate * plan.lhs_strides[axis - 1]; |
184 | 252 | rhs_offset += coordinate * plan.rhs_strides[axis - 1]; |
185 | 252 | } |
186 | 108 | apply_fp16_broadcast_element(op_type, lhs_data, lhs_offset, rhs_data, |
187 | 108 | rhs_offset, output_data, |
188 | 108 | output_storage_offset + linear_index); |
189 | 108 | } |
190 | 16 | } |
191 | | |
192 | | } // namespace |
193 | | |
194 | 17 | void GenericBackend::operation_fp32(const TensorBackend& other, OperationType op_type) { |
195 | 17 | const float* other_data = other.data_fp32(); |
196 | 17 | float* this_data = data_fp32(); |
197 | | |
198 | 17 | if (op_type == OperationType::ADD) { |
199 | 11 | if (size_ >= kParallelAddThreshold && |
200 | 11 | execution_thread_count() > 1) { |
201 | 0 | parallel_for(0, size_, kParallelAddGrainSize, |
202 | 0 | [this_data, other_data](std::size_t begin, |
203 | 0 | std::size_t end) { |
204 | 0 | add_fp32_range(this_data, other_data, begin, end); |
205 | 0 | }); |
206 | 11 | } else { |
207 | 11 | add_fp32_range(this_data, other_data, 0, size_); |
208 | 11 | } |
209 | 11 | return; |
210 | 11 | } |
211 | 6 | if (op_type == OperationType::MUL) { |
212 | 11 | for (std::size_t i = 0; i < size_; ++i) this_data[i] *= other_data[i]; |
213 | 3 | return; |
214 | 3 | } |
215 | 3 | if (op_type == OperationType::DIV) { |
216 | 0 | for (std::size_t i = 0; i < size_; ++i) this_data[i] /= other_data[i]; |
217 | 0 | return; |
218 | 0 | } |
219 | 11 | for (std::size_t i = 0; i < size_; ++i) this_data[i] -= other_data[i]; |
220 | 3 | } |
221 | | |
222 | 11 | void GenericBackend::operation_fp16(const TensorBackend& other, OperationType op_type) { |
223 | 11 | const int16_t* other_data = other.data_fp16(); |
224 | 11 | int16_t* this_data = data_fp16(); |
225 | | |
226 | 64 | for (std::size_t i = 0; i < size_; ++i) { |
227 | 53 | const uint16_t lhs_bits = static_cast<uint16_t>(this_data[i]); |
228 | 53 | const uint16_t rhs_bits = static_cast<uint16_t>(other_data[i]); |
229 | 53 | const float lhs = dequantize_fp16(lhs_bits); |
230 | 53 | const float rhs = dequantize_fp16(rhs_bits); |
231 | 53 | float result = 0.0f; |
232 | 53 | switch (op_type) { |
233 | 17 | case OperationType::ADD: result = lhs + rhs; break; |
234 | 12 | case OperationType::SUB: result = lhs - rhs; break; |
235 | 12 | case OperationType::MUL: result = lhs * rhs; break; |
236 | 12 | case OperationType::DIV: result = lhs / rhs; break; |
237 | 0 | default: throw std::invalid_argument("Unsupported FP16 operation"); |
238 | 53 | } |
239 | 53 | const uint16_t out_bits = quantize_fp16(result); |
240 | 53 | std::memcpy(&this_data[i], &out_bits, sizeof(out_bits)); |
241 | 53 | } |
242 | 11 | } |
243 | | |
244 | | bool GenericBackend::operation_into(const TensorBackend& other, TensorBackend& out, |
245 | 0 | OperationType op_type) const { |
246 | 0 | if (quantization_mode_ != QuantizationMode::FP32 || |
247 | 0 | op_type != OperationType::ADD) { |
248 | 0 | return false; |
249 | 0 | } |
250 | 0 | if (out.size() != size_ || out.quantization_mode() != quantization_mode_) { |
251 | 0 | throw std::runtime_error("Output tensor does not match operand size or mode"); |
252 | 0 | } |
253 | | |
254 | 0 | float* destination = out.data_fp32(); |
255 | 0 | const float* left = data_fp32(); |
256 | 0 | const float* right = other.data_fp32(); |
257 | 0 | const auto add_range = [destination, left, right](std::size_t begin, |
258 | 0 | std::size_t end) { |
259 | 0 | for (std::size_t i = begin; i < end; ++i) { |
260 | 0 | destination[i] = left[i] + right[i]; |
261 | 0 | } |
262 | 0 | }; |
263 | 0 | if (size_ >= kParallelAddThreshold && execution_thread_count() > 1) { |
264 | 0 | parallel_for(0, size_, kParallelAddGrainSize, add_range); |
265 | 0 | } else { |
266 | 0 | add_range(0, size_); |
267 | 0 | } |
268 | 0 | return true; |
269 | 0 | } |
270 | | |
271 | | void GenericBackend::operation_broadcast( |
272 | | const GenericBackend& other, GenericBackend& output, |
273 | | const detail::BroadcastPlan& plan, OperationType op_type, |
274 | 24 | std::size_t lhs_numel, std::size_t rhs_numel) const { |
275 | 24 | if (quantization_mode_ != other.quantization_mode_ || |
276 | 24 | quantization_mode_ != output.quantization_mode_ || |
277 | 24 | (quantization_mode_ != QuantizationMode::FP32 && |
278 | 24 | quantization_mode_ != QuantizationMode::FP16 && |
279 | 24 | quantization_mode_ != QuantizationMode::INT8 && |
280 | 24 | quantization_mode_ != QuantizationMode::INT16)) { |
281 | 0 | throw std::runtime_error( |
282 | 0 | "Generic broadcasting supports matching FP32, FP16, INT8, or INT16 tensors"); |
283 | 0 | } |
284 | 24 | if (size_ != lhs_numel || other.size_ != rhs_numel) { |
285 | 0 | throw std::runtime_error( |
286 | 0 | "Broadcast operand logical element count mismatch"); |
287 | 0 | } |
288 | 24 | if (plan.shape.size() != plan.lhs_strides.size() || |
289 | 24 | plan.shape.size() != plan.rhs_strides.size()) { |
290 | 0 | throw std::invalid_argument( |
291 | 0 | "Broadcast plan shape and stride ranks must match"); |
292 | 0 | } |
293 | | |
294 | 24 | const std::size_t output_numel = |
295 | 24 | detail::checked_broadcast_numel(plan.shape); |
296 | 24 | if (output.size_ != output_numel) { |
297 | 0 | throw std::runtime_error( |
298 | 0 | "Broadcast output logical element count mismatch"); |
299 | 0 | } |
300 | 24 | if (output_numel == 0) { |
301 | 8 | return; |
302 | 8 | } |
303 | | |
304 | 16 | const std::size_t lhs_max_offset = |
305 | 16 | maximum_broadcast_offset(plan.shape, plan.lhs_strides); |
306 | 16 | const std::size_t rhs_max_offset = |
307 | 16 | maximum_broadcast_offset(plan.shape, plan.rhs_strides); |
308 | 16 | validate_broadcast_range(*storage_, storage_offset_, lhs_max_offset); |
309 | 16 | validate_broadcast_range(*other.storage_, other.storage_offset_, |
310 | 16 | rhs_max_offset); |
311 | 16 | validate_broadcast_range(*output.storage_, output.storage_offset_, |
312 | 16 | output_numel - 1); |
313 | | |
314 | 16 | if (quantization_mode_ == QuantizationMode::FP32) { |
315 | 0 | execute_broadcast( |
316 | 0 | static_cast<const float*>(storage_->data()), storage_offset_, |
317 | 0 | static_cast<const float*>(other.storage_->data()), |
318 | 0 | other.storage_offset_, |
319 | 0 | static_cast<float*>(output.storage_->data()), output.storage_offset_, |
320 | 0 | output_numel, plan, op_type); |
321 | 16 | } else if (quantization_mode_ == QuantizationMode::FP16) { |
322 | 16 | execute_fp16_broadcast( |
323 | 16 | static_cast<const int16_t*>(storage_->data()), storage_offset_, |
324 | 16 | static_cast<const int16_t*>(other.storage_->data()), |
325 | 16 | other.storage_offset_, |
326 | 16 | static_cast<int16_t*>(output.storage_->data()), output.storage_offset_, |
327 | 16 | output_numel, plan, op_type); |
328 | 16 | } else if (quantization_mode_ == QuantizationMode::INT8) { |
329 | 0 | execute_broadcast( |
330 | 0 | static_cast<const int8_t*>(storage_->data()), storage_offset_, |
331 | 0 | static_cast<const int8_t*>(other.storage_->data()), |
332 | 0 | other.storage_offset_, |
333 | 0 | static_cast<int8_t*>(output.storage_->data()), output.storage_offset_, |
334 | 0 | output_numel, plan, op_type); |
335 | 0 | } else { |
336 | 0 | execute_broadcast( |
337 | 0 | static_cast<const int16_t*>(storage_->data()), storage_offset_, |
338 | 0 | static_cast<const int16_t*>(other.storage_->data()), |
339 | 0 | other.storage_offset_, |
340 | 0 | static_cast<int16_t*>(output.storage_->data()), |
341 | 0 | output.storage_offset_, output_numel, plan, op_type); |
342 | 0 | } |
343 | 16 | } |
344 | | |
345 | | } // namespace labneura |