human/dist/tfjs.esm.js

7189 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-06-02 16:39:53 +02:00
var n$=Object.create,Yd=Object.defineProperty,s$=Object.getOwnPropertyDescriptor,nk=Object.getOwnPropertyNames,r$=Object.getPrototypeOf,a$=Object.prototype.hasOwnProperty,o$=e=>Yd(e,"__esModule",{value:!0}),zt=(e,t)=>function(){return t||(0,e[nk(e)[0]])((t={exports:{}}).exports,t),t.exports},Ee=(e,t)=>{for(var n in t)Yd(e,n,{get:t[n],enumerable:!0})},i$=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of nk(t))!a$.call(e,r)&&(n||r!=="default")&&Yd(e,r,{get:()=>t[r],enumerable:!(s=s$(t,r))||s.enumerable});return e},wa=(e,t)=>i$(o$(Yd(e!=null?n$(r$(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),u$=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(D){}function s(D,T,L){this.low=D|0,this.high=T|0,this.unsigned=!!L}s.prototype.__isLong__,Object.defineProperty(s.prototype,"__isLong__",{value:!0});function r(D){return(D&&D.__isLong__)===!0}s.isLong=r;var a={},o={};function i(D,T){var L,W,j;return T?(D>>>=0,(j=0<=D&&D<256)&&(W=o[D],W)?W:(L=l(D,(D|0)<0?-1:0,!0),j&&(o[D]=L),L)):(D|=0,(j=-128<=D&&D<128)&&(W=a[D],W)?W:(L=l(D,D<0?-1:0,!1),j&&(a[D]=L),L))}s.fromInt=i;function u(D,T){if(isNaN(D))return T?x:v;if(T){if(D<0)return x;if(D>=g)return E}else{if(D<=-b)return P;if(D+1>=b)return R}return D<0?u(-D,T).neg():l(D%m|0,D/m|0,T)}s.fromNumber=u;function l(D,T,L){return new s(D,T,L)}s.fromBits=l;var c=Math.pow;function p(D,T,L){if(D.length===0)throw Error("empty string");if(D==="NaN"||D==="Infinity"||D==="+Infinity"||D==="-Infinity")return v;if(typeof T=="number"?(L=T,T=!1):T=!!T,L=L||10,L<2||36<L)throw RangeError("radix");var W;if((W=D.indexOf("-"))>0)throw Error("interior hyphen");if(W===0)return p(D.substring(1),T,L).neg();for(var j=u(c(L,8)),Y=v,X=0;X<D.length;X+=8){var Z=Math.min(8,D.length-X),ne=parseInt(D.substring(X,X+Z),L);if(Z<8){var ee=u(c(L,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(D,T){return typeof D=="number"?u(D,T):typeof D=="string"?p(D,T):l(D.low,D.high,typeof T=="boolean"?T:D.unsigned)}s.fromValue=d;var h=1<<16,f=1<<24,m=h*h,g=m*m,b=g/2,y=i(f),v=i(0);s.ZERO=v;var x=i(0,!0);s.UZERO=x;var k=i(1);s.ONE=k;var I=i(1,!0);s.UONE=I;var $=i(-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 L=u(T),W=this.div(L),j=W.mul(L).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-06-02 16:39:53 +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 ie(N,F){if(typeof WebAssembly.Function=="function"){for(var B={i:"i32",j:"i64",f:"f32",d:"f64"},Q={parameters:[],results:F[0]=="v"?[]:[B[F[0]]]},ue=1;ue<F.length;++ue)Q.parameters.push(B[F[ue]]);return new WebAssembly.Function(Q,N)}var he=[1,0,1,96],ye=F.slice(0,1),Te=F.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),Gc=new WebAssembly.Instance(ls,{e:{f:N}}),Tu=Gc.exports.f;return Tu}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,F){for(var B=N;B<N+F;B++){var Q=Eo(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"&&$o("no native wasm support detected");var Ce,ut,ot=!1,Jt;function Nt(N,F){N||$o(F)}function In(N){var F=d["_"+N];return F}function Et(N,F,B,Q,ue){var he={string:function(Tn){var Lo=0;if(Tn!=null&&Tn!==0){var dx=(Tn.length<<2)+1;Lo=zo(dx),Ls(Tn,Lo,dx)}return Lo},array:function(Tn){var Lo=zo(Tn.length);return Ms(Tn,Lo),Lo}};function ye(Tn){return F==="string"?tn(Tn):F==="boolean"?Boolean(Tn):Tn}var Te=In(N),bt=[],us=0;if(Q)for(var ls=0;ls<Q.length;ls++){var Gc=he[B[ls]];Gc?(us===0&&(us=Uf()),bt[ls]=Gc(Q[ls])):bt[ls]=Q[ls]}var Tu=Te.apply(null,bt);function t$(Tn){return us!==0&&Bc(us),ye(Tn)}return Tu=t$(Tu),Tu}function en(N,F,B,Q){B=B||[];var ue=B.every(function(ye){return ye==="number"}),he=F!=="string";return he&&ue&&!Q?In(N):function(){return Et(N,F,B,arguments,Q)}}var Cn=1;function Nn(N){var F=new TextDecoder(N);this.decode=B=>(B.buffer instanceof SharedArrayBuffer&&(B=new Uint8Array(B)),F.decode.call(F,B))}var Yt=typeof TextDecoder!="undefined"?new Nn("utf8"):void 0;function Dn(N,F,B){for(var Q=F+B,ue=F;N[ue]&&!(ue>=Q);)++ue;if(ue-F>16&&N.subarray&&Yt)return Yt.decode(N.subarray(F,ue));for(var he="";F<ue;){var ye=N[F++];if(!(ye&128)){he+=String.fromCharCode(ye);continue}var Te=N[F++]&63;if((ye&224)==192){he+=String.fromCharCode((ye&31)<<6|Te);continue}var bt=N[F++]&63;if((ye&240)==224?ye=(ye&15)<<12|Te<<6|bt:ye=(ye&7)<<18|Te<<12|bt<<6|N[F++]&63,ye<65536)he+=String.fromCharCode(ye);else{var us=ye-65536;he+=String.fromCharCode(55296|us>>10,56320|us&1023)}}return he}function tn(N,F){return N?Dn(o(),N,F):""}function zs(N,F,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;F[B++]=Te}else if(Te<=2047){if(B+1>=he)break;F[B++]=192|Te>>6,F[B++]=128|Te&63}else if(Te<=65535){if(B+2>=he)break;F[B++]=224|Te>>12,F[B++]=128|Te>>6&63,F[B++]=128|Te&63}else{if(B+3>=he)break;F[B++]=240|Te>>18,F[B++]=128|Te>>12&63,F[B++]=128|Te>>6&63,F[B++]=128|Te&63}}return F[B]=0,B-ue}function Ls(N,F,B){return zs(N,o(),F,B)}function Co(N){for(var F=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?++F:Q<=2047?F+=2:Q<=65535?F+=3:F+=4}return F}var Zs=typeof TextDecoder!="undefined"?new Nn("utf-16le"):void 0;function Ms(N,F){a().set(N,F)}function gu(N,F,B){for(var Q=0;Q<N.length;++Q)a()[F++>>0]=N.charCodeAt(Q);B||(a()[F>>0]=0)}function No(N,F){return N%F>0&&(N+=F-N%F),N}var nn,cc,dc,bu,pc,hc,Hv,fc,mc;$&&(nn=d.buffer);function rs(N){nn=N,d.HEAP8=cc=new Int8Array(N),d.HEAP16=bu=new Int16Array(N),d.HEAP32=hc=new Int32Array(N),d.HEAPU8=dc=new Uint8Array(N),d.HEAPU16=pc=new Uint16Array(N),d.HEAPU32=Hv=new Uint32Array(N),d.HEAPF32=fc=new Float32Array(N),d.HEAPF64=mc=
2022-04-21 15:39:40 +02:00
`)),u.join(`
2022-06-02 16:39:53 +02:00
`)}function p_(e,t,n,s){let r=dt(t),a=s[s.length-1],o=new Array(a).fill(0),i=t.length,u=n==="complex64"?Du(e):e;if(i>1)for(let l=0;l<r/a;l++){let c=l*a;for(let p=0;p<a;p++)o[p]=Math.max(o[p],Ru(u[c+p],0,n).length)}return o}function Ru(e,t,n){let s;return Array.isArray(e)?s=`${parseFloat(e[0].toFixed(qf))} + ${parseFloat(e[1].toFixed(qf))}j`:or(e)?s=`'${e}'`:n==="bool"?s=yk(e):s=parseFloat(e.toFixed(qf)).toString(),Vu(s,t)}function yk(e){return e===0?"false":"true"}function ad(e,t,n,s,r,a=!0){let o=n==="complex64"?2:1,i=t[0],u=t.length;if(u===0){if(n==="complex64"){let m=Du(e);return[Ru(m[0],0,n)]}return n==="bool"?[yk(e[0])]:[e[0].toString()]}if(u===1){if(i>gx){let g=$u*o,b=Array.from(e.slice(0,g)),y=Array.from(e.slice((i-$u)*o,i*o));return n==="complex64"&&(b=Du(b),y=Du(y)),["["+b.map((v,x)=>Ru(v,r[x],n)).join(", ")+", ..., "+y.map((v,x)=>Ru(v,r[i-$u+x],n)).join(", ")+"]"]}let m=n==="complex64"?Du(e):Array.from(e);return["["+m.map((g,b)=>Ru(g,r[b],n)).join(", ")+"]"]}let l=t.slice(1),c=s.slice(1),p=s[0]*o,d=[];if(i>gx){for(let m=0;m<$u;m++){let g=m*p,b=g+p;d.push(...ad(e.slice(g,b),l,n,c,r,!1))}d.push("...");for(let m=i-$u;m<i;m++){let g=m*p,b=g+p;d.push(...ad(e.slice(g,b),l,n,c,r,m===i-1))}}else for(let m=0;m<i;m++){let g=m*p,b=g+p;d.push(...ad(e.slice(g,b),l,n,c,r,m===i-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-06-02 16:39:53 +02:00
`;return d[d.length-1]=" "+d[d.length-1]+"]"+(a?"":f),d}function Du(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;O(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||ik(t,this.size),this.strides=ci(e)}set(e,...t){t.length===0&&(t=[0]),O(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,Ho=null,h_=null;function f_(e){cs=e}function m_(e){Ho=e}function g_(e){h_=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=ci(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 Ho.buffer(this.shape,this.dtype,e)}bufferSync(){return Ho.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return Xo(this.shape,e,this.dtype==="complex64")}arraySync(){return Xo(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=>wd(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=>wd(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 Ho.print(this,e)}clone(){return this.throwIfDisposed(),Ho.clone(this)}toString(e=!1){let t=this.dataSync();return d_(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Ho.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 b_(){return gg("Tensor",()=>et)}b_();var kd=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(!Ir(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: ${i.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=$k(k,[x.manifestEntry]);for(let $ in I)p[$]=I[$]}),d+=f}),p}}var bA="application/octet-stream",yA="application/json",Xg=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?(O(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,O(e!=null&&e.length>0,()=>"URL path for http must not be null, undefined or empty."),Array.isArray(e)&&O(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=_k(e,n);t.body.append("model.json",new Blob([JSON.stringify(s)],{type:yA}),"model.json"),e.weightData!=null&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:bA}),"model.weights.bin");let r=await this.fetch(this.path,t);if(r.ok)return{modelArtifactsInfo:Ll(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 Kg(t,r=>this.loadWeights(r))}async loadWeights(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,s]=vA(t),r=this.weightPathPrefix||n,a=[];for(let l of e)a.push(...l.weights);let o=[],i=[];for(let l of e)for(let c of l.paths)this.weightUrlConverter!=null?i.push(this.weightUrlConverter(c)):o.push(r+c+s);this.weightUrlConverter&&o.push(...await Promise.all(i));let u=await zk(o,{requestInit:this.requestInit,fetchFunc:this.fetch,onProgress:this.onProgress});return[a,jg(u)]}};Xg.URL_SCHEME_REGEX=/^https?:\/\//;function vA(e){let t=e.lastIndexOf("/"),n=e.lastIndexOf("?"),s=e.substring(0,t),r=n>t?e.substring(n):"";return[s+"/",r]}function vm(e){return e.match(Xg.URL_SCHEME_REGEX)!=null}var Mk=(e,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let n=!0;if(Array.isArray(e)?n=e.every(s=>vm(s)):n=vm(e),n)return Yg(e,t)}return null};wt.registerSaveRouter(Mk);wt.registerLoadRouter(Mk);function Yg(e,t){return new Xg(e,t)}function xA(e,t){return Yg(e,t)}var Xf=class{
2022-04-21 15:39:40 +02:00
Actual: ${r}.
2022-06-02 16:39:53 +02:00
Expected: ${a}.`);for(let o=0;o<a.length;++o){let i=r[o],u=a[o];if(!n(i,u))throw new Error(`Arrays differ: actual[${o}] = ${i}, expected[${o}] = ${u}.
2022-04-21 15:39:40 +02:00
Actual: ${r}.
2022-06-02 16:39:53 +02:00
Expected: ${a}.`)}}function tE(e,t){e().then(()=>t.fail(),()=>t())}function nE(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return or(e)||or(e[0])||or(t)||or(t[0])?km(e,n,(s,r)=>s==r):km(e,t,(s,r)=>eb(s,r,0))}function sE(e,t,n){if(n==null&&(n=Jg()),!eb(e,t,n))throw new Error(`Numbers differ: actual === ${e}, expected === ${t}`)}function eb(e,t,n){return!isFinite(e)&&!isFinite(t)?!0:!(isNaN(e)||isNaN(t)||Math.abs(e-t)>n)}function rE(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 aE(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 iS(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?iS(n):e[t]=zl(n)}return e}var Gpe="0.0.0";function oE(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(Cr,r)}var oe=M({add_:oE});function iE(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(Ma,r)}var uS=M({floorDiv_:iE});function uE(e,t){let n=_(e,"a","div"),s=_(t,"b","div");if([n,s]=xt(n,s),n.dtype==="int32"&&s.dtype==="int32")return uS(n,s);let r={a:n,b:s},a={};return z.runKernel(Oa,r,a)}var xe=M({div_:uE});function lE(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=M({mul_:lE});function cE(e){let t=_(e,"x","abs");if(t.dtype==="complex64"){let n={x:t};return z.runKernel(sp,n)}else{let n={x:t};return z.runKernel(di,n)}}var Lt=M({abs_:cE});function dE(e){let n={x:_(e,"x","acos")};return z.runKernel(ul,n)}var pE=M({acos_:dE});function hE(e){let n={x:_(e,"x","acosh")};return z.runKernel(ll,n)}var fE=M({acosh_:hE});function mE(e){O(Array.isArray(e),()=>"The argument passed to tf.addN() must be a list of tensors"),O(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(!Ir(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 gE=M({addN_:mE});function bE(e,t=null,n=!1){let r={x:_(e,"x","all","bool")},a={axis:t,keepDims:n};return z.runKernel(cl,r,a)}var lS=M({all_:bE});function yE(e,t=null,n=!1){let r={x:_(e,"x","any","bool")},a={axis:t,keepDims:n};return z.runKernel(dl,r,a)}var Sm=M({any_:yE});function vE(e,t=0){let s={x:_(e,"x","argMax")},r={axis:t};return z.runKernel(Ia,s,r)}var Yu=M({argMax_:vE});function xE(e,t=0){let s={x:_(e,"x","argMin")},r={axis:t};return z.runKernel(pl,s,r)}var wE=M({argMin_:xE});function kE(e){let n={x:_(e,"x","asin")};return z.runKernel(hl,n)}var SE=M({asin_:kE});function IE(e){let n={x:_(e,"x","asinh")};return z.runKernel(fl,n)}var CE=M({asinh_:IE});function NE(e){let n={x:_(e,"x","atan")};return z.runKernel(ml,n)}var TE=M({atan_:NE});function $E(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 _E=M({atan2_:$E});function AE(e){let n={x:_(e,"x","atanh")};return z.runKernel(gl,n)}var EE=M({atanh_:AE});function RE(e,t,n,s,r="NHWC",a){let o=e[3],i=[...t,o],u=pS(r);return Ml(e,i,n,a,s,null,null,u)}function cS(e,t,n,s,r,a,o="channelsLast"){let[i,u]=Nd(t),l;if(o==="channelsLast")l=[i,u,e[3],e[3]];else if(o==="channelsFirst")l=[i,u,e[1],e[1]];else throw new Error(`Unknown dataFormat ${o}`);return Ml(e,l,n,s,r,a,!1,o)}function DE(e,t,n,s,r,a,o="NDHWC"){let[i,u,l]=Im(t),c,p;if(o==="NDHWC")p="channelsLast",c=[i,u,l,e[4],e[4]];else if(o==="NCDHW")p="channelsFirst",c=[i,u,l,e[1],e[1]];else throw new Error(`Unknown dataFormat ${o}`);return dS(e,c,n,s,r,!1,p,a)}function Ml(e,t,n,s,r,a,o=!1,i="channelsLast"){let[u,l,c,p]=[-1,-1,-1,-1];if(i==="channelsLast")[u,l,c,p]=e
with dtype ${a.dtype}. `)}),n.length===1)return lr(n[0]);let s=n,r={axis:t};return z.runKernel(hi,s,r)}var Ft=M({concat_:VE});function WE(e){let n={x:_(e,"x","sigmoid","float32")};return z.runKernel(uo,n)}var qs=M({sigmoid_:WE});function UE(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(Bi,r,a)}var qe=M({slice_:UE});function GE(e){let n={x:_(e,"x","tanh","float32")};return z.runKernel(mo,n)}var Qu=M({tanh_:GE});function HE(e,t,n,s,r,a){let o=_(e,"forgetBias","basicLSTMCell"),i=_(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,i),f=oe(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=oe(V(qs(y),Qu(v)),V(c,qs(oe(o,x)))),$=V(Qu(I),qs(k));return[I,$]}var Hpe=M({basicLSTMCell_:HE});function qE(e,t,n){let s=_(e,"x","batchToSpaceND"),r=t.reduce((i,u)=>i*u);O(s.rank>=1+t.length,()=>`input rank is ${s.rank} but should be > than blockShape.length ${t.length}`),O(n.length===t.length,()=>`crops.length is ${n.length} but should be equal to blockShape.length ${t.length}`),O(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},o={blockShape:t,crops:n};return z.runKernel(pi,a,o)}var sb=M({batchToSpaceND_:qE});function jE(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 KE(e,t,n,s,r,a){a==null&&(a=.001);let o=_(e,"x","batchNorm"),i=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;s!=null&&(c=_(s,"offset","batchNorm")),O(i.rank===u.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),O(c==null||i.rank===c.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),O(l==null||i.rank===l.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let d={x:jE(o),scale:l,offset:c,mean:i,variance:u},h={varianceEpsilon:a},f=z.runKernel(Ba,d,h);return U(f,o.shape)}var Zu=M({batchNorm_:KE});function XE(e,t,n,s,r,a){let o=_(e,"x","batchNorm"),i=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;return s!=null&&(c=_(s,"offset","batchNorm")),O(o.rank===2,()=>`Error in batchNorm2D: x must be rank 2 but got rank ${o.rank}.`),O(i.rank===2||i.rank===1,()=>`Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank ${i.rank}.`),O(u.rank===2||u.rank===1,()=>`Error in batchNorm2D: variance must be rank 2 or rank 1 but got rank ${u.rank}.`),l!=null&&O(l.rank===2||l.rank===1,()=>`Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank ${l.rank}.`),c!=null&&O(c.rank===2||c.rank===1,()=>`Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank ${c.rank}.`),Zu(o,i,u,c,l,a)}var YE=M({batchNorm2d_:XE});function QE(e,t,n,s,r,a){let o=_(e,"x","batchNorm"),i=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;return s!=null&&(c=_(s,"offset","batchNorm")),O(o.rank===3,()=>`Error in batchNorm3D: x must be rank 3 but got rank ${o.rank}.`),O(i.rank===3||i.rank===1,()=>`Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank ${i.rank}.`),O(u.rank===3||u.rank===1,()=>`Error in batchNorm3D: variance must be rank 3 or rank 1 but got rank ${u.rank}.`),l!=null&&O(l.rank===3||l.rank===1,()=>`Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank ${l.rank}.`),c!=null&&O(c.rank===3||c.rank===1,()=>`Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank ${c.rank}.`),Zu(o,i,u,c,l,a)}var ZE=M({batchNorm3d_:QE});function JE(e,t,n,s,r,a){let o=_(e,"x","batchNorm"),i=_(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-06-02 16:39:53 +02:00
${s.shape}`),O(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-06-02 16:39:53 +02:00
${s.shape}`),O(o%(t*t)===0,()=>`Dimension size must be evenly divisible by ${t*t} but is ${o} for depthToSpace with input shape ${s.shape}`);let i={x:s},u={blockSize:t,dataFormat:n};return z.runKernel(gi,i,u)}var ER=M({depthToSpace_:AR});function RR(e,t,n,s,r="NHWC",a=[1,1],o){let i=_(e,"x","depthwiseConv2d","float32"),u=_(t,"filter","depthwiseConv2d","float32"),l=i,c=!1;i.rank===3&&(c=!0,l=U(i,[1,i.shape[0],i.shape[1],i.shape[2]])),O(l.rank===4,()=>`Error in depthwiseConv2d: input must be rank 4, but got rank ${l.rank}.`),O(u.rank===4,()=>`Error in depthwiseConv2d: filter must be rank 4, but got rank ${u.rank}.`),O(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]}.`),fn("depthwiseConv2d",s,o);let p={x:l,filter:u},d={strides:n,pad:s,dataFormat:r,dilations:a,dimRoundingMode:o},h=z.runKernel(Fa,p,d);return c?U(h,[h.shape[1],h.shape[2],h.shape[3]]):h}var kp=M({depthwiseConv2d_:RR});function DR(e){let n={x:_(e,"x","diag")};return z.runKernel(Tg,n)}var qpe=M({diag_:DR});function FR(e,t,n,s,r=[1,1],a="NHWC"){let o=_(e,"x","dilation2d"),i=_(t,"filter","dilation2d");O(o.rank===3||o.rank===4,()=>`Error in dilation2d: input must be rank 3 or 4, but got rank ${o.rank}.`),O(i.rank===3,()=>`Error in dilation2d: filter must be rank 3, but got rank ${i.rank}.`),O(a==="NHWC",()=>`Error in dilation2d: Only NHWC is currently supported, but got dataFormat of ${a}`);let u=o,l=!1;o.rank===3&&(u=U(o,[1,o.shape[0],o.shape[1],o.shape[2]]),l=!0);let c={x:u,filter:i},p={strides:n,pad:s,dilations:r},d=z.runKernel(ap,c,p);return l?U(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var OR=M({dilation2d_:FR});function PR(e,t){let n=_(e,"a","equal","string_or_numeric"),s=_(t,"b","equal","string_or_numeric");[n,s]=xt(n,s),at(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(bi,r)}var Xn=M({equal_:PR});function zR(e,t,n){let s=_(t,"a","where"),r=_(n,"b","where"),a=_(e,"condition","where","bool"),o=at(at(a.shape,s.shape),r.shape),i=id(a,o),u=id(s,o),l=id(r,o),c={condition:i,t:u,e:l};return z.runKernel(Mi,c)}var vn=M({where_:zR});function LR(e){let n={x:_(e,"x","zerosLike")};return z.runKernel(Xi,n)}var je=M({zerosLike_:LR});function MR(e,t){let n=_(e,"a","div"),s=_(t,"b","div");[n,s]=xt(n,s);let r=xe(n,s),a=je(r),o=Xn(s,a);return vn(o,a,r)}var BR=M({divNoNan_:MR});function VR(e,t){let n=_(e,"t1","dot"),s=_(t,"t2","dot");O((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(O(r===a,()=>`Error in dot: inner dimensions of inputs must match, but got ${r} and ${a}.`),n.rank===1&&s.rank===1){let o=U(n,[1,-1]),i=U(s,[-1,1]),u=We(o,i);return U(u,[])}else if(n.rank===1&&s.rank===2){let o=U(n,[1,-1]),i=U(s,[s.shape[0],s.shape[1]]),u=We(o,i);return U(u,[u.size])}else if(n.rank===2&&s.rank===1){let o=U(s,[-1,1]),i=We(n,o);return U(i,[i.size])}else{let o=U(s,[s.shape[0],s.shape[1]]);return We(n,o)}}var jpe=M({dot_:VR});function WR(e,...t){let n=t.map((r,a)=>_(r,`tensors${a}`,"einsum")),s={equation:e};return z.runKernel(op,n,s)}var UR=M({einsum_:WR});function GR(e){let n={x:_(e,"x","elu","float32")};return z.runKernel(Pa,n)}var Sp=M({elu_:GR});function HR(e){let t=_(e,"x","erf");O(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 qR=M({erf_:HR});function ob(e,t){for(let n=0;n<e.length;++n)if(e[e.length-n-1]!==t-1-n)return!1;return!0}function wS(e,t,n){let s=e.length+t.length,r=[],a=0,o=0;for(let i=0;i<s;i++)n.indexOf(i)===-1?r.push(e[a++]):r.push(t[o++]);return r}function kS(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 wS(e,n,t)}function jR(e,t,n){O(ob(t,n),()=>`${e} supports only inner-most axes for now. Got axes ${t} and rank-${n} input.`)}function SS(e,t){if(ob(e,t))return null;let
rank ${a.rank}.`),O(ei(t),()=>`Error in localResponseNormalization: depthRadius must be an integer but got depthRadius ${t}.`);let o=a,i=!1;a.rank===3&&(i=!0,o=U(a,[1,a.shape[0],a.shape[1],a.shape[2]]));let u={x:o},l={depthRadius:t,bias:n,alpha:s,beta:r},c=z.runKernel(up,u,l);return i?U(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var SD=M({localResponseNormalization_:kD});function ID(e){let n={x:_(e,"x","log","float32")};return z.runKernel(Ga,n)}var Qn=M({log_:ID});function CD(e){let n={x:_(e,"x","log1p")};return z.runKernel(Sl,n)}var cb=M({log1p_:CD});function Ype(e){return O(fr(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:o}=z.gradients(()=>e(s),[s],r);return r!=null&&hn(a.shape,r.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Cp(o),o[0]})}}function Qpe(e){return O(fr(e),()=>"The f passed in grads(f) must be a function"),(t,n)=>{O(Array.isArray(t),()=>"The args passed in grads(f)(args) must be an array of `Tensor`s or `TensorLike`s");let s=Ku(t,"args","tf.grads","string_or_numeric"),r=n!=null?_(n,"dy","tf.grads"):null;return z.tidy(()=>{let{value:a,grads:o}=z.gradients(()=>e(...s),s,r);return r!=null&&hn(a.shape,r.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Cp(o),o})}}function Zpe(e){return O(fr(e),()=>"The f passed in valueAndGrad(f) must be a function"),(t,n)=>{O(t instanceof et,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"),O(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 Jpe(e){return O(fr(e),()=>"The f passed in valueAndGrads(f) must be a function"),(t,n)=>{O(Array.isArray(t)&&t.every(r=>r instanceof et),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"),O(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&&hn(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 ND(e,t){O(fr(e),()=>"The f passed in variableGrads(f) must be a function"),O(t==null||Array.isArray(t)&&t.every(l=>l instanceof kd),()=>"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),O(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:o,grads:i}=z.gradients(e,t,null,a);O(i.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()."),O(o.rank===0,()=>`The f passed in variableGrads(f) must return a scalar, but it returned a rank-${o.rank} tensor`);let u={};return t.forEach((l,c)=>{i[c]!=null&&(u[l.name]=i[c])}),s!=null&&s.forEach(l=>u[l.name]=null),{value:o,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 TD(e){let n={x:_(e,"x","softplus")};return z.runKernel(Rl,n)}var Vl=M({softplus_:TD});function $D(e){let t=_(e,"x","logSigmoid");return js(s=>({value:vt(Vl(vt(s))),gradFunc:o=>V(o,qs(vt(s)))}))(t)}var ehe=M({logSigmoid_:$D});function _D(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(fo,r)}var ge=M({sub_:_D});function AD(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 i=As(r,t,!0),u=ge(r,i),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 TS=M({logSoftmax_:AD});function ED(e,t=null,n=!1){let s=_(e,"x","logSumExp"),r=ts(t,s.shape),a=As(s,r,!0),o=ge(s,a),i=Yn(o),u=ve(i,r),l=Qn(u),c=oe(U(a,l.shape),l);if(n){let p=pa(c.shape,r);return U(c,p)}return c}var RD=M({logSumExp_:ED});function DD(e,t){let n=_(e,"a","logicalAnd","bool"),s=_(t,"b","logicalAnd","bool");at(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(Ni,r)}var Ds=M({logicalAnd_:DD});function FD(e){let n={x:_(e,"x","logicalNot","bool")};return z.runKernel(Ti,n)}var db=M({logicalNot_:FD});function OD(e,t){let n=_(e,"a","logicalOr","bool"),s=_(t,"b","logicalOr","bool");at(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(Il,r)}var $S=M({logicalOr_:OD});function PD(e,t){let n=_(e,"a","logicalXor","bool"),s=_(t,"b","logicalXor","bool");return at(n.shape,s.shape),Ds($S(e,t),db(Ds(e,t)))}var the=M({logicalXor_:PD}),qc=2147483648;function zD(e,t,n="left"){let s=_(e,"sortedSequence","searchSorted"),r=_(t,"values","searchSorted"),a=s.shape[s.shape.length-1],o=r.shape[r.shape.length-1],i=U(s,[-1,a]),u=U(r,[-1,o]);if(i.rank<2)throw new Error("Sorted input argument must be at least 2-dimensional");if(i.shape[0]!==u.shape[0])throw new Error("Leading dimension of 'sortedSequence' and 'values' must match.");if(dt(u.shape)>=qc)throw new Error(`values tensor size must less than ${qc}`);if(i.shape[1]>=qc)throw new Error(`trailing dim_size must less than ${qc} for int32 output type, was ${i.shape[1]}`);let l={sortedSequence:i,values:u},c={side:n};return z.runKernel(Mg,l,c)}var _S=M({searchSorted_:zD});function LD(e,t){return _S(e,t,"left")}function MD(e,t,n,s,r){let a=_(e,"x","maxPool"),o=1,i=a,u=!1;a.rank===3&&(u=!0,i=U(a,[1,a.shape[0],a.shape[1],a.shape[2]])),O(i.rank===4,()=>`Error in maxPool: input must be rank 4 but got rank ${i.rank}.`),O(Ps(n,o),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${n} and dilations '${o}'`),fn("maxPool",s,r);let l={x:i},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 pb=M({maxPool_:MD});function BD(e,t=[1,1,1],n,s,r,a="NDHWC"){let o=_(e,"x","maxPool3d"),i=o,u=!1;o.rank===4&&(u=!0,i=U(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]])),O(i.rank===5,()=>`Error in maxPool3d: x must be rank 5 but got rank ${i.rank}.`),O(a==="NDHWC",()=>`Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of ${a}`),fn("maxPool3d",s,r);let l={x:i},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 AS=M({maxPool3d_:BD});function VD(e,t,n,s,r=!1){let o={x:_(e,"x","maxPoolWithArgmax")},i={filterSize:t,strides:n,pad:s,includeBatchInIndex:r},u=z.runKernel(Og,o,i);return{result:u[0],indexes:u[1]}}var WD=M({maxPoolWithArgmax_:VD});function UD(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")),at(n.shape,s.shape);let r={a:n,b:s};return z.runKernel(qa,r)}var Ar=M({maximum_:UD});function GD(e,t=null,n=!1){let r={x:_(e,"x","mean")},a={axis:t,keepDims:n};return z.runKernel(Ka,r,a)}var It=M({mean_:GD});function $t(e,t="float32"){if(t==="complex64"){let s=$t(e,"float32"),r=$t(e,"float32");return mr(s,r)}let n=e
${r.shape}`);if(a.rank!==1)throw new Error(`Values should be Tensor1D but received shape ${a.shape}`);if(o.rank!==1)throw new Error(`Dense shape should be Tensor1D but received shape ${o.shape}`);if(i.rank!==0)throw new Error(`Default value should be a scalar but received shape ${i.shape}`);let u={indices:r,values:a,denseShape:o,defaultValue:i},l=z.runKernel(dp,u);return{outputIndices:l[0],outputValues:l[1],emptyRowIndicator:l[2],reverseIndexMap:l[3]}}var QO=M({sparseFillEmptyRows_:YO});function ZO(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 o={inputIndices:s,inputShape:r,newShape:a},i=z.runKernel(Dl,o);return{outputIndices:i[0],outputShape:i[1]}}var JO=M({sparseReshape_:ZO});function eP(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-06-02 16:39:53 +02:00
${a.shape}`);let o={data:s,indices:r,segmentIds:a};return z.runKernel(pp,o)}var tP=M({sparseSegmentMean_:eP});function nP(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-06-02 16:39:53 +02:00
${a.shape}`);let o={data:s,indices:r,segmentIds:a};return z.runKernel(hp,o)}var sP=M({sparseSegmentSum_:nP});function rP(e,t,n,s,r,a,o,i){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:o,preserveShortSequences:i},p={data:u,dataSplits:l},d=z.runKernel(mp,p,c);return{nGrams:d[0],nGramsSplits:d[1]}}var aP=M({stringNGrams_:rP});function oP(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},o={input:s,delimiter:r},i=z.runKernel(Bg,o,a);return{indices:i[0],values:i[1],shape:i[2]}}var iP=M({stringSplit_:oP});function uP(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(Vg,r,s)}var lP=M({stringToHashBucketFast_:uP}),khe={fft:wb,ifft:$d,rfft:kb,irfft:MS},She={hammingWindow:LF,hannWindow:jS,frame:KS,stft:WF},Kn={flipLeftRight:qF,grayscaleToRGB:KF,resizeNearestNeighbor:yO,resizeBilinear:gO,rotateWithOffset:YF,cropAndResize:GF,nonMaxSuppression:ZF,nonMaxSuppressionAsync:oO,nonMaxSuppressionWithScore:uO,nonMaxSuppressionWithScoreAsync:cO,nonMaxSuppressionPadded:pO,nonMaxSuppressionPaddedAsync:fO,threshold:wO,transform:SO},cP={bandPart:CO,gramSchmidt:TO,qr:_O},Ihe={absoluteDifference:DO,computeWeightedLoss:Ys,cosineDistance:OO,hingeLoss:zO,huberLoss:MO,logLoss:VO,meanSquaredError:UO,sigmoidCrossEntropy:qO,softmaxCrossEntropy:XO},jc={sparseFillEmptyRows:QO,sparseReshape:JO,sparseSegmentMean:tP,sparseSegmentSum:sP},Yf={stringNGrams:aP,stringSplit:iP,stringToHashBucketFast:lP},Er=class extends aS{minimize(e,t=!1,n){let{value:s,grads:r}=this.computeGradients(e,n);if(n!=null){let a=n.map(o=>({name:o.name,tensor:r[o.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 ND(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(Er,Symbol.hasInstance,{value:e=>e.minimize!=null&&e.computeGradients!=null&&e.applyGradients!=null});var Nb=class extends Er{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 o=Array.isArray(e)?e[s].tensor:e[n];if(o==null)return;let i=this.accumulatedGrads[s].variable,u=this.accumulatedUpdates[s].variable;q(()=>{let l=oe(V(i,this.rho),V(ct(o),1-this.rho)),c=V(xe(dn(oe(u,this.epsilon)),dn(oe(i,this.epsilon))),o),p=oe(V(u,this.rho),V(ct(c),1-this.rho));i.assign(l),u.assign(p);let d=oe(V(c,-this.learningRate),r);r.assign(d)})}),this.incrementIterations()}dispose(){this.accumul
indices.shape[0] = ${e}`}function GP(e,t){return`indices(${e}, 0) is invalid: ${t} < 0`}function HP(e,t,n){return`indices(${e}, 0) is invalid: ${t} >= ${n}`}function qP(e,t){return`only one output dimension may be -1, not both ${e} and ${t}`}function jP(e,t){return`size ${e} must be non-negative, not ${t}`}function KP(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function XP(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 YP(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 ZP(){return"segment ids are not increasing"}function JP(e,t){return`Segment id ${e} out of range [0, ${t}), possibly because segmentIds input is not sorted.`}function ez(e,t,n){return`Bad: indices[${e}] == ${t} out of range [0, ${n})`}var tI={};Ee(tI,{collectGatherOpShapeInfo:()=>sz,computeOutShape:()=>nz,segOpComputeOptimalWindowSize:()=>tz});function tz(e,t){let n=!1,s;for(e<=Rb?(s=e,n=!0):s=vd(e,Math.floor(Math.sqrt(e)));!n;)s>t||s===e?n=!0:s=vd(e,s+1);return s}function nz(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 sz(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 o=e.shape[n],i=[],u=1,l=1,c=1;for(let p=0;p<s;++p)i.push(e.shape[p]),u*=e.shape[p];for(let p=s;p<n;p++)i.push(e.shape[p]),l*=e.shape[p];for(let p=s;p<r;p++)i.push(t.shape[p]);for(let p=n+1;p<a;p++)i.push(e.shape[p]),c*=e.shape[p];return{batchSize:u,sliceSize:c,outerSize:l,dimSize:o,outputShape:i}}function rz(e){try{return e.map(t=>wd(t))}catch(t){throw new Error(`Failed to decode encoded string bytes into utf-8, error: ${t}`)}}function az(e){return e.map(t=>zl(t))}var ws={};Ee(ws,{nonMaxSuppressionV3Impl:()=>XS,nonMaxSuppressionV4Impl:()=>YS,nonMaxSuppressionV5Impl:()=>QS,whereImpl:()=>VS});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)}},nI=class extends Error{constructor(e){super(e),Object.setPrototypeOf(this,nI.prototype)}},sI=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 nI(t)}function _x(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 qn={};function Db(e){if(e==null)return null;let t={};return t.className=e.getClassName(),t.config=e.getConfig(),t}function Tm(e){if(!(e==null||typeof e!="object"))if(Array.isArray(e))e.forEach(t=>Tm(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:Tm(s))}}}function Ul(e,t={},n={},s="object",r=!1){if(typeof e=="string"){let a=e,o;if(a in n)o=n[a];else if(a in qn)o=qn[a];else if(o=t[a],o==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-06-02 16:39:53 +02:00
2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);return o}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 o=a.className,i,u;if(o in n?[i,u]=n[o]:o in qn?[i,u]=qn.className:o in t&&([i,u]=t[o]),i==null)throw new G(`Unknown ${s}: ${o}. 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-06-02 16:39:53 +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(qn))l[h]=qn[h];for(let h of Object.keys(n))l[h]=n[h];let c=a.config;c.customObjects=l;let p={...qn};for(let h of Object.keys(n))qn[h]=n[h];Tm(a.config);let d=u(i,a.config,n,r);return qn={...p},d}else{let l={...qn};for(let p of Object.keys(n))qn[p]=n[p];let c=new i(a.config);return qn={...l},c}}}function oz(e,t){return e<t?-1:e>t?1:0}function Kc(e,t){return-1*oz(e,t)}function cr(e){if(e==null)return e;let t=[];for(let n of e)t.indexOf(n)===-1&&t.push(n);return t}function iz(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 yo(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 Fb(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 ${rI(e)}.`)}function rI(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>rI(t)).join(",")+"]":typeof e=="string"?`"${e}"`:`${e}`}function uz(e,t,n){let s=n!=null?n():w.now(),r;return(...o)=>{let i=n!=null?n():w.now();return i-s<t||(s=i,r=e(...o)),r}}function aI(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}var lz=0;function oI(){return lz++}var Xc={};function Dp(e=""){return e in Xc||(Xc[e]=0),Xc[e]+=1,e+Xc[e].toString()}var cz=["channelsFirst","channelsLast"],dz=["nearest","bilinear"],pz=["valid","same","causal"],hz=["max","avg"],fz=["sum","mul","concat","ave"],Bo=new Map;function Ct(e){yo(cz,"DataFormat",e)}function mz(e){yo(dz,"InterpolationFormat",e)}function Hn(e){yo(pz,"PaddingMode",e)}function iI(e){yo(hz,"PoolMode",e)}var Wu=[],Ax="/";function na(e,t){Wu.push(e);try{let n=t();return Wu.pop(),n}catch(n){throw Wu.pop(),n}}function gz(){return Wu.length===0?"":Wu.join(Ax)+Ax}function uI(e){if(!cI(e))throw new Error("Not a valid tensor name: '"+e+"'");return gz()+e}function lI(e){if(!cI(e))throw new Error("Not a valid tensor name: '"+e+"'");Bo.has(e)||Bo.set(e,0);let t=Bo.get(e);if(Bo.set(e,Bo.get(e)+1),t>0){let n=`${e}_${t}`;return Bo.set(n,1),n}else return e}var bz=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function cI(e){return!!e.match(bz)}function yz(e){return e===parseInt(e.toString(),10)}function dr(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 ni(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 yr(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 Zf;function Rt(){return Zf==null&&(Zf=$A().epsilon()),Zf}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 vz(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 $m(n,[1,t,1])})}function xz(e){let t=[dr(e.shape)];return U(e,t)}function wz(e){if(e.rank<=1)throw new G(`batchFlatten requires a minimum rank of 2. Got rank: ${e.rank}.`);let t=[e.shape[0],dr(e.shape,1)];return U(e,t)}function sa(e,t,n){return q(()=>{switch(e.rank){case 1:return yb(e,t,n);case 2:return LS(e,[t,0],[n,e.shape[1]]);case 3:return vb(e,[t,0,0],[n,e.shape[1],e.shape[2]]);case 4:return Td(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),jn(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,qt(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 jn(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 jn(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(i=>(n+=i,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-06-02 16:39:53 +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 i=0;i<e.length;++i){let u=i===0?0:s[i-1],l=[0,u,0],c=[1,e[i],r];a[i]=U(qe(t,l,c),this.elementShape)}return a});let o=[];for(let i=0;i<e.length;i++)o[i]=i;this.writeMany(o,a)}},ri=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}`);jn(t,r.shape,"TensorList shape mismatch: "),qt(r)}),this.idTensor=we(0),this.maxNumElements=s,qt(this.idTensor)}get id(){return this.idTensor.id}copy(){return new ri([...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.`);jn(e,this.elementShape,"TensorList shape mismatch: ");let s=Au(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=Au(this.elementShape,this.tensors,e),s=this.tensors.pop();return jn(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(jn(e.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");qt(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 ri([],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.`);jn(this.tensors[e].shape,t,"TensorList shape mismatch: ");let s=Au(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.`);jn(this.elementShape,t.shape,"TensorList shape mismatch: "),qt(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}`);jn(this.elementShape,n,"TensorList shape mismatch: "),e=e.slice(0,this.size());let s=Au(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}`);jn(this.elementShape,t,"TensorList shape mismatch: ");let n=Au(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-06-02 16:39:53 +02:00
${s}, and tensor's shape is: ${e.shape}`);let a=e.shape.slice(1),o=Km(a,n),i=s===0?0:e.size/s,u=q(()=>{let c=[];e=U(e,[1,s,i]);for(let p=0;p<t.length;++p){let d=p===0?0:r[p-1],h=[0,d,0],f=[1,t[p],i];c[p]=U(qe(e,h,f),o)}return e.dispose(),c}),l=new ri([],n,e.dtype,t.length);for(let c=0;c<u.length;c++)l.setItem(c,u[c]);return l}var R4=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),o=S("args",e,t,n);return(await a.data())[0]?n.functionMap[s].executeFunctionAsync(o,n.tensorArrayMap,n.tensorListMap):n.functionMap[r].executeFunctionAsync(o,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),o=await n.functionMap[r].executeFunctionAsync(a,n.tensorArrayMap,n.tensorListMap),i=a.map(c=>c.id),u=await o[0].data();o.forEach(c=>{!c.kept&&i.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&&i.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&&i.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=>un(r,t,n)!==void 0);if(s){let r=un(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),o=S("dynamicSize",e,t,n),i=S("clearAfterRead",e,t,n),u=S("identicalElementShapes",e,t,n),l=S("name",e,t,n),c=new T4(l,r,s,a,u,o,i);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),o=n.getTensorArray(s.id);return o.write(r,a),[o.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),o=n.getTensorArray(s.id);return o.scatter(r,a),[o.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),o=n.getTensorArray(s.id);return o.split(a,r),[o.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),o=n.getTensorList(s.id);return o.setItem(r,a),[o.idTensor]}case"TensorListGetItem":{let s=S("tensorListId",e,t,n),r=S("index",e,t,n),a=S("elementShape",e,t,n),o=S("elementDType",e,t,n);return[n.getTensorList(s.id).getItem(r,a,o)]}case"TensorListScatterV2":case"TensorListScatter":{let s=S("indices",e,t,n),r=S("tensor",e,t,n),a=S("elementShape",e,t,n),o=S("numElements",e,t,n),i=A4(r,s,a,o);return n.addTensorList(i),[i.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 o=S(a,e,t,n),i=_4(s,r,o);return n.addTensorList(i),[i.idTensor]}case"TensorListGather":{let s=S("tensorListId",e,t,n),r=S("indices",e,t,n),a=S("elementShape",e,t,n),o=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,EU),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=uv(async()=>({value:await t.iterator(),done:!1}));return mU(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=iU.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()}};su.MAX_BUFFER_SIZE=1e4;function $n(e,t=null){return new class extends su{constructor(){super(...arguments),this.size=t}async iterator(){return e()}}}function _U(e){return $n(async()=>U0(e),e.length)}function AU(e){if(!ai(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 M0(e,s=>{if(s instanceof su)return{value:s.iterator(),recurse:!1};if(ai(s))return{value:null,recurse:!0};throw new Error("Leaves of the structure passed to zip() must be Datasets, not primitives.")});return gU(n,1)},t)}function EU(e){if(e===null)return null;let t=e[0];return dU(t)?{value:RU(e),recurse:!1}:{value:null,recurse:!0}}function RU(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 j0=class extends su{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))}},Jc='"',Eu=Symbol("out"),cw=Symbol("field"),ed=Symbol("quote"),sm=Symbol("quoteafterquote"),dw=Symbol("quoteinquote"),K0=class extends su{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 j0(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],o=this.columnConfigs?this.columnConfigs[a]:null;if(!(this.configuredColumnsOnly&&!o)){let i=t[r],u=null;if(i==="")if(o&&o.default!==void 0)u=o.default;else{if(o&&(o.required||o.isLabel))throw new Error(`Required column ${a} is empty in this line: ${e}`);u=void 0}else{let l=Number(i);if(isNaN(l))o&&o.dtype==="bool"?u=this.getBoolean(i):u=i;else if(!o||!o.dtype)u=l;else switch(o.dtype){case"float32":u=l;break;case"int32":u=Math.floor(l);break;case"bool":u=this.getBoolean(i);break;default:u=l}}o&&o.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=Eu;for(let o=0;o<r;o++)switch(a){case Eu:switch(e.charAt(o)){case Jc:s=o+1,a=ed;break;case this.delimiter:if(s=o+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),a=Eu;break;default:a=cw,s=o;break}break;case cw:switch(e.charAt(o)){case this.delimiter:n.push(e.substring(s,o)),a=Eu,s=o+1;break;default:}break;case ed:switch(e.charAt(o)){case Jc:a=sm;break;default:}break;case sm:switch(e.charAt(o)){case this.delimiter:n.push(e.substring(s,o-1)),a=Eu,s=o+1;break;case Jc:a=ed;break;default:a=dw;break}break;case dw:switch(e.charAt(o)){case Jc:a=ed;break;default:}break;default:}if(a===sm?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}},X0=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-06-02 16:39:53 +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 qU(e.shape,t)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}},rC=sC;rC.nextDataId=0;var cv={};Ee(cv,{addImpl:()=>oC,bincountImpl:()=>pv,bincountReduceImpl:()=>iC,ceilImpl:()=>uC,concatImpl:()=>hv,equalImpl:()=>lC,expImpl:()=>dC,expm1Impl:()=>hC,floorImpl:()=>fC,gatherNdImpl:()=>mC,gatherV2Impl:()=>gC,greaterEqualImpl:()=>yC,greaterImpl:()=>bC,lessEqualImpl:()=>xC,lessImpl:()=>vC,linSpaceImpl:()=>wC,logImpl:()=>kC,maxImpl:()=>SC,maximumImpl:()=>IC,minimumImpl:()=>CC,multiplyImpl:()=>fv,negImpl:()=>NC,notEqualImpl:()=>TC,prodImpl:()=>$C,rangeImpl:()=>gv,rsqrtImpl:()=>_C,scatterImpl:()=>Ko,sigmoidImpl:()=>FG,simpleAbsImpl:()=>aC,sliceImpl:()=>Vd,sparseFillEmptyRowsImpl:()=>EC,sparseReshapeImpl:()=>RC,sparseSegmentReductionImpl:()=>bv,sqrtImpl:()=>zG,squaredDifferenceImpl:()=>DC,stridedSliceImpl:()=>FC,stringNGramsImpl:()=>OC,stringSplitImpl:()=>PC,stringToHashBucketFastImpl:()=>zC,subImpl:()=>LC,tileImpl:()=>MC,topKImpl:()=>VC,transposeImpl:()=>mv,uniqueImpl:()=>WC});function aC(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var jU=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=aC(r),n.makeOutput(s,t.shape,t.dtype)},KU={kernelName:di,backendName:"cpu",kernelFunc:jU};function At(e){return(t,n,s,r,a)=>{let o=C.assertAndGetBroadcastShape(t,n),i=o.length,u=w.computeStrides(o),l=w.sizeFromShape(o),c=w.getTypedArrayFromDType(a,l),p=t.length,d=n.length,h=w.computeStrides(t),f=w.computeStrides(n),m=C.getBroadcastDims(t,o),g=C.getBroadcastDims(n,o);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,i,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,o]}}function En(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.data.get(s.dataId).values,o=n.data.get(r.dataId).values,i=n.makeTensorInfo(s.shape,"complex64"),u=n.data.get(i.dataId);return u.complexTensorInfos={real:n.makeTensorInfo(s.shape,"float32",a),imag:n.makeTensorInfo(r.shape,"float32",o)},i}var XU={kernelName:np,backendName:"cpu",kernelFunc:En};function Bd(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:
2022-06-02 16:39:53 +02:00
${r.shape}`);if(o.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
${o.shape}`);let i=n.data.get(s.dataId).values,u=n.data.get(r.dataId).values,l=n.data.get(a.dataId).values,c=n.data.get(o.dataId).values[0],[p,d,h,f,m]=EC(i,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 d5={kernelName:dp,backendName:"cpu",kernelFunc:c5};function p5(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-06-02 16:39:53 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=Array.from(n.data.get(r.dataId).values),i=n.data.get(s.dataId).values,u=Array.from(n.data.get(a.dataId).values),[l,c,p]=RC(i,s.shape,s.dtype,o,u);return[n.makeTensorInfo(c,s.dtype,l),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var h5={kernelName:Dl,backendName:"cpu",kernelFunc:p5};function f5(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-06-02 16:39:53 +02:00
${a.shape}`);if(r.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let o=n.data.get(s.dataId).values,i=n.data.get(r.dataId).values,u=n.data.get(a.dataId).values,[l,c]=bv(o,s.shape,s.dtype,i,u,!0);return n.makeTensorInfo(c,s.dtype,l)}var m5={kernelName:pp,backendName:"cpu",kernelFunc:f5};function g5(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-06-02 16:39:53 +02:00
${a.shape}`);if(r.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let o=n.data.get(s.dataId).values,i=n.data.get(r.dataId).values,u=n.data.get(a.dataId).values,[l,c]=bv(o,s.shape,s.dtype,i,u);return n.makeTensorInfo(c,s.dtype,l)}var b5={kernelName:hp,backendName:"cpu",kernelFunc:g5};function y5(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:o}=t,{outputShape:i}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=C.calculateShapes(a,r,i),h=!1,f=n.bufferSync(r),m;switch(a.dtype){case"bool":{let g=n.bufferSync(a),b=Boolean(n.data.get(o.dataId).values[0]);m=Ko(f,g,i,d,c,l,u,p,b,h);break}case"float32":{let g=n.bufferSync(a),b=n.data.get(o.dataId).values[0];m=Ko(f,g,i,d,c,l,u,p,b,h);break}case"int32":{let g=n.bufferSync(a),b=n.data.get(o.dataId).values[0];m=Ko(f,g,i,d,c,l,u,p,b,h);break}case"string":{let g=n.bufferSync(a),b=w.decodeString(n.data.get(o.dataId).values[0]);m=Ko(f,g,i,d,c,l,u,p,b,h);break}default:throw new Error(`Unsupported type ${a.dtype}`)}return n.makeTensorInfo(i,m.dtype,m.values)}var v5={kernelName:fp,backendName:"cpu",kernelFunc:y5};function x5(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:o}=s,i=w.parseAxisParam(o,r.shape)[0],u=C.prepareSplitSize(r,a,i),l=new Array(r.shape.length).fill(0),c=r.shape.slice();return u.map(p=>{let d=[...c];d[i]=p;let h=ba({inputs:{x:r},backend:n,attrs:{begin:l,size:d}});return l[i]+=p,h})}var w5={kernelName:Ui,backendName:"cpu",kernelFunc:x5},k5={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 i=0;i<r.length;++i){let u=r[i];a[i]=u*u}return{dataId:s.write(a,n.shape,n.dtype),shape:n.shape,dtype:n.dtype}}},S5=st(go,(e,t)=>{let n=t;return isNaN(e)?NaN:e>0?1:n.alpha}),I5={kernelName:go,backendName:"cpu",kernelFunc:S5};function C5(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:o,strides:i,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,o,i,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),$=FC(h,I,x,y);k=n.makeTensorInfo(f,$.dtype,$.values)}return k}var N5={kernelName:Gi,backendName:"cpu",kernelFunc:C5};function T5(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:o,rightPad:i,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]=OC(d,h,r,a,o,i,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var $5={kernelName:mp,backendName:"cpu",kernelFunc:T5};function _5(e){let{inputs:t,backend:n,attrs:s}=e,{skipEmpty:r}=s,{input:a,delimiter:o}=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(o.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${o.shape}`);let i=n.data.get(a.dataId).values,u=n.data.get(o.dataId).values[0],[l,c,p]=PC(i,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 A5={kernelName:Bg,backendName:"cpu",kernelFunc:_5};function E5(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 o=n.data.get(a.dataId).values,i=zC(o,r);return n.makeTensorInfo(a.shape,"int32",i)}var R5={kernelName:Vg,backendName:"cpu",kernelFunc:E5},D5=st(Hi,e=>Math.tan(e)),F5=
`),a=r.length.toString().length+2,o=r.map((p,d)=>w.rightPad((d+1).toString(),a)+p),i=0;for(let p=0;p<o.length;p++)i=Math.max(o[p].length,i);let u=o.slice(0,s-1),l=o.slice(s-1,s),c=o.slice(s);console.log(u.join(`
2022-04-21 15:39:40 +02:00
`)),console.log(t.split(`
2022-06-02 16:39:53 +02:00
`)[0]),console.log(`%c ${w.rightPad(l[0],i)}`,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(c.join(`
`))}function l1(e){return Qs(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function c1(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 ld(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 d1(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 p1(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 pK(){return K().getNumber("WEBGL_VERSION")===2?1:4}function h1(e){return Qs(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function f1(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 m1(e){return Qs(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function Zm(e,t,n,s,r,a,o){let i=e.getAttribLocation(t,n);return i===-1?!1:(fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,s)),fe(e,()=>e.vertexAttribPointer(i,r,e.FLOAT,!1,a,o)),fe(e,()=>e.enableVertexAttribArray(i)),!0)}function g1(e,t,n){w1(e,n),fe(e,()=>e.activeTexture(e.TEXTURE0+n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function hK(e,t){w1(e,t),fe(e,()=>e.activeTexture(e.TEXTURE0+t)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function b1(e,t,n){return Qs(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function y1(e,t,n){return e.getUniformLocation(t,n)}function v1(e,t,n,s){fe(e,()=>g1(e,t,s)),fe(e,()=>e.uniform1i(n,s))}function fK(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 cd(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 Jm(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: "+x1(e,t))}function x1(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 w1(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 dd(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[ya(e),...va(e)]),t}function k1(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-06-02 16:39:53 +02:00
`):(e="",t="attribute",n="varying",s="varying",r="texture2D",a="gl_FragColor",o="",i=`
2022-04-21 15:39:40 +02:00
#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-06-02 16:39:53 +02:00
`),{version:e,attribute:t,varyingVs:n,varyingFs:s,texture2D:r,output:a,defineOutput:o,defineSpecialNaN:i,defineSpecialInf:u,defineRound:l}}function wo(e,t,n="index"){let s=w.computeStrides(t);return s.map((r,a)=>{let o=`int ${e[a]} = ${n} / ${r}`,i=a===s.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * ${r}`:`index -= ${e[a]} * ${r}`;return`${o}; ${i};`}).join("")}function th(e,t,n="index"){let s=w.computeStrides(t);return s.map((r,a)=>{let o=`int ${e[a]} = ${n} / outShapeStrides[${a}]`,i=a===s.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * outShapeStrides[${a}]`:`index -= ${e[a]} * outShapeStrides[${a}]`;return`${o}; ${i};`}).join("")}function yK(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 vK(e,t,n="index"){let s=e.map((a,o)=>o),r=yK(s,t);return r.map((a,o)=>{let i=`int ${e[o]} = ${n} / ${r[o]}`,u=o===r.length-1?`int ${e[o+1]} = ${n} - ${e[o]} * ${r[o]}`:`index -= ${e[o]} * ${r[o]}`;return`${i}; ${u};`}).join("")}function Iv(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-06-02 16:39:53 +02:00
`}function Cv(){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-06-02 16:39:53 +02:00
`}var _1=`
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-06-02 16:39:53 +02:00
`,{getBroadcastDims:A1}=C;function xK(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}=Nv(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=>wK(h,t,n.packedInputs,n.enableShapeUniforms)).join(`
`),o=t.texShape,i=mn(),u=IK(i),l,c,p=TK(i);return t.isPacked?(l=kK(t.logicalShape,o,n.enableShapeUniforms),c=NK(i)):(l=SK(t.logicalShape,o,n.enableShapeUniforms),c=CK(i)),n.packedInputs&&(p+=EK),[p,u,c,r,l,a,n.userCode].join(`
`)}function iu(e,t=!1){let n=e.shapeInfo.logicalShape;switch(n.length){case 0:return UK(e,t);case 1:return HK(e,t);case 2:return jK(e,t);case 3:return XK(e,t);case 4:return QK(e,t);case 5:return ZK(e);case 6:return JK(e);default:throw new Error(`${n.length}-D input sampling is not yet supported`)}}function E1(e,t){switch(e.shapeInfo.logicalShape.length){case 0:return WK(e);case 1:return GK(e,t);case 2:return qK(e,t);case 3:return KK(e,t);default:return YK(e,t)}}function wK(e,t,n=!1,s){let r="";n?r+=E1(e,s):r+=iu(e,s);let a=e.shapeInfo.logicalShape,o=t.logicalShape;return a.length<=o.length&&(n?r+=eX(e,t):r+=tX(e,t)),r}function kK(e,t,n){switch(e.length){case 0:return R1();case 1:return RK(e,t,n);case 2:return BK(e,t,n);case 3:return FK(e,t,n);default:return PK(e,t,n)}}function SK(e,t,n){switch(e.length){case 0:return R1();case 1:return DK(e,t,n);case 2:return VK(e,t,n);case 3:return OK(e,t,n);case 4:return zK(e,t,n);case 5:return LK(e,t);case 6:return MK(e,t);default:throw new Error(`${e.length}-D output sampling is not yet supported`)}}function IK(e){return`
2022-04-21 15:39:40 +02:00
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${e.texture2D}(textureSampler, uv).r;
}
2022-06-02 16:39:53 +02:00
`}function CK(e){return`
2022-04-21 15:39:40 +02:00
void setOutput(float val) {
${e.output} = vec4(val, 0, 0, 0);
}
2022-06-02 16:39:53 +02:00
`}function NK(e){return`
2022-04-21 15:39:40 +02:00
void setOutput(vec4 val) {
${e.output} = val;
}
2022-06-02 16:39:53 +02:00
`}function TK(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-06-02 16:39:53 +02:00
${$K}
${_K}
${AK}
`}var $K=`
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-06-02 16:39:53 +02:00
`,_K=`
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-06-02 16:39:53 +02:00
`,AK=`
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-06-02 16:39:53 +02:00
`,EK=`
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-06-02 16:39:53 +02:00
`;function R1(){return`
2022-04-21 15:39:40 +02:00
int getOutputCoords() {
return 0;
}
2022-06-02 16:39:53 +02:00
`}function RK(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-06-02 16:39:53 +02:00
`}function DK(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-06-02 16:39:53 +02:00
`}function FK(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-06-02 16:39:53 +02:00
`}function OK(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-06-02 16:39:53 +02:00
`;let s=wo(["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-06-02 16:39:53 +02:00
`}function PK(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);
}
2022-06-02 16:39:53 +02:00
`;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),o=a,i="",u="b, r, c";for(let l=2;l<e.length-1;l++)o*=e[e.length-l-1],i=`
int b${l} = index / ${o};
index -= b${l} * ${o};
`+i,u=`b${l}, `+u;return`
2022-04-21 15:39:40 +02:00
ivec${e.length} getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${s[0]}, ${s[1]}));
int index = resTexRC.x * ${s[1]} + resTexRC.y;
2022-06-02 16:39:53 +02:00
${i}
2022-04-21 15:39:40 +02:00
int b = index / ${a};
index -= b * ${a};
int r = 2 * (index / ${r});
int c = imod(index, ${r}) * 2;
return ivec${e.length}(${u});
}
2022-06-02 16:39:53 +02:00
`}function zK(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-06-02 16:39:53 +02:00
`;let s=wo(["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-06-02 16:39:53 +02:00
`}function LK(e,t){let n=wo(["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-06-02 16:39:53 +02:00
`}function MK(e,t){let n=wo(["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-06-02 16:39:53 +02:00
`}function BK(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-06-02 16:39:53 +02:00
`}function VK(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-06-02 16:39:53 +02:00
`}function ko(e){return`offset${e}`}function WK(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),s=mn();return`
2022-04-21 15:39:40 +02:00
vec4 ${n}() {
return ${s.texture2D}(${t}, halfCR);
}
2022-06-02 16:39:53 +02:00
`}function UK(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-06-02 16:39:53 +02:00
`;let o=ko(n);if(t)return`
2022-04-21 15:39:40 +02:00
float ${s}() {
2022-06-02 16:39:53 +02:00
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], ${o});
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`;let[i,u]=e.shapeInfo.texShape;return`
2022-04-21 15:39:40 +02:00
float ${s}() {
2022-06-02 16:39:53 +02:00
vec2 uv = uvFromFlat(${i}, ${u}, ${o});
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`}function GK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape,a=mn();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);
}
2022-06-02 16:39:53 +02:00
`;let o=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)];return`
2022-04-21 15:39:40 +02:00
vec4 ${s}(int index) {
vec2 uv = packedUVfrom1D(
2022-06-02 16:39:53 +02:00
${o[0]}, ${o[1]}, index);
2022-04-21 15:39:40 +02:00
return ${a.texture2D}(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`}function HK(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-06-02 16:39:53 +02:00
${uu(e)}
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`;let r=e.shapeInfo.texShape,a=r[0],o=r[1];if(o===1&&a===1)return`
2022-04-21 15:39:40 +02:00
float ${s}(int index) {
return sampleTexture(${n}, halfCR);
}
2022-06-02 16:39:53 +02:00
`;let i=ko(n);return o===1?t?`
2022-04-21 15:39:40 +02:00
float ${s}(int index) {
2022-06-02 16:39:53 +02:00
vec2 uv = vec2(0.5, (float(index + ${i}) + 0.5) / float(${n}TexShape[0]));
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
2022-06-02 16:39:53 +02:00
vec2 uv = vec2(0.5, (float(index + ${i}) + 0.5) / ${a}.0);
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
`:a===1?t?`
float ${s}(int index) {
2022-06-02 16:39:53 +02:00
vec2 uv = vec2((float(index + ${i}) + 0.5) / float(${n}TexShape[1]), 0.5);
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
2022-06-02 16:39:53 +02:00
vec2 uv = vec2((float(index + ${i}) + 0.5) / ${o}.0, 0.5);
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
`:t?`
float ${s}(int index) {
2022-06-02 16:39:53 +02:00
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], index + ${i});
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
2022-06-02 16:39:53 +02:00
vec2 uv = uvFromFlat(${a}, ${o}, index + ${i});
2022-04-21 15:39:40 +02:00
return sampleTexture(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`}function qK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape,o=a[0],i=a[1],u=mn();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) {
2022-06-02 16:39:53 +02:00
vec2 uv = (vec2(col, row) + halfCR) / vec2(${i}.0, ${o}.0);
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}function jK(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-06-02 16:39:53 +02:00
`}let{newShape:o,keptDims:i}=w.squeezeShape(n),u=o;if(u.length<n.length){let d=lu(e,u),h=["row","col"];return`
${iu(d,t)}
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col) {
2022-06-02 16:39:53 +02:00
return ${r}(${cu(h,i)});
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-06-02 16:39:53 +02:00
${uu(e)}
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`;let l=a[0],c=a[1],p=ko(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-06-02 16:39:53 +02:00
`}function KK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape,o=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)];if(n[0]===1){let d=n.slice(1),h=[1,2],f=lu(e,d),m=["b","row","col"];return`
${E1(f,t)}
2022-04-21 15:39:40 +02:00
vec4 ${r}(int b, int row, int col) {
2022-06-02 16:39:53 +02:00
return ${r}(${cu(m,h)});
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`}let i=mn();if(t)return`
2022-04-21 15:39:40 +02:00
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);
2022-06-02 16:39:53 +02:00
return ${i.texture2D}(${s}, uv);
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`;let u=o[0],l=o[1],c=Math.ceil(n[2]/2),p=c*Math.ceil(n[1]/2);return`
2022-04-21 15:39:40 +02:00
vec4 ${r}(int b, int row, int col) {
vec2 uv = packedUVfrom3D(
${u}, ${l}, ${p}, ${c}, b, row, col);
2022-06-02 16:39:53 +02:00
return ${i.texture2D}(${s}, uv);
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`}function XK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=n[1]*n[2],o=n[2],{newShape:i,keptDims:u}=w.squeezeShape(n),l=i;if(l.length<n.length){let m=lu(e,l),g=["row","col","depth"];return`
${iu(m,t)}
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col, int depth) {
2022-06-02 16:39:53 +02:00
return ${r}(${cu(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),
2022-06-02 16:39:53 +02:00
vec3(${a}, ${o}, 1)));
${uu(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);
2022-06-02 16:39:53 +02:00
float texC = dot(vec2(col, depth), vec2(${o}, 1));
2022-04-21 15:39:40 +02:00
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${d}.0, ${p}.0);
return sampleTexture(${s}, uv);
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`;if(d===o&&h==null)return t?`
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`;let f=ko(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];
2022-06-02 16:39:53 +02:00
int index = row * ${a} + col * ${o} + depth + ${f};
2022-04-21 15:39:40 +02:00
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.
2022-06-02 16:39:53 +02:00
int index = row * ${a} + col * ${o} + depth + ${f};
2022-04-21 15:39:40 +02:00
vec2 uv = uvFromFlat(${p}, ${d}, index);
return sampleTexture(${s}, uv);
}
2022-06-02 16:39:53 +02:00
`}function YK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=mn();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);
}
2022-06-02 16:39:53 +02:00
`;let a=e.shapeInfo.logicalShape,o=a.length,i=e.shapeInfo.texShape,u=[Math.ceil(i[0]/2),Math.ceil(i[1]/2)],l=u[0],c=u[1],p=Math.ceil(a[o-1]/2),d=p*Math.ceil(a[o-2]/2),h="int b, int row, int col",f=`b * ${d} + (row / 2) * ${p} + (col / 2)`;for(let m=2;m<o-1;m++)h=`int b${m}, `+h,d*=a[o-m-1],f=`b${m} * ${d} + `+f;return`
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +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],o=n[2]*a,i=n[1]*o,{newShape:u,keptDims:l}=w.squeezeShape(n);if(u.length<n.length){let y=lu(e,u),v=["row","col","depth","depth2"];return`
${iu(y,t)}
2022-04-21 15:39:40 +02:00
float ${r}(int row, int col, int depth, int depth2) {
2022-06-02 16:39:53 +02:00
return ${r}(${cu(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),
2022-06-02 16:39:53 +02:00
vec4(${i}, ${o}, ${a}, 1)));
${uu(e)}
2022-04-01 15:13:32 +02:00
}
2022-06-02 16:39:53 +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===i&&c==null)return t?`
2022-04-21 15:39:40 +02:00
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),
2022-06-02 16:39:53 +02:00
vec3(${o}, ${a}, 1));
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`;let b=ko(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.
2022-06-02 16:39:53 +02:00
int index = row * ${i} + col * ${o} +
2022-04-21 15:39:40 +02:00
depth * ${a} + depth2;
vec2 uv = uvFromFlat(${d}, ${h}, index + ${b});
return sampleTexture(${s}, uv);
}
2022-06-02 16:39:53 +02:00
`}function ZK(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,o=t[2]*a,i=t[1]*o,{newShape:u,keptDims:l}=w.squeezeShape(t);if(u.length<t.length){let m=lu(e,u),g=["row","col","depth","depth2","depth3"];return`
${iu(m)}
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth, int depth2, int depth3) {
2022-06-02 16:39:53 +02:00
return ${s}(${cu(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),
2022-06-02 16:39:53 +02:00
vec4(${i}, ${o}, ${a}, ${r})) +
2022-04-21 15:39:40 +02:00
depth3;
2022-06-02 16:39:53 +02:00
${uu(e)}
2022-04-18 18:26:05 +02:00
}
2022-06-02 16:39:53 +02:00
`;let c=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1];if(h===i&&c==null)return`
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth, int depth2, int depth3) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
2022-06-02 16:39:53 +02:00
vec4(${o}, ${a}, ${r}, 1));
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`;let f=ko(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.
2022-06-02 16:39:53 +02:00
int index = row * ${i} + col * ${o} + depth * ${a} +
2022-04-21 15:39:40 +02:00
depth2 * ${r} + depth3 + ${f};
vec2 uv = uvFromFlat(${d}, ${h}, index);
return sampleTexture(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`}function JK(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=lu(e,r),b=["row","col","depth","depth2","depth3","depth4"];return`
${iu(g)}
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
2022-06-02 16:39:53 +02:00
return ${s}(${cu(b,a)});
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`}let o=t[5],i=t[4]*o,u=t[3]*i,l=t[2]*u,c=t[1]*l;if(e.shapeInfo.isUniform)return`
2022-04-21 15:39:40 +02:00
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int index = round(dot(
vec4(row, col, depth, depth2),
2022-06-02 16:39:53 +02:00
vec4(${c}, ${l}, ${u}, ${i})) +
2022-04-21 15:39:40 +02:00
dot(
vec2(depth3, depth4),
2022-06-02 16:39:53 +02:00
vec2(${o}, 1)));
${uu(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),
2022-06-02 16:39:53 +02:00
vec4(${l}, ${u}, ${i}, ${o})) +
2022-04-21 15:39:40 +02:00
float(depth4);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`;if(f===o&&p==null)return`
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`;let m=ko(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} +
2022-06-02 16:39:53 +02:00
depth2 * ${i} + depth3 * ${o} + depth4 + ${m};
2022-04-21 15:39:40 +02:00
vec2 uv = uvFromFlat(${h}, ${f}, index);
return sampleTexture(${n}, uv);
}
2022-06-02 16:39:53 +02:00
`}function uu(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-06-02 16:39:53 +02:00
`}function eX(e,t){let n=e.name,s=n.charAt(0).toUpperCase()+n.slice(1),r="get"+s+"AtOutCoords",a=e.shapeInfo.logicalShape.length,o=t.logicalShape.length,i=A1(e.shapeInfo.logicalShape,t.logicalShape),u=rt(o),l=o-a,c,p=["x","y","z","w","u","v"];a===0?c="":o<2&&i.length>=1?c="coords = 0;":c=i.map(y=>`coords.${p[y+l]} = 0;`).join(`
`);let d="";o<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=`
2022-04-21 15:39:40 +02:00
return vec4(outputValue.xy, outputValue.xy);
2022-06-02 16:39:53 +02:00
`;else if(m&&!b)o===1?h=`
2022-04-21 15:39:40 +02:00
return vec4(outputValue.x, outputValue.x, 0., 0.);
`:h=`
return vec4(outputValue.x);
2022-06-02 16:39:53 +02:00
`;else if(i.length){let y=a-2,v=a-1;i.indexOf(y)>-1&&i.indexOf(v)>-1?h="return vec4(outputValue.x);":i.indexOf(y)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":i.indexOf(v)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return`
2022-04-21 15:39:40 +02:00
vec4 ${r}() {
${u} coords = getOutputCoords();
${c}
vec4 outputValue = get${s}(${d});
${h}
}
2022-06-02 16:39:53 +02:00
`}function tX(e,t){let n=e.name,s=n.charAt(0).toUpperCase()+n.slice(1),r="get"+s+"AtOutCoords",a=t.texShape,o=e.shapeInfo.texShape,i=e.shapeInfo.logicalShape.length,u=t.logicalShape.length;if(!e.shapeInfo.isUniform&&i===u&&e.shapeInfo.flatOffset==null&&w.arraysEqual(o,a))return`
2022-04-21 15:39:40 +02:00
float ${r}() {
return sampleTexture(${n}, resultUV);
}
2022-06-02 16:39:53 +02:00
`;let l=rt(u),c=A1(e.shapeInfo.logicalShape,t.logicalShape),p=u-i,d,h=["x","y","z","w","u","v"];i===0?d="":u<2&&c.length>=1?d="coords = 0;":d=c.map(m=>`coords.${h[m+p]} = 0;`).join(`
`);let f="";return u<2&&i>0?f="coords":f=e.shapeInfo.logicalShape.map((m,g)=>`coords.${h[g+p]}`).join(", "),`
2022-04-21 15:39:40 +02:00
float ${r}() {
${l} coords = getOutputCoords();
${d}
return get${s}(${f});
}
2022-06-02 16:39:53 +02:00
`}function rt(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 Nv(e,t,n){let{newShape:s,keptDims:r}=w.squeezeShape(t),a=t.length,o=e&&a===3&&t[0]===1,i=o?t.slice(1):s,u=!e&&a>1&&!w.arraysEqual(t,n)&&s.length<a||o;return{useSqueezeShape:u,uniformShape:u?i:t,keptDims:r}}function lu(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function cu(e,t){return t.map(n=>e[n]).join(", ")}function nX(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),o={logicalShape:s.shape,texShape:s.texData.texShape,isUniform:!1,isPacked:s.texData.isPacked,flatOffset:null},i=xK(r,o,t),u=u1(e.gl,i),l=e.createProgram(u);return K().get("ENGINE_COMPILE_ONLY")?{program:t,fragmentShader:u,source:i,webGLProgram:l,inShapeInfos:a,outShapeInfo:o,uniformLocations:null,customUniformLocations:null,infLoc:null,nanLoc:null,inShapesLocations:null,inTexShapesLocations:null,outShapeLocation:null,outShapeStridesLocation:null,outTexShapeLocation:null}:{program:t,fragmentShader:u,source:i,webGLProgram:l,inShapeInfos:a,outShapeInfo:o,...D1(e,t,l)}}function D1(e,t,n){let s={},r={},a={},o=[],i,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&&(i=e.getUniformLocation(n,"outShape",d),l=e.getUniformLocation(n,"outShapeStrides",d),u=e.getUniformLocation(n,"outTexShape",d)),t.customUniforms&&t.customUniforms.forEach((h,f)=>{o[f]=e.getUniformLocation(n,h.name,d)}),{uniformLocations:s,customUniformLocations:o,infLoc:c,nanLoc:p,inShapesLocations:r,inTexShapesLocations:a,outShapeLocation:i,outShapeStridesLocation:l,outTexShapeLocation:u}}function fw(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],o=a.shape;if(!w.arraysEqual(r,o))throw Error(`Binary was compiled with different shapes than the current args. Shapes ${r} and ${o} must match`);if(n.isUniform&&a.isUniform)return;let i=n.texShape,u=a.isUniform?null:a.texData.texShape;if(!w.arraysEqual(i,u))throw Error(`Binary was compiled with different texture shapes than the current args. Shape ${i} and ${u} must match`)})}function sX(e,t,n,s,r){t.program.enableShapeUniforms||(fw(t.inShapeInfos,n),fw([t.outShapeInfo],[s]));let a=s.texData.texture,o=s.texData.texShape;s.texData.isPacked?e.setOutputPackedMatrixTexture(a.texture,o[0],o[1]):e.setOutputMatrixTexture(a.texture,o[0],o[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}=Nv(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-06-02 16:39:53 +02:00
${this.enableShapeUniforms?th(["r","c","d"],e):wo(["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-06-02 16:39:53 +02:00
`}},oX=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let t=mn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
2022-04-21 15:39:40 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2022-06-02 16:39:53 +02:00
${this.enableShapeUniforms?th(["r","c","d"],e):wo(["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-06-02 16:39:53 +02:00
`}},iX=class{constructor(e){this.variableNames=["A"],this.outTexUsage=3;let t=mn();this.outputShape=e,this.userCode=`
${_1}
2022-04-21 15:39:40 +02:00
void main() {
float x = getAAtOutCoords();
${t.output} = encode_float(x);
}
2022-06-02 16:39:53 +02:00
`}},uX=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=3;let t=mn();this.outputShape=e,this.userCode=`
${_1}
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-06-02 16:39:53 +02:00
`}},lX=class{constructor(e,t=!1){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=mn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let s="result";t&&(s="floor(result * 255. + 0.5)"),this.userCode=`
${this.enableShapeUniforms?Cv():Iv(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-06-02 16:39:53 +02:00
`}},cX=class{constructor(e,t=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=mn();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 o=0;o<=1;o++){let i=a*2+o;s+=`
2022-04-21 15:39:40 +02:00
localCoords = coords;
2022-06-02 16:39:53 +02:00
if(localCoords[2] + ${o} < ${this.enableShapeUniforms?"outShape[2]":`${e[2]}`}) {
localCoords[2] += ${o};
2022-04-21 15:39:40 +02:00
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) {
2022-06-02 16:39:53 +02:00
result[${i}] = values[0];
2022-04-21 15:39:40 +02:00
} else if (offset == 1) {
2022-06-02 16:39:53 +02:00
result[${i}] = values[1];
2022-04-21 15:39:40 +02:00
} else if (offset == 2) {
2022-06-02 16:39:53 +02:00
result[${i}] = values[2];
2022-02-10 18:27:21 +01:00
} else {
2022-06-02 16:39:53 +02:00
result[${i}] = 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-06-02 16:39:53 +02:00
${this.enableShapeUniforms?Cv():Iv(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-06-02 16:39:53 +02:00
`}},dX={};Ee(dX,{bindVertexProgramAttributeStreams:()=>W1,createBufferFromOutputTexture:()=>H1,createFloat16MatrixTexture:()=>L1,createFloat16PackedMatrixTexture:()=>V1,createFloat32MatrixTexture:()=>z1,createIndexBuffer:()=>P1,createPackedMatrixTexture:()=>B1,createUnsignedBytesMatrixTexture:()=>M1,createVertexBuffer:()=>O1,createVertexShader:()=>F1,downloadByteEncodedFloatMatrixFromOutputTexture:()=>j1,downloadFloat32MatrixFromBuffer:()=>q1,downloadMatrixFromPackedOutputTexture:()=>X1,downloadPackedMatrixFromBuffer:()=>K1,getInternalFormatForFloat16MatrixTexture:()=>$v,getInternalFormatForFloat16PackedMatrixTexture:()=>Ev,getInternalFormatForFloat32MatrixTexture:()=>Tv,getInternalFormatForPackedMatrixTexture:()=>Av,getInternalFormatForUnsignedBytesMatrixTexture:()=>_v,uploadDenseMatrixToTexture:()=>U1,uploadPixelDataToTexture:()=>G1});function F1(e){let t=mn(),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-06-02 16:39:53 +02:00
}`;return i1(e,n)}function O1(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 d1(e,t)}function P1(e){let t=new Uint16Array([0,1,2,2,1,3]);return p1(e,t)}function ec(e,t,n,s,r,a){f1(t,n);let o=h1(e),i=e.TEXTURE_2D;return fe(e,()=>e.bindTexture(i,o)),fe(e,()=>e.texParameteri(i,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),fe(e,()=>e.texParameteri(i,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),fe(e,()=>e.texParameteri(i,e.TEXTURE_MIN_FILTER,e.NEAREST)),fe(e,()=>e.texParameteri(i,e.TEXTURE_MAG_FILTER,e.NEAREST)),K().getNumber("WEBGL_VERSION")===1?fe(e,()=>e.texImage2D(i,0,s,t,n,0,r,a,null)):fe(e,()=>e.texStorage2D(i,1,s,t,n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null)),{texture:o,texShape:[n,t]}}function Tv(e){return e.internalFormatFloat}function z1(e,t,n,s){let[r,a]=Jl(t,n);return ec(e,r,a,Tv(s),s.textureFormatFloat,e.FLOAT)}function $v(e){return e.internalFormatHalfFloat}function L1(e,t,n,s){let[r,a]=Jl(t,n);return ec(e,r,a,$v(s),s.textureFormatFloat,s.textureTypeHalfFloat)}function _v(e){return e.downloadTextureFormat}function M1(e,t,n,s){let[r,a]=Jl(t,n);return ec(e,r,a,_v(s),e.RGBA,e.UNSIGNED_BYTE)}function Av(e){return e.internalFormatPackedFloat}function B1(e,t,n,s){let[r,a]=au(t,n);return ec(e,r,a,Av(s),e.RGBA,e.FLOAT)}function Ev(e){return e.internalFormatPackedHalfFloat}function V1(e,t,n,s){let[r,a]=au(t,n);return ec(e,r,a,Ev(s),e.RGBA,s.textureTypeHalfFloat)}function W1(e,t,n){return fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),Zm(e,t,"clipSpacePos",n,3,20,0)&&Zm(e,t,"uv",n,2,20,12)}function U1(e,t,n,s,r,a){fe(e,()=>e.bindTexture(e.TEXTURE_2D,t));let o,i,u;r instanceof Uint8Array?(o=new Uint8Array(n*s*4),i=e.UNSIGNED_BYTE,u=e.RGBA):(o=new Float32Array(n*s*4),i=e.FLOAT,u=a.internalFormatPackedFloat),o.set(r),K().getNumber("WEBGL_VERSION")===2?fe(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,s,e.RGBA,i,o)):fe(e,()=>e.texImage2D(e.TEXTURE_2D,0,u,n,s,0,e.RGBA,i,o)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function G1(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 H1(e,t,n,s){let r=e.createBuffer();fe(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,r));let i=4*4*t*n;return fe(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,i,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 q1(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 j1(e,t,n,s){let[r,a]=Jl(t,n),o=4,i=new Uint8Array(oK(t*n,o));return fe(e,()=>e.readPixels(0,0,r,a,s.downloadTextureFormat,e.UNSIGNED_BYTE,i)),new Float32Array(i.buffer)}function K1(e,t,n,s,r,a,o,i){let u=e,l=new Float32Array(iK(a,o));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 X1(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 am=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,sK(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=Lu(this.gl,r),Mn(this.gl,a))this.textureHalfFloatExtension=Lu(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-06-02 16:39:53 +02:00
`;else{let t=ln("rc",this.rank),n=rt(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-06-02 16:39:53 +02:00
rEdge || cEdge ? 0. : getA(${t[3]})`}},e2=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-06-02 16:39:53 +02:00
${e8(t,this.enableShapeUniforms)}
${this.enableShapeUniforms?Cv():Iv(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-06-02 16:39:53 +02:00
`}};function e8(e,t){return`
2022-04-21 15:39:40 +02:00
ivec3 inputCoordsFromReshapedOutCoords(int index) {
2022-06-02 16:39:53 +02:00
${t?vK(["r","c","d"],"inputShape"):wo(["r","c","d"],e)}
2022-04-21 15:39:40 +02:00
return ivec3(r, c, d);
}
2022-06-02 16:39:53 +02:00
`}var t8=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=gw(t,n),r=bw(e,s,n);r in this.freeTextures||(this.freeTextures[r]=[]),r in this.usedTextures||(this.usedTextures[r]=[]);let a=mw(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 i=this.freeTextures[r].shift();return this.usedTextures[r].push(i),i}let o;return s===3?o=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):s===4?o=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):s===1?o=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):s===0?o=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):s===2&&(o=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[r].push(o),this.numUsedTextures++,this._numBytesAllocated+=a,this.log(),o}releaseTexture(e,t,n,s){if(this.freeTextures==null)return;let r=gw(n,s),a=bw(t,r,s);a in this.freeTextures||(this.freeTextures[a]=[]);let o=mw(t,r,this.gpgpu.gl,this.gpgpu.textureConfig,s),i=K().get("WEBGL_DELETE_TEXTURE_THRESHOLD");i!==-1&&this._numBytesAllocated>i?(this.gpgpu.deleteMatrixTexture(e.texture),this._numBytesAllocated-=o):(this.freeTextures[a].push(e),this.numFreeTextures++,this._numBytesFree+=o),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 n8(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 mw(e,t,n,s,r){let a=s8(t,s),o;if(r){let[u,l]=au(e[0],e[1]);o=u*l}else{let[u,l]=Jl(e[0],e[1]);o=u*l}let i=n8(n,a);return o*i}function s8(e,t){switch(e){case 3:return Av(t);case 4:return Ev(t);case 1:return Tv(t);case 0:return $v(t);case 2:return _v(t);default:throw new Error(`Unknown physical texture type ${e}`)}}function r8(e){return K().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?3:1:e?4:0}function gw(e,t){if(e===1)return 3;if(e===0||e==null)return r8(t);if(e===3||e===2)return 2;throw new Error(`Unknown logical texture type ${e}`)}function bw(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-06-02 16:39:53 +02:00
`}},ss="if (isnan(x)) return x;",a8="return x;",yw="return abs(x);",o8="return (x >= 0.0) ? x : (exp(x) - 1.0);",i8=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : x;
2022-06-02 16:39:53 +02:00
`,u8=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2022-06-02 16:39:53 +02:00
`,Vo="return x;",l8="return 1.0 / (1.0 + exp(-1.0 * x));",c8="return x;",d8=`
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-06-02 16:39:53 +02:00
`,p8=`
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-06-02 16:39:53 +02:00
`,h8=`
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-06-02 16:39:53 +02:00
`,f8="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-06-02 16:39:53 +02:00
`}},m8=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=ln("rc",t),s=rt(t),r=ZX(t,n),a=n.slice(-2),o=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});
2022-06-02 16:39:53 +02:00
setOutput(getChannel(packedInput, ${o}));
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
`}},g8=ws.whereImpl,b8=1e-7,y8=1e-4,sd={};function v8(e){return e in sd||(sd[e]={}),sd[e]}var x8=K().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),w8=600;function k8(){return K().global.screen==null?1024:K().global.screen.height*K().global.screen.width*window.devicePixelRatio*w8/1024/1024}var t2=class extends il{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 am)t=e;else{let n=xs(K().getNumber("WEBGL_VERSION"),e);t=new am(n)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{let n=xs(K().getNumber("WEBGL_VERSION"));t=new am(n),this.binaryCache=v8(K().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=t,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new t8(this.gpgpu),this.numMBBeforeWarning=k8(),this.texData=new Zd(this,ds())}nextDataId(){return t2.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:o,isPacked:i}=t;if(a!=null){let p;i?p=new ea(o,Vo):p=new Gs(o,Vo);let d=this.runWebGLProgram(p,[{dataId:e,shape:o,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:o,isPacked:i}=t;if(r!=null){let h;i?h=new ea(s,Vo):h=new Gs(s,Vo);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,...td(s))}this.pendingRead.set(e,[]),a!=="complex64"&&await this.gpgpu.createAndWaitForFence();let c;if(a==="complex64"){let h=await Promise.all([this.read(o.real.dataId),this.read(o.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-06-02 16:39:53 +02:00
`,li=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=`
2022-06-02 16:39:53 +02:00
${rt(r)} coords = getOutputCoords();
2022-04-21 15:39:40 +02:00
`,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-06-02 16:39:53 +02:00
`;else{let i=ln("coords",r);this.enableShapeUniforms?a+=`
2022-04-21 15:39:40 +02:00
bool nextRowOutOfBounds =
2022-06-02 16:39:53 +02:00
(${i[r-2]} + 1) >= outShape[${r} - 2];
2022-04-21 15:39:40 +02:00
bool nextColOutOfBounds =
2022-06-02 16:39:53 +02:00
(${i[r-1]} + 1) >= outShape[${r} - 1];
2022-04-21 15:39:40 +02:00
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`:a+=`
bool nextRowOutOfBounds =
2022-06-02 16:39:53 +02:00
(${i[r-2]} + 1) >= ${this.outputShape[r-2]};
2022-04-21 15:39:40 +02:00
bool nextColOutOfBounds =
2022-06-02 16:39:53 +02:00
(${i[r-1]} + 1) >= ${this.outputShape[r-1]};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +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 C8={kernelName:Wa,backendName:"webgl",kernelFunc:Rn};function Fr(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.makeTensorInfo(s.shape,"complex64"),o=n.texData.get(a.dataId),i=Rn({inputs:{x:s},backend:n}),u=Rn({inputs:{x:r},backend:n});return o.complexTensorInfos={real:i,imag:u},a}var N8={kernelName:np,backendName:"webgl",kernelFunc:Fr},r2="return (a < 0.) ? b * a : a;",a2=`
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-06-02 16:39:53 +02:00
`;function T8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{alpha:a}=s,o=n.makeTensorInfo([],"float32",w.createScalarValue(a,"float32")),i=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new tc(a2,r.shape,o.shape):new li(r2,r.shape,o.shape),u=n.runWebGLProgram(i,[r,o],"float32");return n.disposeIntermediateTensorInfo(o),u}var $8={kernelName:Ua,backendName:"webgl",kernelFunc:T8},o2="return (a < 0.) ? b * a : a;",i2=`
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-06-02 16:39:53 +02:00
`;function _8(e){let{inputs:t,backend:n}=e,{x:s,alpha:r}=t,a=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new tc(i2,s.shape,r.shape):new li(o2,s.shape,r.shape);return n.runWebGLProgram(a,[s,r],"float32")}var A8={kernelName:to,backendName:"webgl",kernelFunc:_8},du="if (isnan(x)) return x;",E8=`
2022-04-21 15:39:40 +02:00
if (isnan(a)) return a;
if (isnan(b)) return b;
2022-06-02 16:39:53 +02:00
`,R8=`
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-06-02 16:39:53 +02:00
`;function Ke({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:s}){return({inputs:r,backend:a})=>{let{x:o}=r,i=a,u=s||o.dtype;if(i.shouldExecuteOnCPU([o])&&n!=null){let p=i.texData.get(o.dataId),d=n(p.values,u);return i.makeTensorInfo(o.shape,u,d)}let l=K().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,c;return l?c=new ea(o.shape,t):c=new Gs(o.shape,e),i.runWebGLProgram(c,[o],u)}}function jt({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:s=!1,cpuKernelImpl:r,dtype:a}){return({inputs:o,backend:i})=>{let{a:u,b:l}=o,c=i;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 li(e,u.shape,l.shape);return c.runWebGLProgram(R,[I,$],cn(x.dtype,k.dtype))}),y=Fr({inputs:{real:g,imag:b},backend:c});return c.disposeIntermediateTensorInfo(g),c.disposeIntermediateTensorInfo(b),y}let p=a||cn(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 li(e,u.shape,l.shape),c.runWebGLProgram(h,[u,l],p)}}function sh(e,t=!1){if(e==="linear")return t?c8:a8;if(e==="relu")return t?p8:i8;if(e==="elu")return t?d8:o8;if(e==="relu6")return t?h8:u8;if(e==="prelu")return t?i2:o2;if(e==="leakyrelu")return t?a2:r2;if(e==="sigmoid")return t?f8:l8;throw new Error(`Activation ${e} has not been implemented for the WebGL backend.`)}var u2=class{constructor(e,t,n,s=!1,r=!1,a=!1,o=null,i=!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="";o&&(i?m=`vec4 activation(vec4 a) {
2022-04-21 15:39:40 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}`:u?m=`vec4 activation(vec4 a) {
vec4 b = getLeakyreluAlphaAtOutCoords();
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}`:m=`vec4 activation(vec4 x) {
2022-06-02 16:39:53 +02:00
${o}
}`,g="result = activation(result);");let b=a?"result += getBiasAtOutCoords();":"";a&&this.variableNames.push("bias"),i&&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=`
2022-04-21 15:39:40 +02:00
${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-06-02 16:39:53 +02:00
`}},vw={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},xw=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-06-02 16:39:53 +02:00
`}},ww="return a * b;";function Dv(e){let{inputs:t,backend:n}=e,{a:s,b:r}=t,a=C.upcastType(s.dtype,r.dtype);if(s.dtype==="complex64"){let i=n.texData.get(s.dataId),u=n.texData.get(r.dataId),l=new xw(vw.REAL,s.shape,r.shape),c=new xw(vw.IMAG,s.shape,r.shape),p=[{dataId:i.complexTensorInfos.real.dataId,dtype:i.complexTensorInfos.real.dtype,shape:s.shape},{dataId:i.complexTensorInfos.imag.dataId,dtype:i.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=Fr({inputs:{real:d,imag:h},backend:n});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}if(n.shouldExecuteOnCPU([s,r])){let i=n.texData.get(s.dataId),u=n.texData.get(r.dataId),[l,c]=RX(s.shape,r.shape,i.values,u.values,a),p=n.makeTensorInfo(c,a),d=n.texData.get(p.dataId);return d.values=l,p}let o;return K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?o=new tc(ww,s.shape,r.shape):o=new li(ww,s.shape,r.shape),n.runWebGLProgram(o,[s,r],a)}var D8={kernelName:Za,backendName:"webgl",kernelFunc:Dv};function F8(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)],o=new e2(a,s),i=!0,u=[s],l=n.runWebGLProgram(o,[r],e.dtype,u,i);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,o=n,i=w.sizeFromShape(r.shape),u=w.inferFromImplicitShape(a,i),l=w.sizeFromShape(u);w.assert(i===l,()=>`The new shape (${u}) has ${l} elements and the old shape (${r.shape}) has ${i} elements. The new shape and old shape must have the same number of elements.`);let c=o.texData.get(r.dataId);return c.isPacked&&!al(r.shape,u)&&!(c.texture!==null&&al(c.shape,u))?F8(r,u,o):(o.incRef(r.dataId),{dataId:r.dataId,shape:u,dtype:r.dtype})}var O8={kernelName:Oi,backendName:"webgl",kernelFunc:pe},kw=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:s,inSize:r,outSize:a}=e;this.outputShape=[s,a];let o=Math.floor(n/4)*4,i=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;
2022-06-02 16:39:53 +02:00
for (int i = 0; i < ${o}; i += 4) {
2022-04-21 15:39:40 +02:00
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
2022-06-02 16:39:53 +02:00
int inIdx = inOffset + ${o};
if (${i===1}) {
2022-04-21 15:39:40 +02:00
vec4 values = vec4(getValue(batch, inIdx), 0.0, 0.0, 0.0);
${u}
2022-06-02 16:39:53 +02:00
} else if (${i===2}) {
2022-04-21 15:39:40 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1), 0.0, 0.0);
${u}
2022-06-02 16:39:53 +02:00
} else if (${i===3}) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},P8=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:s,inSize:r,outSize:a}=e;this.outputShape=[s,a];let o="0.0",i="";t==="prod"?o="1.0":t==="min"?(o="1.0 / 1e-20",i="min"):t==="max"&&(o="-1.0 / 1e-20",i="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 {
2022-06-02 16:39:53 +02:00
minMaxValue = ${i}(values, minMaxValue);
2022-04-21 15:39:40 +02:00
if (${t==="min"} || ${t==="max"}) {
2022-06-02 16:39:53 +02:00
minMaxValue = ${i}(values, minMaxValue);
2022-04-21 15:39:40 +02:00
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
}
2022-06-02 16:39:53 +02:00
`,d="vec4";t==="all"?(o="1.0",p=`
2022-04-21 15:39:40 +02:00
bool reducedAllValue = all(values);
float floatedReducedAllValue = float(reducedAllValue);
allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
2022-06-02 16:39:53 +02:00
`,d="bvec4"):t==="any"&&(o="0.0",p=`
2022-04-21 15:39:40 +02:00
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=`
2022-06-02 16:39:53 +02:00
const float initializationValue = ${o};
2022-04-21 15:39:40 +02:00
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};
2022-06-02 16:39:53 +02:00
vec4 minMaxValue = vec4(${o});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function z8(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 So(e,t,n,s){let r=z8(e.shape),a=e;for(let o=0;o<r.length;o++){let{inSize:i,windowSize:u,outSize:l}=r[o],c,p;n==="mean"?c=o===0?new kw({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:l},i):new kw({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:l}):c=new P8({windowSize:u,inSize:i,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 L8=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=rt(this.rank),r=M8(t);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${s} resRC = getOutputCoords();
setOutput(getA(${r}));
}
2022-06-02 16:39:53 +02:00
`}};function M8(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 B8=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=rt(this.rank),r=J1("rc",this.rank),a=new Array(this.rank);for(let l=0;l<t.length;l++)a[t[l]]=r[l];let o=`vec2(${a.slice(-2).join()})`,i=`++${r[this.rank-1]} < ${n[this.rank-1]}`,u=`getChannel(getA(${a.join()}), ${o})`;this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${s} rc = getOutputCoords();
vec4 result = vec4(0.);
result[0] = ${u};
2022-06-02 16:39:53 +02:00
if(${i}) {
2022-04-21 15:39:40 +02:00
result[1] = ${u};
}
--${r[this.rank-1]};
if(++${r[this.rank-2]} < ${n[this.rank-2]}) {
result[2] = ${u};
2022-06-02 16:39:53 +02:00
if(${i}) {
2022-04-21 15:39:40 +02:00
result[3] = ${u};
}
}
setOutput(result);
}
2022-06-02 16:39:53 +02:00
`}};function rh(e,t,n){let s=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new B8(e.shape,t):new L8(e.shape,t);return n.runWebGLProgram(s,[e],e.dtype)}function V8(e,t,n,s){let r=t,a=e.shape.length,o=w.parseAxisParam(r,e.shape),i=o,u=C.getAxesPermutation(i,a),l=u!=null,c=e;l&&(c=rh(e,u,s),i=C.getInnerMostAxes(i.length,a)),C.assertAxesAreInnerMostDims("sum",i,a);let[p,d]=C.computeOutAndReduceShapes(c.shape,i),h=p;n&&(h=C.expandShapeToKeepDim(p,o));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=So(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:o}=s;return V8(r,a,o,n)}var W8={kernelName:co,backendName:"webgl",kernelFunc:ah};function pn(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{perm:a}=s,o=n,i=r.shape.length,u=new Array(i);for(let c=0;c<u.length;c++)u[c]=r.shape[a[c]];let l;if(o.shouldExecuteOnCPU([r])){let p=o.texData.get(r.dataId).values,d=Rv(p,r.shape,r.dtype,a,u);l=o.makeTensorInfo(u,r.dtype);let h=o.texData.get(l.dataId);h.values=d}else l=rh(r,a,o);return l}var U8={kernelName:Hs,backendName:"webgl",kernelFunc:pn},l2=1e3;function Hd({a:e,b:t,transposeA:n,transposeB:s,backend:r,bias:a=null,preluActivationWeights:o=null,leakyreluAlpha:i=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=Qi.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],D=a!=null,T=o!=null,L=u==="leakyrelu",W=u!=null?sh(u,!0):null,j=D||T||L||W!=null,Y;if((h===1||f===1)&&A>l2&&j===!1){let Z=$,ne=R;n&&(Z=pn({inputs:{x:$},backend:r,attrs:{perm:[0,2,1]}}),E.push(Z)),s&&(ne=pn({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 ie=f===1?2:1,ae=ne;se&&(ae=pe({inputs:{x:ne},backend:r,attrs:{shape:[P,1,A]}}),E.push(ae));let de=Dv({inputs:{a:te,b:ae},backend:r});Y=ah({inputs:{x:de},backend:r,attrs:{axis:ie,keepDims:!0}}),E.push(de)}else{let Z=cn(e.dtype,t.dtype),ne=new u2(k,I,[P,h,f],n,s,D,W,T,L),ee=[$,R];if(a!=null&&ee.push(a),T&&ee.push(o),L){let se=r.makeTensorInfo([],"float32",w.createScalarValue(i,"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 G8(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a,bias:o,preluActivationWeights:i}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=s;return Hd({a:r,b:a,transposeA:u,transposeB:l,backend:n,bias:o,preluActivationWeights:i,leakyreluAlpha:p,activation:c})}var H8={kernelName:oa,backendName:"webgl",kernelFunc:G8},Sw="return abs(x);";function q8(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])&&s.dtype!=="complex64"){let a=n.texData.get(s.dataId),o=Q1(a.values);return n.makeTensorInfo(s.shape,s.dtype,o)}let r;return K().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new ea(s.shape,Sw):r=new Gs(s.shape,Sw),n.runWebGLProgram(r,[s],s.dtype)}var j8={kernelName:di,backendName:"webgl",kernelFunc:q8},K8=ss+`
2022-04-21 15:39:40 +02:00
if (abs(x) > 1.) {
return NAN;
}
return acos(x);
2022-06-02 16:39:53 +02:00
`,X8=Ke({opSnippet:K8}),Y8={kernelName:ul,backendName:"webgl",kernelFunc:X8},Q8=ss+`
2022-04-21 15:39:40 +02:00
if (x < 1.0) return NAN;
2022-06-02 16:39:53 +02:00
return log(x + sqrt(x * x - 1.0));`,Z8=Ke({opSnippet:Q8}),J8={kernelName:ll,backendName:"webgl",kernelFunc:Z8},Iw="return a + b;",eY=jt({opSnippet:Iw,packedOpSnippet:Iw,supportsComplex:!0,cpuKernelImpl:hX}),tY={kernelName:Cr,backendName:"webgl",kernelFunc:eY},nY=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-06-02 16:39:53 +02:00
`}},sY=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-06-02 16:39:53 +02:00
`}};function fd(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=fd({inputs:s.slice(0,u),backend:n}),c=fd({inputs:s.slice(u),backend:n});return fd({inputs:[l,c],backend:n})}let r=s.map(u=>u.dtype).reduce((u,l)=>cn(u,l)),a=s.map(u=>u.shape),i=K().getBool("WEBGL_PACK")?new sY(s[0].shape,a):new nY(s[0].shape,a);return n.runWebGLProgram(i,s,r)}var rY={kernelName:Sa,backendName:"webgl",kernelFunc:fd};function aY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s,i=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,i),p=r;c!=null&&(p=pn({inputs:{x:r},backend:n,attrs:{perm:c}}),l=C.getInnerMostAxes(l.length,i)),C.assertAxesAreInnerMostDims("all",l,i);let[d,h]=C.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=So(m,m.dtype,"all",n),b;if(o){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 oY={kernelName:cl,backendName:"webgl",kernelFunc:aY};function iY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s,i=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,i),p=r;c!=null&&(p=pn({inputs:{x:r},backend:n,attrs:{perm:c}}),l=C.getInnerMostAxes(l.length,i)),C.assertAxesAreInnerMostDims("any",l,i);let[d,h]=C.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=So(m,m.dtype,"any",n),b;if(o){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 uY={kernelName:dl,backendName:"webgl",kernelFunc:iY},lY=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 o=t==="max"?">":"<",i=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++) {
2022-06-02 16:39:53 +02:00
int inIdx = ${i};
2022-04-21 15:39:40 +02:00
float candidate = getA(batch, inIdx);
2022-06-02 16:39:53 +02:00
if (candidate ${o} bestValue) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},cY=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 o=this.outputShape,i=o.length,u=rt(i),l=ln("coords",i),c,p;if(a===1){p=i+1;let $=rt(p);c=`
2022-04-21 15:39:40 +02:00
${$} sourceLocR = ${$}(${l.join()}, 0);
2022-06-02 16:39:53 +02:00
++${l[i-1]};
2022-04-21 15:39:40 +02:00
${$} sourceLocG = ${$}(${l.join()}, 0);
2022-06-02 16:39:53 +02:00
++${l[i-2]};
2022-04-21 15:39:40 +02:00
${$} sourceLocA = ${$}(${l.join()}, 0);
2022-06-02 16:39:53 +02:00
--${l[i-1]};
2022-04-21 15:39:40 +02:00
${$} sourceLocB = ${$}(${l.join()}, 0);
2022-06-02 16:39:53 +02:00
--${l[i-2]};`}else p=i,c=`
2022-04-21 15:39:40 +02:00
${u} sourceLocR = coords;
2022-06-02 16:39:53 +02:00
++${l[i-1]};
2022-04-21 15:39:40 +02:00
${u} sourceLocG = coords;
2022-06-02 16:39:53 +02:00
++${l[i-2]};
2022-04-21 15:39:40 +02:00
${u} sourceLocA = coords;
2022-06-02 16:39:53 +02:00
--${l[i-1]};
2022-04-21 15:39:40 +02:00
${u} sourceLocB = coords;
2022-06-02 16:39:53 +02:00
--${l[i-2]};`;let d=["x","y","z","w","u","v"].slice(0,p),h="."+d[p-1],f=d.map($=>"int "+$),m=ln("sourceLocR",p-1).concat("inIdx.r"),g=ln("sourceLocG",p-1).concat("inIdx.g"),b=ln("sourceLocB",p-1).concat("inIdx.b"),y=ln("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();
2022-06-02 16:39:53 +02:00
bool hasNextCol = ${l[i-1]} < ${o[i-1]-1};
bool hasNextRow = ${l[i-2]} < ${o[i-2]-1};
2022-04-21 15:39:40 +02:00
${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-06-02 16:39:53 +02:00
`}};function c2(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 o=C.computeOptimalWindowSize(a),i={windowSize:o,inSize:a,batchSize:r,outSize:Math.ceil(a/o)},u=new lY(i,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=c2(e,t,n,c);return e.disposeIntermediateTensorInfo(c),p}function d2(e,t,n,s=null){let r=s!=null?s.shape:t.shape,a=r[r.length-1],o=C.computeOptimalWindowSize(a),i=new cY(r,o,n,s==null),u=s==null?[t]:[t,s],l=e.runWebGLProgram(i,u,"int32");if(l.shape.length===t.shape.length){let c=d2(e,t,n,l);return e.disposeIntermediateTensorInfo(l),c}return l}function p2(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=[],o=e.texData.get(t.dataId),i=o!==null&&o.isPacked,u=t;i&&(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=c2(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 d2(e,t,s)}function dY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,o=w.parseAxisParam(a,r.shape),i=C.getAxesPermutation(o,r.shape.length),u=r,l=[];i!=null&&(u=pn({inputs:{x:r},backend:n,attrs:{perm:i}}),l.push(u),o=C.getInnerMostAxes(o.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMax",[o[0]],u.shape.length);let c=p2(n,u,o[0],"max");return l.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var pY={kernelName:Ia,backendName:"webgl",kernelFunc:dY};function hY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,o=w.parseAxisParam(a,r.shape),i=C.getAxesPermutation(o,r.shape.length),u=r,l=[];i!=null&&(u=pn({inputs:{x:r},backend:n,attrs:{perm:i}}),l.push(u),o=C.getInnerMostAxes(o.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMin",[o[0]],u.shape.length);let c=p2(n,u,o[0],"min");return l.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var fY={kernelName:pl,backendName:"webgl",kernelFunc:hY},mY=ss+`
2022-04-21 15:39:40 +02:00
if (abs(x) > 1.) {
return NAN;
}
return asin(x);
2022-06-02 16:39:53 +02:00
`,gY=Ke({opSnippet:mY}),bY={kernelName:hl,backendName:"webgl",kernelFunc:gY},yY=ss+"return log(x + sqrt(x * x + 1.0));",vY=Ke({opSnippet:yY}),xY={kernelName:fl,backendName:"webgl",kernelFunc:vY},wY=ss+`
2022-04-21 15:39:40 +02:00
return atan(x);
2022-06-02 16:39:53 +02:00
`,kY=Ke({opSnippet:wY}),SY={kernelName:ml,backendName:"webgl",kernelFunc:kY},IY=E8+`
2022-04-21 15:39:40 +02:00
return atan(a, b);
2022-06-02 16:39:53 +02:00
`,CY=`
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-06-02 16:39:53 +02:00
`+R8+`
2022-04-21 15:39:40 +02:00
return result;
2022-06-02 16:39:53 +02:00
`,NY=jt({opSnippet:IY,packedOpSnippet:CY}),TY={kernelName:bl,backendName:"webgl",kernelFunc:NY},$Y=ss+`
2022-04-21 15:39:40 +02:00
if ((x < -1.0) || (x > 1.0)) return NAN;
2022-06-02 16:39:53 +02:00
return (log(1.0 + x) - log(1.0 - x)) / 2.0;`,_Y=Ke({opSnippet:$Y}),AY={kernelName:gl,backendName:"webgl",kernelFunc:_Y},ol=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,o=e.strideHeight,i=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=`
const ivec2 strides = ivec2(${o}, ${i});
2022-04-21 15:39:40 +02:00
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=`
2022-06-02 16:39:53 +02:00
const ivec2 strides = ivec2(${o}, ${i});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},Fv=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,o=e.strideDepth,i=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 =
2022-06-02 16:39:53 +02:00
ivec3(${o}, ${i}, ${u});
2022-04-21 15:39:40 +02:00
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 =
2022-06-02 16:39:53 +02:00
ivec3(${o}, ${i}, ${u});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function EY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t;ou(r,"avgPool");let{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=s,l=1;w.assert(C.eitherStridesOrDilationsAreOne(o,l),()=>`Error in avgPool: Either strides or dilations must be 1. Got strides ${o} and dilations '${l}'`);let c=C.computePool2DInfo(r.shape,a,o,l,i,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Rn({inputs:{x:r},backend:n});let p=new ol(c,"avg",!1);return n.runWebGLProgram(p,[r],"float32")}var RY={kernelName:Ca,backendName:"webgl",kernelFunc:EY};function DY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u,dataFormat:l}=s,c=[1,1,1],p=C.computePool3DInfo(r.shape,a,o,c,i,u,l),d=new Fv(p,"avg",!1);return n.runWebGLProgram(d,[r],"float32")}var FY={kernelName:tp,backendName:"webgl",kernelFunc:DY},OY=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,o=e.dilationWidth,i=e.effectiveFilterHeight,u=e.effectiveFilterWidth,l=i-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;
2022-06-02 16:39:53 +02:00
for (int wR = 0; wR < ${i};
2022-04-21 15:39:40 +02:00
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};
2022-06-02 16:39:53 +02:00
wC+= ${o}) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},PY=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,o=e.strideWidth,i=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};
2022-06-02 16:39:53 +02:00
wD += ${i}) {
2022-04-21 15:39:40 +02:00
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}) {
2022-06-02 16:39:53 +02:00
float dyC = float(dyCCorner + wC) / ${o}.0;
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function zY(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,o=a,{filterSize:i,strides:u,pad:l,dimRoundingMode:c}=s,p=[1,1,1],d=C.computePool3DInfo(o.shape,i,u,p,l,c),h=new PY(d);return n.runWebGLProgram(h,[r],o.dtype)}var LY={kernelName:yg,backendName:"webgl",kernelFunc:zY};function MY(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,o=a;ou([r,a],"avgPoolGrad");let{filterSize:i,strides:u,pad:l}=s,c=C.computePool2DInfo(o.shape,i,u,1,l),p=new OY(c);return n.runWebGLProgram(p,[r],o.dtype)}var BY={kernelName:bg,backendName:"webgl",kernelFunc:MY};function VY(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a}=t,{transposeA:o,transposeB:i}=s;return Hd({a:r,b:a,transposeA:o,transposeB:i,backend:n})}var WY={kernelName:Na,backendName:"webgl",kernelFunc:VY},UY=class{constructor(e,t,n,s,r,a){this.outputShape=[],this.variableNames=["x","mean","variance"],C.assertAndGetBroadcastShape(e,t),C.assertAndGetBroadcastShape(e,n);let o="0.0";s!=null&&(C.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");let i="1.0";r!=null&&(C.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),i="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();
2022-06-02 16:39:53 +02:00
float offset = ${o};
float scale = ${i};
2022-04-21 15:39:40 +02:00
float inv = scale * inversesqrt(variance + float(${a}));
setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));
}
2022-06-02 16:39:53 +02:00
`}},GY=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 o="vec4(0.0)";s!=null&&(C.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");let i="vec4(1.0)";r!=null&&(C.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),i="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
2022-06-02 16:39:53 +02:00
vec4 offset = ${o};
vec4 scale = ${i};
2022-04-21 15:39:40 +02:00
vec4 x = getXAtOutCoords();
vec4 mean = getMeanAtOutCoords();
vec4 variance = getVarianceAtOutCoords();
vec4 inv = scale * inversesqrt(variance + vec4(${a}));
setOutput((x - mean) * inv + offset);
}
2022-06-02 16:39:53 +02:00
`}},HY=({inputs:e,backend:t,attrs:n})=>{let{x:s,mean:r,variance:a,offset:o,scale:i}=e;w.assert(r.shape.length===a.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),w.assert(o==null||r.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),w.assert(i==null||r.shape.length===i.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;o!=null&&(c=o.shape,l.push(o));let p=null;i!=null&&(p=i.shape,l.push(i));let d=K().getBool("WEBGL_PACK_NORMALIZATION")?new GY(s.shape,r.shape,a.shape,c,p,u):new UY(s.shape,r.shape,a.shape,c,p,u);return t.runWebGLProgram(d,l,l[0].dtype)},qY={kernelName:Ba,backendName:"webgl",kernelFunc:HY},jY=class{constructor(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;let t=rt(this.rank);this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let n=KY(this.rank),s,r=e.map((a,o)=>`sourceLoc.${ng[o]} = start[${o}] + coords.${ng[o]};`);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-06-02 16:39:53 +02:00
`}},ng=["x","y","z","w","u","v"];function KY(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`)}var XY=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=rt(this.rank),n=ln("coords",this.rank),s=ln("sourceLoc",this.rank),r=this.rank===1?"sourceLoc":`vec2(${s.slice(-2).join()})`,a=`getChannel(getSource(${s.join()}), ${r})`,o=`
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]};
}
2022-06-02 16:39:53 +02:00
`,i=this.rank===1?"":`
2022-04-21 15:39:40 +02:00
--${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.);
${o}
2022-06-02 16:39:53 +02:00
${i}
2022-04-21 15:39:40 +02:00
setOutput(result);
}
2022-06-02 16:39:53 +02:00
`}};function YY(e,t,n,s){let r=s.texData.get(e.dataId),a=s.makeTensorInfo(n,e.dtype),o=s.texData.get(a.dataId);Object.assign(o,r),o.refCount=1,o.shape=n,o.dtype=e.dtype;let i=kt.computeFlatOffset(t,w.computeStrides(e.shape));r.slice&&(i+=r.slice.flatOffset),o.slice={flatOffset:i,origDataId:r.slice&&r.slice.origDataId||e.dataId};let u=s.dataRefCount.get(o.slice.origDataId)||1;return s.dataRefCount.set(o.slice.origDataId,u+1),a}function pu(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,size:o}=s,[i,u]=kt.parseSliceParams(r,a,o);if(kt.assertParamsValid(r,i,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=BX(p.values,i,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,i,u);if(l||!c){let p=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new XY(u):new jY(u),d=[i];return n.runWebGLProgram(p,[r],r.dtype,d)}return n.uploadToGPU(r.dataId),YY(r,i,u,n)}var QY={kernelName:Bi,backendName:"webgl",kernelFunc:pu},ZY=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,crops:o}=s;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let i=a.reduce((y,v)=>y*v),u=C.getReshaped(r.shape,a,i),l=C.getPermuted(u.length,a.length),c=C.getReshapedPermuted(r.shape,a,i),p=C.getSliceBeginCoords(o,a.length),d=C.getSliceSize(c,o,a.length),h=[],f=pe({inputs:{x:r},backend:n,attrs:{shape:u}}),m=pn({inputs:{x:f},backend:n,attrs:{perm:l}}),g=pe({inputs:{x:m},backend:n,attrs:{shape:c}}),b=pu({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},JY={kernelName:pi,backendName:"webgl",kernelFunc:ZY};function e9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,weights:a}=t,{size:o}=s,i=n.readSync(r.dataId),u=n.readSync(a.dataId),l=Y1(i,u,a.dtype,a.shape,o);return n.makeTensorInfo([o],a.dtype,l)}var t9={kernelName:vg,backendName:"webgl",kernelFunc:e9};function n9(e){let{inputs:t,backend:n}=e,{s0:s,s1:r}=t,a=n.readSync(s.dataId),o=n.readSync(r.dataId),i=C.assertAndGetBroadcastShape(Array.from(a),Array.from(o));return n.makeTensorInfo([i.length],"int32",Int32Array.from(i))}var s9={kernelName:xg,backendName:"webgl",kernelFunc:n9},r9="return float(a != b);",h2=jt({opSnippet:r9,cpuKernelImpl:FX,dtype:"bool"}),a9={kernelName:_i,backendName:"webgl",kernelFunc:h2};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 o9={kernelName:cp,backendName:"webgl",kernelFunc:nc},i9="return float(int(x));";function u9(e,t){let n=new Gs(e.shape,i9),s=t.runWebGLProgram(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 Rn({inputs:{x:r},backend:n});let o=$t(r.shape),i=sg({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),u=Fr({inputs:{real:i,imag:o},backend:n});return o.dispose(),n.disposeIntermediateTensorInfo(i),u}if(r.dtype==="complex64"){let o=nc({inputs:{input:r},backend:n}),i=sg({inputs:{x:o},backend:n,attrs:{dtype:a}});return n.disposeIntermediateTensorInfo(o),i}if(!w.hasEncodingLoss(r.dtype,a)){let o=Rn({inputs:{x:r},backend:n});return{dataId:o.dataId,shape:o.shape,dtype:a}}if(a==="int32")return u9(r,n);if(a==="bool"){let o=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=h2({inputs:{a:r,b:o},backend:n});return n.disposeIntermediateTensorInfo(o),u}throw new Error(`Error in Cast: failed to cast ${r.dtype} to ${a}`)}var l9={kernelName:Ta,backendName:"webgl",kernelFunc:sg},Cw="return ceil(x);",c9=Ke({opSnippet:Cw,packedOpSnippet:Cw,cpuKernelImpl:mX}),d9={kernelName:$a,backendName:"webgl",kernelFunc:c9},p9=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-06-02 16:39:53 +02:00
`}},h9=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-06-02 16:39:53 +02:00
`}};function f9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{clipValueMin:a,clipValueMax:o}=s,i;K().getBool("WEBGL_PACK_CLIP")?i=new h9(r.shape):i=new p9(r.shape);let u=[[a],[o]];return n.runWebGLProgram(i,[r],r.dtype,u)}var m9={kernelName:Nr,backendName:"webgl",kernelFunc:f9},g9=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-06-02 16:39:53 +02:00
`}};function Nw(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function b9(e){let{inputs:t,backend:n}=e,{x:s}=t,r=n.texData.get(s.dataId),a=new g9(s.shape),o=[Nw(s,r.complexTensorInfos.real),Nw(s,r.complexTensorInfos.imag)];return n.runWebGLProgram(a,o,o[0].dtype)}var y9={kernelName:sp,backendName:"webgl",kernelFunc:b9},v9=class{constructor(e){this.outputShape=[],this.outputShape=C.computeOutShape(e,1),this.variableNames=e.map((a,o)=>`T${o}`);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 o=t[a-1];n.push(`else if (yC < ${t[a]}) setOutput(getT${a}(yR, yC-${o}));`)}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-06-02 16:39:53 +02:00
`}},x9=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=rt(s),a=ln("coords",s),o=["x","y","z","w","u","v"].slice(0,s);this.variableNames=e.map((f,m)=>`T${m}`);let i=new Array(e.length-1);i[0]=e[0][t];for(let f=1;f<i.length;f++)i[f]=i[f-1]+e[f][t];let u=o[t],l=o.slice(-2),c=o.join(),p=`if (${u} < ${i[0]}) {
2022-04-21 15:39:40 +02:00
return getChannel(
getT0(${c}), vec2(${l.join()}));
2022-06-02 16:39:53 +02:00
}`;for(let f=1;f<i.length;f++){let m=i[f-1];p+=`
if (${u} < ${i[f]} && ${u} >= ${i[f-1]}) {
2022-04-21 15:39:40 +02:00
return getChannel(
2022-06-02 16:39:53 +02:00
getT${f}(${rd(o,u,m)}),
vec2(${rd(l,u,m)}));
}`}let d=i.length,h=i[i.length-1];p+=`
2022-04-21 15:39:40 +02:00
return getChannel(
2022-06-02 16:39:53 +02:00
getT${d}(${rd(o,u,h)}),
vec2(${rd(l,u,h)}));`,this.userCode=`
float getValue(${o.map(f=>"int "+f)}) {
2022-04-21 15:39:40 +02:00
${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-06-02 16:39:53 +02:00
`}};function rd(e,t,n){let s=e.indexOf(t);return e.map((a,o)=>o===s?`${a} - ${n}`:a).join()}function oh(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 w9={kernelName:ip,backendName:"webgl",kernelFunc:oh};function jo(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=>oh({inputs:{input:m},backend:n})),d=jo(c,t,n),h=jo(p,t,n),f=Fr({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=gX(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=jo(e.slice(0,c),t,n),d=jo(e.slice(c),t,n),h=jo([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 x9(e.map(p=>p.shape),t);return n.runWebGLProgram(c,e,s)}let{tensors2D:a,outShape:o}=k9(e,t,n),i=new v9(a.map(c=>c.shape)),u=n.runWebGLProgram(i,a,s);a.forEach(c=>n.disposeIntermediateTensorInfo(c));let l=pe({inputs:{x:u},attrs:{shape:o},backend:n});return n.disposeIntermediateTensorInfo(u),l}function k9(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 f2(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s,a=w.parseAxisParam(r,t[0].shape)[0],o=C.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(o)===0)return n.makeTensorInfo(o,t[0].dtype,[]);let i=t.filter(l=>w.sizeFromShape(l.shape)>0);if(i.length===1)return Rn({inputs:{x:i[0]},backend:n});let u=i.map(l=>l.shape);return C.assertParamsConsistent(u,a),jo(i,a,n)}var S9={kernelName:hi,backendName:"webgl",kernelFunc:f2},m2=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let a=e.padInfo.top,o=e.padInfo.left,i=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}
2022-06-02 16:39:53 +02:00
const ivec2 strides = ivec2(${i}, ${u});
const ivec2 pads = ivec2(${a}, ${o});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},I9=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,o=e.strideWidth,i=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=`
const ivec3 strides = ivec3(${r}, ${a}, ${o});
2022-04-21 15:39:40 +02:00
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++) {
2022-06-02 16:39:53 +02:00
int xF = xFCorner + wF * ${i};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},C9=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=mn(),r=n==="channelsLast",a=r?1:2,o=r?2:3,i=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+=`
2022-05-30 03:12:18 +02:00
blockIndex = rc.z + ${c};
pos = rc.y + ${l};
2022-04-21 15:39:40 +02:00
2022-06-02 16:39:53 +02:00
${i}
2022-04-21 15:39:40 +02:00
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);
2022-06-02 16:39:53 +02:00
if(d1 < inputShape[${o}] && d1 >= 0) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function qd(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 g2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=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=qd(a.shape,h);x!=null&&(a=pe({inputs:{x:a},backend:s,attrs:{shape:x}}),b.push(a))}if(r!=null){let x=qd(r.shape,h);x!=null&&(r=pe({inputs:{x:r},backend:s,attrs:{shape:x}}),b.push(r))}if(!((p===1||d===1)&&c>l2)&&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(al(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=Hd({a:k,b:$,backend:s,transposeA:f,transposeB:m,bias:r,activation:i,preluActivationWeights:a,leakyreluAlpha:o}),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]}}),$=Hd({a:h?k:I,b:h?I:k,transposeA:!h,transposeB:m,backend:s,bias:r,activation:i,preluActivationWeights:a,leakyreluAlpha:o});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 b2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=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=qd(a.shape,f);X!=null&&(a=pe({inputs:{x:a},backend:s,attrs:{shape:X}}),x.push(a))}if(r!=null){let X=qd(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 C9(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,D=i==="leakyrelu",T=i?sh(i,!0):null,L=new u2(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,D),W=f?[E,k]:[k,E];if(r&&W.push(r),A&&W.push(a),D){let X=s.makeTensorInfo([],"float32",w.createScalarValue(o,"float32"));W.push(X),x.push(X)}let j=s.runWebGLProgram(L,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 N9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:o,pad:i,dataFormat:u,dilations:l,dimRoundingMode:c}=s,p=C.convertConv2DDataFormat(u),d=C.computeConv2DInfo(r.shape,a.shape,o,l,i,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=g2({x:r,filter:a,convInfo:d,backend:n});else if(K().getBool("WEBGL_CONV_IM2COL"))h=b2({x:r,filter:a,convInfo:d,backend:n});else{let m=new m2(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 T9={kernelName:_a,backendName:"webgl",kernelFunc:N9},$9=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-06-02 16:39:53 +02:00
`}},_9=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",o=t-1-e.padInfo.top,i=n-1-e.padInfo.left,u=a?1:2,l=a?2:3,c=a?3:1;this.userCode=`
const ivec2 pads = ivec2(${o}, ${i});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},A9=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,o=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++) {
2022-06-02 16:39:53 +02:00
int xC = wC + yC * ${s} - ${o};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},E9=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,o=e.strideWidth,i=t-1-e.padInfo.front,u=n-1-e.padInfo.top,l=s-1-e.padInfo.left;this.userCode=`
const ivec3 pads = ivec3(${i}, ${u}, ${l});
2022-04-21 15:39:40 +02:00
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++) {
2022-06-02 16:39:53 +02:00
float dyC = float(dyCCorner + wC) / ${o}.0;
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function R9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:o,pad:i,dataFormat:u,dimRoundingMode:l,filterShape:c}=s,p=C.convertConv2DDataFormat(u),d=C.computeConv2DInfo(r.shape,c,o,1,i,l,!1,p),h=new $9(d);return n.runWebGLProgram(h,[r,a],"float32")}var D9={kernelName:wg,backendName:"webgl",kernelFunc:R9};function F9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{inputShape:o,strides:i,pad:u,dataFormat:l,dimRoundingMode:c}=s,p=C.convertConv2DDataFormat(l),d=C.computeConv2DInfo(o,a.shape,i,1,u,c,!1,p),h=new _9(d);return n.runWebGLProgram(h,[r,a],"float32")}var O9={kernelName:Aa,backendName:"webgl",kernelFunc:F9};function P9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:o,pad:i,dilations:u}=s,l=C.computeConv3DInfo(r.shape,a.shape,o,u,i),c=new I9(l);return n.runWebGLProgram(c,[r,a],"float32")}var z9={kernelName:rp,backendName:"webgl",kernelFunc:P9};function L9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:o,pad:i,filterShape:u}=s,l=C.computeConv3DInfo(r.shape,u,o,1,i),c=new A9(l);return n.runWebGLProgram(c,[r,a],"float32")}var M9={kernelName:kg,backendName:"webgl",kernelFunc:L9};function B9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{pad:o,strides:i,inputShape:u}=s,l=C.computeConv3DInfo(u,a.shape,i,1,o),c=new E9(l);return n.runWebGLProgram(c,[r,a],"float32")}var V9={kernelName:Sg,backendName:"webgl",kernelFunc:B9},W9=du+`
2022-04-21 15:39:40 +02:00
return cos(x);
2022-06-02 16:39:53 +02:00
`,U9=Ke({opSnippet:W9}),G9={kernelName:Ea,backendName:"webgl",kernelFunc:U9},H9=`
2022-04-21 15:39:40 +02:00
float e2x = exp(-x);
return (e2x + 1.0 / e2x) / 2.0;
2022-06-02 16:39:53 +02:00
`,q9=Ke({opSnippet:H9}),j9={kernelName:Ra,backendName:"webgl",kernelFunc:q9},K9=class{constructor(e,t,n,s,r){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[a,o,i,u]=e,[l]=t,[c,p]=n;this.outputShape=[l,c,p,u];let d=s==="bilinear"?1:0,[h,f]=[`${o-1}.0`,`${i-1}.0`],[m,g,b]=c>1?[`${(o-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?[`${(i-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-06-02 16:39:53 +02:00
`}},X9=e=>{let{inputs:t,backend:n,attrs:s}=e,{image:r,boxes:a,boxInd:o}=t,{cropSize:i,method:u,extrapolationValue:l}=s,c=new K9(r.shape,a.shape,i,u,l);return n.runWebGLProgram(c,[r,a,o],"float32")},Y9={kernelName:mi,backendName:"webgl",kernelFunc:X9},Tw=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",o=n?a:`getX(${$w(r,"coords",this.op)})`,i=this.outputShape[this.outputShape.length-1],u="",l="";n?(u=s?`end != ${i-1}`:"end != 0",l=s?"end + 1":"end - 1"):(u=s?`end + pow2 < ${i}`:"end >= pow2",l=s?"end + pow2":"end - pow2"),this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
2022-06-02 16:39:53 +02:00
${rt(r)} coords = getOutputCoords();
int end = ${_w(r,"coords",this.op)};
float val = ${o};
2022-04-21 15:39:40 +02:00
int pow2 = int(pow(2.0, index));
if (${u}) {
int idx = ${l};
2022-06-02 16:39:53 +02:00
${_w(r,"coords",this.op)} = idx;
val ${this.op}= getX(${$w(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-06-02 16:39:53 +02:00
`}};function $w(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 _w(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 y2(e,t,n,s,r,a){let o=t.shape.length,i=C.getAxesPermutation([s],o),u=t;i!=null&&(u=pn({inputs:{x:t},backend:n,attrs:{perm:i}}));let l=C.getInnerMostAxes(1,o)[0];if(l!==o-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 Tw(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 Tw(e,u.shape,r,a),h=p;p=n.runWebGLProgram(d,[p],p.dtype),n.disposeIntermediateTensorInfo(h)}if(i!=null){let d=C.getUndoAxesPermutation(i),h=pn({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:o,reverse:i}=s;return y2("*",r,n,a,o,i)}var Z9={kernelName:fi,backendName:"webgl",kernelFunc:Q9};function J9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:o,reverse:i}=s;return y2("+",r,n,a,o,i)}var eQ={kernelName:Da,backendName:"webgl",kernelFunc:J9};function tQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,weights:a}=t,{size:o,binaryOutput:i}=s;if(r.shape.length===1){let u=n.readSync(r.dataId),l=n.readSync(a.dataId),c=Y1(u,l,a.dtype,a.shape,o);return n.makeTensorInfo([o],a.dtype,c)}else if(r.shape.length===2){let u=n.bufferSync(r),l=n.bufferSync(a),c=fX(u,l,o,i);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 nQ={kernelName:Ig,backendName:"webgl",kernelFunc:tQ},sQ=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-06-02 16:39:53 +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 rQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockSize:a,dataFormat:o}=s,i=r.shape[0],u=o==="NHWC"?r.shape[1]:r.shape[2],l=o==="NHWC"?r.shape[2]:r.shape[3],c=o==="NHWC"?r.shape[3]:r.shape[1],p=u*a,d=l*a,h=c/(a*a),f=o==="NHWC"?[i,p,d,h]:[i,h,p,d],m=new sQ(f,a,o);return n.runWebGLProgram(m,[r],r.dtype)}var aQ={kernelName:gi,backendName:"webgl",kernelFunc:rQ},v2=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,o=e.filterWidth,i=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;
2022-06-02 16:39:53 +02:00
int d1 = d2 / ${i};
int q = d2 - d1 * ${i};
2022-04-21 15:39:40 +02:00
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;
}
2022-06-02 16:39:53 +02:00
for (int wC = 0; wC < ${o}; wC++) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},x2=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,o=e.padInfo.left,i=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};
2022-06-02 16:39:53 +02:00
`,i===1){if(b<c&&(o%2===1?(d+=`
2022-04-21 15:39:40 +02:00
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};
2022-06-02 16:39:53 +02:00
`,b+1<c)){let y=o%2===0?w.nearestLargerEven(u):u;u%2===0&&o%2===1||u%2!==0&&o%2!==1?(d+=`
2022-04-21 15:39:40 +02:00
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};
2022-06-02 16:39:53 +02:00
`}}else b<c&&(o%2===1?(d+=`
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function oQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:o,pad:i,dilations:u,dimRoundingMode:l}=s,c=u;c==null&&(c=[1,1]),w.assert(C.eitherStridesOrDilationsAreOne(o,c),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${o} and dilations '${c}'`);let p=C.computeConv2DInfo(r.shape,a.shape,o,c,i,l,!0),d;K().getBool("WEBGL_PACK_DEPTHWISECONV")&&p.strideWidth<=2&&p.outChannels/p.inChannels===1?d=new x2(p):d=new v2(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 iQ={kernelName:Fa,backendName:"webgl",kernelFunc:oQ},uQ=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-06-02 16:39:53 +02:00
`}},lQ=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,o=n-1-e.padInfo.left,i=e.outChannels/e.inChannels;this.userCode=`
const ivec2 pads = ivec2(${a}, ${o});
2022-04-21 15:39:40 +02:00
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
2022-06-02 16:39:53 +02:00
for (int dm = 0; dm < ${i}; dm++) {
int d2 = d1 * ${i} + dm;
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function cQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:o,dilations:i,pad:u,dimRoundingMode:l,filterShape:c}=s,p=C.computeConv2DInfo(r.shape,c,o,i,u,l,!0),d=new uQ(p);return n.runWebGLProgram(d,[r,a],"float32")}var dQ={kernelName:Cg,backendName:"webgl",kernelFunc:cQ};function pQ(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{strides:o,dilations:i,pad:u,dimRoundingMode:l,inputShape:c}=s,p=C.computeConv2DInfo(c,a.shape,o,i,u,l,!0),d=new lQ(p);return n.runWebGLProgram(d,[r,a],"float32")}var hQ={kernelName:Ng,backendName:"webgl",kernelFunc:pQ},fQ=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-06-02 16:39:53 +02:00
`}};function mQ(e){let{inputs:t,backend:n}=e,{x:s}=t,r=[...s.shape,...s.shape],a=w.sizeFromShape(s.shape),o=pe({inputs:{x:s},backend:n,attrs:{shape:[a]}}),i=new fQ(a),u=n.runWebGLProgram(i,[o],o.dtype),l=pe({inputs:{x:u},backend:n,attrs:{shape:r}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(u),l}var gQ={kernelName:Tg,backendName:"webgl",kernelFunc:mQ},bQ=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:s,strideHeight:r,strideWidth:a,filterHeight:o,filterWidth:i,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;
2022-06-02 16:39:53 +02:00
for (int h = 0; h < ${o}; h++) {
2022-04-21 15:39:40 +02:00
int hIn = hBeg + h * ${u};
if (hIn >= 0 && hIn < ${t}) {
2022-06-02 16:39:53 +02:00
for (int w = 0; w < ${i}; w++) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function yQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:o,pad:i,dilations:u}=s,l=C.computeDilation2DInfo(r.shape,a.shape,o,i,"NHWC",u),c,p=new bQ(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 vQ={kernelName:ap,backendName:"webgl",kernelFunc:yQ};function xQ(e){let{inputs:t,backend:n,attrs:s}=e,{equation:r}=s,a=t,{allDims:o,summedDims:i,idDims:u}=C.decodeEinsumEquation(r,a.length);C.checkEinsumDimSizes(o.length,u,a);let{path:l,steps:c}=C.getEinsumComputePath(i,u),p=c.length,d=null,h=o.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=pn({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=Dv({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]-(o.length-h),keepDims:!1}}),f.push(d)),h--)}for(let m of f)m!==d&&n.disposeIntermediateTensorInfo(m);return d}var wQ={kernelName:op,backendName:"webgl",kernelFunc:xQ},kQ="return (x >= 0.0) ? x : (exp(x) - 1.0);",SQ=`
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-06-02 16:39:53 +02:00
`,IQ=Ke({opSnippet:kQ,packedOpSnippet:SQ}),CQ={kernelName:Pa,backendName:"webgl",kernelFunc:IQ},NQ="return (b >= 1.0) ? a : a * (b + 1.0);",TQ=`
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-06-02 16:39:53 +02:00
`,$Q=e=>{let{inputs:t,backend:n}=e,{dy:s,y:r}=t,a=K().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new tc(TQ,s.shape,r.shape):new li(NQ,s.shape,r.shape);return n.runWebGLProgram(a,[s,r],s.dtype)},_Q={kernelName:$g,backendName:"webgl",kernelFunc:$Q},AQ=`
2022-04-21 15:39:40 +02:00
return vec4(equal(a, b));
2022-06-02 16:39:53 +02:00
`,EQ="return float(a == b);",RQ=jt({opSnippet:EQ,packedOpSnippet:AQ,dtype:"bool",cpuKernelImpl:bX}),DQ={kernelName:bi,backendName:"webgl",kernelFunc:RQ},FQ=`
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-06-02 16:39:53 +02:00
`,OQ=Ke({opSnippet:FQ}),PQ={kernelName:yl,backendName:"webgl",kernelFunc:OQ},zQ=du+`
2022-04-21 15:39:40 +02:00
return exp(x);
2022-06-02 16:39:53 +02:00
`,LQ=`
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-06-02 16:39:53 +02:00
`,w2=Ke({opSnippet:zQ,packedOpSnippet:LQ,cpuKernelImpl:yX,dtype:"float32"}),MQ={kernelName:za,backendName:"webgl",kernelFunc:w2};function rg(e){let{inputs:t,attrs:n,backend:s}=e,{dim:r}=n,{input:a}=t,o=a.shape.length,i=a.shape.slice(),u=r;return r<0&&(w.assert(-(o+1)<=r,()=>`Axis must be in the interval [${-(o+1)}, ${o}]`),u=o+r+1),i.splice(u,0,1),pe({inputs:{x:a},backend:s,attrs:{shape:i}})}var BQ={kernelName:yi,backendName:"webgl",kernelFunc:rg},Aw="return exp(x) - 1.0;",VQ=Ke({opSnippet:Aw,packedOpSnippet:Aw,cpuKernelImpl:vX}),WQ={kernelName:vi,backendName:"webgl",kernelFunc:VQ},Ew=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",o;if(e==="real")o="return real * expR - imag * expI;";else if(e==="imag")o="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) {
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}
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-06-02 16:39:53 +02:00
`}};function k2(e,t,n){let s=n.texData.get(e.dataId),r=w.sizeFromShape(e.shape),a=e.shape[e.shape.length-1],o=r/a,i=pe({inputs:{x:e},backend:n,attrs:{shape:[o,a]}}),u=i.shape,l=new Ew("real",u,t),c=new Ew("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=Fr({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(i),n.disposeIntermediateTensorInfo(f),m}function UQ(e){let{inputs:t,backend:n}=e,{input:s}=t;return k2(s,!1,n)}var GQ={kernelName:_g,backendName:"webgl",kernelFunc:UQ},HQ=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-06-02 16:39:53 +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 o=w.getArrayFromDType(a,w.sizeFromShape(s));return o.fill(r),t.makeTensorInfo(s,a,o)}else{let o=new HQ(s,r),i=[[r]];return t.runWebGLProgram(o,[],a,i)}}var qQ={kernelName:vl,backendName:"webgl",kernelFunc:sc},jQ=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-06-02 16:39:53 +02:00
`}},KQ={kernelName:xi,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,s=t,r=new jQ(n.shape);return s.runWebGLProgram(r,[n],n.dtype)}},Rw="return floor(x);",XQ=Ke({opSnippet:Rw,packedOpSnippet:Rw,cpuKernelImpl:xX}),YQ={kernelName:La,backendName:"webgl",kernelFunc:XQ},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-06-02 16:39:53 +02:00
`,ZQ=`
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-06-02 16:39:53 +02:00
`,JQ=jt({opSnippet:QQ,packedOpSnippet:ZQ,dtype:"int32"}),eZ={kernelName:Ma,backendName:"webgl",kernelFunc:JQ},tZ=class{constructor(e){this.variableNames=["A"];let t=mn(),[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-06-02 16:39:53 +02:00
`}},nZ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=mn(),[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-06-02 16:39:53 +02:00
`}},sZ={kernelName:xd,backendName:"webgl",kernelFunc:rZ},Wo;function rZ(e){let{inputs:t,backend:n,attrs:s}=e,{pixels:r}=t,{numChannels:a}=s,o=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,i=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,[u,l]=o?[r.videoWidth,r.videoHeight]:[r.width,r.height],c=[l,u],p=[l,u,a];(i||o)&&(Wo==null&&(Wo=document.createElement("canvas").getContext("2d")),Wo.canvas.width=u,Wo.canvas.height=l,Wo.drawImage(r,0,0,u,l),r=Wo.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 nZ(p):new tZ(p),f=n.runWebGLProgram(h,[d],"int32");return n.disposeData(d.dataId),f}function aZ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:o,preluActivationWeights:i}=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=g2({x:r,filter:a,convInfo:g,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});else if(K().getBool("WEBGL_CONV_IM2COL"))b=b2({x:r,filter:a,convInfo:g,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});else{let x=o!=null,k=i!=null,I=h==="leakyrelu",$=h?sh(h,!1):null,R=new m2(g,x,$,k,I),E=[r,a],P=(A,D)=>{if(D==="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(o,c)),k&&E.push(P(i,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 oZ={kernelName:ia,backendName:"webgl",kernelFunc:aZ};function iZ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:o,preluActivationWeights:i}=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=o!=null,k=i!=null,I=d==="leakyrelu";if(x&&v.push(o),k&&v.push(i),I){let P=n.makeTensorInfo([],"float32",w.createScalarValue(h,"float32"));v.push(P),f.push(P)}let $;b?$=new x2(g,x,y,k,I):$=new v2(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 uZ={kernelName:ua,backendName:"webgl",kernelFunc:iZ},lZ=class{constructor(e,t,n,s){this.sliceDim=e,this.strides=t,this.paramsShape=s,this.variableNames=["x","indices"],this.outputShape=n;let r=rt(t.length),a=rt(n.length),o=this.sliceDim>1?"strides[j]":"strides",i=rt(s.length),u=s.length>1?"paramsShape[j]":"paramsShape";this.userCode=`
${r} strides = ${r}(${this.strides});
${i} paramsShape = ${i}(${this.paramsShape});
2022-04-21 15:39:40 +02:00
void main() {
2022-06-02 16:39:53 +02:00
${a} coords = getOutputCoords();
2022-04-21 15:39:40 +02:00
int flattenIndex = 0;
2022-06-02 16:39:53 +02:00
bool out_of_bounds = false;
2022-04-21 15:39:40 +02:00
for (int j = 0; j < ${this.sliceDim}; j++) {
int index = round(getIndices(coords[0], j));
2022-06-02 16:39:53 +02:00
out_of_bounds = out_of_bounds || index < 0;
out_of_bounds = out_of_bounds || index >= ${u};
flattenIndex += index * ${o};
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
setOutput(out_of_bounds ? 0.0 : getX(flattenIndex, coords[1]));
2022-04-18 18:26:05 +02:00
}
2022-06-02 16:39:53 +02:00
`}};function cZ(e){let{inputs:t,backend:n}=e,{params:s,indices:r}=t,a=r.shape,o=a[a.length-1],i=w.sizeFromShape(s.shape),[u,l,c,p]=C.prepareAndValidate(s,r),d=pe({inputs:{x:r},backend:n,attrs:{shape:[l,o]}}),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=wX(b,y,s.dtype,l,o,c,p,s.shape,i);return n.makeTensorInfo(u,s.dtype,v.values)}let f=new lZ(o,p,[l,c],s.shape),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 dZ={kernelName:ki,backendName:"webgl",kernelFunc:cZ},pZ=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=rt(this.rank),s=hZ(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-06-02 16:39:53 +02:00
`}};function hZ(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 S2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,indices:a}=t,{axis:o,batchDims:i}=s,u=w.parseAxisParam(o,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,i),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=kX(v,y,f);return p.forEach(k=>n.disposeIntermediateTensorInfo(k)),n.makeTensorInfo(l.outputShape,x.dtype,x.values)}let m=new pZ(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 fZ={kernelName:wi,backendName:"webgl",kernelFunc:S2},mZ="return float(a > b);",gZ=`
2022-04-21 15:39:40 +02:00
return vec4(greaterThan(a, b));
2022-06-02 16:39:53 +02:00
`,bZ=jt({opSnippet:mZ,packedOpSnippet:gZ,cpuKernelImpl:SX,dtype:"bool"}),yZ={kernelName:Si,backendName:"webgl",kernelFunc:bZ},vZ="return float(a >= b);",xZ=`
2022-04-21 15:39:40 +02:00
return vec4(greaterThanEqual(a, b));
2022-06-02 16:39:53 +02:00
`,wZ=jt({opSnippet:vZ,packedOpSnippet:xZ,dtype:"bool",cpuKernelImpl:IX}),kZ={kernelName:Va,backendName:"webgl",kernelFunc:wZ};function SZ(e){let{inputs:t,backend:n}=e,{input:s}=t;return k2(s,!0,n)}var IZ={kernelName:Ag,backendName:"webgl",kernelFunc:SZ},CZ="return float(!isnan(x) && !isinf(x));",NZ=Ke({opSnippet:CZ,dtype:"bool"}),TZ={kernelName:xl,backendName:"webgl",kernelFunc:NZ},$Z="return float(isinf(x));",_Z=Ke({opSnippet:$Z,dtype:"bool"}),AZ={kernelName:wl,backendName:"webgl",kernelFunc:_Z},EZ="return float(isnan(x));",RZ=Ke({opSnippet:EZ,dtype:"bool"}),DZ={kernelName:kl,backendName:"webgl",kernelFunc:RZ},FZ="return float(a < b);",OZ=`
2022-04-21 15:39:40 +02:00
return vec4(lessThan(a, b));
2022-06-02 16:39:53 +02:00
`,PZ=jt({opSnippet:FZ,packedOpSnippet:OZ,cpuKernelImpl:CX,dtype:"bool"}),zZ={kernelName:Ii,backendName:"webgl",kernelFunc:PZ},LZ="return float(a <= b);",MZ=`
2022-04-21 15:39:40 +02:00
return vec4(lessThanEqual(a, b));
2022-06-02 16:39:53 +02:00
`,BZ=jt({opSnippet:LZ,packedOpSnippet:MZ,cpuKernelImpl:NX,dtype:"bool"}),VZ={kernelName:Ci,backendName:"webgl",kernelFunc:BZ};function WZ(e){let{backend:t,attrs:n}=e,{start:s,stop:r,num:a}=n,o=TX(s,r,a);return t.makeTensorInfo([o.length],"float32",o)}var UZ={kernelName:Eg,backendName:"webgl",kernelFunc:WZ},GZ=du+`
2022-04-21 15:39:40 +02:00
return x < 0.0 ? 0./0. : log(x);
2022-06-02 16:39:53 +02:00
`,HZ=`
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-06-02 16:39:53 +02:00
`,qZ=Ke({opSnippet:GZ,packedOpSnippet:HZ,cpuKernelImpl:$X}),jZ={kernelName:Ga,backendName:"webgl",kernelFunc:qZ},KZ=du+`
2022-04-21 15:39:40 +02:00
return log(1.0 + x);
2022-06-02 16:39:53 +02:00
`,XZ=Ke({opSnippet:KZ}),YZ={kernelName:Sl,backendName:"webgl",kernelFunc:XZ},QZ="return float(a >= 1.0 && b >= 1.0);",ZZ=`
2022-04-21 15:39:40 +02:00
return vec4(
vec4(greaterThanEqual(a, vec4(1.0))) *
vec4(greaterThanEqual(b, vec4(1.0))));
2022-06-02 16:39:53 +02:00
`,JZ=jt({opSnippet:QZ,packedOpSnippet:ZZ,dtype:"bool"}),e7={kernelName:Ni,backendName:"webgl",kernelFunc:JZ},t7="return float(!(x >= 1.0));",n7=Ke({opSnippet:t7}),s7={kernelName:Ti,backendName:"webgl",kernelFunc:n7},r7="return float(a >= 1.0 || b >= 1.0);",a7=`
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-06-02 16:39:53 +02:00
`,o7=jt({opSnippet:r7,packedOpSnippet:a7,dtype:"bool"}),i7={kernelName:Il,backendName:"webgl",kernelFunc:o7},u7=class{constructor(e,t,n,s,r){this.variableNames=["x"],this.outputShape=[];let a=t,o=e[3]-1;this.outputShape=e;let i,u=`float(${n}) + float(${s}) * sum`;r===.5?i=`inversesqrt(${u})`:r===1?i=`1.0/(${u})`:i=`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;
2022-06-02 16:39:53 +02:00
if (idx >= 0 && idx <= ${o}) {
2022-04-21 15:39:40 +02:00
float z = getX(b, r, c, idx);
sum += z * z;
}
}
2022-06-02 16:39:53 +02:00
float val = x * ${i};
2022-04-21 15:39:40 +02:00
setOutput(val);
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}},l7=class{constructor(e,t,n,s,r){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let a=t,o=e[3]-1;this.outputShape=e;let i,u=`float(${n}) + float(${s}) * sum`;r===.5?i=`inversesqrt(${u})`:r===1?i=`1.0/(${u})`:i=`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));
2022-06-02 16:39:53 +02:00
bvec2 belowUpperBound = lessThanEqual(idx, ivec2(${o}));
2022-04-21 15:39:40 +02:00
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;
}
}
2022-06-02 16:39:53 +02:00
vec4 result = xAtOutputCoords * ${i};
2022-04-21 15:39:40 +02:00
setOutput(result);
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}},c7=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{depthRadius:a,bias:o,alpha:i,beta:u}=s,l=K().getBool("WEBGL_PACK_NORMALIZATION")?new l7(r.shape,a,o,i,u):new u7(r.shape,a,o,i,u);return n.runWebGLProgram(l,[r],r.dtype)},d7={kernelName:up,backendName:"webgl",kernelFunc:c7},p7=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-06-02 16:39:53 +02:00
`}},h7=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r,y:a,dy:o}=t,{depthRadius:i,bias:u,alpha:l,beta:c}=s,p=new p7(r.shape,i,u,l,c);return n.runWebGLProgram(p,[r,a,o],r.dtype)},f7={kernelName:Rg,backendName:"webgl",kernelFunc:h7};function m7(e,t,n,s){let r=w.sizeFromShape(t),o=w.sizeFromShape(e.shape)/r,i=pe({inputs:{x:e},attrs:{shape:[o,r]},backend:s}),u=So(i,e.dtype,"max",s),l=pe({inputs:{x:u},attrs:{shape:n},backend:s});return s.disposeIntermediateTensorInfo(i),s.disposeIntermediateTensorInfo(u),l}function I2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reductionIndices:a,keepDims:o}=s,i=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,i),p=c!=null,d=n.shouldExecuteOnCPU([r]),h=r;if(p){if(d){let v=n.texData.get(h.dataId).values,x=new Array(i);for(let $=0;$<x.length;$++)x[$]=r.shape[c[$]];let k=Rv(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,i)}C.assertAxesAreInnerMostDims("max",l,i);let[f,m]=C.computeOutAndReduceShapes(h.shape,l),g=f;o&&(g=C.expandShapeToKeepDim(f,u));let b;if(d){let v=n.texData.get(h.dataId).values,x=_X(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=m7(h,m,g,n);return p&&n.disposeIntermediateTensorInfo(h),b}var g7={kernelName:Ha,backendName:"webgl",kernelFunc:I2},b7=s2+`
2022-04-21 15:39:40 +02:00
return max(a, b);
2022-06-02 16:39:53 +02:00
`,y7=`
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-06-02 16:39:53 +02:00
`,v7=jt({opSnippet:b7,packedOpSnippet:y7,cpuKernelImpl:AX}),x7={kernelName:qa,backendName:"webgl",kernelFunc:v7};function w7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t;ou(r,"maxPool");let{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=s,l=1;w.assert(C.eitherStridesOrDilationsAreOne(o,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${o} and dilations '${l}'`);let c=C.computePool2DInfo(r.shape,a,o,l,i,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Rn({inputs:{x:r},backend:n});let p=new ol(c,"max",!1);return n.runWebGLProgram(p,[r],r.dtype)}var k7={kernelName:ja,backendName:"webgl",kernelFunc:w7};function S7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:o,pad:i,dataFormat:u,dimRoundingMode:l}=s,c=[1,1,1],p=C.computePool3DInfo(r.shape,a,o,c,i,l,u),d=new Fv(p,"max",!1);return n.runWebGLProgram(d,[r],r.dtype)}var I7={kernelName:lp,backendName:"webgl",kernelFunc:S7},C7=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,o=r-1-e.padInfo.top,i=a-1-e.padInfo.left,u=r*a-1;this.userCode=`
const ivec2 pads = ivec2(${o}, ${i});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},N7=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,o=e.dilationWidth,i=e.effectiveFilterDepth,u=e.effectiveFilterHeight,l=e.effectiveFilterWidth,c=i-1-e.padInfo.front,p=u-1-e.padInfo.top,d=l-1-e.padInfo.left,h=i*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;
2022-06-02 16:39:53 +02:00
for (int wD = 0; wD < ${i};
2022-04-21 15:39:40 +02:00
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};
2022-06-02 16:39:53 +02:00
wC += ${o}) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function T7(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,o=a,{filterSize:i,strides:u,pad:l,dimRoundingMode:c}=s,p=[1,1,1],d=C.computePool3DInfo(o.shape,i,u,p,l,c),h=new Fv(d,"max",!0),f=n.runWebGLProgram(h,[o],o.dtype),m=new N7(d),g=n.runWebGLProgram(m,[r,f],o.dtype);return n.disposeIntermediateTensorInfo(f),g}var $7={kernelName:Fg,backendName:"webgl",kernelFunc:T7};function _7(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a,output:o}=t,i=a;ou([a,o],"maxPoolGrad");let{filterSize:u,strides:l,pad:c,dimRoundingMode:p}=s,d=C.computePool2DInfo(i.shape,u,l,1,c,p),h=!0,f=new ol(d,"max",h),m=n.runWebGLProgram(f,[i],i.dtype),g=new C7(d),b=n.runWebGLProgram(g,[r,m],i.dtype);return n.disposeIntermediateTensorInfo(m),b}var A7={kernelName:Dg,backendName:"webgl",kernelFunc:_7};function E7(e,t,n,s){let r=new ol(n,"max",!1),a=s.runWebGLProgram(r,[e],"float32");r=new ol(n,"max",!0,!0,t);let o=s.runWebGLProgram(r,[e],"float32");return[a,o]}var R7={kernelName:Og,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{filterSize:r,strides:a,pad:o,includeBatchInIndex:i}=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,o),[p,d]=E7(s,i,c,u);return[p,d]}};function D7(e,t,n,s){let r=w.sizeFromShape(t),o=w.sizeFromShape(e.shape)/r,i=pe({inputs:{x:e},attrs:{shape:[o,r]},backend:s}),u=So(i,"float32","mean",s),l=pe({inputs:{x:u},attrs:{shape:n},backend:s});return s.disposeIntermediateTensorInfo(i),s.disposeIntermediateTensorInfo(u),l}var F7={kernelName:Ka,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{keepDims:r,axis:a}=t,o=n,i=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=C.getAxesPermutation(l,i),p=c!=null,d=o.shouldExecuteOnCPU([s]),h=[],f=s;if(p){if(d){let x=o.texData.get(f.dataId).values,k=new Array(i);for(let R=0;R<k.length;R++)k[R]=s.shape[c[R]];let I=Rv(x,s.shape,s.dtype,c,k);f=o.makeTensorInfo(k,s.dtype);let $=o.texData.get(f.dataId);$.values=I}else f=rh(s,c,o);h.push(f),l=C.getInnerMostAxes(l.length,i)}C.assertAxesAreInnerMostDims("sum",l,i);let[m,g]=C.computeOutAndReduceShapes(f.shape,l),b=m;r&&(b=C.expandShapeToKeepDim(m,u));let y=D7(f,g,b,o);for(let v of h)o.disposeIntermediateTensorInfo(v);return y}};function O7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s,i=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=C.getAxesPermutation(l,i),p=r;c!=null&&(p=pn({inputs:{x:r},backend:n,attrs:{perm:c}}),l=C.getInnerMostAxes(l.length,r.shape.length)),C.assertAxesAreInnerMostDims("min",l,i);let[d,h]=C.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=So(m,m.dtype,"min",n),b;if(o){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 P7={kernelName:Xa,backendName:"webgl",kernelFunc:O7},z7=s2+`
2022-04-21 15:39:40 +02:00
return min(a, b);
2022-06-02 16:39:53 +02:00
`,L7=`
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-06-02 16:39:53 +02:00
`,M7=jt({opSnippet:z7,packedOpSnippet:L7,cpuKernelImpl:EX}),B7={kernelName:Ya,backendName:"webgl",kernelFunc:M7},V7=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=rt(s),a=t.map(l=>l[0]).join(","),o=t.map((l,c)=>l[0]+e[c]).join(","),i=["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};
2022-06-02 16:39:53 +02:00
int end = ${o};
2022-04-21 15:39:40 +02:00
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});
2022-06-02 16:39:53 +02:00
${r} end = ${r}(${o});
2022-04-21 15:39:40 +02:00
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;
2022-06-02 16:39:53 +02:00
setOutput(getX(${i}));
2022-04-01 15:13:32 +02:00
}
2022-06-02 16:39:53 +02:00
`}},W7=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=rt(s),a=t.map(h=>h[0]).join(","),o=t.map((h,f)=>h[0]+e[f]).join(","),i=ln("rc",s),u=ln("source",s),l=`${i[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});
2022-06-02 16:39:53 +02:00
${i[s-1]} += 1;
2022-04-21 15:39:40 +02:00
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});
2022-06-02 16:39:53 +02:00
${i[s-1]} += 1;
2022-04-21 15:39:40 +02:00
if(${l}) {
${h}
result[1] = getChannel(getX(${u.join()}), ${c});
}
rc = outputLoc;
2022-06-02 16:39:53 +02:00
${i[s-2]} += 1;
if(${i[s-2]} < ${this.outputShape[s-2]}) {
2022-04-21 15:39:40 +02:00
${h}
result[2] = getChannel(getX(${u.join()}), ${c});
2022-06-02 16:39:53 +02:00
${i[s-1]} += 1;
2022-04-21 15:39:40 +02:00
if(${l}) {
${h}
result[3] = getChannel(getX(${u.join()}), ${c});
}
}
`}this.userCode=`
const ${r} start = ${r}(${a});
2022-06-02 16:39:53 +02:00
const ${r} end = ${r}(${o});
2022-04-21 15:39:40 +02:00
void main() {
${r} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${d}
setOutput(result);
}
2022-06-02 16:39:53 +02:00
`}},U7=({inputs:e,backend:t,attrs:n})=>{let{x:s}=e,{paddings:r,mode:a}=n,o=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new W7(s.shape,r,a):new V7(s.shape,r,a);return t.runWebGLProgram(o,[s],s.dtype)},G7={kernelName:Qa,backendName:"webgl",kernelFunc:U7},H7=`if (b == 0.0) return NAN;
return mod(a, b);`,q7=`
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-06-02 16:39:53 +02:00
`,j7=jt({opSnippet:H7,packedOpSnippet:q7}),K7={kernelName:Cl,backendName:"webgl",kernelFunc:j7},X7=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-06-02 16:39:53 +02:00
`}},Y7=`
2022-04-21 15:39:40 +02:00
if (a == b) {
return 1.0;
2022-04-21 15:38:36 +02:00
};
2022-06-02 16:39:53 +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-06-02 16:39:53 +02:00
`,C2=jt({opSnippet:Y7,packedOpSnippet:Q7,checkOutOfBounds:!0}),Z7={kernelName:Oa,backendName:"webgl",kernelFunc:C2},Dw="return a - b;",N2=jt({opSnippet:Dw,packedOpSnippet:Dw,supportsComplex:!0,cpuKernelImpl:KX}),J7={kernelName:fo,backendName:"webgl",kernelFunc:N2};function T2(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{dim:a}=s,o=w.parseAxisParam([a],r.shape),i=I2({inputs:{x:r},backend:n,attrs:{reductionIndices:o,keepDims:!1}}),u=C.expandShapeToKeepDim(i.shape,o),l=pe({inputs:{x:i},backend:n,attrs:{shape:u}}),c=N2({inputs:{a:r,b:l},backend:n}),p=w2({inputs:{x:c},backend:n}),d=ah({inputs:{x:p},backend:n,attrs:{axis:o,keepDims:!1}}),h=pe({inputs:{x:d},backend:n,attrs:{shape:u}}),f=C2({inputs:{a:p,b:h},backend:n});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(l),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}var eJ={kernelName:po,backendName:"webgl",kernelFunc:T2};function tJ(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{numSamples:a,seed:o,normalized:i}=s,u=i?r:T2({inputs:{logits:r},backend:n,attrs:{dim:r.shape.length-1}}),l=u.shape[0],c=u.shape[1],p=new X7(l,c,a),d=[[o]],h=n.runWebGLProgram(p,[u],"int32",d);return i||n.disposeIntermediateTensorInfo(u),h}var nJ={kernelName:Pg,backendName:"webgl",kernelFunc:tJ},sJ=ss+`
2022-04-21 15:39:40 +02:00
return -x;
2022-06-02 16:39:53 +02:00
`,rJ=`
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-06-02 16:39:53 +02:00
`;function aJ(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])){let a=n.texData.get(s.dataId),[o,i]=DX(a.values,s.shape,s.dtype);return n.makeTensorInfo(i,s.dtype,o)}let r;return K().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new ea(s.shape,rJ):r=new Gs(s.shape,sJ),n.runWebGLProgram(r,[s],s.dtype)}var oJ={kernelName:$i,backendName:"webgl",kernelFunc:aJ},iJ=ws.nonMaxSuppressionV3Impl;function uJ(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:o,iouThreshold:i,scoreThreshold:u}=s,l=n.readSync(r.dataId),c=n.readSync(a.dataId),{selectedIndices:p}=iJ(l,c,o,i,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var lJ={kernelName:Ai,backendName:"webgl",kernelFunc:uJ},cJ=ws.nonMaxSuppressionV4Impl;function dJ(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:o,iouThreshold:i,scoreThreshold:u,padToMaxOutputSize:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),{selectedIndices:d,validOutputs:h}=cJ(c,p,o,i,u,l);return[n.makeTensorInfo([d.length],"int32",new Int32Array(d)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var pJ={kernelName:Nl,backendName:"webgl",kernelFunc:dJ},hJ=ws.nonMaxSuppressionV5Impl;function fJ(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:o,iouThreshold:i,scoreThreshold:u,softNmsSigma:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),d=o,h=i,f=u,m=l,{selectedIndices:g,selectedScores:b}=hJ(c,p,d,h,f,m);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var mJ={kernelName:Ei,backendName:"webgl",kernelFunc:fJ},gJ=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-06-02 16:39:53 +02:00
`}},bJ=e=>{let{inputs:t,backend:n,attrs:s}=e,{indices:r}=t,{depth:a,onValue:o,offValue:i}=s,u=w.sizeFromShape(r.shape),l=new gJ(u,a,o,i),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},yJ={kernelName:Di,backendName:"webgl",kernelFunc:bJ};function jd(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="complex64"){let r=nc({inputs:{input:s},backend:n}),a=jd({inputs:{x:r},backend:n}),o=oh({inputs:{input:s},backend:n}),i=jd({inputs:{x:o},backend:n}),u=Fr({inputs:{real:a,imag:i},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(i),u}else return sc({attrs:{shape:s.shape,dtype:s.dtype,value:s.dtype==="string"?"":0},backend:n})}var vJ={kernelName:Xi,backendName:"webgl",kernelFunc:jd};function $2(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=$2({inputs:{x:r},backend:n}),o=oh({inputs:{input:s},backend:n}),i=jd({inputs:{x:o},backend:n}),u=Fr({inputs:{real:a,imag:i},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(i),u}else return sc({attrs:{shape:s.shape,dtype:s.dtype,value:1},backend:n})}var xJ={kernelName:Ri,backendName:"webgl",kernelFunc:$2};function wJ(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s;if(t.length===1)return rg({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let a=t[0].shape,o=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(o===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let i=[],u=t.map(c=>{let p=rg({inputs:{input:c},backend:n,attrs:{dim:r}});return i.push(p),p}),l=f2({inputs:u,backend:n,attrs:{axis:r}});return i.forEach(c=>n.disposeIntermediateTensorInfo(c)),l}var kJ={kernelName:Fi,backendName:"webgl",kernelFunc:wJ},SJ=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=rt(s),a=t.map(u=>u[0]).join(","),o=t.map((u,l)=>u[0]+e[l]).join(","),i=["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};
2022-06-02 16:39:53 +02:00
int end = ${o};
2022-04-21 15:39:40 +02:00
void main() {
int outC = getOutputCoords();
if (outC < start || outC >= end) {
setOutput(value);
} else {
setOutput(getX(outC - start));
}
}
`;return}this.userCode=`
${r} start = ${r}(${a});
2022-06-02 16:39:53 +02:00
${r} end = ${r}(${o});
2022-04-21 15:39:40 +02:00
void main() {
${r} outC = getOutputCoords();
if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {
setOutput(value);
} else {
${r} coords = outC - start;
2022-06-02 16:39:53 +02:00
setOutput(getX(${i}));
2022-04-21 15:39:40 +02:00
}
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}},IJ=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=rt(s),a=t.map(f=>f[0]).join(","),o=t.map((f,m)=>f[0]+e[m]).join(","),i=ln("rc",s),u=ln("source",s),l=`${i[s-1]} < ${this.outputShape[s-1]}`,c=s===1?"source":`vec2(${u.slice(-2).join()})`,p=[`${r} rc = outputLoc;`,`${i[s-1]} += 1;
2022-04-21 15:39:40 +02:00
if(${l}) {
`,s===1?"":`}
rc = outputLoc;
2022-06-02 16:39:53 +02:00
${i[s-2]} += 1;
if(${i[s-2]} < ${this.outputShape[s-2]}) {`,s===1?"":` ${i[s-1]} += 1;
2022-04-21 15:39:40 +02:00
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});
2022-06-02 16:39:53 +02:00
const ${r} end = ${r}(${o});
2022-04-21 15:39:40 +02:00
void main() {
${r} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${h}
setOutput(result);
}
2022-06-02 16:39:53 +02:00
`}},_2=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{paddings:a,constantValue:o}=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:o,dtype:r.dtype}})}let i=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new IJ(r.shape,a,o):new SJ(r.shape,a,o),u=[[o]];return n.runWebGLProgram(i,[r],r.dtype,u)},CJ={kernelName:Ja,backendName:"webgl",kernelFunc:_2},NJ=`
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-06-02 16:39:53 +02:00
`,TJ=`
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-06-02 16:39:53 +02:00
`,$J=jt({opSnippet:NJ,packedOpSnippet:TJ}),_J={kernelName:eo,backendName:"webgl",kernelFunc:$J};function AJ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s,i=r.shape.length,u=[],l=w.parseAxisParam(a,r.shape),c=l,p=C.getAxesPermutation(c,i),d=r;p!=null&&(d=pn({inputs:{x:r},backend:n,attrs:{perm:p}}),c=C.getInnerMostAxes(c.length,i),u.push(d)),C.assertAxesAreInnerMostDims("prod",c,i);let h;if(n.shouldExecuteOnCPU([d])){let f=n.texData.get(d.dataId).values,{outVals:m,outShape:g,outDtype:b}=OX(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=So(b,y,"prod",n);h=pe({inputs:{x:v},backend:n,attrs:{shape:f}}),u.push(b),u.push(v)}if(o){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 EJ={kernelName:no,backendName:"webgl",kernelFunc:AJ},A2=e=>{let{backend:t,attrs:n}=e,{start:s,stop:r,step:a,dtype:o}=n,i=PX(s,r,a,o);return t.makeTensorInfo([i.length],o,i)},RJ={kernelName:Tl,backendName:"webgl",kernelFunc:A2},DJ="return 1.0 / x;",FJ=Ke({opSnippet:DJ}),OJ={kernelName:$l,backendName:"webgl",kernelFunc:FJ},PJ=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : x;
2022-06-02 16:39:53 +02:00
`,zJ=`
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-06-02 16:39:53 +02:00
`,LJ=Ke({opSnippet:PJ,packedOpSnippet:zJ}),MJ={kernelName:so,backendName:"webgl",kernelFunc:LJ},BJ=ss+`
2022-04-21 15:39:40 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2022-06-02 16:39:53 +02:00
`,VJ=`
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-06-02 16:39:53 +02:00
`,WJ=Ke({opSnippet:BJ,packedOpSnippet:VJ}),UJ={kernelName:ao,backendName:"webgl",kernelFunc:WJ},GJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?o-1:o,s&&n>1?i-1:i],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]});
2022-06-02 16:39:53 +02:00
const vec2 inputShapeRC = vec2(${o}.0, ${i}.0);
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},HJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?o-1:o,s&&n>1?i-1:i],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]});
2022-06-02 16:39:53 +02:00
const vec3 inputShapeRC = vec3(${o}.0, ${i}.0,
${i}.0);
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function qJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:o,size:i}=s,[u,l]=i,c=K().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new HJ(r.shape,u,l,a,o):new GJ(r.shape,u,l,a,o);return n.runWebGLProgram(c,[r],"float32")}var jJ={kernelName:ro,backendName:"webgl",kernelFunc:qJ},KJ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,s,r]=t,[,a,o]=e,i=[n&&a>1?s-1:s,n&&o>1?r-1:r],u=[n&&a>1?a-1:a,n&&o>1?o-1:o],l=i[0]/u[0],c=i[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
2022-06-02 16:39:53 +02:00
if (dyC < 0 || dyC >= ${o}) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function XJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r,dy:a}=t,{alignCorners:o}=s,i=new KJ(a.shape,r.shape,o);return n.runWebGLProgram(i,[a],a.dtype)}var YJ={kernelName:Lg,backendName:"webgl",kernelFunc:XJ},QJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?o-1:o,s&&n>1?i-1:i],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]});
2022-06-02 16:39:53 +02:00
const vec2 inputShapeRC = vec2(${o}.0, ${i}.0);
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},ZJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?o-1:o,s&&n>1?i-1:i],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]});
2022-06-02 16:39:53 +02:00
const vec3 inputShapeRC = vec3(${o}.0, ${i}.0,
${i}.0);
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function JJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:o,size:i}=s,[u,l]=i,c=K().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new ZJ(r.shape,u,l,a,o):new QJ(r.shape,u,l,a,o);return n.runWebGLProgram(c,[r],r.dtype)}var eee={kernelName:_l,backendName:"webgl",kernelFunc:JJ},tee=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,s,r]=t,[,a,o]=e,i=[n&&a>1?s-1:s,n&&o>1?r-1:r],u=[n&&a>1?a-1:a,n&&o>1?o-1:o],l=i[0]/u[0],c=i[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
2022-06-02 16:39:53 +02:00
if (dyC < 0 || dyC >= ${o}) {
2022-04-21 15:39:40 +02:00
continue;
}
float sourceFracRow =
2022-06-02 16:39:53 +02:00
float(${i[0]}) *
2022-04-21 15:39:40 +02:00
(float(dyR) / float(${u[0]}));
float sourceFracCol =
2022-06-02 16:39:53 +02:00
float(${i[1]}) *
2022-04-21 15:39:40 +02:00
(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-06-02 16:39:53 +02:00
`}};function nee(e){let{inputs:t,backend:n,attrs:s}=e,{images:r,dy:a}=t,{alignCorners:o}=s,i=new tee(a.shape,r.shape,o);return n.runWebGLProgram(i,[a],a.dtype)}var see={kernelName:zg,backendName:"webgl",kernelFunc:nee},ree=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-06-02 16:39:53 +02:00
`;return}let s=o=>t.indexOf(o)!==-1&&e[o]!==1?`${e[o]} - coords[${o}] - 1`:`coords[${o}]`,r=e.map((o,i)=>s(i)).join(","),a=rt(n);this.userCode=`
2022-04-21 15:39:40 +02:00
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${r}));
}
2022-06-02 16:39:53 +02:00
`}},aee=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=ln("rc",n),r=`${s[n-1]} + 1 < ${this.outputShape[n-1]}`,a=`${s[n-2]} + 1 < ${this.outputShape[n-2]}`,o=rt(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() {
2022-06-02 16:39:53 +02:00
${o} rc = getOutputCoords();
2022-04-21 15:39:40 +02:00
vec4 result = vec4(0.);
2022-06-02 16:39:53 +02:00
result.r = ${i(s.slice())};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`;function i(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 oee(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dims:a}=s,o=r.shape.length,i=w.parseAxisParam(a,r.shape);if(o===0)return Rn({inputs:{x:r},backend:n});let u=K().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new aee(r.shape,i):new ree(r.shape,i);return n.runWebGLProgram(u,[r],r.dtype)}var iee={kernelName:Pi,backendName:"webgl",kernelFunc:oee},uee=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-06-02 16:39:53 +02:00
`}},lee={kernelName:Yi,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:s}=e,{radians:r,fillValue:a,center:o}=t,i=n,u=new uee(s.shape,a),[l,c]=C.getImageCenter(o,s.shape[1],s.shape[2]),p=[[l,c,Math.sin(r),Math.cos(r)]];return i.runWebGLProgram(u,[s],s.dtype,p)}},cee=`
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-06-02 16:39:53 +02:00
`,dee=Ke({opSnippet:cee}),pee={kernelName:zi,backendName:"webgl",kernelFunc:dee},hee="return inversesqrt(x);",fee=Ke({opSnippet:hee,cpuKernelImpl:zX}),mee={kernelName:oo,backendName:"webgl",kernelFunc:fee},E2=class{constructor(e,t,n,s,r,a,o=!0){this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;let i=rt(r.length),u=rt(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=`
${i} strides = ${i}(${r});
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function gee(e){let{inputs:t,backend:n,attrs:s}=e,{indices:r,updates:a}=t,{shape:o}=s,{sliceRank:i,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=C.calculateShapes(a,r,o),d=[p/l,l];if(p===0)return n.makeTensorInfo(o,r.dtype);let h=pe({inputs:{x:r},backend:n,attrs:{shape:[u,i]}}),f=pe({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=n.makeTensorInfo([],"float32",new Float32Array([0])),g=new E2(u,i,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:o}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(b),n.disposeIntermediateTensorInfo(m),y}var bee={kernelName:Li,backendName:"webgl",kernelFunc:gee},yee=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;`,o=K().getNumber("WEBGL_VERSION")===2?r:a,i=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;
2022-06-02 16:39:53 +02:00
${o}
2022-05-09 14:16:00 +02:00
mid = (left + right) / 2;
2022-06-02 16:39:53 +02:00
if (getSortedSequence(batch, mid) ${i} value) {
2022-05-09 14:16:00 +02:00
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-06-02 16:39:53 +02:00
`}};function vee(e){let{inputs:t,backend:n,attrs:s}=e,{sortedSequence:r,values:a}=t,{side:o}=s,i=new yee(r.shape[0],r.shape[1],a.shape[1],o),u=[[r.shape[1]]];return n.runWebGLProgram(i,[r,a],"int32",u)}var xee={kernelName:Mg,backendName:"webgl",kernelFunc:vee},wee=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 o=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],u=[];for(let l=0;l<t.length;l++)u.push(`${o[l]}`),l<e&&i.push(`${o[l]}`);s=i.join(),r=u.join()}let a=rt(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-06-02 16:39:53 +02:00
`}};function kee(e){let{inputs:t,backend:n}=e,{condition:s,t:r,e:a}=t,o=new wee(s.shape.length,r.shape,r.shape.length);return n.runWebGLProgram(o,[s,r,a],cn(r.dtype,a.dtype))}var See={kernelName:Mi,backendName:"webgl",kernelFunc:kee},Iee=`
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-06-02 16:39:53 +02:00
`,Cee=Ke({opSnippet:Iee}),Nee={kernelName:Al,backendName:"webgl",kernelFunc:Cee},Tee=du+`
2022-04-21 15:39:40 +02:00
return 1.0 / (1.0 + exp(-1.0 * x));
2022-06-02 16:39:53 +02:00
`,$ee=`
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-06-02 16:39:53 +02:00
`,_ee=Ke({opSnippet:Tee,packedOpSnippet:$ee,cpuKernelImpl:MX}),Aee={kernelName:uo,backendName:"webgl",kernelFunc:_ee},Eee=`
2022-04-21 15:39:40 +02:00
if (isnan(x)) { return 0.0; }
return sign(x);
2022-06-02 16:39:53 +02:00
`,Ree=Ke({opSnippet:Eee}),Dee={kernelName:El,backendName:"webgl",kernelFunc:Ree},Fee=du+`
2022-04-21 15:39:40 +02:00
return sin(x);
2022-06-02 16:39:53 +02:00
`,Oee=Ke({opSnippet:Fee}),Pee={kernelName:io,backendName:"webgl",kernelFunc:Oee},zee=`
2022-04-21 15:39:40 +02:00
float e2x = exp(x);
return (e2x - 1.0 / e2x) / 2.0;
2022-06-02 16:39:53 +02:00
`,Lee=Ke({opSnippet:zee}),Mee={kernelName:Vi,backendName:"webgl",kernelFunc:Lee},Bee=`
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-06-02 16:39:53 +02:00
`,Vee=Ke({opSnippet:Bee}),Wee={kernelName:Rl,backendName:"webgl",kernelFunc:Vee},Uee=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,paddings:o}=s;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let i=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...o);for(let b=1+a.length;b<r.shape.length;++b)u.push([0,0]);let l=[],c=_2({inputs:{x:r},backend:n,attrs:{paddings:u,constantValue:0}}),p=C.getReshaped(c.shape,a,i,!1),d=C.getPermuted(p.length,a.length,!1),h=C.getReshapedPermuted(c.shape,a,i,!1),f=pe({inputs:{x:c},backend:n,attrs:{shape:p}}),m=pn({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},Gee={kernelName:Wi,backendName:"webgl",kernelFunc:Uee};function Hee(e){let{inputs:t,backend:n}=e,{indices:s,values:r,denseShape:a,defaultValue:o}=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:
2022-06-02 16:39:53 +02:00
${r.shape}`);if(o.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
${o.shape}`);let i=n.readSync(s.dataId),u=n.readSync(r.dataId),l=n.readSync(a.dataId),c=n.readSync(o.dataId)[0],[p,d,h,f,m]=VX(i,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 qee={kernelName:dp,backendName:"webgl",kernelFunc:Hee};function jee(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 o=Array.from(n.readSync(r.dataId)),i=n.readSync(s.dataId),u=Array.from(n.readSync(a.dataId)),[l,c,p]=WX(i,s.shape,s.dtype,o,u);return[n.makeTensorInfo(c,s.dtype,l),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var Kee={kernelName:Dl,backendName:"webgl",kernelFunc:jee};function Xee(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-06-02 16:39:53 +02:00
${a.shape}`);let o=n.readSync(s.dataId),i=n.readSync(r.dataId),u=n.readSync(a.dataId),[l,c]=Z1(o,s.shape,s.dtype,i,u,!0);return n.makeTensorInfo(c,s.dtype,l)}var Yee={kernelName:pp,backendName:"webgl",kernelFunc:Xee};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-06-02 16:39:53 +02:00
${a.shape}`);let o=n.readSync(s.dataId),i=n.readSync(r.dataId),u=n.readSync(a.dataId),[l,c]=Z1(o,s.shape,s.dtype,i,u);return n.makeTensorInfo(c,s.dtype,l)}var Zee={kernelName:hp,backendName:"webgl",kernelFunc:Qee};function Jee(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:o}=t,{outputShape:i}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=C.calculateShapes(a,r,i),h=!1;if(a.dtype==="string"){let b=n.bufferSync(r),y=n.bufferSync(a),v=w.decodeString(n.readSync(o.dataId)[0]),x=LX(b,y,i,d,c,l,u,p,v,h);return n.makeTensorInfo(i,x.dtype,x.values)}let f=new E2(l,u,r.shape.length,a.shape.length,p,[d,1],h),m=n.runWebGLProgram(f,[a,r,o],a.dtype),g=pe({inputs:{x:m},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(m),g}var ete={kernelName:fp,backendName:"webgl",kernelFunc:Jee};function tte(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:o}=s,i=w.parseAxisParam(o,r.shape)[0],u=C.prepareSplitSize(r,a,i),l=r.shape.length,c=new Array(l).fill(0),p=r.shape.slice();return u.map(d=>{let h=[...p];h[i]=d;let f=pu({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[i]+=d,f})}var nte={kernelName:Ui,backendName:"webgl",kernelFunc:tte},Fw="return sqrt(x);",ste=Ke({opSnippet:Fw,packedOpSnippet:Fw,cpuKernelImpl:UX}),rte={kernelName:lo,backendName:"webgl",kernelFunc:ste},ate="return x * x;",ote=Ke({opSnippet:ate}),ite={kernelName:Fl,backendName:"webgl",kernelFunc:ote},Ow="return (a - b) * (a - b);",ute=jt({opSnippet:Ow,packedOpSnippet:Ow}),lte={kernelName:ho,backendName:"webgl",kernelFunc:ute};function cte({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-06-02 16:39:53 +02:00
`,a=new Gs(s.shape,r);return n.runWebGLProgram(a,[s],s.dtype)}var dte={kernelName:go,backendName:"webgl",kernelFunc:cte},pte=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let s=n.length,r=rt(n.length),a=rt(n.length),o="";if(s===1)o="coords * strides + begin";else{let i=0;o=n.map((u,l)=>(i++,n.length===1?`coords * strides[${l}] + begin[${l}]`:`coords[${i-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();
2022-06-02 16:39:53 +02:00
setOutput(getX(${o}));
2022-04-18 17:29:45 +02:00
}
2022-06-02 16:39:53 +02:00
`}};function hte(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:o,strides:i,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,o,i,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=pu({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=GX(h,E,x,y);k=n.makeTensorInfo(f,r.dtype,P.values)}else{let R=new pte(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 fte={kernelName:Gi,backendName:"webgl",kernelFunc:hte};function mte(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.readSync(c.dataId),h=n.readSync(p.dataId),[f,m]=HX(d,h,r,a,o,i,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var gte={kernelName:mp,backendName:"webgl",kernelFunc:mte};function bte(e){let{inputs:t,backend:n,attrs:s}=e,{skipEmpty:r}=s,{input:a,delimiter:o}=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(o.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${o.shape}`);let i=n.readSync(a.dataId),u=n.readSync(o.dataId)[0],[l,c,p]=qX(i,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 yte={kernelName:Bg,backendName:"webgl",kernelFunc:bte};function vte(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 o=n.readSync(a.dataId),i=jX(o,r);return n.makeTensorInfo(a.shape,"int32",i)}var xte={kernelName:Vg,backendName:"webgl",kernelFunc:vte},wte="return tan(x);",kte=Ke({opSnippet:wte}),Ste={kernelName:Hi,backendName:"webgl",kernelFunc:kte},Ite=`
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-06-02 16:39:53 +02:00
`,Cte=Ke({opSnippet:Ite}),Nte={kernelName:mo,backendName:"webgl",kernelFunc:Cte},Tte=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=rt(this.rank),r=$te(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-06-02 16:39:53 +02:00
`}};function $te(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 R2(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=XX(c,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let o=new Tte(r.shape,a);return n.runWebGLProgram(o,[r],r.dtype)}var _te={kernelName:Tr,backendName:"webgl",kernelFunc:R2},Ate=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-06-02 16:39:53 +02:00
`}},Ete=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-06-02 16:39:53 +02:00
`}};function Hr(e,t){t!==null&&e.disposeIntermediateTensorInfo(t)}function Pw(e){let t=1;for(;t<e;)t*=2;return t}function Rte(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:o}=s,i=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<i||a>u){let P=n.readSync(r.dataId),[A,D]=YX(P,l,r.dtype,a,o);return[n.makeTensorInfo(A.shape,A.dtype,A.values),n.makeTensorInfo(D.shape,D.dtype,D.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=Pw(a),y=Pw(c),v=null,x=()=>v===null?[g,g]:[g,v],k=(P,A,D)=>{let T=x(),L=new Ate(D),j=[[c],[v===null?1:0],[Number.NEGATIVE_INFINITY],[P],[A]],Y=v;v=n.runWebGLProgram(L,T,"int32",j),Hr(n,Y)};for(let P=1;P<b;P*=2){let A=P*2;for(let D=P;D>=1;D/=2)k(A,D,[m,y])}for(let P=y;P>b;P/=2){let A=x(),D=new Ete([m,P/2]),L=[[c],[v===null?1:0],[b]],W=v;v=n.runWebGLProgram(D,A,"int32",L),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=pu({inputs:{x:v},backend:n,attrs:{begin:0,size:[m,a]}}),Hr(n,I);let $=S2({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 Dte={kernelName:qi,backendName:"webgl",kernelFunc:Rte},Fte=class{constructor(e,t,n,s,r,a){this.variableNames=["Image","Transforms"],this.outputShape=a;let o=n==="nearest"?1:2,i;switch(s){case"constant":i=1;break;case"reflect":i=2;break;case"wrap":i=3;break;case"nearest":i=4;break;default:i=1;break}this.userCode=`
2022-04-21 15:39:40 +02:00
float mapCoord(float outCoord, float len) {
float inCoord = outCoord;
2022-06-02 16:39:53 +02:00
if(${i} == 2) {
2022-04-21 15:39:40 +02:00
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);
2022-06-02 16:39:53 +02:00
} else if (${i} == 3) {
2022-04-21 15:39:40 +02:00
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);
2022-06-02 16:39:53 +02:00
} else if (${i} == 4) {
2022-04-21 15:39:40 +02:00
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}));
2022-06-02 16:39:53 +02:00
if (${o} == 1) {
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function Ote(e){let{inputs:t,backend:n,attrs:s}=e,{image:r,transforms:a}=t,{interpolation:o,fillMode:i,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 Fte(p,d,o,i,u,g);return n.runWebGLProgram(b,[r,a],"float32")}var Pte={kernelName:ji,backendName:"webgl",kernelFunc:Ote};function zte(e){let{inputs:t,attrs:n,backend:s}=e,{axis:r}=n,{x:a}=t;ou(a,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let o=s.readSync(a.dataId),{outputValues:i,outputShape:u,indices:l}=QX(o,r,a.shape,a.dtype);return[s.makeTensorInfo(u,a.dtype,i),s.makeTensorInfo([l.length],"int32",l)]}var Lte={kernelName:Wg,backendName:"webgl",kernelFunc:zte};function Mte(e){let{inputs:t,backend:n,attrs:s}=e,{value:r}=t,{axis:a}=s;a<0&&(a+=r.shape.length);let o=r,i=o.shape.length,u=r.shape[a],l=new Array(i-1),c=0;for(let m=0;m<i;m++)m!==a&&(l[c++]=o.shape[m]);let p=[],d=new Array(i).fill(0),h=o.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){d[a]=m;let g=pu({inputs:{x:o},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 Bte={kernelName:Ki,backendName:"webgl",kernelFunc:Mte},Vte=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,s=e.batchSize,r=e.inSize,a=e.numSegments,o=a*Math.ceil(r/n);this.outputShape=[s,o];let i="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=`
2022-06-02 16:39:53 +02:00
const float initializationValue = ${i};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}};function Wte(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,segmentIds:a}=t,{numSegments:o}=s,i=r.shape.length,u=[],l=0,c=C.getAxesPermutation([l],i),p=r;c!=null&&(p=pn({inputs:{x:r},backend:n,attrs:{perm:c}}),u.push(p),l=C.getInnerMostAxes(1,i)[0]);let d=C.segment_util.computeOutShape(p.shape,l,o),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),D={windowSize:A,inSize:P,batchSize:E,numSegments:R},T=new Vte(D,k),L=n.compileAndRun(T,[x,I],$);if(u.push(L),L.shape[1]===R)return L;let W=A2({backend:n,attrs:{start:0,stop:R,step:1,dtype:"float32"}}),j=R2({inputs:{x:W},backend:n,attrs:{reps:[P/A]}});return u.push(W),u.push(j),g(L,k,j,$,R)},b=g(f,"unsortedSegmentSum",a,m,o),y=pe({inputs:{x:b},backend:n,attrs:{shape:d}}),v=y;if(c!=null){u.push(y);let x=C.getUndoAxesPermutation(c);v=pn({inputs:{x:v},backend:n,attrs:{perm:x}})}return u.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var Ute={kernelName:gp,backendName:"webgl",kernelFunc:Wte},Gte=[H8,j8,Y8,J8,tY,rY,oY,uY,pY,fY,bY,xY,SY,TY,AY,RY,FY,LY,BY,WY,qY,JY,t9,s9,l9,d9,m9,N8,y9,S9,T9,D9,O9,z9,M9,V9,G9,j9,Y9,Z9,eQ,nQ,aQ,iQ,dQ,hQ,gQ,vQ,wQ,CQ,_Q,DQ,PQ,MQ,BQ,WQ,GQ,qQ,KQ,YQ,eZ,sZ,oZ,uZ,dZ,fZ,yZ,kZ,C8,IZ,w9,TZ,AZ,DZ,$8,zZ,VZ,UZ,jZ,YZ,e7,s7,i7,d7,f7,g7,x7,k7,I7,$7,A7,R7,F7,P7,B7,G7,K7,nJ,D8,oJ,lJ,pJ,mJ,a9,yJ,xJ,kJ,CJ,_J,A8,EJ,RJ,o9,Z7,OJ,MJ,UJ,O8,jJ,YJ,eee,see,iee,lee,pee,mee,bee,xee,See,Nee,Aee,Dee,Pee,Mee,QY,eJ,Wee,Gee,qee,Kee,Yee,Zee,ete,nte,rte,ite,lte,dte,fte,gte,yte,xte,J7,W8,Ste,Nte,_te,Dte,Pte,U8,Lte,Bte,Ute,vJ];for(let e of Gte)Ol(e);var Or=K();Or.registerFlag("WEBGPU_DEFERRED_SUBMIT_BATCH_SIZE",()=>15);Or.registerFlag("WEBGPU_CPU_FORWARD",()=>!0);Or.registerFlag("WEBGPU_MATMUL_WORK_PER_THREAD",()=>4);Or.registerFlag("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE",()=>!1);Or.registerFlag("WEBGPU_USE_LOW_POWER_GPU",()=>!1);Or.registerFlag("WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD",()=>1e3);Or.registerFlag("WEBGPU_USE_PROFILE_TOOL",()=>!1);Or.registerFlag("WEBGPU_USE_IMPORT",()=>!1);var Hte="return a + b;",qte="return areal * breal - aimag * bimag;",jte="return areal * bimag + aimag * breal;",Kte="return a / b;",Xte="return a * b;",Yte="return (a - b) * (a - b);",Qte="return a - b;",Zte="return f32(a == b);",Jte="return vec4<f32>(a == b);",ene="return f32(a > b);",tne="return vec4<f32>(a > b);",nne="return f32(a >= b);",sne="return vec4<f32>(a >= b);",rne="return f32(a < b);",ane="return vec4<f32>(a < b);",one="return f32(a <= b);",ine="return vec4<f32>(a <= b);",une="return f32(f32(a) >= 1.0 && f32(b) >= 1.0);",lne=`return (vec4<f32>(a >= vec4<f32>(1.0)) *
vec4<f32>(b >= vec4<f32>(1.0)));`,cne=`
2022-04-21 15:39:40 +02:00
if (isnan(a)) { return a; }
if (isnan(b)) { return b; }
2022-06-02 16:39:53 +02:00
`,D2=`
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-06-02 16:39:53 +02:00
`,dne=`
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-06-02 16:39:53 +02:00
`,pne=`
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-06-02 16:39:53 +02:00
`,hne="return f32(a != b);",fne="return vec4<f32>(a != b);",mne=`
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-06-02 16:39:53 +02:00
`,gne=`
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-06-02 16:39:53 +02:00
${D2}
2022-04-21 15:39:40 +02:00
return resultTemp;
2022-06-02 16:39:53 +02:00
`,bne="if (a < 0.0) { return b * a; } return a;",yne=`
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-06-02 16:39:53 +02:00
`;function zw(e,t){let n=t?D2:cne;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-06-02 16:39:53 +02:00
`}function rc(e,t){switch(e){case 0:return Xte;case 1:return Hte;case 2:return Qte;case 3:return Kte;case 4:return t?Jte:Zte;case 5:return t?tne:ene;case 6:return t?sne:nne;case 7:return t?ane:rne;case 8:return t?ine:one;case 9:return t?lne:une;case 10:return t?fne:hne;case 11:return Yte;case 12:return t?pne:dne;case 14:return t?yne:bne;case 15:return zw("max",t);case 16:return zw("min",t);case 13:return t?gne:mne;case 17:return qte;case 18:return jte;default:throw new Error(`BinaryType ${e} is not implemented!`)}}var vne="return abs(a);",xne="return ceil(a);",wne="return cos(a);",kne=`
2022-04-21 15:39:40 +02:00
let e2x = exp(-a);
return (e2x + 1.0 / e2x) / 2.0;
2022-06-02 16:39:53 +02:00
`,Sne="return exp(a) - 1.0;",Ine="if (a >= 0.0) { return a; } return (exp(a) - 1.0);",Cne=`
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-06-02 16:39:53 +02:00
`,Nne="return exp(a);",Tne="return floor(a);",$ne="return a;",_ne=`if (a < 0.0) { return 1.0/0.0; }
return log(a);`,Ane="return f32(!(a >= 1.0));",Ene="return -a;",Rne="if (a < 0.0) { return uniforms.alpha * a; } return a;",Dne=`
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-06-02 16:39:53 +02:00
`,Fne="return select(a, 0.0, a < 0.0);",One="return clamp(a, 0.0, 6.0);",Pne="return clamp(a, vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(6.0, 6.0, 6.0, 6.0));",zne=`
2022-05-09 14:16:00 +02:00
return select(a, vec4<f32>(0.0), a < vec4<f32>(0.0));
2022-06-02 16:39:53 +02:00
`,Lne="return 1.0/sqrt(a);",Mne="return 1.0 / (1.0 + exp(-1.0 * a));",Bne="return sin(a);",Vne=`
2022-04-21 15:39:40 +02:00
let e2x = exp(a);
return (e2x - 1.0 / e2x) / 2.0;
2022-06-02 16:39:53 +02:00
`,Wne="return sqrt(a);",Une="return a * a;",Gne=`
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-06-02 16:39:53 +02:00
`,Hne="return f32(i32((a)));";function jr(e,t){switch(e){case 0:return vne;case 2:return wne;case 3:return kne;case 1:return xne;case 4:return t?Cne:Ine;case 5:return Nne;case 6:return Sne;case 7:return Tne;case 8:return $ne;case 9:return _ne;case 10:return Ane;case 11:return Ene;case 14:return t?Dne:Rne;case 12:return t?zne:Fne;case 13:return t?Pne:One;case 15:return Lne;case 18:return Mne;case 16:return Bne;case 17:return Vne;case 19:return Wne;case 20:return Une;case 21:return Gne;case 22:return Hne;default:throw new Error(`BinaryType ${e} is not implemented!`)}}function Io(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.`)}var F2={};Ee(F2,{ArrayBufferToTypedArray:()=>P2,GPUBytesPerElement:()=>md,computeDispatch:()=>_e,computeWorkGroupSizeForConv2d:()=>Ov,computeWorkGroupSizeForMatMul:()=>O2,computeWorkPerThreadForConv2d:()=>Pv,flatDispatchLayout:()=>Ve,isWebGPUSupported:()=>zv,tilesFitEvenlyIntoShape:()=>qne});var ra=e=>{let t=1;for(let n=0;n<e.length;n++)t*=e[n];return t};function qne(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,o]=[Math.ceil(ra(e.x.map(i=>t[i]))/(n[0]*s[0])),e.y?Math.ceil(ra(e.y.map(i=>t[i]))/(n[1]*s[1])):1,e.z?Math.ceil(ra(e.z.map(i=>t[i]))/(n[2]*s[2])):1];return[r,a,o]}function Ov(e,t,n=!1){if(n)return[8,8,1];let s=ra(e.x.map(a=>t[a])),r=ra(e.y.map(a=>t[a]));return s<=4?[4,16,1]:r<=4?[16,4,1]:[16,16,1]}function O2(e,t,n){return e===1?[32,1,1]:n===1?[1,32,1]:[8,8,1]}function Pv(e,t,n=!1){if(n)return[4,4,1];let s=ra(e.x.map(a=>t[a])),r=ra(e.y.map(a=>t[a]));return s<=4?[1,2,1]:r<=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 P2(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 zv(){return(typeof window!="undefined"||typeof WorkerGlobalScope!="undefined")&&!!navigator.gpu}var jne=(e,t)=>e?`
mm_Asub[inputRow][inputCol] = mm_readA(
t * TileInner + inputRow,
globalRowStart / ${t} + inputCol, globalId);
`:`
mm_Asub[inputRow][inputCol] = mm_readA(
globalRow + innerRow,
t * TileInner / ${t} + inputCol, globalId);
`,Kne=(e,t)=>e?`
let ACached0 = mm_Asub[k * InnerElementSize][localRow];
let ACached1 = mm_Asub[k * InnerElementSize + 1][localRow];
let ACached2 = mm_Asub[k * InnerElementSize + 2][localRow];
${t===3?"":"let ACached3 = mm_Asub[k * InnerElementSize + 3][localRow];"}
for (var i = 0; i < RowPerThread; i = i + 1) {
acc[i] = BCached[0] * ACached0[i] + acc[i];
acc[i] = BCached[1] * ACached1[i] + acc[i];
acc[i] = BCached[2] * ACached2[i] + acc[i];
${t===3?"":"acc[i] = BCached[3] * ACached3[i] + acc[i];"}
}`:`
for (var i = 0; i < RowPerThread; i = i + 1) {
let ACached = mm_Asub[tileRow + i][k];
acc[i] = BCached[0] * ACached.x + acc[i];
acc[i] = BCached[1] * ACached.y + acc[i];
acc[i] = BCached[2] * ACached.z + acc[i];
${t===3?"":"acc[i] = BCached[3] * ACached.w + acc[i];"}
}`;function z2(e,t,n,s,r=4,a=!1){let o=a?t:s,i=a?s:t,u=a?e[1]:r;return w.assert((a&&t===n||s%4===0||s%3===0)&&e[0]===4&&(r===3||r===4),()=>`tileInner ${s} must be divisible by 4|3. ColPerThread ${e[0]} must be 4.
innerElementSize ${r} must be 3|4.`),`
var<workgroup> mm_Asub : array<array<vec${u}<f32>, ${o/u}>, ${i}>;
var<workgroup> mm_Bsub : array<array<vec4<f32>, ${n/e[0]}>, ${s}>;
let RowPerThread = ${e[1]};
let ColPerThread = ${e[0]};
let InnerElementSize = ${r};
let TileInner = ${s};
2022-04-21 15:39:40 +02:00
@stage(compute) @workgroup_size(workGroupSizeX, workGroupSizeY, workGroupSizeZ)
fn main(@builtin(local_invocation_id) LocalId : vec3<u32>,
@builtin(global_invocation_id) GlobalId : vec3<u32>,
2022-06-02 16:39:53 +02:00
@builtin(num_workgroups) NumWorkgroups: vec3<u32>,
@builtin(workgroup_id) workgroupId: vec3<u32>) {
2022-04-21 15:39:40 +02:00
localId = LocalId;
globalId = GlobalId;
numWorkgroups = NumWorkgroups;
2022-06-02 16:39:53 +02:00
let localRow = i32(localId.y);
let tileRow = ${t===1?"0":"localRow * RowPerThread"};
let tileCol = i32(localId.x);
let globalRow = ${t===1?"0":"i32(globalId.y) * RowPerThread"};
let globalCol = i32(globalId.x);
let globalRowStart = i32(workgroupId.y) * ${t};
let numTiles = (uniforms.dimInner - 1) / TileInner + 1;
var acc: array<vec4<f32>, RowPerThread>;
var BCached : array<vec4<f32>, 4>;
// Loop over shared dimension.
let RowPerThreadB = TileInner / i32(workGroupSizeY);
let tileRowB = localRow * 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;
${jne(a,u)}
}
// 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.
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];"}
${Kne(a,r)}
}
workgroupBarrier();
}
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
mm_write(globalRow + innerRow,
globalCol,
acc[innerRow], globalId);
2022-04-18 17:29:45 +02:00
}
2022-06-02 16:39:53 +02:00
}`}var Xne=class{constructor(e,t,n,s,r=!1,a=null,o=null,i=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&&!r?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=i!=null;u&&this.variableNames.push("bias"),l&&this.variableNames.push("preluActivationWeights"),this.tileAOuter=t[1]===1&&!r?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=o,this.hasPreluActivationWeights=l,this.batchAEqualOne=n,this.batchBEqualOne=s,this.transposeA=r;let c=r?e[1]:e[2];this.fitAOuter=t[1]%this.tileAOuter===0,this.fitBOuter=t[2]%this.tileBOuter===0,this.fitInner=c%this.tileInner===0,this.shaderKey=`matMulPackedVec4_${this.activation}_${this.fitAOuter}_${this.fitBOuter}_${this.fitInner}_${this.elementsPerThread}_${this.batchAEqualOne}_${this.batchBEqualOne}_${this.transposeA}`}getUserCode(){let e=this.fitAOuter&&this.fitInner?"return A[batch * batchASize + row * uniforms.aShape[2] / 4 + col]":`if (coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.aShape[1], uniforms.aShape[2]))) {
return A[batch * batchASize + row * uniforms.aShape[2] / 4 + col];
}
return vec4<f32>(0.0)`,t=this.fitInner&&this.fitBOuter?"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];
}
return vec4<f32>(0.0)`,n="",s="";if(this.activation){let o=Io(this.activation,this.isVec4);this.hasPreluActivationWeights?n=`fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${o}
}`:n=`
fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
${o}
}`,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};
}
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};
}
fn mm_write(row : i32, col : i32, valueIn : vec4<f32>, globalId : vec3<u32>) {
if (row < uniforms.dimAOuter && col * 4 < uniforms.dimBOuter)
{
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);
}
}
${z2(this.elementsPerThread,this.tileAOuter,this.tileBOuter,this.tileInner,4,this.transposeA)}
`}};function Yne(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}var Lw=(e,t,n,s,r,a=!1)=>{let o={dtype:r.dtype,shape:r.shape},i=Qne(s,o,t,a),u=e.createShaderModule({code:i,label:t.constructor.name});return e.createComputePipeline({layout:n,compute:{module:u,entryPoint:"main"},label:t.constructor.name})};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 hr(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 Ue(){return`
${ac()}
let index = getGlobalIndex();
`}function ac(){return`
${Lv()}
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 Lv(){return`
@stage(compute) @workgroup_size(workGroupSizeX, workGroupSizeY, workGroupSizeZ)
`}function Qne(e,t,n,s=!1){let r=[];if(r.push(`
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 {
${L2(n)?" 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);
`}
}
`),s===!0)return r.push(`
struct Uniform {
size : i32,
numChannels : i32,
outShapeStrides : vec2<i32>,
dispatchSize : vec3<u32>,
};
@group(0) @binding(0) var<storage, write> result: array<${gd(t.dtype,n.isVec4)}>;
@group(0) @binding(2) var<uniform> uniforms: Uniform;
`),[Bw,r.join(`
`),Vw(t.shape),n.getUserCode()].join(`
`);let a=!1,o=!1,i="struct Uniforms { NAN : f32, ";n.variableNames.forEach((m,g)=>{let b=Wt(e[g].shape.length);(b==="vec5"||b==="vec6")&&(o=!0),(a||o)&&(i+="@align(16) "),a=o,i+=`${m.charAt(0).toLowerCase()+m.slice(1)}Shape : ${b}, `});let u=Wt(t.shape.length);o=u==="vec5"||u==="vec6",(a||o)&&(i+="@align(16) "),a=o,i+=`outShape : ${u}, `;let l=t.shape.length-1,c=Wt(l);o=c==="vec5"||c==="vec6",(a||o)&&(i+="@align(16) "),a=o,i+=`
outShapeStrides: ${c}, `,n.size&&(a&&(i+="@align(16) "),a=!1,i+="size : i32, "),n.uniforms&&(a&&(i+="@align(16) "),i+=n.uniforms),i+="};",r.push(i),n.atomic?r.push(`
@group(0) @binding(0) var<storage, read_write> result: array<atomic<i32>>;
`):r.push(`
@group(0) @binding(0) var<storage, write> result: array<${gd(t.dtype,n.isVec4)}>;
`),n.variableNames.forEach((m,g)=>{r.push(`
@group(0) @binding(${1+g}) var<storage, read> ${m}: array<${n.variableTypes?n.variableTypes[g]:gd(e[g].dtype,n.isVec4)}>;
`)}),i!==""&&r.push(`
@group(0) @binding(${1+n.variableNames.length}) var<uniform> uniforms: Uniforms;
`);let[p,d]=tse(t.shape,n.dispatchLayout),h=[Bw,r.join(`
`),Vw(t.shape),p,nse(t.shape.length)];if(n.atomic||h.push(sse(t.shape,t.dtype,n.isVec4)),d===t.shape.length){let m=e.map((g,b)=>ese(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-06-02 16:39:53 +02:00
`)}function Mw(e,t,n=[],s="",r=""){let a=L2(e)?"flatDispatch":"";return e.shaderKey+"_"+(e.workGroupSize?e.workGroupSize.join(","):"")+t.map(i=>i.length).join(",")+n.join(",")+e.variableNames.join(",")+s+r+a}var Bw=`
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-06-02 16:39:53 +02:00
`;function Vw(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 o=0;o<t;o++)r.push(`d${o}`);if(n.length===1)return` fn getCoordsFromIndex(index : i32) -> vec2<i32> {
let d0 = index / uniforms.outShapeStrides; let d1 = index - d0 * uniforms.outShapeStrides;
return vec2<i32>(d0, d1);
}`;let a;return a="var index2 = index;"+n.map((o,i)=>{let u=`let ${r[i]} = index2 / uniforms.outShapeStrides.${hr(i)}`,l=i===n.length-1?`let ${r[i+1]} = index2 - ${r[i]} * uniforms.outShapeStrides.${hr(i)}`:`index2 = index2 - ${r[i]} * uniforms.outShapeStrides.${hr(i)}`;return`${u}; ${l};`}).join(""),`
fn getCoordsFromIndex(index : i32) -> ${s} {
${a}
return ${s}(${r.join(",")});
}
`}function Zne(e,t){let n=e.name,s=e.shape.length,r=Wt(s),a="get"+n.charAt(0).toUpperCase()+n.slice(1),o=["d0","d1","d2","d3","d4","d5"].slice(0,s),i=o.map(c=>`${c} : i32`).join(", ");if(s<1)return t?`
fn ${a}() -> vec4<f32> {
return vec4<f32>(${n}[0]);
}
`:`
fn ${a}() ->f32 {
return f32(${n}[0]);
}
`;let u=`uniforms.${n.charAt(0).toLowerCase()+n.slice(1)}Shape`,l=`${s}D`;return s===0&&(l="1D"),t?`
fn ${a}(${i}) -> vec4<f32> {
return vec4<f32>(${n}[getIndexFromCoords${l}(${r}(${o.join(",")}),
${u}) / 4]);
}
`:`
fn ${a}(${i}) -> f32 {
return f32(${n}[getIndexFromCoords${l}(${r}(${o.join(",")}),
${u})]);
}
`}function Jne(e,t,n,s){let r=e.name,a=r.charAt(0).toUpperCase()+r.slice(1),o="get"+a+"ByOutput",i=e.shape.length,u=t.length,l=Wt(u);if(w.arraysEqual(e.shape,t)&&s)return n?`
fn ${o}Index(globalIndex : i32) -> vec4<f32> {
return vec4<f32>(${r}[globalIndex]);
}
fn ${o}Coords(coords : ${l}) -> vec4<f32> {
return vec4<f32>(${r}[${u>1?"getOutputIndexFromCoords(coords)":"coords"} / 4]);
}
`:`
fn ${o}Index(globalIndex : i32) -> f32 {
return f32(${r}[globalIndex]);
}
fn ${o}Coords(coords : ${l}) -> f32 {
return f32(${r}[${u>1?"getOutputIndexFromCoords(coords)":"coords"}]);
}
`;let c=C.getBroadcastDims(e.shape,t),p=u-i,d="";if(i===0)return n?`
fn ${o}Index(globalIndex : i32) -> vec4<f32> {
return get${a}();
}
fn ${o}Coords(coords : ${l}) -> vec4<f32> {
return get${a}();
}
`:`
fn ${o}Index(globalIndex : i32) -> f32{
return get${a}();
}
fn ${o}Coords(coords : ${l}) -> f32{
return get${a}();
}
`;u<2&&c.length>=1?d="coords = 0;":d=c.map(g=>`coords.${hr(g+p)} = 0;`).join(`
`);let h="";if(u<2&&i>0)h="coords";else if(u>1){let g=Wt(i),b=e.shape.map((y,v)=>`coords.${hr(v+p)}`).join(", ");h=`${g}(${b})`}else h="coords";let f=`uniforms.${r.charAt(0).toLowerCase()+r.slice(1)}Shape`,m=`${i}D`;return n?`
fn ${o}Index(globalIndex : i32) -> vec4<f32> {
var coords = getCoordsFromIndex(globalIndex);
${d}
return ${r}[getIndexFromCoords${m}(${h}, ${f}) / 4];
}
fn ${o}Coords(coordsIn : ${l}) -> vec4<f32> {
var coords = coordsIn;
${d}
return ${r}[getIndexFromCoords${m}(${h}, ${f}) / 4];
}
`:`
fn ${o}Index(globalIndex : i32) -> f32 {
var coords = getCoordsFromIndex(globalIndex);
${d}
return f32(${r}[getIndexFromCoords${m}(${h}, ${f})]);
}
fn ${o}Coords(coordsIn : ${l}) -> f32 {
var coords = coordsIn;
${d}
return f32(${r}[getIndexFromCoords${m}(${h}, ${f})]);
}
`}function ese(e,t,n,s){let r=Zne(e,n);return e.shape.length<=t.length&&(r+=Jne(e,t,n,s)),r}function tse(e,t){let{x:n,y:s=[],z:r=[]}=t,a=e.length;if(n.length===a)return[`fn getOutputCoords() -> ${Wt(a)}{
let globalIndex = getGlobalIndex();
return getCoordsFromIndex(globalIndex);
}
`,a];let o="",i=[n,s,r],u=0;for(let d=0;d<i.length;d++){let h=i[d];if(h.length!==0)if(u+=h.length,h.length===1)o+=`let d${h[0]} = i32(globalId[${d}]);`;else{let f=Yne(h,"uniforms.outShape");o+=`var index${d} = i32(globalId[${d}]);`;for(let m=0;m<f.length;m++)o+=`let d${h[m]} = index${d} / ${f[m]};`,m===f.length-1?o+=`let d${h[m+1]} = index${d} - d${h[m]} * ${f[m]};`:o+=`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} {
${o}
`;return l.length===0?p+=`return ${c}(0); }`:p+=`return ${c}(${l.join(",")}); }`,[p,u]}function nse(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-06-02 16:39:53 +02:00
`;break;default:w.assert(!1,()=>`Unsupported ${e}D shape`);break}return t}function L2(e){return e.dispatch[1]===1&&e.dispatch[2]===1}function gd(e,t){return e==="float32"?t?"vec4<f32>":"f32":e==="int32"||e==="bool"?t?"vec4<i32>":"i32":e}function sse(e,t,n){let s=e.length,r=gd(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-06-02 16:39:53 +02:00
}`,s>=2){let o=["d0","d1","d2","d3","d4","d5"].slice(0,s),i=Wt(s);n?a+=`
fn setOutputAtCoords(${o.map(u=>`${u} : i32`).join(", ")}, value : vec4<f32>) {
let flatIndex = getOutputIndexFromCoords(${i}(${o.join(", ")}));
2022-04-21 15:39:40 +02:00
setOutputAtIndex(flatIndex / 4, value);
2022-04-18 18:26:05 +02:00
}
2022-06-02 16:39:53 +02:00
fn setOutputAtCoordsI32(${o.map(u=>`${u} : i32`).join(", ")}, value : vec4<i32>) {
let flatIndex = getOutputIndexFromCoords(${i}(${o.join(", ")}));
2022-04-21 15:39:40 +02:00
setOutputAtIndexI32(flatIndex / 4, value);
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
`:a+=`
2022-06-02 16:39:53 +02:00
fn setOutputAtCoords(${o.map(u=>`${u} : i32`).join(", ")}, value : f32) {
let flatIndex = getOutputIndexFromCoords(${i}(${o.join(", ")}));
2022-04-21 15:39:40 +02:00
setOutputAtIndex(flatIndex, value);
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
fn setOutputAtCoordsI32(${o.map(u=>`${u} : i32`).join(", ")}, value : i32) {
let flatIndex = getOutputIndexFromCoords(${i}(${o.join(", ")}));
2022-04-21 15:39:40 +02:00
setOutputAtIndexI32(flatIndex, value);
}
2022-06-02 16:39:53 +02:00
`}return a}var rse=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-06-02 16:39:53 +02:00
`,ase=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];";function Mv(e,t,n=!1,s=32){let r=e[1]*t[1],a=e[0]*t[0],o=n?r:s,i=n?s:r;w.assert(i%t[1]===0&&o%t[0]===0&&s%t[1]===0,()=>`tileAHight ${i} must be divisible by workGroupSize[1]${t[1]}, tileAWidth ${o} must be divisible by workGroupSize[0]${t[0]}, tileInner ${s} must be divisible by workGroupSize[1]${t[1]}`);let u=i/t[1],l=o/t[0],c=s/t[1];return`
var<workgroup> mm_Asub : array<array<f32, ${o}>, ${i}>;
2022-05-24 13:28:51 +02:00
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-06-02 16:39:53 +02:00
${rse(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-06-02 16:39:53 +02:00
${ase(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-06-02 16:39:53 +02:00
mm_write(globalRow + innerRow,
globalCol + innerCol,
acc[innerRow][innerCol], globalId);
2022-04-21 15:39:40 +02:00
}
2022-04-21 15:38:36 +02:00
}
}
2022-06-02 16:39:53 +02:00
`}var ose=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-06-02 16:39:53 +02:00
`;function ise(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-06-02 16:39:53 +02:00
${ac()}
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-06-02 16:39:53 +02:00
mm_Asub[tileCol] = vec4<f32>(${ose(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
2022-06-02 16:39:53 +02:00
mm_write(globalRow, globalCol, acc, globalId);
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}var use=class{constructor(e,t,n,s,r,a=!1,o=!1,i=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=O2(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=i!=null,d=l!=null;p&&this.variableNames.push("bias"),d&&this.variableNames.push("preluActivationWeights"),this.workPerThread=n,this.transposeA=a,this.transposeB=o,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}_${o}_${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,o=t%r===0,i=n%this.tileInner===0;return[a,o,i]}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;
2022-06-02 16:39:53 +02:00
`,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 o=Io(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);
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}
`,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>) {
2022-06-02 16:39:53 +02:00
${this.fitAOuter&&this.fitBOuter?"":"if (row < uniforms.dimAOuter && col < uniforms.dimBOuter)"}
{
2022-04-21 15:39:40 +02:00
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col);
${r}
${s}
setOutputAtCoords(batch, row, col, value);
2022-06-02 16:39:53 +02:00
}
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
${this.outputShape[1]>1?Mv([this.workPerThread,this.workPerThread,1],this.workGroupSize,this.transposeA,this.tileInner):ise(this.workGroupSize,this.transposeA)}
`}};function lse(){return`
2022-04-21 15:39:40 +02:00
var<workgroup> sumValues : array<f32, workGroupSizeX>;
2022-06-02 16:39:53 +02:00
${ac()}
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-06-02 16:39:53 +02:00
`}var cse=class{constructor(e,t,n,s=!1,r=!1,a=null,o=null,i=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=i!=null;u&&this.variableNames.push("bias"),l&&this.variableNames.push("preluActivationWeights"),this.transposeA=s,this.transposeB=r,this.addBias=u,this.activation=o,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 o=Io(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);
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}
`,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-06-02 16:39:53 +02:00
${lse()}
`}};function dse(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-06-02 16:39:53 +02:00
${ac()}
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-06-02 16:39:53 +02:00
`}var pse=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 o=s!=null;o&&this.variableNames.push("bias");let i=a!=null;i&&this.variableNames.push("preluActivationWeights"),this.addBias=o,this.activation=r,this.hasPreluActivationWeights=i,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-06-02 16:39:53 +02:00
return 0.0;`,n="",s="";if(this.activation){let o=Io(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);
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}`:n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
2022-06-02 16:39:53 +02:00
${o}
2022-04-21 15:39:40 +02:00
}`,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-06-02 16:39:53 +02:00
${dse(this.workGroupSize)}
`}};function Le(e){let{inputs:t,attrs:n}=e,{x:s}=t,{shape:r}=n,a=w.sizeFromShape(s.shape),o=w.inferFromImplicitShape(r,a),i=w.sizeFromShape(o);return w.assert(a===i,()=>`The new shape (${o}) has ${i} 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:o,dtype:s.dtype}}var hse={kernelName:Oi,backendName:"webgpu",kernelFunc:Le};function Bv({a:e,b:t,transposeA:n,transposeB:s,backend:r,bias:a=null,preluActivationWeights:o=null,leakyreluAlpha:i=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=Qi.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],$=Le({inputs:{x:e},backend:r,attrs:{shape:k}}),R=Le({inputs:{x:t},backend:r,attrs:{shape:I}}),E=[$,R],P=Math.max(b,y),A=b===1,D=y===1,T=(p%4===0&&!n||h%4===0&&n)&&f%4===0&&!s,L;h*f<=32?L=new cse([P,h,f],A,D,n,s,a,u,o):!n&&!s&&(h<=16&&(f<=512||d>=2*f)||f<=16&&(h<=512||p>=2*h))?L=new pse(k,I,[P,h,f],a,u,o):T?L=new Xne(k,[P,h,f],A,D,n,a,u,o):L=new use(k,[P,h,f],K().get("WEBGPU_MATMUL_WORK_PER_THREAD"),A,D,n,s,a,u,o);let W=[$,R];a&&W.push(a),o&&W.push(o);let j=[{type:"int32",data:[h]},{type:"int32",data:[f]},{type:"int32",data:[p]}];u==="leakyrelu"&&(j.push({type:"float32",data:[i]}),L.uniforms+=" alpha : f32,");let Y=r.runWebGPUProgram(L,W,e.dtype,j),X=Le({inputs:{x:Y},backend:r,attrs:{shape:x}});E.push(Y);for(let Z of E)r.disposeData(Z.dataId);return X}function fse(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a,bias:o,preluActivationWeights:i}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=s;return Bv({a:r,b:a,transposeA:u,transposeB:l,backend:n,bias:o,preluActivationWeights:i,leakyreluAlpha:p,activation:c})}var mse={kernelName:oa,backendName:"webgpu",kernelFunc:fse},Ww=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-06-02 16:39:53 +02:00
`}},gse=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-06-02 16:39:53 +02:00
`}},bse=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-06-02 16:39:53 +02:00
`}},M2=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-06-02 16:39:53 +02:00
`}};function Uw(e,t,n){if(w.arraysEqual(t,n)&&w.sizeFromShape(t)%4===0)return new bse(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 gse(e,t,n,a):new M2(e,t,n)}function Un(e){let{inputs:t}=e,{x:n}=t;return e.backend.incRef(n.dataId),{dataId:n.dataId,shape:n.shape,dtype:n.dtype}}var yse={kernelName:Wa,backendName:"webgpu",kernelFunc:Un};function hu(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.makeTensorInfo(s.shape,"complex64"),o=n.tensorMap.get(a.dataId),i=Un({inputs:{x:s},backend:n}),u=Un({inputs:{x:r},backend:n});return o.complexTensorInfos={real:i,imag:u},a}var vse={kernelName:np,backendName:"webgpu",kernelFunc:hu},oc=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-06-02 16:39:53 +02:00
`}};function Kt({opType:e,cpuKernelImpl:t,dtype:n}){return({inputs:s,backend:r})=>{let{x:a}=s,o=r,i=n||a.dtype;if(o.shouldExecuteOnCPU([a])&&t!=null){let l=o.tensorMap.get(a.dataId),c=t(l.values,i);return o.makeTensorInfo(a.shape,i,c)}let u=new oc(a.shape,e);return o.runWebGPUProgram(u,[a],i)}}function gn({opSnippet:e,cpuKernelImpl:t,supportsComplex:n=!1,dtype:s}){return({inputs:r,backend:a})=>{let{a:o,b:i}=r,u=a;if(n&&o.dtype==="complex64"){let p=u.tensorMap.get(o.dataId),d=u.tensorMap.get(i.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:o.shape},x={dataId:y.dataId,dtype:y.dtype,shape:i.shape},k=Uw(e,o.shape,i.shape);return u.runWebGPUProgram(k,[v,x],cn(b.dtype,y.dtype))});else{let g=new Ww(17,o.shape,i.shape),b=new Ww(18,o.shape,i.shape),y=[{dataId:p.complexTensorInfos.real.dataId,dtype:p.complexTensorInfos.real.dtype,shape:o.shape},{dataId:p.complexTensorInfos.imag.dataId,dtype:p.complexTensorInfos.imag.dtype,shape:o.shape},{dataId:d.complexTensorInfos.real.dataId,dtype:d.complexTensorInfos.real.dtype,shape:i.shape},{dataId:d.complexTensorInfos.imag.dataId,dtype:d.complexTensorInfos.imag.dtype,shape:i.shape}];h=u.runWebGPUProgram(g,y,"float32"),f=u.runWebGPUProgram(b,y,"float32")}let m=hu({inputs:{real:h,imag:f},backend:u});return u.disposeData(h.dataId),u.disposeData(f.dataId),m}let l=s||cn(o.dtype,i.dtype);if((o.dtype==="string"||i.dtype==="string"||u.shouldExecuteOnCPU([o,i]))&&t!=null){let p=u.tensorMap.get(o.dataId).values,d=u.tensorMap.get(i.dataId).values,h=o.dtype==="string"?C.fromUint8ToStringArray(p):p,f=o.dtype==="string"?C.fromUint8ToStringArray(d):d,[m,g]=t(o.shape,i.shape,h,f,l);return u.makeTensorInfo(g,l,m)}let c=Uw(e,o.shape,i.shape);return u.runWebGPUProgram(c,[o,i],l)}}var{addImpl:xse,ceilImpl:wse,concatImpl:kse,equalImpl:Sse,expImpl:Ise,expm1Impl:Cse,floorImpl:Nse,gatherNdImpl:Tse,gatherV2Impl:$se,greaterEqualImpl:_se,greaterImpl:Ase,lessEqualImpl:Ese,lessImpl:Rse,logImpl:Dse,maxImpl:Fse,maximumImpl:Ose,minimumImpl:Pse,multiplyImpl:zse,negImpl:Lse,notEqualImpl:Mse,prodImpl:Bse,rangeImpl:Vse,rsqrtImpl:Wse,scatterImpl:Use,simpleAbsImpl:Gse,sliceImpl:Hse,stridedSliceImpl:qse,stringNGramsImpl:jse,subImpl:Kse,tileImpl:Xse,topKImpl:Yse,transposeImpl:Qse,uniqueImpl:Lhe}=cv,Zse=Kt({opType:0,cpuKernelImpl:Gse}),Jse={kernelName:di,backendName:"webgpu",kernelFunc:Zse},ere=gn({opSnippet:1,cpuKernelImpl:xse,supportsComplex:!0}),tre={kernelName:Cr,backendName:"webgpu",kernelFunc:ere},nre=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-06-02 16:39:53 +02:00
`}};function sre(e){let{inputs:t,backend:n}=e,s=t;if(s.length===1)return Un({inputs:{x:s[0]},backend:n});let r=s.map(i=>i.dtype).reduce((i,u)=>cn(i,u)),a=s.map(i=>i.shape),o=new nre(a);return n.runWebGPUProgram(o,s,r)}var rre={kernelName:Sa,backendName:"webgpu",kernelFunc:sre},B2=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-06-02 16:39:53 +02:00
`,t=()=>this.inputShape.length===1?"uniforms.xShape":`uniforms.xShape.${hr(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.${hr(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-06-02 16:39:53 +02:00
`}},are=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-06-02 16:39:53 +02:00
${Lv()}
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-06-02 16:39:53 +02:00
`}},ore=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=ire(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-06-02 16:39:53 +02:00
`}};function ire(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.${hr(s)}`;return n.join()}function Ks(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{perm:a}=s,o=n,i=r.shape.length,u=new Array(i);for(let c=0;c<u.length;c++)u[c]=r.shape[a[c]];if(n.shouldExecuteOnCPU([r])){let p=o.tensorMap.get(r.dataId).values,d=Qse(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 are(r.shape,a);return o.runWebGPUProgram(c,[r],r.dtype)}let l=new ore(r.shape,a);return o.runWebGPUProgram(l,[r],r.dtype)}var ure={kernelName:Hs,backendName:"webgpu",kernelFunc:Ks};function lre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,o=w.parseAxisParam(a,r.shape),i=C.getAxesPermutation(o,r.shape.length),u=r,l=[];i!=null&&(u=Ks({inputs:{x:r},backend:n,attrs:{perm:i}}),l.push(u),o=C.getInnerMostAxes(o.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMax",[o[0]],u.shape.length);let c=new B2(u.shape,o[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 cre={kernelName:Ia,backendName:"webgpu",kernelFunc:lre};function dre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,o=w.parseAxisParam(a,r.shape),i=C.getAxesPermutation(o,r.shape.length),u=r,l=[];i!=null&&(u=Ks({inputs:{x:r},backend:n,attrs:{perm:i}}),l.push(u),o=C.getInnerMostAxes(o.length,u.shape.length)),C.assertAxesAreInnerMostDims("argMin",[o[0]],u.shape.length);let c=new B2(u.shape,o[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 pre={kernelName:pl,backendName:"webgpu",kernelFunc:dre},V2=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-06-02 16:39:53 +02:00
`}},W2=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-06-02 16:39:53 +02:00
`}};function hre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=s,l=1,c=C.computePool2DInfo(r.shape,a,o,l,i,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Un({inputs:{x:r},backend:n});let p,d=[{type:"int32",data:[c.strideHeight,c.strideWidth]}];return c.filterHeight===1&&c.filterWidth===1?p=new W2(c):(p=new V2(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 fre={kernelName:Ca,backendName:"webgpu",kernelFunc:hre};function mre(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a}=t,{transposeA:o,transposeB:i}=s;return Bv({a:r,b:a,transposeA:o,transposeB:i,backend:n})}var gre={kernelName:Na,backendName:"webgpu",kernelFunc:mre},bre=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=yre(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.${ag[a]} = uniforms.start[${a}] + coords.${ag[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-06-02 16:39:53 +02:00
`}},ag=["x","y","z","w","u","v"];function yre(e){if(e===1)return"sourceLoc";if(e<=6)return ag.slice(0,e).map(t=>`sourceLoc.${t}`).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}function fu(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,size:o}=s,[i,u]=kt.parseSliceParams(r,a,o);if(kt.assertParamsValid(r,i,u),n.shouldExecuteOnCPU([r])||r.dtype==="string"){let p=n.tensorMap.get(r.dataId),d=Hse(p.values,i,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 bre(i,u),c=[{type:"int32",data:i}];return n.runWebGPUProgram(l,[r],r.dtype,c)}var vre={kernelName:Bi,backendName:"webgpu",kernelFunc:fu},xre=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,crops:o}=s;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGPU backend not implemented yet");let i=a.reduce((y,v)=>y*v),u=C.getReshaped(r.shape,a,i),l=C.getPermuted(u.length,a.length),c=C.getReshapedPermuted(r.shape,a,i),p=C.getSliceBeginCoords(o,a.length),d=C.getSliceSize(c,o,a.length),h=[],f=Le({inputs:{x:r},backend:n,attrs:{shape:u}}),m=Ks({inputs:{x:f},backend:n,attrs:{perm:l}}),g=Le({inputs:{x:m},backend:n,attrs:{shape:c}}),b=fu({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},wre={kernelName:pi,backendName:"webgpu",kernelFunc:xre},U2=gn({opSnippet:10,dtype:"bool",cpuKernelImpl:Mse}),kre={kernelName:_i,backendName:"webgpu",kernelFunc:U2};function ic(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.tensorMap.get(s.dataId);return Un({inputs:{x:r.complexTensorInfos.real},backend:n})}var Sre={kernelName:cp,backendName:"webgpu",kernelFunc:ic};function Ire(e,t){let n=new oc(e.shape,22),s=t.runWebGPUProgram(n,[e],"int32");return{dataId:s.dataId,shape:s.shape,dtype:s.dtype}}function og(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dtype:a}=s;if(a==="complex64"){if(r.dtype==="complex64")return Un({inputs:{x:r},backend:n});let o=$t(r.shape),i=og({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),u=hu({inputs:{real:i,imag:o},backend:n});return o.dispose(),n.disposeData(i.dataId),u}if(r.dtype==="complex64"){let o=ic({inputs:{input:r},backend:n}),i=og({inputs:{x:o},backend:n,attrs:{dtype:a}});return n.disposeData(o.dataId),i}if(!w.hasEncodingLoss(r.dtype,a)){let o=Un({inputs:{x:r},backend:n});return{dataId:o.dataId,shape:o.shape,dtype:a}}if(a==="int32")return Ire(r,n);if(a==="bool"){let o=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=U2({inputs:{a:r,b:o},backend:n});return n.disposeData(o.dataId),u}throw new Error(`Error in Cast: failed to cast ${r.dtype} to ${a}`)}var Cre={kernelName:Ta,backendName:"webgpu",kernelFunc:og},Nre=Kt({opType:1,cpuKernelImpl:wse}),Tre={kernelName:$a,backendName:"webgpu",kernelFunc:Nre},$re=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-06-02 16:39:53 +02:00
`}},_re=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-06-02 16:39:53 +02:00
`}};function Are(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{clipValueMin:a,clipValueMax:o}=s,i,u=[{type:"float32",data:[a]},{type:"float32",data:[o]}];return w.sizeFromShape(r.shape)%4===0?i=new $re(r.shape):i=new _re(r.shape),n.runWebGPUProgram(i,[r],r.dtype,u)}var Ere={kernelName:Nr,backendName:"webgpu",kernelFunc:Are},Rre=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-06-02 16:39:53 +02:00
`}};function ih(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.tensorMap.get(s.dataId);return Un({inputs:{x:r.complexTensorInfos.imag},backend:n})}var Dre={kernelName:ip,backendName:"webgpu",kernelFunc:ih};function ig(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=>ih({inputs:{input:y},backend:n})),m=ig(h,t,n),g=ig(f,t,n),b=hu({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 Le({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=kse(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:o}=Fre(e,t,n),i=a.map(h=>h.shape),u=new Rre(i),l=[],c=new Array(i.length-1);if(c.length>0){c[0]=i[0][1],l.push({type:"int32",data:[c[0]]});for(let h=1;h<c.length;h++)c[h]=c[h-1]+i[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=Le({inputs:{x:p},backend:n,attrs:{shape:o}});return n.disposeData(p.dataId),d}function Fre(e,t,n){let s=C.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>Le({inputs:{x:a},backend:n,attrs:{shape:[w.sizeFromShape(a.shape.slice(0,t)),w.sizeFromShape(a.shape.slice(t))]}})),outShape:s}}function G2(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s,a=w.parseAxisParam(r,t[0].shape)[0],o=C.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(o)===0)return n.makeTensorInfo(o,t[0].dtype,[]);let i=t.filter(l=>w.sizeFromShape(l.shape)>0);if(i.length===1)return Un({inputs:{x:i[0]},backend:n});let u=i.map(l=>l.shape);return C.assertParamsConsistent(u,a),ig(i,a,n)}var Ore={kernelName:hi,backendName:"webgpu",kernelFunc:G2},nr=e=>{switch(e){case 1:return"f32";case 2:return"vec2<f32>";case 3:return"vec3<f32>";case 4:return"vec4<f32>";default:throw new Error(`innerElementSize ${e} is not supported.`)}};function Pre(e,t,n,s,r=!1,a=null,o=!1,i=4,u=4,l=4){let c=D=>{switch(D){case 1:return"resData = x[xIndex];";case 3:return"resData = vec3<f32>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);";case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${D} is not supported.`)}},p=D=>{switch(D){case 1:return"return W[row * uniforms.wShape[3] + colIn];";case 4:return"return W[row * uniforms.wShape[3] / 4 + colIn];";default:throw new Error(`innerElementSize ${D} is not supported.`)}},d=e?`
let coord = vec4<i32>(batch, xRow, xCol, xCh);
`:`
let coord = vec4<i32>(batch, xCh, xRow, xCol);
`,h=e?`
let outCoord = vec4<i32>(
batch,
row / outWidth,
row % outWidth,
col);
`:`
let outCoord = vec4<i32>(
batch,
row,
col / outWidth,
col % outWidth);
`,f=e?"uniforms.xShape[1]":"uniforms.xShape[2]",m=e?"uniforms.xShape[2]":"uniforms.xShape[3]",g=e?"row":"col",b=e?"col":"row",y=`
let inChannels = uniforms.wShape[2];
let outWidth = ${e?"uniforms.outShape[2]":"uniforms.outShape[3]"};
let outRow = ${g} / outWidth;
let outCol = ${g} % outWidth;
let WRow = ${b} / (uniforms.filterDims[1] * inChannels);
let WCol = ${b} / inChannels % uniforms.filterDims[1];
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];
let xCh = ${b} % inChannels;
var resData = ${nr(i)}(0.0);
// The bounds checking is always needed since we use it to pad zero for
// the 'same' padding type.
if (xRow >= 0 && xRow < ${f} && xCol >= 0 && xCol < ${m}) {
${d}
let xIndex = getIndexFromCoords4D(coord, uniforms.xShape);
${c(i)}
}
return resData;`,v=e?t&&s?`
let col = colIn * ${i};
${y}`:`
let col = colIn * ${i};
if (row < uniforms.dimAOuter && col < uniforms.dimInner) {
${y}
}
return ${nr(i)}(0.0);`:s&&n?`
let col = colIn * ${i};
${y}`:`
let col = colIn * ${i};
if (row < uniforms.dimInner && col < uniforms.dimBOuter) {
${y}
}
return ${nr(i)}(0.0);`,x=`${p(u)}`,k=nr(l),I=nr(e?i:u),$=nr(e?u:i),R="",E="";if(a){let D=Io(a,l===4);o?R=`fn activation(a: ${k}, outCoord : vec4<i32>) -> ${k} {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${D}
}`:R=`
fn activation(a : ${k}, outCoord : vec4<i32>) -> ${k} {
${D}
}`,E="value = activation(value, outCoord);"}return`
${R}
fn mm_readA(row : i32, colIn : i32, globalId : vec3<u32>) -> ${I} {
var batch = i32(globalId.z);
${e?v:x}
}
fn mm_readB(row : i32, colIn : i32, globalId : vec3<u32>) -> ${$} {
var batch = i32(globalId.z);
${e?x:v}
}
fn mm_write(row : i32, colIn : i32, valueIn : ${k}, globalId : vec3<u32>) {
var col = colIn * ${l};
if (row < uniforms.dimAOuter && col < uniforms.dimBOuter)
{
var batch = i32(globalId.z);
var value = valueIn;
let outWidth = ${e?"uniforms.outShape[2]":"uniforms.outShape[3]"};
${h}
${r?"value = value + getBiasByOutputCoords(outCoord);":""}
${E}
setOutputAtCoords(outCoord[0], outCoord[1], outCoord[2], outCoord[3], value);
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
}`}var zre=class{constructor(e,t,n,s,r=!1,a=null,o=!1,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.isVec4=i,this.dispatchLayout=this.isChannelsLast?{x:[3],y:[1,2],z:[0]}:{x:[2,3],y:[1],z:[0]},this.workGroupSize=Ov(this.dispatchLayout,this.outputShape,this.isVec4),this.elementsPerThread=Pv(this.dispatchLayout,this.outputShape,this.isVec4),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),this.innerElementSize=this.isVec4?e.inChannels%4===0?4:3:this.elementsPerThread[0],this.isVec4&&(this.variableTypes=this.innerElementSize===3?["f32","vec4<f32>"]:["vec4<f32>","vec4<f32>"]),r&&(this.variableNames.push("bias"),this.isVec4&&this.variableTypes.push("vec4<f32>")),o&&(this.variableNames.push("preluActivationWeights"),this.isVec4&&this.variableTypes.push("vec4<f32>")),this.addBias=r,this.activation=a,this.hasPreluActivationWeights=o,this.tileAOuter=this.workGroupSize[1]*this.elementsPerThread[1],this.tileBOuter=this.workGroupSize[0]*this.elementsPerThread[0],this.tileInner=Math.max(this.workGroupSize[0]*this.innerElementSize,this.workGroupSize[1]),this.fitAOuter=t%this.tileAOuter===0,this.fitBOuter=n%this.tileBOuter===0,this.fitInner=s%this.tileInner===0,this.shaderKey=`conv2DMM_${this.elementsPerThread}_${this.activation}}_${this.fitAOuter}_${this.fitBOuter}_${this.fitInner}_${this.isVec4}_${this.innerElementSize}_${this.isChannelsLast}`}getUserCode(){let e=this.isVec4?z2(this.elementsPerThread,this.tileAOuter,this.tileBOuter,this.tileInner,this.innerElementSize,!this.isChannelsLast):Mv(this.elementsPerThread,this.workGroupSize,!this.isChannelsLast,this.tileInner),t=this.isVec4?[this.isChannelsLast?this.innerElementSize:4,4,4]:[1,1,1];return`
${Pre(this.isChannelsLast,this.fitAOuter,this.fitBOuter,this.fitInner,this.addBias,this.activation,this.hasPreluActivationWeights,t[0],t[1],t[2])}
2022-04-21 15:39:40 +02:00
${e}
2022-06-02 16:39:53 +02:00
`}};function Gw(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 Lre({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=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=Le({inputs:{x:e},backend:s,attrs:{shape:[1,n.batchSize,b]}}),f=Le({inputs:{x:t},backend:s,attrs:{shape:[1,b,n.outChannels]}})}else h=Le({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=Le({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}});if(d.push(h),d.push(f),a!=null){let b=Gw(a.shape,u);b!=null&&(a=Le({inputs:{x:a},backend:s,attrs:{shape:b}}),d.push(a))}if(r!=null){let b=Gw(r.shape,u);b!=null&&(r=Le({inputs:{x:r},backend:s,attrs:{shape:b}}),d.push(r))}let m=Bv({a:u?h:f,b:u?f:h,transposeA:l,transposeB:c,backend:s,bias:r,activation:i,preluActivationWeights:a,leakyreluAlpha:o}),g=Le({inputs:{x:m},backend:s,attrs:{shape:n.outShape}});d.push(m);for(let b of d)s.disposeData(b.dataId);return g}function H2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let u=r!=null,l=a!=null,c=n.dataFormat==="channelsLast";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 Lre({x:e,filter:t,convInfo:n,backend:s,bias:r,activation:i,preluActivationWeights:a,leakyreluAlpha:o});let d=((n.inChannels%4===0||n.inChannels%3===0)&&c||n.outWidth%4===0&&!c)&&n.outChannels%4===0,h=c?n.outHeight*n.outWidth:n.outChannels,f=c?n.outChannels:n.outHeight*n.outWidth,m=n.filterHeight*n.filterWidth*n.inChannels,g=[n.padInfo.top,n.padInfo.left],b=[{type:"int32",data:[n.filterHeight,n.filterWidth]},{type:"int32",data:[...g]},{type:"int32",data:[n.strideHeight,n.strideWidth]},{type:"int32",data:[n.dilationHeight,n.dilationWidth]},{type:"int32",data:[h]},{type:"int32",data:[f]},{type:"int32",data:[m]}],y=new zre(n,h,f,m,u,i,l,d),v=[],x=[e,t];u&&(!c&&r.shape.length===1&&(r=Le({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=Le({inputs:{x:a},backend:s,attrs:{shape:[a.shape[0],1,1]}}),v.push(a)),x.push(a)),i==="leakyrelu"&&(b.push({type:"float32",data:[o]}),y.uniforms+=" alpha : f32,");let k=s.runWebGPUProgram(y,x,e.dtype,b);for(let I of v)s.disposeData(I.dataId);return k}function Mre(e){let{inputs:t,attrs:n,backend:s}=e,{x:r,filter:a}=t,{strides:o,pad:i,dataFormat:u,dilations:l,dimRoundingMode:c}=n,p=C.convertConv2DDataFormat(u),d=C.computeConv2DInfo(r.shape,a.shape,o,l,i,c,!1,p);return H2({x:r,filter:a,convInfo:d,backend:s})}var Bre={kernelName:_a,backendName:"webgpu",kernelFunc:Mre},Vre=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=Ov(this.dispatchLayout,this.outputShape),this.elementsPerThread=Pv(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>) {
2022-06-02 16:39:53 +02:00
if (row < uniforms.dimAOuter && col < uniforms.dimBOuter)
{
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
}
2022-04-21 15:38:36 +02:00
}
2022-04-21 15:39:40 +02:00
2022-06-02 16:39:53 +02:00
${Mv(this.elementsPerThread,this.workGroupSize)}
`}},Wre=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-06-02 16:39:53 +02:00
`}};function Ure(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{inputShape:o,strides:i,pad:u,dataFormat:l,dimRoundingMode:c}=s,p=C.convertConv2DDataFormat(l),d=C.computeConv2DInfo(o,a.shape,i,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 Wre(d);else{f=new Vre(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 Gre={kernelName:Aa,backendName:"webgpu",kernelFunc:Ure},Hre=Kt({opType:2}),qre={kernelName:Ea,backendName:"webgpu",kernelFunc:Hre},jre=Kt({opType:3}),Kre={kernelName:Ra,backendName:"webgpu",kernelFunc:jre},Xre=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,o,i]=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};
2022-06-02 16:39:53 +02:00
let width_scale = ${o};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
let in_x = ${i};
2022-04-21 15:39:40 +02:00
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-06-02 16:39:53 +02:00
`}},Yre=e=>{let{inputs:t,backend:n,attrs:s}=e,{image:r,boxes:a,boxInd:o}=t,{cropSize:i,method:u,extrapolationValue:l}=s,c=new Xre(r.shape[3],a.shape,i,u),p=[{type:"float32",data:[l]}];return n.runWebGPUProgram(c,[r,a,o],"float32",p)},Qre={kernelName:mi,backendName:"webgpu",kernelFunc:Yre},Hw=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(${qw(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-06-02 16:39:53 +02:00
let end = ${jw(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-06-02 16:39:53 +02:00
${jw(e,"coords",this.op)} = idx;
val ${this.op}= getX(${qw(e,"coords",this.op)});
2022-04-21 15:39:40 +02:00
}
setOutputAtIndex(index, val);
}
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}};function qw(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 jw(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 q2(e,t,n,s,r,a){let o=t.shape.length,i=C.getAxesPermutation([s],o),u=t;i!=null&&(u=Ks({inputs:{x:t},backend:n,attrs:{perm:i}}));let l=C.getInnerMostAxes(1,o)[0];if(l!==o-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=Un({inputs:{x:u},backend:n});for(let d=0;d<=Math.ceil(Math.log2(c))-1;d++){let h=new Hw(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 Hw(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(i!=null){let d=C.getUndoAxesPermutation(i),h=Ks({inputs:{x:p},backend:n,attrs:{perm:d}});return n.disposeData(p.dataId),n.disposeData(u.dataId),h}return p}function Zre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:o,reverse:i}=s;return q2("*",r,n,a,o,i)}var Jre={kernelName:fi,backendName:"webgpu",kernelFunc:Zre};function eae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:o,reverse:i}=s;return q2("+",r,n,a,o,i)}var tae={kernelName:Da,backendName:"webgpu",kernelFunc:eae},nae=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-06-02 16:39:53 +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 sae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockSize:a,dataFormat:o}=s,i=r.shape[0],u=o==="NHWC"?r.shape[1]:r.shape[2],l=o==="NHWC"?r.shape[2]:r.shape[3],c=o==="NHWC"?r.shape[3]:r.shape[1],p=u*a,d=l*a,h=c/(a*a),f=o==="NHWC"?[i,p,d,h]:[i,h,p,d],m=[{type:"int32",data:[a]}],g=new nae(f,o);return n.runWebGPUProgram(g,[r],r.dtype,m)}var rae={kernelName:gi,backendName:"webgpu",kernelFunc:sae},j2=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=Io(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-06-02 16:39:53 +02:00
${Lv()}
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-06-02 16:39:53 +02:00
`}},K2=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-06-02 16:39:53 +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=Io(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-06-02 16:39:53 +02:00
${ac()}
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-06-02 16:39:53 +02:00
`}};function aae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:o,pad:i,dilations:u,dimRoundingMode:l}=s,c=u;c==null&&(c=[1,1]);let p=C.computeConv2DInfo(r.shape,a.shape,o,c,i,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 j2(p):(h=new K2(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 oae={kernelName:Fa,backendName:"webgpu",kernelFunc:aae},X2=gn({opSnippet:0,cpuKernelImpl:zse,supportsComplex:!0}),iae={kernelName:Za,backendName:"webgpu",kernelFunc:X2},uae=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-06-02 16:39:53 +02:00
`}};function uc(e,t,n,s,r){let a=e.shape.length,o=[],i=w.parseAxisParam(t,e.shape),u=i,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),o.push(c)),C.assertAxesAreInnerMostDims(s,u,a);let[p,d]=C.computeOutAndReduceShapes(c.shape,u),h=p;n&&(h=C.expandShapeToKeepDim(p,i));let f;if((s==="max"||s==="prod")&&r.shouldExecuteOnCPU([c])){let m=r.tensorMap.get(c.dataId).values;switch(s){case"max":let g=Fse(m,w.sizeFromShape(d),h,e.dtype);f=r.makeTensorInfo(h,e.dtype,g);break;case"prod":let{outVals:b,outShape:y,outDtype:v}=Bse(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 uae(y,s),I=r.runWebGPUProgram(k,[c],v,x);o.push(I),f=Le({inputs:{x:I},attrs:{shape:h},backend:r})}return o.forEach(m=>r.disposeData(m.dataId)),f}function Vv(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s;return uc(r,a,o,"sum",n)}var lae={kernelName:co,backendName:"webgpu",kernelFunc:Vv};function cae(e){let{inputs:t,backend:n,attrs:s}=e,{equation:r}=s,a=t,{allDims:o,summedDims:i,idDims:u}=C.decodeEinsumEquation(r,a.length);C.checkEinsumDimSizes(o.length,u,a);let{path:l,steps:c}=C.getEinsumComputePath(i,u),p=c.length,d=null,h=o.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=Le({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),d===null?d=v:(d=X2({inputs:{a:v,b:d},backend:n}),f.push(d))}m<p-1&&(l[m]>=0&&(d=Vv({inputs:{x:d},backend:n,attrs:{axis:l[m]-(o.length-h),keepDims:!1}}),f.push(d)),h--)}for(let m of f)m!==d&&n.disposeData(m.dataId);return d}var dae={kernelName:op,backendName:"webgpu",kernelFunc:cae},pae=Kt({opType:4}),hae={kernelName:Pa,backendName:"webgpu",kernelFunc:pae},fae=gn({opSnippet:4,dtype:"bool",cpuKernelImpl:Sse}),mae={kernelName:bi,backendName:"webgpu",kernelFunc:fae},Y2=Kt({opType:5,cpuKernelImpl:Ise,dtype:"float32"}),gae={kernelName:za,backendName:"webgpu",kernelFunc:Y2};function ug(e){let{inputs:t,attrs:n,backend:s}=e,{dim:r}=n,{input:a}=t,o=a.shape.length,i=a.shape.slice(),u=r;return r<0&&(w.assert(-(o+1)<=r,()=>`Axis must be in the interval [${-(o+1)}, ${o}]`),u=o+r+1),i.splice(u,0,1),Le({inputs:{x:a},backend:s,attrs:{shape:i}})}var bae={kernelName:yi,backendName:"webgpu",kernelFunc:ug},yae=Kt({opType:6,cpuKernelImpl:Cse}),vae={kernelName:vi,backendName:"webgpu",kernelFunc:yae},xae=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-06-02 16:39:53 +02:00
`}};function mu(e){let{backend:t,attrs:n}=e,{shape:s,value:r}=n,{dtype:a}=n;if(a=a||w.inferDtype(r),a==="string"){let o=w.getArrayFromDType(a,w.sizeFromShape(s));return o.fill(r),t.makeTensorInfo(s,a,o)}else{let o=new xae(s),i=[{type:"float32",data:[r]}];return t.runWebGPUProgram(o,[],a,i)}}var wae={kernelName:vl,backendName:"webgpu",kernelFunc:mu},kae=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-06-02 16:39:53 +02:00
`}},Sae={kernelName:xi,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,s=t,r=new kae(n.shape);return s.runWebGPUProgram(r,[n],n.dtype)}},Iae=Kt({opType:7,cpuKernelImpl:Nse}),Cae={kernelName:La,backendName:"webgpu",kernelFunc:Iae},Nae=gn({opSnippet:12,dtype:"int32"}),Tae={kernelName:Ma,backendName:"webgpu",kernelFunc:Nae},$ae=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-06-02 16:39:53 +02:00
`}},_ae={kernelName:xd,backendName:"webgpu",kernelFunc:Aae},Uo;function Aae(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 o=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,i=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]=o?[r.videoWidth,r.videoHeight]:[r.width,r.height],d=[p,c,a];if(K().getBool("WEBGPU_USE_IMPORT")&&o)return Kw({externalImage:r,backend:n,attrs:s,outShape:d,useImport:!0});if((o||i)&&(Uo==null&&(Uo=document.createElement("canvas").getContext("2d")),Uo.canvas.width=c,Uo.canvas.height=p,Uo.drawImage(r,0,0,c,p),r=Uo.canvas),l||u||o||i)return Kw({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 Kw(e){let{externalImage:t,backend:n,attrs:s,outShape:r,useImport:a}=e,{numChannels:o}=s,i=w.sizeFromShape(r),u=w.computeStrides(r),l=new $ae(r,a),c=[{type:"uint32",data:[i]},{type:"uint32",data:[o]},{type:"uint32",data:[...u]},{type:"uint32",data:[...l.dispatch]}];return n.runFromPixelsProgram(l,r,c,a,t)}var Eae=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-06-02 16:39:53 +02:00
`}},Rae={kernelName:Ba,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s,scale:r,offset:a,mean:o,variance:i}=e,{varianceEpsilon:u}=t,l=n,c=[s,o,i],p=null;a!=null&&(p=a.shape,c.push(a));let d=null;r!=null&&(d=r.shape,c.push(r));let h=new Eae(s.shape,o.shape,i.shape,p,d),f=[{type:"float32",data:[u]}];return l.runWebGPUProgram(h,c,s.dtype,f)}};function Dae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:o,preluActivationWeights:i}=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 H2({x:r,filter:a,convInfo:g,backend:n,bias:o,preluActivationWeights:i,leakyreluAlpha:f,activation:h})}var Fae={kernelName:ia,backendName:"webgpu",kernelFunc:Dae};function Oae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:o,preluActivationWeights:i}=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=o!=null,y=i!=null;b&&g.push(o),y&&g.push(i);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 j2(m,b,d,y):(x=new K2(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 Pae={kernelName:ua,backendName:"webgpu",kernelFunc:Oae},zae=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-06-02 16:39:53 +02:00
`}};function Lae(e){let{inputs:t,backend:n}=e,{params:s,indices:r}=t,a=r.shape,o=a[a.length-1],i=w.sizeFromShape(s.shape),[u,l,c,p]=C.prepareAndValidate(s,r),d=Le({inputs:{x:r},backend:n,attrs:{shape:[l,o]}}),h=Le({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=Tse(y,v,s.dtype,l,o,c,p,s.shape,i);return n.makeTensorInfo(u,s.dtype,x.values)}let f=new zae(o,[l,c]),m=[{type:"int32",data:[o]},{type:"int32",data:p}],g=n.runWebGPUProgram(f,[h,d],h.dtype,m),b=Le({inputs:{x:g},backend:n,attrs:{shape:u}});return n.disposeData(d.dataId),n.disposeData(h.dataId),n.disposeData(g.dataId),b}var Mae={kernelName:ki,backendName:"webgpu",kernelFunc:Lae},Bae=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=Vae(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-06-02 16:39:53 +02:00
`}};function Vae(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 Q2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,indices:a}=t,{axis:o,batchDims:i}=s,u=w.parseAxisParam(o,r.shape)[0],l=C.segment_util.collectGatherOpShapeInfo(r,a,u,i),c=w.sizeFromShape(a.shape),p=[],d=Le({inputs:{x:r},backend:n,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),h=Le({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=$se($,x,f);return p.forEach(E=>n.disposeData(E.dataId)),n.makeTensorInfo(l.outputShape,R.dtype,R.values)}let m=new Bae(d.shape,f),g=n.runWebGPUProgram(m,[d,h],d.dtype);p.push(g);let b=Le({inputs:{x:g},backend:n,attrs:{shape:l.outputShape}});return p.forEach(y=>n.disposeData(y.dataId)),b}var Wae={kernelName:wi,backendName:"webgpu",kernelFunc:Q2},Uae=gn({opSnippet:5,cpuKernelImpl:Ase,dtype:"bool"}),Gae={kernelName:Si,backendName:"webgpu",kernelFunc:Uae},Hae=gn({opSnippet:6,dtype:"bool",cpuKernelImpl:_se}),qae={kernelName:Va,backendName:"webgpu",kernelFunc:Hae};function jae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{alpha:a}=s,o=[{type:"float32",data:[a]}],i=new oc(r.shape,14);return i.uniforms="alpha : f32,",n.runWebGPUProgram(i,[r],"float32",o)}var Kae={kernelName:Ua,backendName:"webgpu",kernelFunc:jae},Xae=gn({opSnippet:7,dtype:"bool",cpuKernelImpl:Rse}),Yae={kernelName:Ii,backendName:"webgpu",kernelFunc:Xae},Qae=gn({opSnippet:8,dtype:"bool",cpuKernelImpl:Ese}),Zae={kernelName:Ci,backendName:"webgpu",kernelFunc:Qae},Jae=Kt({opType:9,cpuKernelImpl:Dse}),eoe={kernelName:Ga,backendName:"webgpu",kernelFunc:Jae},toe=gn({opSnippet:9,dtype:"bool"}),noe={kernelName:Ni,backendName:"webgpu",kernelFunc:toe},soe=Kt({opType:10}),roe={kernelName:Ti,backendName:"webgpu",kernelFunc:soe};function Z2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reductionIndices:a,keepDims:o}=s;return uc(r,a,o,"max",n)}var aoe={kernelName:Ha,backendName:"webgpu",kernelFunc:Z2},ooe=gn({opSnippet:15,cpuKernelImpl:Ose}),ioe={kernelName:qa,backendName:"webgpu",kernelFunc:ooe};function uoe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=s,l=1,c=C.computePool2DInfo(r.shape,a,o,l,i,u),p,d=[];if(c.filterHeight===1&&c.filterWidth===1){if(w.arraysEqual(c.inShape,c.outShape))return Un({inputs:{x:r},backend:n});p=new W2(c),d.push({type:"int32",data:[c.strideHeight,c.strideWidth]})}else p=new V2(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 loe={kernelName:ja,backendName:"webgpu",kernelFunc:uoe};function coe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{keepDims:a,axis:o}=s;return uc(r,o,a,"mean",n)}var doe={kernelName:Ka,backendName:"webgpu",kernelFunc:coe};function poe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s;return uc(r,a,o,"min",n)}var hoe={kernelName:Xa,backendName:"webgpu",kernelFunc:poe},foe=gn({opSnippet:16,cpuKernelImpl:Pse}),moe={kernelName:Ya,backendName:"webgpu",kernelFunc:foe},goe=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) {
2022-06-02 16:39:53 +02:00
let start = ${o}(${t});
let end = ${o}(${n});
2022-04-21 15:39:40 +02:00
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;
2022-06-02 16:39:53 +02:00
setOutputAtIndex(index, getX(${i}));
2022-04-21 15:39:40 +02:00
}
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}},boe={kernelName:Qa,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{paddings:r,mode:a}=t,o=n,i=r.map(c=>({type:"int32",data:[c[0],c[1]]})),u=new goe(s.shape,r,a);return o.runWebGPUProgram(u,[s],s.dtype,i)}};function yoe(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])){let a=n.tensorMap.get(s.dataId),[o,i]=Lse(a.values,s.shape,s.dtype);return n.makeTensorInfo(i,s.dtype,o)}let r=new oc(s.shape,11);return n.runWebGPUProgram(r,[s],s.dtype)}var voe={kernelName:$i,backendName:"webgpu",kernelFunc:yoe};function xoe(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:o,iouThreshold:i,scoreThreshold:u}=s,l=n.readSync(r.dataId),c=n.readSync(a.dataId),{selectedIndices:p}=ws.nonMaxSuppressionV3Impl(l,c,o,i,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var woe={kernelName:Ai,backendName:"webgpu",kernelFunc:xoe};function koe(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:o,iouThreshold:i,scoreThreshold:u,softNmsSigma:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),d=o,h=i,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 Soe={kernelName:Ei,backendName:"webgpu",kernelFunc:koe};function Kd(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="complex64"){let r=ic({inputs:{input:s},backend:n}),a=Kd({inputs:{x:r},backend:n}),o=ih({inputs:{input:s},backend:n}),i=Kd({inputs:{x:o},backend:n}),u=hu({inputs:{real:a,imag:i},backend:n});return n.disposeData(r.dataId),n.disposeData(a.dataId),n.disposeData(o.dataId),n.disposeData(i.dataId),u}else return mu({attrs:{shape:s.shape,dtype:s.dtype,value:s.dtype==="string"?"":0},backend:n})}var Ioe={kernelName:Xi,backendName:"webgpu",kernelFunc:Kd};function J2(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=J2({inputs:{x:r},backend:n}),o=ih({inputs:{input:s},backend:n}),i=Kd({inputs:{x:o},backend:n}),u=hu({inputs:{real:a,imag:i},backend:n});return n.disposeData(r.dataId),n.disposeData(a.dataId),n.disposeData(o.dataId),n.disposeData(i.dataId),u}else return mu({attrs:{shape:s.shape,dtype:s.dtype,value:1},backend:n})}var Coe={kernelName:Ri,backendName:"webgpu",kernelFunc:J2};function Noe(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s;if(t.length===1)return ug({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let a=t[0].shape,o=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(o===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let i=[],u=t.map(c=>{let p=ug({inputs:{input:c},backend:n,attrs:{dim:r}});return i.push(p),p}),l=G2({inputs:u,backend:n,attrs:{axis:r}});return i.forEach(c=>n.disposeData(c.dataId)),l}var Toe={kernelName:Fi,backendName:"webgpu",kernelFunc:Noe},$oe=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}`,o=e>1?"any(outC < start)":"outC < start",i=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);
2022-06-02 16:39:53 +02:00
if (${o} || ${i}) {
2022-04-21 15:39:40 +02:00
setOutputAtIndex(index, uniforms.constantValue);
} else {
let coords = outC - start;
setOutputAtIndex(index, getX(${u}));
}
}
2022-04-21 15:38:36 +02:00
}
2022-06-02 16:39:53 +02:00
`}},eN=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{paddings:a,constantValue:o}=s;if(a.every(l=>w.arraysEqual(l,[0,0])))return Un({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 mu({backend:n,attrs:{shape:l,value:o,dtype:r.dtype}})}let i=[{type:"float32",data:[o]}];a.map(l=>i.push({type:"int32",data:[l[0],l[1]]}));let u=new $oe(r.shape,a);return n.runWebGPUProgram(u,[r],r.dtype,i)},_oe={kernelName:Ja,backendName:"webgpu",kernelFunc:eN},Aoe=gn({opSnippet:13}),Eoe={kernelName:eo,backendName:"webgpu",kernelFunc:Aoe};function Roe(e){let{inputs:t,backend:n}=e,{x:s,alpha:r}=t,a=new M2(14,s.shape,r.shape);return n.runWebGPUProgram(a,[s,r],"float32")}var Doe={kernelName:to,backendName:"webgpu",kernelFunc:Roe};function Foe(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:o}=s;return uc(r,a,o,"prod",n)}var Ooe={kernelName:no,backendName:"webgpu",kernelFunc:Foe},Poe=e=>{let{backend:t,attrs:n}=e,{start:s,stop:r,step:a,dtype:o}=n,i=Vse(s,r,a,o);return t.makeTensorInfo([i.length],o,i)},zoe={kernelName:Tl,backendName:"webgpu",kernelFunc:Poe},tN=gn({opSnippet:3}),Loe={kernelName:Oa,backendName:"webgpu",kernelFunc:tN},Moe=Kt({opType:12}),Boe={kernelName:so,backendName:"webgpu",kernelFunc:Moe},Voe=Kt({opType:13}),Woe={kernelName:ao,backendName:"webgpu",kernelFunc:Voe},Uoe=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-06-02 16:39:53 +02:00
`}};function Goe(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,size:o,halfPixelCenters:i}=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:[i?.5:0]}],f=new Uoe(r.shape,u,l);return n.runWebGPUProgram(f,[r],"float32",h)}var Hoe={kernelName:ro,backendName:"webgpu",kernelFunc:Goe},qoe=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-06-02 16:39:53 +02:00
`}};function joe(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:o,size:i}=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:[a?.5:0]}],f=new qoe(r.shape,u,l,o);return n.runWebGPUProgram(f,[r],r.dtype,h)}var Koe={kernelName:_l,backendName:"webgpu",kernelFunc:joe},Xoe=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-06-02 16:39:53 +02:00
`}},Yoe={kernelName:Yi,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:s}=e,{radians:r,fillValue:a,center:o}=t,i=n,u=new Xoe(s.shape,a),[l,c]=C.getImageCenter(o,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}),i.runWebGPUProgram(u,[s],s.dtype,p)}},Qoe=Kt({opType:15,cpuKernelImpl:Wse}),Zoe={kernelName:oo,backendName:"webgpu",kernelFunc:Qoe},Joe=class{constructor(e,t,n,s,r,a,o){this.variableNames=["updates","indices"],this.workGroupSize=[64,1,1],this.atomic=!0,this.outputShape=a,this.type=o,this.dispatchLayout=Ve(e),this.dispatch=_e(this.dispatchLayout,e,this.workGroupSize),this.sliceDimGreaterThanOne=t>1,this.shaderKey=`scatter_${n}_${s}_${this.sliceDimGreaterThanOne}_${o}`;let i=Wt(r.length);this.uniforms=`sliceDim : i32, strides: ${i}, 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-06-02 16:39:53 +02:00
`);let o=`getUpdates(${s})`,i=this.type==="int32"?"atomicAdd(&(result[flatIndex]), i32(updateValue));":`
var oldValue = atomicLoad(&(result[flatIndex]));
var exchanged = false;
for (; !exchanged;) {
let newValueF32 = bitcast<f32>(oldValue) + updateValue;
let newValue = bitcast<i32>(newValueF32);
let res = atomicCompareExchangeWeak(&(result[flatIndex]), oldValue, newValue);
oldValue = res.old_value;
exchanged = res.exchanged;
2022-04-21 15:39:40 +02:00
}
`;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};
}
2022-06-02 16:39:53 +02:00
let updateValue = ${o};
2022-04-21 15:39:40 +02:00
let flatIndex = getOutputIndexFromCoords(${r});
2022-06-02 16:39:53 +02:00
${i}
2022-04-21 15:39:40 +02:00
}
2022-06-02 16:39:53 +02:00
}`}};function eie(e){let{inputs:t,backend:n,attrs:s}=e,{indices:r,updates:a}=t,{shape:o}=s,{sliceRank:i,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=C.calculateShapes(a,r,o),d=[p/l,l];if(p===0)return n.makeTensorInfo(o,r.dtype);let h=Le({inputs:{x:r},backend:n,attrs:{shape:[u,i]}}),f=Le({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=f.dtype,g=mu({backend:n,attrs:{shape:d,value:0,dtype:m}}),b=w.sizeFromShape(f.shape),y=[{type:"int32",data:[i]},{type:"int32",data:c},{type:"int32",data:[b]}],v=new Joe(f.shape,i,h.shape.length,f.shape.length,c,d,m),x=n.runWebGPUProgram(v,[f,h],m,y,g),k=Le({inputs:{x},backend:n,attrs:{shape:o}});return n.disposeData(h.dataId),n.disposeData(f.dataId),n.disposeData(x.dataId),k}var tie={kernelName:Li,backendName:"webgpu",kernelFunc:eie},nie=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 o=0;o<this.outputShape.length;o++)a.push(`${s[o]}`),o<this.cRank&&r.push(`${s[o]}`);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-06-02 16:39:53 +02:00
`}};function sie(e){let{inputs:t,backend:n}=e,{condition:s,t:r,e:a}=t,o=new nie(s.shape.length,r.shape,r.shape.length);return n.runWebGPUProgram(o,[s,r,a],cn(r.dtype,a.dtype))}var rie={kernelName:Mi,backendName:"webgpu",kernelFunc:sie},aie=Kt({opType:18}),oie={kernelName:uo,backendName:"webgpu",kernelFunc:aie},iie=Kt({opType:16}),uie={kernelName:io,backendName:"webgpu",kernelFunc:iie},lie=Kt({opType:17}),cie={kernelName:Vi,backendName:"webgpu",kernelFunc:lie},nN=gn({opSnippet:2,cpuKernelImpl:Kse,supportsComplex:!0}),die={kernelName:fo,backendName:"webgpu",kernelFunc:nN};function pie(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{dim:a}=s,o=w.parseAxisParam([a],r.shape),i=Z2({inputs:{x:r},backend:n,attrs:{reductionIndices:o,keepDims:!1}}),u=C.expandShapeToKeepDim(i.shape,o),l=Le({inputs:{x:i},backend:n,attrs:{shape:u}}),c=nN({inputs:{a:r,b:l},backend:n}),p=Y2({inputs:{x:c},backend:n}),d=Vv({inputs:{x:p},backend:n,attrs:{axis:o,keepDims:!1}}),h=Le({inputs:{x:d},backend:n,attrs:{shape:u}}),f=tN({inputs:{a:p,b:h},backend:n});return n.disposeData(i.dataId),n.disposeData(l.dataId),n.disposeData(c.dataId),n.disposeData(p.dataId),n.disposeData(d.dataId),n.disposeData(h.dataId),f}var hie={kernelName:po,backendName:"webgpu",kernelFunc:pie},fie=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,paddings:o}=s;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGPU backend not implemented yet");let i=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...o);for(let b=1+a.length;b<r.shape.length;++b)u.push([0,0]);let l=[],c=eN({inputs:{x:r},backend:n,attrs:{paddings:u,constantValue:0}}),p=C.getReshaped(c.shape,a,i,!1),d=C.getPermuted(p.length,a.length,!1),h=C.getReshapedPermuted(c.shape,a,i,!1),f=Le({inputs:{x:c},backend:n,attrs:{shape:p}}),m=Ks({inputs:{x:f},backend:n,attrs:{perm:d}}),g=Le({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},mie={kernelName:Wi,backendName:"webgpu",kernelFunc:fie},gie=class{constructor(e,t,n,s,r,a,o=!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 i=t>1;this.shaderKey=`scatter_${n}_${s}_${i}`;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=i?"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-06-02 16:39:53 +02:00
}`}};function bie(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:o}=t,{outputShape:i}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=C.calculateShapes(a,r,i),h=!1;if(a.dtype==="string"){let y=n.bufferSync(r),v=n.bufferSync(a),x=w.decodeString(n.readSync(o.dataId)[0]),k=Use(y,v,i,d,c,l,u,p,x,h);return n.makeTensorInfo(i,k.dtype,k.values)}let f=[{type:"int32",data:[l]},{type:"int32",data:[u]},{type:"int32",data:p}],m=new gie(l,u,r.shape.length,a.shape.length,p,[d,1],h),g=n.runWebGPUProgram(m,[a,r,o],a.dtype,f),b=Le({inputs:{x:g},backend:n,attrs:{shape:i}});return n.disposeData(g.dataId),b}var yie={kernelName:fp,backendName:"webgpu",kernelFunc:bie};function vie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:o}=s,i=w.parseAxisParam(o,r.shape)[0],u=C.prepareSplitSize(r,a,i),l=r.shape.length,c=new Array(l).fill(0),p=r.shape.slice();return u.map(d=>{let h=[...p];h[i]=d;let f=fu({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[i]+=d,f})}var xie={kernelName:Ui,backendName:"webgpu",kernelFunc:vie},wie=Kt({opType:19}),kie={kernelName:lo,backendName:"webgpu",kernelFunc:wie},Sie={kernelName:Fl,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,s=t,r=new oc(n.shape,20);return s.runWebGPUProgram(r,[n],n.dtype)}},Iie=gn({opSnippet:11}),Cie={kernelName:ho,backendName:"webgpu",kernelFunc:Iie},Nie=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-06-02 16:39:53 +02:00
`}};function Tie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:o,strides:i,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,o,i,u,l,c,p,d),k;if(m)k=Le({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),$=fu({inputs:{x:r},backend:n,attrs:{begin:y,size:I}});k=Le({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=qse(h,R,x,y);k=n.makeTensorInfo(f,r.dtype,E.values)}else{let $=new Nie(h),R=[{type:"int32",data:y},{type:"int32",data:x}],E=n.runWebGPUProgram($,[r],r.dtype,R);k=Le({inputs:{x:E},backend:n,attrs:{shape:f}}),n.disposeData(E.dataId)}return k}var $ie={kernelName:Gi,backendName:"webgpu",kernelFunc:Tie};function _ie(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.readSync(c.dataId),h=n.readSync(p.dataId),[f,m]=jse(d,h,r,a,o,i,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var Aie={kernelName:mp,backendName:"webgpu",kernelFunc:_ie},Eie=Kt({opType:21}),Rie={kernelName:mo,backendName:"webgpu",kernelFunc:Eie},Die=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=Fie(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-06-02 16:39:53 +02:00
`}};function Fie(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 Oie(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=Xse(c,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let o=new Die(r.shape,a);return n.runWebGPUProgram(o,[r],r.dtype)}var Pie={kernelName:Tr,backendName:"webgpu",kernelFunc:Oie},zie=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-06-02 16:39:53 +02:00
`}},Lie=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-06-02 16:39:53 +02:00
`}};function Go(e,t){t!==null&&e.disposeData(t.dataId)}function Xw(e){let t=1;for(;t<e;)t*=2;return t}function Mie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:o}=s,i=r.shape,u=i[i.length-1];if(n.shouldExecuteOnCPU([r])){let k=n.readSync(r.dataId),[I,$]=Yse(k,i,r.dtype,a,o);return[n.makeTensorInfo(I.shape,I.dtype,I.values),n.makeTensorInfo($.shape,$.dtype,$.values)]}if(a===0)return i[i.length-1]=0,[n.makeTensorInfo(i,r.dtype,[]),n.makeTensorInfo(i,"int32",[])];if(u===1)return[r,mu({attrs:{shape:i,dtype:"int32",value:0},backend:n})];let c=w.sizeFromShape(i)/u,p=Le({inputs:{x:r},attrs:{shape:[c,u]},backend:n}),d=Xw(a),h=Xw(u),f=null,m=()=>f===null?[p,p]:[p,f],g=(k,I,$)=>{let R=m(),E=new zie($),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]}],D=f;f=n.runWebGPUProgram(E,R,"int32",A),Go(n,D)};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 Lie([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),Go(n,P);let A=d/2,D=A*2;for(let T=A;T>=1;T/=2)g(D,T,f.shape)}let b=f;f=fu({inputs:{x:f},backend:n,attrs:{begin:0,size:[c,a]}}),Go(n,b);let y=Q2({inputs:{x:p,indices:f},backend:n,attrs:{axis:1,batchDims:1}});Go(n,p);let v=i.slice(0,-1);v.push(a),b=f,f=Le({inputs:{x:f},attrs:{shape:v},backend:n}),Go(n,b);let x=y;return y=Le({inputs:{x:y},attrs:{shape:v},backend:n}),Go(n,x),[y,f]}var Bie={kernelName:qi,backendName:"webgpu",kernelFunc:Mie},Vie=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-06-02 16:39:53 +02:00
`}};function Wie(e){let{inputs:t,backend:n,attrs:s}=e,{image:r,transforms:a}=t,{interpolation:o,fillMode:i,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 Vie(g),y=o==="nearest"?1:2,v;switch(i){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 Uie={kernelName:ji,backendName:"webgpu",kernelFunc:Wie};function Gie(e){let{inputs:t,backend:n,attrs:s}=e,{value:r}=t,{axis:a}=s;a<0&&(a+=r.shape.length);let o=r,i=o.shape.length,u=r.shape[a],l=new Array(i-1),c=0;for(let m=0;m<i;m++)m!==a&&(l[c++]=o.shape[m]);let p=[],d=new Array(i).fill(0),h=o.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){d[a]=m;let g=fu({inputs:{x:o},backend:n,attrs:{begin:d,size:h}}),b=Le({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,p.push(g)}return p.forEach(m=>n.disposeData(m.dataId)),f}var Hie={kernelName:Ki,backendName:"webgpu",kernelFunc:Gie},qie=[mse,Jse,tre,rre,cre,pre,fre,gre,wre,Cre,Tre,Ere,vse,Ore,Bre,Gre,qre,Kre,Qre,Jre,tae,rae,oae,dae,hae,mae,gae,bae,vae,wae,Sae,_ae,Cae,Tae,Rae,Fae,Pae,Mae,Wae,Gae,qae,yse,Dre,Kae,Yae,Zae,eoe,noe,roe,aoe,ioe,loe,doe,hoe,moe,boe,iae,voe,woe,Soe,kre,Coe,Toe,_oe,Eoe,Doe,Ooe,zoe,Sre,Loe,Boe,Woe,hse,Hoe,Koe,Yoe,Zoe,tie,rie,oie,uie,cie,vre,$ie,Aie,hie,mie,yie,xie,kie,Sie,Cie,die,lae,Rie,Pie,Bie,Uie,ure,Hie,Ioe];for(let e of qie)Ol(e);var jie=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=Yw(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=Yw(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 Yw(e,t){return`${e}_${t}`}var Kie=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=Zw(n),a=e*t*r,o=Qw(e,t,n,s);if(this.freeTextures.has(o)||this.freeTextures.set(o,[]),this.usedTextures.has(o)||this.usedTextures.set(o,[]),this.numBytesUsed+=a,this.numUsedTextures++,this.freeTextures.get(o).length>0){this.numFreeTextures--;let u=this.freeTextures.get(o).shift();return this.usedTextures.get(o).push(u),u}this.numBytesAllocated+=a;let i=this.device.createTexture({size:[e,t],format:n,usage:s});return this.usedTextures.get(o).push(i),i}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 o=this.usedTextures.get(a),i=o.indexOf(e);if(i<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-06-02 16:39:53 +02:00
${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=t.dataIdMap.get(s.dataId).id,i=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;ZN(o,i,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 Dde={kernelName:Dl,backendName:"wasm",setupFunc:Ede,kernelFunc:Rde},JN;function eT(e){JN=e.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function tT(e,t){let{backend:n,inputs:s}=e,{data:r,indices:a,segmentIds:o}=s,i=a.shape[0],u=n.readSync(o.dataId,i-1,i)[0],c=i>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(o.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;JN(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 Fde(e){return tT(e,!0)}var Ode={kernelName:pp,backendName:"wasm",setupFunc:eT,kernelFunc:Fde};function Pde(e){return tT(e,!1)}var zde={kernelName:hp,backendName:"wasm",setupFunc:eT,kernelFunc:Pde};function Lde(e){let{inputs:t,attrs:n,backend:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:o}=n,i=w.parseAxisParam(o,r.shape)[0],u=C.prepareSplitSize(r,a,i),l=new Array(r.shape.length).fill(0),c=r.shape.slice();return u.map(p=>{let d=[...c];d[i]=p;let h=xa({inputs:{x:r},attrs:{begin:l,size:d},backend:s});return l[i]+=p,h})}var Mde={kernelName:Ui,backendName:"wasm",kernelFunc:Lde},Bde=Ht(lo),Vde=Ht(Fl),Wde=!0,Ude=Xt(ho,Wde),nT;function Gde(e){nT=e.wasm.cwrap(go,null,["number","number","number","number"])}function Hde(e){let{backend:t,inputs:n,attrs:s}=e,{alpha:r}=s,{x:a}=n,o=t.dataIdMap.get(a.dataId).id,i=t.makeOutput(a.shape,a.dtype),u=t.dataIdMap.get(i.dataId).id;return nT(o,r,St[a.dtype],u),i}var qde={kernelName:go,backendName:"wasm",setupFunc:Gde,kernelFunc:Hde},sT;function jde(e){sT=e.wasm.cwrap(Gi,null,["number","array","number","array","array","array","array","array","number","number"])}function Kde(e){let{backend:t,inputs:n,attrs:s}=e,{x:r}=n,{begin:a,end:o,strides:i,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,o,i,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}});`,ype=wa(S$()),vpe=class extends il{constructor(e){super(),this.wasm=e,this.dataIdNextNumber=1,this.wasm.tfjs.initWithThreadsCount(uT),dg=this.wasm.tfjs.getThreadsCount(),this.dataIdMap=new Zd(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 o=w.sizeFromShape(n),i=o*w.bytesPerElement(s),u=this.wasm._malloc(i);this.dataIdMap.set(e,{id:a,memoryOffset:u,shape:n,dtype:s,refCount:r}),this.wasm.tfjs.registerTensor(a,o,u),t!=null&&this.wasm.HEAPU8.set(new Uint8Array(t.buffer,t.byteOffset,i),u)}async read(e){return this.readSync(e)}readSync(e,t,n){let{memoryOffset:s,dtype:r,shape:a,stringBytes:o}=this.dataIdMap.get(e);if(r==="string")return(t==null||t===0)&&(n==null||n>=o.length)?o:o.slice(t,n);t=t||0,n=n||w.sizeFromShape(a);let i=w.bytesPerElement(r),u=this.wasm.HEAPU8.slice(s+t*i,s+n*i);return kpe(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. */