pub mod composite_member;
pub(crate) mod cursor;
mod decode;
mod encode;
pub mod ext_type;
pub mod layout_codec;
pub mod shard;
pub mod statistics;
pub(crate) mod mod_types {
pub(crate) use super::{DescriptorFlags, TensorDescriptor, RESERVED_FLAGS_MASK};
}
pub use composite_member::{CompositeMemberDescriptor, MemberRole};
pub use ext_type::ExtensionTypeDescriptor;
pub use shard::ShardDescriptor;
pub use statistics::{Statistics, StatisticsMask};
pub const MAGIC: [u8; 4] = *b"HRRY";
pub const DESCRIPTOR_VERSION_MAJOR: u8 = 1;
pub const DESCRIPTOR_VERSION_MINOR: u8 = 0;
pub(crate) const MIN_DESCRIPTOR_LEN: u32 = 20;
pub(crate) const RESERVED_FLAGS_MASK: u32 = !0x1F;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DescriptorFlags(pub u32);
impl DescriptorFlags {
pub const HAS_QUANTIZATION: u32 = 1 << 0;
pub const HAS_SHARD: u32 = 1 << 1;
pub const HAS_EXTENSION_TYPE: u32 = 1 << 2;
pub const HAS_STATISTICS: u32 = 1 << 3;
pub const HAS_COMPOSITE_MEMBER: u32 = 1 << 4;
#[inline]
pub fn has_quantization(self) -> bool {
self.0 & Self::HAS_QUANTIZATION != 0
}
#[inline]
pub fn has_shard(self) -> bool {
self.0 & Self::HAS_SHARD != 0
}
#[inline]
pub fn has_extension_type(self) -> bool {
self.0 & Self::HAS_EXTENSION_TYPE != 0
}
#[inline]
pub fn has_statistics(self) -> bool {
self.0 & Self::HAS_STATISTICS != 0
}
#[inline]
pub fn has_composite_member(self) -> bool {
self.0 & Self::HAS_COMPOSITE_MEMBER != 0
}
}
use crate::descriptor::composite_member::CompositeMemberDescriptor as CompositeMemberDesc;
use crate::descriptor::ext_type::ExtensionTypeDescriptor as ExtDesc;
use crate::descriptor::shard::ShardDescriptor as ShardDesc;
use crate::descriptor::statistics::Statistics as Stats;
use crate::quantization::QuantizationDescriptor;
use crate::{BufferHandle, ElementType, Error, LayoutDescriptor, Result, Shape};
#[derive(Debug, Clone, PartialEq)]
pub struct TensorDescriptor {
pub version_major: u8,
pub version_minor: u8,
pub element_type: ElementType,
pub shape: Shape,
pub byte_offset: u64,
pub layout: LayoutDescriptor,
pub buffers: Vec<BufferHandle>,
pub quantization: Option<Vec<u8>>,
pub shard: Option<ShardDesc>,
pub statistics: Option<Stats>,
pub extension_type: Option<ExtDesc>,
pub composite_member: Option<CompositeMemberDesc>,
}
impl TensorDescriptor {
#[allow(clippy::too_many_arguments)]
pub fn new(
version_major: u8,
version_minor: u8,
element_type: ElementType,
shape: Shape,
byte_offset: u64,
layout: LayoutDescriptor,
buffers: Vec<BufferHandle>,
quantization: Option<Vec<u8>>,
shard: Option<ShardDesc>,
statistics: Option<Stats>,
extension_type: Option<ExtDesc>,
) -> Result<Self> {
let is_composite_head = matches!(&layout, LayoutDescriptor::Composite(_));
if is_composite_head {
if !buffers.is_empty() {
return Err(Error::CompositeHeadHasBuffers {
count: buffers.len().min(u8::MAX as usize) as u8,
});
}
if byte_offset != 0 {
return Err(Error::CompositeHeadHasByteOffset { byte_offset });
}
if quantization.is_some() {
return Err(Error::CompositeHeadHasQuantization);
}
} else if buffers.is_empty() {
return Err(Error::EmptyBufferTable);
}
if buffers.len() > 255 {
return Err(Error::InvalidLayout(format!(
"buffer_count {} exceeds the maximum of 255 (uint8 ceiling)",
buffers.len()
)));
}
let is_ext_tag = matches!(element_type.tag(), 0xF0..=0xFE);
let has_ext = extension_type.is_some();
if is_ext_tag != has_ext {
return Err(Error::ExtensionTypeFlagMismatch {
flag_set: has_ext,
type_tag: element_type.tag(),
type_tag_in_range: if is_ext_tag {
"in 0xF0-0xFE"
} else {
"not in 0xF0-0xFE"
},
});
}
if let Some(s) = &shard {
if s.parent_shape.len() != shape.rank() {
return Err(Error::InvalidShape(format!(
"shard.parent_shape.len() ({}) != shape.rank() ({})",
s.parent_shape.len(),
shape.rank()
)));
}
if matches!(&layout, LayoutDescriptor::BlockPaged(_)) {
return Err(Error::InvalidLayout(
"block-paged layout MUST NOT carry a shard descriptor (spec § Sharding)"
.to_string(),
));
}
if matches!(&layout, LayoutDescriptor::Csf(_)) {
return Err(Error::InvalidLayout(
"CSF layout MUST NOT carry a shard descriptor (spec § Sharding)".to_string(),
));
}
}
if let (LayoutDescriptor::BlockPaged(bp), Some(q)) = (&layout, &quantization) {
let (qd, _) = QuantizationDescriptor::decode(q)?;
let (axis, block_size) = match &qd {
QuantizationDescriptor::PerChannelAffine(x) => (x.axis(), 0u32),
QuantizationDescriptor::PerBlockAffine(x) => (x.axis(), x.block_size()),
QuantizationDescriptor::Nf4(x) => (x.axis(), x.block_size()),
QuantizationDescriptor::Mxfp(x) => (x.axis(), x.block_size()),
QuantizationDescriptor::PerTensorAffine(_) => (0u32, 0u32),
};
bp.validate_quantization_compatibility(qd.scheme_tag().tag(), axis, block_size)?;
}
Ok(Self {
version_major,
version_minor,
element_type,
shape,
byte_offset,
layout,
buffers,
quantization,
shard,
statistics,
extension_type,
composite_member: None,
})
}
pub fn with_composite_member(mut self, cm: CompositeMemberDesc) -> Self {
self.composite_member = Some(cm);
self
}
pub fn flags(&self) -> DescriptorFlags {
let mut flags = 0u32;
if self.quantization.is_some() {
flags |= DescriptorFlags::HAS_QUANTIZATION;
}
if self.shard.is_some() {
flags |= DescriptorFlags::HAS_SHARD;
}
if self.extension_type.is_some() {
flags |= DescriptorFlags::HAS_EXTENSION_TYPE;
}
if self.statistics.is_some() {
flags |= DescriptorFlags::HAS_STATISTICS;
}
if self.composite_member.is_some() {
flags |= DescriptorFlags::HAS_COMPOSITE_MEMBER;
}
DescriptorFlags(flags)
}
pub fn encode(&self) -> Result<Vec<u8>> {
encode::encode(self)
}
pub fn decode(bytes: &[u8]) -> Result<Self> {
decode::decode(bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::LayoutDescriptor;
use crate::{BufferHandle, DeviceTag, ElementType, Shape, SyncMode, MIN_BUFFER_ALIGNMENT};
fn worked_example() -> TensorDescriptor {
let shape = Shape::new(vec![3u64, 4]).unwrap();
let buffer = BufferHandle::new(
192,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buffer],
None,
None,
None,
None,
)
.unwrap()
}
#[rustfmt::skip]
const WORKED_EXAMPLE_BYTES: [u8; 61] = [
0x48, 0x52, 0x52, 0x59,
0x01, 0x00,
0x3D, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x03,
0x01,
0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01,
0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00,
0x00,
0x00,
0x00, 0x00,
];
#[test]
fn worked_example_encode_exact_bytes() {
let desc = worked_example();
let encoded = desc.encode().unwrap();
assert_eq!(
encoded.len(),
61,
"expected 61 bytes per spec worked example, got {}",
encoded.len()
);
assert_eq!(
encoded.as_slice(),
&WORKED_EXAMPLE_BYTES,
"encoded bytes do not match spec worked example"
);
}
#[test]
fn worked_example_decode_round_trip() {
let desc = worked_example();
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn worked_example_decode_from_spec_bytes() {
let decoded = TensorDescriptor::decode(&WORKED_EXAMPLE_BYTES).unwrap();
assert_eq!(decoded, worked_example());
}
#[test]
fn decode_bad_magic() {
let mut bytes = WORKED_EXAMPLE_BYTES;
bytes[0] = 0xFF;
let err = TensorDescriptor::decode(&bytes).unwrap_err();
assert!(matches!(err, Error::InvalidMagic { .. }));
}
#[test]
fn decode_version_major_too_high() {
let mut bytes = WORKED_EXAMPLE_BYTES;
bytes[4] = 2; let err = TensorDescriptor::decode(&bytes).unwrap_err();
assert!(matches!(
err,
Error::UnsupportedDescriptorVersion { major: 2, .. }
));
}
#[test]
fn decode_descriptor_too_short() {
let mut bytes = WORKED_EXAMPLE_BYTES;
bytes[6] = 10;
bytes[7] = 0;
bytes[8] = 0;
bytes[9] = 0;
let err = TensorDescriptor::decode(&bytes).unwrap_err();
assert!(matches!(err, Error::DescriptorTooShort { length: 10 }));
}
#[test]
fn decode_reserved_flag_bit_set() {
let mut bytes = WORKED_EXAMPLE_BYTES;
bytes[10] = 0x20;
let err = TensorDescriptor::decode(&bytes).unwrap_err();
assert!(matches!(err, Error::ReservedDescriptorFlagBitsSet { .. }));
}
#[test]
fn decode_empty_buffer_table() {
let desc = worked_example();
let mut bytes = desc.encode().unwrap();
bytes[44] = 0;
let err = TensorDescriptor::decode(&bytes).unwrap_err();
assert!(matches!(err, Error::EmptyBufferTable));
}
#[test]
fn decode_truncated() {
let bytes = &[0x48u8, 0x52, 0x52];
let err = TensorDescriptor::decode(bytes).unwrap_err();
assert!(matches!(err, Error::DescriptorTruncated { .. }));
}
#[test]
fn flags_none_set() {
assert_eq!(worked_example().flags().0, 0);
}
#[test]
fn flags_quantization_set() {
let shape = Shape::new(vec![4u64]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let desc = TensorDescriptor::new(
1,
0,
ElementType::Int8,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buf],
Some(vec![0x01, 0x00, 0x00, 0x00]),
None,
None,
None,
)
.unwrap();
assert!(desc.flags().has_quantization());
assert!(!desc.flags().has_shard());
}
#[test]
fn new_rejects_empty_buffers() {
let shape = Shape::new(vec![4u64]).unwrap();
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![],
None,
None,
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::EmptyBufferTable));
}
#[test]
fn new_rejects_shard_rank_mismatch() {
let shape = Shape::new(vec![4u64, 4]).unwrap(); let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let shard = ShardDescriptor::new(vec![10u64], vec![0u64]).unwrap();
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buf],
None,
Some(shard),
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::InvalidShape(_)));
}
#[test]
fn new_rejects_block_paged_with_shard() {
use crate::layout::{BlockPagedLayout, BlockTableIndexType, KvRole};
let shape = Shape::new(vec![6u64, 2, 8]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let shard = ShardDescriptor::new(vec![12u64, 2, 8], vec![0u64, 0, 0]).unwrap();
let layout = LayoutDescriptor::BlockPaged(BlockPagedLayout::new(
4,
5,
0,
2,
KvRole::Key,
Some(3),
BlockTableIndexType::U32,
));
let err = TensorDescriptor::new(
1,
0,
ElementType::Float16,
shape,
0,
layout,
vec![buf],
None,
Some(shard),
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::InvalidLayout(_)));
}
#[test]
fn new_rejects_block_paged_with_incompatible_quantization() {
use crate::layout::{BlockPagedLayout, BlockTableIndexType, KvRole};
use crate::quantization::PerChannelAffine;
let quant = QuantizationDescriptor::PerChannelAffine(
PerChannelAffine::new_symmetric(0, 1).unwrap(),
)
.encode_to_vec();
let shape = Shape::new(vec![6u64, 2, 8]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let layout = LayoutDescriptor::BlockPaged(BlockPagedLayout::new(
4,
5,
0,
2,
KvRole::Key,
Some(3),
BlockTableIndexType::U32,
));
let err = TensorDescriptor::new(
1,
0,
ElementType::Float16,
shape,
0,
layout,
vec![buf],
Some(quant),
None,
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::InvalidQuantization(_)));
}
#[test]
fn new_accepts_block_paged_with_compatible_quantization() {
use crate::layout::{BlockPagedLayout, BlockTableIndexType, KvRole};
use crate::quantization::PerChannelAffine;
let quant = QuantizationDescriptor::PerChannelAffine(
PerChannelAffine::new_symmetric(1, 1).unwrap(),
)
.encode_to_vec();
let shape = Shape::new(vec![6u64, 2, 8]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let layout = LayoutDescriptor::BlockPaged(BlockPagedLayout::new(
4,
5,
0,
2,
KvRole::Key,
Some(3),
BlockTableIndexType::U32,
));
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float16,
shape,
0,
layout,
vec![buf],
Some(quant),
None,
None,
None,
);
assert!(desc.is_ok());
}
#[test]
fn new_rejects_csf_with_shard() {
use crate::layout::CsfLayout;
let shape = Shape::new(vec![2u64, 3, 4]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let shard = ShardDescriptor::new(vec![4u64, 3, 4], vec![0u64, 0, 0]).unwrap();
let layout = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
layout,
vec![buf],
None,
Some(shard),
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::InvalidLayout(_)));
}
#[test]
fn round_trip_with_shard() {
use crate::layout::LayoutDescriptor;
let shape = Shape::new(vec![4u64, 8]).unwrap();
let buf = BufferHandle::new(
128,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let shard = ShardDescriptor::new(vec![10u64, 20], vec![0u64, 4]).unwrap();
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buf],
None,
Some(shard),
None,
None,
)
.unwrap();
let decoded = TensorDescriptor::decode(&desc.encode().unwrap()).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn round_trip_with_quantization_payload() {
let shape = Shape::new(vec![8u64]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let quant_bytes = vec![0x01u8, 0x00, 0x00, 0x00, 0xAB, 0xCD];
let desc = TensorDescriptor::new(
1,
0,
ElementType::Int8,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buf],
Some(quant_bytes),
None,
None,
None,
)
.unwrap();
let decoded = TensorDescriptor::decode(&desc.encode().unwrap()).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn round_trip_with_statistics() {
use crate::descriptor::statistics::{Statistics, StatisticsMask};
let shape = Shape::new(vec![16u64]).unwrap();
let buf = BufferHandle::new(
128,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let stats = Statistics {
computed_mask: StatisticsMask(StatisticsMask::NNZ_VALID),
nnz: 10,
sparsity_ratio: 0.0,
value_min: 0.0,
value_max: 0.0,
value_abs_max: 0.0,
value_mean: 0.0,
value_stddev: 0.0,
nm_n: 0,
nm_m: 0,
has_nan: false,
has_inf: false,
};
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
vec![buf],
None,
None,
Some(stats),
None,
)
.unwrap();
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded.statistics.as_ref().unwrap().nnz, 10);
assert!(decoded.flags().has_statistics());
}
#[test]
fn round_trip_strided_layout() {
use crate::layout::StridedLayout;
let shape = Shape::new(vec![3u64, 4]).unwrap();
let buf = BufferHandle::new(
192,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let layout = LayoutDescriptor::Strided(StridedLayout::new(vec![8i64, 1]));
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
layout,
vec![buf],
None,
None,
None,
None,
)
.unwrap();
let decoded = TensorDescriptor::decode(&desc.encode().unwrap()).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn round_trip_coo_layout() {
use crate::layout::CooLayout;
let shape = Shape::new(vec![10u64, 10]).unwrap();
let buf0 = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let buf1 = BufferHandle::new(
128,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let layout = LayoutDescriptor::Coo(CooLayout::new(5, true));
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
layout,
vec![buf0, buf1],
None,
None,
None,
None,
)
.unwrap();
let decoded = TensorDescriptor::decode(&desc.encode().unwrap()).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn round_trip_extension_element_type() {
use crate::descriptor::ExtensionTypeDescriptor;
let shape = Shape::new(vec![4u64]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let ext =
ExtensionTypeDescriptor::new(8, 1, false, true, 0, 0, 0, 0, false, false).unwrap();
let desc = TensorDescriptor::new(
1,
0,
ElementType::Extension(0xF1),
shape,
0,
LayoutDescriptor::RowMajor,
vec![buf],
None,
None,
None,
Some(ext.clone()),
)
.unwrap();
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded.element_type, ElementType::Extension(0xF1));
assert_eq!(decoded.extension_type.as_ref().unwrap().bit_width, 8);
assert_eq!(decoded, desc);
}
#[test]
fn new_rejects_buffer_count_exceeding_255() {
let shape = Shape::new(vec![4u64]).unwrap();
let buffers: Vec<BufferHandle> = (0..256)
.map(|_| {
BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap()
})
.collect();
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
LayoutDescriptor::RowMajor,
buffers,
None,
None,
None,
None,
)
.unwrap_err();
assert!(
matches!(err, Error::InvalidLayout(_)),
"expected InvalidLayout, got {err:?}"
);
}
#[test]
fn decode_descriptor_length_mismatch() {
let desc = worked_example();
let mut bytes = desc.encode().unwrap();
let declared = u32::from_le_bytes([bytes[6], bytes[7], bytes[8], bytes[9]]);
let patched = declared + 1;
bytes[6..10].copy_from_slice(&patched.to_le_bytes());
let err = TensorDescriptor::decode(&bytes).unwrap_err();
assert!(
matches!(err, Error::DescriptorLengthMismatch { .. }),
"expected DescriptorLengthMismatch, got {err:?}"
);
}
#[test]
fn decode_tiled_nesting_too_deep() {
let mut layout_bytes: Vec<u8> = Vec::new();
for level in 0..9u32 {
layout_bytes.extend_from_slice(&2u64.to_le_bytes());
layout_bytes.push(0x01);
if level < 8 {
layout_bytes.push(0x04); } else {
layout_bytes.push(0x01); }
layout_bytes.extend_from_slice(&[0x00, 0x00]);
}
use crate::layout::TiledLayout;
let shape = Shape::new(vec![2u64]).unwrap();
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let shallow = LayoutDescriptor::Tiled(Box::new(
TiledLayout::new(vec![2u64], 0x01, 0x01, None, None, None).unwrap(),
));
let desc = TensorDescriptor::new(
1,
0,
ElementType::Float32,
shape,
0,
shallow,
vec![buf],
None,
None,
None,
None,
)
.unwrap();
let mut encoded = desc.encode().unwrap();
let layout_start = 36usize;
let shallow_layout_len = 12usize;
let after_layout = layout_start + shallow_layout_len;
let rest = encoded[after_layout..].to_vec();
encoded.truncate(layout_start);
encoded.extend_from_slice(&layout_bytes);
encoded.extend_from_slice(&rest);
let new_len = encoded.len() as u32;
encoded[6..10].copy_from_slice(&new_len.to_le_bytes());
let err = TensorDescriptor::decode(&encoded).unwrap_err();
assert!(
matches!(err, Error::SubpavingNestingTooDeep),
"expected SubpavingNestingTooDeep, got {err:?}"
);
}
fn composite_head(member_count: u32) -> TensorDescriptor {
use crate::layout::{CompositeLayout, CompositionRule};
let layout = LayoutDescriptor::Composite(
CompositeLayout::new(CompositionRule::Group, member_count).unwrap(),
);
TensorDescriptor::new(
1,
0,
ElementType::Float32,
Shape::new(vec![4u64]).unwrap(),
0,
layout,
vec![],
None,
None,
None,
None,
)
.unwrap()
}
#[test]
fn composite_head_empty_buffers_round_trips() {
let desc = composite_head(0);
let decoded = TensorDescriptor::decode(&desc.encode().unwrap()).unwrap();
assert_eq!(decoded, desc);
assert!(decoded.buffers.is_empty());
}
#[test]
fn composite_head_nonzero_buffer_count_rejected() {
use crate::layout::{CompositeLayout, CompositionRule};
let buf = BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap();
let layout =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
Shape::new(vec![4u64]).unwrap(),
0,
layout,
vec![buf],
None,
None,
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::CompositeHeadHasBuffers { count: 1 }));
}
#[test]
fn composite_head_nonzero_byte_offset_rejected() {
use crate::layout::{CompositeLayout, CompositionRule};
let layout =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
Shape::new(vec![4u64]).unwrap(),
8, layout,
vec![],
None,
None,
None,
None,
)
.unwrap_err();
assert!(matches!(
err,
Error::CompositeHeadHasByteOffset { byte_offset: 8 }
));
}
#[test]
fn composite_head_with_quantization_rejected() {
use crate::layout::{CompositeLayout, CompositionRule};
let layout =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
let err = TensorDescriptor::new(
1,
0,
ElementType::Float32,
Shape::new(vec![4u64]).unwrap(),
0,
layout,
vec![],
Some(vec![0x01, 0x00, 0x00, 0x00]),
None,
None,
None,
)
.unwrap_err();
assert!(matches!(err, Error::CompositeHeadHasQuantization));
}
#[test]
fn composite_head_with_composite_member_rejected_at_encode() {
use crate::descriptor::composite_member::CompositeMemberDescriptor;
let desc = composite_head(0).with_composite_member(CompositeMemberDescriptor::new(
crate::descriptor::MemberRole::Base,
));
let err = desc.encode().unwrap_err();
assert!(matches!(err, Error::InvalidLayout(_)));
}
#[test]
fn composite_head_with_composite_member_rejected_at_decode() {
let desc = composite_head(0);
let mut encoded = desc.encode().unwrap();
encoded[10] |= DescriptorFlags::HAS_COMPOSITE_MEMBER as u8;
let mut cm_bytes = vec![0x00u8]; cm_bytes.extend(std::iter::repeat_n(0u8, 15)); encoded.extend_from_slice(&cm_bytes);
let new_len = encoded.len() as u32;
encoded[6..10].copy_from_slice(&new_len.to_le_bytes());
let err = TensorDescriptor::decode(&encoded).unwrap_err();
assert!(matches!(err, Error::InvalidLayout(_)));
}
#[test]
fn with_composite_member_round_trips() {
use crate::descriptor::composite_member::CompositeMemberDescriptor;
let desc = worked_example().with_composite_member(CompositeMemberDescriptor::new(
crate::descriptor::MemberRole::Base,
));
assert!(desc.flags().has_composite_member());
let decoded = TensorDescriptor::decode(&desc.encode().unwrap()).unwrap();
assert_eq!(decoded, desc);
assert_eq!(
decoded.composite_member.unwrap().member_role,
crate::descriptor::MemberRole::Base
);
}
}