Skip to content

Commit

Permalink
Drop NumPy build dependency (#751)
Browse files Browse the repository at this point in the history
Partially addresses issue: rapidsai/build-planning#82

Even though cuCIM currently `#include`s `<pybind11/numpy.h>`, 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: #751
  • Loading branch information
jakirkham authored Jul 31, 2024
1 parent 0e75c76 commit 80c27d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
3 changes: 1 addition & 2 deletions conda/recipes/cucim/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
31 changes: 26 additions & 5 deletions python/pybind11/cucim_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

#include <fmt/format.h>
#include <fmt/ranges.h>
#include <pybind11/numpy.h>
#include <Python.h>
#include <pybind11/buffer_info.h>
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <pybind11/stl.h>

#include <cucim/cuimage.h>
Expand Down Expand Up @@ -445,11 +447,30 @@ py::object py_read_region(const CuImage& cuimg,
{
py::gil_scoped_acquire scope_guard;

auto arr = pybind11::array_t<int64_t, py::array::c_style | py::array::forcecast>::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<int64_t*>(buf.ptr);
mv_obj = py::cast<py::object>(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<int64_t>::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<const int64_t*>(buf.ptr);
ssize_t data_size = buf.size;
locations.reserve(data_size);
locations.insert(locations.end(), &data_array[0], &data_array[data_size]);
Expand Down

0 comments on commit 80c27d6

Please sign in to comment.