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
36 changes: 31 additions & 5 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -2202,15 +2202,41 @@ extern "C" {
// name before anything is decoded (llama_opt_failure). Set before llama_opt_epoch.
LLAMA_API void llama_opt_set_walk_host_budget(struct llama_context * lctx, size_t bytes);

// Replay a lesson's routing as it was lived (Continuum): `ids` holds, for each MoE layer and each
// A mind's routing as she lived it (Continuum; llama-lived-routing.h). On a mixture-of-experts base
// with capture on, serving records each row's chosen experts per sequence, position and layer; a
// live lesson (llama_opt_learn_begin) replays its span's, and on a mixture-of-experts base is
// refused while capture is off. Off always succeeds.
enum llama_routing_capture {
LLAMA_ROUTING_CAPTURE_RECORDING = 0, // on: serving records, lessons replay
LLAMA_ROUTING_CAPTURE_NO_EXPERTS = 1, // a dense base: nothing to record, and lessons need none
LLAMA_ROUTING_CAPTURE_TOO_MANY = 2, // more experts than a u8 names (256): refused
LLAMA_ROUTING_CAPTURE_NO_ROOM = 3, // the capture buffer could not be allocated: refused
LLAMA_ROUTING_CAPTURE_OFF = 4, // turned off
};
LLAMA_API enum llama_routing_capture llama_set_routing_capture(struct llama_context * ctx, bool on);

// Import a span's routing into the table and pin it: `ids` holds, for each MoE layer and each
// position in [pos0, pos0 + n_pos) of sequence `seq`, its n_used chosen experts
// ([n_layer][n_pos][n_used]). Per row: a row of `seq` alone inside the range replays its experts;
// every other row in the same ubatch (a neighbour's, or `seq` outside the range) routes as it
// serves. n_pos 0 clears. Returns false, setting nothing, when the shape is not the model's (its
// layers and experts per token) or an id is not one of its experts.
// ([n_layer][n_pos][n_used]). Per row: a row of `seq` alone inside the span replays its experts;
// every other row in the same ubatch routes as it serves. n_pos 0 clears the pin. Returns false,
// setting nothing, when the shape is not the model's or an id is not one of its experts.
LLAMA_API bool llama_set_pinned_experts(struct llama_context * ctx, llama_seq_id seq, int32_t n_layer, int32_t n_used,
llama_pos pos0, int32_t n_pos, const int32_t * ids);

// Pin a span from what serving recorded; false, pinning nothing, when a position was never served.
LLAMA_API bool llama_pin_lived_routing(struct llama_context * ctx, llama_seq_id seq, llama_pos pos0, int32_t n_pos);

// 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);

// Read a span's lived routing ([n_layer][n_pos][n_used], -1 where serving never routed that layer at
// that position: the last layer runs only a ubatch's outputs): what a sequence paged out carries
// with it. False when a position was never served.
LLAMA_API bool llama_get_lived_routing(struct llama_context * ctx, llama_seq_id seq, llama_pos pos0, int32_t n_pos, int32_t * ids);

// The host microseconds capture has spent so far (queueing copies and writing the table): its price.
LLAMA_API int64_t llama_lived_routing_host_us(const struct llama_context * ctx);

// The host bytes the exact walk's largest window so far kept (0 before one ran, or when the
// walk is not exact): what a run of this shape needs, from the run's own arithmetic.
LLAMA_API size_t llama_opt_walk_host_bytes(struct llama_context * lctx);
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ set(LLAMA_CORE_SOURCES
llama-context-learn.cpp
llama-context-learn-tiles.cpp
llama-walk.cpp
llama-pinned-experts.cpp
llama-lived-routing.cpp
llama-cparams.cpp
llama-grammar.cpp
llama-graph.cpp
Expand Down
19 changes: 19 additions & 0 deletions src/llama-context-learn.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Live-sequence learning: one owner for synchronous calls and resumable serving-loop chunks.
#include "llama-context.h"
#include "llama-model.h"
#include "llama-memory-hybrid.h"
#include "llama-memory-recurrent.h"
#include <algorithm>
Expand Down Expand Up @@ -66,18 +67,32 @@ bool llama_context::opt_learn_begin(llama_seq_id seq, llama_pos n_cached, const
opt_fail("llama_opt_learn: sequence " + std::to_string(seq) + " has a pending recurrent rollback: finish or drop the draft first");
return false;
}
// the reply's rows, and the served tail it restores, replay the routing she served them with
// (llama-lived-routing.h); with capture on, a span she never served is refused by name, never routed afresh
if (model.hparams.n_expert > 0 && !lived_routing.capture) {
opt_fail("llama_opt_learn: a lesson on a mixture-of-experts base replays the routing she served, and this context "
"records none: turn on llama_set_routing_capture before she serves");
return false;
}
if (lived_routing.capture && !pin_lived(seq, n_cached, std::max(n, n_restore))) {
opt_fail("llama_opt_learn: sequence " + std::to_string(seq) + " has positions in [" + std::to_string(n_cached) + ", "
+ std::to_string(n_cached + std::max(n, n_restore)) + ") its serving never routed; a lesson replays only what she lived");
return false;
}
// her stable recurrent state at the served prefix, for the restore
std::vector<uint8_t> snap;
if (recr != nullptr) {
synchronize();
snap.resize(state_seq_get_size(seq, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY));
if (state_seq_get_data(seq, snap.data(), snap.size(), LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) != snap.size()) {
opt_fail("could not capture the recurrent prefix; live learning was not started");
clear_pin();
return false;
}
}
if (attn != nullptr && !attn->seq_rm(seq, n_cached, -1)) {
opt_fail("llama_opt_learn: could not drop sequence " + std::to_string(seq) + "'s cells past " + std::to_string(n_cached));
clear_pin();
return false;
}
auto state = std::make_unique<opt_live_reply>();
Expand Down Expand Up @@ -203,6 +218,7 @@ llama_opt_learn_status llama_context::opt_learn_step() {
if (!restored) {
opt_fail("live reply could not restore its served prefix: re-prefill it");
opt_reply.reset();
clear_pin(); // Continuum: the lesson no longer replays her routing
return LLAMA_OPT_LEARN_FAILED;
}
}
Expand All @@ -212,6 +228,7 @@ llama_opt_learn_status llama_context::opt_learn_step() {
cparams.walk_active = false;
const auto failure = s.failure;
opt_reply.reset();
clear_pin(); // Continuum: the lesson no longer replays her routing
if (!failure.empty()) {
opt_fail(failure.c_str());
return LLAMA_OPT_LEARN_FAILED;
Expand Down Expand Up @@ -263,6 +280,7 @@ llama_opt_learn_status llama_context::opt_learn_step() {
if (s.restoring) {
opt_fail("live reply restoration failed: re-prefill its sequence");
opt_reply.reset();
clear_pin(); // Continuum: the lesson no longer replays her routing
return LLAMA_OPT_LEARN_FAILED;
}
s.failure = opt_failure.empty() ? "live reply training chunk failed" : opt_failure;
Expand All @@ -273,6 +291,7 @@ llama_opt_learn_status llama_context::opt_learn_step() {
if (s.restoring && s.cursor == end) {
const auto failure = s.failure;
opt_reply.reset();
clear_pin(); // Continuum: the lesson no longer replays her routing
if (!failure.empty()) {
opt_fail(failure.c_str());
return LLAMA_OPT_LEARN_FAILED;
Expand Down
2 changes: 2 additions & 0 deletions src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ void llama_context::synchronize() {
}

ggml_backend_sched_synchronize(sched.get());
routing_settle(); // Continuum: the routing copied after compute lands in her table

// FIXME: if multiple single tokens are evaluated without a synchronization,
// the stats will be added to the prompt evaluation stats
Expand Down Expand Up @@ -2440,6 +2441,7 @@ int llama_context::decode(const llama_batch_ext & batch_inp) {
if (opt_retains_graph(mctx, res)) { // Continuum: the live owner keeps the prepared graph
return 0;
}
routing_queue(res, ubatch); // Continuum: what she served, for the lessons that replay it (llama-lived-routing.cpp)

// plot the computation graph in dot format (for debugging purposes)
//if (n_past%100 == 0) {
Expand Down
25 changes: 21 additions & 4 deletions src/llama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "llama-impl.h"
#include "llama-memory.h"
#include "llama-moe-cache.h"
#include "llama-pinned-experts.h"
#include "llama-lived-routing.h"

#include "ggml-cpp.h"
#include "ggml-opt.h"
Expand Down Expand Up @@ -240,9 +240,26 @@ struct llama_context {

llama_context_buffer_bytes buffer_bytes() const;
int32_t memory_rows(llama_context_memory_row * out, int32_t n_max) const; // Continuum, llama-context-api.cpp
bool set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids); // Continuum, llama-pinned-experts.cpp
void drop_reusable_graphs(); // Continuum: no cached graph is reused across a change it cannot see
llama_pinned_experts pinned_experts; // Continuum: a lesson's lived routing (llama-pinned-experts.h)
// Continuum: her routing as lived (llama-lived-routing.h, .cpp)
llama_routing_capture set_routing_capture(bool on);
bool set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids);
bool pin_lived(llama_seq_id seq, llama_pos pos0, int32_t n_pos);
void clear_pin();
void routing_queue(const llm_graph_result * res, const llama_ubatch & ubatch); // after a serving ubatch computes
void routing_settle(); // at synchronize
void drop_reusable_graphs(); // no cached graph is reused across a change it cannot see
llama_lived_routing lived_routing;
struct routing_pending {
std::vector<std::vector<llama_seq_id>> seqs; // per ubatch row
std::vector<llama_pos> pos;
std::vector<uint8_t> out; // per ubatch row: an output row
std::vector<uint8_t> pinned;
uint32_t n_tokens = 0;
std::vector<std::pair<int32_t, int64_t>> layers; // il, rows it holds at rows [il * n_tokens, ...)
std::vector<int32_t> ids; // the buffer's first n_layer * n_tokens rows
};
std::vector<routing_pending> routing_queued; // reused: [0, routing_used) wait for synchronize
size_t routing_used = 0;
llama_memory_breakdown memory_breakdown() const;

//
Expand Down
4 changes: 2 additions & 2 deletions src/llama-cparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct llama_cparams {
uint32_t walk_acc_base = 0;
// a later chunk's gradient sits on the recurrent state this chunk leaves
bool walk_state_surrogate = false;
// a lesson's routing as it was lived, replayed by the MoE graphs that train its rows (llama-pinned-experts.h)
const struct llama_pinned_experts * pinned_experts = nullptr;
// her routing as lived: captured by serving graphs, replayed by the MoE graphs that train its rows; set while capture or a pin is on (llama-lived-routing.h)
const struct llama_lived_routing * lived_routing = nullptr;
uint32_t n_ctx_seq; // context for a single sequence
uint32_t n_batch;
uint32_t n_ubatch;
Expand Down
5 changes: 3 additions & 2 deletions src/llama-graph.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "llama-graph.h"
#include "llama-pinned-experts.h"
#include "llama-lived-routing.h"

#include "llama-impl.h"
#include "llama-model.h"
Expand Down Expand Up @@ -1570,6 +1570,7 @@ void llm_graph_result::reset() {
t_candidates.clear();
t_sampling_all.clear();
t_moe_ids.clear();
t_routing.clear(); // Continuum

params = {};

Expand Down Expand Up @@ -2456,7 +2457,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
selected_experts = ggml_argsort_top_k(ctx0, selection_probs, n_expert_used); // [n_expert_used, n_tokens]
cb(selected_experts->src[0], "ffn_moe_argsort", il);
}
selected_experts = llama_pin_experts(*this, il, selected_experts); // Continuum: a lesson's rows replay the routing she lived
selected_experts = llama_route_experts(*this, il, selected_experts); // Continuum: replay what she lived, capture what she serves
cb(selected_experts, "ffn_moe_topk", il);

if (arch == LLM_ARCH_GROVEMOE && n_expert != hparams.n_expert) {
Expand Down
1 change: 1 addition & 0 deletions src/llama-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ class llm_graph_result {

// [0] is the LLAMA_MOE_RANDOM_ROUTING input, the rest are views of it
std::vector<ggml_tensor *> t_moe_ids;
std::vector<std::pair<int32_t, ggml_tensor *>> t_routing; // Continuum: each MoE layer's selected ids, for capture (llama-lived-routing.h)

std::vector<llm_graph_input_ptr> inputs;
std::vector<llm_graph_fused_node> fused_nodes;
Expand Down
Loading
Loading