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