interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-infer / tests / ref / llama_ref.c

81 lines · 3254 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Reference harness for aienos-infer golden tests (aienos#34 lane 2+3).
18 * Links the locally built llama.cpp (libllama) and prints, for one raw prompt
19 * string (special tokens parsed, BOS NOT added: the string carries it):
20 * ids <prompt token ids>
21 * step <i> <token id> <logit gap top1-top2> (greedy = argmax, n_predict steps)
22 * and writes the first-position logits (f32 LE, n_vocab values) to argv[4].
23 * usage: llama_ref MODEL.gguf "PROMPT" N_PREDICT LOGITS_OUT.bin [teacher ids...]
24 * Single thread, CPU only (n_gpu_layers = 0). */
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include "llama.h"
29#include "ggml-backend.h"
30
31static int argmax2(const float *l, int n, float *gap) {
32 int b = 0; float b1 = l[0], b2 = -1e30f;
33 for (int i = 1; i < n; i++) {
34 if (l[i] > b1) { b2 = b1; b1 = l[i]; b = i; } else if (l[i] > b2) { b2 = l[i]; }
35 }
36 *gap = b1 - b2;
37 return b;
38}
39
40int main(int argc, char **argv) {
41 if (argc < 5) return 2;
42 ggml_backend_load_all();
43 llama_backend_init();
44 struct llama_model_params mp = llama_model_default_params();
45 mp.n_gpu_layers = 0;
46 struct llama_model *m = llama_model_load_from_file(argv[1], mp);
47 if (!m) return 3;
48 const struct llama_vocab *v = llama_model_get_vocab(m);
49 int nv = llama_vocab_n_tokens(v);
50 const char *p = argv[2];
51 llama_token toks[4096];
52 int n = llama_tokenize(v, p, (int)strlen(p), toks, 4096, /*add_special*/ 0, /*parse_special*/ 1);
53 if (n < 0) return 4;
54 printf("ids");
55 for (int i = 0; i < n; i++) printf(" %d", toks[i]);
56 printf("\n");
57 struct llama_context_params cp = llama_context_default_params();
58 cp.n_ctx = 512; cp.n_batch = 512; cp.n_threads = 1; cp.n_threads_batch = 1;
59 struct llama_context *c = llama_init_from_model(m, cp);
60 if (!c) return 5;
61 int npred = atoi(argv[3]);
62 int nteach = argc - 5;
63 struct llama_batch b = llama_batch_get_one(toks, n);
64 if (llama_decode(c, b)) return 6;
65 for (int i = 0; i < npred; i++) {
66 float *lg = llama_get_logits_ith(c, -1);
67 if (i == 0) {
68 FILE *f = fopen(argv[4], "wb");
69 fwrite(lg, sizeof(float), nv, f);
70 fclose(f);
71 }
72 float gap; int t = argmax2(lg, nv, &gap);
73 printf("step %d %d %.6f\n", i, t, gap);
74 if (llama_vocab_is_eog(v, t)) { printf("eog\n"); break; }
75 llama_token nt = (i < nteach) ? atoi(argv[5 + i]) : t;
76 struct llama_batch b1 = llama_batch_get_one(&nt, 1);
77 if (llama_decode(c, b1)) return 7;
78 }
79 llama_free(c); llama_model_free(m);
80 return 0;
81}