我正在使用Tensorflow C API来运行在python中保存/冻结的模型。我们过去在CPU上运行这些模型,但最近切换到GPU以提高性能。为了与C交互,我们使用了一个名为CPPFlow (https://github.com/serizba/cppflow)的包装库。我最近更新了这个库,这样我们就可以传入GPU配置选项,这样我们就可以控制GPU内存分配。然而,我们现在的系统也有多个GPU,这导致了一些问题。看起来我不能让Tensorflow和我们的软件使用相同的GPU。
我使用的visible_device_list参数与我们的软件具有相同的GPU。如果我将软件设置为在设备1上运行,将Tensorflow设置为在设备1上运行,则Tensorflow将选择设备2。如果将软件设置为使用设备1,将Tensorflow设置为使用设备2,则这两个软件将使用相同的GPU。
Tensorflow如何排序GPU设备,是否需要使用其他方法手动选择设备?我看到的每一个地方都表明它可以使用GPU配置选项来完成。
发布于 2020-08-26 23:54:41
设置设备的一种方法是获取python中的十六进制字符串,然后使用C API中的字符串:例如,示例1:
gpu_options = tf.GPUOptions(allow_growth=True,visible_device_list='1')
config = tf.ConfigProto(gpu_options=gpu_options)
serialized = config.SerializeToString()
print(list(map(hex, serialized)))示例2:
import tensorflow as tf
config = tf.compat.v1.ConfigProto(device_count={"CPU":1}, inter_op_parallelism_threads=1,intra_op_parallelism_threads=1)
ser = config.SerializeToString()
list(map(hex,ser))
Out[]:
['0xa',
'0x7',
'0xa',
'0x3',
'0x43',
'0x50',
'0x55',
'0x10',
'0x1',
'0x10',
'0x1',
'0x28',
'0x1']在C API中使用此字符串作为
uint8_t config[13] = {0xa, 0x7, 0xa, ... , 0x28, 0x1};
TF_SetConfig(opts, (void*)config, 13, status);有关更多详细信息,请参阅:
https://github.com/tensorflow/tensorflow/issues/29217
https://github.com/cyberfire/tensorflow-mtcnn/issues/1
https://github.com/tensorflow/tensorflow/issues/27114发布于 2021-09-06 14:30:45
您可以通过在执行过程中设置环境变量CUDA_VISIBLE_DEVICES来设置Tensorflow GPU顺序。有关更多详细信息,请查看here
//Set TF to use GPU:1 and GPU:0 (in this order)
setenv( "CUDA_VISIBLE_DEVICES", "1,0", 1 );
//Set TF to use only GPU:0 (in this order)
setenv( "CUDA_VISIBLE_DEVICES", "0", 1 );
//Set TF to do not use GPUs
setenv( "CUDA_VISIBLE_DEVICES", "-1", 1 );https://stackoverflow.com/questions/62393258
复制相似问题