E-9: the draft layer's prompt pass in batches through the prompt path's kernels (its Q8_0 matrices as Q8_1 x Q8_0 MMQ, the drafter's own integer dot products): 0.9 s -> 0.07 s of a 32K prompt, the same draft acceptance; STRATA_MTP_BATCH=0 switches back
This commit is contained in:
1 parent
d4e047660b
commit
a402eefdd1
6 files changed
+179
-4
No files matched your search
+2
-2
@@ -664,7 +664,7 @@ if(STRATA_NATIVE_EXPERTS)
|
||||
endif()
|
||||
set(_strata_mmq ${_strata_gg}/src/ggml-cuda)
|
||||
add_library(strata_mmq STATIC src/prefill/moe_mmq.cu src/prefill/ggml_cuda_host.cu ${_strata_mmq}/quantize.cu)
|
||||
foreach(_t q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs)
|
||||
foreach(_t q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs q8_0) # q8_0: the drafter's matrices (E-9)
|
||||
target_sources(strata_mmq PRIVATE ${_strata_mmq}/template-instances/mmq-instance-${_t}.cu)
|
||||
endforeach()
|
||||
target_include_directories(strata_mmq PRIVATE ${_strata_gg}/include ${_strata_gg}/src ${_strata_mmq}
|
||||
@@ -692,7 +692,7 @@ if(STRATA_NATIVE_EXPERTS)
|
||||
src/prefill/moe_mmq.cu
|
||||
src/prefill/ggml_cuda_host.cu
|
||||
"${_strata_mmq}/quantize.cu")
|
||||
foreach(_t q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs)
|
||||
foreach(_t q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs q8_0)
|
||||
list(APPEND _strata_mmq_sources "${_strata_mmq}/template-instances/mmq-instance-${_t}.cu")
|
||||
endforeach()
|
||||
set_source_files_properties(${_strata_mmq_sources} PROPERTIES LANGUAGE HIP)
|
||||
|
||||
@@ -73,6 +73,19 @@ public:
|
||||
double ms_draft = 0, ms_prefill = 0;
|
||||
int64_t rounds = 0;
|
||||
|
||||
/// E-9: the prompt path computes this layer's prompt K/V in batches (Prefill::draft_kv): its tensors, its K/V
|
||||
/// state, the first cell a round can still read, its device, and a wait for its own stream.
|
||||
const float* tensor_f32(const char* name) const { return f32(name); }
|
||||
const uint16_t* tensor_bf16(const char* name) const { return bf16(name); }
|
||||
const void* tensor_q8(const char* name) const { return q8(name); }
|
||||
QsaState& kv_state_rw() { return st_; }
|
||||
int64_t first_needed() const { return (window_ > 0 && prompt_len_ > 0) ? prompt_len_ - window_ - 64 : 0; }
|
||||
int device() const { return device_; }
|
||||
bool idle(std::string& err) {
|
||||
if (cs_ && cudaStreamSynchronize(cs_) != cudaSuccess) { err = "mtp: its stream failed"; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool record_forward(int T, int step_row0, cudaStream_t cs, std::string& err);
|
||||
bool capture_prefill(int T, std::string& err);
|
||||
|
||||
@@ -34,8 +34,19 @@ struct PrefillStats {
|
||||
double ms_ple = 0;
|
||||
};
|
||||
|
||||
} // namespace strata::prefill
|
||||
namespace strata::core { class MtpDrafter; }
|
||||
namespace strata::prefill {
|
||||
|
||||
class Prefill {
|
||||
public:
|
||||
/// E-9: the draft layer's K/V for prompt cells [cell0, cell0 + n) from their final residual rows `R_rows`
|
||||
/// (device) and `next_tokens` (host: the token at cell+1), in batches through this path's GEMMs and its idle
|
||||
/// scratch - call it from on_chunk. false with `err` empty: not applicable here (a ring or hybrid K/V, another
|
||||
/// device, too little scratch; STRATA_MTP_BATCH=0), the caller runs the drafter's own pass. Not bit-identical to
|
||||
/// that pass (FP16 GEMMs instead of Q8_1 activations): the drafts may differ, never the target's tokens' logits.
|
||||
bool draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0,
|
||||
std::string& err);
|
||||
Prefill();
|
||||
~Prefill();
|
||||
Prefill(const Prefill&) = delete;
|
||||
|
||||
@@ -93,6 +93,7 @@ bool supported(int t) {
|
||||
switch ((ggml_type) t) {
|
||||
case GGML_TYPE_Q2_0: case GGML_TYPE_IQ2_XXS: case GGML_TYPE_IQ2_XS: case GGML_TYPE_IQ2_S:
|
||||
case GGML_TYPE_IQ3_XXS: case GGML_TYPE_IQ3_S: case GGML_TYPE_IQ4_NL: case GGML_TYPE_IQ4_XS:
|
||||
case GGML_TYPE_Q8_0: // the draft layer's dense matrices (E-9)
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
@@ -141,6 +142,7 @@ void Context::run(const Product& p, void* stream) {
|
||||
case GGML_TYPE_IQ3_S: mul_mat_q_case<GGML_TYPE_IQ3_S>(ctx, a, s); break;
|
||||
case GGML_TYPE_IQ4_NL: mul_mat_q_case<GGML_TYPE_IQ4_NL>(ctx, a, s); break;
|
||||
case GGML_TYPE_IQ4_XS: mul_mat_q_case<GGML_TYPE_IQ4_XS>(ctx, a, s); break;
|
||||
case GGML_TYPE_Q8_0: mul_mat_q_case<GGML_TYPE_Q8_0>(ctx, a, s); break;
|
||||
default:
|
||||
std::fprintf(stderr, "prefill mmq: type %d is not covered\n", (int) t);
|
||||
std::exit(1);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/prefill/prefill.cpp - see include/strata/prefill/prefill.hpp.
|
||||
#include "strata/prefill/prefill.hpp"
|
||||
#include "strata/core/mtp.hpp"
|
||||
#include "strata/core/progress.hpp"
|
||||
#include "strata/core/on_device.hpp"
|
||||
|
||||
@@ -673,6 +674,151 @@ bool Prefill::relayout(int64_t chunk, void* borrow, uint64_t borrow_bytes, std::
|
||||
}
|
||||
|
||||
int64_t Prefill::chunk() const { return impl_->T; }
|
||||
|
||||
bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0,
|
||||
std::string& err) {
|
||||
Impl& m = *impl_;
|
||||
static const bool off = [] { const char* v = std::getenv("STRATA_MTP_BATCH"); return v != nullptr && v[0] == '0'; }();
|
||||
core::QsaState& st = mtp.kv_state_rw();
|
||||
if (off || n <= 0 || m.g == nullptr || m.region == nullptr || st.kv_mode != 0 || st.kv_hybrid ||
|
||||
mtp.device() != m.device)
|
||||
return false;
|
||||
const auto t0 = Clock::now();
|
||||
const core::ModelGeometry& g = *m.g;
|
||||
const int64_t Nn = g.n_embd, HCN = g.hc * g.n_embd, KV = g.n_head_kv * g.head_dim;
|
||||
constexpr int kQ8_0 = 8; // GGML_TYPE_Q8_0
|
||||
const float* w_ne = mtp.tensor_f32("pre_fc_norm_embedding.weight");
|
||||
const void* w_fe = mtp.tensor_q8("fc_embedding.weight");
|
||||
const float* w_nh = mtp.tensor_f32("pre_fc_norm_hidden.weight");
|
||||
const void* w_fh = mtp.tensor_q8("fc_hidden.weight");
|
||||
const float* w_hn = mtp.tensor_f32("attn_hyper_connection.hc_norm.weight");
|
||||
const uint16_t* w_dn = mtp.tensor_bf16("attn_hyper_connection.input_mix_weight_down.weight");
|
||||
const uint16_t* w_up = mtp.tensor_bf16("attn_hyper_connection.input_mix_weight_up.weight");
|
||||
const void* w_k = mtp.tensor_q8("self_attn.k_proj.weight");
|
||||
const void* w_v = mtp.tensor_q8("self_attn.v_proj.weight");
|
||||
const float* w_kn = mtp.tensor_f32("self_attn.k_norm.weight");
|
||||
if (!w_ne || !w_fe || !w_nh || !w_fh || !w_hn || !w_dn || !w_up || !w_k || !w_v || !w_kn) return false;
|
||||
const core::NativeEmbed* nemb = core::native_embed();
|
||||
const core::WeightRef* wemb = nemb ? nullptr : m.wt->find("token_embd.weight");
|
||||
if (!nemb && (wemb == nullptr || wemb->codebook_iq4nl || wemb->ne0 != g.n_embd || wemb->group_elems <= 0 ||
|
||||
(wemb->code_bits != 2 && wemb->code_bits != 4 && wemb->code_bits != 8)))
|
||||
return false;
|
||||
// the cells the drafter's window can still reach
|
||||
const int64_t r0 = std::max<int64_t>(0, mtp.first_needed() - cell0);
|
||||
if (r0 >= n) return true;
|
||||
// per row: emb/e2 (N), en16 (N half), hn/h2/Rm/gated (HCN), hn16/xn16 (HCN half), lo (LR) + lo16, grs, mixed (N) +
|
||||
// mixed_h, K and V (KV each), the token id
|
||||
// E-9: the drafter's Q8_0 matrices through Q8_1 x Q8_0 MMQ - its own pass's integer dot products (mmvq), so
|
||||
// its K/V stay close to what the drafter computes itself; STRATA_MTP_BATCH_F16=1: FP16 GEMMs (the A/B)
|
||||
static const bool f16_only = [] { const char* v = std::getenv("STRATA_MTP_BATCH_F16"); return v && v[0] == '1'; }();
|
||||
const bool q8 = !f16_only && mmq::built() && mmq::supported(kQ8_0);
|
||||
const uint64_t per_row = 4 * (2 * Nn + 4 * HCN + LR + HC + Nn + 2 * KV + 1) + 2 * (Nn + 2 * HCN + LR + Nn) + 64 +
|
||||
(q8 ? (uint64_t) mmq::q8_bytes(g.hc, Nn) + 4 * g.hc : 0);
|
||||
const int64_t B = std::min<int64_t>(n - r0, (int64_t) (m.region_bytes / per_row) & ~(int64_t) 63);
|
||||
if (B < 64) return false;
|
||||
uint8_t* q = m.region;
|
||||
auto carve = [&](size_t bytes) { void* p = q; q += (bytes + 255) & ~(size_t) 255; return p; };
|
||||
float* emb = (float*) carve((size_t) B * Nn * 4);
|
||||
float* e2 = (float*) carve((size_t) B * Nn * 4);
|
||||
uint16_t* en16 = (uint16_t*) carve((size_t) B * Nn * 2);
|
||||
float* hn = (float*) carve((size_t) B * HCN * 4);
|
||||
float* h2 = (float*) carve((size_t) B * HCN * 4);
|
||||
float* Rm = (float*) carve((size_t) B * HCN * 4);
|
||||
float* gated = (float*) carve((size_t) B * HCN * 4);
|
||||
uint16_t* hn16 = (uint16_t*) carve((size_t) B * HCN * 2);
|
||||
uint16_t* xn16 = (uint16_t*) carve((size_t) B * HCN * 2);
|
||||
float* lo = (float*) carve((size_t) B * LR * 4);
|
||||
uint16_t* lo16 = (uint16_t*) carve((size_t) B * LR * 2);
|
||||
float* grs = (float*) carve((size_t) B * HC * 4);
|
||||
float* mixed = (float*) carve((size_t) B * Nn * 4);
|
||||
uint16_t* mixed_h = (uint16_t*) carve((size_t) B * Nn * 2);
|
||||
float* Kc = (float*) carve((size_t) B * KV * 4);
|
||||
float* Vc = (float*) carve((size_t) B * KV * 4);
|
||||
int32_t* tok = (int32_t*) carve((size_t) B * 4);
|
||||
void* xq = q8 ? carve(mmq::q8_bytes(B * g.hc, Nn)) : nullptr;
|
||||
int32_t* ident = q8 ? (int32_t*) carve((size_t) B * g.hc * 4) : nullptr;
|
||||
int32_t* bnd = q8 ? (int32_t*) carve(16) : nullptr;
|
||||
if ((uint64_t) (q - m.region) > m.region_bytes) return false;
|
||||
if (!mtp.idle(err)) return false; // the drafter's own stream (its graph uploads) before this writes its K/V
|
||||
strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
|
||||
s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
|
||||
s.idx_dim = g.idx_key_dim;
|
||||
std::vector<int32_t> tk((size_t) B);
|
||||
if (q8) {
|
||||
if (!m.mmq_ctx) m.mmq_ctx = std::make_unique<mmq::Context>();
|
||||
mmq::iota(ident, B * g.hc, m.cs);
|
||||
}
|
||||
// y[rows, n_out] = x[rows, k] . w^T for a Q8_0 matrix: MMQ from the FP32 rows (bounds slot 0: rows, 1: rows*hc)
|
||||
auto proj = [&](const float* x, const uint16_t* x16, const void* w, float* y, int64_t rows, int64_t n_out,
|
||||
int64_t k, int slot) {
|
||||
if (!q8) { m.gemm.native(x16, kQ8_0, w, y, rows, n_out, k); return; }
|
||||
mmq::quantize(x, nullptr, xq, kQ8_0, k, k, rows, m.cs);
|
||||
mmq::Product p;
|
||||
p.w = w; p.type = kQ8_0; p.w_rows = n_out; p.w_cols = k; p.expert_bytes = mmq::matrix_bytes(kQ8_0, n_out, k);
|
||||
p.n = 1; p.xq = xq; p.bounds = bnd + 2 * slot; p.ids = ident; p.total_rows = rows; p.max_rows = rows;
|
||||
p.dst = y; p.ld_dst = n_out;
|
||||
m.mmq_ctx->run(p, m.cs);
|
||||
};
|
||||
for (int64_t b0 = r0; b0 < n; b0 += B) {
|
||||
const int64_t nb = std::min(B, n - b0), c0 = cell0 + b0;
|
||||
for (int64_t i = 0; i < nb; ++i) tk[(size_t) i] = next_tokens[b0 + i];
|
||||
const int32_t bh[4] = {0, (int32_t) nb, 0, (int32_t) (nb * g.hc)};
|
||||
if (q8 && cudaMemcpyAsync(bnd, bh, sizeof bh, cudaMemcpyHostToDevice, m.cs) != cudaSuccess) {
|
||||
err = "prefill: the draft bounds' upload failed";
|
||||
return false;
|
||||
}
|
||||
if (cudaMemcpyAsync(tok, tk.data(), (size_t) nb * 4, cudaMemcpyHostToDevice, m.cs) != cudaSuccess) {
|
||||
err = "prefill: the draft tokens' upload failed";
|
||||
return false;
|
||||
}
|
||||
// the input branches: the next token's embedding, and this cell's final residual rows
|
||||
if (nemb) {
|
||||
nemb->gather_dev(tok, nb, emb, m.cs);
|
||||
} else {
|
||||
const auto* codes = (const uint8_t*) wemb->data;
|
||||
const auto* scales = (const float*) (codes + wemb->codes_bytes);
|
||||
const auto* offsets = wemb->has_offset ? (const float*) (codes + wemb->codes_bytes + wemb->scales_bytes)
|
||||
: nullptr;
|
||||
strata::kernels::embedding_gather_dev(codes, scales, offsets, tok, (int) nb, wemb->ne0, wemb->code_bits,
|
||||
wemb->code_bias, wemb->group_elems,
|
||||
(uint64_t) (wemb->ne0 / (8 / wemb->code_bits)),
|
||||
(uint64_t) (wemb->ne0 / wemb->group_elems), emb, m.cs);
|
||||
}
|
||||
rms_rows(emb, w_ne, nb, Nn, Nn, EPS, m.cs);
|
||||
if (!q8) to_f16(emb, en16, nb * Nn, m.cs);
|
||||
proj(emb, en16, w_fe, e2, nb, Nn, Nn, 0);
|
||||
cudaMemcpyAsync(hn, R_rows + (size_t) b0 * HCN, (size_t) nb * HCN * 4, cudaMemcpyDeviceToDevice, m.cs);
|
||||
rms_rows(hn, w_nh, nb, HCN, HCN, EPS, m.cs);
|
||||
if (!q8) to_f16(hn, hn16, nb * HCN, m.cs);
|
||||
proj(hn, hn16, w_fh, h2, nb * g.hc, Nn, Nn, 1); // every stream through fc_hidden
|
||||
strata::kernels::add_streams_broadcast(h2, e2, Rm, Nn, (int) g.hc, (int) nb, m.cs);
|
||||
// the attention hyper-connection's read (its mixed input only: this pass writes nothing back)
|
||||
gr_norm_rs(Rm, w_hn, EPS, grs, xn16, nb, m.cs);
|
||||
m.gemm.bf16(xn16, w_dn, lo, nb, LR, HCN);
|
||||
gr_silu(lo, lo16, nb, m.cs);
|
||||
m.gemm.bf16(lo16, w_up, gated, nb, HCN, LR);
|
||||
gr_mix_r(Rm, grs, w_hn, gated, mixed, nullptr, nb, m.cs, mixed_h);
|
||||
// K and V into the drafter's cache, as the prompt path's QSA layers append theirs
|
||||
proj(mixed, mixed_h, w_k, Kc, nb, KV, Nn, 0);
|
||||
proj(mixed, mixed_h, w_v, Vc, nb, KV, Nn, 0);
|
||||
rms_rows(Kc, w_kn, nb * g.n_head_kv, g.head_dim, g.head_dim, EPS, m.cs);
|
||||
rope(Kc, nb, g.n_head_kv, g.head_dim, KV, c0, (float) strata::kernels::qsa_freq_base(), m.cs);
|
||||
if (st.kv_q4) {
|
||||
strata::kernels::fwht256_inplace_cuda(Kc, nb * g.n_head_kv, m.cs);
|
||||
strata::kernels::fwht256_inplace_cuda(Vc, nb * g.n_head_kv, m.cs);
|
||||
strata::kernels::kv_append_q4(st.k_q4, st.v_q4, st.page_table, c0, nb, Kc, Vc, s, m.cs, &st.host);
|
||||
} else {
|
||||
kv_append(Kc, Vc, nb, c0, st.page_table, s.page_size, st.kv_int8 ? nullptr : st.k_pool,
|
||||
st.kv_int8 ? nullptr : st.v_pool, st.k_q, st.v_q, st.k_scale, st.v_scale, m.cs, &st.host);
|
||||
}
|
||||
}
|
||||
if (cudaStreamSynchronize(m.cs) != cudaSuccess) {
|
||||
err = std::string("prefill: the draft layer's K/V: ") + cudaGetErrorString(cudaGetLastError());
|
||||
return false;
|
||||
}
|
||||
mtp.ms_prefill += ms_since(t0);
|
||||
return true;
|
||||
}
|
||||
void Prefill::set_pinned_share(double share) { g_pinned_share = share; }
|
||||
double Prefill::pinned_share() { return g_pinned_share; }
|
||||
|
||||
|
||||
@@ -3266,7 +3266,9 @@ int main(int argc, char** argv) {
|
||||
sp.on_chunk = [&](const float* R_rows, int64_t T, int64_t p0, std::string& e) -> bool {
|
||||
std::vector<int32_t> nxt((size_t) T);
|
||||
for (int64_t t = 0; t < T; ++t) nxt[(size_t) t] = (int32_t) cur[(size_t) (p0 + t + 1)];
|
||||
if (!mtp.prefill(R_rows, nxt.data(), T, p0, e)) return false;
|
||||
// E-9: batched through the prompt path when it can (one GPU: a layer split's drafter is on the last stage)
|
||||
const bool batched = !multi_gpu && sp.draft_kv(mtp, R_rows, nxt.data(), T, p0, e);
|
||||
if (!e.empty() || (!batched && !mtp.prefill(R_rows, nxt.data(), T, p0, e))) return false;
|
||||
// progress for the server window: PP <position reached> <prompt tokens> <ms> <fresh tokens/s>
|
||||
const int64_t done = p0 + T;
|
||||
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - pp_t0).count();
|
||||
@@ -4371,7 +4373,8 @@ int main(int argc, char** argv) {
|
||||
// cell i pairs R_i with the token at i + 1 (every such token is in the prompt)
|
||||
std::vector<int32_t> nxt((size_t) T);
|
||||
for (int64_t t = 0; t < T; ++t) nxt[(size_t) t] = (int32_t) o.tokens[(size_t) (p0 + t + 1)];
|
||||
return mtp.prefill(R_rows, nxt.data(), T, p0, e);
|
||||
if (prefill.draft_kv(mtp, R_rows, nxt.data(), T, p0, e)) return true; // E-9
|
||||
return e.empty() && mtp.prefill(R_rows, nxt.data(), T, p0, e);
|
||||
};
|
||||
}
|
||||
const Clock::time_point tp0 = Clock::now();
|
||||
|
||||
Reference in new issue
Block a user