cui-llama.rn 1.4.0 → 1.4.2

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 (108) hide show
  1. package/README.md +4 -23
  2. package/android/build.gradle +12 -3
  3. package/android/src/main/CMakeLists.txt +13 -7
  4. package/android/src/main/java/com/rnllama/LlamaContext.java +27 -20
  5. package/android/src/main/java/com/rnllama/RNLlama.java +5 -1
  6. package/android/src/main/jni.cpp +15 -12
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  15. package/cpp/README.md +1 -1
  16. package/cpp/common.cpp +158 -267
  17. package/cpp/common.h +46 -12
  18. package/cpp/ggml-alloc.c +1042 -1037
  19. package/cpp/ggml-backend-impl.h +255 -256
  20. package/cpp/ggml-backend-reg.cpp +582 -582
  21. package/cpp/ggml-backend.cpp +2002 -2002
  22. package/cpp/ggml-backend.h +354 -352
  23. package/cpp/ggml-common.h +1853 -1853
  24. package/cpp/ggml-cpp.h +39 -39
  25. package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
  26. package/cpp/ggml-cpu-aarch64.h +8 -8
  27. package/cpp/ggml-cpu-impl.h +386 -386
  28. package/cpp/ggml-cpu-quants.c +10920 -10839
  29. package/cpp/ggml-cpu-traits.cpp +36 -36
  30. package/cpp/ggml-cpu-traits.h +38 -38
  31. package/cpp/ggml-cpu.c +329 -60
  32. package/cpp/ggml-cpu.cpp +10 -2
  33. package/cpp/ggml-cpu.h +135 -135
  34. package/cpp/ggml-impl.h +567 -567
  35. package/cpp/ggml-metal-impl.h +17 -17
  36. package/cpp/ggml-metal.m +4884 -4884
  37. package/cpp/ggml-quants.c +5238 -5238
  38. package/cpp/ggml-threading.h +14 -14
  39. package/cpp/ggml.c +6514 -6448
  40. package/cpp/ggml.h +2194 -2163
  41. package/cpp/gguf.cpp +1329 -1325
  42. package/cpp/gguf.h +202 -202
  43. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  44. package/cpp/json-schema-to-grammar.h +8 -8
  45. package/cpp/json.hpp +24766 -24766
  46. package/cpp/llama-adapter.cpp +347 -346
  47. package/cpp/llama-adapter.h +74 -73
  48. package/cpp/llama-arch.cpp +1487 -1434
  49. package/cpp/llama-arch.h +400 -395
  50. package/cpp/llama-batch.cpp +368 -368
  51. package/cpp/llama-batch.h +88 -88
  52. package/cpp/llama-chat.cpp +578 -567
  53. package/cpp/llama-chat.h +52 -51
  54. package/cpp/llama-context.cpp +1775 -1771
  55. package/cpp/llama-context.h +128 -128
  56. package/cpp/llama-cparams.cpp +1 -1
  57. package/cpp/llama-cparams.h +37 -37
  58. package/cpp/llama-cpp.h +30 -30
  59. package/cpp/llama-grammar.cpp +1139 -1139
  60. package/cpp/llama-grammar.h +143 -143
  61. package/cpp/llama-hparams.cpp +71 -71
  62. package/cpp/llama-hparams.h +139 -140
  63. package/cpp/llama-impl.cpp +167 -167
  64. package/cpp/llama-impl.h +61 -61
  65. package/cpp/llama-kv-cache.cpp +718 -718
  66. package/cpp/llama-kv-cache.h +218 -218
  67. package/cpp/llama-mmap.cpp +2 -1
  68. package/cpp/llama-mmap.h +67 -67
  69. package/cpp/llama-model-loader.cpp +1124 -1011
  70. package/cpp/llama-model-loader.h +167 -158
  71. package/cpp/llama-model.cpp +3997 -2202
  72. package/cpp/llama-model.h +370 -391
  73. package/cpp/llama-sampling.cpp +2408 -2406
  74. package/cpp/llama-sampling.h +32 -48
  75. package/cpp/llama-vocab.cpp +3247 -1982
  76. package/cpp/llama-vocab.h +125 -182
  77. package/cpp/llama.cpp +416 -2886
  78. package/cpp/llama.h +1323 -1285
  79. package/cpp/log.cpp +401 -401
  80. package/cpp/log.h +121 -121
  81. package/cpp/rn-llama.cpp +822 -0
  82. package/cpp/rn-llama.h +123 -0
  83. package/cpp/rn-llama.hpp +18 -12
  84. package/cpp/sampling.cpp +505 -500
  85. package/cpp/sgemm.cpp +2597 -2597
  86. package/cpp/speculative.cpp +277 -274
  87. package/cpp/speculative.h +28 -28
  88. package/cpp/unicode.cpp +2 -3
  89. package/ios/CMakeLists.txt +99 -0
  90. package/ios/RNLlama.h +5 -1
  91. package/ios/RNLlama.mm +2 -2
  92. package/ios/RNLlamaContext.h +8 -1
  93. package/ios/RNLlamaContext.mm +15 -11
  94. package/ios/rnllama.xcframework/Info.plist +74 -0
  95. package/jest/mock.js +3 -2
  96. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  97. package/lib/commonjs/index.js +4 -2
  98. package/lib/commonjs/index.js.map +1 -1
  99. package/lib/module/NativeRNLlama.js.map +1 -1
  100. package/lib/module/index.js +4 -2
  101. package/lib/module/index.js.map +1 -1
  102. package/lib/typescript/NativeRNLlama.d.ts +5 -1
  103. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  104. package/lib/typescript/index.d.ts.map +1 -1
  105. package/llama-rn.podspec +8 -2
  106. package/package.json +5 -2
  107. package/src/NativeRNLlama.ts +5 -1
  108. package/src/index.ts +9 -2
package/cpp/llama-vocab.h CHANGED
@@ -1,182 +1,125 @@
1
- #pragma once
2
-
3
- #include "llama.h"
4
-
5
- #include <string>
6
- #include <vector>
7
- #include <unordered_map>
8
- #include <map>
9
- #include <set>
10
-
11
- static const char * llama_model_vocab_type_name(enum llama_vocab_type type){
12
- switch (type) {
13
- case LLAMA_VOCAB_TYPE_NONE: return "no vocab";
14
- case LLAMA_VOCAB_TYPE_SPM: return "SPM";
15
- case LLAMA_VOCAB_TYPE_BPE: return "BPE";
16
- case LLAMA_VOCAB_TYPE_WPM: return "WPM";
17
- case LLAMA_VOCAB_TYPE_UGM: return "UGM";
18
- case LLAMA_VOCAB_TYPE_RWKV: return "RWKV";
19
- default: return "unknown";
20
- }
21
- }
22
-
23
- struct llm_tokenizer;
24
-
25
- struct llama_vocab {
26
- using id = llama_token;
27
- using token = std::string;
28
- using tattr = llama_token_attr;
29
-
30
- struct token_data {
31
- token text;
32
- float score;
33
- tattr attr;
34
- };
35
-
36
- uint32_t n_vocab = 0; // TODO: not great because has to keep in sync with hparams.n_vocab
37
-
38
- enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
39
- enum llama_vocab_pre_type type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
40
-
41
- int max_token_len = 0; // used for optimizing longest token search
42
-
43
- std::unordered_map<token, id> token_to_id;
44
- std::vector<token_data> id_to_token;
45
-
46
- std::vector<id> cache_special_tokens;
47
- std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = true);
48
-
49
- std::map<std::pair<std::string, std::string>, int> bpe_ranks;
50
-
51
- // default LLaMA special tokens
52
- // TODO: should we set all of these to LLAMA_TOKEN_NULL?
53
- id special_bos_id = 1;
54
- id special_eos_id = 2;
55
- id special_eot_id = LLAMA_TOKEN_NULL;
56
- id special_eom_id = LLAMA_TOKEN_NULL;
57
- id special_unk_id = 0;
58
- id special_sep_id = LLAMA_TOKEN_NULL;
59
- id special_pad_id = LLAMA_TOKEN_NULL;
60
- id special_cls_id = LLAMA_TOKEN_NULL; // TODO: revisit if this is really needed https://github.com/ggerganov/llama.cpp/pull/10930
61
- id special_mask_id = LLAMA_TOKEN_NULL;
62
-
63
- id linefeed_id = 13;
64
-
65
- // fim tokens
66
- id special_fim_pre_id = LLAMA_TOKEN_NULL;
67
- id special_fim_suf_id = LLAMA_TOKEN_NULL;
68
- id special_fim_mid_id = LLAMA_TOKEN_NULL;
69
- id special_fim_pad_id = LLAMA_TOKEN_NULL;
70
- id special_fim_rep_id = LLAMA_TOKEN_NULL; // repo
71
- id special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator
72
-
73
- // set of all tokens that cause "end of generation"
74
- std::set<id> special_eog_ids;
75
-
76
- // tokenizer flags
77
- bool tokenizer_add_space_prefix = false;
78
- bool tokenizer_add_bos = false;
79
- bool tokenizer_add_eos = false;
80
- bool tokenizer_ignore_merges = false;
81
- bool tokenizer_clean_spaces = false; // clean_up_tokenization_spaces
82
- bool tokenizer_remove_extra_whitespaces = false;
83
- bool tokenizer_escape_whitespaces = true;
84
- bool tokenizer_treat_whitespace_as_suffix = false;
85
-
86
- std::vector<char> precompiled_charsmap;
87
-
88
- llm_tokenizer * tokenizer = nullptr;
89
-
90
- llama_vocab() = default;
91
- ~llama_vocab();
92
-
93
- int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
94
-
95
- void init_tokenizer();
96
- };
97
-
98
- //
99
- // internal API
100
- //
101
-
102
- // TODO: rename to llama_tokenize_impl
103
- // TODO: This should probably be in llama.h
104
- std::vector<llama_vocab::id> llama_tokenize_internal(
105
- const llama_vocab & vocab,
106
- std::string raw_text,
107
- bool add_special,
108
- bool parse_special = false);
109
-
110
- // TODO: move the API below as member functions of llama_vocab
111
- llama_token llama_byte_to_token_impl(const llama_vocab & vocab, uint8_t ch);
112
-
113
- const char * llama_token_get_text_impl(const struct llama_vocab & vocab, llama_token token);
114
-
115
- float llama_token_get_score_impl(const struct llama_vocab & vocab, llama_token token);
116
-
117
- llama_token_attr llama_token_get_attr_impl(const struct llama_vocab & vocab, llama_token token);
118
-
119
- bool llama_token_is_eog_impl(const struct llama_vocab & vocab, llama_token token);
120
-
121
- bool llama_token_is_control_impl(const struct llama_vocab & vocab, llama_token token);
122
-
123
- llama_token llama_token_bos_impl(const struct llama_vocab & vocab);
124
- llama_token llama_token_eos_impl(const struct llama_vocab & vocab);
125
- llama_token llama_token_eot_impl(const struct llama_vocab & vocab);
126
- llama_token llama_token_eom_impl(const struct llama_vocab & vocab);
127
- llama_token llama_token_cls_impl(const struct llama_vocab & vocab);
128
- llama_token llama_token_sep_impl(const struct llama_vocab & vocab);
129
- llama_token llama_token_nl_impl (const struct llama_vocab & vocab);
130
- llama_token llama_token_pad_impl(const struct llama_vocab & vocab);
131
-
132
- llama_token llama_token_prefix_impl(const struct llama_vocab & vocab);
133
- llama_token llama_token_middle_impl(const struct llama_vocab & vocab);
134
- llama_token llama_token_suffix_impl(const struct llama_vocab & vocab);
135
-
136
- llama_token llama_token_fim_pre_impl(const struct llama_vocab & vocab);
137
- llama_token llama_token_fim_suf_impl(const struct llama_vocab & vocab);
138
- llama_token llama_token_fim_mid_impl(const struct llama_vocab & vocab);
139
- llama_token llama_token_fim_pad_impl(const struct llama_vocab & vocab);
140
- llama_token llama_token_fim_rep_impl(const struct llama_vocab & vocab);
141
- llama_token llama_token_fim_sep_impl(const struct llama_vocab & vocab);
142
-
143
- bool llama_add_bos_token_impl(const struct llama_vocab & vocab);
144
- bool llama_add_eos_token_impl(const struct llama_vocab & vocab);
145
-
146
- int32_t llama_tokenize_impl(
147
- const struct llama_vocab & vocab,
148
- const char * text,
149
- int32_t text_len,
150
- llama_token * tokens,
151
- int32_t n_tokens_max,
152
- bool add_special,
153
- bool parse_special);
154
-
155
- // does not write null-terminator to buf
156
- int32_t llama_token_to_piece_impl(
157
- const struct llama_vocab & vocab,
158
- llama_token token,
159
- char * buf,
160
- int32_t length,
161
- int32_t lstrip,
162
- bool special);
163
-
164
- // check if token0 is contained as a prefix in token1
165
- bool llama_token_is_prefix_impl(
166
- const struct llama_vocab & vocab,
167
- llama_token token0,
168
- llama_token token1);
169
-
170
- int32_t llama_detokenize_impl(
171
- const struct llama_vocab & vocab,
172
- const llama_token * tokens,
173
- int32_t n_tokens,
174
- char * text,
175
- int32_t text_len_max,
176
- bool remove_special,
177
- bool unparse_special);
178
-
179
- std::string llama_detokenize(
180
- const struct llama_vocab & vocab,
181
- const std::vector<llama_token> & tokens,
182
- bool special);
1
+ #pragma once
2
+
3
+ #include "llama.h"
4
+
5
+ #include <string>
6
+ #include <vector>
7
+ #include <memory>
8
+
9
+ struct LLM_KV;
10
+ struct llama_model_loader;
11
+
12
+ struct llama_vocab {
13
+ struct token_data {
14
+ std::string text;
15
+ float score;
16
+ llama_token_attr attr;
17
+ };
18
+
19
+ llama_vocab();
20
+ ~llama_vocab();
21
+
22
+ void load(llama_model_loader & ml, const LLM_KV & kv);
23
+
24
+ enum llama_vocab_type get_type() const;
25
+ enum llama_vocab_pre_type get_pre_type() const;
26
+
27
+ uint32_t n_tokens() const;
28
+ uint32_t n_token_types() const;
29
+
30
+ std::string type_name() const;
31
+
32
+ bool is_normal (llama_token id) const;
33
+ bool is_unknown (llama_token id) const;
34
+ bool is_control (llama_token id) const;
35
+ bool is_byte (llama_token id) const;
36
+ bool is_user_defined(llama_token id) const;
37
+ bool is_unused (llama_token id) const;
38
+ bool is_eog (llama_token id) const;
39
+
40
+ uint8_t token_to_byte(llama_token id) const;
41
+ llama_token byte_to_token(uint8_t ch) const;
42
+
43
+ llama_token text_to_token(const std::string & text) const;
44
+
45
+ const token_data & get_token_data(llama_token id) const;
46
+
47
+ const char * token_get_text (llama_token id) const;
48
+ float token_get_score(llama_token id) const;
49
+ llama_token_attr token_get_attr (llama_token id) const;
50
+
51
+ llama_token token_bos() const;
52
+ llama_token token_eos() const;
53
+ llama_token token_eot() const;
54
+ llama_token token_eom() const;
55
+ llama_token token_unk() const;
56
+ llama_token token_sep() const;
57
+ llama_token token_nl () const;
58
+ llama_token token_pad() const;
59
+
60
+ llama_token token_prefix() const;
61
+ llama_token token_middle() const;
62
+ llama_token token_suffix() const;
63
+
64
+ llama_token token_fim_pre() const;
65
+ llama_token token_fim_suf() const;
66
+ llama_token token_fim_mid() const;
67
+ llama_token token_fim_pad() const;
68
+ llama_token token_fim_rep() const;
69
+ llama_token token_fim_sep() const;
70
+
71
+ bool get_add_space_prefix () const;
72
+ bool get_add_bos () const;
73
+ bool get_add_eos () const;
74
+ bool get_ignore_merges () const;
75
+ bool get_clean_spaces () const;
76
+ bool get_remove_extra_whitespaces () const;
77
+ bool get_escape_whitespaces () const;
78
+ bool get_treat_whitespace_as_suffix() const;
79
+
80
+ int max_token_len() const;
81
+
82
+ int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
83
+
84
+ int32_t tokenize(
85
+ const char * text,
86
+ int32_t text_len,
87
+ llama_token * tokens,
88
+ int32_t n_tokens_max,
89
+ bool add_special,
90
+ bool parse_special) const;
91
+
92
+ std::vector<llama_token> tokenize(
93
+ const std::string & raw_text,
94
+ bool add_special,
95
+ bool parse_special = false) const;
96
+
97
+ // does not write null-terminator to buf
98
+ int32_t token_to_piece(
99
+ llama_token token,
100
+ char * buf,
101
+ int32_t length,
102
+ int32_t lstrip,
103
+ bool special) const;
104
+
105
+ // use cached data
106
+ const std::string & token_to_piece(llama_token token) const;
107
+
108
+ int32_t detokenize(
109
+ const llama_token * tokens,
110
+ int32_t n_tokens,
111
+ char * text,
112
+ int32_t text_len_max,
113
+ bool remove_special,
114
+ bool unparse_special) const;
115
+
116
+ std::string detokenize(
117
+ const std::vector<llama_token> & tokens,
118
+ bool special) const;
119
+
120
+ void print_info() const;
121
+
122
+ private:
123
+ struct impl;
124
+ std::unique_ptr<impl> pimpl;
125
+ };