Skip to content

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

Merged
henryiii merged 3 commits into
pybind:masterfrom
henryiii:fix/eigen-nondivisible-stride
Nov 23, 2026
Merged

henryiii merged 3 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.

📚 Documentation preview 📚: https://pybind11--6164.org.readthedocs.build/

@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.

@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.

…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
henryiii force-pushed the fix/eigen-nondivisible-stride branch from 7aa9acc to fbb67b2 Compare November 22, 2026 14:54
@rwgk

rwgk commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

codex GPT-6.2-Sol ultra (I'll add a commit for the suggested test improvement):

I’d approve PR #6164. Henry fixed both findings from the earlier review. A const Eigen Ref with dynamic strides now copies packed or reversed arrays when Eigen can’t represent their strides. A mutable Ref can still map the original array when the odd stride belongs to a length-one axis. I reproduced both failures on the previous PR commit and verified they pass on the current head. Eigen caster · Copy path

One optional test improvement is to use m.add_any(mat["a"], 0, 2, 100.0) in the 2D mutable rejection check. The current add_rm already rejects that array because it requires C-contiguous input. I verified that add_any corrupts the neighboring packed field on the base commit and raises TypeError on the PR head. The existing tests already catch the underlying stride bug, so I wouldn’t hold up the merge for this. Test assertion

Full CI passed for the current head. Clean local builds ran the focused Eigen pytest suite on CPython 3.14.10 with the GIL and free-threaded, with 45 passed and 12 SciPy-dependent skips in each. GitHub CI

Use add_any so the packed matrix reaches Eigen's stride check instead of being refused earlier by the C-contiguous argument requirement.

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, mostly based on a codex GPT-6.2-Sol ultra review.

I'm leaving the final review and merging to @henryiii.

@henryiii henryiii self-assigned this Nov 23, 2026
@henryiii
henryiii merged commit 7c6338b into pybind:master Nov 23, 2026
94 checks passed
@henryiii
henryiii deleted the fix/eigen-nondivisible-stride branch November 23, 2026 04:44
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants