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 ac_freq_buffer: Vec<i64>,
pub cdf_buffer_rans: Vec<u32>,
pub rans_freq_buffer: Vec<i64>,
/* private fields */
}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: StateModel state (recurrent hidden states).
scratch: ScratchBuffersScratch buffers for model forward passes.
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).
ac_freq_buffer: Vec<i64>Scratch frequencies for AC quantization.
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
impl Compressor
Sourcepub fn load_model<P: AsRef<Path>>(model_path: P) -> Result<Arc<Model>>
pub fn load_model<P: AsRef<Path>>(model_path: P) -> Result<Arc<Model>>
Load a model from disk and wrap it in Arc.
Sourcepub fn new_from_model(model: Arc<Model>) -> Self
pub fn new_from_model(model: Arc<Model>) -> Self
Create a compressor from a preloaded model.
Sourcepub fn new_from_method(method: &str) -> Result<Self>
pub fn new_from_method(method: &str) -> Result<Self>
Create a compressor from a user method string.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the model state to initial values.
Call this between independent compression/decompression operations to ensure a clean state.
Sourcepub fn begin_online_policy_stream(
&mut self,
total_symbols: Option<u64>,
) -> Result<()>
pub fn begin_online_policy_stream( &mut self, total_symbols: Option<u64>, ) -> Result<()>
Begin a policy stream with optional total symbol count.
Sourcepub fn finish_online_policy_stream(&mut self) -> Result<()>
pub fn finish_online_policy_stream(&mut self) -> Result<()>
Flush any pending TBPTT segment while preserving the current predictive state.
Sourcepub fn restart_online_policy_stream(
&mut self,
total_symbols: Option<u64>,
) -> Result<()>
pub fn restart_online_policy_stream( &mut self, total_symbols: Option<u64>, ) -> Result<()>
Reset hidden state and TBPTT bookkeeping for a fresh stream.
Sourcepub fn reset_and_prime(&mut self)
pub fn reset_and_prime(&mut self)
Reset state and prime the first predictive distribution.
Sourcepub fn snapshot_runtime(&self) -> RuntimeSnapshot
pub fn snapshot_runtime(&self) -> RuntimeSnapshot
Capture runtime state for later restoration.
Sourcepub fn restore_runtime(&mut self, snapshot: &RuntimeSnapshot)
pub fn restore_runtime(&mut self, snapshot: &RuntimeSnapshot)
Restore previously captured runtime state.
Sourcepub fn absorb_chain(&mut self, parts: &[&[u8]]) -> Result<()>
pub fn absorb_chain(&mut self, parts: &[&[u8]]) -> Result<()>
Absorb a sequence of byte slices as conditioning context.
Sourcepub fn cross_entropy_from_current(&mut self, data: &[u8]) -> Result<f64>
pub fn cross_entropy_from_current(&mut self, data: &[u8]) -> Result<f64>
Score bytes from the current predictive state.
Sourcepub fn cross_entropy_frozen_plugin_chain(
&mut self,
fit_parts: &[&[u8]],
data: &[u8],
) -> Result<f64>
pub fn cross_entropy_frozen_plugin_chain( &mut self, fit_parts: &[&[u8]], data: &[u8], ) -> Result<f64>
Fit on fit_parts, then reset stream state and score data without further adaptation.
Sourcepub fn can_adapt_online(&self) -> bool
pub fn can_adapt_online(&self) -> bool
Returns true when the current online configuration can actually adapt parameters.
Sourcepub fn tokens_processed(&self) -> u64
pub fn tokens_processed(&self) -> u64
Number of tokens processed by the online updater.
Sourcepub fn online_method_string(&self) -> Option<&str>
pub fn online_method_string(&self) -> Option<&str>
Canonical method string for online mode, if enabled.
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Get the vocabulary size (should always be 256 for byte-level).
Sourcepub fn online_apply_logits_bias(&self, logits: &[f32], pdf_out: &mut [f64])
pub fn online_apply_logits_bias(&self, logits: &[f32], pdf_out: &mut [f64])
Apply optional online bias to logits and emit normalized PDF.
Sourcepub fn logits_to_pdf(logits: &[f32], bias: Option<&[f32]>, pdf_out: &mut [f64])
pub fn logits_to_pdf(logits: &[f32], bias: Option<&[f32]>, pdf_out: &mut [f64])
Convert logits (and optional bias) to a normalized PDF.
Sourcepub fn forward_to_pdf(&mut self, token: u32, pdf_out: &mut [f64])
pub fn forward_to_pdf(&mut self, token: u32, pdf_out: &mut [f64])
Forward one token and emit the resulting (optionally biased) PDF.
Sourcepub fn forward_to_internal_pdf(&mut self, token: u32)
pub fn forward_to_internal_pdf(&mut self, token: u32)
Refresh the internal cached PDF buffer from token.
Sourcepub fn copy_current_pdf_to(&self, pdf_out: &mut [f64])
pub fn copy_current_pdf_to(&self, pdf_out: &mut [f64])
Copy the internal cached PDF into pdf_out (length must match vocab size).
Sourcepub fn online_bias_snapshot(&self) -> Option<Vec<f32>>
pub fn online_bias_snapshot(&self) -> Option<Vec<f32>>
Snapshot current online output bias, if online mode is active.
Sourcepub fn online_bias_slice(&self) -> Option<&[f32]>
pub fn online_bias_slice(&self) -> Option<&[f32]>
Borrow online output bias vector when online mode is active.
Sourcepub fn online_update_from_pdf(&mut self, symbol: u8, pdf: &[f64]) -> Result<()>
pub fn online_update_from_pdf(&mut self, symbol: u8, pdf: &[f64]) -> Result<()>
Apply one online update using externally supplied predictive PDF.
Sourcepub fn observe_symbol_from_pdf(&mut self, symbol: u8, pdf: &[f64]) -> Result<()>
pub fn observe_symbol_from_pdf(&mut self, symbol: u8, pdf: &[f64]) -> Result<()>
Update online state from pdf, then advance model state with symbol.
Sourcepub fn observe_symbol_from_current_pdf(&mut self, symbol: u8) -> Result<()>
pub fn observe_symbol_from_current_pdf(&mut self, symbol: u8) -> Result<()>
Update online state using current internal PDF, then consume symbol.
Sourcepub fn export_online<P: AsRef<Path>>(&self, model_path: P) -> Result<()>
pub fn export_online<P: AsRef<Path>>(&self, model_path: P) -> Result<()>
Export model weights and JSON sidecar metadata.
Sourcepub fn compress_into<W: Write>(
&mut self,
data: &[u8],
coder: CoderType,
w: &mut W,
) -> Result<()>
pub fn compress_into<W: Write>( &mut self, data: &[u8], coder: CoderType, w: &mut W, ) -> Result<()>
Compress into an arbitrary writer.
Sourcepub fn compress_chain_into<W: Write>(
&mut self,
parts: &[&[u8]],
coder: CoderType,
w: &mut W,
) -> Result<()>
pub fn compress_chain_into<W: Write>( &mut self, parts: &[&[u8]], coder: CoderType, w: &mut W, ) -> Result<()>
Compress a chain of byte slices into an arbitrary writer.
Sourcepub fn compress_size(&mut self, data: &[u8], coder: CoderType) -> Result<u64>
pub fn compress_size(&mut self, data: &[u8], coder: CoderType) -> Result<u64>
Return compressed byte size without materializing output bytes.
Sourcepub fn compress_size_chain(
&mut self,
parts: &[&[u8]],
coder: CoderType,
) -> Result<u64>
pub fn compress_size_chain( &mut self, parts: &[&[u8]], coder: CoderType, ) -> Result<u64>
Return compressed byte size for chained inputs.
Sourcepub fn cross_entropy(&mut self, data: &[u8]) -> Result<f64>
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).
Sourcepub fn cross_entropy_conditional_chain(
&mut self,
prefix_parts: &[&[u8]],
data: &[u8],
) -> Result<f64>
pub fn cross_entropy_conditional_chain( &mut self, prefix_parts: &[&[u8]], data: &[u8], ) -> Result<f64>
Cross entropy conditioned on chained prefix slices.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Compressor
impl RefUnwindSafe for Compressor
impl Send for Compressor
impl Sync for Compressor
impl Unpin for Compressor
impl UnwindSafe for Compressor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.