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

Method loop

llama.cpp:8870–8893  ·  view source on GitHub ↗

Loop: while i < n_predict, AND any of the beams have not yet reached end-of-beam (eob), AND the highest probability beam(s) (plural in case of ties) are not at end-of-sentence (since all other beam probabilities can only decrease)

Source from the content-addressed store, hash-verified

8868 // * the highest probability beam(s) (plural in case of ties) are not at end-of-sentence
8869 // (since all other beam probabilities can only decrease)
8870 void loop(const llama_beam_search_callback_fn_t callback, void * const callback_data) {
8871 beams.push_back({{}, 1.0f, false}); // Start with one empty beam w/ probability = 1.0 and !eob.
8872 const auto not_eob = [](const llama_beam & beam) { return !beam.eob; };
8873 for (int i = 0 ; i < n_predict && std::any_of(beams.begin(),beams.end(),not_eob) &&
8874 !beams[top_beam_index()].eob ; ++i) {
8875 callback(callback_data, get_beams_state(false)); // Sets common_prefix_length
8876 update_beams_from_beam_views(); // Update values (p,eob) that callback may have changed.
8877 if (common_prefix_length) {
8878 llama_decode(ctx, llama_batch_get_one(beams[0].tokens.data(), common_prefix_length, n_past, 0));
8879 n_past += common_prefix_length;
8880 }
8881 // Zero-out next_beam probabilities to place them last in following min-heap.
8882 std::for_each(next_beams.begin(), next_beams.end(), [](llama_beam & beam) { beam.p = 0.0f; });
8883 for (llama_beam & beam : beams) {
8884 beam.shift_tokens(common_prefix_length);
8885 fill_next_beams_by_top_probabilities(beam);
8886 }
8887 // next_beams become the beams of next/final iteration. Swap them to re-use memory.
8888 beams.swap(next_beams);
8889 renormalize_beam_probabilities(beams);
8890 }
8891 collapse_beams(top_beam_index());
8892 callback(callback_data, get_beams_state(true));
8893 }
8894
8895 // As beams grow, the cumulative probabilities decrease.
8896 // Renormalize them to avoid floating point underflow.

Callers 1

llama_beam_searchFunction · 0.80

Calls 9

for_eachFunction · 0.85
shift_tokensMethod · 0.80
llama_decodeFunction · 0.70
llama_batch_get_oneFunction · 0.70
push_backMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
dataMethod · 0.45
swapMethod · 0.45

Tested by

no test coverage detected