From 80c27d65c6a7e3b58781343ddca3db48c9c7b6f9 Mon Sep 17 00:00:00 2001 From: jakirkham Date: Wed, 31 Jul 2024 15:23:15 -0700 Subject: [PATCH] Drop NumPy build dependency (#751) Partially addresses issue: https://github.com/rapidsai/build-planning/issues/82 Even though cuCIM currently `#include`s ``, the actual C++ code appears not to use NumPy. So this attempts to drop the header and the NumPy build dependency. Authors: - https://github.com/jakirkham Approvers: - Mike Sarahan (https://github.com/msarahan) - Gigon Bae (https://github.com/gigony) URL: https://github.com/rapidsai/cucim/pull/751 --- conda/recipes/cucim/meta.yaml | 3 +-- python/pybind11/cucim_py.cpp | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/conda/recipes/cucim/meta.yaml b/conda/recipes/cucim/meta.yaml index 47768a92f..ed800d086 100644 --- a/conda/recipes/cucim/meta.yaml +++ b/conda/recipes/cucim/meta.yaml @@ -63,7 +63,6 @@ requirements: {% endif %} - cupy >=12.0.0 - libcucim ={{ version }} - - numpy 1.23 - python - rapids-build-backend >=0.3.0,<0.4.0.dev0 - scikit-image >=0.19.0,<0.23.0a0 @@ -74,7 +73,7 @@ requirements: {% if cuda_major != "11" %} - cuda-cudart {% endif %} - - {{ pin_compatible('numpy') }} + - numpy >=1.23,<2.0a0 - click - cupy >=12.0.0 - lazy_loader >=0.1 diff --git a/python/pybind11/cucim_py.cpp b/python/pybind11/cucim_py.cpp index 62f041e2d..bb91b5c43 100644 --- a/python/pybind11/cucim_py.cpp +++ b/python/pybind11/cucim_py.cpp @@ -19,9 +19,11 @@ #include #include -#include +#include +#include #include #include +#include #include #include @@ -445,11 +447,30 @@ py::object py_read_region(const CuImage& cuimg, { py::gil_scoped_acquire scope_guard; - auto arr = pybind11::array_t::ensure(location); - if (arr) // fast copy + py::object mv_obj = py::none(); + try { - py::buffer_info buf = arr.request(); - int64_t* data_array = static_cast(buf.ptr); + mv_obj = py::cast(py::memoryview(location)); + } + catch (const std::exception& e) + { + } + + if (!mv_obj.is_none()) // fast copy + { + py::memoryview mv(mv_obj); + py::buffer_info buf(PyMemoryView_GET_BUFFER(mv.ptr()), false); + + if (buf.format != py::format_descriptor::format()) + { + throw std::invalid_argument("Expected int64 array-like"); + } + if (PyBuffer_IsContiguous(buf.view(), 'C') == 0) + { + throw std::invalid_argument("Expected C-contiguous array-like"); + } + + const int64_t* data_array = static_cast(buf.ptr); ssize_t data_size = buf.size; locations.reserve(data_size); locations.insert(locations.end(), &data_array[0], &data_array[data_size]);