4
votes

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).

Here is what my directory structure looks like:

.
├── perception
│   ├── BUILD
│   └── graph_loader.cc
├── third-party
│   └── tensorflow # I cloned tf repo into this folder
└── WORKSPACE

Here is what's inside my perception/BUILD file:

cc_binary(
    name = "graph-loader",
    srcs = [
        "graph_loader.cc",
    ],
    deps = [
        "@tensorflow//tensorflow:libtensorflow.so",
    ]
)

Here is what's inside my WORKSPACE file:

local_repository(
    name = "tensorflow",
    path = "path/to/my/project/third-party/tensorflow",
)

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

Here are the quesitions:

  1. What am I doing wrong so that my build keeps failing?
  2. Is it even possible to do what I'm trying here, i.e. build tensorflow inside of my project?
2

2 Answers

5
votes

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.

In your WORKSPACE file, copy this:

local_repository(
    name = "org_tensorflow",
    path = "third-party/tensorflow",
)

Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE file. Remove the workspace(name = "org_tensorflow") line.

Finally, change all //* in WOKRSPACE to @org_tensorflow//*.

Note that building Tensorflow in sub-folder is not officially supported.

0
votes

John London's answer may be reasonable if you can use a subfolder.

In some cases you will prefer to use an external dependency, and this is still a bit difficult to set up. See the TensorFlow Serving repo's setup for an example of a project that manages to do this. Due to the bazel restrictions, you will need to copy the contents of the WORKSPACE and possibly .bazelrc into your own repo's.

This will suffice for some cases but not all. If you are using rules defined within the @org_tensorflow repo (e.g. such as tfcompile's tf_library()), the dependencies defined by those rules may need to be redefined within your repo's .bzl to have @org_tensorflow// in front of them.