--!native -- SmolLM2-135M-Instruct forward pass. CPU inference is entirely Luau. local Encoding = game:GetService('EncodingService') local Http = game:GetService('HttpService') local Engine = {} local D,H,KV,HD,FF,LAYERS,VOCAB = 576,9,3,64,1536,30,49152 local EPS,THETA = 1e-5,100000 local rd,wr,ri = buffer.readf32,buffer.writef32,buffer.readi8 local function vec(n) return buffer.create(n*4) end local lastYield=os.clock() local cancelCheck=nil local function checkpoint(force) if force or os.clock()-lastYield>0.012 then task.wait();lastYield=os.clock() if cancelCheck and cancelCheck() then error('CANCELLED',0) end end end local function matvec(w,x,out) local n,m,b=w.rows,w.cols,w.data local so=n*m for r=0,n-1 do local a=r*m;local s=0 for g=0,m/64-1 do local partial=0;local start=g*64 for c=start,start+63,4 do partial+=ri(b,a+c)*rd(x,c*4)+ri(b,a+c+1)*rd(x,(c+1)*4)+ri(b,a+c+2)*rd(x,(c+2)*4)+ri(b,a+c+3)*rd(x,(c+3)*4) end s+=partial*rd(b,so+(r*m/64+g)*4) end wr(out,r*4,s) if r%128==127 then checkpoint(false) end end end local function norm(x,w,out) local s=0 for i=0,D-1 do local v=rd(x,i*4);s+=v*v end local inv=1/math.sqrt(s/D+EPS) for i=0,D-1 do wr(out,i*4,rd(x,i*4)*inv*rd(w.data,i*4)) end end local function add(x,b) for i=0,D-1 do wr(x,i*4,rd(x,i*4)+rd(b,i*4)) end end function Engine.load(folder,onProgress) local manifest=Http:JSONDecode(folder.Manifest.Value) local weights={};local names={} for name in manifest.tensors do table.insert(names,name) end table.sort(names) for index,name in names do local meta=manifest.tensors[name];local node=folder[meta.key] local chunks=node:GetChildren();table.sort(chunks,function(a,b) return a.Name0 then score/1.08 else score*1.08 end if id==0 or id==1 or (id>=3 and id<=16) then score=-math.huge end if #best<32 or score>best[#best][2] then local k=#best+1 while k>1 and best[k-1][2]32 then best[33]=nil end end end if temperature==0 then return best[1][1] end local sum=0 for _,v in best do v[3]=math.exp((v[2]-best[1][2])/temperature);sum+=v[3] end local z=rng:NextNumber()*sum for _,v in best do z-=v[3];if z<=0 then return v[1] end end return best[1][1] end return Engine