albert - a lite bert
# ALBERT - A Lite BERT
## Introduction & Motivation
ALBERT: lightweight BERT via parameter reduction. Factorized embeddings, cross-layer parameter sharing. Applications: mobile deployment, efficient inference.
Motivation: Reduce model size and computational cost.
Applications: Edge deployment, real-time inference, mobile NLP.
---
## Core Concepts & Theory
### Factorized Embeddings
Separate word and hidden dimension sizes.
### Cross-Layer Parameter Sharing
Share weights across transformer layers.
### Sentence Ordering Prediction
Replace NSP with SOP task.
### Sparse Computation
Reduced FLOPs via architecture design.
---
## Mathematical Formulation
Parameter Reduction:
$$ ext{params}_{ ext{ALBERT}} = V \cdot e + T(H^2 + 4H^2)$$
vs BERT:
$$ ext{params}_{ ext{BERT}} = V \cdot H + T(H^2 + 4H^2)$$
Factorization:
$$W = W_2 W_1 ext{ where } W_1 \in \mathbb{R}^{D imes e}, W_2 \in \mathbb{R}^{e imes D}$$
---
## Advanced Theory & Extensions
### Knowledge Distillation
Compress via teacher.
### Multi-Head Attention Sharing
Share attention heads.
### Layer-Wise Adaptation
Fine-tune specific layers.
---
## Computational Considerations
Embedding projection: O(V·e).
Shared transformer: O(T·H²).
Total: ~60% parameter reduction.
---
## Practical Implementation Strategies
### Hyperparameter Factorization
Choose embedding vs hidden dimensions.
### Layer Sharing
Periodic vs full sharing.
### Mixed Precision
FP16 training for efficiency.
---
## Benchmark Datasets & Evaluation
GLUE: Text understanding.
SQuAD: Reading comprehension.
RACE: Multiple choice QA.
---
## Key Challenges & Limitations
### Task Performance Degradation
Slight accuracy loss for efficiency.
### Parameter Sharing Bottleneck
Shared parameters limit expressiveness.
### Training Instability
Harder to train than BERT.
---
## Hyperparameter Tuning
Embedding dimension: 64-128.
Hidden dimension: 256-768.
Sharing pattern: Full or periodic.
---
## Real-World Applications & Case Studies
Mobile Applications: On-device NLP.
Edge Devices: IoT deployment.
Fast Inference: Low-latency systems.
---
## Integration with Other Methods
ALBERT + knowledge distillation; + quantization for further compression.
---
## Summary & Key Takeaways
ALBERT achieves efficiency through parameter factorization and sharing.
Principles:
1. Factorization: Reduce embedding dimension.
2. Sharing: Reuse transformer layers.
3. SOP: Better pre-training task.
4. Efficiency: 70% fewer parameters than BERT.
5. Deployment: Mobile and edge ready.
---
## Appendix: Practical Labs
### Lab 1: Factorized Embeddings
import numpy as np
def factorized_embedding(vocab_size, hidden_dim, embedding_dim):
"""Create factorized embedding: V*E + E*H instead of V*H"""
# Word embedding to lower dimension
word_embed = np.random.randn(vocab_size, embedding_dim) * 0.01
# Lower dimension to hidden dimension
project = np.random.randn(embedding_dim, hidden_dim) * 0.01
params_factorized = vocab_size * embedding_dim + embedding_dim * hidden_dim
params_standard = vocab_size * hidden_dim
return params_factorized, params_standard
vocab = 30000
hidden = 768
embed = 128
fact_params, std_params = factorized_embedding(vocab, hidden, embed)
assert fact_params < std_params
print(f"✓ Factorized: {fact_params}, Standard: {std_params}")### Lab 2: Cross-Layer Sharing
class SharedAlbertLayer:
def __init__(self, hidden_dim):
self.weights = np.random.randn(hidden_dim, hidden_dim) * 0.01
def forward(self, x):
return x @ self.weights
np.random.seed(42)
shared_layer = SharedAlbertLayer(768)
num_layers = 12
# Same weights reused
outputs = [shared_layer.forward(np.random.randn(256, 768)) for _ in range(num_layers)]
assert len(outputs) == num_layers
print("✓ Cross-layer sharing working")### Lab 3: SOP Loss
import numpy as np
def sentence_order_prediction_loss(sent_a, sent_b, predictions):
"""SOP: predict if sentence pair is in correct order"""
labels = np.array([1, 0]) # 1 for correct order, 0 for reversed
loss = -np.mean(labels * np.log(predictions + 1e-8))
return loss
np.random.seed(42)
sent_a = np.random.randn(768)
sent_b = np.random.randn(768)
preds = np.array([0.8, 0.2]) # Model prediction
loss = sentence_order_prediction_loss(sent_a, sent_b, preds)
assert loss > 0
print(f"✓ SOP loss: {loss:.4f}")### Lab 4: Parameter Count Reduction
def compare_parameter_counts(vocab_size, hidden_dim, num_layers):
"""Compare BERT vs ALBERT parameter counts"""
# BERT
bert_embed = vocab_size * hidden_dim
bert_layers = num_layers * (hidden_dim * hidden_dim * 2)
bert_total = bert_embed + bert_layers
# ALBERT (with factorization)
embed_dim = 128
albert_embed = vocab_size * embed_dim + embed_dim * hidden_dim
albert_layers = 1 * (hidden_dim * hidden_dim * 2) # Shared
albert_total = albert_embed + albert_layers
reduction = 1 - (albert_total / bert_total)
return reduction
reduction = compare_parameter_counts(30000, 768, 12)
assert reduction > 0.5
print(f"✓ Parameter reduction: {reduction:.1%}")---