use crate::descriptor::composite_member::CompositeMemberDescriptor;
use crate::descriptor::cursor::ByteCursor;
use crate::descriptor::ext_type::ExtensionTypeDescriptor;
use crate::descriptor::layout_codec::decode_layout_payload;
use crate::descriptor::mod_types::{DescriptorFlags, TensorDescriptor, RESERVED_FLAGS_MASK};
use crate::descriptor::shard::ShardDescriptor;
use crate::descriptor::statistics::Statistics;
use crate::descriptor::{DESCRIPTOR_VERSION_MAJOR, MIN_DESCRIPTOR_LEN};
use crate::{
BufferHandle, DeviceTag, ElementType, Error, LayoutDescriptor, MemoryClass, Result, Shape,
SyncMode,
};
pub(crate) fn decode(bytes: &[u8]) -> Result<TensorDescriptor> {
if bytes.len() < 10 {
return Err(Error::DescriptorTruncated {
offset: 0,
needed: 10,
available: bytes.len(),
});
}
let magic: [u8; 4] = [bytes[0], bytes[1], bytes[2], bytes[3]];
if magic != crate::descriptor::MAGIC {
return Err(Error::InvalidMagic { got: magic });
}
let version_major = bytes[4];
let version_minor = bytes[5];
if version_major > DESCRIPTOR_VERSION_MAJOR {
return Err(Error::UnsupportedDescriptorVersion {
major: version_major,
minor: version_minor,
});
}
let descriptor_length = u32::from_le_bytes([bytes[6], bytes[7], bytes[8], bytes[9]]);
if descriptor_length < MIN_DESCRIPTOR_LEN {
return Err(Error::DescriptorTooShort {
length: descriptor_length,
});
}
let limit = (descriptor_length as usize).min(bytes.len());
let mut cursor = ByteCursor::new(bytes, limit);
cursor.read_bytes(10)?;
let flags_raw = cursor.read_u32_le()?;
if flags_raw & RESERVED_FLAGS_MASK != 0 {
return Err(Error::ReservedDescriptorFlagBitsSet {
flags: flags_raw,
mask: RESERVED_FLAGS_MASK,
});
}
let flags = DescriptorFlags(flags_raw);
let type_tag = cursor.read_u8()?;
let element_type = ElementType::from_tag(type_tag)?;
let layout_tag = cursor.read_u8()?;
let rank = cursor.read_u32_le()?;
if rank as usize > crate::MAX_RANK {
return Err(Error::RankExceedsMaximum {
rank,
max: crate::MAX_RANK as u32,
});
}
let mut dims = Vec::with_capacity(rank as usize);
for _ in 0..rank {
dims.push(cursor.read_u64_le()?);
}
let shape = Shape::new(dims)?;
let byte_offset = cursor.read_u64_le()?;
let layout = decode_layout_payload(layout_tag, rank, &mut cursor, 0)?;
let is_extension_tag = matches!(element_type, ElementType::Extension(_));
if is_extension_tag != flags.has_extension_type() {
return Err(Error::ExtensionTypeFlagMismatch {
flag_set: flags.has_extension_type(),
type_tag,
type_tag_in_range: if is_extension_tag {
"in 0xF0-0xFE"
} else {
"not in 0xF0-0xFE"
},
});
}
let buffer_count = cursor.read_u8()?;
let is_composite_head = matches!(layout, LayoutDescriptor::Composite(_));
if buffer_count == 0 && !is_composite_head {
return Err(Error::EmptyBufferTable);
}
let mut buffers = Vec::with_capacity(buffer_count as usize);
for _ in 0..buffer_count {
let byte_size = cursor.read_u64_le()?;
let alignment = cursor.read_u32_le()?;
let device_tag_byte = cursor.read_u8()?;
let sync_mode_byte = cursor.read_u8()?;
let memory_class_byte = cursor.read_u8()?;
let reserved = cursor.read_u8()?;
if reserved != 0 {
return Err(Error::ReservedBytesNonZero {
field: "buffer_handle._reserved",
});
}
let device_tag = DeviceTag::from_byte(device_tag_byte)?;
let sync_mode = SyncMode::from_byte(sync_mode_byte)?;
let memory_class = MemoryClass::from_byte(memory_class_byte)?;
let handle = BufferHandle::with_memory_class(
byte_size,
alignment,
device_tag,
sync_mode,
memory_class,
)?;
buffers.push(handle);
}
let quantization = if flags.has_quantization() {
let quant_len = cursor.read_u32_le()?;
let quant_bytes = cursor.read_bytes(quant_len as usize)?.to_vec();
Some(quant_bytes)
} else {
None
};
let shard = if flags.has_shard() {
let s = ShardDescriptor::decode_from(&mut cursor, rank)?;
s.validate_against_shape(&shape)?;
Some(s)
} else {
None
};
let statistics = if flags.has_statistics() {
Some(Statistics::decode_from(&mut cursor)?)
} else {
None
};
let extension_type = if flags.has_extension_type() {
Some(ExtensionTypeDescriptor::decode_from(&mut cursor)?)
} else {
None
};
let composite_member = if flags.has_composite_member() {
Some(CompositeMemberDescriptor::decode_from(&mut cursor)?)
} else {
None
};
if version_minor == crate::descriptor::DESCRIPTOR_VERSION_MINOR {
let consumed = cursor.pos();
if consumed != descriptor_length as usize {
return Err(Error::DescriptorLengthMismatch {
declared: descriptor_length,
actual: consumed,
});
}
}
let is_composite_head = matches!(layout, LayoutDescriptor::Composite(_));
let descriptor = TensorDescriptor::new(
version_major,
version_minor,
element_type,
shape,
byte_offset,
layout,
buffers,
quantization,
shard,
statistics,
extension_type,
)?;
match composite_member {
Some(_) if is_composite_head => Err(Error::InvalidLayout(
"composite head MUST NOT carry a Composite Member section (HAS_COMPOSITE_MEMBER)"
.to_string(),
)),
Some(cm) => Ok(descriptor.with_composite_member(cm)),
None => Ok(descriptor),
}
}