Not All Layers of LLMs Are Necessary During Inference

Siqi Fan, Xin Jiang, Xiang Li, Xuying Meng, Peng Han, Shuo Shang, Aixin Sun, Yequan Wang, Zhongyuan Wang

TL;DR

Large Language Models (LLMs) waste significant compute by processing every input through all layers, regardless of difficulty. This paper introduces AdaInfer, a parameter-free, instance-aware early stopping algorithm that uses lightweight statistical classifiers to halt inference as soon as an intermediate layer achieves the final answer. Across models like Llama 2 and OPT, AdaInfer reduces computational costs by an average of 17.8% (and up to 43% on simpler tasks) with less than a 1% drop in accuracy.

What Problem Does This Paper Address?

LLM inference is notoriously resource-intensive, scaling linearly with the number of layers ($L$). However, not all user prompts require the same cognitive depth; a simple sentiment classification task shouldn't demand the same computational budget as a complex math word problem.

Standard efficiency methods like model pruning or weight sparsification alter the model's parameters, risking a degradation in generalizability and in-context learning capabilities. The authors ask: Can we dynamically determine the difficulty of an input during the forward pass and stop processing once the answer is clear, without modifying the underlying LLM?

Figure 1: Accuracies obtained by inference at each decoder layer with the Llama2-7B model (32 layers). The solid line represents the sentiment analysis task, and the dashed line represents the MMLU task.

Methods

The authors propose AdaInfer (Adaptive Inference), a data-driven early stopping strategy that acts as an "early exit" mechanism.

  1. Feature Selection: At each decoding layer, AdaInfer extracts two simple, universal logit-based features:
  2. Top Prob: The probability of the most likely next token.
  3. Gap: The difference in probability between the top token and the second most likely token ($P(\text{top}) - P(\text{second})$).
  4. Classification: These features are fed into a lightweight classical classifier, such as a Support Vector Machine (SVM) or Conditional Random Field (CRF). The classifier solves a binary problem at each layer: Is the prediction at this intermediate layer identical to what the final layer would produce? If the classifier predicts a match, inference terminates early, bypassing all subsequent layers.
Figure 2: An illustration of AdaInfer’s processing and computational savings.

Datasets

The method was evaluated on several standard benchmarks spanning different task types:

  • Question Answering / Reasoning: MMLU (Massive Multitask Language Understanding, covering 57 subjects), CommonsenseQA, and SQuAD (reading comprehension).
  • Text Classification: SST-2 (sentiment analysis) and AG News (news categorization).

These tasks were tested across mainstream decoder-only models: OPT-13B, Llama 2-7B, Llama 2-13B, and Llama 2-70B under zero-shot and few-shot settings.

Key Insights and Ideas

  • Logits are Universal Difficulty Gauges: Features like "gap" and "top prob" consistently increase and stabilize across different tasks and models as the network becomes more confident. This makes them highly reliable, model-agnostic signals for early exiting.
  • Task-Level and Instance-Level Variability: "Easy" tasks (like sentiment analysis) allow the model to reach final-layer accuracy much earlier (e.g., layer 21 out of 40 for Llama2-13B) than "hard" tasks (like MMLU), which require deeper reasoning. Even within the same task, simpler sentence structures exit earlier than complex ones.
  • Over-thinking is Real: In some cases, AdaInfer actually outperforms the full dense model. This suggests that deeper layers can sometimes over-represent or over-complicate certain instances, leading to a performance drop if not halted early.
Figure 3: Changes of feature values along the 40 decod- ing layers in Llama2-13B model.

Key Results

  • Compute Savings: AdaInfer achieves an average layer pruning ratio of 17.8% across all tasks, peaking at a 43% reduction on sentiment analysis tasks.
  • Negligible Performance Impact: The accuracy drop across all evaluated tasks remains strictly under 1% compared to the full dense baseline, vastly outperforming static pruning methods like ShortGPT.
  • Wall-Clock Speedups: In real-world testing (using NVIDIA V100 GPUs), AdaInfer achieved up to a 1.30x wall-clock speedup on sentiment tasks for Llama2-13B. The overhead of running the SVM classifier and calculating the logit features is minimal, accounting for less than 0.04% of total FLOPs.

Limitations and Future Work

  • Single Token Focus: Currently, AdaInfer is evaluated primarily on classification and short-answer tasks where a single forward pass determines the outcome. The authors note that it has not yet been extended to sequential, multi-token text generation tasks.
  • Scaling Challenges: While AdaInfer excels on 7B and 13B models, the authors observed a performance drop (1% to 25%) in few-shot settings when scaling up to Llama 2-70B, indicating a need for more sophisticated feature engineering at massive scales.
  • Feature Exploration: The current model relies heavily on logits; incorporating other internal representations (like hidden states or attention maps) without increasing computational overhead remains an open challenge.

Summary updated

BibTeX
@misc{fan2024not,
  author = {Siqi Fan and Xin Jiang and Xiang Li and Xuying Meng and Peng Han and Shuo Shang and Aixin Sun and Yequan Wang and Zhongyuan Wang},
  title = {{Not All Layers of LLMs Are Necessary During Inference}},
  year = {2024},
  eprint = {2403.02181},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.02181},
}
Figure