Skip to main content

validate_colocation

Function validate_colocation 

Source
pub fn validate_colocation(handles: &[BufferHandle]) -> Result<DeviceTag>
Expand description

Checks that all buffer handles in handles share the same DeviceTag and MemoryClass.

All buffers referenced by a single tensor descriptor — the data buffer plus all quantization-parameter buffers — MUST share the same device_tag AND the same memory_class (see docs/spec/buffer-protocol.md § Device Colocation).

Returns the common DeviceTag on success.

§Errors

§Examples

use hurray_core::{BufferHandle, DeviceTag, Error, MemoryClass, SyncMode, validate_colocation};

// All handles on CPU, all Standard — succeeds.
let handles = [
    BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
    BufferHandle::new(256, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
];
assert_eq!(validate_colocation(&handles).unwrap(), DeviceTag::Cpu);

// Empty slice — error.
assert!(matches!(validate_colocation(&[]), Err(Error::EmptyBufferList)));

// Mixed devices — error.
let mixed_device = [
    BufferHandle::new(1024, 64, DeviceTag::Cpu, SyncMode::ProducerSynced).unwrap(),
    BufferHandle::new(256, 64, DeviceTag::Cuda, SyncMode::ProducerSynced).unwrap(),
];
assert!(matches!(
    validate_colocation(&mixed_device),
    Err(Error::DeviceTagMismatch { expected: 0x00, found: 0x01 })
));

// Mixed memory classes — error.
let mixed_class = [
    BufferHandle::new(1024, 64, DeviceTag::Cuda, SyncMode::ProducerSynced).unwrap(),
    BufferHandle::with_memory_class(256, 64, DeviceTag::Cuda, SyncMode::ProducerSynced, MemoryClass::Unified).unwrap(),
];
assert!(matches!(
    validate_colocation(&mixed_class),
    Err(Error::MemoryClassMismatch { expected: 0x00, found: 0x02 })
));