Windows : Help - Unable to build tensorflow_cc in c++ from source in windows

SUMMARY

I want to add tensor flow to a c++ app in Visual Studio, to read and write TFRecords and ideally execute models trained in Python. I’m, not interested in GPU (I know there is no support for it). I only need to be able to deploy a c++ app in windows with some tensorflow usability.

I need, a .dll, and the corresponding .lib and .h files to link in my software.

First I figured out, I need to compile tensorflow_cpp to do this. But for the life of me, I am unable to do this. This is what I tried, and what I obtained:

VERSIONS TESTED:

tensorflow = 2.16.1 , 2.15.1, 2.15.0
bazel = 7.1.1 , 6.5.0 , 6.4.0 , 6.1.0
python = 3.12.3 , 3.11.9
msys2 = 20240113

compiler = MSVC 2019 (Visual Studio 2022 version 17.9.0, MSVC toolset version 14.39.33519)

STEPS:

  1. Install Python and add to path
pip3 install -U pip
pip3 install -U six numpy wheel packaging
pip3 install -U keras_applications --no-deps
pip3 install -U keras_preprocessing --no-deps
  1. Install Bazel and add to path
  2. Install MSYS2 and add to path
  3. Download and uncompress Tensorflow
  4. Some Configuration
pacman -Syu
pacman -S git patch unzip
pacman -S git patch unzip rsync

python .\configure.py

  • No ROCm support
  • Default optimization flags (/arch:AVX)
  • Not override eigen strong inline (tried both)
  • Not configure Android builds

Preconfigured Bazel build configs. You can use any of the below by adding “–config=<>” to your build command. See .bazelrc for more details.
–config=mkl # Build with MKL support.
–config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
–config=monolithic # Config for mostly static monolithic build.
–config=numa # Build with NUMA support.
–config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
–config=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
–config=nogcp # Disable GCP support.
–config=nonccl # Disable NVIDIA NCCL support.

RESULTS: ABLE TO BUILD

bazel build -c opt --define=no_tensorflow_py_deps=true tensorflow/lite:tensorflowlite
bazel build -c opt --define=no_tensorflow_py_deps=true tensorflow.dll

RESULTS: UNABLE TO BUILD

bazel build -c opt --define=no_tensorflow_py_deps=true tensorflow:tensorflow_cc.dll
bazel build -c opt --define=no_tensorflow_py_deps=true tensorflow:libtensorflow_cc.so

ERRORS

Im just adding the meaningful part in the wall of text.

ERROR: C:/tensorflow-2.16.1/tensorflow/compiler/mlir/quantization/tensorflow/calibrator/BUILD:97:11: Compiling tensorflow/compiler/mlir/quantization/tensorflow/calibrator/calibration_statistics_collector_average_min_max.cc failed: (Exit 2): cl.exe failed: error executing command (from target //tensorflow/compiler/mlir/quantization/tensorflow/calibrator:calibration_statistics_collector_average_min_max)
[…]
cl : command line error D8022 : cannot open ‘bazel-out/x64_windows-opt/bin/tensorflow/compiler/mlir/quantization/tensorflow/calibrator/_objs/calibration_statistics_collector_average_min_max/calibration_statistics_collector_average_min_max.obj.params’
Target //tensorflow:tensorflow_cc.dll failed to build

Some notes:

  • I turned Developer mode on (to allow bazel to create symlinks).
  • Some bazel versions did not work with MSVC 2019, but otherwise I obtain the same results despite the versions of python, bazel and tensorflow.
  • This happens around the 15000/18000 tasks (So it takes around an hour to get here).
  • The file C:/tensorflow-2.16.1/tensorflow/compiler/mlir/quantization/tensorflow/calibrator/calibration_statistics_collector_average_min_max.cc exists and can be opened.
  • I tried to use clang (LLVM-17.0.6-win64) to compile the library with it instead of MSVC, since it appears to be the recommended option with tf-2.16.1, I was unable to do it (there is NO documentation online on how to configure LLVM to do this, and I wasted hours without being able to tell bazel to use properly clang (even after adding the bin to the path, and using set BAZEL_LLVM to the proper path, bazel recognized the installation but kept telling me did not find the files). Also Bazel documentation seems to be for the version 7, which is unsupported.
  • There is no prebuilt distribution of any recent version of tensorflow_cc libtensorflow_cc as far as I know.

QUESTIONS

  • I unsure of the difference between tensorflow.dll compiled in c++ (which I am able to build) and tensorflow_cc.dll and libtensorflow_cc.so (which im not able to build). As I understand the tensorflow_cc is the c++ API detailed in TensorFlow C++ API Reference  |  TensorFlow v2.16.1 , but can I use tensorflow.dll? if so, how? what is the difference?
  • Can somebody explain me how to configure and use LLVM in bazel for testing in my case?
  • I’m unsure on how to get the proper header files, I know I can build the target tensorflow :install_headers, can I get from this target the headers of tensorflowlite or libtensorflow_cc?
  • I have seen that the header files must be edited manually after compile the .dll, adding TF_EXPORT is some undocumented way manually and then recompile the library. Does this affect only the :tensorflow target? Or also tensorflow_cc?
  • Does anyone know how to get any somewhat recent precompiled cpu distribution of tensor_flow_cc
  • AND MOST IMPORTANTLY: Any Idea on how to solve this issue?

Update:

It seems something related to long paths is happening. I manage to get to the last step in the build process if I force bazel to use short paths using:

bazel --output_user_root=C:/tmp build -c opt --define=no_tensorflow_py_deps=true tensorflow:libtensorflow_cc.so

However this DOES NOT SOLVE THE PROBLEM, since it seems bazel is unable to link the library anyway despite compiling successfully all the files. (some naming conflict?)

Update 2:

Activating support for long paths in windows also does NOT solve the problem with or without

--output_user_root being used.

Update 3:

I also tried to go back till TensorFlow 2.14 (with half a dozen bazel versions), but was still unable to build tensorflow-cc, this time obtaining all kinds of errors I assume related with bazel or MSVC version compatibility.

SO…

The problem persists, and seems to me is just not possible at the moment to build tensorflow-cc in windows, or at least with MSVC.

I can only assume (if I’m right) that this is a know and ancient issue (it has to be right?), and Google is just dropping support for c++ or at least being very careless with it, and did not bother with solving the issue or even properly warning users about it (I wasted more than 100 hours at this point for this).

Anyway, any info or help is welcome, and desperately required.