pub struct AprV2Writer { /* private fields */ }Expand description
APR v2 format writer
Implementations§
Source§impl AprV2Writer
impl AprV2Writer
Sourcepub fn new(metadata: AprV2Metadata) -> Self
pub fn new(metadata: AprV2Metadata) -> Self
Create new writer
LAYOUT-002: All new APR files are created with LAYOUT_ROW_MAJOR flag set. This ensures realizar can safely assume row-major layout for all tensors.
Sourcepub fn add_tensor(
&mut self,
name: impl Into<String>,
dtype: TensorDType,
shape: Vec<usize>,
data: Vec<u8>,
)
pub fn add_tensor( &mut self, name: impl Into<String>, dtype: TensorDType, shape: Vec<usize>, data: Vec<u8>, )
Add tensor to the file
Sourcepub fn add_f32_tensor(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
data: &[f32],
)
pub fn add_f32_tensor( &mut self, name: impl Into<String>, shape: Vec<usize>, data: &[f32], )
Add f32 tensor
Sourcepub fn add_tensor_f32_owned(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
data: Vec<f32>,
)
pub fn add_tensor_f32_owned( &mut self, name: impl Into<String>, shape: Vec<usize>, data: Vec<f32>, )
ALB-099: Add f32 tensor from owned Vec — pre-allocated byte conversion. Uses capacity-hinted Vec + extend_from_slice instead of flat_map + collect.
Sourcepub fn add_f16_tensor(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
data: &[f32],
)
pub fn add_f16_tensor( &mut self, name: impl Into<String>, shape: Vec<usize>, data: &[f32], )
Add f16 tensor (converts f32 → f16, 2 bytes per value)
This provides true 2x compression over f32 storage with minimal precision loss for inference workloads. Uses IEEE 754 half-precision format.
Sourcepub fn add_q8_tensor(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
data: &[f32],
)
pub fn add_q8_tensor( &mut self, name: impl Into<String>, shape: Vec<usize>, data: &[f32], )
Add Q8 tensor (8-bit symmetric quantization)
Format: [scale: f32 (4 bytes)] + [quantized: i8 × n] Total size: 4 + n bytes (vs 4n for f32) Compression ratio: ~4x
Sourcepub fn add_q4_tensor(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
data: &[f32],
)
pub fn add_q4_tensor( &mut self, name: impl Into<String>, shape: Vec<usize>, data: &[f32], )
Add Q4 tensor (4-bit symmetric quantization, block-wise)
Format: For each block of 32 values: [block_scale: f16 (2 bytes)] + [packed nibbles: 16 bytes]
Total size per block: 18 bytes (vs 128 bytes for f32) Compression ratio: ~7x
Sourcepub fn add_q4k_raw_tensor(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
raw_data: Vec<u8>,
)
pub fn add_q4k_raw_tensor( &mut self, name: impl Into<String>, shape: Vec<usize>, raw_data: Vec<u8>, )
Add raw Q4_K tensor (GGUF-compatible super-block format)
This stores GGUF Q4_K data directly without re-quantization. Q4_K format: 256-element super-blocks with nested 32-element sub-blocks Each super-block: d (f16, 2B) + dmin (f16, 2B) + scales (12B) + qs (128B) = 144 bytes Effective bits per weight: ~4.5
Use this when importing from GGUF to preserve exact quantization.
Sourcepub fn add_q6k_raw_tensor(
&mut self,
name: impl Into<String>,
shape: Vec<usize>,
raw_data: Vec<u8>,
)
pub fn add_q6k_raw_tensor( &mut self, name: impl Into<String>, shape: Vec<usize>, raw_data: Vec<u8>, )
Add raw Q6_K tensor (GGUF-compatible super-block format)
This stores GGUF Q6_K data directly without re-quantization. Q6_K format: 256-element super-blocks Each super-block: ql (128B) + qh (64B) + scales (16B) + d (f16, 2B) = 210 bytes Effective bits per weight: ~6.5
Sourcepub fn with_lz4_compression(&mut self) -> &mut Self
pub fn with_lz4_compression(&mut self) -> &mut Self
Set LZ4 compression flag
Sourcepub fn set_header_flags(&mut self, flags: AprV2Flags)
pub fn set_header_flags(&mut self, flags: AprV2Flags)
Preserve header flags from an existing APR file on round-trip.
Used by stamp_provenance_bytes so that QUANTIZED / HAS_VOCAB /
HAS_MODEL_CARD / etc. set on the input are carried across — without
this, AprV2Writer::new() would emit an output with only
LAYOUT_ROW_MAJOR set and downstream consumers that branch on
QUANTIZED would silently see a different file. LAYOUT_ROW_MAJOR
is always included regardless of the passed-in value so the
LAYOUT-002 jidoka guard never disengages.
Sourcepub fn with_sharding(
&mut self,
shard_count: usize,
shard_index: usize,
) -> &mut Self
pub fn with_sharding( &mut self, shard_count: usize, shard_index: usize, ) -> &mut Self
Set sharding info
Sourcepub fn write_into(self, path: impl AsRef<Path>) -> Result<(), V2FormatError>
pub fn write_into(self, path: impl AsRef<Path>) -> Result<(), V2FormatError>
ALB-099: Write directly to a file path — consumes writer.
§Errors
Returns error if file creation or write fails.