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++

Merge f-mac-compat 68387f1a

Niko12216294f3468f7e
diff --cc metal/src/engine/window.cpp
index 786f27d12,76bd67142..90dad5891
--- a/metal/src/engine/window.cpp
+++ b/metal/src/engine/window.cpp
@@@ -758,17 -687,17 +786,20 @@@ bool Window::run(int Tn, const 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);
}
+ seg_T_ = Tn;
+ const double r0 = now_ms();
- last_gpu_ms_ = 1000.0 * cx_->run_graph(c.ds);
+ last_gpu_ms_ = 1000.0 * run_ds(c);
+ tp_rg_ = now_ms() - r0;
last_n_disp_ = (int) c.ds.size();
return finish(Tn, tokens, pos0, head, out, t0);
}
+ if (stream_ && !st_->ready() && !stream_arena(err)) return false;
Cached& cw = wcache_[key];
rec_ = &cw.qsa;
+ cuts_ = stream_ ? &cw.cuts : nullptr;
std::vector<mtl::Dispatch>& ds = cw.ds;
nb_ = false;
+ if (fold_n > 0) fold(ds, fold_n, 1 - hp_); // the last window's commit (hp_ already turned)
if (ple_on_) {
xprep(ds, emb_, XE_, N);
gemv(ds, T("blk.1.ple_key.weight"), XE_, pkey_, 10240, Tn);
@@@ -833,67 -762,73 +864,128 @@@
push(ds, d2);
}
rec_ = nullptr;
+ cuts_ = nullptr;
+ seg_T_ = Tn;
+ const double r0 = now_ms();
- last_gpu_ms_ = 1000.0 * cx_->run_graph(ds);
+ last_gpu_ms_ = 1000.0 * run_ds(cw);
+ tp_rg_ = now_ms() - r0;
last_n_disp_ = (int) ds.size();
return finish(Tn, tokens, pos0, head, out, t0);
}
+double Window::run_ds(Cached& c) {
+ if (!stream_) return cx_->run_graph(c.ds);
+ return cx_->run_graph_seg(c.ds, c.cuts, [](void* u, int s) { ((Window*) u)->at_cut(s); }, this);
+}
+
+namespace {
+Window* g_stream_window = nullptr;
+void stream_atexit() {
+ if (g_stream_window) g_stream_window->stream_report(stderr);
+}
+} // namespace
+
+// The arena, once the rest is allocated (the MTP layer loads after init): what the GPU working set and the RAM leave
+// with STRATA_METAL_KEEP_FREE_GB (default 9) kept for macOS; STRATA_METAL_ARENA_GB sets it.
+bool Window::stream_arena(std::string& err) {
+ uint64_t phys = 0;
+ size_t len = sizeof phys;
+ sysctlbyname("hw.memsize", &phys, &len, nullptr, 0);
+ const double gb = 1e9;
+ const double used = (double) cx_->allocated();
+ const char* ek = std::getenv("STRATA_METAL_KEEP_FREE_GB");
+ const double keep = (ek ? std::atof(ek) : 8.0) * gb;
+ // what macOS could give now: free + inactive + speculative + purgeable pages (the file cache is given back)
+ double avail = (double) phys - used;
+ {
+ vm_statistics64_data_t vs;
+ mach_msg_type_number_t cnt = HOST_VM_INFO64_COUNT;
+ if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) &vs, &cnt) == KERN_SUCCESS)
+ avail = (double) (vs.free_count + vs.inactive_count + vs.speculative_count + vs.purgeable_count) * (double) vm_page_size;
+ }
+ double budget = std::min((double) cx_->recommended_working_set() - used - 1.0 * gb, avail - keep);
+ if (const char* e = std::getenv("STRATA_METAL_ARENA_GB")) budget = std::atof(e) * gb;
+ if (budget < 2 * gb) {
+ std::fprintf(stderr, "strata metal stream: only %.1f GB left for the expert arena (the GPU working set %.1f GB, RAM %.1f GB, "
+ "%.1f GB allocated): expect a low hit rate\n", budget / gb, cx_->recommended_working_set() / gb,
+ phys / gb, used / gb);
+ budget = std::max(budget, 1.0 * gb);
+ }
+ std::string prof;
+ if (const char* e = std::getenv("STRATA_METAL_EXPERT_PROFILE")) prof = e;
+ else prof = std::string(STRATA_METAL_DATA_DEFAULT) + "/expert-profile.bin";
+ std::fprintf(stderr, "strata metal stream: %.2f GB allocated before the arena; RAM %.1f GB (%.1f GB available, %.1f GB kept "
+ "for macOS), GPU working set %.1f GB\n", used / gb, phys / gb, avail / gb, keep / gb,
+ cx_->recommended_working_set() / gb);
+ if (!st_->make_arena(*cx_, (uint64_t) budget, prof, err)) return false;
+ g_stream_window = this;
+ std::atexit(stream_atexit);
+ return true;
+}
+
+ 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) {
+ ++win_n_[Tn];
+ win_ms_[Tn] += now_ms() - t0;
+ win_gpu_[Tn] += last_gpu_ms_;
+ 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;