github.com/alwaysproblem/mlserving-tutorial@v0.0.0-20221124033215-121cfddbfbf4/TFserving/CustomOp/custom-op/README.md (about)

     1  # TensorFlow Custom Op
     2  This is a guide for users who want to write custom c++ op for TensorFlow and distribute the op as a pip package. This repository serves as both a working example of the op building and packaging process, as well as a template/starting point for writing your own ops. The way this repository is set up allow you to build your custom ops from TensorFlow's pip package instead of building TensorFlow from scratch. This guarantee that the shared library you build will be binary compatible with TensorFlow's pip packages.
     3  
     4  This guide currently supports Ubuntu and Windows custom ops, and it includes examples for both cpu and gpu ops.
     5  
     6  Starting from Aug 1, 2019, nightly previews `tf-nightly` and `tf-nightly-gpu`, as well as
     7  official releases `tensorflow` and `tensorflow-gpu` past version 1.14.0 are now built with a
     8  different environment (Ubuntu 16.04 compared to Ubuntu 14.04, for example) as part of our effort to make TensorFlow's pip pacakges
     9  manylinux2010 compatible. To help you building custom ops on linux, here we provide our toolchain in the format of a combination of a Docker image and bazel configurations.  Please check the table below for the Docker image name needed to build your custom ops.
    10  
    11  |          |          CPU custom op          |          GPU custom op         |
    12  |----------|:-------------------------------:|:------------------------------:|
    13  | TF nightly  |    nightly-custom-op-ubuntu16   | nightly-custom-op-gpu-ubuntu16 |
    14  | TF >= 2.3   |   2.3.0-custom-op-ubuntu16  |    2.3.0-custom-op-gpu-ubuntu16    |
    15  | TF 1.5, 2.0 | custom-op-ubuntu16-cuda10.0 |       custom-op-gpu-ubuntu16       |
    16  | TF <= 1.4   |        custom-op-ubuntu14       |     custom-op-gpu-ubuntu14     |
    17  
    18  
    19  Note: all above Docker images have prefix `tensorflow/tensorflow:`
    20  
    21  The bazel configurations are included as part of this repository.
    22  
    23  ## Build Example zero_out Op (CPU only)
    24  If you want to try out the process of building a pip package for custom op, you can use the source code from this repository following the instructions below.
    25  
    26  ### For Windows Users
    27  You can skip this section if you are not building on Windows. If you are building custom ops for Windows platform, you will need similar setup as building TensorFlow from source mentioned [here](https://www.tensorflow.org/install/source_windows). Additionally, you can skip all the Docker steps from the instructions below. Otherwise, the bazel commands to build and test custom ops stay the same.
    28  
    29  ### Setup Docker Container
    30  You are going to build the op inside a Docker container. Pull the provided Docker image from TensorFlow's Docker hub and start a container.
    31  
    32  Use the following command if the TensorFlow pip package you are building
    33  against is not yet manylinux2010 compatible:
    34  ```bash
    35    docker pull tensorflow/tensorflow:custom-op-ubuntu14
    36    docker run -it tensorflow/tensorflow:custom-op-ubuntu14 /bin/bash
    37  ```
    38  And the following instead if it is manylinux2010 compatible:
    39  
    40  ```bash
    41    docker pull tensorflow/tensorflow:custom-op-ubuntu16
    42    docker run -it tensorflow/tensorflow:custom-op-ubuntu16 /bin/bash
    43  ```
    44  
    45  Inside the Docker container, clone this repository. The code in this repository came from the [Adding an op](https://www.tensorflow.org/extend/adding_an_op) guide.
    46  ```bash
    47  git clone https://github.com/tensorflow/custom-op.git
    48  cd custom-op
    49  ```
    50  
    51  ### Build PIP Package
    52  You can build the pip package with either Bazel or make.
    53  
    54  With bazel:
    55  ```bash
    56    ./configure.sh
    57    bazel build build_pip_pkg
    58    bazel-bin/build_pip_pkg artifacts
    59  ```
    60  
    61  With Makefile:
    62  ```bash
    63    make zero_out_pip_pkg
    64  ```
    65  
    66  ### Install and Test PIP Package
    67  Once the pip package has been built, you can install it with,
    68  ```bash
    69  pip3 install artifacts/*.whl
    70  ```
    71  Then test out the pip package
    72  ```bash
    73  cd ..
    74  python3 -c "import tensorflow as tf;import tensorflow_zero_out;print(tensorflow_zero_out.zero_out([[1,2], [3,4]]))"
    75  ```
    76  And you should see the op zeroed out all input elements except the first one:
    77  ```bash
    78  [[1 0]
    79   [0 0]]
    80  ```
    81  
    82  ## Create and Distribute Custom Ops
    83  Now you are ready to write and distribute your own ops. The example in this repository has done the boiling plate work for setting up build systems and package files needed for creating a pip package. We recommend using this repository as a template.
    84  
    85  
    86  ### Template Overview
    87  First let's go through a quick overview of the folder structure of this template repository.
    88  ```
    89  ├── gpu  # Set up crosstool and CUDA libraries for Nvidia GPU, only needed for GPU ops
    90  │   ├── crosstool/
    91  │   ├── cuda/
    92  │   ├── BUILD
    93  │   └── cuda_configure.bzl
    94  |
    95  ├── tensorflow_zero_out  # A CPU only op
    96  │   ├── cc
    97  │   │   ├── kernels  # op kernel implementation
    98  │   │   │   └── zero_out_kernels.cc
    99  │   │   └── ops  # op interface definition
   100  │   │       └── zero_out_ops.cc
   101  │   ├── python
   102  │   │   ├── ops
   103  │   │   │   ├── __init__.py
   104  │   │   │   ├── zero_out_ops.py   # Load and extend the ops in python
   105  │   │   │   └── zero_out_ops_test.py  # tests for ops
   106  │   │   └── __init__.py
   107  |   |
   108  │   ├── BUILD  # BUILD file for all op targets
   109  │   └── __init__.py  # top level __init__ file that imports the custom op
   110  │
   111  ├── tensorflow_time_two  # A GPU op
   112  │   ├── cc
   113  │   │   ├── kernels  # op kernel implementation
   114  │   │   │   |── time_two.h
   115  │   │   │   |── time_two_kernels.cc
   116  │   │   │   └── time_two_kernels.cu.cc  # GPU kernel
   117  │   │   └── ops  # op interface definition
   118  │   │       └── time_two_ops.cc
   119  │   ├── python
   120  │   │   ├── ops
   121  │   │   │   ├── __init__.py
   122  │   │   │   ├── time_two_ops.py   # Load and extend the ops in python
   123  │   │   │   └── time_two_ops_test.py  # tests for ops
   124  │   │   └── __init__.py
   125  |   |
   126  │   ├── BUILD  # BUILD file for all op targets
   127  │   └── __init__.py  # top level __init__ file that imports the custom op
   128  |
   129  ├── tf  # Set up TensorFlow pip package as external dependency for Bazel
   130  │   ├── BUILD
   131  │   ├── BUILD.tpl
   132  │   └── tf_configure.bzl
   133  |
   134  ├── BUILD  # top level Bazel BUILD file that contains pip package build target
   135  ├── build_pip_pkg.sh  # script to build pip package for Bazel and Makefile
   136  ├── configure.sh  # script to install TensorFlow and setup action_env for Bazel
   137  ├── LICENSE
   138  ├── Makefile  # Makefile for building shared library and pip package
   139  ├── setup.py  # file for creating pip package
   140  ├── MANIFEST.in  # files for creating pip package
   141  ├── README.md
   142  └── WORKSPACE  # Used by Bazel to specify tensorflow pip package as an external dependency
   143  
   144  ```
   145  The op implementation, including both c++ and python code, goes under `tensorflow_zero_out` dir for CPU only ops, or `tensorflow_time_two` dir for GPU ops. You will want to replace either directory with the corresponding content of your own ops. `tf` folder contains the code for setting up TensorFlow pip package as an external dependency for Bazel only. You shouldn't need to change the content of this folder. You also don't need this folder if you are using other build systems, such as Makefile. The `gpu` folder contains the code for setting up CUDA libraries and toolchain. You only need the `gpu` folder if you are writing a GPU op and using bazel. To build a pip package for your op, you will also need to update a few files at the top level of the template, for example, `setup.py`, `MANIFEST.in` and `build_pip_pkg.sh`.
   146  
   147  ### Setup
   148  First, clone this template repo.
   149  ```bash
   150  git clone https://github.com/tensorflow/custom-op.git my_op
   151  cd my_op
   152  ```
   153  
   154  #### Docker
   155  Next, set up a Docker container using the provided Docker image for building and testing the ops. We provide two sets of Docker images for different versions of pip packages. If the pip package you are building against was released before Aug 1, 2019 and has manylinux1 tag, please use Docker images `tensorflow/tensorflow:custom-op-ubuntu14` and `tensorflow/tensorflow:custom-op-gpu-ubuntu14`, which are based on Ubuntu 14.04. Otherwise, for the newer manylinux2010 packages, please use Docker images `tensorflow/tensorflow:custom-op-ubuntu16` and `tensorflow/tensorflow:custom-op-gpu-ubuntu16` instead. All Docker images come with Bazel pre-installed, as well as the corresponding toolchain used for building the released TensorFlow pacakges. We have seen many cases where dependency version differences and ABI incompatibilities cause the custom op extension users build to not work properly with TensorFlow's released pip packages. Therefore, it is *highly recommended* to use the provided Docker image to build your custom op. To get the CPU Docker image, run one of the following command based on which pip package you are building against:
   156  ```bash
   157  # For pip packages labeled manylinux1
   158  docker pull tensorflow/tensorflow:custom-op-ubuntu14
   159  
   160  # For manylinux2010
   161  docker pull tensorflow/tensorflow:custom-op-ubuntu16
   162  ```
   163  
   164  For GPU, run
   165  ```bash
   166  # For pip packages labeled manylinux1
   167  docker pull tensorflow/tensorflow:custom-op-gpu-ubuntu14
   168  
   169  # For manylinux2010
   170  docker pull tensorflow/tensorflow:custom-op-gpu-ubuntu16
   171  ```
   172  
   173  You might want to use Docker volumes to map a `work_dir` from host to the container, so that you can edit files on the host, and build with the latest changes in the Docker container. To do so, run the following for CPU
   174  ```bash
   175  # For pip packages labeled manylinux1
   176  docker run -it -v ${PWD}:/working_dir -w /working_dir  tensorflow/tensorflow:custom-op-ubuntu14
   177  
   178  # For manylinux2010
   179  docker run -it -v ${PWD}:/working_dir -w /working_dir  tensorflow/tensorflow:custom-op-ubuntu16
   180  ```
   181  
   182  For GPU, you want to use `nvidia-docker`:
   183  ```bash
   184  # For pip packages labeled manylinux1
   185  docker run --runtime=nvidia --privileged  -it -v ${PWD}:/working_dir -w /working_dir  tensorflow/tensorflow:custom-op-gpu-ubuntu14
   186  
   187  # For manylinux2010
   188  docker run --runtime=nvidia --privileged  -it -v ${PWD}:/working_dir -w /working_dir  tensorflow/tensorflow:custom-op-gpu-ubuntu16
   189  
   190  ```
   191  
   192  #### Run configure.sh
   193  Last step before starting implementing the ops, you want to set up the build environment. The custom ops will need to depend on TensorFlow headers and shared library libtensorflow_framework.so, which are distributed with TensorFlow official pip package. If you would like to use Bazel to build your ops, you might also want to set a few action_envs so that Bazel can find the installed TensorFlow. We provide a `configure` script that does these for you. Simply run `./configure.sh` in the docker container and you are good to go.
   194  
   195  
   196  ### Add Op Implementation
   197  Now you are ready to implement your op. Following the instructions at [Adding a New Op](https://www.tensorflow.org/extend/adding_an_op), add definition of your op interface under `<your_op>/cc/ops/` and kernel implementation under `<your_op>/cc/kernels/`.
   198  
   199  
   200  ### Build and Test CPU Op
   201  
   202  #### Bazel
   203  To build the custom op shared library with Bazel, follow the cc_binary example in [`tensorflow_zero_out/BUILD`](https://github.com/tensorflow/custom-op/blob/master/tensorflow_zero_out/BUILD#L5). You will need to depend on the header files and libtensorflow_framework.so from TensorFlow pip package to build your op. Earlier we mentioned that the template has already setup TensorFlow pip package as an external dependency in `tf` directory, and the pip package is listed as `local_config_tf` in [`WORKSPACE`](https://github.com/tensorflow/custom-op/blob/master/WORKSPACE) file. Your op can depend directly on TensorFlow header files and 'libtensorflow_framework.so' with the following:
   204  ```python
   205      deps = [
   206          "@local_config_tf//:libtensorflow_framework",
   207          "@local_config_tf//:tf_header_lib",
   208      ],
   209  ```
   210  
   211  You will need to keep both above dependencies for your op. To build the shared library with Bazel, run the following command in your Docker container
   212  ```bash
   213  bazel build tensorflow_zero_out:python/ops/_zero_out_ops.so
   214  ```
   215  
   216  #### Makefile
   217  To build the custom op shared library with make, follow the example in [`Makefile`](https://github.com/tensorflow/custom-op/blob/master/Makefile) for `_zero_out_ops.so` and run the following command in your Docker container:
   218  ```bash
   219  make op
   220  ```
   221  
   222  #### Extend and Test the Op in Python
   223  Once you have built your custom op shared library, you can follow the example in [`tensorflow_zero_out/python/ops`](https://github.com/tensorflow/custom-op/tree/master/tensorflow_zero_out/python/ops), and instructions [here](https://www.tensorflow.org/extend/adding_an_op#use_the_op_in_python) to create a module in Python for your op. Both guides use TensorFlow API `tf.load_op_library`, which loads the shared library and registers the ops with the TensorFlow framework.
   224  ```python
   225  from tensorflow.python.framework import load_library
   226  from tensorflow.python.platform import resource_loader
   227  
   228  _zero_out_ops = load_library.load_op_library(
   229      resource_loader.get_path_to_datafile('_zero_out_ops.so'))
   230  zero_out = _zero_out_ops.zero_out
   231  
   232  ```
   233  
   234  You can also add Python tests like what we have done in `tensorflow_zero_out/python/ops/zero_out_ops_test.py` to check that your op is working as intended.
   235  
   236  
   237  ##### Run Tests with Bazel
   238  To add the python library and tests targets to Bazel, please follow the examples for `py_library` target `tensorflow_zero_out:zero_out_ops_py` and `py_test` target `tensorflow_zero_out:zero_out_ops_py_test` in `tensorflow_zero_out/BUILD` file. To run your test with bazel, do the following in Docker container,
   239  
   240  ```bash
   241  bazel test tensorflow_zero_out:zero_out_ops_py_test
   242  ```
   243  
   244  ##### Run Tests with Make
   245  To add the test target to make, please follow the example in `Makefile`. To run your python test, simply run the following in Docker container,
   246  ```bash
   247  make test_zero_out
   248  ```
   249  
   250  ### Build and Test GPU Op
   251  
   252  #### Bazel
   253  To build the custom GPU op shared library with Bazel, follow the cc_binary example in [`tensorflow_time_two/BUILD`](https://github.com/tensorflow/custom-op/blob/master/tensorflow_time_two/BUILD#L29). Similar to CPU custom ops, you can directly depend on TensorFlow header files and 'libtensorflow_framework.so' with the following:
   254  ```python
   255      deps = [
   256          "@local_config_tf//:libtensorflow_framework",
   257          "@local_config_tf//:tf_header_lib",
   258      ],
   259  ```
   260  
   261  Additionally, when you ran configure inside the GPU container, `config=cuda` will be set for bazel command, which will also automatically include cuda shared library and cuda headers as part of the dependencies only for GPU version of the op: `if_cuda_is_configured([":cuda",  "@local_config_cuda//cuda:cuda_headers"])`.
   262  
   263  To build the shared library with Bazel, run the following command in your Docker container
   264  ```bash
   265  bazel build tensorflow_time_two:python/ops/_time_two_ops.so
   266  ```
   267  
   268  #### Makefile
   269  To build the custom op shared library with make, follow the example in [`Makefile`](https://github.com/tensorflow/custom-op/blob/master/Makefile) for `_time_two_ops.so` and run the following command in your Docker container:
   270  ```bash
   271  make time_two_op
   272  ```
   273  
   274  #### Extend and Test the Op in Python
   275  Once you have built your custom op shared library, you can follow the example in [`tensorflow_time_two/python/ops`](https://github.com/tensorflow/custom-op/tree/master/tensorflow_time_two/python/ops), and instructions [here](https://www.tensorflow.org/extend/adding_an_op#use_the_op_in_python) to create a module in Python for your op. This part is the same as CPU custom op as shown above.
   276  
   277  
   278  ##### Run Tests with Bazel
   279  Similar to CPU custom op, to run your test with bazel, do the following in Docker container,
   280  
   281  ```bash
   282  bazel test tensorflow_time_two:time_two_ops_py_test
   283  ```
   284  
   285  ##### Run Tests with Make
   286  To add the test target to make, please follow the example in `Makefile`. To run your python test, simply run the following in Docker container,
   287  ```bash
   288  make time_two_test
   289  ```
   290  
   291  
   292  
   293  
   294  ### Build PIP Package
   295  Now your op works, you might want to build a pip package for it so the community can also benefit from your work. This template provides the basic setup needed to build your pip package. First, you will need to update the following top level files based on your op.
   296  
   297  - `setup.py` contains information about your package (such as the name and version) as well as which code files to include.
   298  - `MANIFEST.in` contains the list of additional files you want to include in the source distribution. Here you want to make sure the shared library for your custom op is included in the pip package.
   299  - `build_pip_pkg.sh` creates the package hierarchy, and calls `bdist_wheel` to assemble your pip package.
   300  
   301  You can use either Bazel or Makefile to build the pip package.
   302  
   303  
   304  #### Build with Bazel
   305  You can find the target for pip package in the top level `BUILD` file. Inside the data list of this `build_pip_pkg` target, you want to include the python library target ` //tensorflow_zero_out:zero_out_py` in addition to the top level files. To build the pip package builder, run the following command in Docker container,
   306  ```bash
   307  bazel build :build_pip_pkg
   308  ```
   309  
   310  The bazel build command creates a binary named build_pip_package, which you can use to build the pip package. For example, the following builds your .whl package in the `artifacts` directory:
   311  ```bash
   312  bazel-bin/build_pip_pkg artifacts
   313  ```
   314  
   315  #### Build with make
   316  Building with make also invoke the same `build_pip_pkg.sh` script. You can run,
   317  ```bash
   318  make pip_pkg
   319  ```
   320  
   321  ### Test PIP Package
   322  Before publishing your pip package, test your pip package.
   323  ```bash
   324  pip3 install artifacts/*.whl
   325  python3 -c "import tensorflow as tf;import tensorflow_zero_out;print(tensorflow_zero_out.zero_out([[1,2], [3,4]]))"
   326  ```
   327  
   328  
   329  ### Publish PIP Package
   330  Once your pip package has been thoroughly tested, you can distribute your package by uploading your package to the Python Package Index. Please follow the [official instruction](https://packaging.python.org/tutorials/packaging-projects/#uploading-the-distribution-archives) from Pypi.
   331  
   332  
   333  ### FAQ
   334  
   335  Here are some issues our users have ran into and possible solutions. Feel free to send us a PR to add more entries.
   336  
   337  
   338  | Issue  |  How to? |
   339  |---|---|
   340  |  Do I need both the toolchain and the docker image? | Yes, you will need both to get the same setup we use to build TensorFlow's official pip package. |
   341  |  How do I also create a manylinux2010 binary? | You can use [auditwheel](https://github.com/pypa/auditwheel) version 2.0.0 or newer.  |
   342  |  What do I do if I get `ValueError: Cannot repair wheel, because required library "libtensorflow_framework.so.1" could not be located` or `ValueError: Cannot repair wheel, because required library "libtensorflow_framework.so.2" could not be located` with auditwheel? | Please see [this related issue](https://github.com/tensorflow/tensorflow/issues/31807).  |
   343  | What do I do if I get `In file included from tensorflow_time_two/cc/kernels/time_two_kernels.cu.cc:21:0: /usr/local/lib/python3.6/dist-packages/tensorflow/include/tensorflow/core/util/gpu_kernel_helper.h:22:10: fatal error: third_party/gpus/cuda/include/cuda_fp16.h: No such file or directory` | Copy the CUDA header files to target directory. `mkdir -p /usr/local/lib/python3.6/dist-packages/tensorflow/include/third_party/gpus/cuda/include && cp -r /usr/local/cuda/targets/x86_64-linux/include/* /usr/local/lib/python3.6/dist-packages/tensorflow/include/third_party/gpus/cuda/include` |