MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / cengine_debug.c
| 1 | /* cengine_debug.c — Per-layer forward with debug prints. */ |
| 2 | #include <stdio.h> |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | #include <math.h> |
| 6 | #include <immintrin.h> |
| 7 | #include <omp.h> |
| 8 | #define Q4_0_BS 18 |
| 9 | #define Q8_0_BS 34 |
| 10 | static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } |
| 11 | static inline float hsum_ps(__m256 v) { |
| 12 | __m128 l=_mm256_castps256_ps128(v),h=_mm256_extractf128_ps(v,1); |
| 13 | l=_mm_add_ps(l,h);l=_mm_hadd_ps(l,l);l=_mm_hadd_ps(l,l);return _mm_cvtss_f32(l); |
| 14 | } |
| 15 | static inline float q4d(const uint8_t *W, const float *x, int nc, int r) { |
| 16 | int bp=nc/32; float total=0.0f; |
| 17 | for(int b=0;b<bp;b++){ |
| 18 | const uint8_t *p=W+((size_t)r*bp+b)*Q4_0_BS; |
| 19 | float sc=f16_to_f32((uint16_t)p[0]|((uint16_t)p[1]<<8)); |
| 20 | __m256 sv=_mm256_set1_ps(sc); |
| 21 | __m128i nb=_mm_loadu_si128((const __m128i*)(p+2)); |
| 22 | __m128i lo=_mm_and_si128(nb,_mm_set1_epi8(15)); |
| 23 | __m128i hi=_mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb,_mm_set1_epi8((char)0xF0)),4),_mm_set1_epi8(15)); |
| 24 | __m128i ls=_mm_sub_epi8(lo,_mm_set1_epi8(8)),hs=_mm_sub_epi8(hi,_mm_set1_epi8(8)); |
| 25 | __m256 v0=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(ls))); |
| 26 | __m256 v1=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(ls,0x4e)))); |
| 27 | __m256 v2=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(hs))); |
| 28 | __m256 v3=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(hs,0x4e)))); |
| 29 | v0=_mm256_mul_ps(v0,sv);v1=_mm256_mul_ps(v1,sv);v2=_mm256_mul_ps(v2,sv);v3=_mm256_mul_ps(v3,sv); |
| 30 | int o=b*32; __m256 a=_mm256_mul_ps(v0,_mm256_loadu_ps(x+o)); |
| 31 | a=_mm256_fmadd_ps(v1,_mm256_loadu_ps(x+o+8),a); |
| 32 | a=_mm256_fmadd_ps(v2,_mm256_loadu_ps(x+o+16),a); |
| 33 | a=_mm256_fmadd_ps(v3,_mm256_loadu_ps(x+o+24),a); |
| 34 | __m128 l=_mm256_castps256_ps128(a),h=_mm256_extractf128_ps(a,1); |
| 35 | l=_mm_add_ps(l,h);l=_mm_hadd_ps(l,l);l=_mm_hadd_ps(l,l); |
| 36 | total+=_mm_cvtss_f32(l); |
| 37 | } |
| 38 | return total; |
| 39 | } |
| 40 | static inline float q8d(const uint8_t *W, const float *x, int nc, int r) { |
| 41 | int bp=nc/32; float t=0; |
| 42 | for(int b=0;b<bp;b++){ |
| 43 | const uint8_t *p=W+((size_t)r*bp+b)*Q8_0_BS; |
| 44 | float d=f16_to_f32((uint16_t)p[0]|((uint16_t)p[1]<<8)); |
| 45 | const int8_t *qs=(const int8_t*)(p+2); |
| 46 | __m256 dv=_mm256_set1_ps(d),bs=_mm256_setzero_ps(); int o=b*32; |
| 47 | for(int i=0;i<32;i+=8){ |
| 48 | __m128i q8=_mm_loadl_epi64((const __m128i*)(qs+i)); |
| 49 | __m256 v=_mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(q8))),dv); |
| 50 | bs=_mm256_fmadd_ps(v,_mm256_loadu_ps(x+o+i),bs);} |
| 51 | t+=hsum_ps(bs); |
| 52 | } |
| 53 | return t; |
| 54 | } |
| 55 | static inline void rms(float *o, const float *x, const float *w, int n, float e){ |
| 56 | __m256 ss=_mm256_setzero_ps(); int i; |
| 57 | for(i=0;i<=n-8;i+=8){__m256 v=_mm256_loadu_ps(x+i);ss=_mm256_fmadd_ps(v,v,ss);} |
| 58 | __m256 hh=_mm256_hadd_ps(ss,_mm256_permute2f128_ps(ss,ss,1)); |
| 59 | hh=_mm256_hadd_ps(hh,hh);hh=_mm256_hadd_ps(hh,hh);float sse=_mm256_cvtss_f32(hh); |
| 60 | for(;i<n;i++)sse+=x[i]*x[i];float ir=1.0f/sqrtf(sse/n+e);__m256 iv=_mm256_set1_ps(ir); |
| 61 | for(i=0;i<=n-8;i+=8){__m256 xv=_mm256_loadu_ps(x+i),wv=_mm256_loadu_ps(w+i); |
| 62 | _mm256_storeu_ps(o+i,_mm256_mul_ps(_mm256_mul_ps(xv,iv),wv));} |
| 63 | for(;i<n;i++)o[i]=x[i]*ir*w[i]; |
| 64 | } |
| 65 | static void gqa(float *o, const float *q, const float *kc, const float *vc, |
| 66 | int sl, int nh, int nkh, int hd){ |
| 67 | int gr=nh/nkh; |
| 68 | #pragma omp parallel for schedule(static) |
| 69 | for(int h=0;h<nh;h++){ |
| 70 | int kv=h/gr;const float *qh=q+h*hd,*kb=kc+kv*hd,*vb=vc+kv*hd; |
| 71 | int ks=nkh*hd;float *oh=o+h*hd,sc[4096],mx=-1e30f,inv=1.0f/sqrtf(hd); |
| 72 | for(int s=0;s<sl;s++){const float*ks_=kb+s*ks;__m256 su=_mm256_setzero_ps();int d; |
| 73 | for(d=0;d<=hd-8;d+=8)su=_mm256_fmadd_ps(_mm256_loadu_ps(qh+d),_mm256_loadu_ps(ks_+d),su); |
| 74 | float dot=hsum_ps(su);for(;d<hd;d++)dot+=qh[d]*ks_[d];dot*=inv;sc[s]=dot;if(dot>mx)mx=dot;} |
| 75 | float se=0;for(int s=0;s<sl;s++){sc[s]=expf(sc[s]-mx);se+=sc[s];}float is=1.0f/se; |
| 76 | memset(oh,0,hd*sizeof(float)); |
| 77 | for(int s=0;s<sl;s++){const float*vs_=vb+s*ks;float w=sc[s]*is;__m256 wv=_mm256_set1_ps(w);int d; |
| 78 | for(d=0;d<=hd-8;d+=8){__m256 vv=_mm256_loadu_ps(vs_+d); |
| 79 | __m256 ac=_mm256_loadu_ps(oh+d);_mm256_storeu_ps(oh+d,_mm256_fmadd_ps(wv,vv,ac));} |
| 80 | for(;d<hd;d++)oh[d]+=w*vs_[d];} |
| 81 | } |
| 82 | } |
| 83 | void process_layer( |
| 84 | int layer_id, float *x, |
| 85 | int L, int N, int NH, int NKH, int HD, int FF, float eps, |
| 86 | const uint8_t *w_q, const uint8_t *w_k, const uint8_t *w_v, |
| 87 | const uint8_t *w_o, const uint8_t *w_g, const uint8_t *w_u, const uint8_t *w_d, |
| 88 | const float *w_an, const float *w_fn, |
| 89 | int n_q, int n_k, int n_v, int n_o, int n_g, int n_u, int n_d, int nc, |
| 90 | int nc_down, |
| 91 | float *kv_k, float *kv_v, int *kv_len, int max_pos, |
| 92 | float *xn, float *res, float *q, float *k, float *v, |
| 93 | float *att, float *gate, float *up, float *silu_buf, |
| 94 | float *oproj, float *ffn |
| 95 | ) { |
| 96 | int pos = *kv_len; |
| 97 | memcpy(res, x, N * sizeof(float)); |
| 98 | rms(xn, x, w_an, N, eps); |
| 99 | fprintf(stderr, "L%d xn: %.6f %.6f\n", layer_id, xn[0], xn[1]); |
| 100 | int tq = n_q + n_k + n_v; |
| 101 | #pragma omp parallel for schedule(static) |
| 102 | for(int i=0;i<tq;i++){ |
| 103 | if(i<n_q) q[i]=q4d(w_q,xn,nc,i); |
| 104 | else if(i<n_q+n_k) k[i-n_q]=q4d(w_k,xn,nc,i-n_q); |
| 105 | else v[i-n_q-n_k]=q4d(w_v,xn,nc,i-n_q-n_k); |
| 106 | } |
| 107 | fprintf(stderr, "L%d q: %.6f %.6f\n", layer_id, q[0], q[1]); |
| 108 | for(int hh=0;hh<NH;hh++){float *qh=q+hh*HD; |
| 109 | for(int j=0;j<HD/2;j++){ |
| 110 | double ang=(double)pos/pow(10000.0,2.0*j/HD); |
| 111 | double cs=cos(ang),sn=sin(ang); |
| 112 | float a=qh[j],b=qh[j+HD/2];qh[j]=(float)(a*cs-b*sn);qh[j+HD/2]=(float)(b*cs+a*sn);}} |
| 113 | for(int hh=0;hh<NKH;hh++){float *kh=k+hh*HD; |
| 114 | for(int j=0;j<HD/2;j++){ |
| 115 | double ang=(double)pos/pow(10000.0,2.0*j/HD); |
| 116 | double cs=cos(ang),sn=sin(ang); |
| 117 | float a=kh[j],b=kh[j+HD/2];kh[j]=(float)(a*cs-b*sn);kh[j+HD/2]=(float)(b*cs+a*sn);}} |
| 118 | fprintf(stderr, "L%d q_rope: %.6f %.6f\n", layer_id, q[0], q[1]); |
| 119 | memcpy(kv_k + ((size_t)layer_id*max_pos+pos)*NKH*HD, k, NKH*HD*sizeof(float)); |
| 120 | memcpy(kv_v + ((size_t)layer_id*max_pos+pos)*NKH*HD, v, NKH*HD*sizeof(float)); |
| 121 | gqa(att, q, kv_k+(size_t)layer_id*max_pos*NKH*HD, kv_v+(size_t)layer_id*max_pos*NKH*HD, |
| 122 | pos+1, NH, NKH, HD); |
| 123 | (*kv_len)++; |
| 124 | #pragma omp parallel for schedule(static) |
| 125 | for(int i=0;i<n_o;i++) oproj[i]=q4d(w_o,att,nc,i); |
| 126 | for(int i=0;i<N;i++) x[i]=res[i]+oproj[i]; |
| 127 | fprintf(stderr, "L%d x_after_o: %.6f %.6f\n", layer_id, x[0], x[1]); |
| 128 | memcpy(res, x, N*sizeof(float)); |
| 129 | rms(xn, x, w_fn, N, eps); |
| 130 | int tg=n_g+n_u; |
| 131 | #pragma omp parallel for schedule(static) |
| 132 | for(int i=0;i<tg;i++){ |
| 133 | if(i<n_g) gate[i]=q4d(w_g,xn,nc,i); |
| 134 | else up[i-n_g]=q4d(w_u,xn,nc,i-n_g);} |
| 135 | fprintf(stderr, "L%d gate[0:3]: %.6f %.6f %.6f\n", layer_id, gate[0], gate[1], gate[2]); |
| 136 | for(int i=0;i<FF;i++){float g=gate[i];silu_buf[i]=(g/(1.0f+expf(-g)))*up[i];} |
| 137 | fprintf(stderr, "L%d silu[0:3]: %.6f %.6f %.6f\n", layer_id, silu_buf[0], silu_buf[1], silu_buf[2]); |
| 138 | #pragma omp parallel for schedule(static) |
| 139 | for(int i=0;i<n_d;i++) ffn[i]=q4d(w_d,silu_buf,nc_down,i); |
| 140 | fprintf(stderr, "L%d ffn[0:3]: %.6f %.6f %.6f\n", layer_id, ffn[0], ffn[1], ffn[2]); |
| 141 | for(int i=0;i<N;i++) x[i]=res[i]+ffn[i]; |
| 142 | fprintf(stderr, "L%d x_final: %.6f %.6f\n", layer_id, x[0], x[1]); |
| 143 | } |
| 144 | void process_final(float *x, int N, int HD, float eps, const float *onw, |
| 145 | const uint8_t *w_out, int out_nr, int out_nc, |
| 146 | float *xn, float *logits) { |
| 147 | rms(xn, x, onw, N, eps); |
| 148 | #pragma omp parallel for schedule(static) |
| 149 | for(int i=0;i<out_nr;i++) logits[i]=q8d(w_out,xn,out_nc,i); |
| 150 | } |