char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenmodel: add dots3-note (#27060)5a32f7b66

char/aliceai-t5-moe
4.1 KiB134 linesraw
1#pragma once
2
3#include "llama-kv-cache-dsa.h"
4
5#include <vector>
6
7//
8// llama_kv_cache_dsa_iswa
9//
10
11// utilizes two child memories: llama_kv_cache_dsa for the full-attention (DSA) layers and llama_kv_cache for the SWA layers
12
13class llama_kv_cache_dsa_iswa : public llama_memory_i {
14public:
15    llama_kv_cache_dsa_iswa(
16            const llama_model & model,
17                    ggml_type   type_k,
18                    ggml_type   type_v,
19                         bool   v_trans,
20                         bool   offload,
21                         bool   swa_full,
22                         bool   unified,
23                     uint32_t   kv_size,
24                     uint32_t   n_seq_max,
25                     uint32_t   n_ubatch,
26                     uint32_t   n_pad,
27        const layer_filter_cb & filter_mla,
28        const layer_filter_cb & filter_lid,
29        const  layer_reuse_cb & reuse);
30
31    ~llama_kv_cache_dsa_iswa() = default;
32
33    //
34    // llama_memory_i
35    //
36
37    llama_memory_context_ptr init_batch(
38            llama_batch_allocr & balloc,
39            uint32_t n_ubatch,
40            bool embd_all) override;
41
42    llama_memory_context_ptr init_full() override;
43
44    llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;
45
46    bool get_can_shift() const override;
47
48    void clear(bool data) override;
49
50    bool seq_rm  (llama_seq_id seq_id,                              llama_pos p0, llama_pos p1) override;
51    void seq_cp  (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
52    void seq_keep(llama_seq_id seq_id)                                                          override;
53    void seq_add (llama_seq_id seq_id,                              llama_pos p0, llama_pos p1, llama_pos shift) override;
54    void seq_div (llama_seq_id seq_id,                              llama_pos p0, llama_pos p1, int d) override;
55
56    llama_pos seq_pos_min(llama_seq_id seq_id) const override;
57    llama_pos seq_pos_max(llama_seq_id seq_id) const override;
58
59    std::map<ggml_backend_buffer_type_t, size_t> memory_breakdown() const override;
60
61    // state write/load
62
63    void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1, llama_state_seq_flags flags = 0) const override;
64    void state_read (llama_io_read_i  & io, llama_seq_id seq_id = -1, llama_state_seq_flags flags = 0) override;
65
66    //
67    // llama_kv_cache_dsa_iswa specific API
68    //
69
70    llama_kv_cache_dsa * get_dsa() const;
71    llama_kv_cache     * get_swa() const;
72
73private:
74    const bool unified;
75
76    std::unique_ptr<llama_kv_cache_dsa> kv_dsa;
77    std::unique_ptr<llama_kv_cache>     kv_swa;
78};
79
80class llama_kv_cache_dsa_iswa_context : public llama_memory_context_i {
81public:
82    using slot_info_vec_t = llama_kv_cache::slot_info_vec_t;
83
84    // used for errors
85    llama_kv_cache_dsa_iswa_context(llama_memory_status status);
86
87    // used to create a full-cache context
88    llama_kv_cache_dsa_iswa_context(
89            llama_kv_cache_dsa_iswa * kv);
90
91    // used to create an update context
92    llama_kv_cache_dsa_iswa_context(
93            llama_kv_cache_dsa_iswa * kv,
94            llama_context * lctx,
95            bool optimize);
96
97    // used to create a batch processing context from a batch
98    llama_kv_cache_dsa_iswa_context(
99            llama_kv_cache_dsa_iswa * kv,
100            slot_info_vec_t sinfos_mla,
101            slot_info_vec_t sinfos_lid,
102            slot_info_vec_t sinfos_swa,
103            std::vector<llama_ubatch> ubatches);
104
105    virtual ~llama_kv_cache_dsa_iswa_context();
106
107    //
108    // llama_memory_context_i
109    //
110
111    bool next()  override;
112    bool apply() override;
113
114    llama_memory_status  get_status() const override;
115    const llama_ubatch & get_ubatch() const override;
116
117    //
118    // llama_kv_cache_dsa_iswa_context specific API
119    //
120
121    const llama_kv_cache_dsa_context * get_dsa() const;
122    const llama_kv_cache_context     * get_swa() const;
123
124private:
125    // the index of the next ubatch to process
126    size_t i_next = 0;
127
128    std::vector<llama_ubatch> ubatches;
129
130    const llama_memory_context_ptr ctx_dsa;
131    const llama_memory_context_ptr ctx_swa;
132
133    const llama_memory_status status;
134};