cubecl_runtime/runtime.rs
1use alloc::vec::Vec;
2use cubecl_common::device::{Device, DeviceId};
3use cubecl_ir::TargetProperties;
4use cubecl_zspace::{Shape, Strides};
5
6use crate::{client::Client, server::ServerStorage};
7
8pub use crate::description::{DeviceDescription, DeviceKind};
9pub use cubecl_monitoring::{
10 DevicePower, DeviceTemperature, DeviceUtilization, UtilizationUnavailable,
11};
12
13/// Runtime for the `CubeCL`.
14pub trait Runtime: Sized + Send + Sync + 'static + core::fmt::Debug + Clone {
15 /// The compute server used to run kernels and perform autotuning.
16 type Server: ServerStorage;
17 /// The device used to retrieve the compute client.
18 type Device: Device;
19
20 /// Retrieve the compute client from the runtime device, initializing the
21 /// server on first use.
22 fn client(device: &Self::Device) -> Client {
23 Client::load::<Self::Server>(device.to_id())
24 }
25
26 /// Whether a tensor with `shape` and `strides` can be read as is. If the result is false, the
27 /// tensor should be made contiguous before reading.
28 fn can_read_tensor(shape: &Shape, strides: &Strides) -> bool;
29
30 /// Returns the properties of the target hardware architecture.
31 fn target_properties() -> TargetProperties;
32
33 /// Returns all devices available under the provided type id.
34 fn enumerate_devices(type_id: u16) -> Vec<DeviceId>;
35 /// Returns all devices that can be handled by the runtime.
36 fn enumerate_all_devices() -> Vec<DeviceId> {
37 Self::enumerate_devices(0)
38 }
39
40 /// The devices of `device_id`'s own kind: its peers, and itself where the
41 /// machine has it.
42 ///
43 /// [`enumerate_devices`](Self::enumerate_devices) of its type, for a
44 /// runtime whose ids carry nothing more. One whose ids do answers from the
45 /// list that extra part names — a wgpu device pinned to a graphics API,
46 /// from that API's, the same adapter on another being another device.
47 fn enumerate_devices_like(device_id: DeviceId) -> Vec<DeviceId> {
48 Self::enumerate_devices(device_id.type_id)
49 }
50
51 /// Whether this machine has the device `device_id` names — and where it
52 /// does not, how many of that kind it has instead.
53 ///
54 /// What naming a device is checked against. The runtime's "you choose"
55 /// device names no hardware of its own, so it is there as soon as the
56 /// runtime found anything of its kind.
57 fn find_device(device_id: DeviceId) -> Result<(), usize> {
58 let of_kind = Self::enumerate_devices_like(device_id);
59
60 let found = of_kind.contains(&device_id)
61 || (device_id == Self::Device::default().to_id() && !of_kind.is_empty());
62
63 match found {
64 true => Ok(()),
65 false => Err(of_kind.len()),
66 }
67 }
68
69 /// Whether this machine has hardware worth choosing this runtime for.
70 ///
71 /// What picking a default device walks, so the question is not "could this
72 /// runtime run at all" but "would a caller who did not choose want it".
73 /// Enumerating a device is the answer for most runtimes; one that also
74 /// exposes a software fallback answers no on a machine that has only that,
75 /// leaving a native CPU runtime to claim it.
76 fn is_available() -> bool {
77 !Self::enumerate_all_devices().is_empty()
78 }
79
80 /// Description of the device (name, kind, memory, card, architecture, driver) read from the
81 /// driver without starting a server.
82 ///
83 /// Returns `None` when the machine has no such device, the device is the runtime's "you
84 /// choose" device (which names no hardware until a server picks some), or the runtime
85 /// describes nothing without a server.
86 fn description(_device: &Self::Device) -> Option<DeviceDescription> {
87 None
88 }
89
90 /// How busy `device` is, by the counter its card's driver or its platform keeps. Asking
91 /// creates the device's client, and must never wait on the work the device is running.
92 ///
93 /// A runtime that reads no counter for its devices says so.
94 fn utilization(_device: &Self::Device) -> Result<DeviceUtilization, UtilizationUnavailable> {
95 Err(UtilizationUnavailable::RuntimeReadsNoCounter)
96 }
97
98 /// The current temperature of `device`. Creates a client if needed.
99 ///
100 /// Returns an [`UtilizationUnavailable::RuntimeReadsNoCounter`]
101 /// error if the runtime doesn't read any counter.
102 fn temperature(_device: &Self::Device) -> Result<DeviceTemperature, UtilizationUnavailable> {
103 Err(UtilizationUnavailable::RuntimeReadsNoCounter)
104 }
105
106 /// The power usage of `device`, in Watt. Creates a client if needed.
107 ///
108 /// Returns an [`UtilizationUnavailable::RuntimeReadsNoCounter`]
109 /// error if the runtime doesn't read any counter.
110 fn power(_device: &Self::Device) -> Result<DevicePower, UtilizationUnavailable> {
111 Err(UtilizationUnavailable::RuntimeReadsNoCounter)
112 }
113}