gemini-code-assist[bot] commented on code in PR #19593: URL: https://github.com/apache/tvm/pull/19593#discussion_r3281963720
########## cmake/modules/CUDA.cmake: ########## @@ -50,90 +50,109 @@ if(USE_CUDA) # [0] https://github.com/Kitware/CMake/commit/6377a438 set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0) - tvm_file_glob(GLOB RUNTIME_CUDA_SRCS src/runtime/cuda/*.cc) - list(APPEND RUNTIME_SRCS ${RUNTIME_CUDA_SRCS}) - - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDART_LIBRARY}) - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDA_LIBRARY}) - if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) if(CMAKE_VERSION VERSION_LESS "3.24") message(FATAL_ERROR "CMAKE_CUDA_ARCHITECTURES not set. Please upgrade CMake to 3.24 to use native, or set CMAKE_CUDA_ARCHITECTURES manually") endif() message(STATUS "CMAKE_CUDA_ARCHITECTURES not set, using native") set(CMAKE_CUDA_ARCHITECTURES native) endif() +endif(USE_CUDA) - if(USE_CUDNN) - message(STATUS "Build with cuDNN support") - include_directories(SYSTEM ${CUDA_CUDNN_INCLUDE_DIRS}) - tvm_file_glob(GLOB CUDNN_RELAX_CONTRIB_SRC src/relax/backend/contrib/cudnn/*.cc) - list(APPEND COMPILER_SRCS ${CUDNN_RELAX_CONTRIB_SRC}) - tvm_file_glob(GLOB CONTRIB_CUDNN_SRCS src/runtime/contrib/cudnn/*.cc) - list(APPEND RUNTIME_SRCS ${CONTRIB_CUDNN_SRCS}) - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDNN_LIBRARY}) - endif(USE_CUDNN) - - if (USE_CUDNN_FRONTEND) - message(STATUS "Build with cuDNN Frontend support") - if (IS_DIRECTORY ${USE_CUDNN_FRONTEND}) - find_file(CUDNN_FRONTEND_HEADER cudnn_frontend.h HINTS ${USE_CUDNN_FRONTEND}/include) - include_directories(SYSTEM ${USE_CUDNN_FRONTEND}/include) - else() - find_file(CUDNN_FRONTEND_HEADER cudnn_frontend.h) - endif() - if (NOT CUDNN_FRONTEND_HEADER) - message(FATAL_ERROR "Cannot find cudnn_frontend.h, please set USE_CUDNN_FRONTEND to the path of the cuDNN frontend header") - endif() - tvm_file_glob(GLOB CONTRIB_CUDNN_FRONTEND_SRCS src/runtime/contrib/cudnn/cudnn_frontend/*.cc) - set_property(SOURCE ${CONTRIB_CUDNN_SRCS} APPEND PROPERTY COMPILE_DEFINITIONS TVM_USE_CUDNN_FRONTEND=1) - list(APPEND RUNTIME_SRCS ${CONTRIB_CUDNN_FRONTEND_SRCS}) - endif(USE_CUDNN_FRONTEND) - - if(USE_CUBLAS) - message(STATUS "Build with cuBLAS support") - tvm_file_glob(GLOB CUBLAS_CONTRIB_SRC src/relax/backend/contrib/cublas/*.cc) - list(APPEND COMPILER_SRCS ${CUBLAS_CONTRIB_SRC}) - tvm_file_glob(GLOB CONTRIB_CUBLAS_SRCS src/runtime/contrib/cublas/*.cc) - list(APPEND RUNTIME_SRCS ${CONTRIB_CUBLAS_SRCS}) - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUBLAS_LIBRARY}) - if(NOT CUDA_CUBLASLT_LIBRARY STREQUAL "CUDA_CUBLASLT_LIBRARY-NOTFOUND") - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUBLASLT_LIBRARY}) - endif() - endif(USE_CUBLAS) +if(USE_CUDA) + message(STATUS "Build cuda device runtime") - if(USE_THRUST) - message(STATUS "Build with Thrust support") - tvm_file_glob(GLOB CONTRIB_THRUST_SRC src/runtime/contrib/thrust/*.cu) - add_library(tvm_thrust_objs OBJECT ${CONTRIB_THRUST_SRC}) - target_link_libraries(tvm_thrust_objs PRIVATE tvm_ffi_header) - target_compile_options(tvm_thrust_objs PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--expt-extended-lambda>) - if (NOT USE_THRUST MATCHES ${IS_TRUE_PATTERN}) - find_package(CCCL REQUIRED COMPONENTS Thrust) - target_link_libraries(tvm_thrust_objs PRIVATE CCCL::Thrust) - endif() - list(APPEND TVM_RUNTIME_EXT_OBJS $<TARGET_OBJECTS:tvm_thrust_objs>) - endif(USE_THRUST) + tvm_file_glob(GLOB RUNTIME_CUDA_SRCS src/runtime/cuda/*.cc) + tvm_file_glob(GLOB VM_CUDA_BUILTIN_SRC_CC src/runtime/vm/cuda/*.cc) - if(USE_CURAND) - message(STATUS "Build with cuRAND support") - message(STATUS "${CUDA_CURAND_LIBRARY}") - tvm_file_glob(GLOB CONTRIB_CURAND_SRC_CC src/runtime/contrib/curand/*.cc) - tvm_file_glob(GLOB CONTRIB_CURAND_SRC_CU src/runtime/contrib/curand/*.cu) - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CURAND_LIBRARY}) - list(APPEND RUNTIME_SRCS ${CONTRIB_CURAND_SRC_CC}) - list(APPEND RUNTIME_SRCS ${CONTRIB_CURAND_SRC_CU}) - endif(USE_CURAND) + add_library(tvm_runtime_cuda_objs OBJECT ${RUNTIME_CUDA_SRCS} ${VM_CUDA_BUILTIN_SRC_CC}) + target_link_libraries(tvm_runtime_cuda_objs PUBLIC tvm_ffi_header) + set_target_properties(tvm_runtime_cuda_objs PROPERTIES POSITION_INDEPENDENT_CODE ON) + if(TVM_VISIBILITY_FLAG) + target_compile_options(tvm_runtime_cuda_objs PRIVATE "${TVM_VISIBILITY_FLAG}") + endif() + add_library(tvm_runtime_cuda SHARED $<TARGET_OBJECTS:tvm_runtime_cuda_objs>) + target_link_libraries(tvm_runtime_cuda PUBLIC tvm_runtime ${CUDA_CUDART_LIBRARY} ${CUDA_CUDA_LIBRARY}) + if(TVM_NO_UNDEFINED_SYMBOLS) + set_property(TARGET tvm_runtime_cuda APPEND PROPERTY LINK_OPTIONS "${TVM_NO_UNDEFINED_SYMBOLS}") + endif() + set_target_properties(tvm_runtime_cuda PROPERTIES + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + ) + install(TARGETS tvm_runtime_cuda DESTINATION lib${LIB_SUFFIX}) + if(TVM_BUILD_PYTHON_MODULE) + install(TARGETS tvm_runtime_cuda DESTINATION "lib") + endif() if(USE_NVTX) message(STATUS "Build with NVTX support") - message(STATUS "${CUDA_NVTX_LIBRARY}") - cmake_minimum_required(VERSION 3.13) # to compile CUDA code - enable_language(CUDA) - list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_NVTX_LIBRARY}) - endif(USE_NVTX) - - # Add CUDA builtins to RelaxVM - tvm_file_glob(GLOB VM_CUDA_BUILTIN_SRC_CC src/runtime/vm/cuda/*.cc) - list(APPEND RUNTIME_SRCS ${VM_CUDA_BUILTIN_SRC_CC}) + target_link_libraries(tvm_runtime_cuda PRIVATE ${CUDA_NVTX_LIBRARY}) + endif() endif(USE_CUDA) + +# Contrib sources gated by USE_CUDA go into libtvm_runtime_extra. +# See the RuntimeExtra assembly block in CMakeLists.txt. + +if(USE_CUDA AND USE_CUDNN) + message(STATUS "Build with cuDNN support") + include_directories(SYSTEM ${CUDA_CUDNN_INCLUDE_DIRS}) + tvm_file_glob(GLOB CUDNN_RELAX_CONTRIB_SRC src/relax/backend/contrib/cudnn/*.cc) + list(APPEND COMPILER_SRCS ${CUDNN_RELAX_CONTRIB_SRC}) + tvm_file_glob(GLOB CONTRIB_CUDNN_SRCS src/runtime/contrib/cudnn/*.cc) + list(APPEND RUNTIME_EXTRA_SRCS ${CONTRIB_CUDNN_SRCS}) + list(APPEND TVM_RUNTIME_EXTRA_LINKER_LIBS ${CUDA_CUDNN_LIBRARY}) +endif(USE_CUDNN) + +if(USE_CUDA AND USE_CUDNN_FRONTEND) + message(STATUS "Build with cuDNN Frontend support") + if(IS_DIRECTORY ${USE_CUDNN_FRONTEND}) + find_file(CUDNN_FRONTEND_HEADER cudnn_frontend.h HINTS ${USE_CUDNN_FRONTEND}/include) + include_directories(SYSTEM ${USE_CUDNN_FRONTEND}/include) + else() + find_file(CUDNN_FRONTEND_HEADER cudnn_frontend.h) + endif() + if(NOT CUDNN_FRONTEND_HEADER) + message(FATAL_ERROR "Cannot find cudnn_frontend.h, please set USE_CUDNN_FRONTEND to the path of the cuDNN frontend header") + endif() + tvm_file_glob(GLOB CONTRIB_CUDNN_FRONTEND_SRCS src/runtime/contrib/cudnn/cudnn_frontend/*.cc) + list(APPEND RUNTIME_EXTRA_SRCS ${CONTRIB_CUDNN_FRONTEND_SRCS}) + # CUDNN frontend compile definition applied via the runtime_extra_objs target + set(TVM_CUDNN_FRONTEND_SRCS "${CONTRIB_CUDNN_FRONTEND_SRCS}" CACHE INTERNAL "") Review Comment:  The `TVM_USE_CUDNN_FRONTEND=1` compile definition is no longer being applied to the cuDNN source files. In the previous implementation, this was done via `set_property` on `CONTRIB_CUDNN_SRCS`. The new code defines a cache variable `TVM_CUDNN_FRONTEND_SRCS` but doesn't use it to apply the necessary definition, which will likely break cuDNN frontend support. ``` set_source_files_properties(${CONTRIB_CUDNN_SRCS} PROPERTIES COMPILE_DEFINITIONS TVM_USE_CUDNN_FRONTEND=1) ``` ########## python/tvm/libinfo.py: ########## @@ -39,108 +39,39 @@ def package_lib_paths() -> list[Path]: Anchored on this file's location (``python/tvm/libinfo.py``), the list covers the wheel-install layout (``python/tvm/lib/``) and the in-tree dev - build layouts (``<worktree>/build/lib/`` and ``<worktree>/lib/``). Callers + build layouts (``<worktree>/build/lib/`` and ``<worktree>/lib/``). + ``TVM_LIBRARY_PATH`` is prepended when set so it takes priority. Callers pick the basenames they want (e.g. ``libtvm_runtime.so``) and the load mode; this function only returns the search path. """ pkg = Path(__file__).parent # python/tvm/ - return [ + paths: list[Path] = [] + if os.environ.get("TVM_LIBRARY_PATH"): + paths.append(Path(os.environ["TVM_LIBRARY_PATH"])) + paths += [ pkg / "lib", # wheel layout pkg.parent.parent / "build" / "lib", # dev: <worktree>/build/lib pkg.parent.parent / "lib", # dev: <worktree>/lib ] + return paths -# Mirror of ``tvm_ffi.libinfo.{load_lib_ctypes,_find_library_by_basename}`` with -# the ``extra_lib_paths`` parameter from apache/tvm-ffi#570 so dev-mode lookups -# anchor on the *caller's* package root rather than tvm-ffi's own ``__file__``. -# Once apache/tvm-ffi#570 lands and the submodule bumps, drop these and switch -# ``base.py`` back to ``from tvm_ffi.libinfo import load_lib_ctypes``. - +_BACKEND_RUNTIME_LIBS = ["cuda", "vulkan", "opencl", "metal", "rocm", "hexagon", "extra"] -def _find_library_by_basename( - package: str, - target_name: str, - extra_lib_paths: list[Path] | None = None, -) -> Path: - """Resolve ``lib<target_name>.{so,dylib,dll}`` for ``package``. - Search order: wheel-install RECORD walk → caller-supplied - ``extra_lib_paths`` → ``PATH`` / ``LD_LIBRARY_PATH`` / - ``DYLD_LIBRARY_PATH``. Raises ``RuntimeError`` listing every candidate - directory tried if nothing matches. - """ - if sys.platform.startswith("win32"): - lib_dll_names = (f"{target_name}.dll",) - elif sys.platform.startswith("darwin"): - lib_dll_names = (f"lib{target_name}.dylib", f"lib{target_name}.so") - else: - lib_dll_names = (f"lib{target_name}.so",) - - try: - dist = im.distribution(package) - record = dist.read_text("RECORD") or "" - for line in record.splitlines(): - partial_path, *_ = line.split(",") - if partial_path.endswith(lib_dll_names): - try: - path = (dist._path.parent / partial_path).resolve() - except OSError: - continue - if path.name in lib_dll_names and path.is_file(): - return path - except (im.PackageNotFoundError, OSError): - pass - - dll_paths: list[Path] = [] - if extra_lib_paths is not None: - for i, p in enumerate(extra_lib_paths): - if not isinstance(p, Path): - raise TypeError( - f"extra_lib_paths[{i}] must be a pathlib.Path, got {type(p).__name__}: {p!r}" - ) - dll_paths.extend(extra_lib_paths) - - if sys.platform.startswith("win32"): - dll_paths.extend(Path(p) for p in split_env_var("PATH", ";")) - elif sys.platform.startswith("darwin"): - dll_paths.extend(Path(p) for p in split_env_var("DYLD_LIBRARY_PATH", ":")) - dll_paths.extend(Path(p) for p in split_env_var("PATH", ":")) - else: - dll_paths.extend(Path(p) for p in split_env_var("LD_LIBRARY_PATH", ":")) - dll_paths.extend(Path(p) for p in split_env_var("PATH", ":")) - - for d in dll_paths: - for name in lib_dll_names: - try: - path = (d / name).resolve() - except OSError: - continue - if path.is_file(): - return path - - raise RuntimeError( - f"Cannot find library {', '.join(lib_dll_names)}; searched directories:\n " - + "\n ".join(str(p) for p in dll_paths) - ) - - -def load_lib_ctypes( - package: str, - target_name: str, - mode: str, - extra_lib_paths: list[Path] | None = None, -) -> ctypes.CDLL: - """Locate and ``ctypes.CDLL``-load ``lib<target_name>`` for ``package``. - - ``mode`` is one of ``"RTLD_LOCAL"`` / ``"RTLD_GLOBAL"`` (resolved against - ``ctypes``). On Windows, the library's directory is registered via - ``os.add_dll_directory`` before the load. - """ - lib_path = _find_library_by_basename(package, target_name, extra_lib_paths) - if sys.platform.startswith("win32"): - os.add_dll_directory(str(lib_path.parent)) - return ctypes.CDLL(str(lib_path), getattr(ctypes, mode)) +def load_backend_libs(runtime_lib_path: str) -> None: + """Try to load each known backend runtime DSO; failures are silent.""" + runtime_dir = Path(runtime_lib_path).resolve().parent + for backend in _BACKEND_RUNTIME_LIBS: + try: + load_lib_ctypes( + package="apache-tvm", Review Comment:  The package name used here is `"apache-tvm"`, whereas `python/tvm/base.py` uses `"tvm"`. This inconsistency can lead to failures when `load_lib_ctypes` attempts to resolve the library path via package metadata, as the distribution name might be registered as `tvm` in many environments. It is recommended to use `"tvm"` for consistency with the rest of the codebase. ```suggestion package="tvm", ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
