在 C++ 版本的 Tensorflow 上使用多个 GPU
Use multiple gpus on the C++ version of tensorflow
首先解释一下我的操作环境:
win10x64
cuda9.1 and cudnn7
gtx1080Ti x2
i7-6850k
我使用 c++ 版本的 tensorflow 编写了一个程序,该程序读取 pb 文件,然后输入图像进行预测。我的目标是在一个线程中使用张量流或一个线程一个 GPU 时可以调用所有 GPU。
首先使用 windows 下的 python 调用 TensorFlow slim 训练,然后使用 freeze_graph.py 将保存的模型文件转换为冻结文件。
但是,我发现在使用session->Run((函数时只调用了一个gpu。无论是创建多个线程还是一个线程,我都使用以下方法来调用多个 gpu:
tensorflow::graph::SetDefaultDevice("0", &graphdef);
或
GraphDef graphdef; //Graph Definition for current model
Status status_load = ReadBinaryProto(Env::Default(), model_path, &graphdef); //read graph from pb_file
if (!status_load.ok()) {
std::cout << " ERROR: Loading model failed...n"
<< model_path
<< std::endl;
std::cout << status_load.ToString() << "n";
system("pause");
return;
}
tensorflow::SessionOptions options;
tensorflow::ConfigProto &config = options.config;
config.set_log_device_placement(true);
config.mutable_gpu_options()->set_allow_growth(true);
//config.mutable_gpu_options()->set_allocator_type(std::string("BFC"));
//config.mutable_gpu_options()->set_visible_device_list("");//this no error,but still can only call one gpu
//config.mutable_gpu_options()->set_visible_device_list("0");//error!
config.mutable_gpu_options()->set_visible_device_list("0,1");//error!
config.mutable_gpu_options()->set_per_process_gpu_memory_fraction(1);
Session* session;
Status status = NewSession(SessionOptions(options), &session);
Status status_create = session->Create(graphdef);
以上两种方法都失败了,错误提示相同:
2018-08-08 09:25:55.953495: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcoreplatformcpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2018-08-08 09:25:56.541237: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:1404] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:06:00.0
totalMemory: 11.00GiB freeMemory: 9.02GiB
2018-08-08 09:25:56.708385: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:1404] Found device 1 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:0b:00.0
totalMemory: 11.00GiB freeMemory: 9.02GiB
2018-08-08 09:25:56.731390: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:1483] Adding visible gpu devices: 0, 1
2018-08-08 09:26:04.117910: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:964] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-08-08 09:26:04.131670: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:970] 0 1
2018-08-08 09:26:04.142367: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:983] 0: N N
2018-08-08 09:26:04.152745: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_device.cc:983] 1: N N
2018-08-08 09:26:04.173833: E D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimegpugpu_process_state.cc:105] Invalid allocator type: 0,1
2018-08-08 09:26:04.189278: E D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimedirect_session.cc:158] Internal: Failed to get memory allocator for TF GPU 0 with 11811160064 bytes of memory.
ERROR: Creating Session failed...
Internal: Failed to create session.
Press any key to continue......
根据提示,我切换到"/gpu/:0"和"/device:GPU:0"作为 GPU 的 ID。但提示解决失败,如下所示:
2018-08-08 09:31:07.052736: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcoreplatformcpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2018-08-08 09:31:07.643228: E D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimedirect_session.cc:158] Invalid argument: Could not parse entry in 'visible_device_list': '/device:GPU:0'. visible_device_list = /device:GPU:0
ERROR: Creating Session failed...
Internal: Failed to create session.
或
2018-08-08 09:32:28.753232: I D:MyProjecttensorflow-1.10.0-rc1tensorflowcoreplatformcpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2018-08-08 09:32:29.082282: E D:MyProjecttensorflow-1.10.0-rc1tensorflowcorecommon_runtimedirect_session.cc:158] Invalid argument: Could not parse entry in 'visible_device_list': '/gpu:0'. visible_device_list = /gpu:0
ERROR: Creating Session failed...
Internal: Failed to create session.
然后我在/github/tensorflow 的问题中发现了同样的错误。我根据他们的方法尝试了以下方法:
遵循这些计划 #5379
1.{tf_root}\tensorflow\tf_version_script.lds
修改此文件,添加">protobuf;">
失败!
2. 添加相应的库。
tf_core_gpu_kernelss.lib
training_ops_gen_cc.lib
transform_graph.lib
tf_protos_cc.lib
user_ops_gen_cc.lib
失败!
但是如果我使用以下方法:
config.mutable_gpu_options()->set_visible_device_list("")
或
tensorflow::graph::SetDefaultDevice("", &graphdef)
这可以通过并运行,但仍然只调用一个 gpu!
我在这个问题#18861中发现了同样的错误,但我在下面没有找到C++的解决方案,所以我怀疑这是我的张量流问题,我重新编译了 1.9.0 和最新的 1.10.0-rc1。但得到同样的错误
有人可以帮我解决这个问题吗? └(^o^)┘
我真的很感激!
谢谢你重播我!
我可能已经找到了解决方案,但今天的测试没有达到我的要求。
tensorflow::SessionOptions options;
tensorflow::ConfigProto &config = options.config;
auto* device_count = config.mutable_device_count();
/*device_count->insert({ "CPU", 1 });*/
//device_count->insert({ "GPU", 1 });//1 represents one gpu, not the "/gpu:0"
device_count->insert({ "GPU", 2 });//2 represents two gpu, it is "/gpu:0" and "/gpu:1"
Session* session;
Status status = NewSession(options, &session);//creat new Session
std::vector<DeviceAttributes> response;
session->ListDevices(&response);
//print the device list
for (int temIndex = 0; temIndex < response.size(); ++temIndex) {
auto temValue= response[temIndex];
std::cout << "ListDevices(): " << temIndex << " " << response[temIndex].name() << std::endl;
}
使用此方法与以下方法相同:
options.config.mutable_gpu_options()->set_visible_device_list("");
仍然无法明确定义要使用的GPU,并且仍然将所有计算都放在一个GPU上,我认为这可能是我的方法仍然存在问题。
但我觉得我会找到一个解决方案......
使用CUDA_VISIBLE_DEVICES为不同的进程设置特定的设备,这就是我的解决方案
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在 DirectX 11 中从 GPU 读回顶点缓冲区(并获取顶点)
- 跨平台 GPU 计算
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 请求最简单的 OpenMP 目标 GPU 示例
- DirectX 11 如何处理来自 GPU 上的 sharedHandle 的图像
- 编译 GPU 的张量流示例自定义操作
- 多 GPU 批处理 1D FFT:似乎只有一个 GPU 可以工作
- 如何在GPU支持下编译tflite?
- OpenCL 在 NVIDIA 和 Intel GPU 上启动内核时CL_INVALID_COMMAND_QUEUE
- OpenGL glGetUniformBlockIndex 在 nvidea GPU 上返回INVALID_INDEX
- SDL GPU 为什么将两个图像分成两个单独的循环更快?
- 使 C++ Pi 近似在 GPU Nvidia 970M CUDA 上的 Paralell 中运行
- 具有 GPU 时间表的卤化物产生黑色图像
- TensorFlow c++ SetDefaultDevice 在多 GPU 模式下
- 在C++应用程序中使用 Tensorflow:如何释放 GPU 内存
- 利用 GPU 的最佳方式
- Cuda 中的多个 GPU - 以前可以使用代码,但现在不再使用
- 如何使用 CUDA 将 std::vector<std::string> 复制到 GPU 设备