源码:GitHub - laughtervv/DepthAwareCNN: Depth-aware CNN for RGB-D Segmentation, ECCV 2018
环境:Win10,python 3.7,torch 1.7.1+cu110
depthconv_cuda文件的debug
//nDimension全部替换为dim() //size[x]全部替换为size(x) //THCState_getCurrentStream(state)都替换为c10::cuda::getCurrentCUDAStream() //THCudaBlas_Sgemv替换为at::cuda::blas::gemv,加入头文件#include 该方法能够编译,但是无法链接 //THCudaBlas_xxxxx方法定义于D:ProgramTensorAnaconda3envspytorchLibsite-packagestorchincludeTHCTHCblas.h //最终使用cublasSgemv,加入头文件#include //参考DCNv2 https://github.com/lbin/DCNv2/tree/pytorch_1.6 https://github.com/lbin/DCNv2/ //链接错误:将 extern THCState *state; 改成THCState *state = at::globalContext().lazyInitCUDA();
使用nvcc命令编译depthconv_cuda_kernel.cu 具体指令参考make.sh
使用cl命令编译depthconv.cpp和depthconv_cuda.cpp
depthconv.c和depthconv_cuda.c修改后缀为.cpp
cl -DMS_WIN64 -I "C:Program Files (x86)Windows Kits10Include10.0.17763.0shared" -I"C:Program Files (x86)Windows Kits10Include10.0.17763.0ucrt" -I"D:Program Files (x86)Microsoft Visual Studio2017ProfessionalVCToolsMSVC14.16.27023include" -ID:ProgramTensorAnaconda3envspytorchlibsite-packagestorchinclude -I"D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchincludetorchcsrcapiinclude" -I"D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchincludeTH" -I"D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchincludeTHC" -I"D:ProgramTensorNVIDIACUDAv10.0include" -I"D:ProgramTensorAnaconda3envspytorchinclude" -I"D:ProgramTensorAnaconda3envspytorchinclude" -IE:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvsrc -c E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvsrcdepthconv.c /FoE:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildtemp.win-amd64-3.7Releasesrcdepthconv.o -Zi -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=depthconv -D_GLIBCXX_USE_CXX11_ABI=0 cl -DMS_WIN64 -I "C:Program Files (x86)Windows Kits10Include10.0.17763.0shared" -I"C:Program Files (x86)Windows Kits10Include10.0.17763.0ucrt" -I"D:Program Files (x86)Microsoft Visual Studio2017ProfessionalVCToolsMSVC14.16.27023include" -ID:ProgramTensorAnaconda3envspytorchlibsite-packagestorchinclude -I"D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchincludetorchcsrcapiinclude" -I"D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchincludeTH" -I"D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchincludeTHC" -I"D:ProgramTensorNVIDIACUDAv10.0include" -I"D:ProgramTensorAnaconda3envspytorchinclude" -I"D:ProgramTensorAnaconda3envspytorchinclude" -IE:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvsrc -c E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvsrcdepthconv_cuda.cpp /FoE:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildtemp.win-amd64-3.7Releasesrcdepthconv_cuda.o -Zi -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=depthconv -D_GLIBCXX_USE_CXX11_ABI=0
添加bindings.cpp,并使用cl编译
该cpp用于打包cuda/c++中的函数并导出
#pragma once #include#include #include "depthconv.h" #include "depthconv_cuda.h" #include "depthconv_cuda_kernel.h" PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("depthconv_forward", &depthconv_forward); m.def("depthconv_backward", &depthconv_backward); m.def("depthconv_forward_cuda", &depthconv_forward_cuda); m.def("depthconv_backward_input_cuda", &depthconv_backward_input_cuda); m.def("depthconv_backward_parameters_cuda", &depthconv_backward_parameters_cuda); //m.def("depthconv_im2col", &depthconv_im2col); //m.def("depthconv_col2im", &depthconv_col2im); }
使用link命令链接
link -dll E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildtemp.win-amd64-3.7Releasesrcdepthconv.o E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildtemp.win-amd64-3.7Releasesrcdepthconv_cuda.o E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildtemp.win-amd64-3.7Releasesrc depthconv_cuda_kernel.o E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildtemp.win-amd64-3.7Releasesrc bindings.o /libpath:"D:Program Files (x86)Microsoft Visual Studio2017ProfessionalVCToolsMSVC14.16.27023libonecorex64" /libpath:"D:ProgramTensorAnaconda3libs" /libpath:"C:Program Files (x86)Windows Kits10Lib10.0.17763.0umx64" /libpath:"C:Program Files (x86)Windows Kits10Lib10.0.17763.0ucrtx64" -LIBPATH:D:ProgramTensorAnaconda3envspytorchlibsite-packagestorchlib -LIBPATH:D:ProgramTensorNVIDIACUDAv10.0lib/x64 -LIBPATH:D:ProgramTensorAnaconda3envspytorchlibs -LIBPATH:D:ProgramTensorAnaconda3envspytorchPCbuildamd64 c10.lib c10_cuda.lib torch.lib torch_cpu.lib torch_cuda.lib torch_python.lib cudart.lib cublas.lib python37.lib -OUT:E:ZTensorprogramsDepthAwareCNN-mastermodelsopsdepthconvbuildlib.win-amd64-3.7_extdepthconv.cp37-win_amd64.pyd
以上步骤为单步编译、链接。
按上述修改程序后,其实可以直接构建setup.py,一次性编译。
具体过程请参见《Win10下CUDA/C++的混合编译——PCT中PointNet2模块》
调用
拷贝depthconv.cp37-win_amd64.pyd至depthconv_ext目录下,即可调用。
实际调用中存在问题:函数定义中输入张量均为THCudaTensor *,但是传入的都是torch.Tensor,运行提示参数类型不支持。对此有两种解决方案:
1)改变传入参数的类型(需要高手加以指点);
2)修改CUDA/C++源代码,替换THCudaTensor *指针为at::Tensor对象(可参考https://github.com/lbin/DCNv2/blob/pytorch_1.6/src/cuda/dcn_v2_cuda.cu)。
成功配置VS+CUDA后,可以一次性编译,参见
《Win10下CUDA/C++的混合编译——更新》



