Green Technology for a Greener World: Track, Cut, and Offset CO2 Emissions with CodeCarbon!
Introduction
Electronic devices are now an integral part of our lives. People rely on their personal computing devices to get work done or use it for entertainment. There's an app for every task these days. These apps run on servers in a datacenter or served from an enterprise network.
The entire technology ecosystem from producer to consumer requires enormous amounts of electricity. This energy majorly comes through non-renewable sources.
Before the Industrial Revolution, emissions were minimal, and their growth remained slow until the mid-20th century. In 1950, global CO2 emissions were 6 billion tonnes. By 1990, they had almost quadrupled, reaching over 22 billion tonnes. Currently, emissions have surged to over 34 billion tonnes annually.
While AI offers numerous advantages to society, the substantial energy required to sustain AI computing comes with significant environmental consequences. We need a system which can provide us with meaningful insight through which we can use AI for the greater good and also not harm the environment.
CodeCarbon
CodeCarbon presents a user-friendly software solution that smoothly integrates into your Python codebase. This tool calculates the volume of carbon dioxide (CO2) emitted by the cloud or personal computing resources utilized for code execution.
Furthermore, it provides scientists/developers/analysts with insights on reducing emissions through code optimization or by choosing cloud infrastructure hosting in regions that rely on renewable energy sources.
Coding
Installing CodeCarbon is super easy. You just need to run one command.
pip install codecarbon
or
conda install -c conda-forge codecarbon
Start by creating the config file with the following command.
codecarbon init
Import the track_emissions function from codecarbon package.
from codecarbon import track_emissions
I tried implementing CodeCarbon into one of my machine learning model training API. Only a single line of code is needed to add the track_emissions as decorator into existing code.
@app.route('/train', methods=['POST'])
@track_emissions(offline=True, project_name="diabetes_ml_train", country_iso_code="USA", region="California")
def train():
if request.method == 'POST':
label = request.form['label']
csv_file = request.files['train_file']
if csv_file == None:
raise ValueError('Train File is not provided.')
file_name = csv_file.filename
if file_name.split('.')[1] == 'csv':
clock_time = time.ctime().replace(' ', '-')
save_file = './temp/' + file_name.split('.')[0] + '_' + clock_time + '.csv'
csv_file.save(save_file)
experiment_name = request.form['experiment_name']
dia_df = pd.read_csv(save_file)
metrics = run_train_pipeline(experiment_name, dia_df, label)
return jsonify(metrics)
else:
raise TypeError('Invalid file type. App only accepts csv files.')
It's that simple.
The full code with the model training code is in my github repository.
CodeCarbon supports two modes — online and offline. Online is for systems with internet access. If you are running this in a private network then you can use offline mode by passing offline=True in track_emissions function.
According to my code, at each execution of the /train API CodeCarbon tracks power consumption by RAM, CPU and GPU and the output is logged into emissions.csv file.
Dashboard
CodeCarbon also comes with carbonboard which gives us a dashboard to visualize the data from emissions.csv file.
To start carbonboard, you just need to run the following command.
carbonboard --filepath="emissions.csv"
By default the server runs on http://localhost:8050 which can be accessed from the browser.
Recorded emissions as per the timestamp during the entire ML training process.
Conclusion
CodeCarbon may not be the complete solution but definitely a first step towards tackling climate change. These insights can help in any technology project to track, control and reduce their carbon footprint.
An example code for implementing CodeCarbon is here. I hope you will give it a try.
Below message is for You
Are you a programming, AI, or machine learning enthusiast? Then you’ll love my blog on Medium! I regularly post about these topics and share my insights on the latest trends and tools in data science. If you find my content helpful, please like and follow my blog. And if you want to show some extra support, you can give a tip by clicking the button below. Thanks for your time and support!