Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 145 additions & 1 deletion engine/metal/poly_tests/mv_bench.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// metal/poly_tests/mv_bench.cpp - one token's quantized products (poly_mul) on the shapes of a decode step: the experts
// of a token (8 of them, by expert ids) and a dense projection, for every type poly_mul has. Not a test: run by hand.
// poly_mv_bench [reps (at least; enough for 20 ms a column), default 200 [types "8,22,19,..." [shapes inkling|qwen|sweep|dense|prompt|many,
// poly_mv_bench [reps (at least; enough for 20 ms a column), default 200 [types "8,22,19,..." [shapes inkling|qwen|qwen4|sweep|dense|prompt|many,
// default inkling and qwen [rounds, default 5]]]]
// inkling: Inkling 2 Mini (2,304 wide, experts of 2,304 x 1,152 with their gate and up in one call, downs of 1,152);
// qwen: Qwen3.6-35B-A3B (2,048 wide, experts of 2,048 x 512). Each call reads other experts or another copy of the
Expand Down Expand Up @@ -46,6 +46,150 @@ int main(int argc,char** argv){
const std::string source=blocks+"#define MV_DEQ\n"+md::read_source(STRATA_POLY_QUANT_SOURCE);
const std::string old_source=old?blocks+"#define NVFP4_ROWS 4\n"+md::read_source(old):"";
g::Stream s;ck(g::stream_create(&s));g::metal::set_optimize(true);
if(only=="qwen4"){
auto make_quant_rows=[](int type,int ni,int no){
const size_t rb=k::native_mmvq_weight_bytes(type,ni,1);
const int sample=std::min(no,512);
std::vector<unsigned char> head(rb*sample);
if(type==40){
std::mt19937 rng(40);
for(size_t i=0;i<head.size();++i) head[i]=uint8_t(i%36<4?0x30+rng()%8:rng());
}else{
auto gt=ggml_type(type);ggml_quantize_init(gt);
std::vector<float> wf(size_t(sample)*ni);
for(size_t i=0;i<wf.size();++i) wf[i]=0.4f*std::sin(float(i)*0.37f+0.13f);
std::vector<float> imp(ni,1.0f);
ggml_quantize_chunk(gt,wf.data(),head.data(),0,sample,ni,imp.data());
}
std::vector<unsigned char> out(rb*no);
for(int r=0;r<no;++r) std::memcpy(out.data()+size_t(r)*rb,head.data()+size_t(r%sample)*rb,rb);
return out;
};
auto want_type=[&](int t){
return argc<=2 || !argv[2][0] || std::find(types.begin(),types.end(),t)!=types.end();
};
struct Q4Dense{int type;int ni,no,rows;};
const Q4Dense dense_cases[]={
{13,2048,6144,1},{13,2048,6144,2},{13,2048,6144,4},
{13,2048,12288,1},{13,2048,12288,2},
{14,2048,248320,1},{14,2048,248320,2},
{14,2048,4096,1},{14,2048,4096,2},
{14,4096,2048,1},{14,4096,2048,2},
{12,2048,6144,1},{12,2048,6144,2},{12,2048,6144,4},
{12,2048,248320,1},{12,2048,248320,2},
};
std::printf("%-10s %-20s %8s %8s %9s %9s %7s\n",
"type","shape","opt0 us","opt1 us","opt0 GB/s","opt1 GB/s","check");
for(const Q4Dense& c:dense_cases){
if(!want_type(c.type)) continue;
const size_t per=k::native_mmvq_weight_bytes(c.type,c.ni,c.no);
// Target ~40 MiB across copies to flush M4 SLC; keep copies=1 for large LM head (>64 MiB).
const int copies=per>(64ull<<20)?1:std::clamp<int>(int((40ull<<20)/std::max<size_t>(per,1)),2,16);
auto one=make_quant_rows(c.type,c.ni,c.no);
Buffer dw(per*copies);
for(int e=0;e<copies;++e) ck(g::memcpy_sync(static_cast<unsigned char*>(dw.ptr)+per*e,one.data(),per,g::kHostToDevice));
Buffer dx(values(size_t(c.rows)*c.ni)),q(k::native_q8_1_bytes(c.ni,c.rows)),y(size_t(c.rows)*c.no*4);
k::native_quantize_q8_1(dx.as(),q.ptr,c.ni,c.rows,s);
ck(g::stream_synchronize(s));
auto run_one=[&](bool opt,int i){
g::metal::set_optimize(opt);
const void* w=static_cast<unsigned char*>(dw.ptr)+per*(i%copies);
k::native_mmvq(c.type,w,q.ptr,y.as(),c.ni,c.no,c.rows,s);
};
// Warmup & parity check opt=0 vs opt=1
run_one(false,0);ck(g::stream_synchronize(s));
auto ref=y.read(size_t(c.rows)*c.no);
run_one(true,0);ck(g::stream_synchronize(s));
auto got=y.read(size_t(c.rows)*c.no);
float top=0,mdiff=0;
for(size_t i=0;i<ref.size();++i){top=std::max(top,std::fabs(ref[i]));mdiff=std::max(mdiff,std::fabs(got[i]-ref[i]));}
const bool ok=std::isfinite(mdiff) && mdiff<=1e-3f*(1.0f+top);
// Calibrate reps to >=25 ms
int reps=std::max(10,std::min(reps0,200));
const auto t0=std::chrono::steady_clock::now();
for(int i=0;i<reps;++i) run_one(true,i);
ck(g::stream_synchronize(s));
const double us0=std::chrono::duration<double,std::micro>(std::chrono::steady_clock::now()-t0).count();
if(us0<25000) reps=std::min(kMaxReps,int(reps*25000.0/std::max(us0,1.0))+1);
else if(us0>100000) reps=std::max(8,int(reps*40000.0/us0));
double best[2]={1e30,1e30};
for(int r=0;r<rounds;++r) for(int v=0;v<2;++v){
run_one(v!=0,0);ck(g::stream_synchronize(s));
const auto a0=std::chrono::steady_clock::now();
for(int i=1;i<=reps;++i) run_one(v!=0,i);
ck(g::stream_synchronize(s));
best[v]=std::min(best[v],std::chrono::duration<double,std::micro>(std::chrono::steady_clock::now()-a0).count()/reps);
}
const double bytes=double(per)+double(k::native_q8_1_bytes(c.ni,c.rows))+4.0*double(c.no)*c.rows;
char sh[32];std::snprintf(sh,sizeof sh,"%d->%d r=%d",c.ni,c.no,c.rows);
std::printf("%-10s %-20s %8.1f %8.1f %9.2f %9.2f %7s\n",
ggml_type_name(ggml_type(c.type)),sh,best[0],best[1],
bytes/best[0]/1e3,bytes/best[1]/1e3,ok?"OK":"FAIL");
std::fflush(stdout);
}
// MoE k1 (count=8, [512->2048] down & [2048->512] gate/up)
std::printf("\n%-10s %-20s %8s %8s %9s %9s %7s\n",
"type","moe shape","opt0 us","opt1 us","opt0 GB/s","opt1 GB/s","check");
for(int type:{17,22,18,12}){
if(!want_type(type)) continue;
struct MoeCase{const char* tag;int ni,no,count;};
const MoeCase mcs[]={
{"k1 512->2048 c=8",512,2048,8},
{"k1 2048->512 c=8",2048,512,8},
};
for(const MoeCase& mc:mcs){
const size_t per=k::native_mmvq_weight_bytes(type,mc.ni,mc.no);
const int copies=std::clamp<int>(int((40ull<<20)/std::max<size_t>(per,1)),16,128);
auto one=make_quant_rows(type,mc.ni,mc.no);
Buffer dw(per*copies);
for(int e=0;e<copies;++e) ck(g::memcpy_sync(static_cast<unsigned char*>(dw.ptr)+per*e,one.data(),per,g::kHostToDevice));
std::vector<const void*> table(copies);
for(int e=0;e<copies;++e) table[e]=static_cast<unsigned char*>(dw.ptr)+per*e;
Buffer tab(copies*sizeof(void*)),di(size_t(kMaxReps+2)*mc.count*4);
tab.put(table.data(),copies*sizeof(void*));
std::vector<int32_t> ids(size_t(kMaxReps+2)*mc.count);
for(size_t i=0;i<ids.size();++i) ids[i]=int((i*13+(i/mc.count)*7)%copies);
di.put(ids.data(),ids.size()*4);
Buffer dx(values(size_t(mc.count)*mc.ni)),q(k::native_q8_1_bytes(mc.ni,mc.count));
k::native_quantize_q8_1(dx.as(),q.ptr,mc.ni,mc.count,s);
Buffer y(size_t(mc.count)*mc.no*4);
ck(g::stream_synchronize(s));
auto run_moe=[&](bool opt,int i){
g::metal::set_optimize(opt);
k::MmvqIds a{tab.as<const void*>(),di.as<int32_t>()+size_t(i)*mc.count,0,q.ptr,
k::native_q8_1_bytes(mc.ni),1,y.as(),size_t(mc.no)};
k::native_mmvq_ids(type,a,mc.count,mc.ni,mc.no,s);
};
run_moe(false,0);ck(g::stream_synchronize(s));
auto ref=y.read(size_t(mc.count)*mc.no);
run_moe(true,0);ck(g::stream_synchronize(s));
auto got=y.read(size_t(mc.count)*mc.no);
float top=0,mdiff=0;
for(size_t i=0;i<ref.size();++i){top=std::max(top,std::fabs(ref[i]));mdiff=std::max(mdiff,std::fabs(got[i]-ref[i]));}
const bool ok=std::isfinite(mdiff) && mdiff<=1e-3f*(1.0f+top);
int reps=std::max(50,reps0);
const auto t0=std::chrono::steady_clock::now();
for(int i=0;i<reps;++i) run_moe(true,i);
ck(g::stream_synchronize(s));
const double us0=std::chrono::duration<double,std::micro>(std::chrono::steady_clock::now()-t0).count();
if(us0<25000) reps=std::min(kMaxReps,int(reps*25000.0/std::max(us0,1.0))+1);
double best[2]={1e30,1e30};
for(int r=0;r<rounds;++r) for(int v=0;v<2;++v){
run_moe(v!=0,0);ck(g::stream_synchronize(s));
const auto a0=std::chrono::steady_clock::now();
for(int i=1;i<=reps;++i) run_moe(v!=0,i);
ck(g::stream_synchronize(s));
best[v]=std::min(best[v],std::chrono::duration<double,std::micro>(std::chrono::steady_clock::now()-a0).count()/reps);
}
const double bytes=double(per)*mc.count+double(k::native_q8_1_bytes(mc.ni,mc.count))+4.0*double(mc.no)*mc.count;
std::printf("%-10s %-20s %8.1f %8.1f %9.2f %9.2f %7s\n",
ggml_type_name(ggml_type(type)),mc.tag,best[0],best[1],
bytes/best[0]/1e3,bytes/best[1]/1e3,ok?"OK":"FAIL");
std::fflush(stdout);
}
}
ck(g::stream_destroy(s));ggml_quantize_free();return 0;
}
struct Shape{const char* model;const char* name;int ni,no,used,copies,xr=1;}; // used 0: a dense matrix; xr: the rows of x
const Shape ink[]={{"inkling","experts gate|up",2304,2304,8,64},{"inkling","experts down",1152,2304,8,64},
{"inkling","dense 2304->4096",2304,4096,0,32},{"inkling","dense 4096->2304",4096,2304,0,32}};
Expand Down
17 changes: 11 additions & 6 deletions engine/metal/poly_tests/mv_parity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ int main(int argc,char** argv){
}
}
}
// native_mmvq_many: five matrices of a type (Inkling 2's q, k, v, relative positions' and gate's outputs, and a
// launch of fewer outputs), three inputs among them, in one launch, against the five native_mmvq() calls: the same
// bits. Q8_0 also at Inkling 2's width.
for(int type:types)for(int ni:{256,2304})for(bool big:{true,false}){
// native_mmvq_many: five matrices of a type (Inkling 2's q, k, v, relative positions' and gate's outputs, a launch
// of fewer outputs, and an unaligned-tail launch crossing NR=2/NR=4 thresholds), three inputs among them, in one
// launch, against the five native_mmvq() calls: the same bits. Q8_0 also at Inkling 2's width.
for(int type:types)for(int ni:{256,2304})for(int mode:{0,1,2}){
if(ni%int(ggml_blck_size(ggml_type(type))) || (ni==2304 && type!=8))continue;
const int outs[5]={big?3072:96,big?1024:32,big?1024:32,big?384:12,big?3072:96},from[5]={0,1,2,0,0};
if(mode==2 && ni==2304)continue;
const int outs[5]={mode==0?3072:(mode==1?96:2052), mode==0?1024:(mode==1?32:516),
mode==0?1024:(mode==1?32:13), mode==0?384:(mode==1?12:7),
mode==0?3072:(mode==1?96:1)};
const int from[5]={0,1,2,0,0};
const int total_out=outs[0]+outs[1]+outs[2]+outs[3]+outs[4];
k::NativeMmvqMany m{};std::vector<std::unique_ptr<Buffer>> keep;
auto xs=values(3*ni,.8f);Buffer dx(xs),qx(3*k::native_q8_1_bytes(ni));
k::native_quantize_q8_1(dx.as(),qx.ptr,ni,3,stream);
Expand All @@ -97,7 +102,7 @@ int main(int argc,char** argv){
for(int j=0;j<5;++j){auto got=ys[j]->read(outs[j]),want=refs[j]->read(outs[j]);n+=got.size();
for(int i=0;i<outs[j];++i)differ+=std::memcmp(&got[i],&want[i],4)!=0;}
std::printf("%s many, %d wide, %d outputs: %d of %zu outputs differ in their bits from native_mmvq's\n",
ggml_type_name(ggml_type(type)),ni,big?8576:268,differ,n);
ggml_type_name(ggml_type(type)),ni,total_out,differ,n);
bad+=differ;
}
p::metal_detail::allow_mm_rows(true);
Expand Down
Loading
Loading