Replace setup.py with pyproject.toml (#9021)
* Create pyproject.toml * Implement a custom build backend (see below) in packager directory. Build logic from setup.py has been refactored and migrated into the new backend. * Tested: pip wheel . (build wheel), python -m build --sdist . (source distribution)
This commit is contained in:
committed by
GitHub
parent
a7b3dd3176
commit
a5cd2412de
@@ -1,56 +0,0 @@
|
||||
include README.rst
|
||||
include xgboost/LICENSE
|
||||
include xgboost/VERSION
|
||||
include xgboost/CMakeLists.txt
|
||||
|
||||
include xgboost/py.typed
|
||||
recursive-include xgboost *.py
|
||||
recursive-include xgboost/cmake *
|
||||
exclude xgboost/cmake/RPackageInstall.cmake.in
|
||||
exclude xgboost/cmake/RPackageInstallTargetSetup.cmake
|
||||
exclude xgboost/cmake/Sanitizer.cmake
|
||||
exclude xgboost/cmake/modules/FindASan.cmake
|
||||
exclude xgboost/cmake/modules/FindLSan.cmake
|
||||
exclude xgboost/cmake/modules/FindLibR.cmake
|
||||
exclude xgboost/cmake/modules/FindTSan.cmake
|
||||
exclude xgboost/cmake/modules/FindUBSan.cmake
|
||||
recursive-include xgboost/include *
|
||||
recursive-include xgboost/plugin *
|
||||
recursive-include xgboost/src *
|
||||
|
||||
recursive-include xgboost/gputreeshap/GPUTreeShap *
|
||||
|
||||
include xgboost/rabit/CMakeLists.txt
|
||||
recursive-include xgboost/rabit/include *
|
||||
recursive-include xgboost/rabit/src *
|
||||
prune xgboost/rabit/doc
|
||||
prune xgboost/rabit/guide
|
||||
|
||||
include xgboost/dmlc-core/CMakeLists.txt
|
||||
|
||||
recursive-include xgboost/dmlc-core/cmake *
|
||||
exclude xgboost/dmlc-core/cmake/gtest_cmake.in
|
||||
exclude xgboost/dmlc-core/cmake/lint.cmake
|
||||
exclude xgboost/dmlc-core/cmake/Sanitizer.cmake
|
||||
exclude xgboost/dmlc-core/cmake/Modules/FindASan.cmake
|
||||
exclude xgboost/dmlc-core/cmake/Modules/FindLSan.cmake
|
||||
exclude xgboost/dmlc-core/cmake/Modules/FindTSan.cmake
|
||||
exclude xgboost/dmlc-core/cmake/Modules/FindUBSan.cmake
|
||||
|
||||
recursive-include xgboost/dmlc-core/include *
|
||||
recursive-include xgboost/dmlc-core/include *
|
||||
recursive-include xgboost/dmlc-core/make *
|
||||
recursive-include xgboost/dmlc-core/src *
|
||||
include xgboost/dmlc-core/tracker/dmlc-submit
|
||||
recursive-include xgboost/dmlc-core/tracker/dmlc_tracker *.py
|
||||
include xgboost/dmlc-core/tracker/yarn/build.bat
|
||||
include xgboost/dmlc-core/tracker/yarn/build.sh
|
||||
include xgboost/dmlc-core/tracker/yarn/pom.xml
|
||||
recursive-include xgboost/dmlc-core/tracker/yarn/src *
|
||||
include xgboost/dmlc-core/windows/dmlc.sln
|
||||
include xgboost/dmlc-core/windows/dmlc/dmlc.vcxproj
|
||||
|
||||
prune xgboost/dmlc-core/doc
|
||||
prune xgboost/dmlc-core/scripts/
|
||||
|
||||
global-exclude *.py[oc]
|
||||
22
python-package/hatch_build.py
Normal file
22
python-package/hatch_build.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""
|
||||
Custom hook to customize the behavior of Hatchling.
|
||||
Here, we customize the tag of the generated wheels.
|
||||
"""
|
||||
import sysconfig
|
||||
from typing import Any, Dict
|
||||
|
||||
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
|
||||
|
||||
|
||||
def get_tag() -> str:
|
||||
"""Get appropriate wheel tag according to system"""
|
||||
tag_platform = sysconfig.get_platform().replace("-", "_").replace(".", "_")
|
||||
return f"py3-none-{tag_platform}"
|
||||
|
||||
|
||||
class CustomBuildHook(BuildHookInterface):
|
||||
"""A custom build hook"""
|
||||
|
||||
def initialize(self, version: str, build_data: Dict[str, Any]) -> None:
|
||||
"""This step ccurs immediately before each build."""
|
||||
build_data["tag"] = get_tag()
|
||||
0
python-package/packager/__init__.py
Normal file
0
python-package/packager/__init__.py
Normal file
56
python-package/packager/build_config.py
Normal file
56
python-package/packager/build_config.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""Build configuration"""
|
||||
import dataclasses
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class BuildConfiguration: # pylint: disable=R0902
|
||||
"""Configurations use when building libxgboost"""
|
||||
|
||||
# Whether to hide C++ symbols in libxgboost.so
|
||||
hide_cxx_symbols: bool = True
|
||||
# Whether to enable OpenMP
|
||||
use_openmp: bool = True
|
||||
# Whether to enable CUDA
|
||||
use_cuda: bool = False
|
||||
# Whether to enable NCCL
|
||||
use_nccl: bool = False
|
||||
# Whether to enable HDFS
|
||||
use_hdfs: bool = False
|
||||
# Whether to enable Azure Storage
|
||||
use_azure: bool = False
|
||||
# Whether to enable AWS S3
|
||||
use_s3: bool = False
|
||||
# Whether to enable the dense parser plugin
|
||||
plugin_dense_parser: bool = False
|
||||
# Special option: See explanation below
|
||||
use_system_libxgboost: bool = False
|
||||
|
||||
def _set_config_setting(
|
||||
self, config_settings: Dict[str, Any], field_name: str
|
||||
) -> None:
|
||||
if field_name in config_settings:
|
||||
setattr(
|
||||
self,
|
||||
field_name,
|
||||
(config_settings[field_name].lower() in ["true", "1", "on"]),
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"Field {field_name} is not a valid config_settings")
|
||||
|
||||
def update(self, config_settings: Optional[Dict[str, Any]]) -> None:
|
||||
"""Parse config_settings from Pip (or other PEP 517 frontend)"""
|
||||
if config_settings is not None:
|
||||
for field_name in [x.name for x in dataclasses.fields(self)]:
|
||||
self._set_config_setting(config_settings, field_name)
|
||||
|
||||
def get_cmake_args(self) -> List[str]:
|
||||
"""Convert build configuration to CMake args"""
|
||||
cmake_args = []
|
||||
for field_name in [x.name for x in dataclasses.fields(self)]:
|
||||
if field_name in ["use_system_libxgboost"]:
|
||||
continue
|
||||
cmake_option = field_name.upper()
|
||||
cmake_value = "ON" if getattr(self, field_name) is True else "OFF"
|
||||
cmake_args.append(f"-D{cmake_option}={cmake_value}")
|
||||
return cmake_args
|
||||
157
python-package/packager/nativelib.py
Normal file
157
python-package/packager/nativelib.py
Normal file
@@ -0,0 +1,157 @@
|
||||
"""
|
||||
Functions for building libxgboost
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from platform import system
|
||||
from typing import Optional
|
||||
|
||||
from .build_config import BuildConfiguration
|
||||
|
||||
|
||||
def _lib_name() -> str:
|
||||
"""Return platform dependent shared object name."""
|
||||
if system() in ["Linux", "OS400"] or system().upper().endswith("BSD"):
|
||||
name = "libxgboost.so"
|
||||
elif system() == "Darwin":
|
||||
name = "libxgboost.dylib"
|
||||
elif system() == "Windows":
|
||||
name = "xgboost.dll"
|
||||
else:
|
||||
raise NotImplementedError(f"System {system()} not supported")
|
||||
return name
|
||||
|
||||
|
||||
def build_libxgboost(
|
||||
cpp_src_dir: pathlib.Path,
|
||||
build_dir: pathlib.Path,
|
||||
build_config: BuildConfiguration,
|
||||
) -> pathlib.Path:
|
||||
"""Build libxgboost in a temporary directory and obtain the path to built libxgboost"""
|
||||
logger = logging.getLogger("xgboost.packager.build_libxgboost")
|
||||
|
||||
if not cpp_src_dir.is_dir():
|
||||
raise RuntimeError(f"Expected {cpp_src_dir} to be a directory")
|
||||
logger.info(
|
||||
"Building %s from the C++ source files in %s...", _lib_name(), str(cpp_src_dir)
|
||||
)
|
||||
|
||||
def _build(*, generator: str) -> None:
|
||||
cmake_cmd = [
|
||||
"cmake",
|
||||
str(cpp_src_dir),
|
||||
generator,
|
||||
"-DKEEP_BUILD_ARTIFACTS_IN_BINARY_DIR=ON",
|
||||
]
|
||||
cmake_cmd.extend(build_config.get_cmake_args())
|
||||
|
||||
# Flag for cross-compiling for Apple Silicon
|
||||
# We use environment variable because it's the only way to pass down custom flags
|
||||
# through the cibuildwheel package, which calls `pip wheel` command.
|
||||
if "CIBW_TARGET_OSX_ARM64" in os.environ:
|
||||
cmake_cmd.append("-DCMAKE_OSX_ARCHITECTURES=arm64")
|
||||
|
||||
logger.info("CMake args: %s", str(cmake_cmd))
|
||||
subprocess.check_call(cmake_cmd, cwd=build_dir)
|
||||
|
||||
if system() == "Windows":
|
||||
subprocess.check_call(
|
||||
["cmake", "--build", ".", "--config", "Release"], cwd=build_dir
|
||||
)
|
||||
else:
|
||||
nproc = os.cpu_count()
|
||||
assert build_tool is not None
|
||||
subprocess.check_call([build_tool, f"-j{nproc}"], cwd=build_dir)
|
||||
|
||||
if system() == "Windows":
|
||||
supported_generators = (
|
||||
"-GVisual Studio 17 2022",
|
||||
"-GVisual Studio 16 2019",
|
||||
"-GVisual Studio 15 2017",
|
||||
"-GMinGW Makefiles",
|
||||
)
|
||||
for generator in supported_generators:
|
||||
try:
|
||||
_build(generator=generator)
|
||||
logger.info(
|
||||
"Successfully built %s using generator %s", _lib_name(), generator
|
||||
)
|
||||
break
|
||||
except subprocess.CalledProcessError as e:
|
||||
logger.info(
|
||||
"Tried building with generator %s but failed with exception %s",
|
||||
generator,
|
||||
str(e),
|
||||
)
|
||||
# Empty build directory
|
||||
shutil.rmtree(build_dir)
|
||||
build_dir.mkdir()
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"None of the supported generators produced a successful build!"
|
||||
f"Supported generators: {supported_generators}"
|
||||
)
|
||||
else:
|
||||
build_tool = "ninja" if shutil.which("ninja") else "make"
|
||||
generator = "-GNinja" if build_tool == "ninja" else "-GUnix Makefiles"
|
||||
try:
|
||||
_build(generator=generator)
|
||||
except subprocess.CalledProcessError as e:
|
||||
logger.info("Failed to build with OpenMP. Exception: %s", str(e))
|
||||
build_config.use_openmp = False
|
||||
_build(generator=generator)
|
||||
|
||||
return build_dir / "lib" / _lib_name()
|
||||
|
||||
|
||||
def locate_local_libxgboost(
|
||||
toplevel_dir: pathlib.Path,
|
||||
logger: logging.Logger,
|
||||
) -> Optional[pathlib.Path]:
|
||||
"""
|
||||
Locate libxgboost from the local project directory's lib/ subdirectory.
|
||||
"""
|
||||
libxgboost = toplevel_dir.parent / "lib" / _lib_name()
|
||||
if libxgboost.exists():
|
||||
logger.info("Found %s at %s", libxgboost.name, str(libxgboost.parent))
|
||||
return libxgboost
|
||||
return None
|
||||
|
||||
|
||||
def locate_or_build_libxgboost(
|
||||
toplevel_dir: pathlib.Path,
|
||||
build_dir: pathlib.Path,
|
||||
build_config: BuildConfiguration,
|
||||
) -> pathlib.Path:
|
||||
"""Locate libxgboost; if not exist, build it"""
|
||||
logger = logging.getLogger("xgboost.packager.locate_or_build_libxgboost")
|
||||
|
||||
libxgboost = locate_local_libxgboost(toplevel_dir, logger=logger)
|
||||
if libxgboost is not None:
|
||||
return libxgboost
|
||||
if build_config.use_system_libxgboost:
|
||||
# Find libxgboost from system prefix
|
||||
sys_prefix = pathlib.Path(sys.prefix).absolute().resolve()
|
||||
libxgboost = sys_prefix / "lib" / _lib_name()
|
||||
if not libxgboost.exists():
|
||||
raise RuntimeError(
|
||||
f"use_system_libxgboost was specified but {_lib_name()} is "
|
||||
f"not found in {libxgboost.parent}"
|
||||
)
|
||||
|
||||
logger.info("Using system XGBoost: %s", str(libxgboost))
|
||||
return libxgboost
|
||||
|
||||
if toplevel_dir.joinpath("cpp_src").exists():
|
||||
# Source distribution; all C++ source files to be found in cpp_src/
|
||||
cpp_src_dir = toplevel_dir.joinpath("cpp_src")
|
||||
else:
|
||||
# Probably running "pip install ." from python-package/
|
||||
cpp_src_dir = toplevel_dir.parent
|
||||
if not cpp_src_dir.joinpath("CMakeLists.txt").exists():
|
||||
raise RuntimeError(f"Did not find CMakeLists.txt from {cpp_src_dir}")
|
||||
return build_libxgboost(cpp_src_dir, build_dir=build_dir, build_config=build_config)
|
||||
157
python-package/packager/pep517.py
Normal file
157
python-package/packager/pep517.py
Normal file
@@ -0,0 +1,157 @@
|
||||
"""
|
||||
Custom build backend for XGBoost Python package.
|
||||
Builds source distribution and binary wheels, following PEP 517 / PEP 660.
|
||||
Reuses components of Hatchling (https://github.com/pypa/hatch/tree/master/backend) for the sake
|
||||
of brevity.
|
||||
"""
|
||||
import dataclasses
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import tempfile
|
||||
from contextlib import contextmanager
|
||||
from typing import Any, Dict, Iterator, Optional, Union
|
||||
|
||||
import hatchling.build
|
||||
|
||||
from .build_config import BuildConfiguration
|
||||
from .nativelib import locate_local_libxgboost, locate_or_build_libxgboost
|
||||
from .sdist import copy_cpp_src_tree
|
||||
from .util import copy_with_logging, copytree_with_logging
|
||||
|
||||
|
||||
@contextmanager
|
||||
def cd(path: Union[str, pathlib.Path]) -> Iterator[str]: # pylint: disable=C0103
|
||||
"""
|
||||
Temporarily change working directory.
|
||||
TODO(hcho3): Remove this once we adopt Python 3.11, which implements contextlib.chdir.
|
||||
"""
|
||||
path = str(path)
|
||||
path = os.path.realpath(path)
|
||||
cwd = os.getcwd()
|
||||
os.chdir(path)
|
||||
try:
|
||||
yield path
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
TOPLEVEL_DIR = pathlib.Path(__file__).parent.parent.absolute().resolve()
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
# Aliases
|
||||
get_requires_for_build_sdist = hatchling.build.get_requires_for_build_sdist
|
||||
get_requires_for_build_wheel = hatchling.build.get_requires_for_build_wheel
|
||||
get_requires_for_build_editable = hatchling.build.get_requires_for_build_editable
|
||||
|
||||
|
||||
def build_wheel(
|
||||
wheel_directory: str,
|
||||
config_settings: Optional[Dict[str, Any]] = None,
|
||||
metadata_directory: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Build a wheel"""
|
||||
logger = logging.getLogger("xgboost.packager.build_wheel")
|
||||
|
||||
build_config = BuildConfiguration()
|
||||
build_config.update(config_settings)
|
||||
logger.info("Parsed build configuration: %s", dataclasses.asdict(build_config))
|
||||
|
||||
# Create tempdir with Python package + libxgboost
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
td_path = pathlib.Path(td)
|
||||
build_dir = td_path / "libbuild"
|
||||
build_dir.mkdir()
|
||||
|
||||
workspace = td_path / "whl_workspace"
|
||||
workspace.mkdir()
|
||||
logger.info("Copying project files to temporary directory %s", str(workspace))
|
||||
|
||||
copy_with_logging(TOPLEVEL_DIR / "pyproject.toml", workspace, logger=logger)
|
||||
copy_with_logging(TOPLEVEL_DIR / "hatch_build.py", workspace, logger=logger)
|
||||
copy_with_logging(TOPLEVEL_DIR / "README.rst", workspace, logger=logger)
|
||||
|
||||
pkg_path = workspace / "xgboost"
|
||||
copytree_with_logging(TOPLEVEL_DIR / "xgboost", pkg_path, logger=logger)
|
||||
lib_path = pkg_path / "lib"
|
||||
lib_path.mkdir()
|
||||
libxgboost = locate_or_build_libxgboost(
|
||||
TOPLEVEL_DIR, build_dir=build_dir, build_config=build_config
|
||||
)
|
||||
copy_with_logging(libxgboost, lib_path, logger=logger)
|
||||
|
||||
with cd(workspace):
|
||||
wheel_name = hatchling.build.build_wheel(
|
||||
wheel_directory, config_settings, metadata_directory
|
||||
)
|
||||
return wheel_name
|
||||
|
||||
|
||||
def build_sdist(
|
||||
sdist_directory: str,
|
||||
config_settings: Optional[Dict[str, Any]] = None,
|
||||
) -> str:
|
||||
"""Build a source distribution"""
|
||||
logger = logging.getLogger("xgboost.packager.build_sdist")
|
||||
|
||||
if config_settings:
|
||||
raise NotImplementedError(
|
||||
"XGBoost's custom build backend doesn't support config_settings option "
|
||||
f"when building sdist. {config_settings=}"
|
||||
)
|
||||
|
||||
cpp_src_dir = TOPLEVEL_DIR.parent
|
||||
if not cpp_src_dir.joinpath("CMakeLists.txt").exists():
|
||||
raise RuntimeError(f"Did not find CMakeLists.txt from {cpp_src_dir}")
|
||||
|
||||
# Create tempdir with Python package + C++ sources
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
td_path = pathlib.Path(td)
|
||||
|
||||
workspace = td_path / "sdist_workspace"
|
||||
workspace.mkdir()
|
||||
logger.info("Copying project files to temporary directory %s", str(workspace))
|
||||
|
||||
copy_with_logging(TOPLEVEL_DIR / "pyproject.toml", workspace, logger=logger)
|
||||
copy_with_logging(TOPLEVEL_DIR / "hatch_build.py", workspace, logger=logger)
|
||||
copy_with_logging(TOPLEVEL_DIR / "README.rst", workspace, logger=logger)
|
||||
|
||||
copytree_with_logging(
|
||||
TOPLEVEL_DIR / "xgboost", workspace / "xgboost", logger=logger
|
||||
)
|
||||
copytree_with_logging(
|
||||
TOPLEVEL_DIR / "packager", workspace / "packager", logger=logger
|
||||
)
|
||||
|
||||
temp_cpp_src_dir = workspace / "cpp_src"
|
||||
copy_cpp_src_tree(cpp_src_dir, target_dir=temp_cpp_src_dir, logger=logger)
|
||||
|
||||
with cd(workspace):
|
||||
sdist_name = hatchling.build.build_sdist(sdist_directory, config_settings)
|
||||
return sdist_name
|
||||
|
||||
|
||||
def build_editable(
|
||||
wheel_directory: str,
|
||||
config_settings: Optional[Dict[str, Any]] = None,
|
||||
metadata_directory: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Build an editable installation. We mostly delegate to Hatchling."""
|
||||
logger = logging.getLogger("xgboost.packager.build_editable")
|
||||
|
||||
if config_settings:
|
||||
raise NotImplementedError(
|
||||
"XGBoost's custom build backend doesn't support config_settings option "
|
||||
f"when building editable installation. {config_settings=}"
|
||||
)
|
||||
|
||||
if locate_local_libxgboost(TOPLEVEL_DIR, logger=logger) is None:
|
||||
raise RuntimeError(
|
||||
"To use the editable installation, first build libxgboost with CMake. "
|
||||
"See https://xgboost.readthedocs.io/en/latest/build.html for detailed instructions."
|
||||
)
|
||||
|
||||
return hatchling.build.build_editable(
|
||||
wheel_directory, config_settings, metadata_directory
|
||||
)
|
||||
27
python-package/packager/sdist.py
Normal file
27
python-package/packager/sdist.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
Functions for building sdist
|
||||
"""
|
||||
import logging
|
||||
import pathlib
|
||||
|
||||
from .util import copy_with_logging, copytree_with_logging
|
||||
|
||||
|
||||
def copy_cpp_src_tree(
|
||||
cpp_src_dir: pathlib.Path, target_dir: pathlib.Path, logger: logging.Logger
|
||||
) -> None:
|
||||
"""Copy C++ source tree into build directory"""
|
||||
|
||||
for subdir in [
|
||||
"src",
|
||||
"include",
|
||||
"dmlc-core",
|
||||
"gputreeshap",
|
||||
"rabit",
|
||||
"cmake",
|
||||
"plugin",
|
||||
]:
|
||||
copytree_with_logging(cpp_src_dir / subdir, target_dir / subdir, logger=logger)
|
||||
|
||||
for filename in ["CMakeLists.txt", "LICENSE"]:
|
||||
copy_with_logging(cpp_src_dir.joinpath(filename), target_dir, logger=logger)
|
||||
25
python-package/packager/util.py
Normal file
25
python-package/packager/util.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
Utility functions for implementing PEP 517 backend
|
||||
"""
|
||||
import logging
|
||||
import pathlib
|
||||
import shutil
|
||||
|
||||
|
||||
def copytree_with_logging(
|
||||
src: pathlib.Path, dest: pathlib.Path, logger: logging.Logger
|
||||
) -> None:
|
||||
"""Call shutil.copytree() with logging"""
|
||||
logger.info("Copying %s -> %s", str(src), str(dest))
|
||||
shutil.copytree(src, dest)
|
||||
|
||||
|
||||
def copy_with_logging(
|
||||
src: pathlib.Path, dest: pathlib.Path, logger: logging.Logger
|
||||
) -> None:
|
||||
"""Call shutil.copy() with logging"""
|
||||
if dest.is_dir():
|
||||
logger.info("Copying %s -> %s", str(src), str(dest / src.name))
|
||||
else:
|
||||
logger.info("Copying %s -> %s", str(src), str(dest))
|
||||
shutil.copy(src, dest)
|
||||
42
python-package/pyproject.toml
Normal file
42
python-package/pyproject.toml
Normal file
@@ -0,0 +1,42 @@
|
||||
[build-system]
|
||||
requires = [
|
||||
"hatchling>=1.12.1"
|
||||
]
|
||||
backend-path = ["."]
|
||||
build-backend = "packager.pep517"
|
||||
|
||||
[project]
|
||||
name = "xgboost"
|
||||
version = "2.0.0-dev"
|
||||
authors = [
|
||||
{name = "Hyunsu Cho", email = "chohyu01@cs.washington.edu"},
|
||||
{name = "Jiaming Yuan", email = "jm.yuan@outlook.com"}
|
||||
]
|
||||
description = "XGBoost Python Package"
|
||||
readme = {file = "README.rst", content-type = "text/x-rst"}
|
||||
requires-python = ">=3.8"
|
||||
license = {text = "Apache-2.0"}
|
||||
classifiers = [
|
||||
"License :: OSI Approved :: Apache Software License",
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10"
|
||||
]
|
||||
dependencies = [
|
||||
"numpy",
|
||||
"scipy"
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
pandas = ["pandas"]
|
||||
scikit-learn = ["scikit-learn"]
|
||||
dask = ["dask", "pandas", "distributed"]
|
||||
datatable = ["datatable"]
|
||||
plotting = ["graphviz", "matplotlib"]
|
||||
pyspark = ["pyspark", "scikit-learn", "cloudpickle"]
|
||||
|
||||
[tool.hatch.build.targets.wheel.hooks.custom]
|
||||
@@ -1,399 +0,0 @@
|
||||
"""Setup xgboost package."""
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from platform import system
|
||||
from typing import List, Optional
|
||||
|
||||
from setuptools import Extension, find_packages, setup
|
||||
from setuptools.command import build_ext, install, install_lib, sdist
|
||||
|
||||
# You can't use `pip install .` as pip copies setup.py to a temporary
|
||||
# directory, parent directory is no longer reachable (isolated build) .
|
||||
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||
sys.path.insert(0, CURRENT_DIR)
|
||||
|
||||
# Options only effect `python setup.py install`, building `bdist_wheel`
|
||||
# requires using CMake directly.
|
||||
USER_OPTIONS = {
|
||||
# libxgboost options.
|
||||
"use-openmp": (None, "Build with OpenMP support.", 1),
|
||||
"use-cuda": (None, "Build with GPU acceleration.", 0),
|
||||
"use-nccl": (None, "Build with NCCL to enable distributed GPU support.", 0),
|
||||
"build-with-shared-nccl": (None, "Build with shared NCCL library.", 0),
|
||||
"hide-cxx-symbols": (None, "Hide all C++ symbols during build.", 1),
|
||||
"use-hdfs": (None, "Build with HDFS support", 0),
|
||||
"use-azure": (None, "Build with AZURE support.", 0),
|
||||
"use-s3": (None, "Build with S3 support", 0),
|
||||
"plugin-dense-parser": (None, "Build dense parser plugin.", 0),
|
||||
# Python specific
|
||||
"use-system-libxgboost": (None, "Use libxgboost.so in system path.", 0),
|
||||
}
|
||||
|
||||
NEED_CLEAN_TREE = set()
|
||||
NEED_CLEAN_FILE = set()
|
||||
BUILD_TEMP_DIR = None
|
||||
|
||||
|
||||
def lib_name() -> str:
|
||||
"""Return platform dependent shared object name."""
|
||||
if system() == "Linux" or system().upper().endswith("BSD"):
|
||||
name = "libxgboost.so"
|
||||
elif system() == "Darwin":
|
||||
name = "libxgboost.dylib"
|
||||
elif system() == "Windows":
|
||||
name = "xgboost.dll"
|
||||
elif system() == "OS400":
|
||||
name = "libxgboost.so"
|
||||
return name
|
||||
|
||||
|
||||
def copy_tree(src_dir: str, target_dir: str) -> None:
|
||||
"""Copy source tree into build directory."""
|
||||
|
||||
def clean_copy_tree(src: str, dst: str) -> None:
|
||||
shutil.copytree(src, dst)
|
||||
NEED_CLEAN_TREE.add(os.path.abspath(dst))
|
||||
|
||||
def clean_copy_file(src: str, dst: str) -> None:
|
||||
shutil.copy(src, dst)
|
||||
NEED_CLEAN_FILE.add(os.path.abspath(dst))
|
||||
|
||||
src = os.path.join(src_dir, "src")
|
||||
inc = os.path.join(src_dir, "include")
|
||||
dmlc_core = os.path.join(src_dir, "dmlc-core")
|
||||
gputreeshap = os.path.join(src_dir, "gputreeshap")
|
||||
rabit = os.path.join(src_dir, "rabit")
|
||||
cmake = os.path.join(src_dir, "cmake")
|
||||
plugin = os.path.join(src_dir, "plugin")
|
||||
|
||||
clean_copy_tree(src, os.path.join(target_dir, "src"))
|
||||
clean_copy_tree(inc, os.path.join(target_dir, "include"))
|
||||
clean_copy_tree(dmlc_core, os.path.join(target_dir, "dmlc-core"))
|
||||
clean_copy_tree(gputreeshap, os.path.join(target_dir, "gputreeshap"))
|
||||
clean_copy_tree(rabit, os.path.join(target_dir, "rabit"))
|
||||
clean_copy_tree(cmake, os.path.join(target_dir, "cmake"))
|
||||
clean_copy_tree(plugin, os.path.join(target_dir, "plugin"))
|
||||
|
||||
cmake_list = os.path.join(src_dir, "CMakeLists.txt")
|
||||
clean_copy_file(cmake_list, os.path.join(target_dir, "CMakeLists.txt"))
|
||||
lic = os.path.join(src_dir, "LICENSE")
|
||||
clean_copy_file(lic, os.path.join(target_dir, "LICENSE"))
|
||||
|
||||
|
||||
def clean_up() -> None:
|
||||
"""Removed copied files."""
|
||||
for path in NEED_CLEAN_TREE:
|
||||
shutil.rmtree(path)
|
||||
for path in NEED_CLEAN_FILE:
|
||||
os.remove(path)
|
||||
|
||||
|
||||
class CMakeExtension(Extension): # pylint: disable=too-few-public-methods
|
||||
"""Wrapper for extension"""
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
super().__init__(name=name, sources=[])
|
||||
|
||||
|
||||
class BuildExt(build_ext.build_ext): # pylint: disable=too-many-ancestors
|
||||
"""Custom build_ext command using CMake."""
|
||||
|
||||
logger = logging.getLogger("XGBoost build_ext")
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
def build(
|
||||
self,
|
||||
src_dir: str,
|
||||
build_dir: str,
|
||||
generator: str,
|
||||
build_tool: Optional[str] = None,
|
||||
use_omp: int = 1,
|
||||
) -> None:
|
||||
"""Build the core library with CMake."""
|
||||
cmake_cmd = ["cmake", src_dir, generator]
|
||||
|
||||
for k, v in USER_OPTIONS.items():
|
||||
arg = k.replace("-", "_").upper()
|
||||
value = str(v[2])
|
||||
if arg == "USE_SYSTEM_LIBXGBOOST":
|
||||
continue
|
||||
if arg == "USE_OPENMP" and use_omp == 0:
|
||||
cmake_cmd.append("-D" + arg + "=0")
|
||||
continue
|
||||
cmake_cmd.append("-D" + arg + "=" + value)
|
||||
|
||||
# Flag for cross-compiling for Apple Silicon
|
||||
# We use environment variable because it's the only way to pass down custom flags
|
||||
# through the cibuildwheel package, which otherwise calls `python setup.py bdist_wheel`
|
||||
# command.
|
||||
if "CIBW_TARGET_OSX_ARM64" in os.environ:
|
||||
cmake_cmd.append("-DCMAKE_OSX_ARCHITECTURES=arm64")
|
||||
|
||||
self.logger.info("Run CMake command: %s", str(cmake_cmd))
|
||||
subprocess.check_call(cmake_cmd, cwd=build_dir)
|
||||
|
||||
if system() != "Windows":
|
||||
nproc = os.cpu_count()
|
||||
assert build_tool is not None
|
||||
subprocess.check_call([build_tool, "-j" + str(nproc)], cwd=build_dir)
|
||||
else:
|
||||
subprocess.check_call(
|
||||
["cmake", "--build", ".", "--config", "Release"], cwd=build_dir
|
||||
)
|
||||
|
||||
def build_cmake_extension(self) -> None:
|
||||
"""Configure and build using CMake"""
|
||||
if USER_OPTIONS["use-system-libxgboost"][2]:
|
||||
self.logger.info("Using system libxgboost.")
|
||||
return
|
||||
|
||||
build_dir = self.build_temp
|
||||
global BUILD_TEMP_DIR # pylint: disable=global-statement
|
||||
BUILD_TEMP_DIR = build_dir
|
||||
libxgboost = os.path.abspath(
|
||||
os.path.join(CURRENT_DIR, os.path.pardir, "lib", lib_name())
|
||||
)
|
||||
|
||||
if os.path.exists(libxgboost):
|
||||
self.logger.info("Found shared library, skipping build.")
|
||||
return
|
||||
|
||||
src_dir = "xgboost"
|
||||
try:
|
||||
copy_tree(
|
||||
os.path.join(CURRENT_DIR, os.path.pardir),
|
||||
os.path.join(self.build_temp, src_dir),
|
||||
)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
copy_tree(src_dir, os.path.join(self.build_temp, src_dir))
|
||||
|
||||
self.logger.info("Building from source. %s", libxgboost)
|
||||
if not os.path.exists(build_dir):
|
||||
os.mkdir(build_dir)
|
||||
if shutil.which("ninja"):
|
||||
build_tool = "ninja"
|
||||
else:
|
||||
build_tool = "make"
|
||||
if sys.platform.startswith("os400"):
|
||||
build_tool = "make"
|
||||
|
||||
if system() == "Windows":
|
||||
# Pick up from LGB, just test every possible tool chain.
|
||||
for vs in (
|
||||
"-GVisual Studio 17 2022",
|
||||
"-GVisual Studio 16 2019",
|
||||
"-GVisual Studio 15 2017",
|
||||
"-GVisual Studio 14 2015",
|
||||
"-GMinGW Makefiles",
|
||||
):
|
||||
try:
|
||||
self.build(src_dir, build_dir, vs)
|
||||
self.logger.info(
|
||||
"%s is used for building Windows distribution.", vs
|
||||
)
|
||||
break
|
||||
except subprocess.CalledProcessError:
|
||||
shutil.rmtree(build_dir)
|
||||
os.mkdir(build_dir)
|
||||
continue
|
||||
else:
|
||||
gen = "-GNinja" if build_tool == "ninja" else "-GUnix Makefiles"
|
||||
try:
|
||||
self.build(src_dir, build_dir, gen, build_tool, use_omp=1)
|
||||
except subprocess.CalledProcessError:
|
||||
self.logger.warning("Disabling OpenMP support.")
|
||||
self.build(src_dir, build_dir, gen, build_tool, use_omp=0)
|
||||
|
||||
def build_extension(self, ext: Extension) -> None:
|
||||
"""Override the method for dispatching."""
|
||||
if isinstance(ext, CMakeExtension):
|
||||
self.build_cmake_extension()
|
||||
else:
|
||||
super().build_extension(ext)
|
||||
|
||||
def copy_extensions_to_source(self) -> None:
|
||||
"""Dummy override. Invoked during editable installation. Our binary
|
||||
should available in `lib`.
|
||||
|
||||
"""
|
||||
if not os.path.exists(
|
||||
os.path.join(CURRENT_DIR, os.path.pardir, "lib", lib_name())
|
||||
):
|
||||
raise ValueError(
|
||||
"For using editable installation, please "
|
||||
+ "build the shared object first with CMake."
|
||||
)
|
||||
|
||||
|
||||
class Sdist(sdist.sdist): # pylint: disable=too-many-ancestors
|
||||
"""Copy c++ source into Python directory."""
|
||||
|
||||
logger = logging.getLogger("xgboost sdist")
|
||||
|
||||
def run(self) -> None:
|
||||
copy_tree(
|
||||
os.path.join(CURRENT_DIR, os.path.pardir),
|
||||
os.path.join(CURRENT_DIR, "xgboost"),
|
||||
)
|
||||
libxgboost = os.path.join(CURRENT_DIR, os.path.pardir, "lib", lib_name())
|
||||
if os.path.exists(libxgboost):
|
||||
self.logger.warning(
|
||||
"Found shared library, removing to avoid being included in source distribution."
|
||||
)
|
||||
os.remove(libxgboost)
|
||||
super().run()
|
||||
|
||||
|
||||
class InstallLib(install_lib.install_lib):
|
||||
"""Copy shared object into installation directory."""
|
||||
|
||||
logger = logging.getLogger("xgboost install_lib")
|
||||
|
||||
def install(self) -> List[str]:
|
||||
outfiles = super().install()
|
||||
|
||||
if USER_OPTIONS["use-system-libxgboost"][2] != 0:
|
||||
self.logger.info("Using system libxgboost.")
|
||||
lib_path = os.path.join(sys.prefix, "lib")
|
||||
msg = (
|
||||
"use-system-libxgboost is specified, but "
|
||||
+ lib_name()
|
||||
+ " is not found in: "
|
||||
+ lib_path
|
||||
)
|
||||
assert os.path.exists(os.path.join(lib_path, lib_name())), msg
|
||||
return []
|
||||
|
||||
lib_dir = os.path.join(self.install_dir, "xgboost", "lib")
|
||||
if not os.path.exists(lib_dir):
|
||||
os.mkdir(lib_dir)
|
||||
dst = os.path.join(self.install_dir, "xgboost", "lib", lib_name())
|
||||
|
||||
libxgboost_path = lib_name()
|
||||
|
||||
assert BUILD_TEMP_DIR is not None
|
||||
dft_lib_dir = os.path.join(CURRENT_DIR, os.path.pardir, "lib")
|
||||
build_dir = os.path.join(BUILD_TEMP_DIR, "xgboost", "lib")
|
||||
|
||||
if os.path.exists(os.path.join(dft_lib_dir, libxgboost_path)):
|
||||
# The library is built by CMake directly
|
||||
src = os.path.join(dft_lib_dir, libxgboost_path)
|
||||
else:
|
||||
# The library is built by setup.py
|
||||
src = os.path.join(build_dir, libxgboost_path)
|
||||
self.logger.info("Installing shared library: %s", src)
|
||||
dst, _ = self.copy_file(src, dst)
|
||||
outfiles.append(dst)
|
||||
return outfiles
|
||||
|
||||
|
||||
class Install(install.install): # pylint: disable=too-many-instance-attributes
|
||||
"""An interface to install command, accepting XGBoost specific
|
||||
arguments.
|
||||
|
||||
"""
|
||||
|
||||
user_options = install.install.user_options + [
|
||||
(k, v[0], v[1]) for k, v in USER_OPTIONS.items()
|
||||
]
|
||||
|
||||
def initialize_options(self) -> None:
|
||||
super().initialize_options()
|
||||
self.use_openmp = 1
|
||||
self.use_cuda = 0
|
||||
self.use_nccl = 0
|
||||
self.build_with_shared_nccl = 0
|
||||
self.hide_cxx_symbols = 1
|
||||
|
||||
self.use_hdfs = 0
|
||||
self.use_azure = 0
|
||||
self.use_s3 = 0
|
||||
|
||||
self.plugin_dense_parser = 0
|
||||
|
||||
self.use_system_libxgboost = 0
|
||||
|
||||
def run(self) -> None:
|
||||
# setuptools will configure the options according to user supplied command line
|
||||
# arguments, then here we propagate them into `USER_OPTIONS` for visibility to
|
||||
# other sub-commands like `build_ext`.
|
||||
for k, v in USER_OPTIONS.items():
|
||||
arg = k.replace("-", "_")
|
||||
if hasattr(self, arg):
|
||||
USER_OPTIONS[k] = (v[0], v[1], getattr(self, arg))
|
||||
super().run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Supported commands:
|
||||
# From internet:
|
||||
# - pip install xgboost
|
||||
# - pip install --no-binary :all: xgboost
|
||||
|
||||
# From source tree `xgboost/python-package`:
|
||||
# - python setup.py build
|
||||
# - python setup.py build_ext
|
||||
# - python setup.py install
|
||||
# - python setup.py sdist && pip install <sdist-name>
|
||||
# - python setup.py bdist_wheel && pip install <wheel-name>
|
||||
|
||||
# When XGBoost is compiled directly with CMake:
|
||||
# - pip install -e .
|
||||
# - python setup.py develop # same as above
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
with open(os.path.join(CURRENT_DIR, "README.rst"), encoding="utf-8") as fd:
|
||||
description = fd.read()
|
||||
with open(os.path.join(CURRENT_DIR, "xgboost/VERSION"), encoding="ascii") as fd:
|
||||
version = fd.read().strip()
|
||||
|
||||
setup(
|
||||
name="xgboost",
|
||||
version=version,
|
||||
description="XGBoost Python Package",
|
||||
long_description=description,
|
||||
long_description_content_type="text/x-rst",
|
||||
install_requires=[
|
||||
"numpy",
|
||||
"scipy",
|
||||
],
|
||||
ext_modules=[CMakeExtension("libxgboost")],
|
||||
# error: expected "str": "Type[Command]"
|
||||
cmdclass={
|
||||
"build_ext": BuildExt, # type: ignore
|
||||
"sdist": Sdist, # type: ignore
|
||||
"install_lib": InstallLib, # type: ignore
|
||||
"install": Install, # type: ignore
|
||||
},
|
||||
extras_require={
|
||||
"pandas": ["pandas"],
|
||||
"scikit-learn": ["scikit-learn"],
|
||||
"dask": ["dask", "pandas", "distributed"],
|
||||
"datatable": ["datatable"],
|
||||
"plotting": ["graphviz", "matplotlib"],
|
||||
"pyspark": ["pyspark", "scikit-learn", "cloudpickle"],
|
||||
},
|
||||
maintainer="Hyunsu Cho",
|
||||
maintainer_email="chohyu01@cs.washington.edu",
|
||||
zip_safe=False,
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
license="Apache-2.0",
|
||||
classifiers=[
|
||||
"License :: OSI Approved :: Apache Software License",
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
],
|
||||
python_requires=">=3.8",
|
||||
url="https://github.com/dmlc/xgboost",
|
||||
)
|
||||
|
||||
clean_up()
|
||||
@@ -16,7 +16,7 @@ def config_doc(
|
||||
extra_note: Optional[str] = None,
|
||||
parameters: Optional[str] = None,
|
||||
returns: Optional[str] = None,
|
||||
see_also: Optional[str] = None
|
||||
see_also: Optional[str] = None,
|
||||
) -> Callable[[_F], _F]:
|
||||
"""Decorator to format docstring for config functions.
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ def plot_importance(
|
||||
grid: bool = True,
|
||||
show_values: bool = True,
|
||||
values_format: str = "{v}",
|
||||
**kwargs: Any
|
||||
**kwargs: Any,
|
||||
) -> Axes:
|
||||
"""Plot importance based on fitted trees.
|
||||
|
||||
@@ -155,7 +155,7 @@ def to_graphviz(
|
||||
no_color: Optional[str] = None,
|
||||
condition_node_params: Optional[dict] = None,
|
||||
leaf_node_params: Optional[dict] = None,
|
||||
**kwargs: Any
|
||||
**kwargs: Any,
|
||||
) -> GraphvizSource:
|
||||
"""Convert specified tree to graphviz instance. IPython can automatically plot
|
||||
the returned graphviz instance. Otherwise, you should call .render() method
|
||||
@@ -250,7 +250,7 @@ def plot_tree(
|
||||
num_trees: int = 0,
|
||||
rankdir: Optional[str] = None,
|
||||
ax: Optional[Axes] = None,
|
||||
**kwargs: Any
|
||||
**kwargs: Any,
|
||||
) -> Axes:
|
||||
"""Plot specified tree.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user