lucidrains/vector-quantize-pytorch
Vector (and Scalar) Quantization, in Pytorch
What it solves
This library provides a comprehensive set of tools for vector quantization (VQ), a process used in generative AI to map continuous data (like images or audio) into a discrete set of learned codes. It addresses common challenges in VQ, such as "dead" codebook entries (where some codes are never used) and the difficulty of training non-differentiable quantization layers.
How it works
The library implements various quantization strategies that transform input vectors into discrete indices and back into quantized vectors:
- Standard VQ: Uses a codebook and exponential moving averages (EMA) to update the dictionary.
- Residual VQ: Recursively quantizes the residuals (the difference between the original and quantized vector) using multiple quantizers to achieve higher precision.
- FSQ (Finite Scalar Quantization): Simplifies the process by rounding scalars into discrete levels, removing the need for a learned codebook or commitment losses.
- LFQ (Lookup Free Quantization): Eliminates the codebook entirely by using independent binary latents.
- FSP (Finite Scalar Perturbation): Treats discretization as structured noise injected into representations to stabilize training.
- Advanced Gradient Methods: Implements the "rotation trick" and DiVeQ to provide better gradient flow during training compared to the traditional straight-through estimator (STE).
Who it’s for
It is designed for researchers and developers building generative models (such as VQ-VAE, VQ-GAN, or audio codecs like Encodec and SoundStream) who need flexible, high-performance quantization layers in PyTorch.
Highlights
- Diverse Implementations: Supports standard VQ, Residual VQ, Grouped Residual VQ, FSQ, LFQ, and SimVQ.
- Codebook Health: Includes techniques to combat codebook collapse, such as lower codebook dimensions, cosine similarity, and stale code replacement.
- Flexible Initialization: Supports k-means initialization for codebooks based on the first batch of data.
- Multi-process Support: Automatically synchronizes codebooks across multiple processes.
Related
- Project
- Project
- Project
- Project
- Project