cui-llama.rn 1.3.5 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/android/src/main/CMakeLists.txt +14 -8
  2. package/android/src/main/jni.cpp +38 -37
  3. package/cpp/common.cpp +43 -26
  4. package/cpp/common.h +18 -11
  5. package/cpp/ggml-backend-reg.cpp +5 -0
  6. package/cpp/ggml-backend.cpp +5 -2
  7. package/cpp/ggml-cpp.h +1 -0
  8. package/cpp/ggml-cpu-aarch64.cpp +6 -1
  9. package/cpp/ggml-cpu-quants.c +5 -1
  10. package/cpp/ggml-impl.h +11 -16
  11. package/cpp/ggml-metal.m +2 -2
  12. package/cpp/ggml.c +0 -1276
  13. package/cpp/ggml.h +0 -140
  14. package/cpp/gguf.cpp +1325 -0
  15. package/cpp/gguf.h +202 -0
  16. package/cpp/llama-adapter.cpp +346 -0
  17. package/cpp/llama-adapter.h +73 -0
  18. package/cpp/llama-arch.cpp +1434 -0
  19. package/cpp/llama-arch.h +395 -0
  20. package/cpp/llama-batch.cpp +368 -0
  21. package/cpp/llama-batch.h +88 -0
  22. package/cpp/llama-chat.cpp +567 -0
  23. package/cpp/llama-chat.h +51 -0
  24. package/cpp/llama-context.cpp +1771 -0
  25. package/cpp/llama-context.h +128 -0
  26. package/cpp/llama-cparams.cpp +1 -0
  27. package/cpp/llama-cparams.h +37 -0
  28. package/cpp/llama-cpp.h +30 -0
  29. package/cpp/llama-grammar.cpp +1 -0
  30. package/cpp/llama-grammar.h +3 -1
  31. package/cpp/llama-hparams.cpp +71 -0
  32. package/cpp/llama-hparams.h +140 -0
  33. package/cpp/llama-impl.cpp +167 -0
  34. package/cpp/llama-impl.h +16 -136
  35. package/cpp/llama-kv-cache.cpp +718 -0
  36. package/cpp/llama-kv-cache.h +218 -0
  37. package/cpp/llama-mmap.cpp +589 -0
  38. package/cpp/llama-mmap.h +67 -0
  39. package/cpp/llama-model-loader.cpp +1011 -0
  40. package/cpp/llama-model-loader.h +158 -0
  41. package/cpp/llama-model.cpp +2202 -0
  42. package/cpp/llama-model.h +391 -0
  43. package/cpp/llama-sampling.cpp +117 -4
  44. package/cpp/llama-vocab.cpp +21 -28
  45. package/cpp/llama-vocab.h +13 -1
  46. package/cpp/llama.cpp +8437 -19421
  47. package/cpp/llama.cpp.rej +23 -0
  48. package/cpp/llama.h +31 -6
  49. package/cpp/rn-llama.hpp +39 -37
  50. package/cpp/sgemm.cpp +776 -70
  51. package/cpp/unicode.cpp +6 -0
  52. package/package.json +1 -1
@@ -0,0 +1,218 @@
1
+ #pragma once
2
+
3
+ #include "llama.h"
4
+
5
+ #include "ggml-cpp.h"
6
+
7
+ #include <set>
8
+ #include <vector>
9
+
10
+ struct llama_kv_cell {
11
+ llama_pos pos = -1;
12
+ llama_pos delta = 0;
13
+ int32_t src = -1; // used by recurrent state models to copy states
14
+ int32_t tail = -1;
15
+
16
+ std::set<llama_seq_id> seq_id;
17
+
18
+ bool has_seq_id(const llama_seq_id & id) const {
19
+ return seq_id.find(id) != seq_id.end();
20
+ }
21
+
22
+ bool is_empty() const {
23
+ return seq_id.empty();
24
+ }
25
+
26
+ bool is_same_seq(const llama_kv_cell & other) const {
27
+ return seq_id == other.seq_id;
28
+ }
29
+ };
30
+
31
+ // ring-buffer of cached KV data
32
+ struct llama_kv_cache {
33
+ bool has_shift = false;
34
+ bool do_defrag = false;
35
+ bool recurrent = false; // with recurrent state models, a cell can hold the state for more than one past token
36
+ bool v_trans = true; // the value tensor is transposed
37
+ bool can_shift = false;
38
+
39
+ // Note: The value of head isn't only used to optimize searching
40
+ // for a free KV slot. llama_decode_internal also uses it, so it
41
+ // cannot be freely changed after a slot has been allocated.
42
+ uint32_t head = 0;
43
+ uint32_t size = 0;
44
+ uint32_t used = 0; // used cells (i.e. at least one seq_id)
45
+
46
+ // computed before each graph build
47
+ uint32_t n = 0;
48
+
49
+ lm_ggml_type type_k = LM_GGML_TYPE_F16;
50
+ lm_ggml_type type_v = LM_GGML_TYPE_F16;
51
+
52
+ std::vector<llama_kv_cell> cells;
53
+
54
+ std::vector<struct lm_ggml_tensor *> k_l; // per layer
55
+ std::vector<struct lm_ggml_tensor *> v_l;
56
+
57
+ std::vector<lm_ggml_context_ptr> ctxs;
58
+ std::vector<lm_ggml_backend_buffer_ptr> bufs;
59
+
60
+ size_t total_size() const {
61
+ size_t size = 0;
62
+ for (const auto & buf : bufs) {
63
+ size += lm_ggml_backend_buffer_get_size(buf.get());
64
+ }
65
+
66
+ return size;
67
+ }
68
+
69
+ // TODO: better data structures to reduce the cost of this operation
70
+ llama_pos max_pos() const {
71
+ llama_pos max_pos = -1;
72
+ for (const auto & cell : cells) {
73
+ max_pos = std::max(max_pos, cell.pos);
74
+ }
75
+
76
+ return max_pos;
77
+ }
78
+ };
79
+
80
+ // a structure holds information about the slot found in llama_kv_cache_find_slot
81
+ struct llama_kv_cache_slot_info {
82
+ std::pair<uint32_t, uint32_t> boundaries; // slot boundaries [begin, end)
83
+ bool found = false; // the slot was found
84
+
85
+ explicit llama_kv_cache_slot_info(bool found_) : found{found_} {}
86
+ llama_kv_cache_slot_info(uint32_t begin, uint32_t end) : boundaries{begin, end}, found{true} {}
87
+
88
+ operator bool() const { return found; }
89
+ };
90
+
91
+ // TODO: maybe not needed
92
+ uint32_t llama_kv_cache_get_padding(const struct llama_cparams & cparams);
93
+
94
+ bool llama_kv_cache_init(
95
+ struct llama_kv_cache & cache,
96
+ const llama_model & model,
97
+ const llama_cparams & cparams,
98
+ lm_ggml_type type_k,
99
+ lm_ggml_type type_v,
100
+ uint32_t kv_size,
101
+ bool offload);
102
+
103
+ // find an empty slot of size "n_tokens" in the cache
104
+ // updates the cache head
105
+ // returns a structure holding information about the slot found
106
+ // Note: On success, it's important that cache.head points
107
+ // to the first cell of the slot.
108
+ struct llama_kv_cache_slot_info llama_kv_cache_find_slot(
109
+ struct llama_kv_cache & cache,
110
+ const struct llama_ubatch & batch);
111
+
112
+ // find how many cells are currently in use
113
+ uint32_t llama_kv_cache_cell_max(const struct llama_kv_cache & cache);
114
+
115
+ void llama_kv_cache_clear(struct llama_kv_cache & cache);
116
+
117
+ bool llama_kv_cache_seq_rm(
118
+ struct llama_kv_cache & cache,
119
+ llama_seq_id seq_id,
120
+ llama_pos p0,
121
+ llama_pos p1);
122
+
123
+ void llama_kv_cache_seq_cp(
124
+ struct llama_kv_cache & cache,
125
+ llama_seq_id seq_id_src,
126
+ llama_seq_id seq_id_dst,
127
+ llama_pos p0,
128
+ llama_pos p1);
129
+
130
+ void llama_kv_cache_seq_keep(
131
+ struct llama_kv_cache & cache,
132
+ llama_seq_id seq_id);
133
+
134
+ void llama_kv_cache_seq_add(
135
+ struct llama_kv_cache & cache,
136
+ llama_seq_id seq_id,
137
+ llama_pos p0,
138
+ llama_pos p1,
139
+ llama_pos delta);
140
+
141
+ void llama_kv_cache_seq_div(
142
+ struct llama_kv_cache & cache,
143
+ llama_seq_id seq_id,
144
+ llama_pos p0,
145
+ llama_pos p1,
146
+ int d);
147
+
148
+ llama_pos llama_kv_cache_seq_pos_max(
149
+ struct llama_kv_cache & cache,
150
+ llama_seq_id seq_id);
151
+
152
+ void llama_kv_cache_defrag(struct llama_kv_cache & cache);
153
+
154
+ int32_t llama_get_kv_cache_token_count(const struct llama_kv_cache & kv);
155
+
156
+ int32_t llama_get_kv_cache_used_cells(const struct llama_kv_cache & kv);
157
+
158
+ bool llama_kv_cache_can_shift(const struct llama_kv_cache & kv);
159
+
160
+ //
161
+ // kv cache view
162
+ //
163
+
164
+ struct llama_kv_cache_view llama_kv_cache_view_init(const struct llama_kv_cache & kv, int32_t n_seq_max);
165
+
166
+ void llama_kv_cache_view_update(struct llama_kv_cache_view * view, const struct llama_kv_cache & kv);
167
+
168
+ //
169
+ // kv cache restore
170
+ //
171
+
172
+ // saves the kv_cache state for future recovery.
173
+ // used to rollback llama_kv_cache_find_slot changes.
174
+ struct llama_kv_slot_restorer {
175
+ struct llama_kv_cache_state {
176
+ uint32_t head = 0;
177
+ uint32_t n = 0;
178
+ } old_state;
179
+
180
+ // for non-recurrent models only
181
+ // list of slots to restore
182
+ std::vector<std::pair<uint32_t, uint32_t>> slot_boundaries;
183
+
184
+ bool do_restore = false;
185
+
186
+ explicit llama_kv_slot_restorer(const struct llama_kv_cache & cache) {
187
+ old_state.head = cache.head;
188
+ old_state.n = cache.n;
189
+ }
190
+
191
+ // saves a slot information for future restoration
192
+ void save(const struct llama_kv_cache_slot_info & slot) {
193
+ if (slot) {
194
+ do_restore = true;
195
+ if (slot.boundaries.first != slot.boundaries.second) {
196
+ slot_boundaries.push_back(slot.boundaries);
197
+ }
198
+ }
199
+ }
200
+
201
+ // must be explicitly called to restore the kv_cache state
202
+ // and rollback changes from all llama_kv_cache_find_slot calls
203
+ void restore(struct llama_kv_cache & cache) {
204
+ if (do_restore) {
205
+ cache.head = old_state.head;
206
+ cache.n = old_state.n;
207
+
208
+ if (cache.recurrent) { // recurrent models like Mamba or RWKV can't have a state partially erased
209
+ llama_kv_cache_seq_rm(cache, -1, -1, -1);
210
+ } else {
211
+ for (auto & slot : slot_boundaries) {
212
+ llama_kv_cache_seq_rm(cache, -1, slot.first, slot.second);
213
+ }
214
+ }
215
+ }
216
+ }
217
+ };
218
+