diff --git a/CMakeLists.txt b/CMakeLists.txt index fc480ac1..72a13be8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,6 +254,9 @@ target_link_libraries(strata-dequant PRIVATE strata_artifact) # this file is now nothing but the CLI entry point, and its inherited guard spells that as a "selftest" target_compile_definitions(strata-dequant PRIVATE STRATA_DEQUANT_SELFTEST) if(STRATA_BUILD_TESTS) + add_executable(batch_telemetry_test src/core/batch_telemetry_test.cpp) + target_include_directories(batch_telemetry_test PRIVATE include) + add_test(NAME batch_telemetry_test COMMAND batch_telemetry_test) add_executable(logit_bias_test src/core/logit_bias_test.cpp) target_include_directories(logit_bias_test PRIVATE include) add_test(NAME logit_bias_test COMMAND logit_bias_test) diff --git a/include/strata/core/batch_telemetry.hpp b/include/strata/core/batch_telemetry.hpp new file mode 100644 index 00000000..35bfa78f --- /dev/null +++ b/include/strata/core/batch_telemetry.hpp @@ -0,0 +1,110 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace strata::core { + +struct BatchWindowSample { + int64_t windows = 0; + int64_t processed_rows = 0; // rows run by the verifier, including pipeline padding and MTP rows + int64_t active_rows = 0; // live request slots represented by those windows + int64_t elapsed_ms = 0; // first included window's start to the last completion +}; + +// Count completed decode windows only. The caller emits one small protocol line per second, +// plus a final partial sample when all slots become idle; the default-disabled path emits none. +class BatchWindowSampler { +public: + using Clock = std::chrono::steady_clock; + + explicit BatchWindowSampler(bool enabled) : enabled_(enabled) {} + + std::optional complete(int processed_rows, int active_rows, bool idle, + Clock::time_point started, Clock::time_point now) { + if (!enabled_ || processed_rows < 1 || active_rows < 1 || active_rows > processed_rows || now < started) + return std::nullopt; + if (pending_.windows == 0 || started < first_) first_ = started; + ++pending_.windows; + pending_.processed_rows += processed_rows; + pending_.active_rows += active_rows; + if (!idle && now - first_ < std::chrono::seconds(1)) return std::nullopt; + BatchWindowSample sample = pending_; + sample.elapsed_ms = std::max(1, + std::chrono::duration_cast(now - first_).count()); + pending_ = {}; + return sample; + } + +private: + bool enabled_ = false; + BatchWindowSample pending_{}; + Clock::time_point first_{}; +}; + +struct StageInFlightSample { + int64_t elapsed_us = 0; + std::vector in_flight_us; // host time between a stage launch and its completed poll +}; + +// A pipeline stage holds at most one group. Measure the time it is reserved by +// engine work, including poll latency, without claiming GPU kernel utilization. +class StageInFlightSampler { +public: + using Clock = std::chrono::steady_clock; + + StageInFlightSampler(int stages, bool enabled) + : enabled_(enabled && stages > 1 && stages <= 16), + active_(enabled_ ? (std::size_t) stages : 0), + in_flight_us_(enabled_ ? (std::size_t) stages : 0, 0) {} + + void launch(int stage, Clock::time_point now) { + if (!valid(stage)) return; + if (!started_) started_ = now; + active_[(std::size_t) stage] = now; + } + + void complete(int stage, Clock::time_point now) { + if (!valid(stage)) return; + auto& since = active_[(std::size_t) stage]; + if (!since || now < *since) return; + in_flight_us_[(std::size_t) stage] += + std::chrono::duration_cast(now - *since).count(); + since.reset(); + } + + std::optional sample(Clock::time_point now, bool idle) { + if (!enabled_ || !started_ || now < *started_ || + (!idle && now - *started_ < std::chrono::seconds(1))) return std::nullopt; + const auto elapsed = std::max(1, + std::chrono::duration_cast(now - *started_).count()); + for (std::size_t stage = 0; stage < active_.size(); ++stage) { + if (!active_[stage]) continue; + in_flight_us_[stage] += std::chrono::duration_cast( + now - *active_[stage]).count(); + active_[stage] = now; + } + StageInFlightSample result{elapsed, in_flight_us_}; + for (auto& value : result.in_flight_us) value = std::clamp(value, (int64_t) 0, elapsed); + std::fill(in_flight_us_.begin(), in_flight_us_.end(), 0); + if (idle) { + started_.reset(); + std::fill(active_.begin(), active_.end(), std::nullopt); + } else started_ = now; + return result; + } + +private: + bool valid(int stage) const { return enabled_ && stage >= 0 && (std::size_t) stage < active_.size(); } + + bool enabled_ = false; + std::optional started_; + std::vector> active_; + std::vector in_flight_us_; +}; + +} // namespace strata::core diff --git a/src/core/batch_telemetry_test.cpp b/src/core/batch_telemetry_test.cpp new file mode 100644 index 00000000..b2bc445d --- /dev/null +++ b/src/core/batch_telemetry_test.cpp @@ -0,0 +1,50 @@ +#include "strata/core/batch_telemetry.hpp" + +#include +#include + +int main() { + using Sampler = strata::core::BatchWindowSampler; + const auto t0 = Sampler::Clock::time_point{}; + Sampler disabled(false); + if (disabled.complete(4, 2, true, t0, t0)) return 1; + + Sampler sampler(true); + if (sampler.complete(4, 2, false, t0, t0)) return 2; + if (sampler.complete(6, 3, false, t0 + std::chrono::milliseconds(490), + t0 + std::chrono::milliseconds(500))) return 3; + const auto first = sampler.complete(8, 4, false, t0 + std::chrono::milliseconds(990), + t0 + std::chrono::milliseconds(1000)); + if (!first || first->windows != 3 || first->processed_rows != 18 || + first->active_rows != 9 || first->elapsed_ms != 1000) return 4; + if (sampler.complete(0, 0, true, t0, t0 + std::chrono::milliseconds(1001))) return 5; + const auto last = sampler.complete(2, 1, true, t0 + std::chrono::milliseconds(1001), + t0 + std::chrono::milliseconds(1002)); + if (!last || last->windows != 1 || last->processed_rows != 2 || + last->active_rows != 1 || last->elapsed_ms != 1) return 6; + Sampler pipelined(true); + if (pipelined.complete(4, 2, false, t0 + std::chrono::milliseconds(500), + t0 + std::chrono::milliseconds(900))) return 7; + const auto overlap = pipelined.complete(4, 1, true, t0 + std::chrono::milliseconds(200), + t0 + std::chrono::milliseconds(1000)); + if (!overlap || overlap->windows != 2 || overlap->processed_rows != 8 || + overlap->active_rows != 3 || overlap->elapsed_ms != 800) return 8; + using StageSampler = strata::core::StageInFlightSampler; + StageSampler stages(4, true); + stages.launch(0, t0); + stages.launch(1, t0 + std::chrono::milliseconds(200)); + stages.complete(0, t0 + std::chrono::milliseconds(800)); + if (stages.sample(t0 + std::chrono::milliseconds(999), false)) return 9; + const auto first_stages = stages.sample(t0 + std::chrono::milliseconds(1000), false); + if (!first_stages || first_stages->elapsed_us != 1000000 || + first_stages->in_flight_us != std::vector({800000, 800000, 0, 0})) return 10; + stages.complete(1, t0 + std::chrono::milliseconds(1200)); + const auto last_stages = stages.sample(t0 + std::chrono::milliseconds(1200), true); + if (!last_stages || last_stages->elapsed_us != 200000 || + last_stages->in_flight_us != std::vector({0, 200000, 0, 0})) return 11; + StageSampler disabled_stages(4, false); + disabled_stages.launch(0, t0); + if (disabled_stages.sample(t0 + std::chrono::seconds(2), true)) return 12; + std::puts("batch telemetry sampler: OK"); + return 0; +} diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 770eec4e..fa8eb17c 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -18,6 +18,7 @@ #include "strata/platform/memory.hpp" #include "strata/core/arch_defaults.hpp" #include "strata/kernels/gfx_arch.hpp" +#include "strata/core/batch_telemetry.hpp" #include "strata/core/dma_batch.hpp" #include "strata/core/device.hpp" #include "strata/core/remote_expert_opt.hpp" @@ -10209,6 +10210,19 @@ int main(int argc, char** argv) { bool in_prompt_read = false; // batch_step runs between a prompt's chunks: the adaptive tier stands still then int64_t bt_a_rounds0 = 0, bt_a_swapped0 = 0; // the asynchronous tier's counters when the timed windows began Clock::time_point bt_start = Clock::now(); + const char* batch_telemetry_env = std::getenv("STRATA_BATCH_TELEMETRY"); + const bool batch_telemetry_enabled = batch_telemetry_env != nullptr && + batch_telemetry_env[0] == '1' && batch_telemetry_env[1] == '\0'; + strata::core::BatchWindowSampler batch_telemetry(batch_telemetry_enabled); + auto report_batch_window = [&](int processed_rows, int active_rows, bool idle, Clock::time_point started) { + if (!batch_telemetry_enabled) return; + const auto sample = batch_telemetry.complete(processed_rows, active_rows, idle, started, Clock::now()); + if (!sample) return; + std::printf("BSTAT %lld %lld %lld %lld\n", (long long) sample->windows, + (long long) sample->processed_rows, (long long) sample->active_rows, + (long long) sample->elapsed_ms); + std::fflush(stdout); + }; int admit_slot = -1; ///< the slot the request being read will continue in (BGEN) long long admit_max_new = 0; auto batch_on = [&] { for (const BSlot& b : bs) if (b.active) return true; return false; }; @@ -10523,6 +10537,7 @@ int main(int argc, char** argv) { bt_adapt_wait += msd(wa, Clock::now()); } strata::core::progress().busy.store(was_busy); + report_batch_window(S, A, !batch_on(), w0); if (!batch_on() && bt_windows > 0) { const double w = (double) bt_windows, wall = msd(bt_start, Clock::now()); const double L = (double) g.n_layers; @@ -10573,6 +10588,7 @@ int main(int argc, char** argv) { // ---- --batch-groups: the slot groups pipelined through the stages (--batch-groups G > 1 with a layer split) const int n_pipe = (int) stages.size() + 1; const bool piped = o.batch > 0 && o.batch_groups > 1 && n_pipe > 1; + strata::core::StageInFlightSampler stage_in_flight(n_pipe, piped && batch_telemetry_enabled); if (piped && batch_mtp) // #1413: said, not silent std::fprintf(stderr, "strata generate: WARNING: --batch-groups %d runs the pipelined path, which builds one row per slot: " @@ -10593,6 +10609,8 @@ int main(int argc, char** argv) { int64_t pos[strata::kernels::kVerifyMaxT] = {}; int64_t since = 0; ///< tick it started waiting (fairness) int S = 0; ///< slots in its window: up to its last active one (idle ones cost rows) + int active = 0; ///< live slots at launch; S can also include idle padding rows + Clock::time_point started; ///< the group window's first stage launch }; std::vector pg((size_t) (piped ? o.batch_groups : 0)); std::vector stage_group((size_t) n_pipe, -1); @@ -10622,6 +10640,7 @@ int main(int argc, char** argv) { const int r = vk.batch_poll(win_pool_fn, win_pool_user, err); if (r < 0) { std::printf("ERR %s\n", err.c_str()); return false; } if (r == 0) continue; + if (batch_telemetry_enabled) stage_in_flight.complete(k, Clock::now()); stage_group[(size_t) k] = -1; PGroup& G = pg[(size_t) gi]; if (k + 1 < n_pipe) { G.stage = k + 1; G.since = pipe_tick; continue; } @@ -10671,6 +10690,7 @@ int main(int argc, char** argv) { } ++bt_windows; G.inflight = false; + report_batch_window(G.S, G.active, !pipe_inflight() && !batch_on(), G.started); } // start waiting groups on free stages, the longest waiting first; a new step on stage 0 round-robin for (int k = n_pipe - 1; k >= 0; --k) { @@ -10692,7 +10712,11 @@ int main(int argc, char** argv) { rr = pick + 1; PGroup& G = pg[(size_t) pick]; G.S = 0; - for (int t = 0; t < GS; ++t) if (bs[(size_t) (pick * GS + t)].active) G.S = t + 1; + G.active = 0; + for (int t = 0; t < GS; ++t) if (bs[(size_t) (pick * GS + t)].active) { + G.S = t + 1; + ++G.active; + } for (int t = 0; t < G.S; ++t) { BSlot& sl = bs[(size_t) (pick * GS + t)]; G.tok[t] = sl.active ? sl.x : 0; @@ -10701,11 +10725,13 @@ int main(int argc, char** argv) { } G.inflight = true; G.stage = 0; + if (batch_telemetry_enabled) G.started = Clock::now(); drive.d.failed = false; apply_pending(false); } PGroup& G = pg[(size_t) pick]; strata::core::progress().busy.store(true); + if (batch_telemetry_enabled) stage_in_flight.launch(k, Clock::now()); if (!stage_verifier(k).batch_launch(pick * GS, G.S, G.tok, G.pos, err)) { std::printf("ERR %s\n", err.c_str()); return false; @@ -10713,6 +10739,15 @@ int main(int argc, char** argv) { stage_group[(size_t) k] = pick; } if (drive.d.failed) { std::printf("ERR %s\n", drive.d.fail ? drive.d.fail : "the expert pool failed"); return false; } + if (batch_telemetry_enabled) { + const auto sample = stage_in_flight.sample(Clock::now(), !pipe_inflight() && !batch_on()); + if (sample) { + std::printf("PSTAT %zu %lld", sample->in_flight_us.size(), (long long) sample->elapsed_us); + for (const int64_t busy : sample->in_flight_us) std::printf(" %lld", (long long) busy); + std::printf("\n"); + std::fflush(stdout); + } + } if (!pipe_inflight() && !batch_on() && bt_windows > 0) { // all idle: the timing line, as batch_step const double w = (double) bt_windows, wall = std::chrono::duration(Clock::now() - bt_start).count(); std::fprintf(stderr, "strata batch (pipelined, %d groups of %d): %lld group-steps, %lld rows in %.0f ms = "