use std::fmt;
use crate::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PrivateTag(u8);
impl PrivateTag {
#[inline]
pub fn byte(self) -> u8 {
self.0
}
}
pub const MIN_BUFFER_ALIGNMENT: u32 = 64;
pub const PAGE_ALIGNMENT: u32 = 4096;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SyncMode {
ProducerSynced,
Event,
ConsumerStream,
}
impl SyncMode {
pub fn from_byte(b: u8) -> crate::Result<Self> {
match b {
0x00 => Ok(Self::ProducerSynced),
0x01 => Ok(Self::Event),
0x02 => Ok(Self::ConsumerStream),
_ => Err(Error::InvalidSyncMode(b)),
}
}
pub fn to_byte(self) -> u8 {
match self {
Self::ProducerSynced => 0x00,
Self::Event => 0x01,
Self::ConsumerStream => 0x02,
}
}
}
impl fmt::Display for SyncMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ProducerSynced => f.write_str("producer_synced"),
Self::Event => f.write_str("event"),
Self::ConsumerStream => f.write_str("consumer_stream"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DeviceTag {
Cpu,
Cuda,
Rocm,
Metal,
Vulkan,
WebGpu,
Hexagon,
LevelZero,
OpenCl,
Private(PrivateTag),
}
impl DeviceTag {
pub fn from_byte(b: u8) -> crate::Result<Self> {
match b {
0x00 => Ok(Self::Cpu),
0x01 => Ok(Self::Cuda),
0x02 => Ok(Self::Rocm),
0x03 => Ok(Self::Metal),
0x04 => Ok(Self::Vulkan),
0x05 => Ok(Self::WebGpu),
0x06 => Ok(Self::Hexagon),
0x07 => Ok(Self::LevelZero),
0x08 => Ok(Self::OpenCl),
0x09..=0xEF => Err(Error::ReservedDeviceTag(b)),
0xF0..=0xFE => Ok(Self::Private(PrivateTag(b))),
0xFF => Err(Error::InvalidDeviceTag(b)),
}
}
pub fn to_byte(self) -> u8 {
match self {
Self::Cpu => 0x00,
Self::Cuda => 0x01,
Self::Rocm => 0x02,
Self::Metal => 0x03,
Self::Vulkan => 0x04,
Self::WebGpu => 0x05,
Self::Hexagon => 0x06,
Self::LevelZero => 0x07,
Self::OpenCl => 0x08,
Self::Private(t) => t.0,
}
}
pub fn is_private(self) -> bool {
matches!(self, Self::Private(_))
}
}
impl fmt::Display for DeviceTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cpu => f.write_str("cpu"),
Self::Cuda => f.write_str("cuda"),
Self::Rocm => f.write_str("rocm"),
Self::Metal => f.write_str("metal"),
Self::Vulkan => f.write_str("vulkan"),
Self::WebGpu => f.write_str("webgpu"),
Self::Hexagon => f.write_str("hexagon"),
Self::LevelZero => f.write_str("level_zero"),
Self::OpenCl => f.write_str("opencl"),
Self::Private(t) => write!(f, "private(0x{:02X})", t.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PrivateMemoryClass(u8);
impl PrivateMemoryClass {
pub fn to_byte(self) -> u8 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MemoryClass {
Standard,
HostPinned,
Unified,
Peer,
Private(PrivateMemoryClass),
}
impl MemoryClass {
pub fn from_byte(b: u8) -> crate::Result<Self> {
match b {
0x00 => Ok(Self::Standard),
0x01 => Ok(Self::HostPinned),
0x02 => Ok(Self::Unified),
0x03 => Ok(Self::Peer),
0x04..=0xEF => Err(Error::ReservedMemoryClass(b)),
0xF0..=0xFE => Ok(Self::Private(PrivateMemoryClass(b))),
0xFF => Err(Error::InvalidMemoryClass(b)),
}
}
pub fn to_byte(self) -> u8 {
match self {
Self::Standard => 0x00,
Self::HostPinned => 0x01,
Self::Unified => 0x02,
Self::Peer => 0x03,
Self::Private(p) => p.0,
}
}
pub fn is_private(self) -> bool {
matches!(self, Self::Private(_))
}
}
impl fmt::Display for MemoryClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Standard => f.write_str("standard"),
Self::HostPinned => f.write_str("host_pinned"),
Self::Unified => f.write_str("unified"),
Self::Peer => f.write_str("peer"),
Self::Private(p) => write!(f, "private(0x{:02X})", p.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BufferHandle {
byte_size: u64,
alignment: u32,
device_tag: DeviceTag,
sync_mode: SyncMode,
memory_class: MemoryClass,
}
impl BufferHandle {
pub fn new(
byte_size: u64,
alignment: u32,
device_tag: DeviceTag,
sync_mode: SyncMode,
) -> crate::Result<Self> {
Self::with_memory_class(
byte_size,
alignment,
device_tag,
sync_mode,
MemoryClass::Standard,
)
}
pub fn with_memory_class(
byte_size: u64,
alignment: u32,
device_tag: DeviceTag,
sync_mode: SyncMode,
memory_class: MemoryClass,
) -> crate::Result<Self> {
if !alignment.is_power_of_two() {
return Err(Error::AlignmentNotPowerOfTwo { alignment });
}
if byte_size > 0 && alignment < MIN_BUFFER_ALIGNMENT {
return Err(Error::AlignmentBelowMinimum {
alignment,
minimum: MIN_BUFFER_ALIGNMENT,
});
}
if device_tag == DeviceTag::Cpu && sync_mode != SyncMode::ProducerSynced {
return Err(Error::InvalidSyncMode(sync_mode.to_byte()));
}
Ok(Self {
byte_size,
alignment,
device_tag,
sync_mode,
memory_class,
})
}
pub fn empty(device_tag: DeviceTag) -> Self {
Self {
byte_size: 0,
alignment: 1,
device_tag,
sync_mode: SyncMode::ProducerSynced,
memory_class: MemoryClass::Standard,
}
}
pub fn byte_size(self) -> u64 {
self.byte_size
}
pub fn alignment(self) -> u32 {
self.alignment
}
pub fn device_tag(self) -> DeviceTag {
self.device_tag
}
pub fn sync_mode(self) -> SyncMode {
self.sync_mode
}
pub fn memory_class(self) -> MemoryClass {
self.memory_class
}
pub fn is_empty(self) -> bool {
self.byte_size == 0
}
}
pub fn validate_colocation(handles: &[BufferHandle]) -> crate::Result<DeviceTag> {
let first = handles.first().ok_or(Error::EmptyBufferList)?;
let expected_tag = first.device_tag;
let expected_tag_byte = expected_tag.to_byte();
let expected_class = first.memory_class;
let expected_class_byte = expected_class.to_byte();
for handle in handles.iter().skip(1) {
if handle.device_tag != expected_tag {
return Err(Error::DeviceTagMismatch {
expected: expected_tag_byte,
found: handle.device_tag.to_byte(),
});
}
if handle.memory_class != expected_class {
return Err(Error::MemoryClassMismatch {
expected: expected_class_byte,
found: handle.memory_class.to_byte(),
});
}
}
Ok(expected_tag)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_byte_cpu() {
assert_eq!(DeviceTag::from_byte(0x00).unwrap(), DeviceTag::Cpu);
}
#[test]
fn from_byte_cuda() {
assert_eq!(DeviceTag::from_byte(0x01).unwrap(), DeviceTag::Cuda);
}
#[test]
fn from_byte_rocm() {
assert_eq!(DeviceTag::from_byte(0x02).unwrap(), DeviceTag::Rocm);
}
#[test]
fn from_byte_metal() {
assert_eq!(DeviceTag::from_byte(0x03).unwrap(), DeviceTag::Metal);
}
#[test]
fn from_byte_reserved_lower_bound() {
assert!(matches!(
DeviceTag::from_byte(0x09),
Err(Error::ReservedDeviceTag(0x09))
));
}
#[test]
fn from_byte_reserved_upper_bound() {
assert!(matches!(
DeviceTag::from_byte(0xEF),
Err(Error::ReservedDeviceTag(0xEF))
));
}
#[test]
fn from_byte_reserved_mid_range() {
assert!(matches!(
DeviceTag::from_byte(0x80),
Err(Error::ReservedDeviceTag(0x80))
));
}
#[test]
fn from_byte_private_lower_bound() {
let tag = DeviceTag::from_byte(0xF0).unwrap();
assert!(tag.is_private());
assert_eq!(tag.to_byte(), 0xF0);
}
#[test]
fn from_byte_private_upper_bound() {
let tag = DeviceTag::from_byte(0xFE).unwrap();
assert!(tag.is_private());
assert_eq!(tag.to_byte(), 0xFE);
}
#[test]
fn from_byte_invalid_sentinel() {
assert!(matches!(
DeviceTag::from_byte(0xFF),
Err(Error::InvalidDeviceTag(0xFF))
));
}
#[test]
fn to_byte_cpu() {
assert_eq!(DeviceTag::Cpu.to_byte(), 0x00);
}
#[test]
fn to_byte_cuda() {
assert_eq!(DeviceTag::Cuda.to_byte(), 0x01);
}
#[test]
fn to_byte_rocm() {
assert_eq!(DeviceTag::Rocm.to_byte(), 0x02);
}
#[test]
fn to_byte_metal() {
assert_eq!(DeviceTag::Metal.to_byte(), 0x03);
}
#[test]
fn to_byte_private() {
assert_eq!(DeviceTag::from_byte(0xF2).unwrap().to_byte(), 0xF2);
}
#[test]
fn round_trip_named_variants() {
for b in [0x00u8, 0x01, 0x02, 0x03] {
let tag = DeviceTag::from_byte(b).unwrap();
assert_eq!(tag.to_byte(), b, "round-trip failed for byte 0x{b:02X}");
}
}
#[test]
fn round_trip_private_range() {
for b in 0xF0u8..=0xFE {
let tag = DeviceTag::from_byte(b).unwrap();
assert_eq!(tag.to_byte(), b, "round-trip failed for byte 0x{b:02X}");
}
}
#[test]
fn is_private_true_for_private_variant() {
assert!(DeviceTag::from_byte(0xF0).unwrap().is_private());
}
#[test]
fn is_private_false_for_cpu() {
assert!(!DeviceTag::Cpu.is_private());
}
#[test]
fn is_private_false_for_cuda() {
assert!(!DeviceTag::Cuda.is_private());
}
#[test]
fn is_private_false_for_rocm() {
assert!(!DeviceTag::Rocm.is_private());
}
#[test]
fn is_private_false_for_metal() {
assert!(!DeviceTag::Metal.is_private());
}
#[test]
fn display_cpu() {
assert_eq!(DeviceTag::Cpu.to_string(), "cpu");
}
#[test]
fn display_cuda() {
assert_eq!(DeviceTag::Cuda.to_string(), "cuda");
}
#[test]
fn display_rocm() {
assert_eq!(DeviceTag::Rocm.to_string(), "rocm");
}
#[test]
fn display_metal() {
assert_eq!(DeviceTag::Metal.to_string(), "metal");
}
#[test]
fn display_private() {
assert_eq!(
DeviceTag::from_byte(0xF1).unwrap().to_string(),
"private(0xF1)"
);
}
#[test]
fn display_private_lower_bound() {
assert_eq!(
DeviceTag::from_byte(0xF0).unwrap().to_string(),
"private(0xF0)"
);
}
#[test]
fn display_private_upper_bound() {
assert_eq!(
DeviceTag::from_byte(0xFE).unwrap().to_string(),
"private(0xFE)"
);
}
#[test]
fn min_buffer_alignment_is_64() {
assert_eq!(MIN_BUFFER_ALIGNMENT, 64);
}
#[test]
fn page_alignment_is_4096() {
assert_eq!(PAGE_ALIGNMENT, 4096);
}
#[test]
fn min_buffer_alignment_is_power_of_two() {
assert!(MIN_BUFFER_ALIGNMENT.is_power_of_two());
}
#[test]
fn page_alignment_is_power_of_two() {
assert!(PAGE_ALIGNMENT.is_power_of_two());
}
#[test]
fn new_nonempty_cpu_min_alignment() {
let result = BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced);
assert!(result.is_ok());
}
#[test]
fn new_nonempty_cuda_page_alignment() {
let result = BufferHandle::new(4096, 4096, DeviceTag::Cuda, SyncMode::Event);
assert!(result.is_ok());
}
#[test]
fn new_empty_alignment_1_is_valid() {
let result = BufferHandle::new(0, 1, DeviceTag::Cpu, SyncMode::ProducerSynced);
assert!(result.is_ok());
}
#[test]
fn new_empty_alignment_64_is_valid() {
let result = BufferHandle::new(0, 64, DeviceTag::Cpu, SyncMode::ProducerSynced);
assert!(result.is_ok());
}
#[test]
fn new_alignment_zero_not_power_of_two() {
assert!(matches!(
BufferHandle::new(1024, 0, DeviceTag::Cpu, SyncMode::ProducerSynced),
Err(Error::AlignmentNotPowerOfTwo { alignment: 0 })
));
}
#[test]
fn new_alignment_63_not_power_of_two() {
assert!(matches!(
BufferHandle::new(1024, 63, DeviceTag::Cpu, SyncMode::ProducerSynced),
Err(Error::AlignmentNotPowerOfTwo { alignment: 63 })
));
}
#[test]
fn new_alignment_100_not_power_of_two() {
assert!(matches!(
BufferHandle::new(1024, 100, DeviceTag::Cpu, SyncMode::ProducerSynced),
Err(Error::AlignmentNotPowerOfTwo { alignment: 100 })
));
}
#[test]
fn new_nonempty_alignment_below_minimum_32() {
assert!(matches!(
BufferHandle::new(1, 32, DeviceTag::Cpu, SyncMode::ProducerSynced),
Err(Error::AlignmentBelowMinimum {
alignment: 32,
minimum: 64
})
));
}
#[test]
fn new_nonempty_alignment_below_minimum_1() {
assert!(matches!(
BufferHandle::new(1, 1, DeviceTag::Cpu, SyncMode::ProducerSynced),
Err(Error::AlignmentBelowMinimum {
alignment: 1,
minimum: 64
})
));
}
#[test]
fn new_cpu_with_event_sync_rejected() {
assert!(matches!(
BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::Event),
Err(Error::InvalidSyncMode(0x01))
));
}
#[test]
fn new_cpu_with_consumer_stream_rejected() {
assert!(matches!(
BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ConsumerStream),
Err(Error::InvalidSyncMode(0x02))
));
}
#[test]
fn empty_byte_size_is_zero() {
assert_eq!(BufferHandle::empty(DeviceTag::Cpu).byte_size(), 0);
}
#[test]
fn empty_alignment_is_one() {
assert_eq!(BufferHandle::empty(DeviceTag::Cpu).alignment(), 1);
}
#[test]
fn empty_is_empty_returns_true() {
assert!(BufferHandle::empty(DeviceTag::Cpu).is_empty());
}
#[test]
fn empty_preserves_device_tag_cpu() {
assert_eq!(
BufferHandle::empty(DeviceTag::Cpu).device_tag(),
DeviceTag::Cpu
);
}
#[test]
fn empty_preserves_device_tag_cuda() {
assert_eq!(
BufferHandle::empty(DeviceTag::Cuda).device_tag(),
DeviceTag::Cuda
);
}
#[test]
fn empty_preserves_device_tag_private() {
let private = DeviceTag::from_byte(0xF5).unwrap();
assert_eq!(BufferHandle::empty(private).device_tag(), private);
}
#[test]
fn accessors_byte_size() {
let handle = BufferHandle::new(8192, 4096, DeviceTag::Cuda, SyncMode::Event).unwrap();
assert_eq!(handle.byte_size(), 8192);
}
#[test]
fn accessors_alignment() {
let handle = BufferHandle::new(256, 256, DeviceTag::Metal, SyncMode::Event).unwrap();
assert_eq!(handle.alignment(), 256);
}
#[test]
fn accessors_device_tag() {
let handle = BufferHandle::new(512, 64, DeviceTag::Rocm, SyncMode::ConsumerStream).unwrap();
assert_eq!(handle.device_tag(), DeviceTag::Rocm);
}
#[test]
fn accessors_sync_mode_producer_synced() {
let handle = BufferHandle::new(512, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap();
assert_eq!(handle.sync_mode(), SyncMode::ProducerSynced);
}
#[test]
fn accessors_sync_mode_event() {
let handle = BufferHandle::new(512, 64, DeviceTag::Cuda, SyncMode::Event).unwrap();
assert_eq!(handle.sync_mode(), SyncMode::Event);
}
#[test]
fn accessors_sync_mode_consumer_stream() {
let handle = BufferHandle::new(512, 64, DeviceTag::Cuda, SyncMode::ConsumerStream).unwrap();
assert_eq!(handle.sync_mode(), SyncMode::ConsumerStream);
}
#[test]
fn accessors_is_empty_false_for_nonempty() {
let handle = BufferHandle::new(1, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap();
assert!(!handle.is_empty());
}
#[test]
fn accessors_is_empty_true_for_zero_size() {
let handle = BufferHandle::new(0, 1, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap();
assert!(handle.is_empty());
}
#[test]
fn colocation_empty_slice_returns_error() {
assert!(matches!(
validate_colocation(&[]),
Err(Error::EmptyBufferList)
));
}
#[test]
fn colocation_single_handle_returns_its_tag() {
let handle = BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap();
let result = validate_colocation(&[handle]);
assert_eq!(result.unwrap(), DeviceTag::Cpu);
}
#[test]
fn colocation_all_cpu_three_handles() {
let handles = [
BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
BufferHandle::new(512, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
BufferHandle::new(256, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
];
assert_eq!(validate_colocation(&handles).unwrap(), DeviceTag::Cpu);
}
#[test]
fn colocation_mixed_cpu_cuda_returns_mismatch() {
let handles = [
BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
BufferHandle::new(256, 64, DeviceTag::Cuda, SyncMode::ProducerSynced).unwrap(),
];
assert!(matches!(
validate_colocation(&handles),
Err(Error::DeviceTagMismatch {
expected: 0x00,
found: 0x01
})
));
}
#[test]
fn colocation_all_private_same_tag() {
let private = DeviceTag::from_byte(0xF0).unwrap();
let handles = [
BufferHandle::new(1024, 64, private, SyncMode::ProducerSynced).unwrap(),
BufferHandle::new(512, 64, private, SyncMode::ProducerSynced).unwrap(),
];
assert_eq!(validate_colocation(&handles).unwrap(), private);
}
#[test]
fn colocation_named_and_private_returns_mismatch() {
let private = DeviceTag::from_byte(0xF0).unwrap();
let handles = [
BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
BufferHandle::new(512, 64, private, SyncMode::ProducerSynced).unwrap(),
];
assert!(matches!(
validate_colocation(&handles),
Err(Error::DeviceTagMismatch {
expected: 0x00,
found: 0xF0
})
));
}
#[test]
fn colocation_mismatch_at_third_element() {
let handles = [
BufferHandle::new(1024, 64, DeviceTag::Cuda, SyncMode::Event).unwrap(),
BufferHandle::new(512, 64, DeviceTag::Cuda, SyncMode::Event).unwrap(),
BufferHandle::new(256, 64, DeviceTag::Metal, SyncMode::Event).unwrap(),
];
assert!(matches!(
validate_colocation(&handles),
Err(Error::DeviceTagMismatch {
expected: 0x01,
found: 0x03
})
));
}
mod sync_mode {
use super::*;
use std::collections::HashSet;
#[test]
fn from_byte_producer_synced() {
assert_eq!(SyncMode::from_byte(0x00).unwrap(), SyncMode::ProducerSynced);
}
#[test]
fn from_byte_event() {
assert_eq!(SyncMode::from_byte(0x01).unwrap(), SyncMode::Event);
}
#[test]
fn from_byte_consumer_stream() {
assert_eq!(SyncMode::from_byte(0x02).unwrap(), SyncMode::ConsumerStream);
}
#[test]
fn from_byte_0x03_is_invalid() {
assert!(matches!(
SyncMode::from_byte(0x03),
Err(Error::InvalidSyncMode(0x03))
));
}
#[test]
fn from_byte_0xfe_is_invalid() {
assert!(matches!(
SyncMode::from_byte(0xFE),
Err(Error::InvalidSyncMode(0xFE))
));
}
#[test]
fn from_byte_0xff_is_invalid() {
assert!(matches!(
SyncMode::from_byte(0xFF),
Err(Error::InvalidSyncMode(0xFF))
));
}
#[test]
fn to_byte_producer_synced() {
assert_eq!(SyncMode::ProducerSynced.to_byte(), 0x00);
}
#[test]
fn to_byte_event() {
assert_eq!(SyncMode::Event.to_byte(), 0x01);
}
#[test]
fn to_byte_consumer_stream() {
assert_eq!(SyncMode::ConsumerStream.to_byte(), 0x02);
}
#[test]
fn from_byte_to_byte_identity_producer_synced() {
let m = SyncMode::ProducerSynced;
assert_eq!(SyncMode::from_byte(m.to_byte()).unwrap(), m);
}
#[test]
fn from_byte_to_byte_identity_event() {
let m = SyncMode::Event;
assert_eq!(SyncMode::from_byte(m.to_byte()).unwrap(), m);
}
#[test]
fn from_byte_to_byte_identity_consumer_stream() {
let m = SyncMode::ConsumerStream;
assert_eq!(SyncMode::from_byte(m.to_byte()).unwrap(), m);
}
#[test]
fn display_producer_synced() {
assert_eq!(SyncMode::ProducerSynced.to_string(), "producer_synced");
}
#[test]
fn display_event() {
assert_eq!(SyncMode::Event.to_string(), "event");
}
#[test]
fn display_consumer_stream() {
assert_eq!(SyncMode::ConsumerStream.to_string(), "consumer_stream");
}
#[test]
fn clone_equals_original() {
for m in [
SyncMode::ProducerSynced,
SyncMode::Event,
SyncMode::ConsumerStream,
] {
assert_eq!(m, m.clone());
}
}
#[test]
fn copy_semantics() {
let original = SyncMode::Event;
let copied = original;
assert_eq!(original, copied);
}
#[test]
fn partial_eq_distinct_variants() {
assert_ne!(SyncMode::ProducerSynced, SyncMode::Event);
assert_ne!(SyncMode::ProducerSynced, SyncMode::ConsumerStream);
assert_ne!(SyncMode::Event, SyncMode::ConsumerStream);
}
#[test]
fn hash_all_variants_distinct() {
let set: HashSet<SyncMode> = [
SyncMode::ProducerSynced,
SyncMode::Event,
SyncMode::ConsumerStream,
]
.into_iter()
.collect();
assert_eq!(set.len(), 3);
}
}
mod device_tag_new_variants {
use super::*;
#[test]
fn vulkan_from_byte() {
assert_eq!(DeviceTag::from_byte(0x04).unwrap(), DeviceTag::Vulkan);
}
#[test]
fn vulkan_to_byte() {
assert_eq!(DeviceTag::Vulkan.to_byte(), 0x04);
}
#[test]
fn vulkan_round_trip() {
let tag = DeviceTag::from_byte(0x04).unwrap();
assert_eq!(tag.to_byte(), 0x04);
}
#[test]
fn vulkan_display() {
assert_eq!(DeviceTag::Vulkan.to_string(), "vulkan");
}
#[test]
fn vulkan_is_not_private() {
assert!(!DeviceTag::Vulkan.is_private());
}
#[test]
fn webgpu_from_byte() {
assert_eq!(DeviceTag::from_byte(0x05).unwrap(), DeviceTag::WebGpu);
}
#[test]
fn webgpu_to_byte() {
assert_eq!(DeviceTag::WebGpu.to_byte(), 0x05);
}
#[test]
fn webgpu_round_trip() {
let tag = DeviceTag::from_byte(0x05).unwrap();
assert_eq!(tag.to_byte(), 0x05);
}
#[test]
fn webgpu_display() {
assert_eq!(DeviceTag::WebGpu.to_string(), "webgpu");
}
#[test]
fn webgpu_is_not_private() {
assert!(!DeviceTag::WebGpu.is_private());
}
#[test]
fn hexagon_from_byte() {
assert_eq!(DeviceTag::from_byte(0x06).unwrap(), DeviceTag::Hexagon);
}
#[test]
fn hexagon_to_byte() {
assert_eq!(DeviceTag::Hexagon.to_byte(), 0x06);
}
#[test]
fn hexagon_round_trip() {
let tag = DeviceTag::from_byte(0x06).unwrap();
assert_eq!(tag.to_byte(), 0x06);
}
#[test]
fn hexagon_display() {
assert_eq!(DeviceTag::Hexagon.to_string(), "hexagon");
}
#[test]
fn hexagon_is_not_private() {
assert!(!DeviceTag::Hexagon.is_private());
}
#[test]
fn level_zero_from_byte() {
assert_eq!(DeviceTag::from_byte(0x07).unwrap(), DeviceTag::LevelZero);
}
#[test]
fn level_zero_to_byte() {
assert_eq!(DeviceTag::LevelZero.to_byte(), 0x07);
}
#[test]
fn level_zero_round_trip() {
let tag = DeviceTag::from_byte(0x07).unwrap();
assert_eq!(tag.to_byte(), 0x07);
}
#[test]
fn level_zero_display() {
assert_eq!(DeviceTag::LevelZero.to_string(), "level_zero");
}
#[test]
fn level_zero_is_not_private() {
assert!(!DeviceTag::LevelZero.is_private());
}
#[test]
fn opencl_from_byte() {
assert_eq!(DeviceTag::from_byte(0x08).unwrap(), DeviceTag::OpenCl);
}
#[test]
fn opencl_to_byte() {
assert_eq!(DeviceTag::OpenCl.to_byte(), 0x08);
}
#[test]
fn opencl_round_trip() {
let tag = DeviceTag::from_byte(0x08).unwrap();
assert_eq!(tag.to_byte(), 0x08);
}
#[test]
fn opencl_display() {
assert_eq!(DeviceTag::OpenCl.to_string(), "opencl");
}
#[test]
fn opencl_is_not_private() {
assert!(!DeviceTag::OpenCl.is_private());
}
#[test]
fn from_byte_0x08_is_last_named_tag() {
assert!(DeviceTag::from_byte(0x08).is_ok());
}
#[test]
fn from_byte_0x09_is_first_reserved_after_new_range() {
assert!(matches!(
DeviceTag::from_byte(0x09),
Err(Error::ReservedDeviceTag(0x09))
));
}
#[test]
fn round_trip_new_range_all_bytes() {
for b in 0x04u8..=0x08 {
let tag = DeviceTag::from_byte(b)
.unwrap_or_else(|e| panic!("from_byte(0x{b:02X}) failed: {e}"));
assert_eq!(tag.to_byte(), b, "round-trip failed for byte 0x{b:02X}");
}
}
}
mod memory_class {
use super::*;
#[test]
fn from_byte_standard() {
assert_eq!(MemoryClass::from_byte(0x00).unwrap(), MemoryClass::Standard);
}
#[test]
fn from_byte_host_pinned() {
assert_eq!(
MemoryClass::from_byte(0x01).unwrap(),
MemoryClass::HostPinned
);
}
#[test]
fn from_byte_unified() {
assert_eq!(MemoryClass::from_byte(0x02).unwrap(), MemoryClass::Unified);
}
#[test]
fn from_byte_peer() {
assert_eq!(MemoryClass::from_byte(0x03).unwrap(), MemoryClass::Peer);
}
#[test]
fn from_byte_reserved_lower_bound() {
assert!(matches!(
MemoryClass::from_byte(0x04),
Err(Error::ReservedMemoryClass(0x04))
));
}
#[test]
fn from_byte_reserved_upper_bound() {
assert!(matches!(
MemoryClass::from_byte(0xEF),
Err(Error::ReservedMemoryClass(0xEF))
));
}
#[test]
fn from_byte_private_lower_bound() {
let cls = MemoryClass::from_byte(0xF0).unwrap();
assert!(cls.is_private());
assert_eq!(cls.to_byte(), 0xF0);
}
#[test]
fn from_byte_private_upper_bound() {
let cls = MemoryClass::from_byte(0xFE).unwrap();
assert!(cls.is_private());
assert_eq!(cls.to_byte(), 0xFE);
}
#[test]
fn from_byte_invalid_sentinel() {
assert!(matches!(
MemoryClass::from_byte(0xFF),
Err(Error::InvalidMemoryClass(0xFF))
));
}
#[test]
fn named_variants_round_trip() {
for cls in [
MemoryClass::Standard,
MemoryClass::HostPinned,
MemoryClass::Unified,
MemoryClass::Peer,
] {
assert_eq!(MemoryClass::from_byte(cls.to_byte()).unwrap(), cls);
}
}
#[test]
fn buffer_handle_new_defaults_to_standard() {
let h = BufferHandle::new(64, 64, DeviceTag::Cuda, SyncMode::ProducerSynced).unwrap();
assert_eq!(h.memory_class(), MemoryClass::Standard);
}
#[test]
fn buffer_handle_with_memory_class_unified() {
let h = BufferHandle::with_memory_class(
64,
64,
DeviceTag::Cuda,
SyncMode::ProducerSynced,
MemoryClass::Unified,
)
.unwrap();
assert_eq!(h.memory_class(), MemoryClass::Unified);
}
#[test]
fn empty_handle_is_standard() {
assert_eq!(
BufferHandle::empty(DeviceTag::Cpu).memory_class(),
MemoryClass::Standard
);
}
#[test]
fn validate_colocation_rejects_mixed_memory_class() {
let standard =
BufferHandle::new(64, 64, DeviceTag::Cuda, SyncMode::ProducerSynced).unwrap();
let unified = BufferHandle::with_memory_class(
64,
64,
DeviceTag::Cuda,
SyncMode::ProducerSynced,
MemoryClass::Unified,
)
.unwrap();
assert!(matches!(
validate_colocation(&[standard, unified]),
Err(Error::MemoryClassMismatch {
expected: 0x00,
found: 0x02
})
));
}
#[test]
fn validate_colocation_accepts_uniform_memory_class() {
let a = BufferHandle::with_memory_class(
64,
64,
DeviceTag::Cuda,
SyncMode::ProducerSynced,
MemoryClass::Unified,
)
.unwrap();
let b = BufferHandle::with_memory_class(
128,
64,
DeviceTag::Cuda,
SyncMode::ProducerSynced,
MemoryClass::Unified,
)
.unwrap();
assert_eq!(validate_colocation(&[a, b]).unwrap(), DeviceTag::Cuda);
}
}
mod buffer_handle_sync_mode {
use super::*;
use crate::descriptor::TensorDescriptor;
use crate::layout::LayoutDescriptor;
use crate::{ElementType, Shape, MIN_BUFFER_ALIGNMENT};
#[test]
fn new_with_event_sync_non_cpu_buffer() {
let handle =
BufferHandle::new(512, MIN_BUFFER_ALIGNMENT, DeviceTag::Cuda, SyncMode::Event)
.unwrap();
assert_eq!(handle.sync_mode(), SyncMode::Event);
assert_eq!(handle.device_tag(), DeviceTag::Cuda);
}
#[test]
fn new_with_consumer_stream_sync_non_cpu_buffer() {
let handle = BufferHandle::new(
512,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Rocm,
SyncMode::ConsumerStream,
)
.unwrap();
assert_eq!(handle.sync_mode(), SyncMode::ConsumerStream);
assert_eq!(handle.device_tag(), DeviceTag::Rocm);
}
#[test]
fn new_cpu_rejects_event() {
let result =
BufferHandle::new(512, MIN_BUFFER_ALIGNMENT, DeviceTag::Cpu, SyncMode::Event);
assert!(matches!(result, Err(Error::InvalidSyncMode(0x01))));
}
#[test]
fn new_cpu_rejects_consumer_stream() {
let result = BufferHandle::new(
512,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ConsumerStream,
);
assert!(matches!(result, Err(Error::InvalidSyncMode(0x02))));
}
#[test]
fn new_cpu_allows_producer_synced() {
let result = BufferHandle::new(
512,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
);
assert!(result.is_ok());
}
#[test]
fn empty_has_producer_synced() {
assert_eq!(
BufferHandle::empty(DeviceTag::Cuda).sync_mode(),
SyncMode::ProducerSynced
);
}
#[test]
fn encode_decode_preserves_sync_mode_event() {
let shape = Shape::new(vec![4u64]).unwrap();
let buffer =
BufferHandle::new(64, MIN_BUFFER_ALIGNMENT, DeviceTag::Cuda, SyncMode::Event)
.unwrap();
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buffer],
None,
None,
None,
None,
)
.unwrap();
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded.buffers[0].sync_mode(), SyncMode::Event);
assert_eq!(decoded.buffers[0].device_tag(), DeviceTag::Cuda);
assert_eq!(decoded, desc);
}
#[test]
fn encode_decode_preserves_sync_mode_consumer_stream() {
let shape = Shape::new(vec![8u64]).unwrap();
let buffer = BufferHandle::new(
128,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Vulkan,
SyncMode::ConsumerStream,
)
.unwrap();
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buffer],
None,
None,
None,
None,
)
.unwrap();
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded.buffers[0].sync_mode(), SyncMode::ConsumerStream);
assert_eq!(decoded.buffers[0].device_tag(), DeviceTag::Vulkan);
assert_eq!(decoded, desc);
}
}
}