aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / om / ribbon / chip_probe.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Measurement instrument only. All queue/ownership policy is pipeline.om. |
| 3 | * Connects its actions to real O_DIRECT reads, SHA256 verification and the |
| 4 | * existing Omega FP8 slot API. It is not a model serving implementation. |
| 5 | */ |
| 6 | #define _GNU_SOURCE |
| 7 | #include "omega_native.h" |
| 8 | #include "omega_gpu_fp8_slots.h" |
| 9 | #include <errno.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <inttypes.h> |
| 12 | #include <openssl/sha.h> |
| 13 | #include <pthread.h> |
| 14 | #include <stdatomic.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <time.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | enum { E = 32, N = 2048, K = 768, SCALE_COUNT = (N/128)*(K/128), |
| 24 | WEIGHT_BYTES = N*K, PAYLOAD = WEIGHT_BYTES+2*SCALE_COUNT, |
| 25 | STRIDE = (PAYLOAD+4095)/4096*4096 }; |
| 26 | typedef struct { |
| 27 | int fd; |
| 28 | uint32_t key; |
| 29 | uint64_t ticket, begin, io_end, end; |
| 30 | void *buf; |
| 31 | unsigned char expected[SHA256_DIGEST_LENGTH]; |
| 32 | pthread_t thread; |
| 33 | atomic_int done; |
| 34 | bool active, owned, corrupt; |
| 35 | int ok; |
| 36 | } Read; |
| 37 | static uint64_t state[4096]; |
| 38 | static unsigned char hashes[E][SHA256_DIGEST_LENGTH]; |
| 39 | static uint64_t now_ns(void) { |
| 40 | struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); |
| 41 | return (uint64_t)t.tv_sec*1000000000ull+(uint64_t)t.tv_nsec; |
| 42 | } |
| 43 | static int call(uint64_t op, uint64_t key, uint64_t bytes, uint64_t deadline, |
| 44 | uint64_t verified, uint64_t out[8]) { |
| 45 | uint64_t q[8]={op,key,bytes,deadline,verified,0,now_ns(),0}, result=99; |
| 46 | uint32_t trap=omega_pipeline(&result,state,4096,q,8,out,8); |
| 47 | if (trap) { fprintf(stderr,"Omega trap %u\n",trap); return -1; } |
| 48 | return (int)result; |
| 49 | } |
| 50 | static void *read_job(void *arg) { |
| 51 | Read *r=arg; |
| 52 | r->begin=now_ns(); |
| 53 | ssize_t n; |
| 54 | do { n=pread(r->fd,r->buf,STRIDE,(off_t)r->key*STRIDE); } while(n<0 && errno==EINTR); |
| 55 | r->io_end=now_ns(); |
| 56 | if(r->corrupt && n==STRIDE) ((unsigned char*)r->buf)[128]^=1; |
| 57 | unsigned char got[SHA256_DIGEST_LENGTH]; |
| 58 | r->ok=n==STRIDE && SHA256(r->buf,PAYLOAD,got)!=NULL && memcmp(got,r->expected,sizeof got)==0; |
| 59 | r->end=now_ns(); |
| 60 | atomic_store_explicit(&r->done,1,memory_order_release); |
| 61 | return NULL; |
| 62 | } |
| 63 | static int fixture(const char *path, unsigned char **all_w, uint16_t **all_s) { |
| 64 | *all_w=malloc((size_t)E*WEIGHT_BYTES); *all_s=malloc((size_t)E*2*SCALE_COUNT); |
| 65 | unsigned char *b=NULL; |
| 66 | if(!*all_w || !*all_s || posix_memalign((void**)&b,4096,STRIDE)) return 1; |
| 67 | int fd=open(path,O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC,0600); |
| 68 | if(fd<0) { free(b); return 1; } |
| 69 | for(unsigned e=0;e<E;e++) { |
| 70 | memset(b,0,STRIDE); |
| 71 | uint32_t rng=17+e; |
| 72 | for(size_t j=0;j<WEIGHT_BYTES;j++) { |
| 73 | rng=rng*1664525u+1013904223u; |
| 74 | b[j]=(unsigned char)((rng>>24)%119); /* finite positive E4M3 */ |
| 75 | } |
| 76 | uint16_t *s=(uint16_t*)(b+WEIGHT_BYTES); |
| 77 | for(unsigned j=0;j<SCALE_COUNT;j++) s[j]=0x3b80; /* bf16 scale */ |
| 78 | memcpy(*all_w+(size_t)e*WEIGHT_BYTES,b,WEIGHT_BYTES); |
| 79 | memcpy(*all_s+(size_t)e*SCALE_COUNT,s,2*SCALE_COUNT); |
| 80 | SHA256(b,PAYLOAD,hashes[e]); |
| 81 | if(write(fd,b,STRIDE)!=STRIDE) { close(fd);free(b);return 1; } |
| 82 | } |
| 83 | int rc=fsync(fd); close(fd);free(b);return rc!=0; |
| 84 | } |
| 85 | static int trial(int fd,unsigned buffers,int mutant,OmegaGpuFp8Slots *pool, |
| 86 | const float *x,const float *reference,unsigned trial_id) { |
| 87 | uint64_t out[8], versions[E]={0}; |
| 88 | float *got=malloc(N*sizeof(float)); |
| 89 | Read reads[2]={0}; |
| 90 | for(unsigned i=0;i<2;i++) { |
| 91 | if(posix_memalign(&reads[i].buf,4096,STRIDE)) return 1; |
| 92 | reads[i].fd=fd; atomic_init(&reads[i].done,0); |
| 93 | } |
| 94 | memset(state,0,sizeof state); |
| 95 | int failed=call(1,E,(uint64_t)buffers*STRIDE,buffers,0,out)!=0; |
| 96 | uint64_t start=now_ns(), deadline=start+30000000000ull; |
| 97 | for(unsigned e=0;e<E && !failed;e++) { |
| 98 | failed=call(2,e,STRIDE,deadline,0,out)!=0; |
| 99 | } |
| 100 | unsigned completed=0, rejected=0; |
| 101 | uint64_t overlap_ns=0, read_ns=0, compute_ns=0, copy_ns=0; |
| 102 | uint64_t read_start[E]={0},read_end[E]={0},io_end[E]={0},copy_start[E]={0},copy_end[E]={0},compute_start[E]={0},compute_end[E]={0}; |
| 103 | while(!failed && completed+rejected<E) { |
| 104 | if(now_ns()>deadline) { failed=1;break; } |
| 105 | bool progress=false; |
| 106 | for(unsigned i=0;i<buffers;i++) if(reads[i].active && atomic_load_explicit(&reads[i].done,memory_order_acquire)) { |
| 107 | Read *r=&reads[i]; pthread_join(r->thread,NULL);r->active=false; |
| 108 | read_start[r->key]=r->begin;read_end[r->key]=r->end;io_end[r->key]=r->io_end;read_ns+=r->end-r->begin; |
| 109 | if(call(4,r->ticket,0,0,r->ok?1:0,out)) { failed=1;break; } |
| 110 | if(!r->ok) { |
| 111 | if(call(9,r->ticket,0,0,0,out)) failed=1; |
| 112 | r->owned=false; |
| 113 | rejected++; |
| 114 | } |
| 115 | progress=true; |
| 116 | } |
| 117 | for(unsigned i=0;i<buffers && !failed;i++) if(!reads[i].owned) { |
| 118 | int rc=call(3,0,0,0,0,out); |
| 119 | if(rc==1) break; |
| 120 | if(rc!=0) { failed=1;break; } |
| 121 | Read *r=&reads[i]; r->ticket=out[0];r->key=(uint32_t)out[1]; |
| 122 | r->corrupt=mutant && r->key==3;memcpy(r->expected,hashes[r->key],sizeof r->expected); |
| 123 | atomic_store_explicit(&r->done,0,memory_order_relaxed);r->active=true;r->owned=true; |
| 124 | if(pthread_create(&r->thread,NULL,read_job,r)) { r->active=false;failed=1;break; } |
| 125 | progress=true; |
| 126 | } |
| 127 | if(failed) break; |
| 128 | int rc=call(5,0,0,0,0,out); |
| 129 | if(rc==0) { |
| 130 | uint64_t ticket=out[0];uint32_t key=(uint32_t)out[1]; |
| 131 | Read *r=NULL; |
| 132 | for(unsigned i=0;i<buffers;i++) if(reads[i].ticket==ticket) r=&reads[i]; |
| 133 | /* A completed buffer must not have been reassigned before its copy. */ |
| 134 | if(!r || r->active) { failed=1;break; } |
| 135 | uint64_t c0=now_ns();copy_start[key]=c0; |
| 136 | rc=omega_gpu_fp8_slots_load(pool,key,key,r->buf,(uint16_t*)((unsigned char*)r->buf+WEIGHT_BYTES),&versions[key]); |
| 137 | copy_end[key]=now_ns();copy_ns+=copy_end[key]-c0; |
| 138 | if(call(6,ticket,0,0,rc==0?1:0,out) || rc) { failed=1;break; } |
| 139 | r->owned=false; |
| 140 | if(call(7,ticket,0,0,0,out)) { failed=1;break; } |
| 141 | compute_start[key]=now_ns(); |
| 142 | rc=omega_gpu_fp8_slots_gemv_grouped_f32(pool,1,&key,&versions[key],x,1,got,NULL); |
| 143 | compute_end[key]=now_ns();compute_ns+=compute_end[key]-compute_start[key]; |
| 144 | if(rc || memcmp(got,reference+(size_t)key*N,N*sizeof(float))) { failed=1;break; } |
| 145 | if(call(8,ticket,0,0,0,out) || call(9,ticket,0,0,0,out)) { failed=1;break; } |
| 146 | completed++;progress=true; |
| 147 | } else if(rc!=1) { failed=1;break; } |
| 148 | if(!progress) { struct timespec pause={0,100000}; nanosleep(&pause,NULL); } |
| 149 | } |
| 150 | uint64_t wall=now_ns()-start; |
| 151 | /* Stop admissions, wait for any actual I/O, acknowledge it, then free storage. */ |
| 152 | call(11,0,0,0,0,out); |
| 153 | for(unsigned i=0;i<2;i++) { |
| 154 | if(reads[i].active) { pthread_join(reads[i].thread,NULL);call(4,reads[i].ticket,0,0,0,out); } |
| 155 | free(reads[i].buf); |
| 156 | } |
| 157 | for(unsigned r=0;r<E;r++) for(unsigned c=0;c<E;c++) { |
| 158 | uint64_t lo=read_start[r]>compute_start[c]?read_start[r]:compute_start[c]; |
| 159 | uint64_t hi=read_end[r]<compute_end[c]?read_end[r]:compute_end[c]; |
| 160 | if(hi>lo) overlap_ns+=hi-lo; |
| 161 | } |
| 162 | free(got); |
| 163 | printf("{\"trial\":%u,\"buffers\":%u,\"mutant\":%d,\"completed\":%u,\"rejected\":%u,\"wall_ns\":%"PRIu64",\"read_ns\":%"PRIu64",\"copy_ns\":%"PRIu64",\"compute_ns\":%"PRIu64",\"read_compute_pair_overlap_ns\":%"PRIu64",\"failed\":%d,\"origin_monotonic_ns\":%"PRIu64",\"spans\":[", |
| 164 | trial_id,buffers,mutant,completed,rejected,wall,read_ns,copy_ns,compute_ns,overlap_ns,failed,start); |
| 165 | for(unsigned e=0;e<E;e++) { |
| 166 | printf("%s{\"expert\":%u,\"read\":[%"PRIu64",%"PRIu64"],\"verify\":[%"PRIu64",%"PRIu64"],\"copy\":[%"PRIu64",%"PRIu64"],\"compute\":[%"PRIu64",%"PRIu64"]}", |
| 167 | e?",":"",e,read_start[e],io_end[e],io_end[e],read_end[e],copy_start[e],copy_end[e],compute_start[e],compute_end[e]); |
| 168 | } |
| 169 | printf("]}\n"); |
| 170 | fflush(stdout); |
| 171 | return failed || (mutant ? rejected!=1 || completed!=E-1 : rejected!=0 || completed!=E); |
| 172 | } |
| 173 | int main(int argc,char **argv) { |
| 174 | if(argc!=2) { fprintf(stderr,"usage: chip_probe NEW-FIXTURE-PATH\n");return 2; } |
| 175 | unsigned char *w=NULL;uint16_t *s=NULL; |
| 176 | if(fixture(argv[1],&w,&s)) { perror("fixture");return 2; } |
| 177 | int fd=open(argv[1],O_RDONLY|O_DIRECT|O_CLOEXEC); |
| 178 | if(fd<0) { perror("direct read");return 2; } |
| 179 | OmegaGpuFp8Tensor *resident=NULL;OmegaGpuFp8Slots *pool=NULL; |
| 180 | int rc=omega_gpu_fp8_tensor_upload(E,N,K,w,s,&resident); |
| 181 | if(!rc) rc=omega_gpu_fp8_slots_alloc(E,N,K,&pool); |
| 182 | if(rc) { fprintf(stderr,"GPU setup: %s\n",omega_gpu_fp8_last_error());return 2; } |
| 183 | float x[K],*ref=malloc((size_t)E*N*sizeof(float)); |
| 184 | for(unsigned j=0;j<K;j++) x[j]=(float)(j%17)/17.0f; |
| 185 | for(uint32_t e=0;e<E;e++) { |
| 186 | if(omega_gpu_fp8_gemv_grouped_f32(resident,1,&e,x,1,ref+(size_t)e*N,NULL)) return 2; |
| 187 | } |
| 188 | int failed=trial(fd,1,0,pool,x,ref,0); /* excluded warmup */ |
| 189 | for(unsigned i=1;i<=10 && !failed;i++) failed|=trial(fd,(i&1)?1:2,0,pool,x,ref,i); |
| 190 | if(!failed) failed|=trial(fd,2,1,pool,x,ref,11); |
| 191 | omega_gpu_fp8_slots_free(pool);omega_gpu_fp8_tensor_free(resident); |
| 192 | close(fd);free(w);free(s);free(ref); |
| 193 | return failed; |
| 194 | } |