pub use ruda_core::device::*;
use ruda_core::tensor::{BoolDType, DType, FloatDType, IntDType};
use crate::Backend;
pub use ruda_core::tensor::device_settings::{DeviceSettings, DeviceError};
use ruda_core::tensor::device_settings::DeviceSettingsRegistry;
#[cfg(feature = "std")]
pub use std::collections::HashMap;
#[cfg(not(feature = "std"))]
pub use hashbrown::HashMap;
pub trait DeviceOps: Clone + Default + PartialEq + Send + Sync + core::fmt::Debug + Device {
fn id(&self) -> DeviceId {
self.to_id()
}
fn inner(&self) -> &Self {
self
}
}
pub fn get_device_settings<B: Backend>(device: &B::Device) -> DeviceSettings {
let default_settings = || {
DeviceSettings::new(
default_float::<B>(),
default_int::<B>(),
default_bool::<B>(device),
)
};
DeviceSettingsRegistry::get_or_insert(device, default_settings)
}
fn default_bool<B: Backend>(device: &B::Device) -> BoolDType {
let default_bool: BoolDType = <B::BoolElem as crate::Element>::dtype().into();
ruda_core::tensor::device_settings::select_bool_dtype(default_bool, |dtype| B::supports_dtype(device, dtype))
}
fn default_float<B: Backend>() -> FloatDType {
<B::FloatElem as crate::Element>::dtype().into()
}
fn default_int<B: Backend>() -> IntDType {
<B::IntElem as crate::Element>::dtype().into()
}
fn check_dtype_support<B: Backend>(
device: &B::Device,
dtype: impl Into<DType>,
) -> Result<(), DeviceError> {
let dtype = dtype.into();
if B::supports_dtype(device, dtype) {
Ok(())
} else {
Err(DeviceError::unsupported_dtype(device, dtype))
}
}
pub fn set_default_dtypes<B: Backend>(
device: &B::Device,
float_dtype: impl Into<FloatDType>,
int_dtype: impl Into<IntDType>,
) -> Result<(), DeviceError> {
let float_dtype = float_dtype.into();
let int_dtype = int_dtype.into();
check_dtype_support::<B>(device, float_dtype)?;
check_dtype_support::<B>(device, int_dtype)?;
let settings = DeviceSettings::new(float_dtype, int_dtype, default_bool::<B>(device));
initialize_unchecked(device, settings)?;
Ok(())
}
pub fn set_default_float_dtype<B: Backend>(
device: &B::Device,
dtype: impl Into<FloatDType>,
) -> Result<(), DeviceError> {
let dtype = dtype.into();
check_dtype_support::<B>(device, dtype)?;
let settings = DeviceSettings::new(dtype, default_int::<B>(), default_bool::<B>(device));
initialize_unchecked(device, settings)?;
Ok(())
}
pub fn set_default_int_dtype<B: Backend>(
device: &B::Device,
dtype: impl Into<IntDType>,
) -> Result<(), DeviceError> {
let dtype = dtype.into();
check_dtype_support::<B>(device, dtype)?;
let settings = DeviceSettings::new(default_float::<B>(), dtype, default_bool::<B>(device));
initialize_unchecked(device, settings)?;
Ok(())
}
fn initialize_unchecked<D: DeviceOps>(
device: &D,
settings: DeviceSettings,
) -> Result<(), DeviceError> {
DeviceSettingsRegistry::init(device, settings)
}
mod adapters;