Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

metal: test tooling - STRATA_METAL_LOGPOS (teacher-forced rows for the KL harness) and STRATA_METAL_PROFILE

Niko1221994ccdf451c7
diff --git a/metal/src/engine/window.cpp b/metal/src/engine/window.cpp
index 6f5b4f001..76bd67142 100644
--- a/metal/src/engine/window.cpp
+++ b/metal/src/engine/window.cpp
@@ -172,6 +172,26 @@ private:
int last_n_disp_ = 0;
int32_t last_tokens_[MAXT] = {};
std::vector<float> last_norm_; // the window's normalized PLE rows (T x hc_dim), for commit
+ // STRATA_METAL_LOGPOS=<path> (test tooling, one-token windows): per decode position the log-probability of the
+ // token that came next (the next window's input: with --spec-follow, the forced text) and the top 64, in the
+ // KL harness's row format (phaseA-tests cuda_p0/cuda_kl.py load_lp): pos, target, lp, argmax, 4 x 0, id:lp ...
+ std::FILE* logpos_ = nullptr;
+ bool lp_pend_ = false;
+ int64_t lp_pos_ = 0, lp_first_ = -1;
+ int32_t lp_arg_ = 0;
+ double lp_lse_ = 0.0;
+ std::vector<float> lp_row_;
+ std::vector<std::pair<float, int32_t>> lp_top_;
+ void logpos(int Tn, const int32_t* tokens, int64_t pos0, bool head);
+ // STRATA_METAL_PROFILE=1: where a window's time goes, per window size (printed when the window goes away)
+ struct Prof { int64_t n = 0; double pre = 0, rg = 0, gpu = 0, fin = 0, all = 0; };
+ std::map<int, Prof> prof_;
+ Prof cprof_, dprof_; // commit (folds), the draft rounds
+ double tp_pre_ = 0, tp_rg_ = 0;
+public:
+ ~Window();
+ void prof_add(bool draft, double ms) { Prof& x = draft ? dprof_ : cprof_; ++x.n; x.all += ms; }
+private:
// the Q8_0 copies of the hyper-connection matrices (the default; STRATA_HC_Q8=0: the BF16 originals)
bool hc_q8_ = false;
mtl::Buffer Q8_;
@@ -653,6 +673,7 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
}
if (!ss.ple.table->gather_batch(rows, (size_t) Tn, emb_.as<float>(), err)) return false;
}
+ tp_pre_ = now_ms();
const int key = (((Tn * 2 + (head ? 1 : 0)) * 2 + hp_) * (MAXT + 1)) + pend_fold_;
const int fold_n = pend_fold_;
pend_fold_ = 0;
@@ -666,7 +687,9 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
std::memcpy(d.args.back().bytes.data(), &a, sizeof a);
if (kind == 2) d.grid[0] = (uint32_t) (((pos0 + Tn) / 4 + 1 + 7) / 8);
}
+ const double r0 = now_ms();
last_gpu_ms_ = 1000.0 * cx_->run_graph(c.ds);
+ tp_rg_ = now_ms() - r0;
last_n_disp_ = (int) c.ds.size();
return finish(Tn, tokens, pos0, head, out, t0);
}
@@ -739,12 +762,73 @@ bool Window::run(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t
push(ds, d2);
}
rec_ = nullptr;
+ const double r0 = now_ms();
last_gpu_ms_ = 1000.0 * cx_->run_graph(ds);
+ tp_rg_ = now_ms() - r0;
last_n_disp_ = (int) ds.size();
return finish(Tn, tokens, pos0, head, out, t0);
}
+void Window::logpos(int Tn, const int32_t* tokens, int64_t pos0, bool head) {
+ static const char* path = std::getenv("STRATA_METAL_LOGPOS");
+ if (path == nullptr || !head) return;
+ if (logpos_ == nullptr) logpos_ = std::fopen(path, "ab");
+ if (logpos_ == nullptr) return;
+ if (lp_pend_ && Tn >= 1 && pos0 == lp_pos_ + 1) {
+ const int32_t tgt = tokens[0];
+ std::fprintf(logpos_, "%lld\t%d\t%.6f\t%d\t0\t0\t0\t0", (long long) (lp_pos_ - lp_first_), tgt,
+ (double) lp_row_[(size_t) tgt] - lp_lse_, lp_arg_);
+ for (const auto& [v, id] : lp_top_) std::fprintf(logpos_, "\t%d:%.6f", id, (double) v - lp_lse_);
+ std::fputc('\n', logpos_);
+ std::fflush(logpos_);
+ }
+ lp_pend_ = false;
+ if (Tn != 1) return;
+ if (lp_first_ < 0) lp_first_ = pos0;
+ const float* lg = lm_.as<float>();
+ lp_row_.assign(lg, lg + n_vocab_);
+ float mx = lg[0];
+ int32_t am = 0;
+ for (int64_t i = 1; i < n_vocab_; ++i) if (lg[i] > mx) { mx = lg[i]; am = (int32_t) i; }
+ double s = 0.0;
+ for (int64_t i = 0; i < n_vocab_; ++i) s += std::exp((double) (lg[i] - mx));
+ lp_lse_ = (double) mx + std::log(s);
+ lp_arg_ = am;
+ lp_top_.clear();
+ for (int64_t i = 0; i < n_vocab_; ++i) lp_top_.push_back({lg[i], (int32_t) i});
+ std::partial_sort(lp_top_.begin(), lp_top_.begin() + 64, lp_top_.end(),
+ [](const auto& a, const auto& b) { return a.first > b.first || (a.first == b.first && a.second < b.second); });
+ lp_top_.resize(64);
+ lp_pos_ = pos0;
+ lp_pend_ = true;
+}
+
+Window::~Window() {
+ if (logpos_) std::fclose(logpos_);
+ if (std::getenv("STRATA_METAL_PROFILE") == nullptr) return;
+ auto line = [](const char* what, const Prof& x) {
+ if (x.n == 0) return;
+ std::fprintf(stderr, "strata metal profile: %-10s %6lld x %.3f ms: host before %.3f, run_graph %.3f (GPU %.3f, so "
+ "encode/submit/wait %.3f), patch+finish %.3f\n", what, (long long) x.n, x.all / x.n, x.pre / x.n,
+ x.rg / x.n, x.gpu / x.n, (x.rg - x.gpu) / x.n, x.fin / x.n);
+ };
+ for (const auto& [k, x] : prof_) {
+ const std::string w = "window T" + std::to_string(k / 2) + (k % 2 ? "" : " nohead");
+ line(w.c_str(), x);
+ }
+ line("commit", cprof_);
+ line("draft", dprof_);
+}
+
bool Window::finish(int Tn, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, double t0) {
+ static const bool prof = std::getenv("STRATA_METAL_PROFILE") != nullptr;
+ if (prof) {
+ Prof& x = prof_[Tn * 2 + (head ? 1 : 0)];
+ const double t1 = now_ms();
+ ++x.n; x.pre += tp_pre_ - t0; x.rg += tp_rg_; x.gpu += last_gpu_ms_; x.fin += t1 - tp_pre_ - tp_rg_;
+ x.all += t1 - t0;
+ }
+ logpos(Tn, tokens, pos0, head);
if (Tn == 1) hp_ = 1 - hp_; // a one-token window committed its GDN steps
for (int t = 0; t < Tn; ++t) out[t] = head ? tokout_.as<int32_t>()[t] : -1;
last_T_ = Tn;
@@ -1325,7 +1409,12 @@ bool window_ensure(const strata::core::ModelGeometry& g, strata::core::SessionSt
bool window_run(void* w, int T, const int32_t* tokens, int64_t pos0, bool head, int32_t* out, std::string& err) {
return ((Window*) w)->run(T, tokens, pos0, head, out, err);
}
-bool window_commit(void* w, int n_keep, std::string& err) { return ((Window*) w)->commit(n_keep, err); }
+bool window_commit(void* w, int n_keep, std::string& err) {
+ const double t0 = now_ms();
+ const bool ok = ((Window*) w)->commit(n_keep, err);
+ ((Window*) w)->prof_add(false, now_ms() - t0);
+ return ok;
+}
bool window_copy_logits(void* w, int t, float* host) { return ((Window*) w)->copy_logits(t, host); }
// the MTP hook (src/core/mtp.cpp under STRATA_GPU_METAL): the drafter lives beside the decode window it reads
@@ -1376,7 +1465,10 @@ bool mtp_prefill(const int32_t* next_tokens, int64_t n, int64_t cell0, std::stri
bool mtp_draft(int T, const int32_t* tokens, int64_t p, int a, int max_steps, int32_t* drafts, float* probs, float min_p,
int* n_drafts, std::string& err) {
if (g_window == nullptr) { err = "metal mtp: no decode window"; return false; }
- return g_window->mtp_draft(T, tokens, p, a, max_steps, drafts, probs, min_p, n_drafts, err);
+ const double t0 = now_ms();
+ const bool ok = g_window->mtp_draft(T, tokens, p, a, max_steps, drafts, probs, min_p, n_drafts, err);
+ g_window->prof_add(true, now_ms() - t0);
+ return ok;
}
} // namespace strata::metal_engine