Tensorflow Cmake

broken image


  1. Tensorflow Cmake Build
  2. Tensorflow Cmake Target

Dec 09, 2019 In this tutorial, you will learn to install TensorFlow 2.0 on your Ubuntu system either with or without a GPU. There are a number of important updates in TensorFlow 2.0, including eager execution, automatic differentiation, and better multi-GPU/distributed training support, but the most important update is that Keras is now the official high-level deep learning API for TensorFlow. Regarding contribution in Tensorflow organization for GSoC 21 Amulya Gupta Sun, Mar 21, 12:48 PM to tensorflow-gsoc Dear Sir/Ma'am, I am Amulya Gupta, a sophomore from BITS,Pilani. I have been coding for the past 3 years. Machine learning(ML) is a still growing field, and with the TensorFlow Lite port, there is now support for machine learning on microcontrollers. TensorFlow is an end-to-end ML-platform owned by Google. We want to use TensorFlow Lite to implement support for nRF-chips, and demonstrate that it works for a simple example by TensorFlow. Cmake for userops. GitHub Gist: instantly share code, notes, and snippets. You need CMake installed and downloaded TensorFlow source code. Please check Build TensorFlow Lite with CMake page for the details. Check your target environment The following examples are tested under Raspberry Pi OS, Ubuntu Server 20.04 LTS and Mendel Linux 4.0.

ARTICLE

Implementing Tensorflow Operations in C++ — Including Gradients

In this article, I discuss a simple Tensorflow operation implemented in C++. While the example mostly builds upon the official documentation, it includes trainable parameters and the gradient computation is implemented in C++, as well. As such, the example is slightly more complex compared to the simple ZeroOut operation discussed in the documentation.

Although Tensorflow provides a thorough tutorial on how to add new operations, the provided example is rather simple and gradients are meant to be implemented in Python. However, in many practical cases, operations get more complex and involve parameters that are optimized. In order to get started implementing complex operations for Tensorflow in C++, I implemented a simple linear operation for neural networks (i.e. a matrix-vector multiplication operation, sometimes also referred to as inner product layer). The example includes both trainable parameters and gradients implemented in C++ instead of Python.

The example is not very general and should not be used in actual production code. Instead, it is meant to complement the simple example provided in the documentation. The code is available on GitHub:

Example on GitHub

Forward Operation

The listing below shows the implementation of the forward operation, i.e. given an input vector and a weight matrix the matrix-vector product is calculated. The implemented is saved to inner_product.cc in an arbitrary directory:

Slightly following the documentation, the implementations contains the following important parts:

  • Beginning in line 13 the interface of the operation is defined; this includes defining input and output attributes as well as a function for shape inference. As discussed in the official documentation, attributes can also added here.
  • The Compute method beginning in line 48 contains the actual implementation of the inner product operation.
  • For simplicity, the operation is implemented directly beginning in line 81. However, there should be capabilities for an easier implementation provided by Tensorflow — I just did not find them. The tensor contents are accessed directly via the underlying Eigen tensors. Thanks to C++11's auto, the types do not need be known in detail and the tensors can be accessed via tensorflow_tensor.vec(), tensorflow_tensor.matrix() or in general tensorflow_tensor.tensor
Tensorflow cmake github

As isunchy mentioned in the comments, the matmul implementation in TensorFlow I was not able to find FastGemmFunctor.

The listing above is mostly analogously to the forward operation except for a minor difference:

  • Beginning in line 11, the interface of the operation is defined, taking the original input, the weights and the gradients from the top node in the computation graph (e.g. the top layer in neural network terms) as input, and defining the gradients with respect to the input and the weights as outputs. The shape inference function is omitted.

Given the gradient operation, it needs to be registered and associated with the forward operation. This is done in Python, specifically in _inner_product_grad.py:

It becomes clear that up to now, the forward operation and the gradient operation where completely independent from each other. Also note how the InnerProductGrad operation is imported in Python; this requires to know the location of the corresponding shared library (i.e. the .so file). Building using CMake is discussed in the following section.

Building

Update. As alternative to CMake, a bash script for building can be found in the comments. Also note that the provided CMake file might not work for Tensorflow 1.11; a workaround can — again — be found in the comments.

As I am most comfortable with CMake, I was relieved to find out that Bazel is not mandatory when implementing new operations. The following listing shows a simple CMakeLists.txt doing the job:

There are a few things to note:

  • Note line 5, where tensorflow.sysconfig.get_include() is used to get the include directories of the Tensorflow installation — this is also detailed in the documentation.
  • In lines 10 and 13, the operations are compiled as a shared libraries.

Both operations (which are put together in Python) can be compiled using:

Of course, this assumes that all mentioned files are placed in the same directory. The shared libraries will then be found in the build directory: build/libinner_product.so and build/libinner_product_grad.so.

Tensorflow Cmake Build

Tests

In order to illustrate the usage of the operation, both the forward and backward pass, some unit tests can be found in the listing below:

Some comments:

  • Note that in line 10, only the forward operation — libinner_product.so — is imported. Remember that the backward operation was registered in _inner_product_grad.py which is imported in line 9 and itself imports libinner_product_grad.so.
  • The test beginning in line 15 illustrates some of the cases that are caught by the shape inference function defined for the forward pass. As of my experience, checks (e.g. using DCHECK_XX) inside the Compute function are handled differently than checks in the shape inference function.
  • The test starting in line 22 illustrates a simple forward pass.
  • The remaining two tests illustrate gradient computation with respect to both the input and the weights.

Conclusion

The presented example is simple enough to demonstrate the general idea of adding new operations in Tensorflow. Still, it also includes some more complex cases — such as trainable parameters and the gradient operation implemented in C++ — compared to the official documentation. Overall, Tensorflow tries to make custom operations as easy as possible. Nevertheless, the internal mechanics of Tensorflow are hard to understand — which will hopefully get easier with improved documentation and comments within the Tensorflow core.

Tensorflow Cmake Target

What is your opinion on this article? Did you find it interesting or useful? Let me know your thoughts in the comments below:





broken image