Majid Al-RaimiAMC: pruning ratios as a reinforcement learning problem

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
Understood
0/4 concepts

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

  1. Explain why per-layer ratio selection is a combinatorial search and why sensitivity analysis is sub-optimal.
  2. Name AMC's state, action, agent and reward, and say why the action is continuous and the reward is minus error under a constraint.
  3. Describe what DDPG's actor and critic each do in one AMC episode.
  4. Read the ResNet-50 density plot: peaks at 1x1, crests at 3x3, 5x versus 3.4x at equal accuracy.
  5. 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

  1. Combinations

    Fifty layers, ten candidate ratios each: 10^50 full configurations. No amount of GPU time enumerates that.
  2. 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.
  3. 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.
  4. 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.

MethodWho chooses the ratiosCostInteraction modelledTransfers to a new model
Human expert rulesAn engineer, by trial and errorDays of experiments per modelOnly in the engineer's headNo, rules are written per model
Sensitivity analysisA curve per layer plus one thresholdLayers x ratios single-layer runsNo, one layer at a timeNo, curves belong to one network
Learned policy (AMC)A DDPG agent, layer by layerHundreds of pruned networks, no retrainingYes, the reward sees the whole networkSame method, new search per model and budget
Three ways to allocate one budget across layers

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?

The target fixes only the sum. How to split it across layers is a combinatorial search (10^50 for fifty layers with ten ratios each) whose objective, accuracy after pruning, can only be observed by trying a configuration. Sensitivity analysis sidesteps the search by sweeping one layer at a time and assuming the losses add, which ignores layer interaction and makes its ratios sub-optimal.

Quick check

Why do the ratios from sensitivity analysis end up sub-optimal?

AMC: state, action, reward and a DDPG agent

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.

One step of the loop: the embedding of layer t feeds both the actor and the critic, the actor emits a ratio that hollows out half of layer t, and after the last layer the reward returns to the critic, which scores Q(s_t, a_t).

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

R={Errorif the FLOPs or latency constraint is metotherwiseR = \begin{cases} -\text{Error} & \text{if the FLOPs or latency constraint is met} \\ -\infty & \text{otherwise} \end{cases}
Resource-constrained reward (slide 29)

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.

Wduty=αWallamaxWrestWreduced,atmax ⁣(at,  WdutyWt)W_{\text{duty}} = \alpha\, W_{\text{all}} - a_{\max} W_{\text{rest}} - W_{\text{reduced}}, \qquad a_t \leftarrow \max\!\left(a_t,\; \frac{W_{\text{duty}}}{W_t}\right)
How the paper actually enforces the budget (Algorithm 1)

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.

RFLOPs=Errorlog(FLOPs)R_{\text{FLOPs}} = -\text{Error} \cdot \log(\text{FLOPs})
Accuracy-guaranteed reward (slide 28)

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.

SimulatorAMC episode stepper: five layers, one FLOPs budget
  • L1a=0.5020M, 3x3
  • L2?60M, 3x3
  • L3?100M, 3x3
  • L4?80M, 1x1
  • L5?40M, 3x3
Embedding s_t for layer 1 (8 of the 11 features)
FeatureRawScaled to [0, 1]
t10.00
n320.13
c30.02
k3x31.00
FLOPs[t]20M0.20
reduced0M0.00
rest280M0.93
a[t-1]none0.00
kept FLOPs so far290M of 300Mbudget line at 150M
Layers pruned0of 5one action per layer
Kept so far0MFLOPsfrom the layers already pruned
Budget150MFLOPs50% of 300M
Rewardpendinguntil layer 5arrives once per episode

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.

State: the layer embedding of eleven scaled features (index, n, c, h, w, stride, k, FLOPs of the layer, FLOPs reduced so far, FLOPs remaining, previous action). Action: a continuous sparsity ratio a_t in (0, 1]. Agent: DDPG, an actor-critic method. Reward: -Error after all layers are pruned, subject to the FLOPs or latency constraint. The environment is layer-by-layer channel pruning.

Recall

Why is the reward minus error rather than accuracy, and why is it given only at the end of the episode?

RL maximizes reward, so -Error minimizes error. The budget is a constraint, not a bonus, so the agent gains nothing by over-shrinking and lands on the target. Error can only be measured once every layer has been pruned, so one episode is one pass through the network with one reward.

Recall

Why must the action space be continuous?

Accuracy is very sensitive to the exact ratio, a discrete grid fine enough to matter explodes the number of actions, and discretization loses the ordering between ratios. DDPG supports continuous actions, which is why it was chosen.

Recall

What does AMC not learn?

It does not learn which weights or channels to drop (magnitude or max-response selection inside each layer is fixed) and it does not train from scratch. It learns only the per-layer ratios, then fine-tunes the best network once.

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.

RegionHuman expertAMC
Conv150%43%
ResBlock131%28%
ResBlock231%28%
ResBlock330%23%
ResBlock430%19%
FC20%10%
Total29%20%
ResNet-50 weight density kept, human expert versus AMC (slide 30; lower is better)

Worked example

From density to compression ratio

  1. 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).
  2. 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%).
  3. 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.

Density kept along the layer index of ResNet-50 forms a sawtooth: 1x1 layers stay denser (peaks), 3x3 layers are pruned harder (crests).

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?

3x3 layers have more redundancy and are pruned harder (crests); 1x1 layers have less and are kept denser (peaks). Overall density fell to 20% against the expert's 29%, 5x versus 3.4x, at the same 76.1% top-1.

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%.

ModelMACTop-1LatencySpeedupMemory
1.0 MobileNet569M70.6%119.0 ms1x20.1 MB
AMC (50% FLOPs)285M70.5%64.4 ms1.8x14.3 MB
AMC (50% Time)272M70.2%59.7 ms2.0x13.2 MB
0.75 MobileNet325M68.4%69.5 ms1.7x14.8 MB
MobileNet on a Samsung Galaxy S7 Edge, TF-Lite, single core, batch size 1 (slide 32)

Worked example

Checking the table

  1. 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).
  2. 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.
  3. Accuracy deltas from 70.6%

    -0.1, -0.4 and -2.2 points.
  4. 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
Accuracy against measured latency: the two AMC points sit up and to the left, and the uniformly shrunk 0.75 MobileNet lies inside the region they dominate.

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?

It is constrained by measured time from a lookup table, so it removes work where the phone is actually slow rather than where FLOPs are counted, reaching 59.7 ms (2.0x) against 64.4 ms (1.8x). That allocation is less accuracy-friendly, so top-1 falls from 70.5% to 70.2%.

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