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

Compile v2.1 on windows with vs2019 and cuda11.3 #281

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

initialneil
Copy link

@initialneil initialneil commented Dec 12, 2022

The original code manually call gcc to compile the code JIT. I've changed the compiling tool to cmake+ninja on windows. All changes are wrapped with os.name == 'nt' to be compatible.

To use the code:

  1. set VCVARS64 to the vcvars64.bat file of your Visual Studio. Must do, because there's no other way to find your VS.
    image
    Do not add " in the variable

  2. make sure cmake is available.
    image

  3. make sure pytorch is available.
    image

  4. clone the repo

git clone https://github.com/initialneil/keops.git
cd keops
  1. install keopscore and pykeops. Must use develop mode.
cd keopscore
python setup.py develop
cd ..

cd pykeops
python setup.py develop
cd ..
  1. goto test to check bindings
cd test
python test_keopscore.py

image
JIT files will be generated and copied to C:\Users\Admin\.cache\keops2.1\build

  1. if you encounter CUDA Error, please delete all files in C:\Users\Admin\.cache\keops2.1\build and use step 6 again to regenerate the JIT files. Do not run pykeops.test_numpy_bindings() in keopscore or pykeops folders.

PS: About the change

  • CUDA version is auto found by torch.version.cuda to be compatible with any CUDA version.
  • Support both cmd and powershell.
  • Passing long to pybind is not supported on windows (raise type error), changed to tuple.
  • long is not supported either, changed to int64_t.
  • int[] of dynamic size is not supported (not like gcc does), changed to vector<int>.

@jeanfeydy
Copy link
Contributor

Thanks a lot for your great work @initialneil !
Now that PyTorch 2.0 is out, I'm starting to review your code. I will try it locally on a Windows computer, and we will also try to setup a Windows CI before making the merge. I will add a few comments/questions in the code: if you have time to answer them, that would be fantastic. Otherwise, we should still be able to manage.

@jeanfeydy jeanfeydy mentioned this pull request Mar 28, 2023
@initialneil
Copy link
Author

@jeanfeydy No problems. I'm happy to help. Thanks for the great work.

@adam-hartshorne
Copy link

I have just tried this code with VS2022 and although the setup scripts run without issue, running the test results in the following rather unhelpful error. This is with python 3.10 on Windows 11.

[KeOps] Generating code for formula Sum_Reduction((Var(0,3,0)-Var(1,3,1))|(Var(0,3,0)-Var(1,3,1)),1) ... Traceback (most recent call last):
  File "D:\windows_sdks\keops\test\test_keopscore.py", line 15, in <module>
    pykeops.test_numpy_bindings()
  File "d:\windows_sdks\keops\pykeops\pykeops\numpy\test_install.py", line 20, in test_numpy_bindings
    if np.allclose(my_conv(x, y).flatten(), expected_res):
  File "d:\windows_sdks\keops\pykeops\pykeops\numpy\generic\generic_red.py", line 303, in __call__
    self.myconv = keops_binder["nvrtc" if tagCPUGPU else "cpp"](
  File "d:\windows_sdks\keops\keopscore\keopscore\utils\Cache.py", line 68, in __call__
    obj = self.cls(*args)
  File "d:\windows_sdks\keops\pykeops\pykeops\common\keops_io\LoadKeOps_nvrtc.py", line 15, in __init__
    super().__init__(*args, fast_init=fast_init)
  File "d:\windows_sdks\keops\pykeops\pykeops\common\keops_io\LoadKeOps.py", line 18, in __init__
    self.init(*args)
  File "d:\windows_sdks\keops\pykeops\pykeops\common\keops_io\LoadKeOps.py", line 125, in init
    ) = get_keops_dll(
  File "d:\windows_sdks\keops\keopscore\keopscore\utils\Cache.py", line 27, in __call__
    self.library[str_id] = self.fun(*args)
  File "d:\windows_sdks\keops\keopscore\keopscore\get_keops_dll.py", line 124, in get_keops_dll_impl
    res = map_reduce_obj.get_dll_and_params()
  File "d:\windows_sdks\keops\keopscore\keopscore\binders\LinkCompile.py", line 101, in get_dll_and_params
    self.generate_code()
  File "d:\windows_sdks\keops\keopscore\keopscore\binders\nvrtc\Gpu_link_compile.py", line 74, in generate_code
    self.my_c_dll.Compile(
OSError: [WinError -529697949] Windows Error 0xe06d7363

@initialneil
Copy link
Author

@adam-hartshorne

  1. Gpu_link_compile.py, line 74 is to use nvrtc_jit.dll to compile the needed file JIT. Can you check the nvrtc_jit.dll exist?
    image

  2. Post the print result from the test file would help.

    print('libcuda =', get_compiler_library('libcuda'))
    print('libnvrtc =', get_compiler_library('libnvrtc'))
    print('libcudart =', get_compiler_library('libcudart'))

@adam-hartshorne
Copy link

[KeOps] Warning : There were warnings or errors compiling formula :
CUDA_CUDART_LIBRARY = C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/lib/x64/cudart.lib
CUDA_LIBRARIES = C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/lib/x64/cudart_static.lib
CUDA_TOOLKIT_ROOT_DIR = C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7
set nvrtc path = C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/lib/x64
pybind11_INCLUDE_DIRS = C:/Users/Adam/anaconda3/envs/pytorch/Lib/site-packages/pybind11-2.10.4-py3.10.egg/pybind11/include;C:/Users/Adam/anaconda3/envs/pytorch/Include
PYTHON_LIBRARY = C:/Users/Adam/anaconda3/envs/pytorch/libs/python310.lib

OK
libcuda = nvcuda
libnvrtc = nvrtc-builtins64_117
libcudart = cudart64_110
<CDLL 'C:\Users\Adam\.cache\keops2.1.1\Windows_AdamPC_10_p3.10.10\nvrtc_jit.dll', handle 7ff80b4b0000 at 0x1915099d4b0>

@artths
Copy link

artths commented May 8, 2023

Windows 11, VS2022, Python 3.9, Cuda 11.6

OK
[KeOps] Generating code for formula Sum_Reduction((Var(0,3,0)-Var(1,3,1))|(Var(0,3,0)-Var(1,3,1)),1) ... OK

[KeOps] error: cuMemcpyDtoH(out, (CUdeviceptr) out_d, sizeof(TYPE) * sizeout) failed with error CUDA_ERROR_INVALID_VALUE

Traceback (most recent call last):
  File "D:\Downloads\keops-main\keops-main\test\test_keopscore.py", line 15, in <module>
    pykeops.test_numpy_bindings()
  File "D:\Downloads\keops-main\keops-main\pykeops\pykeops\numpy\test_install.py", line 20, in test_numpy_bindings
    if np.allclose(my_conv(x, y).flatten(), expected_res):
  File "D:\Downloads\keops-main\keops-main\pykeops\pykeops\numpy\generic\generic_red.py", line 347, in __call__
    out = self.myconv.genred_numpy(-1, ranges, nx, ny, nbatchdims, out, *args)
  File "D:\Downloads\keops-main\keops-main\pykeops\pykeops\common\keops_io\LoadKeOps.py", line 230, in genred
    self.call_keops(nx, ny)
  File "D:\Downloads\keops-main\keops-main\pykeops\pykeops\common\keops_io\LoadKeOps_nvrtc.py", line 42, in call_keops
    self.launch_keops(
RuntimeError: [KeOps] Cuda error.

However, the GeomLoss I need works.

@TanCari
Copy link

TanCari commented Jul 10, 2023

Hi guys and thank you very much for your great job!

I would be really eager to try keops on my windows computer, but I am stuck on the following

Problem: No way for me to find the vcvars64.bat file for VS Code. Would you have a suggestion for me? Maybe a smart way to look for it?

Thank you very much!

@initialneil
Copy link
Author

Hi guys and thank you very much for your great job!

I would be really eager to try keops on my windows computer, but I am stuck on the following

Problem: No way for me to find the vcvars64.bat file for VS Code. Would you have a suggestion for me? Maybe a smart way to look for it?

Thank you very much!

@TanCari Hi, the vcvars64.bat file come with Visual Studio, not VSCode.

@JurinJC
Copy link

JurinJC commented Jun 26, 2024

I tried the above method to configure Keops on Windows, but encountered some problems. My torch information, cuda information and problems are as follows:
企业微信截图_17194111669991
企业微信截图_17194112602069
企业微信截图_17194112774534

@JurinJC
Copy link

JurinJC commented Jun 27, 2024

python test_keopscore.py
I have the same problem. Have you solved it?

@abrahamezzeddine
Copy link

abrahamezzeddine commented Dec 22, 2024

python test_keopscore.py
I have the same problem. Have you solved it?

Hello,

In keops\keopscore\config.py, you have a function

def get_compiler_library(lib_key):
    if lib_key == 'libcuda':
        if 'libcuda' in os.environ:
            return os.environ['libcuda']
        else:
            if os.name =='nt':
                return 'nvcuda'
            else:
                return 'cuda'

    if lib_key == 'libnvrtc':
        if 'libnvrtc' in os.environ:
            return os.environ['libnvrtc']
        else:
            if os.name =='nt':
                # nvrtc in windows depends on cuda version
                # e.g. 'nvrtc-builtins64_113' is for cuda '11.3'
                import torch
                cuda_ver = torch.version.cuda
                nvrtc_name = 'nvrtc-builtins64_' + cuda_ver.replace('.', '')
                return nvrtc_name
            else:
                return 'nvrtc'

    if lib_key == 'libcudart':
        if 'libcudart' in os.environ:
            return os.environ['libcudart']
        else:
            if os.name =='nt':
                # nvrtc in windows depends on cuda major version
                # e.g. 'cudart64_110' is for cuda '11.x'
                import torch
                cuda_ver = torch.version.cuda
                nvrtc_name = 'cudart64_' + cuda_ver.split('.')[0] + '0'
                return nvrtc_name
            else:
                return 'nvrtc'

    return None

Above code is adding an extra 0 to the cudart64 file which does not align with higher cuda versions? Change to code below and it will work. @JurinJC


def get_compiler_library(lib_key):
    if lib_key == 'libcuda':
        if 'libcuda' in os.environ:
            return os.environ['libcuda']
        else:
            if os.name == 'nt':
                return 'nvcuda'
            else:
                return 'cuda'

    if lib_key == 'libnvrtc':
        if 'libnvrtc' in os.environ:
            return os.environ['libnvrtc']
        else:
            if os.name == 'nt':
                import torch
                cuda_ver = torch.version.cuda
                # Adjusted to remove the extra '0'
                nvrtc_name = 'nvrtc-builtins64_' + cuda_ver.replace('.', '')
                return nvrtc_name
            else:
                return 'nvrtc'

    if lib_key == 'libcudart':
        if 'libcudart' in os.environ:
            return os.environ['libcudart']
        else:
            if os.name == 'nt':
                import torch
                cuda_ver = torch.version.cuda
                **# Remove the extra '0' to match 'cudart64_12'**
                cudart_name = 'cudart64_' + cuda_ver.split('.')[0]
                return cudart_name
            else:
                return 'nvrtc'

    return None

then it would proceed and find the right file, but I still encounter an error after it;

Same error as @artths
OK
[KeOps] Generating code for formula Sum_Reduction((Var(0,3,0)-Var(1,3,1))|(Var(0,3,0)-Var(1,3,1)),1) ... OK

[KeOps] error: cuMemcpyDtoH(out, (CUdeviceptr) out_d, sizeof(TYPE) * sizeout) failed with error CUDA_ERROR_INVALID_VALUE

Found a solution to it?

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

Successfully merging this pull request may close these issues.

7 participants