1616# libbitsandbytes_rocm70.so even if the system has ROCm 7.2.
1717cmake_minimum_required (VERSION 3.22.1 )
1818
19+ # On Windows with HIP backend, auto-detect compilers from ROCM_PATH before project()
20+ if (WIN32 AND COMPUTE_BACKEND STREQUAL "hip" )
21+ if (DEFINED ENV{ROCM_PATH})
22+ set (ROCM_PATH $ENV{ROCM_PATH} )
23+ endif ()
24+ if (ROCM_PATH AND NOT DEFINED CMAKE_CXX_COMPILER )
25+ set (CMAKE_CXX_COMPILER "${ROCM_PATH} /lib/llvm/bin/clang++.exe" )
26+ endif ()
27+ if (ROCM_PATH AND NOT DEFINED CMAKE_HIP_COMPILER)
28+ set (CMAKE_HIP_COMPILER "${ROCM_PATH} /lib/llvm/bin/clang++.exe" )
29+ endif ()
30+ # On Windows, the HIP compiler needs explicit paths to find device libraries.
31+ if (ROCM_PATH)
32+ find_path (ROCM_DEVICE_LIB_PATH
33+ NAMES oclc_abi_version_400.bc ocml.bc
34+ PATHS "${ROCM_PATH} /amdgcn/bitcode"
35+ "${ROCM_PATH} /lib/llvm/amdgcn/bitcode"
36+ NO_DEFAULT_PATH
37+ )
38+ set (CMAKE_HIP_FLAGS "--rocm-path=${ROCM_PATH} " )
39+ if (ROCM_DEVICE_LIB_PATH)
40+ set (CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} --rocm-device-lib-path=${ROCM_DEVICE_LIB_PATH} " )
41+ endif ()
42+ endif ()
43+ endif ()
44+
1945project (bitsandbytes LANGUAGES CXX )
2046
2147# If run without specifying a build type, default to using the Release configuration:
@@ -204,17 +230,18 @@ if(BUILD_CUDA)
204230 string (APPEND BNB_OUTPUT_NAME "_cuda${CUDA_VERSION_SHORT} " )
205231 add_compile_definitions (BUILD_CUDA )
206232elseif (BUILD_HIP)
207- enable_language (HIP )
208- message ( STATUS "HIP Compiler: ${CMAKE_HIP_COMPILER} " )
233+ # Set target architectures before enable_language(HIP), which would otherwise
234+ # auto-detect a single GPU and override the defaults.
209235 if (DEFINED BNB_ROCM_ARCH)
210236 set (CMAKE_HIP_ARCHITECTURES ${BNB_ROCM_ARCH} )
211- else ()
212- if (NOT AMDGPU_TARGETS AND NOT CMAKE_HIP_ARCHITECTURES)
213- set (CMAKE_HIP_ARCHITECTURES "gfx90a;gfx942;gfx1100;gfx1101;gfx1150;gfx1151;gfx1200;gfx1201" )
214- elseif (AMDGPU_TARGETS AND NOT CMAKE_HIP_ARCHITECTURES)
215- set (CMAKE_HIP_ARCHITECTURES ${AMDGPU_TARGETS} )
216- endif ()
237+ elseif (AMDGPU_TARGETS AND NOT CMAKE_HIP_ARCHITECTURES)
238+ set (CMAKE_HIP_ARCHITECTURES ${AMDGPU_TARGETS} )
239+ elseif (NOT CMAKE_HIP_ARCHITECTURES)
240+ set (CMAKE_HIP_ARCHITECTURES "gfx90a;gfx942;gfx1100;gfx1101;gfx1150;gfx1151;gfx1200;gfx1201" )
217241 endif ()
242+
243+ enable_language (HIP )
244+ message (STATUS "HIP Compiler: ${CMAKE_HIP_COMPILER} " )
218245 message (STATUS "HIP Targets: ${CMAKE_HIP_ARCHITECTURES} " )
219246
220247 list (APPEND SRC_FILES ${HIP_FILES} )
@@ -275,6 +302,8 @@ endif()
275302if (WIN32 )
276303 # Export all symbols
277304 set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON )
305+ # Prevent Windows SDK min/max macros from conflicting with std::min/std::max
306+ add_compile_definitions (NOMINMAX )
278307endif ()
279308
280309if (MSVC )
@@ -327,10 +356,11 @@ if(BUILD_CUDA)
327356 )
328357endif ()
329358if (BUILD_HIP)
330- if (NOT DEFINED ENV{ROCM_PATH})
331- set (ROCM_PATH /opt/rocm)
332- else ()
359+ # Determine ROCM_PATH from environment variable, fallback to /opt/rocm on Linux
360+ if (DEFINED ENV{ROCM_PATH})
333361 set (ROCM_PATH $ENV{ROCM_PATH} )
362+ else ()
363+ set (ROCM_PATH /opt/rocm)
334364 endif ()
335365 list (APPEND CMAKE_PREFIX_PATH ${ROCM_PATH} )
336366 macro (find_package_and_print_version PACKAGE_NAME )
@@ -342,14 +372,23 @@ if(BUILD_HIP)
342372 find_package_and_print_version (hipsparse REQUIRED )
343373
344374 ## hacky way of excluding hip::amdhip64 (with it linked many tests unexpectedly fail e.g. adam8bit because of inaccuracies)
345- set_target_properties (hip::host PROPERTIES INTERFACE_LINK_LIBRARIES "" )
346- set_target_properties (hip-lang::host PROPERTIES INTERFACE_LINK_LIBRARIES "" )
347- set (CMAKE_HIP_IMPLICIT_LINK_LIBRARIES "" )
375+ ## On Windows, we need to link amdhip64 explicitly
376+ if (NOT WIN32 )
377+ set_target_properties (hip::host PROPERTIES INTERFACE_LINK_LIBRARIES "" )
378+ set_target_properties (hip-lang::host PROPERTIES INTERFACE_LINK_LIBRARIES "" )
379+ set (CMAKE_HIP_IMPLICIT_LINK_LIBRARIES "" )
380+ endif ()
348381
349382 target_include_directories (bitsandbytes PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR} /include ${ROCM_PATH} /include /include )
350383 target_link_directories (bitsandbytes PRIVATE ${ROCM_PATH} /lib /lib )
351384 target_link_libraries (bitsandbytes PUBLIC roc::hipblas hip::hiprand roc::hipsparse )
352385
386+ # On Windows, rocblas is not pulled in transitively by roc::hipblas
387+ # and is needed because ops_hip.cuh uses rocblas_handle directly.
388+ if (WIN32 )
389+ target_link_libraries (bitsandbytes PUBLIC rocblas )
390+ endif ()
391+
353392 target_compile_definitions (bitsandbytes PUBLIC BNB_USE_HIP )
354393 set_source_files_properties (${HIP_FILES} PROPERTIES LANGUAGE HIP )
355394 set_target_properties (bitsandbytes PROPERTIES LINKER_LANGUAGE CXX )
0 commit comments