Merge branch 's25-tps2' into f-halo-int

This commit is contained in:
Niko1221 committed 2026-10-03 08:20:18 +00:00
commit f2afdfc244
7 files changed
+99 -9

No files matched your search

+2
View File
@@ -200,6 +200,8 @@ private:
VerifyHits hits_;
const NativeHead* head_ = nullptr;
int max_t_ = 0;
float* ple_key_ = nullptr; ///< STRATA_PLE_BATCH: the window rows' PLE key / value projections
float* ple_val_ = nullptr;
int last_t_ = 0;
int64_t last_pos0_ = 0;
int32_t last_tokens_[8] = {};
+6
View File
@@ -33,6 +33,7 @@ namespace strata::kernels {
/// Opt in to pinned CUDA BF16/F32 MMVF for the PLE value projection only (default false).
/// Configure before session capture; captured graphs retain their selected projection kernels.
void ple_set_native_bf16(bool enabled);
bool ple_native_bf16();
/// Opt in to pinned CUDA postprojection norms, gate, convolution and residual
/// arithmetic (default false). Set before capture; existing graphs keep their
@@ -70,6 +71,11 @@ struct PleWeights {
/// Plan v0.3 P6: the IQ model files keep `ple_key` in BF16 ([n_embd, hc_dim], `w[o*n_embd+i]`); when set it
/// replaces both key paths above.
const uint16_t* key_bf16 = nullptr;
/// S25 (STRATA_PLE_BATCH=1): the key (hc_dim) and value (n_embd) projections of `emb`, already computed by the
/// caller for a window's rows at once (the same kernels' multi-row forms); ple_block then copies them instead of
/// running its own GEMVs
const float* pre_key = nullptr;
const float* pre_value = nullptr;
};
/// Everything the block produces, in the order `ple_layer_xcheck`'s oracle writes it. Any pointer may be
+5 -1
View File
@@ -24,6 +24,7 @@ bool hc_q8_requested() {
return on;
}
bool hc_q8_name(const std::string& name) {
if (name == "output_hc_down.weight" || name == "output_hc_up.weight") return true; // S25: the final mixer too
if (name.rfind("blk.", 0) != 0) return false;
static const char* suffixes[] = {".hc_attn_down.weight", ".hc_attn_up.weight", ".hc_attn_inject.weight",
".hc_ffn_down.weight", ".hc_ffn_up.weight", ".hc_ffn_inject.weight"};
@@ -246,7 +247,10 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tabl
}
if (hc_q8_requested())
for (const auto& tensor : gguf.tensors()) {
const bool f32_inject = tensor.type == 0 && tensor.name.ends_with("_inject.weight");
// S25: the F32 inject rows hold BF16-exact values: the read takes the pack's BF16 rows unless
// STRATA_HC_Q8_INJECT=1 asks for a Q8_0 copy
static const bool q8_inject = [] { const char* v = std::getenv("STRATA_HC_Q8_INJECT"); return v && v[0] == '1'; }();
const bool f32_inject = q8_inject && tensor.type == 0 && tensor.name.ends_with("_inject.weight");
if ((tensor.type != 8 && !f32_inject) || !hc_q8_name(tensor.name) || tensor.shape.size() != 2) continue;
auto found = table.table_.find(tensor.name);
if (found == table.table_.end() || found->second.hc_q8 != nullptr) continue;
+43 -2
View File
@@ -324,6 +324,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
sh_up_ = b.take<float>(T * (uint64_t) g.n_ff); sh_g_ = b.take<float>(T + 4);
head_logits_ = b.take<float>(T * (uint64_t) n_vocab_);
hist_snap_ = b.take<float>(T * HS);
ple_key_ = b.take<float>(T * (uint64_t) strata::kernels::NG_HC_DIM); ple_val_ = b.take<float>(T * N);
};
Bump count;
carve(count);
@@ -503,13 +504,37 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
bool pending = l > 0 && !cvec().covers(l - 1);
if (l == 1 && ple_on) {
float* normalized = (float*) ((uint8_t*) ss.ple.scratch + ple_block_scratch_bytes());
// S25 (STRATA_PLE_BATCH=1): the key and value projections depend only on each row's n-gram embedding, so
// the group's rows run through the multi-row forms of the same kernels (the weights read once); every
// row's arithmetic is the single-row call's. The history-dependent rest stays row by row.
static const bool ple_batch = [] { const char* e = std::getenv("STRATA_PLE_BATCH"); return e && e[0] == '1'; }();
PleWeights pw = ss.ple.w;
const bool nkey = pw.key_native_data != nullptr && pw.key_bf16 == nullptr;
const bool batched = ple_batch && n > 1 && (pw.key_bf16 != nullptr || nkey) && ple_native_bf16();
if (batched) {
try {
if (pw.key_bf16 != nullptr)
bf16_gemv_fp32_mmvf_multi(ple_ + tb * N, N, pw.key_bf16, ple_key_ + (size_t) tb * NG_HC_DIM,
NG_HC_DIM, N, NG_HC_DIM, n, cs);
else {
native_quantize_q8_1(ple_ + tb * N, xq_, (int) N, n, cs);
native_mmvq(pw.key_native_type, pw.key_native_data, xq_, ple_key_ + (size_t) tb * NG_HC_DIM,
(int) N, NG_HC_DIM, n, cs);
}
bf16_gemv_fp32_mmvf_multi(ple_ + tb * N, N, pw.value_bf16, ple_val_ + tb * N, N, N, N, n, cs);
} catch (const std::exception& e) {
err = std::string("verify PLE batch: ") + e.what();
return false;
}
}
for (int t = tb; t < te; ++t) {
if (batched) { pw.pre_key = ple_key_ + (size_t) t * NG_HC_DIM; pw.pre_value = ple_val_ + t * N; }
gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);
PleOut po;
po.normalized = normalized;
po.result = Rt(t);
try {
ple_block(ple_ + t * N, Rt(t), ss.ple.hist, ss.ple.w, po, ss.ple.scratch, cs);
ple_block(ple_ + t * N, Rt(t), ss.ple.hist, pw, po, ss.ple.scratch, cs);
ple_history_advance(ss.ple.hist, normalized, cs);
} catch (const std::exception& e) {
err = std::string("verify PLE: ") + e.what();
@@ -891,7 +916,23 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
const WeightRef *hn = wt.find("output_hc_norm.weight"), *hd = wt.find("output_hc_down.weight"),
*hu = wt.find("output_hc_up.weight");
if (!hn || !hd || !hu) { err = "verify: an output_hc_* weight is missing"; return false; }
for (int t = 0; t < T; ++t) {
// S25 (STRATA_HC_Q8=1): the final mixer from the GGUF's Q8_0 projections too, through the same multi read
// (no pending write, no inject)
const bool mix_q8 = hd->hc_q8 != nullptr && hu->hc_q8 != nullptr && head_ != nullptr && head_->loaded();
if (mix_q8) {
FusedGrArgs fa[kFusedGrMaxT];
for (int t = 0; t < T; ++t) {
FusedGrArgs& a = fa[t];
a.R = Rt(t); a.R_out = Rt(t); a.apply = false;
a.w_norm = (const float*) hn->data; a.w_down = (const uint16_t*) hd->data;
a.w_up = (const uint16_t*) hu->data; a.w_inject = nullptr;
a.q8_down = (const uint8_t*) hd->hc_q8; a.q8_up = (const uint8_t*) hu->hc_q8;
a.eps = EPS; a.lo = lo_ + t * g.hc_lr; a.rs = rs_ + t * HC;
a.mixed = head_mixed_ + t * N;
}
fused_gr_read_multi(fa, T, xn_, cs);
}
for (int t = 0; t < T && !mix_q8; ++t) {
BlockBuffers bb = ss.block;
bb.R = Rt(t);
bb.mixed = head_mixed_ + t * N;
+14 -4
View File
@@ -863,7 +863,10 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* _
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int rg = blockIdx.x, kc = blockIdx.y, c = kc / Q8_CPS;
const bool inj = rg == Q8_RG;
const int nrows = inj ? ((m.a[0].q8_inject != nullptr && warp < HC) ? 1 : 0) : Q8_RPW;
// S25: the inject rows are F32 in the GGUF with BF16-exact values, so by default they are read as the pack's BF16
// (exact); a Q8_0 copy (STRATA_HC_Q8_INJECT=1) is the other option
const bool inj_bf16 = inj && m.a[0].q8_inject == nullptr;
const int nrows = inj ? (((m.a[0].q8_inject != nullptr || m.a[0].w_inject != nullptr) && warp < HC) ? 1 : 0) : Q8_RPW;
const int row0 = inj ? warp : (rg * WARPS + warp) * Q8_RPW;
const uint8_t* wb = inj ? m.a[0].q8_inject : m.a[0].q8_down;
const int sub = lane & 7, bl = lane >> 3; // lanes 8b..8b+7: one Q8_0 block, 4 values each
@@ -871,7 +874,7 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* _
float dq[Q8_RPW][Q8_SPB];
#pragma unroll
for (int r = 0; r < Q8_RPW; ++r) {
if (r >= nrows) break;
if (r >= nrows || inj_bf16) break;
const uint8_t* row = wb + (size_t) (row0 + r) * (D / 32) * Q8B + (size_t) kc * (Q8_KC / 32) * Q8B;
#pragma unroll
for (int s = 0; s < Q8_SPB; ++s) {
@@ -919,8 +922,15 @@ __global__ void __launch_bounds__(THREADS) gr_down_q8_kernel(GrMulti m, float* _
for (int k = 0; k < T; ++k) acc[k] = 0.0f;
#pragma unroll
for (int s = 0; s < Q8_SPB; ++s) {
const float4 w = q8_four(q[r][s], dq[r][s]);
const int v = 32 * (4 * s + bl) + 4 * sub;
float4 w;
if (inj_bf16) {
const uint2 b = *reinterpret_cast<const uint2*>(m.a[0].w_inject + (size_t) row0 * D + (size_t) kc * Q8_KC + v);
w = make_float4(__uint_as_float(b.x << 16), __uint_as_float(b.x & 0xffff0000u),
__uint_as_float(b.y << 16), __uint_as_float(b.y & 0xffff0000u));
} else {
w = q8_four(q[r][s], dq[r][s]);
}
#pragma unroll
for (int k = 0; k < T; ++k) {
const float4 x = *reinterpret_cast<const float4*>(&xs[k][v]);
@@ -1086,7 +1096,7 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi
m.xn = xn_scratch;
m.T = n_tok;
cudaStream_t st = (cudaStream_t) stream;
if (a[0].q8_down != nullptr && a[0].q8_up != nullptr && (a[0].w_inject == nullptr || a[0].q8_inject != nullptr)) {
if (a[0].q8_down != nullptr && a[0].q8_up != nullptr) { // inject: Q8_0 copy, or the BF16 rows
launch_q8(m, xn_scratch, st, stamp_buf, stamp_i0); // S23 experiment: STRATA_HC_Q8=1
const cudaError_t eq = cudaGetLastError();
if (eq != cudaSuccess) {
+7 -2
View File
@@ -197,6 +197,7 @@ void ck(cudaError_t e, const char* what) {
} // namespace
void ple_set_native_bf16(bool enabled) { native_bf16 = enabled; }
bool ple_native_bf16() { return native_bf16; }
void ple_set_native_postops(bool enabled) { native_postops = enabled; }
bool ple_native_postops_enabled() { return native_postops; }
@@ -303,7 +304,9 @@ void ple_block(const float* emb, const float* hidden, const float* hist_rows, co
// ---- key = grouped_norm(ple_key @ emb). The optional native projection
// follows pinned CUDA Q8_1 MMVQ; the default retains its canonical Q8_0 path.
if (w.key_bf16 != nullptr) {
if (w.pre_key != nullptr) {
cudaMemcpyAsync(d_key, w.pre_key, (size_t) hc_dim * sizeof(float), cudaMemcpyDeviceToDevice, st);
} else if (w.key_bf16 != nullptr) {
bf16_gemv_fp32_mmvf(emb, w.key_bf16, d_key, n_embd, hc_dim, stream);
} else if (native_key) {
native_quantize_q8_1(emb, w.key_native_q8_1, n_embd, 1, stream);
@@ -319,7 +322,9 @@ void ple_block(const float* emb, const float* hidden, const float* hist_rows, co
}
// The value projection's independent option leaves the nonlinear PLE operations unchanged.
if (native_bf16) {
if (w.pre_value != nullptr) {
cudaMemcpyAsync(d_value, w.pre_value, (size_t) n_embd * sizeof(float), cudaMemcpyDeviceToDevice, st);
} else if (native_bf16) {
bf16_gemv_fp32_mmvf(emb, w.value_bf16, d_value, n_embd, n_embd, stream);
} else {
to_bf16_kernel<<<(n_embd + THREADS - 1) / THREADS, THREADS, 0, st>>>(emb, d_emb16, n_embd);
+22
View File
@@ -2110,6 +2110,28 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
std::fclose(f);
}
}
if (const char* dump = std::getenv("STRATA_PREFILL_DUMP_R_ALL")) {
// S25 (draft-layer distillation data, opt-in): every position's final multi-stream residual as BF16
// (round-to-nearest-even), rows in position order, appended across chunks and requests: [n][hc*n_embd]
cudaStreamSynchronize(m.cs);
if (std::FILE* f = std::fopen(dump, "ab")) {
constexpr int64_t kRows = 512;
std::vector<float> rows((size_t) (kRows * D));
std::vector<uint16_t> out((size_t) (kRows * D));
for (int64_t t0 = 0; t0 < T; t0 += kRows) {
const int64_t nr = std::min<int64_t>(kRows, T - t0);
cudaMemcpy(rows.data(), m.R + t0 * D, (size_t) (nr * D) * 4, cudaMemcpyDeviceToHost);
for (int64_t i = 0; i < nr * D; ++i) {
uint32_t u;
std::memcpy(&u, &rows[(size_t) i], 4);
u += 0x7fffu + ((u >> 16) & 1u);
out[(size_t) i] = (uint16_t) (u >> 16);
}
std::fwrite(out.data(), 2, (size_t) (nr * D), f);
}
std::fclose(f);
}
}
if (on_chunk || on_stage_chunk) {
const auto toc = Clock::now();
if (cudaStreamSynchronize(m.cs) != cudaSuccess) {