Skip to content

Commit b68959e

Browse files
jagermandean0x7d
authored andcommitted
Use numpy rather than Eigen for copying
We're current copy by creating an Eigen::Map into the input numpy array, then assigning that to the basic eigen type, effectively having Eigen do the copy. That doesn't work for negative strides, though: Eigen doesn't allow them. This commit makes numpy do the copying instead by allocating the eigen type, then having numpy copy from the input array into a numpy reference into the eigen object's data. This also saves a copy when type conversion is required: numpy can do the conversion on-the-fly as part of the copy. Finally this commit also makes non-reference parameters respect the convert flag, declining the load when called in a noconvert pass with a convertible, but non-array input or an array with the wrong dtype.
1 parent 627da3f commit b68959e

File tree

7 files changed

+43
-42
lines changed

7 files changed

+43
-42
lines changed

docs/advanced/pycpp/numpy.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ completely avoid copy operations with Python expressions like
4141
py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
4242
2, /* Number of dimensions */
4343
{ m.rows(), m.cols() }, /* Buffer dimensions */
44-
{ (ssize_t)( sizeof(float) * m.rows() ),/* Strides (in bytes) for each index */
45-
(ssize_t)( sizeof(float) ) }
44+
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
45+
sizeof(float) }
4646
);
4747
});
4848
@@ -118,11 +118,10 @@ as follows:
118118
/* Number of dimensions */
119119
2,
120120
/* Buffer dimensions */
121-
{ (size_t) m.rows(),
122-
(size_t) m.cols() },
121+
{ m.rows(), m.cols() },
123122
/* Strides (in bytes) for each index */
124-
{ (ssize_t)( sizeof(Scalar) * (rowMajor ? m.cols() : 1) ),
125-
(ssize_t)( sizeof(Scalar) * (rowMajor ? 1 : m.rows()) ) }
123+
{ sizeof(Scalar) * (rowMajor ? m.cols() : 1),
124+
sizeof(Scalar) * (rowMajor ? 1 : m.rows()) }
126125
);
127126
})
128127

include/pybind11/buffer_info.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ struct buffer_info {
2121
std::string format; // For homogeneous buffers, this should be set to format_descriptor<T>::format()
2222
size_t ndim = 0; // Number of dimensions
2323
std::vector<size_t> shape; // Shape of the tensor (1 entry per dimension)
24-
std::vector<size_t> strides; // Number of entries between adjacent entries (for each per dimension)
24+
std::vector<ssize_t> strides; // Number of entries between adjacent entries (for each per dimension)
2525

2626
buffer_info() { }
2727

2828
buffer_info(void *ptr, size_t itemsize, const std::string &format, size_t ndim,
29-
detail::any_container<size_t> shape_in, detail::any_container<size_t> strides_in)
29+
detail::any_container<size_t> shape_in, detail::any_container<ssize_t> strides_in)
3030
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
3131
shape(std::move(shape_in)), strides(std::move(strides_in)) {
3232
if (ndim != shape.size() || ndim != strides.size())

include/pybind11/class_support.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
433433
#endif
434434
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
435435
type->tp_dictoffset = type->tp_basicsize; // place dict at the end
436-
type->tp_basicsize += sizeof(PyObject *); // and allocate enough space for it
436+
type->tp_basicsize += (Py_ssize_t)sizeof(PyObject *); // and allocate enough space for it
437437
type->tp_traverse = pybind11_traverse;
438438
type->tp_clear = pybind11_clear;
439439

@@ -459,16 +459,18 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla
459459
view->ndim = 1;
460460
view->internal = info;
461461
view->buf = info->ptr;
462-
view->itemsize = (ssize_t) info->itemsize;
462+
view->itemsize = (Py_ssize_t) info->itemsize;
463463
view->len = view->itemsize;
464464
for (auto s : info->shape)
465-
view->len *= s;
465+
view->len *= (Py_ssize_t) s;
466466
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
467467
view->format = const_cast<char *>(info->format.c_str());
468468
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
469469
view->ndim = (int) info->ndim;
470-
view->strides = (ssize_t *) &info->strides[0];
471-
view->shape = (ssize_t *) &info->shape[0];
470+
view->strides = &info->strides[0];
471+
// Next is a pointer cast, let's make sure it's safe.
472+
static_assert(sizeof(Py_ssize_t)==sizeof(info->shape[0]), "sizeof(Py_ssize_t) != sizeof(size_t)");
473+
view->shape = (Py_ssize_t *) &info->shape[0];
472474
}
473475
Py_INCREF(view->obj);
474476
return 0;

include/pybind11/eigen.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,14 @@ struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
249249
using Scalar = typename Type::Scalar;
250250
using props = EigenProps<Type>;
251251

252-
bool load(handle src, bool) {
253-
auto buf = array_t<Scalar>::ensure(src);
252+
bool load(handle src, bool convert) {
253+
// If we're in no-convert mode, only load if given an array of the correct type
254+
if (!convert && !isinstance<array_t<Scalar>>(src))
255+
return false;
256+
257+
// Coerce into an array, but don't do type conversion yet; the copy below handles it.
258+
auto buf = array::ensure(src);
259+
254260
if (!buf)
255261
return false;
256262

@@ -259,25 +265,16 @@ struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
259265
return false;
260266

261267
auto fits = props::conformable(buf);
262-
if (!fits)
263-
return false; // Non-comformable vector/matrix types
268+
// Allocate the new type, then build a numpy reference into it
269+
value = Type(fits.rows, fits.cols);
270+
auto ref = reinterpret_steal<array>(eigen_ref_array<props>(value));
271+
if (dims == 1) ref = ref.squeeze();
264272

265-
if (fits.negativestrides) {
266-
267-
// Eigen does not support negative strides, so we need to make a copy here with normal strides.
268-
// TODO: when Eigen bug #747 is fixed, remove this if case, always execute the else part.
269-
// http://eigen.tuxfamily.org/bz/show_bug.cgi?id=747
270-
auto buf2 = array_t<Scalar,array::forcecast || array::f_style>::ensure(src);
271-
if (!buf2)
272-
return false;
273-
// not checking sizes, we already did that
274-
fits = props::conformable(buf2);
275-
value = Eigen::Map<const Type, 0, EigenDStride>(buf2.data(), fits.rows, fits.cols, fits.stride);
276-
277-
} else {
278-
279-
value = Eigen::Map<const Type, 0, EigenDStride>(buf.data(), fits.rows, fits.cols, fits.stride);
273+
int result = detail::npy_api::get().PyArray_CopyInto_(ref.ptr(), buf.ptr());
280274

275+
if (result < 0) { // Copy failed!
276+
PyErr_Clear();
277+
return false;
281278
}
282279

283280
return true;

include/pybind11/numpy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ struct npy_api {
152152
(PyTypeObject *, PyObject *, int, Py_intptr_t *,
153153
Py_intptr_t *, void *, int, PyObject *);
154154
PyObject *(*PyArray_DescrNewFromType_)(int);
155+
int (*PyArray_CopyInto_)(PyObject *, PyObject *);
155156
PyObject *(*PyArray_NewCopy_)(PyObject *, int);
156157
PyTypeObject *PyArray_Type_;
157158
PyTypeObject *PyVoidArrType_Type_;
@@ -175,6 +176,7 @@ struct npy_api {
175176
API_PyArray_DescrFromScalar = 57,
176177
API_PyArray_FromAny = 69,
177178
API_PyArray_Resize = 80,
179+
API_PyArray_CopyInto = 82,
178180
API_PyArray_NewCopy = 85,
179181
API_PyArray_NewFromDescr = 94,
180182
API_PyArray_DescrNewFromType = 9,
@@ -205,6 +207,7 @@ struct npy_api {
205207
DECL_NPY_API(PyArray_DescrFromScalar);
206208
DECL_NPY_API(PyArray_FromAny);
207209
DECL_NPY_API(PyArray_Resize);
210+
DECL_NPY_API(PyArray_CopyInto);
208211
DECL_NPY_API(PyArray_NewCopy);
209212
DECL_NPY_API(PyArray_NewFromDescr);
210213
DECL_NPY_API(PyArray_DescrNewFromType);

include/pybind11/stl_bind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ vector_buffer(Class_& cl) {
358358
vec.reserve(info.shape[0]);
359359
T *p = static_cast<T*>(info.ptr);
360360
auto step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
361-
T *end = p + info.shape[0] * step;
361+
T *end = p + static_cast<ssize_t>(info.shape[0]) * step;
362362
for (; p < end; p += step)
363363
vec.push_back(*p);
364364
});

tests/test_numpy_array.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ using arr_t = py::array_t<uint16_t, 0>;
2020
static_assert(std::is_same<arr_t::value_type, uint16_t>::value, "");
2121

2222
template<typename... Ix> arr data(const arr& a, Ix... index) {
23-
return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...));
23+
return arr(a.nbytes() - size_t(a.offset_at(index...)), (const uint8_t *) a.data(index...));
2424
}
2525

2626
template<typename... Ix> arr data_t(const arr_t& a, Ix... index) {
27-
return arr(a.size() - a.index_at(index...), a.data(index...));
27+
return arr(a.size() - size_t(a.index_at(index...)), a.data(index...));
2828
}
2929

3030
arr& mutate_data(arr& a) {
@@ -43,23 +43,23 @@ arr_t& mutate_data_t(arr_t& a) {
4343

4444
template<typename... Ix> arr& mutate_data(arr& a, Ix... index) {
4545
auto ptr = (uint8_t *) a.mutable_data(index...);
46-
for (size_t i = 0; i < a.nbytes() - a.offset_at(index...); i++)
46+
for (size_t i = 0; i < a.nbytes() - size_t(a.offset_at(index...)); i++)
4747
ptr[i] = (uint8_t) (ptr[i] * 2);
4848
return a;
4949
}
5050

5151
template<typename... Ix> arr_t& mutate_data_t(arr_t& a, Ix... index) {
5252
auto ptr = a.mutable_data(index...);
53-
for (size_t i = 0; i < a.size() - a.index_at(index...); i++)
53+
for (size_t i = 0; i < a.size() - size_t(a.index_at(index...)); i++)
5454
ptr[i]++;
5555
return a;
5656
}
5757

58-
template<typename... Ix> size_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); }
59-
template<typename... Ix> size_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); }
60-
template<typename... Ix> size_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); }
61-
template<typename... Ix> size_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); }
62-
template<typename... Ix> size_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); }
58+
template<typename... Ix> py::ssize_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); }
59+
template<typename... Ix> py::ssize_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); }
60+
template<typename... Ix> py::ssize_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); }
61+
template<typename... Ix> py::ssize_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); }
62+
template<typename... Ix> py::ssize_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); }
6363
template<typename... Ix> arr_t& mutate_at_t(arr_t& a, Ix... idx) { a.mutable_at(idx...)++; return a; }
6464

6565
#define def_index_fn(name, type) \

0 commit comments

Comments
 (0)