Skip to content

Commit 4562a4a

Browse files
authored
Merge pull request #1156 from mlco2/feat/intel_npu_support
Intel NPU demo
2 parents 440493e + 86a936b commit 4562a4a

3 files changed

Lines changed: 250 additions & 0 deletions

File tree

docs/how-to/examples.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
```

examples/intel_npu.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
3+
## Prerequisites
4+
5+
Install OpenVINO and PyTorch CPU versions :
6+
uv pip install -U openvino
7+
uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
8+
9+
### Linux support for Intel® Neural Processing Unit (Intel® NPU)
10+
11+
Chaeck if drivers are installed and the NPU is available:
12+
`sudo dmesg | grep vpu` should show something like this if the NPU is properly initialized:
13+
[nnnnn] [drm] Initialized intel_vpu 1.0.0 for 0000:00:0b.0 on minor 0
14+
15+
Else, have a look to https://github.com/intel/linux-npu-driver/releases
16+
17+
### Windows support
18+
19+
You need to install the [Intel drivers](https://www.intel.com/content/www/us/en/download/794734/intel-npu-driver-windows.html).
20+
21+
## Running the example
22+
23+
`uv run examples/intel_npu.py`
24+
25+
"""
26+
27+
import os
28+
29+
import openvino as ov
30+
import torch
31+
from torchvision.models import ShuffleNet_V2_X1_0_Weights, shufflenet_v2_x1_0
32+
33+
from codecarbon import track_emissions
34+
35+
NUMBER_OF_IMAGES = 30
36+
37+
38+
@track_emissions(
39+
measure_power_secs=5,
40+
save_to_file=False,
41+
log_level="debug",
42+
)
43+
def openvino_npu_task():
44+
print("openvino", ov.__version__)
45+
model = shufflenet_v2_x1_0(weights=ShuffleNet_V2_X1_0_Weights.DEFAULT)
46+
print("training before eval", model.training)
47+
model.eval()
48+
print("training after eval", model.training)
49+
# Create a dummy input tensor with the appropriate shape for the model (e.g., [10, 3, 224, 224] for image classification).
50+
example = torch.randn(NUMBER_OF_IMAGES, 3, 224, 224)
51+
ov_model = ov.convert_model(
52+
model, example_input=(example,), input=[NUMBER_OF_IMAGES, 3, 224, 224]
53+
)
54+
print("input partial shape", ov_model.input(0).partial_shape)
55+
for i, out in enumerate(ov_model.outputs):
56+
print("output", i, out.partial_shape)
57+
core = ov.Core()
58+
print("devices", core.available_devices)
59+
if "NPU" in core.available_devices:
60+
compiled = core.compile_model(ov_model, "NPU")
61+
print("compiled on", compiled.get_property("EXECUTION_DEVICES"))
62+
result = compiled({0: example.numpy()})
63+
print("result shape", result[compiled.output(0)].shape)
64+
# output_data = result[compiled.output(0)]
65+
# Classification outputs are flat vectors (e.g. [10, 1000]).
66+
# Render the top-3 scores as simple ASCII bars for terminal readability.
67+
output_data = next(iter(result.values()))
68+
# For classification output shaped like [batch, classes], show top-k per image.
69+
# If output is 1D, treat it as a single-image prediction.
70+
if output_data.ndim == 1:
71+
output_data = output_data.reshape(1, -1)
72+
73+
for image_idx, image_scores in enumerate(output_data):
74+
top_k = min(3, image_scores.size)
75+
top_indices = image_scores.argsort()[-top_k:][::-1]
76+
top_values = image_scores[top_indices]
77+
max_score = float(top_values.max()) if top_values.size else 1.0
78+
scale = max_score if max_score > 0 else 1.0
79+
80+
print(f"Top predictions for image {image_idx} (index: score | bar):")
81+
for idx, score in zip(top_indices, top_values):
82+
bar_len = int(40 * float(score) / scale)
83+
bar = "#" * max(0, bar_len)
84+
print(f" {int(idx):4d}: {float(score): .6f} | {bar}")
85+
else:
86+
print("NPU unavailable")
87+
88+
89+
# Display my process PID so you can check its power usage with `sudo dmesg | grep vpu` in another terminal.
90+
print(f"Process PID: {os.getpid()}")
91+
openvino_npu_task()

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ nav:
147147
- HuggingFace Diffusers: how-to/diffusers.md
148148
- LLMs and Agents: how-to/agents.md
149149
- How-to Guides:
150+
- Examples: how-to/examples.md
150151
- Configure CodeCarbon: how-to/configuration.md
151152
- Compare Model Efficiency: tutorials/comparing-model-efficiency.md
152153
- Dashboard & Visualization:

0 commit comments

Comments
 (0)