Trying to build a torchscript extension results in INVALID TYPE: Only int64_t and bool are supported as an integral argument 类型错误
Trying to build a torchscript extension results in INVALID TYPE: Only int64_t and bool are supported as an integral argument type error
我关注 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
这里有什么问题?
这个错误有点误导!所需要做的就是将所有 return 类型从 int
转换为 int64_t
,仅此而已。现在所有编译都很好!
我关注 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
这里有什么问题?
这个错误有点误导!所需要做的就是将所有 return 类型从 int
转换为 int64_t
,仅此而已。现在所有编译都很好!