简体   繁体   English

Tensorflow C API 选择 GPU

[英]Tensorflow C API Selecting GPU

I am using the Tensorflow C API to run models saved/frozen in python.我正在使用 Tensorflow C API 运行在 Z23EEEB4347BDD7556BFC6B7EE9A 中保存/冻结的模型。 We use to run these models on CPU but recently switched to GPU for performance.我们曾经在 CPU 上运行这些模型,但最近切换到 GPU 以提高性能。 To interact with the C API we use a wrapper library called CPPFlow ( https://github.com/serizba/cppflow ).为了与 C API 进行交互,我们使用了一个名为 CPPFlow 的包装库( https://github.com/serizba/cppflow )。 I recently updated this library so that we can pass in GPU Config options so that we can control GPU memory allocations.我最近更新了这个库,以便我们可以传入 GPU 配置选项,以便我们可以控制 GPU memory 分配。 However we also now have systems with multiple GPUs which is causing some issues.但是,我们现在也有具有多个 GPU 的系统,这会导致一些问题。 It seems like I cant get Tensorflow to use the same GPU as our software does.似乎我无法让 Tensorflow 使用与我们的软件相同的 GPU。

I use the visible_device_list parameter with the same GPU ID as our software.我使用与我们的软件具有相同 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. 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.

How does Tensorflow order GPU devices and do I need to use another method to manually select the device? Tensorflow 如何订购 GPU 设备,我是否需要使用其他方法手动 select 设备? Every where I look suggests it can be done using the GPU Config options.我看到的每个地方都表明可以使用 GPU 配置选项来完成。

One way to set the device is getting the hex string in python and then using the string in C API: For example, Sample 1:设置设备的一种方法是获取 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)))

Sample 2:样本 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']

Use this string in C API as在 C API 中使用此字符串作为

uint8_t config[13] = {0xa, 0x7, 0xa, ... , 0x28, 0x1};
TF_SetConfig(opts, (void*)config, 13, status);

For more details:更多细节:

https://github.com/tensorflow/tensorflow/issues/29217
https://github.com/cyberfire/tensorflow-mtcnn/issues/1
https://github.com/tensorflow/tensorflow/issues/27114

You can set Tensorflow GPU order by setting the environment variable CUDA_VISIBLE_DEVICES during execution.您可以在执行期间通过设置环境变量CUDA_VISIBLE_DEVICES来设置 Tensorflow GPU 顺序。 For more details, you can check it 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 );

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

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