2
2
3
3
import argparse
4
4
import os
5
+ import tomllib
5
6
7
+ from packaging .version import Version
6
8
from test_utils import PY_PACKAGE
7
9
8
10
IN_PATH = os .path .join (PY_PACKAGE , "pyproject.toml.in" )
11
+ STUB_IN_PATH = os .path .join (PY_PACKAGE , "pyproject.toml.stub.in" )
9
12
OUT_PATH = os .path .join (PY_PACKAGE , "pyproject.toml" )
10
13
11
- NCCL_WHL = """ \" nvidia-nccl-cu12 ; platform_system == 'Linux' and platform_machine != 'aarch64'\" ,"""
14
+ NCCL_WHL = """ \" nvidia-nccl-{0} ; platform_system == 'Linux' and platform_machine != 'aarch64'\" ,"""
12
15
13
16
NAME = "{{ name }}"
14
17
NCCL = "{{ nccl }}"
18
+ VERSION = "{{ version }}"
19
+ CUDA_VARIANTS = ["cu12" , "cu13" ]
15
20
16
21
17
22
def copyfile (src : str , dst : str ) -> None :
@@ -21,22 +26,55 @@ def copyfile(src: str, dst: str) -> None:
21
26
fd .write (content )
22
27
23
28
24
- def make_pyproject (* , use_cpu_suffix : int , require_nccl_dep : int ) -> None :
25
- if use_cpu_suffix == 1 and require_nccl_dep == 1 :
29
+ def make_pyproject (
30
+ * , use_suffix : str , require_nccl_dep : str , create_stub : bool = False
31
+ ) -> None :
32
+ if use_suffix == "cpu" and require_nccl_dep != "na" :
26
33
raise ValueError (
27
34
"xgboost-cpu cannot require NCCL dependency. "
28
- "If --use-cpu- suffix=1 , you must set --require-nccl-dep=0 ."
35
+ "When setting --use-suffix='cpu' , you must also set --require-nccl-dep='na' ."
29
36
)
37
+ if (
38
+ use_suffix in CUDA_VARIANTS
39
+ and require_nccl_dep in CUDA_VARIANTS
40
+ and use_suffix != require_nccl_dep
41
+ ):
42
+ raise ValueError (
43
+ "Inconsistent choices for --use-suffix and --require-nccl-dep. "
44
+ "When --use-suffix is set to one of {{{0}}}, --require-nccl-dep must be "
45
+ "set to identical value as --use-suffix." .format ("," .join (CUDA_VARIANTS ))
46
+ )
47
+ if create_stub :
48
+ if use_suffix == "na" :
49
+ raise ValueError ("To create a stub package, --use-suffix must not be 'na'" )
50
+ if require_nccl_dep != "na" :
51
+ raise ValueError (
52
+ "To create a stub package, --require-nccl-dep must be 'na'"
53
+ )
30
54
31
- with open (IN_PATH ) as fd :
55
+ with open (STUB_IN_PATH if create_stub else IN_PATH ) as fd :
32
56
pyproject = fd .read ()
33
57
34
58
readme_dft = os .path .join (PY_PACKAGE , "README.dft.rst" )
35
59
readme_cpu = os .path .join (PY_PACKAGE , "README.cpu.rst" )
60
+ readme_stub = os .path .join (PY_PACKAGE , "README.stub.rst" )
36
61
readme = os .path .join (PY_PACKAGE , "README.rst" )
37
- pyproject = pyproject .replace (NAME , "xgboost-cpu" if use_cpu_suffix else "xgboost" )
38
- copyfile (readme_cpu if use_cpu_suffix else readme_dft , readme )
39
- pyproject = pyproject .replace (NCCL , NCCL_WHL if require_nccl_dep else "" )
62
+ pyproject = pyproject .replace (
63
+ NAME , f"xgboost-{ use_suffix } " if use_suffix != "na" else "xgboost"
64
+ )
65
+ if create_stub :
66
+ copyfile (readme_stub , readme )
67
+ pyproject_parsed = tomllib .loads (pyproject )
68
+ pyproject = pyproject .replace (
69
+ VERSION , str (Version (pyproject_parsed ["project" ]["version" ]))
70
+ )
71
+ elif use_suffix == "cpu" :
72
+ copyfile (readme_cpu , readme )
73
+ else :
74
+ copyfile (readme_dft , readme )
75
+ pyproject = pyproject .replace (
76
+ NCCL , NCCL_WHL .format (require_nccl_dep ) if require_nccl_dep != "na" else ""
77
+ )
40
78
pyproject = (
41
79
f"# Generated by `{ os .path .basename (__file__ )} `, don't edit.\n " + pyproject
42
80
)
@@ -48,21 +86,27 @@ def make_pyproject(*, use_cpu_suffix: int, require_nccl_dep: int) -> None:
48
86
if __name__ == "__main__" :
49
87
parser = argparse .ArgumentParser ()
50
88
parser .add_argument (
51
- "--use-cpu- suffix" ,
52
- type = int ,
53
- choices = [0 , 1 ] ,
54
- required = True ,
55
- help = "Whether to rename the package name to xgboost-cpu " ,
89
+ "--use-suffix" ,
90
+ type = str ,
91
+ choices = ["na" , "cpu" ] + CUDA_VARIANTS ,
92
+ default = "na" ,
93
+ help = "When using this option, rename the package name to xgboost-[suffix]. Set to 'na' to disable " ,
56
94
)
57
95
parser .add_argument (
58
96
"--require-nccl-dep" ,
59
- type = int ,
60
- choices = [0 , 1 ] ,
97
+ type = str ,
98
+ choices = ["na" ] + CUDA_VARIANTS ,
61
99
required = True ,
62
- help = "Whether to require the NCCL dependency" ,
100
+ help = "Which NCCL dependency to use; select 'na' to remove NCCL dependency" ,
101
+ )
102
+ parser .add_argument (
103
+ "--create-stub" ,
104
+ action = "store_true" ,
105
+ help = "Create a stub package that redirects users to install `xgboost`" ,
63
106
)
64
107
args = parser .parse_args ()
65
108
make_pyproject (
66
- use_cpu_suffix = args .use_cpu_suffix ,
109
+ use_suffix = args .use_suffix ,
67
110
require_nccl_dep = args .require_nccl_dep ,
111
+ create_stub = args .create_stub ,
68
112
)
0 commit comments