#include "ling3/linear.h" #include "ling3/cpu_kernels.h" #include "ling3/quantization.h" #include #include #include #include #include int main(int argc,char**argv)try{ if(argc!=2)throw std::runtime_error("usage: linear-bridge-check OFFICIAL.l3r"); setenv("LING3_OFFICIAL_EXECUTION","w8",1); setenv("LING3_KEEP_SOURCE_WEIGHTS","1",1); ling3::ModelPackage package(argv[1]); for(int lanes:{1,3}){ for(const std::string base:{"model.layers.1.mlp.experts.0.gate_up","model.layers.1.mlp.shared_experts.gate_up"}){ const auto&t=package.tensor(base+".weight");int k=t.entry->dims[0],n=t.entry->dims[1]; std::vector cores=lanes==1?std::vector{0}:std::vector{0,1,2}; auto target_cores=lanes==1?std::vector{1}:cores; ling3::Linear owner(package,base,{k,n,1,cores,3}), target(package,base,{k,n,1,target_cores,3}); std::vector x(128*k),y(128*n),scales(128),row(k); std::vector q(x.size()),wq(k); for(size_t i=0;i(x).subspan(r*k,k),std::span(q).subspan(r*k,k)).scale; auto verify=[&](int rows,std::spanindices){ for(int col:{0,1,15,31,63,n/2,n-2,n-1}){ float mx=0; for(int j=0;jdtype==unsigned(ling3::DataType::kBFloat16))row[j]=ling3::BFloat16ToFloat(reinterpret_cast(t.data)[col*k+j]); else{ const auto&s=package.tensor(base+".scales"); row[j]=ling3::DecodeInt4LowFirst({t.data,size_t(t.entry->data_bytes)},size_t(j)*n+col)* ling3::BFloat16ToFloat(reinterpret_cast(s.data)[col*(k/32)+j/32]); } mx=std::max(mx,std::abs(row[j])); } float ws=mx>0?mx/127:1;for(int j=0;j1e-5f*std::max(1.f,std::abs(expected)))throw std::runtime_error("bridge reference mismatch");} } }; for(int rows:{1,3,16,128,3}){ auto out=std::span(y).first(rows*n); owner.RunBatchWithWeights(std::span(x).first(rows*k),rows,target,out);verify(rows,{}); std::vectorindices(rows);for(int r=0;r(x).first(k)); target.RunPrepared(s,std::span(y).first(n));verify(1,{}); target.SetSingleCore(2);target.Run(std::span(x).first(k),std::span(y).first(n));verify(1,{}); } } } std::cout<<"PASS: group32/raw weights, dynamic rows, rebound weights, indexed inputs, shared A and core rebinding\n"; }catch(const std::exception&e){std::cerr<