hip: preserve Q2_0 signed zero on gfx1012 with legacy HIP

This commit is contained in:
David authored and Niko1221 committed 2026-10-03 23:33:46 +02:00
1 parent e3e30eb131
commit e9a5f8d2b8
3 files changed
+64

No files matched your search

+4
View File
@@ -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)
+10
View File
@@ -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<dst_t, __half>) {
if (code == 1) {
yy[b * 64 + i] = __ushort_as_half(__half_as_ushort(x[b].d) & 0x8000u);
continue;
}
}
#endif
yy[b * 64 + i] = cvt<dst_t>(d * (float) (code - 1));
}
}
+50
View File
@@ -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 <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#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<Block> input(blocks);
std::vector<uint16_t> 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<void**>(&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;
}