Model Efficiency & Scaling2016intermediate9 min read

Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding

الضغط العميق: تقليص الشبكات العصبية العميقة عبر التقليم والتكميم المُدرَّب وترميز هوفمان

Han, S. · Mao, H. · Dally, W. J. — ICLR

The problem

Neural networks in 2015 demanded hundreds of megabytes of storage — AlexNet was 240 MB, VGG-16 was 552 MB. These sizes made deployment on mobile phones, embedded sensors, and edge devices impractical: models couldn't fit in on-chip SRAM, requiring slow and power-hungry off-chip DRAM access. Bandwidth constraints made over-the-air updates painful. The field needed a systematic way to shrink models dramatically without sacrificing .

The contribution

A three-stage compression pipeline — , trained , and — that work together to reduce storage by 35× to 49× with no accuracy . Pruning removes unimportant connections (9–13× reduction). Trained quantization clusters remaining weights into shared centroids and fine-tunes them via (32 bits → 5 bits per ). Huffman coding exploits the non-uniform distribution of quantized weights and sparse indices for further lossless compression. The key insight is that each stage is retrained before the next, so accuracy is preserved through the entire pipeline.

The impact

Deep Compression won the Best Paper Award at ICLR 2016 and became the foundational framework for compression. It proved that huge models hide massive redundancy and that pruning + quantization + coding can unlock it without retraining from scratch. Its ideas directly influenced the Lottery Ticket Hypothesis, MobileNet's efficient architectures, LLM.int8() quantization for large language models, and GPTQ post- quantization — making billion- models runnable on consumer GPUs.

A neural network straight from training is like a warehouse full of furniture: thousands of items, many duplicates, plenty you'll never use. Pruning is a ruthless declutter — toss every piece nobody sits on. Quantization replaces the remaining one-of-a-kind chairs with a small catalog of standard models — close enough in comfort, far cheaper to stock. Huffman coding shrinks the inventory list itself, giving the most common items the shortest codes. The warehouse becomes a single shipping container, yet every room in the house can still be furnished.

The problem: neural networks are too large for the edge

By 2015 deep neural networks had conquered image classification, but their size was a barrier to real-world deployment. AlexNet needed 240 MB of storage; VGG-16 needed 552 MB. Mobile phones and embedded devices have limited memory, limited bandwidth, and strict power budgets. Accessing weights from off-chip DRAM costs 100× more energy than an arithmetic operation — so the memory bottleneck is also an energy bottleneck.

The question was: how much of this storage is actually necessary? Can we compress a trained network dramatically without destroying what it has learned?

Open in Lab
Original model sizes vs. Deep Compression results. Drag the slider to see the compression ratio.
The demo wakes as you arrive…

The idea: a three-stage compression pipeline

Deep Compression applies three complementary techniques in sequence, each one building on the previous stage's output and each followed by retraining to recover any lost accuracy:

Stage 1 — Pruning: Remove connections whose weights are near zero. The network learns which connections matter, and the rest are deleted. This reduces the number of weights by 9–13×.

Stage 2 — Trained Quantization: Cluster the remaining weights using k-means so that many connections share the same value. Instead of storing a 32-bit float per weight, store a small index into a codebook of shared centroids. This cuts bits per weight from 32 to ~5.

Stage 3 — Huffman Coding: Encode the quantized weights and sparse indices using variable-length codes — frequent values get short codes, rare values get long ones. This squeezes out the last 20–30% of redundancy losslessly.

Open in Lab
Click each stage to see how the network shrinks step by step.
The demo wakes as you arrive…

Stage 1: Pruning — cutting the dead weight

The insight behind pruning is that trained networks are heavily over-parameterized: many weights end up near zero and contribute almost nothing to the output. The idea traces back to Optimal Brain Damage (LeCun et al., 1990), which showed that removing low-magnitude weights barely affects accuracy.

Deep Compression's pruning works in three steps: (1) train the network normally, (2) set all weights below a threshold to zero, (3) retrain the surviving connections. The threshold is a — set too low and you don't compress much; set too high and you lose accuracy. The paper found that convolutional layers tolerate pruning up to 60–70% of connections, while fully connected layers can lose 90–95% without degradation.

After pruning, the weight matrix becomes sparse. Instead of storing a dense matrix of mostly zeros, the paper uses Compressed Sparse Row (CSR) or Compressed Sparse Column (CSC) format, storing only the nonzero values and their positions.

wij={wijif ∣wij∣>τ0otherwisew_{ij} = \begin{cases} w_{ij} & \text{if } |w_{ij}| > \tau \\ 0 & \text{otherwise} \end{cases}
Weight pruning rule: keep only weights above threshold τ — Each weight is compared against a threshold τ. Weights with absolute value below τ are set to zero (pruned). The surviving weights are then retrained to compensate for the removed connections.
Open in Lab
Drag the threshold slider to prune weights. Watch the network become sparse while accuracy holds.
The demo wakes as you arrive…

Stage 2: Trained quantization — sharing weights

After pruning, each surviving weight is still a unique 32-bit floating-point number. The next question is: do we really need all that precision? Quantization answers with a decisive "no."

The method clusters weights using k-means: for a with thousands of unique weight values, find just kk representative centroids. Each weight is then replaced by the index of its nearest centroid. If k=16k = 16, we need only 4 bits per weight instead of 32 — an 8× reduction in bits alone.

But here's the crucial twist that makes this "trained" quantization: after clustering, gradients are computed normally during backpropagation, and all gradients that map to the same centroid are summed and used to update that centroid's value. The centroids themselves are fine-tuned. This means the quantization isn't a one-shot approximation — the network actively learns the best shared weight values.

Ck←Ck−η∑i,j:idx(wij)=k∂L∂wijC_k \leftarrow C_k - \eta \sum_{i,j : \text{idx}(w_{ij})=k} \frac{\partial \mathcal{L}}{\partial w_{ij}}
Centroid gradient update — fine-tuning shared weights — All gradients flowing to weights assigned to centroid CkC_k are summed, then the centroid is updated with the aggregated gradient. This is what makes the quantization "trained" — the codebook adapts to minimize loss.
Open in Lab
Watch how k-means clusters weights and how centroids are fine-tuned. Toggle the number of clusters to see the precision-compression trade-off.
The demo wakes as you arrive…

Stage 3: Huffman coding — the final squeeze

After pruning and quantization, the weights are stored as small integers (cluster indices) and the sparse structure is encoded as index differences. Both distributions are non-uniform — some values appear far more often than others. Huffman coding is a lossless compression technique that exploits this: it builds a binary tree where frequent symbols get short codes and rare symbols get long codes. The expected code length approaches the of the distribution — the theoretical minimum.

Think of it like Morse code: the letter "E" (the most common in English) is a single dot, while "Q" (rare) is dash-dash-dot-dash. Huffman coding does the same thing automatically for weight values. The paper reports that this final stage saves an additional 20–30% of storage on top of pruning and quantization.

Open in Lab
See how a Huffman tree assigns shorter codes to frequent weight values. Click any leaf to trace its code path.
The demo wakes as you arrive…

The three stages in code

Deep Compression pipeline — pruning, quantization, Huffman codingpython

Simplified to show the idea — not the real implementation.

import numpy as np
from collections import Counter

# ── Stage 1: Pruning ──────────────────────────────────────────────
def prune(weights, threshold):
    """Zero out weights below threshold, return pruned weights + mask."""
    mask = np.abs(weights) > threshold
    return weights * mask, mask

# ── Stage 2: Trained Quantization ─────────────────────────────────
def quantize(weights, mask, n_clusters=16):
    """Cluster nonzero weights into n_clusters centroids."""
    nonzero = weights[mask]                     # only surviving weights
    # Simple k-means: cluster into n_clusters groups
    from sklearn.cluster import KMeans
    km = KMeans(n_clusters=n_clusters, n_init=10)
    km.fit(nonzero.reshape(-1, 1))
    centroids = km.cluster_centers_.flatten()   # the codebook
    labels = km.labels_                         # index per weight
    return centroids, labels

# ── Stage 3: Huffman Coding ───────────────────────────────────────
def huffman_savings(labels):
    """Estimate bits saved by Huffman vs fixed-length encoding."""
    counts = Counter(labels)
    total = len(labels)
    # Shannon entropy = theoretical best avg bits
    entropy = -sum((c/total) * np.log2(c/total) for c in counts.values())
    fixed_bits = np.ceil(np.log2(len(counts)))  # uniform code length
    return fixed_bits - entropy                 # bits saved per weight

# ── Run pipeline ──────────────────────────────────────────────────
W = np.random.randn(1000, 1000).astype(np.float32)
W_pruned, mask = prune(W, threshold=0.5)        # ~62% pruned
centroids, labels = quantize(W_pruned, mask, 16) # 32→4 bits
saving = huffman_savings(labels)                 # extra ~0.7 bits/weight
# Total: 240 MB → ~7 MB for AlexNet-scale networks

Results — 35× to 49× smaller, zero accuracy loss

The compression results are striking. AlexNet was compressed from 240 MB to 6.9 MB (35×) with no loss of top-1 or top-5 accuracy on ImageNet. VGG-16 went from 552 MB to 11.3 MB (49×). The compressed models fit entirely in on-chip SRAM cache, eliminating the need for power-hungry DRAM access.

Each stage contributes multiplicatively: pruning gives 9–13× by removing connections, quantization gives another 4–8× by reducing bits per connection, and Huffman coding squeezes out the last 20–30%. The combination is far more powerful than any single technique alone.

Benchmarks on CPU, , and mobile GPU showed 3–4× layerwise speedup and 3–7× better energy efficiency. The compressed VGG-16 at 11.3 MB fits comfortably on a mobile device that would have struggled with the original 552 MB.

Open in Lab
Stacked bar chart showing each stage's contribution to the total compression ratio for AlexNet and VGG-16.
The demo wakes as you arrive…

Why it mattered

The paper's three-stage approach also demonstrated a powerful meta-principle: compression techniques compose. Pruning, quantization, and coding are orthogonal axes of compression. Each removes a different kind of redundancy — structural, precision, and statistical. This composability is why the combined 35–49× factor exceeds what any single method achieves.

  1. 1990

    Optimal Brain Damage

    LeCun et al. showed that removing low-magnitude weights from a trained network barely affects accuracy. The first demonstration that pruning works.

  2. 2015

    Learning Both Weights and Connections

    Song Han's earlier work demonstrating systematic magnitude-based pruning with retraining. The direct precursor to Deep Compression.

  3. 2016

    Deep Compression (this paper)

    Combined pruning + trained quantization + Huffman coding into a unified pipeline. 35–49× compression with no accuracy loss. Best Paper at ICLR 2016.

  4. 2017

    MobileNet

    Instead of compressing large networks after training, MobileNet designed efficient architectures from the start using depthwise separable convolutions.

  5. 2019

    Lottery Ticket Hypothesis

    Frankle & Carlin showed that dense networks contain sparse subnetworks ("winning tickets") that can match full accuracy when trained from their initial weights — extending the insight that most weights are redundant.

  6. 2022

    LLM.int8()

    Dettmers et al. applied 8-bit quantization to billion-parameter language models, enabling inference on consumer GPUs. Deep Compression's quantization ideas scaled to the LLM era.

  7. 2023

    GPTQ

    Post-training quantization to 3–4 bits for GPT-scale models. Made 175B-parameter models runnable on a single GPU — the logical conclusion of the compression pipeline Deep Compression pioneered.

CitationHan, Mao, Dally. Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding. ICLR, 2016.

Terms in this paper