MUFASA
A Python utility module for CPU core management and GPU memory optimization, particularly useful for machine learning workflows.
Installation
You can install MUFASA directly from PyPI:
pip install mufasa-polimi
Features
- CPU core detection and optimization for SLURM environments
- Automated GPU memory management and cleanup
- Detailed memory usage reporting
Usage
Core Management Functions
from mufasa import getCoreAffinity, setOptimalWorkers
# Get available CPU cores
cpu_count = getCoreAffinity()
print(f"Available CPU cores: {cpu_count}")
# Set optimal number of worker processes
workers = setOptimalWorkers()
print(f"Optimal worker count: {workers}")
getCoreAffinity()
Detects the number of available CPU cores, taking into account SLURM job allocations if running in a SLURM environment. Returns the minimum between SLURM-allocated CPUs and system-available CPUs, or the total system CPU count if not in a SLURM environment.
setOptimalWorkers()
Similar to getCoreAffinity(), but defaults to 1 if no SLURM environment is detected. Useful for setting worker counts in parallel processing scenarios.
GPU Memory Management
from mufasa import gpuClean
# Basic cleanup
freed_count, freed_memory = gpuClean()
# Detailed cleanup with verbose output
freed_count, freed_memory = gpuClean(
exclude_vars=['model', 'optimizer'], # Variables to preserve
verbose=True # Enable detailed reporting
)
gpuClean(local_vars=None, exclude_vars=None, verbose=False)
Automatically detects and frees GPU memory by cleaning up tensor variables.
Parameters:
local_vars(dict, optional): Dictionary of local variables to clean. If None, uses the calling frame's locals.exclude_vars(list, optional): List of variable names to exclude from cleanup.verbose(bool): Whether to print detailed information about cleaned variables.
Returns:
- tuple: (freed_count, freed_memory_mb)
- freed_count: Number of tensors freed
- freed_memory_mb: Approximate memory freed in MB
Features:
- Cleans up PyTorch tensors in local scope
- Handles nested tensors in dictionaries and lists
- Provides detailed memory usage reports when verbose=True
- Allows excluding specific variables from cleanup
- Automatically triggers garbage collection and GPU memory cache clearing
Example with Verbose Output:
import torch
from mufasa import gpuClean
# Create some example tensors
tensor1 = torch.randn(1000, 1000).cuda()
tensor2 = torch.randn(2000, 2000).cuda()
# Clean up with detailed output
freed_count, freed_memory = gpuClean(verbose=True)
The verbose output includes:
- Table of cleaned tensors with their shapes and sizes
- Total number of tensors freed
- Total memory freed
- Current GPU memory allocation status
- List of excluded variables (if any)
Notes
- SLURM-specific features require a SLURM environment
- GPU cleaning functions require PyTorch and a CUDA-capable GPU
- Memory sizes are reported in MB or GB depending on the size
- The module uses the
richlibrary for formatted console output in verbose mode
Metadata
Release files for mufasa-polimi 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mufasa_polimi-0.0.5.tar.gz | 16.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mufasa_polimi-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.2 kB
Release files / mufasa_polimi-0.0.5.tar.gz
| Download URL | mufasa_polimi-0.0.5.tar.gz |
|---|---|
| Size | 16.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
61d2d8052fa3c1b4aeafee939888f4f5c6969598872757e41c0fbe1445f5b50a
|
|
BLAKE2b-256 checksum How to use checksums |
2c3a48a0d26b01fce0e278eb6b33fc5b0345c32e51a0355354f50d25612fa087
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2024.
Transparency logRelease files / mufasa_polimi-0.0.5-py3-none-any.whl
| Download URL | mufasa_polimi-0.0.5-py3-none-any.whl |
|---|---|
| Size | 17.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9e1fd1d3550aa6460b1c2179730b827710ec499acdca2790cbd2902244fe922d
|
|
BLAKE2b-256 checksum How to use checksums |
a76f9f94b792b9103eadb3c775574775f7ef4fd991dfb093edbf12784ffa3bb3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2024.
Transparency log