DistilBERT is a compressed transformer model created by Hugging Face using knowledge distillation, where a smaller student model is trained to mimic the behavior of the larger BERT-base teacher. It retains approximately 97% of BERT's language understanding capabilities while being 40% smaller and 60% faster at inference, making it suitable for resource-constrained environments. The core technique involves training the student on a combination of the original masked language modeling objective and a distillation loss that aligns its output logits with the teacher's softened probabilities.
Glossary
DistilBERT

What is DistilBERT?
DistilBERT is a distilled, general-purpose version of the BERT language model, designed for efficient on-device deployment.
The architecture reduces BERT's layers by 40% while maintaining the same hidden size, utilizing triplet loss during pre-training for effective knowledge transfer. Unlike task-specific models like TinyBERT, DistilBERT is a general-purpose model that can be fine-tuned for downstream NLP tasks. Its development is a foundational example within the teacher-student framework, demonstrating a practical balance between model compression and performance retention for production deployment.
Key Architectural Features of DistilBERT
DistilBERT is a distilled version of BERT that achieves significant size and speed improvements through specific architectural modifications and training techniques.
Reduced Transformer Layers
The core compression technique is a 50% reduction in the number of transformer layers. The original BERT-base model has 12 encoder layers, while DistilBERT uses only 6. This is the primary driver for the model being 40% smaller and 60% faster at inference. The student model's layers are initialized by copying every other layer from the teacher (BERT), providing a strong starting point for training.
Knowledge Distillation Loss
DistilBERT is trained using a triple-loss objective that combines:
- Distillation Loss (L<sub>ce</sub>): A Kullback-Leibler Divergence loss that forces the student's output logits to match the softened probability distribution (soft targets) from the BERT teacher model. Temperature scaling (T=2.0) is applied to the teacher's softmax to create richer, softer targets containing dark knowledge.
- Masked Language Modeling Loss (L<sub>mlm</sub>): The standard BERT pre-training objective, predicting masked tokens.
- Cosine Embedding Loss (L<sub>cos</sub>): Aligns the direction of the student and teacher's hidden state vectors, encouraging similar contextual representations.
Token-Type Embeddings & Pooler Removal
To further streamline the architecture, DistilBERT removes two specific components from BERT:
- Token-Type Embeddings: These embeddings distinguish between sentence pairs (e.g., for Next Sentence Prediction). DistilBERT omits them, as it is trained solely on the Masked Language Modeling objective, simplifying the input processing.
- Pooler Layer: The fully-connected pooler layer used for Next Sentence Prediction classification is eliminated. For downstream tasks requiring a pooled representation, the standard practice is to use the embedding of the
[CLS]token or mean/max pooling over the sequence.
Dimensionality Preservation
While the depth is halved, DistilBERT retains the same hidden dimension size (768) and vocabulary size (30522) as BERT-base. This preservation of width is crucial. It allows the student model to maintain a representational capacity similar to the teacher in each layer, facilitating more effective knowledge transfer. The feed-forward network dimension within each transformer block also remains at 3072.
Initialization via Teacher Weight Copying
DistilBERT's training begins not from random initialization, but from a smart initialization derived from the BERT teacher. Specifically, the 6 layers of the student are initialized by copying weights from alternating layers of the 12-layer BERT teacher (e.g., student layer 1 from teacher layer 2, student layer 2 from teacher layer 4, etc.). This provides the student with a functional, pre-trained starting point, significantly accelerating and stabilizing the distillation process.
Training Regime & Data
DistilBERT is pre-trained on the same corpus as BERT: Wikipedia and BookCorpus, using the Masked Language Modeling objective only. The training leverages:
- Large Batch Sizes: Effective batch size of 4000 sequences.
- Dynamic Masking: A new masking pattern is generated for each sequence each time it is fed to the model, unlike BERT's static masking.
- No Next Sentence Prediction: Simplifies the pre-training task, aligning with the removed architectural components. This focused training, combined with distillation, allows it to retain 97% of BERT's language understanding capability (as measured on the GLUE benchmark) despite the architectural reduction.
DistilBERT vs. BERT: A Technical Comparison
A direct comparison of the original BERT base model and its distilled counterpart, DistilBERT, across architectural, performance, and deployment metrics.
| Feature / Metric | BERT (bert-base-uncased) | DistilBERT (distilbert-base-uncased) |
|---|---|---|
Core Compression Technique | N/A (Baseline) | Knowledge Distillation |
Number of Transformer Layers | 12 | 6 |
Total Parameters | ~110 million | ~66 million |
Model Size Reduction | Baseline | ~40% |
Inference Speedup | Baseline | ~60% faster |
GLUE Score (Average) | ~79.6 | ~77.0 (97% of BERT) |
Token Embedding Dimension | 768 | 768 |
Attention Heads per Layer | 12 | 12 |
Hidden Layer Dimension | 768 | 768 |
Pre-training Objective | Masked LM + NSP | Masked LM + Distillation Loss (Cosine) |
Typical Use Case | Maximum accuracy, server/cloud | Efficient deployment, mobile/edge, latency-sensitive |
Primary Training Cost | High (massive compute) | Lower (leverages pre-trained teacher) |
Frequently Asked Questions
DistilBERT is a cornerstone of efficient natural language processing, demonstrating the power of knowledge distillation to create compact, high-performance models. These FAQs address its core mechanisms, trade-offs, and practical applications.
DistilBERT is a general-purpose, distilled version of the BERT language model that uses knowledge distillation to create a smaller, faster model while retaining most of the original's language understanding capabilities. It works by training a smaller student model (DistilBERT) to mimic the behavior of the larger teacher model (BERT). The primary training objective is logits distillation, where DistilBERT is trained to match the softened output probabilities (soft targets) from BERT's final layer, using a Kullback-Leibler divergence loss. Additionally, it employs triplet loss objectives: a distillation loss (teacher vs. student), a masked language modeling loss (on the original training data), and a cosine embedding loss to align the directions of the student and teacher's hidden states. This multi-objective approach transfers the dark knowledge—the nuanced inter-class relationships learned by BERT—into a 6-layer Transformer architecture that is 40% smaller and 60% faster at inference.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
DistilBERT is a product of knowledge distillation. These related terms define the core techniques, architectures, and loss functions that enable training a smaller, faster student model to mimic a larger teacher.
Knowledge Distillation
Knowledge distillation is the overarching model compression technique where a smaller student model is trained to replicate the behavior and outputs of a larger, pre-trained teacher model. The core idea is to transfer the teacher's learned representations and decision boundaries, often encapsulated in its softened output probabilities (soft targets), which contain richer information than hard class labels. This process allows the student to achieve comparable performance with a fraction of the parameters and computational cost.
Teacher-Student Framework
The teacher-student framework is the foundational two-model architecture for knowledge distillation. It consists of:
- A teacher model: A large, accurate, and often cumbersome pre-trained model (e.g., BERT-base). Its role is to provide supervisory signals.
- A student model: A smaller, more efficient architecture (e.g., DistilBERT) designed for deployment. It is trained to mimic the teacher. The framework defines the direction of knowledge flow and the training objectives used to align the student's predictions with the teacher's.
Logits Distillation & KL Divergence
Logits distillation is the most common form of distillation, where the student is trained to match the teacher's raw, pre-softmax output logits. The primary loss function used is the Kullback-Leibler (KL) Divergence, which measures how one probability distribution diverges from a second, reference distribution. The training objective combines:
- Distillation Loss (KL Divergence): Minimizes the difference between the softened logit distributions of teacher and student.
- Standard Cross-Entropy Loss: Ensures the student also learns from the original hard labels. Temperature scaling (parameter T) is applied to the softmax to soften the teacher's output distribution, making the "dark knowledge" between classes more accessible.
Feature & Attention Distillation
Beyond logits, knowledge can be transferred from intermediate representations. Feature distillation (or hint learning) trains the student to match the teacher's intermediate feature maps or activations from specific layers, often using a mean squared error loss. For transformer models like BERT, attention distillation is particularly effective. Here, the student is trained to replicate the self-attention matrices (or patterns) from the teacher's layers, forcing it to learn similar contextual relationships between tokens. DistilBERT employs a combination of these losses alongside logits distillation.
TinyBERT & Task-Specific Distillation
TinyBERT is a prominent, task-specific distilled version of BERT that achieves even greater compression than DistilBERT. Its key innovation is a two-stage distillation process:
- General Distillation: Distills BERT's knowledge during pre-training on a large corpus.
- Task-Specific Distillation: Further distills the model during fine-tuning on a downstream task (e.g., sentiment analysis). This approach applies transformer-layer distillation (attention and hidden states) at both stages, resulting in a model that is highly optimized for a specific use case and extremely efficient for on-device deployment.
Online Distillation & Self-Distillation
These are variants of the standard distillation paradigm:
- Online Distillation: The teacher and student models are co-trained simultaneously from scratch, rather than using a static, pre-trained teacher. This can be more efficient and allows both models to improve collaboratively.
- Self-Distillation: A model serves as both its own teacher and student. A common technique is born-again networks, where a student with the same architecture as the teacher is trained via distillation to outperform its teacher, often through iterative cycles. Another form distills knowledge from deeper layers to shallower layers within the same network.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us