/home/runner/work/LabNeura/LabNeura/src/labneura/backends/cpu/generic/generic_backend.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/backends/cpu/generic.h" |
2 | | #include <cstdlib> |
3 | | #include <memory> |
4 | | #include <algorithm> |
5 | | #include <stdexcept> |
6 | | #include "labneura/utils/alignment.hpp" |
7 | | |
8 | | namespace labneura { |
9 | | |
10 | | GenericBackend::GenericBackend(std::size_t size, QuantizationMode mode) |
11 | 65 | : size_(size), quantization_mode_(mode) { |
12 | | // 64-byte aligned allocation with fallback to malloc (generic fallback backend) |
13 | 65 | auto free_aligned_float = [](float* p){ if (p) std::free(p); }; |
14 | 65 | auto free_aligned_int8 = [](int8_t* p){ if (p) std::free(p); }; |
15 | 65 | auto allocate_aligned = [](std::size_t bytes){ |
16 | 59 | void* p = nullptr; |
17 | 59 | if (posix_memalign(&p, labneura::util::DEFAULT_ALIGN, bytes) == 0) return p; |
18 | 0 | return std::malloc(bytes); |
19 | 59 | }; |
20 | 65 | if (mode == QuantizationMode::FP32) { |
21 | 31 | float* p = static_cast<float*>(allocate_aligned(size * sizeof(float))); |
22 | 31 | data_fp32_ = std::unique_ptr<float, void(*)(float*)>(p, free_aligned_float); |
23 | 34 | } else if (mode == QuantizationMode::INT8) { |
24 | 28 | int8_t* p = static_cast<int8_t*>(allocate_aligned(size * sizeof(int8_t))); |
25 | 28 | data_int8_ = std::unique_ptr<int8_t, void(*)(int8_t*)>(p, free_aligned_int8); |
26 | 28 | } |
27 | 65 | } |
28 | | |
29 | 129 | std::size_t GenericBackend::size() const { |
30 | 129 | return size_; |
31 | 129 | } |
32 | | |
33 | 91 | QuantizationMode GenericBackend::quantization_mode() const { |
34 | 91 | return quantization_mode_; |
35 | 91 | } |
36 | | |
37 | 71 | float* GenericBackend::data_fp32() { |
38 | 71 | if (quantization_mode_ != QuantizationMode::FP32) { |
39 | 0 | throw std::runtime_error("Tensor is not in FP32 mode"); |
40 | 0 | } |
41 | 71 | return data_fp32_.get(); |
42 | 71 | } |
43 | | |
44 | 12 | const float* GenericBackend::data_fp32() const { |
45 | 12 | if (quantization_mode_ != QuantizationMode::FP32) { |
46 | 0 | throw std::runtime_error("Tensor is not in FP32 mode"); |
47 | 0 | } |
48 | 12 | return data_fp32_.get(); |
49 | 12 | } |
50 | | |
51 | 65 | int8_t* GenericBackend::data_int8() { |
52 | 65 | if (quantization_mode_ != QuantizationMode::INT8) { |
53 | 0 | throw std::runtime_error("Tensor is not in INT8 mode"); |
54 | 0 | } |
55 | 65 | return data_int8_.get(); |
56 | 65 | } |
57 | | |
58 | 11 | const int8_t* GenericBackend::data_int8() const { |
59 | 11 | if (quantization_mode_ != QuantizationMode::INT8) { |
60 | 0 | throw std::runtime_error("Tensor is not in INT8 mode"); |
61 | 0 | } |
62 | 11 | return data_int8_.get(); |
63 | 11 | } |
64 | | |
65 | 13 | std::unique_ptr<TensorBackend> GenericBackend::clone() const { |
66 | 13 | auto backend = std::make_unique<GenericBackend>(size_, quantization_mode_); |
67 | 13 | if (quantization_mode_ == QuantizationMode::FP32) { |
68 | 7 | std::copy(data_fp32_.get(), data_fp32_.get() + size_, |
69 | 7 | backend->data_fp32()); |
70 | 7 | } else if (quantization_mode_ == QuantizationMode::INT8) { |
71 | 6 | std::copy(data_int8_.get(), data_int8_.get() + size_, |
72 | 6 | backend->data_int8()); |
73 | 6 | } |
74 | 13 | return backend; |
75 | 13 | } |
76 | | |
77 | | |
78 | | } // namespace labneura |