Reference for ultralytics/utils/autodevice.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/autodevice.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.autodevice.GPUInfo
GPUInfo()
Manages NVIDIA GPU information via pynvml with robust error handling.
Provides methods to query detailed GPU statistics (utilization, memory, temp, power) and select the most idle GPUs based on configurable criteria. It safely handles the absence or initialization failure of the pynvml library by logging warnings and disabling related features, preventing application crashes.
Includes fallback logic using torch.cuda
for basic device counting if NVML is unavailable during GPU
selection. Manages NVML initialization and shutdown internally.
Attributes:
Name | Type | Description |
---|---|---|
pynvml |
module | None
|
The |
nvml_available |
bool
|
Indicates if |
gpu_stats |
List[Dict[str, Any]]
|
A list of dictionaries, each holding stats for one GPU. Populated on
initialization and by |
Methods:
Name | Description |
---|---|
refresh_stats |
Refresh the internal gpu_stats list by querying NVML. |
print_status |
Print GPU status in a compact table format using current stats. |
select_idle_gpu |
Select the most idle GPUs based on utilization and free memory. |
shutdown |
Shut down NVML if it was initialized. |
Examples:
Initialize GPUInfo and print status
>>> gpu_info = GPUInfo()
>>> gpu_info.print_status()
Select idle GPUs with minimum memory requirements
>>> selected = gpu_info.select_idle_gpu(count=2, min_memory_fraction=0.2)
>>> print(f"Selected GPU indices: {selected}")
Source code in ultralytics/utils/autodevice.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
__del__
__del__()
Ensure NVML is shut down when the object is garbage collected.
Source code in ultralytics/utils/autodevice.py
60 61 62 |
|
print_status
print_status()
Print GPU status in a compact table format using current stats.
Source code in ultralytics/utils/autodevice.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
refresh_stats
refresh_stats()
Refresh the internal gpu_stats list by querying NVML.
Source code in ultralytics/utils/autodevice.py
73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
select_idle_gpu
select_idle_gpu(
count: int = 1, min_memory_fraction: float = 0, min_util_fraction: float = 0
) -> List[int]
Select the most idle GPUs based on utilization and free memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
count
|
int
|
The number of idle GPUs to select. |
1
|
min_memory_fraction
|
float
|
Minimum free memory required as a fraction of total memory. |
0
|
min_util_fraction
|
float
|
Minimum free utilization rate required from 0.0 - 1.0. |
0
|
Returns:
Type | Description |
---|---|
List[int]
|
Indices of the selected GPUs, sorted by idleness (lowest utilization first). |
Notes
Returns fewer than 'count' if not enough qualify or exist. Returns basic CUDA indices if NVML fails. Empty list if no GPUs found.
Source code in ultralytics/utils/autodevice.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
shutdown
shutdown()
Shut down NVML if it was initialized.
Source code in ultralytics/utils/autodevice.py
64 65 66 67 68 69 70 71 |
|