okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1diff --git a/CMakeLists.txt b/CMakeLists.txt 2index995765a ..4a7589b 100644 3--- a/CMakeLists.txt 4+++ b/CMakeLists.txt 5@@ -633,0 +634,5 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) 6+ add_executable(hip_qsa_decode_repeat tests/hip/qsa_decode_repeat.cpp) 7+ target_link_libraries(hip_qsa_decode_repeat PRIVATE strata_kernels strata_hip_runtime) 8+ add_test(NAME hip_qsa_decode_repeat COMMAND hip_qsa_decode_repeat) 9+ set_tests_properties(hip_qsa_decode_repeat PROPERTIES TIMEOUT 120) 10+ list(APPEND _strata_hip_tests hip_qsa_decode_repeat) 11@@ -1376,0 +1382,6 @@ if(STRATA_NATIVE_EXPERTS) 12+ add_executable(hip_prefill_gather_bytes tests/hip/prefill_gather_bytes.cpp) 13+ target_include_directories(hip_prefill_gather_bytes PRIVATE "${_strata_gg}/include") 14+ target_link_libraries(hip_prefill_gather_bytes PRIVATE strata_mmq strata_hip_runtime) 15+ add_test(NAME hip_prefill_gather_bytes COMMAND hip_prefill_gather_bytes) 16+ set_tests_properties(hip_prefill_gather_bytes PROPERTIES TIMEOUT 120) 17+ list(APPEND _strata_hip_tests hip_prefill_gather_bytes) 18diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu 19index1c20986 ..19718c4 100644 20--- a/src/kernels/cuda/iq_kernels.cu 21+++ b/src/kernels/cuda/iq_kernels.cu 22@@ -1711,3 +1711,3 @@ __device__ void dq_q2_0(const void* vx, int64_t ibs, dst_t* yy, int tid) { 23-#if defined(__HIPCC__) && ((defined(__gfx1012__) && HIP_VERSION_MAJOR < 7) || defined(__gfx1151__)) 24- // HIP 5.7 on RDNA1 and the HIP compiler on gfx1151 (PR #895, ROCm 7.13) fold the half path's negative scale 25- // times +0 to +0. Preserve the scale's sign, as the FP32/CPU paths do. 26+#if defined(__HIPCC__) && ((defined(__gfx1012__) && HIP_VERSION_MAJOR < 7) || defined(__gfx1151__) || defined(__gfx1201__)) 27+ // HIP 5.7 on RDNA1, gfx1151 (PR #895, ROCm 7.13), and gfx1201 (ROCm 7.2.3) fold the half path's negative 28+ // scale times +0 to +0. Preserve the scale's sign, as the FP32/CPU paths do. 29diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cpp 30index01133fa ..de3e60f 100644 31--- a/src/kernels/native_expert_parity.cpp 32+++ b/src/kernels/native_expert_parity.cpp 33@@ -335 +335 @@ int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int se 34- size_t differ = 0, h_differ = 0; 35+ size_t differ = 0, h_differ = 0, h_zero_sign_differ = 0; 36@@ -348 +348,4 @@ int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int se 37- h_differ += got_h[i] != ggml_fp32_to_fp16(got_dq[i]); 38+ const uint16_t want_h = ggml_fp32_to_fp16(got_dq[i]); 39+ h_differ += got_h[i] != want_h; 40+ h_zero_sign_differ += got_h[i] != want_h && 41+ (got_h[i] & 0x7fffu) == 0 && (want_h & 0x7fffu) == 0; 42@@ -361,0 +365,3 @@ int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int se 43+ if (h_differ) 44+ std::printf(" FP16 mismatches: %zu signed-zero only, %zu other\n", 45+ h_zero_sign_differ, h_differ - h_zero_sign_differ); 46diff --git a/src/program/generate.cpp b/src/program/generate.cpp 47indexce8e004 ..08d2edd 100644 48--- a/src/program/generate.cpp 49+++ b/src/program/generate.cpp 50@@ -10492,0 +10493,16 @@ int main(int argc, char** argv) { 51+ // Same diagnostic as native generate: row 0 includes the state left by real prefill. 52+ // One file per request (overwritten); a serialized caller can read it after DONE and 53+ // feed a fixed continuation to compare distributions without reloading the model. 54+ if (first_window) { 55+ if (const char* fl = std::getenv("STRATA_DUMP_FIRST_LOGITS")) { 56+ std::vector<float> row((size_t) ver.vocab()); 57+ std::FILE* f = ver.copy_logits(0, row.data()) ? std::fopen(fl, "wb") : nullptr; 58+ const bool ok = f && std::fwrite(row.data(), sizeof(float), row.size(), f) == row.size(); 59+ const bool closed = f && std::fclose(f) == 0; 60+ if (!ok || !closed) { 61+ std::fprintf(stderr, "strata serve: STRATA_DUMP_FIRST_LOGITS: cannot write %s\n", fl); 62+ std::printf("ERR cannot write diagnostic logits\n"); 63+ return 1; 64+ } 65+ } 66+ } 67diff --git a/tests/hip/prefill_gather_bytes.cpp b/tests/hip/prefill_gather_bytes.cpp 68new file mode 100644 69index0000000 ..f472adf 70--- /dev/null 71+++ b/tests/hip/prefill_gather_bytes.cpp 72@@ -0,0 +1,98 @@ 73+// Exact byte/guard test for the grouped gather used by native MMQ prefill. 74+// Arbitrary byte patterns are deliberate: gathering must preserve every bit, independent of quantized values. 75+#include "strata/prefill/moe_mmq.hpp" 76+#include "ggml.h" 77+#include <hip/hip_runtime.h> 78+ 79+#include <algorithm> 80+#include <cstdint> 81+#include <iostream> 82+#include <stdexcept> 83+#include <string> 84+#include <utility> 85+#include <vector> 86+ 87+namespace { 88+namespace mmq = strata::prefill::mmq; 89+void check(hipError_t code) { 90+ if (code != hipSuccess) throw std::runtime_error(hipGetErrorString(code)); 91+} 92+struct Buffer { 93+ uint8_t* p = nullptr; 94+ explicit Buffer(size_t bytes) { check(hipMalloc(reinterpret_cast<void**>(&p), bytes)); } 95+ ~Buffer() { if (p) hipFree(p); } 96+ Buffer(const Buffer&) = delete; 97+ Buffer& operator=(const Buffer&) = delete; 98+}; 99+void equal(const Buffer& device, const std::vector<uint8_t>& expected, const char* label) { 100+ std::vector<uint8_t> actual(expected.size()); 101+ check(hipMemcpy(actual.data(), device.p, actual.size(), hipMemcpyDeviceToHost)); 102+ if (actual != expected) { 103+ const auto at = std::mismatch(actual.begin(), actual.end(), expected.begin()).first - actual.begin(); 104+ throw std::runtime_error(std::string(label) + " byte mismatch at " + std::to_string(at)); 105+ } 106+} 107+void run(ggml_type gu_type, ggml_type d_type) { 108+ constexpr int slots = mmq::kGatherGroupMax; 109+ constexpr size_t guard = 64; 110+ // Production native expert dimensions; gaps also check that offsets and strides are respected. 111+ const size_t half = ggml_row_size(gu_type, 2560) * 640; 112+ const size_t down = ggml_row_size(d_type, 640) * 2560; 113+ const size_t up_off = half + guard, down_off = up_off + half + guard; 114+ const size_t blob_stride = down_off + down + guard; 115+ const size_t gu_stride = 2 * half + guard, d_stride = down + guard; 116+ std::vector<uint8_t> source(slots * blob_stride); 117+ for (size_t i = 0; i < source.size(); ++i) 118+ source[i] = static_cast<uint8_t>((i * 37 + i / 257 + i / blob_stride * 19) & 255); 119+ Buffer blobs(source.size()), gu(guard + slots * gu_stride + guard), d(guard + slots * d_stride + guard); 120+ check(hipMemcpy(blobs.p, source.data(), source.size(), hipMemcpyHostToDevice)); 121+ for (auto range : {std::pair<int, int>{0, 16}, {0, 1}, {0, 15}, {3, 11}, {15, 16}}) { 122+ std::vector<uint8_t> expected_gu(guard + slots * gu_stride + guard, 0xa5); 123+ std::vector<uint8_t> expected_d(guard + slots * d_stride + guard, 0xa5); 124+ check(hipMemset(gu.p, 0xa5, expected_gu.size())); 125+ check(hipMemset(d.p, 0xa5, expected_d.size())); 126+ mmq::GatherGroup group; 127+ group.first = range.first; group.n = range.second; 128+ for (int q = group.first; q < group.n; ++q) { 129+ // Non-contiguous expert order, including the last source allocation. 130+ const size_t offset = static_cast<size_t>((q * 7 + 3) % slots) * blob_stride; 131+ group.blob[q] = blobs.p + offset; 132+ std::copy_n(source.data() + offset, half, expected_gu.data() + guard + q * gu_stride); 133+ std::copy_n(source.data() + offset + up_off, half, expected_gu.data() + guard + q * gu_stride + half); 134+ std::copy_n(source.data() + offset + down_off, down, expected_d.data() + guard + q * d_stride); 135+ } 136+ if (!mmq::gather_native_group(group, up_off, half, down_off, down, 137+ gu.p + guard, gu_stride, d.p + guard, d_stride, nullptr)) 138+ throw std::runtime_error("aligned group unexpectedly rejected"); 139+ check(hipDeviceSynchronize()); 140+ equal(gu, expected_gu, "group gate/up including guards"); 141+ equal(d, expected_d, "group down including guards"); 142+ // The fallback contract is false with no writes for an unaligned source. 143+ ++group.blob[group.first]; 144+ if (mmq::gather_native_group(group, up_off, half, down_off, down, 145+ gu.p + guard, gu_stride, d.p + guard, d_stride, nullptr)) 146+ throw std::runtime_error("unaligned group unexpectedly accepted"); 147+ check(hipDeviceSynchronize()); 148+ equal(gu, expected_gu, "rejected group gate/up"); 149+ equal(d, expected_d, "rejected group down"); 150+ } 151+ std::cout << "PASS types=" << static_cast<int>(gu_type) << '/' << static_cast<int>(d_type) 152+ << " five full/partial groups; byte equality, untouched guards, unaligned rejection\n"; 153+} 154+} // namespace 155+ 156+int main() { 157+ try { 158+ // Union of the nine distinct format pairs in the pinned IQ3_S and IQ2_XS packs. 159+ for (auto pair : {std::pair<ggml_type, ggml_type>{GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ4_NL}, 160+ {GGML_TYPE_IQ3_XXS, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_NL}, 161+ {GGML_TYPE_IQ3_S, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ2_S, GGML_TYPE_IQ4_NL}, 162+ {GGML_TYPE_IQ2_S, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ4_XS, GGML_TYPE_IQ4_NL}, 163+ {GGML_TYPE_IQ2_XXS, GGML_TYPE_Q2_0}, {GGML_TYPE_IQ1_M, GGML_TYPE_Q2_0}}) 164+ run(pair.first, pair.second); 165+ } catch (const std::exception& error) { 166+ std::cerr << "FAIL: " << error.what() << '\n'; 167+ return 1; 168+ } 169+ return 0; 170+} 171diff --git a/tests/hip/qsa_decode_repeat.cpp b/tests/hip/qsa_decode_repeat.cpp 172new file mode 100644 173index0000000 ..f1440e8 174--- /dev/null 175+++ b/tests/hip/qsa_decode_repeat.cpp 176@@ -0,0 +1,74 @@ 177+// A fresh short prompt must not depend on bytes left in attention scratch by an earlier request. 178+#include "strata/kernels/qsa.hpp" 179+#include "strata/kernels/qsa_decode_attn.hpp" 180+#include <hip/hip_runtime.h> 181+#include <algorithm> 182+#include <cmath> 183+#include <cstdint> 184+#include <iostream> 185+#include <stdexcept> 186+#include <vector> 187+ 188+namespace k = strata::kernels; 189+namespace { 190+void check(hipError_t code) { if (code != hipSuccess) throw std::runtime_error(hipGetErrorString(code)); } 191+struct Buffer { 192+ void* p = nullptr; 193+ explicit Buffer(size_t bytes) { check(hipMalloc(&p, bytes)); } 194+ ~Buffer() { if (p) hipFree(p); } 195+ Buffer(const Buffer&) = delete; 196+ Buffer& operator=(const Buffer&) = delete; 197+ template<class T> T* as() const { return static_cast<T*>(p); } 198+}; 199+template<class T> void upload(Buffer& b, const std::vector<T>& v) { 200+ check(hipMemcpy(b.p, v.data(), v.size() * sizeof(T), hipMemcpyHostToDevice)); 201+} 202+void run(int tokens) { 203+ const auto s = k::qsa_real_shapes(); 204+ const int64_t cap = k::qsa_selection_width(32768, s), heads = s.n_head, dim = s.head_dim; 205+ const int64_t pages = (tokens + s.page_size - 1) / s.page_size, rows = pages * s.n_head_kv * s.page_size; 206+ std::vector<int8_t> kc(rows * dim), vc(rows * dim); 207+ std::vector<uint16_t> scales(rows * (dim / 64), 0x2000); // exact fp16 1/128 208+ std::vector<int32_t> table(pages), ids(tokens * cap, -1), steps(tokens * k::kStepCount, 0); 209+ std::vector<float> queries(tokens * heads * dim); 210+ for (int64_t i = 0; i < pages; ++i) table[i] = static_cast<int32_t>(i); 211+ 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; } 212+ for (size_t i = 0; i < queries.size(); ++i) queries[i] = std::sin(static_cast<float>(i % 1007) * 0.03f); 213+ for (int t = 0; t < tokens; ++t) { 214+ steps[t * k::kStepCount + k::kStepWidth] = t + 1; 215+ for (int c = 0; c <= t; ++c) ids[t * cap + c] = c; 216+ } 217+ Buffer dk(kc.size()), dv(vc.size()), ds(scales.size() * 2), dt(table.size() * 4), di(ids.size() * 4), 218+ dp(steps.size() * 4), dq(queries.size() * 4), out(queries.size() * 4); 219+ upload(dk, kc); upload(dv, vc); upload(ds, scales); upload(dt, table); upload(di, ids); upload(dp, steps); upload(dq, queries); 220+ k::QsaAttnPools pools{}; 221+ pools.k_q = dk.as<int8_t>(); pools.v_q = dv.as<int8_t>(); 222+ pools.k_scale = ds.as<uint16_t>(); pools.v_scale = ds.as<uint16_t>(); pools.page_table = dt.as<int32_t>(); 223+ const size_t scratch_bytes = 32 * k::qsa_decode_attn_scratch_floats(cap, s) * sizeof(float); 224+ Buffer scratch(scratch_bytes); 225+ std::vector<uint32_t> reference, got(queries.size()); 226+ for (int rep = 0; rep < 12; ++rep) { 227+ // Includes NaN, zero and nonzero patterns, and full/partial 32-query batches. 228+ check(hipMemset(scratch.p, rep % 3 == 0 ? 0xff : rep % 3 == 1 ? 0 : 0x35, scratch_bytes)); 229+ check(hipMemset(out.p, 0xff, queries.size() * 4)); 230+ for (int q = 0; q < tokens; q += 32) 231+ k::qsa_decode_attn_batch(dq.as<float>() + q * heads * dim, pools, di.as<int32_t>() + q * cap, 232+ dp.as<int32_t>() + q * k::kStepCount, cap, s, scratch.as<float>(), 233+ out.as<float>() + q * heads * dim, std::min(32, tokens - q), nullptr); 234+ check(hipDeviceSynchronize()); 235+ check(hipMemcpy(got.data(), out.p, got.size() * 4, hipMemcpyDeviceToHost)); 236+ std::vector<float> values(got.size()); 237+ check(hipMemcpy(values.data(), out.p, values.size() * 4, hipMemcpyDeviceToHost)); 238+ if (!std::all_of(values.begin(), values.end(), [](float v) { return std::isfinite(v); })) 239+ throw std::runtime_error("nonfinite or unwritten attention output"); 240+ if (rep == 0) reference = got; 241+ else if (got != reference) throw std::runtime_error("attention output changed with scratch contents/repeat"); 242+ } 243+ std::cout << "PASS tokens=" << tokens << " fixed-cap=" << cap << " 12 repeats with poisoned scratch\n"; 244+} 245+} 246+int main() { 247+ try { for (int tokens : {31, 32, 127, 255, 256, 511}) run(tokens); } 248+ catch (const std::exception& e) { std::cerr << "FAIL: " << e.what() << '\n'; return 1; } 249+ return 0; 250+}