/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 <memory> |
3 | | #include <algorithm> |
4 | | #include <stdexcept> |
5 | | #include "labneura/utils/alignment.hpp" |
6 | | |
7 | | namespace labneura { |
8 | | |
9 | | GenericBackend::GenericBackend(std::size_t size, QuantizationMode mode) |
10 | 1.13k | : TensorBackend(TensorStorage::allocate( |
11 | 1.13k | size, size, mode, util::DEFAULT_ALIGN)), |
12 | 1.13k | size_(size), |
13 | 1.13k | quantization_mode_(mode) { |
14 | 1.13k | if (mode == QuantizationMode::FP32) { |
15 | 231 | data_fp32_ = static_cast<float*>(storage_->data()); |
16 | 900 | } else if (mode == QuantizationMode::FP16) { |
17 | 251 | data_fp16_ = static_cast<int16_t*>(storage_->data()); |
18 | 649 | } else if (mode == QuantizationMode::INT8) { |
19 | 169 | data_int8_ = static_cast<int8_t*>(storage_->data()); |
20 | 480 | } else if (mode == QuantizationMode::INT16) { |
21 | 143 | data_int16_ = static_cast<int16_t*>(storage_->data()); |
22 | 337 | } else if (mode == QuantizationMode::BOOL) { |
23 | 337 | data_bool_ = static_cast<std::uint8_t*>(storage_->data()); |
24 | 337 | std::fill(data_bool_, data_bool_ + size_, std::uint8_t{0}); |
25 | 337 | } |
26 | 1.13k | } |
27 | | |
28 | | GenericBackend::GenericBackend(std::shared_ptr<TensorStorage> storage, |
29 | | std::size_t logical_size, |
30 | | std::size_t storage_offset) |
31 | 31 | : TensorBackend(std::move(storage), logical_size, storage_offset), |
32 | 31 | size_(logical_size), |
33 | 31 | quantization_mode_(storage_->dtype) { |
34 | 31 | if (quantization_mode_ == QuantizationMode::FP32) { |
35 | 27 | data_fp32_ = data_at_storage_offset<float>(); |
36 | 27 | } else if (quantization_mode_ == QuantizationMode::FP16) { |
37 | 4 | data_fp16_ = data_at_storage_offset<int16_t>(); |
38 | 4 | } else if (quantization_mode_ == QuantizationMode::INT8) { |
39 | 0 | data_int8_ = data_at_storage_offset<int8_t>(); |
40 | 0 | } else if (quantization_mode_ == QuantizationMode::INT16) { |
41 | 0 | data_int16_ = data_at_storage_offset<int16_t>(); |
42 | 0 | } else if (quantization_mode_ == QuantizationMode::BOOL) { |
43 | 0 | data_bool_ = data_at_storage_offset<std::uint8_t>(); |
44 | 0 | } |
45 | 31 | } |
46 | | |
47 | 19.5k | std::size_t GenericBackend::size() const { |
48 | 19.5k | return size_; |
49 | 19.5k | } |
50 | | |
51 | 261 | QuantizationMode GenericBackend::quantization_mode() const { |
52 | 261 | return quantization_mode_; |
53 | 261 | } |
54 | | |
55 | 396 | float* GenericBackend::data_fp32() { |
56 | 396 | if (quantization_mode_ != QuantizationMode::FP32) { |
57 | 0 | throw std::runtime_error("Tensor is not in FP32 mode"); |
58 | 0 | } |
59 | 396 | return data_fp32_; |
60 | 396 | } |
61 | | |
62 | 197 | const float* GenericBackend::data_fp32() const { |
63 | 197 | if (quantization_mode_ != QuantizationMode::FP32) { |
64 | 0 | throw std::runtime_error("Tensor is not in FP32 mode"); |
65 | 0 | } |
66 | 197 | return data_fp32_; |
67 | 197 | } |
68 | | |
69 | 254 | int8_t* GenericBackend::data_int8() { |
70 | 254 | if (quantization_mode_ != QuantizationMode::INT8) { |
71 | 0 | throw std::runtime_error("Tensor is not in INT8 mode"); |
72 | 0 | } |
73 | 254 | return data_int8_; |
74 | 254 | } |
75 | | |
76 | 157 | const int8_t* GenericBackend::data_int8() const { |
77 | 157 | if (quantization_mode_ != QuantizationMode::INT8) { |
78 | 0 | throw std::runtime_error("Tensor is not in INT8 mode"); |
79 | 0 | } |
80 | 157 | return data_int8_; |
81 | 157 | } |
82 | | |
83 | 344 | int16_t* GenericBackend::data_fp16() { |
84 | 344 | if (quantization_mode_ != QuantizationMode::FP16) { |
85 | 0 | throw std::runtime_error("Tensor is not in FP16 mode"); |
86 | 0 | } |
87 | 344 | return data_fp16_; |
88 | 344 | } |
89 | | |
90 | 179 | const int16_t* GenericBackend::data_fp16() const { |
91 | 179 | if (quantization_mode_ != QuantizationMode::FP16) { |
92 | 0 | throw std::runtime_error("Tensor is not in FP16 mode"); |
93 | 0 | } |
94 | 179 | return data_fp16_; |
95 | 179 | } |
96 | | |
97 | 170 | int16_t* GenericBackend::data_int16() { |
98 | 170 | if (quantization_mode_ != QuantizationMode::INT16) { |
99 | 0 | throw std::runtime_error("Tensor is not in INT16 mode"); |
100 | 0 | } |
101 | 170 | return data_int16_; |
102 | 170 | } |
103 | | |
104 | 145 | const int16_t* GenericBackend::data_int16() const { |
105 | 145 | if (quantization_mode_ != QuantizationMode::INT16) { |
106 | 0 | throw std::runtime_error("Tensor is not in INT16 mode"); |
107 | 0 | } |
108 | 145 | return data_int16_; |
109 | 145 | } |
110 | | |
111 | 868 | std::uint8_t* GenericBackend::data_bool() { |
112 | 868 | if (quantization_mode_ != QuantizationMode::BOOL) { |
113 | 0 | throw std::runtime_error("Tensor is not in BOOL mode"); |
114 | 0 | } |
115 | 868 | return canonicalize_bool_data(data_bool_); |
116 | 868 | } |
117 | | |
118 | 0 | const std::uint8_t* GenericBackend::data_bool() const { |
119 | 0 | if (quantization_mode_ != QuantizationMode::BOOL) { |
120 | 0 | throw std::runtime_error("Tensor is not in BOOL mode"); |
121 | 0 | } |
122 | 0 | return validate_bool_data(data_bool_); |
123 | 0 | } |
124 | | |
125 | | std::unique_ptr<TensorBackend> GenericBackend::create_storage_alias( |
126 | 31 | std::size_t logical_size, std::size_t storage_offset) const { |
127 | 31 | return std::unique_ptr<TensorBackend>( |
128 | 31 | new GenericBackend(storage_, logical_size, storage_offset)); |
129 | 31 | } |
130 | | |
131 | 24 | std::unique_ptr<TensorBackend> GenericBackend::clone() const { |
132 | 24 | auto backend = std::make_unique<GenericBackend>(size_, quantization_mode_); |
133 | 24 | if (quantization_mode_ == QuantizationMode::FP32) { |
134 | 9 | std::copy(data_fp32_, data_fp32_ + size_, |
135 | 9 | backend->data_fp32()); |
136 | 15 | } else if (quantization_mode_ == QuantizationMode::FP16) { |
137 | 6 | std::copy(data_fp16_, data_fp16_ + size_, |
138 | 6 | backend->data_fp16()); |
139 | 9 | } else if (quantization_mode_ == QuantizationMode::INT8) { |
140 | 6 | std::copy(data_int8_, data_int8_ + size_, |
141 | 6 | backend->data_int8()); |
142 | 6 | } else if (quantization_mode_ == QuantizationMode::INT16) { |
143 | 1 | std::copy(data_int16_, data_int16_ + size_, |
144 | 1 | backend->data_int16()); |
145 | 2 | } else if (quantization_mode_ == QuantizationMode::BOOL) { |
146 | 2 | std::copy(data_bool_, data_bool_ + size_, backend->data_bool()); |
147 | 2 | } |
148 | 24 | return backend; |
149 | 24 | } |
150 | | |
151 | | |
152 | | } // namespace labneura |