Coverage Report

Created: 2026-11-02 16:55

/home/runner/work/LabNeura/LabNeura/src/labneura/backends/common/backend_factory.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "labneura/backends/common/backend_factory.h"
2
#include "labneura/backends/common/cpu_features.h"
3
#include "labneura/backends/cpu/generic.h"
4
#include <iostream>
5
#include <cstdlib>
6
#if defined(LABNEURA_ENABLE_AVX2)
7
#include "labneura/backends/cpu/avx2.h"
8
#endif
9
10
#if defined(LABNEURA_ENABLE_NEON)
11
#include "labneura/backends/cpu/neon.h"
12
#endif
13
14
namespace labneura {
15
16
// Return the preferred backend label based on runtime CPU features
17
58
std::string detect_backend() {
18
58
#if (defined(__x86_64__) || defined(_M_X64)) && defined(LABNEURA_ENABLE_AVX2)
19
58
    if (cpu_supports_avx2()) {
20
58
        return "AVX2";
21
58
    }
22
0
    return "GENERIC";
23
#elif (defined(__aarch64__) || defined(__ARM_NEON)) && defined(LABNEURA_ENABLE_NEON)
24
    if (cpu_supports_neon()) {
25
        return "NEON";
26
    }
27
    return "GENERIC";
28
#else
29
    return "GENERIC";
30
#endif
31
58
}
32
33
std::unique_ptr<TensorBackend>
34
1.83k
create_best_backend(std::size_t size, QuantizationMode mode) {
35
36
    // Optional override for testing via environment variable
37
    // LABNEURA_BACKEND can be set to "GENERIC", "NEON", or "AVX2"
38
1.83k
    if (const char* override = std::getenv("LABNEURA_BACKEND")) {
39
60
        std::string value = override;
40
60
        if (value == "GENERIC") {
41
52
            return std::make_unique<GenericBackend>(size, mode);
42
52
        }
43
#if defined(LABNEURA_ENABLE_NEON)
44
        if (value == "NEON") {
45
            return std::make_unique<NEONBackend>(size, mode);
46
        }
47
#endif
48
8
#if defined(LABNEURA_ENABLE_AVX2)
49
8
        if (value == "AVX2") {
50
0
            return std::make_unique<AVX2Backend>(size, mode);
51
0
        }
52
8
#endif
53
        // Fall through to auto-detection if value is unrecognized
54
8
    }
55
56
57
1.78k
#if defined(__x86_64__) || defined(_M_X64)
58
1.78k
    if (cpu_supports_avx2()) {
59
1.78k
#if defined(LABNEURA_ENABLE_AVX2)
60
1.78k
        return std::make_unique<AVX2Backend>(size, mode);
61
1.78k
#endif
62
1.78k
    }
63
0
#endif
64
65
#if defined(__aarch64__) || defined(__ARM_NEON)
66
    if (cpu_supports_neon()) {
67
#if defined(LABNEURA_ENABLE_NEON)
68
        return std::make_unique<NEONBackend>(size, mode);
69
#endif
70
    }
71
#endif
72
73
0
    return std::make_unique<GenericBackend>(size, mode);
74
1.78k
}
75
76
} // namespace labneura