/* dragon_native.c — 560B engine: NATIVE-PRECISION compute + MLA KV cache. * Matmuls read the mmap'd bf16/fp8 bytes DIRECTLY (inline dequant per element). * No f32 weight buffers, no LRU — the OS page cache holds the native bytes (2-4x smaller than f32). * KV cache validated in dragon_fast.c (reproduces recompute output exactly). * Build: gcc -O3 -march=native -fopenmp -D_GNU_SOURCE -o dragon_native dragon_native.c -lm */ #include #include #include #include #include #include #include #include #include #include #define HID 6144 #define H 64 #define NOPE 128 #define ROPE 64 #define HD 192 #define VD 128 #define QLORA 1536 #define KVLORA 512 #define EFFN 2048 #define FFN 12288 #define NEXP_TOT 768 #define NR 512 #define SEQMAX 64 #define NSUB 56 #pragma pack(push,1) typedef struct { char name[128]; int32_t shard, dt; int64_t off, d0, d1; int32_t sshard; int64_t soff; int32_t s0,s1; } Rec; #pragma pack(pop) static Rec* recs; static int nrec; static const uint8_t* shard_base[128]; static int nshard; static void load_manifest(void){ FILE* f=fopen("manifest.bin","rb"); if(!f){perror("manifest");exit(1);} if(fread(&nrec,4,1,f)!=1)exit(1); recs=malloc((long)nrec*sizeof(Rec)); if(fread(recs,sizeof(Rec),nrec,f)!=(size_t)nrec)exit(1); fclose(f); FILE* s=fopen("shards.txt","r"); char line[1024]; while(fgets(line,sizeof line,s)){ line[strcspn(line,"\n")]=0; int fd=open(line,O_RDONLY); struct stat st; fstat(fd,&st); shard_base[nshard]=mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,fd,0); if(shard_base[nshard]==MAP_FAILED){perror(line);exit(1);} close(fd); nshard++; } fclose(s); fprintf(stderr,"manifest: %d tensors, %d shards\n",nrec,nshard); } static Rec* find(const char* nm){ int lo=0,hi=nrec-1; while(lo<=hi){int m=(lo+hi)/2;int c=strcmp(recs[m].name,nm);if(c==0)return &recs[m];if(c<0)lo=m+1;else hi=m-1;} fprintf(stderr,"MISSING %s\n",nm);exit(2); } static inline float bf16f(uint16_t b){uint32_t u=((uint32_t)b)<<16;float f;memcpy(&f,&u,4);return f;} static inline float e4m3f(uint8_t b){int s=(b>>7)&1,e=(b>>3)&0xF,m=b&7;float v;if(e==0xF&&m==7)return NAN;if(e==0)v=ldexpf(m/8.0f,-6);else v=ldexpf(1.0f+m/8.0f,e-7);return s?-v:v;} /* ---- NATIVE matvec: o[M] = W @ x, W read directly from mmap in its own dtype ---- * THE MATMUL: fp8 via 256-entry LUT (e4m3 has only 256 values — zero ldexpf calls); * bf16 via AVX2+FMA (8 weights/instruction: widen u16, <<16, that IS the f32). */ static float E4M3_LUT[256]; static void lut_init(void){ for(int b=0;b<256;b++) E4M3_LUT[b]=e4m3f((uint8_t)b); } #ifdef __AVX2__ #include static inline float hsum8(__m256 v){ __m128 lo=_mm256_castps256_ps128(v), hi=_mm256_extractf128_ps(v,1); lo=_mm_add_ps(lo,hi); lo=_mm_hadd_ps(lo,lo); lo=_mm_hadd_ps(lo,lo); return _mm_cvtss_f32(lo); } static inline float dot_bf16(const uint16_t* w, const float* x, long K){ __m256 a0=_mm256_setzero_ps(), a1=_mm256_setzero_ps(); long k=0; for(; k+16<=K; k+=16){ __m256i w0=_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*)(w+k))),16); __m256i w1=_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*)(w+k+8))),16); a0=_mm256_fmadd_ps(_mm256_castsi256_ps(w0),_mm256_loadu_ps(x+k),a0); a1=_mm256_fmadd_ps(_mm256_castsi256_ps(w1),_mm256_loadu_ps(x+k+8),a1); } float s=hsum8(_mm256_add_ps(a0,a1)); for(; kd0, K=r->d1; const uint8_t* base=shard_base[r->shard]+r->off; if(r->dt==0){ const uint16_t* W=(const uint16_t*)base; #pragma omp parallel for schedule(static) for(long i=0;idt==1){ const float* W=(const float*)base; #pragma omp parallel for schedule(static) for(long i=0;isshard]+r->soff); long s1=r->s1; #pragma omp parallel for schedule(static) for(long i=0;id0*r->d1; const uint8_t* p=shard_base[r->shard]+r->off; if(r->dt==0){const uint16_t* w=(const uint16_t*)p; for(long i=0;imx)mx=sc[k2];} double sm=0;for(int k2=0;k2<=pos;k2++){sc[k2]=expf(sc[k2]-mx);sm+=sc[k2];} for(int d=0;dmx)mx=lg[i];double sm=0;float sf[NEXP_TOT];for(int i=0;ibv){bv=ch[i];bi=i;}}idx[j]=bi;} for(int j=0;j<12;j++){int e=idx[j];float w=sf[e]*6.0f; if(e>=NR){for(int i=0;ishard]+r->off);for(int t=0;td0; const uint16_t*w=(const uint16_t*)(shard_base[r->shard]+r->off); int best=-1;float bv=-1e30f; #pragma omp parallel { int lb=-1;float lv=-1e30f; #pragma omp for for(long v=0;vlv){lv=s;lb=v;}} #pragma omp critical {if(lv>bv){bv=lv;best=lb;}} } return best; } static float* Lf(const char*n,long c){char p[512];snprintf(p,sizeof p,"ref/%s.f32",n);FILE*f=fopen(p,"rb");if(!f){perror(p);exit(1);}float*b=malloc(c*4);if(fread(b,4,c,f)!=(size_t)c)exit(1);fclose(f);return b;} int main(int argc,char**argv){ int NGEN=argc>1?atoi(argv[1]):8; load_manifest(); load_vocab(); lut_init(); KC=malloc((long)NSUB*sizeof(*KC)); VC=malloc((long)NSUB*sizeof(*VC)); G_COS=Lf("rope_cos64",64*64); G_SIN=Lf("rope_sin64",64*64); FILE* pf=fopen("prompt_ids.i32","rb"); fseek(pf,0,SEEK_END);int pn=ftell(pf)/4;fseek(pf,0,SEEK_SET); int ids[SEQMAX]; if(fread(ids,4,pn,pf)!=(size_t)pn)exit(1); fclose(pf); static float x[(long)SEQMAX*HID]; struct timespec t0,t1; clock_gettime(CLOCK_MONOTONIC,&t0); embed_ids(ids,pn,x); for(int L=0;L<28;L++) layer(L,x,pn,0); int nx=lm_argmax(x+(long)(pn-1)*HID); clock_gettime(CLOCK_MONOTONIC,&t1); double pt=(t1.tv_sec-t0.tv_sec)+(t1.tv_nsec-t0.tv_nsec)/1e9; printf("PROMPT: "); for(int i=0;i