Skip to content

Fix MSVC build with CUDA enabled - #116926

Closed
pbelcak wants to merge 1 commit into
pytorch:mainfrom
pbelcak:nvtx-3-fix
Closed

pbelcak wants to merge 1 commit into
pytorch:mainfrom
pbelcak:nvtx-3-fix

Conversation

@pbelcak

@pbelcak pbelcak commented Jan 6, 2024 •

Copy link
Copy Markdown

@pytorch-bot

pytorch-bot Bot commented Jan 6, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/116926

Note: Links to docs will display an error until the docs builds have been completed.

❌ 4 New Failures

As of commit 8f16fd3 with merge base 2dca3e9 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jan 6, 2024 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions Bot added the module: cpu CPU specific problem (e.g., perf, algorithm) label Jan 6, 2024
@pbelcak

pbelcak commented Jan 6, 2024

Copy link
Copy Markdown
Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Jan 6, 2024
@pbelcak

pbelcak commented Jan 6, 2024

Copy link
Copy Markdown
Author

Also fixes #116242

@pytorch-bot

pytorch-bot Bot commented Jan 6, 2024

Copy link
Copy Markdown

Didn't find following labels among repository labels: module:cuda

@pbelcak

pbelcak commented Jan 6, 2024

Copy link
Copy Markdown
Author

@pytorchbot label "module: cuda"

@pytorch-bot pytorch-bot Bot added the module: cuda Related to torch.cuda, and CUDA support in general label Jan 6, 2024
@Skylion007

Skylion007 commented Jan 6, 2024 •

Copy link
Copy Markdown
Collaborator

@pbelcak For the NVTX issue, you should probably take a look at this PR: #109843 . It tries to add NVTX3 as a PyTorch submodule, but was never merged. cc: @cyyever

@Skylion007

Copy link
Copy Markdown
Collaborator

@atalman Is the signal for NVTX3 Green Internally yet? #109843

@pbelcak

pbelcak commented Jan 6, 2024 •

Copy link
Copy Markdown
Author

@atalman Is the signal for NVTX3 Green Internally yet? #109843

Not sure if this PR depends on it (though it might); building pytorch with the CUDA Toolkit 12.x installed out of the box simply fails on Windows because the cudaToolsExt libraries are no longer there; the PR makes changes that alleviate this.

@malfet malfet added release notes: build release notes category topic: bug fixes topic category and removed topic: not user facing topic category labels Jan 6, 2024

@malfet malfet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the change, but do you mind splitting this one int two PRs:

  • One that fixes compilation problem in vec_base.h (please mention the compiler error that were generated before it applied)
  • Another that fixes nvtx discovery (though I it would be better to modify torch:nvtoolsext discovery logic with trying to find it in the cuda toolkit folder and then fallback to the one embedded into the nccl, rather than add CUDA_VERSION_MAJOR > 12 condition, which is not actually correct, nvtx is shipped with the toolkit but no longer installed by default)

// identifier is odr-used or not, and in any case it's hard to tell if
// a variable is odr-used or not. So best to just cut the problem at the root.
static constexpr size_type size_T = sizeof(T); // Workaround to compile with VS2022.
//static constexpr size_type size_T = sizeof(T); // Workaround to compile with VS2022.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind deleting this line altogether? And half of the preamble about not supporting C++17 yet?

@cyyever

cyyever commented Jan 8, 2024

Copy link
Copy Markdown
Collaborator

I can't find cudaToolsExt on Windows too, that is why I was pushing the PR. Unfortunately, some META builds should be fixed before it can be landed.

@ghost ghost mentioned this pull request Jan 15, 2024
@ghost

ghost commented Jan 15, 2024

Copy link
Copy Markdown

Since this PR seems stale for a week, I've created another one to just focus on MSVC error:
Fix MSVC 14.38 - VS 2022 Build

pytorchmergebot pushed a commit that referenced this pull request Jan 18, 2024
Fixes #115922

This PR is prepared to separate existing #116926 and to apply suggestions in the review.

`scalar_t` which is defined as `c10::impl::ScalarTypeToCPPType<ScalarType::Half>::t` appears to be causing the issue with `Visual Studio 2022 17.8.4`  (coming with `MSVC 14.38.33130`)

Error message:
```
aten\src\ATen/cpu/vec/vec_base.h(150): fatal error C1001: Internal compiler error.
(compiler file 'D:\a_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\toinil.c', line 910)
```

---

Related line was added for a similar issue before as a workaround (`scalar_t` definition) [Fix compile error for vs2022](#85958)

Pull Request resolved: #117497
Approved by: https://github.com/ezyang, https://github.com/malfet
@patrikhuber

Copy link
Copy Markdown

Thank you for the change, but do you mind splitting this one int two PRs:

  • One that fixes compilation problem in vec_base.h (please mention the compiler error that were generated before it applied)
  • Another that fixes nvtx discovery (though I it would be better to modify torch:nvtoolsext discovery logic with trying to find it in the cuda toolkit folder and then fallback to the one embedded into the nccl, rather than add CUDA_VERSION_MAJOR > 12 condition, which is not actually correct, nvtx is shipped with the toolkit but no longer installed by default)

Hi,

I've been encountering both of these issues too and I can provide a bit more information on them.

  1. The latest MSVC produced an ICE (internal compiler error). It looks like the workaround in vec_base that was originally needed for an earlier version of MSVC is now no longer needed in the latest MSVC, and in fact creates an ICE. I've logged this with the Visual Studio team https://developercommunity.visualstudio.com/t/post/10539393 but the easiest is, like done in this PR, just to remove the workaround.

  2. NVTX are indeed still present in the latest CUDA versions, but they're no longer a compiled library: It's now a header-only library. That's why there isn't a .lib file anymore. See here for more details: https://nvidia.github.io/NVTX/. Thus, I believe the best way forward is probably to update libtorch's CMake scripts so that they now look for the header-only version of NVTX if libtorch needs it. Or obviously if NVTX is not needed to build libtorch, it should be removed as a dependency in libtorch's CMake scripts.

@patrikhuber

Copy link
Copy Markdown

And just in case it's helpful, it's not a proper fix I believe, but I made the NvToolsExt issue go away by:

Adding in cuda.cmake L59 after find_package(CUDAToolkit REQUIRED):

add_library(CUDA::nvToolsExt INTERFACE IMPORTED)
set_property(TARGET CUDA::nvToolsExt APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CUDAToolkit_nvToolsExt_INCLUDE_DIRS}")

And in share/cmake/Torch/TorchConfig.cmake, this needs to be commented in the if(MSVC)... branch:

    set(TORCH_CUDA_LIBRARIES
      # ${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib
      ${CUDA_LIBRARIES})

And then I think I also needed to add this environment variable in Windows:
NVTOOLSEXT_PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\

A dirty workaround for sure but perhaps it helps someone to craft the right PR.

pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
pfxuan added a commit to pfxuan/OpenSplat that referenced this pull request Feb 25, 2024
@patrikhuber

Copy link
Copy Markdown

Any update on the NVTX issue?

See my comment here for what's happening:

NVTX are indeed still present in the latest CUDA versions, but they're no longer a compiled library: It's now a header-only library. That's why there isn't a .lib file anymore. See here for more details: https://nvidia.github.io/NVTX/. Thus, I believe the best way forward is probably to update libtorch's CMake scripts so that they now look for the header-only version of NVTX if libtorch needs it. Or obviously if NVTX is not needed to build libtorch, it should be removed as a dependency in libtorch's CMake scripts.

@cyyever

cyyever commented Mar 17, 2024

Copy link
Copy Markdown
Collaborator

@patrikhuber We had some troubles when imported the previous NVTX PR into META since some inner builds failed.

@patrikhuber

Copy link
Copy Markdown

@patrikhuber We had some troubles when imported the previous NVTX PR into META since some inner builds failed.

I'm not sure what you mean with "previous NVTX PR" but I'm not necessarily advocating to merge this PR, I'm just saying this is quite a big issue (basically it breaks using/importing the library with CUDA 12 on Windows) and it would be nice to get it fixed.

pytorchbot pushed a commit that referenced this pull request Mar 18, 2024
Fixes #115922

This PR is prepared to separate existing #116926 and to apply suggestions in the review.

`scalar_t` which is defined as `c10::impl::ScalarTypeToCPPType<ScalarType::Half>::t` appears to be causing the issue with `Visual Studio 2022 17.8.4`  (coming with `MSVC 14.38.33130`)

Error message:
```
aten\src\ATen/cpu/vec/vec_base.h(150): fatal error C1001: Internal compiler error.
(compiler file 'D:\a_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\toinil.c', line 910)
```

---

Related line was added for a similar issue before as a workaround (`scalar_t` definition) [Fix compile error for vs2022](#85958)

Pull Request resolved: #117497
Approved by: https://github.com/ezyang, https://github.com/malfet

(cherry picked from commit fa86fa7)
atalman pushed a commit that referenced this pull request Mar 18, 2024
Fixes #115922

This PR is prepared to separate existing #116926 and to apply suggestions in the review.

`scalar_t` which is defined as `c10::impl::ScalarTypeToCPPType<ScalarType::Half>::t` appears to be causing the issue with `Visual Studio 2022 17.8.4`  (coming with `MSVC 14.38.33130`)

Error message:
```
aten\src\ATen/cpu/vec/vec_base.h(150): fatal error C1001: Internal compiler error.
(compiler file 'D:\a_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\toinil.c', line 910)
```

---

Related line was added for a similar issue before as a workaround (`scalar_t` definition) [Fix compile error for vs2022](#85958)

Pull Request resolved: #117497
Approved by: https://github.com/ezyang, https://github.com/malfet

(cherry picked from commit fa86fa7)

Co-authored-by: Ozan Aydin <148207261+ozanMSFT@users.noreply.github.com>
@cyyever

cyyever commented Mar 19, 2024

Copy link
Copy Markdown
Collaborator

@patrikhuber I meant #109843

@ezyang ezyang added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Mar 22, 2024
@ezyang
ezyang requested a review from malfet March 22, 2024 17:21
@patrikhuber

patrikhuber commented Apr 2, 2024 •

Copy link
Copy Markdown

I think the proper way forward with this is that PyTorch has to use find_package(CUDAToolkit) and its CUDA::nvtx3 target - see here https://gitlab.kitware.com/cmake/cmake/-/issues/22893#note_1503430. @malfet @atalman who is the PyTorch CMake maintainer?

@ricardoassaad

Copy link
Copy Markdown

And just in case it's helpful, it's not a proper fix I believe, but I made the NvToolsExt issue go away by:

Adding in cuda.cmake L59 after find_package(CUDAToolkit REQUIRED):

add_library(CUDA::nvToolsExt INTERFACE IMPORTED)
set_property(TARGET CUDA::nvToolsExt APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CUDAToolkit_nvToolsExt_INCLUDE_DIRS}")

And in share/cmake/Torch/TorchConfig.cmake, this needs to be commented in the if(MSVC)... branch:

    set(TORCH_CUDA_LIBRARIES
      # ${NVTOOLEXT_HOME}/lib/x64/nvToolsExt64_1.lib
      ${CUDA_LIBRARIES})

And then I think I also needed to add this environment variable in Windows: NVTOOLSEXT_PATH = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\

A dirty workaround for sure but perhaps it helps someone to craft the right PR.

this worked for me. i am on cuda 11.8. I only needed to change the NVTOOLEXT_HOME and change this line list(APPEND TORCH_INCLUDE_DIRS ${NVTOOLEXT_HOME}) under the MSVC section in the TorchConfig.cmake. Thanks

@cyyever

cyyever commented Apr 3, 2024

Copy link
Copy Markdown
Collaborator

@patrikhuber That means we need to jump CMake version, which may affect packaging.

@patrikhuber

Copy link
Copy Markdown

@patrikhuber That means we need to jump CMake version, which may affect packaging.

It looks like the nvtx3 target was added in 3.25 https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html#findcudatoolkit.
In any case, if you don't want to bump the CMake version, some patch is needed to fix the problem.

@github-actions

github-actions Bot commented Jun 2, 2024

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Jun 2, 2024
@patrikhuber

Copy link
Copy Markdown

I guess if #109843 could be merged, then this could be closed. The other MSVC build problem that this PR tackled was already fixed and merged here #117497.

@pbelcak maybe you want to have a look at #109843?

In any case, @malfet, it would be really great if the NVTX CMake issues could finally be solved, merged, and released.

@github-actions github-actions Bot closed this Jul 2, 2024
@aleksandaratanasov

aleksandaratanasov commented Aug 14, 2024 •

Copy link
Copy Markdown

Bumping up the CMake version doesn't help since the affected TorchConfig.cmake, Caffe2's cuda.cmake and perhaps other files depending on the nvToolsExt binary library are shipped with the download of libtorch 11.8, 12.1 and possibly other versions.

In cuda.cmake from Caffe2 (found inside share\cmake\Caffe2\public of the unpacked libtorch ZIP archive) the dependency is on CUDA::nvToolsExt.

I know that libtorch is not really the best supported part of PyTorch out there but this is seriously breaking projects just because of a binary becoming a header-only library.

@aleksandaratanasov

Copy link
Copy Markdown

I have an older download of libtorch 2.1.0 where the DLL nvToolsExt64_1.dll can be found inside lib of the root directory of libtorch (wherever you have extracted the ZIP/tar archive).

@aleksandaratanasov

Copy link
Copy Markdown

This points a working solution (at least in my case) that does not involve copying DLLs from somewhere. The installer of CUDA 11.8 allows to select only the NXVT component that is then installed at C:/Program Files/NVIDIA Corporation/NvToolsExt.

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

Labels

module: cpu CPU specific problem (e.g., perf, algorithm) module: cuda Related to torch.cuda, and CUDA support in general open source release notes: build release notes category Stale topic: bug fixes topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pytorch build from source fails

9 participants