Knobs LLAMA_LOOP_HOST_VIEWS=0 (upstream's shared tensors) and LLAMA_LOOP_FULL_COPY=0 (skip upstream's #32807 full-copy guard, which the views make redundant; set in production). Squashed from 2 fork commits (original subjects and messages below). ---- llama: inkling2_loop - per-pass views of host weights for the later loop passes (was53d2828bb) inkling2_loop runs the core layers (6..17 on Inkling-2-Mini) three times, and post_arch_tensors makes every cache slot share the tensors of its physical layer. With the experts in host memory (--n-cpu-moe, -ot exps=CPU) the scheduler makes one device copy per (host tensor, backend) per graph, at the first split that reads it, and keeps it until the last reader. So: - the expert copies of all 12 loop layers are alive at once (about 6.5 GiB on UD-Q4_K_XL), which is most of the ~7 GiB Vulkan0 compute buffer; - sched_copy_experts runs only for the first reader. When it reads the ids back (ubatches of 32-63 tokens: MUL_MAT_ID is offloaded from 32 tokens, the F1 skip starts at 4*n_expert ids), it uploads only the experts that pass 0 selected, and passes 1 and 2 read stale VRAM for the others (upstream issue #32755, "GGGG" output). Add a post_load_tensors hook to llama_model (called after the weights are loaded, and on the no_alloc path after the dummy buffers are set). In inkling2_loop it replaces, for each slot of a later pass, every tensor in a host buffer with a view of it (name "<name>.visit<pass>", buffer and data of the original). The scheduler hashes tensors by pointer, so each pass gets its own copy, freed after that pass, and the copy callback sees the ids of that pass. Weights in device buffers and CPU extra buffers (CPU_REPACK, not host) stay shared. The pointer-keyed weight maps (w_scales, prec_policy, hdmd.rot) get entries for the views. The views live in a model-owned context outside ctxs_bufs, so the F4 release/restore, tensor counts and the memory breakdown do not see them; they point only at host buffers, which F4 never releases. LLAMA_LOOP_HOST_VIEWS=0 keeps the shared tensors (A/B knob). Not built or run yet. ---- llama: env knob LLAMA_LOOP_FULL_COPY=0 to skip upstream's inkling2_loop full-copy guard (#32807) (was5f88124b3) Upstream #32807 makes sched_copy_experts return false for inkling2_loop, so the scheduler copies every expert of a host weight it shares across the loop passes (the fix for #32755). With the F13 per-pass views each pass has its own device copy and its own ids, so the partial copy (routing readback below the F1 skip threshold, 32-63-token ubatches on Inkling-2-Mini) is correct again and the guard only costs uploads. LLAMA_LOOP_FULL_COPY=0 skips the guard for an A/B; the default keeps upstream's behaviour. With LLAMA_LOOP_HOST_VIEWS=0 the knob brings #32755 back, do not combine them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ
218 lines
9.6 KiB
C++
218 lines
9.6 KiB
C++
#include "models.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
int llama_model_inkling2_loop::layer_phys(int il) const {
|
|
return il < n_phys ? il : core_start + (il - n_phys) % (core_end - core_start);
|
|
}
|
|
|
|
void llama_model_inkling2_loop::load_arch_hparams(llama_model_loader & ml) {
|
|
llama_model_inkling2::load_arch_hparams(ml);
|
|
|
|
ml.get_key(LLM_KV_LOOP_START, core_start);
|
|
ml.get_key(LLM_KV_LOOP_END, core_end);
|
|
ml.get_key(LLM_KV_LOOP_COUNT, n_passes);
|
|
ml.get_key(LLM_KV_LOOP_GATE_RANK, gate_rank);
|
|
ml.get_key(LLM_KV_LOOP_VARYING, varying, false);
|
|
|
|
n_phys = hparams.n_layer();
|
|
const uint32_t block = hparams.attn_res_block_size;
|
|
// a fixed loop needs at least 2 passes, a variable loop at least 1
|
|
if (n_passes < (varying ? 1u : 2u) || !gate_rank || !core_start || core_start >= core_end || core_end > (uint32_t) n_phys ||
|
|
core_start % block || core_end % block || hparams.n_layer_nextn) {
|
|
throw std::runtime_error(format("inkling2_loop: invalid loop core [%u, %u) with block size %u, %u passes or gate rank %u for %d layers",
|
|
core_start, core_end, block, n_passes, gate_rank, n_phys));
|
|
}
|
|
const size_t n_slot = n_phys + (size_t) (n_passes - 1)*(core_end - core_start);
|
|
if (n_slot > LLAMA_MAX_LAYERS) {
|
|
throw std::runtime_error(format("inkling2_loop: %zu cache slots exceed the maximum of %d", n_slot, LLAMA_MAX_LAYERS));
|
|
}
|
|
|
|
// expand to the cache slots before load_tensors() allocates the layers and the memory
|
|
hparams.n_layer_all = n_slot;
|
|
for (int il = n_phys; il < (int) n_slot; ++il) {
|
|
const int p = layer_phys(il);
|
|
hparams.n_head_arr[il] = hparams.n_head_arr[p];
|
|
hparams.n_head_kv_arr[il] = hparams.n_head_kv_arr[p];
|
|
hparams.n_ff_arr[il] = hparams.n_ff_arr[p];
|
|
hparams.n_ff_exp_arr[il] = hparams.n_ff_exp_arr[p];
|
|
hparams.n_expert_used_arr[il] = hparams.n_expert_used_arr[p];
|
|
hparams.is_swa_impl[il] = hparams.is_swa_impl[p];
|
|
hparams.is_recr_impl[il] = hparams.is_recr_impl[p];
|
|
}
|
|
}
|
|
|
|
void llama_model_inkling2_loop::load_arch_tensors(llama_model_loader & ml) {
|
|
llama_model_inkling2::load_arch_tensors(ml);
|
|
|
|
const int64_t n_embd = hparams.n_embd;
|
|
const int64_t rank = gate_rank;
|
|
const llm_tensor down[LOOP_CTRL_COUNT] = { LLM_TENSOR_LOOP_Q_CTRL_DOWN, LLM_TENSOR_LOOP_K_CTRL_DOWN, LLM_TENSOR_LOOP_V_CTRL_DOWN, LLM_TENSOR_LOOP_FFN_CTRL_DOWN };
|
|
const llm_tensor gain[LOOP_CTRL_COUNT] = { LLM_TENSOR_LOOP_Q_CTRL_GAIN, LLM_TENSOR_LOOP_K_CTRL_GAIN, LLM_TENSOR_LOOP_V_CTRL_GAIN, LLM_TENSOR_LOOP_FFN_CTRL_GAIN };
|
|
const llm_tensor forget[LOOP_CTRL_COUNT] = { LLM_TENSOR_LOOP_Q_CTRL_FORGET, LLM_TENSOR_LOOP_K_CTRL_FORGET, LLM_TENSOR_LOOP_V_CTRL_FORGET, LLM_TENSOR_LOOP_FFN_CTRL_FORGET };
|
|
|
|
loop_layers.resize(core_end - core_start);
|
|
for (uint32_t il = core_start; il < core_end; ++il) {
|
|
auto & l = loop_layers[il - core_start];
|
|
// the q, k, v and ffn message queries
|
|
l.msg_query = create_tensor(tn(LLM_TENSOR_LOOP_MSG_QUERY, "weight", il), {n_embd, LOOP_CTRL_COUNT}, 0);
|
|
for (int c = 0; c < LOOP_CTRL_COUNT; ++c) {
|
|
l.ctrl_down[c] = create_tensor(tn(down[c], "weight", il), {2*n_embd, rank}, 0);
|
|
l.ctrl_gain[c] = create_tensor(tn(gain[c], "weight", il), {rank, n_embd}, 0);
|
|
l.ctrl_forget[c] = create_tensor(tn(forget[c], "weight", il), {rank, n_embd}, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
void llama_model_inkling2_loop::post_arch_tensors(llama_model_loader &) {
|
|
// share the physical weights, each cache slot keeps its own KV and conv state
|
|
for (int il = n_phys; il < (int) hparams.n_layer(); ++il) {
|
|
layers[il] = layers[layer_phys(il)];
|
|
}
|
|
}
|
|
|
|
// the scheduler makes one device copy of a host weight per graph, alive from its first to its last use, and the expert copy
|
|
// callback runs at the first use only: give each later pass its own view, so it gets its own copy with the experts of its own ids
|
|
// knob: LLAMA_LOOP_HOST_VIEWS=0 keeps the shared tensors
|
|
void llama_model_inkling2_loop::post_load_tensors(llama_model_loader & ml) {
|
|
const char * env = getenv("LLAMA_LOOP_HOST_VIEWS");
|
|
if (env != nullptr && strcmp(env, "0") == 0) {
|
|
LLAMA_LOG_INFO("%s: LLAMA_LOOP_HOST_VIEWS=0, the loop passes share the host weights\n", __func__);
|
|
return;
|
|
}
|
|
|
|
// llama_layer holds only tensor pointers, see llama_context::opt_init
|
|
static_assert(sizeof(llama_layer) % sizeof(ggml_tensor *) == 0, "llama_layer must hold only tensor pointers");
|
|
const size_t n_ptr = sizeof(llama_layer)/sizeof(ggml_tensor *);
|
|
|
|
const int n_slot = (int) hparams.n_layer();
|
|
const int n_core = (int) (core_end - core_start);
|
|
|
|
// weights on a device are used in place, only host weights are copied
|
|
std::vector<std::vector<ggml_tensor *>> host(n_slot);
|
|
size_t n_views = 0;
|
|
for (int il = n_phys; il < n_slot; ++il) {
|
|
ggml_tensor ** ts = reinterpret_cast<ggml_tensor **>(&layers[il]);
|
|
for (size_t i = 0; i < n_ptr; ++i) {
|
|
ggml_tensor * t = ts[i];
|
|
if (t == nullptr || t->buffer == nullptr || !ggml_backend_buffer_is_host(t->buffer) || (!ml.no_alloc && t->data == nullptr)) {
|
|
continue;
|
|
}
|
|
if (std::find(host[il].begin(), host[il].end(), t) == host[il].end()) {
|
|
host[il].push_back(t);
|
|
}
|
|
}
|
|
n_views += host[il].size();
|
|
}
|
|
if (n_views == 0) {
|
|
return;
|
|
}
|
|
|
|
ggml_init_params init_params = {
|
|
/*.mem_size =*/ n_views*ggml_tensor_overhead(),
|
|
/*.mem_buffer =*/ nullptr,
|
|
/*.no_alloc =*/ true,
|
|
};
|
|
ctx_visits.reset(ggml_init(init_params));
|
|
if (!ctx_visits) {
|
|
throw std::runtime_error("inkling2_loop: failed to create the context of the loop views");
|
|
}
|
|
|
|
size_t n_bytes = 0;
|
|
for (int il = n_phys; il < n_slot; ++il) {
|
|
const int pass = 1 + (il - n_phys)/n_core;
|
|
|
|
std::unordered_map<const ggml_tensor *, ggml_tensor *> view_of;
|
|
for (ggml_tensor * t : host[il]) {
|
|
ggml_tensor * v = ggml_view_tensor(ctx_visits.get(), t);
|
|
ggml_format_name(v, "%s.visit%d", t->name, pass);
|
|
if (ml.no_alloc) {
|
|
// the dummy buffer has no data, it only tells the scheduler that this is a weight
|
|
v->buffer = t->buffer;
|
|
} else if (ggml_backend_view_init(v) != GGML_STATUS_SUCCESS) {
|
|
throw std::runtime_error(format("inkling2_loop: failed to initialize the view %s", v->name));
|
|
}
|
|
view_of[t] = v;
|
|
n_bytes += ggml_nbytes(t);
|
|
|
|
// the per-weight maps are keyed by the tensor
|
|
if (const auto it = w_scales.find(t); it != w_scales.end()) {
|
|
ggml_tensor * s = it->second;
|
|
w_scales.emplace(v, s);
|
|
}
|
|
if (const auto it = prec_policy.prec_acc.find(t); it != prec_policy.prec_acc.end()) {
|
|
const ggml_prec prec = it->second;
|
|
prec_policy.prec_acc.emplace(v, prec);
|
|
}
|
|
if (const auto it = prec_policy.prec_src1.find(t); it != prec_policy.prec_src1.end()) {
|
|
const ggml_prec prec = it->second;
|
|
prec_policy.prec_src1.emplace(v, prec);
|
|
}
|
|
if (const auto it = hdmd.rot.find(t); it != hdmd.rot.end()) {
|
|
const llama_hadamard_transform tr = it->second;
|
|
hdmd.rot.emplace(v, tr);
|
|
}
|
|
}
|
|
|
|
ggml_tensor ** ts = reinterpret_cast<ggml_tensor **>(&layers[il]);
|
|
for (size_t i = 0; i < n_ptr; ++i) {
|
|
if (const auto it = view_of.find(ts[i]); it != view_of.end()) {
|
|
ts[i] = it->second;
|
|
}
|
|
}
|
|
}
|
|
|
|
LLAMA_LOG_INFO("%s: %zu views of host weights for the loop passes after the first (%.2f MiB, not copied)\n",
|
|
__func__, n_views, n_bytes/1024.0/1024.0);
|
|
}
|
|
|
|
std::unique_ptr<llm_graph_context> llama_model_inkling2_loop::build_arch_graph(const llm_graph_params & params) const {
|
|
return std::make_unique<graph>(params);
|
|
}
|
|
|
|
llama_model_inkling2_loop::graph::graph(const llm_graph_params & params) : llama_model_inkling2::graph(params, no_build{}) {
|
|
const auto & m = static_cast<const llama_model_inkling2_loop &>(model);
|
|
const int start = m.core_start;
|
|
const int end = m.core_end;
|
|
|
|
auto * cur = build_inputs();
|
|
std::vector<ggml_tensor *> blocks;
|
|
for (int il = 0; il < start; ++il) {
|
|
cur = build_layer(il, il, cur, blocks);
|
|
}
|
|
|
|
// every pass starts from the prelude output
|
|
auto * prelude = cur;
|
|
const auto prelude_blocks = blocks;
|
|
for (uint32_t pass = 0; pass < m.n_passes; ++pass) {
|
|
// the bank of the previous pass: its blocks after the first core block, and its output
|
|
std::vector<ggml_tensor *> bank;
|
|
ggml_tensor * bank_out = nullptr;
|
|
if (pass > 0) {
|
|
bank.assign(blocks.begin() + prelude_blocks.size() + 1, blocks.end());
|
|
bank_out = cur;
|
|
cur = prelude;
|
|
blocks = prelude_blocks;
|
|
}
|
|
for (int ip = start; ip < end; ++ip) {
|
|
const int il = pass == 0 ? ip : m.n_phys + (pass - 1)*(end - start) + (ip - start);
|
|
if (pass == 0) {
|
|
cur = build_layer(il, ip, cur, blocks);
|
|
continue;
|
|
}
|
|
const auto & loop = m.loop_layers[ip - start];
|
|
cur = build_layer(il, ip, cur, blocks, mix(bank, bank_out, loop.msg_query), &loop);
|
|
}
|
|
}
|
|
|
|
for (int il = end; il < m.n_phys; ++il) {
|
|
cur = build_layer(il, il, cur, blocks);
|
|
}
|
|
build_output(cur, blocks);
|
|
}
|