@octomil/browser 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/cache.d.ts +25 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +202 -0
- package/dist/cache.js.map +1 -0
- package/dist/device-auth.d.ts +41 -0
- package/dist/device-auth.d.ts.map +1 -0
- package/dist/device-auth.js +203 -0
- package/dist/device-auth.js.map +1 -0
- package/dist/experiments.d.ts +44 -0
- package/dist/experiments.d.ts.map +1 -0
- package/dist/experiments.js +135 -0
- package/dist/experiments.js.map +1 -0
- package/dist/federated.d.ts +53 -0
- package/dist/federated.d.ts.map +1 -0
- package/dist/federated.js +180 -0
- package/dist/federated.js.map +1 -0
- package/dist/index.cjs +2148 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/inference.d.ts +43 -0
- package/dist/inference.d.ts.map +1 -0
- package/dist/inference.js +213 -0
- package/dist/inference.js.map +1 -0
- package/dist/integrity.d.ts +19 -0
- package/dist/integrity.d.ts.map +1 -0
- package/dist/integrity.js +35 -0
- package/dist/integrity.js.map +1 -0
- package/dist/model-loader.d.ts +40 -0
- package/dist/model-loader.d.ts.map +1 -0
- package/dist/model-loader.js +232 -0
- package/dist/model-loader.js.map +1 -0
- package/dist/octomil.d.ts +92 -0
- package/dist/octomil.d.ts.map +1 -0
- package/dist/octomil.js +368 -0
- package/dist/octomil.js.map +1 -0
- package/dist/octomil.min.js +2849 -0
- package/dist/octomil.min.js.map +7 -0
- package/dist/privacy.d.ts +40 -0
- package/dist/privacy.d.ts.map +1 -0
- package/dist/privacy.js +118 -0
- package/dist/privacy.js.map +1 -0
- package/dist/rollouts.d.ts +43 -0
- package/dist/rollouts.d.ts.map +1 -0
- package/dist/rollouts.js +114 -0
- package/dist/rollouts.js.map +1 -0
- package/dist/secure-aggregation.d.ts +50 -0
- package/dist/secure-aggregation.d.ts.map +1 -0
- package/dist/secure-aggregation.js +174 -0
- package/dist/secure-aggregation.js.map +1 -0
- package/dist/streaming.d.ts +25 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +148 -0
- package/dist/streaming.js.map +1 -0
- package/dist/telemetry.d.ts +41 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +130 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/types.d.ts +239 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +17 -0
- package/dist/types.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,2849 @@
|
|
|
1
|
+
"use strict";var __Octomil_ns=(()=>{var Ur=Object.defineProperty;var yy=Object.getOwnPropertyDescriptor;var wy=Object.getOwnPropertyNames;var _y=Object.prototype.hasOwnProperty;var by=(e,t,r)=>t in e?Ur(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var It=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var vy=(e,t)=>()=>(e&&(t=e(e=0)),t);var zo=(e,t)=>{for(var r in t)Ur(e,r,{get:t[r],enumerable:!0})},$y=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of wy(t))!_y.call(e,n)&&n!==r&&Ur(e,n,{get:()=>t[n],enumerable:!(i=yy(t,n))||i.enumerable});return e};var xy=e=>$y(Ur({},"__esModule",{value:!0}),e);var Q=(e,t,r)=>by(e,typeof t!="symbol"?t+"":t,r);var jf={};zo(jf,{InferenceSession:()=>na,TRACE:()=>xr,TRACE_EVENT_BEGIN:()=>_t,TRACE_EVENT_END:()=>bt,TRACE_FUNC_BEGIN:()=>Je,TRACE_FUNC_END:()=>We,Tensor:()=>Xe,default:()=>H0,env:()=>be,registerBackend:()=>Bt});async function Oo(e={}){var t=e,r=!!globalThis.window,i=!!globalThis.WorkerGlobalScope,n=i&&self.name?.startsWith("em-pthread");t.mountExternalData=(o,p)=>{o.startsWith("./")&&(o=o.substring(2)),(t.Zc||(t.Zc=new Map)).set(o,p)},t.unmountExternalData=()=>{delete t.Zc},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,ae:!0}).buffer.constructor;let a=o=>async(...p)=>{try{if(t.$c)throw Error("Session already started");let m=t.$c={Nd:p[0],errors:[]},c=await o(...p);if(t.$c!==m)throw Error("Session mismatch");t.gd?.flush();let b=m.errors;if(0<b.length){let S=await Promise.all(b);if(S=S.filter(z=>z),0<S.length)throw Error(S.join(`
|
|
2
|
+
`))}return c}finally{t.$c=null}};t.jsepInit=(o,p)=>{if(o==="webgpu"){[t.gd,t.Dd,t.Hd,t.jd,t.Gd,t.ac,t.Id,t.Kd,t.Ed,t.Fd,t.Jd]=p;let m=t.gd;t.jsepRegisterBuffer=(c,b,S,z)=>m.registerBuffer(c,b,S,z),t.jsepGetBuffer=c=>m.getBuffer(c),t.jsepCreateDownloader=(c,b,S)=>m.createDownloader(c,b,S),t.jsepOnCreateSession=c=>{m.onCreateSession(c)},t.jsepOnReleaseSession=c=>{m.onReleaseSession(c)},t.jsepOnRunStart=c=>m.onRunStart(c),t.Ld=(c,b)=>{m.upload(c,b)}}else if(o==="webnn"){let m=p[0];[t.Zd,t.vd,t.webnnEnsureTensor,t.xd,t.webnnDownloadTensor,t.Yd,t.webnnEnableTraceEvent]=p.slice(1),t.webnnReleaseTensorId=t.vd,t.webnnUploadTensor=t.xd,t.webnnRegisterMLContext=t.Yd,t.webnnOnRunStart=c=>m.onRunStart(c),t.webnnOnRunEnd=m.onRunEnd.bind(m),t.webnnOnReleaseSession=c=>{m.onReleaseSession(c)},t.webnnCreateMLTensorDownloader=(c,b)=>m.createMLTensorDownloader(c,b),t.webnnRegisterMLTensor=(c,b,S,z)=>m.registerMLTensor(c,b,S,z),t.webnnCreateMLContext=c=>m.createMLContext(c),t.webnnRegisterMLConstant=(c,b,S,z,M,W)=>m.registerMLConstant(c,b,S,z,M,t.Zc,W),t.webnnRegisterGraphInput=m.registerGraphInput.bind(m),t.webnnIsGraphInput=m.isGraphInput.bind(m),t.webnnRegisterGraphOutput=m.registerGraphOutput.bind(m),t.webnnIsGraphOutput=m.isGraphOutput.bind(m),t.webnnCreateTemporaryTensor=m.createTemporaryTensor.bind(m),t.webnnIsGraphInputOutputTypeSupported=m.isGraphInputOutputTypeSupported.bind(m)}};let s=()=>{let o=p=>(...m)=>{let c=tt;return m=p(...m),tt!=c?new Promise((b,S)=>{Ci={resolve:b,reject:S}}):m};(()=>{for(let p of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[p]=o(t[p])})(),a!==void 0&&(t._OrtRun=a(t._OrtRun),t._OrtRunWithBinding=a(t._OrtRunWithBinding)),s=void 0};t.asyncInit=()=>{s?.()};var u,l,d=(o,p)=>{throw p},f=Ye.url,h="";if(r||i){try{h=new URL(".",f).href}catch{}i&&(l=o=>{var p=new XMLHttpRequest;return p.open("GET",o,!1),p.responseType="arraybuffer",p.send(null),new Uint8Array(p.response)}),u=async o=>{if(O(o))return new Promise((m,c)=>{var b=new XMLHttpRequest;b.open("GET",o,!0),b.responseType="arraybuffer",b.onload=()=>{b.status==200||b.status==0&&b.response?m(b.response):c(b.status)},b.onerror=c,b.send(null)});var p=await fetch(o,{credentials:"same-origin"});if(p.ok)return p.arrayBuffer();throw Error(p.status+" : "+p.url)}}var g,w,y,v,T,$,_=console.log.bind(console),I=console.error.bind(console),k=_,E=I,C=!1,O=o=>o.startsWith("file://");function x(){ct.buffer!=Y.buffer&&te()}if(n){let o=function(p){try{var m=p.data,c=m.Uc;if(c==="load"){let b=[];self.onmessage=S=>b.push(S),$=()=>{postMessage({Uc:"loaded"});for(let S of b)o(S);self.onmessage=o};for(let S of m.Ad)t[S]&&!t[S].proxy||(t[S]=(...z)=>{postMessage({Uc:"callHandler",zd:S,args:z})},S=="print"&&(k=t[S]),S=="printErr"&&(E=t[S]));ct=m.Vd,te(),w=m.Wd,Ve(),Pr()}else if(c==="run"){(function(b){var S=(x(),U)[b+52>>>2>>>0];b=(x(),U)[b+56>>>2>>>0],Ps(S,S-b),se(S)})(m.Tc),Mi(m.Tc,0,0,1,0,0),Ua(),ki(m.Tc),L||(Os(),L=!0);try{sm(m.Pd,m.dd)}catch(b){if(b!="unwind")throw b}}else m.target!=="setimmediate"&&(c==="checkMailbox"?L&&Ar():c&&(E(`worker: received unknown command ${c}`),E(m)))}catch(b){throw Rs(),b}};var D=o,L=!1;self.onunhandledrejection=p=>{throw p.reason||p},self.onmessage=o}var Y,q,j,ue,A,U,re,J,H,pe,N,V=!1;function te(){var o=ct.buffer;t.HEAP8=Y=new Int8Array(o),j=new Int16Array(o),t.HEAPU8=q=new Uint8Array(o),ue=new Uint16Array(o),t.HEAP32=A=new Int32Array(o),t.HEAPU32=U=new Uint32Array(o),re=new Float32Array(o),J=new Float64Array(o),H=new BigInt64Array(o),pe=new BigUint64Array(o)}function ce(){V=!0,n?$():st.tb()}function Ie(o){throw E(o="Aborted("+o+")"),C=!0,o=new WebAssembly.RuntimeError(o+". Build with -sASSERTIONS for more info."),T?.(o),o}function Me(){return{a:{ma:Ag,hb:zg,g:om,J:um,f:lm,o:dm,h:pm,ha:cm,b:hm,T:fm,Ia:Fa,n:mm,_:Qa,Ya:Za,Ea:Ya,Ga:Xa,Za:Ja,Wa:es,Pa:ts,Va:rs,ka:is,Fa:ns,Ca:as,Xa:ss,Da:os,cb:gm,ea:wm,xa:_m,va:vm,da:xm,O:Tm,H:Sm,wa:km,Z:Rm,ya:Mm,Sa:Bm,Aa:Dm,Ja:Pm,ta:Um,fa:Wm,Ra:ki,$a:Lm,R:Fm,s:Zm,c:Ti,ib:Ym,y:Xm,M:Jm,D:eg,m:tg,t:ms,jb:rg,I:ig,S:ng,j:ag,v:sg,r:og,l:ug,Ma:lg,Na:dg,Oa:pg,Ka:_s,La:bs,ua:vs,eb:hg,bb:gg,u:yg,aa:wg,ga:_g,ab:fg,V:bg,_a:vg,Ba:$g,F:cg,U:xg,la:Nr,za:Sg,gb:Tg,fb:kg,Ta:Ss,Ua:ks,Ha:_i,$:Is,ja:Es,Qa:Cs,ia:zs,lb:fy,na:oy,mb:hy,oa:sy,G:Yg,d:Bg,q:Rg,w:Og,B:Fg,pb:iy,K:jg,x:Dg,pa:ny,X:uy,ba:ry,nb:cy,ob:py,ra:Xg,qa:ty,qb:Jg,N:Qg,Y:ay,e:Ng,A:Pg,k:Mg,kb:my,p:Wg,z:Lg,C:Ug,E:qg,L:Hg,rb:Zg,Q:ly,ca:Kg,W:dy,sb:Gg,sa:Vg,P:ey,i:Eg,a:ct,db:wi}}}async function Ve(){function o(c,b){var S=st=c.exports;c={};for(let[z,M]of Object.entries(S))typeof M=="function"?(S=qm(M),c[z]=S):c[z]=M;return st=c,st=function(){var z=st,M=G=>ae=>G(ae)>>>0,W=G=>()=>G()>>>0;return(z=Object.assign({},z)).ub=M(z.ub),z.Yb=W(z.Yb),z._b=M(z._b),z.mc=M(z.mc),z.nc=W(z.nc),z.rc=M(z.rc),z}(),Da.push(st.$b),As=(c=st).ub,Os=c.vb,t._OrtInit=c.wb,t._OrtGetLastError=c.xb,t._OrtCreateSessionOptions=c.yb,t._OrtAppendExecutionProvider=c.zb,t._OrtAddFreeDimensionOverride=c.Ab,t._OrtAddSessionConfigEntry=c.Bb,t._OrtReleaseSessionOptions=c.Cb,t._OrtCreateSession=c.Db,t._OrtReleaseSession=c.Eb,t._OrtGetInputOutputCount=c.Fb,t._OrtGetInputOutputMetadata=c.Gb,t._OrtFree=c.Hb,t._OrtCreateTensor=c.Ib,t._OrtGetTensorData=c.Jb,t._OrtReleaseTensor=c.Kb,t._OrtCreateRunOptions=c.Lb,t._OrtAddRunConfigEntry=c.Mb,t._OrtReleaseRunOptions=c.Nb,t._OrtCreateBinding=c.Ob,t._OrtBindInput=c.Pb,t._OrtBindOutput=c.Qb,t._OrtClearBoundOutputs=c.Rb,t._OrtReleaseBinding=c.Sb,t._OrtRunWithBinding=c.Tb,t._OrtRun=c.Ub,t._OrtEndProfiling=c.Vb,t._JsepOutput=c.Wb,t._JsepGetNodeName=c.Xb,Dr=c.Yb,rt=t._free=c.Zb,ir=t._malloc=c._b,Mi=c.bc,Rs=c.cc,Ms=c.dc,Bs=c.ec,Bi=c.fc,Ns=c.gc,Ds=c.hc,le=c.ic,nr=c.jc,Ps=c.kc,se=c.lc,Ni=c.mc,oe=c.nc,Us=c.oc,Di=c.pc,Ws=c.qc,Ls=c.rc,qs=c.sc,Pi=c.tc,Vs=c.uc,Gs=c.vc,Fs=c.wc,Hs=c.xc,Ks=c.yc,js=c.zc,Qs=c.Ac,Zs=c.Bc,Ys=c.Cc,Xs=c.Dc,Js=c.Ec,eo=c.Fc,to=c.Gc,ro=c.Hc,io=c.Ic,no=c.Jc,ao=c.Kc,so=c.Lc,oo=c.Mc,uo=c.Nc,lo=c.Oc,po=c.Pc,co=c.Rc,ho=c.Sc,fo=c.bd,mo=c.cd,go=c.hd,yo=c.kd,wo=c.ld,_o=c.md,bo=c.nd,vo=c.od,$o=c.pd,xo=c.qd,To=c.rd,So=c.wd,ko=c.Rd,Io=c.Sd,Eo=c.Td,Co=c.Ud,w=b,st}var p,m=Me();return t.instantiateWasm?new Promise(c=>{t.instantiateWasm(m,(b,S)=>{c(o(b,S))})}):n?o(new WebAssembly.Instance(w,Me()),w):(N??(N=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",h):h+"ort-wasm-simd-threaded.jsep.wasm":new URL("ort-wasm-simd-threaded.jsep.wasm",Ye.url).href),p=await async function(c){var b=N;if(!g&&!O(b))try{var S=fetch(b,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(S,c)}catch(z){E(`wasm streaming compile failed: ${z}`),E("falling back to ArrayBuffer instantiation")}return async function(z,M){try{var W=await async function(G){if(!g)try{var ae=await u(G);return new Uint8Array(ae)}catch{}if(G==N&&g)G=new Uint8Array(g);else{if(!l)throw"both async and sync fetching of the wasm failed";G=l(G)}return G}(z);return await WebAssembly.instantiate(W,M)}catch(G){E(`failed to asynchronously prepare wasm: ${G}`),Ie(G)}}(b,c)}(m),o(p.instance,p.module))}class nt{constructor(p){Q(this,"name","ExitStatus");this.message=`Program terminated with exit(${p})`,this.status=p}}var Se=o=>{o.terminate(),o.onmessage=()=>{}},$e=[],Re=0,xt=null,kr=o=>{pt.length==0&&(La(),Wa(pt[0]));var p=pt.pop();if(!p)return 6;tr.push(p),Tt[o.Tc]=p,p.Tc=o.Tc;var m={Uc:"run",Pd:o.Od,dd:o.dd,Tc:o.Tc};return p.postMessage(m,o.ud),0},dt=0,ve=(o,p,...m)=>{var c,b=16*m.length,S=oe(),z=Ni(b),M=z>>>3;for(c of m)typeof c=="bigint"?((x(),H)[M++>>>0]=1n,(x(),H)[M++>>>0]=c):((x(),H)[M++>>>0]=0n,(x(),J)[M++>>>0]=c);return o=Ms(o,0,b,z,p),se(S),o};function wi(o){if(n)return ve(0,1,o);if(y=o,!(0<dt)){for(var p of tr)Se(p);for(p of pt)Se(p);pt=[],tr=[],Tt={},C=!0}d(0,new nt(o))}function Na(o){if(n)return ve(1,0,o);_i(o)}var _i=o=>{if(y=o,n)throw Na(o),"unwind";wi(o)},pt=[],tr=[],Da=[],Tt={},Pa=o=>{var p=o.Tc;delete Tt[p],pt.push(o),tr.splice(tr.indexOf(o),1),o.Tc=0,Bs(p)};function Ua(){Da.forEach(o=>o())}var Wa=o=>new Promise(p=>{o.onmessage=b=>{var S=b.data;if(b=S.Uc,S.ad&&S.ad!=Dr()){var z=Tt[S.ad];z?z.postMessage(S,S.ud):E(`Internal error! Worker sent a message "${b}" to target pthread ${S.ad}, but that thread no longer exists!`)}else b==="checkMailbox"?Ar():b==="spawnThread"?kr(S):b==="cleanupThread"?zr(()=>{Pa(Tt[S.Qd])}):b==="loaded"?(o.loaded=!0,p(o)):S.target==="setimmediate"?o.postMessage(S):b==="uncaughtException"?o.onerror(S.error):b==="callHandler"?t[S.zd](...S.args):b&&E(`worker sent an unknown command ${b}`)},o.onerror=b=>{throw E(`worker sent an error! ${b.filename}:${b.lineno}: ${b.message}`),b};var m,c=[];for(m of[])t.propertyIsEnumerable(m)&&c.push(m);o.postMessage({Uc:"load",Ad:c,Vd:ct,Wd:w})});function La(){var o=new Worker((()=>{let p=URL;return Ye.url>"file:"&&Ye.url<"file;"?new p("ort.bundle.min.mjs",Ye.url):new URL(Ye.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});pt.push(o)}var ct,sm=(o,p)=>{dt=0,o=Pi(o,p),0<dt?y=o:Bi(o)},Ir=[],Er=0;function om(o){var p=new bi(o>>>=0);return(x(),Y)[p.Vc+12>>>0]==0&&(qa(p,!0),Er--),Va(p,!1),Ir.push(p),Ls(o)}var Ft=0,um=()=>{le(0,0);var o=Ir.pop();Us(o.ed),Ft=0};function qa(o,p){p=p?1:0,(x(),Y)[o.Vc+12>>>0]=p}function Va(o,p){p=p?1:0,(x(),Y)[o.Vc+13>>>0]=p}class bi{constructor(p){this.ed=p,this.Vc=p-24}}var vi=o=>{var p=Ft;if(!p)return nr(0),0;var m=new bi(p);(x(),U)[m.Vc+16>>>2>>>0]=p;var c=(x(),U)[m.Vc+4>>>2>>>0];if(!c)return nr(0),p;for(var b of o){if(b===0||b===c)break;if(Ws(b,c,m.Vc+16))return nr(b),p}return nr(c),p};function lm(){return vi([])}function dm(o){return vi([o>>>0])}function pm(o,p,m,c){return vi([o>>>0,p>>>0,m>>>0,c>>>0])}var cm=()=>{var o=Ir.pop();o||Ie("no exception to throw");var p=o.ed;throw(x(),Y)[o.Vc+13>>>0]==0&&(Ir.push(o),Va(o,!0),qa(o,!1),Er++),Di(p),Ft=p};function hm(o,p,m){var c=new bi(o>>>=0);throw p>>>=0,m>>>=0,(x(),U)[c.Vc+16>>>2>>>0]=0,(x(),U)[c.Vc+4>>>2>>>0]=p,(x(),U)[c.Vc+8>>>2>>>0]=m,Di(o),Er++,Ft=o}var fm=()=>Er;function Ga(o,p,m,c){return n?ve(2,1,o,p,m,c):Fa(o,p,m,c)}function Fa(o,p,m,c){if(o>>>=0,p>>>=0,m>>>=0,c>>>=0,!globalThis.SharedArrayBuffer)return 6;var b=[];return n&&b.length===0?Ga(o,p,m,c):(o={Od:m,Tc:o,dd:c,ud:b},n?(o.Uc="spawnThread",postMessage(o,b),0):kr(o))}function mm(o){throw Ft||(Ft=o>>>0),Ft}var Ha=globalThis.TextDecoder&&new TextDecoder,Ka=(o,p,m,c)=>{if(m=p+m,c)return m;for(;o[p]&&!(p>=m);)++p;return p},ja=(o,p=0,m,c)=>{if(16<(m=Ka(o,p>>>=0,m,c))-p&&o.buffer&&Ha)return Ha.decode(o.buffer instanceof ArrayBuffer?o.subarray(p,m):o.slice(p,m));for(c="";p<m;){var b=o[p++];if(128&b){var S=63&o[p++];if((224&b)==192)c+=String.fromCharCode((31&b)<<6|S);else{var z=63&o[p++];65536>(b=(240&b)==224?(15&b)<<12|S<<6|z:(7&b)<<18|S<<12|z<<6|63&o[p++])?c+=String.fromCharCode(b):(b-=65536,c+=String.fromCharCode(55296|b>>10,56320|1023&b))}}else c+=String.fromCharCode(b)}return c},ke=(o,p,m)=>(o>>>=0)?ja((x(),q),o,p,m):"";function Qa(o,p,m){return n?ve(3,1,o,p,m):0}function Za(o,p){if(n)return ve(4,1,o,p)}function Ya(o,p){if(n)return ve(5,1,o,p)}function Xa(o,p,m){if(n)return ve(6,1,o,p,m)}function Ja(o,p,m){return n?ve(7,1,o,p,m):0}function es(o,p){if(n)return ve(8,1,o,p)}function ts(o,p,m){if(n)return ve(9,1,o,p,m)}function rs(o,p,m,c){if(n)return ve(10,1,o,p,m,c)}function is(o,p,m,c){if(n)return ve(11,1,o,p,m,c)}function ns(o,p,m,c){if(n)return ve(12,1,o,p,m,c)}function as(o){if(n)return ve(13,1,o)}function ss(o,p){if(n)return ve(14,1,o,p)}function os(o,p,m){if(n)return ve(15,1,o,p,m)}var gm=()=>Ie(""),et=o=>{o>>>=0;for(var p="";;){var m=(x(),q)[o++>>>0];if(!m)return p;p+=String.fromCharCode(m)}},$i={},xi={},ym={},Ht=class extends Error{constructor(o){super(o),this.name="BindingError"}};function at(o,p,m={}){return function(c,b,S={}){var z=b.name;if(!c)throw new Ht(`type "${z}" must have a positive integer typeid pointer`);if(xi.hasOwnProperty(c)){if(S.Bd)return;throw new Ht(`Cannot register type '${z}' twice`)}xi[c]=b,delete ym[c],$i.hasOwnProperty(c)&&(b=$i[c],delete $i[c],b.forEach(M=>M()))}(o,p,m)}var us=(o,p,m)=>{switch(p){case 1:return m?c=>(x(),Y)[c>>>0]:c=>(x(),q)[c>>>0];case 2:return m?c=>(x(),j)[c>>>1>>>0]:c=>(x(),ue)[c>>>1>>>0];case 4:return m?c=>(x(),A)[c>>>2>>>0]:c=>(x(),U)[c>>>2>>>0];case 8:return m?c=>(x(),H)[c>>>3>>>0]:c=>(x(),pe)[c>>>3>>>0];default:throw new TypeError(`invalid integer width (${p}): ${o}`)}};function wm(o,p,m,c,b){o>>>=0,m>>>=0,p=et(p>>>0);let S=z=>z;if(c=c===0n){let z=8*m;S=M=>BigInt.asUintN(z,M),b=S(b)}at(o,{name:p,Qc:S,Xc:(z,M)=>(typeof M=="number"&&(M=BigInt(M)),M),Wc:us(p,m,!c),Yc:null})}function _m(o,p,m,c){at(o>>>=0,{name:p=et(p>>>0),Qc:function(b){return!!b},Xc:function(b,S){return S?m:c},Wc:function(b){return this.Qc((x(),q)[b>>>0])},Yc:null})}var ls=[],St=[0,1,,1,null,1,!0,1,!1,1];function Ti(o){9<(o>>>=0)&&--St[o+1]==0&&(St[o]=void 0,ls.push(o))}var Pe=o=>{if(!o)throw new Ht(`Cannot use deleted val. handle = ${o}`);return St[o]},Ge=o=>{switch(o){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let p=ls.pop()||St.length;return St[p]=o,St[p+1]=1,p}};function Si(o){return this.Qc((x(),U)[o>>>2>>>0])}var bm={name:"emscripten::val",Qc:o=>{var p=Pe(o);return Ti(o),p},Xc:(o,p)=>Ge(p),Wc:Si,Yc:null};function vm(o){return at(o>>>0,bm)}var $m=(o,p)=>{switch(p){case 4:return function(m){return this.Qc((x(),re)[m>>>2>>>0])};case 8:return function(m){return this.Qc((x(),J)[m>>>3>>>0])};default:throw new TypeError(`invalid float width (${p}): ${o}`)}};function xm(o,p,m){m>>>=0,at(o>>>=0,{name:p=et(p>>>0),Qc:c=>c,Xc:(c,b)=>b,Wc:$m(p,m),Yc:null})}function Tm(o,p,m,c,b){o>>>=0,m>>>=0,p=et(p>>>0);let S=M=>M;if(c===0){var z=32-8*m;S=M=>M<<z>>>z,b=S(b)}at(o,{name:p,Qc:S,Xc:(M,W)=>W,Wc:us(p,m,c!==0),Yc:null})}function Sm(o,p,m){function c(S){var z=(x(),U)[S>>>2>>>0];return S=(x(),U)[S+4>>>2>>>0],new b((x(),Y).buffer,S,z)}var b=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][p];at(o>>>=0,{name:m=et(m>>>0),Qc:c,Wc:c},{Bd:!0})}var ht=(o,p,m)=>{var c=(x(),q);if(p>>>=0,0<m){var b=p;m=p+m-1;for(var S=0;S<o.length;++S){var z=o.codePointAt(S);if(127>=z){if(p>=m)break;c[p++>>>0]=z}else if(2047>=z){if(p+1>=m)break;c[p++>>>0]=192|z>>6,c[p++>>>0]=128|63&z}else if(65535>=z){if(p+2>=m)break;c[p++>>>0]=224|z>>12,c[p++>>>0]=128|z>>6&63,c[p++>>>0]=128|63&z}else{if(p+3>=m)break;c[p++>>>0]=240|z>>18,c[p++>>>0]=128|z>>12&63,c[p++>>>0]=128|z>>6&63,c[p++>>>0]=128|63&z,S++}}c[p>>>0]=0,o=p-b}else o=0;return o},Cr=o=>{for(var p=0,m=0;m<o.length;++m){var c=o.charCodeAt(m);127>=c?p++:2047>=c?p+=2:55296<=c&&57343>=c?(p+=4,++m):p+=3}return p};function km(o,p){at(o>>>=0,{name:p=et(p>>>0),Qc(m){var c=(x(),U)[m>>>2>>>0];return c=ke(m+4,c,!0),rt(m),c},Xc(m,c){c instanceof ArrayBuffer&&(c=new Uint8Array(c));var b=typeof c=="string";if(!(b||ArrayBuffer.isView(c)&&c.BYTES_PER_ELEMENT==1))throw new Ht("Cannot pass non-string to std::string");var S=b?Cr(c):c.length,z=ir(4+S+1),M=z+4;return(x(),U)[z>>>2>>>0]=S,b?ht(c,M,S+1):(x(),q).set(c,M>>>0),m!==null&&m.push(rt,z),z},Wc:Si,Yc(m){rt(m)}})}var ds=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,Im=(o,p,m)=>{if(o>>>=1,16<(p=Ka((x(),ue),o,p/2,m))-o&&ds)return ds.decode((x(),ue).slice(o,p));for(m="";o<p;++o){var c=(x(),ue)[o>>>0];m+=String.fromCharCode(c)}return m},Em=(o,p,m)=>{if(m??(m=2147483647),2>m)return 0;var c=p;m=(m-=2)<2*o.length?m/2:o.length;for(var b=0;b<m;++b){var S=o.charCodeAt(b);(x(),j)[p>>>1>>>0]=S,p+=2}return(x(),j)[p>>>1>>>0]=0,p-c},Cm=o=>2*o.length,zm=(o,p,m)=>{var c="";o>>>=2;for(var b=0;!(b>=p/4);b++){var S=(x(),U)[o+b>>>0];if(!S&&!m)break;c+=String.fromCodePoint(S)}return c},Am=(o,p,m)=>{if(p>>>=0,m??(m=2147483647),4>m)return 0;var c=p;m=c+m-4;for(var b=0;b<o.length;++b){var S=o.codePointAt(b);if(65535<S&&b++,(x(),A)[p>>>2>>>0]=S,(p+=4)+4>m)break}return(x(),A)[p>>>2>>>0]=0,p-c},Om=o=>{for(var p=0,m=0;m<o.length;++m)65535<o.codePointAt(m)&&m++,p+=4;return p};function Rm(o,p,m){if(o>>>=0,p>>>=0,m=et(m>>>=0),p===2)var c=Im,b=Em,S=Cm;else c=zm,b=Am,S=Om;at(o,{name:m,Qc:z=>{var M=(x(),U)[z>>>2>>>0];return M=c(z+4,M*p,!0),rt(z),M},Xc:(z,M)=>{if(typeof M!="string")throw new Ht(`Cannot pass non-string to C++ string type ${m}`);var W=S(M),G=ir(4+W+p);return(x(),U)[G>>>2>>>0]=W/p,b(M,G+4,W+p),z!==null&&z.push(rt,G),G},Wc:Si,Yc(z){rt(z)}})}function Mm(o,p){at(o>>>=0,{Cd:!0,name:p=et(p>>>0),Qc:()=>{},Xc:()=>{}})}function Bm(o){Mi(o>>>0,!i,1,!r,131072,!1),Ua()}var zr=o=>{if(!C)try{if(o(),!(0<dt))try{n?Dr()&&Bi(y):_i(y)}catch(p){p instanceof nt||p=="unwind"||d(0,p)}}catch(p){p instanceof nt||p=="unwind"||d(0,p)}},Nm=!Atomics.waitAsync||globalThis.navigator?.userAgent&&91>Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function ki(o){o>>>=0,Nm||(Atomics.waitAsync((x(),A),o>>>2,o).value.then(Ar),o+=128,Atomics.store((x(),A),o>>>2,1))}var Ar=()=>zr(()=>{var o=Dr();o&&(ki(o),Ds())});function Dm(o,p){(o>>>=0)==p>>>0?setTimeout(Ar):n?postMessage({ad:o,Uc:"checkMailbox"}):(o=Tt[o])&&o.postMessage({Uc:"checkMailbox"})}var Ii=[];function Pm(o,p,m,c,b){for(p>>>=0,b>>>=0,Ii.length=0,m=b>>>3,c=b+c>>>3;m<c;){var S;S=(x(),H)[m++>>>0]?(x(),H)[m++>>>0]:(x(),J)[m++>>>0],Ii.push(S)}return(p?Ui[p]:Cg[o])(...Ii)}var Um=()=>{dt=0};function Wm(o){o>>>=0,n?postMessage({Uc:"cleanupThread",Qd:o}):Pa(Tt[o])}function Lm(o){}var Or=o=>{try{o()}catch(p){Ie(p)}};function qm(o){var p=(...m)=>{Rr.push(o);try{return o(...m)}finally{C||(Rr.pop(),tt&&ft===1&&Rr.length===0&&(ft=0,dt+=1,Or(Io),typeof Fibers<"u"&&Fibers.ce()))}};return hs.set(o,p),p}var ft=0,tt=null,ps=0,Rr=[],Ei=new Map,cs=new Map,hs=new Map,Vm=0,Ci=null,Gm=[],fs=o=>function(p){if(!C){if(ft===0){var m=!1,c=!1;p((b=0)=>{if(!C&&(ps=b,m=!0,c)){ft=2,Or(()=>Eo(tt)),typeof MainLoop<"u"&&MainLoop.yd&&MainLoop.resume(),b=!1;try{var S=function(){var W=(x(),A)[tt+8>>>2>>>0];return W=cs.get(W),W=hs.get(W),--dt,W()}()}catch(W){S=W,b=!0}var z=!1;if(!tt){var M=Ci;M&&(Ci=null,(b?M.reject:M.resolve)(S),z=!0)}if(b&&!z)throw S}}),c=!0,m||(ft=1,tt=function(){var b=ir(65548),S=b+12;if((x(),U)[b>>>2>>>0]=S,(x(),U)[b+4>>>2>>>0]=S+65536,S=Rr[0],!Ei.has(S)){var z=Vm++;Ei.set(S,z),cs.set(z,S)}return S=Ei.get(S),(x(),A)[b+8>>>2>>>0]=S,b}(),typeof MainLoop<"u"&&MainLoop.yd&&MainLoop.pause(),Or(()=>ko(tt)))}else ft===2?(ft=0,Or(Co),rt(tt),tt=null,Gm.forEach(zr)):Ie(`invalid state: ${ft}`);return ps}}(p=>{o().then(p)});function Fm(o){return o>>>=0,fs(async()=>{var p=await Pe(o);return Ge(p)})}var zi=[],Hm=o=>{var p=zi.length;return zi.push(o),p},Km=(o,p)=>{for(var m=Array(o),c=0;c<o;++c){var b=c,S=(x(),U)[p+4*c>>>2>>>0],z=xi[S];if(z===void 0)throw o=`parameter ${c}`,S=As(S),p=et(S),rt(S),new Ht(`${o} has unknown type ${p}`);m[b]=z}return m},jm=(o,p,m)=>{var c=[];return o=o(c,m),c.length&&((x(),U)[p>>>2>>>0]=Ge(c)),o},Qm={},Mr=o=>{var p=Qm[o];return p===void 0?et(o):p};function Zm(o,p,m){var[c,...b]=Km(o,p>>>0);p=c.Xc.bind(c);var S=b.map(W=>W.Wc.bind(W));o--;var z={toValue:Pe};switch(o=S.map((W,G)=>{var ae=`argFromPtr${G}`;return z[ae]=W,`${ae}(args${G?"+"+8*G:""})`}),m){case 0:var M="toValue(handle)";break;case 2:M="new (toValue(handle))";break;case 3:M="";break;case 1:z.getStringOrSymbol=Mr,M="toValue(handle)[getStringOrSymbol(methodName)]"}return M+=`(${o})`,c.Cd||(z.toReturnWire=p,z.emval_returnValue=jm,M=`return emval_returnValue(toReturnWire, destructorsRef, ${M})`),M=`return function (handle, methodName, destructorsRef, args) {
|
|
3
|
+
${M}
|
|
4
|
+
}`,m=new Function(Object.keys(z),M)(...Object.values(z)),M=`methodCaller<(${b.map(W=>W.name)}) => ${c.name}>`,Hm(Object.defineProperty(m,"name",{value:M}))}function Ym(o,p){return p>>>=0,(o=Pe(o>>>0))==Pe(p)}function Xm(o){return(o>>>=0)?(o=Mr(o),Ge(globalThis[o])):Ge(globalThis)}function Jm(o){return o=Mr(o>>>0),Ge(t[o])}function eg(o,p){return p>>>=0,o=Pe(o>>>0),p=Pe(p),Ge(o[p])}function tg(o){9<(o>>>=0)&&(St[o+1]+=1)}function ms(o,p,m,c,b){return zi[o>>>0](p>>>0,m>>>0,c>>>0,b>>>0)}function rg(o,p,m,c,b){return ms(o>>>0,p>>>0,m>>>0,c>>>0,b>>>0)}function ig(){return Ge([])}function ng(o){o=Pe(o>>>0);for(var p=Array(o.length),m=0;m<o.length;m++)p[m]=o[m];return Ge(p)}function ag(o){return Ge(Mr(o>>>0))}function sg(){return Ge({})}function og(o){for(var p=Pe(o>>>=0);p.length;){var m=p.pop();p.pop()(m)}Ti(o)}function ug(o,p,m){p>>>=0,m>>>=0,o=Pe(o>>>0),p=Pe(p),m=Pe(m),o[p]=m}function lg(o,p){o=-9007199254740992>o||9007199254740992<o?NaN:Number(o),p>>>=0,o=new Date(1e3*o),(x(),A)[p>>>2>>>0]=o.getUTCSeconds(),(x(),A)[p+4>>>2>>>0]=o.getUTCMinutes(),(x(),A)[p+8>>>2>>>0]=o.getUTCHours(),(x(),A)[p+12>>>2>>>0]=o.getUTCDate(),(x(),A)[p+16>>>2>>>0]=o.getUTCMonth(),(x(),A)[p+20>>>2>>>0]=o.getUTCFullYear()-1900,(x(),A)[p+24>>>2>>>0]=o.getUTCDay(),o=(o.getTime()-Date.UTC(o.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(x(),A)[p+28>>>2>>>0]=o}var gs=o=>o%4==0&&(o%100!=0||o%400==0),ys=[0,31,60,91,121,152,182,213,244,274,305,335],ws=[0,31,59,90,120,151,181,212,243,273,304,334];function dg(o,p){o=-9007199254740992>o||9007199254740992<o?NaN:Number(o),p>>>=0,o=new Date(1e3*o),(x(),A)[p>>>2>>>0]=o.getSeconds(),(x(),A)[p+4>>>2>>>0]=o.getMinutes(),(x(),A)[p+8>>>2>>>0]=o.getHours(),(x(),A)[p+12>>>2>>>0]=o.getDate(),(x(),A)[p+16>>>2>>>0]=o.getMonth(),(x(),A)[p+20>>>2>>>0]=o.getFullYear()-1900,(x(),A)[p+24>>>2>>>0]=o.getDay();var m=(gs(o.getFullYear())?ys:ws)[o.getMonth()]+o.getDate()-1|0;(x(),A)[p+28>>>2>>>0]=m,(x(),A)[p+36>>>2>>>0]=-60*o.getTimezoneOffset(),m=new Date(o.getFullYear(),6,1).getTimezoneOffset();var c=new Date(o.getFullYear(),0,1).getTimezoneOffset();o=0|(m!=c&&o.getTimezoneOffset()==Math.min(c,m)),(x(),A)[p+32>>>2>>>0]=o}function pg(o){o>>>=0;var p=new Date((x(),A)[o+20>>>2>>>0]+1900,(x(),A)[o+16>>>2>>>0],(x(),A)[o+12>>>2>>>0],(x(),A)[o+8>>>2>>>0],(x(),A)[o+4>>>2>>>0],(x(),A)[o>>>2>>>0],0),m=(x(),A)[o+32>>>2>>>0],c=p.getTimezoneOffset(),b=new Date(p.getFullYear(),6,1).getTimezoneOffset(),S=new Date(p.getFullYear(),0,1).getTimezoneOffset(),z=Math.min(S,b);return 0>m?(x(),A)[o+32>>>2>>>0]=+(b!=S&&z==c):0<m!=(z==c)&&(b=Math.max(S,b),p.setTime(p.getTime()+6e4*((0<m?z:b)-c))),(x(),A)[o+24>>>2>>>0]=p.getDay(),m=(gs(p.getFullYear())?ys:ws)[p.getMonth()]+p.getDate()-1|0,(x(),A)[o+28>>>2>>>0]=m,(x(),A)[o>>>2>>>0]=p.getSeconds(),(x(),A)[o+4>>>2>>>0]=p.getMinutes(),(x(),A)[o+8>>>2>>>0]=p.getHours(),(x(),A)[o+12>>>2>>>0]=p.getDate(),(x(),A)[o+16>>>2>>>0]=p.getMonth(),(x(),A)[o+20>>>2>>>0]=p.getYear(),o=p.getTime(),BigInt(isNaN(o)?-1:o/1e3)}function _s(o,p,m,c,b,S,z){return n?ve(16,1,o,p,m,c,b,S,z):-52}function bs(o,p,m,c,b,S){if(n)return ve(17,1,o,p,m,c,b,S)}var rr={},cg=()=>performance.timeOrigin+performance.now();function vs(o,p){if(n)return ve(18,1,o,p);if(rr[o]&&(clearTimeout(rr[o].id),delete rr[o]),!p)return 0;var m=setTimeout(()=>{delete rr[o],zr(()=>Ns(o,performance.timeOrigin+performance.now()))},p);return rr[o]={id:m,be:p},0}function hg(o,p,m,c){o>>>=0,p>>>=0,m>>>=0,c>>>=0;var b=new Date().getFullYear(),S=new Date(b,0,1).getTimezoneOffset();b=new Date(b,6,1).getTimezoneOffset();var z=Math.max(S,b);(x(),U)[o>>>2>>>0]=60*z,(x(),A)[p>>>2>>>0]=+(S!=b),o=(p=M=>{var W=Math.abs(M);return`UTC${0<=M?"-":"+"}${String(Math.floor(W/60)).padStart(2,"0")}${String(W%60).padStart(2,"0")}`})(S),p=p(b),b<S?(ht(o,m,17),ht(p,c,17)):(ht(o,c,17),ht(p,m,17))}var fg=()=>Date.now(),mg=1;function gg(o,p,m){if(m>>>=0,!(0<=o&&3>=o))return 28;if(o===0)o=Date.now();else{if(!mg)return 52;o=performance.timeOrigin+performance.now()}return o=Math.round(1e6*o),(x(),H)[m>>>3>>>0]=BigInt(o),0}var Ai=[],$s=(o,p)=>{Ai.length=0;for(var m;m=(x(),q)[o++>>>0];){var c=m!=105;p+=(c&=m!=112)&&p%8?4:0,Ai.push(m==112?(x(),U)[p>>>2>>>0]:m==106?(x(),H)[p>>>3>>>0]:m==105?(x(),A)[p>>>2>>>0]:(x(),J)[p>>>3>>>0]),p+=c?8:4}return Ai};function yg(o,p,m){return o>>>=0,p=$s(p>>>0,m>>>0),Ui[o](...p)}function wg(o,p,m){return o>>>=0,p=$s(p>>>0,m>>>0),Ui[o](...p)}var _g=()=>{};function bg(o,p){return E(ke(o>>>0,p>>>0))}var vg=()=>{throw dt+=1,"unwind"};function $g(){return 4294901760}var xg=()=>navigator.hardwareConcurrency,kt={},Br=o=>{var p;return(p=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(o))?+p[1]:(p=/:(\d+):\d+(?:\)|$)/.exec(o))?2147483648|+p[1]:0},xs=o=>{for(var p of o)(o=Br(p))&&(kt[o]=p)};function Tg(){var o=Error().stack.toString().split(`
|
|
5
|
+
`);return o[0]=="Error"&&o.shift(),xs(o),kt.sd=Br(o[3]),kt.Md=o,kt.sd}function Nr(o){if(!(o=kt[o>>>0]))return 0;var p;if(p=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(o))o=p[1];else if(p=/^\s+at (.*) \(.*\)$/.exec(o))o=p[1];else{if(!(p=/^(.+?)@/.exec(o)))return 0;o=p[1]}rt(Nr.td??0),p=Cr(o)+1;var m=ir(p);return m&&ht(o,m,p),Nr.td=m,Nr.td}function Sg(o){o>>>=0;var p=(x(),q).length;if(o<=p||4294901760<o)return!1;for(var m=1;4>=m;m*=2){var c=p*(1+.2/m);c=Math.min(c,o+100663296);e:{c=(Math.min(4294901760,65536*Math.ceil(Math.max(o,c)/65536))-ct.buffer.byteLength+65535)/65536|0;try{ct.grow(c),te();var b=1;break e}catch{}b=void 0}if(b)return!0}return!1}function kg(o,p,m){if(o>>>=0,p>>>=0,kt.sd==o)var c=kt.Md;else(c=Error().stack.toString().split(`
|
|
6
|
+
`))[0]=="Error"&&c.shift(),xs(c);for(var b=3;c[b]&&Br(c[b])!=o;)++b;for(o=0;o<m&&c[o+b];++o)(x(),A)[p+4*o>>>2>>>0]=Br(c[o+b]);return o}var Oi,Ri={},Ts=()=>{if(!Oi){var o,p={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(globalThis.navigator?.language??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(o in Ri)Ri[o]===void 0?delete p[o]:p[o]=Ri[o];var m=[];for(o in p)m.push(`${o}=${p[o]}`);Oi=m}return Oi};function Ss(o,p){if(n)return ve(19,1,o,p);o>>>=0,p>>>=0;var m,c=0,b=0;for(m of Ts()){var S=p+c;(x(),U)[o+b>>>2>>>0]=S,c+=ht(m,S,1/0)+1,b+=4}return 0}function ks(o,p){if(n)return ve(20,1,o,p);o>>>=0,p>>>=0;var m=Ts();for(var c of((x(),U)[o>>>2>>>0]=m.length,o=0,m))o+=Cr(c)+1;return(x(),U)[p>>>2>>>0]=o,0}function Is(o){return n?ve(21,1,o):52}function Es(o,p,m,c){return n?ve(22,1,o,p,m,c):52}function Cs(o,p,m,c){return n?ve(23,1,o,p,m,c):70}var Ig=[null,[],[]];function zs(o,p,m,c){if(n)return ve(24,1,o,p,m,c);p>>>=0,m>>>=0,c>>>=0;for(var b=0,S=0;S<m;S++){var z=(x(),U)[p>>>2>>>0],M=(x(),U)[p+4>>>2>>>0];p+=8;for(var W=0;W<M;W++){var G=o,ae=(x(),q)[z+W>>>0],he=Ig[G];ae===0||ae===10?((G===1?k:E)(ja(he)),he.length=0):he.push(ae)}b+=M}return(x(),U)[c>>>2>>>0]=b,0}function Eg(o){return o>>>0}n||function(){for(var o=t.numThreads-1;o--;)La();$e.push(async()=>{var p=async function(){if(!n)return Promise.all(pt.map(Wa))}();Re++,await p,--Re==0&&xt&&(p=xt,xt=null,p())})}(),n||(ct=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),te()),t.wasmBinary&&(g=t.wasmBinary),t.stackSave=()=>oe(),t.stackRestore=o=>se(o),t.stackAlloc=o=>Ni(o),t.setValue=function(o,p,m="i8"){switch(m.endsWith("*")&&(m="*"),m){case"i1":case"i8":(x(),Y)[o>>>0]=p;break;case"i16":(x(),j)[o>>>1>>>0]=p;break;case"i32":(x(),A)[o>>>2>>>0]=p;break;case"i64":(x(),H)[o>>>3>>>0]=BigInt(p);break;case"float":(x(),re)[o>>>2>>>0]=p;break;case"double":(x(),J)[o>>>3>>>0]=p;break;case"*":(x(),U)[o>>>2>>>0]=p;break;default:Ie(`invalid type for setValue: ${m}`)}},t.getValue=function(o,p="i8"){switch(p.endsWith("*")&&(p="*"),p){case"i1":case"i8":return(x(),Y)[o>>>0];case"i16":return(x(),j)[o>>>1>>>0];case"i32":return(x(),A)[o>>>2>>>0];case"i64":return(x(),H)[o>>>3>>>0];case"float":return(x(),re)[o>>>2>>>0];case"double":return(x(),J)[o>>>3>>>0];case"*":return(x(),U)[o>>>2>>>0];default:Ie(`invalid type for getValue: ${p}`)}},t.UTF8ToString=ke,t.stringToUTF8=ht,t.lengthBytesUTF8=Cr;var As,Os,Dr,rt,ir,Mi,Rs,Ms,Bs,Bi,Ns,Ds,le,nr,Ps,se,Ni,oe,Us,Di,Ws,Ls,qs,Pi,Vs,Gs,Fs,Hs,Ks,js,Qs,Zs,Ys,Xs,Js,eo,to,ro,io,no,ao,so,oo,uo,lo,po,co,ho,fo,mo,go,yo,wo,_o,bo,vo,$o,xo,To,So,ko,Io,Eo,Co,st,Cg=[wi,Na,Ga,Qa,Za,Ya,Xa,Ja,es,ts,rs,is,ns,as,ss,os,_s,bs,vs,Ss,ks,Is,Es,Cs,zs],Ui={927820:(o,p,m,c,b)=>{if(t===void 0||!t.Zc)return 1;if((o=ke(Number(o>>>0))).startsWith("./")&&(o=o.substring(2)),!(o=t.Zc.get(o)))return 2;if(p=Number(p>>>0),m=Number(m>>>0),c=Number(c>>>0),p+m>o.byteLength)return 3;try{let S=o.subarray(p,p+m);switch(b){case 0:(x(),q).set(S,c>>>0);break;case 1:t.Xd?t.Xd(c,S):t.Ld(c,S);break;default:return 4}return 0}catch{return 4}},928644:(o,p,m)=>{t.xd(o,(x(),q).subarray(p>>>0,p+m>>>0))},928708:()=>t.Zd(),928750:o=>{t.vd(o)},928787:()=>{t.Ed()},928818:()=>{t.Fd()},928847:()=>{t.Jd()},928872:o=>t.Dd(o),928905:o=>t.Hd(o),928937:(o,p,m)=>{t.jd(Number(o),Number(p),Number(m),!0)},929e3:(o,p,m)=>{t.jd(Number(o),Number(p),Number(m))},929057:()=>typeof wasmOffsetConverter<"u",929114:o=>{t.ac("Abs",o,void 0)},929165:o=>{t.ac("Neg",o,void 0)},929216:o=>{t.ac("Floor",o,void 0)},929269:o=>{t.ac("Ceil",o,void 0)},929321:o=>{t.ac("Reciprocal",o,void 0)},929379:o=>{t.ac("Sqrt",o,void 0)},929431:o=>{t.ac("Exp",o,void 0)},929482:o=>{t.ac("Erf",o,void 0)},929533:o=>{t.ac("Sigmoid",o,void 0)},929588:(o,p,m)=>{t.ac("HardSigmoid",o,{alpha:p,beta:m})},929667:o=>{t.ac("Log",o,void 0)},929718:o=>{t.ac("Sin",o,void 0)},929769:o=>{t.ac("Cos",o,void 0)},929820:o=>{t.ac("Tan",o,void 0)},929871:o=>{t.ac("Asin",o,void 0)},929923:o=>{t.ac("Acos",o,void 0)},929975:o=>{t.ac("Atan",o,void 0)},930027:o=>{t.ac("Sinh",o,void 0)},930079:o=>{t.ac("Cosh",o,void 0)},930131:o=>{t.ac("Asinh",o,void 0)},930184:o=>{t.ac("Acosh",o,void 0)},930237:o=>{t.ac("Atanh",o,void 0)},930290:o=>{t.ac("Tanh",o,void 0)},930342:o=>{t.ac("Not",o,void 0)},930393:(o,p,m)=>{t.ac("Clip",o,{min:p,max:m})},930462:o=>{t.ac("Clip",o,void 0)},930514:(o,p)=>{t.ac("Elu",o,{alpha:p})},930572:o=>{t.ac("Gelu",o,void 0)},930624:o=>{t.ac("Relu",o,void 0)},930676:(o,p)=>{t.ac("LeakyRelu",o,{alpha:p})},930740:(o,p)=>{t.ac("ThresholdedRelu",o,{alpha:p})},930810:(o,p)=>{t.ac("Cast",o,{to:p})},930868:o=>{t.ac("Add",o,void 0)},930919:o=>{t.ac("Sub",o,void 0)},930970:o=>{t.ac("Mul",o,void 0)},931021:o=>{t.ac("Div",o,void 0)},931072:o=>{t.ac("Pow",o,void 0)},931123:o=>{t.ac("Equal",o,void 0)},931176:o=>{t.ac("Greater",o,void 0)},931231:o=>{t.ac("GreaterOrEqual",o,void 0)},931293:o=>{t.ac("Less",o,void 0)},931345:o=>{t.ac("LessOrEqual",o,void 0)},931404:(o,p,m,c,b)=>{t.ac("ReduceMean",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},931579:(o,p,m,c,b)=>{t.ac("ReduceMax",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},931753:(o,p,m,c,b)=>{t.ac("ReduceMin",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},931927:(o,p,m,c,b)=>{t.ac("ReduceProd",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},932102:(o,p,m,c,b)=>{t.ac("ReduceSum",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},932276:(o,p,m,c,b)=>{t.ac("ReduceL1",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},932449:(o,p,m,c,b)=>{t.ac("ReduceL2",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},932622:(o,p,m,c,b)=>{t.ac("ReduceLogSum",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},932799:(o,p,m,c,b)=>{t.ac("ReduceSumSquare",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},932979:(o,p,m,c,b)=>{t.ac("ReduceLogSumExp",o,{keepDims:!!p,noopWithEmptyAxes:!!m,axes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},933159:o=>{t.ac("Where",o,void 0)},933212:(o,p,m)=>{t.ac("Transpose",o,{perm:p?Array.from((x(),A).subarray(Number(p)>>>0,Number(m)>>>0)):[]})},933336:(o,p,m,c)=>{t.ac("DepthToSpace",o,{blocksize:p,mode:ke(m),format:c?"NHWC":"NCHW"})},933469:(o,p,m,c)=>{t.ac("DepthToSpace",o,{blocksize:p,mode:ke(m),format:c?"NHWC":"NCHW"})},933602:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e,mt)=>{t.ac("ConvTranspose",o,{format:W?"NHWC":"NCHW",autoPad:p,dilations:[m],group:c,kernelShape:[b],pads:[S,z],strides:[M],wIsConst:()=>!!(x(),Y)[G>>>0],outputPadding:ae?Array.from((x(),A).subarray(Number(ae)>>>0,Number(he)>>>0)):[],outputShape:ye?Array.from((x(),A).subarray(Number(ye)>>>0,Number(_e)>>>0)):[],activation:ke(mt)})},934035:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e)=>{t.ac("ConvTranspose",o,{format:M?"NHWC":"NCHW",autoPad:p,dilations:Array.from((x(),A).subarray(Number(m)>>>0,2+(Number(m)>>>0)>>>0)),group:c,kernelShape:Array.from((x(),A).subarray(Number(b)>>>0,2+(Number(b)>>>0)>>>0)),pads:Array.from((x(),A).subarray(Number(S)>>>0,4+(Number(S)>>>0)>>>0)),strides:Array.from((x(),A).subarray(Number(z)>>>0,2+(Number(z)>>>0)>>>0)),wIsConst:()=>!!(x(),Y)[W>>>0],outputPadding:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],outputShape:he?Array.from((x(),A).subarray(Number(he)>>>0,Number(ye)>>>0)):[],activation:ke(_e)})},934696:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e,mt)=>{t.ac("ConvTranspose",o,{format:W?"NHWC":"NCHW",autoPad:p,dilations:[m],group:c,kernelShape:[b],pads:[S,z],strides:[M],wIsConst:()=>!!(x(),Y)[G>>>0],outputPadding:ae?Array.from((x(),A).subarray(Number(ae)>>>0,Number(he)>>>0)):[],outputShape:ye?Array.from((x(),A).subarray(Number(ye)>>>0,Number(_e)>>>0)):[],activation:ke(mt)})},935129:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e)=>{t.ac("ConvTranspose",o,{format:M?"NHWC":"NCHW",autoPad:p,dilations:Array.from((x(),A).subarray(Number(m)>>>0,2+(Number(m)>>>0)>>>0)),group:c,kernelShape:Array.from((x(),A).subarray(Number(b)>>>0,2+(Number(b)>>>0)>>>0)),pads:Array.from((x(),A).subarray(Number(S)>>>0,4+(Number(S)>>>0)>>>0)),strides:Array.from((x(),A).subarray(Number(z)>>>0,2+(Number(z)>>>0)>>>0)),wIsConst:()=>!!(x(),Y)[W>>>0],outputPadding:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],outputShape:he?Array.from((x(),A).subarray(Number(he)>>>0,Number(ye)>>>0)):[],activation:ke(_e)})},935790:(o,p)=>{t.ac("GlobalAveragePool",o,{format:p?"NHWC":"NCHW"})},935881:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e)=>{t.ac("AveragePool",o,{format:_e?"NHWC":"NCHW",auto_pad:p,ceil_mode:m,count_include_pad:c,storage_order:b,dilations:S?Array.from((x(),A).subarray(Number(S)>>>0,Number(z)>>>0)):[],kernel_shape:M?Array.from((x(),A).subarray(Number(M)>>>0,Number(W)>>>0)):[],pads:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],strides:he?Array.from((x(),A).subarray(Number(he)>>>0,Number(ye)>>>0)):[]})},936360:(o,p)=>{t.ac("GlobalAveragePool",o,{format:p?"NHWC":"NCHW"})},936451:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e)=>{t.ac("AveragePool",o,{format:_e?"NHWC":"NCHW",auto_pad:p,ceil_mode:m,count_include_pad:c,storage_order:b,dilations:S?Array.from((x(),A).subarray(Number(S)>>>0,Number(z)>>>0)):[],kernel_shape:M?Array.from((x(),A).subarray(Number(M)>>>0,Number(W)>>>0)):[],pads:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],strides:he?Array.from((x(),A).subarray(Number(he)>>>0,Number(ye)>>>0)):[]})},936930:(o,p)=>{t.ac("GlobalMaxPool",o,{format:p?"NHWC":"NCHW"})},937017:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e)=>{t.ac("MaxPool",o,{format:_e?"NHWC":"NCHW",auto_pad:p,ceil_mode:m,count_include_pad:c,storage_order:b,dilations:S?Array.from((x(),A).subarray(Number(S)>>>0,Number(z)>>>0)):[],kernel_shape:M?Array.from((x(),A).subarray(Number(M)>>>0,Number(W)>>>0)):[],pads:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],strides:he?Array.from((x(),A).subarray(Number(he)>>>0,Number(ye)>>>0)):[]})},937492:(o,p)=>{t.ac("GlobalMaxPool",o,{format:p?"NHWC":"NCHW"})},937579:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e)=>{t.ac("MaxPool",o,{format:_e?"NHWC":"NCHW",auto_pad:p,ceil_mode:m,count_include_pad:c,storage_order:b,dilations:S?Array.from((x(),A).subarray(Number(S)>>>0,Number(z)>>>0)):[],kernel_shape:M?Array.from((x(),A).subarray(Number(M)>>>0,Number(W)>>>0)):[],pads:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],strides:he?Array.from((x(),A).subarray(Number(he)>>>0,Number(ye)>>>0)):[]})},938054:(o,p,m,c,b)=>{t.ac("Gemm",o,{alpha:p,beta:m,transA:c,transB:b})},938158:o=>{t.ac("MatMul",o,void 0)},938212:(o,p,m,c)=>{t.ac("ArgMax",o,{keepDims:!!p,selectLastIndex:!!m,axis:c})},938320:(o,p,m,c)=>{t.ac("ArgMin",o,{keepDims:!!p,selectLastIndex:!!m,axis:c})},938428:(o,p)=>{t.ac("Softmax",o,{axis:p})},938491:(o,p)=>{t.ac("Concat",o,{axis:p})},938551:(o,p,m,c,b)=>{t.ac("Split",o,{axis:p,numOutputs:m,splitSizes:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},938707:o=>{t.ac("Expand",o,void 0)},938761:(o,p)=>{t.ac("Gather",o,{axis:Number(p)})},938832:(o,p)=>{t.ac("GatherElements",o,{axis:Number(p)})},938911:(o,p)=>{t.ac("GatherND",o,{batch_dims:Number(p)})},938990:(o,p,m,c,b,S,z,M,W,G,ae)=>{t.ac("Resize",o,{antialias:p,axes:m?Array.from((x(),A).subarray(Number(m)>>>0,Number(c)>>>0)):[],coordinateTransformMode:ke(b),cubicCoeffA:S,excludeOutside:z,extrapolationValue:M,keepAspectRatioPolicy:ke(W),mode:ke(G),nearestMode:ke(ae)})},939352:(o,p,m,c,b,S,z)=>{t.ac("Slice",o,{starts:p?Array.from((x(),A).subarray(Number(p)>>>0,Number(m)>>>0)):[],ends:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[],axes:S?Array.from((x(),A).subarray(Number(S)>>>0,Number(z)>>>0)):[]})},939616:o=>{t.ac("Tile",o,void 0)},939668:(o,p,m)=>{t.ac("InstanceNormalization",o,{epsilon:p,format:m?"NHWC":"NCHW"})},939782:(o,p,m)=>{t.ac("InstanceNormalization",o,{epsilon:p,format:m?"NHWC":"NCHW"})},939896:o=>{t.ac("Range",o,void 0)},939949:(o,p)=>{t.ac("Einsum",o,{equation:ke(p)})},940030:(o,p,m,c,b)=>{t.ac("Pad",o,{mode:p,value:m,pads:c?Array.from((x(),A).subarray(Number(c)>>>0,Number(b)>>>0)):[]})},940173:(o,p,m,c,b,S)=>{t.ac("BatchNormalization",o,{epsilon:p,momentum:m,spatial:!!b,trainingMode:!!c,format:S?"NHWC":"NCHW"})},940342:(o,p,m,c,b,S)=>{t.ac("BatchNormalization",o,{epsilon:p,momentum:m,spatial:!!b,trainingMode:!!c,format:S?"NHWC":"NCHW"})},940511:(o,p,m)=>{t.ac("CumSum",o,{exclusive:Number(p),reverse:Number(m)})},940608:(o,p,m)=>{t.ac("DequantizeLinear",o,{axis:p,blockSize:m})},940698:(o,p,m,c,b)=>{t.ac("GridSample",o,{align_corners:p,mode:ke(m),padding_mode:ke(c),format:b?"NHWC":"NCHW"})},940868:(o,p,m,c,b)=>{t.ac("GridSample",o,{align_corners:p,mode:ke(m),padding_mode:ke(c),format:b?"NHWC":"NCHW"})},941038:(o,p)=>{t.ac("ScatterND",o,{reduction:ke(p)})},941123:(o,p,m,c,b,S,z,M,W)=>{t.ac("Attention",o,{numHeads:p,isUnidirectional:m,maskFilterValue:c,scale:b,doRotary:S,qkvHiddenSizes:z?Array.from((x(),A).subarray(Number(M)>>>0,Number(M)+z>>>0)):[],pastPresentShareBuffer:!!W})},941395:o=>{t.ac("BiasAdd",o,void 0)},941450:o=>{t.ac("BiasSplitGelu",o,void 0)},941511:o=>{t.ac("FastGelu",o,void 0)},941567:(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e,mt,Wi)=>{t.ac("Conv",o,{format:he?"NHWC":"NCHW",auto_pad:p,dilations:m?Array.from((x(),A).subarray(Number(m)>>>0,Number(c)>>>0)):[],group:b,kernel_shape:S?Array.from((x(),A).subarray(Number(S)>>>0,Number(z)>>>0)):[],pads:M?Array.from((x(),A).subarray(Number(M)>>>0,Number(W)>>>0)):[],strides:G?Array.from((x(),A).subarray(Number(G)>>>0,Number(ae)>>>0)):[],w_is_const:()=>!!(x(),Y)[Number(ye)>>>0],activation:ke(_e),activation_params:mt?Array.from((x(),re).subarray(Number(mt)>>>0,Number(Wi)>>>0)):[]})},942151:o=>{t.ac("Gelu",o,void 0)},942203:(o,p,m,c,b,S,z,M,W)=>{t.ac("GroupQueryAttention",o,{numHeads:p,kvNumHeads:m,scale:c,softcap:b,doRotary:S,rotaryInterleaved:z,smoothSoftmax:M,localWindowSize:W})},942420:(o,p,m,c)=>{t.ac("LayerNormalization",o,{axis:p,epsilon:m,simplified:!!c})},942531:(o,p,m,c)=>{t.ac("LayerNormalization",o,{axis:p,epsilon:m,simplified:!!c})},942642:(o,p,m,c,b,S)=>{t.ac("MatMulNBits",o,{k:p,n:m,accuracyLevel:c,bits:b,blockSize:S})},942769:(o,p,m,c,b,S)=>{t.ac("MultiHeadAttention",o,{numHeads:p,isUnidirectional:m,maskFilterValue:c,scale:b,doRotary:S})},942928:(o,p)=>{t.ac("QuickGelu",o,{alpha:p})},942992:(o,p,m,c,b)=>{t.ac("RotaryEmbedding",o,{interleaved:!!p,numHeads:m,rotaryEmbeddingDim:c,scale:b})},943131:(o,p,m)=>{t.ac("SkipLayerNormalization",o,{epsilon:p,simplified:!!m})},943233:(o,p,m)=>{t.ac("SkipLayerNormalization",o,{epsilon:p,simplified:!!m})},943335:(o,p,m,c)=>{t.ac("GatherBlockQuantized",o,{gatherAxis:p,quantizeAxis:m,blockSize:c})},943456:o=>{t.Id(o)},943490:(o,p)=>t.Kd(Number(o),Number(p),t.$c.Nd,t.$c.errors)};function zg(o,p,m){return fs(async()=>{await t.Gd(Number(o),Number(p),Number(m))})}function Ag(){return typeof wasmOffsetConverter<"u"}function Og(o,p,m,c){var b=oe();try{return Zs(o,p,m,c)}catch(S){if(se(b),S!==S+0)throw S;le(1,0)}}function Rg(o,p,m){var c=oe();try{return Hs(o,p,m)}catch(b){if(se(c),b!==b+0)throw b;le(1,0)}}function Mg(o,p,m){var c=oe();try{qs(o,p,m)}catch(b){if(se(c),b!==b+0)throw b;le(1,0)}}function Bg(o,p){var m=oe();try{return Pi(o,p)}catch(c){if(se(m),c!==c+0)throw c;le(1,0)}}function Ng(o){var p=oe();try{Vs(o)}catch(m){if(se(p),m!==m+0)throw m;le(1,0)}}function Dg(o,p,m,c,b,S,z){var M=oe();try{return js(o,p,m,c,b,S,z)}catch(W){if(se(M),W!==W+0)throw W;le(1,0)}}function Pg(o,p){var m=oe();try{Ys(o,p)}catch(c){if(se(m),c!==c+0)throw c;le(1,0)}}function Ug(o,p,m,c,b,S){var z=oe();try{Gs(o,p,m,c,b,S)}catch(M){if(se(z),M!==M+0)throw M;le(1,0)}}function Wg(o,p,m,c){var b=oe();try{Qs(o,p,m,c)}catch(S){if(se(b),S!==S+0)throw S;le(1,0)}}function Lg(o,p,m,c,b){var S=oe();try{Fs(o,p,m,c,b)}catch(z){if(se(S),z!==z+0)throw z;le(1,0)}}function qg(o,p,m,c,b,S,z){var M=oe();try{Js(o,p,m,c,b,S,z)}catch(W){if(se(M),W!==W+0)throw W;le(1,0)}}function Vg(o,p,m,c,b,S,z){var M=oe();try{eo(o,p,m,c,b,S,z)}catch(W){if(se(M),W!==W+0)throw W;le(1,0)}}function Gg(o,p,m,c,b,S,z,M){var W=oe();try{no(o,p,m,c,b,S,z,M)}catch(G){if(se(W),G!==G+0)throw G;le(1,0)}}function Fg(o,p,m,c,b){var S=oe();try{return Xs(o,p,m,c,b)}catch(z){if(se(S),z!==z+0)throw z;le(1,0)}}function Hg(o,p,m,c,b,S,z,M){var W=oe();try{ao(o,p,m,c,b,S,z,M)}catch(G){if(se(W),G!==G+0)throw G;le(1,0)}}function Kg(o,p,m,c,b,S,z,M,W,G,ae,he){var ye=oe();try{to(o,p,m,c,b,S,z,M,W,G,ae,he)}catch(_e){if(se(ye),_e!==_e+0)throw _e;le(1,0)}}function jg(o,p,m,c,b,S){var z=oe();try{return ro(o,p,m,c,b,S)}catch(M){if(se(z),M!==M+0)throw M;le(1,0)}}function Qg(o,p,m){var c=oe();try{return so(o,p,m)}catch(b){if(se(c),b!==b+0)throw b;return le(1,0),0n}}function Zg(o,p,m,c,b,S,z,M,W){var G=oe();try{Ks(o,p,m,c,b,S,z,M,W)}catch(ae){if(se(G),ae!==ae+0)throw ae;le(1,0)}}function Yg(o){var p=oe();try{return oo(o)}catch(m){if(se(p),m!==m+0)throw m;le(1,0)}}function Xg(o,p,m){var c=oe();try{return uo(o,p,m)}catch(b){if(se(c),b!==b+0)throw b;le(1,0)}}function Jg(o,p){var m=oe();try{return So(o,p)}catch(c){if(se(m),c!==c+0)throw c;return le(1,0),0n}}function ey(o,p,m,c,b){var S=oe();try{lo(o,p,m,c,b)}catch(z){if(se(S),z!==z+0)throw z;le(1,0)}}function ty(o){var p=oe();try{return po(o)}catch(m){if(se(p),m!==m+0)throw m;return le(1,0),0n}}function ry(o,p,m,c,b,S){var z=oe();try{return yo(o,p,m,c,b,S)}catch(M){if(se(z),M!==M+0)throw M;le(1,0)}}function iy(o,p,m,c,b,S){var z=oe();try{return wo(o,p,m,c,b,S)}catch(M){if(se(z),M!==M+0)throw M;le(1,0)}}function ny(o,p,m,c,b,S,z,M){var W=oe();try{return io(o,p,m,c,b,S,z,M)}catch(G){if(se(W),G!==G+0)throw G;le(1,0)}}function ay(o,p,m,c,b){var S=oe();try{return _o(o,p,m,c,b)}catch(z){if(se(S),z!==z+0)throw z;return le(1,0),0n}}function sy(o,p,m,c){var b=oe();try{return bo(o,p,m,c)}catch(S){if(se(b),S!==S+0)throw S;le(1,0)}}function oy(o,p,m,c){var b=oe();try{return vo(o,p,m,c)}catch(S){if(se(b),S!==S+0)throw S;le(1,0)}}function uy(o,p,m,c,b,S,z,M,W,G,ae,he){var ye=oe();try{return $o(o,p,m,c,b,S,z,M,W,G,ae,he)}catch(_e){if(se(ye),_e!==_e+0)throw _e;le(1,0)}}function ly(o,p,m,c,b,S,z,M,W,G,ae){var he=oe();try{mo(o,p,m,c,b,S,z,M,W,G,ae)}catch(ye){if(se(he),ye!==ye+0)throw ye;le(1,0)}}function dy(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e,mt,Wi){var gy=oe();try{go(o,p,m,c,b,S,z,M,W,G,ae,he,ye,_e,mt,Wi)}catch(Li){if(se(gy),Li!==Li+0)throw Li;le(1,0)}}function py(o,p,m,c){var b=oe();try{return xo(o,p,m,c)}catch(S){if(se(b),S!==S+0)throw S;le(1,0)}}function cy(o,p,m,c,b){var S=oe();try{return To(o,p,m,c,b)}catch(z){if(se(S),z!==z+0)throw z;le(1,0)}}function hy(o,p,m){var c=oe();try{return co(o,p,m)}catch(b){if(se(c),b!==b+0)throw b;le(1,0)}}function fy(o,p,m){var c=oe();try{return ho(o,p,m)}catch(b){if(se(c),b!==b+0)throw b;le(1,0)}}function my(o,p,m,c){var b=oe();try{fo(o,p,m,c)}catch(S){if(se(b),S!==S+0)throw S;le(1,0)}}function Pr(){if(0<Re)xt=Pr;else if(n)v?.(t),ce();else{for(var o=$e;0<o.length;)o.shift()(t);0<Re?xt=Pr:(t.calledRun=!0,C||(ce(),v?.(t)))}}return n||(st=await Ve(),Pr()),t.PTR_SIZE=4,V?t:new Promise((o,p)=>{v=o,T=p})}var Ye,ra,Iy,Ey,Cy,zy,P,Yt,Ay,$r,sr,gt,Bt,Ao,dp,pp,Oy,cp,Ry,Vi,Ce,hp,be,My,fp,mp,By,Vr,gp,yp,wp,_p,bp,Ny,Ot,yr,Gi,vp,Dy,$p,xp,Py,Ne,ia,Xe,Tp,xr,Fi,Je,We,_t,bt,Sp,kp,Uy,na,Wy,Ly,qy,Vy,Gy,Ep,Le,aa,Cp,Hi,Ki,zp,Fy,Ap,Op,Ro,Hy,ji,Wn,Mo,Be,Rp,Gr,Bo,No,Qi,Do,Zi,Mp,Yi,Bp,sa,Xi,Fr,or,Ji,Po,Uo,Wo,oa,we,Ut,Ze,ni,ge,ua,Np,Ky,Lo,qo,Vo,ur,Go,Dp,jy,Rt,ut,Mt,pi,ai,la,da,Ln,ee,pa,Pp,Fo,Ho,Ko,jo,ca,Qo,de,lt,Zo,Qt,R,si,Up,Wp,Lp,ie,ha,qp,en,qn,tn,Yo,rn,Xo,nn,an,sn,Jo,Vp,Qy,lr,eu,Gp,Zy,fa,on,Hr,Kr,tu,ru,un,Vn,iu,Fp,Yy,nu,me,Te,Zt,jr,Ee,Ae,Z,xe,Gn,jt,vt,K,dr,B,F,Hp,ma,au,Kp,ne,su,ln,ou,uu,lu,du,De,jp,Qp,$t,pu,cu,hu,fu,mu,gu,yu,wu,_u,bu,Fe,Zp,Yp,Xp,Jp,ec,tc,rc,ic,nc,ac,Xy,He,vu,oi,Fn,Ke,$u,xu,Tu,Su,ku,Iu,Eu,Cu,zu,Au,je,sc,oc,uc,lc,dc,pc,cc,hc,fc,mc,ga,dn,gc,yc,Hn,Jy,Ou,Qr,Ru,Mu,Bu,Tr,Nu,wc,ya,Du,Pu,Uu,_c,e0,Wu,Lu,bc,t0,qu,fe,vc,$c,xc,Tc,Sc,kc,Ic,Ec,Cc,Vu,zc,Ac,Oc,Rc,_r,Mc,ii,Bc,Nc,Dc,Pc,Uc,Wc,Lc,qc,Vc,Gc,Fc,Hc,Kc,jc,Qc,Zc,pn,Yc,Kn,jn,Xc,Jc,eh,Gu,Fu,th,wa,Hu,Ku,rh,r0,ju,Qu,Qe,ih,nh,ah,sh,oh,uh,lh,dh,ph,ch,i0,Zu,Yu,Xu,Ju,hh,fh,n0,Nt,Dt,Pt,_a,Wt,ze,mh,ba,gh,a0,vr,va,$a,el,tl,Qn,cn,rl,Zn,il,ui,xa,nl,yh,s0,al,hn,pr,sl,fn,ol,wh,_h,o0,bh,vh,u0,ul,Zr,ll,Yr,Yn,mn,dl,pl,Xn,l0,$h,d0,cl,hl,fl,gn,xh,ml,yn,gl,Th,p0,yl,Sh,kh,c0,wl,_l,bl,Ih,Eh,h0,Xr,cr,wn,vl,$l,xl,Tl,_n,Sl,Ch,zh,f0,kl,bn,Il,El,Ah,m0,Cl,Oh,g0,zl,Al,Rh,Mh,y0,Ol,Bh,Nh,w0,Rl,Ml,Dh,Ph,_0,Bl,Nl,Uh,Wh,b0,Dl,Pl,Lh,qh,v0,it,ot,Et,Ct,Ul,Wl,Ll,ql,Vl,Gl,Fl,Hl,Vh,Gh,$0,Oe,Kl,Fh,vn,jl,br,Hh,Kh,Ql,Zl,Yl,Xl,Jn,jh,Qh,Zh,Jl,li,Yh,Xh,ed,td,$n,rd,Jh,x0,xn,id,nd,ef,T0,ad,sd,tf,S0,od,rf,k0,ud,ld,dd,nf,af,I0,pd,cd,hd,fd,md,gd,yd,wd,sf,E0,hr,Tn,Sn,kn,In,_d,bd,En,Cn,of,uf,zn,lf,df,An,pf,cf,hf,ff,C0,vd,$d,mf,gf,z0,xd,Td,yf,A0,Sd,kd,wf,_f,O0,Id,Ed,Cd,On,zd,Ad,Od,Rd,Md,Bd,Nd,Dd,Rn,Pd,Ud,Wd,Ld,qd,bf,vf,R0,Vd,Gd,$f,M0,Fd,fr,Hd,Mn,Kd,jd,xf,Tf,B0,Qd,Zd,Sf,kf,N0,Bn,Yd,Xd,Jd,If,D0,ep,tp,Ef,P0,Cf,U0,zf,W0,Af,rp,ip,np,Of,L0,Rf,Jr,ap,Mf,q0,sp,Ta,Sa,yt,op,Nn,di,ka,Ia,Dn,Ea,Ca,za,Nf,wt,Ue,Kt,mr,gr,ei,Pn,ti,zt,At,up,Df,Pf,Uf,Wf,Lf,qf,Vf,Gf,Un,lp,Ff,V0,Hf,ea,ta,Kf,G0,F0,H0,Qf=vy(()=>{Ye={};ra=Object.defineProperty,Iy=Object.getOwnPropertyDescriptor,Ey=Object.getOwnPropertyNames,Cy=Object.prototype.hasOwnProperty,zy=(e=>typeof It<"u"?It:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof It<"u"?It:t)[r]}):e)(function(e){if(typeof It<"u")return It.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),P=(e,t)=>()=>(e&&(t=e(e=0)),t),Yt=(e,t)=>{for(var r in t)ra(e,r,{get:t[r],enumerable:!0})},Ay=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of Ey(t))!Cy.call(e,n)&&n!==r&&ra(e,n,{get:()=>t[n],enumerable:!(i=Iy(t,n))||i.enumerable});return e},$r=e=>Ay(ra({},"__esModule",{value:!0}),e),pp=P(()=>{"use strict";sr=new Map,gt=[],Bt=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let i=sr.get(e);if(i===void 0)sr.set(e,{backend:t,priority:r});else{if(i.priority>r)return;if(i.priority===r&&i.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let n=gt.indexOf(e);n!==-1&>.splice(n,1);for(let a=0;a<gt.length;a++)if(sr.get(gt[a]).priority<=r){gt.splice(a,0,e);return}gt.push(e)}return}throw new TypeError("not a valid backend")},Ao=async e=>{let t=sr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(i){return r||(t.error=`${i}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},dp=async e=>{let t=e.executionProviders||[],r=t.map(l=>typeof l=="string"?l:l.name),i=r.length===0?gt:r,n,a=[],s=new Set;for(let l of i){let d=await Ao(l);typeof d=="string"?a.push({name:l,err:d}):(n||(n=d),n===d&&s.add(l))}if(!n)throw new Error(`no available backend found. ERR: ${a.map(l=>`[${l.name}] ${l.err}`).join(", ")}`);for(let{name:l,err:d}of a)r.includes(l)&&console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${d}`);let u=t.filter(l=>s.has(typeof l=="string"?l:l.name));return[n,new Proxy(e,{get:(l,d)=>d==="executionProviders"?u:Reflect.get(l,d)})]}}),Oy=P(()=>{"use strict";pp()}),Ry=P(()=>{"use strict";cp="1.24.2"}),hp=P(()=>{"use strict";Ry(),Vi="warning",Ce={wasm:{},webgl:{},webgpu:{},versions:{common:cp},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Vi=e}},get logLevel(){return Vi}},Object.defineProperty(Ce,"logLevel",{enumerable:!0})}),My=P(()=>{"use strict";hp(),be=Ce}),By=P(()=>{"use strict";fp=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let i=r.getContext("2d");if(i!=null){let n,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(n=e.dims[2],a=e.dims[3]):(n=e.dims[3],a=e.dims[2]);let s=t?.format!==void 0?t.format:"RGB",u=t?.norm,l,d;u===void 0||u.mean===void 0?l=[255,255,255,255]:typeof u.mean=="number"?l=[u.mean,u.mean,u.mean,u.mean]:(l=[u.mean[0],u.mean[1],u.mean[2],0],u.mean[3]!==void 0&&(l[3]=u.mean[3])),u===void 0||u.bias===void 0?d=[0,0,0,0]:typeof u.bias=="number"?d=[u.bias,u.bias,u.bias,u.bias]:(d=[u.bias[0],u.bias[1],u.bias[2],0],u.bias[3]!==void 0&&(d[3]=u.bias[3]));let f=a*n,h=0,g=f,w=f*2,y=-1;s==="RGBA"?(h=0,g=f,w=f*2,y=f*3):s==="RGB"?(h=0,g=f,w=f*2):s==="RBG"&&(h=0,w=f,g=f*2);for(let v=0;v<a;v++)for(let T=0;T<n;T++){let $=(e.data[h++]-d[0])*l[0],_=(e.data[g++]-d[1])*l[1],I=(e.data[w++]-d[2])*l[2],k=y===-1?255:(e.data[y++]-d[3])*l[3];i.fillStyle="rgba("+$+","+_+","+I+","+k+")",i.fillRect(T,v,1,1)}if("toDataURL"in r)return r.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can not access image data")},mp=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),i;if(r!=null){let n,a,s;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(n=e.dims[2],a=e.dims[1],s=e.dims[3]):(n=e.dims[3],a=e.dims[2],s=e.dims[1]);let u=t!==void 0&&t.format!==void 0?t.format:"RGB",l=t?.norm,d,f;l===void 0||l.mean===void 0?d=[255,255,255,255]:typeof l.mean=="number"?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],255],l.mean[3]!==void 0&&(d[3]=l.mean[3])),l===void 0||l.bias===void 0?f=[0,0,0,0]:typeof l.bias=="number"?f=[l.bias,l.bias,l.bias,l.bias]:(f=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(f[3]=l.bias[3]));let h=a*n;if(t!==void 0&&(t.format!==void 0&&s===4&&t.format!=="RGBA"||s===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let g=4,w=0,y=1,v=2,T=3,$=0,_=h,I=h*2,k=-1;u==="RGBA"?($=0,_=h,I=h*2,k=h*3):u==="RGB"?($=0,_=h,I=h*2):u==="RBG"&&($=0,I=h,_=h*2),i=r.createImageData(n,a);for(let E=0;E<a*n;w+=g,y+=g,v+=g,T+=g,E++)i.data[w]=(e.data[$++]-f[0])*d[0],i.data[y]=(e.data[_++]-f[1])*d[1],i.data[v]=(e.data[I++]-f[2])*d[2],i.data[T]=k===-1?255:(e.data[k++]-f[3])*d[3]}else throw new Error("Can not access image data");return i}}),Ny=P(()=>{"use strict";ia(),Vr=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:i}=t,n=t.norm??{mean:255,bias:0},a,s;typeof n.mean=="number"?a=[n.mean,n.mean,n.mean,n.mean]:a=[n.mean[0],n.mean[1],n.mean[2],n.mean[3]??255],typeof n.bias=="number"?s=[n.bias,n.bias,n.bias,n.bias]:s=[n.bias[0],n.bias[1],n.bias[2],n.bias[3]??0];let u=t.format!==void 0?t.format:"RGBA",l=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",d=r*i,f=l==="RGBA"?new Float32Array(d*4):new Float32Array(d*3),h=4,g=0,w=1,y=2,v=3,T=0,$=d,_=d*2,I=-1;u==="RGB"&&(h=3,g=0,w=1,y=2,v=-1),l==="RGBA"?I=d*3:l==="RBG"?(T=0,_=d,$=d*2):l==="BGR"&&(_=0,$=d,T=d*2);for(let k=0;k<d;k++,g+=h,y+=h,w+=h,v+=h)f[T++]=(e[g]+s[0])/a[0],f[$++]=(e[w]+s[1])/a[1],f[_++]=(e[y]+s[2])/a[2],I!==-1&&v!==-1&&(f[I++]=(e[v]+s[3])/a[3]);return l==="RGBA"?new Ne("float32",f,[1,4,r,i]):new Ne("float32",f,[1,3,r,i])},gp=async(e,t)=>{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,i=typeof ImageData<"u"&&e instanceof ImageData,n=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,a=typeof e=="string",s,u=t??{},l=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},d=f=>typeof HTMLCanvasElement<"u"&&f instanceof HTMLCanvasElement||f instanceof OffscreenCanvas?f.getContext("2d"):null;if(r){let f=l();f.width=e.width,f.height=e.height;let h=d(f);if(h!=null){let g=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(g=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(u=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");u.tensorFormat="RGBA",u.height=g,u.width=w}else u.tensorFormat="RGBA",u.height=g,u.width=w;h.drawImage(e,0,0),s=h.getImageData(0,0,w,g).data}else throw new Error("Can not access image data")}else if(i){let f,h;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(f=t.resizedHeight,h=t.resizedWidth):(f=e.height,h=e.width),t!==void 0&&(u=t),u.format="RGBA",u.height=f,u.width=h,t!==void 0){let g=l();g.width=h,g.height=f;let w=d(g);if(w!=null)w.putImageData(e,0,0),s=w.getImageData(0,0,h,f).data;else throw new Error("Can not access image data")}else s=e.data}else if(n){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let f=l();f.width=e.width,f.height=e.height;let h=d(f);if(h!=null){let g=e.height,w=e.width;return h.drawImage(e,0,0,w,g),s=h.getImageData(0,0,w,g).data,u.height=g,u.width=w,Vr(s,u)}else throw new Error("Can not access image data")}else{if(a)return new Promise((f,h)=>{let g=l(),w=d(g);if(!e||!w)return h();let y=new Image;y.crossOrigin="Anonymous",y.src=e,y.onload=()=>{g.width=y.width,g.height=y.height,w.drawImage(y,0,0,g.width,g.height);let v=w.getImageData(0,0,g.width,g.height);u.height=g.height,u.width=g.width,f(Vr(v.data,u))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(s!==void 0)return Vr(s,u);throw new Error("Input data provided is not supported - aborted tensor creation")},yp=(e,t)=>{let{width:r,height:i,download:n,dispose:a}=t,s=[1,i,r,4];return new Ne({location:"texture",type:"float32",texture:e,dims:s,download:n,dispose:a})},wp=(e,t)=>{let{dataType:r,dims:i,download:n,dispose:a}=t;return new Ne({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:i,download:n,dispose:a})},_p=(e,t)=>{let{dataType:r,dims:i,download:n,dispose:a}=t;return new Ne({location:"ml-tensor",type:r??"float32",mlTensor:e,dims:i,download:n,dispose:a})},bp=(e,t,r)=>new Ne({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})}),Dy=P(()=>{"use strict";Ot=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),yr=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),Gi=!1,vp=()=>{if(!Gi){Gi=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=globalThis.Float16Array,i=typeof r<"u"&&r.from;e&&(Ot.set("int64",BigInt64Array),yr.set(BigInt64Array,"int64")),t&&(Ot.set("uint64",BigUint64Array),yr.set(BigUint64Array,"uint64")),i?(Ot.set("float16",r),yr.set(r,"float16")):Ot.set("float16",Uint16Array)}}}),Py=P(()=>{"use strict";ia(),$p=e=>{let t=1;for(let r=0;r<e.length;r++){let i=e[r];if(typeof i!="number"||!Number.isSafeInteger(i))throw new TypeError(`dims[${r}] must be an integer, got: ${i}`);if(i<0)throw new RangeError(`dims[${r}] must be a non-negative integer, got: ${i}`);t*=i}return t},xp=(e,t)=>{switch(e.location){case"cpu":return new Ne(e.type,e.data,t);case"cpu-pinned":return new Ne({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new Ne({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new Ne({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new Ne({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),ia=P(()=>{"use strict";By(),Ny(),Dy(),Py(),Ne=class{constructor(e,t,r){vp();let i,n;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,i=e.type,n=e.dims,e.location){case"cpu-pinned":{let s=Ot.get(i);if(!s)throw new TypeError(`unsupported type "${i}" to create tensor from pinned buffer`);if(!(e.data instanceof s))throw new TypeError(`buffer should be of type ${s.name}`);this.cpuData=e.data;break}case"texture":{if(i!=="float32")throw new TypeError(`unsupported type "${i}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(i!=="float32"&&i!=="float16"&&i!=="int32"&&i!=="int64"&&i!=="uint32"&&i!=="uint64"&&i!=="int8"&&i!=="uint8"&&i!=="bool"&&i!=="uint4"&&i!=="int4")throw new TypeError(`unsupported type "${i}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let s,u;if(typeof e=="string")if(i=e,u=r,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");s=t}else{let l=Ot.get(e);if(l===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&l===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${l.name} as data.`);e==="uint64"||e==="int64"?s=l.from(t,BigInt):s=l.from(t)}else if(t instanceof l)s=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")s=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&l!==Uint16Array)s=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${i} tensor's data must be type of ${l}`)}else if(u=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let l=typeof e[0];if(l==="string")i="string",s=e;else if(l==="boolean")i="bool",s=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${l}.`)}else if(e instanceof Uint8ClampedArray)i="uint8",s=Uint8Array.from(e);else{let l=yr.get(e.constructor);if(l===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);i=l,s=e}if(u===void 0)u=[s.length];else if(!Array.isArray(u))throw new TypeError("A tensor's dims must be a number array");n=u,this.cpuData=s,this.dataLocation="cpu"}let a=$p(n);if(this.cpuData&&a!==this.cpuData.length&&!((i==="uint4"||i==="int4")&&Math.ceil(a/2)===this.cpuData.length))throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=i,this.dims=n,this.size=a}static async fromImage(e,t){return gp(e,t)}static fromTexture(e,t){return yp(e,t)}static fromGpuBuffer(e,t){return wp(e,t)}static fromMLTensor(e,t){return _p(e,t)}static fromPinnedBuffer(e,t,r){return bp(e,t,r)}toDataURL(e){return fp(this,e)}toImageData(e){return mp(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return xp(this,e)}}}),Tp=P(()=>{"use strict";ia(),Xe=Ne}),Sp=P(()=>{"use strict";hp(),xr=(e,t)=>{(typeof Ce.trace>"u"?!Ce.wasm.trace:!Ce.trace)||console.timeStamp(`${e}::ORT::${t}`)},Fi=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],i=!1;for(let n=0;n<r.length;n++){if(i&&!r[n].includes("TRACE_FUNC")){let a=`FUNC_${e}::${r[n].trim().split(" ")[1]}`;t&&(a+=`::${t}`),xr("CPU",a);return}r[n].includes("TRACE_FUNC")&&(i=!0)}},Je=e=>{(typeof Ce.trace>"u"?!Ce.wasm.trace:!Ce.trace)||Fi("BEGIN",e)},We=e=>{(typeof Ce.trace>"u"?!Ce.wasm.trace:!Ce.trace)||Fi("END",e)},_t=e=>{(typeof Ce.trace>"u"?!Ce.wasm.trace:!Ce.trace)||console.time(`ORT::${e}`)},bt=e=>{(typeof Ce.trace>"u"?!Ce.wasm.trace:!Ce.trace)||console.timeEnd(`ORT::${e}`)}}),Uy=P(()=>{"use strict";pp(),Tp(),Sp(),kp=class Ip{constructor(t){this.handler=t}async run(t,r,i){Je(),_t("InferenceSession.run");let n={},a={};if(typeof t!="object"||t===null||t instanceof Xe||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let s=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Xe)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");s=!1;for(let d of r){if(typeof d!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(d)===-1)throw new RangeError(`'fetches' contains invalid output name: ${d}.`);n[d]=null}if(typeof i=="object"&&i!==null)a=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let d=!1,f=Object.getOwnPropertyNames(r);for(let h of this.outputNames)if(f.indexOf(h)!==-1){let g=r[h];(g===null||g instanceof Xe)&&(d=!0,s=!1,n[h]=g)}if(d){if(typeof i=="object"&&i!==null)a=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else a=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let d of this.inputNames)if(typeof t[d]>"u")throw new Error(`input '${d}' is missing in 'feeds'.`);if(s)for(let d of this.outputNames)n[d]=null;let u=await this.handler.run(t,n,a),l={};for(let d in u)if(Object.hasOwnProperty.call(u,d)){let f=u[d];f instanceof Xe?l[d]=f:l[d]=new Xe(f.type,f.data,f.dims)}return bt("InferenceSession.run"),We(),l}async release(){return this.handler.dispose()}static async create(t,r,i,n){Je(),_t("InferenceSession.create");let a,s={};if(typeof t=="string"){if(a=t,typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(a=t,typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let f=t,h=0,g=t.byteLength;if(typeof r=="object"&&r!==null)s=r;else if(typeof r=="number"){if(h=r,!Number.isSafeInteger(h))throw new RangeError("'byteOffset' must be an integer.");if(h<0||h>=f.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${f.byteLength}).`);if(g=t.byteLength-h,typeof i=="number"){if(g=i,!Number.isSafeInteger(g))throw new RangeError("'byteLength' must be an integer.");if(g<=0||h+g>f.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${f.byteLength-h}].`);if(typeof n=="object"&&n!==null)s=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(typeof i<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");a=new Uint8Array(f,h,g)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[u,l]=await dp(s),d=await u.createInferenceSessionHandler(a,l);return bt("InferenceSession.create"),We(),new Ip(d)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Wy=P(()=>{"use strict";Uy(),na=kp}),Ly=P(()=>{"use strict"}),qy=P(()=>{"use strict"}),Vy=P(()=>{"use strict"}),Gy=P(()=>{"use strict"}),Ep={};Yt(Ep,{InferenceSession:()=>na,TRACE:()=>xr,TRACE_EVENT_BEGIN:()=>_t,TRACE_EVENT_END:()=>bt,TRACE_FUNC_BEGIN:()=>Je,TRACE_FUNC_END:()=>We,Tensor:()=>Xe,env:()=>be,registerBackend:()=>Bt});Le=P(()=>{"use strict";Oy(),My(),Wy(),Tp(),Ly(),qy(),Sp(),Vy(),Gy()}),aa=P(()=>{"use strict"}),Cp={};Yt(Cp,{default:()=>zp});Fy=P(()=>{"use strict";Nf(),Ut(),sa(),Hi="ort-wasm-proxy-worker",Ki=globalThis.self?.name===Hi,Ki&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":oa(r.wasm).then(()=>{Ta(r).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})})},i=>{postMessage({type:t,err:i})});break;case"init-ep":{let{epName:i,env:n}=r;Sa(n,i).then(()=>{postMessage({type:t})},a=>{postMessage({type:t,err:a})});break}case"copy-from":{let{buffer:i}=r,n=di(i);postMessage({type:t,out:n});break}case"create":{let{model:i,options:n}=r;ka(i,n).then(a=>{postMessage({type:t,out:a})},a=>{postMessage({type:t,err:a})});break}case"release":Ia(r),postMessage({type:t});break;case"run":{let{sessionId:i,inputIndices:n,inputs:a,outputIndices:s,options:u}=r;Ea(i,n,a,s,new Array(s.length).fill(null),u).then(l=>{l.some(d=>d[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:l},za([...a,...l]))},l=>{postMessage({type:t,err:l})});break}case"end-profiling":Ca(r),postMessage({type:t});break;default:}}catch(i){postMessage({type:t,err:i})}}),zp=Ki?null:e=>new Worker(e??Be,{type:"module",name:Hi})}),Ap={};Yt(Ap,{default:()=>Op});Hy=P(()=>{"use strict";Op=Oo,Ro=globalThis.self?.name?.startsWith("em-pthread"),Ro&&Oo()}),sa=P(()=>{"use strict";aa(),ji=typeof location>"u"?void 0:location.origin,Wn=Ye.url>"file:"&&Ye.url<"file;",Mo=()=>{if(Wn){let e=URL;return new URL(new e("ort.bundle.min.mjs",Ye.url).href,ji).href}return Ye.url},Be=Mo(),Rp=()=>{if(Be&&!Be.startsWith("blob:"))return Be.substring(0,Be.lastIndexOf("/")+1)},Gr=(e,t)=>{try{let r=t??Be;return(r?new URL(e,r):new URL(e)).origin===ji}catch{return!1}},Bo=(e,t)=>{let r=t??Be;try{return(r?new URL(e,r):new URL(e)).href}catch{return}},No=(e,t)=>`${t??"./"}${e}`,Qi=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},Do=async e=>(await import(e)).default,Zi=(Fy(),$r(Cp)).default,Mp=async()=>{if(!Be)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(Gr(Be))return[void 0,Zi()];let e=await Qi(Be);return[e,Zi(e)]},Yi=(Hy(),$r(Ap)).default,Bp=async(e,t,r,i)=>{let n=Yi&&!(e||t);if(n)if(Be)n=Gr(Be);else if(i&&!r)n=!0;else throw new Error("cannot determine the script source URL.");if(n)return[void 0,Yi];{let a="ort-wasm-simd-threaded.jsep.mjs",s=e??Bo(a,t),u=r&&s&&!Gr(s,t),l=u?await Qi(s):s??No(a,t);return[u?l:void 0,await Do(l)]}}}),Ut=P(()=>{"use strict";sa(),Fr=!1,or=!1,Ji=!1,Po=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Uo=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Wo=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},oa=async e=>{if(Fr)return Promise.resolve();if(or)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Ji)throw new Error("previous call to 'initializeWebAssembly()' failed.");or=!0;let t=e.initTimeout,r=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!Wo())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!Uo())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let i=Po();r>1&&!i&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=r=1);let n=e.wasmPaths,a=typeof n=="string"?n:void 0,s=n?.mjs,u=s?.href??s,l=n?.wasm,d=l?.href??l,f=e.wasmBinary,[h,g]=await Bp(u,a,r>1,!!f||!!d),w=!1,y=[];if(t>0&&y.push(new Promise(v=>{setTimeout(()=>{w=!0,v()},t)})),y.push(new Promise((v,T)=>{let $={numThreads:r};if(f)$.wasmBinary=f;else if(d||a)$.locateFile=_=>d??a+_;else if(u&&u.indexOf("blob:")!==0)$.locateFile=_=>new URL(_,u).href;else if(h){let _=Rp();_&&($.locateFile=I=>_+I)}g($).then(_=>{or=!1,Fr=!0,Xi=_,v(),h&&URL.revokeObjectURL(h)},_=>{or=!1,Ji=!0,T(_)})})),await Promise.race(y),w)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},we=()=>{if(Fr&&Xi)return Xi;throw new Error("WebAssembly is not initialized yet.")}}),ua=P(()=>{"use strict";Ut(),Ze=(e,t)=>{let r=we(),i=r.lengthBytesUTF8(e)+1,n=r._malloc(i);return r.stringToUTF8(e,n,i),t.push(n),n},ni=(e,t,r,i)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([n,a])=>{let s=t?t+n:n;if(typeof a=="object")ni(a,s+".",r,i);else if(typeof a=="string"||typeof a=="number")i(s,a.toString());else if(typeof a=="boolean")i(s,a?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof a}`)})},ge=e=>{let t=we(),r=t.stackSave();try{let i=t.PTR_SIZE,n=t.stackAlloc(2*i);t._OrtGetLastError(n,n+i);let a=Number(t.getValue(n,i===4?"i32":"i64")),s=t.getValue(n+i,"*"),u=s?t.UTF8ToString(s):"";throw new Error(`${e} ERROR_CODE: ${a}, ERROR_MESSAGE: ${u}`)}finally{t.stackRestore(r)}}}),Ky=P(()=>{"use strict";Ut(),ua(),Np=e=>{let t=we(),r=0,i=[],n=e||{};try{if(e?.logSeverityLevel===void 0)n.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)n.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(n.terminate=!1);let a=0;return e?.tag!==void 0&&(a=Ze(e.tag,i)),r=t._OrtCreateRunOptions(n.logSeverityLevel,n.logVerbosityLevel,!!n.terminate,a),r===0&&ge("Can't create run options."),e?.extra!==void 0&&ni(e.extra,"",new WeakSet,(s,u)=>{let l=Ze(s,i),d=Ze(u,i);t._OrtAddRunConfigEntry(r,l,d)!==0&&ge(`Can't set a run config entry: ${s} - ${u}.`)}),[r,i]}catch(a){throw r!==0&&t._OrtReleaseRunOptions(r),i.forEach(s=>t._free(s)),a}}}),jy=P(()=>{"use strict";Ut(),ua(),Lo=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},qo=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Vo=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},ur=(e,t,r,i)=>{let n=Ze(t,i),a=Ze(r,i);we()._OrtAddSessionConfigEntry(e,n,a)!==0&&ge(`Can't set a session config entry: ${t} - ${r}.`)},Go=async(e,t,r)=>{let i=t.executionProviders;for(let n of i){let a=typeof n=="string"?n:n.name,s=[];switch(a){case"webnn":if(a="WEBNN",typeof n!="string"){let h=n?.deviceType;h&&ur(e,"deviceType",h,r)}break;case"webgpu":if(a="JS",typeof n!="string"){let h=n;if(h?.preferredLayout){if(h.preferredLayout!=="NCHW"&&h.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${h.preferredLayout}`);ur(e,"preferredLayout",h.preferredLayout,r)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${a}`)}let u=Ze(a,r),l=s.length,d=0,f=0;if(l>0){d=we()._malloc(l*we().PTR_SIZE),r.push(d),f=we()._malloc(l*we().PTR_SIZE),r.push(f);for(let h=0;h<l;h++)we().setValue(d+h*we().PTR_SIZE,s[h][0],"*"),we().setValue(f+h*we().PTR_SIZE,s[h][1],"*")}await we()._OrtAppendExecutionProvider(e,u,d,f,l)!==0&&ge(`Can't append execution provider: ${a}.`)}},Dp=async e=>{let t=we(),r=0,i=[],n=e||{};Vo(n);try{let a=Lo(n.graphOptimizationLevel??"all"),s=qo(n.executionMode??"sequential"),u=typeof n.logId=="string"?Ze(n.logId,i):0,l=n.logSeverityLevel??2;if(!Number.isInteger(l)||l<0||l>4)throw new Error(`log severity level is not valid: ${l}`);let d=n.logVerbosityLevel??0;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log verbosity level is not valid: ${d}`);let f=typeof n.optimizedModelFilePath=="string"?Ze(n.optimizedModelFilePath,i):0;if(r=t._OrtCreateSessionOptions(a,!!n.enableCpuMemArena,!!n.enableMemPattern,s,!!n.enableProfiling,0,u,l,d,f),r===0&&ge("Can't create session options."),n.executionProviders&&await Go(r,n,i),n.enableGraphCapture!==void 0){if(typeof n.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${n.enableGraphCapture}`);ur(r,"enableGraphCapture",n.enableGraphCapture.toString(),i)}if(n.freeDimensionOverrides)for(let[h,g]of Object.entries(n.freeDimensionOverrides)){if(typeof h!="string")throw new Error(`free dimension override name must be a string: ${h}`);if(typeof g!="number"||!Number.isInteger(g)||g<0)throw new Error(`free dimension override value must be a non-negative integer: ${g}`);let w=Ze(h,i);t._OrtAddFreeDimensionOverride(r,w,g)!==0&&ge(`Can't set a free dimension override: ${h} - ${g}.`)}return n.extra!==void 0&&ni(n.extra,"",new WeakSet,(h,g)=>{ur(r,h,g,i)}),[r,i]}catch(a){throw r!==0&&t._OrtReleaseSessionOptions(r)!==0&&ge("Can't release session options."),i.forEach(s=>t._free(s)),a}}}),ee=P(()=>{"use strict";Rt=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},ut=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},Mt=(e,t)=>{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],i=typeof t=="number"?t:t.reduce((n,a)=>n*a,1);return r>0?Math.ceil(i*r):void 0},pi=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},ai=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},la=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",da=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Ln=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),Pp=P(()=>{"use strict";aa(),pa=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),i=r?parseInt(r,10):0;if(i<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let n=t.body.getReader(),a;try{a=new ArrayBuffer(i)}catch(u){if(u instanceof RangeError){let l=Math.ceil(i/65536);a=new WebAssembly.Memory({initial:l,maximum:l}).buffer}else throw u}let s=0;for(;;){let{done:u,value:l}=await n.read();if(u)break;let d=l.byteLength;new Uint8Array(a,s,d).set(l),s+=d}return new Uint8Array(a,0,i)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),lt=P(()=>{"use strict";ee(),Fo=["V","I","W","E","F"],Ho=(e,t)=>{console.log(`[${Fo[e]},${new Date().toISOString()}]${t}`)},ca=(e,t)=>{Ko=e,jo=t},Qo=(e,t)=>{let r=ai(e),i=ai(Ko);r>=i&&Ho(r,typeof t=="function"?t():t)},de=(...e)=>{jo&&Qo(...e)}}),ie=P(()=>{"use strict";Zo=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},Qt=class{static calcShape(e,t,r=!1){let i=e.length,n=t.length;if(i===0)return t;if(n===0)return e;let a=Math.max(e.length,t.length),s=new Array(a);if(r){if(i<2||n<2)return;let u=Zo.calcMatMulShape([e[i-2],e[i-1]],[t[n-2],t[n-1]]);if(u===void 0)return;[s[a-2],s[a-1]]=u}for(let u=r?3:1;u<=a;u++){let l=i-u<0?1:e[i-u],d=n-u<0?1:t[n-u];if(l!==d&&l>1&&d>1)return;let f=Math.max(l,d);if(l&&d)s[a-u]=Math.max(l,d);else{if(f>1)return;s[a-u]=0}}return s}static isValidBroadcast(e,t){let r=e.length,i=t.length;if(r>i)return!1;for(let n=1;n<=r;n++)if(e[r-n]!==1&&e[r-n]!==t[i-n])return!1;return!0}},R=class ri{static size(t){return ri.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let i=t.length;if(i===0)return[];let n=new Array(i),a=i-1;for(;a>=0;){if(t[a]%r===0){n[a]=t[a]/r;break}if(r%t[a]!==0)throw new Error("cannot convert shape");n[a]=1,r/=t[a],a--}for(a--;a>=0;a--)n[a]=t[a];return n}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return ri.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return ri.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,i){let n=1;for(let a=r;a<i;a++){if(t[a]<0)throw new Error("cannot get valid size from specified dimension range. Most likely the range contains negative values in them.");n*=Number(t[a])}return n}static computeStrides(t){let r=t.length;if(r===0)return[];if(r===1)return[1];let i=new Array(r);i[r-1]=1,i[r-2]=t[r-1];for(let n=r-3;n>=0;--n)i[n]=i[n+1]*t[n+1];return i}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(i=>this.normalizeAxis(i,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(i=>t[i]):t.slice().reverse()}static padShape(t,r){let i=t.length;return t.map((n,a)=>n+r[a]+r[a+i])}static areEqual(t,r){return t.length!==r.length?!1:t.every((i,n)=>i===r[n])}},si=class wr{static adjustPoolAttributes(t,r,i,n,a,s){if(!t&&i.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let u=0;u<r.length-2;u++)u>=i.length?i.push(r[u+2]):i[u]=r[u+2];for(let u=0;u<i.length;u++)if(u<n.length){if(n[u]<0)throw new Error("strides should be greater than or equal to 1")}else n.push(1);for(let u=0;u<i.length;u++)if(u<a.length){if(a[u]<0)throw new Error("dilations should be greater than or equal to 1")}else a.push(1);for(let u=0;u<i.length*2;u++)if(u<s.length){if(s[u]<0)throw new Error("pad should be greater than or equal to 1")}else s.push(0);for(let u=0;u<i.length;u++){if(i[u]<=0)throw new Error("kernel shapes need to be greater than 0");if(s[u]>=i[u]||s[u+i.length]>=i[u])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,i,n,a,s,u){if(u){if(a.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let l=0;l<t.length-2;l++)wr.adjustPadAndReturnShape(t[l+(s?1:2)],r[l],i[l],n[l],a,l,l+t.length-2,u)}}static computePoolOutputShape(t,r,i,n,a,s,u){if(r.length<=0)throw new Error("input shape must be of size greater than 0");let l=[r[0],r[1]];return wr.computeShapeHelper(t,r,l,i,n,a,s,u),l}static computeConvOutputShape(t,r,i,n,a,s,u){if(t.length<=0||r.length<=0)throw new Error("invalid input tensor dims or invalid filter tensor dims");let l=[t[0],r[0]];return wr.computeShapeHelper(!1,t,l,i,n,a,s,u),l}static computeShapeHelper(t,r,i,n,a,s,u,l){if(t)for(let d=0;d<r.length-2;d++)i.push(1);else for(let d=0;d<r.length-2;d++)i.push(wr.adjustPadAndReturnShape(r[d+2],n[d],a[d],s[d],u,d,d+r.length-2,l))}static adjustPadAndReturnShape(t,r,i,n,a,s,u,l){let d=i*(n-1)+1;if(l&&l!=="NOTSET")switch(l){case"VALID":return a[s]=0,a[u]=0,Math.floor((t-d)/r+1);case"SAME_LOWER":case"SAME_UPPER":if(i!==1)throw new Error("Dilation not supported for SAME_UPPER or SAME_LOWER");{let f=((t+r-1)/r-1)*r+n-t;return a[s]=Math.floor(l==="SAME_LOWER"?(f+1)/2:f/2),a[u]=f-a[s],Math.floor((t+f-n)/r+1)}default:throw new Error("Unsupported AutoPad type")}else return Math.floor((t+a[s]+a[u]-d)/r+1)}},Up=class{static getShapeOfGemmResult(e,t,r,i,n){if(e.length!==2||r.length!==2)throw new Error("shape need to be of size 2");let a,s,u;t?(a=e[1],s=e[0]):(a=e[0],s=e[1]);let l=-1;if(i?(u=r[0],l=1):(u=r[1],l=0),r[l]!==s)throw new Error("dimension mismatch");if(a<=0||u<=0||s<=0)throw new Error("invalid shape specified");if(n&&!Qt.isValidBroadcast(n,[a,u]))throw new Error("gemm: invalid bias shape for broadcast");return[a,u,s]}},Wp=-34028234663852886e22,Lp=34028234663852886e22}),qp=P(()=>{"use strict";ee(),ha=(e,t)=>new(pi(t))(e)}),Qy=P(()=>{"use strict";ee(),lt(),en=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),qn=(e,t)=>{if(t==="int32")return e;let r=en.get(t);if(!r)throw new Error(`WebNN backend does not support data type: ${t}`);let i=r/8;if(e.byteLength%i!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${i}.`);let n=e.byteLength/i,a=new(pi(t))(e.buffer,e.byteOffset,n);switch(t){case"int64":case"uint64":{let s=new Int32Array(n);for(let u=0;u<n;u++){let l=a[u];if(l>2147483647n||l<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");s[u]=Number(l)}return new Uint8Array(s.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&a.some(u=>u>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let s=Int32Array.from(a,Number);return new Uint8Array(s.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},tn=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let r=e.byteLength/4,i=new Int32Array(e.buffer,e.byteOffset,r);switch(t){case"int64":{let n=BigInt64Array.from(i,BigInt);return new Uint8Array(n.buffer)}case"uint64":{if(i.some(a=>a<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let n=BigUint64Array.from(i,BigInt);return new Uint8Array(n.buffer)}case"int8":{if(i.some(a=>a<-128||a>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let n=Int8Array.from(i,Number);return new Uint8Array(n.buffer)}case"uint8":{if(i.some(n=>n<0||n>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(i,Number)}case"uint32":{if(i.some(a=>a<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let n=Uint32Array.from(i,Number);return new Uint8Array(n.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},Yo=1,rn=()=>Yo++,Xo=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),nn=(e,t)=>{let r=en.get(e);if(!r)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((i,n)=>i*n)*r/8):0},an=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:r,tensor:i,dataType:n,shape:a,fallbackDataType:s}=e;this.sessionId=t,this.mlContext=r,this.mlTensor=i,this.dataType=n,this.tensorShape=a,this.fallbackDataType=s}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return nn(this.dataType,this.tensorShape)}destroy(){de("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),r=tn(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(r);return}else return r.buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,r){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===r.length&&this.tensorShape.every((i,n)=>i===r[n])}setIsDataConverted(e){this.isDataConverted=e}},sn=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,r,i){let n=this.tensorManager.getMLContext(e),a=this.tensorManager.getMLOpSupportLimits(e),s;if(!a?.input.dataTypes.includes(t)){if(s=Xo.get(t),!s||a?.input.dataTypes.includes(s))throw new Error(`WebNN backend does not support data type: ${t}`);de("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${s}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(n,t,r))return this.wrapper.tensor;if(i){if(this.wrapper.byteLength!==nn(t,r))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let u=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,r,u,!0,!0,s),i&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=qn(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else de("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){if(this.activeUpload){let t=this.wrapper?.isDataConverted?tn(this.activeUpload,this.wrapper?.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(t):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(t);return}else return t.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},Jo=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=rn();return this.tensorTrackersById.set(e,new sn(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,r,i,n){de("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${i}, copyOld: ${n}}`);let a=this.tensorTrackersById.get(t);if(!a)throw new Error("Tensor not found.");return a.ensureTensor(e,r,i,n)}upload(e,t){let r=this.tensorTrackersById.get(e);if(!r)throw new Error("Tensor not found.");r.upload(t)}async download(e,t){de("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t?.byteLength}}`);let r=this.tensorTrackersById.get(e);if(!r)throw new Error("Tensor not found.");return r.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,r,i){let n=this.getMLContext(e),a=rn(),s=new an({sessionId:e,context:n,tensor:t,dataType:r,shape:i});return this.tensorTrackersById.set(a,new sn(this,s)),this.externalTensors.add(s),a}async getCachedTensor(e,t,r,i,n,a,s){let u=this.getMLContext(e);for(let[d,f]of this.freeTensors.entries())if(f.canReuseTensor(u,t,r)){de("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${s?`fallbackDataType: ${s},`:""} shape: ${r}`);let h=this.freeTensors.splice(d,1)[0];return h.sessionId=e,h}de("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${s?`fallbackDataType: ${s},`:""} shape: ${r}}`);let l=await u.createTensor({dataType:s??t,shape:r,dimensions:r,usage:i,writable:n,readable:a});return new an({sessionId:e,context:u,tensor:l,dataType:t,shape:r,fallbackDataType:s})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},Vp=(...e)=>new Jo(...e)}),Zy=P(()=>{"use strict";ee(),Ut(),qp(),Qy(),lt(),lr=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),eu=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let r=Object.keys(e).sort(),i=Object.keys(t).sort();return r.length===i.length&&r.every((n,a)=>n===i[a]&&e[n]===t[n])},Gp=class{constructor(e){this.tensorManager=Vp(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,ca(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){de("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){de("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let r of t)de("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${r}}`),this.tensorManager.releaseTensorId(r);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let r=this.mlContextCache.findIndex(i=>i.gpuDevice===e);if(r!==-1)return this.mlContextCache[r].mlContext;{let i=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:i}),i}}else if(e===void 0){let r=this.mlContextCache.findIndex(i=>i.options===void 0&&i.gpuDevice===void 0);if(r!==-1)return this.mlContextCache[r].mlContext;{let i=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:i}),i}}let t=this.mlContextCache.findIndex(r=>eu(r.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let r=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:r}),r}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let r=this.sessionIdsByMLContext.get(t);r||(r=new Set,this.sessionIdsByMLContext.set(t,r)),r.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let r=this.sessionIdsByMLContext.get(t);if(r.delete(e),r.size===0){this.sessionIdsByMLContext.delete(t);let i=this.mlContextCache.findIndex(n=>n.mlContext===t);i!==-1&&this.mlContextCache.splice(i,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){de("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,r,i,n){let a=lr.get(r);if(!a)throw new Error(`Unsupported ONNX data type: ${r}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,a,i,n)}async createTemporaryTensor(e,t,r){de("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${r}}`);let i=lr.get(t);if(!i)throw new Error(`Unsupported ONNX data type: ${t}`);let n=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,n,i,r,!1);let a=this.temporarySessionTensorIds.get(e);return a?a.push(n):this.temporarySessionTensorIds.set(e,[n]),n}uploadTensor(e,t){if(!we().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");de("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let r=await this.tensorManager.download(e);return ha(r,t)}}registerMLTensor(e,t,r,i){let n=lr.get(r);if(!n)throw new Error(`Unsupported ONNX data type: ${r}`);let a=this.tensorManager.registerTensor(e,t,n,i);return de("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${n}, dimensions: ${i}} -> {tensorId: ${a}}`),a}registerMLConstant(e,t,r,i,n,a,s=!1){if(!a)throw new Error("External mounted files are not available.");let u=e;e.startsWith("./")&&(u=e.substring(2));let l=a.get(u);if(!l)throw new Error(`File with name ${u} not found in preloaded files.`);if(t+r>l.byteLength)throw new Error("Out of bounds: data offset and length exceed the external file data size.");let d=l.slice(t,t+r).buffer,f;switch(n.dataType){case"float32":f=new Float32Array(d);break;case"float16":f=typeof Float16Array<"u"&&Float16Array.from?new Float16Array(d):new Uint16Array(d);break;case"int32":f=new Int32Array(d);break;case"uint32":f=new Uint32Array(d);break;case"int64":if(s){let h=qn(new Uint8Array(d),"int64");f=new Int32Array(h.buffer),n.dataType="int32"}else f=new BigInt64Array(d);break;case"uint64":f=new BigUint64Array(d);break;case"int8":f=new Int8Array(d);break;case"int4":case"uint4":case"uint8":f=new Uint8Array(d);break;default:throw new Error(`Unsupported data type: ${n.dataType} in creating WebNN Constant from external data.`)}return de("verbose",()=>`[WebNN] registerMLConstant {dataType: ${n.dataType}, shape: ${n.shape}}} ${s?"(Note: it was int64 data type and registered to int32 as workaround)":""}`),i.constant(n,f)}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let r=this.sessionGraphInputs.get(e);return r?r.includes(t):!1}isGraphOutput(e,t){let r=this.sessionGraphOutputs.get(e);return r?r.includes(t):!1}isGraphInputOutputTypeSupported(e,t,r=!0){let i=lr.get(Rt(t)),n=this.mlOpSupportLimitsBySessionId.get(e);return typeof i>"u"?!1:r?!!n?.input.dataTypes.includes(i):!!n?.output.dataTypes.includes(i)}flush(){}}}),fa=P(()=>{"use strict"}),Yy=P(()=>{"use strict";lt(),fa(),on=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),Hr=[],Kr=e=>Math.ceil(Number(e)/16)*16,tu=e=>{for(let t=0;t<Hr.length;t++){let r=Hr[t];if(e<=r)return r}return Math.ceil(e/16)*16},ru=1,un=()=>ru++,Vn=async(e,t,r,i)=>{let n=Kr(r),a=e.device.createBuffer({size:n,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let s=e.getCommandEncoder();e.endComputePass(),s.copyBufferToBuffer(t,0,a,0,n),e.flush(),await a.mapAsync(GPUMapMode.READ);let u=a.getMappedRange();if(i){let l=i();return l.set(new Uint8Array(u,0,r)),l}else return new Uint8Array(u.slice(0,r))}finally{a.destroy()}},iu=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of on)Hr.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let r=t.buffer,i=t.byteOffset,n=t.byteLength,a=Kr(n),s=this.storageCache.get(e);if(!s)throw new Error("gpu data for uploading does not exist");if(Number(s.originalSize)!==n)throw new Error(`inconsistent data size. gpu data size=${s.originalSize}, data size=${n}`);let u=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),l=u.getMappedRange();new Uint8Array(l).set(new Uint8Array(r,i,n)),u.unmap();let d=this.backend.device.createCommandEncoder();d.copyBufferToBuffer(u,0,s.gpuData.buffer,0,a),this.backend.device.queue.submit([d.finish()]),u.destroy(),de("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let r=this.storageCache.get(e);if(!r)throw new Error("source gpu data for memcpy does not exist");let i=this.storageCache.get(t);if(!i)throw new Error("destination gpu data for memcpy does not exist");if(r.originalSize!==i.originalSize)throw new Error("inconsistent source and destination gpu data size");let n=Kr(r.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(r.gpuData.buffer,0,i.gpuData.buffer,0,n)}registerExternalBuffer(e,t,r){let i;if(r){if(i=r[0],e===r[1])return de("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, buffer is the same, skip.`),i;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet.
|
|
7
|
+
Please use the previous external buffer!`)}else i=un();return this.storageCache.set(i,{gpuData:{id:i,type:0,buffer:e},originalSize:t}),de("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${i}, registered.`),i}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),de("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let r=tu(e),i,n=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(n||a){let u=(n?this.freeBuffers:this.freeUniformBuffers).get(r);u?u.length>0?i=u.pop():i=this.backend.device.createBuffer({size:r,usage:t}):i=this.backend.device.createBuffer({size:r,usage:t})}else i=this.backend.device.createBuffer({size:r,usage:t});let s={id:un(),type:0,buffer:i};return this.storageCache.set(s.id,{gpuData:s,originalSize:Number(e)}),de("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${s.id}`),s}get(e){return this.storageCache.get(e)?.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,r=this.storageCache.get(t);if(!r){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return de("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(e,t){let r=this.storageCache.get(Number(e));if(!r)throw new Error("data does not exist");await Vn(this.backend,r.gpuData.buffer,r.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=on.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let r=this.freeBuffers.get(e.size)||[];t===void 0||r.length>=t?e.destroy():r.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let r=this.freeUniformBuffers.get(e.size)||[];t===void 0||r.length>=t?e.destroy():r.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(r=>{r.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(de("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(r=>{r.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Fp=(...e)=>new iu(...e)}),Te=P(()=>{"use strict";nu=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},me=e=>new nu(e)}),ne=P(()=>{"use strict";ee(),ie(),Zt=64,jr=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}<f16>`:"f16";case 1:return t>1?`vec${t}<f32>`:"f32";case 6:return t>1?`vec${t}<i32>`:"i32";case 12:return t>1?`vec${t}<u32>`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2<u32>","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4<bool>"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},Ee=(e,t=1)=>{let r=jr(e,t);return typeof r=="string"?r:r[0]},Ae=(e,t=1)=>{let r=jr(e,t);return typeof r=="string"?r:r[1]},Z=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:R.computeStrides(r)})}),t},xe=e=>e%4===0?4:e%2===0?2:1,Gn=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,jt=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}<f32>(${r})`,vt=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,K=(e,t,r,i)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?i==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:i==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,dr=(e,t,r,i,n)=>{let a=typeof r=="number",s=a?r:r.length,u=[...new Array(s).keys()],l=s<2?"u32":s<=4?`vec${s}<u32>`:`array<u32, ${s}>`,d=jr(t,n),f=typeof d=="string"?d:d[1],h=typeof d=="string"?d:d[0],g={indices:l,value:f,storage:h,tensor:t},w=N=>typeof N=="string"?N:`${N}u`,y={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},v=a?"uniforms.":"",T=`${v}${e}_shape`,$=`${v}${e}_strides`,_="";for(let N=0;N<s-1;N++)_+=`
|
|
8
|
+
let dim${N} = current / ${K($,N,s)};
|
|
9
|
+
let rest${N} = current % ${K($,N,s)};
|
|
10
|
+
indices[${N}] = dim${N};
|
|
11
|
+
current = rest${N};
|
|
12
|
+
`;_+=`indices[${s-1}] = current;`;let I=s<2?"":`
|
|
13
|
+
fn o2i_${e}(offset: u32) -> ${g.indices} {
|
|
14
|
+
var indices: ${g.indices};
|
|
15
|
+
var current = offset;
|
|
16
|
+
${_}
|
|
17
|
+
return indices;
|
|
18
|
+
}`,k=N=>(y.offsetToIndices=!0,s<2?N:`o2i_${e}(${N})`),E=[];if(s>=2)for(let N=s-1;N>=0;N--)E.push(`${K($,N,s)} * (indices[${N}])`);let C=s<2?"":`
|
|
19
|
+
fn i2o_${e}(indices: ${g.indices}) -> u32 {
|
|
20
|
+
return ${E.join("+")};
|
|
21
|
+
}`,O=N=>(y.indicesToOffset=!0,s<2?N:`i2o_${e}(${N})`),x=(...N)=>s===0?"0u":`${g.indices}(${N.map(w).join(",")})`,D=(N,V)=>s<2?`${N}`:`${K(N,V,s)}`,L=(N,V,te)=>s<2?`${N}=${te};`:`${K(N,V,s)}=${te};`,Y={},q=(N,V)=>{y.broadcastedIndicesToOffset=!0;let te=`${V.name}broadcastedIndicesTo${e}Offset`;if(te in Y)return`${te}(${N})`;let ce=[];for(let Ie=s-1;Ie>=0;Ie--){let Me=V.indicesGet("outputIndices",Ie+V.rank-s);ce.push(`${D($,Ie)} * (${Me} % ${D(T,Ie)})`)}return Y[te]=`fn ${te}(outputIndices: ${V.type.indices}) -> u32 {
|
|
22
|
+
return ${ce.length>0?ce.join("+"):"0u"};
|
|
23
|
+
}`,`${te}(${N})`},j=(N,V)=>(()=>{if(g.storage===g.value)return`${e}[${N}]=${V};`;if(g.storage==="vec2<u32>"&&g.value==="i32")return`${e}[${N}]=vec2<u32>(u32(${V}), select(0u, 0xFFFFFFFFu, ${V} < 0));`;if(g.storage==="vec2<u32>"&&g.value==="u32")return`${e}[${N}]=vec2<u32>(u32(${V}), 0u);`;if(g.storage==="u32"&&g.value==="vec4<bool>")return`${e}[${N}]=dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(${V}));`;throw new Error(`not supported combination of storage type ${g.storage} and value type ${g.value} yet`)})(),ue=N=>(()=>{if(g.storage===g.value)return`${e}[${N}]`;if(g.storage==="vec2<u32>"&&g.value==="i32")return`i32(${e}[${N}].x)`;if(g.storage==="vec2<u32>"&&g.value==="u32")return`u32(${e}[${N}].x)`;if(g.storage==="u32"&&g.value==="vec4<bool>")return`vec4<bool>(bool(${e}[${N}] & 0xFFu), bool(${e}[${N}] & 0xFF00u), bool(${e}[${N}] & 0xFF0000u), bool(${e}[${N}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${g.storage} and value type ${g.value} yet`)})(),A=s<2?"":`
|
|
24
|
+
fn get_${e}ByIndices(indices: ${g.indices}) -> ${f} {
|
|
25
|
+
return ${ue(`i2o_${e}(indices)`)};
|
|
26
|
+
}`,U=s<2?"":(()=>{let N=u.map(te=>`d${te}: u32`).join(", "),V=u.map(te=>`d${te}`).join(", ");return`
|
|
27
|
+
fn get_${e}(${N}) -> ${f} {
|
|
28
|
+
return get_${e}ByIndices(${x(V)});
|
|
29
|
+
}`})(),re=(...N)=>{if(N.length!==s)throw new Error(`indices length must be ${s}`);let V=N.map(w).join(",");return s===0?ue("0u"):s===1?ue(V[0]):(y.get=!0,y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}(${V})`)},J=N=>s<2?ue(N):(y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}ByIndices(${N})`),H=s<2?"":`
|
|
30
|
+
fn set_${e}ByIndices(indices: ${g.indices}, value: ${f}) {
|
|
31
|
+
${j(`i2o_${e}(indices)`,"value")}
|
|
32
|
+
}`,pe=s<2?"":(()=>{let N=u.map(te=>`d${te}: u32`).join(", "),V=u.map(te=>`d${te}`).join(", ");return`
|
|
33
|
+
fn set_${e}(${N}, value: ${f}) {
|
|
34
|
+
set_${e}ByIndices(${x(V)}, value);
|
|
35
|
+
}`})();return{impl:()=>{let N=[],V=!1;return y.offsetToIndices&&(N.push(I),V=!0),y.indicesToOffset&&(N.push(C),V=!0),y.broadcastedIndicesToOffset&&(Object.values(Y).forEach(te=>N.push(te)),V=!0),y.set&&(N.push(pe),V=!0),y.setByIndices&&(N.push(H),V=!0),y.get&&(N.push(U),V=!0),y.getByIndices&&(N.push(A),V=!0),!a&&V&&N.unshift(`const ${T} = ${g.indices}(${r.join(",")});`,`const ${$} = ${g.indices}(${R.computeStrides(r).join(",")});`),N.join(`
|
|
36
|
+
`)},type:g,offsetToIndices:k,indicesToOffset:O,broadcastedIndicesToOffset:q,indices:x,indicesGet:D,indicesSet:L,set:(...N)=>{if(N.length!==s+1)throw new Error(`indices length must be ${s}`);let V=N[s];if(typeof V!="string")throw new Error("value must be string");let te=N.slice(0,s).map(w).join(",");return s===0?j("0u",V):s===1?j(te[0],V):(y.set=!0,y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}(${te}, ${V})`)},setByOffset:j,setByIndices:(N,V)=>s<2?j(N,V):(y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}ByIndices(${N}, ${V});`),get:re,getByOffset:ue,getByIndices:J,usage:i,name:e,strides:$,shape:T,rank:s}},B=(e,t,r,i=1)=>dr(e,t,r,"input",i),F=(e,t,r,i=1)=>dr(e,t,r,"output",i),Hp=(e,t,r)=>dr(e,t,r,"atomicOutput",1),ma=(e,t,r,i=1)=>dr(e,t,r,"internal",i),au=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=Zt){let t=typeof e=="number"?e:e[0],r=typeof e=="number"?1:e[1],i=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||r>this.limits.maxComputeWorkgroupSizeY||i>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${r}, ${i}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*r*i>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${r}, ${i}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let n=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,a=n?`@builtin(global_invocation_id) global_id : vec3<u32>,
|
|
37
|
+
@builtin(workgroup_id) workgroup_id : vec3<u32>,
|
|
38
|
+
@builtin(local_invocation_index) local_idx : u32,
|
|
39
|
+
@builtin(local_invocation_id) local_id : vec3<u32>`:`@builtin(global_invocation_id) global_id : vec3<u32>,
|
|
40
|
+
@builtin(local_invocation_id) local_id : vec3<u32>,
|
|
41
|
+
@builtin(local_invocation_index) local_idx : u32,
|
|
42
|
+
@builtin(workgroup_id) workgroup_id : vec3<u32>,
|
|
43
|
+
@builtin(num_workgroups) num_workgroups : vec3<u32>`,s=n?`let global_idx = global_id.x;
|
|
44
|
+
let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] +
|
|
45
|
+
workgroup_id.y * num_workgroups[0] + workgroup_id.x;
|
|
46
|
+
let global_idx = workgroup_index * ${t*r*i}u + local_idx;`;return`@compute @workgroup_size(${t}, ${r}, ${i})
|
|
47
|
+
fn main(${a}) {
|
|
48
|
+
${s}
|
|
49
|
+
`}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let r=e.usage==="input"?"read":"read_write",i=e.usage==="atomicOutput"?"atomic<i32>":e.type.storage;return`@group(0) @binding(${t}) var<storage, ${r}> ${e.name}: array<${i}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(`
|
|
50
|
+
`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,r=1){return this.uniforms.push({name:e,type:t,length:r}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:r,length:i}of this.uniforms)if(i&&i>4)r==="f16"?e.push(`@align(16) ${t}:array<mat2x4<${r}>, ${Math.ceil(i/8)}>`):e.push(`${t}:array<vec4<${r}>, ${Math.ceil(i/4)}>`);else{let n=i==null||i===1?r:`vec${i}<${r}>`;e.push(`${t}:${n}`)}return`
|
|
51
|
+
struct Uniforms { ${e.join(", ")} };
|
|
52
|
+
@group(0) @binding(${this.variableIndex}) var<uniform> uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(`
|
|
53
|
+
`)+this.internalVariables.map(e=>e.impl()).join(`
|
|
54
|
+
`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},Kp=(e,t)=>new au(e,t)}),$t=P(()=>{"use strict";ee(),ie(),Te(),ne(),su=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},ln=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),ou=(e,t)=>R.sortBasedOnPerm(e,ln(e.length,t)),uu=(e,t,r,i)=>{let n=`fn perm(i: ${i.type.indices}) -> ${r.type.indices} {
|
|
55
|
+
var a: ${r.type.indices};`;for(let a=0;a<t;++a)n+=`a[${e[a]}]=i[${a}];`;return n+="return a;}"},lu=(e,t)=>{let r=[],i=[];for(let n=0;n<e.length;++n)e[n]!==1&&r.push(e[n]),e[t[n]]!==1&&i.push(t[n]);return{newShape:r,newPerm:i}},du=(e,t)=>{let r=0;for(let i=0;i<e.length;++i)if(t[e[i]]!==1){if(e[i]<r)return!1;r=e[i]}return!0},De=(e,t)=>{let r=e.dataType,i=e.dims.length,n=ln(i,t),a=ou(e.dims,n),s=e.dims,u=a,l=i<2||du(n,e.dims),d;if(l)return d=y=>{let v=B("input",r,s,4),T=F("output",r,u,4);return`
|
|
56
|
+
${y.registerUniform("output_size","u32").declareVariables(v,T)}
|
|
57
|
+
${y.mainStart()}
|
|
58
|
+
${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
59
|
+
output[global_idx] = input[global_idx];
|
|
60
|
+
}`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let y=R.size(a);return{outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(y/64/4)},programUniforms:[{type:12,data:Math.ceil(y/4)}]}},getShaderSource:d};let{newShape:f,newPerm:h}=lu(e.dims,n),g=R.areEqual(h,[2,3,1]),w=R.areEqual(h,[3,1,2]);if(f.length===2||g||w){s=g?[f[0],f[1]*f[2]]:w?[f[0]*f[1],f[2]]:f,u=[s[1],s[0]];let y=16;return d=v=>{let T=B("a",r,s.length),$=F("output",r,u.length);return`
|
|
61
|
+
${v.registerUniform("output_size","u32").declareVariables(T,$)}
|
|
62
|
+
var<workgroup> tile : array<array<${$.type.value}, ${y+1}>, ${y}>;
|
|
63
|
+
${v.mainStart([y,y,1])}
|
|
64
|
+
let stride = (uniforms.output_shape[1] - 1) / ${y} + 1;
|
|
65
|
+
let workgroup_id_x = workgroup_index % stride;
|
|
66
|
+
let workgroup_id_y = workgroup_index / stride;
|
|
67
|
+
let input_col = workgroup_id_y * ${y}u + local_id.x;
|
|
68
|
+
let input_row = workgroup_id_x * ${y}u + local_id.y;
|
|
69
|
+
if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) {
|
|
70
|
+
tile[local_id.y][local_id.x] = ${T.getByIndices(`${T.type.indices}(input_row, input_col)`)};
|
|
71
|
+
}
|
|
72
|
+
workgroupBarrier();
|
|
73
|
+
|
|
74
|
+
let output_col = workgroup_id_x * ${y}u + local_id.x;
|
|
75
|
+
let output_row = workgroup_id_y * ${y}u + local_id.y;
|
|
76
|
+
if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) {
|
|
77
|
+
${$.setByIndices(`${$.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")}
|
|
78
|
+
}
|
|
79
|
+
}`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let v=R.size(a);return{outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(u[1]/y),y:Math.ceil(u[0]/y)},programUniforms:[{type:12,data:v},...Z(s,u)]}},getShaderSource:d}}return d=y=>{let v=B("a",r,s.length),T=F("output",r,u.length);return`
|
|
80
|
+
${y.registerUniform("output_size","u32").declareVariables(v,T)}
|
|
81
|
+
|
|
82
|
+
${uu(n,i,v,T)}
|
|
83
|
+
|
|
84
|
+
${y.mainStart()}
|
|
85
|
+
${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
86
|
+
|
|
87
|
+
let indices = ${T.offsetToIndices("global_idx")};
|
|
88
|
+
let aIndices = perm(indices);
|
|
89
|
+
|
|
90
|
+
${T.setByOffset("global_idx",v.getByIndices("aIndices"))}
|
|
91
|
+
}`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let y=R.size(a);return{outputs:[{dims:a,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},...Z(s,u)]}},getShaderSource:d}},jp=(e,t)=>{su(e.inputs,t.perm),e.compute(De(e.inputs[0],t.perm))},Qp=e=>me({perm:e.perm})}),Xy=P(()=>{"use strict";ee(),ie(),ne(),ga(),$t(),pu={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},cu={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},hu={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},fu={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},mu=(e,t)=>{let r=[];for(let i=t-e;i<t;++i)r.push(i);return r},gu=(e,t)=>{let r=[],i=e.length;for(let a=0;a<i;a++)t.indexOf(a)===-1&&r.push(e[a]);let n=t.map(a=>e[a]);return[r,n]},yu=(e,t)=>{let r=e.length+t.length,i=[],n=0;for(let a=0;a<r;a++)t.indexOf(a)===-1?i.push(e[n++]):i.push(1);return i},wu=(e,t)=>{for(let r=0;r<e.length;++r)if(e[e.length-r-1]!==t-1-r)return!1;return!0},_u=(e,t)=>{let r=[];if(!wu(e,t)){for(let i=0;i<t;++i)e.indexOf(i)===-1&&r.push(i);e.forEach(i=>r.push(i))}return r},bu=(e,t,r,i,n,a,s)=>{let u=r[0].dims,l=R.size(a),d=R.size(s),f=B("_A",r[0].dataType,u),h=F("output",n,a),g=64;l===1&&(g=256);let w=`
|
|
92
|
+
var<workgroup> aBestValues : array<f32, ${g}>;
|
|
93
|
+
`,y=v=>`
|
|
94
|
+
${v.registerUniform("reduceSize","u32").declareVariables(f,h)}
|
|
95
|
+
${w}
|
|
96
|
+
fn DIV_CEIL(a : u32, b : u32) -> u32 {
|
|
97
|
+
return ((a - 1u) / b + 1u);
|
|
98
|
+
}
|
|
99
|
+
${v.mainStart(g)}
|
|
100
|
+
|
|
101
|
+
let outputIndex = global_idx / ${g};
|
|
102
|
+
let offset = outputIndex * uniforms.reduceSize;
|
|
103
|
+
|
|
104
|
+
var bestValue = f32(${hu[i]});
|
|
105
|
+
let Length = uniforms.reduceSize;
|
|
106
|
+
for (var k = local_idx; k < Length; k = k + ${g}) {
|
|
107
|
+
let candidate = f32(${f.getByOffset("offset + k")});
|
|
108
|
+
bestValue = ${pu[i]};
|
|
109
|
+
}
|
|
110
|
+
aBestValues[local_idx] = bestValue;
|
|
111
|
+
workgroupBarrier();
|
|
112
|
+
|
|
113
|
+
var reduceSize = min(Length, ${g}u);
|
|
114
|
+
for (var currentSize = reduceSize / 2u; reduceSize > 1u;
|
|
115
|
+
currentSize = reduceSize / 2u) {
|
|
116
|
+
let interval = DIV_CEIL(reduceSize, 2u);
|
|
117
|
+
if (local_idx < currentSize) {
|
|
118
|
+
let candidate = aBestValues[local_idx + interval];
|
|
119
|
+
bestValue = ${cu[i]};
|
|
120
|
+
aBestValues[local_idx] = bestValue;
|
|
121
|
+
}
|
|
122
|
+
reduceSize = interval;
|
|
123
|
+
workgroupBarrier();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (local_idx == 0u) {
|
|
127
|
+
${h.setByOffset("outputIndex",`${i==="mean"?`${h.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${h.type.storage}(${fu[i]})`}`)};
|
|
128
|
+
}
|
|
129
|
+
}`;return{name:e,shaderCache:{hint:`${t};${g}`,inputDependencies:["type"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:a,dataType:n}],dispatchGroup:{x:l},programUniforms:[{type:12,data:d}]})}},Fe=(e,t,r,i)=>{let n=e.inputs.length===1?r:Fn(e.inputs,r),a=n.axes;a.length===0&&!n.noopWithEmptyAxes&&(a=e.inputs[0].dims.map((w,y)=>y));let s=R.normalizeAxes(a,e.inputs[0].dims.length),u=s,l=e.inputs[0],d=_u(u,e.inputs[0].dims.length);d.length>0&&(l=e.compute(De(e.inputs[0],d),{inputs:[0],outputs:[-1]})[0],u=mu(u.length,l.dims.length));let[f,h]=gu(l.dims,u),g=f;n.keepDims&&(g=yu(f,s)),e.compute(bu(t,n.cacheKey,[l],i,e.inputs[0].dataType,g,h),{inputs:[l]})},Zp=(e,t)=>{Fe(e,"ReduceMeanShared",t,"mean")},Yp=(e,t)=>{Fe(e,"ReduceL1Shared",t,"l1")},Xp=(e,t)=>{Fe(e,"ReduceL2Shared",t,"l2")},Jp=(e,t)=>{Fe(e,"ReduceLogSumExpShared",t,"logSumExp")},ec=(e,t)=>{Fe(e,"ReduceMaxShared",t,"max")},tc=(e,t)=>{Fe(e,"ReduceMinShared",t,"min")},rc=(e,t)=>{Fe(e,"ReduceProdShared",t,"prod")},ic=(e,t)=>{Fe(e,"ReduceSumShared",t,"sum")},nc=(e,t)=>{Fe(e,"ReduceSumSquareShared",t,"sumSquare")},ac=(e,t)=>{Fe(e,"ReduceLogSumShared",t,"logSum")}}),ga=P(()=>{"use strict";ee(),ie(),Te(),ne(),Xy(),He=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},vu=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],oi=(e,t,r,i,n,a,s=!1,u=!1)=>{let l=[],d=r[0].dims,f=d.length,h=R.normalizeAxes(n,f),g=!u&&h.length===0;d.forEach((v,T)=>{g||h.indexOf(T)>=0?s&&l.push(1):l.push(v)});let w=l.length,y=R.size(l);return{name:e,shaderCache:t,getShaderSource:v=>{let T=[],$=B("_A",r[0].dataType,f),_=F("output",a,w),I=i($,_,h),k=I[2];for(let E=0,C=0;E<f;E++)g||h.indexOf(E)>=0?(s&&C++,k=`for(var j${E}: u32 = 0; j${E} < ${d[E]}; j${E}++) {
|
|
130
|
+
${I[2].includes("last_index")?`let last_index = j${E};`:""}
|
|
131
|
+
${$.indicesSet("input_indices",E,`j${E}`)}
|
|
132
|
+
${k}
|
|
133
|
+
}`):(T.push(`${$.indicesSet("input_indices",E,_.indicesGet("output_indices",C))};`),C++);return`
|
|
134
|
+
|
|
135
|
+
${v.registerUniform("output_size","u32").declareVariables($,_)}
|
|
136
|
+
|
|
137
|
+
${v.mainStart()}
|
|
138
|
+
${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
139
|
+
var input_indices: ${$.type.indices};
|
|
140
|
+
let output_indices = ${_.offsetToIndices("global_idx")};
|
|
141
|
+
|
|
142
|
+
${T.join(`
|
|
143
|
+
`)}
|
|
144
|
+
${I[0]} // init ops for reduce max/min
|
|
145
|
+
${I[1]}
|
|
146
|
+
${k}
|
|
147
|
+
${I[3]}
|
|
148
|
+
${I.length===4?_.setByOffset("global_idx","value"):I.slice(4).join(`
|
|
149
|
+
`)}
|
|
150
|
+
}`},getRunData:()=>({outputs:[{dims:l,dataType:a}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},...Z(d,l)]})}},Fn=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(i=>r.push(Number(i))),me({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},Ke=(e,t,r,i)=>{let n=e.inputs,a=n.length===1?r:Fn(n,r);e.compute(oi(t,{hint:a.cacheKey,inputDependencies:["rank"]},[n[0]],a.noopWithEmptyAxes&&a.axes.length===0?vu:i,a.axes,n[0].dataType,a.keepDims,a.noopWithEmptyAxes),{inputs:[0]})},$u=(e,t)=>{He(e.inputs),Ke(e,"ReduceLogSum",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,"value = log(value);"])},xu=(e,t)=>{He(e.inputs),Ke(e,"ReduceL1",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += abs(${r.getByIndices("input_indices")});`,""])},Tu=(e,t)=>{He(e.inputs),Ke(e,"ReduceL2",t,(r,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Su=(e,t)=>{He(e.inputs),Ke(e,"ReduceLogSumExp",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += exp(${r.getByIndices("input_indices")});`,"value = log(value);"])},ku=(e,t)=>{He(e.inputs),Ke(e,"ReduceMax",t,(r,i,n)=>{let a=[];for(let s=0;s<r.rank;s++)(n.indexOf(s)>=0||n.length===0)&&a.push(r.indicesSet("input_indices",s,0));return[`${a.join(`
|
|
151
|
+
`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = max(value, ${r.getByIndices("input_indices")});`,""]})},Iu=(e,t)=>{He(e.inputs),Ke(e,"ReduceMean",t,(r,i,n)=>{let a=1;for(let s=0;s<r.rank;s++)(n.indexOf(s)>=0||n.length===0)&&(a*=e.inputs[0].dims[s]);return["var sum = f32(0);","",`sum += f32(${r.getByIndices("input_indices")});`,`let value = ${i.type.value}(sum / ${a});`]})},Eu=(e,t)=>{He(e.inputs),Ke(e,"ReduceMin",t,(r,i,n)=>{let a=[];for(let s=0;s<r.rank;s++)(n.indexOf(s)>=0||n.length===0)&&a.push(`input_indices[${s}] = 0;`);return[`${a.join(`
|
|
152
|
+
`)}`,`var value = ${r.getByIndices("input_indices")};`,`value = min(value, ${r.getByIndices("input_indices")});`,""]})},Cu=(e,t)=>{He(e.inputs),Ke(e,"ReduceProd",t,(r,i)=>[`var value = ${i.type.storage}(1);`,"",`value *= ${r.getByIndices("input_indices")};`,""])},zu=(e,t)=>{He(e.inputs),Ke(e,"ReduceSum",t,(r,i)=>[`var value = ${i.type.storage}(0);`,"",`value += ${r.getByIndices("input_indices")};`,""])},Au=(e,t)=>{He(e.inputs),Ke(e,"ReduceSumSquare",t,(r,i)=>[`var t = ${i.type.value}(0); var value = ${i.type.value}(0);`,"",`t = ${r.getByIndices("input_indices")}; value += t * t;`,""])},je=(e,t,r)=>{if(t.length===0)return r;let i=1,n=1;for(let a=0;a<t.length;a++)t.indexOf(a)===-1?i*=e[a]:n*=e[a];return n<32&&i>1024},sc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Iu(e,t):Zp(e,t)},oc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?xu(e,t):Yp(e,t)},uc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Tu(e,t):Xp(e,t)},lc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Su(e,t):Jp(e,t)},dc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?ku(e,t):ec(e,t)},pc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Eu(e,t):tc(e,t)},cc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Cu(e,t):rc(e,t)},hc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?zu(e,t):ic(e,t)},fc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Au(e,t):nc(e,t)},mc=(e,t)=>{je(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?$u(e,t):ac(e,t)}}),Jy=P(()=>{"use strict";ee(),Te(),ga(),dn=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},gc=(e,t)=>{dn(e.inputs);let r=(i,n,a)=>{let s=[];for(let u=0;u<i.rank;u++)(a.indexOf(u)>=0||a.length===0)&&s.push(`input_indices[${u}] = 0;`);return[`${s.join(`
|
|
153
|
+
`)}`,`var value = ${i.getByIndices("input_indices")};
|
|
154
|
+
var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) {
|
|
155
|
+
value = ${i.getByIndices("input_indices")};
|
|
156
|
+
best_index = i32(last_index);
|
|
157
|
+
}`,"",n.setByOffset("global_idx","best_index")]};e.compute(oi("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},yc=(e,t)=>{dn(e.inputs);let r=(i,n,a)=>{let s=[];for(let u=0;u<i.rank;u++)(a.indexOf(u)>=0||a.length===0)&&s.push(`input_indices[${u}] = 0;`);return[`${s.join(`
|
|
158
|
+
`)}`,`var value = ${i.getByIndices("input_indices")};
|
|
159
|
+
var best_index : i32 = 0;`,`if (${i.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) {
|
|
160
|
+
value = ${i.getByIndices("input_indices")};
|
|
161
|
+
best_index = i32(last_index);
|
|
162
|
+
}`,"",n.setByOffset("global_idx","best_index")]};e.compute(oi("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},Hn=e=>me(e)}),ya=P(()=>{"use strict";ee(),ie(),fa(),ne(),Ou=(e,t)=>{let r=e[0],i=e[1],n=e[2],a=e[3],s=e[4],u=e[5];if(s&&u)throw new Error("Attention cannot have both past and attention_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let l=r.dims[0],d=r.dims[1],f=r.dims[2];if(n.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(i.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(i.dims[0]!==f)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(n.dims[0]!==i.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let h=n.dims[0]/3,g=h,w=g;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let I of t.qkvHiddenSizes)if(I%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");h=t.qkvHiddenSizes[0],g=t.qkvHiddenSizes[1],w=t.qkvHiddenSizes[2]}let y=d;if(h!==g)throw new Error("qkv_hidden_sizes first element should be same as the second");if(n.dims[0]!==h+g+w)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let v=0;if(s){if(g!==w)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(s.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(s.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(s.dims[1]!==l)throw new Error('Input "past" second dimension must be batch_size');if(s.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(s.dims[4]!==g/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(v=s.dims[3])}let T=y+v,$=-1,_=0;if(a)throw new Error("Mask not supported");if(s)throw new Error("past is not supported");if(u){if(u.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(u.dims[0]!==l||u.dims[1]!==t.numHeads||u.dims[2]!==d||u.dims[3]!==T)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:d,pastSequenceLength:v,kvSequenceLength:y,totalSequenceLength:T,maxSequenceLength:$,inputHiddenSize:f,hiddenSize:h,vHiddenSize:w,headSize:Math.floor(h/t.numHeads),vHeadSize:Math.floor(w/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:_,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},Qr=(e,t,r)=>t&&e?`
|
|
163
|
+
let total_sequence_length_input = u32(${t.getByOffset("0")});
|
|
164
|
+
let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length);
|
|
165
|
+
let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input;
|
|
166
|
+
let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input;
|
|
167
|
+
total_sequence_length = u32(${e?.getByOffset("batchIdx")}) + 1;
|
|
168
|
+
var past_sequence_length: u32 = 0;
|
|
169
|
+
if (is_first_prompt == false) {
|
|
170
|
+
past_sequence_length = total_sequence_length - sequence_length;
|
|
171
|
+
}
|
|
172
|
+
`:`
|
|
173
|
+
${r?"let past_sequence_length = uniforms.past_sequence_length":""};
|
|
174
|
+
let present_sequence_length = total_sequence_length;
|
|
175
|
+
`,Ru=(e,t,r,i,n,a,s,u)=>{let l=xe(s?1:a),d=64,f=a/l;f<d&&(d=32);let h=Math.ceil(a/l/d),g=[{type:12,data:t},{type:12,data:r},{type:12,data:i},{type:12,data:n},{type:12,data:f},{type:12,data:h}],w=Ee(e.dataType,l),y=Ae(1,l),v=["type"];s&&v.push("type"),u&&v.push("type");let T=$=>{let _=F("x",e.dataType,e.dims,l),I=[_],k=s?B("seq_lens",s.dataType,s.dims):void 0;k&&I.push(k);let E=u?B("total_sequence_length_input",u.dataType,u.dims):void 0;E&&I.push(E);let C=Ae(e.dataType),O=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return`
|
|
176
|
+
var<workgroup> thread_max: array<f32, ${d}>;
|
|
177
|
+
var<workgroup> thread_sum: array<f32, ${d}>;
|
|
178
|
+
${$.registerUniforms(O).declareVariables(...I)}
|
|
179
|
+
${$.mainStart([d,1,1])}
|
|
180
|
+
let batchIdx = workgroup_id.z / uniforms.num_heads;
|
|
181
|
+
let headIdx = workgroup_id.z % uniforms.num_heads;
|
|
182
|
+
let sequence_length = uniforms.sequence_length;
|
|
183
|
+
var total_sequence_length = uniforms.total_sequence_length;
|
|
184
|
+
${Qr(k,E,!1)}
|
|
185
|
+
let local_offset = local_idx * uniforms.elements_per_thread;
|
|
186
|
+
let offset = (global_idx / ${d}) * uniforms.total_sequence_length + local_offset;
|
|
187
|
+
let seq_causal_length = ${s?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"};
|
|
188
|
+
var thread_max_vector = ${y}(-3.4028234663852886e+38f);
|
|
189
|
+
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
|
|
190
|
+
thread_max_vector = max(${y}(x[offset + i]), thread_max_vector);
|
|
191
|
+
}
|
|
192
|
+
thread_max[local_idx] = ${(()=>{switch(l){case 1:return"thread_max_vector";case 2:return"max(thread_max_vector.x, thread_max_vector.y)";case 4:return"max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))";default:throw new Error(`Unsupported components: ${l}`)}})()};
|
|
193
|
+
workgroupBarrier();
|
|
194
|
+
|
|
195
|
+
var max_value = f32(-3.4028234663852886e+38f);
|
|
196
|
+
for (var i = 0u; i < ${d}; i++) {
|
|
197
|
+
max_value = max(thread_max[i], max_value);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
var sum_vector = ${y}(0);
|
|
201
|
+
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
|
|
202
|
+
sum_vector += exp(${y}(x[offset + i]) - max_value);
|
|
203
|
+
}
|
|
204
|
+
thread_sum[local_idx] = ${(()=>{switch(l){case 1:return"sum_vector";case 2:return"sum_vector.x + sum_vector.y";case 4:return"sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w";default:throw new Error(`Unsupported components: ${l}`)}})()};
|
|
205
|
+
workgroupBarrier();
|
|
206
|
+
|
|
207
|
+
var sum: f32 = 0;
|
|
208
|
+
for (var i = 0u; i < ${d}; i++) {
|
|
209
|
+
sum += thread_sum[i];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (sum == 0) {
|
|
213
|
+
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
|
|
214
|
+
x[offset + i] = ${_.type.value}(${C}(1.0) / ${C}(seq_causal_length));
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) {
|
|
218
|
+
var f32input = ${y}(x[offset + i]);
|
|
219
|
+
x[offset + i] = ${_.type.value}(exp(f32input - max_value) / sum);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
${s?`
|
|
223
|
+
for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) {
|
|
224
|
+
x[offset + total_seq_id] = ${_.type.value}(${C}(0));
|
|
225
|
+
}`:""};
|
|
226
|
+
}`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${d};${w};${l}`,inputDependencies:v},getShaderSource:T,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:n,z:t*r},programUniforms:g})}},Mu=(e,t,r,i,n,a,s,u,l)=>{let d=s+a.kvSequenceLength,f=[a.batchSize,a.numHeads,a.sequenceLength,d],h=e>1&&i,g=a.kvNumHeads?a.kvNumHeads:a.numHeads,w=h?[a.batchSize,g,d,a.headSize]:void 0,y=a.nReps?a.nReps:1,v=a.scale===0?1/Math.sqrt(a.headSize):a.scale,T=xe(a.headSize),$=a.headSize/T,_=12,I={x:Math.ceil(d/_),y:Math.ceil(a.sequenceLength/_),z:a.batchSize*a.numHeads},k=[{type:12,data:a.sequenceLength},{type:12,data:$},{type:12,data:d},{type:12,data:a.numHeads},{type:12,data:a.headSize},{type:1,data:v},{type:12,data:s},{type:12,data:a.kvSequenceLength},{type:12,data:y}],E=h&&i&&R.size(i.dims)>0,C=["type","type"];E&&C.push("type"),n&&C.push("type"),u&&C.push("type"),l&&C.push("type");let O=[{dims:f,dataType:t.dataType,gpuDataType:0}];h&&O.push({dims:w,dataType:t.dataType,gpuDataType:0});let x=D=>{let L=B("q",t.dataType,t.dims,T),Y=B("key",r.dataType,r.dims,T),q=[L,Y];if(E){let H=B("past_key",i.dataType,i.dims,T);q.push(H)}n&&q.push(B("attention_bias",n.dataType,n.dims));let j=u?B("seq_lens",u.dataType,u.dims):void 0;j&&q.push(j);let ue=l?B("total_sequence_length_input",l.dataType,l.dims):void 0;ue&&q.push(ue);let A=F("output",t.dataType,f),U=[A];h&&U.push(F("present_key",t.dataType,w,T));let re=Ae(1,T),J=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return`
|
|
227
|
+
const TILE_SIZE = ${_}u;
|
|
228
|
+
|
|
229
|
+
var<workgroup> tileQ: array<${L.type.storage}, ${_*_}>;
|
|
230
|
+
var<workgroup> tileK: array<${L.type.storage}, ${_*_}>;
|
|
231
|
+
${D.registerUniforms(J).declareVariables(...q,...U)}
|
|
232
|
+
${D.mainStart([_,_,1])}
|
|
233
|
+
// x holds the N and y holds the M
|
|
234
|
+
let headIdx = workgroup_id.z % uniforms.num_heads;
|
|
235
|
+
let kvHeadIdx = ${y===1?"headIdx":"headIdx / uniforms.n_reps"};
|
|
236
|
+
let kv_num_heads = ${y===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"};
|
|
237
|
+
let batchIdx = workgroup_id.z / uniforms.num_heads;
|
|
238
|
+
let m = workgroup_id.y * TILE_SIZE;
|
|
239
|
+
let n = workgroup_id.x * TILE_SIZE;
|
|
240
|
+
let sequence_length = uniforms.M;
|
|
241
|
+
var total_sequence_length = uniforms.N;
|
|
242
|
+
${Qr(j,ue,!0)}
|
|
243
|
+
let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx;
|
|
244
|
+
let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;
|
|
245
|
+
${E&&h?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""};
|
|
246
|
+
let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K;
|
|
247
|
+
${h?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""}
|
|
248
|
+
var value = ${re}(0);
|
|
249
|
+
for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
|
|
250
|
+
if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) {
|
|
251
|
+
tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x];
|
|
252
|
+
}
|
|
253
|
+
if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) {
|
|
254
|
+
var idx = TILE_SIZE * local_id.y + local_id.x;
|
|
255
|
+
${E&&h?`
|
|
256
|
+
if (n + local_id.y < past_sequence_length) {
|
|
257
|
+
tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
|
|
258
|
+
} else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {
|
|
259
|
+
tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x];
|
|
260
|
+
}`:`
|
|
261
|
+
if (n + local_id.y < uniforms.kv_sequence_length) {
|
|
262
|
+
tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x];
|
|
263
|
+
}`}
|
|
264
|
+
${h?`if (n + local_id.y < present_sequence_length) {
|
|
265
|
+
present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx];
|
|
266
|
+
}`:""}
|
|
267
|
+
}
|
|
268
|
+
workgroupBarrier();
|
|
269
|
+
|
|
270
|
+
for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) {
|
|
271
|
+
value += ${re}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
workgroupBarrier();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (global_id.y < uniforms.M && global_id.x < total_sequence_length) {
|
|
278
|
+
let headOffset = workgroup_id.z * uniforms.M * uniforms.N;
|
|
279
|
+
let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x;
|
|
280
|
+
var sum: f32 = ${(()=>{switch(T){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${T}`)}})()};
|
|
281
|
+
output[outputIdx] = ${A.type.value} (sum * uniforms.alpha) + ${n?"attention_bias[outputIdx]":"0.0"};
|
|
282
|
+
}
|
|
283
|
+
}`};return{name:"AttentionProbs",shaderCache:{hint:`${T};${n!==void 0};${i!==void 0};${e}`,inputDependencies:C},getRunData:()=>({outputs:O,dispatchGroup:I,programUniforms:k}),getShaderSource:x}},Bu=(e,t,r,i,n,a,s=void 0,u=void 0)=>{let l=a+n.kvSequenceLength,d=n.nReps?n.nReps:1,f=n.vHiddenSize*d,h=e>1&&i,g=n.kvNumHeads?n.kvNumHeads:n.numHeads,w=h?[n.batchSize,g,l,n.headSize]:void 0,y=[n.batchSize,n.sequenceLength,f],v=12,T={x:Math.ceil(n.vHeadSize/v),y:Math.ceil(n.sequenceLength/v),z:n.batchSize*n.numHeads},$=[{type:12,data:n.sequenceLength},{type:12,data:l},{type:12,data:n.vHeadSize},{type:12,data:n.numHeads},{type:12,data:n.headSize},{type:12,data:f},{type:12,data:a},{type:12,data:n.kvSequenceLength},{type:12,data:d}],_=h&&i&&R.size(i.dims)>0,I=["type","type"];_&&I.push("type"),s&&I.push("type"),u&&I.push("type");let k=[{dims:y,dataType:t.dataType,gpuDataType:0}];h&&k.push({dims:w,dataType:t.dataType,gpuDataType:0});let E=C=>{let O=B("probs",t.dataType,t.dims),x=B("v",r.dataType,r.dims),D=[O,x];_&&D.push(B("past_value",i.dataType,i.dims));let L=s?B("seq_lens",s.dataType,s.dims):void 0;s&&D.push(L);let Y=u?B("total_sequence_length_input",u.dataType,u.dims):void 0;u&&D.push(Y);let q=[F("output",t.dataType,y)];h&&q.push(F("present_value",t.dataType,w));let j=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return`
|
|
284
|
+
const TILE_SIZE = ${v}u;
|
|
285
|
+
var<workgroup> tileQ: array<${O.type.value}, ${v*v}>;
|
|
286
|
+
var<workgroup> tileV: array<${O.type.value}, ${v*v}>;
|
|
287
|
+
${C.registerUniforms(j).declareVariables(...D,...q)}
|
|
288
|
+
${C.mainStart([v,v,1])}
|
|
289
|
+
let headIdx = workgroup_id.z % uniforms.num_heads;
|
|
290
|
+
let batchIdx = workgroup_id.z / uniforms.num_heads;
|
|
291
|
+
let kvHeadIdx = ${d===1?"headIdx":"headIdx / uniforms.n_reps"};
|
|
292
|
+
let kv_num_heads = ${d===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"};
|
|
293
|
+
let m = global_id.y;
|
|
294
|
+
let n = global_id.x;
|
|
295
|
+
let sequence_length = uniforms.M;
|
|
296
|
+
var total_sequence_length = uniforms.K;
|
|
297
|
+
${Qr(L,Y,!0)}
|
|
298
|
+
let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K;
|
|
299
|
+
let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch
|
|
300
|
+
${_&&h?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""};
|
|
301
|
+
let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n;
|
|
302
|
+
${h?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""}
|
|
303
|
+
var value = ${O.type.storage}(0);
|
|
304
|
+
for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
|
|
305
|
+
if (m < uniforms.M && w + local_id.x < uniforms.K) {
|
|
306
|
+
tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x];
|
|
307
|
+
}
|
|
308
|
+
if (n < uniforms.N && w + local_id.y < uniforms.K) {
|
|
309
|
+
var idx = TILE_SIZE * local_id.y + local_id.x;
|
|
310
|
+
${_&&h?`
|
|
311
|
+
if (w + local_id.y < past_sequence_length) {
|
|
312
|
+
tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N];
|
|
313
|
+
} else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) {
|
|
314
|
+
tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N];
|
|
315
|
+
}
|
|
316
|
+
`:`
|
|
317
|
+
if (w + local_id.y < uniforms.kv_sequence_length) {
|
|
318
|
+
tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N];
|
|
319
|
+
}`}
|
|
320
|
+
${h?`
|
|
321
|
+
if (w + local_id.y < present_sequence_length) {
|
|
322
|
+
present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx];
|
|
323
|
+
}`:""}
|
|
324
|
+
}
|
|
325
|
+
workgroupBarrier();
|
|
326
|
+
for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) {
|
|
327
|
+
value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x];
|
|
328
|
+
}
|
|
329
|
+
workgroupBarrier();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// we need to transpose output from BNSH_v to BSND_v
|
|
333
|
+
if (m < uniforms.M && n < uniforms.N) {
|
|
334
|
+
let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size
|
|
335
|
+
+ headIdx * uniforms.N + n;
|
|
336
|
+
output[outputIdx] = value;
|
|
337
|
+
}
|
|
338
|
+
}`};return{name:"AttentionScore",shaderCache:{hint:`${i!==void 0};${e}`,inputDependencies:I},getRunData:()=>({outputs:k,dispatchGroup:T,programUniforms:$}),getShaderSource:E}},Tr=(e,t,r,i,n,a,s,u,l,d,f=void 0,h=void 0)=>{let g=Math.min(e.outputCount,1+(s?1:0)+(u?1:0)),w=g>1?d.pastSequenceLength:0,y=w+d.kvSequenceLength,v=l&&R.size(l.dims)>0?l:void 0,T=[t,r];g>1&&s&&R.size(s.dims)>0&&T.push(s),v&&T.push(v),f&&T.push(f),h&&T.push(h);let $=e.compute(Mu(g,t,r,s,v,d,w,f,h),{inputs:T,outputs:g>1?[-1,1]:[-1]})[0];e.compute(Ru($,d.batchSize,d.numHeads,w,d.sequenceLength,y,f,h),{inputs:f&&h?[$,f,h]:[$],outputs:[]});let _=[$,i];g>1&&u&&R.size(u.dims)>0&&_.push(u),f&&_.push(f),h&&_.push(h),e.compute(Bu(g,$,i,u,d,w,f,h),{inputs:_,outputs:g>1?[0,2]:[0]})},Nu=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],i=t.sequenceLength,n=t.inputHiddenSize,a=t.headSize,s=12,u={x:Math.ceil(t.headSize/s),y:Math.ceil(t.sequenceLength/s),z:t.batchSize*t.numHeads},l=[e.inputs[0],e.inputs[1],e.inputs[2]],d=[{type:12,data:i},{type:12,data:n},{type:12,data:a},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],f=h=>{let g=F("output_q",l[0].dataType,r),w=F("output_k",l[0].dataType,r),y=F("output_v",l[0].dataType,r),v=B("input",l[0].dataType,l[0].dims),T=B("weight",l[1].dataType,l[1].dims),$=B("bias",l[2].dataType,l[2].dims),_=v.type.storage,I=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return`
|
|
339
|
+
const TILE_SIZE = ${s}u;
|
|
340
|
+
var<workgroup> tileInput: array<${_}, ${s*s}>;
|
|
341
|
+
var<workgroup> tileWeightQ: array<${_}, ${s*s}>;
|
|
342
|
+
var<workgroup> tileWeightK: array<${_}, ${s*s}>;
|
|
343
|
+
var<workgroup> tileWeightV: array<${_}, ${s*s}>;
|
|
344
|
+
${h.registerUniforms(I).declareVariables(v,T,$,g,w,y)}
|
|
345
|
+
${h.mainStart([s,s,1])}
|
|
346
|
+
let batchIndex = workgroup_id.z / uniforms.num_heads;
|
|
347
|
+
let headNumber = workgroup_id.z % uniforms.num_heads;
|
|
348
|
+
let m = global_id.y;
|
|
349
|
+
let n = global_id.x;
|
|
350
|
+
|
|
351
|
+
let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K;
|
|
352
|
+
let biasOffsetQ = headNumber * uniforms.head_size;
|
|
353
|
+
let biasOffsetK = uniforms.hidden_size + biasOffsetQ;
|
|
354
|
+
let biasOffsetV = uniforms.hidden_size + biasOffsetK;
|
|
355
|
+
|
|
356
|
+
var valueQ = ${_}(0);
|
|
357
|
+
var valueK = ${_}(0);
|
|
358
|
+
var valueV = ${_}(0);
|
|
359
|
+
for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) {
|
|
360
|
+
if (m < uniforms.M && w + local_id.x < uniforms.K) {
|
|
361
|
+
tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x];
|
|
362
|
+
}
|
|
363
|
+
if (n < uniforms.N && w + local_id.y < uniforms.K) {
|
|
364
|
+
let offset = n + (w + local_id.y) * uniforms.ldb;
|
|
365
|
+
tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset];
|
|
366
|
+
tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset];
|
|
367
|
+
tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset];
|
|
368
|
+
}
|
|
369
|
+
workgroupBarrier();
|
|
370
|
+
for (var k: u32 = 0u; k<TILE_SIZE && w+k < uniforms.K; k++) {
|
|
371
|
+
let inputTileOffset = TILE_SIZE * local_id.y + k;
|
|
372
|
+
let weightTileOffset = TILE_SIZE * k + local_id.x;
|
|
373
|
+
valueQ += tileInput[inputTileOffset] * tileWeightQ[weightTileOffset];
|
|
374
|
+
valueK += tileInput[inputTileOffset] * tileWeightK[weightTileOffset];
|
|
375
|
+
valueV += tileInput[inputTileOffset] * tileWeightV[weightTileOffset];
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
workgroupBarrier();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
let headOffset = (m * uniforms.N + n) % uniforms.head_size;
|
|
382
|
+
valueQ += bias[headOffset + biasOffsetQ];
|
|
383
|
+
valueK += bias[headOffset + biasOffsetK];
|
|
384
|
+
valueV += bias[headOffset + biasOffsetV];
|
|
385
|
+
|
|
386
|
+
let offset = workgroup_id.z * uniforms.M * uniforms.N;
|
|
387
|
+
if (m < uniforms.M && n < uniforms.N) {
|
|
388
|
+
let outputIdx = offset + m * uniforms.N + n;
|
|
389
|
+
output_q[outputIdx] = valueQ;
|
|
390
|
+
output_k[outputIdx] = valueK;
|
|
391
|
+
output_v[outputIdx] = valueV;
|
|
392
|
+
}
|
|
393
|
+
}`};return e.compute({name:"AttentionPrepare",shaderCache:{inputDependencies:["type","type","type"]},getRunData:()=>({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:u,programUniforms:d}),getShaderSource:f},{inputs:l,outputs:[-1,-1,-1]})},wc=(e,t)=>{let r=Ou(e.inputs,t),[i,n,a]=Nu(e,r);return Tr(e,i,n,a,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r)}}),e0=P(()=>{"use strict";Le(),ee(),ie(),Te(),ne(),Du=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(i,n,a)=>{let s=n.length;if(s!==i.length)throw new Error(`${a}: num dimensions != ${s}`);n.forEach((u,l)=>{if(u!==i[l])throw new Error(`${a}: dim[${l}] do not match`)})};if(e[0].dims.length>1){let i=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,i,"Invalid input scale"),r(e[2].dims,i,"Invalid input B"),r(e[3].dims,i,"Invalid input mean"),r(e[4].dims,i,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},Pu=(e,t)=>{let{epsilon:r,spatial:i,format:n}=t,a=e[0].dims,s=i?xe(a[a.length-1]):1,u=n==="NHWC"&&a.length>1?s:1,l=R.size(a)/s,d=i,f=d?a.length:a,h=B("x",e[0].dataType,e[0].dims,s),g=B("scale",e[1].dataType,e[1].dims,u),w=B("bias",e[2].dataType,e[2].dims,u),y=B("inputMean",e[3].dataType,e[3].dims,u),v=B("inputVar",e[4].dataType,e[4].dims,u),T=F("y",e[0].dataType,f,s),$=()=>{let I="";if(i)I=`let cOffset = ${a.length===1?"0u":n==="NHWC"?`outputIndices[${a.length-1}] / ${s}`:"outputIndices[1]"};`;else if(n==="NCHW")I=`
|
|
394
|
+
${T.indicesSet("outputIndices","0","0")}
|
|
395
|
+
let cOffset = ${T.indicesToOffset("outputIndices")};`;else{I=`var cIndices = ${g.type.indices}(0);
|
|
396
|
+
cIndices[0] = outputIndices[${a.length-1}];`;for(let k=1;k<g.rank;k++)I+=`cIndices[${k}] = outputIndices[${k}];`;I+=`let cOffset = ${g.indicesToOffset("cIndices")};`}return I},_=I=>`
|
|
397
|
+
const epsilon = ${r};
|
|
398
|
+
${I.registerUniform("outputSize","u32").declareVariables(h,g,w,y,v,T)}
|
|
399
|
+
${I.mainStart()}
|
|
400
|
+
${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
401
|
+
var outputIndices = ${T.offsetToIndices(`global_idx * ${s}`)};
|
|
402
|
+
${$()}
|
|
403
|
+
let scale = ${g.getByOffset("cOffset")};
|
|
404
|
+
let bias = ${w.getByOffset("cOffset")};
|
|
405
|
+
let inputMean = ${y.getByOffset("cOffset")};
|
|
406
|
+
let inputVar = ${v.getByOffset("cOffset")};
|
|
407
|
+
let x = ${h.getByOffset("global_idx")};
|
|
408
|
+
let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias;
|
|
409
|
+
${T.setByOffset("global_idx","value")}
|
|
410
|
+
}`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${i}_${s}`,inputDependencies:d?["rank","type","type","type","type"]:void 0},getShaderSource:_,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:d?[{type:12,data:l},...Z(a)]:[{type:12,data:l}]})}},Uu=e=>me(e),_c=(e,t)=>{let{inputs:r,outputCount:i}=e,n=Uu({...t,outputCount:i});if(be.webgpu.validateInputContent&&Du(r,n),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(Pu(r,n))}}),t0=P(()=>{"use strict";ie(),ne(),Wu=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Lu=e=>{let t=e[0].dims,r=e[0].dims[2],i=R.size(t)/4,n=e[0].dataType,a=B("input",n,t,4),s=B("bias",n,[r],4),u=B("residual",n,t,4),l=F("output",n,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:d=>`
|
|
411
|
+
const channels = ${r}u / 4;
|
|
412
|
+
${d.declareVariables(a,s,u,l)}
|
|
413
|
+
|
|
414
|
+
${d.mainStart()}
|
|
415
|
+
${d.guardAgainstOutOfBoundsWorkgroupSizes(i)}
|
|
416
|
+
let value = ${a.getByOffset("global_idx")}
|
|
417
|
+
+ ${s.getByOffset("global_idx % channels")} + ${u.getByOffset("global_idx")};
|
|
418
|
+
${l.setByOffset("global_idx","value")}
|
|
419
|
+
}`}},bc=e=>{Wu(e.inputs),e.compute(Lu(e.inputs))}}),wa=P(()=>{"use strict";ee(),ie(),Te(),ne(),qu=(e,t,r,i,n,a,s)=>{let u=Math.ceil(t/4),l="";typeof n=="string"?l=`${n}(a)`:l=n("a");let d=B("inputData",r,[u],4),f=F("outputData",i,[u],4),h=[{name:"vec_size",type:"u32"}];return s&&h.push(...s),`
|
|
420
|
+
${e.registerUniforms(h).declareVariables(d,f)}
|
|
421
|
+
|
|
422
|
+
${a??""}
|
|
423
|
+
|
|
424
|
+
${e.mainStart()}
|
|
425
|
+
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
426
|
+
|
|
427
|
+
let a = ${d.getByOffset("global_idx")};
|
|
428
|
+
${f.setByOffset("global_idx",l)}
|
|
429
|
+
}`},fe=(e,t,r,i,n,a=e.dataType,s,u)=>{let l=[{type:12,data:Math.ceil(R.size(e.dims)/4)}];return s&&l.push(...s),{name:t,shaderCache:{hint:n,inputDependencies:["type"]},getShaderSource:d=>qu(d,R.size(e.dims),e.dataType,a,r,i,u),getRunData:d=>({outputs:[{dims:e.dims,dataType:a}],dispatchGroup:{x:Math.ceil(R.size(d[0].dims)/64/4)},programUniforms:l})}},vc=e=>{e.compute(fe(e.inputs[0],"Abs","abs"))},$c=e=>{e.compute(fe(e.inputs[0],"Acos","acos"))},xc=e=>{e.compute(fe(e.inputs[0],"Acosh","acosh"))},Tc=e=>{e.compute(fe(e.inputs[0],"Asin","asin"))},Sc=e=>{e.compute(fe(e.inputs[0],"Asinh","asinh"))},kc=e=>{e.compute(fe(e.inputs[0],"Atan","atan"))},Ic=e=>{e.compute(fe(e.inputs[0],"Atanh","atanh"))},Ec=e=>me(e),Cc=(e,t)=>{let r;switch(t.to){case 10:r="vec4<f16>";break;case 1:r="vec4<f32>";break;case 12:r="vec4<u32>";break;case 6:r="vec4<i32>";break;case 9:r="vec4<bool>";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(fe(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},Vu=e=>{let t,r,i=e.length>=2&&e[1].data!==0,n=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=i?e[1].getFloat32Array()[0]:-34028234663852886e22,r=n?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=i?e[1].getUint16Array()[0]:64511,r=n?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return me({min:t,max:r})},zc=(e,t)=>{let r=t||Vu(e.inputs),i=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Clip",n=>`clamp(${n}, vec4<${i}>(uniforms.min), vec4<${i}>(uniforms.max))`,void 0,r.cacheKey,void 0,[{type:e.inputs[0].dataType,data:r.min},{type:e.inputs[0].dataType,data:r.max}],[{name:"min",type:i},{name:"max",type:i}]),{inputs:[0]})},Ac=e=>{e.compute(fe(e.inputs[0],"Ceil","ceil"))},Oc=e=>{e.compute(fe(e.inputs[0],"Cos","cos"))},Rc=e=>{e.compute(fe(e.inputs[0],"Cosh","cosh"))},_r=e=>me(e),Mc=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Elu",i=>`elu_vf32(${i})`,`
|
|
430
|
+
const elu_alpha_ = ${r}(${t.alpha});
|
|
431
|
+
|
|
432
|
+
fn elu_f32(a: ${r}) -> ${r} {
|
|
433
|
+
return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> {
|
|
437
|
+
return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w));
|
|
438
|
+
}`,t.cacheKey))},ii=(e="f32")=>`
|
|
439
|
+
const r0: ${e} = 0.3275911;
|
|
440
|
+
const r1: ${e} = 0.254829592;
|
|
441
|
+
const r2: ${e} = -0.284496736;
|
|
442
|
+
const r3: ${e} = 1.421413741;
|
|
443
|
+
const r4: ${e} = -1.453152027;
|
|
444
|
+
const r5: ${e} = 1.061405429;
|
|
445
|
+
|
|
446
|
+
fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> {
|
|
447
|
+
let absv = abs(v);
|
|
448
|
+
let x = 1.0 / (1.0 + r0 * absv);
|
|
449
|
+
return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv));
|
|
450
|
+
}`,Bc=e=>{let t=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,ii(t)))},Nc=e=>{e.compute(fe(e.inputs[0],"Exp","exp"))},Dc=e=>{e.compute(fe(e.inputs[0],"Floor","floor"))},Pc=e=>{let t=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,ii(t)))},Uc=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"LeakyRelu",i=>`select(leaky_relu_alpha_ * ${i}, ${i}, ${i} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},Wc=e=>{e.compute(fe(e.inputs[0],"Not",t=>`!${t}`))},Lc=e=>{e.compute(fe(e.inputs[0],"Neg",t=>`-${t}`))},qc=e=>{e.compute(fe(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},Vc=e=>{let t=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},Gc=e=>{e.compute(fe(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},Fc=e=>me(e),Hc=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"HardSigmoid",i=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${i} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},Kc=e=>{e.compute(fe(e.inputs[0],"Sin","sin"))},jc=e=>{e.compute(fe(e.inputs[0],"Sinh","sinh"))},Qc=e=>{e.compute(fe(e.inputs[0],"Sqrt","sqrt"))},Zc=e=>{e.compute(fe(e.inputs[0],"Tan","tan"))},pn=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,Yc=e=>{e.compute(fe(e.inputs[0],"Tanh",pn))},Kn=(e="f32")=>`
|
|
451
|
+
const fast_gelu_a: ${e} = 0.5;
|
|
452
|
+
const fast_gelu_b: ${e} = 0.7978845608028654;
|
|
453
|
+
const fast_gelu_c: ${e} = 0.035677408136300125;
|
|
454
|
+
|
|
455
|
+
fn tanh_v(v: vec4<${e}>) -> vec4<${e}> {
|
|
456
|
+
return ${pn("v")};
|
|
457
|
+
}
|
|
458
|
+
`,jn=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,Xc=e=>{let t=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"FastGelu",jn,Kn(t),void 0,e.inputs[0].dataType))},Jc=(e,t)=>{let r=Ae(e.inputs[0].dataType);return e.compute(fe(e.inputs[0],"ThresholdedRelu",i=>`select(vec4<${r}>(0.0), ${i}, ${i} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},eh=e=>{e.compute(fe(e.inputs[0],"Log","log"))},Gu=(e,t)=>`
|
|
459
|
+
const alpha = vec4<${e}>(${t});
|
|
460
|
+
const one = ${e}(1.0);
|
|
461
|
+
const zero = ${e}(0.0);
|
|
462
|
+
|
|
463
|
+
fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> {
|
|
464
|
+
let v = x *alpha;
|
|
465
|
+
var x1 : vec4<${e}>;
|
|
466
|
+
for (var i = 0; i < 4; i = i + 1) {
|
|
467
|
+
if (v[i] >= zero) {
|
|
468
|
+
x1[i] = one / (one + exp(-v[i]));
|
|
469
|
+
} else {
|
|
470
|
+
x1[i] = one - one / (one + exp(v[i]));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return x * x1;
|
|
474
|
+
}
|
|
475
|
+
`,Fu=e=>`quick_gelu_impl(${e})`,th=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(fe(e.inputs[0],"QuickGelu",Fu,Gu(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),r0=P(()=>{"use strict";ie(),ne(),wa(),Hu=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Ku=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=B("input",e[0].dataType,e[0].dims,4),i=B("bias",e[0].dataType,[e[0].dims[2]],4),n=F("output",e[0].dataType,t,4),a=R.size(t)/4,s=Ee(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)}}),getShaderSource:u=>`
|
|
476
|
+
const M_SQRT2 = sqrt(2.0);
|
|
477
|
+
const halfChannels = ${e[0].dims[2]/4/2}u;
|
|
478
|
+
|
|
479
|
+
${u.declareVariables(r,i,n)}
|
|
480
|
+
|
|
481
|
+
${ii(s)}
|
|
482
|
+
|
|
483
|
+
${u.mainStart()}
|
|
484
|
+
${u.guardAgainstOutOfBoundsWorkgroupSizes(a)}
|
|
485
|
+
let biasIdx = global_idx % halfChannels;
|
|
486
|
+
let batchIndex = global_idx / halfChannels;
|
|
487
|
+
let inputOffset = biasIdx + batchIndex * halfChannels * 2;
|
|
488
|
+
let valueLeft = input[inputOffset] + bias[biasIdx];
|
|
489
|
+
let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels];
|
|
490
|
+
let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1);
|
|
491
|
+
|
|
492
|
+
${n.setByOffset("global_idx","valueLeft * geluRight")}
|
|
493
|
+
}`}},rh=e=>{Hu(e.inputs),e.compute(Ku(e.inputs))}}),i0=P(()=>{"use strict";ee(),ie(),ne(),ju=(e,t,r,i,n,a,s,u,l,d,f,h)=>{let g,w;typeof u=="string"?g=w=(_,I)=>`${u}((${_}),(${I}))`:typeof u=="function"?g=w=u:(g=u.scalar,w=u.vector);let y=F("outputData",f,i.length,4),v=B("aData",l,t.length,4),T=B("bData",d,r.length,4),$;if(n)if(a){let _=R.size(t)===1,I=R.size(r)===1,k=t.length>0&&t[t.length-1]%4===0,E=r.length>0&&r[r.length-1]%4===0;_||I?$=y.setByOffset("global_idx",w(_?`${v.type.value}(${v.getByOffset("0")}.x)`:v.getByOffset("global_idx"),I?`${T.type.value}(${T.getByOffset("0")}.x)`:T.getByOffset("global_idx"))):$=`
|
|
494
|
+
let outputIndices = ${y.offsetToIndices("global_idx * 4u")};
|
|
495
|
+
let offsetA = ${v.broadcastedIndicesToOffset("outputIndices",y)};
|
|
496
|
+
let offsetB = ${T.broadcastedIndicesToOffset("outputIndices",y)};
|
|
497
|
+
${y.setByOffset("global_idx",w(s||k?v.getByOffset("offsetA / 4u"):`${v.type.value}(${v.getByOffset("offsetA / 4u")}[offsetA % 4u])`,s||E?T.getByOffset("offsetB / 4u"):`${T.type.value}(${T.getByOffset("offsetB / 4u")}[offsetB % 4u])`))}
|
|
498
|
+
`}else $=y.setByOffset("global_idx",w(v.getByOffset("global_idx"),T.getByOffset("global_idx")));else{if(!a)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let _=(I,k,E="")=>{let C=`aData[indexA${k}][componentA${k}]`,O=`bData[indexB${k}][componentB${k}]`;return`
|
|
499
|
+
let outputIndices${k} = ${y.offsetToIndices(`global_idx * 4u + ${k}u`)};
|
|
500
|
+
let offsetA${k} = ${v.broadcastedIndicesToOffset(`outputIndices${k}`,y)};
|
|
501
|
+
let offsetB${k} = ${T.broadcastedIndicesToOffset(`outputIndices${k}`,y)};
|
|
502
|
+
let indexA${k} = offsetA${k} / 4u;
|
|
503
|
+
let indexB${k} = offsetB${k} / 4u;
|
|
504
|
+
let componentA${k} = offsetA${k} % 4u;
|
|
505
|
+
let componentB${k} = offsetB${k} % 4u;
|
|
506
|
+
${I}[${k}] = ${E}(${g(C,O)});
|
|
507
|
+
`};f===9?$=`
|
|
508
|
+
var data = vec4<u32>(0);
|
|
509
|
+
${_("data",0,"u32")}
|
|
510
|
+
${_("data",1,"u32")}
|
|
511
|
+
${_("data",2,"u32")}
|
|
512
|
+
${_("data",3,"u32")}
|
|
513
|
+
outputData[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:$=`
|
|
514
|
+
${_("outputData[global_idx]",0)}
|
|
515
|
+
${_("outputData[global_idx]",1)}
|
|
516
|
+
${_("outputData[global_idx]",2)}
|
|
517
|
+
${_("outputData[global_idx]",3)}
|
|
518
|
+
`}return`
|
|
519
|
+
${e.registerUniform("vec_size","u32").declareVariables(v,T,y)}
|
|
520
|
+
|
|
521
|
+
${h??""}
|
|
522
|
+
|
|
523
|
+
${e.mainStart()}
|
|
524
|
+
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
525
|
+
${$}
|
|
526
|
+
}`},Qu=(e,t,r,i,n,a,s=r.dataType)=>{let u=r.dims.map(Number),l=i.dims.map(Number),d=!R.areEqual(u,l),f=u,h=R.size(u),g=!1,w=!1,y=[d];if(d){let v=Qt.calcShape(u,l,!1);if(!v)throw new Error("Can't perform binary op on the given tensors");f=v.slice(),h=R.size(f);let T=R.size(u)===1,$=R.size(l)===1,_=u.length>0&&u[u.length-1]%4===0,I=l.length>0&&l[l.length-1]%4===0;y.push(T),y.push($),y.push(_),y.push(I);let k=1;for(let E=1;E<f.length;E++){let C=u[u.length-E],O=l[l.length-E];if(C===O)k*=C;else break}k%4===0?(w=!0,g=!0):(T||$||_||I)&&(g=!0)}else g=!0;return y.push(g),{name:e,shaderCache:{hint:t+y.map(v=>v.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:v=>ju(v,u,l,f,g,d,w,n,r.dataType,i.dataType,s,a),getRunData:()=>({outputs:[{dims:f,dataType:s}],dispatchGroup:{x:Math.ceil(h/64/4)},programUniforms:[{type:12,data:Math.ceil(R.size(f)/4)},...Z(u,l,f)]})}},Qe=(e,t,r,i,n,a)=>{e.compute(Qu(t,n??"",e.inputs[0],e.inputs[1],r,i,a))},ih=e=>{Qe(e,"Add",(t,r)=>`${t}+${r}`)},nh=e=>{Qe(e,"Div",(t,r)=>`${t}/${r}`)},ah=e=>{Qe(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4<u32>(${t}==${r})`},void 0,void 0,9)},sh=e=>{Qe(e,"Mul",(t,r)=>`${t}*${r}`)},oh=e=>{let t=B("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;Qe(e,"Pow",{scalar:(r,i)=>`pow_custom(${r},${i})`,vector:(r,i)=>`pow_vector_custom(${r},${i})`},`
|
|
527
|
+
fn pow_custom(a : ${t}, b : ${t}) -> ${t} {
|
|
528
|
+
if (b == ${t}(0.0)) {
|
|
529
|
+
return ${t}(1.0);
|
|
530
|
+
} else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) {
|
|
531
|
+
return ${t}(pow(f32(a), f32(b))); // NaN
|
|
532
|
+
}
|
|
533
|
+
return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b))));
|
|
534
|
+
}
|
|
535
|
+
fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> {
|
|
536
|
+
// TODO: implement vectorized pow
|
|
537
|
+
return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w));
|
|
538
|
+
}
|
|
539
|
+
`)},uh=e=>{Qe(e,"Sub",(t,r)=>`${t}-${r}`)},lh=e=>{Qe(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4<u32>(${t}>${r})`},void 0,void 0,9)},dh=e=>{Qe(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4<u32>(${t}<${r})`},void 0,void 0,9)},ph=e=>{Qe(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4<u32>(${t}>=${r})`},void 0,void 0,9)},ch=e=>{Qe(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4<u32>(${t}<=${r})`},void 0,void 0,9)}}),n0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Zu=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,i=e[r],n=i.dataType,a=i.dims.length;e.forEach((s,u)=>{if(u!==r){if(s.dataType!==n)throw new Error("input tensors should be one type");if(s.dims.length!==a)throw new Error("input tensors should have the same shape");s.dims.forEach((l,d)=>{if(d!==t&&l!==i.dims[d])throw new Error("non concat dimensions must match")})}})},Yu=(e,t)=>`
|
|
540
|
+
fn calculateInputIndex(index: u32) -> u32 {
|
|
541
|
+
let sizeInConcatAxis = array<u32, ${e}u>(${t});
|
|
542
|
+
for (var i: u32 = 0u; i < ${e}; i += 1u ) {
|
|
543
|
+
if (index < sizeInConcatAxis[i]) {
|
|
544
|
+
return i;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return ${e}u;
|
|
548
|
+
}`,Xu=(e,t)=>{let r=e.length,i=[];for(let n=0;n<r;++n){let a=t.setByOffset("global_idx",e[n].getByIndices("indices"));r===1?i.push(a):n===0?i.push(`if (inputIndex == ${n}u) { ${a} }`):n===r-1?i.push(`else { ${a} }`):i.push(`else if (inputIndex == ${n}) { ${a} }`)}return i.join(`
|
|
549
|
+
`)},Ju=(e,t,r,i)=>{let n=R.size(r),a=new Array(e.length),s=new Array(e.length),u=0,l=[],d=[],f=[{type:12,data:n}];for(let v=0;v<e.length;++v)u+=e[v].dims[t],a[v]=u,d.push(e[v].dims.length),s[v]=B(`input${v}`,i,d[v]),l.push("rank"),f.push({type:12,data:a[v]});for(let v=0;v<e.length;++v)f.push(...Z(e[v].dims));f.push(...Z(r));let h=F("output",i,r.length),g=h.indicesGet("indices",t),w=Array.from(Array(a.length).keys()).map(v=>`uniforms.sizeInConcatAxis${v}`).join(","),y=v=>`
|
|
550
|
+
|
|
551
|
+
${(()=>{v.registerUniform("outputSize","u32");for(let T=0;T<e.length;T++)v.registerUniform(`sizeInConcatAxis${T}`,"u32");return v.declareVariables(...s,h)})()}
|
|
552
|
+
|
|
553
|
+
${Yu(a.length,w)}
|
|
554
|
+
|
|
555
|
+
${v.mainStart()}
|
|
556
|
+
${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
557
|
+
|
|
558
|
+
var indices = ${h.offsetToIndices("global_idx")};
|
|
559
|
+
|
|
560
|
+
let inputIndex = calculateInputIndex(${g});
|
|
561
|
+
if (inputIndex != 0u) {
|
|
562
|
+
let sizeInConcatAxis = array<u32, ${a.length}u>(${w});
|
|
563
|
+
${g} -= sizeInConcatAxis[inputIndex - 1u];
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
${Xu(s,h)}
|
|
567
|
+
}`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:r,dataType:i}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:f}),getShaderSource:y}},hh=(e,t)=>{let r=e.inputs,i=r[0].dims,n=R.normalizeAxis(t.axis,i.length);Zu(r,n);let a=i.slice();a[n]=r.reduce((u,l)=>u+(l.dims.length>n?l.dims[n]:0),0);let s=r.filter(u=>R.size(u.dims)>0);e.compute(Ju(s,n,a,r[0].dataType),{inputs:s})},fh=e=>me({axis:e.axis})}),Wt=P(()=>{"use strict";ee(),ie(),Nt=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value));
|
|
568
|
+
value = sign(value) * (1.0 - e2x) / (1.0 + e2x);
|
|
569
|
+
`;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},Dt=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},Pt=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},_a=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,i]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:i}}else if(t==="Clip"){let[r,i]=e?.activation_params||[Wp,Lp];return{activation:t,clipMax:i,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}}),ba=P(()=>{"use strict";ze=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},mh=e=>`
|
|
570
|
+
${e?"value = value + getBiasByOutputCoords(coords);":""}
|
|
571
|
+
`}),a0=P(()=>{"use strict";gh=e=>`
|
|
572
|
+
fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {
|
|
573
|
+
return dot(coords, vec4<i32>(
|
|
574
|
+
shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));
|
|
575
|
+
}
|
|
576
|
+
fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
|
|
577
|
+
return dot(coords, vec4<i32>(
|
|
578
|
+
i32(${e}.x), i32(${e}.y), i32(${e}.z), 1));
|
|
579
|
+
}
|
|
580
|
+
`}),$a=P(()=>{"use strict";ee(),ie(),ne(),Wt(),vr=(e,t,r,i,n)=>{let a=i-r;return`
|
|
581
|
+
${Array.from({length:r}).map((s,u)=>`
|
|
582
|
+
if (${K(t.shape,u,t.rank)} != 1) {
|
|
583
|
+
${t.indicesSet(e,u,K(n,u+a,i))}
|
|
584
|
+
} else {
|
|
585
|
+
${t.indicesSet(e,u,0)}
|
|
586
|
+
}`).join("")}
|
|
587
|
+
`},va=(e,t,r,i,n=!1,a)=>{let s=e[0].dims,u=e[1].dims,l=s[s.length-2],d=u[u.length-1],f=s[s.length-1],h=xe(d),g=xe(f),w=xe(l),y=R.size(r)/h/w,v=e.length>2,T=i?i.slice(0,-2):r.slice(0,-2),$=[R.size(T),l,d],_=[{type:12,data:y},{type:12,data:l},{type:12,data:d},{type:12,data:f}];Dt(t,_),_.push(...Z(T,s,u)),v&&_.push(...Z(e[2].dims)),_.push(...Z($));let I=k=>{let E=ma("batch_dims",e[0].dataType,T.length),C=B("a",e[0].dataType,s.length,g),O=B("b",e[1].dataType,u.length,h),x=F("output",e[0].dataType,$.length,h),D=Ee(x.type.tensor),L=Nt(t,x.type.value,D),Y=[C,O],q="";if(v){let A=n?h:1;Y.push(B("bias",e[2].dataType,e[2].dims.length,A)),q=`${n?`value += bias[col / ${A}];`:`value += ${x.type.value}(bias[row + i]);`}`}let j=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Pt(t,j);let ue=()=>{let A=`var a_data: ${C.type.value};`;for(let U=0;U<g;U++)A+=`
|
|
588
|
+
let b_data${U} = b[(b_offset + (k + ${U}) * uniforms.N + col) / ${h}];`;for(let U=0;U<w;U++){A+=`a_data = a[(a_offset + (row + ${U}) * uniforms.K + k) / ${g}];`;for(let re=0;re<g;re++)A+=`
|
|
589
|
+
values[${U}] = fma(${O.type.value}(a_data${g===1?"":`[${re}]`}), b_data${re}, values[${U}]);
|
|
590
|
+
`}return A};return`
|
|
591
|
+
${k.registerUniforms(j).registerInternalVariables(E).declareVariables(...Y,x)}
|
|
592
|
+
${k.mainStart()}
|
|
593
|
+
${k.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
594
|
+
let col = (global_idx % (uniforms.N / ${h})) * ${h};
|
|
595
|
+
var index1 = global_idx / (uniforms.N / ${h});
|
|
596
|
+
let stride1 = uniforms.M / ${w};
|
|
597
|
+
let row = (index1 % stride1) * ${w};
|
|
598
|
+
let batch = index1 / stride1;
|
|
599
|
+
|
|
600
|
+
${r.length===2?"":`let batch_indices = ${E.offsetToIndices("batch")};`}
|
|
601
|
+
|
|
602
|
+
var a_indices: ${C.type.indices};
|
|
603
|
+
${vr("a_indices",C,C.rank-2,E.rank,"batch_indices")}
|
|
604
|
+
${C.indicesSet("a_indices",C.rank-2,0)}
|
|
605
|
+
${C.indicesSet("a_indices",C.rank-1,0)}
|
|
606
|
+
let a_offset = ${C.indicesToOffset("a_indices")};
|
|
607
|
+
|
|
608
|
+
var b_indices: ${O.type.indices};
|
|
609
|
+
${vr("b_indices",O,O.rank-2,E.rank,"batch_indices")}
|
|
610
|
+
${O.indicesSet("b_indices",O.rank-2,0)}
|
|
611
|
+
${O.indicesSet("b_indices",O.rank-1,0)}
|
|
612
|
+
let b_offset = ${O.indicesToOffset("b_indices")};
|
|
613
|
+
var values: array<${x.type.value}, ${w}>;
|
|
614
|
+
for (var k: u32 = 0u; k < uniforms.K; k = k + ${g}) {
|
|
615
|
+
${ue()}
|
|
616
|
+
}
|
|
617
|
+
for (var i = 0u; i < ${w}u; i++) {
|
|
618
|
+
var value = values[i];
|
|
619
|
+
${q}
|
|
620
|
+
${L}
|
|
621
|
+
let cur_indices = ${x.type.indices}(batch, row + i, col);
|
|
622
|
+
let offset = ${x.indicesToOffset("cur_indices")};
|
|
623
|
+
${x.setByOffset(`offset / ${h}`,"value")};
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
`};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${h};${g};${w};${n}`,inputDependencies:v?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:a?a(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:_}),getShaderSource:I}}}),xa=P(()=>{"use strict";ee(),ie(),ne(),Wt(),$a(),ba(),el=(e,t)=>e?`
|
|
627
|
+
mm_Asub[inputRow][inputCol] = mm_readA(batch,
|
|
628
|
+
kStart + inputRow,
|
|
629
|
+
globalRowStart / innerElementSize + inputCol${t?", batchIndices":""});
|
|
630
|
+
`:`
|
|
631
|
+
mm_Asub[inputRow][inputCol] = mm_readA(batch,
|
|
632
|
+
globalRow + innerRow,
|
|
633
|
+
kStart / innerElementSize + inputCol${t?", batchIndices":""});
|
|
634
|
+
`,tl=(e,t)=>e?`
|
|
635
|
+
let ACached0 = mm_Asub[k * innerElementSize][localRow];
|
|
636
|
+
let ACached1 = mm_Asub[k * innerElementSize + 1][localRow];
|
|
637
|
+
let ACached2 = mm_Asub[k * innerElementSize + 2][localRow];
|
|
638
|
+
${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"}
|
|
639
|
+
for (var i = 0; i < rowPerThread; i = i + 1) {
|
|
640
|
+
acc[i] = BCached0 * ACached0[i] + acc[i];
|
|
641
|
+
acc[i] = BCached1 * ACached1[i] + acc[i];
|
|
642
|
+
acc[i] = BCached2 * ACached2[i] + acc[i];
|
|
643
|
+
${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"}
|
|
644
|
+
}`:`
|
|
645
|
+
for (var i = 0; i < rowPerThread; i = i + 1) {
|
|
646
|
+
let ACached = mm_Asub[tileRow + i][k];
|
|
647
|
+
acc[i] = BCached0 * ACached.x + acc[i];
|
|
648
|
+
acc[i] = BCached1 * ACached.y + acc[i];
|
|
649
|
+
acc[i] = BCached2 * ACached.z + acc[i];
|
|
650
|
+
${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"}
|
|
651
|
+
}`,Qn=(e,t,r="f32",i,n=!1,a=32,s=!1,u=32)=>{let l=t[1]*e[1],d=t[0]*e[0],f=n?l:a,h=n?a:l,g=f/t[0],w=a/t[1];if(!((n&&g===4&&e[1]===4||!n&&(g===3||g===4))&&f%t[0]===0&&a%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${n} is true, innerElementSize ${g} and workPerThread[1] ${e[1]} must be 4.
|
|
652
|
+
Otherwise, innerElementSize ${g} must be 3 or 4.
|
|
653
|
+
tileAWidth ${f} must be divisible by workgroupSize[0]${t[0]}. tileInner ${a} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return`
|
|
654
|
+
var<workgroup> mm_Asub: array<array<vec${g}<${r}>, ${f/g}>, ${h}>;
|
|
655
|
+
var<workgroup> mm_Bsub: array<array<vec4<${r}>, ${d/e[0]}>, ${a}>;
|
|
656
|
+
|
|
657
|
+
const rowPerThread = ${e[1]};
|
|
658
|
+
const colPerThread = ${e[0]};
|
|
659
|
+
const innerElementSize = ${g};
|
|
660
|
+
const tileInner = ${a};
|
|
661
|
+
|
|
662
|
+
@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})
|
|
663
|
+
fn main(@builtin(local_invocation_id) localId : vec3<u32>,
|
|
664
|
+
@builtin(global_invocation_id) globalId : vec3<u32>,
|
|
665
|
+
@builtin(workgroup_id) workgroupId : vec3<u32>) {
|
|
666
|
+
let localRow = i32(localId.y);
|
|
667
|
+
let tileRow = localRow * rowPerThread;
|
|
668
|
+
let tileCol = i32(localId.x);
|
|
669
|
+
|
|
670
|
+
let globalRow =i32(globalId.y) * rowPerThread;
|
|
671
|
+
let globalCol = i32(globalId.x);
|
|
672
|
+
let batch = ${s?"0":"i32(globalId.z)"};
|
|
673
|
+
${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""}
|
|
674
|
+
let globalRowStart = i32(workgroupId.y) * ${l};
|
|
675
|
+
|
|
676
|
+
let num_tiles = ${s?`${Math.ceil(u/a)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};
|
|
677
|
+
var kStart = ${s?`i32(globalId.z) * ${u}`:"0"};
|
|
678
|
+
|
|
679
|
+
var acc: array<vec4<${r}>, rowPerThread>;
|
|
680
|
+
|
|
681
|
+
// Loop over shared dimension.
|
|
682
|
+
let tileRowB = localRow * ${w};
|
|
683
|
+
for (var t = 0; t < num_tiles; t = t + 1) {
|
|
684
|
+
// Load one tile of A into local memory.
|
|
685
|
+
for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
|
|
686
|
+
let inputRow = tileRow + innerRow;
|
|
687
|
+
let inputCol = tileCol;
|
|
688
|
+
${el(n,i)}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// Load one tile of B into local memory.
|
|
692
|
+
for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) {
|
|
693
|
+
let inputRow = tileRowB + innerRow;
|
|
694
|
+
let inputCol = tileCol;
|
|
695
|
+
mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${i?", batchIndices":""});
|
|
696
|
+
}
|
|
697
|
+
kStart = kStart + tileInner;
|
|
698
|
+
workgroupBarrier();
|
|
699
|
+
|
|
700
|
+
// Compute acc values for a single thread.
|
|
701
|
+
for (var k = 0; k < tileInner / innerElementSize; k = k + 1) {
|
|
702
|
+
let BCached0 = mm_Bsub[k * innerElementSize][tileCol];
|
|
703
|
+
let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol];
|
|
704
|
+
let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol];
|
|
705
|
+
${g===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"}
|
|
706
|
+
|
|
707
|
+
${tl(n,g)}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
workgroupBarrier();
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
|
|
714
|
+
mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]);
|
|
715
|
+
}
|
|
716
|
+
}`},cn=(e,t)=>e?`
|
|
717
|
+
mm_Asub[inputRow][inputCol] = mm_readA(batch,
|
|
718
|
+
kStart + inputRow,
|
|
719
|
+
globalRowStart + inputCol${t?", batchIndices":""});
|
|
720
|
+
`:`
|
|
721
|
+
mm_Asub[inputRow][inputCol] = mm_readA(batch,
|
|
722
|
+
globalRowStart + inputRow,
|
|
723
|
+
kStart + inputCol${t?", batchIndices":""});
|
|
724
|
+
`,rl=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",Zn=(e,t,r="f32",i,n=!1,a=32,s=!1,u=32,l=!1)=>{let d=e[1]*t[1],f=e[0]*t[0],h=n?d:a,g=n?a:d;if(!(g%t[1]===0&&h%t[0]===0&&a%t[1]===0))throw new Error(`tileAHight ${g} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${h} must be divisible by workgroupSize[0]${t[0]}, tileInner ${a} must be divisible by workgroupSize[1]${t[1]}`);let w=g/t[1],y=h/t[0],v=a/t[1],T=l?`
|
|
725
|
+
let localRow = i32(localId.y);
|
|
726
|
+
let localCol = i32(localId.x);
|
|
727
|
+
let globalRowStart = i32(workgroupId.y) * ${d};
|
|
728
|
+
let globalColStart = i32(workgroupId.x) * ${f};
|
|
729
|
+
|
|
730
|
+
// Loop over shared dimension.
|
|
731
|
+
for (var t = 0; t < num_tiles; t = t + 1) {
|
|
732
|
+
// Load one tile of A into local memory.
|
|
733
|
+
for (var inputRow = localRow; inputRow < ${g}; inputRow = inputRow + ${t[1]}) {
|
|
734
|
+
for (var inputCol = localCol; inputCol < ${h}; inputCol = inputCol + ${t[0]}) {
|
|
735
|
+
${cn(n,i)}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
// Load one tile of B into local memory.
|
|
739
|
+
for (var inputRow = localRow; inputRow < ${a}; inputRow = inputRow + ${t[1]}) {
|
|
740
|
+
for (var inputCol = localCol; inputCol < ${f}; inputCol = inputCol + ${t[0]}) {
|
|
741
|
+
mm_Bsub[inputRow][inputCol] = mm_readB(batch,
|
|
742
|
+
kStart + inputRow,
|
|
743
|
+
globalColStart + inputCol${i?", batchIndices":""});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
kStart = kStart + tileInner;
|
|
747
|
+
workgroupBarrier();
|
|
748
|
+
|
|
749
|
+
// Compute acc values for a single thread.
|
|
750
|
+
var BCached : array<${r}, colPerThread>;
|
|
751
|
+
for (var k = 0; k < tileInner; k = k + 1) {
|
|
752
|
+
for (var inner = 0; inner < colPerThread; inner = inner + 1) {
|
|
753
|
+
BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}];
|
|
754
|
+
}
|
|
755
|
+
for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
|
|
756
|
+
let ACached = ${n?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`}
|
|
757
|
+
for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
|
|
758
|
+
acc[innerRow][innerCol] = acc[innerRow][innerCol] +
|
|
759
|
+
ACached * BCached[innerCol];
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
workgroupBarrier();
|
|
764
|
+
}
|
|
765
|
+
for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
|
|
766
|
+
let gRow = globalRowStart + localRow + innerRow * ${t[1]};
|
|
767
|
+
for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
|
|
768
|
+
let gCol = globalColStart + localCol + innerCol * ${t[0]};
|
|
769
|
+
mm_write(batch, gRow, gCol, acc[innerRow][innerCol]);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
`:`
|
|
773
|
+
let tileRow = i32(localId.y) * rowPerThread;
|
|
774
|
+
let tileCol = i32(localId.x) * colPerThread;
|
|
775
|
+
|
|
776
|
+
let globalRow = i32(globalId.y) * rowPerThread;
|
|
777
|
+
let globalCol = i32(globalId.x) * colPerThread;
|
|
778
|
+
let globalRowStart = i32(workgroupId.y) * ${d};
|
|
779
|
+
|
|
780
|
+
let tileRowA = i32(localId.y) * ${w};
|
|
781
|
+
let tileColA = i32(localId.x) * ${y};
|
|
782
|
+
let tileRowB = i32(localId.y) * ${v};
|
|
783
|
+
// Loop over shared dimension.
|
|
784
|
+
for (var t = 0; t < num_tiles; t = t + 1) {
|
|
785
|
+
// Load one tile of A into local memory.
|
|
786
|
+
for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) {
|
|
787
|
+
for (var innerCol = 0; innerCol < ${y}; innerCol = innerCol + 1) {
|
|
788
|
+
let inputRow = tileRowA + innerRow;
|
|
789
|
+
let inputCol = tileColA + innerCol;
|
|
790
|
+
${cn(n,i)}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// Load one tile of B into local memory.
|
|
795
|
+
for (var innerRow = 0; innerRow < ${v}; innerRow = innerRow + 1) {
|
|
796
|
+
for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
|
|
797
|
+
let inputRow = tileRowB + innerRow;
|
|
798
|
+
let inputCol = tileCol + innerCol;
|
|
799
|
+
mm_Bsub[inputRow][inputCol] = mm_readB(batch,
|
|
800
|
+
kStart + inputRow,
|
|
801
|
+
globalCol + innerCol${i?", batchIndices":""});
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
kStart = kStart + tileInner;
|
|
805
|
+
workgroupBarrier();
|
|
806
|
+
|
|
807
|
+
// Compute acc values for a single thread.
|
|
808
|
+
var BCached : array<${r}, colPerThread>;
|
|
809
|
+
for (var k = 0; k < tileInner; k = k + 1) {
|
|
810
|
+
for (var inner = 0; inner < colPerThread; inner = inner + 1) {
|
|
811
|
+
BCached[inner] = mm_Bsub[k][tileCol + inner];
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
|
|
815
|
+
${rl(n)}
|
|
816
|
+
for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
|
|
817
|
+
acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
workgroupBarrier();
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) {
|
|
826
|
+
for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) {
|
|
827
|
+
mm_write(batch, globalRow + innerRow, globalCol + innerCol,
|
|
828
|
+
acc[innerRow][innerCol]);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
`;return`
|
|
832
|
+
var<workgroup> mm_Asub : array<array<${r}, ${h}>, ${g}>;
|
|
833
|
+
var<workgroup> mm_Bsub : array<array<${r}, ${f}>, ${a}>;
|
|
834
|
+
const rowPerThread = ${e[1]};
|
|
835
|
+
const colPerThread = ${e[0]};
|
|
836
|
+
const tileInner = ${a};
|
|
837
|
+
|
|
838
|
+
@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]})
|
|
839
|
+
fn main(@builtin(local_invocation_id) localId : vec3<u32>,
|
|
840
|
+
@builtin(global_invocation_id) globalId : vec3<u32>,
|
|
841
|
+
@builtin(workgroup_id) workgroupId : vec3<u32>) {
|
|
842
|
+
let batch = ${s?"0":"i32(globalId.z)"};
|
|
843
|
+
${i?`let batchIndices = ${i.offsetToIndices("u32(batch)")};`:""}
|
|
844
|
+
let num_tiles = ${s?`${Math.ceil(u/a)}`:"(uniforms.dim_inner - 1) / tileInner + 1"};
|
|
845
|
+
var kStart = ${s?`i32(globalId.z) * ${u}`:"0"};
|
|
846
|
+
|
|
847
|
+
var acc : array<array<${r}, colPerThread>, rowPerThread>;
|
|
848
|
+
${T}
|
|
849
|
+
}
|
|
850
|
+
`},il=(e,t,r,i,n=!1)=>{let[a,s,u,l]=i,d=Ee(i[0].type.tensor);return`
|
|
851
|
+
fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${a.type.indices}) -> ${ze(e,d)} {
|
|
852
|
+
var value = ${ze(e,d)}(0.0);
|
|
853
|
+
let col = colIn * ${e};
|
|
854
|
+
if(row < uniforms.dim_a_outer && col < uniforms.dim_inner)
|
|
855
|
+
{
|
|
856
|
+
var aIndices: ${s.type.indices};
|
|
857
|
+
${vr("aIndices",s,s.rank-2,a.rank,"batchIndices")}
|
|
858
|
+
${s.indicesSet("aIndices",s.rank-2,"u32(row)")}
|
|
859
|
+
${s.indicesSet("aIndices",s.rank-1,"u32(colIn)")}
|
|
860
|
+
value = ${s.getByIndices("aIndices")};
|
|
861
|
+
}
|
|
862
|
+
return value;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${a.type.indices}) -> ${ze(e,d)} {
|
|
866
|
+
var value = ${ze(e,d)}(0.0);
|
|
867
|
+
let col = colIn * ${e};
|
|
868
|
+
if(row < uniforms.dim_inner && col < uniforms.dim_b_outer)
|
|
869
|
+
{
|
|
870
|
+
var bIndices: ${u.type.indices};
|
|
871
|
+
${vr("bIndices",u,u.rank-2,a.rank,"batchIndices")}
|
|
872
|
+
${u.indicesSet("bIndices",u.rank-2,"u32(row)")}
|
|
873
|
+
${u.indicesSet("bIndices",u.rank-1,"u32(colIn)")}
|
|
874
|
+
value = ${u.getByIndices("bIndices")};
|
|
875
|
+
}
|
|
876
|
+
return value;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${ze(e,d)}) {
|
|
880
|
+
let col = colIn * ${e};
|
|
881
|
+
if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) {
|
|
882
|
+
var value = valueIn;
|
|
883
|
+
let coords = vec3<i32>(batch, row, colIn);
|
|
884
|
+
${t?`value = value + ${n?"bias[colIn]":`${ze(e,d)}(bias[row])`};`:""}
|
|
885
|
+
${r}
|
|
886
|
+
${l.setByIndices("vec3<u32>(coords)","value")}
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
`},ui=(e,t,r,i,n=!1,a)=>{let s=e[0].dims,u=e[1].dims,l=s.slice(0,-2),d=u.slice(0,-2),f=i?i.slice(0,-2):r.slice(0,-2),h=R.size(f),g=s[s.length-2],w=s[s.length-1],y=u[u.length-1],v=w%4===0&&y%4===0,T=g<=8?[4,1,1]:[4,4,1],$=[8,8,1],_=[Math.ceil(y/$[0]/T[0]),Math.ceil(g/$[1]/T[1]),Math.ceil(h/$[2]/T[2])],I=v?4:1,k=[...l,g,w/I],E=k.length,C=[...d,w,y/I],O=C.length,x=[h,g,y/I],D=[{type:6,data:g},{type:6,data:y},{type:6,data:w}];Dt(t,D),D.push(...Z(f,k,C));let L=["rank","rank"],Y=e.length>2;Y&&(D.push(...Z(e[2].dims)),L.push("rank")),D.push(...Z(x));let q=j=>{let ue=f.length,A=ma("batchDims",e[0].dataType,ue,1),U=Ee(e[0].dataType),re=B("a",e[0].dataType,E,I),J=B("b",e[1].dataType,O,I),H=F("result",e[0].dataType,x.length,I),pe=[re,J];if(Y){let Ie=n?I:1;pe.push(B("bias",e[2].dataType,e[2].dims.length,Ie))}let N=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Pt(t,N);let V=Ee(H.type.tensor),te=Nt(t,H.type.value,V),ce=il(I,Y,te,[A,re,J,H],n);return`
|
|
890
|
+
${j.registerUniforms(N).registerInternalVariables(A).declareVariables(...pe,H)}
|
|
891
|
+
${ce}
|
|
892
|
+
${v?Qn(T,$,U,A):Zn(T,$,U,A)}
|
|
893
|
+
`};return{name:"MatMul",shaderCache:{hint:`${T};${t.activation};${v};${n}`,inputDependencies:L},getRunData:()=>({outputs:[{dims:a?a(r):r,dataType:e[0].dataType}],dispatchGroup:{x:_[0],y:_[1],z:_[2]},programUniforms:D}),getShaderSource:q}}}),s0=P(()=>{"use strict";ee(),lt(),ne(),Wt(),ba(),a0(),xa(),nl=(e,t,r,i,n=!1,a,s=4,u=4,l=4,d="f32")=>{let f=D=>{switch(D){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${d}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${D} is not supported.`)}},h=D=>{switch(D){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${D} is not supported.`)}},g=e?`
|
|
894
|
+
let coord = vec4<i32>(batch, xRow, xCol, xCh);
|
|
895
|
+
`:`
|
|
896
|
+
let coord = vec4<i32>(batch, xCh, xRow, xCol);
|
|
897
|
+
`,w=e?`
|
|
898
|
+
let coords = vec4<i32>(
|
|
899
|
+
batch,
|
|
900
|
+
row / outWidth,
|
|
901
|
+
row % outWidth,
|
|
902
|
+
col);
|
|
903
|
+
`:`
|
|
904
|
+
let coords = vec4<i32>(
|
|
905
|
+
batch,
|
|
906
|
+
row,
|
|
907
|
+
col / outWidth,
|
|
908
|
+
col % outWidth);
|
|
909
|
+
`,y=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",v=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",T=e?"row":"col",$=e?"col":"row",_=`
|
|
910
|
+
let inChannels = i32(uniforms.w_shape[2]);
|
|
911
|
+
let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
|
|
912
|
+
let outRow = ${T} / outWidth;
|
|
913
|
+
let outCol = ${T} % outWidth;
|
|
914
|
+
|
|
915
|
+
let WRow = ${$} / (i32(uniforms.w_shape[1]) * inChannels);
|
|
916
|
+
let WCol = ${$} / inChannels % i32(uniforms.w_shape[1]);
|
|
917
|
+
let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
|
|
918
|
+
let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
|
|
919
|
+
let xCh = ${$} % inChannels;
|
|
920
|
+
var resData = ${ze(s,d)}(0.0);
|
|
921
|
+
// The bounds checking is always needed since we use it to pad zero for
|
|
922
|
+
// the 'same' padding type.
|
|
923
|
+
if (xRow >= 0 && xRow < ${y} && xCol >= 0 && xCol < ${v}) {
|
|
924
|
+
${g}
|
|
925
|
+
let xIndex = getIndexFromCoords4D(coord, vec4<i32>(uniforms.x_shape));
|
|
926
|
+
${f(s)}
|
|
927
|
+
}
|
|
928
|
+
return resData;`,I=e?t&&i?`
|
|
929
|
+
let col = colIn * ${s};
|
|
930
|
+
${_}`:`
|
|
931
|
+
let col = colIn * ${s};
|
|
932
|
+
if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) {
|
|
933
|
+
${_}
|
|
934
|
+
}
|
|
935
|
+
return ${ze(s,d)}(0.0);`:i&&r?`
|
|
936
|
+
let col = colIn * ${s};
|
|
937
|
+
${_}`:`
|
|
938
|
+
let col = colIn * ${s};
|
|
939
|
+
if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
|
|
940
|
+
${_}
|
|
941
|
+
}
|
|
942
|
+
return ${ze(s,d)}(0.0);`,k=e?i&&r?h(u):`
|
|
943
|
+
let col = colIn * ${u};
|
|
944
|
+
if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) {
|
|
945
|
+
${h(u)}
|
|
946
|
+
}
|
|
947
|
+
return ${ze(u,d)}(0.0);`:`
|
|
948
|
+
let col = colIn * ${u};
|
|
949
|
+
if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) {
|
|
950
|
+
${h(u)}
|
|
951
|
+
}
|
|
952
|
+
return ${ze(u,d)}(0.0);`,E=ze(l,d),C=ze(e?s:u,d),O=ze(e?u:s,d),x=Nt(a,E,d);return`
|
|
953
|
+
fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${C} {
|
|
954
|
+
${e?I:k}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${O} {
|
|
958
|
+
${e?k:I}
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${E}) {
|
|
962
|
+
let col = colIn * ${l};
|
|
963
|
+
if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer)
|
|
964
|
+
{
|
|
965
|
+
var value = valueIn;
|
|
966
|
+
let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"};
|
|
967
|
+
${w}
|
|
968
|
+
${mh(n)}
|
|
969
|
+
${x}
|
|
970
|
+
setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);
|
|
971
|
+
}
|
|
972
|
+
}`},yh=(e,t,r,i,n,a,s,u,l)=>{let d=t.format==="NHWC",f=d?e[0].dims[3]:e[0].dims[1],h=r[0],g=d?r[2]:r[3],w=d?r[1]:r[2],y=d?r[3]:r[1],v=d&&(f%4===0||f%3===0)&&y%4===0,T=d?y:g*w,$=d?g*w:y,_=[8,8,1],I=i<=8?[4,1,1]:[4,4,1],k=[Math.ceil(T/_[0]/I[0]),Math.ceil($/_[1]/I[1]),Math.ceil(h/_[2]/I[2])];de("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${k}`);let E=v?d&&f%4!==0?3:4:1,C=_[1]*I[1],O=_[0]*I[0],x=Math.max(_[0]*E,_[1]),D=i%C===0,L=n%O===0,Y=a%x===0,q=v?[E,4,4]:[1,1,1],j=[{type:6,data:i},{type:6,data:n},{type:6,data:a},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];Dt(t,j),j.push(...Z(e[0].dims,e[1].dims));let ue=["rank","rank"];s&&(j.push(...Z(e[2].dims)),ue.push("rank")),j.push(...Z(r));let A=U=>{let re=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];Pt(t,re);let J=v?4:1,H=Ee(e[0].dataType),pe=`
|
|
973
|
+
fn setOutputAtIndex(flatIndex : i32, value : ${v?`vec4<${H}>`:H}) {
|
|
974
|
+
result[flatIndex] = ${v?`vec4<${H}>`:H}(value);
|
|
975
|
+
}
|
|
976
|
+
fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${v?`vec4<${H}>`:H}) {
|
|
977
|
+
let flatIndex = getOutputIndexFromCoords(vec4<i32>(d0, d1, d2, d3));
|
|
978
|
+
setOutputAtIndex(flatIndex ${v?"/ 4":""}, value);
|
|
979
|
+
}`,N=B("x",e[0].dataType,e[0].dims.length,E===3?1:E),V=B("w",e[1].dataType,e[1].dims.length,J),te=[N,V],ce=F("result",e[0].dataType,r.length,J);if(s){let Ie=B("bias",e[2].dataType,e[2].dims.length,J);te.push(Ie),pe+=`
|
|
980
|
+
fn getBiasByOutputCoords(coords : vec4<i32>) -> ${v?`vec4<${H}>`:H} {
|
|
981
|
+
return bias[coords.${d?"w":"y"}${v?"/ 4":""}];
|
|
982
|
+
}`}return`
|
|
983
|
+
${gh("uniforms.result_strides")}
|
|
984
|
+
//struct Uniforms { xShape : vec4<i32>, wShape : vec4<i32>, outShape : vec4<i32>,
|
|
985
|
+
// outShapeStrides: vec3<i32>, filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>,
|
|
986
|
+
// dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32 };
|
|
987
|
+
${U.registerUniforms(re).declareVariables(...te,ce)}
|
|
988
|
+
${pe}
|
|
989
|
+
${nl(d,D,L,Y,s,t,q[0],q[1],q[2],H)}
|
|
990
|
+
${v?Qn(I,_,H,void 0,!d,x):Zn(I,_,H,void 0,!d,x,!1,void 0,u)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${E};${v};${D};${L};${Y};${C};${O};${x}`,inputDependencies:ue},getRunData:()=>({outputs:[{dims:l?l(r):r,dataType:e[0].dataType}],dispatchGroup:{x:k[0],y:k[1],z:k[2]},programUniforms:j}),getShaderSource:A}}}),o0=P(()=>{"use strict";ee(),lt(),ie(),ne(),Wt(),ba(),al=e=>{let t=1;for(let r=0;r<e.length;r++)t*=e[r];return t},hn=e=>typeof e=="number"?[e,e,e]:e,pr=(e,t)=>t<=1?e:e+(e-1)*(t-1),sl=(e,t,r,i=1)=>{let n=pr(t,i);return Math.floor((e[0]*(r-1)-r+n)/2)},fn=(e,t,r,i,n)=>{n==null&&(n=sl(e,t[0],i[0]));let a=[0,0,0,r];for(let s=0;s<3;s++)e[s]+2*n>=t[s]&&(a[s]=Math.trunc((e[s]-t[s]+2*n)/i[s]+1));return a},ol=(e,t,r,i,n,a,s,u,l,d)=>{let f,h,g,w;if(e==="VALID"&&(e=0),typeof e=="number"){f={top:e,bottom:e,left:e,right:e,front:e,back:e};let y=fn([t,r,i,1],[u,l,d],1,[n,a,s],e);h=y[0],g=y[1],w=y[2]}else if(Array.isArray(e)){if(!e.every((v,T,$)=>v===$[0]))throw Error(`Unsupported padding parameter: ${e}`);f={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let y=fn([t,r,i,1],[u,l,d],1,[n,a,s],e[0]);h=y[0],g=y[1],w=y[2]}else if(e==="SAME_UPPER"){h=Math.ceil(t/n),g=Math.ceil(r/a),w=Math.ceil(i/s);let y=(h-1)*n+u-t,v=(g-1)*a+l-r,T=(w-1)*s+d-i,$=Math.floor(y/2),_=y-$,I=Math.floor(v/2),k=v-I,E=Math.floor(T/2),C=T-E;f={top:I,bottom:k,left:E,right:C,front:$,back:_}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:f,outDepth:h,outHeight:g,outWidth:w}},wh=(e,t,r,i,n,a=!1,s="channelsLast")=>{let u,l,d,f,h;if(s==="channelsLast")[u,l,d,f,h]=e;else if(s==="channelsFirst")[u,h,l,d,f]=e;else throw new Error(`Unknown dataFormat ${s}`);let[g,,w,y,v]=t,[T,$,_]=hn(r),[I,k,E]=hn(i),C=pr(w,I),O=pr(y,k),x=pr(v,E),{padInfo:D,outDepth:L,outHeight:Y,outWidth:q}=ol(n,l,d,f,T,$,_,C,O,x),j=a?g*h:g,ue=[0,0,0,0,0];return s==="channelsFirst"?ue=[u,j,L,Y,q]:s==="channelsLast"&&(ue=[u,L,Y,q,j]),{batchSize:u,dataFormat:s,inDepth:l,inHeight:d,inWidth:f,inChannels:h,outDepth:L,outHeight:Y,outWidth:q,outChannels:j,padInfo:D,strideDepth:T,strideHeight:$,strideWidth:_,filterDepth:w,filterHeight:y,filterWidth:v,effectiveFilterDepth:C,effectiveFilterHeight:O,effectiveFilterWidth:x,dilationDepth:I,dilationHeight:k,dilationWidth:E,inShape:e,outShape:ue,filterShape:t}},_h=(e,t,r,i,n,a)=>{let s=a==="channelsLast",u=s?e[0].dims[3]:e[0].dims[1],l=!1,d=[64,1,1],f={x:r.map((_,I)=>I)},h=[Math.ceil(al(f.x.map(_=>r[_]))/d[0]),1,1];de("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${h}`);let g=l?s&&u%4!==0?3:4:1,w=R.size(r),y=[{type:12,data:w},{type:12,data:i},{type:12,data:n},{type:12,data:t.strides},{type:12,data:t.dilations}];Dt(t,y),y.push(...Z(e[0].dims,e[1].dims));let v=["rank","rank"],T=e.length===3;T&&(y.push(...Z(e[2].dims)),v.push("rank")),y.push(...Z(r));let $=_=>{let I=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:i.length},{name:"pads",type:"u32",length:n.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Pt(t,I);let k=l?4:1,E=Ee(e[0].dataType),C=B("x",e[0].dataType,e[0].dims.length,g===3?1:g),O=B("W",e[1].dataType,e[1].dims.length,k),x=[C,O],D=F("result",e[0].dataType,r.length,k),L="";if(T){let j=B("bias",e[2].dataType,e[2].dims.length,k);x.push(j),L+=`
|
|
991
|
+
fn getBiasByOutputCoords(coords : array<u32, 5>) -> ${l?`vec4<${E}>`:E} {
|
|
992
|
+
return bias[${s?K("coords",4,5):K("coords",1,5)}${l?"/ 4":""}];
|
|
993
|
+
}`}let Y=ze(g,E),q=Nt(t,Y,E);return`
|
|
994
|
+
${L}
|
|
995
|
+
fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
|
|
996
|
+
let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
|
|
997
|
+
return ${C.getByIndices("aIndices")};
|
|
998
|
+
}
|
|
999
|
+
fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 {
|
|
1000
|
+
let aIndices = array<u32, 5>(d0, d1, d2, d3, d4);
|
|
1001
|
+
return ${O.getByIndices("aIndices")};
|
|
1002
|
+
}
|
|
1003
|
+
${_.registerUniforms(I).declareVariables(...x,D)}
|
|
1004
|
+
${_.mainStart()}
|
|
1005
|
+
${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1006
|
+
let coords = ${D.offsetToIndices("global_idx")};
|
|
1007
|
+
let batch = ${K("coords",0,C.rank)};
|
|
1008
|
+
let d2 = ${s?K("coords",C.rank-1,C.rank):K("coords",1,C.rank)};
|
|
1009
|
+
let xFRCCorner = vec3<u32>(${s?K("coords",1,C.rank):K("coords",2,C.rank)},
|
|
1010
|
+
${s?K("coords",2,C.rank):K("coords",3,C.rank)},
|
|
1011
|
+
${s?K("coords",3,C.rank):K("coords",4,C.rank)}) * uniforms.strides - uniforms.pads;
|
|
1012
|
+
let xFCorner = xFRCCorner.x;
|
|
1013
|
+
let xRCorner = xFRCCorner.y;
|
|
1014
|
+
let xCCorner = xFRCCorner.z;
|
|
1015
|
+
let xShapeY = ${s?K("uniforms.x_shape",1,C.rank):K("uniforms.x_shape",2,C.rank)};
|
|
1016
|
+
let xShapeZ = ${s?K("uniforms.x_shape",2,C.rank):K("uniforms.x_shape",3,C.rank)};
|
|
1017
|
+
let xShapeW = ${s?K("uniforms.x_shape",3,C.rank):K("uniforms.x_shape",4,C.rank)};
|
|
1018
|
+
let xShapeU = ${s?K("uniforms.x_shape",4,C.rank):K("uniforms.x_shape",1,C.rank)};
|
|
1019
|
+
let inputDepthNearestVec4 = (xShapeU / 4) * 4;
|
|
1020
|
+
let inputDepthVec4Remainder = xShapeU % 4;
|
|
1021
|
+
|
|
1022
|
+
var value = 0.0;
|
|
1023
|
+
for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) {
|
|
1024
|
+
let xF = xFCorner + wF * uniforms.dilations[0];
|
|
1025
|
+
if (xF < 0 || xF >= xShapeY) {
|
|
1026
|
+
continue;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) {
|
|
1030
|
+
let xR = xRCorner + wR * uniforms.dilations[1];
|
|
1031
|
+
if (xR < 0 || xR >= xShapeZ) {
|
|
1032
|
+
continue;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) {
|
|
1036
|
+
let xC = xCCorner + wC * uniforms.dilations[2];
|
|
1037
|
+
if (xC < 0 || xC >= xShapeW) {
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) {
|
|
1042
|
+
${s?`let xValues = vec4<f32>(
|
|
1043
|
+
getX(batch, xF, xR, xC, d1),
|
|
1044
|
+
getX(batch, xF, xR, xC, d1 + 1),
|
|
1045
|
+
getX(batch, xF, xR, xC, d1 + 2),
|
|
1046
|
+
getX(batch, xF, xR, xC, d1 + 3));
|
|
1047
|
+
`:`let xValues = vec4<f32>(
|
|
1048
|
+
getX(batch, d1, xF, xR, xC),
|
|
1049
|
+
getX(batch, d1 + 1, xF, xR, xC),
|
|
1050
|
+
getX(batch, d1 + 2, xF, xR, xC),
|
|
1051
|
+
getX(batch, d1 + 3, xF, xR, xC));
|
|
1052
|
+
`}
|
|
1053
|
+
let wValues = vec4<f32>(
|
|
1054
|
+
getW(d2, d1, wF, wR, wC),
|
|
1055
|
+
getW(d2, d1 + 1, wF, wR, wC),
|
|
1056
|
+
getW(d2, d1 + 2, wF, wR, wC),
|
|
1057
|
+
getW(d2, d1 + 3, wF, wR, wC));
|
|
1058
|
+
value += dot(xValues, wValues);
|
|
1059
|
+
}
|
|
1060
|
+
if (inputDepthVec4Remainder == 1) {
|
|
1061
|
+
${s?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4)
|
|
1062
|
+
* getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC)
|
|
1063
|
+
* getW(d2, inputDepthNearestVec4, wF, wR, wC);`}
|
|
1064
|
+
} else if (inputDepthVec4Remainder == 2) {
|
|
1065
|
+
${s?`let xValues = vec2<f32>(
|
|
1066
|
+
getX(batch, xF, xR, xC, inputDepthNearestVec4),
|
|
1067
|
+
getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1));
|
|
1068
|
+
`:`let xValues = vec2<f32>(
|
|
1069
|
+
getX(batch, inputDepthNearestVec4, xF, xR, xC),
|
|
1070
|
+
getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC));
|
|
1071
|
+
`}
|
|
1072
|
+
let wValues = vec2<f32>(
|
|
1073
|
+
getW(d2, inputDepthNearestVec4, wF, wR, wC),
|
|
1074
|
+
getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC));
|
|
1075
|
+
value += dot(xValues, wValues);
|
|
1076
|
+
} else if (inputDepthVec4Remainder == 3) {
|
|
1077
|
+
${s?`let xValues = vec3<f32>(
|
|
1078
|
+
getX(batch, xF, xR, xC, inputDepthNearestVec4),
|
|
1079
|
+
getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1),
|
|
1080
|
+
getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2));
|
|
1081
|
+
`:`let xValues = vec3<f32>(
|
|
1082
|
+
getX(batch, inputDepthNearestVec4, xF, xR, xC),
|
|
1083
|
+
getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC),
|
|
1084
|
+
getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC));
|
|
1085
|
+
`}
|
|
1086
|
+
let wValues = vec3<f32>(
|
|
1087
|
+
getW(d2, inputDepthNearestVec4, wF, wR, wC),
|
|
1088
|
+
getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC),
|
|
1089
|
+
getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC));
|
|
1090
|
+
value += dot(xValues, wValues);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
${T?"value = value + getBiasByOutputCoords(coords)":""};
|
|
1096
|
+
${q}
|
|
1097
|
+
result[global_idx] = f32(value);
|
|
1098
|
+
}`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${s};${g};${T}`,inputDependencies:v},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:h[0],y:h[1],z:h[2]},programUniforms:y}),getShaderSource:$}}}),u0=P(()=>{"use strict";ee(),ie(),ne(),Wt(),bh=(e,t,r,i)=>{let n=e.length>2,a=n?"value += b[output_channel];":"",s=e[0].dims,u=e[1].dims,l=t.format==="NHWC",d=l?r[3]:r[1],f=d/t.group,h=l&&f>=4?xe(d):1,g=R.size(r)/h,w=[{type:12,data:g},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:f}];Dt(t,w),w.push(...Z(s,[u[0],u[1],u[2],u[3]/h]));let y=n?["rank","rank","rank"]:["rank","rank"];w.push(...Z([r[0],r[1],r[2],r[3]/h]));let v=T=>{let $=F("output",e[0].dataType,r.length,h),_=Ee($.type.tensor),I=Nt(t,$.type.value,_),k=B("x",e[0].dataType,s.length),E=B("w",e[1].dataType,u.length,h),C=[k,E];n&&C.push(B("b",e[2].dataType,e[2].dims,h));let O=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];Pt(t,O);let x=l?`
|
|
1099
|
+
for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) {
|
|
1100
|
+
let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];
|
|
1101
|
+
|
|
1102
|
+
if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) {
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) {
|
|
1107
|
+
let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];
|
|
1108
|
+
if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) {
|
|
1109
|
+
continue;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) {
|
|
1113
|
+
let input_channel = in_channel_offset + wInChannel;
|
|
1114
|
+
let xVal = ${k.get("batch","xHeight","xWidth","input_channel")};
|
|
1115
|
+
let wVal = ${E.get("wHeight","wWidth","wInChannel","output_channel")};
|
|
1116
|
+
value += xVal * wVal;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
`:`
|
|
1121
|
+
for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) {
|
|
1122
|
+
let input_channel = in_channel_offset + wInChannel;
|
|
1123
|
+
for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) {
|
|
1124
|
+
let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0];
|
|
1125
|
+
|
|
1126
|
+
if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) {
|
|
1127
|
+
continue;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) {
|
|
1131
|
+
let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1];
|
|
1132
|
+
if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) {
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
let xVal = ${k.get("batch","input_channel","xHeight","xWidth")};
|
|
1137
|
+
let wVal = ${E.get("output_channel","wInChannel","wHeight","wWidth")};
|
|
1138
|
+
value += xVal * wVal;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
`;return`
|
|
1143
|
+
${T.registerUniforms(O).declareVariables(...C,$)}
|
|
1144
|
+
|
|
1145
|
+
${T.mainStart()}
|
|
1146
|
+
${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1147
|
+
|
|
1148
|
+
let outputIndices = ${$.offsetToIndices("global_idx")};
|
|
1149
|
+
let batch: u32 = outputIndices[0];
|
|
1150
|
+
let output_channel: u32 = outputIndices[${l?3:1}];
|
|
1151
|
+
let xRCCorner: vec2<u32> = vec2<u32>(outputIndices[${l?1:2}], outputIndices[${l?2:3}]) * uniforms.strides - uniforms.pads;
|
|
1152
|
+
let group_id: u32 = output_channel * ${h} / uniforms.output_channels_per_group;
|
|
1153
|
+
var in_channel_offset = group_id * uniforms.w_shape[${l?2:1}];
|
|
1154
|
+
|
|
1155
|
+
var value: ${$.type.value} = ${$.type.value}(0);
|
|
1156
|
+
${x}
|
|
1157
|
+
${a}
|
|
1158
|
+
${I}
|
|
1159
|
+
${$.setByOffset("global_idx","value")}
|
|
1160
|
+
}`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${h}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:w}),getShaderSource:v}},vh=(e,t,r,i)=>{let n=e.length>2,a=xe(r[3]),s=xe(r[2]),u=R.size(r)/a/s,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/a],d=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/a],f=[r[0],r[1],r[2],r[3]/a],h=[{type:12,data:u},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];Dt(t,h),h.push(...Z(l,d,f));let g=(s-1)*t.strides[1]+d[1],w=y=>{let v=F("output",e[0].dataType,f.length,a),T=Ee(v.type.tensor),$=Nt(t,v.type.value,T),_=B("x",e[0].dataType,l.length,a),I=B("w",e[1].dataType,d.length,a),k=[_,I];n&&k.push(B("b",e[2].dataType,e[2].dims,a));let E=n?"value += b[output_channel];":"",C=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Pt(t,C),`
|
|
1161
|
+
${y.registerUniforms(C).declareVariables(...k,v)}
|
|
1162
|
+
${y.mainStart()}
|
|
1163
|
+
${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1164
|
+
let width0 = uniforms.output_shape[3];
|
|
1165
|
+
let output_channel = global_idx % width0;
|
|
1166
|
+
var index1 = global_idx / width0;
|
|
1167
|
+
let width1 = uniforms.output_shape[2] / ${s}u;
|
|
1168
|
+
let col = (index1 % width1) * ${s}u;
|
|
1169
|
+
index1 = index1 / width1;
|
|
1170
|
+
let row = index1 % uniforms.output_shape[1];
|
|
1171
|
+
let batch = index1 / uniforms.output_shape[1];
|
|
1172
|
+
|
|
1173
|
+
let x_corner = vec2<i32>(i32(row), i32(col)) * uniforms.strides - uniforms.pads;
|
|
1174
|
+
|
|
1175
|
+
var x_vals: array<${_.type.value}, ${g}>;
|
|
1176
|
+
var values: array<${v.type.value}, ${s}>;
|
|
1177
|
+
let input_channel = output_channel;
|
|
1178
|
+
// Use constant instead of uniform can give better performance for w's height/width.
|
|
1179
|
+
for (var w_height: u32 = 0u; w_height < ${d[0]}; w_height++) {
|
|
1180
|
+
let x_height = x_corner.x + i32(w_height);
|
|
1181
|
+
if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) {
|
|
1182
|
+
for (var i = 0; i < ${g}; i++) {
|
|
1183
|
+
let x_width = x_corner.y + i;
|
|
1184
|
+
if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) {
|
|
1185
|
+
x_vals[i] = ${_.get("batch","u32(x_height)","u32(x_width)","input_channel")};
|
|
1186
|
+
} else {
|
|
1187
|
+
x_vals[i] = ${_.type.value}(0);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
for (var w_width: u32 = 0u; w_width < ${d[1]}; w_width++) {
|
|
1191
|
+
let w_val = ${I.get("w_height","w_width","0","output_channel")};
|
|
1192
|
+
for (var i = 0u; i < ${s}u; i++) {
|
|
1193
|
+
values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
for (var i = 0u; i < ${s}u; i++) {
|
|
1200
|
+
var value = values[i];
|
|
1201
|
+
${E}
|
|
1202
|
+
${$}
|
|
1203
|
+
${v.set("batch","row","col + i","output_channel","value")};
|
|
1204
|
+
}
|
|
1205
|
+
}`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${a};${s};${g};${d[0]};${d[1]}`,inputDependencies:n?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:h}),getShaderSource:w}}}),l0=P(()=>{"use strict";ie(),s0(),o0(),xa(),u0(),Wt(),$a(),$t(),ul=(e,t,r,i,n,a)=>{let s=e[0],u=e.slice(a?1:2,a?3:4),l=u.length,d=t[0],f=t.slice(2).map((g,w)=>g+(g-1)*(r[w]-1)),h=u.map((g,w)=>g+i[w]+i[w+l]).map((g,w)=>Math.floor((g-f[w]+n[w])/n[w]));return h.splice(0,0,s),h.splice(a?3:1,0,d),h},Zr=[2,3,1,0],ll=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[1]*t.group;if(r!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let n=e[0].dims.length-2;if(t.dilations.length!==n)throw new Error(`dilations should be ${n}D`);if(t.strides.length!==n)throw new Error(`strides should be ${n}D`);if(t.pads.length!==n*2)throw new Error(`pads should be ${n*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},Yr=(e,t)=>{let r=e.kernelShape.slice();r.length<t[1].dims.length-2&&r.push(...Array(t[1].dims.length-2-r.length).fill(0));for(let a=2;a<t[1].dims.length;++a)r[a-2]===0&&(r[a-2]=t[1].dims[a]);let i=e.pads.slice();si.adjustPadsBasedOnAutoPad(t[0].dims,e.strides,e.dilations,r,i,e.format==="NHWC",e.autoPad);let n=Object.assign({},e);return Object.assign(n,{kernelShape:r,pads:i}),n},Yn=e=>{let t=_a(e),r=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],n=e.dilations,a=e.group,s=e.kernel_shape,u=e.pads,l=e.strides,d=e.w_is_const();return{autoPad:i,format:r,dilations:n,group:a,kernelShape:s,pads:u,strides:l,wIsConst:d,...t,cacheKey:`${e.format};${t.activation};`}},mn=(e,t,r,i)=>{let n=r.format==="NHWC",a=ul(t[0].dims,t[1].dims,r.dilations,r.pads,r.strides,n);if(r.group!==1){let C=[t[0]];if(n){let O=e.kernelCustomData.wT??e.compute(De(t[1],Zr),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=O),C.push(O)}else C.push(t[1]);t.length===3&&C.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&n&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1?e.compute(vh(C,r,a,i),{inputs:C}):e.compute(bh(C,r,a,i),{inputs:C});return}let s=t.length===3,u=t[0].dims[n?1:2],l=t[0].dims[n?2:3],d=t[0].dims[n?3:1],f=t[1].dims[2],h=t[1].dims[3],g=a[n?1:2],w=a[n?2:3],y=a[n?3:1],v=n&&f===u&&h===l&&r.pads[0]===0&&r.pads[1]===0;if(v||f===1&&h===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let C=a[0],O,x,D,L=[];if(n){let j=e.kernelCustomData.wT??e.compute(De(t[1],Zr),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=j),v){let ue=u*l*d;O=t[0].reshape([1,C,ue]),x=j.reshape([1,ue,y]),D=[1,C,y]}else O=t[0].reshape([C,u*l,d]),x=j.reshape([1,d,y]),D=[C,g*w,y];L.push(O),L.push(x)}else O=t[0].reshape([C,d,u*l]),x=t[1].reshape([1,y,d]),D=[C,y,g*w],L.push(x),L.push(O);s&&L.push(t[2]);let Y=D[2],q=L[0].dims[L[0].dims.length-1];Y<8&&q<8?e.compute(va(L,r,a,D,n,i),{inputs:L}):e.compute(ui(L,r,a,D,n,i),{inputs:L});return}let T=!0,$=e.kernelCustomData.wT??e.compute(De(t[1],Zr),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=$);let _=[t[0],$];s&&_.push(t[2]);let I=n?g*w:y,k=n?y:g*w,E=f*h*d;e.compute(yh(_,r,a,I,k,E,s,T,i),{inputs:_})},dl=(e,t)=>{let r=t.format==="NHWC",i=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let n=[0,t.pads[0],0,t.pads[1]],a=[1].concat(t.strides),s=[1].concat(t.dilations),u=[1].concat(t.kernelShape),l=Yr({...t,pads:n,strides:a,dilations:s,kernelShape:u},i);mn(e,i,l,d=>r?[d[0],d[2],d[3]]:[d[0],d[1],d[3]])},pl=(e,t,r)=>{let i=r.format==="NHWC"?"channelsLast":"channelsFirst",n=Yr(r,t),a=r.autoPad==="NOTSET"?r.pads:r.autoPad,s=wh(t[0].dims,t[1].dims,r.strides,r.dilations,a,!1,i);e.compute(_h(t,n,s.outShape,[s.filterDepth,s.filterHeight,s.filterWidth],[s.padInfo.front,s.padInfo.top,s.padInfo.left],i))},Xn=(e,t)=>{if(ll(e.inputs,t),e.inputs[0].dims.length===3)dl(e,t);else if(e.inputs[0].dims.length===5)pl(e,e.inputs,t);else{let r=Yr(t,e.inputs);mn(e,e.inputs,r)}}}),d0=P(()=>{"use strict";ee(),lt(),ie(),ne(),$h=(e,t,r)=>{let i=e.length>2,n=t.outputShape,a=t.format==="NHWC",s=t.group,u=e[1].dims,l=u[2]/s,d=u[3],f=a?xe(l):1,h=a&&d===1&&l>=4,g=h?Math.floor(l/4)*4:Math.floor(l/f)*f,w=l-g,y=a?xe(d):1,v=a?d===1?f:y:1,T=R.size(n)/y,$=[Math.ceil(T/64),1,1];de("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${$}`);let _=["rank","rank"],I=[t.strides[0],t.strides[1]],k=[t.kernelShape[a?1:2],t.kernelShape[a?2:3]],E=[t.dilations[0],t.dilations[1]],C=[k[0]+(t.dilations[0]<=1?0:(t.kernelShape[a?1:2]-1)*(t.dilations[0]-1)),k[1]+(t.dilations[1]<=1?0:(t.kernelShape[a?2:3]-1)*(t.dilations[1]-1))],O=[C[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),C[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],x=[{type:12,data:T},{type:12,data:I},{type:12,data:k},{type:12,data:E},{type:12,data:C},{type:6,data:O},{type:12,data:g},{type:12,data:l},{type:12,data:d},...Z(e[0].dims,e[1].dims)];i&&(x.push(...Z(e[2].dims)),_.push("rank")),x.push(...Z(n));let D=L=>{let Y=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:I.length},{name:"filter_dims",type:"u32",length:k.length},{name:"dilations",type:"u32",length:k.length},{name:"effective_filter_dims",type:"u32",length:C.length},{name:"pads",type:"i32",length:O.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],q=Ee(e[0].dataType),j=a?1:2,ue=a?2:3,A=a?3:1,U=B("W",e[1].dataType,e[1].dims.length,v),re=B("Dy",e[0].dataType,e[0].dims.length,f),J=[re,U];i&&J.push(B("bias",e[2].dataType,[n[A]].length,y));let H=F("result",e[0].dataType,n.length,y),pe=()=>{let te="";if(h)f===4?te+=`
|
|
1206
|
+
let xValue = ${re.getByOffset("x_offset")};
|
|
1207
|
+
let wValue = ${U.getByOffset("w_offset")};
|
|
1208
|
+
dotProd = dotProd + dot(xValue, wValue);
|
|
1209
|
+
x_offset += 1u;
|
|
1210
|
+
w_offset += 1u;`:f===2?te+=`
|
|
1211
|
+
dotProd = dotProd + dot(vec4<${q}>(${re.getByOffset("x_offset")}, ${re.getByOffset("x_offset + 1u")}), vec4<${q}>(${U.getByOffset("w_offset")}, ${U.getByOffset("w_offset + 1u")}));
|
|
1212
|
+
x_offset += 2u;
|
|
1213
|
+
w_offset += 2u;`:f===1&&(te+=`
|
|
1214
|
+
dotProd = dotProd + dot(vec4<${q}>(${re.getByOffset("x_offset")}, ${re.getByOffset("x_offset + 1u")}, ${re.getByOffset("x_offset + 2u")}, ${re.getByOffset("x_offset + 3u")}), vec4<${q}>(${U.getByOffset("w_offset")}, ${U.getByOffset("w_offset + 1u")}, ${U.getByOffset("w_offset + 2u")}, ${U.getByOffset("w_offset + 3u")}));
|
|
1215
|
+
x_offset += 4u;
|
|
1216
|
+
w_offset += 4u;`);else if(te+=`
|
|
1217
|
+
let xValue = ${a?re.getByOffset(`${re.indicesToOffset(`${re.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${f}`):re.get("batch","inputChannel","idyR","idyC")};
|
|
1218
|
+
`,f===1)te+=`
|
|
1219
|
+
let w_offset = ${U.indicesToOffset(`${U.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)};
|
|
1220
|
+
let wValue = ${U.getByOffset(`w_offset / ${v}`)};
|
|
1221
|
+
dotProd = dotProd + xValue * wValue;`;else for(let ce=0;ce<f;ce++)te+=`
|
|
1222
|
+
let wValue${ce} = ${U.getByOffset(`${U.indicesToOffset(`${U.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel + ${ce}, wOutChannel)`)} / ${v}`)};
|
|
1223
|
+
dotProd = dotProd + xValue[${ce}] * wValue${ce};`;return te},N=()=>{if(w===0)return"";if(!h)throw new Error(`packInputAs4 ${h} is not true.`);let te="";if(f===1){te+="dotProd = dotProd";for(let ce=0;ce<w;ce++)te+=`
|
|
1224
|
+
+ ${re.getByOffset(`x_offset + ${ce}`)} * ${U.getByOffset(`w_offset + ${ce}`)}`;te+=";"}else if(f===2){if(w!==2)throw new Error(`Invalid inputChannelsRemainder ${w}.`);te+=`
|
|
1225
|
+
let xValue = ${re.getByOffset("x_offset")};
|
|
1226
|
+
let wValue = ${U.getByOffset("w_offset")};
|
|
1227
|
+
dotProd = dotProd + dot(xValue, wValue);`}return te},V=`
|
|
1228
|
+
let outputIndices = ${H.offsetToIndices(`global_idx * ${y}`)};
|
|
1229
|
+
let batch = ${H.indicesGet("outputIndices",0)};
|
|
1230
|
+
let d1 = ${H.indicesGet("outputIndices",A)};
|
|
1231
|
+
let r = ${H.indicesGet("outputIndices",j)};
|
|
1232
|
+
let c = ${H.indicesGet("outputIndices",ue)};
|
|
1233
|
+
let dyCorner = vec2<i32>(i32(r), i32(c)) - uniforms.pads;
|
|
1234
|
+
let dyRCorner = dyCorner.x;
|
|
1235
|
+
let dyCCorner = dyCorner.y;
|
|
1236
|
+
let groupId = d1 / uniforms.output_channels_per_group;
|
|
1237
|
+
let wOutChannel = d1 - groupId * uniforms.output_channels_per_group;
|
|
1238
|
+
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
|
|
1239
|
+
// ? = to be determined. : = across all values in that axis.
|
|
1240
|
+
var dotProd = ${H.type.value}(0.0);
|
|
1241
|
+
var wR: u32 = 0;
|
|
1242
|
+
if (uniforms.dilations.x == 1) {
|
|
1243
|
+
// Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0
|
|
1244
|
+
wR = u32(((dyRCorner + i32(uniforms.strides.x) - 1) / i32(uniforms.strides.x)) * i32(uniforms.strides.x) - dyRCorner);
|
|
1245
|
+
}
|
|
1246
|
+
for (; wR < uniforms.effective_filter_dims.x; wR = wR + 1) {
|
|
1247
|
+
if (wR % uniforms.dilations.x != 0) {
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
let dyR = (${q}(dyRCorner) + ${q}(wR)) / ${q}(uniforms.strides[0]);
|
|
1251
|
+
let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x;
|
|
1252
|
+
if (dyR < 0.0 || dyR >= ${q}(uniforms.Dy_shape[${j}]) || fract(dyR) > 0.0 ||
|
|
1253
|
+
wRPerm < 0) {
|
|
1254
|
+
continue;
|
|
1255
|
+
}
|
|
1256
|
+
let idyR: u32 = u32(dyR);
|
|
1257
|
+
var wC: u32 = 0;
|
|
1258
|
+
if (uniforms.dilations.y == 1) {
|
|
1259
|
+
// Minimum wC >= 0 that satisfies (dyCCorner + wC) % (uniforms.strides.y) == 0
|
|
1260
|
+
wC = u32(((dyCCorner + i32(uniforms.strides.y) - 1) / i32(uniforms.strides.y)) * i32(uniforms.strides.y) - dyCCorner);
|
|
1261
|
+
}
|
|
1262
|
+
for (; wC < uniforms.effective_filter_dims.y; wC = wC + 1) {
|
|
1263
|
+
if (wC % uniforms.dilations.y != 0) {
|
|
1264
|
+
continue;
|
|
1265
|
+
}
|
|
1266
|
+
let dyC = (${q}(dyCCorner) + ${q}(wC)) / ${q}(uniforms.strides.y);
|
|
1267
|
+
let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y;
|
|
1268
|
+
if (dyC < 0.0 || dyC >= ${q}(uniforms.Dy_shape[${ue}]) ||
|
|
1269
|
+
fract(dyC) > 0.0 || wCPerm < 0) {
|
|
1270
|
+
continue;
|
|
1271
|
+
}
|
|
1272
|
+
let idyC: u32 = u32(dyC);
|
|
1273
|
+
var inputChannel = groupId * uniforms.input_channels_per_group;
|
|
1274
|
+
${h?`
|
|
1275
|
+
var x_offset = ${re.indicesToOffset(`${re.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${f};
|
|
1276
|
+
var w_offset = ${U.indicesToOffset(`${U.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${v};
|
|
1277
|
+
`:""}
|
|
1278
|
+
for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${h?4:f}) {
|
|
1279
|
+
${pe()}
|
|
1280
|
+
inputChannel = inputChannel + ${h?4:f};
|
|
1281
|
+
}
|
|
1282
|
+
${N()}
|
|
1283
|
+
wC = wC + uniforms.strides.y - 1;
|
|
1284
|
+
}
|
|
1285
|
+
wR = wR + uniforms.strides[0] - 1;
|
|
1286
|
+
}
|
|
1287
|
+
let value = dotProd${i?` + bias[d1 / ${y}]`:""};
|
|
1288
|
+
${H.setByOffset("global_idx","value")};
|
|
1289
|
+
`;return`
|
|
1290
|
+
${L.registerUniforms(Y).declareVariables(...J,H)}
|
|
1291
|
+
${L.mainStart()}
|
|
1292
|
+
${L.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")};
|
|
1293
|
+
${V}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${f}${v}${y}${h}${w}`,inputDependencies:_},getRunData:()=>({dispatchGroup:{x:$[0],y:$[1],z:$[2]},outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],programUniforms:x}),getShaderSource:D}}}),p0=P(()=>{"use strict";d0(),Wt(),$t(),cl=(e,t,r,i,n,a)=>(e-1)*t+r+(i-1)*n+1-a,hl=(e,t,r,i,n)=>{let a=Math.floor(e/2);t==="SAME_UPPER"?(r[i]=a,r[n]=e-a):t==="SAME_LOWER"&&(r[i]=e-a,r[n]=a)},fl=(e,t,r,i,n,a,s,u,l,d)=>{let f=e.length-2,h=d.length===0;l.length<f&&l.push(...Array(f-l.length).fill(0));let g=e[0],w=t[u?3:1]*n;for(let y=0,v=e.length-f-(u?1:0);y<f;++y,++v){let T=e[v],$=h?T*s[y]:d[y],_=cl(T,s[y],a[y],t[v],r[y],$);hl(_,i,a,y,y+f),h&&d.push(s[y]*(T-1)+l[y]+(t[v]-1)*r[y]+1-a[y]-a[y+f])}d.splice(0,0,g),d.splice(u?3:1,0,w)},gn=(e,t)=>{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((h,g)=>h*g,1)===0){r.length=0;for(let h=2;h<t[1].dims.length;++h)r.push(t[1].dims[h])}let i=e.format==="NHWC";r.splice(0,0,t[1].dims[0]),r.splice(i?3:1,0,t[1].dims[1]);let n=e.pads.slice(),a=e.outputShape.slice(),s=e.outputPadding.slice(),u=t[0].dims,l=e.dilations.slice();if(l.reduce((h,g)=>h+g,0)===0){let h=t[0].dims.length-2;l=new Array(h).fill(1)}let d=e.strides.slice();if(d.reduce((h,g)=>h+g,0)===0){let h=t[0].dims.length-2;d=new Array(h).fill(1)}fl(u,r,l,e.autoPad,e.group,n,d,i,s,a);let f=Object.assign({},e);return Object.assign(f,{kernelShape:r,pads:n,outputPadding:s,outputShape:a,dilations:l,strides:d}),f},xh=e=>{let t=_a(e),r=e.format,i=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],n=e.dilations,a=e.group??1,s=e.kernelShape,u=e.pads,l=e.strides,d=e.wIsConst(),f=e.outputPadding,h=e.outputShape;return{autoPad:i,format:r,dilations:n,group:a,kernelShape:s,outputPadding:f,outputShape:h,pads:u,strides:l,wIsConst:d,...t,cacheKey:`${e.format};${t.activation};`}},ml=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],i=e[1].dims[0];if(r!==i)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let n=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==n))throw new Error("invalid bias");let a=e[0].dims.length-2;if(t.dilations.reduce((s,u)=>s+u,0)>0&&t.dilations.length!==a)throw new Error(`dilations should be ${a}D`);if(t.strides.reduce((s,u)=>s+u,0)>0&&t.strides.length!==a)throw new Error(`strides should be ${a}D`);if(t.pads.reduce((s,u)=>s+u,0)>0&&t.pads.length!==a*2)throw new Error(`pads should be ${a*2}D`);if(t.outputPadding.length!==a&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${a}D`);if(t.kernelShape.reduce((s,u)=>s+u,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},yn=(e,t,r,i)=>{let n=e.kernelCustomData.wT??e.compute(De(t[1],[2,3,0,1]),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=n);let a=[t[0],n];t.length===3&&a.push(t[2]),e.compute($h(a,r,i),{inputs:a})},gl=(e,t)=>{let r=t.format==="NHWC",i=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&i.push(e.inputs[2]);let n=t.kernelShape;(n.length===0||n[0]===0)&&(n=[e.inputs[1].dims[2]]);let a=t.dilations;(a.length===0||a[0]===0)&&(a=[1]);let s=t.strides;(s.length===0||s[0]===0)&&(s=[1]);let u=t.pads;u.length===0&&(u=[0,0]),u=[0,u[0],0,u[1]],s=[1].concat(s),a=[1].concat(a),n=[1].concat(n);let l=t.outputPadding;l=[0].concat(l);let d=gn({...t,pads:u,strides:s,dilations:a,kernelShape:n,outputPadding:l},i);yn(e,i,d,f=>r?[f[0],f[2],f[3]]:[f[0],f[1],f[3]])},Th=(e,t)=>{if(ml(e.inputs,t),e.inputs[0].dims.length===3)gl(e,t);else{let r=gn(t,e.inputs);yn(e,e.inputs,r)}}}),c0=P(()=>{"use strict";ee(),ie(),Te(),ne(),yl=(e,t,r,i)=>{let n=R.size(t),a=t.length,s=B("input",e,a),u=F("output",e,a),l=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),d=R.normalizeAxis(l,a),f=h=>{let g=` i32(${s.indicesGet("inputIndices","uniforms.axis")}) `,w=K("uniforms.input_shape","uniforms.axis",a),y=i.reverse?g+(i.exclusive?" + 1":""):"0",v=i.reverse?w:g+(i.exclusive?"":" + 1");return`
|
|
1294
|
+
${h.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(s,u)}
|
|
1295
|
+
${h.mainStart()}
|
|
1296
|
+
${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
1297
|
+
var inputIndices = ${u.offsetToIndices("global_idx")};
|
|
1298
|
+
var sum = ${u.type.value}(0);
|
|
1299
|
+
let first : i32 = ${y};
|
|
1300
|
+
let last : i32 = ${v};
|
|
1301
|
+
for (var i : i32 = first; i < last; i++) {
|
|
1302
|
+
${s.indicesSet("inputIndices","uniforms.axis","u32(i)")};
|
|
1303
|
+
sum = sum + ${s.getByIndices("inputIndices")};
|
|
1304
|
+
}
|
|
1305
|
+
${u.setByOffset("global_idx","sum")};
|
|
1306
|
+
}`};return{name:"CumSum",shaderCache:{hint:i.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:[{type:12,data:n},{type:12,data:d},...Z(t,t)]}),getShaderSource:f}},Sh=(e,t)=>{let r=e.inputs[0].dims,i=e.inputs[0].dataType,n=e.inputs[1];e.compute(yl(i,r,n,t),{inputs:[0]})},kh=e=>{let t=e.exclusive===1,r=e.reverse===1;return me({exclusive:t,reverse:r})}}),h0=P(()=>{"use strict";ee(),ie(),Te(),ne(),wl=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},_l=(e,t,r,i)=>{let n=[];n.push(`fn perm(i: ${i.type.indices}) -> ${r.type.indices} {
|
|
1307
|
+
var a: ${r.type.indices};`);for(let a=0;a<t;++a)n.push(r.indicesSet("a",e[a],`i[${a}]`));return n.push("return a;}"),n.join(`
|
|
1308
|
+
`)},bl=(e,t)=>{let r,i,n,a,s,u,l=t.format==="NHWC",d=t.blocksize,f=t.mode==="DCR";l?([r,i,n,a]=e.dims,s=f?[r,i,n,d,d,a/d**2]:[r,i,n,a/d**2,d,d],u=f?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,i,n,a]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],s=f?[r,d,d,a/d**2,i,n]:[r,a/d**2,d,d,i,n],u=f?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let h=e.reshape(s),g=h.dims.length,w=e.dataType,y=B("a",w,g),v=F("output",w,g),T=$=>`
|
|
1309
|
+
${$.registerUniform("output_size","u32").declareVariables(y,v)}
|
|
1310
|
+
|
|
1311
|
+
${_l(u,g,y,v)}
|
|
1312
|
+
|
|
1313
|
+
${$.mainStart()}
|
|
1314
|
+
${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1315
|
+
|
|
1316
|
+
let indices = ${v.offsetToIndices("global_idx")};
|
|
1317
|
+
let aIndices = perm(indices);
|
|
1318
|
+
|
|
1319
|
+
${v.setByOffset("global_idx",y.getByIndices("aIndices"))}
|
|
1320
|
+
}`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:$=>{let _=l?[r,i*d,n*d,a/d**2]:[r,a/d**2,i*d,n*d],I=R.size(_),k=h.dims,E=R.sortBasedOnPerm(k,u);return{outputs:[{dims:_,dataType:$[0].dataType}],dispatchGroup:{x:Math.ceil(I/64)},programUniforms:[{type:12,data:I},...Z(k,E)]}},getShaderSource:T}},Ih=(e,t)=>{wl(e.inputs),e.compute(bl(e.inputs[0],t))},Eh=e=>me({blocksize:e.blocksize,mode:e.mode,format:e.format})}),f0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Xr="[a-zA-Z]|\\.\\.\\.",cr="("+Xr+")+",wn="^"+cr+"$",vl="("+cr+",)*"+cr,$l="^"+vl+"$",xl=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let r=this.symbolToIndices.get(e);r===void 0?r=[t]:r.push(t),this.symbolToIndices.set(e,r)}},Tl=class{constructor(e,t){this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[r,i]=t.includes("->")?t.split("->",2):[t,""];if(!r.match(RegExp($l)))throw new Error("Invalid LHS term");if(r.split(",").forEach((n,a)=>{let s=e[a].dims.slice();if(!n.match(RegExp(wn)))throw new Error("Invalid LHS term");let u=this.processTerm(n,!0,s,a);this.lhs.push(u)}),i==="")i+=[...this.symbolToInfo.entries()].filter(([n,a])=>a.count===1||n==="...").map(([n])=>n).join("");else if(!i.match(RegExp(cr)))throw new Error("Invalid RHS");i.match(RegExp(Xr,"g"))?.forEach(n=>{if(n==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let a=this.symbolToInfo.get(n);if(a===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(a.dimValue)}}),this.rhs=this.processTerm(i,!1,this.outputDims)}addSymbol(e,t,r){let i=this.symbolToInfo.get(e);if(i!==void 0){if(i.dimValue!==t&&i.count!==1)throw new Error("Dimension mismatch");i.count++,i.inputIndices.push(r)}else i={count:1,dimValue:t,inputIndices:[r]};this.symbolToInfo.set(e,i)}processTerm(e,t,r,i=-1){let n=r.length,a=!1,s=[],u=0;if(!e.match(RegExp(wn))&&!t&&e!=="")throw new Error("Invalid LHS term");let l=e.match(RegExp(Xr,"g")),d=new xl(i);return l?.forEach((f,h)=>{if(f==="..."){if(a)throw new Error("Only one ellipsis is allowed per input term");a=!0;let g=n-l.length+1;if(g<0)throw new Error("Ellipsis out of bounds");if(s=r.slice(u,u+g),this.hasEllipsis){if(this.ellipsisDims.length!==s.length||this.ellipsisDims.toString()!==s.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=s;else throw new Error("Ellipsis must be specified in the LHS");for(let w=0;w<s.length;w++){let y=String.fromCharCode(48+w);d.addSymbol(y,h+w),this.addSymbol(y,r[u++],i)}}else d.addSymbol(f,h+(this.hasEllipsis?this.ellipsisDims.length-1:0)),this.addSymbol(f,r[u++],i)}),d}},_n=e=>e+"_max",Sl=(e,t,r,i)=>{let n=e.map(d=>d.length).map((d,f)=>B(`input${f}`,t,d)),a=R.size(i),s=F("output",t,i.length),u=[...r.symbolToInfo.keys()].filter(d=>!r.rhs.symbolToIndices.has(d)),l=d=>{let f=[],h="var prod = 1.0;",g="var sum = 0.0;",w="sum += prod;",y=[],v=[],T=[],$=[],_=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((k,E)=>{if(r.rhs.symbolToIndices.has(E)){let C=r.rhs.symbolToIndices.get(E)?.[0];C!==void 0&&r.lhs.forEach((O,x)=>{if(k.inputIndices.includes(x)){let D=O.symbolToIndices.get(E);if(D===void 0)throw new Error("Invalid symbol error");D.forEach(L=>{f.push(`${n[x].indicesSet(`input${x}Indices`,L,s.indicesGet("outputIndices",C))}`)})}})}else r.lhs.forEach((C,O)=>{if(k.inputIndices.includes(O)){let x=C.symbolToIndices.get(E);if(x===void 0)throw new Error("Invalid symbol error");x.forEach(D=>{y.push(`${n[O].indicesSet(`input${O}Indices`,D,`${E}`)}`)}),$.push(`prod *= ${n[O].getByIndices(`input${O}Indices`)};`)}}),v.push(`for(var ${E}: u32 = 0; ${E} < uniforms.${_n(E)}; ${E}++) {`),T.push("}")});let I=_?[...f,`let sum = ${n.map((k,E)=>k.getByIndices(`input${E}Indices`)).join(" * ")};`]:[...f,g,...v,...y,h,...$,w,...T];return`
|
|
1321
|
+
${d.registerUniforms(u.map(k=>({name:`${_n(k)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...n,s)}
|
|
1322
|
+
|
|
1323
|
+
${d.mainStart()}
|
|
1324
|
+
${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
1325
|
+
var outputIndices = ${s.offsetToIndices("global_idx")};
|
|
1326
|
+
${n.map((k,E)=>`var input${E}Indices: ${n[E].type.indices};`).join(`
|
|
1327
|
+
`)}
|
|
1328
|
+
${I.join(`
|
|
1329
|
+
`)};
|
|
1330
|
+
${s.setByOffset("global_idx","sum")};
|
|
1331
|
+
}`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let d=u.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));d.push({type:12,data:a});let f=e.map((h,g)=>[...Z(h)]).reduce((h,g)=>h.concat(g),d);return f.push(...Z(i)),{outputs:[{dims:i,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:f}},getShaderSource:l}},Ch=(e,t)=>{let r=new Tl(e.inputs,t.equation),i=r.outputDims,n=e.inputs.map((a,s)=>a.dims);e.compute(Sl(n,e.inputs[0].dataType,r,i))},zh=e=>{let t=e.equation.replace(/\s+/g,"");return me({equation:t})}}),m0=P(()=>{"use strict";ee(),ie(),ne(),kl=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),i=r.length<t.length?0:r.length-t.length,n=t.length<r.length?0:t.length-r.length;for(;i<r.length&&n<t.length;++i,++n)if(r[i]!==t[n]&&r[i]!==1&&t[n]!==1)throw new Error("Expand requires shape to be broadcastable to input")},bn=(e,t)=>{let r=e.length-t.length,i=[];for(let n=0;n<r;++n)i.push(e[n]);for(let n=0;n<t.length;++n)i.push(t[n]===1?e[n+r]:t[n]);return i},Il=(e,t)=>e.length>t.length?bn(e,t):bn(t,e),El=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),i=Il(t,r),n=e[0].dataType,a=n===9||R.size(t)===1,s=n===9||t.length>0&&t[t.length-1]%4===0?4:1,u=a||i.length>0&&i[i.length-1]%4===0?4:1,l=Math.ceil(R.size(i)/u),d=h=>{let g=B("input",n,t.length,s),w=F("output",n,i.length,u),y;if(n===9){let v=(T,$,_="")=>`
|
|
1332
|
+
let outputIndices${$} = ${w.offsetToIndices(`outputOffset + ${$}u`)};
|
|
1333
|
+
let offset${$} = ${g.broadcastedIndicesToOffset(`outputIndices${$}`,w)};
|
|
1334
|
+
let index${$} = offset${$} / 4u;
|
|
1335
|
+
let component${$} = offset${$} % 4u;
|
|
1336
|
+
${T}[${$}] = ${_}(${g.getByOffset(`index${$}`)}[component${$}]);
|
|
1337
|
+
`;y=`
|
|
1338
|
+
let outputOffset = global_idx * ${u};
|
|
1339
|
+
var data = vec4<u32>(0);
|
|
1340
|
+
${v("data",0,"u32")}
|
|
1341
|
+
${v("data",1,"u32")}
|
|
1342
|
+
${v("data",2,"u32")}
|
|
1343
|
+
${v("data",3,"u32")}
|
|
1344
|
+
${w.setByOffset("global_idx","data")}
|
|
1345
|
+
}`}else y=`
|
|
1346
|
+
let outputIndices = ${w.offsetToIndices(`global_idx * ${u}`)};
|
|
1347
|
+
let inputOffset = ${g.broadcastedIndicesToOffset("outputIndices",w)};
|
|
1348
|
+
let data = ${w.type.value}(${g.getByOffset(`inputOffset / ${s}`)});
|
|
1349
|
+
${w.setByOffset("global_idx","data")}
|
|
1350
|
+
}`;return`
|
|
1351
|
+
${h.registerUniform("vec_size","u32").declareVariables(g,w)}
|
|
1352
|
+
${h.mainStart()}
|
|
1353
|
+
${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
1354
|
+
${y}`},f=[{type:12,data:l},...Z(t,i)];return{name:"Expand",shaderCache:{hint:`${i.length};${s}${u}`,inputDependencies:["rank"]},getShaderSource:d,getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:f})}},Ah=e=>{kl(e.inputs),e.compute(El(e.inputs),{inputs:[0]})}}),g0=P(()=>{"use strict";ee(),ie(),ne(),wa(),Cl=e=>{let t=e[0].dataType,r=R.size(e[0].dims),i=R.size(e[1].dims),n=i%4===0,a=s=>{let u=B("x",t,[1],4),l=B("bias",t,[1],4),d=F("y",t,[1],4),f=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],h=w=>`
|
|
1355
|
+
let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size;
|
|
1356
|
+
let bias${w} = ${l.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`,g=n?`
|
|
1357
|
+
let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${h(0)}${h(1)}${h(2)}${h(3)}
|
|
1358
|
+
let bias = ${u.type.value}(bias0, bias1, bias2, bias3);`;return`${s.registerUniforms(f).declareVariables(u,l,d)}
|
|
1359
|
+
|
|
1360
|
+
${Kn(Ae(t))}
|
|
1361
|
+
|
|
1362
|
+
${s.mainStart(Zt)}
|
|
1363
|
+
${s.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")}
|
|
1364
|
+
|
|
1365
|
+
let x = ${u.getByOffset("global_idx")};
|
|
1366
|
+
${g}
|
|
1367
|
+
let x_in = x + bias;
|
|
1368
|
+
${d.setByOffset("global_idx",jn("x_in"))}
|
|
1369
|
+
}`};return{name:"FastGeluWithBias",shaderCache:{hint:`${n}`,inputDependencies:["type","type"]},getShaderSource:a,getRunData:s=>({outputs:[{dims:s[0].dims,dataType:s[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:i}],dispatchGroup:{x:Math.ceil(r/Zt/4)}})}},Oh=e=>{e.inputs.length<2||R.size(e.inputs[1].dims)===0?Xc(e):e.compute(Cl(e.inputs))}}),y0=P(()=>{"use strict";ee(),ie(),Te(),ne(),zl=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Al=(e,t)=>{let r=e[0].dims,i=e[1].dims,n=r.length,a=R.normalizeAxis(t.axis,n),s=r.slice(0);s.splice(a,1,...i);let u=r[a],l=e[0].dataType===9?4:1,d=Math.ceil(R.size(s)/l),f=[{type:12,data:d},{type:6,data:u},{type:12,data:a},...Z(e[0].dims,e[1].dims,s)],h=g=>{let w=B("data",e[0].dataType,e[0].dims.length,l),y=B("inputIndices",e[1].dataType,e[1].dims.length),v=F("output",e[0].dataType,s.length,l),T=_=>{let I=i.length,k=`var indicesIndices${_} = ${y.type.indices}(0);`;for(let E=0;E<I;E++)k+=`${I>1?`indicesIndices${_}[${E}]`:`indicesIndices${_}`} = ${s.length>1?`outputIndices${_}[uniforms.axis + ${E}]`:`outputIndices${_}`};`;k+=`
|
|
1370
|
+
var idx${_} = ${y.getByIndices(`indicesIndices${_}`)};
|
|
1371
|
+
if (idx${_} < 0) {
|
|
1372
|
+
idx${_} = idx${_} + uniforms.axisDimLimit;
|
|
1373
|
+
}
|
|
1374
|
+
var dataIndices${_} : ${w.type.indices};
|
|
1375
|
+
`;for(let E=0,C=0;E<n;E++)E===a?(k+=`${n>1?`dataIndices${_}[${E}]`:`dataIndices${_}`} = u32(idx${_});`,C+=I):(k+=`${n>1?`dataIndices${_}[${E}]`:`dataIndices${_}`} = ${s.length>1?`outputIndices${_}[${C}]`:`outputIndices${_}`};`,C++);return k},$;if(e[0].dataType===9){let _=(I,k,E="")=>`
|
|
1376
|
+
let outputIndices${k} = ${v.offsetToIndices(`outputOffset + ${k}u`)};
|
|
1377
|
+
${T(k)};
|
|
1378
|
+
let offset${k} = ${w.indicesToOffset(`dataIndices${k}`)};
|
|
1379
|
+
let index${k} = offset${k} / 4u;
|
|
1380
|
+
let component${k} = offset${k} % 4u;
|
|
1381
|
+
${I}[${k}] = ${E}(${w.getByOffset(`index${k}`)}[component${k}]);
|
|
1382
|
+
`;$=`
|
|
1383
|
+
let outputOffset = global_idx * ${l};
|
|
1384
|
+
var value = vec4<u32>(0);
|
|
1385
|
+
${_("value",0,"u32")}
|
|
1386
|
+
${_("value",1,"u32")}
|
|
1387
|
+
${_("value",2,"u32")}
|
|
1388
|
+
${_("value",3,"u32")}
|
|
1389
|
+
${v.setByOffset("global_idx","value")}
|
|
1390
|
+
`}else $=`
|
|
1391
|
+
let outputIndices = ${v.offsetToIndices("global_idx")};
|
|
1392
|
+
${T("")};
|
|
1393
|
+
let value = ${w.getByIndices("dataIndices")};
|
|
1394
|
+
${v.setByOffset("global_idx","value")};
|
|
1395
|
+
`;return`
|
|
1396
|
+
${g.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(w,y,v)}
|
|
1397
|
+
${g.mainStart()}
|
|
1398
|
+
${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
1399
|
+
${$}
|
|
1400
|
+
}`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:s,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:f}),getShaderSource:h}},Rh=e=>me({axis:e.axis}),Mh=(e,t)=>{let r=e.inputs;zl(r),e.compute(Al(e.inputs,t))}}),w0=P(()=>{"use strict";ee(),ie(),ne(),Ol=(e,t,r,i,n,a,s,u,l)=>{let d=[{type:12,data:a},{type:12,data:i},{type:12,data:n},{type:12,data:r},{type:12,data:s},{type:12,data:u},{type:12,data:l}],f=[a];d.push(...Z(t.dims,f));let h=g=>{let w=B("indices_data",t.dataType,t.dims.length),y=F("input_slice_offsets_data",12,1,1),v=[w,y],T=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:n.length},{name:"sizes_from_slice_dims_data",type:"u32",length:r.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return`
|
|
1401
|
+
${g.registerUniforms(T).declareVariables(...v)}
|
|
1402
|
+
${g.mainStart()}
|
|
1403
|
+
${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1404
|
+
let batch_idx = global_idx / uniforms.num_slices_per_batch;
|
|
1405
|
+
let base_offset = batch_idx * uniforms.input_batch_stride;
|
|
1406
|
+
|
|
1407
|
+
let slice_indices_base_offset = global_idx * uniforms.num_slice_dims;
|
|
1408
|
+
var relative_slice_offset = 0;
|
|
1409
|
+
for (var dim_idx = 0u; dim_idx < uniforms.num_slice_dims; dim_idx ++) {
|
|
1410
|
+
var index = i32(indices_data[dim_idx + slice_indices_base_offset].x);
|
|
1411
|
+
let input_dim_idx = uniforms.batch_dims + dim_idx;
|
|
1412
|
+
if (index < 0) {
|
|
1413
|
+
${n.length===1?"index += i32(uniforms.input_dims);":"index += i32(uniforms.input_dims[input_dim_idx]);"}
|
|
1414
|
+
}
|
|
1415
|
+
${r.length===1?"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data);":"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data[dim_idx]);"}
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
input_slice_offsets_data[global_idx] = base_offset + u32(relative_slice_offset);
|
|
1419
|
+
}`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${n.length}_${r.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:f,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d}),getShaderSource:h},{inputs:[t],outputs:[-1]})[0]},Bh=(e,t)=>{let r=e.inputs,i=r[0].dims,n=r[0].dataType,a=r[1].dims,s=a[a.length-1],u=R.sizeToDimension(a,a.length-1),l=R.sizeFromDimension(i,t.batchDims+s),d=R.sizeToDimension(i,t.batchDims),f=R.sizeFromDimension(i,t.batchDims),h=u/d,g=new Array(s),w=l;for(let k=0;k<s;++k)g[s-1-k]=w,w*=i[t.batchDims+s-1-k];let y=Ol(e,r[1],g,t.batchDims,i,u,h,f,s),v=t.batchDims+s;if(v>i.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let T=a.slice(0,-1).concat(i.slice(v)),$=R.size(T),_=[{type:12,data:$},{type:12,data:l},...Z(r[0].dims,y.dims,T)],I=k=>{let E=B("data",r[0].dataType,r[0].dims.length),C=B("slice_offsets",12,y.dims.length),O=F("output",r[0].dataType,T.length);return`
|
|
1420
|
+
${k.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(E,C,O)}
|
|
1421
|
+
${k.mainStart()}
|
|
1422
|
+
${k.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1423
|
+
let slice_offset = slice_offsets[global_idx / uniforms.slice_size];
|
|
1424
|
+
output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size];
|
|
1425
|
+
}`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:T,dataType:n}],dispatchGroup:{x:Math.ceil($/64)},programUniforms:_}),getShaderSource:I},{inputs:[r[0],y]})},Nh=e=>({batchDims:e.batch_dims,cacheKey:""})}),_0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Rl=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let r=R.normalizeAxis(t.quantizeAxis,e[0].dims.length),i=t.blockSize,n=e[0],a=e[2],s=e.length===4?e[3]:void 0;if(a.dims.length!==n.dims.length||!n.dims.map((u,l)=>l===r?Math.ceil(u/i)===a.dims[l]:u===a.dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(s){if(s.dataType!==n.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(s.dims.length!==a.dims.length||!s.dims.map((u,l)=>u===a.dims[l]).reduce((u,l)=>u&&l,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},Ml=(e,t)=>{let r=e[0].dims,i=e[1].dims,n=r.length,a=R.normalizeAxis(t.gatherAxis,n),s=R.normalizeAxis(t.quantizeAxis,n),u=r.slice(0);u.splice(a,1,...i);let l=R.size(u),d=e[2].dataType,f=e[0].dataType===22,h=[{type:12,data:l},{type:12,data:s},{type:12,data:a},{type:12,data:t.blockSize},...Z(...e.map((w,y)=>w.dims),u)],g=w=>{let y=B("data",e[0].dataType,e[0].dims.length),v=B("inputIndices",e[1].dataType,e[1].dims.length),T=B("scales",e[2].dataType,e[2].dims.length),$=e.length>3?B("zeroPoint",e[3].dataType,e[3].dims.length):void 0,_=F("output",d,u.length),I=[y,v,T];$&&I.push($);let k=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return`
|
|
1426
|
+
${w.registerUniforms(k).declareVariables(...I,_)}
|
|
1427
|
+
${w.mainStart()}
|
|
1428
|
+
let output_indices = ${_.offsetToIndices("global_idx")};
|
|
1429
|
+
var indices_indices = ${v.type.indices}(0);
|
|
1430
|
+
${i.length>1?`
|
|
1431
|
+
for (var i: u32 = 0; i < ${i.length}; i++) {
|
|
1432
|
+
let index = ${_.indicesGet("output_indices","uniforms.gather_axis + i")};
|
|
1433
|
+
${v.indicesSet("indices_indices","i","index")};
|
|
1434
|
+
}`:`indices_indices = ${_.indicesGet("output_indices","uniforms.gather_axis")};`};
|
|
1435
|
+
var data_indices = ${y.type.indices}(0);
|
|
1436
|
+
for (var i: u32 = 0; i < uniforms.gather_axis; i++) {
|
|
1437
|
+
let index = ${_.indicesGet("output_indices","i")};
|
|
1438
|
+
${y.indicesSet("data_indices","i","index")};
|
|
1439
|
+
}
|
|
1440
|
+
var index_from_indices = ${v.getByIndices("indices_indices")};
|
|
1441
|
+
if (index_from_indices < 0) {
|
|
1442
|
+
index_from_indices += ${r[a]};
|
|
1443
|
+
}
|
|
1444
|
+
${y.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")};
|
|
1445
|
+
for (var i = uniforms.gather_axis + 1; i < ${u.length}; i++) {
|
|
1446
|
+
let index = ${_.indicesGet("output_indices",`i + ${i.length} - 1`)};
|
|
1447
|
+
${y.indicesSet("data_indices","i","index")};
|
|
1448
|
+
}
|
|
1449
|
+
let data_offset = ${y.indicesToOffset("data_indices")};
|
|
1450
|
+
let data_index = data_offset % 8;
|
|
1451
|
+
// Convert 4-bit packed data to 8-bit packed data.
|
|
1452
|
+
let packed_4bit_quantized_data = ${y.getByOffset("data_offset / 8")};
|
|
1453
|
+
let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f;
|
|
1454
|
+
let quantized_data_vec = ${f?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data));
|
|
1455
|
+
let quantized_data = quantized_data_vec[data_index / 2];
|
|
1456
|
+
var scale_indices = data_indices;
|
|
1457
|
+
let quantize_axis_index = ${T.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size;
|
|
1458
|
+
${T.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")};
|
|
1459
|
+
var scale = ${T.getByIndices("scale_indices")};
|
|
1460
|
+
${$?`
|
|
1461
|
+
let zero_point_indices = scale_indices;
|
|
1462
|
+
let zero_point_offset = ${$.indicesToOffset("zero_point_indices")};
|
|
1463
|
+
let zero_point_index = zero_point_offset % 8;
|
|
1464
|
+
let packed_4bit_zero_points = ${$.getByOffset("zero_point_offset / 8")};
|
|
1465
|
+
let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f;
|
|
1466
|
+
let zero_point_vec = ${f?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points));
|
|
1467
|
+
let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"};
|
|
1468
|
+
let dequantized_data = ${Ae(d)}(quantized_data - zero_point) * scale;
|
|
1469
|
+
${_.setByOffset("global_idx","dequantized_data")};
|
|
1470
|
+
}`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((w,y)=>y!==1).map(w=>w.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(w,y)=>"rank")},getRunData:()=>({outputs:[{dims:u,dataType:d}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:h}),getShaderSource:g}},Dh=(e,t)=>{let r=e.inputs;Rl(r,t),e.compute(Ml(e.inputs,t))},Ph=e=>me({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),b0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Bl=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and
|
|
1471
|
+
indices input tensors be of same rank.`)},Nl=(e,t)=>{let r=e[0].dims,i=e[0].dataType,n=r.length,a=e[1].dims,s=e[1].dataType,u=R.normalizeAxis(t.axis,n),l=r[u],d=a.slice(0),f=R.size(d),h=B("input",i,n),g=B("indicesInput",s,a.length),w=F("output",i,d.length),y=[{type:12,data:f},{type:6,data:l},{type:12,data:u}];return y.push(...Z(r,a,d)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:y}),getShaderSource:v=>`
|
|
1472
|
+
${v.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(h,g,w)}
|
|
1473
|
+
${v.mainStart()}
|
|
1474
|
+
${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
1475
|
+
|
|
1476
|
+
let outputIndices = ${w.offsetToIndices("global_idx")};
|
|
1477
|
+
|
|
1478
|
+
var idx = ${g.getByOffset("global_idx")};
|
|
1479
|
+
if (idx < 0) {
|
|
1480
|
+
idx = idx + uniforms.axisDimLimit;
|
|
1481
|
+
}
|
|
1482
|
+
var inputIndices = ${h.type.indices}(outputIndices);
|
|
1483
|
+
${h.indicesSet("inputIndices","uniforms.axis","u32(idx)")};
|
|
1484
|
+
let value = ${h.getByIndices("inputIndices")};
|
|
1485
|
+
|
|
1486
|
+
${w.setByOffset("global_idx","value")};
|
|
1487
|
+
}`}},Uh=e=>me({axis:e.axis}),Wh=(e,t)=>{let r=e.inputs;Bl(r),e.compute(Nl(e.inputs,t))}}),v0=P(()=>{"use strict";ee(),ie(),ne(),Dl=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},Pl=(e,t)=>{let r=e[0].dims.slice(),i=e[1].dims.slice(),[n,a,s]=Up.getShapeOfGemmResult(r,t.transA,i,t.transB,e.length===3?e[2].dims:void 0),u=[n,a];if(!u)throw new Error("Can't use gemm on the given tensors");let l=16,d=Math.ceil(a/l),f=Math.ceil(n/l),h=!0,g=R.size(u),w=[{type:12,data:h?d:g},{type:12,data:n},{type:12,data:a},{type:12,data:s},{type:1,data:t.alpha},{type:1,data:t.beta}],y=["type","type"];e.length===3&&(w.push(...Z(e[2].dims)),y.push("rank")),w.push(...Z(u));let v=$=>{let _="";t.transA&&t.transB?_="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?_="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?_="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(_="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let I=t.alpha===1?"":"value *= uniforms.alpha;",k=B("a",e[0].dataType,e[0].dims),E=B("b",e[1].dataType,e[1].dims),C=k.type.value,O=null,x=[k,E];e.length===3&&(O=B("c",e[2].dataType,e[2].dims.length),x.push(O));let D=F("output",e[0].dataType,u.length);x.push(D);let L=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return`
|
|
1488
|
+
${$.registerUniforms(L).declareVariables(...x)}
|
|
1489
|
+
|
|
1490
|
+
${$.mainStart()}
|
|
1491
|
+
${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1492
|
+
|
|
1493
|
+
let m = global_idx / uniforms.N;
|
|
1494
|
+
let n = global_idx % uniforms.N;
|
|
1495
|
+
|
|
1496
|
+
var value = ${C}(0);
|
|
1497
|
+
for (var k: u32 = 0u; k < uniforms.K; k++) {
|
|
1498
|
+
${_}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
${I}
|
|
1502
|
+
${O!=null?`let cOffset = ${O.broadcastedIndicesToOffset("vec2(m, n)",D)}; value += ${C}(uniforms.beta) * ${O.getByOffset("cOffset")};`:""}
|
|
1503
|
+
output[global_idx] = value;
|
|
1504
|
+
}`},T=$=>{let _=B("a",e[0].dataType,e[0].dims),I=B("b",e[1].dataType,e[1].dims),k=null,E=[_,I];e.length===3&&(k=B("c",e[2].dataType,e[2].dims.length),E.push(k));let C=F("output",e[0].dataType,u.length);E.push(C);let O=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],x="",D="";t.transA&&t.transB?(D=`
|
|
1505
|
+
var col = tile_row_start + local_id.x;
|
|
1506
|
+
var row = k_start + local_id.y;
|
|
1507
|
+
if (col < uniforms.M && row < uniforms.K) {
|
|
1508
|
+
tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col];
|
|
1509
|
+
} else {
|
|
1510
|
+
tile_a[local_id.y][local_id.x] = ${_.type.value}(0);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
col = k_start + local_id.x;
|
|
1514
|
+
row = tile_col_start + local_id.y;
|
|
1515
|
+
if (col < uniforms.K && row < uniforms.N) {
|
|
1516
|
+
tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col];
|
|
1517
|
+
} else {
|
|
1518
|
+
tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
|
|
1519
|
+
}
|
|
1520
|
+
`,x="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(D=`
|
|
1521
|
+
var col = tile_row_start + local_id.x;
|
|
1522
|
+
var row = k_start + local_id.y;
|
|
1523
|
+
if (col < uniforms.M && row < uniforms.K) {
|
|
1524
|
+
tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col];
|
|
1525
|
+
} else {
|
|
1526
|
+
tile_a[local_id.y][local_id.x] = ${_.type.value}(0);
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
col = tile_col_start + local_id.x;
|
|
1530
|
+
row = k_start + local_id.y;
|
|
1531
|
+
if (col < uniforms.N && row < uniforms.K) {
|
|
1532
|
+
tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col];
|
|
1533
|
+
} else {
|
|
1534
|
+
tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
|
|
1535
|
+
}
|
|
1536
|
+
`,x="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(D=`
|
|
1537
|
+
var col = k_start + local_id.x;
|
|
1538
|
+
var row = tile_row_start + local_id.y;
|
|
1539
|
+
if (col < uniforms.K && row < uniforms.M) {
|
|
1540
|
+
tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col];
|
|
1541
|
+
} else {
|
|
1542
|
+
tile_a[local_id.y][local_id.x] = ${_.type.value}(0);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
col = k_start + local_id.x;
|
|
1546
|
+
row = tile_col_start + local_id.y;
|
|
1547
|
+
if (col < uniforms.K && row < uniforms.N) {
|
|
1548
|
+
tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col];
|
|
1549
|
+
} else {
|
|
1550
|
+
tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
|
|
1551
|
+
}
|
|
1552
|
+
`,x="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(D=`
|
|
1553
|
+
var col = k_start + local_id.x;
|
|
1554
|
+
var row = tile_row_start + local_id.y;
|
|
1555
|
+
if (col < uniforms.K && row < uniforms.M) {
|
|
1556
|
+
tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col];
|
|
1557
|
+
} else {
|
|
1558
|
+
tile_a[local_id.y][local_id.x] = ${_.type.value}(0);
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
col = tile_col_start + local_id.x;
|
|
1562
|
+
row = k_start + local_id.y;
|
|
1563
|
+
if (col < uniforms.N && row < uniforms.K) {
|
|
1564
|
+
tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col];
|
|
1565
|
+
} else {
|
|
1566
|
+
tile_b[local_id.y][local_id.x] = ${I.type.value}(0);
|
|
1567
|
+
}
|
|
1568
|
+
`,x="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let L=t.alpha===1?"":"value *= uniforms.alpha;";return`
|
|
1569
|
+
${$.registerUniforms(O).declareVariables(...E)}
|
|
1570
|
+
var<workgroup> tile_a: array<array<${_.type.storage}, ${l}>, ${l}>;
|
|
1571
|
+
var<workgroup> tile_b: array<array<${I.type.storage}, ${l}>, ${l}>;
|
|
1572
|
+
${$.mainStart([l,l,1])}
|
|
1573
|
+
let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${l};
|
|
1574
|
+
let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${l};
|
|
1575
|
+
let num_tiles = (uniforms.K - 1) / ${l} + 1;
|
|
1576
|
+
var k_start = 0u;
|
|
1577
|
+
var value = ${C.type.value}(0);
|
|
1578
|
+
for (var t: u32 = 0u; t < num_tiles; t++) {
|
|
1579
|
+
${D}
|
|
1580
|
+
k_start = k_start + ${l};
|
|
1581
|
+
workgroupBarrier();
|
|
1582
|
+
|
|
1583
|
+
for (var k: u32 = 0u; k < ${l}; k++) {
|
|
1584
|
+
${x}
|
|
1585
|
+
}
|
|
1586
|
+
workgroupBarrier();
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
${L}
|
|
1590
|
+
let m = tile_row_start + local_id.y;
|
|
1591
|
+
let n = tile_col_start + local_id.x;
|
|
1592
|
+
${k!=null?`let cOffset = ${k.broadcastedIndicesToOffset("vec2(m, n)",C)}; value += ${C.type.value}(uniforms.beta) * ${k.getByOffset("cOffset")};`:""}
|
|
1593
|
+
if (m < uniforms.M && n < uniforms.N) {
|
|
1594
|
+
output[m * uniforms.N + n] = value;
|
|
1595
|
+
}
|
|
1596
|
+
}`};return h?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:d*f},programUniforms:w}),getShaderSource:T}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:w}),getShaderSource:v}},Lh=e=>{let t=e.transA,r=e.transB,i=e.alpha,n=e.beta;return{transA:t,transB:r,alpha:i,beta:n,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},qh=(e,t)=>{Dl(e.inputs),e.compute(Pl(e.inputs,t))}}),$0=P(()=>{"use strict";ee(),ie(),Te(),ne(),[it,ot,Et,Ct]=[0,1,2,3],Ul=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},Wl=`
|
|
1597
|
+
fn gs_get_cubic_coeffs(x: f32) -> vec4<f32> {
|
|
1598
|
+
let cubic_alpha = -0.75f;
|
|
1599
|
+
let x_abs = abs(x);
|
|
1600
|
+
var coeffs: vec4<f32>;
|
|
1601
|
+
coeffs[0] = (((cubic_alpha * (x_abs + 1) - 5 * cubic_alpha) * (x_abs + 1) + 8 * cubic_alpha) * (x_abs + 1) - 4 * cubic_alpha);
|
|
1602
|
+
coeffs[1] = (((cubic_alpha + 2) * x_abs - (cubic_alpha + 3)) * x_abs * x_abs + 1);
|
|
1603
|
+
coeffs[2] = (((cubic_alpha + 2) * (1 - x_abs) - (cubic_alpha + 3)) * (1 - x_abs) * (1 - x_abs) + 1);
|
|
1604
|
+
coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha);
|
|
1605
|
+
return coeffs;
|
|
1606
|
+
}
|
|
1607
|
+
`,Ll=e=>`
|
|
1608
|
+
fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} {
|
|
1609
|
+
var v: vec4<f32>;
|
|
1610
|
+
var coeffs = gs_get_cubic_coeffs(x);
|
|
1611
|
+
for (var i = 0; i < 4; i++) {
|
|
1612
|
+
v[i] = coeffs[0] * p[i][0] + coeffs[1] * p[i][1] + coeffs[2] * p[i][2] + coeffs[3] * p[i][3];
|
|
1613
|
+
}
|
|
1614
|
+
coeffs = gs_get_cubic_coeffs(y);
|
|
1615
|
+
let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]);
|
|
1616
|
+
return pixel;
|
|
1617
|
+
}
|
|
1618
|
+
`,ql=e=>`
|
|
1619
|
+
fn gs_denormalize(n: f32, length: i32) -> f32 {
|
|
1620
|
+
${e.alignCorners===0?`
|
|
1621
|
+
// alignCorners: false => [-1, 1] to [-0.5, length - 0.5]
|
|
1622
|
+
return ((n + 1.0) * f32(length) - 1.0) / 2.0;
|
|
1623
|
+
`:`
|
|
1624
|
+
// alignCorners: true => [-1, 1] to [0, length - 1]
|
|
1625
|
+
return (n + 1.0) / 2.0 * (f32(length - 1));
|
|
1626
|
+
`}
|
|
1627
|
+
}
|
|
1628
|
+
`,Vl=e=>`
|
|
1629
|
+
${e.paddingMode==="reflection"?`
|
|
1630
|
+
fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 {
|
|
1631
|
+
var dx = 0.0;
|
|
1632
|
+
var fx = f32(x);
|
|
1633
|
+
let range = x_max - x_min;
|
|
1634
|
+
if (fx < x_min) {
|
|
1635
|
+
dx = x_min - fx;
|
|
1636
|
+
let n = u32(dx / range);
|
|
1637
|
+
let r = dx - f32(n) * range;
|
|
1638
|
+
if (n % 2 == 0) {
|
|
1639
|
+
fx = x_min + r;
|
|
1640
|
+
} else {
|
|
1641
|
+
fx = x_max - r;
|
|
1642
|
+
}
|
|
1643
|
+
} else if (fx > x_max) {
|
|
1644
|
+
dx = fx - x_max;
|
|
1645
|
+
let n = u32(dx / range);
|
|
1646
|
+
let r = dx - f32(n) * range;
|
|
1647
|
+
if (n % 2 == 0) {
|
|
1648
|
+
fx = x_max - r;
|
|
1649
|
+
} else {
|
|
1650
|
+
fx = x_min + r;
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
return u32(fx);
|
|
1654
|
+
}`:""}
|
|
1655
|
+
`,Gl=(e,t,r)=>`
|
|
1656
|
+
fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4<f32>) -> ${t} {
|
|
1657
|
+
var pixel = ${t}(0);
|
|
1658
|
+
var indices = vec4<u32>(0);
|
|
1659
|
+
indices[${it}] = batch;
|
|
1660
|
+
indices[${ot}] = channel;`+(()=>{switch(r.paddingMode){case"zeros":return`
|
|
1661
|
+
if (r >= 0 && r < H && c >=0 && c < W) {
|
|
1662
|
+
indices[${Et}] = u32(r);
|
|
1663
|
+
indices[${Ct}] = u32(c);
|
|
1664
|
+
} else {
|
|
1665
|
+
return ${t}(0);
|
|
1666
|
+
}
|
|
1667
|
+
`;case"border":return`
|
|
1668
|
+
indices[${Et}] = u32(clamp(r, 0, H - 1));
|
|
1669
|
+
indices[${Ct}] = u32(clamp(c, 0, W - 1));
|
|
1670
|
+
`;case"reflection":return`
|
|
1671
|
+
indices[${Et}] = gs_reflect(r, border[1], border[3]);
|
|
1672
|
+
indices[${Ct}] = gs_reflect(c, border[0], border[2]);
|
|
1673
|
+
`;default:throw new Error(`padding mode ${r.paddingMode} is not supported`)}})()+`
|
|
1674
|
+
return ${e.getByIndices("indices")};
|
|
1675
|
+
}
|
|
1676
|
+
`,Fl=(e,t,r)=>(()=>{switch(r.mode){case"nearest":return`
|
|
1677
|
+
let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${it}], indices[${ot}], border);
|
|
1678
|
+
`;case"bilinear":return`
|
|
1679
|
+
let x1 = i32(floor(x));
|
|
1680
|
+
let y1 = i32(floor(y));
|
|
1681
|
+
let x2 = x1 + 1;
|
|
1682
|
+
let y2 = y1 + 1;
|
|
1683
|
+
|
|
1684
|
+
let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${it}], indices[${ot}], border);
|
|
1685
|
+
let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${it}], indices[${ot}], border);
|
|
1686
|
+
let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${it}], indices[${ot}], border);
|
|
1687
|
+
let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${it}], indices[${ot}], border);
|
|
1688
|
+
|
|
1689
|
+
let dx2 = ${t}(f32(x2) - x);
|
|
1690
|
+
let dx1 = ${t}(x - f32(x1));
|
|
1691
|
+
let dy2 = ${t}(f32(y2) - y);
|
|
1692
|
+
let dy1 = ${t}(y - f32(y1));
|
|
1693
|
+
let result = dy2 * (dx2 * p11 + dx1 * p12) + dy1 * (dx2 * p21 + dx1 * p22);
|
|
1694
|
+
`;case"bicubic":return`
|
|
1695
|
+
let x0 = i32(floor(x)) - 1;
|
|
1696
|
+
let y0 = i32(floor(y)) - 1;
|
|
1697
|
+
var p: mat4x4<${t}>;
|
|
1698
|
+
for (var h = 0; h < 4; h++) {
|
|
1699
|
+
for (var w = 0; w < 4; w++) {
|
|
1700
|
+
p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${it}], indices[${ot}], border);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
let dx = x - f32(x0 + 1);
|
|
1705
|
+
let dy = y - f32(y0 + 1);
|
|
1706
|
+
let result = gs_bicubic_interpolate(p, dx, dy);
|
|
1707
|
+
`;default:throw new Error(`mode ${r.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,Hl=(e,t)=>{let r=B("x",e[0].dataType,e[0].dims.length),i=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],n=B("grid",e[1].dataType,i.length,2),a=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(a=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[it,ot,Et,Ct]=[0,3,1,2]);let s=F("output",e[0].dataType,a.length),u=r.type.value,l=R.size(a),d=[{type:12,data:l},...Z(e[0].dims,i,a)],f=h=>`
|
|
1708
|
+
${h.registerUniform("output_size","u32").declareVariables(r,n,s)}
|
|
1709
|
+
${Wl}
|
|
1710
|
+
${Ll(u)}
|
|
1711
|
+
${ql(t)}
|
|
1712
|
+
${Vl(t)}
|
|
1713
|
+
${Gl(r,u,t)}
|
|
1714
|
+
|
|
1715
|
+
${h.mainStart()}
|
|
1716
|
+
${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1717
|
+
let H_in = i32(uniforms.x_shape[${Et}]);
|
|
1718
|
+
let W_in = i32(uniforms.x_shape[${Ct}]);
|
|
1719
|
+
|
|
1720
|
+
${t.alignCorners===0?`
|
|
1721
|
+
let x_min = -0.5;
|
|
1722
|
+
let x_max = f32(W_in) - 0.5;
|
|
1723
|
+
let y_min = -0.5;
|
|
1724
|
+
let y_max = f32(H_in) - 0.5;
|
|
1725
|
+
`:`
|
|
1726
|
+
let x_min = 0.0;
|
|
1727
|
+
let x_max = f32(W_in) - 1.0;
|
|
1728
|
+
let y_min = 0.0;
|
|
1729
|
+
let y_max = f32(H_in) - 1.0;
|
|
1730
|
+
`};
|
|
1731
|
+
let border = vec4<f32>(x_min, y_min, x_max, y_max);
|
|
1732
|
+
|
|
1733
|
+
let indices = ${s.offsetToIndices("global_idx")};
|
|
1734
|
+
var grid_indices = vec3<u32>(indices[${it}], indices[${Et}], indices[${Ct}]);
|
|
1735
|
+
let nxy = ${n.getByIndices("grid_indices")};
|
|
1736
|
+
var x = gs_denormalize(f32(nxy[0]), W_in);
|
|
1737
|
+
var y = gs_denormalize(f32(nxy[1]), H_in);
|
|
1738
|
+
|
|
1739
|
+
${Fl(s,u,t)}
|
|
1740
|
+
}`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:h=>{let g=R.size(a);return{outputs:[{dims:a,dataType:h[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:d}},getShaderSource:f}},Vh=(e,t)=>{Ul(e.inputs),e.compute(Hl(e.inputs,t))},Gh=e=>me({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),Kh=P(()=>{"use strict";ee(),ie(),Te(),fa(),ya(),ne(),$t(),Oe=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,Kl=(e,t)=>{let r=e[0],i=Oe(e,1),n=Oe(e,2),a=Oe(e,3),s=Oe(e,4),u=Oe(e,5),l=Oe(e,6),d=Oe(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let f=r.dims[0],h=r.dims[1],g=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],w=h,y=0,v=0,T=Math.floor(g/t.numHeads);if(l&&d&&R.size(l.dims)&&R.size(d.dims)){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(l.dims[0]!==f||l.dims[1]!==t.numHeads||l.dims[3]!==T)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[0]!==f||d.dims[1]!==t.numHeads||d.dims[3]!==T)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(l.dims[2]!==d.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');y=l.dims[2],v=l.dims[2]}else if(l&&R.size(l.dims)||d&&R.size(d.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(i&&R.size(i.dims)>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(i.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');$=2,w=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==T)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(n)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,w=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==T)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,w=i.dims[2]}}else{if(r.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(r.dims[2]!==t.numHeads||r.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}if(a&&R.size(a.dims)>0){if(a.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(i&&i.dims.length===5&&i.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let _=y+w,I=0;if(s&&R.size(s.dims)>0){I=8;let O=s.dims;throw O.length===1?O[0]===f?I=1:O[0]===3*f+2&&(I=3):O.length===2&&O[0]===f&&O[1]===_&&(I=5),I===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let k=!1,E=g;if(n&&R.size(n.dims)>0){if(n.dims.length!==3&&n.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(n.dims.length===3){if(w!==n.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');E=n.dims[2]}else{if(w!==n.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');E=n.dims[1]*n.dims[3],k=!0}}let C=!1;if(s&&R.size(s.dims)>0)throw new Error("Key padding mask is not supported");if(u&&R.size(u.dims)>0){if(u.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(u.dims[0]!==f||u.dims[1]!==t.numHeads||u.dims[2]!==h||u.dims[3]!==_)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:f,sequenceLength:h,pastSequenceLength:y,kvSequenceLength:w,totalSequenceLength:_,maxSequenceLength:v,inputHiddenSize:0,hiddenSize:g,vHiddenSize:E,headSize:T,vHeadSize:Math.floor(E/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:I,scale:t.scale,broadcastResPosBias:C,passPastInKv:k,qkvFormat:$}},Fh=e=>me({...e}),vn=me({perm:[0,2,1,3]}),jl=(e,t,r,i,n,a,s)=>{let u=[i,n,a],l=R.size(u),d=[{type:12,data:l},{type:12,data:s},{type:12,data:a}],f=h=>{let g=F("qkv_with_bias",t.dataType,u),w=B("qkv",t.dataType,u),y=B("bias",r.dataType,u),v=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return`
|
|
1741
|
+
${h.registerUniforms(v).declareVariables(w,y,g)}
|
|
1742
|
+
${h.mainStart()}
|
|
1743
|
+
${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1744
|
+
let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset;
|
|
1745
|
+
|
|
1746
|
+
qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx];
|
|
1747
|
+
}`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:u,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:d}),getShaderSource:f},{inputs:[t,r],outputs:[-1]})[0]},br=(e,t,r,i,n,a,s,u)=>{let l=a;if(s&&R.size(s.dims)>0){if(i===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return l=jl(e,a,s,t,i,r*n,u),l=l.reshape([t,i,r,n]),r===1||i===1?l:e.compute(De(l,vn.perm),{inputs:[l],outputs:[-1]})[0]}else return a.dims.length===3&&(l=a.reshape([t,i,r,n])),r===1||i===1?l:e.compute(De(l,vn.perm),{inputs:[l],outputs:[-1]})[0]},Hh=(e,t)=>{let r=Kl(e.inputs,t),i=e.inputs[0],n=Oe(e.inputs,1),a=Oe(e.inputs,2),s=Oe(e.inputs,3),u=Oe(e.inputs,4),l=Oe(e.inputs,5),d=Oe(e.inputs,6),f=Oe(e.inputs,7);if(i.dims.length===5)throw new Error("Packed QKV is not implemented");if(n?.dims.length===5)throw new Error("Packed KV is not implemented");let h=n&&a&&n.dims.length===4&&a.dims.length===4,g=br(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,i,s,0);if(h)return Tr(e,g,n,a,u,void 0,d,f,l,r);if(!n||!a)throw new Error("key and value must be provided");let w=br(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,n,s,r.hiddenSize),y=br(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,a,s,2*r.hiddenSize);Tr(e,g,w,y,u,void 0,d,f,l,r)}}),Zh=P(()=>{"use strict";ee(),ie(),Te(),ne(),Ql=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Zl=(e,t)=>{let r=[],i=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),i=r.length),me({numOutputs:i,axis:t.axis,splitSizes:r})},Yl=e=>`
|
|
1748
|
+
fn calculateOutputIndex(index: u32) -> u32 {
|
|
1749
|
+
for (var i: u32 = 0u; i < ${e}u; i += 1u ) {
|
|
1750
|
+
if (index < ${K("uniforms.size_in_split_axis","i",e)}) {
|
|
1751
|
+
return i;
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
return ${e}u;
|
|
1755
|
+
}`,Xl=e=>{let t=e.length,r=[];for(let i=0;i<t;++i){let n=e[i].setByIndices("indices","input[global_idx]");t===1?r.push(n):i===0?r.push(`if (output_number == ${i}u) { ${n} }`):i===t-1?r.push(`else { ${n} }`):r.push(`else if (output_number == ${i}) { ${n} }`)}return`
|
|
1756
|
+
fn writeBufferData(output_number: u32, indices: ${e[0].type.indices}, global_idx: u32) {
|
|
1757
|
+
${r.join(`
|
|
1758
|
+
`)}
|
|
1759
|
+
}`},Jn=(e,t)=>{let r=e[0].dims,i=R.size(r),n=e[0].dataType,a=R.normalizeAxis(t.axis,r.length),s=new Array(t.numOutputs),u=B("input",n,r.length),l=new Array(t.numOutputs),d=[],f=[],h=0,g=[{type:12,data:i}];for(let y=0;y<t.numOutputs;y++){h+=t.splitSizes[y],l[y]=h;let v=r.slice();v[a]=t.splitSizes[y],f.push(v),s[y]=F(`output${y}`,n,v.length),d.push({dims:f[y],dataType:e[0].dataType})}g.push({type:12,data:l},...Z(r,...f));let w=y=>`
|
|
1760
|
+
${y.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",l.length).declareVariables(u,...s)}
|
|
1761
|
+
${Yl(l.length)}
|
|
1762
|
+
${Xl(s)}
|
|
1763
|
+
|
|
1764
|
+
${y.mainStart()}
|
|
1765
|
+
${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")}
|
|
1766
|
+
|
|
1767
|
+
var indices = ${u.offsetToIndices("global_idx")};
|
|
1768
|
+
var index = ${u.indicesGet("indices",a)};
|
|
1769
|
+
let output_number = calculateOutputIndex(index);
|
|
1770
|
+
if (output_number != 0) {
|
|
1771
|
+
index -= ${K("uniforms.size_in_split_axis","output_number - 1u",l.length)};
|
|
1772
|
+
${u.indicesSet("indices",a,"index")};
|
|
1773
|
+
}
|
|
1774
|
+
writeBufferData(output_number, indices, global_idx);
|
|
1775
|
+
}`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:d,dispatchGroup:{x:Math.ceil(i/64)},programUniforms:g})}},jh=(e,t)=>{Ql(e.inputs);let r=e.inputs.length===1?t:Zl(e.inputs,t);e.compute(Jn(e.inputs,r),{inputs:[0]})},Qh=e=>{let t=e.axis,r=e.splitSizes,i=e.numOutputs<0?r.length:e.numOutputs;if(i!==r.length)throw new Error("numOutputs and splitSizes length must be equal");return me({axis:t,numOutputs:i,splitSizes:r})}}),Xh=P(()=>{"use strict";ee(),ie(),Te(),ne(),Jl=(e,t)=>{let[r,i,n,a]=e,{numHeads:s,rotaryEmbeddingDim:u}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!R.areEqual(i.dims,[])&&!R.areEqual(i.dims,[1])&&i.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${i.dims.length}`);if(n.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${n.dims.length}`);if(a.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${a.dims.length}`);if(!R.areEqual(n.dims,a.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(u>0&&s===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let l=r.dims[0],d=r.dims[r.dims.length-2],f=n.dims[0],h=R.sizeFromDimension(r.dims,1)/d,g=u===0?n.dims[1]*2:h/s;if(u>g)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(i.dims.length===2){if(l!==i.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${i.dims[0]}`);if(d!==i.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${i.dims[1]}`)}if(g/2!==n.dims[1]&&u/2!==n.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${n.dims[1]}`);if(d>f)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},li=(e,t)=>{let{interleaved:r,numHeads:i,rotaryEmbeddingDim:n,scale:a}=t,s=e[0].dims[0],u=R.sizeFromDimension(e[0].dims,1),l=e[0].dims[e[0].dims.length-2],d=u/l,f=e[2].dims[1],h=n===0?f*2:d/i,g=new Array(s,l,d/h,h-f),w=R.computeStrides(g),y=[{type:1,data:a},{type:12,data:g},{type:12,data:w},...e[0].dims.length===3?new Array({type:12,data:[u,d,h,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[u,h,l*h,1]}):[],...Z(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],v=T=>{let $=B("input",e[0].dataType,e[0].dims.length),_=B("position_ids",e[1].dataType,e[1].dims.length),I=B("cos_cache",e[2].dataType,e[2].dims.length),k=B("sin_cache",e[3].dataType,e[3].dims.length),E=F("output",e[0].dataType,e[0].dims.length);return T.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:g.length},{name:"global_strides",type:"u32",length:w.length},{name:"input_output_strides",type:"u32",length:w.length}]),`
|
|
1776
|
+
${T.declareVariables($,_,I,k,E)}
|
|
1777
|
+
|
|
1778
|
+
${T.mainStart(Zt)}
|
|
1779
|
+
let half_rotary_emb_dim = uniforms.${I.name}_shape[1];
|
|
1780
|
+
let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape;
|
|
1781
|
+
let size = uniforms.global_shape[0] * uniforms.global_strides[0];
|
|
1782
|
+
${T.guardAgainstOutOfBoundsWorkgroupSizes("size")}
|
|
1783
|
+
|
|
1784
|
+
if (bsnh[3] < half_rotary_emb_dim) {
|
|
1785
|
+
let position_ids_idx =
|
|
1786
|
+
${_.broadcastedIndicesToOffset("bsnh.xy",F("",_.type.tensor,2))};
|
|
1787
|
+
let position_id =
|
|
1788
|
+
u32(${_.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0);
|
|
1789
|
+
let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r});
|
|
1790
|
+
let j = i + select(half_rotary_emb_dim, 1, ${r});
|
|
1791
|
+
let re = ${$.getByOffset("i")} * ${I.get("position_id","bsnh[3]")} -
|
|
1792
|
+
${$.getByOffset("j")} * ${k.get("position_id","bsnh[3]")};
|
|
1793
|
+
${E.setByOffset("i","re")}
|
|
1794
|
+
let im = ${$.getByOffset("i")} * ${k.get("position_id","bsnh[3]")} +
|
|
1795
|
+
${$.getByOffset("j")} * ${I.get("position_id","bsnh[3]")};
|
|
1796
|
+
${E.setByOffset("j","im")}
|
|
1797
|
+
} else {
|
|
1798
|
+
let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim;
|
|
1799
|
+
${E.setByOffset("k",$.getByOffset("k"))}
|
|
1800
|
+
}
|
|
1801
|
+
}`};return{name:"RotaryEmbedding",shaderCache:{hint:me({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:v,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(R.size(g)/Zt)},programUniforms:y})}},Yh=(e,t)=>{Jl(e.inputs,t),e.compute(li(e.inputs,t))}}),x0=P(()=>{"use strict";Te(),ee(),ya(),Kh(),Zh(),$t(),Xh(),ne(),ed=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let r=e[0],i=e[1],n=e[2],a=e[3],s=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let u=!1,l=r.dims[0],d=r.dims[1],f=r.dims.length===3?u?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],h=d,g=0,w=!i||i.dims.length===0,y=Math.floor(w?f/(t.numHeads+2*t.kvNumHeads):f/t.numHeads);w&&(f=y*t.numHeads);let v=a&&a.dims.length!==0,T=s&&s.dims.length!==0;if(v&&a.dims.length===4&&a.dims[0]===l&&a.dims[1]!==t.kvNumHeads&&a.dims[2]===t.kvNumHeads&&a.dims[3]===y)throw new Error("BSNH pastKey/pastValue is not supported");if(v&&T){if(a.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(s.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=a.dims[2]}else if(v||T)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $=1;if(i&&i.dims.length>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(i.dims.length<3||i.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==i.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(i.dims.length===3){if(r.dims[2]%i.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');h=i.dims[1]}else if(i.dims.length===5){if(i.dims[2]!==t.numHeads||i.dims[3]!==2||i.dims[4]!==y)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(n)throw new Error('Expect "value" be none when "key" has packed kv format.');h=i.dims[1]}else{if(i.dims[1]!==t.numHeads||i.dims[3]!==y)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');h=i.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}let _=0,I=!1,k=t.kvNumHeads?y*t.kvNumHeads:f;if(n&&n.dims.length>0){if(n.dims.length!==3&&n.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(n.dims.length===3){if(h!==n.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');k=n.dims[2]}else{if(h!==n.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');k=n.dims[1]*n.dims[3],I=!0}}let E=e.length>4?e[5]:void 0;if(E&&E.dims.length!==1&&E.dims[0]!==l)throw new Error('Input "seqlens" is expected to have 1 dimension and the same dim 0 as batch_size');return{batchSize:l,sequenceLength:d,pastSequenceLength:g,kvSequenceLength:h,totalSequenceLength:-1,maxSequenceLength:-1,inputHiddenSize:0,hiddenSize:f,vHiddenSize:k,headSize:y,vHeadSize:Math.floor(k/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:_,scale:t.scale,broadcastResPosBias:!1,passPastInKv:I,qkvFormat:$}},td=me({perm:[0,2,1,3]}),$n=(e,t,r)=>{let i=t,n=r.kvNumHeads;return t.dims.length===3&&r.kvSequenceLength!==0&&(i=t.reshape([r.batchSize,r.kvSequenceLength,n,r.headSize]),i=e.compute(De(i,td.perm),{inputs:[i],outputs:[-1]})[0]),i},rd=(e,t,r,i)=>{let n=7,a=["type","type"],s=[e*t],u=e*t,l=[{type:12,data:u},{type:12,data:t},{type:12,data:e}],d=f=>{let h=B("seq_lens",r.dataType,r.dims),g=B("total_seq_lens",i.dataType,i.dims),w=F("pos_ids",n,s),y=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return`
|
|
1802
|
+
${f.registerUniforms(y).declareVariables(h,g,w)}
|
|
1803
|
+
${f.mainStart()}
|
|
1804
|
+
${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1805
|
+
let total_sequence_length = u32(${g.getByOffset("0")});
|
|
1806
|
+
let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length;
|
|
1807
|
+
let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length;
|
|
1808
|
+
let batch_idx = global_idx / uniforms.sequence_length;
|
|
1809
|
+
let sequence_idx = i32(global_idx % uniforms.sequence_length);
|
|
1810
|
+
var pos_id: i32 = 0;
|
|
1811
|
+
let seqlen = ${h.getByOffset("batch_idx")};
|
|
1812
|
+
let total_seqlen = seqlen + 1;
|
|
1813
|
+
if (is_first_prompt) {
|
|
1814
|
+
if (sequence_idx < total_seqlen) {
|
|
1815
|
+
pos_id = sequence_idx;
|
|
1816
|
+
} else {
|
|
1817
|
+
pos_id = 1;
|
|
1818
|
+
}
|
|
1819
|
+
${w.setByOffset("global_idx","pos_id")}
|
|
1820
|
+
} else if (is_subsequent_prompt) {
|
|
1821
|
+
let past_seqlen = total_seqlen - i32(uniforms.sequence_length);
|
|
1822
|
+
if (past_seqlen + sequence_idx < total_seqlen) {
|
|
1823
|
+
pos_id = past_seqlen + sequence_idx;
|
|
1824
|
+
} else {
|
|
1825
|
+
pos_id = 1;
|
|
1826
|
+
}
|
|
1827
|
+
${w.setByOffset("global_idx","pos_id")}
|
|
1828
|
+
} else if (global_idx < uniforms.batch_size) {
|
|
1829
|
+
${w.setByOffset("global_idx","seqlen")}
|
|
1830
|
+
};
|
|
1831
|
+
}
|
|
1832
|
+
`};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:a},getRunData:()=>({outputs:[{dims:s,dataType:n}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:l}),getShaderSource:d}},Jh=(e,t)=>{let r=ed(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let i=e.inputs[0],n=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,a=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,s=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,u=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,l=e.inputs.length>4?e.inputs[5]:void 0,d=e.inputs.length>5?e.inputs[6]:void 0,f=r.kvNumHeads?r.kvNumHeads:r.numHeads,h=me({axis:2,numOutputs:3,splitSizes:[r.numHeads*r.headSize,f*r.headSize,f*r.headSize]}),[g,w,y]=!n&&!a?e.compute(Jn([i],h),{inputs:[i],outputs:[-1,-1,-1]}):[i,n,a],v,T;if(t.doRotary){let k=e.compute(rd(r.batchSize,r.sequenceLength,l,d),{inputs:[l,d],outputs:[-1]})[0],E=e.inputs[7],C=e.inputs[8],O=me({interleaved:t.rotaryInterleaved!==0,numHeads:r.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),x=[g,k,E,C],D=[-1];v=e.compute(li(x,O),{inputs:x,outputs:D})[0],x.splice(0,1,w);let L=me({interleaved:t.rotaryInterleaved!==0,numHeads:r.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});T=e.compute(li(x,L),{inputs:x,outputs:D})[0]}let $=br(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,t.doRotary?v:g,void 0,0),_=$n(e,t.doRotary?T:w,r),I=$n(e,y,r);Tr(e,$,_,I,void 0,void 0,s,u,void 0,r,l,d)}}),T0=P(()=>{"use strict";ee(),ie(),$t(),ne(),xn=(e,t,r,i,n,a,s,u)=>{let l=xe(a),d=l===1?"f32":`vec${l}f`,f=l===1?"vec2f":`mat2x${l}f`,h=n*s,g=64;h===1&&(g=256);let w=[n,s,a/l],y=[n,s,2],v=["rank","type","type"],T=[];T.push(...Z(w,y));let $=_=>{let I=B("x",t.dataType,3,l),k=B("scale",r.dataType,r.dims),E=B("bias",i.dataType,i.dims),C=F("output",1,3,2),O=[I,k,E,C];return`
|
|
1833
|
+
var<workgroup> workgroup_shared : array<${f}, ${g}>;
|
|
1834
|
+
const workgroup_size = ${g}u;
|
|
1835
|
+
${_.declareVariables(...O)}
|
|
1836
|
+
${_.mainStart(g)}
|
|
1837
|
+
let batch = workgroup_index / uniforms.x_shape[1];
|
|
1838
|
+
let channel = workgroup_index % uniforms.x_shape[1];
|
|
1839
|
+
let hight = uniforms.x_shape[2];
|
|
1840
|
+
// initialize workgroup memory
|
|
1841
|
+
var sum = ${d}(0);
|
|
1842
|
+
var squared_sum = ${d}(0);
|
|
1843
|
+
for (var h = local_idx; h < hight; h += workgroup_size) {
|
|
1844
|
+
let value = ${d}(${I.get("batch","channel","h")});
|
|
1845
|
+
sum += value;
|
|
1846
|
+
squared_sum += value * value;
|
|
1847
|
+
}
|
|
1848
|
+
workgroup_shared[local_idx] = ${f}(sum, squared_sum);
|
|
1849
|
+
workgroupBarrier();
|
|
1850
|
+
|
|
1851
|
+
for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) {
|
|
1852
|
+
if (local_idx < currSize) {
|
|
1853
|
+
workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize];
|
|
1854
|
+
}
|
|
1855
|
+
workgroupBarrier();
|
|
1856
|
+
}
|
|
1857
|
+
if (local_idx == 0) {
|
|
1858
|
+
let sum_final = ${vt("workgroup_shared[0][0]",l)} / f32(hight * ${l});
|
|
1859
|
+
let squared_sum_final = ${vt("workgroup_shared[0][1]",l)} / f32(hight * ${l});
|
|
1860
|
+
|
|
1861
|
+
let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${u}));
|
|
1862
|
+
let channel_scale = inv_std_dev * f32(scale[channel]);
|
|
1863
|
+
let channel_shift = f32(bias[channel]) - sum_final * channel_scale;
|
|
1864
|
+
output[workgroup_index] = vec2f(channel_scale, channel_shift);
|
|
1865
|
+
}
|
|
1866
|
+
}`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${l};${u};${g}`,inputDependencies:v},getRunData:()=>({outputs:[{dims:y,dataType:1}],dispatchGroup:{x:h},programUniforms:T}),getShaderSource:$},{inputs:[t,r,i],outputs:[-1]})[0]},id=(e,t,r)=>{let i=t[0].dims,n=i,a=2,s=i[0],u=i[1],l=R.sizeFromDimension(i,a),d=xe(l),f=R.size(n)/d,h=xn(e,t[0],t[1],t[2],s,l,u,r.epsilon),g=[s,u,l/d],w=[s,u],y=["type","none"],v=T=>{let $=B("x",t[0].dataType,g.length,d),_=B("scale_shift",1,w.length,2),I=F("output",t[0].dataType,g.length,d),k=[$,_,I];return`
|
|
1867
|
+
${T.registerUniform("output_size","u32").declareVariables(...k)}
|
|
1868
|
+
${T.mainStart()}
|
|
1869
|
+
${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
1870
|
+
let outputIndices = ${I.offsetToIndices("global_idx")};
|
|
1871
|
+
let batch = outputIndices[0];
|
|
1872
|
+
let channel = outputIndices[1];
|
|
1873
|
+
let scale_shift = ${_.getByIndices("vec2<u32>(batch, channel)")};
|
|
1874
|
+
let value = ${$.getByOffset("global_idx")} * ${I.type.value}(scale_shift.x) + ${I.type.value}(scale_shift.y);
|
|
1875
|
+
${I.setByOffset("global_idx","value")};
|
|
1876
|
+
}`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${d}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:n,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:[{type:12,data:f},...Z(g,w,g)]}),getShaderSource:v},{inputs:[t[0],h]})},nd=(e,t,r)=>{let i=t[0].dims,n=i,a=i[0],s=i[i.length-1],u=R.sizeFromDimension(i,1)/s,l=xe(s),d=R.size(n)/l,f=[{type:12,data:u},{type:12,data:Math.floor(s/l)}],h=["type","type"],g=!1,w=[0,i.length-1];for(let $=0;$<i.length-2;$++)g=g||i[$+1]!==1,w.push($+1);g=g&&i[i.length-1]!==1;let y=g?e.compute(De(e.inputs[0],w),{inputs:[e.inputs[0]],outputs:[-1]})[0]:e.inputs[0].reshape(Array.from({length:i.length},($,_)=>i[w[_]])),v=xn(e,y,t[1],t[2],a,u,s,r.epsilon),T=$=>{let _=Ee(t[0].dataType),I=l===1?"vec2f":`mat${l}x2f`,k=O=>{let x=O===0?"x":"y",D=l===1?"f32":`vec${l}f`;switch(l){case 1:return`${_}(${D}(scale.${x}))`;case 2:return`vec2<${_}>(${D}(scale[0].${x}, scale[1].${x}))`;case 4:return`vec4<${_}>(${D}(scale[0].${x}, scale[1].${x}, scale[2].${x}, scale[3].${x}))`;default:throw new Error(`Not supported compoents ${l}`)}},E=B("input",t[0].dataType,t[0].dims,l),C=F("output",t[0].dataType,n,l);return`
|
|
1877
|
+
@group(0) @binding(0) var<storage, read> input : array<${E.type.storage}>;
|
|
1878
|
+
@group(0) @binding(1) var<storage, read> scale_input : array<${I}>;
|
|
1879
|
+
@group(0) @binding(2) var<storage, read_write> output : array<${C.type.storage}>;
|
|
1880
|
+
struct Uniforms {H: u32, C : u32};
|
|
1881
|
+
@group(0) @binding(3) var<uniform> uniforms: Uniforms;
|
|
1882
|
+
|
|
1883
|
+
${$.mainStart()}
|
|
1884
|
+
let current_image_number = global_idx / (uniforms.C * uniforms.H);
|
|
1885
|
+
let current_channel_number = global_idx % uniforms.C;
|
|
1886
|
+
|
|
1887
|
+
let scale_offset = current_image_number * uniforms.C + current_channel_number;
|
|
1888
|
+
let scale = scale_input[scale_offset];
|
|
1889
|
+
output[global_idx] = fma(input[global_idx], ${k(0)}, ${k(1)});
|
|
1890
|
+
}`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${l}`,inputDependencies:h},getRunData:()=>({outputs:[{dims:n,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:f}),getShaderSource:T},{inputs:[t[0],v]})},ef=(e,t)=>{t.format==="NHWC"?nd(e,e.inputs,t):id(e,e.inputs,t)}}),S0=P(()=>{"use strict";ee(),ie(),ne(),ad=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},sd=(e,t,r)=>{let i=t.simplified,n=e[0].dims,a=e[1],s=!i&&e[2],u=n,l=R.normalizeAxis(t.axis,n.length),d=R.sizeToDimension(n,l),f=R.sizeFromDimension(n,l),h=R.size(a.dims),g=s?R.size(s.dims):0;if(h!==f||s&&g!==f)throw new Error(`Size of X.shape()[axis:] == ${f}.
|
|
1891
|
+
Size of scale and bias (if provided) must match this.
|
|
1892
|
+
Got scale size of ${h} and bias size of ${g}`);let w=[];for(let E=0;E<n.length;++E)E<l?w.push(n[E]):w.push(1);let y=xe(f),v=["type","type"],T=[{type:12,data:d},{type:1,data:f},{type:12,data:Math.floor(f/y)},{type:1,data:t.epsilon}];s&&v.push("type");let $=r>1,_=r>2,I=E=>{let C=Ee(e[0].dataType),O=[B("x",e[0].dataType,e[0].dims,y),B("scale",a.dataType,a.dims,y)];s&&O.push(B("bias",s.dataType,s.dims,y)),O.push(F("output",e[0].dataType,u,y)),$&&O.push(F("mean_data_output",1,w)),_&&O.push(F("inv_std_output",1,w));let x=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return`
|
|
1893
|
+
${E.registerUniforms(x).declareVariables(...O)}
|
|
1894
|
+
${E.mainStart()}
|
|
1895
|
+
${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")}
|
|
1896
|
+
let offset = global_idx * uniforms.norm_size_vectorized;
|
|
1897
|
+
var mean_vector = ${Gn("f32",y)};
|
|
1898
|
+
var mean_square_vector = ${Gn("f32",y)};
|
|
1899
|
+
|
|
1900
|
+
for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) {
|
|
1901
|
+
let value = ${jt(C,y,"x[h + offset]")};
|
|
1902
|
+
mean_vector += value;
|
|
1903
|
+
mean_square_vector += value * value;
|
|
1904
|
+
}
|
|
1905
|
+
let mean = ${vt("mean_vector",y)} / uniforms.norm_size;
|
|
1906
|
+
let inv_std_dev = inverseSqrt(${vt("mean_square_vector",y)} / uniforms.norm_size ${i?"":"- mean * mean"} + uniforms.epsilon);
|
|
1907
|
+
|
|
1908
|
+
for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) {
|
|
1909
|
+
let f32input = ${jt(C,y,"x[j + offset]")};
|
|
1910
|
+
let f32scale = ${jt(C,y,"scale[j]")};
|
|
1911
|
+
output[j + offset] = ${O[0].type.value}((f32input ${i?"":"- mean"}) * inv_std_dev * f32scale
|
|
1912
|
+
${s?`+ ${jt(C,y,"bias[j]")}`:""}
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
${$?"mean_data_output[global_idx] = mean":""};
|
|
1917
|
+
${_?"inv_std_output[global_idx] = inv_std_dev":""};
|
|
1918
|
+
}`},k=[{dims:u,dataType:e[0].dataType}];return $&&k.push({dims:w,dataType:1}),_&&k.push({dims:w,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${y};${r};${i}`,inputDependencies:v},getRunData:()=>({outputs:k,dispatchGroup:{x:Math.ceil(d/64)},programUniforms:T}),getShaderSource:I}},tf=(e,t)=>{ad(e.inputs),e.compute(sd(e.inputs,t,e.outputCount))}}),k0=P(()=>{"use strict";ie(),$a(),xa(),od=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},rf=e=>{od(e.inputs);let t=Qt.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],i=e.inputs[0].dims[e.inputs[0].dims.length-1];if(r<8&&i<8)e.compute(va(e.inputs,{activation:""},t));else{let n=t[t.length-2],a=R.size(e.inputs[0].dims.slice(0,-2)),s=R.size(e.inputs[1].dims.slice(0,-2));if(a!==1&&n===1&&s===1){let u=e.inputs[0].reshape([1,a,i]),l=e.inputs[1].reshape([1,i,r]),d=[1,a,r],f=[u,l];e.compute(ui(f,{activation:""},t,d),{inputs:f})}else e.compute(ui(e.inputs,{activation:""},t))}}}),I0=P(()=>{"use strict";ee(),ie(),Te(),ne(),ud=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],i=r.dims.length;if(r.dims[i-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let n=Math.floor((t.k+t.blockSize-1)/t.blockSize),a=t.blockSize/8*t.bits,s=e[1];if(!R.areEqual(s.dims,[t.n,n,a]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let u=e[2].dims;if(R.size(u)!==t.n*n)throw new Error("scales input size error.");if(e.length===4){let l=e[3].dims,d=t.n*(t.bits===8?n:Math.floor((n*t.bits+7)/8));if(R.size(l)!==d)throw new Error("zeroPoints input size error.")}},ld=(e,t)=>{let r=e[0].dims,i=r.length,n=r[i-2],a=t.k,s=t.n,u=r.slice(0,i-2),l=R.size(u),d=e[1].dims[2]/4,f=e[0].dataType,h=xe(t.k),g=xe(d),w=xe(s),y=u.concat([n,s]),v=n>1&&s/w%2===0?2:1,T=R.size(y)/w/v,$=64,_=[],I=[l,n,a/h],k=R.convertShape(e[1].dims).slice();k.splice(-1,1,d/g),_.push(...Z(I)),_.push(...Z(k)),_.push(...Z(e[2].dims)),e.length===4&&_.push(...Z(R.convertShape(e[3].dims)));let E=[l,n,s/w];_.push(...Z(E));let C=O=>{let x=I.length,D=B("a",e[0].dataType,x,h),L=B("b",12,k.length,g),Y=B("scales",e[2].dataType,e[2].dims.length),q=[D,L,Y],j=e.length===4?B("zero_points",12,e[3].dims.length):void 0;j&&q.push(j);let ue=E.length,A=F("output",e[0].dataType,ue,w),U=Ee(e[0].dataType),re=(()=>{switch(h){case 1:return`array<${U}, 8>`;case 2:return`mat4x2<${U}>`;case 4:return`mat2x4<${U}>`;default:throw new Error(`${h}-component is not supported.`)}})(),J=()=>{let N=`
|
|
1919
|
+
// reuse a data
|
|
1920
|
+
var input_offset = ${D.indicesToOffset(`${D.type.indices}(batch, row, word_offset)`)};
|
|
1921
|
+
var a_data: ${re};
|
|
1922
|
+
for (var j: u32 = 0; j < ${8/h}; j++) {
|
|
1923
|
+
a_data[j] = ${D.getByOffset("input_offset")};
|
|
1924
|
+
input_offset++;
|
|
1925
|
+
}
|
|
1926
|
+
`;for(let V=0;V<w*v;V++)N+=`
|
|
1927
|
+
b_value = ${g===1?`b${V}_data`:`b${V}_data[i]`};
|
|
1928
|
+
b_value_lower = unpack4xU8(b_value & b_mask);
|
|
1929
|
+
b_value_upper = unpack4xU8((b_value >> 4) & b_mask);
|
|
1930
|
+
b_quantized_values = ${re}(${Array.from({length:4},(te,ce)=>`${U}(b_value_lower[${ce}]), ${U}(b_value_upper[${ce}])`).join(", ")});
|
|
1931
|
+
b_dequantized_values = ${h===1?`${re}(${Array.from({length:8},(te,ce)=>`(b_quantized_values[${ce}] - ${j?`zero_point${V}`:"zero_point"}) * scale${V}`).join(", ")});`:`(b_quantized_values - ${re}(${Array(8).fill(`${j?`zero_point${V}`:"zero_point"}`).join(",")})) * scale${V};`};
|
|
1932
|
+
workgroup_shared[local_id.x * ${v} + ${Math.floor(V/w)}]${w>1?`[${V%w}]`:""} += ${Array.from({length:8/h},(te,ce)=>`${h===1?`a_data[${ce}] * b_dequantized_values[${ce}]`:`dot(a_data[${ce}], b_dequantized_values[${ce}])`}`).join(" + ")};
|
|
1933
|
+
`;return N},H=()=>{let N=`
|
|
1934
|
+
var col_index = col * ${w};
|
|
1935
|
+
${j?`
|
|
1936
|
+
let zero_point_bytes_per_col = (nBlocksPerCol + 1) / 2;
|
|
1937
|
+
var zero_point_byte_count: u32;
|
|
1938
|
+
var zero_point_word_index: u32;
|
|
1939
|
+
var zero_point_byte_offset: u32;
|
|
1940
|
+
let zero_point_nibble_offset: u32 = block & 0x1u;
|
|
1941
|
+
var zero_point_bits_offset: u32;
|
|
1942
|
+
var zero_point_word: u32;`:`
|
|
1943
|
+
// The default zero point is 8 for unsigned 4-bit quantization.
|
|
1944
|
+
let zero_point = ${U}(8);`}
|
|
1945
|
+
`;for(let V=0;V<w*v;V++)N+=`
|
|
1946
|
+
let scale${V} = ${Y.getByOffset("col_index * nBlocksPerCol + block")};
|
|
1947
|
+
${j?`
|
|
1948
|
+
zero_point_byte_count = col_index * zero_point_bytes_per_col + (block >> 0x1u);
|
|
1949
|
+
zero_point_word_index = zero_point_byte_count >> 0x2u;
|
|
1950
|
+
zero_point_byte_offset = zero_point_byte_count & 0x3u;
|
|
1951
|
+
zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2);
|
|
1952
|
+
zero_point_word = ${j.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;
|
|
1953
|
+
let zero_point${V} = ${U}((zero_point_word) & 0xFu);`:""}
|
|
1954
|
+
col_index += 1;`;return N},pe=()=>{let N=`col_index = col * ${w};`;for(let V=0;V<w*v;V++)N+=`
|
|
1955
|
+
let b${V}_data = ${L.getByIndices(`${L.type.indices}(col_index, block, word)`)};
|
|
1956
|
+
col_index += 1;`;return N+=`
|
|
1957
|
+
var b_value: u32;
|
|
1958
|
+
let b_mask: u32 = 0x0F0F0F0Fu;
|
|
1959
|
+
var b_value_lower: vec4<u32>;
|
|
1960
|
+
var b_value_upper: vec4<u32>;
|
|
1961
|
+
var b_quantized_values: ${re};
|
|
1962
|
+
var b_dequantized_values: ${re};`,N};return`
|
|
1963
|
+
var<workgroup> workgroup_shared: array<${A.type.value}, ${v*$}>;
|
|
1964
|
+
${O.declareVariables(...q,A)}
|
|
1965
|
+
${O.mainStart([$,1,1])}
|
|
1966
|
+
let output_indices = ${A.offsetToIndices(`(global_idx / ${$}) * ${v}`)};
|
|
1967
|
+
let col = output_indices[2];
|
|
1968
|
+
let row = output_indices[1];
|
|
1969
|
+
let batch = output_indices[0];
|
|
1970
|
+
let nBlocksPerCol = uniforms.b_shape[1];
|
|
1971
|
+
|
|
1972
|
+
for (var block = local_id.x; block < nBlocksPerCol; block += ${$}) {
|
|
1973
|
+
//process one block
|
|
1974
|
+
var word_offset: u32 = block * ${t.blockSize/h};
|
|
1975
|
+
${H()}
|
|
1976
|
+
for (var word: u32 = 0; word < ${d}; word += ${g}) {
|
|
1977
|
+
${pe()}
|
|
1978
|
+
for (var i: u32 = 0; i < ${g}; i++) {
|
|
1979
|
+
${J()}
|
|
1980
|
+
word_offset += ${8/h};
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
workgroupBarrier();
|
|
1985
|
+
|
|
1986
|
+
if (local_id.x < ${v}) {
|
|
1987
|
+
var output_value: ${A.type.value} = ${A.type.value}(0);
|
|
1988
|
+
var workgroup_shared_offset: u32 = local_id.x;
|
|
1989
|
+
for (var b: u32 = 0u; b < ${$}u; b++) {
|
|
1990
|
+
output_value += workgroup_shared[workgroup_shared_offset];
|
|
1991
|
+
workgroup_shared_offset += ${v};
|
|
1992
|
+
}
|
|
1993
|
+
${A.setByIndices(`${A.type.indices}(batch, row, col + local_id.x)`,"output_value")};
|
|
1994
|
+
}
|
|
1995
|
+
}`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${h};${g};${w};${v};${$}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:y,dataType:f}],dispatchGroup:{x:T},programUniforms:_}),getShaderSource:C}},dd=(e,t)=>{let r=e[0].dims,i=r.length,n=r[i-2],a=t.k,s=t.n,u=r.slice(0,i-2),l=R.size(u),d=e[1].dims[2]/4,f=e[0].dataType,h=xe(t.k),g=xe(d),w=u.concat([n,s]),y=128,v=s%8===0?8:s%4===0?4:1,T=y/v,$=T*g*8,_=$/h,I=$/t.blockSize,k=R.size(w)/v,E=[],C=[l,n,a/h],O=R.convertShape(e[1].dims).slice();O.splice(-1,1,d/g),E.push(...Z(C)),E.push(...Z(O)),E.push(...Z(e[2].dims)),e.length===4&&E.push(...Z(R.convertShape(e[3].dims)));let x=[l,n,s];E.push(...Z(x));let D=L=>{let Y=C.length,q=B("a",e[0].dataType,Y,h),j=B("b",12,O.length,g),ue=B("scales",e[2].dataType,e[2].dims.length),A=[q,j,ue],U=e.length===4?B("zero_points",12,e[3].dims.length):void 0;U&&A.push(U);let re=x.length,J=F("output",e[0].dataType,re),H=Ee(e[0].dataType),pe=()=>{switch(h){case 1:return`
|
|
1996
|
+
let a_data0 = vec4<${H}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]);
|
|
1997
|
+
let a_data1 = vec4<${H}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return`
|
|
1998
|
+
let a_data0 = vec4<${H}>(sub_a[word_offset], sub_a[word_offset + 1]);
|
|
1999
|
+
let a_data1 = vec4<${H}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return`
|
|
2000
|
+
let a_data0 = sub_a[word_offset];
|
|
2001
|
+
let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${h}-component is not supported.`)}};return`
|
|
2002
|
+
var<workgroup> sub_a: array<${q.type.value}, ${_}>;
|
|
2003
|
+
var<workgroup> inter_results: array<array<${J.type.value}, ${T}>, ${v}>;
|
|
2004
|
+
${L.declareVariables(...A,J)}
|
|
2005
|
+
${L.mainStart([T,v,1])}
|
|
2006
|
+
let output_indices = ${J.offsetToIndices(`workgroup_index * ${v}`)};
|
|
2007
|
+
let col = output_indices[2];
|
|
2008
|
+
let row = output_indices[1];
|
|
2009
|
+
let batch = output_indices[0];
|
|
2010
|
+
let n_blocks_per_col = uniforms.b_shape[1];
|
|
2011
|
+
let num_tiles = (n_blocks_per_col - 1) / ${I} + 1;
|
|
2012
|
+
|
|
2013
|
+
// Loop over shared dimension.
|
|
2014
|
+
for (var tile: u32 = 0; tile < num_tiles; tile += 1) {
|
|
2015
|
+
let a_col_start = tile * ${_};
|
|
2016
|
+
// load one tile A data into shared memory.
|
|
2017
|
+
for (var a_offset = local_idx; a_offset < ${_}; a_offset += ${y})
|
|
2018
|
+
{
|
|
2019
|
+
let a_col = a_col_start + a_offset;
|
|
2020
|
+
if (a_col < uniforms.a_shape[2])
|
|
2021
|
+
{
|
|
2022
|
+
sub_a[a_offset] = ${q.getByIndices(`${q.type.indices}(batch, row, a_col)`)};
|
|
2023
|
+
} else {
|
|
2024
|
+
sub_a[a_offset] = ${q.type.value}(0);
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
workgroupBarrier();
|
|
2028
|
+
|
|
2029
|
+
// each thread process one block
|
|
2030
|
+
let b_row = col + local_id.y;
|
|
2031
|
+
let block = tile * ${I} + local_id.x;
|
|
2032
|
+
${U?`
|
|
2033
|
+
let zero_point_bytes_per_col = (n_blocks_per_col + 1) / 2;
|
|
2034
|
+
let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block >> 0x1u);
|
|
2035
|
+
let zero_point_word_index = zero_point_byte_count >> 0x2u;
|
|
2036
|
+
let zero_point_byte_offset = zero_point_byte_count & 0x3u;
|
|
2037
|
+
let zero_point_nibble_offset: u32 = block & 0x1u;
|
|
2038
|
+
let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2);
|
|
2039
|
+
let zero_point_word = ${U.getByOffset("zero_point_word_index")} >> zero_point_bits_offset;
|
|
2040
|
+
let zero_point = ${H}((zero_point_word) & 0xFu);`:`
|
|
2041
|
+
// The default zero point is 8 for unsigned 4-bit quantization.
|
|
2042
|
+
let zero_point = ${H}(8);`}
|
|
2043
|
+
let scale = ${ue.getByOffset("b_row * n_blocks_per_col + block")};
|
|
2044
|
+
let b_data = ${j.getByIndices(`${j.type.indices}(b_row, block, 0)`)};
|
|
2045
|
+
var word_offset = local_id.x * ${t.blockSize/h};
|
|
2046
|
+
for (var i: u32 = 0; i < ${g}; i++) {
|
|
2047
|
+
${pe()}
|
|
2048
|
+
let b_value = ${g===1?"b_data":"b_data[i]"};
|
|
2049
|
+
let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu);
|
|
2050
|
+
let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu);
|
|
2051
|
+
let b_quantized_values = mat2x4<${H}>(${Array.from({length:4},(N,V)=>`${H}(b_value_lower[${V}]), ${H}(b_value_upper[${V}])`).join(", ")});
|
|
2052
|
+
let b_dequantized_values = (b_quantized_values - mat2x4<${H}>(${Array(8).fill("zero_point").join(",")})) * scale;
|
|
2053
|
+
inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(N,V)=>`${`dot(a_data${V}, b_dequantized_values[${V}])`}`).join(" + ")};
|
|
2054
|
+
word_offset += ${8/h};
|
|
2055
|
+
}
|
|
2056
|
+
workgroupBarrier();
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
if (local_idx < ${v}) {
|
|
2060
|
+
var output_value: ${J.type.value} = ${J.type.value}(0);
|
|
2061
|
+
for (var b = 0u; b < ${T}; b++) {
|
|
2062
|
+
output_value += inter_results[local_idx][b];
|
|
2063
|
+
}
|
|
2064
|
+
if (col + local_idx < uniforms.output_shape[2])
|
|
2065
|
+
{
|
|
2066
|
+
${J.setByIndices(`${J.type.indices}(batch, row, col + local_idx)`,"output_value")}
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
}`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${h};${g};${T};${v}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:w,dataType:f}],dispatchGroup:{x:k},programUniforms:E}),getShaderSource:D}},nf=(e,t)=>{ud(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(dd(e.inputs,t)):e.compute(ld(e.inputs,t))},af=e=>me(e)}),E0=P(()=>{"use strict";ee(),ie(),ne(),pd=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},cd=(e,t,r)=>{let i="";for(let n=t-1;n>=0;--n)i+=`
|
|
2070
|
+
k = i32(${e.indicesGet("indices",n)}) - ${K("uniforms.pads",n,r)};
|
|
2071
|
+
if (k < 0) {
|
|
2072
|
+
break;
|
|
2073
|
+
}
|
|
2074
|
+
if (k >= i32(${K("uniforms.x_shape",n,t)})) {
|
|
2075
|
+
break;
|
|
2076
|
+
}
|
|
2077
|
+
offset += k * i32(${K("uniforms.x_strides",n,t)});
|
|
2078
|
+
`;return`
|
|
2079
|
+
value = ${e.type.value}(uniforms.constant_value);
|
|
2080
|
+
for (var i = 0; i < 1; i++) {
|
|
2081
|
+
var offset = 0;
|
|
2082
|
+
var k = 0;
|
|
2083
|
+
${i}
|
|
2084
|
+
value = x[offset];
|
|
2085
|
+
}
|
|
2086
|
+
`},hd=(e,t,r)=>{let i="";for(let n=t-1;n>=0;--n)i+=`
|
|
2087
|
+
k = i32(${e.indicesGet("indices",n)}) - ${K("uniforms.pads",n,r)};
|
|
2088
|
+
if (k < 0) {
|
|
2089
|
+
k = -k;
|
|
2090
|
+
}
|
|
2091
|
+
{
|
|
2092
|
+
let _2n_1 = 2 * (i32(${K("uniforms.x_shape",n,t)}) - 1);
|
|
2093
|
+
k = k % _2n_1;
|
|
2094
|
+
if(k >= i32(${K("uniforms.x_shape",n,t)})) {
|
|
2095
|
+
k = _2n_1 - k;
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
offset += k * i32(${K("uniforms.x_strides",n,t)});
|
|
2099
|
+
`;return`
|
|
2100
|
+
var offset = 0;
|
|
2101
|
+
var k = 0;
|
|
2102
|
+
${i}
|
|
2103
|
+
value = x[offset];
|
|
2104
|
+
`},fd=(e,t,r)=>{let i="";for(let n=t-1;n>=0;--n)i+=`
|
|
2105
|
+
k = i32(${e.indicesGet("indices",n)}) - ${K("uniforms.pads",n,r)};
|
|
2106
|
+
if (k < 0) {
|
|
2107
|
+
k = 0;
|
|
2108
|
+
}
|
|
2109
|
+
if (k >= i32(${K("uniforms.x_shape",n,t)})) {
|
|
2110
|
+
k = i32(${K("uniforms.x_shape",n,t)}) - 1;
|
|
2111
|
+
}
|
|
2112
|
+
offset += k * i32(${K("uniforms.x_strides",n,t)});
|
|
2113
|
+
`;return`
|
|
2114
|
+
var offset = 0;
|
|
2115
|
+
var k = 0;
|
|
2116
|
+
${i}
|
|
2117
|
+
value = x[offset];
|
|
2118
|
+
`},md=(e,t,r)=>{let i="";for(let n=t-1;n>=0;--n)i+=`
|
|
2119
|
+
k = i32(${e.indicesGet("indices",n)}) - ${K("uniforms.pads",n,r)};
|
|
2120
|
+
if (k < 0) {
|
|
2121
|
+
k += i32(${K("uniforms.x_shape",n,t)}]);
|
|
2122
|
+
}
|
|
2123
|
+
if (k >= i32(${K("uniforms.x_shape",n,t)})) {
|
|
2124
|
+
k -= i32(${K("uniforms.x_shape",n,t)});
|
|
2125
|
+
}
|
|
2126
|
+
offset += k * i32(${K("uniforms.x_strides",n,t)});
|
|
2127
|
+
`;return`
|
|
2128
|
+
var offset = 0;
|
|
2129
|
+
var k = 0;
|
|
2130
|
+
${i}
|
|
2131
|
+
value = x[offset];
|
|
2132
|
+
`},gd=(e,t,r)=>{switch(r.mode){case 0:return cd(e,t,r.pads.length);case 1:return hd(e,t,r.pads.length);case 2:return fd(e,t,r.pads.length);case 3:return md(e,t,r.pads.length);default:throw new Error("Invalid mode")}},yd=(e,t)=>{let r=R.padShape(e[0].dims.slice(),t.pads),i=e[0].dims,n=R.size(r),a=[{type:12,data:n},{type:6,data:t.pads}],s=e.length>=3&&e[2].data;t.mode===0&&a.push({type:s?e[2].dataType:1,data:t.value}),a.push(...Z(e[0].dims,r));let u=["rank"],l=d=>{let f=F("output",e[0].dataType,r.length),h=B("x",e[0].dataType,i.length),g=h.type.value,w=gd(f,i.length,t),y=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&y.push({name:"constant_value",type:s?g:"f32"}),`
|
|
2133
|
+
${d.registerUniforms(y).declareVariables(h,f)}
|
|
2134
|
+
${d.mainStart()}
|
|
2135
|
+
${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
2136
|
+
|
|
2137
|
+
let indices = ${f.offsetToIndices("global_idx")};
|
|
2138
|
+
|
|
2139
|
+
var value = ${g}(0);
|
|
2140
|
+
${w}
|
|
2141
|
+
output[global_idx] = value;
|
|
2142
|
+
}`};return{name:"Pad",shaderCache:{hint:`${t.mode}${s}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(R.size(r)/64)},programUniforms:a}),getShaderSource:l}},wd=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),i=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,n=e[0].dims.length,a=new Int32Array(2*n).fill(0);if(e.length>=4){let u=e[3].getBigInt64Array();for(let l=0;l<u.length;l++)a[Number(u[l])]=Number(r[l]),a[Number(u[l])+n]=Number(r[l+u.length])}else r.forEach((u,l)=>a[Number(l)]=Number(u));let s=[];return a.forEach(u=>s.push(u)),{mode:t.mode,value:i,pads:s}}else return t},sf=(e,t)=>{pd(e.inputs);let r=wd(e.inputs,t);e.compute(yd(e.inputs,r),{inputs:[0]})}}),C0=P(()=>{"use strict";Le(),ee(),ie(),ne(),hr=e=>{if(be.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Tn=(e,t,r)=>{let i=t.format==="NHWC",n=e.dims.slice();i&&n.splice(1,0,n.pop());let a=Object.hasOwnProperty.call(t,"dilations"),s=t.kernelShape.slice(),u=t.strides.slice(),l=a?t.dilations.slice():[],d=t.pads.slice();si.adjustPoolAttributes(r,n,s,u,l,d);let f=si.computePoolOutputShape(r,n,u,l,s,d,t.autoPad),h=Object.assign({},t);a?Object.assign(h,{kernelShape:s,strides:u,pads:d,dilations:l,cacheKey:t.cacheKey}):Object.assign(h,{kernelShape:s,strides:u,pads:d,cacheKey:t.cacheKey});let g=f.slice();return g.push(g.splice(1,1)[0]),[h,i?g:f]},Sn=(e,t)=>{let r=t.format==="NHWC",i=R.size(e),n=R.size(t.kernelShape),a=[{type:12,data:i},{type:12,data:n}],s=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let u=t.kernelShape[t.kernelShape.length-1],l=t.strides[t.strides.length-1],d=t.pads[t.pads.length/2-1],f=t.pads[t.pads.length-1],h=!!(d+f);a.push({type:12,data:u},{type:12,data:l},{type:12,data:d},{type:12,data:f}),s.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let g=!1;if(t.kernelShape.length===2){let w=t.kernelShape[t.kernelShape.length-2],y=t.strides[t.strides.length-2],v=t.pads[t.pads.length/2-2],T=t.pads[t.pads.length-2];g=!!(v+T),a.push({type:12,data:w},{type:12,data:y},{type:12,data:v},{type:12,data:T}),s.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[a,s,!0,h,g]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let u=R.computeStrides(t.kernelShape);a.push({type:12,data:u},{type:12,data:t.pads},{type:12,data:t.strides}),s.push({name:"kernelStrides",type:"u32",length:u.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let l=t.pads.reduce((d,f)=>d+f);return[a,s,!!l,!1,!1]}},kn=(e,t,r,i,n,a,s,u,l,d,f,h)=>{let g=n.format==="NHWC",w=t.type.value,y=F("output",t.type.tensor,i);if(n.kernelShape.length<=2){let v="",T="",$="",_=r-(g?2:1);if(f?v=`
|
|
2143
|
+
for (var i: u32 = 0u; i < uniforms.kw; i++) {
|
|
2144
|
+
xIndices[${_}] = indices[${_}] * uniforms.sw - uniforms.pwStart + i;
|
|
2145
|
+
if (xIndices[${_}] < 0 || xIndices[${_}]
|
|
2146
|
+
>= uniforms.x_shape[${_}]) {
|
|
2147
|
+
pad++;
|
|
2148
|
+
continue;
|
|
2149
|
+
}
|
|
2150
|
+
let x_val = x[${t.indicesToOffset("xIndices")}];
|
|
2151
|
+
${a}
|
|
2152
|
+
}`:v=`
|
|
2153
|
+
for (var i: u32 = 0u; i < uniforms.kw; i++) {
|
|
2154
|
+
xIndices[${_}] = indices[${_}] * uniforms.sw - uniforms.pwStart + i;
|
|
2155
|
+
let x_val = x[${t.indicesToOffset("xIndices")}];
|
|
2156
|
+
${a}
|
|
2157
|
+
}`,n.kernelShape.length===2){let I=r-(g?3:2);h?T=`
|
|
2158
|
+
for (var j: u32 = 0u; j < uniforms.kh; j++) {
|
|
2159
|
+
xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j;
|
|
2160
|
+
if (xIndices[${I}] < 0 || xIndices[${I}] >= uniforms.x_shape[${I}]) {
|
|
2161
|
+
pad += i32(uniforms.kw);
|
|
2162
|
+
continue;
|
|
2163
|
+
}
|
|
2164
|
+
`:T=`
|
|
2165
|
+
for (var j: u32 = 0u; j < uniforms.kh; j++) {
|
|
2166
|
+
xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j;
|
|
2167
|
+
`,$=`
|
|
2168
|
+
}
|
|
2169
|
+
`}return`
|
|
2170
|
+
${e.registerUniforms(l).declareVariables(t,y)}
|
|
2171
|
+
|
|
2172
|
+
${e.mainStart()}
|
|
2173
|
+
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
2174
|
+
|
|
2175
|
+
let indices = ${y.offsetToIndices("global_idx")};
|
|
2176
|
+
var xIndices = ${y.offsetToIndices("global_idx")};
|
|
2177
|
+
|
|
2178
|
+
var value = ${w}(${u});
|
|
2179
|
+
var pad = 0;
|
|
2180
|
+
${T}
|
|
2181
|
+
${v}
|
|
2182
|
+
${$}
|
|
2183
|
+
${s}
|
|
2184
|
+
|
|
2185
|
+
output[global_idx] = value;
|
|
2186
|
+
}`}else{if(g)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let v=n.kernelShape.length,T=n.pads.length,$="";return d?$=`
|
|
2187
|
+
if (xIndices[j] >= uniforms.x_shape[j]) {
|
|
2188
|
+
pad++;
|
|
2189
|
+
isPad = true;
|
|
2190
|
+
break;
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
if (!isPad) {
|
|
2194
|
+
let x_val = x[${t.indicesToOffset("xIndices")}];
|
|
2195
|
+
${a}
|
|
2196
|
+
}`:$=`
|
|
2197
|
+
}
|
|
2198
|
+
let x_val = x[${t.indicesToOffset("xIndices")}];
|
|
2199
|
+
${a}
|
|
2200
|
+
`,`
|
|
2201
|
+
${e.registerUniforms(l).declareVariables(t,y)}
|
|
2202
|
+
|
|
2203
|
+
${e.mainStart()}
|
|
2204
|
+
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
2205
|
+
let indices = ${y.offsetToIndices("global_idx")};
|
|
2206
|
+
var xIndices = ${y.offsetToIndices("global_idx")};
|
|
2207
|
+
|
|
2208
|
+
var offsets: array<u32, ${v}>;
|
|
2209
|
+
|
|
2210
|
+
var value = ${w}(${u});
|
|
2211
|
+
var pad = 0;
|
|
2212
|
+
var isPad = false;
|
|
2213
|
+
|
|
2214
|
+
for (var i: u32 = 0u; i < uniforms.kernelSize; i++) {
|
|
2215
|
+
var offset = i;
|
|
2216
|
+
for (var j = 0u; j < ${v-1}u; j++) {
|
|
2217
|
+
offsets[j] = offset / ${K("uniforms.kernelStrides","j",v)};
|
|
2218
|
+
offset -= offsets[j] * ${K("uniforms.kernelStrides","j",v)};
|
|
2219
|
+
}
|
|
2220
|
+
offsets[${v-1}] = offset;
|
|
2221
|
+
|
|
2222
|
+
isPad = false;
|
|
2223
|
+
for (var j = ${r-v}u; j < ${r}u; j++) {
|
|
2224
|
+
xIndices[j] = indices[j] * ${K("uniforms.strides",`j - ${r-v}u`,v)}
|
|
2225
|
+
+ offsets[j - ${r-v}u] - ${K("uniforms.pads","j - 2u",T)};
|
|
2226
|
+
${$}
|
|
2227
|
+
}
|
|
2228
|
+
${s}
|
|
2229
|
+
|
|
2230
|
+
output[global_idx] = value;
|
|
2231
|
+
}`}},In=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,_d=e=>`${In(e)};${e.countIncludePad}`,bd=e=>`${In(e)};${e.storageOrder};${e.dilations}`,En=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Cn=(e,t,r,i)=>{let[n,a]=Tn(t,i,r),s=B("x",t.dataType,t.dims.length),u=s.type.value,l="value += x_val;",d="";n.countIncludePad?d+=`value /= ${u}(uniforms.kernelSize);`:d+=`value /= ${u}(i32(uniforms.kernelSize) - pad);`;let[f,h,g,w,y]=Sn(a,n);f.push(...Z(t.dims,a));let v=["rank"];return{name:e,shaderCache:{hint:`${i.cacheKey};${g};${w};${y}`,inputDependencies:v},getRunData:()=>({outputs:[{dims:a,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(R.size(a)/64)},programUniforms:f}),getShaderSource:T=>kn(T,s,t.dims.length,a.length,n,l,d,0,h,g,w,y)}},of=e=>{let t=e.count_include_pad!==0,r=En(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let i={countIncludePad:t,...r,cacheKey:""};return{...i,cacheKey:_d(i)}},uf=(e,t)=>{hr(e.inputs),e.compute(Cn("AveragePool",e.inputs[0],!1,t))},zn={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},lf=e=>{let t=e.format;return{format:t,...zn,cacheKey:t}},df=(e,t)=>{hr(e.inputs),e.compute(Cn("GlobalAveragePool",e.inputs[0],!0,t))},An=(e,t,r,i)=>{let[n,a]=Tn(t,i,r),s=`
|
|
2232
|
+
value = max(x_val, value);
|
|
2233
|
+
`,u="",l=B("x",t.dataType,t.dims.length),d=["rank"],[f,h,g,w,y]=Sn(a,n);return f.push(...Z(t.dims,a)),{name:e,shaderCache:{hint:`${i.cacheKey};${g};${w};${y}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:a,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(R.size(a)/64)},programUniforms:f}),getShaderSource:v=>kn(v,l,t.dims.length,a.length,n,s,u,t.dataType===10?-65504:-1e5,h,g,w,y)}},pf=(e,t)=>{hr(e.inputs),e.compute(An("MaxPool",e.inputs[0],!1,t))},cf=e=>{let t=e.storage_order,r=e.dilations,i=En(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(i.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let n={storageOrder:t,dilations:r,...i,cacheKey:""};return{...n,cacheKey:bd(n)}},hf=e=>{let t=e.format;return{format:t,...zn,cacheKey:t}},ff=(e,t)=>{hr(e.inputs),e.compute(An("GlobalMaxPool",e.inputs[0],!0,t))}}),z0=P(()=>{"use strict";ee(),ie(),Te(),ne(),vd=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,i)=>r===e[2].dims[i]).reduce((r,i)=>r&&i,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((n,a)=>a===t.axis||n===e[0].dims[a]).reduce((n,a)=>n&&a,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let r=e[0].dims[t.axis],i=e[1].dims[t.axis];if(t.blockSize<Math.ceil(r/i)||t.blockSize>Math.ceil(r/(i-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},$d=(e,t)=>{let r=R.normalizeAxis(t.axis,e[0].dims.length),i=e[0].dataType,n=i===3,a=e[0].dims,s=e[1].dataType,u=R.size(a),l=i===3||i===2,d=l?[Math.ceil(R.size(e[0].dims)/4)]:e[0].dims,f=e[1].dims,h=e.length>2?e[2]:void 0,g=h?l?[Math.ceil(R.size(h.dims)/4)]:h.dims:void 0,w=f.length===0||f.length===1&&f[0]===1,y=w===!1&&f.length===1,v=xe(u),T=w&&(!l||v===4),$=T?v:1,_=T&&!l?v:1,I=B("input",l?12:i,d.length,_),k=B("scale",s,f.length),E=h?B("zero_point",l?12:i,g.length):void 0,C=F("output",s,a.length,$),O=[I,k];E&&O.push(E);let x=[d,f];h&&x.push(g);let D=[{type:12,data:u/$},{type:12,data:r},{type:12,data:t.blockSize},...Z(...x,a)],L=Y=>{let q=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return`
|
|
2234
|
+
${Y.registerUniforms(q).declareVariables(...O,C)}
|
|
2235
|
+
${Y.mainStart()}
|
|
2236
|
+
${Y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
2237
|
+
let output_indices = ${C.offsetToIndices("global_idx")};
|
|
2238
|
+
|
|
2239
|
+
// Set input x
|
|
2240
|
+
${l?`
|
|
2241
|
+
let input = ${I.getByOffset("global_idx / 4")};
|
|
2242
|
+
let x_vec = ${n?"unpack4xI8(input)":"unpack4xU8(input)"};
|
|
2243
|
+
let x_value = ${$===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${I.getByOffset("global_idx")};`};
|
|
2244
|
+
|
|
2245
|
+
// Set scale input
|
|
2246
|
+
${w?`let scale_value= ${k.getByOffset("0")}`:y?`
|
|
2247
|
+
let scale_index = ${C.indicesGet("output_indices","uniforms.axis")};
|
|
2248
|
+
let scale_value= ${k.getByOffset("scale_index")};`:`
|
|
2249
|
+
var scale_indices: ${k.type.indices} = output_indices;
|
|
2250
|
+
let index = ${k.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size;
|
|
2251
|
+
${k.indicesSet("scale_indices","uniforms.axis","index")};
|
|
2252
|
+
let scale_value= ${k.getByIndices("scale_indices")};`};
|
|
2253
|
+
|
|
2254
|
+
// Set zero-point input
|
|
2255
|
+
${E?w?l?`
|
|
2256
|
+
let zero_point_input = ${E.getByOffset("0")};
|
|
2257
|
+
let zero_point_vec = ${n?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
|
|
2258
|
+
let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${E.getByOffset("0")}`:y?l?`
|
|
2259
|
+
let zero_point_index = ${C.indicesGet("output_indices","uniforms.axis")};
|
|
2260
|
+
let zero_point_input = ${E.getByOffset("zero_point_index / 4")};
|
|
2261
|
+
let zero_point_vec = ${n?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
|
|
2262
|
+
let zero_point_value = zero_point_vec[zero_point_index % 4]`:`
|
|
2263
|
+
let zero_point_index = ${C.indicesGet("output_indices","uniforms.axis")};
|
|
2264
|
+
let zero_point_value = ${E.getByOffset("zero_point_index")};`:l?`
|
|
2265
|
+
let zero_point_offset = ${k.indicesToOffset("scale_indices")};
|
|
2266
|
+
let zero_point_input = ${E.getByOffset("zero_point_offset / 4")};
|
|
2267
|
+
let zero_point_vec = ${n?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"};
|
|
2268
|
+
let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${E.getByIndices("scale_indices")};`:`let zero_point_value = ${l?n?"i32":"u32":I.type.value}(0);`};
|
|
2269
|
+
// Compute and write output
|
|
2270
|
+
${C.setByOffset("global_idx",`${C.type.value}(x_value - zero_point_value) * scale_value`)};
|
|
2271
|
+
}`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:E?["rank","rank","rank"]:["rank","rank"]},getShaderSource:L,getRunData:()=>({outputs:[{dims:a,dataType:s}],dispatchGroup:{x:Math.ceil(u/$/64),y:1,z:1},programUniforms:D})}},mf=(e,t)=>{vd(e.inputs,t),e.compute($d(e.inputs,t))},gf=e=>me({axis:e.axis,blockSize:e.blockSize})}),A0=P(()=>{"use strict";Le(),ee(),ne(),xd=(e,t,r)=>{let i=e===t,n=e<t&&r<0,a=e>t&&r>0;if(i||n||a)throw new Error("Range these inputs' contents are invalid.")},Td=(e,t,r,i)=>{let n=Math.abs(Math.ceil((t-e)/r)),a=[n],s=n,u=[{type:12,data:s},{type:i,data:e},{type:i,data:r},...Z(a)],l=d=>{let f=F("output",i,a.length),h=f.type.value,g=[{name:"outputSize",type:"u32"},{name:"start",type:h},{name:"delta",type:h}];return`
|
|
2272
|
+
${d.registerUniforms(g).declareVariables(f)}
|
|
2273
|
+
${d.mainStart()}
|
|
2274
|
+
${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
2275
|
+
output[global_idx] = uniforms.start + ${h}(global_idx) * uniforms.delta;
|
|
2276
|
+
}`};return{name:"Range",shaderCache:{hint:`${i}`},getShaderSource:l,getRunData:()=>({outputs:[{dims:a,dataType:i}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:u})}},yf=e=>{let t=0,r=0,i=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],i=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],i=e.inputs[2].getFloat32Array()[0]),be.webgpu.validateInputContent&&xd(t,r,i),e.compute(Td(t,r,i,e.inputs[0].dataType),{inputs:[]})}}),O0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Sd=(e,t,r,i)=>{if(e!=="none"&&i!=="i32"&&i!=="u32"&&i!=="f32")throw new Error(`Input ${i} is not supported with reduction ${e}.`);let n=`{
|
|
2277
|
+
var oldValue = 0;
|
|
2278
|
+
loop {
|
|
2279
|
+
let newValueF32 =`,a=`;
|
|
2280
|
+
let newValue = bitcast<i32>(newValueF32);
|
|
2281
|
+
let res = atomicCompareExchangeWeak(&${t}, oldValue, newValue);
|
|
2282
|
+
if res.exchanged {
|
|
2283
|
+
break;
|
|
2284
|
+
}
|
|
2285
|
+
oldValue = res.old_value;
|
|
2286
|
+
}
|
|
2287
|
+
}`;switch(e){case"none":return`${t}=${r};`;case"add":return i==="i32"||i==="u32"?`atomicAdd(&${t}, bitcast<${i}>(${r}));`:`
|
|
2288
|
+
${n}bitcast<${i}>(oldValue) + (${r})${a}`;case"max":return i==="i32"||i==="u32"?`atomicMax(&${t}, bitcast<${i}>(${r}));`:`
|
|
2289
|
+
${n}max(bitcast<f32>(oldValue), (${r}))${a}`;case"min":return i==="i32"||i==="u32"?`atomicMin(&${t}, bitcast<${i}>(${r}));`:`${n}min(bitcast<${i}>(oldValue), (${r}))${a}`;case"mul":return`${n}(bitcast<${i}>(oldValue) * (${r}))${a}`;default:throw new Error(`Reduction ${e} is not supported.`)}},kd=(e,t)=>{let r=e[0].dims,i=e[1].dims,n=r,a=1,s=Math.ceil(R.sizeToDimension(i,i.length-1)/a),u=i[i.length-1],l=R.sizeFromDimension(r,u),d=[{type:12,data:s},{type:12,data:u},{type:12,data:l},...Z(e[1].dims,e[2].dims,n)],f=h=>{let g=B("indices",e[1].dataType,e[1].dims.length),w=B("updates",e[2].dataType,e[2].dims.length,a),y=t.reduction!=="none"&&t.reduction!==""?Hp("output",e[0].dataType,n.length):F("output",e[0].dataType,n.length,a);return`
|
|
2290
|
+
${h.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(g,w,y)}
|
|
2291
|
+
${h.mainStart()}
|
|
2292
|
+
${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
2293
|
+
var data_offset = 0u;
|
|
2294
|
+
let indices_start = uniforms.last_index_dimension * global_idx;
|
|
2295
|
+
let indices_end = indices_start + uniforms.last_index_dimension;
|
|
2296
|
+
for (var i = indices_start; i < indices_end; i++) {
|
|
2297
|
+
var index = i32(indices[i].x);
|
|
2298
|
+
${e[0].dims.length===1?`
|
|
2299
|
+
let element_count_dim = uniforms.output_strides;
|
|
2300
|
+
let dim_value = uniforms.output_shape;`:`
|
|
2301
|
+
let element_count_dim = uniforms.output_strides[i - indices_start];
|
|
2302
|
+
let dim_value = uniforms.output_shape[i - indices_start];`}
|
|
2303
|
+
if (index >= 0) {
|
|
2304
|
+
if (index >= i32(dim_value)) {
|
|
2305
|
+
index = i32(dim_value - 1);
|
|
2306
|
+
}
|
|
2307
|
+
} else {
|
|
2308
|
+
if (index < -i32(dim_value)) {
|
|
2309
|
+
index = 0;
|
|
2310
|
+
} else {
|
|
2311
|
+
index += i32(dim_value);
|
|
2312
|
+
}
|
|
2313
|
+
}
|
|
2314
|
+
data_offset += u32((u32(index) * element_count_dim));
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
for (var i = 0u; i < uniforms.num_updates_elements; i++) {
|
|
2318
|
+
let value = updates[uniforms.num_updates_elements * global_idx + i];
|
|
2319
|
+
${Sd(t.reduction,"output[data_offset + i]","value",y.type.value)}
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
}`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:d}),getShaderSource:f}},wf=e=>me({reduction:e.reduction}),_f=(e,t)=>{e.compute(kd(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),R0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Id=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and
|
|
2323
|
+
one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Ed=(e,t,r)=>{t.every(n=>n>=0&&n<r||(()=>{throw new Error("Resize requires axes input values to be positive and less than rank")}));let i=new Array(r).fill(1);return t.forEach((n,a)=>i[n]=e[a]),i},Cd=(e,t,r,i,n,a)=>{let[s,u,l]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],d=e[0].dims.length;if(s>0&&e.length>s&&e[s].dims.length>0)e[s].getFloat32Array().forEach(f=>a.push(f));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(u>0&&e.length>u&&e[u].dims.length===1&&e[u].dims[0]>0){if(e[u].getFloat32Array().forEach(f=>i.push(f)),i.length!==0&&i.length!==d&&r>=18&&i.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Id(i,t),t.axes.length>0&&Ed(i,t.axes,d).forEach((f,h)=>i[h]=f)}if(l>0&&e.length>l&&e[l].dims.length===1&&e[l].dims[0]>0&&(e[l].getBigInt64Array().forEach(f=>n.push(Number(f))),n.length!==0&&n.length!==d&&r>=18&&n.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(i.length!==0&&i.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(n.length!==0&&n.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof i<"u"&&typeof n<"u"&&i.length>0&&n.length>d)throw new Error("Resize requires only of scales or sizes to be specified")},On=(e,t,r,i)=>`
|
|
2324
|
+
// The whole part and the fractional part are calculated separately due to inaccuracy of floating
|
|
2325
|
+
// point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an
|
|
2326
|
+
// offset-by-one error later in floor().
|
|
2327
|
+
let big = (${e}) * (${t});
|
|
2328
|
+
let whole = ${i}(big / (${r}));
|
|
2329
|
+
let fract = ${i}(big % (${r})) / ${i}(${r});
|
|
2330
|
+
return whole + fract;
|
|
2331
|
+
`,zd=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32,
|
|
2332
|
+
lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`
|
|
2333
|
+
if (xScale < 1.0 || floor(xScale) != xScale) {
|
|
2334
|
+
return ${t}(xResized) / ${t}(xScale);
|
|
2335
|
+
} else {
|
|
2336
|
+
${On("xResized","lengthOriginal","lengthResized",t)}
|
|
2337
|
+
}
|
|
2338
|
+
`;case"pytorch_half_pixel":return`if (lengthResized > 1) {
|
|
2339
|
+
return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5;
|
|
2340
|
+
} else {
|
|
2341
|
+
return 0.0;
|
|
2342
|
+
}`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) {
|
|
2343
|
+
return 0.0;
|
|
2344
|
+
} else {
|
|
2345
|
+
${On("xResized","lengthOriginal - 1","lengthResized - 1",t)}
|
|
2346
|
+
}`;case"tf_crop_and_resize":return`if (lengthResized > 1) {
|
|
2347
|
+
return ${t}(roiStart) * ${t}(lengthOriginal - 1) +
|
|
2348
|
+
(${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) /
|
|
2349
|
+
${t}(lengthResized - 1);
|
|
2350
|
+
} else {
|
|
2351
|
+
return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1);
|
|
2352
|
+
}`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized);
|
|
2353
|
+
const adjustment = ${t}(lengthResized) / outputWidth;
|
|
2354
|
+
const center = ${t}(lengthOriginal) / 2;
|
|
2355
|
+
const offset = center * (1 - adjustment);
|
|
2356
|
+
return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Ad=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Od=(e,t,r)=>{let i=new Array(r).fill(0).concat(new Array(r).fill(1)),n=e.length===0?i:e.slice();return t.length>0?(t.forEach((a,s)=>{i[a]=n[s],i[s+r]=n[t.length+s]}),i):n},Rd=(e,t,r,i)=>{let n=[];if(r.length>0)if(i.length>0){if(e.forEach(a=>n.push(a)),Math.max(...i)>e.length)throw new Error("axes is out of bound");i.forEach((a,s)=>n[a]=r[s])}else r.forEach(a=>n.push(a));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");n=e.map((a,s)=>Math.round(a*t[s]))}return n},Md=(e,t,r)=>{let i=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(a=>t[a]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(a=>t[a]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let n=e.slice();return r.axes.length>0?(r.axes.forEach(a=>t[a]=i),r.axes.forEach(a=>n[a]=Math.round(e[a]*t[a]))):(t.fill(i,0,t.length),n.forEach((a,s)=>n[s]=Math.round(a*t[s]))),n},Bd=(e,t,r,i,n)=>`
|
|
2357
|
+
fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> {
|
|
2358
|
+
var original_indices: array<${e.type.value}, ${r.length}>;
|
|
2359
|
+
for (var i:u32 = 0; i < ${r.length}; i++) {
|
|
2360
|
+
var output_index = ${e.indicesGet("output_indices","i")};
|
|
2361
|
+
var scale = ${K("uniforms.scales","i",i)};
|
|
2362
|
+
var roi_low = ${K("uniforms.roi","i",n)};
|
|
2363
|
+
var roi_hi = ${K("uniforms.roi",`i + ${t.length}`,n)};
|
|
2364
|
+
if (scale == 1.0) {
|
|
2365
|
+
original_indices[i] = ${e.type.value}(output_index);
|
|
2366
|
+
} else {
|
|
2367
|
+
var input_shape_i = ${K("uniforms.input_shape","i",t.length)};
|
|
2368
|
+
var output_shape_i = ${K("uniforms.output_shape","i",r.length)};
|
|
2369
|
+
original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
|
|
2370
|
+
input_shape_i, roi_low, roi_hi);
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
return original_indices;
|
|
2374
|
+
}`,Nd=(e,t,r,i,n,a,s)=>`
|
|
2375
|
+
fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
|
|
2376
|
+
var input_indices: ${e.type.indices};
|
|
2377
|
+
for (var i:u32 = 0; i < ${i.length}; i++) {
|
|
2378
|
+
var output_index = ${t.indicesGet("output_indices","i")};
|
|
2379
|
+
var input_index: u32;
|
|
2380
|
+
var scale = ${K("uniforms.scales","i",n)};
|
|
2381
|
+
if (scale == 1.0) {
|
|
2382
|
+
input_index = output_index;
|
|
2383
|
+
} else {
|
|
2384
|
+
var roi_low = ${K("uniforms.roi","i",a)};
|
|
2385
|
+
var roi_hi = ${K("uniforms.roi",`i + ${r.length}`,a)};
|
|
2386
|
+
var input_shape_i = ${K("uniforms.input_shape","i",r.length)};
|
|
2387
|
+
var output_shape_i = ${K("uniforms.output_shape","i",i.length)};
|
|
2388
|
+
var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i,
|
|
2389
|
+
input_shape_i, roi_low, roi_hi);
|
|
2390
|
+
if (!${s} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) {
|
|
2391
|
+
if (original_idx < 0) {
|
|
2392
|
+
input_index = 0;
|
|
2393
|
+
} else if (original_idx > ${t.type.value}(input_shape_i - 1)) {
|
|
2394
|
+
input_index = input_shape_i - 1;
|
|
2395
|
+
} else {
|
|
2396
|
+
input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1));
|
|
2397
|
+
}
|
|
2398
|
+
} else {
|
|
2399
|
+
input_index = u32(original_idx);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
${e.indicesSet("input_indices","i","input_index")}
|
|
2403
|
+
}
|
|
2404
|
+
return input_indices;
|
|
2405
|
+
}`,Dd=(e,t)=>`
|
|
2406
|
+
fn checkInputIndices(input_indices: ${e.type.indices}) -> bool {
|
|
2407
|
+
for (var i:u32 = 0; i < ${t.length}; i++) {
|
|
2408
|
+
var input_index = ${e.indicesGet("input_indices","i")};
|
|
2409
|
+
if (input_index < 0 || input_index >= ${K("uniforms.input_shape","i",t.length)}) {
|
|
2410
|
+
return false;
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
return true;
|
|
2414
|
+
}`,Rn=(e,t,r,i)=>e.rank>i?`
|
|
2415
|
+
${e.indicesSet("input_indices",t,"channel")};
|
|
2416
|
+
${e.indicesSet("input_indices",r,"batch")};
|
|
2417
|
+
`:"",Pd=(e,t,r,i,n)=>{let[a,s,u,l]=r.length===2?[-1,0,1,-1]:[0,2,3,1],d=e.type.value;return`
|
|
2418
|
+
fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${d} {
|
|
2419
|
+
var input_indices: ${e.type.indices};
|
|
2420
|
+
${e.indicesSet("input_indices",s,`max(0, min(row, ${r[s]} - 1))`)};
|
|
2421
|
+
${e.indicesSet("input_indices",u,`max(0, min(col, ${r[u]} - 1))`)};
|
|
2422
|
+
${Rn(e,l,a,2)}
|
|
2423
|
+
return ${e.getByIndices("input_indices")};
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${d} {
|
|
2427
|
+
var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
|
|
2428
|
+
var row:${d} = originalIndices[${s}];
|
|
2429
|
+
var col:${d} = originalIndices[${u}];
|
|
2430
|
+
${i?`if (row < 0 || row > (${r[s]} - 1) || col < 0 || col > (${r[u]} - 1)) {
|
|
2431
|
+
return ${n};
|
|
2432
|
+
}`:""};
|
|
2433
|
+
row = max(0, min(row, ${r[s]} - 1));
|
|
2434
|
+
col = max(0, min(col, ${r[u]} - 1));
|
|
2435
|
+
var row1: u32 = u32(row);
|
|
2436
|
+
var col1: u32 = u32(col);
|
|
2437
|
+
var row2: u32 = u32(row + 1);
|
|
2438
|
+
var col2: u32 = u32(col + 1);
|
|
2439
|
+
var channel: u32 = ${r.length>2?`u32(originalIndices[${l}])`:"0"};
|
|
2440
|
+
var batch: u32 = ${r.length>2?`u32(originalIndices[${a}])`:"0"};
|
|
2441
|
+
var x11: ${d} = getInputValue(batch, channel, row1, col1);
|
|
2442
|
+
var x12: ${d} = getInputValue(batch, channel, row1, col2);
|
|
2443
|
+
var x21: ${d} = getInputValue(batch, channel, row2, col1);
|
|
2444
|
+
var x22: ${d} = getInputValue(batch, channel, row2, col2);
|
|
2445
|
+
var dx1: ${d} = abs(row - ${d}(row1));
|
|
2446
|
+
var dx2: ${d} = abs(${d}(row2) - row);
|
|
2447
|
+
var dy1: ${d} = abs(col - ${d}(col1));
|
|
2448
|
+
var dy2: ${d} = abs(${d}(col2) - col);
|
|
2449
|
+
if (row1 == row2) {
|
|
2450
|
+
dx1 = 0.5;
|
|
2451
|
+
dx2 = 0.5;
|
|
2452
|
+
}
|
|
2453
|
+
if (col1 == col2) {
|
|
2454
|
+
dy1 = 0.5;
|
|
2455
|
+
dy2 = 0.5;
|
|
2456
|
+
}
|
|
2457
|
+
return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1);
|
|
2458
|
+
}`},Ud=(e,t,r,i,n,a,s,u,l,d)=>{let f=r.length===2,h=!0,[g,w]=f?[0,1]:h?[2,3]:[1,2],y=e.type.value,v=T=>{let $=T===g?"row":"col";return`
|
|
2459
|
+
fn ${$}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${y} {
|
|
2460
|
+
var output_index = ${t.indicesGet("output_indices",T)};
|
|
2461
|
+
var originalIdx: ${y} = getOriginalCoordinateFromResizedCoordinate(output_index, ${n[T]},
|
|
2462
|
+
${i[T]}, ${r[T]}, ${a[T]}, ${a[T]} + ${r.length});
|
|
2463
|
+
var fractOriginalIdx: ${y} = originalIdx - floor(originalIdx);
|
|
2464
|
+
var coefs = getCubicInterpolationCoefs(fractOriginalIdx);
|
|
2465
|
+
|
|
2466
|
+
if (${u} && (originalIdx < 0 || originalIdx > (${r[T]} - 1))) {
|
|
2467
|
+
return ${l};
|
|
2468
|
+
}
|
|
2469
|
+
var data: array<${y}, 4> = array<${y}, 4>(0.0, 0.0, 0.0, 0.0);
|
|
2470
|
+
for (var i: i32 = -1; i < 3; i++) {
|
|
2471
|
+
var ${$}: ${y} = originalIdx + ${y}(i);
|
|
2472
|
+
if (${$} < 0 || ${$} >= ${r[T]}) {
|
|
2473
|
+
${d?`coefs[i + 1] = 0.0;
|
|
2474
|
+
continue;`:u?`return ${l};`:`${$} = max(0, min(${$}, ${r[T]} - 1));`};
|
|
2475
|
+
}
|
|
2476
|
+
var input_indices_copy: ${e.type.indices} = input_indices;
|
|
2477
|
+
${e.indicesSet("input_indices_copy",T,`u32(${$})`)};
|
|
2478
|
+
data[i + 1] = ${T===g?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"};
|
|
2479
|
+
}
|
|
2480
|
+
return cubicInterpolation1D(data, coefs);
|
|
2481
|
+
}`};return`
|
|
2482
|
+
${v(g)};
|
|
2483
|
+
${v(w)};
|
|
2484
|
+
fn getCubicInterpolationCoefs(s: ${y}) -> array<${y}, 4> {
|
|
2485
|
+
var absS = abs(s);
|
|
2486
|
+
var coeffs: array<${y}, 4> = array<${y}, 4>(0.0, 0.0, 0.0, 0.0);
|
|
2487
|
+
var oneMinusAbsS: ${y} = 1.0 - absS;
|
|
2488
|
+
var twoMinusAbsS: ${y} = 2.0 - absS;
|
|
2489
|
+
var onePlusAbsS: ${y} = 1.0 + absS;
|
|
2490
|
+
coeffs[0] = ((${s} * onePlusAbsS - 5 * ${s}) * onePlusAbsS + 8 * ${s}) * onePlusAbsS - 4 * ${s};
|
|
2491
|
+
coeffs[1] = ((${s} + 2) * absS - (${s} + 3)) * absS * absS + 1;
|
|
2492
|
+
coeffs[2] = ((${s} + 2) * oneMinusAbsS - (${s} + 3)) * oneMinusAbsS * oneMinusAbsS + 1;
|
|
2493
|
+
coeffs[3] = ((${s} * twoMinusAbsS - 5 * ${s}) * twoMinusAbsS + 8 * ${s}) * twoMinusAbsS - 4 * ${s};
|
|
2494
|
+
return coeffs;
|
|
2495
|
+
}
|
|
2496
|
+
|
|
2497
|
+
fn cubicInterpolation1D(x: array<${y}, 4>, coefs: array<${y}, 4>) -> ${y} {
|
|
2498
|
+
var coefsSum: ${y} = coefs[0] + coefs[1] + coefs[2] + coefs[3];
|
|
2499
|
+
return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum;
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${y} {
|
|
2503
|
+
var input_indices: ${e.type.indices} = output_indices;
|
|
2504
|
+
return colCubicInterpolation(input_indices, output_indices);
|
|
2505
|
+
}
|
|
2506
|
+
`},Wd=(e,t,r,i,n)=>{let[a,s,u,l,d]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],f=e.type.value;return`
|
|
2507
|
+
fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${f} {
|
|
2508
|
+
var input_indices: ${e.type.indices};
|
|
2509
|
+
${e.indicesSet("input_indices",s,`max(0, min(depth, ${r[s]} - 1))`)};
|
|
2510
|
+
${e.indicesSet("input_indices",u,`max(0, min(height, ${r[u]} - 1))`)};
|
|
2511
|
+
${e.indicesSet("input_indices",l,`max(0, min(width, ${r[l]} - 1))`)};
|
|
2512
|
+
${Rn(e,d,a,3)}
|
|
2513
|
+
return ${e.getByIndices("input_indices")};
|
|
2514
|
+
}
|
|
2515
|
+
|
|
2516
|
+
fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${f} {
|
|
2517
|
+
var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices);
|
|
2518
|
+
var depth:${f} = originalIndices[${s}];
|
|
2519
|
+
var height:${f} = originalIndices[${u}];
|
|
2520
|
+
var width:${f} = originalIndices[${l}];
|
|
2521
|
+
${i?`if (depth < 0 || depth > (${r[s]} - 1) || height < 0 || height > (${r[u]} - 1) || width < 0 || (width > ${r[l]} - 1)) {
|
|
2522
|
+
return ${n};
|
|
2523
|
+
}`:""};
|
|
2524
|
+
|
|
2525
|
+
depth = max(0, min(depth, ${r[s]} - 1));
|
|
2526
|
+
height = max(0, min(height, ${r[u]} - 1));
|
|
2527
|
+
width = max(0, min(width, ${r[l]} - 1));
|
|
2528
|
+
var depth1: u32 = u32(depth);
|
|
2529
|
+
var height1: u32 = u32(height);
|
|
2530
|
+
var width1: u32 = u32(width);
|
|
2531
|
+
var depth2: u32 = u32(depth + 1);
|
|
2532
|
+
var height2: u32 = u32(height + 1);
|
|
2533
|
+
var width2: u32 = u32(width + 1);
|
|
2534
|
+
var channel: u32 = ${r.length>3?`u32(originalIndices[${d}])`:"0"};
|
|
2535
|
+
var batch: u32 = ${r.length>3?`u32(originalIndices[${a}])`:"0"};
|
|
2536
|
+
|
|
2537
|
+
var x111: ${f} = getInputValue(batch, channel, depth1, height1, width1);
|
|
2538
|
+
var x112: ${f} = getInputValue(batch, channel, depth1, height1, width2);
|
|
2539
|
+
var x121: ${f} = getInputValue(batch, channel, depth1, height2, width1);
|
|
2540
|
+
var x122: ${f} = getInputValue(batch, channel, depth1, height2, width2);
|
|
2541
|
+
var x211: ${f} = getInputValue(batch, channel, depth2, height1, width1);
|
|
2542
|
+
var x212: ${f} = getInputValue(batch, channel, depth2, height1, width2);
|
|
2543
|
+
var x221: ${f} = getInputValue(batch, channel, depth2, height2, width1);
|
|
2544
|
+
var x222: ${f} = getInputValue(batch, channel, depth2, height2, width2);
|
|
2545
|
+
var dx1: ${f} = abs(depth - ${f}(depth1));
|
|
2546
|
+
var dx2: ${f} = abs(${f}(depth2) - depth);
|
|
2547
|
+
var dy1: ${f} = abs(height - ${f}(height1));
|
|
2548
|
+
var dy2: ${f} = abs(${f}(height2) - height);
|
|
2549
|
+
var dz1: ${f} = abs(width - ${f}(width1));
|
|
2550
|
+
var dz2: ${f} = abs(${f}(width2) - width);
|
|
2551
|
+
if (depth1 == depth2) {
|
|
2552
|
+
dx1 = 0.5;
|
|
2553
|
+
dx2 = 0.5;
|
|
2554
|
+
}
|
|
2555
|
+
if (height1 == height2) {
|
|
2556
|
+
dy1 = 0.5;
|
|
2557
|
+
dy2 = 0.5;
|
|
2558
|
+
}
|
|
2559
|
+
if (width1 == width2) {
|
|
2560
|
+
dz1 = 0.5;
|
|
2561
|
+
dz2 = 0.5;
|
|
2562
|
+
}
|
|
2563
|
+
return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 +
|
|
2564
|
+
x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1);
|
|
2565
|
+
}`},Ld=(e,t,r,i,n,a)=>{let s=e.dims,u=Od(a,t.axes,s.length),l=Rd(s,i,n,t.axes),d=i.slice();i.length===0&&(d=s.map((_,I)=>_===0?1:l[I]/_),t.keepAspectRatioPolicy!=="stretch"&&(l=Md(s,d,t)));let f=F("output",e.dataType,l.length),h=B("input",e.dataType,s.length),g=R.size(l),w=s.length===l.length&&s.every((_,I)=>_===l[I]),y=t.coordinateTransformMode==="tf_crop_and_resize",v=t.extrapolationValue,T=h.type.value,$=_=>`
|
|
2566
|
+
${w?"":`
|
|
2567
|
+
${zd(t.coordinateTransformMode,T)};
|
|
2568
|
+
${(()=>{switch(t.mode){case"nearest":return`
|
|
2569
|
+
${Dd(h,s)};
|
|
2570
|
+
${Ad(t.nearestMode,r,T)};
|
|
2571
|
+
${Nd(h,f,s,l,d.length,u.length,y)};
|
|
2572
|
+
`;case"linear":return`
|
|
2573
|
+
${Bd(f,s,l,d.length,u.length)};
|
|
2574
|
+
${(()=>{if(s.length===2||s.length===4)return`${Pd(h,f,s,y,v)}`;if(s.length===3||s.length===5)return`${Wd(h,f,s,y,v)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()};
|
|
2575
|
+
`;case"cubic":return`
|
|
2576
|
+
${(()=>{if(s.length===2||s.length===4)return`${Ud(h,f,s,l,d,u,t.cubicCoeffA,y,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()};
|
|
2577
|
+
`;default:throw Error("Invalid resize mode")}})()};
|
|
2578
|
+
`}
|
|
2579
|
+
${_.registerUniform("output_size","u32").registerUniform("scales","f32",d.length).registerUniform("roi","f32",u.length).declareVariables(h,f)}
|
|
2580
|
+
${_.mainStart()}
|
|
2581
|
+
${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
2582
|
+
${w?"output[global_idx] = input[global_idx];":`
|
|
2583
|
+
let output_indices = ${f.offsetToIndices("global_idx")};
|
|
2584
|
+
var input_indices: ${h.type.indices};
|
|
2585
|
+
${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices);
|
|
2586
|
+
if (checkInputIndices(input_indices)) {
|
|
2587
|
+
output[global_idx] = ${h.getByIndices("input_indices")};
|
|
2588
|
+
} else {
|
|
2589
|
+
output[global_idx] = ${t.extrapolationValue};
|
|
2590
|
+
}`;case"linear":return`output[global_idx] = ${s.length===2||s.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()};
|
|
2591
|
+
`}
|
|
2592
|
+
}`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${d.length>0?t.mode==="cubic"?d:d.length:""}|${n.length>0?n:""}|${u.length>0?u:""}|${w}|${t.mode==="nearest"?s.length:s}`,inputDependencies:["rank"]},getShaderSource:$,getRunData:()=>({outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},{type:1,data:d},{type:1,data:u},...Z(s,l)]})}},qd=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},bf=(e,t)=>{let r=[],i=[],n=[],a=qd(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Cd(e.inputs,t,a,r,i,n),e.compute(Ld(e.inputs[0],t,a,r,i,n),{inputs:[0]})},vf=e=>{let t=e.antialias,r=e.axes,i=e.coordinateTransformMode,n=e.cubicCoeffA,a=e.excludeOutside!==0,s=e.extrapolationValue,u=e.keepAspectRatioPolicy,l=e.mode,d=e.nearestMode===""?"simple":e.nearestMode;return me({antialias:t,axes:r,coordinateTransformMode:i,cubicCoeffA:n,excludeOutside:a,extrapolationValue:s,keepAspectRatioPolicy:u,mode:l,nearestMode:d})}}),M0=P(()=>{"use strict";ee(),ie(),ne(),Vd=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],i=e[2];if(t.dataType!==r.dataType||t.dataType!==i.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let n=t.dims[t.dims.length-1],a=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==n)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==a)throw new Error("Skip must have the same sequence length as input");if(i.dims.length!==1)throw new Error("Gamma must be 1D");if(i.dims[i.dims.length-1]!==n)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let s=e[3];if(s.dims.length!==1)throw new Error("Beta must be 1D");if(s.dims[s.dims.length-1]!==n)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let s=e[4];if(s.dims.length!==1)throw new Error("Bias must be 1D");if(s.dims[s.dims.length-1]!==n)throw new Error("Bias must have the same hidden size as input")}},Gd=(e,t,r,i)=>{let n=t.simplified,a=e[0].dims,s=R.size(a),u=a,l=s,d=a.slice(-1)[0],f=i?a.slice(0,-1).concat(1):[],h=!n&&e.length>3,g=e.length>4,w=i&&r>1,y=i&&r>2,v=r>3,T=64,$=xe(d),_=[{type:12,data:l},{type:12,data:$},{type:12,data:d},{type:1,data:t.epsilon}],I=E=>{let C=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],O=[B("x",e[0].dataType,e[0].dims,$),B("skip",e[1].dataType,e[1].dims,$),B("gamma",e[2].dataType,e[2].dims,$)];h&&O.push(B("beta",e[3].dataType,e[3].dims,$)),g&&O.push(B("bias",e[4].dataType,e[4].dims,$)),O.push(F("output",e[0].dataType,u,$)),w&&O.push(F("mean_output",1,f)),y&&O.push(F("inv_std_output",1,f)),v&&O.push(F("input_skip_bias_sum",e[0].dataType,u,$));let x=Ee(e[0].dataType),D=Ee(1,$);return`
|
|
2593
|
+
|
|
2594
|
+
${E.registerUniforms(C).declareVariables(...O)}
|
|
2595
|
+
var<workgroup> sum_shared : array<${D}, ${T}>;
|
|
2596
|
+
var<workgroup> sum_squared_shared : array<${D}, ${T}>;
|
|
2597
|
+
|
|
2598
|
+
${E.mainStart([T,1,1])}
|
|
2599
|
+
let ix = local_id.x;
|
|
2600
|
+
let iy = global_id.x / ${T};
|
|
2601
|
+
|
|
2602
|
+
let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components;
|
|
2603
|
+
var stride = hidden_size_vectorized / ${T};
|
|
2604
|
+
let offset = ix * stride + iy * hidden_size_vectorized;
|
|
2605
|
+
let offset1d = stride * ix;
|
|
2606
|
+
if (ix == ${T-1}) {
|
|
2607
|
+
stride = hidden_size_vectorized - stride * ix;
|
|
2608
|
+
}
|
|
2609
|
+
for (var i: u32 = 0; i < stride; i++) {
|
|
2610
|
+
let skip_value = skip[offset + i];
|
|
2611
|
+
let bias_value = ${g?"bias[offset1d + i]":x+"(0.0)"};
|
|
2612
|
+
let input_value = x[offset + i];
|
|
2613
|
+
let value = input_value + skip_value + bias_value;
|
|
2614
|
+
${v?"input_skip_bias_sum[offset + i] = value;":""}
|
|
2615
|
+
output[offset + i] = value;
|
|
2616
|
+
let f32_value = ${jt(x,$,"value")};
|
|
2617
|
+
sum_shared[ix] += f32_value;
|
|
2618
|
+
sum_squared_shared[ix] += f32_value * f32_value;
|
|
2619
|
+
}
|
|
2620
|
+
workgroupBarrier();
|
|
2621
|
+
|
|
2622
|
+
var reduce_size : u32 = ${T};
|
|
2623
|
+
for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) {
|
|
2624
|
+
reduce_size = curr_size + (reduce_size & 1);
|
|
2625
|
+
if (ix < curr_size) {
|
|
2626
|
+
sum_shared[ix] += sum_shared[ix + reduce_size];
|
|
2627
|
+
sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size];
|
|
2628
|
+
}
|
|
2629
|
+
workgroupBarrier();
|
|
2630
|
+
}
|
|
2631
|
+
|
|
2632
|
+
let sum = sum_shared[0];
|
|
2633
|
+
let square_sum = sum_squared_shared[0];
|
|
2634
|
+
let mean = ${vt("sum",$)} / f32(uniforms.hidden_size);
|
|
2635
|
+
let inv_std_dev = inverseSqrt(${vt("square_sum",$)} / f32(uniforms.hidden_size) ${n?"":"- mean * mean"} + uniforms.epsilon);
|
|
2636
|
+
${w?"mean_output[global_idx] = mean;":""}
|
|
2637
|
+
${y?"inv_std_output[global_idx] = inv_std_dev;":""}
|
|
2638
|
+
|
|
2639
|
+
for (var i: u32 = 0; i < stride; i++) {
|
|
2640
|
+
output[offset + i] = (output[offset + i] ${n?"":`- ${x}(mean)`}) *
|
|
2641
|
+
${x}(inv_std_dev) * gamma[offset1d + i]
|
|
2642
|
+
${h?"+ beta[offset1d + i]":""};
|
|
2643
|
+
}
|
|
2644
|
+
}`},k=[{dims:u,dataType:e[0].dataType}];return r>1&&k.push({dims:f,dataType:1}),r>2&&k.push({dims:f,dataType:1}),r>3&&k.push({dims:a,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${$};${w};${y};${v}`,inputDependencies:e.map((E,C)=>"type")},getShaderSource:I,getRunData:()=>({outputs:k,dispatchGroup:{x:Math.ceil(l/d)},programUniforms:_})}},$f=(e,t)=>{Vd(e.inputs);let r=[0];e.outputCount>1&&r.push(-3),e.outputCount>2&&r.push(-3),e.outputCount>3&&r.push(3),e.compute(Gd(e.inputs,t,e.outputCount,!1),{outputs:r})}}),B0=P(()=>{"use strict";ee(),ie(),Te(),ne(),Fd=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,i)=>{if(e[i+1].dataType!==6&&e[i+1].dataType!==7)throw new Error(`Input ${i} must be an array of int32 or int64`)})},fr=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(i=>r.push(Number(i)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(i=>r.push(Number(i)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},Hd=(e,t)=>{if(e.length>1){let r=fr(e,1),i=fr(e,2),n=fr(e,3);return n.length===0&&(n=[...Array(e[0].dims.length).keys()]),me({starts:r,ends:i,axes:n})}else return t},Mn=(e,t,r,i,n)=>{let a=e;return e<0&&(a+=r[i[t]]),n[t]<0?Math.max(0,Math.min(a,r[i[t]]-1)):Math.max(0,Math.min(a,r[i[t]]))},Kd=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} {
|
|
2645
|
+
var input_indices: ${e.type.indices};
|
|
2646
|
+
var carry = 0u;
|
|
2647
|
+
for (var i = ${r.length-1}; i >= 0; i--) {
|
|
2648
|
+
let input_shape_i = ${K("uniforms.input_shape","i",r.length)};
|
|
2649
|
+
let steps_i = ${K("uniforms.steps","i",r.length)};
|
|
2650
|
+
let signs_i = ${K("uniforms.signs","i",r.length)};
|
|
2651
|
+
let starts_i = ${K("uniforms.starts","i",r.length)};
|
|
2652
|
+
var output_index = ${t.indicesGet("output_indices","i")};
|
|
2653
|
+
var input_index = output_index * steps_i + starts_i + carry;
|
|
2654
|
+
carry = input_index / input_shape_i;
|
|
2655
|
+
input_index = input_index % input_shape_i;
|
|
2656
|
+
if (signs_i < 0) {
|
|
2657
|
+
input_index = input_shape_i - input_index - 1u + starts_i;
|
|
2658
|
+
}
|
|
2659
|
+
${e.indicesSet("input_indices","i","input_index")};
|
|
2660
|
+
}
|
|
2661
|
+
return input_indices;
|
|
2662
|
+
}`,jd=(e,t)=>{let r=e[0].dims,i=R.size(r),n=t.axes.length>0?R.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],a=fr(e,4);a.forEach($=>$!==0||(()=>{throw new Error("step cannot be 0")})),a.length===0&&(a=Array(n.length).fill(1));let s=t.starts.map(($,_)=>Mn($,_,r,n,a)),u=t.ends.map(($,_)=>Mn($,_,r,n,a));if(n.length!==s.length||n.length!==u.length)throw new Error("start, ends and axes should have the same number of elements");if(n.length!==r.length)for(let $=0;$<r.length;++$)n.includes($)||(s.splice($,0,0),u.splice($,0,r[$]),a.splice($,0,1));let l=a.map($=>Math.sign($));a.forEach(($,_,I)=>{if($<0){let k=(u[_]-s[_])/$,E=s[_],C=E+k*a[_];s[_]=C,u[_]=E,I[_]=-$}});let d=r.slice(0);n.forEach(($,_)=>{d[$]=Math.ceil((u[$]-s[$])/a[$])});let f={dims:d,dataType:e[0].dataType},h=F("output",e[0].dataType,d.length),g=B("input",e[0].dataType,e[0].dims.length),w=R.size(d),y=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:s.length},{name:"signs",type:"i32",length:l.length},{name:"steps",type:"u32",length:a.length}],v=[{type:12,data:w},{type:12,data:s},{type:6,data:l},{type:12,data:a},...Z(e[0].dims,d)],T=$=>`
|
|
2663
|
+
${$.registerUniforms(y).declareVariables(g,h)}
|
|
2664
|
+
${Kd(g,h,r)}
|
|
2665
|
+
${$.mainStart()}
|
|
2666
|
+
${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")}
|
|
2667
|
+
let output_indices = ${h.offsetToIndices("global_idx")};
|
|
2668
|
+
let input_indices = calculateInputIndices(output_indices);
|
|
2669
|
+
${h.setByOffset("global_idx",g.getByIndices("input_indices"))}
|
|
2670
|
+
}`;return{name:"Slice",shaderCache:{hint:`${l.length}_${s.length}_${a.length}`,inputDependencies:["rank"]},getShaderSource:T,getRunData:()=>({outputs:[f],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:v})}},xf=(e,t)=>{Fd(e.inputs,t);let r=Hd(e.inputs,t);e.compute(jd(e.inputs,r),{inputs:[0]})},Tf=e=>{let t=e.starts,r=e.ends,i=e.axes;return me({starts:t,ends:r,axes:i})}}),N0=P(()=>{"use strict";ee(),ie(),Te(),$t(),ne(),Qd=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Zd=(e,t)=>{let r=e.inputs[0],i=r.dims,n=R.size(i),a=i.length,s=R.normalizeAxis(t.axis,a),u=s<i.length-1,l,d=[];u?(d=Array.from({length:a},(O,x)=>x),d[s]=a-1,d[a-1]=s,l=e.compute(De(r,d),{inputs:[r],outputs:[-1]})[0]):l=r;let f=l.dims,h=f[a-1],g=n/h,w=xe(h),y=h/w,v=64;g===1&&(v=256);let T=(O,x)=>x===4?`max(max(${O}.x, ${O}.y), max(${O}.z, ${O}.w))`:x===2?`max(${O}.x, ${O}.y)`:x===3?`max(max(${O}.x, ${O}.y), ${O}.z)`:O,$=B("x",l.dataType,l.dims,w),_=F("result",l.dataType,l.dims,w),I=$.type.value,k=Ee(l.dataType)==="f32"?`var threadMax = ${I}(-3.4028234663852886e+38f);`:`var threadMax = ${I}(-65504.0h);`,E=O=>`
|
|
2671
|
+
var<workgroup> rowMaxShared : ${I};
|
|
2672
|
+
var<workgroup> rowSumShared : ${I};
|
|
2673
|
+
var<workgroup> threadShared : array<${I}, ${v}>;
|
|
2674
|
+
|
|
2675
|
+
fn getValue(row: i32, col: i32, row_stride: i32) -> ${I} {
|
|
2676
|
+
let index = row * row_stride + col;
|
|
2677
|
+
return x[index];
|
|
2678
|
+
}
|
|
2679
|
+
|
|
2680
|
+
fn setValue(row: i32, col: i32, row_stride: i32, value: ${I}) {
|
|
2681
|
+
let index = row * row_stride + col;
|
|
2682
|
+
result[index] = value;
|
|
2683
|
+
}
|
|
2684
|
+
${O.registerUniform("packedCols","i32").declareVariables($,_)}
|
|
2685
|
+
${O.mainStart(v)}
|
|
2686
|
+
let gindex = i32(global_idx);
|
|
2687
|
+
let lindex = i32(local_idx);
|
|
2688
|
+
const wg = ${v};
|
|
2689
|
+
let row = gindex / wg;
|
|
2690
|
+
let cols = uniforms.packedCols;
|
|
2691
|
+
let row_stride : i32 = uniforms.packedCols;
|
|
2692
|
+
|
|
2693
|
+
// find the rows max
|
|
2694
|
+
${k}
|
|
2695
|
+
for (var col = lindex; col < cols; col += wg) {
|
|
2696
|
+
let value = getValue(row, col, row_stride);
|
|
2697
|
+
threadMax = max(threadMax, value);
|
|
2698
|
+
}
|
|
2699
|
+
if (lindex < cols) {
|
|
2700
|
+
threadShared[lindex] = threadMax;
|
|
2701
|
+
}
|
|
2702
|
+
workgroupBarrier();
|
|
2703
|
+
|
|
2704
|
+
var reduceSize = min(cols, wg);
|
|
2705
|
+
for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) {
|
|
2706
|
+
reduceSize = currSize + (reduceSize & 1);
|
|
2707
|
+
if (lindex < currSize) {
|
|
2708
|
+
threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]);
|
|
2709
|
+
}
|
|
2710
|
+
workgroupBarrier();
|
|
2711
|
+
}
|
|
2712
|
+
if (lindex == 0) {
|
|
2713
|
+
rowMaxShared = ${I}(${T("threadShared[0]",w)});
|
|
2714
|
+
}
|
|
2715
|
+
workgroupBarrier();
|
|
2716
|
+
|
|
2717
|
+
// find the rows sum
|
|
2718
|
+
var threadSum = ${I}(0.0);
|
|
2719
|
+
for (var col = lindex; col < cols; col += wg) {
|
|
2720
|
+
let subExp = exp(getValue(row, col, row_stride) - rowMaxShared);
|
|
2721
|
+
threadSum += subExp;
|
|
2722
|
+
}
|
|
2723
|
+
threadShared[lindex] = threadSum;
|
|
2724
|
+
workgroupBarrier();
|
|
2725
|
+
|
|
2726
|
+
for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) {
|
|
2727
|
+
if (lindex < currSize) {
|
|
2728
|
+
threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize];
|
|
2729
|
+
}
|
|
2730
|
+
workgroupBarrier();
|
|
2731
|
+
}
|
|
2732
|
+
if (lindex == 0) {
|
|
2733
|
+
rowSumShared = ${I}(${vt("threadShared[0]",w)});
|
|
2734
|
+
}
|
|
2735
|
+
workgroupBarrier();
|
|
2736
|
+
|
|
2737
|
+
// calculate final value for each element in the row
|
|
2738
|
+
for (var col = lindex; col < cols; col += wg) {
|
|
2739
|
+
var value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared;
|
|
2740
|
+
// max operation protects against NaN since all values should be >=0
|
|
2741
|
+
value = max(value, ${I}(0.0));
|
|
2742
|
+
setValue(row, col, row_stride, value);
|
|
2743
|
+
}
|
|
2744
|
+
}`,C=e.compute({name:"Softmax",shaderCache:{hint:`${w};${v}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:f,dataType:l.dataType}],dispatchGroup:{x:g},programUniforms:[{type:6,data:y}]}),getShaderSource:E},{inputs:[l],outputs:[u?-1:0]})[0];u&&e.compute(De(C,d),{inputs:[C]})},Sf=(e,t)=>{Qd(e.inputs),Zd(e,t)},kf=e=>me({axis:e.axis})}),D0=P(()=>{"use strict";ee(),ie(),ne(),Bn=e=>Array.from(e.getBigInt64Array(),Number),Yd=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Bn(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Xd=(e,t)=>{let r=[];for(let i=0;i<e.length;++i)r.push(e[i]*t[i]);return r},Jd=(e,t)=>{let r=e[0].dims,i=t??Bn(e[1]),n=Xd(r,i),a=R.size(n),s=e[0].dataType,u=B("input",s,r.length),l=F("output",s,n.length),d=f=>`
|
|
2745
|
+
const inputShape = ${u.indices(...r)};
|
|
2746
|
+
${f.registerUniform("output_size","u32").declareVariables(u,l)}
|
|
2747
|
+
${f.mainStart()}
|
|
2748
|
+
${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}
|
|
2749
|
+
let output_indices = ${l.offsetToIndices("global_idx")};
|
|
2750
|
+
var input_indices: ${u.type.indices};
|
|
2751
|
+
for (var i = 0; i < ${r.length}; i++) {
|
|
2752
|
+
let input_dim_i = ${u.indicesGet("uniforms.input_shape","i")};
|
|
2753
|
+
let input_dim_value = ${l.indicesGet("output_indices","i")} % input_dim_i;
|
|
2754
|
+
|
|
2755
|
+
${u.indicesSet("input_indices","i","input_dim_value")}
|
|
2756
|
+
}
|
|
2757
|
+
${l.setByOffset("global_idx",u.getByIndices("input_indices"))}
|
|
2758
|
+
}`;return{name:"Tile",shaderCache:{hint:`${i}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:[{type:12,data:a},...Z(e[0].dims,n)]}),getShaderSource:d}},If=e=>{Yd(e.inputs),e.compute(Jd(e.inputs),{inputs:[0]})}}),P0=P(()=>{"use strict";ee(),ie(),ne(),ep=(e,t,r,i,n)=>{let a=F("output_data",n,r.length,4),s=B("a_data",t[1].dataType,t[1].dims.length,4),u=B("b_data",t[2].dataType,t[2].dims.length,4),l=B("c_data",t[0].dataType,t[0].dims.length,4),d,f=(h,g,w)=>`select(${g}, ${h}, ${w})`;if(!i)d=a.setByOffset("global_idx",f(s.getByOffset("global_idx"),u.getByOffset("global_idx"),l.getByOffset("global_idx")));else{let h=(g,w,y="")=>{let v=`a_data[index_a${w}][component_a${w}]`,T=`b_data[index_b${w}][component_b${w}]`,$=`bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`;return`
|
|
2759
|
+
let output_indices${w} = ${a.offsetToIndices(`global_idx * 4u + ${w}u`)};
|
|
2760
|
+
let offset_a${w} = ${s.broadcastedIndicesToOffset(`output_indices${w}`,a)};
|
|
2761
|
+
let offset_b${w} = ${u.broadcastedIndicesToOffset(`output_indices${w}`,a)};
|
|
2762
|
+
let offset_c${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`,a)};
|
|
2763
|
+
let index_a${w} = offset_a${w} / 4u;
|
|
2764
|
+
let index_b${w} = offset_b${w} / 4u;
|
|
2765
|
+
let index_c${w} = offset_c${w} / 4u;
|
|
2766
|
+
let component_a${w} = offset_a${w} % 4u;
|
|
2767
|
+
let component_b${w} = offset_b${w} % 4u;
|
|
2768
|
+
let component_c${w} = offset_c${w} % 4u;
|
|
2769
|
+
${g}[${w}] = ${y}(${f(v,T,$)});
|
|
2770
|
+
`};n===9?d=`
|
|
2771
|
+
var data = vec4<u32>(0);
|
|
2772
|
+
${h("data",0,"u32")}
|
|
2773
|
+
${h("data",1,"u32")}
|
|
2774
|
+
${h("data",2,"u32")}
|
|
2775
|
+
${h("data",3,"u32")}
|
|
2776
|
+
output_data[global_idx] = dot(vec4<u32>(0x1, 0x100, 0x10000, 0x1000000), vec4<u32>(data));`:d=`
|
|
2777
|
+
${h("output_data[global_idx]",0)}
|
|
2778
|
+
${h("output_data[global_idx]",1)}
|
|
2779
|
+
${h("output_data[global_idx]",2)}
|
|
2780
|
+
${h("output_data[global_idx]",3)}
|
|
2781
|
+
`}return`
|
|
2782
|
+
${e.registerUniform("vec_size","u32").declareVariables(l,s,u,a)}
|
|
2783
|
+
${e.mainStart()}
|
|
2784
|
+
${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")}
|
|
2785
|
+
${d}
|
|
2786
|
+
}`},tp=e=>{let t=e[1].dims,r=e[2].dims,i=e[0].dims,n=e[1].dataType,a=!(R.areEqual(t,r)&&R.areEqual(r,i)),s=t,u=R.size(t);if(a){let d=Qt.calcShape(Qt.calcShape(t,r,!1),i,!1);if(!d)throw new Error("Can't perform where op on the given tensors");s=d,u=R.size(s)}let l=Math.ceil(u/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:d=>ep(d,e,s,a,n),getRunData:()=>({outputs:[{dims:s,dataType:n}],dispatchGroup:{x:Math.ceil(u/64/4)},programUniforms:[{type:12,data:l},...Z(i,t,r,s)]})}},Ef=e=>{e.compute(tp(e.inputs))}}),U0=P(()=>{"use strict";Jy(),ya(),e0(),t0(),r0(),i0(),n0(),l0(),p0(),c0(),h0(),f0(),m0(),g0(),y0(),w0(),_0(),b0(),v0(),$0(),x0(),T0(),S0(),k0(),I0(),Kh(),E0(),C0(),z0(),A0(),O0(),ga(),R0(),Xh(),M0(),B0(),N0(),Zh(),D0(),$t(),wa(),P0(),Cf=new Map([["Abs",[vc]],["Acos",[$c]],["Acosh",[xc]],["Add",[ih]],["ArgMax",[yc,Hn]],["ArgMin",[gc,Hn]],["Asin",[Tc]],["Asinh",[Sc]],["Atan",[kc]],["Atanh",[Ic]],["Attention",[wc]],["AveragePool",[uf,of]],["BatchNormalization",[_c]],["BiasAdd",[bc]],["BiasSplitGelu",[rh]],["Cast",[Cc,Ec]],["Ceil",[Ac]],["Clip",[zc]],["Concat",[hh,fh]],["Conv",[Xn,Yn]],["ConvTranspose",[Th,xh]],["Cos",[Oc]],["Cosh",[Rc]],["CumSum",[Sh,kh]],["DepthToSpace",[Ih,Eh]],["DequantizeLinear",[mf,gf]],["Div",[nh]],["Einsum",[Ch,zh]],["Elu",[Mc,_r]],["Equal",[ah]],["Erf",[Bc]],["Exp",[Nc]],["Expand",[Ah]],["FastGelu",[Oh]],["Floor",[Dc]],["FusedConv",[Xn,Yn]],["Gather",[Mh,Rh]],["GatherElements",[Wh,Uh]],["GatherBlockQuantized",[Dh,Ph]],["GatherND",[Bh,Nh]],["Gelu",[Pc]],["Gemm",[qh,Lh]],["GlobalAveragePool",[df,lf]],["GlobalMaxPool",[ff,hf]],["Greater",[lh]],["GreaterOrEqual",[ph]],["GridSample",[Vh,Gh]],["GroupQueryAttention",[Jh]],["HardSigmoid",[Hc,Fc]],["InstanceNormalization",[ef]],["LayerNormalization",[tf]],["LeakyRelu",[Uc,_r]],["Less",[dh]],["LessOrEqual",[ch]],["Log",[eh]],["MatMul",[rf]],["MatMulNBits",[nf,af]],["MaxPool",[pf,cf]],["Mul",[sh]],["MultiHeadAttention",[Hh,Fh]],["Neg",[Lc]],["Not",[Wc]],["Pad",[sf]],["Pow",[oh]],["QuickGelu",[th,_r]],["Range",[yf]],["Reciprocal",[qc]],["ReduceMin",[pc]],["ReduceMean",[sc]],["ReduceMax",[dc]],["ReduceSum",[hc]],["ReduceProd",[cc]],["ReduceL1",[oc]],["ReduceL2",[uc]],["ReduceLogSum",[mc]],["ReduceLogSumExp",[lc]],["ReduceSumSquare",[fc]],["Relu",[Vc]],["Resize",[bf,vf]],["RotaryEmbedding",[Yh]],["ScatterND",[_f,wf]],["Sigmoid",[Gc]],["Sin",[Kc]],["Sinh",[jc]],["Slice",[xf,Tf]],["SkipLayerNormalization",[$f]],["Split",[jh,Qh]],["Sqrt",[Qc]],["Softmax",[Sf,kf]],["Sub",[uh]],["Tan",[Zc]],["Tanh",[Yc]],["ThresholdedRelu",[Jc,_r]],["Tile",[If]],["Transpose",[jp,Qp]],["Where",[Ef]]])}),W0=P(()=>{"use strict";Le(),lt(),ne(),zf=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,r,i,n){Je(e.programInfo.name);let a=this.backend.device,s=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let u=[];for(let d of t)u.push({binding:u.length,resource:{buffer:d.buffer}});for(let d of r)u.push({binding:u.length,resource:{buffer:d.buffer}});n&&u.push({binding:u.length,resource:n});let l=a.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:u,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let d={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:l,dispatchGroup:i};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(d)}s.setPipeline(e.computePipeline),s.setBindGroup(0,l),s.dispatchWorkgroups(...i),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),We(e.programInfo.name)}dispose(){}build(e,t){Je(e.name);let r=this.backend.device,i=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(d=>{r.features.has(d.feature)&&i.push(`enable ${d.extension};`)});let n=Kp(t,this.backend.device.limits),a=e.getShaderSource(n),s=`${i.join(`
|
|
2787
|
+
`)}
|
|
2788
|
+
${n.additionalImplementations}
|
|
2789
|
+
${a}`,u=r.createShaderModule({code:s,label:e.name});de("verbose",()=>`[WebGPU] ${e.name} shader code: ${s}`);let l=r.createComputePipeline({compute:{module:u,entryPoint:"main"},layout:"auto",label:e.name});return We(e.name),{programInfo:e,computePipeline:l,uniformVariablesInfo:n.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,r=typeof e=="number"?1:e.y||1,i=typeof e=="number"?1:e.z||1,n=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=n&&r<=n&&i<=n)return[t,r,i];let a=t*r*i,s=Math.ceil(Math.sqrt(a));if(s>n){if(s=Math.ceil(Math.cbrt(a)),s>n)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[s,s,s]}else return[s,s,1]}}}),Af={};Yt(Af,{WebGpuBackend:()=>Of});L0=P(()=>{"use strict";Le(),ee(),lt(),qp(),Yy(),U0(),W0(),rp=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let i=0;i<e.length;++i){let n=e[i].dataType;switch(t[i]){case"none":{r.push("");break}case"type":{r.push(`${n}`);break}case"rank":{let a=e[i].dims.length;r.push(`${n};${a}`);break}case"dims":{let a=e[i].dims.join(",");r.push(`${n};${a}`);break}default:throw new Error(`unsupported input dependency: ${t[i]}`)}}return r.join("|")},ip=(e,t,r)=>{let i=e.name;return e.shaderCache?.hint&&(i+="["+e.shaderCache.hint+"]"),i+=":"+r+`:${rp(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,i},np=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},Of=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let r=[],i={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:r},n=a=>t.features.has(a)&&r.push(a)&&!0;n("chromium-experimental-timestamp-query-inside-passes")||n("timestamp-query"),n("shader-f16"),n("subgroups"),this.device=await t.requestDevice(i),this.adapterInfo=new np(t.info||await t.requestAdapterInfo()),this.gpuDataManager=Fp(this),this.programManager=new zf(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,ca(e.logLevel,!!e.debug),this.device.onuncapturederror=a=>{a.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${a.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Je(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{let t=new BigUint64Array(e.getMappedRange()),r=this.pendingQueries.get(e);for(let i=0;i<t.length/2;i++){let n=r[i],a=n.kernelId,s=this.kernels.get(a),u=s.kernelType,l=s.kernelName,d=n.programName,f=n.inputTensorViews,h=n.outputTensorViews,g=t[i*2],w=t[i*2+1];typeof this.queryTimeBase>"u"&&(this.queryTimeBase=g);let y=Number(g-this.queryTimeBase),v=Number(w-this.queryTimeBase);if(!Number.isSafeInteger(y)||!Number.isSafeInteger(v))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:f.map(T=>({dims:T.dims,dataType:ut(T.dataType)})),outputsMetadata:h.map(T=>({dims:T.dims,dataType:ut(T.dataType)})),kernelId:a,kernelType:u,kernelName:l,programName:d,startTime:y,endTime:v});else{let T="";f.forEach((_,I)=>{T+=`input[${I}]: [${_.dims}] | ${ut(_.dataType)}, `});let $="";h.forEach((_,I)=>{$+=`output[${I}]: [${_.dims}] | ${ut(_.dataType)}, `}),console.log(`[profiling] kernel "${a}|${u}|${l}|${d}" ${T}${$}start time: ${y} ns, execution time: ${v-y} ns`)}xr("GPU",`${d}::${g}::${w}`)}e.unmap(),this.pendingQueries.delete(e)}),We()}run(e,t,r,i,n,a){Je(e.name);let s=[];for(let _=0;_<t.length;++_){let I=t[_].data;if(I===0)continue;let k=this.gpuDataManager.get(I);if(!k)throw new Error(`no GPU data for input: ${I}`);s.push(k)}let{outputs:u,dispatchGroup:l,programUniforms:d}=e.getRunData(t),f=r.length===0?u.map((_,I)=>I):r;if(f.length!==u.length)throw new Error(`Output size ${f.length} must be equal to ${u.length}.`);let h=[],g=[];for(let _=0;_<u.length;++_){if(!Number.isInteger(f[_])||f[_]<-3||f[_]>=a)throw new Error(`Invalid output index: ${f[_]}`);if(f[_]===-3)continue;let I=f[_]===-1,k=f[_]===-2,E=I||k?n(u[_].dataType,u[_].dims):i(f[_],u[_].dataType,u[_].dims);if(h.push(E),E.data===0)continue;let C=this.gpuDataManager.get(E.data);if(!C)throw new Error(`no GPU data for output: ${E.data}`);if(I&&this.temporaryData.push(C),k){let O=this.kernelPersistentData.get(this.currentKernelId);O||(O=[],this.kernelPersistentData.set(this.currentKernelId,O)),O.push(C)}g.push(C)}if(s.length!==t.length||g.length!==h.length){if(g.length===0)return We(e.name),h;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let w;if(d){let _=0,I=[];d.forEach(O=>{let x=typeof O.data=="number"?[O.data]:O.data;if(x.length===0)return;let D=O.type===10?2:4,L,Y;O.type===10?(Y=x.length>4?16:x.length>2?8:x.length*D,L=x.length>4?16:D*x.length):(Y=x.length<=2?x.length*D:16,L=16),_=Math.ceil(_/Y)*Y,I.push(_);let q=O.type===10?8:4;_+=x.length>4?Math.ceil(x.length/q)*L:x.length*D});let k=16;_=Math.ceil(_/k)*k;let E=new ArrayBuffer(_);d.forEach((O,x)=>{let D=I[x],L=typeof O.data=="number"?[O.data]:O.data;if(O.type===6)new Int32Array(E,D,L.length).set(L);else if(O.type===12)new Uint32Array(E,D,L.length).set(L);else if(O.type===10)new Uint16Array(E,D,L.length).set(L);else if(O.type===1)new Float32Array(E,D,L.length).set(L);else throw new Error(`Unsupported uniform type: ${ut(O.type)}`)});let C=this.gpuDataManager.create(_,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(C.buffer,0,E,0,_),this.gpuDataManager.release(C.id),w={offset:0,size:_,buffer:C.buffer}}let y=this.programManager.normalizeDispatchGroupSize(l),v=y[1]===1&&y[2]===1,T=ip(e,t,v),$=this.programManager.getArtifact(T);if($||($=this.programManager.build(e,y),this.programManager.setArtifact(T,$),de("info",()=>`[artifact] key: ${T}, programName: ${e.name}`)),d&&$.uniformVariablesInfo){if(d.length!==$.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${$.uniformVariablesInfo.length}, got ${d.length} in program "${$.programInfo.name}".`);for(let _=0;_<d.length;_++){let I=d[_],k=I.type,E=typeof I.data=="number"?1:I.data.length,[C,O]=$.uniformVariablesInfo[_];if(k!==C||E!==O)throw new Error(`Uniform variable ${_} mismatch: expect type ${C} with size ${O}, got type ${k} with size ${E} in program "${$.programInfo.name}".`)}}if(de("info",()=>`[ProgramManager] run "${e.name}" (key=${T}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let _={kernelId:this.currentKernelId,programName:$.programInfo.name,inputTensorViews:t,outputTensorViews:h};this.pendingKernels.push(_),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(_)}return this.programManager.run($,s,g,y,w),We(e.name),h}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,r,i){let n=Cf.get(e);if(!n)throw new Error(`kernel not implemented: ${e}`);let a={kernelType:e,kernelName:i,kernelEntry:n[0],attributes:[n[1],r]};this.kernels.set(t,a)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let r of t)this.gpuDataManager.release(r.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,r){let i=this.kernels.get(e);if(!i)throw new Error(`kernel not created: ${e}`);let n=i.kernelType,a=i.kernelName,s=i.kernelEntry,u=i.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${n}] ${a}" is not allowed to be called recursively`);this.currentKernelId=e,u[0]&&(u[1]=u[0](u[1]),u[0]=void 0),de("info",()=>`[WebGPU] Start to run kernel "[${n}] ${a}"...`);let l=this.env.debug;this.temporaryData=[];try{return l&&this.device.pushErrorScope("validation"),s(t,u[1]),0}catch(d){return r.push(Promise.resolve(`[WebGPU] Kernel "[${n}] ${a}" failed. ${d}`)),1}finally{l&&r.push(this.device.popErrorScope().then(d=>d?`GPU validation error for kernel "[${n}] ${a}": ${d.message}`:null));for(let d of this.temporaryData)this.gpuDataManager.release(d.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,r,i){let n=this.sessionExternalDataMapping.get(e);n||(n=new Map,this.sessionExternalDataMapping.set(e,n));let a=n.get(t),s=this.gpuDataManager.registerExternalBuffer(r,i,a);return n.set(t,[s,r]),s}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(r=>this.gpuDataManager.unregisterExternalBuffer(r[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,r){return async()=>{let i=await Vn(this,e,t);return ha(i.buffer,r)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){de("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){de("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){de("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),r=e.length;this.pendingKernels=[];for(let i=0;i<r;i++){let n=this.getComputePassEncoder(),a=e[i];this.writeTimestamp(this.pendingDispatchNumber*2),n.setPipeline(a.computePipeline),n.setBindGroup(0,a.bindGroup),n.dispatchWorkgroups(...a.dispatchGroup),this.writeTimestamp(this.pendingDispatchNumber*2+1),this.pendingDispatchNumber++,this.queryType!=="none"&&this.pendingKernels.push(t[i]),(this.pendingDispatchNumber>=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),Rf={};Yt(Rf,{init:()=>Mf});q0=P(()=>{"use strict";ee(),lt(),ie(),Zy(),Jr=class Bf{constructor(t,r,i,n){this.module=t,this.dataType=r,this.data=i,this.dims=n}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=R.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=R.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=R.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=R.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(R.size(t)!==R.size(this.dims))throw new Error("Invalid new shape");return new Bf(this.module,this.dataType,this.data,t)}},ap=class{constructor(e,t,r){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let i=e.PTR_SIZE,n=r/e.PTR_SIZE,a=i===4?"i32":"i64";this.opKernelContext=Number(e.getValue(i*n++,a));let s=Number(e.getValue(i*n++,a));this.outputCount=Number(e.getValue(i*n++,a)),this.customDataOffset=Number(e.getValue(i*n++,"*")),this.customDataSize=Number(e.getValue(i*n++,a));let u=[];for(let l=0;l<s;l++){let d=Number(e.getValue(i*n++,a)),f=Number(e.getValue(i*n++,"*")),h=Number(e.getValue(i*n++,a)),g=[];for(let w=0;w<h;w++)g.push(Number(e.getValue(i*n++,a)));u.push(new Jr(e,d,f,g))}this.inputs=u}get kernelCustomData(){return this.backend.currentKernelCustomData}get customDataBuffer(){return this.module.HEAPU8.subarray(this.customDataOffset,this.customDataOffset+this.customDataSize)}compute(e,t){let r=t?.inputs?.map(s=>typeof s=="number"?this.inputs[s]:s)??this.inputs,i=t?.outputs??[],n=(s,u,l)=>new Jr(this.module,u,this.output(s,l),l),a=(s,u)=>{let l=Mt(s,u);if(!l)throw new Error(`Unsupported data type: ${s}`);let d=l>0?this.backend.gpuDataManager.create(l).id:0;return new Jr(this.module,s,d,u)};return this.backend.run(e,r,i,n,a,this.outputCount)}output(e,t){let r=this.module.stackSave();try{let i=this.module.PTR_SIZE,n=i===4?"i32":"i64",a=this.module.stackAlloc((1+t.length)*i);this.module.setValue(a,t.length,n);for(let s=0;s<t.length;s++)this.module.setValue(a+i*(s+1),t[s],n);return this.module._JsepOutput(this.opKernelContext,e,a)}catch(i){throw new Error(`Failed to generate kernel's output[${e}] with dims [${t}]. If you are running with pre-allocated output, please make sure the output type/dims are correct. Error: ${i}`)}finally{this.module.stackRestore(r)}}},Mf=async(e,t,r,i)=>{let n=t.jsepInit;if(!n)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let a=(L0(),$r(Af)).WebGpuBackend,s=new a;await s.initialize(r,i),n("webgpu",[s,u=>s.alloc(Number(u)),u=>s.free(u),(u,l,d,f=!1)=>{if(f)de("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(u)}, dst=${Number(l)}, size=${Number(d)}`),s.memcpy(Number(u),Number(l));else{de("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(u)}, gpuDataId=${Number(l)}, size=${Number(d)}`);let h=t.HEAPU8.subarray(Number(u>>>0),Number(u>>>0)+Number(d));s.upload(Number(l),h)}},async(u,l,d)=>{de("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${u}, dataOffset=${l}, size=${d}`),await s.download(Number(u),()=>t.HEAPU8.subarray(Number(l)>>>0,Number(l+d)>>>0))},(u,l,d)=>s.createKernel(u,Number(l),d,t.UTF8ToString(t._JsepGetNodeName(Number(l)))),u=>s.releaseKernel(u),(u,l,d,f)=>{de("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${d}, kernel=${u}, contextDataOffset=${l}`);let h=new ap(t,s,Number(l));return s.computeKernel(Number(u),h,f)},()=>s.captureBegin(),()=>s.captureEnd(),()=>s.replay()])}else{let a=new Gp(r);n("webnn",[a,()=>a.reserveTensorId(),s=>a.releaseTensorId(s),async(s,u,l,d,f)=>a.ensureTensor(s,u,l,d,f),(s,u)=>{a.uploadTensor(s,u)},async(s,u)=>a.downloadTensor(s,u),(s,u)=>a.registerMLContext(s,u),!!r.trace])}}}),Nf=P(()=>{"use strict";Le(),Ky(),jy(),ee(),Ut(),ua(),Pp(),sp=(e,t)=>{we()._OrtInit(e,t)!==0&&ge("Can't initialize onnxruntime.")},Ta=async e=>{sp(e.wasm.numThreads,ai(e.logLevel))},Sa=async(e,t)=>{we().asyncInit?.();let r=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(r){if(typeof r.limits!="object"||typeof r.features!="object"||typeof r.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let i=e.webgpu.powerPreference;if(i!==void 0&&i!=="low-power"&&i!=="high-performance")throw new Error(`Invalid powerPreference setting: "${i}"`);let n=e.webgpu.forceFallbackAdapter;if(n!==void 0&&typeof n!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${n}"`);if(r=await navigator.gpu.requestAdapter({powerPreference:i,forceFallbackAdapter:n}),!r)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let i=(q0(),$r(Rf)).init;t==="webgpu"&&await i("webgpu",we(),e,r),t==="webnn"&&await i("webnn",we(),e)}},yt=new Map,op=e=>{let t=we(),r=t.stackSave();try{let i=t.PTR_SIZE,n=t.stackAlloc(2*i);t._OrtGetInputOutputCount(e,n,n+i)!==0&&ge("Can't get session input/output count.");let a=i===4?"i32":"i64";return[Number(t.getValue(n,a)),Number(t.getValue(n+i,a))]}finally{t.stackRestore(r)}},Nn=(e,t)=>{let r=we(),i=r.stackSave(),n=0;try{let a=r.PTR_SIZE,s=r.stackAlloc(2*a);r._OrtGetInputOutputMetadata(e,t,s,s+a)!==0&&ge("Can't get session input/output metadata.");let u=Number(r.getValue(s,"*"));n=Number(r.getValue(s+a,"*"));let l=r.HEAP32[n/4];if(l===0)return[u,0];let d=r.HEAPU32[n/4+1],f=[];for(let h=0;h<d;h++){let g=Number(r.getValue(n+8+h*a,"*"));f.push(g!==0?r.UTF8ToString(g):Number(r.getValue(n+8+(h+d)*a,"*")))}return[u,l,f]}finally{r.stackRestore(i),n!==0&&r._OrtFree(n)}},di=e=>{let t=we(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},ka=async(e,t)=>{let r,i,n=we();Array.isArray(e)?[r,i]=e:e.buffer===n.HEAPU8.buffer?[r,i]=[e.byteOffset,e.byteLength]:[r,i]=di(e);let a=0,s=0,u=0,l=[],d=[],f=[];try{if([s,l]=await Dp(t),t?.externalData&&n.mountExternalData){let k=[];for(let E of t.externalData){let C=typeof E=="string"?E:E.path;k.push(pa(typeof E=="string"?E:E.data).then(O=>{n.mountExternalData(C,O)}))}await Promise.all(k)}for(let k of t?.executionProviders??[])if((typeof k=="string"?k:k.name)==="webnn"){if(n.shouldTransferToMLTensor=!1,typeof k!="string"){let E=k,C=E?.context,O=E?.gpuDevice,x=E?.deviceType,D=E?.powerPreference;C?n.currentContext=C:O?n.currentContext=await n.webnnCreateMLContext(O):n.currentContext=await n.webnnCreateMLContext({deviceType:x,powerPreference:D})}else n.currentContext=await n.webnnCreateMLContext();break}a=await n._OrtCreateSession(r,i,s),n.webgpuOnCreateSession?.(a),a===0&&ge("Can't create a session."),n.jsepOnCreateSession?.(),n.currentContext&&(n.webnnRegisterMLContext(a,n.currentContext),n.currentContext=void 0,n.shouldTransferToMLTensor=!0);let[h,g]=op(a),w=!!t?.enableGraphCapture,y=[],v=[],T=[],$=[],_=[];for(let k=0;k<h;k++){let[E,C,O]=Nn(a,k);E===0&&ge("Can't get an input name."),d.push(E);let x=n.UTF8ToString(E);y.push(x),T.push(C===0?{name:x,isTensor:!1}:{name:x,isTensor:!0,type:ut(C),shape:O})}for(let k=0;k<g;k++){let[E,C,O]=Nn(a,k+h);E===0&&ge("Can't get an output name."),f.push(E);let x=n.UTF8ToString(E);v.push(x),$.push(C===0?{name:x,isTensor:!1}:{name:x,isTensor:!0,type:ut(C),shape:O});{if(w&&t?.preferredOutputLocation===void 0){_.push("gpu-buffer");continue}let D=typeof t?.preferredOutputLocation=="string"?t.preferredOutputLocation:t?.preferredOutputLocation?.[x]??"cpu",L=n.webnnIsGraphOutput;if(D==="cpu"&&L&&L(a,x)){_.push("ml-tensor-cpu-output");continue}if(D!=="cpu"&&D!=="cpu-pinned"&&D!=="gpu-buffer"&&D!=="ml-tensor")throw new Error(`Not supported preferred output location: ${D}.`);if(w&&D!=="gpu-buffer")throw new Error(`Not supported preferred output location: ${D}. Only 'gpu-buffer' location is supported when enableGraphCapture is true.`);_.push(D)}}let I=null;return _.some(k=>k==="gpu-buffer"||k==="ml-tensor"||k==="ml-tensor-cpu-output")&&(u=n._OrtCreateBinding(a),u===0&&ge("Can't create IO binding."),I={handle:u,outputPreferredLocations:_,outputPreferredLocationsEncoded:_.map(k=>k==="ml-tensor-cpu-output"?"ml-tensor":k).map(k=>Ln(k))}),yt.set(a,[a,d,f,I,w,!1]),[a,y,v,T,$]}catch(h){throw d.forEach(g=>n._OrtFree(g)),f.forEach(g=>n._OrtFree(g)),u!==0&&n._OrtReleaseBinding(u)!==0&&ge("Can't release IO binding."),a!==0&&n._OrtReleaseSession(a)!==0&&ge("Can't release session."),h}finally{n._free(r),s!==0&&n._OrtReleaseSessionOptions(s)!==0&&ge("Can't release session options."),l.forEach(h=>n._free(h)),n.unmountExternalData?.()}},Ia=e=>{let t=we(),r=yt.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[i,n,a,s,u]=r;s&&(u&&t._OrtClearBoundOutputs(s.handle)!==0&&ge("Can't clear bound outputs."),t._OrtReleaseBinding(s.handle)!==0&&ge("Can't release IO binding.")),t.jsepOnReleaseSession?.(e),t.webnnOnReleaseSession?.(e),t.webgpuOnReleaseSession?.(e),n.forEach(l=>t._OrtFree(l)),a.forEach(l=>t._OrtFree(l)),t._OrtReleaseSession(i)!==0&&ge("Can't release session."),yt.delete(e)},Dn=async(e,t,r,i,n,a,s=!1)=>{if(!e){t.push(0);return}let u=we(),l=u.PTR_SIZE,d=e[0],f=e[1],h=e[3],g=h,w,y;if(d==="string"&&(h==="gpu-buffer"||h==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(s&&h!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${a} when enableGraphCapture is true.`);if(h==="gpu-buffer"){let $=e[2].gpuBuffer;y=Mt(Rt(d),f);{let _=u.jsepRegisterBuffer;if(!_)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');w=_(i,a,$,y)}}else if(h==="ml-tensor"){let $=e[2].mlTensor;y=Mt(Rt(d),f);let _=u.webnnRegisterMLTensor;if(!_)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');w=_(i,$,Rt(d),f)}else{let $=e[2];if(Array.isArray($)){y=l*$.length,w=u._malloc(y),r.push(w);for(let _=0;_<$.length;_++){if(typeof $[_]!="string")throw new TypeError(`tensor data at index ${_} is not a string`);u.setValue(w+_*l,Ze($[_],r),"*")}}else{let _=u.webnnIsGraphInput,I=u.webnnIsGraphOutput;if(d!=="string"&&_&&I){let k=u.UTF8ToString(n);if(_(i,k)||I(i,k)){let E=Rt(d);y=Mt(E,f),g="ml-tensor";let C=u.webnnCreateTemporaryTensor,O=u.webnnUploadTensor;if(!C||!O)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');let x=await C(i,E,f);O(x,new Uint8Array($.buffer,$.byteOffset,$.byteLength)),w=x}else y=$.byteLength,w=u._malloc(y),r.push(w),u.HEAPU8.set(new Uint8Array($.buffer,$.byteOffset,y),w)}else y=$.byteLength,w=u._malloc(y),r.push(w),u.HEAPU8.set(new Uint8Array($.buffer,$.byteOffset,y),w)}}let v=u.stackSave(),T=u.stackAlloc(4*f.length);try{f.forEach((_,I)=>u.setValue(T+I*l,_,l===4?"i32":"i64"));let $=u._OrtCreateTensor(Rt(d),w,y,T,f.length,Ln(g));$===0&&ge(`Can't create tensor for input/output. session=${i}, index=${a}.`),t.push($)}finally{u.stackRestore(v)}},Ea=async(e,t,r,i,n,a)=>{let s=we(),u=s.PTR_SIZE,l=yt.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let d=l[0],f=l[1],h=l[2],g=l[3],w=l[4],y=l[5],v=t.length,T=i.length,$=0,_=[],I=[],k=[],E=[],C=[],O=s.stackSave(),x=s.stackAlloc(v*u),D=s.stackAlloc(v*u),L=s.stackAlloc(T*u),Y=s.stackAlloc(T*u);try{[$,_]=Np(a),_t("wasm prepareInputOutputTensor");for(let A=0;A<v;A++)await Dn(r[A],I,E,e,f[t[A]],t[A],w);for(let A=0;A<T;A++)await Dn(n[A],k,E,e,h[i[A]],v+i[A],w);bt("wasm prepareInputOutputTensor");for(let A=0;A<v;A++)s.setValue(x+A*u,I[A],"*"),s.setValue(D+A*u,f[t[A]],"*");for(let A=0;A<T;A++)s.setValue(L+A*u,k[A],"*"),s.setValue(Y+A*u,h[i[A]],"*");if(g&&!y){let{handle:A,outputPreferredLocations:U,outputPreferredLocationsEncoded:re}=g;if(f.length!==v)throw new Error(`input count from feeds (${v}) is expected to be always equal to model's input count (${f.length}).`);_t("wasm bindInputsOutputs");for(let J=0;J<v;J++){let H=t[J];await s._OrtBindInput(A,f[H],I[J])!==0&&ge(`Can't bind input[${J}] for session=${e}.`)}for(let J=0;J<T;J++){let H=i[J];n[J]?.[3]?(C.push(k[J]),s._OrtBindOutput(A,h[H],k[J],0)!==0&&ge(`Can't bind pre-allocated output[${J}] for session=${e}.`)):s._OrtBindOutput(A,h[H],0,re[H])!==0&&ge(`Can't bind output[${J}] to ${U[J]} for session=${e}.`)}bt("wasm bindInputsOutputs"),yt.set(e,[d,f,h,g,w,!0])}s.jsepOnRunStart?.(d),s.webnnOnRunStart?.(d);let q;g?q=await s._OrtRunWithBinding(d,g.handle,T,L,$):q=await s._OrtRun(d,D,x,v,Y,T,L,$),q!==0&&ge("failed to call OrtRun().");let j=[],ue=[];_t("wasm ProcessOutputTensor");for(let A=0;A<T;A++){let U=Number(s.getValue(L+A*u,"*"));if(U===k[A]||C.includes(k[A])){j.push(n[A]),U!==k[A]&&s._OrtReleaseTensor(U)!==0&&ge("Can't release tensor.");continue}let re=s.stackSave(),J=s.stackAlloc(4*u),H=!1,pe,N=0;try{s._OrtGetTensorData(U,J,J+u,J+2*u,J+3*u)!==0&&ge(`Can't access output tensor data on index ${A}.`);let V=u===4?"i32":"i64",te=Number(s.getValue(J,V));N=s.getValue(J+u,"*");let ce=s.getValue(J+u*2,"*"),Ie=Number(s.getValue(J+u*3,V)),Me=[];for(let Se=0;Se<Ie;Se++)Me.push(Number(s.getValue(ce+Se*u,V)));s._OrtFree(ce)!==0&&ge("Can't free memory for tensor dims.");let Ve=Me.reduce((Se,$e)=>Se*$e,1);pe=ut(te);let nt=g?.outputPreferredLocations[i[A]];if(pe==="string"){if(nt==="gpu-buffer"||nt==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let Se=[];for(let $e=0;$e<Ve;$e++){let Re=s.getValue(N+$e*u,"*"),xt=s.getValue(N+($e+1)*u,"*"),kr=$e===Ve-1?void 0:xt-Re;Se.push(s.UTF8ToString(Re,kr))}j.push([pe,Me,Se,"cpu"])}else if(nt==="gpu-buffer"&&Ve>0){let Se=s.jsepGetBuffer;if(!Se)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let $e=Se(N),Re=Mt(te,Ve);if(Re===void 0||!la(pe))throw new Error(`Unsupported data type: ${pe}`);H=!0,j.push([pe,Me,{gpuBuffer:$e,download:s.jsepCreateDownloader($e,Re,pe),dispose:()=>{s._OrtReleaseTensor(U)!==0&&ge("Can't release tensor.")}},"gpu-buffer"])}else if(nt==="ml-tensor"&&Ve>0){let Se=s.webnnEnsureTensor,$e=s.webnnIsGraphInputOutputTypeSupported;if(!Se||!$e)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(Mt(te,Ve)===void 0||!da(pe))throw new Error(`Unsupported data type: ${pe}`);if(!$e(e,pe,!1))throw new Error(`preferredLocation "ml-tensor" for ${pe} output is not supported by current WebNN Context.`);let Re=await Se(e,N,te,Me,!1);H=!0,j.push([pe,Me,{mlTensor:Re,download:s.webnnCreateMLTensorDownloader(N,pe),dispose:()=>{s.webnnReleaseTensorId(N),s._OrtReleaseTensor(U)}},"ml-tensor"])}else if(nt==="ml-tensor-cpu-output"&&Ve>0){let Se=s.webnnCreateMLTensorDownloader(N,pe)(),$e=j.length;H=!0,ue.push((async()=>{let Re=[$e,await Se];return s.webnnReleaseTensorId(N),s._OrtReleaseTensor(U),Re})()),j.push([pe,Me,[],"cpu"])}else{let Se=pi(pe),$e=new Se(Ve);new Uint8Array($e.buffer,$e.byteOffset,$e.byteLength).set(s.HEAPU8.subarray(N,N+$e.byteLength)),j.push([pe,Me,$e,"cpu"])}}finally{s.stackRestore(re),pe==="string"&&N&&s._free(N),H||s._OrtReleaseTensor(U)}}g&&!w&&(s._OrtClearBoundOutputs(g.handle)!==0&&ge("Can't clear bound outputs."),yt.set(e,[d,f,h,g,w,!1]));for(let[A,U]of await Promise.all(ue))j[A][2]=U;return bt("wasm ProcessOutputTensor"),j}finally{s.webnnOnRunEnd?.(d),s.stackRestore(O),I.forEach(q=>s._OrtReleaseTensor(q)),k.forEach(q=>s._OrtReleaseTensor(q)),E.forEach(q=>s._free(q)),$!==0&&s._OrtReleaseRunOptions($),_.forEach(q=>s._free(q))}},Ca=e=>{let t=we(),r=yt.get(e);if(!r)throw new Error("invalid session id");let i=r[0],n=t._OrtEndProfiling(i);n===0&&ge("Can't get an profile file name."),t._OrtFree(n)},za=e=>{let t=[];for(let r of e){let i=r[2];!Array.isArray(i)&&"buffer"in i&&t.push(i.buffer)}return t}}),Gf=P(()=>{"use strict";Le(),Nf(),Ut(),sa(),wt=()=>!!be.wasm.proxy&&typeof document<"u",Kt=!1,mr=!1,gr=!1,ti=new Map,zt=(e,t)=>{let r=ti.get(e);r?r.push(t):ti.set(e,[t])},At=()=>{if(Kt||!mr||gr||!Ue)throw new Error("worker not ready")},up=e=>{switch(e.data.type){case"init-wasm":Kt=!1,e.data.err?(gr=!0,Pn[1](e.data.err)):(mr=!0,Pn[0]()),ei&&(URL.revokeObjectURL(ei),ei=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=ti.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Df=async()=>{if(!mr){if(Kt)throw new Error("multiple calls to 'initWasm()' detected.");if(gr)throw new Error("previous call to 'initWasm()' failed.");if(Kt=!0,wt())return new Promise((e,t)=>{Ue?.terminate(),Mp().then(([r,i])=>{try{Ue=i,Ue.onerror=a=>t(a),Ue.onmessage=up,Pn=[e,t];let n={type:"init-wasm",in:be};!n.in.wasm.wasmPaths&&(r||Wn)&&(n.in.wasm.wasmPaths={wasm:new URL("ort-wasm-simd-threaded.jsep.wasm",Ye.url).href}),Ue.postMessage(n),ei=r}catch(n){t(n)}},t)});try{await oa(be.wasm),await Ta(be),mr=!0}catch(e){throw gr=!0,e}finally{Kt=!1}}},Pf=async e=>{if(wt())return At(),new Promise((t,r)=>{zt("init-ep",[t,r]);let i={type:"init-ep",in:{epName:e,env:be}};Ue.postMessage(i)});await Sa(be,e)},Uf=async e=>wt()?(At(),new Promise((t,r)=>{zt("copy-from",[t,r]);let i={type:"copy-from",in:{buffer:e}};Ue.postMessage(i,[e.buffer])})):di(e),Wf=async(e,t)=>{if(wt()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return At(),new Promise((r,i)=>{zt("create",[r,i]);let n={type:"create",in:{model:e,options:{...t}}},a=[];e instanceof Uint8Array&&a.push(e.buffer),Ue.postMessage(n,a)})}else return ka(e,t)},Lf=async e=>{if(wt())return At(),new Promise((t,r)=>{zt("release",[t,r]);let i={type:"release",in:e};Ue.postMessage(i)});Ia(e)},qf=async(e,t,r,i,n,a)=>{if(wt()){if(r.some(s=>s[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(n.some(s=>s))throw new Error("pre-allocated output tensor is not supported for proxy.");return At(),new Promise((s,u)=>{zt("run",[s,u]);let l=r,d={type:"run",in:{sessionId:e,inputIndices:t,inputs:l,outputIndices:i,options:a}};Ue.postMessage(d,za(l))})}else return Ea(e,t,r,i,n,a)},Vf=async e=>{if(wt())return At(),new Promise((t,r)=>{zt("end-profiling",[t,r]);let i={type:"end-profiling",in:e};Ue.postMessage(i)});Ca(e)}}),V0=P(()=>{"use strict";Le(),Gf(),ee(),aa(),Pp(),Un=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},lp=e=>{switch(e[3]){case"cpu":return new Xe(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!la(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:i,dispose:n}=e[2];return Xe.fromGpuBuffer(r,{dataType:t,dims:e[1],download:i,dispose:n})}case"ml-tensor":{let t=e[0];if(!da(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:r,download:i,dispose:n}=e[2];return Xe.fromMLTensor(r,{dataType:t,dims:e[1],download:i,dispose:n})}default:throw new Error(`invalid data location: ${e[3]}`)}},Ff=class{async fetchModelAndCopyToWasmMemory(e){return Uf(await pa(e))}async loadModel(e,t){Je();let r;typeof e=="string"?r=await this.fetchModelAndCopyToWasmMemory(e):r=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await Wf(r,t),We()}async dispose(){return Lf(this.sessionId)}async run(e,t,r){Je();let i=[],n=[];Object.entries(e).forEach(h=>{let g=h[0],w=h[1],y=this.inputNames.indexOf(g);if(y===-1)throw new Error(`invalid input '${g}'`);i.push(w),n.push(y)});let a=[],s=[];Object.entries(t).forEach(h=>{let g=h[0],w=h[1],y=this.outputNames.indexOf(g);if(y===-1)throw new Error(`invalid output '${g}'`);a.push(w),s.push(y)});let u=i.map((h,g)=>Un(h,()=>`input "${this.inputNames[n[g]]}"`)),l=a.map((h,g)=>h?Un(h,()=>`output "${this.outputNames[s[g]]}"`):null),d=await qf(this.sessionId,n,u,s,l,r),f={};for(let h=0;h<d.length;h++)f[this.outputNames[s[h]]]=a[h]??lp(d[h]);return We(),f}startProfiling(){}endProfiling(){Vf(this.sessionId)}}}),Hf={};Yt(Hf,{OnnxruntimeWebAssemblyBackend:()=>ta,initializeFlags:()=>ea,wasmBackend:()=>Kf});G0=P(()=>{"use strict";Le(),Gf(),V0(),ea=()=>{(typeof be.wasm.initTimeout!="number"||be.wasm.initTimeout<0)&&(be.wasm.initTimeout=0);let e=be.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),be.wasm.simd=!1),typeof be.wasm.proxy!="boolean"&&(be.wasm.proxy=!1),typeof be.wasm.trace!="boolean"&&(be.wasm.trace=!1),typeof be.wasm.numThreads!="number"||!Number.isInteger(be.wasm.numThreads)||be.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)be.wasm.numThreads=1;else{let t=typeof navigator>"u"?zy("node:os").cpus().length:navigator.hardwareConcurrency;be.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},ta=class{async init(e){ea(),await Df(),await Pf(e)}async createInferenceSessionHandler(e,t){let r=new Ff;return await r.loadModel(e,t),r}},Kf=new ta});Le();Le();Le();F0="1.24.2",H0=Ep;{let e=(G0(),$r(Hf)).wasmBackend;Bt("webgpu",e,5),Bt("webnn",e,5),Bt("cpu",e,10),Bt("wasm",e,10)}Object.defineProperty(be.versions,"web",{value:F0,enumerable:!0});});var J0={};zo(J0,{DeviceAuthManager:()=>hi,ExperimentsClient:()=>yi,FederatedClient:()=>fi,InferenceEngine:()=>Xt,ModelLoader:()=>Jt,Octomil:()=>ci,OctomilError:()=>X,RolloutsManager:()=>gi,SecAggPlus:()=>mi,SecureAggregation:()=>Sr,StreamingInferenceEngine:()=>Gt,TelemetryReporter:()=>Vt,WeightExtractor:()=>er,addGaussianNoise:()=>im,assertModelIntegrity:()=>em,clipGradients:()=>rm,computeHash:()=>Oa,createModelCache:()=>qr,dequantize:()=>am,disposeTelemetry:()=>Xf,getTelemetry:()=>Yf,initTelemetry:()=>Zf,quantize:()=>nm,shamirReconstruct:()=>Ba,shamirSplit:()=>Ma,verifyModelIntegrity:()=>Ra});var X=class extends Error{constructor(r,i,n){super(i);Q(this,"code");Q(this,"cause");this.name="OctomilError",this.code=r,this.cause=n}};var Ty="octomil-models-v1",Sy="octomil-models",Wr="blobs",ky=1,qi=class{async open(){return caches.open(Ty)}async get(t){let i=await(await this.open()).match(t);return i?i.arrayBuffer():null}async put(t,r){let i=await this.open(),n=new Response(r,{headers:{"x-octomil-cached-at":new Date().toISOString(),"x-octomil-size":String(r.byteLength)}});await i.put(t,n)}async has(t){return await(await this.open()).match(t)!==void 0}async remove(t){await(await this.open()).delete(t)}async info(t){let i=await(await this.open()).match(t);if(!i)return{cached:!1,sizeBytes:0};let n=i.headers.get("x-octomil-size"),a=i.headers.get("x-octomil-cached-at");return{cached:!0,sizeBytes:n?parseInt(n,10):0,cachedAt:a??void 0}}},Lr=class{constructor(){Q(this,"dbPromise",null)}openDB(){return this.dbPromise?this.dbPromise:(this.dbPromise=new Promise((t,r)=>{let i=indexedDB.open(Sy,ky);i.onupgradeneeded=()=>{let n=i.result;n.objectStoreNames.contains(Wr)||n.createObjectStore(Wr,{keyPath:"key"})},i.onsuccess=()=>t(i.result),i.onerror=()=>r(i.error)}),this.dbPromise)}async tx(t){return(await this.openDB()).transaction(Wr,t).objectStore(Wr)}async get(t){let r=await this.tx("readonly");return new Promise((i,n)=>{let a=r.get(t);a.onsuccess=()=>{let s=a.result;i(s?.data??null)},a.onerror=()=>n(a.error)})}async put(t,r){let i=await this.tx("readwrite"),n={key:t,data:r,sizeBytes:r.byteLength,cachedAt:new Date().toISOString()};return new Promise((a,s)=>{let u=i.put(n);u.onsuccess=()=>a(),u.onerror=()=>s(u.error)})}async has(t){let r=await this.tx("readonly");return new Promise((i,n)=>{let a=r.count(t);a.onsuccess=()=>i(a.result>0),a.onerror=()=>n(a.error)})}async remove(t){let r=await this.tx("readwrite");return new Promise((i,n)=>{let a=r.delete(t);a.onsuccess=()=>i(),a.onerror=()=>n(a.error)})}async info(t){let r=await this.tx("readonly");return new Promise((i,n)=>{let a=r.get(t);a.onsuccess=()=>{let s=a.result;i(s?{cached:!0,sizeBytes:s.sizeBytes,cachedAt:s.cachedAt}:{cached:!1,sizeBytes:0})},a.onerror=()=>n(a.error)})}},ar=class{async get(t){return null}async put(t,r){}async has(t){return!1}async remove(t){}async info(t){return{cached:!1,sizeBytes:0}}};function qr(e){if(e==="none")return new ar;if(e==="cache-api")return typeof caches<"u"?new qi:typeof indexedDB<"u"?new Lr:new ar;if(e==="indexeddb")return typeof indexedDB<"u"?new Lr:new ar;throw new X("CACHE_ERROR",`Unknown cache strategy: ${e}`)}var Xt=class{constructor(){Q(this,"session",null);Q(this,"ortModule",null);Q(this,"resolvedBackend",null)}async createSession(t,r){let i=await this.loadOrt(),n=await this.resolveProvider(i,r);this.resolvedBackend=n==="webgpu"?"webgpu":"wasm";let a={executionProviders:[n],graphOptimizationLevel:"all"};try{this.session=await i.InferenceSession.create(t,a)}catch(s){if(n==="webgpu")try{this.session=await i.InferenceSession.create(t,{executionProviders:["wasm"],graphOptimizationLevel:"all"}),this.resolvedBackend="wasm"}catch(u){throw new X("MODEL_LOAD_FAILED","Failed to create ONNX session with both WebGPU and WASM backends.",u)}else throw new X("MODEL_LOAD_FAILED",`Failed to create ONNX session: ${String(s)}`,s)}}async run(t){this.ensureSession();let r=this.ortModule,i=this.session,n={};for(let[f,h]of Object.entries(t))n[f]=new r.Tensor(K0(h.data),h.data,h.dims);let a=performance.now(),s;try{s=await i.run(n)}catch(f){throw new X("INFERENCE_FAILED",`Inference run failed: ${String(f)}`,f)}let u=performance.now()-a,l=this.convertOutputs(s),d=this.extractConvenience(l);return{tensors:l,latencyMs:u,...d}}get inputNames(){return this.ensureSession(),this.session.inputNames}get outputNames(){return this.ensureSession(),this.session.outputNames}get activeBackend(){return this.resolvedBackend}dispose(){this.session&&(this.session.release(),this.session=null)}async loadOrt(){if(this.ortModule)return this.ortModule;try{return this.ortModule=await Promise.resolve().then(()=>(Qf(),jf)),this.ortModule}catch(t){throw new X("BACKEND_UNAVAILABLE","Failed to import onnxruntime-web. Make sure the package is installed: npm i onnxruntime-web",t)}}async resolveProvider(t,r){if(r==="wasm")return"wasm";if(r==="webgpu"||r===void 0){if(await this.detectWebGPU())return"webgpu";if(r==="webgpu")throw new X("BACKEND_UNAVAILABLE","WebGPU was explicitly requested but is not available in this browser.")}return"wasm"}async detectWebGPU(){if(typeof navigator>"u")return!1;try{let t=navigator.gpu;return t?await t.requestAdapter()!==null:!1}catch{return!1}}ensureSession(){if(!this.session)throw new X("SESSION_DISPOSED","No active session. Call load() before running inference.")}convertOutputs(t){let r={};for(let i of Object.keys(t)){let n=t[i];r[i]={data:n.data,dims:Array.from(n.dims)}}return r}extractConvenience(t){let r=Object.keys(t);if(r.length===0)return{};let n=t[r[0]].data;if(!(n instanceof Float32Array))return{};if(n.length===0)return{};let a=Array.from(n),s=0,u=-1/0;for(let l=0;l<a.length;l++)a[l]>u&&(u=a[l],s=l);return{label:String(s),score:u,scores:a}}};function K0(e){return e instanceof Float32Array?"float32":e instanceof Int32Array?"int32":e instanceof BigInt64Array?"int64":e instanceof Uint8Array?"uint8":"float32"}var Lt=3,Aa=1e3,Jt=class{constructor(t,r){Q(this,"modelId");Q(this,"serverUrl");Q(this,"apiKey");Q(this,"onProgress");Q(this,"cache");this.modelId=t.model,this.serverUrl=t.serverUrl,this.apiKey=t.apiKey,this.onProgress=t.onProgress,this.cache=r}async load(){let t=await this.resolveModelUrl(),r=await this.cache.get(t);if(r)return r;let i=await this.download(t);return this.validate(i),await this.cache.put(t,i),i}async isCached(){let t=await this.resolveModelUrl();return this.cache.has(t)}async clearCache(){let t=await this.resolveModelUrl();await this.cache.remove(t)}async getCacheInfo(){let t=await this.resolveModelUrl();return this.cache.info(t)}async resolveModelUrl(){if(this.modelId.startsWith("http://")||this.modelId.startsWith("https://"))return this.modelId;if(!this.serverUrl)throw new X("MODEL_NOT_FOUND",`Cannot resolve model "${this.modelId}": no serverUrl configured.`);return this.fetchRegistryUrl(this.modelId)}async fetchRegistryUrl(t){let r=`${this.serverUrl}/api/v1/models/${encodeURIComponent(t)}/metadata`,i={Accept:"application/json"};this.apiKey&&(i.Authorization=`Bearer ${this.apiKey}`);let n;try{n=await fetch(r,{headers:i})}catch(s){throw new X("NETWORK_ERROR",`Failed to reach model registry at ${r}`,s)}if(!n.ok)throw n.status===404?new X("MODEL_NOT_FOUND",`Model "${t}" not found in registry.`):new X("NETWORK_ERROR",`Registry returned HTTP ${n.status}: ${n.statusText}`);return(await n.json()).url}buildHeaders(t={}){let r={"Accept-Encoding":"gzip, deflate, br",...t};return this.apiKey&&(r.Authorization=`Bearer ${this.apiKey}`),r}async download(t){let r=[],i=0,n=0,a=0;for(;a<=Lt;){let l=this.buildHeaders(i>0?{Range:`bytes=${i}-`}:{}),d;try{d=await fetch(t,{headers:l})}catch(g){if(a++,a>Lt)throw new X("NETWORK_ERROR",`Failed to download model from ${t} after ${Lt} retries`,g);await this.delay(Aa*a);continue}if(d.status===416){if(r.length=0,i=0,a++,a>Lt)throw new X("MODEL_LOAD_FAILED",`Model download failed: range request rejected after ${Lt} retries`);await this.delay(Aa*a);continue}if(!d.ok&&d.status!==206)throw new X("MODEL_LOAD_FAILED",`Model download failed: HTTP ${d.status} ${d.statusText}`);if(n===0){let g=d.headers.get("Content-Range");if(g){let w=g.match(/\/(\d+)$/);w&&(n=parseInt(w[1],10))}else n=parseInt(d.headers.get("Content-Length")??"0",10)}if(!d.body)return await d.arrayBuffer();let f=d.body.getReader(),h=!1;try{for(;;){let{done:g,value:w}=await f.read();if(g)break;r.push(w),i+=w.byteLength,this.onProgress?.({loaded:i,total:n,percent:n>0?i/n*100:NaN})}}catch(g){if(h=!0,a++,a>Lt)throw new X("NETWORK_ERROR",`Model download interrupted after ${Lt} retries`,g);await this.delay(Aa*a)}finally{f.releaseLock()}if(!h)break}let s=new Uint8Array(i),u=0;for(let l of r)s.set(l,u),u+=l.byteLength;return s.buffer}delay(t){return new Promise(r=>setTimeout(r,t))}validate(t){if(t.byteLength===0)throw new X("MODEL_LOAD_FAILED","Downloaded model is empty (0 bytes).");if(new Uint8Array(t,0,Math.min(4,t.byteLength))[0]!==8)throw new X("MODEL_LOAD_FAILED","Downloaded file does not appear to be a valid ONNX model.")}};var j0="https://api.octomil.io/v1/telemetry",Vt=class{constructor(t={}){Q(this,"url");Q(this,"flushIntervalMs");Q(this,"maxBatchSize");Q(this,"apiKey");Q(this,"queue",[]);Q(this,"timerId",null);Q(this,"disposed",!1);this.url=t.url??j0,this.flushIntervalMs=t.flushIntervalMs??3e4,this.maxBatchSize=t.maxBatchSize??50,this.apiKey=t.apiKey,this.startAutoFlush()}track(t){this.disposed||(this.queue.push(t),this.queue.length>=this.maxBatchSize&&this.flush())}async flush(){if(this.queue.length===0)return;let t=this.queue.splice(0,this.maxBatchSize);await this.send(t)}dispose(){this.disposed||(this.disposed=!0,this.timerId!==null&&(clearInterval(this.timerId),this.timerId=null),this.queue.length>0&&this.sendBeacon(this.queue.splice(0)))}startAutoFlush(){typeof setInterval>"u"||(this.timerId=setInterval(()=>{this.flush()},this.flushIntervalMs))}async send(t){let r=JSON.stringify({events:t});try{if(this.sendBeacon(t))return;let i={"Content-Type":"application/json"};this.apiKey&&(i.Authorization=`Bearer ${this.apiKey}`),await fetch(this.url,{method:"POST",headers:i,body:r,keepalive:!0})}catch{}}sendBeacon(t){if(typeof navigator>"u"||!navigator.sendBeacon)return!1;try{let r=new Blob([JSON.stringify({events:t})],{type:"application/json"});return navigator.sendBeacon(this.url,r)}catch{return!1}}},qt=null;function Zf(e={}){return qt&&qt.dispose(),qt=new Vt(e),qt}function Yf(){return qt}function Xf(){qt?.dispose(),qt=null}var Gt=class{constructor(t){Q(this,"serverUrl");Q(this,"apiKey");Q(this,"onTelemetry");this.serverUrl=t.serverUrl,this.apiKey=t.apiKey,this.onTelemetry=t.onTelemetry}async*stream(t,r,i={}){let n=new AbortController,a=i.signal?this.combineSignals(i.signal,n.signal):n.signal,s=`${this.serverUrl}/api/v1/models/${encodeURIComponent(t)}/stream`,u={"Content-Type":"application/json",Accept:"text/event-stream"};this.apiKey&&(u.Authorization=`Bearer ${this.apiKey}`);let l=performance.now(),d=null,f=0,h=0;this.onTelemetry?.({type:"streaming_start",model:t,metadata:{modality:i.modality??"text"},timestamp:Date.now()});let g;try{g=await fetch(s,{method:"POST",headers:u,body:JSON.stringify({input:r,modality:i.modality??"text",...i.params}),signal:a})}catch($){throw this.onTelemetry?.({type:"streaming_error",model:t,metadata:{error:String($)},timestamp:Date.now()}),new X("NETWORK_ERROR",`Streaming request failed: ${String($)}`,$)}if(!g.ok)throw new X("INFERENCE_FAILED",`Streaming inference failed: HTTP ${g.status}`);if(!g.body)throw new X("INFERENCE_FAILED","Server did not return a streaming body.");let w=g.body.getReader(),y=new TextDecoder,v="";try{for(;;){let{done:$,value:_}=await w.read();if($)break;v+=y.decode(_,{stream:!0});let I=v.split(`
|
|
2790
|
+
`);v=I.pop()??"";for(let k of I){if(!k.startsWith("data: "))continue;let E=k.slice(6).trim();if(E==="[DONE]")break;let C;try{C=JSON.parse(E)}catch{continue}f++,h+=E.length,d===null&&(d=performance.now()-l,this.onTelemetry?.({type:"streaming_chunk",model:t,durationMs:d,metadata:{chunkIndex:0,ttfc:!0},timestamp:Date.now()})),yield C}}}finally{w.releaseLock()}let T=performance.now()-l;return this.onTelemetry?.({type:"streaming_complete",model:t,durationMs:T,metadata:{chunkCount:f,totalBytes:h,ttfcMs:d},timestamp:Date.now()}),{totalChunks:f,totalBytes:h,durationMs:T,ttfcMs:d??T}}combineSignals(...t){let r=new AbortController;for(let i of t){if(i.aborted)return r.abort(i.reason),r.signal;i.addEventListener("abort",()=>r.abort(i.reason),{once:!0})}return r.signal}};var ci=class{constructor(t){Q(this,"options");Q(this,"cache");Q(this,"loader");Q(this,"engine");Q(this,"telemetry",null);Q(this,"loaded",!1);Q(this,"disposed",!1);this.options={telemetry:!1,cacheStrategy:"cache-api",...t},this.cache=qr(this.options.cacheStrategy),this.loader=new Jt(this.options,this.cache),this.engine=new Xt,this.options.telemetry&&(this.telemetry=new Vt({url:this.options.telemetryUrl,apiKey:this.options.apiKey}))}async load(){this.ensureNotDisposed();let t=performance.now(),r=await this.loader.isCached(),i=await this.loader.load();await this.engine.createSession(i,this.options.backend),this.loaded=!0;let n=performance.now()-t;this.trackEvent({type:"model_load",model:this.options.model,durationMs:n,metadata:{backend:this.engine.activeBackend,cached:r,sizeBytes:i.byteLength},timestamp:Date.now()}),r?this.trackEvent({type:"cache_hit",model:this.options.model,timestamp:Date.now()}):this.trackEvent({type:"cache_miss",model:this.options.model,timestamp:Date.now()})}async predict(t){this.ensureReady();let r=this.prepareTensors(t),i=await this.engine.run(r);return this.trackEvent({type:"inference",model:this.options.model,durationMs:i.latencyMs,metadata:{backend:this.engine.activeBackend},timestamp:Date.now()}),i}async predictBatch(t){this.ensureReady();let r=performance.now(),i=[];for(let a of t){let s=this.prepareTensors(a),u=await this.engine.run(s);i.push(u)}let n=performance.now()-r;return this.trackEvent({type:"inference",model:this.options.model,durationMs:n,metadata:{backend:this.engine.activeBackend,batchSize:t.length},timestamp:Date.now()}),i}async chat(t,r={}){if(this.ensureReady(),!this.options.serverUrl)throw new X("INFERENCE_FAILED","chat() requires serverUrl to be configured.");let i=new Gt({serverUrl:this.options.serverUrl,apiKey:this.options.apiKey,onTelemetry:u=>this.trackEvent(u)}),n=performance.now(),a="",s=i.stream(this.options.model,{messages:t,temperature:r.temperature,max_tokens:r.maxTokens,top_p:r.topP},{modality:"text",signal:r.signal});for await(let u of s)typeof u.data=="string"&&(a+=u.data);return{message:{role:"assistant",content:a},latencyMs:performance.now()-n}}async*chatStream(t,r={}){if(this.ensureReady(),!this.options.serverUrl)throw new X("INFERENCE_FAILED","chatStream() requires serverUrl to be configured.");let n=new Gt({serverUrl:this.options.serverUrl,apiKey:this.options.apiKey,onTelemetry:a=>this.trackEvent(a)}).stream(this.options.model,{messages:t,temperature:r.temperature,max_tokens:r.maxTokens,top_p:r.topP},{modality:"text",signal:r.signal});for await(let a of n)yield{index:a.index,content:typeof a.data=="string"?a.data:JSON.stringify(a.data),done:a.done,role:"assistant"}}async isCached(){return this.ensureNotDisposed(),this.loader.isCached()}async clearCache(){return this.ensureNotDisposed(),this.loader.clearCache()}async cacheInfo(){return this.ensureNotDisposed(),this.loader.getCacheInfo()}get activeBackend(){return this.engine.activeBackend}get inputNames(){return this.ensureReady(),this.engine.inputNames}get outputNames(){return this.ensureReady(),this.engine.outputNames}get isLoaded(){return this.loaded}dispose(){this.disposed||(this.disposed=!0,this.loaded=!1,this.engine.dispose(),this.telemetry?.dispose(),this.telemetry=null)}ensureNotDisposed(){if(this.disposed)throw new X("SESSION_DISPOSED","This Octomil instance has been disposed. Create a new one.")}ensureReady(){if(this.ensureNotDisposed(),!this.loaded)throw new X("NOT_LOADED","Model not loaded. Call load() before predict() or chat().")}prepareTensors(t){if(this.isNamedTensors(t))return t;if("raw"in t&&"dims"in t){let r=this.engine.inputNames[0];if(!r)throw new X("INVALID_INPUT","Model has no input tensors defined.");return{[r]:{data:t.raw,dims:t.dims}}}if("text"in t){let r=this.engine.inputNames[0];if(!r)throw new X("INVALID_INPUT","Model has no input tensors defined.");let i=new Int32Array(Array.from(t.text).map(n=>n.codePointAt(0)??0));return{[r]:{data:i,dims:[1,i.length]}}}if("image"in t)return this.imageToTensors(t.image);throw new X("INVALID_INPUT","Unrecognised PredictInput format. Provide named tensors, { text }, { image }, or { raw, dims }.")}isNamedTensors(t){if("text"in t||"image"in t||"raw"in t)return!1;let r=Object.values(t)[0];return r!==void 0&&typeof r=="object"&&"data"in r&&"dims"in r}imageToTensors(t){let r;if(t instanceof ImageData)r=t;else{let d=t instanceof HTMLCanvasElement?t:(()=>{let h=document.createElement("canvas");return h.width=t.naturalWidth||t.width,h.height=t.naturalHeight||t.height,h.getContext("2d").drawImage(t,0,0),h})(),f=d.getContext("2d");if(!f)throw new X("INVALID_INPUT","Could not get 2D context from canvas.");r=f.getImageData(0,0,d.width,d.height)}let{width:i,height:n,data:a}=r,s=i*n,u=new Float32Array(3*s);for(let d=0;d<s;d++)u[d]=a[d*4]/255,u[s+d]=a[d*4+1]/255,u[2*s+d]=a[d*4+2]/255;let l=this.engine.inputNames[0];if(!l)throw new X("INVALID_INPUT","Model has no input tensors defined.");return{[l]:{data:u,dims:[1,3,n,i]}}}trackEvent(t){this.telemetry?.track(t)}};var Jf=3e4,hi=class{constructor(t){Q(this,"serverUrl");Q(this,"apiKey");Q(this,"token",null);Q(this,"deviceId",null);Q(this,"refreshTimer",null);Q(this,"disposed",!1);this.serverUrl=t.serverUrl,this.apiKey=t.apiKey}async bootstrap(t){this.ensureNotDisposed(),this.deviceId=await this.generateDeviceId();let r=this.collectDeviceInfo(),i=await this.request("/api/v1/devices/register",{method:"POST",body:JSON.stringify({org_id:t,device_id:this.deviceId,platform:"browser",info:r})});if(!i.ok)throw new X("NETWORK_ERROR",`Device registration failed: HTTP ${i.status}`);let n=await i.json();this.token={accessToken:n.token,refreshToken:n.refresh_token,expiresAt:new Date(n.expires_at).getTime()},this.scheduleRefresh()}async getToken(){if(this.ensureNotDisposed(),!this.token)throw new X("NETWORK_ERROR","Not authenticated. Call bootstrap() first.");return this.isTokenExpiringSoon()&&await this.refreshToken(),this.token.accessToken}async refreshToken(){if(this.ensureNotDisposed(),!this.token)throw new X("NETWORK_ERROR","No token to refresh. Call bootstrap() first.");let t=await this.request("/api/v1/auth/refresh",{method:"POST",body:JSON.stringify({refresh_token:this.token.refreshToken,device_id:this.deviceId})});if(!t.ok)throw this.token=null,new X("NETWORK_ERROR",`Token refresh failed: HTTP ${t.status}`);let r=await t.json();this.token={accessToken:r.token,refreshToken:r.refresh_token,expiresAt:new Date(r.expires_at).getTime()},this.scheduleRefresh()}async revokeToken(){if(this.ensureNotDisposed(),!!this.token)try{await this.request("/api/v1/auth/revoke",{method:"POST",body:JSON.stringify({token:this.token.accessToken,device_id:this.deviceId})})}finally{this.clearState()}}get isAuthenticated(){return this.token!==null&&!this.isTokenExpired()}get currentDeviceId(){return this.deviceId}dispose(){this.disposed||(this.disposed=!0,this.clearRefreshTimer())}async request(t,r){let i={"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`};return fetch(`${this.serverUrl}${t}`,{...r,headers:{...i,...r.headers}})}isTokenExpired(){return this.token?Date.now()>=this.token.expiresAt:!0}isTokenExpiringSoon(){return this.token?Date.now()>=this.token.expiresAt-Jf:!0}scheduleRefresh(){if(this.clearRefreshTimer(),!this.token)return;let t=Math.max(0,this.token.expiresAt-Date.now()-Jf);this.refreshTimer=setTimeout(()=>{this.refreshToken().catch(()=>{})},t)}clearRefreshTimer(){this.refreshTimer!==null&&(clearTimeout(this.refreshTimer),this.refreshTimer=null)}clearState(){this.token=null,this.clearRefreshTimer()}async generateDeviceId(){let t=[typeof navigator<"u"?navigator.userAgent:"unknown",typeof screen<"u"?`${screen.width}x${screen.height}`:"0x0",typeof Intl<"u"?Intl.DateTimeFormat().resolvedOptions().timeZone:"UTC",typeof navigator<"u"?navigator.language:"en"].join("|"),r=new TextEncoder().encode(t),i=await crypto.subtle.digest("SHA-256",r),n=new Uint8Array(i);return Array.from(n).map(a=>a.toString(16).padStart(2,"0")).join("")}collectDeviceInfo(){return{userAgent:typeof navigator<"u"?navigator.userAgent:"unknown",language:typeof navigator<"u"?navigator.language:"en",screenWidth:typeof screen<"u"?screen.width:0,screenHeight:typeof screen<"u"?screen.height:0,timezone:typeof Intl<"u"?Intl.DateTimeFormat().resolvedOptions().timeZone:"UTC",webgpu:typeof navigator<"u"&&"gpu"in navigator}}ensureNotDisposed(){if(this.disposed)throw new X("SESSION_DISPOSED","DeviceAuthManager has been disposed.")}};async function Oa(e){let t=await crypto.subtle.digest("SHA-256",e),r=new Uint8Array(t);return Array.from(r).map(i=>i.toString(16).padStart(2,"0")).join("")}async function Ra(e,t){return await Oa(e)===t.toLowerCase()}async function em(e,t){if(!await Ra(e,t))throw new X("MODEL_LOAD_FAILED","Model integrity check failed: SHA-256 hash mismatch. The downloaded model may be corrupted or tampered with.")}var er=class{static computeDelta(t,r){let i={};for(let n of Object.keys(t)){let a=t[n],s=r[n];if(!a||!s||a.length!==s.length)throw new X("INVALID_INPUT",`Weight dimension mismatch for "${n}".`);let u=new Float32Array(a.length);for(let l=0;l<a.length;l++)u[l]=s[l]-a[l];i[n]=u}return i}static applyDelta(t,r){let i={};for(let n of Object.keys(t)){let a=t[n],s=r[n];if(!a)continue;if(!s||a.length!==s.length){i[n]=new Float32Array(a);continue}let u=new Float32Array(a.length);for(let l=0;l<a.length;l++)u[l]=a[l]+s[l];i[n]=u}return i}static l2Norm(t){let r=0;for(let i of Object.values(t))if(i)for(let n=0;n<i.length;n++)r+=i[n]*i[n];return Math.sqrt(r)}},fi=class{constructor(t){Q(this,"serverUrl");Q(this,"apiKey");Q(this,"deviceId");Q(this,"onTelemetry");this.serverUrl=t.serverUrl,this.apiKey=t.apiKey,this.deviceId=t.deviceId,this.onTelemetry=t.onTelemetry}async getTrainingRound(t){let r=await this.request(`/api/v1/federations/${encodeURIComponent(t)}/rounds/current`);if(!r.ok)throw new X("NETWORK_ERROR",`Failed to fetch training round: HTTP ${r.status}`);return await r.json()}async joinRound(t,r){let i=await this.request(`/api/v1/federations/${encodeURIComponent(t)}/rounds/${encodeURIComponent(r)}/join`,{method:"POST",body:JSON.stringify({device_id:this.deviceId})});if(!i.ok)throw new X("NETWORK_ERROR",`Failed to join round: HTTP ${i.status}`)}async train(t,r){let i=performance.now(),n=this.cloneWeights(t);for(let u=0;u<r.epochs;u++)n=(await r.onTrainStep(n,{epoch:u,batchSize:r.batchSize,learningRate:r.learningRate})).weights;let a=er.computeDelta(t,n),s=performance.now()-i;return this.onTelemetry?.({type:"training_complete",model:r.modelId??"unknown",durationMs:s,metadata:{epochs:r.epochs,deltaNorm:er.l2Norm(a)},timestamp:Date.now()}),{finalWeights:n,delta:a}}async submitUpdate(t,r,i,n){let a={};for(let[u,l]of Object.entries(i))l&&(a[u]={data:Array.from(l),shape:[l.length]});let s=await this.request(`/api/v1/federations/${encodeURIComponent(t)}/rounds/${encodeURIComponent(r)}/submit`,{method:"POST",body:JSON.stringify({device_id:this.deviceId,delta:a,metrics:n})});if(!s.ok)throw new X("NETWORK_ERROR",`Failed to submit update: HTTP ${s.status}`)}async request(t,r){let i={"Content-Type":"application/json"};return this.apiKey&&(i.Authorization=`Bearer ${this.apiKey}`),fetch(`${this.serverUrl}${t}`,{...r,headers:{...i,...r?.headers}})}cloneWeights(t){let r={};for(let[i,n]of Object.entries(t))n&&(r[i]=new Float32Array(n));return r}};var Sr=class{constructor(){Q(this,"keyPair",null)}async generateKeyPair(){return this.keyPair=await crypto.subtle.generateKey({name:"ECDH",namedCurve:"P-256"},!0,["deriveBits"]),{publicKey:await crypto.subtle.exportKey("jwk",this.keyPair.publicKey)}}async deriveSharedSecret(t){if(!this.keyPair)throw new Error("Call generateKeyPair() first.");let r=await crypto.subtle.importKey("jwk",t,{name:"ECDH",namedCurve:"P-256"},!1,[]);return crypto.subtle.deriveBits({name:"ECDH",public:r},this.keyPair.privateKey,256)}async createMask(t,r){let i=await crypto.subtle.importKey("raw",t,"HKDF",!1,["deriveBits"]),n=r*4*8,a=await crypto.subtle.deriveBits({name:"HKDF",hash:"SHA-256",salt:new Uint8Array(32),info:new TextEncoder().encode("octomil-secagg-mask")},i,Math.min(n,8160)),s=new Float32Array(r),u=new Float32Array(a);for(let l=0;l<r;l++)s[l]=u[l%u.length];return s}maskUpdate(t,r){let i={};for(let[n,a]of Object.entries(t)){if(!a)continue;let s=new Float32Array(a),u=r.get(n);if(u)for(let l=0;l<s.length;l++)s[l]=s[l]+(u[l]??0);i[n]=s}return i}unmask(t,r){let i={};for(let[n,a]of Object.entries(t)){if(!a)continue;let s=new Float32Array(a),u=r.get(n);if(u)for(let l=0;l<s.length;l++)s[l]=s[l]-(u[l]??0);i[n]=s}return i}},qe=2147483647;function tm(e,t,r){let i=1;for(e=e%r;t>0;)t%2===1&&(i=Number(BigInt(i)*BigInt(e)%BigInt(r))),t=Math.floor(t/2),e=Number(BigInt(e)*BigInt(e)%BigInt(r));return i}function Q0(e,t){return tm(e,t-2,t)}function Ma(e,t,r){let i=[e%qe];for(let a=1;a<t;a++){let s=new Uint32Array(1);crypto.getRandomValues(s),i.push(s[0]%qe)}let n=[];for(let a=1;a<=r;a++){let s=0;for(let u=0;u<i.length;u++)s=Number((BigInt(s)+BigInt(i[u])*BigInt(tm(a,u,qe)))%BigInt(qe));n.push({x:a,y:s})}return n}function Ba(e){let t=0,r=e.length;for(let i=0;i<r;i++){let n=1,a=1;for(let u=0;u<r;u++)i!==u&&(n=Number(BigInt(n)*BigInt(qe-e[u].x)%BigInt(qe)),a=Number(BigInt(a)*BigInt((e[i].x-e[u].x+qe)%qe)%BigInt(qe)));let s=Number(BigInt(n)*BigInt(Q0(a,qe))%BigInt(qe));t=Number((BigInt(t)+BigInt(e[i].y)*BigInt(s))%BigInt(qe))}return t}var mi=class extends Sr{constructor(r){super();Q(this,"threshold");this.threshold=r}splitSecret(r,i){return Ma(r,this.threshold,i)}reconstructSecret(r){if(r.length<this.threshold)throw new Error(`Need at least ${this.threshold} shares, got ${r.length}.`);return Ba(r.slice(0,this.threshold))}};function rm(e,t){let r=0;for(let s of Object.values(e))if(s)for(let u=0;u<s.length;u++)r+=s[u]*s[u];let i=Math.sqrt(r);if(i<=t)return e;let n=t/i,a={};for(let[s,u]of Object.entries(e)){if(!u)continue;let l=new Float32Array(u.length);for(let d=0;d<u.length;d++)l[d]=u[d]*n;a[s]=l}return a}function im(e,t,r,i){let n=r*Math.sqrt(2*Math.log(1.25/i))/t,a={};for(let[s,u]of Object.entries(e)){if(!u)continue;let l=new Float32Array(u.length);for(let d=0;d<u.length;d++)l[d]=u[d]+Z0()*n;a[s]=l}return a}function Z0(){let e,t;do e=Math.random(),t=Math.random();while(e===0);return Math.sqrt(-2*Math.log(e))*Math.cos(2*Math.PI*t)}function nm(e,t=8){let r=t===8?127:32767,i={};for(let[n,a]of Object.entries(e)){if(!a)continue;let s=0;for(let d=0;d<a.length;d++){let f=Math.abs(a[d]);f>s&&(s=f)}let u=s>0?s/r:1,l=t===8?new Int8Array(a.length):new Int16Array(a.length);for(let d=0;d<a.length;d++)l[d]=Math.round(a[d]/u);i[n]={data:l,scale:u,zeroPoint:0}}return i}function am(e){let t={};for(let[r,i]of Object.entries(e)){if(!i)continue;let n=new Float32Array(i.data.length);for(let a=0;a<i.data.length;a++)n[a]=(i.data[a]-i.zeroPoint)*i.scale;t[r]=n}return t}var gi=class{constructor(t){Q(this,"serverUrl");Q(this,"apiKey");Q(this,"cacheTtlMs");Q(this,"onTelemetry");Q(this,"configCache",new Map);this.serverUrl=t.serverUrl,this.apiKey=t.apiKey,this.cacheTtlMs=t.cacheTtlMs??5*60*1e3,this.onTelemetry=t.onTelemetry}async resolveVersion(t,r){let i=await this.getRolloutConfig(t),n=i.versions.find(s=>s.status==="canary");if(n&&this.isInCanaryGroup(t,r,n.percentage))return n.version;let a=i.versions.find(s=>s.status==="active");if(a)return a.version;throw new X("MODEL_NOT_FOUND",`No active version found for model "${t}".`)}async getRolloutConfig(t){let r=this.configCache.get(t);if(r&&Date.now()-r.fetchedAt<this.cacheTtlMs)return r.config;let i=`${this.serverUrl}/api/v1/models/${encodeURIComponent(t)}/rollout`,n={Accept:"application/json"};this.apiKey&&(n.Authorization=`Bearer ${this.apiKey}`);let a=await fetch(i,{headers:n});if(!a.ok)throw new X("NETWORK_ERROR",`Failed to fetch rollout config: HTTP ${a.status}`);let s=await a.json();return this.configCache.set(t,{config:s,fetchedAt:Date.now()}),s}isInCanaryGroup(t,r,i){return Y0(r+t)<i}async getAvailableVersions(t){return(await this.getRolloutConfig(t)).versions}async reportRolloutStatus(t,r,i){let n=`${this.serverUrl}/api/v1/models/${encodeURIComponent(t)}/rollout/status`,a={"Content-Type":"application/json"};this.apiKey&&(a.Authorization=`Bearer ${this.apiKey}`),await fetch(n,{method:"POST",headers:a,body:JSON.stringify({version:r,status:i})}),this.onTelemetry?.({type:"rollout_status",model:t,metadata:{version:r,status:i},timestamp:Date.now()})}clearCache(){this.configCache.clear()}};function Y0(e){let t=5381;for(let r=0;r<e.length;r++)t=(t<<5)+t+e.charCodeAt(r)|0;return Math.abs(t)%100}var yi=class{constructor(t){Q(this,"serverUrl");Q(this,"apiKey");Q(this,"cacheTtlMs");Q(this,"onTelemetry");Q(this,"experimentsCache",null);this.serverUrl=t.serverUrl,this.apiKey=t.apiKey,this.cacheTtlMs=t.cacheTtlMs??5*60*1e3,this.onTelemetry=t.onTelemetry}async getActiveExperiments(){if(this.experimentsCache&&Date.now()-this.experimentsCache.fetchedAt<this.cacheTtlMs)return this.experimentsCache.experiments;let t=`${this.serverUrl}/api/v1/experiments?status=active`,r={Accept:"application/json"};this.apiKey&&(r.Authorization=`Bearer ${this.apiKey}`);let i=await fetch(t,{headers:r});if(!i.ok)throw new X("NETWORK_ERROR",`Failed to fetch experiments: HTTP ${i.status}`);let n=await i.json();return this.experimentsCache={experiments:n.experiments,fetchedAt:Date.now()},n.experiments}async getExperimentConfig(t){let r=`${this.serverUrl}/api/v1/experiments/${encodeURIComponent(t)}`,i={Accept:"application/json"};this.apiKey&&(i.Authorization=`Bearer ${this.apiKey}`);let n=await fetch(r,{headers:i});if(!n.ok)throw new X("NETWORK_ERROR",`Failed to fetch experiment: HTTP ${n.status}`);return await n.json()}getVariant(t,r){if(t.variants.length===0)return null;let i=X0(r+t.id),n=0;for(let a of t.variants)if(n+=a.trafficPercentage,i<n)return a;return t.variants[t.variants.length-1]??null}isEnrolled(t,r){return this.getVariant(t,r)!==null}async resolveModelExperiment(t,r){let i=await this.getActiveExperiments();for(let n of i){if(!n.variants.some(u=>u.modelId===t))continue;let s=this.getVariant(n,r);if(s)return{experiment:n,variant:s}}return null}async trackMetric(t,r,i,n){let a=`${this.serverUrl}/api/v1/experiments/${encodeURIComponent(t)}/metrics`,s={"Content-Type":"application/json"};this.apiKey&&(s.Authorization=`Bearer ${this.apiKey}`),await fetch(a,{method:"POST",headers:s,body:JSON.stringify({metric_name:r,value:i,device_id:n,timestamp:Date.now()})}),this.onTelemetry?.({type:"experiment_metric",model:t,metadata:{metricName:r,value:i},timestamp:Date.now()})}clearCache(){this.experimentsCache=null}};function X0(e){let t=5381;for(let r=0;r<e.length;r++)t=(t<<5)+t+e.charCodeAt(r)|0;return Math.abs(t)%100}return xy(J0);})();
|
|
2791
|
+
/*! Bundled license information:
|
|
2792
|
+
|
|
2793
|
+
onnxruntime-web/dist/ort.bundle.min.mjs:
|
|
2794
|
+
(*!
|
|
2795
|
+
* ONNX Runtime Web v1.24.2
|
|
2796
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2797
|
+
* Licensed under the MIT License.
|
|
2798
|
+
*)
|
|
2799
|
+
(**
|
|
2800
|
+
* @license
|
|
2801
|
+
* Copyright 2021 Google LLC. All Rights Reserved.
|
|
2802
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2803
|
+
* you may not use this file except in compliance with the License.
|
|
2804
|
+
* You may obtain a copy of the License at
|
|
2805
|
+
*
|
|
2806
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2807
|
+
*
|
|
2808
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2809
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2810
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2811
|
+
* See the License for the specific language governing permissions and
|
|
2812
|
+
* limitations under the License.
|
|
2813
|
+
* =============================================================================
|
|
2814
|
+
*)
|
|
2815
|
+
(**
|
|
2816
|
+
* @license
|
|
2817
|
+
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
2818
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2819
|
+
* you may not use this file except in compliance with the License.
|
|
2820
|
+
* You may obtain a copy of the License at
|
|
2821
|
+
*
|
|
2822
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2823
|
+
*
|
|
2824
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2825
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2826
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2827
|
+
* See the License for the specific language governing permissions and
|
|
2828
|
+
* limitations under the License.
|
|
2829
|
+
* =============================================================================
|
|
2830
|
+
*)
|
|
2831
|
+
(**
|
|
2832
|
+
* @license
|
|
2833
|
+
* Copyright 2019 Google LLC. All Rights Reserved.
|
|
2834
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2835
|
+
* you may not use this file except in compliance with the License.
|
|
2836
|
+
* You may obtain a copy of the License at
|
|
2837
|
+
*
|
|
2838
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2839
|
+
*
|
|
2840
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2841
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2842
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2843
|
+
* See the License for the specific language governing permissions and
|
|
2844
|
+
* limitations under the License.
|
|
2845
|
+
* =============================================================================
|
|
2846
|
+
*)
|
|
2847
|
+
*/
|
|
2848
|
+
var Octomil=__Octomil_ns.Octomil;Object.assign(Octomil,__Octomil_ns);
|
|
2849
|
+
//# sourceMappingURL=octomil.min.js.map
|