@novastera-oss/llamarn 0.2.4 → 0.2.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 (123) hide show
  1. package/RNLlamaCpp.podspec +3 -2
  2. package/android/CMakeLists.txt +6 -3
  3. package/android/src/main/cpp/include/llama.h +12 -8
  4. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  8. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  11. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  12. package/cpp/LlamaCppModel.cpp +46 -65
  13. package/cpp/LlamaCppModel.h +5 -0
  14. package/cpp/build-info.cpp +2 -2
  15. package/cpp/llama.cpp/README.md +1 -0
  16. package/cpp/llama.cpp/common/CMakeLists.txt +5 -8
  17. package/cpp/llama.cpp/common/arg.cpp +8 -6
  18. package/cpp/llama.cpp/common/chat-parser.cpp +4 -3
  19. package/cpp/llama.cpp/common/chat-parser.h +2 -1
  20. package/cpp/llama.cpp/common/chat.cpp +4 -4
  21. package/cpp/llama.cpp/common/common.cpp +2 -0
  22. package/cpp/llama.cpp/common/json-partial.cpp +5 -4
  23. package/cpp/llama.cpp/common/json-partial.h +2 -1
  24. package/cpp/llama.cpp/common/json-schema-to-grammar.cpp +2 -1
  25. package/cpp/llama.cpp/common/json-schema-to-grammar.h +4 -4
  26. package/cpp/llama.cpp/convert_hf_to_gguf.py +31 -28
  27. package/cpp/llama.cpp/ggml/include/ggml.h +1 -3
  28. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +2 -0
  29. package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +10 -5
  30. package/cpp/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +3 -3
  31. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +23 -0
  32. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +1 -0
  33. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +1 -1
  34. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +19 -8
  35. package/cpp/llama.cpp/ggml/src/ggml-impl.h +2 -0
  36. package/cpp/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +2 -2
  37. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +0 -8
  38. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +118 -11
  39. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +1 -1
  40. package/cpp/llama.cpp/ggml/src/ggml.c +9 -2
  41. package/cpp/llama.cpp/ggml/src/ggml.cpp +26 -0
  42. package/cpp/llama.cpp/ggml/src/gguf.cpp +19 -2
  43. package/cpp/llama.cpp/include/llama.h +12 -8
  44. package/cpp/llama.cpp/src/CMakeLists.txt +3 -0
  45. package/cpp/llama.cpp/src/llama-batch.cpp +19 -12
  46. package/cpp/llama.cpp/src/llama-batch.h +15 -10
  47. package/cpp/llama.cpp/src/llama-context.cpp +226 -151
  48. package/cpp/llama.cpp/src/llama-context.h +25 -8
  49. package/cpp/llama.cpp/src/llama-graph.cpp +50 -47
  50. package/cpp/llama.cpp/src/llama-graph.h +25 -24
  51. package/cpp/llama.cpp/src/llama-kv-cache-recurrent.cpp +1132 -0
  52. package/cpp/llama.cpp/src/llama-kv-cache-recurrent.h +191 -0
  53. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +249 -0
  54. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.h +136 -0
  55. package/cpp/llama.cpp/src/llama-kv-cache-unified.cpp +1717 -0
  56. package/cpp/llama.cpp/src/llama-kv-cache-unified.h +278 -0
  57. package/cpp/llama.cpp/src/llama-kv-cache.cpp +0 -2746
  58. package/cpp/llama.cpp/src/llama-kv-cache.h +14 -472
  59. package/cpp/llama.cpp/src/llama-kv-cells.h +37 -6
  60. package/cpp/llama.cpp/src/llama-memory.h +44 -0
  61. package/cpp/llama.cpp/src/llama-model.cpp +23 -16
  62. package/cpp/llama.cpp/src/llama-vocab.cpp +7 -2
  63. package/cpp/llama.cpp/vendor/cpp-httplib/httplib.h +10518 -0
  64. package/cpp/llama.cpp/vendor/miniaudio/miniaudio.h +93468 -0
  65. package/cpp/llama.cpp/{common → vendor}/minja/chat-template.hpp +1 -1
  66. package/cpp/llama.cpp/{common → vendor}/minja/minja.hpp +1 -1
  67. package/cpp/llama.cpp/{common → vendor/nlohmann}/json.hpp +3027 -2267
  68. package/cpp/llama.cpp/vendor/nlohmann/json_fwd.hpp +187 -0
  69. package/cpp/llama.cpp/vendor/stb/stb_image.h +7988 -0
  70. package/cpp/rn-completion.cpp +101 -52
  71. package/cpp/rn-utils.hpp +8 -1
  72. package/ios/include/common/minja/chat-template.hpp +1 -1
  73. package/ios/include/common/minja/minja.hpp +1 -1
  74. package/ios/include/json-schema-to-grammar.h +4 -4
  75. package/ios/include/llama.h +12 -8
  76. package/ios/include/{common → nlohmann}/json.hpp +3027 -2267
  77. package/ios/libs/llama.xcframework/Info.plist +22 -22
  78. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  79. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4689 -4617
  80. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +1 -3
  81. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +12 -8
  82. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  83. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  84. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4710 -4638
  85. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3622 -3557
  86. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
  87. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +12 -8
  88. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  89. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  90. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4710 -4638
  91. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3624 -3559
  92. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +1 -3
  93. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +12 -8
  94. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +1 -3
  95. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +12 -8
  96. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  97. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +1 -3
  98. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +12 -8
  99. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  100. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  101. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  102. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4689 -4616
  103. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +1 -3
  104. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +12 -8
  105. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  106. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  107. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4710 -4637
  108. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3622 -3556
  109. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
  110. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +12 -8
  111. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  112. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  113. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4725 -4653
  114. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +1 -3
  115. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +12 -8
  116. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  117. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  118. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4746 -4674
  119. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3652 -3587
  120. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
  121. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +12 -8
  122. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  123. package/package.json +1 -1
@@ -0,0 +1,278 @@
1
+ #pragma once
2
+
3
+ #include "llama-batch.h"
4
+ #include "llama-graph.h"
5
+ #include "llama-kv-cache.h"
6
+ #include "llama-kv-cells.h"
7
+
8
+ #include <unordered_map>
9
+ #include <vector>
10
+
11
+ struct llama_cparams;
12
+ struct llama_hparams;
13
+ struct llama_model;
14
+ struct llama_context;
15
+
16
+ //
17
+ // llama_kv_cache_unified
18
+ //
19
+
20
+ class llama_kv_cache_unified : public llama_kv_cache {
21
+ public:
22
+ static uint32_t get_padding(const llama_cparams & cparams);
23
+
24
+ // this callback is used to filter out layers that should not be included in the cache
25
+ using layer_filter_cb = std::function<bool(int32_t il)>;
26
+
27
+ llama_kv_cache_unified(
28
+ const llama_model & model,
29
+ layer_filter_cb && filter,
30
+ ggml_type type_k,
31
+ ggml_type type_v,
32
+ bool v_trans,
33
+ bool offload,
34
+ uint32_t kv_size,
35
+ uint32_t n_seq_max,
36
+ uint32_t n_pad,
37
+ uint32_t n_swa,
38
+ llama_swa_type swa_type);
39
+
40
+ ~llama_kv_cache_unified() = default;
41
+
42
+ //
43
+ // llama_memory_i
44
+ //
45
+
46
+ void clear() override;
47
+
48
+ bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
49
+ void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
50
+ void seq_keep(llama_seq_id seq_id) override;
51
+ void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override;
52
+ void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
53
+
54
+ llama_pos seq_pos_min(llama_seq_id seq_id) const override;
55
+ llama_pos seq_pos_max(llama_seq_id seq_id) const override;
56
+
57
+ //
58
+ // llama_kv_cache
59
+ //
60
+
61
+ llama_memory_state_ptr init_batch(
62
+ const llama_batch & batch,
63
+ uint32_t n_ubatch,
64
+ bool embd_pooled,
65
+ bool logits_all) override;
66
+
67
+ llama_memory_state_ptr init_full() override;
68
+
69
+ bool update(llama_context & lctx) override;
70
+
71
+ void defrag_sched(float thold) override;
72
+
73
+ bool get_can_shift() const override;
74
+
75
+ // state write/load
76
+
77
+ void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
78
+ void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
79
+
80
+ //
81
+ // llama_kv_cache_unified specific API
82
+ //
83
+
84
+ uint32_t get_size() const;
85
+
86
+ //
87
+ // graph_build API
88
+ //
89
+
90
+ uint32_t get_n_kv() const;
91
+
92
+ // get views of the current state of the cache
93
+ ggml_tensor * get_k(ggml_context * ctx, int32_t il, uint32_t n_kv) const;
94
+ ggml_tensor * get_v(ggml_context * ctx, int32_t il, uint32_t n_kv) const;
95
+
96
+ // store k_cur and v_cur in the cache based on the provided head location
97
+ ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il, uint32_t head_cur) const;
98
+ ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il, uint32_t head_cur) const;
99
+
100
+ //
101
+ // preparation API
102
+ //
103
+
104
+ // find places for the provided ubatches in the cache, returns the head locations
105
+ // return empty vector on failure
106
+ std::vector<uint32_t> prepare(const std::vector<llama_ubatch> & ubatches);
107
+
108
+ // return the cell position where we can insert the ubatch
109
+ // return -1 on failure to find a contiguous slot of kv cells
110
+ int32_t find_slot(const llama_ubatch & ubatch) const;
111
+
112
+ // emplace the ubatch context into slot: [head_cur, head_cur + ubatch.n_tokens)
113
+ void apply_ubatch(uint32_t head_cur, const llama_ubatch & ubatch);
114
+
115
+ //
116
+ // set_input API
117
+ //
118
+
119
+ void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
120
+ void set_input_k_shift (ggml_tensor * dst) const;
121
+ void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
122
+
123
+ private:
124
+ const llama_model & model;
125
+ const llama_hparams & hparams;
126
+
127
+ struct kv_layer {
128
+ // layer index in the model
129
+ // note: can be different from the layer index in the KV cache
130
+ uint32_t il;
131
+
132
+ ggml_tensor * k;
133
+ ggml_tensor * v;
134
+ };
135
+
136
+ bool do_defrag = false;
137
+ bool v_trans = true; // the value tensor is transposed
138
+
139
+ // the current index from where we start searching for a free slot in the ring buffer of KV cells (see find_slot())
140
+ // note: this is not part of the KV state and it's only used to speed-up the find_slot() method
141
+ uint32_t head = 0;
142
+
143
+ const uint32_t n_seq_max = 1;
144
+
145
+ // required padding
146
+ const uint32_t n_pad = 1;
147
+
148
+ // SWA
149
+ const uint32_t n_swa = 0;
150
+
151
+ const llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
152
+
153
+ std::vector<ggml_context_ptr> ctxs;
154
+ std::vector<ggml_backend_buffer_ptr> bufs;
155
+
156
+ llama_kv_cells_unified cells;
157
+
158
+ std::vector<kv_layer> layers;
159
+
160
+ // model layer id -> KV cache layer id
161
+ std::unordered_map<int32_t, int32_t> map_layer_ids;
162
+
163
+ // defrag
164
+ struct {
165
+ std::vector<uint32_t> ids;
166
+ } defrag_info;
167
+
168
+ // return true if cells have been moved
169
+ bool defrag_prepare(int32_t n_max_nodes);
170
+
171
+ size_t total_size() const;
172
+
173
+ size_t size_k_bytes() const;
174
+ size_t size_v_bytes() const;
175
+
176
+ bool is_masked_swa(llama_pos p0, llama_pos p1) const;
177
+
178
+ ggml_tensor * build_rope_shift(
179
+ const llama_cparams & cparams,
180
+ ggml_context * ctx,
181
+ ggml_tensor * cur,
182
+ ggml_tensor * shift,
183
+ ggml_tensor * factors,
184
+ float freq_base,
185
+ float freq_scale) const;
186
+
187
+ llm_graph_result_ptr build_graph_shift(
188
+ const llama_cparams & cparams,
189
+ ggml_context * ctx,
190
+ ggml_cgraph * gf) const;
191
+
192
+ llm_graph_result_ptr build_graph_defrag(
193
+ const llama_cparams & cparams,
194
+ ggml_context * ctx,
195
+ ggml_cgraph * gf) const;
196
+
197
+ void state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) const;
198
+ void state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const;
199
+
200
+ bool state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
201
+ bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
202
+ };
203
+
204
+ class llama_kv_cache_unified_state : public llama_memory_state_i {
205
+ public:
206
+ // used for errors
207
+ llama_kv_cache_unified_state(llama_memory_status status);
208
+
209
+ // used to create a full-cache state
210
+ llama_kv_cache_unified_state(
211
+ llama_memory_status status,
212
+ llama_kv_cache_unified * kv);
213
+
214
+ // used to create a state from a batch
215
+ llama_kv_cache_unified_state(
216
+ llama_memory_status status,
217
+ llama_kv_cache_unified * kv,
218
+ llama_sbatch sbatch,
219
+ std::vector<uint32_t> heads,
220
+ std::vector<llama_ubatch> ubatches);
221
+
222
+ virtual ~llama_kv_cache_unified_state();
223
+
224
+ //
225
+ // llama_memory_state_i
226
+ //
227
+
228
+ bool next() override;
229
+ bool apply() override;
230
+
231
+ std::vector<int64_t> & out_ids() override;
232
+
233
+ llama_memory_status get_status() const override;
234
+ const llama_ubatch & get_ubatch() const override;
235
+
236
+ //
237
+ // llama_kv_cache_unified_state specific API
238
+ //
239
+
240
+ uint32_t get_n_kv() const;
241
+
242
+ // get views of the current state of the cache
243
+ ggml_tensor * get_k(ggml_context * ctx, int32_t il) const;
244
+ ggml_tensor * get_v(ggml_context * ctx, int32_t il) const;
245
+
246
+ // store k_cur and v_cur in the cache based on the provided head location
247
+ ggml_tensor * cpy_k(ggml_context * ctx, ggml_tensor * k_cur, int32_t il) const;
248
+ ggml_tensor * cpy_v(ggml_context * ctx, ggml_tensor * v_cur, int32_t il) const;
249
+
250
+ void set_input_k_shift(ggml_tensor * dst) const;
251
+
252
+ void set_input_kq_mask (ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const;
253
+ void set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const;
254
+
255
+ private:
256
+ const llama_memory_status status;
257
+
258
+ llama_kv_cache_unified * kv;
259
+
260
+ llama_sbatch sbatch;
261
+
262
+ // the index of the next ubatch to process
263
+ size_t i_next = 0;
264
+
265
+ std::vector<uint32_t> heads;
266
+ std::vector<llama_ubatch> ubatches;
267
+
268
+ //
269
+ // data needed for building the compute graph for the current ubatch:
270
+ //
271
+
272
+ // a heuristic, to avoid attending the full cache if it is not yet utilized
273
+ // as the cache gets filled, the benefit from this heuristic disappears
274
+ int32_t n_kv;
275
+
276
+ // the beginning of the current slot in which the ubatch will be inserted
277
+ int32_t head;
278
+ };