Grounded-Segment-Anything icon indicating copy to clipboard operation
Grounded-Segment-Anything copied to clipboard

NameError: name '_C' is not defined

Open bolongliu opened this issue 2 years ago • 35 comments

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

bolongliu avatar Apr 08 '23 13:04 bolongliu

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

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

rentainhe avatar Apr 08 '23 14:04 rentainhe

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.

Andy1621 avatar Apr 08 '23 14:04 Andy1621

@Andy1621 can you specify what you did?

0xbitches avatar Apr 09 '23 22:04 0xbitches

@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. https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

Andy1621 avatar Apr 10 '23 02:04 Andy1621

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!

Hsintien-Ng avatar Apr 10 '23 06:04 Hsintien-Ng

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?

kyungmnlee avatar Apr 10 '23 07:04 kyungmnlee

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

Hsintien-Ng avatar Apr 10 '23 08:04 Hsintien-Ng

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

Bentonmaster avatar Apr 10 '23 09:04 Bentonmaster

pip3 install Cython

cgnerds avatar Apr 10 '23 15:04 cgnerds

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 !

onefish51 avatar Apr 13 '23 03:04 onefish51

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

https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

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

TestPrab avatar Apr 18 '23 11:04 TestPrab

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?

delian11 avatar Apr 19 '23 06:04 delian11

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.

TestPrab avatar Apr 19 '23 06:04 TestPrab

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 avatar Apr 28 '23 13:04 HassanBinHaroon

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.

HassanBinHaroon avatar Apr 28 '23 13:04 HassanBinHaroon

Does ROCm not support it?

YHD233 avatar May 13 '23 09:05 YHD233

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!

superhero-7 avatar May 18 '23 17:05 superhero-7

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

supreeth-sirpi avatar May 23 '23 05:05 supreeth-sirpi

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

Shirley-qp-LAI avatar Jun 02 '23 03:06 Shirley-qp-LAI

@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. https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

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.

ZC0102-shu avatar Jul 19 '23 03:07 ZC0102-shu

@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. https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

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?

BrandonHanx avatar Jul 20 '23 16:07 BrandonHanx

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

ZC0102-shu avatar Jul 24 '23 07:07 ZC0102-shu

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?

XiangBaoSong avatar Aug 16 '23 14:08 XiangBaoSong

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

https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

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.

artonnet avatar Nov 26 '23 09:11 artonnet

@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. https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

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!

honghd16 avatar Dec 01 '23 06:12 honghd16

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

Mehanik avatar Dec 26 '23 11:12 Mehanik

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

catcat569 avatar Jan 26 '24 03:01 catcat569

@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. https://github.com/IDEA-Research/Grounded-Segment-Anything/blob/ab1c44b2d01e695967af7007c3193a4b4066a6bc/GroundingDINO/setup.py#L73-L103

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!!!

somuchtome avatar Feb 04 '24 15:02 somuchtome

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 :heart:

katelgote avatar Aug 07 '24 13:08 katelgote