Lossless Compression with Latent Variable Models

James Townsend

Abstract

This paper presents a framework for leveraging deep generative models, specifically Latent Variable Models (LVMs), for state-of-the-art lossless data compression. Historically, using models like Variational Autoencoders (VAEs) for lossless compression was hindered by the intractability of marginal probabilities and the algorithmic incompatibility between bits back coding and standard Arithmetic Coding. We introduce Bits Back with Asymmetric Numeral Systems (BB-ANS), a method that exploits the stack-like (LIFO) nature of ANS to enable efficient, near-optimal compression. Our results demonstrate that hierarchical VAEs, when paired with BB-ANS, can outperform traditional codecs such as PNG, WebP, and FLIF on high-resolution image datasets like ImageNet.

Introduction

In information theory, the fundamental limit of lossless compression is determined by the entropy of the data distribution. If a perfect probabilistic model of a data source is available, one can theoretically achieve this limit. However, modern deep learning has primarily excelled at lossy compression, where the goal is to capture the "gist" of the data while discarding imperceptible details.

Lossless compression requires bit-perfect reconstruction, which necessitates calculating the exact probability of every possible data point. For Latent Variable Models (LVMs), this involves marginalizing over latent variables—a task that is computationally intractable for complex neural networks. While "bits back coding" was proposed in the 1990s as a theoretical solution to this problem, its implementation remained impractical due to a data structure conflict: bits back coding requires a Last-In-First-Out (LIFO) decoding order, whereas traditional Arithmetic Coding (AC) operates as a First-In-First-Out (FIFO) queue. This work bridges that gap by utilizing Asymmetric Numeral Systems (ANS).

Methodology

Bits Back with Asymmetric Numeral Systems (BB-ANS)

The core contribution of this research is the development of BB-ANS. Unlike Arithmetic Coding, ANS is inherently a stack-based system. This property makes it perfectly compatible with bits back coding, where the compressed bits of a previous data point can serve as the auxiliary information needed to sample latent variables for the current data point. By "chaining" images in a sequence, the overhead of the initial bits is amortized, allowing the compression rate to converge to the model's Evidence Lower Bound (ELBO).

Hierarchical Latent Lossless Compression (HiLLoC)

To scale this approach to high-dimensional data, we developed HiLLoC, an extension of BB-ANS designed for hierarchical VAEs. HiLLoC utilizes Dynamic Discretization to manage continuous latent variables across multiple layers of a neural network. This allows the model to capture both global structures and local textures efficiently.

Implementation and Vectorization

Practical application of these algorithms in Python is often limited by execution speed. To address this, we developed a Vectorized ANS implementation. By moving heavy computational tasks from Python loops to optimized C and Fortran kernels via NumPy, we achieved a significant reduction in latency. Furthermore, we introduced Craystack, a modular software framework designed for rapid prototyping of compression algorithms using deep generative models.

Experimental Results

Performance Benchmarks

The proposed methods were evaluated on several standard benchmarks, including MNIST, CIFAR-10, and ImageNet (both downsampled and full-resolution).

  • State-of-the-Art Compression: On full-size color photographs from the ImageNet dataset, the HiLLoC method achieved a 6.5% improvement in compression rate over the previous state-of-the-art lossless codec, FLIF. It also significantly outperformed ubiquitous standards like PNG and WebP.
  • Theoretical Convergence: The actual bitrates achieved were consistently within approximately 1% of the negative ELBO of the underlying models, confirming that the BB-ANS algorithm effectively reaches the theoretical efficiency of the generative model.
  • Computational Efficiency: Through the vectorized implementation in Craystack, the system achieved a three-order-of-magnitude speedup over serial implementations. A standard ImageNet image can be compressed in roughly 30 seconds on a desktop CPU.

Discussion and Future Work

While BB-ANS represents a significant leap in lossless compression performance, several challenges remain. The primary limitation is computational latency; 30 seconds per image is still significantly slower than traditional codecs like JPEG or PNG, largely due to the multiple neural network forward passes required during the encoding and decoding processes.

Additionally, the requirement for model transmission poses a hurdle for one-off communications. The receiver must possess the trained model weights (often hundreds of megabytes) to decompress the data. Consequently, this method is currently best suited for persistent channels where large volumes of data are transmitted between the same parties.

Future research directions include porting the framework to JAX for GPU acceleration and exploring more expressive architectures, such as Normalizing Flows or Autoregressive models, to further push the boundaries of the entropy limit.

Summary updated

BibTeX
@misc{townsend2021lossless,
  author = {James Townsend},
  title = {{Lossless Compression with Latent Variable Models}},
  year = {2021},
  eprint = {2104.10544},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2104.10544},
}
Figure