mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
strata-q4-lite: the speculative-window attention path knows the quantized KV formats (--kv int8/fp8/k8v4/q4_0 wrote through null fp16 pools: illegal memory access in the next kernel)
This commit is contained in:
1 parent
3e538294b5
commit
e79432dd9c
2 files changed
+16
-11
No files matched your search
@@ -155,27 +155,29 @@ size_t kv_pool_bytes(const AttnShape& s, size_t pages, int fmt) {
|
||||
return 2 * al16(cells * s.hd * 2);
|
||||
}
|
||||
|
||||
// KV append of N tokens (host positions: the step records of sc.steps are already uploaded) or of the one token of the device step record `d_steps`
|
||||
void kv_append_fmt(const AttnShape& s, const AttnState& st, AttnScratch& sc, const float* k, const float* v, int N, const int32_t* d_steps, cudaStream_t stream) {
|
||||
// KV append of N tokens whose step records (kStepCount ints each, position in kStepPos, on the device) are `steps`: N == 1 is the decode token (the *_step kernels), N > 1 a
|
||||
// batch / speculative window (the *_steps kernels). Quantized formats only (fp16 has its own append kernels at the call sites).
|
||||
void kv_append_fmt(const AttnShape& s, const AttnState& st, AttnScratch& sc, const float* k, const float* v, int N, const int32_t* steps, cudaStream_t stream) {
|
||||
const K::QsaShapes q = qshapes(s);
|
||||
const bool one = d_steps != nullptr;
|
||||
const bool one = N == 1;
|
||||
const int32_t* d_steps = steps;
|
||||
if (st.kv_fmt == kKvInt8 || st.kv_fmt == kKvFp8) {
|
||||
if (one) K::kv_append_q8_step(st.k_q, st.v_q, st.k_scale, st.v_scale, st.page_table, d_steps, k, v, q, stream);
|
||||
else K::kv_append_q8_steps(st.k_q, st.v_q, st.k_scale, st.v_scale, st.page_table, sc.steps, K::kStepCount, k, v, s.kv_width(), N, q, stream, nullptr);
|
||||
else K::kv_append_q8_steps(st.k_q, st.v_q, st.k_scale, st.v_scale, st.page_table, steps, K::kStepCount, k, v, s.kv_width(), N, q, stream, nullptr);
|
||||
} else if (st.kv_fmt == kKvQ4) { // q4_0: K and V rotated (K in place in sc.k, V into sc.v_rot), the queries are rotated by the caller
|
||||
K::fwht256_cuda(k, sc.k, (int64_t) (one ? 1 : N) * s.nkv, stream);
|
||||
K::fwht256_cuda(v, sc.v_rot, (int64_t) (one ? 1 : N) * s.nkv, stream);
|
||||
if (one) K::kv_append_q4_step(st.k_q4, st.v_q4, st.page_table, d_steps, sc.k, sc.v_rot, q, stream);
|
||||
else K::kv_append_q4_steps(st.k_q4, st.v_q4, st.page_table, sc.steps, K::kStepCount, N, sc.k, sc.v_rot, q, stream);
|
||||
else K::kv_append_q4_steps(st.k_q4, st.v_q4, st.page_table, steps, K::kStepCount, N, sc.k, sc.v_rot, q, stream);
|
||||
} else { // k8v4: int8 K (unrotated, so the scores stay exact; both halves of the call write K's array) + rotated q4_0 V
|
||||
if (one) {
|
||||
K::kv_append_q8_step(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, d_steps, k, k, q, stream);
|
||||
K::fwht256_cuda(v, sc.v_rot, (int64_t) s.nkv, stream);
|
||||
K::kv_append_q4_step(st.v_q4, st.v_q4, st.page_table, d_steps, sc.v_rot, sc.v_rot, q, stream);
|
||||
} else {
|
||||
K::kv_append_q8_steps(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, sc.steps, K::kStepCount, k, k, s.kv_width(), N, q, stream, nullptr);
|
||||
K::kv_append_q8_steps(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, steps, K::kStepCount, k, k, s.kv_width(), N, q, stream, nullptr);
|
||||
K::fwht256_cuda(v, sc.v_rot, (int64_t) N * s.nkv, stream);
|
||||
K::kv_append_q4_steps(st.v_q4, st.v_q4, st.page_table, sc.steps, K::kStepCount, N, sc.v_rot, sc.v_rot, q, stream);
|
||||
K::kv_append_q4_steps(st.v_q4, st.v_q4, st.page_table, steps, K::kStepCount, N, sc.v_rot, sc.v_rot, q, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -409,7 +411,7 @@ bool attn_run(const AttnShape& s, const AttnWeights& w, const AttnState& st, Att
|
||||
kv_append_batch_kernel<<<(unsigned) (((size_t) N * s.kv_width() + 255) / 256), 256, 0, stream>>>(st.k_pool, st.v_pool, sc.k, v_proj, N, pos0, s.nkv, s.hd);
|
||||
} else {
|
||||
upload_steps(s, sc, N, pos0, stream);
|
||||
kv_append_fmt(s, st, sc, sc.k, v_proj, N, nullptr, stream);
|
||||
kv_append_fmt(s, st, sc, sc.k, v_proj, N, sc.steps, stream);
|
||||
}
|
||||
// 3. indexer append (a window keeps what it needs to be undone)
|
||||
if (vb) {
|
||||
@@ -575,7 +577,9 @@ bool attn_window_dev(const AttnShape& s, const AttnWeights& w, const AttnState&
|
||||
K::native_qsa_rms_norm_rope(k_proj, s.hd, w.k_g, sc.k, n * s.nkv, s.hd, s.n_rot, kEps, K::rope_scaling(), d.pos_k, stream);
|
||||
split_idx_kernel<<<(unsigned) (((size_t) n * (qw + s.ixd) + 255) / 256), 256, 0, stream>>>(idx_qk, sc.qi, sc.raw, n, qw, s.ixd);
|
||||
K::native_qsa_rms_norm_rope(sc.qi, s.ixd, w.ixq_g, sc.qi, n * s.ixh, s.ixd, s.n_rot, kEps, K::rope_scaling(), d.pos_i, stream);
|
||||
kv_append_win_kernel<<<(unsigned) (((size_t) n * s.kv_width() + 255) / 256), 256, 0, stream>>>(st.k_pool, st.v_pool, sc.k, v_proj, n, s.nkv, s.hd, d.steps);
|
||||
if (st.kv_fmt == kKvFp16) kv_append_win_kernel<<<(unsigned) (((size_t) n * s.kv_width() + 255) / 256), 256, 0, stream>>>(st.k_pool, st.v_pool, sc.k, v_proj, n, s.nkv, s.hd, d.steps);
|
||||
else kv_append_fmt(s, st, sc, sc.k, v_proj, n, d.steps, stream); // --kv int8 | fp8 | k8v4 | q4_0: the window's cells go through the format's own append (device step records)
|
||||
if (st.kv_fmt == kKvQ4) K::fwht256_inplace_cuda(sc.q, (int64_t) n * s.nh, stream); // <Hq, Hk> = <q, k>
|
||||
cudaMemcpyAsync(vb.tail_save, st.ix_tail, (size_t) kTail * s.ixd * 4, cudaMemcpyDeviceToDevice, stream);
|
||||
cudaMemcpyAsync(vb.raw, sc.raw, (size_t) n * s.ixd * 4, cudaMemcpyDeviceToDevice, stream);
|
||||
const K::QsaIndexerBuffers ib{st.ix_tail, st.ix_dead, st.ix_pooled, st.ix_block_pos};
|
||||
@@ -583,6 +587,7 @@ bool attn_window_dev(const AttnShape& s, const AttnWeights& w, const AttnState&
|
||||
K::qsa_block_scores(st.ix_pooled, st.ix_dead, sc.qi, d.steps, n, sc.max_blocks, q, sc.sel_scores, stream); // capacity grid (active = -1)
|
||||
K::qsa_block_topk(sc.sel_scores, d.steps, n, sc.max_blocks, sc.cap, q, sc.sel_ids, stream);
|
||||
K::qsa_decode_attn_batch(sc.q, pools_of(st), sc.sel_ids, d.steps, sc.cap, q, sc.attn_scratch, sc.attn, n, stream);
|
||||
if (st.kv_fmt == kKvK8V4 || st.kv_fmt == kKvQ4) K::fwht256_inplace_cuda(sc.attn, (int64_t) n * s.nh, stream); // the values are stored rotated: rotate the mix back
|
||||
K::native_qsa_gate_apply(sc.attn, q_proj, out, n * s.nh, s.hd, stream);
|
||||
} catch (const std::exception& e) {
|
||||
err = std::string("attn_window_dev: ") + e.what();
|
||||
|
||||
@@ -324,7 +324,7 @@ public:
|
||||
bool attn_window(int il, const float* x, const float* xr, int n, float* y, cudaStream_t s, std::string& err) override {
|
||||
namespace L = strata::ling32::gpu;
|
||||
A& a = attn_[il];
|
||||
if (!a.st.k_pool) { err = "attn_window: layer " + std::to_string(il) + " is not an attention layer"; return false; }
|
||||
if (!attn_state_present(a.st)) { err = "attn_window: layer " + std::to_string(il) + " is not an attention layer"; return false; }
|
||||
const AttnLayer& W = m_->layers[il].attn;
|
||||
const Q4Cfg& c = m_->cfg;
|
||||
(void) x;
|
||||
@@ -341,7 +341,7 @@ public:
|
||||
bool commit_window(int n, const int32_t* keep_dev, cudaStream_t s, std::string& err) override {
|
||||
for (size_t il = 0; il < gdn_.size(); ++il) {
|
||||
if (gdn_[il].st.S) { if (!gdn_commit_dev(gs_, gdn_[il].w, gdn_[il].st, gsc_, gdn_[il].vb, n, keep_dev, s, err)) return false; }
|
||||
else if (attn_[il].st.k_pool) { if (!attn_window_commit_dev(as_, attn_[il].w, attn_[il].st, win_, attn_[il].vb, n, keep_dev, s, err)) return false; }
|
||||
else if (attn_state_present(attn_[il].st)) { if (!attn_window_commit_dev(as_, attn_[il].w, attn_[il].st, win_, attn_[il].vb, n, keep_dev, s, err)) return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user