pub struct CompressiveSpectralEncoder { /* private fields */ }Expand description
Compressive Spectral Encoder - designed for actual storage compression.
Unlike SpectralEncoder which is optimized for progressive streaming (and expands storage),
CompressiveSpectralEncoder is optimized for storage compression:
- Truncation: Discards low-energy DCT coefficients (lossy but NN-friendly)
- Single essential storage: Essentials stored once, not replicated in every fragment
- Implicit ordering: Uses zigzag scan order, no index storage overhead
- Efficient packing: Values stored directly without indices
§Compression Pipeline
Input f32 → DCT → Sort by energy → Truncate to retention_ratio
→ Zigzag reorder → Pack values → Fragment distribution§Trade-offs vs SpectralEncoder
| Feature | SpectralEncoder | CompressiveSpectralEncoder |
|---|---|---|
| Storage | 5-13x expansion | 2-10x compression |
| Progressive | Any fragment works | Need essentials first |
| Fault tolerance | High redundancy | Lower redundancy |
| Use case | Streaming inference | Storage/transmission |
§Expected Compression Ratios
| retention_ratio | Compression | Quality Loss |
|---|---|---|
| 0.50 | ~1.6x | Minimal |
| 0.20 | ~3.5x | Low |
| 0.10 | ~5x | Moderate |
| 0.05 | ~8x | Noticeable |
§Combined with Quantization
For maximum compression, combine with 4-bit quantization:
- 4-bit quantization: 8x
- CompressiveSpectral (0.10): 5x
- Zstd (~2x on f32): 2x
- Total: 80x (405B → ~10 GB)
Implementations§
Source§impl CompressiveSpectralEncoder
impl CompressiveSpectralEncoder
Sourcepub fn new(num_fragments: u16, retention_ratio: f32) -> Self
pub fn new(num_fragments: u16, retention_ratio: f32) -> Self
Create encoder with specified fragments and retention ratio.
§Arguments
num_fragments- Number of output fragments (more = better progressive quality)retention_ratio- Fraction of DCT coefficients to keep (0.1-0.5 typical)
§Example
// Keep 10% of coefficients, distribute across 8 fragments
let encoder = CompressiveSpectralEncoder::new(8, 0.10);
let fragments = encoder.encode_2d(&weights, 4096, 4096)?;
// Expected ~5x compression ratioSourcepub fn with_essential_ratio(self, ratio: f32) -> Self
pub fn with_essential_ratio(self, ratio: f32) -> Self
Set ratio of retained coefficients considered essential.
When using num_fragments=1, set this to 1.0 to store all retained coefficients in the single fragment.
Sourcepub fn encode_2d(
&self,
data: &[f32],
width: usize,
height: usize,
) -> Result<Vec<HoloFragment>>
pub fn encode_2d( &self, data: &[f32], width: usize, height: usize, ) -> Result<Vec<HoloFragment>>
Encode 2D tensor with compression using V3 format (bitmap + f16 coefficients).
Returns fragments where:
- Fragment 0: Contains essential coefficients + bitmap marking retained positions
- Fragments 1..N: Contain distributed detail coefficients
§V3 Format (Bitmap + f16)
Fragment 0 layout:
[magic: u32][width: u32][height: u32][retain_count: u32][essential_count: u32]
[detail_per_frag: u32][bitmap_bytes...][essential_coeffs as f16...]Coefficients are stored as f16 (2 bytes) instead of f32 (4 bytes), halving the coefficient storage overhead while maintaining sufficient precision for neural network weights.
Sourcepub fn encode_2d_from_dct(
&self,
dct_coeffs: &[f32],
width: usize,
height: usize,
) -> Result<Vec<HoloFragment>>
pub fn encode_2d_from_dct( &self, dct_coeffs: &[f32], width: usize, height: usize, ) -> Result<Vec<HoloFragment>>
Encode from pre-computed DCT coefficients.
This is useful when DCT is computed externally (e.g., on GPU).
The dct_coeffs must already be in frequency domain.
Sourcepub fn expected_ratio(&self, input_elements: usize) -> f32
pub fn expected_ratio(&self, input_elements: usize) -> f32
Calculate expected compression ratio for given parameters (V3 bitmap + f16 format).
§Returns
The compression ratio (input_size / output_size). Higher is better.
With V3 format (bitmap + f16 coefficients):
- 20% retention: ~3.8x vs f16 original
- 10% retention: ~6x vs f16 original
§Example
let encoder = CompressiveSpectralEncoder::new(8, 0.10);
let ratio = encoder.expected_ratio(4096 * 4096);
println!("Expected {}x compression", ratio); // ~6x vs f16Sourcepub fn expected_ratio_vs_f16(&self, input_elements: usize) -> f32
pub fn expected_ratio_vs_f16(&self, input_elements: usize) -> f32
Calculate expected compression ratio vs f16 original.