[BUG]: Eigen::SparseMatrix -> unexpected overload-resolution / template-instantiation

未关闭
#5,283 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp, python
领域
api

调研方向

从链接的可复现示例以及 tests/test_basic.py 中展示的行为入手。比较 Eigen 的 Dense 和 Sparse 重载注册,包括 RowMajor 和 ColMajor 的情况,并追踪为什么 Sparse 调用报告的是 bool 而不是 float。完成的标准是 Sparse 重载分派保留输入标量类型,并且有一个回归测试覆盖所报告的情况。

由索引模型根据 Issue 内容生成。

描述

triage
Required prerequisites
What version (or hash if on master) of pybind11 are you using?

723307283ed7dc03c901fba1da1127c7b16a4a0d

Problem description

I'm trying to wrap dense as well as sparse-matrices (py -> c++): specifically

  • Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>
  • Eigen::SparseMatrix<T, Eigen::RowMajor>
  • Eigen::SparseMatrix<T, Eigen::ColMajor>

Having tried two approaches, one being based on EigenBase<Derived>, the other being based on splitting the target functions into a dense and a sparse variant (as shown in example).

In both cases, something unexpected is happening to the type in the sparse-case while the dense case works as expected.

tests/test_basic.py
 Dense matrix @ python
[[1. 2. 3. 4.]
[5. 6. 7. 8.]]
float32

Sparse matrix @ python
  (0, 0)        1.0
  (0, 1)        2.0
  (0, 2)        3.0
  (0, 3)        4.0
  (1, 0)        5.0
  (1, 1)        6.0
  (1, 2)        7.0
  (1, 3)        8.0
float32

CALL instantiation -> with dense => c++ receives type?
Dense overload
float
CALL instantiation -> with sparse => c++ receives type?
Sparse overload
bool ??? WHY ???

Reproducible example: https://github.com/sschnug/cmake_example/tree/eigen_sparse_matrix_overload_issue_example

Reproducible example code
See: https://github.com/sschnug/cmake_example/tree/eigen_sparse_matrix_overload_issue_example

But basically:

// OKAY BEHAVIOUR -> dense
template <typename ScalarT>
static void take_matrix(const Eigen::Array<ScalarT, Eigen::Dynamic, Eigen::Dynamic> matrix_in) {
  std::cout << "Dense overload" << std::endl;

  if constexpr(std::is_same_v<ScalarT, float>) {
    std::cout << "float" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, double>) {
    std::cout << "double" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, bool>) {
    std::cout << "bool" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int8_t>) {
    std::cout << "int8_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int16_t>) {
    std::cout << "int16_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int32_t>) {
    std::cout << "int32_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int64_t>) {
    std::cout << "int64_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint8_t>) {
    std::cout << "uint8_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint16_t>) {
    std::cout << "uint16_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint32_t>) {
    std::cout << "uint32_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint64_t>) {
    std::cout << "uint64_t" << std::endl;
  }
  else 
    std::cout << "some other type" << std::endl;
}

// NOT OKAY BEHAVIOUR -> sparse
template<typename ScalarT, int Options>
static void take_matrix(const Eigen::SparseMatrix<ScalarT, Options> matrix_in) {
  std::cout << "Sparse overload" << std::endl;

  if constexpr(std::is_same_v<ScalarT, float>) {
    std::cout << "float" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, double>) {
    std::cout << "double" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, bool>) {
    std::cout << "bool" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int8_t>) {
    std::cout << "int8_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int16_t>) {
    std::cout << "int16_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int32_t>) {
    std::cout << "int32_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, int64_t>) {
    std::cout << "int64_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint8_t>) {
    std::cout << "uint8_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint16_t>) {
    std::cout << "uint16_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint32_t>) {
    std::cout << "uint32_t" << std::endl;
  }
  else if constexpr(std::is_same_v<ScalarT, uint64_t>) {
    std::cout << "uint64_t" << std::endl;
  }
  else 
    std::cout << "some other type" << std::endl;
}

PYBIND11_MODULE(cmake_example, m) {

    // dense
    // -----

    m.def("take_matrix", &take_matrix<bool>, pybind11::arg("matrix_in").noconvert());
    m.def("take_matrix", &take_matrix<float>, pybind11::arg("matrix_in").noconvert());
    m.def("take_matrix", &take_matrix<double>, pybind11::arg("matrix_in").noconvert());

    // sparse
    // ------

    m.def("take_matrix", &take_matrix<bool, Eigen::ColMajor>, pybind11::arg("matrix_in").noconvert());
    m.def("take_matrix", &take_matrix<bool, Eigen::RowMajor>, pybind11::arg("matrix_in").noconvert());

    m.def("take_matrix", &take_matrix<float, Eigen::ColMajor>, pybind11::arg("matrix_in").noconvert());
    m.def("take_matrix", &take_matrix<float, Eigen::RowMajor>, pybind11::arg("matrix_in").noconvert());

    m.def("take_matrix", &take_matrix<double, Eigen::ColMajor>, pybind11::arg("matrix_in").noconvert());
    m.def("take_matrix", &take_matrix<double, Eigen::RowMajor>, pybind11::arg("matrix_in").noconvert());
Is this a regression? Put the last known working version here if it is.

Not a regression

主要语言
C++
星标
18k
派生
2.3k
平均合并
5 天 7 小时
30 天内合并 PR
53

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pybind/pybind11 的其他 Issue

查看 pybind/pybind11 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。