diff --git a/CMakeLists.txt b/CMakeLists.txt index 995765a..4a7589b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -633,0 +634,5 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) + add_executable(hip_qsa_decode_repeat tests/hip/qsa_decode_repeat.cpp) + target_link_libraries(hip_qsa_decode_repeat PRIVATE strata_kernels strata_hip_runtime) + add_test(NAME hip_qsa_decode_repeat COMMAND hip_qsa_decode_repeat) + set_tests_properties(hip_qsa_decode_repeat PROPERTIES TIMEOUT 120) + list(APPEND _strata_hip_tests hip_qsa_decode_repeat) @@ -1376,0 +1382,6 @@ if(STRATA_NATIVE_EXPERTS) + add_executable(hip_prefill_gather_bytes tests/hip/prefill_gather_bytes.cpp) + target_include_directories(hip_prefill_gather_bytes PRIVATE "${_strata_gg}/include") + target_link_libraries(hip_prefill_gather_bytes PRIVATE strata_mmq strata_hip_runtime) + add_test(NAME hip_prefill_gather_bytes COMMAND hip_prefill_gather_bytes) + set_tests_properties(hip_prefill_gather_bytes PROPERTIES TIMEOUT 120) + list(APPEND _strata_hip_tests hip_prefill_gather_bytes) diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu index 1c20986..19718c4 100644 --- a/src/kernels/cuda/iq_kernels.cu +++ b/src/kernels/cuda/iq_kernels.cu @@ -1711,3 +1711,3 @@ __device__ void dq_q2_0(const void* vx, int64_t ibs, dst_t* yy, int tid) { -#if defined(__HIPCC__) && ((defined(__gfx1012__) && HIP_VERSION_MAJOR < 7) || defined(__gfx1151__)) - // HIP 5.7 on RDNA1 and the HIP compiler on gfx1151 (PR #895, ROCm 7.13) fold the half path's negative scale - // times +0 to +0. Preserve the scale's sign, as the FP32/CPU paths do. +#if defined(__HIPCC__) && ((defined(__gfx1012__) && HIP_VERSION_MAJOR < 7) || defined(__gfx1151__) || defined(__gfx1201__)) + // HIP 5.7 on RDNA1, gfx1151 (PR #895, ROCm 7.13), and gfx1201 (ROCm 7.2.3) fold the half path's negative + // scale times +0 to +0. Preserve the scale's sign, as the FP32/CPU paths do. diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cpp index 01133fa..de3e60f 100644 --- a/src/kernels/native_expert_parity.cpp +++ b/src/kernels/native_expert_parity.cpp @@ -335 +335 @@ int check_blob(const cpu::NativeFmt& f, const std::vector& blob, int se - size_t differ = 0, h_differ = 0; + size_t differ = 0, h_differ = 0, h_zero_sign_differ = 0; @@ -348 +348,4 @@ int check_blob(const cpu::NativeFmt& f, const std::vector& blob, int se - h_differ += got_h[i] != ggml_fp32_to_fp16(got_dq[i]); + const uint16_t want_h = ggml_fp32_to_fp16(got_dq[i]); + h_differ += got_h[i] != want_h; + h_zero_sign_differ += got_h[i] != want_h && + (got_h[i] & 0x7fffu) == 0 && (want_h & 0x7fffu) == 0; @@ -361,0 +365,3 @@ int check_blob(const cpu::NativeFmt& f, const std::vector& blob, int se + if (h_differ) + std::printf(" FP16 mismatches: %zu signed-zero only, %zu other\n", + h_zero_sign_differ, h_differ - h_zero_sign_differ); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index ce8e004..08d2edd 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -10492,0 +10493,16 @@ int main(int argc, char** argv) { + // Same diagnostic as native generate: row 0 includes the state left by real prefill. + // One file per request (overwritten); a serialized caller can read it after DONE and + // feed a fixed continuation to compare distributions without reloading the model. + if (first_window) { + if (const char* fl = std::getenv("STRATA_DUMP_FIRST_LOGITS")) { + std::vector row((size_t) ver.vocab()); + std::FILE* f = ver.copy_logits(0, row.data()) ? std::fopen(fl, "wb") : nullptr; + const bool ok = f && std::fwrite(row.data(), sizeof(float), row.size(), f) == row.size(); + const bool closed = f && std::fclose(f) == 0; + if (!ok || !closed) { + std::fprintf(stderr, "strata serve: STRATA_DUMP_FIRST_LOGITS: cannot write %s\n", fl); + std::printf("ERR cannot write diagnostic logits\n"); + return 1; + } + } + } diff --git a/tests/hip/prefill_gather_bytes.cpp b/tests/hip/prefill_gather_bytes.cpp new file mode 100644 index 0000000..f472adf --- /dev/null +++ b/tests/hip/prefill_gather_bytes.cpp @@ -0,0 +1,98 @@ +// Exact byte/guard test for the grouped gather used by native MMQ prefill. +// Arbitrary byte patterns are deliberate: gathering must preserve every bit, independent of quantized values. +#include "strata/prefill/moe_mmq.hpp" +#include "ggml.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace { +namespace mmq = strata::prefill::mmq; +void check(hipError_t code) { + if (code != hipSuccess) throw std::runtime_error(hipGetErrorString(code)); +} +struct Buffer { + uint8_t* p = nullptr; + explicit Buffer(size_t bytes) { check(hipMalloc(reinterpret_cast(&p), bytes)); } + ~Buffer() { if (p) hipFree(p); } + Buffer(const Buffer&) = delete; + Buffer& operator=(const Buffer&) = delete; +}; +void equal(const Buffer& device, const std::vector& expected, const char* label) { + std::vector actual(expected.size()); + check(hipMemcpy(actual.data(), device.p, actual.size(), hipMemcpyDeviceToHost)); + if (actual != expected) { + const auto at = std::mismatch(actual.begin(), actual.end(), expected.begin()).first - actual.begin(); + throw std::runtime_error(std::string(label) + " byte mismatch at " + std::to_string(at)); + } +} +void run(ggml_type gu_type, ggml_type d_type) { + constexpr int slots = mmq::kGatherGroupMax; + constexpr size_t guard = 64; + // Production native expert dimensions; gaps also check that offsets and strides are respected. + const size_t half = ggml_row_size(gu_type, 2560) * 640; + const size_t down = ggml_row_size(d_type, 640) * 2560; + const size_t up_off = half + guard, down_off = up_off + half + guard; + const size_t blob_stride = down_off + down + guard; + const size_t gu_stride = 2 * half + guard, d_stride = down + guard; + std::vector source(slots * blob_stride); + for (size_t i = 0; i < source.size(); ++i) + source[i] = static_cast((i * 37 + i / 257 + i / blob_stride * 19) & 255); + Buffer blobs(source.size()), gu(guard + slots * gu_stride + guard), d(guard + slots * d_stride + guard); + check(hipMemcpy(blobs.p, source.data(), source.size(), hipMemcpyHostToDevice)); + for (auto range : {std::pair{0, 16}, {0, 1}, {0, 15}, {3, 11}, {15, 16}}) { + std::vector expected_gu(guard + slots * gu_stride + guard, 0xa5); + std::vector expected_d(guard + slots * d_stride + guard, 0xa5); + check(hipMemset(gu.p, 0xa5, expected_gu.size())); + check(hipMemset(d.p, 0xa5, expected_d.size())); + mmq::GatherGroup group; + group.first = range.first; group.n = range.second; + for (int q = group.first; q < group.n; ++q) { + // Non-contiguous expert order, including the last source allocation. + const size_t offset = static_cast((q * 7 + 3) % slots) * blob_stride; + group.blob[q] = blobs.p + offset; + std::copy_n(source.data() + offset, half, expected_gu.data() + guard + q * gu_stride); + std::copy_n(source.data() + offset + up_off, half, expected_gu.data() + guard + q * gu_stride + half); + std::copy_n(source.data() + offset + down_off, down, expected_d.data() + guard + q * d_stride); + } + if (!mmq::gather_native_group(group, up_off, half, down_off, down, + gu.p + guard, gu_stride, d.p + guard, d_stride, nullptr)) + throw std::runtime_error("aligned group unexpectedly rejected"); + check(hipDeviceSynchronize()); + equal(gu, expected_gu, "group gate/up including guards"); + equal(d, expected_d, "group down including guards"); + // The fallback contract is false with no writes for an unaligned source. + ++group.blob[group.first]; + if (mmq::gather_native_group(group, up_off, half, down_off, down, + gu.p + guard, gu_stride, d.p + guard, d_stride, nullptr)) + throw std::runtime_error("unaligned group unexpectedly accepted"); + check(hipDeviceSynchronize()); + equal(gu, expected_gu, "rejected group gate/up"); + equal(d, expected_d, "rejected group down"); + } + std::cout << "PASS types=" << static_cast(gu_type) << '/' << static_cast(d_type) + << " five full/partial groups; byte equality, untouched guards, unaligned rejection\n"; +} +} // namespace + +int main() { + try { + // Union of the nine distinct format pairs in the pinned IQ3_S and IQ2_XS packs. + for (auto pair : {std::pair{GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ4_NL}, + {GGML_TYPE_IQ3_XXS, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_NL}, + {GGML_TYPE_IQ3_S, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ2_S, GGML_TYPE_IQ4_NL}, + {GGML_TYPE_IQ2_S, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ4_XS, GGML_TYPE_IQ4_NL}, + {GGML_TYPE_IQ2_XXS, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ1_M, GGML_TYPE_Q2_0}}) + run(pair.first, pair.second); + } catch (const std::exception& error) { + std::cerr << "FAIL: " << error.what() << '\n'; + return 1; + } + return 0; +} diff --git a/tests/hip/qsa_decode_repeat.cpp b/tests/hip/qsa_decode_repeat.cpp new file mode 100644 index 0000000..f1440e8 --- /dev/null +++ b/tests/hip/qsa_decode_repeat.cpp @@ -0,0 +1,74 @@ +// A fresh short prompt must not depend on bytes left in attention scratch by an earlier request. +#include "strata/kernels/qsa.hpp" +#include "strata/kernels/qsa_decode_attn.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace k = strata::kernels; +namespace { +void check(hipError_t code) { if (code != hipSuccess) throw std::runtime_error(hipGetErrorString(code)); } +struct Buffer { + void* p = nullptr; + explicit Buffer(size_t bytes) { check(hipMalloc(&p, bytes)); } + ~Buffer() { if (p) hipFree(p); } + Buffer(const Buffer&) = delete; + Buffer& operator=(const Buffer&) = delete; + template T* as() const { return static_cast(p); } +}; +template void upload(Buffer& b, const std::vector& v) { + check(hipMemcpy(b.p, v.data(), v.size() * sizeof(T), hipMemcpyHostToDevice)); +} +void run(int tokens) { + const auto s = k::qsa_real_shapes(); + const int64_t cap = k::qsa_selection_width(32768, s), heads = s.n_head, dim = s.head_dim; + const int64_t pages = (tokens + s.page_size - 1) / s.page_size, rows = pages * s.n_head_kv * s.page_size; + std::vector kc(rows * dim), vc(rows * dim); + std::vector scales(rows * (dim / 64), 0x2000); // exact fp16 1/128 + std::vector table(pages), ids(tokens * cap, -1), steps(tokens * k::kStepCount, 0); + std::vector queries(tokens * heads * dim); + for (int64_t i = 0; i < pages; ++i) table[i] = static_cast(i); + for (size_t i = 0; i < kc.size(); ++i) { kc[i] = (i * 37 + i / 17) % 255 - 127; vc[i] = (i * 23 + i / 31) % 255 - 127; } + for (size_t i = 0; i < queries.size(); ++i) queries[i] = std::sin(static_cast(i % 1007) * 0.03f); + for (int t = 0; t < tokens; ++t) { + steps[t * k::kStepCount + k::kStepWidth] = t + 1; + for (int c = 0; c <= t; ++c) ids[t * cap + c] = c; + } + Buffer dk(kc.size()), dv(vc.size()), ds(scales.size() * 2), dt(table.size() * 4), di(ids.size() * 4), + dp(steps.size() * 4), dq(queries.size() * 4), out(queries.size() * 4); + upload(dk, kc); upload(dv, vc); upload(ds, scales); upload(dt, table); upload(di, ids); upload(dp, steps); upload(dq, queries); + k::QsaAttnPools pools{}; + pools.k_q = dk.as(); pools.v_q = dv.as(); + pools.k_scale = ds.as(); pools.v_scale = ds.as(); pools.page_table = dt.as(); + const size_t scratch_bytes = 32 * k::qsa_decode_attn_scratch_floats(cap, s) * sizeof(float); + Buffer scratch(scratch_bytes); + std::vector reference, got(queries.size()); + for (int rep = 0; rep < 12; ++rep) { + // Includes NaN, zero and nonzero patterns, and full/partial 32-query batches. + check(hipMemset(scratch.p, rep % 3 == 0 ? 0xff : rep % 3 == 1 ? 0 : 0x35, scratch_bytes)); + check(hipMemset(out.p, 0xff, queries.size() * 4)); + for (int q = 0; q < tokens; q += 32) + k::qsa_decode_attn_batch(dq.as() + q * heads * dim, pools, di.as() + q * cap, + dp.as() + q * k::kStepCount, cap, s, scratch.as(), + out.as() + q * heads * dim, std::min(32, tokens - q), nullptr); + check(hipDeviceSynchronize()); + check(hipMemcpy(got.data(), out.p, got.size() * 4, hipMemcpyDeviceToHost)); + std::vector values(got.size()); + check(hipMemcpy(values.data(), out.p, values.size() * 4, hipMemcpyDeviceToHost)); + if (!std::all_of(values.begin(), values.end(), [](float v) { return std::isfinite(v); })) + throw std::runtime_error("nonfinite or unwritten attention output"); + if (rep == 0) reference = got; + else if (got != reference) throw std::runtime_error("attention output changed with scratch contents/repeat"); + } + std::cout << "PASS tokens=" << tokens << " fixed-cap=" << cap << " 12 repeats with poisoned scratch\n"; +} +} +int main() { + try { for (int tokens : {31, 32, 127, 255, 256, 511}) run(tokens); } + catch (const std::exception& e) { std::cerr << "FAIL: " << e.what() << '\n'; return 1; } + return 0; +}