CodeCarbon: Tracking and Reducing Carbon Emissions in Computing

Fateh Ali Aamir
4 min readNov 6, 2024
Photo by Brendan O’Donnell on Unsplash

As our reliance on artificial intelligence (AI) and machine learning (ML) increases, so does the energy demand associated with training these models. This energy-intensive process significantly contributes to global carbon emissions. CodeCarbon is a tool created to help track and reduce the environmental impact of computational tasks. This article will explore what CodeCarbon is, why it is important, how it works, and how to use it effectively.

1. What is CodeCarbon?

CodeCarbon is an open-source software package that allows developers, data scientists, and organizations to track the carbon footprint of their computational processes. It estimates the carbon dioxide (CO₂) emissions associated with running code, focusing on energy-intensive machine learning tasks. By tracking these emissions, CodeCarbon empowers users to make informed choices about their energy usage and take actionable steps to reduce their environmental impact.

2. Why is CodeCarbon Important?

As artificial intelligence (AI) and machine learning (ML) continue to advance, the demand for computational power has surged dramatically, resulting in increased energy consumption and, subsequently, higher carbon emissions. The environmental impact of training large-scale machine learning models is substantial; for instance, the training of a single transformer model can produce as much CO₂ as five cars do over their entire lifespan.

CodeCarbon addresses this challenge by providing insights into the energy consumption and carbon intensity of these processes. This tool allows researchers and developers to:

  • Make more eco-friendly choices by selecting cloud providers or regions with lower carbon intensities.
  • Optimize energy usage through insights that help balance performance with sustainability.
  • Raise awareness of the environmental cost of compute-heavy operations, encouraging a more sustainable approach to AI development.

3. How Does CodeCarbon Work?

CodeCarbon calculates CO₂ emissions by combining two primary factors:

  1. Carbon Intensity — This represents the CO₂ emitted per kilowatt-hour (kWh) of electricity used. It is based on the energy mix of the region where the computing infrastructure is located (e.g., fossil fuels, renewables).
  2. Energy Consumption — This measures the energy the computational infrastructure consumes over time. CodeCarbon tracks power usage on various hardware resources, including CPU, GPU, and RAM, to estimate energy consumption accurately.

The tool works by periodically sampling the power usage of the computing resources and then multiplying this by the carbon intensity to get a real-time emissions estimate.

For example, if 1 kWh of energy emits 0.5 kg of CO₂ in a particular region, and the computing process consumes 2 kWh, the total emissions would be:

CO₂eq = 0.5kg/kWh x 2kWh = 1 kgCO₂

4. How to Use CodeCarbon

CodeCarbon is highly flexible and can be integrated into most Python-based computational workflows, whether locally or on cloud infrastructure. Here’s a step-by-step guide to getting started with CodeCarbon:

Installation

First, install CodeCarbon using pip:

pip install codecarbon

Using CodeCarbon: Quick Examples

CodeCarbon provides several ways to track emissions, depending on the complexity of your code and workflow. Here are three main approaches:

a. Using the Explicit Object

You can use an EmissionsTracker object explicitly to monitor emissions in your code. Here’s an example of training a neural network on MNIST data:

import tensorflow as tf
from codecarbon import EmissionsTracker

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
tracker = EmissionsTracker()
tracker.start()
model.fit(x_train, y_train, epochs=10)
emissions = tracker.stop()
print(f"CO₂ Emissions: {emissions} kg CO₂")

b. Using the Context Manager

For more concise code, you can use CodeCarbon’s EmissionsTracker as a context manager, which automatically starts and stops the tracker:

from codecarbon import EmissionsTracker
with EmissionsTracker() as tracker:
# Code to be monitored
model.fit(x_train, y_train, epochs=10)

c. Using the Decorator

If you have a training function, CodeCarbon’s decorator makes it easy to wrap the function with emissions tracking:

from codecarbon import track_emissions

@track_emissions(project_name="mnist")
def train_model():
# Training code here
model.fit(x_train, y_train, epochs=10)

train_model()

CodeCarbon Modes

CodeCarbon can operate in both online and offline modes:

  • Online Mode: Requires an internet connection to fetch regional carbon intensity from an API.
  • Offline Mode: Useful for restricted environments without internet access, where you can specify a country code to fetch the appropriate carbon intensity.

Conclusion

In an era where sustainability is paramount, CodeCarbon offers a way for developers and organizations to track, understand, and reduce the environmental footprint of computational tasks. By making energy consumption and emissions visible, CodeCarbon encourages responsible computing, enabling users to make data-driven decisions to reduce their impact on the planet.

Sign up to discover human stories that deepen your understanding of the world.

Fateh Ali Aamir
Fateh Ali Aamir

Written by Fateh Ali Aamir

23. A programmer by profession. A writer by passion.

No responses yet

Write a response