Entropy Coding Explained via Latent Variable Models: ANS Decodes 14x Faster Than Arithmetic Coding

Understanding Entropy Coding With Asymmetric Numeral Systems (ANS): a Statistician's Perspective

Robert Bamler

stat.ML, cs.IT, cs.LG

2022-01-06

A tutorial that derives ANS from the bits-back trick, with a full Python implementation and the constriction library: 0.0015% overhead, decoding 14x faster than arithmetic coding.

What problem this solves

Neural compression now beats traditional codecs on images and video, but the tail end of every pipeline is a purely algorithmic component: entropy coding, the step that turns symbol sequences into bit strings close to their information content. Recent ML-based compression work mostly settles on Asymmetric Numeral Systems (ANS) as its entropy coder, and researchers coming from machine learning routinely fail to understand how it works, so they call it as a black box.

The cost is concrete. Symbol codes like Huffman spend at least one full bit per symbol, while learned compression often spreads total entropy over a huge number of symbols; the paper's example is that at 0.3 bits of entropy per symbol, a symbol code incurs over 200% overhead, which kills the method. Stream codes, which let many symbols share bits, are the only option there, and ANS, the stream code that best fits bits-back methods with latent variable models, is exactly the one people find opaque. This paper targets that barrier: it rederives ANS in the language of latent variable models, walks to a working Python implementation, and ships an open-source library for research and production.

Method

The derivation proceeds in four steps, each matched to runnable code.

The three-step decomposition pays off beyond pedagogy. Section 4.3 shows a direct result: in plain ANS, changing the entropy model of one symbol ripples into the decoding of all later symbols through the internal state, which makes end-to-end optimization through the coder extremely difficult. The decomposition pins the problem on information leaking through the shared stack; a variant called ChainCoder, built on two separate stacks, confines any model change to the current symbol. Random access benefits too: the ANS encoder and decoder are the same data structure sharing one state, so a checkpoint is just (bulk length, head).

Results

The benchmark is real deployment data: discretized parameters of a natural-language model, 209 slices of 3 million parameters each, per-symbol entropy from 0.001 to 10 bits (average 0.41), single-threaded on an i7-7500U.

Method (precision/wordsize/headcapacity)Bitrate overheadEncode ns/symbolDecode ns/symbol
ANS 24/32/64 (default)0.0015%24.26.1
RC 24/32/64 (default)0.0237%16.614.3
AC (63-bit fixed point)0.0004%43.285.6
ANS 12/16/32 (small)3.9567%19.86.9

Arithmetic coding lands closest to the theoretical bound but is slowest at both encoding and decoding; the author attributes this to its bit-by-bit data access, a poor fit for modern hardware. ANS decodes fastest, more than twice as fast as RC and 14x faster than AC, while RC encodes fastest. The small preset's 3.9567% overhead is almost entirely quantization KL divergence (3.9561%), so the precision parameter is not decorative. Decoding slows in the high-entropy regime, where binary search for the symbol's subrange dominates; tabulating a 2^precision-entry lookup table speeds up high-entropy decoding but hurts at low entropy, presumably because large tables are not cache friendly.

Why it matters

For anyone building learned compression, this is a directly usable tutorial plus toolchain. The constriction library offers Python and Rust APIs that are binary compatible, so a prototype validated in Python can be compiled by Rust into a standalone binary or WebAssembly module; the paper's companion web app ships exactly that way. The selection advice is just as practical: queue-semantics RC for autoregressive models, stack-semantics ANS for bits-back coding with latent variable models; prototype on the default preset and tune only after the design settles.

The methodological point matters more: ChainCoder, a new variant that removes ANS's non-local model effects, fell straight out of the three-step decomposition. Understanding the internals bought a new algorithm. The caveat: this is a tutorial plus engineering paper, it does not beat existing methods on compression performance, and the speed comparison depends on implementation quality.

Limitations

The author states the runtime numbers should be taken with a grain of salt: they depend on implementation quality, and the arithmetic coding baseline is a third-party Rust crate (arcode), not a tuned implementation. The benchmark covers one dataset, one laptop CPU, one thread, and constriction was at version 0.1.4. The Python listings are explicitly educational. ChainCoder ships as a code sketch with no empirical evaluation. One engineering trap deserves attention: any symbol with mi = 0 cannot be encoded at all, and quantizing learned model outputs is exactly where such extreme values show up, so the fixed-point approximation needs care.

Terms

Source

What people are saying

Related papers

All paper explainers