|
| 1 | +# CodeCarbon Examples |
| 2 | + |
| 3 | +The directory [examples/](https://github.com/mlco2/codecarbon/tree/master/examples) contains practical examples demonstrating how to use CodeCarbon to track carbon emissions from your computing tasks. The examples below are organized by use case rather than alphabetically. |
| 4 | + |
| 5 | +## Quick Start Examples |
| 6 | + |
| 7 | +| Example | Type | Description | |
| 8 | +|---------|------|-------------| |
| 9 | +| [print_hardware.py](https://github.com/mlco2/codecarbon/blob/master/examples/print_hardware.py) | Python Script | Detect and display available hardware (CPU, GPU, RAM) on your system | |
| 10 | +| [command_line_tool.py](https://github.com/mlco2/codecarbon/blob/master/examples/command_line_tool.py) | Python Script | Track emissions of external command-line tools executed via subprocess | |
| 11 | + |
| 12 | +## Tracking Methods |
| 13 | + |
| 14 | +| Example | Type | Description | |
| 15 | +|---------|------|-------------| |
| 16 | +| [mnist_decorator.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist_decorator.py) | Python Script | Track emissions using the `@track_emissions` decorator on functions | |
| 17 | +| [mnist_context_manager.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist_context_manager.py) | Python Script | Track emissions using `EmissionsTracker` as a context manager (with statement) | |
| 18 | +| [mnist_callback.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist_callback.py) | Python Script | Track emissions using Keras/TensorFlow callbacks during model training | |
| 19 | +| [api_call_demo.py](https://github.com/mlco2/codecarbon/blob/master/examples/api_call_demo.py) | Python Script | Track emissions and send data to the CodeCarbon API with `@track_emissions` | |
| 20 | + |
| 21 | +## Basic Model Training |
| 22 | + |
| 23 | +| Example | Type | Description | |
| 24 | +|---------|------|-------------| |
| 25 | +| [mnist.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist.py) | Python Script | Train a simple neural network on MNIST dataset with TensorFlow | |
| 26 | +| [mnist-sklearn.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist-sklearn.py) | Python Script | Train a scikit-learn model on MNIST and track emissions | |
| 27 | +| [pytorch-multigpu-example.py](https://github.com/mlco2/codecarbon/blob/master/examples/pytorch-multigpu-example.py) | Python Script | PyTorch CNN training on MNIST with multi-GPU support | |
| 28 | + |
| 29 | +## Hyperparameter Search |
| 30 | + |
| 31 | +| Example | Type | Description | |
| 32 | +|---------|------|-------------| |
| 33 | +| [mnist_grid_search.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist_grid_search.py) | Python Script | Grid search hyperparameter optimization with emission tracking | |
| 34 | +| [mnist_random_search.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist_random_search.py) | Python Script | Random search hyperparameter optimization with emission tracking | |
| 35 | + |
| 36 | +## ML Model Inference |
| 37 | + |
| 38 | +| Example | Type | Description | |
| 39 | +|---------|------|-------------| |
| 40 | +| [bert_inference.py](https://github.com/mlco2/codecarbon/blob/master/examples/bert_inference.py) | Python Script | BERT language model inference with task-level tracking | |
| 41 | +| [task_inference.py](https://github.com/mlco2/codecarbon/blob/master/examples/task_inference.py) | Python Script | Track emissions for different inference tasks (load dataset, build model, predict) | |
| 42 | +| [task_loop_same_task.py](https://github.com/mlco2/codecarbon/blob/master/examples/task_loop_same_task.py) | Python Script | Track emissions running the same task multiple times | |
| 43 | +| [transformers_smollm2.py](https://github.com/mlco2/codecarbon/blob/master/examples/transformers_smollm2.py) | Python Script | Small language model (SmolLM2) inference from Hugging Face | |
| 44 | +| [ollama_local_api.py](https://github.com/mlco2/codecarbon/blob/master/examples/ollama_local_api.py) | Python Script | Track emissions of local LLM API calls using Ollama | |
| 45 | + |
| 46 | +## Hardware-Specific Examples |
| 47 | + |
| 48 | +| Example | Type | Description | |
| 49 | +|---------|------|-------------| |
| 50 | +| [intel_npu.py](https://github.com/mlco2/codecarbon/blob/master/examples/intel_npu.py) | Python Script | Intel Neural Processing Unit (NPU) support for model inference | |
| 51 | +| [full_cpu.py](https://github.com/mlco2/codecarbon/blob/master/examples/full_cpu.py) | Python Script | Demonstrate full CPU utilization and emission tracking | |
| 52 | + |
| 53 | +## Parallel & Concurrent Processing |
| 54 | + |
| 55 | +| Example | Type | Description | |
| 56 | +|---------|------|-------------| |
| 57 | +| [multithread.py](https://github.com/mlco2/codecarbon/blob/master/examples/multithread.py) | Python Script | Track emissions from multithreaded workloads | |
| 58 | +| [compare_cpu_load_and_RAPL.py](https://github.com/mlco2/codecarbon/blob/master/examples/compare_cpu_load_and_RAPL.py) | Python Script | Compare RAPL power measurement vs CPU load estimation in parallel workloads | |
| 59 | + |
| 60 | +## Logging & Output Integration |
| 61 | + |
| 62 | +| Example | Type | Description | |
| 63 | +|---------|------|-------------| |
| 64 | +| [logging_to_file.py](https://github.com/mlco2/codecarbon/blob/master/examples/logging_to_file.py) | Python Script | Save emissions data to a local CSV file | |
| 65 | +| [logging_to_file_exclusive_run.py](https://github.com/mlco2/codecarbon/blob/master/examples/logging_to_file_exclusive_run.py) | Python Script | Long-running process with exclusive file logging | |
| 66 | +| [logging_to_google_cloud.py](https://github.com/mlco2/codecarbon/blob/master/examples/logging_to_google_cloud.py) | Python Script | Send emissions data to Google Cloud Logging | |
| 67 | +| [logfire_metrics.py](https://github.com/mlco2/codecarbon/blob/master/examples/logfire_metrics.py) | Python Script | Integrate CodeCarbon with Logfire metrics platform | |
| 68 | +| [prometheus_call.py](https://github.com/mlco2/codecarbon/blob/master/examples/prometheus_call.py) | Python Script | Export emissions metrics to Prometheus | |
| 69 | +| [mnist-comet.py](https://github.com/mlco2/codecarbon/blob/master/examples/mnist-comet.py) | Python Script | Integrate emission tracking with Comet.ml experiment tracking | |
| 70 | + |
| 71 | +## Metrics & Analysis |
| 72 | + |
| 73 | +| Example | Type | Description | |
| 74 | +|---------|------|-------------| |
| 75 | +| [pue.py](https://github.com/mlco2/codecarbon/blob/master/examples/pue.py) | Python Script | Calculate Power Usage Effectiveness (PUE) with CodeCarbon | |
| 76 | +| [wue.py](https://github.com/mlco2/codecarbon/blob/master/examples/wue.py) | Python Script | Calculate Water Usage Effectiveness (WUE) of your computing | |
| 77 | + |
| 78 | +## Interactive Notebooks |
| 79 | + |
| 80 | +| Example | Type | Description | |
| 81 | +|---------|------|-------------| |
| 82 | +| [notebook.ipynb](https://github.com/mlco2/codecarbon/blob/master/examples/notebook.ipynb) | Jupyter Notebook | Basic CodeCarbon usage in Jupyter environment | |
| 83 | +| [compare_cpu_load_and_RAPL.ipynb](https://github.com/mlco2/codecarbon/blob/master/examples/compare_cpu_load_and_RAPL.ipynb) | Jupyter Notebook | Compare different power measurement methods (RAPL vs CPU load) | |
| 84 | +| [local_llms.ipynb](https://github.com/mlco2/codecarbon/blob/master/examples/local_llms.ipynb) | Jupyter Notebook | Track emissions of local LLM inference | |
| 85 | + |
| 86 | +## Setup & Configuration |
| 87 | + |
| 88 | +| Item | Description | |
| 89 | +|------|-------------| |
| 90 | +| [requirements-examples.txt](https://github.com/mlco2/codecarbon/blob/master/examples/requirements-examples.txt) | Python dependencies for running the examples | |
| 91 | +| [rapl/](https://github.com/mlco2/codecarbon/blob/master/examples/rapl/) | Setup instructions for RAPL power measurement support | |
| 92 | +| [slurm_rocm/](https://github.com/mlco2/codecarbon/blob/master/examples/slurm_rocm/) | Configuration for SLURM job scheduler with ROCm GPU support | |
| 93 | +| [notebooks/](https://github.com/mlco2/codecarbon/blob/master/examples/notebooks/) | Additional Jupyter notebooks | |
| 94 | + |
| 95 | +## Running the Examples |
| 96 | + |
| 97 | +### Prerequisites |
| 98 | +```bash |
| 99 | +# Install CodeCarbon |
| 100 | +pip install codecarbon |
| 101 | + |
| 102 | +# Install example dependencies |
| 103 | +# WARNING: it will download huge pacakge. We recommand you to install only the minimum you need for the example you want to run. |
| 104 | +pip install -r examples/requirements-examples.txt |
| 105 | +``` |
| 106 | + |
| 107 | +### Run a Python Example |
| 108 | +```bash |
| 109 | +# Using uv (recommended) |
| 110 | +uv run examples/print_hardware.py |
| 111 | + |
| 112 | +# Or with Python directly |
| 113 | +python examples/print_hardware.py |
| 114 | +``` |
| 115 | + |
| 116 | +### Run a Jupyter Notebook |
| 117 | +```bash |
| 118 | +jupyter notebook examples/notebook.ipynb |
| 119 | +``` |
| 120 | + |
| 121 | +Or just open it in VS Code. |
| 122 | + |
| 123 | +## Common Patterns |
| 124 | + |
| 125 | +### Track with Decorator |
| 126 | +```python |
| 127 | +from codecarbon import track_emissions |
| 128 | + |
| 129 | +@track_emissions(project_name="my_project") |
| 130 | +def my_function(): |
| 131 | + # Your code here |
| 132 | + pass |
| 133 | +``` |
| 134 | + |
| 135 | +### Track with Context Manager |
| 136 | +```python |
| 137 | +from codecarbon import EmissionsTracker |
| 138 | + |
| 139 | +with EmissionsTracker() as tracker: |
| 140 | + # Your code here |
| 141 | + pass |
| 142 | +``` |
| 143 | + |
| 144 | +### Track Specific Tasks |
| 145 | +```python |
| 146 | +from codecarbon import EmissionsTracker |
| 147 | + |
| 148 | +tracker = EmissionsTracker() |
| 149 | +tracker.start() |
| 150 | +tracker.start_task("data_loading") |
| 151 | +# Load data... |
| 152 | +tracker.stop_task() |
| 153 | + |
| 154 | +tracker.start_task("training") |
| 155 | +# Train model... |
| 156 | +tracker.stop_task() |
| 157 | +tracker.stop() |
| 158 | +``` |
0 commit comments