Search

Search papers and lessons

Adaptive Computation Time

Level 4 · 12 min read

Adaptive computation time lets a network learn how many internal steps to spend on each input, by adding a halting unit and a small penalty for each step taken.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

A standard network does the same amount of work on every input. Adaptive computation time (ACT) lets it decide, for each input, how many internal steps to run before it produces an output. A small "halting" unit outputs a number between 0 and 1 after each step, and the network stops once the running total of these numbers is close enough to 1. The output is a weighted average of the intermediate states, and a small penalty on the number of steps keeps the network from thinking forever.

Builds on: Recurrent Neural Network, Sigmoid Function, Weighted Average · Leads to: adaptive-depth transformers and other models that spend more steps on harder inputs (no lesson node yet)

Where it's used

Sequence models that refine a state. A recurrent network, often built from LSTM cells, reads one input at a time and keeps a state. ACT lets the network take several internal updates at one time step, so a hard input can get more refinement than an easy one. Graves introduced the method in 2016 for recurrent networks.

Transformers that vary depth per position. The Universal Transformer, a variant of the Transformer, applies one shared block repeatedly and uses ACT to decide, separately for each token position, how many times to apply it. An easy word can be finished early while a hard one keeps being refined, so the same model handles tokens of different difficulty without a fixed depth.

Early stopping as a budget. Any system that has to meet a latency budget, such as a language model answering on a phone, benefits from a rule that says "stop when you are confident". ACT gives a trainable form of this rule, where the network learns the confidence signal instead of relying on a fixed threshold chosen by hand.

How it works

Take one input at one time step, and a state that is a single number so that every value can be checked by hand. The halting unit gives probabilities p1, p2, p3, and so on, one per internal step. The values below are picked by hand so the arithmetic is visible; the math section shows how a real network computes them. We stop when the running total reaches 0.99, which means the threshold is 1 minus a small number epsilon, here epsilon = 0.01.

  1. Start the step. The network takes the input and the current state and produces a new internal state s1. Suppose s1 = 1.
  2. Read the halting unit. The halting unit turns s1 into a number p1 between 0 and 1. Suppose p1 = 0.2. Add it to the running total: total = 0.2. The total is below 0.99, so keep going.
  3. Take the next internal step. The state updates to s2 = 2 and the halting unit gives p2 = 0.3. The total is 0.2 + 0.3 = 0.5. Still below 0.99, so keep going.
  4. Take another step. The state updates to s3 = 3 and p3 = 0.4. The total is 0.5 + 0.4 = 0.9. Still below 0.99, so keep going.
  5. Take the step that crosses the threshold. The state updates to s4 = 4 and p4 = 0.5. The total would be 1.4, which is at least 0.99, so the network stops here. This is step N = 4.
  6. Use the remainder for the last step. The probabilities already used sum to 0.9, so the last state gets the leftover weight R = 1 minus 0.9 = 0.1. The weights are 0.2, 0.3, 0.4 and 0.1. They add up to 1.
  7. Output a weighted average. The output is 0.2 times 1, plus 0.3 times 2, plus 0.4 times 3, plus 0.1 times 4, which is 0.2 + 0.6 + 1.2 + 0.4 = 2.4.
  8. Charge for the steps. The penalty for this input is N plus R, which is 4 + 0.1 = 4.1. Training adds a small multiple of this to the loss, so steps cost something.

The check after step 7 is that the weights sum to 1, so the output is an average and not a scaled-up value. The check after step 8 is that a cheap input, one that crosses the threshold at step 1, pays N + R = 1 + 1 = 2, which is still less than the 4.1 paid by the four-step input.

The diagram shows the loop for one input.

flowchart TD
  A["Input and current state"] --> B["Take one internal step: new state s_n"]
  B --> C["Halting unit gives p_n between 0 and 1"]
  C --> D{"Running total of p at least 1 minus epsilon?"}
  D -- no --> B
  D -- yes --> E["Output: weighted average of states, last weight is the remainder"]
  E --> F["Penalty: steps taken plus remainder"]

The math

Halting unit (step 2). For each internal step n on one input at time t, the network computes a state s and a halting value. With a weight vector W_h and a bias b_h,

pt n=σ ⁣(Wh st n+bh),p_t^{\,n} = \sigma\!\left(W_h\, s_t^{\,n} + b_h\right),

where sigma is the Sigmoid Function, which squashes any number into the range from 0 to 1. The sigmoid of 0 is 0.5, which is why a fresh halting unit starts near the middle.

Stopping rule (steps 3 to 5). Let epsilon be a small threshold. The number of steps N(t) is the first n at which the running total reaches 1 minus epsilon:

N(t)=min⁡{ n  :  ∑m=1npt m≥1−ε }.N(t) = \min\Big\{\, n \;:\; \sum_{m=1}^{n} p_t^{\,m} \ge 1 - \varepsilon \,\Big\}.

The remainder is what the probabilities before the last step did not cover:

R(t)=1−∑m=1N(t)−1pt m.R(t) = 1 - \sum_{m=1}^{N(t)-1} p_t^{\,m}.

In the worked example, N(t) = 4 and R(t) = 0.1.

Output (step 7). The weight for the state at step n is beta^n, where beta^n = p^n for n less than N(t), and beta^N = R(t). These weights sum to 1, so the output is a weighted average:

yt=∑n=1N(t)βt n st n.y_t = \sum_{n=1}^{N(t)} \beta_t^{\,n}\, s_t^{\,n}.

In the example, the weights are (0.2, 0.3, 0.4, 0.1) and the states are (1, 2, 3, 4), which gives 2.4.

Penalty (step 8). The ponder cost for one input is rho(t) = N(t) + R(t). The total loss is the task loss plus a multiplier tau times the sum of rho(t) over all inputs in the batch:

L=∑tLtask(yt)+τ∑t(N(t)+R(t)).\mathcal{L} = \sum_t \mathcal{L}_{\text{task}}(y_t) + \tau \sum_t \big( N(t) + R(t) \big).

The multiplier tau is a hyperparameter. A large tau pushes the network toward few steps, a small one lets it think longer when it helps the task.

Why the remainder matters. Without R(t), the weights would not sum to 1 and the output would be shrunk toward zero. With R(t), the last state always gets exactly the weight that is missing, so the output stays on the same scale whatever the number of steps.

Worked examples

Example
Example 1: a tiny input that halts in two steps

Suppose epsilon = 0.01, so the threshold is 0.99. The halting values are p1 = 0.5 and p2 = 0.5. The states are s1 = 1 and s2 = 3.

  1. After step 1, the running total is 0.5. This is below 0.99, so continue.
  2. After step 2, the running total is 0.5 + 0.5 = 1.0. This is at least 0.99, so N = 2.
  3. The remainder is R = 1 minus the first probability, which is 1 minus 0.5 = 0.5.
  4. The weights are beta^1 = p1 = 0.5 and beta^2 = R = 0.5. They sum to 1.
  5. The output is 0.5 times 1 plus 0.5 times 3, which is 0.5 + 1.5 = 2.
  6. The ponder cost is N + R = 2 + 0.5 = 2.5.

Interpretation: the output is the midpoint of the two states, and the input cost two steps and a remainder.

Example
Example 2: the standard case, a four-step input

This is the walkthrough from "How it works": p = (0.2, 0.3, 0.4, 0.5), states s = (1, 2, 3, 4), epsilon = 0.01.

  1. The running totals are 0.2, 0.5, 0.9 and 1.4.
  2. The first total at least 0.99 is 1.4, at n = 4, so N = 4.
  3. The remainder is R = 1 minus 0.9 = 0.1.
  4. The weights are (0.2, 0.3, 0.4, 0.1), which sum to 1.0.
  5. The output is 0.2 + 0.6 + 1.2 + 0.4 = 2.4.
  6. The ponder cost is 4 + 0.1 = 4.1.

Interpretation: the network spent four steps, and the output leans toward the later states because they got the larger halting values.

Example
Example 3: a batch with an easy and a hard input (try it)

A batch has two inputs. Input A has halting values p1 = 0.995 at its first step, and epsilon = 0.01. Input B is the walkthrough case from Example 2 with ponder cost 4.1. The penalty multiplier is tau = 0.01.

  1. For input A, the running total after step 1 is 0.995, which is at least 0.99. So N = 1 for input A.
  2. The remainder sums only the probabilities of the steps before N. There are none, so R = 1 minus 0 = 1. Input A's output is its first state with full weight.
  3. Input A's ponder cost is N + R = 1 + 1 = 2. Input B's is 4.1 from Example 2.
  4. The total ponder cost is 2 + 4.1 = 6.1, and the penalty is tau times 6.1 = 0.01 times 6.1 = 0.061.

Try it: What is the total penalty for the batch?

Answer: 0.061. Input A costs 2 and input B costs 4.1, so the sum is 6.1, and 0.01 times 6.1 is 0.061. Input A pays little in steps but the remainder still makes its ponder cost 2, which shows the penalty is a per-input constant plus the steps, not only the steps.

Common mistakes and tradeoffs

It is tempting to think that ACT makes the network "decide" in a human sense. In fact the halting value is a number the network learns to produce because gradient descent on the loss rewards it, and the penalty pushes it toward fewer steps. A network can learn to halt at the wrong time if the task loss does not reward the extra steps, so the penalty multiplier tau has to be tuned.

It is also tempting to think that more steps always means better output. In fact the weighted average can blur the states together when the halting values are spread out, so the output of a long run can be worse than a short one. The remainder keeps the scale right but does not guarantee quality.

ACT gives variable cost per input, which is useful when inputs differ in difficulty. It is less useful when every input needs the same work, because the halting unit then adds parameters and a training signal for no gain. It also makes batch processing harder on hardware that prefers equal-sized work, because inputs in a batch can finish at different steps. Alternatives include a fixed depth with early exits at chosen layers, routing methods such as mixture-of-experts layers, and later stochastic versions that model the halting step as a probability distribution, such as PonderNet.

History

Standard recurrent networks did one fixed update per input token. Graves (2016) asked whether the network could learn the number of steps itself, and gave a differentiable rule based on the halting unit and the penalty described above. The Universal Transformer (2018) applied the same idea per token position, which showed that it also works with attention blocks rather than only recurrent cells.

Recap

  • A halting unit gives a value between 0 and 1 after each internal step, computed from the current state with a sigmoid.
  • The network stops once the running total reaches 1 minus epsilon. The number of steps depends on the input.
  • The output is a weighted average of the intermediate states. The last state gets the remainder, so the weights sum to 1.
  • A penalty on steps plus remainder keeps the cost of thinking visible in the loss.
  • Read next: Recurrent Neural Network if you need the state update, and Backpropagation Through Time to see how gradients flow through the steps.

Check yourself

QuestionWhat does the halting unit output, and what function produces it?Show answer

It outputs a single number between 0 and 1 for each internal step. It is computed from the current state by a linear map followed by the sigmoid function, which squashes any real number into the range from 0 to 1.

QuestionHalting values are 0.3, 0.4 and 0.5, with a threshold of 0.99. How many steps does the network take, and what is the remainder?Show answer

The running totals are 0.3, 0.7 and 1.2. The first total at least 0.99 is 1.2, at step 3, so N = 3. The remainder is 1 minus 0.7, which is 0.3. The number of steps is 3 and the remainder is 0.3.

QuestionWhy does ACT include the remainder R in the output weights instead of just using the halting values?Show answer

The halting values before the last step do not sum to 1, so using them alone would give weights that add up to less than 1 and would shrink the output toward zero. The remainder fills the gap so that the weights sum to 1 and the output is a proper weighted average, whatever the number of steps.

QuestionA team sets tau very large for a language model that answers short questions and long questions. What do you expect, and what would you change?Show answer

A very large tau makes every extra step expensive, so the network learns to halt early, including on long questions that need more steps. The model would then answer hard questions with too little computation. You would lower tau so that the task loss can outweigh the penalty when extra steps help, and check the step counts on long and short inputs separately.

QuestionIn a Universal Transformer, why might ACT help more for tokens than for whole sentences?Show answer

Each token position gets its own halting decision, so an easy word such as a common function word can stop early while a rare or ambiguous word keeps being refined. A single decision for the whole sentence would force all tokens to use the same number of steps, and the easy tokens would waste computation.

Sources