pub struct VSAGradientCompressor { /* private fields */ }Expand description
Compress gradients using Vector Symbolic Architecture.
This compressor uses proper VSA operations:
- Project each gradient to hyperdimensional space
- Bind each projected gradient with a unique random key
- Bundle all bound vectors into a single superposition
- Unbind during decompression to extract individual gradients
The bundled representation achieves significant compression while the bind/unbind operations enable accurate reconstruction due to the quasi-orthogonality of random keys in high dimensions.
§Example
ⓘ
use vsa_optim_rs::vsa::VSAGradientCompressor;
use vsa_optim_rs::VSAConfig;
let compressor = VSAGradientCompressor::new(1_000_000, VSAConfig::default());
// After computing gradients
let (compressed, metadata) = compressor.compress(&gradients)?;
let reconstructed = compressor.decompress(&compressed, &metadata)?;Implementations§
Source§impl VSAGradientCompressor
impl VSAGradientCompressor
Sourcepub fn new(param_count: usize, config: VSAConfig) -> VSAGradientCompressor
pub fn new(param_count: usize, config: VSAConfig) -> VSAGradientCompressor
Create a new VSA gradient compressor.
§Arguments
param_count- Total number of model parametersconfig- VSA configuration
Sourcepub const fn compressed_dim(&self) -> usize
pub const fn compressed_dim(&self) -> usize
Get the hypervector dimension.
Sourcepub fn compress(
&mut self,
gradients: &HashMap<String, Tensor>,
) -> Result<(PackedTritVec, HashMap<String, GradientMetadata>), OptimError>
pub fn compress( &mut self, gradients: &HashMap<String, Tensor>, ) -> Result<(PackedTritVec, HashMap<String, GradientMetadata>), OptimError>
Compress gradients to bundled hyperdimensional representation.
§Algorithm
- Project each gradient to hypervector space
- Quantize to ternary {-1, 0, +1}
- Bind with unique random key
- Bundle all bound vectors via element-wise sum
§Arguments
gradients- Map of parameter names to gradient tensors
§Returns
Tuple of (bundled hypervector, metadata for reconstruction).
Sourcepub fn decompress(
&mut self,
bundled: &PackedTritVec,
metadata: &HashMap<String, GradientMetadata>,
) -> Result<HashMap<String, Tensor>, OptimError>
pub fn decompress( &mut self, bundled: &PackedTritVec, metadata: &HashMap<String, GradientMetadata>, ) -> Result<HashMap<String, Tensor>, OptimError>
Decompress gradients from bundled hypervector.
§Algorithm
For each gradient:
- Unbind with the gradient’s key to extract from bundle
- Inverse project back to gradient space
- Apply stored scale factor
§Arguments
bundled- Bundled hypervector from compressmetadata- Metadata from compression
§Returns
Map of reconstructed gradients.
Sourcepub fn get_compression_stats(&self) -> CompressionStats
pub fn get_compression_stats(&self) -> CompressionStats
Get compression statistics.
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear caches to free memory.
Auto Trait Implementations§
impl Freeze for VSAGradientCompressor
impl !RefUnwindSafe for VSAGradientCompressor
impl Send for VSAGradientCompressor
impl Sync for VSAGradientCompressor
impl Unpin for VSAGradientCompressor
impl !UnwindSafe for VSAGradientCompressor
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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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 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>
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