extension_utils.py 45.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import atexit
16 17
import collections
import glob
18
import hashlib
19
import importlib.util
20
import json
21
import logging
22 23
import os
import re
24
import subprocess
25
import sys
26
import sysconfig
27
import textwrap
28
import threading
29
import warnings
30
from contextlib import contextmanager
31 32
from importlib import machinery

33 34
from setuptools.command import bdist_egg

35 36 37 38 39
try:
    from subprocess import DEVNULL  # py3
except ImportError:
    DEVNULL = open(os.devnull, 'wb')

40
from ...fluid import core
41
from ...fluid.framework import OpProtoHolder
42 43
from ...sysconfig import get_include, get_lib

44
logger = logging.getLogger("utils.cpp_extension")
45 46 47 48 49
logger.setLevel(logging.INFO)
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)
50 51 52

OS_NAME = sys.platform
IS_WINDOWS = OS_NAME.startswith('win')
53 54

MSVC_COMPILE_FLAGS = [
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    '/MT',
    '/wd4819',
    '/wd4251',
    '/wd4244',
    '/wd4267',
    '/wd4275',
    '/wd4018',
    '/wd4190',
    '/EHsc',
    '/w',
    '/DGOOGLE_GLOG_DLL_DECL',
    '/DBOOST_HAS_STATIC_ASSERT',
    '/DNDEBUG',
    '/DPADDLE_USE_DSO',
69
]
70
CLANG_COMPILE_FLAGS = [
71 72 73 74 75 76 77 78
    '-fno-common',
    '-dynamic',
    '-DNDEBUG',
    '-g',
    '-fwrapv',
    '-O3',
    '-arch',
    'x86_64',
79 80
]
CLANG_LINK_FLAGS = [
81 82 83 84 85
    '-dynamiclib',
    '-undefined',
    'dynamic_lookup',
    '-arch',
    'x86_64',
86
]
87

88
MSVC_LINK_FLAGS = ['/MACHINE:X64']
89

90 91
if core.is_compiled_with_rocm():
    COMMON_HIPCC_FLAGS = [
92 93 94
        '-DPADDLE_WITH_HIP',
        '-DEIGEN_USE_GPU',
        '-DEIGEN_USE_HIP',
95 96 97
    ]
else:
    COMMON_NVCC_FLAGS = ['-DPADDLE_WITH_CUDA', '-DEIGEN_USE_GPU']
98

99
GCC_MINI_VERSION = (5, 4, 0)
100
MSVC_MINI_VERSION = (19, 0, 24215)
101 102 103
# Give warning if using wrong compiler
WRONG_COMPILER_WARNING = '''
                        *************************************
104
                        *  Compiler Compatibility WARNING   *
105 106 107 108
                        *************************************

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

109
Found that your compiler ({user_compiler}) is not compatible with the compiler
110 111 112 113
built Paddle for this platform, which is {paddle_compiler} on {platform}. Please
use {paddle_compiler} to compile your custom op. Or you may compile Paddle from
source using {user_compiler}, and then also use it compile your custom op.

114
See https://www.paddlepaddle.org.cn/documentation/docs/zh/install/compile/fromsource.html
115 116 117 118 119 120 121 122 123 124 125 126
for help with compiling Paddle from source.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'''
# Give warning if used compiler version is incompatible
ABI_INCOMPATIBILITY_WARNING = '''
                            **********************************
                            *    ABI Compatibility WARNING   *
                            **********************************

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

127
Found that your compiler ({user_compiler} == {version}) may be ABI-incompatible with pre-installed Paddle!
128 129 130 131 132 133 134
Please use compiler that is ABI-compatible with GCC >= 5.4 (Recommended 8.2).

See https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html for ABI Compatibility
information

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'''
135

136 137 138 139 140
DEFAULT_OP_ATTR_NAMES = [
    core.op_proto_and_checker_maker.kOpRoleAttrName(),
    core.op_proto_and_checker_maker.kOpRoleVarAttrName(),
    core.op_proto_and_checker_maker.kOpNameScopeAttrName(),
    core.op_proto_and_checker_maker.kOpCreationCallstackAttrName(),
141
    core.op_proto_and_checker_maker.kOpDeviceAttrName(),
142
    core.op_proto_and_checker_maker.kOpWithQuantAttrName(),
143 144
]

145

146 147 148 149 150 151 152 153 154 155 156 157
@contextmanager
def bootstrap_context():
    """
    Context to manage how to write `__bootstrap__` code in .egg
    """
    origin_write_stub = bdist_egg.write_stub
    bdist_egg.write_stub = custom_write_stub
    yield

    bdist_egg.write_stub = origin_write_stub


158
def load_op_meta_info_and_register_op(lib_filename):
159
    core.load_op_meta_info_and_register_op(lib_filename)
160 161 162
    return OpProtoHolder.instance().update_op_proto()


163 164 165 166 167
def custom_write_stub(resource, pyfile):
    """
    Customized write_stub function to allow us to inject generated python
    api codes into egg python file.
    """
168 169
    _stub_template = textwrap.dedent(
        """
170 171
        {custom_api}

172 173
        import os
        import sys
174
        import types
175
        import paddle
176
        import importlib.util
177

178 179
        cur_dir = os.path.dirname(os.path.abspath(__file__))
        so_path = os.path.join(cur_dir, "{resource}")
180

181 182
        def __bootstrap__():
            assert os.path.exists(so_path)
183 184 185 186 187 188 189 190 191 192 193 194
            if os.name == 'nt' or sys.platform.startswith('darwin'):
                # Cpp Extension only support Linux now
                mod = types.ModuleType(__name__)
            else:
                try:
                    spec = importlib.util.spec_from_file_location(__name__, so_path)
                    assert spec is not None
                    mod = importlib.util.module_from_spec(spec)
                    assert isinstance(spec.loader, importlib.abc.Loader)
                    spec.loader.exec_module(mod)
                except ImportError:
                    mod = types.ModuleType(__name__)
195 196

            # load custom op shared library with abs path
197 198 199
            custom_ops = paddle.utils.cpp_extension.load_op_meta_info_and_register_op(so_path)
            for custom_ops in custom_ops:
                setattr(mod, custom_ops, eval(custom_ops))
200

201 202
        __bootstrap__()

203 204
        """
    ).lstrip()
205 206 207 208 209 210

    # NOTE: To avoid importing .so file instead of python file because they have same name,
    # we rename .so shared library to another name, see EasyInstallCommand.
    filename, ext = os.path.splitext(resource)
    resource = filename + "_pd_" + ext

211
    api_content = []
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    if CustomOpInfo.instance().empty():
        print("Received len(custom_op) =  0, using cpp extension only")
    else:
        # Parse registering op information
        _, op_info = CustomOpInfo.instance().last()
        so_path = op_info.so_path

        new_custom_ops = load_op_meta_info_and_register_op(so_path)
        for op_name in new_custom_ops:
            api_content.append(_custom_api_content(op_name))
        print(
            "Received len(custom_op) =  %d, using custom operator"
            % len(new_custom_ops)
        )
226

227 228
    with open(pyfile, 'w') as f:
        f.write(
229 230 231 232
            _stub_template.format(
                resource=resource, custom_api='\n\n'.join(api_content)
            )
        )
233 234


235
OpInfo = collections.namedtuple('OpInfo', ['so_name', 'so_path'])
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250


class CustomOpInfo:
    """
    A global Singleton map to record all compiled custom ops information.
    """

    @classmethod
    def instance(cls):
        if not hasattr(cls, '_instance'):
            cls._instance = cls()
        return cls._instance

    def __init__(self):
        assert not hasattr(
251 252
            self.__class__, '_instance'
        ), 'Please use `instance()` to get CustomOpInfo object!'
253 254 255
        # NOTE(Aurelius84): Use OrderedDict to save more order information
        self.op_info_map = collections.OrderedDict()

256 257
    def add(self, op_name, so_name, so_path=None):
        self.op_info_map[op_name] = OpInfo(so_name, so_path)
258 259 260

    def last(self):
        """
H
HongyuJia 已提交
261
        Return the last inserted custom op info.
262 263 264 265
        """
        assert len(self.op_info_map) > 0
        return next(reversed(self.op_info_map.items()))

266 267 268 269 270
    def empty(self):
        if self.op_info_map:
            return False
        return True

271

272 273 274 275 276 277 278 279 280 281 282 283 284
VersionFields = collections.namedtuple(
    'VersionFields',
    [
        'sources',
        'extra_compile_args',
        'extra_link_args',
        'library_dirs',
        'runtime_library_dirs',
        'include_dirs',
        'define_macros',
        'undef_macros',
    ],
)
285 286 287 288 289 290 291 292


class VersionManager:
    def __init__(self, version_field):
        self.version_field = version_field
        self.version = self.hasher(version_field)

    def hasher(self, version_field):
293
        from paddle.utils import flatten
294 295 296 297

        md5 = hashlib.md5()
        for field in version_field._fields:
            elem = getattr(version_field, field)
298 299
            if not elem:
                continue
300 301 302 303 304
            if isinstance(elem, (list, tuple, dict)):
                flat_elem = flatten(elem)
                md5 = combine_hash(md5, tuple(flat_elem))
            else:
                raise RuntimeError(
305 306 307 308
                    "Support types with list, tuple and dict, but received {} with {}.".format(
                        type(elem), elem
                    )
                )
309 310 311 312 313 314 315 316 317 318 319

        return md5.hexdigest()

    @property
    def details(self):
        return self.version_field._asdict()


def combine_hash(md5, value):
    """
    Return new hash value.
320
    DO NOT use `hash()` because it doesn't generate stable value between different process.
321 322 323 324 325 326 327 328
    See https://stackoverflow.com/questions/27522626/hash-function-in-python-3-3-returns-different-results-between-sessions
    """
    md5.update(repr(value).encode())
    return md5


def clean_object_if_change_cflags(so_path, extension):
    """
329
    If already compiling source before, we should check whether cflags
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    have changed and delete the built object to re-compile the source
    even though source file content keeps unchanaged.
    """

    def serialize(path, version_info):
        assert isinstance(version_info, dict)
        with open(path, 'w') as f:
            f.write(json.dumps(version_info, indent=4, sort_keys=True))

    def deserialize(path):
        assert os.path.exists(path)
        with open(path, 'r') as f:
            content = f.read()
            return json.loads(content)

    # version file
    VERSION_FILE = "version.txt"
    base_dir = os.path.dirname(so_path)
    so_name = os.path.basename(so_path)
    version_file = os.path.join(base_dir, VERSION_FILE)

    # version info
    args = [getattr(extension, field, None) for field in VersionFields._fields]
    version_field = VersionFields._make(args)
    versioner = VersionManager(version_field)

    if os.path.exists(so_path) and os.path.exists(version_file):
        old_version_info = deserialize(version_file)
        so_version = old_version_info.get(so_name, None)
359
        # delete shared library file if version is changed to re-compile it.
360 361
        if so_version is not None and so_version != versioner.version:
            log_v(
362 363 364 365
                "Re-Compiling {}, because specified cflags have been changed. New signature {} has been saved into {}.".format(
                    so_name, versioner.version, version_file
                )
            )
366
            os.remove(so_path)
367
            # update new version information
368 369 370 371 372 373 374 375 376 377 378 379
            new_version_info = versioner.details
            new_version_info[so_name] = versioner.version
            serialize(version_file, new_version_info)
    else:
        # If compile at first time, save compiling detail information for debug.
        if not os.path.exists(base_dir):
            os.makedirs(base_dir)
        details = versioner.details
        details[so_name] = versioner.version
        serialize(version_file, details)


380
def prepare_unix_cudaflags(cflags):
381 382 383
    """
    Prepare all necessary compiled flags for nvcc compiling CUDA files.
    """
384
    if core.is_compiled_with_rocm():
385 386 387 388 389 390
        cflags = (
            COMMON_HIPCC_FLAGS
            + ['-Xcompiler', '-fPIC']
            + cflags
            + get_rocm_arch_flags(cflags)
        )
391
    else:
392 393 394 395 396 397 398 399 400 401 402 403 404
        cflags = (
            COMMON_NVCC_FLAGS
            + [
                '-ccbin',
                'cc',
                '-Xcompiler',
                '-fPIC',
                '--expt-relaxed-constexpr',
                '-DNVCC',
            ]
            + cflags
            + get_cuda_arch_flags(cflags)
        )
405 406 407 408

    return cflags


409
def prepare_win_cudaflags(cflags):
410 411 412
    """
    Prepare all necessary compiled flags for nvcc compiling CUDA files.
    """
413
    cflags = COMMON_NVCC_FLAGS + ['-w'] + cflags + get_cuda_arch_flags(cflags)
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

    return cflags


def add_std_without_repeat(cflags, compiler_type, use_std14=False):
    """
    Append -std=c++11/14 in cflags if without specific it before.
    """
    cpp_flag_prefix = '/std:' if compiler_type == 'msvc' else '-std='
    if not any(cpp_flag_prefix in flag for flag in cflags):
        suffix = 'c++14' if use_std14 else 'c++11'
        cpp_flag = cpp_flag_prefix + suffix
        cflags.append(cpp_flag)


def get_cuda_arch_flags(cflags):
    """
    For an arch, say "6.1", the added compile flag will be
    ``-gencode=arch=compute_61,code=sm_61``.
    For an added "+PTX", an additional
    ``-gencode=arch=compute_xx,code=compute_xx`` is added.
    """
    # TODO(Aurelius84):
    return []


440 441 442 443 444 445 446 447
def get_rocm_arch_flags(cflags):
    """
    For ROCm platform, amdgpu target should be added for HIPCC.
    """
    cflags = cflags + ['-fno-gpu-rdc', '-amdgpu-target=gfx906']
    return cflags


448 449 450 451 452
def _get_fluid_path():
    """
    Return installed fluid dir path.
    """
    import paddle
453

454 455 456 457 458 459 460
    return os.path.join(os.path.dirname(paddle.__file__), 'fluid')


def _get_core_name():
    """
    Return pybind DSO module name.
    """
461
    ext_name = '.pyd' if IS_WINDOWS else '.so'
462
    return 'libpaddle' + ext_name
463 464 465 466 467 468 469 470 471 472 473


def _get_lib_core_path():
    """
    Return real path of libcore_(no)avx.dylib on MacOS.
    """
    raw_core_name = _get_core_name()
    lib_core_name = "lib{}.dylib".format(raw_core_name[:-3])
    return os.path.join(_get_fluid_path(), lib_core_name)


474 475 476 477 478
def _get_dll_core_path():
    """
    Return real path of libcore_(no)avx.dylib on Windows.
    """
    raw_core_name = _get_core_name()
479
    dll_core_name = "libpaddle.dll"
480 481 482
    return os.path.join(_get_fluid_path(), dll_core_name)


483 484
def _reset_so_rpath(so_path):
    """
485
    NOTE(Aurelius84): Runtime path of libpaddle.so is modified into `@loader_path/../libs`
486 487 488 489 490 491 492 493 494 495 496
    in setup.py.in. While loading custom op, `@loader_path` is the dirname of custom op
    instead of `paddle/fluid`. So we modify `@loader_path` from custom dylib into `@rpath`
    to ensure dynamic loader find it correctly.

    Moreover, we will add `-rpath site-packages/paddle/fluid` while linking the dylib so
    that we don't need to set `LD_LIBRARY_PATH` any more.
    """
    assert os.path.exists(so_path)
    if OS_NAME.startswith("darwin"):
        origin_runtime_path = "@loader_path/../libs/"
        rpath = "@rpath/{}".format(_get_core_name())
497
        cmd = 'install_name_tool -change {} {} {}'.format(
498 499
            origin_runtime_path, rpath, so_path
        )
500 501 502 503

        run_cmd(cmd)


504 505 506 507 508 509 510 511
def _get_include_dirs_when_compiling(compile_dir):
    """
    Get all include directories when compiling the PaddlePaddle
    source code.
    """
    include_dirs_file = 'includes.txt'
    path = os.path.abspath(compile_dir)
    include_dirs_file = os.path.join(path, include_dirs_file)
512
    assert os.path.isfile(include_dirs_file), "File {} does not exist".format(
513 514
        include_dirs_file
    )
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    with open(include_dirs_file, 'r') as f:
        include_dirs = [line.strip() for line in f.readlines() if line.strip()]

    extra_dirs = ['paddle/fluid/platform']
    all_include_dirs = list(include_dirs)
    for extra_dir in extra_dirs:
        for include_dir in include_dirs:
            d = os.path.join(include_dir, extra_dir)
            if os.path.isdir(d):
                all_include_dirs.append(d)
    all_include_dirs.append(path)
    all_include_dirs.sort()
    return all_include_dirs


530
def normalize_extension_kwargs(kwargs, use_cuda=False):
531
    """
532 533 534
    Normalize include_dirs, library_dir and other attributes in kwargs.
    """
    assert isinstance(kwargs, dict)
535
    compile_include_dirs = []
536 537 538 539 540
    # NOTE: the "_compile_dir" argument is not public to users. It is only
    # reserved for internal usage. We do not guarantee that this argument
    # is always valid in the future release versions.
    compile_dir = kwargs.get("_compile_dir", None)
    if compile_dir:
541
        compile_include_dirs = _get_include_dirs_when_compiling(compile_dir)
542

543
    # append necessary include dir path of paddle
544 545
    include_dirs = list(kwargs.get('include_dirs', []))
    include_dirs.extend(compile_include_dirs)
546
    include_dirs.extend(find_paddle_includes(use_cuda))
547
    include_dirs.extend(find_python_includes())
548

549 550 551 552 553 554 555
    kwargs['include_dirs'] = include_dirs

    # append necessary lib path of paddle
    library_dirs = kwargs.get('library_dirs', [])
    library_dirs.extend(find_paddle_libraries(use_cuda))
    kwargs['library_dirs'] = library_dirs

556 557 558 559 560 561 562
    # append compile flags and check settings of compiler
    extra_compile_args = kwargs.get('extra_compile_args', [])
    if isinstance(extra_compile_args, dict):
        for compiler in ['cxx', 'nvcc']:
            if compiler not in extra_compile_args:
                extra_compile_args[compiler] = []

563 564 565 566 567 568
    if IS_WINDOWS:
        # TODO(zhouwei): may append compile flags in future
        pass
        # append link flags
        extra_link_args = kwargs.get('extra_link_args', [])
        extra_link_args.extend(MSVC_LINK_FLAGS)
569 570
        lib_core_name = create_sym_link_if_not_exist()
        extra_link_args.append('{}'.format(lib_core_name))
571 572
        if use_cuda:
            extra_link_args.extend(['cudadevrt.lib', 'cudart_static.lib'])
573
        kwargs['extra_link_args'] = extra_link_args
574

575
    else:
576
        # ----------------------- Linux Platform ----------------------- #
577 578 579 580 581
        extra_link_args = kwargs.get('extra_link_args', [])
        # On Linux, GCC support '-l:xxx.so' to specify the library name
        # without `lib` prefix.
        if OS_NAME.startswith('linux'):
            extra_link_args.append('-l:{}'.format(_get_core_name()))
582
        # ----------------------- MacOS Platform ----------------------- #
583 584 585 586
        else:
            # See _reset_so_rpath for details.
            extra_link_args.append('-Wl,-rpath,{}'.format(_get_fluid_path()))
            # On MacOS, ld don't support `-l:xx`, so we create a
587
            # liblibpaddle.dylib symbol link.
588 589
            lib_core_name = create_sym_link_if_not_exist()
            extra_link_args.append('-l{}'.format(lib_core_name))
590
        # -----------------------   -- END --    ----------------------- #
591

592
        add_compile_flag(extra_compile_args, ['-w'])  # disable warning
593

594
        if use_cuda:
595 596 597 598
            if core.is_compiled_with_rocm():
                extra_link_args.append('-lamdhip64')
            else:
                extra_link_args.append('-lcudart')
599

600
        kwargs['extra_link_args'] = extra_link_args
601

602 603 604 605
        # add runtime library dirs
        runtime_library_dirs = kwargs.get('runtime_library_dirs', [])
        runtime_library_dirs.extend(find_paddle_libraries(use_cuda))
        kwargs['runtime_library_dirs'] = runtime_library_dirs
606

607 608 609
    if compile_dir is None:
        # Add this compile option to isolate fluid headers
        add_compile_flag(extra_compile_args, ['-DPADDLE_WITH_CUSTOM_KERNEL'])
610 611
    kwargs['extra_compile_args'] = extra_compile_args

612 613 614 615
    kwargs['language'] = 'c++'
    return kwargs


616 617
def create_sym_link_if_not_exist():
    """
618
    Create soft symbol link of `libpaddle.so`
619
    """
620
    assert OS_NAME.startswith('darwin') or IS_WINDOWS
621 622 623

    raw_core_name = _get_core_name()
    core_path = os.path.join(_get_fluid_path(), raw_core_name)
624 625 626 627 628 629 630 631 632
    if IS_WINDOWS:
        new_dll_core_path = _get_dll_core_path()
        # create symbol link on windows
        if not os.path.exists(new_dll_core_path):
            try:
                os.symlink(core_path, new_dll_core_path)
            except Exception:
                warnings.warn(
                    "Failed to create soft symbol link for {}.\n You can run prompt as administrator and execute the "
633 634 635 636 637 638 639
                    "following command manually: `mklink {} {}`. Now it will create hard link for {} trickly.".format(
                        raw_core_name,
                        new_dll_core_path,
                        core_path,
                        raw_core_name,
                    )
                )
640
                run_cmd('mklink /H {} {}'.format(new_dll_core_path, core_path))
641
        # libpaddle with lib suffix
642 643
        assert os.path.exists(new_dll_core_path)
        return raw_core_name[:-4] + ".lib"
644

645 646 647 648 649 650 651 652 653
    else:
        new_lib_core_path = _get_lib_core_path()
        # create symbol link on mac
        if not os.path.exists(new_lib_core_path):
            try:
                os.symlink(core_path, new_lib_core_path)
                assert os.path.exists(new_lib_core_path)
            except Exception:
                raise RuntimeError(
654 655 656 657
                    "Failed to create soft symbol link for {}.\n Please execute the following command manually: `ln -s {} {}`".format(
                        raw_core_name, core_path, new_lib_core_path
                    )
                )
658

659
        # libpaddle without suffix
660
        return raw_core_name[:-3]
661 662


663 664 665 666 667 668 669 670 671 672 673 674
def find_cuda_home():
    """
    Use heuristic method to find cuda path
    """
    # step 1. find in $CUDA_HOME or $CUDA_PATH
    cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')

    # step 2.  find path by `which nvcc`
    if cuda_home is None:
        which_cmd = 'where' if IS_WINDOWS else 'which'
        try:
            with open(os.devnull, 'w') as devnull:
675 676 677
                nvcc_path = subprocess.check_output(
                    [which_cmd, 'nvcc'], stderr=devnull
                )
T
tianshuo78520a 已提交
678
                nvcc_path = nvcc_path.decode()
679 680
                # Multi CUDA, select the first
                nvcc_path = nvcc_path.split('\r\n')[0]
681

682 683 684 685 686 687
                # for example: /usr/local/cuda/bin/nvcc
                cuda_home = os.path.dirname(os.path.dirname(nvcc_path))
        except:
            if IS_WINDOWS:
                # search from default NVIDIA GPU path
                candidate_paths = glob.glob(
688 689
                    'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v*.*'
                )
690 691 692 693 694
                if len(candidate_paths) > 0:
                    cuda_home = candidate_paths[0]
            else:
                cuda_home = "/usr/local/cuda"
    # step 3. check whether path is valid
695 696 697 698 699
    if (
        cuda_home
        and not os.path.exists(cuda_home)
        and core.is_compiled_with_cuda()
    ):
700 701 702 703 704
        cuda_home = None

    return cuda_home


705 706 707 708 709 710 711 712 713 714 715 716
def find_rocm_home():
    """
    Use heuristic method to find rocm path
    """
    # step 1. find in $ROCM_HOME or $ROCM_PATH
    rocm_home = os.environ.get('ROCM_HOME') or os.environ.get('ROCM_PATH')

    # step 2.  find path by `which nvcc`
    if rocm_home is None:
        which_cmd = 'where' if IS_WINDOWS else 'which'
        try:
            with open(os.devnull, 'w') as devnull:
717 718 719
                hipcc_path = subprocess.check_output(
                    [which_cmd, 'hipcc'], stderr=devnull
                )
T
tianshuo78520a 已提交
720
                hipcc_path = hipcc_path.decode()
721 722 723 724 725 726 727
                hipcc_path = hipcc_path.rstrip('\r\n')

                # for example: /opt/rocm/bin/hipcc
                rocm_home = os.path.dirname(os.path.dirname(hipcc_path))
        except:
            rocm_home = "/opt/rocm"
    # step 3. check whether path is valid
728 729 730 731 732
    if (
        rocm_home
        and not os.path.exists(rocm_home)
        and core.is_compiled_with_rocm()
    ):
733 734 735 736 737
        rocm_home = None

    return rocm_home


738 739 740 741 742 743 744 745 746 747 748 749 750
def find_cuda_includes():
    """
    Use heuristic method to find cuda include path
    """
    cuda_home = find_cuda_home()
    if cuda_home is None:
        raise ValueError(
            "Not found CUDA runtime, please use `export CUDA_HOME=XXX` to specific it."
        )

    return [os.path.join(cuda_home, 'include')]


751 752 753 754 755 756 757 758 759 760 761 762 763
def find_rocm_includes():
    """
    Use heuristic method to find rocm include path
    """
    rocm_home = find_rocm_home()
    if rocm_home is None:
        raise ValueError(
            "Not found ROCM runtime, please use `export ROCM_PATH= XXX` to specific it."
        )

    return [os.path.join(rocm_home, 'include')]


764 765 766 767 768 769 770 771 772
def find_paddle_includes(use_cuda=False):
    """
    Return Paddle necessary include dir path.
    """
    # pythonXX/site-packages/paddle/include
    paddle_include_dir = get_include()
    third_party_dir = os.path.join(paddle_include_dir, 'third_party')
    include_dirs = [paddle_include_dir, third_party_dir]

773
    if use_cuda:
774 775 776 777 778 779
        if core.is_compiled_with_rocm():
            rocm_include_dir = find_rocm_includes()
            include_dirs.extend(rocm_include_dir)
        else:
            cuda_include_dir = find_cuda_includes()
            include_dirs.extend(cuda_include_dir)
780

781 782
    if OS_NAME.startswith('darwin'):
        # NOTE(Aurelius84): Ensure to find std v1 headers correctly.
783 784 785
        std_v1_includes = find_clang_cpp_include()
        if std_v1_includes is not None and os.path.exists(std_v1_includes):
            include_dirs.append(std_v1_includes)
786

787 788 789
    return include_dirs


790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
def find_python_includes():
    """
    Return necessary include dir path of Python.h.
    """
    # sysconfig.get_path('include') gives us the location of Python.h
    # Explicitly specify 'posix_prefix' scheme on non-Windows platforms to workaround error on some MacOS
    # installations where default `get_path` points to non-existing `/Library/Python/M.m/include` folder
    python_include_path = sysconfig.get_path(
        'include', scheme='nt' if IS_WINDOWS else 'posix_prefix'
    )
    if python_include_path is not None:
        assert isinstance(python_include_path, str)
        return [python_include_path]
    return []


806 807 808 809
def find_clang_cpp_include(compiler='clang'):
    std_v1_includes = None
    try:
        compiler_version = subprocess.check_output([compiler, "--version"])
T
tianshuo78520a 已提交
810
        compiler_version = compiler_version.decode()
811 812 813 814 815
        infos = compiler_version.split("\n")
        for info in infos:
            if "InstalledDir" in info:
                v1_path = info.split(':')[-1].strip()
                if v1_path and os.path.exists(v1_path):
816 817 818
                    std_v1_includes = os.path.join(
                        os.path.dirname(v1_path), 'include/c++/v1'
                    )
819 820 821 822 823 824 825 826
    except Exception:
        # Just raise warnings because the include dir is not required.
        warnings.warn(
            "Failed to search `include/c++/v1/` include dirs. Don't worry because it's not required."
        )
    return std_v1_includes


827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
def find_cuda_libraries():
    """
    Use heuristic method to find cuda static lib path
    """
    cuda_home = find_cuda_home()
    if cuda_home is None:
        raise ValueError(
            "Not found CUDA runtime, please use `export CUDA_HOME=XXX` to specific it."
        )
    if IS_WINDOWS:
        cuda_lib_dir = [os.path.join(cuda_home, 'lib', 'x64')]
    else:
        cuda_lib_dir = [os.path.join(cuda_home, 'lib64')]

    return cuda_lib_dir


844 845 846 847 848 849 850 851 852 853 854 855 856 857
def find_rocm_libraries():
    """
    Use heuristic method to find rocm dynamic lib path
    """
    rocm_home = find_rocm_home()
    if rocm_home is None:
        raise ValueError(
            "Not found ROCM runtime, please use `export ROCM_PATH=XXX` to specific it."
        )
    rocm_lib_dir = [os.path.join(rocm_home, 'lib')]

    return rocm_lib_dir


858 859 860 861 862 863
def find_paddle_libraries(use_cuda=False):
    """
    Return Paddle necessary library dir path.
    """
    # pythonXX/site-packages/paddle/libs
    paddle_lib_dirs = [get_lib()]
864

865
    if use_cuda:
866 867 868 869 870 871
        if core.is_compiled_with_rocm():
            rocm_lib_dir = find_rocm_libraries()
            paddle_lib_dirs.extend(rocm_lib_dir)
        else:
            cuda_lib_dir = find_cuda_libraries()
            paddle_lib_dirs.extend(cuda_lib_dir)
872

873
    # add `paddle/fluid` to search `libpaddle.so`
874 875
    paddle_lib_dirs.append(_get_fluid_path())

876 877 878
    return paddle_lib_dirs


879 880
def add_compile_flag(extra_compile_args, flags):
    assert isinstance(flags, list)
881 882
    if isinstance(extra_compile_args, dict):
        for args in extra_compile_args.values():
883
            args.extend(flags)
884
    else:
885
        extra_compile_args.extend(flags)
886 887 888 889


def is_cuda_file(path):

890
    cuda_suffix = {'.cu'}
891 892 893 894 895
    items = os.path.splitext(path)
    assert len(items) > 1
    return items[-1] in cuda_suffix


896
def get_build_directory(verbose=False):
897
    """
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    Return paddle extension root directory to put shared library. It could be specified by
    ``export PADDLE_EXTENSION_DIR=XXX`` . If not set, ``~/.cache/paddle_extension`` will be used
    by default.

    Returns:
        The root directory of compiling customized operators.

    Examples:

    .. code-block:: python

        from paddle.utils.cpp_extension import get_build_directory

        build_dir = get_build_directory()
        print(build_dir)

914 915 916 917
    """
    root_extensions_directory = os.environ.get('PADDLE_EXTENSION_DIR')
    if root_extensions_directory is None:
        dir_name = "paddle_extensions"
918 919 920
        root_extensions_directory = os.path.join(
            os.path.expanduser('~/.cache'), dir_name
        )
921 922
        if IS_WINDOWS:
            root_extensions_directory = os.path.normpath(
923 924
                root_extensions_directory
            )
925

926
        log_v(
927 928 929 930 931
            "$PADDLE_EXTENSION_DIR is not set, using path: {} by default.".format(
                root_extensions_directory
            ),
            verbose,
        )
932 933 934 935 936 937 938 939 940 941 942 943 944 945

    if not os.path.exists(root_extensions_directory):
        os.makedirs(root_extensions_directory)

    return root_extensions_directory


def parse_op_info(op_name):
    """
    Parse input names and outpus detail information from registered custom op
    from OpInfoMap.
    """
    if op_name not in OpProtoHolder.instance().op_proto_map:
        raise ValueError(
946 947 948 949
            "Please load {} shared library file firstly by `paddle.utils.cpp_extension.load_op_meta_info_and_register_op(...)`".format(
                op_name
            )
        )
950 951 952
    op_proto = OpProtoHolder.instance().get_op_proto(op_name)

    in_names = [x.name for x in op_proto.inputs]
953 954 955
    attr_names = [
        x.name for x in op_proto.attrs if x.name not in DEFAULT_OP_ATTR_NAMES
    ]
956
    out_names = [x.name for x in op_proto.outputs]
957

958
    return in_names, attr_names, out_names
959 960


961
def _import_module_from_library(module_name, build_directory, verbose=False):
962
    """
963
    Load shared library and import it as callable python module.
964
    """
965 966
    if IS_WINDOWS:
        dynamic_suffix = '.pyd'
967 968
    elif OS_NAME.startswith('darwin'):
        dynamic_suffix = '.dylib'
969 970 971
    else:
        dynamic_suffix = '.so'
    ext_path = os.path.join(build_directory, module_name + dynamic_suffix)
972
    if not os.path.exists(ext_path):
973
        raise FileNotFoundError(
974 975
            "Extension path: {} does not exist.".format(ext_path)
        )
976 977

    # load custom op_info and kernels from .so shared library
978
    log_v('loading shared library from: {}'.format(ext_path), verbose)
979
    op_names = load_op_meta_info_and_register_op(ext_path)
980

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
    if os.name == 'nt' or sys.platform.startswith('darwin'):
        # Cpp Extension only support Linux now
        return _generate_python_module(
            module_name, op_names, build_directory, verbose
        )
    try:
        spec = importlib.util.spec_from_file_location(module_name, ext_path)
        assert spec is not None
        module = importlib.util.module_from_spec(spec)
        assert isinstance(spec.loader, importlib.abc.Loader)
        spec.loader.exec_module(module)
    except ImportError:
        log_v('using custom operator only')
        return _generate_python_module(
            module_name, op_names, build_directory, verbose
        )

998
    # generate Python api in ext_path
999
    op_module = _generate_python_module(
1000 1001
        module_name, op_names, build_directory, verbose
    )
1002 1003 1004 1005 1006
    for op_name in op_names:
        # Mix use of Cpp Extension and Custom Operator
        setattr(module, op_name, getattr(op_module, op_name))

    return module
1007 1008


1009 1010 1011
def _generate_python_module(
    module_name, op_names, build_directory, verbose=False
):
1012 1013 1014
    """
    Automatically generate python file to allow import or load into as module
    """
1015 1016 1017 1018 1019 1020 1021 1022

    def remove_if_exit(filepath):
        if os.path.exists(filepath):
            os.remove(filepath)

    # NOTE: Use unique id as suffix to avoid write same file at same time in
    # both multi-thread and multi-process.
    thread_id = str(threading.currentThread().ident)
1023 1024 1025
    api_file = os.path.join(
        build_directory, module_name + '_' + thread_id + '.py'
    )
1026
    log_v("generate api file: {}".format(api_file), verbose)
1027

1028
    # delete the temp file before exit python process
1029 1030
    atexit.register(lambda: remove_if_exit(api_file))

1031
    # write into .py file with RWLockc
1032
    api_content = [_custom_api_content(op_name) for op_name in op_names]
1033
    with open(api_file, 'w') as f:
1034
        f.write('\n\n'.join(api_content))
1035 1036

    # load module
1037
    custom_module = _load_module_from_file(api_file, module_name, verbose)
1038
    return custom_module
1039 1040


1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
def _gen_output_content(in_names, out_names, inplace_reverse_idx):
    # ' ' * tab space * tab number
    indent = ' ' * 4 * 2
    dynamic_content = ""
    static_content = ""
    for out_idx, out_name in enumerate(out_names):
        in_idx = -1
        if out_idx in inplace_reverse_idx:
            in_idx = inplace_reverse_idx[out_idx]
        if in_idx != -1 and "@VECTOR" in in_names[in_idx]:
            lower_in_names = in_names[in_idx].split("@")[0].lower()
            dynamic_content += f"""
{indent}outs['{out_name}'] = [core.eager.Tensor() for _ in range(len({lower_in_names}))]
{indent}ctx.add_outputs(outs['{out_name}'])"""
            static_content += f"""
{indent}outs['{out_name}'] = [helper.create_variable(dtype='float32') for _ in range(len({lower_in_names}))]"""
        else:
            dynamic_content += f"""
{indent}outs['{out_name}'] = core.eager.Tensor()
{indent}ctx.add_outputs(outs['{out_name}'])"""
            static_content += f"""
{indent}outs['{out_name}'] = helper.create_variable(dtype='float32')"""

    return dynamic_content, static_content


1067
def _custom_api_content(op_name):
1068
    (
1069 1070 1071 1072
        params_list,
        ins_map,
        attrs_map,
        outs_list,
1073
        in_names,
1074 1075 1076
        attr_names,
        out_names,
        inplace_reverse_idx,
1077
    ) = _get_api_inputs_str(op_name)
1078 1079 1080 1081
    dynamic_content, static_content = _gen_output_content(
        in_names, out_names, inplace_reverse_idx
    )
    lower_in_list = [p.split("@")[0].lower() for p in in_names]
1082 1083
    API_TEMPLATE = textwrap.dedent(
        """
1084 1085
        import paddle.fluid.core as core
        from paddle.fluid.core import VarBase, CustomOpKernelContext
姜永久 已提交
1086
        from paddle.fluid.framework import _dygraph_tracer, in_dygraph_mode
1087
        from paddle.fluid.layer_helper import LayerHelper
1088

1089
        def {op_name}({params_list}):
1090
            # prepare inputs and outputs
1091
            outs = {{}}
1092
            outs_list = {outs_list}
1093

1094 1095 1096
            # The output variable's dtype use default value 'float32',
            # and the actual dtype of output variable will be inferred in runtime.
            if in_dygraph_mode():
J
Jiabin Yang 已提交
1097 1098 1099 1100 1101
                ctx = CustomOpKernelContext()
                for i in {in_names}:
                    ctx.add_inputs(i)
                for j in {attr_names}:
                    ctx.add_attr(j)
1102
                {dynamic_content}
J
Jiabin Yang 已提交
1103 1104
                core.eager._run_custom_op(ctx, "{op_name}", True)
            else:
1105
                ins = {{}}
1106
                for key, value in dict({ins_map}).items():
1107 1108 1109
                    # handle optional inputs
                    if value is not None:
                        ins[key] = value
姜永久 已提交
1110
                helper = LayerHelper("{op_name}", **locals())
1111 1112
                {static_content}
                helper.append_op(type="{op_name}", inputs=ins, outputs=outs, attrs={attrs_map})
1113

1114
            res = [outs[out_name] for out_name in outs_list]
1115 1116

            return res[0] if len(res)==1 else res
1117 1118
            """
    ).lstrip()
1119 1120 1121

    # generate python api file
    api_content = API_TEMPLATE.format(
1122
        op_name=op_name,
1123 1124 1125
        params_list=params_list,
        ins_map=ins_map,
        attrs_map=attrs_map,
1126
        # "[x, y, z]""
1127 1128 1129 1130 1131
        in_names="[" + ",".join(lower_in_list) + "]",
        attr_names="[" + ",".join(attr_names) + "]",
        outs_list=outs_list,
        dynamic_content=dynamic_content,
        static_content=static_content,
1132
    )
1133 1134 1135 1136

    return api_content


1137
def _load_module_from_file(api_file_path, module_name, verbose=False):
1138 1139 1140 1141
    """
    Load module from python file.
    """
    if not os.path.exists(api_file_path):
1142
        raise FileNotFoundError(
1143 1144
            "File : {} does not exist.".format(api_file_path)
        )
1145 1146

    # Unique readable module name to place custom api.
1147
    log_v('import module from file: {}'.format(api_file_path), verbose)
1148 1149 1150
    ext_name = "_paddle_cpp_extension_" + module_name

    # load module with RWLock
T
tianshuo78520a 已提交
1151
    loader = machinery.SourceFileLoader(ext_name, api_file_path)
1152 1153 1154
    spec = importlib.util.spec_from_loader(loader.name, loader)
    module = importlib.util.module_from_spec(spec)
    loader.exec_module(module)
1155

1156
    return module
1157 1158 1159 1160 1161 1162


def _get_api_inputs_str(op_name):
    """
    Returns string of api parameters and inputs dict.
    """
1163
    in_names, attr_names, out_names = parse_op_info(op_name)
1164
    # e.g: x, y, z
1165
    param_names = in_names + attr_names
1166
    # NOTE(chenweihang): we add suffix `@VECTOR` for std::vector<Tensor> input,
1167
    # but the string contains `@` cannot used as argument name, so we split
1168
    # input name by `@`, and only use first substr as argument
1169
    params_list = ','.join([p.split("@")[0].lower() for p in param_names])
1170
    # e.g: {'X': x, 'Y': y, 'Z': z}
1171
    ins_map = "{%s}" % ','.join(
1172 1173 1174 1175 1176
        [
            "'{}' : {}".format(in_name, in_name.split("@")[0].lower())
            for in_name in in_names
        ]
    )
1177
    # e.g: {'num': n}
1178
    attrs_map = "{%s}" % ",".join(
1179 1180 1181 1182 1183
        [
            "'{}' : {}".format(attr_name, attr_name.split("@")[0].lower())
            for attr_name in attr_names
        ]
    )
1184
    # e.g: ['Out', 'Index']
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
    outs_list = "[%s]" % ','.join(["'{}'".format(name) for name in out_names])

    inplace_reverse_idx = core.eager._get_custom_operator_inplace_map(op_name)

    return (
        params_list,
        ins_map,
        attrs_map,
        outs_list,
        in_names,
        attr_names,
        out_names,
        inplace_reverse_idx,
    )
1199 1200


1201 1202 1203 1204 1205 1206
def _write_setup_file(
    name,
    sources,
    file_path,
    build_dir,
    include_dirs,
1207
    library_dirs,
1208 1209 1210 1211 1212
    extra_cxx_cflags,
    extra_cuda_cflags,
    link_args,
    verbose=False,
):
1213 1214 1215
    """
    Automatically generate setup.py and write it into build directory.
    """
1216 1217
    template = textwrap.dedent(
        """
1218 1219 1220
    import os
    from paddle.utils.cpp_extension import CppExtension, CUDAExtension, BuildExtension, setup
    from paddle.utils.cpp_extension import get_build_directory
1221 1222


1223 1224 1225 1226 1227 1228
    setup(
        name='{name}',
        ext_modules=[
            {prefix}Extension(
                sources={sources},
                include_dirs={include_dirs},
1229
                library_dirs={library_dirs},
1230
                extra_compile_args={{'cxx':{extra_cxx_cflags}, 'nvcc':{extra_cuda_cflags}}},
1231 1232
                extra_link_args={extra_link_args})],
        cmdclass={{"build_ext" : BuildExtension.with_options(
1233 1234
            output_dir=r'{build_dir}',
            no_python_abi_suffix=True)
1235 1236
        }})"""
    ).lstrip()
1237 1238 1239 1240

    with_cuda = False
    if any([is_cuda_file(source) for source in sources]):
        with_cuda = True
1241
    log_v("with_cuda: {}".format(with_cuda), verbose)
1242

1243 1244 1245 1246 1247
    content = template.format(
        name=name,
        prefix='CUDA' if with_cuda else 'Cpp',
        sources=list2str(sources),
        include_dirs=list2str(include_dirs),
1248
        library_dirs=list2str(library_dirs),
1249 1250 1251 1252 1253
        extra_cxx_cflags=list2str(extra_cxx_cflags),
        extra_cuda_cflags=list2str(extra_cuda_cflags),
        extra_link_args=list2str(link_args),
        build_dir=build_dir,
    )
1254 1255

    log_v('write setup.py into {}'.format(file_path), verbose)
1256 1257 1258 1259 1260 1261
    with open(file_path, 'w') as f:
        f.write(content)


def list2str(args):
    """
1262
    Convert list[str] into string. For example: ['x', 'y'] -> "['x', 'y']"
1263
    """
1264 1265
    if args is None:
        return '[]'
1266
    assert isinstance(args, (list, tuple))
1267 1268
    args = ["{}".format(arg) for arg in args]
    return repr(args)
1269 1270


1271
def _jit_compile(file_path, verbose=False):
1272 1273 1274 1275 1276
    """
    Build shared library in subprocess
    """
    ext_dir = os.path.dirname(file_path)
    setup_file = os.path.basename(file_path)
1277

1278 1279 1280
    # Using interpreter same with current process.
    interpreter = sys.executable

1281 1282
    try:
        py_version = subprocess.check_output([interpreter, '-V'])
T
tianshuo78520a 已提交
1283
        py_version = py_version.decode()
1284 1285
        log_v(
            "Using Python interpreter: {}, version: {}".format(
1286 1287 1288 1289
                interpreter, py_version.strip()
            ),
            verbose,
        )
1290 1291 1292 1293
    except Exception:
        _, error, _ = sys.exc_info()
        raise RuntimeError(
            'Failed to check Python interpreter with `{}`, errors: {}'.format(
1294 1295 1296
                interpreter, error
            )
        )
1297

1298
    if IS_WINDOWS:
1299 1300 1301
        compile_cmd = 'cd /d {} && {} {} build'.format(
            ext_dir, interpreter, setup_file
        )
1302
    else:
1303 1304 1305
        compile_cmd = 'cd {} && {} {} build'.format(
            ext_dir, interpreter, setup_file
        )
1306

1307 1308
    print("Compiling user custom op, it will cost a few seconds.....")
    run_cmd(compile_cmd, verbose)
1309 1310 1311 1312 1313 1314 1315 1316


def parse_op_name_from(sources):
    """
    Parse registerring custom op name from sources.
    """

    def regex(content):
1317
        pattern = re.compile(r'PD_BUILD_OP\(([^,\)]+)\)')
1318 1319
        content = re.sub(r'\s|\t|\n', '', content)
        op_name = pattern.findall(content)
1320
        op_name = {re.sub('_grad', '', name) for name in op_name}
1321 1322 1323 1324 1325 1326 1327 1328 1329

        return op_name

    op_names = set()
    for source in sources:
        with open(source, 'r') as f:
            content = f.read()
            op_names |= regex(content)

1330
    return list(op_names)
1331 1332


1333
def run_cmd(command, verbose=False):
1334 1335 1336
    """
    Execute command with subprocess.
    """
1337 1338 1339 1340 1341 1342
    # logging
    log_v("execute command: {}".format(command), verbose)

    # execute command
    try:
        if verbose:
1343 1344 1345
            return subprocess.check_call(
                command, shell=True, stderr=subprocess.STDOUT
            )
1346 1347 1348 1349
        else:
            return subprocess.check_call(command, shell=True, stdout=DEVNULL)
    except Exception:
        _, error, _ = sys.exc_info()
1350 1351 1352
        raise RuntimeError(
            "Failed to run command: {}, errors: {}".format(compile, error)
        )
1353 1354 1355 1356 1357 1358 1359


def check_abi_compatibility(compiler, verbose=False):
    """
    Check whether GCC version on user local machine is compatible with Paddle in
    site-packages.
    """
1360
    if os.environ.get('PADDLE_SKIP_CHECK_ABI') in ['True', 'true', '1']:
1361 1362
        return True

1363
    if not IS_WINDOWS:
1364 1365 1366
        cmd_out = subprocess.check_output(
            ['which', compiler], stderr=subprocess.STDOUT
        )
T
tianshuo78520a 已提交
1367
        compiler_path = os.path.realpath(cmd_out.decode()).strip()
1368
        # if not found any suitable compiler, raise warning
1369 1370 1371 1372
        if not any(
            name in compiler_path
            for name in _expected_compiler_current_platform()
        ):
1373 1374 1375 1376
            warnings.warn(
                WRONG_COMPILER_WARNING.format(
                    user_compiler=compiler,
                    paddle_compiler=_expected_compiler_current_platform()[0],
1377 1378 1379
                    platform=OS_NAME,
                )
            )
1380
            return False
1381

1382
    version = (0, 0, 0)
1383 1384 1385 1386 1387
    # clang++ have no ABI compatibility problem
    if OS_NAME.startswith('darwin'):
        return True
    try:
        if OS_NAME.startswith('linux'):
1388
            mini_required_version = GCC_MINI_VERSION
1389
            version_info = subprocess.check_output(
1390 1391
                [compiler, '-dumpfullversion', '-dumpversion']
            )
T
tianshuo78520a 已提交
1392
            version_info = version_info.decode()
1393 1394
            version = version_info.strip().split('.')
        elif IS_WINDOWS:
1395
            mini_required_version = MSVC_MINI_VERSION
1396 1397 1398
            compiler_info = subprocess.check_output(
                compiler, stderr=subprocess.STDOUT
            )
T
tianshuo78520a 已提交
1399 1400 1401 1402
            try:
                compiler_info = compiler_info.decode('UTF-8')
            except UnicodeDecodeError:
                compiler_info = compiler_info.decode('gbk')
1403 1404 1405
            match = re.search(r'(\d+)\.(\d+)\.(\d+)', compiler_info.strip())
            if match is not None:
                version = match.groups()
1406
    except Exception:
1407
        # check compiler version failed
1408
        _, error, _ = sys.exc_info()
1409 1410 1411 1412 1413
        warnings.warn(
            'Failed to check compiler version for {}: {}'.format(
                compiler, error
            )
        )
1414
        return False
1415

1416 1417 1418 1419 1420
    # check version compatibility
    assert len(version) == 3
    if tuple(map(int, version)) >= mini_required_version:
        return True
    warnings.warn(
1421 1422 1423 1424
        ABI_INCOMPATIBILITY_WARNING.format(
            user_compiler=compiler, version='.'.join(version)
        )
    )
1425 1426 1427 1428 1429 1430 1431
    return False


def _expected_compiler_current_platform():
    """
    Returns supported compiler string on current platform
    """
1432 1433 1434 1435 1436 1437
    if OS_NAME.startswith('darwin'):
        expect_compilers = ['clang', 'clang++']
    elif OS_NAME.startswith('linux'):
        expect_compilers = ['gcc', 'g++', 'gnu-c++', 'gnu-cc']
    elif IS_WINDOWS:
        expect_compilers = ['cl']
1438 1439 1440
    return expect_compilers


1441
def log_v(info, verbose=True):
1442 1443 1444 1445
    """
    Print log information on stdout.
    """
    if verbose:
1446
        logger.info(info)