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:
- 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
- Install Bazel and add to path
- Install MSYS2 and add to path
- Download and uncompress Tensorflow
- 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) andtensorflow_cc.dll
andlibtensorflow_cc.so
(which im not able to build). As I understand thetensorflow_cc
is the c++ API detailed in TensorFlow C++ API Reference | TensorFlow v2.16.1 , but can I usetensorflow.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?