|
18 | 18 |
|
19 | 19 |
|
20 | 20 | def setup_gpu(gpu_id): |
21 | | - if gpu_id == 'cpu' or gpu_id == -1: |
| 21 | + try: |
| 22 | + visible_gpu_indices = [int(id) for id in gpu_id.split(',')] |
| 23 | + available_gpus = tf.config.list_physical_devices('GPU') |
| 24 | + visible_gpus = [gpu for idx, gpu in enumerate(available_gpus) if idx in visible_gpu_indices] |
| 25 | + |
| 26 | + if visible_gpus: |
| 27 | + try: |
| 28 | + # Currently, memory growth needs to be the same across GPUs. |
| 29 | + for gpu in available_gpus: |
| 30 | + tf.config.experimental.set_memory_growth(gpu, True) |
| 31 | + |
| 32 | + # Use only the selcted gpu. |
| 33 | + tf.config.set_visible_devices(visible_gpus, 'GPU') |
| 34 | + except RuntimeError as e: |
| 35 | + # Visible devices must be set before GPUs have been initialized. |
| 36 | + print(e) |
| 37 | + |
| 38 | + logical_gpus = tf.config.list_logical_devices('GPU') |
| 39 | + print(len(available_gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") |
| 40 | + else: |
| 41 | + tf.config.set_visible_devices([], 'GPU') |
| 42 | + except ValueError: |
22 | 43 | tf.config.set_visible_devices([], 'GPU') |
23 | | - return |
24 | | - |
25 | | - gpus = tf.config.list_physical_devices('GPU') |
26 | | - if gpus: |
27 | | - # Restrict TensorFlow to only use the first GPU. |
28 | | - try: |
29 | | - # Currently, memory growth needs to be the same across GPUs. |
30 | | - for gpu in gpus: |
31 | | - tf.config.experimental.set_memory_growth(gpu, True) |
32 | | - |
33 | | - # Use only the selcted gpu. |
34 | | - tf.config.set_visible_devices(gpus[int(gpu_id)], 'GPU') |
35 | | - except RuntimeError as e: |
36 | | - # Visible devices must be set before GPUs have been initialized. |
37 | | - print(e) |
38 | | - |
39 | | - logical_gpus = tf.config.list_logical_devices('GPU') |
40 | | - print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") |
|
0 commit comments