TinyBERT is a distilled transformer model that transfers knowledge from a large BERT teacher to a smaller student architecture through a two-stage framework: general distillation during pre-training and task-specific distillation during fine-tuning. Unlike methods focusing only on output logits, it distills the teacher's embedding layer, attention matrices, and hidden states, enabling the student to learn the teacher's internal representations and achieve competitive performance with significantly reduced size and latency.
Glossary
TinyBERT

What is TinyBERT?
TinyBERT is a highly compressed version of the BERT language model, created through a specialized two-stage knowledge distillation process to achieve high performance with a fraction of the parameters.
The process employs attention-based distillation and hidden state distillation losses to align the student's intermediate layers with the teacher's. This comprehensive transfer allows TinyBERT to maintain strong performance on GLUE benchmark tasks while being 7.5x smaller and 9.4x faster than BERT-base. It is a foundational technique in the inference optimization pillar, demonstrating efficient model compression for deployment in resource-constrained environments.
Key Features of TinyBERT
TinyBERT is a distilled version of BERT that employs a two-stage distillation framework—during both general pre-training and task-specific fine-tuning—transferring knowledge from the teacher's embedding layer, attention layers, and prediction layer to achieve high performance with a significantly reduced parameter count.
Two-Stage Distillation Framework
TinyBERT's core innovation is its two-phase distillation process, which occurs at both the general-domain pre-training and task-specific fine-tuning stages. This ensures the student model learns both broad linguistic knowledge and specialized task capabilities from the teacher BERT model. The framework transfers knowledge at multiple levels, leading to a more robust and capable compressed model than single-stage distillation.
Multi-Layer Knowledge Transfer
Instead of just mimicking final outputs, TinyBERT distills knowledge from three critical layers of the teacher BERT model:
- Embedding Layer Output: Aligns the student's token and segment embeddings with the teacher's.
- Attention Matrices: Transfers the rich contextual relationships captured in the teacher's self-attention layers.
- Hidden States & Prediction Layer: Aligns intermediate feature representations and final output logits. This comprehensive transfer captures the teacher's representational capacity and reasoning patterns.
Attention-Based Distillation Loss
A key component is the attention-based loss function, which minimizes the Mean Squared Error (MSE) between the student's and teacher's attention matrices. For a transformer with h attention heads and a sequence length of l, this loss is calculated as:
Loss_att = 1/h * Σ (MSE(Teacher_Att_i, Student_Att_i))
This forces the student to learn which tokens the teacher deems important for context, effectively transferring its linguistic focus and syntactic understanding.
Significant Model Compression
TinyBERT achieves dramatic size and speed improvements. The TinyBERT₄ variant, with 4 transformer layers, contains approximately 14.5 million parameters, which is 7.5x smaller and 9.4x faster than the base BERTբᴀsᴇ model (110M parameters). Despite this reduction, it retains over 96% of BERT's performance on the GLUE benchmark. This makes it highly suitable for edge deployment and low-latency inference scenarios.
Transformer Layer Distillation
The distillation process carefully maps layers between the teacher and student. For a student with M layers and a teacher with N layers (where M < N), student layer m is typically aligned with teacher layer n = m * N / M. This layer mapping strategy ensures the student's shallower network learns a condensed version of the teacher's deep feature hierarchy. The loss includes hidden state MSE between these mapped layers.
Task-Agnostic & Task-Specific Distillation
The framework is designed for flexibility:
- General Distillation: Uses a large, unlabeled corpus (e.g., Wikipedia) to produce a task-agnostic TinyBERT model with broad language understanding.
- Task-Specific Distillation: Further distills this general model on labeled data for downstream tasks (e.g., sentiment analysis, natural language inference). This second stage specializes the model efficiently, as it builds upon the already-distilled general knowledge, requiring fewer task-specific examples.
TinyBERT vs. Other Distilled Models
A feature and performance comparison of TinyBERT against other prominent knowledge distillation techniques for transformer models.
| Feature / Metric | TinyBERT | DistilBERT | DeiT (Image) | General KD |
|---|---|---|---|---|
Core Distillation Strategy | Two-stage (pre-training & task-specific) | Single-stage (pre-training only) | Single-stage (CNN teacher) | Varies (often task-specific only) |
Knowledge Transfer Layers | Embedding, Attention, Prediction (full transformer) | Prediction layer (logits) primarily | Prediction layer (logits) | Prediction layer (logits) or features |
Teacher Model Type | BERT-base (or larger) | BERT-base | Convolutional Neural Network (e.g., RegNet) | Any pre-trained model |
Primary Training Signal | Combined loss: attention, embedding, hidden states, logits | Soft target loss (KL Divergence) + MLM loss | Hard distillation (label smoothing) or soft targets | Soft target loss (KL Divergence) |
Parameter Reduction vs. Teacher | ~7.5x smaller (BERT-base -> TinyBERT-4L-312D) | ~1.7x smaller (BERT-base -> DistilBERT) | Architecturally similar to teacher | Defined by student architecture |
Inference Speedup (Approx.) | ~9.4x faster | ~1.6x faster | Comparable to teacher ViT | Proportional to size reduction |
Typical Performance Retention (GLUE avg.) |
| ~97% of BERT-base | Surpasses teacher CNN on ImageNet | Highly variable |
Architecture Flexibility | Fixed student architecture (fewer layers, smaller hidden dim) | Fixed student architecture (fewer layers) | Uses standard ViT architecture | Any student architecture |
Requires Task-Specific Data for Distillation |
Frequently Asked Questions
A concise FAQ addressing common technical questions about TinyBERT, a distilled version of BERT optimized for efficient inference.
TinyBERT is a knowledge-distilled version of the BERT language model that employs a novel two-stage distillation framework to transfer knowledge from a larger teacher model to a smaller, more efficient student model. It works by distilling knowledge during both the general pre-training phase and the task-specific fine-tuning phase, transferring information not just from the teacher's final output logits but also from its embedding layer, attention layers (via attention transfer), and the hidden states of the Transformer layers. This comprehensive approach allows the student model to learn the teacher's rich internal representations, achieving high performance with a significantly reduced parameter count and faster inference speed.
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
TinyBERT is a specific implementation within the broader field of model distillation. These related concepts define the techniques, components, and alternative approaches for transferring knowledge from large models to efficient, deployable ones.
Knowledge Distillation (KD)
Knowledge Distillation (KD) is the overarching model compression technique where a smaller student model is trained to mimic the predictive behavior of a larger teacher model. The core innovation is training the student not just on hard ground-truth labels, but on the teacher's softened output probabilities (soft targets), which contain richer dark knowledge about inter-class relationships.
- The primary training objective is the distillation loss, often a weighted combination of standard cross-entropy and a mimicry loss like Kullback-Leibler Divergence Loss.
- Temperature scaling is applied to the teacher's logits to create a smoother probability distribution for the student to learn from.
- TinyBERT is a direct application of KD, extending it with a two-stage framework and attention-based losses.
Teacher & Student Models
The teacher model is a large, pre-trained, high-performance network (e.g., BERT-base) that serves as the source of knowledge. Its role is to generate the training signals—logits, attention maps, hidden states—for the student.
The student model is the target compact network (e.g., TinyBERT) designed for efficient deployment. Its architecture is significantly smaller (fewer layers, hidden dimensions) and is trained to replicate the teacher's functionality. The success of distillation hinges on the student's capacity to absorb the teacher's knowledge despite its reduced parameter count.
Attention Transfer & Feature-Based Distillation
Beyond mimicking final outputs, feature-based distillation techniques train the student to replicate the teacher's internal representations. Attention Transfer is a key method in this category, highly relevant to transformer models like BERT and TinyBERT.
- In transformers, attention maps reveal how the model weighs the importance of different input tokens.
- TinyBERT's distillation loss explicitly includes an attention-based loss, forcing the student's multi-head attention matrices to align with the teacher's.
- This ensures the student learns not just what the teacher predicts, but how it contextually focuses on the input to make those predictions.
DistilBERT
DistilBERT is a contemporaneous and widely adopted distilled version of BERT, serving as a key benchmark and alternative to TinyBERT.
- Primary Difference: DistilBERT uses a single-stage distillation objective focused primarily on matching the teacher's output logits (using KL divergence loss) and hidden states (using cosine embedding loss).
- Architecture: It reduces the size by 40% (6 layers vs. BERT's 12) and increases speed by 60%.
- TinyBERT Contrast: TinyBERT employs a more comprehensive two-stage distillation (pre-training and task-specific) and includes explicit attention map matching, often leading to better performance at similar or smaller sizes.
Quantization-Aware Distillation (QAD)
Quantization-Aware Distillation (QAD) is a joint optimization technique that combines distillation with model quantization. While TinyBERT reduces model size via architectural distillation, QAD addresses precision reduction.
- In QAD, the student model is trained to be robust to the numerical noise and precision loss that occurs when its weights and activations are converted to lower-bit formats (e.g., INT8).
- The distillation process can occur before quantization (training a full-precision student that quantizes well) or during simulated quantization.
- This technique is complementary: a model like TinyBERT can first be created via architectural distillation and then further compressed via QAD for ultimate edge deployment.
Self-Distillation & Born-Again Networks
Self-distillation is a variant where the teacher and student are architecturally identical or closely related. Born-Again Networks (BAN) are a seminal example, where a student model iteratively learns from the predictions of its predecessor.
- In BANs, the student from generation n+1 is trained using the soft labels from the student at generation n, often leading to performance gains beyond the original teacher.
- Contrast with TinyBERT: TinyBERT uses a static, pre-trained teacher (BERT) and a permanently smaller student architecture. Self-distillation focuses on iterative self-improvement or distilling knowledge within the same model (e.g., from deep to shallow layers) without a fixed capacity gap.

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