c++ - 尝试构建 torchscript 扩展会导致 INVALID TYPE : Only int64_t and bool are supported as an integral argument type error

标签 c++ torch libtorch torchscript

我正在关注 torch_script_custom_classes为了在Python中公开我的C++ dll。我目前拥有的大部分代码如下:

#include <torch/script.h>
#include <string>
#include <vector>
struct PyManager : torch::CustomClassHolder {
       PyManager(std::string model_path, 
                 std::string img_bank_folder_root, 
                 std::string cache_folder,
                 std::string pnet, 
                 std::string rnet, 
                 std::string onet, 
                 bool rebuildCache) {

                std::cout << "ok!";
       }

    int AddUser(std::string user_id, std::string img_fldr_path) { 
       std::cout << user_id << img_fldr_path;
       return 0;
    }
    int RemoveUser(std::string user_id) { 
       std::cout << user_id; 
       return 0;
    }
    int UpdateUser(std::string user_id, std::string new_images_folder_path) { 
        std::cout << user_id << new_images_folder_path; 
        return 0;
    }
    int RenameId(std::string old_id, std::string new_id) { 
        std::cout << old_id << new_id; 
        return 0;
    }
    int UpdateUsersInBulk(std::vector<std::string> userIDs, 
                          std::string ID_folders_path, 
                          bool is_delete_granted = true, bool show_dbg_info = true) {
           std::cout << userIDs.size() << ID_folders_path; 
           return 0;
      }


};

TORCH_LIBRARY(my_classes, m) {
              m.class_<PyManager>("PyManager")
        .def(torch::init<std::string, std::string, std::string, std::string, std::string, std::string, bool>())
        .def("add", &PyManager::AddUser)
        .def("remove", &PyManager::RemoveUser)
        .def("update", &PyManager::UpdateUser)
        .def("update_in_bulk", &PyManager::UpdateUsersInBulk);
}

这是我使用的 cmake 文件:

cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(custom_class)

find_package(Torch REQUIRED)

# Define our library target
add_library(custom_class SHARED class1.cpp)

#define our c++ standard
set(CMAKE_CXX_STANDARD 17)
# Link against LibTorch
target_link_libraries(custom_class "${TORCH_LIBRARIES}")

我这样调用 cmake :

cmake -DCMAKE_PREFIX_PATH="$(python -c 'import torch.utils; print(torch.utils.cmake_prefix_path)')" ..

但我收到以下错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C2338   INVALID TYPE: Only int64_t and bool are supported as an integral argument type  custom_class    ...\libtorch-debug-latest\libtorch\include\ATen\core\op_registration\infer_schema.h 39  

这里出了什么问题?

最佳答案

这个错误有点误导!所需要做的就是将所有返回类型从 int 转换为 int64_t ,仅此而已。现在所有编译都很好!

关于c++ - 尝试构建 torchscript 扩展会导致 INVALID TYPE : Only int64_t and bool are supported as an integral argument type error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63763112/

相关文章:

java - 如何从 Java 在 linux 终端中交互式地执行多个命令?

python - 在从 PyTorch 训练和导出的 LibTorch 中运行模型时获得的结果不正确

c++ - libtorch : How to create a gpu tensor base on data_ptr?

c++ - 为什么在派生类数据之前初始化基类数据

lstm - 类型错误 : view() takes at most 2 arguments (3 given)

c++ - `std::less` 是如何工作的?

Pytorch DataLoader - 选择类 STL10 数据集

c++ - pytorch/libtorch C++ 中的自定义子模块

c++ - 与 std::cout 等效的 Boost 测试框架是什么?

c++ - 计算线程内的时间