Python调用含cuBLAS与cuSPARSE的C++/CUDA库时出现cusparseCreateDnVec未定义符号错误求助
undefined symbol: cusparseCreateDnVec Error in PyTorch CUDA Extension Let's break down why you're hitting this error and how to fix it:
Root Cause
Your custom CUDA extension isn't properly linking against the cuSPARSE library. Even though you added -lcusparse to extra_compile_args, that parameter passes flags to the compilation stage (nvcc), not the linking stage. The cusparseCreateDnVec symbol lives in libcusparse.so, which your extension isn't linking to—this is confirmed by your ldd output, which doesn't list libcusparse.so as a dependency.
Note that cusparseCreateDnMat works because it might be indirectly linked via another library (like PyTorch's internal dependencies), but cusparseCreateDnVec requires explicit linking.
Solution: Correctly Link cuBLAS and cuSPARSE
Modify your setup_debug.py to use the libraries parameter of CUDAExtension, which tells the linker to include the required CUDA libraries. This is more reliable than manually adding -l flags to compile args, since PyTorch's extension tools handle CUDA path resolution automatically.
Here's the updated setup_debug.py:
from setuptools import setup from torch.utils.cpp_extension import BuildExtension, CUDAExtension setup( name='pytorch_custom_cuda', ext_modules=[ CUDAExtension( 'pytorch_custom_cuda', ['min_repr_example.cpp'], libraries=['cublas', 'cusparse'] # Explicitly list libraries to link ) ], cmdclass={ 'build_ext': BuildExtension })
Alternative: Use extra_link_args (If Needed)
If for some reason the libraries parameter doesn't work, you can pass the linking flags directly to the linker via extra_link_args:
CUDAExtension( 'pytorch_custom_cuda', ['min_repr_example.cpp'], extra_link_args=['-lcublas', '-lcusparse'] )
Verification Steps
- Recompile and install: Run
python setup_debug.py installagain to build the extension with proper linking. - Check dependencies: Run
ldd pytorch_custom_cuda.cpython-38-x86_64-linux-gnu.so—you should now seelibcusparse.solisted as a dependency. - Test the script: Run
python min_repr_ex.py—the import error should be resolved.
Additional Checks
- Ensure your PyTorch version is compatible with CUDA 11.4 (check with
torch.version.cuda). Mismatched CUDA versions between PyTorch and your system can cause linking issues. - Confirm that
libcusparse.so.11exists in/usr/local/cuda-11.4/lib64/(it should be included with CUDA 11.4).
内容的提问来源于stack exchange,提问作者壞府_

