Skip to main content

CubeDevice

Enum CubeDevice 

Source
#[non_exhaustive]
pub enum CubeDevice { Cuda(CudaDevice), Hip(AmdDevice), Metal(MetalDevice), Wgpu(WgpuDevice), Cpu(CpuDevice), }
Expand description

The device a cube tensor lives on.

One type across every runtime: which runtime a tensor runs on is what its device says, not what its type is. A device of any runtime.

A value names both the runtime and the device on it. A value can name a runtime the build does not link — the enum is the same in every build — and it is reaching for a client that then fails.

use cubecl::Device;

let client = Device::default().client();
println!("running on {}", client.name());

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Cuda(CudaDevice)

A device of the CUDA runtime.

§

Hip(AmdDevice)

A device of the HIP runtime.

§

Metal(MetalDevice)

A device of the native Metal runtime.

§

Wgpu(WgpuDevice)

A device of the wgpu runtime, on its default compiler.

§

Cpu(CpuDevice)

The device of the CPU runtime.

Implementations§

Source§

impl Device

Naming one device rather than taking Device::default.

Each asks the runtime whether the machine really has what was named, so a device that would only fail at the client call is an error here instead. They are in every build: a runtime this one left out is DeviceUnavailable::NotLinked, not a function that went missing.

use cubecl::Device;

// The second GPU if there is one, otherwise whatever this machine has.
let device = Device::cuda(1).or_else(|_| Device::cpu())?;
let client = device.client();
Source

pub fn cpu() -> Result<Device, DeviceUnavailable>

The CPU device.

Source

pub fn cuda(index: usize) -> Result<Device, DeviceUnavailable>

The CUDA device at index, counting the GPUs CUDA reports.

Source

pub fn rocm(index: usize) -> Result<Device, DeviceUnavailable>

The ROCm device at index, counting the GPUs HIP reports.

Source

pub fn metal(kind: MetalDevice) -> Result<Device, DeviceUnavailable>

The native Metal device kind names.

The Metal runtime proper. Reaching Metal through wgpu’s MSL backend is Device::wgpu instead, and hands back a wgpu device.

Source

pub fn wgpu(kind: WgpuDeviceKind) -> Result<Device, DeviceUnavailable>

The device kind names, on whichever graphics API this machine offers.

The portable one: which API it comes up on — and so which shader compiler it is fed — is the runtime’s to settle, best first, from the enabled features and what the machine has. Naming the API instead is vulkan and its siblings.

Source

pub fn vulkan(kind: WgpuDeviceKind) -> Result<Device, DeviceUnavailable>

The Vulkan device kind names — and so SPIR-V, where the build and the adapter allow it.

Naming a graphics API is a promise the machine has to keep: this fails where the device is not reachable on Vulkan, rather than quietly coming up on something else. The same device on two APIs is two devices, each with an id and a client of its own.

use cubecl::{Device, device::WgpuDeviceKind};

let client = Device::vulkan(WgpuDeviceKind::default())?.client();
Source

pub fn dx12(kind: WgpuDeviceKind) -> Result<Device, DeviceUnavailable>

The DirectX 12 device kind names. See vulkan for what naming an API promises.

Source

pub fn gl(kind: WgpuDeviceKind) -> Result<Device, DeviceUnavailable>

The OpenGL device kind names. See vulkan for what naming an API promises.

Source

pub fn webgpu(kind: WgpuDeviceKind) -> Result<Device, DeviceUnavailable>

The WebGPU device kind names — the browser’s own. See vulkan for what naming an API promises.

Source

pub fn metal_msl(kind: WgpuDeviceKind) -> Result<Device, DeviceUnavailable>

The device kind names on Metal’s own API, reached through wgpu — and so MSL, where the build allows it.

Not the native Metal runtime, which is Device::metal and a different device altogether.

Source

pub fn on(self, backend: WgpuBackend) -> Result<Device, DeviceUnavailable>

This device on backend, where the machine has it there.

For the caller holding a device already and wanting it on a particular API. Only the API named is asked: where the device came up by default says nothing about whether this one has it.

Source

pub fn client(&self) -> Client

The compute client of this device, initialized on first use.

§Panics

Where this build does not link the device’s runtime.

Source

pub fn can_read_tensor(&self, shape: &Shape, strides: &Strides) -> bool

Whether a tensor with shape and strides can be read as it lies, or has to be made contiguous first. A property of the runtime, which is why it is asked of the device rather than of the client.

§Panics

Where this build does not link the device’s runtime.

Source

pub fn enumerate(device_id: DeviceId) -> Vec<DeviceId>

The ids of every device sharing device_id’s runtime and device type — and for a wgpu device, its graphics API, the same adapter on another being another device.

Takes and returns ids in Device’s own encoding, so a caller holding one id can ask what else is on that device’s runtime without unpacking the runtime itself. Whatever device_id spends the high byte on comes back on every id, untouched.

Source

pub fn enumerate_all() -> Vec<Device>

Every device of every runtime this build links.

What a caller listing “the devices” wants, now that one type covers them all — a runtime with no hardware present contributes nothing.

Source

pub fn runtime(&self) -> RuntimeId

The runtime this device belongs to.

Source

pub fn utilization(&self) -> Result<DeviceUtilization, UtilizationUnavailable>

How busy this device is, by the counter its card’s driver or its platform keeps: NVML for an NVIDIA card, gpu_busy_percent for an AMD one and the idle residency of an Intel one on Linux, the GPU engine counters Task Manager reads on Windows, the accelerator’s statistics in the I/O Registry on macOS, and the processor times for the CPU.

The card decides which counter answers, not the runtime, so an NVIDIA card reads the same through CUDA as through wgpu. Asking creates the device’s client, and a card’s counter is opened the first time it is asked and kept for the life of the process.

The Intel, Windows and CPU counters measure the time between two readings. A reading covers the time since the card was last asked, by any caller, and the first is UtilizationUnavailable::NoPreviousReading.

use cubecl::Device;

match Device::default().utilization() {
    Ok(utilization) => println!("{}% busy", utilization.busy_percent),
    Err(reason) => println!("no reading: {reason}"),
}
Source

pub fn from_id(device_id: DeviceId) -> Device

The device a DeviceId in this type’s encoding names.

§Panics

Where device_id names no runtime at all. An id only travels between builds of the same application, so this is a build mismatch rather than input to validate.

Source

pub fn to_id(&self) -> DeviceId

This device’s id, with its runtime stamped in the tag bits so ids from two runtimes cannot collide.

Trait Implementations§

Source§

impl Clone for Device

Source§

fn clone(&self) -> Device

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Device

Available on any_runtime only.

The default device of the most capable runtime with hardware to run on.

Walks the runtimes this build links, most capable first, and takes the first that reports a device — so a binary that links both CUDA and wgpu lands on wgpu when it turns out there is no NVIDIA card, rather than naming a device it cannot reach.

The last runtime in that order is taken without asking. There is nothing left to fall back to, so its answer cannot change the outcome, and a build that links one runtime — the usual one — probes nothing at all. Where a probe does run, its result is kept for the life of the process: a device appearing or disappearing later goes unnoticed.

Source§

fn default() -> Device

Returns the “default value” for a type. Read more
Source§

impl Device for Device

Available on any_runtime only.

What lets this stand in wherever a runtime’s own device type did: burn’s backends take a device through this trait, and are handed this one.

Only where a runtime is linked, since the trait asks for a default.

Source§

fn from_id(device_id: DeviceId) -> Device

Create a device from its id.
Source§

fn to_id(&self) -> DeviceId

Retrieve the device id from the device.
Source§

impl DeviceOps for Device

Source§

fn defaults(&self) -> DeviceSettings

Returns the default settings for this device.
Source§

fn id(&self) -> DeviceId

Returns the device id.
Source§

impl Eq for Device

Source§

impl From<AmdDevice> for Device

Source§

fn from(device: AmdDevice) -> Device

Converts to this type from the input type.
Source§

impl From<CpuDevice> for Device

Source§

fn from(device: CpuDevice) -> Device

Converts to this type from the input type.
Source§

impl From<CudaDevice> for Device

Source§

fn from(device: CudaDevice) -> Device

Converts to this type from the input type.
Source§

impl From<MetalDevice> for Device

Source§

fn from(device: MetalDevice) -> Device

Converts to this type from the input type.
Source§

impl From<WgpuDevice> for Device

Source§

fn from(device: WgpuDevice) -> Device

Converts to this type from the input type.
Source§

impl Hash for Device

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Device

Source§

fn eq(&self, other: &Device) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Device

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneExpand for T
where T: Clone,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CompilationArg for T
where T: Clone + PartialEq + Eq + Hash + Debug + Send + Sync + 'static,

Source§

fn dynamic_cast<Arg>(&self) -> Arg
where Arg: CompilationArg,

Compilation args should be the same even with different element types. However, it isn’t possible to enforce it with the type system. So, we make the compilation args serializable and dynamically cast them. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<K, Q> Equivalent<Q> for K
where K: Borrow<Q> + ?Sized, Q: Eq + ?Sized,

Source§

fn equivalent(&self, key: &Q) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoComptime for T

Source§

fn comptime(self) -> Self

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TuneInputs for T
where T: Clone + Send + Sync + 'static,

Source§

type At<'a> = T

The concrete input type at lifetime 'a.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more