@@ -1000,6 +1000,18 @@ extern "C" {
1000
1000
1001
1001
LLAMA_API struct llama_grammar * llama_grammar_copy (const struct llama_grammar * grammar);
1002
1002
1003
+ // / @details Apply constraints from grammar
1004
+ LLAMA_API void llama_grammar_sample (
1005
+ struct llama_context * ctx,
1006
+ llama_token_data_array * candidates,
1007
+ const struct llama_grammar * grammar);
1008
+
1009
+ // / @details Accepts the sampled token into the grammar
1010
+ LLAMA_API void llama_grammar_accept_token (
1011
+ struct llama_context * ctx,
1012
+ struct llama_grammar * grammar,
1013
+ llama_token token);
1014
+
1003
1015
//
1004
1016
// Sampling functions
1005
1017
//
@@ -1118,18 +1130,6 @@ extern "C" {
1118
1130
struct llama_context * ctx,
1119
1131
llama_token_data_array * candidates);
1120
1132
1121
- // / @details Apply constraints from grammar
1122
- LLAMA_API void llama_sample_grammar (
1123
- struct llama_context * ctx,
1124
- llama_token_data_array * candidates,
1125
- const struct llama_grammar * grammar);
1126
-
1127
- // / @details Accepts the sampled token into the grammar
1128
- LLAMA_API void llama_grammar_accept_token (
1129
- struct llama_context * ctx,
1130
- struct llama_grammar * grammar,
1131
- llama_token token);
1132
-
1133
1133
//
1134
1134
// Model split
1135
1135
//
@@ -1172,38 +1172,41 @@ extern "C" {
1172
1172
1173
1173
struct ggml_tensor ;
1174
1174
1175
+ const std::vector<std::pair<std::string, struct ggml_tensor *>> & llama_internal_get_tensor_map (
1176
+ struct llama_context * ctx
1177
+ );
1178
+
1175
1179
struct llama_partial_utf8 {
1176
1180
uint32_t value; // bit value so far (unshifted)
1177
1181
int n_remain; // num bytes remaining; -1 indicates invalid sequence
1178
1182
};
1179
1183
1180
- struct llama_grammar {
1181
- const std::vector<std::vector<llama_grammar_element>> rules;
1182
- std::vector<std::vector<const llama_grammar_element *>> stacks;
1183
-
1184
- // buffer for partially generated UTF-8 sequence from accepted tokens
1185
- llama_partial_utf8 partial_utf8;
1186
- };
1187
-
1188
1184
struct llama_grammar_candidate {
1189
1185
size_t index;
1190
1186
const uint32_t * code_points;
1191
1187
llama_partial_utf8 partial_utf8;
1192
1188
};
1193
1189
1194
- const std::vector<std::pair<std::string, struct ggml_tensor *>> & llama_internal_get_tensor_map (
1195
- struct llama_context * ctx
1196
- );
1190
+ using llama_grammar_rules = std::vector<std::vector<llama_grammar_element>>;
1191
+ using llama_grammar_stacks = std::vector<std::vector<const llama_grammar_element *>>;
1192
+
1193
+ const llama_grammar_rules & llama_grammar_get_rules (const struct llama_grammar * grammar);
1194
+ llama_grammar_stacks & llama_grammar_get_stacks ( struct llama_grammar * grammar);
1197
1195
1198
1196
void llama_grammar_accept (
1199
- const std::vector<std::vector<llama_grammar_element>> & rules,
1200
- const std::vector<std::vector<const llama_grammar_element *>> & stacks,
1201
- const uint32_t chr,
1202
- std::vector<std::vector<const llama_grammar_element *>> & new_stacks);
1197
+ const llama_grammar_rules & rules,
1198
+ const llama_grammar_stacks & stacks,
1199
+ const uint32_t chr,
1200
+ llama_grammar_stacks & new_stacks);
1201
+
1202
+ std::vector<llama_grammar_candidate> llama_grammar_reject_candidates_for_stack (
1203
+ const std::vector<std::vector<llama_grammar_element>> & rules,
1204
+ const std::vector<const llama_grammar_element *> & stack,
1205
+ const std::vector<llama_grammar_candidate> & candidates);
1203
1206
1204
1207
std::pair<std::vector<uint32_t >, llama_partial_utf8> decode_utf8 (
1205
1208
const std::string & src,
1206
- llama_partial_utf8 partial_start);
1209
+ llama_partial_utf8 partial_start);
1207
1210
1208
1211
// Randomly selects a token from the candidates based on their probabilities using given std::mt19937.
1209
1212
// This is a temporary workaround in order to fix race conditions when sampling with multiple sequences.
0 commit comments