Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arrays with zero-size dimensions #58

Conversation

mwoehlke-kitware
Copy link

@mwoehlke-kitware mwoehlke-kitware commented Jun 29, 2022

When converting an array to an Eigen matrix, ignore the strides if any dimension size is 0. If the array is empty, the strides aren't relevant, and especially numpy ≥ 1.23 explicitly sets the strides to 0 in this case. (See numpy/numpy@dd5ab7b11520.)

Update tests to verify that this works, and continues to work. (The updated test would have caught the behavior change with numpy 1.23.) This expands on the tests added by #38, but also reverts the logic change from that PR, as it is no longer needed, and brings the code closer in line with upstream, since #38 was never pushed upstream.

+@EricCousineau-TRI for review, please.

See also pybind#4038.


This change is Reviewable

@jwnimmer-tri
Copy link

We can't merge patches here unless we also open a Drake PR that refers to this PR, to prove that Drake CI passes (not just RobotLocomotion/pybind11 CI). Please open the Drake PR and mention it here.

mwoehlke-kitware added a commit to mwoehlke-kitware/drake that referenced this pull request Jun 29, 2022
Update to newer pybind11 to address a change in numpy 1.23 that caused
some arrays to fail to be converted to Eigen matrices.

See RobotLocomotion/pybind11#58.
@mwoehlke-kitware
Copy link
Author

Drake CI succeeded for RobotLocomotion/drake#17475. Particularly:

...which should be using numpy 1.23 and thus would be failing without this change.

Meanwhile, upstream merged a version with (non-functional) changes. I'd rather not wait on all the CI to re-run; is it going to be a problem for whoever ends up merging upstream to remember to just take their version, or should I tweak this to more closely match the versions that's now upstream?

@jwnimmer-tri
Copy link

Definitely our patch should be as similar to upstream as possible. If the timeline means foregoing another spin of Drake CI, so be it.

Copy link

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

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

:lgtm: rubber stamp, pending any re-sync with the upstream PR.

When converting an array to an Eigen matrix, ignore the strides if any
dimension size is 0. If the array is empty, the strides aren't relevant,
and especially numpy ≥ 1.23 explicitly sets the strides to 0 in this
case. (See numpy commit dd5ab7b11520.)

Update tests to verify that this works, and continues to work. (This
test likely would have caught the behavior change with numpy 1.23.) This
expands on the tests added by pybind#38, but also reverts the logic change
from that PR, as it is no longer needed, and brings the code closer in
line with upstream, since pybind#38 was never pushed upstream.
@mwoehlke-kitware
Copy link
Author

Definitely our patch should be as similar to upstream as possible.

Okay, done. It should be easy enough to verify in Reviewable that there is no functional change, just aesthetic changes. Once this lands, I'll update the Drake PR to point to the post-merge SHA.

@mwoehlke-kitware
Copy link
Author

...or it would have been if Reviewable had kept the previous version. Since it looks like it didn't, here's that diff:

-        if (rows == 0 || cols == 0)
-            return !negativestrides;
-        return
-            !negativestrides &&
-            (props::inner_stride == Eigen::Dynamic || props::inner_stride == stride.inner() ||
-                (EigenRowMajor ? cols : rows) == 1) &&
-            (props::outer_stride == Eigen::Dynamic || props::outer_stride == stride.outer() ||
-                (EigenRowMajor ? rows : cols) == 1);
+        if (negativestrides) {
+            return false;
+        }
+        if (rows == 0 || cols == 0) {
+            return true;
+        }
+        return (props::inner_stride == Eigen::Dynamic || props::inner_stride == stride.inner()
+                || (EigenRowMajor ? cols : rows) == 1)
+               && (props::outer_stride == Eigen::Dynamic || props::outer_stride == stride.outer()
+                   || (EigenRowMajor ? rows : cols) == 1);

Note also that everything starting with (props::inner_stride differs only in whitespace.

@jwnimmer-tri jwnimmer-tri merged commit 25eac33 into RobotLocomotion:drake Jun 29, 2022
@mwoehlke-kitware mwoehlke-kitware deleted the drake-fix-zero-size-arrays branch June 29, 2022 20:25
mwoehlke-kitware added a commit to mwoehlke-kitware/drake that referenced this pull request Jun 29, 2022
Update to newer pybind11 to address a change in numpy 1.23 that caused
some arrays to fail to be converted to Eigen matrices.

See RobotLocomotion/pybind11#58.
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