MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / build

Function build

imperative/python/megengine/utils/custom_op_tools.py:658–865  ·  view source on GitHub ↗

r"""Build a Custom Op with ninja in the way of just-in-time (JIT). To build the custom op, a Ninja build file is emitted, which is used to compile the given sources into a dynamic library. By default, the directory to which the build file is emitted and the resulting library compil

(
    name: str,
    sources: Union[str, List[str]],
    extra_cflags: Union[str, List[str]] = [],
    extra_cuda_cflags: Union[str, List[str]] = [],
    extra_ldflags: Union[str, List[str]] = [],
    extra_include_paths: Union[str, List[str]] = [],
    with_cuda: Optional[bool] = None,
    build_dir: Optional[bool] = None,
    verbose: bool = False,
    abi_tag: Optional[int] = None,
)

Source from the content-addressed store, hash-verified

656
657
658def build(
659 name: str,
660 sources: Union[str, List[str]],
661 extra_cflags: Union[str, List[str]] = [],
662 extra_cuda_cflags: Union[str, List[str]] = [],
663 extra_ldflags: Union[str, List[str]] = [],
664 extra_include_paths: Union[str, List[str]] = [],
665 with_cuda: Optional[bool] = None,
666 build_dir: Optional[bool] = None,
667 verbose: bool = False,
668 abi_tag: Optional[int] = None,
669) -> str:
670 r"""Build a Custom Op with ninja in the way of just-in-time (JIT).
671
672 To build the custom op, a Ninja build file is emitted, which is used to
673 compile the given sources into a dynamic library.
674
675 By default, the directory to which the build file is emitted and the
676 resulting library compiled to is ``<tmp>/mge_custom_op/<name>``, where
677 ``<tmp>`` is the temporary folder on the current platform and ``<name>``
678 the name of the custom op. This location can be overridden in two ways.
679 First, if the ``MGE_CUSTOM_OP_DIR`` environment variable is set, it
680 replaces ``<tmp>/mge_custom_op`` and all custom op will be compiled
681 into subfolders of this directory. Second, if the ``build_dir``
682 argument to this function is supplied, it overrides the entire path, i.e.
683 the library will be compiled into that folder directly.
684
685 To compile the sources, the default system compiler (``c++``) is used,
686 which can be overridden by setting the ``CXX`` environment variable. To pass
687 additional arguments to the compilation process, ``extra_cflags`` or
688 ``extra_ldflags`` can be provided. For example, to compile your custom op
689 with optimizations, pass ``extra_cflags=['-O3']``. You can also use
690 ``extra_cflags`` to pass further include directories.
691
692 CUDA support with mixed compilation is provided. Simply pass CUDA source
693 files (``.cu`` or ``.cuh``) along with other sources. Such files will be
694 detected and compiled with nvcc rather than the C++ compiler. This includes
695 passing the CUDA lib64 directory as a library directory, and linking
696 ``cudart``. You can pass additional flags to nvcc via
697 ``extra_cuda_cflags``, just like with ``extra_cflags`` for C++. Various
698 heuristics for finding the CUDA install directory are used, which usually
699 work fine. If not, setting the ``CUDA_ROOT_DIR`` environment variable is the
700 safest option. If you use CUDNN, please also setting the ``CUDNN_ROOT_DIR``
701 environment variable.
702
703 Args:
704 name: The name of the custom op to build.
705 sources: A list of relative or absolute paths to C++ source files.
706 extra_cflags: optional list of compiler flags to forward to the build.
707 extra_cuda_cflags: optional list of compiler flags to forward to nvcc
708 when building CUDA sources.
709 extra_ldflags: optional list of linker flags to forward to the build.
710 extra_include_paths: optional list of include directories to forward
711 to the build.
712 with_cuda: Determines whether CUDA headers and libraries are added to
713 the build. If set to ``None`` (default), this value is
714 automatically determined based on the existence of ``.cu`` or
715 ``.cuh`` in ``sources``. Set it to `True`` to force CUDA headers

Callers 1

build_and_loadFunction · 0.70

Calls 15

try_acquireMethod · 0.95
releaseMethod · 0.95
waitMethod · 0.95
strlistFunction · 0.85
printFunction · 0.85
_get_build_dirFunction · 0.85
version_checkFunction · 0.85
strFunction · 0.85
_setup_includesFunction · 0.85
_setup_common_cflagsFunction · 0.85

Tested by

no test coverage detected