okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
7.7 KiB137 linesraw
1# Opt-in HIP configuration. Strata's CUDA-shaped kernels target wave32 RDNA2 / RDNA3 / RDNA4 (64 KiB LDS per
2# workgroup, a signed dot4 instruction). CMake/compiler discovery stays machine-independent; pass CMAKE_HIP_COMPILER when it
3# is not on PATH.
4if(NOT DEFINED CMAKE_HIP_ARCHITECTURES OR CMAKE_HIP_ARCHITECTURES STREQUAL "")
5  set(CMAKE_HIP_ARCHITECTURES gfx1100 CACHE STRING "Strata HIP target architecture(s), e.g. gfx1100 or gfx1100;gfx1201")
6endif()
7# Validated on real cards: gfx1100 (RX 7900 XT / XTX) and gfx1201 (RX 9070 / 9070 XT, Radeon AI PRO R9700) by the
8# maintainers; gfx1101 (RX 7800 XT, #254) and gfx1200 (RX 9060 XT, #256) by their owners. gfx1102 (RX 7600) has the
9# same LDS limit and dot4 instruction and passed ctest (#192), but no model run has been reported yet. RDNA2 gfx1030 (RX 6800 / 6900) has the
10# same LDS limit and wave32 but an older dot4 instruction (v_dot4_i32_i8, hip_compat/intrinsics.hpp); a community
11# report ran it (#311), the maintainers have not. gfx1151 (Ryzen AI Max / Strix Halo, Radeon 8060S, RDNA3.5) is the
12# same wave32 / 64 KiB LDS / sudot4 family as gfx1100, on a unified-memory APU; experimental.
13set(_strata_hip_validated gfx1100 gfx1201)
14set(_strata_hip_community gfx1101 gfx1200)
15set(_strata_hip_unvalidated gfx1010 gfx1011 gfx1012 gfx1102 gfx1103 gfx1030 gfx1031 gfx1032 gfx1034 gfx1150 gfx1151 gfx1152)   # gfx1103: Radeon 780M iGPU, opt-in, WMMA kernels like gfx1100; gfx1152: Krackan, Radeon 860M / 840M (#1625), builds with the portable kernels, no card report yet
16# CMake hands HIP a ';' list, but a -DCMAKE_HIP_ARCHITECTURES typed by hand (or ROCm's own Windows tooling) may use
17# spaces, which foreach(IN LISTS) would otherwise treat as one element.
18string(REPLACE " " ";" _strata_hip_norm "${CMAKE_HIP_ARCHITECTURES}")
19set(STRATA_HIP_ARCH_LIST "")
20foreach(_arch IN LISTS _strata_hip_norm)
21  if(_arch STREQUAL "")
22    continue()
23  endif()
24  string(REGEX REPLACE ":.*$" "" _base "${_arch}")      # gfx1100:xnack- -> gfx1100
25  if(_base IN_LIST _strata_hip_validated)
26  elseif(_base IN_LIST _strata_hip_community)
27    message(STATUS "Strata HIP: ${_base} was validated by community reports (docs/AMD_HIP.md)")
28  elseif(_base IN_LIST _strata_hip_unvalidated)
29    message(WARNING "Strata HIP: ${_base} builds, but it is not validated on a real card yet; please report results")
30  else()
31    message(FATAL_ERROR
32      "Strata HIP supports wave32 gfx1100, gfx1101, gfx1200 and gfx1201 (unvalidated: ${_strata_hip_unvalidated}); "
33      "CMAKE_HIP_ARCHITECTURES is '${CMAKE_HIP_ARCHITECTURES}'")
34  endif()
35  list(APPEND STRATA_HIP_ARCH_LIST "${_base}")
36endforeach()
37list(REMOVE_DUPLICATES STRATA_HIP_ARCH_LIST)
38if(NOT STRATA_HIP_ARCH_LIST)
39  message(FATAL_ERROR "Strata HIP: CMAKE_HIP_ARCHITECTURES is empty")
40endif()
41# The compiled architectures reach the runtime device check (src/core/device.cu) as "gfx1100,gfx1201": a binary
42# carried to a card it has no code for stops at startup with a clear message instead of "invalid device function".
43string(REPLACE ";" "," STRATA_HIP_ARCHS "${STRATA_HIP_ARCH_LIST}")
44
45enable_language(HIP)
46# CMake caches CMAKE_HIP_FLAGS_<CONFIG> while it enables HIP, before it tests the compiler.  A configure that stopped
47# there because HIP could not compile at all (Visual Studio 2026's <cmath> with the HIP SDK 7.2, a wrong ROCm path)
48# cached them empty, and every later configure of that build folder keeps them: setup's Release build then compiled
49# all kernels at -O0 (an RX 6800 decoded at 0.24-0.27 tok/s instead of 28).  Empty ones get what CMake gives a fresh
50# build folder.
51foreach(_cfg IN ITEMS DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
52  string(STRIP "${CMAKE_HIP_FLAGS_${_cfg}_INIT}" _strata_init)
53  if("${CMAKE_HIP_FLAGS_${_cfg}}" STREQUAL "" AND NOT "${_strata_init}" STREQUAL "")
54    set(CMAKE_HIP_FLAGS_${_cfg} "${_strata_init}" CACHE STRING
55        "Flags used by the HIP compiler during ${_cfg} builds." FORCE)
56  endif()
57endforeach()
58find_package(hip CONFIG REQUIRED)
59find_package(hipblas CONFIG REQUIRED)
60# Older distro hipBLAS has no workspace API. The compatibility shim uses
61# rocBLAS directly for that version; newer hipBLAS keeps its existing path.
62set(STRATA_HIP_BLAS_TARGETS roc::hipblas)
63if(hipblas_VERSION VERSION_LESS "1.0")
64  find_package(rocblas CONFIG REQUIRED)
65  list(APPEND STRATA_HIP_BLAS_TARGETS roc::rocblas)
66else()
67  # the prompt path's rocBLAS solution table (STRATA_ROCBLAS_TUNING, gfx103x) calls rocBLAS by name
68  find_package(rocblas CONFIG QUIET)
69endif()
70find_package(hipblaslt CONFIG QUIET)
71
72if(NOT TARGET hip::host)
73  message(FATAL_ERROR "The ROCm hip CMake package did not provide hip::host")
74endif()
75if(NOT TARGET roc::hipblas)
76  message(FATAL_ERROR "The ROCm hipblas CMake package did not provide roc::hipblas")
77endif()
78if(TARGET roc::hipblaslt)
79  set(STRATA_HIPBLASLT_AVAILABLE ON)
80else()
81  set(STRATA_HIPBLASLT_AVAILABLE OFF)
82  message(STATUS "Strata: hipBLASLt not found; solution-table dispatch is unavailable")
83endif()
84if(TARGET roc::rocblas)
85  set(STRATA_ROCBLAS_AVAILABLE ON)
86else()
87  set(STRATA_ROCBLAS_AVAILABLE OFF)
88  message(STATUS "Strata: rocBLAS CMake package not found; the rocBLAS solution table (gfx103x) is unavailable")
89endif()
90
91# HIP's link step produces a PIE; make Strata and ggml objects PIC for the ROCm linker.
92set(CMAKE_POSITION_INDEPENDENT_CODE ON)
93
94set(STRATA_HIP_COMPAT_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include/strata/hip_compat")
95add_library(strata_hip_runtime INTERFACE)
96target_include_directories(strata_hip_runtime BEFORE INTERFACE
97  "${STRATA_HIP_COMPAT_INCLUDE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include")
98target_compile_definitions(strata_hip_runtime INTERFACE STRATA_USE_HIP=1 "STRATA_HIP_ARCHS=\"${STRATA_HIP_ARCHS}\"")
99option(STRATA_GFX1012_PORTABLE_DOT "Use the portable signed-byte dot control on gfx1012" OFF)
100if(STRATA_GFX1012_PORTABLE_DOT)
101  target_compile_definitions(strata_hip_runtime INTERFACE STRATA_GFX1012_PORTABLE_DOT=1)
102endif()
103# #313: the RDNA3 WMMA GEMM's intrinsics only compile for gfx11 targets and the host pass does not define
104# __gfx11xx__, so the build supplies the macro for gfx11 configs (the kernels also gate on the device at runtime)
105if(CMAKE_HIP_ARCHITECTURES MATCHES "gfx11")
106  target_compile_definitions(strata_hip_runtime INTERFACE STRATA_WMMA_GFX11=1)
107endif()
108target_link_libraries(strata_hip_runtime INTERFACE hip::host)
109# The shim renames the CUDA runtime to HIP, force-included into every host and device source. On Windows the host
110# compiler is ROCm's clang++ too (tools/hip/build_windows.bat: CMake refuses to mix cl.exe with Clang HIP), which takes
111# -include like it does on Linux; an MSVC-style front end (cl / clang-cl) takes /FI instead. Forward slashes, so /FI
112# does not read the path's backslashes as escapes.
113file(TO_CMAKE_PATH "${STRATA_HIP_COMPAT_INCLUDE_DIR}/cuda_runtime.h" _strata_hip_force)
114if(MSVC)
115  target_compile_options(strata_hip_runtime INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:/FI${_strata_hip_force}>")
116else()
117  target_compile_options(strata_hip_runtime INTERFACE
118    "$<$<COMPILE_LANGUAGE:CXX>:-include>" "$<$<COMPILE_LANGUAGE:CXX>:${_strata_hip_force}>")
119endif()
120target_compile_options(strata_hip_runtime INTERFACE
121  "$<$<COMPILE_LANGUAGE:HIP>:-include>" "$<$<COMPILE_LANGUAGE:HIP>:${_strata_hip_force}>")
122
123# CMake does not infer HIP from Strata's existing CUDA-shaped .cu suffixes.
124file(GLOB_RECURSE _strata_hip_sources CONFIGURE_DEPENDS
125  "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu"
126  "${CMAKE_CURRENT_SOURCE_DIR}/bench/*.cu"
127  "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cu")
128if(_strata_hip_sources)
129  set_source_files_properties(${_strata_hip_sources} PROPERTIES LANGUAGE HIP)
130endif()
131foreach(_source IN ITEMS tests/hip/intrinsics.cpp tests/hip/native_qsa_score.cpp)
132  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_source}")
133    set_source_files_properties("${_source}" PROPERTIES LANGUAGE HIP)
134  endif()
135endforeach()
136
137message(STATUS "Strata: HIP enabled, arch ${STRATA_HIP_ARCHS}")