首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >多gpu模式下的tensorflow c++ SetDefaultDevice

多gpu模式下的tensorflow c++ SetDefaultDevice
EN

Stack Overflow用户
提问于 2018-09-06 15:23:15
回答 1查看 1.2K关注 0票数 2

我想在多个GPU上加载相同的图形以进行推理,但是我无法使用graph::SetDefaultDevice将图形与设备关联。这个问题不是在SetDefaultDevice中出现的,而是在以后使用图形创建会话时出现的。下面是一个从tensorflow的example_trainer.cc中提取的简单示例

代码语言:javascript
运行
AI代码解释
复制
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/graph/default_device.h"

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();
  auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} }); 
  auto b = Const(root, { {3.f, 5.f} }); 
  auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));

  GraphDef def;
  TF_CHECK_OK(root.ToGraphDef(&def));

  graph::SetDefaultDevice(false ? "/device:GPU:0" : "/cpu:0", &def);
  /*
  for (auto &node: *def.mutable_node()) {
        node.set_device("/cpu:0");
        std::cout << node.name() << " = '" << node.device() <<"'"<< std::endl;
  }
  std::cout << "=======================\n";
  */
  SessionOptions options;
  std::unique_ptr<Session> session(NewSession(options));
  TF_CHECK_OK(session->Create(def));
  return 0;
}

运行时出现以下错误

代码语言:javascript
运行
AI代码解释
复制
2018-09-06 18:18:13.853316: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-09-06 18:18:13.856079: F /home/daniel/tensorflow_cc/example/example.cpp:27] Non-OK-status: session->Create(def) status: Not found: No attr named '/cpu:0' in NodeDef:
     [[Node: Const = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [2,2] values: [3 2][-1]...>, _device="/cpu:0"]()]]
     [[Node: Const = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [2,2] values: [3 2][-1]...>, _device="/cpu:0"]()]]
Aborted (core dumped)

如果我删除SetDefault设备调用,它可以完美地工作。我也曾尝试过在装有GPU的机器上做这件事,但没有成功。

我知道问题不在于SetDefaultDevice,因为手动设置每个节点的设备在创建会话时都会遇到相同的问题。

代码语言:javascript
运行
AI代码解释
复制
Const = '/cpu:0'
Const_1 = '/cpu:0'
v = '/cpu:0'
=======================
2018-09-06 18:15:05.966337: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-09-06 18:15:05.969048: F /home/daniel/tensorflow_cc/example/example.cpp:26] Non-OK-status: session->Create(def) status: Not found: No attr named '/cpu:0' in NodeDef:
     [[Node: Const = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [2,2] values: [3 2][-1]...>, _device="/cpu:0"]()]]
     [[Node: Const = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [2,2] values: [3 2][-1]...>, _device="/cpu:0"]()]]
Aborted (core dumped)
EN

回答 1

Stack Overflow用户

发布于 2018-09-08 07:12:29

这似乎只是单片构建的问题(-- libtensorflow_cc.so =单片),即在构建配置时。我不确定,但这可能与

https://github.com/tensorflow/tensorflow/issues/5379 https://github.com/tensorflow/tensorflow/issues/16291

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52207439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档