Import TensorFlow as Bazel dependency for C++ project

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 trying to use it.

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'

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 !

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật