N:M sparsity is a pruning pattern in which at most N of every M consecutive weights in a neural network layer are nonzero, for example two out of every four in the widely used 2:4 sparsity format. Also called semi-structured sparsity, it sits between irregular unstructured pruning and coarse structured pruning, and in Edge AI and the wider field of Artificial intelligence it matters because it delivers real speedups on supporting hardware while keeping accuracy close to the dense model.
The key property is predictability. Because every small group has the same number of nonzeros, hardware can store the surviving values in a compact array plus a short index and skip the zeros with fixed, regular logic, which is far easier to accelerate than scattered zeros.
How N:M sparsity constrains a weight matrix
N:M sparsity works by dividing each row of a weight matrix, usually along the input or reduction dimension, into consecutive groups of M elements and keeping only the N most important ones in each group. With 2:4, a group of four weights such as 0.8, -0.1, 0.05 and -0.6 becomes 0.8, 0, 0 and -0.6, which yields exactly 50 percent sparsity with a uniform pattern.
In storage, the kept values form a compressed matrix half the original width, and a metadata index of 2 bits per kept value records each value’s position within its group of four. During matrix multiplication, the hardware uses the metadata to select the matching activations, so it performs half the multiply-accumulate operations without branching or irregular memory access.
The table below shows how common N:M ratios translate into sparsity and practical support.
| Pattern | Sparsity | Hardware support | Accuracy impact |
|---|---|---|---|
| 1:2 | 50% | Limited, mainly research | Similar to 2:4 but more restrictive |
| 2:4 | 50% | Sparse Tensor Cores on recent NVIDIA GPUs | Small after fine-tuning |
| 4:8 | 50% | Mostly simulated or custom accelerators | Slightly better than 2:4 |
| 1:4 | 75% | Research and some custom designs | Noticeable without retraining |
| 2:8 | 75% | Research accelerators | Requires careful training |
How N:M sparsity compares with other pruning approaches
N:M sparsity offers a middle ground between flexibility and speed. Unstructured pruning can remove any individual weight and therefore preserves accuracy best at a given sparsity, but its irregular zeros rarely speed up dense hardware. Structured pruning removes entire channels, filters or heads, which speeds up any device but costs more accuracy per removed parameter.
| Approach | What is removed | Speedup on standard hardware | Accuracy at equal sparsity |
|---|---|---|---|
| Unstructured pruning | Individual weights anywhere | Low unless very sparse | Highest |
| N:M sparsity | Weights within fixed small groups | Up to about 2x in matrix math on supporting units | High |
| Block sparsity | Contiguous blocks such as 4×4 or 32×32 | Moderate with sparse kernels | Medium |
| Structured pruning | Whole channels, filters or heads | Yes, model becomes smaller and dense | Lowest |
N:M sparsity also combines well with quantization. A model can be both 2:4 sparse and stored in INT8 or lower precision, and the effects on memory and compute multiply, although combined errors must be checked on a validation set.
Methods for obtaining an N:M sparse model
An N:M sparse model is produced either by pruning a trained dense model or by training with the constraint from the start. The simplest method is magnitude pruning within each group, keeping the N largest absolute values, followed by fine-tuning to recover accuracy. For large language models, one-shot methods such as SparseGPT and Wanda select weights using second-order information or activation statistics and can reach 2:4 sparsity without full retraining.
| Method | Training cost | Typical use |
|---|---|---|
| Magnitude prune and fine-tune | Moderate, short retraining | Vision and speech models |
| Sparse training from scratch | Full training run | New models designed for sparse hardware |
| Learned masks with straight-through estimator | Full or partial training | Research on higher sparsity ratios |
| One-shot pruning such as SparseGPT | Low, calibration data only | Large language models |
A practical rule is that convolutional and Transformer vision models often regain nearly full accuracy at 2:4 after fine-tuning, whereas large language models pruned in one shot usually show a measurable perplexity increase that fine-tuning or distillation only partly closes.
N:M sparsity in edge AI deployment
In edge deployment, N:M sparsity is valuable only when the target runtime and accelerator implement sparse kernels. On embedded GPU modules with sparse tensor units, a 2:4 model can lower latency and power for matrix-heavy layers; on a phone CPU or a neural processing unit without such support, the same model runs as dense and gains nothing beyond possible storage compression.
- Check hardware support first: confirm that the inference runtime exposes sparse matrix kernels for the chosen pattern.
- Measure end-to-end latency, because attention, normalization and memory transfers are not accelerated and limit the total speedup.
- Apply sparsity to large linear and convolution layers and leave small or sensitive layers, such as the first and last ones, dense.
- Validate after export, since model conversion can silently densify weights or drop the sparsity metadata.
Limitations and common mistakes
The most frequent mistake is expecting a 2x end-to-end speedup from 50 percent sparsity. The theoretical gain applies only to the sparse matrix multiplication; real models typically see 1.1x to 1.6x overall, depending on batch size, layer shapes and how much time other operations take. At small batch sizes, inference is often memory-bound, so the reduction in weight bytes matters more than the reduction in arithmetic.
Another limitation is pattern rigidity. Some groups contain three or four important weights, and forcing them to two discards useful information, which is why higher ratios such as 1:4 hurt accuracy sharply. Finally, toolchain fragmentation remains a practical barrier: support differs between frameworks, compilers and devices, and a pattern that runs fast on one accelerator may be unsupported on another.
In everyday language
A neural network holds millions of numbers. N:M sparsity sets a simple rule for removing some of them. In every small group of four numbers, only the two most important ones stay. The other two become zero.
Because the rule is the same everywhere, a chip can skip the zeros easily. It is like a form where every row has exactly two filled boxes. A clerk can process it faster than a form with random gaps. The model stays almost as accurate, but the matching hardware works with less effort.
Seen in the real world
A team building a traffic monitoring camera runs an object detection model on an embedded GPU module mounted at an intersection. The dense model reaches about 22 frames per second, slightly below the target of 30 frames per second for tracking vehicles reliably.
The engineers apply 2:4 magnitude pruning to the convolution and linear layers, keep the first and last layers dense, and fine-tune for a few epochs on their labeled footage. Detection accuracy drops by less than one percentage point. After export to a runtime with sparse kernels, throughput rises to about 29 frames per second, and combining sparsity with INT8 quantization brings it above 30 while reducing power draw enough to stay within the enclosure’s thermal limit.