/home/runner/work/LabNeura/LabNeura/include/labneura/tensor.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <vector> |
4 | | #include <cstdint> |
5 | | #include <cstddef> |
6 | | #include <memory> |
7 | | #include <variant> |
8 | | #include <stdexcept> |
9 | | |
10 | | #pragma once |
11 | | |
12 | | #if defined(__x86_64__) || defined(_M_X64) |
13 | | #define LABNEURA_X86 1 |
14 | | #else |
15 | | #define LABNEURA_X86 0 |
16 | | #endif |
17 | | |
18 | | #if LABNEURA_X86 && defined(__AVX2__) |
19 | | #define LABNEURA_HAVE_AVX2 1 |
20 | | #else |
21 | | #define LABNEURA_HAVE_AVX2 0 |
22 | | #endif |
23 | | #if LABNEURA_X86 && defined(__AVX512F__) |
24 | | #define LABNEURA_HAVE_AVX512 1 |
25 | | #else |
26 | | #define LABNEURA_HAVE_AVX512 0 |
27 | | #endif |
28 | | #if defined(__ARM_NEON) || defined(__ARM_NEON__) |
29 | | #define LABNEURA_HAVE_NEON 1 |
30 | | #else |
31 | | #define LABNEURA_HAVE_NEON 0 |
32 | | #endif |
33 | | |
34 | | namespace labneura { |
35 | | |
36 | | // Quantization modes |
37 | | enum class QuantizationMode { |
38 | | FP64, // 64-bit floating point |
39 | | FP32, // 32-bit floating point (default) |
40 | | FP16, // 16-bit floating point |
41 | | INT32, // 32-bit signed integer quantization |
42 | | INT16, // 16-bit signed integer quantization |
43 | | INT8, // 8-bit signed integer quantization |
44 | | }; |
45 | | |
46 | | enum OperationType { |
47 | | ADD, |
48 | | SUB, |
49 | | MUL, |
50 | | DIV |
51 | | }; |
52 | | |
53 | | // Forward declaration for backend implementation |
54 | | class TensorBackend; |
55 | | |
56 | | // Main Tensor class: handles mixed int/float input with architecture-aware storage |
57 | | class Tensor { |
58 | | public: |
59 | | // Constructor with array/scalar and optional quantization |
60 | | // Supports int, float, or std::vector<int/float> |
61 | | Tensor(); |
62 | | explicit Tensor(const std::vector<float>& data, QuantizationMode mode = QuantizationMode::FP32); |
63 | | explicit Tensor(const std::vector<int>& data, QuantizationMode mode = QuantizationMode::FP32); |
64 | | // 2-D tensor from equal-length rows (stored flat, row-major) |
65 | | explicit Tensor(const std::vector<std::vector<float>>& rows, QuantizationMode mode = QuantizationMode::FP32); |
66 | | explicit Tensor(float scalar, QuantizationMode mode = QuantizationMode::FP32); |
67 | | explicit Tensor(int scalar, QuantizationMode mode = QuantizationMode::FP32); |
68 | | |
69 | | // Destructor |
70 | | ~Tensor(); |
71 | | |
72 | | // Copy and move semantics |
73 | | Tensor(const Tensor& other); |
74 | | Tensor& operator=(const Tensor& other); |
75 | | Tensor(Tensor&& other) noexcept; |
76 | | Tensor& operator=(Tensor&& other) noexcept; |
77 | | |
78 | | // Shape and size queries |
79 | | std::size_t size() const; |
80 | 5 | std::size_t numel() const { return size(); } |
81 | 9.99k | QuantizationMode quantization_mode() const { return quantization_mode_; } |
82 | 3 | const std::vector<std::size_t>& shape() const { return shape_; } |
83 | 1 | std::size_t ndim() const { return shape_.size(); } |
84 | 1 | std::size_t rows() const { return shape_.size() == 2 ? shape_[0] : 1; } |
85 | 1 | std::size_t cols() const { return shape_.size() == 2 ? shape_[1] : size(); } |
86 | | |
87 | | // Get raw data pointer (for internal use and operations) |
88 | | float* data_fp32(); |
89 | | const float* data_fp32() const; |
90 | | int8_t* data_int8(); |
91 | | const int8_t* data_int8() const; |
92 | | int16_t* data_fp16(); |
93 | | const int16_t* data_fp16() const; |
94 | | int16_t* data_int16(); |
95 | | const int16_t* data_int16() const; |
96 | | |
97 | | // Operations |
98 | | // Addition: result = this + other |
99 | | Tensor add(const Tensor& other) const; |
100 | | // Multiplication: result = this * other |
101 | | Tensor mul(const Tensor& other) const; |
102 | | // Subtraction: result = this - other |
103 | | Tensor sub(const Tensor& other) const; |
104 | | // Division: result = this / other |
105 | | Tensor div(const Tensor& other) const; |
106 | | |
107 | | // In-place addition: this += other |
108 | | void add_inplace(const Tensor& other); |
109 | | // In-place multiplication: this *= other |
110 | | void mul_inplace(const Tensor& other); |
111 | | // In-place subtraction: this -= other |
112 | | void sub_inplace(const Tensor& other); |
113 | | // In-place division: this /= other |
114 | | void div_inplace(const Tensor& other); |
115 | | |
116 | | private: |
117 | | QuantizationMode quantization_mode_; |
118 | | std::unique_ptr<TensorBackend> backend_; |
119 | | std::vector<std::size_t> shape_; |
120 | | |
121 | | void check_same_shape(const Tensor& other) const; |
122 | | |
123 | | // Helper to initialize backend based on hardware |
124 | | void init_backend(std::size_t size); |
125 | | }; |
126 | | |
127 | | } // namespace labneura |