Track carbon footprint of ML training jobs — 3 lines of code
Project description
greenai-tracker
Track carbon footprint of ML training jobs — 3 lines of code.
pip install greenai-tracker
Quick Start
import greenai
greenai.init(api_key="gai_live_xxx", project_id="proj_xxx")
with greenai.track("bert-finetuning"):
model.fit(X_train, y_train)
That's it. GreenAI automatically:
- Measures real GPU power draw (via NVIDIA NVML if GPU present)
- Estimates CPU power from utilization × TDP
- Detects your cloud region (AWS/GCP/Azure)
- Computes CO₂ emissions using region-specific grid intensity
- Sends everything to your GreenAI dashboard
Install
# Basic install (CPU tracking)
pip install greenai-tracker
# With GPU support (reads real watts from NVIDIA GPUs)
pip install greenai-tracker[gpu]
# Full install (GPU + better CPU model detection)
pip install greenai-tracker[full]
Usage Patterns
Option 1: Init once, track many runs
import greenai
greenai.init(
api_key="gai_live_xxx",
project_id="proj_xxx",
job_type="training", # or "inference", "data_processing"
region="aws:us-east-1", # auto-detected if omitted
)
# Run 1
with greenai.track("training-epoch-1"):
model.fit(X_train, y_train)
# Run 2
with greenai.track("training-epoch-2"):
model.fit(X_train, y_train)
Option 2: Environment variables (no code change)
export GREENAI_API_KEY=gai_live_xxx
export GREENAI_PROJECT_ID=proj_xxx
import greenai
# Works without any arguments!
with greenai.track("my-training-run"):
model.fit(X_train, y_train)
Option 3: Decorator
@greenai.track_function(api_key="gai_live_xxx", project_id="proj_xxx")
def train_model():
model.fit(X_train, y_train)
train_model() # Tracked automatically
Option 4: Validate your API key
import greenai
result = greenai.validate(api_key="gai_live_xxx")
print(result)
# {'valid': True, 'project': 'My ML Pipeline', 'plan': 'starter', 'job_runs_remaining': 9750}
Framework Integrations
PyTorch Lightning
from greenai_sdk.integrations.pytorch import GreenAICallback
import pytorch_lightning as pl
trainer = pl.Trainer(
callbacks=[GreenAICallback(api_key="gai_live_xxx", project_id="proj_xxx")]
)
trainer.fit(model)
HuggingFace Transformers
from greenai_sdk.integrations.huggingface import GreenAICallback
from transformers import Trainer, TrainingArguments
trainer = Trainer(
model=model,
args=TrainingArguments(output_dir="./results"),
callbacks=[GreenAICallback(api_key="gai_live_xxx", project_id="proj_xxx")]
)
trainer.train()
Scikit-learn
from greenai_sdk.integrations.sklearn import track_fit
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model = track_fit(model, api_key="gai_live_xxx", project_id="proj_xxx")
model.fit(X_train, y_train) # Tracked automatically
How It Works
| Measurement | Method |
|---|---|
| GPU power | nvidia-ml-py reads real watts from NVIDIA GPU |
| CPU power | psutil utilization × CPU TDP from model name |
| Region | Auto-detect from EC2/GCP/Azure metadata endpoints |
| CO₂ | Watts × duration × regional grid intensity (kg CO₂e/kWh) |
Supported regions: 80+ regions across AWS, GCP, and Azure including Mumbai (ap-south-1), Tokyo, Sydney, Frankfurt, and more.
Graceful fallback: Works without NVIDIA GPU (CPU-only estimate). Works offline (caches payloads and retries).
Requirements
- Python 3.8+
requests,psutil(auto-installed)- Optional:
nvidia-ml-pyfor real GPU readings (pip install greenai-tracker[gpu]) - Optional:
py-cpuinfofor better CPU model detection (pip install greenai-tracker[full])
Sign Up
Get your API key at greenai.dev — free tier includes 10,000 job runs/month.
Links
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file greenai_tracker-0.1.1.tar.gz.
File metadata
- Download URL: greenai_tracker-0.1.1.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b3be8ed302646959b82e3ad131e456882d7940b5fb77308c7da998b27370ead
|
|
| MD5 |
fdc3bd3f92adfdf24a968d3e50e5eee6
|
|
| BLAKE2b-256 |
1702465882d31c6a026051ccc0a90af88a46f16b52ae6f364bbd94ac1adb8ee6
|
File details
Details for the file greenai_tracker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: greenai_tracker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9470cc0357bfd26d0c6770a2c956e7b9a572581bb34f7e67c67234ea4a73eab5
|
|
| MD5 |
f530ae72d5b12395d0acf6b3ef3917d9
|
|
| BLAKE2b-256 |
6ee24115dd9570d6623bad84f643860cb4bc8dc9bd71cd7b78bdcb19c28572ed
|