human/dist/tfjs.esm.js

7209 lines
1.3 MiB
JavaScript
Raw Normal View History

2022-02-10 18:27:21 +01:00
/*
Human
homepage: <https://github.com/vladmandic/human>
author: <https://github.com/vladmandic>'
*/
2022-05-30 03:12:18 +02:00
var ZT=Object.create,Xd=Object.defineProperty,JT=Object.getOwnPropertyDescriptor,Jw=Object.getOwnPropertyNames,e$=Object.getPrototypeOf,t$=Object.prototype.hasOwnProperty,n$=e=>Xd(e,"__esModule",{value:!0}),zt=(e,t)=>function(){return t||(0,e[Jw(e)[0]])((t={exports:{}}).exports,t),t.exports},Ee=(e,t)=>{for(var n in t)Xd(e,n,{get:t[n],enumerable:!0})},s$=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Jw(t))!t$.call(e,r)&&(n||r!=="default")&&Xd(e,r,{get:()=>t[r],enumerable:!(s=JT(t,r))||s.enumerable});return e},wa=(e,t)=>s$(n$(Xd(e!=null?ZT(e$(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),r$=zt({"src/node_modules/long/src/long.js"(e,t){t.exports=s;var n=null;try{n=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch(O){}function s(O,T,M){this.low=O|0,this.high=T|0,this.unsigned=!!M}s.prototype.__isLong__,Object.defineProperty(s.prototype,"__isLong__",{value:!0});function r(O){return(O&&O.__isLong__)===!0}s.isLong=r;var a={},i={};function o(O,T){var M,W,j;return T?(O>>>=0,(j=0<=O&&O<256)&&(W=i[O],W)?W:(M=l(O,(O|0)<0?-1:0,!0),j&&(i[O]=M),M)):(O|=0,(j=-128<=O&&O<128)&&(W=a[O],W)?W:(M=l(O,O<0?-1:0,!1),j&&(a[O]=M),M))}s.fromInt=o;function u(O,T){if(isNaN(O))return T?x:v;if(T){if(O<0)return x;if(O>=g)return E}else{if(O<=-b)return P;if(O+1>=b)return R}return O<0?u(-O,T).neg():l(O%m|0,O/m|0,T)}s.fromNumber=u;function l(O,T,M){return new s(O,T,M)}s.fromBits=l;var c=Math.pow;function p(O,T,M){if(O.length===0)throw Error("empty string");if(O==="NaN"||O==="Infinity"||O==="+Infinity"||O==="-Infinity")return v;if(typeof T=="number"?(M=T,T=!1):T=!!T,M=M||10,M<2||36<M)throw RangeError("radix");var W;if((W=O.indexOf("-"))>0)throw Error("interior hyphen");if(W===0)return p(O.substring(1),T,M).neg();for(var j=u(c(M,8)),Y=v,X=0;X<O.length;X+=8){var Z=Math.min(8,O.length-X),ne=parseInt(O.substring(X,X+Z),M);if(Z<8){var ee=u(c(M,Z));Y=Y.mul(ee).add(u(ne))}else Y=Y.mul(j),Y=Y.add(u(ne))}return Y.unsigned=T,Y}s.fromString=p;function d(O,T){return typeof O=="number"?u(O,T):typeof O=="string"?p(O,T):l(O.low,O.high,typeof T=="boolean"?T:O.unsigned)}s.fromValue=d;var h=1<<16,f=1<<24,m=h*h,g=m*m,b=g/2,y=o(f),v=o(0);s.ZERO=v;var x=o(0,!0);s.UZERO=x;var k=o(1);s.ONE=k;var I=o(1,!0);s.UONE=I;var $=o(-1);s.NEG_ONE=$;var R=l(-1,2147483647,!1);s.MAX_VALUE=R;var E=l(-1,-1,!0);s.MAX_UNSIGNED_VALUE=E;var P=l(0,-2147483648,!1);s.MIN_VALUE=P;var A=s.prototype;A.toInt=function(){return this.unsigned?this.low>>>0:this.low},A.toNumber=function(){return this.unsigned?(this.high>>>0)*m+(this.low>>>0):this.high*m+(this.low>>>0)},A.toString=function(T){if(T=T||10,T<2||36<T)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative())if(this.eq(P)){var M=u(T),W=this.div(M),j=W.mul(M).sub(this);return W.toString(T)+j.toInt().toString(T)}else return"-"+this.neg().toString(T);for(var Y=u(c(T,6),this.unsigned),X=this,Z="";;){var ne=X.div(Y),ee=X.sub(ne.mul(Y)).toInt()>>>0,se=ee.toString(T);if(X=ne,X.isZero())return se+Z;for(;se.length<6;)se="0"+se;Z=""+se+Z}},A.getHighBits=function(){return this.high},A.getHighBitsUnsigned=function(){return this.high>>>0},A.getLowBits=function(){return this.low},A.getLow
2022-04-21 15:39:40 +02:00
`),Z=N=>W.writeSync(2,N+`
2022-05-30 03:12:18 +02:00
`));var ne=d.print||X,ee=d.printErr||Z;Object.assign(d,g),g=null,d.arguments&&(b=d.arguments),d.thisProgram&&(y=d.thisProgram),d.quit&&(v=d.quit);var se=4;function te(N){te.shown||(te.shown={}),te.shown[N]||(te.shown[N]=1,ee(N))}function oe(N,D){if(typeof WebAssembly.Function=="function"){for(var B={i:"i32",j:"i64",f:"f32",d:"f64"},Q={parameters:[],results:D[0]=="v"?[]:[B[D[0]]]},ue=1;ue<D.length;++ue)Q.parameters.push(B[D[ue]]);return new WebAssembly.Function(Q,N)}var he=[1,0,1,96],ye=D.slice(0,1),Te=D.slice(1),bt={i:127,j:126,f:125,d:124};he.push(Te.length);for(var ue=0;ue<Te.length;++ue)he.push(bt[Te[ue]]);ye=="v"?he.push(0):he=he.concat([1,bt[ye]]),he[1]=he.length-2;var us=new Uint8Array([0,97,115,109,1,0,0,0].concat(he,[2,7,1,1,101,1,102,0,0,7,5,1,1,102,0,0])),ls=new WebAssembly.Module(us),Uc=new WebAssembly.Instance(ls,{e:{f:N}}),Nu=Uc.exports.f;return Nu}var ae=[],de;function me(){if(ae.length)return ae.pop();try{Fn.grow(1)}catch(N){throw N instanceof RangeError?"Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.":N}return Fn.length-1}function ke(N,D){for(var B=N;B<N+D;B++){var Q=_i(B);Q&&de.set(Q,B)}}var Ie=0,Re=N=>{Ie=N},Pe=Atomics.load,Xe=Atomics.store,Je=Atomics.compareExchange,Ye;d.wasmBinary&&(Ye=d.wasmBinary);var tt=d.noExitRuntime||!0;typeof WebAssembly!="object"&&Ni("no native wasm support detected");var Ce,ut,at=!1,Zt;function Nt(N,D){N||Ni(D)}function In(N){var D=d["_"+N];return D}function Et(N,D,B,Q,ue){var he={string:function(Tn){var Pi=0;if(Tn!=null&&Tn!==0){var ux=(Tn.length<<2)+1;Pi=Oi(ux),Ms(Tn,Pi,ux)}return Pi},array:function(Tn){var Pi=Oi(Tn.length);return Ls(Tn,Pi),Pi}};function ye(Tn){return D==="string"?en(Tn):D==="boolean"?Boolean(Tn):Tn}var Te=In(N),bt=[],us=0;if(Q)for(var ls=0;ls<Q.length;ls++){var Uc=he[B[ls]];Uc?(us===0&&(us=Bf()),bt[ls]=Uc(Q[ls])):bt[ls]=Q[ls]}var Nu=Te.apply(null,bt);function QT(Tn){return us!==0&&Lc(us),ye(Tn)}return Nu=QT(Nu),Nu}function Jt(N,D,B,Q){B=B||[];var ue=B.every(function(ye){return ye==="number"}),he=D!=="string";return he&&ue&&!Q?In(N):function(){return Et(N,D,B,arguments,Q)}}var Cn=1;function Nn(N){var D=new TextDecoder(N);this.decode=B=>(B.buffer instanceof SharedArrayBuffer&&(B=new Uint8Array(B)),D.decode.call(D,B))}var Xt=typeof TextDecoder!="undefined"?new Nn("utf8"):void 0;function Dn(N,D,B){for(var Q=D+B,ue=D;N[ue]&&!(ue>=Q);)++ue;if(ue-D>16&&N.subarray&&Xt)return Xt.decode(N.subarray(D,ue));for(var he="";D<ue;){var ye=N[D++];if(!(ye&128)){he+=String.fromCharCode(ye);continue}var Te=N[D++]&63;if((ye&224)==192){he+=String.fromCharCode((ye&31)<<6|Te);continue}var bt=N[D++]&63;if((ye&240)==224?ye=(ye&15)<<12|Te<<6|bt:ye=(ye&7)<<18|Te<<12|bt<<6|N[D++]&63,ye<65536)he+=String.fromCharCode(ye);else{var us=ye-65536;he+=String.fromCharCode(55296|us>>10,56320|us&1023)}}return he}function en(N,D){return N?Dn(i(),N,D):""}function zs(N,D,B,Q){if(!(Q>0))return 0;for(var ue=B,he=B+Q-1,ye=0;ye<N.length;++ye){var Te=N.charCodeAt(ye);if(Te>=55296&&Te<=57343){var bt=N.charCodeAt(++ye);Te=65536+((Te&1023)<<10)|bt&1023}if(Te<=127){if(B>=he)break;D[B++]=Te}else if(Te<=2047){if(B+1>=he)break;D[B++]=192|Te>>6,D[B++]=128|Te&63}else if(Te<=65535){if(B+2>=he)break;D[B++]=224|Te>>12,D[B++]=128|Te>>6&63,D[B++]=128|Te&63}else{if(B+3>=he)break;D[B++]=240|Te>>18,D[B++]=128|Te>>12&63,D[B++]=128|Te>>6&63,D[B++]=128|Te&63}}return D[B]=0,B-ue}function Ms(N,D,B){return zs(N,i(),D,B)}function Si(N){for(var D=0,B=0;B<N.length;++B){var Q=N.charCodeAt(B);Q>=55296&&Q<=57343&&(Q=65536+((Q&1023)<<10)|N.charCodeAt(++B)&1023),Q<=127?++D:Q<=2047?D+=2:Q<=65535?D+=3:D+=4}return D}var Zs=typeof TextDecoder!="undefined"?new Nn("utf-16le"):void 0;function Ls(N,D){a().set(N,D)}function mu(N,D,B){for(var Q=0;Q<N.length;++Q)a()[D++>>0]=N.charCodeAt(Q);B||(a()[D>>0]=0)}function Ii(N,D){return N%D>0&&(N+=D-N%D),N}var tn,lc,cc,gu,dc,pc,Wv,hc,fc;$&&(tn=d.buffer);function rs(N){tn=N,d.HEAP8=lc=new Int8Array(N),d.HEAP16=gu=new Int16Array(N),d.HEAP32=pc=new Int32Array(N),d.HEAPU8=cc=new Uint8Array(N),d.HEAPU16=dc=new Uint16Array(N),d.HEAPU32=Wv=new Uint32Array(N),d.HEAPF32=hc=new Float32Array(N),d.HEAPF64=fc=
2022-04-21 15:39:40 +02:00
`)),u.join(`
2022-05-30 03:12:18 +02:00
`)}function o_(e,t,n,s){let r=dt(t),a=s[s.length-1],i=new Array(a).fill(0),o=t.length,u=n==="complex64"?Ru(e):e;if(o>1)for(let l=0;l<r/a;l++){let c=l*a;for(let p=0;p<a;p++)i[p]=Math.max(i[p],Eu(u[c+p],0,n).length)}return i}function Eu(e,t,n){let s;return Array.isArray(e)?s=`${parseFloat(e[0].toFixed(Uf))} + ${parseFloat(e[1].toFixed(Uf))}j`:ar(e)?s=`'${e}'`:n==="bool"?s=mk(e):s=parseFloat(e.toFixed(Uf)).toString(),Bu(s,t)}function mk(e){return e===0?"false":"true"}function rd(e,t,n,s,r,a=!0){let i=n==="complex64"?2:1,o=t[0],u=t.length;if(u===0){if(n==="complex64"){let m=Ru(e);return[Eu(m[0],0,n)]}return n==="bool"?[mk(e[0])]:[e[0].toString()]}if(u===1){if(o>hx){let g=Tu*i,b=Array.from(e.slice(0,g)),y=Array.from(e.slice((o-Tu)*i,o*i));return n==="complex64"&&(b=Ru(b),y=Ru(y)),["["+b.map((v,x)=>Eu(v,r[x],n)).join(", ")+", ..., "+y.map((v,x)=>Eu(v,r[o-Tu+x],n)).join(", ")+"]"]}let m=n==="complex64"?Ru(e):Array.from(e);return["["+m.map((g,b)=>Eu(g,r[b],n)).join(", ")+"]"]}let l=t.slice(1),c=s.slice(1),p=s[0]*i,d=[];if(o>hx){for(let m=0;m<Tu;m++){let g=m*p,b=g+p;d.push(...rd(e.slice(g,b),l,n,c,r,!1))}d.push("...");for(let m=o-Tu;m<o;m++){let g=m*p,b=g+p;d.push(...rd(e.slice(g,b),l,n,c,r,m===o-1))}}else for(let m=0;m<o;m++){let g=m*p,b=g+p;d.push(...rd(e.slice(g,b),l,n,c,r,m===o-1))}let h=u===2?",":"";d[0]="["+d[0]+h;for(let m=1;m<d.length-1;m++)d[m]=" "+d[m]+h;let f=`,
2022-04-21 15:39:40 +02:00
`;for(let m=2;m<u;m++)f+=`
2022-05-30 03:12:18 +02:00
`;return d[d.length-1]=" "+d[d.length-1]+"]"+(a?"":f),d}function Ru(e){let t=[];for(let n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}var Vt=class{constructor(e,t,n){if(this.dtype=t,this.shape=e.slice(),this.size=dt(e),n!=null){let s=n.length;F(s===this.size,()=>`Length of values '${s}' does not match the size inferred by the shape '${this.size}'.`)}if(t==="complex64")throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag).");this.values=n||rk(t,this.size),this.strides=uo(e)}set(e,...t){t.length===0&&(t=[0]),F(t.length===this.rank,()=>`The number of provided coordinates (${t.length}) must match the rank (${this.rank})`);let n=this.locToIndex(t);this.values[n]=e}get(...e){e.length===0&&(e=[0]);let t=0;for(let s of e){if(s<0||s>=this.shape[t]){let r=`Requested out of range element at ${e}. Buffer shape=${this.shape}`;throw new Error(r)}t++}let n=e[e.length-1];for(let s=0;s<e.length-1;++s)n+=this.strides[s]*e[s];return this.values[n]}locToIndex(e){if(this.rank===0)return 0;if(this.rank===1)return e[0];let t=e[e.length-1];for(let n=0;n<e.length-1;++n)t+=this.strides[n]*e[n];return t}indexToLoc(e){if(this.rank===0)return[];if(this.rank===1)return[e];let t=new Array(this.shape.length);for(let n=0;n<t.length-1;++n)t[n]=Math.floor(e/this.strides[n]),e-=t[n]*this.strides[n];return t[t.length-1]=e,t}get rank(){return this.shape.length}toTensor(){return cs().makeTensor(this.values,this.shape,this.dtype)}},cs=null,Ui=null,u_=null;function l_(e){cs=e}function c_(e){Ui=e}function d_(e){u_=e}var et=class{constructor(e,t,n,s){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=dt(e),this.strides=uo(e),this.dataId=n,this.id=s,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){let e=await this.data();return Ui.buffer(this.shape,this.dtype,e)}bufferSync(){return Ui.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return ji(this.shape,e,this.dtype==="complex64")}arraySync(){return ji(this.shape,this.dataSync(),this.dtype==="complex64")}async data(){this.throwIfDisposed();let e=cs().read(this.dataId);if(this.dtype==="string"){let t=await e;try{return t.map(n=>xd(n))}catch(n){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}}return e}dataToGPU(e){return this.throwIfDisposed(),cs().readToGPU(this.dataId,e)}dataSync(){this.throwIfDisposed();let e=cs().readSync(this.dataId);if(this.dtype==="string")try{return e.map(t=>xd(t))}catch(t){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}return e}async bytes(){this.throwIfDisposed();let e=await cs().read(this.dataId);return this.dtype==="string"?e:new Uint8Array(e.buffer)}dispose(){this.isDisposed||(cs().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(e=!1){return Ui.print(this,e)}clone(){return this.throwIfDisposed(),Ui.clone(this)}toString(e=!1){let t=this.dataSync();return i_(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Ui.cast(this,e)}variable(e=!0,t,n){return this.throwIfDisposed(),cs().makeVariable(this,e,t,n)}};Object.defineProperty(et,Symbol.hasInstance,{value:e=>!!e&&e.data!=null&&e.dataSync!=null&&e.throwIfDisposed!=null});function p_(){return hg("Tensor",()=>et)}p_();var wd=class extends et{constructor(e,t,n,s){super(e.shape,e.dtype,e.dataId,s),this.trainable=t,this.name=n}assign(e){if(e.dtype!==this.dtype)throw new Error(`dtype of the new value (${e.dtype}) and previous value (${this.dtype}) must match`);if(!Sr(e.shape,this.shape))throw new Error(`shape of the new value (${e.shape}) and previous value (${this.shape}) must match`);cs().disposeTensor(this),this.dataId=e.dataId,cs().incRef(this,null)}dispose(){cs().disposeVariable(this),this.isDisposedInternal=!0}};Ob
Manifest JSON has weights with names: ${o.join(", ")}.`)}let u=r.reduce((h,f,m)=>(f&&h.push(m),h),[]),l=[];u.forEach(h=>{t[h].paths.forEach(f=>{let m=n+(n.endsWith("/")?"":"/")+f;l.push(m)})});let c=await e(l),p={},d=0;return u.forEach(h=>{let f=t[h].paths.length,m=0;for(let x=0;x<f;x++)m+=c[d+x].byteLength;let g=new ArrayBuffer(m),b=new Uint8Array(g),y=0;for(let x=0;x<f;x++){let k=new Uint8Array(c[d+x]);b.set(k,y),y+=k.byteLength}a[h].forEach(x=>{let k=g.slice(x.groupOffset,x.groupOffset+x.sizeBytes),I=Ck(k,[x.manifestEntry]);for(let $ in I)p[$]=I[$]}),d+=f}),p}}var pA="application/octet-stream",hA="application/json",qg=class{constructor(e,t){if(this.DEFAULT_METHOD="POST",t==null&&(t={}),this.weightPathPrefix=t.weightPathPrefix,this.onProgress=t.onProgress,this.weightUrlConverter=t.weightUrlConverter,t.fetchFunc!=null?(F(typeof t.fetchFunc=="function",()=>"Must pass a function that matches the signature of `fetch` (see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)"),this.fetch=t.fetchFunc):this.fetch=K().platform.fetch,F(e!=null&&e.length>0,()=>"URL path for http must not be null, undefined or empty."),Array.isArray(e)&&F(e.length===2,()=>`URL paths for http must have a length of 2, (actual length is ${e.length}).`),this.path=e,t.requestInit!=null&&t.requestInit.body!=null)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=t.requestInit||{}}async save(e){if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");let t=Object.assign({method:this.DEFAULT_METHOD},this.requestInit);t.body=new FormData;let n=[{paths:["./model.weights.bin"],weights:e.weightSpecs}],s=Nk(e,n);t.body.append("model.json",new Blob([JSON.stringify(s)],{type:hA}),"model.json"),e.weightData!=null&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:pA}),"model.weights.bin");let r=await this.fetch(this.path,t);if(r.ok)return{modelArtifactsInfo:Ml(e),responses:[r]};throw new Error(`BrowserHTTPRequest.save() failed due to HTTP response status ${r.status}.`)}async load(){let e=await this.fetch(this.path,this.requestInit);if(!e.ok)throw new Error(`Request to ${this.path} failed with status code ${e.status}. Please verify this URL points to the model JSON of the model to load.`);let t;try{t=await e.json()}catch(r){let a=`Failed to parse model JSON of response from ${this.path}.`;throw this.path.endsWith(".pb")?a+=" Your path contains a .pb file extension. Support for .pb models have been removed in TensorFlow.js 1.0 in favor of .json models. You can re-convert your Python TensorFlow model using the TensorFlow.js 1.0 conversion scripts or you can convert your.pb models with the 'pb2json'NPM script in the tensorflow/tfjs-converter repository.":a+=" Please make sure the server is serving valid JSON for this request.",new Error(a)}let n=t.modelTopology,s=t.weightsManifest;if(n==null&&s==null)throw new Error(`The JSON from HTTP path ${this.path} contains neither model topology or manifest for weights.`);return Hg(t,r=>this.loadWeights(r))}async loadWeights(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,s]=fA(t),r=this.weightPathPrefix||n,a=[];for(let l of e)a.push(...l.weights);let i=[],o=[];for(let l of e)for(let c of l.paths)this.weightUrlConverter!=null?o.push(this.weightUrlConverter(c)):i.push(r+c+s);this.weightUrlConverter&&i.push(...await Promise.all(o));let u=await Fk(i,{requestInit:this.requestInit,fetchFunc:this.fetch,onProgress:this.onProgress});return[a,Gg(u)]}};qg.URL_SCHEME_REGEX=/^https?:\/\//;function fA(e){let t=e.lastIndexOf("/"),n=e.lastIndexOf("?"),s=e.substring(0,t),r=n>t?e.substring(n):"";return[s+"/",r]}function gm(e){return e.match(qg.URL_SCHEME_REGEX)!=null}var Pk=(e,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let n=!0;if(Array.isArray(e)?n=e.every(s=>gm(s)):n=gm(e),n)return jg(e,t)}return null};wt.registerSaveRouter(Pk);wt.registerLoadRouter(Pk);function jg(e,t){return new qg(e,t)}function mA(e,t){return jg(e,t)}var qf=class{
2022-04-21 15:39:40 +02:00
Actual: ${r}.
Expected: ${a}.`);for(let i=0;i<a.length;++i){let o=r[i],u=a[i];if(!n(o,u))throw new Error(`Arrays differ: actual[${i}] = ${o}, expected[${i}] = ${u}.
Actual: ${r}.
2022-05-30 03:12:18 +02:00
Expected: ${a}.`)}}function YA(e,t){e().then(()=>t.fail(),()=>t())}function QA(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return ar(e)||ar(e[0])||ar(t)||ar(t[0])?vm(e,n,(s,r)=>s==r):vm(e,t,(s,r)=>Qg(s,r,0))}function ZA(e,t,n){if(n==null&&(n=Yg()),!Qg(e,t,n))throw new Error(`Numbers differ: actual === ${e}, expected === ${t}`)}function Qg(e,t,n){return!isFinite(e)&&!isFinite(t)?!0:!(isNaN(e)||isNaN(t)||Math.abs(e-t)>n)}function JA(e,t,n){for(let s=0;s<e.length;s++)if(e[s]<t||e[s]>n)throw new Error(`Value out of range:${e[s]} low: ${t}, high: ${n}`)}function eE(e,t){let n=new Float32Array(e),s=new Float32Array(t);if(n.length!==s.length)throw new Error(`Expected ArrayBuffer to be of length ${s.length}, but it was ${n.length}`);for(let r=0;r<s.length;r++)if(n[r]!==s[r])throw new Error(`Expected ArrayBuffer value at ${r} to be ${s[r]} but got ${n[r]} instead`)}function rS(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?rS(n):e[t]=zl(n)}return e}var Rpe="0.0.0";function tE(e,t){let n=_(e,"a","add"),s=_(t,"b","add");[n,s]=xt(n,s);let r={a:n,b:s};return z.runKernel(Ir,r)}var ie=L({add_:tE});function nE(e,t){let n=_(e,"a","floorDiv"),s=_(t,"b","floorDiv");[n,s]=xt(n,s);let r={a:n,b:s};return z.runKernel(La,r)}var aS=L({floorDiv_:nE});function sE(e,t){let n=_(e,"a","div"),s=_(t,"b","div");if([n,s]=xt(n,s),n.dtype==="int32"&&s.dtype==="int32")return aS(n,s);let r={a:n,b:s},a={};return z.runKernel(Oa,r,a)}var xe=L({div_:sE});function rE(e,t){let n=_(e,"a","mul"),s=_(t,"b","mul");[n,s]=xt(n,s);let r={a:n,b:s};return z.runKernel(Za,r)}var V=L({mul_:rE});function aE(e){let t=_(e,"x","abs");if(t.dtype==="complex64"){let n={x:t};return z.runKernel(np,n)}else{let n={x:t};return z.runKernel(lo,n)}}var Mt=L({abs_:aE});function iE(e){let n={x:_(e,"x","acos")};return z.runKernel(ul,n)}var oE=L({acos_:iE});function uE(e){let n={x:_(e,"x","acosh")};return z.runKernel(ll,n)}var lE=L({acosh_:uE});function cE(e){F(Array.isArray(e),()=>"The argument passed to tf.addN() must be a list of tensors"),F(e.length>=1,()=>`Must pass at least one tensor to tf.addN(), but got ${e.length}`);let t=e.map((r,a)=>_(r,`tensors${a}`,"addN")),n=t[0];t.forEach(r=>{if(r.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),t.forEach(r=>{if(!Sr(r.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});let s=t;return z.runKernel(Sa,s)}var dE=L({addN_:cE});function pE(e,t=null,n=!1){let r={x:_(e,"x","all","bool")},a={axis:t,keepDims:n};return z.runKernel(cl,r,a)}var iS=L({all_:pE});function hE(e,t=null,n=!1){let r={x:_(e,"x","any","bool")},a={axis:t,keepDims:n};return z.runKernel(dl,r,a)}var xm=L({any_:hE});function fE(e,t=0){let s={x:_(e,"x","argMax")},r={axis:t};return z.runKernel(Ia,s,r)}var Xu=L({argMax_:fE});function mE(e,t=0){let s={x:_(e,"x","argMin")},r={axis:t};return z.runKernel(pl,s,r)}var gE=L({argMin_:mE});function bE(e){let n={x:_(e,"x","asin")};return z.runKernel(hl,n)}var yE=L({asin_:bE});function vE(e){let n={x:_(e,"x","asinh")};return z.runKernel(fl,n)}var xE=L({asinh_:vE});function wE(e){let n={x:_(e,"x","atan")};return z.runKernel(ml,n)}var kE=L({atan_:wE});function SE(e,t){let n=_(e,"a","atan2"),s=_(t,"b","atan2");[n,s]=xt(n,s);let r={a:n,b:s};return z.runKernel(bl,r)}var IE=L({atan2_:SE});function CE(e){let n={x:_(e,"x","atanh")};return z.runKernel(gl,n)}var NE=L({atanh_:CE});function TE(e,t,n,s,r="NHWC",a){let i=e[3],o=[...t,i],u=lS(r);return Ll(e,o,n,a,s,null,null,u)}function oS(e,t,n,s,r,a,i="channelsLast"){let[o,u]=Cd(t),l;if(i==="channelsLast")l=[o,u,e[3],e[3]];else if(i==="channelsFirst")l=[o,u,e[1],e[1]];else throw new Error(`Unknown dataFormat ${i}`);return Ll(e,l,n,s,r,a,!1,i)}function $E(e,t,n,s,r,a,i="NDHWC"){let[o,u,l]=wm(t),c,p;if(i==="NDHWC")p="channelsLast",c=[o,u,l,e[4],e[4]];else if(i==="NCDHW")p="channelsFirst",c=[o,u,l,e[1],e[1]];else throw new Error(`Unknown dataFormat ${i}`);return uS(e,c,n,s,r,!1,p,a)}function Ll(e,t,n,s,r,a,i=!1,o="channelsLast"){let[u,l,c,p]=[-1,-1,-1,-1];if(o==="channelsLast")[u,l,c,p]=e
with dtype ${a.dtype}. `)}),n.length===1)return ur(n[0]);let s=n,r={axis:t};return z.runKernel(po,s,r)}var Ft=L({concat_:PE});function zE(e){let n={x:_(e,"x","sigmoid","float32")};return z.runKernel(ui,n)}var qs=L({sigmoid_:zE});function ME(e,t,n){let s=_(e,"x","slice","string_or_numeric");if(s.rank===0)throw new Error("Slicing scalar is not possible");let r={x:s},a={begin:t,size:n};return z.runKernel(Mo,r,a)}var qe=L({slice_:ME});function LE(e){let n={x:_(e,"x","tanh","float32")};return z.runKernel(fi,n)}var Yu=L({tanh_:LE});function BE(e,t,n,s,r,a){let i=_(e,"forgetBias","basicLSTMCell"),o=_(t,"lstmKernel","basicLSTMCell"),u=_(n,"lstmBias","basicLSTMCell"),l=_(s,"data","basicLSTMCell"),c=_(r,"c","basicLSTMCell"),p=_(a,"h","basicLSTMCell"),d=Ft([l,p],1),h=We(d,o),f=ie(h,u),m=f.shape[0],g=f.shape[1]/4,b=[m,g],y=qe(f,[0,0],b),v=qe(f,[0,g],b),x=qe(f,[0,g*2],b),k=qe(f,[0,g*3],b),I=ie(V(qs(y),Yu(v)),V(c,qs(ie(i,x)))),$=V(Yu(I),qs(k));return[I,$]}var Dpe=L({basicLSTMCell_:BE});function VE(e,t,n){let s=_(e,"x","batchToSpaceND"),r=t.reduce((o,u)=>o*u);F(s.rank>=1+t.length,()=>`input rank is ${s.rank} but should be > than blockShape.length ${t.length}`),F(n.length===t.length,()=>`crops.length is ${n.length} but should be equal to blockShape.length ${t.length}`),F(s.shape[0]%r===0,()=>`input tensor batch is ${s.shape[0]} but is not divisible by the product of the elements of blockShape ${t.join(" * ")} === ${r}`);let a={x:s},i={blockShape:t,crops:n};return z.runKernel(co,a,i)}var eb=L({batchToSpaceND_:VE});function WE(e){let t;return e.rank===0||e.rank===1?t=U(e,[1,1,1,e.size]):e.rank===2?t=U(e,[1,1,e.shape[0],e.shape[1]]):e.rank===3?t=U(e,[1,e.shape[0],e.shape[1],e.shape[2]]):t=e,t}function UE(e,t,n,s,r,a){a==null&&(a=.001);let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;s!=null&&(c=_(s,"offset","batchNorm")),F(o.rank===u.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),F(c==null||o.rank===c.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),F(l==null||o.rank===l.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let d={x:WE(i),scale:l,offset:c,mean:o,variance:u},h={varianceEpsilon:a},f=z.runKernel(Ba,d,h);return U(f,i.shape)}var Qu=L({batchNorm_:UE});function GE(e,t,n,s,r,a){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;return s!=null&&(c=_(s,"offset","batchNorm")),F(i.rank===2,()=>`Error in batchNorm2D: x must be rank 2 but got rank ${i.rank}.`),F(o.rank===2||o.rank===1,()=>`Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank ${o.rank}.`),F(u.rank===2||u.rank===1,()=>`Error in batchNorm2D: variance must be rank 2 or rank 1 but got rank ${u.rank}.`),l!=null&&F(l.rank===2||l.rank===1,()=>`Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank ${l.rank}.`),c!=null&&F(c.rank===2||c.rank===1,()=>`Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank ${c.rank}.`),Qu(i,o,u,c,l,a)}var HE=L({batchNorm2d_:GE});function qE(e,t,n,s,r,a){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;return s!=null&&(c=_(s,"offset","batchNorm")),F(i.rank===3,()=>`Error in batchNorm3D: x must be rank 3 but got rank ${i.rank}.`),F(o.rank===3||o.rank===1,()=>`Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank ${o.rank}.`),F(u.rank===3||u.rank===1,()=>`Error in batchNorm3D: variance must be rank 3 or rank 1 but got rank ${u.rank}.`),l!=null&&F(l.rank===3||l.rank===1,()=>`Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank ${l.rank}.`),c!=null&&F(c.rank===3||c.rank===1,()=>`Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank ${c.rank}.`),Qu(i,o,u,c,l,a)}var jE=L({batchNorm3d_:qE});function KE(e,t,n,s,r,a){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNo
2022-04-21 15:39:40 +02:00
${r} and ${t} for depthToSpace with input shape
2022-05-18 14:33:33 +02:00
${s.shape}`),F(a*t>=0,()=>`Negative dimension size caused by overflow when multiplying
2022-04-21 15:39:40 +02:00
${a} and ${t} for depthToSpace with input shape
2022-05-30 03:12:18 +02:00
${s.shape}`),F(i%(t*t)===0,()=>`Dimension size must be evenly divisible by ${t*t} but is ${i} for depthToSpace with input shape ${s.shape}`);let o={x:s},u={blockSize:t,dataFormat:n};return z.runKernel(mo,o,u)}var NR=L({depthToSpace_:CR});function TR(e,t,n,s,r="NHWC",a=[1,1],i){let o=_(e,"x","depthwiseConv2d","float32"),u=_(t,"filter","depthwiseConv2d","float32"),l=o,c=!1;o.rank===3&&(c=!0,l=U(o,[1,o.shape[0],o.shape[1],o.shape[2]])),F(l.rank===4,()=>`Error in depthwiseConv2d: input must be rank 4, but got rank ${l.rank}.`),F(u.rank===4,()=>`Error in depthwiseConv2d: filter must be rank 4, but got rank ${u.rank}.`),F(l.shape[3]===u.shape[2],()=>`Error in depthwiseConv2d: number of input channels (${l.shape[3]}) must match the inChannels dimension in filter ${u.shape[2]}.`),hn("depthwiseConv2d",s,i);let p={x:l,filter:u},d={strides:n,pad:s,dataFormat:r,dilations:a,dimRoundingMode:i},h=z.runKernel(Fa,p,d);return c?U(h,[h.shape[1],h.shape[2],h.shape[3]]):h}var kp=L({depthwiseConv2d_:TR});function $R(e){let n={x:_(e,"x","diag")};return z.runKernel(Ig,n)}var Fpe=L({diag_:$R});function _R(e,t,n,s,r=[1,1],a="NHWC"){let i=_(e,"x","dilation2d"),o=_(t,"filter","dilation2d");F(i.rank===3||i.rank===4,()=>`Error in dilation2d: input must be rank 3 or 4, but got rank ${i.rank}.`),F(o.rank===3,()=>`Error in dilation2d: filter must be rank 3, but got rank ${o.rank}.`),F(a==="NHWC",()=>`Error in dilation2d: Only NHWC is currently supported, but got dataFormat of ${a}`);let u=i,l=!1;i.rank===3&&(u=U(i,[1,i.shape[0],i.shape[1],i.shape[2]]),l=!0);let c={x:u,filter:o},p={strides:n,pad:s,dilations:r},d=z.runKernel(rp,c,p);return l?U(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var AR=L({dilation2d_:_R});function ER(e,t){let n=_(e,"a","equal","string_or_numeric"),s=_(t,"b","equal","string_or_numeric");[n,s]=xt(n,s),rt(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(go,r)}var Xn=L({equal_:ER});function RR(e,t,n){let s=_(t,"a","where"),r=_(n,"b","where"),a=_(e,"condition","where","bool"),i=rt(rt(a.shape,s.shape),r.shape),o=id(a,i),u=id(s,i),l=id(r,i),c={condition:o,t:u,e:l};return z.runKernel(zo,c)}var vn=L({where_:RR});function DR(e){let n={x:_(e,"x","zerosLike")};return z.runKernel(jo,n)}var je=L({zerosLike_:DR});function FR(e,t){let n=_(e,"a","div"),s=_(t,"b","div");[n,s]=xt(n,s);let r=xe(n,s),a=je(r),i=Xn(s,a);return vn(i,a,r)}var OR=L({divNoNan_:FR});function PR(e,t){let n=_(e,"t1","dot"),s=_(t,"t2","dot");F((n.rank===1||n.rank===2)&&(s.rank===1||s.rank===2),()=>`Error in dot: inputs must all be rank 1 or 2, but got ranks ${n.rank} and ${s.rank}.`);let r=n.rank===1?n.size:n.shape[1],a=s.rank===1?s.size:s.shape[0];if(F(r===a,()=>`Error in dot: inner dimensions of inputs must match, but got ${r} and ${a}.`),n.rank===1&&s.rank===1){let i=U(n,[1,-1]),o=U(s,[-1,1]),u=We(i,o);return U(u,[])}else if(n.rank===1&&s.rank===2){let i=U(n,[1,-1]),o=U(s,[s.shape[0],s.shape[1]]),u=We(i,o);return U(u,[u.size])}else if(n.rank===2&&s.rank===1){let i=U(s,[-1,1]),o=We(n,i);return U(o,[o.size])}else{let i=U(s,[s.shape[0],s.shape[1]]);return We(n,i)}}var Ope=L({dot_:PR});function zR(e,...t){let n=t.map((r,a)=>_(r,`tensors${a}`,"einsum")),s={equation:e};return z.runKernel(ap,n,s)}var MR=L({einsum_:zR});function LR(e){let n={x:_(e,"x","elu","float32")};return z.runKernel(Pa,n)}var Sp=L({elu_:LR});function BR(e){let t=_(e,"x","erf");F(t.dtype==="int32"||t.dtype==="float32",()=>"Input dtype must be `int32` or `float32`."),t.dtype==="int32"&&(t=le(t,"float32"));let n={x:t};return z.runKernel(yl,n)}var VR=L({erf_:BR});function sb(e,t){for(let n=0;n<e.length;++n)if(e[e.length-n-1]!==t-1-n)return!1;return!0}function yS(e,t,n){let s=e.length+t.length,r=[],a=0,i=0;for(let o=0;o<s;o++)n.indexOf(o)===-1?r.push(e[a++]):r.push(t[i++]);return r}function vS(e,t){let n=[],s=e.length;for(let a=0;a<s;a++)t.indexOf(a)===-1&&n.push(e[a]);let r=t.map(a=>e[a]);return[n,r]}function pa(e,t){let n=t.map(s=>1);return yS(e,n,t)}function WR(e,t,n){F(sb(t,n),()=>`${e} supports only inner-most axes for now. Got axes ${t} and rank-${n} input.`)}function xS(e,t){if(sb(e,t))return null;let
rank ${a.rank}.`),F(Zi(t),()=>`Error in localResponseNormalization: depthRadius must be an integer but got depthRadius ${t}.`);let i=a,o=!1;a.rank===3&&(o=!0,i=U(a,[1,a.shape[0],a.shape[1],a.shape[2]]));let u={x:i},l={depthRadius:t,bias:n,alpha:s,beta:r},c=z.runKernel(up,u,l);return o?U(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var yD=L({localResponseNormalization_:bD});function vD(e){let n={x:_(e,"x","log","float32")};return z.runKernel(Ga,n)}var Qn=L({log_:vD});function xD(e){let n={x:_(e,"x","log1p")};return z.runKernel(Sl,n)}var ob=L({log1p_:xD});function Mpe(e){return F(hr(e),()=>"The f passed in grad(f) must be a function"),(t,n)=>{let s=_(t,"x","tf.grad","string_or_numeric"),r=n!=null?_(n,"dy","tf.grad"):null;return z.tidy(()=>{let{value:a,grads:i}=z.gradients(()=>e(s),[s],r);return r!=null&&pn(a.shape,r.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Cp(i),i[0]})}}function Lpe(e){return F(hr(e),()=>"The f passed in grads(f) must be a function"),(t,n)=>{F(Array.isArray(t),()=>"The args passed in grads(f)(args) must be an array of `Tensor`s or `TensorLike`s");let s=ju(t,"args","tf.grads","string_or_numeric"),r=n!=null?_(n,"dy","tf.grads"):null;return z.tidy(()=>{let{value:a,grads:i}=z.gradients(()=>e(...s),s,r);return r!=null&&pn(a.shape,r.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Cp(i),i})}}function Bpe(e){return F(hr(e),()=>"The f passed in valueAndGrad(f) must be a function"),(t,n)=>{F(t instanceof et,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"),F(n==null||n instanceof et,()=>"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");let{grads:s,value:r}=z.gradients(()=>e(t),[t],n);return Cp(s),{grad:s[0],value:r}}}function Vpe(e){return F(hr(e),()=>"The f passed in valueAndGrads(f) must be a function"),(t,n)=>{F(Array.isArray(t)&&t.every(r=>r instanceof et),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"),F(n==null||n instanceof et,()=>"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");let s=z.gradients(()=>e(...t),t,n);return n!=null&&pn(s.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Cp(s.grads),s}}function wD(e,t){F(hr(e),()=>"The f passed in variableGrads(f) must be a function"),F(t==null||Array.isArray(t)&&t.every(l=>l instanceof wd),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let n=t!=null;if(!n){t=[];for(let l in z.registeredVariables)t.push(z.registeredVariables[l])}let s=n?t.filter(l=>!l.trainable):null,r=t.length;t=t.filter(l=>l.trainable),F(t.length>0,()=>`variableGrads() expects at least one of the input variables to be trainable, but none of the ${r} variables is trainable.`);let a=!0,{value:i,grads:o}=z.gradients(e,t,null,a);F(o.some(l=>l!=null),()=>"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),F(i.rank===0,()=>`The f passed in variableGrads(f) must return a scalar, but it returned a rank-${i.rank} tensor`);let u={};return t.forEach((l,c)=>{o[c]!=null&&(u[l.name]=o[c])}),s!=null&&s.forEach(l=>u[l.name]=null),{value:i,grads:u}}function js(e){return z.customGrad(e)}function Cp(e){if(e.filter(n=>n==null).length>0)throw new Error(`Cannot compute gradient of y=f(x) with respect to x. Make sure that
the f you passed encloses all operations that lead from x to y.`)}function kD(e){let n={x:_(e,"x","softplus")};return z.runKernel(Rl,n)}var Vl=L({softplus_:kD});function SD(e){let t=_(e,"x","logSigmoid");return js(s=>({value:vt(Vl(vt(s))),gradFunc:i=>V(i,qs(vt(s)))}))(t)}var Wpe=L({logSigmoid_:SD});function ID(e,t){let n=_(e,"a","sub"),s=_(t,"b","sub");[n,s]=xt(n,s);let r={a:n,b:s};return z.runKernel(hi,r)}var ge=L({sub_:ID});function CD(e,t=-1){let n=_(e,"logits","logSoftmax");if(t===-1&&(t=n.rank-1),t!==n.rank-1)throw Error(`Log Softmax along a non-last dimension is not yet supported. Logits was rank ${n.rank} and axis was ${t}`);return js((r,a)=>{let o=As(r,t,!0),u=ge(r,o),l=ge(le(u,"float32"),Qn(ve(Yn(u),t,!0)));return a([l]),{value:l,gradFunc:(p,d)=>{let[h]=d,f=!0,m=Yn(h);return ge(p,V(ve(p,t,f),m))}}})(n)}var IS=L({logSoftmax_:CD});function ND(e,t=null,n=!1){let s=_(e,"x","logSumExp"),r=ts(t,s.shape),a=As(s,r,!0),i=ge(s,a),o=Yn(i),u=ve(o,r),l=Qn(u),c=ie(U(a,l.shape),l);if(n){let p=pa(c.shape,r);return U(c,p)}return c}var TD=L({logSumExp_:ND});function $D(e,t){let n=_(e,"a","logicalAnd","bool"),s=_(t,"b","logicalAnd","bool");rt(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(Co,r)}var Ds=L({logicalAnd_:$D});function _D(e){let n={x:_(e,"x","logicalNot","bool")};return z.runKernel(Il,n)}var ub=L({logicalNot_:_D});function AD(e,t){let n=_(e,"a","logicalOr","bool"),s=_(t,"b","logicalOr","bool");rt(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(op,r)}var CS=L({logicalOr_:AD});function ED(e,t){let n=_(e,"a","logicalXor","bool"),s=_(t,"b","logicalXor","bool");return rt(n.shape,s.shape),Ds(CS(e,t),ub(Ds(e,t)))}var Upe=L({logicalXor_:ED}),Hc=2147483648;function RD(e,t,n="left"){let s=_(e,"sortedSequence","searchSorted"),r=_(t,"values","searchSorted"),a=s.shape[s.shape.length-1],i=r.shape[r.shape.length-1],o=U(s,[-1,a]),u=U(r,[-1,i]);if(o.rank<2)throw new Error("Sorted input argument must be at least 2-dimensional");if(o.shape[0]!==u.shape[0])throw new Error("Leading dimension of 'sortedSequence' and 'values' must match.");if(dt(u.shape)>=Hc)throw new Error(`values tensor size must less than ${Hc}`);if(o.shape[1]>=Hc)throw new Error(`trailing dim_size must less than ${Hc} for int32 output type, was ${o.shape[1]}`);let l={sortedSequence:o,values:u},c={side:n};return z.runKernel(Pg,l,c)}var NS=L({searchSorted_:RD});function DD(e,t){return NS(e,t,"left")}function FD(e,t,n,s,r){let a=_(e,"x","maxPool"),i=1,o=a,u=!1;a.rank===3&&(u=!0,o=U(a,[1,a.shape[0],a.shape[1],a.shape[2]])),F(o.rank===4,()=>`Error in maxPool: input must be rank 4 but got rank ${o.rank}.`),F(Ps(n,i),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${n} and dilations '${i}'`),hn("maxPool",s,r);let l={x:o},c={filterSize:t,strides:n,pad:s,dimRoundingMode:r},p=z.runKernel(ja,l,c);return u?U(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var lb=L({maxPool_:FD});function OD(e,t=[1,1,1],n,s,r,a="NDHWC"){let i=_(e,"x","maxPool3d"),o=i,u=!1;i.rank===4&&(u=!0,o=U(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])),F(o.rank===5,()=>`Error in maxPool3d: x must be rank 5 but got rank ${o.rank}.`),F(a==="NDHWC",()=>`Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of ${a}`),hn("maxPool3d",s,r);let l={x:o},c={filterSize:t,strides:n,pad:s,dimRoundingMode:r,dataFormat:a},p=z.runKernel(lp,l,c);return u?U(p,[p.shape[1],p.shape[2],p.shape[3],p.shape[4]]):p}var TS=L({maxPool3d_:OD});function PD(e,t,n,s,r=!1){let i={x:_(e,"x","maxPoolWithArgmax")},o={filterSize:t,strides:n,pad:s,includeBatchInIndex:r},u=z.runKernel(Rg,i,o);return{result:u[0],indexes:u[1]}}var zD=L({maxPoolWithArgmax_:PD});function MD(e,t){let n=_(e,"a","maximum"),s=_(t,"b","maximum");[n,s]=xt(n,s),n.dtype==="bool"&&(n=le(n,"int32"),s=le(s,"int32")),rt(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(qa,r)}var _r=L({maximum_:MD});function LD(e,t=null,n=!1){let r={x:_(e,"x","mean")},a={axis:t,keepDims:n};return z.runKernel(Ka,r,a)}var It=L({mean_:LD});function $t(e,t="float32"){if(t==="complex64"){let s=$t(e,"float32"),r=$t(e,"float32");return fr(s,r)}let n=J
${r.shape}`);if(a.rank!==1)throw new Error(`Values should be Tensor1D but received shape ${a.shape}`);if(i.rank!==1)throw new Error(`Dense shape should be Tensor1D but received shape ${i.shape}`);if(o.rank!==0)throw new Error(`Default value should be a scalar but received shape ${o.shape}`);let u={indices:r,values:a,denseShape:i,defaultValue:o},l=z.runKernel(dp,u);return{outputIndices:l[0],outputValues:l[1],emptyRowIndicator:l[2],reverseIndexMap:l[3]}}var qO=L({sparseFillEmptyRows_:HO});function jO(e,t,n){let s=_(e,"inputIndices","sparseReshape","int32"),r=_(t,"inputShape","sparseReshape","int32"),a=_(n,"newShape","sparseReshape","int32");if(s.rank!==2)throw new Error(`Input indices should be Tensor2D but received shape
${s.shape}`);if(r.rank!==1)throw new Error(`Input shape should be Tensor1D but received shape ${r.shape}`);if(a.rank!==1)throw new Error(`New shape should be Tensor1D but received shape ${a.shape}`);let i={inputIndices:s,inputShape:r,newShape:a},o=z.runKernel(Dl,i);return{outputIndices:o[0],outputShape:o[1]}}var KO=L({sparseReshape_:jO});function XO(e,t,n){let s=_(e,"data","sparseSegmentMean"),r=_(t,"indices","sparseSegmentMean","int32"),a=_(n,"segmentIds","sparseSegmentMean","int32");if(s.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.rank!==1)throw new Error(`Indices should be Tensor1D but received shape
2022-04-21 15:39:40 +02:00
${r.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
2022-05-30 03:12:18 +02:00
${a.shape}`);let i={data:s,indices:r,segmentIds:a};return z.runKernel(pp,i)}var YO=L({sparseSegmentMean_:XO});function QO(e,t,n){let s=_(e,"data","sparseSegmentSum"),r=_(t,"indices","sparseSegmentSum","int32"),a=_(n,"segmentIds","sparseSegmentSum","int32");if(s.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.rank!==1)throw new Error(`Indices should be Tensor1D but received shape
2022-04-21 15:39:40 +02:00
${r.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
2022-05-30 03:12:18 +02:00
${a.shape}`);let i={data:s,indices:r,segmentIds:a};return z.runKernel(hp,i)}var ZO=L({sparseSegmentSum_:QO});function JO(e,t,n,s,r,a,i,o){let u=_(e,"data","stringNGrams","string");if(u.dtype!=="string")throw new Error("Data must be of datatype string");if(u.shape.length!==1)throw new Error(`Data must be a vector, saw: ${u.shape}`);let l=_(t,"dataSplits","stringNGrams");if(l.dtype!=="int32")throw new Error("Data splits must be of datatype int32");let c={separator:n,nGramWidths:s,leftPad:r,rightPad:a,padWidth:i,preserveShortSequences:o},p={data:u,dataSplits:l},d=z.runKernel(mp,p,c);return{nGrams:d[0],nGramsSplits:d[1]}}var eP=L({stringNGrams_:JO});function tP(e,t,n=!0){let s=_(e,"input","stringSplit","string"),r=_(t,"delimiter","stringSplit","string");if(s.rank!==1)throw new Error(`Input should be Tensor1D but received shape ${s.shape}`);if(r.rank!==0)throw new Error(`Delimiter should be a scalar but received shape ${r.shape}`);let a={skipEmpty:n},i={input:s,delimiter:r},o=z.runKernel(zg,i,a);return{indices:o[0],values:o[1],shape:o[2]}}var nP=L({stringSplit_:tP});function sP(e,t){let n=_(e,"input","stringToHashBucketFast","string"),s={numBuckets:t};if(t<=0)throw new Error("Number of buckets must be at least 1");let r={input:n};return z.runKernel(Mg,r,s)}var rP=L({stringToHashBucketFast_:sP}),che={fft:yb,ifft:Td,rfft:vb,irfft:PS},dhe={hammingWindow:DF,hannWindow:GS,frame:HS,stft:zF},jn={flipLeftRight:VF,grayscaleToRGB:UF,resizeNearestNeighbor:hO,resizeBilinear:dO,rotateWithOffset:HF,cropAndResize:LF,nonMaxSuppression:jF,nonMaxSuppressionAsync:tO,nonMaxSuppressionWithScore:sO,nonMaxSuppressionWithScoreAsync:aO,nonMaxSuppressionPadded:oO,nonMaxSuppressionPaddedAsync:lO,threshold:gO,transform:yO},aP={bandPart:xO,gramSchmidt:kO,qr:IO},phe={absoluteDifference:$O,computeWeightedLoss:Ys,cosineDistance:AO,hingeLoss:RO,huberLoss:FO,logLoss:PO,meanSquaredError:MO,sigmoidCrossEntropy:VO,softmaxCrossEntropy:GO},qc={sparseFillEmptyRows:qO,sparseReshape:KO,sparseSegmentMean:YO,sparseSegmentSum:ZO},jf={stringNGrams:eP,stringSplit:nP,stringToHashBucketFast:rP},Ar=class extends nS{minimize(e,t=!1,n){let{value:s,grads:r}=this.computeGradients(e,n);if(n!=null){let a=n.map(i=>({name:i.name,tensor:r[i.name]}));this.applyGradients(a)}else this.applyGradients(r);return De(r),t?s:(s.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(e,t){return wD(e,t)}dispose(){this.iterations_!=null&&De(this.iterations_)}async saveIterations(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:we(this.iterations_,"int32")}}async getWeights(){throw new Error("getWeights() is not implemented for this optimizer yet.")}async setWeights(e){throw new Error(`setWeights() is not implemented for this optimizer class ${this.getClassName()}`)}async extractIterations(e){return this.iterations_=(await e[0].tensor.data())[0],e.slice(1)}};Object.defineProperty(Ar,Symbol.hasInstance,{value:e=>e.minimize!=null&&e.computeGradients!=null&&e.applyGradients!=null});var Sb=class extends Ar{constructor(e,t,n=null){super(),this.learningRate=e,this.rho=t,this.epsilon=n,this.accumulatedGrads=[],this.accumulatedUpdates=[],n==null&&(this.epsilon=z.backend.epsilon())}applyGradients(e){(Array.isArray(e)?e.map(n=>n.name):Object.keys(e)).forEach((n,s)=>{let r=z.registeredVariables[n],a=!1;this.accumulatedGrads[s]==null&&(this.accumulatedGrads[s]={originalName:`${n}/accum_grad`,variable:q(()=>je(r).variable(a))}),this.accumulatedUpdates[s]==null&&(this.accumulatedUpdates[s]={originalName:`${n}/accum_var`,variable:q(()=>je(r).variable(a))});let i=Array.isArray(e)?e[s].tensor:e[n];if(i==null)return;let o=this.accumulatedGrads[s].variable,u=this.accumulatedUpdates[s].variable;q(()=>{let l=ie(V(o,this.rho),V(ct(i),1-this.rho)),c=V(xe(cn(ie(u,this.epsilon)),cn(ie(o,this.epsilon))),i),p=ie(V(u,this.rho),V(ct(c),1-this.rho));o.assign(l),u.assign(p);let d=ie(V(c,-this.learningRate),r);r.assign(d)})}),this.incrementIterations()}dispose(){this.accumul
indices.shape[0] = ${e}`}function LP(e,t){return`indices(${e}, 0) is invalid: ${t} < 0`}function BP(e,t,n){return`indices(${e}, 0) is invalid: ${t} >= ${n}`}function VP(e,t){return`only one output dimension may be -1, not both ${e} and ${t}`}function WP(e,t){return`size ${e} must be non-negative, not ${t}`}function UP(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function GP(e,t){let n=dt(e),s=dt(t);return`Input to reshape is a SparseTensor with ${n}
dense values, but the requested shape requires a multiple of ${s}. inputShape=${e} outputShape= ${t}`}function HP(e,t){let n=dt(e),s=dt(t);return`Input to reshape is a tensor with ${n} dense values, but the requested shape has ${s}. inputShape=${e} outputShape=${t}`}function qP(){return"segment ids must be >= 0"}function jP(){return"segment ids are not increasing"}function KP(e,t){return`Segment id ${e} out of range [0, ${t}), possibly because segmentIds input is not sorted.`}function XP(e,t,n){return`Bad: indices[${e}] == ${t} out of range [0, ${n})`}var ZS={};Ee(ZS,{collectGatherOpShapeInfo:()=>ZP,computeOutShape:()=>QP,segOpComputeOptimalWindowSize:()=>YP});function YP(e,t){let n=!1,s;for(e<=_b?(s=e,n=!0):s=yd(e,Math.floor(Math.sqrt(e)));!n;)s>t||s===e?n=!0:s=yd(e,s+1);return s}function QP(e,t,n){let s=[],r=e.length;for(let a=0;a<r;a++)a!==t?s.push(e[a]):s.push(n);return s}function ZP(e,t,n,s){let r=t.shape.length,a=e.shape.length;if(s!==0&&(s<-r||s>r))throw new Error(`Expect batchDims in the range of [-${r}, ${r}], but got ${s}`);if(s<0&&(s+=r),s>a)throw new Error(`batchDims (${s}) must be less than rank(x) (
${a}).`);if(n<s)throw new Error(`batchDims (${s}) must be less than or equal to axis (${n}).`);for(let p=0;p<s;++p)if(e.shape[p]!==t.shape[p])throw new Error(`x.shape[${p}]: ${e.shape[p]} should be equal to indices.shape[${p}]: ${t.shape[p]}.`);let i=e.shape[n],o=[],u=1,l=1,c=1;for(let p=0;p<s;++p)o.push(e.shape[p]),u*=e.shape[p];for(let p=s;p<n;p++)o.push(e.shape[p]),l*=e.shape[p];for(let p=s;p<r;p++)o.push(t.shape[p]);for(let p=n+1;p<a;p++)o.push(e.shape[p]),c*=e.shape[p];return{batchSize:u,sliceSize:c,outerSize:l,dimSize:i,outputShape:o}}function JP(e){try{return e.map(t=>xd(t))}catch(t){throw new Error(`Failed to decode encoded string bytes into utf-8, error: ${t}`)}}function ez(e){return e.map(t=>zl(t))}var ws={};Ee(ws,{nonMaxSuppressionV3Impl:()=>qS,nonMaxSuppressionV4Impl:()=>jS,nonMaxSuppressionV5Impl:()=>KS,whereImpl:()=>MS});var Bs=class extends Error{constructor(e){super(e),Object.setPrototypeOf(this,Bs.prototype)}},fs=class extends Error{constructor(e){super(e),Object.setPrototypeOf(this,fs.prototype)}},G=class extends Error{constructor(e){super(e),Object.setPrototypeOf(this,G.prototype)}},Fe=class extends Error{constructor(e){super(e),Object.setPrototypeOf(this,Fe.prototype)}},JS=class extends Error{constructor(e){super(e),Object.setPrototypeOf(this,JS.prototype)}},eI=class{constructor(e){this.maxEntries=e||100,this.cache=new Map}get(e){let t;return this.cache.has(e)&&(t=this.cache.get(e),this.cache.delete(e),this.cache.set(e,t)),t}put(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxEntries){let n=this.cache.keys().next().value;this.cache.delete(n)}this.cache.set(e,t)}getMaxEntries(){return this.maxEntries}setMaxEntries(e){if(e<0)throw new Error(`The maxEntries of LRU caches must be at least 0, but got ${e}.`);if(this.maxEntries>e)for(let t=0;t<this.maxEntries-e;t++){let n=this.cache.keys().next().value;this.cache.delete(n)}this.maxEntries=e}};function ma(e,t){if(Array.isArray(e)){let n=[];for(let s=0;s<t;s++)n=n.concat(e);return n}else{let n=new Array(t);return n.fill(e),n}}function Cs(e,t){if(!e)throw new JS(t)}function Nx(e,t){let n=0;for(let s of e)s===t&&n++;return n}function bn(e){return e.length===1?e[0]:e}function ht(e){return Array.isArray(e)?e:[e]}function Vs(e){let n=e.replace(/(.)([A-Z][a-z0-9]+)/g,"$1_$2").replace(/([a-z])([A-Z])/g,"$1_$2").toLowerCase();return n[0]!=="_"?n:"private"+n}function Yr(e){return e.length<=1||e.indexOf("_")===-1?e:e.replace(/[_]+(\w|$)/g,(t,n)=>n.toUpperCase())}var Hn={};function Ab(e){if(e==null)return null;let t={};return t.className=e.getClassName(),t.config=e.getConfig(),t}function Im(e){if(!(e==null||typeof e!="object"))if(Array.isArray(e))e.forEach(t=>Im(t));else{let t=Object.keys(e);for(let n of t){let s=e[n];s!=null&&typeof s=="object"&&(!Array.isArray(s)&&s.type==="ndarray"&&typeof s.value=="number"?e[n]=s.value:Im(s))}}}function Ul(e,t={},n={},s="object",r=!1){if(typeof e=="string"){let a=e,i;if(a in n)i=n[a];else if(a in Hn)i=Hn[a];else if(i=t[a],i==null)throw new G(`Unknown ${s}: ${e}. This may be due to one of the following reasons:
2022-04-21 15:39:40 +02:00
1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2022-05-09 14:16:00 +02:00
2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);return i}else{let a=e;if(a.className==null||a.config==null)throw new G(`${s}: Improper config format: ${JSON.stringify(a)}.
'className' and 'config' must set.`);let i=a.className,o,u;if(i in n?[o,u]=n[i]:i in Hn?[o,u]=Hn.className:i in t&&([o,u]=t[i]),o==null)throw new G(`Unknown ${s}: ${i}. This may be due to one of the following reasons:
2022-04-21 15:39:40 +02:00
1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2022-05-30 03:12:18 +02:00
2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);if(u!=null){let l={};for(let h of Object.keys(Hn))l[h]=Hn[h];for(let h of Object.keys(n))l[h]=n[h];let c=a.config;c.customObjects=l;let p={...Hn};for(let h of Object.keys(n))Hn[h]=n[h];Im(a.config);let d=u(o,a.config,n,r);return Hn={...p},d}else{let l={...Hn};for(let p of Object.keys(n))Hn[p]=n[p];let c=new o(a.config);return Hn={...l},c}}}function tz(e,t){return e<t?-1:e>t?1:0}function jc(e,t){return-1*tz(e,t)}function lr(e){if(e==null)return e;let t=[];for(let n of e)t.indexOf(n)===-1&&t.push(n);return t}function nz(e){if(e==null)throw new G(`Invalid value in obj: ${JSON.stringify(e)}`);for(let t in e)if(e.hasOwnProperty(t))return!1;return!0}function bi(e,t,n){if(n!=null&&e.indexOf(n)<0)throw new G(`${n} is not a valid ${t}. Valid values are ${e} or null/undefined.`)}function Eb(e,t,n=0,s=1/0){return Cs(n>=0),Cs(s>=n),Array.isArray(e)&&e.length>=n&&e.length<=s&&e.every(r=>typeof r===t)}function Bt(e,t){Array.isArray(e)?(w.assert(e.length>0,()=>`${t} is unexpectedly an empty array.`),e.forEach((n,s)=>Bt(n,`element ${s+1} of ${t}`))):w.assert(Number.isInteger(e)&&e>0,()=>`Expected ${t} to be a positive integer, but got ${tI(e)}.`)}function tI(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>tI(t)).join(",")+"]":typeof e=="string"?`"${e}"`:`${e}`}function sz(e,t,n){let s=n!=null?n():w.now(),r;return(...i)=>{let o=n!=null?n():w.now();return o-s<t||(s=o,r=e(...i)),r}}function nI(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}var rz=0;function sI(){return rz++}var Kc={};function Dp(e=""){return e in Kc||(Kc[e]=0),Kc[e]+=1,e+Kc[e].toString()}var az=["channelsFirst","channelsLast"],iz=["nearest","bilinear"],oz=["valid","same","causal"],uz=["max","avg"],lz=["sum","mul","concat","ave"],Mi=new Map;function Ct(e){bi(az,"DataFormat",e)}function cz(e){bi(iz,"InterpolationFormat",e)}function Gn(e){bi(oz,"PaddingMode",e)}function rI(e){bi(uz,"PoolMode",e)}var Vu=[],Tx="/";function na(e,t){Vu.push(e);try{let n=t();return Vu.pop(),n}catch(n){throw Vu.pop(),n}}function dz(){return Vu.length===0?"":Vu.join(Tx)+Tx}function aI(e){if(!oI(e))throw new Error("Not a valid tensor name: '"+e+"'");return dz()+e}function iI(e){if(!oI(e))throw new Error("Not a valid tensor name: '"+e+"'");Mi.has(e)||Mi.set(e,0);let t=Mi.get(e);if(Mi.set(e,Mi.get(e)+1),t>0){let n=`${e}_${t}`;return Mi.set(n,1),n}else return e}var pz=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function oI(e){return!!e.match(pz)}function hz(e){return e===parseInt(e.toString(),10)}function cr(e,t,n){t==null&&(t=0),n==null&&(n=e.length);let s=1;for(let r=t;r<n;++r)s*=e[r];return s}function eo(e){if(e.length===0)return Number.NaN;let t=Number.POSITIVE_INFINITY;for(let n=0;n<e.length;n++){let s=e[n];s<t&&(t=s)}return t}function br(e){if(e.length===0)return Number.NaN;let t=Number.NEGATIVE_INFINITY;for(let n=0;n<e.length;n++){let s=e[n];s>t&&(t=s)}return t}function ys(e,t){if(t<e)throw new G(`end (${t}) < begin (${e}) is forbidden.`);let n=[];for(let s=e;s<t;++s)n.push(s);return n}var Xf;function Rt(){return Xf==null&&(Xf=SA().epsilon()),Xf}function vs(){return"channelsLast"}function Fp(e,t){return le(e,t)}function Gl(e,t=-1){let n=e.shape.slice();return t<0&&(t=n.length+t+1),n.splice(t,0,1),U(e,n)}function fz(e,t){return q(()=>{if(e.shape.length!==2)throw new G(`repeat() expects a rank-2 tensor, but received a rank-${e.shape.length} tensor.`);let n=Gl(e,1);return Cm(n,[1,t,1])})}function mz(e){let t=[cr(e.shape)];return U(e,t)}function gz(e){if(e.rank<=1)throw new G(`batchFlatten requires a minimum rank of 2. Got rank: ${e.rank}.`);let t=[e.shape[0],cr(e.shape,1)];return U(e,t)}function sa(e,t,n){return q(()=>{switch(e.rank){case 1:return mb(e,t,n);case 2:return OS(e,[t,0],[n,e.shape[1]]);case 3:return gb(e,[t,0,0],[n,e.shape[1],e.shape[2]]);case 4:return Nd(e,[t,0,0,0],[n,e.shape[1],e.shape[2],e.shape[3]]);case 5:return qe(e,[t,0,0,0,0],[n,e.shape[1],e.shape[2],e.shape[3],e.shape[4]]);case 6:return qe(e,[t,0,0,0,0,0],[n,e.shape[1],e.shape[
because the value dtype is ${t.dtype}, but TensorArray dtype is ${this.dtype}.`);if(this.size()===0&&(this.elementShape==null||this.elementShape.length===0)&&(this.elementShape=t.shape),qn(this.elementShape,t.shape,`TensorArray ${this.name}: Could not write to TensorArray index ${e}.`),n.read)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e}, because it has already been read.`);if(n.written)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e}, because it has already been written.`);n.tensor=t,Ht(t),n.written=!0,this.tensors[e]=n}writeMany(e,t){if(e.length!==t.length)throw new Error(`TensorArray ${this.name}: could not write multiple tensors,because the index size: ${e.length} is not the same as tensors size: ${t.length}.`);e.forEach((n,s)=>this.write(n,t[s]))}gather(e,t){if(!!t&&t!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but gather requested dtype ${t}`);if(e)e=e.slice(0,this.size());else{e=[];for(let s=0;s<this.size();s++)e.push(s)}if(e.length===0)return ms([],[0].concat(this.elementShape));let n=this.readMany(e);return qn(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),es(n,0)}concat(e){if(!!e&&e!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but concat requested dtype ${e}`);if(this.size()===0)return ms([],[0].concat(this.elementShape));let t=[];for(let s=0;s<this.size();s++)t.push(s);let n=this.readMany(t);return qn(this.elementShape,n[0].shape,`TensorArray shape mismatch: tensor array shape (${this.elementShape}) vs first tensor shape (${n[0].shape})`),Ft(n,0)}scatter(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);if(e.length!==t.shape[0])throw new Error(`Expected len(indices) == tensor.shape[0], but saw: ${e.length} vs. ${t.shape[0]}`);let n=Math.max(...e);if(!this.dynamicSize&&n>=this.maxSize)throw new Error(`Max index must be < array size (${n} vs. ${this.maxSize})`);this.writeMany(e,Fs(t,0))}split(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);let n=0,s=e.map(o=>(n+=o,n));if(n!==t.shape[0])throw new Error(`Expected sum of lengths to be equal to
2022-04-21 15:39:40 +02:00
tensor.shape[0], but sum of lengths is
2022-05-30 03:12:18 +02:00
${n}, and tensor's shape is: ${t.shape}`);if(!this.dynamicSize&&e.length!==this.maxSize)throw new Error(`TensorArray's size is not equal to the size of lengths (${this.maxSize} vs. ${e.length}), and the TensorArray is not marked as dynamically resizeable`);let r=n===0?0:t.size/n,a=[];q(()=>{t=U(t,[1,n,r]);for(let o=0;o<e.length;++o){let u=o===0?0:s[o-1],l=[0,u,0],c=[1,e[o],r];a[o]=U(qe(t,l,c),this.elementShape)}return a});let i=[];for(let o=0;o<e.length;o++)i[o]=o;this.writeMany(i,a)}},no=class{constructor(e,t,n,s=-1){this.tensors=e,this.elementShape=t,this.elementDtype=n,e!=null&&e.forEach(r=>{if(n!==r.dtype)throw new Error(`Invalid data types; op elements ${n}, but list elements ${r.dtype}`);qn(t,r.shape,"TensorList shape mismatch: "),Ht(r)}),this.idTensor=we(0),this.maxNumElements=s,Ht(this.idTensor)}get id(){return this.idTensor.id}copy(){return new no([...this.tensors],this.elementShape,this.elementDtype)}clearAndClose(e){this.tensors.forEach(t=>{(e==null||!e.has(t.id))&&t.dispose()}),this.tensors.length=0,this.idTensor.dispose()}size(){return this.tensors.length}stack(e,t,n=-1){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);if(n!==-1&&this.tensors.length!==n)throw new Error(`Operation expected a list with ${n} elements but got a list with ${this.tensors.length} elements.`);qn(e,this.elementShape,"TensorList shape mismatch: ");let s=_u(this.elementShape,this.tensors,e);return q(()=>{let r=this.tensors.map(a=>U(a,s));return es(r,0)})}popBack(e,t){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);if(this.size()===0)throw new Error("Trying to pop from an empty list.");let n=_u(this.elementShape,this.tensors,e),s=this.tensors.pop();return qn(s.shape,e,"TensorList shape mismatch: "),U(s,n)}pushBack(e){if(e.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${e.dtype}, but list elements ${this.elementDtype}`);if(qn(e.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");Ht(e),this.tensors.push(e)}resize(e){if(e<0)throw new Error(`TensorListResize expects size to be non-negative. Got: ${e}`);if(this.maxNumElements!==-1&&e>this.maxNumElements)throw new Error(`TensorListResize input size ${e} is greater maxNumElement ${this.maxNumElements}.`);let t=new no([],this.elementShape,this.elementDtype,this.maxNumElements);t.tensors.length=e;for(let n=0;n<Math.min(this.tensors.length,e);++n)t.tensors[n]=this.tensors[n];return t}getItem(e,t,n){if(n!==this.elementDtype)throw new Error(`Invalid data types; op elements ${n}, but list elements ${this.elementDtype}`);if(e<0||e>this.tensors.length)throw new Error(`Trying to access element ${e} in a list with ${this.tensors.length} elements.`);if(this.tensors[e]==null)throw new Error(`element at index ${e} is null.`);qn(this.tensors[e].shape,t,"TensorList shape mismatch: ");let s=_u(this.elementShape,this.tensors,t);return U(this.tensors[e],s)}setItem(e,t){if(t.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t.dtype}, but list elements ${this.elementDtype}`);if(e<0||this.maxNumElements!==-1&&e>=this.maxNumElements)throw new Error(`Trying to set element ${e} in a list with max ${this.maxNumElements} elements.`);qn(this.elementShape,t.shape,"TensorList shape mismatch: "),Ht(t),this.tensors[e]=t}gather(e,t,n){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);qn(this.elementShape,n,"TensorList shape mismatch: "),e=e.slice(0,this.size());let s=_u(this.elementShape,this.tensors,n);return e.length===0?ms([],[0].concat(s)):q(()=>{let r=e.map(a=>U(this.tensors[a],s));return es(r,0)})}concat(e,t){if(!!e&&e!==this.elementDtype)throw new Error(`TensorList dtype is ${this.elementDtype} but concat requested dtype ${e}`);qn(this.elementShape,t,"TensorList shape mismatch: ");let n=_u(this.elementShape,this.tensors,t);return this.size()===0?m
2022-04-21 15:39:40 +02:00
tensor.shape[0], but sum of lengths is
2022-05-30 03:12:18 +02:00
${s}, and tensor's shape is: ${e.shape}`);let a=e.shape.slice(1),i=Hm(a,n),o=s===0?0:e.size/s,u=q(()=>{let c=[];e=U(e,[1,s,o]);for(let p=0;p<t.length;++p){let d=p===0?0:r[p-1],h=[0,d,0],f=[1,t[p],o];c[p]=U(qe(e,h,f),i)}return e.dispose(),c}),l=new no([],n,e.dtype,t.length);for(let c=0;c<u.length;c++)l.setItem(c,u[c]);return l}var T4=async(e,t,n)=>{switch(e.op){case"If":case"StatelessIf":{let s=S("thenBranch",e,t,n),r=S("elseBranch",e,t,n),a=S("cond",e,t,n),i=S("args",e,t,n);return(await a.data())[0]?n.functionMap[s].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap):n.functionMap[r].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap)}case"While":case"StatelessWhile":{let s=S("body",e,t,n),r=S("cond",e,t,n),a=S("args",e,t,n),i=await n.functionMap[r].executeFunctionAsync(a,n.tensorArrayMap,n.tensorListMap),o=a.map(c=>c.id),u=await i[0].data();i.forEach(c=>{!c.kept&&o.indexOf(c.id)===-1&&c.dispose()});let l=a;for(;u[0];){let c=l;l=await n.functionMap[s].executeFunctionAsync(l,n.tensorArrayMap,n.tensorListMap);let p=l.map(h=>h.id);c.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()});let d=await n.functionMap[r].executeFunctionAsync(l,n.tensorArrayMap,n.tensorListMap);u=await d[0].data(),d.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()})}return l}case"LoopCond":{let s=S("pred",e,t,n);return[Ws(s)]}case"Switch":{let s=S("pred",e,t,n),r=S("data",e,t,n);return r.kept||(r=Ws(r)),(await s.data())[0]?[void 0,r]:[r,void 0]}case"Merge":{let s=e.inputNames.find(r=>on(r,t,n)!==void 0);if(s){let r=on(s,t,n);return[Ws(r)]}return}case"Enter":{let s=S("frameName",e,t,n),r=S("tensor",e,t,n);return n.enterFrame(s),[Ws(r)]}case"Exit":{let s=S("tensor",e,t,n);return n.exitFrame(),[Ws(s)]}case"NextIteration":{let s=S("tensor",e,t,n);return n.nextIteration(),[Ws(s)]}case"TensorArrayV3":{let s=S("size",e,t,n),r=S("dtype",e,t,n),a=S("elementShape",e,t,n),i=S("dynamicSize",e,t,n),o=S("clearAfterRead",e,t,n),u=S("identicalElementShapes",e,t,n),l=S("name",e,t,n),c=new k4(l,r,s,a,u,i,o);return n.addTensorArray(c),[c.idTensor,we(1)]}case"TensorArrayWriteV3":{let s=S("tensorArrayId",e,t,n),r=S("index",e,t,n),a=S("tensor",e,t,n),i=n.getTensorArray(s.id);return i.write(r,a),[i.idTensor]}case"TensorArrayReadV3":{let s=S("tensorArrayId",e,t,n),r=S("index",e,t,n);return[n.getTensorArray(s.id).read(r)]}case"TensorArrayGatherV3":{let s=S("tensorArrayId",e,t,n),r=S("indices",e,t,n),a=S("dtype",e,t,n);return[n.getTensorArray(s.id).gather(r,a)]}case"TensorArrayScatterV3":{let s=S("tensorArrayId",e,t,n),r=S("indices",e,t,n),a=S("tensor",e,t,n),i=n.getTensorArray(s.id);return i.scatter(r,a),[i.idTensor]}case"TensorArrayConcatV3":{let s=S("tensorArrayId",e,t,n),r=n.getTensorArray(s.id),a=S("dtype",e,t,n);return[r.concat(a)]}case"TensorArraySplitV3":{let s=S("tensorArrayId",e,t,n),r=S("tensor",e,t,n),a=S("lengths",e,t,n),i=n.getTensorArray(s.id);return i.split(a,r),[i.idTensor]}case"TensorArraySizeV3":{let s=S("tensorArrayId",e,t,n),r=n.getTensorArray(s.id);return[we(r.size(),"int32")]}case"TensorArrayCloseV3":{let s=S("tensorArrayId",e,t,n),r=n.getTensorArray(s.id);return r.clearAndClose(),[r.idTensor]}case"TensorListSetItem":{let s=S("tensorListId",e,t,n),r=S("index",e,t,n),a=S("tensor",e,t,n),i=n.getTensorList(s.id);return i.setItem(r,a),[i.idTensor]}case"TensorListGetItem":{let s=S("tensorListId",e,t,n),r=S("index",e,t,n),a=S("elementShape",e,t,n),i=S("elementDType",e,t,n);return[n.getTensorList(s.id).getItem(r,a,i)]}case"TensorListScatterV2":case"TensorListScatter":{let s=S("indices",e,t,n),r=S("tensor",e,t,n),a=S("elementShape",e,t,n),i=S("numElements",e,t,n),o=C4(r,s,a,i);return n.addTensorList(o),[o.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let s=S("elementShape",e,t,n),r=S("elementDType",e,t,n),a;e.op==="TensorListReserve"?a="numElements":a="maxNumElements";let i=S(a,e,t,n),o=I4(s,r,i);return n.addTensorList(o),[o.idTensor]}case"TensorListGather":{let s=S("tensorListId",e,t,n),r=S("indices",e,t,n),a=S("elementShape",e,t,n),i=S("elementDType",e,t,n);return[n.getTensor
${e}`);let s;return this.size===1/0||this.size==null?s=this.size:t?s=Math.ceil(this.size/e):s=Math.floor(this.size/e),$n(async()=>(await n.iterator()).columnMajorBatch(e,t,NU),s)}concatenate(e){let t=this,n;return this.size===1/0||e.size===1/0?n=1/0:this.size!=null&&e.size!=null?n=this.size+e.size:n=null,$n(async()=>(await t.iterator()).concatenate(await e.iterator()),n)}filter(e){let t=this,n;return this.size===1/0?n=1/0:n=null,$n(async()=>(await t.iterator()).filter(s=>q(()=>e(s))),n)}async forEachAsync(e){return(await this.iterator()).forEachAsync(e)}map(e){let t=this;return $n(async()=>(await t.iterator()).map(n=>q(()=>e(n))),this.size)}mapAsync(e){let t=this;return $n(async()=>(await t.iterator()).mapAsync(e),this.size)}prefetch(e){if(e==null)throw new RangeError("`Dataset.prefetch()` requires bufferSize to be specified.");let t=this;return $n(async()=>(await t.iterator()).prefetch(e),this.size)}repeat(e){let t=this,n;return this.size!=null&&e>0?n=this.size*e:e===0?n=0:this.size!=null&&(e===void 0||e<0)?n=1/0:n=null,$n(async()=>{let s=av(async()=>({value:await t.iterator(),done:!1}));return cU(s.take(e))},n)}skip(e){let t=this,n;return this.size!=null&&e>=0&&this.size>=e?n=this.size-e:this.size!=null&&(this.size<e||e===void 0||e<0)?n=0:n=null,$n(async()=>(await t.iterator()).skip(e),n)}shuffle(e,t,n=!0){if(e==null||e<0)throw this.size==null?new RangeError("`Dataset.shuffle()` requires bufferSize to be specified."):new RangeError(`\`Dataset.shuffle()\` requires bufferSize to be specified. If your data fits in main memory (for regular JS objects), and/or GPU memory (for \`tf.Tensor\`s), consider setting bufferSize to the dataset size (${this.size} elements)`);let s=this,r=nU.alea(t||w.now().toString());return $n(async()=>{let a=r.int32();return n&&(a+=r.int32()),(await s.iterator()).shuffle(e,a.toString())},this.size)}take(e){let t=this,n;return this.size!=null&&this.size>e?n=e:this.size!=null&&this.size<=e?n=this.size:n=null,$n(async()=>(await t.iterator()).take(e),n)}async toArray(){if(this.size===1/0)throw new Error("Can not convert infinite data stream to array.");return(await this.iterator()).toArray()}async toArrayForTest(){if(this.size===1/0)throw new Error("Can not convert infinite data stream to array.");return(await this.iterator()).toArrayForTest()}};tu.MAX_BUFFER_SIZE=1e4;function $n(e,t=null){return new class extends tu{constructor(){super(...arguments),this.size=t}async iterator(){return e()}}}function IU(e){return $n(async()=>B0(e),e.length)}function CU(e){if(!so(e))throw new Error("The argument to zip() must be an object or array.");let t;if(Array.isArray(e))for(let n=0;n<e.length;n++)t=t==null?e[n].size:Math.min(t,e[n].size);else if(e instanceof Object)for(let n in e)t=t==null?e[n].size:Math.min(t,e[n].size);return $n(async()=>{let n=await P0(e,s=>{if(s instanceof tu)return{value:s.iterator(),recurse:!1};if(so(s))return{value:null,recurse:!0};throw new Error("Leaves of the structure passed to zip() must be Datasets, not primitives.")});return dU(n,1)},t)}function NU(e){if(e===null)return null;let t=e[0];return iU(t)?{value:TU(e),recurse:!1}:{value:null,recurse:!0}}function TU(e){if(e.length===0)throw new Error("Can't make a batch of zero elements.");return e[0]instanceof et?es(e):ms(e)}var G0=class extends tu{constructor(e){super(),this.input=e}async iterator(){return(await this.input.iterator()).decodeUTF8().split(`
`).map(s=>(s.endsWith("\r")&&(s=s.slice(0,-1)),s))}},Zc='"',Au=Symbol("out"),ow=Symbol("field"),Jc=Symbol("quote"),em=Symbol("quoteafterquote"),uw=Symbol("quoteinquote"),H0=class extends tu{constructor(e,t){super(),this.input=e,this.hasHeader=!0,this.fullColumnNames=null,this.columnNamesValidated=!1,this.columnConfigs=null,this.configuredColumnsOnly=!1,this.delimiter=",",this.delimWhitespace=!1,this.base=new G0(e),t||(t={}),this.hasHeader=t.hasHeader!==!1,this.fullColumnNames=t.columnNames,this.columnConfigs=t.columnConfigs,this.configuredColumnsOnly=t.configuredColumnsOnly,t.delimWhitespace?(w.assert(t.delimiter==null,()=>"Delimiter should not be provided when delimWhitespace is true."),this.delimWhitespace=!0,this.delimiter=" "):this.delimiter=t.delimiter?t.delimiter:","}async columnNames(){return this.columnNamesValidated||await this.setColumnNames(),this.configuredColumnsOnly?Object.keys(this.columnConfigs):this.fullColumnNames}async setColumnNames(){let e=await this.maybeReadHeaderLine();if(!this.fullColumnNames&&!e)throw new Error("Column names must be provided if there is no header line.");this.fullColumnNames&&e&&w.assert(e.length===this.fullColumnNames.length,()=>"The length of provided columnNames ("+this.fullColumnNames.length.toString()+") does not match the length of the header line read from file ("+e.length.toString()+")."),this.fullColumnNames||(this.fullColumnNames=e);let t=this.fullColumnNames.reduce((s,r)=>(s[r]=s[r]+1||1,s),{}),n=Object.keys(t).filter(s=>t[s]>1);if(w.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let s of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(s)===-1)throw new Error('The key "'+s+'" provided in columnConfigs does not match any of the column names ('+this.fullColumnNames.toString()+").")}this.columnNamesValidated=!0}async maybeReadHeaderLine(){if(this.hasHeader){let t=await(await this.base.iterator()).next();if(t.done)throw new Error("No data was found for CSV parsing.");let n=t.value;return this.parseRow(n,!1)}else return null}async iterator(){this.columnNamesValidated||await this.setColumnNames();let e=await this.base.iterator();return this.hasHeader&&(e=e.skip(1)),e.map(t=>this.makeDataElement(t))}makeDataElement(e){let t=this.parseRow(e),n={},s={};for(let r=0;r<this.fullColumnNames.length;r++){let a=this.fullColumnNames[r],i=this.columnConfigs?this.columnConfigs[a]:null;if(!(this.configuredColumnsOnly&&!i)){let o=t[r],u=null;if(o==="")if(i&&i.default!==void 0)u=i.default;else{if(i&&(i.required||i.isLabel))throw new Error(`Required column ${a} is empty in this line: ${e}`);u=void 0}else{let l=Number(o);if(isNaN(l))i&&i.dtype==="bool"?u=this.getBoolean(o):u=o;else if(!i||!i.dtype)u=l;else switch(i.dtype){case"float32":u=l;break;case"int32":u=Math.floor(l);break;case"bool":u=this.getBoolean(o);break;default:u=l}}i&&i.isLabel?s[a]=u:n[a]=u}}return Object.keys(s).length===0?n:{xs:n,ys:s}}getBoolean(e){return e==="1"||e.toLowerCase()==="true"?1:0}parseRow(e,t=!0){let n=[],s=0,r=e.length,a=Au;for(let i=0;i<r;i++)switch(a){case Au:switch(e.charAt(i)){case Zc:s=i+1,a=Jc;break;case this.delimiter:if(s=i+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),a=Au;break;default:a=ow,s=i;break}break;case ow:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(s,i)),a=Au,s=i+1;break;default:}break;case Jc:switch(e.charAt(i)){case Zc:a=em;break;default:}break;case em:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(s,i-1)),a=Au,s=i+1;break;case Zc:a=Jc;break;default:a=uw;break}break;case uw:switch(e.charAt(i)){case Zc:a=Jc;break;default:}break;default:}if(a===em?n.push(e.substring(s,r-1)):n.push(e.substring(s)),t&&n.length!==this.fullColumnNames.length)throw new Error(`Invalid row in csv file. Should have ${this.fullColumnNames.length} elements in a row, but got ${n}`);return n}},q0=class extends Ut{constructor(e){super(),this.microphoneConfig=e,this.isClosed=!1,this.fftSize=e.fftSize||1024;let t=Math.log2(this.fftSize);if(this.fftSize<0||t<4||t>14||!Number.isInteger(t))throw new Error(`Invalid fftSi
2022-04-21 15:39:40 +02:00
============================
2022-05-18 14:33:33 +02:00
Hi, looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, visit https://github.com/tensorflow/tfjs-node for more details.
2022-05-30 03:12:18 +02:00
============================`));let s={id:this.nextDataId()};return this.data.set(s,{values:e,dtype:n,refCount:1}),s}makeTensorInfo(e,t,n){let s;if(t==="string"&&n!=null&&n.length>0&&w.isString(n[0])){let r=n.map(a=>w.encodeString(a));s=this.write(r,e,t)}else s=this.write(n,e,t);return{dataId:s,shape:e,dtype:t}}refCount(e){return this.data.has(e)?this.data.get(e).refCount:0}incRef(e){let t=this.data.get(e);t.refCount++}decRef(e){if(this.data.has(e)){let t=this.data.get(e);t.refCount--}}move(e,t,n,s,r){this.data.set(e,{values:t,dtype:s,refCount:r})}numDataIds(){return this.data.numDataIds()}async read(e){return this.readSync(e)}readSync(e){let{dtype:t,complexTensorInfos:n}=this.data.get(e);if(t==="complex64"){let s=this.readSync(n.real.dataId),r=this.readSync(n.imag.dataId);return C.mergeRealAndImagArrays(s,r)}return this.data.get(e).values}bufferSync(e){let t=this.readSync(e.dataId);if(e.dtype==="string")try{let n=t.map(s=>w.decodeString(s));return Ae(e.shape,e.dtype,n)}catch(n){throw new Error("Failed to decode encoded string bytes into utf-8")}return Ae(e.shape,e.dtype,t)}makeOutput(e,t,n){return ds().makeTensorFromTensorInfo(this.makeTensorInfo(t,n,e),this)}disposeData(e,t=!1){if(this.data.has(e)){if(this.data.get(e).refCount--,!t&&this.data.get(e).refCount>0)return!1;let{complexTensorInfos:n}=this.data.get(e);n!=null&&(this.disposeData(n.real.dataId,!0),this.disposeData(n.imag.dataId,!0)),this.data.delete(e)}return!0}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}async time(e){let t=w.now();return e(),{kernelMs:w.now()-t}}memory(){return{unreliable:!0,reasons:["The reported memory is an upper bound. Due to automatic garbage collection, the true allocated memory may be less."]}}where(e){be([e],"where");let t=this.readSync(e.dataId);return VU(e.shape,t)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}},tC=eC;tC.nextDataId=0;var ov={};Ee(ov,{addImpl:()=>sC,bincountImpl:()=>lv,bincountReduceImpl:()=>rC,ceilImpl:()=>aC,concatImpl:()=>cv,equalImpl:()=>iC,expImpl:()=>uC,expm1Impl:()=>cC,floorImpl:()=>dC,gatherNdImpl:()=>pC,gatherV2Impl:()=>hC,greaterEqualImpl:()=>mC,greaterImpl:()=>fC,lessEqualImpl:()=>bC,lessImpl:()=>gC,linSpaceImpl:()=>yC,logImpl:()=>vC,maxImpl:()=>xC,maximumImpl:()=>wC,minimumImpl:()=>kC,multiplyImpl:()=>dv,negImpl:()=>SC,notEqualImpl:()=>IC,prodImpl:()=>CC,rangeImpl:()=>hv,rsqrtImpl:()=>NC,scatterImpl:()=>qi,sigmoidImpl:()=>_G,simpleAbsImpl:()=>nC,sliceImpl:()=>Bd,sparseFillEmptyRowsImpl:()=>$C,sparseReshapeImpl:()=>_C,sparseSegmentReductionImpl:()=>fv,sqrtImpl:()=>RG,squaredDifferenceImpl:()=>AC,stridedSliceImpl:()=>EC,stringNGramsImpl:()=>RC,stringSplitImpl:()=>DC,stringToHashBucketFastImpl:()=>FC,subImpl:()=>OC,tileImpl:()=>PC,topKImpl:()=>MC,transposeImpl:()=>pv,uniqueImpl:()=>LC});function nC(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var WU=e=>{let{x:t}=e.inputs,n=e.backend;be(t,"abs");let s=new Float32Array(w.sizeFromShape(t.shape)),r=n.data.get(t.dataId).values;return s=nC(r),n.makeOutput(s,t.shape,t.dtype)},UU={kernelName:lo,backendName:"cpu",kernelFunc:WU};function At(e){return(t,n,s,r,a)=>{let i=C.assertAndGetBroadcastShape(t,n),o=i.length,u=w.computeStrides(i),l=w.sizeFromShape(i),c=w.getTypedArrayFromDType(a,l),p=t.length,d=n.length,h=w.computeStrides(t),f=w.computeStrides(n),m=C.getBroadcastDims(t,i),g=C.getBroadcastDims(n,i);if(m.length+g.length===0)for(let b=0;b<c.length;++b)c[b]=e(s[b%s.length],r[b%r.length]);else for(let b=0;b<c.length;++b){let y=w.indexToLoc(b,o,u),v=y.slice(-p);m.forEach($=>v[$]=0);let x=w.locToIndex(v,p,h),k=y.slice(-d);g.forEach($=>k[$]=0);let I=w.locToIndex(k,d,f);c[b]=e(s[x],r[I])}return[c,i]}}function En(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.data.get(s.dataId).values,i=n.data.get(r.dataId).values,o=n.makeTensorInfo(s.shape,"complex64"),u=n.data.get(o.dataId);return u.complexTensorInfos={real:n.makeTensorInfo(s.shape,"float32",a),imag:n.makeTensorInfo(r.shape,"float32",i)},o}var GU={kernelName:tp,backendName:"cpu",kernelFunc:En};function Ld(e,t,n="float32"){if(n==="
2022-04-21 15:39:40 +02:00
${a.shape}`);if(s.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${s.shape}`);if(r.shape.length!==1)throw new Error(`Values must be a vector, saw:
${r.shape}`);if(i.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
2022-05-30 03:12:18 +02:00
${i.shape}`);let o=n.data.get(s.dataId).values,u=n.data.get(r.dataId).values,l=n.data.get(a.dataId).values,c=n.data.get(i.dataId).values[0],[p,d,h,f,m]=$C(o,s.shape,s.dtype,u,r.dtype,l,c);return[n.makeTensorInfo(d,s.dtype,p),n.makeTensorInfo([d[0]],r.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(g=>Number(g)))),n.makeTensorInfo([m.length],s.dtype,new Int32Array(m))]}var i5={kernelName:dp,backendName:"cpu",kernelFunc:a5};function o5(e){let{inputs:t,backend:n}=e,{inputIndices:s,inputShape:r,newShape:a}=t;if(s.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape
2022-04-21 15:39:40 +02:00
${s.shape}`);if(r.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
2022-05-30 03:12:18 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let i=Array.from(n.data.get(r.dataId).values),o=n.data.get(s.dataId).values,u=Array.from(n.data.get(a.dataId).values),[l,c,p]=_C(o,s.shape,s.dtype,i,u);return[n.makeTensorInfo(c,s.dtype,l),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var u5={kernelName:Dl,backendName:"cpu",kernelFunc:o5};function l5(e){let{inputs:t,backend:n}=e,{data:s,indices:r,segmentIds:a}=t;if(s.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2022-04-21 15:39:40 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2022-05-30 03:12:18 +02:00
${a.shape}`);if(r.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let i=n.data.get(s.dataId).values,o=n.data.get(r.dataId).values,u=n.data.get(a.dataId).values,[l,c]=fv(i,s.shape,s.dtype,o,u,!0);return n.makeTensorInfo(c,s.dtype,l)}var c5={kernelName:pp,backendName:"cpu",kernelFunc:l5};function d5(e){let{inputs:t,backend:n}=e,{data:s,indices:r,segmentIds:a}=t;if(s.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2022-04-21 15:39:40 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2022-05-30 03:12:18 +02:00
${a.shape}`);if(r.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let i=n.data.get(s.dataId).values,o=n.data.get(r.dataId).values,u=n.data.get(a.dataId).values,[l,c]=fv(i,s.shape,s.dtype,o,u);return n.makeTensorInfo(c,s.dtype,l)}var p5={kernelName:hp,backendName:"cpu",kernelFunc:d5};function h5(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:i}=t,{outputShape:o}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=C.calculateShapes(a,r,o),h=!1,f=n.bufferSync(r),m;switch(a.dtype){case"bool":{let g=n.bufferSync(a),b=Boolean(n.data.get(i.dataId).values[0]);m=qi(f,g,o,d,c,l,u,p,b,h);break}case"float32":{let g=n.bufferSync(a),b=n.data.get(i.dataId).values[0];m=qi(f,g,o,d,c,l,u,p,b,h);break}case"int32":{let g=n.bufferSync(a),b=n.data.get(i.dataId).values[0];m=qi(f,g,o,d,c,l,u,p,b,h);break}case"string":{let g=n.bufferSync(a),b=w.decodeString(n.data.get(i.dataId).values[0]);m=qi(f,g,o,d,c,l,u,p,b,h);break}default:throw new Error(`Unsupported type ${a.dtype}`)}return n.makeTensorInfo(o,m.dtype,m.values)}var f5={kernelName:fp,backendName:"cpu",kernelFunc:h5};function m5(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=s,o=w.parseAxisParam(i,r.shape)[0],u=C.prepareSplitSize(r,a,o),l=new Array(r.shape.length).fill(0),c=r.shape.slice();return u.map(p=>{let d=[...c];d[o]=p;let h=ba({inputs:{x:r},backend:n,attrs:{begin:l,size:d}});return l[o]+=p,h})}var g5={kernelName:Vo,backendName:"cpu",kernelFunc:m5},b5={kernelName:Fl,backendName:"cpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,s=t;be(n,"square");let r=s.data.get(n.dataId).values,a=new Float32Array(r.length);for(let o=0;o<r.length;++o){let u=r[o];a[o]=u*u}return{dataId:s.write(a,n.shape,n.dtype),shape:n.shape,dtype:n.dtype}}},y5=st(mi,(e,t)=>{let n=t;return isNaN(e)?NaN:e>0?1:n.alpha}),v5={kernelName:mi,backendName:"cpu",kernelFunc:y5};function x5(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s;be(r,"stridedSlice");let{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=kt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=pt({inputs:{x:r},backend:n,attrs:{shape:f}});else if(g||b){w.assert(r.shape.length>=1,()=>`Input must have rank at least 1, got: ${r.shape.length}`);let I=kt.computeOutShape(y,v,x),$=ba({inputs:{x:r},backend:n,attrs:{begin:y,size:I}});k=pt({inputs:{x:$},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo($)}else{let I=n.bufferSync(r),$=EC(h,I,x,y);k=n.makeTensorInfo(f,$.dtype,$.values)}return k}var w5={kernelName:Wo,backendName:"cpu",kernelFunc:x5};function k5(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:i,rightPad:o,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.data.get(c.dataId).values,h=n.data.get(p.dataId).values,[f,m]=RC(d,h,r,a,i,o,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var S5={kernelName:mp,backendName:"cpu",kernelFunc:k5};function I5(e){let{inputs:t,backend:n,attrs:s}=e,{skipEmpty:r}=s,{input:a,delimiter:i}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(i.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${i.shape}`);let o=n.data.get(a.dataId).values,u=n.data.get(i.dataId).values[0],[l,c,p]=DC(o,u,r),d=c.length;return[n.makeTensorInfo([d,2],"int32",l),n.makeTensorInfo([d],"string",c),n.makeTensorInfo([2],"int32",new Int32Array(p))]}var C5={kernelName:zg,backendName:"cpu",kernelFunc:I5};function N5(e){let{inputs:t,backend:n,attrs:s}=e,{numBuckets:r}=s,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(r<=0)throw new Error("Number of buckets must be at least 1");let i=n.data.get(a.dataId).values,o=FC(i,r);return n.makeTensorInfo(a.shape,"int32",o)}var T5={kernelName:Mg,backendName:"cpu",kernelFunc:N5},$5=st(Uo,e=>Math.tan(e)),_5=
2022-04-21 15:39:40 +02:00
`),a=r.length.toString().length+2,i=r.map((p,d)=>w.rightPad((d+1).toString(),a)+p),o=0;for(let p=0;p<i.length;p++)o=Math.max(i[p].length,o);let u=i.slice(0,s-1),l=i.slice(s-1,s),c=i.slice(s);console.log(u.join(`
`)),console.log(t.split(`
`)[0]),console.log(`%c ${w.rightPad(l[0],o)}`,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(c.join(`
2022-05-30 03:12:18 +02:00
`))}function i1(e){return Qs(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function o1(e,t){if(fe(e,()=>e.linkProgram(t)),!K().get("ENGINE_COMPILE_ONLY")&&e.getProgramParameter(t,e.LINK_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Failed to link vertex and fragment shaders.")}function ud(e,t){if(fe(e,()=>e.validateProgram(t)),e.getProgramParameter(t,e.VALIDATE_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Shader program validation failed.")}function u1(e,t){let n=Qs(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),fe(e,()=>e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function l1(e,t){let n=Qs(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return fe(e,()=>e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,n)),fe(e,()=>e.bufferData(e.ELEMENT_ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function oK(){return K().getNumber("WEBGL_VERSION")===2?1:4}function c1(e){return Qs(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function d1(e,t){let n=K().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e<=0||t<=0){let s=`[${e}x${t}]`;throw new Error("Requested texture size "+s+" is invalid.")}if(e>n||t>n){let s=`[${e}x${t}]`,r=`[${n}x${n}]`;throw new Error("Requested texture size "+s+" greater than WebGL maximum on this browser / GPU "+r+".")}}function p1(e){return Qs(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function Xm(e,t,n,s,r,a,i){let o=e.getAttribLocation(t,n);return o===-1?!1:(fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,s)),fe(e,()=>e.vertexAttribPointer(o,r,e.FLOAT,!1,a,i)),fe(e,()=>e.enableVertexAttribArray(o)),!0)}function h1(e,t,n){y1(e,n),fe(e,()=>e.activeTexture(e.TEXTURE0+n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function uK(e,t){y1(e,t),fe(e,()=>e.activeTexture(e.TEXTURE0+t)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function f1(e,t,n){return Qs(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function m1(e,t,n){return e.getUniformLocation(t,n)}function g1(e,t,n,s){fe(e,()=>h1(e,t,s)),fe(e,()=>e.uniform1i(n,s))}function lK(e){fe(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,null)),fe(e,()=>e.viewport(0,0,e.canvas.width,e.canvas.height)),fe(e,()=>e.scissor(0,0,e.canvas.width,e.canvas.height))}function ld(e,t,n){fe(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,n)),fe(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0))}function Ym(e,t){fe(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,t)),fe(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0))}function Mu(e){let t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+b1(e,t))}function b1(e,t){switch(t){case e.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case e.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return`unknown error ${t}`}}function Qs(e,t,n){let s=fe(e,()=>t());if(s==null)throw new Error(n);return s}function y1(e,t){let n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,s=t+e.TEXTURE0;if(s<e.TEXTURE0||s>n){let r=`[gl.TEXTURE0, gl.TEXTURE${n}]`;throw new Error(`textureUnit must be in ${r}.`)}}function ya(e,t=2){return w.sizeFromShape(e.slice(0,e.length-t))}function va(e){if(e.length===0)throw Error("Cannot get rows and columns of an empty shape array.");return[e.length>1?e[e.length-2]:1,e[e.length-1]]}function cd(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[ya(e),...va(e)]),t}function v1(e,t=!1){let n=K().getNumber("WEBGL_MAX_TEXTURE_SIZE");t&&(n=n*2,e=e.map((r,a)=>a>=e.length-2?w.nearestLargerEven(e[a]):e[a]),e.length===1&&(e=[2,e[0]])),e.length!==2&&(e=w.squeezeShape(e).newShape);let s=w.sizeFromShape(e);if(e.length<=1&&s<=n)return[1,s];if(e.length===2&&e[0]<=n&&e[1]<=n)return e;if(e.length===3&&e[0]*e[1]<=n&&e[2]<=n)return[e[0]*e[1],e[2]];if(e.length===3&&e[0]<=n&&e[1]*e[2]<=n)return[e[0],e[1]
2022-04-21 15:39:40 +02:00
bool isnan_custom(float val) {
uint floatToUint = floatBitsToUint(val);
return (floatToUint & 0x7fffffffu) > 0x7f800000u;
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
bvec4 isnan_custom(vec4 val) {
return bvec4(isnan_custom(val.x),
isnan_custom(val.y), isnan_custom(val.z), isnan_custom(val.w));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
#define isnan(value) isnan_custom(value)
`,u="",l=`
#define round(value) newRound(value)
int newRound(float value) {
return int(floor(value + 0.5));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
ivec4 newRound(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`):(e="",t="attribute",n="varying",s="varying",r="texture2D",a="gl_FragColor",i="",o=`
#define isnan(value) isnan_custom(value)
bool isnan_custom(float val) {
return (val > 0. || val < 1. || val == 0.) ? false : true;
2022-04-01 15:12:04 +02:00
}
2022-04-21 15:39:40 +02:00
bvec4 isnan_custom(vec4 val) {
return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));
2022-04-01 15:12:04 +02:00
}
2022-04-21 15:39:40 +02:00
`,u=`
uniform float INFINITY;
bool isinf(float val) {
return abs(val) == INFINITY;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
bvec4 isinf(vec4 val) {
return equal(abs(val), vec4(INFINITY));
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`,l=`
int round(float value) {
return int(floor(value + 0.5));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
ivec4 round(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`),{version:e,attribute:t,varyingVs:n,varyingFs:s,texture2D:r,output:a,defineOutput:i,defineSpecialNaN:o,defineSpecialInf:u,defineRound:l}}function xi(e,t,n="index"){let s=w.computeStrides(t);return s.map((r,a)=>{let i=`int ${e[a]} = ${n} / ${r}`,o=a===s.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * ${r}`:`index -= ${e[a]} * ${r}`;return`${i}; ${o};`}).join("")}function th(e,t,n="index"){let s=w.computeStrides(t);return s.map((r,a)=>{let i=`int ${e[a]} = ${n} / outShapeStrides[${a}]`,o=a===s.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * outShapeStrides[${a}]`:`index -= ${e[a]} * outShapeStrides[${a}]`;return`${i}; ${o};`}).join("")}function hK(e,t){let n=e.length,s=e.map(a=>`${t}[${a}]`),r=new Array(n-1);r[n-2]=s[n-1];for(let a=n-3;a>=0;--a)r[a]=`(${r[a+1]} * ${s[a+1]})`;return r}function fK(e,t,n="index"){let s=e.map((a,i)=>i),r=hK(s,t);return r.map((a,i)=>{let o=`int ${e[i]} = ${n} / ${r[i]}`,u=i===r.length-1?`int ${e[i+1]} = ${n} - ${e[i]} * ${r[i]}`:`index -= ${e[i]} * ${r[i]}`;return`${o}; ${u};`}).join("")}function wv(e){let t=w.computeStrides(e).map(n=>n.toString());return`
2022-04-21 15:39:40 +02:00
int getFlatIndex(ivec3 coords) {
return coords.x * ${t[0]} + coords.y * ${t[1]} + coords.z;
}
2022-05-30 03:12:18 +02:00
`}function kv(){return`
2022-04-21 15:39:40 +02:00
int getFlatIndex(ivec3 coords) {
return coords.x * outShapeStrides[0] + coords.y * outShapeStrides[1] + coords.z;
}
2022-05-30 03:12:18 +02:00
`}var N1=`
2022-04-21 15:39:40 +02:00
const float FLOAT_MAX = 1.70141184e38;
const float FLOAT_MIN = 1.17549435e-38;
lowp vec4 encode_float(highp float v) {
if (isnan(v)) {
return vec4(255, 255, 255, 255);
}
highp float av = abs(v);
if(av < FLOAT_MIN) {
return vec4(0.0, 0.0, 0.0, 0.0);
} else if(v > FLOAT_MAX) {
return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;
} else if(v < -FLOAT_MAX) {
return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;
}
highp vec4 c = vec4(0,0,0,0);
highp float e = floor(log2(av));
highp float m = exp2(fract(log2(av))) - 1.0;
c[2] = floor(128.0 * m);
m -= c[2] / 128.0;
c[1] = floor(32768.0 * m);
m -= c[1] / 32768.0;
c[0] = floor(8388608.0 * m);
highp float ebias = e + 127.0;
c[3] = floor(ebias / 2.0);
ebias -= c[3] * 2.0;
c[2] += floor(ebias) * 128.0;
c[3] += 128.0 * step(0.0, -v);
return c / 255.0;
}
2022-05-30 03:12:18 +02:00
`,{getBroadcastDims:T1}=C;function mK(e,t,n){let s=[];if(e.forEach(h=>{let f=w.sizeFromShape(h.shapeInfo.logicalShape);if(h.shapeInfo.isUniform?s.push(`uniform float ${h.name}${f>1?`[${f}]`:""};`):(s.push(`uniform sampler2D ${h.name};`),s.push(`uniform int offset${h.name};`)),n.enableShapeUniforms){let{uniformShape:m}=Sv(n.packedInputs,h.shapeInfo.logicalShape,h.shapeInfo.texShape);switch(m.length){case 1:s.push(`uniform int ${h.name}Shape;`);break;case 2:s.push(`uniform ivec2 ${h.name}Shape;`);break;case 3:s.push(`uniform ivec3 ${h.name}Shape;`);break;case 4:s.push(`uniform ivec4 ${h.name}Shape;`);break;default:break}s.push(`uniform ivec2 ${h.name}TexShape;`)}}),n.enableShapeUniforms){switch(t.logicalShape.length){case 1:s.push("uniform int outShape;");break;case 2:s.push("uniform ivec2 outShape;"),s.push("uniform int outShapeStrides;");break;case 3:s.push("uniform ivec3 outShape;"),s.push("uniform ivec2 outShapeStrides;");break;case 4:s.push("uniform ivec4 outShape;"),s.push("uniform ivec3 outShapeStrides;");break;default:break}s.push("uniform ivec2 outTexShape;")}n.customUniforms&&n.customUniforms.forEach(h=>{s.push(`uniform ${h.type} ${h.name}${h.arrayIndex?`[${h.arrayIndex}]`:""};`)});let r=s.join(`
`),a=e.map(h=>gK(h,t,n.packedInputs,n.enableShapeUniforms)).join(`
`),i=t.texShape,o=fn(),u=vK(o),l,c,p=kK(o);return t.isPacked?(l=bK(t.logicalShape,i,n.enableShapeUniforms),c=wK(o)):(l=yK(t.logicalShape,i,n.enableShapeUniforms),c=xK(o)),n.packedInputs&&(p+=NK),[p,u,c,r,l,a,n.userCode].join(`
`)}function au(e,t=!1){let n=e.shapeInfo.logicalShape;switch(n.length){case 0:return MK(e,t);case 1:return BK(e,t);case 2:return WK(e,t);case 3:return GK(e,t);case 4:return qK(e,t);case 5:return jK(e);case 6:return KK(e);default:throw new Error(`${n.length}-D input sampling is not yet supported`)}}function $1(e,t){switch(e.shapeInfo.logicalShape.length){case 0:return zK(e);case 1:return LK(e,t);case 2:return VK(e,t);case 3:return UK(e,t);default:return HK(e,t)}}function gK(e,t,n=!1,s){let r="";n?r+=$1(e,s):r+=au(e,s);let a=e.shapeInfo.logicalShape,i=t.logicalShape;return a.length<=i.length&&(n?r+=XK(e,t):r+=YK(e,t)),r}function bK(e,t,n){switch(e.length){case 0:return _1();case 1:return TK(e,t,n);case 2:return OK(e,t,n);case 3:return _K(e,t,n);default:return EK(e,t,n)}}function yK(e,t,n){switch(e.length){case 0:return _1();case 1:return $K(e,t,n);case 2:return PK(e,t,n);case 3:return AK(e,t,n);case 4:return RK(e,t,n);case 5:return DK(e,t);case 6:return FK(e,t);default:throw new Error(`${e.length}-D output sampling is not yet supported`)}}function vK(e){return`
2022-04-21 15:39:40 +02:00
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${e.texture2D}(textureSampler, uv).r;
}
2022-05-30 03:12:18 +02:00
`}function xK(e){return`
2022-04-21 15:39:40 +02:00
void setOutput(float val) {
${e.output} = vec4(val, 0, 0, 0);
}
2022-05-30 03:12:18 +02:00
`}function wK(e){return`
2022-04-21 15:39:40 +02:00
void setOutput(vec4 val) {
${e.output} = val;
}
2022-05-30 03:12:18 +02:00
`}function kK(e){return`${e.version}
2022-04-21 15:39:40 +02:00
precision highp float;
precision highp int;
precision highp sampler2D;
${e.varyingFs} vec2 resultUV;
${e.defineOutput}
const vec2 halfCR = vec2(0.5, 0.5);
struct ivec5
{
int x;
int y;
int z;
int w;
int u;
2022-04-21 15:38:36 +02:00
};
2022-04-21 15:39:40 +02:00
struct ivec6
{
int x;
int y;
int z;
int w;
int u;
int v;
2022-04-21 15:38:36 +02:00
};
2022-04-21 15:39:40 +02:00
uniform float NAN;
${e.defineSpecialNaN}
${e.defineSpecialInf}
${e.defineRound}
int imod(int x, int y) {
return x - y * (x / y);
}
int idiv(int a, int b, float sign) {
int res = a / b;
int mod = imod(a, b);
if (sign < 0. && mod != 0) {
res -= 1;
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
return res;
}
//Based on the work of Dave Hoskins
//https://www.shadertoy.com/view/4djSRW
#define HASHSCALE1 443.8975
float random(float seed){
vec2 p = resultUV * seed;
vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);
p3 += dot(p3, p3.yzx + 19.19);
return fract((p3.x + p3.y) * p3.z);
}
2022-05-22 14:50:51 +02:00
${SK}
2022-05-30 03:12:18 +02:00
${IK}
${CK}
`}var SK=`
2022-04-21 15:39:40 +02:00
vec2 uvFromFlat(int texNumR, int texNumC, int index) {
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 packedUVfrom1D(int texNumR, int texNumC, int index) {
int texelIndex = index / 2;
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2022-05-30 03:12:18 +02:00
`,IK=`
2022-04-21 15:39:40 +02:00
vec2 packedUVfrom2D(int texelsInLogicalRow, int texNumR,
int texNumC, int row, int col) {
int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2);
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2022-05-30 03:12:18 +02:00
`,CK=`
2022-04-21 15:39:40 +02:00
vec2 packedUVfrom3D(int texNumR, int texNumC,
int texelsInBatch, int texelsInLogicalRow, int b,
int row, int col) {
int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2);
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2022-05-30 03:12:18 +02:00
`,NK=`
2022-04-21 15:39:40 +02:00
float getChannel(vec4 frag, vec2 innerDims) {
vec2 modCoord = mod(innerDims, 2.);
return modCoord.x == 0. ?
(modCoord.y == 0. ? frag.r : frag.g) :
(modCoord.y == 0. ? frag.b : frag.a);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
float getChannel(vec4 frag, int dim) {
float modCoord = mod(float(dim), 2.);
return modCoord == 0. ? frag.r : frag.g;
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`;function _1(){return`
2022-04-21 15:39:40 +02:00
int getOutputCoords() {
return 0;
}
2022-05-30 03:12:18 +02:00
`}function TK(e,t,n){let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return s[0]===1?n?`
2022-04-21 15:39:40 +02:00
int getOutputCoords() {
return 2 * int(resultUV.x * ceil(float(outTexShape[1]) / 2.0));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
int getOutputCoords() {
return 2 * int(resultUV.x * ${s[1]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:s[1]===1?n?`
int getOutputCoords() {
return 2 * int(resultUV.y * ceil(float(outTexShape[0]) / 2.0));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
int getOutputCoords() {
return 2 * int(resultUV.y * ${s[0]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:n?`
int getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
return 2 * (resTexRC.x * packedTexShape[1] + resTexRC.y);
}
`:`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${s[0]}, ${s[1]}));
return 2 * (resTexRC.x * ${s[1]} + resTexRC.y);
}
2022-05-30 03:12:18 +02:00
`}function $K(e,t,n){return t[0]===1?n?`
2022-04-21 15:39:40 +02:00
int getOutputCoords() {
return int(resultUV.x * float(outTexShape[1]));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
int getOutputCoords() {
return int(resultUV.x * ${t[1]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:t[1]===1?n?`
int getOutputCoords() {
return int(resultUV.y * float(outTexShape[0]));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
int getOutputCoords() {
return int(resultUV.y * ${t[0]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:n?`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
return resTexRC.x * outTexShape[1] + resTexRC.y;
}
`:`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
return resTexRC.x * ${t[1]} + resTexRC.y;
}
2022-05-30 03:12:18 +02:00
`}function _K(e,t,n){if(n)return`
2022-04-21 15:39:40 +02:00
ivec3 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[2]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec3(b, r, c);
}
`;let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],r=Math.ceil(e[2]/2),a=r*Math.ceil(e[1]/2);return`
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${s[0]}, ${s[1]}));
int index = resTexRC.x * ${s[1]} + resTexRC.y;
int b = index / ${a};
index -= b * ${a};
int r = 2 * (index / ${r});
int c = imod(index, ${r}) * 2;
return ivec3(b, r, c);
}
2022-05-30 03:12:18 +02:00
`}function AK(e,t,n){if(n)return`
2022-04-21 15:39:40 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
2022-05-30 03:12:18 +02:00
${th(["r","c","d"],e)}
2022-04-21 15:39:40 +02:00
return ivec3(r, c, d);
2022-04-21 15:38:36 +02:00
}
2022-05-24 13:28:51 +02:00
`;let s=xi(["r","c","d"],e);return`
2022-04-21 15:39:40 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${s}
return ivec3(r, c, d);
}
2022-05-30 03:12:18 +02:00
`}function EK(e,t,n){if(n)return`
2022-04-21 15:39:40 +02:00
ivec4 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int texelsInLogicalRow = int(ceil(float(outShape[3]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[2]) / 2.0));
int texelsInBatchN = texelsInBatch * outShape[1];
int b2 = index / texelsInBatchN;
index -= b2 * texelsInBatchN;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec4(b2, b, r, c);
}
`;let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],r=Math.ceil(e[e.length-1]/2),a=r*Math.ceil(e[e.length-2]/2),i=a,o="",u="b, r, c";for(let l=2;l<e.length-1;l++)i*=e[e.length-l-1],o=`
int b${l} = index / ${i};
index -= b${l} * ${i};
`+o,u=`b${l}, `+u;return`
ivec${e.length} getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${s[0]}, ${s[1]}));
int index = resTexRC.x * ${s[1]} + resTexRC.y;
${o}
int b = index / ${a};
index -= b * ${a};
int r = 2 * (index / ${r});
int c = imod(index, ${r}) * 2;
return ivec${e.length}(${u});
}
2022-05-30 03:12:18 +02:00
`}function RK(e,t,n){if(n)return`
2022-04-21 15:39:40 +02:00
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
2022-05-30 03:12:18 +02:00
${th(["r","c","d","d2"],e)}
2022-04-21 15:39:40 +02:00
return ivec4(r, c, d, d2);
}
2022-05-24 13:28:51 +02:00
`;let s=xi(["r","c","d","d2"],e);return`
2022-04-21 15:39:40 +02:00
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${s}
return ivec4(r, c, d, d2);
}
2022-05-30 03:12:18 +02:00
`}function DK(e,t){let n=xi(["r","c","d","d2","d3"],e);return`
2022-04-21 15:39:40 +02:00
ivec5 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(${t[0]},
${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${n}
ivec5 outShape = ivec5(r, c, d, d2, d3);
return outShape;
}
2022-05-30 03:12:18 +02:00
`}function FK(e,t){let n=xi(["r","c","d","d2","d3","d4"],e);return`
2022-04-21 15:39:40 +02:00
ivec6 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${n}
ivec6 result = ivec6(r, c, d, d2, d3, d4);
return result;
}
2022-05-30 03:12:18 +02:00
`}function OK(e,t,n){let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];if(w.arraysEqual(e,t))return n?`
2022-04-21 15:39:40 +02:00
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
return 2 * ivec2(resultUV.yx * vec2(packedTexShape[0], packedTexShape[1]));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
ivec2 getOutputCoords() {
return 2 * ivec2(resultUV.yx * vec2(${s[0]}, ${s[1]}));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`;let r=Math.ceil(e[1]/2);return n?`
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec2(r, c);
}
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${s[0]}, ${s[1]}));
int index = resTexRC.x * ${s[1]} + resTexRC.y;
int r = 2 * (index / ${r});
int c = imod(index, ${r}) * 2;
return ivec2(r, c);
}
2022-05-30 03:12:18 +02:00
`}function PK(e,t,n){return w.arraysEqual(e,t)?n?`
2022-04-21 15:39:40 +02:00
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(outTexShape[0], outTexShape[1]));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(${t[0]}, ${t[1]}));
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:e[1]===1?n?`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
return ivec2(index, 0);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
return ivec2(index, 0);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:e[0]===1?n?`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
return ivec2(0, index);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
return ivec2(0, index);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:n?`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
int r = index / outShape[1];
int c = index - r * outShape[1];
return ivec2(r, c);
}
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
int r = index / ${e[1]};
int c = index - r * ${e[1]};
return ivec2(r, c);
}
2022-05-30 03:12:18 +02:00
`}function wi(e){return`offset${e}`}function zK(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),s=fn();return`
2022-04-21 15:39:40 +02:00
vec4 ${n}() {
return ${s.texture2D}(${t}, halfCR);
}
2022-05-30 03:12:18 +02:00
`}function MK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`float ${s}() {return ${n};}`;let[r,a]=e.shapeInfo.texShape;if(r===1&&a===1)return`
2022-04-21 15:39:40 +02:00
float ${s}() {
return sampleTexture(${n}, halfCR);
2022-02-10 18:27:21 +01:00
}
2022-05-24 13:28:51 +02:00
`;let i=wi(n);if(t)return`
2022-04-21 15:39:40 +02:00
float ${s}() {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], ${i});
return sampleTexture(${n}, uv);
}
`;let[o,u]=e.shapeInfo.texShape;return`
float ${s}() {
vec2 uv = uvFromFlat(${o}, ${u}, ${i});
return sampleTexture(${n}, uv);
}
2022-05-30 03:12:18 +02:00
`}function LK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape,a=fn();if(t)return`
2022-04-21 15:39:40 +02:00
vec4 ${s}(int index) {
ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
vec2 uv = packedUVfrom1D(
packedTexShape[0], packedTexShape[1], index);
return ${a.texture2D}(${n}, uv);
}
`;let i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)];return`
vec4 ${s}(int index) {
vec2 uv = packedUVfrom1D(
${i[0]}, ${i[1]}, index);
return ${a.texture2D}(${n}, uv);
}
2022-05-30 03:12:18 +02:00
`}function BK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`
2022-04-21 15:39:40 +02:00
float ${s}(int index) {
2022-05-24 13:28:51 +02:00
${iu(e)}
2022-04-21 15:39:40 +02:00
}
`;let r=e.shapeInfo.texShape,a=r[0],i=r[1];if(i===1&&a===1)return`
float ${s}(int index) {
return sampleTexture(${n}, halfCR);
}
2022-05-24 13:28:51 +02:00
`;let o=wi(n);return i===1?t?`
2022-04-21 15:39:40 +02:00
float ${s}(int index) {
vec2 uv = vec2(0.5, (float(index + ${o}) + 0.5) / float(${n}TexShape[0]));
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
vec2 uv = vec2(0.5, (float(index + ${o}) + 0.5) / ${a}.0);
return sampleTexture(${n}, uv);
}
`:a===1?t?`
float ${s}(int index) {
vec2 uv = vec2((float(index + ${o}) + 0.5) / float(${n}TexShape[1]), 0.5);
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
vec2 uv = vec2((float(index + ${o}) + 0.5) / ${i}.0, 0.5);
return sampleTexture(${n}, uv);
}
`:t?`
float ${s}(int index) {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], index + ${o});
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
vec2 uv = uvFromFlat(${a}, ${i}, index + ${o});
return sampleTexture(${n}, uv);
}
2022-05-30 03:12:18 +02:00
`}function VK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape,i=a[0],o=a[1],u=fn();if(a!=null&&w.arraysEqual(n,a))return t?`
2022-04-21 15:39:40 +02:00
vec4 ${r}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${s}TexShape[1], ${s}TexShape[0]);
return ${u.texture2D}(${s}, uv);
}
`:`
vec4 ${r}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${o}.0, ${i}.0);
return ${u.texture2D}(${s}, uv);
}
`;if(t)return`
vec4 ${r}(int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${s}TexShape[0]) / 2.0), ceil(float(${s}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${s}Shape[1]) / 2.0));
vec2 uv = packedUVfrom2D(valuesPerRow, packedTexShape[0], packedTexShape[1], row, col);
return ${u.texture2D}(${s}, uv);
}
`;let l=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],c=Math.ceil(n[1]/2);return`
vec4 ${r}(int row, int col) {
vec2 uv = packedUVfrom2D(${c}, ${l[0]}, ${l[1]}, row, col);
return ${u.texture2D}(${s}, uv);
}
2022-05-30 03:12:18 +02:00
`}function WK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape;if(a!=null&&w.arraysEqual(n,a)){if(t)return`
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
}
`;let d=a[0],h=a[1];return`
float ${r}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${h}.0, ${d}.0);
return sampleTexture(${s}, uv);
}
2022-05-24 13:28:51 +02:00
`}let{newShape:i,keptDims:o}=w.squeezeShape(n),u=i;if(u.length<n.length){let d=ou(e,u),h=["row","col"];return`
${au(d,t)}
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col) {
2022-05-24 13:28:51 +02:00
return ${r}(${uu(h,o)});
2022-04-21 15:39:40 +02:00
}
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col) {
int index = round(dot(vec2(row, col), vec2(${n[1]}, 1)));
2022-05-24 13:28:51 +02:00
${iu(e)}
2022-04-21 15:39:40 +02:00
}
2022-05-24 13:28:51 +02:00
`;let l=a[0],c=a[1],p=wi(s);return c===1?t?`
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${s}Shape[1], 1, 1));
vec2 uv = vec2(0.5, (index + 0.5) / float(${s}TexShape[0]));
return sampleTexture(${s}, uv);
}
`:`
float ${r}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${n[1]}, 1, 1));
vec2 uv = vec2(0.5, (index + 0.5) / ${l}.0);
return sampleTexture(${s}, uv);
}
`:l===1?t?`
float ${r}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${s}Shape[1], 1, 1));
vec2 uv = vec2((index + 0.5) / float(${s}TexShape[1]), 0.5);
return sampleTexture(${s}, uv);
}
`:`
float ${r}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${n[1]}, 1, 1));
vec2 uv = vec2((index + 0.5) / ${c}.0, 0.5);
return sampleTexture(${s}, uv);
}
`:t?`
float ${r}(int row, int col) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${s}Shape[1] + col + ${p};
vec2 uv = uvFromFlat(${s}TexShape[0], ${s}TexShape[1], index);
return sampleTexture(${s}, uv);
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
`:`
float ${r}(int row, int col) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${n[1]} + col + ${p};
vec2 uv = uvFromFlat(${l}, ${c}, index);
return sampleTexture(${s}, uv);
}
2022-05-30 03:12:18 +02:00
`}function UK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape,i=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)];if(n[0]===1){let d=n.slice(1),h=[1,2],f=ou(e,d),m=["b","row","col"];return`
${$1(f,t)}
2022-04-21 15:39:40 +02:00
vec4 ${r}(int b, int row, int col) {
2022-05-24 13:28:51 +02:00
return ${r}(${uu(m,h)});
2022-04-21 15:39:40 +02:00
}
`}let o=fn();if(t)return`
vec4 ${r}(int b, int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${s}TexShape[0]) / 2.0), ceil(float(${s}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${s}Shape[2]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${s}Shape[1]) / 2.0));
vec2 uv = packedUVfrom3D(
packedTexShape[0], packedTexShape[1], texelsInBatch, valuesPerRow, b, row, col);
return ${o.texture2D}(${s}, uv);
}
`;let u=i[0],l=i[1],c=Math.ceil(n[2]/2),p=c*Math.ceil(n[1]/2);return`
vec4 ${r}(int b, int row, int col) {
vec2 uv = packedUVfrom3D(
${u}, ${l}, ${p}, ${c}, b, row, col);
return ${o.texture2D}(${s}, uv);
}
2022-05-30 03:12:18 +02:00
`}function GK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=n[1]*n[2],i=n[2],{newShape:o,keptDims:u}=w.squeezeShape(n),l=o;if(l.length<n.length){let m=ou(e,l),g=["row","col","depth"];return`
2022-05-24 13:28:51 +02:00
${au(m,t)}
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col, int depth) {
2022-05-24 13:28:51 +02:00
return ${r}(${uu(g,u)});
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col, int depth) {
int index = round(dot(vec3(row, col, depth),
vec3(${a}, ${i}, 1)));
2022-05-24 13:28:51 +02:00
${iu(e)}
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
`;let c=e.shapeInfo.texShape,p=c[0],d=c[1],h=e.shapeInfo.flatOffset;if(d===a&&h==null)return t?`
float ${r}(int row, int col, int depth) {
int stride1 = ${s}Shape[2];
float texR = float(row);
float texC = dot(vec2(col, depth), vec2(stride1, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
float ${r}(int row, int col, int depth) {
float texR = float(row);
float texC = dot(vec2(col, depth), vec2(${i}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${d}.0, ${p}.0);
return sampleTexture(${s}, uv);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`;if(d===i&&h==null)return t?`
float ${r}(int row, int col, int depth) {
float texR = dot(vec2(row, col), vec2(${s}Shape[1], 1));
float texC = float(depth);
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
float ${r}(int row, int col, int depth) {
float texR = dot(vec2(row, col), vec2(${n[1]}, 1));
float texC = float(depth);
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${d}.0, ${p}.0);
return sampleTexture(${s}, uv);
}
2022-05-24 13:28:51 +02:00
`;let f=wi(s);return t?`
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int stride0 = ${s}Shape[1] * ${s}Shape[2];
int stride1 = ${s}Shape[2];
int index = row * ${a} + col * ${i} + depth + ${f};
vec2 uv = uvFromFlat(${s}TexShape[0], ${s}TexShape[1], index);
return sampleTexture(${s}, uv);
}
`:`
float ${r}(int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${a} + col * ${i} + depth + ${f};
vec2 uv = uvFromFlat(${p}, ${d}, index);
return sampleTexture(${s}, uv);
}
2022-05-30 03:12:18 +02:00
`}function HK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=fn();if(t)return`
2022-04-21 15:39:40 +02:00
vec4 ${s}(int b2, int b, int row, int col) {
int valuesPerRow = int(ceil(float(${n}Shape[3]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${n}Shape[2]) / 2.0));
int index = b * texelsInBatch + (row / 2) * valuesPerRow + (col / 2);
texelsInBatch *= ${n}Shape[1];
index = b2 * texelsInBatch + index;
ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
int texR = index / packedTexShape[1];
int texC = index - texR * packedTexShape[1];
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(packedTexShape[1], packedTexShape[0]); return ${r.texture2D}(${n}, uv);
}
`;let a=e.shapeInfo.logicalShape,i=a.length,o=e.shapeInfo.texShape,u=[Math.ceil(o[0]/2),Math.ceil(o[1]/2)],l=u[0],c=u[1],p=Math.ceil(a[i-1]/2),d=p*Math.ceil(a[i-2]/2),h="int b, int row, int col",f=`b * ${d} + (row / 2) * ${p} + (col / 2)`;for(let m=2;m<i-1;m++)h=`int b${m}, `+h,d*=a[i-m-1],f=`b${m} * ${d} + `+f;return`
vec4 ${s}(${h}) {
int index = ${f};
int texR = index / ${c};
int texC = index - texR * ${c};
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${c}, ${l});
return ${r.texture2D}(${n}, uv);
}
2022-05-30 03:12:18 +02:00
`}function qK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=n[3],i=n[2]*a,o=n[1]*i,{newShape:u,keptDims:l}=w.squeezeShape(n);if(u.length<n.length){let y=ou(e,u),v=["row","col","depth","depth2"];return`
2022-05-24 13:28:51 +02:00
${au(y,t)}
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col, int depth, int depth2) {
2022-05-24 13:28:51 +02:00
return ${r}(${uu(v,l)});
2022-04-21 15:39:40 +02:00
}
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col, int depth, int depth2) {
int index = round(dot(vec4(row, col, depth, depth2),
vec4(${o}, ${i}, ${a}, 1)));
2022-05-24 13:28:51 +02:00
${iu(e)}
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
`;let c=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1],f=`int stride2 = ${s}Shape[3];`,m=`int stride1 = ${s}Shape[2] * stride2;`,g=`int stride0 = ${s}Shape[1] * stride1;`;if(h===o&&c==null)return t?`
float ${r}(int row, int col, int depth, int depth2) {
${f}
${m}
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(stride1, stride2, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
float ${r}(int row, int col, int depth, int depth2) {
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(${i}, ${a}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${s}, uv);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`;if(h===a&&c==null)return t?`
float ${r}(int row, int col, int depth, int depth2) {
float texR = dot(vec3(row, col, depth),
vec3(${s}Shape[1] * ${s}Shape[2], ${s}Shape[2], 1));
float texC = float(depth2);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
float ${r}(int row, int col, int depth, int depth2) {
float texR = dot(vec3(row, col, depth),
vec3(${n[1]*n[2]}, ${n[2]}, 1));
float texC = float(depth2);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${s}, uv);
2022-04-18 18:26:05 +02:00
}
2022-05-24 13:28:51 +02:00
`;let b=wi(s);return t?`
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col, int depth, int depth2) {
// Explicitly use integer operations as dot() only works on floats.
${f}
${m}
${g}
int index = row * stride0 + col * stride1 +
depth * stride2 + depth2;
vec2 uv = uvFromFlat(${s}TexShape[0], ${s}TexShape[1], index + ${b});
return sampleTexture(${s}, uv);
}
`:`
float ${r}(int row, int col, int depth, int depth2) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${o} + col * ${i} +
depth * ${a} + depth2;
vec2 uv = uvFromFlat(${d}, ${h}, index + ${b});
return sampleTexture(${s}, uv);
}
2022-05-30 03:12:18 +02:00
`}function jK(e){let t=e.shapeInfo.logicalShape,n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=t[4],a=t[3]*r,i=t[2]*a,o=t[1]*i,{newShape:u,keptDims:l}=w.squeezeShape(t);if(u.length<t.length){let m=ou(e,u),g=["row","col","depth","depth2","depth3"];return`
2022-05-24 13:28:51 +02:00
${au(m)}
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth, int depth2, int depth3) {
2022-05-24 13:28:51 +02:00
return ${s}(${uu(g,l)});
2022-04-21 15:39:40 +02:00
}
`}if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
float index = dot(
vec4(row, col, depth, depth2),
vec4(${o}, ${i}, ${a}, ${r})) +
depth3;
2022-05-24 13:28:51 +02:00
${iu(e)}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`;let c=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1];if(h===o&&c==null)return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
vec4(${i}, ${a}, ${r}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${n}, uv);
}
`;if(h===r&&c==null)return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
float texR = dot(
vec4(row, col, depth, depth2),
vec4(${t[1]*t[2]*t[3]},
${t[2]*t[3]}, ${t[3]}, 1));
int texC = depth3;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${n}, uv);
}
2022-05-24 13:28:51 +02:00
`;let f=wi(n);return`
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth, int depth2, int depth3) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${o} + col * ${i} + depth * ${a} +
depth2 * ${r} + depth3 + ${f};
vec2 uv = uvFromFlat(${d}, ${h}, index);
return sampleTexture(${n}, uv);
}
2022-05-30 03:12:18 +02:00
`}function KK(e){let t=e.shapeInfo.logicalShape,n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),{newShape:r,keptDims:a}=w.squeezeShape(t);if(r.length<t.length){let g=ou(e,r),b=["row","col","depth","depth2","depth3","depth4"];return`
2022-05-24 13:28:51 +02:00
${au(g)}
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
2022-05-24 13:28:51 +02:00
return ${s}(${uu(b,a)});
2022-04-21 15:39:40 +02:00
}
`}let i=t[5],o=t[4]*i,u=t[3]*o,l=t[2]*u,c=t[1]*l;if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int index = round(dot(
vec4(row, col, depth, depth2),
vec4(${c}, ${l}, ${u}, ${o})) +
dot(
vec2(depth3, depth4),
vec2(${i}, 1)));
2022-05-24 13:28:51 +02:00
${iu(e)}
2022-04-21 15:39:40 +02:00
}
`;let p=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,h=d[0],f=d[1];if(f===c&&p==null)return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
vec4(${l}, ${u}, ${o}, ${i})) +
float(depth4);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
`;if(f===i&&p==null)return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
float texR = dot(vec4(row, col, depth, depth2),
vec4(${t[1]*t[2]*t[3]*t[4]},
${t[2]*t[3]*t[4]},
${t[3]*t[4]},
${t[4]})) + float(depth3);
int texC = depth4;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
2022-05-24 13:28:51 +02:00
`;let m=wi(n);return`
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${c} + col * ${l} + depth * ${u} +
depth2 * ${o} + depth3 * ${i} + depth4 + ${m};
vec2 uv = uvFromFlat(${h}, ${f}, index);
return sampleTexture(${n}, uv);
}
2022-05-24 13:28:51 +02:00
`}function iu(e){let t=e.name,n=w.sizeFromShape(e.shapeInfo.logicalShape);return n<2?`return ${t};`:`
2022-04-21 15:39:40 +02:00
for (int i = 0; i < ${n}; i++) {
if (i == index) {
return ${t}[i];
}
}
2022-05-30 03:12:18 +02:00
`}function XK(e,t){let n=e.name,s=n.charAt(0).toUpperCase()+n.slice(1),r="get"+s+"AtOutCoords",a=e.shapeInfo.logicalShape.length,i=t.logicalShape.length,o=T1(e.shapeInfo.logicalShape,t.logicalShape),u=ot(i),l=i-a,c,p=["x","y","z","w","u","v"];a===0?c="":i<2&&o.length>=1?c="coords = 0;":c=o.map(y=>`coords.${p[y+l]} = 0;`).join(`
2022-04-21 15:39:40 +02:00
`);let d="";i<2&&a>0?d="coords":d=e.shapeInfo.logicalShape.map((y,v)=>`coords.${p[v+l]}`).join(", ");let h="return outputValue;",m=w.sizeFromShape(e.shapeInfo.logicalShape)===1,b=w.sizeFromShape(t.logicalShape)===1;if(a===1&&!m&&!b)h=`
return vec4(outputValue.xy, outputValue.xy);
`;else if(m&&!b)i===1?h=`
return vec4(outputValue.x, outputValue.x, 0., 0.);
`:h=`
return vec4(outputValue.x);
`;else if(o.length){let y=a-2,v=a-1;o.indexOf(y)>-1&&o.indexOf(v)>-1?h="return vec4(outputValue.x);":o.indexOf(y)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":o.indexOf(v)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return`
vec4 ${r}() {
${u} coords = getOutputCoords();
${c}
vec4 outputValue = get${s}(${d});
${h}
}
2022-05-30 03:12:18 +02:00
`}function YK(e,t){let n=e.name,s=n.charAt(0).toUpperCase()+n.slice(1),r="get"+s+"AtOutCoords",a=t.texShape,i=e.shapeInfo.texShape,o=e.shapeInfo.logicalShape.length,u=t.logicalShape.length;if(!e.shapeInfo.isUniform&&o===u&&e.shapeInfo.flatOffset==null&&w.arraysEqual(i,a))return`
2022-04-21 15:39:40 +02:00
float ${r}() {
return sampleTexture(${n}, resultUV);
}
2022-05-30 03:12:18 +02:00
`;let l=ot(u),c=T1(e.shapeInfo.logicalShape,t.logicalShape),p=u-o,d,h=["x","y","z","w","u","v"];o===0?d="":u<2&&c.length>=1?d="coords = 0;":d=c.map(m=>`coords.${h[m+p]} = 0;`).join(`
2022-04-21 15:39:40 +02:00
`);let f="";return u<2&&o>0?f="coords":f=e.shapeInfo.logicalShape.map((m,g)=>`coords.${h[g+p]}`).join(", "),`
float ${r}() {
${l} coords = getOutputCoords();
${d}
return get${s}(${f});
}
2022-05-30 03:12:18 +02:00
`}function ot(e){if(e<=1)return"int";if(e===2)return"ivec2";if(e===3)return"ivec3";if(e===4)return"ivec4";if(e===5)return"ivec5";if(e===6)return"ivec6";throw Error(`GPU for rank ${e} is not yet supported`)}function Sv(e,t,n){let{newShape:s,keptDims:r}=w.squeezeShape(t),a=t.length,i=e&&a===3&&t[0]===1,o=i?t.slice(1):s,u=!e&&a>1&&!w.arraysEqual(t,n)&&s.length<a||i;return{useSqueezeShape:u,uniformShape:u?o:t,keptDims:r}}function ou(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function uu(e,t){return t.map(n=>e[n]).join(", ")}function QK(e,t,n,s){let r=n.map((c,p)=>{let d={logicalShape:c.shape,texShape:c.isUniform?null:c.texData.texShape,isUniform:c.isUniform,isPacked:c.isUniform?!1:c.texData.isPacked,flatOffset:null};return c.texData!=null&&c.texData.slice!=null&&c.texData.slice.flatOffset>0&&(d.flatOffset=c.texData.slice.flatOffset),{name:t.variableNames[p],shapeInfo:d}}),a=r.map(c=>c.shapeInfo),i={logicalShape:s.shape,texShape:s.texData.texShape,isUniform:!1,isPacked:s.texData.isPacked,flatOffset:null},o=mK(r,i,t),u=a1(e.gl,o),l=e.createProgram(u);return K().get("ENGINE_COMPILE_ONLY")?{program:t,fragmentShader:u,source:o,webGLProgram:l,inShapeInfos:a,outShapeInfo:i,uniformLocations:null,customUniformLocations:null,infLoc:null,nanLoc:null,inShapesLocations:null,inTexShapesLocations:null,outShapeLocation:null,outShapeStridesLocation:null,outTexShapeLocation:null}:{program:t,fragmentShader:u,source:o,webGLProgram:l,inShapeInfos:a,outShapeInfo:i,...A1(e,t,l)}}function A1(e,t,n){let s={},r={},a={},i=[],o,u,l,c=null,p=null;p=e.getUniformLocation(n,"NAN",!1),K().getNumber("WEBGL_VERSION")===1&&(c=e.getUniformLocation(n,"INFINITY",!1));let d=!1;for(let h=0;h<t.variableNames.length;h++){let f=t.variableNames[h];s[f]=e.getUniformLocation(n,f,d),s[`offset${f}`]=e.getUniformLocation(n,`offset${f}`,d),t.enableShapeUniforms&&(r[`${f}Shape`]=e.getUniformLocation(n,`${f}Shape`,d),a[`${f}TexShape`]=e.getUniformLocation(n,`${f}TexShape`,d))}return t.enableShapeUniforms&&(o=e.getUniformLocation(n,"outShape",d),l=e.getUniformLocation(n,"outShapeStrides",d),u=e.getUniformLocation(n,"outTexShape",d)),t.customUniforms&&t.customUniforms.forEach((h,f)=>{i[f]=e.getUniformLocation(n,h.name,d)}),{uniformLocations:s,customUniformLocations:i,infLoc:c,nanLoc:p,inShapesLocations:r,inTexShapesLocations:a,outShapeLocation:o,outShapeStridesLocation:l,outTexShapeLocation:u}}function dw(e,t){if(e.length!==t.length)throw Error(`Binary was compiled with ${e.length} inputs, but was executed with ${t.length} inputs`);e.forEach((n,s)=>{let r=n.logicalShape,a=t[s],i=a.shape;if(!w.arraysEqual(r,i))throw Error(`Binary was compiled with different shapes than the current args. Shapes ${r} and ${i} must match`);if(n.isUniform&&a.isUniform)return;let o=n.texShape,u=a.isUniform?null:a.texData.texShape;if(!w.arraysEqual(o,u))throw Error(`Binary was compiled with different texture shapes than the current args. Shape ${o} and ${u} must match`)})}function ZK(e,t,n,s,r){t.program.enableShapeUniforms||(dw(t.inShapeInfos,n),dw([t.outShapeInfo],[s]));let a=s.texData.texture,i=s.texData.texShape;s.texData.isPacked?e.setOutputPackedMatrixTexture(a.texture,i[0],i[1]):e.setOutputMatrixTexture(a.texture,i[0],i[1]),e.setProgram(t.webGLProgram),K().getNumber("WEBGL_VERSION")===1&&t.infLoc!==null&&e.gl.uniform1f(t.infLoc,1/0),t.nanLoc!==null&&e.gl.uniform1f(t.nanLoc,NaN),n.forEach((u,l)=>{let c=t.program.variableNames[l],p=t.uniformLocations[c],d=t.uniformLocations[`offset${c}`],h=t.inShapesLocations[`${c}Shape`],f=t.inTexShapesLocations[`${c}TexShape`];if(h){let{uniformShape:m}=Sv(t.program.packedInputs,u.shape,u.texData.texShape);switch(m.length){case 1:e.gl.uniform1iv(h,new Int32Array(m));break;case 2:e.gl.uniform2iv(h,new Int32Array(m));break;case 3:e.gl.uniform3iv(h,new Int32Array(m));break;case 4:e.gl.uniform4iv(h,new Int32Array(m));break;default:break}}if(f&&e.gl.uniform2i(f,u.texData.texShape[0],u.texData.texShape[1]),p!=null){if(u.isUniform){if(w.sizeFromShape(u.shape)<2)e.gl.uniform1f(p,u.uniformValues[0]);else{let m=u.uniformValues;m instanc
2022-04-21 15:39:40 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2022-05-30 03:12:18 +02:00
${this.enableShapeUniforms?th(["r","c","d"],e):xi(["r","c","d"],e)}
2022-04-21 15:39:40 +02:00
return ivec3(r, c, d);
}
void main() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);
vec4 result = vec4(0.);
for (int i=0; i<4; i++) {
int flatIndex = index + i;
ivec3 rc = outCoordsFromFlatIndex(flatIndex);
result[i] = getA(rc.x, rc.y, rc.z);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${t.output} = result;
}
2022-05-30 03:12:18 +02:00
`}},tX=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let t=fn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
2022-04-21 15:39:40 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2022-05-30 03:12:18 +02:00
${this.enableShapeUniforms?th(["r","c","d"],e):xi(["r","c","d"],e)}
2022-04-21 15:39:40 +02:00
return ivec3(r, c, d);
}
void main() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);
vec4 result = vec4(0.);
for (int i=0; i<4; i++) {
int flatIndex = index + i;
ivec3 rc = outCoordsFromFlatIndex(flatIndex);
result[i] = getChannel(getA(rc.x, rc.y, rc.z), vec2(rc.y, rc.z));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${t.output} = result;
}
2022-05-30 03:12:18 +02:00
`}},nX=class{constructor(e){this.variableNames=["A"],this.outTexUsage=3;let t=fn();this.outputShape=e,this.userCode=`
${N1}
2022-04-21 15:39:40 +02:00
void main() {
float x = getAAtOutCoords();
${t.output} = encode_float(x);
}
2022-05-30 03:12:18 +02:00
`}},sX=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=3;let t=fn();this.outputShape=e,this.userCode=`
${N1}
2022-04-21 15:39:40 +02:00
void main() {
ivec3 coords = getOutputCoords();
float x = getChannel(getAAtOutCoords(), vec2(coords.y, coords.z));
${t.output} = encode_float(x);
}
2022-05-30 03:12:18 +02:00
`}},rX=class{constructor(e,t=!1){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=fn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let s="result";t&&(s="floor(result * 255. + 0.5)"),this.userCode=`
${this.enableShapeUniforms?kv():wv(e)}
2022-04-21 15:39:40 +02:00
void main() {
ivec3 coords = getOutputCoords();
int flatIndex = getFlatIndex(coords);
int offset = imod(flatIndex, 4);
flatIndex = idiv(flatIndex, 4, 1.);
int r = flatIndex / texShape[1];
int c = imod(flatIndex, texShape[1]);
vec2 uv = (vec2(c, r) + halfCR) / vec2(texShape[1], texShape[0]);
vec4 values = ${n.texture2D}(A, uv);
float result;
if(offset == 0) {
result = values[0];
} else if(offset == 1) {
result = values[1];
} else if(offset == 2) {
result = values[2];
2022-02-10 18:27:21 +01:00
} else {
2022-04-21 15:39:40 +02:00
result = values[3];
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
${n.output} = vec4(${s}, 0., 0., 0.);
}
2022-05-30 03:12:18 +02:00
`}},aX=class{constructor(e,t=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=fn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let s="",r="result";t&&(r="floor(result * 255. + 0.5)");for(let a=0;a<=1;a++)for(let i=0;i<=1;i++){let o=a*2+i;s+=`
2022-04-21 15:39:40 +02:00
localCoords = coords;
if(localCoords[2] + ${i} < ${this.enableShapeUniforms?"outShape[2]":`${e[2]}`}) {
localCoords[2] += ${i};
if (localCoords[1] + ${a} < ${this.enableShapeUniforms?"outShape[1]":`${e[1]}`}) {
localCoords[1] += ${a};
flatIndex = getFlatIndex(localCoords);
offset = imod(flatIndex, 4);
flatIndex = idiv(flatIndex, 4, 1.);
int r = flatIndex / texShape[1];
int c = imod(flatIndex, texShape[1]);
vec2 uv = (vec2(c, r) + halfCR) / vec2(texShape[1], texShape[0]);
values = ${n.texture2D}(A, uv);
if (offset == 0) {
result[${o}] = values[0];
} else if (offset == 1) {
result[${o}] = values[1];
} else if (offset == 2) {
result[${o}] = values[2];
2022-02-10 18:27:21 +01:00
} else {
2022-04-21 15:39:40 +02:00
result[${o}] = values[3];
2022-02-10 18:27:21 +01:00
}
}
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`}this.userCode=`
2022-05-30 03:12:18 +02:00
${this.enableShapeUniforms?kv():wv(e)}
2022-04-21 15:39:40 +02:00
void main() {
ivec3 coords = getOutputCoords();
vec4 result = vec4(0.);
int flatIndex, r, c, offset;
ivec3 localCoords;
vec2 uv;
vec4 values;
${s}
${n.output} = ${r};
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},iX={};Ee(iX,{bindVertexProgramAttributeStreams:()=>L1,createBufferFromOutputTexture:()=>W1,createFloat16MatrixTexture:()=>O1,createFloat16PackedMatrixTexture:()=>M1,createFloat32MatrixTexture:()=>F1,createIndexBuffer:()=>D1,createPackedMatrixTexture:()=>z1,createUnsignedBytesMatrixTexture:()=>P1,createVertexBuffer:()=>R1,createVertexShader:()=>E1,downloadByteEncodedFloatMatrixFromOutputTexture:()=>G1,downloadFloat32MatrixFromBuffer:()=>U1,downloadMatrixFromPackedOutputTexture:()=>q1,downloadPackedMatrixFromBuffer:()=>H1,getInternalFormatForFloat16MatrixTexture:()=>Cv,getInternalFormatForFloat16PackedMatrixTexture:()=>$v,getInternalFormatForFloat32MatrixTexture:()=>Iv,getInternalFormatForPackedMatrixTexture:()=>Tv,getInternalFormatForUnsignedBytesMatrixTexture:()=>Nv,uploadDenseMatrixToTexture:()=>B1,uploadPixelDataToTexture:()=>V1});function E1(e){let t=fn(),n=`${t.version}
2022-04-21 15:39:40 +02:00
precision highp float;
${t.attribute} vec3 clipSpacePos;
${t.attribute} vec2 uv;
${t.varyingVs} vec2 resultUV;
void main() {
gl_Position = vec4(clipSpacePos, 1);
resultUV = uv;
2022-05-30 03:12:18 +02:00
}`;return r1(e,n)}function R1(e){let t=new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]);return u1(e,t)}function D1(e){let t=new Uint16Array([0,1,2,2,1,3]);return l1(e,t)}function ec(e,t,n,s,r,a){d1(t,n);let i=c1(e),o=e.TEXTURE_2D;return fe(e,()=>e.bindTexture(o,i)),fe(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),fe(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),fe(e,()=>e.texParameteri(o,e.TEXTURE_MIN_FILTER,e.NEAREST)),fe(e,()=>e.texParameteri(o,e.TEXTURE_MAG_FILTER,e.NEAREST)),K().getNumber("WEBGL_VERSION")===1?fe(e,()=>e.texImage2D(o,0,s,t,n,0,r,a,null)):fe(e,()=>e.texStorage2D(o,1,s,t,n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null)),{texture:i,texShape:[n,t]}}function Iv(e){return e.internalFormatFloat}function F1(e,t,n,s){let[r,a]=Jl(t,n);return ec(e,r,a,Iv(s),s.textureFormatFloat,e.FLOAT)}function Cv(e){return e.internalFormatHalfFloat}function O1(e,t,n,s){let[r,a]=Jl(t,n);return ec(e,r,a,Cv(s),s.textureFormatFloat,s.textureTypeHalfFloat)}function Nv(e){return e.downloadTextureFormat}function P1(e,t,n,s){let[r,a]=Jl(t,n);return ec(e,r,a,Nv(s),e.RGBA,e.UNSIGNED_BYTE)}function Tv(e){return e.internalFormatPackedFloat}function z1(e,t,n,s){let[r,a]=su(t,n);return ec(e,r,a,Tv(s),e.RGBA,e.FLOAT)}function $v(e){return e.internalFormatPackedHalfFloat}function M1(e,t,n,s){let[r,a]=su(t,n);return ec(e,r,a,$v(s),e.RGBA,s.textureTypeHalfFloat)}function L1(e,t,n){return fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),Xm(e,t,"clipSpacePos",n,3,20,0)&&Xm(e,t,"uv",n,2,20,12)}function B1(e,t,n,s,r,a){fe(e,()=>e.bindTexture(e.TEXTURE_2D,t));let i,o,u;r instanceof Uint8Array?(i=new Uint8Array(n*s*4),o=e.UNSIGNED_BYTE,u=e.RGBA):(i=new Float32Array(n*s*4),o=e.FLOAT,u=a.internalFormatPackedFloat),i.set(r),K().getNumber("WEBGL_VERSION")===2?fe(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,s,e.RGBA,o,i)):fe(e,()=>e.texImage2D(e.TEXTURE_2D,0,u,n,s,0,e.RGBA,o,i)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function V1(e,t,n){fe(e,()=>e.bindTexture(e.TEXTURE_2D,t)),n.data instanceof Uint8Array?K().getNumber("WEBGL_VERSION")===2?fe(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n.width,n.height,e.RGBA,e.UNSIGNED_BYTE,n.data)):fe(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,n.width,n.height,0,e.RGBA,e.UNSIGNED_BYTE,n.data)):K().getNumber("WEBGL_VERSION")===2?fe(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,e.RGBA,e.UNSIGNED_BYTE,n)):fe(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function W1(e,t,n,s){let r=e.createBuffer();fe(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,r));let o=4*4*t*n;return fe(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,o,e.STREAM_READ)),fe(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,0)),fe(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,null)),r}function U1(e,t,n){let s=e,r=new Float32Array(n);return s.bindBuffer(s.PIXEL_PACK_BUFFER,t),s.getBufferSubData(s.PIXEL_PACK_BUFFER,0,r),s.bindBuffer(s.PIXEL_PACK_BUFFER,null),r}function G1(e,t,n,s){let[r,a]=Jl(t,n),i=4,o=new Uint8Array(tK(t*n,i));return fe(e,()=>e.readPixels(0,0,r,a,s.downloadTextureFormat,e.UNSIGNED_BYTE,o)),new Float32Array(o.buffer)}function H1(e,t,n,s,r,a,i,o){let u=e,l=new Float32Array(nK(a,i));return u.bindBuffer(u.PIXEL_PACK_BUFFER,t),u.getBufferSubData(u.PIXEL_PACK_BUFFER,0,l),u.bindBuffer(u.PIXEL_PACK_BUFFER,null),l}function q1(e,t,n){let s=new Float32Array(t*n*4);return fe(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,s)),s}var nm=class{constructor(e){this.outputTexture=null,this.program=null,this.disposed=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[];let t=K().getNumber("WEBGL_VERSION");e!=null?(this.gl=e,Z5(t,e)):this.gl=xs(t);let n="WEBGL_color_buffer_float",s="EXT_color_buffer_half_float";if(this.parallelCompilationExtension=this.gl.getExtension("KHR_parallel_shader_compile"),K().getNumber("WEBGL_VERSION")===1){let r="OES_texture_float",a="OES_texture_half_float";if(this.textureFloatExtension=zu(this.gl,r),Ln(this.gl,a))this.textureHalfFloatExtension=zu(this.gl,a);else if(K().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support half float textures, yet the environm
2022-04-21 15:39:40 +02:00
void main() {
setOutput(vec4(getA(), 0., 0., 0.));
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`;else{let t=un("rc",this.rank),n=ot(this.rank),s=this.getOutOfBoundsCondition(t),r=this.getSetup(t),a=this.getOutput(t);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${n} rc = getOutputCoords();
if(${s}) {
setOutput(vec4(0));
2022-02-10 18:27:21 +01:00
} else {
2022-04-21 15:39:40 +02:00
${r}
setOutput(vec4(${a}));
2022-04-21 15:38:36 +02:00
}
}
2022-04-21 15:39:40 +02:00
`}}getSourceCoordsArr(e){let t=[];for(let n=0;n<=1;n++)for(let s=0;s<=1;s++){let r=`${n===0?"r":"rp1"}, ${s===0?"c":"cp1"}`;for(let a=2;a<this.rank;a++)r=`${e[e.length-1-a]},`+r;t.push(r)}return t}getOutOfBoundsCondition(e){if(this.rank===1)return`rc > ${this.enableShapeUniforms?"outShape":this.outputShape[0]}`;let t="";for(let n=this.rank-2;n<this.rank;n++)t+=`${e[n]} >= ${this.enableShapeUniforms?`outShape[${n}]`:this.outputShape[n]}`,n<this.rank-1&&(t+="||");return t}getSetup(e){if(this.rank===1)return"";let t=e.slice(-2),n=this.enableShapeUniforms?`outShape[${this.rank} - 1]`:this.outputShape[this.rank-1],s=this.enableShapeUniforms?`outShape[${this.rank} - 2]`:this.outputShape[this.rank-2];return`
int r = ${t[0]};
int c = ${t[1]};
int rp1 = r + 1;
int cp1 = c + 1;
bool cEdge = cp1 >= ${n};
bool rEdge = rp1 >= ${s};
`}getOutput(e){let t=this.getSourceCoordsArr(e);return this.rank===1?`getA(rc), (rc + 1 >= ${this.enableShapeUniforms?"outShape":this.outputShape[0]} ? 0. : getA(rc + 1)), 0, 0`:`getA(${t[0]}),
cEdge ? 0. : getA(${t[1]}),
rEdge ? 0. : getA(${t[2]}),
2022-05-30 03:12:18 +02:00
rEdge || cEdge ? 0. : getA(${t[3]})`}},Q1=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec3"}],this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let n="";for(let s=0;s<4;s++){let r="thisRC = rc;";s%2===1&&(r+="thisRC.z += 1;"),s>1&&(r+="thisRC.y += 1;"),n+=`
2022-04-21 15:39:40 +02:00
${r}
${s>0?"if(thisRC.y < rows && thisRC.z < cols){":""}
int flatIndex = getFlatIndex(thisRC);
ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);
vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));
result[${s}] =
getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);
${s>0?"}":""}
`}this.userCode=`
2022-05-30 03:12:18 +02:00
${XX(t,this.enableShapeUniforms)}
${this.enableShapeUniforms?kv():wv(e)}
2022-04-21 15:39:40 +02:00
void main() {
ivec3 rc = getOutputCoords();
vec4 result = vec4(0.);
ivec3 thisRC;
int rows = ${this.enableShapeUniforms?"outShape[1]":e[1]};
int cols = ${this.enableShapeUniforms?"outShape[2]":e[2]};
${n}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function XX(e,t){return`
2022-04-21 15:39:40 +02:00
ivec3 inputCoordsFromReshapedOutCoords(int index) {
2022-05-30 03:12:18 +02:00
${t?fK(["r","c","d"],"inputShape"):xi(["r","c","d"],e)}
2022-04-21 15:39:40 +02:00
return ivec3(r, c, d);
}
2022-05-30 03:12:18 +02:00
`}var YX=class{constructor(e){this.gpgpu=e,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}acquireTexture(e,t,n){let s=hw(t,n),r=fw(e,s,n);r in this.freeTextures||(this.freeTextures[r]=[]),r in this.usedTextures||(this.usedTextures[r]=[]);let a=pw(e,s,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[r].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=a,this.log();let o=this.freeTextures[r].shift();return this.usedTextures[r].push(o),o}let i;return s===3?i=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):s===4?i=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):s===1?i=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):s===0?i=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):s===2&&(i=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[r].push(i),this.numUsedTextures++,this._numBytesAllocated+=a,this.log(),i}releaseTexture(e,t,n,s){if(this.freeTextures==null)return;let r=hw(n,s),a=fw(t,r,s);a in this.freeTextures||(this.freeTextures[a]=[]);let i=pw(t,r,this.gpgpu.gl,this.gpgpu.textureConfig,s),o=K().get("WEBGL_DELETE_TEXTURE_THRESHOLD");o!==-1&&this._numBytesAllocated>o?(this.gpgpu.deleteMatrixTexture(e.texture),this._numBytesAllocated-=i):(this.freeTextures[a].push(e),this.numFreeTextures++,this._numBytesFree+=i),this.numUsedTextures--;let u=this.usedTextures[a],l=u.indexOf(e);if(l<0)throw new Error("Cannot release a texture that was never provided by this texture manager");u.splice(l,1),this.log()}log(){if(!this.logEnabled)return;let e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",`${this.numFreeTextures} / ${this.numUsedTextures}`,`(${e})`);let t=this._numBytesFree/this._numBytesAllocated;console.log(`Bytes allocated: ${this._numBytesAllocated}`),console.log(`Bytes unused: ${this._numBytesFree} (${Math.round(100*t)}%)`)}get numBytesAllocated(){return this._numBytesAllocated}get numBytesFree(){return this._numBytesFree}getNumUsedTextures(){return this.numUsedTextures}getNumFreeTextures(){return this.numFreeTextures}dispose(){if(this.freeTextures!=null){for(let e in this.freeTextures)this.freeTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t.texture)});for(let e in this.usedTextures)this.usedTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t.texture)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0}}};function QX(e,t){let n=e;if(t===n.R32F)return 4;if(t===n.R16F)return 2;if(t===n.RGBA32F)return 16;if(t===e.RGBA)return 16;if(t===n.RGBA16F)return 8;if(t===n.RGBA8)return 4;throw new Error(`Unknown internal format ${t}`)}function pw(e,t,n,s,r){let a=ZX(t,s),i;if(r){let[u,l]=su(e[0],e[1]);i=u*l}else{let[u,l]=Jl(e[0],e[1]);i=u*l}let o=QX(n,a);return i*o}function ZX(e,t){switch(e){case 3:return Tv(t);case 4:return $v(t);case 1:return Iv(t);case 0:return Cv(t);case 2:return Nv(t);default:throw new Error(`Unknown physical texture type ${e}`)}}function JX(e){return K().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?3:1:e?4:0}function hw(e,t){if(e===1)return 3;if(e===0||e==null)return JX(t);if(e===3||e===2)return 2;throw new Error(`Unknown logical texture type ${e}`)}function fw(e,t,n){return`${e[0]}_${e[1]}_${t}_${n}`}var Gs=class{constructor(e,t){this.variableNames=["A"],this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
2022-04-21 15:39:40 +02:00
float unaryOperation(float x) {
${t}
}
void main() {
float x = getAAtOutCoords();
float y = unaryOperation(x);
setOutput(y);
}
2022-05-30 03:12:18 +02:00
`}},ss="if (isnan(x)) return x;",e8="return x;",mw="return abs(x);",t8="return (x >= 0.0) ? x : (exp(x) - 1.0);",n8=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : x;
2022-05-30 03:12:18 +02:00
`,s8=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2022-05-30 03:12:18 +02:00
`,Li="return x;",r8="return 1.0 / (1.0 + exp(-1.0 * x));",a8="return x;",i8=`
2022-04-21 15:39:40 +02:00
vec4 result;
result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);
return result;
2022-05-30 03:12:18 +02:00
`,o8=`
2022-04-21 15:39:40 +02:00
vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`,u8=`
2022-04-21 15:39:40 +02:00
vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`,l8="return 1.0 / (1.0 + exp(-1.0 * x));",ea=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
2022-04-21 15:39:40 +02:00
vec4 unaryOperation(vec4 x) {
${t}
}
void main() {
vec4 x = getAAtOutCoords();
vec4 y = unaryOperation(x);
setOutput(y);
}
2022-05-30 03:12:18 +02:00
`}},c8=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let t=e.length,n=un("rc",t),s=ot(t),r=jX(t,n),a=n.slice(-2),i=t<=1?"rc":`vec2(${a.join(",")})`;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${s} rc = getOutputCoords();
vec4 packedInput = getA(${r});
setOutput(getChannel(packedInput, ${i}));
}
2022-05-30 03:12:18 +02:00
`}},d8=ws.whereImpl,p8=1e-7,h8=1e-4,nd={};function f8(e){return e in nd||(nd[e]={}),nd[e]}var m8=K().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),g8=600;function b8(){return K().global.screen==null?1024:K().global.screen.height*K().global.screen.width*window.devicePixelRatio*g8/1024/1024}var Z1=class extends ol{constructor(e){if(super(),this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.lastGlFlushTime=0,this.warnedAboutMemory=!1,this.pendingDeletes=0,this.disposed=!1,!K().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");let t;if(e!=null){if(e instanceof nm)t=e;else{let n=xs(K().getNumber("WEBGL_VERSION"),e);t=new nm(n)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{let n=xs(K().getNumber("WEBGL_VERSION"));t=new nm(n),this.binaryCache=f8(K().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=t,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new YX(this.gpgpu),this.numMBBeforeWarning=b8(),this.texData=new Qd(this,ds())}nextDataId(){return Z1.nextDataId++}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}write(e,t,n){if((K().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||K().getBool("DEBUG"))&&this.checkNumericalProblems(e),n==="complex64"&&e!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");let s={id:this.nextDataId()};return this.texData.set(s,{shape:t,dtype:n,values:e,usage:1,refCount:1}),s}refCount(e){return this.texData.has(e)?this.texData.get(e).refCount:0}incRef(e){let t=this.texData.get(e);t.refCount++}decRef(e){if(this.texData.has(e)){let t=this.texData.get(e);t.refCount--}}move(e,t,n,s,r){if(K().getBool("DEBUG")&&this.checkNumericalProblems(t),s==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(e,{shape:n,dtype:s,values:t,usage:1,refCount:r})}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}readSync(e){let t=this.texData.get(e),{values:n,dtype:s,complexTensorInfos:r,slice:a,shape:i,isPacked:o}=t;if(a!=null){let p;o?p=new ea(i,Li):p=new Gs(i,Li);let d=this.runWebGLProgram(p,[{dataId:e,shape:i,dtype:s}],s),h=this.readSync(d.dataId);return this.disposeIntermediateTensorInfo(d),h}if(n!=null)return this.convertAndCacheOnCPU(e);if(s==="string")return n;let u=this.activeTimers!=null,l;u&&(l=w.now());let c;if(s==="complex64"){let p=this.readSync(r.real.dataId),d=this.readSync(r.imag.dataId);c=C.mergeRealAndImagArrays(p,d)}else c=this.getValuesFromTexture(e);return u&&(this.downloadWaitMs+=w.now()-l),this.convertAndCacheOnCPU(e,c)}async read(e){if(this.pendingRead.has(e)){let h=this.pendingRead.get(e);return new Promise(f=>h.push(f))}let t=this.texData.get(e),{values:n,shape:s,slice:r,dtype:a,complexTensorInfos:i,isPacked:o}=t;if(r!=null){let h;o?h=new ea(s,Li):h=new Gs(s,Li);let f=this.runWebGLProgram(h,[{dataId:e,shape:s,dtype:a}],a),m=this.read(f.dataId);return this.disposeIntermediateTensorInfo(f),m}if(n!=null)return this.convertAndCacheOnCPU(e);if(K().getBool("DEBUG")&&!K().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&K().getNumber("WEBGL_VERSION")===2)throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");let u=null,l;if(a!=="complex64"&&K().get("WEBGL_BUFFER_SUPPORTED")){l=this.decode(e);let h=this.texData.get(l.dataId);u=this.gpgpu.createBufferFromTexture(h.texture.texture,...ed(s))}this.pendingRead.set(e,[]),a!=="complex64"&&await this.gpgpu.createAndWaitForFence();let c;if(a==="complex64"){let h=await Promise.all([this.read(i.real.dataId),this.read(i.imag.dataId)]),f=h[0],m=h[1];c=C.mergeRealAndImagArrays(f,m)}else if(u==null)c=this.getValuesFromTexture(e);else{let h=w.sizeFromShape(s);c=this.gpgpu.downloadFloat32MatrixFromBuffer(u,h)}if(l!=null&&this.disposeIntermediateTensorInfo(l),u!=null){let h=this.gpgpu.gl;fe(h,()=>h.deleteBuffer(u))}let p=this.convertAndCacheOnCPU(e,c),d=this.pendingRead.get(e);return this.pendingRead.delete(e),d.forEach(h=>h(p)),this.pend
2022-04-21 15:39:40 +02:00
if (isnan(a)) return a;
if (isnan(b)) return b;
2022-05-24 13:28:51 +02:00
`,oo=class{constructor(e,t,n){this.variableNames=["A","B"],this.outputShape=C.assertAndGetBroadcastShape(t,n),this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
2022-04-21 15:39:40 +02:00
float binaryOperation(float a, float b) {
${e}
}
void main() {
float a = getAAtOutCoords();
float b = getBAtOutCoords();
setOutput(binaryOperation(a, b));
}
2022-05-30 03:12:18 +02:00
`}},nh=`
2022-04-21 15:39:40 +02:00
result.r = isNaN.r > 0. ? NAN : result.r;
result.g = isNaN.g > 0. ? NAN : result.g;
result.b = isNaN.b > 0. ? NAN : result.b;
result.a = isNaN.a > 0. ? NAN : result.a;
2022-05-22 14:50:51 +02:00
`,tc=class{constructor(e,t,n,s=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=C.assertAndGetBroadcastShape(t,n);let r=this.outputShape.length;this.enableShapeUniforms=Sn(r);let a="";if(s)if(r===0||w.sizeFromShape(this.outputShape)===1)a=`
2022-04-21 15:39:40 +02:00
result.y = 0.;
result.z = 0.;
result.w = 0.;
`;else if(a=`
${ot(r)} coords = getOutputCoords();
`,r===1)this.enableShapeUniforms?a+=`
result.y = (coords + 1) >= outShape ? 0. : result.y;
result.z = 0.;
result.w = 0.;
`:a+=`
result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
result.z = 0.;
result.w = 0.;
2022-05-30 03:12:18 +02:00
`;else{let o=un("coords",r);this.enableShapeUniforms?a+=`
2022-04-21 15:39:40 +02:00
bool nextRowOutOfBounds =
(${o[r-2]} + 1) >= outShape[${r} - 2];
bool nextColOutOfBounds =
(${o[r-1]} + 1) >= outShape[${r} - 1];
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`:a+=`
bool nextRowOutOfBounds =
(${o[r-2]} + 1) >= ${this.outputShape[r-2]};
bool nextColOutOfBounds =
(${o[r-1]} + 1) >= ${this.outputShape[r-1]};
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`}this.userCode=`
vec4 binaryOperation(vec4 a, vec4 b) {
${e}
}
void main() {
vec4 a = getAAtOutCoords();
vec4 b = getBAtOutCoords();
vec4 result = binaryOperation(a, b);
${a}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function Rn(e){let{inputs:t,backend:n}=e,{x:s}=t;return n.incRef(s.dataId),{dataId:s.dataId,shape:s.shape,dtype:s.dtype}}var x8={kernelName:Wa,backendName:"webgl",kernelFunc:Rn};function Dr(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.makeTensorInfo(s.shape,"complex64"),i=n.texData.get(a.dataId),o=Rn({inputs:{x:s},backend:n}),u=Rn({inputs:{x:r},backend:n});return i.complexTensorInfos={real:o,imag:u},a}var w8={kernelName:tp,backendName:"webgl",kernelFunc:Dr},t2="return (a < 0.) ? b * a : a;",n2=`
2022-04-21 15:39:40 +02:00
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
2022-05-30 03:12:18 +02:00
`;function k8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{alpha:a}=s,i=n.makeTensorInfo([],"float32",w.createScalarValue(a,"float32")),o=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new tc(n2,r.shape,i.shape):new oo(t2,r.shape,i.shape),u=n.runWebGLProgram(o,[r,i],"float32");return n.disposeIntermediateTensorInfo(i),u}var S8={kernelName:Ua,backendName:"webgl",kernelFunc:k8},s2="return (a < 0.) ? b * a : a;",r2=`
2022-04-21 15:39:40 +02:00
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
2022-05-30 03:12:18 +02:00
`;function I8(e){let{inputs:t,backend:n}=e,{x:s,alpha:r}=t,a=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new tc(r2,s.shape,r.shape):new oo(s2,s.shape,r.shape);return n.runWebGLProgram(a,[s,r],"float32")}var C8={kernelName:ti,backendName:"webgl",kernelFunc:I8},lu="if (isnan(x)) return x;",N8=`
2022-04-21 15:39:40 +02:00
if (isnan(a)) return a;
if (isnan(b)) return b;
2022-05-30 03:12:18 +02:00
`,T8=`
2022-04-21 15:39:40 +02:00
result.r = isNaN.r > 0. ? NAN : result.r;
result.g = isNaN.g > 0. ? NAN : result.g;
result.b = isNaN.b > 0. ? NAN : result.b;
result.a = isNaN.a > 0. ? NAN : result.a;
2022-05-30 03:12:18 +02:00
`;function Ke({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:s}){return({inputs:r,backend:a})=>{let{x:i}=r,o=a,u=s||i.dtype;if(o.shouldExecuteOnCPU([i])&&n!=null){let p=o.texData.get(i.dataId),d=n(p.values,u);return o.makeTensorInfo(i.shape,u,d)}let l=K().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,c;return l?c=new ea(i.shape,t):c=new Gs(i.shape,e),o.runWebGLProgram(c,[i],u)}}function qt({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:s=!1,cpuKernelImpl:r,dtype:a}){return({inputs:i,backend:o})=>{let{a:u,b:l}=i,c=o;if(s&&u.dtype==="complex64"){let f=c.texData.get(u.dataId),m=c.texData.get(l.dataId),[g,b]=[[f.complexTensorInfos.real,m.complexTensorInfos.real],[f.complexTensorInfos.imag,m.complexTensorInfos.imag]].map(v=>{let[x,k]=v,I={dataId:x.dataId,dtype:x.dtype,shape:u.shape},$={dataId:k.dataId,dtype:k.dtype,shape:l.shape},R=new oo(e,u.shape,l.shape);return c.runWebGLProgram(R,[I,$],ln(x.dtype,k.dtype))}),y=Dr({inputs:{real:g,imag:b},backend:c});return c.disposeIntermediateTensorInfo(g),c.disposeIntermediateTensorInfo(b),y}let p=a||ln(u.dtype,l.dtype);if((u.dtype==="string"||l.dtype==="string"||c.shouldExecuteOnCPU([u,l]))&&r!=null){let f=c.texData.get(u.dataId).values,m=c.texData.get(l.dataId).values,g=u.dtype==="string"?C.fromUint8ToStringArray(f):f,b=u.dtype==="string"?C.fromUint8ToStringArray(m):m,[y,v]=r(u.shape,l.shape,g,b,p),x=c.makeTensorInfo(v,p),k=c.texData.get(x.dataId);return k.values=y,x}let d=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,h;return d?h=new tc(t,u.shape,l.shape,n):h=new oo(e,u.shape,l.shape),c.runWebGLProgram(h,[u,l],p)}}function sh(e,t=!1){if(e==="linear")return t?a8:e8;if(e==="relu")return t?o8:n8;if(e==="elu")return t?i8:t8;if(e==="relu6")return t?u8:s8;if(e==="prelu")return t?r2:s2;if(e==="leakyrelu")return t?n2:t2;if(e==="sigmoid")return t?l8:r8;throw new Error(`Activation ${e} has not been implemented for the WebGL backend.`)}var a2=class{constructor(e,t,n,s=!1,r=!1,a=!1,i=null,o=!1,u=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n,this.enableShapeUniforms=Sn(this.outputShape.length);let l=s?e[1]:e[2],c=Math.ceil(l/2),p=s?"i * 2, rc.y":"rc.y, i * 2",d=r?"rc.z, i * 2":"i * 2, rc.z",h=s?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],f=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],m="",g="";i&&(o?m=`vec4 activation(vec4 a) {
2022-04-21 15:39:40 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
${i}
}`:u?m=`vec4 activation(vec4 a) {
vec4 b = getLeakyreluAlphaAtOutCoords();
${i}
}`:m=`vec4 activation(vec4 x) {
${i}
}`,g="result = activation(result);");let b=a?"result += getBiasAtOutCoords();":"";a&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),u&&this.variableNames.push("leakyreluAlpha");let y="rc.x",v="rc.x";e[0]<t[0]?y=`int(min(float(rc.x), ${e[0]-1}.))`:t[0]<e[0]&&(v=`int(min(float(rc.x), ${t[0]-1}.))`),this.userCode=`
${m}
// Don't use uniform for sharedDimensionPacked for performance.
const float sharedDimension = ${c}.0;
vec4 dot2x2ARowBCol(ivec3 rc) {
vec4 result = vec4(0);
for (int i = 0; i < ${c}; i++) {
int batchA = ${y};
int batchB = ${v};
vec4 a = getMatrixA(batchA, ${p});
vec4 b = getMatrixB(batchB, ${d});
// These swizzled products need to be separately added.
// See: https://github.com/tensorflow/tfjs/issues/1735
result += (${h[0]} * ${f[0]});
result += (${h[1]} * ${f[1]});
2022-04-01 15:12:04 +02:00
}
2022-04-21 15:39:40 +02:00
return result;
}
void main() {
ivec3 rc = getOutputCoords();
vec4 result = dot2x2ARowBCol(rc);
${b}
${g}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}},gw={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},bw=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=C.assertAndGetBroadcastShape(t,n),this.userCode=`
2022-04-21 15:39:40 +02:00
float binaryOpComplex(
float areal, float aimag, float breal, float bimag) {
${e}
}
void main() {
float areal = getARealAtOutCoords();
float aimag = getAImagAtOutCoords();
float breal = getBRealAtOutCoords();
float bimag = getBImagAtOutCoords();
setOutput(binaryOpComplex(areal, aimag, breal, bimag));
}
2022-05-30 03:12:18 +02:00
`}},yw="return a * b;";function Av(e){let{inputs:t,backend:n}=e,{a:s,b:r}=t,a=C.upcastType(s.dtype,r.dtype);if(s.dtype==="complex64"){let o=n.texData.get(s.dataId),u=n.texData.get(r.dataId),l=new bw(gw.REAL,s.shape,r.shape),c=new bw(gw.IMAG,s.shape,r.shape),p=[{dataId:o.complexTensorInfos.real.dataId,dtype:o.complexTensorInfos.real.dtype,shape:s.shape},{dataId:o.complexTensorInfos.imag.dataId,dtype:o.complexTensorInfos.imag.dtype,shape:s.shape},{dataId:u.complexTensorInfos.real.dataId,dtype:u.complexTensorInfos.real.dtype,shape:r.shape},{dataId:u.complexTensorInfos.imag.dataId,dtype:u.complexTensorInfos.imag.dtype,shape:r.shape}],d=n.runWebGLProgram(l,p,"float32"),h=n.runWebGLProgram(c,p,"float32"),f=Dr({inputs:{real:d,imag:h},backend:n});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}if(n.shouldExecuteOnCPU([s,r])){let o=n.texData.get(s.dataId),u=n.texData.get(r.dataId),[l,c]=TX(s.shape,r.shape,o.values,u.values,a),p=n.makeTensorInfo(c,a),d=n.texData.get(p.dataId);return d.values=l,p}let i;return K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?i=new tc(yw,s.shape,r.shape):i=new oo(yw,s.shape,r.shape),n.runWebGLProgram(i,[s,r],a)}var $8={kernelName:Za,backendName:"webgl",kernelFunc:Av};function _8(e,t,n){let s=[ya(e.shape),...va(e.shape)],r={dtype:e.dtype,shape:s,dataId:e.dataId},a=[ya(t),...va(t)],i=new Q1(a,s),o=!0,u=[s],l=n.runWebGLProgram(i,[r],e.dtype,u,o);return{dataId:l.dataId,shape:t,dtype:l.dtype}}function pe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{shape:a}=s,i=n,o=w.sizeFromShape(r.shape),u=w.inferFromImplicitShape(a,o),l=w.sizeFromShape(u);w.assert(o===l,()=>`The new shape (${u}) has ${l} elements and the old shape (${r.shape}) has ${o} elements. The new shape and old shape must have the same number of elements.`);let c=i.texData.get(r.dataId);return c.isPacked&&!rl(r.shape,u)&&!(c.texture!==null&&rl(c.shape,u))?_8(r,u,i):(i.incRef(r.dataId),{dataId:r.dataId,shape:u,dtype:r.dtype})}var A8={kernelName:Do,backendName:"webgl",kernelFunc:pe},vw=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:s,inSize:r,outSize:a}=e;this.outputShape=[s,a];let i=Math.floor(n/4)*4,o=n%4,u="sumValue += dot(values, ones);";if(t!=null){let c=1/t;u=`sumValue += dot(values * ${w.isInt(c)?c.toPrecision(2):c}, ones);`}let l="";r%n>0&&(l=`
2022-04-21 15:39:40 +02:00
if (inIdx < 0 || inIdx >= ${r}) {
return 0.0;
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`),this.userCode=`
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
${l}
return getX(batch, inIdx);
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${n};
float sumValue = 0.0;
for (int i = 0; i < ${i}; i += 4) {
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
${u}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
int inIdx = inOffset + ${i};
if (${o===1}) {
vec4 values = vec4(getValue(batch, inIdx), 0.0, 0.0, 0.0);
${u}
} else if (${o===2}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1), 0.0, 0.0);
${u}
} else if (${o===3}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2), 0.0);
${u}
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(sumValue);
}
2022-05-30 03:12:18 +02:00
`}},E8=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:s,inSize:r,outSize:a}=e;this.outputShape=[s,a];let i="0.0",o="";t==="prod"?i="1.0":t==="min"?(i="1.0 / 1e-20",o="min"):t==="max"&&(i="-1.0 / 1e-20",o="max");let u=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="sum"?u="sumValue":t==="prod"?u="prodValue":t==="all"?u="allValue":t==="any"&&(u="anyValue");let l=Math.floor(n/4)*4,c=n%4,p=`
2022-04-21 15:39:40 +02:00
if (${t==="sum"}) {
sumValue += dot(values, ones);
} else if (${t==="prod"}) {
vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);
prodValue *= tmp[0] * tmp[1];
} else {
minMaxValue = ${o}(values, minMaxValue);
if (${t==="min"} || ${t==="max"}) {
minMaxValue = ${o}(values, minMaxValue);
bvec4 isNaN = isnan(values);
if (isNaN.r || isNaN.g || isNaN.b || isNaN.a) {
minMaxValue = vec4(NAN);
2022-04-21 15:38:36 +02:00
}
}
2022-04-21 15:39:40 +02:00
}
`,d="vec4";t==="all"?(i="1.0",p=`
bool reducedAllValue = all(values);
float floatedReducedAllValue = float(reducedAllValue);
allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
`,d="bvec4"):t==="any"&&(i="0.0",p=`
bool reducedAnyValue = any(values);
float floatedReducedAnyValue = float(reducedAnyValue);
anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);
`,d="bvec4");let h="";r%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${r}) {
return initializationValue;
2022-04-01 15:12:04 +02:00
}
2022-04-21 15:39:40 +02:00
`),this.userCode=`
const float initializationValue = ${i};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
${h}
return getX(batch, inIdx);
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${n};
vec4 minMaxValue = vec4(${i});
float prodValue = 1.0;
float sumValue = 0.0;
float allValue = 1.0;
float anyValue = 0.0;
for (int i = 0; i < ${l}; i += 4) {
int inIdx = inOffset + i;
${d} values = ${d}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
${p}
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
int inIdx = inOffset + ${l};
if (${c===1}) {
${d} values = ${d}(
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
${p}
} else if (${c===2}) {
${d} values = ${d}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
${p}
} else if (${c===3}) {
${d} values = ${d}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
${p}
}
setOutput(${u});
}
2022-05-30 03:12:18 +02:00
`}};function R8(e){let t=[];for(;t.length===0||t[t.length-1].outSize!==1;){let n=t.length?t[t.length-1].outSize:e[1],s=C.computeOptimalWindowSize(n);t.push({inSize:n,windowSize:s,outSize:Math.ceil(n/s)})}return t}function ki(e,t,n,s){let r=R8(e.shape),a=e;for(let i=0;i<r.length;i++){let{inSize:o,windowSize:u,outSize:l}=r[i],c,p;n==="mean"?c=i===0?new vw({windowSize:u,inSize:o,batchSize:e.shape[0],outSize:l},o):new vw({windowSize:u,inSize:o,batchSize:e.shape[0],outSize:l}):c=new E8({windowSize:u,inSize:o,batchSize:e.shape[0],outSize:l},n),p=a,a=s.runWebGLProgram(c,[a],t),p.dataId!==e.dataId&&s.disposeIntermediateTensorInfo(p)}return a}var D8=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let a=0;a<n.length;a++)n[a]=e[t[a]];this.outputShape=n,this.rank=n.length;let s=ot(this.rank),r=F8(t);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${s} resRC = getOutputCoords();
setOutput(getA(${r}));
}
2022-05-30 03:12:18 +02:00
`}};function F8(e){let t=e.length;if(t>6)throw Error(`Transpose for rank ${t} is not yet supported`);let n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],s=new Array(t);for(let r=0;r<e.length;r++)s[e[r]]=n[r];return s.join()}var O8=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0;let n=new Array(e.length);for(let l=0;l<n.length;l++)n[l]=e[t[l]];if(this.outputShape=n,this.rank=n.length,this.rank>6)throw Error(`Packed transpose for rank ${this.rank} is not yet supported.`);let s=ot(this.rank),r=Y1("rc",this.rank),a=new Array(this.rank);for(let l=0;l<t.length;l++)a[t[l]]=r[l];let i=`vec2(${a.slice(-2).join()})`,o=`++${r[this.rank-1]} < ${n[this.rank-1]}`,u=`getChannel(getA(${a.join()}), ${i})`;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${s} rc = getOutputCoords();
vec4 result = vec4(0.);
result[0] = ${u};
if(${o}) {
result[1] = ${u};
}
--${r[this.rank-1]};
if(++${r[this.rank-2]} < ${n[this.rank-2]}) {
result[2] = ${u};
if(${o}) {
result[3] = ${u};
}
}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function rh(e,t,n){let s=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new O8(e.shape,t):new D8(e.shape,t);return n.runWebGLProgram(s,[e],e.dtype)}function P8(e,t,n,s){let r=t,a=e.shape.length,i=w.parseAxisParam(r,e.shape),o=i,u=C.getAxesPermutation(o,a),l=u!=null,c=e;l&&(c=rh(e,u,s),o=C.getInnerMostAxes(o.length,a)),C.assertAxesAreInnerMostDims("sum",o,a);let[p,d]=C.computeOutAndReduceShapes(c.shape,o),h=p;n&&(h=C.expandShapeToKeepDim(p,i));let f=w.sizeFromShape(d),g=w.sizeFromShape(e.shape)/f,b=pe({inputs:{x:c},attrs:{shape:[g,f]},backend:s}),y=yp(e.dtype),v=ki(b,y,"sum",s),x=pe({inputs:{x:v},attrs:{shape:h},backend:s});return s.disposeIntermediateTensorInfo(b),s.disposeIntermediateTensorInfo(v),l&&s.disposeIntermediateTensorInfo(c),x}function ah(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return P8(r,a,i,n)}var z8={kernelName:ci,backendName:"webgl",kernelFunc:ah};function dn(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{perm:a}=s,i=n,o=r.shape.length,u=new Array(o);for(let c=0;c<u.length;c++)u[c]=r.shape[a[c]];let l;if(i.shouldExecuteOnCPU([r])){let p=i.texData.get(r.dataId).values,d=_v(p,r.shape,r.dtype,a,u);l=i.makeTensorInfo(u,r.dtype);let h=i.texData.get(l.dataId);h.values=d}else l=rh(r,a,i);return l}var M8={kernelName:Hs,backendName:"webgl",kernelFunc:dn},i2=1e3;function Gd({a:e,b:t,transposeA:n,transposeB:s,backend:r,bias:a=null,preluActivationWeights:i=null,leakyreluAlpha:o=0,activation:u=null}){let l=e.shape.length,c=t.shape.length,p=n?e.shape[l-2]:e.shape[l-1],d=s?t.shape[c-1]:t.shape[c-2],h=n?e.shape[l-1]:e.shape[l-2],f=s?t.shape[c-2]:t.shape[c-1],m=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(m),y=w.sizeFromShape(g),x=Xo.assertAndGetBroadcastShape(e.shape.slice(0,-2),t.shape.slice(0,-2)).concat([h,f]);w.assert(p===d,()=>`Error in matMul: inner shapes (${p}) and (${d}) of Tensors with shapes ${e.shape} and ${t.shape} and transposeA=${n} and transposeB=${s} must match.`);let k=n?[b,p,h]:[b,h,p],I=s?[y,f,d]:[y,d,f],$=pe({inputs:{x:e},backend:r,attrs:{shape:k}}),R=pe({inputs:{x:t},backend:r,attrs:{shape:I}}),E=[$,R],P=Math.max(b,y),A=n?$.shape[1]:$.shape[2],O=a!=null,T=i!=null,M=u==="leakyrelu",W=u!=null?sh(u,!0):null,j=O||T||M||W!=null,Y;if((h===1||f===1)&&A>i2&&j===!1){let Z=$,ne=R;n&&(Z=dn({inputs:{x:$},backend:r,attrs:{perm:[0,2,1]}}),E.push(Z)),s&&(ne=dn({inputs:{x:R},backend:r,attrs:{perm:[0,2,1]}}),E.push(ne));let ee=f!==1,se=f===1,te=Z;ee&&(te=pe({inputs:{x:Z},backend:r,attrs:{shape:[P,A,1]}}),E.push(te));let oe=f===1?2:1,ae=ne;se&&(ae=pe({inputs:{x:ne},backend:r,attrs:{shape:[P,1,A]}}),E.push(ae));let de=Av({inputs:{a:te,b:ae},backend:r});Y=ah({inputs:{x:de},backend:r,attrs:{axis:oe,keepDims:!0}}),E.push(de)}else{let Z=ln(e.dtype,t.dtype),ne=new a2(k,I,[P,h,f],n,s,O,W,T,M),ee=[$,R];if(a!=null&&ee.push(a),T&&ee.push(i),M){let se=r.makeTensorInfo([],"float32",w.createScalarValue(o,"float32"));ee.push(se),E.push(se)}Y=r.runWebGLProgram(ne,ee,Z)}let X=pe({inputs:{x:Y},backend:r,attrs:{shape:x}});E.push(Y);for(let Z of E)r.disposeIntermediateTensorInfo(Z);return X}function L8(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a,bias:i,preluActivationWeights:o}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=s;return Gd({a:r,b:a,transposeA:u,transposeB:l,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:p,activation:c})}var B8={kernelName:ia,backendName:"webgl",kernelFunc:L8},xw="return abs(x);";function V8(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])&&s.dtype!=="complex64"){let a=n.texData.get(s.dataId),i=K1(a.values);return n.makeTensorInfo(s.shape,s.dtype,i)}let r;return K().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new ea(s.shape,xw):r=new Gs(s.shape,xw),n.runWebGLProgram(r,[s],s.dtype)}var W8={kernelName:lo,backendName:"webgl",kernelFunc:V8},U8=ss+`
2022-04-21 15:39:40 +02:00
if (abs(x) > 1.) {
return NAN;
}
return acos(x);
2022-05-30 03:12:18 +02:00
`,G8=Ke({opSnippet:U8}),H8={kernelName:ul,backendName:"webgl",kernelFunc:G8},q8=ss+`
2022-04-21 15:39:40 +02:00
if (x < 1.0) return NAN;
2022-05-30 03:12:18 +02:00
return log(x + sqrt(x * x - 1.0));`,j8=Ke({opSnippet:q8}),K8={kernelName:ll,backendName:"webgl",kernelFunc:j8},ww="return a + b;",X8=qt({opSnippet:ww,packedOpSnippet:ww,supportsComplex:!0,cpuKernelImpl:uX}),Y8={kernelName:Ir,backendName:"webgl",kernelFunc:X8},Q8=class{constructor(e,t){this.outputShape=[],this.outputShape=e,this.variableNames=t.map((r,a)=>`T${a}`);let n=[];this.variableNames.forEach(r=>{n.push(`float v${r} = get${r}AtOutCoords();`)});let s=this.variableNames.map(r=>`v${r}`).join(" + ");this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${n.join(`
`)}
float result = ${s};
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}},Z8=class{constructor(e,t){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.variableNames=t.map((r,a)=>`T${a}`);let n=[];this.variableNames.forEach(r=>{n.push(`vec4 v${r} = get${r}AtOutCoords();`)});let s=this.variableNames.map(r=>`v${r}`).join(" + ");this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${n.join(`
`)}
vec4 result = ${s};
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function hd(e){let{inputs:t,backend:n}=e,s=t;if(s.length===1)return Rn({inputs:{x:s[0]},backend:n});if(s.length>K().get("WEBGL_MAX_TEXTURES_IN_SHADER")){let u=Math.floor(s.length/2),l=hd({inputs:s.slice(0,u),backend:n}),c=hd({inputs:s.slice(u),backend:n});return hd({inputs:[l,c],backend:n})}let r=s.map(u=>u.dtype).reduce((u,l)=>ln(u,l)),a=s.map(u=>u.shape),o=K().getBool("WEBGL_PACK")?new Z8(s[0].shape,a):new Q8(s[0].shape,a);return n.runWebGLProgram(o,s,r)}var J8={kernelName:Sa,backendName:"webgl",kernelFunc:hd};function eY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,o),p=r;c!=null&&(p=dn({inputs:{x:r},backend:n,attrs:{perm:c}}),l=C.getInnerMostAxes(l.length,o)),C.assertAxesAreInnerMostDims("all",l,o);let[d,h]=C.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=ki(m,m.dtype,"all",n),b;if(i){let y=C.expandShapeToKeepDim(d,u);b=pe({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=pe({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),b}var tY={kernelName:cl,backendName:"webgl",kernelFunc:eY};function nY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,o),p=r;c!=null&&(p=dn({inputs:{x:r},backend:n,attrs:{perm:c}}),l=C.getInnerMostAxes(l.length,o)),C.assertAxesAreInnerMostDims("any",l,o);let[d,h]=C.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=ki(m,m.dtype,"any",n),b;if(i){let y=C.expandShapeToKeepDim(d,u);b=pe({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=pe({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),b}var sY={kernelName:dl,backendName:"webgl",kernelFunc:nY},rY=class{constructor(e,t,n){this.variableNames=["A"];let{windowSize:s,batchSize:r,outSize:a}=e;n||this.variableNames.push("bestIndicesA"),this.outputShape=[r,a];let i=t==="max"?">":"<",o=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${s};
int bestIndex = inOffset;
float bestValue = getA(batch, bestIndex);
for (int i = 0; i < ${s}; i++) {
int inIdx = ${o};
float candidate = getA(batch, inIdx);
if (candidate ${i} bestValue) {
bestValue = candidate;
bestIndex = inIdx;
2022-04-18 17:29:45 +02:00
}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(float(bestIndex));
}
2022-05-30 03:12:18 +02:00
`}},aY=class{constructor(e,t,n,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,w.assert(e.length>2,()=>`Packed arg${n.charAt(0).toUpperCase()+n.slice(1)} supports only inputs with rank above 2.`);let r=e[e.length-1],a=Math.ceil(r/t);this.outputShape=e.slice(0,-1),a>1&&this.outputShape.push(a),s||this.variableNames.push("bestIndicesA");let i=this.outputShape,o=i.length,u=ot(o),l=un("coords",o),c,p;if(a===1){p=o+1;let $=ot(p);c=`
2022-04-21 15:39:40 +02:00
${$} sourceLocR = ${$}(${l.join()}, 0);
++${l[o-1]};
${$} sourceLocG = ${$}(${l.join()}, 0);
++${l[o-2]};
${$} sourceLocA = ${$}(${l.join()}, 0);
--${l[o-1]};
${$} sourceLocB = ${$}(${l.join()}, 0);
--${l[o-2]};`}else p=o,c=`
${u} sourceLocR = coords;
++${l[o-1]};
${u} sourceLocG = coords;
++${l[o-2]};
${u} sourceLocA = coords;
--${l[o-1]};
${u} sourceLocB = coords;
2022-05-30 03:12:18 +02:00
--${l[o-2]};`;let d=["x","y","z","w","u","v"].slice(0,p),h="."+d[p-1],f=d.map($=>"int "+$),m=un("sourceLocR",p-1).concat("inIdx.r"),g=un("sourceLocG",p-1).concat("inIdx.g"),b=un("sourceLocB",p-1).concat("inIdx.b"),y=un("sourceLocA",p-1).concat("inIdx.a"),v=n==="max"?"greaterThan":"lessThan",x=s?"":`
2022-04-21 15:39:40 +02:00
inIdx = round(vec4(getBestIndicesAChannel(${m.join()}),
getBestIndicesAChannel(${g.join()}),
getBestIndicesAChannel(${b.join()}),
getBestIndicesAChannel(${y.join()})));`,k=`vec4(
getAChannel(${m.join()}),
hasNextCol ? getAChannel(${g.join()}) : 0.,
hasNextRow ? getAChannel(${b.join()}) : 0.,
2022-05-09 14:16:00 +02:00
hasNextRow && hasNextCol ? getAChannel(${y.join()}) : 0.)`,I=s?"":`
2022-04-21 15:39:40 +02:00
float getBestIndicesAChannel(${f.join()}) {
return getChannel(getBestIndicesA(${d.join()}),
vec2(${d.slice(-2).join()}));
}`;this.userCode=`
float getAChannel(${f.join()}) {
return getChannel(getA(${d.join()}),
vec2(${d.slice(-2).join()}));
}
2022-05-09 14:16:00 +02:00
${I}
2022-04-21 15:39:40 +02:00
void main() {
${u} coords = getOutputCoords();
bool hasNextCol = ${l[o-1]} < ${i[o-1]-1};
bool hasNextRow = ${l[o-2]} < ${i[o-2]-1};
${c}
ivec4 srcIdx = ivec4(sourceLocR${h}, sourceLocG${h},
sourceLocB${h}, sourceLocA${h}) * ${t};
ivec4 inIdx = srcIdx;
vec4 bestIndex = vec4(inIdx);
vec4 bestValue = ${k};
for (int i = 0; i < ${t}; i++) {
inIdx = srcIdx;
${x}
vec4 candidate = ${k};
bvec4 nan = isnan(candidate);
bvec4 replace = bvec4(
vec4(${v}(candidate, bestValue)) * (vec4(1.0) - vec4(nan)));
bestValue = vec4(replace.x ? candidate.x : bestValue.x,
replace.y ? candidate.y : bestValue.y,
replace.z ? candidate.z : bestValue.z,
replace.w ? candidate.w : bestValue.w);
bestIndex = mix(bestIndex, vec4(inIdx), vec4(replace));
srcIdx++;
}
setOutput(bestIndex);
}
2022-05-30 03:12:18 +02:00
`}};function o2(e,t,n,s=null){let r=t.shape[0],a=t.shape[1];s!=null&&(r=s.shape[0],a=s.shape[1]);let i=C.computeOptimalWindowSize(a),o={windowSize:i,inSize:a,batchSize:r,outSize:Math.ceil(a/i)},u=new rY(o,n,s==null),l=[t];s!=null&&l.push(s);let c=e.runWebGLProgram(u,l,"int32");if(c.shape[1]===1)return c;let p=o2(e,t,n,c);return e.disposeIntermediateTensorInfo(c),p}function u2(e,t,n,s=null){let r=s!=null?s.shape:t.shape,a=r[r.length-1],i=C.computeOptimalWindowSize(a),o=new aY(r,i,n,s==null),u=s==null?[t]:[t,s],l=e.runWebGLProgram(o,u,"int32");if(l.shape.length===t.shape.length){let c=u2(e,t,n,l);return e.disposeIntermediateTensorInfo(l),c}return l}function l2(e,t,n,s){let r=[n];if(C.assertAxesAreInnerMostDims("arg"+s.charAt(0).toUpperCase()+s.slice(1),r,t.shape.length),!K().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let a=[],i=e.texData.get(t.dataId),o=i!==null&&i.isPacked,u=t;o&&(u=e.unpackTensor(t),a.push(u));let[l,c]=C.computeOutAndReduceShapes(u.shape,r),p=w.sizeFromShape(c),d=pe({inputs:{x:u},backend:e,attrs:{shape:[-1,p]}});a.push(d);let h=o2(e,d,s);a.push(h);let f=pe({inputs:{x:h},backend:e,attrs:{shape:l}});return a.forEach(m=>e.disposeIntermediateTensorInfo(m)),f}return u2(e,t,s)}function iY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=C.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=dn({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=C.getInnerMostAxes(i.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMax",[i[0]],u.shape.length);let c=l2(n,u,i[0],"max");return l.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var oY={kernelName:Ia,backendName:"webgl",kernelFunc:iY};function uY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=C.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=dn({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=C.getInnerMostAxes(i.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMin",[i[0]],u.shape.length);let c=l2(n,u,i[0],"min");return l.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var lY={kernelName:pl,backendName:"webgl",kernelFunc:uY},cY=ss+`
2022-04-21 15:39:40 +02:00
if (abs(x) > 1.) {
return NAN;
}
return asin(x);
2022-05-30 03:12:18 +02:00
`,dY=Ke({opSnippet:cY}),pY={kernelName:hl,backendName:"webgl",kernelFunc:dY},hY=ss+"return log(x + sqrt(x * x + 1.0));",fY=Ke({opSnippet:hY}),mY={kernelName:fl,backendName:"webgl",kernelFunc:fY},gY=ss+`
2022-04-21 15:39:40 +02:00
return atan(x);
2022-05-30 03:12:18 +02:00
`,bY=Ke({opSnippet:gY}),yY={kernelName:ml,backendName:"webgl",kernelFunc:bY},vY=N8+`
2022-04-21 15:39:40 +02:00
return atan(a, b);
2022-05-30 03:12:18 +02:00
`,xY=`
2022-04-21 15:39:40 +02:00
vec4 result = atan(a, b);
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
2022-05-30 03:12:18 +02:00
`+T8+`
2022-04-21 15:39:40 +02:00
return result;
2022-05-30 03:12:18 +02:00
`,wY=qt({opSnippet:vY,packedOpSnippet:xY}),kY={kernelName:bl,backendName:"webgl",kernelFunc:wY},SY=ss+`
2022-04-21 15:39:40 +02:00
if ((x < -1.0) || (x > 1.0)) return NAN;
2022-05-30 03:12:18 +02:00
return (log(1.0 + x) - log(1.0 - x)) / 2.0;`,IY=Ke({opSnippet:SY}),CY={kernelName:gl,backendName:"webgl",kernelFunc:IY},al=class{constructor(e,t,n,s=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,i=e.strideHeight,o=e.strideWidth,u=e.dilationHeight,l=e.dilationWidth,c=e.effectiveFilterHeight,p=e.effectiveFilterWidth,d=e.padInfo.top,h=e.padInfo.left;this.outputShape=e.outShape;let f=t==="avg",m=`((batch * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + d`,g=`(xR * ${e.inWidth} + xC) * ${e.inChannels} + d`,b="0.0";if(f||(b="-1.0 / 1e-20"),n){let $=">=";this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec2 strides = ivec2(${i}, ${o});
const ivec2 pads = ivec2(${d}, ${h});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d = coords[3];
ivec2 xRCCorner = coords.yz * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
float minMaxValue = 0.0;
float minMaxValueFound = 0.0;
int minMaxPosition = 0;
float avgValue = 0.0;
for (int wR = 0; wR < ${c};
wR += ${u}) {
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
2022-04-18 18:26:05 +02:00
continue;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wC = 0; wC < ${p};
wC += ${l}) {
int xC = xCCorner + wC;
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float value = getX(batch, xR, xC, d);
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix(
value, minMaxValue, minMaxValueFound);
if (value ${$} currMinMaxValue) {
minMaxValue = value;
minMaxValueFound = 1.0;
minMaxPosition = ${s?r?m:g:`wR * ${p} + wC`};
2022-04-18 17:29:45 +02:00
}
2022-02-10 18:27:21 +01:00
}
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(float(minMaxPosition));
2022-04-18 17:29:45 +02:00
}
2022-05-09 14:16:00 +02:00
`;return}let y="max",v=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(v="avgValue / count");let x=Math.floor(a/4)*4,k=a%4,I=`
2022-04-21 15:39:40 +02:00
if (${f}) {
avgValue += dot(values, ones);
} else {
minMaxValue = ${y}(values, minMaxValue);
}
`;this.userCode=`
const ivec2 strides = ivec2(${i}, ${o});
const ivec2 pads = ivec2(${d}, ${h});
const float initializationValue = ${b};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float count = 0.0;
float getValue(int batch, int xR, int xC, int d) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
count += 1.0;
return getX(batch, xR, xC, d);
}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d = coords[3];
ivec2 xRCCorner = coords.yz * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
vec4 minMaxValue = vec4(${b});
float avgValue = 0.0;
count = 0.0;
for (int wR = 0; wR < ${c};
wR += ${u}) {
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wC = 0; wC < ${x}; wC += 4) {
int xC = xCCorner + wC * ${l};
vec4 values = vec4(
getValue(batch, xR, xC, d),
getValue(batch, xR, xC + ${l}, d),
getValue(batch, xR, xC + 2 * ${l}, d),
getValue(batch, xR, xC + 3 * ${l}, d)
);
2022-05-09 14:16:00 +02:00
${I}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int xC = xCCorner + ${x};
if (${k===1}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
initializationValue,
initializationValue,
initializationValue
);
2022-05-09 14:16:00 +02:00
${I}
2022-04-21 15:39:40 +02:00
} else if (${k===2}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
getValue(batch, xR, xC + ${l}, d),
initializationValue,
initializationValue
);
2022-05-09 14:16:00 +02:00
${I}
2022-04-21 15:39:40 +02:00
} else if (${k===3}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
getValue(batch, xR, xC + ${l}, d),
getValue(batch, xR, xC + 2 * ${l}, d),
initializationValue
);
2022-05-09 14:16:00 +02:00
${I}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
setOutput(${v});
}
2022-05-30 03:12:18 +02:00
`}},Ev=class{constructor(e,t,n,s=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,i=e.strideDepth,o=e.strideHeight,u=e.strideWidth,l=e.dilationDepth,c=e.dilationHeight,p=e.dilationWidth,d=e.effectiveFilterDepth,h=e.effectiveFilterHeight,f=e.effectiveFilterWidth,m=e.padInfo.front,g=e.padInfo.top,b=e.padInfo.left;this.outputShape=e.outShape;let y=t==="avg",v="0.0";if(y||(v="-1.0 / 1e-20"),n){let E=">=";this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec3 strides =
ivec3(${i}, ${o}, ${u});
const ivec3 pads = ivec3(${m}, ${g}, ${b});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
// max/min x(?, ?, ?, ch) to get y(yD, yR, yC, ch).
// ? = to be determined
float minMaxValue = 0.0;
float minMaxValueFound = 0.0;
int minMaxPosition = 0;
for (int wD = 0; wD < ${d};
wD += ${l}) {
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wR = 0; wR < ${h};
wR += ${c}) {
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wC = 0; wC < ${f};
wC += ${p}) {
int xC = xCCorner + wC;
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float value = getX(batch, xD, xR, xC, ch);
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix(
value, minMaxValue, minMaxValueFound);
2022-05-18 14:33:33 +02:00
if (value ${E} currMinMaxValue) {
2022-04-21 15:39:40 +02:00
minMaxValue = value;
minMaxValueFound = 1.0;
minMaxPosition = ${s?r?`(((batch * ${e.inDepth} + xD) * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + ch`:`((xD * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + ch`:`wD * ${h} * ${f} +
wR * ${f} + wC`};
}
2022-04-21 15:38:36 +02:00
}
2022-04-01 15:12:04 +02:00
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(float(minMaxPosition));
2022-04-21 15:38:36 +02:00
}
2022-05-18 14:33:33 +02:00
`;return}let x="max",k=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(k="avgValue / count");let I=Math.floor(a/4)*4,$=a%4,R=`
2022-04-21 15:39:40 +02:00
if (${y}) {
avgValue += dot(values, ones);
} else {
minMaxValue = ${x}(values, minMaxValue);
}
`;this.userCode=`
const ivec3 strides =
ivec3(${i}, ${o}, ${u});
const ivec3 pads = ivec3(${m}, ${g}, ${b});
const float initializationValue = ${v};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float count = 0.0;
float getValue(int batch, int xD, int xR, int xC, int ch) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
count += 1.0;
return getX(batch, xD, xR, xC, ch);
}
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
// max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).
// ? = to be determined
vec4 minMaxValue = vec4(${v});
float avgValue = 0.0;
count = 0.0;
for (int wD = 0; wD < ${d};
wD += ${l}) {
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wR = 0; wR < ${h};
wR += ${c}) {
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-02-10 18:27:21 +01:00
}
2022-04-21 15:39:40 +02:00
2022-05-09 14:16:00 +02:00
for (int wC = 0; wC < ${I}; wC += 4) {
2022-04-21 15:39:40 +02:00
int xC = xCCorner + wC * ${p};
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${p}, ch),
getValue(batch, xD, xR, xC + 2 * ${p}, ch),
getValue(batch, xD, xR, xC + 3 * ${p}, ch)
);
2022-05-18 14:33:33 +02:00
${R}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
2022-05-09 14:16:00 +02:00
int xC = xCCorner + ${I};
2022-04-21 15:39:40 +02:00
if (${$===1}) {
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
initializationValue,
initializationValue,
initializationValue
);
2022-05-18 14:33:33 +02:00
${R}
2022-04-21 15:39:40 +02:00
} else if (${$===2}) {
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${p}, ch),
initializationValue,
initializationValue
);
2022-05-18 14:33:33 +02:00
${R}
2022-04-21 15:39:40 +02:00
} else if (${$===3}) {
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${p}, ch),
getValue(batch, xD, xR, xC + 2 * ${p}, ch),
initializationValue
);
2022-05-18 14:33:33 +02:00
${R}
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(${k});
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function NY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t;ru(r,"avgPool");let{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1;w.assert(C.eitherStridesOrDilationsAreOne(i,l),()=>`Error in avgPool: Either strides or dilations must be 1. Got strides ${i} and dilations '${l}'`);let c=C.computePool2DInfo(r.shape,a,i,l,o,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Rn({inputs:{x:r},backend:n});let p=new al(c,"avg",!1);return n.runWebGLProgram(p,[r],"float32")}var TY={kernelName:Ca,backendName:"webgl",kernelFunc:NY};function $Y(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dimRoundingMode:u,dataFormat:l}=s,c=[1,1,1],p=C.computePool3DInfo(r.shape,a,i,c,o,u,l),d=new Ev(p,"avg",!1);return n.runWebGLProgram(d,[r],"float32")}var _Y={kernelName:ep,backendName:"webgl",kernelFunc:$Y},AY=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,s=e.strideHeight,r=e.strideWidth,a=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterHeight,u=e.effectiveFilterWidth,l=o-1-e.padInfo.top,c=u-1-e.padInfo.left,p=1/(t*n);this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec2 pads = ivec2(${l}, ${c});
const float avgMultiplier = float(${p});
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${o};
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${s}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int idyR = int(dyR);
for (int wC = 0; wC < ${u};
wC+= ${i}) {
float dyC = float(dyCCorner + wC) / ${r}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
dotProd += dyValue * avgMultiplier;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}},EY=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,s=e.filterWidth,r=e.strideDepth,a=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,u=e.dilationHeight,l=e.dilationWidth,c=e.effectiveFilterDepth,p=e.effectiveFilterHeight,d=e.effectiveFilterWidth,h=c-1-e.padInfo.front,f=p-1-e.padInfo.top,m=d-1-e.padInfo.left,g=1/(t*n*s);this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec3 pads = ivec3(${h}, ${f}, ${m});
const float avgMultiplier = float(${g});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyDCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
// Convolve dy(?, ?, ?, d) with pos mask(:, :, :, ch) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wD = 0; wD < ${c};
wD += ${o}) {
float dyD = float(dyDCorner + wD) / ${r}.0;
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int idyD = int(dyD);
for (int wR = 0; wR < ${p};
wR += ${u}) {
float dyR = float(dyRCorner + wR) / ${a}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int idyR = int(dyR);
for (int wC = 0; wC < ${d};
wC += ${l}) {
float dyC = float(dyCCorner + wC) / ${i}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
dotProd += dyValue * avgMultiplier;
2022-04-01 15:12:04 +02:00
}
2022-02-10 18:27:21 +01:00
}
}
2022-04-21 15:39:40 +02:00
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}};function RY(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,i=a,{filterSize:o,strides:u,pad:l,dimRoundingMode:c}=s,p=[1,1,1],d=C.computePool3DInfo(i.shape,o,u,p,l,c),h=new EY(d);return n.runWebGLProgram(h,[r],i.dtype)}var DY={kernelName:mg,backendName:"webgl",kernelFunc:RY};function FY(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,i=a;ru([r,a],"avgPoolGrad");let{filterSize:o,strides:u,pad:l}=s,c=C.computePool2DInfo(i.shape,o,u,1,l),p=new AY(c);return n.runWebGLProgram(p,[r],i.dtype)}var OY={kernelName:fg,backendName:"webgl",kernelFunc:FY};function PY(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a}=t,{transposeA:i,transposeB:o}=s;return Gd({a:r,b:a,transposeA:i,transposeB:o,backend:n})}var zY={kernelName:Na,backendName:"webgl",kernelFunc:PY},MY=class{constructor(e,t,n,s,r,a){this.outputShape=[],this.variableNames=["x","mean","variance"],C.assertAndGetBroadcastShape(e,t),C.assertAndGetBroadcastShape(e,n);let i="0.0";s!=null&&(C.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="1.0";r!=null&&(C.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
float x = getXAtOutCoords();
float mean = getMeanAtOutCoords();
float variance = getVarianceAtOutCoords();
float offset = ${i};
float scale = ${o};
float inv = scale * inversesqrt(variance + float(${a}));
setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));
}
2022-05-30 03:12:18 +02:00
`}},LY=class{constructor(e,t,n,s,r,a){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],C.assertAndGetBroadcastShape(e,t),C.assertAndGetBroadcastShape(e,n);let i="vec4(0.0)";s!=null&&(C.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="vec4(1.0)";r!=null&&(C.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
vec4 offset = ${i};
vec4 scale = ${o};
vec4 x = getXAtOutCoords();
vec4 mean = getMeanAtOutCoords();
vec4 variance = getVarianceAtOutCoords();
vec4 inv = scale * inversesqrt(variance + vec4(${a}));
setOutput((x - mean) * inv + offset);
}
2022-05-30 03:12:18 +02:00
`}},BY=({inputs:e,backend:t,attrs:n})=>{let{x:s,mean:r,variance:a,offset:i,scale:o}=e;w.assert(r.shape.length===a.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),w.assert(i==null||r.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),w.assert(o==null||r.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:u}=n;u==null&&(u=.001);let l=[s,r,a],c=null;i!=null&&(c=i.shape,l.push(i));let p=null;o!=null&&(p=o.shape,l.push(o));let d=K().getBool("WEBGL_PACK_NORMALIZATION")?new LY(s.shape,r.shape,a.shape,c,p,u):new MY(s.shape,r.shape,a.shape,c,p,u);return t.runWebGLProgram(d,l,l[0].dtype)},VY={kernelName:Ba,backendName:"webgl",kernelFunc:BY},WY=class{constructor(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;let t=ot(this.rank);this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let n=UY(this.rank),s,r=e.map((a,i)=>`sourceLoc.${Jm[i]} = start[${i}] + coords.${Jm[i]};`);s=`
2022-04-21 15:39:40 +02:00
${t} sourceLoc;
${t} coords = getOutputCoords();
${r.join(`
`)}
`,this.userCode=`
void main() {
${s}
setOutput(getSource(${n}));
}
2022-05-30 03:12:18 +02:00
`}},Jm=["x","y","z","w","u","v"];function UY(e){if(e===1)return"sourceLoc";if(e<=6)return Jm.slice(0,e).map(t=>"sourceLoc."+t).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}var GY=class{constructor(e){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.rank=e.length,this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let t=ot(this.rank),n=un("coords",this.rank),s=un("sourceLoc",this.rank),r=this.rank===1?"sourceLoc":`vec2(${s.slice(-2).join()})`,a=`getChannel(getSource(${s.join()}), ${r})`,i=`
2022-04-21 15:39:40 +02:00
result.x = ${a};
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
++${s[this.rank-1]};
result.y = ${a};
--${s[this.rank-1]};
}
`,o=this.rank===1?"":`
--${n[this.rank-1]};
if (++${n[this.rank-2]} < ${e[this.rank-2]}) {
++${s[this.rank-2]};
result.z = ${a};
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
++${s[this.rank-1]};
result.w = ${a};
}
}
`,u=this.rank<=4?`sourceLoc = coords +
${t}(${e.map((l,c)=>`start[${c}]`).join()});`:e.map((l,c)=>`${s[c]} = ${n[c]} + start[${c}];`).join(`
`);this.userCode=`
void main() {
${t} coords = getOutputCoords();
${t} sourceLoc;
${u}
vec4 result = vec4(0.);
${i}
${o}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function HY(e,t,n,s){let r=s.texData.get(e.dataId),a=s.makeTensorInfo(n,e.dtype),i=s.texData.get(a.dataId);Object.assign(i,r),i.refCount=1,i.shape=n,i.dtype=e.dtype;let o=kt.computeFlatOffset(t,w.computeStrides(e.shape));r.slice&&(o+=r.slice.flatOffset),i.slice={flatOffset:o,origDataId:r.slice&&r.slice.origDataId||e.dataId};let u=s.dataRefCount.get(i.slice.origDataId)||1;return s.dataRefCount.set(i.slice.origDataId,u+1),a}function cu(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,size:i}=s,[o,u]=kt.parseSliceParams(r,a,i);if(kt.assertParamsValid(r,o,u),w.sizeFromShape(u)===0)return n.makeTensorInfo(u,r.dtype,[]);if(n.shouldExecuteOnCPU([r])||r.dtype==="string"){let p=n.texData.get(r.dataId),d=OX(p.values,o,u,r.shape,r.dtype);return n.makeTensorInfo(u,r.dtype,d)}let{isPacked:l}=n.texData.get(r.dataId),c=kt.isSliceContinous(r.shape,o,u);if(l||!c){let p=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new GY(u):new WY(u),d=[o];return n.runWebGLProgram(p,[r],r.dtype,d)}return n.uploadToGPU(r.dataId),HY(r,o,u,n)}var qY={kernelName:Mo,backendName:"webgl",kernelFunc:cu},jY=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,crops:i}=s;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let o=a.reduce((y,v)=>y*v),u=C.getReshaped(r.shape,a,o),l=C.getPermuted(u.length,a.length),c=C.getReshapedPermuted(r.shape,a,o),p=C.getSliceBeginCoords(i,a.length),d=C.getSliceSize(c,i,a.length),h=[],f=pe({inputs:{x:r},backend:n,attrs:{shape:u}}),m=dn({inputs:{x:f},backend:n,attrs:{perm:l}}),g=pe({inputs:{x:m},backend:n,attrs:{shape:c}}),b=cu({inputs:{x:g},backend:n,attrs:{begin:p,size:d}});return h.push(f),h.push(m),h.push(g),h.forEach(y=>n.disposeIntermediateTensorInfo(y)),b},KY={kernelName:co,backendName:"webgl",kernelFunc:jY};function XY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,weights:a}=t,{size:i}=s,o=n.readSync(r.dataId),u=n.readSync(a.dataId),l=j1(o,u,a.dtype,a.shape,i);return n.makeTensorInfo([i],a.dtype,l)}var YY={kernelName:gg,backendName:"webgl",kernelFunc:XY};function QY(e){let{inputs:t,backend:n}=e,{s0:s,s1:r}=t,a=n.readSync(s.dataId),i=n.readSync(r.dataId),o=C.assertAndGetBroadcastShape(Array.from(a),Array.from(i));return n.makeTensorInfo([o.length],"int32",Int32Array.from(o))}var ZY={kernelName:bg,backendName:"webgl",kernelFunc:QY},JY="return float(a != b);",c2=qt({opSnippet:JY,cpuKernelImpl:_X,dtype:"bool"}),e9={kernelName:To,backendName:"webgl",kernelFunc:c2};function nc(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.texData.get(s.dataId);return Rn({inputs:{x:r.complexTensorInfos.real},backend:n})}var t9={kernelName:cp,backendName:"webgl",kernelFunc:nc},n9="return float(int(x));";function s9(e,t){let n=new Gs(e.shape,n9),s=t.runWebGLProgram(n,[e],"int32");return{dataId:s.dataId,shape:s.shape,dtype:s.dtype}}function eg(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dtype:a}=s;if(a==="complex64"){if(r.dtype==="complex64")return Rn({inputs:{x:r},backend:n});let i=$t(r.shape),o=eg({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),u=Dr({inputs:{real:o,imag:i},backend:n});return i.dispose(),n.disposeIntermediateTensorInfo(o),u}if(r.dtype==="complex64"){let i=nc({inputs:{input:r},backend:n}),o=eg({inputs:{x:i},backend:n,attrs:{dtype:a}});return n.disposeIntermediateTensorInfo(i),o}if(!w.hasEncodingLoss(r.dtype,a)){let i=Rn({inputs:{x:r},backend:n});return{dataId:i.dataId,shape:i.shape,dtype:a}}if(a==="int32")return s9(r,n);if(a==="bool"){let i=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=c2({inputs:{a:r,b:i},backend:n});return n.disposeIntermediateTensorInfo(i),u}throw new Error(`Error in Cast: failed to cast ${r.dtype} to ${a}`)}var r9={kernelName:Ta,backendName:"webgl",kernelFunc:eg},kw="return ceil(x);",a9=Ke({opSnippet:kw,packedOpSnippet:kw,cpuKernelImpl:cX}),i9={kernelName:$a,backendName:"webgl",kernelFunc:a9},o9=class{constructor(e){this.variableNames=["A"],this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
float value = getAAtOutCoords();
if (isnan(value)) {
setOutput(value);
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(clamp(value, minVal, maxVal));
}
2022-05-30 03:12:18 +02:00
`}},u9=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
vec4 value = getAAtOutCoords();
if (any(isnan(value))) {
setOutput(value);
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(clamp(value, vec4(minVal), vec4(maxVal)));
}
2022-05-30 03:12:18 +02:00
`}};function l9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{clipValueMin:a,clipValueMax:i}=s,o;K().getBool("WEBGL_PACK_CLIP")?o=new u9(r.shape):o=new o9(r.shape);let u=[[a],[i]];return n.runWebGLProgram(o,[r],r.dtype,u)}var c9={kernelName:Cr,backendName:"webgl",kernelFunc:l9},d9=class{constructor(e){this.variableNames=["real","imag"],this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
float re = abs(getRealAtOutCoords());
float im = abs(getImagAtOutCoords());
float mx = max(re, im);
// sadly the length function in glsl is not underflow-safe
// (at least not on Intel GPUs). So the safe solution is
// to ensure underflow-safety in all cases.
setOutput(
mx == 0.0 ? 0.0 : mx * length(vec2(1, min(re, im)/mx))
);
}
2022-05-30 03:12:18 +02:00
`}};function Sw(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function p9(e){let{inputs:t,backend:n}=e,{x:s}=t,r=n.texData.get(s.dataId),a=new d9(s.shape),i=[Sw(s,r.complexTensorInfos.real),Sw(s,r.complexTensorInfos.imag)];return n.runWebGLProgram(a,i,i[0].dtype)}var h9={kernelName:np,backendName:"webgl",kernelFunc:p9},f9=class{constructor(e){this.outputShape=[],this.outputShape=C.computeOutShape(e,1),this.variableNames=e.map((a,i)=>`T${i}`);let t=new Array(e.length-1);t[0]=e[0][1];for(let a=1;a<t.length;a++)t[a]=t[a-1]+e[a][1];let n=[`if (yC < ${t[0]}) setOutput(getT0(yR, yC));`];for(let a=1;a<t.length;a++){let i=t[a-1];n.push(`else if (yC < ${t[a]}) setOutput(getT${a}(yR, yC-${i}));`)}let s=t.length,r=t[t.length-1];n.push(`else setOutput(getT${s}(yR, yC-${r}));`),this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
int yR = coords.x;
int yC = coords.y;
${n.join(`
`)}
}
2022-05-30 03:12:18 +02:00
`}},m9=class{constructor(e,t){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=C.computeOutShape(e,t);let n=this.outputShape,s=n.length,r=ot(s),a=un("coords",s),i=["x","y","z","w","u","v"].slice(0,s);this.variableNames=e.map((f,m)=>`T${m}`);let o=new Array(e.length-1);o[0]=e[0][t];for(let f=1;f<o.length;f++)o[f]=o[f-1]+e[f][t];let u=i[t],l=i.slice(-2),c=i.join(),p=`if (${u} < ${o[0]}) {
2022-04-21 15:39:40 +02:00
return getChannel(
getT0(${c}), vec2(${l.join()}));
}`;for(let f=1;f<o.length;f++){let m=o[f-1];p+=`
if (${u} < ${o[f]} && ${u} >= ${o[f-1]}) {
return getChannel(
2022-05-22 14:50:51 +02:00
getT${f}(${sd(i,u,m)}),
vec2(${sd(l,u,m)}));
2022-04-21 15:39:40 +02:00
}`}let d=o.length,h=o[o.length-1];p+=`
return getChannel(
2022-05-22 14:50:51 +02:00
getT${d}(${sd(i,u,h)}),
vec2(${sd(l,u,h)}));`,this.userCode=`
2022-04-21 15:39:40 +02:00
float getValue(${i.map(f=>"int "+f)}) {
${p}
}
void main() {
${r} coords = getOutputCoords();
vec4 result = vec4(getValue(${a}), 0., 0., 0.);
${a[s-1]} = ${a[s-1]} + 1;
if (${a[s-1]} < ${n[s-1]}) {
result.g = getValue(${a});
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${a[s-2]} = ${a[s-2]} + 1;
if (${a[s-2]} < ${n[s-2]}) {
result.a = getValue(${a});
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${a[s-1]} = ${a[s-1]} - 1;
if (${a[s-2]} < ${n[s-2]} &&
${a[s-1]} < ${n[s-1]}) {
result.b = getValue(${a});
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function sd(e,t,n){let s=e.indexOf(t);return e.map((a,i)=>i===s?`${a} - ${n}`:a).join()}function ih(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.texData.get(s.dataId);return Rn({inputs:{x:r.complexTensorInfos.imag},backend:n})}var g9={kernelName:ip,backendName:"webgl",kernelFunc:ih};function Hi(e,t,n){let s=e[0].dtype;if(s==="complex64"){let c=e.map(m=>nc({inputs:{input:m},backend:n})),p=e.map(m=>ih({inputs:{input:m},backend:n})),d=Hi(c,t,n),h=Hi(p,t,n),f=Dr({inputs:{real:d,imag:h},backend:n});return c.forEach(m=>n.disposeIntermediateTensorInfo(m)),p.forEach(m=>n.disposeIntermediateTensorInfo(m)),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}let r=n.shouldExecuteOnCPU(e);if(s==="string"&&(r=!0),r){let c=e.map(b=>{let y=w.sizeFromShape(b.shape.slice(t));return pe({inputs:{x:b},backend:n,attrs:{shape:[-1,y]}})}),p=c.map(b=>({vals:n.readSync(b.dataId),shape:b.shape})),d=C.computeOutShape(c.map(b=>b.shape),1),h=c[0].shape[0]===1,f=dX(p,d,s,h),m=C.computeOutShape(e.map(b=>b.shape),t),g=n.makeTensorInfo(m,s,f);return c.forEach(b=>n.disposeIntermediateTensorInfo(b)),g}if(e.length>K().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let c=Math.floor(e.length/2),p=Hi(e.slice(0,c),t,n),d=Hi(e.slice(c),t,n),h=Hi([p,d],t,n);return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),h}if(K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&e[0].shape.length>1){let c=new m9(e.map(p=>p.shape),t);return n.runWebGLProgram(c,e,s)}let{tensors2D:a,outShape:i}=b9(e,t,n),o=new f9(a.map(c=>c.shape)),u=n.runWebGLProgram(o,a,s);a.forEach(c=>n.disposeIntermediateTensorInfo(c));let l=pe({inputs:{x:u},attrs:{shape:i},backend:n});return n.disposeIntermediateTensorInfo(u),l}function b9(e,t,n){let s=C.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>pe({inputs:{x:a},attrs:{shape:[-1,w.sizeFromShape(a.shape.slice(t))]},backend:n})),outShape:s}}function d2(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s,a=w.parseAxisParam(r,t[0].shape)[0],i=C.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(i)===0)return n.makeTensorInfo(i,t[0].dtype,[]);let o=t.filter(l=>w.sizeFromShape(l.shape)>0);if(o.length===1)return Rn({inputs:{x:o[0]},backend:n});let u=o.map(l=>l.shape);return C.assertParamsConsistent(u,a),Hi(o,a,n)}var y9={kernelName:po,backendName:"webgl",kernelFunc:d2},p2=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let a=e.padInfo.top,i=e.padInfo.left,o=e.strideHeight,u=e.strideWidth,l=e.dilationHeight,c=e.dilationWidth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4,m=e.dataFormat==="channelsLast",g=m?1:2,b=m?2:3,y=m?3:1,v="",x="";n&&(s?v=`float activation(float a) {
2022-04-21 15:39:40 +02:00
float b = getPreluActivationWeightsAtOutCoords();
${n}
}`:r?v=`float activation(float a) {
float b = getLeakyreluAlphaAtOutCoords();
${n}
}`:v=`
float activation(float x) {
${n}
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`,x="result = activation(result);");let k=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${v}
const ivec2 strides = ivec2(${o}, ${u});
const ivec2 pads = ivec2(${a}, ${i});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d2 = coords[${y}];
ivec2 xRCCorner =
ivec2(coords[${g}], coords[${b}]) * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${p}; wR++) {
int xR = xRCorner + wR * ${l};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wC = 0; wC < ${d}; wC++) {
int xC = xCCorner + wC * ${c};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int d1 = 0; d1 < ${h}; d1 += 4) {
vec4 wValues = vec4(
getW(wR, wC, d1, d2),
getW(wR, wC, d1 + 1, d2),
getW(wR, wC, d1 + 2, d2),
getW(wR, wC, d1 + 3, d2)
);
if (${m}) {
vec4 xValues = vec4(
getX(batch, xR, xC, d1),
getX(batch, xR, xC, d1 + 1),
getX(batch, xR, xC, d1 + 2),
getX(batch, xR, xC, d1 + 3)
);
dotProd += dot(xValues, wValues);
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
vec4 xValues = vec4(
getX(batch, d1, xR, xC),
getX(batch, d1 + 1, xR, xC),
getX(batch, d1 + 2, xR, xC),
getX(batch, d1 + 3, xR, xC)
);
dotProd += dot(xValues, wValues);
2022-04-21 15:38:36 +02:00
}
}
2022-04-21 15:39:40 +02:00
if (${f===1}) {
if (${m}) {
dotProd +=
getX(batch, xR, xC, ${h}) *
getW(wR, wC, ${h}, d2);
} else {
dotProd +=
getX(batch, ${h}, xR, xC) *
getW(wR, wC, ${h}, d2);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
} else if (${f===2}) {
vec2 wValues = vec2(
getW(wR, wC, ${h}, d2),
getW(wR, wC, ${h} + 1, d2)
);
if (${m}) {
vec2 xValues = vec2(
getX(batch, xR, xC, ${h}),
getX(batch, xR, xC, ${h} + 1)
);
dotProd += dot(xValues, wValues);
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
vec2 xValues = vec2(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC)
);
dotProd += dot(xValues, wValues);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
} else if (${f===3}) {
vec3 wValues = vec3(
getW(wR, wC, ${h}, d2),
getW(wR, wC, ${h} + 1, d2),
getW(wR, wC, ${h} + 2, d2)
);
if (${m}) {
vec3 xValues = vec3(
getX(batch, xR, xC, ${h}),
getX(batch, xR, xC, ${h} + 1),
getX(batch, xR, xC, ${h} + 2)
);
dotProd += dot(xValues, wValues);
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
vec3 xValues = vec3(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC),
getX(batch, ${h} + 2, xR, xC)
);
dotProd += dot(xValues, wValues);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
2022-04-18 17:29:45 +02:00
}
2022-04-01 15:13:32 +02:00
}
}
2022-04-21 15:39:40 +02:00
float result = dotProd;
${k}
${x}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}},v9=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let t=e.padInfo.front,n=e.padInfo.top,s=e.padInfo.left,r=e.strideDepth,a=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,u=e.dilationHeight,l=e.dilationWidth,c=e.filterDepth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec3 strides = ivec3(${r}, ${a}, ${i});
const ivec3 pads = ivec3(${t}, ${n}, ${s});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d2 = coords.u;
ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xFCorner = xFRCCorner.x;
int xRCorner = xFRCCorner.y;
int xCCorner = xFRCCorner.z;
// Convolve x(?, ?, ?, d1) with w(:, :, :, d1, d2) to get
// y(yF, yR, yC, d2). ? = to be determined. : = across all
// values in that axis.
float dotProd = 0.0;
for (int wF = 0; wF < ${c}; wF++) {
int xF = xFCorner + wF * ${o};
if (xF < 0 || xF >= ${e.inDepth}) {
continue;
}
for (int wR = 0; wR < ${p}; wR++) {
int xR = xRCorner + wR * ${u};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int wC = 0; wC < ${d}; wC++) {
int xC = xCCorner + wC * ${l};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
for (int d1 = 0; d1 < ${h}; d1 += 4) {
vec4 xValues = vec4(
getX(batch, xF, xR, xC, d1),
getX(batch, xF, xR, xC, d1 + 1),
getX(batch, xF, xR, xC, d1 + 2),
getX(batch, xF, xR, xC, d1 + 3)
);
vec4 wValues = vec4(
getW(wF, wR, wC, d1, d2),
getW(wF, wR, wC, d1 + 1, d2),
getW(wF, wR, wC, d1 + 2, d2),
getW(wF, wR, wC, d1 + 3, d2)
);
dotProd += dot(xValues, wValues);
}
if (${f===1}) {
dotProd +=
getX(batch, xF, xR, xC, ${h}) *
getW(wF, wR, wC, ${h}, d2);
} else if (${f===2}) {
vec2 xValues = vec2(
getX(batch, xF, xR, xC, ${h}),
getX(batch, xF, xR, xC, ${h} + 1)
);
vec2 wValues = vec2(
getW(wF, wR, wC, ${h}, d2),
getW(wF, wR, wC, ${h} + 1, d2)
);
dotProd += dot(xValues, wValues);
} else if (${f===3}) {
vec3 xValues = vec3(
getX(batch, xF, xR, xC, ${h}),
getX(batch, xF, xR, xC, ${h} + 1),
getX(batch, xF, xR, xC, ${h} + 2)
);
vec3 wValues = vec3(
getW(wF, wR, wC, ${h}, d2),
getW(wF, wR, wC, ${h} + 1, d2),
getW(wF, wR, wC, ${h} + 2, d2)
);
dotProd += dot(xValues, wValues);
}
2022-04-21 15:38:36 +02:00
}
}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}},x9=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec4"},{name:"pad",type:"ivec2"},{name:"stride",type:"ivec2"},{name:"dilation",type:"ivec2"},{name:"inChannels",type:"int"},{name:"itemsPerBlockRow",type:"int"},{name:"outWidth",type:"int"}],this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let{dataFormat:n}=t,s=fn(),r=n==="channelsLast",a=r?1:2,i=r?2:3,o=this.enableShapeUniforms?"if(blockIndex < outShape[2] && pos < outShape[1]) {":`if(blockIndex < ${e[2]} && pos < ${e[1]}) {`,u="";for(let l=0;l<=1;l++)for(let c=0;c<=1;c++)u+=`
blockIndex = rc.z + ${c};
pos = rc.y + ${l};
2022-04-21 15:39:40 +02:00
${o}
offsetY = int(blockIndex / outWidth) * stride[0] - pad[0];
d0 = offsetY + dilation[0] * (pos / itemsPerBlockRow);
if(d0 < inputShape[${a}] && d0 >= 0) {
// Use custom imod instead mod. On Intel GPU, mod may generate
// unexpected value.
// https://github.com/tensorflow/tfjs/issues/5447
offsetX = imod(blockIndex, outWidth) * stride[1] - pad[1];
d1 = offsetX + dilation[1] * (imod(pos, itemsPerBlockRow) /
inChannels);
if(d1 < inputShape[${i}] && d1 >= 0) {
ch = imod(pos, inChannels);
if (${r}) {
innerDims = vec2(d1, ch);
result[${l*2+c}] = getChannel(
2022-05-30 03:12:18 +02:00
getA(rc.x, d0, int(innerDims.x),
2022-04-21 15:39:40 +02:00
int(innerDims.y)), innerDims);
} else {
innerDims = vec2(d0, d1);
result[${l*2+c}] = getChannel(
2022-05-30 03:12:18 +02:00
getA(rc.x, ch, int(innerDims.x),
2022-04-21 15:39:40 +02:00
int(innerDims.y)), innerDims);
}
}
2022-04-21 15:38:36 +02:00
}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`;this.userCode=`
void main() {
2022-05-30 03:12:18 +02:00
ivec3 rc = getOutputCoords();
2022-04-21 15:39:40 +02:00
vec4 result = vec4(0);
int blockIndex, pos, offsetY, d0, offsetX, d1, ch;
vec2 innerDims;
${u}
${s.output} = result;
}
2022-05-30 03:12:18 +02:00
`}};function Hd(e,t){let n=e.length;return n>=3?t?[...e.slice(0,-3),e[n-3]*e[n-2],e[n-1]]:[...e.slice(0,-3),e[n-3],e[n-2]*e[n-1]]:!t&&n===1&&e[0]>1?[e[0],1]:null}function h2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let u=e.shape,l=s.texData.get(e.dataId),c=n.inChannels,p=u[0]*u[1]*u[2],d=n.outChannels,h=n.dataFormat==="channelsLast",f=!1,m=!1,g,b=[];if(a!=null){let x=Hd(a.shape,h);x!=null&&(a=pe({inputs:{x:a},backend:s,attrs:{shape:x}}),b.push(a))}if(r!=null){let x=Hd(r.shape,h);x!=null&&(r=pe({inputs:{x:r},backend:s,attrs:{shape:x}}),b.push(r))}if(!((p===1||d===1)&&c>i2)&&l.isPacked&&h&&l.texture!=null&&u[2]%2!==0&&w.arraysEqual(l.shape.slice(-3),u.slice(-3))){let x=u[0]*u[1]*(u[2]+1),k={dataId:e.dataId,shape:[1,x,n.inChannels],dtype:e.dtype},I=l.shape;l.shape=l.shape.slice(),l.shape[l.shape.length-2]++,w.assert(rl(l.shape,k.shape),()=>`packed reshape ${l.shape} to ${k.shape} isn't free`);let $=pe({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}});b.push($);let R=Gd({a:k,b:$,backend:s,transposeA:f,transposeB:m,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i}),E=s.texData.get(R.dataId);w.assert(E.isPacked,()=>"batchMatMul result is expected to be packed"),l.shape=I,E.shape=n.outShape,g=Rn({inputs:{x:R},backend:s}),g.shape=n.outShape,b.push(R)}else{let x=n.outHeight*n.outWidth,k=pe({inputs:{x:e},backend:s,attrs:{shape:h?[n.batchSize,x,n.inChannels]:[n.batchSize,n.inChannels,x]}}),I=pe({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}}),$=Gd({a:h?k:I,b:h?I:k,transposeA:!h,transposeB:m,backend:s,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i});g=pe({inputs:{x:$},backend:s,attrs:{shape:n.outShape}}),b.push(k),b.push(I),b.push($)}for(let x of b)s.disposeIntermediateTensorInfo(x);return g}function f2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let{filterWidth:u,filterHeight:l,inChannels:c,outWidth:p,outHeight:d,dataFormat:h}=n,f=h==="channelsLast",m=u*l*c,g=d*p,b=[n.batchSize,m,g],y=!0,v=!1,x=[];if(a!=null){let X=Hd(a.shape,f);X!=null&&(a=pe({inputs:{x:a},backend:s,attrs:{shape:X}}),x.push(a))}if(r!=null){let X=Hd(r.shape,f);X!=null&&(r=pe({inputs:{x:r},backend:s,attrs:{shape:X}}),x.push(r))}let k=pe({inputs:{x:t},backend:s,attrs:{shape:[1,m,w.sizeFromShape(t.shape)/m]}});x.push(k);let I=new x9(b,n),$=[e.shape,[n.padInfo.top,n.padInfo.left],[n.strideHeight,n.strideWidth],[n.dilationHeight,n.dilationWidth],[n.inChannels],[n.filterWidth*n.inChannels],[n.outWidth]],R=s.runWebGLProgram(I,[e],"float32",$),E=pe({inputs:{x:R},backend:s,attrs:{shape:b}});x.push(R),x.push(E);let P=r!=null,A=a!=null,O=o==="leakyrelu",T=o?sh(o,!0):null,M=new a2(f?E.shape:k.shape,f?k.shape:E.shape,f?[n.batchSize,g,n.outChannels]:[n.batchSize,n.outChannels,g],y,v,P,T,A,O),W=f?[E,k]:[k,E];if(r&&W.push(r),A&&W.push(a),O){let X=s.makeTensorInfo([],"float32",w.createScalarValue(i,"float32"));W.push(X),x.push(X)}let j=s.runWebGLProgram(M,W,"float32"),Y=pe({inputs:{x:j},backend:s,attrs:{shape:n.outShape}});x.push(j);for(let X of x)s.disposeIntermediateTensorInfo(X);return Y}function w9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dataFormat:u,dilations:l,dimRoundingMode:c}=s,p=C.convertConv2DDataFormat(u),d=C.computeConv2DInfo(r.shape,a.shape,i,l,o,c,!1,p),h;if(d.filterHeight===1&&d.filterWidth===1&&d.dilationHeight===1&&d.dilationWidth===1&&d.strideHeight===1&&d.strideWidth===1&&(d.padInfo.type==="SAME"||d.padInfo.type==="VALID"))h=h2({x:r,filter:a,convInfo:d,backend:n});else if(K().getBool("WEBGL_CONV_IM2COL"))h=f2({x:r,filter:a,convInfo:d,backend:n});else{let m=new p2(d);h=n.runWebGLProgram(m,[r,a],"float32")}let f=pe({inputs:{x:h},backend:n,attrs:{shape:d.outShape}});return n.disposeIntermediateTensorInfo(h),f}var k9={kernelName:_a,backendName:"webgl",kernelFunc:w9},S9=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,s=e.padInfo.top,r=e.padInfo.lef
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int wR = coords.x;
int wC = coords.y;
int d1 = coords.z;
int d2 = coords.w;
// Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${t} - ${s};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${n} - ${r};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
if (${a}) {
float dyValue = getDy(b, yR, yC, d2);
float xValue = getX(b, xR, xC, d1);
dotProd += (xValue * dyValue);
} else {
float dyValue = getDy(b, d2, yR, yC);
float xValue = getX(b, d1, xR, xC);
dotProd += (xValue * dyValue);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
2022-04-21 15:38:36 +02:00
}
}
}
2022-04-21 15:39:40 +02:00
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}},I9=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,s=e.strideHeight,r=e.strideWidth,a=e.dataFormat==="channelsLast",i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,u=a?1:2,l=a?2:3,c=a?3:1;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec2 pads = ivec2(${i}, ${o});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d1 = coords[${c}];
ivec2 dyCorner = ivec2(coords[${u}], coords[${l}]) - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / ${s}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int idyR = int(dyR);
int wRPerm = ${t} - 1 - wR;
for (int wC = 0; wC < ${n}; wC++) {
float dyC = float(dyCCorner + wC) / ${r}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
2022-04-18 18:26:05 +02:00
continue;
}
2022-04-21 15:39:40 +02:00
int idyC = int(dyC);
int wCPerm = ${n} - 1 - wC;
for (int d2 = 0; d2 < ${e.outChannels}; d2++) {
if (${a}) {
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
float xValue = getDy(batch, d2, idyR, idyC);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
2022-04-18 18:26:05 +02:00
}
}
}
2022-04-21 15:39:40 +02:00
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}},C9=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideDepth,n=e.strideHeight,s=e.strideWidth,r=e.padInfo.front,a=e.padInfo.top,i=e.padInfo.left;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec5 coords = getOutputCoords();
int wF = coords.x;
int wR = coords.y;
int wC = coords.z;
int d1 = coords.w;
int d2 = coords.u;
float dotProd = 0.0;
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yF = 0; yF < ${e.outDepth}; yF++) {
int xF = wF + yF * ${t} - ${r};
if (xF < 0 || xF >= ${e.inDepth}) {
continue;
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${n} - ${a};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${s} - ${i};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float dyValue = getDy(b, yF, yR, yC, d2);
float xValue = getX(b, xF, xR, xC, d1);
dotProd += (xValue * dyValue);
}
2022-04-18 18:26:05 +02:00
}
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}},N9=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,s=e.filterWidth,r=e.strideDepth,a=e.strideHeight,i=e.strideWidth,o=t-1-e.padInfo.front,u=n-1-e.padInfo.top,l=s-1-e.padInfo.left;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec3 pads = ivec3(${o}, ${u}, ${l});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyFCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
float dotProd = 0.0;
for (int wF = 0; wF < ${t}; wF++) {
float dyF = float(dyFCorner + wF) / ${r}.0;
if (dyF < 0.0 || dyF >= ${e.outDepth}.0 || fract(dyF) > 0.0) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
int idyF = int(dyF);
int wFPerm = ${t} - 1 - wF;
for (int wR = 0; wR < ${n}; wR++) {
float dyR = float(dyRCorner + wR) / ${a}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
int idyR = int(dyR);
int wRPerm = ${n} - 1 - wR;
for (int wC = 0; wC < ${s}; wC++) {
float dyC = float(dyCCorner + wC) / ${i}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
int wCPerm = ${s} - 1 - wC;
for (int d2 = 0; d2 < ${e.outChannels}; d2++) {
float xValue = getDy(batch, idyF, idyR, idyC, d2);
float wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
}
2022-04-21 15:38:36 +02:00
}
2022-04-18 18:26:05 +02:00
}
}
2022-04-21 15:39:40 +02:00
setOutput(dotProd);
}
2022-05-30 03:12:18 +02:00
`}};function T9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:i,pad:o,dataFormat:u,dimRoundingMode:l,filterShape:c}=s,p=C.convertConv2DDataFormat(u),d=C.computeConv2DInfo(r.shape,c,i,1,o,l,!1,p),h=new S9(d);return n.runWebGLProgram(h,[r,a],"float32")}var $9={kernelName:yg,backendName:"webgl",kernelFunc:T9};function _9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{inputShape:i,strides:o,pad:u,dataFormat:l,dimRoundingMode:c}=s,p=C.convertConv2DDataFormat(l),d=C.computeConv2DInfo(i,a.shape,o,1,u,c,!1,p),h=new I9(d);return n.runWebGLProgram(h,[r,a],"float32")}var A9={kernelName:Aa,backendName:"webgl",kernelFunc:_9};function E9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u}=s,l=C.computeConv3DInfo(r.shape,a.shape,i,u,o),c=new v9(l);return n.runWebGLProgram(c,[r,a],"float32")}var R9={kernelName:sp,backendName:"webgl",kernelFunc:E9};function D9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:i,pad:o,filterShape:u}=s,l=C.computeConv3DInfo(r.shape,u,i,1,o),c=new C9(l);return n.runWebGLProgram(c,[r,a],"float32")}var F9={kernelName:vg,backendName:"webgl",kernelFunc:D9};function O9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{pad:i,strides:o,inputShape:u}=s,l=C.computeConv3DInfo(u,a.shape,o,1,i),c=new N9(l);return n.runWebGLProgram(c,[r,a],"float32")}var P9={kernelName:xg,backendName:"webgl",kernelFunc:O9},z9=lu+`
2022-04-21 15:39:40 +02:00
return cos(x);
2022-05-30 03:12:18 +02:00
`,M9=Ke({opSnippet:z9}),L9={kernelName:Ea,backendName:"webgl",kernelFunc:M9},B9=`
2022-04-21 15:39:40 +02:00
float e2x = exp(-x);
return (e2x + 1.0 / e2x) / 2.0;
2022-05-30 03:12:18 +02:00
`,V9=Ke({opSnippet:B9}),W9={kernelName:Ra,backendName:"webgl",kernelFunc:V9},U9=class{constructor(e,t,n,s,r){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[a,i,o,u]=e,[l]=t,[c,p]=n;this.outputShape=[l,c,p,u];let d=s==="bilinear"?1:0,[h,f]=[`${i-1}.0`,`${o-1}.0`],[m,g,b]=c>1?[`${(i-1)/(c-1)}`,"(y2-y1) * height_ratio",`y1*${h} + float(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${h}`],[y,v,x]=p>1?[`${(o-1)/(p-1)}`,"(x2-x1) * width_ratio",`x1*${f} + float(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${f}`];this.userCode=`
2022-04-21 15:39:40 +02:00
const float height_ratio = float(${m});
const float width_ratio = float(${y});
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int y = coords[1];
int x = coords[2];
int d = coords[3];
// get box vals
float y1 = getBoxes(b,0);
float x1 = getBoxes(b,1);
float y2 = getBoxes(b,2);
float x2 = getBoxes(b,3);
// get image in batch index
int bInd = round(getBoxInd(b));
if(bInd < 0 || bInd >= ${a}) {
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
float height_scale = ${g};
float width_scale = ${v};
float in_y = ${b};
if( in_y < 0.0 || in_y > ${h} ) {
setOutput(float(${r}));
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
float in_x = ${x};
if( in_x < 0.0 || in_x > ${f} ) {
setOutput(float(${r}));
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
vec2 sourceFracIndexCR = vec2(in_x,in_y);
if(${d} == 1) {
// Compute the four integer indices.
ivec2 sourceFloorCR = ivec2(sourceFracIndexCR);
ivec2 sourceCeilCR = ivec2(ceil(sourceFracIndexCR));
float topLeft = getImage(b, sourceFloorCR.y, sourceFloorCR.x, d);
float bottomLeft = getImage(b, sourceCeilCR.y, sourceFloorCR.x, d);
float topRight = getImage(b, sourceFloorCR.y, sourceCeilCR.x, d);
float bottomRight = getImage(b, sourceCeilCR.y, sourceCeilCR.x, d);
vec2 fracCR = sourceFracIndexCR - vec2(sourceFloorCR);
float top = topLeft + (topRight - topLeft) * fracCR.x;
float bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;
float newValue = top + (bottom - top) * fracCR.y;
setOutput(newValue);
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestCR = ivec2(floor(
sourceFracIndexCR + vec2(0.5,0.5)));
float newValue = getImage(b, sourceNearestCR.y, sourceNearestCR.x, d);
setOutput(newValue);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
2022-05-30 03:12:18 +02:00
`}},G9=e=>{let{inputs:t,backend:n,attrs:s}=e,{image:r,boxes:a,boxInd:i}=t,{cropSize:o,method:u,extrapolationValue:l}=s,c=new U9(r.shape,a.shape,o,u,l);return n.runWebGLProgram(c,[r,a,i],"float32")},H9={kernelName:fo,backendName:"webgl",kernelFunc:G9},Iw=class{constructor(e,t,n,s){this.op=e,this.outputShape=t,this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}];let r=this.outputShape.length,a=this.op==="*"?"1.0":"0.0",i=n?a:`getX(${Cw(r,"coords",this.op)})`,o=this.outputShape[this.outputShape.length-1],u="",l="";n?(u=s?`end != ${o-1}`:"end != 0",l=s?"end + 1":"end - 1"):(u=s?`end + pow2 < ${o}`:"end >= pow2",l=s?"end + pow2":"end - pow2"),this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${ot(r)} coords = getOutputCoords();
2022-05-30 03:12:18 +02:00
int end = ${Nw(r,"coords",this.op)};
2022-04-21 15:39:40 +02:00
float val = ${i};
int pow2 = int(pow(2.0, index));
if (${u}) {
int idx = ${l};
2022-05-30 03:12:18 +02:00
${Nw(r,"coords",this.op)} = idx;
val ${this.op}= getX(${Cw(r,"coords",this.op)});
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(val);
2022-02-10 18:27:21 +01:00
}
2022-05-30 03:12:18 +02:00
`}};function Cw(e,t,n){if(e===1)return`${t}`;if(e===2)return`${t}.x, ${t}.y`;if(e===3)return`${t}.x, ${t}.y, ${t}.z`;if(e===4)return`${t}.x, ${t}.y, ${t}.z, ${t}.w`;throw new Error(`Cumulative ${n} for rank ${e} is not yet supported`)}function Nw(e,t,n){if(e===1)return`${t}`;if(e===2)return`${t}.y`;if(e===3)return`${t}.z`;if(e===4)return`${t}.w`;throw new Error(`Cumulative ${n} for rank ${e} is not yet supported`)}function m2(e,t,n,s,r,a){let i=t.shape.length,o=C.getAxesPermutation([s],i),u=t;o!=null&&(u=dn({inputs:{x:t},backend:n,attrs:{perm:o}}));let l=C.getInnerMostAxes(1,i)[0];if(l!==i-1)throw new Error(`WebGL cumprod shader expects an inner-most axis=${t.shape.length-1} but got axis=${s}`);let c=u.shape[l],p=Rn({inputs:{x:u},backend:n});for(let d=0;d<=Math.ceil(Math.log2(c))-1;d++){let h=new Iw(e,u.shape,!1,a),f=[[d]],m=p;p=n.runWebGLProgram(h,[p],p.dtype,f),n.disposeIntermediateTensorInfo(m)}if(r){let d=new Iw(e,u.shape,r,a),h=p;p=n.runWebGLProgram(d,[p],p.dtype),n.disposeIntermediateTensorInfo(h)}if(o!=null){let d=C.getUndoAxesPermutation(o),h=dn({inputs:{x:p},backend:n,attrs:{perm:d}});return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(u),h}return p}function q9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:i,reverse:o}=s;return m2("*",r,n,a,i,o)}var j9={kernelName:ho,backendName:"webgl",kernelFunc:q9};function K9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:i,reverse:o}=s;return m2("+",r,n,a,i,o)}var X9={kernelName:Da,backendName:"webgl",kernelFunc:K9};function Y9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,weights:a}=t,{size:i,binaryOutput:o}=s;if(r.shape.length===1){let u=n.readSync(r.dataId),l=n.readSync(a.dataId),c=j1(u,l,a.dtype,a.shape,i);return n.makeTensorInfo([i],a.dtype,c)}else if(r.shape.length===2){let u=n.bufferSync(r),l=n.bufferSync(a),c=lX(u,l,i,o);return n.makeTensorInfo(c.shape,a.dtype,c.values)}throw new Error(`Error in denseBincount: input must be at most rank 2, but got rank${r.shape.length}.`)}var Q9={kernelName:wg,backendName:"webgl",kernelFunc:Y9},Z9=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int h = ${this.getHeightCoordString()};
int w = ${this.getWidthCoordString()};
int d = ${this.getDepthCoordString()};
int in_h = h / ${t};
int offset_h = imod(h, ${t});
int in_w = w / ${t};
int offset_w = imod(w, ${t});
int offset_d = (offset_h * ${t} + offset_w) *
${this.getOutputDepthSize()};
int in_d = d + offset_d;
float result = ${this.getInputSamplingString()};
setOutput(result);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}getHeightCoordString(){return this.dataFormat==="NHWC"?"coords[1]":"coords[2]"}getWidthCoordString(){return this.dataFormat==="NHWC"?"coords[2]":"coords[3]"}getDepthCoordString(){return this.dataFormat==="NHWC"?"coords[3]":"coords[1]"}getOutputDepthSize(){return this.dataFormat==="NHWC"?this.outputShape[3]:this.outputShape[1]}getInputSamplingString(){return this.dataFormat==="NHWC"?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"}};function J9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockSize:a,dataFormat:i}=s,o=r.shape[0],u=i==="NHWC"?r.shape[1]:r.shape[2],l=i==="NHWC"?r.shape[2]:r.shape[3],c=i==="NHWC"?r.shape[3]:r.shape[1],p=u*a,d=l*a,h=c/(a*a),f=i==="NHWC"?[o,p,d,h]:[o,h,p,d],m=new Z9(f,a,i);return n.runWebGLProgram(m,[r],r.dtype)}var eQ={kernelName:mo,backendName:"webgl",kernelFunc:J9},g2=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=Sn(this.outputShape.length);let a=e.filterHeight,i=e.filterWidth,o=e.outChannels/e.inChannels,u="",l="";n&&(s?u=`float activation(float a) {
2022-04-21 15:39:40 +02:00
float b = getPreluActivationWeightsAtOutCoords();
${n}
}`:r?u=`float activation(float a) {
float b = getLeakyreluAlphaAtOutCoords();
${n}
}`:u=`
float activation(float x) {
${n}
}
`,l="result = activation(result);");let c=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${u}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
int d1 = d2 / ${o};
int q = d2 - d1 * ${o};
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
// TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.
for (int wR = 0; wR < ${a}; wR++) {
int xR = xRCorner + wR * dilations[0];
if (xR < 0 || xR >= inDims[0]) {
continue;
}
for (int wC = 0; wC < ${i}; wC++) {
int xC = xCCorner + wC * dilations[1];
if (xC < 0 || xC >= inDims[1]) {
continue;
}
float xVal = getX(batch, xR, xC, d1);
float wVal = getW(wR, wC, d1, q);
dotProd += xVal * wVal;
}
}
float result = dotProd;
${c}
${l}
setOutput(result);
2022-04-18 17:29:45 +02:00
}
2022-05-30 03:12:18 +02:00
`}},b2=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=Sn(this.outputShape.length);let a=e.outChannels/e.inChannels,i=e.padInfo.left,o=e.strideWidth,u=e.dilationWidth,l=e.filterHeight,c=e.filterWidth,p=c,d=`
2022-04-21 15:39:40 +02:00
int xR; int xC; int xCOffset;
vec4 wTexel; vec4 previous; vec4 final;`;for(let g=0;g<c;g++)d+=`
vec4 xTexelC${g*2};
int xTexelC${g*2}Ready;
vec4 xTexelC${g*2+1};
int xTexelC${g*2+1}Ready;
vec4 xC${g};`;d+=`
for (int r = 0; r < ${l}; r++) {
`;for(let g=0;g<c;g++)d+=`
xTexelC${g*2} = vec4(0.0);
xTexelC${g*2}Ready = 0;
xTexelC${g*2+1} = vec4(0.0);
xTexelC${g*2+1}Ready = 0;
xC${g} = vec4(0.0);`;d+=`
xR = xRCorner + r * dilations[0];
if (xR >=0 && xR < inDims[0]) {
`;for(let g=0;g<(p+1)/2;g++){let b=g*2;if(d+=`
xC = xCCorner + ${b*u};
`,o===1){if(b<c&&(i%2===1?(d+=`
xCOffset = xC + 1;
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
`,u===1&&b>0?d+=`
xC${b} = vec4(xTexelC${b-2}.zw, xTexelC${b}.xy);
`:d+=`
xCOffset = xC + 1 - 2;
if (xCOffset >= 0 && xCOffset < inDims[1]) {
previous = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
previous.zw = vec2(0.0);
}
xC${b} = vec4(previous.zw, xTexelC${b}.xy);
} else {
xC${b} = vec4(0.0, 0.0, xTexelC${b}.xy);
}
`):d+=`
if (xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
if (xC + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
xC${b} = xTexelC${b};
`,b+1<c)){let y=i%2===0?w.nearestLargerEven(u):u;u%2===0&&i%2===1||u%2!==0&&i%2!==1?(d+=`
xCOffset = xC + imod(pads[1], 2) + ${y};
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.0);
}
xTexelC${b+1}Ready = 1;
}
`,u>1&&(d+=`
xCOffset -= 2;
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
xTexelC${b}Ready = 1;
}
`),d+=`
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.xy);
`):y===1?d+=`
xC${b+1} = xTexelC${b};
`:d+=`
xCOffset = xC + ${y};
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.0);
}
xTexelC${b+1}Ready = 1;
}
xC${b+1} = xTexelC${b+1};
`}}else b<c&&(i%2===1?(d+=`
xCOffset = xC + 1 - strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
if(xC + 1 >= 0 && xC + 1 < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xC + 1, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xC + 2 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.0);
}
xTexelC${b+1}Ready = 1;
}
xC${b} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
`,b+1<c&&(d+=`
final = vec4(0.0);
xCOffset = xC + 1 + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1]) {
final = getX(batch, xR, xCOffset, d1);
}
xC${b+1} = vec4(xTexelC${b+1}.xy, final.xy);
`)):(d+=`
if(xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
if (xC + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
xCOffset = xC + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.);
}
xTexelC${b+1}Ready = 1;
}
xC${b} = vec4(
xTexelC${b}.xy, xTexelC${b+1}.xy);
`,b+1<c&&(d+=`
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
`)));b<c&&(d+=`
wTexel = getW(r, ${b}, d1, q);
dotProd += xC${b} * vec4(wTexel.xz, wTexel.xz);
`,b+1<c&&(d+=`
wTexel = getW(r, ${b+1}, d1, q);
dotProd += xC${b+1} * vec4(wTexel.xz, wTexel.xz);
`))}d+=`
}
`,d+=`
}
`;let h="",f="";n&&(s?h=`vec4 activation(vec4 a) {
vec4 b = getPreluActivationWeightsAtOutCoords();
${n}
}`:r?h=`vec4 activation(vec4 a) {
vec4 b = getLeakyreluAlphaAtOutCoords();
${n}
}`:h=`vec4 activation(vec4 x) {
${n}
}`,f="result = activation(result);");let m=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${h}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
int d1 = d2 / ${a};
int q = d2 - d1 * ${a};
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
//intialize dotProd with a small epsilon seems to reduce GPU accuracy loss.
vec4 dotProd = vec4(0.000000000000001);
${d}
vec4 result = dotProd - vec4(0.000000000000001);
${m}
${f}
setOutput(result);
2022-04-18 17:29:45 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function tQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u,dimRoundingMode:l}=s,c=u;c==null&&(c=[1,1]),w.assert(C.eitherStridesOrDilationsAreOne(i,c),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${i} and dilations '${c}'`);let p=C.computeConv2DInfo(r.shape,a.shape,i,c,o,l,!0),d;K().getBool("WEBGL_PACK_DEPTHWISECONV")&&p.strideWidth<=2&&p.outChannels/p.inChannels===1?d=new b2(p):d=new g2(p);let h=[[p.padInfo.top,p.padInfo.left],[p.strideHeight,p.strideWidth],[p.dilationHeight,p.dilationWidth],[p.inHeight,p.inWidth]];return n.runWebGLProgram(d,[r,a],"float32",h)}var nQ={kernelName:Fa,backendName:"webgl",kernelFunc:tQ},sQ=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,s=e.padInfo.top,r=e.padInfo.left,a=e.outChannels/e.inChannels;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int wR = coords.x;
int wC = coords.y;
int d1 = coords.z;
int dm = coords.w;
int d2 = d1 * ${a} + dm;
float dotProd = 0.0;
// TO DO: Vec4 over the batch size
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${t} - ${s};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${n} - ${r};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float dyValue = getDy(b, yR, yC, d2);
float xValue = getX(b, xR, xC, d1);
dotProd += (xValue * dyValue);
}
}
}
setOutput(dotProd);
2022-04-18 18:26:05 +02:00
}
2022-05-30 03:12:18 +02:00
`}},rQ=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,s=e.strideHeight,r=e.strideWidth,a=t-1-e.padInfo.top,i=n-1-e.padInfo.left,o=e.outChannels/e.inChannels;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec2 pads = ivec2(${a}, ${i});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d1 = coords[3];
ivec2 dyCorner = coords.yz - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
float dotProd = 0.0;
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / ${s}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
int wRPerm = ${t} - 1 - wR;
for (int wC = 0; wC < ${n}; wC++) {
float dyC = float(dyCCorner + wC) / ${r}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
int wCPerm = ${n} - 1 - wC;
// TO DO: Vec4 over the channelMul
for (int dm = 0; dm < ${o}; dm++) {
int d2 = d1 * ${o} + dm;
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, dm);
dotProd += xValue * wValue;
}
}
}
setOutput(dotProd);
2022-02-10 18:27:21 +01:00
}
2022-05-30 03:12:18 +02:00
`}};function aQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:i,dilations:o,pad:u,dimRoundingMode:l,filterShape:c}=s,p=C.computeConv2DInfo(r.shape,c,i,o,u,l,!0),d=new sQ(p);return n.runWebGLProgram(d,[r,a],"float32")}var iQ={kernelName:kg,backendName:"webgl",kernelFunc:aQ};function oQ(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{strides:i,dilations:o,pad:u,dimRoundingMode:l,inputShape:c}=s,p=C.computeConv2DInfo(c,a.shape,i,o,u,l,!0),d=new rQ(p);return n.runWebGLProgram(d,[r,a],"float32")}var uQ={kernelName:Sg,backendName:"webgl",kernelFunc:oQ},lQ=class{constructor(e){this.variableNames=["X"],this.outputShape=[e,e],this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
float val = coords[0] == coords[1] ? getX(coords[0]) : 0.0;
setOutput(val);
2022-02-10 18:27:21 +01:00
}
2022-05-30 03:12:18 +02:00
`}};function cQ(e){let{inputs:t,backend:n}=e,{x:s}=t,r=[...s.shape,...s.shape],a=w.sizeFromShape(s.shape),i=pe({inputs:{x:s},backend:n,attrs:{shape:[a]}}),o=new lQ(a),u=n.runWebGLProgram(o,[i],i.dtype),l=pe({inputs:{x:u},backend:n,attrs:{shape:r}});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(u),l}var dQ={kernelName:Ig,backendName:"webgl",kernelFunc:cQ},pQ=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:s,strideHeight:r,strideWidth:a,filterHeight:i,filterWidth:o,dilationHeight:u,dilationWidth:l}=e,{top:c,left:p}=s;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec2 strides = ivec2(${r}, ${a});
const ivec2 pads = ivec2(${c}, ${p});
const float neg_infinity = -3.4e38;
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.w;
ivec2 outTopLeftCorner =
coords.yz * strides - pads;
int hBeg = outTopLeftCorner.x;
int wBeg = outTopLeftCorner.y;
float curVal = neg_infinity;
for (int h = 0; h < ${i}; h++) {
int hIn = hBeg + h * ${u};
if (hIn >= 0 && hIn < ${t}) {
for (int w = 0; w < ${o}; w++) {
int wIn = wBeg + w * ${l};
if (wIn >= 0 && wIn < ${n}) {
float xVal = getX(batch, hIn, wIn, d1);
float wVal = getW(h, w, d1);
float val = xVal + wVal;
if (val > curVal) {
curVal = val;
}
}
}
}
}
float result = curVal;
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}};function hQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u}=s,l=C.computeDilation2DInfo(r.shape,a.shape,i,o,"NHWC",u),c,p=new pQ(l);c=n.runWebGLProgram(p,[r,a],"float32");let d=pe({inputs:{x:c},backend:n,attrs:{shape:l.outShape}});return n.disposeIntermediateTensorInfo(c),d}var fQ={kernelName:rp,backendName:"webgl",kernelFunc:hQ};function mQ(e){let{inputs:t,backend:n,attrs:s}=e,{equation:r}=s,a=t,{allDims:i,summedDims:o,idDims:u}=C.decodeEinsumEquation(r,a.length);C.checkEinsumDimSizes(i.length,u,a);let{path:l,steps:c}=C.getEinsumComputePath(o,u),p=c.length,d=null,h=i.length,f=[];for(let m=0;m<p;++m){for(let g of c[m]){let{permutationIndices:b,expandDims:y}=C.getEinsumPermutation(h,u[g]),v;C.isIdentityPermutation(b)?v=a[g]:(v=dn({inputs:{x:a[g]},backend:n,attrs:{perm:b}}),f.push(v));let x=v.shape.slice();for(let k=0;k<y.length;++k)x.splice(y[k],0,1);w.arraysEqual(v.shape,x)||(v=pe({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),d===null?d=v:(d=Av({inputs:{a:v,b:d},backend:n}),f.push(d))}m<p-1&&(l[m]>=0&&(d=ah({inputs:{x:d},backend:n,attrs:{axis:l[m]-(i.length-h),keepDims:!1}}),f.push(d)),h--)}for(let m of f)m!==d&&n.disposeIntermediateTensorInfo(m);return d}var gQ={kernelName:ap,backendName:"webgl",kernelFunc:mQ},bQ="return (x >= 0.0) ? x : (exp(x) - 1.0);",yQ=`
2022-04-21 15:39:40 +02:00
vec4 result;
result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);
return result;
2022-05-30 03:12:18 +02:00
`,vQ=Ke({opSnippet:bQ,packedOpSnippet:yQ}),xQ={kernelName:Pa,backendName:"webgl",kernelFunc:vQ},wQ="return (b >= 1.0) ? a : a * (b + 1.0);",kQ=`
2022-04-21 15:39:40 +02:00
vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));
return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));
2022-05-30 03:12:18 +02:00
`,SQ=e=>{let{inputs:t,backend:n}=e,{dy:s,y:r}=t,a=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new tc(kQ,s.shape,r.shape):new oo(wQ,s.shape,r.shape);return n.runWebGLProgram(a,[s,r],s.dtype)},IQ={kernelName:Cg,backendName:"webgl",kernelFunc:SQ},CQ=`
2022-04-21 15:39:40 +02:00
return vec4(equal(a, b));
2022-05-30 03:12:18 +02:00
`,NQ="return float(a == b);",TQ=qt({opSnippet:NQ,packedOpSnippet:CQ,dtype:"bool",cpuKernelImpl:pX}),$Q={kernelName:go,backendName:"webgl",kernelFunc:TQ},_Q=`
2022-04-21 15:39:40 +02:00
// Error function is calculated approximately with elementary function.
// See "Handbook of Mathematical Functions with Formulas,
// Graphs, and Mathematical Tables", Abramowitz and Stegun.
float p = ${C.ERF_P};
float a1 = ${C.ERF_A1};
float a2 = ${C.ERF_A2};
float a3 = ${C.ERF_A3};
float a4 = ${C.ERF_A4};
float a5 = ${C.ERF_A5};
float sign = sign(x);
x = abs(x);
float t = 1.0 / (1.0 + p * x);
return sign * (1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x));
2022-05-30 03:12:18 +02:00
`,AQ=Ke({opSnippet:_Q}),EQ={kernelName:yl,backendName:"webgl",kernelFunc:AQ},RQ=lu+`
2022-04-21 15:39:40 +02:00
return exp(x);
2022-05-30 03:12:18 +02:00
`,DQ=`
2022-04-21 15:39:40 +02:00
vec4 result = exp(x);
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`,y2=Ke({opSnippet:RQ,packedOpSnippet:DQ,cpuKernelImpl:hX,dtype:"float32"}),FQ={kernelName:za,backendName:"webgl",kernelFunc:y2};function tg(e){let{inputs:t,attrs:n,backend:s}=e,{dim:r}=n,{input:a}=t,i=a.shape.length,o=a.shape.slice(),u=r;return r<0&&(w.assert(-(i+1)<=r,()=>`Axis must be in the interval [${-(i+1)}, ${i}]`),u=i+r+1),o.splice(u,0,1),pe({inputs:{x:a},backend:s,attrs:{shape:o}})}var OQ={kernelName:bo,backendName:"webgl",kernelFunc:tg},Tw="return exp(x) - 1.0;",PQ=Ke({opSnippet:Tw,packedOpSnippet:Tw,cpuKernelImpl:fX}),zQ={kernelName:yo,backendName:"webgl",kernelFunc:PQ},$w=class{constructor(e,t,n){this.variableNames=["real","imag"];let s=t[1];this.outputShape=t;let r=n?`2.0 * ${Math.PI}`:`-2.0 * ${Math.PI}`,a=n?`${s}.0`:"1.0",i;if(e==="real")i="return real * expR - imag * expI;";else if(e==="imag")i="return real * expI + imag * expR;";else throw new Error(`FFT component must be either "real" or "imag", got ${e}.`);this.userCode=`
2022-04-21 15:39:40 +02:00
const float exponentMultiplier = ${r};
float unaryOpComplex(float real, float expR, float imag, float expI) {
${i}
}
float mulMatDFT(int batch, int index) {
float indexRatio = float(index) / float(${s});
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio;
float result = 0.0;
for (int i = 0; i < ${s}; i++) {
// x = (-2|2 * PI / N) * index * i;
float x = exponentMultiplierTimesIndexRatio * float(i);
float expR = cos(x);
float expI = sin(x);
float real = getReal(batch, i);
float imag = getImag(batch, i);
result +=
unaryOpComplex(real, expR, imag, expI) / ${a};
}
return result;
}
void main() {
ivec2 coords = getOutputCoords();
setOutput(mulMatDFT(coords[0], coords[1]));
}
2022-05-30 03:12:18 +02:00
`}};function v2(e,t,n){let s=n.texData.get(e.dataId),r=w.sizeFromShape(e.shape),a=e.shape[e.shape.length-1],i=r/a,o=pe({inputs:{x:e},backend:n,attrs:{shape:[i,a]}}),u=o.shape,l=new $w("real",u,t),c=new $w("imag",u,t),p=[{dataId:s.complexTensorInfos.real.dataId,dtype:s.complexTensorInfos.real.dtype,shape:u},{dataId:s.complexTensorInfos.imag.dataId,dtype:s.complexTensorInfos.imag.dtype,shape:u}],d=n.runWebGLProgram(l,p,"float32"),h=n.runWebGLProgram(c,p,"float32"),f=Dr({inputs:{real:d,imag:h},backend:n});n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h);let m=pe({inputs:{x:f},backend:n,attrs:{shape:e.shape}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(f),m}function MQ(e){let{inputs:t,backend:n}=e,{input:s}=t;return v2(s,!1,n)}var LQ={kernelName:Ng,backendName:"webgl",kernelFunc:MQ},BQ=class{constructor(e,t){this.outputShape=[],this.customUniforms=[{name:"value",type:"float"}],this.variableNames=["x"],this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
// Input can be obtained from uniform value.
setOutput(value);
}
2022-05-30 03:12:18 +02:00
`}};function sc(e){let{backend:t,attrs:n}=e,{shape:s,value:r}=n,{dtype:a}=n;if(a=a||w.inferDtype(r),a==="string"){let i=w.getArrayFromDType(a,w.sizeFromShape(s));return i.fill(r),t.makeTensorInfo(s,a,i)}else{let i=new BQ(s,r),o=[[r]];return t.runWebGLProgram(i,[],a,o)}}var VQ={kernelName:vl,backendName:"webgl",kernelFunc:sc},WQ=class{constructor(e){this.variableNames=["Image"],this.outputShape=[];let t=e[2];this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int x = coords[2];
int coordX = ${t} - x - 1;
float outputValue;
if(coordX >= 0 && coordX < ${t}) {
outputValue = getImage(coords[0], coords[1], coordX, coords[3]);
} else {
outputValue = getImage(coords[0], coords[1], coords[2], coords[3]);
}
setOutput(outputValue);
}
2022-05-30 03:12:18 +02:00
`}},UQ={kernelName:vo,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,s=t,r=new WQ(n.shape);return s.runWebGLProgram(r,[n],n.dtype)}},_w="return floor(x);",GQ=Ke({opSnippet:_w,packedOpSnippet:_w,cpuKernelImpl:mX}),HQ={kernelName:Ma,backendName:"webgl",kernelFunc:GQ},qQ=`
2022-04-21 15:39:40 +02:00
float s = sign(a) * sign(b);
int ia = round(a);
int ib = round(b);
if (ib != 0) {
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
return float(idiv(ia, ib, s));
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
return NAN;
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`,jQ=`
2022-04-21 15:39:40 +02:00
ivec4 ia = round(a);
ivec4 ib = round(b);
bvec4 cond = notEqual(ib, ivec4(0));
ivec4 result = ivec4(0);
vec4 s = sign(a) * sign(b);
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
if (cond[0]) {
result[0] = idiv(ia[0], ib[0], s[0]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (cond[1]) {
result[1] = idiv(ia[1], ib[1], s[1]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (cond[2]) {
result[2] = idiv(ia[2], ib[2], s[2]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (cond[3]) {
result[3] = idiv(ia[3], ib[3], s[3]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return vec4(result);
2022-05-30 03:12:18 +02:00
`,KQ=qt({opSnippet:qQ,packedOpSnippet:jQ,dtype:"int32"}),XQ={kernelName:La,backendName:"webgl",kernelFunc:KQ},YQ=class{constructor(e){this.variableNames=["A"];let t=fn(),[n,s]=e;this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec3 coords = getOutputCoords();
int texR = coords[0];
int texC = coords[1];
int depth = coords[2];
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${s}.0, ${n}.0);
vec4 values = ${t.texture2D}(A, uv);
float value;
if (depth == 0) {
value = values.r;
} else if (depth == 1) {
value = values.g;
} else if (depth == 2) {
value = values.b;
} else if (depth == 3) {
value = values.a;
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(floor(value * 255.0 + 0.5));
2022-02-10 18:27:21 +01:00
}
2022-05-30 03:12:18 +02:00
`}},QQ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=fn(),[n,s]=e;this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec3 coords = getOutputCoords();
int texR = coords[0];
int texC = coords[1];
int depth = coords[2];
vec4 result = vec4(0.);
for(int row=0; row<=1; row++) {
for(int col=0; col<=1; col++) {
texC = coords[1] + row;
depth = coords[2] + col;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${s}.0, ${n}.0);
vec4 values = ${t.texture2D}(A, uv);
float value;
if (depth == 0) {
value = values.r;
} else if (depth == 1) {
value = values.g;
} else if (depth == 2) {
value = values.b;
} else if (depth == 3) {
value = values.a;
}
result[row * 2 + col] = floor(value * 255.0 + 0.5);
}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
${t.output} = result;
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},ZQ={kernelName:vd,backendName:"webgl",kernelFunc:JQ},Bi;function JQ(e){let{inputs:t,backend:n,attrs:s}=e,{pixels:r}=t,{numChannels:a}=s,i=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,o=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,[u,l]=i?[r.videoWidth,r.videoHeight]:[r.width,r.height],c=[l,u],p=[l,u,a];(o||i)&&(Bi==null&&(Bi=document.createElement("canvas").getContext("2d")),Bi.canvas.width=u,Bi.canvas.height=l,Bi.drawImage(r,0,0,u,l),r=Bi.canvas);let d=n.makeTensorInfo(c,"int32");n.texData.get(d.dataId).usage=2,n.gpgpu.uploadPixelDataToTexture(n.getTexture(d.dataId),r);let h=K().getBool("WEBGL_PACK")?new QQ(p):new YQ(p),f=n.runWebGLProgram(h,[d],"int32");return n.disposeData(d.dataId),f}function eZ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dataFormat:c,dilations:p,dimRoundingMode:d,activation:h,leakyreluAlpha:f}=s,m=C.convertConv2DDataFormat(c),g=C.computeConv2DInfo(r.shape,a.shape,u,p,l,d,!1,m),b,y=[];if(g.filterHeight===1&&g.filterWidth===1&&g.dilationHeight===1&&g.dilationWidth===1&&g.strideHeight===1&&g.strideWidth===1&&(g.padInfo.type==="SAME"||g.padInfo.type==="VALID"))b=h2({x:r,filter:a,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:f});else if(K().getBool("WEBGL_CONV_IM2COL"))b=f2({x:r,filter:a,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:f});else{let x=i!=null,k=o!=null,I=h==="leakyrelu",$=h?sh(h,!1):null,R=new p2(g,x,$,k,I),E=[r,a],P=(A,O)=>{if(O==="NCHW"&&A.shape.length===1&&A.shape[0]!==1){let T=pe({inputs:{x:A},backend:n,attrs:{shape:[A.shape[0],1,1]}});return y.push(T),T}return A};if(x&&E.push(P(i,c)),k&&E.push(P(o,c)),I){let A=n.makeTensorInfo([],"float32",w.createScalarValue(f,"float32"));E.push(A),y.push(A)}b=n.runWebGLProgram(R,E,"float32")}let v=pe({inputs:{x:b},backend:n,attrs:{shape:g.outShape}});return y.push(b),y.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var tZ={kernelName:oa,backendName:"webgl",kernelFunc:eZ};function nZ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:p,activation:d,leakyreluAlpha:h}=s,f=[],m=c;m==null&&(m=[1,1]),w.assert(C.eitherStridesOrDilationsAreOne(u,m),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${m}'`);let g=C.computeConv2DInfo(r.shape,a.shape,u,m,l,p,!0),b=K().getBool("WEBGL_PACK_DEPTHWISECONV")&&g.strideWidth<=2&&g.outChannels/g.inChannels===1,y=d?sh(d,b):null,v=[r,a],x=i!=null,k=o!=null,I=d==="leakyrelu";if(x&&v.push(i),k&&v.push(o),I){let P=n.makeTensorInfo([],"float32",w.createScalarValue(h,"float32"));v.push(P),f.push(P)}let $;b?$=new b2(g,x,y,k,I):$=new g2(g,x,y,k,I);let R=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]],E=n.runWebGLProgram($,v,"float32",R);return f.forEach(P=>n.disposeIntermediateTensorInfo(P)),E}var sZ={kernelName:ua,backendName:"webgl",kernelFunc:nZ},rZ=class{constructor(e,t,n){this.sliceDim=e,this.strides=t,this.variableNames=["x","indices"],this.outputShape=n;let s=ot(t.length),r=ot(n.length),a=this.sliceDim>1?"strides[j]":"strides";this.userCode=`
2022-04-21 15:39:40 +02:00
${s} strides = ${s}(${this.strides});
void main() {
${r} coords = getOutputCoords();
int flattenIndex = 0;
for (int j = 0; j < ${this.sliceDim}; j++) {
int index = round(getIndices(coords[0], j));
flattenIndex += index * ${a};
}
setOutput(getX(flattenIndex, coords[1]));
2022-04-18 18:26:05 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function aZ(e){let{inputs:t,backend:n}=e,{params:s,indices:r}=t,a=r.shape,i=a[a.length-1],o=w.sizeFromShape(s.shape),[u,l,c,p]=C.prepareAndValidate(s,r),d=pe({inputs:{x:r},backend:n,attrs:{shape:[l,i]}}),h=pe({inputs:{x:s},backend:n,attrs:{shape:[w.sizeFromShape(s.shape)/c,c]}});if(n.shouldExecuteOnCPU([s,r])||s.dtype==="string"){let b=n.readSync(r.dataId),y=n.bufferSync(s),v=gX(b,y,s.dtype,l,i,c,p,s.shape,o);return n.makeTensorInfo(u,s.dtype,v.values)}let f=new rZ(i,p,[l,c]),m=n.runWebGLProgram(f,[h,d],h.dtype),g=pe({inputs:{x:m},backend:n,attrs:{shape:u}});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(m),g}var iZ={kernelName:wo,backendName:"webgl",kernelFunc:aZ},oZ=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=ot(this.rank),s=uZ(e,2);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${n} resRC = getOutputCoords();
int index = int(getIndices(resRC.x, resRC.z));
float inBounds = (index >= 0) && (index < ${e[2]}) ? 1.0 : 0.0;
setOutput(inBounds * getA(${s}));
2022-02-10 18:27:21 +01:00
}
2022-05-30 03:12:18 +02:00
`}};function uZ(e,t){let n=["resRC.x","resRC.y","resRC.z","resRC.w"],s=[];for(let r=0;r<e.length;r++)r===2?s.push("index"):s.push(`${n[r]}`);return s.join()}function x2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,indices:a}=t,{axis:i,batchDims:o}=s,u=w.parseAxisParam(i,r.shape)[0];if(K().get("DEBUG")){let y=n.readSync(a.dataId),v=r.shape[u];for(let x=0;x<y.length;++x){let k=y[x];w.assert(k<=v-1&&k>=0,()=>`GatherV2: the index value ${k} is not in [0, ${v-1}]`)}}let l=C.segment_util.collectGatherOpShapeInfo(r,a,u,o),c=w.sizeFromShape(a.shape),p=[],d=pe({inputs:{x:r},backend:n,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),h=pe({inputs:{x:a},backend:n,attrs:{shape:[l.batchSize,c/l.batchSize]}});p.push(d),p.push(h);let f=[l.batchSize,l.outerSize,c/l.batchSize,l.sliceSize];if(n.shouldExecuteOnCPU([r,a])||r.dtype==="string"){let y=n.bufferSync(h),v=n.bufferSync(d),x=bX(v,y,f);return p.forEach(k=>n.disposeIntermediateTensorInfo(k)),n.makeTensorInfo(l.outputShape,x.dtype,x.values)}let m=new oZ(d.shape,f),g=n.runWebGLProgram(m,[d,h],d.dtype);p.push(g);let b=pe({inputs:{x:g},backend:n,attrs:{shape:l.outputShape}});return p.forEach(y=>n.disposeIntermediateTensorInfo(y)),b}var lZ={kernelName:xo,backendName:"webgl",kernelFunc:x2},cZ="return float(a > b);",dZ=`
2022-04-21 15:39:40 +02:00
return vec4(greaterThan(a, b));
2022-05-30 03:12:18 +02:00
`,pZ=qt({opSnippet:cZ,packedOpSnippet:dZ,cpuKernelImpl:yX,dtype:"bool"}),hZ={kernelName:ko,backendName:"webgl",kernelFunc:pZ},fZ="return float(a >= b);",mZ=`
2022-04-21 15:39:40 +02:00
return vec4(greaterThanEqual(a, b));
2022-05-30 03:12:18 +02:00
`,gZ=qt({opSnippet:fZ,packedOpSnippet:mZ,dtype:"bool",cpuKernelImpl:vX}),bZ={kernelName:Va,backendName:"webgl",kernelFunc:gZ};function yZ(e){let{inputs:t,backend:n}=e,{input:s}=t;return v2(s,!0,n)}var vZ={kernelName:Tg,backendName:"webgl",kernelFunc:yZ},xZ="return float(!isnan(x) && !isinf(x));",wZ=Ke({opSnippet:xZ,dtype:"bool"}),kZ={kernelName:xl,backendName:"webgl",kernelFunc:wZ},SZ="return float(isinf(x));",IZ=Ke({opSnippet:SZ,dtype:"bool"}),CZ={kernelName:wl,backendName:"webgl",kernelFunc:IZ},NZ="return float(isnan(x));",TZ=Ke({opSnippet:NZ,dtype:"bool"}),$Z={kernelName:kl,backendName:"webgl",kernelFunc:TZ},_Z="return float(a < b);",AZ=`
2022-04-21 15:39:40 +02:00
return vec4(lessThan(a, b));
2022-05-30 03:12:18 +02:00
`,EZ=qt({opSnippet:_Z,packedOpSnippet:AZ,cpuKernelImpl:xX,dtype:"bool"}),RZ={kernelName:So,backendName:"webgl",kernelFunc:EZ},DZ="return float(a <= b);",FZ=`
2022-04-21 15:39:40 +02:00
return vec4(lessThanEqual(a, b));
2022-05-30 03:12:18 +02:00
`,OZ=qt({opSnippet:DZ,packedOpSnippet:FZ,cpuKernelImpl:wX,dtype:"bool"}),PZ={kernelName:Io,backendName:"webgl",kernelFunc:OZ};function zZ(e){let{backend:t,attrs:n}=e,{start:s,stop:r,num:a}=n,i=kX(s,r,a);return t.makeTensorInfo([i.length],"float32",i)}var MZ={kernelName:$g,backendName:"webgl",kernelFunc:zZ},LZ=lu+`
2022-04-21 15:39:40 +02:00
return x < 0.0 ? 0./0. : log(x);
2022-05-30 03:12:18 +02:00
`,BZ=`
2022-04-21 15:39:40 +02:00
vec4 result = log(x);
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : (x.r < 0.0 ? 0./0. : result.r);
result.g = isNaN.g ? x.g : (x.g < 0.0 ? 0./0. : result.g);
result.b = isNaN.b ? x.b : (x.b < 0.0 ? 0./0. : result.b);
result.a = isNaN.a ? x.a : (x.a < 0.0 ? 0./0. : result.a);
return result;
2022-05-30 03:12:18 +02:00
`,VZ=Ke({opSnippet:LZ,packedOpSnippet:BZ,cpuKernelImpl:SX}),WZ={kernelName:Ga,backendName:"webgl",kernelFunc:VZ},UZ=lu+`
2022-04-21 15:39:40 +02:00
return log(1.0 + x);
2022-05-30 03:12:18 +02:00
`,GZ=Ke({opSnippet:UZ}),HZ={kernelName:Sl,backendName:"webgl",kernelFunc:GZ},qZ="return float(a >= 1.0 && b >= 1.0);",jZ=`
2022-04-21 15:39:40 +02:00
return vec4(
vec4(greaterThanEqual(a, vec4(1.0))) *
vec4(greaterThanEqual(b, vec4(1.0))));
2022-05-30 03:12:18 +02:00
`,KZ=qt({opSnippet:qZ,packedOpSnippet:jZ,dtype:"bool"}),XZ={kernelName:Co,backendName:"webgl",kernelFunc:KZ},YZ="return float(!(x >= 1.0));",QZ=Ke({opSnippet:YZ}),ZZ={kernelName:Il,backendName:"webgl",kernelFunc:QZ},JZ="return float(a >= 1.0 || b >= 1.0);",e7=`
2022-04-21 15:39:40 +02:00
return min(
vec4(greaterThanEqual(a, vec4(1.0))) +
vec4(greaterThanEqual(b, vec4(1.0))),
vec4(1.0));
2022-05-30 03:12:18 +02:00
`,t7=qt({opSnippet:JZ,packedOpSnippet:e7,dtype:"bool"}),n7={kernelName:op,backendName:"webgl",kernelFunc:t7},s7=class{constructor(e,t,n,s,r){this.variableNames=["x"],this.outputShape=[];let a=t,i=e[3]-1;this.outputShape=e;let o,u=`float(${n}) + float(${s}) * sum`;r===.5?o=`inversesqrt(${u})`:r===1?o=`1.0/(${u})`:o=`exp(log(${u}) * float(-${r}));`,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
int d = coords[3];
float x = getX(b, r, c, d);
float sum = 0.0;
for (int j = -${a}; j <= ${a}; j++) {
int idx = d + j;
if (idx >= 0 && idx <= ${i}) {
float z = getX(b, r, c, idx);
sum += z * z;
}
}
float val = x * ${o};
setOutput(val);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},r7=class{constructor(e,t,n,s,r){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let a=t,i=e[3]-1;this.outputShape=e;let o,u=`float(${n}) + float(${s}) * sum`;r===.5?o=`inversesqrt(${u})`:r===1?o=`1.0/(${u})`:o=`exp(log(${u}) * float(-${r}));`,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords.x;
int r = coords.y;
int c = coords.z;
int d = coords.w;
bool hasNextCol = d < ${this.outputShape[3]};
bool hasNextRow = c < ${this.outputShape[2]};
vec4 sum = vec4(0.);
vec4 xFragAtOutputCoords = getX(b, r, c, d);
vec4 xAtOutputCoords = vec4(
getChannel(xFragAtOutputCoords, vec2(c, d)),
hasNextCol ?
getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,
hasNextRow ?
getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,
(hasNextRow && hasNextCol) ?
getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0
);
int firstChannel = d - ${a};
vec2 cache = vec2(0.);
if(firstChannel >= 0){
vec4 firstChannelFrag = getX(b, r, c, firstChannel);
cache.x = getChannel(firstChannelFrag, vec2(c, firstChannel));
if(hasNextRow){
cache.y = getChannel(firstChannelFrag, vec2(c + 1, firstChannel));
}
}
ivec2 depth = ivec2(d, d + 1);
for (int j = - ${a}; j <= ${a}; j++) {
ivec2 idx = depth + j;
bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));
bvec2 belowUpperBound = lessThanEqual(idx, ivec2(${i}));
bool depthInRange = aboveLowerBound.x && belowUpperBound.x;
bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;
if(depthInRange || depthPlusOneInRange){
vec4 z = vec4(0.);
vec4 xFragAtCurrentDepth;
z.xz = cache.xy;
if(depthPlusOneInRange && hasNextCol){
xFragAtCurrentDepth = idx.y != d ?
getX(b, r, c, idx.y) : xFragAtOutputCoords;
z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));
if(hasNextRow){
z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));
}
}
cache.xy = z.yw;
sum += z * z;
}
}
vec4 result = xAtOutputCoords * ${o};
setOutput(result);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},a7=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{depthRadius:a,bias:i,alpha:o,beta:u}=s,l=K().getBool("WEBGL_PACK_NORMALIZATION")?new r7(r.shape,a,i,o,u):new s7(r.shape,a,i,o,u);return n.runWebGLProgram(l,[r],r.dtype)},i7={kernelName:up,backendName:"webgl",kernelFunc:a7},o7=class{constructor(e,t,n,s,r){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=s,this.beta=r,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
float result = 0.0;
for (int d = 0; d < ${this.depth}; ++d) {
int depthBegin = int(max(0.0, float(d - ${t})));
int depthEnd = int(min(float(${this.depth}),
float(d + ${t} + 1)));
const int MIN_DEPTH_BEGIN = 0;
const int MAX_DEPTH_END = ${this.depth};
float norm = 0.0;
for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {
if (k < depthBegin){
continue;
}
else if (k >= depthBegin && k < depthEnd) {
norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);
}
else {
break;
}
}
norm = float(${s}) * norm + float(${n});
for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){
if (k < depthBegin){
continue;
}
else if (k >= depthBegin && k < depthEnd){
float dyi = -2.0 * float(${s})
* float(${r})
* getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)
/ norm;
if (k == d) {
dyi += pow(norm, -1.0 * ${r});
}
if (k == coords[3]) {
dyi *= getDy(b, r, c, d);
result += dyi;
}
}
else {
break;
}
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(result);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},u7=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r,y:a,dy:i}=t,{depthRadius:o,bias:u,alpha:l,beta:c}=s,p=new o7(r.shape,o,u,l,c);return n.runWebGLProgram(p,[r,a,i],r.dtype)},l7={kernelName:_g,backendName:"webgl",kernelFunc:u7};function c7(e,t,n,s){let r=w.sizeFromShape(t),i=w.sizeFromShape(e.shape)/r,o=pe({inputs:{x:e},attrs:{shape:[i,r]},backend:s}),u=ki(o,e.dtype,"max",s),l=pe({inputs:{x:u},attrs:{shape:n},backend:s});return s.disposeIntermediateTensorInfo(o),s.disposeIntermediateTensorInfo(u),l}function w2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reductionIndices:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,o),p=c!=null,d=n.shouldExecuteOnCPU([r]),h=r;if(p){if(d){let v=n.texData.get(h.dataId).values,x=new Array(o);for(let $=0;$<x.length;$++)x[$]=r.shape[c[$]];let k=_v(v,r.shape,r.dtype,c,x);h=n.makeTensorInfo(x,r.dtype);let I=n.texData.get(h.dataId);I.values=k}else h=rh(r,c,n);l=C.getInnerMostAxes(l.length,o)}C.assertAxesAreInnerMostDims("max",l,o);let[f,m]=C.computeOutAndReduceShapes(h.shape,l),g=f;i&&(g=C.expandShapeToKeepDim(f,u));let b;if(d){let v=n.texData.get(h.dataId).values,x=IX(v,w.sizeFromShape(m),g,r.dtype);b=n.makeTensorInfo(g,r.dtype);let k=n.texData.get(b.dataId);k.values=x}else b=c7(h,m,g,n);return p&&n.disposeIntermediateTensorInfo(h),b}var d7={kernelName:Ha,backendName:"webgl",kernelFunc:w2},p7=e2+`
2022-04-21 15:39:40 +02:00
return max(a, b);
2022-05-30 03:12:18 +02:00
`,h7=`
2022-04-21 15:39:40 +02:00
vec4 result = vec4(max(a, b));
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
2022-05-30 03:12:18 +02:00
`+nh+`
2022-04-21 15:39:40 +02:00
return result;
2022-05-30 03:12:18 +02:00
`,f7=qt({opSnippet:p7,packedOpSnippet:h7,cpuKernelImpl:CX}),m7={kernelName:qa,backendName:"webgl",kernelFunc:f7};function g7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t;ru(r,"maxPool");let{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1;w.assert(C.eitherStridesOrDilationsAreOne(i,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${i} and dilations '${l}'`);let c=C.computePool2DInfo(r.shape,a,i,l,o,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Rn({inputs:{x:r},backend:n});let p=new al(c,"max",!1);return n.runWebGLProgram(p,[r],r.dtype)}var b7={kernelName:ja,backendName:"webgl",kernelFunc:g7};function y7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dataFormat:u,dimRoundingMode:l}=s,c=[1,1,1],p=C.computePool3DInfo(r.shape,a,i,c,o,l,u),d=new Ev(p,"max",!1);return n.runWebGLProgram(d,[r],r.dtype)}var v7={kernelName:lp,backendName:"webgl",kernelFunc:y7},x7=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideHeight,n=e.strideWidth,s=e.dilationHeight,r=e.effectiveFilterHeight,a=e.effectiveFilterWidth,i=r-1-e.padInfo.top,o=a-1-e.padInfo.left,u=r*a-1;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec2 pads = ivec2(${i}, ${o});
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${r};
wR += ${s}) {
float dyR = float(dyRCorner + wR) / ${t}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
for (int wC = 0; wC < ${a}; wC++) {
float dyC = float(dyCCorner + wC) / ${n}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
int maxPosValue = ${u} - int(getMaxPos(b, idyR, idyC, d));
// Get the current value, check it against the value from the
// position matrix.
int curPosValue = wR * ${a} + wC;
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
dotProd += dyValue * mask;
}
}
setOutput(dotProd);
2022-04-01 15:12:04 +02:00
}
2022-05-30 03:12:18 +02:00
`}},w7=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideDepth,n=e.strideHeight,s=e.strideWidth,r=e.dilationDepth,a=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterDepth,u=e.effectiveFilterHeight,l=e.effectiveFilterWidth,c=o-1-e.padInfo.front,p=u-1-e.padInfo.top,d=l-1-e.padInfo.left,h=o*u*l-1;this.userCode=`
2022-04-21 15:39:40 +02:00
const ivec3 pads = ivec3(${c}, ${p}, ${d});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyDCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
// Convolve dy(?, ?, ?, ch) with pos mask(:, :, :, d) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wD = 0; wD < ${o};
wD += ${r}) {
float dyD = float(dyDCorner + wD) / ${t}.0;
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
continue;
}
int idyD = int(dyD);
for (int wR = 0; wR < ${u};
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${n}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
for (int wC = 0; wC < ${l};
wC += ${i}) {
float dyC = float(dyCCorner + wC) / ${s}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
int maxPosValue = ${h} -
int(getMaxPos(batch, idyD, idyR, idyC, ch));
// Get the current value, check it against the value from the
// position matrix.
int curPosValue =
wD * ${u} * ${l} +
wR * ${l} + wC;
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
dotProd += dyValue * mask;
}
}
}
setOutput(dotProd);
2022-04-01 15:13:32 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function k7(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,i=a,{filterSize:o,strides:u,pad:l,dimRoundingMode:c}=s,p=[1,1,1],d=C.computePool3DInfo(i.shape,o,u,p,l,c),h=new Ev(d,"max",!0),f=n.runWebGLProgram(h,[i],i.dtype),m=new w7(d),g=n.runWebGLProgram(m,[r,f],i.dtype);return n.disposeIntermediateTensorInfo(f),g}var S7={kernelName:Eg,backendName:"webgl",kernelFunc:k7};function I7(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a,output:i}=t,o=a;ru([a,i],"maxPoolGrad");let{filterSize:u,strides:l,pad:c,dimRoundingMode:p}=s,d=C.computePool2DInfo(o.shape,u,l,1,c,p),h=!0,f=new al(d,"max",h),m=n.runWebGLProgram(f,[o],o.dtype),g=new x7(d),b=n.runWebGLProgram(g,[r,m],o.dtype);return n.disposeIntermediateTensorInfo(m),b}var C7={kernelName:Ag,backendName:"webgl",kernelFunc:I7};function N7(e,t,n,s){let r=new al(n,"max",!1),a=s.runWebGLProgram(r,[e],"float32");r=new al(n,"max",!0,!0,t);let i=s.runWebGLProgram(r,[e],"float32");return[a,i]}var T7={kernelName:Rg,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{filterSize:r,strides:a,pad:i,includeBatchInIndex:o}=t,u=n;w.assert(s.shape.length===4,()=>`Error in maxPool: input must be rank 4 but got rank ${s.shape.length}.`);let l=[1,1];w.assert(C.eitherStridesOrDilationsAreOne(a,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${a} and dilations '${l}'`);let c=C.computePool2DInfo(s.shape,r,a,l,i),[p,d]=N7(s,o,c,u);return[p,d]}};function $7(e,t,n,s){let r=w.sizeFromShape(t),i=w.sizeFromShape(e.shape)/r,o=pe({inputs:{x:e},attrs:{shape:[i,r]},backend:s}),u=ki(o,"float32","mean",s),l=pe({inputs:{x:u},attrs:{shape:n},backend:s});return s.disposeIntermediateTensorInfo(o),s.disposeIntermediateTensorInfo(u),l}var _7={kernelName:Ka,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{keepDims:r,axis:a}=t,i=n,o=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=C.getAxesPermutation(l,o),p=c!=null,d=i.shouldExecuteOnCPU([s]),h=[],f=s;if(p){if(d){let x=i.texData.get(f.dataId).values,k=new Array(o);for(let R=0;R<k.length;R++)k[R]=s.shape[c[R]];let I=_v(x,s.shape,s.dtype,c,k);f=i.makeTensorInfo(k,s.dtype);let $=i.texData.get(f.dataId);$.values=I}else f=rh(s,c,i);h.push(f),l=C.getInnerMostAxes(l.length,o)}C.assertAxesAreInnerMostDims("sum",l,o);let[m,g]=C.computeOutAndReduceShapes(f.shape,l),b=m;r&&(b=C.expandShapeToKeepDim(m,u));let y=$7(f,g,b,i);for(let v of h)i.disposeIntermediateTensorInfo(v);return y}};function A7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,o),p=r;c!=null&&(p=dn({inputs:{x:r},backend:n,attrs:{perm:c}}),l=C.getInnerMostAxes(l.length,r.shape.length)),C.assertAxesAreInnerMostDims("min",l,o);let[d,h]=C.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=ki(m,m.dtype,"min",n),b;if(i){let y=C.expandShapeToKeepDim(d,u);b=pe({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=pe({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),b}var E7={kernelName:Xa,backendName:"webgl",kernelFunc:A7},R7=e2+`
2022-04-21 15:39:40 +02:00
return min(a, b);
2022-05-30 03:12:18 +02:00
`,D7=`
2022-04-21 15:39:40 +02:00
vec4 result = vec4(min(a, b));
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
2022-05-30 03:12:18 +02:00
`+nh+`
2022-04-21 15:39:40 +02:00
return result;
2022-05-30 03:12:18 +02:00
`,F7=qt({opSnippet:R7,packedOpSnippet:D7,cpuKernelImpl:NX}),O7={kernelName:Ya,backendName:"webgl",kernelFunc:F7},P7=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=t.map((l,c)=>l[0]+e[c]+l[1]);let s=e.length,r=ot(s),a=t.map(l=>l[0]).join(","),i=t.map((l,c)=>l[0]+e[c]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,s),u=n==="reflect"?0:1;if(s===1){this.userCode=`
2022-04-21 15:39:40 +02:00
int start = ${a};
int end = ${i};
void main() {
int outC = getOutputCoords();
if (outC < start) {
outC = start * 2 - outC - ${u};
} else if(outC >= end) {
outC = (end - 1) * 2 - outC + ${u};
}
setOutput(getX(outC - start));
}
`;return}this.userCode=`
${r} start = ${r}(${a});
${r} end = ${r}(${i});
void main() {
${r} outC = getOutputCoords();
for (int i = 0; i < ${s}; i++) {
if (outC[i] < start[i]) {
outC[i] = start[i] * 2 - outC[i] - ${u};
} else if(outC[i] >= end[i]) {
outC[i] = (end[i] - 1) * 2 - outC[i] + ${u};
}
}
${r} coords = outC - start;
setOutput(getX(${o}));
2022-04-01 15:13:32 +02:00
}
2022-05-30 03:12:18 +02:00
`}},z7=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.map((h,f)=>h[0]+e[f]+h[1]);let s=e.length,r=ot(s),a=t.map(h=>h[0]).join(","),i=t.map((h,f)=>h[0]+e[f]).join(","),o=un("rc",s),u=un("source",s),l=`${o[s-1]} < ${this.outputShape[s-1]}`,c=s===1?"source":`vec2(${u.slice(-2).join()})`,p=n==="reflect"?0:1,d="";if(s===1){let h=`
2022-04-21 15:39:40 +02:00
${r} source = rc;
if (source < start) {
source = start * 2 - source - ${p};
} else if (source >= end) {
source = (end - 1) * 2 - source + ${p};
}
source -= start;
`;d=`
${r} rc = outputLoc;
${h}
result[0] = getChannel(getX(${u.join()}), ${c});
${o[s-1]} += 1;
if(${l}) {
${h}
result[1] = getChannel(getX(${u.join()}), ${c});
}
`}else{let h=`
${r} source = rc;
${r} lt = ${r}(lessThan(source, start));
${r} gte = ${r}(greaterThanEqual(source, end));
${r} orig = 1 - (lt + gte);
source = orig * source +
lt * (start * 2 - source - ${p}) +
gte * ((end - 1) * 2 - source + ${p});
source -= start;
`;d=`
${r} rc = outputLoc;
${h}
result[0] = getChannel(getX(${u.join()}), ${c});
${o[s-1]} += 1;
if(${l}) {
${h}
result[1] = getChannel(getX(${u.join()}), ${c});
}
rc = outputLoc;
${o[s-2]} += 1;
if(${o[s-2]} < ${this.outputShape[s-2]}) {
${h}
result[2] = getChannel(getX(${u.join()}), ${c});
${o[s-1]} += 1;
if(${l}) {
${h}
result[3] = getChannel(getX(${u.join()}), ${c});
}
}
`}this.userCode=`
const ${r} start = ${r}(${a});
const ${r} end = ${r}(${i});
void main() {
${r} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${d}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}},M7=({inputs:e,backend:t,attrs:n})=>{let{x:s}=e,{paddings:r,mode:a}=n,i=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new z7(s.shape,r,a):new P7(s.shape,r,a);return t.runWebGLProgram(i,[s],s.dtype)},L7={kernelName:Qa,backendName:"webgl",kernelFunc:M7},B7=`if (b == 0.0) return NAN;
return mod(a, b);`,V7=`
2022-04-21 15:39:40 +02:00
vec4 result = mod(a, b);
vec4 isNaN = vec4(equal(b, vec4(0.0)));
2022-05-30 03:12:18 +02:00
`+nh+`
2022-04-21 15:39:40 +02:00
return result;
2022-05-30 03:12:18 +02:00
`,W7=qt({opSnippet:B7,packedOpSnippet:V7}),U7={kernelName:Cl,backendName:"webgl",kernelFunc:W7},G7=class{constructor(e,t,n){this.variableNames=["probs"],this.customUniforms=[{name:"seed",type:"float"}],this.outputShape=[e,n],this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
float r = random(seed);
float cdf = 0.0;
for (int i = 0; i < ${t-1}; i++) {
cdf += getProbs(batch, i);
if (r < cdf) {
setOutput(float(i));
return;
}
}
// If no other event happened, last event happened.
setOutput(float(${t-1}));
}
2022-05-30 03:12:18 +02:00
`}},H7=`
2022-04-21 15:39:40 +02:00
if (a == b) {
return 1.0;
2022-04-21 15:38:36 +02:00
};
2022-05-30 03:12:18 +02:00
return a / b;`,q7=`
2022-04-21 15:39:40 +02:00
// vec4 one = vec4(equal(a, b));
// return one + (vec4(1.0) - one) * a / b;
vec4 result = a / b;
if(a.x == b.x) {
result.x = 1.;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
if(a.y == b.y) {
result.y = 1.;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
if(a.z == b.z) {
result.z = 1.;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
if(a.w == b.w) {
result.w = 1.;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
return result;
2022-05-30 03:12:18 +02:00
`,k2=qt({opSnippet:H7,packedOpSnippet:q7,checkOutOfBounds:!0}),j7={kernelName:Oa,backendName:"webgl",kernelFunc:k2},Aw="return a - b;",S2=qt({opSnippet:Aw,packedOpSnippet:Aw,supportsComplex:!0,cpuKernelImpl:UX}),K7={kernelName:hi,backendName:"webgl",kernelFunc:S2};function I2(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{dim:a}=s,i=w.parseAxisParam([a],r.shape),o=w2({inputs:{x:r},backend:n,attrs:{reductionIndices:i,keepDims:!1}}),u=C.expandShapeToKeepDim(o.shape,i),l=pe({inputs:{x:o},backend:n,attrs:{shape:u}}),c=S2({inputs:{a:r,b:l},backend:n}),p=y2({inputs:{x:c},backend:n}),d=ah({inputs:{x:p},backend:n,attrs:{axis:i,keepDims:!1}}),h=pe({inputs:{x:d},backend:n,attrs:{shape:u}}),f=k2({inputs:{a:p,b:h},backend:n});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(l),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}var X7={kernelName:di,backendName:"webgl",kernelFunc:I2};function Y7(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{numSamples:a,seed:i,normalized:o}=s,u=o?r:I2({inputs:{logits:r},backend:n,attrs:{dim:r.shape.length-1}}),l=u.shape[0],c=u.shape[1],p=new G7(l,c,a),d=[[i]],h=n.runWebGLProgram(p,[u],"int32",d);return o||n.disposeIntermediateTensorInfo(u),h}var Q7={kernelName:Dg,backendName:"webgl",kernelFunc:Y7},Z7=ss+`
2022-04-21 15:39:40 +02:00
return -x;
2022-05-30 03:12:18 +02:00
`,J7=`
2022-04-21 15:39:40 +02:00
vec4 result = -x;
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`;function eJ(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])){let a=n.texData.get(s.dataId),[i,o]=$X(a.values,s.shape,s.dtype);return n.makeTensorInfo(o,s.dtype,i)}let r;return K().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new ea(s.shape,J7):r=new Gs(s.shape,Z7),n.runWebGLProgram(r,[s],s.dtype)}var tJ={kernelName:No,backendName:"webgl",kernelFunc:eJ},nJ=ws.nonMaxSuppressionV3Impl;function sJ(e){C.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u}=s,l=n.readSync(r.dataId),c=n.readSync(a.dataId),{selectedIndices:p}=nJ(l,c,i,o,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var rJ={kernelName:$o,backendName:"webgl",kernelFunc:sJ},aJ=ws.nonMaxSuppressionV4Impl;function iJ(e){C.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u,padToMaxOutputSize:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),{selectedIndices:d,validOutputs:h}=aJ(c,p,i,o,u,l);return[n.makeTensorInfo([d.length],"int32",new Int32Array(d)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var oJ={kernelName:Nl,backendName:"webgl",kernelFunc:iJ},uJ=ws.nonMaxSuppressionV5Impl;function lJ(e){C.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u,softNmsSigma:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),d=i,h=o,f=u,m=l,{selectedIndices:g,selectedScores:b}=uJ(c,p,d,h,f,m);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var cJ={kernelName:_o,backendName:"webgl",kernelFunc:lJ},dJ=class{constructor(e,t,n,s){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
int index = round(getIndices(coords.x));
setOutput(mix(float(${s}), float(${n}),
float(index == coords.y)));
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},pJ=e=>{let{inputs:t,backend:n,attrs:s}=e,{indices:r}=t,{depth:a,onValue:i,offValue:o}=s,u=w.sizeFromShape(r.shape),l=new dJ(u,a,i,o),c=pe({inputs:{x:r},backend:n,attrs:{shape:[u]}}),p=n.runWebGLProgram(l,[c],r.dtype);n.disposeIntermediateTensorInfo(c);let d=[...r.shape,a],h=pe({inputs:{x:p},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(p),h},hJ={kernelName:Eo,backendName:"webgl",kernelFunc:pJ};function qd(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="complex64"){let r=nc({inputs:{input:s},backend:n}),a=qd({inputs:{x:r},backend:n}),i=ih({inputs:{input:s},backend:n}),o=qd({inputs:{x:i},backend:n}),u=Dr({inputs:{real:a,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),u}else return sc({attrs:{shape:s.shape,dtype:s.dtype,value:s.dtype==="string"?"":0},backend:n})}var fJ={kernelName:jo,backendName:"webgl",kernelFunc:qd};function C2(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(s.dtype==="complex64"){let r=nc({inputs:{input:s},backend:n}),a=C2({inputs:{x:r},backend:n}),i=ih({inputs:{input:s},backend:n}),o=qd({inputs:{x:i},backend:n}),u=Dr({inputs:{real:a,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),u}else return sc({attrs:{shape:s.shape,dtype:s.dtype,value:1},backend:n})}var mJ={kernelName:Ao,backendName:"webgl",kernelFunc:C2};function gJ(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s;if(t.length===1)return tg({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let a=t[0].shape,i=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(i===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let o=[],u=t.map(c=>{let p=tg({inputs:{input:c},backend:n,attrs:{dim:r}});return o.push(p),p}),l=d2({inputs:u,backend:n,attrs:{axis:r}});return o.forEach(c=>n.disposeIntermediateTensorInfo(c)),l}var bJ={kernelName:Ro,backendName:"webgl",kernelFunc:gJ},yJ=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((u,l)=>u[0]+e[l]+u[1]);let s=e.length,r=ot(s),a=t.map(u=>u[0]).join(","),i=t.map((u,l)=>u[0]+e[l]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,s);if(s===1){this.userCode=`
2022-04-21 15:39:40 +02:00
int start = ${a};
int end = ${i};
void main() {
int outC = getOutputCoords();
if (outC < start || outC >= end) {
setOutput(value);
} else {
setOutput(getX(outC - start));
}
}
`;return}this.userCode=`
${r} start = ${r}(${a});
${r} end = ${r}(${i});
void main() {
${r} outC = getOutputCoords();
if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {
setOutput(value);
} else {
${r} coords = outC - start;
setOutput(getX(${o}));
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},vJ=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((f,m)=>f[0]+e[m]+f[1]);let s=e.length,r=ot(s),a=t.map(f=>f[0]).join(","),i=t.map((f,m)=>f[0]+e[m]).join(","),o=un("rc",s),u=un("source",s),l=`${o[s-1]} < ${this.outputShape[s-1]}`,c=s===1?"source":`vec2(${u.slice(-2).join()})`,p=[`${r} rc = outputLoc;`,`${o[s-1]} += 1;
2022-04-21 15:39:40 +02:00
if(${l}) {
`,s===1?"":`}
rc = outputLoc;
${o[s-2]} += 1;
if(${o[s-2]} < ${this.outputShape[s-2]}) {`,s===1?"":` ${o[s-1]} += 1;
if(${l}) {`],d=s===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="";for(let f=0,m=s===1?2:4;f<m;f++)h+=`
${p[f]}
if (${d}) {
result[${f}] = float(value);
} else {
${r} source = rc - start;
result[${f}] = getChannel(getX(${u.join()}), ${c});
}
`;h+=s===1?"} ":"}}",this.userCode=`
const ${r} start = ${r}(${a});
const ${r} end = ${r}(${i});
void main() {
${r} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${h}
setOutput(result);
}
2022-05-30 03:12:18 +02:00
`}},N2=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{paddings:a,constantValue:i}=s;if(w.sizeFromShape(r.shape)===0){let l=a.map((c,p)=>c[0]+r.shape[p]+c[1]);return sc({backend:n,attrs:{shape:l,value:i,dtype:r.dtype}})}let o=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new vJ(r.shape,a,i):new yJ(r.shape,a,i),u=[[i]];return n.runWebGLProgram(o,[r],r.dtype,u)},xJ={kernelName:Ja,backendName:"webgl",kernelFunc:N2},wJ=`
2022-04-21 15:39:40 +02:00
if(a < 0.0 && floor(b) < b){
return NAN;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
if (b == 0.0) {
return 1.0;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
return (round(mod(b, 2.0)) != 1) ?
pow(abs(a), b) : sign(a) * pow(abs(a), b);
2022-05-30 03:12:18 +02:00
`,kJ=`
2022-04-21 15:39:40 +02:00
// isModRound1 has 1 for components with round(mod(b, 2.0)) == 1, 0 otherwise.
vec4 isModRound1 = vec4(equal(round(mod(b, 2.0)), ivec4(1)));
vec4 multiplier = sign(a) * isModRound1 + (vec4(1.0) - isModRound1);
vec4 result = multiplier * pow(abs(a), b);
// Ensure that a^0 = 1, including 0^0 = 1 as this correspond to TF and JS
bvec4 isExpZero = equal(b, vec4(0.0));
result.r = isExpZero.r ? 1.0 : result.r;
result.g = isExpZero.g ? 1.0 : result.g;
result.b = isExpZero.b ? 1.0 : result.b;
result.a = isExpZero.a ? 1.0 : result.a;
vec4 isNaN = vec4(lessThan(a, vec4(0.0))) * vec4(lessThan(floor(b), b));
2022-05-30 03:12:18 +02:00
`+nh+`
2022-04-21 15:39:40 +02:00
return result;
2022-05-30 03:12:18 +02:00
`,SJ=qt({opSnippet:wJ,packedOpSnippet:kJ}),IJ={kernelName:ei,backendName:"webgl",kernelFunc:SJ};function CJ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=[],l=w.parseAxisParam(a,r.shape),c=l,p=C.getAxesPermutation(c,o),d=r;p!=null&&(d=dn({inputs:{x:r},backend:n,attrs:{perm:p}}),c=C.getInnerMostAxes(c.length,o),u.push(d)),C.assertAxesAreInnerMostDims("prod",c,o);let h;if(n.shouldExecuteOnCPU([d])){let f=n.texData.get(d.dataId).values,{outVals:m,outShape:g,outDtype:b}=AX(d.shape,d.dtype,f,c);h=n.makeTensorInfo(g,b,m)}else{let[f,m]=C.computeOutAndReduceShapes(d.shape,c),g=w.sizeFromShape(m),b=pe({inputs:{x:d},backend:n,attrs:{shape:[-1,g]}}),y=yp(r.dtype),v=ki(b,y,"prod",n);h=pe({inputs:{x:v},backend:n,attrs:{shape:f}}),u.push(b),u.push(v)}if(i){u.push(h);let f=C.expandShapeToKeepDim(h.shape,l);h=pe({inputs:{x:h},backend:n,attrs:{shape:f}})}return u.forEach(f=>n.disposeIntermediateTensorInfo(f)),h}var NJ={kernelName:ni,backendName:"webgl",kernelFunc:CJ},T2=e=>{let{backend:t,attrs:n}=e,{start:s,stop:r,step:a,dtype:i}=n,o=EX(s,r,a,i);return t.makeTensorInfo([o.length],i,o)},TJ={kernelName:Tl,backendName:"webgl",kernelFunc:T2},$J="return 1.0 / x;",_J=Ke({opSnippet:$J}),AJ={kernelName:$l,backendName:"webgl",kernelFunc:_J},EJ=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : x;
2022-05-30 03:12:18 +02:00
`,RJ=`
2022-04-21 15:39:40 +02:00
vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`,DJ=Ke({opSnippet:EJ,packedOpSnippet:RJ}),FJ={kernelName:si,backendName:"webgl",kernelFunc:DJ},OJ=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2022-05-30 03:12:18 +02:00
`,PJ=`
2022-04-21 15:39:40 +02:00
vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`,zJ=Ke({opSnippet:OJ,packedOpSnippet:PJ}),MJ={kernelName:ai,backendName:"webgl",kernelFunc:zJ},LJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p;r?p="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":p="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2022-04-21 15:39:40 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2(
${l[0]/c[0]},
${l[1]/c[1]});
const vec2 inputShapeRC = vec2(${i}.0, ${o}.0);
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
// Fractional source index.
vec2 sourceFracIndexRC = ${p};
// Compute the four integer indices.
ivec2 sourceFloorRC = ivec2(max(sourceFracIndexRC, vec2(0.0)));
ivec2 sourceCeilRC = ivec2(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);
float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);
float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);
float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);
vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);
float top = topLeft + (topRight - topLeft) * fracRC.y;
float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
float newValue = top + (bottom - top) * fracRC.x;
setOutput(newValue);
2022-04-18 17:29:45 +02:00
}
2022-05-30 03:12:18 +02:00
`}},BJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p;r?p="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":p="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2022-04-21 15:39:40 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3(
${l[0]/c[0]},
${l[1]/c[1]},
${l[1]/c[1]});
const vec3 inputShapeRC = vec3(${i}.0, ${o}.0,
${o}.0);
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
// Calculate values for next column in yRC.z.
ivec3 yRC = coords.yzz + ivec3(0, 0, 1);
// Fractional source index.
vec3 sourceFracIndexRC = ${p};
// Compute the four integer indices.
ivec3 sourceFloorRC = ivec3(max(sourceFracIndexRC, vec3(0.0)));
ivec3 sourceCeilRC = ivec3(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
// Should we calculate next column and row elements in 2x2 packed cell.
bool hasNextCol = d < ${u-1};
bool hasNextRow = coords.z < ${n-1};
// In parallel, construct four corners for all four components in
// packed 2x2 cell.
vec4 topLeft = vec4(
getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d),
hasNextCol ? getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d + 1) : 0.0);
vec4 bottomLeft = vec4(
getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d),
hasNextCol ? getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d + 1) : 0.0);
vec4 topRight = vec4(
getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d),
hasNextCol ? getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d + 1) : 0.0);
vec4 bottomRight = vec4(
getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d),
hasNextCol ? getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d + 1) : 0.0);
vec3 fracRC = sourceFracIndexRC - vec3(sourceFloorRC);
vec4 top = mix(topLeft, topRight, fracRC.yyzz);
vec4 bottom = mix(bottomLeft, bottomRight, fracRC.yyzz);
vec4 newValue = mix(top, bottom, fracRC.x);
setOutput(newValue);
2022-04-01 15:12:04 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function VJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:i,size:o}=s,[u,l]=o,c=K().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new BJ(r.shape,u,l,a,i):new LJ(r.shape,u,l,a,i);return n.runWebGLProgram(c,[r],"float32")}var WJ={kernelName:ri,backendName:"webgl",kernelFunc:VJ},UJ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,s,r]=t,[,a,i]=e,o=[n&&a>1?s-1:s,n&&i>1?r-1:r],u=[n&&a>1?a-1:a,n&&i>1?i-1:i],l=o[0]/u[0],c=o[1]/u[1],p=1/l,d=1/c,h=Math.ceil(p)*2+2,f=Math.ceil(d)*2+2;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
float accumulator = 0.0;
const float heightScale = float(${l});
const float widthScale = float(${c});
const float invHeightScale = float(${p});
const float invWidthScale = float(${d});
const int winHeight = int(${h});
const int winWidth = int(${f});
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(startRLerp - float(winHeight / 2));
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(startCLerp - float(winWidth / 2));
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
// Guard against the window exceeding the bounds of dy
if (dyR < 0 || dyR >= ${a}) {
continue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
// Guard against the window exceeding the bounds of dy
if (dyC < 0 || dyC >= ${i}) {
continue;
}
float dxR = float(dyR) * heightScale;
int topDxRIndex = int(floor(dxR));
int bottomDxRIndex = int(min(ceil(dxR), ${s-1}.0));
float dxRLerp = dxR - float(topDxRIndex);
float inverseDxRLerp = 1.0 - dxRLerp;
float dxC = float(dyC) * widthScale;
int leftDxCIndex = int(floor(dxC));
int rightDxCIndex = int(min(ceil(dxC), ${r-1}.0));
float dxCLerp = dxC - float(leftDxCIndex);
float inverseDxCLerp = 1.0 - dxCLerp;
if (r == topDxRIndex && c == leftDxCIndex) {
// topLeft
accumulator +=
getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;
}
if (r == topDxRIndex && c == rightDxCIndex) {
// topRight
accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;
}
if (r == bottomDxRIndex && c == leftDxCIndex) {
// bottomLeft
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;
}
if (r == bottomDxRIndex && c == rightDxCIndex) {
// bottomRight
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
// End loop over dy
setOutput(accumulator);
2022-04-01 15:12:04 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function GJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r,dy:a}=t,{alignCorners:i}=s,o=new UJ(a.shape,r.shape,i);return n.runWebGLProgram(o,[a],a.dtype)}var HJ={kernelName:Og,backendName:"webgl",kernelFunc:GJ},qJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p=s?"0.5":"0.0",d;r?d="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":d="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2022-04-21 15:39:40 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2(
${l[0]/c[0]},
${l[1]/c[1]});
const vec2 inputShapeRC = vec2(${i}.0, ${o}.0);
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
// Fractional source index.
vec2 sourceFracIndexRC = ${d};
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestRC = ivec2(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${p})));
float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);
setOutput(newValue);
2022-04-18 18:26:05 +02:00
}
2022-05-30 03:12:18 +02:00
`}},jJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p=s?"0.5":"0.0",d;r?d="max((vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC, vec3(0.0))":d="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2022-04-21 15:39:40 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3(
${l[0]/c[0]},
${l[1]/c[1]},
${l[1]/c[1]});
const vec3 inputShapeRC = vec3(${i}.0, ${o}.0,
${o}.0);
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
2022-04-01 15:12:04 +02:00
}
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
// Calculate values for next column in yRC.z.
ivec3 yRC = coords.yzz + ivec3(0, 0, 1);
// Fractional source index.
vec3 sourceFracIndexRC = ${d};
// Compute the coordinators of nearest neighbor point.
ivec3 sourceNearestRC = ivec3(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${p})));
// Should we calculate next column and row elements in 2x2 packed cell.
bool hasNextCol = d < ${u-1};
bool hasNextRow = coords.z < ${n-1};
vec4 newValue = vec4(
getAValue(b, sourceNearestRC.x, sourceNearestRC.y, d),
hasNextCol ? getAValue(b, sourceNearestRC.x, sourceNearestRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceNearestRC.x, sourceNearestRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceNearestRC.x, sourceNearestRC.z, d + 1) : 0.0);
setOutput(newValue);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function KJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:i,size:o}=s,[u,l]=o,c=K().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new jJ(r.shape,u,l,a,i):new qJ(r.shape,u,l,a,i);return n.runWebGLProgram(c,[r],r.dtype)}var XJ={kernelName:_l,backendName:"webgl",kernelFunc:KJ},YJ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,s,r]=t,[,a,i]=e,o=[n&&a>1?s-1:s,n&&i>1?r-1:r],u=[n&&a>1?a-1:a,n&&i>1?i-1:i],l=o[0]/u[0],c=o[1]/u[1],p=1/l,d=1/c,h=Math.ceil(p)*2+2,f=Math.ceil(d)*2+2;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
float accumulator = 0.0;
const float heightScale = float(${l});
const float widthScale = float(${c});
const float invHeightScale = float(${p});
const float invWidthScale = float(${d});
const int winHeight = int(${h});
const int winWidth = int(${f});
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(floor(startRLerp - float(winHeight / 2)));
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(floor(startCLerp - float(winWidth / 2)));
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
// Guard against the window exceeding the bounds of dy
if (dyR < 0 || dyR >= ${a}) {
continue;
}
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
// Guard against the window exceeding the bounds of dy
if (dyC < 0 || dyC >= ${i}) {
continue;
}
float sourceFracRow =
float(${o[0]}) *
(float(dyR) / float(${u[0]}));
float sourceFracCol =
float(${o[1]}) *
(float(dyC) / float(${u[1]}));
int sourceNearestRow = int(min(
float(int(${s}) - 1),
${n} ? float(round(sourceFracRow)) :
float(floor(sourceFracRow))));
int sourceNearestCol = int(min(
float(int(${r}) - 1),
${n} ? float(round(sourceFracCol)) :
float(floor(sourceFracCol))));
if (r == sourceNearestRow && c == sourceNearestCol) {
accumulator += getDy(b, dyR, dyC, d);
}
}
}
// End loop over dy
setOutput(accumulator);
2022-04-18 18:26:05 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function QJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r,dy:a}=t,{alignCorners:i}=s,o=new YJ(a.shape,r.shape,i);return n.runWebGLProgram(o,[a],a.dtype)}var ZJ={kernelName:Fg,backendName:"webgl",kernelFunc:QJ},JJ=class{constructor(e,t){this.variableNames=["x"];let n=e.length;if(n>4)throw new Error(`WebGL backend: Reverse of rank-${n} tensor is not yet supported`);if(this.outputShape=e,n===1){this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
int coord = getOutputCoords();
setOutput(getX(${e[0]} - coord - 1));
2022-04-01 15:13:32 +02:00
}
2022-04-21 15:39:40 +02:00
`;return}let s=i=>t.indexOf(i)!==-1&&e[i]!==1?`${e[i]} - coords[${i}] - 1`:`coords[${i}]`,r=e.map((i,o)=>s(o)).join(","),a=ot(n);this.userCode=`
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${r}));
}
2022-05-30 03:12:18 +02:00
`}},eee=class{constructor(e,t){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0;let n=e.length;if(n>4)throw new Error(`WebGL backend: Reverse of rank-${n} tensor is not yet supported`);this.outputShape=e;let s=un("rc",n),r=`${s[n-1]} + 1 < ${this.outputShape[n-1]}`,a=`${s[n-2]} + 1 < ${this.outputShape[n-2]}`,i=ot(n);n===1?this.userCode=`
2022-04-21 15:39:40 +02:00
void main(){
int rc = getOutputCoords();
vec4 result = vec4(0.);
result.r = getChannel(getX(${e[0]} - rc - 1),
${e[0]} - rc - 1);
if(${r}){
result.g = getChannel(getX(${e[0]} - (rc + 1) - 1),
${e[0]} - (rc + 1) - 1);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(result);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
`:this.userCode=`
void main() {
${i} rc = getOutputCoords();
vec4 result = vec4(0.);
result.r = ${o(s.slice())};
if(${r}){
result.g = ${u(s.slice())};
}
if(${a}) {
result.b = ${l(s.slice())};
if(${r}) {
result.a = ${c(s.slice())};
}
}
setOutput(result);
2022-04-18 18:26:05 +02:00
}
2022-05-30 03:12:18 +02:00
`;function o(h){return p(h)}function u(h){return h[n-1]="("+h[n-1]+" + 1)",p(h)}function l(h){return h[n-2]="("+h[n-2]+" + 1)",p(h)}function c(h){return h[n-1]="("+h[n-1]+" + 1)",h[n-2]="("+h[n-2]+" + 1)",p(h)}function p(h){let f=e.map((b,y)=>d(y,h)),m=f.join(","),g=f.slice(-2).join(",");return`getChannel(getX(${m}), vec2(${g}))`}function d(h,f){return t.indexOf(h)!==-1&&e[h]!==1?`${e[h]} - ${f[h]} - 1`:`${f[h]}`}}};function tee(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dims:a}=s,i=r.shape.length,o=w.parseAxisParam(a,r.shape);if(i===0)return Rn({inputs:{x:r},backend:n});let u=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new eee(r.shape,o):new JJ(r.shape,o);return n.runWebGLProgram(u,[r],r.dtype)}var nee={kernelName:Fo,backendName:"webgl",kernelFunc:tee},see=class{constructor(e,t){this.variableNames=["Image"],this.outputShape=[],this.customUniforms=[{name:"params",type:"vec4"}];let n=e[1],s=e[2];this.outputShape=e;let r="";typeof t=="number"?r=`float outputValue = ${t.toFixed(2)};`:r=`
2022-04-21 15:39:40 +02:00
vec3 fill = vec3(${t.join(",")});
float outputValue = fill[coords[3]];`,this.userCode=`
void main() {
ivec4 coords = getOutputCoords();
int x = coords[2];
int y = coords[1];
float coordXFloat = (float(x) - params[0]) * params[3] -
(float(y) - params[1]) * params[2];
float coordYFloat = (float(x) - params[0]) * params[2] +
(float(y) - params[1]) * params[3];
int coordX = int(round(coordXFloat + params[0]));
int coordY = int(round(coordYFloat + params[1]));
${r}
if(coordX >= 0 && coordX < ${s} && coordY >= 0 && coordY < ${n}) {
outputValue = getImage(coords[0], coordY, coordX, coords[3]);
}
setOutput(outputValue);
2022-04-18 18:26:05 +02:00
}
2022-05-30 03:12:18 +02:00
`}},ree={kernelName:Ko,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:s}=e,{radians:r,fillValue:a,center:i}=t,o=n,u=new see(s.shape,a),[l,c]=C.getImageCenter(i,s.shape[1],s.shape[2]),p=[[l,c,Math.sin(r),Math.cos(r)]];return o.runWebGLProgram(u,[s],s.dtype,p)}},aee=`
2022-04-21 15:39:40 +02:00
// OpenGL ES does not support round function.
// The algorithm is based on banker's rounding.
float base = floor(x);
if ((x - base) < 0.5) {
return floor(x);
} else if ((x - base) > 0.5) {
return ceil(x);
} else {
if (mod(base, 2.0) == 0.0) {
return base;
} else {
return base + 1.0;
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`,iee=Ke({opSnippet:aee}),oee={kernelName:Oo,backendName:"webgl",kernelFunc:iee},uee="return inversesqrt(x);",lee=Ke({opSnippet:uee,cpuKernelImpl:RX}),cee={kernelName:ii,backendName:"webgl",kernelFunc:lee},$2=class{constructor(e,t,n,s,r,a,i=!0){this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;let o=ot(r.length),u=ot(a.length),l="";n===1?l="i":n===2&&(l="i, j");let c=`getIndices(${l})`,p="";s===1?p="i":s===2&&(p="i, coords[1]");let d=`getUpdates(${p})`,h=t>1?"strides[j]":"strides";this.userCode=`
2022-04-21 15:39:40 +02:00
${o} strides = ${o}(${r});
void main() {
${u} coords = getOutputCoords();
float sum = 0.0;
bool found = false;
for (int i = 0; i < ${e}; i++) {
int flattenedIndex = 0;
for (int j = 0; j < ${t}; j++) {
int index = round(${c});
flattenedIndex += index * ${h};
}
if (flattenedIndex == coords[0]) {
sum += ${d};
found = true;
}
}
setOutput(mix(getDefaultValue(), sum, float(found)));
}
2022-05-30 03:12:18 +02:00
`}};function dee(e){let{inputs:t,backend:n,attrs:s}=e,{indices:r,updates:a}=t,{shape:i}=s,{sliceRank:o,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=C.calculateShapes(a,r,i),d=[p/l,l];if(p===0)return n.makeTensorInfo(i,r.dtype);let h=pe({inputs:{x:r},backend:n,attrs:{shape:[u,o]}}),f=pe({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=n.makeTensorInfo([],"float32",new Float32Array([0])),g=new $2(u,o,h.shape.length,f.shape.length,c,d),b=n.runWebGLProgram(g,[f,h,m],f.dtype),y=pe({inputs:{x:b},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(b),n.disposeIntermediateTensorInfo(m),y}var pee={kernelName:Po,backendName:"webgl",kernelFunc:dee},hee=class{constructor(e,t,n,s){this.variableNames=["sortedSequence","values"],this.customUniforms=[{name:"numInputs",type:"int"}],this.outputShape=[e,n];let r="while (left < right) {",a=`for (int i = 0; i < ${Math.ceil(Math.log2(t+1))}; ++i) { if (left >= right) break;`,i=K().getNumber("WEBGL_VERSION")===2?r:a,o=s==="left"?"<":"<=";this.userCode=`
2022-05-09 14:16:00 +02:00
int findBound(int batch, float value) {
int left = 0;
int right = numInputs;
int mid;
${i}
mid = (left + right) / 2;
if (getSortedSequence(batch, mid) ${o} value) {
left = mid + 1;
} else {
right = mid;
}
}
return right;
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int valueIndex = coords[1];
float value = getValues(batch, valueIndex);
setOutput(float(findBound(batch, value)));
}
2022-05-30 03:12:18 +02:00
`}};function fee(e){let{inputs:t,backend:n,attrs:s}=e,{sortedSequence:r,values:a}=t,{side:i}=s,o=new hee(r.shape[0],r.shape[1],a.shape[1],i),u=[[r.shape[1]]];return n.runWebGLProgram(o,[r,a],"int32",u)}var mee={kernelName:Pg,backendName:"webgl",kernelFunc:fee},gee=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.outputShape=t;let s,r;if(n>4)throw Error(`Where for rank ${n} is not yet supported`);if(n===1)r="resRC",s="resRC";else{let i=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],u=[];for(let l=0;l<t.length;l++)u.push(`${i[l]}`),l<e&&o.push(`${i[l]}`);s=o.join(),r=u.join()}let a=ot(n);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${a} resRC = getOutputCoords();
float cVal = getC(${s});
if (cVal >= 1.0) {
setOutput(getA(${r}));
} else {
setOutput(getB(${r}));
}
2022-04-01 15:13:32 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function bee(e){let{inputs:t,backend:n}=e,{condition:s,t:r,e:a}=t,i=new gee(s.shape.length,r.shape,r.shape.length);return n.runWebGLProgram(i,[s,r,a],ln(r.dtype,a.dtype))}var yee={kernelName:zo,backendName:"webgl",kernelFunc:bee},vee=`
2022-04-21 15:39:40 +02:00
// Stable and Attracting Fixed Point (0, 1) for Normalized Weights.
// see: https://arxiv.org/abs/1706.02515
float scaleAlpha = ${C.SELU_SCALEALPHA};
float scale = ${C.SELU_SCALE};
return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);
2022-05-30 03:12:18 +02:00
`,xee=Ke({opSnippet:vee}),wee={kernelName:Al,backendName:"webgl",kernelFunc:xee},kee=lu+`
2022-04-21 15:39:40 +02:00
return 1.0 / (1.0 + exp(-1.0 * x));
2022-05-30 03:12:18 +02:00
`,See=`
2022-04-21 15:39:40 +02:00
vec4 result = 1.0 / (1.0 + exp(-1.0 * x));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2022-05-30 03:12:18 +02:00
`,Iee=Ke({opSnippet:kee,packedOpSnippet:See,cpuKernelImpl:FX}),Cee={kernelName:ui,backendName:"webgl",kernelFunc:Iee},Nee=`
2022-04-21 15:39:40 +02:00
if (isnan(x)) { return 0.0; }
return sign(x);
2022-05-30 03:12:18 +02:00
`,Tee=Ke({opSnippet:Nee}),$ee={kernelName:El,backendName:"webgl",kernelFunc:Tee},_ee=lu+`
2022-04-21 15:39:40 +02:00
return sin(x);
2022-05-30 03:12:18 +02:00
`,Aee=Ke({opSnippet:_ee}),Eee={kernelName:oi,backendName:"webgl",kernelFunc:Aee},Ree=`
2022-04-21 15:39:40 +02:00
float e2x = exp(x);
return (e2x - 1.0 / e2x) / 2.0;
2022-05-30 03:12:18 +02:00
`,Dee=Ke({opSnippet:Ree}),Fee={kernelName:Lo,backendName:"webgl",kernelFunc:Dee},Oee=`
2022-04-21 15:39:40 +02:00
float epsilon = 1.1920928955078125e-7;
float threshold = log(epsilon) + 2.0;
bool too_large = x > -threshold;
bool too_small = x < threshold;
float result;
float exp_x = exp(x);
if (too_large){
result = x;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
else if (too_small){
result = exp_x;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
else{
result = log(exp_x + 1.0);
2022-04-21 15:38:36 +02:00
}
return result;
2022-05-30 03:12:18 +02:00
`,Pee=Ke({opSnippet:Oee}),zee={kernelName:Rl,backendName:"webgl",kernelFunc:Pee},Mee=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,paddings:i}=s;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let o=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...i);for(let b=1+a.length;b<r.shape.length;++b)u.push([0,0]);let l=[],c=N2({inputs:{x:r},backend:n,attrs:{paddings:u,constantValue:0}}),p=C.getReshaped(c.shape,a,o,!1),d=C.getPermuted(p.length,a.length,!1),h=C.getReshapedPermuted(c.shape,a,o,!1),f=pe({inputs:{x:c},backend:n,attrs:{shape:p}}),m=dn({inputs:{x:f},backend:n,attrs:{perm:d}}),g=pe({inputs:{x:m},backend:n,attrs:{shape:h}});return l.push(c),l.push(f),l.push(m),l.forEach(b=>n.disposeIntermediateTensorInfo(b)),g},Lee={kernelName:Bo,backendName:"webgl",kernelFunc:Mee};function Bee(e){let{inputs:t,backend:n}=e,{indices:s,values:r,denseShape:a,defaultValue:i}=t;if(a.shape.length!==1)throw new Error(`Dense shape must be a vector, saw:
2022-04-21 15:39:40 +02:00
${a.shape}`);if(s.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${s.shape}`);if(r.shape.length!==1)throw new Error(`Values must be a vector, saw:
${r.shape}`);if(i.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
2022-05-30 03:12:18 +02:00
${i.shape}`);let o=n.readSync(s.dataId),u=n.readSync(r.dataId),l=n.readSync(a.dataId),c=n.readSync(i.dataId)[0],[p,d,h,f,m]=PX(o,s.shape,s.dtype,u,r.dtype,l,c);return[n.makeTensorInfo(d,s.dtype,p),n.makeTensorInfo([d[0]],r.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(g=>Number(g)))),n.makeTensorInfo([m.length],s.dtype,new Int32Array(m))]}var Vee={kernelName:dp,backendName:"webgl",kernelFunc:Bee};function Wee(e){let{inputs:t,backend:n}=e,{inputIndices:s,inputShape:r,newShape:a}=t;if(s.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape ${s.shape}`);if(r.shape.length!==1)throw new Error(`Input shape should be a vector but received shape ${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let i=Array.from(n.readSync(r.dataId)),o=n.readSync(s.dataId),u=Array.from(n.readSync(a.dataId)),[l,c,p]=zX(o,s.shape,s.dtype,i,u);return[n.makeTensorInfo(c,s.dtype,l),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var Uee={kernelName:Dl,backendName:"webgl",kernelFunc:Wee};function Gee(e){let{inputs:t,backend:n}=e,{data:s,indices:r,segmentIds:a}=t;if(s.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2022-04-21 15:39:40 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2022-05-30 03:12:18 +02:00
${a.shape}`);let i=n.readSync(s.dataId),o=n.readSync(r.dataId),u=n.readSync(a.dataId),[l,c]=X1(i,s.shape,s.dtype,o,u,!0);return n.makeTensorInfo(c,s.dtype,l)}var Hee={kernelName:pp,backendName:"webgl",kernelFunc:Gee};function qee(e){let{inputs:t,backend:n}=e,{data:s,indices:r,segmentIds:a}=t;if(s.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2022-04-21 15:39:40 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2022-05-30 03:12:18 +02:00
${a.shape}`);let i=n.readSync(s.dataId),o=n.readSync(r.dataId),u=n.readSync(a.dataId),[l,c]=X1(i,s.shape,s.dtype,o,u);return n.makeTensorInfo(c,s.dtype,l)}var jee={kernelName:hp,backendName:"webgl",kernelFunc:qee};function Kee(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:i}=t,{outputShape:o}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=C.calculateShapes(a,r,o),h=!1;if(a.dtype==="string"){let b=n.bufferSync(r),y=n.bufferSync(a),v=w.decodeString(n.readSync(i.dataId)[0]),x=DX(b,y,o,d,c,l,u,p,v,h);return n.makeTensorInfo(o,x.dtype,x.values)}let f=new $2(l,u,r.shape.length,a.shape.length,p,[d,1],h),m=n.runWebGLProgram(f,[a,r,i],a.dtype),g=pe({inputs:{x:m},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(m),g}var Xee={kernelName:fp,backendName:"webgl",kernelFunc:Kee};function Yee(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=s,o=w.parseAxisParam(i,r.shape)[0],u=C.prepareSplitSize(r,a,o),l=r.shape.length,c=new Array(l).fill(0),p=r.shape.slice();return u.map(d=>{let h=[...p];h[o]=d;let f=cu({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[o]+=d,f})}var Qee={kernelName:Vo,backendName:"webgl",kernelFunc:Yee},Ew="return sqrt(x);",Zee=Ke({opSnippet:Ew,packedOpSnippet:Ew,cpuKernelImpl:MX}),Jee={kernelName:li,backendName:"webgl",kernelFunc:Zee},ete="return x * x;",tte=Ke({opSnippet:ete}),nte={kernelName:Fl,backendName:"webgl",kernelFunc:tte},Rw="return (a - b) * (a - b);",ste=qt({opSnippet:Rw,packedOpSnippet:Rw}),rte={kernelName:pi,backendName:"webgl",kernelFunc:ste};function ate({inputs:e,attrs:t,backend:n}){let{x:s}=e,r=ss+`
2022-04-21 15:39:40 +02:00
return x > 0.0 ? 1.0 : float(${t.alpha});
2022-05-30 03:12:18 +02:00
`,a=new Gs(s.shape,r);return n.runWebGLProgram(a,[s],s.dtype)}var ite={kernelName:mi,backendName:"webgl",kernelFunc:ate},ote=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let s=n.length,r=ot(n.length),a=ot(n.length),i="";if(s===1)i="coords * strides + begin";else{let o=0;i=n.map((u,l)=>(o++,n.length===1?`coords * strides[${l}] + begin[${l}]`:`coords[${o-1}] * strides[${l}] + begin[${l}]`)).join(",")}this.userCode=`
2022-04-21 15:39:40 +02:00
${r} begin = ${r}(${e});
${r} strides = ${r}(${t});
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${i}));
2022-04-18 17:29:45 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function ute(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=kt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=pe({inputs:{x:r},backend:n,attrs:{shape:f}});else if(g||b){w.assert(r.shape.length>=1,()=>`Input must have rank at least 1, got: ${r.shape.length}`);let $=kt.computeOutShape(y,v,x),R=cu({inputs:{x:r},backend:n,attrs:{begin:y,size:$}});k=pe({inputs:{x:R},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(R)}else if(n.shouldExecuteOnCPU([r])){let R=n.readSync(r.dataId),E=Ae(r.shape,r.dtype,R),P=LX(h,E,x,y);k=n.makeTensorInfo(f,r.dtype,P.values)}else{let R=new ote(y,x,h);k=n.runWebGLProgram(R,[r],r.dtype)}let I=pe({inputs:{x:k},backend:n,attrs:{shape:f}});return n.disposeIntermediateTensorInfo(k),I}var lte={kernelName:Wo,backendName:"webgl",kernelFunc:ute};function cte(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:i,rightPad:o,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.readSync(c.dataId),h=n.readSync(p.dataId),[f,m]=BX(d,h,r,a,i,o,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var dte={kernelName:mp,backendName:"webgl",kernelFunc:cte};function pte(e){let{inputs:t,backend:n,attrs:s}=e,{skipEmpty:r}=s,{input:a,delimiter:i}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(i.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${i.shape}`);let o=n.readSync(a.dataId),u=n.readSync(i.dataId)[0],[l,c,p]=VX(o,u,r),d=c.length;return[n.makeTensorInfo([d,2],"int32",l),n.makeTensorInfo([d],"string",c),n.makeTensorInfo([2],"int32",new Int32Array(p))]}var hte={kernelName:zg,backendName:"webgl",kernelFunc:pte};function fte(e){let{inputs:t,backend:n,attrs:s}=e,{numBuckets:r}=s,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(r<=0)throw new Error("Number of buckets must be at least 1");let i=n.readSync(a.dataId),o=WX(i,r);return n.makeTensorInfo(a.shape,"int32",o)}var mte={kernelName:Mg,backendName:"webgl",kernelFunc:fte},gte="return tan(x);",bte=Ke({opSnippet:gte}),yte={kernelName:Uo,backendName:"webgl",kernelFunc:bte},vte=`
2022-04-21 15:39:40 +02:00
float e2x = exp(-2.0 * abs(x));
return sign(x) * (1.0 - e2x) / (1.0 + e2x);
2022-05-30 03:12:18 +02:00
`,xte=Ke({opSnippet:vte}),wte={kernelName:fi,backendName:"webgl",kernelFunc:xte},kte=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let a=0;a<n.length;a++)n[a]=e[a]*t[a];this.outputShape=n,this.rank=n.length;let s=ot(this.rank),r=Ste(e);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${s} resRC = getOutputCoords();
setOutput(getA(${r}));
2022-04-18 17:29:45 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Ste(e){let t=e.length;if(t>5)throw Error(`Tile for rank ${t} is not yet supported`);if(t===1)return`imod(resRC, ${e[0]})`;let n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],s=[];for(let r=0;r<e.length;r++)s.push(`imod(${n[r]}, ${e[r]})`);return s.join()}function _2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reps:a}=s;if(r.dtype==="string"||r.shape.length>5){let u=n.readSync(r.dataId),l=r.dtype==="string"?u.map(d=>w.decodeString(d)):u,c=Ae(r.shape,r.dtype,l),p=GX(c,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let i=new kte(r.shape,a);return n.runWebGLProgram(i,[r],r.dtype)}var Ite={kernelName:Nr,backendName:"webgl",kernelFunc:_2},Cte=class{constructor(e){this.variableNames=["x","indices"],this.customUniforms=[{name:"n",type:"int"},{name:"firstPass",type:"int"},{name:"negativeInf",type:"float"},{name:"dir",type:"int"},{name:"inc",type:"int"}],this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int elemIdx = coords[1];
// We compare elements pair-wise within a group of size 2 * inc.
// The comparing rule for each group alternates between ascending
// and descending. Within each group, we compare each pair at
// positions i and i+inc. To decide whether an element at position i
// is x0 or x1, we mod it by 2 * inc, if the result is smaller than
// inc, it is in the first half of the group, we denote it as x0,
// otherwise we denote it as x1.
// For example, as shown in the Bitonic top K paper referenced above,
// Figure5(a) shows that element[1] is in the
// second half of the group when group size is 2, but it is in the
// first half of the group when group size is 4.
bool isFirstInPair = imod(elemIdx, 2 * inc) < inc;
int i = isFirstInPair ? elemIdx : elemIdx - inc;
int i0 = firstPass == 1 ? i : int(getIndices(batch, i));
int i1 = firstPass == 1 ? i + inc : int(getIndices(batch, i + inc));
float x0 = i0 < n ? getX(batch, i0) : negativeInf;
float x1 = i1 < n ? getX(batch, i1) : negativeInf;
// Denotes which direction indices are in (ascending or descending).
bool reverse = imod(elemIdx, 2 * dir) >= dir;
bool isGreater = x0 > x1 || (x0 == x1 && i1 > i0);
if (reverse == isGreater) { // Elements in opposite order of direction
int iTemp = i0;
i0 = i1;
i1 = iTemp;
}
if (isFirstInPair) {
setOutput(float(i0));
} else {
setOutput(float(i1));
}
}
2022-05-30 03:12:18 +02:00
`}},Nte=class{constructor(e){this.variableNames=["x","indices"],this.customUniforms=[{name:"n",type:"int"},{name:"firstPass",type:"int"},{name:"k",type:"int"}],this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
// Takes max of indices (0, k), (1, k + 1), (2, k + 2) ...
ivec2 coords = getOutputCoords();
int batch = coords[0];
int elemIdx = coords[1];
// The output size is half of the previous size.
// If the previous sequence is | | | | _ _ _ _ | | | | _ _ _ _ (k=4),
// we only need to output the indices at positions |, the indices at
// positions _ can be thrown away, see Figure5(b) After Phase 2
// (Merge phase) in the Bitonic Top K paper referenced above.
// For example, the paper shows we only need to output the orange bars.
// The output sequence should look like this | | | | | | | |.
// Because the sequence is halved, to map the output index back
// to the previous sequence to find the corresponding value,
// we need to double the index. When we double the index,
// we basically interpolate a position, so 2i looks like
// | _ | _ | _ | _ | _ | _ | _. We move the | to the first k position
// of each 2k positions by - elemIdx % k. E.g. for output at
// index 4,5,6,7, we want to get the corresponding element at
// original index 8,9,10,11, for output at index 8,9,10,11,
// we want to get the corresponding element at original index
// 16,17,18,19, so on and so forth.
int i = elemIdx < k ? elemIdx : (elemIdx * 2 - imod(elemIdx, k));
int i0 = firstPass == 1 ? i : int(getIndices(batch, i));
int i1 = firstPass == 1 ? i + k : int(getIndices(batch, i + k));
float x0 = getX(batch, i0);
float x1 = i1 < n ? getX(batch, i1) : x0;
setOutput(x0 >= x1 ? float(i0) : float(i1));
}
2022-05-30 03:12:18 +02:00
`}};function Hr(e,t){t!==null&&e.disposeIntermediateTensorInfo(t)}function Dw(e){let t=1;for(;t<e;)t*=2;return t}function Tte(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:i}=s,o=K().getNumber("TOPK_LAST_DIM_CPU_HANDOFF_SIZE_THRESHOLD"),u=K().getNumber("TOPK_K_CPU_HANDOFF_THRESHOLD"),l=r.shape,c=l[l.length-1];if(n.shouldExecuteOnCPU([r])||c<o||a>u){let P=n.readSync(r.dataId),[A,O]=HX(P,l,r.dtype,a,i);return[n.makeTensorInfo(A.shape,A.dtype,A.values),n.makeTensorInfo(O.shape,O.dtype,O.values)]}if(a===0)return l[l.length-1]=0,[n.makeTensorInfo(l,r.dtype,[]),n.makeTensorInfo(l,"int32",[])];if(c===1)return[r,sc({attrs:{shape:l,dtype:"int32",value:0},backend:n})];let p=n.texData.get(r.dataId),d=p!==null&&p.isPacked,h=d?n.unpackTensor(r):r,m=w.sizeFromShape(l)/c,g=pe({inputs:{x:h},attrs:{shape:[m,c]},backend:n});d&&Hr(n,h);let b=Dw(a),y=Dw(c),v=null,x=()=>v===null?[g,g]:[g,v],k=(P,A,O)=>{let T=x(),M=new Cte(O),j=[[c],[v===null?1:0],[Number.NEGATIVE_INFINITY],[P],[A]],Y=v;v=n.runWebGLProgram(M,T,"int32",j),Hr(n,Y)};for(let P=1;P<b;P*=2){let A=P*2;for(let O=P;O>=1;O/=2)k(A,O,[m,y])}for(let P=y;P>b;P/=2){let A=x(),O=new Nte([m,P/2]),M=[[c],[v===null?1:0],[b]],W=v;v=n.runWebGLProgram(O,A,"int32",M),Hr(n,W);let j=b/2,Y=j*2;for(let X=j;X>=1;X/=2)k(Y,X,v.shape)}let I=v;v=cu({inputs:{x:v},backend:n,attrs:{begin:0,size:[m,a]}}),Hr(n,I);let $=x2({inputs:{x:g,indices:v},backend:n,attrs:{axis:1,batchDims:1}});Hr(n,g);let R=l.slice(0,-1);R.push(a),I=v,v=pe({inputs:{x:v},attrs:{shape:R},backend:n}),Hr(n,I);let E=$;return $=pe({inputs:{x:$},attrs:{shape:R},backend:n}),Hr(n,E),[$,v]}var $te={kernelName:Go,backendName:"webgl",kernelFunc:Tte},_te=class{constructor(e,t,n,s,r,a){this.variableNames=["Image","Transforms"],this.outputShape=a;let i=n==="nearest"?1:2,o;switch(s){case"constant":o=1;break;case"reflect":o=2;break;case"wrap":o=3;break;case"nearest":o=4;break;default:o=1;break}this.userCode=`
2022-04-21 15:39:40 +02:00
float mapCoord(float outCoord, float len) {
float inCoord = outCoord;
if(${o} == 2) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz2 = 2.0 * len;
if (inCoord < sz2) {
inCoord = sz2 * float(int(float(-inCoord / sz2))) +
inCoord;
}
inCoord = inCoord < -len ? inCoord + sz2 : -inCoord - 1.0;
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz2 = 2.0 * len;
inCoord -= sz2 * float(int(float(inCoord / sz2)));
if (inCoord >= len) {
inCoord = sz2 - inCoord - 1.0;
}
}
}
return clamp(inCoord, 0.0, len - 1.0);
} else if (${o} == 3) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz = len - 1.0;
inCoord += len * (float(int(float(-inCoord / sz))) + 1.0);
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz = len - 1.0;
inCoord -= len * float(int(float(inCoord / sz)));
}
}
return clamp(inCoord, 0.0, len - 1.0);
} else if (${o} == 4) {
return clamp(outCoord, 0.0, len - 1.0);
} else {
return outCoord;
}
}
float readWithFillValue(int batch, int coordY, int coordX,
int channel) {
float outputValue;
if (0 <= coordY && coordY < ${e} && 0 <= coordX && coordX < ${t}) {
outputValue = getImage(batch, coordY, coordX, channel);
} else {
outputValue = float(${r});
}
return outputValue;
}
void main() {
ivec4 coords = getOutputCoords();
float outputValue;
int batch = coords[0];
int x = coords[2];
int y = coords[1];
int channel = coords[3];
float xf = float(x);
float yf = float(y);
float a1 = getTransforms(batch, 0);
float a2 = getTransforms(batch, 1);
float a3 = getTransforms(batch, 2);
float b1 = getTransforms(batch, 3);
float b2 = getTransforms(batch, 4);
float b3 = getTransforms(batch, 5);
float c1 = getTransforms(batch, 6);
float c2 = getTransforms(batch, 7);
float projection = c1 * xf + c2 * yf + 1.0;
if (projection == 0.0) {
outputValue = float(${r});
} else {
float inX = (a1 * xf + a2 * yf + a3) / projection;
float inY = (b1 * xf + b2 * yf + b3) / projection;
float mapX = mapCoord(inX, float(${t}));
float mapY = mapCoord(inY, float(${e}));
if (${i} == 1) {
int coordY = int(round(mapY));
int coordX = int(round(mapX));
outputValue = readWithFillValue(batch, coordY, coordX,
channel);
} else {
float yFloor = floor(mapY);
float xFloor = floor(mapX);
float yCeil = yFloor + 1.0;
float xCeil = xFloor + 1.0;
float valueYFloor = (xCeil - mapX) *
readWithFillValue(batch, int(yFloor), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yFloor), int(xCeil), channel);
float valueYCeil = (xCeil - mapX) *
readWithFillValue(batch, int(yCeil), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yCeil), int(xCeil), channel);
outputValue = (yCeil - mapY) * valueYFloor +
(mapY - yFloor) * valueYCeil;
}
}
setOutput(outputValue);
}
2022-05-30 03:12:18 +02:00
`}};function Ate(e){let{inputs:t,backend:n,attrs:s}=e,{image:r,transforms:a}=t,{interpolation:i,fillMode:o,fillValue:u,outputShape:l}=s,[c,p,d,h]=r.shape,[f,m]=l!=null?l:[p,d],g=[c,f,m,h],b=new _te(p,d,i,o,u,g);return n.runWebGLProgram(b,[r,a],"float32")}var Ete={kernelName:Ho,backendName:"webgl",kernelFunc:Ate};function Rte(e){let{inputs:t,attrs:n,backend:s}=e,{axis:r}=n,{x:a}=t;ru(a,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let i=s.readSync(a.dataId),{outputValues:o,outputShape:u,indices:l}=qX(i,r,a.shape,a.dtype);return[s.makeTensorInfo(u,a.dtype,o),s.makeTensorInfo([l.length],"int32",l)]}var Dte={kernelName:Lg,backendName:"webgl",kernelFunc:Rte};function Fte(e){let{inputs:t,backend:n,attrs:s}=e,{value:r}=t,{axis:a}=s;a<0&&(a+=r.shape.length);let i=r,o=i.shape.length,u=r.shape[a],l=new Array(o-1),c=0;for(let m=0;m<o;m++)m!==a&&(l[c++]=i.shape[m]);let p=[],d=new Array(o).fill(0),h=i.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){d[a]=m;let g=cu({inputs:{x:i},backend:n,attrs:{begin:d,size:h}}),b=pe({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,p.push(g)}return p.forEach(m=>n.disposeIntermediateTensorInfo(m)),f}var Ote={kernelName:qo,backendName:"webgl",kernelFunc:Fte},Pte=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,s=e.batchSize,r=e.inSize,a=e.numSegments,i=a*Math.ceil(r/n);this.outputShape=[s,i];let o="0.0",u="sumValue",l=Math.floor(n/4)*4,c=n%4,p=`
2022-04-21 15:39:40 +02:00
sumValue += dot(values, segFilter);
`,d="";r%n>0&&(d=`
if (inIdx < 0 || inIdx >= ${r}) {
return initializationValue;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`);let h="";r%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${r}) {
return -1.0;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`),this.userCode=`
const float initializationValue = ${o};
float getValue(int batch, int inIdx) {
${d}
return getX(batch, inIdx);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
float getSegmentIdAtIndex(int inIdx) {
${h}
return getSegmentIds(inIdx);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = int(floor(float(outIdx) / float(
${a})) * float(${n}));
int currentSeg = int(mod(float(outIdx), float(${a})));
float sumValue = 0.0;
for (int i = 0; i < ${l}; i += 4) {
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0
);
${p}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
int inIdx = inOffset + ${l};
if (${c===1}) {
vec4 values = vec4(
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
int inIdxSeg = int(getSegmentIdAtIndex(inIdx));
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
0,
0,
0
);
${p}
} else if (${c===2}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
0,
0
);
${p}
} else if (${c===3}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
0
);
${p}
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
setOutput(${u});
2022-04-18 17:29:45 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function zte(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,segmentIds:a}=t,{numSegments:i}=s,o=r.shape.length,u=[],l=0,c=C.getAxesPermutation([l],o),p=r;c!=null&&(p=dn({inputs:{x:r},backend:n,attrs:{perm:c}}),u.push(p),l=C.getInnerMostAxes(1,o)[0]);let d=C.segment_util.computeOutShape(p.shape,l,i),h=w.sizeFromShape([p.shape[l]]),f=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,h]}});u.push(f);let m=yp(r.dtype),g=(x,k,I,$,R)=>{let E=x.shape[0],P=x.shape[1],A=C.segment_util.segOpComputeOptimalWindowSize(P,R),O={windowSize:A,inSize:P,batchSize:E,numSegments:R},T=new Pte(O,k),M=n.compileAndRun(T,[x,I],$);if(u.push(M),M.shape[1]===R)return M;let W=T2({backend:n,attrs:{start:0,stop:R,step:1,dtype:"float32"}}),j=_2({inputs:{x:W},backend:n,attrs:{reps:[P/A]}});return u.push(W),u.push(j),g(M,k,j,$,R)},b=g(f,"unsortedSegmentSum",a,m,i),y=pe({inputs:{x:b},backend:n,attrs:{shape:d}}),v=y;if(c!=null){u.push(y);let x=C.getUndoAxesPermutation(c);v=dn({inputs:{x:v},backend:n,attrs:{perm:x}})}return u.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var Mte={kernelName:gp,backendName:"webgl",kernelFunc:zte},Lte=[B8,W8,H8,K8,Y8,J8,tY,sY,oY,lY,pY,mY,yY,kY,CY,TY,_Y,DY,OY,zY,VY,KY,YY,ZY,r9,i9,c9,w8,h9,y9,k9,$9,A9,R9,F9,P9,L9,W9,H9,j9,X9,Q9,eQ,nQ,iQ,uQ,dQ,fQ,gQ,xQ,IQ,$Q,EQ,FQ,OQ,zQ,LQ,VQ,UQ,HQ,XQ,ZQ,tZ,sZ,iZ,lZ,hZ,bZ,x8,vZ,g9,kZ,CZ,$Z,S8,RZ,PZ,MZ,WZ,HZ,XZ,ZZ,n7,i7,l7,d7,m7,b7,v7,S7,C7,T7,_7,E7,O7,L7,U7,Q7,$8,tJ,rJ,oJ,cJ,e9,hJ,mJ,bJ,xJ,IJ,C8,NJ,TJ,t9,j7,AJ,FJ,MJ,A8,WJ,HJ,XJ,ZJ,nee,ree,oee,cee,pee,mee,yee,wee,Cee,$ee,Eee,Fee,qY,X7,zee,Lee,Vee,Uee,Hee,jee,Xee,Qee,Jee,nte,rte,ite,lte,dte,hte,mte,K7,z8,yte,wte,Ite,$te,Ete,M8,Dte,Ote,Mte,fJ];for(let e of Lte)Ol(e);var Fr=K();Fr.registerFlag("WEBGPU_DEFERRED_SUBMIT_BATCH_SIZE",()=>15);Fr.registerFlag("WEBGPU_CPU_FORWARD",()=>!0);Fr.registerFlag("WEBGPU_MATMUL_WORK_PER_THREAD",()=>4);Fr.registerFlag("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE",()=>!1);Fr.registerFlag("WEBGPU_USE_LOW_POWER_GPU",()=>!1);Fr.registerFlag("WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD",()=>1e3);Fr.registerFlag("WEBGPU_USE_PROFILE_TOOL",()=>!1);Fr.registerFlag("WEBGPU_USE_IMPORT",()=>!1);var Bte="return a + b;",Vte="return areal * breal - aimag * bimag;",Wte="return areal * bimag + aimag * breal;",Ute="return a / b;",Gte="return a * b;",Hte="return (a - b) * (a - b);",qte="return a - b;",jte="return f32(a == b);",Kte="return vec4<f32>(a == b);",Xte="return f32(a > b);",Yte="return vec4<f32>(a > b);",Qte="return f32(a >= b);",Zte="return vec4<f32>(a >= b);",Jte="return f32(a < b);",ene="return vec4<f32>(a < b);",tne="return f32(a <= b);",nne="return vec4<f32>(a <= b);",sne="return f32(f32(a) >= 1.0 && f32(b) >= 1.0);",rne=`return (vec4<f32>(a >= vec4<f32>(1.0)) *
vec4<f32>(b >= vec4<f32>(1.0)));`,ane=`
2022-04-21 15:39:40 +02:00
if (isnan(a)) { return a; }
if (isnan(b)) { return b; }
2022-05-30 03:12:18 +02:00
`,A2=`
2022-04-21 15:39:40 +02:00
if (isNaN.r) {
resultTemp.r = uniforms.NAN;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (isNaN.g) {
resultTemp.g = uniforms.NAN;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (isNaN.b) {
resultTemp.b = uniforms.NAN;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (isNaN.a) {
resultTemp.a = uniforms.NAN;
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`,ine=`
2022-04-21 15:39:40 +02:00
let s = sign(a) * sign(b);
let ia = i32(round(a));
let ib = i32(round(b));
return f32(idiv(ia, ib, s));
2022-05-30 03:12:18 +02:00
`,one=`
2022-04-21 15:39:40 +02:00
let ia = vec4<i32>(round(a));
let ib = vec4<i32>(round(b));
let cond = ib != vec4<i32>(0);
var resultTemp = vec4<i32>(0);
let s = sign(a) * sign(b);
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
if (cond[0]) {
resultTemp[0] = idiv(ia[0], ib[0], s[0]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (cond[1]) {
resultTemp[1] = idiv(ia[1], ib[1], s[1]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (cond[2]) {
resultTemp[2] = idiv(ia[2], ib[2], s[2]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (cond[3]) {
resultTemp[3] = idiv(ia[3], ib[3], s[3]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return vec4<f32>(resultTemp);
2022-05-30 03:12:18 +02:00
`,une="return f32(a != b);",lne="return vec4<f32>(a != b);",cne=`
2022-04-21 15:39:40 +02:00
if(a < 0.0 && floor(b) < b) {
return uniforms.NAN;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (b == 0.0) {
return 1.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (round(abs(b) % 2.0) != 1.0) {
return pow(abs(a), b);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return sign(a) * pow(abs(a), b);
2022-05-30 03:12:18 +02:00
`,dne=`
2022-04-21 15:39:40 +02:00
let isModRound1Bool = vec4<i32>(round(abs(b) % vec4<f32>(2.0))) == vec4<i32>(1);
let isModRound1 = vec4<f32>(isModRound1Bool);
let multiplier = sign(a) * isModRound1 + (vec4<f32>(1.0) - isModRound1);
var resultTemp = multiplier * pow(abs(a), b);
// Ensure that a^0 = 1, including 0^0 = 1 as this correspond to TF and JS
let isExpZero = b == vec4<f32>(0.0);
if (isExpZero.r) {
resultTemp.r = 1.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (isExpZero.g) {
resultTemp.g = 1.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (isExpZero.b) {
resultTemp.b = 1.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (isExpZero.a) {
resultTemp.a = 1.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
let isNaN = a < vec4<f32>(0.0) & floor(b) < b;
2022-05-30 03:12:18 +02:00
${A2}
2022-04-21 15:39:40 +02:00
return resultTemp;
2022-05-30 03:12:18 +02:00
`,pne="if (a < 0.0) { return b * a; } return a;",hne=`
2022-04-21 15:39:40 +02:00
let aLessThanZero = vec4<f32>(a < vec4<f32>(0.0));
return (aLessThanZero * (b * a)) + ((vec4<f32>(1.0) - aLessThanZero) * a);
2022-05-30 03:12:18 +02:00
`;function Fw(e,t){let n=t?A2:ane;return t?`
2022-04-21 15:39:40 +02:00
var resultTemp = vec4<f32>(${e}(a, b));
let isNaN = isnanVec4(a) | isnanVec4(b);
`+n+`
return resultTemp;
`:n+`
return ${e}(a, b);
2022-05-30 03:12:18 +02:00
`}function rc(e,t){switch(e){case 0:return Gte;case 1:return Bte;case 2:return qte;case 3:return Ute;case 4:return t?Kte:jte;case 5:return t?Yte:Xte;case 6:return t?Zte:Qte;case 7:return t?ene:Jte;case 8:return t?nne:tne;case 9:return t?rne:sne;case 10:return t?lne:une;case 11:return Hte;case 12:return t?one:ine;case 14:return t?hne:pne;case 15:return Fw("max",t);case 16:return Fw("min",t);case 13:return t?dne:cne;case 17:return Vte;case 18:return Wte;default:throw new Error(`BinaryType ${e} is not implemented!`)}}var fne="return abs(a);",mne="return ceil(a);",gne="return cos(a);",bne=`
2022-04-21 15:39:40 +02:00
let e2x = exp(-a);
return (e2x + 1.0 / e2x) / 2.0;
2022-05-30 03:12:18 +02:00
`,yne="return exp(a) - 1.0;",vne="if (a >= 0.0) { return a; } return (exp(a) - 1.0);",xne=`
2022-04-21 15:39:40 +02:00
var resFloat = exp(a) - vec4<f32>(1.0);
if (a.r >= 0.0) {
resFloat.r = a.r;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (a.g >= 0.0) {
resFloat.g = a.g;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (a.b >= 0.0) {
resFloat.b = a.b;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (a.a >= 0.0) {
resFloat.a = a.a;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return resFloat;
2022-05-30 03:12:18 +02:00
`,wne="return exp(a);",kne="return floor(a);",Sne="return a;",Ine=`if (a < 0.0) { return 1.0/0.0; }
return log(a);`,Cne="return f32(!(a >= 1.0));",Nne="return -a;",Tne="if (a < 0.0) { return uniforms.alpha * a; } return a;",$ne=`
2022-04-21 15:39:40 +02:00
let aLessThanZero = vec4<f32>(a < vec4<f32>(0.0));
return (aLessThanZero * (uniforms.alpha * a)) + ((vec4<f32>(1.0) - aLessThanZero) * a);
2022-05-30 03:12:18 +02:00
`,_ne="return select(a, 0.0, a < 0.0);",Ane="return clamp(a, 0.0, 6.0);",Ene="return clamp(a, vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(6.0, 6.0, 6.0, 6.0));",Rne=`
2022-05-09 14:16:00 +02:00
return select(a, vec4<f32>(0.0), a < vec4<f32>(0.0));
2022-05-30 03:12:18 +02:00
`,Dne="return 1.0/sqrt(a);",Fne="return 1.0 / (1.0 + exp(-1.0 * a));",One="return sin(a);",Pne=`
2022-04-21 15:39:40 +02:00
let e2x = exp(a);
return (e2x - 1.0 / e2x) / 2.0;
2022-05-30 03:12:18 +02:00
`,zne="return sqrt(a);",Mne="return a * a;",Lne=`
2022-04-21 15:39:40 +02:00
let e2x = exp(-2.0 * abs(a));
return sign(a) * (1.0 - e2x) / (1.0 + e2x);
2022-05-30 03:12:18 +02:00
`,Bne="return f32(i32((a)));";function jr(e,t){switch(e){case 0:return fne;case 2:return gne;case 3:return bne;case 1:return mne;case 4:return t?xne:vne;case 5:return wne;case 6:return yne;case 7:return kne;case 8:return Sne;case 9:return Ine;case 10:return Cne;case 11:return Nne;case 14:return t?$ne:Tne;case 12:return t?Rne:_ne;case 13:return t?Ene:Ane;case 15:return Dne;case 18:return Fne;case 16:return One;case 17:return Pne;case 19:return zne;case 20:return Mne;case 21:return Lne;case 22:return Bne;default:throw new Error(`BinaryType ${e} is not implemented!`)}}function Or(e,t=!1){if(e===null)return null;if(e==="linear")return jr(8);if(e==="relu")return jr(12,t);if(e==="elu")return jr(4,t);if(e==="relu6")return jr(13,t);if(e==="prelu")return rc(14,t);if(e==="sigmoid")return jr(18,t);if(e==="leakyrelu")return jr(14,t);throw new Error(`Activation ${e} has not been implemented for the WebGPU backend.`)}function Vne(e,t){if(Math.max(...e)>3)throw new Error("Cannot symbolically compute strides for rank > 4 tensor.");let n=e.length,s=e.map(a=>`${t}[${a}]`),r=new Array(n-1);r[n-2]=s[n-1];for(let a=n-3;a>=0;--a)r[a]=`(${r[a+1]} * ${s[a+1]})`;return r}function Wt(e){if(e<=1)return"i32";if(e===2)return"vec2<i32>";if(e===3)return"vec3<i32>";if(e===4)return"vec4<i32>";if(e===5)return"vec5";if(e===6)return"vec6";throw Error(`GPU for rank ${e} is not yet supported`)}function pr(e){if(e===0)return"x";if(e===1)return"y";if(e===2)return"z";if(e===3)return"w";if(e===4)return"u";if(e===5)return"v";throw Error(`Index ${e} is not yet supported`)}function fd(e,t){return e==="float32"?t?"vec4<f32>":"f32":e==="int32"||e==="bool"?t?"vec4<i32>":"i32":e}function Rv(){return`
2022-04-21 15:39:40 +02:00
@stage(compute) @workgroup_size(workGroupSizeX, workGroupSizeY, workGroupSizeZ)
2022-05-24 13:28:51 +02:00
`}function du(){return`
2022-05-30 03:12:18 +02:00
${Rv()}
2022-04-21 15:39:40 +02:00
fn main(@builtin(local_invocation_id) LocalId : vec3<u32>,
@builtin(global_invocation_id) GlobalId : vec3<u32>,
@builtin(num_workgroups) NumWorkgroups: vec3<u32>) {
localId = LocalId;
globalId = GlobalId;
numWorkgroups = NumWorkgroups;
`}function Ue(){return`
2022-05-24 13:28:51 +02:00
${du()}
2022-04-21 15:39:40 +02:00
let index = getGlobalIndex();
2022-05-30 03:12:18 +02:00
`}function Wne(e,t,n,s=!1){let r=[];if(r.push(`
2022-04-21 15:39:40 +02:00
let workGroupSizeX = ${n.workGroupSize[0]}u;
let workGroupSizeY = ${n.workGroupSize[1]}u;
let workGroupSizeZ = ${n.workGroupSize[2]}u;
var<private> localId: vec3<u32>;
var<private> globalId: vec3<u32>;
var<private> numWorkgroups: vec3<u32>;
// Only used when the y/z dimension of workgroup size is 1.
fn getGlobalIndex() -> i32 {
2022-05-30 03:12:18 +02:00
${n.dispatch[1]===1&&n.dispatch[2]===1?" return i32(globalId.x);":` let localInvocationIndex = localId.z * workGroupSizeX * workGroupSizeY +
localId.y * workGroupSizeX + localId.x;
let workGroupID = (globalId - localId)/vec3<u32>(
workGroupSizeX, workGroupSizeY, workGroupSizeZ);
return i32((workGroupID.z * numWorkgroups.x * numWorkgroups.y +
workGroupID.y * numWorkgroups.x + workGroupID.x) *
(workGroupSizeX * workGroupSizeY * workGroupSizeZ) +
localInvocationIndex);
`}
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`),s===!0)return r.push(`
struct Uniform {
size : i32,
numChannels : i32,
outShapeStrides : vec2<i32>,
dispatchSize : vec3<u32>,
};
2022-05-22 14:50:51 +02:00
@group(0) @binding(0) var<storage, write> result: array<${fd(t.dtype,n.isVec4)}>;
2022-04-21 15:39:40 +02:00
@group(0) @binding(2) var<uniform> uniforms: Uniform;
2022-05-30 03:12:18 +02:00
`),[Ow,r.join(`
`),Pw(t.shape),n.getUserCode()].join(`
`);let a=!1,i=!1,o="struct Uniforms { NAN : f32, ";n.variableNames.forEach((m,g)=>{let b=Wt(e[g].shape.length);(b==="vec5"||b==="vec6")&&(i=!0),(a||i)&&(o+="@align(16) "),a=i,o+=`${m.charAt(0).toLowerCase()+m.slice(1)}Shape : ${b}, `});let u=Wt(t.shape.length);i=u==="vec5"||u==="vec6",(a||i)&&(o+="@align(16) "),a=i,o+=`outShape : ${u}, `;let l=t.shape.length-1,c=Wt(l);i=c==="vec5"||c==="vec6",(a||i)&&(o+="@align(16) "),a=i,o+=`
2022-05-09 14:16:00 +02:00
outShapeStrides: ${c}, `,n.size&&(a&&(o+="@align(16) "),a=!1,o+="size : i32, "),n.uniforms&&(a&&(o+="@align(16) "),o+=n.uniforms),o+="};",r.push(o),n.atomic?r.push(`
2022-04-21 15:39:40 +02:00
@group(0) @binding(0) var<storage, read_write> result: array<atomic<i32>>;
`):r.push(`
2022-05-22 14:50:51 +02:00
@group(0) @binding(0) var<storage, write> result: array<${fd(t.dtype,n.isVec4)}>;
2022-05-09 14:16:00 +02:00
`),n.variableNames.forEach((m,g)=>{r.push(`
2022-05-22 14:50:51 +02:00
@group(0) @binding(${1+g}) var<storage, read> ${m}: array<${n.variableTypes?n.variableTypes[g]:fd(e[g].dtype,n.isVec4)}>;
`)}),o!==""&&r.push(`
2022-04-21 15:39:40 +02:00
@group(0) @binding(${1+n.variableNames.length}) var<uniform> uniforms: Uniforms;
2022-05-30 03:12:18 +02:00
`);let[p,d]=Kne(t.shape,n.dispatchLayout),h=[Ow,r.join(`
`),Pw(t.shape),p,Une(t.shape.length)];if(n.atomic||h.push(Gne(t.shape,t.dtype,n.isVec4)),d===t.shape.length){let m=e.map((g,b)=>Hne(g,t.shape,n.variableTypes?n.variableTypes[b]==="vec4<f32>":n.isVec4,n.dispatchLayout.x.length===t.shape.length)).join(`
2022-05-09 14:16:00 +02:00
`);h.push(m)}return h.push(n.getUserCode()),h.join(`
2022-05-30 03:12:18 +02:00
`)}var Ow=`
2022-05-09 14:16:00 +02:00
struct vec5 {x: i32, y: i32, z: i32, w: i32, u: i32};
struct vec6 {x: i32, y: i32, z: i32, w: i32, u: i32, v: i32};
2022-04-21 15:39:40 +02:00
// Checks whether coordinates lie within the bounds of the shape.
fn coordsInBounds2D(coord : vec2<i32>, shape : vec2<i32>) -> bool {
return all(coord >= vec2<i32>(0)) && all(coord < shape);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn coordsInBounds3D(coord : vec3<i32>, shape : vec3<i32>) -> bool {
return all(coord >= vec3<i32>(0)) && all(coord < shape);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn coordsInBounds4D(coord : vec4<i32>, shape : vec4<i32>) -> bool {
return all(coord >= vec4<i32>(0)) && all(coord < shape);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn getIndexFromCoords1D(coord : i32, shape : i32) -> i32 {
return coord;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn getIndexFromCoords2D(coords : vec2<i32>, shape : vec2<i32>) -> i32 {
return dot(coords, vec2<i32>(shape.y, 1));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn getIndexFromCoords3D(coords : vec3<i32>, shape : vec3<i32>) -> i32 {
return dot(coords, vec3<i32>(shape.y * shape.z, shape.z, 1));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn getIndexFromCoords4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {
return dot(coords, vec4<i32>(
shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));
2022-04-21 15:38:36 +02:00
}
2022-05-09 14:16:00 +02:00
fn getIndexFromCoords5D(coords : vec5, shape : vec5) -> i32 {
let shapeStrides: vec5 = vec5(shape.y * shape.z * shape.w * shape.u, shape.z * shape.w * shape.u, shape.w * shape.u, shape.u, 1);
return coords.x*shapeStrides.x + coords.y*shapeStrides.y + coords.z*shapeStrides.z + coords.w*shapeStrides.w + coords.u*shapeStrides.u;
}
fn getIndexFromCoords6D(coords : vec6, shape : vec6) -> i32 {
let shapeStrides: vec6 = vec6(shape.y * shape.z * shape.w * shape.u * shape.v, shape.z * shape.w * shape.u * shape.v, shape.w * shape.u * shape.v, shape.u * shape.v, shape.v, 1);
return coords.x*shapeStrides.x + coords.y*shapeStrides.y + coords.z*shapeStrides.z + coords.w*shapeStrides.w + coords.u*shapeStrides.u + coords.v*shapeStrides.v;
}
2022-04-21 15:39:40 +02:00
fn idiv(a: i32, b: i32, sign: f32) -> i32 {
var res: i32 = a / b;
let mod: i32 = a % b;
if (sign < 0. && mod != 0) {
res = res - 1;
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
return res;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
// NaN defination in IEEE 754-1985 is :
// - sign = either 0 or 1.
// - biased exponent = all 1 bits.
// - fraction = anything except all 0 bits (since all 0 bits represents infinity).
// https://en.wikipedia.org/wiki/IEEE_754-1985#Representation_of_non-numbers
fn isnan(val: f32) -> bool {
let floatToUint: u32 = bitcast<u32>(val);
return (floatToUint & 0x7fffffffu) > 0x7f800000u;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn isnanVec4(val : vec4<f32>) -> vec4<bool> {
return vec4<bool>(isnan(val[0]), isnan(val[1]), isnan(val[2]), isnan(val[3]));
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`;function Une(e){let t="";switch(e){case 0:case 1:t+=`
2022-04-21 15:39:40 +02:00
fn getOutputIndexFromCoords(coords : i32) -> i32 {
return coords;
}
`;break;case 2:t+=`
fn getOutputIndexFromCoords(coords : vec2<i32>) -> i32 {
return dot(coords, vec2<i32>(uniforms.outShapeStrides, 1));
}
`;break;case 3:t+=`
fn getOutputIndexFromCoords(coords : vec3<i32>) -> i32 {
return dot(coords, vec3<i32>(uniforms.outShapeStrides.x, uniforms.outShapeStrides.y, 1));
}
`;break;case 4:t+=`
fn getOutputIndexFromCoords(coords : vec4<i32>) -> i32 {
return dot(coords, vec4<i32>(
uniforms.outShapeStrides.x, uniforms.outShapeStrides.y, uniforms.outShapeStrides.z, 1));
}
2022-05-09 14:16:00 +02:00
`;break;case 5:t+=`
fn getOutputIndexFromCoords(coords : vec5) -> i32 {
return coords.x * uniforms.outShapeStrides.x +
coords.y * uniforms.outShapeStrides.y +
coords.z * uniforms.outShapeStrides.z +
coords.w * uniforms.outShapeStrides.w +
coords.u;
}
`;break;case 6:t+=`
fn getOutputIndexFromCoords(coords : vec6) -> i32 {
return coords.x * uniforms.outShapeStrides.x +
coords.y * uniforms.outShapeStrides.y +
coords.z * uniforms.outShapeStrides.z +
coords.w * uniforms.outShapeStrides.w +
coords.u * uniforms.outShapeStrides.u +
coords.v;
}
2022-05-30 03:12:18 +02:00
`;break;default:w.assert(!1,()=>`Unsupported ${e}D shape`);break}return t}function Gne(e,t,n){let s=e.length,r=fd(t,n),a;if(n?a=`fn setOutputAtIndex(flatIndex : i32, value : vec4<f32>) {
2022-04-21 15:39:40 +02:00
result[flatIndex] = ${r}(value);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn setOutputAtIndexI32(flatIndex : i32, value : vec4<i32>) {
result[flatIndex] = ${r}(value);
}`:a=`fn setOutputAtIndex(flatIndex : i32, value : f32) {
result[flatIndex] = ${r}(value);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn setOutputAtIndexI32(flatIndex : i32, value : i32) {
result[flatIndex] = ${r}(value);
2022-05-30 03:12:18 +02:00
}`,s>=2){let i=["d0","d1","d2","d3","d4","d5"].slice(0,s),o=Wt(s);n?a+=`
2022-04-21 15:39:40 +02:00
fn setOutputAtCoords(${i.map(u=>`${u} : i32`).join(", ")}, value : vec4<f32>) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndex(flatIndex / 4, value);
2022-04-18 18:26:05 +02:00
}
2022-04-21 15:39:40 +02:00
fn setOutputAtCoordsI32(${i.map(u=>`${u} : i32`).join(", ")}, value : vec4<i32>) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndexI32(flatIndex / 4, value);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`:a+=`
fn setOutputAtCoords(${i.map(u=>`${u} : i32`).join(", ")}, value : f32) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndex(flatIndex, value);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn setOutputAtCoordsI32(${i.map(u=>`${u} : i32`).join(", ")}, value : i32) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndexI32(flatIndex, value);
}
2022-05-30 03:12:18 +02:00
`}return a}function Hne(e,t,n,s){let r=qne(e,n);return e.shape.length<=t.length&&(r+=jne(e,t,n,s)),r}function qne(e,t){let n=e.name,s=e.shape.length,r=Wt(s),a="get"+n.charAt(0).toUpperCase()+n.slice(1),i=["d0","d1","d2","d3","d4","d5"].slice(0,s),o=i.map(c=>`${c} : i32`).join(", ");if(s<1)return t?`
2022-04-21 15:39:40 +02:00
fn ${a}() -> vec4<f32> {
return vec4<f32>(${n}[0]);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
fn ${a}() ->f32 {
return f32(${n}[0]);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`;let u=`uniforms.${n.charAt(0).toLowerCase()+n.slice(1)}Shape`,l=`${s}D`;return s===0&&(l="1D"),t?`
fn ${a}(${o}) -> vec4<f32> {
return vec4<f32>(${n}[getIndexFromCoords${l}(${r}(${i.join(",")}),
${u}) / 4]);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
fn ${a}(${o}) -> f32 {
return f32(${n}[getIndexFromCoords${l}(${r}(${i.join(",")}),
${u})]);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}function jne(e,t,n,s){let r=e.name,a=r.charAt(0).toUpperCase()+r.slice(1),i="get"+a+"ByOutput",o=e.shape.length,u=t.length,l=Wt(u);if(w.arraysEqual(e.shape,t)&&s)return n?`
2022-04-21 15:39:40 +02:00
fn ${i}Index(globalIndex : i32) -> vec4<f32> {
return vec4<f32>(${r}[globalIndex]);
}
fn ${i}Coords(coords : ${l}) -> vec4<f32> {
return vec4<f32>(${r}[${u>1?"getOutputIndexFromCoords(coords)":"coords"} / 4]);
}
`:`
fn ${i}Index(globalIndex : i32) -> f32 {
return f32(${r}[globalIndex]);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
fn ${i}Coords(coords : ${l}) -> f32 {
return f32(${r}[${u>1?"getOutputIndexFromCoords(coords)":"coords"}]);
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`;let c=C.getBroadcastDims(e.shape,t),p=u-o,d="";if(o===0)return n?`
fn ${i}Index(globalIndex : i32) -> vec4<f32> {
return get${a}();
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
fn ${i}Coords(coords : ${l}) -> vec4<f32> {
return get${a}();
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
`:`
fn ${i}Index(globalIndex : i32) -> f32{
return get${a}();
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
fn ${i}Coords(coords : ${l}) -> f32{
return get${a}();
2022-04-18 17:29:45 +02:00
}
2022-05-24 13:28:51 +02:00
`;u<2&&c.length>=1?d="coords = 0;":d=c.map(g=>`coords.${pr(g+p)} = 0;`).join(`
2022-05-30 03:12:18 +02:00
`);let h="";if(u<2&&o>0)h="coords";else if(u>1){let g=Wt(o),b=e.shape.map((y,v)=>`coords.${pr(v+p)}`).join(", ");h=`${g}(${b})`}else h="coords";let f=`uniforms.${r.charAt(0).toLowerCase()+r.slice(1)}Shape`,m=`${o}D`;return n?`
2022-04-21 15:39:40 +02:00
fn ${i}Index(globalIndex : i32) -> vec4<f32> {
var coords = getCoordsFromIndex(globalIndex);
${d}
return ${r}[getIndexFromCoords${m}(${h}, ${f}) / 4];
2022-04-18 17:29:45 +02:00
}
2022-04-21 15:39:40 +02:00
fn ${i}Coords(coordsIn : ${l}) -> vec4<f32> {
var coords = coordsIn;
${d}
return ${r}[getIndexFromCoords${m}(${h}, ${f}) / 4];
}
`:`
fn ${i}Index(globalIndex : i32) -> f32 {
var coords = getCoordsFromIndex(globalIndex);
${d}
return f32(${r}[getIndexFromCoords${m}(${h}, ${f})]);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn ${i}Coords(coordsIn : ${l}) -> f32 {
var coords = coordsIn;
${d}
return f32(${r}[getIndexFromCoords${m}(${h}, ${f})]);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}function Kne(e,t){let{x:n,y:s=[],z:r=[]}=t,a=e.length;if(n.length===a)return[`fn getOutputCoords() -> ${Wt(a)}{
2022-04-21 15:39:40 +02:00
let globalIndex = getGlobalIndex();
return getCoordsFromIndex(globalIndex);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`,a];let i="",o=[n,s,r],u=0;for(let d=0;d<o.length;d++){let h=o[d];if(h.length!==0)if(u+=h.length,h.length===1)i+=`let d${h[0]} = i32(globalId[${d}]);`;else{let f=Vne(h,"uniforms.outShape");i+=`var index${d} = i32(globalId[${d}]);`;for(let m=0;m<f.length;m++)i+=`let d${h[m]} = index${d} / ${f[m]};`,m===f.length-1?i+=`let d${h[m+1]} = index${d} - d${h[m]} * ${f[m]};`:i+=`index${d} = index${d} - d${h[m]} * ${f[m]};`}}let l=[];for(let d=0;d<u;d++)l.push(`d${d}`);let c=Wt(u),p=`fn getOutputCoords() -> ${c} {
2022-04-21 15:39:40 +02:00
${i}
2022-05-30 03:12:18 +02:00
`;return l.length===0?p+=`return ${c}(0); }`:p+=`return ${c}(${l.join(",")}); }`,[p,u]}function Pw(e){let t=e.length;if(t<=1)return"fn getCoordsFromIndex(index : i32) -> i32 { return index; }";let n=w.computeStrides(e),s=Wt(t),r=[];for(let i=0;i<t;i++)r.push(`d${i}`);if(n.length===1)return` fn getCoordsFromIndex(index : i32) -> vec2<i32> {
2022-04-21 15:39:40 +02:00
let d0 = index / uniforms.outShapeStrides; let d1 = index - d0 * uniforms.outShapeStrides;
return vec2<i32>(d0, d1);
2022-05-24 13:28:51 +02:00
}`;let a;return a="var index2 = index;"+n.map((i,o)=>{let u=`let ${r[o]} = index2 / uniforms.outShapeStrides.${pr(o)}`,l=o===n.length-1?`let ${r[o+1]} = index2 - ${r[o]} * uniforms.outShapeStrides.${pr(o)}`:`index2 = index2 - ${r[o]} * uniforms.outShapeStrides.${pr(o)}`;return`${u}; ${l};`}).join(""),`
2022-04-21 15:39:40 +02:00
fn getCoordsFromIndex(index : i32) -> ${s} {
${a}
return ${s}(${r.join(",")});
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}var E2={};Ee(E2,{ArrayBufferToTypedArray:()=>D2,GPUBytesPerElement:()=>md,computeDispatch:()=>_e,computeWorkGroupSizeForConv2d:()=>Dv,computeWorkGroupSizeForMatMul:()=>R2,computeWorkPerThreadForConv2d:()=>Fv,flatDispatchLayout:()=>Ve,isWebGPUSupported:()=>Ov,tilesFitEvenlyIntoShape:()=>il});var ra=e=>{let t=1;for(let n=0;n<e.length;n++)t*=e[n];return t};function il(e,t){if(e.length!==t.length)throw new Error(`Cannot compute whether rank ${e.length} tiles fit evenly into rank ${t.length} shape - ranks must match.`);return t.every((n,s)=>n%e[s]===0)}function _e(e,t,n=[1,1,1],s=[1,1,1]){let[r,a,i]=[Math.ceil(ra(e.x.map(o=>t[o]))/(n[0]*s[0])),e.y?Math.ceil(ra(e.y.map(o=>t[o]))/(n[1]*s[1])):1,e.z?Math.ceil(ra(e.z.map(o=>t[o]))/(n[2]*s[2])):1];return[r,a,i]}function Dv(e,t){let n=ra(e.x.map(r=>t[r])),s=ra(e.y.map(r=>t[r]));return n<=4?[4,16,1]:s<=4?[16,4,1]:[16,16,1]}function R2(e,t,n){return e===1?[32,1,1]:n===1?[1,32,1]:[8,8,1]}function Fv(e,t){let n=ra(e.x.map(r=>t[r])),s=ra(e.y.map(r=>t[r]));return n<=4?[1,2,1]:s<=4?[2,1,1]:[2,2,1]}function Ve(e){return{x:e.map((t,n)=>n)}}function md(e){if(e==="float32"||e==="int32"||e==="bool"||e==="string")return 4;if(e==="complex64")return 8;throw new Error(`Unknown dtype ${e}`)}function D2(e,t){if(t==="float32")return new Float32Array(e);if(t==="int32")return new Int32Array(e);if(t==="bool"||t==="string")return Uint8Array.from(new Int32Array(e));throw new Error(`Unknown dtype ${t}`)}function Ov(){return(typeof window!="undefined"||typeof WorkerGlobalScope!="undefined")&&!!navigator.gpu}function F2(e,t,n,s,r=4){return w.assert((s%4===0||s%3===0)&&e[0]===4&&(r===3||r===4),()=>`tileInner must be divisible by 4|3. ColPerThread must be 4.
2022-05-22 14:50:51 +02:00
innerElementSize must be 3|4.`),`
var<workgroup> mm_Asub : array<array<vec${r}<f32>, ${s/r}>, ${t}>;
2022-04-21 15:39:40 +02:00
var<workgroup> mm_Bsub : array<array<vec4<f32>, ${n/e[0]}>, ${s}>;
let RowPerThread = ${e[1]};
let ColPerThread = ${e[0]};
2022-05-22 14:50:51 +02:00
let InnerElementSize = ${r};
2022-04-21 15:39:40 +02:00
let TileInner = ${s};
2022-05-24 13:28:51 +02:00
${du()}
2022-04-21 15:39:40 +02:00
let tileRow = ${t===1?"0":"i32(localId.y) * RowPerThread"};
let tileCol = i32(localId.x);
let globalRow = ${t===1?"0":"i32(globalId.y) * RowPerThread"};
let globalCol = i32(globalId.x);
let numTiles = (uniforms.dimInner - 1) / TileInner + 1;
var acc: array<vec4<f32>, RowPerThread>;
var BCached : array<vec4<f32>, 4>;
// Loop over shared dimension.
var globalColA = tileCol;
let RowPerThreadB = TileInner / i32(workGroupSizeY);
let tileRowB = i32(localId.y) * RowPerThreadB;
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
let inputRow = tileRow + innerRow;
let inputCol = tileCol;
mm_Asub[inputRow][inputCol] = mm_readA(globalRow + innerRow, globalColA, globalId);
}
2022-05-22 14:50:51 +02:00
globalColA = globalColA + TileInner / InnerElementSize;
2022-04-21 15:39:40 +02:00
// Load one tile of B into local memory.
for (var innerRow = 0; innerRow < RowPerThreadB; innerRow = innerRow + 1) {
let inputRow = tileRowB + innerRow;
let inputCol = tileCol;
mm_Bsub[inputRow][inputCol] = mm_readB(t * TileInner + inputRow, globalCol, globalId);
}
workgroupBarrier();
// Compute acc values for a single thread.
2022-05-22 14:50:51 +02:00
for (var k = 0; k < TileInner / InnerElementSize; k = k + 1) {
BCached[0] = mm_Bsub[k * InnerElementSize][tileCol];
BCached[1] = mm_Bsub[k * InnerElementSize + 1][tileCol];
BCached[2] = mm_Bsub[k * InnerElementSize + 2][tileCol];
${r===3?"":"BCached[3] = mm_Bsub[k * InnerElementSize + 3][tileCol];"}
2022-04-21 15:39:40 +02:00
for (var i = 0; i < RowPerThread; i = i + 1) {
2022-05-22 14:50:51 +02:00
let ACached = mm_Asub[tileRow + i][k];
2022-04-21 15:39:40 +02:00
acc[i] = BCached[0] * ACached.x + acc[i];
acc[i] = BCached[1] * ACached.y + acc[i];
acc[i] = BCached[2] * ACached.z + acc[i];
2022-05-22 14:50:51 +02:00
${r===3?"":"acc[i] = BCached[3] * ACached.w + acc[i];"}
2022-04-21 15:39:40 +02:00
}
}
workgroupBarrier();
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
mm_write(globalRow + innerRow,
globalCol,
acc[innerRow], globalId);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
}`}var Xne=class{constructor(e,t,n,s,r,a=null,i=null,o=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.workGroupSize=[8,8,1],this.isVec4=!0,this.outputShape=t,this.dispatchLayout={x:[2],y:[1],z:[0]},t[1]===1?this.elementsPerThread=[4,1,1]:this.elementsPerThread=[4,4,1],this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread);let u=a!=null,l=o!=null;u&&this.variableNames.push("bias"),l&&this.variableNames.push("preluActivationWeights"),this.tileAOuter=t[1]===1?1:this.workGroupSize[1]*this.elementsPerThread[1],this.tileBOuter=this.workGroupSize[0]*this.elementsPerThread[0],this.tileInner=this.tileBOuter,this.aShape=e,this.addBias=u,this.activation=i,this.hasPreluActivationWeights=l,this.batchAEqualOne=s,this.batchBEqualOne=r,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`matMulPackedVec4_${this.activation}_${this.fitA}_${this.fitB}_${this.elementsPerThread}_${this.batchAEqualOne}_${this.batchBEqualOne}`}getShapeFit(){let e=this.aShape[2],t=this.outputShape[2],n=[this.outputShape[0],e,t],s=[this.tileAOuter,this.tileInner],r=[this.tileInner,this.tileBOuter];return[il(s,this.aShape.slice(1)),il(r,n.slice(1))]}getUserCode(){let e=this.fitA?"return A[batch * batchASize + row * uniforms.dimInner / 4 + col]":`if (coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
2022-04-21 15:39:40 +02:00
return A[batch * batchASize + row * uniforms.dimInner / 4 + col];
}
return vec4<f32>(0.0)`,t=this.fitB?"return B[batch * batchBSize + row * uniforms.dimBOuter / 4 + col]":`if(coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B[batch * batchBSize + row * uniforms.dimBOuter / 4 + col];
}
2022-05-24 13:28:51 +02:00
return vec4<f32>(0.0)`,n="",s="";if(this.activation){let i=Or(this.activation,this.isVec4);this.hasPreluActivationWeights?n=`fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`
fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
${i}
}`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
${this.batchAEqualOne?`
let batchASize = 0;
let batch = 0;
`:`
let batchASize = uniforms.aShape[1] * uniforms.aShape[2] / 4;
let batch = i32(globalId.z);
`}
${e};
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
${this.batchBEqualOne?`
let batchBSize = 0;
let batch = 0;
`:`
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2] / 4;
let batch = i32(globalId.z);
`}
${t};
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(row : i32, col : i32, valueIn : vec4<f32>, globalId : vec3<u32>) {
if (row < uniforms.aShape[1] && col * 4 < uniforms.bShape[2])
{
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col * 4);
${r}
${s}
setOutputAtCoords(outCoord[0], outCoord[1], outCoord[2], value);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
${F2(this.elementsPerThread,this.tileAOuter,this.tileBOuter,this.tileInner)}
`}},Yne=e=>e?`
2022-05-24 13:28:51 +02:00
mm_Asub[inputRow][inputCol] = mm_readA(
t * TileInner + inputRow,
globalRowStart + inputCol, globalId);
`:`
mm_Asub[inputRow][inputCol] = mm_readA(
globalRowStart + inputRow,
t * TileInner + inputCol, globalId);
2022-05-30 03:12:18 +02:00
`,Qne=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];";function Pv(e,t,n=!1,s=32){let r=e[1]*t[1],a=e[0]*t[0],i=n?r:s,o=n?s:r;w.assert(o%t[1]===0&&i%t[0]===0&&s%t[1]===0,()=>`tileAHight ${o} must be divisible by workGroupSize[1]${t[1]}, tileAWidth ${i} must be divisible by workGroupSize[0]${t[0]}, tileInner ${s} must be divisible by workGroupSize[1]${t[1]}`);let u=o/t[1],l=i/t[0],c=s/t[1];return`
2022-05-24 13:28:51 +02:00
var<workgroup> mm_Asub : array<array<f32, ${i}>, ${o}>;
var<workgroup> mm_Bsub : array<array<f32, ${a}>, ${s}>;
let RowPerThread = ${e[1]};
let ColPerThread = ${e[0]};
let TileInner = ${s};
@stage(compute) @workgroup_size(workGroupSizeX, workGroupSizeY, workGroupSizeZ)
fn main(@builtin(local_invocation_id) LocalId : vec3<u32>,
@builtin(global_invocation_id) GlobalId : vec3<u32>,
@builtin(num_workgroups) NumWorkgroups: vec3<u32>,
@builtin(workgroup_id) workgroupId: vec3<u32>) {
localId = LocalId;
globalId = GlobalId;
numWorkgroups = NumWorkgroups;
let tileRow = i32(localId.y) * RowPerThread;
let tileCol = i32(localId.x) * ColPerThread;
let globalRow = i32(globalId.y) * RowPerThread;
let globalCol = i32(globalId.x) * ColPerThread;
2022-04-21 15:39:40 +02:00
2022-05-24 13:28:51 +02:00
let globalRowStart = i32(workgroupId.y) * ${r};
2022-04-21 15:39:40 +02:00
2022-05-24 13:28:51 +02:00
let numTiles = (uniforms.dimInner - 1) / TileInner + 1;
2022-04-21 15:39:40 +02:00
2022-05-24 13:28:51 +02:00
var acc : array<array<f32, ColPerThread>, RowPerThread>;
2022-04-21 15:39:40 +02:00
// Without this initialization strange values show up in acc.
2022-05-24 13:28:51 +02:00
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ColPerThread; innerCol = innerCol + 1) {
2022-04-21 15:39:40 +02:00
acc[innerRow][innerCol] = 0.0;
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
2022-05-24 13:28:51 +02:00
let tileRowA = i32(localId.y) * ${u};
let tileColA = i32(localId.x) * ${l};
let tileRowB = i32(localId.y) * ${c};
2022-04-21 15:39:40 +02:00
// Loop over shared dimension.
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
2022-05-24 13:28:51 +02:00
for (var innerRow = 0; innerRow < ${u}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${l}; innerCol = innerCol + 1) {
let inputRow = tileRowA + innerRow;
2022-04-21 15:39:40 +02:00
let inputCol = tileColA + innerCol;
2022-05-30 03:12:18 +02:00
${Yne(n)}
2022-04-21 15:39:40 +02:00
}
}
2022-05-24 13:28:51 +02:00
2022-04-21 15:39:40 +02:00
// Load one tile of B into local memory.
2022-05-24 13:28:51 +02:00
for (var innerRow = 0; innerRow < ${c}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ColPerThread; innerCol = innerCol + 1) {
2022-04-21 15:39:40 +02:00
let inputRow = tileRowB + innerRow;
let inputCol = tileCol + innerCol;
mm_Bsub[inputRow][inputCol] = mm_readB(
2022-05-24 13:28:51 +02:00
t * ${s} + inputRow,
2022-04-21 15:39:40 +02:00
globalCol + innerCol, globalId);
}
}
workgroupBarrier();
// Compute acc values for a single thread.
2022-05-24 13:28:51 +02:00
var BCached : array<f32, ColPerThread>;
for (var k = 0; k < TileInner; k = k + 1) {
for (var inner = 0; inner < ColPerThread; inner = inner + 1) {
2022-04-21 15:39:40 +02:00
BCached[inner] = mm_Bsub[k][tileCol + inner];
}
2022-05-24 13:28:51 +02:00
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
2022-05-30 03:12:18 +02:00
${Qne(n)}
2022-05-24 13:28:51 +02:00
for (var innerCol = 0; innerCol < ColPerThread; innerCol = innerCol + 1) {
2022-04-21 15:39:40 +02:00
acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
}
}
}
workgroupBarrier();
}
2022-05-24 13:28:51 +02:00
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ColPerThread; innerCol = innerCol + 1) {
2022-04-21 15:39:40 +02:00
if ((globalCol + innerCol) < uniforms.dimBOuter &&
(globalRow + innerRow) < uniforms.dimAOuter) {
mm_write(globalRow + innerRow,
globalCol + innerCol,
acc[innerRow][innerCol], globalId);
}
}
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}var Zne=e=>e?`
2022-05-24 13:28:51 +02:00
mm_readA(colA, globalRow, globalId),
mm_readA(colA + 1, globalRow, globalId),
mm_readA(colA + 2, globalRow, globalId),
mm_readA(colA + 3, globalRow, globalId)
`:`
mm_readA(globalRow, colA, globalId),
mm_readA(globalRow, colA + 1, globalId),
mm_readA(globalRow, colA + 2, globalId),
mm_readA(globalRow, colA + 3, globalId)
2022-05-30 03:12:18 +02:00
`;function Jne(e,t=!1){return w.assert(e[1]===1&&e[2]===1,()=>`A linear work group size is required. But got ${e}.`),`
2022-04-21 15:39:40 +02:00
let TileSize = ${e[0]*4};
var<workgroup> mm_Asub : array<vec4<f32>, ${e[0]}>;
2022-05-24 13:28:51 +02:00
${du()}
2022-04-21 15:39:40 +02:00
let tileCol = i32(localId.x);
let globalCol = i32(globalId.x);
let globalRow = i32(globalId.y);
let numTiles = (uniforms.dimInner - 1) / TileSize + 1;
// Without this initialization strange values show up in acc.
var acc = 0.0;
// Loop over shared dimension.
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
let colA = t * TileSize + tileCol * 4;
2022-05-30 03:12:18 +02:00
mm_Asub[tileCol] = vec4<f32>(${Zne(t)});
2022-04-21 15:39:40 +02:00
workgroupBarrier();
// Compute acc values for a single thread.
for (var k = 0; k < TileSize / 4; k = k + 1) {
let rowB = t * TileSize + k * 4;
let BCached = vec4<f32>(mm_readB(rowB, globalCol, globalId),
mm_readB(rowB + 1, globalCol, globalId),
mm_readB(rowB + 2, globalCol, globalId),
mm_readB(rowB + 3, globalCol, globalId));
let ACached = mm_Asub[k];
acc = acc + dot(ACached, BCached);
}
workgroupBarrier();
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (globalRow < uniforms.dimAOuter && globalCol < uniforms.dimBOuter) {
mm_write(globalRow, globalCol, acc, globalId);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}var ese=class{constructor(e,t,n,s,r,a=!1,i=!1,o=null,u=null,l=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.workGroupSize=[16,16,1],this.outputShape=t,this.dispatchLayout={x:[2],y:[1],z:[0]};let c=a?e[1]:e[2];this.workGroupSize=R2(t[1],c,t[2]),(t[1]===1||t[2]===1)&&(n=1),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[n,n,1]),w.arraysEqual(this.dispatch,[1,1,1])&&(n=1,this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[n,n,1]));let p=o!=null,d=l!=null;p&&this.variableNames.push("bias"),d&&this.variableNames.push("preluActivationWeights"),this.workPerThread=n,this.transposeA=a,this.transposeB=i,this.addBias=p,this.activation=u,this.hasPreluActivationWeights=d,this.batchAEqualOne=s,this.batchBEqualOne=r,[this.fitAOuter,this.fitBOuter,this.fitInner]=this.getShapeFit(t[1],t[2],c),this.shaderKey=`matMulPacked_${this.workPerThread}_${a}_${i}_${this.activation}_${this.fitAOuter}_${this.fitBOuter}_${this.fitInner}_${this.outputShape[1]>1}_${this.batchAEqualOne}_${this.batchBEqualOne}`}getShapeFit(e,t,n){let s=this.workGroupSize[1]*this.workPerThread,r=this.workGroupSize[0]*this.workPerThread;this.tileInner=32,this.outputShape[1]===1&&(this.tileInner=this.workGroupSize[0]*4);let a=e%s===0,i=t%r===0,o=n%this.tileInner===0;return[a,i,o]}getUserCode(){let e=this.fitAOuter&&this.fitInner?"return A[batch * batchASize + row * uniforms.aShape[2] + col];":`
2022-05-24 13:28:51 +02:00
if(row < uniforms.aShape[1] && col < uniforms.aShape[2]) {
return A[batch * batchASize + row * uniforms.aShape[2] + col];
}
return 0.0;
`,t;this.transposeB===!1?t="return B[batch * batchBSize + row * uniforms.dimBOuter + col];":t="return B[batch * batchBSize + col * uniforms.dimInner + row];";let n="",s="";if(this.activation){let i=Or(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${i}
}
`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
${this.batchAEqualOne?`
let batch = 0;
let batchASize = 0;
`:`
let batch = i32(globalId.z);
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
`}
${e}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
${this.batchBEqualOne?`
let batch = 0;
let batchBSize = 0;
`:`
let batch = i32(globalId.z);
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
`}
${t}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(row : i32, col : i32, valueIn : f32, globalId : vec3<u32>) {
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col);
${r}
${s}
setOutputAtCoords(batch, row, col, value);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
${this.outputShape[1]>1?Pv([this.workPerThread,this.workPerThread,1],this.workGroupSize,this.transposeA,this.tileInner):Jne(this.workGroupSize,this.transposeA)}
`}};function tse(){return`
2022-04-21 15:39:40 +02:00
var<workgroup> sumValues : array<f32, workGroupSizeX>;
2022-05-24 13:28:51 +02:00
${du()}
2022-04-21 15:39:40 +02:00
let coords = getOutputCoords();
let batch = coords[0];
let row = coords[1];
let col = coords[2];
var sum = 0.0;
let Length = uniforms.dimInner;
for (var k = i32(localId.x); k < Length; k = k + i32(workGroupSizeX)) {
let dataA = mm_readA(batch, row, k);
let dataB = mm_readB(batch, k, col);
sum = sum + dataA * dataB;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
sumValues[localId.x] = sum;
workgroupBarrier();
for(var currentSize = workGroupSizeX / 2u; currentSize > 1u;
currentSize = currentSize / 2u) {
if (localId.x < currentSize)
{
sumValues[localId.x] = sumValues[localId.x] + sumValues[localId.x + currentSize];
}
workgroupBarrier();
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (localId.x == 0u) {
sum = sumValues[0] + sumValues[1];
mm_write(batch, row, col, sum);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}var nse=class{constructor(e,t,n,s=!1,r=!1,a=null,i=null,o=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.workGroupSize=[256,1,1],this.outputShape=e,this.dispatchLayout={x:[],y:[1,2],z:[0]},this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize);let u=a!=null,l=o!=null;u&&this.variableNames.push("bias"),l&&this.variableNames.push("preluActivationWeights"),this.transposeA=s,this.transposeB=r,this.addBias=u,this.activation=i,this.hasPreluActivationWeights=l,this.batchAEqualOne=t,this.batchBEqualOne=n,this.shaderKey=`matMulReduce_${this.activation}_${s}_${r}_${this.batchAEqualOne}_${this.batchBEqualOne}`}getUserCode(){let e;this.transposeA===!1?e="return f32(A[batch * batchASize + row * uniforms.dimInner + col]);":e="return f32(A[batch * batchASize + col * uniforms.dimAOuter + row]);";let t;this.transposeB===!1?t="return f32(B[batch * batchBSize + row * uniforms.dimBOuter + col]);":t="return f32(B[batch * batchBSize + col * uniforms.dimInner + row]);";let n="",s="";if(this.activation){let i=Or(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${i}
}
`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(batchIn: i32, row : i32, col : i32) -> f32 {
${this.batchAEqualOne?`
let batchASize = 0;
let batch = 0;
`:`
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
let batch = batchIn;
`}
${e}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(batchIn: i32, row : i32, col : i32) -> f32 {
${this.batchBEqualOne?`
let batch = 0;
let batchBSize = 0;
`:`
let batch = batchIn;
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
`}
${t}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(batch: i32, row : i32, col : i32, valueIn : f32) {
var value = valueIn;
let outCoord = vec3<i32>(batch, row, col);
${r}
${s}
setOutputAtCoords(batch, row, col, value);
}
2022-05-30 03:12:18 +02:00
${tse()}
`}};function sse(e){let t=e[1]/2,n=e[0],s=t>n?t:n;return`
2022-04-21 15:39:40 +02:00
var<workgroup> mm_Asub1 : array<array<f32, ${s}>, ${t}>;
var<workgroup> mm_Bsub1 : array<array<f32, ${n}>, ${s}>;
var<workgroup> mm_Asub2 : array<array<f32, ${s}>, ${t}>;
var<workgroup> mm_Bsub2 : array<array<f32, ${n}>, ${s}>;
// If the output size is small for matrix multiplication, avoid to use vec4
// and handle some elements per thread to optimally utilize the ALU.
// Introduces two shared memory buffers, some logical threads could handle
// arithmetic operations and others handle IO operations between barrier api,
// makes ALUs and load/store units work simultaneously, could improves
// the performance.
2022-05-24 13:28:51 +02:00
${du()}
2022-04-21 15:39:40 +02:00
let tileRow = i32(localId.y);
let tileCol = i32(localId.x);
let globalRow = i32(globalId.y);
let globalCol = i32(globalId.x);
// uniforms.dimInner should be greater than 0.
let numTiles = (uniforms.dimInner - 1) / ${s} + 1;
var acc = 0.0;
var globalColA = tileCol;
var globalRowB = tileRow;
for (var t = 0; t < numTiles; t = t + 1) {
if (t == 0) {
if (tileRow < ${t}) {
// Load one tile of A and B into local memory.
// globalRow is always greater than or equal tileRow.
mm_Asub1[tileRow][tileCol] =
mm_readA((globalRow - tileRow) / 2 + tileRow, globalColA, globalId);
globalColA = globalColA + ${s};
mm_Bsub1[tileRow][tileCol] = mm_readB(globalRowB, globalCol, globalId);
globalRowB = globalRowB + ${s};
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
} else {
if (tileRow < ${t}) {
// Load one tile of A and B into local memory.
// globalRow is always greater than or equal tileRow.
mm_Asub1[tileRow][tileCol] =
mm_readA((globalRow - tileRow) / 2 + tileRow, globalColA, globalId);
globalColA = globalColA + ${s};
mm_Bsub1[tileRow][tileCol] = mm_readB(globalRowB, globalCol, globalId);
globalRowB = globalRowB + ${s};
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
// Compute acc values for a single thread.
for (var k = 0; k < ${s}; k = k + 1) {
let subRow = tileRow - ${t};
if (subRow < 0) {
continue;
}
acc = acc + mm_Asub2[subRow][k] * mm_Bsub2[k][tileCol];
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
workgroupBarrier();
if (t != 0) {
t = t + 1;
}
if (t < numTiles) {
if (tileRow < ${t}) {
// Load one tile of A and B into local memory.
// globalRow is always greater than or equal tileRow.
mm_Asub2[tileRow][tileCol] =
mm_readA((globalRow - tileRow) / 2 + tileRow, globalColA, globalId);
globalColA = globalColA + ${s};
mm_Bsub2[tileRow][tileCol] = mm_readB(globalRowB, globalCol, globalId);
globalRowB = globalRowB + ${s};
2022-04-21 15:38:36 +02:00
} else {
2022-04-21 15:39:40 +02:00
// Compute acc values for a single thread.
for (var k = 0; k < ${s}; k = k + 1) {
let subRow = tileRow - ${t};
if (subRow < 0) {
continue;
}
acc = acc + mm_Asub1[subRow][k] * mm_Bsub1[k][tileCol];
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
workgroupBarrier();
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
let writeCol = (globalRow - tileRow) / 2 + tileRow - ${t};
if (tileRow >= ${t} && writeCol >= 0) {
mm_write(writeCol, globalCol, acc, globalId);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}var rse=class{constructor(e,t,n,s=null,r=null,a=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.workGroupSize=[8,16,1],w.assert(e[1]<=16||t[2]<=16,()=>"This program can be only used when A width or B Height are small"),this.outputShape=n,this.dispatchLayout={x:[2],y:[1],z:[0]},this.dispatch=[Math.ceil(n[2]/this.workGroupSize[0]),Math.ceil(n[1]*2/this.workGroupSize[1]),n[0]];let i=s!=null;i&&this.variableNames.push("bias");let o=a!=null;o&&this.variableNames.push("preluActivationWeights"),this.addBias=i,this.activation=r,this.hasPreluActivationWeights=o,this.batchAEqualOne=e[0]===1,this.batchBEqualOne=t[0]===1,this.shaderKey=`matMulSmallOutputSize_${this.activation}_${this.batchAEqualOne}_${this.batchBEqualOne}`}getUserCode(){let e=`if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
2022-04-21 15:39:40 +02:00
return A[batch * batchASize + row * uniforms.dimInner + col];
}
return 0.0;`,t=`if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B[batch * batchBSize + row * uniforms.dimBOuter + col];
}
2022-05-24 13:28:51 +02:00
return 0.0;`,n="",s="";if(this.activation){let i=Or(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${i}
}`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
${this.batchAEqualOne?`
let batch = 0;
let batchASize = 0;
`:`
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
let batch = i32(globalId.z);
`}
${e}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
${this.batchBEqualOne?`
let batch = 0;
let batchBSize = 0;
`:`
let batch = i32(globalId.z);
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
`}
${t}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(row : i32, col : i32, valueIn : f32, globalId : vec3<u32>) {
if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimBOuter))) {
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col);
var value = valueIn;
${r}
${s}
setOutputAtCoords(batch, row, col, value);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
${sse(this.workGroupSize)}
`}};function Me(e){let{inputs:t,attrs:n}=e,{x:s}=t,{shape:r}=n,a=w.sizeFromShape(s.shape),i=w.inferFromImplicitShape(r,a),o=w.sizeFromShape(i);return w.assert(a===o,()=>`The new shape (${i}) has ${o} elements and the old shape (${s.shape}) has ${a} elements. The new shape and old shape must have the same number of elements.`),e.backend.incRef(s.dataId),{dataId:s.dataId,shape:i,dtype:s.dtype}}var ase={kernelName:Do,backendName:"webgpu",kernelFunc:Me};function zv({a:e,b:t,transposeA:n,transposeB:s,backend:r,bias:a=null,preluActivationWeights:i=null,leakyreluAlpha:o=0,activation:u=null}){let l=e.shape.length,c=t.shape.length,p=n?e.shape[l-2]:e.shape[l-1],d=s?t.shape[c-1]:t.shape[c-2],h=n?e.shape[l-1]:e.shape[l-2],f=s?t.shape[c-2]:t.shape[c-1],m=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(m),y=w.sizeFromShape(g),x=Xo.assertAndGetBroadcastShape(e.shape.slice(0,-2),t.shape.slice(0,-2)).concat([h,f]);w.assert(p===d,()=>`Error in matMul: inner shapes (${p}) and (${d}) of Tensors with shapes ${e.shape} and ${t.shape} and transposeA=${n} and transposeB=${s} must match.`);let k=n?[b,p,h]:[b,h,p],I=s?[y,f,d]:[y,d,f],$=Me({inputs:{x:e},backend:r,attrs:{shape:k}}),R=Me({inputs:{x:t},backend:r,attrs:{shape:I}}),E=[$,R],P=Math.max(b,y),A=b===1,O=y===1,T=p%4===0&&f%4===0&&!n&&!s,M;h*f<=32?M=new nse([P,h,f],A,O,n,s,a,u,i):!n&&!s&&(h<=16&&(f<=512||d>=2*f)||f<=16&&(h<=512||p>=2*h))?M=new rse(k,I,[P,h,f],a,u,i):T?M=new Xne(k,[P,h,f],K().get("WEBGPU_MATMUL_WORK_PER_THREAD"),A,O,a,u,i):M=new ese(k,[P,h,f],K().get("WEBGPU_MATMUL_WORK_PER_THREAD"),A,O,n,s,a,u,i);let W=[$,R];a&&W.push(a),i&&W.push(i);let j=[{type:"int32",data:[h]},{type:"int32",data:[f]},{type:"int32",data:[p]}];u==="leakyrelu"&&(j.push({type:"float32",data:[o]}),M.uniforms+=" alpha : f32,");let Y=r.runWebGPUProgram(M,W,e.dtype,j),X=Me({inputs:{x:Y},backend:r,attrs:{shape:x}});E.push(Y);for(let Z of E)r.disposeData(Z.dataId);return X}function ise(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a,bias:i,preluActivationWeights:o}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=s;return zv({a:r,b:a,transposeA:u,transposeB:l,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:p,activation:c})}var ose={kernelName:ia,backendName:"webgpu",kernelFunc:ise},zw=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.workGroupSize=[128,1,1],this.size=!0,this.outputShape=C.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`binaryOpComplex_${e}`,this.op=e}getUserCode(){return`
2022-04-21 15:39:40 +02:00
fn binaryOpComplex(
areal : f32, aimag : f32, breal : f32, bimag : f32) -> f32 {
2022-05-22 14:50:51 +02:00
${rc(this.op,!1)}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${Ue()}
if(index < uniforms.size) {
let areal = getARealByOutputIndex(index);
let aimag = getAImagByOutputIndex(index);
let breal = getBRealByOutputIndex(index);
let bimag = getBImagByOutputIndex(index);
setOutputAtIndex(index, binaryOpComplex(areal, aimag, breal, bimag));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
2022-05-30 03:12:18 +02:00
`}},use=class{constructor(e,t,n,s){this.variableNames=["A","B"],this.size=!0;let r=256;this.workGroupSize=[r,1,1],this.outputShape=C.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Ve(this.outputShape),this.lastDimensionSize=s?n[0]:t[0],this.lastDimensionSize<256?this.workPerThread=1:this.lastDimensionSize<512?this.workPerThread=2:this.workPerThread=4,this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.useSharedMemoryWithB=s,this.op=e,this.shaderKey=`binaryShared_${e}_${this.lastDimensionSize}_${this.useSharedMemoryWithB}`}getUserCode(){let e=this.lastDimensionSize>1?`coords[${this.outputShape.length-1}]`:"0",t=this.useSharedMemoryWithB?`let a = getAByOutputCoords(coords);
2022-04-21 15:39:40 +02:00
let b = sharedBuf[${e}];`:`let a = sharedBuf[${e}];
let b = getBByOutputCoords(coords);`;return`
fn binaryOperation(a : f32, b : f32) -> f32 {
2022-05-22 14:50:51 +02:00
${rc(this.op,!1)}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
var<workgroup> sharedBuf : array<f32, ${this.lastDimensionSize}>;
${Ue()}
// Fill in the shared memory buffer. Here we need a loop to make sure
// that all data in A|B are uploaded when |sharedMemorySize| is larger
// than work group size.
for(var localIndex = i32(localId.x); localIndex < ${this.lastDimensionSize}; localIndex = localIndex + ${this.workGroupSize[0]}) {
sharedBuf[localIndex] = f32(${this.useSharedMemoryWithB?"B":"A"}[localIndex]);
}
workgroupBarrier();
for(var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if(flatIndex < uniforms.size) {
let coords = getCoordsFromIndex(flatIndex);
${t}
setOutputAtIndex(flatIndex, binaryOperation(a, b));
}
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},lse=class{constructor(e,t,n){this.variableNames=["A","B"],this.workPerThread=4,this.isVec4=!0,this.size=!0;let s=128;this.workGroupSize=[s,1,1],this.outputShape=C.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.op=e,this.shaderKey=`binaryVec4_${e}`}getUserCode(){return`
2022-04-21 15:39:40 +02:00
fn binaryOperation(a : vec4<f32>, b : vec4<f32>) -> vec4<f32> {
2022-05-22 14:50:51 +02:00
${rc(this.op,this.isVec4)}
2022-04-21 15:39:40 +02:00
}
${Ue()}
if (index < uniforms.size) {
let a = getAByOutputIndex(index);
let b = getBByOutputIndex(index);
setOutputAtIndex(index, binaryOperation(a, b));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
2022-05-30 03:12:18 +02:00
`}},O2=class{constructor(e,t,n){this.variableNames=["A","B"],this.size=!0;let s=128;this.workGroupSize=[s,1,1],this.outputShape=C.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`binary_${e}`,this.op=e}getUserCode(){return`
2022-04-21 15:39:40 +02:00
fn binaryOperation(a : f32, b : f32) -> f32 {
2022-05-22 14:50:51 +02:00
${rc(this.op,!1)}
2022-04-21 15:39:40 +02:00
}
${Ue()}
if (index < uniforms.size) {
let a = getAByOutputIndex(index);
let b = getBByOutputIndex(index);
setOutputAtIndex(index, binaryOperation(a, b));
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Mw(e,t,n){if(w.arraysEqual(t,n)&&w.sizeFromShape(t)%4===0)return new lse(e,t,n);let r=t.length===1&&n.length>1&&t[0]<1024,a=n.length===1&&t.length>1&&n[0]<1024;return r||a?new use(e,t,n,a):new O2(e,t,n)}function Wn(e){let{inputs:t}=e,{x:n}=t;return e.backend.incRef(n.dataId),{dataId:n.dataId,shape:n.shape,dtype:n.dtype}}var cse={kernelName:Wa,backendName:"webgpu",kernelFunc:Wn};function pu(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.makeTensorInfo(s.shape,"complex64"),i=n.tensorMap.get(a.dataId),o=Wn({inputs:{x:s},backend:n}),u=Wn({inputs:{x:r},backend:n});return i.complexTensorInfos={real:o,imag:u},a}var dse={kernelName:tp,backendName:"webgpu",kernelFunc:pu},ac=class{constructor(e,t){this.variableNames=["A"],this.size=!0;let n=128;this.workGroupSize=[n,1,1],this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.op=t,this.shaderKey=`unary_${t}`}getUserCode(){return`
2022-04-21 15:39:40 +02:00
fn unaryOperation(a : f32) -> f32 {
2022-05-24 13:28:51 +02:00
${jr(this.op,!1)}
2022-04-21 15:39:40 +02:00
}
${Ue()}
if (index < uniforms.size) {
let a = getAByOutputIndex(index);
setOutputAtIndex(index, unaryOperation(a));
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function jt({opType:e,cpuKernelImpl:t,dtype:n}){return({inputs:s,backend:r})=>{let{x:a}=s,i=r,o=n||a.dtype;if(i.shouldExecuteOnCPU([a])&&t!=null){let l=i.tensorMap.get(a.dataId),c=t(l.values,o);return i.makeTensorInfo(a.shape,o,c)}let u=new ac(a.shape,e);return i.runWebGPUProgram(u,[a],o)}}function mn({opSnippet:e,cpuKernelImpl:t,supportsComplex:n=!1,dtype:s}){return({inputs:r,backend:a})=>{let{a:i,b:o}=r,u=a;if(n&&i.dtype==="complex64"){let p=u.tensorMap.get(i.dataId),d=u.tensorMap.get(o.dataId),h,f;if(e!==0)[h,f]=[[p.complexTensorInfos.real,d.complexTensorInfos.real],[p.complexTensorInfos.imag,d.complexTensorInfos.imag]].map(g=>{let[b,y]=g,v={dataId:b.dataId,dtype:b.dtype,shape:i.shape},x={dataId:y.dataId,dtype:y.dtype,shape:o.shape},k=Mw(e,i.shape,o.shape);return u.runWebGPUProgram(k,[v,x],ln(b.dtype,y.dtype))});else{let g=new zw(17,i.shape,o.shape),b=new zw(18,i.shape,o.shape),y=[{dataId:p.complexTensorInfos.real.dataId,dtype:p.complexTensorInfos.real.dtype,shape:i.shape},{dataId:p.complexTensorInfos.imag.dataId,dtype:p.complexTensorInfos.imag.dtype,shape:i.shape},{dataId:d.complexTensorInfos.real.dataId,dtype:d.complexTensorInfos.real.dtype,shape:o.shape},{dataId:d.complexTensorInfos.imag.dataId,dtype:d.complexTensorInfos.imag.dtype,shape:o.shape}];h=u.runWebGPUProgram(g,y,"float32"),f=u.runWebGPUProgram(b,y,"float32")}let m=pu({inputs:{real:h,imag:f},backend:u});return u.disposeData(h.dataId),u.disposeData(f.dataId),m}let l=s||ln(i.dtype,o.dtype);if((i.dtype==="string"||o.dtype==="string"||u.shouldExecuteOnCPU([i,o]))&&t!=null){let p=u.tensorMap.get(i.dataId).values,d=u.tensorMap.get(o.dataId).values,h=i.dtype==="string"?C.fromUint8ToStringArray(p):p,f=i.dtype==="string"?C.fromUint8ToStringArray(d):d,[m,g]=t(i.shape,o.shape,h,f,l);return u.makeTensorInfo(g,l,m)}let c=Mw(e,i.shape,o.shape);return u.runWebGPUProgram(c,[i,o],l)}}var{addImpl:pse,ceilImpl:hse,concatImpl:fse,equalImpl:mse,expImpl:gse,expm1Impl:bse,floorImpl:yse,gatherNdImpl:vse,gatherV2Impl:xse,greaterEqualImpl:wse,greaterImpl:kse,lessEqualImpl:Sse,lessImpl:Ise,logImpl:Cse,maxImpl:Nse,maximumImpl:Tse,minimumImpl:$se,multiplyImpl:_se,negImpl:Ase,notEqualImpl:Ese,prodImpl:Rse,rangeImpl:Dse,rsqrtImpl:Fse,scatterImpl:Ose,simpleAbsImpl:Pse,sliceImpl:zse,stridedSliceImpl:Mse,stringNGramsImpl:Lse,subImpl:Bse,tileImpl:Vse,topKImpl:Wse,transposeImpl:Use,uniqueImpl:Nhe}=ov,Gse=jt({opType:0,cpuKernelImpl:Pse}),Hse={kernelName:lo,backendName:"webgpu",kernelFunc:Gse},qse=mn({opSnippet:1,cpuKernelImpl:pse,supportsComplex:!0}),jse={kernelName:Ir,backendName:"webgpu",kernelFunc:qse},Kse=class{constructor(e){this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e[0],this.variableNames=e.map((t,n)=>`T${n}`),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.shaderKey="addN"}getUserCode(){let e=[];this.variableNames.forEach(s=>{e.push(`let v${s} = get${s}ByOutputCoords(coords);`)});let t=this.variableNames.map(s=>`v${s}`).join(" + ");return`
2022-04-21 15:39:40 +02:00
${Ue()}
for (var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if (flatIndex < uniforms.size) {
let coords = getCoordsFromIndex(flatIndex);
${e.join(`
`)}
setOutputAtIndex(flatIndex, ${t});
}
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Xse(e){let{inputs:t,backend:n}=e,s=t;if(s.length===1)return Wn({inputs:{x:s[0]},backend:n});let r=s.map(o=>o.dtype).reduce((o,u)=>ln(o,u)),a=s.map(o=>o.shape),i=new Kse(a);return n.runWebGPUProgram(i,s,r)}var Yse={kernelName:Sa,backendName:"webgpu",kernelFunc:Xse},P2=class{constructor(e,t,n){this.workGroupSize=[64,1,1],this.variableNames=["x"],this.uniforms="infinityValue : f32,",this.size=!0;let s=[t];C.assertAxesAreInnerMostDims("arg"+n.charAt(0).toUpperCase()+n.slice(1),s,e.length),this.op=n==="min"?"<":">";let[r]=C.computeOutAndReduceShapes(e,s);this.outputShape=r.length===0?[1]:r,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,[1,1,1]),this.inputShape=e,this.shaderKey=`argMinMax${this.op}`}getUserCode(){let e=`
2022-04-21 15:39:40 +02:00
var<workgroup> xBestIndices : array<i32, ${this.workGroupSize[0]}>;
var<workgroup> xBestValues : array<f32, ${this.workGroupSize[0]}>;
2022-05-24 13:28:51 +02:00
`,t=()=>this.inputShape.length===1?"uniforms.xShape":`uniforms.xShape.${pr(this.inputShape.length-1)}`,n=()=>{let r="";if(this.outputShape.length===1)this.inputShape.length!==1&&(r+="outputCoords,");else for(let a=0;a<this.outputShape.length;a++)r+=`outputCoords.${pr(a)},`;return r};return`
2022-04-21 15:39:40 +02:00
fn DIV_CEIL(a : u32, b : u32) -> u32 {
return ((a - 1u) / b + 1u);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${e}
${Ue()}
let outputIndex = index / i32(workGroupSizeX);
2022-05-09 14:16:00 +02:00
let reduceLength = ${t()};
2022-04-21 15:39:40 +02:00
var bestIndex = i32(localId.x);
var bestValue = uniforms.infinityValue;
2022-05-09 14:16:00 +02:00
let outputCoords = getCoordsFromIndex(outputIndex);
for (var k = i32(localId.x); k < reduceLength && outputIndex < uniforms.size;
2022-04-21 15:39:40 +02:00
k = k + i32(workGroupSizeX)) {
2022-05-09 14:16:00 +02:00
let candidate = getX(${n()} k);
2022-04-21 15:39:40 +02:00
if (!isnan(candidate) && candidate ${this.op} bestValue) {
bestValue = candidate;
bestIndex = k;
}
}
xBestValues[localId.x] = bestValue;
xBestIndices[localId.x] = bestIndex;
workgroupBarrier();
2022-05-09 14:16:00 +02:00
var reduceSize = min(u32(reduceLength), workGroupSizeX);
2022-04-21 15:39:40 +02:00
for (var currentSize = reduceSize / 2u; reduceSize > 1u;
currentSize = reduceSize / 2u) {
let interval = DIV_CEIL(reduceSize, 2u);
if (localId.x < currentSize) {
let candidate = xBestValues[localId.x + interval];
if (candidate ${this.op} bestValue) {
bestValue = candidate;
xBestValues[localId.x] = bestValue;
xBestIndices[localId.x] = xBestIndices[localId.x + interval];
}
}
reduceSize = interval;
workgroupBarrier();
}
if (localId.x == 0u && outputIndex < uniforms.size) {
setOutputAtIndexI32(outputIndex, xBestIndices[localId.x]);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},Qse=class{constructor(e,t){this.variableNames=["A"],this.workGroupSize=[16,16,1];let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[t[s]];this.outputShape=n,this.dispatchLayout={x:[0],y:[1]},this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[1,1,1]),this.shaderKey="transposeShared"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
let TILE_DIM = ${this.workGroupSize[0]};
var<workgroup> tile : array<array<f32, ${this.workGroupSize[0]+1}>, ${this.workGroupSize[0]}>;
2022-05-30 03:12:18 +02:00
${Rv()}
2022-04-21 15:39:40 +02:00
fn main(@builtin(local_invocation_id) localId : vec3<u32>,
@builtin(workgroup_id) workgroupId : vec3<u32>) {
var x = i32(workgroupId.x) * TILE_DIM + i32(localId.x);
var y = i32(workgroupId.y) * TILE_DIM + i32(localId.y);
let width = uniforms.outShape[0];
let height = uniforms.outShape[1];
if (x < width && y < height) {
tile[localId.y][localId.x] = A[y * width + x];
}
workgroupBarrier();
x = i32(workgroupId.y) * TILE_DIM + i32(localId.x);
y = i32(workgroupId.x) * TILE_DIM + i32(localId.y);
if (x < height && y < width) {
setOutputAtIndex((y * height + x), tile[localId.x]
[localId.y]);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},Zse=class{constructor(e,t){this.variableNames=["A"],this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0;let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[t[s]];this.outputShape=n,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.newDim=t,this.shaderKey=`transpose_${t}`}getUserCode(){let e=Wt(this.outputShape.length),t=Jse(this.newDim);return`
2022-04-21 15:39:40 +02:00
${Ue()}
for(var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if(flatIndex < uniforms.size) {
let resRC = getCoordsFromIndex(flatIndex);
setOutputAtIndex(flatIndex, A[getIndexFromCoords${this.outputShape.length}D(
${e}(${t}), uniforms.aShape)]);
}
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function Jse(e){let t=e.length;if(t>6)throw Error(`Transpose for rank ${t} is not yet supported`);let n=new Array(t);for(let s=0;s<e.length;s++)n[e[s]]=`resRC.${pr(s)}`;return n.join()}function Ks(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{perm:a}=s,i=n,o=r.shape.length,u=new Array(o);for(let c=0;c<u.length;c++)u[c]=r.shape[a[c]];if(n.shouldExecuteOnCPU([r])){let p=i.tensorMap.get(r.dataId).values,d=Use(p,r.shape,r.dtype,a,u);return n.makeTensorInfo(u,r.dtype,d)}if(r.shape.length===2&&w.arraysEqual(a,[1,0])){let c=new Qse(r.shape,a);return i.runWebGPUProgram(c,[r],r.dtype)}let l=new Zse(r.shape,a);return i.runWebGPUProgram(l,[r],r.dtype)}var ere={kernelName:Hs,backendName:"webgpu",kernelFunc:Ks};function tre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=C.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=Ks({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=C.getInnerMostAxes(i.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMax",[i[0]],u.shape.length);let c=new P2(u.shape,i[0],"max"),p=[{type:"float32",data:[Number.NEGATIVE_INFINITY]}],d=n.runWebGPUProgram(c,[u],"int32",p);return l.forEach(h=>n.disposeData(h.dataId)),d}var nre={kernelName:Ia,backendName:"webgpu",kernelFunc:tre};function sre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=C.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=Ks({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=C.getInnerMostAxes(i.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMin",[i[0]],u.shape.length);let c=new P2(u.shape,i[0],"min"),p=[{type:"float32",data:[Number.POSITIVE_INFINITY]}],d=n.runWebGPUProgram(c,[u],"int32",p);return l.forEach(h=>n.disposeData(h.dataId)),d}var rre={kernelName:pl,backendName:"webgpu",kernelFunc:sre},z2=class{constructor(e,t){this.variableNames=["x"],this.uniforms="stride : vec2<i32>, pad : vec2<i32>, dilation : vec2<i32>, convDims : vec2<i32>, filterDims : vec2<i32>,",this.workGroupSize=[128,1,1],this.size=!0,this.outputShape=e.outShape,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`pool2D_${t}`,this.poolType=t}getUserCode(){let e="resultValue = max(value, resultValue);";this.poolType==="avg"&&(e="resultValue = resultValue + value; count = count + 1.0;");let t="resultValue";return this.poolType==="avg"&&(t="resultValue / count"),`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let batch = coords[0];
let xRCCorner = vec2<i32>(coords.yz) * uniforms.stride - uniforms.pad;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
var resultValue = ${this.poolType==="avg"?"0.0":"-1.0 / pow(10.0, -20.0)"};
var count = 0.0;
for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + uniforms.dilation.x) {
let xR = xRCorner + wR;
if (xR < 0 || xR >= uniforms.convDims.x) {
continue;
}
for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + uniforms.dilation.y) {
let xC = xCCorner + wC;
if (xC < 0 || xC >= uniforms.convDims.y) {
continue;
}
let value = getX(batch, xR, xC, coords[3]);
${e}
}
}
setOutputAtIndex(index, ${t});
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},M2=class{constructor(e){this.variableNames=["x"],this.uniforms="stride : vec2<i32>,",this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e.outShape,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="poolWithFilterSizeEqualsOne"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let batch = coords[0];
let d = coords[3];
let xRCCorner = coords.yz * uniforms.stride;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
let value = getX(batch, xRCorner, xCCorner, d);
setOutputAtIndex(index, value);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function are(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1,c=C.computePool2DInfo(r.shape,a,i,l,o,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Wn({inputs:{x:r},backend:n});let p,d=[{type:"int32",data:[c.strideHeight,c.strideWidth]}];return c.filterHeight===1&&c.filterWidth===1?p=new M2(c):(p=new z2(c,"avg"),d.push({type:"int32",data:[c.padInfo.top,c.padInfo.left]},{type:"int32",data:[c.dilationHeight,c.dilationWidth]},{type:"int32",data:[c.inHeight,c.inWidth]},{type:"int32",data:[c.effectiveFilterHeight,c.effectiveFilterWidth]})),n.runWebGPUProgram(p,[r],r.dtype,d)}var ire={kernelName:Ca,backendName:"webgpu",kernelFunc:are};function ore(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a}=t,{transposeA:i,transposeB:o}=s;return zv({a:r,b:a,transposeA:i,transposeB:o,backend:n})}var ure={kernelName:Na,backendName:"webgpu",kernelFunc:ore},lre=class{constructor(e,t){this.variableNames=["source"],this.workPerThread=1,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.rank=t.length,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.start=e,this.uniforms=`start : ${Wt(e.length)}, `,this.shaderKey="slice"}getUserCode(){let e=Wt(this.rank),t=cre(this.rank),n;return this.start.length===1?n=this.outputShape.map((r,a)=>"sourceLoc = uniforms.start + coords;"):n=this.outputShape.map((r,a)=>`sourceLoc.${ng[a]} = uniforms.start[${a}] + coords.${ng[a]};`),`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
var sourceLoc : ${e};
let coords = getCoordsFromIndex(index);
${n.join(`
`)}
setOutputAtIndex(index, getSource(${t}));
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},ng=["x","y","z","w","u","v"];function cre(e){if(e===1)return"sourceLoc";if(e<=6)return ng.slice(0,e).map(t=>`sourceLoc.${t}`).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}function hu(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,size:i}=s,[o,u]=kt.parseSliceParams(r,a,i);if(kt.assertParamsValid(r,o,u),n.shouldExecuteOnCPU([r])||r.dtype==="string"){let p=n.tensorMap.get(r.dataId),d=zse(p.values,o,u,r.shape,r.dtype);return n.makeTensorInfo(u,r.dtype,d)}if(w.sizeFromShape(u)===0)return n.makeTensorInfo(u,r.dtype,[]);let l=new lre(o,u),c=[{type:"int32",data:o}];return n.runWebGPUProgram(l,[r],r.dtype,c)}var dre={kernelName:Mo,backendName:"webgpu",kernelFunc:hu},pre=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,crops:i}=s;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGPU backend not implemented yet");let o=a.reduce((y,v)=>y*v),u=C.getReshaped(r.shape,a,o),l=C.getPermuted(u.length,a.length),c=C.getReshapedPermuted(r.shape,a,o),p=C.getSliceBeginCoords(i,a.length),d=C.getSliceSize(c,i,a.length),h=[],f=Me({inputs:{x:r},backend:n,attrs:{shape:u}}),m=Ks({inputs:{x:f},backend:n,attrs:{perm:l}}),g=Me({inputs:{x:m},backend:n,attrs:{shape:c}}),b=hu({inputs:{x:g},backend:n,attrs:{begin:p,size:d}});return h.push(f),h.push(m),h.push(g),h.forEach(y=>n.disposeData(y.dataId)),b},hre={kernelName:co,backendName:"webgpu",kernelFunc:pre},L2=mn({opSnippet:10,dtype:"bool",cpuKernelImpl:Ese}),fre={kernelName:To,backendName:"webgpu",kernelFunc:L2};function ic(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.tensorMap.get(s.dataId);return Wn({inputs:{x:r.complexTensorInfos.real},backend:n})}var mre={kernelName:cp,backendName:"webgpu",kernelFunc:ic};function gre(e,t){let n=new ac(e.shape,22),s=t.runWebGPUProgram(n,[e],"int32");return{dataId:s.dataId,shape:s.shape,dtype:s.dtype}}function sg(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dtype:a}=s;if(a==="complex64"){if(r.dtype==="complex64")return Wn({inputs:{x:r},backend:n});let i=$t(r.shape),o=sg({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),u=pu({inputs:{real:o,imag:i},backend:n});return i.dispose(),n.disposeData(o.dataId),u}if(r.dtype==="complex64"){let i=ic({inputs:{input:r},backend:n}),o=sg({inputs:{x:i},backend:n,attrs:{dtype:a}});return n.disposeData(i.dataId),o}if(!w.hasEncodingLoss(r.dtype,a)){let i=Wn({inputs:{x:r},backend:n});return{dataId:i.dataId,shape:i.shape,dtype:a}}if(a==="int32")return gre(r,n);if(a==="bool"){let i=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=L2({inputs:{a:r,b:i},backend:n});return n.disposeData(i.dataId),u}throw new Error(`Error in Cast: failed to cast ${r.dtype} to ${a}`)}var bre={kernelName:Ta,backendName:"webgpu",kernelFunc:sg},yre=jt({opType:1,cpuKernelImpl:hse}),vre={kernelName:$a,backendName:"webgpu",kernelFunc:yre},xre=class{constructor(e){this.variableNames=["A"],this.uniforms="minVal : f32, maxVal : f32,",this.workPerThread=4,this.workGroupSize=[64,1,1],this.isVec4=!0,this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.shaderKey="clipVec4"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if(index < uniforms.size) {
let value = getAByOutputIndex(index);
var clampedValue : vec4<f32>;
for (var i = 0; i < 4; i = i + 1) {
if (isnan(value[i])) {
clampedValue[i] = value[i];
} else {
clampedValue[i] = clamp(value[i], uniforms.minVal, uniforms.maxVal);
}
}
setOutputAtIndex(index, clampedValue);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},wre=class{constructor(e){this.variableNames=["A"],this.uniforms="minVal : f32, maxVal : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="clip"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if(index < uniforms.size) {
let value = getAByOutputIndex(index);
if (isnan(value)) {
setOutputAtIndex(index, value);
return;
}
setOutputAtIndex(index, clamp(value, uniforms.minVal, uniforms.maxVal));
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function kre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{clipValueMin:a,clipValueMax:i}=s,o,u=[{type:"float32",data:[a]},{type:"float32",data:[i]}];return w.sizeFromShape(r.shape)%4===0?o=new xre(r.shape):o=new wre(r.shape),n.runWebGPUProgram(o,[r],r.dtype,u)}var Sre={kernelName:Cr,backendName:"webgpu",kernelFunc:kre},Ire=class{constructor(e){this.uniforms="",this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=C.computeOutShape(e,1),this.variableNames=e.map((t,n)=>`T${n}`),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.offsetLength=e.length-1;for(let t=0;t<this.offsetLength;t++)this.uniforms+=`offset${t} : i32,`;this.shaderKey="concat"}getUserCode(){let e=[];if(this.offsetLength>0){e.push("if (yC < uniforms.offset0){ setOutputAtCoords(coords.x, coords.y, getT0(yR, yC)); }");for(let r=1;r<this.offsetLength;r++)e.push(`else if (yC < uniforms.offset${[r]}){ setOutputAtCoords(coords.x, coords.y, getT${r}(yR, yC - uniforms.offset${r-1})); }`);let n=this.offsetLength,s=this.offsetLength-1;e.push(`else { setOutputAtCoords(coords.x, coords.y, getT${n}(yR, yC - uniforms.offset${s})); }`)}else e.push("setOutputAtCoords(coords.x, coords.y, getT0(yR, yC));");return`
2022-04-21 15:39:40 +02:00
${Ue()}
for(var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if(flatIndex < uniforms.size) {
let coords = getCoordsFromIndex(flatIndex);
let yR = coords.x;
let yC = coords.y;
${e.join(`
`)}
}
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function oh(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.tensorMap.get(s.dataId);return Wn({inputs:{x:r.complexTensorInfos.imag},backend:n})}var Cre={kernelName:ip,backendName:"webgpu",kernelFunc:oh};function rg(e,t,n){let s=e[0].dtype;if(s==="complex64"){let h=e.map(y=>ic({inputs:{input:y},backend:n})),f=e.map(y=>oh({inputs:{input:y},backend:n})),m=rg(h,t,n),g=rg(f,t,n),b=pu({inputs:{real:m,imag:g},backend:n});return h.forEach(y=>n.disposeData(y.dataId)),f.forEach(y=>n.disposeData(y.dataId)),n.disposeData(m.dataId),n.disposeData(g.dataId),b}let r=n.shouldExecuteOnCPU(e);if(s==="string"&&(r=!0),r){let h=e.map(x=>{let k=w.sizeFromShape(x.shape.slice(t));return Me({inputs:{x},backend:n,attrs:{shape:[-1,k]}})}),f=h.map(x=>({vals:n.readSync(x.dataId),shape:x.shape})),m=C.computeOutShape(h.map(x=>x.shape),1),g=h[0].shape[0]===1,b=fse(f,m,s,g),y=C.computeOutShape(e.map(x=>x.shape),t),v=n.makeTensorInfo(y,s,b);return h.forEach(x=>n.disposeData(x.dataId)),v}let{tensors2D:a,outShape:i}=Nre(e,t,n),o=a.map(h=>h.shape),u=new Ire(o),l=[],c=new Array(o.length-1);if(c.length>0){c[0]=o[0][1],l.push({type:"int32",data:[c[0]]});for(let h=1;h<c.length;h++)c[h]=c[h-1]+o[h][1],l.push({type:"int32",data:[c[h]]})}let p=n.runWebGPUProgram(u,a,a[0].dtype,l);a.forEach(h=>n.disposeData(h.dataId));let d=Me({inputs:{x:p},backend:n,attrs:{shape:i}});return n.disposeData(p.dataId),d}function Nre(e,t,n){let s=C.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>Me({inputs:{x:a},backend:n,attrs:{shape:[w.sizeFromShape(a.shape.slice(0,t)),w.sizeFromShape(a.shape.slice(t))]}})),outShape:s}}function B2(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s,a=w.parseAxisParam(r,t[0].shape)[0],i=C.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(i)===0)return n.makeTensorInfo(i,t[0].dtype,[]);let o=t.filter(l=>w.sizeFromShape(l.shape)>0);if(o.length===1)return Wn({inputs:{x:o[0]},backend:n});let u=o.map(l=>l.shape);return C.assertParamsConsistent(u,a),rg(o,a,n)}var Tre={kernelName:po,backendName:"webgpu",kernelFunc:B2},$re=class{constructor(e,t=!1,n=null,s=!1){this.variableNames=["x","W"],this.uniforms=`filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>,
dimAOuter : i32, dimBOuter : i32, dimInner : i32,`,this.workGroupSize=[8,8,1],this.isVec4=!0,this.outputShape=e.outShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.outputShape[1]===1?this.elementsPerThread=[4,1,1]:this.elementsPerThread=[4,4,1],this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivationWeights=s,this.innerElementSize=this.convInfo.inChannels%4===0?4:3,this.innerElementSize===3?this.variableTypes=["f32","vec4<f32>"]:this.variableTypes=["vec4<f32>","vec4<f32>"],this.addBias&&(this.variableNames.push("bias"),this.variableTypes.push("vec4<f32>")),this.hasPreluActivationWeights&&(this.variableNames.push("preluActivationWeights"),this.variableTypes.push("vec4<f32>")),this.tileAOuter=this.outputShape[1]===1?1:this.workGroupSize[1]*this.elementsPerThread[1],this.tileBOuter=this.workGroupSize[0]*this.elementsPerThread[0],this.tileInner=this.workGroupSize[0]*this.innerElementSize,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`conv2DMMVec4_${this.activation}_${this.fitA}_${this.fitB}_${this.elementsPerThread}_${this.innerElementSize}`}getShapeFit(){let e=[this.tileAOuter,this.tileInner],t=[this.tileInner,this.tileBOuter],n=this.outputShape[1]*this.outputShape[2],s=this.outputShape[3],r=this.convInfo.filterHeight*this.convInfo.filterWidth*this.convInfo.inChannels;return[il(e,[n,r]),il(t,[r,s])]}getUserCode(){let e=F2(this.elementsPerThread,this.tileAOuter,this.tileBOuter,this.tileInner,this.innerElementSize),t=`let outRow = r / uniforms.outShape[2];
2022-04-21 15:39:40 +02:00
let outCol = r % uniforms.outShape[2];
let WRow = c / (uniforms.filterDims[1] * uniforms.xShape[3]);
let WCol = c / uniforms.xShape[3] % uniforms.filterDims[1];
let inChCoord = c % uniforms.xShape[3];
2022-05-22 14:50:51 +02:00
let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
var resData = vec${this.innerElementSize}<f32>(0.0);
// The bounds checking is always needed since we use it to pad zero for
// the 'same' padding type.
if (xRow >= 0 && xRow < uniforms.xShape[1] && xCol >= 0 && xCol < uniforms.xShape[2]) {
var coord = vec4<i32>(
2022-04-21 15:39:40 +02:00
batch,
2022-05-22 14:50:51 +02:00
xRow,
xCol,
2022-04-21 15:39:40 +02:00
inChCoord);
2022-05-22 14:50:51 +02:00
let xIndex = getIndexFromCoords4D(coord, uniforms.xShape);
${this.innerElementSize===3?"resData = vec3<f32>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);":"resData = x[xIndex / 4];"}
}
return resData;`,n=this.fitA?`${t}`:`if (r < uniforms.dimAOuter && c < uniforms.dimInner) {
${t}
2022-04-21 15:39:40 +02:00
}
2022-05-22 14:50:51 +02:00
return vec${this.innerElementSize}<f32>(0.0);
`,s=this.fitB?"return W[row * uniforms.dimBOuter / 4 + col];":`if(coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
2022-04-21 15:39:40 +02:00
return W[row * uniforms.dimBOuter / 4 + col];
}
return vec4<f32>(0.0);
2022-05-24 13:28:51 +02:00
`,r="",a="";if(this.activation){let u=Or(this.activation,this.isVec4);this.hasPreluActivationWeights?r=`fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
2022-05-22 14:50:51 +02:00
${u}
}`:r=`
2022-04-21 15:39:40 +02:00
fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
2022-05-22 14:50:51 +02:00
${u}
}`,a="value = activation(value, outCoord);"}let i=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${r}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> vec${this.innerElementSize}<f32> {
2022-04-21 15:39:40 +02:00
let r = row;
2022-05-22 14:50:51 +02:00
let c = col * ${this.innerElementSize};
2022-04-21 15:39:40 +02:00
var batch = i32(globalId.z);
2022-05-22 14:50:51 +02:00
${n}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
2022-05-22 14:50:51 +02:00
${s}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(row : i32, col : i32, valueInput : vec4<f32>, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
if (row < uniforms.dimAOuter && col * 4 < uniforms.dimBOuter)
{
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col * 4);
2022-05-09 14:16:00 +02:00
${i}
2022-05-22 14:50:51 +02:00
${a}
2022-04-21 15:39:40 +02:00
setOutputAtCoords(outCoord[0], outCoord[1], outCoord[2], outCoord[3],
value);
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${e}
2022-05-30 03:12:18 +02:00
`}},_re=class{constructor(e,t,n,s,r=!1,a=null,i=!1){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.outputShape=e.outShape,this.isChannelsLast=e.dataFormat==="channelsLast",this.dispatchLayout=this.isChannelsLast?{x:[3],y:[1,2],z:[0]}:{x:[2,3],y:[1],z:[0]},this.workGroupSize=Dv(this.dispatchLayout,this.outputShape),this.elementsPerThread=Fv(this.dispatchLayout,this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),r&&this.variableNames.push("bias"),i&&this.variableNames.push("preluActivationWeights"),this.addBias=r,this.activation=a,this.hasPreluActivationWeights=i;let o=this.workGroupSize[1]*this.elementsPerThread[1],u=this.workGroupSize[0]*this.elementsPerThread[0];this.tileInner=32,this.fitAOuter=t%o===0,this.fitBOuter=n%u===0,this.fitInner=s%this.tileInner===0,this.shaderKey=`conv2DMM_${this.elementsPerThread}_${this.activation}}_${this.fitAOuter}_${this.fitBOuter}_${this.fitInner}_${this.isChannelsLast}`}getUserCode(){let e=this.isChannelsLast?`
2022-05-24 13:28:51 +02:00
let coord = vec4<i32>(batch, xRow, xCol, xCh);
2022-04-21 15:39:40 +02:00
`:`
2022-05-24 13:28:51 +02:00
let coord = vec4<i32>(batch, xCh, xRow, xCol);
2022-04-21 15:39:40 +02:00
`,t=this.isChannelsLast?`
let outCoord = vec4<i32>(
batch,
row / outWidth,
row % outWidth,
col);
`:`
let outCoord = vec4<i32>(
batch,
2022-05-24 13:28:51 +02:00
row,
col / outWidth,
col % outWidth);
2022-05-30 03:12:18 +02:00
`,n=Pv(this.elementsPerThread,this.workGroupSize,!this.isChannelsLast,this.tileInner),s=this.isChannelsLast?"row":"col",r=this.isChannelsLast?"col":"row",a=`
2022-04-21 15:39:40 +02:00
let inChannels = uniforms.wShape[2];
let outWidth = ${this.isChannelsLast?"uniforms.outShape[2]":"uniforms.outShape[3]"};
2022-05-24 13:28:51 +02:00
let outRow = ${s} / outWidth;
let outCol = ${s} % outWidth;
2022-04-21 15:39:40 +02:00
2022-05-24 13:28:51 +02:00
let WRow = ${r} / (uniforms.filterDims[1] * inChannels);
let WCol = ${r} / inChannels % uniforms.filterDims[1];
2022-04-21 15:39:40 +02:00
let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0];
let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1];
2022-05-24 13:28:51 +02:00
let xCh = ${r} % inChannels;
2022-04-21 15:39:40 +02:00
${e}
// The bounds checking is always needed since we use it to pad zero for the
// 'same' padding type.
if(coordsInBounds4D(coord, uniforms.xShape)) {
return x[getIndexFromCoords4D(coord, uniforms.xShape)];
2022-04-21 15:38:36 +02:00
}
2022-05-24 13:28:51 +02:00
return 0.0;`,i=this.isChannelsLast?this.fitAOuter&&this.fitInner?`${a}`:`if (row < uniforms.dimAOuter && col < uniforms.dimInner) {
${a}
2022-04-21 15:38:36 +02:00
}
2022-05-24 13:28:51 +02:00
return 0.0;`:this.fitInner&&this.fitBOuter?`${a}`:`if (row < uniforms.dimInner && col < uniforms.dimBOuter) {
${a}
}
return 0.0;`,o="return W[row * uniforms.wShape[3] + col];",u="",l="";if(this.activation){let d=Or(this.activation,!1);this.hasPreluActivationWeights?u=`fn activation(a: f32, outCoord : vec4<i32>) -> f32 {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
2022-05-24 13:28:51 +02:00
${d}
}`:u=`
2022-04-21 15:39:40 +02:00
fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
2022-05-24 13:28:51 +02:00
${d}
2022-04-21 15:39:40 +02:00
}
2022-05-24 13:28:51 +02:00
`,l="value = activation(value, outCoord);"}let c=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${u}
2022-04-21 15:39:40 +02:00
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
var batch = i32(globalId.z);
2022-05-24 13:28:51 +02:00
${this.isChannelsLast?i:o}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
2022-05-24 13:28:51 +02:00
var batch = i32(globalId.z);
${this.isChannelsLast?o:i}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(row : i32, col : i32, valueInput : f32, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
let outWidth = ${this.isChannelsLast?"uniforms.outShape[2]":"uniforms.outShape[3]"};
${t}
2022-05-24 13:28:51 +02:00
${c}
${l}
2022-04-21 15:39:40 +02:00
result[getIndexFromCoords4D(outCoord, uniforms.outShape)] = value;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${n}
2022-05-30 03:12:18 +02:00
`}};function Lw(e,t){let n=e.length;return n>=3?t?[...e.slice(0,-3),e[n-3]*e[n-2],e[n-1]]:[...e.slice(0,-3),e[n-3],e[n-2]*e[n-1]]:!t&&n===1&&e[0]>1?[e[0],1]:null}function Are({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let u=n.dataFormat==="channelsLast",l=!u,c=!1,p=u&&n.filterHeight===n.inHeight&&n.filterWidth===n.inWidth&&n.padInfo.type==="VALID",d=[],h,f;if(p){let b=n.inHeight*n.inWidth*n.inChannels;h=Me({inputs:{x:e},backend:s,attrs:{shape:[1,n.batchSize,b]}}),f=Me({inputs:{x:t},backend:s,attrs:{shape:[1,b,n.outChannels]}})}else h=Me({inputs:{x:e},backend:s,attrs:{shape:u?[n.batchSize,n.inHeight*n.inWidth,n.inChannels]:[n.batchSize,n.inChannels,n.inHeight*n.inWidth]}}),f=Me({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}});if(d.push(h),d.push(f),a!=null){let b=Lw(a.shape,u);b!=null&&(a=Me({inputs:{x:a},backend:s,attrs:{shape:b}}),d.push(a))}if(r!=null){let b=Lw(r.shape,u);b!=null&&(r=Me({inputs:{x:r},backend:s,attrs:{shape:b}}),d.push(r))}let m=zv({a:u?h:f,b:u?f:h,transposeA:l,transposeB:c,backend:s,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i}),g=Me({inputs:{x:m},backend:s,attrs:{shape:n.outShape}});d.push(m);for(let b of d)s.disposeData(b.dataId);return g}function V2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let u=r!=null,l=a!=null,c=n.dataFormat==="channelsLast",p;if(c&&n.filterHeight===n.inHeight&&n.filterWidth===n.inWidth&&n.padInfo.type==="VALID"||n.filterHeight===1&&n.filterWidth===1&&n.dilationHeight===1&&n.dilationWidth===1&&n.strideHeight===1&&n.strideWidth===1&&(n.padInfo.type==="SAME"||n.padInfo.type==="VALID"))return Are({x:e,filter:t,convInfo:n,backend:s,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i});let h=(n.inChannels%4===0||n.inChannels%3===0)&&n.outChannels%4===0&&c,f=c?n.outHeight*n.outWidth:n.outChannels,m=c?n.outChannels:n.outHeight*n.outWidth,g=n.filterHeight*n.filterWidth*n.inChannels,b=[n.padInfo.top,n.padInfo.left],y=[{type:"int32",data:[n.filterHeight,n.filterWidth]},{type:"int32",data:[...b]},{type:"int32",data:[n.strideHeight,n.strideWidth]},{type:"int32",data:[n.dilationHeight,n.dilationWidth]},{type:"int32",data:[f]},{type:"int32",data:[m]},{type:"int32",data:[g]}];h?p=new $re(n,u,o,l):p=new _re(n,f,m,g,u,o,l);let v=[],x=[e,t];u&&(!c&&r.shape.length===1&&(r=Me({inputs:{x:r},backend:s,attrs:{shape:[r.shape[0],1,1]}}),v.push(r)),x.push(r)),l&&(!c&&a.shape.length===1&&(a=Me({inputs:{x:a},backend:s,attrs:{shape:[a.shape[0],1,1]}}),v.push(a)),x.push(a)),o==="leakyrelu"&&(y.push({type:"float32",data:[i]}),p.uniforms+=" alpha : f32,");let k=s.runWebGPUProgram(p,x,e.dtype,y);for(let I of v)s.disposeData(I.dataId);return k}function Ere(e){let{inputs:t,attrs:n,backend:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dataFormat:u,dilations:l,dimRoundingMode:c}=n,p=C.convertConv2DDataFormat(u),d=C.computeConv2DInfo(r.shape,a.shape,i,l,o,c,!1,p);return V2({x:r,filter:a,convInfo:d,backend:s})}var Rre={kernelName:_a,backendName:"webgpu",kernelFunc:Ere},Dre=class{constructor(e){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>, pads : vec2<i32>, stride : vec2<i32>, outBackprop : vec4<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.outputShape=e.inShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.workGroupSize=Dv(this.dispatchLayout,this.outputShape),this.elementsPerThread=Fv(this.dispatchLayout,this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),this.shaderKey=`conv2DDerInputMM_${this.elementsPerThread}`}getUserCode(){return`
2022-04-21 15:39:40 +02:00
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
var batch = i32(globalId.z);
if (row < uniforms.dimAOuter && col < uniforms.dimInner) {
let outRow = row / uniforms.outShape[2];
let outCol = row % uniforms.outShape[2];
let WRow = col / (uniforms.filterDims[1] * uniforms.outBackprop[3]);
let WCol = col / uniforms.outBackprop[3] % uniforms.filterDims[1];
let xR = f32(outRow - uniforms.pads[0] + WRow) / f32(uniforms.stride[0]);
let xC = f32(outCol - uniforms.pads[1] + WCol) / f32(uniforms.stride[1]);
if (xR < 0.0 || xR >= f32(uniforms.outBackprop[1]) || fract(xR) > 0.0) {
return 0.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
if (xC < 0.0 || xC >= f32(uniforms.outBackprop[2]) || fract(xC) > 0.0) {
return 0.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
let coord = vec4<i32>(
batch,
i32(xR),
i32(xC),
col % uniforms.outBackprop[3]);
return x[getIndexFromCoords4D(coord, uniforms.xShape)];
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return 0.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let coordX = uniforms.filterDims.x - 1 -
row / (uniforms.filterDims[1] * uniforms.outBackprop[3]);
let coordY = uniforms.filterDims.y - 1 -
(row / uniforms.outBackprop[3]) % uniforms.filterDims[1];
if (row < uniforms.dimInner && col < uniforms.dimBOuter &&
coordX >= 0 && coordY >= 0) {
let coord = vec4<i32>(coordX, coordY, col,
row % uniforms.outBackprop[3]);
return W[getIndexFromCoords4D(coord, uniforms.wShape)];
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return 0.0;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn mm_write(row : i32, col : i32, valueInput : f32, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col);
result[getIndexFromCoords4D(outCoord, uniforms.outShape)] = value;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
2022-05-30 03:12:18 +02:00
${Pv(this.elementsPerThread,this.workGroupSize)}
`}},Fre=class{constructor(e){this.variableNames=["dy","W"],this.uniforms="filterDims : vec2<i32>, pads : vec2<i32>, stride : vec2<i32>, outBackprop : vec4<i32>,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e.inShape,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.isChannelsLast=e.dataFormat==="channelsLast",this.shaderKey=`conv2DDerInput_${this.isChannelsLast}`}getUserCode(){let e=this.isChannelsLast?1:2,t=this.isChannelsLast?2:3,n=this.isChannelsLast?3:1;return`
2022-04-21 15:39:40 +02:00
${Ue()} {
if(index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let batch = coords[0];
let d1 = coords[${n}];
let dyCorner = vec2<i32>(coords[${e}]), coords[${t}]) - uniforms.pads;
let dyRCorner = dyCorner.x;
let dyCCorner = dyCorner.y;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
var dotProd = 0.0;
for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + 1) {
let dyR = (f32(dyRCorner) + f32(wR)) / f32(uniforms.stride.x);
let wRPerm = uniforms.filterDims.x - 1 - wR;
if (dyR < 0.0 || dyR >= f32(uniforms.outBackprop[1]) || fract(dyR) > 0.0 ||
wRPerm < 0) {
continue;
}
let idyR = dyR;
for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + 1) {
let dyC = (f32(dyCCorner) + f32(wC)) / f32(uniforms.stride.y);
let wCPerm = uniforms.filterDims.y - 1 - wC;
if (dyC < 0.0 || dyC >= f32(uniforms.outBackprop[2]) ||
fract(dyC) > 0.0 || wCPerm < 0) {
continue;
}
let idyC = dyC;
for (var d2 = 0; d2 < uniforms.outBackprop[3]; d2 = d2 + 1) {
if (${this.isChannelsLast}) {
let xValue = getDy(batch, idyR, idyC, d2);
let wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd = dotProd + xValue * wValue;
} else {
let xValue = getDy(batch, d2, idyR, idyC);
let wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd = dotProd + xValue * wValue;
}
}
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutputAtIndex(index, dotProd);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function Ore(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{inputShape:i,strides:o,pad:u,dataFormat:l,dimRoundingMode:c}=s,p=C.convertConv2DDataFormat(l),d=C.computeConv2DInfo(i,a.shape,o,1,u,c,!1,p),h=[{type:"int32",data:[d.filterHeight,d.filterWidth]},{type:"int32",data:[d.filterHeight-1-d.padInfo.top,d.filterWidth-1-d.padInfo.left]},{type:"int32",data:[d.strideHeight,d.strideWidth]},{type:"int32",data:[d.batchSize,d.outHeight,d.outWidth,d.outChannels]}],f;if(K().getBool("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE"))f=new Fre(d);else{f=new Dre(d);let m=d.inShape[1]*d.inShape[2],g=d.inShape[3],b=d.filterHeight*d.filterWidth*d.outChannels;h.push({type:"uint32",data:[m]},{type:"uint32",data:[g]},{type:"uint32",data:[b]})}return n.runWebGPUProgram(f,[r,a],"float32",h)}var Pre={kernelName:Aa,backendName:"webgpu",kernelFunc:Ore},zre=jt({opType:2}),Mre={kernelName:Ea,backendName:"webgpu",kernelFunc:zre},Lre=jt({opType:3}),Bre={kernelName:Ra,backendName:"webgpu",kernelFunc:Lre},Vre=class{constructor(e,t,n,s){this.variableNames=["Image","Boxes","BoxInd"],this.uniforms="extrapolationValue : f32,",this.workGroupSize=[64,1,1],this.size=!0;let[r]=t;this.outputShape=[r,n[0],n[1],e],this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.methodId=s==="bilinear"?1:0,this.cropHeightBiggerThan1=this.outputShape[1]>1,this.cropWidthBiggerThan1=this.outputShape[2]>1,this.shaderKey=`cropAndResize_${this.methodId}_${this.cropHeightBiggerThan1}_${this.cropWidthBiggerThan1}`}getUserCode(){let[e,t]=["f32(uniforms.imageShape[1] - 1)","f32(uniforms.imageShape[2] - 1)"],[n,s,r]=this.cropHeightBiggerThan1?[`(${e} / f32(uniforms.outShape[1] - 1))`,"(y2-y1) * height_ratio",`y1*${e} + f32(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${e}`],[a,i,o]=this.cropWidthBiggerThan1?[`(${t} / f32(uniforms.outShape[2] - 1))`,"(x2-x1) * width_ratio",`x1*${t} + f32(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${t}`];return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let height_ratio = f32(${n});
let width_ratio = f32(${a});
let b = coords[0];
let y = coords[1];
let x = coords[2];
let d = coords[3];
// get box vals
let y1 = getBoxes(b, 0);
let x1 = getBoxes(b, 1);
let y2 = getBoxes(b, 2);
let x2 = getBoxes(b, 3);
// get image in batch index
let bInd = i32(round(getBoxInd(b)));
if(bInd < 0 || bInd >= uniforms.outShape[0]) {
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
let height_scale = ${s};
let width_scale = ${i};
let in_y = ${r};
if( in_y < 0.0 || in_y > ${e} ) {
setOutputAtIndex(index, uniforms.extrapolationValue);
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
let in_x = ${o};
if( in_x < 0.0 || in_x > ${t} ) {
setOutputAtIndex(index, uniforms.extrapolationValue);
return;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
let sourceFracIndexCR = vec2<f32>(in_x,in_y);
if(${this.methodId} == 1) {
// Compute the four integer indices.
let sourceFloorCR = vec2<i32>(sourceFracIndexCR);
let sourceCeilCR = vec2<i32>(ceil(sourceFracIndexCR));
let topLeft = getImage(bInd, sourceFloorCR.y, sourceFloorCR.x, d);
let bottomLeft = getImage(bInd, sourceCeilCR.y, sourceFloorCR.x, d);
let topRight = getImage(bInd, sourceFloorCR.y, sourceCeilCR.x, d);
let bottomRight = getImage(bInd, sourceCeilCR.y, sourceCeilCR.x, d);
let fracCR = sourceFracIndexCR - vec2<f32>(sourceFloorCR);
let top = topLeft + (topRight - topLeft) * fracCR.x;
let bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;
let newValue = top + (bottom - top) * fracCR.y;
setOutputAtIndex(index, newValue);
} else {
// Compute the coordinators of nearest neighbor point.
let sourceNearestCR = vec2<i32>(floor(
sourceFracIndexCR + vec2<f32>(0.5,0.5)));
let newValue = getImage(
bInd, sourceNearestCR.y, sourceNearestCR.x, d);
setOutputAtIndex(index, newValue);
2022-04-21 15:38:36 +02:00
}
}
}
2022-05-30 03:12:18 +02:00
`}},Wre=e=>{let{inputs:t,backend:n,attrs:s}=e,{image:r,boxes:a,boxInd:i}=t,{cropSize:o,method:u,extrapolationValue:l}=s,c=new Vre(r.shape[3],a.shape,o,u),p=[{type:"float32",data:[l]}];return n.runWebGPUProgram(c,[r,a,i],"float32",p)},Ure={kernelName:fo,backendName:"webgpu",kernelFunc:Wre},Bw=class{constructor(e,t,n,s){this.variableNames=["x"],this.uniforms="index : f32,",this.size=!0;let r=128;this.workGroupSize=[r,1,1],this.outputShape=t,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.exclusive=n,this.reverse=s,this.op=e,this.shaderKey=`cum_${this.op}_${this.exclusive}_${this.reverse}`}getUserCode(){let e=this.outputShape.length,t=this.op==="*"?"1.0":"0.0",n=this.exclusive?t:`getX(${Vw(e,"coords",this.op)})`,s=this.outputShape[this.outputShape.length-1],r="",a="";return this.exclusive?(r=this.reverse?`end != ${s-1}`:"end != 0",a=this.reverse?"end + 1":"end - 1"):(r=this.reverse?`end + pow2 < ${s}`:"end >= pow2",a=this.reverse?"end + pow2":"end - pow2"),`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
var coords = getCoordsFromIndex(index);
2022-05-30 03:12:18 +02:00
let end = ${Ww(e,"coords",this.op)};
2022-04-21 15:39:40 +02:00
var val = ${n};
let pow2 = i32(pow(2.0, uniforms.index));
if (${r}) {
let idx = ${a};
2022-05-30 03:12:18 +02:00
${Ww(e,"coords",this.op)} = idx;
val ${this.op}= getX(${Vw(e,"coords",this.op)});
2022-04-21 15:39:40 +02:00
}
setOutputAtIndex(index, val);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Vw(e,t,n){if(e===1)return`${t}`;if(e===2)return`${t}.x, ${t}.y`;if(e===3)return`${t}.x, ${t}.y, ${t}.z`;if(e===4)return`${t}.x, ${t}.y, ${t}.z, ${t}.w`;throw Error(`Cumulative ${n} for rank ${e} is not yet supported`)}function Ww(e,t,n){if(e===1)return`${t}`;if(e===2)return`${t}.y`;if(e===3)return`${t}.z`;if(e===4)return`${t}.w`;throw Error(`Cumulative ${n} for rank ${e} is not yet supported`)}function W2(e,t,n,s,r,a){let i=t.shape.length,o=C.getAxesPermutation([s],i),u=t;o!=null&&(u=Ks({inputs:{x:t},backend:n,attrs:{perm:o}}));let l=C.getInnerMostAxes(1,i)[0];if(l!==i-1)throw new Error(`WebGPU cumprod shader expects an inner-most axis=${t.shape.length-1} but got axis=${s}`);let c=u.shape[l],p=Wn({inputs:{x:u},backend:n});for(let d=0;d<=Math.ceil(Math.log2(c))-1;d++){let h=new Bw(e,u.shape,!1,a),f=p,m=[{type:"float32",data:[d]}];p=n.runWebGPUProgram(h,[p],p.dtype,m),n.disposeData(f.dataId)}if(r){let d=new Bw(e,u.shape,r,a),h=p,f=[{type:"float32",data:[0]}];p=n.runWebGPUProgram(d,[p],p.dtype,f),n.disposeData(h.dataId)}if(o!=null){let d=C.getUndoAxesPermutation(o),h=Ks({inputs:{x:p},backend:n,attrs:{perm:d}});return n.disposeData(p.dataId),n.disposeData(u.dataId),h}return p}function Gre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:i,reverse:o}=s;return W2("*",r,n,a,i,o)}var Hre={kernelName:ho,backendName:"webgpu",kernelFunc:Gre};function qre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:i,reverse:o}=s;return W2("+",r,n,a,i,o)}var jre={kernelName:Da,backendName:"webgpu",kernelFunc:qre},Kre=class{constructor(e,t){this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.uniforms="blockSize : i32,",this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`depthToSpace_${t}`,this.dataFormat=t}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let b = coords[0];
let h = ${this.getHeightCoordString()};
let w = ${this.getWidthCoordString()};
let d = ${this.getDepthCoordString()};
let in_h = h / uniforms.blockSize;
let offset_h = h % uniforms.blockSize;
let in_w = w / uniforms.blockSize;
let offset_w = w % uniforms.blockSize;
let offset_d = (offset_h * uniforms.blockSize + offset_w) *
${this.getOutputDepthSize()};
let in_d = d + offset_d;
let rlt = ${this.getInputSamplingString()};
setOutputAtIndex(index, rlt);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
}`}getHeightCoordString(){return this.dataFormat==="NHWC"?"coords[1]":"coords[2]"}getWidthCoordString(){return this.dataFormat==="NHWC"?"coords[2]":"coords[3]"}getDepthCoordString(){return this.dataFormat==="NHWC"?"coords[3]":"coords[1]"}getOutputDepthSize(){return this.dataFormat==="NHWC"?"uniforms.outShape[3]":"uniforms.outShape[1]"}getInputSamplingString(){return this.dataFormat==="NHWC"?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"}};function Xre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockSize:a,dataFormat:i}=s,o=r.shape[0],u=i==="NHWC"?r.shape[1]:r.shape[2],l=i==="NHWC"?r.shape[2]:r.shape[3],c=i==="NHWC"?r.shape[3]:r.shape[1],p=u*a,d=l*a,h=c/(a*a),f=i==="NHWC"?[o,p,d,h]:[o,h,p,d],m=[{type:"int32",data:[a]}],g=new Kre(f,i);return n.runWebGPUProgram(g,[r],r.dtype,m)}var Yre={kernelName:mo,backendName:"webgpu",kernelFunc:Xre},U2=class{constructor(e,t=!1,n=null,s=!1){this.variableNames=["x","W"],this.uniforms="pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>, inDims : vec2<i32>,",this.workGroupSize=[4,4,4],this.isVec4=!0,this.outputShape=e.outShape,this.dispatchLayout={x:[0,1],y:[2],z:[3]},this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[1,4,4]),w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivation=s,this.shaderKey=`depthwise3x3_${n}`}getUserCode(){let e="",t="";if(this.activation){let r=Or(this.activation,this.isVec4);this.hasPreluActivation?e=`fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${r}
}`:e=`
fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
${r}
}
`,t="dotProd[i] = activation(dotProd[i], coords);"}let n=this.addBias?"dotProd[i] = dotProd[i] + getBiasByOutputCoords(coords);":"";return`
${e}
2022-05-30 03:12:18 +02:00
${Rv()}
2022-04-21 15:39:40 +02:00
fn main(@builtin(global_invocation_id) globalId: vec3<u32>) {
let batch = 0;
let r = i32(globalId.x);
let c = i32(globalId.y) * 4;
let d2 = i32(globalId.z) * 4;
let xRCCorner = vec2<i32>(r, c) * uniforms.stride - uniforms.pad;
let d1 = d2;
let q = 0;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
var wVals : array<vec4<f32>, 9>;
wVals[0] = getW(0, 0, d1, q);
wVals[1] = getW(0, 1, d1, q);
wVals[2] = getW(0, 2, d1, q);
wVals[3] = getW(1, 0, d1, q);
wVals[4] = getW(1, 1, d1, q);
wVals[5] = getW(1, 2, d1, q);
wVals[6] = getW(2, 0, d1, q);
wVals[7] = getW(2, 1, d1, q);
wVals[8] = getW(2, 2, d1, q);
var xVals : array<array<vec4<f32>, 6>, 3>;
for (var wR = 0; wR < 3; wR = wR + 1) {
let xR = xRCorner + wR * uniforms.dilation[0];
for (var wC = 0; wC < 6; wC = wC + 1) {
let xC = xCCorner + wC * uniforms.dilation[1];
if (xR < 0 || xR >= uniforms.inDims[0] || xC < 0 || xC >= uniforms.inDims[1]) {
xVals[wR][wC] = vec4<f32>(0.0);
} else {
xVals[wR][wC] = getX(batch, xR, xC, d1);
}
2022-04-21 15:38:36 +02:00
}
}
2022-04-21 15:39:40 +02:00
var dotProd : array<vec4<f32>, 4>;
dotProd[0] = vec4<f32>(0.0);
dotProd[1] = vec4<f32>(0.0);
dotProd[2] = vec4<f32>(0.0);
dotProd[3] = vec4<f32>(0.0);
for (var wR = 0; wR < 3; wR = wR + 1) {
for (var wC = 0; wC < 3; wC = wC + 1) {
let indexW = wR * 3 + wC;
dotProd[0] = dotProd[0] + xVals[wR][0 + wC] * wVals[indexW];
dotProd[1] = dotProd[1] + xVals[wR][1 + wC] * wVals[indexW];
dotProd[2] = dotProd[2] + xVals[wR][2 + wC] * wVals[indexW];
dotProd[3] = dotProd[3] + xVals[wR][3 + wC] * wVals[indexW];
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
for (var i = 0; i < 4; i = i + 1) {
let coords = vec4<i32>(batch, r, c + i, d2);
if (coordsInBounds4D(coords, uniforms.outShape)) {
${n}
${t}
setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], dotProd[i]);
}
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}},G2=class{constructor(e,t=!1,n=null,s=!1){this.variableNames=["x","W"],this.uniforms=`pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>,
2022-04-21 15:39:40 +02:00
inDims : vec2<i32>, filterHeight : i32, filterWidth : i32,
2022-05-30 03:12:18 +02:00
channelMul : i32,`,this.workGroupSize=[256,1,1],this.outputShape=e.outShape,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivation=s,this.shaderKey=`depthwise_${this.activation}`}getUserCode(){let e="",t="";if(this.activation){let r=Or(this.activation,!1);this.hasPreluActivation?e=`fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
2022-04-21 15:39:40 +02:00
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${r}
}`:e=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
${r}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`,t="dotProd = activation(dotProd, coords);"}let n=this.addBias?"dotProd = dotProd + getBiasByOutputCoords(coords);":"";return`
${e}
fn writeResult(batch : i32, row : i32, col : i32, chan : i32,
value : f32) {
let coord = vec4<i32>(batch, row, col, chan);
if (coordsInBounds4D(coord, uniforms.outShape)) {
setOutputAtCoords(batch, row, col, chan, value);
2022-04-21 15:38:36 +02:00
}
}
2022-04-21 15:39:40 +02:00
2022-05-24 13:28:51 +02:00
${du()}
2022-04-21 15:39:40 +02:00
let coords = getOutputCoords();
let batch = coords[0];
let xRCCorner = vec2<i32>(coords.yz) * uniforms.stride - uniforms.pad;
let d2 = coords[3];
let d1 = d2 / uniforms.channelMul;
let q = d2 - d1 * uniforms.channelMul;
let inputRowStart = xRCCorner.x;
let inputColStart = xRCCorner.y;
let inputRowEnd = inputRowStart + uniforms.filterHeight *
uniforms.dilation[0];
let inputColEnd = inputColStart + uniforms.filterWidth *
uniforms.dilation[1];
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
var dotProd = 0.0;
// Extract if checking out of for loop for performance.
if (inputRowStart >= 0 && inputColStart >= 0 &&
inputRowEnd < uniforms.inDims[0] &&
inputColEnd < uniforms.inDims[1]) {
// Here using a constant value |this.convInfo.filterHeight| instead
// of uniform value is in order to loop unrolling.
for (var wR = 0; wR < uniforms.filterHeight; wR = wR + 1) {
let xR = inputRowStart + wR * uniforms.dilation[0];
for (var wC = 0; wC < uniforms.filterWidth; wC = wC + 1) {
let xC = inputColStart + wC * uniforms.dilation[1];
let xVal = getX(batch, xR, xC, d1);
let wVal = getW(wR, wC, d1, q);
dotProd = dotProd + xVal * wVal;
}
}
} else {
for (var wR = 0; wR < uniforms.filterHeight; wR = wR + 1) {
let xR = inputRowStart + wR * uniforms.dilation[0];
if (xR < 0 || xR >= uniforms.inDims[0]) {
continue;
}
for (var wC = 0; wC < uniforms.filterWidth; wC = wC + 1) {
let xC = inputColStart + wC * uniforms.dilation[1];
if (xC < 0 || xC >= uniforms.inDims[1]) {
continue;
}
let xVal = getX(batch, xR, xC, d1);
let wVal = getW(wR, wC, d1, q);
dotProd = dotProd + xVal * wVal;
}
}
}
${n}
${t}
writeResult(batch, coords[1], coords[2], d2, dotProd);
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Qre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u,dimRoundingMode:l}=s,c=u;c==null&&(c=[1,1]);let p=C.computeConv2DInfo(r.shape,a.shape,i,c,o,l,!0),d=[{type:"int32",data:[p.padInfo.top,p.padInfo.left]},{type:"int32",data:[p.strideHeight,p.strideWidth]},{type:"int32",data:[p.dilationHeight,p.dilationWidth]},{type:"int32",data:[p.inHeight,p.inWidth]}],h;return p.batchSize===1&&p.inHeight===p.outHeight&&p.inWidth===p.outWidth&&p.strideHeight===1&&p.strideWidth===1&&p.filterHeight===p.filterWidth&&p.inChannels===p.outChannels&&p.dilationHeight===1&&p.dilationWidth===1&&p.filterHeight===3&&p.inChannels%4===0?h=new U2(p):(h=new G2(p),d.push({type:"int32",data:[p.filterHeight]},{type:"int32",data:[p.filterWidth]},{type:"int32",data:[p.outChannels/p.inChannels]})),n.runWebGPUProgram(h,[r,a],r.dtype,d)}var Zre={kernelName:Fa,backendName:"webgpu",kernelFunc:Qre},H2=mn({opSnippet:0,cpuKernelImpl:_se,supportsComplex:!0}),Jre={kernelName:Za,backendName:"webgpu",kernelFunc:H2},eae=class{constructor(e,t){this.workGroupSize=[64,1,1],this.variableNames=["x"],this.uniforms="reduceSize : i32,",this.size=!0,this.inputShape=[e.batchSize,e.inSize];let[n]=C.computeOutAndReduceShapes(this.inputShape,[1]);this.outputShape=n.length===0?[1]:n,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,[1,1,1]),this.reduceType=t,this.shaderKey=`reduce_${t}`}getUserCode(){let e="",t="0.0";this.reduceType==="min"||this.reduceType==="max"?(e=`
2022-04-21 15:39:40 +02:00
if (isnan(candidate)) {
bestValue = uniforms.NAN;
} else if (!isnan(bestValue) && candidate ${this.reduceType==="min"?"<":">"} bestValue)
{ bestValue = candidate; }`,t="f32(x[offset])"):this.reduceType==="sum"||this.reduceType==="mean"?e=" bestValue = bestValue + candidate; ":this.reduceType==="prod"&&(e=" bestValue = bestValue * candidate; ",t="1.0");let n=this.reduceType==="mean"?"setOutputAtIndex(outputIndex, bestValue / f32(uniforms.reduceSize));":"setOutputAtIndex(outputIndex, bestValue);";return`
fn DIV_CEIL(a : u32, b : u32) -> u32 {
return ((a - 1u) / b + 1u);
}
${`
var<workgroup> xBestValues : array<f32, ${this.workGroupSize[0]}>;
`}
fn getOffset(outputIndex : i32) -> i32 {
let outputCoords = getCoordsFromIndex(outputIndex);
let offset = ${this.outputShape.length===1?"outputCoords":"outputCoords[0]"} * uniforms.reduceSize;
return offset;
}
${Ue()}
let outputIndex = index / i32(workGroupSizeX);
let offset = getOffset(outputIndex);
var bestValue = ${t};
let Length = uniforms.reduceSize;
let WorkPerThread = DIV_CEIL(u32(Length), workGroupSizeX);
for (var k = i32(localId.x); k < Length && outputIndex < uniforms.size;
k = k + i32(workGroupSizeX)) {
let candidate = f32(x[offset + k]);
${e}
}
xBestValues[localId.x] = bestValue;
workgroupBarrier();
var reduceSize = min(u32(Length), workGroupSizeX);
for (var currentSize = reduceSize / 2u; reduceSize > 1u;
currentSize = reduceSize / 2u) {
let interval = DIV_CEIL(reduceSize, 2u);
if (localId.x < currentSize) {
let candidate = xBestValues[localId.x + interval];
${e}
xBestValues[localId.x] = bestValue;
}
reduceSize = interval;
workgroupBarrier();
}
if (localId.x == 0u && outputIndex < uniforms.size) {
${n}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function oc(e,t,n,s,r){let a=e.shape.length,i=[],o=w.parseAxisParam(t,e.shape),u=o,l=C.getAxesPermutation(u,a),c=e;l!=null&&(c=Ks({inputs:{x:e},attrs:{perm:l},backend:r}),u=C.getInnerMostAxes(u.length,a),i.push(c)),C.assertAxesAreInnerMostDims(s,u,a);let[p,d]=C.computeOutAndReduceShapes(c.shape,u),h=p;n&&(h=C.expandShapeToKeepDim(p,o));let f;if((s==="max"||s==="prod")&&r.shouldExecuteOnCPU([c])){let m=r.tensorMap.get(c.dataId).values;switch(s){case"max":let g=Nse(m,w.sizeFromShape(d),h,e.dtype);f=r.makeTensorInfo(h,e.dtype,g);break;case"prod":let{outVals:b,outShape:y,outDtype:v}=Rse(c.shape,c.dtype,m,u);f=r.makeTensorInfo(y,v,b);break;default:throw new Error(`${s} CPU implementation is not yet supported.`)}}else{let m=w.sizeFromShape(d),b=w.sizeFromShape(c.shape)/m,y={windowSize:m,inSize:m,batchSize:b,outSize:1},v=s==="mean"?"float32":yp(e.dtype),x=[{type:"int32",data:[m]}],k=new eae(y,s),I=r.runWebGPUProgram(k,[c],v,x);i.push(I),f=Me({inputs:{x:I},attrs:{shape:h},backend:r})}return i.forEach(m=>r.disposeData(m.dataId)),f}function Mv(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return oc(r,a,i,"sum",n)}var tae={kernelName:ci,backendName:"webgpu",kernelFunc:Mv};function nae(e){let{inputs:t,backend:n,attrs:s}=e,{equation:r}=s,a=t,{allDims:i,summedDims:o,idDims:u}=C.decodeEinsumEquation(r,a.length);C.checkEinsumDimSizes(i.length,u,a);let{path:l,steps:c}=C.getEinsumComputePath(o,u),p=c.length,d=null,h=i.length,f=[];for(let m=0;m<p;++m){for(let g of c[m]){let{permutationIndices:b,expandDims:y}=C.getEinsumPermutation(h,u[g]),v;C.isIdentityPermutation(b)?v=a[g]:(v=Ks({inputs:{x:a[g]},backend:n,attrs:{perm:b}}),f.push(v));let x=v.shape.slice();for(let k=0;k<y.length;++k)x.splice(y[k],0,1);w.arraysEqual(v.shape,x)||(v=Me({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),d===null?d=v:(d=H2({inputs:{a:v,b:d},backend:n}),f.push(d))}m<p-1&&(l[m]>=0&&(d=Mv({inputs:{x:d},backend:n,attrs:{axis:l[m]-(i.length-h),keepDims:!1}}),f.push(d)),h--)}for(let m of f)m!==d&&n.disposeData(m.dataId);return d}var sae={kernelName:ap,backendName:"webgpu",kernelFunc:nae},rae=jt({opType:4}),aae={kernelName:Pa,backendName:"webgpu",kernelFunc:rae},iae=mn({opSnippet:4,dtype:"bool",cpuKernelImpl:mse}),oae={kernelName:go,backendName:"webgpu",kernelFunc:iae},q2=jt({opType:5,cpuKernelImpl:gse,dtype:"float32"}),uae={kernelName:za,backendName:"webgpu",kernelFunc:q2};function ag(e){let{inputs:t,attrs:n,backend:s}=e,{dim:r}=n,{input:a}=t,i=a.shape.length,o=a.shape.slice(),u=r;return r<0&&(w.assert(-(i+1)<=r,()=>`Axis must be in the interval [${-(i+1)}, ${i}]`),u=i+r+1),o.splice(u,0,1),Me({inputs:{x:a},backend:s,attrs:{shape:o}})}var lae={kernelName:bo,backendName:"webgpu",kernelFunc:ag},cae=jt({opType:6,cpuKernelImpl:bse}),dae={kernelName:yo,backendName:"webgpu",kernelFunc:cae},pae=class{constructor(e){this.variableNames=[],this.outputShape=[],this.uniforms="value : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="fill"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
setOutputAtIndex(index, uniforms.value);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function fu(e){let{backend:t,attrs:n}=e,{shape:s,value:r}=n,{dtype:a}=n;if(a=a||w.inferDtype(r),a==="string"){let i=w.getArrayFromDType(a,w.sizeFromShape(s));return i.fill(r),t.makeTensorInfo(s,a,i)}else{let i=new pae(s),o=[{type:"float32",data:[r]}];return t.runWebGPUProgram(i,[],a,o)}}var hae={kernelName:vl,backendName:"webgpu",kernelFunc:fu},fae=class{constructor(e){this.outputShape=[],this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="flipLeftRight"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let coordX = uniforms.xShape[2] - coords[2] - 1;
let outputValue = getX(coords[0], coords[1], coordX, coords[3]);
setOutputAtIndex(index, outputValue);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}},mae={kernelName:vo,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,s=t,r=new fae(n.shape);return s.runWebGPUProgram(r,[n],n.dtype)}},gae=jt({opType:7,cpuKernelImpl:yse}),bae={kernelName:Ma,backendName:"webgpu",kernelFunc:gae},yae=mn({opSnippet:12,dtype:"int32"}),vae={kernelName:La,backendName:"webgpu",kernelFunc:yae},xae=class{constructor(e,t=!1){this.outputShape=[0],this.variableNames=[],this.workGroupSize=[256,1,1],this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.useImport=t,this.shaderKey=`fromPixels_${this.useImport}`}getUserCode(){let e=this.useImport?"textureLoad(src, vec2<i32>(coords.yx));":"textureLoad(src, vec2<i32>(coords.yx), 0)";return`
2022-04-21 15:39:40 +02:00
@binding(1) @group(0) var src: ${this.useImport?"texture_external":"texture_2d<f32>"};
${Ue()}
let flatIndexBase = index * uniforms.numChannels;
for (var i = 0; i < uniforms.numChannels; i = i + 1) {
let flatIndex = flatIndexBase + i;
if (flatIndex < uniforms.size) {
let coords = getCoordsFromIndex(flatIndexBase);
let values = ${e};
result[flatIndex] = i32(floor(255.0 * values[i]));
}
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}},wae={kernelName:vd,backendName:"webgpu",kernelFunc:kae},Vi;function kae(e){let{inputs:t,backend:n,attrs:s}=e,{pixels:r}=t,{numChannels:a}=s;if(r==null)throw new Error("pixels passed to tf.browser.fromPixels() can not be null");let i=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,o=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,u=typeof HTMLCanvasElement!="undefined"&&r instanceof HTMLCanvasElement||typeof OffscreenCanvas!="undefined"&&r instanceof OffscreenCanvas,l=typeof ImageBitmap!="undefined"&&r instanceof ImageBitmap,[c,p]=i?[r.videoWidth,r.videoHeight]:[r.width,r.height],d=[p,c,a];if(K().getBool("WEBGPU_USE_IMPORT")&&i)return Uw({externalImage:r,backend:n,attrs:s,outShape:d,useImport:!0});if((i||o)&&(Vi==null&&(Vi=document.createElement("canvas").getContext("2d")),Vi.canvas.width=c,Vi.canvas.height=p,Vi.drawImage(r,0,0,c,p),r=Vi.canvas),l||u||i||o)return Uw({externalImage:r,backend:n,attrs:s,outShape:d,useImport:!1});let h=r.data,f=h;if(a!=null&&a!==4){f=new Uint8Array(r.width*r.height*a);let b=h.length,y=0;for(let v=0;v<b;v++)v%4<a&&(f[y++]=h[v])}let m=n.makeTensorInfo(d,"int32"),g=n.tensorMap.get(m.dataId);return g.values=new Int32Array(f),n.maybeReleaseBuffer(m.dataId),n.uploadToGPU(m.dataId),m}function Uw(e){let{externalImage:t,backend:n,attrs:s,outShape:r,useImport:a}=e,{numChannels:i}=s,o=w.sizeFromShape(r),u=w.computeStrides(r),l=new xae(r,a),c=[{type:"uint32",data:[o]},{type:"uint32",data:[i]},{type:"uint32",data:[...u]},{type:"uint32",data:[...l.dispatch]}];return n.runFromPixelsProgram(l,r,c,a,t)}var Sae=class{constructor(e,t,n,s,r){this.uniforms="varianceEpsilon : f32,",this.workGroupSize=[128,1,1],this.size=!0,this.variableNames=["x","mean","variance"],C.assertAndGetBroadcastShape(e,t),C.assertAndGetBroadcastShape(e,n),this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),s!=null&&(C.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset")),r!=null&&(C.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale")),this.offsetShape=s,this.scaleShape=r,this.shaderKey="batchNorm"}getUserCode(){let e="0.0";this.offsetShape!=null&&(e="getOffsetByOutputIndex(index)");let t="1.0";return this.scaleShape!=null&&(t="getScaleByOutputIndex(index)"),`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size)
{
let xValue = getXByOutputIndex(index);
let meanValue = getMeanByOutputIndex(index);
let varianValue = getVarianceByOutputIndex(index);
let offsetValue = ${e};
let scaleValue = ${t};
let inv = scaleValue * inverseSqrt(varianValue + f32(uniforms.varianceEpsilon));
setOutputAtIndex(index,dot(vec3<f32>(xValue, -meanValue, offsetValue), vec3<f32>(inv, inv, 1.0)));
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}},Iae={kernelName:Ba,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s,scale:r,offset:a,mean:i,variance:o}=e,{varianceEpsilon:u}=t,l=n,c=[s,i,o],p=null;a!=null&&(p=a.shape,c.push(a));let d=null;r!=null&&(d=r.shape,c.push(r));let h=new Sae(s.shape,i.shape,o.shape,p,d),f=[{type:"float32",data:[u]}];return l.runWebGPUProgram(h,c,s.dtype,f)}};function Cae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dataFormat:c,dilations:p,dimRoundingMode:d,activation:h,leakyreluAlpha:f}=s,m=C.convertConv2DDataFormat(c),g=C.computeConv2DInfo(r.shape,a.shape,u,p,l,d,!1,m);return V2({x:r,filter:a,convInfo:g,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:f,activation:h})}var Nae={kernelName:oa,backendName:"webgpu",kernelFunc:Cae};function Tae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:p,activation:d,leakyreluAlpha:h}=s,f=c;f==null&&(f=[1,1]),w.assert(C.eitherStridesOrDilationsAreOne(u,f),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${f}'`);let m=C.computeConv2DInfo(r.shape,a.shape,u,f,l,p,!0),g=[r,a],b=i!=null,y=o!=null;b&&g.push(i),y&&g.push(o);let v=[{type:"int32",data:[m.padInfo.top,m.padInfo.left]},{type:"int32",data:[m.strideHeight,m.strideWidth]},{type:"int32",data:[m.dilationHeight,m.dilationWidth]},{type:"int32",data:[m.inHeight,m.inWidth]}],x;return m.batchSize===1&&m.inHeight===m.outHeight&&m.inWidth===m.outWidth&&m.strideHeight===1&&m.strideWidth===1&&m.filterHeight===m.filterWidth&&m.inChannels===m.outChannels&&m.dilationHeight===1&&m.dilationWidth===1&&m.filterHeight===3&&m.inChannels%4===0?x=new U2(m,b,d,y):(x=new G2(m,b,d,y),v.push({type:"int32",data:[m.filterHeight]},{type:"int32",data:[m.filterWidth]},{type:"int32",data:[m.outChannels/m.inChannels]})),d==="leakyrelu"&&(v.push({type:"float32",data:[h]}),x.uniforms+=" alpha : f32,"),n.runWebGPUProgram(x,g,"float32",v)}var $ae={kernelName:ua,backendName:"webgpu",kernelFunc:Tae},_ae=class{constructor(e,t){this.variableNames=["A","indices"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`gathernd_${e}`,this.sliceDim=e,this.uniforms=`sliceDim : i32, strides : ${Wt(e)},`}getUserCode(){let e;return this.sliceDim>1?e="uniforms.strides[j]":e="uniforms.strides",`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
var flattenIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexTemp = i32(round(getIndices(coords[0], j)));
let strideNum = ${e};
flattenIndex = flattenIndex + indexTemp * strideNum;
}
setOutputAtIndex(index, getA(flattenIndex, coords[1]));
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function Aae(e){let{inputs:t,backend:n}=e,{params:s,indices:r}=t,a=r.shape,i=a[a.length-1],o=w.sizeFromShape(s.shape),[u,l,c,p]=C.prepareAndValidate(s,r),d=Me({inputs:{x:r},backend:n,attrs:{shape:[l,i]}}),h=Me({inputs:{x:s},backend:n,attrs:{shape:[w.sizeFromShape(s.shape)/c,c]}});if(n.shouldExecuteOnCPU([s,r])||s.dtype==="string"){let y=n.readSync(r.dataId),v=n.bufferSync(s),x=vse(y,v,s.dtype,l,i,c,p,s.shape,o);return n.makeTensorInfo(u,s.dtype,x.values)}let f=new _ae(i,[l,c]),m=[{type:"int32",data:[i]},{type:"int32",data:p}],g=n.runWebGPUProgram(f,[h,d],h.dtype,m),b=Me({inputs:{x:g},backend:n,attrs:{shape:u}});return n.disposeData(d.dataId),n.disposeData(h.dataId),n.disposeData(g.dataId),b}var Eae={kernelName:wo,backendName:"webgpu",kernelFunc:Aae},Rae=class{constructor(e,t){this.variableNames=["A","indices"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e.slice(),this.aShape=e,this.outputShape=t,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="gather"}getUserCode(){let e=Dae(this.aShape);return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromIndex(index);
let indexZ = i32(getIndices(resRC.x, resRC.z));
let inBounds = select(0.0, 1.0, indexZ >= 0 && indexZ < uniforms.aShape[2]);
setOutputAtIndex(index, inBounds * getA(${e}));
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Dae(e){let t=["resRC.x","resRC.y","resRC.z","resRC.w"],n=[];for(let s=0;s<e.length;s++)s===2?n.push("indexZ"):n.push(`${t[s]}`);return n.join()}function j2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,indices:a}=t,{axis:i,batchDims:o}=s,u=w.parseAxisParam(i,r.shape)[0],l=C.segment_util.collectGatherOpShapeInfo(r,a,u,o),c=w.sizeFromShape(a.shape),p=[],d=Me({inputs:{x:r},backend:n,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),h=Me({inputs:{x:a},backend:n,attrs:{shape:[l.batchSize,c/l.batchSize]}});p.push(d),p.push(h);let f=[l.batchSize,l.outerSize,c/l.batchSize,l.sliceSize];if(n.shouldExecuteOnCPU([r,a])){let v=n.tensorMap.get(h.dataId).values,x=Ae(h.shape,h.dtype,v),I=n.tensorMap.get(d.dataId).values,$=Ae(d.shape,d.dtype,I),R=xse($,x,f);return p.forEach(E=>n.disposeData(E.dataId)),n.makeTensorInfo(l.outputShape,R.dtype,R.values)}let m=new Rae(d.shape,f),g=n.runWebGPUProgram(m,[d,h],d.dtype);p.push(g);let b=Me({inputs:{x:g},backend:n,attrs:{shape:l.outputShape}});return p.forEach(y=>n.disposeData(y.dataId)),b}var Fae={kernelName:xo,backendName:"webgpu",kernelFunc:j2},Oae=mn({opSnippet:5,cpuKernelImpl:kse,dtype:"bool"}),Pae={kernelName:ko,backendName:"webgpu",kernelFunc:Oae},zae=mn({opSnippet:6,dtype:"bool",cpuKernelImpl:wse}),Mae={kernelName:Va,backendName:"webgpu",kernelFunc:zae};function Lae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{alpha:a}=s,i=[{type:"float32",data:[a]}],o=new ac(r.shape,14);return o.uniforms="alpha : f32,",n.runWebGPUProgram(o,[r],"float32",i)}var Bae={kernelName:Ua,backendName:"webgpu",kernelFunc:Lae},Vae=mn({opSnippet:7,dtype:"bool",cpuKernelImpl:Ise}),Wae={kernelName:So,backendName:"webgpu",kernelFunc:Vae},Uae=mn({opSnippet:8,dtype:"bool",cpuKernelImpl:Sse}),Gae={kernelName:Io,backendName:"webgpu",kernelFunc:Uae},Hae=jt({opType:9,cpuKernelImpl:Cse}),qae={kernelName:Ga,backendName:"webgpu",kernelFunc:Hae},jae=mn({opSnippet:9,dtype:"bool"}),Kae={kernelName:Co,backendName:"webgpu",kernelFunc:jae},Xae=jt({opType:10}),Yae={kernelName:Il,backendName:"webgpu",kernelFunc:Xae};function K2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reductionIndices:a,keepDims:i}=s;return oc(r,a,i,"max",n)}var Qae={kernelName:Ha,backendName:"webgpu",kernelFunc:K2},Zae=mn({opSnippet:15,cpuKernelImpl:Tse}),Jae={kernelName:qa,backendName:"webgpu",kernelFunc:Zae};function eie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1,c=C.computePool2DInfo(r.shape,a,i,l,o,u),p,d=[];if(c.filterHeight===1&&c.filterWidth===1){if(w.arraysEqual(c.inShape,c.outShape))return Wn({inputs:{x:r},backend:n});p=new M2(c),d.push({type:"int32",data:[c.strideHeight,c.strideWidth]})}else p=new z2(c,"max"),d.push({type:"int32",data:[c.strideHeight,c.strideWidth]},{type:"int32",data:[c.padInfo.top,c.padInfo.left]},{type:"int32",data:[c.dilationHeight,c.dilationWidth]},{type:"int32",data:[c.inHeight,c.inWidth]},{type:"int32",data:[c.effectiveFilterHeight,c.effectiveFilterWidth]});return n.runWebGPUProgram(p,[r],r.dtype,d)}var tie={kernelName:ja,backendName:"webgpu",kernelFunc:eie};function nie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{keepDims:a,axis:i}=s;return oc(r,i,a,"mean",n)}var sie={kernelName:Ka,backendName:"webgpu",kernelFunc:nie};function rie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return oc(r,a,i,"min",n)}var aie={kernelName:Xa,backendName:"webgpu",kernelFunc:rie},iie=mn({opSnippet:16,cpuKernelImpl:$se}),oie={kernelName:Ya,backendName:"webgpu",kernelFunc:iie},uie=class{constructor(e,t,n){this.uniforms="",this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t.map((s,r)=>s[0]+e[r]+s[1]),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.xShape=e,t.map((s,r)=>{this.uniforms+=` pad${r} : vec2<i32>,`}),this.offset=n==="reflect"?0:1,this.shaderKey=`mirrorPad_${n}`}getUserCode(){let e=this.xShape.length,t=this.xShape.map((u,l)=>`uniforms.pad${l}[0]`).join(","),n=this.xShape.map((u,l)=>`uniforms.pad${l}[0] + uniforms.xShape${e>1?`[${l}]`:""
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let start = ${i}(${t});
let end = ${i}(${n});
var outC = getCoordsFromIndex(index);
for (var i = 0; i < ${e}; i = i + 1) {
if (${a} < ${s}) {
${a} = ${s} * 2 - ${a} - ${this.offset};
} else if(${a} >= ${r}) {
${a} = (${r} - 1) * 2 - ${a} + ${this.offset};
}
}
let coords = outC - start;
setOutputAtIndex(index, getX(${o}));
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},lie={kernelName:Qa,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{paddings:r,mode:a}=t,i=n,o=r.map(c=>({type:"int32",data:[c[0],c[1]]})),u=new uie(s.shape,r,a);return i.runWebGPUProgram(u,[s],s.dtype,o)}};function cie(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])){let a=n.tensorMap.get(s.dataId),[i,o]=Ase(a.values,s.shape,s.dtype);return n.makeTensorInfo(o,s.dtype,i)}let r=new ac(s.shape,11);return n.runWebGPUProgram(r,[s],s.dtype)}var die={kernelName:No,backendName:"webgpu",kernelFunc:cie};function pie(e){console.warn("tf.nonMaxSuppression() in webgpu locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u}=s,l=n.readSync(r.dataId),c=n.readSync(a.dataId),{selectedIndices:p}=ws.nonMaxSuppressionV3Impl(l,c,i,o,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var hie={kernelName:$o,backendName:"webgpu",kernelFunc:pie};function fie(e){console.warn("tf.nonMaxSuppression() in webgpu locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u,softNmsSigma:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),d=i,h=o,f=u,m=l,{selectedIndices:g,selectedScores:b}=ws.nonMaxSuppressionV5Impl(c,p,d,h,f,m);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var mie={kernelName:_o,backendName:"webgpu",kernelFunc:fie};function jd(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="complex64"){let r=ic({inputs:{input:s},backend:n}),a=jd({inputs:{x:r},backend:n}),i=oh({inputs:{input:s},backend:n}),o=jd({inputs:{x:i},backend:n}),u=pu({inputs:{real:a,imag:o},backend:n});return n.disposeData(r.dataId),n.disposeData(a.dataId),n.disposeData(i.dataId),n.disposeData(o.dataId),u}else return fu({attrs:{shape:s.shape,dtype:s.dtype,value:s.dtype==="string"?"":0},backend:n})}var gie={kernelName:jo,backendName:"webgpu",kernelFunc:jd};function X2(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(s.dtype==="complex64"){let r=ic({inputs:{input:s},backend:n}),a=X2({inputs:{x:r},backend:n}),i=oh({inputs:{input:s},backend:n}),o=jd({inputs:{x:i},backend:n}),u=pu({inputs:{real:a,imag:o},backend:n});return n.disposeData(r.dataId),n.disposeData(a.dataId),n.disposeData(i.dataId),n.disposeData(o.dataId),u}else return fu({attrs:{shape:s.shape,dtype:s.dtype,value:1},backend:n})}var bie={kernelName:Ao,backendName:"webgpu",kernelFunc:X2};function yie(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s;if(t.length===1)return ag({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let a=t[0].shape,i=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(i===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let o=[],u=t.map(c=>{let p=ag({inputs:{input:c},backend:n,attrs:{dim:r}});return o.push(p),p}),l=B2({inputs:u,backend:n,attrs:{axis:r}});return o.forEach(c=>n.disposeData(c.dataId)),l}var vie={kernelName:Ro,backendName:"webgpu",kernelFunc:yie},xie=class{constructor(e,t){this.variableNames=["x"],this.uniforms="constantValue : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t.map((n,s)=>n[0]+e[s]+n[1]),this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),t.map((n,s)=>{this.uniforms+=` pad${s} : vec2<i32>,`}),this.xShape=e,this.shaderKey="pad"}getUserCode(){let e=this.xShape.length,t=Wt(e),n=this.xShape.map((c,p)=>`uniforms.pad${p}[0]`).join(","),s=this.xShape.map((c,p)=>`uniforms.pad${p}[0] + uniforms.xShape${e>1?`[${p}]`:""}`).join(","),r=e>1?`${t}(${n})`:`${n}`,a=e>1?`${t}(${s})`:`${s}`,i=e>1?"any(outC < start)":"outC < start",o=e>1?"any(outC >= end)":"outC >= end",u=e>1?["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,e):"coords";return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let start = ${r};
let end = ${a};
let outC = getCoordsFromIndex(index);
if (${i} || ${o}) {
setOutputAtIndex(index, uniforms.constantValue);
} else {
let coords = outC - start;
setOutputAtIndex(index, getX(${u}));
}
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}},Y2=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{paddings:a,constantValue:i}=s;if(a.every(l=>w.arraysEqual(l,[0,0])))return Wn({inputs:{x:r},backend:n});if(w.sizeFromShape(r.shape)===0){let l=a.map((c,p)=>c[0]+r.shape[p]+c[1]);return fu({backend:n,attrs:{shape:l,value:i,dtype:r.dtype}})}let o=[{type:"float32",data:[i]}];a.map(l=>o.push({type:"int32",data:[l[0],l[1]]}));let u=new xie(r.shape,a);return n.runWebGPUProgram(u,[r],r.dtype,o)},wie={kernelName:Ja,backendName:"webgpu",kernelFunc:Y2},kie=mn({opSnippet:13}),Sie={kernelName:ei,backendName:"webgpu",kernelFunc:kie};function Iie(e){let{inputs:t,backend:n}=e,{x:s,alpha:r}=t,a=new O2(14,s.shape,r.shape);return n.runWebGPUProgram(a,[s,r],"float32")}var Cie={kernelName:ti,backendName:"webgpu",kernelFunc:Iie};function Nie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return oc(r,a,i,"prod",n)}var Tie={kernelName:ni,backendName:"webgpu",kernelFunc:Nie},$ie=e=>{let{backend:t,attrs:n}=e,{start:s,stop:r,step:a,dtype:i}=n,o=Dse(s,r,a,i);return t.makeTensorInfo([o.length],i,o)},_ie={kernelName:Tl,backendName:"webgpu",kernelFunc:$ie},Q2=mn({opSnippet:3}),Aie={kernelName:Oa,backendName:"webgpu",kernelFunc:Q2},Eie=jt({opType:12}),Rie={kernelName:si,backendName:"webgpu",kernelFunc:Eie},Die=jt({opType:13}),Fie={kernelName:ai,backendName:"webgpu",kernelFunc:Die},Oie=class{constructor(e,t,n){this.variableNames=["x"],this.uniforms="adjustHeightWidth : vec2<f32>, halfPixelCenters : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=[e[0],t,n,e[3]],this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="resizeBilinear"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let b = coords[0];
let d = coords[3];
let rc = coords.yz;
let effectiveInSize = vec2<f32>(
f32(uniforms.xShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.xShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveOutSize = vec2<f32>(
f32(uniforms.outShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.outShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveInputOverOutputRatioRC =
effectiveInSize / effectiveOutSize;
// Fractional source index
let sourceFracIndexRC =
(vec2<f32>(rc) + vec2<f32>(uniforms.halfPixelCenters)) *
effectiveInputOverOutputRatioRC - vec2<f32>(uniforms.halfPixelCenters);
// Compute the four integer indices.
let sourceFloorRC = vec2<i32>(sourceFracIndexRC);
let sourceCeilRC = vec2<i32>(
min(vec2<f32>(uniforms.xShape.yz) - vec2<f32>(1.0), ceil(sourceFracIndexRC)));
let topLeft = getX(b, sourceFloorRC.x, sourceFloorRC.y, d);
let bottomLeft = getX(b, sourceCeilRC.x, sourceFloorRC.y, d);
let topRight = getX(b, sourceFloorRC.x, sourceCeilRC.y, d);
let bottomRight = getX(b, sourceCeilRC.x, sourceCeilRC.y, d);
let fracRC = sourceFracIndexRC - vec2<f32>(sourceFloorRC);
let top = topLeft + (topRight - topLeft) * fracRC.y;
let bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
let newValue = top + (bottom - top) * fracRC.x;
setOutputAtIndex(index, newValue);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function Pie(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,size:i,halfPixelCenters:o}=s,[u,l]=i,c=a&&u>1?1:0,p=a&&l>1?1:0,h=[{type:"float32",data:[c,p]},{type:"float32",data:[o?.5:0]}],f=new Oie(r.shape,u,l);return n.runWebGPUProgram(f,[r],"float32",h)}var zie={kernelName:ri,backendName:"webgpu",kernelFunc:Pie},Mie=class{constructor(e,t,n,s){this.variableNames=["x"],this.uniforms="adjustHeightWidth : vec2<f32>, roundBase : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=[e[0],t,n,e[3]],this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.halfPixelCenters=s,this.shaderKey=`resizeNearest_${s}`}getUserCode(){let e;return this.halfPixelCenters?e="max((vec2<f32>(rc) + vec2<f32>(0.5)) * effectiveInputOverOutputRatioRC, vec2<f32>(0.0))":e="vec2<f32>(rc) * effectiveInputOverOutputRatioRC",`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let b = coords[0];
let d = coords[3];
let rc = coords.yz;
let effectiveInSize = vec2<f32>(
f32(uniforms.xShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.xShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveOutSize = vec2<f32>(
f32(uniforms.outShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.outShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveInputOverOutputRatioRC =
effectiveInSize / effectiveOutSize;
// Fractional source index
let sourceFracIndexRC = ${e};
// Compute the coordinators of nearest neighbor point.
let inputShapeRC = vec2<f32>(f32(uniforms.xShape.y), f32(uniforms.xShape.z));
let sourceNearestRC = vec2<i32>(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + uniforms.roundBase)));
let newValue = getX(b, sourceNearestRC.x, sourceNearestRC.y, d);
setOutputAtIndex(index, newValue);
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Lie(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:i,size:o}=s,[u,l]=o,c=a&&u>1?1:0,p=a&&l>1?1:0,h=[{type:"float32",data:[c,p]},{type:"float32",data:[a?.5:0]}],f=new Mie(r.shape,u,l,i);return n.runWebGPUProgram(f,[r],r.dtype,h)}var Bie={kernelName:_l,backendName:"webgpu",kernelFunc:Lie},Vie=class{constructor(e,t){this.outputShape=[],this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms=`centerX : f32, centerY : f32, sinRadians : f32,
2022-04-21 15:39:40 +02:00
cosRadians : f32,`,this.shaderKey="rotate",this.outputShape=e,typeof t=="number"?(this.uniforms+=" fillValue : f32,",this.fillSnippet="var outputValue = uniforms.fillValue;",this.shaderKey+="_float"):(this.uniforms+=" fillValue : vec3<f32>,",this.fillSnippet="var outputValue = uniforms.fillValue[coords[3]];",this.shaderKey+="_vec3")}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let coordXFloat = (f32(coords[2]) - uniforms.centerX) *
uniforms.cosRadians - (f32(coords[1]) - uniforms.centerY) *
uniforms.sinRadians;
let coordYFloat = (f32(coords[2]) - uniforms.centerX) *
uniforms.sinRadians + (f32(coords[1]) - uniforms.centerY) *
uniforms.cosRadians;
let coordX = i32(round(coordXFloat + uniforms.centerX));
let coordY = i32(round(coordYFloat + uniforms.centerY));
${this.fillSnippet}
if(coordX >= 0 && coordX < uniforms.xShape[2] && coordY >= 0 &&
coordY < uniforms.xShape[1]) {
outputValue = getX(coords[0], coordY, coordX, coords[3]);
}
setOutputAtIndex(index, outputValue);
}
}
2022-05-30 03:12:18 +02:00
`}},Wie={kernelName:Ko,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:s}=e,{radians:r,fillValue:a,center:i}=t,o=n,u=new Vie(s.shape,a),[l,c]=C.getImageCenter(i,s.shape[1],s.shape[2]),p=[{type:"float32",data:[l]},{type:"float32",data:[c]},{type:"float32",data:[Math.sin(r)]},{type:"float32",data:[Math.cos(r)]}];return typeof a=="number"?p.push({type:"float32",data:[Number.parseFloat(a.toFixed(2))]}):p.push({type:"float32",data:a}),o.runWebGPUProgram(u,[s],s.dtype,p)}},Uie=jt({opType:15,cpuKernelImpl:Fse}),Gie={kernelName:ii,backendName:"webgpu",kernelFunc:Uie},Hie=class{constructor(e,t,n,s,r,a,i){this.variableNames=["updates","indices"],this.workGroupSize=[64,1,1],this.atomic=!0,this.outputShape=a,this.type=i,this.dispatchLayout=Ve(e),this.dispatch=_e(this.dispatchLayout,e,this.workGroupSize),this.sliceDimGreaterThanOne=t>1,this.shaderKey=`scatter_${n}_${s}_${this.sliceDimGreaterThanOne}_${i}`;let o=Wt(r.length);this.uniforms=`sliceDim : i32, strides: ${o}, size: i32,`,this.updatesRank=s,this.indicesRank=n}getUserCode(){let e="";this.indicesRank===1?e="coords[0]":this.indicesRank===2&&(e="coords[0], j");let t=`getIndices(${e})`,n=this.sliceDimGreaterThanOne?"uniforms.strides[j]":"uniforms.strides",s="",r="",a="";this.updatesRank===1?(s="coords[0]",r="flattenedIndex",a=`
2022-04-21 15:39:40 +02:00
fn getUpdatesCoordsFromFlatIndex(index : i32) -> i32 {
return index;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`):this.updatesRank===2&&(s="coords[0], coords[1]",r="vec2<i32>(flattenedIndex, coords[1])",a=`
fn getUpdatesCoordsFromFlatIndex(index : i32) -> vec2<i32> {
let d0 = index / uniforms.updatesShape[1];
let d1 = index - d0 * uniforms.updatesShape[1];
return vec2<i32>(d0, d1);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`);let i=`getUpdates(${s})`,o=this.type==="int32"?"atomicAdd(&(result[flatIndex]), i32(updateValue));":`
var assumed = atomicLoad(&(result[flatIndex]));
var success = 0;
for (; success == 0;) {
let new = bitcast<f32>(assumed) + updateValue;
let newI32 = bitcast<i32>(new);
let resValue = atomicCompareExchangeWeak(&(result[flatIndex]), assumed, newI32);
assumed = resValue[0];
success = resValue[1];
}
`;return`
${a}
${Ue()}
if (index < uniforms.size) {
let coords = getUpdatesCoordsFromFlatIndex(index);
var flattenedIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexInside = i32(round(${t}));
flattenedIndex = flattenedIndex + indexInside * ${n};
}
let updateValue = ${i};
let flatIndex = getOutputIndexFromCoords(${r});
${o}
}
2022-05-30 03:12:18 +02:00
}`}};function qie(e){let{inputs:t,backend:n,attrs:s}=e,{indices:r,updates:a}=t,{shape:i}=s,{sliceRank:o,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=C.calculateShapes(a,r,i),d=[p/l,l];if(p===0)return n.makeTensorInfo(i,r.dtype);let h=Me({inputs:{x:r},backend:n,attrs:{shape:[u,o]}}),f=Me({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=f.dtype,g=fu({backend:n,attrs:{shape:d,value:0,dtype:m}}),b=w.sizeFromShape(f.shape),y=[{type:"int32",data:[o]},{type:"int32",data:c},{type:"int32",data:[b]}],v=new Hie(f.shape,o,h.shape.length,f.shape.length,c,d,m),x=n.runWebGPUProgram(v,[f,h],m,y,g),k=Me({inputs:{x},backend:n,attrs:{shape:i}});return n.disposeData(h.dataId),n.disposeData(f.dataId),n.disposeData(x.dataId),k}var jie={kernelName:Po,backendName:"webgpu",kernelFunc:qie},Kie=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.cRank=e,this.rank=n,this.shaderKey="select"}getUserCode(){let e,t;if(this.rank>4)throw Error(`Where for rank ${this.rank} is not yet supported`);if(this.rank===1)t="resRC",e="resRC";else{let s=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[],a=[];for(let i=0;i<this.outputShape.length;i++)a.push(`${s[i]}`),i<this.cRank&&r.push(`${s[i]}`);e=r.join(),t=a.join()}return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromIndex(index);
let cVal = getC(${e});
if (cVal >= 1.0) {
setOutputAtIndex(index, getA(${t}));
} else {
setOutputAtIndex(index, getB(${t}));
}
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Xie(e){let{inputs:t,backend:n}=e,{condition:s,t:r,e:a}=t,i=new Kie(s.shape.length,r.shape,r.shape.length);return n.runWebGPUProgram(i,[s,r,a],ln(r.dtype,a.dtype))}var Yie={kernelName:zo,backendName:"webgpu",kernelFunc:Xie},Qie=jt({opType:18}),Zie={kernelName:ui,backendName:"webgpu",kernelFunc:Qie},Jie=jt({opType:16}),eoe={kernelName:oi,backendName:"webgpu",kernelFunc:Jie},toe=jt({opType:17}),noe={kernelName:Lo,backendName:"webgpu",kernelFunc:toe},Z2=mn({opSnippet:2,cpuKernelImpl:Bse,supportsComplex:!0}),soe={kernelName:hi,backendName:"webgpu",kernelFunc:Z2};function roe(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{dim:a}=s,i=w.parseAxisParam([a],r.shape),o=K2({inputs:{x:r},backend:n,attrs:{reductionIndices:i,keepDims:!1}}),u=C.expandShapeToKeepDim(o.shape,i),l=Me({inputs:{x:o},backend:n,attrs:{shape:u}}),c=Z2({inputs:{a:r,b:l},backend:n}),p=q2({inputs:{x:c},backend:n}),d=Mv({inputs:{x:p},backend:n,attrs:{axis:i,keepDims:!1}}),h=Me({inputs:{x:d},backend:n,attrs:{shape:u}}),f=Q2({inputs:{a:p,b:h},backend:n});return n.disposeData(o.dataId),n.disposeData(l.dataId),n.disposeData(c.dataId),n.disposeData(p.dataId),n.disposeData(d.dataId),n.disposeData(h.dataId),f}var aoe={kernelName:di,backendName:"webgpu",kernelFunc:roe},ioe=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,paddings:i}=s;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGPU backend not implemented yet");let o=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...i);for(let b=1+a.length;b<r.shape.length;++b)u.push([0,0]);let l=[],c=Y2({inputs:{x:r},backend:n,attrs:{paddings:u,constantValue:0}}),p=C.getReshaped(c.shape,a,o,!1),d=C.getPermuted(p.length,a.length,!1),h=C.getReshapedPermuted(c.shape,a,o,!1),f=Me({inputs:{x:c},backend:n,attrs:{shape:p}}),m=Ks({inputs:{x:f},backend:n,attrs:{perm:d}}),g=Me({inputs:{x:m},backend:n,attrs:{shape:h}});return l.push(c),l.push(f),l.push(m),l.forEach(b=>n.disposeData(b.dataId)),g},ooe={kernelName:Bo,backendName:"webgpu",kernelFunc:ioe},uoe=class{constructor(e,t,n,s,r,a,i=!0){this.variableNames=["updates","indices","defaultValue"],this.workGroupSize=[64,1,1],this.workPerThread=4,this.size=!0,this.outputShape=a,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]);let o=t>1;this.shaderKey=`scatter_${n}_${s}_${o}`;let u=Wt(r.length);this.uniforms=`updateSize : i32, sliceDim : i32, strides: ${u},`;let l="";n===1?l="i":n===2&&(l="i, j"),this.indicesSnippet=`getIndices(${l})`;let c="";s===1?c="i":s===2&&(c="i, coords[1]"),this.updatesSnippet=`getUpdates(${c})`,this.strideString=o?"uniforms.strides[j]":"uniforms.strides"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
let globalIndex = index * ${this.workPerThread};
if (globalIndex < uniforms.size) {
var sum = vec4<f32>(0.0);
var found = vec4<bool>(false);
for (var i = 0; i < uniforms.updateSize; i = i + 1) {
var flattenedIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexInside = i32(round(${this.indicesSnippet}));
flattenedIndex = flattenedIndex + indexInside * ${this.strideString};
}
for (var innerIndex = 0; innerIndex < ${this.workPerThread}; innerIndex = innerIndex + 1) {
let curIndex = globalIndex + innerIndex;
let coords = getCoordsFromIndex(curIndex);
if (flattenedIndex == coords[0]) {
sum[innerIndex] = sum[innerIndex] + ${this.updatesSnippet};
found[innerIndex] = true;
}
}
}
for (var innerIndex = 0; innerIndex < ${this.workPerThread}; innerIndex = innerIndex + 1) {
let curIndex = globalIndex + innerIndex;
if (curIndex < uniforms.size)
{
setOutputAtIndex(curIndex, mix(getDefaultValue(), sum[innerIndex], f32(found[innerIndex])));
}
}
}
2022-05-30 03:12:18 +02:00
}`}};function loe(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:i}=t,{outputShape:o}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=C.calculateShapes(a,r,o),h=!1;if(a.dtype==="string"){let y=n.bufferSync(r),v=n.bufferSync(a),x=w.decodeString(n.readSync(i.dataId)[0]),k=Ose(y,v,o,d,c,l,u,p,x,h);return n.makeTensorInfo(o,k.dtype,k.values)}let f=[{type:"int32",data:[l]},{type:"int32",data:[u]},{type:"int32",data:p}],m=new uoe(l,u,r.shape.length,a.shape.length,p,[d,1],h),g=n.runWebGPUProgram(m,[a,r,i],a.dtype,f),b=Me({inputs:{x:g},backend:n,attrs:{shape:o}});return n.disposeData(g.dataId),b}var coe={kernelName:fp,backendName:"webgpu",kernelFunc:loe};function doe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=s,o=w.parseAxisParam(i,r.shape)[0],u=C.prepareSplitSize(r,a,o),l=r.shape.length,c=new Array(l).fill(0),p=r.shape.slice();return u.map(d=>{let h=[...p];h[o]=d;let f=hu({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[o]+=d,f})}var poe={kernelName:Vo,backendName:"webgpu",kernelFunc:doe},hoe=jt({opType:19}),foe={kernelName:li,backendName:"webgpu",kernelFunc:hoe},moe={kernelName:Fl,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,s=t,r=new ac(n.shape,20);return s.runWebGPUProgram(r,[n],n.dtype)}},goe=mn({opSnippet:11}),boe={kernelName:pi,backendName:"webgpu",kernelFunc:goe},yoe=class{constructor(e){this.variableNames=["x"],this.workPerThread=1,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]);let t=Wt(this.outputShape.length);this.uniforms=`begin : ${t}, strides : ${t}, `,this.shaderKey="stridedSlice"}getUserCode(){let e=this.outputShape.length,t="";if(e===1)t="coords * uniforms.strides + uniforms.begin";else{let s=0;t=this.outputShape.map((r,a)=>(s++,this.outputShape.length===1?`coords * uniforms.strides[${a}] + uniforms.begin[${a}]`:`coords[${s-1}] * uniforms.strides[${a}] + uniforms.begin[${a}]`)).join(",")}return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
setOutputAtIndex(index, getX(${t}));
}
}
2022-05-30 03:12:18 +02:00
`}};function voe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=kt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=Me({inputs:{x:r},backend:n,attrs:{shape:f}});else if(g||b){w.assert(r.shape.length>=1,()=>`Input must have rank at least 1, got: ${r.shape.length}`);let I=kt.computeOutShape(y,v,x),$=hu({inputs:{x:r},backend:n,attrs:{begin:y,size:I}});k=Me({inputs:{x:$},backend:n,attrs:{shape:f}}),n.disposeData($.dataId)}else if(n.shouldExecuteOnCPU([r])){let $=n.readSync(r.dataId),R=Ae(r.shape,r.dtype,$),E=Mse(h,R,x,y);k=n.makeTensorInfo(f,r.dtype,E.values)}else{let $=new yoe(h),R=[{type:"int32",data:y},{type:"int32",data:x}],E=n.runWebGPUProgram($,[r],r.dtype,R);k=Me({inputs:{x:E},backend:n,attrs:{shape:f}}),n.disposeData(E.dataId)}return k}var xoe={kernelName:Wo,backendName:"webgpu",kernelFunc:voe};function woe(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:i,rightPad:o,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.readSync(c.dataId),h=n.readSync(p.dataId),[f,m]=Lse(d,h,r,a,i,o,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var koe={kernelName:mp,backendName:"webgpu",kernelFunc:woe},Soe=jt({opType:21}),Ioe={kernelName:fi,backendName:"webgpu",kernelFunc:Soe},Coe=class{constructor(e,t){this.variableNames=["A"],this.workGroupSize=[64,1,1],this.size=!0;let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[s]*t[s];this.outputShape=n,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.rank=this.outputShape.length,this.shaderKey="tile"}getUserCode(){let e=Noe(this.rank,"uniforms.");return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromIndex(index);
setOutputAtIndex(index, getA(${e}));
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Noe(e,t=""){if(e>=5)throw Error(`Tile for rank ${e} is not yet supported`);if(e===1)return`(resRC % ${t}aShape)`;let n=["resRC.x","resRC.y","resRC.z","resRC.w"],s=[];for(let r=0;r<e;r++)s.push(`(${n[r]} % ${t}aShape[${r}])`);return s.join()}function Toe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reps:a}=s;if(n.shouldExecuteOnCPU([r])||r.dtype==="string"||r.shape.length>=5){let u=n.readSync(r.dataId),l=r.dtype==="string"?u.map(d=>w.decodeString(d)):u,c=Ae(r.shape,r.dtype,l),p=Vse(c,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let i=new Coe(r.shape,a);return n.runWebGPUProgram(i,[r],r.dtype)}var $oe={kernelName:Nr,backendName:"webgpu",kernelFunc:Toe},_oe=class{constructor(e){this.variableNames=["x","indices"],this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms=`inputSize : i32, firstPass : i32, negativeInf : f32,
2022-04-21 15:39:40 +02:00
dir : i32, inc : i32,`,this.shaderKey="swap"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let outC = getCoordsFromIndex(index);
let batch = outC[0];
let elemIdx = outC[1];
// We compare elements pair-wise within a group of size 2 * inc.
// The comparing rule for each group alternates between ascending
// and descending. Within each group, we compare each pair at
// positions i and i+inc. To decide whether an element at position i
// is x0 or x1, we mod it by 2 * inc, if the result is smaller than
// inc, it is in the first half of the group, we denote it as x0,
// otherwise we denote it as x1.
// For example, as shown in the Bitonic top K paper referenced
// above, Figure5(a) shows that element[1] is in the second half of
// the group when group size is 2, but it is in the first half of
// the group when group size is 4.
let isFirstInPair = elemIdx % (2 * uniforms.inc) < uniforms.inc;
var i = 0;
if (isFirstInPair) {
i = elemIdx;
} else {
i = elemIdx - uniforms.inc;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
var i0 = 0;
if (uniforms.firstPass == 1) {
i0 = i;
} else {
i0 = i32(getIndices(batch, i));
}
var i1 = 0;
if (uniforms.firstPass == 1) {
i1 = i + uniforms.inc;
} else {
i1 = i32(getIndices(batch, i + uniforms.inc));
}
var x0 = f32(0.0);
var x1 = f32(0.0);
if (i0 < uniforms.inputSize) {
x0 = getX(batch, i0);
} else {
x0 = uniforms.negativeInf;
}
if (i1 < uniforms.inputSize) {
x1 = getX(batch, i1);
} else {
x1 = uniforms.negativeInf;
}
let reverse = elemIdx % (2 * uniforms.dir) >= uniforms.dir;
let isGreater = x0 > x1 || (x0 == x1 && i1 > i0);
if (reverse == isGreater) {
// Elements in opposite order of direction
let iTemp = i0;
i0 = i1;
i1 = iTemp;
}
if (isFirstInPair) {
setOutputAtIndex(index, f32(i0));
} else {
setOutputAtIndex(index, f32(i1));
2022-04-21 15:38:36 +02:00
}
}
}
2022-05-30 03:12:18 +02:00
`}},Aoe=class{constructor(e){this.variableNames=["x","indices"],this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms="inputSize : i32, firstPass : i32, k : i32,",this.shaderKey="merge"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let outC = getCoordsFromIndex(index);
let batch = outC[0];
let elemIdx = outC[1];
// The output size is half of the previous size.
// If the previous sequence is | | | | _ _ _ _ | | | | _ _ _ _
// (k=4), we only need to output the indices at positions |, the
// indices at positions _ can be thrown away, see Figure5(b) After
// Phase 2 (Merge phase) in the Bitonic Top K paper referenced
// above.
// For example, the paper shows we only need to output the orange
// bars. The output sequence should look like this | | | | | | | |.
// Because the sequence is halved, to map the output index back to
// the previous sequence to find the corresponding value, we need
// to double the index. When we double the index, we basically
// interpolate a position, so 2i looks like
// | _ | _ | _ | _ | _ | _ | _. We move the | to the first k
// position of each 2k positions by - elemIdx % k. E.g. for output
// at index 4,5,6,7, we want to get the corresponding element at
// original index 8,9,10,11, for output at index 8,9,10,11,
// we want to get the corresponding element at original index
// 16,17,18,19, so on and so forth.
var i = 0;
if (elemIdx < uniforms.k) {
i = elemIdx;
} else {
i = elemIdx * 2 - elemIdx % uniforms.k;
}
var i0 = 0;
if (uniforms.firstPass == 1) {
i0 = i;
} else {
i0 = i32(getIndices(batch, i));
}
var i1 = 0;
if (uniforms.firstPass == 1) {
i1 = i + uniforms.k;
} else {
i1 = i32(getIndices(batch, i + uniforms.k));
}
let x0 = getX(batch, i0);
var x1 = f32(0.0);
if (i1 < uniforms.inputSize) {
x1 = getX(batch, i1);
} else {
x1 = x0;
}
if (x0 >= x1) {
setOutputAtIndex(index, f32(i0));
} else {
setOutputAtIndex(index, f32(i1));
}
}
2022-04-21 15:38:36 +02:00
}
2022-05-30 03:12:18 +02:00
`}};function Wi(e,t){t!==null&&e.disposeData(t.dataId)}function Gw(e){let t=1;for(;t<e;)t*=2;return t}function Eoe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:i}=s,o=r.shape,u=o[o.length-1];if(n.shouldExecuteOnCPU([r])){let k=n.readSync(r.dataId),[I,$]=Wse(k,o,r.dtype,a,i);return[n.makeTensorInfo(I.shape,I.dtype,I.values),n.makeTensorInfo($.shape,$.dtype,$.values)]}if(a===0)return o[o.length-1]=0,[n.makeTensorInfo(o,r.dtype,[]),n.makeTensorInfo(o,"int32",[])];if(u===1)return[r,fu({attrs:{shape:o,dtype:"int32",value:0},backend:n})];let c=w.sizeFromShape(o)/u,p=Me({inputs:{x:r},attrs:{shape:[c,u]},backend:n}),d=Gw(a),h=Gw(u),f=null,m=()=>f===null?[p,p]:[p,f],g=(k,I,$)=>{let R=m(),E=new _oe($),A=[{type:"int32",data:[u]},{type:"int32",data:[f===null?1:0]},{type:"float32",data:[Number.NEGATIVE_INFINITY]},{type:"int32",data:[k]},{type:"int32",data:[I]}],O=f;f=n.runWebGPUProgram(E,R,"int32",A),Wi(n,O)};for(let k=1;k<d;k*=2){let I=k*2;for(let $=k;$>=1;$/=2)g(I,$,[c,h])}for(let k=h;k>d;k/=2){let I=m(),$=new Aoe([c,k/2]),E=[{type:"int32",data:[u]},{type:"int32",data:[f===null?1:0]},{type:"int32",data:[d]}],P=f;f=n.runWebGPUProgram($,I,"int32",E),Wi(n,P);let A=d/2,O=A*2;for(let T=A;T>=1;T/=2)g(O,T,f.shape)}let b=f;f=hu({inputs:{x:f},backend:n,attrs:{begin:0,size:[c,a]}}),Wi(n,b);let y=j2({inputs:{x:p,indices:f},backend:n,attrs:{axis:1,batchDims:1}});Wi(n,p);let v=o.slice(0,-1);v.push(a),b=f,f=Me({inputs:{x:f},attrs:{shape:v},backend:n}),Wi(n,b);let x=y;return y=Me({inputs:{x:y},attrs:{shape:v},backend:n}),Wi(n,x),[y,f]}var Roe={kernelName:Go,backendName:"webgpu",kernelFunc:Eoe},Doe=class{constructor(e){this.variableNames=["Image","Transforms"],this.uniforms="interpolationModeId : i32, fillModeId : i32, fillValue : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Ve(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="transform"}getUserCode(){return`
2022-04-21 15:39:40 +02:00
fn mapCoord(outCoord : f32, len : f32) -> f32{
var inCoord = outCoord;
if(uniforms.fillModeId == 2) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz2 = 2.0 * len;
if (inCoord < sz2) {
inCoord = sz2 * f32(i32(f32(-inCoord / sz2))) +
inCoord;
}
if (inCoord < -len) {
inCoord = inCoord + sz2;
} else {
inCoord = -inCoord - 1.0;
}
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz2 = 2.0 * len;
inCoord = inCoord - sz2 * f32(i32(f32(inCoord / sz2)));
if (inCoord >= len) {
inCoord = sz2 - inCoord - 1.0;
}
}
}
return clamp(inCoord, 0.0, len - 1.0);
} else if (uniforms.fillModeId == 3) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz = len - 1.0;
inCoord = inCoord + len * (f32(i32(f32(-inCoord / sz))) + 1.0);
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz = len - 1.0;
inCoord = inCoord - len * f32(i32(f32(inCoord / sz)));
}
}
return clamp(inCoord, 0.0, len - 1.0);
} else if (uniforms.fillModeId == 4) {
return clamp(outCoord, 0.0, len - 1.0);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return outCoord;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
fn readWithFillValue(batch : i32, coordY : i32, coordX : i32,
channel : i32) -> f32 {
var outputValue : f32;
if (0 <= coordY && coordY < uniforms.imageShape[1] && 0 <= coordX && coordX < uniforms.imageShape[2]) {
outputValue = getImage(batch, coordY, coordX, channel);
} else {
outputValue = uniforms.fillValue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
return outputValue;
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
var outputValue : f32;
let batch = coords[0];
let x = coords[2];
let y = coords[1];
let channel = coords[3];
let xf = f32(x);
let yf = f32(y);
let a1 = getTransforms(batch, 0);
let a2 = getTransforms(batch, 1);
let a3 = getTransforms(batch, 2);
let b1 = getTransforms(batch, 3);
let b2 = getTransforms(batch, 4);
let b3 = getTransforms(batch, 5);
let c1 = getTransforms(batch, 6);
let c2 = getTransforms(batch, 7);
let projection = c1 * xf + c2 * yf + 1.0;
if (projection == 0.0) {
outputValue = uniforms.fillValue;
} else {
let inX = (a1 * xf + a2 * yf + a3) / projection;
let inY = (b1 * xf + b2 * yf + b3) / projection;
let mapX = mapCoord(inX, f32(uniforms.imageShape[2]));
let mapY = mapCoord(inY, f32(uniforms.imageShape[1]));
if (uniforms.interpolationModeId == 1) {
let coordY = i32(round(mapY));
let coordX = i32(round(mapX));
outputValue = readWithFillValue(batch, coordY, coordX,
channel);
} else {
let yFloor = floor(mapY);
let xFloor = floor(mapX);
let yCeil = yFloor + 1.0;
let xCeil = xFloor + 1.0;
let valueYFloor = (xCeil - mapX) *
readWithFillValue(batch, i32(yFloor), i32(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, i32(yFloor), i32(xCeil), channel);
let valueYCeil = (xCeil - mapX) *
readWithFillValue(batch, i32(yCeil), i32(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, i32(yCeil), i32(xCeil), channel);
outputValue = (yCeil - mapY) * valueYFloor +
(mapY - yFloor) * valueYCeil;
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
setOutputAtIndex(index, outputValue);
2022-04-21 15:38:36 +02:00
}
}
2022-05-30 03:12:18 +02:00
`}};function Foe(e){let{inputs:t,backend:n,attrs:s}=e,{image:r,transforms:a}=t,{interpolation:i,fillMode:o,fillValue:u,outputShape:l}=s,[c,p,d,h]=r.shape,[f,m]=l!=null?l:[p,d],g=[c,f,m,h],b=new Doe(g),y=i==="nearest"?1:2,v;switch(o){case"constant":v=1;break;case"reflect":v=2;break;case"wrap":v=3;break;case"nearest":v=4;break;default:v=1;break}let x=[{type:"int32",data:[y]},{type:"int32",data:[v]},{type:"float32",data:[u]}];return n.runWebGPUProgram(b,[r,a],"float32",x)}var Ooe={kernelName:Ho,backendName:"webgpu",kernelFunc:Foe};function Poe(e){let{inputs:t,backend:n,attrs:s}=e,{value:r}=t,{axis:a}=s;a<0&&(a+=r.shape.length);let i=r,o=i.shape.length,u=r.shape[a],l=new Array(o-1),c=0;for(let m=0;m<o;m++)m!==a&&(l[c++]=i.shape[m]);let p=[],d=new Array(o).fill(0),h=i.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){d[a]=m;let g=hu({inputs:{x:i},backend:n,attrs:{begin:d,size:h}}),b=Me({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,p.push(g)}return p.forEach(m=>n.disposeData(m.dataId)),f}var zoe={kernelName:qo,backendName:"webgpu",kernelFunc:Poe},Moe=[ose,Hse,jse,Yse,nre,rre,ire,ure,hre,bre,vre,Sre,dse,Tre,Rre,Pre,Mre,Bre,Ure,Hre,jre,Yre,Zre,sae,aae,oae,uae,lae,dae,hae,mae,wae,bae,vae,Iae,Nae,$ae,Eae,Fae,Pae,Mae,cse,Cre,Bae,Wae,Gae,qae,Kae,Yae,Qae,Jae,tie,sie,aie,oie,lie,Jre,die,hie,mie,fre,bie,vie,wie,Sie,Cie,Tie,_ie,mre,Aie,Rie,Fie,ase,zie,Bie,Wie,Gie,jie,Yie,Zie,eoe,noe,dre,xoe,koe,aoe,ooe,coe,poe,foe,moe,boe,soe,tae,Ioe,$oe,Roe,Ooe,ere,zoe,gie];for(let e of Moe)Ol(e);var Loe=class{constructor(e){this.device=e,this.numUsedBuffers=0,this.numFreeBuffers=0,this.freeBuffers=new Map,this.usedBuffers=new Map,this.numBytesUsed=0,this.numBytesAllocated=0}acquireUploadBuffer(e,t){return this.acquireBuffer(e,t,!0)}acquireBuffer(e,t,n=!1){let s=Hw(e,t);if(this.freeBuffers.has(s)||this.freeBuffers.set(s,[]),this.usedBuffers.has(s)||this.usedBuffers.set(s,[]),this.numBytesUsed+=e,this.numUsedBuffers++,this.freeBuffers.get(s).length>0){this.numFreeBuffers--;let a=this.freeBuffers.get(s).shift();return this.usedBuffers.get(s).push(a),a}this.numBytesAllocated+=e;let r=this.device.createBuffer({mappedAtCreation:n,size:e,usage:t});return this.usedBuffers.get(s).push(r),r}releaseBuffer(e,t,n){if(this.freeBuffers.size===0)return;let s=Hw(t,n);this.freeBuffers.has(s)||this.freeBuffers.set(s,[]),this.freeBuffers.get(s).push(e),this.numFreeBuffers++,this.numUsedBuffers--;let r=this.usedBuffers.get(s),a=r.indexOf(e);if(a<0)throw new Error("Cannot release a buffer that was never provided by this buffer manager");r.splice(a,1),this.numBytesUsed-=t}releaseUploadBuffer(e,t,n){e.mapAsync(GPUMapMode.WRITE).then(()=>{this.releaseBuffer(e,t,n)},s=>{})}getNumUsedBuffers(){return this.numUsedBuffers}getNumFreeBuffers(){return this.numFreeBuffers}dispose(){this.freeBuffers.forEach((e,t)=>{e.forEach(n=>{n.destroy()})}),this.usedBuffers.forEach((e,t)=>{e.forEach(n=>{n.destroy()})}),this.freeBuffers=new Map,this.usedBuffers=new Map,this.numUsedBuffers=0,this.numFreeBuffers=0,this.numBytesUsed=0,this.numBytesAllocated=0}};function Hw(e,t){return`${e}_${t}`}var Boe=class{constructor(e){this.device=e,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures=new Map,this.usedTextures=new Map,this.numBytesUsed=0,this.numBytesAllocated=0}acquireTexture(e,t,n,s){let r=jw(n),a=e*t*r,i=qw(e,t,n,s);if(this.freeTextures.has(i)||this.freeTextures.set(i,[]),this.usedTextures.has(i)||this.usedTextures.set(i,[]),this.numBytesUsed+=a,this.numUsedTextures++,this.freeTextures.get(i).length>0){this.numFreeTextures--;let u=this.freeTextures.get(i).shift();return this.usedTextures.get(i).push(u),u}this.numBytesAllocated+=a;let o=this.device.createTexture({size:[e,t],format:n,usage:s});return this.usedTextures.get(i).push(o),o}releaseTexture(e,t,n,s,r){if(this.freeTextures.size===0)return;let a=qw(t,n,s,r);this.freeTextures.has(a)||this.freeTextures.set(a,[]),this.freeTextures.get(a).push(e),this.numFreeTextures++,this.numUsedTextures--;let i=this.usedTextures.get(a),o=i.indexOf(e);if(o<0)throw new Error("Cannot release a texture that was never pr
2022-04-21 15:39:40 +02:00
${s.shape}`);if(r.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
2022-05-30 03:12:18 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let i=t.dataIdMap.get(s.dataId).id,o=t.dataIdMap.get(r.dataId).id,u=t.dataIdMap.get(a.dataId).id,l=s.shape[0],c=w.sizeFromShape(a.shape),p=t.makeOutput([l,c],s.dtype),d=t.dataIdMap.get(p.dataId).id,h=t.makeOutput([c],a.dtype),f=t.dataIdMap.get(h.dataId).id,m=t.makeOutput([3],"int32"),g=t.dataIdMap.get(m.dataId).id;KN(i,o,u,l,d,f,g);let b=t.readSync(m.dataId),y;switch(b[0]){case 0:{y=C.getSparseReshapeMultipleNegativeOneOutputDimErrorMessage(b[1],b[2]);break}case 1:{y=C.getSparseReshapeNegativeOutputDimErrorMessage(b[1],b[2]);break}case 2:y=C.getSparseReshapeEmptyTensorZeroOutputDimErrorMessage();break;case 3:{let v=Array.from(t.readSync(r.dataId)),x=Array.from(t.readSync(h.dataId));y=C.getSparseReshapeInputOutputMultipleErrorMessage(v,x);break}case 4:{let v=Array.from(t.readSync(r.dataId)),x=Array.from(t.readSync(h.dataId));y=C.getSparseReshapeInputOutputMismatchErrorMessage(v,x);break}default:y=""}if(t.disposeData(m.dataId),y)throw t.disposeData(p.dataId),t.disposeData(h.dataId),new Error(y);return[p,h]}var wde={kernelName:Dl,backendName:"wasm",setupFunc:vde,kernelFunc:xde},XN;function YN(e){XN=e.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function QN(e,t){let{backend:n,inputs:s}=e,{data:r,indices:a,segmentIds:i}=s,o=a.shape[0],u=n.readSync(i.dataId,o-1,o)[0],c=o>0?u+1:0;if(c<0)throw new Error(C.getSparseSegmentReductionNegativeSegmentIdsErrorMessage());let p=r.shape.slice();p[0]=c;let d=n.dataIdMap.get(r.dataId).id,h=n.dataIdMap.get(a.dataId).id,f=n.dataIdMap.get(i.dataId).id,m=n.makeOutput(p,r.dtype),g=n.dataIdMap.get(m.dataId).id,b=n.makeOutput([4],"int32"),y=n.dataIdMap.get(b.dataId).id;XN(d,St[r.dtype],r.shape[0],h,f,g,y,t,0);let v=n.readSync(b.dataId),x;switch(v[0]){case 0:{x=C.getSparseSegmentReductionNegativeSegmentIdsErrorMessage();break}case 1:{x=C.getSparseSegmentReductionNonIncreasingSegmentIdsErrorMessage();break}case 2:x=C.getSparseSegmentReductionSegmentIdOutOfRangeErrorMessage(v[1],v[2]);break;case 3:x=C.getSparseSegmentReductionIndicesOutOfRangeErrorMessage(v[1],v[2],v[3]);break;default:x=""}if(n.disposeData(b.dataId),x)throw n.disposeData(m.dataId),new Error(x);return m}function kde(e){return QN(e,!0)}var Sde={kernelName:pp,backendName:"wasm",setupFunc:YN,kernelFunc:kde};function Ide(e){return QN(e,!1)}var Cde={kernelName:hp,backendName:"wasm",setupFunc:YN,kernelFunc:Ide};function Nde(e){let{inputs:t,attrs:n,backend:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=n,o=w.parseAxisParam(i,r.shape)[0],u=C.prepareSplitSize(r,a,o),l=new Array(r.shape.length).fill(0),c=r.shape.slice();return u.map(p=>{let d=[...c];d[o]=p;let h=xa({inputs:{x:r},attrs:{begin:l,size:d},backend:s});return l[o]+=p,h})}var Tde={kernelName:Vo,backendName:"wasm",kernelFunc:Nde},$de=Kt(li),_de=Kt(Fl),Ade=!0,Ede=gn(pi,Ade),ZN;function Rde(e){ZN=e.wasm.cwrap(mi,null,["number","number","number","number"])}function Dde(e){let{backend:t,inputs:n,attrs:s}=e,{alpha:r}=s,{x:a}=n,i=t.dataIdMap.get(a.dataId).id,o=t.makeOutput(a.shape,a.dtype),u=t.dataIdMap.get(o.dataId).id;return ZN(i,r,St[a.dtype],u),o}var Fde={kernelName:mi,backendName:"wasm",setupFunc:Rde,kernelFunc:Dde},JN;function Ode(e){JN=e.wasm.cwrap(Wo,null,["number","array","number","array","array","array","array","array","number","number"])}function Pde(e){let{backend:t,inputs:n,attrs:s}=e,{x:r}=n,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=kt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=yn({inputs:{x:r},backend:t,attrs:{shape:f}});else if(g||b){w.assert(r.shape.length>=1,()=>`Input must have rank at least 1, got: ${r.shape.length}`);let I=kt.computeOutShape(y,v,x),$=xa({inputs:{x:r},backend:t,attrs:{begin:y,size:I}});k=yn({inputs:{x:$},backend:t,attrs:{shape:f}}),t.disposeData($.dataId)}else{let I=t.makeOutput(h,"float32"),$=t.dataIdMap.get(
");return}console.error(text)}function threadAlert(){var text=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:text,threadId:Module["_pthread_self"]()})}var err=threadPrintErr;self.alert=threadAlert;Module["instantiateWasm"]=((info,receiveInstance)=>{var instance=new WebAssembly.Instance(Module["wasmModule"],info);receiveInstance(instance);Module["wasmModule"]=null;return instance.exports});self.onmessage=(e=>{try{if(e.data.cmd==="load"){Module["wasmModule"]=e.data.wasmModule;Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob==="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}WasmBackendModuleThreadedSimd(Module).then(function(instance){Module=instance})}else if(e.data.cmd==="run"){Module["__performance_now_clock_drift"]=performance.now()-e.data.time;Module["__emscripten_thread_init"](e.data.threadInfoStruct,0,0,1);Module["establishStackSpace"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInit();try{var result=Module["invokeEntryPoint"](e.data.start_routine,e.data.arg);if(Module["keepRuntimeAlive"]()){Module["PThread"].setExitStatus(result)}else{Module["__emscripten_thread_exit"](result)}}catch(ex){if(ex!="unwind"){if(ex instanceof Module["ExitStatus"]){if(Module["keepRuntimeAlive"]()){}else{Module["__emscripten_thread_exit"](ex.status)}}else{throw ex}}}}else if(e.data.cmd==="cancel"){if(Module["_pthread_self"]()){Module["__emscripten_thread_exit"](-1)}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="processThreadQueue"){if(Module["_pthread_self"]()){Module["_emscripten_current_thread_process_queued_calls"]()}}else if(e.data.cmd==="processProxyingQueue"){if(Module["_pthread_self"]()){Module["_emscripten_proxy_execute_queue"](e.data.queue)}}else{err("worker.js received unknown command "+e.data.cmd);err(e.data)}}catch(ex){err("worker.js onmessage() captured an uncaught exception: "+ex);if(ex&&ex.stack)err(ex.stack);if(Module["__emscripten_thread_crashed"]){Module["__emscripten_thread_crashed"]()}throw ex}});`,ipe=wa(v$()),ope=class extends ol{constructor(e){super(),this.wasm=e,this.dataIdNextNumber=1,this.wasm.tfjs.initWithThreadsCount(rT),ug=this.wasm.tfjs.getThreadsCount(),this.dataIdMap=new Qd(this,ds())}write(e,t,n){let s={id:this.dataIdNextNumber++};return this.move(s,e,t,n,1),s}numDataIds(){return this.dataIdMap.numDataIds()}async time(e){let t=w.now();return e(),{kernelMs:w.now()-t}}move(e,t,n,s,r){let a=this.dataIdNextNumber++;if(s==="string"){let l=t;this.dataIdMap.set(e,{id:a,stringBytes:l,shape:n,dtype:s,memoryOffset:null,refCount:r});return}let i=w.sizeFromShape(n),o=i*w.bytesPerElement(s),u=this.wasm._malloc(o);this.dataIdMap.set(e,{id:a,memoryOffset:u,shape:n,dtype:s,refCount:r}),this.wasm.tfjs.registerTensor(a,i,u),t!=null&&this.wasm.HEAPU8.set(new Uint8Array(t.buffer,t.byteOffset,o),u)}async read(e){return this.readSync(e)}readSync(e,t,n){let{memoryOffset:s,dtype:r,shape:a,stringBytes:i}=this.dataIdMap.get(e);if(r==="string")return(t==null||t===0)&&(n==null||n>=i.length)?i:i.slice(t,n);t=t||0,n=n||w.sizeFromShape(a);let o=w.bytesPerElement(r),u=this.wasm.HEAPU8.slice(s+t*o,s+n*o);return cpe(u.buffer,r)}disposeData(e,t=!1){if(this.dataIdMap.has(e)){let n=this.dataIdMap.get(e);if(n.refCount--,!t&&n.refCount>0)return!1;this.wasm._free(n.memoryOffset),this.wasm.tfjs.disposeData(n.id),this.dataIdMap.delete(e)}return!0}refCount(e){return this.dataIdMap.has(e)?this.dataIdMap.get(e).refCount:0}incRef(e){let t=this.dataIdMap.get(e);t!=null&&t.refCount++}floatPrecision(){return 32}getMemoryOffset(e){return this.dataIdMap.get(e).memoryOffset}dispose(){this.wasm.tfjs.dispose(),"PThread"in this.wasm&&this.wasm.PThread.terminateAllThreads(),this.wasm=null}memory(){return{unreliable:!1}}makeOutput(e,t,n){let s;if(n==null)s=this.write(null,e,t);else{let r=this.dataIdNextNumber++;s={id:r},this.dataIdMap.set(s,{id:r,memoryOffset:n,shape:e,dtype:t,refCo
2022-02-10 18:27:21 +01:00
/**
* @license
* Copyright 2017 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use backend file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
2022-04-14 17:55:49 +02:00
/**
* @license
* Copyright 2022 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2022 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
2022-03-16 16:19:56 +01:00
/**
* @license
* Copyright 2022 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2022 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
2022-02-10 18:27:21 +01:00
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/** @license See the LICENSE file. */