Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: name '_C' is not defined #15

Open
bolongliu opened this issue Apr 8, 2023 · 30 comments
Open

NameError: name '_C' is not defined #15

bolongliu opened this issue Apr 8, 2023 · 30 comments

Comments

@bolongliu
Copy link

File "Grounded-Segment-Anything/GroundingDINO/groundingdino/models/GroundingDINO/ms_deform_attn.py", line 53, in forward
output = _C.ms_deform_attn_forward(

@rentainhe
Copy link
Collaborator

You should set CUDA_HOME before installing the repo~ here is an example

export CUDA_HOME=/path/to/cuda-11.3/
pip install ...

@Andy1621
Copy link
Contributor

Andy1621 commented Apr 8, 2023

In my case, CUDA_HOME can be automatically set via 'torch'.
However, I met this bug because installing GroundingDINO without a GPU. After reinstalling it with GPU, it runs normally.

@0xbitches
Copy link

@Andy1621 can you specify what you did?

@Andy1621
Copy link
Contributor

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

@Hsintien-Ng
Copy link

I met the same error with an annaconda env, and I solved it through:

cd GroundingDINO
python setup.py build.py
python setup.py install.py

This will install the groundingdino with GPU. But in my case, some extra library such as config, datasets miss after using the command above.

ModuleNotFoundError: No module named 'groundingdino.config'

So I copy them into the env library, and it works!

@kyungmnlee
Copy link

I met the same error with an annaconda env, and I solved it through:

cd GroundingDINO
python setup.py build.py
python setup.py install.py

This will install the groundingdino with GPU. But in my case, some extra library such as config, datasets miss after using the command above.

ModuleNotFoundError: No module named 'groundingdino.config'

So I copy them into the env library, and it works!

Hi Hsintein-Ng, could you please elaborate more on how you've installed on conda environmen?

@Hsintien-Ng
Copy link

@kyungmnlee I install the conda environment through following https://github.com/facebookresearch/segment-anything
Then, I install the package in requirements.txt in https://github.com/IDEA-Research/Grounded-Segment-Anything
Next, I update the torch version to 2.0
Finally, I use the setup.py in GroundingDINO to install it with GPU.

@Bentonmaster
Copy link

I met the same error with an annaconda env, and I solved it through:

cd GroundingDINO
python setup.py build.py
python setup.py install.py

This will install the groundingdino with GPU. But in my case, some extra library such as config, datasets miss after using the command above.

ModuleNotFoundError: No module named 'groundingdino.config'

So I copy them into the env library, and it works!

It should be:

cd GroundingDINO
python setup.py build
python setup.py install

@cgnerds
Copy link

cgnerds commented Apr 10, 2023

pip3 install Cython

@onefish51
Copy link

onefish51 commented Apr 13, 2023

I run it OK ,but when I updated GroundingDINO/groundingdino, and in my code device = torch.device('cuda') . then I run it again , It's failed !

I fixed it by python -m pip install -e GroundingDINO or device="cpu" , then I run it again , It's OK !

As above guys said

The groundingdino._C isn't installed without GPU.

Have a try !

@TestPrab
Copy link

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

Is there any way to verify if it's installed via GPU or not? I have cuda available and CUDA_HOME is also set up properly, also I can see this structure after the python -m pip install -e GroundingDINO .

image
I can find a file with _C name but still I face error for device =cuda

@delian11
Copy link

I have set CUDA_HOME and install pytorch==1.10.0 with CUDA 11.3 and cudnn 8.2.0, and then install the groundingdino with both "python -m pip install -e GroundingDINO" and "cd GroundingDINO; python setup.py build; python setup.py install", but I still get the "Failed to load custom C++ ops. Running on CPU mode Only! " error. When I input "from groundingdino import _C" in python environment, it return "undefined symbol: _ZN6caffe28TypeMeta21_typeMetaDataInstanceIdEEPKNS_6detail12TypeMetaDataEv" error, what should I do?

@TestPrab
Copy link

You should set CUDA_HOME before installing the repo~ here is an example

export CUDA_HOME=/path/to/cuda-11.3/
pip install ...

Simply setting up CUDA HOME, won't work it also needs the same CUDA version that the pytorch supports for e.g I have CUDA 12.1 setup and also the CUDA HOME pointing the same, yet I get this error when it tries to build _C file

raise RuntimeError(CUDA_MISMATCH_MESSAGE.format(cuda_str_version, torch.version.cuda))
RuntimeError:
The detected CUDA version (12.1) mismatches the version that was used to compile
 PyTorch (11.8). Please make sure to use the same CUDA versions.

@HassanBinHaroon
Copy link

In my case, CUDA_HOME can be automatically set via 'torch'. However, I met this bug because installing GroundingDINO without a GPU. After reinstalling it with GPU, it runs normally.

For running on CPU, ignore the error add --cpu-only at the end of the command. I should work that way.

@HassanBinHaroon
Copy link

You should set CUDA_HOME before installing the repo~ here is an example

export CUDA_HOME=/path/to/cuda-11.3/
pip install ...

Simply setting up CUDA HOME, won't work it also needs the same CUDA version that the pytorch supports for e.g I have CUDA 12.1 setup and also the CUDA HOME pointing the same, yet I get this error when it tries to build _C file

raise RuntimeError(CUDA_MISMATCH_MESSAGE.format(cuda_str_version, torch.version.cuda))
RuntimeError:
The detected CUDA version (12.1) mismatches the version that was used to compile
 PyTorch (11.8). Please make sure to use the same CUDA versions.

In case of CUDA and PyTorch correct version mismatch, use command like this e.g "pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/cu114/torch_stable.html"

Install PyTorch according to your CUDA version.

@YHD233
Copy link

YHD233 commented May 13, 2023

Does ROCm not support it?

@superhero-7
Copy link

superhero-7 commented May 18, 2023

cd GroundingDINO
export PATH=/usr/local/cuda/bin:$PATH
export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

pip install -e .

And then I fixed all error...... That is crazy!

@supreeth-sirpi
Copy link

You should set CUDA_HOME before installing the repo~ here is an example

export CUDA_HOME=/path/to/cuda-11.3/
pip install ...

Simply setting up CUDA HOME, won't work it also needs the same CUDA version that the pytorch supports for e.g I have CUDA 12.1 setup and also the CUDA HOME pointing the same, yet I get this error when it tries to build _C file

raise RuntimeError(CUDA_MISMATCH_MESSAGE.format(cuda_str_version, torch.version.cuda))
RuntimeError:
The detected CUDA version (12.1) mismatches the version that was used to compile
 PyTorch (11.8). Please make sure to use the same CUDA versions.

In case of CUDA and PyTorch correct version mismatch, use command like this e.g "pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/cu114/torch_stable.html"

Install PyTorch according to your CUDA version.

Thanks, the above solution works!
For me, I got this bug while running a processing job in SM pipeline.
Here's the messy Dockerfile I've used :)

FROM pytorch/pytorch:1.13.1-cuda11.6-cudnn8-devel

ENV CUDA_HOME /usr/local/cuda-11.6/

RUN apt-get update && \
   DEBIAN_FRONTEND=noninteractive \ 
   apt-get install --no-install-recommends --assume-yes \
        git
RUN pip install Cython numpy
RUN pip install torch==2.0.1 torchvision==0.15.2 torchaudio -f https://download.pytorch.org/whl/cu116/torch_stable.html
RUN pip install git+https://github.com/IDEA-Research/GroundingDINO.git

Once again, please make sure your PyTorch wheel file is compatible with your CUDA version :)

@Shirley-qp-LAI
Copy link

I met the same problem,then I solved it as follows:

  1. set up the enviroment
    export AM_I_DOCKER=False
    export BUILD_WITH_CUDA=True
    export CUDA_HOME=/path/to/cuda-11.3/
  2. remove the build files, then
    run python setup.py install

@ZC0102-shu
Copy link

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

Is there any way to verify if it's installed via GPU or not? I have cuda available and CUDA_HOME is also set up properly, also I can see this structure after the python -m pip install -e GroundingDINO .

image I can find a file with _C name but still I face error for device =cuda

I got the _C file as well under groundingdino, but i also failed to import it. The CUDA_HOME is set properly.

@BrandonHanx
Copy link

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

Is there any way to verify if it's installed via GPU or not? I have cuda available and CUDA_HOME is also set up properly, also I can see this structure after the python -m pip install -e GroundingDINO .
image I can find a file with _C name but still I face error for device =cuda

I got the _C file as well under groundingdino, but i also failed to import it. The CUDA_HOME is set properly.

same here

Any suggestions?

@ZC0102-shu
Copy link

@BrandonHanx I installed GroundingDINO alone and it works, but when replacing it with the folder in MAM the issure remains. Have you solved it?

@XiangBaoSong
Copy link

try:
from groundingdino import _C
except Exception as e:
warnings.warn("Failed to load custom C++ ops. Running on CPU mode Only!")

ms_deform_attn.py:31 show error below :

_C.cpython-310-x86_64-linux-gnu.so: undefined symbol: _ZNSt15__exception_ptr13exception_ptr9_M_addrefEv

how can l sovel this problem?

@artonnet
Copy link

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

I encountered this issue in Win10, CUDA v11.7, and Python 3.10.
I found the value of torch.cuda.is_available() in the GroundingDINO\setup.py file is False.
This problem seems to be caused by the creation of an isolated dependency environment during pip install, which installs the CPU version of torch.
For me, this issue can be resolved by using pip install -e GroundingDINO --no-build-isolation (with proper CUDA_HOME).

By the way, I tested torch==1.13.1+cu117 torchvision==0.14.1+cu117 and torch==2.0.1+cu117 torchvision==0.15.2+cu117, both work for me.
I hope this information is helpful.

@honghd16
Copy link

honghd16 commented Dec 1, 2023

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

I encountered this issue in Win10, CUDA v11.7, and Python 3.10. I found the value of torch.cuda.is_available() in the GroundingDINO\setup.py file is False. This problem seems to be caused by the creation of an isolated dependency environment during pip install, which installs the CPU version of torch. For me, this issue can be resolved by using pip install -e GroundingDINO --no-build-isolation (with proper CUDA_HOME).

By the way, I tested torch==1.13.1+cu117 torchvision==0.14.1+cu117 and torch==2.0.1+cu117 torchvision==0.15.2+cu117, both work for me. I hope this information is helpful.

Thanks, artonnet. This also solves my issue!

@Mehanik
Copy link

Mehanik commented Dec 26, 2023

In my case the problem was caused by ABI incompatibility. System GCC was used at building time, but python binary comes from conda. Installing gcc/gxx from conda solves the issue:

conda install gcc_linux-64
conda install gxx_linux-64

@catcat569
Copy link

I've had a lot of warning issues since I fixed this issue,I don't understand the reason for the warning, hope it helps me out, thank you
1706240301875

@somuchtome
Copy link

@jasonborn0 You have to make sure your GroundingDINO is installed with a GPU instead of CPU. The groundingdino._C isn't installed without GPU. And the default hyperparameter is --device "cuda", which calls the bug.

if torch.cuda.is_available() and CUDA_HOME is not None:
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
else:
print("Compiling without CUDA")
define_macros += [("WITH_HIP", None)]
extra_compile_args["nvcc"] = []
return None
sources = [os.path.join(extensions_dir, s) for s in sources]
include_dirs = [extensions_dir]
ext_modules = [
extension(
"groundingdino._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
)
]
return ext_modules

I encountered this issue in Win10, CUDA v11.7, and Python 3.10. I found the value of torch.cuda.is_available() in the GroundingDINO\setup.py file is False. This problem seems to be caused by the creation of an isolated dependency environment during pip install, which installs the CPU version of torch. For me, this issue can be resolved by using pip install -e GroundingDINO --no-build-isolation (with proper CUDA_HOME).

By the way, I tested torch==1.13.1+cu117 torchvision==0.14.1+cu117 and torch==2.0.1+cu117 torchvision==0.15.2+cu117, both work for me. I hope this information is helpful.

That solve my problem as well. Thank you very much!!!

@katelgote
Copy link

I was using RHEL9 container
My torch.version.cuda was 12.1
CUDA_HOME was set to /usr/local/cuda-12.5/
Still got the same error. But the above comment by @somuchtome solved the issue:-
After going to the Grounded-Segment-Anything/GroundingDINO folder
pip install -e . --no-build-isolation This solution worked ❤️

@Ansonkkk
Copy link

我在 annaconda 环境中遇到了同样的错误,我通过解决了它:

cd 接地DINO python setup.py build.py python setup.py install.py

这将安装带有 GPU 的 groundingdino。但是就我而言,使用上述命令后,一些额外的库(例如 config、datasets )会丢失。

ModuleNotFoundError:没有名为“groundingdino.config”的模块

所以我将它们复制到 env 库中,它起作用了!

它应该是:

cd 接地DINO python setup.py 构建 python setup.py 安装

This method was a great solution to this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests