kv-cache : keep rotation constants on the GPU (#31073)

* kv-cache : keep rotation constants on the GPU

Assisted-by: Codex

* tests : verify resident KV rotation buffers

Assisted-by: Codex

* kv-cache : document remaining multi-device rotation work

Assisted-by: Codex

* kv-cache : support per-layer rotation buffers

Assisted-by: Codex

* kv-cache : fix hybrid SWA rotation inputs and mixed placement check

Assisted-by: Codex

* models : use per-layer rotation in mistral-large4 and dflash

* tests: drop the rotation buffer tests

---------

Co-authored-by: Pascal <admin@serveurperso.com>
This commit is contained in:
Anton IandPascal authored and GitHub committed 2026-10-30 10:15:06 +02:00
1 parent ab3768cdc2
commit fb5997274e
15 files changed
+179 -246

No files matched your search

+72 -60
View File
@@ -81,8 +81,8 @@ llama_kv_cache::llama_kv_cache(
const layer_share_cb & share,
const char * name_tag) :
model(model), hparams(hparams), v_trans(v_trans),
n_seq_max(n_seq_max), n_stream(unified ? 1 : n_seq_max), n_swa(n_swa), swa_type(swa_type),
ctxs_bufs(no_alloc),
n_seq_max(n_seq_max), n_stream(unified ? 1 : n_seq_max), n_swa(n_swa),
attn_rot_bufs(no_alloc), swa_type(swa_type), ctxs_bufs(no_alloc),
other(static_cast<llama_kv_cache *>(mem_other)),
v_cells_impl(other ? other->v_cells_impl : std::make_shared<llama_kv_cells_vec>()),
v_cells(*v_cells_impl) {
@@ -350,7 +350,6 @@ llama_kv_cache::llama_kv_cache(
LLAMA_LOG_INFO("%s: n_rot_v = %u, n_embd_head_k_all = %d\n", __func__, n_rot_v, n_embd_head_v_all);
// pre-compute the haramard matrices and keep them in host memory
// TODO: in the future, we can make copies in the backend buffers to avoid host -> device transfers
if (n_rot_k || n_rot_v) {
for (int64_t n = 64; n <= std::max(n_embd_head_k_all, n_embd_head_v_all); n *= 2) {
attn_rot_hadamard[n] = std::vector<float>(n*n);
@@ -370,10 +369,60 @@ llama_kv_cache::llama_kv_cache(
}
}
init_rotation(name_tag);
const char * LLAMA_KV_CACHE_DEBUG = getenv("LLAMA_KV_CACHE_DEBUG");
debug = LLAMA_KV_CACHE_DEBUG ? atoi(LLAMA_KV_CACHE_DEBUG) : 0;
}
void llama_kv_cache::init_rotation(const char * name_tag) {
if (!(n_rot_k || n_rot_v) || layers.empty()) {
return;
}
std::map<ggml_backend_buffer_type_t, ggml_context_ptr> ctx_map;
for (auto & layer : layers) {
for (bool is_k : { true, false }) {
const uint32_t n_rot = is_k ? n_rot_k : n_rot_v;
auto * cache = is_k ? layer.k : layer.v;
if (!n_rot || !cache) {
continue;
}
const auto buft = ggml_backend_buffer_get_type(cache->buffer);
auto & ctx = ctx_map[buft];
if (!ctx) {
ggml_init_params params = { 2*layers.size()*ggml_tensor_overhead(), nullptr, true };
ctx.reset(ggml_init(params));
if (!ctx) {
throw std::runtime_error("failed to create ggml context for rotation matrices");
}
}
auto * rot = ggml_new_tensor_2d(ctx.get(), GGML_TYPE_F32, n_rot, n_rot);
ggml_format_name(rot, "cache_%s%s_rot_l%d", name_tag, is_k ? "k" : "v", layer.il);
(is_k ? layer.k_rot : layer.v_rot) = rot;
}
}
for (auto & [buft, ctx] : ctx_map) {
auto * buf = attn_rot_bufs.emplace(std::move(ctx), buft, "rotation matrices");
ggml_backend_buffer_set_usage(buf, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
LLAMA_LOG_INFO("%s: %10s rotation buffer size = %8.2f MiB\n", __func__,
ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
}
if (!attn_rot_bufs.no_alloc) {
for (const auto & layer : layers) {
for (auto * t : { layer.k_rot, layer.v_rot }) {
if (t) {
ggml_backend_tensor_set(t, attn_rot_hadamard.at(t->ne[0]).data(), 0, ggml_nbytes(t));
}
}
}
}
}
void llama_kv_cache::clear(bool data) {
for (uint32_t s = 0; s < n_stream; ++s) {
v_cells[s].reset();
@@ -691,7 +740,13 @@ llama_pos llama_kv_cache::seq_pos_max(llama_seq_id seq_id) const {
}
std::map<ggml_backend_buffer_type_t, size_t> llama_kv_cache::memory_breakdown() const {
return ctxs_bufs.memory_breakdown();
auto ret = ctxs_bufs.memory_breakdown();
for (const auto & [buft, size] : attn_rot_bufs.memory_breakdown()) {
ret[buft] += size;
}
return ret;
}
llama_memory_context_ptr llama_kv_cache::init_batch(
@@ -1440,27 +1495,19 @@ ggml_tensor * llama_kv_cache::build_input_v_idxs(ggml_context * ctx, const llama
return v_idxs;
}
ggml_tensor * llama_kv_cache::build_input_k_rot(ggml_context * ctx) const {
ggml_tensor * res = nullptr;
if (n_rot_k) {
res = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_rot_k, n_rot_k);
ggml_set_input(res);
ggml_set_name(res, "attn_inp_k_rot");
std::vector<ggml_tensor *> llama_kv_cache::get_k_rot() const {
std::vector<ggml_tensor *> res(hparams.n_layer_all, nullptr);
for (const auto & [il, idx] : map_layer_ids) {
res[il] = layers[idx].k_rot;
}
return res;
}
ggml_tensor * llama_kv_cache::build_input_v_rot(ggml_context * ctx) const {
ggml_tensor * res = nullptr;
if (n_rot_v) {
res = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_rot_v, n_rot_v);
ggml_set_input(res);
ggml_set_name(res, "attn_inp_v_rot");
std::vector<ggml_tensor *> llama_kv_cache::get_v_rot() const {
std::vector<ggml_tensor *> res(hparams.n_layer_all, nullptr);
for (const auto & [il, idx] : map_layer_ids) {
res[il] = layers[idx].v_rot;
}
return res;
}
@@ -1804,24 +1851,6 @@ void llama_kv_cache::set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch
}
}
void llama_kv_cache::set_input_k_rot(ggml_tensor * dst) const {
GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
const auto n_rot = dst->ne[0];
GGML_ASSERT(attn_rot_hadamard.count(dst->ne[0]));
memcpy(dst->data, attn_rot_hadamard.at(n_rot).data(), ggml_nbytes(dst));
}
void llama_kv_cache::set_input_v_rot(ggml_tensor * dst) const {
GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
const auto n_rot = dst->ne[0];
GGML_ASSERT(attn_rot_hadamard.count(dst->ne[0]));
memcpy(dst->data, attn_rot_hadamard.at(n_rot).data(), ggml_nbytes(dst));
}
bool llama_kv_cache::has_cell_ext() const {
// M-RoPE needs the 2D position, the PLE / Engram n-gram hashes need the token id
return hparams.n_pos_per_embd() > 1 || hparams.ple_n_heads > 0;
@@ -1977,9 +2006,6 @@ public:
ggml_tensor * k_shift; // I32 [kv_size*n_stream]
// note: assumes k_rot^2 == I
ggml_tensor * k_rot = nullptr;
const llama_kv_cache * kv_self;
};
@@ -1989,10 +2015,6 @@ void llm_graph_input_k_shift::set_input(const llama_ubatch * ubatch) {
if (k_shift && k_shift->buffer) {
kv_self->set_input_k_shift(k_shift);
}
if (k_rot && k_rot->buffer) {
kv_self->set_input_k_rot(k_rot);
}
}
ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_context * lctx) const {
@@ -2007,8 +2029,6 @@ ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_co
inp->k_shift = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, (int64_t) get_size()*n_stream);
ggml_set_input(inp->k_shift);
inp->k_rot = build_input_k_rot(ctx);
const auto & cparams = lctx->get_cparams();
for (const auto & layer : layers) {
@@ -2037,7 +2057,7 @@ ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_co
ggml_row_size(layer.k->type, n_embd_k_gqa),
ggml_row_size(layer.k->type, n_embd_nope));
ggml_tensor * cur = build_rope_shift(cparams, ctx, k, inp->k_shift, inp->k_rot, rope_factors, freq_base_l, freq_scale_l, il);
ggml_tensor * cur = build_rope_shift(cparams, ctx, k, inp->k_shift, layer.k_rot, rope_factors, freq_base_l, freq_scale_l, il);
ggml_build_forward_expand(gf, cur);
}
@@ -2898,12 +2918,12 @@ ggml_tensor * llama_kv_cache_context::build_input_v_idxs(ggml_context * ctx, con
return kv->build_input_v_idxs(ctx, ubatch);
}
ggml_tensor * llama_kv_cache_context::build_input_k_rot(ggml_context * ctx) const {
return kv->build_input_k_rot(ctx);
std::vector<ggml_tensor *> llama_kv_cache_context::get_k_rot() const {
return kv->get_k_rot();
}
ggml_tensor * llama_kv_cache_context::build_input_v_rot(ggml_context * ctx) const {
return kv->build_input_v_rot(ctx);
std::vector<ggml_tensor *> llama_kv_cache_context::get_v_rot() const {
return kv->get_v_rot();
}
void llama_kv_cache_context::set_input_k_shift(ggml_tensor * dst) const {
@@ -2926,14 +2946,6 @@ void llama_kv_cache_context::set_input_pos_bucket(ggml_tensor * dst, const llama
kv->set_input_pos_bucket(dst, ubatch);
}
void llama_kv_cache_context::set_input_k_rot(ggml_tensor * dst) const {
kv->set_input_k_rot(dst);
}
void llama_kv_cache_context::set_input_v_rot(ggml_tensor * dst) const {
kv->set_input_v_rot(dst);
}
void llama_kv_cache_context::get_prev_tokens(const llama_ubatch & ubatch, uint32_t n, std::vector<llama_token> & res) const {
kv->get_prev_tokens(ubatch, n, res);
}