diff --git a/metal/CMakeLists.txt b/metal/CMakeLists.txt index 39c88d8e..eab19017 100644 --- a/metal/CMakeLists.txt +++ b/metal/CMakeLists.txt @@ -39,7 +39,8 @@ target_compile_definitions(mtl_rt PRIVATE target_link_libraries(mtl_rt PUBLIC "-framework Metal" "-framework Foundation") file(GLOB STRATA_METAL_TESTS CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp) -list(REMOVE_ITEM STRATA_METAL_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/runtime_hello.cpp) +list(REMOVE_ITEM STRATA_METAL_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/runtime_hello.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tests/dsu_ref_vs_ggml.cpp) # (needs ggml: below) foreach(src ${STRATA_METAL_TESTS}) get_filename_component(t ${src} NAME_WE) add_executable(${t} ${src}) @@ -86,6 +87,10 @@ set(GGML_BACKEND_DL OFF CACHE BOOL "" FORCE) set(GGML_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(GGML_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) add_subdirectory(${STRATA_GGML_DIR}/ggml ${CMAKE_BINARY_DIR}/ggml EXCLUDE_FROM_ALL) +# ref_quant's IQ1_S / IQ1_M / Q6_K dequantizers against ggml's own (the Unsloth sets' kernels are checked against ref_quant: dsu_parity) +add_executable(dsu_ref_vs_ggml tests/dsu_ref_vs_ggml.cpp) +target_include_directories(dsu_ref_vs_ggml PRIVATE ${STRATA_GGML_DIR}/ggml/include) +target_link_libraries(dsu_ref_vs_ggml PRIVATE mtl_rt ggml-base ggml-cpu) set(S ${STRATA_ROOT}/src) set(STRATA_METAL_HOST_SOURCES diff --git a/metal/tests/dsu_job.metal b/metal/tests/dsu_job.metal new file mode 100644 index 00000000..02a8a788 --- /dev/null +++ b/metal/tests/dsu_job.metal @@ -0,0 +1,44 @@ +// metal/tests/dsu_job.metal - test-only helpers appended to ds4_iq.metal / moe_mm.metal in dsu_parity.cpp. +// dsu_mkjob writes a Ds4MvJob whose pointers are inside the one buffer it was given (so everything the real kernel +// dereferences is a bound, resident buffer): mem + offsets. +#ifdef DSU_MKJOB +kernel void dsu_mkjob(device uchar* mem [[buffer(0)]], constant ulong* o [[buffer(1)]], constant int* a [[buffer(2)]]) { + // o: job, w, xq, xd, y offsets; a: type, k, n, ncol, ld + device Ds4MvJob& j = *(device Ds4MvJob*) (mem + o[0]); + j.w = mem + o[1]; + j.ld = ulong(a[4]); + j.type = a[0]; + j.k = a[1]; + j.n = a[2]; + j.ncol = a[3]; + j.xq = (device const char*) (mem + o[2]); + j.xd = (device const float*) (mem + o[3]); + j.y = (device float*) (mem + o[4]); + j.ycs = ulong(a[2]); + j.bias = nullptr; + j.alpha = 1.0f; +} +#endif +#ifdef DSU_W8 +// every 4-value run the tile loader reads (k and k + 16 of each 32-group's first half) for T, into half rows +template +static inline void dsu_w8_row(device const uchar* row, int k_total, device half* out, uint gid) { + const int runs = k_total / 32 * 4; // per row: 4 runs of 4 values (k = 0,4,8,12 of the group), each giving k and k+16 + if (int(gid) >= runs) return; + const int grp = int(gid) >> 2, r = int(gid) & 3; + const int k = grp * 32 + r * 4; + threadgroup uint tab[1]; + half4 a, b; + W8::run(row, k, tab, a, b); + out[k] = a.x; out[k + 1] = a.y; out[k + 2] = a.z; out[k + 3] = a.w; + out[k + 16] = b.x; out[k + 17] = b.y; out[k + 18] = b.z; out[k + 19] = b.w; +} +kernel void dsu_w8_iq1s(device const uchar* W [[buffer(0)]], device half* out [[buffer(1)]], constant uint& k [[buffer(2)]], + constant uint& rb [[buffer(3)]], uint2 gid [[thread_position_in_grid]]) { + dsu_w8_row(W + ulong(gid.y) * rb, int(k), out + ulong(gid.y) * k, gid.x); +} +kernel void dsu_w8_iq1m(device const uchar* W [[buffer(0)]], device half* out [[buffer(1)]], constant uint& k [[buffer(2)]], + constant uint& rb [[buffer(3)]], uint2 gid [[thread_position_in_grid]]) { + dsu_w8_row(W + ulong(gid.y) * rb, int(k), out + ulong(gid.y) * k, gid.x); +} +#endif diff --git a/metal/tests/dsu_parity.cpp b/metal/tests/dsu_parity.cpp new file mode 100644 index 00000000..e1ec08a5 --- /dev/null +++ b/metal/tests/dsu_parity.cpp @@ -0,0 +1,151 @@ +// metal/tests/dsu_parity.cpp - the kernels Unsloth's UD-IQ1_S / UD-IQ1_M DeepSeek sets need on the Metal engine, against the CPU +// references in ref_quant.cpp (transcribed from ggml's dequantize_row_*; dsu_ref_vs_ggml checks that transcription against ggml itself): +// 1. ds4_iq1s_mvn1/2/4/8 (IQ1_S expert matvec; IQ1_M and IQ2_XXS as controls) and ds4_iq_mv_k (Q6_K dense matvec): the real engine +// kernels run on int8 activations; relative L2 error against the double-precision dot of the dequantized weights. +// 2. W8 (the prompt tile's weight loader, ds4_mm_gu_19 / ds4_mm_dn_19): bit-exact against the half-rounded reference. +// 3. ds4_dq_q6k (the dense prompt matrices' Q6_K -> half): bit-exact against the half-rounded reference. +#include "mtl_rt.h" +#include "ref_quant.h" + +#include +#include +#include +#include +#include + +namespace { +int fails = 0; +void report(const char* what, bool ok, double err) { + std::printf("%-62s err %.3e %s\n", what, err, ok ? "PASS" : "FAIL"); + fails += !ok; +} +size_t al(size_t v) { return (v + 255) & ~size_t(255); } + +// activations as the engine's q8 quantiser: per 32 values d = amax / 127, q = round(x / d) +void quant_x(const std::vector& x, int k, int ncol, std::vector& q, std::vector& d) { + q.assign((size_t)ncol * k, 0); + d.assign((size_t)ncol * k / 32, 0.f); + for (int c = 0; c < ncol; ++c) + for (int g = 0; g < k / 32; ++g) { + float am = 0; + for (int i = 0; i < 32; ++i) am = std::fmax(am, std::fabs(x[(size_t)c * k + g * 32 + i])); + const float dd = am / 127.f; + d[(size_t)c * (k / 32) + g] = dd; + for (int i = 0; i < 32; ++i) + q[(size_t)c * k + g * 32 + i] = dd == 0 ? 0 : (int8_t)std::lround(x[(size_t)c * k + g * 32 + i] / dd); + } +} +} // namespace + +int main() { + mtl::Context cx; + std::printf("device %s\n", cx.device_name().c_str()); + const int lib = cx.compile({"../src/engine/msl/ds4_iq.metal", "../tests/dsu_job.metal"}, "#define DSU_MKJOB 1\n"); + mtl::Pipeline mk = cx.pipeline(lib, "dsu_mkjob"); + + struct Case { const char* kern; int type; int nc; }; + const std::vector cases = {{"ds4_iq1s_mvn1", 19, 1}, {"ds4_iq1s_mvn2", 19, 2}, {"ds4_iq1s_mvn4", 19, 4}, {"ds4_iq1s_mvn8", 19, 8}, + {"ds4_iq1m_mvn4", 29, 4}, {"ds4_iq2xxs_mvn4", 16, 4}, {"ds4_iq_mv_k", 14, 8}, {"ds4_iq_mv_k", 14, 1}}; + for (const Case& cs : cases) { + for (int k : {512, 4096}) { + const int n = 83; // not a multiple of 8: the last threadgroup's tail rows + const int nc = cs.nc; + const size_t rb = ref::row_bytes(cs.type, k); + std::vector w(rb * n); + ref::random_blocks(cs.type, w.data(), n, k, 11 + cs.type + k, 0.05f); + ref::Rng r(77); + std::vector x((size_t)nc * k); + for (auto& v : x) v = r.sym() * 3.f; + std::vector xq; + std::vector xd; + quant_x(x, k, nc, xq, xd); + const size_t o_job = 0, o_w = al(80), o_xq = al(o_w + w.size()), o_xd = al(o_xq + xq.size()), o_y = al(o_xd + xd.size() * 4); + const size_t total = al(o_y + (size_t)nc * n * 4); + mtl::Buffer mem = cx.alloc(total), offs = cx.alloc(64), args = cx.alloc(64); + std::memcpy(mem.as() + o_w, w.data(), w.size()); + std::memcpy(mem.as() + o_xq, xq.data(), xq.size()); + std::memcpy(mem.as() + o_xd, xd.data(), xd.size() * 4); + unsigned long ov[5] = {o_job, o_w, o_xq, o_xd, o_y}; + int av[5] = {cs.type, k, n, nc, (int)rb}; + std::memcpy(offs.ptr, ov, sizeof ov); + std::memcpy(args.ptr, av, sizeof av); + mtl::Pipeline pk = cx.pipeline(lib, cs.kern); + mtl::Dispatch d0; + d0.p = &mk; d0.args = {mtl::Arg::B(mem), mtl::Arg::B(offs), mtl::Arg::B(args)}; d0.grid[0] = 1; d0.tg[0] = 1; + mtl::Dispatch d1; + d1.p = &pk; d1.args = {mtl::Arg::B(mem, o_job)}; d1.grid[0] = (n + 7) / 8; d1.grid[1] = 1; d1.tg[0] = 256; + cx.run({d0, d1}); + std::vector wf((size_t)n * k); + ref::dequant(cs.type, w.data(), wf.data(), wf.size()); + double se = 0, sr = 0; + const float* y = (const float*)(mem.as() + o_y); + for (int c = 0; c < nc; ++c) + for (int row = 0; row < n; ++row) { + double acc = 0; + for (int i = 0; i < k; ++i) + acc += (double)wf[(size_t)row * k + i] * ((double)xq[(size_t)c * k + i] * xd[(size_t)c * (k / 32) + i / 32]); + const double g = y[(size_t)c * n + row]; + se += (g - acc) * (g - acc); + sr += acc * acc; + } + const double e = std::sqrt(se / sr); + char nm[128]; + std::snprintf(nm, sizeof nm, "%s %s k=%d n=%d ncol=%d", cs.kern, ref::type_name(cs.type), k, n, nc); + report(nm, e < 2e-6, e); + } + } + + // 2. the tile loader + { + const int libw = cx.compile({"quant.metal", "units.metal", "mma.metal", "moe_mm.metal", "../tests/dsu_job.metal"}, "#define DSU_W8 1\n"); + for (int ty : {19, 29}) { + const int k = 2048, n = 37; + const size_t rb = ref::row_bytes(ty, k); + mtl::Buffer W = cx.alloc(rb * n), out = cx.alloc((size_t)n * k * 2); + ref::random_blocks(ty, W.as(), n, k, 5 + ty, 0.05f); + mtl::Pipeline pw = cx.pipeline(libw, ty == 19 ? "dsu_w8_iq1s" : "dsu_w8_iq1m"); + mtl::Dispatch d; + d.p = &pw; d.args = {mtl::Arg::B(W), mtl::Arg::B(out), mtl::Arg::V((uint32_t)k), mtl::Arg::V((uint32_t)rb)}; + d.grid[0] = (k / 32 * 4 + 63) / 64; d.grid[1] = n; d.tg[0] = 64; + cx.run({d}); + std::vector wf((size_t)n * k); + ref::dequant(ty, W.ptr, wf.data(), wf.size()); + size_t bad = 0; + const uint16_t* o = out.as(); + for (size_t i = 0; i < wf.size(); ++i) + if (o[i] != ref::f32_to_f16(wf[i])) ++bad; + char nm[112]; + std::snprintf(nm, sizeof nm, "tile loader W8<%s>, %zu values, mismatching half bits", ref::type_name(ty), wf.size()); + report(nm, bad == 0, (double)bad); + } + } + + // 3. Q6_K -> half + { + const int libd = cx.compile({"../src/engine/msl/ds4_init.metal"}); + mtl::Pipeline pq = cx.pipeline(libd, "ds4_dq_q6k"); + for (int k : {256, 2048}) { + const int n = 45; + const size_t rb = ref::row_bytes(14, k); + mtl::Buffer W = cx.alloc(rb * n), out = cx.alloc((size_t)n * k * 2); + ref::random_blocks(14, W.as(), n, k, 3 + k, 0.05f); + mtl::Dispatch d; + const unsigned long ld = rb; + const uint32_t uk = k, un = n; + d.p = &pq; d.args = {mtl::Arg::B(W), mtl::Arg::V(ld), mtl::Arg::V(uk), mtl::Arg::V(un), mtl::Arg::B(out)}; + d.grid[0] = (uint32_t)(((size_t)n * (k / 256) * 64 + 255) / 256); d.tg[0] = 256; + cx.run({d}); + std::vector wf((size_t)n * k); + ref::dequant(14, W.ptr, wf.data(), wf.size()); + size_t bad = 0; + const uint16_t* o = out.as(); + for (size_t i = 0; i < wf.size(); ++i) + if (o[i] != ref::f32_to_f16(wf[i])) ++bad; + char nm[96]; + std::snprintf(nm, sizeof nm, "ds4_dq_q6k k=%d n=%d, mismatching half bits", k, n); + report(nm, bad == 0, (double)bad); + } + } + std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS"); + return fails ? 1 : 0; +} diff --git a/metal/tests/dsu_ref_vs_ggml.cpp b/metal/tests/dsu_ref_vs_ggml.cpp new file mode 100644 index 00000000..d755ab0f --- /dev/null +++ b/metal/tests/dsu_ref_vs_ggml.cpp @@ -0,0 +1,29 @@ +// metal/tests/dsu_ref_vs_ggml.cpp - ref_quant.cpp's IQ1_S / IQ1_M / Q6_K dequantizers against ggml's own to_float (the pinned llama.cpp +// tree the engine build links), bit for bit. Built only with the engine (needs ggml-base). +#include "ref_quant.h" + +#include "ggml.h" + +#include +#include +#include + +int main() { + int fails = 0; + for (int ty : {ref::IQ1_S, ref::IQ1_M, ref::Q6_K, ref::IQ2_XXS}) { + const int n = 64, k = 2048; + std::vector w(ref::row_bytes(ty, k) * n); + ref::random_blocks(ty, w.data(), n, k, 21 + ty, 0.05f); + std::vector a((size_t)n * k), b((size_t)n * k); + ref::dequant(ty, w.data(), a.data(), a.size()); + const ggml_type_traits* tr = ggml_get_type_traits((ggml_type)ty); + if (!tr || !tr->to_float) { std::printf("%s: ggml has no to_float\n", ref::type_name(ty)); return 2; } + tr->to_float(w.data(), b.data(), (int64_t)b.size()); + const size_t bad = std::memcmp(a.data(), b.data(), a.size() * 4) == 0 ? 0 : 1; + size_t nbad = 0; + for (size_t i = 0; i < a.size(); ++i) nbad += std::memcmp(&a[i], &b[i], 4) != 0; + std::printf("%-8s ref_quant vs ggml to_float: %zu of %zu values differ %s\n", ref::type_name(ty), nbad, a.size(), nbad == 0 ? "PASS" : "FAIL"); + fails += (bad || nbad); + } + return fails ? 1 : 0; +}