Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions engine/metal/poly_tests/qwen4_parity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ggml.h"

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -529,6 +530,222 @@ int test_qsa(g::Stream s) {
bad += compare(dout.read(ref.size()), ref, lbl, 1e-4f);
}
}

// 4) q4_qsa_attend batch MMA parity matrix (rows=1,64,512; G=4,8; dim=256; n_ids=0,17,64,512,2048; ring=0 & >0; Q8_0 & FP16) + timing
{
struct BatchCase {
const char* name;
int rows;
int G;
int n_kv;
int dim;
int n_cache;
int sel_cap;
int n_ids; // fixed n_ids, or -1 for mixed {0, 17, 64, 512}
int ring; // 0 or > 0
bool strided; // test non-default Q8 row stride
bool bench; // run timing microbenchmark
};
const BatchCase cases[] = {
{"r1_G8_n0", 1, 8, 2, 256, 128, 64, 0, 0, false, false},
{"r1_G4_n17", 1, 4, 2, 256, 128, 64, 17, 0, false, false},
{"r1_G8_n64", 1, 8, 2, 256, 128, 64, 64, 0, false, false},
{"r64_G4_n512_rng", 64, 4, 2, 256, 257, 512, -1, 257, false, false},
{"r64_G8_n2048", 64, 8, 2, 256, 2200, 2048, 2048, 0, false, false},
{"r512_G8_n2048", 512, 8, 2, 256, 2200, 2048, 2048, 0, false, true},
{"r20_G8_d128_fb", 20, 8, 2, 128, 160, 128, 80, 0, false, false},
{"r20_G8_strided", 20, 8, 2, 256, 160, 128, 97, 0, true, false},
};
for (const auto& tc : cases) {
for (int use_q8 : {0, 1}) {
if (tc.strided && !use_q8) continue;
const int rows = tc.rows;
const int n_kv = tc.n_kv;
const int n_head = tc.G * n_kv;
const int dim = tc.dim;
const int n_cache = tc.n_cache;
const int sel_cap = tc.sel_cap;
const float scale = 1.0f / std::sqrt(float(dim));

auto q = values(size_t(rows) * n_head * dim, 0.4f);
auto kf = values(size_t(n_cache) * n_kv * dim, 0.5f);
auto vf = values(size_t(n_cache) * n_kv * dim, 0.5f);

std::vector<int32_t> h_ids(size_t(rows) * sel_cap, 0), h_cnt(rows, 0);
const int mix_counts[4] = {0, 17, 64, 512};
for (int r = 0; r < rows; ++r) {
const int cnt = (tc.n_ids >= 0) ? tc.n_ids : mix_counts[r & 3];
h_cnt[r] = cnt;
for (int j = 0; j < cnt; ++j) {
// When ring > 0, emit un-wrapped logical indices > ring to exercise modulo wrap
const int raw = (tc.ring > 0) ? (j * 7 + r * 13 + tc.ring) : ((j * 7 + r * 5) % n_cache);
h_ids[size_t(r) * sel_cap + j] = raw;
}
}

Buffer dq(q), dids(size_t(rows) * sel_cap * 4), dcnt(size_t(rows) * 4);
Buffer dout(size_t(rows) * n_head * dim * 4);
dids.put(h_ids.data(), h_ids.size() * 4);
dcnt.put(h_cnt.data(), h_cnt.size() * 4);
// Poison output buffer to prove n_ids == 0 explicitly zeroes out
ck(g::memset_async(dout.ptr, 0x3f, size_t(rows) * n_head * dim * 4, s));

const size_t kv_cols = size_t(n_kv) * dim;
const size_t k_rs = tc.strided ? (kv_cols + 64) : kv_cols;
const size_t v_rs = tc.strided ? (kv_cols + 96) : kv_cols;

Buffer dk16(use_q8 ? 4 : size_t(n_cache) * kv_cols * 2);
Buffer dv16(use_q8 ? 4 : size_t(n_cache) * kv_cols * 2);
Buffer dkc(use_q8 ? size_t(n_cache) * k_rs : 4);
Buffer dks(use_q8 ? size_t(n_cache) * (k_rs / 32) * 2 : 4);
Buffer dvc(use_q8 ? size_t(n_cache) * v_rs : 4);
Buffer dvs(use_q8 ? size_t(n_cache) * (v_rs / 32) * 2 : 4);

if (use_q8) {
if (tc.strided) {
Buffer dkf(kf), dvf(vf);
Buffer tkc(size_t(n_cache) * kv_cols), tks(size_t(n_cache) * (kv_cols / 32) * 2);
Buffer tvc(size_t(n_cache) * kv_cols), tvs(size_t(n_cache) * (kv_cols / 32) * 2);
p::store_q8(dkf.as(), tkc.as<int8_t>(), tks.as<uint16_t>(), int(kv_cols), 0, n_cache, s);
p::store_q8(dvf.as(), tvc.as<int8_t>(), tvs.as<uint16_t>(), int(kv_cols), 0, n_cache, s);
ck(g::stream_synchronize(s));
auto hkc = tkc.read<int8_t>(size_t(n_cache) * kv_cols);
auto hks = tks.read<uint16_t>(size_t(n_cache) * (kv_cols / 32));
auto hvc = tvc.read<int8_t>(size_t(n_cache) * kv_cols);
auto hvs = tvs.read<uint16_t>(size_t(n_cache) * (kv_cols / 32));
std::vector<int8_t> pkc(size_t(n_cache) * k_rs, 0), pvc(size_t(n_cache) * v_rs, 0);
std::vector<uint16_t> pks(size_t(n_cache) * (k_rs / 32), 0), pvs(size_t(n_cache) * (v_rs / 32), 0);
for (int r = 0; r < n_cache; ++r) {
std::memcpy(pkc.data() + size_t(r) * k_rs, hkc.data() + size_t(r) * kv_cols, kv_cols);
std::memcpy(pks.data() + size_t(r) * (k_rs / 32), hks.data() + size_t(r) * (kv_cols / 32), (kv_cols / 32) * 2);
std::memcpy(pvc.data() + size_t(r) * v_rs, hvc.data() + size_t(r) * kv_cols, kv_cols);
std::memcpy(pvs.data() + size_t(r) * (v_rs / 32), hvs.data() + size_t(r) * (kv_cols / 32), (kv_cols / 32) * 2);
}
dkc.put(pkc.data(), pkc.size());
dks.put(pks.data(), pks.size() * 2);
dvc.put(pvc.data(), pvc.size());
dvs.put(pvs.data(), pvs.size() * 2);
} else {
Buffer dkf(kf), dvf(vf);
p::store_q8(dkf.as(), dkc.as<int8_t>(), dks.as<uint16_t>(), int(kv_cols), 0, n_cache, s);
p::store_q8(dvf.as(), dvc.as<int8_t>(), dvs.as<uint16_t>(), int(kv_cols), 0, n_cache, s);
}
} else {
Buffer dkf(kf), dvf(vf);
p::store_f16(dkf.as(), dk16.as<uint16_t>(), int(kv_cols), 0, n_cache, s);
p::store_f16(dvf.as(), dv16.as<uint16_t>(), int(kv_cols), 0, n_cache, s);
}

p::Kvq8 kq = use_q8 ? p::Kvq8{dkc.as<int8_t>(), dks.as<uint16_t>(), tc.strided ? k_rs : 0} : p::Kvq8{};
p::Kvq8 vq = use_q8 ? p::Kvq8{dvc.as<int8_t>(), dvs.as<uint16_t>(), tc.strided ? v_rs : 0} : p::Kvq8{};

// Pass part_buf = nullptr so even rows == 1 routes through the batch kernel.
p::q4_qsa_attend(dq.as(), use_q8 ? nullptr : dk16.as<uint16_t>(), use_q8 ? nullptr : dv16.as<uint16_t>(),
kq, vq, dids.as<int32_t>(), sel_cap, dcnt.as<int32_t>(), dout.as(),
nullptr, 0, dim, n_head, n_kv, rows, scale, tc.ring, s);
ck(g::stream_synchronize(s));

// Reconstruct exact dequantized K/V on CPU
std::vector<float> kd(size_t(n_cache) * kv_cols), vd(size_t(n_cache) * kv_cols);
if (use_q8) {
auto kc = dkc.read<int8_t>(size_t(n_cache) * k_rs);
auto ks = dks.read<uint16_t>(size_t(n_cache) * (k_rs / 32));
auto vc = dvc.read<int8_t>(size_t(n_cache) * v_rs);
auto vs = dvs.read<uint16_t>(size_t(n_cache) * (v_rs / 32));
for (int r = 0; r < n_cache; ++r) {
for (size_t c = 0; c < kv_cols; ++c) {
kd[size_t(r) * kv_cols + c] = float(kc[size_t(r) * k_rs + c]) * ggml_fp16_to_fp32(ks[size_t(r) * (k_rs / 32) + c / 32]);
vd[size_t(r) * kv_cols + c] = float(vc[size_t(r) * v_rs + c]) * ggml_fp16_to_fp32(vs[size_t(r) * (v_rs / 32) + c / 32]);
}
}
} else {
auto k16 = dk16.read<uint16_t>(size_t(n_cache) * kv_cols);
auto v16 = dv16.read<uint16_t>(size_t(n_cache) * kv_cols);
for (size_t i = 0; i < kd.size(); ++i) {
kd[i] = ggml_fp16_to_fp32(k16[i]);
vd[i] = ggml_fp16_to_fp32(v16[i]);
}
}

// Sample rows for large n_ids cases to keep CPU reference fast while covering boundaries
std::vector<int> check_rows;
if (rows <= 64 && sel_cap <= 512) {
check_rows.resize(rows);
std::iota(check_rows.begin(), check_rows.end(), 0);
} else {
for (int r : {0, 1, 7, 17, 31, 63, 128, 255, 384, rows - 1}) {
if (r >= 0 && r < rows && (check_rows.empty() || check_rows.back() != r))
check_rows.push_back(r);
}
}

auto got_all = dout.read(size_t(rows) * n_head * dim);
std::vector<float> got_sub(check_rows.size() * n_head * dim, 0.0f);
std::vector<float> ref_sub(check_rows.size() * n_head * dim, 0.0f);
for (size_t ri = 0; ri < check_rows.size(); ++ri) {
const int r = check_rows[ri];
std::memcpy(got_sub.data() + ri * n_head * dim,
got_all.data() + size_t(r) * n_head * dim,
size_t(n_head) * dim * 4);
const int cnt = h_cnt[r];
if (cnt <= 0) continue;
std::vector<double> sc(cnt);
for (int h = 0; h < n_head; ++h) {
const int kvh = h / tc.G;
double mx = -1e300;
for (int j = 0; j < cnt; ++j) {
int cell = h_ids[size_t(r) * sel_cap + j];
if (tc.ring > 0) cell %= tc.ring;
double dot = 0.0;
for (int d = 0; d < dim; ++d)
dot += double(q[(size_t(r) * n_head + h) * dim + d]) *
double(kd[(size_t(cell) * n_kv + kvh) * dim + d]);
sc[j] = dot * scale;
if (sc[j] > mx) mx = sc[j];
}
double sum = 0.0;
for (int j = 0; j < cnt; ++j) { sc[j] = std::exp(sc[j] - mx); sum += sc[j]; }
for (int d = 0; d < dim; ++d) {
double acc = 0.0;
for (int j = 0; j < cnt; ++j) {
int cell = h_ids[size_t(r) * sel_cap + j];
if (tc.ring > 0) cell %= tc.ring;
acc += sc[j] * double(vd[(size_t(cell) * n_kv + kvh) * dim + d]);
}
ref_sub[(ri * n_head + h) * dim + d] = float(acc / sum);
}
}
}

char lbl[96];
std::snprintf(lbl, sizeof(lbl), "q4_qsa_attend_batch %s %s", use_q8 ? "q8" : "f16", tc.name);
const float tol = (dim == 256 && tc.G <= 8) ? 2e-4f : 1e-4f;
bad += compare(got_sub, ref_sub, lbl, tol);

if (tc.bench) {
const int warmup = 3, iters = 10;
for (int it = 0; it < warmup; ++it) {
p::q4_qsa_attend(dq.as(), use_q8 ? nullptr : dk16.as<uint16_t>(), use_q8 ? nullptr : dv16.as<uint16_t>(),
kq, vq, dids.as<int32_t>(), sel_cap, dcnt.as<int32_t>(), dout.as(),
nullptr, 0, dim, n_head, n_kv, rows, scale, tc.ring, s);
}
ck(g::stream_synchronize(s));
const auto t0 = std::chrono::steady_clock::now();
for (int it = 0; it < iters; ++it) {
p::q4_qsa_attend(dq.as(), use_q8 ? nullptr : dk16.as<uint16_t>(), use_q8 ? nullptr : dv16.as<uint16_t>(),
kq, vq, dids.as<int32_t>(), sel_cap, dcnt.as<int32_t>(), dout.as(),
nullptr, 0, dim, n_head, n_kv, rows, scale, tc.ring, s);
}
ck(g::stream_synchronize(s));
const auto t1 = std::chrono::steady_clock::now();
const double ms = std::chrono::duration<double, std::milli>(t1 - t0).count() / iters;
const double flops = 4.0 * double(rows) * double(n_head) * double(tc.n_ids) * double(dim);
const double tflops = (flops / (ms * 1e-3)) / 1e12;
std::printf(" bench %s: %.3f ms/call (%.2f TFLOP/s)\n", lbl, ms, tflops);
}
}
}
}
return bad;
}

Expand Down
9 changes: 6 additions & 3 deletions engine/metal/src/poly/qwen4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,12 @@ void q4_qsa_attend(const float* q, const uint16_t* keys, const uint16_t* vals, K
gpu::metal::Access::R, gpu::metal::Access::R, gpu::metal::Access::W
};
const size_t s[10] = {q_b, 0ULL, 0ULL, 0ULL, 0ULL, 0ULL, 0ULL, ids_b, cnt_b, q_b};
const char* kbatch = is_q8 ? (G <= 8 ? "q4_qsa_attend_batch_q8_8" : "q4_qsa_attend_batch_q8_16")
: (G <= 8 ? "q4_qsa_attend_batch_f16_8" : "q4_qsa_attend_batch_f16_16");
run(kbatch, a, stream, {uint32_t(n_kv), uint32_t(rows), 1}, 256, {a.p, 10, true, nullptr, m, s});
const bool use_mma = (dim == 256 && G <= 8 && gpu::metal::optimize());
const char* kbatch = use_mma
? (is_q8 ? "q4_qsa_attend_batch_mma_q8_8" : "q4_qsa_attend_batch_mma_f16_8")
: (is_q8 ? (G <= 8 ? "q4_qsa_attend_batch_q8_8" : "q4_qsa_attend_batch_q8_16")
: (G <= 8 ? "q4_qsa_attend_batch_f16_8" : "q4_qsa_attend_batch_f16_16"));
run(kbatch, a, stream, {uint32_t(n_kv), uint32_t(rows), 1}, use_mma ? 128 : 256, {a.p, 10, true, nullptr, m, s});
}
}

Expand Down
Loading
Loading