CodeCarbon Documentation

repository·master·Indexed 23 days ago

https://github.com/mlco2/codecarbon

A lightweight Python library to estimate and track carbon emissions produced by local computing hardware (CPU, GPU, and RAM) during code execution. Includes documentation for the carbonserver API, database management via Alembic and SQLAlchemy, Docker deployment, and various tracking patterns including decorators, Keras callbacks, and Comet integration.

Tokens
44.7K
Snippets
127
Records
286
Agent score
83%

What's inside CodeCarbon

  1. Overview of CodeCarbon usage examples

    master

    The CodeCarbon repository provides a wide range of examples categorized by use case:

    • Quick Start: Hardware detection (print_hardware.py) and tracking external CLI tools (command_line_tool.py).
    • Tracking Methods: Decorators, context managers, Keras/TensorFlow callbacks, and API integration.
    • Machine Learning: Training (MNIST with TensorFlow, scikit-learn, PyTorch multi-GPU) and Hyperparameter Search (Grid/Random search).
    • Inference: BERT, Hugging Face Transformers (SmolLM2), and local LLM APIs via Ollama.
    • Hardware & Parallelism: Intel NPU support, full CPU utilization, multithreading, and RAPL vs CPU load comparisons.
    • Integration & Logging: Exporting to BoAmps format, CSV files, Google Cloud Logging, Prometheus, Comet.ml, and Logfire.
    • Metrics: Calculating Power Usage Effectiveness (PUE) and Water Usage Effectiveness (WUE).
    • Notebooks: Interactive usage in Jupyter environments.
  2. Features of the carbonboard offline dashboard

    master

    The carbonboard local dashboard provides several visualization capabilities for analyzing CSV emissions logs:

    • Summary and Equivalents: View net power consumption and emissions across projects, with real-world comparisons (e.g., weekly share of an average American household, miles driven, or LCD TV time).
    • Regional Comparisons: Benchmark emissions against electricity grids across different countries to understand regional carbon intensity variations.
    • Cloud Regions: Benchmark equivalent emissions across different cloud provider regions to identify the most eco-friendly hosting locations.
  3. Features of the CodeCarbon online dashboard

    master

    The online dashboard provides high-level and granular views of emissions data:

    • Organization & Project Overview: Displays global energy consumption and emissions at the organization level, broken down by project, including real-world comparison points.
    • Experiments, Runs & Detailed Metrics: Projects are organized into experiments, which contain multiple runs. Visualizations include bar charts for total emissions per experiment and bubble charts for individual runs.
    • Run Drill-Down: Clicking a run bubble reveals full time-series graphs and detailed metadata, including timestamps, energy breakdowns, and hardware information.
    • Global Carbon Intensity: Visualizes the regional carbon intensity of electricity production by country to assist in deployment region selection.
  4. Understand the CodeCarbon project structure

    master

    CodeCarbon is a mono-repo consisting of four main components:

    1. codecarbon/: The core Python package used for tracking carbon emissions via CLI or library calls.
    2. carbonserver/: A FastAPI-based REST API with PostgreSQL integration for data persistence and user management.
    3. dashboard/viz: A Python-based interactive dashboard using Dash and Plotly that visualizes emission data from CSV files.
    4. webapp/: A modern React/Next.js web application that connects to the carbonserver API.

    Core logic and tests are located in tests/, usage examples in examples/, and deployment configurations in deploy/.

  5. Use the Explicit Object Pattern for long experiments

    master

    For iterative experiments, such as training multiple models in a loop or running long-lived Jupyter notebooks, use the explicit object pattern with EmissionsTracker. This provides fine-grained control over when tracking starts and stops.

    Best Practice: Always wrap your training code in a try...finally block. Calling tracker.stop() inside the finally block ensures that CodeCarbon's internal scheduler is properly shut down and background threads are terminated, even if your training code encounters an error.

    tracker = EmissionsTracker(project_name="my_experiment")
    tracker.start()
    try:
        # Your training code here
        pass
    finally:
        emissions = tracker.stop()
  6. Choosing between CodeCarbon and EcoLogits

    master

    Decide which tool to use based on where your code is executing:

    • Use CodeCarbon when you run code on hardware you control. This includes training models, local inference, or any code running on your own machine, a local server, or a cloud Virtual Machine (VM).
    • Use EcoLogits when you are calling remote GenAI APIs (such as OpenAI, Anthropic, Mistral, etc.) and want to estimate the environmental impact of those specific requests.

    Both tools are complementary: CodeCarbon tracks local computing, while EcoLogits tracks remote API inference.

  7. How CodeCarbon calculates emissions

    master

    CodeCarbon estimates the electricity power consumption of your hardware (specifically CPU, GPU, and RAM) and applies the carbon intensity of the region where the computation is occurring.

    Note that it does not explicitly model disk I/O, network transfers, displays, or cooling, as these are typically much smaller for local code-level experiments and are not easily exposed through low-overhead measurement interfaces.

  8. How CodeCarbon estimates emissions and data sources

    master

    CodeCarbon focuses on the direct emissions produced by running your code. It uses different data sources for energy carbon intensity depending on your infrastructure:

    Cloud Computing

    • Google Cloud: Uses Google's published carbon intensity data.
    • AWS: Amazon does not make datacenter carbon footprints publicly available.
    • Azure: Microsoft provides a Sustainability Calculator but does not publish specific datacenter carbon intensity.

    Private Infrastructure

    • Our World in Data: Used when available via ourworldindata.org.
    • Global Petrol Prices: Uses the electricity mix from globalpetrolprices.com multiplied by the carbon intensity of the electricity source.
    • Default: If no specific data is available, CodeCarbon defaults to 475 gCO2.eq/kWh (based on IEA data).
  9. How CodeCarbon tracks CPU energy consumption

    master

    CodeCarbon uses a prioritized hierarchy to estimate CPU energy consumption. It first attempts to use low-level hardware interfaces for high accuracy:

    1. Hardware Counters: It tries to read energy consumption directly from interfaces like RAPL (Running Average Power Limit) or powermetrics.
    2. Fallback Mode: If hardware counters are unavailable, CodeCarbon switches to an estimation model:
      • It detects the specific CPU hardware and maps it to a database of 2000+ Intel and AMD CPUs to find their Thermal Design Power (TDP).
      • If the CPU is unknown, it applies a global constant.
      • If psutil is installed, it estimates consumption by combining the TDP with the current CPU load.
      • Approximation: In fallback mode, CodeCarbon assumes an average power consumption of 50% of the TDP.

    Energy is calculated using the formula: Energy = Power * Time (measured in kWh).

  10. Configure CodeCarbon parameters

    master

    CodeCarbon parameters can be set using several methods. The priority order for these methods is defined in the Configuration guide. You can provide parameters via:

    • Initializing EmissionsTracker() or OfflineEmissionsTracker()
    • Using the @track_emissions decorator
    • Providing a configuration file
    • Setting environment variables

    Key Concepts:

    • PUE (Power Usage Effectiveness): A user-provided multiplication factor. Values typically range from 1.1 (green datacenters) to 2.2 (older datacenters).
    • GPU Selection: If you use CUDA_VISIBLE_DEVICES or ROCR_VISIBLE_DEVICES to restrict GPU access, CodeCarbon will automatically populate gpu_ids. However, providing a manual gpu_ids value will override this automatic detection.
  11. Understand equivalent emission comparisons

    master

    The CodeCarbon dashboard provides context for your carbon impact by comparing your energy usage to common activities. These comparisons use the following approximate emission factors:

    Car Usage

    • Factor: 0.12 kgCO₂ per kilometer driven.
    • Comparison: 1 kWh of energy is approximately equivalent to 8.33 kilometers driven.

    TV Usage

    • Factor: Based on an average consumption of 138 Wh per day (approx. 21.2 W per hour).
    • Comparison: 1 kWh of energy is approximately equivalent to 11.9 hours of TV usage.

    US Citizen Weekly Emissions

    • Factor: Based on an average of 13.3 tons of CO₂ equivalent per year (approx. 0.256 tons per week).
    • Comparison: Uses the formula Total Emissions (tons) / 0.256 to show how your usage compares to a typical US citizen's weekly footprint.

    Note: These are approximations based on European and US averages and may vary based on local grid intensity and vehicle efficiency.