How phi_mask chooses
phi_mask(weight, axis=0, score_fn="magnitude", density=None) scores each position along axis by the L1 norm of its slice: for a Conv2d weight shaped [out_c, in_c, kH, kW] or a Linear weight shaped [out, in], that is one score per output channel. A 1-D tensor's positions are scored by their absolute values. "magnitude" is the one score function, and any other value raises ValueError.
adjacency_dp(scores) then finds the maximum-weight independent set on the path graph of the positions. It walks forward keeping two values per position and reads the mask off backwards:
keep[i] = skip[i-1] + scores[i] # keep i, so i-1 was skipped
skip[i] = max(keep[i-1], skip[i-1]) # skip i, after the better of the two
The backward pass keeps position i when keep[i] >= skip[i], so a tie keeps it, and then jumps two places. With scores of zero or more at least one position is kept, and an empty array gives an empty mask.
The 1-D mask is reshaped to size 1 on every axis but axis and expanded to the weight's shape, dtype and device, so a dropped output channel loses every one of its weights. The mask is ready to multiply.
When every score is above zero, the heaviest set leaves no dropped position whose two neighbours are both dropped too, since keeping it would add weight. So the φ mask keeps between a third and a half of the positions, at most (n + 1) // 2. On the MLP layers of the small transformer the transformer check prunes, it kept 0.4932, 0.4937 and 0.4946 of the neurons in its three seeds, as low.json records.
Source: phi_prune/masks.py, adjacency_dp and phi_mask. The problem and its recurrence are the case study "Max Weight Independent Set on a Path" of Cornell's CS 4820 notes on dynamic programming.
Keeping fewer than the heaviest set
phi_mask(weight, density=d) calls adjacency_dp_density(scores, d), which aims at round(d * n) kept positions. When the heaviest set already keeps that many or fewer, it is returned as it is, so no density keeps more than (n + 1) // 2.
Otherwise it subtracts a penalty λ from every score, so that adjacency_dp keeps only positions whose score clears λ, and bisects λ between 0 and the largest score plus 1 for up to 60 rounds. It returns the mask whose kept count came closest to the target, and ties among scores can leave that count one or two off. A density so low that round(d * n) is 0 keeps no position at all.
prune passes a density through only below 0.5. At 0.5 or above, each φ mask is the heaviest set the constraint allows.
Source: adjacency_dp_density in phi_prune/masks.py, and prune in phi_prune/api.py.
The mask that keeps 2 of every 4 channels
two_four_mask(weight, axis=0) scores positions as phi_mask does, keeps the 2 with the largest L1 norm in each group of 4 consecutive positions, and keeps a trailing group shorter than 4 whole. It is the baseline the φ mask is measured against, and prune(model, pattern="2:4") applies it.
Along axis 0 it keeps or drops whole output channels. NVIDIA's 2:4 sparsity keeps 2 of every 4 weights inside each row instead, and runs on the Sparse Tensor Cores of Ampere and later GPUs, as Mishra et al., 2021 describe. The pattern value "2:4", which checkpoints store, names the channel pattern of this module.
It takes no density: prune with pattern="2:4" and a density below 0.5 raises ValueError.
Checking a mask
verify_mask(mask_1d)is true when no two adjacent values are 1.verify_two_four(mask_1d)is true when every full group of 4 keeps exactly 2 and a trailing shorter group keeps all of its positions.mask_stats(mask, axis=0, pattern="phi")reads the mask alongaxisat index 0 of every other dimension and returnsdensity,sparsity,active_count,total_count,capacity_utilization, the kept count over the most the pattern keeps, andvalid.validholds when the pattern's verifier accepts that slice and the whole mask equals the slice broadcast over the other dimensions, so a bit flipped anywhere in the mask fails it.PATTERNSmaps"phi"and"2:4"to their generator and verifier, andget_pattern(name)returns that pair or raisesValueErrorfor any other name.
phi_prune.check and the phi-prune check command both read masks through mask_stats; Pruning a model covers them.
How many masks a rule allows
The words of n bits with no two adjacent 1s number F(n+2), with F(1) = F(2) = 1: 8 words of 4 bits, 55 of 8 bits, 377 of 12 bits. log₂ of that count over n tends to log₂ φ ≈ 0.6942 bits per symbol, the capacity of the (1,∞)-RLL constraint, which Sabag, Permuter and Kashyap give as about 0.6942. The bi-infinite version of the same rule is called the golden mean shift.
That count runs over every density at once. A mask is chosen at one density, and at half of the channels the constraint allows very few: mask_choice.py counts the masks each pattern allows on 64 channels and prints log₂ of that count per channel.
| mask | density | bits/channel |
|---|---|---|
| phi | 0.250 | 0.650 |
| phi | 0.281 | 0.657 |
| phi | 0.375 | 0.580 |
| phi | 0.500 | 0.079 |
| 2 of every 4 | 0.500 | 0.646 |
| 1 of every 4 | 0.250 | 0.500 |
| any 50%, no pattern | 0.500 | 0.948 |
Result file: experiments/phi-register/results/register/mask_choice.txt in ezexe/p4-phi-experiments, which the script prints with the standard library alone.
How the φ mask fares against 2 of every 4 in training is on Measurements.
Related work
- Channel pruning and structured pruning are the documented names for keeping or dropping whole channels; Hoefler et al., "Sparsity in Deep Learning", JMLR 2021, survey them. φ-pruning is phi's name for channel pruning under the (1,∞)-RLL constraint.
- Simon, Rey, Levisse, Ansaloni, Zapater and Atienza, "Exact Neural Networks from Inexact Multipliers via Fibonacci Weight Encoding", DAC 2021, quantize weights to values whose binary form has no two adjacent 1s. That is a rule on weight values, not a pruning mask; Quantization and the code sets it beside phi's encoder.