carbontracker
Track and predict the energy consumption and carbon footprint of training deep learning models.
https://github.com/lfwa/carbontracker
Category: Consumption
Sub Category: Computation and Communication
Keywords from Contributors
automl
Last synced: about 10 hours ago
JSON representation
Repository metadata
Track and predict the energy consumption and carbon footprint of training deep learning models.
- Host: GitHub
- URL: https://github.com/lfwa/carbontracker
- Owner: lfwa
- License: mit
- Created: 2020-04-21T12:01:38.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-04-09T11:58:58.000Z (18 days ago)
- Last Synced: 2025-04-25T12:05:44.990Z (2 days ago)
- Language: Python
- Homepage:
- Size: 2.89 MB
- Stars: 434
- Watchers: 13
- Forks: 31
- Open Issues: 19
- Releases: 7
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
README.md
carbontracker
About
carbontracker is a tool for tracking and predicting the energy consumption and carbon footprint of training deep learning models as described in Anthony et al. (2020).
Citation
Kindly cite our work if you use carbontracker in a scientific publication:
@misc{anthony2020carbontracker,
title={Carbontracker: Tracking and Predicting the Carbon Footprint of Training Deep Learning Models},
author={Lasse F. Wolff Anthony and Benjamin Kanding and Raghavendra Selvan},
howpublished={ICML Workshop on Challenges in Deploying and monitoring Machine Learning Systems},
month={July},
note={arXiv:2007.03051},
year={2020}}
Installation
PyPi
pip install carbontracker
Basic usage
Command Line Mode
Wrap any of your scripts (python, bash, etc.):
carbontracker python script.py
Embed into Python Scripts
Required arguments
epochs
:
Total epochs of your training loop.
Optional arguments
epochs_before_pred
(default=1):
Epochs to monitor before outputting predicted consumption. Set to -1 for all epochs. Set to 0 for no prediction.monitor_epochs
(default=1):
Total number of epochs to monitor. Outputs actual consumption when reached. Set to -1 for all epochs. Cannot be less thanepochs_before_pred
or equal to 0.update_interval
(default=10):
Interval in seconds between power usage measurements are taken.interpretable
(default=True):
If set to True then the CO2eq are also converted to interpretable numbers such as the equivalent distance travelled in a car, etc. Otherwise, no conversions are done.stop_and_confirm
(default=False):
If set to True then the main thread (with your training loop) is paused afterepochs_before_pred
epochs to output the prediction and the user will need to confirm to continue training. Otherwise, prediction is output and training is continued instantly.ignore_errors
(default=False):
If set to True then all errors will cause energy monitoring to be stopped and training will continue. Otherwise, training will be interrupted as with regular errors.components
(default="all"):
Comma-separated string of which components to monitor. Options are: "all", "gpu", "cpu", or "gpu,cpu".devices_by_pid
(default=False):
If True, only devices (under the chosen components) running processes associated with the main process are measured. If False, all available devices are measured (see Section 'Notes' for jobs running on SLURM or in containers). Note that this requires your devices to have active processes before instantiating theCarbonTracker
class.log_dir
(default=None):
Path to the desired directory to write log files. If None, then no logging will be done.log_file_prefix
(default=""):
Prefix to add to the log file name.verbose
(default=1):
Sets the level of verbosity.decimal_precision
(default=6):
Desired decimal precision of reported values.
Example usage
from carbontracker.tracker import CarbonTracker
tracker = CarbonTracker(epochs=max_epochs)
# Training loop.
for epoch in range(max_epochs):
tracker.epoch_start()
# Your model training.
tracker.epoch_end()
# Optional: Add a stop in case of early termination before all monitor_epochs has
# been monitored to ensure that actual consumption is reported.
tracker.stop()
Example output
Default settings
CarbonTracker:
Actual consumption for 1 epoch(s):
Time: 0:00:10
Energy: 0.000038 kWh
CO2eq: 0.003130 g
This is equivalent to:
0.000026 km travelled by car
CarbonTracker:
Predicted consumption for 1000 epoch(s):
Time: 2:52:22
Energy: 0.038168 kWh
CO2eq: 4.096665 g
This is equivalent to:
0.034025 km travelled by car
CarbonTracker: Finished monitoring.
verbose=2
CarbonTracker: The following components were found: CPU with device(s) cpu:0.
CarbonTracker: Average carbon intensity during training was 82.00 gCO2/kWh at detected location: Copenhagen, Capital Region, DK.
CarbonTracker:
Actual consumption for 1 epoch(s):
Time: 0:00:10
Energy: 0.000041 kWh
CO2eq: 0.003357 g
This is equivalent to:
0.000028 km travelled by car
CarbonTracker: Carbon intensity for the next 2:59:06 is predicted to be 107.49 gCO2/kWh at detected location: Copenhagen, Capital Region, DK.
CarbonTracker:
Predicted consumption for 1000 epoch(s):
Time: 2:59:06
Energy: 0.040940 kWh
CO2eq: 4.400445 g
This is equivalent to:
0.036549 km travelled by car
CarbonTracker: Finished monitoring.
Parsing log files
Aggregating log files
carbontracker supports aggregating all log files in a specified directory to a single estimate of the carbon footprint.
Example usage
from carbontracker import parser
parser.print_aggregate(log_dir="./my_log_directory/")
Example output
The training of models in this work is estimated to use 4.494 kWh of electricity contributing to 0.423 kg of CO2eq. This is equivalent to 3.515 km travelled by car. Measured by carbontracker (https://github.com/lfwa/carbontracker).
Convert logs to dictionary objects
Log files can be parsed into dictionaries using parser.parse_all_logs()
or parser.parse_logs()
.
Example usage
from carbontracker import parser
logs = parser.parse_all_logs(log_dir="./logs/")
first_log = logs[0]
print(f"Output file name: {first_log['output_filename']}")
print(f"Standard file name: {first_log['standard_filename']}")
print(f"Stopped early: {first_log['early_stop']}")
print(f"Measured consumption: {first_log['actual']}")
print(f"Predicted consumption: {first_log['pred']}")
print(f"Measured GPU devices: {first_log['components']['gpu']['devices']}")
Example output
Output file name: ./logs/2020-05-17T19:02Z_carbontracker_output.log
Standard file name: ./logs/2020-05-17T19:02Z_carbontracker.log
Stopped early: False
Measured consumption: {'epochs': 1, 'duration (s)': 8.0, 'energy (kWh)': 6.5e-05, 'co2eq (g)': 0.019201, 'equivalents': {'km travelled by car': 0.000159}}
Predicted consumption: {'epochs': 3, 'duration (s)': 25.0, 'energy (kWh)': 1000.000196, 'co2eq (g)': 10000.057604, 'equivalents': {'km travelled by car': 10000.000478}}
Measured GPU devices: ['Tesla T4']
Compatibility
carbontracker is compatible with:
- NVIDIA GPUs that support NVIDIA Management Library (NVML)
- Intel CPUs that support Intel RAPL
- Slurm
- Google Colab / Jupyter Notebook
Notes
Availability of GPUs and Slurm
- Available GPU devices are determined by first checking the environment variable
CUDA_VISIBLE_DEVICES
(only ifdevices_by_pid
=False otherwise we find devices by PID). This ensures that for Slurm we only fetch GPU devices associated with the current job and not the entire cluster. If this fails we measure all available GPUs. - NVML cannot find processes for containers spawned without
--pid=host
. This affects thedevice_by_pids
parameter and means that it will never find any active processes for GPUs in affected containers.
Extending carbontracker
See CONTRIBUTING.md.
Star History
carbontracker in media
Owner metadata
- Name:
- Login: lfwa
- Email:
- Kind: user
- Description:
- Website: lfwa.github.io
- Location: Denmark/Switzerland
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/8901046?u=337c33538421e2fa6f875eb189f87e702cc6afbe&v=4
- Repositories: 9
- Last ynced at: 2024-06-11T15:58:35.497Z
- Profile URL: https://github.com/lfwa
GitHub Events
Total
- Create event: 8
- Release event: 2
- Issues event: 8
- Watch event: 55
- Delete event: 2
- Issue comment event: 5
- Push event: 16
- Pull request event: 13
- Fork event: 5
Last Year
- Create event: 8
- Release event: 2
- Issues event: 8
- Watch event: 55
- Delete event: 2
- Issue comment event: 5
- Push event: 16
- Pull request event: 13
- Fork event: 5
Committers metadata
Last synced: 6 days ago
Total Commits: 181
Total Committers: 7
Avg Commits per committer: 25.857
Development Distribution Score (DDS): 0.685
Commits in past year: 58
Committers in past year: 5
Avg Commits per committer in past year: 11.6
Development Distribution Score (DDS) in past year: 0.241
Name | Commits | |
---|---|---|
Lasse | l****y@g****m | 57 |
Rasmus Hag Løvstad | r****d@g****m | 44 |
kanding | b****2@l****k | 39 |
Pedram Bakh | 5****h | 25 |
Raghav | r****v@d****k | 14 |
Laurențiu Nicola | l****a | 1 |
Andreas Fehlner | f****r@a****e | 1 |
Committer domains:
Issue and Pull Request metadata
Last synced: 1 day ago
Total issues: 68
Total pull requests: 27
Average time to close issues: 9 months
Average time to close pull requests: 7 days
Total issue authors: 25
Total pull request authors: 8
Average comments per issue: 1.85
Average comments per pull request: 0.11
Merged pull request: 23
Bot issues: 0
Bot pull requests: 0
Past year issues: 6
Past year pull requests: 21
Past year average time to close issues: 20 days
Past year average time to close pull requests: 8 days
Past year issue authors: 5
Past year pull request authors: 4
Past year average comments per issue: 0.83
Past year average comments per pull request: 0.1
Past year merged pull request: 19
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- lfwa (21)
- raghavian (8)
- leondz (8)
- kanding (3)
- lnicola (3)
- PedramBakh (2)
- Princec711 (2)
- LukasHedegaard (2)
- Snailed (2)
- mpariente (2)
- Teamsusai (1)
- tuanaqeelbohoran (1)
- HishamAbulfeilat (1)
- sagnik (1)
- ib31iat (1)
Top Pull Request Authors
- Snailed (20)
- DaniG2106 (1)
- leondz (1)
- yoviny (1)
- lfwa (1)
- snehaaprabhu (1)
- lnicola (1)
- andife (1)
Top Issue Labels
- enhancement (23)
- bug (23)
- question (3)
- API (3)
- help wanted (1)
- duplicate (1)
Top Pull Request Labels
- enhancement (1)
Package metadata
- Total packages: 1
-
Total downloads:
- pypi: 2,310 last-month
- Total docker downloads: 41
- Total dependent packages: 2
- Total dependent repositories: 4
- Total versions: 26
- Total maintainers: 3
pypi.org: carbontracker
Tracking and predicting the carbon footprint of training deep learning models.
- Homepage:
- Documentation: https://carbontracker.readthedocs.io/
- Licenses: MIT License Copyright (c) 2020 Lasse F. Wolff Anthony & Benjamin Kanding Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Latest release: 2.1.2 (published 3 months ago)
- Last Synced: 2025-04-26T12:32:11.609Z (1 day ago)
- Versions: 26
- Dependent Packages: 2
- Dependent Repositories: 4
- Downloads: 2,310 Last month
- Docker Downloads: 41
-
Rankings:
- Dependent packages count: 3.161%
- Docker downloads count: 3.264%
- Average: 5.085%
- Downloads: 6.422%
- Dependent repos count: 7.495%
- Maintainers (3)
Dependencies
- actions/checkout v3 composite
- actions/setup-python v3 composite
- pypa/gh-action-pypi-publish v1.4.2 composite
- actions/checkout v3 composite
- actions/setup-python v3 composite
- geocoder *
- importlib-metadata *
- numpy *
- pandas *
- psutil *
- pynvml *
- requests *
Score: 15.82864281162386