# Neurosurgery for Neural Networks - How we Cut a Language Model in Half ...

- Speaker: [Tomer Asida](/wiki/speakers/tomer-asida.md)
- Event: [Reversim Summit 2021](/wiki/years/2021.md)
- Video: https://www.youtube.com/watch?v=SdSqQMwgw_4
- Transcript: [/transcripts/2021/32-SdSqQMwgw_4.md](/transcripts/2021/32-SdSqQMwgw_4.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Machine Learning](/wiki/topics/machine-learning.md), [AI and LLMs](/wiki/topics/generative-ai.md), [Performance](/wiki/topics/performance.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

This talk explores how to compress a massive deep learning language model without sacrificing output quality to improve application latency. The speaker details a process of removing layers from a transformer-based model, nicknamed lobotomy, and retraining it using knowledge distillation. Through careful evaluation of meaning preservation, diversity, and fluency, the speaker demonstrates how a smaller model can achieve performance comparable to its larger predecessor. Ultimately, the audience learns practical compression techniques for large language models to make user-facing text generation faster and more efficient.

## Key points

- [4:25](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=265s) Low latency is critical for text assistance products to prevent user frustration and maintain conversational flow.
- [5:32](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=332s) Paraphrase quality must be evaluated based on two main metrics: meaning preservation and diversity.
- [7:43](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=463s) An automated classifier and machine translation metrics like BLEU can be repurposed to measure semantic meaning and text diversity.
- [13:21](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=801s) Transformer models are deep and sequential, meaning that fewer layers would result in faster computation and reduced memory usage.
- [14:33](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=873s) The speaker introduces 'lobotomy', a process of removing layers from a large language model to create a smaller and faster version.
- [20:01](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=1201s) Visual inspection of results revealed that the compressed model lacked fluency, proving that automated metrics alone are insufficient.
- [24:33](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=1473s) Knowledge distillation using KL divergence loss helps transfer the rich probability distribution of the large model into the smaller model.
- [27:48](https://www.youtube.com/watch?v=SdSqQMwgw_4&t=1668s) Combining layer removal, knowledge distillation, and a two-stage training process successfully produces a fast, high-quality small model.

## Takeaways

- Use model compression techniques like layer pruning and knowledge distillation to significantly reduce latency and memory consumption.
- Always manually inspect model outputs rather than relying solely on automated evaluation metrics.
- Repurpose academic metrics from other domains, such as machine translation, to evaluate your specific machine learning use cases.

## Questions this talk answers

- How can you compress a large language model without losing output quality?
- What is knowledge distillation and how is it used in training smaller neural networks?
- How do you effectively evaluate paraphrase quality using both automated metrics and manual inspection?

## Technologies

Python, PyTorch

## Tags

deep learning, knowledge distillation, latency, machine learning, model compression, nlp, transformers
