|
| 1 | +#ifndef _GNU_SOURCE |
| 2 | +#define _GNU_SOURCE |
| 3 | +#endif |
| 4 | + |
| 5 | +#include "common.h" |
| 6 | +#include "llama.h" |
| 7 | +#include "build-info.h" |
| 8 | + |
| 9 | +#include <cassert> |
| 10 | +#include <cinttypes> |
| 11 | +#include <cmath> |
| 12 | +#include <cstdio> |
| 13 | +#include <cstring> |
| 14 | +#include <ctime> |
| 15 | +#include <fstream> |
| 16 | +#include <iostream> |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) |
| 21 | +#include <signal.h> |
| 22 | +#include <unistd.h> |
| 23 | +#elif defined (_WIN32) |
| 24 | +#define WIN32_LEAN_AND_MEAN |
| 25 | +#define NOMINMAX |
| 26 | +#include <windows.h> |
| 27 | +#include <signal.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +// Used for debugging to print out beam tokens. |
| 31 | +struct ostream_beam_view { |
| 32 | + llama_context * ctx; |
| 33 | + llama_beam_view beam_view; |
| 34 | +}; |
| 35 | +std::ostream& operator<<(std::ostream& os, const ostream_beam_view & obv) { |
| 36 | + os << "p(" << obv.beam_view.p << ") eob(" << std::boolalpha << obv.beam_view.eob << ") tokens("; |
| 37 | + for (size_t i = 0 ; i < obv.beam_view.n_tokens ; ++i) { |
| 38 | + os << llama_token_to_str(obv.ctx, obv.beam_view.tokens[i]); |
| 39 | + } |
| 40 | + return os << ')'; |
| 41 | +} |
| 42 | + |
| 43 | +// Put here anything you want back in beam_search_callback(). |
| 44 | +struct beam_search_callback_data { |
| 45 | + llama_context * ctx; |
| 46 | + std::vector<llama_token> response; |
| 47 | +}; |
| 48 | + |
| 49 | +// In this case, end-of-beam (eob) is equivalent to end-of-sentence (eos) but this need not always be the same. |
| 50 | +// For example, eob can be flagged due to maximum token length, stop words, etc. |
| 51 | +bool is_at_eob(const beam_search_callback_data & callback_data, const llama_token * tokens, const size_t n_tokens) { |
| 52 | + return n_tokens && tokens[n_tokens-1] == llama_token_eos(callback_data.ctx); |
| 53 | +} |
| 54 | + |
| 55 | +// Function matching type llama_beam_search_callback_fn_t. |
| 56 | +// Custom callback example is called each time the beams lengths increase: |
| 57 | +// * Show progress by printing ',' following by number of convergent beam tokens if any. |
| 58 | +// * When all beams converge to a common prefix, they are made available in beams_state.beams[0]. |
| 59 | +// This is also called when the stop condition is met. |
| 60 | +// Collect tokens into std::vector<llama_token> response which is pointed to by callback_data. |
| 61 | +void beam_search_callback(void * callback_data_ptr, llama_beams_state beams_state) { |
| 62 | + auto& callback_data = *static_cast<beam_search_callback_data*>(callback_data_ptr); |
| 63 | + // Mark beams as EOS as needed. |
| 64 | + for (size_t i = 0 ; i < beams_state.n_beams ; ++i) { |
| 65 | + llama_beam_view& beam_view = beams_state.beam_views[i]; |
| 66 | + if (!beam_view.eob && is_at_eob(callback_data, beam_view.tokens, beam_view.n_tokens)) { |
| 67 | + beam_view.eob = true; |
| 68 | + } |
| 69 | + } |
| 70 | + printf(","); // Show progress |
| 71 | + if (const size_t n = beams_state.common_prefix_length) { |
| 72 | + callback_data.response.resize(callback_data.response.size() + n); |
| 73 | + assert(0u < beams_state.n_beams); |
| 74 | + const llama_token * tokens = beams_state.beam_views[0].tokens; |
| 75 | + std::copy(tokens, tokens + n, callback_data.response.end() - n); |
| 76 | + printf("%lu", n); |
| 77 | + } |
| 78 | + fflush(stdout); |
| 79 | +#if 1 // DEBUG: print current beams for this iteration |
| 80 | + std::cout << "\n\nCurrent beams (last_call=" << beams_state.last_call << "):\n"; |
| 81 | + for (size_t i = 0 ; i < beams_state.n_beams ; ++i) { |
| 82 | + std::cout << "beams["<<i<<"]: " << ostream_beam_view{callback_data.ctx,beams_state.beam_views[i]} << std::endl; |
| 83 | + } |
| 84 | +#endif |
| 85 | +} |
| 86 | + |
| 87 | +int main(int argc, char ** argv) |
| 88 | +{ |
| 89 | + gpt_params params; |
| 90 | + //params.n_gpu_layers = 200; |
| 91 | + |
| 92 | + //--------------------------------- |
| 93 | + // Print help : |
| 94 | + //--------------------------------- |
| 95 | + |
| 96 | + if ( argc < 2 || argv[1][0] == '-' ) |
| 97 | + { |
| 98 | + printf( "Usage: %s MODEL_PATH [BEAM_WIDTH=2] [PROMPT]\n" , argv[0] ); |
| 99 | + return 1 ; |
| 100 | + } |
| 101 | + |
| 102 | + //--------------------------------- |
| 103 | + // Load parameters : |
| 104 | + //--------------------------------- |
| 105 | + |
| 106 | + params.model = argv[1]; |
| 107 | + |
| 108 | + params.n_beams = 2 < argc ? std::stoi(argv[2]) : 2; |
| 109 | + |
| 110 | + if ( argc > 3 ) |
| 111 | + { |
| 112 | + params.prompt = argv[3]; |
| 113 | + } |
| 114 | + |
| 115 | + if ( params.prompt.empty() ) |
| 116 | + { |
| 117 | + params.prompt = "### Request:\nHow many countries are there?\n\n### Response:\n"; |
| 118 | + } |
| 119 | + |
| 120 | + //--------------------------------- |
| 121 | + // Init LLM : |
| 122 | + //--------------------------------- |
| 123 | + |
| 124 | + llama_backend_init(params.numa); |
| 125 | + |
| 126 | + llama_model * model; |
| 127 | + llama_context * ctx; |
| 128 | + |
| 129 | + std::tie(model, ctx) = llama_init_from_gpt_params( params ); |
| 130 | + |
| 131 | + if ( model == NULL ) |
| 132 | + { |
| 133 | + fprintf( stderr , "%s: error: unable to load model\n" , __func__ ); |
| 134 | + return 1; |
| 135 | + } |
| 136 | + |
| 137 | + //--------------------------------- |
| 138 | + // Tokenize the prompt : |
| 139 | + //--------------------------------- |
| 140 | + |
| 141 | + std::vector<llama_token> tokens_list = llama_tokenize(ctx, params.prompt, true); |
| 142 | + |
| 143 | + const size_t max_context_size = llama_n_ctx( ctx ); |
| 144 | + const size_t max_tokens_list_size = max_context_size - 4 ; |
| 145 | + |
| 146 | + if (tokens_list.size() > max_tokens_list_size) |
| 147 | + { |
| 148 | + fprintf( stderr , "%s: error: prompt too long (%lu tokens, max %lu)\n" , |
| 149 | + __func__ , tokens_list.size() , max_tokens_list_size ); |
| 150 | + return 1; |
| 151 | + } |
| 152 | + |
| 153 | + fprintf( stderr, "\n\n" ); |
| 154 | + |
| 155 | + // Print the tokens from the prompt : |
| 156 | + |
| 157 | + for( auto id : tokens_list ) |
| 158 | + { |
| 159 | + std::cout << llama_token_to_str(ctx, id); |
| 160 | + } |
| 161 | + std::cout << std::flush; |
| 162 | + |
| 163 | + int n_past = llama_get_kv_cache_token_count(ctx); |
| 164 | + if (llama_eval(ctx, tokens_list.data(), tokens_list.size(), n_past, params.n_threads)) |
| 165 | + { |
| 166 | + fprintf(stderr, "%s : failed to eval prompt.\n" , __func__ ); |
| 167 | + return 1; |
| 168 | + } |
| 169 | + n_past += tokens_list.size(); |
| 170 | + |
| 171 | + beam_search_callback_data callback_data{ctx, {}}; |
| 172 | + size_t const beam_width = static_cast<size_t>(params.n_beams); |
| 173 | + int const n_predict = 256; |
| 174 | + llama_beam_search(ctx, beam_search_callback, &callback_data, beam_width, n_past, n_predict, params.n_threads); |
| 175 | + |
| 176 | + std::cout << "\n\n"; |
| 177 | + for (llama_token const token_id : callback_data.response) { |
| 178 | + std::cout << llama_token_to_str(ctx,token_id); |
| 179 | + } |
| 180 | + std::cout << std::endl; |
| 181 | + |
| 182 | + llama_free( ctx ); |
| 183 | + llama_free_model( model ); |
| 184 | + |
| 185 | + llama_backend_free(); |
| 186 | + |
| 187 | + return 0; |
| 188 | +} |
0 commit comments