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 / native / kernel / x86 / model / runtime.inc
| 1 | /* Boot-only experiment. Every floating-point inference operation is in llama.om. |
| 2 | * This hardware/ABI adapter owns signatures, pages, deadlines and text display. */ |
| 3 | static void model_init(void) |
| 4 | { |
| 5 | struct cka_parsed p; |
| 6 | struct cka_ident id; |
| 7 | uint8_t fp[32], digest[32]; |
| 8 | const struct ck_local_artifact *a = &ck_model_artifact; |
| 9 | if (!equal(ck_model_key, ck_local_keys[0], 32) || |
| 10 | cka_verify_profile(a->bytes, a->length, ck_local_keys, 1, &p, &id, fp, |
| 11 | CKA_TARGET_X86_64_LE, CKA_ABI_OMEGA_X86_MODEL_V1)) return; |
| 12 | sha256_hash(a->bytes, a->length, digest); |
| 13 | if (!equal(digest, a->hash, 32) || p.entry || p.ncaps || p.data.flen != 64 || |
| 14 | p.code.flen > sizeof model_code || ck_model_weights_end - ck_model_weights != 60816028) |
| 15 | return; |
| 16 | const uint8_t *bindings = a->bytes + p.payload_off + p.data.rel; |
| 17 | sha256_hash(ck_model_weights, ck_model_weights_end - ck_model_weights, digest); |
| 18 | if (!equal(digest, bindings, 32)) return; |
| 19 | sha256_hash(ck_model_tokenizer, ck_model_tokenizer_end - ck_model_tokenizer, digest); |
| 20 | if (!equal(digest, bindings + 32, 32)) return; |
| 21 | model_bytes = a->bytes + p.payload_off; model_len = p.code.flen; |
| 22 | sha256_hash(model_bytes, model_len, model_code_hash); |
| 23 | model_ok = 1; |
| 24 | ck_puts("local_model: admitted=1 model=TinyStories15M inference=Omega backend=CPU-f32 profile=3\n"); |
| 25 | } |
| 26 | static void model_map(uint64_t address, const uint8_t *backing, size_t length, uint64_t flags) |
| 27 | { |
| 28 | size_t pages = (length + PAGE - 1) / PAGE; |
| 29 | for (size_t i = 0; i < pages; i++, address += PAGE) { |
| 30 | unsigned dir = (unsigned)((address - BASE) >> 21); |
| 31 | unsigned leaf = (unsigned)((address >> 12) & 511); |
| 32 | pd[dir] = (uint64_t)(uintptr_t)model_pt[dir - 1] | USER | WRITE; |
| 33 | model_pt[dir - 1][leaf] = (uint64_t)(uintptr_t)(backing + i * PAGE) | USER | flags; |
| 34 | } |
| 35 | } |
| 36 | static int model_prepare(void) |
| 37 | { |
| 38 | /* prepare supplies the common entry veneer, stack, input, and FP state. */ |
| 39 | const uint8_t placeholder[] = {0x0f, 0x0b}; |
| 40 | uint64_t args[] = {1, 32, 60816028 / 4, 1}; /* public BOS, greedy, 32 tokens */ |
| 41 | if (!model_ok || prepare(placeholder, sizeof placeholder, args, sizeof args)) return -1; |
| 42 | model_running = 1; |
| 43 | memcpy(model_code, model_bytes, model_len); |
| 44 | uint8_t digest[32]; sha256_hash(model_code, model_len, digest); |
| 45 | if (!equal(digest, model_code_hash, 32)) return -1; |
| 46 | /* Remove the small tools' code and work mappings from this address space. */ |
| 47 | pt[1] = 0; |
| 48 | for (unsigned i = 24; i < 56; i++) pt[i] = 0; |
| 49 | model_map(MODEL_CODE, model_code, model_len, 0); |
| 50 | model_map(MODEL_WORK, model_work, sizeof model_work, NX | WRITE); |
| 51 | model_map(MODEL_WEIGHTS, ck_model_weights, ck_model_weights_end - ck_model_weights, NX); |
| 52 | uint64_t *end = (void *)(stack + sizeof stack); |
| 53 | end[-2] = MODEL_WORK; end[-4] = MODEL_WEIGHTS; |
| 54 | frame.rip = MODEL_CODE; |
| 55 | cpu_limit = 120 * hz; elapsed_limit = 180 * hz; |
| 56 | return 0; |
| 57 | } |
| 58 | /* The tokenizer is a signed fixed asset. Bounds are checked before every read; |
| 59 | * no tokenizer pointer is ever exposed to the model. */ |
| 60 | static int model_decode(const struct ck_local_result *r, char *out, size_t cap) |
| 61 | { |
| 62 | if (r->state != CK_LOCAL_RETURNED || r->length < 64 || cap < 1) return -1; |
| 63 | uint32_t count; memcpy(&count, r->output, 4); |
| 64 | if (count > 32 || r->length != 4 * (16 + count)) return -1; |
| 65 | const uint8_t *end = ck_model_tokenizer_end; |
| 66 | size_t used = 0; |
| 67 | for (unsigned i = 0; i < count; i++) { |
| 68 | uint32_t token; memcpy(&token, r->output + 4 * (16 + i), 4); |
| 69 | if (token >= 32000) return -1; |
| 70 | const uint8_t *p = ck_model_tokenizer + 4; |
| 71 | uint32_t len = 0; |
| 72 | for (uint32_t j = 0; j <= token; j++) { |
| 73 | if (end - p < 8) return -1; |
| 74 | memcpy(&len, p + 4, 4); p += 8; |
| 75 | if (len > (size_t)(end - p)) return -1; |
| 76 | if (j != token) p += len; |
| 77 | } |
| 78 | if (i == 0 && len && p[0] == ' ') { p++; len--; } |
| 79 | if (len > cap - 1 - used) return -1; |
| 80 | for (uint32_t j = 0; j < len; j++) { |
| 81 | unsigned char c = p[j]; |
| 82 | out[used++] = (c >= 32 && c < 127) ? (char)c : ' '; |
| 83 | } |
| 84 | } |
| 85 | out[used] = 0; |
| 86 | return (int)count; |
| 87 | } |
| 88 | const char *ck_local_model_demo_step(void) |
| 89 | { |
| 90 | static char display[1024]; |
| 91 | if (!model_demo_state) { |
| 92 | model_demo_state = 1; |
| 93 | if (!model_ok || running || active_request) { |
| 94 | model_demo_state = 2; |
| 95 | ck_puts("local_model: experiment skipped (admission or active request)\n"); |
| 96 | return 0; |
| 97 | } |
| 98 | wipe(&model_result, sizeof model_result); |
| 99 | running = &model_result; running->state = CK_LOCAL_RUNNING; |
| 100 | if (model_prepare()) { finish(CK_LOCAL_REFUSED, CK_LOCAL_ADMISSION); clear_task(); } |
| 101 | ck_puts("local_model: starting public BOS experiment tokens=32 cpu_limit_s=120\n"); |
| 102 | } |
| 103 | if (model_demo_state != 1 || model_running) return 0; |
| 104 | model_demo_state = 2; |
| 105 | char sample[768]; |
| 106 | int tokens = model_decode(&model_result, sample, sizeof sample); |
| 107 | if (tokens < 0) { |
| 108 | ck_printf("local_model: incomplete state=%u reason=%u\n", model_result.state, model_result.reason); |
| 109 | return 0; |
| 110 | } |
| 111 | ck_printf("local_model: completed tokens=%u cpu_ms=%llu elapsed_ms=%llu real_cpl3=yes\n", |
| 112 | (unsigned)tokens, (unsigned long long)model_cpu_ms, (unsigned long long)model_elapsed_ms); |
| 113 | /* Only this fixed public prompt is logged, never a user conversation. */ |
| 114 | ck_printf("local_model: sample=%s\n", sample); |
| 115 | ck_snprintf(display, sizeof display, |
| 116 | "Native Omega model test: %u tokens in %llu ms on this iMac's CPU. TinyStories15M says: %s", |
| 117 | (unsigned)tokens, (unsigned long long)model_elapsed_ms, sample); |
| 118 | return display; |
| 119 | } |