TL;DR
SHARCS (Sample Hardness Aware Routing based on Confidence Scores) is a novel adaptive inference framework that improves Transformer efficiency by dynamically routing inputs to sub-networks of varying widths. By predicting the "hardness" of a sample and using narrower sub-networks for easier inputs, SHARCS achieves up to a 2x speedup on CPUs with minimal accuracy loss, outperforming traditional early-exiting methods.
What Problem Does This Paper Address?
Large-scale Transformer models like BERT and RoBERTa are powerful but computationally expensive, making them difficult to deploy on resource-constrained devices. Most existing efficiency techniques—like pruning, quantization, or distillation—are "static," meaning they apply the same amount of computation to every input regardless of its complexity.
However, not all inputs are created equal. A simple sentiment analysis task might find "I love this movie!" trivial, while a nuanced, sarcastic review requires more "brainpower." Adaptive inference aims to solve this by adjusting compute per sample. While previous methods focused on early-exiting (skipping layers) or token dropping (reducing sequence length), SHARCS explores a third, complementary dimension: dynamic width.
Methods
The core of SHARCS is a lightweight router that sits between a fixed "non-adaptive" set of initial layers and a "dynamic" set of adaptive layers.
- Estimating Hardness: Since there are no ground-truth labels for how "hard" a sentence is, the authors use the model's own training history. If the model consistently predicts the correct class with high confidence over a moving window of training epochs, the sample is labeled as "easy."
- The Router: A small neural network is trained to predict these hardness labels. At inference time, the router looks at the intermediate representation from the first few layers and selects a "reduction factor" (e.g., 0.25x, 0.5x, or 1.0x width).
- Dynamic Width Sub-networks: The adaptive part of the model consists of layers that can operate at different widths. If the router picks a 0.25x reduction, the input is "pooled" (down-sampled) to a smaller dimension, processed by narrow Multi-Head Attention (MHA) and Feed-Forward Network (FFN) modules, and then "unpooled" back to the original dimension for final classification.
The width reduction is achieved by retaining only the most important neurons and attention heads, as illustrated in the figure below:
Datasets
The authors evaluated SHARCS on the standard GLUE (General Language Understanding Evaluation) benchmark, which includes eight diverse classification tasks:
- MNLI-m (Natural Language Inference)
- QNLI (Question Answering/Inference)
- QQP (Quora Question Pairs)
- SST-2 (Sentiment Analysis)
- MRPC & RTE (Paraphrase/Entailment)
- CoLA (Linguistic Acceptability)
- WNLI (Winograd Schema)
Key Insights and Ideas
- Width is a Viable Adaptive Dimension: Most adaptive research focuses on depth. SHARCS proves that reducing the hidden dimension (width) is an effective way to save FLOPs (Floating Point Operations) while maintaining high accuracy.
- Hardness is a Moving Target: By using a moving window of training history to define hardness, the authors mitigate the stochastic nature of training, leading to a more stable and reliable router.
- Synergy with Token Dropping: Because SHARCS modifies width and methods like Transkimmer modify sequence length, they can be used together. Combining them leads to even greater efficiency gains than either method alone.
- Model Agnostic: SHARCS isn't just for standard BERT; it can be applied to already-compressed models like DistilBERT or DynaBERT to squeeze out even more performance.
Key Results
SHARCS consistently outperforms existing per-sample adaptive methods like DeeBERT, FastBERT, and PABEE across the GLUE benchmark.
- Efficiency Gains: On the QQP dataset, SHARCS reduced the FLOPs of RoBERTa-base by 2.75x with only a 1% drop in accuracy.
- Real-world Latency: In CPU-based inference tests, SHARCS achieved a 2.57x speedup on BERT-base with less than 1% accuracy loss.
- Superior Trade-offs: As shown in the plots below, SHARCS (the blue line) stays higher and further to the left than competing methods, indicating better accuracy for the same computational cost.
Limitations and Future Work
- Architecture Scope: The current study focuses exclusively on Transformer encoders (like BERT). The authors note that extending this to decoder-only (GPT-style) and encoder-decoder (T5-style) models is a key direction for future work.
- Task Scope: While applicable to classification, the paper does not provide results for regression tasks, which would require minor adjustments to the hardness estimation heuristic.
- Hardware Optimization: While CPU speedups are significant, further work could explore how these dynamic widths interact with specialized hardware accelerators (like TPUs or specific GPU kernels) that often prefer static shapes.
Summary updated