簡體   English   中英

我可以使用 pybind11 將 numpy 數組傳遞給接受 Eigen::Tensor 的 function 嗎?

[英]Can I use pybind11 to pass a numpy array to a function accepting a Eigen::Tensor?

我可以使用pybind1將三維 numpy 數組傳遞給 c++ function 接受Eigen::Tensor作為參數。 例如,考慮以下 c++ function:

Eigen::Tensor<double, 3> addition_tensor(Eigen::Tensor<double, 3> a,
                                         Eigen::Tensor<double, 3> b) {
    return a + b;
}

編譯 function 后,將其導入 python 並傳遞 numpy 數組np.ones((1, 2, 2))給它,我收到以下錯誤消息:

TypeError: addition_tensor(): incompatible function arguments. The following argument types are supported:
    1. (arg0: Eigen::Tensor<double, 3, 0, long>, arg1: Eigen::Tensor<double, 3, 0, long>) -> Eigen::Tensor<double, 3, 0, long>

我特別驚訝於無法傳遞三維 numpy 數組,因為我可以將二維numpy array傳遞給 function 接受: Eigen::MatrixXd

Eigen::MatrixXd addition(Eigen::MatrixXd a, Eigen::MatrixXd b) { return a + b; }

我用於此示例的整個代碼是:

#include <eigen-git-mirror/Eigen/Dense>
#include <eigen-git-mirror/unsupported/Eigen/CXX11/Tensor>
#include "pybind11/include/pybind11/eigen.h"
#include "pybind11/include/pybind11/pybind11.h"

Eigen::MatrixXd addition(Eigen::MatrixXd a, Eigen::MatrixXd b) { return a + b; }

Eigen::Tensor<double, 3> addition_tensor(Eigen::Tensor<double, 3> a,
                                         Eigen::Tensor<double, 3> b) {
    return a + b;
}

PYBIND11_MODULE(example, m) {
    m.def("addition", &addition, "A function which adds two numbers");
    m.def("addition_tensor", &addition_tensor,
          "A function which adds two numbers");
}

我用g++ -shared -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`編譯了上面的代碼。 有人知道我如何將三維numpy數組轉換為接受三維Eigen::Tensor的 function 嗎?

它不被直接支持,這里有一些討論(包括一些代碼來做映射,如果你想把它添加到你的項目中): https://github.com/pybind/pybind11/issues/1377

感謝@John Zwinck 的回答,我可以實現我想要的。 如果有人感興趣,這里是復制:

#include <eigen-git-mirror/Eigen/Dense>
#include <eigen-git-mirror/unsupported/Eigen/CXX11/Tensor>
#include "pybind11/include/pybind11/eigen.h"
#include "pybind11/include/pybind11/numpy.h"
#include "pybind11/include/pybind11/pybind11.h"

Eigen::Tensor<double, 3, Eigen::RowMajor> getTensor(
    pybind11::array_t<double> inArray) {
    // request a buffer descriptor from Python
    pybind11::buffer_info buffer_info = inArray.request();

    // extract data an shape of input array
    double *data = static_cast<double *>(buffer_info.ptr);
    std::vector<ssize_t> shape = buffer_info.shape;

    // wrap ndarray in Eigen::Map:
    // the second template argument is the rank of the tensor and has to be
    // known at compile time
    Eigen::TensorMap<Eigen::Tensor<double, 3, Eigen::RowMajor>> in_tensor(
        data, shape[0], shape[1], shape[2]);
    return in_tensor;
}

pybind11::array_t<double> return_array(
    Eigen::Tensor<double, 3, Eigen::RowMajor> inp) {
    std::vector<ssize_t> shape(3);
    shape[0] = inp.dimension(0);
    shape[1] = inp.dimension(1);
    shape[2] = inp.dimension(2);
    return pybind11::array_t<double>(
        shape,  // shape
        {shape[1] * shape[2] * sizeof(double), shape[2] * sizeof(double),
         sizeof(double)},  // strides
        inp.data());       // data pointer
}

pybind11::array_t<double> addition(pybind11::array_t<double> a,
                                   pybind11::array_t<double> b) {
    Eigen::Tensor<double, 3, Eigen::RowMajor> a_t = getTensor(a);
    Eigen::Tensor<double, 3, Eigen::RowMajor> b_t = getTensor(b);
    Eigen::Tensor<double, 3, Eigen::RowMajor> res = a_t + b_t;
    return return_array(res);
}

PYBIND11_MODULE(example, m) {
    m.def("addition", &addition, "A function which adds two numbers");
}

與約翰提到的鏈接中的建議相反,我不介意對Eigen::Tensor使用RowMajor存儲順序。 我看到這個存儲順序在tensorflow代碼中也被多次使用。 我不知道上面的代碼是否不必要地復制數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM