From e9a5f8d2b877e76b425f339e98b1907a764284fa Mon Sep 17 00:00:00 2001 From: David Date: Thu, 1 Oct 2026 16:47:17 -0400 Subject: [PATCH] hip: preserve Q2_0 signed zero on gfx1012 with legacy HIP --- CMakeLists.txt | 4 +++ src/kernels/cuda/iq_kernels.cu | 10 +++++++ tests/hip/q2_zero.cpp | 50 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/hip/q2_zero.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a8b0716b..5215e180 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -479,6 +479,10 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(hip_prefill_gemm PRIVATE strata_prefill strata_hip_runtime) add_test(NAME hip_prefill_gemm COMMAND hip_prefill_gemm) list(APPEND _strata_hip_tests hip_prefill_gemm) + add_executable(hip_q2_zero tests/hip/q2_zero.cpp) + target_link_libraries(hip_q2_zero PRIVATE strata_kernels strata_hip_runtime) + add_test(NAME hip_q2_zero COMMAND hip_q2_zero) + list(APPEND _strata_hip_tests hip_q2_zero) add_executable(hip_expert_cache_staging tests/hip/expert_cache_staging.cpp) target_link_libraries(hip_expert_cache_staging PRIVATE strata_engine) add_test(NAME hip_expert_cache_staging COMMAND hip_expert_cache_staging) diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu index 2bb7f4d8..bbcf1d06 100644 --- a/src/kernels/cuda/iq_kernels.cu +++ b/src/kernels/cuda/iq_kernels.cu @@ -1226,6 +1226,16 @@ __device__ void dq_q2_0(const void* vx, int64_t ibs, dst_t* yy, int tid) { for (int j = 0; j < 8; ++j) { const int i = part * 8 + j; const int code = (x[b].qs[i / 4] >> ((i % 4) * 2)) & 3; +#if defined(__HIPCC__) && defined(__gfx1012__) && HIP_VERSION_MAJOR < 7 + // HIP 5.7 on RDNA1 folds the half path's negative scale times +0 + // to +0. Preserve the scale's sign, as the FP32/CPU paths do. + if constexpr (std::is_same_v) { + if (code == 1) { + yy[b * 64 + i] = __ushort_as_half(__half_as_ushort(x[b].d) & 0x8000u); + continue; + } + } +#endif yy[b * 64 + i] = cvt(d * (float) (code - 1)); } } diff --git a/tests/hip/q2_zero.cpp b/tests/hip/q2_zero.cpp new file mode 100644 index 00000000..e897e522 --- /dev/null +++ b/tests/hip/q2_zero.cpp @@ -0,0 +1,50 @@ +// Q2_0 dequant must preserve signed zero when a negative scale meets code 1. +// This caught HIP 5.7/gfx1012 folding the half product to positive zero. +#include "strata/kernels/iq_kernels.hpp" +#include +#include +#include +#include +#include +#include +#include + +#define CHECK(call) do { const auto e = (call); if (e != hipSuccess) { \ + std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(e)); std::exit(2); } } while (0) + +int main() { + // GGUF Q2_0: one half scale followed by sixteen packed 2-bit bytes. + struct Block { uint16_t d; uint8_t qs[16]; }; + static_assert(sizeof(Block) == 18); + constexpr int blocks = 16, values = blocks * 64; + std::vector input(blocks); + std::vector expected(values), got(values); + for (int b = 0; b < blocks; ++b) { + const float scale = (b & 1) ? -0.5f : 0.5f; + const __half half_scale = __float2half(scale); + std::memcpy(&input[b].d, &half_scale, 2); + for (int i = 0; i < 16; ++i) input[b].qs[i] = 0xe4; // codes 0,1,2,3 + for (int i = 0; i < 64; ++i) { + const __half value = __float2half(scale * float((i % 4) - 1)); + std::memcpy(&expected[b * 64 + i], &value, 2); + } + } + void* weights = nullptr; + uint16_t* output = nullptr; + CHECK(hipMalloc(&weights, input.size() * sizeof(Block))); + CHECK(hipMalloc(reinterpret_cast(&output), values * sizeof(uint16_t))); + CHECK(hipMemcpy(weights, input.data(), input.size() * sizeof(Block), hipMemcpyHostToDevice)); + strata::kernels::iq_dequant_f16(42, weights, values, output, nullptr); + CHECK(hipDeviceSynchronize()); + CHECK(hipMemcpy(got.data(), output, values * sizeof(uint16_t), hipMemcpyDeviceToHost)); + int failures = 0; + for (int i = 0; i < values; ++i) { + if (got[i] == expected[i]) continue; + if (failures++ < 4) + std::printf("index=%d got=0x%04x expected=0x%04x\n", i, unsigned(got[i]), unsigned(expected[i])); + } + CHECK(hipFree(weights)); + CHECK(hipFree(output)); + std::printf("hip_q2_zero: %d values, %d failures\n", values, failures); + return failures ? 1 : 0; +}