Carbon footprint AI training: misurare e ridurre
Training AI models generates significant environmental impact: some language models require as much energy as thousands of homes in a year. This article explains how to measure carbon footprint during training, which metrics to use, and how to implement concrete reduction strategies in your development workflow — with real code examples and already-available tools.
Why measure AI training carbon footprint
The energy cost of machine learning is not an academic curiosity. In 2022, according to a study by MIT and Lawrence Berkeley National Laboratory, training a single generative language model produced 626 metric tons of CO₂ equivalent — equivalent to driving a car for 1.3 million kilometers. The data centers hosting them consume 1-2% of global electricity.
As a software professional, you have two responsibilities:
- Immediate efficiency: reduce the time and energy of every training run, lowering costs and impact.
- Strategic sustainability: choose green data centers, efficient training methods, and optimized models.
This article guides you through measurement and optimization.
Fundamental metrics for carbon footprint
Before measuring, you need to know which metrics matter. The main ones are:
- PUE (Power Usage Effectiveness): Ratio of total data center energy to energy used by IT. PUE = 1.0 is ideal (all used by servers), 1.5+ signals inefficiency.
- CUE (Carbon Usage Effectiveness): kg of CO₂ per kWh used. Depends on the energy grid (renewable vs. coal). Global average is ~0.4 kg CO₂/kWh; with green energy, it drops to 0.05.
- FLOPS (Floating Point Operations Per Second): Computational operations per second. More FLOPS per watt = more efficient.
- Training Time: Duration of training in hours/days. Reducing time = reducing total energy.
- Total CO₂ (kg or metric tons equivalent): CO₂ = (Energy in kWh) × (Grid CUE).
Tools and libraries for measurement
You don't need to calculate manually. There are open-source tools ready to use:
1. Codecarbon (Python)
The most popular library. Tracks energy and CO₂ in real-time during training.
# Installation
pip install codecarbon
# Basic usage
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="IT")
tracker.start()
# Your training code here (PyTorch, TensorFlow, etc.)
for epoch in range(10):
model.train()
# training code...
emissions = tracker.stop()
print(f"Carbon emissions: {emissions} kg CO2")
Codecarbon estimates energy by monitoring CPU/GPU and memory consumption. Supports cloud estimates (AWS, Google Cloud, Azure).
2. Captura (PyTorch-native)
Plugin specific to PyTorch that integrates tracking without overhead.
# Installation
pip install captura
# Usage with PyTorch
from captura import capture_emissions
import torch
import torch.nn as nn
@capture_emissions
def train_model():
model = nn.Sequential(
nn.Linear(784, 256),
nn.ReLU(),
nn.Linear(256, 10)
)
optimizer = torch.optim.Adam(model.parameters())
for epoch in range(5):
for batch in train_loader:
optimizer.zero_grad()
loss = model(batch).sum()
loss.backward()
optimizer.step()
train_model()
# Output: Emissions = X kg CO2
3. Experiment Tracking (MLflow, Weights & Biases)
Integrates carbon tracking into your MLOps experiments to compare efficiency across runs.
# Integration with MLflow
import mlflow
from codecarbon import EmissionsTracker
with mlflow.start_run():
tracker = EmissionsTracker()
tracker.start()
# Training
model.fit(X_train, y_train, epochs=10)
emissions = tracker.stop()
mlflow.log_metric("carbon_emissions_kg", emissions)
mlflow.log_metric("training_time_hours", training_hours)
Strategies for reducing carbon footprint
1. Choose efficient hardware
Not all GPUs/TPUs consume energy the same way. Google's TPUs are generally 2-3x more efficient for training than GPUs, thanks to specialized architecture. If you use GPUs:
- Prefer NVIDIA A100 or H100 (newer = more efficient for TFLOPS/watt).
- Avoid old GPUs (GTX 1080) for serious training — too much energy for FLOPS.
- Choose modern CPUs (Intel Xeon Platinum, AMD EPYC) for CPU-bound tasks.
2. Optimize the training algorithm
Shorter algorithms = less energy consumed.
# BAD: Full training without optimizations
model = BertModel()
for epoch in range(100):
full_training_step(model)
# GOOD: Transfer learning + fine-tuning
model = BertModel.from_pretrained("bert-base-uncased")
# Freeze early layers — fewer gradients to compute
for param in model.encoder.layers[:8].parameters():
param.requires_grad = False
# Fine-tuning on few layers
for epoch in range(10):
fine_tuning_step(model)
# Reduces FLOPS and time by ~80%
3. Use learning rate scheduling and early stopping
Often training doesn't need all epochs. Early stopping halts training when validation doesn't improve, saving energy.
# Keras integration
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
early_stop = EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True
)
reduce_lr = ReduceLROnPlateau(
monitor='val_loss',
factor=0.5,
patience=3,
min_lr=1e-6
)
model.fit(
X_train, y_train,
validation_split=0.2,
epochs=100,
callbacks=[early_stop, reduce_lr]
)
# Stops early if no improvement
4. Choose a green data center
50% of the impact depends on the carbon intensity of the energy grid. If training on cloud:
- Google Cloud: 40% renewable energy average (best-in-class among major clouds).
- AWS: ~30% renewable (EU regions slightly better than US).
- Azure: ~45% renewable in Europe.
- Local on-prem: Depends on your grid (e.g., Italy ~40% renewable).
Use Google's Carbon Footprint API to choose the data center with the lowest carbon intensity.
Model distribution and quantization
Not all impact is in training. Distribution matters too. Large models consume energy even in inference. Reduce runtime impact:
8-bit quantization
# PyTorch: Dynamic quantization
import torch
model = torch.load("my_model.pth")
quantized_model = torch.quantization.quantize_dynamic(
model,
{torch.nn.Linear},
dtype=torch.qint8
)
# Model 4x smaller, inference 3-5x faster
torch.save(quantized_model, "model_quantized.pth")
Knowledge Distillation
Train a small model (student) to copy a large model (teacher). Inference uses the student, reducing energy.
# Simplified knowledge distillation
import torch.nn.functional as F
def distillation_loss(student_output, teacher_output, temperature=3):
soft_target = F.softmax(teacher_output / temperature, dim=1)
soft_pred = F.log_softmax(student_output / temperature, dim=1)
return F.kl_div(soft_pred, soft_target, reduction='batchmean')
# During training
for data, target in train_loader:
student_logits = student_model(data)
teacher_logits = teacher_model(data)
loss = distillation_loss(student_logits, teacher_logits)
loss.backward()
optimizer.step()
Monitoring and reporting
Once optimizations are implemented, track your progress:
# Complete carbon footprint report
from codecarbon import OfflineEmissionsTracker
import json
def run_and_report(model_name, country="IT", epochs=10):
tracker = OfflineEmissionsTracker(
country_iso_code=country,
log_level="warning"
)
tracker.start()
start_time = time.time()
train_model(epochs=epochs)
end_time = time.time()
emissions_kg = tracker.stop()
report = {
"model": model_name,
"epochs": epochs,
"training_time_hours": (end_time - start_time) / 3600,
"emissions_kg_co2": round(emissions_kg, 4),
"emissions_metric_tons": round(emissions_kg / 1000, 4),
"country": country,
"trees_to_offset": round(emissions_kg / 21, 1), # 1 tree absorbs ~21 kg CO2/year
}
with open(f"{model_name}_carbon_report.json", "w") as f:
json.dump(report, f, indent=2)
print(f"Report saved: {model_name}_carbon_report.json")
return report
# Execution
report = run_and_report("bert-fine-tuned-v1", epochs=20)
print(f"CO2 emissions: {report['emissions_kg_co2']} kg")
Best practices for Sustainable AI
- Baseline first: Always compare against a baseline without ML. Sometimes the problem solves with simple rules.
- Conscious hyperparameter tuning: Use restricted grid search or Bayesian optimization, not unlimited random search.
- Model versioning: Track which version was trained on which hardware and in which region. Facilitates replay and benchmarking.
- Optimal batch size: Too small batch size wastes GPU. Too large = memory overflow. Experiment to find the sweet spot.
- Communicate carbon footprint: Include environmental impact in your model README, as a metric alongside accuracy and latency.
FAQ
Q: How much energy does training a language model like GPT cost?
A: A 175-billion-parameter LLM consumed ~1,287 MWh during training, equivalent to ~552 kg CO₂. Unofficial estimates for larger-scale models suggest 5-10x more. For reference: training a BERT small (110 million parameters) costs approximately 0.7-1.5 kg CO₂.
Q: If I use Ollama on-prem for local inference, is the impact lower?
A: Yes, but it depends on what "lower" means. Local inference on CPU/GPU consumes less energy than a cloud round-trip (network latency). However, if your machine uses fossil fuel energy and a cloud data center uses 100% renewable, the cloud might be greener. Key point: measure both training and inference in your specific context.
Q: Is it ethical to train AI models considering environmental impact?
A: Yes, as long as you measure, optimize, and communicate impact. Many useful models have calculated and public carbon footprints. The issue is not using AI, but using it consciously — just like driving a car is ok if you know its consumption and don't waste it.
Q: Does Codecarbon provide accurate estimates?
A: Codecarbon uses heuristics (monitors GPU TDP, CPU, memory). Estimates are often ±20% from reality, depending on hardware. For precise measurements, use data center energy logs or your local UPS logs. Codecarbon is useful for relative comparisons (this run vs. that one) even if absolute estimates aren't 100% precise.
Q: Which training model is most carbon-efficient?
A: Transfer learning + fine-tuning. Training a small model from scratch costs 10-100x more. If you need to solve an NLP problem, always start with a pre-trained model (BERT, RoBERTa, Mistral) and adapt it to your data. The overhead of downloading pre-trained is negligible compared to training from scratch.
Conclusion
The carbon footprint of AI training is not invisible: it's measurable, reducible, and manageable. With the right tools (Codecarbon, Captura) and strategies (transfer learning, quantization, early stopping), you can reduce impact by an order of magnitude without sacrificing performance.
The final message: always measure. You can't optimize what you don't measure. Start today with a small project, integrate Codecarbon into your workflow, and build a culture of Sustainable AI in your team.
Commenti
Caricamento commenti...
Sign in to comment