In LibTorch 2.0.1 I was able to use the library without installing it (download library and set CMAKE_PREFIX_PATH
to allow CMake to find the library). Using LibTorch 2.5.1 and CUDA 12.6 I have to download nvToolsExt and place it in the path: {project}/third_party/NVTX/...
File: 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)
So having the nvtx3 folder there, CMake is able to find nvtx3, but still CMake shows the warning message:
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.
Going deeper, the standard CMake /usr/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake.txt
is generating this warning:
``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 ()
Option 1:
From the previous block I found that I can modify the variable in my CMakeLists.txt
so the warning does not show up anymore:
set(FPHSA_NAME_MISMATCHED ON)
It seems it worked to avoid the warning, but I don't know if this can be problematic for other libraries (I am not using more libraries).
The question is: Am I missing something? Now my project compiles properly without any warning.
Option 2:
Since I know that I am including nvToolsExt in the folder /third_party/NVTX...
I could update libtorch/share/cmake/Caffe2/public/cuda.cmake
affecting the library:
replace
# 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()
by
# 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)
In this case CMake is able to compile without any warning.
Are any of those options valid, or there is a better solution to avoid the warning?
Note: I am not using nvToolsExt, so removing the script that adds it also worked for me.
I would recommend reading the documentation before diving into the source code. It says:
Note: If
<PackageName>
does not matchCMAKE_FIND_PACKAGE_NAME
for the calling module, a warning that there is a mismatch is given. TheFPHSA_NAME_MISMATCHED
variable may be set to bypass the warning if using the old signature and theNAME_MISMATCHED
argument using the new signature. To avoid forcing the caller to require newer versions of CMake for usage, the variable's value will be used if defined when theNAME_MISMATCHED
argument is not passed for the new signature (but using both is an error).
So yes, option 1 seems fine (although I would just pass -DFPHSA_NAME_MISMATCHED
at configuration time instead of changing any CMake files.