#include "llama-moe-cache.h"
#include "llama-impl.h"
#include "llama-memory.h"
#include "llama-model.h"
#include "ggml-cpp.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <unordered_map>
#include <vector>
namespace {
struct moe_cache_lru {
static constexpr int n_class = 8; static constexpr float crf_half_life = 128.0f;
int32_t n_expert = 0;
int32_t n_slots = 0;
int32_t empty = 0;
std::vector<int32_t *> slot_map;
std::vector<int32_t> key_of;
std::vector<int32_t> prev; std::vector<int32_t> next; int32_t head[n_class];
int32_t tail[n_class];
std::vector<int8_t> list_of; int32_t n_filled = 0;
std::vector<float> crf; std::vector<int64_t> last_use; std::vector<int64_t> clock;
std::vector<uint32_t> seen; uint32_t seen_gen = 0;
std::vector<int32_t> uniq; std::vector<int32_t> misses;
std::vector<uint64_t> token_zero;
void init(int32_t n_layer, int32_t n_expert, int32_t n_slots) {
GGML_ASSERT(n_class == (int) std::log2(1.0f/(1.0f - std::exp2(-1.0f/crf_half_life))) + 1);
this->n_expert = n_expert;
this->n_slots = n_slots;
this->empty = n_slots;
slot_map.assign(n_layer, nullptr);
key_of.assign(n_slots, -1);
prev.resize(n_slots);
next.resize(n_slots);
for (int32_t s = 0; s < n_slots; ++s) {
prev[s] = s - 1;
next[s] = s + 1 < n_slots ? s + 1 : -1;
}
for (int c = 0; c < n_class; ++c) {
head[c] = -1;
tail[c] = -1;
}
head[0] = 0;
tail[0] = n_slots - 1;
list_of.assign(n_slots, 0);
crf.assign((size_t) n_layer*n_expert, 0.0f);
last_use.assign((size_t) n_layer*n_expert, -1);
clock.assign(n_layer, 0);
seen.assign(n_expert, 0);
}
void move(int32_t s, int c) {
const int from = list_of[s];
if (from == c && s == tail[from]) {
return;
}
if (prev[s] >= 0) {
next[prev[s]] = next[s];
} else {
head[from] = next[s];
}
if (next[s] >= 0) {
prev[next[s]] = prev[s];
} else {
tail[from] = prev[s];
}
prev[s] = tail[c];
next[s] = -1;
if (tail[c] >= 0) {
next[tail[c]] = s;
} else {
head[c] = s;
}
tail[c] = s;
list_of[s] = c;
}
int32_t make_key(int32_t il, int32_t e) const { return il*n_expert + e; }
int32_t key_layer (int32_t key) const { return key / n_expert; }
int32_t key_expert(int32_t key) const { return key % n_expert; }
int class_of(int32_t key) const {
return std::min(n_class - 1, (int) std::log2(crf[key]));
}
float decayed_crf(int32_t il, int32_t key) const {
return crf[key]*std::exp2(-(clock[il] - last_use[key])/crf_half_life);
}
void use(int32_t il, int32_t key) {
crf[key] = decayed_crf(il, key) + 1.0f;
last_use[key] = clock[il];
}
bool is_selected(int32_t il, int32_t key) const {
if (key < 0) {
return false; }
if (key_layer(key) != il) {
return false; }
return seen[key_expert(key)] == seen_gen;
}
int32_t candidate(int c, int32_t il) const {
for (int32_t s = head[c]; s >= 0; s = next[s]) {
if (!is_selected(il, key_of[s])) {
return s;
}
}
return -1;
}
int32_t victim(int32_t il) const {
int32_t res = -1;
double res_score = -1.0;
for (int c = 0; c < n_class; ++c) {
const int32_t s = candidate(c, il);
if (s < 0) {
continue;
}
const int32_t key = key_of[s];
if (key < 0) {
return s; }
const int64_t age = clock[key_layer(key)] - last_use[key];
const double score = std::ldexp((double) age, -c); if (score > res_score) {
res = s;
res_score = score;
}
}
if (res < 0) {
GGML_ABORT("no MoE cache victim");
}
return res;
}
struct fill {
int32_t expert;
int32_t slot;
};
template <typename F>
bool plan(int32_t il, const int32_t * ids, size_t n_ids, int64_t n_tokens, int32_t n_used, int32_t n_zero, F && n_cpu,
std::vector<fill> & fills, std::vector<fill> & cpu, size_t & n_hit) {
fills.clear();
cpu.clear();
n_hit = 0;
if (++seen_gen == 0) {
std::fill(seen.begin(), seen.end(), 0);
seen_gen = 1;
}
uniq.clear();
for (size_t i = 0; i < n_ids; ++i) {
GGML_ASSERT(ids[i] >= 0 && ids[i] < n_expert);
if (seen[ids[i]] != seen_gen) {
seen[ids[i]] = seen_gen;
uniq.push_back(ids[i]);
}
}
if (uniq.size() > (size_t) n_slots) {
return false;
}
int32_t * slots = slot_map[il];
misses.clear();
for (int32_t e : uniq) {
const int32_t key = make_key(il, e);
use(il, key);
if (slots[e] < n_slots) {
move(slots[e], class_of(key));
n_hit++;
} else {
misses.push_back(e);
}
}
const size_t n_c = std::min(n_cpu(misses.size()), misses.size());
if (n_c > 0) {
std::sort(misses.begin(), misses.end(), [&](int32_t a, int32_t b) {
const float crf_a = crf[make_key(il, a)];
const float crf_b = crf[make_key(il, b)];
return crf_a != crf_b ? crf_a < crf_b : a < b;
});
GGML_ASSERT((size_t) n_tokens*n_used == n_ids);
token_zero.assign(n_tokens, 0);
size_t n_keep = n_c;
for (size_t i = 0; i < n_keep; ) {
const int32_t e = misses[i];
uint64_t used = 0;
for (int64_t it = 0; it < n_tokens; ++it) {
for (int32_t k = 0; k < n_used; ++k) {
if (ids[it*n_used + k] == e) {
used |= token_zero[it];
}
}
}
int32_t z = 0;
while (z < 64 && (used >> z & 1)) {
z++;
}
if (z >= n_zero) {
std::swap(misses[i], misses[--n_keep]);
continue;
}
for (int64_t it = 0; it < n_tokens; ++it) {
for (int32_t k = 0; k < n_used; ++k) {
if (ids[it*n_used + k] == e) {
token_zero[it] |= 1ull << z;
}
}
}
slots[e] = n_slots + z;
cpu.push_back({ e, n_slots + z });
i++;
}
misses.erase(misses.begin(), misses.begin() + n_keep);
}
std::sort(misses.begin(), misses.end());
for (int32_t e : misses) {
const int32_t s = victim(il);
if (key_of[s] >= 0) {
slot_map[key_layer(key_of[s])][key_expert(key_of[s])] = empty;
} else {
n_filled++;
}
const int32_t key = make_key(il, e);
key_of[s] = key;
slots[e] = s;
move(s, class_of(key));
fills.push_back({ e, s });
}
clock[il] += n_tokens;
return true;
}
};
static std::vector<ggml_tensor *> llama_moe_cache_layer_experts(const llama_layer & layer) {
std::vector<ggml_tensor *> res;
for (ggml_tensor * t : { layer.ffn_gate_up_exps, layer.ffn_gate_exps, layer.ffn_up_exps, layer.ffn_down_exps }) {
if (t != nullptr) {
res.push_back(t);
}
}
return res;
}
static bool llama_moe_cache_same_layout(const std::vector<ggml_tensor *> & a, const std::vector<ggml_tensor *> & b) {
if (a.size() != b.size()) {
return false;
}
for (size_t i = 0; i < a.size(); ++i) {
if (a[i]->type != b[i]->type || !ggml_are_same_shape(a[i], b[i]) || a[i]->nb[2] != b[i]->nb[2]) {
return false;
}
}
return true;
}
static bool llama_moe_cache_is_host_weight(const ggml_tensor * t) {
return t->buffer != nullptr &&
ggml_backend_buffer_get_usage(t->buffer) == GGML_BACKEND_BUFFER_USAGE_WEIGHTS &&
ggml_backend_buffer_is_host(t->buffer);
}
}
struct llama_moe_cache::impl {
struct device {
ggml_backend_t backend;
ggml_backend_buffer_type_t buft;
size_t host_bytes = 0; double split = 0.0;
ggml_context_ptr ctx;
ggml_backend_buffer_ptr buf;
size_t buf_size = 0;
};
struct group {
int32_t id; std::vector<ggml_tensor *> ref; std::vector<int32_t> layers;
std::vector<ggml_tensor *> banks; size_t host_bytes = 0;
int32_t n_slots = 0;
moe_cache_lru lru;
};
struct layer {
int32_t ig = -1; ggml_tensor * slot_map = nullptr; ggml_tensor * slot_map_cpu = nullptr; ggml_tensor * cpu_map = nullptr; std::vector<int32_t> cpu_experts; uint32_t cpu_acc = 0; std::vector<ggml_tensor *> experts; };
struct binding {
int32_t il;
int32_t ip; ggml_tensor * cached; };
struct stats {
size_t hits = 0;
size_t misses = 0;
size_t cpu = 0; size_t bytes = 0;
};
static constexpr int64_t max_batch = 32;
int32_t n_expert_used;
stats stats_small; stats stats_large;
stats stats_copy;
std::vector<device> devices;
std::vector<group> groups;
std::vector<layer> layers;
std::unordered_map<const ggml_tensor *, binding> bindings; std::unordered_map<const ggml_tensor *, std::pair<int32_t, bool>> layer_of;
std::vector<int32_t> ids;
std::vector<moe_cache_lru::fill> fills;
std::vector<moe_cache_lru::fill> cpu;
float cpu_share = 0.0f;
uint32_t cpu_share_q = 0; int32_t n_zero = 0;
FILE * trace = nullptr;
ggml_context_ptr ctx_host;
ggml_backend_buffer_ptr buf_host;
size_t buf_host_size = 0;
ggml_context_ptr ctx_views;
impl(const llama_model & model, bool no_alloc, const std::vector<ggml_backend_t> & backends, const std::vector<ggml_backend_buffer_type_t> & bufts, size_t size, float cpu_share) :
n_expert_used(model.hparams.n_expert_used_max()), layers(model.layers.size()), cpu_share(std::clamp(cpu_share, 0.0f, 1.0f)) {
for (size_t i = 0; i < backends.size(); ++i) {
const auto dev_type = ggml_backend_dev_type(ggml_backend_get_device(backends[i]));
if (dev_type == GGML_BACKEND_DEVICE_TYPE_GPU || dev_type == GGML_BACKEND_DEVICE_TYPE_IGPU) {
auto & d = devices.emplace_back();
d.backend = backends[i];
d.buft = bufts[i];
}
}
if (devices.empty()) {
throw std::runtime_error("MoE cache requires a GPU backend");
}
if (model.split_mode() == LLAMA_SPLIT_MODE_TENSOR) {
throw std::runtime_error("MoE cache does not support tensor parallelism");
}
if (model.hparams.n_expert == 0 || n_expert_used == 0) {
throw std::runtime_error("MoE cache requires a MoE model");
}
for (size_t il = 0; il < model.layers.size(); ++il) {
auto experts = llama_moe_cache_layer_experts(model.layers[il]);
if (experts.empty() || !std::all_of(experts.begin(), experts.end(), llama_moe_cache_is_host_weight)) {
continue;
}
const auto it_dev = std::find_if(devices.begin(), devices.end(), [&](const device & d) { return ggml_backend_get_device(d.backend) == model.dev_layer(il); });
if (it_dev == devices.end()) {
continue;
}
const int32_t id = (int32_t) (it_dev - devices.begin());
auto it = std::find_if(groups.begin(), groups.end(), [&](const group & g) { return g.id == id && llama_moe_cache_same_layout(g.ref, experts); });
if (it == groups.end()) {
groups.emplace_back();
it = groups.end() - 1;
it->id = id;
it->ref = experts;
}
it->layers.push_back(il);
for (const ggml_tensor * t : experts) {
it->host_bytes += ggml_nbytes(t);
devices[id].host_bytes += ggml_nbytes(t);
}
}
if (groups.empty()) {
LLAMA_LOG_WARN("%s: no layer has all of its experts in host memory, MoE cache is disabled\n", __func__);
return;
}
n_zero = cpu_share > 0.0f ? std::min<int32_t>(n_expert_used, 64) : 0;
auto alloc_size = [&](const group & g, int32_t n_slots) {
const size_t alignment = ggml_backend_buft_get_alignment(devices[g.id].buft);
size_t res = 0;
for (const ggml_tensor * t : g.ref) {
res += GGML_PAD(t->nb[2]*(n_slots + n_zero + 1), alignment);
}
return res;
};
const float * tensor_split = model.tensor_split();
const bool split_by_free = tensor_split == nullptr ||
std::all_of(tensor_split, tensor_split + model.n_devices(), [](float x) { return x == 0.0f; });
double split_sum = 0.0;
for (device & d : devices) {
if (d.host_bytes == 0) {
continue;
}
ggml_backend_dev_t dev = ggml_backend_get_device(d.backend);
if (split_by_free) {
size_t free;
size_t total;
ggml_backend_dev_memory(dev, &free, &total);
d.split = (double) free;
} else {
const auto it = std::find_if(model.devices.begin(), model.devices.end(), [&](const llama_device & ld) { return ld.dev == dev; });
GGML_ASSERT(it != model.devices.end());
d.split = (double) tensor_split[it - model.devices.begin()];
}
split_sum += d.split;
}
if (split_sum == 0.0) {
for (device & d : devices) {
d.split = d.host_bytes > 0 ? 1.0 : 0.0;
split_sum += d.split;
}
}
cpu_share_q = (uint32_t) std::lround(cpu_share*65536.0f);
if (const char * path = getenv("LLAMA_MOE_CACHE_TRACE"); path != nullptr && *path != '\0') {
trace = fopen(path, "wb");
if (trace == nullptr) {
throw std::runtime_error(std::string("failed to open the MoE cache trace ") + path);
}
write_trace_header(split_sum);
}
std::vector<size_t> n_tensors(devices.size(), 0);
size_t n_tensors_host = 0;
for (group & g : groups) {
const device & d = devices[g.id];
const int32_t n_expert = g.ref[0]->ne[2];
const size_t budget = (size_t) ((double) size*d.split/split_sum*g.host_bytes/d.host_bytes);
const int32_t max_slots = g.layers.size()*n_expert;
while (g.n_slots < max_slots && alloc_size(g, g.n_slots + 1) <= budget) {
g.n_slots++;
}
if (g.n_slots < n_expert_used) {
LLAMA_LOG_WARN("%s: MoE cache budget is too small for %zu layers, they are not cached\n", __func__, g.layers.size());
g.n_slots = 0;
continue;
}
g.lru.init(model.layers.size(), n_expert, g.n_slots);
n_tensors[g.id] += g.ref.size()*(1 + g.layers.size());
n_tensors_host += g.layers.size()*(n_zero > 0 ? 3 : 1); }
if (n_tensors_host == 0) {
throw std::runtime_error("MoE cache is too small to hold the experts of one token");
}
auto init_ctx = [](size_t n_tensors) {
ggml_init_params params = {
n_tensors*ggml_tensor_overhead(),
nullptr,
true,
};
ggml_context_ptr res(ggml_init(params));
if (!res) {
throw std::runtime_error("failed to create the MoE cache context");
}
return res;
};
for (size_t id = 0; id < devices.size(); ++id) {
if (n_tensors[id] > 0) {
devices[id].ctx = init_ctx(n_tensors[id]);
}
}
ctx_host = init_ctx(n_tensors_host);
ctx_views = init_ctx(2);
ggml_backend_buffer_type_t buft_host = ggml_backend_cpu_buffer_type();
const size_t alignment_host = ggml_backend_buft_get_alignment(buft_host);
for (size_t ig = 0; ig < groups.size(); ++ig) {
group & g = groups[ig];
if (g.n_slots == 0) {
continue;
}
ggml_context * ctx = devices[g.id].ctx.get();
for (const ggml_tensor * t : g.ref) {
ggml_tensor * bank = ggml_new_tensor_3d(ctx, t->type, t->ne[0], t->ne[1], g.n_slots + n_zero + 1);
GGML_ASSERT(bank->nb[2] == t->nb[2]);
ggml_format_name(bank, "moe_cache.%zu.%s", ig, t->name);
g.banks.push_back(bank);
}
for (int32_t il : g.layers) {
layer & l = layers[il];
l.ig = (int32_t) ig;
l.experts = llama_moe_cache_layer_experts(model.layers[il]);
for (size_t ip = 0; ip < l.experts.size(); ++ip) {
ggml_tensor * bank = g.banks[ip];
ggml_tensor * cached = ggml_view_3d(ctx, bank, bank->ne[0], bank->ne[1], g.n_slots + n_zero, bank->nb[1], bank->nb[2], 0);
ggml_format_name(cached, "moe_cache.%s", l.experts[ip]->name);
bindings[l.experts[ip]] = { il, (int32_t) ip, cached };
}
l.slot_map = ggml_new_tensor_2d(ctx_host.get(), GGML_TYPE_I32, 1, g.ref[0]->ne[2]);
ggml_format_name(l.slot_map, "moe_cache.slot_map-%d", il);
layer_of[l.slot_map] = { il, false };
buf_host_size += GGML_PAD(ggml_nbytes(l.slot_map), alignment_host);
if (n_zero > 0) {
l.slot_map_cpu = ggml_new_tensor_2d(ctx_host.get(), GGML_TYPE_I32, 1, g.ref[0]->ne[2]);
ggml_format_name(l.slot_map_cpu, "moe_cache.slot_map_cpu-%d", il);
layer_of[l.slot_map_cpu] = { il, true };
buf_host_size += GGML_PAD(ggml_nbytes(l.slot_map_cpu), alignment_host);
l.cpu_map = ggml_new_tensor_2d(ctx_host.get(), GGML_TYPE_I32, 1, g.ref[0]->ne[2]);
ggml_format_name(l.cpu_map, "moe_cache.cpu_map-%d", il);
buf_host_size += GGML_PAD(ggml_nbytes(l.cpu_map), alignment_host);
}
}
devices[g.id].buf_size += alloc_size(g, g.n_slots);
}
for (device & d : devices) {
if (!d.ctx) {
continue;
}
d.buf = llama_memory_bufs::alloc_buf(d.ctx.get(), d.buft, no_alloc, "MoE cache");
if (!no_alloc) {
ggml_backend_buffer_clear(d.buf.get(), 0);
d.buf_size = ggml_backend_buffer_get_size(d.buf.get());
}
}
buf_host = llama_memory_bufs::alloc_buf(ctx_host.get(), buft_host, no_alloc, "MoE cache slot maps");
if (!no_alloc) {
ggml_backend_buffer_clear(buf_host.get(), 0xff); buf_host_size = ggml_backend_buffer_get_size(buf_host.get());
for (group & g : groups) {
for (int32_t il : g.layers) {
if (layers[il].slot_map != nullptr) {
int32_t * slots = (int32_t *) layers[il].slot_map->data;
std::fill(slots, slots + layers[il].slot_map->ne[1], g.lru.empty);
g.lru.slot_map[il] = slots;
}
}
}
}
for (device & d : devices) {
if (d.buf) {
ggml_backend_buffer_set_usage(d.buf.get(), GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
}
}
ggml_backend_buffer_set_usage(buf_host.get(), GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
for (size_t id = 0; id < devices.size(); ++id) {
const device & d = devices[id];
if (d.host_bytes == 0) {
continue;
}
LLAMA_LOG_INFO("%s: %10s MoE cache size = %8.2f MiB for %.2f MiB of host experts, %.0f%% of the misses on the CPU\n", __func__,
ggml_backend_buft_name(d.buft), d.buf_size/1024.0/1024.0, d.host_bytes/1024.0/1024.0, 100.0*cpu_share);
for (const group & g : groups) {
if (g.id == (int32_t) id) {
LLAMA_LOG_INFO("%s: %2zu layers, %s: %5d slots (%.1f%%, %.1f tokens worth)\n", __func__,
g.layers.size(), ggml_type_name(g.ref.back()->type), g.n_slots, 100.0*g.n_slots/(g.layers.size()*g.ref[0]->ne[2]),
(double) g.n_slots/(g.layers.size()*n_expert_used));
}
}
}
}
ggml_backend_t backend(int32_t il) const {
return devices[groups[layers[il].ig].id].backend;
}
void write_trace_header(double split_sum) {
auto u32 = [&](uint32_t v) { fwrite(&v, sizeof(v), 1, trace); };
auto u64 = [&](uint64_t v) { fwrite(&v, sizeof(v), 1, trace); };
auto f64 = [&](double v) { fwrite(&v, sizeof(v), 1, trace); };
fwrite("MCT1", 1, 4, trace);
u32(layers.size());
u32(n_expert_used);
u32(groups.size());
for (const group & g : groups) {
const device & d = devices[g.id];
u32(g.id);
u32(g.ref[0]->ne[2]);
u32(g.layers.size());
for (int32_t il : g.layers) {
u32(il);
}
f64(d.split);
f64(split_sum);
u64(g.host_bytes);
u64(d.host_bytes);
u64(ggml_backend_buft_get_alignment(d.buft));
u32(g.ref.size());
for (const ggml_tensor * t : g.ref) {
u64(t->nb[2]);
}
}
}
void write_trace(int32_t il, int32_t n_used) {
const int32_t rec[3] = { il, n_used, (int32_t) ids.size() };
fwrite(rec, sizeof(int32_t), 3, trace);
fwrite(ids.data(), sizeof(int32_t), ids.size(), trace);
}
~impl() {
log_stats();
if (trace != nullptr) {
fclose(trace);
}
}
ggml_tensor * get_slot_map(int32_t il, int64_t n_tokens, int64_t n_expert_used, bool cpu_part) const {
if (il < 0 || il >= (int32_t) layers.size() || layers[il].ig < 0) {
return nullptr;
}
const layer & l = layers[il];
if (n_tokens == 0 || n_tokens > max_batch || std::min(n_tokens*n_expert_used, l.slot_map->ne[1]) > groups[l.ig].n_slots) {
return nullptr;
}
GGML_ASSERT(!cpu_part || l.slot_map_cpu != nullptr);
return cpu_part ? l.slot_map_cpu : l.slot_map;
}
ggml_tensor * get_cpu_map(int32_t il, int64_t n_tokens) const {
if (il < 0 || il >= (int32_t) layers.size() || layers[il].cpu_map == nullptr) {
return nullptr;
}
ggml_tensor probe = {};
probe.op = GGML_OP_MUL_MAT_ID;
probe.type = GGML_TYPE_F32;
probe.ne[0] = 1;
probe.ne[1] = n_expert_used;
probe.ne[2] = n_tokens;
probe.ne[3] = 1;
for (const device & d : devices) {
if (ggml_backend_offload_op(d.backend, &probe)) {
return nullptr;
}
}
return layers[il].cpu_map;
}
ggml_tensor * get_experts(const ggml_tensor * w) const {
const auto it = bindings.find(w);
return it != bindings.end() ? it->second.cached : nullptr;
}
int64_t copy_experts(ggml_backend_t backend, const ggml_tensor * w, ggml_tensor * dst, int64_t e, int64_t last) {
const auto it = bindings.find(w);
if (it == bindings.end()) {
return 0;
}
const binding & b = it->second;
const group & g = groups[layers[b.il].ig];
if (backend != devices[g.id].backend) {
return 0;
}
const int32_t * slots = g.lru.slot_map[b.il];
if (slots == nullptr || slots[e] >= g.n_slots) {
return 0;
}
int64_t n = 1;
while (e + n <= last && slots[e + n] < g.n_slots && slots[e + n] == slots[e] + n) {
n++;
}
ggml_tensor * bank = g.banks[b.ip];
ggml_reset(ctx_views.get());
ggml_tensor * src_view = ggml_view_3d(ctx_views.get(), bank, bank->ne[0], bank->ne[1], n, bank->nb[1], bank->nb[2], slots[e]*bank->nb[2]);
ggml_tensor * dst_view = ggml_view_3d(ctx_views.get(), dst, dst->ne[0], dst->ne[1], n, dst->nb[1], dst->nb[2], e*dst->nb[2]);
ggml_backend_view_init(src_view);
ggml_backend_view_init(dst_view);
ggml_backend_tensor_copy_async(backend, backend, src_view, dst_view);
stats_copy.hits += n;
stats_copy.bytes += ggml_nbytes(src_view);
return n;
}
bool copy(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst, ggml_cgraph * graph) {
const auto it = layer_of.find(src);
if (it == layer_of.end()) {
return false;
}
const int32_t il = it->second.first;
const bool has_cpu = it->second.second;
layer & l = layers[il];
group & g = groups[l.ig];
GGML_ASSERT(backend == devices[g.id].backend);
const int n_nodes = ggml_graph_n_nodes(graph);
const ggml_tensor * lookup = nullptr;
for (int i = 0; i < n_nodes && lookup == nullptr; ++i) {
const ggml_tensor * node = ggml_graph_node(graph, i);
if (node->op == GGML_OP_GET_ROWS && node->src[0] == dst) {
lookup = node;
}
}
GGML_ASSERT(lookup != nullptr);
const ggml_tensor * sel = lookup->src[1];
for (int i = 0; i < n_nodes; ++i) {
const ggml_tensor * node = ggml_graph_node(graph, i);
if (node == sel || node == sel->view_src) {
GGML_ABORT("the experts of layer %d are selected in the same split as their MoE cache lookup", il);
}
}
GGML_ASSERT(ggml_is_contiguous(sel));
GGML_ASSERT(sel->op == GGML_OP_RESHAPE);
const int32_t n_used = (int32_t) sel->src[0]->ne[0];
ids.resize(ggml_nelements(sel));
ggml_backend_tensor_get_async(backend, sel, ids.data(), 0, ggml_nbytes(sel));
ggml_backend_synchronize(backend);
if (trace != nullptr) {
write_trace(il, n_used);
}
const uint32_t share_q = has_cpu && g.lru.n_filled == g.lru.n_slots ? cpu_share_q : 0;
auto n_cpu = [&](size_t n_miss) {
const uint64_t acc = (uint64_t) n_miss*share_q + l.cpu_acc;
l.cpu_acc = acc & 0xffff;
return (size_t) (acc >> 16);
};
size_t n_hit = 0;
if (!g.lru.plan(il, ids.data(), ids.size(), sel->src[0]->ne[1], n_used, n_zero, n_cpu, fills, cpu, n_hit)) {
GGML_ABORT("the MoE cache is too small for the experts selected in layer %d", il);
}
if (has_cpu) {
int32_t * cpu_map = (int32_t *) l.cpu_map->data;
for (int32_t e : l.cpu_experts) {
cpu_map[e] = -1;
}
l.cpu_experts.clear();
for (const auto & f : cpu) {
cpu_map[f.expert] = f.expert;
l.cpu_experts.push_back(f.expert);
}
}
size_t bytes = 0;
for (size_t ip = 0; ip < l.experts.size(); ++ip) {
const ggml_tensor * w = l.experts[ip];
ggml_tensor * bank = g.banks[ip];
const size_t expert_size = w->nb[2];
for (size_t i = 0; i < fills.size();) {
size_t n = 1;
while (i + n < fills.size() && fills[i + n].expert == fills[i].expert + (int32_t) n && fills[i + n].slot == fills[i].slot + (int32_t) n) {
n++;
}
ggml_backend_tensor_set_async(backend, bank, (const uint8_t *) w->data + fills[i].expert*expert_size, fills[i].slot*expert_size, n*expert_size);
bytes += n*expert_size;
i += n;
}
}
stats & st = ids.size() <= (size_t) 8*n_expert_used ? stats_small : stats_large;
st.hits += n_hit;
st.misses += fills.size() + cpu.size();
st.cpu += cpu.size();
st.bytes += bytes;
ggml_backend_tensor_set_async(backend, dst, l.slot_map->data, 0, ggml_nbytes(l.slot_map));
return true;
}
void log_stats() const {
auto log = [](const char * name, const stats & st) {
const size_t n = st.hits + st.misses;
if (n == 0) {
return;
}
LLAMA_LOG_INFO("llama_moe_cache: %s: hits = %zu, misses = %zu (%zu on the CPU), hit rate = %.2f%%, uploaded = %.2f MiB\n",
name, st.hits, st.misses, st.cpu, 100.0*st.hits/n, st.bytes/1024.0/1024.0);
};
log("ubatch <= 8", stats_small);
log("ubatch > 8", stats_large);
if (stats_copy.hits > 0) {
LLAMA_LOG_INFO("llama_moe_cache: large batches: %zu experts copied from the cache, %.2f MiB\n", stats_copy.hits, stats_copy.bytes/1024.0/1024.0);
}
}
};
llama_moe_cache::llama_moe_cache(const llama_model & model, bool no_alloc, const std::vector<ggml_backend_t> & backends, const std::vector<ggml_backend_buffer_type_t> & bufts, size_t size, float cpu_share) :
pimpl(new impl(model, no_alloc, backends, bufts, size, cpu_share)) {
}
llama_moe_cache::~llama_moe_cache() = default;
ggml_backend_t llama_moe_cache::backend(int32_t il) const {
return pimpl->backend(il);
}
ggml_tensor * llama_moe_cache::get_slot_map(int32_t il, int64_t n_tokens, int64_t n_expert_used, bool cpu_part) const {
return pimpl->get_slot_map(il, n_tokens, n_expert_used, cpu_part);
}
ggml_tensor * llama_moe_cache::get_experts(const ggml_tensor * w) const {
return pimpl->get_experts(w);
}
ggml_tensor * llama_moe_cache::get_cpu_map(int32_t il, int64_t n_tokens) const {
return pimpl->get_cpu_map(il, n_tokens);
}
bool llama_moe_cache::copy(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst, ggml_cgraph * graph) {
return pimpl->copy(backend, src, dst, graph);
}
int64_t llama_moe_cache::copy_experts(ggml_backend_t backend, const ggml_tensor * w, ggml_tensor * dst, int64_t e, int64_t last) {
return pimpl->copy_experts(backend, w, dst, e, last);
}
std::map<ggml_backend_buffer_type_t, size_t> llama_moe_cache::memory_breakdown() const {
std::map<ggml_backend_buffer_type_t, size_t> res;
for (const auto & d : pimpl->devices) {
if (d.buf) {
res[ggml_backend_buffer_get_type(d.buf.get())] += d.buf_size;
}
}
if (pimpl->buf_host) {
res[ggml_backend_buffer_get_type(pimpl->buf_host.get())] += pimpl->buf_host_size;
}
return res;
}