Import TensorFlow as Bazel dependency for C++ project

101 views
Skip to first unread message

Lucien Perouze

unread,
May 8, 2024, 4:33:55 PMMay 8
to bazel-discuss
I've asked the question on Stack Overflow but got no response since.

I'm starting a new C++ project that requires TensorFlow. I would like to configure a way to import TensorFlow from github without having to build it manually for each environment.

I'm using Bazel to build my project and manage dependencies because TensorFlow is using it and it seems less of a struggle to import it with Bazel. On top of that I'm using bzlmod (the new way of importing modules).

I can't find any good documentation or tutorial to import it properly. I'm quite new to C++ builds practices (coming from java / node world). I've tried TensorFlow Serving way of importing TensorFlow as mentioned in some posts. But nothing works for me.

As for now I have this error when trying to sync Bazel on CLion :


error loading package '@@org_tensorflow~//tensorflow/core': Unable to find package for @@[unknown repo 'local_config_cuda' requested from @@org_tensorflow~]//cuda:build_defs.bzl: The repository '@@[unknown repo 'local_config_cuda' requested from @@org_tensorflow~]' could not be resolved: No repository visible as '@local_config_cuda' from repository '@@org_tensorflow~'. and referenced by '//src/main:my_project_main'

I'm on a MacBook, that might explain why it misses some cuda stuff. How can I toggle on and off cuda functions based on the OS I'm running on ? I might need it later on production but disabled on my mac.

Here is my current configuration :

registry/
  modules/
    org_tensorflow/
      2.15.1/
        MODULE.bazel
        source.json
      metadata.json
    bazel_registry.json
src/
  main/
    BUILD.bazel
    main.cc
.bazelrc
MODULE.bazel
WORKSPACE.bazel


I put aside the registry part that just specify the url to get the archive from as it seems to be working properly. The archive is well retrieve from github and I seem to have correct access to its files.

My .bazelrc looks like this :

# Enable Bzlmod for every Bazel command
common --enable_bzlmod --registry=https://bcr.bazel.build --registry=file://%workspace%/registry --experimental_repo_remote_exec


My MODULE.bazel looks like this

module(
    name = "my_project",
    repo_name = "my_project",
)

bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_java", version = "7.5.0")
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "org_tensorflow", version = "2.15.1")


My WORKSPACE.bazel look like this :

load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")

py_repositories()

python_register_toolchains(
    name = "python",
    ignore_root_user_error = True,
    python_version = "3.9",
)

load("@python//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "package_annotation", "pip_parse")

NUMPY_ANNOTATIONS = {
    "numpy": package_annotation(
        additive_build_content = """\
filegroup(
    name = "includes",
    srcs = glob(["site-packages/numpy/core/include/**/*.h"]),
)
cc_library(
    name = "numpy_headers",
    hdrs = [":includes"],
    strip_include_prefix="site-packages/numpy/core/include/",
)
""",
    ),
}

pip_parse(
    name = "pypi",
    annotations = NUMPY_ANNOTATIONS,
    python_interpreter_target = interpreter,
    requirements_lock = "@org_tensorflow//:requirements_lock_3_9.txt",
)

load("@pypi//:requirements.bzl", "install_deps")

install_deps()

load("@org_tensorflow//tensorflow:workspace3.bzl", "tf_workspace3")

tf_workspace3()

load("@org_tensorflow//tensorflow:workspace2.bzl", "tf_workspace2")

tf_workspace2()

load("@org_tensorflow//tensorflow:workspace1.bzl", "tf_workspace1")

tf_workspace1()

load("@org_tensorflow//tensorflow:workspace0.bzl", "tf_workspace0")

tf_workspace0()


I tried to copied the way TensorFlow Serving include and configure TensorFlow and the way TensorFlow's WORKSPACE is setup. But I have no real idea of what I'm doing.

Then I'm importing the code in my project with my src/main/BUILD.bazel like that :

cc_binary(
    name = "my_project_main",
    srcs = [
        "main.cc",
    ],
    deps = [
        "@org_tensorflow//tensorflow/core:tensorflow",
    ],
)


By the way I don't know which target to specify as dependency. TensorFlow Serving uses @org_tensorflow//tensorflow/core:lib. Do you know which target is suited for my use case ? I will need to use basic TensorFlow classes in my C++ code such as tf.Tensor(), tf.float(), etc...


If you have any advice, solutions, thank you !

Lucien Perouze

unread,
May 9, 2024, 4:57:00 AMMay 9
to bazel-discuss
I've created a public GitHub repo so you can see and test by yourself : https://github.com/lucien-perouze/tensorflow_bazel

I've slightly changed some files since my post to test new things but still get the same error : ERROR: error loading package '@@org_tensorflow~//tensorflow/core': Unable to find package for @@[unknown repo 'local_config_cuda' requested from @@org_tensorflow~]//cuda:build_defs.bzl: The repository '@@[unknown repo 'local_config_cuda' requested from @@org_tensorflow~]' could not be resolved: No repository visible as '@local_config_cuda' from repository '@@org_tensorflow~'.

I've tried to run the Tensorflow python configuration file in an module extension but it seems that the extension is not called at all since my print() isn't showed in logs. What am I doing wrong ?

Thank you for your help !

Xudong Yang

unread,
May 9, 2024, 6:17:10 PMMay 9
to bazel-discuss
The (cruel?) answer is that TensorFlow doesn't support Bzlmod yet. The simple MODULE.bazel file you created for it does not include all the dependencies it needs (the error message is complaining about `local_config_cuda`, for example).

Your best bet now is probably just to use WORKSPACE to bring in TensorFlow.

Keith Smiley

unread,
May 9, 2024, 7:41:44 PMMay 9
to Xudong Yang, bazel-discuss
I think many folks who bring in TF attempt to bring it in as binary artifacts as opposed to trying to host their whole build in your bazel build.
--
Keith Smiley


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/5bc85f95-8e41-4d41-8fe0-25aa71606b12n%40googlegroups.com.

Lucien Perouze

unread,
May 10, 2024, 4:39:06 AMMay 10
to bazel-discuss
I might end up doing that. But I wanted to try with the archive to keep TensorFlow's adaptability on each OS and environment.
My goal is to have the same kind of adaptability for my binary so anyone with any kind of machine could seamlessly build and run the whole project.

And I would like to be kept up to date with TensorFlow's latests releases without having to rebuild the binary for x OSs times n configs each time.

Lucien Perouze

unread,
May 10, 2024, 10:11:58 AMMay 10
to bazel-discuss
Just for my understanding,

When I'm using a local repository and specifying TensorFlow archive as a source like I did.
Isn't there a phase where I can manipulate the repo / run bzl scripts before the BUILD phase ?
The MODULE.bazel file that I created for the version of Tensorflow doesn't act as a MODULE.bazel file that would be present in the archive ?

The migration documentation says that I can use module extension or repo rule to import dependencies to my module : https://bazel.build/external/migration#fetch-deps-module-extensions
Can't I just call TensorFlow's workspace[0-3].bzl files in a module extension to load all dependencies ?

There is really no way to use legacy dependencies in Bzlmod ?
How can it motivate developper to switch to Bzlmod ? It will discourage any project to adopt it and it's a vicious circle.
It should allow some kind of backward compatibility.

Lucien Perouze

unread,
May 15, 2024, 10:47:52 AMMay 15
to Filip Filmar, bazel-discuss
I've created a public GitHub repo so you can see and test by yourself : https://github.com/lucien-perouze/tensorflow_bazel

I've slightly changed some files since my post to test new things but still get the same error : ERROR: error loading package '@@org_tensorflow~//tensorflow/core': Unable to find package for @@[unknown repo 'local_config_cuda' requested from @@org_tensorflow~]//cuda:build_defs.bzl: The repository '@@[unknown repo 'local_config_cuda' requested from @@org_tensorflow~]' could not be resolved: No repository visible as '@local_config_cuda' from repository '@@org_tensorflow~’.

I've tried to run the Tensorflow python configuration file in an module extension but it seems that the extension is not called at all since my print() isn't showed in logs. What am I doing wrong ?

Thank you for your help !

> Le 9 mai 2024 à 00:09, Filip Filmar <fil...@gmail.com> a écrit :
>
> Is there a way to share your entire repo for someone to take a closer look at?
> --
> You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/3a14ed37-521a-4099-b867-e46a379a93f5n%40googlegroups.com.

Filip Filmar

unread,
May 15, 2024, 10:47:52 AMMay 15
to Lucien Perouze, bazel-discuss
Is there a way to share your entire repo for someone to take a closer look at?

On Wed, May 8, 2024 at 1:33 PM Lucien Perouze <lucien...@gmail.com> wrote:
--

David Turner

unread,
May 15, 2024, 10:47:52 AMMay 15
to Lucien Perouze, bazel-discuss
Tensorflow simply doesn't support BzlMod, see https://github.com/tensorflow/tensorflow/issues/62598

The error you are seeing is because the TensorFlow BUILD files reference external repositories such as @local_config_cuda, which are populated by running custom repository rules, invoked from the WORKSPACE file.

To support BzlMod, the TensorFlow MODULE.bazel and associated .bzl files would need to properly declare and define module extensions for each one of these custom repositories.
This is a major undertaking, which is why the corresponding GitHub issue is still open.

Try _not_ enabling BzlMod, your WORKSPACE seems to have the right directive (though I haven't tested it), but is ignored when you enable BzlMod.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

Lucien Perouze

unread,
May 15, 2024, 10:47:52 AMMay 15
to David Turner, bazel-discuss
Thank you very much for your answer !

I would try a bit further to make it work with Bzlmod since I will need it for further dependencies.

I’m actually trying to trigger workspaces0.bzl, workspaces1.bzl, workspaces2.bzl and workspaces3.bzl inside a module extension to keep legacy code as explained in the migration guide to Bzlmod.

You can see that in my root configure.bzl called in my MODULE.bazel.

But this extension doesn’t seemed to be called at build as I tried to add a print() that is never printed in console.

Am I declaring or calling my module extension wrong ?

Xudong Yang

unread,
May 15, 2024, 10:48:09 AMMay 15
to Lucien Perouze, bazel-discuss
The (cruel?) answer is that TensorFlow doesn't support Bzlmod yet. The simple MODULE.bazel file you created for it does not include all the dependencies it needs (the error message is complaining about `local_config_cuda`, for example).

Your best bet now is probably just to use WORKSPACE to bring in TensorFlow.

"  YÁNG Xùdōng (杨旭东)
#  SURNAME Givenname
=  Software Engineer
@  Google New York


--
Reply all
Reply to author
Forward
0 new messages