torch benchmarking tool
Project description
PyTorch Model Benchmarking Tool
This tool provides a comprehensive set of utilities for benchmarking PyTorch models, including performance metrics, memory usage, and model statistics.
Features
- Measure inference latency on both CPU and GPU
- Track GPU memory usage
- Calculate model size and number of parameters
- Compute MACs (Multiply-Accumulate operations)
- Calculate model sparsity
- Generate visualizations of parameter distributions and weight distributions
- Provide formatted output of benchmark results
Installation
You can install the package using pip:
pip install pytorch-bench
Example
import torch
from torchvision.models import resnet50, ResNet50_Weights
from pytorch_bench import benchmark
# Load model and example input
model = resnet50(weights=ResNet50_Weights.DEFAULT)
example_input = torch.randn(1, 3, 224, 224)
# Run benchmark
results = benchmark(model, example_input)
You can run example.py to see the output in your terminal and play with the different functions.
Advanced Usage
Tracking gpu memory for a torch model
from pytorch_bench import track_gpu_memory
with track_gpu_memory():
# Your GPU operations here
pass
max_memory = track_gpu_memory.max_memory
current_memory = track_gpu_memory.current_memory
print(f"Max GPU memory used: {max_memory:.2f} MB")
print(f"Current GPU memory used: {current_memory:.2f} MB")
Getting info about GPU memory
from pytorch_bench import detailed_memory_info
detailed_memory_info()
Calculating model sparsity
from pytorch_bench import get_model_sparsity, get_layer_sparsity
sparsity = get_model_sparsity(model)
print(f"Model sparsity: {sparsity:.2f}")
get_layer_sparsity(model)
Visualizations
When plot=True is set in the benchmark function, two plots will be generated:
- num_parameters_distribution.png: Bar chart showing the number of parameters in each layer.
- weight_distribution.png: Histograms of weight distributions for each layer.
These plots can provide insights into the model's architecture and weight patterns.
Notes
- Ensure you have a CUDA-capable GPU for GPU benchmarking.
- The tool uses CUDA events for precise GPU timing.
- Memory usage is tracked using PyNVML.
- MACs calculation requires the torchprofile package.
Contributing
This project started as a personal tool to simplify the process of benchmarking models on EdgeAI resources. It's designed to be a lightweight, easy-to-use solution that can be quickly installed and utilized.
While this is primarily a personal project, I'm open to suggestions and improvements. If you have ideas or find any issues, feel free to:
- Open an issue on the GitHub repository to report bugs or suggest enhancements.
- Submit pull requests for minor fixes or improvements..
If you find this tool helpful, feel free to star the repository or share it with others who might benefit from it. Thanks for your interest!
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytorch_bench-0.1.3.tar.gz.
File metadata
- Download URL: pytorch_bench-0.1.3.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8e0b8afed5fc3ee9bdbc1fb142f94e44540f9b80a312c20147e57275b05f000
|
|
| MD5 |
ec1a9199997a02f8579690ebb5114d41
|
|
| BLAKE2b-256 |
5b24e60d37372f07a0d2f0d39e099b40183196992c28045316525c834aec9a28
|
File details
Details for the file pytorch_bench-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pytorch_bench-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e215b66a04c6144c09a3611304b112ab021d5103845a27ff6b05648417d2fd0
|
|
| MD5 |
1757e1f7e603b0d8c31e0a9d0ac4ccf2
|
|
| BLAKE2b-256 |
f08bd26e0a3bf60f654ab390185a5b280538c2bb4d6dc55a6a9e6f67bc3def17
|