perplexity.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. #include "common.h"
  2. #include "llama.h"
  3. #include "build-info.h"
  4. #include <cmath>
  5. #include <ctime>
  6. #include <sstream>
  7. #include <cstring>
  8. #if defined(_MSC_VER)
  9. #pragma warning(disable: 4244 4267) // possible loss of data
  10. #endif
  11. std::vector<float> softmax(const std::vector<float>& logits) {
  12. std::vector<float> probs(logits.size());
  13. float max_logit = logits[0];
  14. for (float v : logits) max_logit = std::max(max_logit, v);
  15. double sum_exp = 0.0;
  16. for (size_t i = 0; i < logits.size(); i++) {
  17. // Subtract the maximum logit value from the current logit value for numerical stability
  18. const float logit = logits[i] - max_logit;
  19. const float exp_logit = expf(logit);
  20. sum_exp += exp_logit;
  21. probs[i] = exp_logit;
  22. }
  23. for (size_t i = 0; i < probs.size(); i++) probs[i] /= sum_exp;
  24. return probs;
  25. }
  26. void perplexity_v2(llama_context * ctx, const gpt_params & params) {
  27. // Download: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research
  28. // Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
  29. // Output: `perplexity: 13.5106 [114/114]`
  30. // BOS tokens will be added for each chunk before eval
  31. if (params.ppl_stride <= 0) {
  32. fprintf(stderr, "%s: stride is %d but must be greater than zero!\n",__func__,params.ppl_stride);
  33. return;
  34. }
  35. const bool is_spm = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM;
  36. const bool add_bos = is_spm;
  37. fprintf(stderr, "%s: tokenizing the input ..\n", __func__);
  38. auto tokens = ::llama_tokenize(ctx, params.prompt, add_bos);
  39. const int calc_chunk = params.n_ctx;
  40. fprintf(stderr, "%s: have %zu tokens. Calculation chunk = %d\n", __func__, tokens.size(), calc_chunk);
  41. if (int(tokens.size()) <= calc_chunk) {
  42. fprintf(stderr, "%s: there are only %zu tokens, this is not enough for a context size of %d and stride %d\n",__func__,
  43. tokens.size(), params.n_ctx, params.ppl_stride);
  44. return;
  45. }
  46. const int n_chunk_max = (tokens.size() - calc_chunk + params.ppl_stride - 1) / params.ppl_stride;
  47. const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
  48. const int n_vocab = llama_n_vocab(ctx);
  49. const int n_batch = params.n_batch;
  50. int count = 0;
  51. double nll = 0.0;
  52. fprintf(stderr, "%s: calculating perplexity over %d chunks, batch_size=%d\n", __func__, n_chunk, n_batch);
  53. for (int i = 0; i < n_chunk; ++i) {
  54. const int start = i * params.ppl_stride;
  55. const int end = start + calc_chunk;
  56. const int num_batches = (calc_chunk + n_batch - 1) / n_batch;
  57. //fprintf(stderr, "%s: evaluating %d...%d using %d batches\n", __func__, start, end, num_batches);
  58. std::vector<float> logits;
  59. const auto t_start = std::chrono::high_resolution_clock::now();
  60. for (int j = 0; j < num_batches; ++j) {
  61. const int batch_start = start + j * n_batch;
  62. const int batch_size = std::min(end - batch_start, n_batch);
  63. //fprintf(stderr, " Batch %d: starts at %d, size is %d, n_past is %d\n",j,batch_start,batch_size,j * n_batch);
  64. if (llama_eval(ctx, tokens.data() + batch_start, batch_size, j * n_batch, params.n_threads)) {
  65. //fprintf(stderr, "%s : failed to eval\n", __func__);
  66. return;
  67. }
  68. // save original token and restore it after eval
  69. const auto token_org = tokens[batch_start];
  70. // add BOS token for the first batch of each chunk
  71. if (add_bos && j == 0) {
  72. tokens[batch_start] = llama_token_bos(ctx);
  73. }
  74. const auto batch_logits = llama_get_logits(ctx);
  75. logits.insert(logits.end(), batch_logits, batch_logits + batch_size * n_vocab);
  76. if (j == 0) {
  77. tokens[batch_start] = token_org;
  78. }
  79. }
  80. const auto t_end = std::chrono::high_resolution_clock::now();
  81. if (i == 0) {
  82. const float t_total = std::chrono::duration<float>(t_end - t_start).count();
  83. fprintf(stderr, "%s: %.2f seconds per pass - ETA ", __func__, t_total);
  84. int total_seconds = (int)(t_total * n_chunk);
  85. if (total_seconds >= 60*60) {
  86. fprintf(stderr, "%d hours ", total_seconds / (60*60));
  87. total_seconds = total_seconds % (60*60);
  88. }
  89. fprintf(stderr, "%.2f minutes\n", total_seconds / 60.0);
  90. }
  91. //fprintf(stderr, "%s: using tokens %d...%d\n",__func__,params.n_ctx - params.ppl_stride + start, params.n_ctx + start);
  92. for (int j = params.n_ctx - params.ppl_stride - 1; j < params.n_ctx - 1; ++j) {
  93. // Calculate probability of next token, given the previous ones.
  94. const std::vector<float> tok_logits(
  95. logits.begin() + (j + 0) * n_vocab,
  96. logits.begin() + (j + 1) * n_vocab);
  97. const float prob = softmax(tok_logits)[tokens[start + j + 1]];
  98. nll += -std::log(prob);
  99. ++count;
  100. }
  101. // perplexity is e^(average negative log-likelihood)
  102. if (params.ppl_output_type == 0) {
  103. printf("[%d]%.4lf,", i + 1, std::exp(nll / count));
  104. } else {
  105. printf("%8d %.4lf\n", i*params.ppl_stride, std::exp(nll / count));
  106. }
  107. fflush(stdout);
  108. }
  109. printf("\n");
  110. }
  111. void perplexity(llama_context * ctx, const gpt_params & params) {
  112. if (params.ppl_stride > 0) {
  113. perplexity_v2(ctx, params);
  114. return;
  115. }
  116. // Download: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research
  117. // Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
  118. // Output: `perplexity: 13.5106 [114/114]`
  119. // BOS tokens will be added for each chunk before eval
  120. const bool is_spm = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM;
  121. const bool add_bos = is_spm;
  122. fprintf(stderr, "%s: tokenizing the input ..\n", __func__);
  123. auto tokens = ::llama_tokenize(ctx, params.prompt, add_bos);
  124. const int n_chunk_max = tokens.size() / params.n_ctx;
  125. const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
  126. const int n_vocab = llama_n_vocab(ctx);
  127. const int n_batch = params.n_batch;
  128. int count = 0;
  129. double nll = 0.0;
  130. fprintf(stderr, "%s: calculating perplexity over %d chunks, batch_size=%d\n", __func__, n_chunk, n_batch);
  131. for (int i = 0; i < n_chunk; ++i) {
  132. const int start = i * params.n_ctx;
  133. const int end = start + params.n_ctx;
  134. const int num_batches = (params.n_ctx + n_batch - 1) / n_batch;
  135. std::vector<float> logits;
  136. const auto t_start = std::chrono::high_resolution_clock::now();
  137. for (int j = 0; j < num_batches; ++j) {
  138. const int batch_start = start + j * n_batch;
  139. const int batch_size = std::min(end - batch_start, n_batch);
  140. // save original token and restore it after eval
  141. const auto token_org = tokens[batch_start];
  142. // add BOS token for the first batch of each chunk
  143. if (add_bos && j == 0) {
  144. tokens[batch_start] = llama_token_bos(ctx);
  145. }
  146. if (llama_eval(ctx, tokens.data() + batch_start, batch_size, j * n_batch, params.n_threads)) {
  147. fprintf(stderr, "%s : failed to eval\n", __func__);
  148. return;
  149. }
  150. // restore the original token in case it was set to BOS
  151. tokens[batch_start] = token_org;
  152. const auto batch_logits = llama_get_logits(ctx);
  153. logits.insert(logits.end(), batch_logits, batch_logits + batch_size * n_vocab);
  154. }
  155. const auto t_end = std::chrono::high_resolution_clock::now();
  156. if (i == 0) {
  157. const float t_total = std::chrono::duration<float>(t_end - t_start).count();
  158. fprintf(stderr, "%s: %.2f seconds per pass - ETA ", __func__, t_total);
  159. int total_seconds = (int)(t_total * n_chunk);
  160. if (total_seconds >= 60*60) {
  161. fprintf(stderr, "%d hours ", total_seconds / (60*60));
  162. total_seconds = total_seconds % (60*60);
  163. }
  164. fprintf(stderr, "%.2f minutes\n", total_seconds / 60.0);
  165. }
  166. // We get the logits for all the tokens in the context window (params.n_ctx)
  167. // from llama_eval above. Now, based on https://huggingface.co/docs/transformers/perplexity,
  168. // calculate the perplexity over the last half of the window (so the model always has
  169. // some context to predict the token).
  170. //
  171. // We rely on the fact that attention in the forward pass only looks at previous
  172. // tokens here, so the logits returned for each token are an accurate representation
  173. // of what the model would have predicted at that point.
  174. //
  175. // Example, we have a context window of 512, we will compute perplexity for each of the
  176. // last 256 tokens. Then, we split the input up into context window size chunks to
  177. // process the entire prompt.
  178. for (int j = std::min(512, params.n_ctx / 2); j < params.n_ctx - 1; ++j) {
  179. // Calculate probability of next token, given the previous ones.
  180. const std::vector<float> tok_logits(
  181. logits.begin() + (j + 0) * n_vocab,
  182. logits.begin() + (j + 1) * n_vocab);
  183. const float prob = softmax(tok_logits)[tokens[start + j + 1]];
  184. nll += -std::log(prob);
  185. ++count;
  186. }
  187. // perplexity is e^(average negative log-likelihood)
  188. if (params.ppl_output_type == 0) {
  189. printf("[%d]%.4lf,", i + 1, std::exp(nll / count));
  190. } else {
  191. printf("%8d %.4lf\n", i*params.n_ctx, std::exp(nll / count));
  192. }
  193. fflush(stdout);
  194. }
  195. printf("\n");
  196. }
  197. std::vector<float> hellaswag_evaluate_tokens(llama_context * ctx, const std::vector<int>& tokens, int n_past, int n_batch,
  198. int n_vocab, int n_thread) {
  199. std::vector<float> result;
  200. result.reserve(tokens.size() * n_vocab);
  201. size_t n_chunk = (tokens.size() + n_batch - 1)/n_batch;
  202. for (size_t i_chunk = 0; i_chunk < n_chunk; ++i_chunk) {
  203. size_t n_tokens = tokens.size() - i_chunk * n_batch;
  204. n_tokens = std::min(n_tokens, size_t(n_batch));
  205. if (llama_eval(ctx, tokens.data() + i_chunk * n_batch, n_tokens, n_past, n_thread)) {
  206. fprintf(stderr, "%s : failed to eval\n", __func__);
  207. return {};
  208. }
  209. const auto logits = llama_get_logits(ctx);
  210. result.insert(result.end(), logits, logits + n_tokens * n_vocab);
  211. n_past += n_tokens;
  212. }
  213. return result;
  214. }
  215. void hellaswag_score(llama_context * ctx, const gpt_params & params) {
  216. // Calculates hellaswag score (acc_norm) from prompt
  217. //
  218. // Data extracted from the HellaSwag validation dataset (MIT license) https://github.com/rowanz/hellaswag/blob/master/data/hellaswag_val.jsonl
  219. // All used data fields are preprocessed as in https://github.com/EleutherAI/lm-evaluation-harness/blob/df3da98c5405deafd519c2ddca52bb7c3fe36bef/lm_eval/tasks/hellaswag.py#L62-L68
  220. //
  221. // All 10042 tasks should be extracted to keep the results standardized like other implementations.
  222. //
  223. // Datafile layout:
  224. // ['??'] denotes json fields
  225. // 6 lines per task:
  226. // ['activity_label'] + ": " +['ctx'] - The first part of the query, the context
  227. // ['label'] - The index the best common sense ending aka gold ending
  228. // ['endings'][0] - Endings added to the first part of the query
  229. // ['endings'][1]
  230. // ['endings'][2]
  231. // ['endings'][3]
  232. std::vector<std::string> prompt_lines;
  233. std::istringstream strstream(params.prompt);
  234. std::string line;
  235. while (std::getline(strstream,line,'\n')) {
  236. prompt_lines.push_back(line);
  237. }
  238. if( prompt_lines.size() % 6 != 0) {
  239. fprintf(stderr, "%s : number of lines in prompt not a multiple of 6.\n", __func__);
  240. return;
  241. }
  242. size_t hs_task_count = prompt_lines.size()/6;
  243. fprintf(stderr, "%s : loaded %zu tasks from prompt.\n", __func__, hs_task_count);
  244. const bool is_spm = llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM;
  245. // This is needed as usual for LLaMA models
  246. const bool add_bos = is_spm;
  247. // Number of tasks to use when computing the score
  248. if ( params.hellaswag_tasks < hs_task_count ) {
  249. hs_task_count = params.hellaswag_tasks;
  250. }
  251. // The tasks should be randomized so the score stabilizes quickly.
  252. bool randomize_tasks = true;
  253. // The random seed should not impact the final result if the computation is done over enough tasks, so kept hardcoded for now
  254. std::mt19937 rng(1);
  255. // Dataholder for hellaswag tasks
  256. struct hs_data_t {
  257. std::string context;
  258. size_t gold_ending_idx;
  259. std::string ending[4];
  260. size_t ending_logprob_count[4];
  261. double ending_logprob[4];
  262. };
  263. fprintf(stderr, "%s : selecting %zu %s tasks.\n", __func__, hs_task_count, (randomize_tasks?"randomized":"the first") );
  264. // Select and read data from prompt lines
  265. hs_data_t *hs_data = new hs_data_t[hs_task_count];
  266. for (size_t i=0; i < hs_task_count; i++) {
  267. size_t idx = i;
  268. // Select a random example of those left in the prompt
  269. if (randomize_tasks) {
  270. std::uniform_int_distribution<size_t> dist(0, prompt_lines.size()/6-1 ) ;
  271. idx = dist(rng);
  272. }
  273. hs_data[i].context = prompt_lines[idx*6];
  274. hs_data[i].gold_ending_idx = std::stoi( prompt_lines[idx*6+1] );
  275. for (size_t j=0; j < 4; j++) {
  276. hs_data[i].ending[j] = " " + prompt_lines[idx*6+2+j];
  277. }
  278. // Delete the selected random example from the prompt
  279. if (randomize_tasks) {
  280. prompt_lines.erase( std::next(prompt_lines.begin(),idx*6) , std::next(prompt_lines.begin(),idx*6+6) );
  281. }
  282. }
  283. fprintf(stderr, "%s : calculating hellaswag score over selected tasks.\n", __func__);
  284. printf("\ntask\tacc_norm\n");
  285. double acc = 0.0f;
  286. const int n_vocab = llama_n_vocab(ctx);
  287. std::vector<float> tok_logits(n_vocab);
  288. for (size_t task_idx = 0; task_idx < hs_task_count; task_idx++) {
  289. // Tokenize the context to count tokens
  290. std::vector<int> context_embd = ::llama_tokenize(ctx, hs_data[task_idx].context, add_bos);
  291. size_t context_size = context_embd.size();
  292. // Do the 1st ending
  293. // In this case we include the context when evaluating
  294. auto query_embd = ::llama_tokenize(ctx, hs_data[task_idx].context + hs_data[task_idx].ending[0], add_bos);
  295. auto query_size = query_embd.size();
  296. //printf("First query: %d\n",(int)query_size);
  297. // Stop if query wont fit the ctx window
  298. if (query_size > (size_t)params.n_ctx) {
  299. fprintf(stderr, "%s : number of tokens in query %zu > n_ctxl\n", __func__, query_size);
  300. return;
  301. }
  302. // Speedup small evaluations by evaluating atleast 32 tokens
  303. if (query_size < 32) {
  304. query_embd.resize(32);
  305. }
  306. auto logits = hellaswag_evaluate_tokens(ctx, query_embd, 0, params.n_batch, n_vocab, params.n_threads);
  307. if (logits.empty()) {
  308. fprintf(stderr, "%s : failed to eval\n", __func__);
  309. return;
  310. }
  311. std::memcpy(tok_logits.data(), logits.data() + (context_size-1)*n_vocab, n_vocab*sizeof(float));
  312. const auto first_probs = softmax(tok_logits);
  313. hs_data[task_idx].ending_logprob_count[0] = 1;
  314. hs_data[task_idx].ending_logprob[0] = std::log(first_probs[query_embd[context_size]]);
  315. // Calculate the logprobs over the ending
  316. for (size_t j = context_size; j < query_size - 1; j++) {
  317. std::memcpy(tok_logits.data(), logits.data() + j*n_vocab, n_vocab*sizeof(float));
  318. const float prob = softmax(tok_logits)[query_embd[j + 1]];
  319. hs_data[task_idx].ending_logprob[0] += std::log(prob);
  320. hs_data[task_idx].ending_logprob_count[0]++;
  321. }
  322. // Calculate the mean token logprob for acc_norm
  323. hs_data[task_idx].ending_logprob[0] /= hs_data[task_idx].ending_logprob_count[0];
  324. // Do the remaining endings
  325. // For these, we use the bare ending with n_past = context_size
  326. //
  327. for (size_t ending_idx = 1; ending_idx < 4; ending_idx++) {
  328. // Tokenize the query
  329. query_embd = ::llama_tokenize(ctx, hs_data[task_idx].ending[ending_idx], false);
  330. query_size = query_embd.size();
  331. // Stop if query wont fit the ctx window
  332. if (context_size + query_size > (size_t)params.n_ctx) {
  333. fprintf(stderr, "%s : number of tokens in query %zu > n_ctxl\n", __func__, query_size);
  334. return;
  335. }
  336. // Speedup small evaluations by evaluating atleast 32 tokens
  337. // No, resizing to 32 is actually slightly slower (at least on CUDA)
  338. //if (query_size < 32) {
  339. // query_embd.resize(32);
  340. //}
  341. // Evaluate the query
  342. logits = hellaswag_evaluate_tokens(ctx, query_embd, context_size, params.n_batch, n_vocab, params.n_threads);
  343. if (logits.empty()) {
  344. fprintf(stderr, "%s : failed to eval\n", __func__);
  345. return;
  346. }
  347. hs_data[task_idx].ending_logprob_count[ending_idx] = 1;
  348. hs_data[task_idx].ending_logprob[ending_idx] = std::log(first_probs[query_embd[0]]);
  349. // Calculate the logprobs over the ending
  350. for (size_t j = 0; j < query_size - 1; j++) {
  351. std::memcpy(tok_logits.data(), logits.data() + j*n_vocab, n_vocab*sizeof(float));
  352. const float prob = softmax(tok_logits)[query_embd[j + 1]];
  353. hs_data[task_idx].ending_logprob[ending_idx] += std::log(prob);
  354. hs_data[task_idx].ending_logprob_count[ending_idx]++;
  355. }
  356. // Calculate the mean token logprob for acc_norm
  357. hs_data[task_idx].ending_logprob[ending_idx] /= hs_data[task_idx].ending_logprob_count[ending_idx];
  358. // printf("task %lu, ending %lu, whole_len %lu, context_len %lu, ending_logprob_count %lu, ending_logprob %.4f\n",
  359. // task_idx,ending_idx,whole_size,context_size, hs_data[task_idx].ending_logprob_count[ending_idx], hs_data[task_idx].ending_logprob[ending_idx] );
  360. }
  361. // Find the ending with maximum logprob
  362. size_t ending_logprob_max_idx = 0;
  363. double ending_logprob_max_val = hs_data[task_idx].ending_logprob[0];
  364. for (size_t j = 1; j < 4; j++) {
  365. if (hs_data[task_idx].ending_logprob[j] > ending_logprob_max_val) {
  366. ending_logprob_max_idx = j;
  367. ending_logprob_max_val = hs_data[task_idx].ending_logprob[j];
  368. }
  369. }
  370. // printf("max logprob ending idx %lu, gold ending idx %lu\n", ending_logprob_max_idx, hs_data[task_idx].gold_ending_idx);
  371. // If the gold ending got the maximum logprobe add one accuracy point
  372. if (ending_logprob_max_idx == hs_data[task_idx].gold_ending_idx) {
  373. acc += 1.0;
  374. }
  375. // Print the accumulated accuracy mean x 100
  376. printf("%zu\t%.8lf\n",task_idx+1, acc/double(task_idx+1)*100.0);
  377. fflush(stdout);
  378. }
  379. delete [] hs_data;
  380. printf("\n");
  381. }
  382. int main(int argc, char ** argv) {
  383. gpt_params params;
  384. params.n_batch = 512;
  385. if (gpt_params_parse(argc, argv, params) == false) {
  386. return 1;
  387. }
  388. params.perplexity = true;
  389. params.n_batch = std::min(params.n_batch, params.n_ctx);
  390. if (params.ppl_stride > 0) {
  391. fprintf(stderr, "Will perform strided perplexity calculation -> adjusting context size from %d to %d\n",
  392. params.n_ctx, params.n_ctx + params.ppl_stride/2);
  393. params.n_ctx += params.ppl_stride/2;
  394. }
  395. if (params.n_ctx > 2048) {
  396. fprintf(stderr, "%s: warning: model might not support context sizes greater than 2048 tokens (%d specified);"
  397. "expect poor results\n", __func__, params.n_ctx);
  398. }
  399. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  400. if (params.seed == LLAMA_DEFAULT_SEED) {
  401. params.seed = time(NULL);
  402. }
  403. fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
  404. std::mt19937 rng(params.seed);
  405. if (params.random_prompt) {
  406. params.prompt = gpt_random_prompt(rng);
  407. }
  408. llama_backend_init(params.numa);
  409. llama_model * model;
  410. llama_context * ctx;
  411. // load the model and apply lora adapter, if any
  412. std::tie(model, ctx) = llama_init_from_gpt_params(params);
  413. if (model == NULL) {
  414. fprintf(stderr, "%s: error: unable to load model\n", __func__);
  415. return 1;
  416. }
  417. // print system information
  418. {
  419. fprintf(stderr, "\n");
  420. fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
  421. params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
  422. }
  423. if (params.hellaswag) {
  424. hellaswag_score(ctx, params);
  425. } else {
  426. perplexity(ctx, params);
  427. }
  428. llama_print_timings(ctx);
  429. llama_free(ctx);
  430. llama_free_model(model);
  431. llama_backend_free();
  432. return 0;
  433. }