CoolFace
Apppublic

Nagachaitanya47/bert-compression-poc

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

๐Ÿ”ฌ BERT Compression POC

A hands-on demonstration of ML model compression applied to a pretrained DistilBERT sentiment classifier.

Covers three core compression concepts: Quantization, with benchmarks comparing FP32 baseline vs INT8 quantized model across accuracy, inference speed, and model size.


What This Project Does

Takes a pretrained DistilBERT model fine-tuned on SST-2 sentiment analysis, applies Dynamic INT8 Post-Training Quantization using PyTorch, and measures the real-world impact across three dimensions:

MetricBaseline (FP32)Quantized (INT8)
Model Size~255 MB~65 MB
Avg Inference Time~120 ms~40 ms
Accuracy (SST-2)~91%~90.3%
Speedupโ€”~3x faster
Size Reductionโ€”~75% smaller
Actual numbers will vary slightly based on hardware. Run `compare.py` to see your results.

Concepts Demonstrated

Quantization โ€” Reducing model weight precision from FP32 (32-bit float) to INT8 (8-bit integer). Four times fewer bytes per parameter. Runs faster on hardware with integer arithmetic units (most modern CPUs and mobile NPUs).

Dynamic vs Static Quantization โ€” This POC uses dynamic quantization, which is the standard approach for transformer/NLP models. Weights are pre-quantized; activations are quantized at runtime. No calibration dataset required.

Accuracy-Efficiency Trade-off โ€” Demonstrates that aggressive compression (4x size reduction) results in less than 1% accuracy degradation โ€” the core insight from the model compression literature.


Project Structure

bert-compression-poc/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ load_model.py     # Load DistilBERT from HuggingFace
โ”‚   โ”œโ”€โ”€ evaluate.py       # Accuracy + inference time measurement
โ”‚   โ”œโ”€โ”€ quantize.py       # INT8 quantization pipeline
โ”‚   โ””โ”€โ”€ compare.py        # Side-by-side results table
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ baseline/         # Saved FP32 model
โ”‚   โ””โ”€โ”€ quantized/        # Saved INT8 model
โ”œโ”€โ”€ results/
โ”‚   โ””โ”€โ”€ comparison.json   # Benchmark output
โ”œโ”€โ”€ notebook/
โ”‚   โ””โ”€โ”€ demo.ipynb        # Full walkthrough notebook
โ”œโ”€โ”€ app.py                # Gradio UI โ€” live comparison
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

Setup

Requirements: Python 3.10+, pip

bash
# Clone the repo
git clone https://github.com/YOUR_USERNAME/bert-compression-poc.git
cd bert-compression-poc

# Install dependencies
pip install -r requirements.txt

How to Run

Step 1 โ€” Download and save the baseline model

bash
python src/load_model.py

Step 2 โ€” Apply INT8 quantization

bash
python src/quantize.py

Step 3 โ€” Run the benchmark comparison

bash
python src/compare.py

Step 4 โ€” Launch the Gradio UI

bash
python app.py
# Open http://localhost:7860

Or โ€” open the notebook for a full walkthrough

bash
jupyter notebook notebook/demo.ipynb

Key Findings

  • โ€”Dynamic INT8 quantization achieves ~3x inference speedup on CPU with less than 1% accuracy drop
  • โ€”Model size reduces from ~255 MB to ~65 MB โ€” a ~75% reduction
  • โ€”No retraining required โ€” compression is applied post-training in under 30 seconds
  • โ€”PyTorch's quantize_dynamic supports this natively with 3 lines of code

Tech Stack

  • โ€”PyTorch โ€” quantization engine
  • โ€”HuggingFace Transformers โ€” DistilBERT model and tokenizer
  • โ€”HuggingFace Datasets โ€” SST-2 evaluation data
  • โ€”Gradio โ€” interactive comparison UI

Further Reading

  • โ€”Hinton et al. (2015) โ€” Knowledge Distillation: https://arxiv.org/abs/1503.02531
  • โ€”Jacob et al. (2018) โ€” Quantization for efficient inference: https://arxiv.org/abs/1712.05877
  • โ€”Sanh et al. (2019) โ€” DistilBERT: https://arxiv.org/abs/1910.01108
  • โ€”PyTorch Quantization Docs: https://pytorch.org/docs/stable/quantization.html

Built as part of MCA (AI/ML Specialisation) minor project research on ML model compression for edge deployment.