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_uuid` /
24 /// `transcribe_cpp::Device::device_id` / `transcribe_cpp::Device::name`,
25 /// not by this axis.
26 Unknown,
27}
28
29/// Backend kinds allowed to register in this process. A backend outside the
30/// mask never runs any code, so a broken GPU driver can be kept out of a
31/// worker entirely. CPU is always allowed; `TRANSCRIBE_BACKENDS` can only
32/// narrow the mask.
33///
34/// The constants are the C header's `TRANSCRIBE_BACKEND_MASK_*` values;
35/// transcribe-cpp tests them against it.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38pub struct BackendMask(u32);
39
40impl BackendMask {
41 /// CPU plus host-memory accelerators (BLAS). Always implied.
42 pub const CPU: BackendMask = BackendMask(1);
43 /// Apple Metal.
44 pub const METAL: BackendMask = BackendMask(1 << 1);
45 /// Vulkan.
46 pub const VULKAN: BackendMask = BackendMask(1 << 2);
47 /// NVIDIA CUDA.
48 pub const CUDA: BackendMask = BackendMask(1 << 3);
49 /// AMD ROCm / HIP.
50 pub const ROCM: BackendMask = BackendMask(1 << 4);
51 /// Every backend without a dedicated bit (SYCL, OpenCL, RPC, ...).
52 pub const OTHER: BackendMask = BackendMask(1 << 31);
53 /// Everything. The default.
54 pub const ALL: BackendMask = BackendMask(u32::MAX);
55
56 /// The smallest mask that can serve a model-load `backend` request
57 /// ([`Backend::Auto`] needs everything).
58 pub const fn for_backend(backend: Backend) -> BackendMask {
59 match backend {
60 Backend::Auto => BackendMask::ALL,
61 Backend::Cpu | Backend::CpuAccel => BackendMask::CPU,
62 Backend::Metal => BackendMask::METAL.union(BackendMask::CPU),
63 Backend::Vulkan => BackendMask::VULKAN.union(BackendMask::CPU),
64 Backend::Cuda => BackendMask::CUDA.union(BackendMask::CPU),
65 Backend::Rocm => BackendMask::ROCM.union(BackendMask::CPU),
66 }
67 }
68
69 /// The raw `TRANSCRIBE_BACKEND_MASK_*` bits.
70 pub const fn bits(self) -> u32 {
71 self.0
72 }
73
74 /// A mask from raw `TRANSCRIBE_BACKEND_MASK_*` bits.
75 pub const fn from_bits(bits: u32) -> BackendMask {
76 BackendMask(bits)
77 }
78
79 /// Both masks' backends.
80 pub const fn union(self, other: BackendMask) -> BackendMask {
81 BackendMask(self.0 | other.0)
82 }
83
84 /// Whether every backend in `other` is in `self`.
85 pub const fn contains(self, other: BackendMask) -> bool {
86 self.0 & other.0 == other.0
87 }
88}
89
90impl Default for BackendMask {
91 fn default() -> Self {
92 BackendMask::ALL
93 }
94}
95
96impl std::ops::BitOr for BackendMask {
97 type Output = BackendMask;
98 fn bitor(self, rhs: BackendMask) -> BackendMask {
99 self.union(rhs)
100 }
101}
102
103impl std::ops::BitOrAssign for BackendMask {
104 fn bitor_assign(&mut self, rhs: BackendMask) {
105 *self = self.union(rhs);
106 }
107}