1use std::sync::OnceLock;
4
5use tch::{Cuda, Device, Kind, Tensor};
6
7use crate::{Result, RustTorchError};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum DeviceSpec {
15 Auto,
17 Cpu,
19 Cuda(usize),
21 Mps,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct DeviceCapabilities {
32 pub cpu: bool,
34 pub cuda: bool,
36 pub cuda_device_count: usize,
38 pub cudnn: bool,
40 pub mps: bool,
42}
43
44pub fn available_devices() -> DeviceCapabilities {
46 let cuda = Cuda::is_available();
47 let cuda_device_count = if cuda {
48 Cuda::device_count().max(0) as usize
49 } else {
50 0
51 };
52 DeviceCapabilities {
53 cpu: true,
54 cuda,
55 cuda_device_count,
56 cudnn: cuda && Cuda::cudnn_is_available(),
57 mps: mps_is_available(),
58 }
59}
60
61pub fn resolve_device(spec: DeviceSpec) -> Result<Device> {
63 let capabilities = available_devices();
64 match spec {
65 DeviceSpec::Auto if capabilities.cuda => Ok(Device::Cuda(0)),
66 DeviceSpec::Auto if capabilities.mps => Ok(Device::Mps),
67 DeviceSpec::Auto | DeviceSpec::Cpu => Ok(Device::Cpu),
68 DeviceSpec::Cuda(index) if index < capabilities.cuda_device_count => {
69 Ok(Device::Cuda(index))
70 }
71 DeviceSpec::Cuda(index) => Err(RustTorchError::BackendUnavailable {
72 backend: "CUDA",
73 reason: format!(
74 "device {index} was requested, but the linked LibTorch build exposes {} usable CUDA device(s)",
75 capabilities.cuda_device_count
76 ),
77 }),
78 DeviceSpec::Mps if capabilities.mps => Ok(Device::Mps),
79 DeviceSpec::Mps => Err(RustTorchError::BackendUnavailable {
80 backend: "MPS",
81 reason:
82 "the linked LibTorch build or current machine does not provide a usable MPS device"
83 .to_owned(),
84 }),
85 }
86}
87
88fn mps_is_available() -> bool {
89 static AVAILABLE: OnceLock<bool> = OnceLock::new();
90 *AVAILABLE.get_or_init(|| {
91 Tensor::f_zeros([1], (Kind::Float, Device::Mps))
92 .and_then(|tensor| tensor.f_add_scalar(1.0))
93 .is_ok()
94 })
95}
96
97pub fn ensure_device(context: impl Into<String>, tensor: &Tensor, expected: Device) -> Result<()> {
104 if !tensor.defined() {
105 return Err(RustTorchError::InvalidDimensions {
106 context: context.into(),
107 expected: "a defined tensor".to_owned(),
108 actual: "undefined tensor".to_owned(),
109 });
110 }
111 let actual = tensor.device();
112 if actual == expected {
113 Ok(())
114 } else {
115 Err(RustTorchError::DeviceMismatch {
116 context: context.into(),
117 expected,
118 actual,
119 })
120 }
121}