Simple GPU/CPU mode switching utilities with automatic detection
Project description
Smart GPU
Simple GPU/CPU mode switching utilities with automatic detection.
Problem Statement
When developing data science and machine learning applications, you often need to support both CPU and GPU environments. This creates several challenges:
The Challenge
- Code Duplication: You need separate code paths for CPU (NumPy/Pandas) and GPU (CuPy/CuDF) operations
- Environment Complexity: Different users have different hardware setups (some with GPUs, some without)
- Deployment Issues: Code that works on your GPU machine might fail on CPU-only servers
- Maintenance Overhead: Keeping CPU and GPU code paths in sync is error-prone and time-consuming
- Platform Limitations: GPU libraries like CuPy and CuDF only work on Linux with NVIDIA GPUs
The Solution
Smart GPU provides a unified interface that automatically detects your environment and uses the best available resources:
- Write Once, Run Anywhere: Use the same code on CPU and GPU systems
- Automatic Detection: Seamlessly switches between CPU and GPU modes based on hardware availability
- Graceful Fallbacks: Automatically falls back to CPU mode when GPU is unavailable
- Environment Control: Override automatic detection with environment variables when needed
- Cross-Platform: Works on Linux (with GPU support), macOS, and Windows (CPU-only)
Why Smart GPU?
Before Smart GPU
# You had to write code like this:
import numpy as np
import pandas as pd
try:
import cupy as cp
import cudf
USE_GPU = True
except ImportError:
USE_GPU = False
# Duplicate code paths
if USE_GPU:
# GPU code path
arr = cp.array([1, 2, 3, 4, 5])
df = cudf.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
result = cp.sum(arr)
else:
# CPU code path
arr = np.array([1, 2, 3, 4, 5])
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
result = np.sum(arr)
With Smart GPU
# Clean, unified interface
from smart_gpu import gpu_utils, array, DataFrame
# Same code works everywhere
arr = array([1, 2, 3, 4, 5])
df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
result = gpu_utils.np.sum(arr)
# Automatically uses GPU if available, CPU otherwise
print(f"Using: {'GPU' if gpu_utils.is_gpu_mode else 'CPU'}")
Even Simpler with Direct Import
from smart_gpu import gpu_utils
# Import np and pd directly - they automatically switch between GPU/CPU
np = gpu_utils.np
pd = gpu_utils.pd
# Use exactly like regular NumPy/Pandas
arr = np.array([1, 2, 3, 4, 5])
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
result = np.sum(arr)
Real-World Benefits
- Faster Development: Write code once, test everywhere
- Reduced Bugs: No more "works on my machine" issues
- Easier Deployment: Same codebase works on laptops, servers, and cloud environments
- Better Performance: Automatically leverages GPU acceleration when available
- Simplified CI/CD: No need for separate CPU and GPU test environments
Features
- Automatic GPU Detection: Automatically detects NVIDIA GPU hardware and CUDA availability
- Smart Mode Switching: Seamlessly switches between CPU (NumPy/Pandas) and GPU (CuPy/CuDF) modes
- Environment Variable Control: Override detection with environment variables
- Cross-Platform Support: Works on Linux (with GPU support) and other platforms (CPU-only)
- Easy Integration: Drop-in replacement for NumPy and Pandas operations
Installation
Basic Installation (CPU-only)
pip install smart-gpu
With GPU Support (Linux only)
pip install smart-gpu[gpu]
Development Installation
git clone https://github.com/ardydedase/smart-gpu.git
cd smart-gpu
pip install -e ".[dev]"
Running Tests
# Run all tests
python -m pytest tests/ -v
# Run tests with coverage
python -m pytest tests/ --cov=smart_gpu --cov-report=term-missing
# Run tests and generate HTML coverage report
python -m pytest tests/ --cov=smart_gpu --cov-report=html
# Run specific test file
python -m pytest tests/test_gpu_utils.py -v
# Run tests matching a pattern
python -m pytest tests/ -k "gpu" -v
Quick Start
from smart_gpu import gpu_utils, array, DataFrame, to_cpu
# Check if GPU mode is active
print(f"GPU mode: {gpu_utils.is_gpu_mode}")
# Create arrays - automatically uses GPU if available
data = array([1, 2, 3, 4, 5])
print(f"Array type: {type(data)}")
# Create DataFrames - automatically uses GPU if available
df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print(f"DataFrame type: {type(df)}")
# Convert back to CPU if needed
cpu_data = to_cpu(data)
cpu_df = to_cpu(df)
Usage
Basic Usage
from smart_gpu import gpu_utils
# The package automatically detects GPU availability
utils = gpu_utils
# Use NumPy/CuPy operations
arr = utils.np.array([1, 2, 3, 4, 5])
result = utils.np.sum(arr)
# Use Pandas/CuDF operations
df = utils.pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
sum_result = df.sum()
Direct Import Method
from smart_gpu import gpu_utils
# Import np and pd directly for automatic GPU/CPU switching
np = gpu_utils.np
pd = gpu_utils.pd
# Now you can use np and pd normally - they'll automatically use GPU if available
arr = np.array([1, 2, 3, 4, 5])
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# All NumPy/Pandas operations work as expected
result = np.sum(arr)
mean_val = df.mean()
Manual Mode Control
from smart_gpu import set_gpu_mode, get_gpu_mode
# Force CPU mode
set_gpu_mode(False)
# Force GPU mode (if available)
set_gpu_mode(True)
# Check current mode
print(f"Current mode: {'GPU' if get_gpu_mode() else 'CPU'}")
Environment Variables
You can control the GPU mode using environment variables:
# Force CPU mode
export SMART_GPU_FORCE_CPU=true
# Explicitly enable GPU mode
export USE_GPU=true
# Explicitly disable GPU mode
export USE_GPU=false
Advanced Usage
from smart_gpu import GPUUtils
# Create a custom instance
gpu_utils = GPUUtils(gpu_mode=True) # Force GPU mode
# Create arrays and DataFrames
arr = gpu_utils.array([1, 2, 3, 4, 5])
df = gpu_utils.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Synchronize GPU operations
gpu_utils.synchronize()
# Convert to CPU format
cpu_arr = gpu_utils.to_cpu(arr)
cpu_df = gpu_utils.to_cpu(df)
API Reference
Core Functions
detect_gpu_hardware(): Detect NVIDIA GPU hardwareis_gpu_available(): Check if GPU libraries are availableauto_detect_gpu_mode(): Auto-detect optimal GPU modeset_gpu_mode(enabled): Set global GPU modeget_gpu_mode(): Get current GPU mode
GPUUtils Class
is_gpu_mode: Property indicating if GPU mode is activenp: NumPy or CuPy based on modepd: Pandas or CuDF based on modearray(data, **kwargs): Create array with appropriate libraryDataFrame(data, **kwargs): Create DataFrame with appropriate libraryto_cpu(data): Convert GPU data to CPU formatsynchronize(): Synchronize GPU operations
Convenience Functions
array(data, **kwargs): Create array using current modeDataFrame(data, **kwargs): Create DataFrame using current modeto_cpu(data): Convert data to CPU formatsynchronize(): Synchronize GPU operations
Platform Support
| Platform | GPU Support | CPU Support |
|---|---|---|
| Linux | ✅ NVIDIA + CUDA | ✅ |
| macOS | ❌ | ✅ |
| Windows | ❌ | ✅ |
Requirements
- Python 3.8+
- NumPy 1.20+
- Pandas 1.3+
Optional (for GPU support)
- Linux OS
- NVIDIA GPU
- CUDA Toolkit
- CuPy
- CuDF
Testing
The package includes comprehensive unit tests with 87% code coverage:
- GPU Detection Tests: Platform detection, hardware detection, library availability
- Mode Switching Tests: Environment variable overrides, manual mode control
- GPUUtils Class Tests: Array creation, DataFrame creation, data conversion
- Convenience Functions Tests: Global functions and utilities
- Logging Tests: Logger configuration and behavior
Test Coverage
# View current coverage
python -m pytest tests/ --cov=smart_gpu --cov-report=term-missing
# Generate detailed HTML report
python -m pytest tests/ --cov=smart_gpu --cov-report=html
# Open htmlcov/index.html in your browser
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite and ensure all tests pass
- Check code coverage and maintain or improve it
- Submit a pull request
Publishing
For information on publishing this package to PyPI, see:
- PyPI Setup Guide - How to set up authentication tokens
- Publishing Guide - Complete publishing workflow
License
MIT License - see LICENSE file for details.
Changelog
0.1.0
- Initial release
- Automatic GPU detection
- CPU/GPU mode switching
- Basic NumPy/Pandas compatibility
Project details
Release history Release notifications | RSS feed
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 smart_gpu-0.1.0.tar.gz.
File metadata
- Download URL: smart_gpu-0.1.0.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c815fd1323093a5cd62c050aaff60544c9b8919ffff67806a05c6d5247a5acd6
|
|
| MD5 |
4a4570ab5dd542055b62e672826c9e72
|
|
| BLAKE2b-256 |
68fd0d7c3908a0f283400b6ed225741887842f77aada577b9df75f496c246aa2
|
File details
Details for the file smart_gpu-0.1.0-py3-none-any.whl.
File metadata
- Download URL: smart_gpu-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2505b89779ecc83ec0bfa677c47ba149e6746bc2117e9a7a90b77cc517548a36
|
|
| MD5 |
c24431a8346efd4e0e60694a53c6066e
|
|
| BLAKE2b-256 |
838f2c9748fd74cbea77940dd182537ba18b0f6212469a6b775d0008b4091fdd
|