/home/runner/work/LabNeura/LabNeura/src/labneura/backends/common/tensor_storage.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/backends/common/tensor_storage.h" |
2 | | #include "labneura/backends/common/dtype_validation.h" |
3 | | |
4 | | #include "labneura/utils/alignment.hpp" |
5 | | |
6 | | #include <limits> |
7 | | #include <new> |
8 | | #include <stdexcept> |
9 | | |
10 | | namespace labneura { |
11 | | namespace { |
12 | | |
13 | 8.56k | std::size_t dtype_size(QuantizationMode dtype) { |
14 | 8.56k | switch (dtype) { |
15 | 3.06k | case QuantizationMode::FP32: |
16 | 3.06k | return sizeof(float); |
17 | 1.72k | case QuantizationMode::FP16: |
18 | 2.73k | case QuantizationMode::INT16: |
19 | 2.73k | return sizeof(int16_t); |
20 | 1.63k | case QuantizationMode::INT8: |
21 | 1.63k | return sizeof(int8_t); |
22 | 1.13k | case QuantizationMode::BOOL: |
23 | 1.13k | return sizeof(std::uint8_t); |
24 | 0 | case QuantizationMode::FP64: |
25 | 0 | case QuantizationMode::INT32: |
26 | 0 | throw std::invalid_argument("Unsupported tensor dtype"); |
27 | 8.56k | } |
28 | 0 | throw std::invalid_argument("Unsupported tensor dtype"); |
29 | 8.56k | } |
30 | | |
31 | | void* allocate_data(std::size_t count, QuantizationMode dtype, |
32 | 4.28k | std::size_t alignment) { |
33 | 4.28k | switch (dtype) { |
34 | 1.53k | case QuantizationMode::FP32: |
35 | 1.53k | return util::allocate_aligned<float>(count, alignment); |
36 | 862 | case QuantizationMode::FP16: |
37 | 1.36k | case QuantizationMode::INT16: |
38 | 1.36k | return util::allocate_aligned<int16_t>(count, alignment); |
39 | 818 | case QuantizationMode::INT8: |
40 | 818 | return util::allocate_aligned<int8_t>(count, alignment); |
41 | 565 | case QuantizationMode::BOOL: |
42 | 565 | return util::allocate_aligned<std::uint8_t>(count, alignment); |
43 | 0 | case QuantizationMode::FP64: |
44 | 0 | case QuantizationMode::INT32: |
45 | 0 | throw std::invalid_argument("Unsupported tensor dtype"); |
46 | 4.28k | } |
47 | 0 | throw std::invalid_argument("Unsupported tensor dtype"); |
48 | 4.28k | } |
49 | | |
50 | | } // namespace |
51 | | |
52 | | TensorStorage::TensorStorage(void* data, std::size_t logical_size_value, |
53 | | std::size_t allocation_capacity_value, |
54 | | QuantizationMode dtype_value, |
55 | | std::size_t alignment_value, Deleter deleter) |
56 | 4.28k | : logical_size(logical_size_value), |
57 | 4.28k | allocation_capacity(allocation_capacity_value), |
58 | 4.28k | dtype(dtype_value), |
59 | 4.28k | alignment(alignment_value), |
60 | 4.28k | data_(data), |
61 | 4.28k | deleter_(deleter) { |
62 | 4.28k | validate_tensor_dtype_supported(dtype); |
63 | 4.28k | if (data_ == nullptr || deleter_ == nullptr) { |
64 | 0 | throw std::invalid_argument("TensorStorage requires owned data and a deleter"); |
65 | 0 | } |
66 | 4.28k | if (allocation_capacity < logical_size) { |
67 | 0 | throw std::invalid_argument( |
68 | 0 | "TensorStorage capacity cannot be smaller than its logical size"); |
69 | 0 | } |
70 | 4.28k | if (allocation_capacity > |
71 | 4.28k | std::numeric_limits<std::size_t>::max() / dtype_size(dtype)) { |
72 | 0 | throw std::bad_array_new_length(); |
73 | 0 | } |
74 | 4.28k | if (alignment < sizeof(void*) || |
75 | 4.28k | (alignment & (alignment - 1)) != 0 || |
76 | 4.28k | !util::is_aligned(data_, alignment)) { |
77 | 0 | throw std::invalid_argument("TensorStorage data does not meet its alignment"); |
78 | 0 | } |
79 | 4.28k | } |
80 | | |
81 | 4.28k | TensorStorage::~TensorStorage() { |
82 | 4.28k | deleter_(data_); |
83 | 4.28k | } |
84 | | |
85 | | std::shared_ptr<TensorStorage> TensorStorage::allocate( |
86 | | std::size_t logical_size, std::size_t allocation_capacity, |
87 | 4.28k | QuantizationMode dtype, std::size_t alignment) { |
88 | 4.28k | validate_tensor_dtype_supported(dtype); |
89 | 4.28k | if (allocation_capacity < logical_size) { |
90 | 0 | throw std::invalid_argument( |
91 | 0 | "TensorStorage capacity cannot be smaller than its logical size"); |
92 | 0 | } |
93 | 4.28k | if (allocation_capacity > |
94 | 4.28k | std::numeric_limits<std::size_t>::max() / dtype_size(dtype)) { |
95 | 0 | throw std::bad_array_new_length(); |
96 | 0 | } |
97 | | |
98 | 4.28k | void* data = allocate_data(allocation_capacity, dtype, alignment); |
99 | 4.28k | return adopt(data, logical_size, allocation_capacity, dtype, alignment, |
100 | 4.28k | util::aligned_free); |
101 | 4.28k | } |
102 | | |
103 | | std::shared_ptr<TensorStorage> TensorStorage::adopt( |
104 | | void* data, std::size_t logical_size, std::size_t allocation_capacity, |
105 | 4.28k | QuantizationMode dtype, std::size_t alignment, Deleter deleter) { |
106 | 4.28k | TensorStorage* storage = nullptr; |
107 | 4.28k | try { |
108 | 4.28k | storage = new TensorStorage( |
109 | 4.28k | data, logical_size, allocation_capacity, dtype, alignment, deleter); |
110 | 4.28k | } catch (...) { |
111 | 0 | if (data != nullptr && deleter != nullptr) { |
112 | 0 | deleter(data); |
113 | 0 | } |
114 | 0 | throw; |
115 | 0 | } |
116 | 4.28k | return std::shared_ptr<TensorStorage>(storage); |
117 | 4.28k | } |
118 | | |
119 | | } // namespace labneura |