[Paper Review] TwinBERT: Distilling Knowledge to Twin-Structured BERT Models for Efficient Retrieval
TwinBERT proposes a twin-structured BERT model that decouples query and document encoding, enabling document embeddings to be pre-computed and cached, which reduces inference time to ~20ms on CPUs. By using knowledge distillation and efficient network designs, TwinBERT achieves BERT-Base-level performance with 77–663× faster inference than BERT-Base and BERT-Large, enabling low-latency deployment in production IR systems.
Pre-trained language models like BERT have achieved great success in a wide variety of NLP tasks, while the superior performance comes with high demand in computational resources, which hinders the application in low-latency IR systems. We present TwinBERT model for effective and efficient retrieval, which has twin-structured BERT-like encoders to represent query and document respectively and a crossing layer to combine the embeddings and produce a similarity score. Different from BERT, where the two input sentences are concatenated and encoded together, TwinBERT decouples them during encoding and produces the embeddings for query and document independently, which allows document embeddings to be pre-computed offline and cached in memory. Thereupon, the computation left for run-time is from the query encoding and query-document crossing only. This single change can save large amount of computation time and resources, and therefore significantly improve serving efficiency. Moreover, a few well-designed network layers and training strategies are proposed to further reduce computational cost while at the same time keep the performance as remarkable as BERT model. Lastly, we develop two versions of TwinBERT for retrieval and relevance tasks correspondingly, and both of them achieve close or on-par performance to BERT-Base model. The model was trained following the teacher-student framework and evaluated with data from one of the major search engines. Experimental results showed that the inference time was significantly reduced and was firstly controlled around 20ms on CPUs while at the same time the performance gain from fine-tuned BERT-Base model was mostly retained. Integration of the models into production systems also demonstrated remarkable improvements on relevance metrics with negligible influence on latency.
Motivation & Objective
- To address the high inference latency of BERT in real-time information retrieval (IR) systems.
- To enable efficient online serving of deep neural models in low-latency environments such as sponsored search.
- To maintain high retrieval and relevance performance while drastically reducing computational cost.
- To explore knowledge distillation techniques that preserve performance while improving inference efficiency.
- To enable deployment of dense semantic models on CPUs without sacrificing relevance quality.
Proposed method
- TwinBERT uses two separate BERT-like encoders to process queries and documents independently, decoupling the input encoding process from BERT’s standard concatenation approach.
- Document embeddings are pre-computed offline and cached in memory, eliminating run-time encoding for documents during inference.
- A crossing layer combines query and document embeddings using either cosine similarity or a residual network to compute relevance scores.
- Knowledge distillation is applied with a BERT-Base model as the teacher to train TwinBERT, preserving performance while reducing model complexity.
- The model is optimized for CPU inference using ONNX Runtime, minimizing serving overhead in production systems.
- Efficient network components and training strategies are designed to reduce computational cost without significant performance drop.
Experimental results
Research questions
- RQ1Can decoupling query and document encoding in BERT reduce inference latency while maintaining high retrieval performance?
- RQ2To what extent can knowledge distillation preserve BERT-level performance in a smaller, faster model for IR tasks?
- RQ3How effective is pre-computing and caching document embeddings in reducing run-time computation in retrieval systems?
- RQ4Can TwinBERT achieve sub-20ms inference latency on CPUs while matching BERT-Base performance in relevance ranking?
- RQ5What is the impact of TwinBERT on production search systems in terms of latency, accuracy, and deployment feasibility?
Key findings
- TwinBERT achieved an average inference time of approximately 20ms on CPUs when scoring 100 documents per query, significantly reducing latency compared to BERT.
- With pre-computed document embeddings, TwinBERT was 77× and 422× faster than BERT-Base and BERT-Large, respectively, in inference speed.
- The model retained 90% or more of the incremental performance gain from fine-tuned BERT-12 in production A/B tests, with a 10%+ reduction in bad ad impressions.
- TwinBERT models were successfully deployed in a major sponsored search system, demonstrating negligible latency impact and high relevance quality.
- The cosine similarity version of TwinBERT achieved 121× and 663× speedup over BERT-3 and BERT-12, respectively, under the same conditions.
- Even when query embeddings were re-computed at run-time, TwinBERT remained faster than BERT-3, highlighting its efficiency advantage.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.