cui-llama.rn 1.2.6 → 1.3.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 (70) hide show
  1. package/README.md +3 -2
  2. package/android/src/main/CMakeLists.txt +20 -5
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +115 -27
  4. package/android/src/main/java/com/rnllama/RNLlama.java +40 -7
  5. package/android/src/main/jni.cpp +222 -34
  6. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +9 -4
  7. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +9 -4
  8. package/cpp/common.cpp +1682 -2114
  9. package/cpp/common.h +600 -613
  10. package/cpp/ggml-aarch64.c +129 -3478
  11. package/cpp/ggml-aarch64.h +19 -39
  12. package/cpp/ggml-alloc.c +1040 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +216 -216
  15. package/cpp/ggml-backend-reg.cpp +195 -0
  16. package/cpp/ggml-backend.cpp +1997 -2661
  17. package/cpp/ggml-backend.h +328 -314
  18. package/cpp/ggml-common.h +1853 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  20. package/cpp/ggml-cpu-aarch64.c +3560 -0
  21. package/cpp/ggml-cpu-aarch64.h +30 -0
  22. package/cpp/ggml-cpu-impl.h +371 -614
  23. package/cpp/ggml-cpu-quants.c +10822 -0
  24. package/cpp/ggml-cpu-quants.h +63 -0
  25. package/cpp/ggml-cpu.c +13975 -13720
  26. package/cpp/ggml-cpu.cpp +663 -0
  27. package/cpp/ggml-cpu.h +177 -150
  28. package/cpp/ggml-impl.h +550 -296
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4294 -3933
  31. package/cpp/ggml-quants.c +5247 -15739
  32. package/cpp/ggml-quants.h +100 -147
  33. package/cpp/ggml-threading.cpp +12 -0
  34. package/cpp/ggml-threading.h +12 -0
  35. package/cpp/ggml.c +8180 -8390
  36. package/cpp/ggml.h +2411 -2441
  37. package/cpp/llama-grammar.cpp +1138 -1138
  38. package/cpp/llama-grammar.h +144 -144
  39. package/cpp/llama-impl.h +181 -181
  40. package/cpp/llama-sampling.cpp +2348 -2345
  41. package/cpp/llama-sampling.h +48 -48
  42. package/cpp/llama-vocab.cpp +1984 -1984
  43. package/cpp/llama-vocab.h +170 -170
  44. package/cpp/llama.cpp +22132 -22046
  45. package/cpp/llama.h +1253 -1255
  46. package/cpp/log.cpp +401 -401
  47. package/cpp/log.h +121 -121
  48. package/cpp/rn-llama.hpp +83 -19
  49. package/cpp/sampling.cpp +466 -466
  50. package/cpp/sgemm.cpp +1884 -1276
  51. package/ios/RNLlama.mm +43 -20
  52. package/ios/RNLlamaContext.h +9 -3
  53. package/ios/RNLlamaContext.mm +133 -33
  54. package/jest/mock.js +0 -1
  55. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  56. package/lib/commonjs/index.js +52 -15
  57. package/lib/commonjs/index.js.map +1 -1
  58. package/lib/module/NativeRNLlama.js.map +1 -1
  59. package/lib/module/index.js +51 -15
  60. package/lib/module/index.js.map +1 -1
  61. package/lib/typescript/NativeRNLlama.d.ts +29 -5
  62. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  63. package/lib/typescript/index.d.ts +12 -5
  64. package/lib/typescript/index.d.ts.map +1 -1
  65. package/package.json +1 -1
  66. package/src/NativeRNLlama.ts +41 -6
  67. package/src/index.ts +82 -27
  68. package/cpp/json-schema-to-grammar.cpp +0 -1045
  69. package/cpp/json-schema-to-grammar.h +0 -8
  70. package/cpp/json.hpp +0 -24766
package/cpp/llama-vocab.h CHANGED
@@ -1,170 +1,170 @@
1
- #pragma once
2
-
3
- #include "llama-impl.h"
4
-
5
- #include <string>
6
- #include <vector>
7
- #include <unordered_map>
8
- #include <map>
9
- #include <set>
10
-
11
- struct llm_tokenizer;
12
-
13
- struct llama_vocab {
14
- using id = llama_token;
15
- using token = std::string;
16
- using tattr = llama_token_attr;
17
-
18
- struct token_data {
19
- token text;
20
- float score;
21
- tattr attr;
22
- };
23
-
24
- uint32_t n_vocab = 0; // TODO: not great because has to keep in sync with hparams.n_vocab
25
-
26
- enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
27
- enum llama_vocab_pre_type type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
28
-
29
- int max_token_len = 0; // used for optimizing longest token search
30
-
31
- std::unordered_map<token, id> token_to_id;
32
- std::vector<token_data> id_to_token;
33
-
34
- std::vector<id> cache_special_tokens;
35
- std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = true);
36
-
37
- std::map<std::pair<std::string, std::string>, int> bpe_ranks;
38
-
39
- // default LLaMA special tokens
40
- // TODO: should we set all of these to LLAMA_TOKEN_NULL?
41
- id special_bos_id = 1;
42
- id special_eos_id = 2;
43
- id special_eot_id = LLAMA_TOKEN_NULL;
44
- id special_eom_id = LLAMA_TOKEN_NULL;
45
- id special_unk_id = 0;
46
- id special_sep_id = LLAMA_TOKEN_NULL;
47
- id special_pad_id = LLAMA_TOKEN_NULL;
48
- id special_cls_id = LLAMA_TOKEN_NULL;
49
- id special_mask_id = LLAMA_TOKEN_NULL;
50
-
51
- id linefeed_id = 13;
52
-
53
- // fim tokens
54
- id special_fim_pre_id = LLAMA_TOKEN_NULL;
55
- id special_fim_suf_id = LLAMA_TOKEN_NULL;
56
- id special_fim_mid_id = LLAMA_TOKEN_NULL;
57
- id special_fim_pad_id = LLAMA_TOKEN_NULL;
58
- id special_fim_rep_id = LLAMA_TOKEN_NULL; // repo
59
- id special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator
60
-
61
- // set of all tokens that cause "end of generation"
62
- std::set<id> special_eog_ids;
63
-
64
- // tokenizer flags
65
- bool tokenizer_add_space_prefix = false;
66
- bool tokenizer_add_bos = false;
67
- bool tokenizer_add_eos = false;
68
- bool tokenizer_ignore_merges = false;
69
- bool tokenizer_clean_spaces = false; // clean_up_tokenization_spaces
70
- bool tokenizer_remove_extra_whitespaces = false;
71
- bool tokenizer_escape_whitespaces = true;
72
- bool tokenizer_treat_whitespace_as_suffix = false;
73
-
74
- std::vector<char> precompiled_charsmap;
75
-
76
- llm_tokenizer * tokenizer = nullptr;
77
-
78
- llama_vocab() = default;
79
- ~llama_vocab();
80
-
81
- int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
82
-
83
- void init_tokenizer();
84
- };
85
-
86
- //
87
- // internal API
88
- //
89
-
90
- // TODO: rename to llama_tokenize_impl
91
- // TODO: This should probably be in llama.h
92
- std::vector<llama_vocab::id> llama_tokenize_internal(
93
- const llama_vocab & vocab,
94
- std::string raw_text,
95
- bool add_special,
96
- bool parse_special = false);
97
-
98
- // TODO: move the API below as member functions of llama_vocab
99
- llama_token llama_byte_to_token_impl(const llama_vocab & vocab, uint8_t ch);
100
-
101
- const char * llama_token_get_text_impl(const struct llama_vocab & vocab, llama_token token);
102
-
103
- float llama_token_get_score_impl(const struct llama_vocab & vocab, llama_token token);
104
-
105
- llama_token_attr llama_token_get_attr_impl(const struct llama_vocab & vocab, llama_token token);
106
-
107
- bool llama_token_is_eog_impl(const struct llama_vocab & vocab, llama_token token);
108
-
109
- bool llama_token_is_control_impl(const struct llama_vocab & vocab, llama_token token);
110
-
111
- llama_token llama_token_bos_impl(const struct llama_vocab & vocab);
112
- llama_token llama_token_eos_impl(const struct llama_vocab & vocab);
113
- llama_token llama_token_eot_impl(const struct llama_vocab & vocab);
114
- llama_token llama_token_eom_impl(const struct llama_vocab & vocab);
115
- llama_token llama_token_cls_impl(const struct llama_vocab & vocab);
116
- llama_token llama_token_sep_impl(const struct llama_vocab & vocab);
117
- llama_token llama_token_nl_impl (const struct llama_vocab & vocab);
118
- llama_token llama_token_pad_impl(const struct llama_vocab & vocab);
119
-
120
- llama_token llama_token_prefix_impl(const struct llama_vocab & vocab);
121
- llama_token llama_token_middle_impl(const struct llama_vocab & vocab);
122
- llama_token llama_token_suffix_impl(const struct llama_vocab & vocab);
123
-
124
- llama_token llama_token_fim_pre_impl(const struct llama_vocab & vocab);
125
- llama_token llama_token_fim_suf_impl(const struct llama_vocab & vocab);
126
- llama_token llama_token_fim_mid_impl(const struct llama_vocab & vocab);
127
- llama_token llama_token_fim_pad_impl(const struct llama_vocab & vocab);
128
- llama_token llama_token_fim_rep_impl(const struct llama_vocab & vocab);
129
- llama_token llama_token_fim_sep_impl(const struct llama_vocab & vocab);
130
-
131
- bool llama_add_bos_token_impl(const struct llama_vocab & vocab);
132
- bool llama_add_eos_token_impl(const struct llama_vocab & vocab);
133
-
134
- int32_t llama_tokenize_impl(
135
- const struct llama_vocab & vocab,
136
- const char * text,
137
- int32_t text_len,
138
- llama_token * tokens,
139
- int32_t n_tokens_max,
140
- bool add_special,
141
- bool parse_special);
142
-
143
- // does not write null-terminator to buf
144
- int32_t llama_token_to_piece_impl(
145
- const struct llama_vocab & vocab,
146
- llama_token token,
147
- char * buf,
148
- int32_t length,
149
- int32_t lstrip,
150
- bool special);
151
-
152
- // check if token0 is contained as a prefix in token1
153
- bool llama_token_is_prefix_impl(
154
- const struct llama_vocab & vocab,
155
- llama_token token0,
156
- llama_token token1);
157
-
158
- int32_t llama_detokenize_impl(
159
- const struct llama_vocab & vocab,
160
- const llama_token * tokens,
161
- int32_t n_tokens,
162
- char * text,
163
- int32_t text_len_max,
164
- bool remove_special,
165
- bool unparse_special);
166
-
167
- std::string llama_detokenize(
168
- const struct llama_vocab & vocab,
169
- const std::vector<llama_token> & tokens,
170
- bool special);
1
+ #pragma once
2
+
3
+ #include "llama-impl.h"
4
+
5
+ #include <string>
6
+ #include <vector>
7
+ #include <unordered_map>
8
+ #include <map>
9
+ #include <set>
10
+
11
+ struct llm_tokenizer;
12
+
13
+ struct llama_vocab {
14
+ using id = llama_token;
15
+ using token = std::string;
16
+ using tattr = llama_token_attr;
17
+
18
+ struct token_data {
19
+ token text;
20
+ float score;
21
+ tattr attr;
22
+ };
23
+
24
+ uint32_t n_vocab = 0; // TODO: not great because has to keep in sync with hparams.n_vocab
25
+
26
+ enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
27
+ enum llama_vocab_pre_type type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
28
+
29
+ int max_token_len = 0; // used for optimizing longest token search
30
+
31
+ std::unordered_map<token, id> token_to_id;
32
+ std::vector<token_data> id_to_token;
33
+
34
+ std::vector<id> cache_special_tokens;
35
+ std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = true);
36
+
37
+ std::map<std::pair<std::string, std::string>, int> bpe_ranks;
38
+
39
+ // default LLaMA special tokens
40
+ // TODO: should we set all of these to LLAMA_TOKEN_NULL?
41
+ id special_bos_id = 1;
42
+ id special_eos_id = 2;
43
+ id special_eot_id = LLAMA_TOKEN_NULL;
44
+ id special_eom_id = LLAMA_TOKEN_NULL;
45
+ id special_unk_id = 0;
46
+ id special_sep_id = LLAMA_TOKEN_NULL;
47
+ id special_pad_id = LLAMA_TOKEN_NULL;
48
+ id special_cls_id = LLAMA_TOKEN_NULL;
49
+ id special_mask_id = LLAMA_TOKEN_NULL;
50
+
51
+ id linefeed_id = 13;
52
+
53
+ // fim tokens
54
+ id special_fim_pre_id = LLAMA_TOKEN_NULL;
55
+ id special_fim_suf_id = LLAMA_TOKEN_NULL;
56
+ id special_fim_mid_id = LLAMA_TOKEN_NULL;
57
+ id special_fim_pad_id = LLAMA_TOKEN_NULL;
58
+ id special_fim_rep_id = LLAMA_TOKEN_NULL; // repo
59
+ id special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator
60
+
61
+ // set of all tokens that cause "end of generation"
62
+ std::set<id> special_eog_ids;
63
+
64
+ // tokenizer flags
65
+ bool tokenizer_add_space_prefix = false;
66
+ bool tokenizer_add_bos = false;
67
+ bool tokenizer_add_eos = false;
68
+ bool tokenizer_ignore_merges = false;
69
+ bool tokenizer_clean_spaces = false; // clean_up_tokenization_spaces
70
+ bool tokenizer_remove_extra_whitespaces = false;
71
+ bool tokenizer_escape_whitespaces = true;
72
+ bool tokenizer_treat_whitespace_as_suffix = false;
73
+
74
+ std::vector<char> precompiled_charsmap;
75
+
76
+ llm_tokenizer * tokenizer = nullptr;
77
+
78
+ llama_vocab() = default;
79
+ ~llama_vocab();
80
+
81
+ int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
82
+
83
+ void init_tokenizer();
84
+ };
85
+
86
+ //
87
+ // internal API
88
+ //
89
+
90
+ // TODO: rename to llama_tokenize_impl
91
+ // TODO: This should probably be in llama.h
92
+ std::vector<llama_vocab::id> llama_tokenize_internal(
93
+ const llama_vocab & vocab,
94
+ std::string raw_text,
95
+ bool add_special,
96
+ bool parse_special = false);
97
+
98
+ // TODO: move the API below as member functions of llama_vocab
99
+ llama_token llama_byte_to_token_impl(const llama_vocab & vocab, uint8_t ch);
100
+
101
+ const char * llama_token_get_text_impl(const struct llama_vocab & vocab, llama_token token);
102
+
103
+ float llama_token_get_score_impl(const struct llama_vocab & vocab, llama_token token);
104
+
105
+ llama_token_attr llama_token_get_attr_impl(const struct llama_vocab & vocab, llama_token token);
106
+
107
+ bool llama_token_is_eog_impl(const struct llama_vocab & vocab, llama_token token);
108
+
109
+ bool llama_token_is_control_impl(const struct llama_vocab & vocab, llama_token token);
110
+
111
+ llama_token llama_token_bos_impl(const struct llama_vocab & vocab);
112
+ llama_token llama_token_eos_impl(const struct llama_vocab & vocab);
113
+ llama_token llama_token_eot_impl(const struct llama_vocab & vocab);
114
+ llama_token llama_token_eom_impl(const struct llama_vocab & vocab);
115
+ llama_token llama_token_cls_impl(const struct llama_vocab & vocab);
116
+ llama_token llama_token_sep_impl(const struct llama_vocab & vocab);
117
+ llama_token llama_token_nl_impl (const struct llama_vocab & vocab);
118
+ llama_token llama_token_pad_impl(const struct llama_vocab & vocab);
119
+
120
+ llama_token llama_token_prefix_impl(const struct llama_vocab & vocab);
121
+ llama_token llama_token_middle_impl(const struct llama_vocab & vocab);
122
+ llama_token llama_token_suffix_impl(const struct llama_vocab & vocab);
123
+
124
+ llama_token llama_token_fim_pre_impl(const struct llama_vocab & vocab);
125
+ llama_token llama_token_fim_suf_impl(const struct llama_vocab & vocab);
126
+ llama_token llama_token_fim_mid_impl(const struct llama_vocab & vocab);
127
+ llama_token llama_token_fim_pad_impl(const struct llama_vocab & vocab);
128
+ llama_token llama_token_fim_rep_impl(const struct llama_vocab & vocab);
129
+ llama_token llama_token_fim_sep_impl(const struct llama_vocab & vocab);
130
+
131
+ bool llama_add_bos_token_impl(const struct llama_vocab & vocab);
132
+ bool llama_add_eos_token_impl(const struct llama_vocab & vocab);
133
+
134
+ int32_t llama_tokenize_impl(
135
+ const struct llama_vocab & vocab,
136
+ const char * text,
137
+ int32_t text_len,
138
+ llama_token * tokens,
139
+ int32_t n_tokens_max,
140
+ bool add_special,
141
+ bool parse_special);
142
+
143
+ // does not write null-terminator to buf
144
+ int32_t llama_token_to_piece_impl(
145
+ const struct llama_vocab & vocab,
146
+ llama_token token,
147
+ char * buf,
148
+ int32_t length,
149
+ int32_t lstrip,
150
+ bool special);
151
+
152
+ // check if token0 is contained as a prefix in token1
153
+ bool llama_token_is_prefix_impl(
154
+ const struct llama_vocab & vocab,
155
+ llama_token token0,
156
+ llama_token token1);
157
+
158
+ int32_t llama_detokenize_impl(
159
+ const struct llama_vocab & vocab,
160
+ const llama_token * tokens,
161
+ int32_t n_tokens,
162
+ char * text,
163
+ int32_t text_len_max,
164
+ bool remove_special,
165
+ bool unparse_special);
166
+
167
+ std::string llama_detokenize(
168
+ const struct llama_vocab & vocab,
169
+ const std::vector<llama_token> & tokens,
170
+ bool special);