Compressor

Struct Compressor 

Source
pub struct Compressor {
    pub model: Arc<Model>,
    pub state: State,
    pub scratch: ScratchBuffers,
    pub pdf_buffer: Vec<f64>,
    pub cdf_buffer_ac: Vec<u32>,
    pub cdf_buffer_rans: Vec<u32>,
    pub rans_freq_buffer: Vec<i64>,
}
Expand description

Main compressor/decompressor that combines RWKV7 inference with entropy coding.

The compressor maintains internal state and pre-allocated buffers to minimize allocations during the compression/decompression hot path.

Fields§

§model: Arc<Model>

RWKV7 model for generating probability distributions.

§state: State

Model state (recurrent hidden states).

§scratch: ScratchBuffers§pdf_buffer: Vec<f64>

Pre-allocated PDF buffer (eliminates allocations in compression loop).

§cdf_buffer_ac: Vec<u32>

Reusable AC CDF buffer (vocab_size + 1 entries).

§cdf_buffer_rans: Vec<u32>

Reusable rANS CDF buffer (vocab_size + 1 entries).

§rans_freq_buffer: Vec<i64>

Scratch frequencies for rANS quantization.

Implementations§

Source§

impl Compressor

Source

pub fn new<P: AsRef<Path>>(model_path: P) -> Result<Self>

Create a new compressor with the given model.

§Arguments
  • model_path - Path to RWKV7 model weights (.safetensors format)
§Returns

A new Compressor ready for compression/decompression operations.

Source

pub fn load_model<P: AsRef<Path>>(model_path: P) -> Result<Arc<Model>>

Source

pub fn new_from_model(model: Arc<Model>) -> Self

Source

pub fn reset(&mut self)

Reset the model state to initial values.

Call this between independent compression/decompression operations to ensure a clean state.

Source

pub fn vocab_size(&self) -> usize

Get the vocabulary size (should always be 256 for byte-level).

Source

pub fn compress(&mut self, data: &[u8], coder: CoderType) -> Result<Vec<u8>>

Compress data using the specified entropy coder.

§Arguments
  • data - Raw bytes to compress
  • coder - Entropy coder to use (AC or rANS)
§Returns

Compressed data including header with checksum.

Source

pub fn compress_into<W: Write>( &mut self, data: &[u8], coder: CoderType, w: &mut W, ) -> Result<()>

Source

pub fn compress_chain_into<W: Write>( &mut self, parts: &[&[u8]], coder: CoderType, w: &mut W, ) -> Result<()>

Source

pub fn compress_size(&mut self, data: &[u8], coder: CoderType) -> Result<u64>

Source

pub fn compress_size_chain( &mut self, parts: &[&[u8]], coder: CoderType, ) -> Result<u64>

Source

pub fn decompress(&mut self, data: &[u8]) -> Result<Vec<u8>>

Decompress data.

§Arguments
  • data - Compressed data (must include header)
§Returns

Original decompressed data. Returns error if checksum doesn’t match.

Source

pub fn cross_entropy(&mut self, data: &[u8]) -> Result<f64>

Calculate cross-entropy (bits per byte) for data without compression.

This measures how well the model predicts the data, giving a theoretical lower bound on achievable compression. Useful for evaluating model quality.

§Arguments
  • data - Data to analyze
§Returns

Average bits per byte (lower is better, 8.0 means no compression possible).

Source

pub fn cross_entropy_conditional_chain( &mut self, prefix_parts: &[&[u8]], data: &[u8], ) -> Result<f64>

Source

pub fn cross_entropy_conditional( &mut self, prefix: &[u8], data: &[u8], ) -> Result<f64>

Source

pub fn joint_cross_entropy_aligned_min( &mut self, x: &[u8], y: &[u8], ) -> Result<f64>

Trait Implementations§

Source§

impl Clone for Compressor

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V