Skip to main content

CompressiveSpectralEncoder

Struct CompressiveSpectralEncoder 

Source
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:

  1. Truncation: Discards low-energy DCT coefficients (lossy but NN-friendly)
  2. Single essential storage: Essentials stored once, not replicated in every fragment
  3. Implicit ordering: Uses zigzag scan order, no index storage overhead
  4. 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

FeatureSpectralEncoderCompressiveSpectralEncoder
Storage5-13x expansion2-10x compression
ProgressiveAny fragment worksNeed essentials first
Fault toleranceHigh redundancyLower redundancy
Use caseStreaming inferenceStorage/transmission

§Expected Compression Ratios

retention_ratioCompressionQuality Loss
0.50~1.6xMinimal
0.20~3.5xLow
0.10~5xModerate
0.05~8xNoticeable

§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

Source

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 ratio
Source

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.

Source

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.

Source

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.

Source

pub fn encode_1d(&self, data: &[f32]) -> Result<Vec<HoloFragment>>

Encode 1D tensor.

Source

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 f16
Source

pub fn expected_ratio_vs_f16(&self, input_elements: usize) -> f32

Calculate expected compression ratio vs f16 original.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.