|
1 | 1 | import configparser |
2 | 2 | import os |
3 | 3 | from pathlib import Path |
4 | | -from typing import List, Union |
| 4 | +from typing import List, Optional, Union |
5 | 5 |
|
6 | 6 | from codecarbon.external.logger import logger |
7 | 7 |
|
@@ -73,6 +73,43 @@ def parse_gpu_ids(gpu_ids: Union[str, List[int]]) -> List[str]: |
73 | 73 | ) |
74 | 74 |
|
75 | 75 |
|
| 76 | +def normalize_gpu_ids( |
| 77 | + gpu_ids: Optional[Union[str, List[Union[int, str]]]], |
| 78 | +) -> Optional[List[Union[int, str]]]: |
| 79 | + """ |
| 80 | + Normalize GPU IDs from config/user input into a list of ids consumable by hardware |
| 81 | + resolution code. |
| 82 | +
|
| 83 | + Supports: |
| 84 | + - comma-separated string values (sanitized via parse_gpu_ids) |
| 85 | + - lists containing ints and/or strings |
| 86 | + """ |
| 87 | + if gpu_ids is None: |
| 88 | + return None |
| 89 | + |
| 90 | + if isinstance(gpu_ids, str): |
| 91 | + return parse_gpu_ids(gpu_ids) |
| 92 | + |
| 93 | + if isinstance(gpu_ids, list): |
| 94 | + normalized_gpu_ids: List[Union[int, str]] = [] |
| 95 | + for gpu_id in gpu_ids: |
| 96 | + if isinstance(gpu_id, int): |
| 97 | + normalized_gpu_ids.append(gpu_id) |
| 98 | + elif isinstance(gpu_id, str): |
| 99 | + normalized_gpu_ids.extend(parse_gpu_ids(gpu_id)) |
| 100 | + else: |
| 101 | + logger.warning( |
| 102 | + "Ignoring invalid gpu_id entry of type %s; expected int or str.", |
| 103 | + type(gpu_id).__name__, |
| 104 | + ) |
| 105 | + return normalized_gpu_ids |
| 106 | + |
| 107 | + logger.warning( |
| 108 | + "Invalid gpu_ids format. Expected a string or a list of ints/strings." |
| 109 | + ) |
| 110 | + return None |
| 111 | + |
| 112 | + |
76 | 113 | def get_hierarchical_config(): |
77 | 114 | """ |
78 | 115 | Get the user-defined codecarbon configuration ConfigParser dictionnary |
|
0 commit comments