Estimate and track carbon emissions from your compute, quantify and analyze their impact.
- About CodeCarbon π‘
- Installation π
- Quickstart π
- Examples π€
- Built-in Visualization Tool π
- Comet Integration π₯
- Report your emissions: LateX template π»
- Infrastructure Support π₯οΈ
- Data Sources ποΈ
- Contributing π€
- Build Documentation π¨οΈ
- Contact π
While computing currently represents roughly 0.5% of the worldβs energy consumption, that percentage is projected to grow beyond 2% in the coming years, which will entail a significant rise in global CO2 emissions if not done properly. Given this increase, it is important to quantify and track the extent and origin of this energy usage, and to minimize the emissions incurred as much as possible.
For this purpose, we created CodeCarbon, a Python package for tracking the carbon emissions produced by various kinds of computer programs, from straightforward algorithms to deep neural networks.
By taking into account your computing infrastructure, location, usage and running time, CodeCarbon can provide an estimate of how much CO2 you produced, and give you some comparisons with common modes of transportation to give you an order of magnitude.
Our hope is that this package will be used widely for estimating the carbon footprint of computing, and for establishing best practices with regards to the disclosure and reduction of this footprint.
Follow the steps below to set up the package and don't hesitate to open an issue if you need help!
Create a virtual environment using conda for easier management of dependencies and packages.
For installing conda, follow the instructions on the official conda website
conda create --name codecarbon python=3.6
conda activate codecarbon
pip install codecarbon
or
pip install codecarbon[viz]
if you wish to use the built-in visualization tool described below.
conda install -c codecarbon -c conda-forge codecarbon
codecarbon is now installed in your the local environment
This is the most straightforward usage of the package, which is possible if you have access to the Internet, which is necessary to gather information regarding your geographical location.
from codecarbon import EmissionsTracker
tracker = EmissionsTracker()
tracker.start()
# GPU Intensive code goes here
tracker.stop()You can also use the decorator:
from codecarbon import track_emissions
@track_emissions
def training_loop():
passThe EmissionsTracker also works as a context manager:
from codecarbon import EmissionsTracker
with EmissionsTracker() as tracker:
# GPU Intensive code goes hereThis will write a csv file of the CO2 emissions in the current directory.
(This feature is currently in ALPHA stage, meaning it's only available for developers installing CodeCarbon from this repository. The documentation and implementation are is still under development.)
This mode use the Code Carbon API to upload the timeseries of your emissions.
Before using it, you need an experiment_id, to get one, run:
codecarbon init
It will ask the API for an experiment_id on the default project and save it to .codecarbon.config for you.
Then, you could tell Code Carbon to use the API in your code:
from codecarbon import track_emissions
@track_emissions(save_to_api=True)
def train_model():
# Your code here
if __name__ == "__main__":
train_model()This is the simplest way. You could specify others options in @track_emissions or in .codecarbon.config.
This mode can be used in setups without internet access, but requires a manual specification of your country code. A complete list of country ISO codes can be found on Wikipedia.
The offline tracker can be used as follows:
from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker(country_iso_code="CAN")
tracker.start()
# GPU Intensive code goes here
tracker.stop()or by using the decorator:
from codecarbon import track_emissions
@track_emissions(offline=True, country_iso_code="CAN")
def training_loop():
passor by using the context manager:
from codecarbon import EmissionsTracker
with EmissionsTracker() as tracker:
# GPU Intensive code goes here