You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用含cuBLAS与cuSPARSE的C++/CUDA库时出现cusparseCreateDnVec未定义符号错误求助

Fixing 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.

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
    })

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

  1. Recompile and install: Run python setup_debug.py install again to build the extension with proper linking.
  2. Check dependencies: Run ldd pytorch_custom_cuda.cpython-38-x86_64-linux-gnu.so—you should now see libcusparse.so listed as a dependency.
  3. 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.11 exists in /usr/local/cuda-11.4/lib64/ (it should be included with CUDA 11.4).

内容的提问来源于stack exchange,提问作者壞府_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:12:52