Libtorch, Cannot find NVTX3, find old NVTX instead

Hey,

I am compiling (using CMake 3.28) a CPP solver that links to libtorch (2.5.1). However, there is a warning about NVTX3 as "-- Could NOT find nvtx3 (missing: nvtx3_dir) "

I find it comes from 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)
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()

I checked “{PROJECT_SOURCE_DIR}” variable, and it refers to my source codes directory (where .c files are located), and of course, there is no “third-party” folder.

Configuration:
libtorch version: stable 2.5.1
CUDA version: 12.4

Do you have ideas on how to address this warning and check where NVTX3 is in the computer? Thanks for your time.

Best, Weitao.

Hey guys,

I found a solution to this problem. You can download NVTX3 from GitHub - NVIDIA/NVTX: The NVIDIA® Tools Extension SDK (NVTX) is a C-based Application Programming Interface (API) for annotating events, code ranges, and resources in your applications.. Then, the directory pathway in libtorch/share/cmake/Caffe2/public/cuda.cmake should be adjusted correspondingly. Once you have the NVTX3, the warning will not appear.

Best, Weitao.

3 Likes

For at least pytorch 2.7 (where I encountered this problem), I noticed there’s a flag to use NVTX already installed on your system, enabling with -DUSE_SYSTEM_NVTX:BOOL=ON during cmake configure stage will fix this without needing to download anything else or change internal pytorch cmake code. This will then use the NVTX already included with CUDA Toolkit.

torch/share/cmake/Caffe2/public/cuda.cmake#L173

# nvToolsExt
if(USE_SYSTEM_NVTX)
  find_path(nvtx3_dir NAMES nvtx3 PATHS ${CUDA_INCLUDE_DIRS})
else()
  find_path(nvtx3_dir NAMES nvtx3 PATHS "${PROJECT_SOURCE_DIR}/third_party/NVTX/c/include" NO_DEFAULT_PATH)
endif()