ModelCompressionBase<T>

Base Classes · AiDotNet.ModelCompression

Provides a base implementation for model compression techniques used to reduce model size while preserving accuracy.

Implements: IModelCompressionStrategy<T>

Known derived types: DeepCompression<T>, HuffmanEncodingCompression<T>, HybridHuffmanClusteringCompression<T>, LowRankFactorizationCompression<T>, ProductQuantizationCompression<T>, SparsePruningCompression<T>, WeightClusteringCompression<T>

For Beginners

Think of model compression as packing for a trip - you want to fit everything you need into a smaller suitcase.

When you train an AI model:

  • It learns millions or billions of parameters (weights)
  • These weights need to be stored and loaded when making predictions
  • Larger models are slower and use more memory

Model compression helps by:

  • Reducing the file size (easier to download and store)
  • Speeding up predictions (less data to process)
  • Enabling deployment on phones, tablets, or embedded devices
  • Lowering costs in cloud environments

This base class provides the common structure that all compression techniques share. Different compression approaches (like weight clustering, quantization, or Huffman coding) work in different ways, but they all aim to make your model smaller and faster while keeping it accurate.

How It Works

ModelCompressionBase serves as an abstract foundation for implementing various compression strategies. Model compression reduces the storage and computational requirements of machine learning models, making them more suitable for deployment on resource-constrained devices or in bandwidth-limited environments.

Methods

Method Summary
Matrix<T> DecompressMatrix(Matrix<T> compressedWeights, ICompressionMetadata<T> metadata) Decompresses the compressed matrix weights back to their original form.
Tensor<T> DecompressTensor(Tensor<T> compressedWeights, ICompressionMetadata<T> metadata) Decompresses the compressed tensor weights back to their original form.
ValueTuple<Matrix<T>, ICompressionMetadata<T>> CompressMatrix(Matrix<T> weights) Compresses a 2D matrix of weights.
ValueTuple<Tensor<T>, ICompressionMetadata<T>> CompressTensor(Tensor<T> weights) Compresses an N-dimensional tensor of weights.
ValueTuple<Vector<T>, ICompressionMetadata<T>> Compress(Vector<T> weights) Compresses the given model weights.
Vector<T> Decompress(Vector<T> compressedWeights, ICompressionMetadata<T> metadata) Decompresses the compressed weights back to their original form.
double CalculateCompressionRatio(long originalSize, long compressedSize) Calculates the compression ratio achieved.
long GetCompressedSize(Matrix<T> compressedWeights, ICompressionMetadata<T> metadata) Gets the size in bytes of the compressed tensor representation.
long GetCompressedSize(Tensor<T> compressedWeights, ICompressionMetadata<T> metadata) Gets the size in bytes of the compressed tensor representation.
long GetCompressedSize(Vector<T> compressedWeights, ICompressionMetadata<T> metadata) Gets the size in bytes of the compressed tensor representation.