0

I need to build Tensorflow as a static library to include into a product. As of now it seems that there's only support for building a shared/dynamic library with bazel. My current objective is to build a library for MacOS(darwin-arm64), but I'm also going to build one for x86.

Has anyone solved this before?


I've gotten some things to work thanks to this thread: https://github.com/tensorflow/rust/pull/351

What I've done is to compile and leave all of the .a and .lo file cache:

bazel build --config=monolithic --macos_minimum_os=11.0 --cpu=darwin_arm64 -j 1 //tensorflow:libtensorflow_cc.so

And then tried to link them together using libtool with using the param generated by bazel to try and get the needed files sorting out unwanted lines and filtering duplicates:

libtool -static -o libtensorflow_arm64_libtool_SO3.a $(cat bazel-bin/tensorflow/libtensorflow_cc.so.*.params | sed -e 's!-Wl,-force_load,!!' | grep -e '\.a$' -e '\.lo$' | sort -t: -u -k1,1)

Some simple things work with this approach but I can for instance run into this following error whilst running my code interfacing the C-API:

F tensorflow/core/framework/allocator_registry.cc:85] No registered CPU AllocatorFactory
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Maxxxxxx
  • 31
  • 5
  • It is a static library, you can copy and run with a suitable environment with the correct right permissions. ( I had re-compile using VS code in Windows 10 too ) https://www.tensorflow.org/install/lang_c#build_from_source – Jirayu Kaewprateep Nov 16 '22 at 15:10
  • It is? I thought .dylib files was dynamic libraries? I would like to compile to a .a file as the project I'm building can't bundle dynamic libraries such as .dylib or .dll as for the case for Windows. It seems that the Windows version has a .lib file included, but I need it to work for MacOS with the arm64 architecture which doesn't have a nightly build. – Maxxxxxx Nov 16 '22 at 16:09
  • Python Lib is .pdb, Window lib has .dll, C++ has .cpp, .h followed the instruction had MacOS already, I also running on Native Windows 10 ( compiled from sources ). Working with features you need to create environments that work with it when they try to work with AVX or AVX2 and CPU or GPU features please find those instructions. – Jirayu Kaewprateep Nov 17 '22 at 05:54
  • A static library on Linux is a .a file, on Windows it is an .lib file and on MacOS it is a .a file. You obviously have no idea what you're talking about here. – Maxxxxxx Nov 17 '22 at 10:09
  • In the link, I reply in the comment because using native Windows 10 but they provided sources where you can build a match environment and self compile the program as me. – Jirayu Kaewprateep Nov 18 '22 at 12:06
  • Ah I see! The problem is that bazel doesn't support building static libraries from source. But I have buildt it from source as stated in my "partial solution". – Maxxxxxx Nov 18 '22 at 13:53
  • It is as the same you download C++ source, On Windows I complied using VSCode please follow the instructions. – Jirayu Kaewprateep Nov 19 '22 at 22:34

1 Answers1

0

Indeed there's no support whatsoever at the moment for building a static library for the Tensorflow C-API. This is due to bazel being the build tool. At the moment of writing bazel doesn't have support for writing static libraries:

https://github.com/bazelbuild/bazel/issues/1920

This has been an issue for quite some time, and this is also the reason why the entire C-API can't be built as a static library at the moment.

But, there's a way around this. You can build Tensorflow lite as a static library with Cmake as can be found here in the Tensorflow git repository: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite

I also found this thread very helpful: TensorFlow static C API library - how to link with 10 sub-dependencies?

After building this you will also need to include the Google flatbuffer library in your project(which you of course can include in your static library as well): https://github.com/google/flatbuffers

TFlite can run most models and works even for my most complex models I've built. So it is the best way to get Tensorflow to work as a static library at the moment. For more information on TFLite see: https://www.tensorflow.org/lite

Maxxxxxx
  • 31
  • 5