aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / docs / native / pi5-8 / measure-ids.patch
| 1 | diff --git a/crates/aienos-infer-kernel/src/session.rs b/crates/aienos-infer-kernel/src/session.rs |
| 2 | index b1dad4f..c4870bc 100644 |
| 3 | --- a/crates/aienos-infer-kernel/src/session.rs |
| 4 | +++ b/crates/aienos-infer-kernel/src/session.rs |
| 5 | @@ -20,6 +20,7 @@ |
| 6 | use alloc::vec::Vec; |
| 7 | use core::cell::UnsafeCell; |
| 8 | use core::slice; |
| 9 | +use core::sync::atomic::{AtomicU32, Ordering}; |
| 10 | |
| 11 | use aienos_infer::{argmax, DecodeState, Gguf, Model, Tokenizer}; |
| 12 | |
| 13 | @@ -93,6 +94,9 @@ struct Slot(UnsafeCell<Option<Session>>); |
| 14 | unsafe impl Sync for Slot {} |
| 15 | static SLOT: Slot = Slot(UnsafeCell::new(None)); |
| 16 | |
| 17 | +/// PI5-8 measurement build only: end-of-generation id of the model under test (default: the Llama-3 EOT). |
| 18 | +static EOS: AtomicU32 = AtomicU32::new(EOT); |
| 19 | + |
| 20 | fn slot() -> &'static mut Option<Session> { |
| 21 | // SAFETY: single-threaded by contract; no reference escapes a call. |
| 22 | unsafe { &mut *SLOT.0.get() } |
| 23 | @@ -253,7 +257,7 @@ pub unsafe extern "C" fn aienos_infer_session_step(token: *mut u32, us: *mut u64 |
| 24 | if !token.is_null() { |
| 25 | *token = t; |
| 26 | } |
| 27 | - if t == EOT || s.out.len() >= s.limits.max_new as usize { |
| 28 | + if t == EOS.load(Ordering::Relaxed) || s.out.len() >= s.limits.max_new as usize { |
| 29 | s.done = true; |
| 30 | rc = STEP_LAST; |
| 31 | } else { |
| 32 | @@ -382,3 +386,38 @@ pub unsafe extern "C" fn aienos_infer_session_close() -> i32 { |
| 33 | *slot() = None; |
| 34 | 0 |
| 35 | } |
| 36 | + |
| 37 | +/// PI5-8 measurement build only: set the end-of-generation token id. |
| 38 | +#[no_mangle] |
| 39 | +pub extern "C" fn aienos_infer_session_set_eos(id: u32) { |
| 40 | + EOS.store(id, Ordering::Relaxed); |
| 41 | +} |
| 42 | + |
| 43 | +/// PI5-8 measurement build only: start a request from token ids (the engine's tokenizer is bypassed; |
| 44 | +/// the ids come from llama.cpp). Same checks and effects as `aienos_infer_session_prompt`. |
| 45 | +/// |
| 46 | +/// # Safety |
| 47 | +/// `ids` must point to `n` readable u32s. Same execution contract as open. |
| 48 | +#[no_mangle] |
| 49 | +pub unsafe extern "C" fn aienos_infer_session_prompt_set_ids(ids: *const u32, n: u32) -> i32 { |
| 50 | + let s = match slot() { |
| 51 | + Some(s) => s, |
| 52 | + None => return E_STATE, |
| 53 | + }; |
| 54 | + if ids.is_null() || n == 0 { |
| 55 | + return E_ARG; |
| 56 | + } |
| 57 | + if heap_remaining() < s.limits.heap_reserve as usize { |
| 58 | + return E_HEAP; |
| 59 | + } |
| 60 | + let v: Vec<u32> = slice::from_raw_parts(ids, n as usize).to_vec(); |
| 61 | + if v.len() + s.limits.max_new as usize > s.limits.kv_capacity as usize { |
| 62 | + return E_TOO_LONG; |
| 63 | + } |
| 64 | + s.st.reset(); |
| 65 | + s.prompt = v; |
| 66 | + s.fed = 0; |
| 67 | + s.out = Vec::new(); |
| 68 | + s.done = false; |
| 69 | + s.prompt.len() as i32 |
| 70 | +} |
| 71 | diff --git a/native/kernel/core/infer.c b/native/kernel/core/infer.c |
| 72 | index 4f25b37..db15f94 100644 |
| 73 | --- a/native/kernel/core/infer.c |
| 74 | +++ b/native/kernel/core/infer.c |
| 75 | @@ -91,6 +91,8 @@ extern int32_t aienos_infer_session_prompt_ids(uint32_t *out, uint32_t cap); |
| 76 | extern int32_t aienos_infer_session_text(const uint32_t *ids, uint32_t n, uint8_t *out, uint32_t cap); |
| 77 | extern int32_t aienos_infer_session_close(void); |
| 78 | extern void aienos_infer_heap_stats(uint64_t *used, uint64_t *peak); |
| 79 | +extern int32_t aienos_infer_session_prompt_set_ids(const uint32_t *ids, uint32_t n); |
| 80 | +extern void aienos_infer_session_set_eos(uint32_t id); |
| 81 | #define SESS_E_STATE (-12) |
| 82 | #define SESS_E_PROMPT_BYTES (-16) |
| 83 | #define SESS_STEP_DONE 0 |
| 84 | @@ -118,6 +120,10 @@ static const uint32_t expect_ids[4] = {128000, 128006, 882, 128007}; |
| 85 | /* The greedy reply llama.cpp produced for the same prompt ("step" lines of the |
| 86 | * same fixture): "The capital of France is Paris." then <|eot_id|>. */ |
| 87 | static const uint32_t expect_gen[8] = {791, 6864, 315, 9822, 374, 12366, 13, 128009}; |
| 88 | +#ifdef CK_PI5_MEASURE |
| 89 | +/* PI5-8 measurement build: prompt ids and llama.cpp expected tokens of the model under test (generated header). */ |
| 90 | +#include "pi5_measure.h" |
| 91 | +#endif |
| 92 | /* Decoded reply, escaped for one console line: printable ASCII other than |
| 93 | * backslash as is, everything else as \\xNN. 4 bytes per input byte + NUL. */ |
| 94 | static char esc[4 * INFER_TEXT_BYTES + 1]; |
| 95 | @@ -268,7 +274,11 @@ static void hex(char *out, const uint8_t *b, unsigned n) |
| 96 | /* Session limits of the probe: a 64-position cache, 16 generated tokens, a |
| 97 | * 256-byte prompt and 64 KiB of heap kept free (every prompt costs a few |
| 98 | * KiB that the bump allocator never gives back). */ |
| 99 | +#ifdef CK_PI5_MEASURE |
| 100 | +#define SESS_KV 192u |
| 101 | +#else |
| 102 | #define SESS_KV 64u |
| 103 | +#endif |
| 104 | #define SESS_MAX_NEW 16u |
| 105 | #define SESS_PROMPT_BYTES 256u |
| 106 | #define SESS_RESERVE (64u << 10) |
| 107 | @@ -342,6 +352,44 @@ static int session_request(unsigned which, const char *text, unsigned cancel_aft |
| 108 | return ok; |
| 109 | } |
| 110 | |
| 111 | +#ifdef CK_PI5_MEASURE |
| 112 | +/* PI5-8 measurement request q: prompt ids from the generated header, steps with a pet each, tokens compared with |
| 113 | + * llama.cpp's greedy reply. One line per request: prefill and decode microseconds, tokens, match. */ |
| 114 | +static int measure_request(unsigned q) |
| 115 | +{ |
| 116 | + const struct meas_req *r = &meas_reqs[q]; |
| 117 | + uint64_t t0 = ck_time_us(), pre_us = 0, tok_us_sum = 0; |
| 118 | + unsigned prefill = 0, produced = 0, last = 0; |
| 119 | + int32_t ids = aienos_infer_session_prompt_set_ids(r->ids, r->nids); |
| 120 | + if (ids < 0) { |
| 121 | + ck_printf("infer: measure req=%u name=%s REFUSED rc=%d\n", q, r->name, ids); |
| 122 | + return 0; |
| 123 | + } |
| 124 | + int32_t rc = 0; |
| 125 | + for (;;) { |
| 126 | + uint32_t t = 0; |
| 127 | + uint64_t us = 0; |
| 128 | + rc = aienos_infer_session_step(&t, &us); |
| 129 | + pet(); |
| 130 | + if (rc < 0 || rc == SESS_STEP_DONE) break; |
| 131 | + if (rc == SESS_STEP_PREFILL) { prefill++; pre_us += us; continue; } |
| 132 | + produced++; |
| 133 | + tok_us_sum += us; |
| 134 | + if (rc == SESS_STEP_LAST) { last = 1; break; } |
| 135 | + } |
| 136 | + static uint32_t toks[SESS_MAX_NEW]; |
| 137 | + int32_t n = aienos_infer_session_tokens(toks, SESS_MAX_NEW); |
| 138 | + int ok = rc >= 0 && n == (int32_t)produced && last && (unsigned)ids == prefill && produced == r->nexp; |
| 139 | + for (unsigned i = 0; ok && i < r->nexp; i++) ok = ok && toks[i] == r->exp[i]; |
| 140 | + uint64_t mean = produced > 1 ? tok_us_sum / (produced - 1) : 0; |
| 141 | + ck_printf("infer: measure req=%u name=%s ids=%d prefill=%u prefill_us=%llu produced=%u mean_tok_us=%llu tokens=", q, r->name, ids, |
| 142 | + prefill, (unsigned long long)pre_us, produced, (unsigned long long)mean); |
| 143 | + for (unsigned i = 0; i < produced; i++) ck_printf("%s%u", i ? " " : "", toks[i]); |
| 144 | + ck_printf(" golden=%s request_us=%llu rc=%d\n", ok ? "match" : "MISMATCH", (unsigned long long)(ck_time_us() - t0), rc); |
| 145 | + return ok; |
| 146 | +} |
| 147 | +#endif |
| 148 | + |
| 149 | /* The bounded session over the ingested model: open with explicit limits, |
| 150 | * a request cancelled during its prefill, the golden prompt run to its end |
| 151 | * through the step API (must equal the one-shot probe's reply), a second |
| 152 | @@ -369,6 +417,12 @@ static void session_run(uint64_t mpa, uint64_t msize) |
| 153 | if (ok) { |
| 154 | /* Cheap refusals: a second open, an over-long prompt, a step with no |
| 155 | * request pending. None of them touches the model. */ |
| 156 | +#ifdef CK_PI5_MEASURE |
| 157 | + aienos_infer_session_set_eos(MEAS_EOS); |
| 158 | + for (unsigned q = 0; q < MEAS_NREQ; q++) ok = measure_request(q) && ok; |
| 159 | + if (0) |
| 160 | +#endif |
| 161 | + { |
| 162 | static char longp[SESS_PROMPT_BYTES + 2]; |
| 163 | memset(longp, 'a', sizeof longp - 1); |
| 164 | longp[sizeof longp - 1] = 0; |
| 165 | @@ -382,6 +436,7 @@ static void session_run(uint64_t mpa, uint64_t msize) |
| 166 | ok = session_request(2, "What is the capital of France?", 0, expect_gen, |
| 167 | sizeof expect_gen / sizeof expect_gen[0], expect_ids) && ok; |
| 168 | ok = session_request(3, "Say hello.", 1, 0, 0, 0) && ok; |
| 169 | + } |
| 170 | } |
| 171 | uint64_t used = 0, peak = 0; |
| 172 | aienos_infer_heap_stats(&used, &peak); |