/home/runner/work/LabNeura/LabNeura/src/labneura/backends/common/base.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/backends/common/base.h" |
2 | | #include <stdexcept> |
3 | | |
4 | | namespace labneura { |
5 | | |
6 | 0 | void TensorBackend::add_inplace(const TensorBackend& other) { |
7 | 0 | operation(other, OperationType::ADD); |
8 | 0 | } |
9 | | |
10 | 0 | void TensorBackend::sub_inplace(const TensorBackend& other) { |
11 | 0 | operation(other, OperationType::SUB); |
12 | 0 | } |
13 | | |
14 | 0 | void TensorBackend::mul_inplace(const TensorBackend& other) { |
15 | 0 | operation(other, OperationType::MUL); |
16 | 0 | } |
17 | | |
18 | 0 | void TensorBackend::div_inplace(const TensorBackend& other) { |
19 | 0 | operation(other, OperationType::DIV); |
20 | 0 | } |
21 | | |
22 | 1.03k | void TensorBackend::operation(const TensorBackend& other, OperationType op) { |
23 | 1.03k | if (size() != other.size()) { |
24 | 12 | throw std::runtime_error("Tensor size mismatch"); |
25 | 12 | } |
26 | | |
27 | 1.02k | if (quantization_mode() != other.quantization_mode()) { |
28 | 16 | throw std::runtime_error("Quantization mode mismatch"); |
29 | 16 | } |
30 | 1.00k | if (quantization_mode() == QuantizationMode::BOOL) { |
31 | 0 | throw std::runtime_error( |
32 | 0 | "Arithmetic operations are not supported for BOOL tensors"); |
33 | 0 | } |
34 | | |
35 | 1.00k | if (storage_.get() == other.storage_.get() && |
36 | 1.00k | storage_offset_ != other.storage_offset_) { |
37 | 1 | const std::size_t lhs_offset = storage_offset_; |
38 | 1 | const std::size_t rhs_offset = other.storage_offset_; |
39 | 1 | const bool overlaps = |
40 | 1 | lhs_offset < rhs_offset |
41 | 1 | ? rhs_offset - lhs_offset < size() |
42 | 1 | : lhs_offset - rhs_offset < size(); |
43 | 1 | if (overlaps) { |
44 | 1 | throw std::invalid_argument( |
45 | 1 | "In-place operation does not support partially overlapping tensor views"); |
46 | 1 | } |
47 | 1 | } |
48 | | |
49 | 1.00k | if (quantization_mode() == QuantizationMode::FP32) { |
50 | 465 | operation_fp32(other, op); |
51 | 542 | } else if (quantization_mode() == QuantizationMode::FP16) { |
52 | 231 | operation_fp16(other, op); |
53 | 311 | } else if (quantization_mode() == QuantizationMode::INT16) { |
54 | 96 | operation_int16(other, op); |
55 | 215 | } else { |
56 | 215 | operation_int8(other, op); |
57 | 215 | } |
58 | 1.00k | } |
59 | | |
60 | 345 | std::unique_ptr<TensorBackend> TensorBackend::operation_new(const TensorBackend& other, OperationType op) const { |
61 | 345 | if (size() != other.size()) { |
62 | 3 | throw std::runtime_error("Tensor size mismatch"); |
63 | 3 | } |
64 | | |
65 | 342 | if (quantization_mode() != other.quantization_mode()) { |
66 | 0 | throw std::runtime_error("Quantization mode mismatch"); |
67 | 0 | } |
68 | 342 | if (quantization_mode() == QuantizationMode::BOOL) { |
69 | 1 | throw std::runtime_error( |
70 | 1 | "Arithmetic operations are not supported for BOOL tensors"); |
71 | 1 | } |
72 | | |
73 | 341 | auto out = create_output(); |
74 | 341 | if (!out) { |
75 | 234 | return nullptr; |
76 | 234 | } |
77 | 107 | if (!operation_into(other, *out, op)) { |
78 | 0 | return nullptr; |
79 | 0 | } |
80 | 107 | return out; |
81 | 107 | } |
82 | | |
83 | 21 | std::unique_ptr<TensorBackend> TensorBackend::create_output() const { |
84 | 21 | return nullptr; |
85 | 21 | } |
86 | | |
87 | | std::unique_ptr<TensorBackend> TensorBackend::create_storage_alias( |
88 | 0 | std::size_t, std::size_t) const { |
89 | 0 | throw std::runtime_error( |
90 | 0 | "Storage sharing is not implemented for this backend"); |
91 | 0 | } |
92 | | |
93 | 0 | bool TensorBackend::operation_into(const TensorBackend&, TensorBackend&, OperationType) const { |
94 | 0 | return false; |
95 | 0 | } |
96 | | |
97 | | std::uint8_t* TensorBackend::canonicalize_bool_data( |
98 | 1.48k | std::uint8_t* values) const { |
99 | 19.4k | for (std::size_t i = 0; i < size(); ++i) { |
100 | 17.9k | values[i] = values[i] != 0 ? 1 : 0; |
101 | 17.9k | } |
102 | 1.48k | return values; |
103 | 1.48k | } |
104 | | |
105 | | const std::uint8_t* TensorBackend::validate_bool_data( |
106 | 0 | const std::uint8_t* values) const { |
107 | 0 | for (std::size_t i = 0; i < size(); ++i) { |
108 | 0 | if (values[i] > 1) { |
109 | 0 | throw std::runtime_error( |
110 | 0 | "BOOL storage contains a noncanonical value"); |
111 | 0 | } |
112 | 0 | } |
113 | 0 | return values; |
114 | 0 | } |
115 | | |
116 | 0 | int16_t* TensorBackend::data_fp16() { |
117 | 0 | throw std::runtime_error("FP16 data access not implemented for this backend"); |
118 | 0 | } |
119 | | |
120 | 0 | const int16_t* TensorBackend::data_fp16() const { |
121 | 0 | throw std::runtime_error("FP16 data access not implemented for this backend"); |
122 | 0 | } |
123 | | |
124 | 0 | int16_t* TensorBackend::data_int16() { |
125 | 0 | throw std::runtime_error("INT16 data access not implemented for this backend"); |
126 | 0 | } |
127 | | |
128 | 0 | const int16_t* TensorBackend::data_int16() const { |
129 | 0 | throw std::runtime_error("INT16 data access not implemented for this backend"); |
130 | 0 | } |
131 | | |
132 | 0 | std::uint8_t* TensorBackend::data_bool() { |
133 | 0 | throw std::runtime_error("BOOL data access not implemented for this backend"); |
134 | 0 | } |
135 | | |
136 | 0 | const std::uint8_t* TensorBackend::data_bool() const { |
137 | 0 | throw std::runtime_error("BOOL data access not implemented for this backend"); |
138 | 0 | } |
139 | | |
140 | 0 | void TensorBackend::operation_fp16(const TensorBackend&, OperationType) { |
141 | 0 | throw std::runtime_error("FP16 operations not implemented for this backend"); |
142 | 0 | } |
143 | | |
144 | 0 | void TensorBackend::operation_int16(const TensorBackend&, OperationType) { |
145 | 0 | throw std::runtime_error("INT16 operations not implemented for this backend"); |
146 | 0 | } |
147 | | |
148 | | } // namespace labneura |