Coverage Report

Created: 2026-11-07 16:24

/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
    BOOL,      // 8-bit canonical boolean storage
45
};
46
47
enum OperationType {
48
    ADD,
49
    SUB,
50
    MUL,
51
    DIV
52
};
53
54
enum class ComparisonType {
55
    EQ,
56
    NE,
57
    LT,
58
    LE,
59
    GT,
60
    GE
61
};
62
63
// Forward declaration for backend implementation
64
class TensorBackend;
65
class TensorStorage;
66
67
// Main Tensor class: handles mixed int/float input with architecture-aware storage
68
class Tensor {
69
public:
70
    // Constructor with array/scalar and optional quantization
71
    // Supports int, float, or std::vector<int/float>
72
    Tensor();
73
    explicit Tensor(const std::vector<float>& data, QuantizationMode mode = QuantizationMode::FP32);
74
    explicit Tensor(const std::vector<int>& data, QuantizationMode mode = QuantizationMode::FP32);
75
    explicit Tensor(const std::vector<std::uint8_t>& data,
76
                    QuantizationMode mode = QuantizationMode::BOOL);
77
    // 2-D tensor from equal-length rows (stored flat, row-major)
78
    explicit Tensor(const std::vector<std::vector<float>>& rows, QuantizationMode mode = QuantizationMode::FP32);
79
    explicit Tensor(float scalar, QuantizationMode mode = QuantizationMode::FP32);
80
    explicit Tensor(int scalar, QuantizationMode mode = QuantizationMode::FP32);
81
82
    // Construct a contiguous tensor from flat row-major data and an explicit shape.
83
    // An empty shape represents a scalar and therefore requires exactly one value.
84
    static Tensor from_flat(const std::vector<float>& data,
85
                            const std::vector<std::size_t>& shape,
86
                            QuantizationMode mode = QuantizationMode::FP32);
87
    static Tensor from_flat(const std::vector<int>& data,
88
                            const std::vector<std::size_t>& shape,
89
                            QuantizationMode mode = QuantizationMode::FP32);
90
    static Tensor from_flat(const std::vector<std::uint8_t>& data,
91
                            const std::vector<std::size_t>& shape,
92
                            QuantizationMode mode = QuantizationMode::BOOL);
93
94
    // Return an independently owned contiguous copy with the requested shape.
95
    Tensor reshape(const std::vector<std::size_t>& shape) const;
96
    // Return a contiguous reshape that explicitly shares storage with this tensor.
97
    Tensor reshape_view(const std::vector<std::size_t>& shape) const;
98
    // Return a first-axis range view sharing storage with this tensor.
99
    Tensor slice_first_axis(std::size_t start, std::size_t stop) const;
100
    // Return a view selecting one element along the first axis.
101
    Tensor select_first_axis(std::size_t index) const;
102
    // Return an independently owned one-dimensional copy in row-major order.
103
    Tensor flatten() const;
104
105
    // Destructor
106
    ~Tensor();
107
108
    // Copy and move semantics
109
    Tensor(const Tensor& other);
110
    Tensor& operator=(const Tensor& other);
111
    Tensor(Tensor&& other) noexcept;
112
    Tensor& operator=(Tensor&& other) noexcept;
113
114
    // Shape and size queries
115
    std::size_t size() const;
116
1.74k
    std::size_t numel() const { return backend_ ? checked_numel(shape_) : 0; }
117
12.5k
    QuantizationMode quantization_mode() const { return quantization_mode_; }
118
658
    const std::vector<std::size_t>& shape() const { return shape_; }
119
    // Strides and storage offset are measured in elements.
120
40
    const std::vector<std::size_t>& strides() const { return strides_; }
121
643
    std::size_t storage_offset() const { return storage_offset_; }
122
    bool is_contiguous() const;
123
7
    std::size_t ndim() const { return shape_.size(); }
124
1
    std::size_t rows() const { return shape_.size() == 2 ? shape_[0] : 1; }
125
1
    std::size_t cols() const { return shape_.size() == 2 ? shape_[1] : size(); }
126
127
    // Get raw data pointer (for internal use and operations)
128
    float* data_fp32();
129
    const float* data_fp32() const;
130
    int8_t* data_int8();
131
    const int8_t* data_int8() const;
132
    int16_t* data_fp16();
133
    const int16_t* data_fp16() const;
134
    int16_t* data_int16();
135
    const int16_t* data_int16() const;
136
    // BOOL storage is canonicalized on mutable access; callers must write only
137
    // 0 or 1. Const access rejects storage made noncanonical through a pointer.
138
    std::uint8_t* data_bool();
139
    const std::uint8_t* data_bool() const;
140
141
    // Operations
142
    // Addition: result = this + other
143
    Tensor add(const Tensor& other) const;
144
    // Write this + other into an existing same-shape, same-backend FP32 tensor.
145
    // Exact aliasing of out with either input is supported and overwrites it.
146
    void add_into(const Tensor& other, Tensor& out) const;
147
    // Multiplication: result = this * other
148
    Tensor mul(const Tensor& other) const;
149
    // Subtraction: result = this - other
150
    Tensor sub(const Tensor& other) const;
151
    // Division: result = this / other
152
    Tensor div(const Tensor& other) const;
153
154
    // Elementwise comparisons return a newly allocated BOOL Tensor.
155
    Tensor eq(const Tensor& other) const;
156
    Tensor ne(const Tensor& other) const;
157
    Tensor lt(const Tensor& other) const;
158
    Tensor le(const Tensor& other) const;
159
    Tensor gt(const Tensor& other) const;
160
    Tensor ge(const Tensor& other) const;
161
    
162
    // In-place addition: this += other
163
    void add_inplace(const Tensor& other);
164
    // In-place multiplication: this *= other
165
    void mul_inplace(const Tensor& other);
166
    // In-place subtraction: this -= other
167
    void sub_inplace(const Tensor& other);
168
    // In-place division: this /= other
169
    void div_inplace(const Tensor& other);
170
171
private:
172
    QuantizationMode quantization_mode_;
173
    std::unique_ptr<TensorBackend> backend_;
174
    std::shared_ptr<TensorStorage> storage_;
175
    std::vector<std::size_t> shape_;
176
    std::vector<std::size_t> strides_;
177
    std::size_t storage_offset_{0};
178
179
    void check_same_shape(const Tensor& other) const;
180
181
    // Allocating binary operation. Computes directly into a new output when the
182
    // backend supports it, otherwise clones this tensor and runs the in-place kernel.
183
    Tensor(QuantizationMode mode, std::unique_ptr<TensorBackend> backend, std::vector<std::size_t> shape);
184
    Tensor binary_op(const Tensor& other, OperationType op) const;
185
    Tensor comparison_op(const Tensor& other, ComparisonType op) const;
186
    void binary_inplace(const Tensor& other, OperationType op);
187
188
    // Helper to initialize backend based on hardware
189
    void init_backend(std::size_t size);
190
    static std::size_t checked_numel(const std::vector<std::size_t>& shape);
191
    static std::vector<std::size_t> checked_contiguous_strides(
192
        const std::vector<std::size_t>& shape);
193
    static void validate_allocation_size(std::size_t size,
194
                                         QuantizationMode mode);
195
};
196
197
} // namespace labneura