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

Method init_batch

subprojects/llama.cpp/src/llama-memory-hybrid.cpp:62–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60 )) {}
61
62llama_memory_context_ptr llama_memory_hybrid::init_batch(llama_batch_allocr & balloc, uint32_t n_ubatch, bool embd_all) {
63 do {
64 balloc.split_reset();
65
66 // follow the recurrent pattern for creating the ubatch splits
67 std::vector<llama_ubatch> ubatches;
68
69 while (true) {
70 llama_ubatch ubatch;
71
72 if (embd_all) {
73 // if all tokens are output, split by sequence
74 ubatch = balloc.split_seq(n_ubatch);
75 } else {
76 // TODO: non-sequential equal split can be done if using unified KV cache
77 // for simplicity, we always use sequential equal split for now
78 ubatch = balloc.split_equal(n_ubatch, true);
79 }
80
81 if (ubatch.n_tokens == 0) {
82 break;
83 }
84
85 ubatches.push_back(std::move(ubatch)); // NOLINT
86 }
87
88 if (balloc.get_n_used() < balloc.get_n_tokens()) {
89 // failed to find a suitable split
90 break;
91 }
92
93 // prepare the recurrent batches first
94 if (!mem_recr->prepare(ubatches)) {
95 // TODO: will the recurrent cache be in an undefined context at this point?
96 LLAMA_LOG_ERROR("%s: failed to prepare recurrent ubatches\n", __func__);
97 return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
98 }
99
100 // prepare the attention cache
101 auto heads_attn = mem_attn->prepare(ubatches);
102 if (heads_attn.empty()) {
103 LLAMA_LOG_ERROR("%s: failed to prepare attention ubatches\n", __func__);
104 return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
105 }
106
107 return std::make_unique<llama_memory_hybrid_context>(
108 this, std::move(heads_attn), std::move(ubatches));
109 } while(false);
110
111 return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
112}
113
114llama_memory_context_ptr llama_memory_hybrid::init_full() {
115 return std::make_unique<llama_memory_hybrid_context>(this);

Callers

nothing calls this directly

Calls 8

split_resetMethod · 0.80
split_seqMethod · 0.80
split_equalMethod · 0.80
get_n_usedMethod · 0.80
get_n_tokensMethod · 0.80
emptyMethod · 0.65
push_backMethod · 0.45
prepareMethod · 0.45

Tested by

no test coverage detected