mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
qwen35: prompt experts of Qwen3.6 on the fused int8 kernels where they cover a layer's formats, MMQ for the rest, the older grouped kernels as the fallback
The Qwen3.6 engine ran every prompt chunk of more than 8 rows through llama.cpp's MMQ (CUDA), or through its own grouped decode kernels where MMQ does not exist or STRATA_Q36_NO_MMQ=1 (0.35-0.41 of MMQ's speed on a Tesla P100). Per layer it now takes, in this order: the fused kernels of the previous commit (fused_q36) when native_supported(gate/up type, down type) holds on the device; else MMQ when it has tiles for the layer's formats; else the old kernels as before. Nothing the user sees changes except the engine's one line (marked [DRAFT] in the code; the wording is the owner's, listed in par\OWNER-WORDING-1111.md under P4-Q36): "prompt experts on the fused int8 kernels" when every layer is on them, or "... (N of M layers), the rest on MMQ (int8)". No new setting; STRATA_PF_FUSED=0 gives MMQ again, STRATA_PF_FUSED=0 with STRATA_Q36_NO_MMQ=1 the old kernels. For a fused layer the prompt path quantizes the activations (quantize_act_native), groups the routed rows on the GPU (fused::group, no host sync outside the hybrid mode), launches 128 experts at a time with a Batch of the experts' blobs where they are (the resident weights, or the streamed stage of the hybrid mode, which streams as it did for MMQ) and combines through the group's row map. The hybrid mode streams a layer's experts for a prompt chunk when some path of the chunk is a fused or MMQ one. main sets STRATA_PF_FUSED_KQ, _Q6K and _Q8GU to 1 when unset (existing switches, so those formats' kernels are set up on the device). Covered with this commit on sm_80+ CUDA: UD-Q6_K_XL (Q6_K / Q8_0 on 40 layers, Q8_0 / Q8_0 on 1) and the Q8_0 file (Q8_0 / Q8_0 on 41). UD-Q4_K_XL's downs (Q5_K, Q6_K) have no fused decoder yet: it stays on MMQ, layer by layer. No default of another model changes (the fused_q36 entry points are only called here). RTX A4000 (sm_86, 16 GB, UD-Q6_K_XL, hybrid, z440; the engine of the first build, whose only difference from this tree is the [DRAFT] tag of the line): - engine line "prompt experts on the fused int8 kernels"; the 64-token answer of a 2K prompt reads as the MMQ arm's summary and diverges from it at token 26 (MMQ at another chunk size also diverges); - first-token KL against MMQ at the default chunk (floor: MMQ at chunk 1024): 2K x 8 prompts fused mean 0.00315 / max 0.00868, floor 0.00311 / 0.00948; 8K x 4 fused 0.00674 / 0.01940, floor 0.00276 / 0.00521 (one prompt carries it, the floor is also highest on it); the same top token on all 12 prompts (old kernels at 2K x 4: 0.00265, same top token); - prompt speed, --bench-pp, tok/s: 2K fused 763.0 / 763.1, MMQ 763.3 / 763.3, old kernels 383.8 / 385.7; 8K 763.4 / 763.3, 763.6 / 763.6, 376.0 / 373.4; 20K 745.5 / 745.4, 745.8 / 745.7, 360.5 / 357.4. On this card the experts of every layer stream over PCIe for each chunk in both arms, which bounds both: the one-layer test of the previous commit shows the kernels (2048 tokens MMQ / fused 1.12 and 1.39, 8192 tokens 0.64 and 0.65). Not run for this commit: a card with every expert in VRAM end to end (the next job), the forced-token, first-read, short-prompt and agent-turn checks of the default gate. The switch is not a default change on any card.
This commit is contained in:
1 parent
bd5c4604fd
commit
3a61182bbf
1 file changed
+130
-39
+130
-39
@@ -24,6 +24,8 @@
|
||||
#include "strata/kernels/native_moe.hpp"
|
||||
#include "strata/kernels/native_router.hpp"
|
||||
#include "strata/prefill/gemm.hpp"
|
||||
#include "strata/prefill/moe_fused.hpp"
|
||||
#include "strata/prefill/moe_fused_q36.hpp"
|
||||
#include "strata/prefill/moe_mmq.hpp"
|
||||
#include "strata/qwen35/engine.hpp"
|
||||
#include "strata/qwen35/sampling.hpp"
|
||||
@@ -64,6 +66,8 @@ namespace {
|
||||
|
||||
namespace K = strata::kernels;
|
||||
namespace MMQ = strata::prefill::mmq;
|
||||
namespace PF = strata::prefill::fused; // the grouping and the int8 sizes (shape-free)
|
||||
namespace FQ = strata::prefill::fused_q36; // the fused kernels for Qwen3.6's shape (CUDA)
|
||||
|
||||
#define CK(x) \
|
||||
do { \
|
||||
@@ -1182,6 +1186,14 @@ struct Engine {
|
||||
__half* att_p16 = nullptr; // the probabilities in FP16 (kAttScoreFloats halves)
|
||||
__half *k16 = nullptr, *v16 = nullptr, *q16 = nullptr; // the prompt attention's FP16 operands
|
||||
size_t kAttScoreFloats = (size_t) 64 << 20; // 256 MB (16 M floats when the experts' fit is close)
|
||||
// the prompt's experts on Strata's fused int8 kernels (moe_fused_iq_q36.cu, CUDA): per layer (fused_on), the rest on MMQ
|
||||
// where it has a tile, else the native grouped kernels. use_pf: every layer's prompt chunks have one of the two, so the
|
||||
// hybrid mode streams each layer's experts for a chunk (the strategy MMQ had alone before)
|
||||
std::vector<char> fused_on; // by state index (the MTP block last)
|
||||
bool use_pf = false;
|
||||
void* f_scr = nullptr; // the grouping tables (fused::group_bytes)
|
||||
void *f_xq = nullptr, *f_ha = nullptr; // the layer's input and H in the kernels' int8 form (fused::act_bytes)
|
||||
int32_t *f_slot = nullptr, *f_src = nullptr;
|
||||
// the prompt's MMQ experts
|
||||
bool use_mmq = false;
|
||||
std::unique_ptr<MMQ::Context> mctx;
|
||||
@@ -1371,22 +1383,58 @@ struct Engine {
|
||||
k16 = dalloc_t<__half>((size_t) g.n_head_kv * ctx * g.head_dim);
|
||||
v16 = dalloc_t<__half>((size_t) g.n_head_kv * ctx * g.head_dim);
|
||||
q16 = dalloc_t<__half>(T * g.n_head * g.head_dim);
|
||||
use_mmq = MMQ::built() && std::getenv("STRATA_Q36_NO_MMQ") == nullptr;
|
||||
const bool mmq_avail = MMQ::built() && std::getenv("STRATA_Q36_NO_MMQ") == nullptr;
|
||||
auto mmq_ok = [&](const Layer& L) {
|
||||
return MMQ::supported(L.L.gu_type) && MMQ::supported(L.L.d_type) && MMQ::fits(L.L.gu_type, 2 * g.n_ff) &&
|
||||
MMQ::fits(L.L.d_type, g.n_embd);
|
||||
};
|
||||
for (const Layer& L : m->layers) use_mmq = use_mmq && mmq_ok(L);
|
||||
if (m->has_mtp) use_mmq = use_mmq && mmq_ok(m->mtp);
|
||||
if (use_mmq) {
|
||||
const size_t n_state = m->layers.size() + (m->has_mtp ? 1 : 0);
|
||||
fused_on.assign(n_state, 0);
|
||||
#if !defined(__HIPCC__)
|
||||
// Strata's fused kernels where they cover the layer's pair on this card (STRATA_PF_FUSED=0: none, as for Flash-Next)
|
||||
if (const char* pf = std::getenv("STRATA_PF_FUSED"); !(pf != nullptr && pf[0] == '0') && g.n_embd == FQ::kQ36Embd &&
|
||||
g.n_ff == FQ::kQ36FF)
|
||||
for (size_t i = 0; i < n_state; ++i) {
|
||||
const Layer& L = layer_at((int) i);
|
||||
fused_on[i] = FQ::native_supported(L.L.gu_type, L.L.d_type) ? 1 : 0;
|
||||
}
|
||||
#endif
|
||||
use_pf = true;
|
||||
bool any_mmq = false;
|
||||
size_t n_fused = 0;
|
||||
for (size_t i = 0; i < n_state; ++i) {
|
||||
const bool mq = !fused_on[i] && mmq_avail && mmq_ok(layer_at((int) i));
|
||||
use_pf = use_pf && (fused_on[i] || mq);
|
||||
any_mmq = any_mmq || mq;
|
||||
n_fused += fused_on[i] ? 1 : 0;
|
||||
}
|
||||
if (!use_pf) { // a layer has neither: every prompt chunk on the native grouped kernels, as before
|
||||
std::fill(fused_on.begin(), fused_on.end(), 0);
|
||||
n_fused = 0;
|
||||
any_mmq = false;
|
||||
}
|
||||
use_mmq = use_pf && any_mmq;
|
||||
if (use_pf) {
|
||||
const int64_t ne = (int64_t) T * Kp;
|
||||
mctx = std::make_unique<MMQ::Context>();
|
||||
rcnt = dalloc_t<int32_t>((size_t) g.n_expert);
|
||||
rcur = dalloc_t<int32_t>((size_t) g.n_expert);
|
||||
rbounds = dalloc_t<int32_t>((size_t) g.n_expert + 1);
|
||||
rmax = dalloc_t<int32_t>(1);
|
||||
ident = dalloc_t<int32_t>((size_t) ne);
|
||||
rinv = dalloc_t<int32_t>((size_t) ne);
|
||||
#if !defined(__HIPCC__)
|
||||
if (n_fused > 0) {
|
||||
CK(cudaMalloc(&f_scr, PF::group_bytes(ne, (int) g.n_expert)));
|
||||
CK(cudaMalloc(&f_xq, PF::act_bytes((int64_t) T, g.n_embd)));
|
||||
CK(cudaMalloc(&f_ha, PF::act_bytes(ne, g.n_ff)));
|
||||
f_slot = dalloc_t<int32_t>((size_t) ne);
|
||||
f_src = dalloc_t<int32_t>((size_t) ne);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (use_mmq) {
|
||||
const int64_t ne = (int64_t) T * Kp;
|
||||
mctx = std::make_unique<MMQ::Context>();
|
||||
ident = dalloc_t<int32_t>((size_t) ne);
|
||||
CK(cudaDeviceSynchronize()); // ident's cudaMemset (legacy stream) before the iota on the non-blocking stream
|
||||
MMQ::iota(ident, ne, st);
|
||||
CK(cudaMalloc(&mq_x, MMQ::q8_bytes(ne, g.n_embd)));
|
||||
@@ -1394,7 +1442,13 @@ struct Engine {
|
||||
mq_gu = dalloc((size_t) ne * 2 * FF);
|
||||
mq_h = dalloc((size_t) ne * FF);
|
||||
}
|
||||
std::fprintf(stderr, "strata-q36: prompt experts on %s\n", use_mmq ? "MMQ (int8)" : "the native grouped kernels");
|
||||
if (n_fused == n_state)
|
||||
std::fprintf(stderr, "strata-q36: [DRAFT] prompt experts on the fused int8 kernels\n");
|
||||
else if (n_fused > 0)
|
||||
std::fprintf(stderr, "strata-q36: [DRAFT] prompt experts on the fused int8 kernels (%zu of %zu layers), the rest on MMQ (int8)\n",
|
||||
n_fused, n_state);
|
||||
else
|
||||
std::fprintf(stderr, "strata-q36: prompt experts on %s\n", use_mmq ? "MMQ (int8)" : "the native grouped kernels");
|
||||
{
|
||||
std::string gerr;
|
||||
int64_t big = 0;
|
||||
@@ -2010,44 +2064,72 @@ struct Engine {
|
||||
// the hybrid rows path: a batch of up to hybrid_t rows; when only the MTP block is hybrid, its every batch
|
||||
// (its prompt pass too: no staging buffers then)
|
||||
const bool hy_rows = lhy(li) && (T <= hybrid_t || !main_hybrid);
|
||||
const bool mmq_rows = !hy_rows && T > kGemmT && use_mmq;
|
||||
const bool mmq_rows = !hy_rows && T > kGemmT && use_pf;
|
||||
if (!(!mmq_rows && route_group(L, hy_rows ? hy[li].slot_of_d : nullptr, T, ext ? (int) g.n_expert + 1 : (int) g.n_expert)))
|
||||
K::native_router_topk(logits_r, ids, ewts, (int) g.n_expert, Kp, T, st);
|
||||
const int64_t n_ent = (int64_t) T * Kp;
|
||||
const int32_t* prow = nullptr;
|
||||
if (hy_rows) {
|
||||
moe_hybrid_gpu(L, li, T);
|
||||
} else if (T > kGemmT && use_mmq) {
|
||||
// a prompt chunk: the experts on llama.cpp's MMQ (int8, the weights read once per expert): the entries
|
||||
// sorted by expert on the device, gate+up as one [2 FF] product per expert, SwiGLU, down into rows the
|
||||
// combine reads through entry -> sorted row
|
||||
} else if (T > kGemmT && use_pf) {
|
||||
// a prompt chunk: each expert's weights read once for all its rows, on Strata's fused int8 kernels where they
|
||||
// cover the layer (fused_on), else on llama.cpp's MMQ. In the hybrid mode the layer's experts come streamed
|
||||
// (stream_take); the routing counts feed the slots' scores there
|
||||
const int ne = (int) g.n_expert;
|
||||
CK(cudaMemsetAsync(rcnt, 0, (size_t) ne * 4, st));
|
||||
k_route_count<<<grid1(n_ent), kThreads, 0, st>>>(ids, (int) n_ent, rcnt);
|
||||
k_route_scan<<<1, 32, 0, st>>>(rcnt, ne, rbounds, rcur, rmax);
|
||||
k_route_scatter<<<grid1(n_ent), kThreads, 0, st>>>(ids, (int) n_ent, Kp, rcur, ent_dst, ent_tok, rinv);
|
||||
CK(cudaMemcpyAsync(hpin + 10, rmax, 4, cudaMemcpyDeviceToHost, st));
|
||||
const bool fz = fused_on[li] != 0;
|
||||
int64_t maxr = 0;
|
||||
if (!fz || lhy(li)) {
|
||||
CK(cudaMemsetAsync(rcnt, 0, (size_t) ne * 4, st));
|
||||
k_route_count<<<grid1(n_ent), kThreads, 0, st>>>(ids, (int) n_ent, rcnt);
|
||||
}
|
||||
if (!fz) {
|
||||
// MMQ: the entries sorted by expert on the device, gate+up as one [2 FF] product per expert, SwiGLU, down
|
||||
// into rows the combine reads through entry -> sorted row
|
||||
k_route_scan<<<1, 32, 0, st>>>(rcnt, ne, rbounds, rcur, rmax);
|
||||
k_route_scatter<<<grid1(n_ent), kThreads, 0, st>>>(ids, (int) n_ent, Kp, rcur, ent_dst, ent_tok, rinv);
|
||||
CK(cudaMemcpyAsync(hpin + 10, rmax, 4, cudaMemcpyDeviceToHost, st));
|
||||
}
|
||||
if (lhy(li)) CK(cudaMemcpyAsync(h_cnt + li * g.n_expert, rcnt, (size_t) ne * 4, cudaMemcpyDeviceToHost, st));
|
||||
CK(cudaStreamSynchronize(st));
|
||||
if (lhy(li))
|
||||
for (int x = 0; x < ne; ++x) hy[li].score[(size_t) x] += h_cnt[li * g.n_expert + x];
|
||||
const int64_t maxr = hpin[10];
|
||||
if (!fz || lhy(li)) {
|
||||
CK(cudaStreamSynchronize(st));
|
||||
if (lhy(li))
|
||||
for (int x = 0; x < ne; ++x) hy[li].score[(size_t) x] += h_cnt[li * g.n_expert + x];
|
||||
maxr = hpin[10];
|
||||
}
|
||||
const uint8_t* wbase = lhy(li) ? stream_take((int) li) : L.blobs;
|
||||
MMQ::quantize(h, ent_tok, mq_x, L.L.gu_type, N, N, n_ent, st);
|
||||
MMQ::Product gu;
|
||||
gu.w = wbase; gu.type = L.L.gu_type; gu.w_rows = 2 * FF; gu.w_cols = N; gu.expert_bytes = L.stride;
|
||||
gu.n = ne; gu.xq = mq_x; gu.bounds = rbounds; gu.ids = ident; gu.total_rows = n_ent; gu.max_rows = maxr;
|
||||
gu.dst = mq_gu; gu.ld_dst = 2 * FF;
|
||||
mctx->run(gu, st);
|
||||
MMQ::swiglu(mq_gu, mq_h, n_ent, FF, false, st);
|
||||
MMQ::quantize(mq_h, nullptr, mq_hq, L.L.d_type, FF, FF, n_ent, st);
|
||||
MMQ::Product dn;
|
||||
dn.w = wbase + L.L.down_off; dn.type = L.L.d_type; dn.w_rows = N; dn.w_cols = FF;
|
||||
dn.expert_bytes = L.stride; dn.n = ne; dn.xq = mq_hq; dn.bounds = rbounds; dn.ids = ident;
|
||||
dn.total_rows = n_ent; dn.max_rows = maxr; dn.dst = eparts; dn.ld_dst = N;
|
||||
mctx->run(dn, st);
|
||||
if (fz) {
|
||||
#if !defined(__HIPCC__)
|
||||
// the layer's input to int8 once per token, the rows of each expert grouped on the device (no host wait),
|
||||
// then per batch of experts gate/up + SwiGLU + H to int8 + down into the entries' rows (f_slot)
|
||||
FQ::quantize_act_native(h, T, N, f_xq, st);
|
||||
PF::group(ids, n_ent, Kp, ne, f_scr, f_slot, f_src, st);
|
||||
const FQ::NativeGeom ng{L.L.gu_type, L.L.d_type, L.L.gu_row, L.L.d_row, L.L.up_off, L.L.down_off};
|
||||
for (int e = 0; e < ne; e += FQ::kQ36MaxBatch) {
|
||||
FQ::Batch b;
|
||||
b.e0 = e;
|
||||
b.e1 = std::min(ne, e + FQ::kQ36MaxBatch);
|
||||
for (int x = b.e0; x < b.e1; ++x) b.blob[x - b.e0] = wbase + (size_t) x * L.stride;
|
||||
FQ::experts_native(b, ng, ne, n_ent, f_scr, f_xq, f_src, f_ha, eparts, st);
|
||||
}
|
||||
prow = f_slot;
|
||||
#endif
|
||||
} else {
|
||||
MMQ::quantize(h, ent_tok, mq_x, L.L.gu_type, N, N, n_ent, st);
|
||||
MMQ::Product gu;
|
||||
gu.w = wbase; gu.type = L.L.gu_type; gu.w_rows = 2 * FF; gu.w_cols = N; gu.expert_bytes = L.stride;
|
||||
gu.n = ne; gu.xq = mq_x; gu.bounds = rbounds; gu.ids = ident; gu.total_rows = n_ent; gu.max_rows = maxr;
|
||||
gu.dst = mq_gu; gu.ld_dst = 2 * FF;
|
||||
mctx->run(gu, st);
|
||||
MMQ::swiglu(mq_gu, mq_h, n_ent, FF, false, st);
|
||||
MMQ::quantize(mq_h, nullptr, mq_hq, L.L.d_type, FF, FF, n_ent, st);
|
||||
MMQ::Product dn;
|
||||
dn.w = wbase + L.L.down_off; dn.type = L.L.d_type; dn.w_rows = N; dn.w_cols = FF;
|
||||
dn.expert_bytes = L.stride; dn.n = ne; dn.xq = mq_hq; dn.bounds = rbounds; dn.ids = ident;
|
||||
dn.total_rows = n_ent; dn.max_rows = maxr; dn.dst = eparts; dn.ld_dst = N;
|
||||
mctx->run(dn, st);
|
||||
prow = rinv;
|
||||
}
|
||||
if (lhy(li)) stream_done();
|
||||
prow = rinv;
|
||||
} else {
|
||||
group_entries(L, nullptr, T);
|
||||
if (!xq_post) quant(h, N, T, xq); // the experts' and the shared expert's input
|
||||
@@ -2081,7 +2163,7 @@ struct Engine {
|
||||
void forward(int T, int64_t p0, int heads, bool graph_mode, bool verify) {
|
||||
const auto& g = m->g;
|
||||
const int N = (int) g.n_embd;
|
||||
if (main_hybrid && T > hybrid_t && use_mmq) {
|
||||
if (main_hybrid && T > hybrid_t && use_pf) {
|
||||
std::vector<int> seq;
|
||||
for (size_t l = 0; l < m->layers.size(); ++l)
|
||||
if (lhy(l)) seq.push_back((int) l);
|
||||
@@ -2137,7 +2219,7 @@ struct Engine {
|
||||
void forward_mtp(int T, int64_t p0, const float* seeds, bool draft, bool graph_mode) {
|
||||
const auto& g = m->g;
|
||||
const int N = (int) g.n_embd;
|
||||
if (main_hybrid && lhy(m->layers.size()) && T > hybrid_t && use_mmq) stream_begin({(int) m->layers.size()});
|
||||
if (main_hybrid && lhy(m->layers.size()) && T > hybrid_t && use_pf) stream_begin({(int) m->layers.size()});
|
||||
K::iq_embed_rows(m->embd_type, m->embd, m->embd_row, tok_m, T, N, y, st);
|
||||
if (map_on) k_put_rows<<<T, kThreads, 0, st>>>(y, d_img, d_map, N);
|
||||
k_rms_ld<<<T, kThreads, 0, st>>>(y, N, m->mtp_enorm, mtp_cat, 2 * N, N, m->eps); // [e | ...]
|
||||
@@ -2895,8 +2977,8 @@ bool place_experts(Model& m, Engine& E, double vram_gib, int threads, int hybrid
|
||||
if (E.main_hybrid) E.use_graph = false; // the host takes part in every layer
|
||||
E.mtp_graph = false; // the MTP block's layer is hybrid either way
|
||||
E.hybrid_t = std::max(1, std::min(hybrid_t, E.use_mmq ? E.tmax : E.tmax));
|
||||
if (!E.use_mmq) E.hybrid_t = E.tmax; // no streamed prompt path: every batch on slots + the pool
|
||||
const size_t staging = E.use_mmq && E.main_hybrid ? 2 * (max_layer + kMmqTail) : 0;
|
||||
if (!E.use_pf) E.hybrid_t = E.tmax; // no streamed prompt path: every batch on slots + the pool
|
||||
const size_t staging = E.use_pf && E.main_hybrid ? 2 * (max_layer + kMmqTail) : 0;
|
||||
size_t slot_bytes = budget2 > staging ? budget2 - staging : 0;
|
||||
if (slot_bytes + staging + ((size_t) 256 << 20) > fr) slot_bytes = fr > staging + ((size_t) 512 << 20) ? fr - staging - ((size_t) 512 << 20) : 0;
|
||||
// --expert-slots: that many per layer (tests: the same split with and without the MTP block)
|
||||
@@ -3797,6 +3879,15 @@ int strata::qwen35::run(int argc, char** argv) {
|
||||
if (std::getenv("STRATA_Q8_PACKED") == nullptr) _putenv_s("STRATA_Q8_PACKED", "1"); // as on Linux (it was missing here)
|
||||
if (std::getenv("CUDA_MODULE_LOADING") == nullptr) _putenv_s("CUDA_MODULE_LOADING", "EAGER");
|
||||
#endif
|
||||
// Strata's fused prompt kernels for this family's formats (moe_fused_iq_q36.cu): the K-quant, Q6_K and Q8_0 gate/up
|
||||
// instances are opt-in for Flash-Next's packs; this engine sets them up by default (=0 keeps a format on MMQ)
|
||||
for (const char* v : {"STRATA_PF_FUSED_KQ", "STRATA_PF_FUSED_Q6K", "STRATA_PF_FUSED_Q8GU"}) {
|
||||
#if !defined(_WIN32)
|
||||
setenv(v, "1", 0);
|
||||
#else
|
||||
if (std::getenv(v) == nullptr) _putenv_s(v, "1");
|
||||
#endif
|
||||
}
|
||||
if (mtp_d < 0 || mtp_d > 7) { std::fprintf(stderr, "strata-q36: --mtp takes 0 .. 7 drafts\n"); return 2; }
|
||||
if (chunk < 0 || chunk > 4096 || ctx < 256) { std::fprintf(stderr, "strata-q36: bad --chunk / --max-context\n"); return 2; }
|
||||
Model m;
|
||||
|
||||
Reference in new issue
Block a user