Skip to content

fix(eigen): reject strides that are not a multiple of the scalar size - #6164

Open
henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/eigen-nondivisible-stride
Open

henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/eigen-nondivisible-stride

Conversation

@henryiii

@henryiii henryiii commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

EigenProps::conformable divided the numpy byte strides by sizeof(Scalar) with integer
division. A stride that is not a whole number of scalars was truncated. A field of a packed
structured array (for example np.zeros(4, dtype=[('a', 'f8'), ('b', 'i1')])['a'], which has a
stride of 9 bytes) was thus seen as contiguous, and a mutable Eigen::Ref read and wrote the
neighbouring field.

The conformability result now records such strides. A mutable Eigen::Ref refuses the array, and
by-value and const Ref arguments copy it, as they already do for negative strides. The stride of
an axis of length 1 is not used, so it is not checked.

A const Ref with fully dynamic strides (py::EigenDRef<const M>) did not copy, because
array_t::ensure returns the source array when the dtype is correct. The caster now forces a
copy in that case. This also fixes the same failure for negative strides.

Fixes item 5 of #6159.

Suggested changelog entry:

  • Fixed Eigen matrix casters accepting numpy arrays whose stride is not a multiple of the scalar
    size, which let a mutable Eigen::Ref corrupt neighbouring data. A const Eigen::Ref
    with fully dynamic strides now copies such arrays, and arrays with negative strides, instead of
    raising TypeError.

@rwgk

rwgk commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

I checked both cases below on with-GIL and free-threaded CPython 3.14.10 with NumPy 2.5.5.

  • Force a copy for a const reference with fully dynamic strides. A binding taking py::EigenDRef<const Eigen::MatrixXd> raises TypeError on a packed float64 field with shape (2, 2) and byte strides (18, 9). For this reference, Array::ensure(src) can return the same array because its dtype already matches and both strides are dynamic. The second stride check rejects it. The base read the wrong value as expected from the bug being fixed. To support the intended const-reference conversion, force a copy on this path and test a fully dynamic const reference. Copy and second stride check
  • Keep safely mappable singleton axes working. A float64 array of shape (4, 1) with byte strides (8, 1) was accepted by a mutable py::EigenDRef<Eigen::MatrixXd> on the base, and incrementing (1, 0) changed the correct element in the shared buffer. The PR rejects it because the odd stride is on the singleton column axis. That axis is never stepped over. Consider ignoring non-multiple strides on singleton axes, as the existing compatibility check does for ordinary stride mismatches. Byte stride detection · Singleton compatibility

Ralf has yet to review these findings.

…ngth-1 axis

A const Ref with fully dynamic strides got the source array back from
ensure() and then failed the stride check. Force a copy in that case.
This also lets such a Ref accept an array with negative strides.

A stride that is not a multiple of the scalar size is now ignored on an
axis of length 1, because that stride is never used.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

🤖 AI text below 🤖

I reproduced both points on CPython 3.14.7 with NumPy 2.5.5. Both are fixed in 7aa9acc.

  • Const reference with fully dynamic strides: Confirmed. Array::ensure returned the source array, and the second stride check rejected it. The caster now calls PyArray_NewCopy when the result of ensure is conformable but not stride compatible. The same path failed for negative strides before this PR (matrix_multiply(a[::-1, ::-1], ...) raised TypeError), so that case is fixed too. The test uses the existing matrix_multiply binding for both.
  • Singleton axes: Confirmed. Shape (4, 1) with strides (8, 1) was rejected by add_any. badstrides is now set only for an axis of length > 1. The test covers (4, 1), (1, 4) and a 1-D array of length 1 with a mutable py::EigenDRef<Eigen::MatrixXd>.

The new assertions failed before the fix. I updated the PR description and the changelog entry.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants