| 8654 | } |
| 8655 | |
| 8656 | void llama_grammar_accept_token(struct llama_context * ctx, struct llama_grammar * grammar, llama_token token) { |
| 8657 | const int64_t t_start_sample_us = ggml_time_us(); |
| 8658 | |
| 8659 | if (token == llama_token_eos(&ctx->model)) { |
| 8660 | for (const auto & stack : grammar->stacks) { |
| 8661 | if (stack.empty()) { |
| 8662 | return; |
| 8663 | } |
| 8664 | } |
| 8665 | GGML_ASSERT(false); |
| 8666 | } |
| 8667 | |
| 8668 | const std::string piece = llama_token_to_piece(ctx, token); |
| 8669 | |
| 8670 | // Note terminating 0 in decoded string |
| 8671 | const auto decoded = decode_utf8(piece.c_str(), grammar->partial_utf8); |
| 8672 | const auto & code_points = decoded.first; |
| 8673 | for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) { |
| 8674 | grammar->stacks = llama_grammar_accept(grammar->rules, grammar->stacks, *it); |
| 8675 | } |
| 8676 | grammar->partial_utf8 = decoded.second; |
| 8677 | GGML_ASSERT(!grammar->stacks.empty()); |
| 8678 | |
| 8679 | ctx->t_sample_us += ggml_time_us() - t_start_sample_us; |
| 8680 | } |
| 8681 | |
| 8682 | // |
| 8683 | // Beam search |
no test coverage detected