Skip to content

Commit

Permalink
remove all ctypes.pointer[]
Browse files Browse the repository at this point in the history
  • Loading branch information
bridgream committed Mar 2, 2022
1 parent 7100194 commit 926bf6c
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,31 @@
# ArrayLike = Union[np.ndarray, Sequence]
ArrayLike = Any
T = TypeVar("T")
ctype_t = Union[ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_float, ctypes.c_uint, ctypes.c_size_t]

ctype_numeric_t = Union[
ctype_t = Union[ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_float, ctypes.c_uint, ctypes.c_size_t]
c_numeric_t = Union[
ctypes.c_float,
ctypes.c_double,
ctypes.c_uint,
ctypes.c_uint64,
ctypes.c_int32,
ctypes.c_int64
]

# cptr_numeric_t = Union[
# c_numeric_t = Union[
# ctypes.pointer[ctypes.c_float],
# ctypes.pointer[ctypes.c_double],
# ctypes.pointer[ctypes.c_uint],
# ctypes.pointer[ctypes.c_uint64],
# ctypes.pointer[ctypes.c_int32],
# ctypes.pointer[ctypes.c_int64],
# ]
cptr_numeric_t = ctypes.pointer

# cstr_ptr_t = ctypes.pointer[ctypes.c_char_p]
cstr_ptr_t = Any
c_numeric_ptr_t = ctypes.pointer
# c_str_ptr_t = ctypes.pointer[ctypes.c_char_p]
c_str_pptr_t = Any
# c_str_ptr_t = ctypes.pointer[ctypes.c_char]
c_str_ptr_t = Any
# c_float_ptr_t = ctypes.pointer[ctypes.c_float]
c_float_ptr_t = Any

DataType = Any
cupy_t = ArrayLike
Expand All @@ -66,7 +68,7 @@ class XGBoostError(ValueError):
"""Error thrown by xgboost trainer."""


def from_pystr_to_cstr(data: Union[str, List[str]]) -> Union[bytes, cstr_ptr_t]:
def from_pystr_to_cstr(data: Union[str, List[str]]) -> Union[bytes, c_str_pptr_t]:
"""Convert a Python str or list of Python str to C pointer
Parameters
Expand All @@ -85,7 +87,7 @@ def from_pystr_to_cstr(data: Union[str, List[str]]) -> Union[bytes, cstr_ptr_t]:
raise TypeError()


def from_cstr_to_pystr(data: cstr_ptr_t, length: c_bst_ulong) -> List[str]:
def from_cstr_to_pystr(data: c_str_pptr_t, length: c_bst_ulong) -> List[str]:
"""Revert C pointer to Python str
Parameters
Expand Down Expand Up @@ -242,8 +244,8 @@ def build_info() -> dict:
return res


def _numpy2ctypes_type(dtype: Type[np.number]) -> Type[ctype_numeric_t]:
_NUMPY_TO_CTYPES_MAPPING: Dict[Type[np.number], Type[ctype_numeric_t]] = {
def _numpy2ctypes_type(dtype: Type[np.number]) -> Type[c_numeric_t]:
_NUMPY_TO_CTYPES_MAPPING: Dict[Type[np.number], Type[c_numeric_t]] = {
np.float32: ctypes.c_float,
np.float64: ctypes.c_double,
np.uint32: ctypes.c_uint,
Expand Down Expand Up @@ -271,9 +273,9 @@ def _cuda_array_interface(data: DataType) -> bytes:
return interface_str


def ctypes2numpy(cptr: cptr_numeric_t, length: int, dtype: Type[np.number]) -> np.ndarray:
def ctypes2numpy(cptr: c_numeric_ptr_t, length: int, dtype: Type[np.number]) -> np.ndarray:
"""Convert a ctypes pointer array to a numpy array."""
ctype: Type[ctype_numeric_t] = _numpy2ctypes_type(dtype)
ctype: Type[c_numeric_t] = _numpy2ctypes_type(dtype)
if not isinstance(cptr, ctypes.POINTER(ctype)):
raise RuntimeError(f"expected {ctype} pointer")
res = np.zeros(length, dtype=dtype)
Expand All @@ -282,7 +284,7 @@ def ctypes2numpy(cptr: cptr_numeric_t, length: int, dtype: Type[np.number]) -> n
return res


def ctypes2cupy(cptr: cptr_numeric_t, length: int, dtype: Type[np.number]) -> cupy_t:
def ctypes2cupy(cptr: c_numeric_ptr_t, length: int, dtype: Type[np.number]) -> cupy_t:
"""Convert a ctypes pointer array to a cupy array."""
# pylint: disable=import-error
import cupy
Expand All @@ -308,7 +310,7 @@ def ctypes2cupy(cptr: cptr_numeric_t, length: int, dtype: Type[np.number]) -> cu
return arr


def ctypes2buffer(cptr: ctypes.pointer[ctypes.c_char], length: int) -> bytearray:
def ctypes2buffer(cptr: c_str_ptr_t, length: int) -> bytearray:
"""Convert ctypes pointer to buffer type."""
if not isinstance(cptr, ctypes.POINTER(ctypes.c_char)):
raise RuntimeError('expected char pointer')
Expand All @@ -332,9 +334,9 @@ def c_array(ctype: Type[ctype_t], values: ArrayLike) -> ctypes.Array:


def _prediction_output(
shape: cptr_numeric_t,
shape: c_numeric_ptr_t,
dims: c_bst_ulong,
predts: ctypes.pointer[ctypes.c_float],
predts: c_float_ptr_t,
is_cuda: bool
) -> numpy_or_cupy_t:
arr_shape = ctypes2numpy(shape, dims.value, np.uint64)
Expand Down

0 comments on commit 926bf6c

Please sign in to comment.