Hardware-Aware Model Optimization › Sparsity on Devices

Michael BrenndoerferDecember 1, 20263 min read
Language AI HandbookAll chapters
Part 43 of 67Small and On-Device Language Models
Chapter 328Hardware-Aware Model Optimization › Sparsity on Devices
Reading section 5 of 11 · Sparsity on Devices · 3 minSection 5 of 11 · 3 min

Section 5 of 11 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

A sparse representation can avoid storing zeros. Executing fewer arithmetic operations also requires an appropriate sparse kernel or a graph whose tensor shapes have changed. Selected NVIDIA TensorRT configurations can use structured-sparse tactics. For some input shapes, TensorRT still chooses a dense tactic. NVIDIA, TensorRT Structured Sparsity

Structured vs unstructured

  • Unstructured sparsity zeros individual weights. Compressed storage can save space. Skipping their multiplications requires a representation and execution path that exploit the zeros.
  • Shape-changing pruning removes whole channels, heads, or blocks and updates connected tensors. It reduces the modeled work of affected dense operators. Runtime depends on the resulting shapes and kernels.
2:4 Structured Sparsity

A 2:4 pattern permits at most two nonzero values in each group of four along the required axis. This pattern preserves the original matrix dimensions and has different execution requirements from removing complete channels. Check the operand types and grouping rules of the sparse kernel. NVIDIA, cuSPARSELt Sparsity Types

The bandwidth interaction

Compressing a sparse tensor reduces DRAM traffic when the runtime streams the compressed representation at that boundary. The payload includes values and position metadata. Expanding it into a dense DRAM tensor before the hot loop can remove the expected per-step traffic reduction.

The following cases separate storage from execution. Shape-changing pruning removes dimensions and may require updates to connected layers. A fixed-pattern representation leaves the dimensions in place and encodes the positions of the surviving values. Two models described as having the same sparsity can exercise different instructions because of that difference. Report the pattern, the stored format, and the actual kernel when comparing them.

  • Compressed values with a matching sparse kernel can reduce traffic and work.
  • Zero values in a dense tensor still occupy the same payload and pass through an ordinary dense kernel.
  • Shape-changing pruning can use smaller dense tensors. Measure how the new shapes map to the runtime. Structured Pruning

When comparing candidates, keep the quality target fixed:

  • Dense low-bit.
  • Compressed sparse.
  • Smaller dense.

Sparsity has a deployment cost when it changes the format or requires a different kernel.

A quick sparse-vs-dense check

Consider a toy encoding for exactly two stored slots in each group of four. There are six possible position pairs, so a fixed-width index needs at least three bits per group. With two four-bit values and that index, the packed payload is (2⋅4+3)/4=2.75(2\cdot4+3)/4=2.75 bits per original weight. That saves 31.25% relative to the four-bit dense payload. This example omits scales, alignment, and other metadata, and it describes its own encoding.

PythonIn [22]
CollapseExpand
def bytes_per_weight_dense(bits):
    return bits / 8


def bytes_per_weight_2to4(bits, block=4, keep=2, index_bits=3):
    # keep nonzero values + index bits per block
    return (keep * bits + index_bits) / block / 8


sparse_dense_results = {}
for bits in [4, 8]:
    dense = bytes_per_weight_dense(bits)
    sparse = bytes_per_weight_2to4(bits)
    sparse_dense_results[bits] = (dense, sparse)

The same assumed layout uses 4.75 bits per original weight with eight-bit values, a 40.625% reduction relative to dense INT8. Compare either result against the storage and kernel cost of changing bit width instead. A production sparse format can have a different encoding or alignment.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy