Structured pruning removes an entire channel or unit and its connected weights so the exported computation can actually shrink, subject to backend support and accuracy checks.
Structured pruning and compute shape
Distinguish a zero from a smaller graph
Setting individual weights to zero creates sparsity but can leave dense tensor shapes unchanged. Removing a whole hidden unit changes the first layer’s output width and the next layer’s input width. The code removes one unit from a two-layer toy head, updates both matrices and counts stored weights. The operation is illustrative; a real graph must also update biases, normalization and all downstream branches. Measured profiling decides whether the edit helps.
Choose units on development data
Magnitude, activation statistics and sensitivity tests can propose units to prune, but none is a guaranteed measure of operational importance. Remove in stages, retrain or fine-tune if needed, and compare with an unpruned checkpoint on the same held-out target cohort. Checkpoint selection keeps the final test isolated.
Preserve the feature contract
A pruned head still expects the same incoming embedding ordering unless the encoder is also changed. If pruning reaches the encoder, output shapes and every connected consumer need versioned updates. A stale consumer can fail loudly or, worse, accept a wrong vector length after padding. The target contract pins shape and preprocessing.
Do not promise runtime speed from sparsity alone
Backends differ in whether they exploit zero blocks or smaller dense matrices. A pruned graph may serialize smaller yet run slower because of added conversion, poor kernel choice or memory movement. Benchmark artifact bytes, peak memory and p95 latency on the real serving device. The Pareto review uses observed cost, not a theoretical operations count.
Audit error after each removal
A unit with low average activation may be important for a rare package material. Compare false negatives by label, site and image type, and keep an unpruned rollback artifact. Paired slice checks capture a regression hidden by mean accuracy.
Implementation
# Two-layer inspection head: three input features, four hidden units,
# then one output. Units are columns in input_to_hidden and rows below.
input_to_hidden = [
[0.3, 0.8, -0.4, 0.2],
[0.1, -0.5, 0.7, 0.6],
[0.9, 0.2, 0.4, -0.3],
]
hidden_to_output = [[0.6], [-0.2], [0.8], [0.1]]
kept_units = (0, 2, 3)
def remove_hidden_units(first_layer, second_layer, retained):
if len(second_layer) != len(first_layer[0]):
raise ValueError("hidden width mismatch")
return ([[row[index] for index in retained] for row in first_layer],
[second_layer[index] for index in retained])
smaller_first, smaller_second = remove_hidden_units(
input_to_hidden, hidden_to_output, kept_units)
original_weights = sum(map(len, input_to_hidden)) + sum(map(len, hidden_to_output))
remaining_weights = sum(map(len, smaller_first)) + sum(map(len, smaller_second))
assert original_weights == 16
assert remaining_weights == 12Performance and operating cost
Removing H of U hidden units in a dense D-to-U-to-O head reduces weight storage from DU + UO to D(U-H) + (U-H)O. Rebuilding matrices costs O(DU + UO) time in the teaching code. Actual speed depends on backend kernels, batch size and surrounding preprocessing.
Common Mistakes
- Do not call zero-valued weights a guaranteed latency improvement.
- Do not prune one layer without updating every connected tensor and state.
- Do not select units using the final target test.
