通常,我需要生成一个共享库文件(.so),我可以使用<torch.ops.load_库()>;函数,该函数来自python脚本
在Windows 10:64位操作系统上工作
尝试了两种方法-都不生成此文件
1>CMakesList.txt-构建成功,我得到了一个dll和.lib文件,我尝试使用python ctypes-cdll.load_library()加载该文件,但没有函数,通过vs代码使用dumpbin进行交叉检查。
cmake_minimum_required (VERSION 3.8)
find_package(Torch REQUIRED)
project(reductionResNet VERSION 1.0 DESCRIPTION "Deep_Learning")
# add_executable (customOperator "customOperator.cpp" "customOperator.h")
# Define our library target
add_library(customOperator SHARED customOperator.cpp)
# Enable C++14
target_compile_features(customOperator PRIVATE cxx_std_14)
# Link against LibTorch
target_link_libraries(customOperator "${TORCH_LIBRARIES}")
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET customOperator
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:customOperator>)
endif (MSVC)
2>Python Setuptools.py-抛出错误->;链接:错误LNK2001:未解析的外部符号PyInit_缩减
from setuptools import setup, Extension
from torch.utils import cpp_extension
setup(name='customOperator',
ext_modules=[cpp_extension.CppExtension('customOperator', ['customOperator.cpp'],
include_dirs = ['C:/Libtorch/libtorch-win-shared-with-deps-1.8.1+cpu/libtorch'])],
cmdclass={'build_ext':cpp_extension.BuildExtension.with_options(no_python_abi_suffix=True)} )
这些是我一直关注的教程Pytorch_Docs&Git_tutorial
这是Tested.cpp文件,其中包含两个函数-reduce和repeatInterleave
#include "customOperator.h"
#include <torch/torch.h>
using namespace std;
torch::Tensor repeatInterleave(
torch::Tensor input,
int val
) {
auto output_ = torch::repeat_interleave(input, val);
return output_;
}
torch::Tensor reduction(
torch::Tensor layerOne,
torch::Tensor layerTwo,
torch::Tensor layerThree,
torch::Tensor layerFour) {
auto layerOne_ = repeatInterleave(layerOne, 8);
auto layerTwo_ = repeatInterleave(layerTwo, 4);
auto layerThree_ = repeatInterleave(layerThree, 2);
int len = layerFour.sizes()[0];
//cout << len << endl;
torch::Tensor arr[512] = {};
//torch::Tensor* arr = new torch::Tensor[len];
//std::vector<std::string> x = { "a", "b", "c" };
//x.push_back("d");
//std::vector <torch::Tensor> arr = {};
for (int i = 0; i < len; i += 1) {
arr[i] = (layerOne_[i] + layerTwo_[i] + layerThree_[i] + layerFour[i]) / 4;
//arr.push_back((layerOne_[i] + layerTwo_[i] + layerThree_[i] + layerFour[i]) / 4);
//cout << arr[i] << endl;
}
//cout << arr << endl;
//torch::Tensor output = torch::zeros(layerFour.sizes());
//delete[] arr;
auto ouput = torch::stack(arr);
return ouput;
}
int main()
{
cout << "Hello CMake." << endl;
return 0;
}
目前没有回答
相关问题 更多 >
编程相关推荐