Skip to content

Conversation

vlarsson
Copy link
Collaborator

Playing around a bit with dense matches it seems that the pybind is quite slow. Adding this file (stolen from pycolmap) seem to improve runtime a bit. Without it there seems to be a 4-6ms overhead calling from python with 10k matches.

Maybe @sarlinpe or @Phil26AT have some more insight. In particular if anything needs to be adapted, or could be removed.

@pablospe
Copy link
Collaborator

pablospe commented Oct 22, 2024

Maybe try using nanobind. Nanobind is created by the same author as pybind11, which we currently use without any fancy features. Therefore, nanobind should be a drop-in replacement that provides the same core functionality.

Porting guide:
https://nanobind.readthedocs.io/en/latest/porting.html

Comment on lines 51 to 86
// Autocast from numpy.ndarray to std::vector<Eigen::Vector>
template <typename Scalar, int Size>
struct type_caster<std::vector<Eigen::Matrix<Scalar, Size, 1>>> {
public:
using MatrixType =
typename Eigen::Matrix<Scalar, Eigen::Dynamic, Size, Eigen::RowMajor>;
using VectorType = typename Eigen::Matrix<Scalar, Size, 1>;
using props = EigenProps<MatrixType>;
PYBIND11_TYPE_CASTER(std::vector<VectorType>, props::descriptor);

bool load(handle src, bool) {
const auto buf = array::ensure(src);
if (!buf) {
return false;
}
const buffer_info info = buf.request();
if (info.ndim != 2 || info.shape[1] != Size) {
return false;
}
const size_t num_elements = info.shape[0];
value.resize(num_elements);
const auto& mat = src.cast<Eigen::Ref<const MatrixType>>();
Eigen::Map<MatrixType>(
reinterpret_cast<Scalar*>(value.data()), num_elements, Size) = mat;
return true;
}

static handle cast(const std::vector<VectorType>& vec,
return_value_policy /* policy */,
handle h) {
Eigen::Map<const MatrixType> mat(
reinterpret_cast<const Scalar*>(vec.data()), vec.size(), Size);
return type_caster<Eigen::Map<const MatrixType>>::cast(
mat, return_value_policy::copy, h);
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

You only need this code block, which should indeed speed up all conversions to std::vector<Eigen::Vector>. I'd appreciate due credit being given since it is copied from COLMAP and I wrote it :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perfect, thanks! Of course, I'll add the proper attribution before merging it :)

@vlarsson vlarsson marked this pull request as ready for review November 23, 2024 15:49
@vlarsson vlarsson merged commit 324b74e into master Dec 14, 2024
1 check passed
@vlarsson vlarsson deleted the pybind11_extension branch December 14, 2024 12:24
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.

3 participants