transcribe_cpp_types/backend.rs
1//! Backend and device plain data: [`DeviceType`] and [`BackendMask`]. The
2//! `transcribe_cpp::Device` handle and backend loading stay in transcribe-cpp.
3
4use crate::types::Backend;
5
6/// ggml's vendor-agnostic class for a compute device, orthogonal to
7/// `transcribe_cpp::Device::kind` (which carries the vendor). Backends report
8/// this classification themselves, so use it as a runtime hint rather than a
9/// portable hardware-memory taxonomy.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub enum DeviceType {
13 /// CPU using system memory.
14 Cpu,
15 /// Backend-reported GPU.
16 Gpu,
17 /// Backend-reported integrated GPU.
18 Igpu,
19 /// Host-memory accelerator (BLAS/AMX/...).
20 Accel,
21 /// A device-type value this binding does not recognize — reported by a
22 /// runtime newer than the header this binding was generated against.
23 /// Distinguish such devices by `transcribe_cpp::Device::device_id` / `transcribe_cpp::Device::name`,
24 /// not by this axis.
25 Unknown,
26}
27
28/// Backend kinds allowed to register in this process. A backend outside the
29/// mask never runs any code, so a broken GPU driver can be kept out of a
30/// worker entirely. CPU is always allowed; `TRANSCRIBE_BACKENDS` can only
31/// narrow the mask.
32///
33/// The constants are the C header's `TRANSCRIBE_BACKEND_MASK_*` values;
34/// transcribe-cpp tests them against it.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37pub struct BackendMask(u32);
38
39impl BackendMask {
40 /// CPU plus host-memory accelerators (BLAS). Always implied.
41 pub const CPU: BackendMask = BackendMask(1);
42 /// Apple Metal.
43 pub const METAL: BackendMask = BackendMask(1 << 1);
44 /// Vulkan.
45 pub const VULKAN: BackendMask = BackendMask(1 << 2);
46 /// NVIDIA CUDA.
47 pub const CUDA: BackendMask = BackendMask(1 << 3);
48 /// AMD ROCm / HIP.
49 pub const ROCM: BackendMask = BackendMask(1 << 4);
50 /// Every backend without a dedicated bit (SYCL, OpenCL, RPC, ...).
51 pub const OTHER: BackendMask = BackendMask(1 << 31);
52 /// Everything. The default.
53 pub const ALL: BackendMask = BackendMask(u32::MAX);
54
55 /// The smallest mask that can serve a model-load `backend` request
56 /// ([`Backend::Auto`] needs everything).
57 pub const fn for_backend(backend: Backend) -> BackendMask {
58 match backend {
59 Backend::Auto => BackendMask::ALL,
60 Backend::Cpu | Backend::CpuAccel => BackendMask::CPU,
61 Backend::Metal => BackendMask::METAL.union(BackendMask::CPU),
62 Backend::Vulkan => BackendMask::VULKAN.union(BackendMask::CPU),
63 Backend::Cuda => BackendMask::CUDA.union(BackendMask::CPU),
64 Backend::Rocm => BackendMask::ROCM.union(BackendMask::CPU),
65 }
66 }
67
68 /// The raw `TRANSCRIBE_BACKEND_MASK_*` bits.
69 pub const fn bits(self) -> u32 {
70 self.0
71 }
72
73 /// A mask from raw `TRANSCRIBE_BACKEND_MASK_*` bits.
74 pub const fn from_bits(bits: u32) -> BackendMask {
75 BackendMask(bits)
76 }
77
78 /// Both masks' backends.
79 pub const fn union(self, other: BackendMask) -> BackendMask {
80 BackendMask(self.0 | other.0)
81 }
82
83 /// Whether every backend in `other` is in `self`.
84 pub const fn contains(self, other: BackendMask) -> bool {
85 self.0 & other.0 == other.0
86 }
87}
88
89impl Default for BackendMask {
90 fn default() -> Self {
91 BackendMask::ALL
92 }
93}
94
95impl std::ops::BitOr for BackendMask {
96 type Output = BackendMask;
97 fn bitor(self, rhs: BackendMask) -> BackendMask {
98 self.union(rhs)
99 }
100}
101
102impl std::ops::BitOrAssign for BackendMask {
103 fn bitor_assign(&mut self, rhs: BackendMask) {
104 *self = self.union(rhs);
105 }
106}