Skip to content
Draft
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
17 changes: 17 additions & 0 deletions engine/include/strata/kernels/native_mmvq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ struct MmvqIds {
bool native_mmvq_ids_supported(int ggml_type);
void native_mmvq_ids_group(const int32_t* ids, int count, void* group_buf, void* stream);
bool native_mmvq_ids(int ggml_type, const MmvqIds& ids, int count, int n_in, int n_out, void* stream);
#if defined(STRATA_GPU_METAL)
struct MmvqIdsGuSwiglu {
const void* const* table;
const int32_t* ids;
std::size_t gate_offset;
std::size_t up_offset;
const void* x;
std::size_t x_stride;
int x_every;
void* q8_out;
void* group_buf = nullptr; ///< optional kMmvqIdsGroupBytes scratch for GPU expert dedup (count <= 64)
bool group_ready = false; ///< true if native_mmvq_ids_group() already filled group_buf for this ids array
};
bool native_mmvq_ids_gu_swiglu_q8_supported(int ggml_type);
bool native_mmvq_ids_gu_swiglu_q8(int ggml_type, const MmvqIdsGuSwiglu& ids, int count, int n_in, int n_ff,
void* stream);
#endif

// Storage/dispatch helpers take stable GGML type IDs, avoiding a ggml runtime
// dependency in the engine: Q4_0=2, Q5_0=6, Q8_0=8, Q2_K=10, Q3_K=11, Q4_K=12, Q5_K=13,
Expand Down
113 changes: 113 additions & 0 deletions engine/metal/poly_tests/quantized_parity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,119 @@ int main(){
}
}
}
// Fused routed-expert gate_up + SwiGLU + Q8_1 (poly_mul_*_k1_gu_swiglu_q8 & _k1g_gu_swiglu_q8)
// across Q4_K, IQ2_XS, IQ3_XXS, IQ2_S, NVFP4, including ungrouped, grouped (group size > 8), and id == -1 zeroing.
{
g::metal::set_optimize(true);
const int fni = 512, fnf = 256, n_exp = 6, fcount = 32;
auto fwf = values(size_t(n_exp) * (2 * fnf) * fni, 0.5f);
auto fx = values(size_t(fcount) * fni, 0.7f);
std::vector<float> fimp(fni, 1.f);
Buffer dfx(fx), fqx(k::native_q8_1_bytes(fni, fcount));
k::native_quantize_q8_1(dfx.as(), fqx.ptr, fni, fcount, stream);
ck(g::stream_synchronize(stream));

// Expert 0 appears 9 times (> 8 chunk!), expert 2 appears 3 times, expert 4 appears 1 time, and -1 appears 3 times!
std::vector<int32_t> fids = {
0, -1, 0, 2, 0, 3, 0, 5,
0, 2, 0, 1, 0, 4, 0, 5,
0, 3, 2, -1, 3, 5, 1, 3,
5, 1, 3, 5, 1, -1, 5, 1,
};
// Non-negative copy for unfused reference (we zero -1 rows in reference explicitly)
std::vector<int32_t> fids_ref = fids;
for (int32_t& id : fids_ref) if (id < 0) id = 0;

Buffer dfids(fcount * 4), dfids_ref(fcount * 4), dfgrp(k::kMmvqIdsGroupBytes);
dfids.put(fids.data(), fcount * 4);
dfids_ref.put(fids_ref.data(), fcount * 4);
k::native_mmvq_ids_group(dfids.as<int32_t>(), fcount, dfgrp.ptr, stream);

const size_t q8_out_bytes = k::native_q8_1_bytes(fnf, fcount);
const size_t q8_row_bytes = k::native_q8_1_bytes(fnf, 1);

auto cmp_q8_with_skip = [&](const Buffer& got_b, const Buffer& ref_b, const char* lbl) {
auto gv = got_b.read<uint8_t>(q8_out_bytes);
auto rv = ref_b.read<uint8_t>(q8_out_bytes);
int mism = 0;
const size_t nblk_row = size_t(fnf / 32);
for (int r = 0; r < fcount; ++r) {
const size_t base = size_t(r) * q8_row_bytes;
if (fids[r] < 0) {
for (size_t i = 0; i < q8_row_bytes; ++i)
if (gv[base + i] != 0) ++mism;
} else {
for (size_t b = 0; b < nblk_row; ++b) {
const uint8_t* gb = gv.data() + base + b * 36;
const uint8_t* rb = rv.data() + base + b * 36;
_Float16 gd, gs, rd, rs;
std::memcpy(&gd, gb, 2); std::memcpy(&gs, gb + 2, 2);
std::memcpy(&rd, rb, 2); std::memcpy(&rs, rb + 2, 2);
if (std::abs(float(gd) - float(rd)) > 1e-4f + 2e-3f * std::abs(float(rd))) ++mism;
if (std::abs(float(gs) - float(rs)) > 1e-3f + 2e-3f * std::abs(float(rs))) ++mism;
for (int j = 0; j < 32; ++j) {
const float vg = float(gd) * float(int8_t(gb[4 + j]));
const float vr = float(rd) * float(int8_t(rb[4 + j]));
if (std::abs(vg - vr) > std::max(2e-3f, 2.0f * std::abs(float(rd)))) ++mism;
}
}
}
}
std::printf("%s: %d mismatches across %zu bytes\n", lbl, mism, q8_out_bytes);
return mism;
};

for (int ftype : {12, 17, 18, 22, 40}) {
auto ft = ggml_type(ftype);
ggml_quantize_init(ft);
const size_t gu_half_bytes = k::native_mmvq_weight_bytes(ftype, fni, fnf);
const size_t exp_bytes = 2 * gu_half_bytes;
std::vector<uint8_t> fpacked(exp_bytes * n_exp);
ggml_quantize_chunk(ft, fwf.data(), fpacked.data(), 0, n_exp * (2 * fnf), fni, fimp.data());
Buffer dfw(fpacked.size());
dfw.put(fpacked.data(), fpacked.size());
std::vector<const void*> ftab(n_exp);
for (int e = 0; e < n_exp; ++e) ftab[e] = static_cast<uint8_t*>(dfw.ptr) + size_t(e) * exp_bytes;
Buffer dftab(n_exp * sizeof(void*));
dftab.put(ftab.data(), n_exp * sizeof(void*));

Buffer fg_ref(size_t(fcount) * 2 * fnf * 4), fq_ref(q8_out_bytes), fq_ung(q8_out_bytes), fq_grp(q8_out_bytes);
for (int every : {1, 8}) {
// Unfused reference: native_mmvq_ids + swiglu_rows_quantize_q8_1
k::MmvqIds ref_ids{dftab.as<const void*>(), dfids_ref.as<int32_t>(), 0, fqx.ptr,
k::native_q8_1_bytes(fni), every, fg_ref.as(), size_t(2 * fnf)};
k::native_mmvq_ids(ftype, ref_ids, fcount, fni, 2 * fnf, stream);
p::swiglu_rows_quantize_q8_1(fg_ref.as(), fq_ref.ptr, fnf, fcount, stream);

// Pre-fill outputs with non-zero sentinel to verify id == -1 actually writes zeros!
ck(g::memset_async(fq_ung.ptr, 0x5A, q8_out_bytes, stream));
ck(g::memset_async(fq_grp.ptr, 0x5A, q8_out_bytes, stream));

k::MmvqIdsGuSwiglu gu_ung{dftab.as<const void*>(), dfids.as<int32_t>(), 0, gu_half_bytes,
fqx.ptr, k::native_q8_1_bytes(fni), every, fq_ung.ptr};
k::MmvqIdsGuSwiglu gu_grp = gu_ung;
gu_grp.q8_out = fq_grp.ptr;
gu_grp.group_buf = dfgrp.ptr;
gu_grp.group_ready = true;

const bool ok_u = k::native_mmvq_ids_gu_swiglu_q8(ftype, gu_ung, fcount, fni, fnf, stream);
const bool ok_g = k::native_mmvq_ids_gu_swiglu_q8(ftype, gu_grp, fcount, fni, fnf, stream);
ck(g::stream_synchronize(stream));
if (!ok_u || !ok_g) {
std::printf("FAIL: native_mmvq_ids_gu_swiglu_q8 returned false for %s every=%d\n",
ggml_type_name(ft), every);
++bad;
continue;
}
char flbl[128];
std::snprintf(flbl, sizeof(flbl), "gu_swiglu_q8 ungrouped %s every=%d", ggml_type_name(ft), every);
bad += cmp_q8_with_skip(fq_ung, fq_ref, flbl);
std::snprintf(flbl, sizeof(flbl), "gu_swiglu_q8 grouped %s every=%d", ggml_type_name(ft), every);
bad += cmp_q8_with_skip(fq_grp, fq_ref, flbl);
}
}
}

p::metal_detail::allow_mm_rows(true);
ck(g::stream_synchronize(stream));ck(g::stream_destroy(stream));ggml_quantize_free();return bad?1:0;
}
Loading
Loading