Coverage Report

Created: 2026-02-26 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/llama-memory-hybrid-iswa.cpp
Line
Count
Source
1
#include "llama-memory-hybrid-iswa.h"
2
3
#include "llama-impl.h"
4
#include "llama-model.h"
5
#include "llama-context.h"
6
7
//
8
// llama_memory_hybrid_iswa
9
//
10
11
llama_memory_hybrid_iswa::llama_memory_hybrid_iswa(
12
        const llama_model & model,
13
                            /* attn */
14
                ggml_type   type_k,
15
                ggml_type   type_v,
16
                     bool   v_trans,
17
                     bool   swa_full,
18
                 uint32_t   kv_size,
19
                 uint32_t   n_ubatch,
20
                 uint32_t   n_pad,
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_iswa(
34
0
        model,
35
0
        type_k,
36
0
        type_v,
37
0
        v_trans,
38
0
        offload,
39
0
        swa_full,
40
0
        unified,
41
0
        kv_size,
42
0
        n_seq_max,
43
0
        n_ubatch,
44
0
        n_pad,
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_iswa::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_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
98
0
        }
99
100
        // prepare the attention cache (iswa version returns both base and swa slot infos)
101
0
        auto sinfos_base = mem_attn->get_base()->prepare(ubatches);
102
0
        if (sinfos_base.empty()) {
103
0
            LLAMA_LOG_ERROR("%s: failed to prepare attention base ubatches\n", __func__);
104
0
            return std::make_unique<llama_memory_hybrid_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
105
0
        }
106
107
0
        auto sinfos_swa = mem_attn->get_swa()->prepare(ubatches);
108
0
        if (sinfos_swa.empty()) {
109
0
            LLAMA_LOG_ERROR("%s: failed to prepare attention swa ubatches\n", __func__);
110
0
            return std::make_unique<llama_memory_hybrid_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
111
0
        }
112
113
0
        return std::make_unique<llama_memory_hybrid_iswa_context>(
114
0
                this, std::move(sinfos_base), std::move(sinfos_swa), std::move(ubatches));
115
0
    } while(false);
116
117
0
    return std::make_unique<llama_memory_hybrid_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
118
0
}
119
120
0
llama_memory_context_ptr llama_memory_hybrid_iswa::init_full() {
121
0
    return std::make_unique<llama_memory_hybrid_iswa_context>(this);
122
0
}
123
124
0
llama_memory_context_ptr llama_memory_hybrid_iswa::init_update(llama_context * lctx, bool optimize) {
125
0
    return std::make_unique<llama_memory_hybrid_iswa_context>(this, lctx, optimize);
126
0
}
127
128
0
bool llama_memory_hybrid_iswa::get_can_shift() const {
129
    // Shifting is trivially supported for recurrent
130
0
    return mem_attn->get_can_shift();
131
0
}
132
133
0
void llama_memory_hybrid_iswa::clear(bool data) {
134
0
    mem_attn->clear(data);
135
0
    mem_recr->clear(data);
136
0
}
137
138
0
bool llama_memory_hybrid_iswa::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
139
    // Try removing from the recurrent cache first since it may fail. If it does
140
    // fail, the cache will not have been mutated.
141
0
    if (!mem_recr->seq_rm(seq_id, p0, p1)) {
142
0
        return false;
143
0
    }
144
0
    return mem_attn->seq_rm(seq_id, p0, p1);
145
0
}
146
147
0
void llama_memory_hybrid_iswa::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
148
0
    mem_attn->seq_cp(seq_id_src, seq_id_dst, p0, p1);
149
0
    mem_recr->seq_cp(seq_id_src, seq_id_dst, p0, p1);
150
0
}
151
152
0
void llama_memory_hybrid_iswa::seq_keep(llama_seq_id seq_id) {
153
0
    mem_attn->seq_keep(seq_id);
154
0
    mem_recr->seq_keep(seq_id);
155
0
}
156
157
0
void llama_memory_hybrid_iswa::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
158
0
    mem_attn->seq_add(seq_id, p0, p1, shift);
159
0
    mem_recr->seq_add(seq_id, p0, p1, shift);
160
0
}
161
162
0
void llama_memory_hybrid_iswa::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
163
0
    mem_attn->seq_div(seq_id, p0, p1, d);
164
0
    mem_recr->seq_div(seq_id, p0, p1, d);
165
0
}
166
167
0
llama_pos llama_memory_hybrid_iswa::seq_pos_min(llama_seq_id seq_id) const {
168
    // the min of the total cache is the max of the two caches' min values
169
0
    return std::max(mem_attn->seq_pos_min(seq_id), mem_recr->seq_pos_min(seq_id));
170
0
}
171
172
0
llama_pos llama_memory_hybrid_iswa::seq_pos_max(llama_seq_id seq_id) const {
173
    // the max of the total cache is the min of the two caches' max values
174
0
    return std::min(mem_attn->seq_pos_max(seq_id), mem_recr->seq_pos_max(seq_id));
175
0
}
176
177
0
std::map<ggml_backend_buffer_type_t, size_t> llama_memory_hybrid_iswa::memory_breakdown() const {
178
0
    std::map<ggml_backend_buffer_type_t, size_t> mb = mem_attn->memory_breakdown();
179
0
    for (const auto & buft_size : mem_recr->memory_breakdown()) {
180
0
        mb[buft_size.first] += buft_size.second;
181
0
    }
182
0
    return mb;
183
0
}
184
185
0
void llama_memory_hybrid_iswa::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const {
186
0
    mem_attn->state_write(io, seq_id, flags);
187
0
    mem_recr->state_write(io, seq_id, flags);
188
0
}
189
190
0
void llama_memory_hybrid_iswa::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) {
191
0
    mem_attn->state_read(io, seq_id, flags);
192
0
    mem_recr->state_read(io, seq_id, flags);
193
0
}
194
195
0
llama_kv_cache_iswa * llama_memory_hybrid_iswa::get_mem_attn() const {
196
0
    return mem_attn.get();
197
0
}
198
199
0
llama_memory_recurrent * llama_memory_hybrid_iswa::get_mem_recr() const {
200
0
    return mem_recr.get();
201
0
}
202
203
//
204
// llama_memory_hybrid_iswa_context
205
//
206
207
0
llama_memory_hybrid_iswa_context::llama_memory_hybrid_iswa_context(llama_memory_status status) : status(status) {}
208
209
llama_memory_hybrid_iswa_context::llama_memory_hybrid_iswa_context(llama_memory_hybrid_iswa * mem) :
210
0
    ctx_attn(mem->get_mem_attn()->init_full()),
211
0
    ctx_recr(mem->get_mem_recr()->init_full()),
212
0
    status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
213
0
}
214
215
llama_memory_hybrid_iswa_context::llama_memory_hybrid_iswa_context(
216
        llama_memory_hybrid_iswa * mem,
217
                   llama_context * lctx,
218
                            bool   optimize) :
219
0
    ctx_attn(mem->get_mem_attn()->init_update(lctx, optimize)),
220
0
    ctx_recr(mem->get_mem_recr()->init_update(lctx, optimize)),
221
0
    status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
222
0
}
223
224
llama_memory_hybrid_iswa_context::llama_memory_hybrid_iswa_context(
225
           llama_memory_hybrid_iswa * mem,
226
                    slot_info_vec_t   sinfos_base,
227
                    slot_info_vec_t   sinfos_swa,
228
          std::vector<llama_ubatch>   ubatches) :
229
0
    ubatches(std::move(ubatches)),
230
    // note: here we copy the ubatches. not sure if this is ideal
231
0
    ctx_attn(new llama_kv_cache_iswa_context(mem->get_mem_attn(), std::move(sinfos_base), std::move(sinfos_swa), this->ubatches)),
232
0
    ctx_recr(new llama_memory_recurrent_context(mem->get_mem_recr(), this->ubatches)),
233
0
    status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
234
0
}
235
236
0
bool llama_memory_hybrid_iswa_context::next() {
237
0
    assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
238
239
0
    ctx_attn->next();
240
0
    ctx_recr->next();
241
242
0
    if (++i_next >= ubatches.size()) {
243
0
        return false;
244
0
    }
245
246
0
    return true;
247
0
}
248
249
0
bool llama_memory_hybrid_iswa_context::apply() {
250
0
    assert(!llama_memory_status_is_fail(status));
251
252
0
    bool res = true;
253
254
0
    res = res & ctx_attn->apply();
255
0
    res = res & ctx_recr->apply();
256
257
0
    return res;
258
0
}
259
260
0
llama_memory_status llama_memory_hybrid_iswa_context::get_status() const {
261
0
    return status;
262
0
}
263
264
0
const llama_ubatch & llama_memory_hybrid_iswa_context::get_ubatch() const {
265
0
    assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
266
0
    return ubatches[i_next];
267
0
}
268
269
0
const llama_kv_cache_iswa_context * llama_memory_hybrid_iswa_context::get_attn() const {
270
0
    return static_cast<const llama_kv_cache_iswa_context *>(ctx_attn.get());
271
0
}
272
273
0
const llama_memory_recurrent_context * llama_memory_hybrid_iswa_context::get_recr() const {
274
0
    return static_cast<const llama_memory_recurrent_context *>(ctx_recr.get());
275
0
}