MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / llama_kv_cache

Method llama_kv_cache

subprojects/llama.cpp/src/llama-kv-cache.cpp:20–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18//
19
20llama_kv_cache::llama_kv_cache(
21 const llama_model & model,
22 ggml_type type_k,
23 ggml_type type_v,
24 bool v_trans,
25 bool offload,
26 bool unified,
27 uint32_t kv_size,
28 uint32_t n_seq_max,
29 uint32_t n_pad,
30 uint32_t n_swa,
31 llama_swa_type swa_type,
32 const layer_filter_cb & filter,
33 const layer_reuse_cb & reuse) :
34 model(model), hparams(model.hparams), v_trans(v_trans),
35 n_seq_max(n_seq_max), n_stream(unified ? 1 : n_seq_max), n_pad(n_pad), n_swa(n_swa), swa_type(swa_type) {
36
37 GGML_ASSERT(kv_size % n_pad == 0);
38
39 const uint32_t n_layer_kv = hparams.n_layer_kv();
40
41 // define a comparator for the buft -> ctx map to ensure that the order is well-defined:
42 struct ggml_backend_buft_comparator {
43 bool operator()(const ggml_backend_buffer_type_t & lhs, const ggml_backend_buffer_type_t & rhs) const {
44 return strcmp(ggml_backend_buft_name(lhs), ggml_backend_buft_name(rhs)) < 0;
45 }
46 };
47 std::map<ggml_backend_buffer_type_t, ggml_context_ptr, ggml_backend_buft_comparator> ctx_map;
48
49 // create a context for each buffer type
50 auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
51 auto it = ctx_map.find(buft);
52 if (it == ctx_map.end()) {
53 ggml_init_params params = {
54 /*.mem_size =*/ size_t(2u*(1 + n_stream)*n_layer_kv*ggml_tensor_overhead()),
55 /*.mem_buffer =*/ NULL,
56 /*.no_alloc =*/ true,
57 };
58
59 ggml_context * ctx = ggml_init(params);
60 if (!ctx) {
61 return nullptr;
62 }
63
64 ctx_map.emplace(buft, ctx);
65
66 return ctx;
67 }
68
69 return it->second.get();
70 };
71
72 GGML_ASSERT(n_stream == 1 || n_stream == n_seq_max);
73
74 v_heads.resize(n_stream);
75 for (uint32_t s = 0; s < n_stream; ++s) {
76 v_heads[s] = 0;
77 }

Callers

nothing calls this directly

Calls 15

ggml_tensor_overheadFunction · 0.85
ggml_initFunction · 0.85
ggml_backend_dev_nameFunction · 0.85
ggml_new_tensor_3dFunction · 0.85
ggml_format_nameFunction · 0.85
ggml_view_2dFunction · 0.85
ggml_get_first_tensorFunction · 0.85
ggml_get_next_tensorFunction · 0.85

Tested by

no test coverage detected