Skip to content
Open
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
5 changes: 5 additions & 0 deletions ggml/include/ggml-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ extern "C" {
GGML_API bool ggml_backend_sched_alloc_graph_admitted(ggml_backend_sched_t sched, struct ggml_cgraph * graph,
const size_t * max_new, size_t * sizes, ggml_backend_sched_allocation_callback callback, void * user_data);
GGML_API bool ggml_backend_sched_alloc_graph_within(ggml_backend_sched_t sched, struct ggml_cgraph * graph, const size_t * max_new, size_t * sizes); // returns success
// Continuum: a scheduler of one copy that orders its split inputs by its own events instead of waiting for its
// backends to drain, so a graph of its own can be queued behind other work still on the device (a mind's graph
// slot, queued while another mind's step computes). False when no backend of it records events (each such backend
// keeps its drain).
GGML_API bool ggml_backend_sched_use_events(ggml_backend_sched_t sched);
GGML_API enum ggml_status ggml_backend_sched_graph_compute(ggml_backend_sched_t sched, struct ggml_cgraph * graph);
GGML_API enum ggml_status ggml_backend_sched_graph_compute_async(ggml_backend_sched_t sched, struct ggml_cgraph * graph);
GGML_API void ggml_backend_sched_synchronize(ggml_backend_sched_t sched);
Expand Down
19 changes: 19 additions & 0 deletions ggml/src/ggml-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3301,6 +3301,25 @@ bool ggml_backend_sched_alloc_graph_admitted(ggml_backend_sched_t sched, struct
return true;
}

// Continuum (ggml-backend.h): events for a scheduler of one copy. Each split input then waits for this scheduler's
// own last use of its backend, never for everything queued on the device; with one copy that use is this
// scheduler's previous graph.
bool ggml_backend_sched_use_events(ggml_backend_sched_t sched) {
GGML_ASSERT(sched);
if (sched->n_copies > 1) {
return true; // a pipeline-parallel scheduler has them already
}
// a backend whose device records none (the CPU) keeps its drain: it runs synchronously anyway
bool any = false;
for (int b = 0; b < sched->n_backends; b++) {
if (sched->events[b][0] == NULL) {
sched->events[b][0] = ggml_backend_event_new(sched->backends[b]->device);
}
any = any || sched->events[b][0] != NULL;
}
return any;
}

bool ggml_backend_sched_alloc_graph_within(ggml_backend_sched_t sched, struct ggml_cgraph * graph,
const size_t * max_new, size_t * sizes) {
return ggml_backend_sched_alloc_graph_admitted(sched, graph, max_new, sizes, nullptr, nullptr);
Expand Down
14 changes: 14 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,20 @@ extern "C" {
// hers. Name her when she is bound to the sequence; an unnamed sequence is keyed by its id.
LLAMA_API void llama_set_graph_slot_key(struct llama_context * ctx, llama_seq_id seq, uint64_t key);

// Keep each decode's logits readable while the next decode is queued behind it (Continuum;
// llama-output-ring.h): two output buffers swapped by pointer per decode, and an event per decode,
// so a caller serving several minds queues mind B's step before reading mind A's. false, leaving
// it off, when the output device records no events.
LLAMA_API bool llama_set_output_ring(struct llama_context * ctx, bool on);

// The last decode's ticket with the ring on (-1 before any decode, or with it off).
LLAMA_API int64_t llama_output_ticket(const struct llama_context * ctx);

// Row j of the outputs decode `ticket` produced, in the order it produced them, once they landed:
// waits for that decode alone, never for one queued after it. NULL once the ticket is no longer
// held (the decode after next has begun) or j is past its outputs.
LLAMA_API float * llama_ticket_logits(struct llama_context * ctx, int64_t ticket, int32_t j);

// The bytes the lived-routing table holds, for the owner of this context's memory.
LLAMA_API size_t llama_lived_routing_bytes(const struct llama_context * ctx);

Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(LLAMA_CORE_SOURCES
llama-persona-file.cpp
llama-context-opt.cpp
llama-graph-slots.cpp
llama-output-ring.cpp
llama-context-opt-measure.cpp
llama-context-learn.cpp
llama-context-learn-tiles.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,7 @@ int llama_context::decode(const llama_batch_ext & batch_inp) {

// reserve output buffer (none for outputs that only feed a training loss)
const bool host_outputs = opt_host_outputs(); // Continuum
output_ring_swap(); // Continuum: the latest decode's outputs stay readable while this one writes (llama-output-ring.cpp)
if (output_reserve(host_outputs ? n_outputs_all : 0) < (host_outputs ? n_outputs_all : 0)) {
LLAMA_LOG_ERROR("%s: could not reserve space for batch with %d outputs\n", __func__, n_outputs_all);
return -2;
Expand Down Expand Up @@ -2612,6 +2613,7 @@ int llama_context::decode(const llama_batch_ext & batch_inp) {

// set to total number of outputs in the batch, for use in llama_get_logits_ith
n_outputs = host_outputs ? n_outputs_all : 0;
output_ring_record(); // Continuum: this decode's outputs readable by its ticket once they land (llama-output-ring.cpp)

// set output mappings
if (n_outputs > 0) {
Expand Down
7 changes: 7 additions & 0 deletions src/llama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "llama-moe-cache.h"
#include "llama-lived-routing.h"
#include "llama-graph-slots.h"
#include "llama-output-ring.h"

#include "ggml-cpp.h"
#include "ggml-opt.h"
Expand Down Expand Up @@ -252,6 +253,9 @@ struct llama_context {
void set_graph_slots(size_t n); // each mind's decode graph kept across a switch (llama-graph-slots.cpp)
size_t graph_slots_bytes() const;
void set_graph_slot_key(llama_seq_id seq, uint64_t key);
bool set_output_ring(bool on); // Continuum: each decode's outputs readable by ticket (llama-output-ring.cpp)
int64_t output_ticket() const;
float * ticket_logits(int64_t ticket, int32_t j);
llama_lived_routing lived_routing;
struct routing_pending {
std::vector<std::vector<llama_seq_id>> seqs; // per ubatch row
Expand Down Expand Up @@ -566,6 +570,9 @@ struct llama_context {

// Continuum: the minds' decode schedulers, swapped in for a step (llama-graph-slots.cpp)
llama_graph_slots graph_slots;
llama_output_ring output_ring; // Continuum (llama-output-ring.cpp)
void output_ring_swap();
void output_ring_record();
void graph_slot_swap(llama_graph_slot & slot);
void graph_slot_enter(const llama_ubatch & ubatch);
void graph_slot_leave();
Expand Down
1 change: 1 addition & 0 deletions src/llama-graph-slots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void llama_context::graph_slot_enter(const llama_ubatch & ubatch) {
sched_params_cur.max_nodes, sched_params_cur.parallel, sched_params_cur.op_offload));
ggml_backend_sched_set_copy_callback(made->sched.get(), sched_copy_experts, this);
ggml_backend_sched_set_realloc_callback(made->sched.get(), sched_graph_realloc, this);
ggml_backend_sched_use_events(made->sched.get()); // her inputs wait for her own last step, never for every mind's queued one
for (auto & res : made->gf_res_prev) {
res = std::make_unique<llm_graph_result>(sched_params_cur.max_nodes);
}
Expand Down
94 changes: 94 additions & 0 deletions src/llama-output-ring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Continuum: a decode's outputs kept readable while the next decode is queued behind it (llama-output-ring.h)

#include "llama-output-ring.h"

#include "llama-context.h"
#include "llama-model.h"

#include <utility>

bool llama_context::set_output_ring(bool on) {
synchronize();
auto & ring = output_ring;
ring.on = false;
ring.spare.reset();
for (auto & slot : ring.slots) {
slot = llama_output_ticket_slot{};
}
if (!on) {
return true;
}
// the output copies run on the output device's backend, so its events say when they landed
ggml_backend_dev_t dev = model.dev_output();
for (auto & backend : backends) {
if (ggml_backend_get_device(backend.get()) == dev) {
ring.backend = backend.get();
}
}
if (ring.backend == nullptr) {
return false;
}
for (auto & slot : ring.slots) {
slot.event.reset(ggml_backend_event_new(dev));
if (!slot.event) {
return false;
}
}
ring.on = true;
return true;
}

// before a decode reserves its outputs: it writes the spare, so the latest decode's outputs stay where they are and
// become the one before; what the decode before that held is overwritten
void llama_context::output_ring_swap() {
auto & ring = output_ring;
if (!ring.on) {
return;
}
buf_output.swap(ring.spare);
std::swap(ring.slots[0], ring.slots[1]);
ring.slots[0].ticket = -1; // being written
}

// once the decode's output copies are queued
void llama_context::output_ring_record() {
auto & ring = output_ring;
if (!ring.on) {
return;
}
auto & slot = ring.slots[0];
slot.ticket = ring.decodes++;
slot.n_outputs = n_outputs;
slot.logits = logits.data;
ggml_backend_event_record(slot.event.get(), ring.backend);
}

int64_t llama_context::output_ticket() const {
return output_ring.on ? output_ring.slots[0].ticket : -1;
}

float * llama_context::ticket_logits(int64_t ticket, int32_t j) {
for (auto & slot : output_ring.slots) {
if (ticket < 0 || slot.ticket != ticket) {
continue;
}
if (j < 0 || j >= slot.n_outputs || slot.logits == nullptr) {
return nullptr;
}
ggml_backend_event_synchronize(slot.event.get()); // this decode's outputs only, never one queued after it
return slot.logits + (size_t) j * model.vocab.n_tokens();
}
return nullptr;
}

bool llama_set_output_ring(llama_context * ctx, bool on) {
return ctx->set_output_ring(on);
}

int64_t llama_output_ticket(const llama_context * ctx) {
return ctx->output_ticket();
}

float * llama_ticket_logits(llama_context * ctx, int64_t ticket, int32_t j) {
return ctx->ticket_logits(ticket, j);
}
33 changes: 33 additions & 0 deletions src/llama-output-ring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

// Continuum: a decode's outputs kept readable while the next decode is queued behind it.
//
// A context has one output buffer and reads it only after synchronize(), which waits for everything queued, so a
// caller serving several minds could not queue mind B's step until it had read mind A's: on the M5 (Qwen3.6-35B-A3B,
// 2026-11-28) each token was ~5.5 ms of submit with the GPU idle, then ~15.5 ms of GPU, strictly in turn.
//
// With the ring on, the context keeps two output buffers. Each decode takes the spare by pointer swap before it
// reserves its outputs, so the previous decode's outputs stay where they are, and records an event once its own
// output copies are queued. A ticket names one decode: its logits are read after waiting on that decode's event
// alone, so A is sampled while B computes. A ticket is readable until the decode after next begins.

#include "ggml-backend.h"
#include "ggml-cpp.h"

#include <array>
#include <cstdint>

struct llama_output_ticket_slot {
int64_t ticket = -1; // the decode it holds, or -1
int32_t n_outputs = 0;
float * logits = nullptr; // its rows, in the order the decode produced them
ggml_backend_event_ptr event; // recorded once its output copies were queued
};

struct llama_output_ring {
bool on = false;
ggml_backend_t backend = nullptr; // where the output copies run, so where the events are recorded
ggml_backend_buffer_ptr spare; // the previous decode's buffer while the context writes the other
std::array<llama_output_ticket_slot, 2> slots; // [the latest decode, the one before]
int64_t decodes = 0;
};
1 change: 1 addition & 0 deletions tests/continuum-tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ if (NOT WIN32 OR NOT BUILD_SHARED_LIBS)
# hybrid model (-m), on a GPU backend as well as the CPU (a hybrid passed on the CPU while Metal tore its state)
llama_build(test-walk-exact.cpp)
llama_build(test-graph-slots.cpp)
llama_build(test-output-ring.cpp)
llama_build_and_test(test-server-tokens-range.cpp)
target_include_directories(test-server-tokens-range PRIVATE ${PROJECT_SOURCE_DIR}/tools/server ${PROJECT_SOURCE_DIR}/tools/mtmd)
target_link_libraries(test-server-tokens-range PRIVATE server-context)
Expand Down
Loading
Loading