Merge PR #559 (blange48, crazyaimachine): batch slots, pipelined layer split, parking with a split
Conflicts with 0.1.39: --trim-stage-weights is now the same switch as STRATA_STAGE_TRIM=1 (PR #639's implementation, already in 0.1.39); the #533 VRAM command is taken between batch windows only when no slot is decoding.
This commit is contained in:
commit
89351a799d
17 files changed
+2021
-106
No files matched your search
+493
-47
@@ -277,6 +277,12 @@ Verifier::~Verifier() {
|
||||
for (auto& e : exec_)
|
||||
if (e) cudaGraphExecDestroy(e);
|
||||
if (commit_exec_) cudaGraphExecDestroy(commit_exec_);
|
||||
for (auto& kv : exec_bm_)
|
||||
if (kv.second) cudaGraphExecDestroy(kv.second);
|
||||
for (auto& kv : commit_bm_)
|
||||
if (kv.second) cudaGraphExecDestroy(kv.second);
|
||||
if (arena_b_) cudaFree(arena_b_);
|
||||
if (h_commitb_) cudaFreeHost(h_commitb_);
|
||||
if (cs_) cudaStreamDestroy(cs_);
|
||||
if (copy_) { cudaStreamSynchronize(copy_); cudaStreamDestroy(copy_); }
|
||||
if (commit_done_) cudaEventDestroy(commit_done_);
|
||||
@@ -521,14 +527,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const int64_t TS = (s.idx_block - 1) * ID;
|
||||
const bool ple_on = ss.ple.ready() && ple_stage();
|
||||
auto Rt = [&](int t) { return R_ + (size_t) t * HC * N; };
|
||||
const int G = (split_ && T >= 2) ? 2 : 1;
|
||||
const int G = (split_ && T >= 2 && !batch_rec_) ? 2 : 1; // a batch window is one group
|
||||
static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
|
||||
auto stamp = [&](int64_t l, int i, int grp) {
|
||||
if (prof_on_ && grp == 0) gpu_stamp(prof_, (int) (l * kProfPer + i), cs);
|
||||
if (trace_m_ != nullptr) gpu_stamp(trace_m_, (int) ((l * kProfPer + i) * 2 + grp), cs); // #649
|
||||
};
|
||||
const int tb_[2] = {0, (T + 1) / 2}, te_[2] = {G == 2 ? (T + 1) / 2 : T, T};
|
||||
groups_[T] = G;
|
||||
if (!batch_rec_) groups_[T] = G;
|
||||
const int64_t nQall = g.n_qsa_layers();
|
||||
// a batch window: row t is slot t, whose state lives in its own session
|
||||
auto slot_ss = [&](int t) -> SessionState& { return batch_rec_ ? *slots_[(size_t) (row_base_ + t)] : ss; };
|
||||
const int hrow0 = batch_rec_ ? row_base_ : 0; // a slot group's own hand-off rows
|
||||
|
||||
// ---- the window's inputs, from mapped staging
|
||||
copy_i32_from_mapped(tok_, m_tok_, T, cs);
|
||||
@@ -544,9 +554,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const int64_t HB = Verifier::handoff_floats(g);
|
||||
if (lb_ > 0) {
|
||||
for (int t = 0; t < T; ++t) {
|
||||
copy_from_mapped(Rt(t), hand_in_ + (size_t) t * HB, HC * N, cs);
|
||||
copy_from_mapped(bo_ + (size_t) t * N, hand_in_ + (size_t) t * HB + HC * N, N, cs);
|
||||
copy_from_mapped(inj2_ + (size_t) t * HC, hand_in_ + (size_t) t * HB + HC * N + N, HC, cs);
|
||||
const size_t hr = (size_t) (hrow0 + t) * HB;
|
||||
copy_from_mapped(Rt(t), hand_in_ + hr, HC * N, cs);
|
||||
copy_from_mapped(bo_ + (size_t) t * N, hand_in_ + hr + HC * N, N, cs);
|
||||
copy_from_mapped(inj2_ + (size_t) t * HC, hand_in_ + hr + HC * N + N, HC, cs);
|
||||
}
|
||||
} else if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table
|
||||
ne->gather_dev(tok_, T, emb_, cs);
|
||||
@@ -601,14 +612,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
PleOut po;
|
||||
po.normalized = normalized;
|
||||
po.result = Rt(t);
|
||||
float* hist = batch_rec_ ? slot_ss(t).ple_hist : ss.ple.hist; // the row's own history
|
||||
try {
|
||||
ple_block(ple_ + t * N, Rt(t), ss.ple.hist, ss.ple.w, po, ss.ple.scratch, cs);
|
||||
ple_history_advance(ss.ple.hist, normalized, cs);
|
||||
ple_block(ple_ + t * N, Rt(t), hist, ss.ple.w, po, ss.ple.scratch, cs);
|
||||
ple_history_advance(hist, normalized, cs);
|
||||
} catch (const std::exception& e) {
|
||||
err = std::string("verify PLE: ") + e.what();
|
||||
return false;
|
||||
}
|
||||
copy_from_mapped(hist_snap_ + (size_t) t * HS, ss.ple.hist, HS, cs);
|
||||
copy_from_mapped(hist_snap_ + (size_t) t * HS, hist, HS, cs);
|
||||
}
|
||||
pending = false;
|
||||
}
|
||||
@@ -651,6 +663,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
native_quantize_q8_1(xm, xq_, (int) N, n, cs);
|
||||
native_mmvq(wqkv->native_type, wqkv->native_data, xq_, qkv + (size_t) tb * C, (int) N, (int) C, n, cs);
|
||||
stamp(l, 2, grp);
|
||||
if (batch_rec_) { // each row from its own slot's conv history, one row each
|
||||
for (int t = tb; t < te; ++t) {
|
||||
SessionState& sx = slot_ss(t);
|
||||
const float* cx = sx.gdn_state + (size_t) (gi - sx.gdn_ord0) * gdn_floats +
|
||||
(uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
|
||||
gdn_conv_l2_multi(cx, qkv + (size_t) t * C, (const float*) wc->data, hb + (size_t) t * C, (int) C,
|
||||
(int) (2 * HK), EPS, 1, cs, 0);
|
||||
}
|
||||
} else
|
||||
gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb);
|
||||
stamp(l, 3, grp);
|
||||
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
|
||||
@@ -660,6 +681,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
native_mmvq(wg->native_type, wg->native_data, xq_, z_ + (size_t) tb * ZV, (int) N, (int) ZV, n, cs);
|
||||
stamp(l, 5, grp);
|
||||
// the recurrence from the untouched state over tokens [0, te); outputs only for this group's
|
||||
if (batch_rec_) { // each row's recurrence from its own slot's state, one token
|
||||
for (int t = tb; t < te; ++t) {
|
||||
SessionState& sx = slot_ss(t);
|
||||
float* stx = sx.gdn_state + (size_t) (gi - sx.gdn_ord0) * gdn_floats;
|
||||
gdn_step_norm_multi(stx, hb + (size_t) t * C, (int) C, gate + (size_t) t * HV, beta + (size_t) t * HV,
|
||||
z_ + (size_t) t * ZV, (const float*) wnm->data, EPS, y_ + (size_t) t * ZV,
|
||||
(int) HK, (int) HV, 1, nullptr, cs, 0);
|
||||
}
|
||||
} else
|
||||
gdn_step_norm_multi(state, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, y_, (int) HK,
|
||||
(int) HV, te, nullptr, cs, tb);
|
||||
stamp(l, 6, grp);
|
||||
@@ -704,8 +734,14 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
fwht256_inplace_cuda(vcur_ + tb * NKV * HD, (int64_t) n * NKV, cs);
|
||||
}
|
||||
stamp(l, 8, grp);
|
||||
if (batch_rec_) { // every slot's indexer tail, restored by its commit
|
||||
for (int t = tb; t < te; ++t)
|
||||
copy_from_mapped(tail_snap_b_ + ((size_t) (row_base_ + t) * nQall + qi) * TS, slot_ss(t).qsa_states[qi].idx_tail,
|
||||
TS, cs);
|
||||
} else
|
||||
if (grp == 0) copy_from_mapped(tail_snap_ + (size_t) qi * TS, st.idx_tail, TS, cs);
|
||||
for (int t = tb; t < te; ++t) {
|
||||
const QsaState& st = slot_ss(t).qsa_states[qi]; // the row's own K/V (ss's outside a batch)
|
||||
const int32_t* step_t = step_ + t * kStepCount;
|
||||
if (st.kv_hybrid) { // K8V4: the unused half's lanes folded onto the used pool (layer.cpp)
|
||||
kv_append_q8_step(st.k_q, st.k_q, st.k_scale, st.k_scale, st.page_table, step_t,
|
||||
@@ -722,11 +758,13 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
kv_append_step(st.k_pool, st.v_pool, st.page_table, step_t, kcur_ + t * NKV * HD,
|
||||
vcur_ + t * NKV * HD, s, cs, &st.host);
|
||||
}
|
||||
const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos};
|
||||
for (int t = tb; t < te; ++t)
|
||||
for (int t = tb; t < te; ++t) {
|
||||
const QsaState& sx = slot_ss(t).qsa_states[qi];
|
||||
const QsaIndexerBuffers ib{sx.idx_tail, sx.idx_dead, sx.idx_pooled, sx.idx_block_pos};
|
||||
native_qsa_indexer_append(idx_raw + t * ID, step_ + t * kStepCount + kStepPos, 0,
|
||||
(const float*) wikn->data, EPS, ib, s, st.max_cells,
|
||||
(const float*) wikn->data, EPS, ib, s, sx.max_cells,
|
||||
rope_scaling(), cs);
|
||||
}
|
||||
stamp(l, 9, grp);
|
||||
native_mmvq(wq->native_type, wq->native_data, xq_, qfull_ + tb * NH * 2 * HD, (int) N, (int) (NH * 2 * HD),
|
||||
n, cs);
|
||||
@@ -759,6 +797,20 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
}
|
||||
}
|
||||
stamp(l, 10, grp);
|
||||
if (batch_rec_) { // each row selects and attends over its own slot's K/V
|
||||
for (int t = tb; t < te; ++t) {
|
||||
const QsaState& sx = slot_ss(t).qsa_states[qi];
|
||||
qsa_block_scores(sx.idx_pooled, sx.idx_dead, qidx_ + t * IQ * ID, step_ + t * kStepCount, 1,
|
||||
max_blocks_, s, scores_ + (size_t) t * max_blocks_, cs);
|
||||
qsa_block_topk(scores_ + (size_t) t * max_blocks_, step_ + t * kStepCount, 1, max_blocks_, cap_, s,
|
||||
sel_ + (size_t) t * cap_, cs);
|
||||
qsa_kv_resolve(sx, *g_, sel_ + (size_t) t * cap_, step_ + t * kStepCount, 1, cap_, cs);
|
||||
const QsaAttnPools px = qsa_attn_pools(sx);
|
||||
qsa_decode_attn_batch(qcur_ + t * NH * HD, px, sel_ + (size_t) t * cap_, step_ + t * kStepCount,
|
||||
cap_, s, attn_scratch_ + (size_t) t * attn_scratch_floats_,
|
||||
attn_ + t * NH * HD, 1, cs);
|
||||
}
|
||||
} else {
|
||||
qsa_block_scores(st.idx_pooled, st.idx_dead, qidx_ + tb * IQ * ID, step_ + tb * kStepCount, n, max_blocks_,
|
||||
s, scores_ + (size_t) tb * max_blocks_, cs);
|
||||
qsa_block_topk(scores_ + (size_t) tb * max_blocks_, step_ + tb * kStepCount, n, max_blocks_, cap_, s,
|
||||
@@ -770,6 +822,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const QsaAttnPools pools = qsa_attn_pools(st);
|
||||
qsa_decode_attn_batch(qcur_ + tb * NH * HD, pools, sel_ + (size_t) tb * cap_, step_ + tb * kStepCount, cap_,
|
||||
s, attn_scratch_ + (size_t) tb * attn_scratch_floats_, attn_ + tb * NH * HD, n, cs);
|
||||
}
|
||||
stamp(l, 13, grp);
|
||||
if (st.kv_rot || st.kv_hybrid) fwht256_inplace_cuda(attn_ + tb * NH * HD, (int64_t) n * NH, cs); // back: H^-1 = H
|
||||
if (qb) native_qsa_gate_apply(attn_ + tb * NH * HD, qfull_ + tb * NH * 2 * HD, attn32_ + tb * NH * HD,
|
||||
@@ -947,9 +1000,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
}
|
||||
if (le_ < g.n_layers) { // a layer split's earlier stage: hand the residual on, no head
|
||||
for (int t = 0; t < T; ++t) {
|
||||
copy_from_mapped(hand_out_ + (size_t) t * HB, Rt(t), HC * N, cs);
|
||||
copy_from_mapped(hand_out_ + (size_t) t * HB + HC * N, bo_ + (size_t) t * N, N, cs);
|
||||
copy_from_mapped(hand_out_ + (size_t) t * HB + HC * N + N, inj2_ + (size_t) t * HC, HC, cs);
|
||||
const size_t hr = (size_t) (hrow0 + t) * HB;
|
||||
copy_from_mapped(hand_out_ + hr, Rt(t), HC * N, cs);
|
||||
copy_from_mapped(hand_out_ + hr + HC * N, bo_ + (size_t) t * N, N, cs);
|
||||
copy_from_mapped(hand_out_ + hr + HC * N + N, inj2_ + (size_t) t * HC, HC, cs);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1146,10 +1200,45 @@ bool Verifier::capture_commit(std::string& err) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Verifier::collect_profile() {
|
||||
const ModelGeometry& g = *g_;
|
||||
cudaMemcpy(prof_h_.data(), prof_, prof_h_.size() * 8, cudaMemcpyDeviceToHost);
|
||||
const int64_t L = g.n_layers;
|
||||
auto at = [&](int64_t l, int i) { return prof_h_[(size_t) (l * kProfPer + i)]; };
|
||||
// D8: the derived columns below read stamps the hc-read kernels write themselves or the next
|
||||
// layer's first. A slot no kernel stamped is 0 and its unsigned difference wrapped to ~1e19 ns -
|
||||
// which is why (gap)/head/hc0 printed ~1e14 ms per window. A missing or out-of-order stamp now
|
||||
// contributes nothing.
|
||||
const auto gap = [](unsigned long long to, unsigned long long from) {
|
||||
return (from != 0 && to != 0 && to >= from) ? (double) (to - from) : 0.0;
|
||||
};
|
||||
for (int64_t l = 0; l < L; ++l) {
|
||||
const int kind = is_qsa_layer(g, l) ? 1 : 0;
|
||||
unsigned long long prev = at(l, 0);
|
||||
for (int i = 1; i <= 24; ++i) {
|
||||
const unsigned long long x = at(l, i);
|
||||
if (x == 0 || x < prev) continue;
|
||||
prof_sum_[kind][i] += (double) (x - prev);
|
||||
prev = x;
|
||||
}
|
||||
if (l + 1 < L) prof_sum_[kind][25] += gap(at(l + 1, 0), at(l, 24));
|
||||
const double dn = gap(at(l, 27), at(l, 0)), dd = gap(at(l, 28), at(l, 27)), du = gap(at(l, 1), at(l, 28));
|
||||
if (dn > 0 && dd > 0 && du > 0) { // the split exists: show it split, not twice
|
||||
prof_sum_[kind][27] += dn; // hc-read0: norm
|
||||
prof_sum_[kind][28] += dd; // down
|
||||
prof_sum_[kind][29] += du; // up (through both halves, as before)
|
||||
prof_sum_[kind][1] -= gap(at(l, 1), at(l, 0)); // (hc-read0 shown split)
|
||||
}
|
||||
}
|
||||
prof_sum_[0][26] += gap(at(L, 1), at(L, 0));
|
||||
++prof_windows_;
|
||||
}
|
||||
|
||||
bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out,
|
||||
std::string& err) {
|
||||
using namespace strata::kernels;
|
||||
const OnDevice on_device(device_);
|
||||
last_batch_ = false;
|
||||
if (T < 1 || T > max_t_) { err = "verify: window size out of range"; return false; }
|
||||
if (released_.load()) { err = "verify: an earlier window never finished on the GPU (#267); restart the engine"; return false; }
|
||||
const ModelGeometry& g = *g_;
|
||||
@@ -1280,38 +1369,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
if (se != cudaSuccess) { err = std::string("verify: ") + cudaGetErrorString(se); return false; }
|
||||
progress_at("verify window: waiting for the expert copies", (int64_t) T);
|
||||
cudaStreamSynchronize(copy_); // no host function of this window may raise flag B in the next one
|
||||
if (prof_on_ && G == 1) { // the window's GPU stage stamps
|
||||
cudaMemcpy(prof_h_.data(), prof_, prof_h_.size() * 8, cudaMemcpyDeviceToHost);
|
||||
const int64_t L = g.n_layers;
|
||||
auto at = [&](int64_t l, int i) { return prof_h_[(size_t) (l * kProfPer + i)]; };
|
||||
// D8: the derived columns below read stamps the hc-read kernels write themselves or the next
|
||||
// layer's first. A slot no kernel stamped is 0 and its unsigned difference wrapped to ~1e19 ns -
|
||||
// which is why (gap)/head/hc0 printed ~1e14 ms per window. A missing or out-of-order stamp now
|
||||
// contributes nothing.
|
||||
const auto gap = [](unsigned long long to, unsigned long long from) {
|
||||
return (from != 0 && to != 0 && to >= from) ? (double) (to - from) : 0.0;
|
||||
};
|
||||
for (int64_t l = 0; l < L; ++l) {
|
||||
const int kind = is_qsa_layer(g, l) ? 1 : 0;
|
||||
unsigned long long prev = at(l, 0);
|
||||
for (int i = 1; i <= 24; ++i) {
|
||||
const unsigned long long x = at(l, i);
|
||||
if (x == 0 || x < prev) continue;
|
||||
prof_sum_[kind][i] += (double) (x - prev);
|
||||
prev = x;
|
||||
}
|
||||
if (l + 1 < L) prof_sum_[kind][25] += gap(at(l + 1, 0), at(l, 24));
|
||||
const double dn = gap(at(l, 27), at(l, 0)), dd = gap(at(l, 28), at(l, 27)), du = gap(at(l, 1), at(l, 28));
|
||||
if (dn > 0 && dd > 0 && du > 0) { // the split exists: show it split, not twice
|
||||
prof_sum_[kind][27] += dn; // hc-read0: norm
|
||||
prof_sum_[kind][28] += dd; // down
|
||||
prof_sum_[kind][29] += du; // up (through both halves, as before)
|
||||
prof_sum_[kind][1] -= gap(at(l, 1), at(l, 0)); // (hc-read0 shown split)
|
||||
}
|
||||
}
|
||||
prof_sum_[0][26] += gap(at(L, 1), at(L, 0));
|
||||
++prof_windows_;
|
||||
}
|
||||
if (prof_on_ && G == 1) collect_profile(); // the window's GPU stage stamps
|
||||
// ---- a sampled or penalized request: the head's sampling again, host-side so its parameters are this call's
|
||||
// own (a captured kernel would replay the same draws forever). Row t's draw is Philox(seed, pos0 + t): tied to
|
||||
// the POSITION it samples, not to how the text was cut into windows, so a seed replays the same text whatever
|
||||
@@ -1366,7 +1424,7 @@ void Verifier::set_plan_slot(int grp) {
|
||||
sink_.ptr = (unsigned long long*) (base + ptr_off);
|
||||
sink_.ptr2 = sink_.ptr + cap;
|
||||
sink_.start2 = base + ptr_off + 4 * cap;
|
||||
const int G = groups_[last_t_] > 0 ? groups_[last_t_] : 1;
|
||||
const int G = last_batch_ ? 1 : (groups_[last_t_] > 0 ? groups_[last_t_] : 1);
|
||||
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
|
||||
sink_.staging = (unsigned long long) (staging_ + (size_t) (grp * per) * strata::kernels::cpu::expert_layout().max_blob);
|
||||
sink_.staging_cap = per;
|
||||
@@ -1508,6 +1566,394 @@ bool Verifier::wait_commit(std::string& err) {
|
||||
return next_ == nullptr || next_->wait_commit(err);
|
||||
}
|
||||
|
||||
|
||||
// ================================ BATCH WINDOWS (see init_slots) ================================
|
||||
|
||||
bool Verifier::init_slots(const std::vector<SessionState*>& slots, std::string& err) {
|
||||
const OnDevice on_device(device_);
|
||||
if (g_ == nullptr || ss_ == nullptr) { err = "verify: init_slots before init"; return false; }
|
||||
if (slots.empty() || (int) slots.size() > max_t_) {
|
||||
err = "verify: init_slots needs 1.." + std::to_string(max_t_) + " sessions";
|
||||
return false;
|
||||
}
|
||||
for (SessionState* x : slots) {
|
||||
if (x == nullptr || x->layer_lo != ss_->layer_lo || x->layer_hi != ss_->layer_hi ||
|
||||
x->max_cells != ss_->max_cells || x->gdn_ord0 != ss_->gdn_ord0 || x->qsa_ord0 != ss_->qsa_ord0) {
|
||||
err = "verify: a slot's session is not carved like the stage's own (layer range, context)";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const strata::kernels::QsaShapes s = shapes_of(*g_);
|
||||
const int64_t S = (int64_t) slots.size(), CB = 2 + max_t_;
|
||||
const int64_t TS = (s.idx_block - 1) * g_->idx_key_dim, nQ = g_->n_qsa_layers();
|
||||
void* d = nullptr;
|
||||
if (!mapped((size_t) (S * CB * 4 + 16), (void**) &h_commitb_, (void**) &m_commitb_)) {
|
||||
err = "verify: the batch commit staging failed";
|
||||
return false;
|
||||
}
|
||||
const uint64_t a = ((uint64_t) S * CB * 4 + 255) & ~255ull;
|
||||
if (cudaMalloc(&d, a + (uint64_t) S * std::max<int64_t>(nQ, 1) * TS * 4) != cudaSuccess) {
|
||||
err = "verify: the batch buffers do not fit";
|
||||
return false;
|
||||
}
|
||||
arena_b_ = d;
|
||||
commitb_ = (int32_t*) d;
|
||||
tail_snap_b_ = (float*) ((uint8_t*) d + a);
|
||||
slots_ = slots;
|
||||
slot_sp_.assign(slots.size(), sampling_); // greedy until set_slot_sampling
|
||||
std::fprintf(stderr, "strata verify: batch windows of up to %lld sequences (layers [%lld, %lld))\n", (long long) S,
|
||||
(long long) lb_, (long long) le_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verifier::capture_batch(int base, int S, std::string& err) {
|
||||
cudaGraphExec_t& ex = exec_bm_[base * 16 + S];
|
||||
if (ex != nullptr) return true;
|
||||
if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) {
|
||||
err = "verify: begin batch capture failed";
|
||||
return false;
|
||||
}
|
||||
batch_rec_ = true;
|
||||
row_base_ = base;
|
||||
std::string rerr;
|
||||
const bool ok = record_window(S, cs_, rerr);
|
||||
batch_rec_ = false;
|
||||
row_base_ = 0;
|
||||
cudaGraph_t graph = nullptr;
|
||||
const cudaError_t ce = cudaStreamEndCapture(cs_, &graph);
|
||||
if (!ok || ce != cudaSuccess) {
|
||||
if (graph) cudaGraphDestroy(graph);
|
||||
err = !ok ? rerr : std::string("verify: end batch capture: ") + cudaGetErrorString(ce);
|
||||
return false;
|
||||
}
|
||||
const cudaError_t ie = cudaGraphInstantiate(&ex, graph, 0);
|
||||
cudaGraphDestroy(graph);
|
||||
if (ie != cudaSuccess) { err = std::string("verify: batch instantiate: ") + cudaGetErrorString(ie); return false; }
|
||||
cudaGraphUpload(ex, cs_);
|
||||
cudaStreamSynchronize(cs_);
|
||||
std::fprintf(stderr, "strata verify: captured the batch window over slots [%d, %d)\n", base, base + S);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
|
||||
cudaGraphExec_t& cex = commit_bm_[base * 16 + S];
|
||||
if (cex != nullptr) return true;
|
||||
using namespace strata::kernels;
|
||||
const ModelGeometry& g = *g_;
|
||||
const QsaShapes s = shapes_of(g);
|
||||
const int64_t C = g.ssm_conv_channels, HV = g.ssm_v_heads, ZV = g.ssm_value_dim, ID = g.idx_key_dim, MT = max_t_;
|
||||
const int64_t CB = 2 + MT, nQ = g.n_qsa_layers();
|
||||
const uint64_t gdn_floats = (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size +
|
||||
(uint64_t) g.ssm_conv_channels * (g.ssm_d_conv - 1);
|
||||
const int64_t TS = (s.idx_block - 1) * ID;
|
||||
const int64_t HS = (int64_t) NG_HIST * NG_HC_DIM;
|
||||
if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) {
|
||||
err = "verify: begin batch commit capture failed";
|
||||
return false;
|
||||
}
|
||||
bool ok = true;
|
||||
try {
|
||||
copy_i32_from_mapped(commitb_ + (size_t) base * CB, m_commitb_ + (size_t) base * CB, S * CB, cs_);
|
||||
int64_t qsa_index = 0, gdn_index = 0;
|
||||
for (int64_t l = 0; l < lb_; ++l) (is_qsa_layer(g, l) ? qsa_index : gdn_index) += 1;
|
||||
for (int64_t l = lb_; l < le_ && ok; ++l) {
|
||||
const LayerView v(*wt_, l);
|
||||
if (!is_qsa_layer(g, l)) {
|
||||
const WeightRef* wnm = need(v, "ssm_norm.weight", err);
|
||||
if (!wnm) { ok = false; break; }
|
||||
for (int t = 0; t < S; ++t) {
|
||||
SessionState& sx = *slots_[(size_t) (base + t)];
|
||||
const int32_t* keep = commitb_ + (size_t) (base + t) * CB;
|
||||
float* state = sx.gdn_state + (size_t) (gdn_index - sx.gdn_ord0) * gdn_floats;
|
||||
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
|
||||
gdn_conv_commit(conv, qkv_L_ + (size_t) gdn_index * MT * C + (size_t) t * C, (int) C, keep, cs_);
|
||||
gdn_step_norm_multi(state, h_L_ + (size_t) gdn_index * MT * C + (size_t) t * C, (int) C,
|
||||
gate_L_ + (size_t) gdn_index * MT * HV + (size_t) t * HV,
|
||||
beta_L_ + (size_t) gdn_index * MT * HV + (size_t) t * HV, z_ + (size_t) t * ZV,
|
||||
(const float*) wnm->data, EPS, y_dummy_ + (size_t) t * ZV, (int) g.ssm_k_heads,
|
||||
(int) HV, 1, keep, cs_);
|
||||
}
|
||||
++gdn_index;
|
||||
} else {
|
||||
const WeightRef* wikn = need(v, "indexer.k_norm.weight", err);
|
||||
if (!wikn) { ok = false; break; }
|
||||
for (int t = 0; t < S; ++t) {
|
||||
const QsaState& st = slots_[(size_t) (base + t)]->qsa_states[qsa_index];
|
||||
copy_from_mapped(st.idx_tail, tail_snap_b_ + ((size_t) (base + t) * nQ + qsa_index) * TS, TS, cs_);
|
||||
const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos};
|
||||
native_qsa_indexer_append(idx_raw_L_ + (size_t) (qsa_index * MT + t) * ID, commitb_ + (size_t) (base + t) * CB + 2,
|
||||
0, (const float*) wikn->data, EPS, ib, s, st.max_cells, rope_scaling(), cs_);
|
||||
}
|
||||
++qsa_index;
|
||||
}
|
||||
}
|
||||
if (ok && ss_->ple.ready() && ple_stage())
|
||||
for (int t = 0; t < S; ++t)
|
||||
copy_indexed(slots_[(size_t) (base + t)]->ple_hist, hist_snap_ + (size_t) t * HS, HS,
|
||||
commitb_ + (size_t) (base + t) * CB + 1, HS, cs_);
|
||||
} catch (const std::exception& e) {
|
||||
err = std::string("verify batch commit: ") + e.what();
|
||||
ok = false;
|
||||
}
|
||||
cudaGraph_t graph = nullptr;
|
||||
const cudaError_t ce = cudaStreamEndCapture(cs_, &graph);
|
||||
if (!ok) {
|
||||
if (graph) cudaGraphDestroy(graph);
|
||||
return false;
|
||||
}
|
||||
if (ce != cudaSuccess || cudaGraphInstantiate(&cex, graph, 0) != cudaSuccess) {
|
||||
if (graph) cudaGraphDestroy(graph);
|
||||
err = std::string("verify: batch commit capture: ") + cudaGetErrorString(ce);
|
||||
return false;
|
||||
}
|
||||
cudaGraphDestroy(graph);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verifier::stage_batch(int base, int S, const int32_t* tokens, const int64_t* pos, std::string& err) {
|
||||
using namespace strata::kernels;
|
||||
if (S < 1 || base < 0 || base + S > (int) slots_.size()) { err = "verify: batch rows out of range (init_slots)"; return false; }
|
||||
if (released_.load()) { err = "verify: an earlier window never finished on the GPU (#267); restart the engine"; return false; }
|
||||
const ModelGeometry& g = *g_;
|
||||
for (int t = 0; t < S; ++t)
|
||||
if (pos[t] < 0 || pos[t] + 1 > slots_[(size_t) (base + t)]->max_cells) {
|
||||
err = "verify: slot " + std::to_string(base + t) + " runs past its context";
|
||||
return false;
|
||||
}
|
||||
if (!capture_batch(base, S, err) || !capture_commit_batch(base, S, err)) return false;
|
||||
const Clock::time_point t0 = Clock::now();
|
||||
const QsaShapes s = shapes_of(g);
|
||||
for (int t = 0; t < S; ++t) {
|
||||
h_tok_[t] = tokens[t];
|
||||
qsa_step_fill(h_step_ + t * kStepCount, pos[t], s);
|
||||
for (int64_t h = 0; h < g.n_head; ++h) h_pos_[t * g.n_head + h] = (int32_t) pos[t];
|
||||
int32_t* pk = h_pos_ + (size_t) max_t_ * g.n_head;
|
||||
int32_t* pi = pk + (size_t) max_t_ * g.n_head_kv;
|
||||
for (int64_t h = 0; h < g.n_head_kv; ++h) pk[t * g.n_head_kv + h] = (int32_t) pos[t];
|
||||
for (int64_t h = 0; h < g.idx_q_heads; ++h) pi[t * g.idx_q_heads + h] = (int32_t) pos[t];
|
||||
}
|
||||
if (ss_->ple.ready() && ple_stage()) {
|
||||
uint32_t rows[kVerifyMaxT * PLE_N_HEADS];
|
||||
for (int t = 0; t < S; ++t) {
|
||||
const SessionState& sx = *slots_[(size_t) (base + t)];
|
||||
int32_t prev[2] = {sx.ple_prev[0], sx.ple_prev[1]};
|
||||
ngram_rows(&tokens[t], prev, 1, ss_->ple.consts, rows + t * PLE_N_HEADS);
|
||||
}
|
||||
if (!ss_->ple.table->gather_batch(rows, (size_t) S, h_ple_, err)) return false;
|
||||
}
|
||||
// the commit's rows (a batch window keeps every row): [1, 0, position, -1 ..] per slot
|
||||
const int64_t CB = 2 + max_t_;
|
||||
for (int t = 0; t < S; ++t) {
|
||||
int32_t* c = h_commitb_ + (size_t) (base + t) * CB;
|
||||
c[0] = 1;
|
||||
c[1] = 0;
|
||||
c[2] = (int32_t) pos[t];
|
||||
for (int64_t j = 3; j < CB; ++j) c[j] = -1;
|
||||
}
|
||||
*(volatile uint32_t*) h_seq_ = 0;
|
||||
*(volatile uint32_t*) h_flag_ = 0;
|
||||
*(volatile uint32_t*) h_flagA_ = 0;
|
||||
*(volatile uint32_t*) h_flagB_ = 0;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
last_t_ = S;
|
||||
last_base_ = base;
|
||||
last_batch_ = true;
|
||||
for (int t = 0; t < S; ++t) { last_tokens_[t] = tokens[t]; last_pos_b_[t] = pos[t]; }
|
||||
ms_host += ms_since(t0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verifier::run_slots(int S, const int32_t* tokens, const int64_t* pos, PoolMultiFn pool, void* user, int32_t* out,
|
||||
std::string& err) {
|
||||
using namespace strata::kernels;
|
||||
const OnDevice on_device(device_);
|
||||
const ModelGeometry& g = *g_;
|
||||
if (!stage_batch(0, S, tokens, pos, err)) return false;
|
||||
const cudaError_t le = cudaGraphLaunch(exec_bm_[0 * 16 + S], cs_);
|
||||
if (le != cudaSuccess) { err = std::string("verify: batch launch: ") + cudaGetErrorString(le); return false; }
|
||||
(void) cudaStreamQuery(cs_);
|
||||
volatile uint32_t* const seq = h_seq_;
|
||||
volatile uint32_t* const flag = h_flag_;
|
||||
const int64_t steps = le_ - lb_;
|
||||
for (int64_t k = 0; k < steps; ++k) {
|
||||
const int64_t l = lb_ + k;
|
||||
const uint32_t want = (uint32_t) (k + 1);
|
||||
const Clock::time_point a = Clock::now();
|
||||
auto last_flush = a;
|
||||
uint32_t spins = 0;
|
||||
progress_at("verify batch: waiting for the GPU to reach layer", l);
|
||||
while (*seq < want) {
|
||||
_mm_pause();
|
||||
if ((++spins & 1023u) != 0) continue;
|
||||
const auto now = Clock::now();
|
||||
if (now - last_flush > std::chrono::microseconds(2000)) {
|
||||
last_flush = now;
|
||||
const cudaError_t q = cudaStreamQuery(cs_);
|
||||
if (q != cudaErrorNotReady && *seq < want) {
|
||||
err = "verify batch: layer " + std::to_string(l) + " never rang (" +
|
||||
(q == cudaSuccess ? std::string("graph finished") : std::string(cudaGetErrorString(q))) + ")";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (now - a > std::chrono::seconds(20)) {
|
||||
err = "verify batch: timed out at layer " + std::to_string(l) + released_note(release_gpu_waits(5000));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const Clock::time_point b = Clock::now();
|
||||
cur_layer_ = want - 1;
|
||||
set_plan_slot(0);
|
||||
progress_at("verify batch: the CPU experts of layer", l);
|
||||
if (pool != nullptr) pool(user, h_x_, h_ids_, S, ss_->k, h_ymiss_, l);
|
||||
progress_tick();
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
_mm_sfence();
|
||||
if (*(volatile uint32_t*) h_flagA_ != want) { // the pool did not publish a plan: an empty one
|
||||
sink_.counts[0] = 0;
|
||||
sink_.counts[1] = 0;
|
||||
sink_.counts[2] = 0;
|
||||
sink_.start[0] = 0;
|
||||
sink_.start2[0] = 0;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
*(volatile uint32_t*) h_flagA_ = want;
|
||||
raise_flag(h_flagB_, want);
|
||||
}
|
||||
*flag = want;
|
||||
ms_wait += std::chrono::duration<double, std::milli>(b - a).count();
|
||||
ms_pool += ms_since(b);
|
||||
}
|
||||
const cudaError_t se = cudaStreamSynchronize(cs_);
|
||||
if (se != cudaSuccess) { err = std::string("verify batch: ") + cudaGetErrorString(se); return false; }
|
||||
cudaStreamSynchronize(copy_);
|
||||
if (prof_on_) collect_profile();
|
||||
++windows;
|
||||
if (le_ < g.n_layers) return next_ == nullptr || next_->run_slots(S, tokens, pos, pool, next_user_, out, err);
|
||||
if (!sample_rows(0, S, err)) return false;
|
||||
for (int t = 0; t < S; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
|
||||
progress_at("decode");
|
||||
progress_beat();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verifier::commit_slots(std::string& err) {
|
||||
const OnDevice on_device(device_);
|
||||
if (!last_batch_ || last_t_ < 1) { err = "verify: commit_slots without a batch window"; return false; }
|
||||
const int S = last_t_;
|
||||
const Clock::time_point t0 = Clock::now();
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
const cudaError_t le = cudaGraphLaunch(commit_bm_[last_base_ * 16 + S], cs_);
|
||||
if (le != cudaSuccess) { err = std::string("verify: batch commit launch: ") + cudaGetErrorString(le); return false; }
|
||||
const cudaError_t se = cudaStreamSynchronize(cs_);
|
||||
if (se != cudaSuccess) { err = std::string("verify: batch commit: ") + cudaGetErrorString(se); return false; }
|
||||
if (ple_stage())
|
||||
for (int t = 0; t < S; ++t) {
|
||||
SessionState& sx = *slots_[(size_t) (last_base_ + t)];
|
||||
sx.ple_prev[0] = sx.ple_prev[1];
|
||||
sx.ple_prev[1] = last_tokens_[t];
|
||||
}
|
||||
ms_commit += ms_since(t0);
|
||||
return next_ == nullptr || next_->commit_slots(err);
|
||||
}
|
||||
|
||||
bool Verifier::sample_rows(int base, int S, std::string& err) {
|
||||
bool any = false;
|
||||
for (int t = 0; t < S; ++t) {
|
||||
strata::kernels::SamplerParams sp = slot_sp_[(size_t) (base + t)];
|
||||
if (sp.greedy || sp.temperature <= 0.0f) continue;
|
||||
sp.counter = (uint64_t) last_pos_b_[t]; // Philox(seed, position): the solo window's draw for this position
|
||||
sp.penalty_last_n = 0;
|
||||
strata::kernels::sample_tokens(head_logits_ + (size_t) t * (size_t) n_vocab_, 1, (int) n_vocab_, nullptr, 0, sp,
|
||||
m_out_ + t, cs_);
|
||||
any = true;
|
||||
}
|
||||
if (any && cudaStreamSynchronize(cs_) != cudaSuccess) { err = "verify batch: the row sampling failed"; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Verifier::batch_launch(int base, int S, const int32_t* tokens, const int64_t* pos, std::string& err) {
|
||||
const OnDevice on_device(device_);
|
||||
if (b_running_) { err = "verify: batch_launch while this stage is busy"; return false; }
|
||||
if (!stage_batch(base, S, tokens, pos, err)) return false;
|
||||
cudaError_t le = cudaGraphLaunch(exec_bm_[base * 16 + S], cs_);
|
||||
if (le == cudaSuccess) le = cudaGraphLaunch(commit_bm_[base * 16 + S], cs_); // right behind it: every row is kept
|
||||
if (le != cudaSuccess) { err = std::string("verify: batch launch: ") + cudaGetErrorString(le); return false; }
|
||||
(void) cudaStreamQuery(cs_);
|
||||
if (ple_stage()) // the host's side of the commit (the hash's last two tokens)
|
||||
for (int t = 0; t < S; ++t) {
|
||||
SessionState& sx = *slots_[(size_t) (base + t)];
|
||||
sx.ple_prev[0] = sx.ple_prev[1];
|
||||
sx.ple_prev[1] = tokens[t];
|
||||
}
|
||||
b_running_ = true;
|
||||
b_k_ = 0;
|
||||
b_steps_ = le_ - lb_;
|
||||
b_last_ = Clock::now();
|
||||
return true;
|
||||
}
|
||||
|
||||
int Verifier::batch_poll(PoolMultiFn pool, void* user, std::string& err) {
|
||||
if (!b_running_) return 1;
|
||||
const OnDevice on_device(device_);
|
||||
volatile uint32_t* const seq = h_seq_;
|
||||
const int S = last_t_;
|
||||
while (b_k_ < b_steps_) {
|
||||
const uint32_t want = (uint32_t) (b_k_ + 1);
|
||||
if (*seq < want) {
|
||||
const auto now = Clock::now();
|
||||
if (now - b_last_ > std::chrono::milliseconds(2)) {
|
||||
const cudaError_t q = cudaStreamQuery(cs_);
|
||||
if (q != cudaErrorNotReady && *seq < want) {
|
||||
err = "verify batch: layer " + std::to_string(lb_ + b_k_) + " never rang (" +
|
||||
(q == cudaSuccess ? std::string("graph finished") : std::string(cudaGetErrorString(q))) + ")";
|
||||
b_running_ = false;
|
||||
return -1;
|
||||
}
|
||||
if (now - b_last_ > std::chrono::seconds(20)) {
|
||||
err = "verify batch: timed out at layer " + std::to_string(lb_ + b_k_) + released_note(release_gpu_waits(5000));
|
||||
b_running_ = false;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
const Clock::time_point b = Clock::now();
|
||||
cur_layer_ = want - 1;
|
||||
set_plan_slot(0);
|
||||
if (pool != nullptr) pool(user, h_x_, h_ids_, S, ss_->k, h_ymiss_, lb_ + b_k_);
|
||||
progress_tick();
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
_mm_sfence();
|
||||
if (*(volatile uint32_t*) h_flagA_ != want) { // the pool did not publish a plan: an empty one
|
||||
sink_.counts[0] = 0;
|
||||
sink_.counts[1] = 0;
|
||||
sink_.counts[2] = 0;
|
||||
sink_.start[0] = 0;
|
||||
sink_.start2[0] = 0;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
*(volatile uint32_t*) h_flagA_ = want;
|
||||
raise_flag(h_flagB_, want);
|
||||
}
|
||||
*(volatile uint32_t*) h_flag_ = want;
|
||||
ms_pool += ms_since(b);
|
||||
b_last_ = Clock::now();
|
||||
++b_k_;
|
||||
}
|
||||
const cudaError_t q = cudaStreamQuery(cs_);
|
||||
if (q == cudaErrorNotReady) return 0;
|
||||
if (q != cudaSuccess) { err = std::string("verify batch: ") + cudaGetErrorString(q); b_running_ = false; return -1; }
|
||||
const cudaError_t qc = cudaStreamQuery(copy_); // no host function of this window may raise flag B in the next
|
||||
if (qc == cudaErrorNotReady) return 0;
|
||||
if (prof_on_) collect_profile();
|
||||
if (last_stage()) {
|
||||
if (!sample_rows(last_base_, S, err)) { b_running_ = false; return -1; }
|
||||
for (int t = 0; t < S; ++t) b_out_[t] = ((volatile int32_t*) h_out_)[t];
|
||||
}
|
||||
++windows;
|
||||
b_running_ = false;
|
||||
progress_beat();
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool Verifier::copy_logits(int t, float* host) const {
|
||||
if (next_ != nullptr) return next_->copy_logits(t, host); // a layer split: the head is on the last stage
|
||||
if (head_logits_ == nullptr || host == nullptr || t < 0 || n_vocab_ <= 0) return false;
|
||||
|
||||
Reference in new issue
Block a user