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

Method init

subprojects/llama.cpp/src/llama-adapter.cpp:31–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29}
30
31bool llama_adapter_cvec::init(const llama_model & model) {
32 const auto & hparams = model.hparams;
33
34 GGML_ASSERT(tensors.empty());
35 GGML_ASSERT(ctxs.empty());
36 GGML_ASSERT(bufs.empty());
37
38 // create a context for each buffer type
39 std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
40 auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
41 auto it = ctx_map.find(buft);
42 if (it == ctx_map.end()) {
43 ggml_init_params params = {
44 /*.mem_size =*/ hparams.n_layer*ggml_tensor_overhead(),
45 /*.mem_buffer =*/ NULL,
46 /*.no_alloc =*/ true,
47 };
48
49 ggml_context * ctx = ggml_init(params);
50 if (!ctx) {
51 return nullptr;
52 }
53
54 ctx_map[buft] = ctx;
55 ctxs.emplace_back(ctx);
56
57 return ctx;
58 }
59
60 return it->second;
61 };
62
63 // make tensors
64 tensors.reserve(hparams.n_layer);
65 tensors.push_back(nullptr); // there's never a tensor for layer 0
66 for (size_t il = 1; il < hparams.n_layer; il++) {
67 ggml_backend_buffer_type_t buft = model.select_buft(il);
68 ggml_context * ctx = ctx_for_buft(buft);
69 if (!ctx) {
70 LLAMA_LOG_ERROR("%s: failed to allocate context for control vector\n", __func__);
71 return false;
72 }
73 ggml_tensor * tensor = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
74 tensors.push_back(tensor);
75 }
76
77 // allocate tensors / buffers and zero
78 bufs.reserve(ctx_map.size());
79 for (auto it : ctx_map) {
80 ggml_backend_buffer_type_t buft = it.first;
81 ggml_context * ctx = it.second;
82 ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
83 if (!buf) {
84 LLAMA_LOG_ERROR("%s: failed to allocate buffer for control vector\n", __func__);
85 return false;
86 }
87 ggml_backend_buffer_clear(buf, 0);
88 bufs.emplace_back(buf);

Callers

nothing calls this directly

Calls 11

ggml_tensor_overheadFunction · 0.85
ggml_initFunction · 0.85
ggml_new_tensor_1dFunction · 0.85
select_buftMethod · 0.80
emptyMethod · 0.65
findMethod · 0.65
sizeMethod · 0.65
endMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected