/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 "backend_selection.h" |
3 | | #include "labneura/backends/common/cpu_features.h" |
4 | | #include "labneura/backends/common/dtype_validation.h" |
5 | | #include "labneura/backends/cpu/generic.h" |
6 | | #include <cstdlib> |
7 | | #if defined(LABNEURA_ENABLE_AVX2) |
8 | | #include "labneura/backends/cpu/avx2.h" |
9 | | #endif |
10 | | |
11 | | #if defined(LABNEURA_ENABLE_NEON) |
12 | | #include "labneura/backends/cpu/neon.h" |
13 | | #endif |
14 | | |
15 | | namespace labneura { |
16 | | |
17 | | namespace { |
18 | | |
19 | 4.08k | detail::BackendKind selected_backend() { |
20 | 4.08k | const CPUFeatures features = detect_cpu_features(); |
21 | 4.08k | const bool neon_supported = cpu_supports_neon(); |
22 | 4.08k | #if defined(LABNEURA_ENABLE_AVX2) |
23 | 4.08k | constexpr bool avx2_compiled = true; |
24 | | #else |
25 | | constexpr bool avx2_compiled = false; |
26 | | #endif |
27 | | #if defined(LABNEURA_ENABLE_NEON) |
28 | | constexpr bool neon_compiled = true; |
29 | | #else |
30 | 4.08k | constexpr bool neon_compiled = false; |
31 | 4.08k | #endif |
32 | 4.08k | return detail::select_backend(std::getenv("LABNEURA_BACKEND"), |
33 | 4.08k | avx2_compiled, features.avx2, |
34 | 4.08k | neon_compiled, neon_supported); |
35 | 4.08k | } |
36 | | |
37 | | } // namespace |
38 | | |
39 | 763 | std::string detect_backend() { |
40 | 763 | return detail::backend_name(selected_backend()); |
41 | 763 | } |
42 | | |
43 | | std::unique_ptr<TensorBackend> |
44 | 3.31k | create_best_backend(std::size_t size, QuantizationMode mode) { |
45 | 3.31k | validate_tensor_dtype_supported(mode); |
46 | 3.31k | switch (selected_backend()) { |
47 | 766 | case detail::BackendKind::GENERIC: |
48 | 766 | return std::make_unique<GenericBackend>(size, mode); |
49 | 0 | case detail::BackendKind::NEON: |
50 | | #if defined(LABNEURA_ENABLE_NEON) |
51 | | return std::make_unique<NEONBackend>(size, mode); |
52 | | #else |
53 | 0 | break; |
54 | 0 | #endif |
55 | 2.55k | case detail::BackendKind::AVX2: |
56 | 2.55k | #if defined(LABNEURA_ENABLE_AVX2) |
57 | 2.55k | return std::make_unique<AVX2Backend>(size, mode); |
58 | | #else |
59 | | break; |
60 | | #endif |
61 | 3.31k | } |
62 | 0 | return std::make_unique<GenericBackend>(size, mode); |
63 | 3.31k | } |
64 | | |
65 | | } // namespace labneura |