Skip to content

Commit fdb169b

Browse files
Aurelius84chenwhql
authored andcommitted
[CustomOp] Split build directory for each setup.py (PaddlePaddle#31124)
* split build directory for each setup.py * fix template string
1 parent f94bab8 commit fdb169b

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

python/paddle/utils/cpp_extension/cpp_extension.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import setuptools
2323
from setuptools.command.easy_install import easy_install
2424
from setuptools.command.build_ext import build_ext
25+
from distutils.command.build import build
2526

2627
from .extension_utils import find_cuda_home, normalize_extension_kwargs, add_compile_flag, bootstrap_context
2728
from .extension_utils import is_cuda_file, prepare_unix_cudaflags, prepare_win_cudaflags, add_std_without_repeat, get_build_directory
@@ -103,6 +104,13 @@ def setup(**attr):
103104
assert 'easy_install' not in cmdclass
104105
cmdclass['easy_install'] = EasyInstallCommand
105106

107+
# Note(Aurelius84): Add rename build_base directory hook in build command.
108+
# To avoid using same build directory that will lead to remove the directory
109+
# by mistake while parallelling execute setup.py, for example on CI.
110+
assert 'build' not in cmdclass
111+
build_base = os.path.join('build', attr['name'])
112+
cmdclass['build'] = BuildCommand.with_options(build_base=build_base)
113+
106114
# Always set zip_safe=False to make compatible in PY2 and PY3
107115
# See http://peak.telecommunity.com/DevCenter/setuptools#setting-the-zip-safe-flag
108116
attr['zip_safe'] = False
@@ -491,6 +499,43 @@ def run(self, *args, **kwargs):
491499
assert os.path.exists(new_so_path)
492500

493501

502+
class BuildCommand(build, object):
503+
"""
504+
Extend build Command to control the behavior of specifying `build_base` root directory.
505+
506+
NOTE(Aurelius84): This is a hook subclass inherited Command used to specify customized
507+
build_base directory.
508+
"""
509+
510+
@classmethod
511+
def with_options(cls, **options):
512+
"""
513+
Returns a BuildCommand subclass containing use-defined options.
514+
"""
515+
516+
class cls_with_options(cls):
517+
def __init__(self, *args, **kwargs):
518+
kwargs.update(options)
519+
cls.__init__(self, *args, **kwargs)
520+
521+
return cls_with_options
522+
523+
def __init__(self, *args, **kwargs):
524+
# Note: shall put before super()
525+
self._specified_build_base = kwargs.get('build_base', None)
526+
527+
super(BuildCommand, self).__init__(*args, **kwargs)
528+
529+
def initialize_options(self):
530+
"""
531+
build_base is root directory for all sub-command, such as
532+
build_lib, build_temp. See `distutils.command.build` for details.
533+
"""
534+
super(BuildCommand, self).initialize_options()
535+
if self._specified_build_base is not None:
536+
self.build_base = self._specified_build_base
537+
538+
494539
def load(name,
495540
sources,
496541
extra_cflags=None,
@@ -569,11 +614,13 @@ def load(name,
569614
verbose)
570615

571616
# write setup.py file and compile it
572-
_write_setup_file(name, sources, file_path, extra_include_paths,
573-
compile_flags, extra_ldflags, verbose)
617+
build_base_dir = os.path.join(build_directory, name)
618+
_write_setup_file(name, sources, file_path, build_base_dir,
619+
extra_include_paths, compile_flags, extra_ldflags,
620+
verbose)
574621
_jit_compile(file_path, interpreter, verbose)
575622

576623
# import as callable python api
577-
custom_op_api = _import_module_from_library(name, build_directory, verbose)
624+
custom_op_api = _import_module_from_library(name, build_base_dir, verbose)
578625

579626
return custom_op_api

python/paddle/utils/cpp_extension/extension_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def _get_api_inputs_str(op_name):
580580
def _write_setup_file(name,
581581
sources,
582582
file_path,
583+
build_dir,
583584
include_dirs,
584585
compile_flags,
585586
link_args,
@@ -600,7 +601,7 @@ def _write_setup_file(name,
600601
extra_compile_args={extra_compile_args},
601602
extra_link_args={extra_link_args})],
602603
cmdclass={{"build_ext" : BuildExtension.with_options(
603-
output_dir=get_build_directory(),
604+
output_dir='{build_dir}',
604605
no_python_abi_suffix=True,
605606
use_new_method={use_new_method})
606607
}})""").lstrip()
@@ -617,6 +618,7 @@ def _write_setup_file(name,
617618
include_dirs=list2str(include_dirs),
618619
extra_compile_args=list2str(compile_flags),
619620
extra_link_args=list2str(link_args),
621+
build_dir=build_dir,
620622
use_new_method=use_new_custom_op_load_method())
621623

622624
log_v('write setup.py into {}'.format(file_path), verbose)

0 commit comments

Comments
 (0)