This repo serves as a demo/guide of how we track emissions within the AI Lab of the municipality of Amsterdam. This method aims at tracking the power usage of resources during code runtime.
Note: If for any reason it is not possible to track usage during runtime, a good alternative might be Green Algorithms. This is an online tool where you can calculate your emissions by manually inputting your resources and runtime.
As the AI Lab we would like to be able to track the climate impact (CI) of (the modelling part of) our projects. This is because development and usage of AI models can cause significant environmental impact. The goal of tracking is to facilitate:
- Awareness
- We want to create awareness, for ourselves and for users, what the environmental impact of the AI model is
- Reduction
- Identifying what the relative impact of different parts of the process is allows for focused optimization and impact reduction
- Comparison
- When an AI system is proposed, insight in the environmental impact can help in (the discussion about) the benefits vs. (environmental) costs
notebooks
: Jupyter notebooks / tutorials
- Clone this repository:
git clone https://github.com/Amsterdam-AI-Team/emissions_tracking.git
- Install all dependencies:
pip install -r requirements.txt
The code has been tested with Python 3.10 on Windows & Linux.
To track the carbon emissions of our projects, we use CodeCarbon, a Python package that estimates your hardware electricity power consumption (GPU + CPU + RAM) and applies it to the carbon intensity of the region where the computing is done.
More specifically it does it as following:
Carbon intensity is calculated as weighted average of emissions from different energy sources used to generate electricity. Codecarbon attempts to retrieve carbon intensity based on location.
- When available, CodeCarbon uses global carbon intensity of electricity per cloud provider or per country.
- When it doesn’t have the carbon intensity, but has the energy mix, it computes the carbon intensity
- If it has neither, it uses a world average of
475 gCO2.eq/kWh
CodeCarbon attempts to establish and track the power usage of the resources (CPU/GPU/RAM) used to run your code.
By default it checks it every 15 seconds.
For CPU it uses tracking tools if they are available. Which tracking tool is dependent on your OS.
- Windows&Mac Intel > Intel Power Gadget (need to install)
- M-chip Mac > powermetrics (native on any Mac)
- Linux > RAPL (Running Average Power Limit) files
If no tracking tools are available it switches to fallback mode:
- Detect CPU Hardware > Map to Termal Design Power (TDP)
- If CPU can’t be mapped use global constant
- Assume 50% average power consumption
Note: Tracking tools are currently unavailable for managed devices of the municipality of Amsterdam. Azure also does not allow access to specific resource usage statistics, so for both these options CodeCarbon will run in fallback mode. (TODO: Check access to RAPL files in WSL.)
- Tracks Nvidia GPUs energy consumption using
pynvml
library
- CodeCarbon uses a 3 Watts for 8GB ratio.
You have 2 main options (More options are presented in the notebook in this repo.)
In your command prompt use: codecarbon monitor
.
The package will track your emissions independently from your code.
from codecarbon import track_emissions
@track_emissions()
def your_function_to_track():
# your code
The package will track the emissions generated by the execution of your function.
CodeCarbon has a built in logger that logs automatically into a .csv
. It contains info such as:
Project_name
, Emissions
, Energy_consumed
, Cloud_region
, etc.
CodeCarbon comes with a Dash App
where the emissions are visualized.
To host it locally it, we execute the CLI command below:
carbonboard --filepath="emissions.csv" --port=3333
Feel free to help out! Open an issue, submit a PR or contact us.
This repository was created by Amsterdam Intelligence for the City of Amsterdam.
It mainly serves as a guide/example on how to use CodeCarbon. For more detailed information, check out their documentation.
This project is licensed under the terms of the European Union Public License 1.2 (EUPL-1.2).