MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / llama_grammar_accept

Function llama_grammar_accept

llama.cpp:7959–7985  ·  view source on GitHub ↗

takes a set of possible pushdown stacks on a grammar, which are required to be positioned at a character range (see `llama_grammar_advance_stack`), and produces the N possible stacks if the given char is accepted at those positions

Source from the content-addressed store, hash-verified

7957// produces the N possible stacks if the given char is accepted at those
7958// positions
7959static std::vector<std::vector<const llama_grammar_element *>> llama_grammar_accept(
7960 const std::vector<std::vector<llama_grammar_element>> & rules,
7961 const std::vector<std::vector<const llama_grammar_element *>> & stacks,
7962 const uint32_t chr) {
7963
7964 std::vector<std::vector<const llama_grammar_element *>> new_stacks;
7965
7966 for (const auto & stack : stacks) {
7967 if (stack.empty()) {
7968 continue;
7969 }
7970
7971 auto match = llama_grammar_match_char(stack.back(), chr);
7972 if (match.first) {
7973 const llama_grammar_element * pos = match.second;
7974
7975 // update top of stack to next element, if any
7976 std::vector<const llama_grammar_element *> new_stack(stack.begin(), stack.end() - 1);
7977 if (!llama_grammar_is_end_of_sequence(pos)) {
7978 new_stack.push_back(pos);
7979 }
7980 llama_grammar_advance_stack(rules, new_stack, new_stacks);
7981 }
7982 }
7983
7984 return new_stacks;
7985}
7986
7987static std::vector<llama_grammar_candidate> llama_grammar_reject_candidates(
7988 const std::vector<std::vector<llama_grammar_element>> & rules,

Callers 1

Calls 7

llama_grammar_match_charFunction · 0.70
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected