cui-llama.rn 1.3.0 → 1.3.3

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 (76) hide show
  1. package/android/src/main/CMakeLists.txt +6 -1
  2. package/android/src/main/jni.cpp +6 -6
  3. package/cpp/amx/amx.cpp +196 -0
  4. package/cpp/amx/amx.h +20 -0
  5. package/cpp/amx/common.h +101 -0
  6. package/cpp/amx/mmq.cpp +2524 -0
  7. package/cpp/amx/mmq.h +16 -0
  8. package/cpp/common.cpp +1981 -1682
  9. package/cpp/common.h +636 -600
  10. package/cpp/ggml-aarch64.c +129 -129
  11. package/cpp/ggml-aarch64.h +19 -19
  12. package/cpp/ggml-alloc.c +1038 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +238 -216
  15. package/cpp/ggml-backend-reg.cpp +423 -195
  16. package/cpp/ggml-backend.cpp +1999 -1997
  17. package/cpp/ggml-backend.h +351 -328
  18. package/cpp/ggml-common.h +1859 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  20. package/cpp/ggml-cpu-aarch64.c +3823 -3560
  21. package/cpp/ggml-cpu-aarch64.h +32 -30
  22. package/cpp/ggml-cpu-impl.h +386 -371
  23. package/cpp/ggml-cpu-quants.c +10835 -10822
  24. package/cpp/ggml-cpu-quants.h +63 -63
  25. package/cpp/ggml-cpu.c +99 -103
  26. package/cpp/ggml-cpu.cpp +69 -17
  27. package/cpp/ggml-cpu.h +152 -177
  28. package/cpp/ggml-impl.h +556 -550
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4426 -4294
  31. package/cpp/ggml-quants.c +5247 -5247
  32. package/cpp/ggml-quants.h +100 -100
  33. package/cpp/ggml-threading.cpp +12 -12
  34. package/cpp/ggml-threading.h +12 -12
  35. package/cpp/ggml.c +7618 -8180
  36. package/cpp/ggml.h +2255 -2411
  37. package/cpp/json-schema-to-grammar.cpp +1045 -0
  38. package/cpp/json-schema-to-grammar.h +8 -0
  39. package/cpp/json.hpp +24766 -0
  40. package/cpp/llama-grammar.cpp +1138 -1138
  41. package/cpp/llama-grammar.h +144 -144
  42. package/cpp/llama-impl.h +181 -181
  43. package/cpp/llama-sampling.cpp +2348 -2348
  44. package/cpp/llama-sampling.h +48 -48
  45. package/cpp/llama-vocab.cpp +1984 -1984
  46. package/cpp/llama-vocab.h +170 -170
  47. package/cpp/llama.cpp +22332 -22132
  48. package/cpp/llama.h +1259 -1253
  49. package/cpp/log.cpp +401 -401
  50. package/cpp/log.h +121 -121
  51. package/cpp/rn-llama.hpp +6 -6
  52. package/cpp/sampling.cpp +505 -466
  53. package/cpp/sampling.h +22 -1
  54. package/cpp/sgemm.cpp +1884 -1884
  55. package/cpp/speculative.cpp +270 -0
  56. package/cpp/speculative.h +28 -0
  57. package/cpp/unicode.cpp +11 -0
  58. package/ios/RNLlamaContext.mm +13 -0
  59. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  60. package/lib/commonjs/grammar.js +4 -2
  61. package/lib/commonjs/grammar.js.map +1 -1
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/module/NativeRNLlama.js.map +1 -1
  64. package/lib/module/grammar.js +2 -1
  65. package/lib/module/grammar.js.map +1 -1
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/typescript/NativeRNLlama.d.ts +94 -4
  68. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  69. package/lib/typescript/grammar.d.ts +5 -6
  70. package/lib/typescript/grammar.d.ts.map +1 -1
  71. package/lib/typescript/index.d.ts +4 -2
  72. package/lib/typescript/index.d.ts.map +1 -1
  73. package/package.json +2 -1
  74. package/src/NativeRNLlama.ts +97 -10
  75. package/src/grammar.ts +10 -8
  76. package/src/index.ts +22 -1
@@ -1,144 +1,144 @@
1
- #pragma once
2
-
3
- #include "llama-impl.h"
4
-
5
- #include <map>
6
-
7
- struct llama_vocab;
8
-
9
- // grammar element type
10
- enum llama_gretype {
11
- // end of rule definition
12
- LLAMA_GRETYPE_END = 0,
13
-
14
- // start of alternate definition for rule
15
- LLAMA_GRETYPE_ALT = 1,
16
-
17
- // non-terminal element: reference to rule
18
- LLAMA_GRETYPE_RULE_REF = 2,
19
-
20
- // terminal element: character (code point)
21
- LLAMA_GRETYPE_CHAR = 3,
22
-
23
- // inverse char(s) ([^a], [^a-b] [^abc])
24
- LLAMA_GRETYPE_CHAR_NOT = 4,
25
-
26
- // modifies a preceding LLAMA_GRETYPE_CHAR or LLAMA_GRETYPE_CHAR_ALT to
27
- // be an inclusive range ([a-z])
28
- LLAMA_GRETYPE_CHAR_RNG_UPPER = 5,
29
-
30
- // modifies a preceding LLAMA_GRETYPE_CHAR or
31
- // LLAMA_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
32
- LLAMA_GRETYPE_CHAR_ALT = 6,
33
-
34
- // any character (.)
35
- LLAMA_GRETYPE_CHAR_ANY = 7,
36
- };
37
-
38
- typedef struct llama_grammar_element {
39
- enum llama_gretype type;
40
- uint32_t value; // Unicode code point or rule ID
41
- } llama_grammar_element;
42
-
43
- struct llama_partial_utf8 {
44
- uint32_t value; // bit value so far (unshifted)
45
- int n_remain; // num bytes remaining; -1 indicates invalid sequence
46
- };
47
-
48
- struct llama_grammar_candidate {
49
- size_t index;
50
- const uint32_t * code_points;
51
- llama_partial_utf8 partial_utf8;
52
- };
53
-
54
- using llama_grammar_rule = std::vector< llama_grammar_element>;
55
- using llama_grammar_stack = std::vector<const llama_grammar_element *>;
56
-
57
- using llama_grammar_rules = std::vector<llama_grammar_rule>;
58
- using llama_grammar_stacks = std::vector<llama_grammar_stack>;
59
- using llama_grammar_candidates = std::vector<llama_grammar_candidate>;
60
-
61
- const llama_grammar_rules & llama_grammar_get_rules (const struct llama_grammar * grammar);
62
- llama_grammar_stacks & llama_grammar_get_stacks( struct llama_grammar * grammar);
63
-
64
- // takes a set of possible pushdown stacks on a grammar, which are required to
65
- // be positioned at a character range (see `llama_grammar_advance_stack`), and
66
- // produces the N possible stacks if the given char is accepted at those
67
- // positions
68
- void llama_grammar_accept(
69
- const llama_grammar_rules & rules,
70
- const llama_grammar_stacks & stacks,
71
- uint32_t chr,
72
- llama_grammar_stacks & stacks_new);
73
-
74
- std::vector<llama_grammar_candidate> llama_grammar_reject_candidates_for_stack(
75
- const llama_grammar_rules & rules,
76
- const llama_grammar_stack & stack,
77
- const llama_grammar_candidates & candidates);
78
-
79
- struct llama_grammar_parser {
80
- std::map<std::string, uint32_t> symbol_ids;
81
-
82
- llama_grammar_rules rules;
83
-
84
- llama_grammar_stack c_rules() const;
85
-
86
- uint32_t get_symbol_id(const char * src, size_t len);
87
- uint32_t generate_symbol_id(const std::string & base_name);
88
-
89
- void add_rule(uint32_t rule_id, const llama_grammar_rule & rule);
90
-
91
- const char * parse_alternates(
92
- const char * src,
93
- const std::string & rule_name,
94
- uint32_t rule_id,
95
- bool is_nested);
96
-
97
- const char * parse_sequence(
98
- const char * src,
99
- const std::string & rule_name,
100
- llama_grammar_rule & rule,
101
- bool is_nested);
102
-
103
- const char * parse_rule(const char * src);
104
-
105
- bool parse(const char * src);
106
- void print(FILE * file);
107
- };
108
-
109
- struct llama_grammar {
110
- // note: allow null vocab for testing (not great)
111
- const llama_vocab * vocab;
112
-
113
- const llama_grammar_rules rules; // TODO: shared ptr
114
- llama_grammar_stacks stacks;
115
-
116
- // buffer for partially generated UTF-8 sequence from accepted tokens
117
- llama_partial_utf8 partial_utf8;
118
- };
119
-
120
- //
121
- // internal API
122
- //
123
-
124
- // note: needed for tests (not great)
125
- struct llama_grammar * llama_grammar_init_impl(
126
- const struct llama_vocab * vocab,
127
- const llama_grammar_element ** rules,
128
- size_t n_rules,
129
- size_t start_rule_index);
130
-
131
- struct llama_grammar * llama_grammar_init_impl(const struct llama_vocab * vocab, const char * grammar_str, const char * grammar_root);
132
-
133
- void llama_grammar_free_impl(struct llama_grammar * grammar);
134
-
135
- struct llama_grammar * llama_grammar_clone_impl(const struct llama_grammar & grammar);
136
-
137
- // TODO: move the API below as member functions of llama_grammar
138
- void llama_grammar_apply_impl(
139
- const struct llama_grammar & grammar,
140
- llama_token_data_array * cur_p);
141
-
142
- void llama_grammar_accept_impl(
143
- struct llama_grammar & grammar,
144
- llama_token token);
1
+ #pragma once
2
+
3
+ #include "llama-impl.h"
4
+
5
+ #include <map>
6
+
7
+ struct llama_vocab;
8
+
9
+ // grammar element type
10
+ enum llama_gretype {
11
+ // end of rule definition
12
+ LLAMA_GRETYPE_END = 0,
13
+
14
+ // start of alternate definition for rule
15
+ LLAMA_GRETYPE_ALT = 1,
16
+
17
+ // non-terminal element: reference to rule
18
+ LLAMA_GRETYPE_RULE_REF = 2,
19
+
20
+ // terminal element: character (code point)
21
+ LLAMA_GRETYPE_CHAR = 3,
22
+
23
+ // inverse char(s) ([^a], [^a-b] [^abc])
24
+ LLAMA_GRETYPE_CHAR_NOT = 4,
25
+
26
+ // modifies a preceding LLAMA_GRETYPE_CHAR or LLAMA_GRETYPE_CHAR_ALT to
27
+ // be an inclusive range ([a-z])
28
+ LLAMA_GRETYPE_CHAR_RNG_UPPER = 5,
29
+
30
+ // modifies a preceding LLAMA_GRETYPE_CHAR or
31
+ // LLAMA_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
32
+ LLAMA_GRETYPE_CHAR_ALT = 6,
33
+
34
+ // any character (.)
35
+ LLAMA_GRETYPE_CHAR_ANY = 7,
36
+ };
37
+
38
+ typedef struct llama_grammar_element {
39
+ enum llama_gretype type;
40
+ uint32_t value; // Unicode code point or rule ID
41
+ } llama_grammar_element;
42
+
43
+ struct llama_partial_utf8 {
44
+ uint32_t value; // bit value so far (unshifted)
45
+ int n_remain; // num bytes remaining; -1 indicates invalid sequence
46
+ };
47
+
48
+ struct llama_grammar_candidate {
49
+ size_t index;
50
+ const uint32_t * code_points;
51
+ llama_partial_utf8 partial_utf8;
52
+ };
53
+
54
+ using llama_grammar_rule = std::vector< llama_grammar_element>;
55
+ using llama_grammar_stack = std::vector<const llama_grammar_element *>;
56
+
57
+ using llama_grammar_rules = std::vector<llama_grammar_rule>;
58
+ using llama_grammar_stacks = std::vector<llama_grammar_stack>;
59
+ using llama_grammar_candidates = std::vector<llama_grammar_candidate>;
60
+
61
+ const llama_grammar_rules & llama_grammar_get_rules (const struct llama_grammar * grammar);
62
+ llama_grammar_stacks & llama_grammar_get_stacks( struct llama_grammar * grammar);
63
+
64
+ // takes a set of possible pushdown stacks on a grammar, which are required to
65
+ // be positioned at a character range (see `llama_grammar_advance_stack`), and
66
+ // produces the N possible stacks if the given char is accepted at those
67
+ // positions
68
+ void llama_grammar_accept(
69
+ const llama_grammar_rules & rules,
70
+ const llama_grammar_stacks & stacks,
71
+ uint32_t chr,
72
+ llama_grammar_stacks & stacks_new);
73
+
74
+ std::vector<llama_grammar_candidate> llama_grammar_reject_candidates_for_stack(
75
+ const llama_grammar_rules & rules,
76
+ const llama_grammar_stack & stack,
77
+ const llama_grammar_candidates & candidates);
78
+
79
+ struct llama_grammar_parser {
80
+ std::map<std::string, uint32_t> symbol_ids;
81
+
82
+ llama_grammar_rules rules;
83
+
84
+ llama_grammar_stack c_rules() const;
85
+
86
+ uint32_t get_symbol_id(const char * src, size_t len);
87
+ uint32_t generate_symbol_id(const std::string & base_name);
88
+
89
+ void add_rule(uint32_t rule_id, const llama_grammar_rule & rule);
90
+
91
+ const char * parse_alternates(
92
+ const char * src,
93
+ const std::string & rule_name,
94
+ uint32_t rule_id,
95
+ bool is_nested);
96
+
97
+ const char * parse_sequence(
98
+ const char * src,
99
+ const std::string & rule_name,
100
+ llama_grammar_rule & rule,
101
+ bool is_nested);
102
+
103
+ const char * parse_rule(const char * src);
104
+
105
+ bool parse(const char * src);
106
+ void print(FILE * file);
107
+ };
108
+
109
+ struct llama_grammar {
110
+ // note: allow null vocab for testing (not great)
111
+ const llama_vocab * vocab;
112
+
113
+ const llama_grammar_rules rules; // TODO: shared ptr
114
+ llama_grammar_stacks stacks;
115
+
116
+ // buffer for partially generated UTF-8 sequence from accepted tokens
117
+ llama_partial_utf8 partial_utf8;
118
+ };
119
+
120
+ //
121
+ // internal API
122
+ //
123
+
124
+ // note: needed for tests (not great)
125
+ struct llama_grammar * llama_grammar_init_impl(
126
+ const struct llama_vocab * vocab,
127
+ const llama_grammar_element ** rules,
128
+ size_t n_rules,
129
+ size_t start_rule_index);
130
+
131
+ struct llama_grammar * llama_grammar_init_impl(const struct llama_vocab * vocab, const char * grammar_str, const char * grammar_root);
132
+
133
+ void llama_grammar_free_impl(struct llama_grammar * grammar);
134
+
135
+ struct llama_grammar * llama_grammar_clone_impl(const struct llama_grammar & grammar);
136
+
137
+ // TODO: move the API below as member functions of llama_grammar
138
+ void llama_grammar_apply_impl(
139
+ const struct llama_grammar & grammar,
140
+ llama_token_data_array * cur_p);
141
+
142
+ void llama_grammar_accept_impl(
143
+ struct llama_grammar & grammar,
144
+ llama_token token);
package/cpp/llama-impl.h CHANGED
@@ -1,181 +1,181 @@
1
- #pragma once
2
-
3
- #include "llama.h"
4
-
5
- #include <string>
6
- #include <vector>
7
- #include <stdexcept>
8
-
9
- #ifdef __GNUC__
10
- #ifdef __MINGW32__
11
- #define LLAMA_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
12
- #else
13
- #define LLAMA_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
14
- #endif
15
- #else
16
- #define LLAMA_ATTRIBUTE_FORMAT(...)
17
- #endif
18
-
19
- //
20
- // logging
21
- //
22
-
23
- LLAMA_ATTRIBUTE_FORMAT(2, 3)
24
- void llama_log_internal (lm_ggml_log_level level, const char * format, ...);
25
- void llama_log_callback_default(lm_ggml_log_level level, const char * text, void * user_data);
26
-
27
- #define LLAMA_LOG(...) llama_log_internal(LM_GGML_LOG_LEVEL_NONE , __VA_ARGS__)
28
- #define LLAMA_LOG_INFO(...) llama_log_internal(LM_GGML_LOG_LEVEL_INFO , __VA_ARGS__)
29
- #define LLAMA_LOG_WARN(...) llama_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
30
- #define LLAMA_LOG_ERROR(...) llama_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
31
- #define LLAMA_LOG_DEBUG(...) llama_log_internal(LM_GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
32
- #define LLAMA_LOG_CONT(...) llama_log_internal(LM_GGML_LOG_LEVEL_CONT , __VA_ARGS__)
33
-
34
- //
35
- // helpers
36
- //
37
-
38
- struct time_meas {
39
- time_meas(int64_t & t_acc, bool disable = false) : t_start_us(disable ? -1 : lm_ggml_time_us()), t_acc(t_acc) {}
40
-
41
- ~time_meas() {
42
- if (t_start_us >= 0) {
43
- t_acc += lm_ggml_time_us() - t_start_us;
44
- }
45
- }
46
-
47
- const int64_t t_start_us;
48
-
49
- int64_t & t_acc;
50
- };
51
-
52
- static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
53
- if (search.empty()) {
54
- return;
55
- }
56
- std::string builder;
57
- builder.reserve(s.length());
58
- size_t pos = 0;
59
- size_t last_pos = 0;
60
- while ((pos = s.find(search, last_pos)) != std::string::npos) {
61
- builder.append(s, last_pos, pos - last_pos);
62
- builder.append(replace);
63
- last_pos = pos + search.length();
64
- }
65
- builder.append(s, last_pos, std::string::npos);
66
- s = std::move(builder);
67
- }
68
-
69
- const std::vector<std::pair<std::string, struct lm_ggml_tensor *>> & llama_internal_get_tensor_map(
70
- struct llama_context * ctx
71
- );
72
-
73
- // the ring buffer works similarly to std::deque, but with a fixed capacity
74
- template<typename T>
75
- struct ring_buffer {
76
- ring_buffer(size_t cap) : capacity(cap), data(cap) {}
77
-
78
- T & front() {
79
- if (sz == 0) {
80
- throw std::runtime_error("ring buffer is empty");
81
- }
82
- return data[first];
83
- }
84
-
85
- const T & front() const {
86
- if (sz == 0) {
87
- throw std::runtime_error("ring buffer is empty");
88
- }
89
- return data[first];
90
- }
91
-
92
- T & back() {
93
- if (sz == 0) {
94
- throw std::runtime_error("ring buffer is empty");
95
- }
96
- return data[pos];
97
- }
98
-
99
- const T & back() const {
100
- if (sz == 0) {
101
- throw std::runtime_error("ring buffer is empty");
102
- }
103
- return data[pos];
104
- }
105
-
106
- void push_back(const T & value) {
107
- if (capacity == 0) {
108
- throw std::runtime_error("ring buffer: capacity is zero");
109
- }
110
-
111
- if (sz == capacity) {
112
- // advance the start when buffer is full
113
- first = (first + 1) % capacity;
114
- } else {
115
- sz++;
116
- }
117
- data[pos] = value;
118
- pos = (pos + 1) % capacity;
119
- }
120
-
121
- T pop_front() {
122
- if (sz == 0) {
123
- throw std::runtime_error("ring buffer is empty");
124
- }
125
- T value = data[first];
126
- first = (first + 1) % capacity;
127
- sz--;
128
- return value;
129
- }
130
-
131
- //T & operator[](size_t i) {
132
- // if (i >= sz) {
133
- // throw std::runtime_error("ring buffer: index out of bounds");
134
- // }
135
- // return data[(first + i) % capacity];
136
- //}
137
-
138
- //const T & at(size_t i) const {
139
- // if (i >= sz) {
140
- // throw std::runtime_error("ring buffer: index out of bounds");
141
- // }
142
- // return data[(first + i) % capacity];
143
- //}
144
-
145
- const T & rat(size_t i) const {
146
- if (i >= sz) {
147
- throw std::runtime_error("ring buffer: index out of bounds");
148
- }
149
- return data[(first + sz - i - 1) % capacity];
150
- }
151
-
152
- std::vector<T> to_vector() const {
153
- std::vector<T> result;
154
- result.reserve(sz);
155
- for (size_t i = 0; i < sz; i++) {
156
- result.push_back(data[(first + i) % capacity]);
157
- }
158
- return result;
159
- }
160
-
161
- void clear() {
162
- // here only reset the status of the buffer
163
- sz = 0;
164
- first = 0;
165
- pos = 0;
166
- }
167
-
168
- bool empty() const {
169
- return sz == 0;
170
- }
171
-
172
- size_t size() const {
173
- return sz;
174
- }
175
-
176
- size_t capacity = 0;
177
- size_t sz = 0;
178
- size_t first = 0;
179
- size_t pos = 0;
180
- std::vector<T> data;
181
- };
1
+ #pragma once
2
+
3
+ #include "llama.h"
4
+
5
+ #include <string>
6
+ #include <vector>
7
+ #include <stdexcept>
8
+
9
+ #ifdef __GNUC__
10
+ #ifdef __MINGW32__
11
+ #define LLAMA_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
12
+ #else
13
+ #define LLAMA_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
14
+ #endif
15
+ #else
16
+ #define LLAMA_ATTRIBUTE_FORMAT(...)
17
+ #endif
18
+
19
+ //
20
+ // logging
21
+ //
22
+
23
+ LLAMA_ATTRIBUTE_FORMAT(2, 3)
24
+ void llama_log_internal (lm_ggml_log_level level, const char * format, ...);
25
+ void llama_log_callback_default(lm_ggml_log_level level, const char * text, void * user_data);
26
+
27
+ #define LLAMA_LOG(...) llama_log_internal(LM_GGML_LOG_LEVEL_NONE , __VA_ARGS__)
28
+ #define LLAMA_LOG_INFO(...) llama_log_internal(LM_GGML_LOG_LEVEL_INFO , __VA_ARGS__)
29
+ #define LLAMA_LOG_WARN(...) llama_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
30
+ #define LLAMA_LOG_ERROR(...) llama_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
31
+ #define LLAMA_LOG_DEBUG(...) llama_log_internal(LM_GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
32
+ #define LLAMA_LOG_CONT(...) llama_log_internal(LM_GGML_LOG_LEVEL_CONT , __VA_ARGS__)
33
+
34
+ //
35
+ // helpers
36
+ //
37
+
38
+ struct time_meas {
39
+ time_meas(int64_t & t_acc, bool disable = false) : t_start_us(disable ? -1 : lm_ggml_time_us()), t_acc(t_acc) {}
40
+
41
+ ~time_meas() {
42
+ if (t_start_us >= 0) {
43
+ t_acc += lm_ggml_time_us() - t_start_us;
44
+ }
45
+ }
46
+
47
+ const int64_t t_start_us;
48
+
49
+ int64_t & t_acc;
50
+ };
51
+
52
+ static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
53
+ if (search.empty()) {
54
+ return;
55
+ }
56
+ std::string builder;
57
+ builder.reserve(s.length());
58
+ size_t pos = 0;
59
+ size_t last_pos = 0;
60
+ while ((pos = s.find(search, last_pos)) != std::string::npos) {
61
+ builder.append(s, last_pos, pos - last_pos);
62
+ builder.append(replace);
63
+ last_pos = pos + search.length();
64
+ }
65
+ builder.append(s, last_pos, std::string::npos);
66
+ s = std::move(builder);
67
+ }
68
+
69
+ const std::vector<std::pair<std::string, struct lm_ggml_tensor *>> & llama_internal_get_tensor_map(
70
+ struct llama_context * ctx
71
+ );
72
+
73
+ // the ring buffer works similarly to std::deque, but with a fixed capacity
74
+ template<typename T>
75
+ struct ring_buffer {
76
+ ring_buffer(size_t cap) : capacity(cap), data(cap) {}
77
+
78
+ T & front() {
79
+ if (sz == 0) {
80
+ throw std::runtime_error("ring buffer is empty");
81
+ }
82
+ return data[first];
83
+ }
84
+
85
+ const T & front() const {
86
+ if (sz == 0) {
87
+ throw std::runtime_error("ring buffer is empty");
88
+ }
89
+ return data[first];
90
+ }
91
+
92
+ T & back() {
93
+ if (sz == 0) {
94
+ throw std::runtime_error("ring buffer is empty");
95
+ }
96
+ return data[pos];
97
+ }
98
+
99
+ const T & back() const {
100
+ if (sz == 0) {
101
+ throw std::runtime_error("ring buffer is empty");
102
+ }
103
+ return data[pos];
104
+ }
105
+
106
+ void push_back(const T & value) {
107
+ if (capacity == 0) {
108
+ throw std::runtime_error("ring buffer: capacity is zero");
109
+ }
110
+
111
+ if (sz == capacity) {
112
+ // advance the start when buffer is full
113
+ first = (first + 1) % capacity;
114
+ } else {
115
+ sz++;
116
+ }
117
+ data[pos] = value;
118
+ pos = (pos + 1) % capacity;
119
+ }
120
+
121
+ T pop_front() {
122
+ if (sz == 0) {
123
+ throw std::runtime_error("ring buffer is empty");
124
+ }
125
+ T value = data[first];
126
+ first = (first + 1) % capacity;
127
+ sz--;
128
+ return value;
129
+ }
130
+
131
+ //T & operator[](size_t i) {
132
+ // if (i >= sz) {
133
+ // throw std::runtime_error("ring buffer: index out of bounds");
134
+ // }
135
+ // return data[(first + i) % capacity];
136
+ //}
137
+
138
+ //const T & at(size_t i) const {
139
+ // if (i >= sz) {
140
+ // throw std::runtime_error("ring buffer: index out of bounds");
141
+ // }
142
+ // return data[(first + i) % capacity];
143
+ //}
144
+
145
+ const T & rat(size_t i) const {
146
+ if (i >= sz) {
147
+ throw std::runtime_error("ring buffer: index out of bounds");
148
+ }
149
+ return data[(first + sz - i - 1) % capacity];
150
+ }
151
+
152
+ std::vector<T> to_vector() const {
153
+ std::vector<T> result;
154
+ result.reserve(sz);
155
+ for (size_t i = 0; i < sz; i++) {
156
+ result.push_back(data[(first + i) % capacity]);
157
+ }
158
+ return result;
159
+ }
160
+
161
+ void clear() {
162
+ // here only reset the status of the buffer
163
+ sz = 0;
164
+ first = 0;
165
+ pos = 0;
166
+ }
167
+
168
+ bool empty() const {
169
+ return sz == 0;
170
+ }
171
+
172
+ size_t size() const {
173
+ return sz;
174
+ }
175
+
176
+ size_t capacity = 0;
177
+ size_t sz = 0;
178
+ size_t first = 0;
179
+ size_t pos = 0;
180
+ std::vector<T> data;
181
+ };