From 90359513e3b123584b205cf22e75fa2fa58eedde Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 24 Oct 2026 22:18:40 +0200 Subject: [PATCH] DeepSeek Metal: DS4_BUSY_ALL experiment (off), progress lines --- metal/src/mtl_rt.mm | 7 +++++-- src/dsv41/ds4_main.cpp | 8 ++++++++ src/dsv41/ds4_model.cu | 7 +++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/metal/src/mtl_rt.mm b/metal/src/mtl_rt.mm index 8a0ba797..bd0fd849 100644 --- a/metal/src/mtl_rt.mm +++ b/metal/src/mtl_rt.mm @@ -218,6 +218,7 @@ struct Busy { id ps = nil; id flag = nil; id last = nil; + bool running = false; }; Busy g_busy; } // namespace @@ -250,7 +251,8 @@ kernel void busy_spin(device volatile atomic_uint* flag [[buffer(0)]], constant g_busy.ok = true; } } - if (!g_busy.ok) return; + if (!g_busy.ok || g_busy.running) return; + g_busy.running = true; if (g_busy.last != nil) [g_busy.last waitUntilCompleted]; // (the previous spin has left) *(volatile uint32_t*) [g_busy.flag contents] = 0; uint32_t cap = std::getenv("DS4_BUSY_CAP") ? (uint32_t) std::atoi(std::getenv("DS4_BUSY_CAP")) : 20000u; // ~ tens of ms at most @@ -266,8 +268,9 @@ kernel void busy_spin(device volatile atomic_uint* flag [[buffer(0)]], constant } } extern "C" void ds4_busy_end() { - if (!g_busy.ok || g_busy.last == nil) return; + if (!g_busy.ok || g_busy.last == nil || !g_busy.running) return; *(volatile uint32_t*) [g_busy.flag contents] = 1; + g_busy.running = false; } Context::Context() : impl_(new Impl) { diff --git a/src/dsv41/ds4_main.cpp b/src/dsv41/ds4_main.cpp index 1ecf4fa2..9a997e77 100644 --- a/src/dsv41/ds4_main.cpp +++ b/src/dsv41/ds4_main.cpp @@ -991,6 +991,14 @@ int main(int argc, char** argv) { out.push_back(tok); std::printf("%d,", tok); std::fflush(stdout); + if (std::getenv("DS4_PROGRESS") && out.size() % 200 == 0) { // (sustained-run time series) + static double tp = now_ms(); + static size_t np = 0; + const double tn = now_ms(); + std::fprintf(stderr, "progress %zu tokens: %.2f tok/s over the last %zu\n", out.size(), 1000.0 * (double) (out.size() - np) / (tn - tp), out.size() - np); + tp = tn; + np = out.size(); + } if ((tok == 1 && std::getenv("DS4_NO_EOS") == nullptr) || (int) out.size() >= n) break; // (DS4_NO_EOS: decode n tokens whatever) if (got.empty()) { int off = 0, acc = 0; diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 60f56c5b..34153695 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -7584,6 +7584,13 @@ struct Model::Impl { } bool forward(const int* tk, int T, int logits_rows, std::string& err, int seed_rows = -1) { +#if defined(STRATA_METAL) + struct BusyAll { // DS4_BUSY_ALL: the GPU is held busy for the whole step (the host gaps between its bursts too) + bool on; + BusyAll() : on(std::getenv("DS4_BUSY_ALL") != nullptr) { if (on) ds4_busy_begin(); } + ~BusyAll() { if (on) ds4_busy_end(); } + } busy_all; +#endif if (T < 1 || T > kWin) { err = "a window of " + std::to_string(T) + " tokens"; return false;