okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1// src/core/dflash2_parity.cpp - the engine's DFlash2 drafter against the torch port, on a synthetic case (GPU, no model). 2// 3// dflash2_parity <pack> <case.bin> [rounds] 4// 5// case.bin comes from tools/dflash2/mk_parity_case.py: n_ctx random tap rows, an anchor, a random Q8_0 head over a draft-vocabulary subset, the 6// target embedding rows of the anchor and the mask token, and the torch answer (tokens, probabilities, the 8 final normalized rows). The engine's 7// drafter reads the same pack (Q8_0 weights, Q8_1 activations), so the rows agree to quantization noise, not bitwise: the test checks the rows' 8// cosine similarity, the walk's tokens and the time of a round. 9#include "strata/core/dflash2.hpp" 10 11#include <cuda_runtime.h> 12 13#include <chrono> 14#include <cmath> 15#include <cstdint> 16#include <cstdio> 17#include <cstdlib> 18#include <vector> 19 20using strata::core::Dflash2Drafter ; 21 22int main (int argc ,char ** argv ) { 23if (argc < 3 ) { std::fprintf (stderr ,"usage: dflash2_parity <pack> <case.bin> [rounds]\n" );return 2 ; } 24const int rounds = argc > 3 ? std::atoi (argv [3 ]) :20 ; 25 std::FILE * f = std::fopen (argv [2 ],"rb" ); 26if (!f ) { std::fprintf (stderr ,"cannot open %s\n" ,argv [2 ]);return 2 ; } 27int32_t hdr [4 ]; 28if (std::fread (hdr ,4 ,4 ,f )!= 4 )return 2 ; 29const int n_ctx = hdr [0 ],anchor = hdr [1 ],vp = hdr [2 ]; 30const int H = 2560 ,tapn = 5 * H ; 31 std::vector < float > taps ((size_t )n_ctx * tapn ),emb (2 * H ),fn_ref (8 * H ); 32 std::vector < int32_t > dvocab (vp ),tok_ref (7 ); 33 std::vector < float > prob_ref (7 ); 34const size_t row_bytes = (size_t ) (H /32 )* 34 ; 35 std::vector < uint8_t > head ((size_t )vp * row_bytes ); 36bool ok = std::fread (taps .data (),4 ,taps .size (),f )== taps .size ()&& std::fread (emb .data (),4 ,emb .size (),f )== emb .size ()&& 37 std::fread (dvocab .data (),4 ,dvocab .size (),f )== dvocab .size ()&& std::fread (head .data (),1 ,head .size (),f )== head .size ()&& 38 std::fread (tok_ref .data (),4 ,7 ,f )== 7 && std::fread (prob_ref .data (),4 ,7 ,f )== 7 && std::fread (fn_ref .data (),4 ,fn_ref .size (),f )== fn_ref .size (); 39 std::fclose (f ); 40if (!ok ) { std::fprintf (stderr ,"case file is cut short\n" );return 2 ; } 41 42 std::string err ; 43Dflash2Drafter d ; 44if (!d .load (argv [1 ],0 ,err )) { std::fprintf (stderr ,"load: %s\n" ,err .c_str ());return 1 ; } 45float * d_taps = nullptr ,* d_emb = nullptr ; 46uint8_t * d_head = nullptr ; 47cudaMalloc ((void ** )& d_taps ,taps .size ()* 4 ); 48cudaMalloc ((void ** )& d_emb ,emb .size ()* 4 ); 49cudaMalloc ((void ** )& d_head ,head .size ()); 50cudaMemcpy (d_taps ,taps .data (),taps .size ()* 4 ,cudaMemcpyHostToDevice ); 51cudaMemcpy (d_emb ,emb .data (),emb .size ()* 4 ,cudaMemcpyHostToDevice ); 52cudaMemcpy (d_head ,head .data (),head .size (),cudaMemcpyHostToDevice ); 53auto embed = [& ](const int32_t * ,int n ,float * out ,cudaStream_t cs ) { 54cudaMemcpyAsync (out ,d_emb , (size_t )H * 4 ,cudaMemcpyDeviceToDevice ,cs ); 55for (int i = 1 ;i < n ;++ i )cudaMemcpyAsync (out + (size_t )i * H ,d_emb + H , (size_t )H * 4 ,cudaMemcpyDeviceToDevice ,cs ); 56 }; 57if (!d .bind_raw (embed ,d_head ,8 ,vp ,dvocab ,err )) { std::fprintf (stderr ,"bind: %s\n" ,err .c_str ());return 1 ; } 58 std::printf ("drafter VRAM %.0f MiB\n" , (double )d .vram_bytes () /1048576.0 ); 59 60// the context but the last row, then one draft round that adds the last row 61if (!d .context (d_taps ,n_ctx - 1 ,0 ,err )) { std::fprintf (stderr ,"context: %s\n" ,err .c_str ());return 1 ; } 62int32_t drafts [7 ]; 63float probs [7 ]; 64const float * last = d_taps + (size_t ) (n_ctx - 1 )* tapn ; 65if (!d .draft (last ,1 ,n_ctx - 1 ,anchor ,drafts ,probs ,err )) { std::fprintf (stderr ,"draft: %s\n" ,err .c_str ());return 1 ; } 66 std::vector < float > rows ; 67d .debug_rows (rows ,err ); 68int bad = 0 ; 69double min_cos = 1.0 ; 70for (int r = 0 ;r < 8 ;++ r ) { 71double dot = 0 ,na = 0 ,nb = 0 ; 72for (int c = 0 ;c < H ;++ c ) { 73const double a = rows [(size_t )r * H + c ],b = fn_ref [(size_t )r * H + c ]; 74dot += a * b ;na += a * a ;nb += b * b ; 75 } 76const double cs = dot / std::sqrt (na * nb + 1e-30 ); 77min_cos = std::min (min_cos ,cs ); 78 std::printf ("row %d: cos %.5f norm ratio %.4f\n" ,r ,cs , std::sqrt (na / (nb + 1e-30 ))); 79 } 80int same = 0 ; 81for (int i = 0 ;i < 7 ;++ i ) { 82same += drafts [i ]== tok_ref [i ]; 83 std::printf ("draft %d: engine %d (p %.3f) torch %d (p %.3f)\n" ,i ,drafts [i ],probs [i ],tok_ref [i ],prob_ref [i ]); 84 } 85if (min_cos < 0.98 ) { std::printf ("FAIL: a final row's cosine similarity %.4f is below 0.98\n" ,min_cos );++ bad ; } 86// timing: the same round again (the graph replays; the ring rewrites the same slot) 87cudaDeviceSynchronize (); 88const auto t0 = std::chrono::steady_clock::now (); 89for (int i = 0 ;i < rounds ;++ i ) 90if (!d .draft (last ,1 ,n_ctx - 1 ,anchor ,drafts ,probs ,err )) { std::fprintf (stderr ,"draft: %s\n" ,err .c_str ());return 1 ; } 91const double ms = std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count () /rounds ; 92 std::printf ("tokens equal %d of 7; %.3f ms per round (1 context row + the block), GPU %.3f ms\n" ,same ,ms ,d .rounds > 0 ?d .ms_gpu / (double )d .rounds :0.0 ); 93 std::printf (bad ?"dflash2_parity: FAIL\n" :"dflash2_parity: ok\n" ); 94return bad ?1 :0 ; 95}