use crate::dtype::DataType;
use crate::error::{Error, Result};
pub(crate) fn check_host_read<D: DataType, T>(dtype: D) -> Result<()> {
let elem_bytes = std::mem::size_of::<T>();
let dtype_bytes = dtype.size_in_bytes();
if dtype.is_quantized() || dtype_bytes != elem_bytes {
return Err(Error::HostReadTypeMismatch {
dtype: dtype.short_name(),
dtype_bytes,
elem_bytes,
});
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::dtype::DType;
use crate::error::Error;
use crate::runtime::cpu::{CpuDevice, CpuRuntime};
use crate::tensor::{Storage, Tensor};
fn is_mismatch<T>(result: crate::error::Result<T>, bytes: usize, elem: usize) -> bool {
matches!(
result,
Err(Error::HostReadTypeMismatch { dtype_bytes, elem_bytes, .. })
if dtype_bytes == bytes && elem_bytes == elem
)
}
#[test]
fn tensor_read_of_a_narrower_dtype_is_an_error() {
let device = CpuDevice::new();
let bits = [0x3f80u16, 0x4000, 0x4040];
let bytes: &[u8] = bytemuck::cast_slice(&bits);
let tensor = Tensor::<CpuRuntime>::from_bytes(bytes, &[3], DType::BF16, &device)
.expect("bf16 tensor builds");
assert!(is_mismatch(tensor.try_to_vec::<f32>(), 2, 4));
assert_eq!(tensor.try_to_vec::<u16>().ok(), Some(bits.to_vec()));
}
#[test]
fn narrowed_read_in_the_native_width_returns_the_last_row() {
let device = CpuDevice::new();
let bits: Vec<u16> = (0..6).collect();
let bytes: &[u8] = bytemuck::cast_slice(&bits);
let tensor = Tensor::<CpuRuntime>::from_bytes(bytes, &[1, 2, 3], DType::F16, &device)
.expect("f16 tensor builds");
let row = tensor.narrow(-2, 1, 1).expect("narrow runs");
assert!(is_mismatch(row.try_to_vec::<f32>(), 2, 4));
assert_eq!(row.try_to_vec::<u16>().ok(), Some(vec![3, 4, 5]));
}
#[test]
fn tensor_read_of_a_wider_dtype_is_an_error() {
let device = CpuDevice::new();
let tensor = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2], &device)
.expect("f32 tensor builds");
assert!(is_mismatch(tensor.try_to_vec::<u8>(), 4, 1));
assert!(is_mismatch(tensor.try_to_vec::<f64>(), 4, 8));
assert_eq!(tensor.try_to_vec::<f32>().ok(), Some(vec![1.0, 2.0]));
}
#[test]
fn pipelined_read_of_a_mismatched_dtype_is_an_error() {
let device = CpuDevice::new();
let tensor = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2], &device)
.expect("f32 tensor builds");
let event = tensor.record_event().expect("event records");
assert!(is_mismatch(tensor.to_vec_pipelined::<f64>(event), 4, 8));
}
#[test]
fn item_of_a_mismatched_dtype_is_an_error() {
let device = CpuDevice::new();
let bytes: &[u8] = bytemuck::cast_slice(&[0x3f80u16]);
let scalar = Tensor::<CpuRuntime>::from_bytes(bytes, &[], DType::BF16, &device)
.expect("bf16 scalar builds");
assert!(is_mismatch(scalar.item::<f32>(), 2, 4));
assert_eq!(scalar.item::<u16>().ok(), Some(0x3f80));
}
#[test]
fn storage_read_of_a_mismatched_dtype_is_an_error() {
let device = CpuDevice::new();
let bits = [1u16, 2, 3, 4];
let bytes: &[u8] = bytemuck::cast_slice(&bits);
let storage =
Storage::<CpuRuntime>::from_bytes(bytes, DType::F16, &device).expect("storage builds");
assert!(is_mismatch(storage.try_to_vec::<f32>(), 2, 4));
assert_eq!(storage.try_to_vec::<u16>().ok(), Some(bits.to_vec()));
}
}