Coverage Report

Created: 2026-11-07 16:24

/home/runner/work/LabNeura/LabNeura/include/labneura/backends/common/base.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
#include <memory>
3
#include <stdexcept>
4
#include <utility>
5
#include "labneura/tensor.h"
6
#include "labneura/backends/common/tensor_storage.h"
7
#include "labneura/visibility.h"
8
9
namespace labneura {
10
11
class LABNEURA_API TensorBackend {
12
public:
13
    explicit TensorBackend(std::shared_ptr<TensorStorage> storage)
14
4.28k
        : storage_(std::move(storage)) {
15
4.28k
        if (!storage_) {
16
0
            throw std::invalid_argument("TensorBackend requires storage");
17
0
        }
18
4.28k
    }
19
    TensorBackend(std::shared_ptr<TensorStorage> storage,
20
                  std::size_t logical_size, std::size_t storage_offset)
21
113
        : storage_(std::move(storage)), storage_offset_(storage_offset) {
22
113
        if (!storage_) {
23
0
            throw std::invalid_argument("TensorBackend requires storage");
24
0
        }
25
113
        if (storage_offset_ > storage_->logical_size ||
26
113
            logical_size > storage_->logical_size - storage_offset_) {
27
0
            throw std::invalid_argument(
28
0
                "Backend alias exceeds the storage logical element range");
29
0
        }
30
113
    }
31
4.39k
    virtual ~TensorBackend() = default;
32
33
    // ---- Metadata ----
34
    virtual std::size_t size() const = 0;
35
    virtual QuantizationMode quantization_mode() const = 0;
36
0
    std::size_t allocation_capacity() const noexcept {
37
0
        return storage_->allocation_capacity;
38
0
    }
39
0
    std::size_t storage_alignment() const noexcept {
40
0
        return storage_->alignment;
41
0
    }
42
43
    // ---- Raw data access ----
44
    virtual float* data_fp32() = 0;
45
    virtual const float* data_fp32() const = 0;
46
    virtual int8_t* data_int8() = 0;
47
    virtual const int8_t* data_int8() const = 0;
48
    virtual std::uint8_t* data_bool();
49
    virtual const std::uint8_t* data_bool() const;
50
    // Optional FP16/INT16 accessors (default throw)
51
    virtual int16_t* data_fp16();
52
    virtual const int16_t* data_fp16() const;
53
    virtual int16_t* data_int16();
54
    virtual const int16_t* data_int16() const;
55
56
    // ---- Cloning ----
57
    virtual std::unique_ptr<TensorBackend> clone() const = 0;
58
59
    // ---- Public operations (NON-virtual interface) ----
60
    void add_inplace(const TensorBackend& other);
61
    void sub_inplace(const TensorBackend& other);
62
    void mul_inplace(const TensorBackend& other);
63
    void div_inplace(const TensorBackend& other);
64
    void operation(const TensorBackend& other, OperationType op);
65
66
    // Allocating operation: out = this (op) other, without modifying either input.
67
    // Returns nullptr when this backend has no direct-output kernel for the current
68
    // dtype; callers then fall back to clone() + in-place. Validation errors are
69
    // reported with the same exceptions as operation().
70
    std::unique_ptr<TensorBackend> operation_new(const TensorBackend& other, OperationType op) const;
71
72
protected:
73
    std::shared_ptr<TensorStorage> storage_;
74
    std::size_t storage_offset_{0};
75
76
    template <typename Element>
77
113
    Element* data_at_storage_offset() noexcept {
78
113
        return static_cast<Element*>(storage_->data()) + storage_offset_;
79
113
    }
_ZN8labneura13TensorBackend22data_at_storage_offsetIfEEPT_v
Line
Count
Source
77
71
    Element* data_at_storage_offset() noexcept {
78
71
        return static_cast<Element*>(storage_->data()) + storage_offset_;
79
71
    }
_ZN8labneura13TensorBackend22data_at_storage_offsetIsEEPT_v
Line
Count
Source
77
28
    Element* data_at_storage_offset() noexcept {
78
28
        return static_cast<Element*>(storage_->data()) + storage_offset_;
79
28
    }
_ZN8labneura13TensorBackend22data_at_storage_offsetIaEEPT_v
Line
Count
Source
77
14
    Element* data_at_storage_offset() noexcept {
78
14
        return static_cast<Element*>(storage_->data()) + storage_offset_;
79
14
    }
Unexecuted instantiation: _ZN8labneura13TensorBackend22data_at_storage_offsetIhEEPT_v
80
    std::uint8_t* canonicalize_bool_data(std::uint8_t* values) const;
81
    const std::uint8_t* validate_bool_data(
82
        const std::uint8_t* values) const;
83
84
    // Creates a distinct backend instance with this backend's type and shared
85
    // allocation, bounded by its logical size and element-based offset.
86
    virtual std::unique_ptr<TensorBackend> create_storage_alias(
87
        std::size_t logical_size, std::size_t storage_offset) const;
88
89
    // ---- Optional direct-output hooks (defaults: unsupported) ----
90
    // Returns an uninitialised output backend of the same type, size and mode, or
91
    // nullptr if operation_into() is not supported for this backend/mode.
92
    virtual std::unique_ptr<TensorBackend> create_output() const;
93
    // Computes out = this (op) other for equal-size, equal-mode operands. Exact
94
    // aliasing (out's buffer == an input's buffer) is supported by elementwise
95
    // kernels; partial overlap is not allowed.
96
    // Returns false if unsupported.
97
    virtual bool operation_into(const TensorBackend& other, TensorBackend& out, OperationType op) const;
98
99
    // ---- Backend-specific kernels (virtual hooks) ----
100
    virtual void operation_fp32(const TensorBackend& other, OperationType op) = 0;
101
    virtual void operation_int8(const TensorBackend& other, OperationType op) = 0;
102
    // Optional FP16/INT16 kernels (default throw)
103
    virtual void operation_fp16(const TensorBackend& other, OperationType op);
104
    virtual void operation_int16(const TensorBackend& other, OperationType op);
105
106
    friend class Tensor;
107
};
108
109
} // namespace labneura