Add support for Eigen's Tensor module#1320
Conversation
481b4b4 to
3b55efb
Compare
+ for const refs, falls back to Plain tensor caster
|
Hi @ManifoldFR, this looks great! I responded to one comment inline. A few other thoughts: Test coverage seems a bit less than the tensor module of pybind11. I recommend taking a look at that and recovering useful bits. Are you testing special cases like tensors with length zero along an axis, and scalars (ndim=0)? Why are there GC calls in the test suite? I don't think you are checking that things are actually deallocated. Thanks, |
|
Thanks for the feedback Wenzel. I can't see your reply in the comment? Might be a GitHub bug. I didn't realize pybind11 had official Tensor support, I'll checked out their code. As for the gc test, I merely replicated what's done in the current Eigen dense test suite: Lines 207 to 223 in cfbb02f I wrote a few more comprehensive tests (casts, 0-dim tensor and tensors with zeroes in one of the sizes). |
a3466ed to
ffb6d3e
Compare
ffb6d3e to
cfd2b71
Compare
|
Thank you @ManifoldFR. One more thing: can you explanation on how to use the Eigen type casters to |
|
I think you can remove the gc.collect() calls from the Eigen test suite. They are likely leftovers from a debugging session where I checked for leaks at the end. |
| types are all supported, and map to `numpy.ndarray` with the appropriate sizes. | ||
| Both column-major and row-major tensors are supported. | ||
|
|
||
| Remark taking non-contiguous NumPy arrays as arguments is not supported. |
There was a problem hiding this comment.
Was this meant to be a .. remark RST block? Probably it's overkill, you can just merge that with the previous paragraph. Are non-contiguous arrays just unsupported for Map/Ref or also for the base tensor?
There was a problem hiding this comment.
Non-contiguous arrays are not supported for tensor maps/refs nor the base tensor type, because these types themselves are contiguous in Eigen. I'll edit the text.
There was a problem hiding this comment.
A question about that: if you pass a non-contiguous matrix or vector to a function taking Eigen::Matrix or Eigen::Vector, nanobind will AFAIK make a contiguous copy if implicit conversions are allowed. This would be a surprising deviation from the vector/matrix case. nanobind's underlying ndarray infrastructure can do this automatically, so I'm wondering where the difference comes from (and if it is intentional)
There was a problem hiding this comment.
You're right, I just tested taking a slice on a 3d tensor and passing it to a function taking a const reference to a plain tensor type, and it worked; I'll add a test case.
There was a problem hiding this comment.
Sounds good. In that case, this comment is moot, right? "Note that taking
non-contiguous NumPy arrays as arguments is not supported for any of these types."
It would be also worth adding a test (if not already present) that Map and Ref (less sure about this one) refuse such a tensor. At least for Map, the idea is IIRC that it wraps a data buffer without copies, so implicit conversions are very much not wanted.
There was a problem hiding this comment.
I've added the relevant pytest.raises tests and updated the comment in eigen.rst.
I know that TensorRef can hold a reference to the striding expression/operation but that needs a base Eigen Tensor/TensorMap to start from. Perhaps someone could do something with this (with some kind of custom op replicating it with an external pointer?)
|
I left one more comment. All good after this? (i.e., ready to merge?) |
d61a6ac to
9c28676
Compare
After this, all good :) |
|
Thank you for your hard work @ManifoldFR! |
|
Thanks for merging this!! This will be useful for our big upgrade to nanobind 😄 |
This PR adds support for the types in Eigen's "unsupported" Tensor module, from the header
<unsupported/Eigen/CXX11/Tensor>(we keepCXX11/in the include to support Eigen3).To-do list
Eigen::TensortypesEigen::TensorMap<T>Eigen::TensorRef<T>Background
We are contributing this upstream as it is part of the porting of Pinocchio over to nanobind 😄