cui-llama.rn 1.3.6 → 1.4.1

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 (101) hide show
  1. package/README.md +22 -1
  2. package/android/src/main/CMakeLists.txt +25 -26
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +31 -9
  4. package/android/src/main/java/com/rnllama/RNLlama.java +98 -0
  5. package/android/src/main/jni-utils.h +94 -0
  6. package/android/src/main/jni.cpp +133 -63
  7. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +15 -0
  8. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +15 -0
  9. package/cpp/common.cpp +2085 -1982
  10. package/cpp/common.h +696 -664
  11. package/cpp/ggml-alloc.c +1042 -1037
  12. package/cpp/ggml-backend-impl.h +255 -256
  13. package/cpp/ggml-backend-reg.cpp +582 -582
  14. package/cpp/ggml-backend.cpp +2002 -2002
  15. package/cpp/ggml-backend.h +354 -352
  16. package/cpp/ggml-common.h +1853 -1853
  17. package/cpp/ggml-cpp.h +39 -39
  18. package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
  19. package/cpp/ggml-cpu-aarch64.h +8 -8
  20. package/cpp/ggml-cpu-impl.h +386 -386
  21. package/cpp/ggml-cpu-quants.c +10920 -10839
  22. package/cpp/ggml-cpu-traits.cpp +36 -36
  23. package/cpp/ggml-cpu-traits.h +38 -38
  24. package/cpp/ggml-cpu.c +14391 -14122
  25. package/cpp/ggml-cpu.cpp +635 -627
  26. package/cpp/ggml-cpu.h +135 -135
  27. package/cpp/ggml-impl.h +567 -567
  28. package/cpp/ggml-metal-impl.h +288 -0
  29. package/cpp/ggml-metal.m +4884 -4884
  30. package/cpp/ggml-opt.cpp +854 -0
  31. package/cpp/ggml-opt.h +216 -0
  32. package/cpp/ggml-quants.c +5238 -5238
  33. package/cpp/ggml-threading.h +14 -14
  34. package/cpp/ggml.c +6514 -6448
  35. package/cpp/ggml.h +2194 -2163
  36. package/cpp/gguf.cpp +1329 -1325
  37. package/cpp/gguf.h +202 -202
  38. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  39. package/cpp/json-schema-to-grammar.h +8 -8
  40. package/cpp/json.hpp +24766 -24766
  41. package/cpp/llama-adapter.cpp +347 -346
  42. package/cpp/llama-adapter.h +74 -73
  43. package/cpp/llama-arch.cpp +1487 -1434
  44. package/cpp/llama-arch.h +400 -395
  45. package/cpp/llama-batch.cpp +368 -368
  46. package/cpp/llama-batch.h +88 -88
  47. package/cpp/llama-chat.cpp +578 -567
  48. package/cpp/llama-chat.h +52 -51
  49. package/cpp/llama-context.cpp +1775 -1771
  50. package/cpp/llama-context.h +128 -128
  51. package/cpp/llama-cparams.cpp +1 -1
  52. package/cpp/llama-cparams.h +37 -37
  53. package/cpp/llama-cpp.h +30 -30
  54. package/cpp/llama-grammar.cpp +1139 -1139
  55. package/cpp/llama-grammar.h +143 -143
  56. package/cpp/llama-hparams.cpp +71 -71
  57. package/cpp/llama-hparams.h +139 -140
  58. package/cpp/llama-impl.cpp +167 -167
  59. package/cpp/llama-impl.h +61 -61
  60. package/cpp/llama-kv-cache.cpp +718 -718
  61. package/cpp/llama-kv-cache.h +218 -218
  62. package/cpp/llama-mmap.cpp +590 -589
  63. package/cpp/llama-mmap.h +67 -67
  64. package/cpp/llama-model-loader.cpp +1124 -1011
  65. package/cpp/llama-model-loader.h +167 -158
  66. package/cpp/llama-model.cpp +3997 -2202
  67. package/cpp/llama-model.h +370 -391
  68. package/cpp/llama-sampling.cpp +2408 -2406
  69. package/cpp/llama-sampling.h +32 -48
  70. package/cpp/llama-vocab.cpp +3247 -1982
  71. package/cpp/llama-vocab.h +125 -182
  72. package/cpp/llama.cpp +10077 -12544
  73. package/cpp/llama.h +1323 -1285
  74. package/cpp/log.cpp +401 -401
  75. package/cpp/log.h +121 -121
  76. package/cpp/rn-llama.hpp +123 -116
  77. package/cpp/sampling.cpp +505 -500
  78. package/cpp/sgemm.cpp +2597 -2597
  79. package/cpp/sgemm.h +14 -14
  80. package/cpp/speculative.cpp +277 -274
  81. package/cpp/speculative.h +28 -28
  82. package/cpp/unicode.cpp +2 -3
  83. package/ios/RNLlama.mm +47 -0
  84. package/ios/RNLlamaContext.h +3 -1
  85. package/ios/RNLlamaContext.mm +71 -14
  86. package/jest/mock.js +15 -3
  87. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  88. package/lib/commonjs/index.js +33 -37
  89. package/lib/commonjs/index.js.map +1 -1
  90. package/lib/module/NativeRNLlama.js.map +1 -1
  91. package/lib/module/index.js +31 -35
  92. package/lib/module/index.js.map +1 -1
  93. package/lib/typescript/NativeRNLlama.d.ts +26 -6
  94. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  95. package/lib/typescript/index.d.ts +21 -36
  96. package/lib/typescript/index.d.ts.map +1 -1
  97. package/llama-rn.podspec +4 -18
  98. package/package.json +2 -3
  99. package/src/NativeRNLlama.ts +32 -13
  100. package/src/index.ts +52 -47
  101. package/cpp/llama.cpp.rej +0 -23
@@ -1,218 +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
-
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
+