如何解决尝试构建torchscript扩展会导致INVALID TYPE:整数参数类型错误仅支持int64_t和bool
我正在关注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,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
,仅此而已。现在所有编译都可以了!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。