bazel-pybind-pytorch-undefined符号

zengzsys  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(307)

尝试用bazel、pybind、pytorch/libtorch编写玩具示例失败,在导入自定义库时出现以下错误

import libTest
undefined symbol: _ZN3c1019UndefinedTensorImpl10_singletonE

bazel工作区:

http_archive(
    name = "pytorch_cpu",
    build_file = "//tools:pytorch_cpu.BUILD",
    sha256 = "6b99edc046f37ad37a3b04dc86663895f10c362af09fdd10884f30360d0ba023",
    urls = [
        "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.9.0%2Bcpu.zip"
    ],
)

bazel构建文件

package(default_visibility = ["//visibility:public"])

cc_library(
    name = "pytorch_cpu",
    includes = ["libtorch/include/torch/csrc/api/include", "libtorch/include"],
    hdrs = glob([
      "libtorch/include/**"
    ]),
    srcs = glob([
      "libtorch/lib/*",
    ]),
)

还有pybind中的玩具示例


# include <pybind11/pybind11.h>

# include <pybind11/stl.h>

# include <pybind11/numpy.h>

# include <pybind11/pytypes.h>

# include <torch/torch.h>

# include <torch/csrc/utils/pybind.h>

namespace py = pybind11;

torch::Tensor myUsefulMethod() 
{
  return torch::zeros({62,1});
}

PYBIND11_MODULE(libTest, m) {

  m.def("myUsefulMethod", &myUsefulMethod, "myUsefulMethod");

  #ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
  #else
    m.attr("__version__") = "dev";
  #endif
}

# endif

建成

cc_library(
    name = "Test",
    srcs = ["Test.cpp"],
    deps = [
        "@pybind11",
        "@pytorch_cpu"
    ],
)

这里可能有什么问题?这个设置在cmake中似乎运行良好,但在我的bazel设置中给出了未定义的Tensorimpl错误。需要注意的是,libtorch 1.9二进制文件是从上面的链接中提取出来的,pytorch 1.9使用默认的pip安装指令安装在自己的pyenv中。但它们肯定是同一个版本

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题