Skip to content
Merged
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
9 changes: 9 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,15 @@ 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
// 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.
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);

// 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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(LLAMA_CORE_SOURCES
llama-context-learn.cpp
llama-context-learn-tiles.cpp
llama-walk.cpp
llama-pinned-experts.cpp
llama-cparams.cpp
llama-grammar.cpp
llama-graph.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/llama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "llama-impl.h"
#include "llama-memory.h"
#include "llama-moe-cache.h"
#include "llama-pinned-experts.h"

#include "ggml-cpp.h"
#include "ggml-opt.h"
Expand Down Expand Up @@ -239,6 +240,9 @@ 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)
llama_memory_breakdown memory_breakdown() const;

//
Expand Down
2 changes: 2 additions & 0 deletions src/llama-cparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +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;
uint32_t n_ctx_seq; // context for a single sequence
uint32_t n_batch;
uint32_t n_ubatch;
Expand Down
2 changes: 2 additions & 0 deletions src/llama-graph.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "llama-graph.h"
#include "llama-pinned-experts.h"

#include "llama-impl.h"
#include "llama-model.h"
Expand Down Expand Up @@ -2455,6 +2456,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
cb(selected_experts, "ffn_moe_topk", il);

if (arch == LLM_ARCH_GROVEMOE && n_expert != hparams.n_expert) {
Expand Down
108 changes: 108 additions & 0 deletions src/llama-pinned-experts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "llama-pinned-experts.h"

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

#include <cstring>

bool llama_pinned_experts::pinned(const llama_ubatch & ubatch, uint32_t i) const {
return n_pos > 0 && ubatch.n_seq_id[i] == 1 && ubatch.seq_id[i][0] == seq &&
ubatch.pos[i] >= pos0 && ubatch.pos[i] < pos0 + n_pos;
}

const int32_t * llama_pinned_experts::at(int32_t il, llama_pos pos) const {
return ids.data() + ((size_t) il * (size_t) n_pos + (size_t) (pos - pos0)) * (size_t) n_used;
}

void llm_graph_input_pinned_experts::set_input(const llama_ubatch * ubatch) {
GGML_ASSERT(ggml_backend_buffer_is_host(ids->buffer) && ggml_backend_buffer_is_host(mask->buffer));
int32_t * id = (int32_t *) ids->data;
float * m = (float *) mask->data;
const bool every_row = ids->ne[1] == (int64_t) ubatch->n_tokens;
GGML_ASSERT(every_row || ubatch->output);
int64_t row = 0;
for (uint32_t i = 0; i < ubatch->n_tokens; ++i) {
if (!every_row && !ubatch->output[i]) {
continue;
}
const bool p = pinned.pinned(*ubatch, i);
m[row] = p ? 1.0f : 0.0f;
if (p) {
std::memcpy(id + (size_t) row * pinned.n_used, pinned.at(il, ubatch->pos[i]), sizeof(int32_t) * pinned.n_used);
} else {
std::memset(id + (size_t) row * pinned.n_used, 0, sizeof(int32_t) * pinned.n_used);
}
++row;
}
GGML_ASSERT(row == ids->ne[1]);
}

bool llm_graph_input_pinned_experts::can_reuse(const llm_graph_params & params) {
// the same pin is still set (setting or clearing one also drops the cached graphs)
return params.cparams.pinned_experts == &pinned;
}

ggml_tensor * llama_pin_experts(const llm_graph_context & graph, int32_t il, ggml_tensor * routed) {
const llama_pinned_experts * pinned = graph.cparams.pinned_experts;
// While a pin is set, every MoE layer carries it, covered rows or not: the graph's shape does not
// depend on which rows a ubatch covers, so a reused graph never skips a pin (Codex on #167); its
// input sets each row's mask per ubatch.
if (pinned == nullptr || pinned->n_used != routed->ne[0] || il >= pinned->n_layer) {
return routed;
}
const int64_t n_used = routed->ne[0], n_rows = routed->ne[1];
auto inp = std::make_unique<llm_graph_input_pinned_experts>(*pinned, il);
inp->ids = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_I32, n_used, n_rows);
inp->mask = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_F32, 1, n_rows);
ggml_set_input(inp->ids);
ggml_set_input(inp->mask);
ggml_tensor * ids = inp->ids;
ggml_tensor * mask = inp->mask;
graph.res->add_input(std::move(inp));
// per row: routed + mask * (pinned - routed), in F32 (expert ids are exact far below 2^24)
ggml_context * ctx = graph.ctx0;
ggml_tensor * r = ggml_cast(ctx, routed, GGML_TYPE_F32);
ggml_tensor * p = ggml_cast(ctx, ids, GGML_TYPE_F32);
ggml_tensor * mixed = ggml_add(ctx, r, ggml_mul(ctx, ggml_sub(ctx, p, r), mask));
return ggml_cast(ctx, mixed, GGML_TYPE_I32);
}

bool llama_context::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) {
if (n_pos == 0) {
pinned_experts = {};
cparams.pinned_experts = nullptr;
drop_reusable_graphs();
return true;
}
const auto & hp = model.hparams;
if (ids == nullptr || seq < 0 || n_pos < 0 || n_layer != (int32_t) hp.n_layer() || n_used != (int32_t) hp.n_expert_used()) {
return false;
}
const size_t n = (size_t) n_layer * (size_t) n_pos * (size_t) n_used;
for (size_t k = 0; k < n; ++k) {
if (ids[k] < 0 || ids[k] >= (int32_t) hp.n_expert) {
return false; // not an expert of this model
}
}
pinned_experts.seq = seq;
pinned_experts.n_layer = n_layer;
pinned_experts.n_used = n_used;
pinned_experts.pos0 = pos0;
pinned_experts.n_pos = n_pos;
pinned_experts.ids.assign(ids, ids + n);
cparams.pinned_experts = &pinned_experts;
drop_reusable_graphs(); // a graph built without this pin has no input to carry it
return true;
}

void llama_context::drop_reusable_graphs() {
for (auto & res : gf_res_prev) {
res->reset();
}
gf_res_prev_active = nullptr;
}

bool llama_set_pinned_experts(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) {
return ctx && ctx->set_pinned_experts(seq, n_layer, n_used, pos0, n_pos, ids);
}
51 changes: 51 additions & 0 deletions src/llama-pinned-experts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

// A lesson's routing as it was lived (Continuum): each position's chosen experts, per layer, for ONE
// sequence, so a mixture-of-experts graph that trains those rows replays them instead of routing
// again. A near-tie in a row's top-k picks a different expert when the arithmetic differs slightly (a
// tile of 64 rows against one of 512, CPU against CUDA), and that discrete choice turns a 1e-4
// difference into a different function; replaying the lived choice trains the function that was
// served. The pin is per row: a row of the pinned sequence inside its range replays, every other row
// (a neighbour's, or the lesson's own outside the range) routes as it serves, in the same ubatch.

#include "llama.h"
#include "llama-graph.h"

#include <cstdint>
#include <vector>

struct llama_pinned_experts {
llama_seq_id seq = -1;
int32_t n_layer = 0;
int32_t n_used = 0;
llama_pos pos0 = 0;
int32_t n_pos = 0;
// [layer][position - pos0][n_used]
std::vector<int32_t> ids;

// row i of `ubatch` replays: it belongs to the pinned sequence alone and its position is covered
bool pinned(const llama_ubatch & ubatch, uint32_t i) const;
const int32_t * at(int32_t il, llama_pos pos) const;
};

// The graph input that carries, for the rows a layer sees, each pinned row's experts and a mask (1
// pinned, 0 routed). Rows are the ubatch's, or after the last layer's cut only its outputs, in ubatch
// order (as out_ids keeps them).
class llm_graph_input_pinned_experts : public llm_graph_input_i {
public:
llm_graph_input_pinned_experts(const llama_pinned_experts & pinned, int32_t il) : pinned(pinned), il(il) {}
virtual ~llm_graph_input_pinned_experts() = default;

void set_input(const llama_ubatch * ubatch) override;
bool can_reuse(const llm_graph_params & params) override;

ggml_tensor * ids = nullptr; // I32 [n_used, n_rows], 0 on a routed row
ggml_tensor * mask = nullptr; // F32 [1, n_rows]

const llama_pinned_experts & pinned;
const int32_t il;
};

// `routed` ([n_used, n_rows], the router's choice) with every pinned row of this graph replaced by its
// lived choice; `routed` itself when no row of the ubatch is pinned.
ggml_tensor * llama_pin_experts(const llm_graph_context & graph, int32_t il, ggml_tensor * routed);
Loading
Loading