Skip to content

[Question] Building wheel with GPU support on Windows #742

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

Closed
senovr opened this issue Nov 2, 2022 · 9 comments
Closed

[Question] Building wheel with GPU support on Windows #742

senovr opened this issue Nov 2, 2022 · 9 comments

Comments

@senovr
Copy link

senovr commented Nov 2, 2022

Hi,
I have an issue when trying to build pip wheel with GPU support on windows.
System configuration:
Windows 10
Visual Studio 2022 Community edition
Cmake 3.25.0-rc3
CUDA 11.7
CUDNN 8.6
python 3.10.7

Steps to reproduce:

git clone --recursive https://github.com/opencv/opencv-python
cd opencv-python 
# creating separate venv for build
python -m venv .venv
source .venv/Scripts/activate
# setting build params
set CMAKE_ARGS="-DBUILD_opencv_world=ON -DWITH_CUDA=ON -DCUDA_FAST_MATH=ON -DWITH_CUBLAS=ON -DCUDA_ARCH_PTX=8.6 -DWITH_NVCUVID=ON"
pip wheel . --verbose

Actual behaviour

It starts to install packages and fails with the following traceback:

#first, it warns that ignoring installed numpy

Installing build dependencies: started
  Running command pip subprocess to install build dependencies
  Ignoring numpy: markers 'python_version == "3.6" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment
  Ignoring numpy: markers 'python_version == "3.7" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment
  Ignoring numpy: markers 'python_version == "3.8" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment
  Ignoring numpy: markers 'python_version <= "3.9" and sys_platform == "linux" and platform_machine == "aarch64"' don't match your environment
  Ignoring numpy: markers 'python_version <= "3.9" and sys_platform == "darwin" and platform_machine == "arm64"' don't match your environment
  Ignoring numpy: markers 'python_version == "3.9" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment
  Ignoring numpy: markers 'python_version >= "3.10" and platform_system == "Darwin"' don't match your environment

after that, it fails during numpy build.

Building wheels for collected packages: numpy
   Building wheel for numpy (pyproject.toml): started
   Building wheel for numpy (pyproject.toml): finished with status 'error'
   error: subprocess-exited-with-error

   Building wheel for numpy (pyproject.toml) did not run successfully.

   [272 lines of output]
   setup.py:63: RuntimeWarning: NumPy 1.21.2 may not yet support Python 3.10.
   exit code: 1

Any thoughts/ideas?
Should I downgrade to python 3.9 or any other version?

@skvark
Copy link
Member

skvark commented Nov 2, 2022

The numpy version in the pyproject.toml seems to be too old. 1.21.2 does not have Windows wheels, so pip tries to build from sources and fails.

You can set the version to 1.21.3 in pyproject.toml:

"numpy==1.21.2; python_version>='3.10'",

@senovr
Copy link
Author

senovr commented Nov 2, 2022

@skvark , thanks for the prompt reply!
It installs successfully, but fails during build:

 cv2_highgui.cpp
  C:\Users\user\repos\opencv-python\opencv\modules\python\src2\cv2.hpp(36,10): fatal  error C1083: Cannot open include file: 'numpy/ndarrayobject.h': No such file or directory (compiling source file C:\Users\user\repos\opencv-python\opencv\modules\python\src2\cv2.cpp) [C:\Users\user\repos\opencv-python\_skbuild\win-amd64-3.10\cmake-build\modules\python3\opencv_python3.vcxproj]

Apparently, there is no /numpy/ndarrayobject.h in opencv-python\opencv\modules\python\src2
Is this due to fact that I am using .venv?

Edit:
I also tried to install numpy separately into .venv with:
pip install numpy==1.21.3 # the same version as in modified .toml
and now I have ndarrayobject.h in .venv\Lib\site-packages\numpy\core\include\numpy

Still, it doesn't recognized by the compiler - fails with the same error.

BTW, is it ok to keep this thread here, or it is better to move it to SO?

@skvark
Copy link
Member

skvark commented Nov 2, 2022

I think it's okay to keep it here since there is an outdated dependency in the pyproject.toml which is a bug.

Try running the pip wheel . --verbose without venv with the modified pyproject.toml. Pip will install the build time dependencies into a temporary location so venv should not be needed.

@senovr
Copy link
Author

senovr commented Nov 2, 2022

I tried running pip wheel without activating .venv
packagers included in system python:

pip list
Package    Version
---------- -------
pip        22.3
setuptools 63.2.0
wheel      0.37.1

pip wheel . --verbose still fails with the same error.

 cv2_highgui.cpp
  C:\Users\user\repos\opencv-python\opencv\modules\python\src2\cv2.hpp(36,10): fatal  error C1083: Cannot open include file: 'numpy/ndarrayobject.h': No such file or directory (compiling source file C:\Users\user\repos\opencv-python\opencv\modules\python\src2\cv2.cpp) [C:\Users\user\repos\opencv-python\_skbuild\win-amd64-3.10\cmake-build\modules\python3\opencv_python3.vcxproj]

Intrestingly enough, in section General configuration for OpenCV 4.6.0

I do have a path to numpy core...

--   Python 3:
  --     Interpreter:                 C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe (ver 3.10.7)
  --     Libraries:                   C:/Users/user/AppData/Local/Programs/Python/Python310/libs/python310.lib (ver 3.10.7)
  --     numpy:                       C:/Users/user/AppData/Local/Temp/pip-build-env-83x0atve/overlay/Lib/site-packages/numpy/core/include (ver 1.21.3)
  --     install path:                python/cv2/python-3

@skvark
Copy link
Member

skvark commented Nov 2, 2022

Try to remove the _skbuild directory and try again. There might be some cached value in there from previous runs. The numpy location is detected correctly so if that does not help I have no idea why the header is not found.

@senovr
Copy link
Author

senovr commented Nov 2, 2022

@skvark
After removing the skbuild directory, wheel has been build successfully.
Meanwhile, I noticed some other issue:
in final cmake command, I had the following output:

Command:
      cmake 'C:\Users\user\repos\opencv-python\opencv'
	  -G 'Visual Studio 17 2022'
	  '-DCMAKE_INSTALL_PREFIX:PATH=C:\Users\user\repos\opencv-python\_skbuild\win-amd64-3.10\cmake-install'
	  -DPYTHON_VERSION_STRING:STRING=3.10.7
	  -DSKBUILD:INTERNAL=TRUE '
	  -DCMAKE_MODULE_PATH:PATH=C:\Users\user\AppData\Local\Temp\pip-build-env-57x_hqy7\overlay\Lib\site-packages\skbuild\resources\cmake' 
	  '-DPYTHON_EXECUTABLE:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe' 
	  '-DPYTHON_INCLUDE_DIR:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310\Include' 
	  '-DPYTHON_LIBRARY:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310\libs\python310.lib' 
	  '-DPython_EXECUTABLE:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe' 
	  '-DPython_ROOT_DIR:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310' 
	  -DPython_FIND_REGISTRY:STRING=NEVER '
	  -DPython_NumPy_INCLUDE_DIRS:PATH=C:\Users\user\AppData\Local\Temp\pip-build-env-57x_hqy7\overlay\Lib\site-packages\numpy\core\include' 
	  '-DPython3_EXECUTABLE:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe' 
	  '-DPython3_ROOT_DIR:PATH=C:\Users\user\AppData\Local\Programs\Python\Python310' 
	  -DPython3_FIND_REGISTRY:STRING=NEVER 
	  '-DPython3_NumPy_INCLUDE_DIRS:PATH=C:\Users\user\AppData\Local\Temp\pip-build-env-57x_hqy7\overlay\Lib\site-packages\numpy\core\include'
	  -T v143 -A x64 
	  '-DPYTHON3_EXECUTABLE=C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe' 
	  -DPYTHON3_INCLUDE_DIR=C:/Users/user/AppData/Local/Programs/Python/Python310/Include 
	  -DPYTHON3_LIBRARY=C:/Users/user/AppData/Local/Programs/Python/Python310/libs/python310.lib 
	  -DBUILD_opencv_python3=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_java=OFF 
	  -DOPENCV_PYTHON3_INSTALL_PATH=python -DINSTALL_CREATE_DISTRIB=ON 
	  -DBUILD_opencv_apps=OFF -DBUILD_opencv_freetype=OFF -DBUILD_SHARED_LIBS=OFF 
	  -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_DOCS=OFF 
	  -DPYTHON3_LIMITED_API=ON -DBUILD_OPENEXR=ON 
	  -DCMAKE_GENERATOR_PLATFORM=x64 -DCMAKE_BUILD_TYPE:STRING=Release

No any args (like -DWITH_CUDA=ON ) I set earlier...
Apparently, .whl that was build has no GPU capability (confirmed with simple test script)

Edit:
It seems when using git bash as CLI tool in windows, "set" doesn't work. To make this works, I need to use Linux-style "export".
Will check the results and close this question if .whl will be build with GPU

@senovr
Copy link
Author

senovr commented Nov 6, 2022

@skvark - after removing skbuild directory, I have successfully build a custom wheel. Probably the final question from my side- the build itself is substantionnaly slow, due to utilizing only one core of the cpu. What are the args ( similar to make --j8) to utilize all the cores during build on windows?

@skvark
Copy link
Member

skvark commented Nov 8, 2022

I think CMake / OpenCV should use all available cores by default. Try to use Powershell or the command line, I think Git Bash might have something to do with that issue as well.

You can however try to add the -DCMAKE_CXX_FLAGS=/MP flag if you must use Git Bash for some reason.

@senovr
Copy link
Author

senovr commented Nov 8, 2022

@skvark , Thank you so much! I am closing this issue.

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

2 participants