无法使用Tensorflow作为依赖项的Bazel构建C ++项目 [英] Failing to Bazel build C++ project with tensorflow as a dependency
问题描述
我看到很多教程,它们解释了如何在Tensorflow的Bazel WORKSPACE内部构建项目(例如此一个).但是我似乎无法找到一种方法来构建自己的项目并将tensorflow作为依赖项.我查看了这 Bazel文档,显然有一个外部依赖关系进行构建的方法,我尝试遵循自己的方法. (因为tf也是用bazel构建的.)
I see a lot of tutorials that explain how to build projects inside Tensorflow's Bazel WORKSPACE (like this one). But I can't seem to be able to find a way to build my own project and include tensorflow as a dependency. I looked at this Bazel documentation, and there is clearly a way to build with external dependencies, which I tried to follow myself. (since tf is also built with bazel).
这是我的目录结构:
.
├── perception
│ ├── BUILD
│ └── graph_loader.cc
├── third-party
│ └── tensorflow # I cloned tf repo into this folder
└── WORKSPACE
这是我的perception/BUILD
文件中的内容:
Here is what's inside my perception/BUILD
file:
cc_binary(
name = "graph-loader",
srcs = [
"graph_loader.cc",
],
deps = [
"@tensorflow//tensorflow:libtensorflow.so",
]
)
这是我的WORKSPACE
文件中的内容:
Here is what's inside my WORKSPACE
file:
local_repository(
name = "tensorflow",
path = "path/to/my/project/third-party/tensorflow",
)
这是我的perception/graph_loader.cc
文件中的内容:
Here is what's inside my perception/graph_loader.cc
file:
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main() {
using namespace tensorflow;
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();
// Matrix A = [3 2; -1 0]
auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// Vector b = [3 5]
auto b = Const(root, { {3.f, 5.f} });
// v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}
我使用以下命令运行构建:
I run my build with the following command:
build //perception:graph-loader
但是它失败,并显示以下消息:
But it fails with this message:
ERROR: path/to/my/project/perception/BUILD:1:1: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved and referenced by '//perception:graph-loader'
ERROR: Analysis of target '//perception:graph-loader' failed; build aborted: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved
INFO: Elapsed time: 0.037s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
currently loading: @tensorflow//tensorflow
以下是问题:
- 我在做什么错,从而使我的构建不断失败?
- 是否有可能做我在这里尝试的事情,即在我的项目内部构建tensorflow?
推荐答案
由于没有在WORKSPACE
中添加必需的存储库规则,因此出现此错误. Bazel当前没有递归工作区,因此您需要手动将所有依赖项的存储库复制到主WORKSPACE
中.
You are getting this error because you have not added the required repository rules in your WORKSPACE
. Bazel currently does not have recursive workspace, so you need to manually copy those repositories of all your dependencies into the main WORKSPACE
.
在您的WORKSPACE
文件中,复制以下内容:
In your WORKSPACE
file, copy this:
local_repository(
name = "org_tensorflow",
path = "third-party/tensorflow",
)
附加 https://github.com/tensorflow/tensorflow/blob/的所有内容master/WORKSPACE 放入WORKSPACE
文件.删除workspace(name = "org_tensorflow")
行.
Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE
file. Remove the workspace(name = "org_tensorflow")
line.
最后,将WOKRSPACE
中的所有//*
更改为@org_tensorflow//*
.
Finally, change all //*
in WOKRSPACE
to @org_tensorflow//*
.
请注意,尚未正式支持在子文件夹中构建Tensorflow.
Note that building Tensorflow in sub-folder is not officially supported.
这篇关于无法使用Tensorflow作为依赖项的Bazel构建C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!