| 4381 | } |
| 4382 | |
| 4383 | static 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 | |
| 4420 | static struct ggml_tensor * llm_build_norm( |
| 4421 | struct ggml_context * ctx, |