Skip to main content

A package for monitoring GPU power and energy consumption

Project description

PyGPUEnergy

A Python package for monitoring GPU power consumption and energy usage during code execution. This package provides both a decorator-based approach and a manual monitoring interface for tracking GPU metrics.

How it works

The package uses the nvidia-smi command to monitor GPU metrics. It runs a background process that periodically calls nvidia-smi to get the GPU metrics. The metrics are then saved to a CSV file.

During the execution of the code, the GPU monitor will record the start and end time of the user code region.

The energy consumption is calculated by equation:

$$E = \int P(t) dt$$

where $P(t)$ is the power consumption of the GPU at time $t$.

Installation

pip install PyGPUEnergy

Usage

Using the Decorator

from PyGPUEnergy.decorator import monitor_gpu

@monitor_gpu(gpu_id=0, sampling_period_ms=100)
def your_gpu_function():
    # Your GPU-intensive code here
    pass

The decorator supports the following parameters:

  • gpu_id: GPU device ID to monitor (default: 0)
  • sampling_period_ms: Sampling period in milliseconds (default: 100)

Manual Monitoring

from PyGPUEnergy.monitor import GPUMonitor

# Create monitor instance
monitor = GPUMonitor(gpu_id=0, sampling_period_ms=100)

# Start monitoring
monitor.start_monitoring()

# Your GPU-intensive code here

# Stop monitoring
monitor.stop_monitoring()

# Get metrics with energy consumption information
metrics_df = monitor.get_metrics()

Visualization

from PyGPUEnergy.visualize import plot_gpu_metrics

# Plot metrics from a log file
plot_gpu_metrics("path/to/log_file.csv", save_path="metrics_plot.png")

# Plot with custom settings
plot_gpu_metrics(
    "gpu_logs/log_file.csv",
    records_file="gpu_logs/gpu_records_0.json",  # Function execution records
    t0_file="gpu_logs/t0.txt",                   # Start time reference
    save_path="metrics_plot.png",
    show=True
)

Example

See the examples/usage_example.py file for a complete example of using the package. Here's a quick example:

from PyGPUEnergy.decorator import monitor_gpu
import torch

@monitor_gpu(gpu_id=0, sampling_period_ms=100)
def train_model():
    model = torch.nn.Linear(1000, 1000).cuda()
    optimizer = torch.optim.Adam(model.parameters())
    
    for _ in range(100):
        x = torch.randn(1000, 1000).cuda()
        y = model(x)
        loss = y.sum()
        loss.backward()
        optimizer.step()

License

MIT License

Contributing

This project is a very basic implementation and can be improved in many ways. Contributions are welcome! Please feel free to submit a Pull Request.

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

pygpuenergy-0.1.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

pygpuenergy-0.1.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file pygpuenergy-0.1.1.tar.gz.

File metadata

  • Download URL: pygpuenergy-0.1.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for pygpuenergy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 77b9164f353f47eb65a63a9a572ba7dcc73d0741ee08d2d31d33f26aed3d39bd
MD5 e947d5b42766629e01d7c9e4fc0ce25b
BLAKE2b-256 b58df4e2c61d95bd513967576dae8140b387179c82be4ec77a9fdc33275bd1df

See more details on using hashes here.

File details

Details for the file pygpuenergy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pygpuenergy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for pygpuenergy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 149804ed365317ca8febba2a85e01a3f3ce748f2e5a494084bd4c9cb13f4e3f9
MD5 d7c1d81ab96fa6bc755178c08e105d35
BLAKE2b-256 49854fb81ab085957effa8d49eb972e87f5a83983e38245d633c9266fb80e60c

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