-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_gpu.py
68 lines (60 loc) · 2.94 KB
/
config_gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
********************************************************************************
configures gpu
********************************************************************************
"""
import tensorflow as tf
from tensorflow.python.client import device_lib
# gpu_flg = 1 # 0, 1, or 2 (see: https://www.tensorflow.org/guide/gpu)
# # 0: Restrict TensorFlow to only use the first GPU
# # 1: Find the first GPU, and restrict the memory usage (adaptive)
# # 2: Create 2 virtual GPUs with 1GB memory each
def config_gpu(gpu_flg):
# gpu configuration
tf.debugging.set_log_device_placement(False) # True to check executing device carefully
gpus = tf.config.experimental.list_physical_devices("GPU")
if gpu_flg == 0:
print("gpu_flg:", gpu_flg)
if gpus:
# Restrict TensorFlow to only use the first GPU
try:
tf.config.experimental.set_visible_devices(gpus[0], "GPU")
logical_gpus = tf.config.experimental.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
except RuntimeError as e:
# Visible devices must be set before GPUs have been initialized
print(e)
elif gpu_flg == 1:
print("gpu_flg:", gpu_flg)
if gpus:
# Find the first GPU, and restrict the memory usage (adaptive)
try:
tf.config.experimental.set_visible_devices(gpus[0], "GPU")
tf.config.experimental.set_memory_growth(gpus[0], True)
logical_gpus = tf.config.experimental.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
print("\nDevice information;")
print(device_lib.list_local_devices())
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
elif gpu_flg == 2:
print("gpu_flg:", gpu_flg)
if gpus:
# Create 2 virtual GPUs with 1GB memory each
try:
tf.config.experimental.set_virtual_device_configuration(
gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit = 2 ** 10),
tf.config.experimental.VirtualDeviceConfiguration(memory_limit = 2 ** 10)])
logical_gpus = tf.config.experimental.list_logical_devices("GPU")
print(len(gpus), "Physical GPU,", len(logical_gpus), "Logical GPUs")
print("\nDevice information;")
print(device_lib.list_local_devices())
except RuntimeError as e:
# Virtual devices must be set before GPUs have been initialized
print(e)
else:
raise NotImplementedError(">>>>> gpu_config")
if __name__ == "__main__":
config_gpu()