load

paddle.utils.cpp_extension. load ( name, sources, extra_cxx_cflags=None, extra_cuda_cflags=None, extra_ldflags=None, extra_include_paths=None, extra_library_paths=None, build_directory=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. On Linux platform, it requires GCC compiler whose version is greater than 5.4 and it should be soft linked to /usr/bin/cc . On Windows platform, it requires Visual Studio whose version is greater than 2017. On MacOS, clang++ is requited. In addition, 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(Linux) , cl.exe(Windows) on local machine is compatible with pre-installed Paddle whl in python site-packages.

For Linux, GCC version will be checked . 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. For Windows, Visual Studio version will be checked, and it should be greater than or equal to that of PaddlePaddle (Visual Studio 2017). If the above conditions are not met, the corresponding warning will be printed, and a fatal error may 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. Currently we support Linux, MacOS and Windows platform.

  2. On Linux platform, we recommend to use GCC 8.2 as soft linking candidate of /usr/bin/cc . Then, Use which cc to ensure location of cc and using cc --version to ensure linking GCC version.

  3. On Windows platform, we recommend to install `` Visual Studio`` (>=2017).

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 customized op
    extra_cxx_cflags=['-g', '-w'],               # optional, specify extra flags to compile .cc/.cpp file
    extra_cuda_cflags=['-O2'],                   # optional, specify extra flags to compile .cu file
    verbose=True                                 # optional, specify to 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.

  • 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. 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.

  • extra_library_paths (list[str], optional) – Specify additional library path used to search library files. By default all basic libraries are included implicitly from site-packages/paddle/libs . 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.

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

Returns

A callable python module contains all CustomOp Layer APIs.

Return type

Module