Compute savings from confidence-based early exit

Why it matters Early exit and layer skipping are used to cut decode latency for easy tokens in on-device and real-time models.

Early-exit models attach a small classifier after several layers. A token stops at the first exit whose confidence is high enough, so easy tokens use fewer layers.

Write early_exit(conf, threshold). conf has shape (N, L): conf[n, l] is the confidence of the exit after layer l + 1 for token n. Token n exits at the first layer (1-based) where conf >= threshold; if none qualifies it runs all L layers. Return (exit_layers as a list of ints, mean layers used, speedup L / mean).

Signaturedef early_exit(conf, threshold)

Example

conf [[0.5, 0.95, 0.99], [0.2, 0.3, 0.4]], threshold 0.9 gives ([2, 3], 2.5, 1.2).

Understand the idea

Early exit is like an exam where you may hand in your paper as soon as you are sure of the answer. Easy questions finish early, hard ones use the full time. In a deep network, each exit head guesses the output and reports how confident it is. Worked example: one token has confidences [0.5, 0.95, 0.99] after layers 1 to 3. With threshold 0.9, it exits after layer 2. Another token never reaches 0.9, so it runs all 3. Mean layers = (2 + 3) / 2 = 2.5, speedup = 3 / 2.5 = 1.2. Rule: exit = first l with conf >= threshold, else L. A lower threshold saves more compute but risks wrong early answers, so it is tuned on validation data.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: exampleearly_exit([[0.5, 0.95, 0.99], [0.2, 0.3, 0.4]], 0.9)
  • Sample 2: first layerearly_exit([[0.99, 0.1], [0.91, 0.99], [0.1, 0.92]], 0.9)
  • Test 3: boundaryearly_exit([[0.8, 0.9, 0.7, 0.6]], 0.9)
  • Test 4: neverearly_exit([[0.1, 0.2], [0.3, 0.4]], 0.95)