Building TF custom ops for custom tensorflow and python versions
Following the instructions from the tensorflow team, building tensorflow custom operation inside a docker container works out of the box for TF 2.4. But if this custom op is needed for a custom tensorflow and custom python version, some issues need to be addressed. Here I will summarize the steps needed to successfully built tensorflow example “zero out op” for TF 1.15, python 3.7, without GPU support (but the steps can probably be generalized for other setups also):
- Following the instructions from the tensorflow team run (installed docker is needed):
docker pull tensorflow/tensorflow:custom-op-ubuntu16 docker run -it tensorflow/tensorflow:custom-op-ubuntu16 /bin/bashand clone the repo
git clone https://github.com/tensorflow/custom-op.gitAlternatively, you can clone my fork of the custom-op-repo, where I included also the inner product operation with gradient computation from the repo of David Stutz.
- If python version other than 3.6 (here 3.7) is desired, install it:
apt update apt install python3.7Make
python3point onpython3.7:rm /etc/alternatives/python3 ln -s /usr/local/bin/python3.7 /etc/alternatives/python3 - Fixing pip. At this point my pip3.7 was broken. To fix it, following a comment in this post, run
vim /usr/bin/lsb_releaseand edit the first line to be
#!/usr/bin/python3.7 -Es. You can then try to runpip3.7 listwhich should now output no errors. Now, makepip3point onpip3.7:rm /usr/local/bin/pip3 ln -s /usr/local/bin/pip3.7 /usr/local/bin/pip3Finally, upgrade pip (this is important to be able to install tensorflow 1.15)
pip3 install --upgrade pip - Install desired version of tensorflow (I tried 1.15):
pip3 install tensorflow==1.15 pip3 install tensorflow-cpu==1.15 - Before building the package another issue needs to be fixed -the tensorflow 2.1 needs to be removed from the requirements of the new pip package. Execute
vim /custom-op/setup.pyand change the lines
REQUIRED_PACKAGES = [ 'tensorflow >= 2.1.0', ]to
REQUIRED_PACKAGES = [ 'tensorflow >= 1.15.0', ](or other version that you have installed). This is not needed if you use my fork of the repo and TF version >=1.15.
- Finally, following the custom op repo, build the package. At first, execute:
cd /custom-op ./configure.shDo not forget to answer the last question with “n”, otherwise the operation will be built for tensorflow 2.x. After that, run
bazel build build_pip_pkg bazel-bin/build_pip_pkg artifactsNow, the pip package is ready in folder
/custom-op/artifactsand can be installed withpip3 install artifacts/*.whl - Now you can test the op directly in docker container by running
cd / python3and executing the following code chunk:
import tensorflow as tf import tensorflow_zero_out x=tf.constant([[1,2], [3,4]]) y=tensorflow_zero_out.zero_out(x) with tf.Session() as sess: print(sess.run(y))The output should be
[[1 0] [0 0]]You can also copy the package using
docker cp <container-id>:/custom-op/artifacts <path-where-to-copy-the-package>to install and test it locally.
Thats all. These steps were successfully applied to build the custom zero-out operation for python 3.7, tensorflow 1.15 and without GPU support. For other setups, or after update of the tensorflow/tensorflow:custom-op-ubuntu16 image, different issues may of course appear.