xgb-diffusion 0.1.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
xgb_diffusion/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.3.0-dev
@@ -0,0 +1,18 @@
1
+ """Diffusion estimators backed by a fork of XGBoost."""
2
+
3
+ from importlib.metadata import PackageNotFoundError, version
4
+
5
+ from ._c_api import _py_version
6
+ from .xgbddpm import XGBDDPMClassifier, XGBDDPMRegressor
7
+ from .xgbdiffusion import XGBDiffusionRegressor
8
+
9
+ try:
10
+ __version__ = version("xgb-diffusion")
11
+ except PackageNotFoundError:
12
+ __version__ = _py_version()
13
+
14
+ __all__ = [
15
+ "XGBDDPMClassifier",
16
+ "XGBDDPMRegressor",
17
+ "XGBDiffusionRegressor",
18
+ ]
@@ -0,0 +1,195 @@
1
+ """Low-level ctypes bridge for the XGBoost C API."""
2
+
3
+ import ctypes
4
+ import json
5
+ import os
6
+ import warnings
7
+ from typing import Any, Callable, List, Tuple, Union, cast, overload
8
+
9
+ from ._typing import CStrPptr, c_bst_ulong
10
+ from .compat import py_str
11
+ from .libpath import find_lib_path
12
+
13
+
14
+ class XGBoostError(ValueError):
15
+ """Error thrown by xgboost trainer."""
16
+
17
+
18
+ @overload
19
+ def from_pystr_to_cstr(data: str) -> bytes: ...
20
+
21
+
22
+ @overload
23
+ def from_pystr_to_cstr(data: List[str]) -> ctypes.Array: ...
24
+
25
+
26
+ def from_pystr_to_cstr(data: Union[str, List[str]]) -> Union[bytes, ctypes.Array]:
27
+ """Convert a Python str or list of Python str to C pointer."""
28
+ if isinstance(data, str):
29
+ return bytes(data, "utf-8")
30
+ if isinstance(data, list):
31
+ data_as_bytes: List[bytes] = [bytes(d, "utf-8") for d in data]
32
+ pointers: ctypes.Array[ctypes.c_char_p] = (
33
+ ctypes.c_char_p * len(data_as_bytes)
34
+ )(*data_as_bytes)
35
+ return pointers
36
+ raise TypeError()
37
+
38
+
39
+ def from_cstr_to_pystr(data: CStrPptr, length: c_bst_ulong) -> List[str]:
40
+ """Revert C pointer to Python str."""
41
+ res = []
42
+ for i in range(length.value):
43
+ try:
44
+ res.append(str(cast(bytes, data[i]).decode("ascii")))
45
+ except UnicodeDecodeError:
46
+ res.append(str(cast(bytes, data[i]).decode("utf-8")))
47
+ return res
48
+
49
+
50
+ def make_jcargs(**kwargs: Any) -> bytes:
51
+ """Make JSON-based arguments for C functions."""
52
+ return from_pystr_to_cstr(json.dumps(kwargs))
53
+
54
+
55
+ def _log_callback(msg: bytes) -> None:
56
+ """Redirect logs from native library into Python console."""
57
+ smsg = py_str(msg)
58
+ if smsg.find("WARNING:") != -1:
59
+ # Stacklevel:
60
+ # 1: This line
61
+ # 2: XGBoost C functions like `_LIB.XGBoosterTrainOneIter`.
62
+ # 3: The Python function that calls the C function.
63
+ warnings.warn(smsg, UserWarning, stacklevel=3)
64
+ return
65
+ print(smsg)
66
+
67
+
68
+ def _get_log_callback_func() -> Callable:
69
+ """Wrap log_callback() method in ctypes callback type."""
70
+ c_callback = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
71
+ return c_callback(_log_callback)
72
+
73
+
74
+ def _lib_version(lib: ctypes.CDLL) -> Tuple[int, int, int]:
75
+ """Get the XGBoost version from native shared object."""
76
+ major = ctypes.c_int()
77
+ minor = ctypes.c_int()
78
+ patch = ctypes.c_int()
79
+ lib.XGBoostVersion(ctypes.byref(major), ctypes.byref(minor), ctypes.byref(patch))
80
+ return major.value, minor.value, patch.value
81
+
82
+
83
+ def _py_version() -> str:
84
+ """Get the XGBoost version from Python version file."""
85
+ version_file = os.path.join(os.path.dirname(__file__), "VERSION")
86
+ with open(version_file, encoding="ascii") as f:
87
+ return f.read().strip()
88
+
89
+
90
+ def _register_log_callback(lib: ctypes.CDLL) -> None:
91
+ lib.XGBGetLastError.restype = ctypes.c_char_p
92
+ lib.callback = _get_log_callback_func() # type: ignore[attr-defined]
93
+ if lib.XGBRegisterLogCallback(lib.callback) != 0:
94
+ raise XGBoostError(lib.XGBGetLastError())
95
+
96
+
97
+ def _parse_version(ver: str) -> Tuple[Tuple[int, int, int], str]:
98
+ """Avoid dependency on packaging (PEP 440)."""
99
+ # 2.0.0-dev, 2.0.0, 2.0.0.post1, or 2.0.0rc1
100
+ if ver.find("post") != -1:
101
+ major, minor, patch = ver.split(".")[:-1]
102
+ postfix = ver.split(".")[-1]
103
+ elif "-dev" in ver:
104
+ major, minor, patch = ver.split("-")[0].split(".")
105
+ postfix = "dev"
106
+ else:
107
+ major, minor, patch = ver.split(".")
108
+ rc = patch.find("rc")
109
+ if rc != -1:
110
+ postfix = patch[rc:]
111
+ patch = patch[:rc]
112
+ else:
113
+ postfix = ""
114
+
115
+ return (int(major), int(minor), int(patch)), postfix
116
+
117
+
118
+ def _load_lib() -> ctypes.CDLL:
119
+ """Load xgboost library."""
120
+ lib_paths = find_lib_path()
121
+ if not lib_paths:
122
+ # This happens only when building document.
123
+ return None # type: ignore[return-value]
124
+ try:
125
+ path_backup = os.environ["PATH"].split(os.pathsep)
126
+ except KeyError:
127
+ path_backup = []
128
+ lib_success = False
129
+ os_error_list = []
130
+ for lib_path in lib_paths:
131
+ try:
132
+ # needed when the lib is linked with non-system-available
133
+ # dependencies
134
+ os.environ["PATH"] = os.pathsep.join(
135
+ path_backup + [os.path.dirname(lib_path)]
136
+ )
137
+ lib = ctypes.cdll.LoadLibrary(lib_path)
138
+ setattr(lib, "path", os.path.normpath(lib_path))
139
+ lib_success = True
140
+ break
141
+ except OSError as e:
142
+ os_error_list.append(str(e))
143
+ continue
144
+ finally:
145
+ os.environ["PATH"] = os.pathsep.join(path_backup)
146
+ if not lib_success:
147
+ libname = os.path.basename(lib_paths[0])
148
+ raise XGBoostError(f"""
149
+ XGBoost Library ({libname}) could not be loaded.
150
+ Likely causes:
151
+ * OpenMP runtime is not installed
152
+ - vcomp140.dll or libgomp-1.dll for Windows
153
+ - libomp.dylib for Mac OSX
154
+ - libgomp.so for Linux and other UNIX-like OSes
155
+ Mac OSX users: Run `brew install libomp` to install OpenMP runtime.
156
+
157
+ * You are running 32-bit Python on a 64-bit OS
158
+
159
+ Error message(s): {os_error_list}
160
+ """)
161
+ _register_log_callback(lib)
162
+
163
+ libver = _lib_version(lib)
164
+ pyver, _ = _parse_version(_py_version())
165
+
166
+ # verify that we are loading the correct binary.
167
+ if pyver != libver:
168
+ pyver_str = ".".join((str(v) for v in pyver))
169
+ libver_str = ".".join((str(v) for v in libver))
170
+ msg = (
171
+ "Mismatched version between the Python package and the native shared "
172
+ f"""object. Python package version: {pyver_str}. Shared object """
173
+ f"""version: {libver_str}. Shared object is loaded from: {lib.path}.
174
+ Likely cause:
175
+ * XGBoost is first installed with anaconda then upgraded with pip. To fix it """
176
+ "please remove one of the installations."
177
+ )
178
+ raise ValueError(msg)
179
+
180
+ return lib
181
+
182
+
183
+ # load the XGBoost library globally
184
+ _LIB = _load_lib()
185
+
186
+
187
+ def _check_call(ret: int) -> None:
188
+ """Check the return value of C API call."""
189
+ if ret != 0:
190
+ raise XGBoostError(py_str(_LIB.XGBGetLastError()))
191
+
192
+
193
+ def c_str(string: str) -> ctypes.c_char_p:
194
+ """Convert a python string to cstring."""
195
+ return ctypes.c_char_p(string.encode("utf-8"))