Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to program with C++ API library on Windows using Bazel?

What I want to do

First of all, my goal is using Tensorflow C++ API as a library on Windows, which is part of my project, instead of building my project inside Tensorflow.

Background

I had achieved this by building Tensorflow with CMake. However, from Tensorflow 1.10, building with CMake was deprecated and Bazel is recommended instead. But the official way to use C++ API is building project inside Tensorflow with Bazel. Thus, this way is not good for me.

What I have done

To use a newer version of Tensorflow, I have been trying to build Tensorflow with Bazel as a standalone library.

Some maintainer denoted that it is possible by substituting //tensorflow/tools/pip_package:build_pip_package to //tensorflow:libtensorflow_cc.so in the official tutorial. But in fact I encountered some problems and solved them by reading this tutorial. Now I have successfully built libtensorflow_cc.so.

What the problem is

However, I have no idea what should be done next to use the built result. And it is exactly what my problem is. There is no documentation of course. Only some incomplete ideas on it I have found, and I will show all of them, trying to give you more information:

  • There is somebody already successfully linking built *.so and having solved the problems he has encountered.
  • There is a repo doing the what I want to do on Ubuntu and Arch Linux. I have contacted with the maintainer and he told me that they have no plan for supporting Windows now.
  • A related issue: Building a .dll on Windows.
  • A related issue: Packaged TensorFlow C++ library for bazel-independent use.
  • A related issue: Feature request: provide a means to configure, build, and install that includes cc.
  • A related question: How to build and use Google TensorFlow C++ api. The scope of this question is a little larger without 'using bazel' and 'on Windows' restrictions.
  • A related pull request: C++ API

There must be someone struggling with similar problems like me. I hope this question can build a reservoir of ways to solve the problem.

like image 765
Guikarist Avatar asked Dec 17 '18 05:12

Guikarist


People also ask

How to build TensorFlow C++ API on Windows using Bazel?

Current state: Now we can build Tensorflow C++ API on Windows using Bazel by bazel build //tensorflow:libtensorflow_cc.so (I even wrote a script to do this). However, there are some problems: The built libraries have .so extensions which are not for Windows platform, although after being renamed to .dll the dynamic libraries can be used.

Is there a Windows API tutorial for C programming language?

This is Windows API tutorial for the C programming language. It is pure Windows API tutorial. It does not cover MFC. After reading this tutorial, you will be able to program non trivial Windows applications. Even if you program in a higher level programming language, this tutorial will give you valuable knowledge of how things work under the hood.

Where does Bazel build output files for pY_binary rules?

On Windows, Bazel builds two output files for java_binary rules: C:\projects\bazel> bazel build //examples/java-native/src/main/java/com/example/myproject:hello-world C:\projects\bazel> bazel-bin\examples\java-native\src\main\java\com\example\myproject\hello-world.exe On Windows, Bazel builds two output files for py_binary rules:

How do I set Bazel to use a specific Visual C++ compiler?

Bazel automatically detects the Visual C++ compiler on your system. To tell Bazel to use a specific VC installation, you can set the following environment variables: For Visual Studio 2017 and 2019, set one of BAZEL_VC. Additionally you may also set BAZEL_VC_FULL_VERSION.


1 Answers

It's over 2 years since this question was asked, and the news is not good: it seems there are insufficient people with Windows skills in a position to provide the support to integrate Tensorflow into Windows applications using the familiar headers + library model. And Tensorflow advances week by week, meaning that the Windows support falls further behind.

In my assessment, the path to building on Windows is currently blocked due to inadequate documentation. It's not so much that "There is no documentation of course" as the OP asserts, it's that the sparse documentation is distributed throughout dozens of separate posts, each of which dates rapidly with the continuing development of the Tensorflow along paths other than Windows C++.

I originally gave this answer to a similar question, but updated it with advice along the following lines yesterday:

  • Windows is a Microsoft product, so watch what Microsoft is doing
    • Hint: Microsoft is investing in the ONNX format
    • you can convert Tensorflow to ONNX, or Keras to ONNX
  • You can implement your (ONNX) model on Windows in C++ in at least 3 ways:
    • Windows ML (uses Onnx runtime)
    • Onnx runtime (supports DirectML as an execution provider)
    • DirectML (how Microsoft uses graphics cards to boost performance)

We don't have the latest or best hardware (e.g. we have Intel graphics cards), but have been able to get a solution based on Onnx runtime that classifies 224 x 224 RGB images in about 20 milliseconds for us. We found the Windows ML path much more difficult to work with legacy code, and also slower to run.

like image 150
omatai Avatar answered Sep 23 '22 21:09

omatai