A simple and efficient Python toolkit for measuring and scoring ML model energy consumption using CodeCarbon.
ML Energy Score provides:
- Energy consumption measurement for any ML model
- Star-based efficiency scoring to compare models
- Support for multiple hardware platforms (CPU, GPU, Apple Silicon)
- Integration with CodeCarbon for accurate energy tracking
- JSON-formatted results for easy analysis
As AI models grow in size and complexity, their energy consumption and environmental impact increase significantly. ML Energy Score helps organizations make informed decisions about model selection by quantifying energy efficiency, enabling:
- Sustainable AI Development: Choose models that minimize environmental impact
- Cost Optimization: Reduce energy costs in production deployments
- Transparency: Provide clear metrics for AI sustainability reporting
- Informed Decision-Making: Balance model performance with energy efficiency
This tool empowers AI engineers and sustainability professionals to build more sustainable AI systems without sacrificing functionality.
# Install from GitHub
pip install git+https://github.com/pascaljoly/ml-energy-score.git
# Or install dependencies locally
pip install -r requirements.txt
# Measure energy consumption of a model
python energy_measurement/measure_energy.py
# Test the measurement function
python energy_measurement/test/test_dummy.py
python energy_measurement/test/test_pytorch.py
# Calculate energy efficiency scores
python energy_score/calculate_scores.pyml-energy-score/
βββ utils/ # Shared utilities
β βββ __init__.py
β βββ security_utils.py # Security and validation utilities
β
βββ energy_measurement/ # Energy measurement package
β βββ __init__.py
β βββ measure_energy.py # Main measurement function
β βββ example_usage.py # Usage examples
β βββ test/ # Measurement tests
β β βββ __init__.py
β β βββ test_dummy.py # Test with dummy model
β β βββ test_pytorch.py # Test with real PyTorch model
β β βββ sample_dataset.py # Dataset sampling utilities
β βββ README.md # Detailed measurement documentation
β
βββ energy_score/ # Energy scoring package
β βββ __init__.py
β βββ calculate_scores.py # Star rating calculation
β βββ test/ # Scoring tests
β βββ __init__.py
β βββ test_scoring.py # Test scoring functionality
β
βββ archive/ # Archived older functionality
βββ results/ # Energy measurement results (generated)
β βββ {task_name}/
β βββ {hardware}/
β βββ {model}_{timestamp}.json
βββ scores/ # Energy scoring results (generated)
β βββ {task_name}/
β βββ {task}_{hardware}_{timestamp}.json
βββ setup.py # Package installation configuration
βββ requirements.txt # Core dependencies
βββ LICENSE # MIT License
βββ README.md # This file
Measure energy consumption of any ML model:
from energy_measurement.measure_energy import measure_energy
def my_inference_function(sample):
# Your model inference code
return processed_result
# Measure energy
results = measure_energy(
inference_fn=my_inference_function,
dataset=my_dataset,
model_name="my_model",
task_name="text-classification",
hardware="CPU",
num_samples=1000
)
print(f"Energy: {results['kwh_per_1000_queries']:.4f} kWh/1k queries")Features:
- β CodeCarbon Integration: Accurate energy tracking with PUE=1.2
- β Hardware Support: CPU, T4, V100, A100, H100, M1, M2
- β Progress Tracking: Shows progress every 100 samples
- β JSON Output: Structured results with timestamps
- β Error Handling: Validates hardware and datasets
Calculate star ratings for energy efficiency:
from energy_score.calculate_scores import calculate_scores, print_scores
# Calculate scores and display
scores = calculate_scores('image-classification', 'CPU')
print_scores(scores)
# Calculate scores and save to file (recommended)
scores = calculate_scores('image-classification', 'CPU', output_dir='scores')
# Saves to: scores/image-classification/image-classification_CPU_20250111_143022.json
print_scores(scores)Features:
- β Star Rating System: Quintile-based scoring (5 stars = top 20%, 1 star = bottom 20%)
- β Auto-Save Results: Optional output_dir parameter saves scores to timestamped JSON files
- β Organized Storage: Files organized by task name with hardware and timestamp in filename
- β Percentile Rankings: Each model gets a percentile ranking (0-100, lower is better)
- β Energy Statistics: Min, max, and median energy consumption for the task
Star Rating System:
- βββββ 5 stars: Top 20% (most efficient)
- βββββ 4 stars: Next 20%
- βββββ 3 stars: Middle 20%
- βββββ 2 stars: Next 20%
- βββββ 1 star: Bottom 20% (least efficient)
Saved Score Format:
{
"task_name": "image-classification",
"hardware": "CPU",
"num_models": 10,
"models": [
{
"model_name": "efficientnet-b0",
"star_rating": 5,
"kwh_per_1000_queries": 0.0250,
"percentile": 0
}
],
"energy_range": {"min": 0.0250, "max": 0.1850, "median": 0.0680},
"score_timestamp": "2025-01-11T14:30:22.123456"
}python energy_measurement/test/test_dummy.pypython energy_measurement/test/test_pytorch.pypython energy_score/test/test_scoring.pyCPU Models (10 models):
βββββ efficientnet-b0: 0.0250 kWh (percentile: 0)
βββββ mobilenet-v2: 0.0420 kWh (percentile: 11)
βββββ resnet18: 0.0680 kWh (percentile: 22)
βββββ resnet50: 0.1250 kWh (percentile: 33)
βββββ vgg16: 0.1850 kWh (percentile: 44)
GPU Models (5 models):
βββββ efficientnet-b0-gpu: 0.0150 kWh (percentile: 0)
βββββ resnet50-gpu: 0.0350 kWh (percentile: 25)
βββββ vgg16-gpu: 0.0550 kWh (percentile: 50)
- CPU: CPU-only execution
- T4: NVIDIA Tesla T4 16GB
- A10: NVIDIA A10 24GB
- V100: NVIDIA Tesla V100 32GB
- A100: NVIDIA A100 40GB/80GB
- H100: NVIDIA H100 80GB
- M1/M2: Apple Silicon (development)
Results are saved as JSON files:
{
"model_name": "mobilenet-v2",
"task_name": "image-classification",
"hardware": "CPU",
"timestamp": "2025-10-25T17:56:08.882447",
"num_samples": 100,
"energy_kwh": 0.000008,
"co2_kg": 0.000002,
"duration_seconds": 3.16,
"kwh_per_1000_queries": 0.000084
}- Python 3.8+
- CodeCarbon >= 2.4.0
- NumPy >= 1.20.0
- PyTorch >= 1.9.0 (optional, for PyTorch tests)
- Main Documentation:
energy_measurement/README.md - API Reference: See docstrings in source files
- Examples:
energy_measurement/example_usage.py
Older functionality has been moved to the archive/ directory:
archive/energy-compare/: Previous energy comparison frameworkarchive/ml_energy_score/: Previous ML energy scoring systemarchive/tests/: Previous test suitesarchive/: Configuration system and demos
MIT License - see LICENSE file for details.
This is a focused energy measurement toolkit. For contributions, please ensure:
- All tests pass
- Code follows the existing style
- New features include comprehensive tests
- Documentation is updated
Pascal Joly Sustainability Consulting IT Climate Ed, LLC
This project is inspired by and builds upon:
- Hugging Face AI Energy Score: Methodology for measuring and comparing ML model energy consumption
- CodeCarbon: Python library for tracking carbon emissions from computing
Created: October 25, 2025 License: MIT Status: Open source tool for sustainable AI development