/* Focused device/CPU DSpark differential. Build with hipcc, then pass libr4d.so. * This intentionally calls the real exported launchers, not the host SIMT emulator. * See docs/DSPARK-VALIDATION-WIP.md for a scoped invocation and its limitations. */ #include "rad_abi.h" #include "rad_plugin.h" #include #include #include #include #include #include #include #include #include using Fn = int (*)(const RadArgs*, RadStream); static void check(bool b, const char* s) { if (!b) { std::fprintf(stderr, "FAIL %s\n", s); std::exit(1); } } static void hipcheck(hipError_t e) { if (e != hipSuccess) { std::fprintf(stderr, "HIP: %s\n", hipGetErrorString(e)); std::exit(1); } } static uint16_t bf(float f) { uint32_t x; std::memcpy(&x, &f, 4); x += 0x7fff + ((x >> 16) & 1); return x >> 16; } static float fb(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; std::memcpy(&f, &x, 4); return f; } template struct Device { T* p = nullptr; size_t n; explicit Device(const std::vector& v): n(v.size()) { hipcheck(hipMalloc(&p, n*sizeof(T))); hipcheck(hipMemcpy(p, v.data(), n*sizeof(T), hipMemcpyHostToDevice)); } ~Device() { (void)hipFree(p); } std::vector get() { std::vector v(n); hipcheck(hipMemcpy(v.data(), p, n*sizeof(T), hipMemcpyDeviceToHost)); return v; } }; static RadTensor t(void* p, int dt, std::initializer_list sh) { RadTensor x{}; x.data=p; x.dtype=dt; x.rank=sh.size(); int i=0; for (auto d:sh) x.shape[i++]=d; rad_tensor_pack(&x); return x; } static RadParam pi(const char* k, long long v) { RadParam p{}; p.key=k; p.kind=RAD_P_INT; p.ival=v; return p; } static RadParam ps(const char* k, const char* v) { RadParam p{}; p.key=k; p.kind=RAD_P_STR; p.sval=v; return p; } static Fn symbol(void* h, const char* name) { auto f = reinterpret_cast(dlsym(h,name)); check(f != nullptr, name); return f; } static void markov(Fn fn, int R) { constexpr int M=2, S=2, V=130, T=3, LP=137, SP=5, TP=7, OP=4; std::vector l(M*S*LP,bf(0)), p(V*R,bf(0)), u(V*R,bf(0)); for (int m=0;m anchor{1,-1,2,-1}, ti(M*TP,-777), out(M*OP,-777); std::vector ts(M*SP,-777.f); Device dl(l), dp(p), du(u); Device da(anchor), di(ti), dout(out); Device ds(ts); RadTensor a[]={t(dl.p,RAD_BF16,{M*S,V}),t(da.p,RAD_I32,{M,2}),t(dp.p,RAD_BF16,{V,R}),t(du.p,RAD_BF16,{V,R}),t(ds.p,RAD_F32,{M,T}),t(di.p,RAD_I32,{M,T}),t(dout.p,RAD_I32,{M,S})}; a[0].stride[0]=LP; a[4].stride[0]=SP; a[5].stride[0]=TP; a[6].stride[0]=OP; RadParam par[]={pi("M",M),pi("steps",S),pi("rank",R),pi("n_vocab",V),pi("anchor_stride",2),ps("dtype","bf16")}; RadArgs args{}; args.t=a; args.n_t=7; args.p=par; args.n_p=6; check(fn(&args,nullptr)==RAD_OK,"markov launch"); hipcheck(hipDeviceSynchronize()); auto go=dout.get(); auto gs=ds.get(); auto gi=di.get(); // Independent scalar oracle. Values are deliberately exactly representable so reduction order // cannot explain a discrepancy. Sequential conditioning and tile winners are both checked. for(int m=0;mbest) { best=score; winner=v; } } check(go[m*OP+s]==winner,"markov CPU winner"); prev=winner; } for(int tile=0;tilebest) {best=score; win=v;} } check(gs[m*SP+tile]==best && gi[m*TP+tile]==win,"markov pitched tile oracle"); } for(int i=T;i l{bf(1.001f),bf(1.002f)}, w(V*R,bf(0)); std::vector anchor{0}, token{-1}, tile{-1}; std::vector score{-1}; check(l[0]==l[1],"near tie quantizes identically"); Device dl(l), dw(w); Device da(anchor), dt(tile), dout(token); Device ds(score); RadTensor a[]={t(dl.p,RAD_BF16,{1,V}),t(da.p,RAD_I32,{1}),t(dw.p,RAD_BF16,{V,R}),t(dw.p,RAD_BF16,{V,R}),t(ds.p,RAD_F32,{1,1}),t(dt.p,RAD_I32,{1,1}),t(dout.p,RAD_I32,{1,1})}; RadParam par[]={pi("steps",1),pi("rank",R),pi("n_vocab",V),pi("anchor_stride",1)}; RadArgs args{}; args.t=a; args.n_t=7; args.p=par; args.n_p=4; check(fn(&args,nullptr)==RAD_OK,"near tie launch"); hipcheck(hipDeviceSynchronize()); check(dout.get()[0]==0 && 1.002f>1.001f,"BF16/F32 near tie divergence"); std::puts("near tie: F32 argmax=1, BF16 input argmax=0: PASS (synthetic)"); } static void attention(Fn shared, Fn ordinary, int G, int ctx0, int ctx1, int causal) { constexpr int M=2, Q=7, H=256, BS=16, W=513; const int ctx[M]={ctx0,ctx1}, blocks=(std::max(ctx0,ctx1)+15)/16, nblocks=M*blocks; std::vector q(M*Q*G*H,bf(0)), kv((size_t)nblocks*BS*2*H,bf(0)), out(M*Q*G*H,bf(0)); std::vector bt(M*blocks), su{ctx0,ctx1}, cu{0,Q,2*Q}; // Reverse physical pages: the oracle must consult block_table rather than logical positions. for(int s=0;s dq(q), dk(kv), dout(out); Device db(bt), ds(su), dc(cu); RadTensor a[]={t(dq.p,RAD_BF16,{M*Q,G,H}),t(dk.p,RAD_BF16,{nblocks,1,BS,2*H}), t(db.p,RAD_I32,{M,blocks}),t(ds.p,RAD_I32,{M}),RadTensor{},RadTensor{}, t(dc.p,RAD_I32,{M+1}),t(dout.p,RAD_BF16,{M*Q,G,H})}; RadParam par[]={pi("q_len",Q),pi("head_dim",H),pi("gqa",G),pi("block_size",BS), pi("causal",causal),pi("window",W),pi("n_head",G),pi("max_seqs",M), ps("q_dtype","bf16"),ps("kv_dtype","bf16")}; RadArgs args{}; args.t=a; args.n_t=8; args.p=par; args.n_p=10; check(shared(&args,nullptr)==RAD_OK,"shared anchor attention launch"); hipcheck(hipDeviceSynchronize()); auto got=dout.get(); std::vector expected(M*Q*G); for(int s=0;s0.03) distinguished=true; check(distinguished,"attention per-row window negative control"); std::printf("attention gqa=%d ctx=%d,%d causal=%d shared oracle and negative control: PASS\n",G,ctx0,ctx1,causal); } int main(int argc,char** argv) { check(argc==2,"usage: dspark_device_test libr4d.so"); void* h=dlopen(argv[1],RTLD_NOW|RTLD_LOCAL); if(!h) { std::fprintf(stderr,"dlopen: %s\n",dlerror()); return 1; } Fn fn=symbol(h,"r4d_rad_dflash_markov_greedy"); for(int r:{32,64,128,512}) markov(fn,r); near_tie(fn); for(int g:{1,2}) { const char* shared=g==1?"r4d_rad_attn_anchor_window_h256_gqa1_bf16kv":"r4d_rad_attn_anchor_window_h256_gqa2_bf16kv"; const char* ordinary=g==1?"r4d_rad_attn_window_h256_gqa1_bf16":"r4d_rad_attn_window_h256_gqa2_bf16"; for(int causal:{0,1}) { attention(symbol(h,shared),symbol(h,ordinary),g,520,1049,causal); attention(symbol(h,shared),symbol(h,ordinary),g,521,1050,causal); } } dlclose(h); std::puts("DSpark device differential: PASS"); }