【问题标题】:When using bazel to build TensorFlow C++ call model, .so file doesn't work使用 bazel 构建 TensorFlow C++ 调用模型时,.so 文件不起作用
【发布时间】:2018-12-26 07:52:56
【问题描述】:

我想使用 TensorFlow C++ api 来调用模型并预测答案。 首先,我克隆了 tensorflow repo

git clone --recursive https://github.com/tensorflow/tensorflow

然后我编写如下 C++ 代码:

一个代码是一个调用TensorFlow api的类,头文件是这样的:

#ifndef _DEEPMODEL_H_
#define _DEEPMODEL_H_

#include <iostream>
#include <string>
#include <vector>
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

using namespace std;
using namespace tensorflow;

class DeepModel{
public:
    DeepModel(const string graph_path, const string checkpoint_path);
    virtual ~DeepModel();

    bool onInit();
    void unInit();
    vector<float> predict(vector<vector<float>>& x, string input_name, string output_name);
private:
    string graph_path;
    string checkpoint_path;
    MetaGraphDef graph_def;
    Session* my_sess;
};

#endif

在这之后,我写了一个简单的封装代码。我想编译一个.so,将来使用没有tensorflow源代码的.so。我的封装代码如下:

#ifndef _MODEL_HELPER_H_
#define _MODEL_HELPER_H_

#include <vector>
#include <string>

using namespace std;

class ModelHelper{
public:
    ModelHelper(const string graph_path, const string checkpoint_path);
    virtual ~ModelHelper();

    vector<float> predict(vector<vector<float> >& x, string input_name, string output_name);
private:
    string graph_path;
    string checkpoint_path;
};

#endif

我已经编写了代码来测试上面的代码,它运行良好。然后我想用bazel编译.so。

我的 BUILD 文件如下:

load("//tensorflow:tensorflow.bzl", "tf_cc_binary")

tf_cc_binary(
    name = "my_helper.so",
    srcs = ["model_helper.cc", "model_helper.h", "deepmodel.cc", "deepmodel.h"],
    linkshared = 1,
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/core:tensorflow"
        ],
)

然后我将model_helper.so重命名为libmodel_helper.so,并编写cpp代码来测试.so文件。而我要编译代码,命令是这样的

g++ -std=c++11 test_so.cpp -L./ -lmy_helper -I./ -o my_helper

然后我遇到错误:

.//libmy_helper.so: undefined reference to `stream_executor::cuda::ScopedActivateExecutorContext::~ScopedActivateExecutorContext()'
.//libmy_helper.so: undefined reference to `stream_executor::cuda::ScopedActivateExecutorContext::ScopedActivateExecutorContext(stream_executor::StreamExecutor*)'
.//libmy_helper.so: undefined reference to `tensorflow::DeviceName<Eigen::GpuDevice>::value[abi:cxx11]'
collect2: error: ld returned 1 exit status

我真的不知道为什么。我不能单独使用 .so 吗?

【问题讨论】:

  • 在 tensorflow repo 中运行 ./configure 时,我没有设置 cuda 选项。然后编译就ok了。它运作良好。有什么问题...

标签: c++ tensorflow bazel


【解决方案1】:

您应该在 makefile 中引用 libtensorflow_frameowork.so。就像下面的代码:

g++ -std=c++11 test_so.cpp -L./ -lmy_helper -ltensorflow_framework -I./ -o my_helper

我猜 bazel 在编译我的代码时错过了 tensorflow 中的一些源代码到 .so 中。

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多