load

paddle.utils.cpp_extension.cpp_extension. load ( name, sources, extra_cxx_cflags=None, extra_cuda_cflags=None, extra_ldflags=None, extra_include_paths=None, build_directory=None, interpreter=None, verbose=False ) [source]

An Interface to automatically compile C++/CUDA source files Just-In-Time and return callable python function as other Paddle layers API. It will append user defined custom operators in background while building models.

It will perform compiling, linking, Python API generation and module loading processes under a individual subprocess. It does not require CMake or Ninja environment and only g++/nvcc on Linux and clang++ on MacOS. For example it requires GCC compiler with version is greater than 5.4 and linked into /usr/bin/cc . If compiling Operators supporting GPU device, please make sure nvcc compiler is installed in local environment.

Moreover, ABI compatibility will be checked to ensure that compiler version from cc on local machine is compatible with pre-installed Paddle whl in python site-packages. For example if Paddle with CUDA 10.1 is built with GCC 8.2, then the version of user’s local machine should satisfy GCC >= 8.2. Otherwise, a fatal error will occur because of ABI compatibility.

Compared with setup interface, it doesn’t need extra setup.py and excute python setup.py install command. The interface contains all compiling and installing process underground.

Note

  1. Compiler ABI compatibility is forward compatible. On Linux platform, we recommend to use GCC 8.2 as soft linking condidate of /usr/bin/cc .

  2. Using which cc to ensure location of cc and using cc --version to ensure linking GCC version on Linux.

  3. Currenly we support Linux and Windows platfrom. MacOS is supporting…

A simple example:

import paddle
from paddle.utils.cpp_extension import load

custom_op_module = load(
    name="op_shared_libary_name",                # name of shared library
    sources=['relu_op.cc', 'relu_op.cu'],        # source files of cusomized op
    extra_cxx_cflags=['-DPADDLE_WITH_MKLDNN'],   # need to specify the flag if pre-installed Paddle supports MKLDNN
    extra_cuda_cflags=['-DPADDLE_WITH_MKLDNN'],  # need to specify the flag if pre-installed Paddle supports MKLDNN
    interpreter='python3.7',                     # optional, specify another python interpreter
    verbose=True                                 # output log information
)

x = paddle.randn([4, 10], dtype='float32')
out = custom_op_module.relu(x)
Parameters
  • name (str) – Specify the name of generated shared library file name, not including .so and .dll suffix.

  • sources (list[str]) – Specify source files name of customized operators. Supporting .cc , .cpp for CPP file and .cu for CUDA file.

  • extra_cxx_cflags (list[str], optional) – Specify additional flags used to compile CPP files. By default all basic and framework related flags have been included. If your pre-insall Paddle supported MKLDNN, please add -DPADDLE_WITH_MKLDNN . Default is None.

  • extra_cuda_cflags (list[str], optional) – Specify additional flags used to compile CUDA files. By default all basic and framework related flags have been included. If your pre-insall Paddle supported MKLDNN, please add -DPADDLE_WITH_MKLDNN . Default None. See Cuda Compiler Driver NVCC for details. Default is None.

  • extra_ldflags (list[str], optional) – Specify additional flags used to link shared library. See GCC Link Options for details. Default is None.

  • extra_include_paths (list[str], optional) – Specify additional include path used to search header files. By default all basic headers are included implicitly from site-package/paddle/include . Default is None.

  • build_directory (str, optional) – Specify root directory path to put shared library file. If set None, it will use PADDLE_EXTENSION_DIR from os.environ. Use paddle.utils.cpp_extension.get_build_directory() to see the location. Default is None.

  • interpreter (str, optional) – Specify nterpreter path, supporting alias and full path. If set None, it will use python as default interpreter. If local environment contains more than one python interpreters and want to use new interpreter to apply compilation, please specify this parameter, such as python3.7 . Default is None.

  • verbose (bool, optional) – whether to verbose compiled log information. Default is False

Returns

A callable python module contains all CustomOp Layer APIs.

Return type

Moudle