繁体   English   中英

Tensorflow C API 选择 GPU

[英]Tensorflow C API Selecting GPU

我正在使用 Tensorflow C API 运行在 Z23EEEB4347BDD7556BFC6B7EE9A 中保存/冻结的模型。 我们曾经在 CPU 上运行这些模型,但最近切换到 GPU 以提高性能。 为了与 C API 进行交互,我们使用了一个名为 CPPFlow 的包装库( https://github.com/serizba/cppflow )。 我最近更新了这个库,以便我们可以传入 GPU 配置选项,以便我们可以控制 GPU memory 分配。 但是,我们现在也有具有多个 GPU 的系统,这会导致一些问题。 似乎我无法让 Tensorflow 使用与我们的软件相同的 GPU。

我使用与我们的软件具有相同 GPU ID 的 visible_device_list 参数。 If I set our software to run on device 1 and Tensorflow to device 1, Tensorflow will pick device 2. If I set our software to use device 1 and Tensorflow to use device 2, both software use the same GPU.

Tensorflow 如何订购 GPU 设备,我是否需要使用其他方法手动 select 设备? 我看到的每个地方都表明可以使用 GPU 配置选项来完成。

设置设备的一种方法是获取 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

您可以在执行期间通过设置环境变量CUDA_VISIBLE_DEVICES来设置 Tensorflow GPU 顺序。 有关更多详细信息,您可以在此处查看

//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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM