use super::StorageBackend;
use crate::err::Result;
use crate::shapes::ShapeBuf;
use crate::tensor::device::DeviceId;
use crate::tensor::dtype::{DType, DTypeDescriptor};
pub trait HostReadback: StorageBackend {
fn float_to_vec1<K: DType>(storage: &Self::Storage<K>) -> Result<alloc::vec::Vec<f64>>;
fn int_to_vec1<K: DType>(storage: &Self::Storage<K>) -> Result<alloc::vec::Vec<i64>>;
}
pub trait HostInterop: StorageBackend + HostReadback {
fn host_shape<K: DType>(storage: &Self::Storage<K>) -> ShapeBuf {
<Self as StorageBackend>::shape(storage)
}
fn host_storage_dtype<K: DType>(storage: &Self::Storage<K>) -> Option<DTypeDescriptor> {
<Self as StorageBackend>::storage_dtype(storage)
}
fn host_storage_device<K: DType>(storage: &Self::Storage<K>) -> Option<DeviceId> {
<Self as StorageBackend>::storage_device(storage)
}
fn host_format_display<K: DType>(storage: &Self::Storage<K>) -> alloc::string::String {
use crate::tensor::display::{Values, render};
let shape = Self::shape(storage);
match Self::storage_dtype(storage) {
None => alloc::format!("<tensor: shape={shape:?}, dtype unknown to this backend>"),
Some(dtype) if dtype.is_quantized() => alloc::format!(
"<{} tensor: shape={shape:?}, not printable without dequantizing>",
dtype.name()
),
Some(dtype) if dtype.is_integer() => match Self::int_to_vec1(storage) {
Ok(values) => render(&shape, &Values::Int(values)),
Err(err) => alloc::format!("<tensor: shape={shape:?}, values unavailable: {err}>"),
},
Some(_) => match Self::float_to_vec1(storage) {
Ok(values) => render(&shape, &Values::Float(values)),
Err(err) => alloc::format!("<tensor: shape={shape:?}, values unavailable: {err}>"),
},
}
}
fn host_format_debug<K: DType>(storage: &Self::Storage<K>) -> alloc::string::String {
Self::host_format_display(storage)
}
fn to_bytes<K: DType>(storage: &Self::Storage<K>) -> Result<alloc::vec::Vec<u8>>;
fn from_bytes<K: DType>(
bytes: &[u8],
shape: &[usize],
dtype: DTypeDescriptor,
device: &DeviceId,
) -> Result<Self::Storage<K>>;
}
pub trait TransferBackend<NewD: crate::tensor::device::Device>:
super::StorageTransfer<NewD>
{
}
impl<B, NewD> TransferBackend<NewD> for B
where
B: super::StorageTransfer<NewD>,
NewD: crate::tensor::device::Device,
{
}