Skip to main content

carbontracker

Build PyPI Python Unit Tests License Downloads

Website

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

Optional Dependencies

To generate PDF reports from carbontracker logs, install with the pdfreport extra:

pip install 'carbontracker[pdfreport]'

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 than epochs_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 after epochs_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 the CarbonTracker 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.
  • sim_cpu (default=None): Name of the simulated CPU. If set, will use simulated CPU power measurements.
  • sim_cpu_tdp (default=None): Thermal Design Power (TDP) in Watts for the simulated CPU. Required if sim_cpu is set.
  • sim_cpu_util (default=None): CPU utilization factor between 0 and 1. If not set, defaults to 0.5 (50% utilization).
  • sim_gpu (default=None): Name of the simulated GPU. If set, will use simulated GPU power measurements.
  • sim_gpu_watts (default=None): Power consumption in Watts for the simulated GPU. Required if sim_gpu is set.
  • sim_gpu_util (default=None): GPU utilization factor between 0 and 1. If not set, defaults to 0.5 (50% utilization).

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 gCO2eq/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 gCO2eq/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']

Generating PDF reports

Carbontracker can generate detailed PDF reports from log files. This feature requires the optional reportlab dependency.

Note: You must install the PDF report dependencies first:

pip install 'carbontracker[pdfreport]'

Example usage

from carbontracker.report import generate_report_from_log

generate_report_from_log("./logs/carbontracker.log", "./report.pdf")

The generated PDF includes:

  • Energy consumption metrics and visualizations
  • Carbon footprint analysis with CO2eq calculations
  • Power usage breakdown by component (CPU/GPU)
  • Training duration and efficiency metrics

Compatibility

carbontracker is compatible with:

Notes

Availability of GPUs and Slurm

  • Available GPU devices are determined by first checking the environment variable CUDA_VISIBLE_DEVICES (only if devices_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 the devices_by_pid parameter and means that it will never find any active processes for GPUs in affected containers.

Extending carbontracker

See CONTRIBUTING.md.

Star History

Star History Chart

carbontracker in media

  • Official press release from University of Copenhagen can be obtained here: en da

  • Carbontracker has received some attention in popular science forums within, and outside of, Denmark [1][2][3][4][5][6][7][8]

Metadata

Release files for carbontracker 2.4.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for carbontracker 2.4.7
File Size Uploaded
carbontracker-2.4.7.tar.gz 97.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for carbontracker 2.4.7
File Interpreter ABI Platform
carbontracker-2.4.7-py3-none-any.whl Python 3 none any Details

Total release size: 164.9 kB

Release files / carbontracker-2.4.7.tar.gz

Download URL carbontracker-2.4.7.tar.gz
Size 97.2 kB
Tags Source
SHA-256 checksum
How to use checksums
46ad3067e360f7616e4f92625f50fde2b3da20ee5531af54a9acf555824db378
BLAKE2b-256 checksum
How to use checksums
3b9872c5191f7b941cddac7cdd732a98afc605bc162fe13bf349963d79644b58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / carbontracker-2.4.7-py3-none-any.whl

Download URL carbontracker-2.4.7-py3-none-any.whl
Size 67.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b83395096344f6466d526e4f20c12d38bafbfc0a56fd793085b1ac7df8a0aa0f
BLAKE2b-256 checksum
How to use checksums
48d6a3b3cf7c4bfd9c96d51e58481759700479a07d76be4f8529800c329ba269
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

2.4.7 This release

2 release files

2.4.6

2 release files

2.4.5

2 release files

2.4.4

2 release files

2.4.3

2 release files

2.4.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page