getkeops / keops

KErnel OPerationS, on CPUs and GPUs, with autodiff and without memory overflows
https://www.kernel-operations.io
MIT License
1.04k stars 64 forks source link

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

Open initialneil opened 1 year ago

initialneil commented 1 year ago

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
  1. make sure cmake is available. image

  2. make sure pytorch is available. image

  3. clone the repo

    git clone https://github.com/initialneil/keops.git
    cd keops
  4. install keopscore and pykeops. Must use develop mode.

    
    cd keopscore
    python setup.py develop
    cd ..

cd pykeops python setup.py develop cd ..


6. goto `test` to check bindings

cd test python test_keopscore.py


![image](https://user-images.githubusercontent.com/4156648/207049116-c7373d25-cc75-454c-a473-c7f349f872f7.png)
JIT files will be generated and copied to `C:\Users\Admin\.cache\keops2.1\build`

7. 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 commented 1 year ago

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.

initialneil commented 1 year ago

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

adam-hartshorne commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago
[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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 3 months ago

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 commented 3 months ago

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