Skip to content

Commit

Permalink
Only use functions in the limited API (#5871)
Browse files Browse the repository at this point in the history
This PR removes usage of the only method in raft's Cython that is not part of the Python limited API. Contributes to rapidsai/build-planning#42

Authors:
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Dante Gama Dessavre (https://github.com/dantegd)

URL: #5871
  • Loading branch information
vyasr authored Apr 30, 2024
1 parent de181d8 commit 3c1a72c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
1 change: 0 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ option(SINGLEGPU "Disable all mnmg components and comms libraries" OFF)
set(CUML_RAFT_CLONE_ON_PIN OFF)



# todo: use CMAKE_MESSAGE_CONTEXT for prefix for logging.
# https://github.com/rapidsai/cuml/issues/4843
message(VERBOSE "CUML_PY: Build only cuML CPU Python components.: ${CUML_CPU}")
Expand Down
21 changes: 13 additions & 8 deletions python/cuml/ensemble/randomforest_shared.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,9 @@

# distutils: language = c++

from cpython.buffer cimport PyObject_GetBuffer, PyBuffer_Release, PyBUF_FULL_RO

from libcpp.vector cimport vector
from cpython.object cimport PyObject
from libc.stdint cimport uintptr_t
from libcpp.memory cimport unique_ptr
from typing import Dict, List, Union
Expand All @@ -37,9 +38,6 @@ cdef extern from "treelite/tree.h" namespace "treelite":
@staticmethod
unique_ptr[Model] DeserializeFromPyBuffer(const vector[TreelitePyBufferFrame] &) except +

cdef extern from "Python.h":
Py_buffer* PyMemoryView_GET_BUFFER(PyObject* mview)

cdef class PyBufferFrameWrapper:
cdef TreelitePyBufferFrame _handle
cdef Py_ssize_t shape[1]
Expand Down Expand Up @@ -92,18 +90,25 @@ def get_frames(model: uintptr_t) -> List[memoryview]:
def init_from_frames(frames: List[np.ndarray],
format_str: List[str], itemsize: List[int]) -> uintptr_t:
cdef vector[TreelitePyBufferFrame] cpp_frames
# Need to keep track of the buffers to release them later.
cdef vector[Py_buffer] buffers
cdef Py_buffer* buf
cdef TreelitePyBufferFrame cpp_frame
format_bytes = [s.encode('utf-8') for s in format_str]
for i, frame in enumerate(frames):
x = memoryview(frame)
buf = PyMemoryView_GET_BUFFER(<PyObject*>x)
buffers.emplace_back()
buf = &buffers.back()
PyObject_GetBuffer(frame, buf, PyBUF_FULL_RO)
cpp_frame.buf = buf.buf
cpp_frame.format = format_bytes[i]
cpp_frame.itemsize = itemsize[i]
cpp_frame.nitem = buf.len // itemsize[i]
cpp_frames.push_back(cpp_frame)
return <uintptr_t> _init_from_frames(cpp_frames)
output = <uintptr_t> _init_from_frames(cpp_frames)
cdef int j
for j in range(buffers.size()):
PyBuffer_Release(&buffers[j])
return output


def treelite_serialize(
Expand Down

0 comments on commit 3c1a72c

Please sign in to comment.