Skip to content

Some example code and readme with info for tracking the (carbon) emissions of our projects

License

Notifications You must be signed in to change notification settings

Amsterdam-AI-Team/emissions-tracker

Repository files navigation

emissions_tracker

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.

Background

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

Folder Structure

Installation

  1. Clone this repository:
git clone https://github.com/Amsterdam-AI-Team/emissions_tracking.git
  1. Install all dependencies:
pip install -r requirements.txt

The code has been tested with Python 3.10 on Windows & Linux.

Usage

How it works

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.

image

More specifically it does it as following:

Calculate the Carbon Intensity of the local energy grid

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.

  1. When available, CodeCarbon uses global carbon intensity of electricity per cloud provider or per country.
  2. When it doesn’t have the carbon intensity, but has the energy mix, it computes the carbon intensity
  3. If it has neither, it uses a world average of 475 gCO2.eq/kWh

image

Power Usage

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.

CPU

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.)

GPU

  • Tracks Nvidia GPUs energy consumption using pynvml library

RAM

  • CodeCarbon uses a 3 Watts for 8GB ratio.

Tracking your emissions

You have 2 main options (More options are presented in the notebook in this repo.)

Monitoring your machine 💻

In your command prompt use: codecarbon monitor. The package will track your emissions independently from your code.

In your Python 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.

Output

Logger

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.

Dashboard

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

image image

Contributing

Feel free to help out! Open an issue, submit a PR or contact us.

Acknowledgements

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.

License

This project is licensed under the terms of the European Union Public License 1.2 (EUPL-1.2).

About

Some example code and readme with info for tracking the (carbon) emissions of our projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published