MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / llm_build_kv_store

Function llm_build_kv_store

llama.cpp:4383–4418  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4381}
4382
4383static std::pair<ggml_tensor*, ggml_tensor*> llm_build_kv_store(
4384 struct ggml_context * ctx,
4385 const llama_hparams & hparams,
4386 const llama_kv_cache & kv,
4387 struct ggml_cgraph * graph,
4388 struct ggml_tensor * k_cur,
4389 struct ggml_tensor * v_cur,
4390 int64_t n_ctx,
4391 int32_t n_tokens,
4392 int32_t kv_head,
4393 const llm_build_cb & cb,
4394 int64_t il) {
4395 const int64_t n_embd_gqa = hparams.n_embd_gqa();
4396
4397 // compute the transposed [n_tokens, n_embd] V matrix
4398 struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, n_embd_gqa, n_tokens));
4399 //struct ggml_tensor * v_cur_t = ggml_transpose(ctx, v_cur); // TODO: reshape above is likely not needed
4400 cb(v_cur_t, "v_cur_t", il);
4401
4402 struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, kv.k, n_tokens*n_embd_gqa,
4403 (ggml_element_size(kv.k)*n_embd_gqa)*(il*n_ctx + kv_head));
4404 cb(k_cache_view, "k_cache_view", il);
4405
4406 struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, kv.v, n_tokens, n_embd_gqa,
4407 ( n_ctx)*ggml_element_size(kv.v),
4408 (il*n_ctx)*ggml_element_size(kv.v)*n_embd_gqa + kv_head*ggml_element_size(kv.v));
4409 cb(v_cache_view, "v_cache_view", il);
4410
4411 // important: storing RoPE-ed version of K in the KV cache!
4412 ggml_tensor * k_cpy = ggml_cpy(ctx, k_cur, k_cache_view);
4413 ggml_tensor * v_cpy = ggml_cpy(ctx, v_cur_t, v_cache_view);
4414 //ggml_build_forward_expand(graph, ggml_cpy(ctx, k_cur, k_cache_view));
4415 //ggml_build_forward_expand(graph, ggml_cpy(ctx, v_cur_t, v_cache_view));
4416
4417 return {k_cpy, v_cpy};
4418}
4419
4420static struct ggml_tensor * llm_build_norm(
4421 struct ggml_context * ctx,

Callers 12

build_llama_variantsMethod · 0.85
build_optMethod · 0.85
build_baichuanMethod · 0.85
build_falconMethod · 0.85
build_starcoderMethod · 0.85
build_persimmonMethod · 0.85
build_refactMethod · 0.85
build_bloomMethod · 0.85
build_mptMethod · 0.85
build_stablelmMethod · 0.85
build_graphMethod · 0.85
build_graphMethod · 0.85

Calls 8

cbFunction · 0.85
ggml_transposeFunction · 0.70
ggml_reshape_2dFunction · 0.70
ggml_view_1dFunction · 0.70
ggml_element_sizeFunction · 0.70
ggml_view_2dFunction · 0.70
ggml_cpyFunction · 0.70
n_embd_gqaMethod · 0.45

Tested by 2

build_graphMethod · 0.68
build_graphMethod · 0.68