human/dist/tfjs.esm.js

7220 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-04-01 15:13:32 +02:00
var LT=Object.create,Vd=Object.defineProperty,BT=Object.getOwnPropertyDescriptor,Vw=Object.getOwnPropertyNames,VT=Object.getPrototypeOf,WT=Object.prototype.hasOwnProperty,UT=e=>Vd(e,"__esModule",{value:!0}),zt=(e,t)=>function(){return t||(0,e[Vw(e)[0]])((t={exports:{}}).exports,t),t.exports},Ae=(e,t)=>{for(var n in t)Vd(e,n,{get:t[n],enumerable:!0})},GT=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Vw(t))!WT.call(e,r)&&(n||r!=="default")&&Vd(e,r,{get:()=>t[r],enumerable:!(s=BT(t,r))||s.enumerable});return e},xa=(e,t)=>GT(UT(Vd(e!=null?LT(VT(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),HT=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(F){}function s(F,$,z){this.low=F|0,this.high=$|0,this.unsigned=!!z}s.prototype.__isLong__,Object.defineProperty(s.prototype,"__isLong__",{value:!0});function r(F){return(F&&F.__isLong__)===!0}s.isLong=r;var a={},i={};function o(F,$){var z,W,q;return $?(F>>>=0,(q=0<=F&&F<256)&&(W=i[F],W)?W:(z=l(F,(F|0)<0?-1:0,!0),q&&(i[F]=z),z)):(F|=0,(q=-128<=F&&F<128)&&(W=a[F],W)?W:(z=l(F,F<0?-1:0,!1),q&&(a[F]=z),z))}s.fromInt=o;function u(F,$){if(isNaN(F))return $?x:v;if($){if(F<0)return x;if(F>=g)return A}else{if(F<=-b)return P;if(F+1>=b)return E}return F<0?u(-F,$).neg():l(F%m|0,F/m|0,$)}s.fromNumber=u;function l(F,$,z){return new s(F,$,z)}s.fromBits=l;var c=Math.pow;function p(F,$,z){if(F.length===0)throw Error("empty string");if(F==="NaN"||F==="Infinity"||F==="+Infinity"||F==="-Infinity")return v;if(typeof $=="number"?(z=$,$=!1):$=!!$,z=z||10,z<2||36<z)throw RangeError("radix");var W;if((W=F.indexOf("-"))>0)throw Error("interior hyphen");if(W===0)return p(F.substring(1),$,z).neg();for(var q=u(c(z,8)),K=v,Y=0;Y<F.length;Y+=8){var Z=Math.min(8,F.length-Y),te=parseInt(F.substring(Y,Y+Z),z);if(Z<8){var ee=u(c(z,Z));K=K.mul(ee).add(u(te))}else K=K.mul(q),K=K.add(u(te))}return K.unsigned=$,K}s.fromString=p;function d(F,$){return typeof F=="number"?u(F,$):typeof F=="string"?p(F,$):l(F.low,F.high,typeof $=="boolean"?$:F.unsigned)}s.fromValue=d;var h=1<<16,f=1<<24,m=h*h,g=m*m,b=g/2,y=o(f),v=o(0);s.ZERO=v;var x=o(0,!0);s.UZERO=x;var k=o(1);s.ONE=k;var T=o(1,!0);s.UONE=T;var N=o(-1);s.NEG_ONE=N;var E=l(-1,2147483647,!1);s.MAX_VALUE=E;var A=l(-1,-1,!0);s.MAX_UNSIGNED_VALUE=A;var P=l(0,-2147483648,!1);s.MIN_VALUE=P;var R=s.prototype;R.toInt=function(){return this.unsigned?this.low>>>0:this.low},R.toNumber=function(){return this.unsigned?(this.high>>>0)*m+(this.low>>>0):this.high*m+(this.low>>>0)},R.toString=function($){if($=$||10,$<2||36<$)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative())if(this.eq(P)){var z=u($),W=this.div(z),q=W.mul(z).sub(this);return W.toString($)+q.toInt().toString($)}else return"-"+this.neg().toString($);for(var K=u(c($,6),this.unsigned),Y=this,Z="";;){var te=Y.div(K),ee=Y.sub(te.mul(K)).toInt()>>>0,se=ee.toString($);if(Y=te,Y.isZero())return se+Z;for(;se.length<6;)se="0"+se;Z=""+se+Z}},R.getHighBits=function(){return this.high},R.getHighBitsUnsigned=function(){return this.high>>>0},R.getLowBits=function(){return this.low},R.getLow
`),Z=C=>W.writeSync(2,C+`
`));var te=d.print||Y,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 ne(C){ne.shown||(ne.shown={}),ne.shown[C]||(ne.shown[C]=1,ee(C))}function oe(C,D){if(typeof WebAssembly.Function=="function"){for(var B={i:"i32",j:"i64",f:"f32",d:"f64"},Q={parameters:[],results:D[0]=="v"?[]:[B[D[0]]]},ue=1;ue<D.length;++ue)Q.parameters.push(B[D[ue]]);return new WebAssembly.Function(Q,C)}var pe=[1,0,1,96],ye=D.slice(0,1),Te=D.slice(1),bt={i:127,j:126,f:125,d:124};pe.push(Te.length);for(var ue=0;ue<Te.length;++ue)pe.push(bt[Te[ue]]);ye=="v"?pe.push(0):pe=pe.concat([1,bt[ye]]),pe[1]=pe.length-2;var us=new Uint8Array([0,97,115,109,1,0,0,0].concat(pe,[2,7,1,1,101,1,102,0,0,7,5,1,1,102,0,0])),ls=new WebAssembly.Module(us),Bc=new WebAssembly.Instance(ls,{e:{f:C}}),ku=Bc.exports.f;return ku}var re=[],le;function me(){if(re.length)return re.pop();try{Fn.grow(1)}catch(C){throw C instanceof RangeError?"Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.":C}return Fn.length-1}function ke(C,D){for(var B=C;B<C+D;B++){var Q=$i(B);Q&&le.set(Q,B)}}var Se=0,Ee=C=>{Se=C},Pe=Atomics.load,Xe=Atomics.store,Je=Atomics.compareExchange,Ye;d.wasmBinary&&(Ye=d.wasmBinary);var tt=d.noExitRuntime||!0;typeof WebAssembly!="object"&&Ci("no native wasm support detected");var Ce,ut,at=!1,Jt;function Nt(C,D){C||Ci(D)}function Cn(C){var D=d["_"+C];return D}function Et(C,D,B,Q,ue){var pe={string:function($n){var Oi=0;if($n!=null&&$n!==0){var ex=($n.length<<2)+1;Oi=Fi(ex),Ls($n,Oi,ex)}return Oi},array:function($n){var Oi=Fi($n.length);return Bs($n,Oi),Oi}};function ye($n){return D==="string"?tn($n):D==="boolean"?Boolean($n):$n}var Te=Cn(C),bt=[],us=0;if(Q)for(var ls=0;ls<Q.length;ls++){var Bc=pe[B[ls]];Bc?(us===0&&(us=Ef()),bt[ls]=Bc(Q[ls])):bt[ls]=Q[ls]}var ku=Te.apply(null,bt);function MT($n){return us!==0&&Pc(us),ye($n)}return ku=MT(ku),ku}function en(C,D,B,Q){B=B||[];var ue=B.every(function(ye){return ye==="number"}),pe=D!=="string";return pe&&ue&&!Q?Cn(C):function(){return Et(C,D,B,arguments,Q)}}var Nn=1;function Tn(C){var D=new TextDecoder(C);this.decode=B=>(B.buffer instanceof SharedArrayBuffer&&(B=new Uint8Array(B)),D.decode.call(D,B))}var Yt=typeof TextDecoder!="undefined"?new Tn("utf8"):void 0;function Dn(C,D,B){for(var Q=D+B,ue=D;C[ue]&&!(ue>=Q);)++ue;if(ue-D>16&&C.subarray&&Yt)return Yt.decode(C.subarray(D,ue));for(var pe="";D<ue;){var ye=C[D++];if(!(ye&128)){pe+=String.fromCharCode(ye);continue}var Te=C[D++]&63;if((ye&224)==192){pe+=String.fromCharCode((ye&31)<<6|Te);continue}var bt=C[D++]&63;if((ye&240)==224?ye=(ye&15)<<12|Te<<6|bt:ye=(ye&7)<<18|Te<<12|bt<<6|C[D++]&63,ye<65536)pe+=String.fromCharCode(ye);else{var us=ye-65536;pe+=String.fromCharCode(55296|us>>10,56320|us&1023)}}return pe}function tn(C,D){return C?Dn(i(),C,D):""}function Ms(C,D,B,Q){if(!(Q>0))return 0;for(var ue=B,pe=B+Q-1,ye=0;ye<C.length;++ye){var Te=C.charCodeAt(ye);if(Te>=55296&&Te<=57343){var bt=C.charCodeAt(++ye);Te=65536+((Te&1023)<<10)|bt&1023}if(Te<=127){if(B>=pe)break;D[B++]=Te}else if(Te<=2047){if(B+1>=pe)break;D[B++]=192|Te>>6,D[B++]=128|Te&63}else if(Te<=65535){if(B+2>=pe)break;D[B++]=224|Te>>12,D[B++]=128|Te>>6&63,D[B++]=128|Te&63}else{if(B+3>=pe)break;D[B++]=240|Te>>18,D[B++]=128|Te>>12&63,D[B++]=128|Te>>6&63,D[B++]=128|Te&63}}return D[B]=0,B-ue}function Ls(C,D,B){return Ms(C,i(),D,B)}function ki(C){for(var D=0,B=0;B<C.length;++B){var Q=C.charCodeAt(B);Q>=55296&&Q<=57343&&(Q=65536+((Q&1023)<<10)|C.charCodeAt(++B)&1023),Q<=127?++D:Q<=2047?D+=2:Q<=65535?D+=3:D+=4}return D}var Js=typeof TextDecoder!="undefined"?new Tn("utf-16le"):void 0;function Bs(C,D){a().set(C,D)}function du(C,D,B){for(var Q=0;Q<C.length;++Q)a()[D++>>0]=C.charCodeAt(Q);B||(a()[D>>0]=0)}function Ii(C,D){return C%D>0&&(C+=D-C%D),C}var nn,ic,oc,pu,uc,lc,Fv,cc,dc;N&&(nn=d.buffer);function rs(C){nn=C,d.HEAP8=ic=new Int8Array(C),d.HEAP16=pu=new Int16Array(C),d.HEAP32=lc=new Int32Array(C),d.HEAPU8=oc=new Uint8Array(C),d.HEAPU16=uc=new Uint16Array(C),d.HEAPU32=Fv=new Uint32Array(C),d.HEAPF32=cc=new Float32Array(C),d.HEAPF64=dc=
`)),u.join(`
`)}function K$(e,t,n,s){let r=pt(t),a=s[s.length-1],i=new Array(a).fill(0),o=t.length,u=n==="complex64"?$u(e):e;if(o>1)for(let l=0;l<r/a;l++){let c=l*a;for(let p=0;p<a;p++)i[p]=Math.max(i[p],Tu(u[c+p],0,n).length)}return i}function Tu(e,t,n){let s;return Array.isArray(e)?s=`${parseFloat(e[0].toFixed(Ff))} + ${parseFloat(e[1].toFixed(Ff))}j`:ir(e)?s=`'${e}'`:n==="bool"?s=sk(e):s=parseFloat(e.toFixed(Ff)).toString(),Pu(s,t)}function sk(e){return e===0?"false":"true"}function ed(e,t,n,s,r,a=!0){let i=n==="complex64"?2:1,o=t[0],u=t.length;if(u===0){if(n==="complex64"){let m=$u(e);return[Tu(m[0],0,n)]}return n==="bool"?[sk(e[0])]:[e[0].toString()]}if(u===1){if(o>ax){let g=Iu*i,b=Array.from(e.slice(0,g)),y=Array.from(e.slice((o-Iu)*i,o*i));return n==="complex64"&&(b=$u(b),y=$u(y)),["["+b.map((v,x)=>Tu(v,r[x],n)).join(", ")+", ..., "+y.map((v,x)=>Tu(v,r[o-Iu+x],n)).join(", ")+"]"]}let m=n==="complex64"?$u(e):Array.from(e);return["["+m.map((g,b)=>Tu(g,r[b],n)).join(", ")+"]"]}let l=t.slice(1),c=s.slice(1),p=s[0]*i,d=[];if(o>ax){for(let m=0;m<Iu;m++){let g=m*p,b=g+p;d.push(...ed(e.slice(g,b),l,n,c,r,!1))}d.push("...");for(let m=o-Iu;m<o;m++){let g=m*p,b=g+p;d.push(...ed(e.slice(g,b),l,n,c,r,m===o-1))}}else for(let m=0;m<o;m++){let g=m*p,b=g+p;d.push(...ed(e.slice(g,b),l,n,c,r,m===o-1))}let h=u===2?",":"";d[0]="["+d[0]+h;for(let m=1;m<d.length-1;m++)d[m]=" "+d[m]+h;let f=`,
`;for(let m=2;m<u;m++)f+=`
`;return d[d.length-1]=" "+d[d.length-1]+"]"+(a?"":f),d}function $u(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=pt(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||qw(t,this.size),this.strides=ro(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,Wi=null,X$=null;function Y$(e){cs=e}function Q$(e){Wi=e}function Z$(e){X$=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=pt(e),this.strides=ro(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 Wi.buffer(this.shape,this.dtype,e)}bufferSync(){return Wi.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return Hi(this.shape,e,this.dtype==="complex64")}arraySync(){return Hi(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=>md(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=>md(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 Wi.print(this,e)}clone(){return this.throwIfDisposed(),Wi.clone(this)}toString(e=!1){let t=this.dataSync();return j$(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Wi.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 J$(){return ag("Tensor",()=>et)}J$();var gd=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(!kr(e.shape,this.shape))throw new Error(`shape of the new value (${e.shape}) and previous value (${this.shape}) must match`);cs().disposeTensor(this),this.dataId=e.dataId,cs().incRef(this,null)}dispose(){cs().disposeVariable(this),this.isDisposedInternal=!0}};Ob
Manifest JSON has weights with names: ${o.join(", ")}.`)}let u=r.reduce((h,f,m)=>(f&&h.push(m),h),[]),l=[];u.forEach(h=>{t[h].paths.forEach(f=>{let m=n+(n.endsWith("/")?"":"/")+f;l.push(m)})});let c=await e(l),p={},d=0;return u.forEach(h=>{let f=t[h].paths.length,m=0;for(let x=0;x<f;x++)m+=c[d+x].byteLength;let g=new ArrayBuffer(m),b=new Uint8Array(g),y=0;for(let x=0;x<f;x++){let k=new Uint8Array(c[d+x]);b.set(k,y),y+=k.byteLength}a[h].forEach(x=>{let k=g.slice(x.groupOffset,x.groupOffset+x.sizeBytes),T=hk(k,[x.manifestEntry]);for(let N in T)p[N]=T[N]}),d+=f}),p}}var J_="application/octet-stream",eA="application/json",zg=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=X().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=fk(e,n);t.body.append("model.json",new Blob([JSON.stringify(s)],{type:eA}),"model.json"),e.weightData!=null&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:J_}),"model.weights.bin");let r=await this.fetch(this.path,t);if(r.ok)return{modelArtifactsInfo:Dl(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 Pg(t,r=>this.loadWeights(r))}async loadWeights(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,s]=tA(t),r=this.weightPathPrefix||n,a=[];for(let l of e)a.push(...l.weights);let i=[],o=[];for(let l of e)for(let c of l.paths)this.weightUrlConverter!=null?o.push(this.weightUrlConverter(c)):i.push(r+c+s);this.weightUrlConverter&&i.push(...await Promise.all(o));let u=await kk(i,{requestInit:this.requestInit,fetchFunc:this.fetch,onProgress:this.onProgress});return[a,Og(u)]}};zg.URL_SCHEME_REGEX=/^https?:\/\//;function tA(e){let t=e.lastIndexOf("/"),n=e.lastIndexOf("?"),s=e.substring(0,t),r=n>t?e.substring(n):"";return[s+"/",r]}function um(e){return e.match(zg.URL_SCHEME_REGEX)!=null}var Sk=(e,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let n=!0;if(Array.isArray(e)?n=e.every(s=>um(s)):n=um(e),n)return Mg(e,t)}return null};xt.registerSaveRouter(Sk);xt.registerLoadRouter(Sk);function Mg(e,t){return new zg(e,t)}function nA(e,t){return Mg(e,t)}var zf=class{
Actual: ${r}.
Expected: ${a}.`);for(let i=0;i<a.length;++i){let o=r[i],u=a[i];if(!n(o,u))throw new Error(`Arrays differ: actual[${i}] = ${o}, expected[${i}] = ${u}.
Actual: ${r}.
Expected: ${a}.`)}}function RA(e,t){e().then(()=>t.fail(),()=>t())}function DA(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return ir(e)||ir(e[0])||ir(t)||ir(t[0])?cm(e,n,(s,r)=>s==r):cm(e,t,(s,r)=>Wg(s,r,0))}function FA(e,t,n){if(n==null&&(n=Vg()),!Wg(e,t,n))throw new Error(`Numbers differ: actual === ${e}, expected === ${t}`)}function Wg(e,t,n){return!isFinite(e)&&!isFinite(t)?!0:!(isNaN(e)||isNaN(t)||Math.abs(e-t)>n)}function OA(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 PA(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 Uk(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?Uk(n):e[t]=Rl(n)}return e}var Ede="0.0.0";function Rde(){X().set("PROD",!0)}function Dde(){X().set("DEBUG",!0)}function Fde(){X().set("DEPRECATION_WARNINGS_ENABLED",!1),console.warn("TensorFlow.js deprecation warnings have been disabled.")}function Gk(e){X().getBool("DEPRECATION_WARNINGS_ENABLED")&&console.warn(e+" You can disable deprecation warnings with tf.disableDeprecationWarnings().")}Z$(Gk);function Ode(){M.disposeVariables()}function Ss(){return M}function dm(){return M.memory()}function Pde(e){return M.profile(e)}function j(e,t){return M.tidy(e,t)}function Re(e){Rg(e).forEach(n=>n.dispose())}function Ht(e){return M.keep(e)}function zde(e){return M.time(e)}function Mde(e){return M.setBackend(e)}function Lde(){return M.ready()}function Bde(){return M.backendName}function Vde(e){M.removeBackend(e)}function Wde(e){return M.findBackend(e)}function Ude(e){return M.findBackendFactory(e)}function pp(e,t,n=1){return M.registerBackend(e,t,n)}function zA(){return M.backend}function Gde(e,t){X().setPlatform(e,t)}function MA(e,t){let n=_(e,"a","add"),s=_(t,"b","add");[n,s]=vt(n,s);let r={a:n,b:s};return M.runKernel(Ir,r)}var ie=L({add_:MA});function LA(e,t){let n=_(e,"a","floorDiv"),s=_(t,"b","floorDiv");[n,s]=vt(n,s);let r={a:n,b:s};return M.runKernel(za,r)}var Hk=L({floorDiv_:LA});function BA(e,t){let n=_(e,"a","div"),s=_(t,"b","div");if([n,s]=vt(n,s),n.dtype==="int32"&&s.dtype==="int32")return Hk(n,s);let r={a:n,b:s},a={};return M.runKernel(Da,r,a)}var xe=L({div_:BA});function VA(e,t){let n=_(e,"a","mul"),s=_(t,"b","mul");[n,s]=vt(n,s);let r={a:n,b:s};return M.runKernel(Ya,r)}var V=L({mul_:VA});function WA(e){let t=_(e,"x","abs");if(t.dtype==="complex64"){let n={x:t};return M.runKernel(Kd,n)}else{let n={x:t};return M.runKernel(ao,n)}}var Mt=L({abs_:WA});function UA(e){let n={x:_(e,"x","acos")};return M.runKernel(nl,n)}var GA=L({acos_:UA});function HA(e){let n={x:_(e,"x","acosh")};return M.runKernel(sl,n)}var qA=L({acosh_:HA});function jA(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(!kr(r.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});let s=t;return M.runKernel(ka,s)}var KA=L({addN_:jA});function XA(e,t=null,n=!1){let r={x:_(e,"x","all","bool")},a={axis:t,keepDims:n};return M.runKernel(rl,r,a)}var qk=L({all_:XA});function YA(e,t=null,n=!1){let r={x:_(e,"x","any","bool")},a={axis:t,keepDims:n};return M.runKernel(al,r,a)}var pm=L({any_:YA});function QA(e,t=0){let s={x:_(e,"x","argMax")},r={axis:t};return M.runKernel(Ia,s,r)}var Gu=L({argMax_:QA});function ZA(e,t=0){let s={x:_(e,"x","argMin")},r={axis:t};return M.runKernel(il,s,r)}var JA=L({argMin_:ZA});function eE(e){let n={x:_(e,"x","asin")};return M.runKernel(ol,n)}var tE=L({asin_:eE});function nE(e){let n={x:_(e,"x","asinh")};return M.runKernel(ul,n)}var sE=L({asinh_
with dtype ${a.dtype}. `)}),n.length===1)return lr(n[0]);let s=n,r={axis:t};return M.runKernel(oo,s,r)}var Ft=L({concat_:vE});function xE(e){let n={x:_(e,"x","sigmoid","float32")};return M.runKernel(ai,n)}var qs=L({sigmoid_:xE});function wE(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 M.runKernel(Oo,r,a)}var He=L({slice_:wE});function kE(e){let n={x:_(e,"x","tanh","float32")};return M.runKernel(di,n)}var Hu=L({tanh_:kE});function IE(e,t,n,s,r,a){let i=_(e,"forgetBias","basicLSTMCell"),o=_(t,"lstmKernel","basicLSTMCell"),u=_(n,"lstmBias","basicLSTMCell"),l=_(s,"data","basicLSTMCell"),c=_(r,"c","basicLSTMCell"),p=_(a,"h","basicLSTMCell"),d=Ft([l,p],1),h=We(d,o),f=ie(h,u),m=f.shape[0],g=f.shape[1]/4,b=[m,g],y=He(f,[0,0],b),v=He(f,[0,g],b),x=He(f,[0,g*2],b),k=He(f,[0,g*3],b),T=ie(V(qs(y),Hu(v)),V(c,qs(ie(i,x)))),N=V(Hu(T),qs(k));return[T,N]}var Hde=L({basicLSTMCell_:IE});function SE(e,t,n){let s=_(e,"x","batchToSpaceND"),r=t.reduce((o,u)=>o*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},i={blockShape:t,crops:n};return M.runKernel(io,a,i)}var Hg=L({batchToSpaceND_:SE});function CE(e){let t;return e.rank===0||e.rank===1?t=G(e,[1,1,1,e.size]):e.rank===2?t=G(e,[1,1,e.shape[0],e.shape[1]]):e.rank===3?t=G(e,[1,e.shape[0],e.shape[1],e.shape[2]]):t=e,t}function NE(e,t,n,s,r,a){a==null&&(a=.001);let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;s!=null&&(c=_(s,"offset","batchNorm")),O(o.rank===u.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),O(c==null||o.rank===c.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),O(l==null||o.rank===l.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let d={x:CE(i),scale:l,offset:c,mean:o,variance:u},h={varianceEpsilon:a},f=M.runKernel(Ma,d,h);return G(f,i.shape)}var qu=L({batchNorm_:NE});function TE(e,t,n,s,r,a){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;return s!=null&&(c=_(s,"offset","batchNorm")),O(i.rank===2,()=>`Error in batchNorm2D: x must be rank 2 but got rank ${i.rank}.`),O(o.rank===2||o.rank===1,()=>`Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank ${o.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}.`),qu(i,o,u,c,l,a)}var $E=L({batchNorm2d_:TE});function _E(e,t,n,s,r,a){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNorm"));let c;return s!=null&&(c=_(s,"offset","batchNorm")),O(i.rank===3,()=>`Error in batchNorm3D: x must be rank 3 but got rank ${i.rank}.`),O(o.rank===3||o.rank===1,()=>`Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank ${o.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}.`),qu(i,o,u,c,l,a)}var AE=L({batchNorm3d_:_E});function EE(e,t,n,s,r,a){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),l;r!=null&&(l=_(r,"scale","batchNo
${r} and ${t} for depthToSpace with input shape
${s.shape}`),O(a*t>=0,()=>`Negative dimension size caused by overflow when multiplying
${a} and ${t} for depthToSpace with input shape
${s.shape}`),O(i%(t*t)===0,()=>`Dimension size must be evenly divisible by ${t*t} but is ${i} for depthToSpace with input shape ${s.shape}`);let o={x:s},u={blockSize:t,dataFormat:n};return M.runKernel(co,o,u)}var cR=L({depthToSpace_:lR});function dR(e,t,n,s,r="NHWC",a=[1,1],i){let o=_(e,"x","depthwiseConv2d","float32"),u=_(t,"filter","depthwiseConv2d","float32"),l=o,c=!1;o.rank===3&&(c=!0,l=G(o,[1,o.shape[0],o.shape[1],o.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]}.`),hn("depthwiseConv2d",s,i);let p={x:l,filter:u},d={strides:n,pad:s,dataFormat:r,dilations:a,dimRoundingMode:i},h=M.runKernel(Ra,p,d);return c?G(h,[h.shape[1],h.shape[2],h.shape[3]]):h}var hp=L({depthwiseConv2d_:dR});function pR(e){let n={x:_(e,"x","diag")};return M.runKernel(gg,n)}var qde=L({diag_:pR});function hR(e,t,n,s,r=[1,1],a="NHWC"){let i=_(e,"x","dilation2d"),o=_(t,"filter","dilation2d");O(i.rank===3||i.rank===4,()=>`Error in dilation2d: input must be rank 3 or 4, but got rank ${i.rank}.`),O(o.rank===3,()=>`Error in dilation2d: filter must be rank 3, but got rank ${o.rank}.`),O(a==="NHWC",()=>`Error in dilation2d: Only NHWC is currently supported, but got dataFormat of ${a}`);let u=i,l=!1;i.rank===3&&(u=G(i,[1,i.shape[0],i.shape[1],i.shape[2]]),l=!0);let c={x:u,filter:o},p={strides:n,pad:s,dilations:r},d=M.runKernel(Yd,c,p);return l?G(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var fR=L({dilation2d_:hR});function mR(e,t){let n=_(e,"a","equal","string_or_numeric"),s=_(t,"b","equal","string_or_numeric");[n,s]=vt(n,s),ot(n.shape,s.shape);let r={a:n,b:s};return M.runKernel(po,r)}var Kn=L({equal_:mR});function gR(e,t,n){let s=_(t,"a","where"),r=_(n,"b","where"),a=_(e,"condition","where","bool"),i=ot(ot(a.shape,s.shape),r.shape),o=nd(a,i),u=nd(s,i),l=nd(r,i),c={condition:o,t:u,e:l};return M.runKernel(Fo,c)}var vn=L({where_:gR});function bR(e){let n={x:_(e,"x","zerosLike")};return M.runKernel(Go,n)}var je=L({zerosLike_:bR});function yR(e,t){let n=_(e,"a","div"),s=_(t,"b","div");[n,s]=vt(n,s);let r=xe(n,s),a=je(r),i=Kn(s,a);return vn(i,a,r)}var vR=L({divNoNan_:yR});function xR(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 i=G(n,[1,-1]),o=G(s,[-1,1]),u=We(i,o);return G(u,[])}else if(n.rank===1&&s.rank===2){let i=G(n,[1,-1]),o=G(s,[s.shape[0],s.shape[1]]),u=We(i,o);return G(u,[u.size])}else if(n.rank===2&&s.rank===1){let i=G(s,[-1,1]),o=We(n,i);return G(o,[o.size])}else{let i=G(s,[s.shape[0],s.shape[1]]);return We(n,i)}}var jde=L({dot_:xR});function wR(e,...t){let n=t.map((r,a)=>_(r,`tensors${a}`,"einsum")),s={equation:e};return M.runKernel(Qd,n,s)}var kR=L({einsum_:wR});function IR(e){let n={x:_(e,"x","elu","float32")};return M.runKernel(Fa,n)}var fp=L({elu_:IR});function SR(e){let t=_(e,"x","erf");O(t.dtype==="int32"||t.dtype==="float32",()=>"Input dtype must be `int32` or `float32`."),t.dtype==="int32"&&(t=ce(t,"float32"));let n={x:t};return M.runKernel(hl,n)}var CR=L({erf_:SR});function NR(e){let n={x:_(e,"x","exp")};return M.runKernel(Oa,n)}var Xn=L({exp_:NR});function TR(e,t=0){let n=_(e,"x","expandDims","string_or_numeric");O(t<=n.rank,()=>"Axis must be <= rank of the tensor");let s={input:n},r={dim:t};return M.runKernel(ho,s,r)}var Pn=L({expandDims_:TR});function $R(e){let n={x:_(e,"x","expm1")};return M.runKernel(fo,n)}var _R=L({expm1_:$R});function AR(e,t){let n=_(e,"x","tile","string_or_numeric");O(n.rank===t.length,()=>`Error in transpose: rank of input ${n.rank} must match length of reps ${t}.`);let s={x:n},r={re
rank ${a.rank}.`),O(Yi(t),()=>`Error in localResponseNormalization: depthRadius must be an integer but got depthRadius ${t}.`);let i=a,o=!1;a.rank===3&&(o=!0,i=G(a,[1,a.shape[0],a.shape[1],a.shape[2]]));let u={x:i},l={depthRadius:t,bias:n,alpha:s,beta:r},c=M.runKernel(ep,u,l);return o?G(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var qR=L({localResponseNormalization_:HR});function jR(e){let n={x:_(e,"x","log","float32")};return M.runKernel(Wa,n)}var Yn=L({log_:jR});function KR(e){let n={x:_(e,"x","log1p")};return M.runKernel(yl,n)}var Yg=L({log1p_:KR});function Yde(e){return O(hr(e),()=>"The f passed in grad(f) must be a function"),(t,n)=>{let s=_(t,"x","tf.grad","string_or_numeric"),r=n!=null?_(n,"dy","tf.grad"):null;return M.tidy(()=>{let{value:a,grads:i}=M.gradients(()=>e(s),[s],r);return r!=null&&pn(a.shape,r.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),gp(i),i[0]})}}function Qde(e){return O(hr(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=Uu(t,"args","tf.grads","string_or_numeric"),r=n!=null?_(n,"dy","tf.grads"):null;return M.tidy(()=>{let{value:a,grads:i}=M.gradients(()=>e(...s),s,r);return r!=null&&pn(a.shape,r.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),gp(i),i})}}function Zde(e){return O(hr(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}=M.gradients(()=>e(t),[t],n);return gp(s),{grad:s[0],value:r}}}function Jde(e){return O(hr(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=M.gradients(()=>e(...t),t,n);return n!=null&&pn(s.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),gp(s.grads),s}}function XR(e,t){O(hr(e),()=>"The f passed in variableGrads(f) must be a function"),O(t==null||Array.isArray(t)&&t.every(l=>l instanceof gd),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let n=t!=null;if(!n){t=[];for(let l in M.registeredVariables)t.push(M.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:i,grads:o}=M.gradients(e,t,null,a);O(o.some(l=>l!=null),()=>"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),O(i.rank===0,()=>`The f passed in variableGrads(f) must return a scalar, but it returned a rank-${i.rank} tensor`);let u={};return t.forEach((l,c)=>{o[c]!=null&&(u[l.name]=o[c])}),s!=null&&s.forEach(l=>u[l.name]=null),{value:i,grads:u}}function js(e){return M.customGrad(e)}function gp(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 YR(e){let n={x:_(e,"x","neg")};return M.runKernel(ko,n)}var kt=L({neg_:YR});function QR(e){let n={x:_(e,"x","softplus")};return M.runKernel(Tl,n)}var Pl=L({softplus_:QR});function ZR(e){let t=_(e,"x","logSigmoid");return js(s=>({value:kt(Pl(kt(s))),gradFunc:i=>V(i,qs(kt(s)))}))(t)}var epe=L({logSigmoid_:ZR});function JR(e,t=null,n=!1){let r={x:_(e,"x","max")},a={reductionIndices:t,keepDims:n};return M.runKernel(Ua,r,a)}var As=L({max_:JR});function eD(e,t){let n=_(e,"a","sub"),s=_(t,"b","sub");[n,s]=vt(n,s);let r={a:n,b:s};return M.runKernel(ci,r)}var ge=L({sub_:eD});function tD(e,t=null,n=!1){let s=_(e,"x","sum");s.dtype==="bool"&&(s=ce(s,"int32"));let r={x:s},a={axis:t,keepDims:n};return M.runKernel(oi,r,a)}var ve=L({sum_:tD});function nD(e,t=-1){let n=_(e,"logits","logSoftmax");if(t===-1&&(t=n.rank-1),t!==n.rank-1)throw Error(`Log Softmax along a non-last dimension is not yet supported. Logits was rank ${n.rank} and axis was ${t}`);return js((r,a)=>{let o=As(r,t,!0),u=ge(r,o),l=ge(ce(u,"float32"),Yn(ve(Xn(u),t,!0)));return a([l]),{value:l,gradFunc:(p,d)=>{let[h]=d,f=!0,m=Xn(h);return ge(p,V(ve(p,t,f),m))}}})(n)}var iI=L({logSoftmax_:nD});function Qg(e,t){for(let n=0;n<e.length;++n)if(e[e.length-n-1]!==t-1-n)return!1;return!0}function oI(e,t,n){let s=e.length+t.length,r=[],a=0,i=0;for(let o=0;o<s;o++)n.indexOf(o)===-1?r.push(e[a++]):r.push(t[i++]);return r}function uI(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 da(e,t){let n=t.map(s=>1);return oI(e,n,t)}function sD(e,t,n){O(Qg(t,n),()=>`${e} supports only inner-most axes for now. Got axes ${t} and rank-${n} input.`)}function lI(e,t){if(Qg(e,t))return null;let n=[];for(let s=0;s<t;++s)e.indexOf(s)===-1&&n.push(s);return e.forEach(s=>n.push(s)),n}function Zg(e){return e.map((t,n)=>[n,t]).sort((t,n)=>t[1]-n[1]).map(t=>t[0])}function rD(e,t){let n=[];for(let s=t-e;s<t;++s)n.push(s);return n}function aD(e,t=null,n=!1){let s=_(e,"x","logSumExp"),r=ts(t,s.shape),a=As(s,r,!0),i=ge(s,a),o=Xn(i),u=ve(o,r),l=Yn(u),c=ie(G(a,l.shape),l);if(n){let p=da(c.shape,r);return G(c,p)}return c}var iD=L({logSumExp_:aD});function oD(e,t){let n=_(e,"a","logicalAnd","bool"),s=_(t,"b","logicalAnd","bool");ot(n.shape,s.shape);let r={a:n,b:s};return M.runKernel(wo,r)}var Ds=L({logicalAnd_:oD});function uD(e){let n={x:_(e,"x","logicalNot","bool")};return M.runKernel(vl,n)}var Jg=L({logicalNot_:uD});function lD(e,t){let n=_(e,"a","logicalOr","bool"),s=_(t,"b","logicalOr","bool");ot(n.shape,s.shape);let r={a:n,b:s};return M.runKernel(Jd,r)}var cI=L({logicalOr_:lD});function cD(e,t){let n=_(e,"a","logicalXor","bool"),s=_(t,"b","logicalXor","bool");return ot(n.shape,s.shape),Ds(cI(e,t),Jg(Ds(e,t)))}var tpe=L({logicalXor_:cD});function dD(e,t,n,s,r){let a=_(e,"x","maxPool"),i=1,o=a,u=!1;a.rank===3&&(u=!0,o=G(a,[1,a.shape[0],a.shape[1],a.shape[2]])),O(o.rank===4,()=>`Error in maxPool: input must be rank 4 but got rank ${o.rank}.`),O(Ps(n,i),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${n} and dilations '${i}'`),hn("maxPool",s,r);let l={x:o},c={filterSize:t,strides:n,pad:s,dimRoundingMode:r},p=M.runKernel(Ha,l,c);return u?G(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var eb=L({maxPool_:dD});function pD(e,t=[1,1,1],n,s,r,a="NDHWC"){let i=_(e,"x","maxPool3d"),o=i,u=!1;i.rank===4&&(u=!0,o=G(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])),O(o.rank===5,()=>`Error in maxPool3d: x must be rank 5 but got rank ${o.rank}.`),O(a==="NDHWC",()=>`Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of ${a}`),hn("maxPool3d",s,r);let l={x:o},c={filterSize:t,strides:n,pad:s,dimRoundingMode:r,dataFormat:a},p=M.runKernel(tp,l,c);return u?G(p,[p.shape[1],p.shape[2],p.shape[3],p.shape[4]]):p}var dI=L({maxPool3d_:pD});function hD(e,t,n,s,r=!1){let i={x:_(e,"x","maxPoolWithArgmax")},o={filterSize:t,strides:n,pad:s,includeBatchInIndex:r},u=M.runKernel(Sg,i,o);return{result:u[0],indexes:u[1]}}var fD=L({maxPoolWithArgmax_:hD});fu
${r.shape}`);if(a.rank!==1)throw new Error(`Values should be Tensor1D but received shape ${a.shape}`);if(i.rank!==1)throw new Error(`Dense shape should be Tensor1D but received shape ${i.shape}`);if(o.rank!==0)throw new Error(`Default value should be a scalar but received shape ${o.shape}`);let u={indices:r,values:a,denseShape:i,defaultValue:o},l=M.runKernel(sp,u);return{outputIndices:l[0],outputValues:l[1],emptyRowIndicator:l[2],reverseIndexMap:l[3]}}var $O=L({sparseFillEmptyRows_:TO});function _O(e,t,n){let s=_(e,"inputIndices","sparseReshape","int32"),r=_(t,"inputShape","sparseReshape","int32"),a=_(n,"newShape","sparseReshape","int32");if(s.rank!==2)throw new Error(`Input indices should be Tensor2D but received shape
${s.shape}`);if(r.rank!==1)throw new Error(`Input shape should be Tensor1D but received shape ${r.shape}`);if(a.rank!==1)throw new Error(`New shape should be Tensor1D but received shape ${a.shape}`);let i={inputIndices:s,inputShape:r,newShape:a},o=M.runKernel($l,i);return{outputIndices:o[0],outputShape:o[1]}}var AO=L({sparseReshape_:_O});function EO(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
${r.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
${a.shape}`);let i={data:s,indices:r,segmentIds:a};return M.runKernel(rp,i)}var RO=L({sparseSegmentMean_:EO});function DO(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
${r.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
${a.shape}`);let i={data:s,indices:r,segmentIds:a};return M.runKernel(ap,i)}var FO=L({sparseSegmentSum_:DO});function OO(e,t,n,s,r,a,i,o){let u=_(e,"data","stringNGrams","string");if(u.dtype!=="string")throw new Error("Data must be of datatype string");if(u.shape.length!==1)throw new Error(`Data must be a vector, saw: ${u.shape}`);let l=_(t,"dataSplits","stringNGrams");if(l.dtype!=="int32")throw new Error("Data splits must be of datatype int32");let c={separator:n,nGramWidths:s,leftPad:r,rightPad:a,padWidth:i,preserveShortSequences:o},p={data:u,dataSplits:l},d=M.runKernel(op,p,c);return{nGrams:d[0],nGramsSplits:d[1]}}var PO=L({stringNGrams_:OO});function zO(e,t,n=!0){let s=_(e,"input","stringSplit","string"),r=_(t,"delimiter","stringSplit","string");if(s.rank!==1)throw new Error(`Input should be Tensor1D but received shape ${s.shape}`);if(r.rank!==0)throw new Error(`Delimiter should be a scalar but received shape ${r.shape}`);let a={skipEmpty:n},i={input:s,delimiter:r},o=M.runKernel($g,i,a);return{indices:o[0],values:o[1],shape:o[2]}}var MO=L({stringSplit_:zO});function LO(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 M.runKernel(_g,r,s)}var BO=L({stringToHashBucketFast_:LO}),kpe={fft:lb,ifft:Id,rfft:cb,irfft:xI},Ipe={hammingWindow:mF,hannWindow:AI,frame:EI,stft:vF},qn={flipLeftRight:IF,grayscaleToRGB:CF,resizeNearestNeighbor:XF,resizeBilinear:jF,rotateWithOffset:TF,cropAndResize:wF,nonMaxSuppression:_F,nonMaxSuppressionAsync:zF,nonMaxSuppressionWithScore:LF,nonMaxSuppressionWithScoreAsync:VF,nonMaxSuppressionPadded:UF,nonMaxSuppressionPaddedAsync:HF,threshold:ZF,transform:eO},VO={bandPart:nO,gramSchmidt:rO,qr:iO},Spe={absoluteDifference:cO,computeWeightedLoss:Ys,cosineDistance:pO,hingeLoss:fO,huberLoss:gO,logLoss:yO,meanSquaredError:xO,sigmoidCrossEntropy:IO,softmaxCrossEntropy:NO},Wc={sparseFillEmptyRows:$O,sparseReshape:AO,sparseSegmentMean:RO,sparseSegmentSum:FO},Mf={stringNGrams:PO,stringSplit:MO,stringToHashBucketFast:BO},_r=class extends Vk{minimize(e,t=!1,n){let{value:s,grads:r}=this.computeGradients(e,n);if(n!=null){let a=n.map(i=>({name:i.name,tensor:r[i.name]}));this.applyGradients(a)}else this.applyGradients(r);return Re(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 XR(e,t)}dispose(){this.iterations_!=null&&Re(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(_r,Symbol.hasInstance,{value:e=>e.minimize!=null&&e.computeGradients!=null&&e.applyGradients!=null});var fb=class extends _r{constructor(e,t,n=null){super();this.learningRate=e,this.rho=t,this.epsilon=n,this.accumulatedGrads=[],this.accumulatedUpdates=[],n==null&&(this.epsilon=M.backend.epsilon())}applyGradients(e){(Array.isArray(e)?e.map(n=>n.name):Object.keys(e)).forEach((n,s)=>{let r=M.registeredVariables[n],a=!1;this.accumulatedGrads[s]==null&&(this.accumulatedGrads[s]={originalName:`${n}/accum_grad`,variable:j(()=>je(r).variable(a))}),this.accumulatedUpdates[s]==null&&(this.accumulatedUpdates[s]={originalName:`${n}/accum_var`,variable:j(()=>je(r).variable(a))});let i=Array.isArray(e)?e[s].tensor:e[n];if(i==null)return;let o=this.accumulatedGrads[s].variable,u=this.accumulatedUpdates[s].variable;j(()=>{let l=ie(V(o,this.rho),V(ct(i),1-this.rho)),c=V(xe(dn(ie(u,this.epsilon)),dn(ie(o,this.epsilon))),i),p=ie(V(u,this.rho),V(ct(c),1-this.rho));o.assign(l),u.assign(p);let d=ie(V(c,-this.learningRate),r);r.assign(d)})}),this.incrementIterations()}dispose(){this.accumul
indices.shape[0] = ${e}`}function wP(e,t){return`indices(${e}, 0) is invalid: ${t} < 0`}function kP(e,t,n){return`indices(${e}, 0) is invalid: ${t} >= ${n}`}function IP(e,t){return`only one output dimension may be -1, not both ${e} and ${t}`}function SP(e,t){return`size ${e} must be non-negative, not ${t}`}function CP(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function NP(e,t){let n=pt(e),s=pt(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 TP(e,t){let n=pt(e),s=pt(t);return`Input to reshape is a tensor with ${n} dense values, but the requested shape has ${s}. inputShape=${e} outputShape=${t}`}function $P(){return"segment ids must be >= 0"}function _P(){return"segment ids are not increasing"}function AP(e,t){return`Segment id ${e} out of range [0, ${t}), possibly because segmentIds input is not sorted.`}function EP(e,t,n){return`Bad: indices[${e}] == ${t} out of range [0, ${n})`}var MI={};Ae(MI,{collectGatherOpShapeInfo:()=>FP,computeOutShape:()=>DP,segOpComputeOptimalWindowSize:()=>RP});function RP(e,t){let n=!1,s;for(e<=xb?(s=e,n=!0):s=hd(e,Math.floor(Math.sqrt(e)));!n;)s>t||s===e?n=!0:s=hd(e,s+1);return s}function DP(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 FP(e,t,n,s){let r=t.shape.length,a=e.shape.length;if(s!==0&&(s<-r||s>r))throw new Error(`Expect batchDims in the range of [-${r}, ${r}], but got ${s}`);if(s<0&&(s+=r),s>a)throw new Error(`batchDims (${s}) must be less than rank(x) (
${a}).`);if(n<s)throw new Error(`batchDims (${s}) must be less than or equal to axis (${n}).`);for(let p=0;p<s;++p)if(e.shape[p]!==t.shape[p])throw new Error(`x.shape[${p}]: ${e.shape[p]} should be equal to indices.shape[${p}]: ${t.shape[p]}.`);let i=e.shape[n],o=[],u=1,l=1,c=1;for(let p=0;p<s;++p)o.push(e.shape[p]),u*=e.shape[p];for(let p=s;p<n;p++)o.push(e.shape[p]),l*=e.shape[p];for(let p=s;p<r;p++)o.push(t.shape[p]);for(let p=n+1;p<a;p++)o.push(e.shape[p]),c*=e.shape[p];return{batchSize:u,sliceSize:c,outerSize:l,dimSize:i,outputShape:o}}function OP(e){try{return e.map(t=>md(t))}catch(t){throw new Error(`Failed to decode encoded string bytes into utf-8, error: ${t}`)}}function PP(e){return e.map(t=>Rl(t))}var xs={};Ae(xs,{nonMaxSuppressionV3Impl:()=>RI,nonMaxSuppressionV4Impl:()=>DI,nonMaxSuppressionV5Impl:()=>FI,whereImpl:()=>kI});var LI={kernelName:ao,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>V(e,yp(ce(n,"float32"),-1))}}},zP={kernelName:nl,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let s=ct(ce(n,"float32")),r=dn(ge(we(1),s));return kt(xe(e,r))}}}},MP={kernelName:sl,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let s=dn(ge(ct(ce(n,"float32")),1));return xe(e,s)}}}},LP={kernelName:Ir,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,s]=t,r=ot(n.shape,s.shape);return{a:()=>{let o=e,u=_t(n.shape,r);return u.length>0&&(o=ve(o,u)),G(o,n.shape)},b:()=>{let o=e,u=_t(s.shape,r);return u.length>0&&(o=ve(o,u)),G(o,s.shape)}}}},BP={kernelName:ka,saveAllInputs:!0,gradFunc:(e,t)=>{let n={};return t.forEach((s,r)=>{n[r]=()=>e.clone()}),n}},VP={kernelName:Ia,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>je(n)}}},WP={kernelName:il,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>je(n)}}},UP={kernelName:ol,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>xe(e,dn(ge(we(1),ct(ce(n,"float32")))))}}},GP={kernelName:ul,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let s=dn(ie(we(1),ct(ce(n,"float32"))));return xe(e,s)}}}},HP={kernelName:dl,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,s]=t,r=ot(n.shape,s.shape);return{a:()=>{let o=ie(ct(n),ct(s)),u=V(e,xe(s,o)),l=_t(n.shape,r);return l.length>0&&(u=ve(u,l)),G(u,n.shape)},b:()=>{let o=ie(ct(n),ct(s)),u=kt(V(e,xe(n,o))),l=_t(s.shape,r);return l.length>0&&(u=ve(u,l)),G(u,s.shape)}}}},qP={kernelName:ll,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>xe(e,ie(ct(ce(n,"float32")),1))}}},jP={kernelName:cl,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>xe(e,ge(we(1),ct(ce(n,"float32"))))}}};function KP(e,t,n,s,r,a){let i=_(e,"dy","avgPool3dGrad"),o=_(t,"input","avgPool3dGrad"),u=i,l=o,c=!1;o.rank===4&&(c=!0,u=G(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]]),l=G(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]])),O(u.rank===5,()=>`Error in avgPool3dGrad: dy must be rank 5 but got rank ${u.rank}.`),O(l.rank===5,()=>`Error in avgPool3dGrad: input must be rank 5 but got rank ${l.rank}.`),hn("avgPool3dGrad",r,a);let p={dy:u,input:l},d={filterSize:n,strides:s,pad:r,dimRoundingMode:a},h=M.runKernel(og,p,d);return c?G(h,[h.shape[1],h.shape[2],h.shape[3],h.shape[4]]):h}var XP=L({avgPool3dGrad_:KP}),YP={kernelName:qd,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[s]=t,{filterSize:r,strides:a,pad:i,dimRoundingMode:o}=n;return{x:()=>XP(e,s,r,a,i,o)}}};function QP(e,t,n,s,r){let a=_(e,"dy","avgPoolGrad"),i=_(t,"input","avgPoolGrad");O(i.rank===a.rank,()=>`Rank of input (${i.rank}) does not match rank of dy (${a.rank})`);let o=i,u=a,l=!1;i.rank===3&&(l=!0,o=G(i,[1,i.shape[0],i.shape[1],i.shape[2]]),u=G(a,[1,a.shape[0],a.shape[1],a.shape[2]])),O(u.rank===4,()=>`Error in avgPoolGrad: dy must be rank 4 but got rank ${u.rank}.`),O(o.rank===4,()=>`Error in avgPoolGrad: input must be rank 4 but got rank ${o.rank}.`);let c={dy:u,input:o},p={filterSize:n,strides:s,pad:r},d=M.runKernel(ig,c,p);return l?G(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var ZP=L({avgPoolGrad_:QP}),JP={kernelName:Sa,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[s]=t,{filterSize:r,strides:a,pad:i}=n;return{x:()=>ZP(e,s,r,a,i)}}},ez={kernelName:
1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);return i}else{let a=e;if(a.className==null||a.config==null)throw new U(`${s}: Improper config format: ${JSON.stringify(a)}.
'className' and 'config' must set.`);let i=a.className,o,u;if(i in n?[o,u]=n[i]:i in Gn?[o,u]=Gn.className:i in t&&([o,u]=t[i]),o==null)throw new U(`Unknown ${s}: ${i}. This may be due to one of the following reasons:
1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
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(Gn))l[h]=Gn[h];for(let h of Object.keys(n))l[h]=n[h];let c=a.config;c.customObjects=l;let p={...Gn};for(let h of Object.keys(n))Gn[h]=n[h];mm(a.config);let d=u(o,a.config,n,r);return Gn={...p},d}else{let l={...Gn};for(let p of Object.keys(n))Gn[p]=n[p];let c=new o(a.config);return Gn={...l},c}}}function LM(e,t){return e<t?-1:e>t?1:0}function Uc(e,t){return-1*LM(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 BM(e){if(e==null)throw new U(`Invalid value in obj: ${JSON.stringify(e)}`);for(let t in e)if(e.hasOwnProperty(t))return!1;return!0}function mi(e,t,n){if(n!=null&&e.indexOf(n)<0)throw new U(`${n} is not a valid ${t}. Valid values are ${e} or null/undefined.`)}function kb(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 ${WI(e)}.`)}function WI(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>WI(t)).join(",")+"]":typeof e=="string"?`"${e}"`:`${e}`}function VM(e,t,n){let s=n!=null?n():w.now(),r;return(...i)=>{let o=n!=null?n():w.now();return o-s<t||(s=o,r=e(...i)),r}}function UI(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}function Ib(e,t){return j(()=>dn(ve(V(e,e),t,!0)))}var Ll=class extends ae.Serializable{getConfig(){return{}}},Sb=class extends Ll{constructor(e){super();this.defaultMaxValue=2,this.defaultAxis=0,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return j(()=>{let t=Ib(e,this.axis),n=Vn(t,0,this.maxValue);return V(e,xe(n,ie(Rt(),t)))})}getConfig(){return{maxValue:this.maxValue,axis:this.axis}}};Sb.className="MaxNorm";ae.registerClass(Sb);var Cb=class extends Ll{constructor(e){super();this.defaultAxis=0,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return j(()=>xe(e,ie(Rt(),Ib(e,this.axis))))}getConfig(){return{axis:this.axis}}};Cb.className="UnitNorm";ae.registerClass(Cb);var Nb=class extends Ll{apply(e){return Xs(e)}};Nb.className="NonNeg";ae.registerClass(Nb);var Tb=class extends Ll{constructor(e){super();this.defaultMinValue=0,this.defaultMaxValue=1,this.defaultRate=1,this.defaultAxis=0,this.minValue=e.minValue!=null?e.minValue:this.defaultMinValue,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.rate=e.rate!=null?e.rate:this.defaultRate,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return j(()=>{let t=Ib(e,this.axis),n=ie(V(this.rate,Vn(t,this.minValue,this.maxValue)),V(1-this.rate,t));return V(e,xe(n,ie(Rt(),t)))})}getConfig(){return{minValue:this.minValue,maxValue:this.maxValue,rate:this.rate,axis:this.axis}}};Tb.className="MinMaxNorm";ae.registerClass(Tb);var Cx={maxNorm:"MaxNorm",minMaxNorm:"MinMaxNorm",nonNeg:"NonNeg",unitNorm:"UnitNorm"};function Ot(e){return wb(e)}function Nx(e,t={}){return Ml(e,ae.SerializationMap.getMap().classNameMap,t,"constraint")}function Pt(e){if(e==null)return null;if(typeof e=="string"){let n={className:e in Cx?Cx[e]:e,config:{}};return Nx(n)}else return e instanceof Ll?e:Nx(e)}function WM(e){return new Sb(e)}function UM(e){return new Cb(e)}function GM(){return new Nb}function HM(e){return new Tb(e)}var qM={};Ae(qM,{constant:()=>mL,glorotNormal:()=>kL,glorotUniform:()=>wL,heNormal:()=>IL,heUniform:()=>SL,identity:()=>vL,leCunNormal:()=>CL,leCunUniform:()=>NL,ones:()=>fL,orthogonal:()=>TL,randomNormal:()=>bL,randomUniform:()=>gL,truncatedNormal:()=>yL,varianceScaling:()=>xL,zeros:()=>hL});var jM=["channelsFirst","channelsLast"],KM=["nearest","bilinear"],XM=["valid","same","causal"],YM=["max","avg"],QM=["sum","mul","concat","ave"],zi=new Map;function Ct(e){mi(jM,"DataFormat",e)}function ZM(e){mi(KM,"InterpolationForm
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),Hn(this.elementShape,t.shape,`TensorArray ${this.name}: Could not write to TensorArray index ${e}.`),n.read)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e}, because it has already been read.`);if(n.written)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e}, because it has already been written.`);n.tensor=t,Ht(t),n.written=!0,this.tensors[e]=n}writeMany(e,t){if(e.length!==t.length)throw new Error(`TensorArray ${this.name}: could not write multiple tensors,because the index size: ${e.length} is not the same as tensors size: ${t.length}.`);e.forEach((n,s)=>this.write(n,t[s]))}gather(e,t){if(!!t&&t!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but gather requested dtype ${t}`);if(e)e=e.slice(0,this.size());else{e=[];for(let s=0;s<this.size();s++)e.push(s)}if(e.length===0)return fs([],[0].concat(this.elementShape));let n=this.readMany(e);return Hn(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),Jn(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 fs([],[0].concat(this.elementShape));let t=[];for(let s=0;s<this.size();s++)t.push(s);let n=this.readMany(t);return Hn(this.elementShape,n[0].shape,`TensorArray shape mismatch: tensor array shape (${this.elementShape}) vs first tensor shape (${n[0].shape})`),Ft(n,0)}scatter(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);if(e.length!==t.shape[0])throw new Error(`Expected len(indices) == tensor.shape[0], but saw: ${e.length} vs. ${t.shape[0]}`);let n=Math.max(...e);if(!this.dynamicSize&&n>=this.maxSize)throw new Error(`Max index must be < array size (${n} vs. ${this.maxSize})`);this.writeMany(e,Fs(t,0))}split(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);let n=0,s=e.map(o=>(n+=o,n));if(n!==t.shape[0])throw new Error(`Expected sum of lengths to be equal to
tensor.shape[0], but sum of lengths is
${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=[];j(()=>{t=G(t,[1,n,r]);for(let o=0;o<e.length;++o){let u=o===0?0:s[o-1],l=[0,u,0],c=[1,e[o],r];a[o]=G(He(t,l,c),this.elementShape)}return a});let i=[];for(let o=0;o<e.length;o++)i[o]=o;this.writeMany(i,a)}},jl=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}`);Hn(t,r.shape,"TensorList shape mismatch: "),Ht(r)}),this.idTensor=we(0),this.maxNumElements=s,Ht(this.idTensor)}get id(){return this.idTensor.id}copy(){return new jl([...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.`);Hn(e,this.elementShape,"TensorList shape mismatch: ");let s=Cu(this.elementShape,this.tensors,e);return j(()=>{let r=this.tensors.map(a=>G(a,s));return Jn(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=Cu(this.elementShape,this.tensors,e),s=this.tensors.pop();return Hn(s.shape,e,"TensorList shape mismatch: "),G(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(Hn(e.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");Ht(e),this.tensors.push(e)}resize(e){if(e<0)throw new Error(`TensorListResize expects size to be non-negative. Got: ${e}`);if(this.maxNumElements!==-1&&e>this.maxNumElements)throw new Error(`TensorListResize input size ${e} is greater maxNumElement ${this.maxNumElements}.`);this.tensors.length=e}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.`);Hn(this.tensors[e].shape,t,"TensorList shape mismatch: ");let s=Cu(this.elementShape,this.tensors,t);return G(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.`);Hn(this.elementShape,t.shape,"TensorList shape mismatch: "),Ht(t),this.tensors[e]=t}gather(e,t,n){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);Hn(this.elementShape,n,"TensorList shape mismatch: "),e=e.slice(0,this.size());let s=Cu(this.elementShape,this.tensors,n);return e.length===0?fs([],[0].concat(s)):j(()=>{let r=e.map(a=>G(this.tensors[a],s));return Jn(r,0)})}concat(e,t){if(!!e&&e!==this.elementDtype)throw new Error(`TensorList dtype is ${this.elementDtype} but concat requested dtype ${e}`);Hn(this.elementShape,t,"TensorList shape mismatch: ");let n=Cu(this.elementShape,this.tensors,t);return this.size()===0?fs([],[0].concat(n)):j(()=>{let s=this.tensors.map(r=>G(r,n));return Ft(s,0)})}};function e4(e,t,n){let s=e.dtype;if(e.shape.length<1)throw new Error(`Tensor m
tensor.shape[0], but sum of lengths is
${s}, and tensor's shape is: ${e.shape}`);let a=e.shape.slice(1),i=Pm(a,n),o=s===0?0:e.size/s,u=j(()=>{let c=[];e=G(e,[1,s,o]);for(let p=0;p<t.length;++p){let d=p===0?0:r[p-1],h=[0,d,0],f=[1,t[p],o];c[p]=G(He(e,h,f),i)}return e.dispose(),c}),l=new jl([],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=I("thenBranch",e,t,n),r=I("elseBranch",e,t,n),a=I("cond",e,t,n),i=I("args",e,t,n);return(await a.data())[0]?n.functionMap[s].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap):n.functionMap[r].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap)}case"While":case"StatelessWhile":{let s=I("body",e,t,n),r=I("cond",e,t,n),a=I("args",e,t,n),i=await n.functionMap[r].executeFunctionAsync(a,n.tensorArrayMap,n.tensorListMap),o=a.map(c=>c.id),u=await i[0].data();i.forEach(c=>{!c.kept&&o.indexOf(c.id)===-1&&c.dispose()});let l=a;for(;u[0];){let c=l;l=await n.functionMap[s].executeFunctionAsync(l,n.tensorArrayMap,n.tensorListMap);let p=l.map(h=>h.id);c.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()});let d=await n.functionMap[r].executeFunctionAsync(l,n.tensorArrayMap,n.tensorListMap);u=await d[0].data(),d.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()})}return l}case"LoopCond":{let s=I("pred",e,t,n);return[Us(s)]}case"Switch":{let s=I("pred",e,t,n),r=I("data",e,t,n);return r.kept||(r=Us(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[Us(r)]}return}case"Enter":{let s=I("frameName",e,t,n),r=I("tensor",e,t,n);return n.enterFrame(s),[Us(r)]}case"Exit":{let s=I("tensor",e,t,n);return n.exitFrame(),[Us(s)]}case"NextIteration":{let s=I("tensor",e,t,n);return n.nextIteration(),[Us(s)]}case"TensorArrayV3":{let s=I("size",e,t,n),r=I("dtype",e,t,n),a=I("elementShape",e,t,n),i=I("dynamicSize",e,t,n),o=I("clearAfterRead",e,t,n),u=I("identicalElementShapes",e,t,n),l=I("name",e,t,n),c=new JW(l,r,s,a,u,i,o);return n.addTensorArray(c),[c.idTensor,we(1)]}case"TensorArrayWriteV3":{let s=I("tensorArrayId",e,t,n),r=I("index",e,t,n),a=I("tensor",e,t,n),i=n.getTensorArray(s.id);return i.write(r,a),[i.idTensor]}case"TensorArrayReadV3":{let s=I("tensorArrayId",e,t,n),r=I("index",e,t,n);return[n.getTensorArray(s.id).read(r)]}case"TensorArrayGatherV3":{let s=I("tensorArrayId",e,t,n),r=I("indices",e,t,n),a=I("dtype",e,t,n);return[n.getTensorArray(s.id).gather(r,a)]}case"TensorArrayScatterV3":{let s=I("tensorArrayId",e,t,n),r=I("indices",e,t,n),a=I("tensor",e,t,n),i=n.getTensorArray(s.id);return i.scatter(r,a),[i.idTensor]}case"TensorArrayConcatV3":{let s=I("tensorArrayId",e,t,n),r=n.getTensorArray(s.id),a=I("dtype",e,t,n);return[r.concat(a)]}case"TensorArraySplitV3":{let s=I("tensorArrayId",e,t,n),r=I("tensor",e,t,n),a=I("lengths",e,t,n),i=n.getTensorArray(s.id);return i.split(a,r),[i.idTensor]}case"TensorArraySizeV3":{let s=I("tensorArrayId",e,t,n),r=n.getTensorArray(s.id);return[we(r.size(),"int32")]}case"TensorArrayCloseV3":{let s=I("tensorArrayId",e,t,n),r=n.getTensorArray(s.id);return r.clearAndClose(),[r.idTensor]}case"TensorListSetItem":{let s=I("tensorListId",e,t,n),r=I("index",e,t,n),a=I("tensor",e,t,n),i=n.getTensorList(s.id);return i.setItem(r,a),[i.idTensor]}case"TensorListGetItem":{let s=I("tensorListId",e,t,n),r=I("index",e,t,n),a=I("elementShape",e,t,n),i=I("elementDType",e,t,n);return[n.getTensorList(s.id).getItem(r,a,i)]}case"TensorListScatterV2":case"TensorListScatter":{let s=I("indices",e,t,n),r=I("tensor",e,t,n),a=I("elementShape",e,t,n),i=I("numElements",e,t,n),o=n4(r,s,a,i);return n.addTensorList(o),[o.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let s=I("elementShape",e,t,n),r=I("elementDType",e,t,n),a;e.op==="TensorListReserve"?a="numElements":a="maxNumElements";let i=I(a,e,t,n),o=t4(s,r,i);return n.addTensorList(o),[o.idTensor]}case"TensorListGather":{let s=I("tensorListId",e,t,n),r=I("indices",e,t,n),a=I("elementShape",e,t,n),i=I("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,sU),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=>j(()=>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=>j(()=>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=Yy(async()=>({value:await t.iterator(),done:!1}));return V4(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=D4.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()}};Zo.MAX_BUFFER_SIZE=1e4;function _n(e,t=null){return new class extends Zo{constructor(){super(...arguments);this.size=t}async iterator(){return e()}}}function tU(e){return _n(async()=>S0(e),e.length)}function nU(e){if(!eo(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 x0(e,s=>{if(s instanceof Zo)return{value:s.iterator(),recurse:!1};if(eo(s))return{value:null,recurse:!0};throw new Error("Leaves of the structure passed to zip() must be Datasets, not primitives.")});return W4(n,1)},t)}function sU(e){if(e===null)return null;let t=e[0];return z4(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?Jn(e):fs(e)}var $0=class extends Zo{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))}},Kc='"',Nu=Symbol("out"),ew=Symbol("field"),Xc=Symbol("quote"),qf=Symbol("quoteafterquote"),tw=Symbol("quoteinquote"),_0=class extends Zo{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 $0(e),t||(t={}),this.hasHeader=t.hasHeader!==!1,this.fullColumnNames=t.columnNames,this.columnConfigs=t.columnConfigs,this.configuredColumnsOnly=t.configuredColumnsOnly,t.delimWhitespace?(w.assert(t.delimiter==null,()=>"Delimiter should not be provided when delimWhitespace is true."),this.delimWhitespace=!0,this.delimiter=" "):this.delimiter=t.delimiter?t.delimiter:","}async columnNames(){return this.columnNamesValidated||await this.setColumnNames(),this.configuredColumnsOnly?Object.keys(this.columnConfigs):this.fullColumnNames}async setColumnNames(){let e=await this.maybeReadHeaderLine();if(!this.fullColumnNames&&!e)throw new Error("Column names must be provided if there is no header line.");this.fullColumnNames&&e&&w.assert(e.length===this.fullColumnNames.length,()=>"The length of provided columnNames ("+this.fullColumnNames.length.toString()+") does not match the length of the header line read from file ("+e.length.toString()+")."),this.fullColumnNames||(this.fullColumnNames=e);let t=this.fullColumnNames.reduce((s,r)=>(s[r]=s[r]+1||1,s),{}),n=Object.keys(t).filter(s=>t[s]>1);if(w.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let s of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(s)===-1)throw new Error('The key "'+s+'" provided in columnConfigs does not match any of the column names ('+this.fullColumnNames.toString()+").")}this.columnNamesValidated=!0}async maybeReadHeaderLine(){if(this.hasHeader){let t=await(await this.base.iterator()).next();if(t.done)throw new Error("No data was found for CSV parsing.");let n=t.value;return this.parseRow(n,!1)}else return null}async iterator(){this.columnNamesValidated||await this.setColumnNames();let e=await this.base.iterator();return this.hasHeader&&(e=e.skip(1)),e.map(t=>this.makeDataElement(t))}makeDataElement(e){let t=this.parseRow(e),n={},s={};for(let r=0;r<this.fullColumnNames.length;r++){let a=this.fullColumnNames[r],i=this.columnConfigs?this.columnConfigs[a]:null;if(!(this.configuredColumnsOnly&&!i)){let o=t[r],u=null;if(o==="")if(i&&i.default!==void 0)u=i.default;else{if(i&&(i.required||i.isLabel))throw new Error(`Required column ${a} is empty in this line: ${e}`);u=void 0}else{let l=Number(o);if(isNaN(l))i&&i.dtype==="bool"?u=this.getBoolean(o):u=o;else if(!i||!i.dtype)u=l;else switch(i.dtype){case"float32":u=l;break;case"int32":u=Math.floor(l);break;case"bool":u=this.getBoolean(o);break;default:u=l}}i&&i.isLabel?s[a]=u:n[a]=u}}return Object.keys(s).length===0?n:{xs:n,ys:s}}getBoolean(e){return e==="1"||e.toLowerCase()==="true"?1:0}parseRow(e,t=!0){let n=[],s=0,r=e.length,a=Nu;for(let i=0;i<r;i++)switch(a){case Nu:switch(e.charAt(i)){case Kc:s=i+1,a=Xc;break;case this.delimiter:if(s=i+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),a=Nu;break;default:a=ew,s=i;break}break;case ew:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(s,i)),a=Nu,s=i+1;break;default:}break;case Xc:switch(e.charAt(i)){case Kc:a=qf;break;default:}break;case qf:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(s,i-1)),a=Nu,s=i+1;break;case Kc:a=Xc;break;default:a=tw;break}break;case tw:switch(e.charAt(i)){case Kc:a=Xc;break;default:}break;default:}if(a===qf?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}},A0=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
============================
Hi there \u{1F44B}. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.
============================`));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 S.mergeRealAndImagArrays(s,r)}return this.data.get(e).values}bufferSync(e){let t=this.readSync(e.dataId),n=t;if(e.dtype==="string")try{n=t.map(s=>w.decodeString(s))}catch(s){throw new Error("Failed to decode encoded string bytes into utf-8")}return De(e.shape,e.dtype,n)}makeOutput(e,t,n){let s=this.write(e,t,n);return Ss().makeTensorFromDataId(s,t,n,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 yU(e.shape,t)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}},L0=M0;L0.nextDataId=0;var Zy={};Ae(Zy,{addImpl:()=>V0,bincountImpl:()=>ev,bincountReduceImpl:()=>W0,ceilImpl:()=>U0,concatImpl:()=>tv,equalImpl:()=>G0,expImpl:()=>q0,expm1Impl:()=>K0,floorImpl:()=>X0,gatherNdImpl:()=>Y0,gatherV2Impl:()=>Q0,greaterEqualImpl:()=>J0,greaterImpl:()=>Z0,lessEqualImpl:()=>tC,lessImpl:()=>eC,linSpaceImpl:()=>nC,logImpl:()=>sC,maxImpl:()=>rC,maximumImpl:()=>aC,minimumImpl:()=>iC,multiplyImpl:()=>nv,negImpl:()=>oC,notEqualImpl:()=>uC,prodImpl:()=>lC,rangeImpl:()=>rv,rsqrtImpl:()=>cC,sigmoidImpl:()=>iG,simpleAbsImpl:()=>B0,sliceImpl:()=>Fd,sparseFillEmptyRowsImpl:()=>pC,sparseReshapeImpl:()=>hC,sparseSegmentReductionImpl:()=>av,sqrtImpl:()=>lG,squaredDifferenceImpl:()=>fC,stridedSliceImpl:()=>mC,stringNGramsImpl:()=>gC,stringSplitImpl:()=>bC,stringToHashBucketFastImpl:()=>yC,subImpl:()=>vC,tileImpl:()=>xC,topKImpl:()=>kC,transposeImpl:()=>sv,uniqueImpl:()=>IC});function B0(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var vU=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=B0(r),n.makeOutput(s,t.shape,t.dtype)},xU={kernelName:ao,backendName:"cpu",kernelFunc:vU};function At(e){return(t,n,s,r,a)=>{let i=S.assertAndGetBroadcastShape(t,n),o=i.length,u=w.computeStrides(i),l=w.sizeFromShape(i),c=w.getTypedArrayFromDType(a,l),p=t.length,d=n.length,h=w.computeStrides(t),f=w.computeStrides(n),m=S.getBroadcastDims(t,i),g=S.getBroadcastDims(n,i);if(m.length+g.length===0)for(let b=0;b<c.length;++b)c[b]=e(s[b%s.length],r[b%r.length]);else for(let b=0;b<c.length;++b){let y=w.indexToLoc(b,o,u),v=y.slice(-p);m.forEach(N=>v[N]=0);let x=w.locToIndex(v,p,h),k=y.slice(-d);g.forEach(N=>k[N]=0);let T=w.locToIndex(k,d,f);c[b]=e(s[x],r[T])}return[c,i]}}function Rn(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.data.get(s.dataId).values,i=n.data.get(r.dataId).values,o=n.makeTensorInfo(s.shape,"complex64"),u=n.data.get(o.dataId);return u.complexTensorInfos={real:n.makeTensorInfo(s.shape,"float32",a),imag:n.makeTensorInfo(r.shape,"float32",i)},o}var wU={kernelName:jd,backendName:"cpu",kernelFunc:Rn};function Dd(e,t,n="float32"){if(n==="complex64"){let r=Dd(e,t,"float32"),a=Dd(e,t,"flo
${a.shape}`);if(s.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${s.shape}`);if(r.shape.length!==1)throw new Error(`Values must be a vector, saw:
${r.shape}`);if(i.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
${i.shape}`);let o=n.data.get(s.dataId).values,u=n.data.get(r.dataId).values,l=n.data.get(a.dataId).values,c=n.data.get(i.dataId).values[0],[p,d,h,f,m]=pC(o,s.shape,s.dtype,u,r.dtype,l,c);return[n.makeTensorInfo(d,s.dtype,p),n.makeTensorInfo([d[0]],r.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(g=>Number(g)))),n.makeTensorInfo([m.length],s.dtype,new Int32Array(m))]}var Rj={kernelName:sp,backendName:"cpu",kernelFunc:Ej};function Dj(e){let{inputs:t,backend:n}=e,{inputIndices:s,inputShape:r,newShape:a}=t;if(s.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape
${s.shape}`);if(r.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let i=Array.from(n.data.get(r.dataId).values),o=n.data.get(s.dataId).values,u=Array.from(n.data.get(a.dataId).values),[l,c,p]=hC(o,s.shape,s.dtype,i,u);return[n.makeTensorInfo(c,s.dtype,l),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var Fj={kernelName:$l,backendName:"cpu",kernelFunc:Dj};function Oj(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
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);if(r.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let i=n.data.get(s.dataId).values,o=n.data.get(r.dataId).values,u=n.data.get(a.dataId).values,[l,c]=av(i,s.shape,s.dtype,o,u,!0);return n.makeTensorInfo(c,s.dtype,l)}var Pj={kernelName:rp,backendName:"cpu",kernelFunc:Oj};function zj(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
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);if(r.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let i=n.data.get(s.dataId).values,o=n.data.get(r.dataId).values,u=n.data.get(a.dataId).values,[l,c]=av(i,s.shape,s.dtype,o,u);return n.makeTensorInfo(c,s.dtype,l)}var Mj={kernelName:ap,backendName:"cpu",kernelFunc:zj};function Lj(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:i}=t,{outputShape:o}=s,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:d}=S.calculateShapes(a,r,o),h=!1,f=n.bufferSync(r),m=n.bufferSync(a),g=n.data.get(i.dataId).values[0],b=BC(f,m,o,d,c,l,u,p,g,h);return n.makeTensorInfo(o,b.dtype,b.values)}var Bj={kernelName:ip,backendName:"cpu",kernelFunc:Lj};function Vj(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=s,o=w.parseAxisParam(i,r.shape)[0],u=S.prepareSplitSize(r,a,o),l=new Array(r.shape.length).fill(0),c=r.shape.slice();return u.map(p=>{let d=[...c];d[o]=p;let h=ga({inputs:{x:r},backend:n,attrs:{begin:l,size:d}});return l[o]+=p,h})}var Wj={kernelName:Mo,backendName:"cpu",kernelFunc:Vj},Uj={kernelName:_l,backendName:"cpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,s=t;be(n,"square");let r=s.data.get(n.dataId).values,a=new Float32Array(r.length);for(let o=0;o<r.length;++o){let u=r[o];a[o]=u*u}return{dataId:s.write(a,n.shape,n.dtype),shape:n.shape,dtype:n.dtype}}},Gj=st(hi,(e,t)=>{let n=t;return isNaN(e)?NaN:e>0?1:n.alpha}),Hj={kernelName:hi,backendName:"cpu",kernelFunc:Gj};function qj(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s;be(r,"stridedSlice");let{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=wt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=mt({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 T=wt.computeOutShape(y,v,x),N=ga({inputs:{x:r},backend:n,attrs:{begin:y,size:T}});k=mt({inputs:{x:N},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(N)}else{let T=n.bufferSync(r),N=mC(h,T,x,y);k=n.makeTensorInfo(f,N.dtype,N.values)}return k}var jj={kernelName:Lo,backendName:"cpu",kernelFunc:qj};function Kj(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:i,rightPad:o,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.data.get(c.dataId).values,h=n.data.get(p.dataId).values,[f,m]=gC(d,h,r,a,i,o,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var Xj={kernelName:op,backendName:"cpu",kernelFunc:Kj};function Yj(e){let{inputs:t,backend:n,attrs:s}=e,{skipEmpty:r}=s,{input:a,delimiter:i}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(i.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${i.shape}`);let o=n.data.get(a.dataId).values,u=n.data.get(i.dataId).values[0],[l,c,p]=bC(o,u,r),d=c.length;return[n.makeTensorInfo([d,2],"int32",l),n.makeTensorInfo([d],"string",c),n.makeTensorInfo([2],"int32",new Int32Array(p))]}var Qj={kernelName:$g,backendName:"cpu",kernelFunc:Yj};function Zj(e){let{inputs:t,backend:n,attrs:s}=e,{numBuckets:r}=s,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(r<=0)throw new Error("Number of buckets must be at least 1");let i=n.data.get(a.dataId).values,o=yC(i,r);return n.makeTensorInfo(a.shape,"int32",o)}var Jj={kernelName:_g,backendName:"cpu",kernelFunc:Zj},e5=st(Bo,e=>Math.tan(e)),t5={kernelName:Bo,backendName:"cpu",kernelFunc:e5},n5=st(di,e=>Math.tanh(e)),s5={kernelName:di,backendName:"cpu",kernelFunc:n5};function r5(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reps:a}=s;be(r,"tile");let i=xC(n.bufferSync(r),a);return n.makeTensorInfo(i.shape,i.dtype,i.values)}var a5={kernelName:Cr,backendName:"cpu",kernelFunc:r5};function i5(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:i}=s;be(r,"topk
`),a=r.length.toString().length+2,i=r.map((p,d)=>w.rightPad((d+1).toString(),a)+p),o=0;for(let p=0;p<i.length;p++)o=Math.max(i[p].length,o);let u=i.slice(0,s-1),l=i.slice(s-1,s),c=i.slice(s);console.log(u.join(`
`)),console.log(t.split(`
`)[0]),console.log(`%c ${w.rightPad(l[0],o)}`,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(c.join(`
`))}function HC(e){return Qs(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function qC(e,t){if(fe(e,()=>e.linkProgram(t)),!X().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 rd(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 jC(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 KC(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 D5(){return X().getNumber("WEBGL_VERSION")===2?1:4}function XC(e){return Qs(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function YC(e,t){let n=X().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 QC(e){return Qs(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function Bm(e,t,n,s,r,a,i){let o=e.getAttribLocation(t,n);return o===-1?!1:(fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,s)),fe(e,()=>e.vertexAttribPointer(o,r,e.FLOAT,!1,a,i)),fe(e,()=>e.enableVertexAttribArray(o)),!0)}function ZC(e,t,n){s1(e,n),fe(e,()=>e.activeTexture(e.TEXTURE0+n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function F5(e,t){s1(e,t),fe(e,()=>e.activeTexture(e.TEXTURE0+t)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function JC(e,t,n){return Qs(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function e1(e,t,n){return e.getUniformLocation(t,n)}function t1(e,t,n,s){fe(e,()=>ZC(e,t,s)),fe(e,()=>e.uniform1i(n,s))}function O5(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 ad(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 Vm(e,t){fe(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,t)),fe(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0))}function Fu(e){let t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+n1(e,t))}function n1(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 s1(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 ba(e,t=2){return w.sizeFromShape(e.slice(0,e.length-t))}function ya(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 id(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[ba(e),...ya(e)]),t}function r1(e,t=!1){let n=X().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]
bool isnan_custom(float val) {
uint floatToUint = floatBitsToUint(val);
return (floatToUint & 0x7fffffffu) > 0x7f800000u;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-01 15:13:32 +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-01 15:13:32 +02:00
ivec4 newRound(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`):(e="",t="attribute",n="varying",s="varying",r="texture2D",a="gl_FragColor",i="",o=`
#define isnan(value) isnan_custom(value)
bool isnan_custom(float val) {
return (val > 0. || val < 1. || val == 0.) ? false : true;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
bvec4 isnan_custom(vec4 val) {
return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`,u=`
uniform float INFINITY;
bool isinf(float val) {
return abs(val) == INFINITY;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
bvec4 isinf(vec4 val) {
return equal(abs(val), vec4(INFINITY));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`,l=`
int round(float value) {
return int(floor(value + 0.5));
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
ivec4 round(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`),{version:e,attribute:t,varyingVs:n,varyingFs:s,texture2D:r,output:a,defineOutput:i,defineSpecialNaN:o,defineSpecialInf:u,defineRound:l}}function yi(e,t,n="index"){let s=w.computeStrides(t);return s.map((r,a)=>{let i=`int ${e[a]} = ${n} / ${r}`,o=a===s.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * ${r}`:`index -= ${e[a]} * ${r}`;return`${i}; ${o};`}).join("")}function qp(e,t,n="index"){let s=w.computeStrides(t);return s.map((r,a)=>{let i=`int ${e[a]} = ${n} / outShapeStrides[${a}]`,o=a===s.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * outShapeStrides[${a}]`:`index -= ${e[a]} * outShapeStrides[${a}]`;return`${i}; ${o};`}).join("")}function L5(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 B5(e,t,n="index"){let s=e.map((a,i)=>i),r=L5(s,t);return r.map((a,i)=>{let o=`int ${e[i]} = ${n} / ${r[i]}`,u=i===r.length-1?`int ${e[i+1]} = ${n} - ${e[i]} * ${r[i]}`:`index -= ${e[i]} * ${r[i]}`;return`${o}; ${u};`}).join("")}function hv(e){let t=w.computeStrides(e).map(n=>n.toString());return`
int getFlatIndex(ivec3 coords) {
return coords.x * ${t[0]} + coords.y * ${t[1]} + coords.z;
}
`}function fv(){return`
int getFlatIndex(ivec3 coords) {
return coords.x * outShapeStrides[0] + coords.y * outShapeStrides[1] + coords.z;
}
`}var d1=`
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;
}
`,{getBroadcastDims:p1}=S;function V5(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}=mv(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=>W5(h,t,n.packedInputs,n.enableShapeUniforms)).join(`
`),i=t.texShape,o=fn(),u=H5(o),l,c,p=K5(o);return t.isPacked?(l=U5(t.logicalShape,i,n.enableShapeUniforms),c=j5(o)):(l=G5(t.logicalShape,i,n.enableShapeUniforms),c=q5(o)),n.packedInputs&&(p+=Z5),[p,u,c,r,l,a,n.userCode].join(`
`)}function nu(e,t=!1){let n=e.shapeInfo.logicalShape;switch(n.length){case 0:return cK(e,t);case 1:return pK(e,t);case 2:return fK(e,t);case 3:return gK(e,t);case 4:return yK(e,t);case 5:return vK(e);case 6:return xK(e);default:throw new Error(`${n.length}-D input sampling is not yet supported`)}}function h1(e,t){switch(e.shapeInfo.logicalShape.length){case 0:return lK(e);case 1:return dK(e,t);case 2:return hK(e,t);case 3:return mK(e,t);default:return bK(e,t)}}function W5(e,t,n=!1,s){let r="";n?r+=h1(e,s):r+=nu(e,s);let a=e.shapeInfo.logicalShape,i=t.logicalShape;return a.length<=i.length&&(n?r+=wK(e,t):r+=kK(e,t)),r}function U5(e,t,n){switch(e.length){case 0:return f1();case 1:return J5(e,t,n);case 2:return oK(e,t,n);case 3:return tK(e,t,n);default:return sK(e,t,n)}}function G5(e,t,n){switch(e.length){case 0:return f1();case 1:return eK(e,t,n);case 2:return uK(e,t,n);case 3:return nK(e,t,n);case 4:return rK(e,t,n);case 5:return aK(e,t);case 6:return iK(e,t);default:throw new Error(`${e.length}-D output sampling is not yet supported`)}}function H5(e){return`
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${e.texture2D}(textureSampler, uv).r;
}
`}function q5(e){return`
void setOutput(float val) {
${e.output} = vec4(val, 0, 0, 0);
}
`}function j5(e){return`
void setOutput(vec4 val) {
${e.output} = val;
}
`}function K5(e){return`${e.version}
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-01 15:12:04 +02:00
};
2022-04-01 15:13:32 +02:00
struct ivec6
{
int x;
int y;
int z;
int w;
int u;
int v;
2022-04-01 15:12:04 +02:00
};
2022-04-01 15:13:32 +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-01 15:13:32 +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);
}
${X5}
${Y5}
${Q5}
`}var X5=`
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);
}
`,Y5=`
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);
}
`,Q5=`
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);
}
`,Z5=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
float getChannel(vec4 frag, int dim) {
float modCoord = mod(float(dim), 2.);
return modCoord == 0. ? frag.r : frag.g;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;function f1(){return`
int getOutputCoords() {
return 0;
}
`}function J5(e,t,n){let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return s[0]===1?n?`
int getOutputCoords() {
return 2 * int(resultUV.x * ceil(float(outTexShape[1]) / 2.0));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
int getOutputCoords() {
return 2 * int(resultUV.x * ${s[1]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-01 15:13:32 +02:00
`:`
int getOutputCoords() {
return 2 * int(resultUV.y * ${s[0]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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);
}
`}function eK(e,t,n){return t[0]===1?n?`
int getOutputCoords() {
return int(resultUV.x * float(outTexShape[1]));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
int getOutputCoords() {
return int(resultUV.x * ${t[1]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-01 15:13:32 +02:00
`:`
int getOutputCoords() {
return int(resultUV.y * ${t[0]}.0);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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;
}
`}function tK(e,t,n){if(n)return`
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);
}
`}function nK(e,t,n){if(n)return`
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
${qp(["r","c","d"],e)}
return ivec3(r, c, d);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;let s=yi(["r","c","d"],e);return`
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);
}
`}function sK(e,t,n){if(n)return`
ivec4 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int texelsInLogicalRow = int(ceil(float(outShape[3]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[2]) / 2.0));
int texelsInBatchN = texelsInBatch * outShape[1];
int b2 = index / texelsInBatchN;
index -= b2 * texelsInBatchN;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec4(b2, b, r, c);
}
`;let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],r=Math.ceil(e[e.length-1]/2),a=r*Math.ceil(e[e.length-2]/2),i=a,o="",u="b, r, c";for(let l=2;l<e.length-1;l++)i*=e[e.length-l-1],o=`
int b${l} = index / ${i};
index -= b${l} * ${i};
`+o,u=`b${l}, `+u;return`
ivec${e.length} getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${s[0]}, ${s[1]}));
int index = resTexRC.x * ${s[1]} + resTexRC.y;
${o}
int b = index / ${a};
index -= b * ${a};
int r = 2 * (index / ${r});
int c = imod(index, ${r}) * 2;
return ivec${e.length}(${u});
}
`}function rK(e,t,n){if(n)return`
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
${qp(["r","c","d","d2"],e)}
return ivec4(r, c, d, d2);
}
`;let s=yi(["r","c","d","d2"],e);return`
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);
}
`}function aK(e,t){let n=yi(["r","c","d","d2","d3"],e);return`
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;
}
`}function iK(e,t){let n=yi(["r","c","d","d2","d3","d4"],e);return`
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;
}
`}function oK(e,t,n){let s=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];if(w.arraysEqual(e,t))return n?`
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-01 15:13:32 +02:00
`:`
ivec2 getOutputCoords() {
return 2 * ivec2(resultUV.yx * vec2(${s[0]}, ${s[1]}));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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);
}
`}function uK(e,t,n){return w.arraysEqual(e,t)?n?`
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(outTexShape[0], outTexShape[1]));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(${t[0]}, ${t[1]}));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-01 15:13:32 +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-01 15:13:32 +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-01 15:13:32 +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-01 15:13:32 +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);
}
`}function vi(e){return`offset${e}`}function lK(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),s=fn();return`
vec4 ${n}() {
return ${s.texture2D}(${t}, halfCR);
}
`}function cK(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`
float ${s}() {
return sampleTexture(${n}, halfCR);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;let i=vi(n);if(t)return`
float ${s}() {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], ${i});
return sampleTexture(${n}, uv);
}
`;let[o,u]=e.shapeInfo.texShape;return`
float ${s}() {
vec2 uv = uvFromFlat(${o}, ${u}, ${i});
return sampleTexture(${n}, uv);
}
`}function dK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape,a=fn();if(t)return`
vec4 ${s}(int index) {
ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
vec2 uv = packedUVfrom1D(
packedTexShape[0], packedTexShape[1], index);
return ${a.texture2D}(${n}, uv);
}
`;let i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)];return`
vec4 ${s}(int index) {
vec2 uv = packedUVfrom1D(
${i[0]}, ${i[1]}, index);
return ${a.texture2D}(${n}, uv);
}
`}function pK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`
float ${s}(int index) {
${su(e)}
}
`;let r=e.shapeInfo.texShape,a=r[0],i=r[1];if(i===1&&a===1)return`
float ${s}(int index) {
return sampleTexture(${n}, halfCR);
}
`;let o=vi(n);return i===1?t?`
float ${s}(int index) {
vec2 uv = vec2(0.5, (float(index + ${o}) + 0.5) / float(${n}TexShape[0]));
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
vec2 uv = vec2(0.5, (float(index + ${o}) + 0.5) / ${a}.0);
return sampleTexture(${n}, uv);
}
`:a===1?t?`
float ${s}(int index) {
vec2 uv = vec2((float(index + ${o}) + 0.5) / float(${n}TexShape[1]), 0.5);
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
vec2 uv = vec2((float(index + ${o}) + 0.5) / ${i}.0, 0.5);
return sampleTexture(${n}, uv);
}
`:t?`
float ${s}(int index) {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], index + ${o});
return sampleTexture(${n}, uv);
}
`:`
float ${s}(int index) {
vec2 uv = uvFromFlat(${a}, ${i}, index + ${o});
return sampleTexture(${n}, uv);
}
`}function hK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape,i=a[0],o=a[1],u=fn();if(a!=null&&w.arraysEqual(n,a))return t?`
vec4 ${r}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${s}TexShape[1], ${s}TexShape[0]);
return ${u.texture2D}(${s}, uv);
}
`:`
vec4 ${r}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${o}.0, ${i}.0);
return ${u.texture2D}(${s}, uv);
}
`;if(t)return`
vec4 ${r}(int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${s}TexShape[0]) / 2.0), ceil(float(${s}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${s}Shape[1]) / 2.0));
vec2 uv = packedUVfrom2D(valuesPerRow, packedTexShape[0], packedTexShape[1], row, col);
return ${u.texture2D}(${s}, uv);
}
`;let l=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],c=Math.ceil(n[1]/2);return`
vec4 ${r}(int row, int col) {
vec2 uv = packedUVfrom2D(${c}, ${l[0]}, ${l[1]}, row, col);
return ${u.texture2D}(${s}, uv);
}
`}function fK(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`
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);
}
`}let{newShape:i,keptDims:o}=w.squeezeShape(n),u=i;if(u.length<n.length){let d=ru(e,u),h=["row","col"];return`
${nu(d,t)}
float ${r}(int row, int col) {
return ${r}(${au(h,o)});
}
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col) {
int index = round(dot(vec2(row, col), vec2(${n[1]}, 1)));
${su(e)}
}
`;let l=a[0],c=a[1],p=vi(s);return c===1?t?`
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-01 15:13:32 +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);
}
`}function mK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=e.shapeInfo.texShape,i=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)];if(n[0]===1){let d=n.slice(1),h=[1,2],f=ru(e,d),m=["b","row","col"];return`
${h1(f,t)}
vec4 ${r}(int b, int row, int col) {
return ${r}(${au(m,h)});
}
`}let o=fn();if(t)return`
vec4 ${r}(int b, int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${s}TexShape[0]) / 2.0), ceil(float(${s}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${s}Shape[2]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${s}Shape[1]) / 2.0));
vec2 uv = packedUVfrom3D(
packedTexShape[0], packedTexShape[1], texelsInBatch, valuesPerRow, b, row, col);
return ${o.texture2D}(${s}, uv);
}
`;let u=i[0],l=i[1],c=Math.ceil(n[2]/2),p=c*Math.ceil(n[1]/2);return`
vec4 ${r}(int b, int row, int col) {
vec2 uv = packedUVfrom3D(
${u}, ${l}, ${p}, ${c}, b, row, col);
return ${o.texture2D}(${s}, uv);
}
`}function gK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=n[1]*n[2],i=n[2],{newShape:o,keptDims:u}=w.squeezeShape(n),l=o;if(l.length<n.length){let m=ru(e,l),g=["row","col","depth"];return`
${nu(m,t)}
float ${r}(int row, int col, int depth) {
return ${r}(${au(g,u)});
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col, int depth) {
int index = round(dot(vec3(row, col, depth),
vec3(${a}, ${i}, 1)));
${su(e)}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
float ${r}(int row, int col, int depth) {
float texR = float(row);
float texC = dot(vec2(col, depth), vec2(${i}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${d}.0, ${p}.0);
return sampleTexture(${s}, uv);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;if(d===i&&h==null)return t?`
float ${r}(int row, int col, int depth) {
float texR = dot(vec2(row, col), vec2(${s}Shape[1], 1));
float texC = float(depth);
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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);
}
`;let f=vi(s);return t?`
float ${r}(int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int stride0 = ${s}Shape[1] * ${s}Shape[2];
int stride1 = ${s}Shape[2];
int index = row * ${a} + col * ${i} + depth + ${f};
vec2 uv = uvFromFlat(${s}TexShape[0], ${s}TexShape[1], index);
return sampleTexture(${s}, uv);
}
`:`
float ${r}(int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${a} + col * ${i} + depth + ${f};
vec2 uv = uvFromFlat(${p}, ${d}, index);
return sampleTexture(${s}, uv);
}
`}function bK(e,t){let n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=fn();if(t)return`
vec4 ${s}(int b2, int b, int row, int col) {
int valuesPerRow = int(ceil(float(${n}Shape[3]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${n}Shape[2]) / 2.0));
int index = b * texelsInBatch + (row / 2) * valuesPerRow + (col / 2);
texelsInBatch *= ${n}Shape[1];
index = b2 * texelsInBatch + index;
ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
int texR = index / packedTexShape[1];
int texC = index - texR * packedTexShape[1];
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(packedTexShape[1], packedTexShape[0]); return ${r.texture2D}(${n}, uv);
}
`;let a=e.shapeInfo.logicalShape,i=a.length,o=e.shapeInfo.texShape,u=[Math.ceil(o[0]/2),Math.ceil(o[1]/2)],l=u[0],c=u[1],p=Math.ceil(a[i-1]/2),d=p*Math.ceil(a[i-2]/2),h="int b, int row, int col",f=`b * ${d} + (row / 2) * ${p} + (col / 2)`;for(let m=2;m<i-1;m++)h=`int b${m}, `+h,d*=a[i-m-1],f=`b${m} * ${d} + `+f;return`
vec4 ${s}(${h}) {
int index = ${f};
int texR = index / ${c};
int texC = index - texR * ${c};
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${c}, ${l});
return ${r.texture2D}(${n}, uv);
}
`}function yK(e,t){let n=e.shapeInfo.logicalShape,s=e.name,r="get"+s.charAt(0).toUpperCase()+s.slice(1),a=n[3],i=n[2]*a,o=n[1]*i,{newShape:u,keptDims:l}=w.squeezeShape(n);if(u.length<n.length){let y=ru(e,u),v=["row","col","depth","depth2"];return`
${nu(y,t)}
float ${r}(int row, int col, int depth, int depth2) {
return ${r}(${au(v,l)});
}
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col, int depth, int depth2) {
int index = round(dot(vec4(row, col, depth, depth2),
vec4(${o}, ${i}, ${a}, 1)));
${su(e)}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;let c=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1],f=`int stride2 = ${s}Shape[3];`,m=`int stride1 = ${s}Shape[2] * stride2;`,g=`int stride0 = ${s}Shape[1] * stride1;`;if(h===o&&c==null)return t?`
float ${r}(int row, int col, int depth, int depth2) {
${f}
${m}
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(stride1, stride2, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${s}TexShape[1], ${s}TexShape[0]);
return sampleTexture(${s}, uv);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
float ${r}(int row, int col, int depth, int depth2) {
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(${i}, ${a}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${s}, uv);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;let b=vi(s);return t?`
float ${r}(int row, int col, int depth, int depth2) {
// Explicitly use integer operations as dot() only works on floats.
${f}
${m}
${g}
int index = row * stride0 + col * stride1 +
depth * stride2 + depth2;
vec2 uv = uvFromFlat(${s}TexShape[0], ${s}TexShape[1], index + ${b});
return sampleTexture(${s}, uv);
}
`:`
float ${r}(int row, int col, int depth, int depth2) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${o} + col * ${i} +
depth * ${a} + depth2;
vec2 uv = uvFromFlat(${d}, ${h}, index + ${b});
return sampleTexture(${s}, uv);
}
`}function vK(e){let t=e.shapeInfo.logicalShape,n=e.name,s="get"+n.charAt(0).toUpperCase()+n.slice(1),r=t[4],a=t[3]*r,i=t[2]*a,o=t[1]*i,{newShape:u,keptDims:l}=w.squeezeShape(t);if(u.length<t.length){let m=ru(e,u),g=["row","col","depth","depth2","depth3"];return`
${nu(m)}
float ${s}(int row, int col, int depth, int depth2, int depth3) {
return ${s}(${au(g,l)});
}
`}if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
float index = dot(
vec4(row, col, depth, depth2),
vec4(${o}, ${i}, ${a}, ${r})) +
depth3;
${su(e)}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;let c=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1];if(h===o&&c==null)return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
vec4(${i}, ${a}, ${r}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${n}, uv);
}
`;if(h===r&&c==null)return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
float texR = dot(
vec4(row, col, depth, depth2),
vec4(${t[1]*t[2]*t[3]},
${t[2]*t[3]}, ${t[3]}, 1));
int texC = depth3;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${d}.0);
return sampleTexture(${n}, uv);
}
`;let f=vi(n);return`
float ${s}(int row, int col, int depth, int depth2, int depth3) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${o} + col * ${i} + depth * ${a} +
depth2 * ${r} + depth3 + ${f};
vec2 uv = uvFromFlat(${d}, ${h}, index);
return sampleTexture(${n}, uv);
}
`}function xK(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=ru(e,r),b=["row","col","depth","depth2","depth3","depth4"];return`
${nu(g)}
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
return ${s}(${au(b,a)});
}
`}let i=t[5],o=t[4]*i,u=t[3]*o,l=t[2]*u,c=t[1]*l;if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int index = round(dot(
vec4(row, col, depth, depth2),
vec4(${c}, ${l}, ${u}, ${o})) +
dot(
vec2(depth3, depth4),
vec2(${i}, 1)));
${su(e)}
}
`;let p=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,h=d[0],f=d[1];if(f===c&&p==null)return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
vec4(${l}, ${u}, ${o}, ${i})) +
float(depth4);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
`;if(f===i&&p==null)return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
float texR = dot(vec4(row, col, depth, depth2),
vec4(${t[1]*t[2]*t[3]*t[4]},
${t[2]*t[3]*t[4]},
${t[3]*t[4]},
${t[4]})) + float(depth3);
int texC = depth4;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
`;let m=vi(n);return`
float ${s}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${c} + col * ${l} + depth * ${u} +
depth2 * ${o} + depth3 * ${i} + depth4 + ${m};
vec2 uv = uvFromFlat(${h}, ${f}, index);
return sampleTexture(${n}, uv);
}
`}function su(e){let t=e.name,n=w.sizeFromShape(e.shapeInfo.logicalShape);return n<2?`return ${t};`:`
for (int i = 0; i < ${n}; i++) {
if (i == index) {
return ${t}[i];
}
}
`}function wK(e,t){let n=e.name,s=n.charAt(0).toUpperCase()+n.slice(1),r="get"+s+"AtOutCoords",a=e.shapeInfo.logicalShape.length,i=t.logicalShape.length,o=p1(e.shapeInfo.logicalShape,t.logicalShape),u=rt(i),l=i-a,c,p=["x","y","z","w","u","v"];a===0?c="":i<2&&o.length>=1?c="coords = 0;":c=o.map(y=>`coords.${p[y+l]} = 0;`).join(`
`);let d="";i<2&&a>0?d="coords":d=e.shapeInfo.logicalShape.map((y,v)=>`coords.${p[v+l]}`).join(", ");let h="return outputValue;",m=w.sizeFromShape(e.shapeInfo.logicalShape)===1,b=w.sizeFromShape(t.logicalShape)===1;if(a===1&&!m&&!b)h=`
return vec4(outputValue.xy, outputValue.xy);
`;else if(m&&!b)i===1?h=`
return vec4(outputValue.x, outputValue.x, 0., 0.);
`:h=`
return vec4(outputValue.x);
`;else if(o.length){let y=a-2,v=a-1;o.indexOf(y)>-1&&o.indexOf(v)>-1?h="return vec4(outputValue.x);":o.indexOf(y)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":o.indexOf(v)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return`
vec4 ${r}() {
${u} coords = getOutputCoords();
${c}
vec4 outputValue = get${s}(${d});
${h}
}
`}function kK(e,t){let n=e.name,s=n.charAt(0).toUpperCase()+n.slice(1),r="get"+s+"AtOutCoords",a=t.texShape,i=e.shapeInfo.texShape,o=e.shapeInfo.logicalShape.length,u=t.logicalShape.length;if(!e.shapeInfo.isUniform&&o===u&&e.shapeInfo.flatOffset==null&&w.arraysEqual(i,a))return`
float ${r}() {
return sampleTexture(${n}, resultUV);
}
`;let l=rt(u),c=p1(e.shapeInfo.logicalShape,t.logicalShape),p=u-o,d,h=["x","y","z","w","u","v"];o===0?d="":u<2&&c.length>=1?d="coords = 0;":d=c.map(m=>`coords.${h[m+p]} = 0;`).join(`
`);let f="";return u<2&&o>0?f="coords":f=e.shapeInfo.logicalShape.map((m,g)=>`coords.${h[g+p]}`).join(", "),`
float ${r}() {
${l} coords = getOutputCoords();
${d}
return get${s}(${f});
}
`}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 mv(e,t,n){let{newShape:s,keptDims:r}=w.squeezeShape(t),a=t.length,i=e&&a===3&&t[0]===1,o=i?t.slice(1):s,u=!e&&a>1&&!w.arraysEqual(t,n)&&s.length<a||i;return{useSqueezeShape:u,uniformShape:u?o:t,keptDims:r}}function ru(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function au(e,t){return t.map(n=>e[n]).join(", ")}function IK(e,t,n,s){let r=n.map((c,p)=>{let d={logicalShape:c.shape,texShape:c.isUniform?null:c.texData.texShape,isUniform:c.isUniform,isPacked:c.isUniform?!1:c.texData.isPacked,flatOffset:null};return c.texData!=null&&c.texData.slice!=null&&c.texData.slice.flatOffset>0&&(d.flatOffset=c.texData.slice.flatOffset),{name:t.variableNames[p],shapeInfo:d}}),a=r.map(c=>c.shapeInfo),i={logicalShape:s.shape,texShape:s.texData.texShape,isUniform:!1,isPacked:s.texData.isPacked,flatOffset:null},o=V5(r,i,t),u=GC(e.gl,o),l=e.createProgram(u);return X().get("ENGINE_COMPILE_ONLY")?{program:t,fragmentShader:u,source:o,webGLProgram:l,inShapeInfos:a,outShapeInfo:i,uniformLocations:null,customUniformLocations:null,infLoc:null,nanLoc:null,inShapesLocations:null,inTexShapesLocations:null,outShapeLocation:null,outShapeStridesLocation:null,outTexShapeLocation:null}:{program:t,fragmentShader:u,source:o,webGLProgram:l,inShapeInfos:a,outShapeInfo:i,...m1(e,t,l)}}function m1(e,t,n){let s={},r={},a={},i=[],o,u,l,c=null,p=null;p=e.getUniformLocation(n,"NAN",!1),X().getNumber("WEBGL_VERSION")===1&&(c=e.getUniformLocation(n,"INFINITY",!1));let d=!1;for(let h=0;h<t.variableNames.length;h++){let f=t.variableNames[h];s[f]=e.getUniformLocation(n,f,d),s[`offset${f}`]=e.getUniformLocation(n,`offset${f}`,d),t.enableShapeUniforms&&(r[`${f}Shape`]=e.getUniformLocation(n,`${f}Shape`,d),a[`${f}TexShape`]=e.getUniformLocation(n,`${f}TexShape`,d))}return t.enableShapeUniforms&&(o=e.getUniformLocation(n,"outShape",d),l=e.getUniformLocation(n,"outShapeStrides",d),u=e.getUniformLocation(n,"outTexShape",d)),t.customUniforms&&t.customUniforms.forEach((h,f)=>{i[f]=e.getUniformLocation(n,h.name,d)}),{uniformLocations:s,customUniformLocations:i,infLoc:c,nanLoc:p,inShapesLocations:r,inTexShapesLocations:a,outShapeLocation:o,outShapeStridesLocation:l,outTexShapeLocation:u}}function rw(e,t){if(e.length!==t.length)throw Error(`Binary was compiled with ${e.length} inputs, but was executed with ${t.length} inputs`);e.forEach((n,s)=>{let r=n.logicalShape,a=t[s],i=a.shape;if(!w.arraysEqual(r,i))throw Error(`Binary was compiled with different shapes than the current args. Shapes ${r} and ${i} must match`);if(n.isUniform&&a.isUniform)return;let o=n.texShape,u=a.isUniform?null:a.texData.texShape;if(!w.arraysEqual(o,u))throw Error(`Binary was compiled with different texture shapes than the current args. Shape ${o} and ${u} must match`)})}function SK(e,t,n,s,r){t.program.enableShapeUniforms||(rw(t.inShapeInfos,n),rw([t.outShapeInfo],[s]));let a=s.texData.texture,i=s.texData.texShape;s.texData.isPacked?e.setOutputPackedMatrixTexture(a.texture,i[0],i[1]):e.setOutputMatrixTexture(a.texture,i[0],i[1]),e.setProgram(t.webGLProgram),X().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}=mv(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
ivec3 outCoordsFromFlatIndex(int index) {
${this.enableShapeUniforms?qp(["r","c","d"],e):yi(["r","c","d"],e)}
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${t.output} = result;
}
`}},TK=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let t=fn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
ivec3 outCoordsFromFlatIndex(int index) {
${this.enableShapeUniforms?qp(["r","c","d"],e):yi(["r","c","d"],e)}
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${t.output} = result;
}
`}},$K=class{constructor(e){this.variableNames=["A"],this.outTexUsage=3;let t=fn();this.outputShape=e,this.userCode=`
${d1}
void main() {
float x = getAAtOutCoords();
${t.output} = encode_float(x);
}
`}},_K=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=3;let t=fn();this.outputShape=e,this.userCode=`
${d1}
void main() {
ivec3 coords = getOutputCoords();
float x = getChannel(getAAtOutCoords(), vec2(coords.y, coords.z));
${t.output} = encode_float(x);
}
`}},AK=class{constructor(e,t=!1){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=fn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let s="result";t&&(s="floor(result * 255. + 0.5)"),this.userCode=`
${this.enableShapeUniforms?fv():hv(e)}
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-01 15:13:32 +02:00
result = values[3];
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
${n.output} = vec4(${s}, 0., 0., 0.);
}
`}},EK=class{constructor(e,t=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=fn();this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let s="",r="result";t&&(r="floor(result * 255. + 0.5)");for(let a=0;a<=1;a++)for(let i=0;i<=1;i++){let o=a*2+i;s+=`
localCoords = coords;
if(localCoords[2] + ${i} < ${this.enableShapeUniforms?"outShape[2]":`${e[2]}`}) {
localCoords[2] += ${i};
if (localCoords[1] + ${a} < ${this.enableShapeUniforms?"outShape[1]":`${e[1]}`}) {
localCoords[1] += ${a};
flatIndex = getFlatIndex(localCoords);
offset = imod(flatIndex, 4);
flatIndex = idiv(flatIndex, 4, 1.);
int r = flatIndex / texShape[1];
int c = imod(flatIndex, texShape[1]);
vec2 uv = (vec2(c, r) + halfCR) / vec2(texShape[1], texShape[0]);
values = ${n.texture2D}(A, uv);
if (offset == 0) {
result[${o}] = values[0];
} else if (offset == 1) {
result[${o}] = values[1];
} else if (offset == 2) {
result[${o}] = values[2];
2022-02-10 18:27:21 +01:00
} else {
2022-04-01 15:13:32 +02:00
result[${o}] = values[3];
2022-02-10 18:27:21 +01:00
}
}
}
2022-04-01 15:13:32 +02:00
`}this.userCode=`
${this.enableShapeUniforms?fv():hv(e)}
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},RK={};Ae(RK,{bindVertexProgramAttributeStreams:()=>S1,createBufferFromOutputTexture:()=>T1,createFloat16MatrixTexture:()=>x1,createFloat16PackedMatrixTexture:()=>I1,createFloat32MatrixTexture:()=>v1,createIndexBuffer:()=>y1,createPackedMatrixTexture:()=>k1,createUnsignedBytesMatrixTexture:()=>w1,createVertexBuffer:()=>b1,createVertexShader:()=>g1,downloadByteEncodedFloatMatrixFromOutputTexture:()=>_1,downloadFloat32MatrixFromBuffer:()=>$1,downloadMatrixFromPackedOutputTexture:()=>E1,downloadPackedMatrixFromBuffer:()=>A1,getInternalFormatForFloat16MatrixTexture:()=>bv,getInternalFormatForFloat16PackedMatrixTexture:()=>xv,getInternalFormatForFloat32MatrixTexture:()=>gv,getInternalFormatForPackedMatrixTexture:()=>vv,getInternalFormatForUnsignedBytesMatrixTexture:()=>yv,uploadDenseMatrixToTexture:()=>C1,uploadPixelDataToTexture:()=>N1});function g1(e){let t=fn(),n=`${t.version}
precision highp float;
${t.attribute} vec3 clipSpacePos;
${t.attribute} vec2 uv;
${t.varyingVs} vec2 resultUV;
void main() {
gl_Position = vec4(clipSpacePos, 1);
resultUV = uv;
}`;return UC(e,n)}function b1(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 jC(e,t)}function y1(e){let t=new Uint16Array([0,1,2,2,1,3]);return KC(e,t)}function Ql(e,t,n,s,r,a){YC(t,n);let i=XC(e),o=e.TEXTURE_2D;return fe(e,()=>e.bindTexture(o,i)),fe(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),fe(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),fe(e,()=>e.texParameteri(o,e.TEXTURE_MIN_FILTER,e.NEAREST)),fe(e,()=>e.texParameteri(o,e.TEXTURE_MAG_FILTER,e.NEAREST)),X().getNumber("WEBGL_VERSION")===1?fe(e,()=>e.texImage2D(o,0,s,t,n,0,r,a,null)):fe(e,()=>e.texStorage2D(o,1,s,t,n)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null)),{texture:i,texShape:[n,t]}}function gv(e){return e.internalFormatFloat}function v1(e,t,n,s){let[r,a]=Yl(t,n);return Ql(e,r,a,gv(s),s.textureFormatFloat,e.FLOAT)}function bv(e){return e.internalFormatHalfFloat}function x1(e,t,n,s){let[r,a]=Yl(t,n);return Ql(e,r,a,bv(s),s.textureFormatFloat,s.textureTypeHalfFloat)}function yv(e){return e.downloadTextureFormat}function w1(e,t,n,s){let[r,a]=Yl(t,n);return Ql(e,r,a,yv(s),e.RGBA,e.UNSIGNED_BYTE)}function vv(e){return e.internalFormatPackedFloat}function k1(e,t,n,s){let[r,a]=eu(t,n);return Ql(e,r,a,vv(s),e.RGBA,e.FLOAT)}function xv(e){return e.internalFormatPackedHalfFloat}function I1(e,t,n,s){let[r,a]=eu(t,n);return Ql(e,r,a,xv(s),e.RGBA,s.textureTypeHalfFloat)}function S1(e,t,n){return fe(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),Bm(e,t,"clipSpacePos",n,3,20,0)&&Bm(e,t,"uv",n,2,20,12)}function C1(e,t,n,s,r,a){fe(e,()=>e.bindTexture(e.TEXTURE_2D,t));let i,o,u;r instanceof Uint8Array?(i=new Uint8Array(n*s*4),o=e.UNSIGNED_BYTE,u=e.RGBA):(i=new Float32Array(n*s*4),o=e.FLOAT,u=a.internalFormatPackedFloat),i.set(r),X().getNumber("WEBGL_VERSION")===2?fe(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,s,e.RGBA,o,i)):fe(e,()=>e.texImage2D(e.TEXTURE_2D,0,u,n,s,0,e.RGBA,o,i)),fe(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function N1(e,t,n){fe(e,()=>e.bindTexture(e.TEXTURE_2D,t)),n.data instanceof Uint8Array?X().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)):X().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 T1(e,t,n,s){let r=e.createBuffer();fe(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,r));let o=4*4*t*n;return fe(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,o,e.STREAM_READ)),fe(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,0)),fe(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,null)),r}function $1(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 _1(e,t,n,s){let[r,a]=Yl(t,n),i=4,o=new Uint8Array(T5(t*n,i));return fe(e,()=>e.readPixels(0,0,r,a,s.downloadTextureFormat,e.UNSIGNED_BYTE,o)),new Float32Array(o.buffer)}function A1(e,t,n,s,r,a,i,o){let u=e,l=new Float32Array($5(a,i));return u.bindBuffer(u.PIXEL_PACK_BUFFER,t),u.getBufferSubData(u.PIXEL_PACK_BUFFER,0,l),u.bindBuffer(u.PIXEL_PACK_BUFFER,null),l}function E1(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 Kf=class{constructor(e){this.outputTexture=null,this.program=null,this.disposed=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[];let t=X().getNumber("WEBGL_VERSION");e!=null?(this.gl=e,S5(t,e)):this.gl=vs(t);let n="WEBGL_color_buffer_float",s="EXT_color_buffer_half_float";if(this.parallelCompilationExtension=this.gl.getExtension("KHR_parallel_shader_compile"),X().getNumber("WEBGL_VERSION")===1){let r="OES_texture_float",a="OES_texture_half_float";if(this.textureFloatExtension=Du(this.gl,r),Ln(this.gl,a))this.textureHalfFloatExtension=Du(this.gl,a);else if(X().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support half float textures, yet the environm
void main() {
setOutput(vec4(getA(), 0., 0., 0.));
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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=`
void main() {
${n} rc = getOutputCoords();
if(${s}) {
setOutput(vec4(0));
2022-02-10 18:27:21 +01:00
} else {
2022-04-01 15:13:32 +02:00
${r}
setOutput(vec4(${a}));
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +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]}),
rEdge || cEdge ? 0. : getA(${t[3]})`}},P1=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+=`
${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=`
${xX(t,this.enableShapeUniforms)}
${this.enableShapeUniforms?fv():hv(e)}
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);
}
`}};function xX(e,t){return`
ivec3 inputCoordsFromReshapedOutCoords(int index) {
${t?B5(["r","c","d"],"inputShape"):yi(["r","c","d"],e)}
return ivec3(r, c, d);
}
`}var wX=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=iw(t,n),r=ow(e,s,n);r in this.freeTextures||(this.freeTextures[r]=[]),r in this.usedTextures||(this.usedTextures[r]=[]);let a=aw(e,s,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[r].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=a,this.log();let o=this.freeTextures[r].shift();return this.usedTextures[r].push(o),o}let i;return s===3?i=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):s===4?i=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):s===1?i=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):s===0?i=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):s===2&&(i=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[r].push(i),this.numUsedTextures++,this._numBytesAllocated+=a,this.log(),i}releaseTexture(e,t,n,s){if(this.freeTextures==null)return;let r=iw(n,s),a=ow(t,r,s);a in this.freeTextures||(this.freeTextures[a]=[]);let i=aw(t,r,this.gpgpu.gl,this.gpgpu.textureConfig,s),o=X().get("WEBGL_DELETE_TEXTURE_THRESHOLD");o!==-1&&this._numBytesAllocated>o?(this.gpgpu.deleteMatrixTexture(e.texture),this._numBytesAllocated-=i):(this.freeTextures[a].push(e),this.numFreeTextures++,this._numBytesFree+=i),this.numUsedTextures--;let u=this.usedTextures[a],l=u.indexOf(e);if(l<0)throw new Error("Cannot release a texture that was never provided by this texture manager");u.splice(l,1),this.log()}log(){if(!this.logEnabled)return;let e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",`${this.numFreeTextures} / ${this.numUsedTextures}`,`(${e})`);let t=this._numBytesFree/this._numBytesAllocated;console.log(`Bytes allocated: ${this._numBytesAllocated}`),console.log(`Bytes unused: ${this._numBytesFree} (${Math.round(100*t)}%)`)}get numBytesAllocated(){return this._numBytesAllocated}get numBytesFree(){return this._numBytesFree}getNumUsedTextures(){return this.numUsedTextures}getNumFreeTextures(){return this.numFreeTextures}dispose(){if(this.freeTextures!=null){for(let e in this.freeTextures)this.freeTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t.texture)});for(let e in this.usedTextures)this.usedTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t.texture)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0}}};function kX(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 aw(e,t,n,s,r){let a=IX(t,s),i;if(r){let[u,l]=eu(e[0],e[1]);i=u*l}else{let[u,l]=Yl(e[0],e[1]);i=u*l}let o=kX(n,a);return i*o}function IX(e,t){switch(e){case 3:return vv(t);case 4:return xv(t);case 1:return gv(t);case 0:return bv(t);case 2:return yv(t);default:throw new Error(`Unknown physical texture type ${e}`)}}function SX(e){return X().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?3:1:e?4:0}function iw(e,t){if(e===1)return 3;if(e===0||e==null)return SX(t);if(e===3||e===2)return 2;throw new Error(`Unknown logical texture type ${e}`)}function ow(e,t,n){return`${e[0]}_${e[1]}_${t}_${n}`}var Hs=class{constructor(e,t){this.variableNames=["A"],this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
float unaryOperation(float x) {
${t}
}
void main() {
float x = getAAtOutCoords();
float y = unaryOperation(x);
setOutput(y);
}
`}},ss="if (isnan(x)) return x;",CX="return x;",uw="return abs(x);",NX="return (x >= 0.0) ? x : (exp(x) - 1.0);",TX=ss+`
return (x < 0.0) ? 0.0 : x;
`,$X=ss+`
return (x < 0.0) ? 0.0 : min(6.0, x);
`,Mi="return x;",_X="return 1.0 / (1.0 + exp(-1.0 * x));",AX="return x;",EX=`
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;
`,RX=`
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;
`,DX=`
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;
`,FX="return 1.0 / (1.0 + exp(-1.0 * x));",Zr=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
vec4 unaryOperation(vec4 x) {
${t}
}
void main() {
vec4 x = getAAtOutCoords();
vec4 y = unaryOperation(x);
setOutput(y);
}
`}},OX=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=yX(t,n),a=n.slice(-2),i=t<=1?"rc":`vec2(${a.join(",")})`;this.userCode=`
void main() {
${s} rc = getOutputCoords();
vec4 packedInput = getA(${r});
setOutput(getChannel(packedInput, ${i}));
}
`}},PX=xs.whereImpl,zX=1e-7,MX=1e-4,Zc={};function LX(e){return e in Zc||(Zc[e]={}),Zc[e]}var BX=X().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),VX=600;function WX(){return X().global.screen==null?1024:X().global.screen.height*X().global.screen.width*window.devicePixelRatio*VX/1024/1024}var z1=class extends tl{constructor(e){super();if(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,!X().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");let t;if(e!=null){if(e instanceof Kf)t=e;else{let n=vs(X().getNumber("WEBGL_VERSION"),e);t=new Kf(n)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{let n=vs(X().getNumber("WEBGL_VERSION"));t=new Kf(n),this.binaryCache=LX(X().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=t,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new wX(this.gpgpu),this.numMBBeforeWarning=WX(),this.texData=new Ud(this,Ss())}nextDataId(){return z1.nextDataId++}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}write(e,t,n){if((X().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||X().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(X().getBool("DEBUG")&&this.checkNumericalProblems(t),s==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(e,{shape:n,dtype:s,values:t,usage:1,refCount:r})}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}readSync(e){let t=this.texData.get(e),{values:n,dtype:s,complexTensorInfos:r,slice:a,shape:i,isPacked:o}=t;if(a!=null){let p;o?p=new Zr(i,Mi):p=new Hs(i,Mi);let d=this.runWebGLProgram(p,[{dataId:e,shape:i,dtype:s}],s),h=this.readSync(d.dataId);return this.disposeIntermediateTensorInfo(d),h}if(n!=null)return this.convertAndCacheOnCPU(e);if(s==="string")return n;let u=this.activeTimers!=null,l;u&&(l=w.now());let c;if(s==="complex64"){let p=this.readSync(r.real.dataId),d=this.readSync(r.imag.dataId);c=S.mergeRealAndImagArrays(p,d)}else c=this.getValuesFromTexture(e);return u&&(this.downloadWaitMs+=w.now()-l),this.convertAndCacheOnCPU(e,c)}async read(e){if(this.pendingRead.has(e)){let h=this.pendingRead.get(e);return new Promise(f=>h.push(f))}let t=this.texData.get(e),{values:n,shape:s,slice:r,dtype:a,complexTensorInfos:i,isPacked:o}=t;if(r!=null){let h;o?h=new Zr(s,Mi):h=new Hs(s,Mi);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(X().getBool("DEBUG")&&!X().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&X().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"&&X().get("WEBGL_BUFFER_SUPPORTED")){l=this.decode(e);let h=this.texData.get(l.dataId);u=this.gpgpu.createBufferFromTexture(h.texture.texture,...Yc(s))}this.pendingRead.set(e,[]),a!=="complex64"&&await this.gpgpu.createAndWaitForFence();let c;if(a==="complex64"){let h=await Promise.all([this.read(i.real.dataId),this.read(i.imag.dataId)]),f=h[0],m=h[1];c=S.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
if (isnan(a)) return a;
if (isnan(b)) return b;
`,so=class{constructor(e,t,n){this.variableNames=["A","B"],this.outputShape=S.assertAndGetBroadcastShape(t,n),this.enableShapeUniforms=Sn(this.outputShape.length),this.userCode=`
float binaryOperation(float a, float b) {
${e}
}
void main() {
float a = getAAtOutCoords();
float b = getBAtOutCoords();
setOutput(binaryOperation(a, b));
}
`}},jp=`
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;
`,Zl=class{constructor(e,t,n,s=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=S.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=`
result.y = 0.;
result.z = 0.;
result.w = 0.;
`;else if(a=`
${rt(r)} coords = getOutputCoords();
`,r===1)this.enableShapeUniforms?a+=`
result.y = (coords + 1) >= outShape ? 0. : result.y;
result.z = 0.;
result.w = 0.;
`:a+=`
result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
result.z = 0.;
result.w = 0.;
`;else{let o=ln("coords",r);this.enableShapeUniforms?a+=`
bool nextRowOutOfBounds =
(${o[r-2]} + 1) >= outShape[${r} - 2];
bool nextColOutOfBounds =
(${o[r-1]} + 1) >= outShape[${r} - 1];
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`:a+=`
bool nextRowOutOfBounds =
(${o[r-2]} + 1) >= ${this.outputShape[r-2]};
bool nextColOutOfBounds =
(${o[r-1]} + 1) >= ${this.outputShape[r-1]};
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`}this.userCode=`
vec4 binaryOperation(vec4 a, vec4 b) {
${e}
}
void main() {
vec4 a = getAAtOutCoords();
vec4 b = getBAtOutCoords();
vec4 result = binaryOperation(a, b);
${a}
setOutput(result);
}
`}};function kn(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 HX={kernelName:Ba,backendName:"webgl",kernelFunc:kn};function Rr(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.makeTensorInfo(s.shape,"complex64"),i=n.texData.get(a.dataId),o=kn({inputs:{x:s},backend:n}),u=kn({inputs:{x:r},backend:n});return i.complexTensorInfos={real:o,imag:u},a}var qX={kernelName:jd,backendName:"webgl",kernelFunc:Rr},B1="return (a < 0.) ? b * a : a;",V1=`
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
`;function jX(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{alpha:a}=s,i=n.makeTensorInfo([],"float32",w.createScalarValue(a,"float32")),o=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Zl(V1,r.shape,i.shape):new so(B1,r.shape,i.shape),u=n.runWebGLProgram(o,[r,i],"float32");return n.disposeIntermediateTensorInfo(i),u}var KX={kernelName:Va,backendName:"webgl",kernelFunc:jX},W1="return (a < 0.) ? b * a : a;",U1=`
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
`;function XX(e){let{inputs:t,backend:n}=e,{x:s,alpha:r}=t,a=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Zl(U1,s.shape,r.shape):new so(W1,s.shape,r.shape);return n.runWebGLProgram(a,[s,r],"float32")}var YX={kernelName:Ja,backendName:"webgl",kernelFunc:XX},iu="if (isnan(x)) return x;",QX=`
if (isnan(a)) return a;
if (isnan(b)) return b;
`,ZX=`
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;
`;function Ke({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:s}){return({inputs:r,backend:a})=>{let{x:i}=r,o=a,u=s||i.dtype;if(o.shouldExecuteOnCPU([i])&&n!=null){let p=o.texData.get(i.dataId),d=n(p.values,u);return o.makeTensorInfo(i.shape,u,d)}let l=X().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,c;return l?c=new Zr(i.shape,t):c=new Hs(i.shape,e),o.runWebGLProgram(c,[i],u)}}function jt({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:s=!1,cpuKernelImpl:r,dtype:a}){return({inputs:i,backend:o})=>{let{a:u,b:l}=i,c=o;if(s&&u.dtype==="complex64"){let f=c.texData.get(u.dataId),m=c.texData.get(l.dataId),[g,b]=[[f.complexTensorInfos.real,m.complexTensorInfos.real],[f.complexTensorInfos.imag,m.complexTensorInfos.imag]].map(v=>{let[x,k]=v,T={dataId:x.dataId,dtype:x.dtype,shape:u.shape},N={dataId:k.dataId,dtype:k.dtype,shape:l.shape},E=new so(e,u.shape,l.shape);return c.runWebGLProgram(E,[T,N],cn(x.dtype,k.dtype))}),y=Rr({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"?S.fromUint8ToStringArray(f):f,b=u.dtype==="string"?S.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=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,h;return d?h=new Zl(t,u.shape,l.shape,n):h=new so(e,u.shape,l.shape),c.runWebGLProgram(h,[u,l],p)}}function Kp(e,t=!1){if(e==="linear")return t?AX:CX;if(e==="relu")return t?RX:TX;if(e==="elu")return t?EX:NX;if(e==="relu6")return t?DX:$X;if(e==="prelu")return t?U1:W1;if(e==="leakyrelu")return t?V1:B1;if(e==="sigmoid")return t?FX:_X;throw new Error(`Activation ${e} has not been implemented for the WebGL backend.`)}var G1=class{constructor(e,t,n,s=!1,r=!1,a=!1,i=null,o=!1,u=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n,this.enableShapeUniforms=Sn(this.outputShape.length);let l=s?e[1]:e[2],c=Math.ceil(l/2),p=s?"i * 2, rc.y":"rc.y, i * 2",d=r?"rc.z, i * 2":"i * 2, rc.z",h=s?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],f=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],m="",g="";i&&(o?m=`vec4 activation(vec4 a) {
vec4 b = getPreluActivationWeightsAtOutCoords();
${i}
}`:u?m=`vec4 activation(vec4 a) {
vec4 b = getLeakyreluAlphaAtOutCoords();
${i}
}`:m=`vec4 activation(vec4 x) {
${i}
}`,g="result = activation(result);");let b=a?"result += getBiasAtOutCoords();":"";a&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),u&&this.variableNames.push("leakyreluAlpha");let y="rc.x",v="rc.x";e[0]<t[0]?y=`int(min(float(rc.x), ${e[0]-1}.))`:t[0]<e[0]&&(v=`int(min(float(rc.x), ${t[0]-1}.))`),this.userCode=`
${m}
// Don't use uniform for sharedDimensionPacked for performance.
const float sharedDimension = ${c}.0;
vec4 dot2x2ARowBCol(ivec3 rc) {
vec4 result = vec4(0);
for (int i = 0; i < ${c}; i++) {
int batchA = ${y};
int batchB = ${v};
vec4 a = getMatrixA(batchA, ${p});
vec4 b = getMatrixB(batchB, ${d});
// These swizzled products need to be separately added.
// See: https://github.com/tensorflow/tfjs/issues/1735
result += (${h[0]} * ${f[0]});
result += (${h[1]} * ${f[1]});
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
return result;
}
void main() {
ivec3 rc = getOutputCoords();
vec4 result = dot2x2ARowBCol(rc);
${b}
${g}
setOutput(result);
}
`}},lw={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},cw=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=S.assertAndGetBroadcastShape(t,n),this.userCode=`
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));
}
`}},dw="return a * b;";function kv(e){let{inputs:t,backend:n}=e,{a:s,b:r}=t,a=S.upcastType(s.dtype,r.dtype);if(s.dtype==="complex64"){let o=n.texData.get(s.dataId),u=n.texData.get(r.dataId),l=new cw(lw.REAL,s.shape,r.shape),c=new cw(lw.IMAG,s.shape,r.shape),p=[{dataId:o.complexTensorInfos.real.dataId,dtype:o.complexTensorInfos.real.dtype,shape:s.shape},{dataId:o.complexTensorInfos.imag.dataId,dtype:o.complexTensorInfos.imag.dtype,shape:s.shape},{dataId:u.complexTensorInfos.real.dataId,dtype:u.complexTensorInfos.real.dtype,shape:r.shape},{dataId:u.complexTensorInfos.imag.dataId,dtype:u.complexTensorInfos.imag.dtype,shape:r.shape}],d=n.runWebGLProgram(l,p,"float32"),h=n.runWebGLProgram(c,p,"float32"),f=Rr({inputs:{real:d,imag:h},backend:n});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}if(n.shouldExecuteOnCPU([s,r])){let o=n.texData.get(s.dataId),u=n.texData.get(r.dataId),[l,c]=JK(s.shape,r.shape,o.values,u.values,a),p=n.makeTensorInfo(c,a),d=n.texData.get(p.dataId);return d.values=l,p}let i;return X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?i=new Zl(dw,s.shape,r.shape):i=new so(dw,s.shape,r.shape),n.runWebGLProgram(i,[s,r],a)}var JX={kernelName:Ya,backendName:"webgl",kernelFunc:kv};function e8(e,t,n){let s=[ba(e.shape),...ya(e.shape)],r={dtype:e.dtype,shape:s,dataId:e.dataId},a=[ba(t),...ya(t)],i=new P1(a,s),o=!0,u=[s],l=n.runWebGLProgram(i,[r],e.dtype,u,o);return{dataId:l.dataId,shape:t,dtype:l.dtype}}function he(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{shape:a}=s,i=n,o=w.sizeFromShape(r.shape),u=w.inferFromImplicitShape(a,o),l=w.sizeFromShape(u);w.assert(o===l,()=>`The new shape (${u}) has ${l} elements and the old shape (${r.shape}) has ${o} elements. The new shape and old shape must have the same number of elements.`);let c=i.texData.get(r.dataId);return c.isPacked&&!Ju(r.shape,u)&&!(c.texture!==null&&Ju(c.shape,u))?e8(r,u,i):(i.incRef(r.dataId),{dataId:r.dataId,shape:u,dtype:r.dtype})}var t8={kernelName:Ao,backendName:"webgl",kernelFunc:he},pw=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:s,inSize:r,outSize:a}=e;this.outputShape=[s,a];let i=Math.floor(n/4)*4,o=n%4,u="sumValue += dot(values, ones);";if(t!=null){let c=1/t;u=`sumValue += dot(values * ${w.isInt(c)?c.toPrecision(2):c}, ones);`}let l="";r%n>0&&(l=`
if (inIdx < 0 || inIdx >= ${r}) {
return 0.0;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`),this.userCode=`
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
${l}
return getX(batch, inIdx);
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${n};
float sumValue = 0.0;
for (int i = 0; i < ${i}; i += 4) {
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
${u}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
int inIdx = inOffset + ${i};
if (${o===1}) {
vec4 values = vec4(getValue(batch, inIdx), 0.0, 0.0, 0.0);
${u}
} else if (${o===2}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1), 0.0, 0.0);
${u}
} else if (${o===3}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2), 0.0);
${u}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
setOutput(sumValue);
}
`}},n8=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:s,inSize:r,outSize:a}=e;this.outputShape=[s,a];let i="0.0",o="";t==="prod"?i="1.0":t==="min"?(i="1.0 / 1e-20",o="min"):t==="max"&&(i="-1.0 / 1e-20",o="max");let u=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="sum"?u="sumValue":t==="prod"?u="prodValue":t==="all"?u="allValue":t==="any"&&(u="anyValue");let l=Math.floor(n/4)*4,c=n%4,p=`
if (${t==="sum"}) {
sumValue += dot(values, ones);
} else if (${t==="prod"}) {
vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);
prodValue *= tmp[0] * tmp[1];
} else {
minMaxValue = ${o}(values, minMaxValue);
if (${t==="min"} || ${t==="max"}) {
minMaxValue = ${o}(values, minMaxValue);
bvec4 isNaN = isnan(values);
if (isNaN.r || isNaN.g || isNaN.b || isNaN.a) {
minMaxValue = vec4(NAN);
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
}
`,d="vec4";t==="all"?(i="1.0",p=`
bool reducedAllValue = all(values);
float floatedReducedAllValue = float(reducedAllValue);
allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
`,d="bvec4"):t==="any"&&(i="0.0",p=`
bool reducedAnyValue = any(values);
float floatedReducedAnyValue = float(reducedAnyValue);
anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);
`,d="bvec4");let h="";r%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${r}) {
return initializationValue;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`),this.userCode=`
const float initializationValue = ${i};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
${h}
return getX(batch, inIdx);
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${n};
vec4 minMaxValue = vec4(${i});
float prodValue = 1.0;
float sumValue = 0.0;
float allValue = 1.0;
float anyValue = 0.0;
for (int i = 0; i < ${l}; i += 4) {
int inIdx = inOffset + i;
${d} values = ${d}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
${p}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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});
}
`}};function s8(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=S.computeOptimalWindowSize(n);t.push({inSize:n,windowSize:s,outSize:Math.ceil(n/s)})}return t}function xi(e,t,n,s){let r=s8(e.shape),a=e;for(let i=0;i<r.length;i++){let{inSize:o,windowSize:u,outSize:l}=r[i],c,p;n==="mean"?c=i===0?new pw({windowSize:u,inSize:o,batchSize:e.shape[0],outSize:l},o):new pw({windowSize:u,inSize:o,batchSize:e.shape[0],outSize:l}):c=new n8({windowSize:u,inSize:o,batchSize:e.shape[0],outSize:l},n),p=a,a=s.runWebGLProgram(c,[a],t),p.dataId!==e.dataId&&s.disposeIntermediateTensorInfo(p)}return a}var r8=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=a8(t);this.userCode=`
void main() {
${s} resRC = getOutputCoords();
setOutput(getA(${r}));
}
`}};function a8(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 i8=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=O1("rc",this.rank),a=new Array(this.rank);for(let l=0;l<t.length;l++)a[t[l]]=r[l];let i=`vec2(${a.slice(-2).join()})`,o=`++${r[this.rank-1]} < ${n[this.rank-1]}`,u=`getChannel(getA(${a.join()}), ${i})`;this.userCode=`
void main() {
${s} rc = getOutputCoords();
vec4 result = vec4(0.);
result[0] = ${u};
if(${o}) {
result[1] = ${u};
}
--${r[this.rank-1]};
if(++${r[this.rank-2]} < ${n[this.rank-2]}) {
result[2] = ${u};
if(${o}) {
result[3] = ${u};
}
}
setOutput(result);
}
`}};function Xp(e,t,n){let s=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new i8(e.shape,t):new r8(e.shape,t);return n.runWebGLProgram(s,[e],e.dtype)}function o8(e,t,n,s){let r=t,a=e.shape.length,i=w.parseAxisParam(r,e.shape),o=i,u=S.getAxesPermutation(o,a),l=u!=null,c=e;l&&(c=Xp(e,u,s),o=S.getInnerMostAxes(o.length,a)),S.assertAxesAreInnerMostDims("sum",o,a);let[p,d]=S.computeOutAndReduceShapes(c.shape,o),h=p;n&&(h=S.expandShapeToKeepDim(p,i));let f=w.sizeFromShape(d),g=w.sizeFromShape(e.shape)/f,b=he({inputs:{x:c},attrs:{shape:[g,f]},backend:s}),y=cp(e.dtype),v=xi(b,y,"sum",s),x=he({inputs:{x:v},attrs:{shape:h},backend:s});return s.disposeIntermediateTensorInfo(b),s.disposeIntermediateTensorInfo(v),l&&s.disposeIntermediateTensorInfo(c),x}function Yp(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return o8(r,a,i,n)}var u8={kernelName:oi,backendName:"webgl",kernelFunc:Yp};function qt(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{perm:a}=s,i=n,o=r.shape.length,u=new Array(o);for(let c=0;c<u.length;c++)u[c]=r.shape[a[c]];let l;if(i.shouldExecuteOnCPU([r])){let p=i.texData.get(r.dataId).values,d=wv(p,r.shape,r.dtype,a,u);l=i.makeTensorInfo(u,r.dtype);let h=i.texData.get(l.dataId);h.values=d}else l=Xp(r,a,i);return l}var l8={kernelName:pi,backendName:"webgl",kernelFunc:qt},H1=1e3;function zd({a:e,b:t,transposeA:n,transposeB:s,backend:r,bias:a=null,preluActivationWeights:i=null,leakyreluAlpha:o=0,activation:u=null}){let l=e.shape.length,c=t.shape.length,p=n?e.shape[l-2]:e.shape[l-1],d=s?t.shape[c-1]:t.shape[c-2],h=n?e.shape[l-1]:e.shape[l-2],f=s?t.shape[c-2]:t.shape[c-1],m=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(m),y=w.sizeFromShape(g),x=qo.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],T=s?[y,f,d]:[y,d,f],N=he({inputs:{x:e},backend:r,attrs:{shape:k}}),E=he({inputs:{x:t},backend:r,attrs:{shape:T}}),A=[N,E],P=Math.max(b,y),R=n?N.shape[1]:N.shape[2],F=a!=null,$=i!=null,z=u==="leakyrelu",W=u!=null?Kp(u,!0):null,q=F||$||z||W!=null,K;if((h===1||f===1)&&R>H1&&q===!1){let Z=N,te=E;n&&(Z=qt({inputs:{x:N},backend:r,attrs:{perm:[0,2,1]}}),A.push(Z)),s&&(te=qt({inputs:{x:E},backend:r,attrs:{perm:[0,2,1]}}),A.push(te));let ee=f!==1,se=f===1,ne=Z;ee&&(ne=he({inputs:{x:Z},backend:r,attrs:{shape:[P,R,1]}}),A.push(ne));let oe=f===1?2:1,re=te;se&&(re=he({inputs:{x:te},backend:r,attrs:{shape:[P,1,R]}}),A.push(re));let le=kv({inputs:{a:ne,b:re},backend:r});K=Yp({inputs:{x:le},backend:r,attrs:{axis:oe,keepDims:!0}}),A.push(le)}else{let Z=cn(e.dtype,t.dtype),te=new G1(k,T,[P,h,f],n,s,F,W,$,z),ee=[N,E];if(a!=null&&ee.push(a),$&&ee.push(i),z){let se=r.makeTensorInfo([],"float32",w.createScalarValue(o,"float32"));ee.push(se),A.push(se)}K=r.runWebGLProgram(te,ee,Z)}let Y=he({inputs:{x:K},backend:r,attrs:{shape:x}});A.push(K);for(let Z of A)r.disposeIntermediateTensorInfo(Z);return Y}function c8(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a,bias:i,preluActivationWeights:o}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=s;return zd({a:r,b:a,transposeA:u,transposeB:l,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:p,activation:c})}var d8={kernelName:ra,backendName:"webgl",kernelFunc:c8},hw="return abs(x);";function p8(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])&&s.dtype!=="complex64"){let a=n.texData.get(s.dataId),i=D1(a.values);return n.makeTensorInfo(s.shape,s.dtype,i)}let r;return X().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new Zr(s.shape,hw):r=new Hs(s.shape,hw),n.runWebGLProgram(r,[s],s.dtype)}var h8={kernelName:ao,backendName:"webgl",kernelFunc:p8},f8=ss+`
if (abs(x) > 1.) {
return NAN;
}
return acos(x);
`,m8=Ke({opSnippet:f8}),g8={kernelName:nl,backendName:"webgl",kernelFunc:m8},b8=ss+`
if (x < 1.0) return NAN;
return log(x + sqrt(x * x - 1.0));`,y8=Ke({opSnippet:b8}),v8={kernelName:sl,backendName:"webgl",kernelFunc:y8},fw="return a + b;",x8=jt({opSnippet:fw,packedOpSnippet:fw,supportsComplex:!0,cpuKernelImpl:FK}),w8={kernelName:Ir,backendName:"webgl",kernelFunc:x8},k8=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=`
void main() {
${n.join(`
`)}
float result = ${s};
setOutput(result);
}
`}},I8=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=`
void main() {
${n.join(`
`)}
vec4 result = ${s};
setOutput(result);
}
`}};function ld(e){let{inputs:t,backend:n}=e,s=t;if(s.length===1)return kn({inputs:{x:s[0]},backend:n});if(s.length>X().get("WEBGL_MAX_TEXTURES_IN_SHADER")){let u=Math.floor(s.length/2),l=ld({inputs:s.slice(0,u),backend:n}),c=ld({inputs:s.slice(u),backend:n});return ld({inputs:[l,c],backend:n})}let r=s.map(u=>u.dtype).reduce((u,l)=>cn(u,l)),a=s.map(u=>u.shape),o=X().getBool("WEBGL_PACK")?new I8(s[0].shape,a):new k8(s[0].shape,a);return n.runWebGLProgram(o,s,r)}var S8={kernelName:ka,backendName:"webgl",kernelFunc:ld};function C8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=S.getAxesPermutation(l,o),p=r;c!=null&&(p=qt({inputs:{x:r},backend:n,attrs:{perm:c}}),l=S.getInnerMostAxes(l.length,o)),S.assertAxesAreInnerMostDims("all",l,o);let[d,h]=S.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=he({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=xi(m,m.dtype,"all",n),b;if(i){let y=S.expandShapeToKeepDim(d,u);b=he({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=he({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),b}var N8={kernelName:rl,backendName:"webgl",kernelFunc:C8};function T8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=S.getAxesPermutation(l,o),p=r;c!=null&&(p=qt({inputs:{x:r},backend:n,attrs:{perm:c}}),l=S.getInnerMostAxes(l.length,o)),S.assertAxesAreInnerMostDims("any",l,o);let[d,h]=S.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=he({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=xi(m,m.dtype,"any",n),b;if(i){let y=S.expandShapeToKeepDim(d,u);b=he({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=he({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),b}var $8={kernelName:al,backendName:"webgl",kernelFunc:T8},_8=class{constructor(e,t,n){this.variableNames=["A"];let{windowSize:s,batchSize:r,outSize:a}=e;n||this.variableNames.push("bestIndicesA"),this.outputShape=[r,a];let i=t==="max"?">":"<",o=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode=`
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${s};
int bestIndex = inOffset;
float bestValue = getA(batch, bestIndex);
for (int i = 0; i < ${s}; i++) {
int inIdx = ${o};
float candidate = getA(batch, inIdx);
if (candidate ${i} bestValue) {
bestValue = candidate;
bestIndex = inIdx;
2022-02-10 18:27:21 +01:00
}
}
2022-04-01 15:13:32 +02:00
setOutput(float(bestIndex));
}
`}},A8=class{constructor(e,t,n,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,w.assert(e.length>2,()=>`Packed arg${n.charAt(0).toUpperCase()+n.slice(1)} supports only inputs with rank above 2.`);let r=e[e.length-1],a=Math.ceil(r/t);this.outputShape=e.slice(0,-1),a>1&&this.outputShape.push(a),s||this.variableNames.push("bestIndicesA");let i=this.outputShape,o=i.length,u=rt(o),l=ln("coords",o),c,p;if(a===1){p=o+1;let N=rt(p);c=`
${N} sourceLocR = ${N}(${l.join()}, 0);
++${l[o-1]};
${N} sourceLocG = ${N}(${l.join()}, 0);
++${l[o-2]};
${N} sourceLocA = ${N}(${l.join()}, 0);
--${l[o-1]};
${N} sourceLocB = ${N}(${l.join()}, 0);
--${l[o-2]};`}else p=o,c=`
${u} sourceLocR = coords;
++${l[o-1]};
${u} sourceLocG = coords;
++${l[o-2]};
${u} sourceLocA = coords;
--${l[o-1]};
${u} sourceLocB = coords;
--${l[o-2]};`;let d=["x","y","z","w","u","v"].slice(0,p),h="."+d[p-1],f=d.map(N=>"int "+N),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?"":`
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.,
hasNextRow && hasNextCol ? getAChannel(${y.join()}) : 0.)`,T=s?"":`
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()}));
}
${T}
void main() {
${u} coords = getOutputCoords();
bool hasNextCol = ${l[o-1]} < ${i[o-1]-1};
bool hasNextRow = ${l[o-2]} < ${i[o-2]-1};
${c}
ivec4 srcIdx = ivec4(sourceLocR${h}, sourceLocG${h},
sourceLocB${h}, sourceLocA${h}) * ${t};
ivec4 inIdx = srcIdx;
vec4 bestIndex = vec4(inIdx);
vec4 bestValue = ${k};
for (int i = 0; i < ${t}; i++) {
inIdx = srcIdx;
${x}
vec4 candidate = ${k};
bvec4 nan = isnan(candidate);
bvec4 replace = bvec4(
vec4(${v}(candidate, bestValue)) * (vec4(1.0) - vec4(nan)));
bestValue = vec4(replace.x ? candidate.x : bestValue.x,
replace.y ? candidate.y : bestValue.y,
replace.z ? candidate.z : bestValue.z,
replace.w ? candidate.w : bestValue.w);
bestIndex = mix(bestIndex, vec4(inIdx), vec4(replace));
srcIdx++;
}
setOutput(bestIndex);
}
`}};function q1(e,t,n,s=null){let r=t.shape[0],a=t.shape[1];s!=null&&(r=s.shape[0],a=s.shape[1]);let i=S.computeOptimalWindowSize(a),o={windowSize:i,inSize:a,batchSize:r,outSize:Math.ceil(a/i)},u=new _8(o,n,s==null),l=[t];s!=null&&l.push(s);let c=e.runWebGLProgram(u,l,"int32");if(c.shape[1]===1)return c;let p=q1(e,t,n,c);return e.disposeIntermediateTensorInfo(c),p}function j1(e,t,n,s=null){let r=s!=null?s.shape:t.shape,a=r[r.length-1],i=S.computeOptimalWindowSize(a),o=new A8(r,i,n,s==null),u=s==null?[t]:[t,s],l=e.runWebGLProgram(o,u,"int32");if(l.shape.length===t.shape.length){let c=j1(e,t,n,l);return e.disposeIntermediateTensorInfo(l),c}return l}function K1(e,t,n,s){let r=[n];if(S.assertAxesAreInnerMostDims("arg"+s.charAt(0).toUpperCase()+s.slice(1),r,t.shape.length),!X().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let a=[],i=e.texData.get(t.dataId),o=i!==null&&i.isPacked,u=t;o&&(u=e.unpackTensor(t),a.push(u));let[l,c]=S.computeOutAndReduceShapes(u.shape,r),p=w.sizeFromShape(c),d=he({inputs:{x:u},backend:e,attrs:{shape:[-1,p]}});a.push(d);let h=q1(e,d,s);a.push(h);let f=he({inputs:{x:h},backend:e,attrs:{shape:l}});return a.forEach(m=>e.disposeIntermediateTensorInfo(m)),f}return j1(e,t,s)}function E8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=S.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=qt({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=S.getInnerMostAxes(i.length,u.shape.length)),S.assertAxesAreInnerMostDims("argMax",[i[0]],u.shape.length);let c=K1(n,u,i[0],"max");return l.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var R8={kernelName:Ia,backendName:"webgl",kernelFunc:E8};function D8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=S.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=qt({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=S.getInnerMostAxes(i.length,u.shape.length)),S.assertAxesAreInnerMostDims("argMin",[i[0]],u.shape.length);let c=K1(n,u,i[0],"min");return l.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var F8={kernelName:il,backendName:"webgl",kernelFunc:D8},O8=ss+`
if (abs(x) > 1.) {
return NAN;
}
return asin(x);
`,P8=Ke({opSnippet:O8}),z8={kernelName:ol,backendName:"webgl",kernelFunc:P8},M8=ss+"return log(x + sqrt(x * x + 1.0));",L8=Ke({opSnippet:M8}),B8={kernelName:ul,backendName:"webgl",kernelFunc:L8},V8=ss+`
return atan(x);
`,W8=Ke({opSnippet:V8}),U8={kernelName:ll,backendName:"webgl",kernelFunc:W8},G8=QX+`
return atan(a, b);
`,H8=`
vec4 result = atan(a, b);
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
`+ZX+`
return result;
`,q8=jt({opSnippet:G8,packedOpSnippet:H8}),j8={kernelName:dl,backendName:"webgl",kernelFunc:q8},K8=ss+`
if ((x < -1.0) || (x > 1.0)) return NAN;
return (log(1.0 + x) - log(1.0 - x)) / 2.0;`,X8=Ke({opSnippet:K8}),Y8={kernelName:cl,backendName:"webgl",kernelFunc:X8},el=class{constructor(e,t,n,s=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,i=e.strideHeight,o=e.strideWidth,u=e.dilationHeight,l=e.dilationWidth,c=e.effectiveFilterHeight,p=e.effectiveFilterWidth,d=e.padInfo.top,h=e.padInfo.left;this.outputShape=e.outShape;let f=t==="avg",m=`((batch * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + d`,g=`(xR * ${e.inWidth} + xC) * ${e.inChannels} + d`,b="0.0";if(f||(b="-1.0 / 1e-20"),n){let N=">=";this.userCode=`
const ivec2 strides = ivec2(${i}, ${o});
const ivec2 pads = ivec2(${d}, ${h});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d = coords[3];
ivec2 xRCCorner = coords.yz * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
float minMaxValue = 0.0;
float minMaxValueFound = 0.0;
int minMaxPosition = 0;
float avgValue = 0.0;
for (int wR = 0; wR < ${c};
wR += ${u}) {
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
2022-02-10 18:27:21 +01:00
continue;
}
2022-04-01 15:13:32 +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 ${N} currMinMaxValue) {
minMaxValue = value;
minMaxValueFound = 1.0;
minMaxPosition = ${s?r?m:g:`wR * ${p} + wC`};
2022-02-10 18:27:21 +01:00
}
}
}
2022-04-01 15:13:32 +02:00
setOutput(float(minMaxPosition));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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,T=`
if (${f}) {
avgValue += dot(values, ones);
} else {
minMaxValue = ${y}(values, minMaxValue);
}
`;this.userCode=`
const ivec2 strides = ivec2(${i}, ${o});
const ivec2 pads = ivec2(${d}, ${h});
const float initializationValue = ${b};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float count = 0.0;
float getValue(int batch, int xR, int xC, int d) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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)
);
${T}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
int xC = xCCorner + ${x};
if (${k===1}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
initializationValue,
initializationValue,
initializationValue
);
${T}
} else if (${k===2}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
getValue(batch, xR, xC + ${l}, d),
initializationValue,
initializationValue
);
${T}
} 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
);
${T}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
setOutput(${v});
}
`}},Iv=class{constructor(e,t,n,s=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,i=e.strideDepth,o=e.strideHeight,u=e.strideWidth,l=e.dilationDepth,c=e.dilationHeight,p=e.dilationWidth,d=e.effectiveFilterDepth,h=e.effectiveFilterHeight,f=e.effectiveFilterWidth,m=e.padInfo.front,g=e.padInfo.top,b=e.padInfo.left;this.outputShape=e.outShape;let y=t==="avg",v="0.0";if(y||(v="-1.0 / 1e-20"),n){let A=">=";this.userCode=`
const ivec3 strides =
ivec3(${i}, ${o}, ${u});
const ivec3 pads = ivec3(${m}, ${g}, ${b});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
// max/min x(?, ?, ?, ch) to get y(yD, yR, yC, ch).
// ? = to be determined
float minMaxValue = 0.0;
float minMaxValueFound = 0.0;
int minMaxPosition = 0;
for (int wD = 0; wD < ${d};
wD += ${l}) {
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
for (int wR = 0; wR < ${h};
wR += ${c}) {
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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);
if (value ${A} currMinMaxValue) {
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-01 15:12:04 +02:00
}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(float(minMaxPosition));
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;return}let x="max",k=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(k="avgValue / count");let T=Math.floor(a/4)*4,N=a%4,E=`
if (${y}) {
avgValue += dot(values, ones);
} else {
minMaxValue = ${x}(values, minMaxValue);
}
`;this.userCode=`
const ivec3 strides =
ivec3(${i}, ${o}, ${u});
const ivec3 pads = ivec3(${m}, ${g}, ${b});
const float initializationValue = ${v};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float count = 0.0;
float getValue(int batch, int xD, int xR, int xC, int ch) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:13:32 +02:00
for (int wC = 0; wC < ${T}; wC += 4) {
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)
);
${E}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
int xC = xCCorner + ${T};
if (${N===1}) {
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
initializationValue,
initializationValue,
initializationValue
);
${E}
} else if (${N===2}) {
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${p}, ch),
initializationValue,
initializationValue
);
${E}
} else if (${N===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
);
${E}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(${k});
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
`}};function Q8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t;tu(r,"avgPool");let{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1;w.assert(S.eitherStridesOrDilationsAreOne(i,l),()=>`Error in avgPool: Either strides or dilations must be 1. Got strides ${i} and dilations '${l}'`);let c=S.computePool2DInfo(r.shape,a,i,l,o,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return kn({inputs:{x:r},backend:n});let p=new el(c,"avg",!1);return n.runWebGLProgram(p,[r],"float32")}var Z8={kernelName:Sa,backendName:"webgl",kernelFunc:Q8};function J8(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dimRoundingMode:u,dataFormat:l}=s,c=[1,1,1],p=S.computePool3DInfo(r.shape,a,i,c,o,u,l),d=new Iv(p,"avg",!1);return n.runWebGLProgram(d,[r],"float32")}var eY={kernelName:qd,backendName:"webgl",kernelFunc:J8},tY=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,s=e.strideHeight,r=e.strideWidth,a=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterHeight,u=e.effectiveFilterWidth,l=o-1-e.padInfo.top,c=u-1-e.padInfo.left,p=1/(t*n);this.userCode=`
const ivec2 pads = ivec2(${l}, ${c});
const float avgMultiplier = float(${p});
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${o};
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${s}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
int idyR = int(dyR);
for (int wC = 0; wC < ${u};
wC+= ${i}) {
float dyC = float(dyCCorner + wC) / ${r}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
dotProd += dyValue * avgMultiplier;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
setOutput(dotProd);
}
`}},nY=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,s=e.filterWidth,r=e.strideDepth,a=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,u=e.dilationHeight,l=e.dilationWidth,c=e.effectiveFilterDepth,p=e.effectiveFilterHeight,d=e.effectiveFilterWidth,h=c-1-e.padInfo.front,f=p-1-e.padInfo.top,m=d-1-e.padInfo.left,g=1/(t*n*s);this.userCode=`
const ivec3 pads = ivec3(${h}, ${f}, ${m});
const float avgMultiplier = float(${g});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyDCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
// Convolve dy(?, ?, ?, d) with pos mask(:, :, :, ch) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wD = 0; wD < ${c};
wD += ${o}) {
float dyD = float(dyDCorner + wD) / ${r}.0;
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
int idyR = int(dyR);
for (int wC = 0; wC < ${d};
wC += ${l}) {
float dyC = float(dyCCorner + wC) / ${i}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
dotProd += dyValue * avgMultiplier;
2022-02-10 18:27:21 +01:00
}
}
}
2022-04-01 15:13:32 +02:00
setOutput(dotProd);
}
`}};function sY(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,i=a,{filterSize:o,strides:u,pad:l,dimRoundingMode:c}=s,p=[1,1,1],d=S.computePool3DInfo(i.shape,o,u,p,l,c),h=new nY(d);return n.runWebGLProgram(h,[r],i.dtype)}var rY={kernelName:og,backendName:"webgl",kernelFunc:sY};function aY(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,i=a;tu([r,a],"avgPoolGrad");let{filterSize:o,strides:u,pad:l}=s,c=S.computePool2DInfo(i.shape,o,u,1,l),p=new tY(c);return n.runWebGLProgram(p,[r],i.dtype)}var iY={kernelName:ig,backendName:"webgl",kernelFunc:aY};function oY(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a}=t,{transposeA:i,transposeB:o}=s;return zd({a:r,b:a,transposeA:i,transposeB:o,backend:n})}var uY={kernelName:Ca,backendName:"webgl",kernelFunc:oY},lY=class{constructor(e,t,n,s,r,a){this.outputShape=[],this.variableNames=["x","mean","variance"],S.assertAndGetBroadcastShape(e,t),S.assertAndGetBroadcastShape(e,n);let i="0.0";s!=null&&(S.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="1.0";r!=null&&(S.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
void main() {
float x = getXAtOutCoords();
float mean = getMeanAtOutCoords();
float variance = getVarianceAtOutCoords();
float offset = ${i};
float scale = ${o};
float inv = scale * inversesqrt(variance + float(${a}));
setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));
}
`}},cY=class{constructor(e,t,n,s,r,a){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],S.assertAndGetBroadcastShape(e,t),S.assertAndGetBroadcastShape(e,n);let i="vec4(0.0)";s!=null&&(S.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="vec4(1.0)";r!=null&&(S.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
void main() {
vec4 offset = ${i};
vec4 scale = ${o};
vec4 x = getXAtOutCoords();
vec4 mean = getMeanAtOutCoords();
vec4 variance = getVarianceAtOutCoords();
vec4 inv = scale * inversesqrt(variance + vec4(${a}));
setOutput((x - mean) * inv + offset);
}
`}},dY=({inputs:e,backend:t,attrs:n})=>{let{x:s,mean:r,variance:a,offset:i,scale:o}=e;w.assert(r.shape.length===a.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),w.assert(i==null||r.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),w.assert(o==null||r.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:u}=n;u==null&&(u=.001);let l=[s,r,a],c=null;i!=null&&(c=i.shape,l.push(i));let p=null;o!=null&&(p=o.shape,l.push(o));let d=X().getBool("WEBGL_PACK_NORMALIZATION")?new cY(s.shape,r.shape,a.shape,c,p,u):new lY(s.shape,r.shape,a.shape,c,p,u);return t.runWebGLProgram(d,l,l[0].dtype)},pY={kernelName:Ma,backendName:"webgl",kernelFunc:dY},hY=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=fY(this.rank),s,r=e.map((a,i)=>`sourceLoc.${Gm[i]} = start[${i}] + coords.${Gm[i]};`);s=`
${t} sourceLoc;
${t} coords = getOutputCoords();
${r.join(`
`)}
`,this.userCode=`
void main() {
${s}
setOutput(getSource(${n}));
}
`}},Gm=["x","y","z","w","u","v"];function fY(e){if(e===1)return"sourceLoc";if(e<=6)return Gm.slice(0,e).map(t=>"sourceLoc."+t).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}var mY=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})`,i=`
result.x = ${a};
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
++${s[this.rank-1]};
result.y = ${a};
--${s[this.rank-1]};
}
`,o=this.rank===1?"":`
--${n[this.rank-1]};
if (++${n[this.rank-2]} < ${e[this.rank-2]}) {
++${s[this.rank-2]};
result.z = ${a};
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
++${s[this.rank-1]};
result.w = ${a};
}
}
`,u=this.rank<=4?`sourceLoc = coords +
${t}(${e.map((l,c)=>`start[${c}]`).join()});`:e.map((l,c)=>`${s[c]} = ${n[c]} + start[${c}];`).join(`
`);this.userCode=`
void main() {
${t} coords = getOutputCoords();
${t} sourceLoc;
${u}
vec4 result = vec4(0.);
${i}
${o}
setOutput(result);
}
`}};function gY(e,t,n,s){let r=s.texData.get(e.dataId),a=s.makeTensorInfo(n,e.dtype),i=s.texData.get(a.dataId);Object.assign(i,r),i.refCount=1,i.shape=n,i.dtype=e.dtype;let o=wt.computeFlatOffset(t,w.computeStrides(e.shape));r.slice&&(o+=r.slice.flatOffset),i.slice={flatOffset:o,origDataId:r.slice&&r.slice.origDataId||e.dataId};let u=s.dataRefCount.get(i.slice.origDataId)||1;return s.dataRefCount.set(i.slice.origDataId,u+1),a}function ou(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,size:i}=s,[o,u]=wt.parseSliceParams(r,a,i);if(wt.assertParamsValid(r,o,u),w.sizeFromShape(u)===0)return n.makeTensorInfo(u,r.dtype,[]);if(n.shouldExecuteOnCPU([r])||r.dtype==="string"){let p=n.texData.get(r.dataId),d=iX(p.values,o,u,r.shape,r.dtype);return n.makeTensorInfo(u,r.dtype,d)}let{isPacked:l}=n.texData.get(r.dataId),c=wt.isSliceContinous(r.shape,o,u);if(l||!c){let p=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new mY(u):new hY(u),d=[o];return n.runWebGLProgram(p,[r],r.dtype,d)}return n.uploadToGPU(r.dataId),gY(r,o,u,n)}var bY={kernelName:Oo,backendName:"webgl",kernelFunc:ou},yY=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,crops:i}=s;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let o=a.reduce((y,v)=>y*v),u=S.getReshaped(r.shape,a,o),l=S.getPermuted(u.length,a.length),c=S.getReshapedPermuted(r.shape,a,o),p=S.getSliceBeginCoords(i,a.length),d=S.getSliceSize(c,i,a.length),h=[],f=he({inputs:{x:r},backend:n,attrs:{shape:u}}),m=qt({inputs:{x:f},backend:n,attrs:{perm:l}}),g=he({inputs:{x:m},backend:n,attrs:{shape:c}}),b=ou({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},vY={kernelName:io,backendName:"webgl",kernelFunc:yY};function xY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,weights:a}=t,{size:i}=s,o=n.readSync(r.dataId),u=n.readSync(a.dataId),l=R1(o,u,a.dtype,a.shape,i);return n.makeTensorInfo([i],a.dtype,l)}var wY={kernelName:ug,backendName:"webgl",kernelFunc:xY};function kY(e){let{inputs:t,backend:n}=e,{s0:s,s1:r}=t,a=n.readSync(s.dataId),i=n.readSync(r.dataId),o=S.assertAndGetBroadcastShape(Array.from(a),Array.from(i));return n.makeTensorInfo([o.length],"int32",Int32Array.from(o))}var IY={kernelName:lg,backendName:"webgl",kernelFunc:kY},SY="return float(a != b);",X1=jt({opSnippet:SY,cpuKernelImpl:tX,dtype:"bool"}),CY={kernelName:Io,backendName:"webgl",kernelFunc:X1};function Jl(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.texData.get(s.dataId);return kn({inputs:{x:r.complexTensorInfos.real},backend:n})}var NY={kernelName:np,backendName:"webgl",kernelFunc:Jl},TY="return float(int(x));";function $Y(e,t){let n=new Hs(e.shape,TY),s=t.runWebGLProgram(n,[e],"int32");return{dataId:s.dataId,shape:s.shape,dtype:s.dtype}}function Hm(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dtype:a}=s;if(a==="complex64"){if(r.dtype==="complex64")return kn({inputs:{x:r},backend:n});let i=$t(r.shape),o=Hm({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),u=Rr({inputs:{real:o,imag:i},backend:n});return i.dispose(),n.disposeIntermediateTensorInfo(o),u}if(r.dtype==="complex64"){let i=Jl({inputs:{input:r},backend:n}),o=Hm({inputs:{x:i},backend:n,attrs:{dtype:a}});return n.disposeIntermediateTensorInfo(i),o}if(!w.hasEncodingLoss(r.dtype,a)){let i=kn({inputs:{x:r},backend:n});return{dataId:i.dataId,shape:i.shape,dtype:a}}if(a==="int32")return $Y(r,n);if(a==="bool"){let i=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=X1({inputs:{a:r,b:i},backend:n});return n.disposeIntermediateTensorInfo(i),u}throw new Error(`Error in Cast: failed to cast ${r.dtype} to ${a}`)}var _Y={kernelName:Na,backendName:"webgl",kernelFunc:Hm},mw="return ceil(x);",AY=Ke({opSnippet:mw,packedOpSnippet:mw,cpuKernelImpl:PK}),EY={kernelName:Ta,backendName:"webgl",kernelFunc:AY},RY=class{constructor(e){this.variableNames=["A"],this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=e,this.userCode=`
void main() {
float value = getAAtOutCoords();
if (isnan(value)) {
setOutput(value);
return;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(clamp(value, minVal, maxVal));
}
`}},DY=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=`
void main() {
vec4 value = getAAtOutCoords();
if (any(isnan(value))) {
setOutput(value);
return;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(clamp(value, vec4(minVal), vec4(maxVal)));
}
`}};function FY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{clipValueMin:a,clipValueMax:i}=s,o;X().getBool("WEBGL_PACK_CLIP")?o=new DY(r.shape):o=new RY(r.shape);let u=[[a],[i]];return n.runWebGLProgram(o,[r],r.dtype,u)}var OY={kernelName:Sr,backendName:"webgl",kernelFunc:FY},PY=class{constructor(e){this.variableNames=["real","imag"],this.outputShape=e,this.userCode=`
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))
);
}
`}};function gw(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function zY(e){let{inputs:t,backend:n}=e,{x:s}=t,r=n.texData.get(s.dataId),a=new PY(s.shape),i=[gw(s,r.complexTensorInfos.real),gw(s,r.complexTensorInfos.imag)];return n.runWebGLProgram(a,i,i[0].dtype)}var MY={kernelName:Kd,backendName:"webgl",kernelFunc:zY},LY=class{constructor(e){this.outputShape=[],this.outputShape=S.computeOutShape(e,1),this.variableNames=e.map((a,i)=>`T${i}`);let t=new Array(e.length-1);t[0]=e[0][1];for(let a=1;a<t.length;a++)t[a]=t[a-1]+e[a][1];let n=[`if (yC < ${t[0]}) setOutput(getT0(yR, yC));`];for(let a=1;a<t.length;a++){let i=t[a-1];n.push(`else if (yC < ${t[a]}) setOutput(getT${a}(yR, yC-${i}));`)}let s=t.length,r=t[t.length-1];n.push(`else setOutput(getT${s}(yR, yC-${r}));`),this.userCode=`
void main() {
ivec2 coords = getOutputCoords();
int yR = coords.x;
int yC = coords.y;
${n.join(`
`)}
}
`}},BY=class{constructor(e,t){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=S.computeOutShape(e,t);let n=this.outputShape,s=n.length,r=rt(s),a=ln("coords",s),i=["x","y","z","w","u","v"].slice(0,s);this.variableNames=e.map((f,m)=>`T${m}`);let o=new Array(e.length-1);o[0]=e[0][t];for(let f=1;f<o.length;f++)o[f]=o[f-1]+e[f][t];let u=i[t],l=i.slice(-2),c=i.join(),p=`if (${u} < ${o[0]}) {
return getChannel(
getT0(${c}), vec2(${l.join()}));
}`;for(let f=1;f<o.length;f++){let m=o[f-1];p+=`
if (${u} < ${o[f]} && ${u} >= ${o[f-1]}) {
return getChannel(
getT${f}(${Jc(i,u,m)}),
vec2(${Jc(l,u,m)}));
}`}let d=o.length,h=o[o.length-1];p+=`
return getChannel(
getT${d}(${Jc(i,u,h)}),
vec2(${Jc(l,u,h)}));`,this.userCode=`
float getValue(${i.map(f=>"int "+f)}) {
${p}
}
void main() {
${r} coords = getOutputCoords();
vec4 result = vec4(getValue(${a}), 0., 0., 0.);
${a[s-1]} = ${a[s-1]} + 1;
if (${a[s-1]} < ${n[s-1]}) {
result.g = getValue(${a});
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${a[s-2]} = ${a[s-2]} + 1;
if (${a[s-2]} < ${n[s-2]}) {
result.a = getValue(${a});
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(result);
}
`}};function Jc(e,t,n){let s=e.indexOf(t);return e.map((a,i)=>i===s?`${a} - ${n}`:a).join()}function Qp(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.texData.get(s.dataId);return kn({inputs:{x:r.complexTensorInfos.imag},backend:n})}var VY={kernelName:Zd,backendName:"webgl",kernelFunc:Qp};function Gi(e,t,n){let s=e[0].dtype;if(s==="complex64"){let c=e.map(m=>Jl({inputs:{input:m},backend:n})),p=e.map(m=>Qp({inputs:{input:m},backend:n})),d=Gi(c,t,n),h=Gi(p,t,n),f=Rr({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 he({inputs:{x:b},backend:n,attrs:{shape:[-1,y]}})}),p=c.map(b=>({vals:n.readSync(b.dataId),shape:b.shape})),d=S.computeOutShape(c.map(b=>b.shape),1),h=c[0].shape[0]===1,f=zK(p,d,s,h),m=S.computeOutShape(e.map(b=>b.shape),t),g=n.makeTensorInfo(m,s,f);return c.forEach(b=>n.disposeIntermediateTensorInfo(b)),g}if(e.length>X().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let c=Math.floor(e.length/2),p=Gi(e.slice(0,c),t,n),d=Gi(e.slice(c),t,n),h=Gi([p,d],t,n);return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),h}if(X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&e[0].shape.length>1){let c=new BY(e.map(p=>p.shape),t);return n.runWebGLProgram(c,e,s)}let{tensors2D:a,outShape:i}=WY(e,t,n),o=new LY(a.map(c=>c.shape)),u=n.runWebGLProgram(o,a,s);a.forEach(c=>n.disposeIntermediateTensorInfo(c));let l=he({inputs:{x:u},attrs:{shape:i},backend:n});return n.disposeIntermediateTensorInfo(u),l}function WY(e,t,n){let s=S.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>he({inputs:{x:a},attrs:{shape:[-1,w.sizeFromShape(a.shape.slice(t))]},backend:n})),outShape:s}}function Y1(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s,a=w.parseAxisParam(r,t[0].shape)[0],i=S.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(i)===0)return n.makeTensorInfo(i,t[0].dtype,[]);let o=t.filter(l=>w.sizeFromShape(l.shape)>0);if(o.length===1)return kn({inputs:{x:o[0]},backend:n});let u=o.map(l=>l.shape);return S.assertParamsConsistent(u,a),Gi(o,a,n)}var UY={kernelName:oo,backendName:"webgl",kernelFunc:Y1},Q1=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let a=e.padInfo.top,i=e.padInfo.left,o=e.strideHeight,u=e.strideWidth,l=e.dilationHeight,c=e.dilationWidth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4,m=e.dataFormat==="channelsLast",g=m?1:2,b=m?2:3,y=m?3:1,v="",x="";n&&(s?v=`float activation(float a) {
float b = getPreluActivationWeightsAtOutCoords();
${n}
}`:r?v=`float activation(float a) {
float b = getLeakyreluAlphaAtOutCoords();
${n}
}`:v=`
float activation(float x) {
${n}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`,x="result = activation(result);");let k=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${v}
const ivec2 strides = ivec2(${o}, ${u});
const ivec2 pads = ivec2(${a}, ${i});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d2 = coords[${y}];
ivec2 xRCCorner =
ivec2(coords[${g}], coords[${b}]) * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${p}; wR++) {
int xR = xRCorner + wR * ${l};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
for (int wC = 0; wC < ${d}; wC++) {
int xC = xCCorner + wC * ${c};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
} else {
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
} else {
2022-04-01 15:13:32 +02:00
vec2 xValues = vec2(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC)
);
dotProd += dot(xValues, wValues);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
} else {
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
2022-02-10 18:27:21 +01:00
}
}
}
2022-04-01 15:13:32 +02:00
float result = dotProd;
${k}
${x}
setOutput(result);
}
`}},GY=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let t=e.padInfo.front,n=e.padInfo.top,s=e.padInfo.left,r=e.strideDepth,a=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,u=e.dilationHeight,l=e.dilationWidth,c=e.filterDepth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4;this.userCode=`
const ivec3 strides = ivec3(${r}, ${a}, ${i});
const ivec3 pads = ivec3(${t}, ${n}, ${s});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d2 = coords.u;
ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xFCorner = xFRCCorner.x;
int xRCorner = xFRCCorner.y;
int xCCorner = xFRCCorner.z;
// Convolve x(?, ?, ?, d1) with w(:, :, :, d1, d2) to get
// y(yF, yR, yC, d2). ? = to be determined. : = across all
// values in that axis.
float dotProd = 0.0;
for (int wF = 0; wF < ${c}; wF++) {
int xF = xFCorner + wF * ${o};
if (xF < 0 || xF >= ${e.inDepth}) {
continue;
}
for (int wR = 0; wR < ${p}; wR++) {
int xR = xRCorner + wR * ${u};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
}
2022-03-16 16:19:56 +01:00
}
2022-04-01 15:13:32 +02:00
setOutput(dotProd);
}
`}},HY=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec3"},{name:"pad",type:"ivec2"},{name:"stride",type:"ivec2"},{name:"dilation",type:"ivec2"},{name:"inChannels",type:"int"},{name:"itemsPerBlockRow",type:"int"},{name:"outWidth",type:"int"}],this.outputShape=e,this.enableShapeUniforms=Sn(this.outputShape.length);let{dataFormat:n}=t,s=fn(),r=n==="channelsLast",a=r?0:1,i=r?1:2,o=this.enableShapeUniforms?"if(blockIndex < outShape[1] && pos < outShape[0]) {":`if(blockIndex < ${e[1]} && pos < ${e[0]}) {`,u="";for(let l=0;l<=1;l++)for(let c=0;c<=1;c++)u+=`
blockIndex = rc.y + ${c};
pos = rc.x + ${l};
${o}
offsetY = int(blockIndex / outWidth) * stride[0] - pad[0];
d0 = offsetY + dilation[0] * (pos / itemsPerBlockRow);
if(d0 < inputShape[${a}] && d0 >= 0) {
// Use custom imod instead mod. On Intel GPU, mod may generate
// unexpected value.
// https://github.com/tensorflow/tfjs/issues/5447
offsetX = imod(blockIndex, outWidth) * stride[1] - pad[1];
d1 = offsetX + dilation[1] * (imod(pos, itemsPerBlockRow) /
inChannels);
if(d1 < inputShape[${i}] && d1 >= 0) {
ch = imod(pos, inChannels);
if (${r}) {
innerDims = vec2(d1, ch);
result[${l*2+c}] = getChannel(
getA(d0, int(innerDims.x),
int(innerDims.y)), innerDims);
} else {
innerDims = vec2(d0, d1);
result[${l*2+c}] = getChannel(
getA(ch, int(innerDims.x),
int(innerDims.y)), innerDims);
}
}
2022-04-01 15:12:04 +02:00
}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;this.userCode=`
void main() {
ivec2 rc = getOutputCoords();
vec4 result = vec4(0);
int blockIndex, pos, offsetY, d0, offsetX, d1, ch;
vec2 innerDims;
${u}
${s.output} = result;
}
`}};function Z1({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let u=e.shape,l=s.texData.get(e.dataId),c=n.inChannels,p=u[0]*u[1]*u[2],d=n.outChannels,h=n.dataFormat==="channelsLast",f=!1,m=!1,g,b=[];if(!((p===1||d===1)&&c>H1)&&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},T=l.shape;l.shape=l.shape.slice(),l.shape[l.shape.length-2]++,w.assert(Ju(l.shape,k.shape),()=>`packed reshape ${l.shape} to ${k.shape} isn't free`);let N=he({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}});b.push(N);let E=zd({a:k,b:N,backend:s,transposeA:f,transposeB:m,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i}),A=s.texData.get(E.dataId);w.assert(A.isPacked,()=>"batchMatMul result is expected to be packed"),l.shape=T,A.shape=n.outShape,g=kn({inputs:{x:E},backend:s}),g.shape=n.outShape,b.push(E)}else{let x=h?u[0]*u[1]*u[2]:u[0]*u[2]*u[3],k=he({inputs:{x:e},backend:s,attrs:{shape:[1,x,n.inChannels]}}),T=he({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}}),N=zd({a:k,b:T,transposeA:f,transposeB:m,backend:s,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i});g=he({inputs:{x:N},backend:s,attrs:{shape:n.outShape}}),b.push(k),b.push(T),b.push(N)}for(let x of b)s.disposeIntermediateTensorInfo(x);return g}function J1({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let{filterWidth:u,filterHeight:l,inChannels:c,outWidth:p,outHeight:d,dataFormat:h}=n,f=h==="channelsLast",m=u*l*c,g=d*p,b=[m,g],y=!0,v=!1,x=[],k=he({inputs:{x:e},backend:s,attrs:{shape:e.shape.slice(1)}}),T=he({inputs:{x:t},backend:s,attrs:{shape:[1,m,w.sizeFromShape(t.shape)/m]}});x.push(k),x.push(T);let N=new HY(b,n),E=[k.shape,[n.padInfo.top,n.padInfo.left],[n.strideHeight,n.strideWidth],[n.dilationHeight,n.dilationWidth],[n.inChannels],[n.filterWidth*n.inChannels],[n.outWidth]],A=s.runWebGLProgram(N,[k],"float32",E),P=he({inputs:{x:A},backend:s,attrs:{shape:[1,b[0],b[1]]}});x.push(A),x.push(P);let R=r!=null,F=a!=null,$=o==="leakyrelu",z=o?Kp(o,!0):null,W=new G1(P.shape,T.shape,[1,g,n.outChannels],y,v,R,z,F,$),q=[P,T];if(r&&q.push(r),F&&q.push(a),$){let te=s.makeTensorInfo([],"float32",w.createScalarValue(i,"float32"));q.push(te),x.push(te)}let K=s.runWebGLProgram(W,q,"float32"),Y=f?[1,d,p,n.outChannels]:[1,n.outChannels,d,p],Z=he({inputs:{x:K},backend:s,attrs:{shape:Y}});x.push(K);for(let te of x)s.disposeIntermediateTensorInfo(te);return Z}function qY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dataFormat:u,dilations:l,dimRoundingMode:c}=s,p=S.convertConv2DDataFormat(u),d=S.computeConv2DInfo(r.shape,a.shape,i,l,o,c,!1,p),h;if(d.filterHeight===1&&d.filterWidth===1&&d.dilationHeight===1&&d.dilationWidth===1&&d.strideHeight===1&&d.strideWidth===1&&(d.padInfo.type==="SAME"||d.padInfo.type==="VALID"))h=Z1({x:r,filter:a,convInfo:d,backend:n});else if(X().getBool("WEBGL_CONV_IM2COL")&&r.shape[0]===1)h=J1({x:r,filter:a,convInfo:d,backend:n});else{let m=new Q1(d);h=n.runWebGLProgram(m,[r,a],"float32")}let f=he({inputs:{x:h},backend:n,attrs:{shape:d.outShape}});return n.disposeIntermediateTensorInfo(h),f}var jY={kernelName:$a,backendName:"webgl",kernelFunc:qY},KY=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.dataFormat==="channelsLast";this.userCode=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
2022-04-01 15:12:04 +02:00
}
}
}
2022-04-01 15:13:32 +02:00
setOutput(dotProd);
}
`}},XY=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,s=e.strideHeight,r=e.strideWidth,a=e.dataFormat==="channelsLast",i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,u=a?1:2,l=a?2:3,c=a?3:1;this.userCode=`
const ivec2 pads = ivec2(${i}, ${o});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d1 = coords[${c}];
ivec2 dyCorner = ivec2(coords[${u}], coords[${l}]) - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / ${s}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
continue;
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
} else {
2022-04-01 15:13:32 +02:00
float xValue = getDy(batch, d2, idyR, idyC);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
2022-02-10 18:27:21 +01:00
}
}
}
2022-04-01 15:13:32 +02:00
setOutput(dotProd);
}
`}},YY=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideDepth,n=e.strideHeight,s=e.strideWidth,r=e.padInfo.front,a=e.padInfo.top,i=e.padInfo.left;this.userCode=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${n} - ${a};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${s} - ${i};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float dyValue = getDy(b, yF, yR, yC, d2);
float xValue = getX(b, xF, xR, xC, d1);
dotProd += (xValue * dyValue);
}
2022-02-10 18:27:21 +01:00
}
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(dotProd);
}
`}},QY=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,s=e.filterWidth,r=e.strideDepth,a=e.strideHeight,i=e.strideWidth,o=t-1-e.padInfo.front,u=n-1-e.padInfo.top,l=s-1-e.padInfo.left;this.userCode=`
const ivec3 pads = ivec3(${o}, ${u}, ${l});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyFCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
float dotProd = 0.0;
for (int wF = 0; wF < ${t}; wF++) {
float dyF = float(dyFCorner + wF) / ${r}.0;
if (dyF < 0.0 || dyF >= ${e.outDepth}.0 || fract(dyF) > 0.0) {
continue;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
int idyR = int(dyR);
int wRPerm = ${n} - 1 - wR;
for (int wC = 0; wC < ${s}; wC++) {
float dyC = float(dyCCorner + wC) / ${i}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
int wCPerm = ${s} - 1 - wC;
for (int d2 = 0; d2 < ${e.outChannels}; d2++) {
float xValue = getDy(batch, idyF, idyR, idyC, d2);
float wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
}
2022-04-01 15:12:04 +02:00
}
2022-02-10 18:27:21 +01:00
}
}
2022-04-01 15:13:32 +02:00
setOutput(dotProd);
}
`}};function ZY(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:i,pad:o,dataFormat:u,dimRoundingMode:l,filterShape:c}=s,p=S.convertConv2DDataFormat(u),d=S.computeConv2DInfo(r.shape,c,i,1,o,l,!1,p),h=new KY(d);return n.runWebGLProgram(h,[r,a],"float32")}var JY={kernelName:cg,backendName:"webgl",kernelFunc:ZY};function e9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{inputShape:i,strides:o,pad:u,dataFormat:l,dimRoundingMode:c}=s,p=S.convertConv2DDataFormat(l),d=S.computeConv2DInfo(i,a.shape,o,1,u,c,!1,p),h=new XY(d);return n.runWebGLProgram(h,[r,a],"float32")}var t9={kernelName:_a,backendName:"webgl",kernelFunc:e9};function n9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u}=s,l=S.computeConv3DInfo(r.shape,a.shape,i,u,o),c=new GY(l);return n.runWebGLProgram(c,[r,a],"float32")}var s9={kernelName:Xd,backendName:"webgl",kernelFunc:n9};function r9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:i,pad:o,filterShape:u}=s,l=S.computeConv3DInfo(r.shape,u,i,1,o),c=new YY(l);return n.runWebGLProgram(c,[r,a],"float32")}var a9={kernelName:dg,backendName:"webgl",kernelFunc:r9};function i9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{pad:i,strides:o,inputShape:u}=s,l=S.computeConv3DInfo(u,a.shape,o,1,i),c=new QY(l);return n.runWebGLProgram(c,[r,a],"float32")}var o9={kernelName:pg,backendName:"webgl",kernelFunc:i9},u9=iu+`
return cos(x);
`,l9=Ke({opSnippet:u9}),c9={kernelName:Aa,backendName:"webgl",kernelFunc:l9},d9=`
float e2x = exp(-x);
return (e2x + 1.0 / e2x) / 2.0;
`,p9=Ke({opSnippet:d9}),h9={kernelName:Ea,backendName:"webgl",kernelFunc:p9},f9=class{constructor(e,t,n,s,r){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[a,i,o,u]=e,[l]=t,[c,p]=n;this.outputShape=[l,c,p,u];let d=s==="bilinear"?1:0,[h,f]=[`${i-1}.0`,`${o-1}.0`],[m,g,b]=c>1?[`${(i-1)/(c-1)}`,"(y2-y1) * height_ratio",`y1*${h} + float(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${h}`],[y,v,x]=p>1?[`${(o-1)/(p-1)}`,"(x2-x1) * width_ratio",`x1*${f} + float(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${f}`];this.userCode=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
float in_x = ${x};
if( in_x < 0.0 || in_x > ${f} ) {
setOutput(float(${r}));
return;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
} else {
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
`}},m9=e=>{let{inputs:t,backend:n,attrs:s}=e,{image:r,boxes:a,boxInd:i}=t,{cropSize:o,method:u,extrapolationValue:l}=s,c=new f9(r.shape,a.shape,o,u,l);return n.runWebGLProgram(c,[r,a,i],"float32")},g9={kernelName:lo,backendName:"webgl",kernelFunc:m9},bw=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}],this.outputShape=e;let s=e.length,r=t?"1.0":`getX(${yw(s,"coords")})`,a=e[e.length-1],i="",o="";t?(i=n?`end != ${a-1}`:"end != 0",o=n?"end + 1":"end - 1"):(i=n?`end + pow2 < ${a}`:"end >= pow2",o=n?"end + pow2":"end - pow2"),this.userCode=`
void main() {
${rt(s)} coords = getOutputCoords();
int end = ${vw(s,"coords")};
float val = ${r};
int pow2 = int(pow(2.0, index));
if (${i}) {
int idx = ${o};
${vw(s,"coords")} = idx;
val *= getX(${yw(s,"coords")});
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(val);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function yw(e,t){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 product for rank ${e} is not yet supported`)}function vw(e,t){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 product for rank ${e} is not yet supported`)}function b9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:i,reverse:o}=s,u=r.shape.length,l=S.getAxesPermutation([a],u),c=r;l!=null&&(c=qt({inputs:{x:r},backend:n,attrs:{perm:l}}));let p=S.getInnerMostAxes(1,u)[0];if(p!==u-1)throw new Error(`WebGL cumprod shader expects an inner-most axis=${r.shape.length-1} but got axis=${a}`);let d=c.shape[p],h=kn({inputs:{x:c},backend:n});for(let f=0;f<=Math.ceil(Math.log2(d))-1;f++){let m=new bw(c.shape,!1,o),g=[[f]],b=h;h=n.runWebGLProgram(m,[h],h.dtype,g),n.disposeIntermediateTensorInfo(b)}if(i){let f=new bw(c.shape,i,o),m=h;h=n.runWebGLProgram(f,[h],h.dtype),n.disposeIntermediateTensorInfo(m)}if(l!=null){let f=S.getUndoAxesPermutation(l),m=qt({inputs:{x:h},backend:n,attrs:{perm:f}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(c),m}return h}var y9={kernelName:pl,backendName:"webgl",kernelFunc:b9},xw=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}],this.outputShape=e;let s=e.length,r=t?"0.0":`getX(${ww(s,"coords")})`,a=e[e.length-1],i="",o="";t?(i=n?`end != ${a-1}`:"end != 0",o=n?"end + 1":"end - 1"):(i=n?`end + pow2 < ${a}`:"end >= pow2",o=n?"end + pow2":"end - pow2"),this.userCode=`
void main() {
${rt(s)} coords = getOutputCoords();
int end = ${kw(s,"coords")};
float val = ${r};
int pow2 = int(pow(2.0, index));
if (${i}) {
int idx = ${o};
${kw(s,"coords")} = idx;
val += getX(${ww(s,"coords")});
}
setOutput(val);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function ww(e,t){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 sum for rank ${e} is not yet supported`)}function kw(e,t){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 sum for rank ${e} is not yet supported`)}function v9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,exclusive:i,reverse:o}=s,u=r.shape.length,l=S.getAxesPermutation([a],u),c=r;l!=null&&(c=qt({inputs:{x:r},backend:n,attrs:{perm:l}}));let p=S.getInnerMostAxes(1,u)[0];if(p!==u-1)throw new Error(`WebGL cumsum shader expects an inner-most axis=${r.shape.length-1} but got axis=${a}`);let d=c.shape[p],h=kn({inputs:{x:c},backend:n});for(let f=0;f<=Math.ceil(Math.log2(d))-1;f++){let m=new xw(c.shape,!1,o),g=[[f]],b=h;h=n.runWebGLProgram(m,[h],h.dtype,g),n.disposeIntermediateTensorInfo(b)}if(i){let f=new xw(c.shape,i,o),m=h;h=n.runWebGLProgram(f,[h],h.dtype),n.disposeIntermediateTensorInfo(m)}if(l!=null){let f=S.getUndoAxesPermutation(l),m=qt({inputs:{x:h},backend:n,attrs:{perm:f}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(c),m}return h}var x9={kernelName:uo,backendName:"webgl",kernelFunc:v9};function w9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,weights:a}=t,{size:i,binaryOutput:o}=s;if(r.shape.length===1){let u=n.readSync(r.dataId),l=n.readSync(a.dataId),c=R1(u,l,a.dtype,a.shape,i);return n.makeTensorInfo([i],a.dtype,c)}else if(r.shape.length===2){let u=n.bufferSync(r),l=n.bufferSync(a),c=OK(u,l,i,o);return n.makeTensorInfo(c.shape,a.dtype,c.values)}throw new Error(`Error in denseBincount: input must be at most rank 2, but got rank${r.shape.length}.`)}var k9={kernelName:hg,backendName:"webgl",kernelFunc:w9},I9=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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 S9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockSize:a,dataFormat:i}=s,o=r.shape[0],u=i==="NHWC"?r.shape[1]:r.shape[2],l=i==="NHWC"?r.shape[2]:r.shape[3],c=i==="NHWC"?r.shape[3]:r.shape[1],p=u*a,d=l*a,h=c/(a*a),f=i==="NHWC"?[o,p,d,h]:[o,h,p,d],m=new I9(f,a,i);return n.runWebGLProgram(m,[r],r.dtype)}var C9={kernelName:co,backendName:"webgl",kernelFunc:S9},e2=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=Sn(this.outputShape.length);let a=e.filterHeight,i=e.filterWidth,o=e.outChannels/e.inChannels,u="",l="";n&&(s?u=`float activation(float a) {
float b = getPreluActivationWeightsAtOutCoords();
${n}
}`:r?u=`float activation(float a) {
float b = getLeakyreluAlphaAtOutCoords();
${n}
}`:u=`
float activation(float x) {
${n}
}
`,l="result = activation(result);");let c=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${u}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
int d1 = d2 / ${o};
int q = d2 - d1 * ${o};
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
// TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.
for (int wR = 0; wR < ${a}; wR++) {
int xR = xRCorner + wR * dilations[0];
if (xR < 0 || xR >= inDims[0]) {
continue;
}
for (int wC = 0; wC < ${i}; wC++) {
int xC = xCCorner + wC * dilations[1];
if (xC < 0 || xC >= inDims[1]) {
continue;
}
float xVal = getX(batch, xR, xC, d1);
float wVal = getW(wR, wC, d1, q);
dotProd += xVal * wVal;
}
}
float result = dotProd;
${c}
${l}
setOutput(result);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},t2=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=Sn(this.outputShape.length);let a=e.outChannels/e.inChannels,i=e.padInfo.left,o=e.strideWidth,u=e.dilationWidth,l=e.filterHeight,c=e.filterWidth,p=c,d=`
int xR; int xC; int xCOffset;
vec4 wTexel; vec4 previous; vec4 final;`;for(let g=0;g<c;g++)d+=`
vec4 xTexelC${g*2};
int xTexelC${g*2}Ready;
vec4 xTexelC${g*2+1};
int xTexelC${g*2+1}Ready;
vec4 xC${g};`;d+=`
for (int r = 0; r < ${l}; r++) {
`;for(let g=0;g<c;g++)d+=`
xTexelC${g*2} = vec4(0.0);
xTexelC${g*2}Ready = 0;
xTexelC${g*2+1} = vec4(0.0);
xTexelC${g*2+1}Ready = 0;
xC${g} = vec4(0.0);`;d+=`
xR = xRCorner + r * dilations[0];
if (xR >=0 && xR < inDims[0]) {
`;for(let g=0;g<(p+1)/2;g++){let b=g*2;if(d+=`
xC = xCCorner + ${b*u};
`,o===1){if(b<c&&(i%2===1?(d+=`
xCOffset = xC + 1;
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
`,u===1&&b>0?d+=`
xC${b} = vec4(xTexelC${b-2}.zw, xTexelC${b}.xy);
`:d+=`
xCOffset = xC + 1 - 2;
if (xCOffset >= 0 && xCOffset < inDims[1]) {
previous = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
previous.zw = vec2(0.0);
}
xC${b} = vec4(previous.zw, xTexelC${b}.xy);
} else {
xC${b} = vec4(0.0, 0.0, xTexelC${b}.xy);
}
`):d+=`
if (xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
if (xC + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
xC${b} = xTexelC${b};
`,b+1<c)){let y=i%2===0?w.nearestLargerEven(u):u;u%2===0&&i%2===1||u%2!==0&&i%2!==1?(d+=`
xCOffset = xC + imod(pads[1], 2) + ${y};
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.0);
}
xTexelC${b+1}Ready = 1;
}
`,u>1&&(d+=`
xCOffset -= 2;
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
xTexelC${b}Ready = 1;
}
`),d+=`
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.xy);
`):y===1?d+=`
xC${b+1} = xTexelC${b};
`:d+=`
xCOffset = xC + ${y};
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.0);
}
xTexelC${b+1}Ready = 1;
}
xC${b+1} = xTexelC${b+1};
`}}else b<c&&(i%2===1?(d+=`
xCOffset = xC + 1 - strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
if(xC + 1 >= 0 && xC + 1 < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xC + 1, d1);
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xC + 2 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.0);
}
xTexelC${b+1}Ready = 1;
}
xC${b} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
`,b+1<c&&(d+=`
final = vec4(0.0);
xCOffset = xC + 1 + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1]) {
final = getX(batch, xR, xCOffset, d1);
}
xC${b+1} = vec4(xTexelC${b+1}.xy, final.xy);
`)):(d+=`
if(xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
if (xC + 1 >= inDims[1]) {
xTexelC${b}.zw = vec2(0.0);
}
xTexelC${b}Ready = 1;
}
xCOffset = xC + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
if (xCOffset + 1 >= inDims[1]) {
xTexelC${b+1}.zw = vec2(0.);
}
xTexelC${b+1}Ready = 1;
}
xC${b} = vec4(
xTexelC${b}.xy, xTexelC${b+1}.xy);
`,b+1<c&&(d+=`
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
`)));b<c&&(d+=`
wTexel = getW(r, ${b}, d1, q);
dotProd += xC${b} * vec4(wTexel.xz, wTexel.xz);
`,b+1<c&&(d+=`
wTexel = getW(r, ${b+1}, d1, q);
dotProd += xC${b+1} * vec4(wTexel.xz, wTexel.xz);
`))}d+=`
}
`,d+=`
}
`;let h="",f="";n&&(s?h=`vec4 activation(vec4 a) {
vec4 b = getPreluActivationWeightsAtOutCoords();
${n}
}`:r?h=`vec4 activation(vec4 a) {
vec4 b = getLeakyreluAlphaAtOutCoords();
${n}
}`:h=`vec4 activation(vec4 x) {
${n}
}`,f="result = activation(result);");let m=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${h}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
int d1 = d2 / ${a};
int q = d2 - d1 * ${a};
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
//intialize dotProd with a small epsilon seems to reduce GPU accuracy loss.
vec4 dotProd = vec4(0.000000000000001);
${d}
vec4 result = dotProd - vec4(0.000000000000001);
${m}
${f}
setOutput(result);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function N9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u,dimRoundingMode:l}=s,c=u;c==null&&(c=[1,1]),w.assert(S.eitherStridesOrDilationsAreOne(i,c),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${i} and dilations '${c}'`);let p=S.computeConv2DInfo(r.shape,a.shape,i,c,o,l,!0),d;X().getBool("WEBGL_PACK_DEPTHWISECONV")&&p.strideWidth<=2&&p.outChannels/p.inChannels===1?d=new t2(p):d=new e2(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 T9={kernelName:Ra,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.left,a=e.outChannels/e.inChannels;this.userCode=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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=t-1-e.padInfo.top,i=n-1-e.padInfo.left,o=e.outChannels/e.inChannels;this.userCode=`
const ivec2 pads = ivec2(${a}, ${i});
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d1 = coords[3];
ivec2 dyCorner = coords.yz - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
float dotProd = 0.0;
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / ${s}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
int wRPerm = ${t} - 1 - wR;
for (int wC = 0; wC < ${n}; wC++) {
float dyC = float(dyCCorner + wC) / ${r}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
int wCPerm = ${n} - 1 - wC;
// TO DO: Vec4 over the channelMul
for (int dm = 0; dm < ${o}; dm++) {
int d2 = d1 * ${o} + dm;
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, dm);
dotProd += xValue * wValue;
}
}
}
setOutput(dotProd);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function A9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,dy:a}=t,{strides:i,dilations:o,pad:u,dimRoundingMode:l,filterShape:c}=s,p=S.computeConv2DInfo(r.shape,c,i,o,u,l,!0),d=new $9(p);return n.runWebGLProgram(d,[r,a],"float32")}var E9={kernelName:fg,backendName:"webgl",kernelFunc:A9};function R9(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{strides:i,dilations:o,pad:u,dimRoundingMode:l,inputShape:c}=s,p=S.computeConv2DInfo(c,a.shape,i,o,u,l,!0),d=new _9(p);return n.runWebGLProgram(d,[r,a],"float32")}var D9={kernelName:mg,backendName:"webgl",kernelFunc:R9},F9=class{constructor(e){this.variableNames=["X"],this.outputShape=[e,e],this.userCode=`
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-04-01 15:13:32 +02:00
`}};function O9(e){let{inputs:t,backend:n}=e,{x:s}=t,r=[...s.shape,...s.shape],a=w.sizeFromShape(s.shape),i=he({inputs:{x:s},backend:n,attrs:{shape:[a]}}),o=new F9(a),u=n.runWebGLProgram(o,[i],i.dtype),l=he({inputs:{x:u},backend:n,attrs:{shape:r}});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(u),l}var P9={kernelName:gg,backendName:"webgl",kernelFunc:O9},z9=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:s,strideHeight:r,strideWidth:a,filterHeight:i,filterWidth:o,dilationHeight:u,dilationWidth:l}=e,{top:c,left:p}=s;this.userCode=`
const ivec2 strides = ivec2(${r}, ${a});
const ivec2 pads = ivec2(${c}, ${p});
const float neg_infinity = -3.4e38;
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.w;
ivec2 outTopLeftCorner =
coords.yz * strides - pads;
int hBeg = outTopLeftCorner.x;
int wBeg = outTopLeftCorner.y;
float curVal = neg_infinity;
for (int h = 0; h < ${i}; h++) {
int hIn = hBeg + h * ${u};
if (hIn >= 0 && hIn < ${t}) {
for (int w = 0; w < ${o}; w++) {
int wIn = wBeg + w * ${l};
if (wIn >= 0 && wIn < ${n}) {
float xVal = getX(batch, hIn, wIn, d1);
float wVal = getW(h, w, d1);
float val = xVal + wVal;
if (val > curVal) {
curVal = val;
}
}
}
}
}
float result = curVal;
setOutput(result);
}
`}};function M9(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u}=s,l=S.computeDilation2DInfo(r.shape,a.shape,i,o,"NHWC",u),c,p=new z9(l);c=n.runWebGLProgram(p,[r,a],"float32");let d=he({inputs:{x:c},backend:n,attrs:{shape:l.outShape}});return n.disposeIntermediateTensorInfo(c),d}var L9={kernelName:Yd,backendName:"webgl",kernelFunc:M9};function B9(e){let{inputs:t,backend:n,attrs:s}=e,{equation:r}=s,a=t,{allDims:i,summedDims:o,idDims:u}=S.decodeEinsumEquation(r,a.length);S.checkEinsumDimSizes(i.length,u,a);let{path:l,steps:c}=S.getEinsumComputePath(o,u),p=c.length,d=null,h=i.length,f=[];for(let m=0;m<p;++m){for(let g of c[m]){let{permutationIndices:b,expandDims:y}=S.getEinsumPermutation(h,u[g]),v;S.isIdentityPermutation(b)?v=a[g]:(v=qt({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=he({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),d===null?d=v:(d=kv({inputs:{a:v,b:d},backend:n}),f.push(d))}m<p-1&&(l[m]>=0&&(d=Yp({inputs:{x:d},backend:n,attrs:{axis:l[m]-(i.length-h),keepDims:!1}}),f.push(d)),h--)}for(let m of f)m!==d&&n.disposeIntermediateTensorInfo(m);return d}var V9={kernelName:Qd,backendName:"webgl",kernelFunc:B9},W9="return (x >= 0.0) ? x : (exp(x) - 1.0);",U9=`
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;
`,G9=Ke({opSnippet:W9,packedOpSnippet:U9}),H9={kernelName:Fa,backendName:"webgl",kernelFunc:G9},q9="return (b >= 1.0) ? a : a * (b + 1.0);",j9=`
vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));
return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));
`,K9=e=>{let{inputs:t,backend:n}=e,{dy:s,y:r}=t,a=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Zl(j9,s.shape,r.shape):new so(q9,s.shape,r.shape);return n.runWebGLProgram(a,[s,r],s.dtype)},X9={kernelName:bg,backendName:"webgl",kernelFunc:K9},Y9=`
return vec4(equal(a, b));
`,Q9="return float(a == b);",Z9=jt({opSnippet:Q9,packedOpSnippet:Y9,dtype:"bool",cpuKernelImpl:MK}),J9={kernelName:po,backendName:"webgl",kernelFunc:Z9},eQ=`
// Error function is calculated approximately with elementary function.
// See "Handbook of Mathematical Functions with Formulas,
// Graphs, and Mathematical Tables", Abramowitz and Stegun.
float p = ${S.ERF_P};
float a1 = ${S.ERF_A1};
float a2 = ${S.ERF_A2};
float a3 = ${S.ERF_A3};
float a4 = ${S.ERF_A4};
float a5 = ${S.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));
`,tQ=Ke({opSnippet:eQ}),nQ={kernelName:hl,backendName:"webgl",kernelFunc:tQ},sQ=iu+`
return exp(x);
`,rQ=`
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;
`,n2=Ke({opSnippet:sQ,packedOpSnippet:rQ,cpuKernelImpl:LK,dtype:"float32"}),aQ={kernelName:Oa,backendName:"webgl",kernelFunc:n2};function qm(e){let{inputs:t,attrs:n,backend:s}=e,{dim:r}=n,{input:a}=t,i=a.shape.length,o=a.shape.slice(),u=r;return r<0&&(w.assert(-(i+1)<=r,()=>`Axis must be in the interval [${-(i+1)}, ${i}]`),u=i+r+1),o.splice(u,0,1),he({inputs:{x:a},backend:s,attrs:{shape:o}})}var iQ={kernelName:ho,backendName:"webgl",kernelFunc:qm},Iw="return exp(x) - 1.0;",oQ=Ke({opSnippet:Iw,packedOpSnippet:Iw,cpuKernelImpl:BK}),uQ={kernelName:fo,backendName:"webgl",kernelFunc:oQ},Sw=class{constructor(e,t,n){this.variableNames=["real","imag"];let s=t[1];this.outputShape=t;let r=n?`2.0 * ${Math.PI}`:`-2.0 * ${Math.PI}`,a=n?`${s}.0`:"1.0",i;if(e==="real")i="return real * expR - imag * expI;";else if(e==="imag")i="return real * expI + imag * expR;";else throw new Error(`FFT component must be either "real" or "imag", got ${e}.`);this.userCode=`
const float exponentMultiplier = ${r};
float unaryOpComplex(float real, float expR, float imag, float expI) {
${i}
}
float mulMatDFT(int batch, int index) {
float indexRatio = float(index) / float(${s});
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio;
float result = 0.0;
for (int i = 0; i < ${s}; i++) {
// x = (-2|2 * PI / N) * index * i;
float x = exponentMultiplierTimesIndexRatio * float(i);
float expR = cos(x);
float expI = sin(x);
float real = getReal(batch, i);
float imag = getImag(batch, i);
result +=
unaryOpComplex(real, expR, imag, expI) / ${a};
}
return result;
}
void main() {
ivec2 coords = getOutputCoords();
setOutput(mulMatDFT(coords[0], coords[1]));
}
`}};function s2(e,t,n){let s=n.texData.get(e.dataId),r=w.sizeFromShape(e.shape),a=e.shape[e.shape.length-1],i=r/a,o=he({inputs:{x:e},backend:n,attrs:{shape:[i,a]}}),u=o.shape,l=new Sw("real",u,t),c=new Sw("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=Rr({inputs:{real:d,imag:h},backend:n});n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h);let m=he({inputs:{x:f},backend:n,attrs:{shape:e.shape}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(f),m}function lQ(e){let{inputs:t,backend:n}=e,{input:s}=t;return s2(s,!1,n)}var cQ={kernelName:yg,backendName:"webgl",kernelFunc:lQ},dQ=class{constructor(e,t){this.outputShape=[],this.customUniforms=[{name:"value",type:"float"}],this.variableNames=["x"],this.outputShape=e,this.userCode=`
void main() {
// Input can be obtained from uniform value.
setOutput(value);
}
`}};function ec(e){let{backend:t,attrs:n}=e,{shape:s,value:r}=n,{dtype:a}=n;if(a=a||w.inferDtype(r),a==="string"){let i=w.getArrayFromDType(a,w.sizeFromShape(s));return i.fill(r),t.makeTensorInfo(s,a,i)}else{let i=new dQ(s,r),o=[[r]];return t.runWebGLProgram(i,[],a,o)}}var pQ={kernelName:fl,backendName:"webgl",kernelFunc:ec},hQ=class{constructor(e){this.variableNames=["Image"],this.outputShape=[];let t=e[2];this.outputShape=e,this.userCode=`
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);
}
`}},fQ={kernelName:mo,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,s=t,r=new hQ(n.shape);return s.runWebGLProgram(r,[n],n.dtype)}},Cw="return floor(x);",mQ=Ke({opSnippet:Cw,packedOpSnippet:Cw,cpuKernelImpl:VK}),gQ={kernelName:Pa,backendName:"webgl",kernelFunc:mQ},bQ=`
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-01 15:12:04 +02:00
} else {
2022-04-01 15:13:32 +02:00
return NAN;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`,yQ=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (cond[1]) {
result[1] = idiv(ia[1], ib[1], s[1]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (cond[2]) {
result[2] = idiv(ia[2], ib[2], s[2]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (cond[3]) {
result[3] = idiv(ia[3], ib[3], s[3]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return vec4(result);
`,vQ=jt({opSnippet:bQ,packedOpSnippet:yQ,dtype:"int32"}),xQ={kernelName:za,backendName:"webgl",kernelFunc:vQ},wQ=class{constructor(e){this.variableNames=["A"];let t=fn(),[n,s]=e;this.outputShape=e,this.userCode=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
setOutput(floor(value * 255.0 + 0.5));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},kQ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=fn(),[n,s]=e;this.outputShape=e,this.userCode=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
${t.output} = result;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},IQ={kernelName:fd,backendName:"webgl",kernelFunc:SQ},Li;function SQ(e){let{inputs:t,backend:n,attrs:s}=e,{pixels:r}=t,{numChannels:a}=s,i=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,o=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,[u,l]=i?[r.videoWidth,r.videoHeight]:[r.width,r.height],c=[l,u],p=[l,u,a];(o||i)&&(Li==null&&(Li=document.createElement("canvas").getContext("2d")),Li.canvas.width=u,Li.canvas.height=l,Li.drawImage(r,0,0,u,l),r=Li.canvas);let d=n.makeTensorInfo(c,"int32");n.texData.get(d.dataId).usage=2,n.gpgpu.uploadPixelDataToTexture(n.getTexture(d.dataId),r);let h=X().getBool("WEBGL_PACK")?new kQ(p):new wQ(p),f=n.runWebGLProgram(h,[d],"int32");return n.disposeData(d.dataId),f}function CQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dataFormat:c,dilations:p,dimRoundingMode:d,activation:h,leakyreluAlpha:f}=s,m=S.convertConv2DDataFormat(c),g=S.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=Z1({x:r,filter:a,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:f});else if(X().getBool("WEBGL_CONV_IM2COL")&&r.shape[0]===1)b=J1({x:r,filter:a,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:f});else{let x=i!=null,k=o!=null,T=h==="leakyrelu",N=h?Kp(h,!1):null,E=new Q1(g,x,N,k,T),A=[r,a];if(i&&A.push(i),o&&A.push(o),T){let P=n.makeTensorInfo([],"float32",w.createScalarValue(f,"float32"));A.push(P),y.push(P)}b=n.runWebGLProgram(E,A,"float32")}let v=he({inputs:{x:b},backend:n,attrs:{shape:g.outShape}});return y.push(b),y.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var NQ={kernelName:aa,backendName:"webgl",kernelFunc:CQ};function TQ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:p,activation:d,leakyreluAlpha:h}=s,f=[],m=c;m==null&&(m=[1,1]),w.assert(S.eitherStridesOrDilationsAreOne(u,m),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${m}'`);let g=S.computeConv2DInfo(r.shape,a.shape,u,m,l,p,!0),b=X().getBool("WEBGL_PACK_DEPTHWISECONV")&&g.strideWidth<=2&&g.outChannels/g.inChannels===1,y=d?Kp(d,b):null,v=[r,a],x=i!=null,k=o!=null,T=d==="leakyrelu";if(x&&v.push(i),k&&v.push(o),T){let P=n.makeTensorInfo([],"float32",w.createScalarValue(h,"float32"));v.push(P),f.push(P)}let N;b?N=new t2(g,x,y,k,T):N=new e2(g,x,y,k,T);let E=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]],A=n.runWebGLProgram(N,v,"float32",E);return f.forEach(P=>n.disposeIntermediateTensorInfo(P)),A}var $Q={kernelName:ia,backendName:"webgl",kernelFunc:TQ},_Q=class{constructor(e,t,n){this.sliceDim=e,this.strides=t,this.variableNames=["x","indices"],this.outputShape=n;let s=rt(t.length),r=rt(n.length),a=this.sliceDim>1?"strides[j]":"strides";this.userCode=`
${s} strides = ${s}(${this.strides});
void main() {
${r} coords = getOutputCoords();
int flattenIndex = 0;
for (int j = 0; j < ${this.sliceDim}; j++) {
int index = round(getIndices(coords[0], j));
flattenIndex += index * ${a};
}
setOutput(getX(flattenIndex, coords[1]));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function AQ(e){let{inputs:t,backend:n}=e,{params:s,indices:r}=t,a=r.shape,i=a[a.length-1],o=w.sizeFromShape(s.shape),[u,l,c,p]=S.prepareAndValidate(s,r),d=he({inputs:{x:r},backend:n,attrs:{shape:[l,i]}}),h=he({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=WK(b,y,s.dtype,l,i,c,p,s.shape,o);return n.makeTensorInfo(u,s.dtype,v.values)}let f=new _Q(i,p,[l,c]),m=n.runWebGLProgram(f,[h,d],h.dtype),g=he({inputs:{x:m},backend:n,attrs:{shape:u}});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(m),g}var EQ={kernelName:bo,backendName:"webgl",kernelFunc:AQ},RQ=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=rt(this.rank),s=DQ(e,2);this.userCode=`
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-04-01 15:13:32 +02:00
`}};function DQ(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 r2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,indices:a}=t,{axis:i,batchDims:o}=s,u=w.parseAxisParam(i,r.shape)[0];if(X().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=S.segment_util.collectGatherOpShapeInfo(r,a,u,o),c=w.sizeFromShape(a.shape),p=[],d=he({inputs:{x:r},backend:n,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),h=he({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=UK(v,y,f);return p.forEach(k=>n.disposeIntermediateTensorInfo(k)),n.makeTensorInfo(l.outputShape,x.dtype,x.values)}let m=new RQ(d.shape,f),g=n.runWebGLProgram(m,[d,h],d.dtype);p.push(g);let b=he({inputs:{x:g},backend:n,attrs:{shape:l.outputShape}});return p.forEach(y=>n.disposeIntermediateTensorInfo(y)),b}var FQ={kernelName:go,backendName:"webgl",kernelFunc:r2},OQ="return float(a > b);",PQ=`
return vec4(greaterThan(a, b));
`,zQ=jt({opSnippet:OQ,packedOpSnippet:PQ,cpuKernelImpl:GK,dtype:"bool"}),MQ={kernelName:yo,backendName:"webgl",kernelFunc:zQ},LQ="return float(a >= b);",BQ=`
return vec4(greaterThanEqual(a, b));
`,VQ=jt({opSnippet:LQ,packedOpSnippet:BQ,dtype:"bool",cpuKernelImpl:HK}),WQ={kernelName:La,backendName:"webgl",kernelFunc:VQ};function UQ(e){let{inputs:t,backend:n}=e,{input:s}=t;return s2(s,!0,n)}var GQ={kernelName:vg,backendName:"webgl",kernelFunc:UQ},HQ="return float(!isnan(x) && !isinf(x));",qQ=Ke({opSnippet:HQ,dtype:"bool"}),jQ={kernelName:ml,backendName:"webgl",kernelFunc:qQ},KQ="return float(isinf(x));",XQ=Ke({opSnippet:KQ,dtype:"bool"}),YQ={kernelName:gl,backendName:"webgl",kernelFunc:XQ},QQ="return float(isnan(x));",ZQ=Ke({opSnippet:QQ,dtype:"bool"}),JQ={kernelName:bl,backendName:"webgl",kernelFunc:ZQ},eZ="return float(a < b);",tZ=`
return vec4(lessThan(a, b));
`,nZ=jt({opSnippet:eZ,packedOpSnippet:tZ,cpuKernelImpl:qK,dtype:"bool"}),sZ={kernelName:vo,backendName:"webgl",kernelFunc:nZ},rZ="return float(a <= b);",aZ=`
return vec4(lessThanEqual(a, b));
`,iZ=jt({opSnippet:rZ,packedOpSnippet:aZ,cpuKernelImpl:jK,dtype:"bool"}),oZ={kernelName:xo,backendName:"webgl",kernelFunc:iZ};function uZ(e){let{backend:t,attrs:n}=e,{start:s,stop:r,num:a}=n,i=KK(s,r,a);return t.makeTensorInfo([i.length],"float32",i)}var lZ={kernelName:xg,backendName:"webgl",kernelFunc:uZ},cZ=iu+`
return x < 0.0 ? 0./0. : log(x);
`,dZ=`
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;
`,pZ=Ke({opSnippet:cZ,packedOpSnippet:dZ,cpuKernelImpl:XK}),hZ={kernelName:Wa,backendName:"webgl",kernelFunc:pZ},fZ=iu+`
return log(1.0 + x);
`,mZ=Ke({opSnippet:fZ}),gZ={kernelName:yl,backendName:"webgl",kernelFunc:mZ},bZ="return float(a >= 1.0 && b >= 1.0);",yZ=`
return vec4(
vec4(greaterThanEqual(a, vec4(1.0))) *
vec4(greaterThanEqual(b, vec4(1.0))));
`,vZ=jt({opSnippet:bZ,packedOpSnippet:yZ,dtype:"bool"}),xZ={kernelName:wo,backendName:"webgl",kernelFunc:vZ},wZ="return float(!(x >= 1.0));",kZ=Ke({opSnippet:wZ}),IZ={kernelName:vl,backendName:"webgl",kernelFunc:kZ},SZ="return float(a >= 1.0 || b >= 1.0);",CZ=`
return min(
vec4(greaterThanEqual(a, vec4(1.0))) +
vec4(greaterThanEqual(b, vec4(1.0))),
vec4(1.0));
`,NZ=jt({opSnippet:SZ,packedOpSnippet:CZ,dtype:"bool"}),TZ={kernelName:Jd,backendName:"webgl",kernelFunc:NZ},$Z=class{constructor(e,t,n,s,r){this.variableNames=["x"],this.outputShape=[];let a=t,i=e[3]-1;this.outputShape=e;let o,u=`float(${n}) + float(${s}) * sum`;r===.5?o=`inversesqrt(${u})`:r===1?o=`1.0/(${u})`:o=`exp(log(${u}) * float(-${r}));`,this.userCode=`
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
int d = coords[3];
float x = getX(b, r, c, d);
float sum = 0.0;
for (int j = -${a}; j <= ${a}; j++) {
int idx = d + j;
if (idx >= 0 && idx <= ${i}) {
float z = getX(b, r, c, idx);
sum += z * z;
}
}
float val = x * ${o};
setOutput(val);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},_Z=class{constructor(e,t,n,s,r){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let a=t,i=e[3]-1;this.outputShape=e;let o,u=`float(${n}) + float(${s}) * sum`;r===.5?o=`inversesqrt(${u})`:r===1?o=`1.0/(${u})`:o=`exp(log(${u}) * float(-${r}));`,this.userCode=`
void main() {
ivec4 coords = getOutputCoords();
int b = coords.x;
int r = coords.y;
int c = coords.z;
int d = coords.w;
bool hasNextCol = d < ${this.outputShape[3]};
bool hasNextRow = c < ${this.outputShape[2]};
vec4 sum = vec4(0.);
vec4 xFragAtOutputCoords = getX(b, r, c, d);
vec4 xAtOutputCoords = vec4(
getChannel(xFragAtOutputCoords, vec2(c, d)),
hasNextCol ?
getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,
hasNextRow ?
getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,
(hasNextRow && hasNextCol) ?
getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0
);
int firstChannel = d - ${a};
vec2 cache = vec2(0.);
if(firstChannel >= 0){
vec4 firstChannelFrag = getX(b, r, c, firstChannel);
cache.x = getChannel(firstChannelFrag, vec2(c, firstChannel));
if(hasNextRow){
cache.y = getChannel(firstChannelFrag, vec2(c + 1, firstChannel));
}
}
ivec2 depth = ivec2(d, d + 1);
for (int j = - ${a}; j <= ${a}; j++) {
ivec2 idx = depth + j;
bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));
bvec2 belowUpperBound = lessThanEqual(idx, ivec2(${i}));
bool depthInRange = aboveLowerBound.x && belowUpperBound.x;
bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;
if(depthInRange || depthPlusOneInRange){
vec4 z = vec4(0.);
vec4 xFragAtCurrentDepth;
z.xz = cache.xy;
if(depthPlusOneInRange && hasNextCol){
xFragAtCurrentDepth = idx.y != d ?
getX(b, r, c, idx.y) : xFragAtOutputCoords;
z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));
if(hasNextRow){
z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));
}
}
cache.xy = z.yw;
sum += z * z;
}
}
vec4 result = xAtOutputCoords * ${o};
setOutput(result);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},AZ=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{depthRadius:a,bias:i,alpha:o,beta:u}=s,l=X().getBool("WEBGL_PACK_NORMALIZATION")?new _Z(r.shape,a,i,o,u):new $Z(r.shape,a,i,o,u);return n.runWebGLProgram(l,[r],r.dtype)},EZ={kernelName:ep,backendName:"webgl",kernelFunc:AZ},RZ=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=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
setOutput(result);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},DZ=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r,y:a,dy:i}=t,{depthRadius:o,bias:u,alpha:l,beta:c}=s,p=new RZ(r.shape,o,u,l,c);return n.runWebGLProgram(p,[r,a,i],r.dtype)},FZ={kernelName:wg,backendName:"webgl",kernelFunc:DZ};function OZ(e,t,n,s){let r=w.sizeFromShape(t),i=w.sizeFromShape(e.shape)/r,o=he({inputs:{x:e},attrs:{shape:[i,r]},backend:s}),u=xi(o,e.dtype,"max",s),l=he({inputs:{x:u},attrs:{shape:n},backend:s});return s.disposeIntermediateTensorInfo(o),s.disposeIntermediateTensorInfo(u),l}function a2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reductionIndices:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=S.getAxesPermutation(l,o),p=c!=null,d=n.shouldExecuteOnCPU([r]),h=r;if(p){if(d){let v=n.texData.get(h.dataId).values,x=new Array(o);for(let N=0;N<x.length;N++)x[N]=r.shape[c[N]];let k=wv(v,r.shape,r.dtype,c,x);h=n.makeTensorInfo(x,r.dtype);let T=n.texData.get(h.dataId);T.values=k}else h=Xp(r,c,n);l=S.getInnerMostAxes(l.length,o)}S.assertAxesAreInnerMostDims("max",l,o);let[f,m]=S.computeOutAndReduceShapes(h.shape,l),g=f;i&&(g=S.expandShapeToKeepDim(f,u));let b;if(d){let v=n.texData.get(h.dataId).values,x=YK(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=OZ(h,m,g,n);return p&&n.disposeIntermediateTensorInfo(h),b}var PZ={kernelName:Ua,backendName:"webgl",kernelFunc:a2},zZ=L1+`
return max(a, b);
`,MZ=`
vec4 result = vec4(max(a, b));
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
`+jp+`
return result;
`,LZ=jt({opSnippet:zZ,packedOpSnippet:MZ,cpuKernelImpl:QK}),BZ={kernelName:Ga,backendName:"webgl",kernelFunc:LZ};function VZ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t;tu(r,"maxPool");let{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1;w.assert(S.eitherStridesOrDilationsAreOne(i,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${i} and dilations '${l}'`);let c=S.computePool2DInfo(r.shape,a,i,l,o,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return kn({inputs:{x:r},backend:n});let p=new el(c,"max",!1);return n.runWebGLProgram(p,[r],r.dtype)}var WZ={kernelName:Ha,backendName:"webgl",kernelFunc:VZ};function UZ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dataFormat:u,dimRoundingMode:l}=s,c=[1,1,1],p=S.computePool3DInfo(r.shape,a,i,c,o,l,u),d=new Iv(p,"max",!1);return n.runWebGLProgram(d,[r],r.dtype)}var GZ={kernelName:tp,backendName:"webgl",kernelFunc:UZ},HZ=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideHeight,n=e.strideWidth,s=e.dilationHeight,r=e.effectiveFilterHeight,a=e.effectiveFilterWidth,i=r-1-e.padInfo.top,o=a-1-e.padInfo.left,u=r*a-1;this.userCode=`
const ivec2 pads = ivec2(${i}, ${o});
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${r};
wR += ${s}) {
float dyR = float(dyRCorner + wR) / ${t}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
for (int wC = 0; wC < ${a}; wC++) {
float dyC = float(dyCCorner + wC) / ${n}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
int maxPosValue = ${u} - int(getMaxPos(b, idyR, idyC, d));
// Get the current value, check it against the value from the
// position matrix.
int curPosValue = wR * ${a} + wC;
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
dotProd += dyValue * mask;
}
}
setOutput(dotProd);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},qZ=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideDepth,n=e.strideHeight,s=e.strideWidth,r=e.dilationDepth,a=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterDepth,u=e.effectiveFilterHeight,l=e.effectiveFilterWidth,c=o-1-e.padInfo.front,p=u-1-e.padInfo.top,d=l-1-e.padInfo.left,h=o*u*l-1;this.userCode=`
const ivec3 pads = ivec3(${c}, ${p}, ${d});
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyDCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
// Convolve dy(?, ?, ?, ch) with pos mask(:, :, :, d) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wD = 0; wD < ${o};
wD += ${r}) {
float dyD = float(dyDCorner + wD) / ${t}.0;
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
continue;
}
int idyD = int(dyD);
for (int wR = 0; wR < ${u};
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${n}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
for (int wC = 0; wC < ${l};
wC += ${i}) {
float dyC = float(dyCCorner + wC) / ${s}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
int maxPosValue = ${h} -
int(getMaxPos(batch, idyD, idyR, idyC, ch));
// Get the current value, check it against the value from the
// position matrix.
int curPosValue =
wD * ${u} * ${l} +
wR * ${l} + wC;
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
dotProd += dyValue * mask;
}
}
}
setOutput(dotProd);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function jZ(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a}=t,i=a,{filterSize:o,strides:u,pad:l,dimRoundingMode:c}=s,p=[1,1,1],d=S.computePool3DInfo(i.shape,o,u,p,l,c),h=new Iv(d,"max",!0),f=n.runWebGLProgram(h,[i],i.dtype),m=new qZ(d),g=n.runWebGLProgram(m,[r,f],i.dtype);return n.disposeIntermediateTensorInfo(f),g}var KZ={kernelName:Ig,backendName:"webgl",kernelFunc:jZ};function XZ(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,input:a,output:i}=t,o=a;tu([a,i],"maxPoolGrad");let{filterSize:u,strides:l,pad:c,dimRoundingMode:p}=s,d=S.computePool2DInfo(o.shape,u,l,1,c,p),h=!0,f=new el(d,"max",h),m=n.runWebGLProgram(f,[o],o.dtype),g=new HZ(d),b=n.runWebGLProgram(g,[r,m],o.dtype);return n.disposeIntermediateTensorInfo(m),b}var YZ={kernelName:kg,backendName:"webgl",kernelFunc:XZ};function QZ(e,t,n,s){let r=new el(n,"max",!1),a=s.runWebGLProgram(r,[e],"float32");r=new el(n,"max",!0,!0,t);let i=s.runWebGLProgram(r,[e],"float32");return[a,i]}var ZZ={kernelName:Sg,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{filterSize:r,strides:a,pad:i,includeBatchInIndex:o}=t,u=n;w.assert(s.shape.length===4,()=>`Error in maxPool: input must be rank 4 but got rank ${s.shape.length}.`);let l=[1,1];w.assert(S.eitherStridesOrDilationsAreOne(a,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${a} and dilations '${l}'`);let c=S.computePool2DInfo(s.shape,r,a,l,i),[p,d]=QZ(s,o,c,u);return[p,d]}};function JZ(e,t,n,s){let r=w.sizeFromShape(t),i=w.sizeFromShape(e.shape)/r,o=he({inputs:{x:e},attrs:{shape:[i,r]},backend:s}),u=xi(o,"float32","mean",s),l=he({inputs:{x:u},attrs:{shape:n},backend:s});return s.disposeIntermediateTensorInfo(o),s.disposeIntermediateTensorInfo(u),l}var e7={kernelName:qa,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{keepDims:r,axis:a}=t,i=n,o=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=S.getAxesPermutation(l,o),p=c!=null,d=i.shouldExecuteOnCPU([s]),h=[],f=s;if(p){if(d){let x=i.texData.get(f.dataId).values,k=new Array(o);for(let E=0;E<k.length;E++)k[E]=s.shape[c[E]];let T=wv(x,s.shape,s.dtype,c,k);f=i.makeTensorInfo(k,s.dtype);let N=i.texData.get(f.dataId);N.values=T}else f=Xp(s,c,i);h.push(f),l=S.getInnerMostAxes(l.length,o)}S.assertAxesAreInnerMostDims("sum",l,o);let[m,g]=S.computeOutAndReduceShapes(f.shape,l),b=m;r&&(b=S.expandShapeToKeepDim(m,u));let y=JZ(f,g,b,i);for(let v of h)i.disposeIntermediateTensorInfo(v);return y}};function t7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=S.getAxesPermutation(l,o),p=r;c!=null&&(p=qt({inputs:{x:r},backend:n,attrs:{perm:c}}),l=S.getInnerMostAxes(l.length,r.shape.length)),S.assertAxesAreInnerMostDims("min",l,o);let[d,h]=S.computeOutAndReduceShapes(p.shape,l),f=w.sizeFromShape(h),m=he({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),g=xi(m,m.dtype,"min",n),b;if(i){let y=S.expandShapeToKeepDim(d,u);b=he({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=he({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),b}var n7={kernelName:ja,backendName:"webgl",kernelFunc:t7},s7=L1+`
return min(a, b);
`,r7=`
vec4 result = vec4(min(a, b));
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
`+jp+`
return result;
`,a7=jt({opSnippet:s7,packedOpSnippet:r7,cpuKernelImpl:ZK}),i7={kernelName:Ka,backendName:"webgl",kernelFunc:a7},o7=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(","),i=t.map((l,c)=>l[0]+e[c]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,s),u=n==="reflect"?0:1;if(s===1){this.userCode=`
int start = ${a};
int end = ${i};
void main() {
int outC = getOutputCoords();
if (outC < start) {
outC = start * 2 - outC - ${u};
} else if(outC >= end) {
outC = (end - 1) * 2 - outC + ${u};
}
setOutput(getX(outC - start));
}
`;return}this.userCode=`
${r} start = ${r}(${a});
${r} end = ${r}(${i});
void main() {
${r} outC = getOutputCoords();
for (int i = 0; i < ${s}; i++) {
if (outC[i] < start[i]) {
outC[i] = start[i] * 2 - outC[i] - ${u};
} else if(outC[i] >= end[i]) {
outC[i] = (end[i] - 1) * 2 - outC[i] + ${u};
}
}
${r} coords = outC - start;
setOutput(getX(${o}));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},u7=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(","),i=t.map((h,f)=>h[0]+e[f]).join(","),o=ln("rc",s),u=ln("source",s),l=`${o[s-1]} < ${this.outputShape[s-1]}`,c=s===1?"source":`vec2(${u.slice(-2).join()})`,p=n==="reflect"?0:1,d="";if(s===1){let h=`
${r} source = rc;
if (source < start) {
source = start * 2 - source - ${p};
} else if (source >= end) {
source = (end - 1) * 2 - source + ${p};
}
source -= start;
`;d=`
${r} rc = outputLoc;
${h}
result[0] = getChannel(getX(${u.join()}), ${c});
${o[s-1]} += 1;
if(${l}) {
${h}
result[1] = getChannel(getX(${u.join()}), ${c});
}
`}else{let h=`
${r} source = rc;
${r} lt = ${r}(lessThan(source, start));
${r} gte = ${r}(greaterThanEqual(source, end));
${r} orig = 1 - (lt + gte);
source = orig * source +
lt * (start * 2 - source - ${p}) +
gte * ((end - 1) * 2 - source + ${p});
source -= start;
`;d=`
${r} rc = outputLoc;
${h}
result[0] = getChannel(getX(${u.join()}), ${c});
${o[s-1]} += 1;
if(${l}) {
${h}
result[1] = getChannel(getX(${u.join()}), ${c});
}
rc = outputLoc;
${o[s-2]} += 1;
if(${o[s-2]} < ${this.outputShape[s-2]}) {
${h}
result[2] = getChannel(getX(${u.join()}), ${c});
${o[s-1]} += 1;
if(${l}) {
${h}
result[3] = getChannel(getX(${u.join()}), ${c});
}
}
`}this.userCode=`
const ${r} start = ${r}(${a});
const ${r} end = ${r}(${i});
void main() {
${r} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${d}
setOutput(result);
}
`}},l7=({inputs:e,backend:t,attrs:n})=>{let{x:s}=e,{paddings:r,mode:a}=n,i=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new u7(s.shape,r,a):new o7(s.shape,r,a);return t.runWebGLProgram(i,[s],s.dtype)},c7={kernelName:Xa,backendName:"webgl",kernelFunc:l7},d7=`if (b == 0.0) return NAN;
return mod(a, b);`,p7=`
vec4 result = mod(a, b);
vec4 isNaN = vec4(equal(b, vec4(0.0)));
`+jp+`
return result;
`,h7=jt({opSnippet:d7,packedOpSnippet:p7}),f7={kernelName:xl,backendName:"webgl",kernelFunc:h7},m7=class{constructor(e,t,n){this.variableNames=["probs"],this.customUniforms=[{name:"seed",type:"float"}],this.outputShape=[e,n],this.userCode=`
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}));
}
`}},g7=`
if (a == b) {
return 1.0;
2022-04-01 15:12:04 +02:00
};
2022-04-01 15:13:32 +02:00
return a / b;`,b7=`
// 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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
if(a.y == b.y) {
result.y = 1.;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
if(a.z == b.z) {
result.z = 1.;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
if(a.w == b.w) {
result.w = 1.;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return result;
`,i2=jt({opSnippet:g7,packedOpSnippet:b7,checkOutOfBounds:!0}),y7={kernelName:Da,backendName:"webgl",kernelFunc:i2},Nw="return a - b;",o2=jt({opSnippet:Nw,packedOpSnippet:Nw,supportsComplex:!0,cpuKernelImpl:fX}),v7={kernelName:ci,backendName:"webgl",kernelFunc:o2};function u2(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{dim:a}=s,i=w.parseAxisParam([a],r.shape),o=a2({inputs:{x:r},backend:n,attrs:{reductionIndices:i,keepDims:!1}}),u=S.expandShapeToKeepDim(o.shape,i),l=he({inputs:{x:o},backend:n,attrs:{shape:u}}),c=o2({inputs:{a:r,b:l},backend:n}),p=n2({inputs:{x:c},backend:n}),d=Yp({inputs:{x:p},backend:n,attrs:{axis:i,keepDims:!1}}),h=he({inputs:{x:d},backend:n,attrs:{shape:u}}),f=i2({inputs:{a:p,b:h},backend:n});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(l),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}var x7={kernelName:ui,backendName:"webgl",kernelFunc:u2};function w7(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{numSamples:a,seed:i,normalized:o}=s,u=o?r:u2({inputs:{logits:r},backend:n,attrs:{dim:r.shape.length-1}}),l=u.shape[0],c=u.shape[1],p=new m7(l,c,a),d=[[i]],h=n.runWebGLProgram(p,[u],"int32",d);return o||n.disposeIntermediateTensorInfo(u),h}var k7={kernelName:Cg,backendName:"webgl",kernelFunc:w7},I7=ss+`
return -x;
`,S7=`
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;
`;function C7(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])){let a=n.texData.get(s.dataId),[i,o]=eX(a.values,s.shape,s.dtype);return n.makeTensorInfo(o,s.dtype,i)}let r;return X().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new Zr(s.shape,S7):r=new Hs(s.shape,I7),n.runWebGLProgram(r,[s],s.dtype)}var N7={kernelName:ko,backendName:"webgl",kernelFunc:C7},T7=xs.nonMaxSuppressionV3Impl;function $7(e){S.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u}=s,l=n.readSync(r.dataId),c=n.readSync(a.dataId),{selectedIndices:p}=T7(l,c,i,o,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var _7={kernelName:So,backendName:"webgl",kernelFunc:$7},A7=xs.nonMaxSuppressionV4Impl;function E7(e){S.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u,padToMaxOutputSize:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),{selectedIndices:d,validOutputs:h}=A7(c,p,i,o,u,l);return[n.makeTensorInfo([d.length],"int32",new Int32Array(d)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var R7={kernelName:wl,backendName:"webgl",kernelFunc:E7},D7=xs.nonMaxSuppressionV5Impl;function F7(e){S.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u,softNmsSigma:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),d=i,h=o,f=u,m=l,{selectedIndices:g,selectedScores:b}=D7(c,p,d,h,f,m);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var O7={kernelName:Co,backendName:"webgl",kernelFunc:F7},P7=class{constructor(e,t,n,s){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode=`
void main() {
ivec2 coords = getOutputCoords();
int index = round(getIndices(coords.x));
setOutput(mix(float(${s}), float(${n}),
float(index == coords.y)));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},z7=e=>{let{inputs:t,backend:n,attrs:s}=e,{indices:r}=t,{depth:a,onValue:i,offValue:o}=s,u=w.sizeFromShape(r.shape),l=new P7(u,a,i,o),c=he({inputs:{x:r},backend:n,attrs:{shape:[u]}}),p=n.runWebGLProgram(l,[c],r.dtype);n.disposeIntermediateTensorInfo(c);let d=[...r.shape,a],h=he({inputs:{x:p},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(p),h},M7={kernelName:To,backendName:"webgl",kernelFunc:z7};function Md(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="complex64"){let r=Jl({inputs:{input:s},backend:n}),a=Md({inputs:{x:r},backend:n}),i=Qp({inputs:{input:s},backend:n}),o=Md({inputs:{x:i},backend:n}),u=Rr({inputs:{real:a,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),u}else return ec({attrs:{shape:s.shape,dtype:s.dtype,value:s.dtype==="string"?"":0},backend:n})}var L7={kernelName:Go,backendName:"webgl",kernelFunc:Md};function l2(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=Jl({inputs:{input:s},backend:n}),a=l2({inputs:{x:r},backend:n}),i=Qp({inputs:{input:s},backend:n}),o=Md({inputs:{x:i},backend:n}),u=Rr({inputs:{real:a,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),u}else return ec({attrs:{shape:s.shape,dtype:s.dtype,value:1},backend:n})}var B7={kernelName:No,backendName:"webgl",kernelFunc:l2};function V7(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s;if(t.length===1)return qm({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let a=t[0].shape,i=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(i===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let o=[],u=t.map(c=>{let p=qm({inputs:{input:c},backend:n,attrs:{dim:r}});return o.push(p),p}),l=Y1({inputs:u,backend:n,attrs:{axis:r}});return o.forEach(c=>n.disposeIntermediateTensorInfo(c)),l}var W7={kernelName:$o,backendName:"webgl",kernelFunc:V7},U7=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(","),i=t.map((u,l)=>u[0]+e[l]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,s);if(s===1){this.userCode=`
int start = ${a};
int end = ${i};
void main() {
int outC = getOutputCoords();
if (outC < start || outC >= end) {
setOutput(value);
} else {
setOutput(getX(outC - start));
}
}
`;return}this.userCode=`
${r} start = ${r}(${a});
${r} end = ${r}(${i});
void main() {
${r} outC = getOutputCoords();
if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {
setOutput(value);
} else {
${r} coords = outC - start;
setOutput(getX(${o}));
}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},G7=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(","),i=t.map((f,m)=>f[0]+e[m]).join(","),o=ln("rc",s),u=ln("source",s),l=`${o[s-1]} < ${this.outputShape[s-1]}`,c=s===1?"source":`vec2(${u.slice(-2).join()})`,p=[`${r} rc = outputLoc;`,`${o[s-1]} += 1;
if(${l}) {
`,s===1?"":`}
rc = outputLoc;
${o[s-2]} += 1;
if(${o[s-2]} < ${this.outputShape[s-2]}) {`,s===1?"":` ${o[s-1]} += 1;
if(${l}) {`],d=s===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="";for(let f=0,m=s===1?2:4;f<m;f++)h+=`
${p[f]}
if (${d}) {
result[${f}] = float(value);
} else {
${r} source = rc - start;
result[${f}] = getChannel(getX(${u.join()}), ${c});
}
`;h+=s===1?"} ":"}}",this.userCode=`
const ${r} start = ${r}(${a});
const ${r} end = ${r}(${i});
void main() {
${r} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${h}
setOutput(result);
}
`}},c2=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{paddings:a,constantValue:i}=s;if(w.sizeFromShape(r.shape)===0){let l=a.map((c,p)=>c[0]+r.shape[p]+c[1]);return ec({backend:n,attrs:{shape:l,value:i,dtype:r.dtype}})}let o=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new G7(r.shape,a,i):new U7(r.shape,a,i),u=[[i]];return n.runWebGLProgram(o,[r],r.dtype,u)},H7={kernelName:Qa,backendName:"webgl",kernelFunc:c2},q7=`
if(a < 0.0 && floor(b) < b){
return NAN;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
if (b == 0.0) {
return 1.0;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
return (round(mod(b, 2.0)) != 1) ?
pow(abs(a), b) : sign(a) * pow(abs(a), b);
`,j7=`
// 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));
`+jp+`
return result;
`,K7=jt({opSnippet:q7,packedOpSnippet:j7}),X7={kernelName:Za,backendName:"webgl",kernelFunc:K7};function Y7(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s,o=r.shape.length,u=[],l=w.parseAxisParam(a,r.shape),c=l,p=S.getAxesPermutation(c,o),d=r;p!=null&&(d=qt({inputs:{x:r},backend:n,attrs:{perm:p}}),c=S.getInnerMostAxes(c.length,o),u.push(d)),S.assertAxesAreInnerMostDims("prod",c,o);let h;if(n.shouldExecuteOnCPU([d])){let f=n.texData.get(d.dataId).values,{outVals:m,outShape:g,outDtype:b}=nX(d.shape,d.dtype,f,c);h=n.makeTensorInfo(g,b,m)}else{let[f,m]=S.computeOutAndReduceShapes(d.shape,c),g=w.sizeFromShape(m),b=he({inputs:{x:d},backend:n,attrs:{shape:[-1,g]}}),y=cp(r.dtype),v=xi(b,y,"prod",n);h=he({inputs:{x:v},backend:n,attrs:{shape:f}}),u.push(b),u.push(v)}if(i){u.push(h);let f=S.expandShapeToKeepDim(h.shape,l);h=he({inputs:{x:h},backend:n,attrs:{shape:f}})}return u.forEach(f=>n.disposeIntermediateTensorInfo(f)),h}var Q7={kernelName:_o,backendName:"webgl",kernelFunc:Y7},d2=e=>{let{backend:t,attrs:n}=e,{start:s,stop:r,step:a,dtype:i}=n,o=sX(s,r,a,i);return t.makeTensorInfo([o.length],i,o)},Z7={kernelName:kl,backendName:"webgl",kernelFunc:d2},J7="return 1.0 / x;",eJ=Ke({opSnippet:J7}),tJ={kernelName:Il,backendName:"webgl",kernelFunc:eJ},nJ=ss+`
return (x < 0.0) ? 0.0 : x;
`,sJ=`
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;
`,rJ=Ke({opSnippet:nJ,packedOpSnippet:sJ}),aJ={kernelName:ei,backendName:"webgl",kernelFunc:rJ},iJ=ss+`
return (x < 0.0) ? 0.0 : min(6.0, x);
`,oJ=`
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;
`,uJ=Ke({opSnippet:iJ,packedOpSnippet:oJ}),lJ={kernelName:ni,backendName:"webgl",kernelFunc:uJ},cJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p;r?p="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":p="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
const vec2 effectiveInputOverOutputRatioRC = vec2(
${l[0]/c[0]},
${l[1]/c[1]});
const vec2 inputShapeRC = vec2(${i}.0, ${o}.0);
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
// Fractional source index.
vec2 sourceFracIndexRC = ${p};
// Compute the four integer indices.
ivec2 sourceFloorRC = ivec2(max(sourceFracIndexRC, vec2(0.0)));
ivec2 sourceCeilRC = ivec2(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);
float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);
float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);
float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);
vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);
float top = topLeft + (topRight - topLeft) * fracRC.y;
float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
float newValue = top + (bottom - top) * fracRC.x;
setOutput(newValue);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},dJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p;r?p="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":p="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
const vec3 effectiveInputOverOutputRatioRC = vec3(
${l[0]/c[0]},
${l[1]/c[1]},
${l[1]/c[1]});
const vec3 inputShapeRC = vec3(${i}.0, ${o}.0,
${o}.0);
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function pJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:i,size:o}=s,[u,l]=o,c=X().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new dJ(r.shape,u,l,a,i):new cJ(r.shape,u,l,a,i);return n.runWebGLProgram(c,[r],"float32")}var hJ={kernelName:ti,backendName:"webgl",kernelFunc:pJ},fJ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,s,r]=t,[,a,i]=e,o=[n&&a>1?s-1:s,n&&i>1?r-1:r],u=[n&&a>1?a-1:a,n&&i>1?i-1:i],l=o[0]/u[0],c=o[1]/u[1],p=1/l,d=1/c,h=Math.ceil(p)*2+2,f=Math.ceil(d)*2+2;this.userCode=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
// Guard against the window exceeding the bounds of dy
if (dyC < 0 || dyC >= ${i}) {
continue;
}
float dxR = float(dyR) * heightScale;
int topDxRIndex = int(floor(dxR));
int bottomDxRIndex = int(min(ceil(dxR), ${s-1}.0));
float dxRLerp = dxR - float(topDxRIndex);
float inverseDxRLerp = 1.0 - dxRLerp;
float dxC = float(dyC) * widthScale;
int leftDxCIndex = int(floor(dxC));
int rightDxCIndex = int(min(ceil(dxC), ${r-1}.0));
float dxCLerp = dxC - float(leftDxCIndex);
float inverseDxCLerp = 1.0 - dxCLerp;
if (r == topDxRIndex && c == leftDxCIndex) {
// topLeft
accumulator +=
getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;
}
if (r == topDxRIndex && c == rightDxCIndex) {
// topRight
accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;
}
if (r == bottomDxRIndex && c == leftDxCIndex) {
// bottomLeft
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;
}
if (r == bottomDxRIndex && c == rightDxCIndex) {
// bottomRight
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;
}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
}
// End loop over dy
setOutput(accumulator);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function mJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r,dy:a}=t,{alignCorners:i}=s,o=new fJ(a.shape,r.shape,i);return n.runWebGLProgram(o,[a],a.dtype)}var gJ={kernelName:Tg,backendName:"webgl",kernelFunc:mJ},bJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p=s?"0.5":"0.0",d;r?d="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":d="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
const vec2 effectiveInputOverOutputRatioRC = vec2(
${l[0]/c[0]},
${l[1]/c[1]});
const vec2 inputShapeRC = vec2(${i}.0, ${o}.0);
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
// Fractional source index.
vec2 sourceFracIndexRC = ${d};
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestRC = ivec2(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${p})));
float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);
setOutput(newValue);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},yJ=class{constructor(e,t,n,s,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,i,o,u]=e;this.outputShape=[a,t,n,u];let l=[s&&t>1?i-1:i,s&&n>1?o-1:o],c=[s&&t>1?t-1:t,s&&n>1?n-1:n],p=s?"0.5":"0.0",d;r?d="max((vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC, vec3(0.0))":d="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
const vec3 effectiveInputOverOutputRatioRC = vec3(
${l[0]/c[0]},
${l[1]/c[1]},
${l[1]/c[1]});
const vec3 inputShapeRC = vec3(${i}.0, ${o}.0,
${o}.0);
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function vJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:i,size:o}=s,[u,l]=o,c=X().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new yJ(r.shape,u,l,a,i):new bJ(r.shape,u,l,a,i);return n.runWebGLProgram(c,[r],r.dtype)}var xJ={kernelName:Sl,backendName:"webgl",kernelFunc:vJ},wJ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,s,r]=t,[,a,i]=e,o=[n&&a>1?s-1:s,n&&i>1?r-1:r],u=[n&&a>1?a-1:a,n&&i>1?i-1:i],l=o[0]/u[0],c=o[1]/u[1],p=1/l,d=1/c,h=Math.ceil(p)*2+2,f=Math.ceil(d)*2+2;this.userCode=`
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
float accumulator = 0.0;
const float heightScale = float(${l});
const float widthScale = float(${c});
const float invHeightScale = float(${p});
const float invWidthScale = float(${d});
const int winHeight = int(${h});
const int winWidth = int(${f});
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(floor(startRLerp - float(winHeight / 2)));
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(floor(startCLerp - float(winWidth / 2)));
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
// Guard against the window exceeding the bounds of dy
if (dyR < 0 || dyR >= ${a}) {
continue;
}
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
// Guard against the window exceeding the bounds of dy
if (dyC < 0 || dyC >= ${i}) {
continue;
}
float sourceFracRow =
float(${o[0]}) *
(float(dyR) / float(${u[0]}));
float sourceFracCol =
float(${o[1]}) *
(float(dyC) / float(${u[1]}));
int sourceNearestRow = int(min(
float(int(${s}) - 1),
${n} ? float(round(sourceFracRow)) :
float(floor(sourceFracRow))));
int sourceNearestCol = int(min(
float(int(${r}) - 1),
${n} ? float(round(sourceFracCol)) :
float(floor(sourceFracCol))));
if (r == sourceNearestRow && c == sourceNearestCol) {
accumulator += getDy(b, dyR, dyC, d);
}
}
}
// End loop over dy
setOutput(accumulator);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function kJ(e){let{inputs:t,backend:n,attrs:s}=e,{images:r,dy:a}=t,{alignCorners:i}=s,o=new wJ(a.shape,r.shape,i);return n.runWebGLProgram(o,[a],a.dtype)}var IJ={kernelName:Ng,backendName:"webgl",kernelFunc:kJ},SJ=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=`
void main() {
int coord = getOutputCoords();
setOutput(getX(${e[0]} - coord - 1));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;return}let s=i=>t.indexOf(i)!==-1&&e[i]!==1?`${e[i]} - coords[${i}] - 1`:`coords[${i}]`,r=e.map((i,o)=>s(o)).join(","),a=rt(n);this.userCode=`
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${r}));
}
`}},CJ=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]}`,i=rt(n);n===1?this.userCode=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
setOutput(result);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:this.userCode=`
void main() {
${i} rc = getOutputCoords();
vec4 result = vec4(0.);
result.r = ${o(s.slice())};
if(${r}){
result.g = ${u(s.slice())};
}
if(${a}) {
result.b = ${l(s.slice())};
if(${r}) {
result.a = ${c(s.slice())};
}
}
setOutput(result);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;function o(h){return p(h)}function u(h){return h[n-1]="("+h[n-1]+" + 1)",p(h)}function l(h){return h[n-2]="("+h[n-2]+" + 1)",p(h)}function c(h){return h[n-1]="("+h[n-1]+" + 1)",h[n-2]="("+h[n-2]+" + 1)",p(h)}function p(h){let f=e.map((b,y)=>d(y,h)),m=f.join(","),g=f.slice(-2).join(",");return`getChannel(getX(${m}), vec2(${g}))`}function d(h,f){return t.indexOf(h)!==-1&&e[h]!==1?`${e[h]} - ${f[h]} - 1`:`${f[h]}`}}};function NJ(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dims:a}=s,i=r.shape.length,o=w.parseAxisParam(a,r.shape);if(i===0)return kn({inputs:{x:r},backend:n});let u=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new CJ(r.shape,o):new SJ(r.shape,o);return n.runWebGLProgram(u,[r],r.dtype)}var TJ={kernelName:Eo,backendName:"webgl",kernelFunc:NJ},$J=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=`
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},_J={kernelName:Ho,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:s}=e,{radians:r,fillValue:a,center:i}=t,o=n,u=new $J(s.shape,a),[l,c]=S.getImageCenter(i,s.shape[1],s.shape[2]),p=[[l,c,Math.sin(r),Math.cos(r)]];return o.runWebGLProgram(u,[s],s.dtype,p)}},AJ=`
// 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-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`,EJ=Ke({opSnippet:AJ}),RJ={kernelName:Ro,backendName:"webgl",kernelFunc:EJ},DJ="return inversesqrt(x);",FJ=Ke({opSnippet:DJ,cpuKernelImpl:rX}),OJ={kernelName:si,backendName:"webgl",kernelFunc:FJ},p2=class{constructor(e,t,n,s,r,a,i=!0){this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;let o=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=`
${o} strides = ${o}(${r});
void main() {
${u} coords = getOutputCoords();
float sum = 0.0;
bool found = false;
for (int i = 0; i < ${e}; i++) {
int flattenedIndex = 0;
for (int j = 0; j < ${t}; j++) {
int index = round(${c});
flattenedIndex += index * ${h};
}
if (flattenedIndex == coords[0]) {
sum += ${d};
found = true;
}
}
setOutput(mix(getDefaultValue(), sum, float(found)));
}
`}};function PJ(e){let{inputs:t,backend:n,attrs:s}=e,{indices:r,updates:a}=t,{shape:i}=s,{sliceRank:o,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=S.calculateShapes(a,r,i),d=[p/l,l];if(p===0)return n.makeTensorInfo(i,r.dtype);let h=he({inputs:{x:r},backend:n,attrs:{shape:[u,o]}}),f=he({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=n.makeTensorInfo([],"float32",new Float32Array([0])),g=new p2(u,o,h.shape.length,f.shape.length,c,d),b=n.runWebGLProgram(g,[f,h,m],f.dtype),y=he({inputs:{x:b},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(b),n.disposeIntermediateTensorInfo(m),y}var zJ={kernelName:Do,backendName:"webgl",kernelFunc:PJ},MJ=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.outputShape=t;let s,r;if(n>4)throw Error(`Where for rank ${n} is not yet supported`);if(n===1)r="resRC",s="resRC";else{let i=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],u=[];for(let l=0;l<t.length;l++)u.push(`${i[l]}`),l<e&&o.push(`${i[l]}`);s=o.join(),r=u.join()}let a=rt(n);this.userCode=`
void main() {
${a} resRC = getOutputCoords();
float cVal = getC(${s});
if (cVal >= 1.0) {
setOutput(getA(${r}));
} else {
setOutput(getB(${r}));
}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function LJ(e){let{inputs:t,backend:n}=e,{condition:s,t:r,e:a}=t,i=new MJ(s.shape.length,r.shape,r.shape.length);return n.runWebGLProgram(i,[s,r,a],cn(r.dtype,a.dtype))}var BJ={kernelName:Fo,backendName:"webgl",kernelFunc:LJ},VJ=`
// Stable and Attracting Fixed Point (0, 1) for Normalized Weights.
// see: https://arxiv.org/abs/1706.02515
float scaleAlpha = ${S.SELU_SCALEALPHA};
float scale = ${S.SELU_SCALE};
return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);
`,WJ=Ke({opSnippet:VJ}),UJ={kernelName:Cl,backendName:"webgl",kernelFunc:WJ},GJ=iu+`
return 1.0 / (1.0 + exp(-1.0 * x));
`,HJ=`
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;
`,qJ=Ke({opSnippet:GJ,packedOpSnippet:HJ,cpuKernelImpl:aX}),jJ={kernelName:ai,backendName:"webgl",kernelFunc:qJ},KJ=`
if (isnan(x)) { return 0.0; }
return sign(x);
`,XJ=Ke({opSnippet:KJ}),YJ={kernelName:Nl,backendName:"webgl",kernelFunc:XJ},QJ=iu+`
return sin(x);
`,ZJ=Ke({opSnippet:QJ}),JJ={kernelName:ri,backendName:"webgl",kernelFunc:ZJ},eee=`
float e2x = exp(x);
return (e2x - 1.0 / e2x) / 2.0;
`,tee=Ke({opSnippet:eee}),nee={kernelName:Po,backendName:"webgl",kernelFunc:tee},see=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
else if (too_small){
result = exp_x;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
else{
result = log(exp_x + 1.0);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return result;
`,ree=Ke({opSnippet:see}),aee={kernelName:Tl,backendName:"webgl",kernelFunc:ree},iee=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,paddings:i}=s;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let o=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...i);for(let b=1+a.length;b<r.shape.length;++b)u.push([0,0]);let l=[],c=c2({inputs:{x:r},backend:n,attrs:{paddings:u,constantValue:0}}),p=S.getReshaped(c.shape,a,o,!1),d=S.getPermuted(p.length,a.length,!1),h=S.getReshapedPermuted(c.shape,a,o,!1),f=he({inputs:{x:c},backend:n,attrs:{shape:p}}),m=qt({inputs:{x:f},backend:n,attrs:{perm:d}}),g=he({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},oee={kernelName:zo,backendName:"webgl",kernelFunc:iee};function uee(e){let{inputs:t,backend:n}=e,{indices:s,values:r,denseShape:a,defaultValue:i}=t;if(a.shape.length!==1)throw new Error(`Dense shape must be a vector, saw:
${a.shape}`);if(s.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${s.shape}`);if(r.shape.length!==1)throw new Error(`Values must be a vector, saw:
${r.shape}`);if(i.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
${i.shape}`);let o=n.readSync(s.dataId),u=n.readSync(r.dataId),l=n.readSync(a.dataId),c=n.readSync(i.dataId)[0],[p,d,h,f,m]=oX(o,s.shape,s.dtype,u,r.dtype,l,c);return[n.makeTensorInfo(d,s.dtype,p),n.makeTensorInfo([d[0]],r.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(g=>Number(g)))),n.makeTensorInfo([m.length],s.dtype,new Int32Array(m))]}var lee={kernelName:sp,backendName:"webgl",kernelFunc:uee};function cee(e){let{inputs:t,backend:n}=e,{inputIndices:s,inputShape:r,newShape:a}=t;if(s.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape ${s.shape}`);if(r.shape.length!==1)throw new Error(`Input shape should be a vector but received shape ${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let i=Array.from(n.readSync(r.dataId)),o=n.readSync(s.dataId),u=Array.from(n.readSync(a.dataId)),[l,c,p]=uX(o,s.shape,s.dtype,i,u);return[n.makeTensorInfo(c,s.dtype,l),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var dee={kernelName:$l,backendName:"webgl",kernelFunc:cee};function pee(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
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);let i=n.readSync(s.dataId),o=n.readSync(r.dataId),u=n.readSync(a.dataId),[l,c]=F1(i,s.shape,s.dtype,o,u,!0);return n.makeTensorInfo(c,s.dtype,l)}var hee={kernelName:rp,backendName:"webgl",kernelFunc:pee};function fee(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
${r.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);let i=n.readSync(s.dataId),o=n.readSync(r.dataId),u=n.readSync(a.dataId),[l,c]=F1(i,s.shape,s.dtype,o,u);return n.makeTensorInfo(c,s.dtype,l)}var mee={kernelName:ap,backendName:"webgl",kernelFunc:fee};function gee(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:i}=t,{outputShape:o}=s,{sliceRank:u,numUpdates:l,strides:c,outputSize:p}=S.calculateShapes(a,r,o),d=!1,h=new p2(l,u,r.shape.length,a.shape.length,c,[p,1],d),f=n.runWebGLProgram(h,[a,r,i],a.dtype),m=he({inputs:{x:f},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(f),m}var bee={kernelName:ip,backendName:"webgl",kernelFunc:gee};function yee(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=s,o=w.parseAxisParam(i,r.shape)[0],u=S.prepareSplitSize(r,a,o),l=r.shape.length,c=new Array(l).fill(0),p=r.shape.slice();return u.map(d=>{let h=[...p];h[o]=d;let f=ou({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[o]+=d,f})}var vee={kernelName:Mo,backendName:"webgl",kernelFunc:yee},Tw="return sqrt(x);",xee=Ke({opSnippet:Tw,packedOpSnippet:Tw,cpuKernelImpl:lX}),wee={kernelName:ii,backendName:"webgl",kernelFunc:xee},kee="return x * x;",Iee=Ke({opSnippet:kee}),See={kernelName:_l,backendName:"webgl",kernelFunc:Iee},$w="return (a - b) * (a - b);",Cee=jt({opSnippet:$w,packedOpSnippet:$w}),Nee={kernelName:li,backendName:"webgl",kernelFunc:Cee};function Tee({inputs:e,attrs:t,backend:n}){let{x:s}=e,r=ss+`
return x > 0.0 ? 1.0 : float(${t.alpha});
`,a=new Hs(s.shape,r);return n.runWebGLProgram(a,[s],s.dtype)}var $ee={kernelName:hi,backendName:"webgl",kernelFunc:Tee},_ee=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let s=n.length,r=rt(n.length),a=rt(n.length),i="";if(s===1)i="coords * strides + begin";else{let o=0;i=n.map((u,l)=>(o++,n.length===1?`coords * strides[${l}] + begin[${l}]`:`coords[${o-1}] * strides[${l}] + begin[${l}]`)).join(",")}this.userCode=`
${r} begin = ${r}(${e});
${r} strides = ${r}(${t});
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${i}));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function Aee(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=wt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=he({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 N=wt.computeOutShape(y,v,x),E=ou({inputs:{x:r},backend:n,attrs:{begin:y,size:N}});k=he({inputs:{x:E},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(E)}else if(n.shouldExecuteOnCPU([r])){let E=n.readSync(r.dataId),A=De(r.shape,r.dtype,E),P=cX(h,A,x,y);k=n.makeTensorInfo(f,r.dtype,P.values)}else{let E=new _ee(y,x,h);k=n.runWebGLProgram(E,[r],r.dtype)}let T=he({inputs:{x:k},backend:n,attrs:{shape:f}});return n.disposeIntermediateTensorInfo(k),T}var Eee={kernelName:Lo,backendName:"webgl",kernelFunc:Aee};function Ree(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:i,rightPad:o,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.readSync(c.dataId),h=n.readSync(p.dataId),[f,m]=dX(d,h,r,a,i,o,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var Dee={kernelName:op,backendName:"webgl",kernelFunc:Ree};function Fee(e){let{inputs:t,backend:n,attrs:s}=e,{skipEmpty:r}=s,{input:a,delimiter:i}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(i.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${i.shape}`);let o=n.readSync(a.dataId),u=n.readSync(i.dataId)[0],[l,c,p]=pX(o,u,r),d=c.length;return[n.makeTensorInfo([d,2],"int32",l),n.makeTensorInfo([d],"string",c),n.makeTensorInfo([2],"int32",new Int32Array(p))]}var Oee={kernelName:$g,backendName:"webgl",kernelFunc:Fee};function Pee(e){let{inputs:t,backend:n,attrs:s}=e,{numBuckets:r}=s,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(r<=0)throw new Error("Number of buckets must be at least 1");let i=n.readSync(a.dataId),o=hX(i,r);return n.makeTensorInfo(a.shape,"int32",o)}var zee={kernelName:_g,backendName:"webgl",kernelFunc:Pee},Mee="return tan(x);",Lee=Ke({opSnippet:Mee}),Bee={kernelName:Bo,backendName:"webgl",kernelFunc:Lee},Vee=`
float e2x = exp(-2.0 * abs(x));
return sign(x) * (1.0 - e2x) / (1.0 + e2x);
`,Wee=Ke({opSnippet:Vee}),Uee={kernelName:di,backendName:"webgl",kernelFunc:Wee},Gee=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=Hee(e);this.userCode=`
void main() {
${s} resRC = getOutputCoords();
setOutput(getA(${r}));
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}};function Hee(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 h2(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=De(r.shape,r.dtype,l),p=mX(c,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let i=new Gee(r.shape,a);return n.runWebGLProgram(i,[r],r.dtype)}var qee={kernelName:Cr,backendName:"webgl",kernelFunc:h2},jee=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=`
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));
}
}
`}},Kee=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=`
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));
}
`}};function Ur(e,t){t!==null&&e.disposeIntermediateTensorInfo(t)}function _w(e){let t=1;for(;t<e;)t*=2;return t}function Xee(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:i}=s,o=X().getNumber("TOPK_LAST_DIM_CPU_HANDOFF_SIZE_THRESHOLD"),u=X().getNumber("TOPK_K_CPU_HANDOFF_THRESHOLD"),l=r.shape,c=l[l.length-1];if(n.shouldExecuteOnCPU([r])||c<o||a>u){let P=n.readSync(r.dataId),[R,F]=gX(P,l,r.dtype,a,i);return[n.makeTensorInfo(R.shape,R.dtype,R.values),n.makeTensorInfo(F.shape,F.dtype,F.values)]}if(a===0)return l[l.length-1]=0,[n.makeTensorInfo(l,r.dtype,[]),n.makeTensorInfo(l,"int32",[])];if(c===1)return[r,ec({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=he({inputs:{x:h},attrs:{shape:[m,c]},backend:n});d&&Ur(n,h);let b=_w(a),y=_w(c),v=null,x=()=>v===null?[g,g]:[g,v],k=(P,R,F)=>{let $=x(),z=new jee(F),q=[[c],[v===null?1:0],[Number.NEGATIVE_INFINITY],[P],[R]],K=v;v=n.runWebGLProgram(z,$,"int32",q),Ur(n,K)};for(let P=1;P<b;P*=2){let R=P*2;for(let F=P;F>=1;F/=2)k(R,F,[m,y])}for(let P=y;P>b;P/=2){let R=x(),F=new Kee([m,P/2]),z=[[c],[v===null?1:0],[b]],W=v;v=n.runWebGLProgram(F,R,"int32",z),Ur(n,W);let q=b/2,K=q*2;for(let Y=q;Y>=1;Y/=2)k(K,Y,v.shape)}let T=v;v=ou({inputs:{x:v},backend:n,attrs:{begin:0,size:[m,a]}}),Ur(n,T);let N=r2({inputs:{x:g,indices:v},backend:n,attrs:{axis:1,batchDims:1}});Ur(n,g);let E=l.slice(0,-1);E.push(a),T=v,v=he({inputs:{x:v},attrs:{shape:E},backend:n}),Ur(n,T);let A=N;return N=he({inputs:{x:N},attrs:{shape:E},backend:n}),Ur(n,A),[N,v]}var Yee={kernelName:Vo,backendName:"webgl",kernelFunc:Xee},Qee=class{constructor(e,t,n,s,r,a){this.variableNames=["Image","Transforms"],this.outputShape=a;let i=n==="nearest"?1:2,o;switch(s){case"constant":o=1;break;case"reflect":o=2;break;case"wrap":o=3;break;case"nearest":o=4;break;default:o=1;break}this.userCode=`
float mapCoord(float outCoord, float len) {
float inCoord = outCoord;
if(${o} == 2) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz2 = 2.0 * len;
if (inCoord < sz2) {
inCoord = sz2 * float(int(float(-inCoord / sz2))) +
inCoord;
}
inCoord = inCoord < -len ? inCoord + sz2 : -inCoord - 1.0;
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz2 = 2.0 * len;
inCoord -= sz2 * float(int(float(inCoord / sz2)));
if (inCoord >= len) {
inCoord = sz2 - inCoord - 1.0;
}
}
}
return clamp(inCoord, 0.0, len - 1.0);
} else if (${o} == 3) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz = len - 1.0;
inCoord += len * (float(int(float(-inCoord / sz))) + 1.0);
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz = len - 1.0;
inCoord -= len * float(int(float(inCoord / sz)));
}
}
return clamp(inCoord, 0.0, len - 1.0);
} else if (${o} == 4) {
return clamp(outCoord, 0.0, len - 1.0);
} else {
return outCoord;
}
}
float readWithFillValue(int batch, int coordY, int coordX,
int channel) {
float outputValue;
if (0 <= coordY && coordY < ${e} && 0 <= coordX && coordX < ${t}) {
outputValue = getImage(batch, coordY, coordX, channel);
} else {
outputValue = float(${r});
}
return outputValue;
}
void main() {
ivec4 coords = getOutputCoords();
float outputValue;
int batch = coords[0];
int x = coords[2];
int y = coords[1];
int channel = coords[3];
float xf = float(x);
float yf = float(y);
float a1 = getTransforms(batch, 0);
float a2 = getTransforms(batch, 1);
float a3 = getTransforms(batch, 2);
float b1 = getTransforms(batch, 3);
float b2 = getTransforms(batch, 4);
float b3 = getTransforms(batch, 5);
float c1 = getTransforms(batch, 6);
float c2 = getTransforms(batch, 7);
float projection = c1 * xf + c2 * yf + 1.0;
if (projection == 0.0) {
outputValue = float(${r});
} else {
float inX = (a1 * xf + a2 * yf + a3) / projection;
float inY = (b1 * xf + b2 * yf + b3) / projection;
float mapX = mapCoord(inX, float(${t}));
float mapY = mapCoord(inY, float(${e}));
if (${i} == 1) {
int coordY = int(round(mapY));
int coordX = int(round(mapX));
outputValue = readWithFillValue(batch, coordY, coordX,
channel);
} else {
float yFloor = floor(mapY);
float xFloor = floor(mapX);
float yCeil = yFloor + 1.0;
float xCeil = xFloor + 1.0;
float valueYFloor = (xCeil - mapX) *
readWithFillValue(batch, int(yFloor), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yFloor), int(xCeil), channel);
float valueYCeil = (xCeil - mapX) *
readWithFillValue(batch, int(yCeil), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yCeil), int(xCeil), channel);
outputValue = (yCeil - mapY) * valueYFloor +
(mapY - yFloor) * valueYCeil;
}
}
setOutput(outputValue);
}
`}};function Zee(e){let{inputs:t,backend:n,attrs:s}=e,{image:r,transforms:a}=t,{interpolation:i,fillMode:o,fillValue:u,outputShape:l}=s,[c,p,d,h]=r.shape,[f,m]=l!=null?l:[p,d],g=[c,f,m,h],b=new Qee(p,d,i,o,u,g);return n.runWebGLProgram(b,[r,a],"float32")}var Jee={kernelName:Wo,backendName:"webgl",kernelFunc:Zee};function ete(e){let{inputs:t,attrs:n,backend:s}=e,{axis:r}=n,{x:a}=t;tu(a,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let i=s.readSync(a.dataId),{outputValues:o,outputShape:u,indices:l}=bX(i,r,a.shape,a.dtype);return[s.makeTensorInfo(u,a.dtype,o),s.makeTensorInfo([l.length],"int32",l)]}var tte={kernelName:Ag,backendName:"webgl",kernelFunc:ete};function nte(e){let{inputs:t,backend:n,attrs:s}=e,{value:r}=t,{axis:a}=s;a<0&&(a+=r.shape.length);let i=r,o=i.shape.length,u=r.shape[a],l=new Array(o-1),c=0;for(let m=0;m<o;m++)m!==a&&(l[c++]=i.shape[m]);let p=[],d=new Array(o).fill(0),h=i.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){d[a]=m;let g=ou({inputs:{x:i},backend:n,attrs:{begin:d,size:h}}),b=he({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,p.push(g)}return p.forEach(m=>n.disposeIntermediateTensorInfo(m)),f}var ste={kernelName:Uo,backendName:"webgl",kernelFunc:nte},rte=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,s=e.batchSize,r=e.inSize,a=e.numSegments,i=a*Math.ceil(r/n);this.outputShape=[s,i];let o="0.0",u="sumValue",l=Math.floor(n/4)*4,c=n%4,p=`
sumValue += dot(values, segFilter);
`,d="";r%n>0&&(d=`
if (inIdx < 0 || inIdx >= ${r}) {
return initializationValue;
}
`);let h="";r%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${r}) {
return -1.0;
}
`),this.userCode=`
const float initializationValue = ${o};
float getValue(int batch, int inIdx) {
${d}
return getX(batch, inIdx);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
float getSegmentIdAtIndex(int inIdx) {
${h}
return getSegmentIds(inIdx);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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}
}
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}
}
setOutput(${u});
}
`}};function ate(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,segmentIds:a}=t,{numSegments:i}=s,o=r.shape.length,u=[],l=0,c=S.getAxesPermutation([l],o),p=r;c!=null&&(p=qt({inputs:{x:r},backend:n,attrs:{perm:c}}),u.push(p),l=S.getInnerMostAxes(1,o)[0]);let d=S.segment_util.computeOutShape(p.shape,l,i),h=w.sizeFromShape([p.shape[l]]),f=he({inputs:{x:p},backend:n,attrs:{shape:[-1,h]}});u.push(f);let m=cp(r.dtype),g=(x,k,T,N,E)=>{let A=x.shape[0],P=x.shape[1],R=S.segment_util.segOpComputeOptimalWindowSize(P,E),F={windowSize:R,inSize:P,batchSize:A,numSegments:E},$=new rte(F,k),z=n.compileAndRun($,[x,T],N);if(u.push(z),z.shape[1]===E)return z;let W=d2({backend:n,attrs:{start:0,stop:E,step:1,dtype:"float32"}}),q=h2({inputs:{x:W},backend:n,attrs:{reps:[P/R]}});return u.push(W),u.push(q),g(z,k,q,N,E)},b=g(f,"unsortedSegmentSum",a,m,i),y=he({inputs:{x:b},backend:n,attrs:{shape:d}}),v=y;if(c!=null){u.push(y);let x=S.getUndoAxesPermutation(c);v=qt({inputs:{x:v},backend:n,attrs:{perm:x}})}return u.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var ite={kernelName:up,backendName:"webgl",kernelFunc:ate},ote=[d8,h8,g8,v8,w8,S8,N8,$8,R8,F8,z8,B8,U8,j8,Y8,Z8,eY,rY,iY,uY,pY,vY,wY,IY,_Y,EY,OY,qX,MY,UY,jY,JY,t9,s9,a9,o9,c9,h9,g9,y9,x9,k9,C9,T9,E9,D9,P9,L9,V9,H9,X9,J9,nQ,aQ,iQ,uQ,cQ,pQ,fQ,gQ,xQ,IQ,NQ,$Q,EQ,FQ,MQ,WQ,HX,GQ,VY,jQ,YQ,JQ,KX,sZ,oZ,lZ,hZ,gZ,xZ,IZ,TZ,EZ,FZ,PZ,BZ,WZ,GZ,KZ,YZ,ZZ,e7,n7,i7,c7,f7,k7,JX,N7,_7,R7,O7,CY,M7,B7,W7,H7,X7,YX,Q7,Z7,NY,y7,tJ,aJ,lJ,t8,hJ,gJ,xJ,IJ,TJ,_J,RJ,OJ,zJ,BJ,UJ,jJ,YJ,JJ,nee,bY,x7,aee,oee,lee,dee,hee,mee,bee,vee,wee,See,Nee,$ee,Eee,Dee,Oee,zee,v7,u8,Bee,Uee,qee,Yee,Jee,l8,tte,ste,ite,L7];for(let e of ote)Al(e);var zs=X();zs.registerFlag("WEBGPU_DEFERRED_SUBMIT_BATCH_SIZE",()=>15);zs.registerFlag("WEBGPU_CPU_FORWARD",()=>!0);zs.registerFlag("WEBGPU_MATMUL_WORK_PER_THREAD",()=>4);zs.registerFlag("WEBGPU_USE_NAIVE_CONV2D",()=>!1);zs.registerFlag("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE",()=>!1);zs.registerFlag("WEBGPU_CONV_SEPARATE_IM2COL_SHADER",()=>!1);zs.registerFlag("WEBGPU_USE_LOW_POWER_GPU",()=>!1);zs.registerFlag("WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD",()=>1e3);zs.registerFlag("WEBGPU_USE_PROFILE_TOOL",()=>!1);zs.registerFlag("WEBGPU_USE_IMPORT",()=>!1);var ute="return a + b;",lte="return areal * breal - aimag * bimag;",cte="return areal * bimag + aimag * breal;",dte="return a / b;",pte="return a * b;",hte="return (a - b) * (a - b);",fte="return a - b;",mte="return f32(a == b);",gte="return vec4<f32>(a == b);",bte="return f32(a > b);",yte="return vec4<f32>(a > b);",vte="return f32(a >= b);",xte="return vec4<f32>(a >= b);",wte="return f32(a < b);",kte="return vec4<f32>(a < b);",Ite="return f32(a <= b);",Ste="return vec4<f32>(a <= b);",Cte="return f32(f32(a) >= 1.0 && f32(b) >= 1.0);",Nte=`return (vec4<f32>(a >= vec4<f32>(1.0)) *
vec4<f32>(b >= vec4<f32>(1.0)));`,Tte=`
if (isnan(a)) { return a; }
if (isnan(b)) { return b; }
`,f2=`
if (isNaN.r) {
resultTemp.r = uniforms.NAN;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isNaN.g) {
resultTemp.g = uniforms.NAN;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isNaN.b) {
resultTemp.b = uniforms.NAN;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isNaN.a) {
resultTemp.a = uniforms.NAN;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`,$te=`
let s = sign(a) * sign(b);
let ia = i32(round(a));
let ib = i32(round(b));
return f32(idiv(ia, ib, s));
`,_te=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (cond[1]) {
resultTemp[1] = idiv(ia[1], ib[1], s[1]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (cond[2]) {
resultTemp[2] = idiv(ia[2], ib[2], s[2]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (cond[3]) {
resultTemp[3] = idiv(ia[3], ib[3], s[3]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return vec4<f32>(resultTemp);
`,Ate="return f32(a != b);",Ete="return vec4<f32>(a != b);",Rte=`
if(a < 0.0 && floor(b) < b) {
return uniforms.NAN;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (b == 0.0) {
return 1.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (round(abs(b) % 2.0) != 1.0) {
return pow(abs(a), b);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return sign(a) * pow(abs(a), b);
`,Dte=`
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isExpZero.g) {
resultTemp.g = 1.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isExpZero.b) {
resultTemp.b = 1.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isExpZero.a) {
resultTemp.a = 1.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
let isNaN = a < vec4<f32>(0.0) & floor(b) < b;
${f2}
return resultTemp;
`,Fte="if (a < 0.0) { return b * a; } return a;",Ote=`
let aLessThanZero = vec4<f32>(a < vec4<f32>(0.0));
return (aLessThanZero * (b * a)) + ((vec4<f32>(1.0) - aLessThanZero) * a);
`;function Aw(e,t){let n=t?f2:Tte;return t?`
var resultTemp = vec4<f32>(${e}(a, b));
let isNaN = isnanVec4(a) | isnanVec4(b);
`+n+`
return resultTemp;
`:n+`
return ${e}(a, b);
`}function tc(e,t){switch(e){case 0:return pte;case 1:return ute;case 2:return fte;case 3:return dte;case 4:return t?gte:mte;case 5:return t?yte:bte;case 6:return t?xte:vte;case 7:return t?kte:wte;case 8:return t?Ste:Ite;case 9:return t?Nte:Cte;case 10:return t?Ete:Ate;case 11:return hte;case 12:return t?_te:$te;case 14:return t?Ote:Fte;case 15:return Aw("max",t);case 16:return Aw("min",t);case 13:return t?Dte:Rte;case 17:return lte;case 18:return cte;default:throw new Error(`BinaryType ${e} is not implemented!`)}}var Pte="return abs(a);",zte="return ceil(a);",Mte="return cos(a);",Lte=`
let e2x = exp(-a);
return (e2x + 1.0 / e2x) / 2.0;
`,Bte="return exp(a) - 1.0;",Vte="if (a >= 0.0) { return a; } return (exp(a) - 1.0);",Wte=`
var resFloat = exp(a) - vec4<f32>(1.0);
if (a.r >= 0.0) {
resFloat.r = a.r;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (a.g >= 0.0) {
resFloat.g = a.g;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (a.b >= 0.0) {
resFloat.b = a.b;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (a.a >= 0.0) {
resFloat.a = a.a;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return resFloat;
`,Ute="return exp(a);",Gte="return floor(a);",Hte="return a;",qte=`if (a < 0.0) { return 1.0/0.0; }
return log(a);`,jte="return f32(!(a >= 1.0));",Kte="return -a;",Xte="if (a < 0.0) { return uniforms.alpha * a; } return a;",Yte="if(a < 0.0) { return 0.0; } return a;",Qte="return clamp(a, 0.0, 6.0);",Zte="return clamp(a, vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(6.0, 6.0, 6.0, 6.0));",Jte=`
var resFloat = a * vec4<f32>(a >= vec4<f32>(0.0));
let isNaN = isnanVec4(a);
if (isNaN.r) {
resFloat.r = a.r;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isNaN.g) {
resFloat.g = a.g;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isNaN.b) {
resFloat.b = a.b;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (isNaN.a) {
resFloat.a = a.a;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return resFloat;
`,ene="return 1.0/sqrt(a);",tne="return 1.0 / (1.0 + exp(-1.0 * a));",nne="return sin(a);",sne=`
let e2x = exp(a);
return (e2x - 1.0 / e2x) / 2.0;
`,rne="return sqrt(a);",ane="return a * a;",ine=`
let e2x = exp(-2.0 * abs(a));
return sign(a) * (1.0 - e2x) / (1.0 + e2x);
`,one="return f32(i32((a)));";function Hr(e,t){switch(e){case 0:return Pte;case 2:return Mte;case 3:return Lte;case 1:return zte;case 4:return t?Wte:Vte;case 5:return Ute;case 6:return Bte;case 7:return Gte;case 8:return Hte;case 9:return qte;case 10:return jte;case 11:return Kte;case 14:return Xte;case 12:return t?Jte:Yte;case 13:return t?Zte:Qte;case 15:return ene;case 18:return tne;case 16:return nne;case 17:return sne;case 19:return rne;case 20:return ane;case 21:return ine;case 22:return one;default:throw new Error(`BinaryType ${e} is not implemented!`)}}function Zs(e,t=!1){if(e===null)return null;if(e==="linear")return Hr(8);if(e==="relu")return Hr(12,t);if(e==="elu")return Hr(4,t);if(e==="relu6")return Hr(13,t);if(e==="prelu")return tc(14,t);if(e==="sigmoid")return Hr(18);if(e==="leakyrelu")return Hr(14);throw new Error(`Activation ${e} has not been implemented for the WebGPU backend.`)}function une(e,t){if(Math.max(...e)>3)throw new Error("Cannot symbolically compute strides for rank > 4 tensor.");let n=e.length,s=e.map(a=>`${t}[${a}]`),r=new Array(n-1);r[n-2]=s[n-1];for(let a=n-3;a>=0;--a)r[a]=`(${r[a+1]} * ${s[a+1]})`;return r}function Wt(e){if(e<=1)return"i32";if(e===2)return"vec2<i32>";if(e===3)return"vec3<i32>";if(e===4)return"vec4<i32>";throw Error(`GPU for rank ${e} is not yet supported`)}function cd(e,t){return e==="float32"?t?"vec4<f32>":"f32":e==="int32"||e==="bool"?t?"vec4<i32>":"i32":e}function Sv(){return`
@stage(compute) @workgroup_size(workGroupSizeX, workGroupSizeY, workGroupSizeZ)
`}function Dr(){return`
${Sv()}
fn main(@builtin(local_invocation_id) LocalId : vec3<u32>,
@builtin(global_invocation_id) GlobalId : vec3<u32>,
@builtin(num_workgroups) NumWorkgroups: vec3<u32>) {
localId = LocalId;
globalId = GlobalId;
numWorkgroups = NumWorkgroups;
`}function Ue(){return`
${Dr()}
let index = getGlobalIndex();
`}function lne(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 {
if (numWorkgroups.y == 1u && numWorkgroups.z == 1u) {
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<${cd(t.dtype,n.isVec4)}>;
@group(0) @binding(2) var<uniform> uniforms: Uniform;
`),[Ew,r.join(`
`),Rw(t.shape),n.getUserCode()].join(`
`);let a="struct Uniforms { NAN : f32, ";n.variableNames.forEach((p,d)=>{a+=`${p.charAt(0).toLowerCase()+p.slice(1)}Shape : ${Wt(e[d].shape.length)}, `}),a+=`outShape : ${Wt(t.shape.length)}, `;let i=t.shape.length-1;a+=`
outShapeStrides: ${Wt(i)}, `,n.size&&(a+="size : i32, "),n.uniforms&&(a+=n.uniforms),a+="};",r.push(a),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<${cd(t.dtype,n.isVec4)}>;
`),n.variableNames.forEach((p,d)=>{r.push(`
@group(0) @binding(${1+d}) var<storage, read> ${p}: array<${cd(e[d].dtype,n.isVec4)}>;
`)}),a!==""&&r.push(`
@group(0) @binding(${1+n.variableNames.length}) var<uniform> uniforms: Uniforms;
`);let[o,u]=mne(t.shape,n.dispatchLayout),l=[Ew,r.join(`
`),Rw(t.shape),o,cne(t.shape.length)];if(n.atomic||l.push(dne(t.shape,t.dtype,n.isVec4)),u===t.shape.length){let p=e.map(d=>pne(d,t.shape,n.isVec4,n.dispatchLayout.x.length===t.shape.length)).join(`
`);l.push(p)}return l.push(n.getUserCode()),l.join(`
`)}var Ew=`
// 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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn coordsInBounds3D(coord : vec3<i32>, shape : vec3<i32>) -> bool {
return all(coord >= vec3<i32>(0)) && all(coord < shape);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn coordsInBounds4D(coord : vec4<i32>, shape : vec4<i32>) -> bool {
return all(coord >= vec4<i32>(0)) && all(coord < shape);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn getIndexFromCoords1D(coord : i32, shape : i32) -> i32 {
return coord;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn getIndexFromCoords2D(coords : vec2<i32>, shape : vec2<i32>) -> i32 {
return dot(coords, vec2<i32>(shape.y, 1));
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return res;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;function cne(e){let t="";switch(e){case 0:case 1:t+=`
fn getOutputIndexFromCoords(coords : i32) -> i32 {
return coords;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;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));
}
`;break;default:w.assert(!1,()=>`Unsupported ${e}D shape`);break}return t}function dne(e,t,n){let s=e.length,r=cd(t,n),a;if(n?a=`fn setOutputAtIndex(flatIndex : i32, value : vec4<f32>) {
result[flatIndex] = ${r}(value);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn setOutputAtIndexI32(flatIndex : i32, value : i32) {
result[flatIndex] = ${r}(value);
}`,s>=2){let i=["d0","d1","d2","d3"].slice(0,s),o=Wt(s);n?a+=`
fn setOutputAtCoords(${i.map(u=>`${u} : i32`).join(", ")}, value : vec4<f32>) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndex(flatIndex / 4, value);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn setOutputAtCoordsI32(${i.map(u=>`${u} : i32`).join(", ")}, value : vec4<i32>) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndexI32(flatIndex / 4, value);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:a+=`
fn setOutputAtCoords(${i.map(u=>`${u} : i32`).join(", ")}, value : f32) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndex(flatIndex, value);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn setOutputAtCoordsI32(${i.map(u=>`${u} : i32`).join(", ")}, value : i32) {
let flatIndex = getOutputIndexFromCoords(${o}(${i.join(", ")}));
setOutputAtIndexI32(flatIndex, value);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}return a}function pne(e,t,n,s){let r=hne(e,n);return e.shape.length<=t.length&&(r+=fne(e,t,n,s)),r}function hne(e,t){let n=e.name,s=e.shape.length,r=Wt(s),a="get"+n.charAt(0).toUpperCase()+n.slice(1),i=["d0","d1","d2","d3"].slice(0,s),o=i.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]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;let u=`uniforms.${n.charAt(0).toLowerCase()+n.slice(1)}Shape`,l=`${s}D`;return s===0&&(l="1D"),t?`
fn ${a}(${o}) -> vec4<f32> {
return vec4<f32>(${n}[getIndexFromCoords${l}(${r}(${i.join(",")}),
${u}) / 4]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
fn ${a}(${o}) -> f32 {
return f32(${n}[getIndexFromCoords${l}(${r}(${i.join(",")}),
${u})]);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}function fne(e,t,n,s){let r=e.name,a=r.charAt(0).toUpperCase()+r.slice(1),i="get"+a+"ByOutput",o=e.shape.length,u=t.length,l=Wt(u);if(w.arraysEqual(e.shape,t)&&s)return n?`
fn ${i}Index(globalIndex : i32) -> vec4<f32> {
return vec4<f32>(${r}[globalIndex]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn ${i}Coords(coords : ${l}) -> vec4<f32> {
return vec4<f32>(${r}[${u>1?"getOutputIndexFromCoords(coords)":"coords"} / 4]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
fn ${i}Index(globalIndex : i32) -> f32 {
return f32(${r}[globalIndex]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn ${i}Coords(coords : ${l}) -> f32 {
return f32(${r}[${u>1?"getOutputIndexFromCoords(coords)":"coords"}]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`;let c=S.getBroadcastDims(e.shape,t),p=u-o,d="";if(o===0)return n?`
fn ${i}Index(globalIndex : i32) -> vec4<f32> {
return get${a}();
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn ${i}Coords(coords : ${l}) -> vec4<f32> {
return get${a}();
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`:`
fn ${i}Index(globalIndex : i32) -> f32{
return get${a}();
}
fn ${i}Coords(coords : ${l}) -> f32{
return get${a}();
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`;u<2&&c.length>=1?d="coords = 0;":d=c.map(g=>`coords[${g+p}] = 0;`).join(`
`);let h="";if(u<2&&o>0)h="coords";else if(u>1){let g=Wt(o),b=e.shape.map((y,v)=>`coords[${v+p}]`).join(", ");h=`${g}(${b})`}else h="coords";let f=`uniforms.${r.charAt(0).toLowerCase()+r.slice(1)}Shape`,m=`${o}D`;return n?`
fn ${i}Index(globalIndex : i32) -> vec4<f32> {
var coords = getCoordsFromIndex(globalIndex);
${d}
return ${r}[getIndexFromCoords${m}(${h}, ${f}) / 4];
}
fn ${i}Coords(coordsIn : ${l}) -> vec4<f32> {
var coords = coordsIn;
${d}
return ${r}[getIndexFromCoords${m}(${h}, ${f}) / 4];
}
`:`
fn ${i}Index(globalIndex : i32) -> f32 {
var coords = getCoordsFromIndex(globalIndex);
${d}
return f32(${r}[getIndexFromCoords${m}(${h}, ${f})]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn ${i}Coords(coordsIn : ${l}) -> f32 {
var coords = coordsIn;
${d}
return f32(${r}[getIndexFromCoords${m}(${h}, ${f})]);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}function mne(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);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`,a];let i="",o=[n,s,r],u=0;for(let d=0;d<o.length;d++){let h=o[d];if(h.length!==0)if(u+=h.length,h.length===1)i+=`let d${h[0]} = i32(globalId[${d}]);`;else{let f=une(h,"uniforms.outShape");i+=`var index${d} = i32(globalId[${d}]);`;for(let m=0;m<f.length;m++)i+=`let d${h[m]} = index${d} / ${f[m]};`,m===f.length-1?i+=`let d${h[m+1]} = index${d} - d${h[m]} * ${f[m]};`:i+=`index${d} = index${d} - d${h[m]} * ${f[m]};`}}let l=[];for(let d=0;d<u;d++)l.push(`d${d}`);let c=Wt(u),p=`fn getOutputCoords() -> ${c} {
${i}
`;return l.length===0?p+=`return ${c}(0); }`:p+=`return ${c}(${l.join(",")}); }`,[p,u]}function Rw(e){let t=e.length;if(t<=1)return"fn getCoordsFromIndex(index : i32) -> i32 { return index; }";let n=w.computeStrides(e),s=Wt(t),r=[];for(let i=0;i<t;i++)r.push(`d${i}`);if(n.length===1)return` fn getCoordsFromIndex(index : i32) -> vec2<i32> {
let d0 = index / uniforms.outShapeStrides; let d1 = index - d0 * uniforms.outShapeStrides;
return vec2<i32>(d0, d1);
}`;let a="var index2 = index;"+n.map((i,o)=>{let u=`let ${r[o]} = index2 / uniforms.outShapeStrides[${o}]`,l=o===n.length-1?`let ${r[o+1]} = index2 - ${r[o]} * uniforms.outShapeStrides[${o}]`:`index2 = index2 - ${r[o]} * uniforms.outShapeStrides[${o}]`;return`${u}; ${l};`}).join("");return`
fn getCoordsFromIndex(index : i32) -> ${s} {
${a}
return ${s}(${r.join(",")});
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}var m2={};Ae(m2,{ArrayBufferToTypedArray:()=>b2,GPUBytesPerElement:()=>jm,computeDispatch:()=>_e,computeWorkGroupSizeForConv2d:()=>Cv,computeWorkGroupSizeForMatMul:()=>g2,computeWorkPerThreadForConv2d:()=>Nv,flatDispatchLayout:()=>Be,isWebGPUSupported:()=>Tv,tilesFitEvenlyIntoShape:()=>Ks});var na=e=>{let t=1;for(let n=0;n<e.length;n++)t*=e[n];return t};function Ks(e,t){if(e.length!==t.length)throw new Error(`Cannot compute whether rank ${e.length} tiles fit evenly into rank ${t.length} shape - ranks must match.`);return t.every((n,s)=>n%e[s]===0)}function _e(e,t,n=[1,1,1],s=[1,1,1]){let[r,a,i]=[Math.ceil(na(e.x.map(o=>t[o]))/(n[0]*s[0])),e.y?Math.ceil(na(e.y.map(o=>t[o]))/(n[1]*s[1])):1,e.z?Math.ceil(na(e.z.map(o=>t[o]))/(n[2]*s[2])):1];return[r,a,i]}function Cv(e,t){let n=na(e.x.map(r=>t[r])),s=na(e.y.map(r=>t[r]));return n<=4?[4,16,1]:s<=4?[16,4,1]:[16,16,1]}function g2(e,t,n){return e===1?[32,1,1]:n===1?[1,32,1]:[8,8,1]}function Nv(e,t){let n=na(e.x.map(r=>t[r])),s=na(e.y.map(r=>t[r]));return n<=4?[1,2,1]:s<=4?[2,1,1]:[2,2,1]}function Be(e){return{x:e.map((t,n)=>n)}}function jm(e){if(e==="float32"||e==="int32"||e==="bool"||e==="string")return 4;if(e==="complex64")return 8;throw new Error(`Unknown dtype ${e}`)}function b2(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 Tv(){return(typeof window!="undefined"||typeof WorkerGlobalScope!="undefined")&&!!navigator.gpu}function y2(e,t,n,s){return w.assert(s%4===0&&e[0]===4,()=>"tileInner must be divisible by 4. And ColPerThread must be 4"),`
var<workgroup> mm_Asub : array<array<vec4<f32>, ${s/e[0]}>, ${t}>;
var<workgroup> mm_Bsub : array<array<vec4<f32>, ${n/e[0]}>, ${s}>;
let RowPerThread = ${e[1]};
let ColPerThread = ${e[0]};
let TileInner = ${s};
${Dr()}
let tileRow = ${t===1?"0":"i32(localId.y) * RowPerThread"};
let tileCol = i32(localId.x);
let globalRow = ${t===1?"0":"i32(globalId.y) * RowPerThread"};
let globalCol = i32(globalId.x);
let numTiles = (uniforms.dimInner - 1) / TileInner + 1;
var acc: array<vec4<f32>, RowPerThread>;
var ACached : vec4<f32>;
var BCached : array<vec4<f32>, 4>;
// Loop over shared dimension.
var globalColA = tileCol;
let RowPerThreadB = TileInner / i32(workGroupSizeY);
let tileRowB = i32(localId.y) * RowPerThreadB;
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
let inputRow = tileRow + innerRow;
let inputCol = tileCol;
mm_Asub[inputRow][inputCol] = mm_readA(globalRow + innerRow, globalColA, globalId);
}
globalColA = globalColA + TileInner / ColPerThread;
// 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 / ColPerThread; k = k + 1) {
BCached[0] = mm_Bsub[k * ColPerThread][tileCol];
BCached[1] = mm_Bsub[k * ColPerThread + 1][tileCol];
BCached[2] = mm_Bsub[k * ColPerThread + 2][tileCol];
BCached[3] = mm_Bsub[k * ColPerThread + 3][tileCol];
for (var i = 0; i < RowPerThread; i = i + 1) {
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];
acc[i] = BCached[3] * ACached.w + acc[i];
}
}
workgroupBarrier();
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
mm_write(globalRow + innerRow,
globalCol,
acc[innerRow], globalId);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}`}var gne=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,8,1],this.isVec4=!0,this.outputShape=t,this.dispatchLayout={x:[2],y:[1],z:[0]},t[1]===1?this.elementsPerThread=[4,1,1]:this.elementsPerThread=[4,4,1],this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread);let i=s!=null,o=a!=null;i&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),this.tileAOuter=t[1]===1?1:this.workGroupSize[1]*this.elementsPerThread[1],this.tileBOuter=this.workGroupSize[0]*this.elementsPerThread[0],this.tileInner=this.tileBOuter,this.aShape=e,this.addBias=i,this.activation=r,this.hasPreluActivationWeights=o,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`matMulPackedVec4_${this.activation}_${this.fitA}_${this.fitB}_${this.elementsPerThread}`}getShapeFit(){let e=this.aShape[2],t=this.outputShape[2],n=[this.outputShape[0],e,t],s=[this.tileAOuter,this.tileInner],r=[this.tileInner,this.tileBOuter];return[Ks(s,this.aShape.slice(1)),Ks(r,n.slice(1))]}getUserCode(){let e=this.fitA?"return A[batch * batchASize + row * uniforms.dimInner / 4 + col]":`if (coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A[batch * batchASize + row * uniforms.dimInner / 4 + col];
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
return vec4<f32>(0.0)`,t=this.fitB?"return B[batch * batchBSize + row * uniforms.dimBOuter / 4 + col]":`if(coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B[batch * batchBSize + row * uniforms.dimBOuter / 4 + col];
}
return vec4<f32>(0.0)`,n="",s="";if(this.activation){let i=Zs(this.activation,this.isVec4);this.hasPreluActivationWeights?n=`fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`
fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
${i}
}`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2] / 4;
let batch = i32(globalId.z);
${e};
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2] / 4;
let batch = i32(globalId.z);
${t};
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn mm_write(row : i32, col : i32, valueIn : vec4<f32>, globalId : vec3<u32>) {
if (row < uniforms.aShape[1] && col * 4 < uniforms.bShape[2])
{
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col * 4);
${r}
${s}
setOutputAtCoords(outCoord[0], outCoord[1], outCoord[2], value);
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
}
${y2(this.elementsPerThread,this.tileAOuter,this.tileBOuter,this.tileInner)}
`}};function $v(e,t){let n=t[1]*e[1],s=t[0]*e[0],r=n>s?n:s;return`
var<workgroup> mm_Asub : array<array<f32, ${r}>, ${n}>;
var<workgroup> mm_Bsub : array<array<f32, ${s}>, ${r}>;
${Dr()}
let tileRow = i32(localId.y) * ${e[1]};
let tileCol = i32(localId.x) * ${e[0]};
let globalRow = i32(globalId.y) * ${e[1]};
let globalCol = i32(globalId.x) * ${e[0]};
let numTiles = (uniforms.dimInner - 1) / ${r} + 1;
var acc : array<array<f32, ${e[0]}>, ${e[1]}>;
var ACached : f32;
var BCached : array<f32, ${e[0]}>;
// Without this initialization strange values show up in acc.
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
acc[innerRow][innerCol] = 0.0;
2022-02-10 18:27:21 +01:00
}
}
2022-04-01 15:13:32 +02:00
let ColPerThreadA = ${r} / ${t[0]};
let tileColA = i32(localId.x) * ColPerThreadA;
let RowPerThreadB = ${r} / ${t[1]};
let tileRowB = i32(localId.y) * RowPerThreadB;
// Loop over shared dimension.
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ColPerThreadA; innerCol = innerCol + 1) {
let inputRow = tileRow + innerRow;
let inputCol = tileColA + innerCol;
mm_Asub[inputRow][inputCol] = mm_readA(
globalRow + innerRow,
t * ${r} + inputCol, globalId);
}
}
// Load one tile of B into local memory.
for (var innerRow = 0; innerRow < RowPerThreadB; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
let inputRow = tileRowB + innerRow;
let inputCol = tileCol + innerCol;
mm_Bsub[inputRow][inputCol] = mm_readB(
t * ${r} + inputRow,
globalCol + innerCol, globalId);
}
}
workgroupBarrier();
// Compute acc values for a single thread.
for (var k = 0; k < ${r}; k = k + 1) {
for (var inner = 0; inner < ${e[0]}; inner = inner + 1) {
BCached[inner] = mm_Bsub[k][tileCol + inner];
}
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
ACached = mm_Asub[tileRow + innerRow][k];
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
}
}
}
workgroupBarrier();
}
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
if ((globalCol + innerCol) < uniforms.dimBOuter &&
(globalRow + innerRow) < uniforms.dimAOuter) {
mm_write(globalRow + innerRow,
globalCol + innerCol,
acc[innerRow][innerCol], globalId);
}
}
2022-04-01 15:12:04 +02:00
}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}function bne(e){return`
let TileSize = ${e[0]*4};
var<workgroup> mm_Asub : array<vec4<f32>, ${e[0]}>;
${Dr()}
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;
mm_Asub[tileCol] = vec4<f32>(mm_readA(globalRow, colA, globalId),
mm_readA(globalRow, colA + 1, globalId),
mm_readA(globalRow, colA + 2, globalId),
mm_readA(globalRow, colA + 3, globalId));
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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
if (globalRow < uniforms.dimAOuter && globalCol < uniforms.dimBOuter) {
mm_write(globalRow, globalCol, acc, globalId);
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}var v2=class{constructor(e,t,n,s=!1,r=!1,a=null,i=null,o=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.workGroupSize=[16,16,1],this.outputShape=t,this.dispatchLayout={x:[2],y:[1],z:[0]};let u=s?e[1]:e[2];this.workGroupSize=g2(t[1],u,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 l=a!=null,c=o!=null;l&&this.variableNames.push("bias"),c&&this.variableNames.push("preluActivationWeights"),this.workPerThread=n,this.aShape=e,this.transposeA=s,this.transposeB=r,this.addBias=l,this.activation=i,this.hasPreluActivationWeights=c;let p=this.outputShape[2],d=this.transposeB?[this.outputShape[0],p,u]:[this.outputShape[0],u,p];[this.fitA,this.fitB]=this.getShapeFit(d),this.shaderKey=`matMulPacked_${this.workPerThread}_${s}_${r}_${this.activation}_${this.fitA}_${this.fitB}_${this.outputShape[1]>1}`}getShapeFit(e){let t=this.workGroupSize[1]*this.workPerThread,n=this.workGroupSize[0]*this.workPerThread,s=t>n?t:n;this.outputShape[1]===1&&(s*=4),w.assert(s%this.workGroupSize[0]===0&&s%this.workGroupSize[1]===0,()=>"tileInner must be multiple of workgroupsize.x and workgroupsize.y");let r=[t,s],a=[s,n];return[Ks(r,this.aShape.slice(1)),Ks(a,e.slice(1))]}getUserCode(){let e;this.transposeA===!1?e=this.fitA?"return A[batch * batchASize + row * uniforms.dimInner + col];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A[batch * batchASize + row * uniforms.dimInner + col];
}
return 0.0;`:e=this.fitA?"return A[batch * batchASize + col * uniforms.dimAOuter + row];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A[batch* batchASize + col * uniforms.dimAOuter + row];
}
return 0.0;`;let t;this.transposeB===!1?t=this.fitB?"return B[batch * batchBSize + row * uniforms.dimBOuter + col];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B[batch * batchBSize + row * uniforms.dimBOuter + col];
}
return 0.0;`:t=this.fitB?"return B[batch * batchBSize + col * uniforms.dimInner + row];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B[batch * batchBSize + col * uniforms.dimInner + row];
}
return 0.0;`;let n="",s="";if(this.activation){let i=Zs(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${i}
}
`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
let batch = i32(globalId.z);
${e}
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batch = i32(globalId.z);
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
${t}
}
fn mm_write(row : i32, col : i32, valueIn : f32, globalId : vec3<u32>) {
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col);
${r}
${s}
setOutputAtCoords(batch, row, col, value);
}
${this.outputShape[1]>1?$v([this.workPerThread,this.workPerThread,1],this.workGroupSize):bne(this.workGroupSize)}
`}};function yne(){return`
var<workgroup> sumValues : array<f32, workGroupSizeX>;
${Dr()}
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;
}
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];
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
workgroupBarrier();
}
if (localId.x == 0u) {
sum = sumValues[0] + sumValues[1];
mm_write(batch, row, col, sum);
2022-02-10 18:27:21 +01:00
}
}
2022-04-01 15:13:32 +02:00
`}var vne=class{constructor(e,t=!1,n=!1,s=null,r=null,a=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 i=s!=null,o=a!=null;i&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),this.transposeA=t,this.transposeB=n,this.addBias=i,this.activation=r,this.hasPreluActivationWeights=o,this.shaderKey=`matMulReduce_${this.activation}_${t}_${n}`}getUserCode(){let e;this.transposeA===!1?e="return A[batch * batchASize + row * uniforms.dimInner + col];":e="return A[batch * batchASize + col * uniforms.dimAOuter + row];";let t;this.transposeB===!1?t="return B[batch * batchBSize + row * uniforms.dimBOuter + col];":t="return B[batch * batchBSize + col * uniforms.dimInner + row];";let n="",s="";if(this.activation){let i=Zs(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${i}
}
`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(batch: i32, row : i32, col : i32) -> f32 {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
${e}
}
fn mm_readB(batch: i32, row : i32, col : i32) -> f32 {
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
${t}
}
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);
}
${yne()}
`}};function xne(e){let t=e[1]/2,n=e[0],s=t>n?t:n;return`
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.
${Dr()}
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};
}
} 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-02-10 18:27:21 +01:00
} else {
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
}
2022-04-01 15:13:32 +02:00
workgroupBarrier();
if (t != 0) {
t = t + 1;
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
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};
} else {
// 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];
}
}
}
workgroupBarrier();
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
let writeCol = (globalRow - tileRow) / 2 + tileRow - ${t};
if (tileRow >= ${t} && writeCol >= 0) {
mm_write(writeCol, globalCol, acc, globalId);
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}var wne=class{constructor(e,t,n,s=null,r=null,a=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.workGroupSize=[8,16,1],w.assert(e[1]<=16||t[2]<=16,()=>"This program can be only used when A width or B Height are small"),this.outputShape=n,this.dispatchLayout={x:[2],y:[1],z:[0]},this.dispatch=[Math.ceil(n[2]/this.workGroupSize[0]),Math.ceil(n[1]*2/this.workGroupSize[1]),n[0]];let i=s!=null;i&&this.variableNames.push("bias");let o=a!=null;o&&this.variableNames.push("preluActivationWeights"),this.addBias=i,this.activation=r,this.hasPreluActivationWeights=o,this.shaderKey=`matMulSmallOutputSize_${this.activation}`}getUserCode(){let e=`if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
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];
}
return 0.0;`,n="",s="";if(this.activation){let i=Zs(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${i}
}`:n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${i}
}`,s="value = activation(value, outCoord);"}let r=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
let batch = i32(globalId.z);
${e}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batch = i32(globalId.z);
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
${t}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
${xne(this.workGroupSize)}
`}};function Me(e){let{inputs:t,attrs:n}=e,{x:s}=t,{shape:r}=n,a=w.sizeFromShape(s.shape),i=w.inferFromImplicitShape(r,a),o=w.sizeFromShape(i);return w.assert(a===o,()=>`The new shape (${i}) has ${o} elements and the old shape (${s.shape}) has ${a} elements. The new shape and old shape must have the same number of elements.`),e.backend.incRef(s.dataId),{dataId:s.dataId,shape:i,dtype:s.dtype}}var kne={kernelName:Ao,backendName:"webgpu",kernelFunc:Me};function _v({a:e,b:t,transposeA:n,transposeB:s,backend:r,bias:a=null,preluActivationWeights:i=null,leakyreluAlpha:o=0,activation:u=null}){let l=e.shape.length,c=t.shape.length,p=n?e.shape[l-2]:e.shape[l-1],d=s?t.shape[c-1]:t.shape[c-2],h=n?e.shape[l-1]:e.shape[l-2],f=s?t.shape[c-2]:t.shape[c-1],m=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(m),y=w.sizeFromShape(g),x=qo.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],T=s?[y,f,d]:[y,d,f],N=Me({inputs:{x:e},backend:r,attrs:{shape:k}}),E=Me({inputs:{x:t},backend:r,attrs:{shape:T}}),A=[N,E],P=Math.max(b,y),R=p%4===0&&f%4===0&&!n&&!s&&f>=32,F;h*f<=32?F=new vne([P,h,f],n,s,a,u,i):!n&&!s&&(h<=16&&(f<=512||d>=2*f)||f<=16&&(h<=512||p>=2*h))?F=new wne(k,T,[P,h,f],a,u,i):R?F=new gne(k,[P,h,f],X().get("WEBGPU_MATMUL_WORK_PER_THREAD"),a,u,i):F=new v2(k,[P,h,f],X().get("WEBGPU_MATMUL_WORK_PER_THREAD"),n,s,a,u,i);let $=[N,E];a&&$.push(a),i&&$.push(i);let z=[{type:"int32",data:[h]},{type:"int32",data:[f]},{type:"int32",data:[p]}];u==="leakyrelu"&&(z.push({type:"float32",data:[o]}),F.uniforms+=" alpha : f32,");let W=r.runWebGPUProgram(F,$,e.dtype,z),q=Me({inputs:{x:W},backend:r,attrs:{shape:x}});A.push(W);for(let K of A)r.disposeData(K.dataId);return q}function Ine(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a,bias:i,preluActivationWeights:o}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=s;return _v({a:r,b:a,transposeA:u,transposeB:l,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:p,activation:c})}var Sne={kernelName:ra,backendName:"webgpu",kernelFunc:Ine},Dw=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.workGroupSize=[128,1,1],this.size=!0,this.outputShape=S.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`binaryOpComplex_${e}`,this.op=e}getUserCode(){return`
fn binaryOpComplex(
areal : f32, aimag : f32, breal : f32, bimag : f32) -> f32 {
${tc(this.op,!1)}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +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-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
`}},Cne=class{constructor(e,t,n,s){this.variableNames=["A","B"],this.size=!0;let r=256;this.workGroupSize=[r,1,1],this.outputShape=S.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Be(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);
let b = sharedBuf[${e}];`:`let a = sharedBuf[${e}];
let b = getBByOutputCoords(coords);`;return`
fn binaryOperation(a : f32, b : f32) -> f32 {
${tc(this.op,!1)}
}
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));
}
}
}
`}},Nne=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=S.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.op=e,this.shaderKey=`binaryVec4_${e}`}getUserCode(){return`
fn binaryOperation(a : vec4<f32>, b : vec4<f32>) -> vec4<f32> {
${tc(this.op,this.isVec4)}
2022-02-10 18:27:21 +01:00
}
2022-04-01 15:13:32 +02:00
${Ue()}
if (index < uniforms.size) {
let a = getAByOutputIndex(index);
let b = getBByOutputIndex(index);
setOutputAtIndex(index, binaryOperation(a, b));
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},x2=class{constructor(e,t,n){this.variableNames=["A","B"],this.size=!0;let s=128;this.workGroupSize=[s,1,1],this.outputShape=S.assertAndGetBroadcastShape(t,n),this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`binary_${e}`,this.op=e}getUserCode(){return`
fn binaryOperation(a : f32, b : f32) -> f32 {
${tc(this.op,!1)}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${Ue()}
if (index < uniforms.size) {
let a = getAByOutputIndex(index);
let b = getBByOutputIndex(index);
setOutputAtIndex(index, binaryOperation(a, b));
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Fw(e,t,n){if(w.arraysEqual(t,n)&&w.sizeFromShape(t)%4===0)return new Nne(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 Cne(e,t,n,a):new x2(e,t,n)}function es(e){let{inputs:t}=e,{x:n}=t;return e.backend.incRef(n.dataId),{dataId:n.dataId,shape:n.shape,dtype:n.dtype}}var Tne={kernelName:Ba,backendName:"webgpu",kernelFunc:es};function uu(e){let{inputs:t,backend:n}=e,{real:s,imag:r}=t,a=n.makeTensorInfo(s.shape,"complex64"),i=n.tensorMap.get(a.dataId),o=es({inputs:{x:s},backend:n}),u=es({inputs:{x:r},backend:n});return i.complexTensorInfos={real:o,imag:u},a}var $ne={kernelName:jd,backendName:"webgpu",kernelFunc:uu},nc=class{constructor(e,t){this.variableNames=["A"],this.size=!0;let n=128;this.workGroupSize=[n,1,1],this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.op=t,this.shaderKey=`unary_${t}`}getUserCode(){return`
fn unaryOperation(a : f32) -> f32 {
${Hr(this.op,!1)}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${Ue()}
if (index < uniforms.size) {
let a = getAByOutputIndex(index);
setOutputAtIndex(index, unaryOperation(a));
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Kt({opType:e,cpuKernelImpl:t,dtype:n}){return({inputs:s,backend:r})=>{let{x:a}=s,i=r,o=n||a.dtype;if(i.shouldExecuteOnCPU([a])&&t!=null){let l=i.tensorMap.get(a.dataId),c=t(l.values,o);return i.makeTensorInfo(a.shape,o,c)}let u=new nc(a.shape,e);return i.runWebGPUProgram(u,[a],o)}}function mn({opSnippet:e,cpuKernelImpl:t,supportsComplex:n=!1,dtype:s}){return({inputs:r,backend:a})=>{let{a:i,b:o}=r,u=a;if(n&&i.dtype==="complex64"){let p=u.tensorMap.get(i.dataId),d=u.tensorMap.get(o.dataId),h,f;if(e!==0)[h,f]=[[p.complexTensorInfos.real,d.complexTensorInfos.real],[p.complexTensorInfos.imag,d.complexTensorInfos.imag]].map(g=>{let[b,y]=g,v={dataId:b.dataId,dtype:b.dtype,shape:i.shape},x={dataId:y.dataId,dtype:y.dtype,shape:o.shape},k=Fw(e,i.shape,o.shape);return u.runWebGPUProgram(k,[v,x],cn(b.dtype,y.dtype))});else{let g=new Dw(17,i.shape,o.shape),b=new Dw(18,i.shape,o.shape),y=[{dataId:p.complexTensorInfos.real.dataId,dtype:p.complexTensorInfos.real.dtype,shape:i.shape},{dataId:p.complexTensorInfos.imag.dataId,dtype:p.complexTensorInfos.imag.dtype,shape:i.shape},{dataId:d.complexTensorInfos.real.dataId,dtype:d.complexTensorInfos.real.dtype,shape:o.shape},{dataId:d.complexTensorInfos.imag.dataId,dtype:d.complexTensorInfos.imag.dtype,shape:o.shape}];h=u.runWebGPUProgram(g,y,"float32"),f=u.runWebGPUProgram(b,y,"float32")}let m=uu({inputs:{real:h,imag:f},backend:u});return u.disposeData(h.dataId),u.disposeData(f.dataId),m}let l=s||cn(i.dtype,o.dtype);if((i.dtype==="string"||o.dtype==="string"||u.shouldExecuteOnCPU([i,o]))&&t!=null){let p=u.tensorMap.get(i.dataId).values,d=u.tensorMap.get(o.dataId).values,h=i.dtype==="string"?S.fromUint8ToStringArray(p):p,f=i.dtype==="string"?S.fromUint8ToStringArray(d):d,[m,g]=t(i.shape,o.shape,h,f,l);return u.makeTensorInfo(g,l,m)}let c=Fw(e,i.shape,o.shape);return u.runWebGPUProgram(c,[i,o],l)}}var{addImpl:_ne,ceilImpl:Ane,concatImpl:Ene,equalImpl:Rne,expImpl:Dne,expm1Impl:Fne,floorImpl:One,gatherNdImpl:Pne,gatherV2Impl:zne,greaterEqualImpl:Mne,greaterImpl:Lne,lessEqualImpl:Bne,lessImpl:Vne,logImpl:Wne,maxImpl:Une,maximumImpl:Gne,minimumImpl:Hne,multiplyImpl:qne,negImpl:jne,notEqualImpl:Kne,prodImpl:Xne,rangeImpl:Yne,rsqrtImpl:Qne,simpleAbsImpl:Zne,sliceImpl:Jne,stridedSliceImpl:ese,stringNGramsImpl:tse,subImpl:nse,tileImpl:sse,topKImpl:rse,transposeImpl:ase,uniqueImpl:zpe}=Zy,ise=Kt({opType:0,cpuKernelImpl:Zne}),ose={kernelName:ao,backendName:"webgpu",kernelFunc:ise},use=mn({opSnippet:1,cpuKernelImpl:_ne,supportsComplex:!0}),lse={kernelName:Ir,backendName:"webgpu",kernelFunc:use},cse=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=Be(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`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function dse(e){let{inputs:t,backend:n}=e,s=t;if(s.length===1)return es({inputs:{x:s[0]},backend:n});let r=s.map(o=>o.dtype).reduce((o,u)=>cn(o,u)),a=s.map(o=>o.shape),i=new cse(a);return n.runWebGPUProgram(i,s,r)}var pse={kernelName:ka,backendName:"webgpu",kernelFunc:dse},w2=class{constructor(e,t,n){this.workGroupSize=[64,1,1],this.variableNames=["x"],this.uniforms="axis : i32, infinityValue : f32,",this.size=!0;let s=[t];S.assertAxesAreInnerMostDims("arg"+n.charAt(0).toUpperCase()+n.slice(1),s,e.length),this.op=n==="min"?"<":">";let[r]=S.computeOutAndReduceShapes(e,s);this.outputShape=r.length===0?[1]:r,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,[1,1,1]),this.inputShape=e,this.shaderKey=`argMinMax${this.op}`}getUserCode(){let e=`
var<workgroup> xBestIndices : array<i32, ${this.workGroupSize[0]}>;
var<workgroup> xBestValues : array<f32, ${this.workGroupSize[0]}>;
`,t=(r,a)=>this.outputShape.length===1?r:`${r}[${a}]`,n=r=>this.inputShape.length===1?"uniforms.xShape":`uniforms.xShape[${r}]`;return`
fn DIV_CEIL(a : u32, b : u32) -> u32 {
return ((a - 1u) / b + 1u);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${e}
// In order to get a flattened index into the input tensor, we need to
// add back the index along the reduced dimension to |outputCoords|.
// This function outputs the offset to the first value along
// |axis| and the stride to get the next value of the input along |axis|.
fn getInputCoordInfo(outputIndex : i32) -> vec2<i32>{
let outputCoords = getCoordsFromIndex(outputIndex);
var i = ${this.outputShape.length-1};
var stride = 1;
var inputStride = 1;
var offset = 0;
for (var r = 1; r <= ${this.inputShape.length}; r = r + 1) {
let length = ${n(`${this.inputShape.length} - r`)};
if (${this.inputShape.length} - r == uniforms.axis) {
inputStride = stride;
} else {
offset = offset + ${t("outputCoords","i")} * stride;
i = i - 1;
}
stride = stride * length;
}
return vec2<i32>(offset, inputStride);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn getInputIndex(coordInfo : vec2<i32>, index : i32) -> i32{
return coordInfo[0] + coordInfo[1] * index;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${Ue()}
let outputIndex = index / i32(workGroupSizeX);
let coordInfo = getInputCoordInfo(outputIndex);
let Length = ${n("uniforms.axis")};
var bestIndex = i32(localId.x);
var bestValue = uniforms.infinityValue;
for (var k = i32(localId.x); k < Length && outputIndex < uniforms.size;
k = k + i32(workGroupSizeX)) {
let candidate = f32(x[getInputIndex(coordInfo, k)]);
if (!isnan(candidate) && candidate ${this.op} bestValue) {
bestValue = candidate;
bestIndex = k;
}
}
xBestValues[localId.x] = bestValue;
xBestIndices[localId.x] = bestIndex;
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];
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},hse=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`
let TILE_DIM = ${this.workGroupSize[0]};
var<workgroup> tile : array<array<f32, ${this.workGroupSize[0]+1}>, ${this.workGroupSize[0]}>;
${Sv()}
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},fse=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=Be(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=mse(this.newDim);return`
${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-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}};function mse(e){let t=e.length;if(t>4)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[${s}]`;return n.join()}function wi(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{perm:a}=s,i=n,o=r.shape.length,u=new Array(o);for(let c=0;c<u.length;c++)u[c]=r.shape[a[c]];if(n.shouldExecuteOnCPU([r])){let p=i.tensorMap.get(r.dataId).values,d=ase(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 hse(r.shape,a);return i.runWebGPUProgram(c,[r],r.dtype)}let l=new fse(r.shape,a);return i.runWebGPUProgram(l,[r],r.dtype)}var gse={kernelName:pi,backendName:"webgpu",kernelFunc:wi};function bse(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=S.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=wi({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=S.getInnerMostAxes(i.length,u.shape.length)),S.assertAxesAreInnerMostDims("argMax",[i[0]],u.shape.length);let c=new w2(u.shape,i[0],"max"),p=[{type:"int32",data:[i[0]]},{type:"float32",data:[Number.NEGATIVE_INFINITY]}],d=n.runWebGPUProgram(c,[u],"int32",p);return l.forEach(h=>n.disposeData(h.dataId)),d}var yse={kernelName:Ia,backendName:"webgpu",kernelFunc:bse};function vse(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a}=s,i=w.parseAxisParam(a,r.shape),o=S.getAxesPermutation(i,r.shape.length),u=r,l=[];o!=null&&(u=wi({inputs:{x:r},backend:n,attrs:{perm:o}}),l.push(u),i=S.getInnerMostAxes(i.length,u.shape.length)),S.assertAxesAreInnerMostDims("argMin",[i[0]],u.shape.length);let c=new w2(u.shape,i[0],"min"),p=[{type:"int32",data:[i[0]]},{type:"float32",data:[Number.POSITIVE_INFINITY]}],d=n.runWebGPUProgram(c,[u],"int32",p);return l.forEach(h=>n.disposeData(h.dataId)),d}var xse={kernelName:il,backendName:"webgpu",kernelFunc:vse},k2=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=Be(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"),`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
`}},I2=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="poolWithFilterSizeEqualsOne"}getUserCode(){return`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
`}};function wse(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1,c=S.computePool2DInfo(r.shape,a,i,l,o,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return es({inputs:{x:r},backend:n});let p,d=[{type:"int32",data:[c.strideHeight,c.strideWidth]}];return c.filterHeight===1&&c.filterWidth===1?p=new I2(c):(p=new k2(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 kse={kernelName:Sa,backendName:"webgpu",kernelFunc:wse};function Ise(e){let{inputs:t,backend:n,attrs:s}=e,{a:r,b:a}=t,{transposeA:i,transposeB:o}=s;return _v({a:r,b:a,transposeA:i,transposeB:o,backend:n})}var Sse={kernelName:Ca,backendName:"webgpu",kernelFunc:Ise},Cse=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=Be(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=Nse(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.${Km[a]} = uniforms.start[${a}] + coords.${Km[a]};`),`
${Ue()}
if (index < uniforms.size) {
var sourceLoc : ${e};
let coords = getCoordsFromIndex(index);
${n.join(`
`)}
setOutputAtIndex(index, getSource(${t}));
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}},Km=["x","y","z","w","u","v"];function Nse(e){if(e===1)return"sourceLoc";if(e<=6)return Km.slice(0,e).map(t=>`sourceLoc.${t}`).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}function lu(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,size:i}=s,[o,u]=wt.parseSliceParams(r,a,i);if(wt.assertParamsValid(r,o,u),n.shouldExecuteOnCPU([r])||r.dtype==="string"){let p=n.tensorMap.get(r.dataId),d=Jne(p.values,o,u,r.shape,r.dtype);return n.makeTensorInfo(u,r.dtype,d)}if(w.sizeFromShape(u)===0)return n.makeTensorInfo(u,r.dtype,[]);let l=new Cse(o,u),c=[{type:"int32",data:o}];return n.runWebGPUProgram(l,[r],r.dtype,c)}var Tse={kernelName:Oo,backendName:"webgpu",kernelFunc:lu},$se=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,crops:i}=s;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGPU backend not implemented yet");let o=a.reduce((y,v)=>y*v),u=S.getReshaped(r.shape,a,o),l=S.getPermuted(u.length,a.length),c=S.getReshapedPermuted(r.shape,a,o),p=S.getSliceBeginCoords(i,a.length),d=S.getSliceSize(c,i,a.length),h=[],f=Me({inputs:{x:r},backend:n,attrs:{shape:u}}),m=wi({inputs:{x:f},backend:n,attrs:{perm:l}}),g=Me({inputs:{x:m},backend:n,attrs:{shape:c}}),b=lu({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},_se={kernelName:io,backendName:"webgpu",kernelFunc:$se},S2=mn({opSnippet:10,dtype:"bool",cpuKernelImpl:Kne}),Ase={kernelName:Io,backendName:"webgpu",kernelFunc:S2};function sc(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.tensorMap.get(s.dataId);return es({inputs:{x:r.complexTensorInfos.real},backend:n})}var Ese={kernelName:np,backendName:"webgpu",kernelFunc:sc};function Rse(e,t){let n=new nc(e.shape,22),s=t.runWebGPUProgram(n,[e],"int32");return{dataId:s.dataId,shape:s.shape,dtype:s.dtype}}function Xm(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{dtype:a}=s;if(a==="complex64"){if(r.dtype==="complex64")return es({inputs:{x:r},backend:n});let i=$t(r.shape),o=Xm({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),u=uu({inputs:{real:o,imag:i},backend:n});return i.dispose(),n.disposeData(o.dataId),u}if(r.dtype==="complex64"){let i=sc({inputs:{input:r},backend:n}),o=Xm({inputs:{x:i},backend:n,attrs:{dtype:a}});return n.disposeData(i.dataId),o}if(!w.hasEncodingLoss(r.dtype,a)){let i=es({inputs:{x:r},backend:n});return{dataId:i.dataId,shape:i.shape,dtype:a}}if(a==="int32")return Rse(r,n);if(a==="bool"){let i=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=S2({inputs:{a:r,b:i},backend:n});return n.disposeData(i.dataId),u}throw new Error(`Error in Cast: failed to cast ${r.dtype} to ${a}`)}var Dse={kernelName:Na,backendName:"webgpu",kernelFunc:Xm},Fse=Kt({opType:1,cpuKernelImpl:Ane}),Ose={kernelName:Ta,backendName:"webgpu",kernelFunc:Fse},Pse=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.shaderKey="clipVec4"}getUserCode(){return`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},zse=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="clip"}getUserCode(){return`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Mse(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{clipValueMin:a,clipValueMax:i}=s,o,u=[{type:"float32",data:[a]},{type:"float32",data:[i]}];return w.sizeFromShape(r.shape)%4===0?o=new Pse(r.shape):o=new zse(r.shape),n.runWebGPUProgram(o,[r],r.dtype,u)}var Lse={kernelName:Sr,backendName:"webgpu",kernelFunc:Mse},Bse=class{constructor(e){this.uniforms="",this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=S.computeOutShape(e,1),this.variableNames=e.map((t,n)=>`T${n}`),this.dispatchLayout=Be(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`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Zp(e){let{inputs:t,backend:n}=e,{input:s}=t,r=n.tensorMap.get(s.dataId);return es({inputs:{x:r.complexTensorInfos.imag},backend:n})}var Vse={kernelName:Zd,backendName:"webgpu",kernelFunc:Zp};function Ym(e,t,n){let s=e[0].dtype;if(s==="complex64"){let h=e.map(y=>sc({inputs:{input:y},backend:n})),f=e.map(y=>Zp({inputs:{input:y},backend:n})),m=Ym(h,t,n),g=Ym(f,t,n),b=uu({inputs:{real:m,imag:g},backend:n});return h.forEach(y=>n.disposeData(y.dataId)),f.forEach(y=>n.disposeData(y.dataId)),n.disposeData(m.dataId),n.disposeData(g.dataId),b}let r=n.shouldExecuteOnCPU(e);if(s==="string"&&(r=!0),r){let h=e.map(x=>{let k=w.sizeFromShape(x.shape.slice(t));return Me({inputs:{x},backend:n,attrs:{shape:[-1,k]}})}),f=h.map(x=>({vals:n.readSync(x.dataId),shape:x.shape})),m=S.computeOutShape(h.map(x=>x.shape),1),g=h[0].shape[0]===1,b=Ene(f,m,s,g),y=S.computeOutShape(e.map(x=>x.shape),t),v=n.makeTensorInfo(y,s,b);return h.forEach(x=>n.disposeData(x.dataId)),v}let{tensors2D:a,outShape:i}=Wse(e,t,n),o=a.map(h=>h.shape),u=new Bse(o),l=[],c=new Array(o.length-1);if(c.length>0){c[0]=o[0][1],l.push({type:"int32",data:[c[0]]});for(let h=1;h<c.length;h++)c[h]=c[h-1]+o[h][1],l.push({type:"int32",data:[c[h]]})}let p=n.runWebGPUProgram(u,a,a[0].dtype,l);a.forEach(h=>n.disposeData(h.dataId));let d=Me({inputs:{x:p},backend:n,attrs:{shape:i}});return n.disposeData(p.dataId),d}function Wse(e,t,n){let s=S.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>Me({inputs:{x:a},backend:n,attrs:{shape:[w.sizeFromShape(a.shape.slice(0,t)),w.sizeFromShape(a.shape.slice(t))]}})),outShape:s}}function C2(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s,a=w.parseAxisParam(r,t[0].shape)[0],i=S.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(i)===0)return n.makeTensorInfo(i,t[0].dtype,[]);let o=t.filter(l=>w.sizeFromShape(l.shape)>0);if(o.length===1)return es({inputs:{x:o[0]},backend:n});let u=o.map(l=>l.shape);return S.assertParamsConsistent(u,a),Ym(o,a,n)}var Use={kernelName:oo,backendName:"webgpu",kernelFunc:C2},Gse=class{constructor(e,t=!1,n=null,s=!1,r=!1){this.variableNames=["x","W"],this.uniforms=`filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>,
dimAOuter : i32, dimBOuter : i32, dimInner : i32,`,this.workGroupSize=[8,8,1],this.isVec4=!0,this.outputShape=e.outShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.outputShape[1]===1?this.elementsPerThread=[4,1,1]:this.elementsPerThread=[4,4,1],this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivationWeights=s,this.hasLeakyreluAlpha=r,this.addBias&&this.variableNames.push("bias"),this.hasPreluActivationWeights&&this.variableNames.push("preluActivationWeights"),this.hasLeakyreluAlpha&&this.variableNames.push("leakyreluAlpha"),this.tileAOuter=this.outputShape[1]===1?1:this.workGroupSize[1]*this.elementsPerThread[1],this.tileBOuter=this.workGroupSize[0]*this.elementsPerThread[0],this.tileInner=this.tileBOuter,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`conv2DMMVec4_${this.activation}_${this.fitA}_${this.fitB}_${this.elementsPerThread}`}getShapeFit(){let e=[this.tileAOuter,this.tileInner],t=[this.tileInner,this.tileBOuter],n=this.outputShape[1]*this.outputShape[2],s=this.outputShape[3],r=this.convInfo.filterHeight*this.convInfo.filterWidth*this.convInfo.inChannels;return[Ks(e,[n,r]),Ks(t,[r,s])]}getSampleAWithRemainder(e){return`let flatIndex${e} = getIndexFromCoords4D(coord, uniforms.xShape);
let divBy4Remainder${e} = flatIndex${e} % 4;
let divBy4Index${e} = flatIndex${e} / 4;
let curData${e} = x[divBy4Index${e}];
if (divBy4Remainder${e} == 0) {
temp = curData${e};
} else {
// TODO: This could end up being a redundant load with another one in
// the same shader invocation. Perhaps there's an opportunity for
// optimization
let nextData${e} = x[divBy4Index${e} + 1];
if (divBy4Remainder${e} == 1) {
temp = vec4<f32>(curData${e}.yzw, nextData${e}.x);
} else if (divBy4Remainder${e} == 2) {
temp = vec4<f32>(curData${e}.zw, nextData${e}.xy);
} else if (divBy4Remainder${e} == 3) {
temp = vec4<f32>(curData${e}.w, nextData${e}.xyz);
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}getUserCode(){let e=y2(this.elementsPerThread,this.tileAOuter,this.tileBOuter,this.tileInner),s=`let outRow = r / uniforms.outShape[2];
let outCol = r % uniforms.outShape[2];
let WRow = c / (uniforms.filterDims[1] * uniforms.xShape[3]);
let WCol = c / uniforms.xShape[3] % uniforms.filterDims[1];
let inChCoord = c % uniforms.xShape[3];
var coord = vec4<i32>(
batch,
outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0],
outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1],
inChCoord);
var resData = vec4<f32>(0.0);
${this.convInfo.inChannels%4===0?`// The bounds checking is always needed since we use it to pad zero for
// the 'same' padding type.
if (coordsInBounds4D(coord, uniforms.xShape)) {
resData = x[getIndexFromCoords4D(coord, uniforms.xShape) / 4];
} else {
resData = vec4<f32>(0.0); }`:`var temp = vec4<f32>(0.0);
${this.getSampleAWithRemainder(1)}
resData = temp;
if (WCol == (uniforms.filterDims[1] - 1)) {
coord = vec4<i32>(
coord.x, coord.y + 1, coord.z + 1 - uniforms.filterDims[1], 0);
${this.getSampleAWithRemainder(2)}
if (inChCoord == 0) {
resData = vec4<f32>(resData.xyz, temp.x);
} else if (inChCoord == 1) {
resData = vec4<f32>(resData.xy, temp.xy);
} else {
resData = vec4<f32>(resData.x, temp.xyz);
}
}
`}
return resData;`,r=this.fitA?`${s}`:`if (r < uniforms.dimAOuter && c < uniforms.dimInner) {
${s}
}
return vec4<f32>(0.0);
`,a=this.fitB?"return W[row * uniforms.dimBOuter / 4 + col];":`if(coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return W[row * uniforms.dimBOuter / 4 + col];
}
return vec4<f32>(0.0);
`,i="",o="";if(this.activation){let c=Zs(this.activation,this.isVec4);if(this.hasPreluActivationWeights)i=`fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${c}
}`;else{if(this.hasLeakyreluAlpha)throw i=`fn activation(outCoord: vec4<f32>) -> vec4<f32> {
let b = getLeakyreluAlphaByOutputCoords(outCoord);
${c}
}`,new Error("Leakyrelu is not supported.");i=`
fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
${c}
}`}o="value = activation(value, outCoord);"}let u=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${i}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
let r = row;
let c = col * 4;
var batch = i32(globalId.z);
${r}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
${a}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn mm_write(row : i32, col : i32, valueInput : vec4<f32>, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
if (row < uniforms.dimAOuter && col * 4 < uniforms.dimBOuter)
{
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col * 4);
${u}
${o}
setOutputAtCoords(outCoord[0], outCoord[1], outCoord[2], outCoord[3],
value);
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${e}
`}},Hse=class{constructor(e,t=!1,n=null,s=!1){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>, dimAOuter : i32, dimBOuter : i32, dimInner : i32,",this.outputShape=e.outShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.workGroupSize=Cv(this.dispatchLayout,this.outputShape),this.elementsPerThread=Nv(this.dispatchLayout,this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),t&&this.variableNames.push("bias"),s&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivationWeights=s,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`conv2DMM_${this.elementsPerThread}_${this.activation}_${this.fitA}_${this.fitB}`}getShapeFit(){let e=this.workGroupSize[1]*this.elementsPerThread[1],t=this.workGroupSize[0]*this.elementsPerThread[0],n=e>t?e:t;w.assert(n%this.workGroupSize[0]===0&&n%this.workGroupSize[1]===0,()=>"tileInner must be multiple of workgroupsize.x and workgroupsize.y");let s=[e,n],r=[n,t],a=this.outputShape[1]*this.outputShape[2],i=this.outputShape[3],o=this.convInfo.filterHeight*this.convInfo.filterWidth*this.convInfo.inChannels;return[Ks(s,[a,o]),Ks(r,[o,i])]}getUserCode(){let e=$v(this.elementsPerThread,this.workGroupSize),t=`
let outRow = row / uniforms.outShape[2];
let outCol = row % uniforms.outShape[2];
let WRow = col / (uniforms.filterDims[1] * uniforms.xShape[3]);
let WCol = col / uniforms.xShape[3] % uniforms.filterDims[1];
let coord = vec4<i32>(
batch,
outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0],
outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1],
col % uniforms.xShape[3]);
// The bounds checking is always needed since we use it to pad zero for the
// 'same' padding type.
if(coordsInBounds4D(coord, uniforms.xShape)) {
return x[getIndexFromCoords4D(coord, uniforms.xShape)];
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return 0.0;`,n=this.fitA?`${t}`:`if (row < uniforms.dimAOuter && col < uniforms.dimInner) {
${t}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return 0.0;
`,s=this.fitB?"return W[row * uniforms.dimBOuter + col];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return W[row * uniforms.dimBOuter + col];
}
return 0.0;
`,r="",a="";if(this.activation){let u=Zs(this.activation,!1);this.hasPreluActivationWeights?r=`fn activation(a: f32, outCoord : vec4<i32>) -> f32 {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${u}
}`:r=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
${u}
}
`,a="value = activation(value, outCoord);"}let i=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${r}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
var batch = i32(globalId.z);
${n}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
${s}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn mm_write(row : i32, col : i32, valueInput : f32, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col);
${i}
${a}
result[getIndexFromCoords4D(outCoord, uniforms.outShape)] = value;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${e}
`}},qse=class{constructor(e,t=!1,n=null,s=!1){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>, pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>,",this.workGroupSize=[128,1,1],this.outputShape=e.outShape,this.dispatchLayout=Be(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.hasPreluActivationWeights=s,this.shaderKey=`conv2DNaive_${this.activation}`}getUserCode(){let e="",t="";if(this.activation){let r=Zs(this.activation);this.hasPreluActivationWeights?e=`fn activation(a : f32, outCoord : vec4<i32>) -> f32{
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${r}
}`:e=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32{
${r}
}
`,t="value = activation(value, outCoord);"}let n=this.addBias?"value = value + getBiasByOutputCoords(outCoord);":"";return`
${e}
fn readInp(batch : i32, row : i32, col : i32, chan : i32) -> f32 {
let coord = vec4<i32>(batch, row, col, chan);
if(coordsInBounds4D(coord, uniforms.xShape)) {
return getX(batch, row, col, chan);
}
return 0.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn readFilt(row : i32, col : i32, xChannel : i32, outChannel : i32) -> f32{
let coord = vec4<i32>(row, col, xChannel, outChannel);
if(coordsInBounds4D(coord, uniforms.wShape)) {
return getW(row, col, xChannel, outChannel);
}
return 0.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
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)) {
${n}
${t}
setOutputAtCoords(batch, row, col, chan, value);
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${Dr()}
let coords = getOutputCoords();
let batch = coords[0];
let outChannel = coords[3];
var acc = 0.0;
for (var row = 0; row < uniforms.filterDims[0]; row = row + 1) {
for (var col = 0; col < uniforms.filterDims[1]; col = col + 1) {
for (var xChannel = 0; xChannel < uniforms.xShape[3]; xChannel = xChannel + 1) {
let coordRow = coords[1] * uniforms.stride[0] + uniforms.dilation[0] * row - uniforms.pad[0];
let coordCol = coords[2] * uniforms.stride[1] + uniforms.dilation[1] * col - uniforms.pad[1];
let v = readInp(batch, coordRow, coordCol, xChannel);
let f = readFilt(row, col, xChannel, outChannel);
acc = acc + v * f;
}
}
}
writeResult(batch, coords[1], coords[2], outChannel, acc);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},jse=class{constructor(e,t){this.variableNames=["A"],this.uniforms=`pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>, outWidth : i32, itemsPerBlockRow : i32,
inChannels : i32,`,this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.isChannelsLast=t,this.shaderKey=`im2col_${this.isChannelsLast}`}getUserCode(){let e=this.isChannelsLast?0:1,t=this.isChannelsLast?1:2;return`
${Ue()}
for(var i = 0; i<${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
let rc = getCoordsFromIndex(flatIndex);
if(flatIndex < uniforms.size) {
let blockIndex = rc[0];
let pos = rc[1];
let offsetY = blockIndex / uniforms.outWidth * uniforms.stride[1] - uniforms.pad[1];
let d0 = offsetY + uniforms.dilation[1] * pos / uniforms.itemsPerBlockRow;
var value = 0.0;
if(d0 < uniforms.aShape[${e}] && d0 >= 0) {
let offsetX = (blockIndex % uniforms.outWidth) * uniforms.stride[0] -
uniforms.pad[0];
let d1 = offsetX + uniforms.dilation[0] * ((pos %
uniforms.itemsPerBlockRow) / uniforms.inChannels);
let ch = pos % uniforms.inChannels;
if(d1 < uniforms.aShape[${t}] && d1 >= 0) {
value = getA(d0, d1, ch);
}
}
setOutputAtIndex(flatIndex, value);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Kse({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let u=e.shape,l=n.dataFormat==="channelsLast",c=!1,p=!1,d=n.filterHeight===n.inHeight&&n.filterWidth===n.inWidth&&n.padInfo.type==="VALID",h,f;if(d){let b=n.inHeight*n.inWidth*n.inChannels;h=Me({inputs:{x:e},backend:s,attrs:{shape:[1,n.batchSize,b]}}),f=Me({inputs:{x:t},backend:s,attrs:{shape:[1,b,n.outChannels]}})}else{let b=l?u[0]*u[1]*u[2]:u[0]*u[2]*u[3];h=Me({inputs:{x:e},backend:s,attrs:{shape:[1,b,n.inChannels]}}),f=Me({inputs:{x:t},backend:s,attrs:{shape:[1,n.inChannels,n.outChannels]}})}let m=_v({a:h,b:f,transposeA:c,transposeB:p,backend:s,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i}),g=Me({inputs:{x:m},backend:s,attrs:{shape:n.outShape}});return s.disposeData(h.dataId),s.disposeData(f.dataId),s.disposeData(m.dataId),g}function Xse({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let{filterWidth:u,filterHeight:l,inChannels:c,strideWidth:p,strideHeight:d,padInfo:h,outWidth:f,outHeight:m,dilationWidth:g,dilationHeight:b,dataFormat:y}=n,v=y==="channelsLast",x=u*l*c,k=m*f,T=[k,x],N=!1,E=!1,A=[],P=Me({inputs:{x:e},backend:s,attrs:{shape:e.shape.slice(1)}}),R=Me({inputs:{x:t},backend:s,attrs:{shape:[1,x,-1]}});A.push(P),A.push(R);let F=new jse(T,v),$=[{type:"int32",data:[h.left,h.top]},{type:"int32",data:[p,d]},{type:"int32",data:[g,b]},{type:"int32",data:[f]},{type:"int32",data:[c*u]},{type:"int32",data:[c]}],z=s.runWebGPUProgram(F,[P],P.dtype,$),W=Me({inputs:{x:z},backend:s,attrs:{shape:[1,T[0],T[1]]}});A.push(z),A.push(W);let q=[1,T[0],T[1]],K=new v2(q,[1,k,n.outChannels],X().get("WEBGPU_MATMUL_WORK_PER_THREAD"),N,E,r,o,a),Y=q[1],Z=q[2],te=n.outChannels,ee=[{type:"int32",data:[Y]},{type:"int32",data:[te]},{type:"int32",data:[Z]}],se=[W,R];r&&se.push(r),a&&se.push(a),o==="leakyrelu"&&($.push({type:"float32",data:[i]}),K.uniforms+=" alpha : f32,");let ne=s.runWebGPUProgram(K,se,W.dtype,ee),oe=v?[1,m,f,n.outChannels]:[1,n.outChannels,m,f],re=Me({inputs:{x:ne},backend:s,attrs:{shape:oe}});A.push(ne);for(let le of A)s.disposeData(le.dataId);return re}function N2({x:e,filter:t,convInfo:n,backend:s,bias:r=null,preluActivationWeights:a=null,leakyreluAlpha:i=0,activation:o=null}){let u=r!=null,l=a!=null,c;if(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 Kse({x:e,filter:t,convInfo:n,backend:s,bias:r,activation:o,preluActivationWeights:a,leakyreluAlpha:i});if(X().getBool("WEBGPU_CONV_SEPARATE_IM2COL_SHADER")&&e.shape[0]===1)return Xse({x:e,filter:t,convInfo:n,backend:s,bias:r,preluActivationWeights:a,leakyreluAlpha:i,activation:o});let d=X().getBool("WEBGPU_USE_NAIVE_CONV2D"),h=(n.inChannels%4===0||n.inChannels===3&&n.padInfo.type==="VALID")&&n.outChannels%4===0&&n.outChannels>=32,f=[n.padInfo.top,n.padInfo.left],m=[{type:"int32",data:[n.filterHeight,n.filterWidth]},{type:"int32",data:[...f]},{type:"int32",data:[n.strideHeight,n.strideWidth]},{type:"int32",data:[n.dilationHeight,n.dilationWidth]}];if(d)c=new qse(n,u,o,l);else{h?c=new Gse(n,u,o,l):c=new Hse(n,u,o,l);let b=n.outShape[1]*n.outShape[2],y=n.outShape[3],v=n.filterHeight*n.filterWidth*n.inShape[3];m.push({type:"int32",data:[b]},{type:"int32",data:[y]},{type:"int32",data:[v]})}let g=[e,t];return u&&g.push(r),l&&g.push(a),o==="leakyrelu"&&(m.push({type:"float32",data:[i]}),c.uniforms+=" alpha : f32,"),s.runWebGPUProgram(c,g,e.dtype,m)}function Yse(e){let{inputs:t,attrs:n,backend:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dataFormat:u,dilations:l,dimRoundingMode:c}=n,p=S.convertConv2DDataFormat(u),d=S.computeConv2DInfo(r.shape,a.shape,i,l,o,c,!1,p);return N2({x:r,filter:a,convInfo:d,backend:s})}var Qse={kernelName:$a,backendName:"webgpu",kernelFunc:Yse},Zse=class{constructor(e){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>,
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
if (xC < 0.0 || xC >= f32(uniforms.outBackprop[2]) || fract(xC) > 0.0) {
return 0.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
let coord = vec4<i32>(
batch,
i32(xR),
i32(xC),
col % uniforms.outBackprop[3]);
return x[getIndexFromCoords4D(coord, uniforms.xShape)];
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
return 0.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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)];
}
return 0.0;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
fn mm_write(row : i32, col : i32, valueInput : f32, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col);
result[getIndexFromCoords4D(outCoord, uniforms.outShape)] = value;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${$v(this.elementsPerThread,this.workGroupSize)}
`}},Jse=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=Be(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`
${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;
}
}
}
}
setOutputAtIndex(index, dotProd);
2022-04-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}};function ere(e){let{inputs:t,backend:n,attrs:s}=e,{dy:r,filter:a}=t,{inputShape:i,strides:o,pad:u,dataFormat:l,dimRoundingMode:c}=s,p=S.convertConv2DDataFormat(l),d=S.computeConv2DInfo(i,a.shape,o,1,u,c,!1,p),h=[{type:"int32",data:[d.filterHeight,d.filterWidth]},{type:"int32",data:[d.filterHeight-1-d.padInfo.top,d.filterWidth-1-d.padInfo.left]},{type:"int32",data:[d.strideHeight,d.strideWidth]},{type:"int32",data:[d.batchSize,d.outHeight,d.outWidth,d.outChannels]}],f;if(X().getBool("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE"))f=new Jse(d);else{f=new Zse(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 tre={kernelName:_a,backendName:"webgpu",kernelFunc:ere},nre=Kt({opType:2}),sre={kernelName:Aa,backendName:"webgpu",kernelFunc:nre},rre=Kt({opType:3}),are={kernelName:Ea,backendName:"webgpu",kernelFunc:rre},ire=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.methodId=s==="bilinear"?1:0,this.cropHeightBiggerThan1=this.outputShape[1]>1,this.cropWidthBiggerThan1=this.outputShape[2]>1,this.shaderKey=`cropAndResize_${this.methodId}_${this.cropHeightBiggerThan1}_${this.cropWidthBiggerThan1}`}getUserCode(){let[e,t]=["f32(uniforms.imageShape[1] - 1)","f32(uniforms.imageShape[2] - 1)"],[n,s,r]=this.cropHeightBiggerThan1?[`(${e} / f32(uniforms.outShape[1] - 1))`,"(y2-y1) * height_ratio",`y1*${e} + f32(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${e}`],[a,i,o]=this.cropWidthBiggerThan1?[`(${t} / f32(uniforms.outShape[2] - 1))`,"(x2-x1) * width_ratio",`x1*${t} + f32(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${t}`];return`
${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;
}
let height_scale = ${s};
let width_scale = ${i};
let in_y = ${r};
if( in_y < 0.0 || in_y > ${e} ) {
setOutputAtIndex(index, uniforms.extrapolationValue);
return;
}
let in_x = ${o};
if( in_x < 0.0 || in_x > ${t} ) {
setOutputAtIndex(index, uniforms.extrapolationValue);
return;
}
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-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}},ore=e=>{let{inputs:t,backend:n,attrs:s}=e,{image:r,boxes:a,boxInd:i}=t,{cropSize:o,method:u,extrapolationValue:l}=s,c=new ire(r.shape[3],a.shape,o,u),p=[{type:"float32",data:[l]}];return n.runWebGPUProgram(c,[r,a,i],"float32",p)},ure={kernelName:lo,backendName:"webgpu",kernelFunc:ore},lre=class{constructor(e,t){this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.uniforms="blockSize : i32,",this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`depthToSpace_${t}`,this.dataFormat=t}getUserCode(){return`
${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);
}
}`}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 cre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockSize:a,dataFormat:i}=s,o=r.shape[0],u=i==="NHWC"?r.shape[1]:r.shape[2],l=i==="NHWC"?r.shape[2]:r.shape[3],c=i==="NHWC"?r.shape[3]:r.shape[1],p=u*a,d=l*a,h=c/(a*a),f=i==="NHWC"?[o,p,d,h]:[o,h,p,d],m=[{type:"int32",data:[a]}],g=new lre(f,i);return n.runWebGPUProgram(g,[r],r.dtype,m)}var dre={kernelName:co,backendName:"webgpu",kernelFunc:cre},T2=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=Zs(this.activation,this.isVec4);this.hasPreluActivation?e=`fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
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}
${Sv()}
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);
}
}
}
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];
}
}
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},$2=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>, filterHeight : i32, filterWidth : i32,
channelMul : i32,`,this.workGroupSize=[256,1,1],this.outputShape=e.outShape,this.dispatchLayout=Be(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=Zs(this.activation,!1);this.hasPreluActivation?e=`fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
let b = getPreluActivationWeightsByOutputCoords(outCoord);
${r}
}`:e=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
${r}
}
`,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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
${Dr()}
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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function pre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a}=t,{strides:i,pad:o,dilations:u,dimRoundingMode:l}=s,c=u;c==null&&(c=[1,1]);let p=S.computeConv2DInfo(r.shape,a.shape,i,c,o,l,!0),d=[{type:"int32",data:[p.padInfo.top,p.padInfo.left]},{type:"int32",data:[p.strideHeight,p.strideWidth]},{type:"int32",data:[p.dilationHeight,p.dilationWidth]},{type:"int32",data:[p.inHeight,p.inWidth]}],h;return p.batchSize===1&&p.inHeight===p.outHeight&&p.inWidth===p.outWidth&&p.strideHeight===1&&p.strideWidth===1&&p.filterHeight===p.filterWidth&&p.inChannels===p.outChannels&&p.dilationHeight===1&&p.dilationWidth===1&&p.filterHeight===3&&p.inChannels%4===0?h=new T2(p):(h=new $2(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 hre={kernelName:Ra,backendName:"webgpu",kernelFunc:pre},_2=mn({opSnippet:0,cpuKernelImpl:qne,supportsComplex:!0}),fre={kernelName:Ya,backendName:"webgpu",kernelFunc:_2},mre=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]=S.computeOutAndReduceShapes(this.inputShape,[1]);this.outputShape=n.length===0?[1]:n,this.dispatchLayout=Be(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=`
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}
}
}
`}};function rc(e,t,n,s,r){let a=e.shape.length,i=[],o=w.parseAxisParam(t,e.shape),u=o,l=S.getAxesPermutation(u,a),c=e;l!=null&&(c=wi({inputs:{x:e},attrs:{perm:l},backend:r}),u=S.getInnerMostAxes(u.length,a),i.push(c)),S.assertAxesAreInnerMostDims(s,u,a);let[p,d]=S.computeOutAndReduceShapes(c.shape,u),h=p;n&&(h=S.expandShapeToKeepDim(p,o));let f;if((s==="max"||s==="prod")&&r.shouldExecuteOnCPU([c])){let m=r.tensorMap.get(c.dataId).values;switch(s){case"max":let g=Une(m,w.sizeFromShape(d),h,e.dtype);f=r.makeTensorInfo(h,e.dtype,g);break;case"prod":let{outVals:b,outShape:y,outDtype:v}=Xne(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":cp(e.dtype),x=[{type:"int32",data:[m]}],k=new mre(y,s),T=r.runWebGPUProgram(k,[c],v,x);i.push(T),f=Me({inputs:{x:T},attrs:{shape:h},backend:r})}return i.forEach(m=>r.disposeData(m.dataId)),f}function Av(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return rc(r,a,i,"sum",n)}var gre={kernelName:oi,backendName:"webgpu",kernelFunc:Av};function bre(e){let{inputs:t,backend:n,attrs:s}=e,{equation:r}=s,a=t,{allDims:i,summedDims:o,idDims:u}=S.decodeEinsumEquation(r,a.length);S.checkEinsumDimSizes(i.length,u,a);let{path:l,steps:c}=S.getEinsumComputePath(o,u),p=c.length,d=null,h=i.length,f=[];for(let m=0;m<p;++m){for(let g of c[m]){let{permutationIndices:b,expandDims:y}=S.getEinsumPermutation(h,u[g]),v;S.isIdentityPermutation(b)?v=a[g]:(v=wi({inputs:{x:a[g]},backend:n,attrs:{perm:b}}),f.push(v));let x=v.shape.slice();for(let k=0;k<y.length;++k)x.splice(y[k],0,1);w.arraysEqual(v.shape,x)||(v=Me({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),d===null?d=v:(d=_2({inputs:{a:v,b:d},backend:n}),f.push(d))}m<p-1&&(l[m]>=0&&(d=Av({inputs:{x:d},backend:n,attrs:{axis:l[m]-(i.length-h),keepDims:!1}}),f.push(d)),h--)}for(let m of f)m!==d&&n.disposeData(m.dataId);return d}var yre={kernelName:Qd,backendName:"webgpu",kernelFunc:bre},vre=Kt({opType:4}),xre={kernelName:Fa,backendName:"webgpu",kernelFunc:vre},wre=mn({opSnippet:4,dtype:"bool",cpuKernelImpl:Rne}),kre={kernelName:po,backendName:"webgpu",kernelFunc:wre},A2=Kt({opType:5,cpuKernelImpl:Dne,dtype:"float32"}),Ire={kernelName:Oa,backendName:"webgpu",kernelFunc:A2};function Qm(e){let{inputs:t,attrs:n,backend:s}=e,{dim:r}=n,{input:a}=t,i=a.shape.length,o=a.shape.slice(),u=r;return r<0&&(w.assert(-(i+1)<=r,()=>`Axis must be in the interval [${-(i+1)}, ${i}]`),u=i+r+1),o.splice(u,0,1),Me({inputs:{x:a},backend:s,attrs:{shape:o}})}var Sre={kernelName:ho,backendName:"webgpu",kernelFunc:Qm},Cre=Kt({opType:6,cpuKernelImpl:Fne}),Nre={kernelName:fo,backendName:"webgpu",kernelFunc:Cre},Tre=class{constructor(e){this.variableNames=[],this.outputShape=[],this.uniforms="value : f32,",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="fill"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
setOutputAtIndex(index, uniforms.value);
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
}
`}};function cu(e){let{backend:t,attrs:n}=e,{shape:s,value:r}=n,{dtype:a}=n;if(a=a||w.inferDtype(r),a==="string"){let i=w.getArrayFromDType(a,w.sizeFromShape(s));return i.fill(r),t.makeTensorInfo(s,a,i)}else{let i=new Tre(s),o=[{type:"float32",data:[r]}];return t.runWebGPUProgram(i,[],a,o)}}var $re={kernelName:fl,backendName:"webgpu",kernelFunc:cu},_re=class{constructor(e){this.outputShape=[],this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="flipLeftRight"}getUserCode(){return`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},Are={kernelName:mo,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,s=t,r=new _re(n.shape);return s.runWebGPUProgram(r,[n],n.dtype)}},Ere=Kt({opType:7,cpuKernelImpl:One}),Rre={kernelName:Pa,backendName:"webgpu",kernelFunc:Ere},Dre=mn({opSnippet:12,dtype:"int32"}),Fre={kernelName:za,backendName:"webgpu",kernelFunc:Dre},Ore=(e,t,n,s,r)=>{let a=[s,...n];return r&&a.push(r),e.createBindGroup({layout:t,entries:a.map((i,o)=>({binding:o,resource:i}))})},E2=(e,t,n,s,r,a=!1)=>{let i={dtype:r.dtype,shape:r.shape},o=lne(s,i,t,a),u=e.createShaderModule({code:o,label:t.constructor.name});return e.createComputePipeline({layout:n,compute:{module:u,entryPoint:"main"},label:t.constructor.name})};function R2(e,t,n,s="",r=""){return e.shaderKey+"_"+(e.workGroupSize?e.workGroupSize.join(","):"")+t.map(i=>i.length).join(",")+n.join(",")+e.variableNames.join(",")+s+r}function Ow(e){let{externalImage:t,backend:n,attrs:s,outShape:r,useImport:a}=e,{numChannels:i}=s,o=w.sizeFromShape(r),u=w.computeStrides(r),l=n.makeTensorInfo(r,"int32"),c=n.getFromPixelsProgram(a?"import":"copyExternal");c.updateOutputShape(r);let p=[l.shape],d=[l.dtype,a?"import":"copyExternal"],h=R2(c,p,d),f=c.getLayout(n.device),m=n.getAndSavePipeline(h,()=>E2(n.device,c,f.pipelineLayout,[],l,!0));c.setPipeline(m),a||n.queue.copyExternalImageToTexture({source:t,origin:{x:0,y:0}},{texture:c.makeInputTexture(n.device,r[1],r[0])},[r[1],r[0]]);let g=n.tensorMap.get(l.dataId);g.bufferInfo.buffer=n.acquireBuffer(g.bufferInfo.byteSize);let b=[o,i,...u,...c.dispatch];c.setUniform(n.device,b);let y;if(a){let v={source:t};y=n.device.importExternalTexture(v)}else y=c.inputTexture.createView();return n.runFromPixelsProgram(c,g.bufferInfo.buffer,f,y,l.dataId),l}var Pre={kernelName:fd,backendName:"webgpu",kernelFunc:zre},Bi;function zre(e){let{inputs:t,backend:n,attrs:s}=e,{pixels:r}=t,{numChannels:a}=s;if(r==null)throw new Error("pixels passed to tf.browser.fromPixels() can not be null");let i=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,o=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,u=typeof HTMLCanvasElement!="undefined"&&r instanceof HTMLCanvasElement||typeof OffscreenCanvas!="undefined"&&r instanceof OffscreenCanvas,l=typeof ImageBitmap!="undefined"&&r instanceof ImageBitmap,[c,p]=i?[r.videoWidth,r.videoHeight]:[r.width,r.height],d=[p,c,a];if(X().getBool("WEBGPU_USE_IMPORT")&&i)return Ow({externalImage:r,backend:n,attrs:s,outShape:d,useImport:!0});if((i||o)&&(Bi==null&&(Bi=document.createElement("canvas").getContext("2d")),Bi.canvas.width=c,Bi.canvas.height=p,Bi.drawImage(r,0,0,c,p),r=Bi.canvas),l||u||i||o)return Ow({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}var Mre=class{constructor(e,t,n,s,r){this.uniforms="varianceEpsilon : f32,",this.workGroupSize=[128,1,1],this.size=!0,this.variableNames=["x","mean","variance"],S.assertAndGetBroadcastShape(e,t),S.assertAndGetBroadcastShape(e,n),this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),s!=null&&(S.assertAndGetBroadcastShape(e,s),this.variableNames.push("offset")),r!=null&&(S.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)"),`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},Lre={kernelName:Ma,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s,scale:r,offset:a,mean:i,variance:o}=e,{varianceEpsilon:u}=t,l=n,c=[s,i,o],p=null;a!=null&&(p=a.shape,c.push(a));let d=null;r!=null&&(d=r.shape,c.push(r));let h=new Mre(s.shape,i.shape,o.shape,p,d),f=[{type:"float32",data:[u]}];return l.runWebGPUProgram(h,c,s.dtype,f)}};function Bre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dataFormat:c,dilations:p,dimRoundingMode:d,activation:h,leakyreluAlpha:f}=s,m=S.convertConv2DDataFormat(c),g=S.computeConv2DInfo(r.shape,a.shape,u,p,l,d,!1,m);return N2({x:r,filter:a,convInfo:g,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:f,activation:h})}var Vre={kernelName:aa,backendName:"webgpu",kernelFunc:Bre};function Wre(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,filter:a,bias:i,preluActivationWeights:o}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:p,activation:d,leakyreluAlpha:h}=s,f=c;f==null&&(f=[1,1]),w.assert(S.eitherStridesOrDilationsAreOne(u,f),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${f}'`);let m=S.computeConv2DInfo(r.shape,a.shape,u,f,l,p,!0),g=[r,a],b=i!=null,y=o!=null;b&&g.push(i),y&&g.push(o);let v=[{type:"int32",data:[m.padInfo.top,m.padInfo.left]},{type:"int32",data:[m.strideHeight,m.strideWidth]},{type:"int32",data:[m.dilationHeight,m.dilationWidth]},{type:"int32",data:[m.inHeight,m.inWidth]}],x;return m.batchSize===1&&m.inHeight===m.outHeight&&m.inWidth===m.outWidth&&m.strideHeight===1&&m.strideWidth===1&&m.filterHeight===m.filterWidth&&m.inChannels===m.outChannels&&m.dilationHeight===1&&m.dilationWidth===1&&m.filterHeight===3&&m.inChannels%4===0?x=new T2(m,b,d,y):(x=new $2(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 Ure={kernelName:ia,backendName:"webgpu",kernelFunc:Wre},Gre=class{constructor(e,t){this.variableNames=["A","indices"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.dispatchLayout=Be(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",`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Hre(e){let{inputs:t,backend:n}=e,{params:s,indices:r}=t,a=r.shape,i=a[a.length-1],o=w.sizeFromShape(s.shape),[u,l,c,p]=S.prepareAndValidate(s,r),d=Me({inputs:{x:r},backend:n,attrs:{shape:[l,i]}}),h=Me({inputs:{x:s},backend:n,attrs:{shape:[w.sizeFromShape(s.shape)/c,c]}});if(n.shouldExecuteOnCPU([s,r])||s.dtype==="string"){let y=n.readSync(r.dataId),v=n.bufferSync(s),x=Pne(y,v,s.dtype,l,i,c,p,s.shape,o);return n.makeTensorInfo(u,s.dtype,x.values)}let f=new Gre(i,[l,c]),m=[{type:"int32",data:[i]},{type:"int32",data:p}],g=n.runWebGPUProgram(f,[h,d],h.dtype,m),b=Me({inputs:{x:g},backend:n,attrs:{shape:u}});return n.disposeData(d.dataId),n.disposeData(h.dataId),n.disposeData(g.dataId),b}var qre={kernelName:bo,backendName:"webgpu",kernelFunc:Hre},jre=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="gather"}getUserCode(){let e=Kre(this.aShape);return`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Kre(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 D2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r,indices:a}=t,{axis:i,batchDims:o}=s,u=w.parseAxisParam(i,r.shape)[0],l=S.segment_util.collectGatherOpShapeInfo(r,a,u,o),c=w.sizeFromShape(a.shape),p=[],d=Me({inputs:{x:r},backend:n,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),h=Me({inputs:{x:a},backend:n,attrs:{shape:[l.batchSize,c/l.batchSize]}});p.push(d),p.push(h);let f=[l.batchSize,l.outerSize,c/l.batchSize,l.sliceSize];if(n.shouldExecuteOnCPU([r,a])){let v=n.tensorMap.get(h.dataId).values,x=De(h.shape,h.dtype,v),T=n.tensorMap.get(d.dataId).values,N=De(d.shape,d.dtype,T),E=zne(N,x,f);return p.forEach(A=>n.disposeData(A.dataId)),n.makeTensorInfo(l.outputShape,E.dtype,E.values)}let m=new jre(d.shape,f),g=n.runWebGPUProgram(m,[d,h],d.dtype);p.push(g);let b=Me({inputs:{x:g},backend:n,attrs:{shape:l.outputShape}});return p.forEach(y=>n.disposeData(y.dataId)),b}var Xre={kernelName:go,backendName:"webgpu",kernelFunc:D2},Yre=mn({opSnippet:5,cpuKernelImpl:Lne,dtype:"bool"}),Qre={kernelName:yo,backendName:"webgpu",kernelFunc:Yre},Zre=mn({opSnippet:6,dtype:"bool",cpuKernelImpl:Mne}),Jre={kernelName:La,backendName:"webgpu",kernelFunc:Zre};function eae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{alpha:a}=s,i=[{type:"float32",data:[a]}],o=new nc(r.shape,14);return o.uniforms="alpha : f32,",n.runWebGPUProgram(o,[r],"float32",i)}var tae={kernelName:Va,backendName:"webgpu",kernelFunc:eae},nae=mn({opSnippet:7,dtype:"bool",cpuKernelImpl:Vne}),sae={kernelName:vo,backendName:"webgpu",kernelFunc:nae},rae=mn({opSnippet:8,dtype:"bool",cpuKernelImpl:Bne}),aae={kernelName:xo,backendName:"webgpu",kernelFunc:rae},iae=Kt({opType:9,cpuKernelImpl:Wne}),oae={kernelName:Wa,backendName:"webgpu",kernelFunc:iae},uae=mn({opSnippet:9,dtype:"bool"}),lae={kernelName:wo,backendName:"webgpu",kernelFunc:uae},cae=Kt({opType:10}),dae={kernelName:vl,backendName:"webgpu",kernelFunc:cae};function F2(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{reductionIndices:a,keepDims:i}=s;return rc(r,a,i,"max",n)}var pae={kernelName:Ua,backendName:"webgpu",kernelFunc:F2},hae=mn({opSnippet:15,cpuKernelImpl:Gne}),fae={kernelName:Ga,backendName:"webgpu",kernelFunc:hae};function mae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{filterSize:a,strides:i,pad:o,dimRoundingMode:u}=s,l=1,c=S.computePool2DInfo(r.shape,a,i,l,o,u),p,d=[];if(c.filterHeight===1&&c.filterWidth===1){if(w.arraysEqual(c.inShape,c.outShape))return es({inputs:{x:r},backend:n});p=new I2(c),d.push({type:"int32",data:[c.strideHeight,c.strideWidth]})}else p=new k2(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 gae={kernelName:Ha,backendName:"webgpu",kernelFunc:mae};function bae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{keepDims:a,axis:i}=s;return rc(r,i,a,"mean",n)}var yae={kernelName:qa,backendName:"webgpu",kernelFunc:bae};function vae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return rc(r,a,i,"min",n)}var xae={kernelName:ja,backendName:"webgpu",kernelFunc:vae},wae=mn({opSnippet:16,cpuKernelImpl:Hne}),kae={kernelName:Ka,backendName:"webgpu",kernelFunc:wae},Iae=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=Be(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}]`:""
${Ue()}
if (index < uniforms.size) {
let start = ${i}(${t});
let end = ${i}(${n});
var outC = getCoordsFromIndex(index);
for (var i = 0; i < ${e}; i = i + 1) {
if (${a} < ${s}) {
${a} = ${s} * 2 - ${a} - ${this.offset};
} else if(${a} >= ${r}) {
${a} = (${r} - 1) * 2 - ${a} + ${this.offset};
}
}
let coords = outC - start;
setOutputAtIndex(index, getX(${o}));
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},Sae={kernelName:Xa,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:s}=e,{paddings:r,mode:a}=t,i=n,o=r.map(c=>({type:"int32",data:[c[0],c[1]]})),u=new Iae(s.shape,r,a);return i.runWebGPUProgram(u,[s],s.dtype,o)}};function Cae(e){let{inputs:t,backend:n}=e,{x:s}=t;if(n.shouldExecuteOnCPU([s])){let a=n.tensorMap.get(s.dataId),[i,o]=jne(a.values,s.shape,s.dtype);return n.makeTensorInfo(o,s.dtype,i)}let r=new nc(s.shape,11);return n.runWebGPUProgram(r,[s],s.dtype)}var Nae={kernelName:ko,backendName:"webgpu",kernelFunc:Cae};function Tae(e){console.warn("tf.nonMaxSuppression() in webgpu locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u}=s,l=n.readSync(r.dataId),c=n.readSync(a.dataId),{selectedIndices:p}=xs.nonMaxSuppressionV3Impl(l,c,i,o,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var $ae={kernelName:So,backendName:"webgpu",kernelFunc:Tae};function _ae(e){console.warn("tf.nonMaxSuppression() in webgpu locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:s}=e,{boxes:r,scores:a}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:u,softNmsSigma:l}=s,c=n.readSync(r.dataId),p=n.readSync(a.dataId),d=i,h=o,f=u,m=l,{selectedIndices:g,selectedScores:b}=xs.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 Aae={kernelName:Co,backendName:"webgpu",kernelFunc:_ae};function Ld(e){let{inputs:t,backend:n}=e,{x:s}=t;if(s.dtype==="complex64"){let r=sc({inputs:{input:s},backend:n}),a=Ld({inputs:{x:r},backend:n}),i=Zp({inputs:{input:s},backend:n}),o=Ld({inputs:{x:i},backend:n}),u=uu({inputs:{real:a,imag:o},backend:n});return n.disposeData(r.dataId),n.disposeData(a.dataId),n.disposeData(i.dataId),n.disposeData(o.dataId),u}else return cu({attrs:{shape:s.shape,dtype:s.dtype,value:s.dtype==="string"?"":0},backend:n})}var Eae={kernelName:Go,backendName:"webgpu",kernelFunc:Ld};function O2(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=sc({inputs:{input:s},backend:n}),a=O2({inputs:{x:r},backend:n}),i=Zp({inputs:{input:s},backend:n}),o=Ld({inputs:{x:i},backend:n}),u=uu({inputs:{real:a,imag:o},backend:n});return n.disposeData(r.dataId),n.disposeData(a.dataId),n.disposeData(i.dataId),n.disposeData(o.dataId),u}else return cu({attrs:{shape:s.shape,dtype:s.dtype,value:1},backend:n})}var Rae={kernelName:No,backendName:"webgpu",kernelFunc:O2};function Dae(e){let{inputs:t,backend:n,attrs:s}=e,{axis:r}=s;if(t.length===1)return Qm({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let a=t[0].shape,i=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(i===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let o=[],u=t.map(c=>{let p=Qm({inputs:{input:c},backend:n,attrs:{dim:r}});return o.push(p),p}),l=C2({inputs:u,backend:n,attrs:{axis:r}});return o.forEach(c=>n.disposeData(c.dataId)),l}var Fae={kernelName:$o,backendName:"webgpu",kernelFunc:Dae},Oae=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),t.map((n,s)=>{this.uniforms+=` pad${s} : vec2<i32>,`}),this.xShape=e,this.shaderKey="pad"}getUserCode(){let e=this.xShape.length,t=Wt(e),n=this.xShape.map((c,p)=>`uniforms.pad${p}[0]`).join(","),s=this.xShape.map((c,p)=>`uniforms.pad${p}[0] + uniforms.xShape${e>1?`[${p}]`:""}`).join(","),r=e>1?`${t}(${n})`:`${n}`,a=e>1?`${t}(${s})`:`${s}`,i=e>1?"any(outC < start)":"outC < start",o=e>1?"any(outC >= end)":"outC >= end",u=e>1?["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,e):"coords";return`
${Ue()}
if (index < uniforms.size) {
let start = ${r};
let end = ${a};
let outC = getCoordsFromIndex(index);
if (${i} || ${o}) {
setOutputAtIndex(index, uniforms.constantValue);
} else {
let coords = outC - start;
setOutputAtIndex(index, getX(${u}));
}
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}},P2=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{paddings:a,constantValue:i}=s;if(a.every(l=>w.arraysEqual(l,[0,0])))return es({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 cu({backend:n,attrs:{shape:l,value:i,dtype:r.dtype}})}let o=[{type:"float32",data:[i]}];a.map(l=>o.push({type:"int32",data:[l[0],l[1]]}));let u=new Oae(r.shape,a);return n.runWebGPUProgram(u,[r],r.dtype,o)},Pae={kernelName:Qa,backendName:"webgpu",kernelFunc:P2},zae=mn({opSnippet:13}),Mae={kernelName:Za,backendName:"webgpu",kernelFunc:zae};function Lae(e){let{inputs:t,backend:n}=e,{x:s,alpha:r}=t,a=new x2(14,s.shape,r.shape);return n.runWebGPUProgram(a,[s,r],"float32")}var Bae={kernelName:Ja,backendName:"webgpu",kernelFunc:Lae};function Vae(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{axis:a,keepDims:i}=s;return rc(r,a,i,"prod",n)}var Wae={kernelName:_o,backendName:"webgpu",kernelFunc:Vae},Uae=e=>{let{backend:t,attrs:n}=e,{start:s,stop:r,step:a,dtype:i}=n,o=Yne(s,r,a,i);return t.makeTensorInfo([o.length],i,o)},Gae={kernelName:kl,backendName:"webgpu",kernelFunc:Uae},z2=mn({opSnippet:3}),Hae={kernelName:Da,backendName:"webgpu",kernelFunc:z2},qae=Kt({opType:12}),jae={kernelName:ei,backendName:"webgpu",kernelFunc:qae},Kae=Kt({opType:13}),Xae={kernelName:ni,backendName:"webgpu",kernelFunc:Kae},Yae=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="resizeBilinear"}getUserCode(){return`
${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-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}};function Qae(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,size:i,halfPixelCenters:o}=s,[u,l]=i,c=a&&u>1?1:0,p=a&&l>1?1:0,h=[{type:"float32",data:[c,p]},{type:"float32",data:[o?.5:0]}],f=new Yae(r.shape,u,l);return n.runWebGPUProgram(f,[r],"float32",h)}var Zae={kernelName:ti,backendName:"webgpu",kernelFunc:Qae},Jae=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=Be(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",`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function eie(e){let{inputs:t,backend:n,attrs:s}=e,{images:r}=t,{alignCorners:a,halfPixelCenters:i,size:o}=s,[u,l]=o,c=a&&u>1?1:0,p=a&&l>1?1:0,h=[{type:"float32",data:[c,p]},{type:"float32",data:[a?.5:0]}],f=new Jae(r.shape,u,l,i);return n.runWebGPUProgram(f,[r],r.dtype,h)}var tie={kernelName:Sl,backendName:"webgpu",kernelFunc:eie},nie=class{constructor(e,t){this.outputShape=[],this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms=`centerX : f32, centerY : f32, sinRadians : f32,
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);
}
}
`}},sie={kernelName:Ho,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:s}=e,{radians:r,fillValue:a,center:i}=t,o=n,u=new nie(s.shape,a),[l,c]=S.getImageCenter(i,s.shape[1],s.shape[2]),p=[{type:"float32",data:[l]},{type:"float32",data:[c]},{type:"float32",data:[Math.sin(r)]},{type:"float32",data:[Math.cos(r)]}];return typeof a=="number"?p.push({type:"float32",data:[Number.parseFloat(a.toFixed(2))]}):p.push({type:"float32",data:a}),o.runWebGPUProgram(u,[s],s.dtype,p)}},rie=Kt({opType:15,cpuKernelImpl:Qne}),aie={kernelName:si,backendName:"webgpu",kernelFunc:rie},iie=class{constructor(e,t,n,s,r,a,i){this.variableNames=["updates","indices"],this.workGroupSize=[64,1,1],this.atomic=!0,this.outputShape=a,this.type=i,this.dispatchLayout=Be(e),this.dispatch=_e(this.dispatchLayout,e,this.workGroupSize),this.sliceDimGreaterThanOne=t>1,this.shaderKey=`scatter_${n}_${s}_${this.sliceDimGreaterThanOne}_${i}`;let o=Wt(r.length);this.uniforms=`sliceDim : i32, strides: ${o}, size: i32,`,this.updatesRank=s,this.indicesRank=n}getUserCode(){let e="";this.indicesRank===1?e="coords[0]":this.indicesRank===2&&(e="coords[0], j");let t=`getIndices(${e})`,n=this.sliceDimGreaterThanOne?"uniforms.strides[j]":"uniforms.strides",s="",r="",a="";this.updatesRank===1?(s="coords[0]",r="flattenedIndex",a=`
fn getUpdatesCoordsFromFlatIndex(index : i32) -> i32 {
return index;
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`);let i=`getUpdates(${s})`,o=this.type==="int32"?"atomicAdd(&(result[flatIndex]), i32(updateValue));":`
var assumed = atomicLoad(&(result[flatIndex]));
var success = 0;
for (; success == 0;) {
let new = bitcast<f32>(assumed) + updateValue;
let newI32 = bitcast<i32>(new);
let resValue = atomicCompareExchangeWeak(&(result[flatIndex]), assumed, newI32);
assumed = resValue[0];
success = resValue[1];
}
`;return`
${a}
${Ue()}
if (index < uniforms.size) {
let coords = getUpdatesCoordsFromFlatIndex(index);
var flattenedIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexInside = i32(round(${t}));
flattenedIndex = flattenedIndex + indexInside * ${n};
}
let updateValue = ${i};
let flatIndex = getOutputIndexFromCoords(${r});
${o}
}
}`}};function oie(e){let{inputs:t,backend:n,attrs:s}=e,{indices:r,updates:a}=t,{shape:i}=s,{sliceRank:o,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=S.calculateShapes(a,r,i),d=[p/l,l];if(p===0)return n.makeTensorInfo(i,r.dtype);let h=Me({inputs:{x:r},backend:n,attrs:{shape:[u,o]}}),f=Me({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=f.dtype,g=cu({backend:n,attrs:{shape:d,value:0,dtype:m}}),b=w.sizeFromShape(f.shape),y=[{type:"int32",data:[o]},{type:"int32",data:c},{type:"int32",data:[b]}],v=new iie(f.shape,o,h.shape.length,f.shape.length,c,d,m),x=n.runWebGPUProgram(v,[f,h],m,y,g),k=Me({inputs:{x},backend:n,attrs:{shape:i}});return n.disposeData(h.dataId),n.disposeData(f.dataId),n.disposeData(x.dataId),k}var uie={kernelName:Do,backendName:"webgpu",kernelFunc:oie},lie=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.cRank=e,this.rank=n,this.shaderKey="select"}getUserCode(){let e,t;if(this.rank>4)throw Error(`Where for rank ${this.rank} is not yet supported`);if(this.rank===1)t="resRC",e="resRC";else{let s=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[],a=[];for(let i=0;i<this.outputShape.length;i++)a.push(`${s[i]}`),i<this.cRank&&r.push(`${s[i]}`);e=r.join(),t=a.join()}return`
${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-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function cie(e){let{inputs:t,backend:n}=e,{condition:s,t:r,e:a}=t,i=new lie(s.shape.length,r.shape,r.shape.length);return n.runWebGPUProgram(i,[s,r,a],cn(r.dtype,a.dtype))}var die={kernelName:Fo,backendName:"webgpu",kernelFunc:cie},pie=Kt({opType:18}),hie={kernelName:ai,backendName:"webgpu",kernelFunc:pie},fie=Kt({opType:16}),mie={kernelName:ri,backendName:"webgpu",kernelFunc:fie},gie=Kt({opType:17}),bie={kernelName:Po,backendName:"webgpu",kernelFunc:gie},M2=mn({opSnippet:2,cpuKernelImpl:nse,supportsComplex:!0}),yie={kernelName:ci,backendName:"webgpu",kernelFunc:M2};function vie(e){let{inputs:t,backend:n,attrs:s}=e,{logits:r}=t,{dim:a}=s,i=w.parseAxisParam([a],r.shape),o=F2({inputs:{x:r},backend:n,attrs:{reductionIndices:i,keepDims:!1}}),u=S.expandShapeToKeepDim(o.shape,i),l=Me({inputs:{x:o},backend:n,attrs:{shape:u}}),c=M2({inputs:{a:r,b:l},backend:n}),p=A2({inputs:{x:c},backend:n}),d=Av({inputs:{x:p},backend:n,attrs:{axis:i,keepDims:!1}}),h=Me({inputs:{x:d},backend:n,attrs:{shape:u}}),f=z2({inputs:{a:p,b:h},backend:n});return n.disposeData(o.dataId),n.disposeData(l.dataId),n.disposeData(c.dataId),n.disposeData(p.dataId),n.disposeData(d.dataId),n.disposeData(h.dataId),f}var xie={kernelName:ui,backendName:"webgpu",kernelFunc:vie},wie=e=>{let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{blockShape:a,paddings:i}=s;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGPU backend not implemented yet");let o=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...i);for(let b=1+a.length;b<r.shape.length;++b)u.push([0,0]);let l=[],c=P2({inputs:{x:r},backend:n,attrs:{paddings:u,constantValue:0}}),p=S.getReshaped(c.shape,a,o,!1),d=S.getPermuted(p.length,a.length,!1),h=S.getReshapedPermuted(c.shape,a,o,!1),f=Me({inputs:{x:c},backend:n,attrs:{shape:p}}),m=wi({inputs:{x:f},backend:n,attrs:{perm:d}}),g=Me({inputs:{x:m},backend:n,attrs:{shape:h}});return l.push(c),l.push(f),l.push(m),l.forEach(b=>n.disposeData(b.dataId)),g},kie={kernelName:zo,backendName:"webgpu",kernelFunc:wie},Iie=class{constructor(e,t,n,s,r,a,i=!0){this.variableNames=["updates","indices","defaultValue"],this.workGroupSize=[64,1,1],this.workPerThread=4,this.size=!0,this.outputShape=a,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]);let o=t>1;this.shaderKey=`scatter_${n}_${s}_${o}`;let u=Wt(r.length);this.uniforms=`updateSize : i32, sliceDim : i32, strides: ${u},`;let l="";n===1?l="i":n===2&&(l="i, j"),this.indicesSnippet=`getIndices(${l})`;let c="";s===1?c="i":s===2&&(c="i, coords[1]"),this.updatesSnippet=`getUpdates(${c})`,this.strideString=o?"uniforms.strides[j]":"uniforms.strides"}getUserCode(){return`
${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])));
}
}
}
}`}};function Sie(e){let{inputs:t,backend:n,attrs:s}=e,{sparseIndices:r,sparseValues:a,defaultValue:i}=t,{outputShape:o}=s,{sliceRank:u,numUpdates:l,strides:c,outputSize:p}=S.calculateShapes(a,r,o),d=!1,h=[{type:"int32",data:[l]},{type:"int32",data:[u]},{type:"int32",data:c}],f=new Iie(l,u,r.shape.length,a.shape.length,c,[p,1],d),m=n.runWebGPUProgram(f,[a,r,i],a.dtype,h),g=Me({inputs:{x:m},backend:n,attrs:{shape:o}});return n.disposeData(m.dataId),g}var Cie={kernelName:ip,backendName:"webgpu",kernelFunc:Sie};function Nie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=s,o=w.parseAxisParam(i,r.shape)[0],u=S.prepareSplitSize(r,a,o),l=r.shape.length,c=new Array(l).fill(0),p=r.shape.slice();return u.map(d=>{let h=[...p];h[o]=d;let f=lu({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[o]+=d,f})}var Tie={kernelName:Mo,backendName:"webgpu",kernelFunc:Nie},$ie=Kt({opType:19}),_ie={kernelName:ii,backendName:"webgpu",kernelFunc:$ie},Aie={kernelName:_l,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,s=t,r=new nc(n.shape,20);return s.runWebGPUProgram(r,[n],n.dtype)}},Eie=mn({opSnippet:11}),Rie={kernelName:li,backendName:"webgpu",kernelFunc:Eie},Die=class{constructor(e){this.variableNames=["x"],this.workPerThread=1,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(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`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromIndex(index);
setOutputAtIndex(index, getX(${t}));
}
}
`}};function Fie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=wt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=Me({inputs:{x:r},backend:n,attrs:{shape:f}});else if(g||b){w.assert(r.shape.length>=1,()=>`Input must have rank at least 1, got: ${r.shape.length}`);let T=wt.computeOutShape(y,v,x),N=lu({inputs:{x:r},backend:n,attrs:{begin:y,size:T}});k=Me({inputs:{x:N},backend:n,attrs:{shape:f}}),n.disposeData(N.dataId)}else if(n.shouldExecuteOnCPU([r])){let N=n.readSync(r.dataId),E=De(r.shape,r.dtype,N),A=ese(h,E,x,y);k=n.makeTensorInfo(f,r.dtype,A.values)}else{let N=new Die(h),E=[{type:"int32",data:y},{type:"int32",data:x}],A=n.runWebGPUProgram(N,[r],r.dtype,E);k=Me({inputs:{x:A},backend:n,attrs:{shape:f}}),n.disposeData(A.dataId)}return k}var Oie={kernelName:Lo,backendName:"webgpu",kernelFunc:Fie};function Pie(e){let{inputs:t,backend:n,attrs:s}=e,{separator:r,nGramWidths:a,leftPad:i,rightPad:o,padWidth:u,preserveShortSequences:l}=s,{data:c,dataSplits:p}=t,d=n.readSync(c.dataId),h=n.readSync(p.dataId),[f,m]=tse(d,h,r,a,i,o,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",m)]}var zie={kernelName:op,backendName:"webgpu",kernelFunc:Pie},Mie=Kt({opType:21}),Lie={kernelName:di,backendName:"webgpu",kernelFunc:Mie},Bie=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.rank=this.outputShape.length,this.shaderKey="tile"}getUserCode(){let e=Vie(this.rank,"uniforms.");return`
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromIndex(index);
setOutputAtIndex(index, getA(${e}));
}
2022-04-01 15:12:04 +02:00
}
2022-04-01 15:13:32 +02:00
`}};function Vie(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 Wie(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=De(r.shape,r.dtype,l),p=sse(c,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let i=new Bie(r.shape,a);return n.runWebGPUProgram(i,[r],r.dtype)}var Uie={kernelName:Cr,backendName:"webgpu",kernelFunc:Wie},Gie=class{constructor(e){this.variableNames=["x","indices"],this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms=`inputSize : i32, firstPass : i32, negativeInf : f32,
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;
}
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-01 15:12:04 +02:00
}
}
}
2022-04-01 15:13:32 +02:00
`}},Hie=class{constructor(e){this.variableNames=["x","indices"],this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms="inputSize : i32, firstPass : i32, k : i32,",this.shaderKey="merge"}getUserCode(){return`
${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-01 15:12:04 +02:00
}
}
}
2022-04-01 15:13:32 +02:00
`}};function Vi(e,t){t!==null&&e.disposeData(t.dataId)}function Pw(e){let t=1;for(;t<e;)t*=2;return t}function qie(e){let{inputs:t,backend:n,attrs:s}=e,{x:r}=t,{k:a,sorted:i}=s,o=r.shape,u=o[o.length-1];if(n.shouldExecuteOnCPU([r])){let k=n.readSync(r.dataId),[T,N]=rse(k,o,r.dtype,a,i);return[n.makeTensorInfo(T.shape,T.dtype,T.values),n.makeTensorInfo(N.shape,N.dtype,N.values)]}if(a===0)return o[o.length-1]=0,[n.makeTensorInfo(o,r.dtype,[]),n.makeTensorInfo(o,"int32",[])];if(u===1)return[r,cu({attrs:{shape:o,dtype:"int32",value:0},backend:n})];let c=w.sizeFromShape(o)/u,p=Me({inputs:{x:r},attrs:{shape:[c,u]},backend:n}),d=Pw(a),h=Pw(u),f=null,m=()=>f===null?[p,p]:[p,f],g=(k,T,N)=>{let E=m(),A=new Gie(N),R=[{type:"int32",data:[u]},{type:"int32",data:[f===null?1:0]},{type:"float32",data:[Number.NEGATIVE_INFINITY]},{type:"int32",data:[k]},{type:"int32",data:[T]}],F=f;f=n.runWebGPUProgram(A,E,"int32",R),Vi(n,F)};for(let k=1;k<d;k*=2){let T=k*2;for(let N=k;N>=1;N/=2)g(T,N,[c,h])}for(let k=h;k>d;k/=2){let T=m(),N=new Hie([c,k/2]),A=[{type:"int32",data:[u]},{type:"int32",data:[f===null?1:0]},{type:"int32",data:[d]}],P=f;f=n.runWebGPUProgram(N,T,"int32",A),Vi(n,P);let R=d/2,F=R*2;for(let $=R;$>=1;$/=2)g(F,$,f.shape)}let b=f;f=lu({inputs:{x:f},backend:n,attrs:{begin:0,size:[c,a]}}),Vi(n,b);let y=D2({inputs:{x:p,indices:f},backend:n,attrs:{axis:1,batchDims:1}});Vi(n,p);let v=o.slice(0,-1);v.push(a),b=f,f=Me({inputs:{x:f},attrs:{shape:v},backend:n}),Vi(n,b);let x=y;return y=Me({inputs:{x:y},attrs:{shape:v},backend:n}),Vi(n,x),[y,f]}var jie={kernelName:Vo,backendName:"webgpu",kernelFunc:qie},Kie=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=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="transform"}getUserCode(){return`
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);
}
return outCoord;
}
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;
}
return outputValue;
}
${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;
}
}
setOutputAtIndex(index, outputValue);
}
}
`}};function Xie(e){let{inputs:t,backend:n,attrs:s}=e,{image:r,transforms:a}=t,{interpolation:i,fillMode:o,fillValue:u,outputShape:l}=s,[c,p,d,h]=r.shape,[f,m]=l!=null?l:[p,d],g=[c,f,m,h],b=new Kie(g),y=i==="nearest"?1:2,v;switch(o){case"constant":v=1;break;case"reflect":v=2;break;case"wrap":v=3;break;case"nearest":v=4;break;default:v=1;break}let x=[{type:"int32",data:[y]},{type:"int32",data:[v]},{type:"float32",data:[u]}];return n.runWebGPUProgram(b,[r,a],"float32",x)}var Yie={kernelName:Wo,backendName:"webgpu",kernelFunc:Xie};function Qie(e){let{inputs:t,backend:n,attrs:s}=e,{value:r}=t,{axis:a}=s;a<0&&(a+=r.shape.length);let i=r,o=i.shape.length,u=r.shape[a],l=new Array(o-1),c=0;for(let m=0;m<o;m++)m!==a&&(l[c++]=i.shape[m]);let p=[],d=new Array(o).fill(0),h=i.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){d[a]=m;let g=lu({inputs:{x:i},backend:n,attrs:{begin:d,size:h}}),b=Me({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,p.push(g)}return p.forEach(m=>n.disposeData(m.dataId)),f}var Zie={kernelName:Uo,backendName:"webgpu",kernelFunc:Qie},Jie=[Sne,ose,lse,pse,yse,xse,kse,Sse,_se,Dse,Ose,Lse,$ne,Use,Qse,tre,sre,are,ure,dre,hre,yre,xre,kre,Ire,Sre,Nre,$re,Are,Pre,Rre,Fre,Lre,Vre,Ure,qre,Xre,Qre,Jre,Tne,Vse,tae,sae,aae,oae,lae,dae,pae,fae,gae,yae,xae,kae,Sae,fre,Nae,$ae,Aae,Ase,Rae,Fae,Pae,Mae,Bae,Wae,Gae,Ese,Hae,jae,Xae,kne,Zae,tie,sie,aie,uie,die,hie,mie,bie,Tse,Oie,zie,xie,kie,Cie,Tie,_ie,Aie,Rie,yie,gre,Lie,Uie,jie,Yie,gse,Zie,Eae];for(let e of Jie)Al(e);var eoe=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=zw(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=zw(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 zw(e,t){return`${e}_${t}`}var L2=class{constructor(){this.outputShape=[0],this.variableNames=[],this.workGroupSize=[256,1,1],this.lastUniformData=[],this.inputTexture=null,this.layout=null,this.lastPixelSize={width:0,height:0},this.disposed=!1,this.shaderKey="fromPixels",this.useImport=!1}updateOutputShape(e){w.arraysEqual(this.outputShape,e)||(this.outputShape=e,this.workPerThread=e[2],this.dispatchLayout=Be(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]))}makeFromPixelsSource(){let e=this.useImport?"textureLoad(src, vec2<i32>(coords.yx));":"textureLoad(src, vec2<i32>(coords.yx), 0)";return`
@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-01 15:12:04 +02:00
}
}
2022-04-01 15:13:32 +02:00
`}getUserCode(){return this.makeFromPixelsSource()}setPipeline(e){this.pipeline=e}setUniform(e,t){if(!this.uniform){let n=e.createBuffer({size:t.length*4,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST});this.uniform=n}!t||t.length===this.lastUniformData.length&&t.every((n,s)=>n===this.lastUniformData[s])||(e.queue.writeBuffer(this.uniform,0,new Uint32Array(t)),this.lastUniformData=t)}makeInputTexture(e,t,n){return(!this.inputTexture||this.lastPixelSize.width!==t||this.lastPixelSize.height!==n)&&(this.inputTexture&&this.inputTexture.destroy(),this.inputTexture=e.createTexture({size:[t,n],format:"rgba8unorm",usage:GPUTextureUsage.COPY_DST|GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.TEXTURE_BINDING}),this.lastPixelSize.width=t,this.lastPixelSize.height=n),this.inputTexture}dispose(){this.disposed||(this.uniform&&this.uniform.destroy(),this.inputTexture&&this.inputTexture.destroy(),this.disposed=!0)}getLayout(e){return this.layout===null&&(this.layout=this.createTextureLayout(e)),this.layout}createTextureLayout(e){let t=[];t.push({binding:0,visibility:GPUShaderStage.COMPUTE,buffer:{type:"storage"}}),t.push({binding:1,visibility:GPUShaderStage.COMPUTE,texture:{}}),t.push({binding:2,visibility:GPUShaderStage.COMPUTE,buffer:{}});let n=e.createBindGroupLayout({entries:t}),s=e.createPipelineLayout({bindGroupLayouts:[n]});return{bindGroupLayout:n,pipelineLayout:s}}},toe=class extends L2{constructor(){super(...arguments);this.layout=null,this.useImport=!0}getUserCode(){return this.makeFromPixelsSource()}getLayout(e){return this.layout===null&&(this.layout=this.createTextureImportLayout(e)),this.layout}createTextureImportLayout(e){let t=[];t.push({binding:0,visibility:GPUShaderStage.COMPUTE,buffer:{type:"storage"}}),t.push({binding:1,visibility:GPUShaderStage.COMPUTE,externalTexture:{}}),t.push({binding:2,visibility:GPUShaderStage.COMPUTE,buffer:{}});let n=e.createBindGroupLayout({entries:t}),s=e.createPipelineLayout({bindGroupLayouts:[n]});return{bindGroupLayout:n,pipelineLayout:s}}},noe=X().getNumber("WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD"),Mw=(e,t)=>{let n=e.limits.maxComputeWorkgroupsPerDimension,s=t.dispatchLayout,r=t.dispatch;if(r.every(i=>i<=n))return r;w.assert(r[0]>n&&s.y===void 0&&s.z===void 0,()=>"Dispatch size exceeds WebGPU limits in Y or Z dimension.");let a=Math.ceil(Math.sqrt(r[0]));return a>n?(a=Math.ceil(Math.cbrt(r[0])),w.assert(a<=n,()=>"Total dispatch size exceeds WebGPU maximum."),[a,a,a]):[a,a,1]},B2=class extends tl{constructor(e,t=!1){super();if(this.commandQueueOwnedIds=new WeakSet,this.tensorDisposalQueue=[],this.uniformDisposalQueue=[],this.stagingDisposalQueue=[],this.disposed=!1,this.uploadWaitMs=0,this.downloadWaitMs=0,this.dispatchNumberInEncoder=0,!Tv())throw new Error("WebGPU is not supported on this device");this.layoutCache={},this.pipelineCache={},this.device=e,this.queue=e.queue,this.currentCommandEncoder=null,this.currentComputePass=null,this.supportTimeQuery=t,this.bufferManager=new eoe(this.device),this.tensorMap=new Ud(this,Ss()),this.supportTimeQuery&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:2})),X().getBool("WEBGPU_USE_PROFILE_TOOL")&&(this.dummyCanvas=document.createElement("canvas"),this.dummyCanvas.width=1,this.dummyCanvas.height=1,this.dummyContext=this.dummyCanvas.getContext("webgpu"),this.dummyContext.configure({device:e,format:"bgra8unorm"}),document.body.appendChild(this.dummyCanvas))}nextDataId(){return B2.nextDataId++}floatPrecision(){return 32}defaultGpuBufferUsage(){return GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}flushDisposalQueue(){this.tensorDisposalQueue.forEach(e=>{this.maybeReleaseBuffer(e),this.tensorMap.delete(e)}),this.uniformDisposalQueue.forEach(e=>this.bufferManager.releaseBuffer(e.buffer,e.byteSize,e.usage)),this.stagingDisposalQueue.forEach(e=>this.bufferManager.releaseUploadBuffer(e.buffer,e.byteSize,e.usage)),this.tensorDisposalQueue=[],this.uniformDisposalQueue=[],this.stagingDisposalQueue=[]}disposeData(e,t=!1){if(this.tensorMap.has(e)){let n=this.tensorMap.get(e);if(n.refCount--,!t&&n.refCount>
${s.shape}`);if(r.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
${r.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let i=t.dataIdMap.get(s.dataId).id,o=t.dataIdMap.get(r.dataId).id,u=t.dataIdMap.get(a.dataId).id,l=s.shape[0],c=w.sizeFromShape(a.shape),p=t.makeOutput([l,c],s.dtype),d=t.dataIdMap.get(p.dataId).id,h=t.makeOutput([c],a.dtype),f=t.dataIdMap.get(h.dataId).id,m=t.makeOutput([3],"int32"),g=t.dataIdMap.get(m.dataId).id;ON(i,o,u,l,d,f,g);let b=t.readSync(m.dataId),y;switch(b[0]){case 0:{y=S.getSparseReshapeMultipleNegativeOneOutputDimErrorMessage(b[1],b[2]);break}case 1:{y=S.getSparseReshapeNegativeOutputDimErrorMessage(b[1],b[2]);break}case 2:y=S.getSparseReshapeEmptyTensorZeroOutputDimErrorMessage();break;case 3:{let v=Array.from(t.readSync(r.dataId)),x=Array.from(t.readSync(h.dataId));y=S.getSparseReshapeInputOutputMultipleErrorMessage(v,x);break}case 4:{let v=Array.from(t.readSync(r.dataId)),x=Array.from(t.readSync(h.dataId));y=S.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 Oce={kernelName:$l,backendName:"wasm",setupFunc:Dce,kernelFunc:Fce},PN;function zN(e){PN=e.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function MN(e,t){let{backend:n,inputs:s}=e,{data:r,indices:a,segmentIds:i}=s,o=a.shape[0],u=n.readSync(i.dataId,o-1,o)[0],c=o>0?u+1:0;if(c<0)throw new Error(S.getSparseSegmentReductionNegativeSegmentIdsErrorMessage());let p=r.shape.slice();p[0]=c;let d=n.dataIdMap.get(r.dataId).id,h=n.dataIdMap.get(a.dataId).id,f=n.dataIdMap.get(i.dataId).id,m=n.makeOutput(p,r.dtype),g=n.dataIdMap.get(m.dataId).id,b=n.makeOutput([4],"int32"),y=n.dataIdMap.get(b.dataId).id;PN(d,It[r.dtype],r.shape[0],h,f,g,y,t,0);let v=n.readSync(b.dataId),x;switch(v[0]){case 0:{x=S.getSparseSegmentReductionNegativeSegmentIdsErrorMessage();break}case 1:{x=S.getSparseSegmentReductionNonIncreasingSegmentIdsErrorMessage();break}case 2:x=S.getSparseSegmentReductionSegmentIdOutOfRangeErrorMessage(v[1],v[2]);break;case 3:x=S.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 Pce(e){return MN(e,!0)}var zce={kernelName:rp,backendName:"wasm",setupFunc:zN,kernelFunc:Pce};function Mce(e){return MN(e,!1)}var Lce={kernelName:ap,backendName:"wasm",setupFunc:zN,kernelFunc:Mce};function Bce(e){let{inputs:t,attrs:n,backend:s}=e,{x:r}=t,{numOrSizeSplits:a,axis:i}=n,o=w.parseAxisParam(i,r.shape)[0],u=S.prepareSplitSize(r,a,o),l=new Array(r.shape.length).fill(0),c=r.shape.slice();return u.map(p=>{let d=[...c];d[o]=p;let h=va({inputs:{x:r},attrs:{begin:l,size:d},backend:s});return l[o]+=p,h})}var Vce={kernelName:Mo,backendName:"wasm",kernelFunc:Bce},Wce=Xt(ii),Uce=Xt(_l),Gce=!0,Hce=gn(li,Gce),LN;function qce(e){LN=e.wasm.cwrap(hi,null,["number","number","number","number"])}function jce(e){let{backend:t,inputs:n,attrs:s}=e,{alpha:r}=s,{x:a}=n,i=t.dataIdMap.get(a.dataId).id,o=t.makeOutput(a.shape,a.dtype),u=t.dataIdMap.get(o.dataId).id;return LN(i,r,It[a.dtype],u),o}var Kce={kernelName:hi,backendName:"wasm",setupFunc:qce,kernelFunc:jce},BN;function Xce(e){BN=e.wasm.cwrap(Lo,null,["number","array","number","array","array","array","array","array","number","number"])}function Yce(e){let{backend:t,inputs:n,attrs:s}=e,{x:r}=n,{begin:a,end:i,strides:o,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=s,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=wt.sliceInfo(r.shape,a,i,o,u,l,c,p,d),k;if(m)k=yn({inputs:{x:r},backend:t,attrs:{shape:f}});else if(g||b){w.assert(r.shape.length>=1,()=>`Input must have rank at least 1, got: ${r.shape.length}`);let T=wt.computeOutShape(y,v,x),N=va({inputs:{x:r},backend:t,attrs:{begin:y,size:T}});k=yn({inputs:{x:N},backend:t,attrs:{shape:f}}),t.disposeData(N.dataId)}else{let T=t.makeOutput(h,"float32"),N=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}});`,xde=xa(i$()),wde=class extends tl{constructor(e){super();this.wasm=e,this.dataIdNextNumber=1,this.wasm.tfjs.initWithThreadsCount(HN),eg=this.wasm.tfjs.getThreadsCount(),this.dataIdMap=new Ud(this,Ss())}write(e,t,n){let s={id:this.dataIdNextNumber++};return this.move(s,e,t,n,1),s}numDataIds(){return this.dataIdMap.numDataIds()}async time(e){let t=w.now();return e(),{kernelMs:w.now()-t}}move(e,t,n,s,r){let a=this.dataIdNextNumber++;if(s==="string"){let l=t;this.dataIdMap.set(e,{id:a,stringBytes:l,shape:n,dtype:s,memoryOffset:null,refCount:r});return}let i=w.sizeFromShape(n),o=i*w.bytesPerElement(s),u=this.wasm._malloc(o);this.dataIdMap.set(e,{id:a,memoryOffset:u,shape:n,dtype:s,refCount:r}),this.wasm.tfjs.registerTensor(a,i,u),t!=null&&this.wasm.HEAPU8.set(new Uint8Array(t.buffer,t.byteOffset,o),u)}async read(e){return this.readSync(e)}readSync(e,t,n){let{memoryOffset:s,dtype:r,shape:a,stringBytes:i}=this.dataIdMap.get(e);if(r==="string")return(t==null||t===0)&&(n==null||n>=i.length)?i:i.slice(t,n);t=t||0,n=n||w.sizeFromShape(a);let o=w.bytesPerElement(r),u=this.wasm.HEAPU8.slice(s+t*o,s+n*o);return Sde(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-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. */