cmake 3.28.3 C++ LibTorch 2.5.1 + CUDA-12.6 + nvtx3

问题描述 投票:0回答:1

在 LibTorch 2.0.1 中,我无需安装即可使用该库(下载库并设置 CMAKE_PREFIX_PATH 以允许 CMake 找到该库)。使用 LibTorch 2.5.1 和 CUDA 12.6 我必须下载 nvToolsExt 并将其放置在路径中:{project}/third_party/NVTX/...

文件:

libtorch/share/cmake/Caffe2/public/cuda.cmake

find_path(nvtx3_dir NAMES nvtx3 PATHS "${PROJECT_SOURCE_DIR}/third_party/NVTX/c/include" NO_DEFAULT_PATH)
find_package_handle_standard_args(nvtx3 DEFAULT_MSG nvtx3_dir)

因此,有了 nvtx3 文件夹,cmake 就能够找到 nvtx3,但 CMAKE 仍然显示警告消息:

CMake Warning (dev) at /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:438 (message):
  The package name passed to `find_package_handle_standard_args` (nvtx3) does
  not match the name of the calling package (Caffe2).  This can lead to
-- Found nvtx3: /home/dev/Documents/projects_cpp/Forex/third_party/NVTX/c/include  
  problems in calling code that expects `find_package` result variables
  (e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
  /home/dev/Documents/libtorch_2_5_1/share/cmake/Caffe2/public/cuda.cmake:180 (find_package_handle_standard_args)
  /home/dev/Documents/libtorch_2_5_1/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
  /home/dev/Documents/libtorch_2_5_1/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
  CMakeLists.txt:131 (find_package)
This warning is for project developers.

更深入地说,标准 CMAKE

/usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake.txt
正在生成此警告:

  ``NAME_MISMATCHED``
    .. versionadded:: 3.17

    Indicate that the ``<PackageName>`` does not match
    ``${CMAKE_FIND_PACKAGE_NAME}``. This is usually a mistake and raises a
    warning, but it may be intentional for usage of the command for components
    of a larger package.
...
...
...
  if (DEFINED CMAKE_FIND_PACKAGE_NAME
      AND NOT FPHSA_NAME_MISMATCHED
      AND NOT _NAME STREQUAL CMAKE_FIND_PACKAGE_NAME)
    message(AUTHOR_WARNING
      "The package name passed to `find_package_handle_standard_args` "
      "(${_NAME}) does not match the name of the calling package "
      "(${CMAKE_FIND_PACKAGE_NAME}). This can lead to problems in calling "
      "code that expects `find_package` result variables (e.g., `_FOUND`) "
      "to follow a certain pattern.")
  endif ()

选项 1: 从上一个块中,我发现我可以修改 CMakeLists.txt 中的变量,以便警告不再显示:

set(FPHSA_NAME_MISMATCHED ON)

它似乎可以避免警告,但我不知道这是否会对其他库造成问题(我没有使用更多库)

问题是:我错过了什么吗?现在我的项目可以正确编译,没有任何警告。

选项2:

因为我知道我将 nvToolsExt 包含在文件夹 /third_party/NVTX 中...

我可以更新

libtorch/share/cmake/Caffe2/public/cuda.cmake
影响图书馆:

替换代码:

# nvToolsExt
find_path(nvtx3_dir NAMES nvtx3 PATHS "${PROJECT_SOURCE_DIR}/third_party/NVTX/c/include" NO_DEFAULT_PATH)
find_package_handle_standard_args(nvtx3 DEFAULT_MSG nvtx3_dir)
if(nvtx3_FOUND)
  add_library(torch::nvtx3 INTERFACE IMPORTED)
  target_include_directories(torch::nvtx3 INTERFACE "${nvtx3_dir}")
  target_compile_definitions(torch::nvtx3 INTERFACE TORCH_CUDA_USE_NVTX3)
else()
  message(WARNING "Cannot find NVTX3, find old NVTX instead")
  add_library(torch::nvtoolsext INTERFACE IMPORTED)
  set_property(TARGET torch::nvtoolsext PROPERTY INTERFACE_LINK_LIBRARIES CUDA::nvToolsExt)
endif()

作者:

# nvToolsExt
add_library(torch::nvtx3 INTERFACE IMPORTED)
target_include_directories(torch::nvtx3 INTERFACE "${PROJECT_SOURCE_DIR}/third_party/NVTX/c/include")
target_compile_definitions(torch::nvtx3 INTERFACE TORCH_CUDA_USE_NVTX3)

在这种情况下,CMAKE 能够在没有任何警告的情况下进行编译。

这些选项是否有效,或者有更好的解决方案来避免警告?

注意:我没有使用 nvToolsExt,因此删除添加它的脚本也对我有用。

c++ cmake cuda artificial-intelligence libtorch
1个回答
0
投票

我建议在深入研究源代码之前阅读文档。它说:

注意: 如果

<PackageName>
与调用模块的
CMAKE_FIND_PACKAGE_NAME
不匹配,则会发出不匹配的警告。如果使用旧签名,则可以设置
FPHSA_NAME_MISMATCHED
变量来绕过警告,而
NAME_MISMATCHED
参数则使用新签名。为了避免强制调用者需要使用较新版本的 CMake,如果在未为新签名传递
NAME_MISMATCHED
参数时定义了该变量的值,则将使用该变量的值(但使用两者都是错误的)。

所以,是的,选项 1 看起来不错(尽管我只是在配置时传递

-DFPHSA_NAME_MISMATCHED
,而不是更改任何 CMake 文件。

© www.soinside.com 2019 - 2024. All rights reserved.