/src/llama.cpp/src/llama-memory-hybrid.cpp
Line | Count | Source |
1 | | #include "llama-memory-hybrid.h" |
2 | | |
3 | | #include "llama-impl.h" |
4 | | #include "llama-model.h" |
5 | | #include "llama-context.h" |
6 | | |
7 | | // |
8 | | // llama_memory_hybrid |
9 | | // |
10 | | |
11 | | llama_memory_hybrid::llama_memory_hybrid( |
12 | | const llama_model & model, |
13 | | /* attn */ |
14 | | ggml_type type_k, |
15 | | ggml_type type_v, |
16 | | bool v_trans, |
17 | | uint32_t kv_size, |
18 | | uint32_t n_pad, |
19 | | uint32_t n_swa, |
20 | | llama_swa_type swa_type, |
21 | | /* recurrent */ |
22 | | ggml_type type_r, |
23 | | ggml_type type_s, |
24 | | uint32_t rs_size, |
25 | | /* common */ |
26 | | uint32_t n_seq_max, |
27 | | bool offload, |
28 | | bool unified, |
29 | | /* layer filters */ |
30 | | const layer_filter_cb & filter_attn, |
31 | | const layer_filter_cb & filter_recr) : |
32 | 0 | hparams(model.hparams), |
33 | 0 | mem_attn(new llama_kv_cache( |
34 | 0 | model, |
35 | 0 | type_k, |
36 | 0 | type_v, |
37 | 0 | v_trans, |
38 | 0 | offload, |
39 | 0 | unified, |
40 | 0 | kv_size, |
41 | 0 | n_seq_max, |
42 | 0 | n_pad, |
43 | 0 | n_swa, |
44 | 0 | swa_type, |
45 | 0 | filter_attn == nullptr ? |
46 | 0 | [&](int32_t il) { return !hparams.is_recurrent(il); } |
47 | 0 | : filter_attn, |
48 | 0 | nullptr |
49 | 0 | )), |
50 | 0 | mem_recr(new llama_memory_recurrent( |
51 | 0 | model, |
52 | 0 | type_r, |
53 | 0 | type_s, |
54 | 0 | offload, |
55 | 0 | rs_size, |
56 | 0 | n_seq_max, |
57 | 0 | filter_recr == nullptr ? |
58 | 0 | [&](int32_t il) { return hparams.is_recurrent(il); } |
59 | 0 | : filter_recr |
60 | 0 | )) {} |
61 | | |
62 | 0 | llama_memory_context_ptr llama_memory_hybrid::init_batch(llama_batch_allocr & balloc, uint32_t n_ubatch, bool embd_all) { |
63 | 0 | do { |
64 | 0 | balloc.split_reset(); |
65 | | |
66 | | // follow the recurrent pattern for creating the ubatch splits |
67 | 0 | std::vector<llama_ubatch> ubatches; |
68 | |
|
69 | 0 | while (true) { |
70 | 0 | llama_ubatch ubatch; |
71 | |
|
72 | 0 | if (embd_all) { |
73 | | // if all tokens are output, split by sequence |
74 | 0 | ubatch = balloc.split_seq(n_ubatch); |
75 | 0 | } 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 | 0 | ubatch = balloc.split_equal(n_ubatch, true); |
79 | 0 | } |
80 | |
|
81 | 0 | if (ubatch.n_tokens == 0) { |
82 | 0 | break; |
83 | 0 | } |
84 | | |
85 | 0 | ubatches.push_back(std::move(ubatch)); // NOLINT |
86 | 0 | } |
87 | |
|
88 | 0 | if (balloc.get_n_used() < balloc.get_n_tokens()) { |
89 | | // failed to find a suitable split |
90 | 0 | break; |
91 | 0 | } |
92 | | |
93 | | // prepare the recurrent batches first |
94 | 0 | if (!mem_recr->prepare(ubatches)) { |
95 | | // TODO: will the recurrent cache be in an undefined context at this point? |
96 | 0 | LLAMA_LOG_ERROR("%s: failed to prepare recurrent ubatches\n", __func__); |
97 | 0 | return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE); |
98 | 0 | } |
99 | | |
100 | | // prepare the attention cache |
101 | 0 | auto heads_attn = mem_attn->prepare(ubatches); |
102 | 0 | if (heads_attn.empty()) { |
103 | 0 | LLAMA_LOG_ERROR("%s: failed to prepare attention ubatches\n", __func__); |
104 | 0 | return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE); |
105 | 0 | } |
106 | | |
107 | 0 | return std::make_unique<llama_memory_hybrid_context>( |
108 | 0 | this, std::move(heads_attn), std::move(ubatches)); |
109 | 0 | } while(false); |
110 | | |
111 | 0 | return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE); |
112 | 0 | } |
113 | | |
114 | 0 | llama_memory_context_ptr llama_memory_hybrid::init_full() { |
115 | 0 | return std::make_unique<llama_memory_hybrid_context>(this); |
116 | 0 | } |
117 | | |
118 | 0 | llama_memory_context_ptr llama_memory_hybrid::init_update(llama_context * lctx, bool optimize) { |
119 | 0 | return std::make_unique<llama_memory_hybrid_context>(this, lctx, optimize); |
120 | 0 | } |
121 | | |
122 | 0 | bool llama_memory_hybrid::get_can_shift() const { |
123 | | // Shifting is trivially supported for recurrent |
124 | 0 | return mem_attn->get_can_shift(); |
125 | 0 | } |
126 | | |
127 | 0 | void llama_memory_hybrid::clear(bool data) { |
128 | 0 | mem_attn->clear(data); |
129 | 0 | mem_recr->clear(data); |
130 | 0 | } |
131 | | |
132 | 0 | bool llama_memory_hybrid::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { |
133 | | // Try removing from the recurrent cache first since it may fail. If it does |
134 | | // fail, the cache will not have been mutated. |
135 | 0 | if (!mem_recr->seq_rm(seq_id, p0, p1)) { |
136 | 0 | return false; |
137 | 0 | } |
138 | 0 | return mem_attn->seq_rm(seq_id, p0, p1); |
139 | 0 | } |
140 | | |
141 | 0 | void llama_memory_hybrid::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) { |
142 | 0 | mem_attn->seq_cp(seq_id_src, seq_id_dst, p0, p1); |
143 | 0 | mem_recr->seq_cp(seq_id_src, seq_id_dst, p0, p1); |
144 | 0 | } |
145 | | |
146 | 0 | void llama_memory_hybrid::seq_keep(llama_seq_id seq_id) { |
147 | 0 | mem_attn->seq_keep(seq_id); |
148 | 0 | mem_recr->seq_keep(seq_id); |
149 | 0 | } |
150 | | |
151 | 0 | void llama_memory_hybrid::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) { |
152 | 0 | mem_attn->seq_add(seq_id, p0, p1, shift); |
153 | 0 | mem_recr->seq_add(seq_id, p0, p1, shift); |
154 | 0 | } |
155 | | |
156 | 0 | void llama_memory_hybrid::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) { |
157 | 0 | mem_attn->seq_div(seq_id, p0, p1, d); |
158 | 0 | mem_recr->seq_div(seq_id, p0, p1, d); |
159 | 0 | } |
160 | | |
161 | 0 | llama_pos llama_memory_hybrid::seq_pos_min(llama_seq_id seq_id) const { |
162 | | // the min of the total cache is the max of the two caches' min values |
163 | 0 | return std::max(mem_attn->seq_pos_min(seq_id), mem_recr->seq_pos_min(seq_id)); |
164 | 0 | } |
165 | | |
166 | 0 | llama_pos llama_memory_hybrid::seq_pos_max(llama_seq_id seq_id) const { |
167 | | // the max of the total cache is the min of the two caches' max values |
168 | 0 | return std::min(mem_attn->seq_pos_max(seq_id), mem_recr->seq_pos_max(seq_id)); |
169 | 0 | } |
170 | | |
171 | 0 | std::map<ggml_backend_buffer_type_t, size_t> llama_memory_hybrid::memory_breakdown() const { |
172 | 0 | std::map<ggml_backend_buffer_type_t, size_t> mb = mem_attn->memory_breakdown(); |
173 | 0 | for (const auto & buft_size : mem_recr->memory_breakdown()) { |
174 | 0 | mb[buft_size.first] += buft_size.second; |
175 | 0 | } |
176 | 0 | return mb; |
177 | 0 | } |
178 | | |
179 | 0 | void llama_memory_hybrid::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const { |
180 | 0 | if ((flags & LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) == 0) { |
181 | 0 | mem_attn->state_write(io, seq_id, flags); |
182 | 0 | } |
183 | 0 | mem_recr->state_write(io, seq_id, flags); |
184 | 0 | } |
185 | | |
186 | 0 | void llama_memory_hybrid::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) { |
187 | 0 | if ((flags & LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) == 0) { |
188 | 0 | mem_attn->state_read(io, seq_id, flags); |
189 | 0 | } |
190 | 0 | mem_recr->state_read(io, seq_id, flags); |
191 | 0 | } |
192 | | |
193 | 0 | llama_kv_cache * llama_memory_hybrid::get_mem_attn() const { |
194 | 0 | return mem_attn.get(); |
195 | 0 | } |
196 | | |
197 | 0 | llama_memory_recurrent * llama_memory_hybrid::get_mem_recr() const { |
198 | 0 | return mem_recr.get(); |
199 | 0 | } |
200 | | |
201 | 0 | llama_memory_hybrid_context::llama_memory_hybrid_context(llama_memory_status status) : status(status) {} |
202 | | |
203 | | llama_memory_hybrid_context::llama_memory_hybrid_context(llama_memory_hybrid * mem) : |
204 | 0 | ctx_attn(mem->get_mem_attn()->init_full()), |
205 | 0 | ctx_recr(mem->get_mem_recr()->init_full()), |
206 | 0 | status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) { |
207 | 0 | } |
208 | | |
209 | | llama_memory_hybrid_context::llama_memory_hybrid_context( |
210 | | llama_memory_hybrid * mem, |
211 | | llama_context * lctx, |
212 | | bool optimize) : |
213 | 0 | ctx_attn(mem->get_mem_attn()->init_update(lctx, optimize)), |
214 | 0 | ctx_recr(mem->get_mem_recr()->init_update(lctx, optimize)), |
215 | 0 | status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) { |
216 | 0 | } |
217 | | |
218 | | llama_memory_hybrid_context::llama_memory_hybrid_context( |
219 | | llama_memory_hybrid * mem, |
220 | | slot_info_vec_t sinfos_attn, |
221 | | std::vector<llama_ubatch> ubatches) : |
222 | 0 | ubatches(std::move(ubatches)), |
223 | | // note: here we copy the ubatches. not sure if this is ideal |
224 | 0 | ctx_attn(new llama_kv_cache_context(mem->get_mem_attn(), std::move(sinfos_attn), this->ubatches)), |
225 | 0 | ctx_recr(new llama_memory_recurrent_context(mem->get_mem_recr(), this->ubatches)), |
226 | 0 | status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) { |
227 | 0 | } |
228 | | |
229 | 0 | bool llama_memory_hybrid_context::next() { |
230 | 0 | assert(status == LLAMA_MEMORY_STATUS_SUCCESS); |
231 | |
|
232 | 0 | ctx_attn->next(); |
233 | 0 | ctx_recr->next(); |
234 | |
|
235 | 0 | if (++i_next >= ubatches.size()) { |
236 | 0 | return false; |
237 | 0 | } |
238 | | |
239 | 0 | return true; |
240 | 0 | } |
241 | | |
242 | 0 | bool llama_memory_hybrid_context::apply() { |
243 | 0 | assert(!llama_memory_status_is_fail(status)); |
244 | |
|
245 | 0 | bool res = true; |
246 | |
|
247 | 0 | res = res & ctx_attn->apply(); |
248 | 0 | res = res & ctx_recr->apply(); |
249 | |
|
250 | 0 | return res; |
251 | 0 | } |
252 | | |
253 | 0 | llama_memory_status llama_memory_hybrid_context::get_status() const { |
254 | 0 | return status; |
255 | 0 | } |
256 | | |
257 | 0 | const llama_ubatch & llama_memory_hybrid_context::get_ubatch() const { |
258 | 0 | assert(status == LLAMA_MEMORY_STATUS_SUCCESS); |
259 | 0 | return ubatches[i_next]; |
260 | 0 | } |
261 | | |
262 | 0 | const llama_kv_cache_context * llama_memory_hybrid_context::get_attn() const { |
263 | 0 | return static_cast<const llama_kv_cache_context *>(ctx_attn.get()); |
264 | 0 | } |
265 | | |
266 | 0 | const llama_memory_recurrent_context * llama_memory_hybrid_context::get_recr() const { |
267 | 0 | return static_cast<const llama_memory_recurrent_context *>(ctx_recr.get()); |
268 | 0 | } |