Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions common/arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,14 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
params.train_dir = value;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_TRAIN_DIR"));
add_opt(common_arg(
{"--frame-costs"},
"record each serving-loop frame's completed work and host time into a fixed ring, with totals on /metrics "
"and a summary line every 10 s (off unless set, so its own cost is measured against a run without it)",
[](common_params & params) {
params.frame_costs = true;
}
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_FRAME_COSTS"));
add_opt(common_arg(
{"--lora"}, "FNAME",
"path to LoRA adapter (use comma-separated values to load multiple adapters)",
Expand Down
1 change: 1 addition & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ struct common_params {
float val_split = 0.05f; // fraction of the data used for the validation set
// llama-server /train: the ONLY directory it may write adapters into; empty disables the route
std::string train_dir;
bool frame_costs = false; // record each serving-loop frame's work and host time (off unless asked)

// embedding
bool embedding = false; // get only sentence embedding
Expand Down
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ target_compile_features(test-server-checkpoint-policy PRIVATE cxx_std_17)
add_test(NAME test-server-checkpoint-policy COMMAND test-server-checkpoint-policy)
set_tests_properties(test-server-checkpoint-policy PROPERTIES LABELS "main")

add_executable(test-server-frame-costs test-server-frame-costs.cpp)
target_compile_features(test-server-frame-costs PRIVATE cxx_std_17)
add_test(NAME test-server-frame-costs COMMAND test-server-frame-costs)
set_tests_properties(test-server-frame-costs PROPERTIES LABELS "main")

add_executable(test-server-prefill test-server-prefill.cpp)
target_compile_features(test-server-prefill PRIVATE cxx_std_17)
add_test(NAME test-server-prefill COMMAND test-server-prefill)
Expand Down
90 changes: 90 additions & 0 deletions tests/test-server-frame-costs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// what this catches: the engine's frame recorder (tools/server/server-frame-costs.h, CONTINUUM-ENGINE 4.13 step 1)
// claiming what it did not measure: host uptime before activation counted as idle; a mixed frame filed as prefill or
// decode; a failed or retried batch counted as plain work; a reader that fell behind not told what it lost; a window
// the ring overwrote presented as complete. Clocks start far from zero, as ggml_time_us does. Model-free.
#include "../tools/server/server-frame-costs.h"

#include <cmath>
#include <cstdlib>
#include <iostream>

static void require(bool condition, const char * message) {
if (!condition) {
std::cerr << "FAILED: " << message << '\n';
std::exit(1);
}
}

static frame_cost frame(int64_t end_us, int64_t loop, int64_t batch, int64_t learn, int32_t gen, int32_t prompt,
frame_outcome outcome = frame_outcome::ok) {
frame_cost f;
f.end_us = end_us;
f.host_loop_us = loop;
f.host_batch_us = batch;
f.host_learn_us = learn;
f.gen_accepted = gen;
f.prompt_done = prompt;
f.sequences = 4;
f.max_context = 60'000;
f.outcome = outcome;
return f;
}

static void a_window_never_reaches_before_activation() {
const int64_t t0 = 3'600'000'000; // an hour of host uptime before recording began
frame_costs<8> costs(t0);
costs.record(frame(t0 + 100'000, 60'000, 50'000, 0, 4, 0));
const frame_window w = costs.window(0, t0 + 1'000'000);
require(w.wall_us == 1'000'000, "wall time starts at activation, not at the clock's origin");
require(w.outside_loop_us() == 940'000, "outside-loop time is the measured interval minus loop time");
}

static void work_is_filed_by_what_completed() {
const int64_t t0 = 5'000'000;
frame_costs<8> costs(t0);
costs.record(frame(t0 + 100'000, 60'000, 50'000, 0, 4, 0)); // decode
costs.record(frame(t0 + 200'000, 230'000, 200'000, 0, 0, 2048)); // prefill
costs.record(frame(t0 + 400'000, 130'000, 100'000, 20'000, 3, 512)); // mixed, with learning after
costs.record(frame(t0 + 500'000, 40'000, 30'000, 0, 0, 0, frame_outcome::failed));
costs.record(frame(t0 + 600'000, 70'000, 60'000, 0, 4, 0, frame_outcome::retried));
const frame_window w = costs.window(t0, t0 + 1'000'000);
require(w.frames == 5, "every frame in the window");
require(w.batch_us[(size_t) frame_kind::decode] == 110'000, "decode-only frames, including the retried one");
require(w.batch_us[(size_t) frame_kind::prefill] == 200'000, "prefill-only frames");
require(w.batch_us[(size_t) frame_kind::mixed] == 100'000, "a mixed frame stays mixed");
require(w.batch_us[(size_t) frame_kind::none] == 30'000, "a failed frame completed nothing");
require(w.failed == 1 && w.retried == 1, "outcomes counted");
require(w.gen_accepted == 11 && w.prompt_done == 2560, "completed work only");
require(w.host_learn_us == 20'000, "learning time kept apart");
require(w.unclassified_us == 10'000 + 30'000 + 10'000 + 10'000 + 10'000, "loop time no named segment covers");
require(std::fabs(w.gen_per_wall_s() - 11.0) < 1e-9, "accepted output over wall time");
require(w.complete, "nothing overwritten");
}

static void a_reader_that_falls_behind_is_told_what_it_lost() {
const int64_t t0 = 9'000'000;
frame_costs<4> costs(t0);
for (int i = 1; i <= 6; ++i) {
costs.record(frame(t0 + i * 10, 5, 4, 0, 1, 0));
}
std::vector<frame_cost> out;
const frame_drain d = costs.drain(0, out);
require(d.dropped == 2 && out.size() == 4, "two overwritten, the newest four returned");
require(out.front().seq == 2 && out.back().seq == 5 && d.next_seq == 6, "oldest first, sequence numbers intact");
out.clear();
costs.record(frame(t0 + 70, 5, 4, 0, 1, 0));
const frame_drain again = costs.drain(d.next_seq, out);
require(again.dropped == 0 && out.size() == 1 && out.front().seq == 6, "a caught-up reader gets only what is new");

const frame_window w = costs.window(t0, t0 + 1000);
require(!w.complete && w.frames == 4, "a window the ring overwrote is marked incomplete");
require(costs.window(t0 + 45, t0 + 1000).complete, "a window entirely within the ring is complete");
}

int main() {
a_window_never_reaches_before_activation();
work_is_filed_by_what_completed();
a_reader_that_falls_behind_is_told_what_it_lost();
std::cout << "test-server-frame-costs: ok\n";
return 0;
}
22 changes: 22 additions & 0 deletions tools/server/server-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,28 @@ struct server_metrics {
uint64_t n_decode = 0;
uint64_t n_busy_slots = 0;

// serving-loop frames (CONTINUUM-ENGINE 4.13 step 1, server-frame-costs.h): cumulative totals; the per-frame ring
// lives in the server context because this struct is copied into every metrics result. Host time, not device time.
uint64_t n_frames = 0;
uint64_t n_frames_failed = 0;
uint64_t n_frame_slot_failures = 0;
uint64_t n_frame_gen_accepted = 0;
uint64_t t_frame_loop_us = 0;
uint64_t t_frame_batch_us = 0;
uint64_t t_frame_learn_us = 0;
uint64_t t_frame_record_us = 0; // the recorder's own cost, so its overhead is measured, not assumed

template <typename Frame> void add_frame(const Frame & f, int64_t record_us) {
++n_frames;
n_frames_failed += f.outcome == decltype(f.outcome)::failed;
n_frame_slot_failures += (uint64_t) f.slot_failures;
n_frame_gen_accepted += (uint64_t) f.gen_accepted;
t_frame_loop_us += (uint64_t) f.host_loop_us;
t_frame_batch_us += (uint64_t) f.host_batch_us;
t_frame_learn_us += (uint64_t) f.host_learn_us;
t_frame_record_us += (uint64_t) record_us;
}

uint64_t n_draft_tokens = 0; // Total draft tokens generated
uint64_t n_draft_accepted = 0; // Draft tokens actually accepted
uint64_t n_draft_verif_steps = 0; // Total draft token verification steps by the target model
Expand Down
93 changes: 93 additions & 0 deletions tools/server/server-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "server-checkpoint-policy.h"
#include "server-view-end.h"
#include "server-mapped-file.h"
#include "server-frame-costs.h"
#include <filesystem>
#include <thread>
#include "../../src/llama-ext.h"
Expand Down Expand Up @@ -1664,6 +1665,11 @@ struct server_context_impl {
});

metrics.init();
if (params_base.frame_costs) {
frames = std::make_unique<frame_costs<4096>>(metrics.t_start);
frame_slot_stamp.assign(slots.size(), UINT64_MAX);
}
frames_reported_us = metrics.t_start;
publish_ledger(); // readable before the loop's first boundary

if (params_base.cache_idle_slots) {
Expand Down Expand Up @@ -3395,6 +3401,7 @@ struct server_context_impl {
try {
callback(slot);
} catch (const std::exception & e) {
++frame_now.slot_failures;
SLT_ERR(slot, "got exception: %s\n", e.what());
send_error(slot, std::string("got exception: ") + e.what(), ERROR_TYPE_SERVER);
slot.release();
Expand All @@ -3407,6 +3414,7 @@ struct server_context_impl {
try {
callback(*slot);
} catch (const std::exception & e) {
++frame_now.slot_failures;
SLT_ERR(*slot, "got exception: %s\n", e.what());
send_error(*slot, std::string("got exception: ") + e.what(), ERROR_TYPE_SERVER);
slot->release();
Expand Down Expand Up @@ -3436,6 +3444,15 @@ struct server_context_impl {
// time-fair interleave: after a prefill step, generating slots are owed decode-only time equal to that step
int64_t t_decode_owed_us = 0;
int32_t n_batch_gen = 0; // batch size before prompt tokens are added
// CONTINUUM-ENGINE 4.13 step 1: where each frame's device time went, and a window summary every 10 s
// created when serving starts, so no window reaches back into host uptime
std::unique_ptr<frame_costs<4096>> frames;
// the frame in progress: completion owners add to it (prompt tokens of a successful decode, drafts proposed and
// accepted); accepted output comes from n_tokens_generated, the one accepted-token counter
frame_cost frame_now;
int64_t frames_reported_us = 0;
// per-slot stamp of the last frame that counted it: the shape snapshot needs no per-frame allocation or clearing
std::vector<uint64_t> frame_slot_stamp;
server_prefill_order prefill_order;
// #define DEBUG_TIMINGS
#ifdef DEBUG_TIMINGS
Expand All @@ -3459,6 +3476,9 @@ struct server_context_impl {
#endif

void update_slots() {
const int64_t t_loop_start = ggml_time_us();
const int64_t gen_at_start = n_tokens_generated.load(std::memory_order_relaxed);
frame_now = {};
if (minds_on()) {
minds_reap(); // a landed flash write frees her RAM copy; a failed one is written again
}
Expand Down Expand Up @@ -3505,11 +3525,24 @@ struct server_context_impl {
}
}

// from here the iteration is a frame: it is recorded on every way out, including a failed pre_decode
struct frame_guard {
server_context_impl * s;
int64_t t_loop_start;
int64_t gen_at_start;
~frame_guard() {
if (s->frames) {
s->finish_frame(t_loop_start, gen_at_start);
}
}
} frame_guard_{ this, t_loop_start, gen_at_start };

try {
scoped_timer t(t_pre_decode, n_pre_decode);
pre_decode();
batch.render();
} catch (const std::exception & e) {
frame_now.outcome = frame_outcome::failed;
SRV_ERR("pre_decode() failed: %s\n", e.what());
abort_all_slots("pre_decode() failed: " + std::string(e.what()));

Expand Down Expand Up @@ -3542,6 +3575,9 @@ struct server_context_impl {
int32_t off_next = 0;
int32_t n_batch = llama_n_batch(ctx_tgt);
const bool has_prompt = batch.size() > n_batch_gen;
if (frames) {
frame_shape_before_decode();
}
const int64_t t_step_start = ggml_time_us();
for (int32_t off = 0; off < batch.size(); off = off_next) {
const int32_t n_tokens = std::min(n_batch, batch.size() - off);
Expand All @@ -3563,9 +3599,11 @@ struct server_context_impl {
n_batch = llama_n_batch(ctx_tgt);
} else {
// try again with the updated n_batch
frame_now.outcome = frame_outcome::retried;
continue;
}
} catch (const std::exception & e) {
frame_now.outcome = frame_outcome::failed;
SRV_ERR("decode() failed: %s\n", e.what());
abort_all_slots("decode() failed: " + std::string(e.what()));
break; // stop any further processing
Expand All @@ -3575,6 +3613,7 @@ struct server_context_impl {
scoped_timer t(t_post_decode, n_post_decode);
post_decode(n_tokens, off, batch_view);
} catch (const std::exception & e) {
frame_now.outcome = frame_outcome::failed;
SRV_ERR("post_decode() failed: %s\n", e.what());
abort_all_slots("post_decode() failed: " + std::string(e.what()));
break; // stop any further processing
Expand All @@ -3587,7 +3626,58 @@ struct server_context_impl {
// Completion-triggered learning can begin inside post_decode. Its entry
// preparation is already one measured blocking segment; do not append a
// graph segment before the queued NEXT_RESPONSE lets serving run again.
frame_now.host_batch_us = t_step;
const int64_t t_learn_start = ggml_time_us();
if (learning_at_start) { advance_learning(); }
frame_now.host_learn_us = ggml_time_us() - t_learn_start;
}

void finish_frame(int64_t t_loop_start, int64_t gen_at_start) {
const int64_t t_end = ggml_time_us();
frame_now.end_us = t_end;
frame_now.host_loop_us = t_end - t_loop_start;
frame_now.gen_accepted = (int32_t) (n_tokens_generated.load(std::memory_order_relaxed) - gen_at_start);
record_frame();
}

// the batch's shape before decode mutates anything: a slot its own completion releases is still counted
void frame_shape_before_decode() {
const int64_t t0 = ggml_time_us();
const uint64_t stamp = frames->recorded(); // this frame's sequence number, once recorded
for (int32_t i = 0; i < batch.size(); ++i) {
const int id = batch.tokens[i].id_slot;
if (id >= 0 && (size_t) id < frame_slot_stamp.size() && frame_slot_stamp[id] != stamp) {
frame_slot_stamp[id] = stamp;
++frame_now.sequences;
frame_now.max_context = std::max(frame_now.max_context, (int32_t) slots[id].prompt.n_tokens());
}
}
frame_now.instrument_us += ggml_time_us() - t0;
}

void record_frame() {
const int64_t t_record_start = ggml_time_us();
frames->record(frame_now);
if (frame_now.end_us - frames_reported_us >= 10 * 1000 * 1000) {
// an export for people reading the log; the record itself is read through frames->drain
const frame_window w = frames->window(frames_reported_us, frame_now.end_us);
frames_reported_us = frame_now.end_us;
const double wall = std::max<double>(1.0, (double) w.wall_us);
SRV_INF("frames%s: %" PRId64 " in %.1f s; host batch: decode %.1f%% prefill %.1f%% mixed %.1f%% none %.1f%%, "
"learn %.1f%%, unclassified %.1f%%, outside the loop %.1f%%; accepted %" PRId64 " tok (%.1f/s wall), "
"prompt %" PRId64 " tok, drafts %" PRId64 "/%" PRId64 "; retried %" PRId64 " failed %" PRId64
" slot failures %" PRId64 "; widest %" PRId64 " seq, longest context %" PRId64
"; recording cost %.3f%% of loop time\n",
w.complete ? "" : " (INCOMPLETE: the ring overwrote part of this window)",
w.frames, w.wall_us / 1e6,
100.0 * w.batch_us[(size_t) frame_kind::decode] / wall, 100.0 * w.batch_us[(size_t) frame_kind::prefill] / wall,
100.0 * w.batch_us[(size_t) frame_kind::mixed] / wall, 100.0 * w.batch_us[(size_t) frame_kind::none] / wall,
100.0 * w.host_learn_us / wall, 100.0 * w.unclassified_us / wall, 100.0 * w.outside_loop_us() / wall,
w.gen_accepted, w.gen_per_wall_s(), w.prompt_done, w.draft_accepted, w.draft_proposed,
w.retried, w.failed, w.slot_failures, w.max_sequences, w.max_context,
100.0 * w.instrument_us / std::max<double>(1.0, (double) w.host_loop_us));
}
metrics.add_frame(frame_now, frame_now.instrument_us + (ggml_time_us() - t_record_start));
}

void pre_decode() {
Expand Down Expand Up @@ -3799,6 +3889,7 @@ struct server_context_impl {
}

slot.stats.n_draft_tokens += draft.size();
frame_now.draft_proposed += (int32_t) draft.size();
});

// update the batch with the sampled/drafted tokens
Expand Down Expand Up @@ -4777,6 +4868,7 @@ struct server_context_impl {

// update how many tokens out of those tested were accepted
slot.stats.n_draft_accepted += n_accepted;
frame_now.draft_accepted += (int32_t) n_accepted;
slot.stats.n_draft_verif_steps += 1;

auto & n_accepted_per_pos = slot.n_accepted_per_pos;
Expand Down Expand Up @@ -4894,6 +4986,7 @@ struct server_context_impl {
}

metrics_queue_prompt(n_prompt_tokens);
frame_now.prompt_done += (int32_t) n_prompt_tokens; // this decode succeeded: its prompt tokens are done

if (has_output) {
// the context is already synchronized, so the timings are correct
Expand Down
Loading
Loading