pub struct Device { /* private fields */ }Expand description
A device on which tensors can be created and computations performed.
Internally this is an Arc<dyn DynBackendDevice> — a trait-object handle
to the concrete backend device. CPU, CUDA, Metal, and arbitrary third-party
backends are all accessed through this single type.
§Example
use fuel_tensor::{Device, DType};
use fuel_tensor::lazy::Tensor;
let dev = Device::cpu();
let t = Tensor::zeros((2, 3), DType::F32, &dev)?;
assert_eq!(t.shape().dims(), &[2, 3]);Implementations§
Source§impl Device
impl Device
Sourcepub fn cpu() -> Self
pub fn cpu() -> Self
Returns a CPU device.
§Example
use fuel_tensor::Device;
let dev = Device::cpu();
assert!(dev.is_cpu());Sourcepub fn custom(device: Arc<dyn DynBackendDevice>) -> Self
pub fn custom(device: Arc<dyn DynBackendDevice>) -> Self
Creates a device backed by a custom DynBackendDevice.
The device uses dynamic dispatch for all operations, enabling
third-party backends without modifying fuel-core.
Sourcepub fn weight_residency_stats(&self) -> WeightResidencyStats
pub fn weight_residency_stats(&self) -> WeightResidencyStats
Counters of this device’s weight residency: how many weight Consts were served from an
earlier upload, how many were uploaded, how many were evicted and how many are resident now.
Shared by every clone of this Device; always zero on a CPU device.
Sourcepub fn clear_weight_residency(&self)
pub fn clear_weight_residency(&self)
The unload call. Drop every resident weight upload of this device, so the card gets the
model’s memory back at once. Call it when a model is unloaded or replaced and the process goes on
using the device: without it a dropped model’s weights are only evicted at the next seeding on
this device (see [crate::const_residency]), and one that is still loaded is only evicted when
the byte cap forces it out. The next realize that needs a weight uploads it again. Storages a
running realize (or a held decode session) still holds stay alive until it ends.
Sourcepub fn as_dyn(&self) -> &Arc<dyn DynBackendDevice> ⓘ
pub fn as_dyn(&self) -> &Arc<dyn DynBackendDevice> ⓘ
Borrow the underlying Arc<dyn DynBackendDevice>.
Phase 7.5 work item G2: fuel-graph’s slot-populating
constructors (Tensor::from_f32, const_f32_like, etc.) take
a &Arc<dyn DynBackendDevice> so they can allocate Storage on
the right device without depending on fuel-core. Callers that
hold a &fuel_tensor::Device thread it through with
dev.as_dyn().
Sourcepub fn set_seed(&self, seed: u64) -> Result<()>
pub fn set_seed(&self, seed: u64) -> Result<()>
Sets the random seed for this device’s random number generator.
Sourcepub fn get_current_seed(&self) -> Result<u64>
pub fn get_current_seed(&self) -> Result<u64>
Returns the current random seed for this device.
Sourcepub fn same_device(&self, rhs: &Self) -> bool
pub fn same_device(&self, rhs: &Self) -> bool
Returns true if both devices refer to the same physical device.
§Example
use fuel_tensor::Device;
assert!(Device::cpu().same_device(&Device::cpu()));Sourcepub fn location(&self) -> DeviceLocation
pub fn location(&self) -> DeviceLocation
Returns the physical DeviceLocation for this device.
§Example
use fuel_tensor::{Device, DeviceLocation};
assert_eq!(Device::cpu().location(), DeviceLocation::Cpu);Sourcepub fn is_cpu(&self) -> bool
pub fn is_cpu(&self) -> bool
Returns true if this is the CPU device.
§Example
use fuel_tensor::Device;
assert!(Device::cpu().is_cpu());Sourcepub fn is_cuda(&self) -> bool
pub fn is_cuda(&self) -> bool
Returns true if this is a CUDA device.
§Example
use fuel_tensor::Device;
assert!(!Device::cpu().is_cuda());Sourcepub fn is_metal(&self) -> bool
pub fn is_metal(&self) -> bool
Returns true if this is a Metal device.
§Example
use fuel_tensor::Device;
assert!(!Device::cpu().is_metal());Sourcepub fn supports_bf16(&self) -> bool
pub fn supports_bf16(&self) -> bool
Returns true if this device has native BF16 support.
§Example
use fuel_tensor::Device;
// CPU does not have native BF16 support
assert!(!Device::cpu().supports_bf16());Sourcepub fn bf16_default_to_f32(&self) -> DType
pub fn bf16_default_to_f32(&self) -> DType
Returns DType::BF16 if supported, otherwise DType::F32.
§Example
use fuel_tensor::{Device, DType};
assert_eq!(Device::cpu().bf16_default_to_f32(), DType::F32);Sourcepub fn synchronize(&self) -> Result<()>
pub fn synchronize(&self) -> Result<()>
Synchronizes the device, waiting for all pending operations to complete.
This is a no-op on CPU.
use fuel_tensor::Device;
Device::cpu().synchronize()?;Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Device
impl !UnwindSafe for Device
impl Forget for Device
impl Freeze for Device
impl Move for Device
impl Send for Device
impl Sync for Device
impl Unpin for Device
impl UnsafeUnpin for Device
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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