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

Missing module '_C'? #53

Closed
Pizzawookiee opened this issue Jul 31, 2024 · 6 comments
Closed

Missing module '_C'? #53

Pizzawookiee opened this issue Jul 31, 2024 · 6 comments

Comments

@Pizzawookiee
Copy link

Pizzawookiee commented Jul 31, 2024

I get an error with this line of code in segment-anything-2\sam2\utils\misc.py (about line 60-ish)

def get_connected_components(mask):
"""
Get the connected components (8-connectivity) of binary masks of shape (N, 1, H, W).

Inputs:
- mask: A binary mask tensor of shape (N, 1, H, W), where 1 is foreground and 0 is
        background.

Outputs:
- labels: A tensor of shape (N, 1, H, W) containing the connected component labels
          for foreground pixels and 0 for background pixels.
- counts: A tensor of shape (N, 1, H, W) containing the area of the connected
          components for foreground pixels and 0 for background pixels.
"""
from sam2 import _C

return _C.get_connected_componnets(mask.to(torch.uint8).contiguous())

The code I use to call it comes from the example automatic mask generation notebook, it seems to fail due to changing the mask generator line to this:

mask_generator_2 = SAM2AutomaticMaskGenerator(
model=sam2,
points_per_side=64,
points_per_batch=128,
pred_iou_thresh=0.7,
stability_score_thresh=0.92,
stability_score_offset=0.7,
crop_n_layers=1,
box_nms_thresh=0.7,
crop_n_points_downscale_factor=2,
min_mask_region_area=25.0,
use_m2m=True,
)

The specific error is:

ImportError: cannot import name '_C' from 'sam2'

@XinrunXu
Copy link

same

@Qingqingniu
Copy link

I meet the same problem, I solved it with running the command"python setup.py build_ext --inplace" @Pizzawookiee @XinrunXu

@omrastogi
Copy link

I meet the same problem, I solved it with running the command"python setup.py build_ext --inplace" @Pizzawookiee @XinrunXu

This worked for me as well. Thanks!

@XinrunXu
Copy link

Do not work for me:

(sam2) C:\Users\PS\Desktop\segment-anything-2>python setup.py clean --all
CUDA_HOME in setup.py: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
running clean
removing 'build\temp.win-amd64-cpython-310' (and everything under it)
'build\lib.win-amd64-cpython-310' does not exist -- can't clean it
'build\bdist.win-amd64' does not exist -- can't clean it
'build\scripts-3.10' does not exist -- can't clean it
removing 'build'

(sam2) C:\Users\PS\Desktop\segment-anything-2>python setup.py build_ext --inplace
CUDA_HOME in setup.py: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
running build_ext
C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py:384: UserWarning: Error checking compiler version for cl: [WinError 2] The system cannot find the file specified
warnings.warn(f'Error checking compiler version for {compiler}: {error}')
building 'sam2.C' extension
creating C:\Users\PS\Desktop\segment-anything-2\build
creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310
creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release
creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2
creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2\csrc
Emitting ninja build file C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\build.ninja...
Compiling objects...
Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)
[1/1] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin\nvcc --generate-dependencies-with-compile --dependency-output C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj.d -std=c++17 --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /wd4624 -Xcompiler /wd4067 -Xcompiler /wd4068 -Xcompiler /EHsc -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\torch\csrc\api\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\TH -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\include" -IC:\Users\PS.conda\envs\sam2\include -IC:\Users\PS.conda\envs\sam2\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt" -c C:\Users\PS\Desktop\segment-anything-2\sam2\csrc[connected_components.cu](http://connected_components.cu/) -o C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj -D__CUDA_NO_HALF_OPERATORS
_ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -DCUDA_HAS_FP16=1 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=C -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_80,code=sm_80
FAILED: C:/Users/PS/Desktop/segment-anything-2/build/temp.win-amd64-cpython-310/Release/sam2/csrc/connected_components.obj
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin\nvcc --generate-dependencies-with-compile --dependency-output C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj.d -std=c++17 --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /wd4624 -Xcompiler /wd4067 -Xcompiler /wd4068 -Xcompiler /EHsc -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\torch\csrc\api\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\TH -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\include" -IC:\Users\PS.conda\envs\sam2\include -IC:\Users\PS.conda\envs\sam2\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt" -c C:\Users\PS\Desktop\segment-anything-2\sam2\csrc[connected_components.cu](http://connected_components.cu/) -o C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj -D__CUDA_NO_HALF_OPERATORS
_ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -DCUDA_HAS_FP16=1 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_C -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_80,code=sm_80
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\include\crt/host_config.h(153): fatal error C1189: #error: -- unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2022 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.
connected_components.cu
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 2107, in _run_ninja_build
subprocess.run(
File "C:\Users\PS.conda\envs\sam2\lib\subprocess.py", line 526, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\PS\Desktop\segment-anything-2\setup.py", line 68, in
setup(
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_init_.py", line 104, in setup
return distutils.core.setup(**attrs)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\core.py", line 184, in setup
return run_commands(dist)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\core.py", line 200, in run_commands
dist.run_commands()
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\dist.py", line 969, in run_commands
self.run_command(cmd)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools\dist.py", line 967, in run_command
super().run_command(command)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\dist.py", line 988, in run_command
cmd_obj.run()
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools\command\build_ext.py", line 91, in run
_build_ext.run(self)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 359, in run
self.build_extensions()
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 870, in build_extensions
build_ext.build_extensions(self)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 479, in build_extensions
self._build_extensions_serial()
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 505, in _build_extensions_serial
self.build_extension(ext)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools\command\build_ext.py", line 252, in build_extension
_build_ext.build_extension(self, ext)
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 560, in build_extension
objects = self.compiler.compile(
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 842, in win_wrap_ninja_compile
_write_ninja_file_and_compile_objects(
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 1783, in _write_ninja_file_and_compile_objects
_run_ninja_build(
File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 2123, in _run_ninja_build
raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension

I meet the same problem, I solved it with running the command"python setup.py build_ext --inplace" @Pizzawookiee @XinrunXu

@ronghanghu
Copy link
Contributor

Hi, we have added INSTALL.md as an installation FAQ.

Regarding this error:

This is usually because you haven't run the pip install -e ".[demo]" step above or the installation failed. Please install SAM 2 first, and see the other issues if your installation fails. (You may also try python setup.py build_ext --inplace in the repo root as others suggested in #77)

@ronghanghu
Copy link
Contributor

ronghanghu commented Aug 2, 2024

@XinrunXu the issue you're facing is likely due to the line

unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2022 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.

in your error log. It suggested that your local compiler doesn't match the one required for the nvcc command in CUDA toolkit. You may refer to https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/ to use an nvcc-compatible compiler. It looks like you're using Windows and WSL (Windows Subsystem for Linux) could be an alternative solution

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

No branches or pull requests

5 participants