COE 592Lecture 4.2Part 04
AMC: pruning ratios as a reinforcement learning problem
Given an overall compression target, AMC lets a DDPG agent choose each layer's sparsity, rewarded by accuracy under a FLOPs or latency constraint, and beats hand-tuned and uniformly scaled MobileNets on a real phone.
- Concepts
- 4
- Slides
- 24-32
- Reading
- 24 min
Why this part matters
Your research models will have to meet a latency or FLOPs budget on one specific device, and someone has to decide how much of that budget each layer gives up. Part 03 showed the hand method. This part shows the first method that learns the answer, and it is the one the exam will ask you to formulate.
AMC (He et al., ECCV 2018) is the canonical example of turning a hand-tuned hyperparameter search into a learned policy. You will see why the search is hard, how the four ingredients of reinforcement learning map onto pruning, what the agent discovered on ResNet-50 that no expert had written down, and a table of phone measurements that settles the argument between counting FLOPs and measuring time.
By the end you can
- Explain why per-layer ratio selection is a combinatorial search and why sensitivity analysis is sub-optimal.
- Name AMC's state, action, agent and reward, and say why the action is continuous and the reward is minus error under a constraint.
- Describe what DDPG's actor and critic each do in one AMC episode.
- Read the ResNet-50 density plot: peaks at 1x1, crests at 3x3, 5x versus 3.4x at equal accuracy.
- Interpret the MobileNet table: measured time versus FLOPs targets, and why both beat uniform 0.75 shrinking.
Take a ResNet-50 and allow each of its roughly fifty prunable layers to keep one of ten possible fractions of its weights. That is 10^50 ways to spend one overall budget. The Sensitivity analysis of part 03 escapes the explosion by never looking at combinations at all: it prunes one layer at a time, runs 50 x 10 = 500 single-layer experiments, and reads each layer's Pruning ratio off its own curve where it crosses the accuracy threshold.
The saving comes from a hidden assumption: that the accuracy lost by pruning several layers at once is the sum of what each loses alone. The AMC authors tested exactly this and report that the sensitivity approach "assumes that errors of different pruned layers can be summed up linearly, which does not stand according to our experiments" (He et al., 2018). That is the Layer interaction that slide 24 says the analysis ignores. Pruning layer 3 changes what layer 4 receives, so the curve you measured for layer 4 on a dense network is the wrong curve once layer 3 is thin. The ratios are usable, but they are sub-optimal, and they belong to the network they were measured on. Change the architecture and every curve must be measured again.
Worked example
Counting the search space
Combinations
Fifty layers, ten candidate ratios each: 10^50 full configurations. No amount of GPU time enumerates that.What sensitivity analysis actually tries
One layer at a time: 50 x 10 = 500 pruned networks, each with only one layer changed. It then picks the ratios by a threshold, never trying the chosen combination until the very end.What is never measured
Any configuration in which two or more layers are pruned together. The compounding of part 03 (both layers at once losing more than either alone) lives entirely in the unmeasured region.A first-order estimate, not an optimum
Sensitivity analysis is a valid linear approximation. The per-layer rates it produces are a starting point, not the best allocation of the budget.
The bottleneck is people
Slide 25 shows the practical consequence. Conventionally the allocation relies on human expertise and trial and error. Three customers with three engineers is a workable shop. Many customers overwhelm even a team of engineers (the red cross and the alarmed manager in the middle column), and every new model, new target device or new budget restarts the trial and error from scratch, because rules such as prune the first layer less and the fully connected layers more are, in the paper's words, "non-optimal, and doesn't transfer from one model to another" (He et al., 2018). The right column serves the same crowd with one engineer and an engine that takes the model and the constraint and returns the compressed network.
Slide 26 names the ambition: a push-the-button solution. Today an efficient network needs someone who is both a machine learning expert and a hardware expert. The goal is that a non-expert plus a hardware-centric AutoML tool reaches the same efficient network. This is the definition of Automated pruning in this lecture: an algorithm, not a person, chooses the per-layer ratios for an overall target, and it does so with the target hardware in the loop.
| Method | Who chooses the ratios | Cost | Interaction modelled | Transfers to a new model |
|---|---|---|---|---|
| Human expert rules | An engineer, by trial and error | Days of experiments per model | Only in the engineer's head | No, rules are written per model |
| Sensitivity analysis | A curve per layer plus one threshold | Layers x ratios single-layer runs | No, one layer at a time | No, curves belong to one network |
| Learned policy (AMC) | A DDPG agent, layer by layer | Hundreds of pruned networks, no retraining | Yes, the reward sees the whole network | Same method, new search per model and budget |
Recall
An overall compression target is given. Why is choosing the per-layer ratios still a hard problem, and what does sensitivity analysis do about it?
Quick check
Why do the ratios from sensitivity analysis end up sub-optimal?
Follow one pass of AMC over MobileNet. Layer t-1 has just been pruned at 30%. The agent now looks at layer t: it receives a short vector describing that layer, its index, its channel counts, its kernel size, its FLOPs, how many FLOPs earlier layers already gave up and how many remain in later layers. It answers with one number, say 50%. The environment removes half of that layer's channels by magnitude and presents layer t+1. When the last layer is done, the whole pruned network is evaluated on a held-out set, and a single number comes back: minus the error. That number is the only feedback the agent ever gets, and it arrives once per pass.
That story contains every ingredient of a reinforcement learning problem, which is what slide 27 means by "pruning as a reinforcement learning problem". The paper's own description: the agent "processes the network in a layer-wise manner", "receives a layer embedding s_t", "outputs a precise compression ratio a_t", and after every layer is compressed the "validation accuracy of the pruned model with all layers compressed is evaluated without fine-tuning, which is an efficient delegate of the fine-tuned accuracy" (He et al., 2018). Only the best network found after the search is fine-tuned.
The four RL ingredients in AMC, plus two words the exam expects around them
- State
- The Layer embedding of layer t: eleven features (t, n, c, h, w, stride, k, FLOPs[t], reduced, rest, a[t-1]), each scaled to [0, 1]. The slide's [N, C, H, W, i, ...] abbreviates this list.
- Action
- One continuous number, the sparsity ratio a_t in (0, 1], for the current layer only.
- Agent
- DDPG, an actor-critic method: the actor proposes a_t from s_t, the critic estimates the value Q(s_t, a_t) of that choice.
- Environment
- Channel pruning of the pretrained network, one layer per step, using magnitude selection inside the layer.
- Reward
- R = -Error when the FLOPs or latency budget holds, effectively minus infinity otherwise (slide 29). For accuracy-guaranteed search, R = -Error x log(FLOPs) (slide 28).
- Episode
- One full pass over all layers, producing one pruned network and one reward. The signal is validation accuracy before fine-tuning, on a few thousand training images.
Why the action must be continuous, and why an actor and a critic
A pruning ratio is a real number, and the compressed model's accuracy is very sensitive to it. A discrete agent would need a grid fine enough to tell 48% from 52%, which explodes the number of actions, and a grid also throws away the order between neighbouring ratios (He et al., 2018), so the agent could not exploit the fact that neighbouring ratios behave alike. DDPG (Lillicrap et al., 2015) is the standard actor-critic method for continuous control: it concurrently learns a Q-function and a policy and can only be used with continuous action spaces (OpenAI Spinning Up). Sutton and Barto give the naming: when a value function is used "to assess, or criticize, the policy's action selections, then the value function is called a critic and the policy is called an actor". In AMC the critic learns, from replayed transitions, how much reward a state and ratio pair leads to; the actor is then nudged by gradient ascent on that estimate to propose ratios the critic scores higher. The critic judges, the actor proposes, and neither ever sees a gradient of the pruned network itself.
Why the reward is minus error under a constraint
Reinforcement learning maximizes reward, so maximizing -Error is minimizing error. The budget appears as a constraint rather than a bonus on purpose: if the agent were paid for shrinking, it would keep shrinking past the target and lose accuracy for nothing. With a hard constraint there is no reward for finishing under budget, so the agent "can precisely arrive at the target compression ratio" (He et al., 2018). The minus infinity branch is the slide's compact way of saying an infeasible network earns nothing useful.
A wasted episode teaches the critic nothing, so the paper never lets one happen. Before each action it computes the duty: the share of the removal target alpha W_all that this layer must carry, given what earlier layers already removed (W_reduced) and the most the remaining layers could remove at the cap a_max (0.8 for convolutions and 0.98 for fully connected layers in fine-grained pruning; 0.8 for every layer in channel pruning). If the actor's proposal is too timid, it is raised to the duty. The budget is therefore met by construction, and the minus infinity case is a formal backstop, not a common outcome.
Slide 28 shows the other protocol. When the goal is the smallest model with no accuracy loss, the budget is unknown in advance, so the reward itself must push on size. The bubble chart on the slide (Canziani et al., 2016) shows that top-1 accuracy rises roughly with the logarithm of the operation count, so multiplying -Error by log(FLOPs) gives, in the paper's words, "a small incentive for reducing FLOPs" while staying "sensitive to Error". The ResNet-50 and MobileNet results in the next two concepts use the constrained reward, not this one.
The constraint need not be FLOPs. The paper notes that the resource can be "FLOPs or the actual inference time on mobile device", and slide 29 adds the mechanism: a pre-built Latency lookup table of measured per-layer times on the target phone, the same device-in-the-loop idea NetAdapt uses in part 05. Substituting measured time for FLOPs turns a FLOPs-constrained search into a latency-constrained one, and the MobileNet table at the end of this part shows why that matters.
- L1a=0.5020M, 3x3
- L2?60M, 3x3
- L3?100M, 3x3
- L4?80M, 1x1
- L5?40M, 3x3
| Feature | Raw | Scaled to [0, 1] |
|---|---|---|
| t | 1 | 0.00 |
| n | 32 | 0.13 |
| c | 3 | 0.02 |
| k | 3x3 | 1.00 |
| FLOPs[t] | 20M | 0.20 |
| reduced | 0M | 0.00 |
| rest | 280M | 0.93 |
| a[t-1] | none | 0.00 |
Toy numbers. Error is 30 + 100 x sum of s_j a_j^2 with sensitivities 0.20, 0.06, 0.04, 0.05, 0.10, so the first and last layers are expensive to prune. The budget bar projects the unpruned layers at full cost until you act on them.
Recall
Name the four RL ingredients of AMC and what each is concretely.
Recall
Why is the reward minus error rather than accuracy, and why is it given only at the end of the episode?
Recall
Why must the action space be continuous?
Recall
What does AMC not learn?
Quick check
Which fact makes AMC choose DDPG rather than a discrete-action agent?
Quick check
Under a FLOPs budget, what reward does the agent get when the pruned model breaks the budget?
Slide 30 puts the agent up against Song Han's own hand-pruned ResNet-50 from his PhD thesis. The human expert kept 29% of the weights, a 3.4x reduction. The agent kept 20%, a 5x reduction, at the same accuracy: 76.13% top-1 for the original and 76.11% for AMC (He et al., 2018). This experiment uses fine-grained weight pruning, not channel pruning, and it is run as Iterative pruning in four rounds at 50%, 35%, 25% and 20% overall density with 30 fine-tuning epochs after each round, which is the schedule part 06 explains.
| Region | Human expert | AMC |
|---|---|---|
| Conv1 | 50% | 43% |
| ResBlock1 | 31% | 28% |
| ResBlock2 | 31% | 28% |
| ResBlock3 | 30% | 23% |
| ResBlock4 | 30% | 19% |
| FC | 20% | 10% |
| Total | 29% | 20% |
Worked example
From density to compression ratio
Human expert
Density 29%, so compression 1 / 0.29 = 3.4x. The thesis records the rules behind it: first layer 50%, residual blocks about 30%, fully connected layer 20% (Han, 2017).AMC
Density 20%, so compression 1 / 0.20 = 5.0x, with the biggest gains in the late blocks (30% to 19% in ResBlock4) and the classifier (20% to 10%).Same accuracy, 1.5x fewer weights
76.13% versus 76.11% top-1, a difference well inside run-to-run noise, for 5x instead of 3.4x.
What the agent noticed inside a residual block
The per-region bars hide the interesting pattern, which slide 31 shows layer by layer. ResNet-50 is built from bottleneck blocks: a 1x1 convolution that reduces the channel count, a 3x3 convolution, and a 1x1 convolution that restores it (He et al., 2016). Walk along the layer index and the kept density forms a sawtooth. The peaks, where the agent kept more, are the 1x1 layers. The crests, where it cut deep, are the 3x3 layers. The paper draws the conclusion directly: the agent "automatically learns that 3x3 convolution has more redundancy than 1x1 convolution and can be pruned more" (He et al., 2018). Nobody told it about kernel shapes; the kernel size k is just one of the eleven features in the state.
The finding makes sense once you think about where redundancy lives. A 1x1 kernel mixes channels and has no spatial extent, so every weight is a distinct channel-to-channel connection with nothing next to it to stand in for it. A 3x3 kernel has nine spatial taps per channel pair, many of which are near zero after training, and it also holds most of a block's weights: in a stage-3 bottleneck the 3x3 layer carries about 590K of the block's 1.1M parameters against 262K for each 1x1 layer (Han, 2017). Cutting the 3x3 layers hard is where the compression is, and it is also where accuracy is cheapest to spend, which is exactly the trade the reward rewards.
Recall
What did AMC learn about 3x3 versus 1x1 convolutions in ResNet-50, and what did that buy?
Quick check
Inside the residual blocks of ResNet-50, which layers did the agent keep densest?
The final slide moves from weights to milliseconds. MobileNet is run with TF-Lite on a Samsung Galaxy S7 Edge with a Qualcomm Snapdragon SoC, single core, batch size 1, which is the latency-oriented setting of a phone serving one image at a time. Four models are measured: the full 1.0 MobileNet, two AMC results and the 0.75 MobileNet baseline, which is the same architecture with every layer's width scaled by 75%.
| Model | MAC | Top-1 | Latency | Speedup | Memory |
|---|---|---|---|---|---|
| 1.0 MobileNet | 569M | 70.6% | 119.0 ms | 1x | 20.1 MB |
| AMC (50% FLOPs) | 285M | 70.5% | 64.4 ms | 1.8x | 14.3 MB |
| AMC (50% Time) | 272M | 70.2% | 59.7 ms | 2.0x | 13.2 MB |
| 0.75 MobileNet | 325M | 68.4% | 69.5 ms | 1.7x | 14.8 MB |
Worked example
Checking the table
Speedups
119.0 / 64.4 = 1.85 (shown as 1.8x), 119.0 / 59.7 = 1.99 (2.0x), 119.0 / 69.5 = 1.71 (1.7x).MAC fractions
285 / 569 = 0.50, 272 / 569 = 0.48, 325 / 569 = 0.57. The width multiplier scales cost by roughly alpha^2 = 0.5625 (Howard et al., 2017), which matches the 0.57.Accuracy deltas from 70.6%
-0.1, -0.4 and -2.2 points.Both AMC rows dominate the uniform baseline
Each AMC model has fewer MACs, lower latency, less memory and higher accuracy than 0.75 MobileNet. Halving FLOPs gave 1.85x, not 2x; targeting time came closest, at 1.99x.
Reading the two AMC rows against each other
- Latency
- 50% Time is 4.7 ms faster (59.7 versus 64.4 ms)
- Accuracy
- 50% Time loses 0.3 points (70.2% versus 70.5%)
- MACs
- 272M versus 285M: the time target also ends up slightly smaller
- Against 0.75 MobileNet
- Both are at least 1.8 points more accurate, at least 5 ms faster, with fewer MACs
Two lessons in one table
First, FLOPs are a proxy. Halving the multiply-accumulates bought 1.85x, not 2x, because a phone does not spend time in proportion to arithmetic. MobileNet's depthwise layers have a low ratio of computation to memory traffic, so trimming them saves fewer milliseconds than the MAC count suggests; the paper reports about 2x speedup on the 1x1 convolutions but less on the depthwise ones (He et al., 2018). When the agent is instead constrained by measured time from the lookup table, it removes work where the phone is actually slow, and the search lands at 59.7 ms, a 1.99x speedup that all but hits the 2x target. That allocation is less accuracy-friendly, which is the 0.3 point cost. You choose the target you care about, and you pay in the other currency.
Second, non-uniform beats uniform. Uniform shrinking with the width multiplier is a single knob turned on every layer, and the thinner model must be trained from scratch (Howard et al., 2017). AMC prunes a pretrained network with a different ratio per layer, keeps 2.1 (50% FLOPs) or 1.8 (50% Time) more points of accuracy with fewer MACs than 0.75 MobileNet, and does it after a short fine-tune. In the accuracy versus latency plane the AMC points sit above and to the left, which is the definition of a better Pareto frontier.
Recall
In the MobileNet table, why does 50% Time beat 50% FLOPs on latency but lose 0.3 points?
Quick check
Why does AMC (50% Time) run faster than AMC (50% FLOPs) on the Galaxy S7 Edge?
Recap
If you remember nothing else
- An overall compression ratio fixes the sum; allocating it across layers is a search whose objective is only observable by trying it.
- Sensitivity analysis assumes per-layer losses add. Layers interact, so its ratios are sub-optimal and do not transfer between models.
- AMC: state = layer embedding of 11 scaled features, action = continuous sparsity ratio, agent = DDPG, reward = minus error once all layers are pruned, budget enforced as a constraint.
- Continuous actions because ratios are real numbers and grids lose ordering; the critic scores Q(s, a), the actor proposes a.
- AMC learns only the ratios. Magnitude selection inside each layer and one final fine-tune are unchanged.
- ResNet-50: 20% density (5x) versus the expert's 29% (3.4x) at 76.1% top-1; 3x3 layers are pruned harder than 1x1 layers.
- MobileNet on a Galaxy S7 Edge: 50% FLOPs gives 64.4 ms at 70.5%, 50% time gives 59.7 ms at 70.2%, both beating 0.75 MobileNet at 69.5 ms and 68.4%.
- Targeting measured time lands within a whisker of the speed goal; targeting FLOPs is only a proxy.
Sources
- AMC: AutoML for Model Compression and Acceleration on Mobile DevicesPaperECCV 2018, He, Lin, Liu, Wang, Li and HanEleven-feature state, continuous action, rewards, Algorithm 1, ResNet-50 density, Pixel 1 MobileNet table. DOI 10.1007/978-3-030-01234-2_48.(opens in a new tab)
- 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 4: Pruning and Sparsity Part IIDocsMIT HAN Lab, Fall 2023Source of slides 27 to 32 and of the Galaxy S7 Edge measurements.(opens in a new tab)
- Continuous control with deep reinforcement learningPaperLillicrap et al., 2015DDPG: an actor-critic, model-free algorithm for continuous action spaces.(opens in a new tab)
- Deep Deterministic Policy GradientDocsOpenAI Spinning UpLearns a Q-function and a policy concurrently; only for continuous actions; off-policy; actor trained by ascent on Q.(opens in a new tab)
- Reinforcement Learning: An Introduction, 2nd editionBookMIT Press 2018, Sutton and BartoChapter 13: the critic assesses the policy's action selections, the policy is the actor.(opens in a new tab)
- Efficient Methods and Hardware for Deep LearningPaperStanford PhD thesis 2017, Song HanTable 3.8: human-expert ResNet-50 densities, 3.4x; per-layer parameter counts of a bottleneck block.(opens in a new tab)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperHoward et al., 2017Table 6: 1.0 MobileNet 569M and 70.6%, 0.75 MobileNet 325M and 68.4%; the width multiplier thins every layer uniformly and needs retraining.(opens in a new tab)
- Deep Residual Learning for Image RecognitionPaperHe, Zhang, Ren and Sun, CVPR 2016The bottleneck block: 1x1 reduce, 3x3, 1x1 restore.(opens in a new tab)
- An Analysis of Deep Neural Network Models for Practical ApplicationsPaperCanziani, Paszke and Culurciello, 2016The accuracy versus operations bubble chart on slide 28; accuracy rises roughly with log operations.(opens in a new tab)
- mit-han-lab/amcDocsGitHubReference implementation of AMC.(opens in a new tab)