cui-llama.rn 1.4.6 → 1.5.0

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 (80) hide show
  1. package/android/src/main/CMakeLists.txt +9 -2
  2. package/android/src/main/jni.cpp +52 -34
  3. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  4. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  11. package/cpp/binary-ops.cpp +158 -0
  12. package/cpp/binary-ops.h +16 -0
  13. package/cpp/chat.cpp +1769 -1779
  14. package/cpp/chat.h +9 -1
  15. package/cpp/common.cpp +20 -522
  16. package/cpp/common.h +13 -36
  17. package/cpp/cpu-common.h +72 -0
  18. package/cpp/ggml-common.h +12 -6
  19. package/cpp/ggml-cpu-aarch64.cpp +1557 -80
  20. package/cpp/ggml-cpu-impl.h +2 -21
  21. package/cpp/ggml-cpu-quants.c +904 -405
  22. package/cpp/ggml-cpu.c +909 -13237
  23. package/cpp/ggml-impl.h +50 -23
  24. package/cpp/ggml-metal-impl.h +77 -3
  25. package/cpp/ggml-metal.m +794 -580
  26. package/cpp/ggml.c +92 -3
  27. package/cpp/ggml.h +29 -5
  28. package/cpp/gguf.cpp +1 -0
  29. package/cpp/llama-adapter.cpp +55 -20
  30. package/cpp/llama-adapter.h +11 -9
  31. package/cpp/llama-arch.cpp +217 -16
  32. package/cpp/llama-arch.h +25 -0
  33. package/cpp/llama-batch.h +2 -2
  34. package/cpp/llama-chat.cpp +54 -2
  35. package/cpp/llama-chat.h +3 -0
  36. package/cpp/llama-context.cpp +2294 -1238
  37. package/cpp/llama-context.h +214 -77
  38. package/cpp/llama-cparams.h +1 -0
  39. package/cpp/llama-graph.cpp +1695 -0
  40. package/cpp/llama-graph.h +592 -0
  41. package/cpp/llama-hparams.cpp +8 -0
  42. package/cpp/llama-hparams.h +17 -0
  43. package/cpp/llama-io.cpp +15 -0
  44. package/cpp/llama-io.h +35 -0
  45. package/cpp/llama-kv-cache.cpp +965 -303
  46. package/cpp/llama-kv-cache.h +145 -151
  47. package/cpp/llama-memory.cpp +1 -0
  48. package/cpp/llama-memory.h +21 -0
  49. package/cpp/llama-mmap.cpp +1 -1
  50. package/cpp/llama-model-loader.cpp +10 -5
  51. package/cpp/llama-model-loader.h +5 -3
  52. package/cpp/llama-model.cpp +9194 -201
  53. package/cpp/llama-model.h +40 -1
  54. package/cpp/llama-sampling.cpp +5 -0
  55. package/cpp/llama-vocab.cpp +36 -5
  56. package/cpp/llama.cpp +51 -9984
  57. package/cpp/llama.h +102 -22
  58. package/cpp/log.cpp +34 -0
  59. package/cpp/minja/chat-template.hpp +15 -7
  60. package/cpp/minja/minja.hpp +120 -94
  61. package/cpp/ops.cpp +8723 -0
  62. package/cpp/ops.h +128 -0
  63. package/cpp/rn-llama.cpp +44 -53
  64. package/cpp/rn-llama.h +2 -12
  65. package/cpp/sampling.cpp +3 -0
  66. package/cpp/sgemm.cpp +533 -88
  67. package/cpp/simd-mappings.h +888 -0
  68. package/cpp/speculative.cpp +4 -4
  69. package/cpp/unary-ops.cpp +186 -0
  70. package/cpp/unary-ops.h +28 -0
  71. package/cpp/vec.cpp +258 -0
  72. package/cpp/vec.h +802 -0
  73. package/ios/CMakeLists.txt +5 -2
  74. package/ios/RNLlama.mm +2 -2
  75. package/ios/RNLlamaContext.mm +40 -24
  76. package/package.json +1 -1
  77. package/src/NativeRNLlama.ts +6 -4
  78. package/src/index.ts +3 -1
  79. package/cpp/chat-template.hpp +0 -529
  80. package/cpp/minja.hpp +0 -2915
@@ -3,66 +3,213 @@
3
3
  #include "llama.h"
4
4
  #include "llama-batch.h"
5
5
  #include "llama-cparams.h"
6
- #include "llama-model.h"
7
- #include "llama-kv-cache.h"
6
+ #include "llama-graph.h"
8
7
  #include "llama-adapter.h"
9
8
 
10
9
  #include "ggml-cpp.h"
11
10
 
12
11
  #include <map>
13
- #include <unordered_map>
14
12
  #include <vector>
15
- #include <set>
13
+
14
+ struct llama_model;
15
+ struct llama_kv_cache;
16
+
17
+ class llama_io_read_i;
18
+ class llama_io_write_i;
16
19
 
17
20
  struct llama_context {
18
- llama_context(const llama_model & model)
19
- : model(model)
20
- , t_start_us(model.t_start_us)
21
- , t_load_us(model.t_load_us) {}
21
+ // init scheduler and compute buffers, reserve worst-case graphs
22
+ llama_context(
23
+ const llama_model & model,
24
+ llama_context_params params);
22
25
 
23
- const struct llama_model & model;
26
+ ~llama_context();
24
27
 
25
- struct llama_cparams cparams;
26
- struct llama_sbatch sbatch; // TODO: revisit if needed
27
- struct llama_kv_cache kv_self;
28
- struct llama_adapter_cvec cvec;
28
+ void synchronize();
29
29
 
30
- std::unordered_map<struct llama_adapter_lora *, float> lora;
30
+ const llama_model & get_model() const;
31
31
 
32
- std::vector<lm_ggml_backend_ptr> backends;
33
- std::vector<std::pair<lm_ggml_backend_t, lm_ggml_backend_set_n_threads_t>> set_n_threads_fns;
32
+ uint32_t n_ctx() const;
33
+ uint32_t n_ctx_per_seq() const;
34
+ uint32_t n_batch() const;
35
+ uint32_t n_ubatch() const;
36
+ uint32_t n_seq_max() const;
34
37
 
35
- lm_ggml_backend_t backend_cpu = nullptr;
38
+ uint32_t n_threads() const;
39
+ uint32_t n_threads_batch() const;
36
40
 
37
- lm_ggml_threadpool_t threadpool = nullptr;
38
- lm_ggml_threadpool_t threadpool_batch = nullptr;
41
+ llama_kv_cache * get_kv_self();
42
+ const llama_kv_cache * get_kv_self() const;
39
43
 
40
- bool has_evaluated_once = false;
44
+ void kv_self_update();
41
45
 
42
- mutable int64_t t_start_us;
43
- mutable int64_t t_load_us;
44
- mutable int64_t t_p_eval_us = 0;
45
- mutable int64_t t_eval_us = 0;
46
+ enum llama_pooling_type pooling_type() const;
46
47
 
47
- mutable int64_t t_compute_start_us = 0;
48
- mutable int64_t n_queued_tokens = 0;
48
+ float * get_logits();
49
+ float * get_logits_ith(int32_t i);
49
50
 
50
- mutable int32_t n_p_eval = 0; // number of tokens in eval calls for the prompt (with batch size > 1)
51
- mutable int32_t n_eval = 0; // number of eval calls
51
+ float * get_embeddings();
52
+ float * get_embeddings_ith(int32_t i);
53
+ float * get_embeddings_seq(llama_seq_id seq_id);
52
54
 
53
- // host buffer for the model output (logits and embeddings)
54
- lm_ggml_backend_buffer_ptr buf_output;
55
+ void attach_threadpool(
56
+ lm_ggml_threadpool_t threadpool,
57
+ lm_ggml_threadpool_t threadpool_batch);
55
58
 
56
- // decode output (2-dimensional array: [n_outputs][n_vocab])
57
- size_t logits_size = 0; // capacity (of floats) for logits
58
- float * logits = nullptr;
59
+ void detach_threadpool();
59
60
 
60
- std::vector<int32_t> output_ids; // map batch token positions to ids of the logits and embd buffers
61
- size_t output_size = 0; // capacity (of tokens positions) for the output buffers
62
- int32_t n_outputs = 0; // number of actually-used outputs in the current ubatch or last logical batch
61
+ void set_n_threads(int32_t n_threads, int32_t n_threads_batch);
62
+
63
+ void set_abort_callback(bool (*abort_callback)(void * data), void * abort_callback_data);
64
+
65
+ void set_embeddings (bool value);
66
+ void set_causal_attn(bool value);
67
+ void set_warmup(bool value);
68
+
69
+ void set_adapter_lora(
70
+ llama_adapter_lora * adapter,
71
+ float scale);
72
+
73
+ bool rm_adapter_lora(
74
+ llama_adapter_lora * adapter);
75
+
76
+ void clear_adapter_lora();
77
+
78
+ bool apply_adapter_cvec(
79
+ const float * data,
80
+ size_t len,
81
+ int32_t n_embd,
82
+ int32_t il_start,
83
+ int32_t il_end);
84
+
85
+ int encode(llama_batch & inp_batch);
86
+ int decode(llama_batch & inp_batch);
87
+
88
+ //
89
+ // state save/load
90
+ //
91
+
92
+ size_t state_get_size();
93
+ size_t state_get_data( uint8_t * dst, size_t size);
94
+ size_t state_set_data(const uint8_t * src, size_t size);
95
+
96
+ size_t state_seq_get_size(llama_seq_id seq_id);
97
+ size_t state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, size_t size);
98
+ size_t state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size);
99
+
100
+ bool state_load_file(
101
+ const char * filepath,
102
+ llama_token * tokens_out,
103
+ size_t n_token_capacity,
104
+ size_t * n_token_count_out);
105
+
106
+ bool state_save_file(
107
+ const char * filepath,
108
+ const llama_token * tokens,
109
+ size_t n_token_count);
110
+
111
+ size_t state_seq_load_file(
112
+ llama_seq_id seq_id,
113
+ const char * filepath,
114
+ llama_token * tokens_out,
115
+ size_t n_token_capacity,
116
+ size_t * n_token_count_out);
117
+
118
+ size_t state_seq_save_file(
119
+ llama_seq_id seq_id,
120
+ const char * filepath,
121
+ const llama_token * tokens,
122
+ size_t n_token_count);
123
+
124
+ //
125
+ // perf
126
+ //
127
+
128
+ llama_perf_context_data perf_get_data() const;
129
+ void perf_reset();
130
+
131
+ private:
132
+ //
133
+ // output
134
+ //
63
135
 
136
+ // Make sure enough space is available for outputs.
137
+ // Returns max number of outputs for which space was reserved.
138
+ int32_t output_reserve(int32_t n_outputs);
139
+
140
+ // make the outputs have the same order they had in the user-provided batch
141
+ // TODO: maybe remove this
142
+ void output_reorder();
143
+
144
+ //
145
+ // graph
146
+ //
147
+
148
+ int32_t graph_max_nodes() const;
149
+
150
+ // zero-out inputs and create the ctx_compute for the compute graph
151
+ lm_ggml_cgraph * graph_init();
152
+
153
+ llm_graph_result_ptr graph_build(
154
+ lm_ggml_context * ctx,
155
+ lm_ggml_cgraph * gf,
156
+ const llama_ubatch & ubatch,
157
+ llm_graph_type gtype);
158
+
159
+ // returns the result of lm_ggml_backend_sched_graph_compute_async execution
160
+ lm_ggml_status graph_compute(
161
+ lm_ggml_cgraph * gf,
162
+ bool batched);
163
+
164
+ llm_graph_cb graph_get_cb() const;
165
+
166
+ // used by kv_self_update()
167
+ lm_ggml_tensor * build_rope_shift(
168
+ lm_ggml_context * ctx0,
169
+ lm_ggml_tensor * cur,
170
+ lm_ggml_tensor * shift,
171
+ lm_ggml_tensor * factors,
172
+ float freq_base,
173
+ float freq_scale,
174
+ lm_ggml_backend_buffer * bbuf) const;
175
+
176
+ llm_graph_result_ptr build_kv_self_shift(
177
+ lm_ggml_context * ctx0,
178
+ lm_ggml_cgraph * gf) const;
179
+
180
+ llm_graph_result_ptr build_kv_self_defrag(
181
+ lm_ggml_context * ctx0,
182
+ lm_ggml_cgraph * gf) const;
183
+
184
+ // TODO: read/write lora adapters and cvec
185
+ size_t state_write_data(llama_io_write_i & io);
186
+ size_t state_read_data (llama_io_read_i & io);
187
+
188
+ size_t state_seq_write_data(llama_io_write_i & io, llama_seq_id seq_id);
189
+ size_t state_seq_read_data (llama_io_read_i & io, llama_seq_id seq_id);
190
+
191
+ //
192
+ // members
193
+ //
194
+
195
+ const llama_model & model;
196
+
197
+ llama_cparams cparams;
198
+ llama_adapter_cvec cvec;
199
+ llama_adapter_loras loras;
200
+ llama_sbatch sbatch;
201
+
202
+ llama_cross cross; // TODO: tmp for handling cross-attention - need something better probably
203
+
204
+ std::unique_ptr<llama_kv_cache_unified> kv_self;
205
+
206
+ // TODO: remove
64
207
  bool logits_all = false;
65
208
 
209
+ // decode output (2-dimensional array: [n_outputs][n_vocab])
210
+ size_t logits_size = 0; // capacity (of floats) for logits
211
+ float * logits = nullptr;
212
+
66
213
  // embeddings output (2-dimensional array: [n_outputs][n_embd])
67
214
  // populated only when pooling_type == LLAMA_POOLING_TYPE_NONE
68
215
  size_t embd_size = 0; // capacity (of floats) for embeddings
@@ -72,57 +219,47 @@ struct llama_context {
72
219
  // populated only when pooling_type != LLAMA_POOLING_TYPE_NONE
73
220
  std::map<llama_seq_id, std::vector<float>> embd_seq;
74
221
 
75
- // whether we are computing encoder output or decoder output
76
- bool is_encoding = false;
77
-
78
- // TODO: find a better way to accommodate mutli-dimension position encoding methods
79
- // number of position id each token get, 1 for each token in most cases.
80
- // when using m-rope, it will be 3 position ids per token to representing 3 dimension coordinate.
81
- int n_pos_per_token = 1;
222
+ int32_t n_outputs = 0; // number of actually-used outputs in the current ubatch or last logical batch
223
+ int32_t n_outputs_max = 0; // capacity (of tokens positions) for the output buffers
82
224
 
83
- // output of the encoder part of the encoder-decoder models
84
- std::vector<float> embd_enc;
85
- std::vector<std::set<llama_seq_id>> seq_ids_enc;
225
+ std::vector<int32_t> output_ids; // map batch token positions to ids of the logits and embd buffers
86
226
 
87
- // memory buffers used to evaluate the model
88
- std::vector<uint8_t> buf_compute_meta;
89
227
  lm_ggml_backend_sched_ptr sched;
90
228
 
229
+ lm_ggml_backend_t backend_cpu = nullptr;
230
+ std::vector<lm_ggml_backend_ptr> backends;
231
+
232
+ lm_ggml_context_ptr ctx_compute;
233
+
234
+ lm_ggml_threadpool_t threadpool = nullptr;
235
+ lm_ggml_threadpool_t threadpool_batch = nullptr;
236
+
91
237
  lm_ggml_abort_callback abort_callback = nullptr;
92
238
  void * abort_callback_data = nullptr;
93
239
 
94
- // input tensors
95
- struct lm_ggml_tensor * inp_tokens; // I32 [n_batch]
96
- struct lm_ggml_tensor * inp_embd; // F32 [n_embd, n_batch]
97
- struct lm_ggml_tensor * inp_pos; // I32 [n_batch]
98
- struct lm_ggml_tensor * inp_out_ids; // I32 [n_outputs]
99
- struct lm_ggml_tensor * inp_KQ_mask; // F32 [kv_size, n_batch]
100
- struct lm_ggml_tensor * inp_KQ_mask_swa; // F32 [kv_size, n_batch]
101
- struct lm_ggml_tensor * inp_K_shift; // I32 [kv_size]
102
- struct lm_ggml_tensor * inp_mean; // F32 [n_batch, n_batch]
103
- struct lm_ggml_tensor * inp_cls; // I32 [n_batch]
104
- struct lm_ggml_tensor * inp_s_copy; // I32 [kv_size]
105
- struct lm_ggml_tensor * inp_s_mask; // F32 [1, n_kv]
106
- struct lm_ggml_tensor * inp_s_seq; // I32 [n_kv, n_batch]
107
- struct lm_ggml_tensor * inp_pos_bucket; // I32 [n_batch|n_kv, n_batch]
108
- struct lm_ggml_tensor * inp_embd_enc; // F32 [n_embd, n_outputs_enc]
109
- struct lm_ggml_tensor * inp_KQ_mask_cross; // F32 [n_outputs_enc, n_batch]
110
- };
240
+ std::vector<std::pair<lm_ggml_backend_t, lm_ggml_backend_set_n_threads_t>> set_n_threads_fns;
241
+
242
+ // buffer types used for the compute buffer of each backend
243
+ std::vector<lm_ggml_backend_t> backend_ptrs;
244
+ std::vector<lm_ggml_backend_buffer_type_t> backend_buft;
111
245
 
112
- // TODO: make these methods of llama_context
113
- void llama_set_k_shift(struct llama_context & lctx);
246
+ // memory buffers used to evaluate the model
247
+ std::vector<uint8_t> buf_compute_meta;
114
248
 
115
- void llama_set_s_copy(struct llama_context & lctx);
249
+ // host buffer for the model output (logits and embeddings)
250
+ lm_ggml_backend_buffer_ptr buf_output;
116
251
 
117
- void llama_set_inputs(llama_context & lctx, const llama_ubatch & ubatch);
252
+ bool has_evaluated_once = false;
118
253
 
119
- // Make sure enough space is available for outputs.
120
- // Returns max number of outputs for which space was reserved.
121
- size_t llama_output_reserve(struct llama_context & lctx, size_t n_outputs);
254
+ // perf
255
+ mutable int64_t t_start_us = 0;
256
+ mutable int64_t t_load_us = 0;
257
+ mutable int64_t t_p_eval_us = 0;
258
+ mutable int64_t t_eval_us = 0;
122
259
 
123
- // make the outputs have the same order they had in the user-provided batch
124
- void llama_output_reorder(struct llama_context & ctx);
260
+ mutable int64_t t_compute_start_us = 0;
261
+ mutable int64_t n_queued_tokens = 0;
125
262
 
126
- // For internal test use
127
- // TODO: remove
128
- const std::vector<std::pair<std::string, struct lm_ggml_tensor *>> & llama_internal_get_tensor_map(struct llama_context * ctx);
263
+ mutable int32_t n_p_eval = 0; // number of tokens in eval calls for the prompt (with batch size > 1)
264
+ mutable int32_t n_eval = 0; // number of eval calls
265
+ };
@@ -29,6 +29,7 @@ struct llama_cparams {
29
29
  bool offload_kqv;
30
30
  bool flash_attn;
31
31
  bool no_perf;
32
+ bool warmup;
32
33
 
33
34
  enum llama_pooling_type pooling_type;
34
35