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

Method init_batch

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

Source from the content-addressed store, hash-verified

60 )) {}
61
62llama_memory_context_ptr llama_memory_hybrid_iswa::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_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
98 }
99
100 // prepare the attention cache (iswa version returns both base and swa slot infos)
101 auto sinfos_base = mem_attn->get_base()->prepare(ubatches);
102 if (sinfos_base.empty()) {
103 LLAMA_LOG_ERROR("%s: failed to prepare attention base ubatches\n", __func__);
104 return std::make_unique<llama_memory_hybrid_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
105 }
106
107 auto sinfos_swa = mem_attn->get_swa()->prepare(ubatches);
108 if (sinfos_swa.empty()) {
109 LLAMA_LOG_ERROR("%s: failed to prepare attention swa ubatches\n", __func__);
110 return std::make_unique<llama_memory_hybrid_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
111 }
112
113 return std::make_unique<llama_memory_hybrid_iswa_context>(
114 this, std::move(sinfos_base), std::move(sinfos_swa), std::move(ubatches));
115 } while(false);
116
117 return std::make_unique<llama_memory_hybrid_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
118}
119

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected