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 cubecl_monitoring::{DeviceUtilization, UtilizationUnavailable};
9
10/// Runtime for the `CubeCL`.
11pub trait Runtime: Sized + Send + Sync + 'static + core::fmt::Debug + Clone {
12 /// The compute server used to run kernels and perform autotuning.
13 type Server: ServerStorage;
14 /// The device used to retrieve the compute client.
15 type Device: Device;
16
17 /// Retrieve the compute client from the runtime device, initializing the
18 /// server on first use.
19 fn client(device: &Self::Device) -> Client {
20 Client::load::<Self::Server>(device.to_id())
21 }
22
23 /// Whether a tensor with `shape` and `strides` can be read as is. If the result is false, the
24 /// tensor should be made contiguous before reading.
25 fn can_read_tensor(shape: &Shape, strides: &Strides) -> bool;
26
27 /// Returns the properties of the target hardware architecture.
28 fn target_properties() -> TargetProperties;
29
30 /// Returns all devices available under the provided type id.
31 fn enumerate_devices(type_id: u16) -> Vec<DeviceId>;
32 /// Returns all devices that can be handled by the runtime.
33 fn enumerate_all_devices() -> Vec<DeviceId> {
34 Self::enumerate_devices(0)
35 }
36
37 /// The devices of `device_id`'s own kind: its peers, and itself where the
38 /// machine has it.
39 ///
40 /// [`enumerate_devices`](Self::enumerate_devices) of its type, for a
41 /// runtime whose ids carry nothing more. One whose ids do answers from the
42 /// list that extra part names — a wgpu device pinned to a graphics API,
43 /// from that API's, the same adapter on another being another device.
44 fn enumerate_devices_like(device_id: DeviceId) -> Vec<DeviceId> {
45 Self::enumerate_devices(device_id.type_id)
46 }
47
48 /// Whether this machine has the device `device_id` names — and where it
49 /// does not, how many of that kind it has instead.
50 ///
51 /// What naming a device is checked against. The runtime's "you choose"
52 /// device names no hardware of its own, so it is there as soon as the
53 /// runtime found anything of its kind.
54 fn find_device(device_id: DeviceId) -> Result<(), usize> {
55 let of_kind = Self::enumerate_devices_like(device_id);
56
57 let found = of_kind.contains(&device_id)
58 || (device_id == Self::Device::default().to_id() && !of_kind.is_empty());
59
60 match found {
61 true => Ok(()),
62 false => Err(of_kind.len()),
63 }
64 }
65
66 /// Whether this machine has hardware worth choosing this runtime for.
67 ///
68 /// What picking a default device walks, so the question is not "could this
69 /// runtime run at all" but "would a caller who did not choose want it".
70 /// Enumerating a device is the answer for most runtimes; one that also
71 /// exposes a software fallback answers no on a machine that has only that,
72 /// leaving a native CPU runtime to claim it.
73 fn is_available() -> bool {
74 !Self::enumerate_all_devices().is_empty()
75 }
76
77 /// How busy `device` is, by the counter its card's driver or its platform keeps. Asking
78 /// creates the device's client, and must never wait on the work the device is running.
79 ///
80 /// A runtime that reads no counter for its devices says so.
81 fn utilization(_device: &Self::Device) -> Result<DeviceUtilization, UtilizationUnavailable> {
82 Err(UtilizationUnavailable::RuntimeReadsNoCounter)
83 }
84}