use crate::error::Result;
use crate::type_traits::AnyCompatible;
use tvm_ffi_sys::dlpack::DLDevice;
use tvm_ffi_sys::{TVMFFIAny, TVMFFITypeIndex as TypeIndex};
use tvm_ffi_sys::{TVMFFIEnvGetStream, TVMFFIEnvSetStream, TVMFFIStreamHandle};
pub fn current_stream(device: &DLDevice) -> TVMFFIStreamHandle {
unsafe { TVMFFIEnvGetStream(device.device_type, device.device_id) }
}
pub unsafe fn with_stream<T>(
device: &DLDevice,
stream: TVMFFIStreamHandle,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
let mut prev_stream: TVMFFIStreamHandle = std::ptr::null_mut();
unsafe {
crate::check_safe_call!(TVMFFIEnvSetStream(
device.device_type,
device.device_id,
stream,
&mut prev_stream as *mut TVMFFIStreamHandle
))?;
}
let restore = RestoreStream {
device: *device,
stream: prev_stream,
};
let result = f();
let restored = restore.restore();
let value = result?;
restored?;
Ok(value)
}
struct RestoreStream {
device: DLDevice,
stream: TVMFFIStreamHandle,
}
impl RestoreStream {
fn set(&self) -> i32 {
unsafe {
TVMFFIEnvSetStream(
self.device.device_type,
self.device.device_id,
self.stream,
std::ptr::null_mut(),
)
}
}
fn restore(self) -> Result<()> {
let restore = std::mem::ManuallyDrop::new(self);
crate::check_safe_call!(restore.set())
}
}
impl Drop for RestoreStream {
fn drop(&mut self) {
self.set();
}
}
unsafe impl AnyCompatible for DLDevice {
#[inline]
unsafe fn is_undefined(field: *const Self) -> bool {
unsafe { *std::ptr::addr_of!((*field).device_type) == 0 }
}
fn type_str() -> String {
"Device".to_string()
}
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIDevice as i32;
data.small_str_len = 0;
data.data_union.v_uint64 = 0;
data.data_union.v_device = *src;
}
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIDevice as i32;
data.small_str_len = 0;
data.data_union.v_int64 = 0;
data.data_union.v_device = src;
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
data.type_index == TypeIndex::kTVMFFIDevice as i32
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { data.data_union.v_device }
}
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { data.data_union.v_device }
}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFIDevice as i32 {
return Ok(unsafe { data.data_union.v_device });
}
let text = unsafe { <crate::String as AnyCompatible>::try_cast_from_any_view(data)? };
parse_device(text.as_str()).ok_or(())
}
}
#[cold]
fn parse_device(text: &str) -> Option<DLDevice> {
use tvm_ffi_sys::dlpack::DLDeviceType::*;
let text = text.split(' ').next().unwrap_or_default();
let (name, index) = match text.split_once(':') {
Some((name, index)) => (name, Some(index)),
None => (text, None),
};
let device_type = match name {
"cpu" => kDLCPU,
"cuda" => kDLCUDA,
"opencl" => kDLOpenCL,
"vulkan" => kDLVulkan,
"metal" | "mps" => kDLMetal,
"vpi" => kDLVPI,
"rocm" => kDLROCM,
"ext_dev" => kDLExtDev,
"oneapi" | "xpu" => kDLOneAPI,
"hexagon" => kDLHexagon,
"wgpu" | "webgpu" => kDLWebGPU,
"maia" => kDLMAIA,
"trn" => kDLTrn,
"tpu" => kDLTPU,
"tpu_host" => kDLTPUHost,
"ascend" => kDLAscend,
_ => return None,
};
let device_id = match index {
Some(index) if !index.is_empty() && index.bytes().all(|b| b.is_ascii_digit()) => {
index.parse::<i32>().ok()?
}
Some(_) => return None,
None => 0,
};
Some(DLDevice {
device_type: device_type as i32,
device_id,
})
}