Skip to main content

Tracking and predicting the carbon footprint of training deep learning models.

Project description

carbontracker

pypi Python 3.6 build License: MIT

About

carbontracker is a tool for tracking and predicting the carbon footprint of training deep learning models.

Installation

PyPi

pip install carbontracker

Basic usage

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.
  • verbose (default=0): Sets the level of verbosity.

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.

Aggregating log files

carbontracker supports aggregating all log files in a specified directory to a single estimate of the carbon footprint.

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

Compatability

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 device_by_pids parameter and means that it will never find any active processes for GPUs in affected containers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

carbontracker-1.1.1.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

carbontracker-1.1.1-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file carbontracker-1.1.1.tar.gz.

File metadata

  • Download URL: carbontracker-1.1.1.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

File hashes

Hashes for carbontracker-1.1.1.tar.gz
Algorithm Hash digest
SHA256 020b91581e2cf5128f31ee2bc391db5d13b432121d2052a23dc1a155a52a5cd4
MD5 34855efe9b062c421e450ca65d8a865a
BLAKE2b-256 3e759ac3c4c059648e6bea430c2fdd6bd5b1e3fac09156f1e19a19530cbfe203

See more details on using hashes here.

File details

Details for the file carbontracker-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: carbontracker-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

File hashes

Hashes for carbontracker-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1375ff93d24aa802f5d2cccc011144c10bd7a1beef35e8eb16a72fbe61930d0
MD5 165c063dcbccca4dd8aa39743fe16626
BLAKE2b-256 d2aea89ed8192278f6b5d5edb715f1ec4b7de35f72bd16ffe9144b6bcd2e4a5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page