Skip to content

ATULSHARMA1234/GreenFormer

Repository files navigation

🌱 GreenFormer

A Multi-Architecture Model Compression Framework for Sustainable AI

Python PyTorch HuggingFace License: MIT

GreenFormer is a generalized pipeline that reduces the energy consumption, carbon footprint, and inference latency of any Transformer-based NLP model through a sequential compression pipeline: Knowledge Distillation β†’ Structured Pruning β†’ INT8 Quantization (PTQ & QAT).


🎯 Key Features

  • πŸ”„ Multi-Architecture Support β€” Works across BERT, RoBERTa, ALBERT, and ELECTRA via a pluggable Model Registry
  • ⚑ 4-Stage Compression Pipeline β€” Knowledge Distillation β†’ Structured Pruning β†’ Post-Training Quantization β†’ Quantization-Aware Training
  • πŸ“Š Real-Time Energy Tracking β€” CodeCarbon integration for measuring kWh and COβ‚‚ emissions per training run
  • πŸ–₯️ Dual-Platform Benchmarking β€” Compare algorithmic vs. datacenter efficiency on Apple M2 CPU and NVIDIA T4 GPU
  • πŸ“ˆ Interactive Dashboard β€” Streamlit-based research visualization with comparative tables and charts
  • πŸ“„ Auto-Generated Reports β€” One-click Word document generation for academic submissions

πŸ—οΈ Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚         MODEL REGISTRY                   β”‚
                    β”‚  bert | roberta | albert | electra       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚       GENERALIZED PIPELINE               β”‚
                    β”‚                                          β”‚
                    β”‚  Stage 1: Baseline Teacher Training      β”‚
                    β”‚            ↓                             β”‚
                    β”‚  Stage 2: Knowledge Distillation (KD)    β”‚
                    β”‚            ↓                             β”‚
                    β”‚  Stage 3: Structured Pruning (30%)       β”‚
                    β”‚            ↓                             β”‚
                    β”‚  Stage 4a: Post-Training Quantization    β”‚
                    β”‚  Stage 4b: Quantization-Aware Training   β”‚
                    β”‚                                          β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚         OUTPUT                           β”‚
                    β”‚  β€’ Compressed INT8 model                 β”‚
                    β”‚  β€’ Energy & COβ‚‚ metrics (JSON)           β”‚
                    β”‚  β€’ ONNX export for deployment            β”‚
                    β”‚  β€’ Streamlit dashboard                   β”‚
                    β”‚  β€’ Auto-generated DOCX report            β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Š Results Summary (SST-2, NVIDIA T4 GPU)

Stage Model Accuracy Size (MB) Speedup Energy Reduction
Baseline BERT-base (Teacher) 93.1% 418 1.0Γ— β€”
Knowledge Distillation DistilBERT-6L (Student) 81.0% 255 1.9Γ— -36%
Structured Pruning Pruned DistilBERT (30%) 81.4% 255 2.1Γ— -41%
INT8 QAT Quantized DistilBERT 81.3% 91 4.7Γ— -68%

Key Finding: INT8 quantization performance is highly hardware-dependent β€” Apple M2 (CPU) shows latency increases due to lack of native INT8 support, while NVIDIA T4 (GPU) achieves significant speedups.


πŸš€ Quick Start

Prerequisites

# Clone the repository
git clone https://github.com/ATULSHARMA1234/GreenFormer.git
cd GreenFormer

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # macOS/Linux

# Install dependencies
pip install -r requirements.txt

Run the Original Pipeline (BERT only)

python run_fast_pipeline.py --task sst2

Run the Generalized Pipeline (Any Model)

# Single model
python run_generalized_pipeline.py --model bert --task sst2
python run_generalized_pipeline.py --model roberta --task cola
python run_generalized_pipeline.py --model albert --task sst2

# All models Γ— all tasks
python run_generalized_pipeline.py --model all --task both

Launch the Dashboard

streamlit run dashboard/unified_paper_dashboard.py

Generate Research Report

python generate_report.py

πŸ“ Project Structure

GreenFormer/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ model_registry.py       # πŸ”Œ Pluggable model family registry
β”‚   β”œβ”€β”€ train_baseline.py       # Stage 1: Teacher training
β”‚   β”œβ”€β”€ distillation.py         # Stage 2: Knowledge Distillation
β”‚   β”œβ”€β”€ pruning.py              # Stage 3: Structured Pruning
β”‚   β”œβ”€β”€ quantization.py         # Stage 4: PTQ & QAT
β”‚   β”œβ”€β”€ evaluation.py           # Metrics & benchmarking
β”‚   β”œβ”€β”€ energy_tracker.py       # CodeCarbon wrapper
β”‚   β”œβ”€β”€ export_onnx.py          # ONNX model export
β”‚   β”œβ”€β”€ evaluate_onnx.py        # ONNX Runtime benchmarking
β”‚   └── utils.py                # Shared utilities
β”œβ”€β”€ dashboard/
β”‚   β”œβ”€β”€ unified_paper_dashboard.py  # Main Streamlit dashboard
β”‚   β”œβ”€β”€ graph_dashboard.py          # Chart visualizations
β”‚   └── tables_dashboard.py         # Comparative tables
β”œβ”€β”€ run_generalized_pipeline.py # 🌱 Generalized multi-model pipeline
β”œβ”€β”€ run_fast_pipeline.py        # Original BERT-specific pipeline
β”œβ”€β”€ generate_report.py          # Auto-generate Word report
β”œβ”€β”€ requirements.txt
└── README.md

πŸ”Œ Supported Model Families

Family Teacher Student Parameters
BERT bert-base-uncased (110M) DistilBERT-6L (66M) 40% reduction
RoBERTa roberta-base (125M) DistilRoBERTa (82M) 34% reduction
ALBERT albert-base-v2 (11.8M) ALBERT-tiny 4L (~4M) 66% reduction
ELECTRA electra-base (110M) ELECTRA-small (14M) 87% reduction

Adding a new model requires only a new entry in src/model_registry.py β€” zero pipeline code changes.


πŸ§ͺ Benchmarking Platforms

Platform Role Hardware
Apple MacBook Air M2 Algorithmic efficiency (CPU) 8-core CPU, 8GB RAM
Google Colab / RunPod Datacenter efficiency (GPU) NVIDIA Tesla T4, 16GB VRAM

πŸ“š Techniques Implemented

Knowledge Distillation (Hinton et al., 2015)

L = Ξ± Β· TΒ² Β· KL(student_soft β€– teacher_soft) + (1 - Ξ±) Β· CE(student, labels)
  • Temperature T = 4.0, Alpha Ξ± = 0.7

Structured Pruning

  • L1-norm global unstructured pruning targeting all nn.Linear layers
  • 30% sparsity with post-pruning fine-tuning for accuracy recovery

Quantization

  • PTQ: Dynamic INT8 quantization via torch.quantization.quantize_dynamic
  • QAT: Quantization-aware training with fake quantization nodes during forward pass

Energy Measurement

  • CodeCarbon for real-time energy (kWh) and COβ‚‚ (kg) tracking
  • PUE scaling: E_total = E_IT Γ— PUE (industry avg 1.58 β†’ optimized 1.09)

πŸ› οΈ Tools & Technologies

Frameworks: PyTorch, HuggingFace Transformers, ONNX Runtime, Streamlit

Libraries: CodeCarbon, Scikit-learn, Plotly, Matplotlib, Pandas, NumPy, TensorBoard

Platforms: Python 3.10+, Google Colab (NVIDIA T4 GPU), Apple M2 CPU

Benchmarks: GLUE (SST-2 β€” Sentiment, CoLA β€” Linguistic Acceptability)


πŸ‘¨β€πŸ’» Author

Atul Sharma , Sarthak Goyal , Panya Kapoor


πŸ“ License

This project is licensed under the MIT License.


🌟 Citation

If you use GreenFormer in your research, please cite:

@software{greenformer2026,
  author = {Sharma, Atul},
  title = {GreenFormer: A Multi-Architecture Model Compression Framework for Sustainable AI},
  year = {2026},
  url = {https://github.com/ATULSHARMA1234/GreenFormer}
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors