在 python 中的 运行 脚本中关闭 GPU
Switch off the GPU in a running script in python
以下代码激活 GPU 0 并加载必要的库,如 Cuda 等
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
当不再需要时,如何在 运行ning 脚本中关闭 GPU?例如,因为训练模型的评估需要 运行 on CPU。
我尝试更改环境变量。
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
但是此代码不会在 运行ning 脚本中关闭 GPU。只能在计算初期使用。
非常感谢任何想法
提前致谢
python 中没有一种选择 GPU 的标准方法,这取决于您在 python 中使用的访问 GPU 的框架。
您描述的 environment variable 用法有些粗糙,并且按照您描述的方式运行。环境变量会影响 CUDA 初始化时的 CUDA 行为,这通常只会发生一次,通常是在 python 脚本的开头。此后,对环境变量的更改将无效。 CUDA 仅在初始化时对其进行一次采样。然而,在其他方面,这种效果应该或多或少与您在 python.
中使用 GPU 的方式无关。
为了更进一步,我们需要讨论一个特定的框架。底层的 CUDA 技术 (CUDA C++) 允许框架开发人员动态控制在特定点可以使用哪些 GPU(从那些通过环境变量“公开”的 GPU 中;你不能覆盖它。)这个动态的方法所公开的控件会因框架而异。由于您的问题被标记为 tensorflow
,您可以找到关于 TF 如何公开 GPU selection/control here.
的一个很好的概述
具体来说,您问的是不使用 GPU。我建议您参考“手动设备放置”section。请注意,这里的“设备”指的是 CPU(s) 和 GPU(s),因此在这种方法中“关闭”GPU 使用的方法是使用 CPU 设备代替,并在此处给出示例:
If you would like a particular operation to run on a device of your choice instead of what's automatically selected for you, you can use with tf.device to create a device context, and all the operations within that context will run on the same designated device.
tf.debugging.set_log_device_placement(True)
# Place tensors on the CPU
with tf.device('/CPU:0'):
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
# Run on the GPU
c = tf.matmul(a, b)
print(c)
Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
tf.Tensor(
[[22. 28.]
[49. 64.]], shape=(2, 2), dtype=float32)
You will see that now a and b are assigned to CPU:0. Since a device was not explicitly specified for the MatMul operation, the TensorFlow runtime will choose one based on the operation and available devices (GPU:0 in this example) and automatically copy tensors between devices if required.
如果您希望 tf.matmul
操作不使用 GPU,只需将其移动到前面的 with
语句的范围内即可。
以下代码激活 GPU 0 并加载必要的库,如 Cuda 等
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
当不再需要时,如何在 运行ning 脚本中关闭 GPU?例如,因为训练模型的评估需要 运行 on CPU。 我尝试更改环境变量。
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
但是此代码不会在 运行ning 脚本中关闭 GPU。只能在计算初期使用。
非常感谢任何想法
提前致谢
python 中没有一种选择 GPU 的标准方法,这取决于您在 python 中使用的访问 GPU 的框架。
您描述的 environment variable 用法有些粗糙,并且按照您描述的方式运行。环境变量会影响 CUDA 初始化时的 CUDA 行为,这通常只会发生一次,通常是在 python 脚本的开头。此后,对环境变量的更改将无效。 CUDA 仅在初始化时对其进行一次采样。然而,在其他方面,这种效果应该或多或少与您在 python.
中使用 GPU 的方式无关。为了更进一步,我们需要讨论一个特定的框架。底层的 CUDA 技术 (CUDA C++) 允许框架开发人员动态控制在特定点可以使用哪些 GPU(从那些通过环境变量“公开”的 GPU 中;你不能覆盖它。)这个动态的方法所公开的控件会因框架而异。由于您的问题被标记为 tensorflow
,您可以找到关于 TF 如何公开 GPU selection/control here.
具体来说,您问的是不使用 GPU。我建议您参考“手动设备放置”section。请注意,这里的“设备”指的是 CPU(s) 和 GPU(s),因此在这种方法中“关闭”GPU 使用的方法是使用 CPU 设备代替,并在此处给出示例:
If you would like a particular operation to run on a device of your choice instead of what's automatically selected for you, you can use with tf.device to create a device context, and all the operations within that context will run on the same designated device.
tf.debugging.set_log_device_placement(True)
# Place tensors on the CPU
with tf.device('/CPU:0'):
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
# Run on the GPU
c = tf.matmul(a, b)
print(c)
Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
tf.Tensor(
[[22. 28.]
[49. 64.]], shape=(2, 2), dtype=float32)
You will see that now a and b are assigned to CPU:0. Since a device was not explicitly specified for the MatMul operation, the TensorFlow runtime will choose one based on the operation and available devices (GPU:0 in this example) and automatically copy tensors between devices if required.
如果您希望 tf.matmul
操作不使用 GPU,只需将其移动到前面的 with
语句的范围内即可。