diff --git a/CMakeLists.txt b/CMakeLists.txt index b60719e0..89d127f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -783,6 +783,10 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(strata-dsv41 PRIVATE strata_dsv41_kernels ${_strata_gpu_runtime_target} ${_strata_gpu_blas_target}) find_package(Threads REQUIRED) target_link_libraries(strata-dsv41 PRIVATE Threads::Threads) + if(WIN32 AND STRATA_ENABLE_HIP) # cudaMemGetInfo is the DXGI-budgeted hip_compat::mem_get_info (src/core/device.cu): on a Strix Halo the + # carve-out is not all the GPU may use, and the engine sizes its expert tiers by what the budget says is free + target_link_libraries(strata-dsv41 PRIVATE strata_core) + endif() if(STRATA_NATIVE_EXPERTS) # Strata's CPU trellis kernels (tq_cpu.hpp) for the trellis experts on the CPU lane target_link_libraries(strata-dsv41 PRIVATE strata_kernels_cpu) target_compile_definitions(strata-dsv41 PRIVATE STRATA_DSV41_TQ_CPU=1) diff --git a/src/dsv41/ds4_iq.cu b/src/dsv41/ds4_iq.cu index ed251059..844e32ac 100644 --- a/src/dsv41/ds4_iq.cu +++ b/src/dsv41/ds4_iq.cu @@ -96,6 +96,10 @@ __device__ __forceinline__ int sgn4(uint32_t g, uint32_t b) { const uint32_t lo = (((b & 15u) * 0x10204080u) & 0x80808080u) >> 7; return (int) ((g ^ (lo * 255u)) + lo); } +// per-byte wrapping add (CUDA's __vadd4, which HIP has not): no carry crosses a byte +__device__ __forceinline__ uint32_t add4(uint32_t a, uint32_t b) { + return ((a & 0x7f7f7f7fu) + (b & 0x7f7f7f7fu)) ^ ((a ^ b) & 0x80808080u); +} __device__ __forceinline__ int2 tab16(uint32_t q4, const uint32_t* t) { // vecdotq's get_int_from_table_16 uint32_t tmp[2]; const uint32_t sel = 0x32103210u | ((q4 & 0x88888888u) >> 1); @@ -212,8 +216,8 @@ __device__ __forceinline__ void decode(const uint8_t* __restrict__ row, int kb, const uint32_t h = (uint32_t) qh[l >> 1] >> (4 * (l & 1)); const uint32_t e = tab[(uint32_t) qs[l] | ((h & 7u) << 8)]; const uint32_t dl = (h & 8u) ? 0xf7f7f7f7u : 0xf9f9f9f9u; // -8 -+ 1 in each byte - s.q[2 * l] = (int) __vadd4((e & 0x0f0f0f0fu) << 3, dl); - s.q[2 * l + 1] = (int) __vadd4(((e >> 4) & 0x0f0f0f0fu) << 3, dl); + s.q[2 * l] = (int) add4((e & 0x0f0f0f0fu) << 3, dl); + s.q[2 * l + 1] = (int) add4(((e >> 4) & 0x0f0f0f0fu) << 3, dl); } s.s0 = 2 * (int) (sc & 7u) + 1; s.s1 = 2 * (int) ((sc >> 3) & 7u) + 1; diff --git a/tools/dsv41/build_hip_linux.sh b/tools/dsv41/build_hip_linux.sh new file mode 100644 index 00000000..cba51e07 --- /dev/null +++ b/tools/dsv41/build_hip_linux.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# strata-dsv41 + the kernel parity tests for AMD (HIP) on Linux. Default arch gfx1151 (Strix Halo, RDNA3.5); gfx1201 (R9700) works the same. +# Built and ready, NOT run on a Linux Strix box (there is none); the Windows build of the same sources is the tested one. +# +# ROCM_ROOT=/opt/rocm (or the TheRock wheel root, e.g. .../_rocm_sdk_devel) tools/dsv41/build_hip_linux.sh [targets...] +# env: STRATA_HIP_ARCHS (default gfx1151), BUILD_DIR (default build-dsv41-hip), JOBS (default nproc), STRATA_GGML_DIR (llama.cpp checkout) +# Needs: cmake, ninja, a ROCm with clang++ (ROCm >= 6.4 knows gfx1151; the wheel's device package is _rocm_sdk_libraries_gfx115X_all +# or gfx1151), and glibc matching the box. The engine has no RPATH: run with the ROCm lib dir on LD_LIBRARY_PATH. +set -e +SRC="$(cd "$(dirname "$0")/../.." && pwd)" +ARCH="${STRATA_HIP_ARCHS:-gfx1151}" +R="${ROCM_ROOT:-/opt/rocm}" +B="${BUILD_DIR:-$SRC/build-dsv41-hip}" +J="${JOBS:-$(nproc)}" +CLANG="$R/llvm/bin/clang++"; [ -x "$CLANG" ] || CLANG="$R/lib/llvm/bin/clang++" +GGML=""; [ -n "$STRATA_GGML_DIR" ] && GGML="-DSTRATA_GGML_DIR=$STRATA_GGML_DIR" +BC="$R/amdgcn/bitcode"; [ -d "$BC" ] || BC="$R/lib/llvm/amdgcn/bitcode"; [ -d "$BC" ] || BC="$R/llvm/amdgcn/bitcode" +export PATH="$R/bin:$PATH" +if [ ! -f "$B/build.ninja" ]; then + cmake -S "$SRC" -B "$B" -G Ninja -DCMAKE_BUILD_TYPE=Release \ + -DSTRATA_ENABLE_HIP=ON -DCMAKE_HIP_ARCHITECTURES="$ARCH" -DSTRATA_BUILD_TESTS=OFF -DSTRATA_BUILD_DSV41=ON \ + -DSTRATA_NATIVE_EXPERTS=ON $GGML \ + -DCMAKE_HIP_COMPILER="$CLANG" -DCMAKE_HIP_COMPILER_ROCM_ROOT="$R" \ + "-DCMAKE_HIP_FLAGS=--rocm-path=$R --rocm-device-lib-path=$BC" \ + "-DCMAKE_PREFIX_PATH=$R" +fi +cmake --build "$B" -j "$J" --target ${@:-strata-dsv41 ds4_iq_test} -- -k 0 +echo "built in $B" diff --git a/tools/dsv41/win/build_hip_win.bat b/tools/dsv41/win/build_hip_win.bat new file mode 100644 index 00000000..0c76d743 --- /dev/null +++ b/tools/dsv41/win/build_hip_win.bat @@ -0,0 +1,45 @@ +@echo off +rem strata-dsv41 + the kernel parity tests for AMD (HIP) on Windows, ROCm 10.2 (TheRock wheels), default gfx1151 (Strix Halo). +rem +rem tools\dsv41\win\build_hip_win.bat [targets...] (default: strata-dsv41 ds4_iq_test) +rem +rem Same toolchain as tools\hip\build_windows.bat: the ROCm venv, MSVC's headers and linker from vcvars, ROCm's clang for host and +rem device code. Settings (all optional): +rem STRATA_HIP_ARCHS gfx1151 (also gfx1100;gfx1201 ...) ROCM_VENV \.rocm-win +rem BUILD_DIR \build-dsv41-hip STRATA_GGML_DIR a llama.cpp checkout at the pinned commit +rem JOBS parallel compile jobs (default 6) +setlocal EnableDelayedExpansion +for %%I in ("%~dp0..\..\..") do set "SRC=%%~fI" +if not defined STRATA_HIP_ARCHS set "STRATA_HIP_ARCHS=gfx1151" +if not defined ROCM_VENV set "ROCM_VENV=%SRC%\.rocm-win" +if not defined BUILD_DIR set "BUILD_DIR=%SRC%\build-dsv41-hip" +if not defined JOBS set "JOBS=6" +set "TARGETS=%*" +if "%TARGETS%"=="" set "TARGETS=strata-dsv41 ds4_iq_test" +for /f "delims=" %%R in ('"%ROCM_VENV%\Scripts\rocm-sdk.exe" path --root') do set "ROCM=%%R" +if not exist "%ROCM%\lib\llvm\bin\clang++.exe" (echo ROCm has no compiler in "%ROCM%" & exit /b 1) +set "ROCM_F=%ROCM:\=/%" +set "BITCODE=%ROCM_F%/lib/llvm/amdgcn/bitcode" +set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" +set "VS=" +for /f "delims=" %%V in ('"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath') do set "VS=%%V" +if not defined VS (echo Visual Studio Build Tools with the C++ workload are needed & exit /b 1) +call "%VS%\VC\Auxiliary\Build\vcvars64.bat" >nul || exit /b 1 +set "HIP_PLATFORM=amd" +set "HIP_PATH=%ROCM%" +set "ROCM_PATH=%ROCM%" +set "PATH=%ROCM%\bin;%ROCM%\lib\llvm\bin;%PATH%" +set "GGML=" +if defined STRATA_GGML_DIR set "GGML=-DSTRATA_GGML_DIR=%STRATA_GGML_DIR:\=/%" +if not exist "%BUILD_DIR%\build.ninja" ( + cmake -G Ninja -S "%SRC%" -B "%BUILD_DIR%" -DCMAKE_BUILD_TYPE=Release ^ + -DSTRATA_ENABLE_HIP=ON -DSTRATA_ENABLE_CUDA=OFF -DSTRATA_BUILD_TESTS=OFF -DSTRATA_BUILD_DSV41=ON ^ + -DSTRATA_NATIVE_EXPERTS=ON -DSTRATA_PORTABLE=ON "-DCMAKE_HIP_ARCHITECTURES=%STRATA_HIP_ARCHS%" ^ + "-DCMAKE_C_COMPILER=%ROCM_F%/lib/llvm/bin/clang.exe" "-DCMAKE_CXX_COMPILER=%ROCM_F%/lib/llvm/bin/clang++.exe" ^ + "-DCMAKE_HIP_COMPILER=%ROCM_F%/lib/llvm/bin/clang++.exe" "-DCMAKE_HIP_COMPILER_ROCM_ROOT=%ROCM_F%" ^ + "-DCMAKE_PREFIX_PATH=%ROCM_F%" "-DCMAKE_HIP_FLAGS=--rocm-path=%ROCM_F% --rocm-device-lib-path=%BITCODE%" ^ + %GGML% || exit /b 1 +) +cmake --build "%BUILD_DIR%" -j %JOBS% --target %TARGETS% -- -k 0 +echo BUILD-EXIT %errorlevel% +endlocal