pub const MAGIC_V2: [u8; 4] = [0x41, 0x50, 0x52, 0x00];
pub const VERSION_V2: (u8, u8) = (2, 0);
pub const HEADER_SIZE_V2: usize = 64;
pub const ALIGNMENT: usize = 64;
pub const LZ4_BLOCK_SIZE: usize = 64 * 1024;
pub const MAX_METADATA_SIZE: usize = 16 * 1024 * 1024;
pub const MAX_TENSOR_NAME_LEN: usize = 256;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AprV2Flags(u16);
impl AprV2Flags {
pub const LZ4_COMPRESSED: u16 = 0b0000_0000_0000_0001;
pub const ZSTD_COMPRESSED: u16 = 0b0000_0000_0000_0010;
pub const ENCRYPTED: u16 = 0b0000_0000_0000_0100;
pub const SIGNED: u16 = 0b0000_0000_0000_1000;
pub const SHARDED: u16 = 0b0000_0000_0001_0000;
pub const QUANTIZED: u16 = 0b0000_0000_0010_0000;
pub const HAS_FILTERBANK: u16 = 0b0000_0000_0100_0000;
pub const HAS_MODEL_CARD: u16 = 0b0000_0000_1000_0000;
pub const STREAMING: u16 = 0b0000_0001_0000_0000;
pub const HAS_VOCAB: u16 = 0b0000_0010_0000_0000;
pub const LAYOUT_ROW_MAJOR: u16 = 0b0000_0100_0000_0000;
pub const LAYOUT_COLUMN_MAJOR: u16 = 0b0000_1000_0000_0000;
#[must_use]
pub const fn new() -> Self {
Self(0)
}
#[must_use]
pub const fn from_bits(bits: u16) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u16 {
self.0
}
#[must_use]
pub const fn contains(self, flag: u16) -> bool {
(self.0 & flag) == flag
}
#[must_use]
pub const fn with(self, flag: u16) -> Self {
Self(self.0 | flag)
}
#[must_use]
pub const fn without(self, flag: u16) -> Self {
Self(self.0 & !flag)
}
#[must_use]
pub const fn is_lz4_compressed(self) -> bool {
self.contains(Self::LZ4_COMPRESSED)
}
#[must_use]
pub const fn is_zstd_compressed(self) -> bool {
self.contains(Self::ZSTD_COMPRESSED)
}
#[must_use]
pub const fn is_encrypted(self) -> bool {
self.contains(Self::ENCRYPTED)
}
#[must_use]
pub const fn is_sharded(self) -> bool {
self.contains(Self::SHARDED)
}
#[must_use]
pub const fn is_quantized(self) -> bool {
self.contains(Self::QUANTIZED)
}
#[must_use]
pub const fn is_row_major(self) -> bool {
self.contains(Self::LAYOUT_ROW_MAJOR)
}
#[must_use]
pub const fn is_column_major(self) -> bool {
self.contains(Self::LAYOUT_COLUMN_MAJOR)
}
#[must_use]
pub const fn is_layout_valid(self) -> bool {
!self.is_column_major()
}
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct AprV2Header {
pub magic: [u8; 4],
pub version: (u8, u8),
pub flags: AprV2Flags,
pub tensor_count: u32,
pub metadata_offset: u64,
pub metadata_size: u32,
pub tensor_index_offset: u64,
pub data_offset: u64,
pub checksum: u32,
pub reserved: [u8; 20],
}
impl Default for AprV2Header {
fn default() -> Self {
Self::new()
}
}
mod header_impl;
mod reader_impl;
mod streaming_writer;
mod tensor_index_impl;
mod v2format_error;
mod writer;
pub use header_impl::{
AprV2Metadata, ChatSpecialTokens, QuantizationMetadata, ShardingMetadata, TensorIndexEntry,
};
pub use reader_impl::{AprV2Reader, AprV2ReaderRef, ShardInfo, ShardManifest};
pub use streaming_writer::AprV2StreamingWriter;
pub use tensor_index_impl::{align_64, align_up, is_aligned_64, padding_to_align, TensorDType};
pub use v2format_error::V2FormatError;
pub use writer::AprV2Writer;
pub mod stamp;
pub use stamp::{stamp_provenance_bytes, ProvenancePatch};
#[cfg(test)]
mod tests;