TensorFlow Python APIs are automatically generated by Pybind11 and some utility scripts. This blog introduces how these Python APIs are generated.
1. bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
This command builds TensorFlow Python packages, including automatically generating Python APIs. The corresponding BUILD file is located at tensorflow/tools/pip_package/BUILD
as below:
1 | sh_binary( |
Where, COMMON_PIP_DEPS
contains all the Python dependencies.
COMMON_PIP_DEPS
looks like below
1 | COMMON_PIP_DEPS = [ |
Where, //tensorflow:tensorflow_py
contains the Python low level APIs.
2. //tensorflow:tensorflow_py
It is located at tensorflow/BUILD
and defined as below
1 | py_library( |
Where, :tensorflow_py_no_contrib
is defined as below:
1 | py_library( |
It contains the dependency //tensorflow/python:no_contrib
.
3. //tensorflow/python:no_contrib
It is defined at tensorflow/python/BUILD
as below:
1 | py_library( |
Where, _pywrap_python_op_gen
and :pywrap_tensorflow
are used to automatically generate the Python APIs.
_pywrap_python_op_gen
is defined as below:
1 | tf_python_pybind_extension( |
More details about how it is executed can be found in def tf_python_pybind_extension(...)
and def pybind_extension(...)
in the file tensorflow/tensorflow.bzl
.
The Python APIs are generated for different groups of operations seperately. For example, the above :array_ops
is defined as below:
1 | py_library( |
Where :array_ops_gen
is included in the dependencies and defined as below:
1 | tf_gen_op_wrapper_private_py( |
4. tf_gen_op_wrapper_private_py
It id defined at tensorflow/python/build_defs.bzl
as below:
1 | load("//tensorflow:tensorflow.bzl", "tf_gen_op_wrapper_py") |
It calls the function tf_gen_op_wrapper_py
internally.
5. tf_gen_op_wrapper_py
It is defined at tensorflow/tensorflow.bzl
as below:
1 | # Generates a Python library target wrapping the ops registered in "deps". |
The above function defines a tf_cc_binary
as a util tool for generating the Python APIs for the input Ops as below:
1 | . |
The above function is trigger by below:
1 | if hidden_file: |
The actual code used to generate Python code is defined at //tensorflow/python:python_op_gen_main
.
6. //tensorflow/python:python_op_gen_main
It is located at tensorflow/python/BUILD
as below:
1 | cc_library( |
tensorflow/python/framework/python_op_gen_main.cc
is the entrance for Python code generation. It takes the API definition files undertensorflow/core/api_def/base_api
as the inputs.tensorflow/python/framework/python_op_gen.[h|cc]
are used to automatically generate Python files for the TensorFlow Python ops based on the op definitions. The automatic generated files are located atbazel-bin/tensorflow/python/ops
and these files start withgen_...
BTW: C++ APIs
C++ APIs are generated by the similar way with Python APIs. The code is located at tensorflow/cc/framework/cc_op_gen_main.cc
. The BUILD block is located at tensorflow/cc/BUILD
as below:
1 | cc_library( |
where, all the API definition files under tensorflow/core/api_def/base_api
will be used as the input for cc_op_gen_main
.