face-api/dist/face-api.js

5010 lines
1.3 MiB
JavaScript
Raw Normal View History

2022-08-22 19:17:39 +02:00
/*
Face-API
homepage: <https://github.com/vladmandic/face-api>
author: <https://github.com/vladmandic>'
2021-09-08 19:51:28 +02:00
*/
2024-01-17 22:44:24 +01:00
"use strict";var faceapi=(()=>{var dv=Object.defineProperty;var NR=Object.getOwnPropertyDescriptor;var _R=Object.getOwnPropertyNames;var ER=Object.prototype.hasOwnProperty;var AR=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(t,n)=>(typeof require!="undefined"?require:t)[n]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var $h=(e,t)=>{for(var n in t)dv(e,n,{get:t[n],enumerable:!0})},DR=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of _R(t))!ER.call(e,s)&&s!==n&&dv(e,s,{get:()=>t[s],enumerable:!(r=NR(t,s))||r.enumerable});return e};var $R=e=>DR(dv({},"__esModule",{value:!0}),e);var Jge={};$h(Jge,{AgeGenderNet:()=>Kp,BoundingBox:()=>iu,Box:()=>on,ComposableTask:()=>Ir,ComputeAllFaceDescriptorsTask:()=>Hs,ComputeFaceDescriptorsTaskBase:()=>Jp,ComputeSingleFaceDescriptorTask:()=>js,DetectAllFaceLandmarksTask:()=>eh,DetectAllFacesTask:()=>Hl,DetectFaceLandmarksTaskBase:()=>Qp,DetectFacesTaskBase:()=>nh,DetectSingleFaceLandmarksTask:()=>th,DetectSingleFaceTask:()=>rh,Dimensions:()=>Un,FACE_EXPRESSION_LABELS:()=>H0,FaceDetection:()=>Tt,FaceDetectionNet:()=>J0,FaceExpressionNet:()=>qp,FaceExpressions:()=>Vs,FaceLandmark68Net:()=>yu,FaceLandmark68TinyNet:()=>Xp,FaceLandmarkNet:()=>X0,FaceLandmarks:()=>or,FaceLandmarks5:()=>D0,FaceLandmarks68:()=>lu,FaceMatch:()=>Al,FaceMatcher:()=>tS,FaceRecognitionNet:()=>vu,Gender:()=>wg,LabeledBox:()=>Dl,LabeledFaceDescriptors:()=>Ba,NetInput:()=>xs,NeuralNetwork:()=>cn,ObjectDetection:()=>uu,Point:()=>Ue,PredictedBox:()=>$0,Rect:()=>cu,SsdMobilenetv1:()=>za,SsdMobilenetv1Options:()=>wr,TinyFaceDetector:()=>ku,TinyFaceDetectorOptions:()=>Zp,TinyYolov2:()=>wu,TinyYolov2Options:()=>Qr,allFaces:()=>Xge,allFacesSsdMobilenetv1:()=>jF,allFacesTinyYolov2:()=>Kge,awaitMediaLoaded:()=>B0,bufferToImage:()=>z0,computeFaceDescriptor:()=>Rge,createCanvas:()=>mu,createCanvasFromMedia:()=>Up,createFaceDetectionNet:()=>Nge,createFaceRecognitionNet:()=>gge,createSsdMobilenetv1:()=>DF,createTinyFaceDetector:()=>Yge,createTinyYolov2:()=>Age,detectAllFaces:()=>Dg,detectFaceLandmarks:()=>GF,detectFaceLandmarksTiny:()=>Fge,detectLandmarks:()=>jge,detectSingleFace:()=>qge,draw:()=>q0,env:()=>nt,euclideanDistance:()=>eS,extendWithAge:()=>Cg,extendWithFaceDescriptor:()=>Sg,extendWithFaceDetection:()=>du,extendWithFaceExpressions:()=>bg,extendWithFaceLandmarks:()=>zl,extendWithGender:()=>Tg,extractFaceTensors:()=>Rl,extractFaces:()=>Fl,fetchImage:()=>nge,fetchJson:()=>U0,fetchNetWeights:()=>rge,fetchOrThrow:()=>Ws,fetchVideo:()=>sge,getContext2dOrThrow:()=>Gn,getMediaDimensions:()=>fu,imageTensorToCanvas:()=>W0,imageToSquare:()=>V0,inverseSigmoid:()=>Xme,iou:()=>N0,isMediaElement:()=>cg,isMediaLoaded:()=>Vp,isWithAge:()=>bge,isWithFaceDetection:()=>vs,isWithFaceExpressions:()=>j0,isWithFaceLandmarks:()=>bu,isWithGender:()=>yge,loadAgeGenderModel:()=>Uge,loadFaceDetectionModel:()=>Gge,loadFaceExpressionModel:()=>Vge,loadFaceLandmarkModel:()=>Bge,loadFaceLandmarkTinyModel:()=>zge,loadFaceRecognitionModel:()=>Wge,loadSsdMobilenetv1Model:()=>HF,loadTinyFaceDetectorModel:()=>Mge,loadTinyYolov2Model:()=>Lge,loadWeightMap:()=>G0,locateFaces:()=>Hge,matchDimensions:()=>age,minBbox:()=>_0,nets:()=>rt,nonMaxSuppression:()=>E0,normalize:()=>Zr,padToSquare:()=>A0,predictAgeAndGender:()=>Oge,recognizeFaceExpressions:()=>Pge,resizeResults:()=>qF,resolveInput:()=>pu,shuffleArray:()=>Kme,sigmoid:()=>Bp,ssdMobilenetv1:()=>UF,tf:()=>Oe,tinyFaceDetector:()=>Dge,tinyYolov2:()=>$ge,toNetInput:()=>vt,utils:()=>T0,validateConfig:()=>Q0,version:()=>Zge});var Oe={};$h(Oe,{Abs:()=>cc,Acos:()=>Eo,Acosh:()=>Ao,AdadeltaOptimizer:()=>Yw,AdagradOptimizer:()=>Zw,AdamOptimizer:()=>Jw,AdamaxOptimizer:()=>Qw,Add:()=>Sa,AddN:()=>Do,All:()=>lc,Any:()=>dc,ArgMax:()=>pc,ArgMin:()=>hc,Asin:()=>$o,Asinh:()=>Fo,Atan:()=>Ro,Atan2:()=>Oo,Atanh:()=>Po,AvgPool:()=>Mo,AvgPool3D:()=>fc,AvgPool3DGrad:()=>zd,AvgPoolGrad:()=>Bd,BackendWasm:()=>oF,BatchMatMul:()=>Lo,BatchToSpaceND:()=>mc,Bincount:()=>gc,BitwiseAn
`),q=P=>U.writeSync(2,P+`
`));var Z=d.print||K,J=d.printErr||q;Object.assign(d,m),m=null,d.arguments&&(b=d.arguments),d.thisProgram&&(y=d.thisProgram),d.quit&&(v=d.quit);var ee=4,se=Atomics.load,te=Atomics.store,oe=Atomics.compareExchange,ne;d.wasmBinary&&(ne=d.wasmBinary);var de=d.noExitRuntime||!0;typeof WebAssembly!="object"&&Ha("no native wasm support detected");var ce,we,ve=!1,Ce;function Ae(P,j){P||Ha(j)}var qe=typeof TextDecoder!="undefined"?new TextDecoder("utf8"):void 0;function at(P,j,ue){j>>>=0;for(var Se=j+ue,Pe=j;P[Pe]&&!(Pe>=Se);)++Pe;if(Pe-j>16&&P.buffer&&qe)return qe.decode(P.buffer instanceof SharedArrayBuffer?P.slice(j,Pe):P.subarray(j,Pe));for(var $e="";j<Pe;){var me=P[j++];if(!(me&128)){$e+=String.fromCharCode(me);continue}var Ie=P[j++]&63;if((me&224)==192){$e+=String.fromCharCode((me&31)<<6|Ie);continue}var Nt=P[j++]&63;if((me&240)==224?me=(me&15)<<12|Ie<<6|Nt:me=(me&7)<<18|Ie<<12|Nt<<6|P[j++]&63,me<65536)$e+=String.fromCharCode(me);else{var cr=me-65536;$e+=String.fromCharCode(55296|cr>>10,56320|cr&1023)}}return $e}function ft(P,j){return P>>>=0,P?at(o(),P,j):""}function st(P,j,ue,Se){if(ue>>>=0,!(Se>0))return 0;for(var Pe=ue,$e=ue+Se-1,me=0;me<P.length;++me){var Ie=P.charCodeAt(me);if(Ie>=55296&&Ie<=57343){var Nt=P.charCodeAt(++me);Ie=65536+((Ie&1023)<<10)|Nt&1023}if(Ie<=127){if(ue>=$e)break;j[ue++>>>0]=Ie}else if(Ie<=2047){if(ue+1>=$e)break;j[ue++>>>0]=192|Ie>>6,j[ue++>>>0]=128|Ie&63}else if(Ie<=65535){if(ue+2>=$e)break;j[ue++>>>0]=224|Ie>>12,j[ue++>>>0]=128|Ie>>6&63,j[ue++>>>0]=128|Ie&63}else{if(ue+3>=$e)break;j[ue++>>>0]=240|Ie>>18,j[ue++>>>0]=128|Ie>>12&63,j[ue++>>>0]=128|Ie>>6&63,j[ue++>>>0]=128|Ie&63}}return j[ue>>>0]=0,ue-Pe}function Je(P,j,ue){return st(P,o(),j,ue)}var Le,ot,jn,$t,kr,ln,Sr,wn,qn;N&&(Le=d.buffer);function xt(P){Le=P,d.HEAP8=ot=new Int8Array(P),d.HEAP16=$t=new Int16Array(P),d.HEAP32=ln=new Int32Array(P),d.HEAPU8=jn=new Uint8Array(P),d.HEAPU16=kr=new Uint16Array(P),d.HEAPU32=Sr=new Uint32Array(P),d.HEAPF32=wn=new Float32Array(P),d.HEAPF64=qn=new Float64Array(P)}var Fn=d.INITIAL_MEMORY||16777216;if(N)ce=d.wasmMemory,Le=d.buffer;else if(d.wasmMemory)ce=d.wasmMemory;else if(ce=new WebAssembly.Memory({initial:Fn/65536,maximum:65536,shared:!0}),!(ce.buffer instanceof SharedArrayBuffer))throw J("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),S&&J("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");ce&&(Le=ce.buffer),Fn=Le.byteLength,xt(Le);var gn,es=[],qs=[],Cr=[],jl=!1;function Tr(){return de}function Ks(){if(d.preRun)for(typeof d.preRun=="function"&&(d.preRun=[d.preRun]);d.preRun.length;)$g(d.preRun.shift());Kl(es)}function Jt(){jl=!0,!N&&Kl(qs)}function sh(){if(!N){if(d.postRun)for(typeof d.postRun=="function"&&(d.postRun=[d.postRun]);d.postRun.length;)nS(d.postRun.shift());Kl(Cr)}}function $g(P){es.unshift(P)}function Fg(P){qs.unshift(P)}function nS(P){Cr.unshift(P)}var Xs=0,_u=null,ws=null;function Rg(P){Xs++,d.monitorRunDependencies&&d.monitorRunDependencies(Xs)}function ah(P){if(Xs--,d.monitorRunDependencies&&d.monitorRunDependencies(Xs),Xs==0&&(_u!==null&&(clearInterval(_u),_u=null),ws)){var j=ws;ws=null,j()}}function Ha(P){d.onAbort&&d.onAbort(P),P="Aborted("+P+")",J(P),ve=!0,Ce=1,P+=". Build with -sASSERTIONS for more info.";var j=new WebAssembly.RuntimeError(P);throw f(j),j}var Pg="data:application/octet-stream;base64,";function oh(P){return P.startsWith(Pg)}function Eu(P){return P.startsWith("file://")}var bn;bn="tfjs-backend-wasm-threaded-simd.wasm",oh(bn)||(bn=$(bn));function ih(P){try{if(P==bn&&ne)return new Uint8Array(ne);if(R)return R(P);throw"both async and sync fetching of the wasm failed"}catch(j){Ha(j)}}function Og(){if(!ne&&(x||k)){if(typeof fetch=="function"&&!Eu(bn))return fetch(bn,{credentials:"same-origin"}).then(function(P){if(!P.ok)throw"failed to load wasm binary file at '"+bn+"'";return P.arrayBuffer()}).catch(function(){return ih(
");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.onunhandledrejection=e=>{throw e.reason??e};self.startWorker=instance=>{Module=instance;postMessage({"cmd":"loaded"})};self.onmessage=e=>{try{if(e.data.cmd==="load"){Module["wasmModule"]=e.data.wasmModule;for(const handler of e.data.handlers){Module[handler]=function(){postMessage({cmd:"callHandler",handler:handler,args:[...arguments]})}}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)}else if(e.data.cmd==="run"){Module["__emscripten_thread_init"](e.data.pthread_ptr,0,0,1);Module["establishStackSpace"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInitTLS();if(!initializedJS){pendingNotifiedProxyingQueues.forEach(queue=>{Module["executeNotifiedProxyingQueue"](queue)});pendingNotifiedProxyingQueues=[];initializedJS=true}try{Module["invokeEntryPoint"](e.data.start_routine,e.data.arg)}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==="processProxyingQueue"){if(initializedJS){Module["executeNotifiedProxyingQueue"](e.data.queue)}else{pendingNotifiedProxyingQueues.push(e.data.queue)}}else if(e.data.cmd){err("worker.js received unknown command "+e.data.cmd);err(e.data)}}catch(ex){if(Module["__emscripten_thread_crashed"]){Module["__emscripten_thread_crashed"]()}throw ex}};`}}),eP=Vt({"node_modules/.pnpm/@tensorflow+tfjs-backend-wasm@4.16.0_@tensorflow+tfjs-core@4.16.0/node_modules/@tensorflow/tfjs-backend-wasm/wasm-out/tfjs-backend-wasm.js"(e,t){"use strict";var n=(()=>{var r=typeof document!="undefined"&&document.currentScript?document.currentScript.src:void 0;return typeof __filename!="undefined"&&(r=r||__filename),function(s){s=s||{};var a=typeof s!="undefined"?s:{},o,i;a.ready=new Promise(function(Y,ie){o=Y,i=ie});var u;typeof process!="undefined"&&process.listeners&&(u={uncaughtException:process.listeners("uncaughtException"),unhandledRejection:process.listeners("unhandledRejection")});var c=Object.assign({},a),l=[],p="./this.program",d=(Y,ie)=>{throw ie},h=typeof window=="object",f=typeof importScripts=="function",g=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string",m="";function b(Y){return a.locateFile?a.locateFile(Y,m):m+Y}var y,v,x,k;function S(Y){if(Y instanceof _u)return;F("exiting due to exception: "+Y)}if(g){var N=Ax(),E=nC();f?m=E.dirname(m)+"/":m=__dirname+"/",y=(Y,ie)=>(Y=Ks(Y)?new URL(Y):E.normalize(Y),N.readFileSync(Y,ie?void 0:"utf8")),x=Y=>{var ie=y(Y,!0);return ie.buffer||(ie=new Uint8Array(ie)),ie},v=(Y,ie,ke)=>{Y=Ks(Y)?new URL(Y):E.normalize(Y),N.readFile(Y,function(Ye,wt){Ye?ke(Ye):ie(wt.buffer)})},process.argv.length>1&&(p=process.argv[1].replace(/\\/g,"/")),l=process.argv.slice(2),process.on("uncaughtException",function(Y){if(!(Y instanceof _u))throw Y}),process.on("unhandledRejection",function(Y){throw Y}),d=(Y,ie)=>{if(jn())throw process.exitCode=Y,ie;S(ie),process.exit(Y)},a.inspect=function(){return"[Emscripten Module object]"}}else(h||f)&&(f?m=self.location.href:typeof document!="undefined"&&document.currentScript&&(m=document.currentScript.src),r&&(m=r),m.indexOf("blob:")!==0?m=m.substr(0,m.replace(/[?#].*/,"").lastIndexOf("/")+1):m="",y
`)),u.join(`
`)}function jP(e,t,n,r){let s=ut(t),a=r[r.length-1],o=new Array(a).fill(0),i=t.length,u=n==="complex64"?od(e):e;if(i>1)for(let c=0;c<s/a;c++){let l=c*a;for(let p=0;p<a;p++)o[p]=Math.max(o[p],ad(u[l+p],0,n).length)}return o}function ad(e,t,n){let r;return Array.isArray(e)?r=`${parseFloat(e[0].toFixed(hv))} + ${parseFloat(e[1].toFixed(hv))}j`:ra(e)?r=`'${e}'`:n==="bool"?r=IC(e):r=parseFloat(e.toFixed(hv)).toString(),md(r,t)}function IC(e){return e===0?"false":"true"}function Kh(e,t,n,r,s,a=!0){let o=n==="complex64"?2:1,i=t[0],u=t.length;if(u===0){if(n==="complex64"){let g=od(e);return[ad(g[0],0,n)]}return n==="bool"?[IC(e[0])]:[e[0].toString()]}if(u===1){if(i>bS){let m=ed*o,b=Array.from(e.slice(0,m)),y=Array.from(e.slice((i-ed)*o,i*o));return n==="complex64"&&(b=od(b),y=od(y)),["["+b.map((v,x)=>ad(v,s[x],n)).join(", ")+", ..., "+y.map((v,x)=>ad(v,s[i-ed+x],n)).join(", ")+"]"]}return["["+(n==="complex64"?od(e):Array.from(e)).map((m,b)=>ad(m,s[b],n)).join(", ")+"]"]}let c=t.slice(1),l=r.slice(1),p=r[0]*o,d=[];if(i>bS){for(let g=0;g<ed;g++){let m=g*p,b=m+p;d.push(...Kh(e.slice(m,b),c,n,l,s,!1))}d.push("...");for(let g=i-ed;g<i;g++){let m=g*p,b=m+p;d.push(...Kh(e.slice(m,b),c,n,l,s,g===i-1))}}else for(let g=0;g<i;g++){let m=g*p,b=m+p;d.push(...Kh(e.slice(m,b),c,n,l,s,g===i-1))}let h=u===2?",":"";d[0]="["+(i>0?d[0]+h:"");for(let g=1;g<d.length-1;g++)d[g]=" "+d[g]+h;let f=`,
`;for(let g=2;g<u;g++)f+=`
`;return d[d.length-1]=" "+d[d.length-1]+"]"+(a?"":f),d}function od(e){let t=[];for(let n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}var zt=class{constructor(e,t,n){if(this.dtype=t,this.shape=e.slice(),this.size=ut(e),n!=null){let r=n.length;A(r===this.size,()=>`Length of values '${r}' 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||Dx(t,this.size),this.strides=uc(e)}set(e,...t){t.length===0&&(t=[0]),A(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 r of e){if(r<0||r>=this.shape[t]){let s=`Requested out of range element at ${e}. Buffer shape=${this.shape}`;throw new Error(s)}t++}let n=e[e.length-1];for(let r=0;r<e.length-1;++r)n+=this.strides[r]*e[r];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 zr().makeTensor(this.values,this.shape,this.dtype)}},zr=null,Mu=null,qP=null;function KP(e){zr=e}function XP(e){Mu=e}function YP(e){qP=e}var Ne=class{constructor(e,t,n,r){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=ut(e),this.strides=uc(e),this.dataId=n,this.id=r,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){let e=await this.data();return Mu.buffer(this.shape,this.dtype,e)}bufferSync(){return Mu.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return Wu(this.shape,e,this.dtype==="complex64")}arraySync(){return Wu(this.shape,this.dataSync(),this.dtype==="complex64")}async data(){this.throwIfDisposed();let e=zr().read(this.dataId);if(this.dtype==="string"){let t=await e;try{return t.map(n=>cf(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(),zr().readToGPU(this.dataId,e)}dataSync(){this.throwIfDisposed();let e=zr().readSync(this.dataId);if(this.dtype==="string")try{return e.map(t=>cf(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 zr().read(this.dataId);return this.dtype==="string"?e:new Uint8Array(e.buffer)}dispose(){this.isDisposed||(this.kerasMask&&this.kerasMask.dispose(),zr().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(e=!1){return Mu.print(this,e)}clone(){return this.throwIfDisposed(),Mu.clone(this)}toString(e=!1){let t=this.dataSync();return HP(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Mu.cast(this,e)}variable(e=!0,t,n){return this.throwIfDisposed(),zr().makeVariable(this,e,t,n)}};Object.defineProperty(Ne,Symbol.hasInstance,{value:e=>!!e&&e.data!=null&&e.dataSync!=null&&e.throwIfDisposed!=null});function Q(){return Rx("Tensor",()=>Ne)}Q();var pa=class extends Ne{constructor(e,t,n,r){super(e.shape,e.dtype,e.dataId,r),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(!$s(e.shape,this.shape))throw new Error(`shape of the new value (${e.shape}) and previous value (${this.shape}) must match`);zr().disposeTensor(this),this.dataId=e.dataId,zr().incRef(this,null)}dispose(){zr().disposeVariabl
with dtype ${a.dtype}. `)}),n.length===1)return us(n[0]);let r=n,s={axis:t};return M.runKernel(yc,r,s)}var tt=B({concat_:N3});function _3(e,t,n=!1,r=!1){let s=_(e,"a","matMul"),a=_(t,"b","matMul");[s,a]=At(s,a);let o={a:s,b:a},i={transposeA:n,transposeB:r};return M.runKernel(Lo,o,i)}var Fe=B({matMul_:_3});function E3(e){let n={x:_(e,"x","sigmoid","float32")};return M.runKernel(Li,n)}var pr=B({sigmoid_:E3});function A3(e,t,n){let r=_(e,"x","slice","string_or_numeric");if(r.rank===0)throw new Error("Slicing scalar is not possible");let s={x:r},a={begin:t,size:n};return M.runKernel(el,s,a)}var Ve=B({slice_:A3});function D3(e){let n={x:_(e,"x","tanh","float32")};return M.runKernel(ji,n)}var bo=B({tanh_:D3});function $3(e,t,n,r,s,a){let o=_(e,"forgetBias","basicLSTMCell"),i=_(t,"lstmKernel","basicLSTMCell"),u=_(n,"lstmBias","basicLSTMCell"),c=_(r,"data","basicLSTMCell"),l=_(s,"c","basicLSTMCell"),p=_(a,"h","basicLSTMCell"),d=tt([c,p],1),h=Fe(d,i),f=X(h,u),g=f.shape[0],m=f.shape[1]/4,b=[g,m],y=Ve(f,[0,0],b),v=Ve(f,[0,m],b),x=Ve(f,[0,m*2],b),k=Ve(f,[0,m*3],b),S=X(z(pr(y),bo(v)),z(l,pr(X(o,x)))),N=z(bo(S),pr(k));return[S,N]}var KC=B({basicLSTMCell_:$3});function F3(e,t,n){let r=_(e,"x","batchToSpaceND"),s=t.reduce((i,u)=>i*u);A(r.rank>=1+t.length,()=>`input rank is ${r.rank} but should be > than blockShape.length ${t.length}`),A(n.length===t.length,()=>`crops.length is ${n.length} but should be equal to blockShape.length ${t.length}`),A(r.shape[0]%s===0,()=>`input tensor batch is ${r.shape[0]} but is not divisible by the product of the elements of blockShape ${t.join(" * ")} === ${s}`);let a={x:r},o={blockShape:t,crops:n};return M.runKernel(mc,a,o)}var pp=B({batchToSpaceND_:F3});function R3(e){let t;return e.rank===0||e.rank===1?t=W(e,[1,1,1,e.size]):e.rank===2?t=W(e,[1,1,e.shape[0],e.shape[1]]):e.rank===3?t=W(e,[1,e.shape[0],e.shape[1],e.shape[2]]):t=e,t}function P3(e,t,n,r,s,a){a==null&&(a=.001);let o=_(e,"x","batchNorm"),i=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),c;s!=null&&(c=_(s,"scale","batchNorm"));let l;r!=null&&(l=_(r,"offset","batchNorm")),A(i.rank===u.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),A(l==null||i.rank===l.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),A(c==null||i.rank===c.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let d={x:R3(o),scale:c,offset:l,mean:i,variance:u},h={varianceEpsilon:a},f=M.runKernel(ni,d,h);return W(f,o.shape)}var Ea=B({batchNorm_:P3});function O3(e,t,n,r,s,a){let o=_(e,"x","batchNorm"),i=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),c;s!=null&&(c=_(s,"scale","batchNorm"));let l;return r!=null&&(l=_(r,"offset","batchNorm")),A(o.rank===2,()=>`Error in batchNorm2D: x must be rank 2 but got rank ${o.rank}.`),A(i.rank===2||i.rank===1,()=>`Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank ${i.rank}.`),A(u.rank===2||u.rank===1,()=>`Error in batchNorm2D: variance must be rank 2 or rank 1 but got rank ${u.rank}.`),c!=null&&A(c.rank===2||c.rank===1,()=>`Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank ${c.rank}.`),l!=null&&A(l.rank===2||l.rank===1,()=>`Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank ${l.rank}.`),Ea(o,i,u,l,c,a)}var ew=B({batchNorm2d_:O3});function M3(e,t,n,r,s,a){let o=_(e,"x","batchNorm"),i=_(t,"mean","batchNorm"),u=_(n,"variance","batchNorm"),c;s!=null&&(c=_(s,"scale","batchNorm"));let l;return r!=null&&(l=_(r,"offset","batchNorm")),A(o.rank===3,()=>`Error in batchNorm3D: x must be rank 3 but got rank ${o.rank}.`),A(i.rank===3||i.rank===1,()=>`Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank ${i.rank}.`),A(u.rank===3||u.rank===1,()=>`Error in batchNorm3D: variance must be rank 3 or rank 1 but got rank ${u.rank}.`),c!=null&&A(c.rank===3||c.rank===1,()=>`Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank ${c.rank}.`),l!=null&&A(l.rank===3||l.rank===1,()=>`Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank ${l.rank}.`),Ea(o,i,u,
${s} and ${t} for depthToSpace with input shape
${r.shape}`),A(a*t>=0,()=>`Negative dimension size caused by overflow when multiplying
${a} and ${t} for depthToSpace with input shape
${r.shape}`),A(o%(t*t)===0,()=>`Dimension size must be evenly divisible by ${t*t} but is ${o} for depthToSpace with input shape ${r.shape}`);let i={x:r},u={blockSize:t,dataFormat:n};return M.runKernel(kc,i,u)}var pw=B({depthToSpace_:iM});function uM(e,t,n,r,s="NHWC",a=[1,1],o){let i=_(e,"x","depthwiseConv2d","float32"),u=_(t,"filter","depthwiseConv2d","float32"),c=i,l=!1;i.rank===3&&(l=!0,c=W(i,[1,i.shape[0],i.shape[1],i.shape[2]])),A(c.rank===4,()=>`Error in depthwiseConv2d: input must be rank 4, but got rank ${c.rank}.`),A(u.rank===4,()=>`Error in depthwiseConv2d: filter must be rank 4, but got rank ${u.rank}.`);let p=s==="NHWC"?c.shape[3]:c.shape[1];A(p===u.shape[2],()=>`Error in depthwiseConv2d: number of input channels (${p}) must match the inChannels dimension in filter ${u.shape[2]}.`),_n("depthwiseConv2d",r,o);let d={x:c,filter:u},h={strides:n,pad:r,dataFormat:s,dilations:a,dimRoundingMode:o},f=M.runKernel(qo,d,h);return l?W(f,[f.shape[1],f.shape[2],f.shape[3]]):f}var Aa=B({depthwiseConv2d_:uM});function cM(e){let n={x:_(e,"x","diag")};return M.runKernel(Gd,n)}var JC=B({diag_:cM});function lM(e,t,n,r,s=[1,1],a="NHWC"){let o=_(e,"x","dilation2d"),i=_(t,"filter","dilation2d");A(o.rank===3||o.rank===4,()=>`Error in dilation2d: input must be rank 3 or 4, but got rank ${o.rank}.`),A(i.rank===3,()=>`Error in dilation2d: filter must be rank 3, but got rank ${i.rank}.`),A(a==="NHWC",()=>`Error in dilation2d: Only NHWC is currently supported, but got dataFormat of ${a}`);let u=o,c=!1;o.rank===3&&(u=W(o,[1,o.shape[0],o.shape[1],o.shape[2]]),c=!0),A(u.shape[3]===i.shape[2],()=>`Error in dilation2d: input and filter must have the same depth: ${u.shape[3]} vs ${i.shape[2]}`);let l={x:u,filter:i},p={strides:n,pad:r,dilations:s},d=M.runKernel(Ko,l,p);return c?W(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var hw=B({dilation2d_:lM}),dl={};Ee(dl,{assertAndGetBroadcastShape:()=>dt,getBroadcastDims:()=>QC,getReductionAxes:()=>Wt});function QC(e,t){let n=e.length,r=[];for(let s=0;s<n;s++){let a=n-1-s,o=e[a]||1;(t[t.length-1-s]||1)>1&&o===1&&r.unshift(a)}return r}function Wt(e,t){let n=[];for(let r=0;r<t.length;r++){let s=e[e.length-r-1],a=t.length-r-1,o=t[a];(s==null||s===1&&o>1)&&n.unshift(a)}return n}function dt(e,t){let n=Math.max(e.length,t.length),r=new Array(n);for(let s=0;s<n;s++){let a=e[e.length-s-1];a==null&&(a=1);let o=t[t.length-s-1];if(o==null&&(o=1),a===1)r[n-s-1]=o;else if(o===1)r[n-s-1]=a;else if(a!==o){let i=`Operands could not be broadcast together with shapes ${e} and ${t}.`;throw Error(i)}else r[n-s-1]=a}return r}function dM(e,t){let n=_(e,"a","equal","string_or_numeric"),r=_(t,"b","equal","string_or_numeric");[n,r]=At(n,r),dt(n.shape,r.shape);let s={a:n,b:r};return M.runKernel(Cc,s)}var tr=B({equal_:dM});function pM(e,t,n){let r=_(t,"a","where"),s=_(n,"b","where"),a=_(e,"condition","where","bool"),o=dt(dt(a.shape,r.shape),s.shape),i=ao(a,o),u=ao(r,o),c=ao(s,o),l={condition:i,t:u,e:c};return M.runKernel(Qc,l)}var nn=B({where_:pM});function hM(e){let n={x:_(e,"x","zerosLike")};return M.runKernel(cl,n)}var je=B({zerosLike_:hM});function fM(e,t){let n=_(e,"a","div"),r=_(t,"b","div");[n,r]=At(n,r);let s=fe(n,r),a=je(s),o=tr(r,a);return nn(o,a,s)}var fw=B({divNoNan_:fM});function mM(e,t){let n=_(e,"t1","dot"),r=_(t,"t2","dot");A((n.rank===1||n.rank===2)&&(r.rank===1||r.rank===2),()=>`Error in dot: inputs must all be rank 1 or 2, but got ranks ${n.rank} and ${r.rank}.`);let s=n.rank===1?n.size:n.shape[1],a=r.rank===1?r.size:r.shape[0];if(A(s===a,()=>`Error in dot: inner dimensions of inputs must match, but got ${s} and ${a}.`),n.rank===1&&r.rank===1){let o=W(n,[1,-1]),i=W(r,[-1,1]),u=Fe(o,i);return W(u,[])}else if(n.rank===1&&r.rank===2){let o=W(n,[1,-1]),i=W(r,[r.shape[0],r.shape[1]]),u=Fe(o,i);return W(u,[u.size])}else if(n.rank===2&&r.rank===1){let o=W(r,[-1,1]),i=Fe(n,o);return W(i,[i.size])}else{let o=W(r,[r.shape[0],r.shape[1]]);return Fe(n,o)}}var mw=B({dot_:mM});function gM(e,...t){let n=t.map((s,a)=>_(s,`tensors${a}`,"einsum")),r={equation:e};return M.runKernel(Vf,n,r)}var Qa=B({einsum_:gM});function bM(e
rank ${a.rank}.`),A(Hu(t),()=>`Error in localResponseNormalization: depthRadius must be an integer but got depthRadius ${t}.`);let o=a,i=!1;a.rank===3&&(i=!0,o=W(a,[1,a.shape[0],a.shape[1],a.shape[2]]));let u={x:o},c={depthRadius:t,bias:n,alpha:r,beta:s},l=M.runKernel(di,u,c);return i?W(l,[l.shape[1],l.shape[2],l.shape[3]]):l}var Sw=B({localResponseNormalization_:jM});function qM(e){let n={x:_(e,"x","log","float32")};return M.runKernel(ci,n)}var nr=B({log_:qM});function KM(e){let n={x:_(e,"x","log1p")};return M.runKernel(li,n)}var gp=B({log1p_:KM});function XM(e){return A(la(e),()=>"The f passed in grad(f) must be a function"),(t,n)=>{let r=_(t,"x","tf.grad","string_or_numeric"),s=n!=null?_(n,"dy","tf.grad"):null;return M.tidy(()=>{let{value:a,grads:o}=M.gradients(()=>e(r),[r],s);return s!=null&&Nn(a.shape,s.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),om(o),o[0]})}}function YM(e){return A(la(e),()=>"The f passed in grads(f) must be a function"),(t,n)=>{A(Array.isArray(t),()=>"The args passed in grads(f)(args) must be an array of `Tensor`s or `TensorLike`s");let r=kd(t,"args","tf.grads","string_or_numeric"),s=n!=null?_(n,"dy","tf.grads"):null;return M.tidy(()=>{let{value:a,grads:o}=M.gradients(()=>e(...r),r,s);return s!=null&&Nn(a.shape,s.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),om(o),o})}}function ZM(e){return A(la(e),()=>"The f passed in valueAndGrad(f) must be a function"),(t,n)=>{A(t instanceof Ne,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"),A(n==null||n instanceof Ne,()=>"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");let{grads:r,value:s}=M.gradients(()=>e(t),[t],n);return om(r),{grad:r[0],value:s}}}function JM(e){return A(la(e),()=>"The f passed in valueAndGrads(f) must be a function"),(t,n)=>{A(Array.isArray(t)&&t.every(s=>s instanceof Ne),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"),A(n==null||n instanceof Ne,()=>"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");let r=M.gradients(()=>e(...t),t,n);return n!=null&&Nn(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),om(r.grads),r}}function oT(e,t){A(la(e),()=>"The f passed in variableGrads(f) must be a function"),A(t==null||Array.isArray(t)&&t.every(c=>c instanceof pa),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let n=t!=null;if(!n){t=[];for(let c in M.registeredVariables)t.push(M.registeredVariables[c])}let r=n?t.filter(c=>!c.trainable):null,s=t.length;t=t.filter(c=>c.trainable),A(t.length>0,()=>`variableGrads() expects at least one of the input variables to be trainable, but none of the ${s} variables is trainable.`);let a=!0,{value:o,grads:i}=M.gradients(e,t,null,a);A(i.some(c=>c!=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()."),A(o.rank===0,()=>`The f passed in variableGrads(f) must return a scalar, but it returned a rank-${o.rank} tensor`);let u={};return t.forEach((c,l)=>{i[l]!=null&&(u[c.name]=i[l])}),r!=null&&r.forEach(c=>u[c.name]=null),{value:o,grads:u}}function ps(e){return M.customGrad(e)}function om(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 QM(e){let n={x:_(e,"x","neg")};return M.runKernel(zc,n)}var bt=B({neg_:QM});function eL(e){let n={x:_(e,"x","softplus")};return M.runKernel(Bi,n)}var qi=B({softplus_:eL});function tL(e){let t=_(e,"x","logSigmoid");return ps(r=>({value:bt(qi(bt(r))),gradFunc:o=>z(o,pr(bt(r)))}))(t)}var Cw=B({logSigmoid_:tL});function nL(e,t){let n=_(e,"a","sub"),r=_(t,"b","sub");[n,r]=At(n,r);let s={a:n,b:r};return M.runKernel(Gi,s)}var le=B({sub_:nL});function rL(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 ps((s,a)=>{let i=hr(s,t,!0),u=le(s,i),c=le(ae(u,"float32"),nr(ge(fn(u),t,!0)));return a([c]),{value:c,gradFunc:(p,d)=>{let[h]=d,f=!0,g=fn(h);return le(p,z(ge(p,t,f),g))}}})(n)}var im=B({logSoftmax_:rL});function sL(e,t=null,n=!1){let r=_(e,"x","logSumExp"),s=Fr(t,r.shape),a=hr(r,s,!0),o=le(r,a),i=fn(o),u=ge(i,s),c=nr(u),l=X(W(a,c.shape),c);if(n){let p=yo(l.shape,s);return W(l,p)}return l}var bp=B({logSumExp_:sL});function aL(e,t){let n=_(e,"a","logicalAnd","bool"),r=_(t,"b","logicalAnd","bool");dt(n.shape,r.shape);let s={a:n,b:r};return M.runKernel(Rc,s)}var $r=B({logicalAnd_:aL});function oL(e){let n={x:_(e,"x","logicalNot","bool")};return M.runKernel(Pc,n)}var yp=B({logicalNot_:oL});function iL(e,t){let n=_(e,"a","logicalOr","bool"),r=_(t,"b","logicalOr","bool");dt(n.shape,r.shape);let s={a:n,b:r};return M.runKernel(Oc,s)}var um=B({logicalOr_:iL});function uL(e,t){let n=_(e,"a","logicalXor","bool"),r=_(t,"b","logicalXor","bool");return dt(n.shape,r.shape),$r(um(e,t),yp($r(e,t)))}var Tw=B({logicalXor_:uL}),Rh=2147483648;function cL(e,t,n="left"){let r=_(e,"sortedSequence","searchSorted"),s=_(t,"values","searchSorted"),a=r.shape[r.shape.length-1],o=s.shape[s.shape.length-1],i=W(r,[-1,a]),u=W(s,[-1,o]);if(i.rank<2)throw new Error("Sorted input argument must be at least 2-dimensional");if(i.shape[0]!==u.shape[0])throw new Error("Leading dimension of 'sortedSequence' and 'values' must match.");if(ut(u.shape)>=Rh)throw new Error(`values tensor size must less than ${Rh}`);if(i.shape[1]>=Rh)throw new Error(`trailing dim_size must less than ${Rh} for int32 output type, was ${i.shape[1]}`);let c={sortedSequence:i,values:u},l={side:n};return M.runKernel(Jc,c,l)}var cm=B({searchSorted_:cL});function iT(e,t){return cm(e,t,"left")}function lL(e,t,n,r,s){let a=_(e,"x","maxPool"),o=1,i=a,u=!1;a.rank===3&&(u=!0,i=W(a,[1,a.shape[0],a.shape[1],a.shape[2]])),A(i.rank===4,()=>`Error in maxPool: input must be rank 4 but got rank ${i.rank}.`),A(fs(n,o),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${n} and dilations '${o}'`),_n("maxPool",r,s);let c={x:i},l={filterSize:t,strides:n,pad:r,dimRoundingMode:s},p=M.runKernel(fi,c,l);return u?W(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var Rt=B({maxPool_:lL});function dL(e,t=[1,1,1],n,r,s,a="NDHWC"){let o=_(e,"x","maxPool3d"),i=o,u=!1;o.rank===4&&(u=!0,i=W(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]])),A(i.rank===5,()=>`Error in maxPool3d: x must be rank 5 but got rank ${i.rank}.`),A(a==="NDHWC",()=>`Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of ${a}`),_n("maxPool3d",r,s);let c={x:i},l={filterSize:t,strides:n,pad:r,dimRoundingMode:s,dataFormat:a},p=M.runKernel(Lc,c,l);return u?W(p,[p.shape[1],p.shape[2],p.shape[3],p.shape[4]]):p}var Nw=B({maxPool3d_:dL});function pL(e,t,n,r,s=!1){let o={x:_(e,"x","maxPoolWithArgmax")},i={filterSize:t,strides:n,pad:r,includeBatchInIndex:s},u=M.runKernel(Kd,o,i);return{result:u[0],indexes:u[1]}}var uT=B({maxPoolWithArgmax_:pL});function hL(e,t){let n=_(e,"a","maximum"),r=_(t,"b","maximum");[n,r]=At(n,r),n.dtype==="bool"&&(n=ae(n,"int32"),r=ae(r,"int32")),dt(n.shape,r.shape);let s={a:n,b:r};return M.runKernel(hi,s)}var ms=B({maximum_:hL});function fL(e,t=null,n=!1){let s={x:_(e,"x","mean")},a={axis:t,keepDims:n};return M.runKernel(mi,s,a)}var _t=B({mean_:fL});function kt(e,t="float32"){
Actual: ${s}.
Expected: ${a}.`);for(let o=0;o<a.length;++o){let i=s[o],u=a[o];if(!n(i,u))throw new Error(`Arrays differ: actual[${o}] = ${i}, expected[${o}] = ${u}.
Actual: ${s}.
Expected: ${a}.`)}typeof expect!="undefined"&&expect().nothing()}function VL(e,t){e().then(()=>t.fail(),()=>t()),typeof expect!="undefined"&&expect().nothing()}function UL(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return ra(e)||ra(e[0])||ra(t)||ra(t[0])?Wv(e,n,(r,s)=>r==s):Wv(e,t,(r,s)=>Rw(r,s,0))}function GL(e,t,n){if(n==null&&(n=Fw()),!Rw(e,t,n))throw new Error(`Numbers differ: actual === ${e}, expected === ${t}`);typeof expect!="undefined"&&expect().nothing()}function Rw(e,t,n){return!isFinite(e)&&!isFinite(t)?!0:!(isNaN(e)||isNaN(t)||Math.abs(e-t)>n)}function HL(e,t,n){for(let r=0;r<e.length;r++)if(e[r]<t||e[r]>n)throw new Error(`Value out of range:${e[r]} low: ${t}, high: ${n}`)}function jL(e,t){let n=new Float32Array(e),r=new Float32Array(t);if(n.length!==r.length)throw new Error(`Expected ArrayBuffer to be of length ${r.length}, but it was ${n.length}`);for(let s=0;s<r.length;s++)if(n[s]!==r[s])throw new Error(`Expected ArrayBuffer value at ${s} to be ${r[s]} but got ${n[s]} instead`)}function kT(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?kT(n):e[t]=up(n)}return e}function qL(e){let t=document.createElement("video");return"playsInline"in t&&(t.playsInline=!0),t.muted=!0,t.loop=!0,t.style.position="fixed",t.style.left="0px",t.style.top="0px",t.preload="auto",t.appendChild(e),new Promise(n=>{t.addEventListener("loadeddata",r=>n(t)),t.load()})}async function KL(e){await e.play(),"requestVideoFrameCallback"in e&&await new Promise(t=>{e.requestVideoFrameCallback(t)})}var Pw=class{constructor(e,t,n,r,s){this.mean=e,this.stdDev=t,this.dtype=n,this.nextVal=NaN,this.truncated=r,this.truncated&&(this.upper=this.mean+this.stdDev*2,this.lower=this.mean-this.stdDev*2);let a=s||Math.random();this.random=$w.alea(a.toString())}nextValue(){if(!isNaN(this.nextVal)){let r=this.nextVal;return this.nextVal=NaN,r}let e,t,n=!1;for(;!n;){let r,s,a;do r=2*this.random()-1,s=2*this.random()-1,a=r*r+s*s;while(a>=1||a===0);let o=Math.sqrt(-2*Math.log(a)/a);e=this.mean+this.stdDev*r*o,t=this.mean+this.stdDev*s*o,(!this.truncated||this.isValidTruncated(e))&&(n=!0)}return(!this.truncated||this.isValidTruncated(t))&&(this.nextVal=this.convertValue(t)),this.convertValue(e)}convertValue(e){return this.dtype==null||this.dtype==="float32"?e:Math.round(e)}isValidTruncated(e){return e<=this.upper&&e>=this.lower}},XL=class{constructor(e,t,n,r){this.alpha=e,this.beta=1/t,this.dtype=n;let s=r||Math.random();this.randu=$w.alea(s.toString()),this.randn=new Pw(0,1,n,!1,this.randu()),e<1?this.d=e+2/3:this.d=e-1/3,this.c=1/Math.sqrt(9*this.d)}nextValue(){let e,t,n,r,s,a;for(;;){do r=this.randn.nextValue(),a=1+this.c*r;while(a<=0);if(a*=a*a,e=r*r,t=1-.331*e*e,n=.5*e+this.d*(1-a+Math.log(a)),s=this.randu(),s<t||Math.log(s)<n)break}return a=1/this.beta*this.d*a,this.alpha<1&&(a*=Math.pow(this.randu(),1/this.alpha)),this.convertValue(a)}convertValue(e){return this.dtype==="float32"?e:Math.round(e)}},YL=class{constructor(e=0,t=1,n,r){if(this.canReturnFloat=()=>this.dtype==null||this.dtype==="float32",this.min=e,this.range=t-e,this.dtype=n,r==null&&(r=Math.random()),typeof r=="number"&&(r=r.toString()),!this.canReturnFloat()&&this.range<=1)throw new Error(`The difference between ${e} - ${t} <= 1 and dtype is not float`);this.random=$w.alea(r)}convertValue(e){return this.canReturnFloat()?e:Math.round(e)}nextValue(){return this.convertValue(this.min+this.range*this.random())}};function ZL(e,t,n=1,r="float32",s){if(ar(e),n==null&&(n=1),r==null&&(r="float32"),r!=="float32"&&r!=="int32")throw new Error(`Unsupported data type ${r}`);let a=new XL(t,n,r,s),o=Me(e,r);for(let i=0;i<o.values.length;i++)o.values[i]=a.nextValue();return o.toTensor()}var ST=B({randomGamma_:ZL});function JL(e,t=0,n=1,r,s){if(ar(e),r!=null&&r==="bool")throw new Error(`Unsupported data type ${r}`);let a=new Pw(t,n,r,!1,s),o=Me(e,r);for(let i=0;i<o.values.length;i++)o.values[i]=a.nextValue();return o.toTensor()}var lm=B({randomNormal_:JL});function QL(e,t,n){if(t!=null&&t==="bool")throw new Error(`Unsupported data type ${t}`);return lm(e,0,1,t,n)}va
${s.shape}`);if(a.rank!==1)throw new Error(`Values should be Tensor1D but received shape ${a.shape}`);if(o.rank!==1)throw new Error(`Dense shape should be Tensor1D but received shape ${o.shape}`);if(i.rank!==0)throw new Error(`Default value should be a scalar but received shape ${i.shape}`);let u={indices:s,values:a,denseShape:o,defaultValue:i},c=M.runKernel(Yd,u);return{outputIndices:c[0],outputValues:c[1],emptyRowIndicator:c[2],reverseIndexMap:c[3]}}var xW=B({sparseFillEmptyRows_:vW});function wW(e,t,n){let r=_(e,"inputIndices","sparseReshape","int32"),s=_(t,"inputShape","sparseReshape","int32"),a=_(n,"newShape","sparseReshape","int32");if(r.rank!==2)throw new Error(`Input indices should be Tensor2D but received shape
${r.shape}`);if(s.rank!==1)throw new Error(`Input shape should be Tensor1D but received shape ${s.shape}`);if(a.rank!==1)throw new Error(`New shape should be Tensor1D but received shape ${a.shape}`);let o={inputIndices:r,inputShape:s,newShape:a},i=M.runKernel(rl,o);return{outputIndices:i[0],outputShape:i[1]}}var IW=B({sparseReshape_:wW});function kW(e,t,n){let r=_(e,"data","sparseSegmentMean"),s=_(t,"indices","sparseSegmentMean","int32"),a=_(n,"segmentIds","sparseSegmentMean","int32");if(r.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.rank!==1)throw new Error(`Indices should be Tensor1D but received shape
${s.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
${a.shape}`);let o={data:r,indices:s,segmentIds:a};return M.runKernel(Zd,o)}var SW=B({sparseSegmentMean_:kW});function CW(e,t,n){let r=_(e,"data","sparseSegmentSum"),s=_(t,"indices","sparseSegmentSum","int32"),a=_(n,"segmentIds","sparseSegmentSum","int32");if(r.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.rank!==1)throw new Error(`Indices should be Tensor1D but received shape
${s.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
${a.shape}`);let o={data:r,indices:s,segmentIds:a};return M.runKernel(Jd,o)}var TW=B({sparseSegmentSum_:CW});function NW(e,t,n,r,s,a,o,i){let u=_(e,"data","stringNGrams","string");if(u.dtype!=="string")throw new Error("Data must be of datatype string");if(u.shape.length!==1)throw new Error(`Data must be a vector, saw: ${u.shape}`);let c=_(t,"dataSplits","stringNGrams");if(c.dtype!=="int32")throw new Error("Data splits must be of datatype int32");let l={separator:n,nGramWidths:r,leftPad:s,rightPad:a,padWidth:o,preserveShortSequences:i},p={data:u,dataSplits:c},d=M.runKernel(tp,p,l);return{nGrams:d[0],nGramsSplits:d[1]}}var _W=B({stringNGrams_:NW});function EW(e,t,n=!0){let r=_(e,"input","stringSplit","string"),s=_(t,"delimiter","stringSplit","string");if(r.rank!==1)throw new Error(`Input should be Tensor1D but received shape ${r.shape}`);if(s.rank!==0)throw new Error(`Delimiter should be a scalar but received shape ${s.shape}`);let a={skipEmpty:n},o={input:r,delimiter:s},i=M.runKernel(np,o,a);return{indices:i[0],values:i[1],shape:i[2]}}var AW=B({stringSplit_:EW});function DW(e,t){let n=_(e,"input","stringToHashBucketFast","string"),r={numBuckets:t};if(t<=0)throw new Error("Number of buckets must be at least 1");let s={input:n};return M.runKernel(rp,s,r)}var $W=B({stringToHashBucketFast_:DW});function FW(e,t,n,r=!0){let s=_(e,"input","staticRegexReplace","string"),a={pattern:t,rewrite:n,replaceGlobal:r};return M.runKernel(ep,{x:s},a)}var RW=B({staticRegexReplace_:FW}),QT={fft:kp,ifft:ec,rfft:Sp,irfft:ym},eN={hammingWindow:uz,hannWindow:jT,frame:qT,stft:pz},er={flipLeftRight:gz,grayscaleToRGB:yz,resizeNearestNeighbor:JT,resizeBilinear:ZT,rgbToGrayscale:xz,rotateWithOffset:Iz,cropAndResize:fz,nonMaxSuppression:Sz,nonMaxSuppressionAsync:$z,nonMaxSuppressionWithScore:Rz,nonMaxSuppressionWithScoreAsync:Oz,nonMaxSuppressionPadded:Lz,nonMaxSuppressionPaddedAsync:zz,threshold:Hz,transform:qz},Xw={bandPart:Xz,gramSchmidt:Zz,qr:Qz},tN={absoluteDifference:nW,computeWeightedLoss:Ps,cosineDistance:sW,hingeLoss:oW,huberLoss:uW,logLoss:lW,meanSquaredError:pW,sigmoidCrossEntropy:mW,softmaxCrossEntropy:yW},nN={sparseFillEmptyRows:xW,sparseReshape:IW,sparseSegmentMean:SW,sparseSegmentSum:TW},rN={stringNGrams:_W,stringSplit:AW,stringToHashBucketFast:$W,staticRegexReplace:RW},re={};Ee(re,{Serializable:()=>sN,SerializationMap:()=>aN,getRegisteredName:()=>OW,registerClass:()=>oN});var PW=new Map,Vv=new Map,sN=class{getClassName(){return this.constructor.className}static fromConfig(e,t){return new e(t)}},aN=class Bu{constructor(){this.classNameMap={}}static getMap(){return Bu.instance==null&&(Bu.instance=new Bu),Bu.instance}static register(t){Bu.getMap().classNameMap[t.className]=[t,t.fromConfig]}};function oN(e,t,n){A(e.className!=null,()=>"Class being registered does not have the static className property defined."),A(typeof e.className=="string",()=>"className is required to be a string, but got type "+typeof e.className),A(e.className.length>0,()=>"Class being registered has an empty-string as its className, which is disallowed."),typeof t=="undefined"&&(t="Custom"),typeof n=="undefined"&&(n=e.className);let r=n,s=t+">"+r;return aN.register(e),PW.set(s,e),Vv.set(e,s),e}function OW(e){return Vv.has(e)?Vv.get(e):e.className}var Os=class extends sN{minimize(e,t=!1,n){let{value:r,grads:s}=this.computeGradients(e,n);if(n!=null){let a=n.map(o=>({name:o.name,tensor:s[o.name]}));this.applyGradients(a)}else this.applyGradients(s);return _e(s),t?r:(r.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(e,t){return oT(e,t)}dispose(){this.iterations_!=null&&_e(this.iterations_)}async saveIterations(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:xe(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 extractI
Manifest JSON has weights with names: ${i.join(", ")}.`)}let u=s.reduce((h,f,g)=>(f&&h.push(g),h),[]),c=[];u.forEach(h=>{t[h].paths.forEach(f=>{let g=n+(n.endsWith("/")?"":"/")+f;c.push(g)})});let l=await e(c),p={},d=0;return u.forEach(h=>{let f=t[h].paths.length,g=new Fs(l.slice(d,d+f));a[h].forEach(b=>{let y=g.slice(b.groupOffset,b.groupOffset+b.sizeBytes),v=$C(y,[b.manifestEntry]);for(let x in v)p[x]=v[x]}),d+=f}),p}}var KW="application/octet-stream",XW="application/json",nI=class{constructor(e,t){if(this.DEFAULT_METHOD="POST",t==null&&(t={}),this.weightPathPrefix=t.weightPathPrefix,this.weightUrlConverter=t.weightUrlConverter,t.fetchFunc!=null?(A(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=G().platform.fetch,A(e!=null&&e.length>0,()=>"URL path for http must not be null, undefined or empty."),Array.isArray(e)&&A(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||{},this.loadOptions=t}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}],r=PC(e,n);if(t.body.append("model.json",new Blob([JSON.stringify(r)],{type:XW}),"model.json"),e.weightData!=null){let a=Fs.join(e.weightData);t.body.append("model.weights.bin",new Blob([a],{type:KW}),"model.weights.bin")}let s=await this.fetch(this.path,t);if(s.ok)return{modelArtifactsInfo:lp(e),responses:[s]};throw new Error(`BrowserHTTPRequest.save() failed due to HTTP response status ${s.status}.`)}async loadModelJSON(){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(s){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,r=t.weightsManifest;if(n==null&&r==null)throw new Error(`The JSON from HTTP path ${this.path} contains neither model topology or manifest for weights.`);return t}async load(){if(this.loadOptions.streamWeights)return this.loadStream();let e=await this.loadModelJSON();return Vx(e,t=>this.loadWeights(t))}async loadStream(){let e=await this.loadModelJSON(),t=await this.getWeightUrls(e.weightsManifest),n=Ov(e.weightsManifest),r=()=>jW(t,this.loadOptions);return Object.assign(Object.assign({},e),{weightSpecs:n,getWeightStream:r})}async getWeightUrls(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,r]=YW(t),s=this.weightPathPrefix||n,a=[],o=[];for(let i of e)for(let u of i.paths)this.weightUrlConverter!=null?o.push(this.weightUrlConverter(u)):a.push(s+u+r);return this.weightUrlConverter&&a.push(...await Promise.all(o)),a}async loadWeights(e){let t=await this.getWeightUrls(e),n=Ov(e),r=await iN(t,this.loadOptions);return[n,r]}};nI.URL_SCHEME_REGEX=/^https?:\/\//;function YW(e){let t=e.lastIndexOf("/"),n=e.lastIndexOf("?"),r=e.substring(0,t),s=n>t?e.substring(n):"";return[r+"/",s]}function Gv(e){return e.match(nI.URL_SCHEME_REGEX)!=null}var cN=(e,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let n=!0;if(Array.isArray(e)?n=e.every(r=>Gv(r)
indices.shape[0] = ${e}`}function o4(e,t){return`indices(${e}, 0) is invalid: ${t} < 0`}function i4(e,t,n){return`indices(${e}, 0) is invalid: ${t} >= ${n}`}function u4(e,t){return`only one output dimension may be -1, not both ${e} and ${t}`}function c4(e,t){return`size ${e} must be non-negative, not ${t}`}function l4(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function d4(e,t){let n=ut(e),r=ut(t);return`Input to reshape is a SparseTensor with ${n}
dense values, but the requested shape requires a multiple of ${r}. inputShape=${e} outputShape= ${t}`}function p4(e,t){let n=ut(e),r=ut(t);return`Input to reshape is a tensor with ${n} dense values, but the requested shape has ${r}. inputShape=${e} outputShape=${t}`}function h4(){return"segment ids must be >= 0"}function f4(){return"segment ids are not increasing"}function m4(e,t){return`Segment id ${e} out of range [0, ${t}), possibly because segmentIds input is not sorted.`}function g4(e,t,n){return`Bad: indices[${e}] == ${t} out of range [0, ${n})`}var _N={};Ee(_N,{collectGatherOpShapeInfo:()=>v4,computeOutShape:()=>y4,segOpComputeOptimalWindowSize:()=>b4});function b4(e,t){let n=!1,r;for(e<=oI?(r=e,n=!0):r=af(e,Math.floor(Math.sqrt(e)));!n;)r>t||r===e?n=!0:r=af(e,r+1);return r}function y4(e,t,n){let r=[],s=e.length;for(let a=0;a<s;a++)a!==t?r.push(e[a]):r.push(n);return r}function v4(e,t,n,r){let s=t.shape.length,a=e.shape.length;if(r!==0&&(r<-s||r>s))throw new Error(`Expect batchDims in the range of [-${s}, ${s}], but got ${r}`);if(r<0&&(r+=s),r>a)throw new Error(`batchDims (${r}) must be less than rank(x) (
${a}).`);if(n<r)throw new Error(`batchDims (${r}) must be less than or equal to axis (${n}).`);for(let p=0;p<r;++p)if(e.shape[p]!==t.shape[p])throw new Error(`x.shape[${p}]: ${e.shape[p]} should be equal to indices.shape[${p}]: ${t.shape[p]}.`);let o=e.shape[n],i=[],u=1,c=1,l=1;for(let p=0;p<r;++p)i.push(e.shape[p]),u*=e.shape[p];for(let p=r;p<n;p++)i.push(e.shape[p]),c*=e.shape[p];for(let p=r;p<s;p++)i.push(t.shape[p]);for(let p=n+1;p<a;p++)i.push(e.shape[p]),l*=e.shape[p];return{batchSize:u,sliceSize:l,outerSize:c,dimSize:o,outputShape:i}}function x4(e){try{return e.map(t=>cf(t))}catch(t){throw new Error(`Failed to decode encoded string bytes into utf-8, error: ${t}`)}}function w4(e){return e.map(t=>up(t))}var gs={};Ee(gs,{nonMaxSuppressionV3Impl:()=>KT,nonMaxSuppressionV4Impl:()=>XT,nonMaxSuppressionV5Impl:()=>YT,whereImpl:()=>MT});LW();var EN={kernelName:cc,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>z(e,Xi(ae(n,"float32"),-1))}}},I4={kernelName:Eo,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let r=lt(ae(n,"float32")),s=hn(le(xe(1),r));return bt(fe(e,s))}}}},k4={kernelName:Ao,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let r=hn(le(lt(ae(n,"float32")),1));return fe(e,r)}}}},S4={kernelName:Sa,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,r]=t,s=dt(n.shape,r.shape);return{a:()=>{let i=e,u=Wt(n.shape,s);return u.length>0&&(i=ge(i,u)),W(i,n.shape)},b:()=>{let i=e,u=Wt(r.shape,s);return u.length>0&&(i=ge(i,u)),W(i,r.shape)}}}},C4={kernelName:Do,saveAllInputs:!0,gradFunc:(e,t)=>{let n={};return t.forEach((r,s)=>{n[s]=()=>e.clone()}),n}},T4={kernelName:pc,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>je(n)}}},N4={kernelName:hc,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>je(n)}}},_4={kernelName:$o,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>fe(e,hn(le(xe(1),lt(ae(n,"float32")))))}}},E4={kernelName:Fo,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let r=hn(X(xe(1),lt(ae(n,"float32"))));return fe(e,r)}}}},A4={kernelName:Oo,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,r]=t,s=dt(n.shape,r.shape);return{a:()=>{let i=X(lt(n),lt(r)),u=z(e,fe(r,i)),c=Wt(n.shape,s);return c.length>0&&(u=ge(u,c)),W(u,n.shape)},b:()=>{let i=X(lt(n),lt(r)),u=bt(z(e,fe(n,i))),c=Wt(r.shape,s);return c.length>0&&(u=ge(u,c)),W(u,r.shape)}}}},D4={kernelName:Ro,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>fe(e,X(lt(ae(n,"float32")),1))}}},$4={kernelName:Po,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>fe(e,le(xe(1),lt(ae(n,"float32"))))}}};function F4(e,t,n,r,s,a){let o=_(e,"dy","avgPool3dGrad"),i=_(t,"input","avgPool3dGrad"),u=o,c=i,l=!1;i.rank===4&&(l=!0,u=W(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]]),c=W(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])),A(u.rank===5,()=>`Error in avgPool3dGrad: dy must be rank 5 but got rank ${u.rank}.`),A(c.rank===5,()=>`Error in avgPool3dGrad: input must be rank 5 but got rank ${c.rank}.`),_n("avgPool3dGrad",s,a);let p={dy:u,input:c},d={filterSize:n,strides:r,pad:s,dimRoundingMode:a},h=M.runKernel(zd,p,d);return l?W(h,[h.shape[1],h.shape[2],h.shape[3],h.shape[4]]):h}var R4=B({avgPool3dGrad_:F4}),P4={kernelName:fc,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[r]=t,{filterSize:s,strides:a,pad:o,dimRoundingMode:i}=n;return{x:()=>R4(e,r,s,a,o,i)}}};function O4(e,t,n,r,s){let a=_(e,"dy","avgPoolGrad"),o=_(t,"input","avgPoolGrad");A(o.rank===a.rank,()=>`Rank of input (${o.rank}) does not match rank of dy (${a.rank})`);let i=o,u=a,c=!1;o.rank===3&&(c=!0,i=W(o,[1,o.shape[0],o.shape[1],o.shape[2]]),u=W(a,[1,a.shape[0],a.shape[1],a.shape[2]])),A(u.rank===4,()=>`Error in avgPoolGrad: dy must be rank 4 but got rank ${u.rank}.`),A(i.rank===4,()=>`Error in avgPoolGrad: input must be rank 4 but got rank ${i.rank}.`);let l={dy:u,input:i},p={filterSize:n,strides:r,pad:s},d=M.runKernel(Bd,l,p);return c?W(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var M4=B({avgPoolGrad_:O4}),L4={kernelName:Mo,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[r]=t,{filterSize:s,strides:a,pad:o}=n;return{x:()=>M4(e,r,s,a,o)}}},B4={kernelName
1. The ${r} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom ${r} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);return o}else{let a=e;if(a.className==null||a.config==null)throw new V(`${r}: Improper config format: ${JSON.stringify(a)}.
'className' and 'config' must set.`);let o=a.className,i,u;if(o in n?[i,u]=n[o]:o in Nr?[i,u]=Nr.className:o in t&&([i,u]=t[o]),i==null)throw new V(`Unknown ${r}: ${o}. This may be due to one of the following reasons:
1. The ${r} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom ${r} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);if(u!=null){let c={};for(let h of Object.keys(Nr))c[h]=Nr[h];for(let h of Object.keys(n))c[h]=n[h];let l=a.config;l.customObjects=c;let p=Object.assign({},Nr);for(let h of Object.keys(n))Nr[h]=n[h];jv(a.config);let d=u(i,a.config,n,s);return Nr=Object.assign({},p),d}else{let c=Object.assign({},Nr);for(let p of Object.keys(n))Nr[p]=n[p];let l=new i(a.config);return Nr=Object.assign({},c),l}}}function NG(e,t){return e<t?-1:e>t?1:0}function Ph(e,t){return-1*NG(e,t)}function ua(e){if(e==null)return e;let t=[];for(let n of e)t.indexOf(n)===-1&&t.push(n);return t}function _G(e){if(e==null)throw new V(`Invalid value in obj: ${JSON.stringify(e)}`);for(let t in e)if(e.hasOwnProperty(t))return!1;return!0}function Zi(e,t,n){if(n!=null&&e.indexOf(n)<0)throw new V(`${n} is not a valid ${t}. Valid values are ${e} or null/undefined.`)}function uI(e,t,n=0,r=1/0){return ss(n>=0),ss(r>=n),Array.isArray(e)&&e.length>=n&&e.length<=r&&e.every(s=>typeof s===t)}function tn(e,t){Array.isArray(e)?(w.assert(e.length>0,()=>`${t} is unexpectedly an empty array.`),e.forEach((n,r)=>tn(n,`element ${r+1} of ${t}`))):w.assert(Number.isInteger(e)&&e>0,()=>`Expected ${t} to be a positive integer, but got ${MN(e)}.`)}function MN(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>MN(t)).join(",")+"]":typeof e=="string"?`"${e}"`:`${e}`}function EG(e,t,n){let r=n!=null?n():w.now(),s;return(...o)=>{let i=n!=null?n():w.now();return i-r<t||(r=i,s=e(...o)),s}}function LN(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}var AG=0;function BN(){return AG++}var Oh={};function Am(e=""){return e in Oh||(Oh[e]=0),Oh[e]+=1,e+Oh[e].toString()}var DG=["channelsFirst","channelsLast"],$G=["nearest","bilinear"],FG=["valid","same","causal"],RG=["max","avg"],PG=["sum","mul","concat","ave"],Pu=new Map;function Pt(e){Zi(DG,"DataFormat",e)}function OG(e){Zi($G,"InterpolationFormat",e)}function vr(e){Zi(FG,"PaddingMode",e)}function zN(e){Zi(RG,"PoolMode",e)}var gd=[],LS="/";function oo(e,t){gd.push(e);try{let n=t();return gd.pop(),n}catch(n){throw gd.pop(),n}}function MG(){return gd.length===0?"":gd.join(LS)+LS}function WN(e){if(!UN(e))throw new Error("Not a valid tensor name: '"+e+"'");return MG()+e}function VN(e){if(!UN(e))throw new Error("Not a valid tensor name: '"+e+"'");Pu.has(e)||Pu.set(e,0);let t=Pu.get(e);if(Pu.set(e,Pu.get(e)+1),t>0){let n=`${e}_${t}`;return Pu.set(n,1),n}else return e}var LG=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function UN(e){return!!e.match(LG)}function BG(e){return e===parseInt(e.toString(),10)}function ca(e,t,n){t==null&&(t=0),n==null&&(n=e.length);let r=1;for(let s=t;s<n;++s)r*=e[s];return r}function nc(e){if(e.length===0)return Number.NaN;let t=Number.POSITIVE_INFINITY;for(let n=0;n<e.length;n++){let r=e[n];r<t&&(t=r)}return t}function ba(e){if(e.length===0)return Number.NaN;let t=Number.NEGATIVE_INFINITY;for(let n=0;n<e.length;n++){let r=e[n];r>t&&(t=r)}return t}function Gr(e,t){if(t<e)throw new V(`end (${t}) < begin (${e}) is forbidden.`);let n=[];for(let r=e;r<t;++r)n.push(r);return n}var yv;function qt(){return yv==null&&(yv=zx().epsilon()),yv}function Hr(){return"channelsLast"}function cs(e,t){return ae(e,t)}function Np(e,t=-1){let n=e.shape.slice();return t<0&&(t=n.length+t+1),n.splice(t,0,1),W(e,n)}function zG(e,t){return O(()=>{if(e.shape.length!==2)throw new V(`repeat() expects a rank-2 tensor, but received a rank-${e.shape.length} tensor.`);let n=Np(e,1);return qv(n,[1,t,1])})}function WG(e){let t=[ca(e.shape)];return W(e,t)}function VG(e){if(e.rank<=1)throw new V(`batchFlatten requires a minimum rank of 2. Got rank: ${e.rank}.`);let t=[e.shape[0],ca(e.shape,1)];return W(e,t)}function io(e,t,n){return O(()=>{switch(e.rank){case 1:return Ip(e,t,n);case 2:return bm(e,[t,0],[n,e.shape[1]]);case 3:return Ki(e,[t,0,0],[n,e.shape[1],e.shape[2]]);case 4:return Qu(e,[t,0,0,0],[n,e.shape[1],e.shape[2],e.shape[3]]);case 5:return Ve(e,[t,0,0,0,0],[n,e.shape[1],e.shape[2],e.shape[3],e.shape[4]]);cas
Received countWeights=${t.countWeights}`);n=Te(t.countWeights)}let r=hr(e),s=Xu(e),a=En(this.numTokens,r).bufferSync().get(0),o=Rs(s,0).bufferSync().get(0);if(!(a&&o))throw new V(`Input values must be between 0 < values <= numTokens with numTokens=${this.numTokens}`);return B6(e,this.outputMode,this.numTokens,n)})}};Dk.className="CategoryEncoding";re.registerClass(Dk);var z6=["bilinear","nearest"],r1=new Set(z6),$k=class extends ze{constructor(e){if(super(e),this.height=e.height,this.width=e.width,e.interpolation)if(r1.has(e.interpolation))this.interpolation=e.interpolation;else throw new V(`Invalid interpolation parameter: ${e.interpolation} is not implemented`);else this.interpolation="bilinear";this.cropToAspectRatio=!!e.cropToAspectRatio}computeOutputShape(e){e=Qe(e);let t=e[2];return[this.height,this.width,t]}getConfig(){let e={height:this.height,width:this.width,interpolation:this.interpolation,cropToAspectRatio:this.cropToAspectRatio},t=super.getConfig();return Object.assign(e,t),e}call(e,t){return O(()=>{let n=[this.height,this.width];if(this.interpolation==="bilinear")return er.resizeBilinear(e,n,!this.cropToAspectRatio);if(this.interpolation==="nearest")return er.resizeNearestNeighbor(e,n,!this.cropToAspectRatio);throw new Error(`Interpolation is ${this.interpolation} but only ${[...r1]} are supported`)})}};$k.className="Resizing";re.registerClass($k);var Y2=class{constructor(e){this.seed=e}next(){if(this.seed!==void 0)return this.seed++}};Y2.className="RandomSeed";var Z2=class extends ze{constructor(e){super(e),this.randomGenerator=new Y2(e.seed)}getConfig(){let e={seed:this.randomGenerator.seed},t=super.getConfig();return Object.assign(e,t),e}};Z2.className="BaseRandomLayer";var W6=["bilinear","nearest"],s1=new Set(W6),Fk=class extends Z2{constructor(e){super(e);let{factor:t,interpolation:n="bilinear"}=e;if(this.factor=t,Array.isArray(this.factor)&&this.factor.length===2)this.widthLower=this.factor[0],this.widthUpper=this.factor[1];else if(!Array.isArray(this.factor)&&this.factor>0)this.widthLower=-this.factor,this.widthUpper=this.factor;else throw new V(`Invalid factor: ${this.factor}. Must be positive number or tuple of 2 numbers`);if(this.widthLower<-1||this.widthUpper<-1)throw new V(`factor must have values larger than -1. Got: ${this.factor}`);if(this.widthUpper<this.widthLower)throw new V(`factor cannot have upper bound less than lower bound.
2023-05-08 15:08:30 +02:00
Got upper bound: ${this.widthUpper}.
Got lower bound: ${this.widthLower}
2024-01-17 22:44:24 +01:00
`);if(n)if(s1.has(n))this.interpolation=n;else throw new V(`Invalid interpolation parameter: ${n} is not implemented`)}getConfig(){let e={factor:this.factor,interpolation:this.interpolation},t=super.getConfig();return Object.assign(e,t),e}computeOutputShape(e){e=Qe(e);let t=e[2];return[this.imgHeight,-1,t]}call(e,t){return O(()=>{let n=Te(e);this.imgHeight=n.shape[n.shape.length-3];let r=n.shape[n.shape.length-2];this.widthFactor=$a([1],1+this.widthLower,1+this.widthUpper,"float32",this.randomGenerator.next());let s=this.widthFactor.dataSync()[0]*r;s=Math.round(s);let a=[this.imgHeight,s];switch(this.interpolation){case"bilinear":return er.resizeBilinear(e,a);case"nearest":return er.resizeNearestNeighbor(e,a);default:throw new Error(`Interpolation is ${this.interpolation}
but only ${[...s1]} are supported`)}})}};Fk.className="RandomWidth";re.registerClass(Fk);function V6(e){return new bl(e)}function U6(e){return new RI(e)}function G6(e){return new DI(e)}function H6(e){return new $I(e)}function j6(e){return new FI(e)}function q6(e){return new OI(e)}function K6(e){return new PI(e)}function X6(e){return new WI(e)}function Y6(e){return new Gm(e)}function Z6(e){return new LI(e)}function J6(e){return new Hm(e)}function Q6(e){return new BI(e)}function ej(e){return new zI(e)}function tj(e){return new VI(e)}function nj(e){return new UI(e)}function rj(e){return new GI(e)}function sj(e){return new JI(e)}function aj(e){return new YI(e)}function oj(e){return new Ym(e)}function ij(e){return new XI(e)}function uj(e){return new ZI(e)}function cj(e){return new QI(e)}function lj(e){return new ek(e)}function dj(e){return new tk(e)}function pj(e){return new rk(e)}function hj(e){return new sk(e)}function fj(e){return new ok(e)}function mj(e){return new ck(e)}function gj(e){return new ik(e)}function bj(e){return new uk(e)}function yj(e){return new ak(e)}function vj(e){return new lk(e)}function xj(e){return new fk(e)}function wj(e){return new mk(e)}function Ij(e){return new gk(e)}function Rk(e){return new yk(e)}function kj(e){return Rk(e)}function Sj(e){return Rk(e)}function Pk(e){return new xk(e)}function Cj(e){return Pk(e)}function Tj(e){return Pk(e)}function Ok(e){return new Ik(e)}function Nj(e){return Ok(e)}function _j(e){return Ok(e)}function Ej(e){return new kk(e)}function Aj(e){return new Ck(e)}function J2(e){return new Sk(e)}function Q2(e){return new Tk(e)}function e_(e){return new bk(e)}function t_(e){return new vk(e)}function Dj(e){return new wk(e)}function $j(e){return new jI(e)}function Fj(e){return new qm(e)}function Rj(e){return new qI(e)}function Pj(e){return new Fp(e)}function Oj(e){return new HI(e)}function Mj(e){return new jm(e)}function Lj(e){return new KI(e)}function Bj(e){return new Xm(e)}function zj(e){return new Ms(e)}function Wj(e){return new Km(e)}function Vj(e){return new _k(e)}function Uj(e){return new Nk(e)}var Gj=J2,Hj=Q2,jj=e_,qj=t_;function Kj(e){return new dk(e)}function Xj(e){return new pk(e)}function Yj(e){return new hk(e)}function Zj(e){return new nk(e)}function Jj(e){return new Ek(e)}function Qj(e){return new Ak(e)}function e5(e){return new $k(e)}function t5(e){return new Dk(e)}function n5(e){return new Fk(e)}var n_={};Ee(n_,{MAPE:()=>h5,MSE:()=>g5,binaryAccuracy:()=>r5,binaryCrossentropy:()=>s5,categoricalAccuracy:()=>o5,categoricalCrossentropy:()=>i5,cosineProximity:()=>l5,mape:()=>f5,meanAbsoluteError:()=>d5,meanAbsolutePercentageError:()=>p5,meanSquaredError:()=>m5,mse:()=>b5,precision:()=>u5,recall:()=>c5,sparseCategoricalAccuracy:()=>a5});function r5(e,t){return CI(e,t)}function s5(e,t){return i2(e,t)}function a5(e,t){return u2(e,t)}function o5(e,t){return TI(e,t)}function i5(e,t){return NI(e,t)}function u5(e,t){return o2(e,t)}function c5(e,t){return WH(e,t)}function l5(e,t){return SI(e,t)}function d5(e,t){return zm(e,t)}function p5(e,t){return yl(e,t)}function h5(e,t){return yl(e,t)}function f5(e,t){return yl(e,t)}function m5(e,t){return Ji(e,t)}function g5(e,t){return Ji(e,t)}function b5(e,t){return Ji(e,t)}var r_={};Ee(r_,{modelFromJSON:()=>b6});var s_={};Ee(s_,{l1:()=>v5,l1l2:()=>y5,l2:()=>x5});function y5(e){return new Dp(e)}function v5(e){return S6(e)}function x5(e){return C6(e)}var a_=class extends rc{constructor(){super(...arguments),this.model=null}setModel(e){if(!(e instanceof Es))throw new Error("model must be a LayersModel, not some other Container");this.model=e}};function Bh(e,t){return e<t}function a1(e,t){return e>t}var o_=class extends a_{constructor(e){if(super(),e==null&&(e={}),e.restoreBestWeights)throw new Be("restoreBestWeights = True is not implemented in EarlyStopping yet.");this.monitor=e.monitor||"val_loss",this.minDelta=Math.abs(e.minDelta||0),this.patience=e.patience||0,this.verbose=e.verbose||0,this.mode=e.mode||"auto",this.baseline=e.baseline,["auto","min","max"].indexOf(this.mode)===-1&&(console.warn(`EarlyStopping mode '${this.
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),Ar(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,r)=>this.write(n,t[r]))}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 r=0;r<this.size();r++)e.push(r)}if(e.length===0)return yn([],[0].concat(this.elementShape));let n=this.readMany(e);return Ar(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),Dt(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 yn([],[0].concat(this.elementShape));let t=[];for(let r=0;r<this.size();r++)t.push(r);let n=this.readMany(t);return Ar(this.elementShape,n[0].shape,`TensorArray shape mismatch: tensor array shape (${this.elementShape}) vs first tensor shape (${n[0].shape})`),tt(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,pt(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,r=e.map(i=>(n+=i,n));if(n!==t.shape[0])throw new Error(`Expected sum of lengths to be equal to
2021-09-11 17:11:38 +02:00
tensor.shape[0], but sum of lengths is
2024-01-17 22:44:24 +01:00
${n}, and tensor's shape is: ${t.shape}`);if(!this.dynamicSize&&e.length!==this.maxSize)throw new Error(`TensorArray's size is not equal to the size of lengths (${this.maxSize} vs. ${e.length}), and the TensorArray is not marked as dynamically resizeable`);let s=n===0?0:t.size/n,a=[];O(()=>{t=W(t,[1,n,s]);for(let i=0;i<e.length;++i){let c=[0,i===0?0:r[i-1],0],l=[1,e[i],s];a[i]=W(Ve(t,c,l),this.elementShape)}return a});let o=[];for(let i=0;i<e.length;i++)o[i]=i;this.writeMany(o,a)}},Jm=class hx{get id(){return this.idTensor.id}constructor(t,n,r,s=-1){this.tensors=t,this.elementShape=n,this.elementDtype=r,t!=null&&t.forEach(a=>{if(r!==a.dtype)throw new Error(`Invalid data types; op elements ${r}, but list elements ${a.dtype}`);Ar(n,a.shape,"TensorList shape mismatch: "),Ht(a)}),this.idTensor=xe(0),this.maxNumElements=s,Ht(this.idTensor)}copy(){return new hx([...this.tensors],this.elementShape,this.elementDtype)}clearAndClose(t){this.tensors.forEach(n=>{(t==null||!t.has(n.id))&&n.dispose()}),this.tensors.length=0,this.idTensor.dispose()}size(){return this.tensors.length}stack(t,n,r=-1){if(n!==this.elementDtype)throw new Error(`Invalid data types; op elements ${n}, but list elements ${this.elementDtype}`);if(r!==-1&&this.tensors.length!==r)throw new Error(`Operation expected a list with ${r} elements but got a list with ${this.tensors.length} elements.`);Ar(t,this.elementShape,"TensorList shape mismatch: ");let s=rd(this.elementShape,this.tensors,t);return O(()=>{let a=this.tensors.map(o=>W(o,s));return Dt(a,0)})}popBack(t,n){if(n!==this.elementDtype)throw new Error(`Invalid data types; op elements ${n}, but list elements ${this.elementDtype}`);if(this.size()===0)throw new Error("Trying to pop from an empty list.");let r=rd(this.elementShape,this.tensors,t),s=this.tensors.pop();return s.kept=!1,Ar(s.shape,t,"TensorList shape mismatch: "),W(s,r)}pushBack(t){if(t.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t.dtype}, but list elements ${this.elementDtype}`);if(Ar(t.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");Ht(t),this.tensors.push(t)}resize(t){if(t<0)throw new Error(`TensorListResize expects size to be non-negative. Got: ${t}`);if(this.maxNumElements!==-1&&t>this.maxNumElements)throw new Error(`TensorListResize input size ${t} is greater maxNumElement ${this.maxNumElements}.`);let n=new hx([],this.elementShape,this.elementDtype,this.maxNumElements);n.tensors.length=t;for(let r=0;r<Math.min(this.tensors.length,t);++r)n.tensors[r]=this.tensors[r];return n}getItem(t,n,r){if(r!==this.elementDtype)throw new Error(`Invalid data types; op elements ${r}, but list elements ${this.elementDtype}`);if(t<0||t>this.tensors.length)throw new Error(`Trying to access element ${t} in a list with ${this.tensors.length} elements.`);if(this.tensors[t]==null)throw new Error(`element at index ${t} is null.`);Ar(this.tensors[t].shape,n,"TensorList shape mismatch: ");let s=rd(this.elementShape,this.tensors,n);return W(this.tensors[t],s)}setItem(t,n){if(n.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${n.dtype}, but list elements ${this.elementDtype}`);if(t<0||this.maxNumElements!==-1&&t>=this.maxNumElements)throw new Error(`Trying to set element ${t} in a list with max ${this.maxNumElements} elements.`);Ar(this.elementShape,n.shape,"TensorList shape mismatch: "),Ht(n),this.tensors[t]!=null&&(this.tensors[t].kept=!1),this.tensors[t]=n}gather(t,n,r){if(n!==this.elementDtype)throw new Error(`Invalid data types; op elements ${n}, but list elements ${this.elementDtype}`);Ar(this.elementShape,r,"TensorList shape mismatch: "),t=t.slice(0,this.size());let s=rd(this.elementShape,this.tensors,r);return t.length===0?yn([],[0].concat(s)):O(()=>{let a=t.map(o=>W(this.tensors[o],s));return Dt(a,0)})}concat(t,n){if(t&&t!==this.elementDtype)throw new Error(`TensorList dtype is ${this.elementDtype} but concat requested dtype ${t}`);Ar(this.elementShape,n,"TensorList shape mismatch: ");let r=rd(th
2021-09-11 17:11:38 +02:00
tensor.shape[0], but sum of lengths is
2024-01-17 22:44:24 +01:00
${r}, and tensor's shape is: ${e.shape}`);let a=e.shape.slice(1),o=px(a,n),i=r===0?0:e.size/r,u=O(()=>{let l=[];e=W(e,[1,r,i]);for(let p=0;p<t.length;++p){let h=[0,p===0?0:s[p-1],0],f=[1,t[p],i];l[p]=W(Ve(e,h,f),o)}return e.dispose(),l}),c=new Jm([],n,e.dtype,t.length);for(let l=0;l<u.length;l++)c.setItem(l,u[l]);return c}var eq=async(e,t,n)=>{switch(e.op){case"If":case"StatelessIf":{let r=I("thenBranch",e,t,n),s=I("elseBranch",e,t,n),a=I("cond",e,t,n),o=I("args",e,t,n);return(await a.data())[0]?n.functionMap[r].executeFunctionAsync(o,n.tensorArrayMap,n.tensorListMap):n.functionMap[s].executeFunctionAsync(o,n.tensorArrayMap,n.tensorListMap)}case"While":case"StatelessWhile":{let r=I("body",e,t,n),s=I("cond",e,t,n),a=I("args",e,t,n),o=await n.functionMap[s].executeFunctionAsync(a,n.tensorArrayMap,n.tensorListMap),i=a.map(l=>l.id),u=await o[0].data();o.forEach(l=>{!l.kept&&i.indexOf(l.id)===-1&&l.dispose()});let c=a;for(;u[0];){let l=c;c=await n.functionMap[r].executeFunctionAsync(c,n.tensorArrayMap,n.tensorListMap);let p=c.map(h=>h.id);l.forEach(h=>{!h.kept&&i.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()});let d=await n.functionMap[s].executeFunctionAsync(c,n.tensorArrayMap,n.tensorListMap);u=await d[0].data(),d.forEach(h=>{!h.kept&&i.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()})}return c}case"LoopCond":{let r=I("pred",e,t,n);return[Ts(r)]}case"Switch":{let r=I("pred",e,t,n),s=I("data",e,t,n);return s.kept||(s=Ts(s)),(await r.data())[0]?[void 0,s]:[s,void 0]}case"Merge":{let r=e.inputNames.find(s=>dn(s,t,n)!==void 0);if(r){let s=dn(r,t,n);return[Ts(s)]}return}case"Enter":{let r=I("frameName",e,t,n),s=I("tensor",e,t,n);return n.enterFrame(r),[Ts(s)]}case"Exit":{let r=I("tensor",e,t,n);return n.exitFrame(),[Ts(r)]}case"NextIteration":{let r=I("tensor",e,t,n);return n.nextIteration(),[Ts(r)]}case"TensorArrayV3":{let r=I("size",e,t,n),s=I("dtype",e,t,n),a=I("elementShape",e,t,n),o=I("dynamicSize",e,t,n),i=I("clearAfterRead",e,t,n),u=I("identicalElementShapes",e,t,n),c=I("name",e,t,n),l=new X5(c,s,r,a,u,o,i);return n.addTensorArray(l),[l.idTensor,xe(1)]}case"TensorArrayWriteV3":{let r=I("tensorArrayId",e,t,n),s=I("index",e,t,n),a=I("tensor",e,t,n),o=n.getTensorArray(r.id);return o.write(s,a),[o.idTensor]}case"TensorArrayReadV3":{let r=I("tensorArrayId",e,t,n),s=I("index",e,t,n);return[n.getTensorArray(r.id).read(s)]}case"TensorArrayGatherV3":{let r=I("tensorArrayId",e,t,n),s=I("indices",e,t,n),a=I("dtype",e,t,n);return[n.getTensorArray(r.id).gather(s,a)]}case"TensorArrayScatterV3":{let r=I("tensorArrayId",e,t,n),s=I("indices",e,t,n),a=I("tensor",e,t,n),o=n.getTensorArray(r.id);return o.scatter(s,a),[o.idTensor]}case"TensorArrayConcatV3":{let r=I("tensorArrayId",e,t,n),s=n.getTensorArray(r.id),a=I("dtype",e,t,n);return[s.concat(a)]}case"TensorArraySplitV3":{let r=I("tensorArrayId",e,t,n),s=I("tensor",e,t,n),a=I("lengths",e,t,n),o=n.getTensorArray(r.id);return o.split(a,s),[o.idTensor]}case"TensorArraySizeV3":{let r=I("tensorArrayId",e,t,n),s=n.getTensorArray(r.id);return[xe(s.size(),"int32")]}case"TensorArrayCloseV3":{let r=I("tensorArrayId",e,t,n),s=n.getTensorArray(r.id);return s.clearAndClose(),[s.idTensor]}case"TensorListSetItem":{let r=I("tensorListId",e,t,n),s=I("index",e,t,n),a=I("tensor",e,t,n),o=n.getTensorList(r.id);return o.setItem(s,a),[o.idTensor]}case"TensorListGetItem":{let r=I("tensorListId",e,t,n),s=I("index",e,t,n),a=I("elementShape",e,t,n),o=I("elementDType",e,t,n);return[n.getTensorList(r.id).getItem(s,a,o)]}case"TensorListScatterV2":case"TensorListScatter":{let r=I("indices",e,t,n),s=I("tensor",e,t,n),a=I("elementShape",e,t,n),o=I("numElements",e,t,n),i=J5(s,r,a,o);return n.addTensorList(i),[i.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let r=I("elementShape",e,t,n),s=I("elementDType",e,t,n),a;e.op==="TensorListReserve"?a="numElements":a="maxNumElements";let o=I(a,e,t,n),i=e.op==="TensorListReserve"?-1:o,u=Z5(r,s,o,i);return n.addTensorList(u),[u.idTensor]}case"TensorListGather":{let r=I("tensorListId",e,t,n),s=I("indices",e,t,n),a=I("elementShape",e,t,n),o=I("element
${e}`);let r;return this.size===1/0||this.size==null?r=this.size:t?r=Math.ceil(this.size/e):r=Math.floor(this.size/e),Zn(async()=>(await n.iterator()).columnMajorBatch(e,t,a8),r)}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,Zn(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,Zn(async()=>(await t.iterator()).filter(r=>O(()=>e(r))),n)}async forEachAsync(e){return(await this.iterator()).forEachAsync(e)}map(e){let t=this;return Zn(async()=>(await t.iterator()).map(n=>O(()=>e(n))),this.size)}mapAsync(e){let t=this;return Zn(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 Zn(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,Zn(async()=>{let r=zk(async()=>({value:await t.iterator(),done:!1}));return Uq(r.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,Zn(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 r=this,s=Pq.alea(t||w.now().toString());return Zn(async()=>{let a=s.int32();return n&&(a+=s.int32()),(await r.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,Zn(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()}};vl.MAX_BUFFER_SIZE=1e4;function Zn(e,t=null){return new class extends vl{constructor(){super(...arguments),this.size=t}async iterator(){return e()}}}function r8(e){return Zn(async()=>M_(e),e.length)}function s8(e){if(!sc(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 Zn(async()=>{let n=await F_(e,r=>{if(r instanceof vl)return{value:r.iterator(),recurse:!1};if(sc(r))return{value:null,recurse:!0};throw new Error("Leaves of the structure passed to zip() must be Datasets, not primitives.")});return Gq(n,aa.SHORTEST)},t)}function a8(e){if(e===null)return null;let t=e[0];return Bq(t)?{value:o8(e),recurse:!1}:{value:null,recurse:!0}}function o8(e){if(e.length===0)throw new Error("Can't make a batch of zero elements.");return e[0]instanceof Ne?Dt(e):yn(e)}var z_=class extends vl{constructor(e){super(),this.input=e}async iterator(){return(await this.input.iterator()).decodeUTF8().split(`
`).map(r=>(r.endsWith("\r")&&(r=r.slice(0,-1)),r))}},Wh='"',sd=Symbol("out"),b1=Symbol("field"),Vh=Symbol("quote"),Cv=Symbol("quoteafterquote"),y1=Symbol("quoteinquote"),W_=class extends vl{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((r,s)=>(r[s]=r[s]+1||1,r),{}),n=Object.keys(t).filter(r=>t[r]>1);if(w.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let r of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(r)===-1)throw new Error('The key "'+r+'" 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}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 z_(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 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={},r={};for(let s=0;s<this.fullColumnNames.length;s++){let a=this.fullColumnNames[s],o=this.columnConfigs?this.columnConfigs[a]:null;if(!(this.configuredColumnsOnly&&!o)){let i=t[s],u=null;if(i==="")if(o&&o.default!==void 0)u=o.default;else{if(o&&(o.required||o.isLabel))throw new Error(`Required column ${a} is empty in this line: ${e}`);u=void 0}else{let c=Number(i);if(isNaN(c))o&&o.dtype==="bool"?u=this.getBoolean(i):u=i;else if(!o||!o.dtype)u=c;else switch(o.dtype){case"float32":u=c;break;case"int32":u=Math.floor(c);break;case"bool":u=this.getBoolean(i);break;default:u=c}}o&&o.isLabel?r[a]=u:n[a]=u}}return Object.keys(r).length===0?n:{xs:n,ys:r}}getBoolean(e){return e==="1"||e.toLowerCase()==="true"?1:0}parseRow(e,t=!0){let n=[],r=0,s=e.length,a=sd;for(let o=0;o<s;o++)switch(a){case sd:switch(e.charAt(o)){case Wh:r=o+1,a=Vh;break;case this.delimiter:if(r=o+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),a=sd;break;default:a=b1,r=o;break}break;case b1:switch(e.charAt(o)){case this.delimiter:n.push(e.substring(r,o)),a=sd,r=o+1;break;default:}break;case Vh:switch(e.charAt(o)){case Wh:a=Cv;break;default:}break;case Cv:switch(e.charAt(o)){case this.delimiter:n.push(e.substring(r,o-1)),a=sd,r=o+1;break;case Wh:a=Vh;break;default:a=y1;break}break;case y1:switch(e.charAt(o)){case Wh:a=Vh;break;default:}break;default:}if(a===Cv?n.push(e.substring(r,s-1)):n.push(e.substring(r)),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}},i8=class V_ extends sn{constructor(t){super(),this.microphoneConfig=t,this.isClosed=!1,this.fftSize=t.fftSize||1024;let n=Math.log2(this.fftSize);if(this.fftSize<0||n<4||n>14||!Number.isInteger(n))throw new Error(`Invalid ff
2021-09-11 17:11:38 +02:00
============================
2022-05-18 14:36:05 +02:00
Hi, looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, visit https://github.com/tensorflow/tfjs-node for more details.
2024-01-17 22:44:24 +01:00
============================`));let s={id:this.nextDataId()};return this.data.set(s,{values:t,dtype:r,refCount:1}),s}makeTensorInfo(t,n,r){let s;if(n==="string"&&r!=null&&r.length>0&&w.isString(r[0])){let a=r.map(o=>w.encodeString(o));s=this.write(a,t,n)}else s=this.write(r,t,n);return{dataId:s,shape:t,dtype:n}}refCount(t){return this.data.has(t)?this.data.get(t).refCount:0}incRef(t){let n=this.data.get(t);n.refCount++}decRef(t){if(this.data.has(t)){let n=this.data.get(t);n.refCount--}}move(t,n,r,s,a){this.data.set(t,{values:n,dtype:s,refCount:a})}numDataIds(){return this.data.numDataIds()}async read(t){return this.readSync(t)}readSync(t){let{dtype:n,complexTensorInfos:r}=this.data.get(t);if(n==="complex64"){let s=this.readSync(r.real.dataId),a=this.readSync(r.imag.dataId);return T.mergeRealAndImagArrays(s,a)}return w.convertBackendValuesAndArrayBuffer(this.data.get(t).values,n)}bufferSync(t){let n=this.readSync(t.dataId);if(t.dtype==="string")try{let r=n.map(s=>w.decodeString(s));return Me(t.shape,t.dtype,r)}catch(r){throw new Error("Failed to decode encoded string bytes into utf-8")}return Me(t.shape,t.dtype,n)}makeOutput(t,n,r){return Er().makeTensorFromTensorInfo(this.makeTensorInfo(n,r,t),this)}disposeData(t,n=!1){if(this.data.has(t)){if(this.data.get(t).refCount--,!n&&this.data.get(t).refCount>0)return!1;let{complexTensorInfos:r}=this.data.get(t);r!=null&&(this.disposeData(r.real.dataId,!0),this.disposeData(r.imag.dataId,!0)),this.data.delete(t)}return!0}disposeIntermediateTensorInfo(t){this.disposeData(t.dataId)}async time(t){let n=w.now();return t(),{kernelMs:w.now()-n}}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(t){be([t],"where");let n=this.readSync(t.dataId);return I8(t.shape,n)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}};Vk.nextDataId=0;var Uk={};Ee(Uk,{addImpl:()=>Q_,bincountImpl:()=>Hk,bincountReduceImpl:()=>eE,bitwiseAndImpl:()=>tE,castImpl:()=>J_,ceilImpl:()=>nE,concatImpl:()=>jk,equalImpl:()=>rE,expImpl:()=>aE,expm1Impl:()=>iE,floorDivImpl:()=>cE,floorImpl:()=>uE,gatherNdImpl:()=>lE,gatherV2Impl:()=>dE,greaterEqualImpl:()=>hE,greaterImpl:()=>pE,lessEqualImpl:()=>mE,lessImpl:()=>fE,linSpaceImpl:()=>gE,logImpl:()=>bE,maxImpl:()=>yE,maximumImpl:()=>vE,minimumImpl:()=>xE,multiplyImpl:()=>qk,negImpl:()=>wE,notEqualImpl:()=>IE,prodImpl:()=>kE,raggedGatherImpl:()=>SE,raggedRangeImpl:()=>CE,raggedTensorToTensorImpl:()=>TE,rangeImpl:()=>Xk,rsqrtImpl:()=>NE,scatterImpl:()=>so,sigmoidImpl:()=>wK,simpleAbsImpl:()=>Z_,sliceImpl:()=>Cf,sparseFillEmptyRowsImpl:()=>EE,sparseReshapeImpl:()=>AE,sparseSegmentReductionImpl:()=>Yk,sqrtImpl:()=>SK,squaredDifferenceImpl:()=>DE,staticRegexReplaceImpl:()=>$E,stridedSliceImpl:()=>FE,stringNGramsImpl:()=>Zk,stringSplitImpl:()=>Jk,stringToHashBucketFastImpl:()=>Qk,subImpl:()=>RE,tileImpl:()=>PE,topKImpl:()=>ME,transposeImpl:()=>Kk,uniqueImpl:()=>t0});function Z_(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var k8=e=>{let{x:t}=e.inputs,n=e.backend;be(t,"abs");let r=new Float32Array(w.sizeFromShape(t.shape)),s=n.data.get(t.dataId).values;return r=Z_(s),n.makeOutput(r,t.shape,t.dtype)},S8={kernelName:cc,backendName:"cpu",kernelFunc:k8};function Ot(e){return(t,n,r,s,a)=>{let o=T.assertAndGetBroadcastShape(t,n),i=o.length,u=w.computeStrides(o),c=w.sizeFromShape(o),l=w.getTypedArrayFromDType(a,c),p=t.length,d=n.length,h=w.computeStrides(t),f=w.computeStrides(n),g=T.getBroadcastDims(t,o),m=T.getBroadcastDims(n,o);if(g.length+m.length===0)for(let b=0;b<l.length;++b)l[b]=e(r[b%r.length],s[b%s.length]);else for(let b=0;b<l.length;++b){let y=w.indexToLoc(b,i,u),v=y.slice(-p);g.forEach(N=>v[N]=0);let x=w.locToIndex(v,p,h),k=y.slice(-d);m.forEach(N=>k[N]=0);let S=w.locToIndex(k,d,f);l[b]=e(r[x],s[S])}return[l,o]}}function Qn(e){let{inputs:t,backend:n}=e,{real:r,imag:s}=t,a=n.data.get(r.dataId).values,o=n.data.get(s.dataId).values,i=n.makeTensorInfo(r.shape,"complex64"),u=n.data.get(i.dataId);return u.
${a.shape}`);if(r.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${r.shape}`);if(s.shape.length!==1)throw new Error(`Values must be a vector, saw:
${s.shape}`);if(o.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
${o.shape}`);let i=n.data.get(r.dataId).values,u=n.data.get(s.dataId).values,c=n.data.get(a.dataId).values,l=n.data.get(o.dataId).values[0],[p,d,h,f,g]=EE(i,r.shape,r.dtype,u,s.dtype,c,l);return[n.makeTensorInfo(d,r.dtype,p),n.makeTensorInfo([d[0]],s.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(m=>Number(m)))),n.makeTensorInfo([g.length],r.dtype,new Int32Array(g))]}var oZ={kernelName:Yd,backendName:"cpu",kernelFunc:aZ};function iZ(e){let{inputs:t,backend:n}=e,{inputIndices:r,inputShape:s,newShape:a}=t;if(r.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape
${r.shape}`);if(s.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=Array.from(n.data.get(s.dataId).values),i=n.data.get(r.dataId).values,u=Array.from(n.data.get(a.dataId).values),[c,l,p]=AE(i,r.shape,r.dtype,o,u);return[n.makeTensorInfo(l,r.dtype,c),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var uZ={kernelName:rl,backendName:"cpu",kernelFunc:iZ};function cZ(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);if(s.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let o=n.data.get(r.dataId).values,i=n.data.get(s.dataId).values,u=n.data.get(a.dataId).values,[c,l]=Yk(o,r.shape,r.dtype,i,u,!0);return n.makeTensorInfo(l,r.dtype,c)}var lZ={kernelName:Zd,backendName:"cpu",kernelFunc:cZ};function dZ(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);if(s.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let o=n.data.get(r.dataId).values,i=n.data.get(s.dataId).values,u=n.data.get(a.dataId).values,[c,l]=Yk(o,r.shape,r.dtype,i,u);return n.makeTensorInfo(l,r.dtype,c)}var pZ={kernelName:Jd,backendName:"cpu",kernelFunc:dZ};function hZ(e){let{inputs:t,backend:n,attrs:r}=e,{sparseIndices:s,sparseValues:a,defaultValue:o}=t,{outputShape:i}=r,{sliceRank:u,numUpdates:c,sliceSize:l,strides:p,outputSize:d}=T.calculateShapes(a,s,i),h=!1,f=n.bufferSync(s),g;switch(a.dtype){case"bool":{let m=n.bufferSync(a),b=!!n.data.get(o.dataId).values[0];g=so(f,m,i,d,l,c,u,p,b,h);break}case"float32":{let m=n.bufferSync(a),b=n.data.get(o.dataId).values[0];g=so(f,m,i,d,l,c,u,p,b,h);break}case"int32":{let m=n.bufferSync(a),b=n.data.get(o.dataId).values[0];g=so(f,m,i,d,l,c,u,p,b,h);break}case"string":{let m=n.bufferSync(a),b=w.decodeString(n.data.get(o.dataId).values[0]);g=so(f,m,i,d,l,c,u,p,b,h);break}default:throw new Error(`Unsupported type ${a.dtype}`)}return n.makeTensorInfo(i,g.dtype,g.values)}var fZ={kernelName:sl,backendName:"cpu",kernelFunc:hZ};function mZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=r,i=w.parseAxisParam(o,s.shape)[0],u=T.prepareSplitSize(s,a,i),c=new Array(s.shape.length).fill(0),l=s.shape.slice();return u.map(p=>{let d=[...l];d[i]=p;let h=ko({inputs:{x:s},backend:n,attrs:{begin:c,size:d}});return c[i]+=p,h})}var gZ={kernelName:nl,backendName:"cpu",kernelFunc:mZ},bZ={kernelName:Qd,backendName:"cpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,r=t;be(n,"square");let s=r.data.get(n.dataId).values,a=new Float32Array(s.length);for(let i=0;i<s.length;++i){let u=s[i];a[i]=u*u}return{dataId:r.write(a,n.shape,n.dtype),shape:n.shape,dtype:n.dtype}}},yZ=ct(Na,(e,t)=>{let n=t;return isNaN(e)?NaN:e>0?1:n.alpha}),vZ={kernelName:Na,backendName:"cpu",kernelFunc:yZ};function xZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,end:o,strides:i,beginMask:u,endMask:c,ellipsisMask:l,newAxisMask:p,shrinkAxisMask:d}=r;be(s,"stridedSlice");let{finalShapeSparse:h,finalShape:f,isIdentity:g,sliceDim0:m,isSimpleSlice:b,begin:y,end:v,strides:x}=Kt.sliceInfo(s.shape,a,o,i,u,c,l,p,d),k;if(g)k=yt({inputs:{x:s},backend:n,attrs:{shape:f}});else if(m||b){w.assert(s.shape.length>=1,()=>`Input must have rank at least 1, got: ${s.shape.length}`);let S=Kt.computeOutShape(y,v,x),N=ko({inputs:{x:s},backend:n,attrs:{begin:y,size:S}});k=yt({inputs:{x:N},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(N)}else{let S=n.bufferSync(s),N=FE(h,S,x,y);k=n.makeTensorInfo(f,N.dtype,N.values)}return k}var wZ={kernelName:al,backendName:"cpu",kernelFunc:xZ};function IZ(e){let{inputs:t,backend:n,attrs:r}=e,{separator:s,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:c}=r,{data:l,dataSplits:p}=t,d=n.data.get(l.dataId).values,h=n.data.get(p.dataId).values,[f,g]=Zk(d,h,s,a,o,i,u,c);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",g)]}var kZ={kernelName:tp,backendName:"cpu",kernelFunc:IZ};function SZ(e){let{inputs:t,backend:n,attrs:r}=e,{skipEmpty:s}=r,{input:a,delimiter:o}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(o.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${o.shape}`);let i=n.data.get(a.dataId).values,u=n.data.get(o.dataId).values[0],[c,l,p]=Jk(i,u,s),d=l.length;return[n.makeTensorInfo([d,2],"int32",c),n.makeTensorInfo([d],"string",l),n.makeTensorInfo([2],"int32",new Int32Array(p))]}var CZ={kernelName:np,backendName:"cpu",kernelFunc:SZ};function TZ(e){let{inputs:t,backend:n,attrs:r}=e,{numBuckets:s}=r,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(s<=0)throw new Error("Number of buckets must be at least 1");let o=n.data.get(a.dataId).values,i=Qk(o,s);return n.makeTensorInfo(a.shape,"int32",i)}var NZ={kernelName:rp,backendName:"cpu",kernelFunc:TZ},_Z=ct(Hi,e=>Math.tan(e)),EZ={kernel
`),a=s.length.toString().length+2,o=s.map((p,d)=>w.rightPad((d+1).toString(),a)+p),i=0;for(let p=0;p<o.length;p++)i=Math.max(o[p].length,i);let u=o.slice(0,r-1),c=o.slice(r-1,r),l=o.slice(r);console.log(u.join(`
2021-09-11 17:11:38 +02:00
`)),console.log(t.split(`
2024-01-17 22:44:24 +01:00
`)[0]),console.log(`%c ${w.rightPad(c[0],i)}`,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(l.join(`
`))}function oA(e){return Ls(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function iA(e,t){if(he(e,()=>e.linkProgram(t)),!G().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 Zh(e,t){if(he(e,()=>e.validateProgram(t)),e.getProgramParameter(t,e.VALIDATE_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Shader program validation failed.")}function uA(e,t){let n=Ls(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return he(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),he(e,()=>e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function cA(e,t){let n=Ls(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return he(e,()=>e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,n)),he(e,()=>e.bufferData(e.ELEMENT_ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function iJ(){return G().getNumber("WEBGL_VERSION")===2?1:4}function lA(e){return Ls(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function dA(e,t){let n=G().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e<=0||t<=0){let r=`[${e}x${t}]`;throw new Error("Requested texture size "+r+" is invalid.")}if(e>n||t>n){let r=`[${e}x${t}]`,s=`[${n}x${n}]`;throw new Error("Requested texture size "+r+" greater than WebGL maximum on this browser / GPU "+s+".")}}function pA(e){return Ls(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function bx(e,t,n,r,s,a,o){let i=e.getAttribLocation(t,n);return i===-1?!1:(he(e,()=>e.bindBuffer(e.ARRAY_BUFFER,r)),he(e,()=>e.vertexAttribPointer(i,s,e.FLOAT,!1,a,o)),he(e,()=>e.enableVertexAttribArray(i)),!0)}function hA(e,t,n){yA(e,n),he(e,()=>e.activeTexture(e.TEXTURE0+n)),he(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function uJ(e,t){yA(e,t),he(e,()=>e.activeTexture(e.TEXTURE0+t)),he(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function fA(e,t,n){return Ls(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function mA(e,t,n){return e.getUniformLocation(t,n)}function gA(e,t,n,r){he(e,()=>hA(e,t,r)),he(e,()=>e.uniform1i(n,r))}function cJ(e){he(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,null)),he(e,()=>e.viewport(0,0,e.canvas.width,e.canvas.height)),he(e,()=>e.scissor(0,0,e.canvas.width,e.canvas.height))}function Jh(e,t,n){he(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,n)),he(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0))}function yx(e,t){he(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,t)),he(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0))}function dd(e){let t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+bA(e,t))}function bA(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 Ls(e,t,n){let r=he(e,()=>t());if(r==null)throw new Error(n);return r}function yA(e,t){let n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=t+e.TEXTURE0;if(r<e.TEXTURE0||r>n){let s=`[gl.TEXTURE0, gl.TEXTURE${n}]`;throw new Error(`textureUnit must be in ${s}.`)}}function So(e,t=2){return w.sizeFromShape(e.slice(0,e.length-t))}function Co(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 pd(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[So(e),...Co(e)]),t}function vA(e,t=!1){let n=G().getNumber("WEBGL_MAX_TEXTURE_SIZE"),r=G().getNumber("WEBGL_MAX_SIZE_FOR_NARROW_TEXTURE");r===1/0&&G().getBool("WEBGL_AUTO_SQUARIFY_NARROW_TEXTURE_SHAPE")&&(r=n/2),t&&(n=n*2,r=r*2,e=e.map((i,u)=>u>=e.length-2?w.nearestLargerEven(e[u]):e[u]),e.length===1&&(e=[2,e[0]])),e.length!==2&&(e=w.squeezeShape(e).newShape);let s=w.sizeFromShape(e),a=null;e.length<=1&&s<=n?a=[1,s]:e.length===2&&e[0]<=n&&e[1]
2021-09-11 17:11:38 +02:00
bool isnan_custom(float val) {
2022-03-07 19:17:31 +01:00
uint floatToUint = floatBitsToUint(val);
return (floatToUint & 0x7fffffffu) > 0x7f800000u;
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +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));
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
#define isnan(value) isnan_custom(value)
2024-01-17 22:44:24 +01:00
`:"",u="",c=`
2021-09-11 17:11:38 +02:00
#define round(value) newRound(value)
int newRound(float value) {
return int(floor(value + 0.5));
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
ivec4 newRound(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
2021-06-04 14:54:48 +02:00
}
2024-01-17 22:44:24 +01:00
`):(e="",t="attribute",n="varying",r="varying",s="texture2D",a="gl_FragColor",o="",i=`
2021-09-11 17:11:38 +02:00
#define isnan(value) isnan_custom(value)
bool isnan_custom(float val) {
return (val > 0. || val < 1. || val == 0.) ? false : true;
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
bvec4 isnan_custom(vec4 val) {
return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));
2021-06-04 14:54:48 +02:00
}
2024-01-17 22:44:24 +01:00
`,u=`
2021-09-11 17:11:38 +02:00
uniform float INFINITY;
bool isinf(float val) {
return abs(val) == INFINITY;
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
bvec4 isinf(vec4 val) {
return equal(abs(val), vec4(INFINITY));
2021-06-04 14:54:48 +02:00
}
2024-01-17 22:44:24 +01:00
`,c=`
2021-09-11 17:11:38 +02:00
int round(float value) {
return int(floor(value + 0.5));
}
ivec4 round(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
}
2024-01-17 22:44:24 +01:00
`),{version:e,attribute:t,varyingVs:n,varyingFs:r,texture2D:s,output:a,defineOutput:o,defineSpecialNaN:i,defineSpecialInf:u,defineRound:c}}function eu(e,t,n="index"){let r=w.computeStrides(t);return r.map((s,a)=>{let o=`int ${e[a]} = ${n} / ${s}`,i=a===r.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * ${s}`:`index -= ${e[a]} * ${s}`;return`${o}; ${i};`}).join("")}function eg(e,t,n="index"){let r=w.computeStrides(t);return r.map((s,a)=>{let o=`int ${e[a]} = ${n} / outShapeStrides[${a}]`,i=a===r.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * outShapeStrides[${a}]`:`index -= ${e[a]} * outShapeStrides[${a}]`;return`${o}; ${i};`}).join("")}function hJ(e,t){let n=e.length,r=e.map(a=>`${t}[${a}]`),s=new Array(n-1);s[n-2]=r[n-1];for(let a=n-3;a>=0;--a)s[a]=`(${s[a+1]} * ${r[a+1]})`;return s}function fJ(e,t,n="index"){let r=e.map((a,o)=>o),s=hJ(r,t);return s.map((a,o)=>{let i=`int ${e[o]} = ${n} / ${s[o]}`,u=o===s.length-1?`int ${e[o+1]} = ${n} - ${e[o]} * ${s[o]}`:`index -= ${e[o]} * ${s[o]}`;return`${i}; ${u};`}).join("")}function i0(e){let t=w.computeStrides(e).map(n=>n.toString());return`
2021-09-11 17:11:38 +02:00
int getFlatIndex(ivec3 coords) {
return coords.x * ${t[0]} + coords.y * ${t[1]} + coords.z;
}
2024-01-17 22:44:24 +01:00
`}function u0(){return`
2021-09-11 17:11:38 +02:00
int getFlatIndex(ivec3 coords) {
return coords.x * outShapeStrides[0] + coords.y * outShapeStrides[1] + coords.z;
}
2024-01-17 22:44:24 +01:00
`}var TA=`
2021-09-11 17:11:38 +02:00
const float FLOAT_MAX = 1.70141184e38;
const float FLOAT_MIN = 1.17549435e-38;
lowp vec4 encode_float(highp float v) {
if (isnan(v)) {
return vec4(255, 255, 255, 255);
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
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;
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
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;
}
2024-01-17 22:44:24 +01:00
`,{getBroadcastDims:NA}=T;function mJ(e,t,n){let r=[];if(e.forEach(h=>{let f=w.sizeFromShape(h.shapeInfo.logicalShape);if(h.shapeInfo.isUniform?r.push(`uniform float ${h.name}${f>1?`[${f}]`:""};`):(r.push(`uniform sampler2D ${h.name};`),r.push(`uniform int offset${h.name};`)),n.enableShapeUniforms){let{uniformShape:g}=c0(n.packedInputs,h.shapeInfo.logicalShape,h.shapeInfo.texShape);switch(g.length){case 1:r.push(`uniform int ${h.name}Shape;`);break;case 2:r.push(`uniform ivec2 ${h.name}Shape;`);break;case 3:r.push(`uniform ivec3 ${h.name}Shape;`);break;case 4:r.push(`uniform ivec4 ${h.name}Shape;`);break;default:break}r.push(`uniform ivec2 ${h.name}TexShape;`)}}),n.enableShapeUniforms){switch(t.logicalShape.length){case 1:r.push("uniform int outShape;");break;case 2:r.push("uniform ivec2 outShape;"),r.push("uniform int outShapeStrides;");break;case 3:r.push("uniform ivec3 outShape;"),r.push("uniform ivec2 outShapeStrides;");break;case 4:r.push("uniform ivec4 outShape;"),r.push("uniform ivec3 outShapeStrides;");break;default:break}r.push("uniform ivec2 outTexShape;")}n.customUniforms&&n.customUniforms.forEach(h=>{r.push(`uniform ${h.type} ${h.name}${h.arrayIndex?`[${h.arrayIndex}]`:""};`)});let s=r.join(`
`),a=e.map(h=>gJ(h,t,n.packedInputs,n.enableShapeUniforms)).join(`
`),o=t.texShape,i=An(),u=vJ(i),c,l,p=IJ(i);return t.isPacked?(c=bJ(t.logicalShape,o,n.enableShapeUniforms),l=wJ(i)):(c=yJ(t.logicalShape,o,n.enableShapeUniforms),l=xJ(i)),n.packedInputs&&(p+=TJ),[p,u,l,s,c,a,n.userCode].join(`
`)}function Il(e,t=!1){let n=e.shapeInfo.logicalShape;switch(n.length){case 0:return LJ(e,t);case 1:return zJ(e,t);case 2:return VJ(e,t);case 3:return GJ(e,t);case 4:return jJ(e,t);case 5:return qJ(e);case 6:return KJ(e);default:throw new Error(`${n.length}-D input sampling is not yet supported`)}}function _A(e,t){switch(e.shapeInfo.logicalShape.length){case 0:return MJ(e);case 1:return BJ(e,t);case 2:return WJ(e,t);case 3:return UJ(e,t);default:return HJ(e,t)}}function gJ(e,t,n=!1,r){let s="";n?s+=_A(e,r):s+=Il(e,r);let a=e.shapeInfo.logicalShape,o=t.logicalShape;return a.length<=o.length&&(n?s+=XJ(e,t):s+=YJ(e,t)),s}function bJ(e,t,n){switch(e.length){case 0:return EA();case 1:return NJ(e,t,n);case 2:return PJ(e,t,n);case 3:return EJ(e,t,n);default:return DJ(e,t,n)}}function yJ(e,t,n){switch(e.length){case 0:return EA();case 1:return _J(e,t,n);case 2:return OJ(e,t,n);case 3:return AJ(e,t,n);case 4:return $J(e,t,n);case 5:return FJ(e,t);case 6:return RJ(e,t);default:throw new Error(`${e.length}-D output sampling is not yet supported`)}}function vJ(e){return`
2021-09-11 17:11:38 +02:00
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${e.texture2D}(textureSampler, uv).r;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function xJ(e){return`
2021-09-11 17:11:38 +02:00
void setOutput(float val) {
${e.output} = vec4(val, 0, 0, 0);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function wJ(e){return`
2021-09-11 17:11:38 +02:00
void setOutput(vec4 val) {
${e.output} = val;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function IJ(e){return`${e.version}
2021-09-11 17:11:38 +02:00
precision highp float;
precision highp int;
precision highp sampler2D;
${e.varyingFs} vec2 resultUV;
${e.defineOutput}
const vec2 halfCR = vec2(0.5, 0.5);
struct ivec5
{
int x;
int y;
int z;
int w;
int u;
};
struct ivec6
{
int x;
int y;
int z;
int w;
int u;
int v;
};
uniform float NAN;
${e.defineSpecialNaN}
${e.defineSpecialInf}
${e.defineRound}
int imod(int x, int y) {
return x - y * (x / y);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
int idiv(int a, int b, float sign) {
int res = a / b;
int mod = imod(a, b);
if (sign < 0. && mod != 0) {
res -= 1;
}
return res;
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
//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);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
${kJ}
${SJ}
${CJ}
`}var kJ=`
2021-09-11 17:11:38 +02:00
vec2 uvFromFlat(int texNumR, int texNumC, int index) {
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 packedUVfrom1D(int texNumR, int texNumC, int index) {
int texelIndex = index / 2;
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2024-01-17 22:44:24 +01:00
`,SJ=`
2021-09-11 17:11:38 +02:00
vec2 packedUVfrom2D(int texelsInLogicalRow, int texNumR,
int texNumC, int row, int col) {
int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2);
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2024-01-17 22:44:24 +01:00
`,CJ=`
2021-09-11 17:11:38 +02:00
vec2 packedUVfrom3D(int texNumR, int texNumC,
int texelsInBatch, int texelsInLogicalRow, int b,
int row, int col) {
int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2);
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2024-01-17 22:44:24 +01:00
`,TJ=`
2021-09-11 17:11:38 +02:00
float getChannel(vec4 frag, vec2 innerDims) {
vec2 modCoord = mod(innerDims, 2.);
return modCoord.x == 0. ?
(modCoord.y == 0. ? frag.r : frag.g) :
(modCoord.y == 0. ? frag.b : frag.a);
}
float getChannel(vec4 frag, int dim) {
float modCoord = mod(float(dim), 2.);
return modCoord == 0. ? frag.r : frag.g;
}
2024-01-17 22:44:24 +01:00
`;function EA(){return`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return 0;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function NJ(e,t,n){let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return r[0]===1?n?`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return 2 * int(resultUV.x * ceil(float(outTexShape[1]) / 2.0));
}
`:`
int getOutputCoords() {
2024-01-17 22:44:24 +01:00
return 2 * int(resultUV.x * ${r[1]}.0);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`:r[1]===1?n?`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return 2 * int(resultUV.y * ceil(float(outTexShape[0]) / 2.0));
}
`:`
int getOutputCoords() {
2024-01-17 22:44:24 +01:00
return 2 * int(resultUV.y * ${r[0]}.0);
2021-09-11 17:11:38 +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);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
2024-01-17 22:44:24 +01:00
vec2(${r[0]}, ${r[1]}));
return 2 * (resTexRC.x * ${r[1]} + resTexRC.y);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function _J(e,t,n){return t[0]===1?n?`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return int(resultUV.x * float(outTexShape[1]));
}
`:`
int getOutputCoords() {
return int(resultUV.x * ${t[1]}.0);
}
`:t[1]===1?n?`
int getOutputCoords() {
return int(resultUV.y * float(outTexShape[0]));
}
`:`
int getOutputCoords() {
return int(resultUV.y * ${t[0]}.0);
}
`:n?`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
return resTexRC.x * outTexShape[1] + resTexRC.y;
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
return resTexRC.x * ${t[1]} + resTexRC.y;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function EJ(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[2]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec3(b, r, c);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`;let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],s=Math.ceil(e[2]/2),a=s*Math.ceil(e[1]/2);return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
2024-01-17 22:44:24 +01:00
vec2(${r[0]}, ${r[1]}));
int index = resTexRC.x * ${r[1]} + resTexRC.y;
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
int b = index / ${a};
index -= b * ${a};
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
int r = 2 * (index / ${s});
int c = imod(index, ${s}) * 2;
2021-09-11 17:11:38 +02:00
return ivec3(b, r, c);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function AJ(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
2024-01-17 22:44:24 +01:00
${eg(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
}
2024-01-17 22:44:24 +01:00
`;let r=eu(["r","c","d"],e);return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
2024-01-17 22:44:24 +01:00
${r}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function DJ(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec4 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int texelsInLogicalRow = int(ceil(float(outShape[3]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[2]) / 2.0));
int texelsInBatchN = texelsInBatch * outShape[1];
int b2 = index / texelsInBatchN;
index -= b2 * texelsInBatchN;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec4(b2, b, r, c);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`;let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],s=Math.ceil(e[e.length-1]/2),a=s*Math.ceil(e[e.length-2]/2),o=a,i="",u="b, r, c";for(let c=2;c<e.length-1;c++)o*=e[e.length-c-1],i=`
int b${c} = index / ${o};
index -= b${c} * ${o};
`+i,u=`b${c}, `+u;return`
2021-09-11 17:11:38 +02:00
ivec${e.length} getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
2024-01-17 22:44:24 +01:00
vec2(${r[0]}, ${r[1]}));
int index = resTexRC.x * ${r[1]} + resTexRC.y;
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
${i}
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
int b = index / ${a};
index -= b * ${a};
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
int r = 2 * (index / ${s});
int c = imod(index, ${s}) * 2;
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
return ivec${e.length}(${u});
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function $J(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
2024-01-17 22:44:24 +01:00
${eg(["r","c","d","d2"],e)}
2021-09-11 17:11:38 +02:00
return ivec4(r, c, d, d2);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`;let r=eu(["r","c","d","d2"],e);return`
2021-09-11 17:11:38 +02:00
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
2024-01-17 22:44:24 +01:00
${r}
2021-09-11 17:11:38 +02:00
return ivec4(r, c, d, d2);
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function FJ(e,t){let n=eu(["r","c","d","d2","d3"],e);return`
2021-09-11 17:11:38 +02:00
ivec5 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(${t[0]},
${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${n}
ivec5 outShape = ivec5(r, c, d, d2, d3);
return outShape;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function RJ(e,t){let n=eu(["r","c","d","d2","d3","d4"],e);return`
2021-09-11 17:11:38 +02:00
ivec6 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${n}
ivec6 result = ivec6(r, c, d, d2, d3, d4);
return result;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:44:24 +01:00
`}function PJ(e,t,n){let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];if(w.arraysEqual(e,t))return n?`
2021-09-11 17:11:38 +02:00
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
return 2 * ivec2(resultUV.yx * vec2(packedTexShape[0], packedTexShape[1]));
}
`:`
ivec2 getOutputCoords() {
2024-01-17 22:44:24 +01:00
return 2 * ivec2(resultUV.yx * vec2(${r[0]}, ${r[1]}));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let s=Math.ceil(e[1]/2);return n?`
2021-09-11 17:11:38 +02:00
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);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
2024-01-17 22:44:24 +01:00
vec2(${r[0]}, ${r[1]}));
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
int index = resTexRC.x * ${r[1]} + resTexRC.y;
int r = 2 * (index / ${s});
int c = imod(index, ${s}) * 2;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return ivec2(r, c);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}function OJ(e,t,n){return w.arraysEqual(e,t)?n?`
2021-09-11 17:11:38 +02:00
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(outTexShape[0], outTexShape[1]));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(${t[0]}, ${t[1]}));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +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);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +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);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +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);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +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);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +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);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
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);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}function tu(e){return`offset${e}`}function MJ(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),r=An();return`
2021-09-11 17:11:38 +02:00
vec4 ${n}() {
2024-01-17 22:44:24 +01:00
return ${r.texture2D}(${t}, halfCR);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}function LJ(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`float ${r}() {return ${n};}`;let[s,a]=e.shapeInfo.texShape;if(s===1&&a===1)return`
float ${r}() {
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, halfCR);
}
2024-01-17 22:44:24 +01:00
`;let o=tu(n);if(t)return`
float ${r}() {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], ${o});
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;let[i,u]=e.shapeInfo.texShape;return`
float ${r}() {
vec2 uv = uvFromFlat(${i}, ${u}, ${o});
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}function BJ(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),s=e.shapeInfo.texShape,a=An();if(t)return`
vec4 ${r}(int index) {
2021-09-11 17:11:38 +02:00
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);
2024-01-17 22:44:24 +01:00
return ${a.texture2D}(${n}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;let o=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)];return`
vec4 ${r}(int index) {
2021-09-11 17:11:38 +02:00
vec2 uv = packedUVfrom1D(
2024-01-17 22:44:24 +01:00
${o[0]}, ${o[1]}, index);
return ${a.texture2D}(${n}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function zJ(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`
float ${r}(int index) {
${kl(e)}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let s=e.shapeInfo.texShape,a=s[0],o=s[1];if(o===1&&a===1)return`
float ${r}(int index) {
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, halfCR);
}
2024-01-17 22:44:24 +01:00
`;let i=tu(n);return o===1?t?`
float ${r}(int index) {
vec2 uv = vec2(0.5, (float(index + ${i}) + 0.5) / float(${n}TexShape[0]));
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
`:`
2024-01-17 22:44:24 +01:00
float ${r}(int index) {
vec2 uv = vec2(0.5, (float(index + ${i}) + 0.5) / ${a}.0);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`:a===1?t?`
float ${r}(int index) {
vec2 uv = vec2((float(index + ${i}) + 0.5) / float(${n}TexShape[1]), 0.5);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
`:`
2024-01-17 22:44:24 +01:00
float ${r}(int index) {
vec2 uv = vec2((float(index + ${i}) + 0.5) / ${o}.0, 0.5);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
`:t?`
2024-01-17 22:44:24 +01:00
float ${r}(int index) {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], index + ${i});
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
`:`
2024-01-17 22:44:24 +01:00
float ${r}(int index) {
vec2 uv = uvFromFlat(${a}, ${o}, index + ${i});
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`}function WJ(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape,o=a[0],i=a[1],u=An();if(a!=null&&w.arraysEqual(n,a))return t?`
vec4 ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
vec4 ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${i}.0, ${o}.0);
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`;if(t)return`
2024-01-17 22:44:24 +01:00
vec4 ${s}(int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${r}TexShape[0]) / 2.0), ceil(float(${r}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${r}Shape[1]) / 2.0));
2021-09-11 17:11:38 +02:00
vec2 uv = packedUVfrom2D(valuesPerRow, packedTexShape[0], packedTexShape[1], row, col);
2024-01-17 22:44:24 +01:00
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let c=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],l=Math.ceil(n[1]/2);return`
vec4 ${s}(int row, int col) {
vec2 uv = packedUVfrom2D(${l}, ${c[0]}, ${c[1]}, row, col);
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function VJ(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape;if(a!=null&&w.arraysEqual(n,a)){if(t)return`
float ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
}
`;let d=a[0],h=a[1];return`
float ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${h}.0, ${d}.0);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}let{newShape:o,keptDims:i}=w.squeezeShape(n),u=o;if(u.length<n.length){let d=Sl(e,u),h=["row","col"];return`
${Il(d,t)}
float ${s}(int row, int col) {
return ${s}(${Cl(h,i)});
2021-09-11 17:11:38 +02:00
}
`}if(e.shapeInfo.isUniform)return`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col) {
2021-09-11 17:11:38 +02:00
int index = round(dot(vec2(row, col), vec2(${n[1]}, 1)));
2024-01-17 22:44:24 +01:00
${kl(e)}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let c=a[0],l=a[1],p=tu(r);return l===1?t?`
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${r}Shape[1], 1, 1));
vec2 uv = vec2(0.5, (index + 0.5) / float(${r}TexShape[0]));
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${n[1]}, 1, 1));
vec2 uv = vec2(0.5, (index + 0.5) / ${c}.0);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`:c===1?t?`
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${r}Shape[1], 1, 1));
vec2 uv = vec2((index + 0.5) / float(${r}TexShape[1]), 0.5);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${p}), vec3(${n[1]}, 1, 1));
vec2 uv = vec2((index + 0.5) / ${l}.0, 0.5);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:t?`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int index = row * ${r}Shape[1] + col + ${p};
vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int index = row * ${n[1]} + col + ${p};
vec2 uv = uvFromFlat(${c}, ${l}, index);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function UJ(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape,o=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)];if(n[0]===1){let d=n.slice(1),h=[1,2],f=Sl(e,d),g=["b","row","col"];return`
${_A(f,t)}
vec4 ${s}(int b, int row, int col) {
return ${s}(${Cl(g,h)});
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}let i=An();if(t)return`
vec4 ${s}(int b, int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${r}TexShape[0]) / 2.0), ceil(float(${r}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${r}Shape[2]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${r}Shape[1]) / 2.0));
2021-09-11 17:11:38 +02:00
vec2 uv = packedUVfrom3D(
packedTexShape[0], packedTexShape[1], texelsInBatch, valuesPerRow, b, row, col);
2024-01-17 22:44:24 +01:00
return ${i.texture2D}(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;let u=o[0],c=o[1],l=Math.ceil(n[2]/2),p=l*Math.ceil(n[1]/2);return`
vec4 ${s}(int b, int row, int col) {
2021-09-11 17:11:38 +02:00
vec2 uv = packedUVfrom3D(
2024-01-17 22:44:24 +01:00
${u}, ${c}, ${p}, ${l}, b, row, col);
return ${i.texture2D}(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}function GJ(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=n[1]*n[2],o=n[2],{newShape:i,keptDims:u}=w.squeezeShape(n),c=i;if(c.length<n.length){let g=Sl(e,c),m=["row","col","depth"];return`
${Il(g,t)}
float ${s}(int row, int col, int depth) {
return ${s}(${Cl(m,u)});
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`}if(e.shapeInfo.isUniform)return`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth) {
2021-09-11 17:11:38 +02:00
int index = round(dot(vec3(row, col, depth),
2024-01-17 22:44:24 +01:00
vec3(${a}, ${o}, 1)));
${kl(e)}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;let l=e.shapeInfo.texShape,p=l[0],d=l[1],h=e.shapeInfo.flatOffset;if(d===a&&h==null)return t?`
float ${s}(int row, int col, int depth) {
int stride1 = ${r}Shape[2];
2021-09-11 17:11:38 +02:00
float texR = float(row);
float texC = dot(vec2(col, depth), vec2(stride1, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth) {
2021-09-11 17:11:38 +02:00
float texR = float(row);
2024-01-17 22:44:24 +01:00
float texC = dot(vec2(col, depth), vec2(${o}, 1));
2021-09-11 17:11:38 +02:00
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${d}.0, ${p}.0);
return sampleTexture(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;if(d===o&&h==null)return t?`
float ${s}(int row, int col, int depth) {
float texR = dot(vec2(row, col), vec2(${r}Shape[1], 1));
2021-09-11 17:11:38 +02:00
float texC = float(depth);
2024-01-17 22:44:24 +01:00
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth) {
2021-09-11 17:11:38 +02:00
float texR = dot(vec2(row, col), vec2(${n[1]}, 1));
float texC = float(depth);
2024-01-17 22:44:24 +01:00
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${d}.0, ${p}.0);
return sampleTexture(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;let f=tu(r);return t?`
float ${s}(int row, int col, int depth) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int stride0 = ${r}Shape[1] * ${r}Shape[2];
int stride1 = ${r}Shape[2];
int index = row * stride0 + col * stride1 + depth + ${f};
vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int index = row * ${a} + col * ${o} + depth + ${f};
vec2 uv = uvFromFlat(${p}, ${d}, index);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function HJ(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),s=An();if(t)return`
vec4 ${r}(int b2, int b, int row, int col) {
2021-09-11 17:11:38 +02:00
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];
2024-01-17 22:44:24 +01:00
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(packedTexShape[1], packedTexShape[0]); return ${s.texture2D}(${n}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let a=e.shapeInfo.logicalShape,o=a.length,i=e.shapeInfo.texShape,u=[Math.ceil(i[0]/2),Math.ceil(i[1]/2)],c=u[0],l=u[1],p=Math.ceil(a[o-1]/2),d=p*Math.ceil(a[o-2]/2),h="int b, int row, int col",f=`b * ${d} + (row / 2) * ${p} + (col / 2)`;for(let g=2;g<o-1;g++)h=`int b${g}, `+h,d*=a[o-g-1],f=`b${g} * ${d} + `+f;return`
vec4 ${r}(${h}) {
int index = ${f};
int texR = index / ${l};
int texC = index - texR * ${l};
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${l}, ${c});
return ${s.texture2D}(${n}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function jJ(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=n[3],o=n[2]*a,i=n[1]*o,{newShape:u,keptDims:c}=w.squeezeShape(n);if(u.length<n.length){let y=Sl(e,u),v=["row","col","depth","depth2"];return`
${Il(y,t)}
float ${s}(int row, int col, int depth, int depth2) {
return ${s}(${Cl(v,c)});
2021-09-11 17:11:38 +02:00
}
`}if(e.shapeInfo.isUniform)return`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth, int depth2) {
2021-09-11 17:11:38 +02:00
int index = round(dot(vec4(row, col, depth, depth2),
2024-01-17 22:44:24 +01:00
vec4(${i}, ${o}, ${a}, 1)));
${kl(e)}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let l=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1],f=`int stride2 = ${r}Shape[3];`,g=`int stride1 = ${r}Shape[2] * stride2;`,m=`int stride0 = ${r}Shape[1] * stride1;`;if(h===i&&l==null)return t?`
float ${s}(int row, int col, int depth, int depth2) {
2022-10-18 13:23:49 +02:00
${f}
2024-01-17 22:44:24 +01:00
${g}
2021-09-11 17:11:38 +02:00
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(stride1, stride2, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth, int depth2) {
2021-09-11 17:11:38 +02:00
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
2024-01-17 22:44:24 +01:00
vec3(${o}, ${a}, 1));
2021-09-11 17:11:38 +02:00
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${h}.0, ${d}.0);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;if(h===a&&l==null)return t?`
float ${s}(int row, int col, int depth, int depth2) {
2021-09-11 17:11:38 +02:00
float texR = dot(vec3(row, col, depth),
2024-01-17 22:44:24 +01:00
vec3(${r}Shape[1] * ${r}Shape[2], ${r}Shape[2], 1));
2021-09-11 17:11:38 +02:00
float texC = float(depth2);
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth, int depth2) {
2021-09-11 17:11:38 +02:00
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) /
2024-01-17 22:44:24 +01:00
vec2(${h}.0, ${d}.0);
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let b=tu(r);return t?`
float ${s}(int row, int col, int depth, int depth2) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2022-10-18 13:23:49 +02:00
${f}
2021-09-11 17:11:38 +02:00
${g}
2024-01-17 22:44:24 +01:00
${m}
2021-09-11 17:11:38 +02:00
int index = row * stride0 + col * stride1 +
depth * stride2 + depth2;
2024-01-17 22:44:24 +01:00
vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index + ${b});
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
2024-01-17 22:44:24 +01:00
float ${s}(int row, int col, int depth, int depth2) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int index = row * ${i} + col * ${o} +
depth * ${a} + depth2;
vec2 uv = uvFromFlat(${d}, ${h}, index + ${b});
return sampleTexture(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function qJ(e){let t=e.shapeInfo.logicalShape,n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),s=t[4],a=t[3]*s,o=t[2]*a,i=t[1]*o,{newShape:u,keptDims:c}=w.squeezeShape(t);if(u.length<t.length){let g=Sl(e,u),m=["row","col","depth","depth2","depth3"];return`
${Il(g)}
float ${r}(int row, int col, int depth, int depth2, int depth3) {
return ${r}(${Cl(m,c)});
2021-09-11 17:11:38 +02:00
}
`}if(e.shapeInfo.isUniform)return`
2024-01-17 22:44:24 +01:00
float ${r}(int row, int col, int depth, int depth2, int depth3) {
2021-09-11 17:11:38 +02:00
float index = dot(
vec4(row, col, depth, depth2),
2024-01-17 22:44:24 +01:00
vec4(${i}, ${o}, ${a}, ${s})) +
2021-09-11 17:11:38 +02:00
depth3;
2024-01-17 22:44:24 +01:00
${kl(e)}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let l=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1];if(h===i&&l==null)return`
float ${r}(int row, int col, int depth, int depth2, int depth3) {
2021-09-11 17:11:38 +02:00
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
2024-01-17 22:44:24 +01:00
vec4(${o}, ${a}, ${s}, 1));
2021-09-11 17:11:38 +02:00
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${h}.0, ${d}.0);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`;if(h===s&&l==null)return`
float ${r}(int row, int col, int depth, int depth2, int depth3) {
2021-09-11 17:11:38 +02:00
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) /
2024-01-17 22:44:24 +01:00
vec2(${h}.0, ${d}.0);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`;let f=tu(n);return`
float ${r}(int row, int col, int depth, int depth2, int depth3) {
2021-09-11 17:11:38 +02:00
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int index = row * ${i} + col * ${o} + depth * ${a} +
depth2 * ${s} + depth3 + ${f};
vec2 uv = uvFromFlat(${d}, ${h}, index);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`}function KJ(e){let t=e.shapeInfo.logicalShape,n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),{newShape:s,keptDims:a}=w.squeezeShape(t);if(s.length<t.length){let m=Sl(e,s),b=["row","col","depth","depth2","depth3","depth4"];return`
${Il(m)}
float ${r}(int row, int col, int depth,
2021-09-11 17:11:38 +02:00
int depth2, int depth3, int depth4) {
2024-01-17 22:44:24 +01:00
return ${r}(${Cl(b,a)});
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}let o=t[5],i=t[4]*o,u=t[3]*i,c=t[2]*u,l=t[1]*c;if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col, int depth,
2021-09-11 17:11:38 +02:00
int depth2, int depth3, int depth4) {
int index = round(dot(
vec4(row, col, depth, depth2),
2024-01-17 22:44:24 +01:00
vec4(${l}, ${c}, ${u}, ${i})) +
2021-09-11 17:11:38 +02:00
dot(
vec2(depth3, depth4),
2024-01-17 22:44:24 +01:00
vec2(${o}, 1)));
${kl(e)}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;let p=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,h=d[0],f=d[1];if(f===l&&p==null)return`
float ${r}(int row, int col, int depth,
2021-09-11 17:11:38 +02:00
int depth2, int depth3, int depth4) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
2024-01-17 22:44:24 +01:00
vec4(${c}, ${u}, ${i}, ${o})) +
2021-09-11 17:11:38 +02:00
float(depth4);
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${f}.0, ${h}.0);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`;if(f===o&&p==null)return`
float ${r}(int row, int col, int depth,
2021-09-11 17:11:38 +02:00
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) /
2024-01-17 22:44:24 +01:00
vec2(${f}.0, ${h}.0);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`;let g=tu(n);return`
float ${r}(int row, int col, int depth,
2021-09-11 17:11:38 +02:00
int depth2, int depth3, int depth4) {
// Explicitly use integer operations as dot() only works on floats.
2024-01-17 22:44:24 +01:00
int index = row * ${l} + col * ${c} + depth * ${u} +
depth2 * ${i} + depth3 * ${o} + depth4 + ${g};
vec2 uv = uvFromFlat(${h}, ${f}, index);
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
}
2024-01-17 22:44:24 +01:00
`}function kl(e){let t=e.name,n=w.sizeFromShape(e.shapeInfo.logicalShape);return n<2?`return ${t};`:`
2021-09-11 17:11:38 +02:00
for (int i = 0; i < ${n}; i++) {
if (i == index) {
return ${t}[i];
}
}
2024-01-17 22:44:24 +01:00
`}function XJ(e,t){let n=e.name,r=n.charAt(0).toUpperCase()+n.slice(1),s="get"+r+"AtOutCoords",a=e.shapeInfo.logicalShape.length,o=t.logicalShape.length,i=NA(e.shapeInfo.logicalShape,t.logicalShape),u=ht(o),c=o-a,l,p=["x","y","z","w","u","v"];a===0?l="":o<2&&i.length>=1?l="coords = 0;":l=i.map(y=>`coords.${p[y+c]} = 0;`).join(`
`);let d="";o<2&&a>0?d="coords":d=e.shapeInfo.logicalShape.map((y,v)=>`coords.${p[v+c]}`).join(", ");let h="return outputValue;",g=w.sizeFromShape(e.shapeInfo.logicalShape)===1,b=w.sizeFromShape(t.logicalShape)===1;if(a===1&&!g&&!b)h=`
2021-09-11 17:11:38 +02:00
return vec4(outputValue.xy, outputValue.xy);
2024-01-17 22:44:24 +01:00
`;else if(g&&!b)o===1?h=`
2021-09-11 17:11:38 +02:00
return vec4(outputValue.x, outputValue.x, 0., 0.);
`:h=`
return vec4(outputValue.x);
2024-01-17 22:44:24 +01:00
`;else if(i.length){let y=a-2,v=a-1;i.indexOf(y)>-1&&i.indexOf(v)>-1?h="return vec4(outputValue.x);":i.indexOf(y)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":i.indexOf(v)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return`
vec4 ${s}() {
${u} coords = getOutputCoords();
${l}
vec4 outputValue = get${r}(${d});
2021-09-11 17:11:38 +02:00
${h}
}
2024-01-17 22:44:24 +01:00
`}function YJ(e,t){let n=e.name,r=n.charAt(0).toUpperCase()+n.slice(1),s="get"+r+"AtOutCoords",a=t.texShape,o=e.shapeInfo.texShape,i=e.shapeInfo.logicalShape.length,u=t.logicalShape.length;if(!e.shapeInfo.isUniform&&i===u&&e.shapeInfo.flatOffset==null&&w.arraysEqual(o,a))return`
float ${s}() {
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, resultUV);
}
2024-01-17 22:44:24 +01:00
`;let c=ht(u),l=NA(e.shapeInfo.logicalShape,t.logicalShape),p=u-i,d,h=["x","y","z","w","u","v"];i===0?d="":u<2&&l.length>=1?d="coords = 0;":d=l.map(g=>`coords.${h[g+p]} = 0;`).join(`
`);let f="";return u<2&&i>0?f="coords":f=e.shapeInfo.logicalShape.map((g,m)=>`coords.${h[m+p]}`).join(", "),`
float ${s}() {
${c} coords = getOutputCoords();
${d}
return get${r}(${f});
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}function ht(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 c0(e,t,n){let{newShape:r,keptDims:s}=w.squeezeShape(t),a=t.length,o=e&&a===3&&t[0]===1,i=o?t.slice(1):r,u=!e&&a>1&&!w.arraysEqual(t,n)&&r.length<a||o;return{useSqueezeShape:u,uniformShape:u?i:t,keptDims:s}}function Sl(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function Cl(e,t){return t.map(n=>e[n]).join(", ")}function ZJ(e,t,n,r){let s=n.map((l,p)=>{let d={logicalShape:l.shape,texShape:l.isUniform?null:l.texData.texShape,isUniform:l.isUniform,isPacked:l.isUniform?!1:l.texData.isPacked,flatOffset:null};return l.texData!=null&&l.texData.slice!=null&&l.texData.slice.flatOffset>0&&(d.flatOffset=l.texData.slice.flatOffset),{name:t.variableNames[p],shapeInfo:d}}),a=s.map(l=>l.shapeInfo),o={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1,isPacked:r.texData.isPacked,flatOffset:null},i=mJ(s,o,t),u=aA(e.gl,i),c=e.createProgram(u);return G().get("ENGINE_COMPILE_ONLY")?{program:t,fragmentShader:u,source:i,webGLProgram:c,inShapeInfos:a,outShapeInfo:o,variablesLocations:null,customUniformLocations:null,infLoc:null,nanLoc:null,outShapeLocation:null,outShapeStridesLocation:null,outTexShapeLocation:null}:(e.buildVao(c),Object.assign({program:t,fragmentShader:u,source:i,webGLProgram:c,inShapeInfos:a,outShapeInfo:o},AA(e,t,c)))}function AA(e,t,n){let r=[],s=[],a,o,i,u=null,c=null;c=e.getUniformLocation(n,"NAN",!1),G().getNumber("WEBGL_VERSION")===1&&(u=e.getUniformLocation(n,"INFINITY",!1));let l=!1;for(let p of t.variableNames){let d={name:p,uniform:e.getUniformLocation(n,p,l),offset:e.getUniformLocation(n,`offset${p}`,l)};t.enableShapeUniforms&&(d.shape=e.getUniformLocation(n,`${p}Shape`,l),d.texShape=e.getUniformLocation(n,`${p}TexShape`,l)),r.push(d)}if(t.enableShapeUniforms&&(a=e.getUniformLocation(n,"outShape",l),i=e.getUniformLocation(n,"outShapeStrides",l),o=e.getUniformLocation(n,"outTexShape",l)),t.customUniforms)for(let p of t.customUniforms)s.push(e.getUniformLocation(n,p.name,l));return{variablesLocations:r,customUniformLocations:s,infLoc:u,nanLoc:c,outShapeLocation:a,outShapeStridesLocation:i,outTexShapeLocation:o}}function C1(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,r)=>{let s=n.logicalShape,a=t[r],o=a.shape;if(!w.arraysEqual(s,o))throw Error(`Binary was compiled with different shapes than the current args. Shapes ${s} and ${o} must match`);if(n.isUniform&&a.isUniform)return;let i=n.texShape,u=a.isUniform?null:a.texData.texShape;if(!w.arraysEqual(i,u))throw Error(`Binary was compiled with different texture shapes than the current args. Shape ${i} and ${u} must match`)})}function JJ(e,t,n,r,s){t.program.enableShapeUniforms||(C1(t.inShapeInfos,n),C1([t.outShapeInfo],[r]));let a=r.texData.texture,o=r.texData.texShape;r.texData.isPacked?e.setOutputPackedMatrixTexture(a.texture,o[0],o[1]):e.setOutputMatrixTexture(a.texture,o[0],o[1]),e.setProgram(t.webGLProgram),e.bindVertexArray(t.webGLProgram.vao),G().getNumber("WEBGL_VERSION")===1&&t.infLoc!==null&&e.gl.uniform1f(t.infLoc,1/0),t.nanLoc!==null&&e.gl.uniform1f(t.nanLoc,NaN);for(let u=0;u<n.length;++u){let c=n[u],{uniform:l,offset:p,shape:d,texShape:h}=t.variablesLocations[u];if(d){let{uniformShape:f}=c0(t.program.packedInputs,c.shape,c.texData.texShape);switch(f.length){case 1:e.gl.uniform1iv(d,new Int32Array(f));break;case 2:e.gl.uniform2iv(d,new Int32Array(f));break;case 3:e.gl.uniform3iv(d,new Int32Array(f));break;case 4:e.gl.uniform4iv(d,new Int32Array(f));break;default:break}}if(h&&e.gl.uniform2i(h,c.texData.texShape[0],c.texData.texShape[1]),l!=null){if(c.isUniform){if(w.sizeFromShape(c.shape)<2)e.gl.uniform1f(l,c.uniformValues[0]);else{let f=c.uniformValues;f instanceof Float32Array||(f=new Float32Array(f)),e.gl.uniform1fv(l,f)}continue}c.texData.slice!=null&&p!=null&&e.gl.uniform1i(p,c.texData.slice.flatOffset)
2021-09-11 17:11:38 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2024-01-17 22:44:24 +01:00
${this.enableShapeUniforms?eg(["r","c","d"],e):eu(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i=0; i<4; i++) {
int flatIndex = index + i;
ivec3 rc = outCoordsFromFlatIndex(flatIndex);
result[i] = getA(rc.x, rc.y, rc.z);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${t.output} = result;
}
2024-01-17 22:44:24 +01:00
`}},tQ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=Dd.DENSE,this.customUniforms=[{name:"texShape",type:"ivec2"}];let t=An();this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2024-01-17 22:44:24 +01:00
${this.enableShapeUniforms?eg(["r","c","d"],e):eu(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
${t.output} = result;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},nQ=class{constructor(e){this.variableNames=["A"],this.outTexUsage=lr.DOWNLOAD;let t=An();this.outputShape=e,this.userCode=`
${TA}
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
float x = getAAtOutCoords();
${t.output} = encode_float(x);
}
2024-01-17 22:44:24 +01:00
`}},rQ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=lr.DOWNLOAD;let t=An();this.outputShape=e,this.userCode=`
${TA}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
float x = getChannel(getAAtOutCoords(), vec2(coords.y, coords.z));
${t.output} = encode_float(x);
}
2024-01-17 22:44:24 +01:00
`}},sQ={R:0,G:1,B:2,A:3},T1=class{constructor(e,t=!1,n="RGBA"){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let r=An();this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let s="result";t&&(s="floor(result * 255. + 0.5)");let a="";for(let o=0;o<n.length;o++){let i=n[o];a+=`
if(offset == ${o}) {
result = values[${sQ[i]}];
2022-10-14 14:01:39 +02:00
}`}this.userCode=`
2024-01-17 22:44:24 +01:00
${this.enableShapeUniforms?u0():i0(e)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
int flatIndex = getFlatIndex(coords);
2022-10-14 14:01:39 +02:00
float result = 0.;
int offset = imod(flatIndex, ${n.length});
2021-04-01 19:39:54 +02:00
2022-10-14 14:01:39 +02:00
flatIndex = idiv(flatIndex, ${n.length}, 1.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int r = flatIndex / texShape[1];
2022-10-14 14:01:39 +02:00
if (r < texShape[0]) {
int c = imod(flatIndex, texShape[1]);
vec2 uv = (vec2(c, r) + halfCR) / vec2(texShape[1], texShape[0]);
2024-01-17 22:44:24 +01:00
vec4 values = ${r.texture2D}(A, uv);
${a}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
${r.output} = vec4(${s}, 0., 0., 0.);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}},aQ=class{constructor(e,t=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=An();this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let r="",s="result";t&&(s="floor(result * 255. + 0.5)");for(let a=0;a<=1;a++)for(let o=0;o<=1;o++){let i=a*2+o;r+=`
2021-09-11 17:11:38 +02:00
localCoords = coords;
2024-01-17 22:44:24 +01:00
if(localCoords[2] + ${o} < ${this.enableShapeUniforms?"outShape[2]":`${e[2]}`}) {
localCoords[2] += ${o};
if (localCoords[1] + ${a} < ${this.enableShapeUniforms?"outShape[1]":`${e[1]}`}) {
localCoords[1] += ${a};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
flatIndex = getFlatIndex(localCoords);
offset = imod(flatIndex, 4);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
flatIndex = idiv(flatIndex, 4, 1.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (offset == 0) {
2024-01-17 22:44:24 +01:00
result[${i}] = values[0];
2021-09-11 17:11:38 +02:00
} else if (offset == 1) {
2024-01-17 22:44:24 +01:00
result[${i}] = values[1];
2021-09-11 17:11:38 +02:00
} else if (offset == 2) {
2024-01-17 22:44:24 +01:00
result[${i}] = values[2];
2021-09-11 17:11:38 +02:00
} else {
2024-01-17 22:44:24 +01:00
result[${i}] = values[3];
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
`}this.userCode=`
2024-01-17 22:44:24 +01:00
${this.enableShapeUniforms?u0():i0(e)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
int flatIndex, r, c, offset;
ivec3 localCoords;
vec2 uv;
vec4 values;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${r}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${n.output} = ${s};
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},DA={};Ee(DA,{bindVertexProgramAttributeStreams:()=>zA,createBufferFromOutputTexture:()=>UA,createFloat16MatrixTexture:()=>OA,createFloat16PackedMatrixTexture:()=>BA,createFloat32MatrixTexture:()=>PA,createIndexBuffer:()=>RA,createPackedMatrixTexture:()=>LA,createUnsignedBytesMatrixTexture:()=>MA,createVertexBuffer:()=>FA,createVertexShader:()=>$A,downloadByteEncodedFloatMatrixFromOutputTexture:()=>HA,downloadFloat32MatrixFromBuffer:()=>GA,downloadMatrixFromPackedOutputTexture:()=>qA,downloadPackedMatrixFromBuffer:()=>jA,getInternalFormatForFloat16MatrixTexture:()=>d0,getInternalFormatForFloat16PackedMatrixTexture:()=>f0,getInternalFormatForFloat32MatrixTexture:()=>l0,getInternalFormatForPackedMatrixTexture:()=>h0,getInternalFormatForUnsignedBytesMatrixTexture:()=>p0,uploadDenseMatrixToTexture:()=>WA,uploadPixelDataToTexture:()=>VA});function $A(e){let t=An(),n=`${t.version}
2021-09-11 17:11:38 +02:00
precision highp float;
${t.attribute} vec3 clipSpacePos;
${t.attribute} vec2 uv;
${t.varyingVs} vec2 resultUV;
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
gl_Position = vec4(clipSpacePos, 1);
resultUV = uv;
2024-01-17 22:44:24 +01:00
}`;return sA(e,n)}function FA(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 uA(e,t)}function RA(e){let t=new Uint16Array([0,1,2,2,1,3]);return cA(e,t)}function Op(e,t,n,r,s,a){dA(t,n);let o=lA(e),i=e.TEXTURE_2D;return he(e,()=>e.bindTexture(i,o)),he(e,()=>e.texParameteri(i,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),he(e,()=>e.texParameteri(i,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),he(e,()=>e.texParameteri(i,e.TEXTURE_MIN_FILTER,e.NEAREST)),he(e,()=>e.texParameteri(i,e.TEXTURE_MAG_FILTER,e.NEAREST)),G().getNumber("WEBGL_VERSION")===1?he(e,()=>e.texImage2D(i,0,r,t,n,0,s,a,null)):he(e,()=>e.texStorage2D(i,1,r,t,n)),he(e,()=>e.bindTexture(e.TEXTURE_2D,null)),{texture:o,texShape:[n,t]}}function l0(e){return e.internalFormatFloat}function PA(e,t,n,r){let[s,a]=Pp(t,n);return Op(e,s,a,l0(r),r.textureFormatFloat,e.FLOAT)}function d0(e){return e.internalFormatHalfFloat}function OA(e,t,n,r){let[s,a]=Pp(t,n);return Op(e,s,a,d0(r),r.textureFormatFloat,r.textureTypeHalfFloat)}function p0(e){return e.downloadTextureFormat}function MA(e,t,n,r){let[s,a]=Pp(t,n);return Op(e,s,a,p0(r),e.RGBA,e.UNSIGNED_BYTE)}function h0(e){return e.internalFormatPackedFloat}function LA(e,t,n,r){let[s,a]=xl(t,n);return Op(e,s,a,h0(r),e.RGBA,e.FLOAT)}function f0(e){return e.internalFormatPackedHalfFloat}function BA(e,t,n,r){let[s,a]=xl(t,n);return Op(e,s,a,f0(r),e.RGBA,r.textureTypeHalfFloat)}function zA(e,t,n){return he(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),bx(e,t,"clipSpacePos",n,3,20,0)&&bx(e,t,"uv",n,2,20,12)}function WA(e,t,n,r,s,a){he(e,()=>e.bindTexture(e.TEXTURE_2D,t));let o,i,u;s instanceof Uint8Array?(o=new Uint8Array(n*r*4),i=e.UNSIGNED_BYTE,u=e.RGBA):(o=new Float32Array(n*r*4),i=e.FLOAT,u=a.internalFormatPackedFloat),o.set(s),G().getNumber("WEBGL_VERSION")===2?he(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,e.RGBA,i,o)):he(e,()=>e.texImage2D(e.TEXTURE_2D,0,u,n,r,0,e.RGBA,i,o)),he(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function VA(e,t,n){he(e,()=>e.bindTexture(e.TEXTURE_2D,t)),n.data instanceof Uint8Array?G().getNumber("WEBGL_VERSION")===2?he(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n.width,n.height,e.RGBA,e.UNSIGNED_BYTE,n.data)):he(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,n.width,n.height,0,e.RGBA,e.UNSIGNED_BYTE,n.data)):G().getNumber("WEBGL_VERSION")===2?he(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,e.RGBA,e.UNSIGNED_BYTE,n)):he(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)),he(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function UA(e,t,n,r){let s=e.createBuffer();he(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,s));let i=4*4*t*n;return he(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,i,e.STREAM_READ)),he(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,0)),he(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,null)),s}function GA(e,t,n){let r=e,s=new Float32Array(n);return r.bindBuffer(r.PIXEL_PACK_BUFFER,t),r.getBufferSubData(r.PIXEL_PACK_BUFFER,0,s),r.bindBuffer(r.PIXEL_PACK_BUFFER,null),s}function HA(e,t,n,r){let[s,a]=Pp(t,n),o=4,i=new Uint8Array(tJ(t*n,o));return he(e,()=>e.readPixels(0,0,s,a,r.downloadTextureFormat,e.UNSIGNED_BYTE,i)),new Float32Array(i.buffer)}function jA(e,t,n,r,s,a,o,i){let u=e,c=new Float32Array(nJ(a,o));return u.bindBuffer(u.PIXEL_PACK_BUFFER,t),u.getBufferSubData(u.PIXEL_PACK_BUFFER,0,c),u.bindBuffer(u.PIXEL_PACK_BUFFER,null),c}function qA(e,t,n){let r=new Float32Array(t*n*4);return he(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,r)),r}var tf=class{constructor(e){this.outputTexture=null,this.program=null,this.disposed=!1,this.itemsToPoll=[];let t=G().getNumber("WEBGL_VERSION");if(e!=null?(this.gl=e,tA(t,e)):this.gl=qr(t),e=this.gl,G().getNumber("WEBGL_VERSION")===2){let s=e;this.createVertexArray=()=>he(s,()=>s.createVertexArray()),this.bindVertexArray=a=>he(s,()=>s.bindVertexArray(a)),this.deleteVertexArray=a=>he(s,()=>s.deleteVertexArray(a)),this.getVertexArray=()=>he(s,()=>s.getParameter(s.VERTEX_ARRAY_BINDING))}else if(e!=null){let s=e.getExtension("OES_vertex_array_object");if(s==null)throw new Error("All WebGL1 implementations are expected to offer OES_vertex_array_object.");this.createVertexArray=()=>h
2021-09-11 17:11:38 +02:00
void main() {
setOutput(vec4(getA(), 0., 0., 0.));
}
2024-01-17 22:44:24 +01:00
`;else{let t=Cn("rc",this.rank),n=ht(this.rank),r=this.getOutOfBoundsCondition(t),s=this.getSetup(t),a=this.getOutput(t);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2021-12-09 20:22:22 +01:00
${n} rc = getOutputCoords();
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if(${r}) {
2021-09-11 17:11:38 +02:00
setOutput(vec4(0));
} else {
2024-01-17 22:44:24 +01:00
${s}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
setOutput(vec4(${a}));
2021-04-01 19:39:54 +02:00
}
}
2024-01-17 22:44:24 +01:00
`}}getSourceCoordsArr(e){let t=[];for(let n=0;n<=1;n++)for(let r=0;r<=1;r++){let s=`${n===0?"r":"rp1"}, ${r===0?"c":"cp1"}`;for(let a=2;a<this.rank;a++)s=`${e[e.length-1-a]},`+s;t.push(s)}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],r=this.enableShapeUniforms?`outShape[${this.rank} - 2]`:this.outputShape[this.rank-2];return`
2021-12-09 20:22:22 +01:00
int r = ${t[0]};
int c = ${t[1]};
int rp1 = r + 1;
int cp1 = c + 1;
bool cEdge = cp1 >= ${n};
2024-01-17 22:44:24 +01:00
bool rEdge = rp1 >= ${r};
2022-01-14 16:04:13 +01:00
`}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]}),
2021-12-09 20:22:22 +01:00
cEdge ? 0. : getA(${t[1]}),
rEdge ? 0. : getA(${t[2]}),
2024-01-17 22:44:24 +01:00
rEdge || cEdge ? 0. : getA(${t[3]})`}},JA=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec3"}],this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let n="";for(let r=0;r<4;r++){let s="thisRC = rc;";r%2===1&&(s+="thisRC.z += 1;"),r>1&&(s+="thisRC.y += 1;"),n+=`
${s}
${r>0?"if(thisRC.y < rows && thisRC.z < cols){":""}
2021-09-11 17:11:38 +02:00
int flatIndex = getFlatIndex(thisRC);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);
vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
result[${r}] =
2021-09-11 17:11:38 +02:00
getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);
2024-01-17 22:44:24 +01:00
${r>0?"}":""}
2021-09-11 17:11:38 +02:00
`}this.userCode=`
2024-01-17 22:44:24 +01:00
${eee(t,this.enableShapeUniforms)}
${this.enableShapeUniforms?u0():i0(e)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 rc = getOutputCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 thisRC;
int rows = ${this.enableShapeUniforms?"outShape[1]":e[1]};
int cols = ${this.enableShapeUniforms?"outShape[2]":e[2]};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${n}
2021-04-01 19:39:54 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}};function eee(e,t){return`
2021-09-11 17:11:38 +02:00
ivec3 inputCoordsFromReshapedOutCoords(int index) {
2024-01-17 22:44:24 +01:00
${t?fJ(["r","c","d"],"inputShape"):eu(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}var tee=class{constructor(e){this.gpgpu=e,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0,this.freeTextures={},this.usedTextures={},this.logEnabled=!1}acquireTexture(e,t,n){let r=_1(t,n),s=E1(e,r,n);s in this.freeTextures||(this.freeTextures[s]=[]),s in this.usedTextures||(this.usedTextures[s]=[]);let a=N1(e,r,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[s].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=a,this.log();let i=this.freeTextures[s].pop();return this.usedTextures[s].push(i),i}let o;return r===pn.PACKED_2X2_FLOAT32?o=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):r===pn.PACKED_2X2_FLOAT16?o=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):r===pn.UNPACKED_FLOAT32?o=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):r===pn.UNPACKED_FLOAT16?o=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):r===pn.PACKED_4X1_UNSIGNED_BYTE&&(o=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[s].push(o),this.numUsedTextures++,this._numBytesAllocated+=a,this.log(),o}releaseTexture(e,t,n,r){if(this.freeTextures==null)return;let s=_1(n,r),a=E1(t,s,r);a in this.freeTextures||(this.freeTextures[a]=[]);let o=N1(t,s,this.gpgpu.gl,this.gpgpu.textureConfig,r),i=G().getNumber("WEBGL_DELETE_TEXTURE_THRESHOLD");i!==-1&&this._numBytesAllocated>i?(this.gpgpu.deleteMatrixTexture(e.texture),this._numBytesAllocated-=o):(this.freeTextures[a].push(e),this.numFreeTextures++,this._numBytesFree+=o),this.numUsedTextures--;let u=this.usedTextures[a],c=u&&u.indexOf(e);if(c==null||c<0)throw new Error("Cannot release a texture that was never provided by this texture manager");u[c]=u[u.length-1],u.pop(),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 nee(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 N1(e,t,n,r,s){let a=ree(t,r),o;if(s){let[u,c]=xl(e[0],e[1]);o=u*c}else{let[u,c]=Pp(e[0],e[1]);o=u*c}let i=nee(n,a);return o*i}function ree(e,t){switch(e){case pn.PACKED_2X2_FLOAT32:return h0(t);case pn.PACKED_2X2_FLOAT16:return f0(t);case pn.UNPACKED_FLOAT32:return l0(t);case pn.UNPACKED_FLOAT16:return d0(t);case pn.PACKED_4X1_UNSIGNED_BYTE:return p0(t);default:throw new Error(`Unknown physical texture type ${e}`)}}function see(e){return G().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?pn.PACKED_2X2_FLOAT32:pn.UNPACKED_FLOAT32:e?pn.PACKED_2X2_FLOAT16:pn.UNPACKED_FLOAT16}function _1(e,t){if(e===lr.UPLOAD)return pn.PACKED_2X2_FLOAT32;if(e===lr.RENDER||e==null)return see(t);if(e===lr.DOWNLOAD||e===lr.PIXELS)return pn.PACKED_4X1_UNSIGNED_BYTE;throw new Error(`Unknown logical texture type ${e}`)}function E1(e,t,n){return`${e[0]}_${e[1]}_${t}_${n}`}var is=class{constructor(e,t){this.variableNames=["A"],this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
float unaryOperation(float x) {
${t}
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
float x = getAAtOutCoords();
float y = unaryOperation(x);
2021-09-11 17:11:38 +02:00
setOutput(y);
}
2024-01-17 22:44:24 +01:00
`}},Or="if (isnan(x)) return x;",aee="return x;",A1="return abs(x);",oee="return (x >= 0.0) ? x : (exp(x) - 1.0);",iee=Or+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : x;
2024-01-17 22:44:24 +01:00
`,uee=Or+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2024-01-17 22:44:24 +01:00
`,ta="return x;",cee="return 1.0 / (1.0 + exp(-1.0 * x));",lee="return x;",dee=`
2021-09-11 17:11:38 +02:00
vec4 result;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,pee=`
2021-09-11 17:11:38 +02:00
vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,hee=`
2021-09-11 17:11:38 +02:00
vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,fee="return 1.0 / (1.0 + exp(-1.0 * x));",oa=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
vec4 unaryOperation(vec4 x) {
${t}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
vec4 x = getAAtOutCoords();
vec4 y = unaryOperation(x);
setOutput(y);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},mee=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let t=e.length,n=Cn("rc",t),r=ht(t),s=JQ(t,n),a=n.slice(-2),o=t<=1?"rc":`vec2(${a.join(",")})`;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${r} rc = getOutputCoords();
vec4 packedInput = getA(${s});
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
setOutput(getChannel(packedInput, ${o}));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},gee=gs.whereImpl,bee=1e-7,yee=1e-4,jh={};function vee(e){return e in jh||(jh[e]={}),jh[e]}var xee=G().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),wee=600;function Iee(){return G().global.screen==null?1024:G().global.screen.height*G().global.screen.width*window.devicePixelRatio*wee/1024/1024}var g0=class QA extends Md{nextDataId(){return QA.nextDataId++}constructor(t){if(super(),this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.lastGlFlushTime=0,this.warnedAboutMemory=!1,this.pendingDeletes=0,this.disposed=!1,!G().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");let n;if(t!=null){if(t instanceof tf)n=t;else{let r=qr(G().getNumber("WEBGL_VERSION"),t);n=new tf(r)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{let r=qr(G().getNumber("WEBGL_VERSION"));n=new tf(r),this.binaryCache=vee(G().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=n,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new tee(this.gpgpu),this.numMBBeforeWarning=Iee(),this.texData=new Rf(this,Er())}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}writeTexture(t,n,r,s,a,o){let i=this.makeTensorInfo(n,r),u=this.texData.get(i.dataId);u.isPacked=!1,u.texture={texture:t,texShape:[s,a]},u.texShape=[s,a];let c=pd(n),l=new T1(c,!1,o),p=this.runWebGLProgram(l,[i],r,[[s,a]]);return p.shape=n,u.texture=null,this.disposeIntermediateTensorInfo(i),p.dataId}write(t,n,r){if((G().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||G().getBool("DEBUG"))&&this.checkNumericalProblems(t),r==="complex64"&&t!=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:n,dtype:r,values:t,usage:lr.UPLOAD,refCount:1}),s}refCount(t){return this.texData.has(t)?this.texData.get(t).refCount:0}incRef(t){let n=this.texData.get(t);n.refCount++}decRef(t){if(this.texData.has(t)){let n=this.texData.get(t);n.refCount--}}move(t,n,r,s,a){if(G().getBool("DEBUG")&&this.checkNumericalProblems(n),s==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(t,{shape:r,dtype:s,values:n,usage:lr.UPLOAD,refCount:a})}disposeIntermediateTensorInfo(t){this.disposeData(t.dataId)}readSync(t){let n=this.texData.get(t),{values:r,dtype:s,complexTensorInfos:a,slice:o,shape:i,isPacked:u}=n;if(o!=null){let d;u?d=new oa(i,ta):d=new is(i,ta);let h=this.runWebGLProgram(d,[{dataId:t,shape:i,dtype:s}],s),f=this.readSync(h.dataId);return this.disposeIntermediateTensorInfo(h),f}if(r!=null)return this.convertAndCacheOnCPU(t);if(s==="string")return r;let c=this.activeTimers!=null,l;c&&(l=w.now());let p;if(s==="complex64"){let d=this.readSync(a.real.dataId),h=this.readSync(a.imag.dataId);p=T.mergeRealAndImagArrays(d,h)}else p=this.getValuesFromTexture(t);return c&&(this.downloadWaitMs+=w.now()-l),this.convertAndCacheOnCPU(t,p)}async read(t){if(this.pendingRead.has(t)){let f=this.pendingRead.get(t);return new Promise(g=>f.push(g))}let n=this.texData.get(t),{values:r,shape:s,slice:a,dtype:o,complexTensorInfos:i,isPacked:u}=n;if(a!=null){let f;u?f=new oa(s,ta):f=new is(s,ta);let g=this.runWebGLProgram(f,[{dataId:t,shape:s,dtype:o}],o),m=this.read(g.dataId);return this.disposeIntermediateTensorInfo(g),m}if(r!=null)return this.convertAndCacheOnCPU(t);if(G().getBool("DEBUG")&&!G().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&G().getNumber("WEBGL_VERSION")===2)throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");let c=null,l;if(o!=="complex64"&&G().get("WEBGL_BUFFER_SUPPORTED")){l=this.decode(t);let f=this.texData.get(l.dataId);c=this.gpgpu.createBufferFromTexture(f.texture.texture,...Gh(s))}this.pendingRead.set(t,[]),o!=="complex64"&&await this.gpgpu.createAndWaitForFence();let p;if(o==="complex64"){let f=await Promise.all([this.read(i.real.dataId),this.read(i.imag.dataId)]),g=f[0],m=f[1];p=T.mergeRealAndImagArrays(g,m)}else if(c==null)p=this.getV
2021-09-11 17:11:38 +02:00
if (isnan(a)) return a;
if (isnan(b)) return b;
2024-01-17 22:44:24 +01:00
`,To=class{constructor(e,t,n){this.variableNames=["A","B"],this.outputShape=T.assertAndGetBroadcastShape(t,n),this.enableShapeUniforms=xn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
float binaryOperation(float a, float b) {
${e}
2021-04-01 19:39:54 +02:00
}
void main() {
2021-09-11 17:11:38 +02:00
float a = getAAtOutCoords();
float b = getBAtOutCoords();
setOutput(binaryOperation(a, b));
}
2024-01-17 22:44:24 +01:00
`}},nu=`
2022-10-09 19:40:33 +02:00
result.r = isNaN.r ? NAN : result.r;
result.g = isNaN.g ? NAN : result.g;
result.b = isNaN.b ? NAN : result.b;
result.a = isNaN.a ? NAN : result.a;
2024-01-17 22:44:24 +01:00
`,Tl=class{constructor(e,t,n,r=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=T.assertAndGetBroadcastShape(t,n);let s=this.outputShape.length;this.enableShapeUniforms=xn(s);let a="";if(r)if(s===0||w.sizeFromShape(this.outputShape)===1)a=`
2021-09-11 17:11:38 +02:00
result.y = 0.;
result.z = 0.;
result.w = 0.;
2024-01-17 22:44:24 +01:00
`;else if(a=`
${ht(s)} coords = getOutputCoords();
`,s===1)this.enableShapeUniforms?a+=`
2021-09-11 17:11:38 +02:00
result.y = (coords + 1) >= outShape ? 0. : result.y;
result.z = 0.;
result.w = 0.;
2024-01-17 22:44:24 +01:00
`:a+=`
2021-09-11 17:11:38 +02:00
result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
result.z = 0.;
result.w = 0.;
2024-01-17 22:44:24 +01:00
`;else{let i=Cn("coords",s);this.enableShapeUniforms?a+=`
2021-09-11 17:11:38 +02:00
bool nextRowOutOfBounds =
2024-01-17 22:44:24 +01:00
(${i[s-2]} + 1) >= outShape[${s} - 2];
2021-09-11 17:11:38 +02:00
bool nextColOutOfBounds =
2024-01-17 22:44:24 +01:00
(${i[s-1]} + 1) >= outShape[${s} - 1];
2021-09-11 17:11:38 +02:00
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
2024-01-17 22:44:24 +01:00
`:a+=`
2021-09-11 17:11:38 +02:00
bool nextRowOutOfBounds =
2024-01-17 22:44:24 +01:00
(${i[s-2]} + 1) >= ${this.outputShape[s-2]};
2021-09-11 17:11:38 +02:00
bool nextColOutOfBounds =
2024-01-17 22:44:24 +01:00
(${i[s-1]} + 1) >= ${this.outputShape[s-1]};
2021-09-11 17:11:38 +02:00
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`}this.userCode=`
vec4 binaryOperation(vec4 a, vec4 b) {
${e}
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
vec4 a = getAAtOutCoords();
vec4 b = getBAtOutCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = binaryOperation(a, b);
2024-01-17 22:44:24 +01:00
${a}
2021-04-01 19:39:54 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}};function sr(e){let{inputs:t,backend:n}=e,{x:r}=t;return n.incRef(r.dataId),{dataId:r.dataId,shape:r.shape,dtype:r.dtype}}var Tee={kernelName:si,backendName:"webgl",kernelFunc:sr};function Oa(e){let{inputs:t,backend:n}=e,{real:r,imag:s}=t,a=n.makeTensorInfo(r.shape,"complex64"),o=n.texData.get(a.dataId),i=sr({inputs:{x:r},backend:n}),u=sr({inputs:{x:s},backend:n});return o.complexTensorInfos={real:i,imag:u},a}var Nee={kernelName:Mf,backendName:"webgl",kernelFunc:Oa},tD="return (a < 0.) ? b * a : a;",nD=`
2021-09-11 17:11:38 +02:00
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
2024-01-17 22:44:24 +01:00
`;function _ee(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{alpha:a}=r,o=n.makeTensorInfo([],"float32",w.createScalarValue(a,"float32")),i=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Tl(nD,s.shape,o.shape):new To(tD,s.shape,o.shape),u=n.runWebGLProgram(i,[s,o],"float32");return n.disposeIntermediateTensorInfo(o),u}var Eee={kernelName:ui,backendName:"webgl",kernelFunc:_ee},rD="return (a < 0.) ? b * a : a;",sD=`
2021-09-11 17:11:38 +02:00
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
2024-01-17 22:44:24 +01:00
`;function Aee(e){let{inputs:t,backend:n}=e,{x:r,alpha:s}=t,a=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Tl(sD,r.shape,s.shape):new To(rD,r.shape,s.shape);return n.runWebGLProgram(a,[r,s],"float32")}var Dee={kernelName:Si,backendName:"webgl",kernelFunc:Aee},Nl="if (isnan(x)) return x;";function Ze({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:r}){return({inputs:s,backend:a})=>{let{x:o}=s,i=a,u=r||o.dtype;if(i.shouldExecuteOnCPU([o])&&n!=null){let p=i.texData.get(o.dataId),d=n(p.values,u);return i.makeTensorInfo(o.shape,u,d)}let c=G().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,l;return c?l=new oa(o.shape,t):l=new is(o.shape,e),i.runWebGLProgram(l,[o],u)}}function mn({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:r=!1,cpuKernelImpl:s,dtype:a}){return({inputs:o,backend:i})=>{let{a:u,b:c}=o,l=i;if(r&&u.dtype==="complex64"){let f=l.texData.get(u.dataId),g=l.texData.get(c.dataId),[m,b]=[[f.complexTensorInfos.real,g.complexTensorInfos.real],[f.complexTensorInfos.imag,g.complexTensorInfos.imag]].map(v=>{let[x,k]=v,S={dataId:x.dataId,dtype:x.dtype,shape:u.shape},N={dataId:k.dataId,dtype:k.dtype,shape:c.shape},E=new To(e,u.shape,c.shape);return l.runWebGLProgram(E,[S,N],fr(x.dtype,k.dtype))}),y=Oa({inputs:{real:m,imag:b},backend:l});return l.disposeIntermediateTensorInfo(m),l.disposeIntermediateTensorInfo(b),y}let p=a||fr(u.dtype,c.dtype);if((u.dtype==="string"||c.dtype==="string"||l.shouldExecuteOnCPU([u,c]))&&s!=null){let f=l.texData.get(u.dataId).values,g=l.texData.get(c.dataId).values,m=u.dtype==="string"?T.fromUint8ToStringArray(f):f,b=u.dtype==="string"?T.fromUint8ToStringArray(g):g,[y,v]=s(u.shape,c.shape,m,b,p),x=l.makeTensorInfo(v,p),k=l.texData.get(x.dataId);return k.values=y,x}let d=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,h;return d?h=new Tl(t,u.shape,c.shape,n):h=new To(e,u.shape,c.shape),l.runWebGLProgram(h,[u,c],p)}}function Fd(e,t=!1){if(e==="linear")return t?lee:aee;if(e==="relu")return t?pee:iee;if(e==="elu")return t?dee:oee;if(e==="relu6")return t?hee:uee;if(e==="prelu")return t?sD:rD;if(e==="leakyrelu")return t?nD:tD;if(e==="sigmoid")return t?fee:cee;throw new Error(`Activation ${e} has not been implemented for the WebGL backend.`)}var aD=class{constructor(e,t,n,r=!1,s=!1,a=!1,o=null,i=!1,u=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n,this.enableShapeUniforms=xn(this.outputShape.length);let c=r?e[1]:e[2],l=Math.ceil(c/2),p=r?"i * 2, rc.y":"rc.y, i * 2",d=s?"rc.z, i * 2":"i * 2, rc.z",h=r?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],f=s?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],g="",m="";o&&(i?g=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
2024-01-17 22:44:24 +01:00
${o}
}`:u?g=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getLeakyreluAlphaAtOutCoords();
2024-01-17 22:44:24 +01:00
${o}
}`:g=`vec4 activation(vec4 x) {
${o}
}`,m="result = activation(result);");let b=a?"result += getBiasAtOutCoords();":"";a&&this.variableNames.push("bias"),i&&this.variableNames.push("preluActivationWeights"),u&&this.variableNames.push("leakyreluAlpha");let y="rc.x",v="rc.x";e[0]<t[0]?y=`imod(rc.x, ${e[0]})`:t[0]<e[0]&&(v=`imod(rc.x, ${t[0]})`),this.userCode=`
${g}
2021-09-11 17:11:38 +02:00
// Don't use uniform for sharedDimensionPacked for performance.
2024-01-17 22:44:24 +01:00
const float sharedDimension = ${l}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 dot2x2ARowBCol(ivec3 rc) {
vec4 result = vec4(0);
2023-03-21 13:00:18 +01:00
int batchA = ${y};
2024-01-17 22:44:24 +01:00
int batchB = ${v};
for (int i = 0; i < ${l}; i++) {
vec4 a = getMatrixA(batchA, ${p});
vec4 b = getMatrixB(batchB, ${d});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// These swizzled products need to be separately added.
// See: https://github.com/tensorflow/tfjs/issues/1735
2024-01-17 22:44:24 +01:00
result += (${h[0]} * ${f[0]});
result += (${h[1]} * ${f[1]});
2021-09-11 17:11:38 +02:00
}
return result;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 rc = getOutputCoords();
vec4 result = dot2x2ARowBCol(rc);
2021-04-01 19:39:54 +02:00
2023-03-21 13:00:18 +01:00
${b}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${m}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}},D1={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},$1=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=T.assertAndGetBroadcastShape(t,n),this.userCode=`
2021-09-11 17:11:38 +02:00
float binaryOpComplex(
float areal, float aimag, float breal, float bimag) {
${e}
}
void main() {
float areal = getARealAtOutCoords();
float aimag = getAImagAtOutCoords();
float breal = getBRealAtOutCoords();
float bimag = getBImagAtOutCoords();
setOutput(binaryOpComplex(areal, aimag, breal, bimag));
}
2024-01-17 22:44:24 +01:00
`}},F1="return a * b;";function y0(e){let{inputs:t,backend:n}=e,{a:r,b:s}=t,a=T.upcastType(r.dtype,s.dtype);if(r.dtype==="complex64"){let i=n.texData.get(r.dataId),u=n.texData.get(s.dataId),c=new $1(D1.REAL,r.shape,s.shape),l=new $1(D1.IMAG,r.shape,s.shape),p=[{dataId:i.complexTensorInfos.real.dataId,dtype:i.complexTensorInfos.real.dtype,shape:r.shape},{dataId:i.complexTensorInfos.imag.dataId,dtype:i.complexTensorInfos.imag.dtype,shape:r.shape},{dataId:u.complexTensorInfos.real.dataId,dtype:u.complexTensorInfos.real.dtype,shape:s.shape},{dataId:u.complexTensorInfos.imag.dataId,dtype:u.complexTensorInfos.imag.dtype,shape:s.shape}],d=n.runWebGLProgram(c,p,"float32"),h=n.runWebGLProgram(l,p,"float32"),f=Oa({inputs:{real:d,imag:h},backend:n});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}if(n.shouldExecuteOnCPU([r,s])){let i=n.texData.get(r.dataId),u=n.texData.get(s.dataId),[c,l]=_Q(r.shape,s.shape,i.values,u.values,a),p=n.makeTensorInfo(l,a),d=n.texData.get(p.dataId);return d.values=c,p}let o;return G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?o=new Tl(F1,r.shape,s.shape):o=new To(F1,r.shape,s.shape),n.runWebGLProgram(o,[r,s],a)}var $ee={kernelName:xi,backendName:"webgl",kernelFunc:y0};function Fee(e,t,n){let r=[So(e.shape),...Co(e.shape)],s={dtype:e.dtype,shape:r,dataId:e.dataId},a=[So(t),...Co(t)],o=new JA(a,r),i=!0,u=[r],c=n.runWebGLProgram(o,[s],e.dtype,u,i);return{dataId:c.dataId,shape:t,dtype:c.dtype}}function pe(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{shape:a}=r,o=n,i=w.sizeFromShape(s.shape),u=w.inferFromImplicitShape(a,i),c=w.sizeFromShape(u);w.assert(i===c,()=>`The new shape (${u}) has ${c} elements and the old shape (${s.shape}) has ${i} elements. The new shape and old shape must have the same number of elements.`);let l=o.texData.get(s.dataId);return l.isPacked&&!$d(s.shape,u)&&!(l.texture!==null&&$d(l.shape,u))?Fee(s,u,o):(o.incRef(s.dataId),{dataId:s.dataId,shape:u,dtype:s.dtype})}var Ree={kernelName:qc,backendName:"webgl",kernelFunc:pe},R1=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:r,inSize:s,outSize:a}=e;this.outputShape=[r,a];let o=Math.floor(n/4)*4,i=n%4,u="sumValue += dot(values, ones);";if(t!=null){let l=1/t;u=`sumValue += dot(values * ${w.isInt(l)?l.toPrecision(2):l}, ones);`}let c="";s%n>0&&(c=`
if (inIdx < 0 || inIdx >= ${s}) {
2021-09-11 17:11:38 +02:00
return 0.0;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`),this.userCode=`
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
2024-01-17 22:44:24 +01:00
${c}
2021-09-11 17:11:38 +02:00
return getX(batch, inIdx);
2021-04-01 19:39:54 +02:00
}
void main() {
2021-09-11 17:11:38 +02:00
ivec2 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
int batch = coords[0];
2021-09-11 17:11:38 +02:00
int outIdx = coords[1];
int inOffset = outIdx * ${n};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float sumValue = 0.0;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int i = 0; i < ${o}; i += 4) {
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${u}
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
int inIdx = inOffset + ${o};
if (${i===1}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(getValue(batch, inIdx), 0.0, 0.0, 0.0);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${u}
} else if (${i===2}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1), 0.0, 0.0);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${u}
} else if (${i===3}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2), 0.0);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${u}
2021-09-11 17:11:38 +02:00
}
setOutput(sumValue);
}
2024-01-17 22:44:24 +01:00
`}},Pee=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:r,inSize:s,outSize:a}=e;this.outputShape=[r,a];let o="0.0",i="";t==="prod"?o="1.0":t==="min"?(o="1.0 / 1e-20",i="min"):t==="max"&&(o="-1.0 / 1e-20",i="max");let u=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="sum"?u="sumValue":t==="prod"?u="prodValue":t==="all"?u="allValue":t==="any"&&(u="anyValue");let c=Math.floor(n/4)*4,l=n%4,p=`
2021-09-11 17:11:38 +02:00
if (${t==="sum"}) {
sumValue += dot(values, ones);
} else if (${t==="prod"}) {
vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);
prodValue *= tmp[0] * tmp[1];
} else {
2024-01-17 22:44:24 +01:00
minMaxValue = ${i}(values, minMaxValue);
2021-09-11 17:11:38 +02:00
if (${t==="min"} || ${t==="max"}) {
2024-01-17 22:44:24 +01:00
minMaxValue = ${i}(values, minMaxValue);
2021-09-11 17:11:38 +02:00
bvec4 isNaN = isnan(values);
if (isNaN.r || isNaN.g || isNaN.b || isNaN.a) {
minMaxValue = vec4(NAN);
2021-04-01 19:39:54 +02:00
}
}
}
2024-01-17 22:44:24 +01:00
`,d="vec4";t==="all"?(o="1.0",p=`
2021-09-11 17:11:38 +02:00
bool reducedAllValue = all(values);
float floatedReducedAllValue = float(reducedAllValue);
allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
2024-01-17 22:44:24 +01:00
`,d="bvec4"):t==="any"&&(o="0.0",p=`
2021-09-11 17:11:38 +02:00
bool reducedAnyValue = any(values);
float floatedReducedAnyValue = float(reducedAnyValue);
anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);
2024-01-17 22:44:24 +01:00
`,d="bvec4");let h="";s%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${s}) {
2021-09-11 17:11:38 +02:00
return initializationValue;
}
`),this.userCode=`
2024-01-17 22:44:24 +01:00
const float initializationValue = ${o};
2021-09-11 17:11:38 +02:00
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
${h}
return getX(batch, inIdx);
2021-04-01 19:39:54 +02:00
}
void main() {
2021-09-11 17:11:38 +02:00
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${n};
2024-01-17 22:44:24 +01:00
vec4 minMaxValue = vec4(${o});
2021-09-11 17:11:38 +02:00
float prodValue = 1.0;
float sumValue = 0.0;
float allValue = 1.0;
float anyValue = 0.0;
2024-01-17 22:44:24 +01:00
for (int i = 0; i < ${c}; i += 4) {
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + i;
2024-01-17 22:44:24 +01:00
${d} values = ${d}(
2021-09-11 17:11:38 +02:00
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${p}
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
int inIdx = inOffset + ${c};
if (${l===1}) {
${d} values = ${d}(
2021-09-11 17:11:38 +02:00
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${p}
} else if (${l===2}) {
${d} values = ${d}(
2021-09-11 17:11:38 +02:00
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${p}
} else if (${l===3}) {
${d} values = ${d}(
2021-09-11 17:11:38 +02:00
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${p}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
setOutput(${u});
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Oee(e){let t=[];for(;t.length===0||t[t.length-1].outSize!==1;){let n=t.length?t[t.length-1].outSize:e[1],r=T.computeOptimalWindowSize(n);t.push({inSize:n,windowSize:r,outSize:Math.ceil(n/r)})}return t}function ru(e,t,n,r){let s=Oee(e.shape),a=e;for(let o=0;o<s.length;o++){let{inSize:i,windowSize:u,outSize:c}=s[o],l,p;n==="mean"?l=o===0?new R1({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:c},i):new R1({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:c}):l=new Pee({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:c},n),p=a,a=r.runWebGLProgram(l,[a],t),p.dataId!==e.dataId&&r.disposeIntermediateTensorInfo(p)}return a}var Mee=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 r=ht(this.rank),s=Lee(t);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${r} resRC = getOutputCoords();
setOutput(getA(${s}));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Lee(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"],r=new Array(t);for(let s=0;s<e.length;s++)r[e[s]]=n[s];return r.join()}var Bee=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0;let n=new Array(e.length);for(let c=0;c<n.length;c++)n[c]=e[t[c]];if(this.outputShape=n,this.rank=n.length,this.rank>6)throw Error(`Packed transpose for rank ${this.rank} is not yet supported.`);let r=ht(this.rank),s=ZA("rc",this.rank),a=new Array(this.rank);for(let c=0;c<t.length;c++)a[t[c]]=s[c];let o=`vec2(${a.slice(-2).join()})`,i=`++${s[this.rank-1]} < ${n[this.rank-1]}`,u=`getChannel(getA(${a.join()}), ${o})`;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${r} rc = getOutputCoords();
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2024-01-17 22:44:24 +01:00
result[0] = ${u};
if(${i}) {
result[1] = ${u};
}
--${s[this.rank-1]};
if(++${s[this.rank-2]} < ${n[this.rank-2]}) {
result[2] = ${u};
if(${i}) {
result[3] = ${u};
}
}
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}};function tg(e,t,n){let r=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Bee(e.shape,t):new Mee(e.shape,t);return n.runWebGLProgram(r,[e],e.dtype)}function zee(e,t,n,r){let s=t,a=e.shape.length,o=w.parseAxisParam(s,e.shape),i=o,u=T.getAxesPermutation(i,a),c=u!=null,l=e;c&&(l=tg(e,u,r),i=T.getInnerMostAxes(i.length,a)),T.assertAxesAreInnerMostDims("sum",i,a);let[p,d]=T.computeOutAndReduceShapes(l.shape,i),h=p;n&&(h=T.expandShapeToKeepDim(p,o));let f=w.sizeFromShape(d),m=w.sizeFromShape(e.shape)/f,b=pe({inputs:{x:l},attrs:{shape:[m,f]},backend:r}),y=Zf(e.dtype),v=ru(b,y,"sum",r),x=pe({inputs:{x:v},attrs:{shape:h},backend:r});return r.disposeIntermediateTensorInfo(b),r.disposeIntermediateTensorInfo(v),c&&r.disposeIntermediateTensorInfo(l),x}function ng(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r;return zee(s,a,o,n)}var Wee={kernelName:Wi,backendName:"webgl",kernelFunc:ng};function Tn(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{perm:a}=r,o=n,i=s.shape.length,u=new Array(i);for(let l=0;l<u.length;l++)u[l]=s.shape[a[l]];let c;if(o.shouldExecuteOnCPU([s])){let p=o.texData.get(s.dataId).values,d=m0(p,s.shape,s.dtype,a,u);c=o.makeTensorInfo(u,s.dtype);let h=o.texData.get(c.dataId);h.values=d}else c=tg(s,a,o);return c}var Vee={kernelName:_s,backendName:"webgl",kernelFunc:Tn},oD=1e3;function Ef({a:e,b:t,transposeA:n,transposeB:r,backend:s,bias:a=null,preluActivationWeights:o=null,leakyreluAlpha:i=0,activation:u=null}){let c=e.shape.length,l=t.shape.length,p=n?e.shape[c-2]:e.shape[c-1],d=r?t.shape[l-1]:t.shape[l-2],h=n?e.shape[c-1]:e.shape[c-2],f=r?t.shape[l-2]:t.shape[l-1],g=e.shape.slice(0,-2),m=t.shape.slice(0,-2),b=w.sizeFromShape(g),y=w.sizeFromShape(m),x=dl.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=${r} must match.`);let k=n?[b,p,h]:[b,h,p],S=r?[y,f,d]:[y,d,f],N=pe({inputs:{x:e},backend:s,attrs:{shape:k}}),E=pe({inputs:{x:t},backend:s,attrs:{shape:S}}),$=[N,E],F=Math.max(b,y),D=n?N.shape[1]:N.shape[2],R=a!=null,C=o!=null,L=u==="leakyrelu",U=u!=null?Fd(u,!0):null,H=R||C||L||U!=null,K;if((h===1||f===1)&&D>oD&&H===!1){let Z=N,J=E;n&&(Z=Tn({inputs:{x:N},backend:s,attrs:{perm:[0,2,1]}}),$.push(Z)),r&&(J=Tn({inputs:{x:E},backend:s,attrs:{perm:[0,2,1]}}),$.push(J));let ee=f!==1,se=f===1,te=Z;ee&&(te=pe({inputs:{x:Z},backend:s,attrs:{shape:[F,D,1]}}),$.push(te));let oe=f===1?2:1,ne=J;se&&(ne=pe({inputs:{x:J},backend:s,attrs:{shape:[F,1,D]}}),$.push(ne));let de=y0({inputs:{a:te,b:ne},backend:s});K=ng({inputs:{x:de},backend:s,attrs:{axis:oe,keepDims:!0}}),$.push(de)}else{let Z=fr(e.dtype,t.dtype),J=new aD(k,S,[F,h,f],n,r,R,U,C,L),ee=[N,E];if(a!=null&&ee.push(a),C&&ee.push(o),L){let se=s.makeTensorInfo([],"float32",w.createScalarValue(i,"float32"));ee.push(se),$.push(se)}K=s.runWebGLProgram(J,ee,Z)}let q=pe({inputs:{x:K},backend:s,attrs:{shape:x}});$.push(K);for(let Z of $)s.disposeIntermediateTensorInfo(Z);return q}function Uee(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a,bias:o,preluActivationWeights:i}=t,{transposeA:u,transposeB:c,activation:l,leakyreluAlpha:p}=r;return Ef({a:s,b:a,transposeA:u,transposeB:c,backend:n,bias:o,preluActivationWeights:i,leakyreluAlpha:p,activation:l})}var Gee={kernelName:uo,backendName:"webgl",kernelFunc:Uee},P1="return abs(x);";function Hee(e){let{inputs:t,backend:n}=e,{x:r}=t;if(n.shouldExecuteOnCPU([r])&&r.dtype!=="complex64"){let a=n.texData.get(r.dataId),o=XA(a.values);return n.makeTensorInfo(r.shape,r.dtype,o)}let s;return G().getBool("WEBGL_PACK_UNARY_OPERATIONS")?s=new oa(r.shape,P1):s=new is(r.shape,P1),n.runWebGLProgram(s,[r],r.dtype)}var jee={kernelName:cc,backendName:"webgl",kernelFunc:Hee},qee=Or+`
2021-09-11 17:11:38 +02:00
if (abs(x) > 1.) {
return NAN;
}
2021-09-11 17:11:38 +02:00
return acos(x);
2024-01-17 22:44:24 +01:00
`,Kee=Ze({opSnippet:qee}),Xee={kernelName:Eo,backendName:"webgl",kernelFunc:Kee},Yee=Or+`
2021-09-11 17:11:38 +02:00
if (x < 1.0) return NAN;
2024-01-17 22:44:24 +01:00
return log(x + sqrt(x * x - 1.0));`,Zee=Ze({opSnippet:Yee}),Jee={kernelName:Ao,backendName:"webgl",kernelFunc:Zee},O1="return a + b;",Qee=mn({opSnippet:O1,packedOpSnippet:O1,supportsComplex:!0,cpuKernelImpl:iQ}),ete={kernelName:Sa,backendName:"webgl",kernelFunc:Qee},tte=class{constructor(e,t){this.outputShape=[],this.outputShape=e,this.variableNames=t.map((s,a)=>`T${a}`);let n=[];this.variableNames.forEach(s=>{n.push(`float v${s} = get${s}AtOutCoords();`)});let r=this.variableNames.map(s=>`v${s}`).join(" + ");this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${n.join(`
`)}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
float result = ${r};
2021-09-11 17:11:38 +02:00
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},nte=class{constructor(e,t){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.variableNames=t.map((s,a)=>`T${a}`);let n=[];this.variableNames.forEach(s=>{n.push(`vec4 v${s} = get${s}AtOutCoords();`)});let r=this.variableNames.map(s=>`v${s}`).join(" + ");this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${n.join(`
`)}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
vec4 result = ${r};
2021-09-11 17:11:38 +02:00
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function nf(e){let{inputs:t,backend:n}=e,r=t;if(r.length===1)return sr({inputs:{x:r[0]},backend:n});if(r.length>G().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let u=Math.floor(r.length/2),c=nf({inputs:r.slice(0,u),backend:n}),l=nf({inputs:r.slice(u),backend:n});return nf({inputs:[c,l],backend:n})}let s=r.map(u=>u.dtype).reduce((u,c)=>fr(u,c)),a=r.map(u=>u.shape),i=G().getBool("WEBGL_PACK")?new nte(r[0].shape,a):new tte(r[0].shape,a);return n.runWebGLProgram(i,r,s)}var rte={kernelName:Do,backendName:"webgl",kernelFunc:nf};function ste(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),c=u,l=T.getAxesPermutation(c,i),p=s;l!=null&&(p=Tn({inputs:{x:s},backend:n,attrs:{perm:l}}),c=T.getInnerMostAxes(c.length,i)),T.assertAxesAreInnerMostDims("all",c,i);let[d,h]=T.computeOutAndReduceShapes(p.shape,c),f=w.sizeFromShape(h),g=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),m=ru(g,g.dtype,"all",n),b;if(o){let y=T.expandShapeToKeepDim(d,u);b=pe({inputs:{x:m},backend:n,attrs:{shape:y}})}else b=pe({inputs:{x:m},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(g),n.disposeIntermediateTensorInfo(m),l!=null&&n.disposeIntermediateTensorInfo(p),b}var ate={kernelName:lc,backendName:"webgl",kernelFunc:ste};function ote(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),c=u,l=T.getAxesPermutation(c,i),p=s;l!=null&&(p=Tn({inputs:{x:s},backend:n,attrs:{perm:l}}),c=T.getInnerMostAxes(c.length,i)),T.assertAxesAreInnerMostDims("any",c,i);let[d,h]=T.computeOutAndReduceShapes(p.shape,c),f=w.sizeFromShape(h),g=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),m=ru(g,g.dtype,"any",n),b;if(o){let y=T.expandShapeToKeepDim(d,u);b=pe({inputs:{x:m},backend:n,attrs:{shape:y}})}else b=pe({inputs:{x:m},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(g),n.disposeIntermediateTensorInfo(m),l!=null&&n.disposeIntermediateTensorInfo(p),b}var ite={kernelName:dc,backendName:"webgl",kernelFunc:ote},ute=class{constructor(e,t,n){this.variableNames=["A"];let{windowSize:r,batchSize:s,outSize:a}=e;n||this.variableNames.push("bestIndicesA"),this.outputShape=[s,a];let o=t==="max"?">":"<",i=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
2024-01-17 22:44:24 +01:00
int inOffset = outIdx * ${r};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int bestIndex = inOffset;
float bestValue = getA(batch, bestIndex);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int i = 0; i < ${r}; i++) {
int inIdx = ${i};
2021-09-11 17:11:38 +02:00
float candidate = getA(batch, inIdx);
2024-01-17 22:44:24 +01:00
if (candidate ${o} bestValue) {
2021-09-11 17:11:38 +02:00
bestValue = candidate;
bestIndex = inIdx;
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(float(bestIndex));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},cte=class{constructor(e,t,n,r){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 s=e[e.length-1],a=Math.ceil(s/t);this.outputShape=e.slice(0,-1),a>1&&this.outputShape.push(a),r||this.variableNames.push("bestIndicesA");let o=this.outputShape,i=o.length,u=ht(i),c=Cn("coords",i),l,p;if(a===1){p=i+1;let N=ht(p);l=`
${N} sourceLocR = ${N}(${c.join()}, 0);
++${c[i-1]};
${N} sourceLocG = ${N}(${c.join()}, 0);
++${c[i-2]};
${N} sourceLocA = ${N}(${c.join()}, 0);
--${c[i-1]};
${N} sourceLocB = ${N}(${c.join()}, 0);
--${c[i-2]};`}else p=i,l=`
${u} sourceLocR = coords;
++${c[i-1]};
${u} sourceLocG = coords;
++${c[i-2]};
${u} sourceLocA = coords;
--${c[i-1]};
${u} sourceLocB = coords;
--${c[i-2]};`;let d=["x","y","z","w","u","v"].slice(0,p),h="."+d[p-1],f=d.map(N=>"int "+N),g=Cn("sourceLocR",p-1).concat("inIdx.r"),m=Cn("sourceLocG",p-1).concat("inIdx.g"),b=Cn("sourceLocB",p-1).concat("inIdx.b"),y=Cn("sourceLocA",p-1).concat("inIdx.a"),v=n==="max"?"greaterThan":"lessThan",x=r?"":`
inIdx = round(vec4(getBestIndicesAChannel(${g.join()}),
getBestIndicesAChannel(${m.join()}),
2023-03-21 13:00:18 +01:00
getBestIndicesAChannel(${b.join()}),
2024-01-17 22:44:24 +01:00
getBestIndicesAChannel(${y.join()})));`,k=`vec4(
getAChannel(${g.join()}),
hasNextCol ? getAChannel(${m.join()}) : 0.,
2023-03-21 13:00:18 +01:00
hasNextRow ? getAChannel(${b.join()}) : 0.,
2024-01-17 22:44:24 +01:00
hasNextRow && hasNextCol ? getAChannel(${y.join()}) : 0.)`,S=r?"":`
float getBestIndicesAChannel(${f.join()}) {
return getChannel(getBestIndicesA(${d.join()}),
vec2(${d.slice(-2).join()}));
2021-09-11 17:11:38 +02:00
}`;this.userCode=`
2024-01-17 22:44:24 +01:00
float getAChannel(${f.join()}) {
return getChannel(getA(${d.join()}),
vec2(${d.slice(-2).join()}));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
${S}
2021-04-01 19:39:54 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${u} coords = getOutputCoords();
bool hasNextCol = ${c[i-1]} < ${o[i-1]-1};
bool hasNextRow = ${c[i-2]} < ${o[i-2]-1};
${l}
2021-09-11 17:11:38 +02:00
ivec4 srcIdx = ivec4(sourceLocR${h}, sourceLocG${h},
sourceLocB${h}, sourceLocA${h}) * ${t};
ivec4 inIdx = srcIdx;
vec4 bestIndex = vec4(inIdx);
2024-01-17 22:44:24 +01:00
vec4 bestValue = ${k};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i = 0; i < ${t}; i++) {
inIdx = srcIdx;
2024-01-17 22:44:24 +01:00
${x}
vec4 candidate = ${k};
2021-09-11 17:11:38 +02:00
bvec4 nan = isnan(candidate);
bvec4 replace = bvec4(
2024-01-17 22:44:24 +01:00
vec4(${v}(candidate, bestValue)) * (vec4(1.0) - vec4(nan)));
2021-09-11 17:11:38 +02:00
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++;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(bestIndex);
}
2024-01-17 22:44:24 +01:00
`}};function iD(e,t,n,r=null){let s=t.shape[0],a=t.shape[1];r!=null&&(s=r.shape[0],a=r.shape[1]);let o=T.computeOptimalWindowSize(a),i={windowSize:o,inSize:a,batchSize:s,outSize:Math.ceil(a/o)},u=new ute(i,n,r==null),c=[t];r!=null&&c.push(r);let l=e.runWebGLProgram(u,c,"int32");if(l.shape[1]===1)return l;let p=iD(e,t,n,l);return e.disposeIntermediateTensorInfo(l),p}function uD(e,t,n,r=null){let s=r!=null?r.shape:t.shape,a=s[s.length-1],o=T.computeOptimalWindowSize(a),i=new cte(s,o,n,r==null),u=r==null?[t]:[t,r],c=e.runWebGLProgram(i,u,"int32");if(c.shape.length===t.shape.length){let l=uD(e,t,n,c);return e.disposeIntermediateTensorInfo(c),l}return c}function cD(e,t,n,r){let s=[n];if(T.assertAxesAreInnerMostDims("arg"+r.charAt(0).toUpperCase()+r.slice(1),s,t.shape.length),!G().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let a=[],o=e.texData.get(t.dataId),i=o!==null&&o.isPacked,u=t;i&&(u=e.unpackTensor(t),a.push(u));let[c,l]=T.computeOutAndReduceShapes(u.shape,s),p=w.sizeFromShape(l),d=pe({inputs:{x:u},backend:e,attrs:{shape:[-1,p]}});a.push(d);let h=iD(e,d,r);a.push(h);let f=pe({inputs:{x:h},backend:e,attrs:{shape:c}});return a.forEach(g=>e.disposeIntermediateTensorInfo(g)),f}return uD(e,t,r)}function lte(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a}=r,o=w.parseAxisParam(a,s.shape),i=T.getAxesPermutation(o,s.shape.length),u=s,c=[];i!=null&&(u=Tn({inputs:{x:s},backend:n,attrs:{perm:i}}),c.push(u),o=T.getInnerMostAxes(o.length,u.shape.length)),T.assertAxesAreInnerMostDims("argMax",[o[0]],u.shape.length);let l=cD(n,u,o[0],"max");return c.forEach(p=>n.disposeIntermediateTensorInfo(p)),l}var dte={kernelName:pc,backendName:"webgl",kernelFunc:lte};function pte(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a}=r,o=w.parseAxisParam(a,s.shape),i=T.getAxesPermutation(o,s.shape.length),u=s,c=[];i!=null&&(u=Tn({inputs:{x:s},backend:n,attrs:{perm:i}}),c.push(u),o=T.getInnerMostAxes(o.length,u.shape.length)),T.assertAxesAreInnerMostDims("argMin",[o[0]],u.shape.length);let l=cD(n,u,o[0],"min");return c.forEach(p=>n.disposeIntermediateTensorInfo(p)),l}var hte={kernelName:hc,backendName:"webgl",kernelFunc:pte},fte=Or+`
2021-09-11 17:11:38 +02:00
if (abs(x) > 1.) {
2021-04-01 19:39:54 +02:00
return NAN;
}
2021-09-11 17:11:38 +02:00
return asin(x);
2024-01-17 22:44:24 +01:00
`,mte=Ze({opSnippet:fte}),gte={kernelName:$o,backendName:"webgl",kernelFunc:mte},bte=Or+"return log(x + sqrt(x * x + 1.0));",yte=Ze({opSnippet:bte}),vte={kernelName:Fo,backendName:"webgl",kernelFunc:yte},xte=Or+`
2021-09-11 17:11:38 +02:00
return atan(x);
2024-01-17 22:44:24 +01:00
`,wte=Ze({opSnippet:xte}),Ite={kernelName:Ro,backendName:"webgl",kernelFunc:wte},kte=b0+`
2021-09-11 17:11:38 +02:00
return atan(a, b);
2024-01-17 22:44:24 +01:00
`,Ste=`
2021-09-11 17:11:38 +02:00
vec4 result = atan(a, b);
2022-10-09 19:40:33 +02:00
bvec4 isNaNA = isnan(a);
bvec4 isNaNB = isnan(b);
bvec4 isNaN = bvec4(isNaNA.x || isNaNB.x, isNaNA.y || isNaNB.y, isNaNA.z || isNaNB.z, isNaNA.w || isNaNB.w);
2024-01-17 22:44:24 +01:00
`+nu+`
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,Cte=mn({opSnippet:kte,packedOpSnippet:Ste}),Tte={kernelName:Oo,backendName:"webgl",kernelFunc:Cte},Nte=Or+`
2021-09-11 17:11:38 +02:00
if ((x < -1.0) || (x > 1.0)) return NAN;
2024-01-17 22:44:24 +01:00
return (log(1.0 + x) - log(1.0 - x)) / 2.0;`,_te=Ze({opSnippet:Nte}),Ete={kernelName:Po,backendName:"webgl",kernelFunc:_te},Rd=class{constructor(e,t,n,r=!1,s=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,o=e.strideHeight,i=e.strideWidth,u=e.dilationHeight,c=e.dilationWidth,l=e.effectiveFilterHeight,p=e.effectiveFilterWidth,d=e.padInfo.top,h=e.padInfo.left;this.outputShape=e.outShape;let f=t==="avg",g=`((batch * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + d`,m=`(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(${o}, ${i});
const ivec2 pads = ivec2(${d}, ${h});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d = coords[3];
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec2 xRCCorner = coords.yz * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// 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;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${l};
wR += ${u}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${p};
wC += ${c}) {
2021-09-11 17:11:38 +02:00
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);
2024-01-17 22:44:24 +01:00
if (value ${N} currMinMaxValue) {
2021-09-11 17:11:38 +02:00
minMaxValue = value;
minMaxValueFound = 1.0;
2024-01-17 22:44:24 +01:00
minMaxPosition = ${r?s?g:m:`wR * ${p} + wC`};
2021-09-11 17:11:38 +02:00
}
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(float(minMaxPosition));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;return}let y="max",v=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(v="avgValue / max(count, 1.0)");let x=Math.floor(a/4)*4,k=a%4,S=`
if (${f}) {
2021-09-11 17:11:38 +02:00
avgValue += dot(values, ones);
2021-04-01 19:39:54 +02:00
} else {
2023-03-21 13:00:18 +01:00
minMaxValue = ${y}(values, minMaxValue);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`;this.userCode=`
2024-01-17 22:44:24 +01:00
const ivec2 strides = ivec2(${o}, ${i});
const ivec2 pads = ivec2(${d}, ${h});
2023-03-21 13:00:18 +01:00
const float initializationValue = ${b};
2021-09-11 17:11:38 +02:00
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float count = 0.0;
float getValue(int batch, int xR, int xC, int d) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
}
count += 1.0;
return getX(batch, xR, xC, d);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int batch = coords[0];
2021-04-01 19:39:54 +02:00
int d = coords[3];
2021-09-11 17:11:38 +02:00
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
2023-03-21 13:00:18 +01:00
vec4 minMaxValue = vec4(${b});
2021-09-11 17:11:38 +02:00
float avgValue = 0.0;
count = 0.0;
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${l};
wR += ${u}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${x}; wC += 4) {
int xC = xCCorner + wC * ${c};
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xR, xC, d),
2024-01-17 22:44:24 +01:00
getValue(batch, xR, xC + ${c}, d),
getValue(batch, xR, xC + 2 * ${c}, d),
getValue(batch, xR, xC + 3 * ${c}, d)
2021-09-11 17:11:38 +02:00
);
2024-01-17 22:44:24 +01:00
${S}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
int xC = xCCorner + ${x};
if (${k===1}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xR, xC, d),
initializationValue,
initializationValue,
initializationValue
);
2024-01-17 22:44:24 +01:00
${S}
} else if (${k===2}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xR, xC, d),
2024-01-17 22:44:24 +01:00
getValue(batch, xR, xC + ${c}, d),
2021-09-11 17:11:38 +02:00
initializationValue,
initializationValue
);
2024-01-17 22:44:24 +01:00
${S}
} else if (${k===3}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xR, xC, d),
2024-01-17 22:44:24 +01:00
getValue(batch, xR, xC + ${c}, d),
getValue(batch, xR, xC + 2 * ${c}, d),
2021-09-11 17:11:38 +02:00
initializationValue
);
2024-01-17 22:44:24 +01:00
${S}
2021-04-01 19:39:54 +02:00
}
}
2024-01-17 22:44:24 +01:00
setOutput(${v});
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},v0=class{constructor(e,t,n,r=!1,s=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,o=e.strideDepth,i=e.strideHeight,u=e.strideWidth,c=e.dilationDepth,l=e.dilationHeight,p=e.dilationWidth,d=e.effectiveFilterDepth,h=e.effectiveFilterHeight,f=e.effectiveFilterWidth,g=e.padInfo.front,m=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 $=">=";this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec3 strides =
2024-01-17 22:44:24 +01:00
ivec3(${o}, ${i}, ${u});
const ivec3 pads = ivec3(${g}, ${m}, ${b});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// 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;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int wD = 0; wD < ${d};
wD += ${c}) {
2021-09-11 17:11:38 +02:00
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${h};
2024-01-17 22:44:24 +01:00
wR += ${l}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${f};
wC += ${p}) {
2021-09-11 17:11:38 +02:00
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);
2024-01-17 22:44:24 +01:00
if (value ${$} currMinMaxValue) {
2021-09-11 17:11:38 +02:00
minMaxValue = value;
minMaxValueFound = 1.0;
2024-01-17 22:44:24 +01:00
minMaxPosition = ${r?s?`(((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`};
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
setOutput(float(minMaxPosition));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`;return}let x="max",k=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(k="avgValue / max(count, 1.0)");let S=Math.floor(a/4)*4,N=a%4,E=`
2023-03-21 13:00:18 +01:00
if (${y}) {
2021-09-11 17:11:38 +02:00
avgValue += dot(values, ones);
} else {
2024-01-17 22:44:24 +01:00
minMaxValue = ${x}(values, minMaxValue);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`;this.userCode=`
const ivec3 strides =
2024-01-17 22:44:24 +01:00
ivec3(${o}, ${i}, ${u});
const ivec3 pads = ivec3(${g}, ${m}, ${b});
const float initializationValue = ${v};
2021-09-11 17:11:38 +02:00
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;
}
count += 1.0;
return getX(batch, xD, xR, xC, ch);
}
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).
// ? = to be determined
2024-01-17 22:44:24 +01:00
vec4 minMaxValue = vec4(${v});
2021-09-11 17:11:38 +02:00
float avgValue = 0.0;
count = 0.0;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int wD = 0; wD < ${d};
wD += ${c}) {
2021-09-11 17:11:38 +02:00
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
}
for (int wR = 0; wR < ${h};
2024-01-17 22:44:24 +01:00
wR += ${l}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${S}; wC += 4) {
int xC = xCCorner + wC * ${p};
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
2024-01-17 22:44:24 +01:00
getValue(batch, xD, xR, xC + ${p}, ch),
getValue(batch, xD, xR, xC + 2 * ${p}, ch),
getValue(batch, xD, xR, xC + 3 * ${p}, ch)
2021-09-11 17:11:38 +02:00
);
2023-01-06 19:24:17 +01:00
${E}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
int xC = xCCorner + ${S};
if (${N===1}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
initializationValue,
initializationValue,
initializationValue
);
2023-01-06 19:24:17 +01:00
${E}
2024-01-17 22:44:24 +01:00
} else if (${N===2}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
2024-01-17 22:44:24 +01:00
getValue(batch, xD, xR, xC + ${p}, ch),
2021-09-11 17:11:38 +02:00
initializationValue,
initializationValue
);
2023-01-06 19:24:17 +01:00
${E}
2024-01-17 22:44:24 +01:00
} else if (${N===3}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
2024-01-17 22:44:24 +01:00
getValue(batch, xD, xR, xC + ${p}, ch),
getValue(batch, xD, xR, xC + 2 * ${p}, ch),
2021-09-11 17:11:38 +02:00
initializationValue
);
2021-04-01 19:39:54 +02:00
2023-01-06 19:24:17 +01:00
${E}
2021-04-01 19:39:54 +02:00
}
}
}
2024-01-17 22:44:24 +01:00
setOutput(${k});
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Ate(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t;wl(s,"avgPool");let{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=r,c=1;w.assert(T.eitherStridesOrDilationsAreOne(o,c),()=>`Error in avgPool: Either strides or dilations must be 1. Got strides ${o} and dilations '${c}'`);let l=T.computePool2DInfo(s.shape,a,o,c,i,u);if(l.filterWidth===1&&l.filterHeight===1&&w.arraysEqual(l.inShape,l.outShape))return sr({inputs:{x:s},backend:n});let p=new Rd(l,"avg",!1);return n.runWebGLProgram(p,[s],"float32")}var Dte={kernelName:Mo,backendName:"webgl",kernelFunc:Ate};function $te(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u,dataFormat:c}=r,l=[1,1,1],p=T.computePool3DInfo(s.shape,a,o,l,i,u,c),d=new v0(p,"avg",!1);return n.runWebGLProgram(d,[s],"float32")}var Fte={kernelName:fc,backendName:"webgl",kernelFunc:$te},Rte=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,s=e.strideWidth,a=e.dilationHeight,o=e.dilationWidth,i=e.effectiveFilterHeight,u=e.effectiveFilterWidth,c=i-1-e.padInfo.top,l=u-1-e.padInfo.left,p=1/(t*n);this.userCode=`
const ivec2 pads = ivec2(${c}, ${l});
const float avgMultiplier = float(${p});
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${i};
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${r}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
int idyR = int(dyR);
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${u};
wC+= ${o}) {
float dyC = float(dyCCorner + wC) / ${s}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
2021-04-01 19:39:54 +02:00
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
2021-09-11 17:11:38 +02:00
dotProd += dyValue * avgMultiplier;
2021-04-01 19:39:54 +02:00
}
}
setOutput(dotProd);
}
2024-01-17 22:44:24 +01:00
`}},Pte=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,r=e.filterWidth,s=e.strideDepth,a=e.strideHeight,o=e.strideWidth,i=e.dilationDepth,u=e.dilationHeight,c=e.dilationWidth,l=e.effectiveFilterDepth,p=e.effectiveFilterHeight,d=e.effectiveFilterWidth,h=l-1-e.padInfo.front,f=p-1-e.padInfo.top,g=d-1-e.padInfo.left,m=1/(t*n*r);this.userCode=`
const ivec3 pads = ivec3(${h}, ${f}, ${g});
const float avgMultiplier = float(${m});
2021-04-01 19:39:54 +02:00
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;
2021-09-11 17:11:38 +02:00
// Convolve dy(?, ?, ?, d) with pos mask(:, :, :, ch) to get
2021-04-01 19:39:54 +02:00
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
2024-01-17 22:44:24 +01:00
for (int wD = 0; wD < ${l};
wD += ${i}) {
float dyD = float(dyDCorner + wD) / ${s}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
int idyD = int(dyD);
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${p};
wR += ${u}) {
float dyR = float(dyRCorner + wR) / ${a}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
2021-04-01 19:39:54 +02:00
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${d};
wC += ${c}) {
float dyC = float(dyCCorner + wC) / ${o}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
2021-04-01 19:39:54 +02:00
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
2021-09-11 17:11:38 +02:00
dotProd += dyValue * avgMultiplier;
2021-04-01 19:39:54 +02:00
}
}
}
setOutput(dotProd);
}
2024-01-17 22:44:24 +01:00
`}};function Ote(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a}=t,o=a,{filterSize:i,strides:u,pad:c,dimRoundingMode:l}=r,p=[1,1,1],d=T.computePool3DInfo(o.shape,i,u,p,c,l),h=new Pte(d);return n.runWebGLProgram(h,[s],o.dtype)}var Mte={kernelName:zd,backendName:"webgl",kernelFunc:Ote};function Lte(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a}=t,o=a;wl([s,a],"avgPoolGrad");let{filterSize:i,strides:u,pad:c}=r,l=T.computePool2DInfo(o.shape,i,u,1,c),p=new Rte(l);return n.runWebGLProgram(p,[s],o.dtype)}var Bte={kernelName:Bd,backendName:"webgl",kernelFunc:Lte};function zte(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a}=t,{transposeA:o,transposeB:i}=r;return Ef({a:s,b:a,transposeA:o,transposeB:i,backend:n})}var Wte={kernelName:Lo,backendName:"webgl",kernelFunc:zte},Vte=class{constructor(e,t,n,r,s,a){this.outputShape=[],this.variableNames=["x","mean","variance"],T.assertAndGetBroadcastShape(e,t),T.assertAndGetBroadcastShape(e,n);let o="0.0";r!=null&&(T.assertAndGetBroadcastShape(e,r),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");let i="1.0";s!=null&&(T.assertAndGetBroadcastShape(e,s),this.variableNames.push("scale"),i="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
float x = getXAtOutCoords();
float mean = getMeanAtOutCoords();
float variance = getVarianceAtOutCoords();
2024-01-17 22:44:24 +01:00
float offset = ${o};
float scale = ${i};
float inv = scale * inversesqrt(variance + float(${a}));
2021-09-11 17:11:38 +02:00
setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},Ute=class{constructor(e,t,n,r,s,a){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],T.assertAndGetBroadcastShape(e,t),T.assertAndGetBroadcastShape(e,n);let o="vec4(0.0)";r!=null&&(T.assertAndGetBroadcastShape(e,r),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");let i="vec4(1.0)";s!=null&&(T.assertAndGetBroadcastShape(e,s),this.variableNames.push("scale"),i="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2024-01-17 22:44:24 +01:00
vec4 offset = ${o};
vec4 scale = ${i};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 x = getXAtOutCoords();
vec4 mean = getMeanAtOutCoords();
vec4 variance = getVarianceAtOutCoords();
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
vec4 inv = scale * inversesqrt(variance + vec4(${a}));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput((x - mean) * inv + offset);
}
2024-01-17 22:44:24 +01:00
`}},Gte=({inputs:e,backend:t,attrs:n})=>{let{x:r,mean:s,variance:a,offset:o,scale:i}=e;w.assert(s.shape.length===a.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),w.assert(o==null||s.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),w.assert(i==null||s.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:u}=n;u==null&&(u=.001);let c=[r,s,a],l=null;o!=null&&(l=o.shape,c.push(o));let p=null;i!=null&&(p=i.shape,c.push(i));let d=G().getBool("WEBGL_PACK_NORMALIZATION")?new Ute(r.shape,s.shape,a.shape,l,p,u):new Vte(r.shape,s.shape,a.shape,l,p,u);return t.runWebGLProgram(d,c,c[0].dtype)},Hte={kernelName:ni,backendName:"webgl",kernelFunc:Gte},jte=class{constructor(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;let t=ht(this.rank);this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let n=qte(this.rank),r,s=e.map((a,o)=>`sourceLoc.${wx[o]} = start[${o}] + coords.${wx[o]};`);r=`
2021-09-11 17:11:38 +02:00
${t} sourceLoc;
${t} coords = getOutputCoords();
2024-01-17 22:44:24 +01:00
${s.join(`
2021-09-11 17:11:38 +02:00
`)}
`,this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${r}
2021-09-11 17:11:38 +02:00
setOutput(getSource(${n}));
}
2024-01-17 22:44:24 +01:00
`}},wx=["x","y","z","w","u","v"];function qte(e){if(e===1)return"sourceLoc";if(e<=6)return wx.slice(0,e).map(t=>"sourceLoc."+t).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}var Kte=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=ht(this.rank),n=Cn("coords",this.rank),r=Cn("sourceLoc",this.rank),s=this.rank===1?"sourceLoc":`vec2(${r.slice(-2).join()})`,a=`getChannel(getSource(${r.join()}), ${s})`,o=`
result.x = ${a};
2021-09-11 17:11:38 +02:00
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
2024-01-17 22:44:24 +01:00
++${r[this.rank-1]};
result.y = ${a};
--${r[this.rank-1]};
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`,i=this.rank===1?"":`
2021-09-11 17:11:38 +02:00
--${n[this.rank-1]};
if (++${n[this.rank-2]} < ${e[this.rank-2]}) {
2024-01-17 22:44:24 +01:00
++${r[this.rank-2]};
result.z = ${a};
2021-09-11 17:11:38 +02:00
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
2024-01-17 22:44:24 +01:00
++${r[this.rank-1]};
result.w = ${a};
2021-09-11 17:11:38 +02:00
}
}
2024-01-17 22:44:24 +01:00
`,u=this.rank<=4?`sourceLoc = coords +
${t}(${e.map((c,l)=>`start[${l}]`).join()});`:e.map((c,l)=>`${r[l]} = ${n[l]} + start[${l}];`).join(`
2021-09-11 17:11:38 +02:00
`);this.userCode=`
void main() {
${t} coords = getOutputCoords();
${t} sourceLoc;
2024-01-17 22:44:24 +01:00
${u}
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2022-10-18 13:23:49 +02:00
${o}
2024-01-17 22:44:24 +01:00
${i}
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}};function Xte(e,t,n,r){let s=r.texData.get(e.dataId),a=r.makeTensorInfo(n,e.dtype),o=r.texData.get(a.dataId);Object.assign(o,s),o.refCount=1,o.shape=n,o.dtype=e.dtype;let i=Kt.computeFlatOffset(t,w.computeStrides(e.shape));s.slice&&(i+=s.slice.flatOffset),o.slice={flatOffset:i,origDataId:s.slice&&s.slice.origDataId||e.dataId};let u=r.dataRefCount.get(o.slice.origDataId)||1;return r.dataRefCount.set(o.slice.origDataId,u+1),a}function _l(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,size:o}=r,[i,u]=Kt.parseSliceParams(s,a,o);if(Kt.assertParamsValid(s,i,u),w.sizeFromShape(u)===0)return n.makeTensorInfo(u,s.dtype,[]);if(n.shouldExecuteOnCPU([s])||s.dtype==="string"){let p=n.texData.get(s.dataId),d=BQ(p.values,i,u,s.shape,s.dtype);return n.makeTensorInfo(u,s.dtype,d)}let{isPacked:c}=n.texData.get(s.dataId),l=Kt.isSliceContinous(s.shape,i,u);if(c||!l){let p=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Kte(u):new jte(u),d=[i];return n.runWebGLProgram(p,[s],s.dtype,d)}return n.uploadToGPU(s.dataId),Xte(s,i,u,n)}var Yte={kernelName:el,backendName:"webgl",kernelFunc:_l},Zte=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockShape:a,crops:o}=r;w.assert(s.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let i=a.reduce((y,v)=>y*v),u=T.getReshaped(s.shape,a,i),c=T.getPermuted(u.length,a.length),l=T.getReshapedPermuted(s.shape,a,i),p=T.getSliceBeginCoords(o,a.length),d=T.getSliceSize(l,o,a.length),h=[],f=pe({inputs:{x:s},backend:n,attrs:{shape:u}}),g=Tn({inputs:{x:f},backend:n,attrs:{perm:c}}),m=pe({inputs:{x:g},backend:n,attrs:{shape:l}}),b=_l({inputs:{x:m},backend:n,attrs:{begin:p,size:d}});return h.push(f),h.push(g),h.push(m),h.forEach(y=>n.disposeIntermediateTensorInfo(y)),b},Jte={kernelName:mc,backendName:"webgl",kernelFunc:Zte};function Qte(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,weights:a}=t,{size:o}=r,i=n.readSync(s.dataId),u=n.readSync(a.dataId),c=KA(i,u,a.dtype,a.shape,o);return n.makeTensorInfo([o],a.dtype,c)}var ene={kernelName:gc,backendName:"webgl",kernelFunc:Qte},tne=`
2023-06-12 18:02:21 +02:00
int r = int(a.r) & int(b.r);
int g = int(a.g) & int(b.g);
int rb = int(a.b) & int(b.b);
int ra = int(a.a) & int(b.a);
return vec4(r, g, rb, ra);
2024-01-17 22:44:24 +01:00
`,nne=`
2023-06-12 18:02:21 +02:00
return float(int(a.r) & int(b.r));
2024-01-17 22:44:24 +01:00
`;function rne(e){let{inputs:t,backend:n}=e,{a:r,b:s}=t,a=G().getBool("WEBGL_PACK_BINARY_OPERATIONS"),o=G().getNumber("WEBGL_VERSION");if(n.shouldExecuteOnCPU([r,s])||o===1){let u=n.texData.get(r.dataId).values,c=n.texData.get(s.dataId).values,[l,p]=cQ(r.shape,s.shape,u,c,r.dtype),d=n.makeTensorInfo(p,r.dtype),h=n.texData.get(d.dataId);return h.values=l,d}let i;return a?i=new Tl(tne,r.shape,s.shape,!1):i=new To(nne,r.shape,s.shape),n.runWebGLProgram(i,[r,s],r.dtype)}var sne={kernelName:bc,backendName:"webgl",kernelFunc:rne};function ane(e){let{inputs:t,backend:n}=e,{s0:r,s1:s}=t,a=n.readSync(r.dataId),o=n.readSync(s.dataId),i=T.assertAndGetBroadcastShape(Array.from(a),Array.from(o));return n.makeTensorInfo([i.length],"int32",Int32Array.from(i))}var one={kernelName:Wd,backendName:"webgl",kernelFunc:ane},ine="return float(a != b);",lD=mn({opSnippet:ine,cpuKernelImpl:AQ,dtype:"bool"}),une={kernelName:Wc,backendName:"webgl",kernelFunc:lD};function Mp(e){let{inputs:t,backend:n}=e,{input:r}=t,s=n.texData.get(r.dataId);return sr({inputs:{x:s.complexTensorInfos.real},backend:n})}var cne={kernelName:Xf,backendName:"webgl",kernelFunc:Mp},lne="return float(int(x));";function dne(e,t){let n=new is(e.shape,lne),r=t.runWebGLProgram(n,[e],"int32");return{dataId:r.dataId,shape:r.shape,dtype:r.dtype}}function Ix(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{dtype:a}=r;if(a==="complex64"){if(s.dtype==="complex64")return sr({inputs:{x:s},backend:n});let o=kt(s.shape),i=Ix({inputs:{x:s},backend:n,attrs:{dtype:"float32"}}),u=Oa({inputs:{real:i,imag:o},backend:n});return o.dispose(),n.disposeIntermediateTensorInfo(i),u}if(s.dtype==="complex64"){let o=Mp({inputs:{input:s},backend:n}),i=Ix({inputs:{x:o},backend:n,attrs:{dtype:a}});return n.disposeIntermediateTensorInfo(o),i}if(!w.hasEncodingLoss(s.dtype,a)){let o=sr({inputs:{x:s},backend:n});return{dataId:o.dataId,shape:o.shape,dtype:a}}if(n.shouldExecuteOnCPU([s])){let o=n.texData.get(s.dataId).values,[i,u,c]=lQ(o,s.shape,s.dtype,a);return n.makeTensorInfo(i,u,c)}if(a==="int32")return dne(s,n);if(a==="bool"){let o=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=lD({inputs:{a:s,b:o},backend:n});return n.disposeIntermediateTensorInfo(o),u}throw new Error(`Error in Cast: failed to cast ${s.dtype} to ${a}`)}var pne={kernelName:Bo,backendName:"webgl",kernelFunc:Ix},M1="return ceil(x);",hne=Ze({opSnippet:M1,packedOpSnippet:M1,cpuKernelImpl:dQ}),fne={kernelName:zo,backendName:"webgl",kernelFunc:hne},mne=class{constructor(e){this.variableNames=["A"],this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=e,this.userCode=`
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
float value = getAAtOutCoords();
if (isnan(value)) {
setOutput(value);
return;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput(clamp(value, minVal, maxVal));
}
2024-01-17 22:44:24 +01:00
`}},gne=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=`
2021-09-11 17:11:38 +02:00
void main() {
vec4 value = getAAtOutCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (any(isnan(value))) {
setOutput(value);
return;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput(clamp(value, vec4(minVal), vec4(maxVal)));
}
2024-01-17 22:44:24 +01:00
`}};function bne(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{clipValueMin:a,clipValueMax:o}=r,i;G().getBool("WEBGL_PACK_CLIP")?i=new gne(s.shape):i=new mne(s.shape);let u=[[a],[o]];return n.runWebGLProgram(i,[s],s.dtype,u)}var yne={kernelName:Ca,backendName:"webgl",kernelFunc:bne},vne=class{constructor(e){this.variableNames=["real","imag"],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
float re = abs(getRealAtOutCoords());
float im = abs(getImagAtOutCoords());
float mx = max(re, im);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// 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))
);
}
2024-01-17 22:44:24 +01:00
`}};function L1(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function xne(e){let{inputs:t,backend:n}=e,{x:r}=t,s=n.texData.get(r.dataId),a=new vne(r.shape),o=[L1(r,s.complexTensorInfos.real),L1(r,s.complexTensorInfos.imag)];return n.runWebGLProgram(a,o,o[0].dtype)}var wne={kernelName:Vd,backendName:"webgl",kernelFunc:xne},Ine=class{constructor(e){this.outputShape=[],this.outputShape=T.computeOutShape(e,1),this.variableNames=e.map((a,o)=>`T${o}`);let t=new Array(e.length-1);t[0]=e[0][1];for(let a=1;a<t.length;a++)t[a]=t[a-1]+e[a][1];let n=[`if (yC < ${t[0]}) setOutput(getT0(yR, yC));`];for(let a=1;a<t.length;a++){let o=t[a-1];n.push(`else if (yC < ${t[a]}) setOutput(getT${a}(yR, yC-${o}));`)}let r=t.length,s=t[t.length-1];n.push(`else setOutput(getT${r}(yR, yC-${s}));`),this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int yR = coords.x;
int yC = coords.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${n.join(`
`)}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},kne=class{constructor(e,t){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=T.computeOutShape(e,t);let n=this.outputShape,r=n.length,s=ht(r),a=Cn("coords",r),o=["x","y","z","w","u","v"].slice(0,r);this.variableNames=e.map((f,g)=>`T${g}`);let i=new Array(e.length-1);i[0]=e[0][t];for(let f=1;f<i.length;f++)i[f]=i[f-1]+e[f][t];let u=o[t],c=o.slice(-2),l=o.join(),p=`if (${u} < ${i[0]}) {
2021-09-11 17:11:38 +02:00
return getChannel(
2024-01-17 22:44:24 +01:00
getT0(${l}), vec2(${c.join()}));
}`;for(let f=1;f<i.length;f++){let g=i[f-1];p+=`
if (${u} < ${i[f]} && ${u} >= ${i[f-1]}) {
2021-09-11 17:11:38 +02:00
return getChannel(
2024-01-17 22:44:24 +01:00
getT${f}(${qh(o,u,g)}),
vec2(${qh(c,u,g)}));
}`}let d=i.length,h=i[i.length-1];p+=`
2021-09-11 17:11:38 +02:00
return getChannel(
2024-01-17 22:44:24 +01:00
getT${d}(${qh(o,u,h)}),
vec2(${qh(c,u,h)}));`,this.userCode=`
float getValue(${o.map(f=>"int "+f)}) {
${p}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${s} coords = getOutputCoords();
vec4 result = vec4(getValue(${a}), 0., 0., 0.);
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
${a[r-1]} = ${a[r-1]} + 1;
if (${a[r-1]} < ${n[r-1]}) {
result.g = getValue(${a});
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
${a[r-2]} = ${a[r-2]} + 1;
if (${a[r-2]} < ${n[r-2]}) {
result.a = getValue(${a});
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
${a[r-1]} = ${a[r-1]} - 1;
if (${a[r-2]} < ${n[r-2]} &&
${a[r-1]} < ${n[r-1]}) {
result.b = getValue(${a});
2021-09-11 17:11:38 +02:00
}
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function qh(e,t,n){let r=e.indexOf(t);return e.map((a,o)=>o===r?`${a} - ${n}`:a).join()}function rg(e){let{inputs:t,backend:n}=e,{input:r}=t,s=n.texData.get(r.dataId);return sr({inputs:{x:s.complexTensorInfos.imag},backend:n})}var Sne={kernelName:Hf,backendName:"webgl",kernelFunc:rg};function hd(e,t,n){let r=e[0].dtype;if(r==="complex64"){let h=e.map(y=>Mp({inputs:{input:y},backend:n})),f=e.map(y=>rg({inputs:{input:y},backend:n})),g=hd(h,t,n),m=hd(f,t,n),b=Oa({inputs:{real:g,imag:m},backend:n});return h.forEach(y=>n.disposeIntermediateTensorInfo(y)),f.forEach(y=>n.disposeIntermediateTensorInfo(y)),n.disposeIntermediateTensorInfo(g),n.disposeIntermediateTensorInfo(m),b}let s=n.shouldExecuteOnCPU(e);if(r==="string"&&(s=!0),s){let h=e.map(x=>{let S=[-1,w.sizeFromShape(x.shape.slice(t))];return pe({inputs:{x},backend:n,attrs:{shape:S}})}),f=h.map(x=>({vals:n.readSync(x.dataId),shape:x.shape})),g=T.computeOutShape(h.map(x=>x.shape),1),m=h[0].shape[0]===1,b=pQ(f,g,r,m),y=T.computeOutShape(e.map(x=>x.shape),t),v=n.makeTensorInfo(y,r,b);return h.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}let a=e.filter(h=>w.sizeFromShape(h.shape)>0),o=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&a[0].shape.length>1;if(a.length===1){let h=o?new is(e[0].shape,ta):new oa(e[0].shape,ta);return n.runWebGLProgram(h,e,r)}let i=G().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER");if(a.length>i){let h=[];for(let g=0;g<a.length;g+=i){let m=a.slice(g,g+i);h.push(hd(m,t,n))}let f=hd(h,t,n);for(let g of h)n.disposeIntermediateTensorInfo(g);return f}if(o){let h=new kne(a.map(f=>f.shape),t);return n.runWebGLProgram(h,a,r)}let{tensors2D:u,outShape:c}=Cne(a,t,n),l=new Ine(u.map(h=>h.shape)),p=n.runWebGLProgram(l,u,r);u.forEach(h=>n.disposeIntermediateTensorInfo(h));let d=pe({inputs:{x:p},attrs:{shape:c},backend:n});return n.disposeIntermediateTensorInfo(p),d}function Cne(e,t,n){let r=T.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>pe({inputs:{x:a},attrs:{shape:[-1,w.sizeFromShape(a.shape.slice(t))]},backend:n})),outShape:r}}function dD(e){let{inputs:t,backend:n,attrs:r}=e,{axis:s}=r,a=w.parseAxisParam(s,t[0].shape)[0],o=t.map(c=>c.shape);T.assertParamsConsistent(o,a);let i=T.computeOutShape(t.map(c=>c.shape),a);if(w.sizeFromShape(i)===0)return n.makeTensorInfo(i,t[0].dtype,[]);let u=t.filter(c=>w.sizeFromShape(c.shape)>0);return u.length===1?sr({inputs:{x:u[0]},backend:n}):hd(u,a,n)}var Tne={kernelName:yc,backendName:"webgl",kernelFunc:dD},pD=class{constructor(e,t=!1,n=null,r=!1,s=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let a=e.padInfo.top,o=e.padInfo.left,i=e.strideHeight,u=e.strideWidth,c=e.dilationHeight,l=e.dilationWidth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4,g=e.dataFormat==="channelsLast",m=g?1:2,b=g?2:3,y=g?3:1,v="",x="";n&&(r?v=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getPreluActivationWeightsAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:s?v=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getLeakyreluAlphaAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:v=`
2021-09-11 17:11:38 +02:00
float activation(float x) {
${n}
}
2024-01-17 22:44:24 +01:00
`,x="result = activation(result);");let k=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${v}
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
const ivec2 strides = ivec2(${i}, ${u});
const ivec2 pads = ivec2(${a}, ${o});
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int batch = coords[0];
2023-03-21 13:00:18 +01:00
int d2 = coords[${y}];
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec2 xRCCorner =
2024-01-17 22:44:24 +01:00
ivec2(coords[${m}], coords[${b}]) * strides - pads;
2021-09-11 17:11:38 +02:00
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// 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;
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${p}; wR++) {
int xR = xRCorner + wR * ${c};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${d}; wC++) {
int xC = xCCorner + wC * ${l};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +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)
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if (${g}) {
2021-09-11 17:11:38 +02:00
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);
} else {
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);
}
}
2021-03-31 13:01:22 +02:00
2024-01-17 22:44:24 +01:00
if (${f===1}) {
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if (${g}) {
2021-09-11 17:11:38 +02:00
dotProd +=
getX(batch, xR, xC, ${h}) *
getW(wR, wC, ${h}, d2);
} else {
dotProd +=
getX(batch, ${h}, xR, xC) *
getW(wR, wC, ${h}, d2);
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
} else if (${f===2}) {
2021-09-11 17:11:38 +02:00
vec2 wValues = vec2(
getW(wR, wC, ${h}, d2),
getW(wR, wC, ${h} + 1, d2)
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if (${g}) {
2021-09-11 17:11:38 +02:00
vec2 xValues = vec2(
getX(batch, xR, xC, ${h}),
getX(batch, xR, xC, ${h} + 1)
);
dotProd += dot(xValues, wValues);
} else {
vec2 xValues = vec2(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC)
);
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
} else if (${f===3}) {
2021-09-11 17:11:38 +02:00
vec3 wValues = vec3(
getW(wR, wC, ${h}, d2),
getW(wR, wC, ${h} + 1, d2),
getW(wR, wC, ${h} + 2, d2)
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if (${g}) {
2021-09-11 17:11:38 +02:00
vec3 xValues = vec3(
getX(batch, xR, xC, ${h}),
getX(batch, xR, xC, ${h} + 1),
getX(batch, xR, xC, ${h} + 2)
);
dotProd += dot(xValues, wValues);
} else {
vec3 xValues = vec3(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC),
getX(batch, ${h} + 2, xR, xC)
);
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
}
}
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float result = dotProd;
2024-01-17 22:44:24 +01:00
${k}
${x}
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}},Nne=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let t=e.padInfo.front,n=e.padInfo.top,r=e.padInfo.left,s=e.strideDepth,a=e.strideHeight,o=e.strideWidth,i=e.dilationDepth,u=e.dilationHeight,c=e.dilationWidth,l=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(${s}, ${a}, ${o});
const ivec3 pads = ivec3(${t}, ${n}, ${r});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d2 = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xFCorner = xFRCCorner.x;
int xRCorner = xFRCCorner.y;
int xCCorner = xFRCCorner.z;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// 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;
2024-01-17 22:44:24 +01:00
for (int wF = 0; wF < ${l}; wF++) {
int xF = xFCorner + wF * ${i};
2021-09-11 17:11:38 +02:00
if (xF < 0 || xF >= ${e.inDepth}) {
2021-04-01 19:39:54 +02:00
continue;
}
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${p}; wR++) {
int xR = xRCorner + wR * ${u};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
2021-04-01 19:39:54 +02:00
continue;
}
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${d}; wC++) {
int xC = xCCorner + wC * ${c};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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)
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if (${f===1}) {
2021-09-11 17:11:38 +02:00
dotProd +=
getX(batch, xF, xR, xC, ${h}) *
getW(wF, wR, wC, ${h}, d2);
2024-01-17 22:44:24 +01:00
} else if (${f===2}) {
2021-09-11 17:11:38 +02:00
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);
2024-01-17 22:44:24 +01:00
} else if (${f===3}) {
2021-09-11 17:11:38 +02:00
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);
}
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},hD=class{constructor(e,t=!1,n=null,r=!1,s=!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=xn(this.outputShape.length);let a=e.padInfo.left,o=e.strideWidth,i=e.dilationWidth,u=e.filterHeight,c=e.filterWidth,l=c,p=`
2022-08-24 14:21:15 +02:00
int xR; int xC; int xCOffset;
2024-01-17 22:44:24 +01:00
vec4 wTexel; vec4 previous; vec4 final;`;for(let g=0;g<c;g++)p+=`
vec4 xTexelC${g*2};
int xTexelC${g*2}Ready;
vec4 xTexelC${g*2+1};
int xTexelC${g*2+1}Ready;
vec4 xC${g};`;p+=`
for (int r = 0; r < ${u}; r++) {
2022-08-24 14:21:15 +02:00
for (int d1 = 0; d1 < ${e.inChannels}; d1 += 2) {
2024-01-17 22:44:24 +01:00
`;for(let g=0;g<c;g++)p+=`
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);`;p+=`
2022-08-24 14:21:15 +02:00
xR = xRCorner + r * dilations[0];
if (xR >=0 && xR < inDims[0]) {
2024-01-17 22:44:24 +01:00
`;for(let g=0;g<(l+1)/2;g++){let m=g*2;if(p+=`
xC = xCCorner + ${m*i};
`,o===1){if(m<c&&(a%2===1?(p+=`
2022-08-24 14:21:15 +02:00
xCOffset = xC + 1;
2024-01-17 22:44:24 +01:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${m}Ready == 0) {
xTexelC${m} = getX(batch, xR, xCOffset, d1);
2022-08-24 14:21:15 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`,i===1&&m>0?p+=`
xC${m} = vec4(xTexelC${m-2}.zw, xTexelC${m}.xy);
`:p+=`
2022-08-24 14:21:15 +02:00
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);
}
2024-01-17 22:44:24 +01:00
xC${m} = vec4(previous.zw, xTexelC${m}.xy);
2022-08-24 14:21:15 +02:00
} else {
2024-01-17 22:44:24 +01:00
xC${m} = vec4(0.0, 0.0, xTexelC${m}.xy);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`):p+=`
if (xC >= 0 && xC < inDims[1] && xTexelC${m}Ready == 0) {
xTexelC${m} = getX(batch, xR, xC, d1);
2022-08-24 14:21:15 +02:00
if (xC + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xC${m} = xTexelC${m};
`,m+1<c)){let b=a%2===0?w.nearestLargerEven(i):i;i%2===0&&a%2===1||i%2!==0&&a%2!==1?(p+=`
2023-03-21 13:00:18 +01:00
xCOffset = xC + imod(pads[1], 2) + ${b};
2022-08-24 14:21:15 +02:00
2024-01-17 22:44:24 +01:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${m+1}Ready == 0) {
xTexelC${m+1} = getX(batch, xR, xCOffset, d1);
2022-08-24 14:21:15 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m+1}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m+1}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`,i>1?p+=`
2022-08-24 14:21:15 +02:00
xCOffset -= 2;
if (xCOffset >= 0 && xCOffset < inDims[1]) {
previous = getX(batch, xR, xCOffset, d1);
2024-01-17 22:44:24 +01:00
xC${m+1} = vec4(previous.zw, xTexelC${m+1}.xy);
2022-08-24 14:21:15 +02:00
} else {
2024-01-17 22:44:24 +01:00
xC${m+1} = vec4(0.0, 0.0, xTexelC${m+1}.xy);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`:p+=`
xC${m+1} = vec4(xTexelC${m}.zw, xTexelC${m+1}.xy);
`):b===1?p+=`
xC${m+1} = xTexelC${m};
`:p+=`
2023-03-21 13:00:18 +01:00
xCOffset = xC + ${b};
2022-08-24 14:21:15 +02:00
2024-01-17 22:44:24 +01:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${m+1}Ready == 0) {
xTexelC${m+1} = getX(batch, xR, xCOffset, d1);
2022-08-24 14:21:15 +02:00
if (xCOffset + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m+1}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m+1}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xC${m+1} = xTexelC${m+1};
`}}else m<c&&(a%2===1?(p+=`
2022-08-24 14:21:15 +02:00
xCOffset = xC + 1 - strides[1];
2024-01-17 22:44:24 +01:00
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${m}Ready == 0) {
xTexelC${m} = getX(batch, xR, xCOffset, d1);
2022-08-24 14:21:15 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
if(xC + 1 >= 0 && xC + 1 < inDims[1] && xTexelC${m+1}Ready == 0) {
xTexelC${m+1} = getX(batch, xR, xC + 1, d1);
2022-08-24 14:21:15 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xC + 2 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m+1}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m+1}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xC${m} = vec4(xTexelC${m}.zw, xTexelC${m+1}.zw);
`,m+1<c&&(p+=`
2022-08-24 14:21:15 +02:00
final = vec4(0.0);
xCOffset = xC + 1 + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1]) {
final = getX(batch, xR, xCOffset, d1);
}
2024-01-17 22:44:24 +01:00
xC${m+1} = vec4(xTexelC${m+1}.xy, final.xy);
`)):(p+=`
if(xC >= 0 && xC < inDims[1] && xTexelC${m}Ready == 0) {
xTexelC${m} = getX(batch, xR, xC, d1);
2022-08-24 14:21:15 +02:00
if (xC + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m}.zw = vec2(0.0);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m}Ready = 1;
2022-08-24 14:21:15 +02:00
}
xCOffset = xC + strides[1];
2024-01-17 22:44:24 +01:00
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${m+1}Ready == 0) {
xTexelC${m+1} = getX(batch, xR, xCOffset, d1);
2022-08-24 14:21:15 +02:00
if (xCOffset + 1 >= inDims[1]) {
2024-01-17 22:44:24 +01:00
xTexelC${m+1}.zw = vec2(0.);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xTexelC${m+1}Ready = 1;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
xC${m} = vec4(
xTexelC${m}.xy, xTexelC${m+1}.xy);
`,m+1<c&&(p+=`
xC${m+1} = vec4(xTexelC${m}.zw, xTexelC${m+1}.zw);
`)));m<c&&(p+=`
wTexel = getW(r, ${m}, d1, d2);
dotProd += xC${m}.xxzz * vec4(wTexel.xy, wTexel.xy);
2022-08-24 14:21:15 +02:00
if(d1 + 1 < ${e.inChannels}) {
2024-01-17 22:44:24 +01:00
dotProd += xC${m}.yyww * vec4(wTexel.zw, wTexel.zw);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`,m+1<c&&(p+=`
wTexel = getW(r, ${m+1}, d1, d2);
dotProd += xC${m+1}.xxzz * vec4(wTexel.xy, wTexel.xy);
2022-08-24 14:21:15 +02:00
if(d1 + 1 < ${e.inChannels}) {
2024-01-17 22:44:24 +01:00
dotProd += xC${m+1}.yyww * vec4(wTexel.zw, wTexel.zw);
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`))}p+=`
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`,p+=`
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`,p+=`
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:44:24 +01:00
`;let d="",h="";n&&(r?d=`vec4 activation(vec4 a) {
2022-08-24 14:21:15 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:s?d=`vec4 activation(vec4 a) {
2022-08-24 14:21:15 +02:00
vec4 b = getLeakyreluAlphaAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:d=`vec4 activation(vec4 x) {
2022-08-24 14:21:15 +02:00
${n}
2024-01-17 22:44:24 +01:00
}`,h="result = activation(result);");let f=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${d}
2022-08-24 14:21:15 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
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);
2024-01-17 22:44:24 +01:00
${p}
2022-08-24 14:21:15 +02:00
vec4 result = dotProd - vec4(0.000000000000001);
2024-01-17 22:44:24 +01:00
${f}
2022-08-24 14:21:15 +02:00
${h}
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}},_ne=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec4"},{name:"pad",type:"ivec2"},{name:"stride",type:"ivec2"},{name:"dilation",type:"ivec2"},{name:"inChannels",type:"int"},{name:"itemsPerBlockRow",type:"int"},{name:"outWidth",type:"int"}],this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let{dataFormat:n}=t,r=An(),s=n==="channelsLast",a=s?1:2,o=s?2:3,i=this.enableShapeUniforms?"if(blockIndex < outShape[2] && pos < outShape[1]) {":`if(blockIndex < ${e[2]} && pos < ${e[1]}) {`,u="";for(let c=0;c<=1;c++)for(let l=0;l<=1;l++)u+=`
blockIndex = rc.z + ${l};
pos = rc.y + ${c};
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${i}
2021-09-11 17:11:38 +02:00
offsetY = int(blockIndex / outWidth) * stride[0] - pad[0];
d0 = offsetY + dilation[0] * (pos / itemsPerBlockRow);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if(d0 < inputShape[${a}] && d0 >= 0) {
2021-09-11 17:11:38 +02:00
// 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);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if(d1 < inputShape[${o}] && d1 >= 0) {
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
ch = imod(pos, inChannels);
2021-04-30 18:01:04 +02:00
2024-01-17 22:44:24 +01:00
if (${s}) {
2021-09-11 17:11:38 +02:00
innerDims = vec2(d1, ch);
2024-01-17 22:44:24 +01:00
result[${c*2+l}] = getChannel(
2022-07-25 14:23:57 +02:00
getA(rc.x, d0, int(innerDims.x),
2021-09-11 17:11:38 +02:00
int(innerDims.y)), innerDims);
} else {
innerDims = vec2(d0, d1);
2024-01-17 22:44:24 +01:00
result[${c*2+l}] = getChannel(
2022-07-25 14:23:57 +02:00
getA(rc.x, ch, int(innerDims.x),
2021-09-11 17:11:38 +02:00
int(innerDims.y)), innerDims);
}
}
}
}
`;this.userCode=`
2021-04-30 18:01:04 +02:00
void main() {
2022-07-25 14:23:57 +02:00
ivec3 rc = getOutputCoords();
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0);
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
int blockIndex, pos, offsetY, d0, offsetX, d1, ch;
vec2 innerDims;
2021-04-30 18:01:04 +02:00
2024-01-17 22:44:24 +01:00
${u}
2021-04-30 18:01:04 +02:00
2024-01-17 22:44:24 +01:00
${r.output} = result;
2021-04-30 18:01:04 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Af(e,t){let n=e.length;return n>=3?t?[...e.slice(0,-3),e[n-3]*e[n-2],e[n-1]]:[...e.slice(0,-3),e[n-3],e[n-2]*e[n-1]]:!t&&n===1&&e[0]>1?[e[0],1]:null}function fD({x:e,filter:t,convInfo:n,backend:r,bias:s=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let u=e.shape,c=r.texData.get(e.dataId),l=n.inChannels,p=u[0]*u[1]*u[2],d=n.outChannels,h=n.dataFormat==="channelsLast",f=!1,g=!1,m,b=[];if(a!=null){let x=Af(a.shape,h);x!=null&&(a=pe({inputs:{x:a},backend:r,attrs:{shape:x}}),b.push(a))}if(s!=null){let x=Af(s.shape,h);x!=null&&(s=pe({inputs:{x:s},backend:r,attrs:{shape:x}}),b.push(s))}if(!((p===1||d===1)&&l>oD)&&c.isPacked&&h&&c.texture!=null&&u[2]%2!==0&&w.arraysEqual(c.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},S=c.shape;c.shape=c.shape.slice(),c.shape[c.shape.length-2]++,w.assert($d(c.shape,k.shape),()=>`packed reshape ${c.shape} to ${k.shape} isn't free`);let N=pe({inputs:{x:t},backend:r,attrs:{shape:[1,n.inChannels,n.outChannels]}});b.push(N);let E=Ef({a:k,b:N,backend:r,transposeA:f,transposeB:g,bias:s,activation:i,preluActivationWeights:a,leakyreluAlpha:o}),$=r.texData.get(E.dataId);w.assert($.isPacked,()=>"batchMatMul result is expected to be packed"),c.shape=S,$.shape=n.outShape,m=sr({inputs:{x:E},backend:r}),m.shape=n.outShape,b.push(E)}else{let x=n.outHeight*n.outWidth,k=pe({inputs:{x:e},backend:r,attrs:{shape:h?[n.batchSize,x,n.inChannels]:[n.batchSize,n.inChannels,x]}}),S=pe({inputs:{x:t},backend:r,attrs:{shape:[1,n.inChannels,n.outChannels]}}),N=Ef({a:h?k:S,b:h?S:k,transposeA:!h,transposeB:g,backend:r,bias:s,activation:i,preluActivationWeights:a,leakyreluAlpha:o});m=pe({inputs:{x:N},backend:r,attrs:{shape:n.outShape}}),b.push(k),b.push(S),b.push(N)}for(let x of b)r.disposeIntermediateTensorInfo(x);return m}function mD({x:e,filter:t,convInfo:n,backend:r,bias:s=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let{filterWidth:u,filterHeight:c,inChannels:l,outWidth:p,outHeight:d,dataFormat:h}=n,f=h==="channelsLast",g=u*c*l,m=d*p,b=[n.batchSize,g,m],y=!0,v=!1,x=[];if(a!=null){let q=Af(a.shape,f);q!=null&&(a=pe({inputs:{x:a},backend:r,attrs:{shape:q}}),x.push(a))}if(s!=null){let q=Af(s.shape,f);q!=null&&(s=pe({inputs:{x:s},backend:r,attrs:{shape:q}}),x.push(s))}let k=pe({inputs:{x:t},backend:r,attrs:{shape:[1,g,w.sizeFromShape(t.shape)/g]}});x.push(k);let S=new _ne(b,n),N=[e.shape,[n.padInfo.top,n.padInfo.left],[n.strideHeight,n.strideWidth],[n.dilationHeight,n.dilationWidth],[n.inChannels],[n.filterWidth*n.inChannels],[n.outWidth]],E=r.runWebGLProgram(S,[e],"float32",N),$=pe({inputs:{x:E},backend:r,attrs:{shape:b}});x.push(E),x.push($);let F=s!=null,D=a!=null,R=i==="leakyrelu",C=i?Fd(i,!0):null,L=new aD(f?$.shape:k.shape,f?k.shape:$.shape,f?[n.batchSize,m,n.outChannels]:[n.batchSize,n.outChannels,m],y,v,F,C,D,R),U=f?[$,k]:[k,$];if(s&&U.push(s),D&&U.push(a),R){let q=r.makeTensorInfo([],"float32",w.createScalarValue(o,"float32"));U.push(q),x.push(q)}let H=r.runWebGLProgram(L,U,"float32"),K=pe({inputs:{x:H},backend:r,attrs:{shape:n.outShape}});x.push(H);for(let q of x)r.disposeIntermediateTensorInfo(q);return K}function Ene(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dataFormat:u,dilations:c,dimRoundingMode:l}=r,p=T.convertConv2DDataFormat(u),d=T.computeConv2DInfo(s.shape,a.shape,o,c,i,l,!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=fD({x:s,filter:a,convInfo:d,backend:n});else if(d.strideWidth<=2&&p==="channelsLast"&&G().getBool("WEBGL_EXP_CONV")){let g=new hD(d),m=[[d.padInfo.top,d.padInfo.left],[d.strideHeight,d.strideWidth],[d.dilationHeight,d.dilationWidth],[d.inHeight,d.inWidth]];h=n.runWebGLProgram(g,[s,a],"float32",m)}else if(G().getBool("WEBGL_CONV_IM2COL"))h=mD({x:s,filter:a,convInfo:d,backend:n});else{let g=new pD(d);h=n.runWebGLProgram(g,[s,a],"float32")}let f=pe({inputs:{x:h},backend:n,attrs:{shape:d.outSha
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int wR = coords.x;
int wC = coords.y;
int d1 = coords.z;
int d2 = coords.w;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// 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;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yR = 0; yR < ${e.outHeight}; yR++) {
2024-01-17 22:44:24 +01:00
int xR = wR + yR * ${t} - ${r};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int yC = 0; yC < ${e.outWidth}; yC++) {
2024-01-17 22:44:24 +01:00
int xC = wC + yC * ${n} - ${s};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${a?`float dyValue = getDy(b, yR, yC, d2);
2023-03-21 13:00:18 +01:00
float xValue = getX(b, xR, xC, d1);
dotProd += (xValue * dyValue);`:`float dyValue = getDy(b, d2, yR, yC);
float xValue = getX(b, d1, xR, xC);
dotProd += (xValue * dyValue);`}
2021-09-11 17:11:38 +02:00
}
}
}
setOutput(dotProd);
}
2024-01-17 22:44:24 +01:00
`}},$ne=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,s=e.strideWidth,a=e.dataFormat==="channelsLast",o=t-1-e.padInfo.top,i=n-1-e.padInfo.left,u=a?1:2,c=a?2:3,l=a?3:1;this.userCode=`
const ivec2 pads = ivec2(${o}, ${i});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
2024-01-17 22:44:24 +01:00
int d1 = coords[${l}];
2021-09-11 17:11:38 +02:00
2024-01-17 22:44:24 +01:00
ivec2 dyCorner = ivec2(coords[${u}], coords[${c}]) - pads;
2021-09-11 17:11:38 +02:00
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++) {
2024-01-17 22:44:24 +01:00
float dyR = float(dyRCorner + wR) / ${r}.0;
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
int idyR = int(dyR);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wRPerm = ${t} - 1 - wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${n}; wC++) {
2024-01-17 22:44:24 +01:00
float dyC = float(dyCCorner + wC) / ${s}.0;
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
int idyC = int(dyC);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wCPerm = ${n} - 1 - wC;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int d2 = 0; d2 < ${e.outChannels}; d2++) {
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
if (${a}) {
2021-09-11 17:11:38 +02:00
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
} else {
float xValue = getDy(batch, d2, idyR, idyC);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
}
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},Fne=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideDepth,n=e.strideHeight,r=e.strideWidth,s=e.padInfo.front,a=e.padInfo.top,o=e.padInfo.left;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int wF = coords.x;
int wR = coords.y;
int wC = coords.z;
int d1 = coords.w;
int d2 = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float dotProd = 0.0;
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yF = 0; yF < ${e.outDepth}; yF++) {
2024-01-17 22:44:24 +01:00
int xF = wF + yF * ${t} - ${s};
2021-09-11 17:11:38 +02:00
if (xF < 0 || xF >= ${e.inDepth}) {
continue;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
for (int yR = 0; yR < ${e.outHeight}; yR++) {
2024-01-17 22:44:24 +01:00
int xR = wR + yR * ${n} - ${a};
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
for (int yC = 0; yC < ${e.outWidth}; yC++) {
2024-01-17 22:44:24 +01:00
int xC = wC + yC * ${r} - ${o};
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float dyValue = getDy(b, yF, yR, yC, d2);
float xValue = getX(b, xF, xR, xC, d1);
dotProd += (xValue * dyValue);
}
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},Rne=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,r=e.filterWidth,s=e.strideDepth,a=e.strideHeight,o=e.strideWidth,i=t-1-e.padInfo.front,u=n-1-e.padInfo.top,c=r-1-e.padInfo.left;this.userCode=`
const ivec3 pads = ivec3(${i}, ${u}, ${c});
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyFCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
float dotProd = 0.0;
for (int wF = 0; wF < ${t}; wF++) {
2024-01-17 22:44:24 +01:00
float dyF = float(dyFCorner + wF) / ${s}.0;
2021-09-11 17:11:38 +02:00
if (dyF < 0.0 || dyF >= ${e.outDepth}.0 || fract(dyF) > 0.0) {
continue;
}
int idyF = int(dyF);
int wFPerm = ${t} - 1 - wF;
for (int wR = 0; wR < ${n}; wR++) {
2024-01-17 22:44:24 +01:00
float dyR = float(dyRCorner + wR) / ${a}.0;
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
int wRPerm = ${n} - 1 - wR;
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${r}; wC++) {
float dyC = float(dyCCorner + wC) / ${o}.0;
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
2024-01-17 22:44:24 +01:00
int wCPerm = ${r} - 1 - wC;
2021-09-11 17:11:38 +02:00
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;
}
}
2021-04-01 19:39:54 +02:00
}
}
2021-09-11 17:11:38 +02:00
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Pne(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,dy:a}=t,{strides:o,pad:i,dataFormat:u,dimRoundingMode:c,filterShape:l}=r,p=T.convertConv2DDataFormat(u),d=T.computeConv2DInfo(s.shape,l,o,1,i,c,!1,p),h=new Dne(d);return n.runWebGLProgram(h,[s,a],"float32")}var One={kernelName:Lf,backendName:"webgl",kernelFunc:Pne},Mne=class{constructor(e){this.variableNames=["dy","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"strides",type:"vec2"}],this.outputShape=e.inShape,this.enableShapeUniforms=xn(this.outputShape.length);let t=e.filterHeight,n=e.filterWidth,r=t-1-e.padInfo.top,s=n-1-e.padInfo.left;this.userCode=`
const ivec2 pads = ivec2(${r}, ${s});
2023-03-21 13:00:18 +01:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d1 = coords[3];
ivec2 dyCorner = ivec2(coords[1], coords[2]) - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
vec4 result = vec4(0.);
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / strides[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++) {
int wCPerm = ${n} - 1 - wC;
float dyC = float(dyCCorner + wC) / strides[1];
bool idyCVal = (dyC >= 0.0) && (dyC < ${e.outWidth}.0)
&& (fract(dyC) == 0.0);
int idyC = int(dyC);
float dyC2 = float(dyCCorner + wC + 1) / strides[1];
bool idyCVal2 = (dyC2 >= 0.0) && (dyC2 < ${e.outWidth}.0)
&& (fract(dyC2) == 0.0);
int idyC2 = int(dyC2);
if (idyCVal && idyCVal2) {
for (int d2 = 0; d2 < ${e.outChannels}; d2 += 2) {
vec4 wValue = getW(wRPerm, wCPerm, d1, d2);
vec4 dySample = getDy(batch, idyR, idyC, d2);
vec4 dySample2 = (idyC / 2 == idyC2 / 2) ?
dySample : getDy(batch, idyR, idyC2, d2);
vec2 dyValue = mod(float(idyC), 2.) == 0. ?
dySample.xy : dySample.zw;
result.xy += vec2(dot(dyValue, wValue.xy),
dot(dyValue, wValue.zw));
dyValue = mod(float(idyC2), 2.) == 0. ?
dySample2.xy : dySample2.zw;
result.zw += vec2(dot(dyValue, wValue.xy),
dot(dyValue, wValue.zw));
}
} else if (idyCVal) {
for (int d2 = 0; d2 < ${e.outChannels}; d2 += 2) {
vec4 wValue = getW(wRPerm, wCPerm, d1, d2);
vec4 dySample = getDy(batch, idyR, idyC, d2);
vec2 dyValue = mod(float(idyC), 2.) == 0. ?
dySample.xy : dySample.zw;
result.xy += vec2(dot(dyValue, wValue.xy),
dot(dyValue, wValue.zw));
}
} else if (idyCVal2) {
for (int d2 = 0; d2 < ${e.outChannels}; d2 += 2) {
vec4 wValue = getW(wRPerm, wCPerm, d1, d2);
vec4 dySample = getDy(batch, idyR, idyC2, d2);
vec2 dyValue = mod(float(idyC2), 2.) == 0. ?
dySample.xy : dySample.zw;
result.zw += vec2(dot(dyValue, wValue.xy),
dot(dyValue, wValue.zw));
}
}
}
}
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}};function Lne(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{inputShape:o,strides:i,pad:u,dataFormat:c,dimRoundingMode:l}=r,p=T.convertConv2DDataFormat(c),d=T.computeConv2DInfo(o,a.shape,i,1,u,l,!1,p);if(G().getBool("WEBGL_PACK_CONV2DTRANSPOSE")&&p==="channelsLast"){let h=[[d.strideHeight,d.strideWidth]],f=new Mne(d);return n.runWebGLProgram(f,[s,a],"float32",h)}else{let h=new $ne(d);return n.runWebGLProgram(h,[s,a],"float32")}}var Bne={kernelName:Vo,backendName:"webgl",kernelFunc:Lne};function zne(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u}=r,c=T.computeConv3DInfo(s.shape,a.shape,o,u,i),l=new Nne(c);return n.runWebGLProgram(l,[s,a],"float32")}var Wne={kernelName:Uo,backendName:"webgl",kernelFunc:zne};function Vne(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,dy:a}=t,{strides:o,pad:i,filterShape:u}=r,c=T.computeConv3DInfo(s.shape,u,o,1,i),l=new Fne(c);return n.runWebGLProgram(l,[s,a],"float32")}var Une={kernelName:vc,backendName:"webgl",kernelFunc:Vne};function Gne(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{pad:o,strides:i,inputShape:u}=r,c=T.computeConv3DInfo(u,a.shape,i,1,o),l=new Rne(c);return n.runWebGLProgram(l,[s,a],"float32")}var Hne={kernelName:xc,backendName:"webgl",kernelFunc:Gne},jne=Nl+`
2021-09-11 17:11:38 +02:00
return cos(x);
2024-01-17 22:44:24 +01:00
`,qne=`
2023-03-21 13:00:18 +01:00
vec4 result = cos(x);
bvec4 isNaN = isnan(x);
2024-01-17 22:44:24 +01:00
${nu}
2023-03-21 13:00:18 +01:00
return result;
2024-01-17 22:44:24 +01:00
`,Kne=Ze({opSnippet:jne,packedOpSnippet:qne}),Xne={kernelName:Go,backendName:"webgl",kernelFunc:Kne},Yne=`
2021-09-11 17:11:38 +02:00
float e2x = exp(-x);
return (e2x + 1.0 / e2x) / 2.0;
2024-01-17 22:44:24 +01:00
`,Zne=Ze({opSnippet:Yne}),Jne={kernelName:Ho,backendName:"webgl",kernelFunc:Zne},Qne=class{constructor(e,t,n,r,s){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[a,o,i,u]=e,[c]=t,[l,p]=n;this.outputShape=[c,l,p,u];let d=r==="bilinear"?1:0,[h,f]=[`${o-1}.0`,`${i-1}.0`],[g,m,b]=l>1?[`${(o-1)/(l-1)}`,"(y2-y1) * height_ratio",`y1*${h} + float(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${h}`],[y,v,x]=p>1?[`${(i-1)/(p-1)}`,"(x2-x1) * width_ratio",`x1*${f} + float(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${f}`];this.userCode=`
const float height_ratio = float(${g});
2023-03-21 13:00:18 +01:00
const float width_ratio = float(${y});
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
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));
2024-01-17 22:44:24 +01:00
if(bInd < 0 || bInd >= ${a}) {
2021-09-11 17:11:38 +02:00
return;
2021-04-01 19:39:54 +02:00
}
2021-03-31 13:01:22 +02:00
2024-01-17 22:44:24 +01:00
float height_scale = ${m};
float width_scale = ${v};
2021-04-01 19:39:54 +02:00
2023-03-21 13:00:18 +01:00
float in_y = ${b};
2021-09-11 17:11:38 +02:00
if( in_y < 0.0 || in_y > ${h} ) {
2024-01-17 22:44:24 +01:00
setOutput(float(${s}));
2021-09-11 17:11:38 +02:00
return;
}
2024-01-17 22:44:24 +01:00
float in_x = ${x};
if( in_x < 0.0 || in_x > ${f} ) {
setOutput(float(${s}));
2021-09-11 17:11:38 +02:00
return;
}
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
vec2 sourceFracIndexCR = vec2(in_x,in_y);
2024-01-17 22:44:24 +01:00
if(${d} == 1) {
2021-09-11 17:11:38 +02:00
// Compute the four integer indices.
ivec2 sourceFloorCR = ivec2(sourceFracIndexCR);
ivec2 sourceCeilCR = ivec2(ceil(sourceFracIndexCR));
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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);
} else {
// 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);
}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},ere=e=>{let{inputs:t,backend:n,attrs:r}=e,{image:s,boxes:a,boxInd:o}=t,{cropSize:i,method:u,extrapolationValue:c}=r,l=new Qne(s.shape,a.shape,i,u,c);return n.runWebGLProgram(l,[s,a,o],"float32")},tre={kernelName:Ic,backendName:"webgl",kernelFunc:ere},Pd;(function(e){e.Prod="*",e.Sum="+"})(Pd||(Pd={}));var B1=class{constructor(e,t,n,r){this.op=e,this.outputShape=t,this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}];let s=this.outputShape.length,a=this.op===Pd.Prod?"1.0":"0.0",o=n?a:`getX(${z1(s,"coords",this.op)})`,i=this.outputShape[this.outputShape.length-1],u="",c="";n?(u=r?`end != ${i-1}`:"end != 0",c=r?"end + 1":"end - 1"):(u=r?`end + pow2 < ${i}`:"end >= pow2",c=r?"end + pow2":"end - pow2"),this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${ht(s)} coords = getOutputCoords();
int end = ${W1(s,"coords",this.op)};
float val = ${o};
2021-09-11 17:11:38 +02:00
int pow2 = int(pow(2.0, index));
2024-01-17 22:44:24 +01:00
if (${u}) {
int idx = ${c};
${W1(s,"coords",this.op)} = idx;
val ${this.op}= getX(${z1(s,"coords",this.op)});
2021-09-11 17:11:38 +02:00
}
setOutput(val);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function z1(e,t,n){if(e===1)return`${t}`;if(e===2)return`${t}.x, ${t}.y`;if(e===3)return`${t}.x, ${t}.y, ${t}.z`;if(e===4)return`${t}.x, ${t}.y, ${t}.z, ${t}.w`;throw new Error(`Cumulative ${n} for rank ${e} is not yet supported`)}function W1(e,t,n){if(e===1)return`${t}`;if(e===2)return`${t}.y`;if(e===3)return`${t}.z`;if(e===4)return`${t}.w`;throw new Error(`Cumulative ${n} for rank ${e} is not yet supported`)}function gD(e,t,n,r,s,a){let o=t.shape.length,i=T.getAxesPermutation([r],o),u=t;i!=null&&(u=Tn({inputs:{x:t},backend:n,attrs:{perm:i}}));let c=T.getInnerMostAxes(1,o)[0];if(c!==o-1)throw new Error(`WebGL cumprod shader expects an inner-most axis=${t.shape.length-1} but got axis=${r}`);let l=u.shape[c],p=sr({inputs:{x:u},backend:n});for(let d=0;d<=Math.ceil(Math.log2(l))-1;d++){let h=new B1(e,u.shape,!1,a),f=[[d]],g=p;p=n.runWebGLProgram(h,[p],p.dtype,f),n.disposeIntermediateTensorInfo(g)}if(s){let d=new B1(e,u.shape,s,a),h=p;p=n.runWebGLProgram(d,[p],p.dtype),n.disposeIntermediateTensorInfo(h)}if(i!=null){let d=T.getUndoAxesPermutation(i),h=Tn({inputs:{x:p},backend:n,attrs:{perm:d}});return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(u),h}return p}function nre(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,exclusive:o,reverse:i}=r;return gD(Pd.Prod,s,n,a,o,i)}var rre={kernelName:wc,backendName:"webgl",kernelFunc:nre};function sre(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,exclusive:o,reverse:i}=r;return gD(Pd.Sum,s,n,a,o,i)}var are={kernelName:jo,backendName:"webgl",kernelFunc:sre};function ore(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,weights:a}=t,{size:o,binaryOutput:i}=r;if(s.shape.length===1){let u=n.readSync(s.dataId),c=n.readSync(a.dataId),l=KA(u,c,a.dtype,a.shape,o);return n.makeTensorInfo([o],a.dtype,l)}else if(s.shape.length===2){let u=n.bufferSync(s),c=n.bufferSync(a),l=uQ(u,c,o,i);return n.makeTensorInfo(l.shape,a.dtype,l.values)}throw new Error(`Error in denseBincount: input must be at most rank 2, but got rank${s.shape.length}.`)}var ire={kernelName:Ud,backendName:"webgl",kernelFunc:ore},ure=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int h = ${this.getHeightCoordString()};
int w = ${this.getWidthCoordString()};
int d = ${this.getDepthCoordString()};
int in_h = h / ${t};
int offset_h = imod(h, ${t});
int in_w = w / ${t};
int offset_w = imod(w, ${t});
int offset_d = (offset_h * ${t} + offset_w) *
${this.getOutputDepthSize()};
int in_d = d + offset_d;
float result = ${this.getInputSamplingString()};
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01: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 cre(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockSize:a,dataFormat:o}=r,i=s.shape[0],u=o==="NHWC"?s.shape[1]:s.shape[2],c=o==="NHWC"?s.shape[2]:s.shape[3],l=o==="NHWC"?s.shape[3]:s.shape[1],p=u*a,d=c*a,h=l/(a*a),f=o==="NHWC"?[i,p,d,h]:[i,h,p,d],g=new ure(f,a,o);return n.runWebGLProgram(g,[s],s.dtype)}var lre={kernelName:kc,backendName:"webgl",kernelFunc:cre},bD=class{constructor(e,t=!1,n=null,r=!1,s=!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=xn(this.outputShape.length);let a=e.filterHeight,o=e.filterWidth,i=e.outChannels/e.inChannels,u="",c="";n&&(r?u=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getPreluActivationWeightsAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:s?u=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getLeakyreluAlphaAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:u=`
2021-09-11 17:11:38 +02:00
float activation(float x) {
${n}
}
2024-01-17 22:44:24 +01:00
`,c="result = activation(result);");let l=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${u}
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
2024-01-17 22:44:24 +01:00
int d1 = d2 / ${i};
int q = d2 - d1 * ${i};
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
// TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${a}; wR++) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR * dilations[0];
if (xR < 0 || xR >= inDims[0]) {
continue;
}
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${o}; wC++) {
2021-09-11 17:11:38 +02:00
int xC = xCCorner + wC * dilations[1];
if (xC < 0 || xC >= inDims[1]) {
continue;
}
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
float xVal = getX(batch, xR, xC, d1);
float wVal = getW(wR, wC, d1, q);
dotProd += xVal * wVal;
}
}
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
float result = dotProd;
2024-01-17 22:44:24 +01:00
${l}
${c}
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`}},yD=class{constructor(e,t=!1,n=null,r=!1,s=!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=xn(this.outputShape.length);let a=e.outChannels/e.inChannels,o=e.padInfo.left,i=e.strideWidth,u=e.dilationWidth,c=e.filterHeight,l=e.filterWidth,p=l,d=`
2021-09-11 17:11:38 +02:00
int xR; int xC; int xCOffset;
2024-01-17 22:44:24 +01:00
vec4 wTexel; vec4 previous; vec4 final;`;for(let m=0;m<l;m++)d+=`
vec4 xTexelC${m*2};
int xTexelC${m*2}Ready;
vec4 xTexelC${m*2+1};
int xTexelC${m*2+1}Ready;
vec4 xC${m};`;d+=`
for (int r = 0; r < ${c}; r++) {
`;for(let m=0;m<l;m++)d+=`
xTexelC${m*2} = vec4(0.0);
xTexelC${m*2}Ready = 0;
xTexelC${m*2+1} = vec4(0.0);
xTexelC${m*2+1}Ready = 0;
xC${m} = vec4(0.0);`;d+=`
2021-10-22 15:06:43 +02:00
xR = xRCorner + r * dilations[0];
2021-09-11 17:11:38 +02:00
if (xR >=0 && xR < inDims[0]) {
2024-01-17 22:44:24 +01:00
`;for(let m=0;m<(p+1)/2;m++){let b=m*2;if(d+=`
xC = xCCorner + ${b*u};
`,i===1){if(b<l&&(o%2===1?(d+=`
2021-09-11 17:11:38 +02:00
xCOffset = xC + 1;
2023-03-21 13:00:18 +01:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b}.zw = vec2(0.0);
2021-09-11 17:11:38 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`,u===1&&b>0?d+=`
2023-03-21 13:00:18 +01:00
xC${b} = vec4(xTexelC${b-2}.zw, xTexelC${b}.xy);
2024-01-17 22:44:24 +01:00
`:d+=`
2021-09-11 17:11:38 +02:00
xCOffset = xC + 1 - 2;
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1]) {
previous = getX(batch, xR, xCOffset, d1);
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
previous.zw = vec2(0.0);
}
2021-07-29 15:05:49 +02:00
2023-03-21 13:00:18 +01:00
xC${b} = vec4(previous.zw, xTexelC${b}.xy);
2021-04-01 19:39:54 +02:00
} else {
2023-03-21 13:00:18 +01:00
xC${b} = vec4(0.0, 0.0, xTexelC${b}.xy);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`):d+=`
2023-03-21 13:00:18 +01:00
if (xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
2021-09-11 17:11:38 +02:00
if (xC + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b}.zw = vec2(0.0);
2021-09-11 17:11:38 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2023-03-21 13:00:18 +01:00
xC${b} = xTexelC${b};
2024-01-17 22:44:24 +01:00
`,b+1<l)){let y=o%2===0?w.nearestLargerEven(u):u;u%2===0&&o%2===1||u%2!==0&&o%2!==1?(d+=`
2023-03-21 13:00:18 +01:00
xCOffset = xC + imod(pads[1], 2) + ${y};
2021-09-11 17:11:38 +02:00
2023-03-21 13:00:18 +01:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b+1}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b+1}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`,u>1?d+=`
2021-09-11 17:11:38 +02:00
xCOffset -= 2;
2022-08-24 14:21:15 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1]) {
previous = getX(batch, xR, xCOffset, d1);
2023-03-21 13:00:18 +01:00
xC${b+1} = vec4(previous.zw, xTexelC${b+1}.xy);
2022-08-24 14:21:15 +02:00
} else {
2023-03-21 13:00:18 +01:00
xC${b+1} = vec4(0.0, 0.0, xTexelC${b+1}.xy);
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`:d+=`
2023-03-21 13:00:18 +01:00
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.xy);
2024-01-17 22:44:24 +01:00
`):y===1?d+=`
2023-03-21 13:00:18 +01:00
xC${b+1} = xTexelC${b};
2024-01-17 22:44:24 +01:00
`:d+=`
2023-03-21 13:00:18 +01:00
xCOffset = xC + ${y};
2022-10-18 13:23:49 +02:00
2023-03-21 13:00:18 +01:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
if (xCOffset + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b+1}.zw = vec2(0.0);
2021-09-11 17:11:38 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b+1}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
2023-03-21 13:00:18 +01:00
xC${b+1} = xTexelC${b+1};
2024-01-17 22:44:24 +01:00
`}}else b<l&&(o%2===1?(d+=`
2021-09-11 17:11:38 +02:00
xCOffset = xC + 1 - strides[1];
2023-03-21 13:00:18 +01:00
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
2023-03-21 13:00:18 +01:00
if(xC + 1 >= 0 && xC + 1 < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xC + 1, d1);
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xC + 2 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b+1}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b+1}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2023-03-21 13:00:18 +01:00
xC${b} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
2024-01-17 22:44:24 +01:00
`,b+1<l&&(d+=`
2021-09-11 17:11:38 +02:00
final = vec4(0.0);
xCOffset = xC + 1 + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1]) {
final = getX(batch, xR, xCOffset, d1);
}
2023-03-21 13:00:18 +01:00
xC${b+1} = vec4(xTexelC${b+1}.xy, final.xy);
2024-01-17 22:44:24 +01:00
`)):(d+=`
2023-03-21 13:00:18 +01:00
if(xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
2021-09-11 17:11:38 +02:00
if (xC + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
xCOffset = xC + strides[1];
2023-03-21 13:00:18 +01:00
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
if (xCOffset + 1 >= inDims[1]) {
2023-03-21 13:00:18 +01:00
xTexelC${b+1}.zw = vec2(0.);
2021-09-11 17:11:38 +02:00
}
2023-03-21 13:00:18 +01:00
xTexelC${b+1}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2023-03-21 13:00:18 +01:00
xC${b} = vec4(
xTexelC${b}.xy, xTexelC${b+1}.xy);
2024-01-17 22:44:24 +01:00
`,b+1<l&&(d+=`
2023-03-21 13:00:18 +01:00
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
2024-01-17 22:44:24 +01:00
`)));b<l&&(d+=`
2023-03-21 13:00:18 +01:00
wTexel = getW(r, ${b}, d1, q);
dotProd += xC${b} * vec4(wTexel.xz, wTexel.xz);
2024-01-17 22:44:24 +01:00
`,b+1<l&&(d+=`
2023-03-21 13:00:18 +01:00
wTexel = getW(r, ${b+1}, d1, q);
dotProd += xC${b+1} * vec4(wTexel.xz, wTexel.xz);
2024-01-17 22:44:24 +01:00
`))}d+=`
2021-10-22 15:06:43 +02:00
}
2024-01-17 22:44:24 +01:00
`,d+=`
2021-10-22 15:06:43 +02:00
}
2024-01-17 22:44:24 +01:00
`;let h="",f="";n&&(r?h=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
${n}
2024-01-17 22:44:24 +01:00
}`:s?h=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getLeakyreluAlphaAtOutCoords();
${n}
}`:h=`vec4 activation(vec4 x) {
${n}
2024-01-17 22:44:24 +01:00
}`,f="result = activation(result);");let g=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
2021-09-11 17:11:38 +02:00
${h}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
2024-01-17 22:44:24 +01:00
int d1 = d2 / ${a};
int q = d2 - d1 * ${a};
2021-09-11 17:11:38 +02:00
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);
2024-01-17 22:44:24 +01:00
${d}
2021-09-11 17:11:38 +02:00
vec4 result = dotProd - vec4(0.000000000000001);
2024-01-17 22:44:24 +01:00
${g}
2022-10-18 13:10:36 +02:00
${f}
2021-09-11 17:11:38 +02:00
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function dre(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u,dimRoundingMode:c}=r,l=u;l==null&&(l=[1,1]),w.assert(T.eitherStridesOrDilationsAreOne(o,l),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${o} and dilations '${l}'`);let p=T.computeConv2DInfo(s.shape,a.shape,o,l,i,c,!0),d;G().getBool("WEBGL_PACK_DEPTHWISECONV")&&p.strideWidth<=2&&p.outChannels/p.inChannels===1?d=new yD(p):d=new bD(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,[s,a],"float32",h)}var pre={kernelName:qo,backendName:"webgl",kernelFunc:dre},hre=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,s=e.padInfo.left,a=e.outChannels/e.inChannels;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int wR = coords.x;
int wC = coords.y;
int d1 = coords.z;
int dm = coords.w;
2024-01-17 22:44:24 +01:00
int d2 = d1 * ${a} + dm;
2021-09-11 17:11:38 +02:00
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++) {
2024-01-17 22:44:24 +01:00
int xR = wR + yR * ${t} - ${r};
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int yC = 0; yC < ${e.outWidth}; yC++) {
2024-01-17 22:44:24 +01:00
int xC = wC + yC * ${n} - ${s};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},fre=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,s=e.strideWidth,a=t-1-e.padInfo.top,o=n-1-e.padInfo.left,i=e.outChannels/e.inChannels;this.userCode=`
const ivec2 pads = ivec2(${a}, ${o});
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
ivec4 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
int batch = coords[0];
2021-09-11 17:11:38 +02:00
int d1 = coords[3];
ivec2 dyCorner = coords.yz - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float dotProd = 0.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${t}; wR++) {
2024-01-17 22:44:24 +01:00
float dyR = float(dyRCorner + wR) / ${r}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wRPerm = ${t} - 1 - wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${n}; wC++) {
2024-01-17 22:44:24 +01:00
float dyC = float(dyCCorner + wC) / ${s}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wCPerm = ${n} - 1 - wC;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// TO DO: Vec4 over the channelMul
2024-01-17 22:44:24 +01:00
for (int dm = 0; dm < ${i}; dm++) {
int d2 = d1 * ${i} + dm;
2021-09-11 17:11:38 +02:00
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, dm);
dotProd += xValue * wValue;
}
}
}
setOutput(dotProd);
}
2024-01-17 22:44:24 +01:00
`}};function mre(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,dy:a}=t,{strides:o,dilations:i,pad:u,dimRoundingMode:c,filterShape:l}=r,p=T.computeConv2DInfo(s.shape,l,o,i,u,c,!0),d=new hre(p);return n.runWebGLProgram(d,[s,a],"float32")}var gre={kernelName:Bf,backendName:"webgl",kernelFunc:mre};function bre(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{strides:o,dilations:i,pad:u,dimRoundingMode:c,inputShape:l}=r,p=T.computeConv2DInfo(l,a.shape,o,i,u,c,!0),d=new fre(p);return n.runWebGLProgram(d,[s,a],"float32")}var yre={kernelName:zf,backendName:"webgl",kernelFunc:bre},vre=class{constructor(e){this.variableNames=["X"],this.outputShape=[e,e],this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
float val = coords[0] == coords[1] ? getX(coords[0]) : 0.0;
setOutput(val);
}
2024-01-17 22:44:24 +01:00
`}};function xre(e){let{inputs:t,backend:n}=e,{x:r}=t,s=[...r.shape,...r.shape],a=w.sizeFromShape(r.shape),o=pe({inputs:{x:r},backend:n,attrs:{shape:[a]}}),i=new vre(a),u=n.runWebGLProgram(i,[o],o.dtype),c=pe({inputs:{x:u},backend:n,attrs:{shape:s}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(u),c}var wre={kernelName:Gd,backendName:"webgl",kernelFunc:xre},Ire=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:r,strideHeight:s,strideWidth:a,filterHeight:o,filterWidth:i,dilationHeight:u,dilationWidth:c}=e,{top:l,left:p}=r;this.userCode=`
const ivec2 strides = ivec2(${s}, ${a});
const ivec2 pads = ivec2(${l}, ${p});
2021-09-11 17:11:38 +02:00
const float neg_infinity = -3.4e38;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float curVal = neg_infinity;
2024-01-17 22:44:24 +01:00
for (int h = 0; h < ${o}; h++) {
int hIn = hBeg + h * ${u};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
if (hIn >= 0 && hIn < ${t}) {
2024-01-17 22:44:24 +01:00
for (int w = 0; w < ${i}; w++) {
int wIn = wBeg + w * ${c};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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;
}
}
}
}
2021-07-29 15:05:49 +02:00
}
2021-09-11 17:11:38 +02:00
float result = curVal;
setOutput(result);
2021-07-29 15:05:49 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function kre(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u}=r,c=T.computeDilation2DInfo(s.shape,a.shape,o,i,"NHWC",u),l,p=new Ire(c);l=n.runWebGLProgram(p,[s,a],"float32");let d=pe({inputs:{x:l},backend:n,attrs:{shape:c.outShape}});return n.disposeIntermediateTensorInfo(l),d}var Sre={kernelName:Ko,backendName:"webgl",kernelFunc:kre};function Cre(e){let{inputs:t,backend:n,attrs:r}=e,{equation:s}=r,a=t,{allDims:o,summedDims:i,idDims:u}=T.decodeEinsumEquation(s,a.length);T.checkEinsumDimSizes(o.length,u,a);let{path:c,steps:l}=T.getEinsumComputePath(i,u),p=l.length,d=null,h=o.length,f=[];for(let g=0;g<p;++g){for(let m of l[g]){let{permutationIndices:b,expandDims:y}=T.getEinsumPermutation(h,u[m]),v;T.isIdentityPermutation(b)?v=a[m]:(v=Tn({inputs:{x:a[m]},backend:n,attrs:{perm:b}}),f.push(v));let x=v.shape.slice();for(let k=0;k<y.length;++k)x.splice(y[k],0,1);w.arraysEqual(v.shape,x)||(v=pe({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),d===null?d=v:(d=y0({inputs:{a:v,b:d},backend:n}),f.push(d))}g<p-1&&(c[g]>=0&&(d=ng({inputs:{x:d},backend:n,attrs:{axis:c[g]-(o.length-h),keepDims:!1}}),f.push(d)),h--)}for(let g of f)g!==d&&n.disposeIntermediateTensorInfo(g);return d}var Tre={kernelName:Vf,backendName:"webgl",kernelFunc:Cre},Nre="return (x >= 0.0) ? x : (exp(x) - 1.0);",_re=`
2021-09-11 17:11:38 +02:00
vec4 result;
result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);
return result;
2024-01-17 22:44:24 +01:00
`,Ere=Ze({opSnippet:Nre,packedOpSnippet:_re}),Are={kernelName:Yo,backendName:"webgl",kernelFunc:Ere},Dre="return (b >= 0.0) ? a : a * (b + 1.0);",$re=`
2021-09-11 17:11:38 +02:00
vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));
return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));
2024-01-17 22:44:24 +01:00
`,Fre=e=>{let{inputs:t,backend:n}=e,{dy:r,y:s}=t,a=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Tl($re,r.shape,s.shape):new To(Dre,r.shape,s.shape);return n.runWebGLProgram(a,[r,s],r.dtype)},Rre={kernelName:Sc,backendName:"webgl",kernelFunc:Fre},Pre=`
2021-09-11 17:11:38 +02:00
return vec4(equal(a, b));
2024-01-17 22:44:24 +01:00
`,Ore="return float(a == b);",Mre=mn({opSnippet:Ore,packedOpSnippet:Pre,dtype:"bool",cpuKernelImpl:hQ}),Lre={kernelName:Cc,backendName:"webgl",kernelFunc:Mre},Bre=`
2021-09-11 17:11:38 +02:00
// Error function is calculated approximately with elementary function.
// See "Handbook of Mathematical Functions with Formulas,
// Graphs, and Mathematical Tables", Abramowitz and Stegun.
2024-01-17 22:44:24 +01:00
float p = ${T.ERF_P};
float a1 = ${T.ERF_A1};
float a2 = ${T.ERF_A2};
float a3 = ${T.ERF_A3};
float a4 = ${T.ERF_A4};
float a5 = ${T.ERF_A5};
2021-09-11 17:11:38 +02:00
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));
2024-01-17 22:44:24 +01:00
`,zre=Ze({opSnippet:Bre}),Wre={kernelName:Zo,backendName:"webgl",kernelFunc:zre},Vre=Nl+`
2022-01-14 16:04:13 +01:00
return exp(x);
2024-01-17 22:44:24 +01:00
`,Ure=`
2022-01-14 16:04:13 +01:00
vec4 result = exp(x);
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2024-01-17 22:44:24 +01:00
`,vD=Ze({opSnippet:Vre,packedOpSnippet:Ure,cpuKernelImpl:fQ,dtype:"float32"}),Gre={kernelName:Jo,backendName:"webgl",kernelFunc:vD};function kx(e){let{inputs:t,attrs:n,backend:r}=e,{dim:s}=n,{input:a}=t,o=a.shape.length,i=a.shape.slice(),u=s;return s<0&&(w.assert(-(o+1)<=s,()=>`Axis must be in the interval [${-(o+1)}, ${o}]`),u=o+s+1),i.splice(u,0,1),pe({inputs:{x:a},backend:r,attrs:{shape:i}})}var Hre={kernelName:Tc,backendName:"webgl",kernelFunc:kx},V1="return exp(x) - 1.0;",jre=Ze({opSnippet:V1,packedOpSnippet:V1,cpuKernelImpl:mQ}),qre={kernelName:Qo,backendName:"webgl",kernelFunc:jre},U1=class{constructor(e,t,n){this.variableNames=["real","imag"];let r=t[1];this.outputShape=t;let s=n?`2.0 * ${Math.PI}`:`-2.0 * ${Math.PI}`,a=n?`${r}.0`:"1.0",o;if(e==="real")o="return real * expR - imag * expI;";else if(e==="imag")o="return real * expI + imag * expR;";else throw new Error(`FFT component must be either "real" or "imag", got ${e}.`);this.userCode=`
const float exponentMultiplier = ${s};
2021-09-11 17:11:38 +02:00
float unaryOpComplex(float real, float expR, float imag, float expI) {
2024-01-17 22:44:24 +01:00
${o}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
float mulMatDFT(int batch, int index) {
2024-01-17 22:44:24 +01:00
float indexRatio = float(index) / float(${r});
2021-09-11 17:11:38 +02:00
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio;
float result = 0.0;
2024-01-17 22:44:24 +01:00
for (int i = 0; i < ${r}; i++) {
2021-09-11 17:11:38 +02:00
// 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 +=
2024-01-17 22:44:24 +01:00
unaryOpComplex(real, expR, imag, expI) / ${a};
2021-09-11 17:11:38 +02:00
}
return result;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
setOutput(mulMatDFT(coords[0], coords[1]));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function xD(e,t,n){let r=n.texData.get(e.dataId),s=w.sizeFromShape(e.shape),a=e.shape[e.shape.length-1],o=s/a,i=pe({inputs:{x:e},backend:n,attrs:{shape:[o,a]}}),u=i.shape,c=new U1("real",u,t),l=new U1("imag",u,t),p=[{dataId:r.complexTensorInfos.real.dataId,dtype:r.complexTensorInfos.real.dtype,shape:u},{dataId:r.complexTensorInfos.imag.dataId,dtype:r.complexTensorInfos.imag.dtype,shape:u}],d=n.runWebGLProgram(c,p,"float32"),h=n.runWebGLProgram(l,p,"float32"),f=Oa({inputs:{real:d,imag:h},backend:n});n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h);let g=pe({inputs:{x:f},backend:n,attrs:{shape:e.shape}});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(f),g}function Kre(e){let{inputs:t,backend:n}=e,{input:r}=t;return xD(r,!1,n)}var Xre={kernelName:Uf,backendName:"webgl",kernelFunc:Kre},Yre=class{constructor(e,t){this.outputShape=[],this.customUniforms=[{name:"value",type:"float"}],this.variableNames=["x"],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
// Input can be obtained from uniform value.
setOutput(value);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Lp(e){let{backend:t,attrs:n}=e,{shape:r,value:s}=n,{dtype:a}=n;if(a=a||w.inferDtype(s),a==="string"){let o=w.getArrayFromDType(a,w.sizeFromShape(r));return o.fill(s),t.makeTensorInfo(r,a,o)}else{let o=new Yre(r,s),i=[[s]];return t.runWebGLProgram(o,[],a,i)}}var Zre={kernelName:Hd,backendName:"webgl",kernelFunc:Lp},Jre=class{constructor(e){this.variableNames=["Image"],this.outputShape=[];let t=e[2];this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int x = coords[2];
int coordX = ${t} - x - 1;
float outputValue;
if(coordX >= 0 && coordX < ${t}) {
outputValue = getImage(coords[0], coords[1], coordX, coords[3]);
} else {
outputValue = getImage(coords[0], coords[1], coords[2], coords[3]);
}
setOutput(outputValue);
}
2024-01-17 22:44:24 +01:00
`}},Qre={kernelName:Nc,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,r=t,s=new Jre(n.shape);return r.runWebGLProgram(s,[n],n.dtype)}},G1="return floor(x);",ese=Ze({opSnippet:G1,packedOpSnippet:G1,cpuKernelImpl:gQ}),tse={kernelName:ei,backendName:"webgl",kernelFunc:ese},nse=`
2021-09-11 17:11:38 +02:00
float s = sign(a) * sign(b);
int ia = round(a);
int ib = round(b);
if (ib != 0) {
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
return float(idiv(ia, ib, s));
} else {
return NAN;
2021-04-30 18:01:04 +02:00
}
2024-01-17 22:44:24 +01:00
`,rse=`
2021-09-11 17:11:38 +02:00
ivec4 ia = round(a);
ivec4 ib = round(b);
bvec4 cond = notEqual(ib, ivec4(0));
ivec4 result = ivec4(0);
vec4 s = sign(a) * sign(b);
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
if (cond[0]) {
result[0] = idiv(ia[0], ib[0], s[0]);
2021-04-30 18:01:04 +02:00
}
2021-09-11 17:11:38 +02:00
if (cond[1]) {
result[1] = idiv(ia[1], ib[1], s[1]);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if (cond[2]) {
result[2] = idiv(ia[2], ib[2], s[2]);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if (cond[3]) {
result[3] = idiv(ia[3], ib[3], s[3]);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
return vec4(result);
2024-01-17 22:44:24 +01:00
`,sse=mn({opSnippet:nse,packedOpSnippet:rse,dtype:"int32"}),ase={kernelName:ti,backendName:"webgl",kernelFunc:sse},ose=class{constructor(e){this.variableNames=["A"];let t=An(),[n,r]=e;this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
int texR = coords[0];
int texC = coords[1];
int depth = coords[2];
2024-01-17 22:44:24 +01:00
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${r}.0, ${n}.0);
2021-09-11 17:11:38 +02:00
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;
2021-03-31 13:01:22 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(floor(value * 255.0 + 0.5));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},ise=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=An(),[n,r]=e;this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
int texR = coords[0];
int texC = coords[1];
int depth = coords[2];
vec4 result = vec4(0.);
for(int row=0; row<=1; row++) {
for(int col=0; col<=1; col++) {
texC = coords[1] + row;
depth = coords[2] + col;
vec2 uv = (vec2(texC, texR) + halfCR) /
2024-01-17 22:44:24 +01:00
vec2(${r}.0, ${n}.0);
2021-09-11 17:11:38 +02:00
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);
}
2021-03-31 13:01:22 +02:00
}
2021-09-11 17:11:38 +02:00
${t.output} = result;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},use={kernelName:of,backendName:"webgl",kernelFunc:cse},Ou,Tv=G().getBool("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU");function cse(e){let{inputs:t,backend:n,attrs:r}=e,{pixels:s}=t,{numChannels:a}=r,o=typeof HTMLVideoElement!="undefined"&&s instanceof HTMLVideoElement,i=typeof HTMLImageElement!="undefined"&&s instanceof HTMLImageElement,[u,c]=o?[s.videoWidth,s.videoHeight]:[s.width,s.height],l=[c,u],p=[c,u,a];if(i||o){let g=G().getBool("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU");(Ou==null||g!==Tv)&&(Tv=g,Ou=document.createElement("canvas").getContext("2d",{willReadFrequently:Tv})),Ou.canvas.width=u,Ou.canvas.height=c,Ou.drawImage(s,0,0,u,c),s=Ou.canvas}let d=n.makeTensorInfo(l,"int32");n.texData.get(d.dataId).usage=lr.PIXELS,n.gpgpu.uploadPixelDataToTexture(n.getTexture(d.dataId),s);let h=G().getBool("WEBGL_PACK")?new ise(p):new ose(p),f=n.runWebGLProgram(h,[d],"int32");return n.disposeData(d.dataId),f}function lse(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a,bias:o,preluActivationWeights:i}=t,{strides:u,pad:c,dataFormat:l,dilations:p,dimRoundingMode:d,activation:h,leakyreluAlpha:f}=r,g=T.convertConv2DDataFormat(l),m=T.computeConv2DInfo(s.shape,a.shape,u,p,c,d,!1,g),b,y=[],v=o!=null,x=i!=null,k=h==="leakyrelu",S=()=>{let E=[s,a],$=(F,D)=>{if(D==="NCHW"&&F.shape.length===1&&F.shape[0]!==1){let R=pe({inputs:{x:F},backend:n,attrs:{shape:[F.shape[0],1,1]}});return y.push(R),R}return F};if(v&&E.push($(o,l)),x&&E.push($(i,l)),k){let F=n.makeTensorInfo([],"float32",w.createScalarValue(f,"float32"));E.push(F),y.push(F)}return E};if(m.filterHeight===1&&m.filterWidth===1&&m.dilationHeight===1&&m.dilationWidth===1&&m.strideHeight===1&&m.strideWidth===1&&(m.padInfo.type==="SAME"||m.padInfo.type==="VALID"))b=fD({x:s,filter:a,convInfo:m,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});else if(m.strideWidth<=2&&g==="channelsLast"&&G().getBool("WEBGL_EXP_CONV")){let E=h?Fd(h,!0):null,$=new hD(m,v,E,x,k),F=[[m.padInfo.top,m.padInfo.left],[m.strideHeight,m.strideWidth],[m.dilationHeight,m.dilationWidth],[m.inHeight,m.inWidth]],D=S();b=n.runWebGLProgram($,D,"float32",F)}else if(G().getBool("WEBGL_CONV_IM2COL"))b=mD({x:s,filter:a,convInfo:m,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});else{let E=h?Fd(h,!1):null,$=new pD(m,v,E,x,k),F=S();b=n.runWebGLProgram($,F,"float32")}let N=pe({inputs:{x:b},backend:n,attrs:{shape:m.outShape}});return y.push(b),y.forEach(E=>n.disposeIntermediateTensorInfo(E)),N}var dse={kernelName:co,backendName:"webgl",kernelFunc:lse};function pse(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a,bias:o,preluActivationWeights:i}=t,{strides:u,pad:c,dilations:l,dimRoundingMode:p,activation:d,leakyreluAlpha:h}=r,f=[],g=l;g==null&&(g=[1,1]),w.assert(T.eitherStridesOrDilationsAreOne(u,g),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${g}'`);let m=T.computeConv2DInfo(s.shape,a.shape,u,g,c,p,!0),b=G().getBool("WEBGL_PACK_DEPTHWISECONV")&&m.strideWidth<=2&&m.outChannels/m.inChannels===1,y=d?Fd(d,b):null,v=[s,a],x=o!=null,k=i!=null,S=d==="leakyrelu";if(x&&v.push(o),k&&v.push(i),S){let F=n.makeTensorInfo([],"float32",w.createScalarValue(h,"float32"));v.push(F),f.push(F)}let N;b?N=new yD(m,x,y,k,S):N=new bD(m,x,y,k,S);let E=[[m.padInfo.top,m.padInfo.left],[m.strideHeight,m.strideWidth],[m.dilationHeight,m.dilationWidth],[m.inHeight,m.inWidth]],$=n.runWebGLProgram(N,v,"float32",E);return f.forEach(F=>n.disposeIntermediateTensorInfo(F)),$}var hse={kernelName:lo,backendName:"webgl",kernelFunc:pse},fse=class{constructor(e,t,n,r){this.sliceDim=e,this.strides=t,this.paramsShape=r,this.variableNames=["x","indices"],this.outputShape=n;let s=ht(n.length),a=`
int index;`;for(let o=0;o<this.sliceDim;o++)a+=`
index = round(getIndices(coords[0], ${o}));
2022-10-09 19:40:33 +02:00
out_of_bounds = out_of_bounds || index < 0;
2024-01-17 22:44:24 +01:00
out_of_bounds = out_of_bounds || index >= ${this.paramsShape[o]};
flattenIndex += index * ${this.strides[o]};`;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${s} coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int flattenIndex = 0;
2022-07-25 14:23:57 +02:00
bool out_of_bounds = false;
2022-10-09 19:40:33 +02:00
2024-01-17 22:44:24 +01:00
${a}
2022-10-09 19:40:33 +02:00
2022-07-25 14:23:57 +02:00
setOutput(out_of_bounds ? 0.0 : getX(flattenIndex, coords[1]));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function mse(e){let{inputs:t,backend:n}=e,{params:r,indices:s}=t,a=s.shape,o=a[a.length-1],i=w.sizeFromShape(r.shape),[u,c,l,p]=T.prepareAndValidate(r,s),d=pe({inputs:{x:s},backend:n,attrs:{shape:[c,o]}}),h=pe({inputs:{x:r},backend:n,attrs:{shape:[w.sizeFromShape(r.shape)/l,l]}});if(n.shouldExecuteOnCPU([r,s])||r.dtype==="string"){let b=n.readSync(s.dataId),y=n.bufferSync(r),v=bQ(b,y,r.dtype,c,o,l,p,r.shape,i);return n.makeTensorInfo(u,r.dtype,v.values)}let f=new fse(o,p,[c,l],r.shape),g=n.runWebGLProgram(f,[h,d],h.dtype),m=pe({inputs:{x:g},backend:n,attrs:{shape:u}});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(g),m}var gse={kernelName:Ec,backendName:"webgl",kernelFunc:mse},bse=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=ht(this.rank),r=yse(e,2);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${n} resRC = getOutputCoords();
2022-01-14 16:04:13 +01:00
int index = int(getIndices(resRC.x, resRC.z));
float inBounds = (index >= 0) && (index < ${e[2]}) ? 1.0 : 0.0;
2024-01-17 22:44:24 +01:00
setOutput(inBounds * getA(${r}));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function yse(e,t){let n=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[];for(let s=0;s<e.length;s++)s===2?r.push("index"):r.push(`${n[s]}`);return r.join()}function wD(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,indices:a}=t,{axis:o,batchDims:i}=r,u=w.parseAxisParam(o,s.shape)[0];if(G().get("DEBUG")){let y=n.readSync(a.dataId),v=s.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 c=T.segment_util.collectGatherOpShapeInfo(s,a,u,i),l=w.sizeFromShape(a.shape),p=[],d=pe({inputs:{x:s},backend:n,attrs:{shape:[c.batchSize,c.outerSize,c.dimSize,c.sliceSize]}}),h=pe({inputs:{x:a},backend:n,attrs:{shape:[c.batchSize,l/c.batchSize]}});p.push(d),p.push(h);let f=[c.batchSize,c.outerSize,l/c.batchSize,c.sliceSize];if(n.shouldExecuteOnCPU([s,a])||s.dtype==="string"){let y=n.bufferSync(h),v=n.bufferSync(d),x=yQ(v,y,f);return p.forEach(k=>n.disposeIntermediateTensorInfo(k)),n.makeTensorInfo(c.outputShape,x.dtype,x.values)}let g=new bse(d.shape,f),m=n.runWebGLProgram(g,[d,h],d.dtype);p.push(m);let b=pe({inputs:{x:m},backend:n,attrs:{shape:c.outputShape}});return p.forEach(y=>n.disposeIntermediateTensorInfo(y)),b}var vse={kernelName:_c,backendName:"webgl",kernelFunc:wD},xse="return float(a > b);",wse=`
2021-09-11 17:11:38 +02:00
return vec4(greaterThan(a, b));
2024-01-17 22:44:24 +01:00
`,Ise=mn({opSnippet:xse,packedOpSnippet:wse,cpuKernelImpl:vQ,dtype:"bool"}),kse={kernelName:Ac,backendName:"webgl",kernelFunc:Ise},Sse="return float(a >= b);",Cse=`
2021-09-11 17:11:38 +02:00
return vec4(greaterThanEqual(a, b));
2024-01-17 22:44:24 +01:00
`,Tse=mn({opSnippet:Sse,packedOpSnippet:Cse,dtype:"bool",cpuKernelImpl:xQ}),Nse={kernelName:ri,backendName:"webgl",kernelFunc:Tse};function _se(e){let{inputs:t,backend:n}=e,{input:r}=t;return xD(r,!0,n)}var Ese={kernelName:Gf,backendName:"webgl",kernelFunc:_se},Ase="return float(!isnan(x) && !isinf(x));",Dse=Ze({opSnippet:Ase,dtype:"bool"}),$se={kernelName:ai,backendName:"webgl",kernelFunc:Dse},Fse="return float(isinf(x));",Rse=Ze({opSnippet:Fse,dtype:"bool"}),Pse={kernelName:oi,backendName:"webgl",kernelFunc:Rse},Ose="return float(isnan(x));",Mse=Ze({opSnippet:Ose,dtype:"bool"}),Lse={kernelName:ii,backendName:"webgl",kernelFunc:Mse},Bse="return float(a < b);",zse=`
2021-09-11 17:11:38 +02:00
return vec4(lessThan(a, b));
2024-01-17 22:44:24 +01:00
`,Wse=mn({opSnippet:Bse,packedOpSnippet:zse,cpuKernelImpl:wQ,dtype:"bool"}),Vse={kernelName:Dc,backendName:"webgl",kernelFunc:Wse},Use="return float(a <= b);",Gse=`
2021-09-11 17:11:38 +02:00
return vec4(lessThanEqual(a, b));
2024-01-17 22:44:24 +01:00
`,Hse=mn({opSnippet:Use,packedOpSnippet:Gse,cpuKernelImpl:IQ,dtype:"bool"}),jse={kernelName:$c,backendName:"webgl",kernelFunc:Hse};function qse(e){let{backend:t,attrs:n}=e,{start:r,stop:s,num:a}=n,o=kQ(r,s,a);return t.makeTensorInfo([o.length],"float32",o)}var Kse={kernelName:Fc,backendName:"webgl",kernelFunc:qse},Xse=Nl+`
2022-01-14 16:04:13 +01:00
return x < 0.0 ? 0./0. : log(x);
2024-01-17 22:44:24 +01:00
`,Yse=`
2021-09-11 17:11:38 +02:00
vec4 result = log(x);
2022-01-14 16:04:13 +01:00
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);
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,Zse=Ze({opSnippet:Xse,packedOpSnippet:Yse,cpuKernelImpl:SQ}),Jse={kernelName:ci,backendName:"webgl",kernelFunc:Zse},Qse=Nl+`
2022-01-14 16:04:13 +01:00
return log(1.0 + x);
2024-01-17 22:44:24 +01:00
`,eae=Ze({opSnippet:Qse}),tae={kernelName:li,backendName:"webgl",kernelFunc:eae},nae="return float(a >= 1.0 && b >= 1.0);",rae=`
2021-09-11 17:11:38 +02:00
return vec4(
vec4(greaterThanEqual(a, vec4(1.0))) *
vec4(greaterThanEqual(b, vec4(1.0))));
2024-01-17 22:44:24 +01:00
`,sae=mn({opSnippet:nae,packedOpSnippet:rae,dtype:"bool"}),aae={kernelName:Rc,backendName:"webgl",kernelFunc:sae},oae="return float(!(x >= 1.0));",iae=Ze({opSnippet:oae}),uae={kernelName:Pc,backendName:"webgl",kernelFunc:iae},cae="return float(a >= 1.0 || b >= 1.0);",lae=`
2021-09-11 17:11:38 +02:00
return min(
vec4(greaterThanEqual(a, vec4(1.0))) +
vec4(greaterThanEqual(b, vec4(1.0))),
vec4(1.0));
2024-01-17 22:44:24 +01:00
`,dae=mn({opSnippet:cae,packedOpSnippet:lae,dtype:"bool"}),pae={kernelName:Oc,backendName:"webgl",kernelFunc:dae},hae=class{constructor(e,t,n,r,s){this.variableNames=["x"],this.outputShape=[];let a=t,o=e[3]-1;this.outputShape=e;let i,u=`float(${n}) + float(${r}) * sum`;s===.5?i=`inversesqrt(${u})`:s===1?i=`1.0/(${u})`:i=`exp(log(${u}) * float(-${s}));`,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
int d = coords[3];
float x = getX(b, r, c, d);
float sum = 0.0;
2024-01-17 22:44:24 +01:00
for (int j = -${a}; j <= ${a}; j++) {
2021-09-11 17:11:38 +02:00
int idx = d + j;
2024-01-17 22:44:24 +01:00
if (idx >= 0 && idx <= ${o}) {
2021-09-11 17:11:38 +02:00
float z = getX(b, r, c, idx);
sum += z * z;
}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
float val = x * ${i};
2021-09-11 17:11:38 +02:00
setOutput(val);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},fae=class{constructor(e,t,n,r,s){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let a=t,o=e[3]-1;this.outputShape=e;let i,u=`float(${n}) + float(${r}) * sum`;s===.5?i=`inversesqrt(${u})`:s===1?i=`1.0/(${u})`:i=`exp(log(${u}) * float(-${s}));`,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords.x;
int r = coords.y;
int c = coords.z;
int d = coords.w;
bool hasNextCol = d < ${this.outputShape[3]};
bool hasNextRow = c < ${this.outputShape[2]};
vec4 sum = vec4(0.);
vec4 xFragAtOutputCoords = getX(b, r, c, d);
vec4 xAtOutputCoords = vec4(
getChannel(xFragAtOutputCoords, vec2(c, d)),
hasNextCol ?
getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,
hasNextRow ?
getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,
(hasNextRow && hasNextCol) ?
getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0
);
2024-01-17 22:44:24 +01:00
int firstChannel = d - ${a};
2021-09-11 17:11:38 +02:00
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));
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
ivec2 depth = ivec2(d, d + 1);
2024-01-17 22:44:24 +01:00
for (int j = - ${a}; j <= ${a}; j++) {
2021-09-11 17:11:38 +02:00
ivec2 idx = depth + j;
bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));
2024-01-17 22:44:24 +01:00
bvec2 belowUpperBound = lessThanEqual(idx, ivec2(${o}));
2021-09-11 17:11:38 +02:00
bool depthInRange = aboveLowerBound.x && belowUpperBound.x;
bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;
if(depthInRange || depthPlusOneInRange){
vec4 z = vec4(0.);
vec4 xFragAtCurrentDepth;
z.xz = cache.xy;
if(depthPlusOneInRange && hasNextCol){
xFragAtCurrentDepth = idx.y != d ?
getX(b, r, c, idx.y) : xFragAtOutputCoords;
z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));
if(hasNextRow){
z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));
}
}
cache.xy = z.yw;
sum += z * z;
}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
vec4 result = xAtOutputCoords * ${i};
2021-09-11 17:11:38 +02:00
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},mae=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{depthRadius:a,bias:o,alpha:i,beta:u}=r,c=G().getBool("WEBGL_PACK_NORMALIZATION")?new fae(s.shape,a,o,i,u):new hae(s.shape,a,o,i,u);return n.runWebGLProgram(c,[s],s.dtype)},gae={kernelName:di,backendName:"webgl",kernelFunc:mae},bae=class{constructor(e,t,n,r,s){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=r,this.beta=s,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
float result = 0.0;
for (int d = 0; d < ${this.depth}; ++d) {
int depthBegin = int(max(0.0, float(d - ${t})));
int depthEnd = int(min(float(${this.depth}),
float(d + ${t} + 1)));
const int MIN_DEPTH_BEGIN = 0;
const int MAX_DEPTH_END = ${this.depth};
float norm = 0.0;
for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {
if (k < depthBegin){
continue;
}
else if (k >= depthBegin && k < depthEnd) {
norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);
}
else {
break;
}
}
2024-01-17 22:44:24 +01:00
norm = float(${r}) * norm + float(${n});
2021-09-11 17:11:38 +02:00
for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){
if (k < depthBegin){
continue;
}
else if (k >= depthBegin && k < depthEnd){
2024-01-17 22:44:24 +01:00
float dyi = -2.0 * float(${r})
* float(${s})
2023-01-06 19:24:17 +01:00
* getInputImage(b, r, c, k) * getOutputImage(b, r, c, d)
2021-09-11 17:11:38 +02:00
/ norm;
if (k == d) {
2024-01-17 22:44:24 +01:00
dyi += pow(norm, -1.0 * ${s});
2021-09-11 17:11:38 +02:00
}
if (k == coords[3]) {
dyi *= getDy(b, r, c, d);
result += dyi;
}
}
else {
break;
}
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(result);
2021-03-26 15:26:02 +01:00
}
2024-01-17 22:44:24 +01:00
`}},yae=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s,y:a,dy:o}=t,{depthRadius:i,bias:u,alpha:c,beta:l}=r,p=new bae(s.shape,i,u,c,l);return n.runWebGLProgram(p,[s,a,o],s.dtype)},vae={kernelName:Mc,backendName:"webgl",kernelFunc:yae};function xae(e,t,n,r){let s=w.sizeFromShape(t),o=w.sizeFromShape(e.shape)/s,i=pe({inputs:{x:e},attrs:{shape:[o,s]},backend:r}),u=ru(i,e.dtype,"max",r),c=pe({inputs:{x:u},attrs:{shape:n},backend:r});return r.disposeIntermediateTensorInfo(i),r.disposeIntermediateTensorInfo(u),c}function ID(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reductionIndices:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),c=u,l=T.getAxesPermutation(c,i),p=l!=null,d=n.shouldExecuteOnCPU([s]),h=s;if(p){if(d){let v=n.texData.get(h.dataId).values,x=new Array(i);for(let N=0;N<x.length;N++)x[N]=s.shape[l[N]];let k=m0(v,s.shape,s.dtype,l,x);h=n.makeTensorInfo(x,s.dtype);let S=n.texData.get(h.dataId);S.values=k}else h=tg(s,l,n);c=T.getInnerMostAxes(c.length,i)}T.assertAxesAreInnerMostDims("max",c,i);let[f,g]=T.computeOutAndReduceShapes(h.shape,c),m=f;o&&(m=T.expandShapeToKeepDim(f,u));let b;if(d){let v=n.texData.get(h.dataId).values,x=CQ(v,w.sizeFromShape(g),m,s.dtype);b=n.makeTensorInfo(m,s.dtype);let k=n.texData.get(b.dataId);k.values=x}else b=xae(h,g,m,n);return p&&n.disposeIntermediateTensorInfo(h),b}var wae={kernelName:pi,backendName:"webgl",kernelFunc:ID},Iae=b0+`
2021-09-11 17:11:38 +02:00
return max(a, b);
2024-01-17 22:44:24 +01:00
`,kae=`
2021-09-11 17:11:38 +02:00
vec4 result = vec4(max(a, b));
2022-10-09 19:40:33 +02:00
bvec4 isNaNA = isnan(a);
bvec4 isNaNB = isnan(b);
bvec4 isNaN = bvec4(isNaNA.x || isNaNB.x, isNaNA.y || isNaNB.y, isNaNA.z || isNaNB.z, isNaNA.w || isNaNB.w);
2024-01-17 22:44:24 +01:00
`+nu+`
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,Sae=mn({opSnippet:Iae,packedOpSnippet:kae,cpuKernelImpl:TQ}),Cae={kernelName:hi,backendName:"webgl",kernelFunc:Sae};function Tae(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t;wl(s,"maxPool");let{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=r,c=1;w.assert(T.eitherStridesOrDilationsAreOne(o,c),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${o} and dilations '${c}'`);let l=T.computePool2DInfo(s.shape,a,o,c,i,u);if(l.filterWidth===1&&l.filterHeight===1&&w.arraysEqual(l.inShape,l.outShape))return sr({inputs:{x:s},backend:n});let p=new Rd(l,"max",!1);return n.runWebGLProgram(p,[s],s.dtype)}var Nae={kernelName:fi,backendName:"webgl",kernelFunc:Tae};function _ae(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{filterSize:a,strides:o,pad:i,dataFormat:u,dimRoundingMode:c}=r,l=[1,1,1],p=T.computePool3DInfo(s.shape,a,o,l,i,c,u),d=new v0(p,"max",!1);return n.runWebGLProgram(d,[s],s.dtype)}var Eae={kernelName:Lc,backendName:"webgl",kernelFunc:_ae},Aae=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideHeight,n=e.strideWidth,r=e.dilationHeight,s=e.effectiveFilterHeight,a=e.effectiveFilterWidth,o=s-1-e.padInfo.top,i=a-1-e.padInfo.left,u=s*a-1;this.userCode=`
const ivec2 pads = ivec2(${o}, ${i});
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${s};
wR += ${r}) {
2021-09-11 17:11:38 +02:00
float dyR = float(dyRCorner + wR) / ${t}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${a}; wC++) {
2021-09-11 17:11:38 +02:00
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);
2024-01-17 22:44:24 +01:00
int maxPosValue = ${u} - int(getMaxPos(b, idyR, idyC, d));
2021-09-11 17:11:38 +02:00
// Get the current value, check it against the value from the
// position matrix.
2024-01-17 22:44:24 +01:00
int curPosValue = wR * ${a} + wC;
2021-09-11 17:11:38 +02:00
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
dotProd += dyValue * mask;
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},Dae=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideDepth,n=e.strideHeight,r=e.strideWidth,s=e.dilationDepth,a=e.dilationHeight,o=e.dilationWidth,i=e.effectiveFilterDepth,u=e.effectiveFilterHeight,c=e.effectiveFilterWidth,l=i-1-e.padInfo.front,p=u-1-e.padInfo.top,d=c-1-e.padInfo.left,h=i*u*c-1;this.userCode=`
const ivec3 pads = ivec3(${l}, ${p}, ${d});
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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;
2024-01-17 22:44:24 +01:00
for (int wD = 0; wD < ${i};
wD += ${s}) {
2021-09-11 17:11:38 +02:00
float dyD = float(dyDCorner + wD) / ${t}.0;
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
continue;
}
int idyD = int(dyD);
2024-01-17 22:44:24 +01:00
for (int wR = 0; wR < ${u};
wR += ${a}) {
2021-09-11 17:11:38 +02:00
float dyR = float(dyRCorner + wR) / ${n}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2024-01-17 22:44:24 +01:00
for (int wC = 0; wC < ${c};
wC += ${o}) {
float dyC = float(dyCCorner + wC) / ${r}.0;
2021-09-11 17:11:38 +02:00
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));
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// Get the current value, check it against the value from the
// position matrix.
int curPosValue =
2024-01-17 22:44:24 +01:00
wD * ${u} * ${c} +
wR * ${c} + wC;
2021-09-11 17:11:38 +02:00
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
dotProd += dyValue * mask;
}
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function $ae(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a}=t,o=a,{filterSize:i,strides:u,pad:c,dimRoundingMode:l}=r,p=[1,1,1],d=T.computePool3DInfo(o.shape,i,u,p,c,l),h=new v0(d,"max",!0),f=n.runWebGLProgram(h,[o],o.dtype),g=new Dae(d),m=n.runWebGLProgram(g,[s,f],o.dtype);return n.disposeIntermediateTensorInfo(f),m}var Fae={kernelName:qd,backendName:"webgl",kernelFunc:$ae};function Rae(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a,output:o}=t,i=a;wl([a,o],"maxPoolGrad");let{filterSize:u,strides:c,pad:l,dimRoundingMode:p}=r,d=T.computePool2DInfo(i.shape,u,c,1,l,p),h=!0,f=new Rd(d,"max",h),g=n.runWebGLProgram(f,[i],i.dtype),m=new Aae(d),b=n.runWebGLProgram(m,[s,g],i.dtype);return n.disposeIntermediateTensorInfo(g),b}var Pae={kernelName:jd,backendName:"webgl",kernelFunc:Rae};function Oae(e,t,n,r){let s=new Rd(n,"max",!1),a=r.runWebGLProgram(s,[e],"float32");s=new Rd(n,"max",!0,!0,t);let o=r.runWebGLProgram(s,[e],"float32");return[a,o]}var Mae={kernelName:Kd,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:r}=e,{filterSize:s,strides:a,pad:o,includeBatchInIndex:i}=t,u=n;w.assert(r.shape.length===4,()=>`Error in maxPool: input must be rank 4 but got rank ${r.shape.length}.`);let c=[1,1];w.assert(T.eitherStridesOrDilationsAreOne(a,c),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${a} and dilations '${c}'`);let l=T.computePool2DInfo(r.shape,s,a,c,o),[p,d]=Oae(r,i,l,u);return[p,d]}};function Lae(e,t,n,r){let s=w.sizeFromShape(t),o=w.sizeFromShape(e.shape)/s,i=pe({inputs:{x:e},attrs:{shape:[o,s]},backend:r}),u=ru(i,"float32","mean",r),c=pe({inputs:{x:u},attrs:{shape:n},backend:r});return r.disposeIntermediateTensorInfo(i),r.disposeIntermediateTensorInfo(u),c}var Bae={kernelName:mi,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:r}=e,{keepDims:s,axis:a}=t,o=n,i=r.shape.length,u=w.parseAxisParam(a,r.shape),c=u,l=T.getAxesPermutation(c,i),p=l!=null,d=o.shouldExecuteOnCPU([r]),h=[],f=r;if(p){if(d){let x=o.texData.get(f.dataId).values,k=new Array(i);for(let E=0;E<k.length;E++)k[E]=r.shape[l[E]];let S=m0(x,r.shape,r.dtype,l,k);f=o.makeTensorInfo(k,r.dtype);let N=o.texData.get(f.dataId);N.values=S}else f=tg(r,l,o);h.push(f),c=T.getInnerMostAxes(c.length,i)}T.assertAxesAreInnerMostDims("sum",c,i);let[g,m]=T.computeOutAndReduceShapes(f.shape,c),b=g;s&&(b=T.expandShapeToKeepDim(g,u));let y=Lae(f,m,b,o);for(let v of h)o.disposeIntermediateTensorInfo(v);return y}};function zae(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),c=u,l=T.getAxesPermutation(c,i),p=s;l!=null&&(p=Tn({inputs:{x:s},backend:n,attrs:{perm:l}}),c=T.getInnerMostAxes(c.length,s.shape.length)),T.assertAxesAreInnerMostDims("min",c,i);let[d,h]=T.computeOutAndReduceShapes(p.shape,c),f=w.sizeFromShape(h),g=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,f]}}),m=ru(g,g.dtype,"min",n),b;if(o){let y=T.expandShapeToKeepDim(d,u);b=pe({inputs:{x:m},backend:n,attrs:{shape:y}})}else b=pe({inputs:{x:m},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(g),n.disposeIntermediateTensorInfo(m),l!=null&&n.disposeIntermediateTensorInfo(p),b}var Wae={kernelName:gi,backendName:"webgl",kernelFunc:zae},Vae=b0+`
2021-09-11 17:11:38 +02:00
return min(a, b);
2024-01-17 22:44:24 +01:00
`,Uae=`
2021-09-11 17:11:38 +02:00
vec4 result = vec4(min(a, b));
2022-10-09 19:40:33 +02:00
bvec4 isNaNA = isnan(a);
bvec4 isNaNB = isnan(b);
bvec4 isNaN = bvec4(isNaNA.x || isNaNB.x, isNaNA.y || isNaNB.y, isNaNA.z || isNaNB.z, isNaNA.w || isNaNB.w);
2024-01-17 22:44:24 +01:00
`+nu+`
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,Gae=mn({opSnippet:Vae,packedOpSnippet:Uae,cpuKernelImpl:NQ}),Hae={kernelName:bi,backendName:"webgl",kernelFunc:Gae},jae=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=t.map((c,l)=>c[0]+e[l]+c[1]);let r=e.length,s=ht(r),a=t.map(c=>c[0]).join(","),o=t.map((c,l)=>c[0]+e[l]).join(","),i=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r),u=n==="reflect"?0:1;if(r===1){this.userCode=`
int start = ${a};
int end = ${o};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
int outC = getOutputCoords();
if (outC < start) {
2024-01-17 22:44:24 +01:00
outC = start * 2 - outC - ${u};
2021-09-11 17:11:38 +02:00
} else if(outC >= end) {
2024-01-17 22:44:24 +01:00
outC = (end - 1) * 2 - outC + ${u};
2021-09-11 17:11:38 +02:00
}
setOutput(getX(outC - start));
}
`;return}this.userCode=`
2024-01-17 22:44:24 +01:00
${s} start = ${s}(${a});
${s} end = ${s}(${o});
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${s} outC = getOutputCoords();
for (int i = 0; i < ${r}; i++) {
2021-09-11 17:11:38 +02:00
if (outC[i] < start[i]) {
2024-01-17 22:44:24 +01:00
outC[i] = start[i] * 2 - outC[i] - ${u};
2021-09-11 17:11:38 +02:00
} else if(outC[i] >= end[i]) {
2024-01-17 22:44:24 +01:00
outC[i] = (end[i] - 1) * 2 - outC[i] + ${u};
2021-09-11 17:11:38 +02:00
}
}
2024-01-17 22:44:24 +01:00
${s} coords = outC - start;
setOutput(getX(${i}));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}},qae=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 r=e.length,s=ht(r),a=t.map(h=>h[0]).join(","),o=t.map((h,f)=>h[0]+e[f]).join(","),i=Cn("rc",r),u=Cn("source",r),c=`${i[r-1]} < ${this.outputShape[r-1]}`,l=r===1?"source":`vec2(${u.slice(-2).join()})`,p=n==="reflect"?0:1,d="";if(r===1){let h=`
${s} source = rc;
2021-09-11 17:11:38 +02:00
if (source < start) {
2024-01-17 22:44:24 +01:00
source = start * 2 - source - ${p};
2021-09-11 17:11:38 +02:00
} else if (source >= end) {
2024-01-17 22:44:24 +01:00
source = (end - 1) * 2 - source + ${p};
2021-09-11 17:11:38 +02:00
}
source -= start;
2024-01-17 22:44:24 +01:00
`;d=`
${s} rc = outputLoc;
2021-09-11 17:11:38 +02:00
${h}
2024-01-17 22:44:24 +01:00
result[0] = getChannel(getX(${u.join()}), ${l});
${i[r-1]} += 1;
if(${c}) {
2021-09-11 17:11:38 +02:00
${h}
2024-01-17 22:44:24 +01:00
result[1] = getChannel(getX(${u.join()}), ${l});
2021-09-11 17:11:38 +02:00
}
`}else{let h=`
2024-01-17 22:44:24 +01:00
${s} source = rc;
${s} lt = ${s}(lessThan(source, start));
${s} gte = ${s}(greaterThanEqual(source, end));
${s} orig = 1 - (lt + gte);
2021-09-11 17:11:38 +02:00
source = orig * source +
2024-01-17 22:44:24 +01:00
lt * (start * 2 - source - ${p}) +
gte * ((end - 1) * 2 - source + ${p});
2021-09-11 17:11:38 +02:00
source -= start;
2024-01-17 22:44:24 +01:00
`;d=`
${s} rc = outputLoc;
2021-09-11 17:11:38 +02:00
${h}
2024-01-17 22:44:24 +01:00
result[0] = getChannel(getX(${u.join()}), ${l});
${i[r-1]} += 1;
if(${c}) {
2021-09-11 17:11:38 +02:00
${h}
2024-01-17 22:44:24 +01:00
result[1] = getChannel(getX(${u.join()}), ${l});
2021-09-11 17:11:38 +02:00
}
rc = outputLoc;
2024-01-17 22:44:24 +01:00
${i[r-2]} += 1;
if(${i[r-2]} < ${this.outputShape[r-2]}) {
2021-09-11 17:11:38 +02:00
${h}
2024-01-17 22:44:24 +01:00
result[2] = getChannel(getX(${u.join()}), ${l});
${i[r-1]} += 1;
if(${c}) {
2021-09-11 17:11:38 +02:00
${h}
2024-01-17 22:44:24 +01:00
result[3] = getChannel(getX(${u.join()}), ${l});
2021-09-11 17:11:38 +02:00
}
}
`}this.userCode=`
2024-01-17 22:44:24 +01:00
const ${s} start = ${s}(${a});
const ${s} end = ${s}(${o});
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${s} outputLoc = getOutputCoords();
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2024-01-17 22:44:24 +01:00
${d}
2021-09-11 17:11:38 +02:00
setOutput(result);
2020-12-15 14:15:43 +01:00
}
2024-01-17 22:44:24 +01:00
`}},Kae=({inputs:e,backend:t,attrs:n})=>{let{x:r}=e,{paddings:s,mode:a}=n,o=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new qae(r.shape,s,a):new jae(r.shape,s,a);return t.runWebGLProgram(o,[r],r.dtype)},Xae={kernelName:yi,backendName:"webgl",kernelFunc:Kae},Yae=`if (b == 0.0) return NAN;
return mod(a, b);`,Zae=`
2021-09-11 17:11:38 +02:00
vec4 result = mod(a, b);
2022-10-09 19:40:33 +02:00
bvec4 isNaN = equal(b, vec4(0.0));
2024-01-17 22:44:24 +01:00
`+nu+`
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,Jae=mn({opSnippet:Yae,packedOpSnippet:Zae}),Qae={kernelName:vi,backendName:"webgl",kernelFunc:Jae},eoe=class{constructor(e,t,n){this.variableNames=["probs"],this.customUniforms=[{name:"seed",type:"float"}],this.outputShape=[e,n],this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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;
}
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// If no other event happened, last event happened.
setOutput(float(${t-1}));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},toe=`
2021-09-11 17:11:38 +02:00
if (a == b) {
return 1.0;
};
2024-01-17 22:44:24 +01:00
return a / b;`,noe=`
2021-09-11 17:11:38 +02:00
// vec4 one = vec4(equal(a, b));
// return one + (vec4(1.0) - one) * a / b;
vec4 result = a / b;
if(a.x == b.x) {
result.x = 1.;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if(a.y == b.y) {
result.y = 1.;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if(a.z == b.z) {
result.z = 1.;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if(a.w == b.w) {
result.w = 1.;
2021-04-01 19:39:54 +02:00
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,kD=mn({opSnippet:toe,packedOpSnippet:noe,checkOutOfBounds:!0}),roe={kernelName:Xo,backendName:"webgl",kernelFunc:kD},H1="return a - b;",SD=mn({opSnippet:H1,packedOpSnippet:H1,supportsComplex:!0,cpuKernelImpl:KQ}),soe={kernelName:Gi,backendName:"webgl",kernelFunc:SD};function CD(e){let{inputs:t,backend:n,attrs:r}=e,{logits:s}=t,{dim:a}=r,o=w.parseAxisParam([a],s.shape),i=ID({inputs:{x:s},backend:n,attrs:{reductionIndices:o,keepDims:!1}}),u=T.expandShapeToKeepDim(i.shape,o),c=pe({inputs:{x:i},backend:n,attrs:{shape:u}}),l=SD({inputs:{a:s,b:c},backend:n}),p=vD({inputs:{x:l},backend:n}),d=ng({inputs:{x:p},backend:n,attrs:{axis:o,keepDims:!1}}),h=pe({inputs:{x:d},backend:n,attrs:{shape:u}}),f=kD({inputs:{a:p,b:h},backend:n});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(l),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),f}var aoe={kernelName:Vi,backendName:"webgl",kernelFunc:CD};function ooe(e){let{inputs:t,backend:n,attrs:r}=e,{logits:s}=t,{numSamples:a,seed:o,normalized:i}=r,u=i?s:CD({inputs:{logits:s},backend:n,attrs:{dim:s.shape.length-1}}),c=u.shape[0],l=u.shape[1],p=new eoe(c,l,a),d=[[o]],h=n.runWebGLProgram(p,[u],"int32",d);return i||n.disposeIntermediateTensorInfo(u),h}var ioe={kernelName:Bc,backendName:"webgl",kernelFunc:ooe},uoe=Or+`
2022-01-14 16:04:13 +01:00
return -x;
2024-01-17 22:44:24 +01:00
`,coe=`
2022-01-14 16:04:13 +01:00
vec4 result = -x;
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2024-01-17 22:44:24 +01:00
`;function loe(e){let{inputs:t,backend:n}=e,{x:r}=t;if(n.shouldExecuteOnCPU([r])){let a=n.texData.get(r.dataId),[o,i]=EQ(a.values,r.shape,r.dtype);return n.makeTensorInfo(i,r.dtype,o)}let s;return G().getBool("WEBGL_PACK_UNARY_OPERATIONS")?s=new oa(r.shape,coe):s=new is(r.shape,uoe),n.runWebGLProgram(s,[r],r.dtype)}var doe={kernelName:zc,backendName:"webgl",kernelFunc:loe},poe=gs.nonMaxSuppressionV3Impl;function hoe(e){T.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u}=r,c=n.readSync(s.dataId),l=n.readSync(a.dataId),{selectedIndices:p}=poe(c,l,o,i,u);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var foe={kernelName:Vc,backendName:"webgl",kernelFunc:hoe},moe=gs.nonMaxSuppressionV4Impl;function goe(e){T.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u,padToMaxOutputSize:c}=r,l=n.readSync(s.dataId),p=n.readSync(a.dataId),{selectedIndices:d,validOutputs:h}=moe(l,p,o,i,u,c);return[n.makeTensorInfo([d.length],"int32",new Int32Array(d)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var boe={kernelName:Uc,backendName:"webgl",kernelFunc:goe},yoe=gs.nonMaxSuppressionV5Impl;function voe(e){T.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u,softNmsSigma:c}=r,l=n.readSync(s.dataId),p=n.readSync(a.dataId),d=o,h=i,f=u,g=c,{selectedIndices:m,selectedScores:b}=yoe(l,p,d,h,f,g);return[n.makeTensorInfo([m.length],"int32",new Int32Array(m)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var xoe={kernelName:Gc,backendName:"webgl",kernelFunc:voe},woe=class{constructor(e,t,n,r){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int index = round(getIndices(coords.x));
2024-01-17 22:44:24 +01:00
setOutput(mix(float(${r}), float(${n}),
2021-09-11 17:11:38 +02:00
float(index == coords.y)));
2021-03-26 15:26:02 +01:00
}
2024-01-17 22:44:24 +01:00
`}},Ioe=e=>{let{inputs:t,backend:n,attrs:r}=e,{indices:s}=t,{dtype:a,depth:o,onValue:i,offValue:u}=r,c=w.sizeFromShape(s.shape),l=new woe(c,o,i,u),p=pe({inputs:{x:s},backend:n,attrs:{shape:[c]}}),d=n.runWebGLProgram(l,[p],a);n.disposeIntermediateTensorInfo(p);let h=[...s.shape,o],f=pe({inputs:{x:d},backend:n,attrs:{shape:h}});return n.disposeIntermediateTensorInfo(d),f},koe={kernelName:wi,backendName:"webgl",kernelFunc:Ioe};function Df(e){let{inputs:t,backend:n}=e,{x:r}=t;if(r.dtype==="complex64"){let s=Mp({inputs:{input:r},backend:n}),a=Df({inputs:{x:s},backend:n}),o=rg({inputs:{input:r},backend:n}),i=Df({inputs:{x:o},backend:n}),u=Oa({inputs:{real:a,imag:i},backend:n});return n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(i),u}else return Lp({attrs:{shape:r.shape,dtype:r.dtype,value:r.dtype==="string"?"":0},backend:n})}var Soe={kernelName:cl,backendName:"webgl",kernelFunc:Df};function TD(e){let{inputs:t,backend:n}=e,{x:r}=t;if(r.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(r.dtype==="complex64"){let s=Mp({inputs:{input:r},backend:n}),a=TD({inputs:{x:s},backend:n}),o=rg({inputs:{input:r},backend:n}),i=Df({inputs:{x:o},backend:n}),u=Oa({inputs:{real:a,imag:i},backend:n});return n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(i),u}else return Lp({attrs:{shape:r.shape,dtype:r.dtype,value:1},backend:n})}var Coe={kernelName:Hc,backendName:"webgl",kernelFunc:TD};function Toe(e){let{inputs:t,backend:n,attrs:r}=e,{axis:s}=r;if(t.length===1)return kx({inputs:{input:t[0]},backend:n,attrs:{dim:s}});let a=t[0].shape,o=t[0].dtype;t.forEach(l=>{w.assertShapesMatch(a,l.shape,"All tensors passed to stack must have matching shapes"),w.assert(o===l.dtype,()=>"All tensors passed to stack must have matching dtypes")});let i=[],u=t.map(l=>{let p=kx({inputs:{input:l},backend:n,attrs:{dim:s}});return i.push(p),p}),c=dD({inputs:u,backend:n,attrs:{axis:s}});return i.forEach(l=>n.disposeIntermediateTensorInfo(l)),c}var Noe={kernelName:jc,backendName:"webgl",kernelFunc:Toe},_oe=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((u,c)=>u[0]+e[c]+u[1]);let r=e.length,s=ht(r),a=t.map(u=>u[0]).join(","),o=t.map((u,c)=>u[0]+e[c]).join(","),i=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r);if(r===1){this.userCode=`
int start = ${a};
int end = ${o};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
int outC = getOutputCoords();
if (outC < start || outC >= end) {
setOutput(value);
} else {
setOutput(getX(outC - start));
}
}
`;return}this.userCode=`
2024-01-17 22:44:24 +01:00
${s} start = ${s}(${a});
${s} end = ${s}(${o});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${s} outC = getOutputCoords();
2021-09-11 17:11:38 +02:00
if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {
setOutput(value);
} else {
2024-01-17 22:44:24 +01:00
${s} coords = outC - start;
setOutput(getX(${i}));
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},Eoe=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,g)=>f[0]+e[g]+f[1]);let r=e.length,s=ht(r),a=t.map(f=>f[0]).join(","),o=t.map((f,g)=>f[0]+e[g]).join(","),i=Cn("rc",r),u=Cn("source",r),c=`${i[r-1]} < ${this.outputShape[r-1]}`,l=r===1?"source":`vec2(${u.slice(-2).join()})`,p=[`${s} rc = outputLoc;`,`${i[r-1]} += 1;
if(${c}) {
`,r===1?"":`}
2021-09-11 17:11:38 +02:00
rc = outputLoc;
2024-01-17 22:44:24 +01:00
${i[r-2]} += 1;
if(${i[r-2]} < ${this.outputShape[r-2]}) {`,r===1?"":` ${i[r-1]} += 1;
if(${c}) {`],d=r===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="";for(let f=0,g=r===1?2:4;f<g;f++)h+=`
${p[f]}
if (${d}) {
result[${f}] = float(value);
2021-09-11 17:11:38 +02:00
} else {
2024-01-17 22:44:24 +01:00
${s} source = rc - start;
result[${f}] = getChannel(getX(${u.join()}), ${l});
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`;h+=r===1?"} ":"}}",this.userCode=`
const ${s} start = ${s}(${a});
const ${s} end = ${s}(${o});
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${s} outputLoc = getOutputCoords();
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
${h}
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},ND=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{paddings:a,constantValue:o}=r;if(w.sizeFromShape(s.shape)===0){let c=a.map((l,p)=>l[0]+s.shape[p]+l[1]);return Lp({backend:n,attrs:{shape:c,value:o,dtype:s.dtype}})}let i=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Eoe(s.shape,a,o):new _oe(s.shape,a,o),u=[[o]];return n.runWebGLProgram(i,[s],s.dtype,u)},Aoe={kernelName:Ii,backendName:"webgl",kernelFunc:ND},Doe=`
2021-09-11 17:11:38 +02:00
if(a < 0.0 && floor(b) < b){
return NAN;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if (b == 0.0) {
return 1.0;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
return (round(mod(b, 2.0)) != 1) ?
pow(abs(a), b) : sign(a) * pow(abs(a), b);
2024-01-17 22:44:24 +01:00
`,$oe=`
2021-09-11 17:11:38 +02:00
// isModRound1 has 1 for components with round(mod(b, 2.0)) == 1, 0 otherwise.
vec4 isModRound1 = vec4(equal(round(mod(b, 2.0)), ivec4(1)));
vec4 multiplier = sign(a) * isModRound1 + (vec4(1.0) - isModRound1);
vec4 result = multiplier * pow(abs(a), b);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// 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;
2020-10-11 18:41:17 +02:00
2022-10-09 19:40:33 +02:00
bvec4 isNaN1 = lessThan(a, vec4(0.0));
bvec4 isNaN2 = lessThan(floor(b), b);
bvec4 isNaN = bvec4(isNaN1.x && isNaN2.x, isNaN1.y && isNaN2.y, isNaN1.z && isNaN2.z, isNaN1.w && isNaN2.w);
2024-01-17 22:44:24 +01:00
`+nu+`
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,Foe=mn({opSnippet:Doe,packedOpSnippet:$oe}),Roe={kernelName:ki,backendName:"webgl",kernelFunc:Foe};function Poe(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=[],c=w.parseAxisParam(a,s.shape),l=c,p=T.getAxesPermutation(l,i),d=s;p!=null&&(d=Tn({inputs:{x:s},backend:n,attrs:{perm:p}}),l=T.getInnerMostAxes(l.length,i),u.push(d)),T.assertAxesAreInnerMostDims("prod",l,i);let h;if(n.shouldExecuteOnCPU([d])){let f=n.texData.get(d.dataId).values,{outVals:g,outShape:m,outDtype:b}=DQ(d.shape,d.dtype,f,l);h=n.makeTensorInfo(m,b,g)}else{let[f,g]=T.computeOutAndReduceShapes(d.shape,l),m=w.sizeFromShape(g),b=pe({inputs:{x:d},backend:n,attrs:{shape:[-1,m]}}),y=Zf(s.dtype),v=ru(b,y,"prod",n);h=pe({inputs:{x:v},backend:n,attrs:{shape:f}}),u.push(b),u.push(v)}if(o){u.push(h);let f=T.expandShapeToKeepDim(h.shape,c);h=pe({inputs:{x:h},backend:n,attrs:{shape:f}})}return u.forEach(f=>n.disposeIntermediateTensorInfo(f)),h}var Ooe={kernelName:Ci,backendName:"webgl",kernelFunc:Poe};function Moe(e){let{inputs:t,backend:n,attrs:r}=e,{paramsNestedSplits:s,paramsDenseValues:a,indices:o}=t,{outputRaggedRank:i}=r,u=s.map(b=>n.readSync(b.dataId)),c=s.map(b=>b.shape),l=n.readSync(a.dataId),p=n.readSync(o.dataId),[d,h,f]=$Q(u,c,l,a.shape,a.dtype,p,o.shape,i),g=d.map(b=>n.makeTensorInfo([b.length],"int32",b)),m=n.makeTensorInfo(f,a.dtype,h);return g.concat([m])}var Loe={kernelName:jf,backendName:"webgl",kernelFunc:Moe};function Boe(e){let{inputs:t,backend:n}=e,{starts:r,limits:s,deltas:a}=t,o=n.readSync(r.dataId),i=n.readSync(s.dataId),u=n.readSync(a.dataId),[c,l]=FQ(o,r.shape,r.dtype,i,s.shape,u,a.shape),p=n.makeTensorInfo([c.length],"int32",c),d=n.makeTensorInfo([l.length],r.dtype,l);return[p,d]}var zoe={kernelName:qf,backendName:"webgl",kernelFunc:Boe};function Woe(e){let{inputs:t,backend:n,attrs:r}=e,{shape:s,values:a,defaultValue:o,rowPartitionTensors:i}=t,{rowPartitionTypes:u}=r,c=n.readSync(s.dataId),l=n.readSync(a.dataId),p=n.readSync(o.dataId),d=i.map(m=>n.readSync(m.dataId)),h=i.map(m=>m.shape),[f,g]=RQ(c,s.shape,l,a.shape,a.dtype,p,o.shape,d,h,u);return n.makeTensorInfo(f,a.dtype,g)}var Voe={kernelName:Kf,backendName:"webgl",kernelFunc:Woe},_D=e=>{let{backend:t,attrs:n}=e,{start:r,stop:s,step:a,dtype:o}=n,i=PQ(r,s,a,o);return t.makeTensorInfo([i.length],o,i)},Uoe={kernelName:Xd,backendName:"webgl",kernelFunc:_D},Goe="return 1.0 / x;",Hoe=Ze({opSnippet:Goe}),joe={kernelName:Ti,backendName:"webgl",kernelFunc:Hoe},qoe=Or+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : x;
2024-01-17 22:44:24 +01:00
`,Koe=`
2021-09-11 17:11:38 +02:00
vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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;
2024-01-17 22:44:24 +01:00
`,Xoe=Ze({opSnippet:qoe,packedOpSnippet:Koe}),Yoe={kernelName:Ni,backendName:"webgl",kernelFunc:Xoe},Zoe=Or+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2024-01-17 22:44:24 +01:00
`,Joe=`
2021-09-11 17:11:38 +02:00
vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2024-01-17 22:44:24 +01:00
`,Qoe=Ze({opSnippet:Zoe,packedOpSnippet:Joe}),eie={kernelName:Ai,backendName:"webgl",kernelFunc:Qoe},tie=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let c=[r&&t>1?o-1:o,r&&n>1?i-1:i],l=[r&&t>1?t-1:t,r&&n>1?n-1:n],p;s?p="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":p="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2(
2024-01-17 22:44:24 +01:00
${c[0]/l[0]},
${c[1]/l[1]});
const vec2 inputShapeRC = vec2(${o}.0, ${i}.0);
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
2024-01-17 22:44:24 +01:00
vec2 sourceFracIndexRC = ${p};
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
// Compute the four integer indices.
ivec2 sourceFloorRC = ivec2(max(sourceFracIndexRC, vec2(0.0)));
ivec2 sourceCeilRC = ivec2(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
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);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
float top = topLeft + (topRight - topLeft) * fracRC.y;
float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
float newValue = top + (bottom - top) * fracRC.x;
setOutput(newValue);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},nie=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let c=[r&&t>1?o-1:o,r&&n>1?i-1:i],l=[r&&t>1?t-1:t,r&&n>1?n-1:n],p;s?p="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":p="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3(
2024-01-17 22:44:24 +01:00
${c[0]/l[0]},
${c[1]/l[1]},
${c[1]/l[1]});
const vec3 inputShapeRC = vec3(${o}.0, ${i}.0,
${i}.0);
2021-09-11 17:11:38 +02:00
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +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);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
2024-01-17 22:44:24 +01:00
vec3 sourceFracIndexRC = ${p};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Compute the four integer indices.
ivec3 sourceFloorRC = ivec3(max(sourceFracIndexRC, vec3(0.0)));
ivec3 sourceCeilRC = ivec3(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Should we calculate next column and row elements in 2x2 packed cell.
2024-01-17 22:44:24 +01:00
bool hasNextCol = d < ${u-1};
2021-09-11 17:11:38 +02:00
bool hasNextRow = coords.z < ${n-1};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// 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);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec3 fracRC = sourceFracIndexRC - vec3(sourceFloorRC);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 top = mix(topLeft, topRight, fracRC.yyzz);
vec4 bottom = mix(bottomLeft, bottomRight, fracRC.yyzz);
vec4 newValue = mix(top, bottom, fracRC.x);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
setOutput(newValue);
}
2024-01-17 22:44:24 +01:00
`}};function rie(e){let{inputs:t,backend:n,attrs:r}=e,{images:s}=t,{alignCorners:a,halfPixelCenters:o,size:i}=r,[u,c]=i,l=G().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new nie(s.shape,u,c,a,o):new tie(s.shape,u,c,a,o);return n.runWebGLProgram(l,[s],"float32")}var sie={kernelName:Ei,backendName:"webgl",kernelFunc:rie},aie=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,r,s]=t,[,a,o]=e,i=[n&&a>1?r-1:r,n&&o>1?s-1:s],u=[n&&a>1?a-1:a,n&&o>1?o-1:o],c=i[0]/u[0],l=i[1]/u[1],p=1/c,d=1/l,h=Math.ceil(p)*2+2,f=Math.ceil(d)*2+2;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
float accumulator = 0.0;
2020-10-11 18:41:17 +02:00
2024-01-17 22:44:24 +01:00
const float heightScale = float(${c});
const float widthScale = float(${l});
2020-10-11 18:41:17 +02:00
2024-01-17 22:44:24 +01:00
const float invHeightScale = float(${p});
const float invWidthScale = float(${d});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
const int winHeight = int(${h});
2024-01-17 22:44:24 +01:00
const int winWidth = int(${f});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(startRLerp - float(winHeight / 2));
2021-09-11 17:11:38 +02:00
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(startCLerp - float(winWidth / 2));
2021-09-11 17:11:38 +02:00
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
2024-01-17 22:44:24 +01:00
if (dyR < 0 || dyR >= ${a}) {
2021-09-11 17:11:38 +02:00
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
2024-01-17 22:44:24 +01:00
if (dyC < 0 || dyC >= ${o}) {
2021-09-11 17:11:38 +02:00
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float dxR = float(dyR) * heightScale;
int topDxRIndex = int(floor(dxR));
2024-01-17 22:44:24 +01:00
int bottomDxRIndex = int(min(ceil(dxR), ${r-1}.0));
2021-09-11 17:11:38 +02:00
float dxRLerp = dxR - float(topDxRIndex);
float inverseDxRLerp = 1.0 - dxRLerp;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float dxC = float(dyC) * widthScale;
int leftDxCIndex = int(floor(dxC));
2024-01-17 22:44:24 +01:00
int rightDxCIndex = int(min(ceil(dxC), ${s-1}.0));
2021-09-11 17:11:38 +02:00
float dxCLerp = dxC - float(leftDxCIndex);
float inverseDxCLerp = 1.0 - dxCLerp;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
if (r == topDxRIndex && c == leftDxCIndex) {
// topLeft
accumulator +=
getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;
}
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
if (r == topDxRIndex && c == rightDxCIndex) {
// topRight
accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
if (r == bottomDxRIndex && c == leftDxCIndex) {
// bottomLeft
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
if (r == bottomDxRIndex && c == rightDxCIndex) {
// bottomRight
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;
}
}
}
// End loop over dy
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
setOutput(accumulator);
2021-03-31 13:01:22 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function oie(e){let{inputs:t,backend:n,attrs:r}=e,{images:s,dy:a}=t,{alignCorners:o}=r,i=new aie(a.shape,s.shape,o);return n.runWebGLProgram(i,[a],a.dtype)}var iie={kernelName:Xc,backendName:"webgl",kernelFunc:oie},uie=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let c=[r&&t>1?o-1:o,r&&n>1?i-1:i],l=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=r?"0.5":"0.0",d;s?d="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":d="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2(
2024-01-17 22:44:24 +01:00
${c[0]/l[0]},
${c[1]/l[1]});
const vec2 inputShapeRC = vec2(${o}.0, ${i}.0);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
2024-01-17 22:44:24 +01:00
vec2 sourceFracIndexRC = ${d};
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestRC = ivec2(
2024-01-17 22:44:24 +01:00
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${p})));
2021-09-11 17:11:38 +02:00
float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);
setOutput(newValue);
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}},cie=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let c=[r&&t>1?o-1:o,r&&n>1?i-1:i],l=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=r?"0.5":"0.0",d;s?d="max((vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC, vec3(0.0))":d="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3(
2024-01-17 22:44:24 +01:00
${c[0]/l[0]},
${c[1]/l[1]},
${c[1]/l[1]});
const vec3 inputShapeRC = vec3(${o}.0, ${i}.0,
${i}.0);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +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);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
2024-01-17 22:44:24 +01:00
vec3 sourceFracIndexRC = ${d};
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Compute the coordinators of nearest neighbor point.
ivec3 sourceNearestRC = ivec3(
2024-01-17 22:44:24 +01:00
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${p})));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Should we calculate next column and row elements in 2x2 packed cell.
2024-01-17 22:44:24 +01:00
bool hasNextCol = d < ${u-1};
2021-09-11 17:11:38 +02:00
bool hasNextRow = coords.z < ${n-1};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
setOutput(newValue);
}
2024-01-17 22:44:24 +01:00
`}};function lie(e){let{inputs:t,backend:n,attrs:r}=e,{images:s}=t,{alignCorners:a,halfPixelCenters:o,size:i}=r,[u,c]=i,l=G().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new cie(s.shape,u,c,a,o):new uie(s.shape,u,c,a,o);return n.runWebGLProgram(l,[s],s.dtype)}var die={kernelName:_i,backendName:"webgl",kernelFunc:lie},pie=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,r,s]=t,[,a,o]=e,i=[n&&a>1?r-1:r,n&&o>1?s-1:s],u=[n&&a>1?a-1:a,n&&o>1?o-1:o],c=i[0]/u[0],l=i[1]/u[1],p=1/c,d=1/l,h=Math.ceil(p)*2+2,f=Math.ceil(d)*2+2;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float accumulator = 0.0;
2020-12-15 14:15:43 +01:00
2024-01-17 22:44:24 +01:00
const float heightScale = float(${c});
const float widthScale = float(${l});
2020-12-15 14:15:43 +01:00
2024-01-17 22:44:24 +01:00
const float invHeightScale = float(${p});
const float invWidthScale = float(${d});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
const int winHeight = int(${h});
2024-01-17 22:44:24 +01:00
const int winWidth = int(${f});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(floor(startRLerp - float(winHeight / 2)));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(floor(startCLerp - float(winWidth / 2)));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
2024-01-17 22:44:24 +01:00
if (dyR < 0 || dyR >= ${a}) {
2021-09-11 17:11:38 +02:00
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
2024-01-17 22:44:24 +01:00
if (dyC < 0 || dyC >= ${o}) {
2021-09-11 17:11:38 +02:00
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float sourceFracRow =
2024-01-17 22:44:24 +01:00
float(${i[0]}) *
(float(dyR) / float(${u[0]}));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
float sourceFracCol =
2024-01-17 22:44:24 +01:00
float(${i[1]}) *
(float(dyC) / float(${u[1]}));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
int sourceNearestRow = int(min(
2024-01-17 22:44:24 +01:00
float(int(${r}) - 1),
2021-09-11 17:11:38 +02:00
${n} ? float(round(sourceFracRow)) :
float(floor(sourceFracRow))));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
int sourceNearestCol = int(min(
2024-01-17 22:44:24 +01:00
float(int(${s}) - 1),
2021-09-11 17:11:38 +02:00
${n} ? float(round(sourceFracCol)) :
float(floor(sourceFracCol))));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (r == sourceNearestRow && c == sourceNearestCol) {
accumulator += getDy(b, dyR, dyC, d);
}
}
}
// End loop over dy
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
setOutput(accumulator);
}
2024-01-17 22:44:24 +01:00
`}};function hie(e){let{inputs:t,backend:n,attrs:r}=e,{images:s,dy:a}=t,{alignCorners:o}=r,i=new pie(a.shape,s.shape,o);return n.runWebGLProgram(i,[a],a.dtype)}var fie={kernelName:Kc,backendName:"webgl",kernelFunc:hie},mie=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=`
2021-09-11 17:11:38 +02:00
void main() {
int coord = getOutputCoords();
setOutput(getX(${e[0]} - coord - 1));
}
2024-01-17 22:44:24 +01:00
`;return}let r=o=>t.indexOf(o)!==-1&&e[o]!==1?`${e[o]} - coords[${o}] - 1`:`coords[${o}]`,s=e.map((o,i)=>r(i)).join(","),a=ht(n);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${a} coords = getOutputCoords();
setOutput(getX(${s}));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}},gie=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 r=Cn("rc",n),s=`${r[n-1]} + 1 < ${this.outputShape[n-1]}`,a=`${r[n-2]} + 1 < ${this.outputShape[n-2]}`,o=ht(n);n===1?this.userCode=`
2021-09-11 17:11:38 +02:00
void main(){
int rc = getOutputCoords();
vec4 result = vec4(0.);
result.r = getChannel(getX(${e[0]} - rc - 1),
${e[0]} - rc - 1);
2024-01-17 22:44:24 +01:00
if(${s}){
2021-09-11 17:11:38 +02:00
result.g = getChannel(getX(${e[0]} - (rc + 1) - 1),
${e[0]} - (rc + 1) - 1);
}
setOutput(result);
}
`:this.userCode=`
void main() {
2024-01-17 22:44:24 +01:00
${o} rc = getOutputCoords();
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2024-01-17 22:44:24 +01:00
result.r = ${i(r.slice())};
if(${s}){
result.g = ${u(r.slice())};
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
if(${a}) {
result.b = ${c(r.slice())};
if(${s}) {
result.a = ${l(r.slice())};
2021-09-11 17:11:38 +02:00
}
}
setOutput(result);
}
2024-01-17 22:44:24 +01:00
`;function i(h){return p(h)}function u(h){return h[n-1]="("+h[n-1]+" + 1)",p(h)}function c(h){return h[n-2]="("+h[n-2]+" + 1)",p(h)}function l(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)),g=f.join(","),m=f.slice(-2).join(",");return`getChannel(getX(${g}), vec2(${m}))`}function d(h,f){return t.indexOf(h)!==-1&&e[h]!==1?`${e[h]} - ${f[h]} - 1`:`${f[h]}`}}};function bie(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{dims:a}=r,o=s.shape.length,i=w.parseAxisParam(a,s.shape);if(o===0)return sr({inputs:{x:s},backend:n});let u=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new gie(s.shape,i):new mie(s.shape,i);return n.runWebGLProgram(u,[s],s.dtype)}var yie={kernelName:Di,backendName:"webgl",kernelFunc:bie},vie=class{constructor(e,t){this.variableNames=["Image"],this.outputShape=[],this.customUniforms=[{name:"params",type:"vec4"}];let n=e[1],r=e[2];this.outputShape=e;let s="";typeof t=="number"?s=`float outputValue = ${t.toFixed(2)};`:s=`
2021-09-11 17:11:38 +02:00
vec3 fill = vec3(${t.join(",")});
float outputValue = fill[coords[3]];`,this.userCode=`
void main() {
ivec4 coords = getOutputCoords();
int x = coords[2];
int y = coords[1];
float coordXFloat = (float(x) - params[0]) * params[3] -
(float(y) - params[1]) * params[2];
float coordYFloat = (float(x) - params[0]) * params[2] +
(float(y) - params[1]) * params[3];
int coordX = int(round(coordXFloat + params[0]));
int coordY = int(round(coordYFloat + params[1]));
2024-01-17 22:44:24 +01:00
${s}
if(coordX >= 0 && coordX < ${r} && coordY >= 0 && coordY < ${n}) {
2021-09-11 17:11:38 +02:00
outputValue = getImage(coords[0], coordY, coordX, coords[3]);
}
setOutput(outputValue);
}
2024-01-17 22:44:24 +01:00
`}},xie={kernelName:ll,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:r}=e,{radians:s,fillValue:a,center:o}=t,i=n,u=new vie(r.shape,a),[c,l]=T.getImageCenter(o,r.shape[1],r.shape[2]),p=[[c,l,Math.sin(s),Math.cos(s)]];return i.runWebGLProgram(u,[r],r.dtype,p)}},wie=`
2021-09-11 17:11:38 +02:00
// OpenGL ES does not support round function.
// The algorithm is based on banker's rounding.
float base = floor(x);
if ((x - base) < 0.5) {
return floor(x);
} else if ((x - base) > 0.5) {
return ceil(x);
} else {
if (mod(base, 2.0) == 0.0) {
return base;
} else {
return base + 1.0;
2021-04-01 19:39:54 +02:00
}
}
2024-01-17 22:44:24 +01:00
`,Iie=Ze({opSnippet:wie}),kie={kernelName:$i,backendName:"webgl",kernelFunc:Iie},Sie="return inversesqrt(x);",Cie=Ze({opSnippet:Sie,cpuKernelImpl:OQ}),Tie={kernelName:Fi,backendName:"webgl",kernelFunc:Cie},x0=class{constructor(e,t,n,r,s,a,o=!0,i=!1){this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;let u=ht(s.length),c=ht(a.length),l="";n===1?l="i":n===2&&(l="i, j");let p=`getIndices(${l})`,d="";r===1?d="i":r===2&&(d="i, coords[1]");let h=`getUpdates(${d})`,f="";i&&(f="coords[0], coords[1]");let g=`getDefaultValue(${f})`,m=t>1?"strides[j]":"strides";this.userCode=`
${u} strides = ${u}(${s});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${c} coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
float sum = 0.0;
bool found = false;
for (int i = 0; i < ${e}; i++) {
int flattenedIndex = 0;
for (int j = 0; j < ${t}; j++) {
2024-01-17 22:44:24 +01:00
int index = round(${p});
flattenedIndex += index * ${m};
2021-09-11 17:11:38 +02:00
}
if (flattenedIndex == coords[0]) {
2023-03-21 13:00:18 +01:00
sum += ${h};
2021-09-11 17:11:38 +02:00
found = true;
}
}
2024-01-17 22:44:24 +01:00
setOutput(mix(${g}, sum, float(found)));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}},Nie=class{constructor(e,t,n,r,s,a,o=!0,i=!1){this.variableNames=["updates","indices","defaultValue"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=a;let u=ht(s.length),c=ht(a.length),l="";n===1?l="i":n===2&&(l="i, j");let p=`getIndices(${l})`,d="";r===1?d="i":r===2&&(d="i, coords[1]");let h=`getUpdates(${d})`,f="";i&&(f="coords[0], coords[1]");let g=`getDefaultValue(${f})`,m=t>1?"strides[j]":"strides",b=t>1?"strides[j + 1]":"strides";this.userCode=`
${u} strides = ${u}(${s});
2023-03-21 13:00:18 +01:00
void main() {
2024-01-17 22:44:24 +01:00
${c} coords = getOutputCoords();
2023-03-21 13:00:18 +01:00
vec4 sum = vec4(0.);
vec4 found = vec4(0.);
for (int i = 0; i < ${e}; i+=2) {
ivec2 flattenedIndex = ivec2(0);
for (int j = 0; j < ${t}; j+=2) {
2024-01-17 22:44:24 +01:00
ivec4 index = round(${p});
flattenedIndex += index.xz * ${m};
2023-03-21 13:00:18 +01:00
if (j + 1 < ${t}) {
flattenedIndex += index.yw * ${b};
}
}
if (flattenedIndex[0] == coords[0] || flattenedIndex[1] == coords[0] ||
flattenedIndex[0] == coords[0] + 1 || flattenedIndex[1] == coords[0] + 1) {
vec4 updVals = ${h};
if (flattenedIndex[0] == coords[0]) {
sum.xy += updVals.xy;
found.xy = vec2(1.);
} else if (flattenedIndex[0] == coords[0] + 1) {
sum.zw += updVals.xy;
found.zw = vec2(1.);
}
if (flattenedIndex[1] == coords[0]) {
sum.xy += updVals.zw;
found.xy = vec2(1.);
} else if (flattenedIndex[1] == coords[0] + 1) {
sum.zw += updVals.zw;
found.zw = vec2(1.);
}
}
}
2024-01-17 22:44:24 +01:00
setOutput(mix(${g}, sum, found));
2023-03-21 13:00:18 +01:00
}
2024-01-17 22:44:24 +01:00
`}};function _ie(e){let{inputs:t,backend:n,attrs:r}=e,{indices:s,updates:a}=t,{shape:o}=r,{sliceRank:i,numUpdates:u,sliceSize:c,strides:l,outputSize:p}=T.calculateShapes(a,s,o),d=[p/c,c];if(p===0)return n.makeTensorInfo(o,s.dtype);let h=pe({inputs:{x:s},backend:n,attrs:{shape:[u,i]}}),f=pe({inputs:{x:a},backend:n,attrs:{shape:[u,c]}}),g=n.makeTensorInfo([],"float32",new Float32Array([0])),m;G().getBool("WEBGL_PACK")?m=new Nie(u,i,h.shape.length,f.shape.length,l,d):m=new x0(u,i,h.shape.length,f.shape.length,l,d);let b=n.runWebGLProgram(m,[f,h,g],f.dtype),y=pe({inputs:{x:b},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(b),n.disposeIntermediateTensorInfo(g),y}var Eie={kernelName:Yc,backendName:"webgl",kernelFunc:_ie},Aie=class{constructor(e,t,n,r){this.variableNames=["sortedSequence","values"],this.customUniforms=[{name:"numInputs",type:"int"}],this.outputShape=[e,n];let s="while (left < right) {",a=`for (int i = 0; i < ${Math.ceil(Math.log2(t+1))}; ++i) { if (left >= right) break;`,o=G().getNumber("WEBGL_VERSION")===2?s:a,i=r==="left"?"<":"<=";this.userCode=`
2022-05-18 14:36:05 +02:00
int findBound(int batch, float value) {
int left = 0;
int right = numInputs;
int mid;
2024-01-17 22:44:24 +01:00
${o}
2022-05-18 14:36:05 +02:00
mid = (left + right) / 2;
2024-01-17 22:44:24 +01:00
if (getSortedSequence(batch, mid) ${i} value) {
2022-05-18 14:36:05 +02:00
left = mid + 1;
} else {
right = mid;
}
}
return right;
}
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int valueIndex = coords[1];
float value = getValues(batch, valueIndex);
setOutput(float(findBound(batch, value)));
}
2024-01-17 22:44:24 +01:00
`}};function Die(e){let{inputs:t,backend:n,attrs:r}=e,{sortedSequence:s,values:a}=t,{side:o}=r,i=new Aie(s.shape[0],s.shape[1],a.shape[1],o),u=[[s.shape[1]]];return n.runWebGLProgram(i,[s,a],"int32",u)}var $ie={kernelName:Jc,backendName:"webgl",kernelFunc:Die},Fie=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.outputShape=t;let r,s;if(n>4)throw Error(`Where for rank ${n} is not yet supported`);if(n===1)s="resRC",r="resRC";else{let o=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],u=[];for(let c=0;c<t.length;c++)u.push(`${o[c]}`),c<e&&i.push(`${o[c]}`);r=i.join(),s=u.join()}let a=ht(n);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${a} resRC = getOutputCoords();
float cVal = getC(${r});
2021-09-11 17:11:38 +02:00
if (cVal >= 1.0) {
2024-01-17 22:44:24 +01:00
setOutput(getA(${s}));
2021-09-11 17:11:38 +02:00
} else {
2024-01-17 22:44:24 +01:00
setOutput(getB(${s}));
2021-09-11 17:11:38 +02:00
}
2021-03-26 15:26:02 +01:00
}
2024-01-17 22:44:24 +01:00
`}};function Rie(e){let{inputs:t,backend:n}=e,{condition:r,t:s,e:a}=t,o=new Fie(r.shape.length,s.shape,s.shape.length);return n.runWebGLProgram(o,[r,s,a],fr(s.dtype,a.dtype))}var Pie={kernelName:Qc,backendName:"webgl",kernelFunc:Rie},Oie=`
2021-09-11 17:11:38 +02:00
// Stable and Attracting Fixed Point (0, 1) for Normalized Weights.
// see: https://arxiv.org/abs/1706.02515
2024-01-17 22:44:24 +01:00
float scaleAlpha = ${T.SELU_SCALEALPHA};
float scale = ${T.SELU_SCALE};
2021-09-11 17:11:38 +02:00
return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);
2024-01-17 22:44:24 +01:00
`,Mie=Ze({opSnippet:Oie}),Lie={kernelName:Ri,backendName:"webgl",kernelFunc:Mie},Bie=Nl+`
2022-01-14 16:04:13 +01:00
return 1.0 / (1.0 + exp(-1.0 * x));
2024-01-17 22:44:24 +01:00
`,zie=`
2022-01-14 16:04:13 +01:00
vec4 result = 1.0 / (1.0 + exp(-1.0 * x));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2024-01-17 22:44:24 +01:00
`,Wie=Ze({opSnippet:Bie,packedOpSnippet:zie,cpuKernelImpl:LQ}),Vie={kernelName:Li,backendName:"webgl",kernelFunc:Wie},Uie=`
2021-09-11 17:11:38 +02:00
if (isnan(x)) { return 0.0; }
return sign(x);
2024-01-17 22:44:24 +01:00
`,Gie=Ze({opSnippet:Uie}),Hie={kernelName:Mi,backendName:"webgl",kernelFunc:Gie},jie=Nl+`
2021-09-11 17:11:38 +02:00
return sin(x);
2024-01-17 22:44:24 +01:00
`,qie=`
2023-03-21 13:00:18 +01:00
vec4 result = sin(x);
bvec4 isNaN = isnan(x);
2024-01-17 22:44:24 +01:00
${nu}
2023-03-21 13:00:18 +01:00
return result;
2024-01-17 22:44:24 +01:00
`,Kie=Ze({opSnippet:jie,packedOpSnippet:qie}),Xie={kernelName:Pi,backendName:"webgl",kernelFunc:Kie},Yie=`
2021-09-11 17:11:38 +02:00
float e2x = exp(x);
return (e2x - 1.0 / e2x) / 2.0;
2024-01-17 22:44:24 +01:00
`,Zie=Ze({opSnippet:Yie}),Jie={kernelName:Oi,backendName:"webgl",kernelFunc:Zie},Qie=`
2021-09-11 17:11:38 +02:00
float epsilon = 1.1920928955078125e-7;
float threshold = log(epsilon) + 2.0;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
bool too_large = x > -threshold;
bool too_small = x < threshold;
float result;
float exp_x = exp(x);
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
if (too_large){
result = x;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
else if (too_small){
result = exp_x;
}
else{
result = log(exp_x + 1.0);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
return result;
2024-01-17 22:44:24 +01:00
`,eue=Ze({opSnippet:Qie}),tue={kernelName:Bi,backendName:"webgl",kernelFunc:eue},nue=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockShape:a,paddings:o}=r;w.assert(s.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let i=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...o);for(let b=1+a.length;b<s.shape.length;++b)u.push([0,0]);let c=[],l=ND({inputs:{x:s},backend:n,attrs:{paddings:u,constantValue:0}}),p=T.getReshaped(l.shape,a,i,!1),d=T.getPermuted(p.length,a.length,!1),h=T.getReshapedPermuted(l.shape,a,i,!1),f=pe({inputs:{x:l},backend:n,attrs:{shape:p}}),g=Tn({inputs:{x:f},backend:n,attrs:{perm:d}}),m=pe({inputs:{x:g},backend:n,attrs:{shape:h}});return c.push(l),c.push(f),c.push(g),c.forEach(b=>n.disposeIntermediateTensorInfo(b)),m},rue={kernelName:tl,backendName:"webgl",kernelFunc:nue};function sue(e){let{inputs:t,backend:n}=e,{indices:r,values:s,denseShape:a,defaultValue:o}=t;if(a.shape.length!==1)throw new Error(`Dense shape must be a vector, saw:
${a.shape}`);if(r.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${r.shape}`);if(s.shape.length!==1)throw new Error(`Values must be a vector, saw:
${s.shape}`);if(o.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
${o.shape}`);let i=n.readSync(r.dataId),u=n.readSync(s.dataId),c=n.readSync(a.dataId),l=n.readSync(o.dataId)[0],[p,d,h,f,g]=zQ(i,r.shape,r.dtype,u,s.dtype,c,l);return[n.makeTensorInfo(d,r.dtype,p),n.makeTensorInfo([d[0]],s.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(m=>Number(m)))),n.makeTensorInfo([g.length],r.dtype,new Int32Array(g))]}var aue={kernelName:Yd,backendName:"webgl",kernelFunc:sue};function oue(e){let{inputs:t,backend:n}=e,{inputIndices:r,inputShape:s,newShape:a}=t;if(r.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape ${r.shape}`);if(s.shape.length!==1)throw new Error(`Input shape should be a vector but received shape ${s.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=Array.from(n.readSync(s.dataId)),i=n.readSync(r.dataId),u=Array.from(n.readSync(a.dataId)),[c,l,p]=WQ(i,r.shape,r.dtype,o,u);return[n.makeTensorInfo(l,r.dtype,c),n.makeTensorInfo([p.length],a.dtype,new Int32Array(p))]}var iue={kernelName:rl,backendName:"webgl",kernelFunc:oue};function uue(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);let o=n.readSync(r.dataId),i=n.readSync(s.dataId),u=n.readSync(a.dataId),[c,l]=YA(o,r.shape,r.dtype,i,u,!0);return n.makeTensorInfo(l,r.dtype,c)}var cue={kernelName:Zd,backendName:"webgl",kernelFunc:uue};function lue(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
${a.shape}`);let o=n.readSync(r.dataId),i=n.readSync(s.dataId),u=n.readSync(a.dataId),[c,l]=YA(o,r.shape,r.dtype,i,u);return n.makeTensorInfo(l,r.dtype,c)}var due={kernelName:Jd,backendName:"webgl",kernelFunc:lue};function pue(e){let{inputs:t,backend:n,attrs:r}=e,{sparseIndices:s,sparseValues:a,defaultValue:o}=t,{outputShape:i}=r,{sliceRank:u,numUpdates:c,sliceSize:l,strides:p,outputSize:d}=T.calculateShapes(a,s,i),h=!1;if(a.dtype==="string"){let b=n.bufferSync(s),y=n.bufferSync(a),v=w.decodeString(n.readSync(o.dataId)[0]),x=MQ(b,y,i,d,l,c,u,p,v,h);return n.makeTensorInfo(i,x.dtype,x.values)}let f=new x0(c,u,s.shape.length,a.shape.length,p,[d,1],h),g=n.runWebGLProgram(f,[a,s,o],a.dtype),m=pe({inputs:{x:g},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(g),m}var hue={kernelName:sl,backendName:"webgl",kernelFunc:pue};function fue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=r,i=w.parseAxisParam(o,s.shape)[0],u=T.prepareSplitSize(s,a,i),c=s.shape.length,l=new Array(c).fill(0),p=s.shape.slice();return u.map(d=>{let h=[...p];h[i]=d;let f=_l({inputs:{x:s},backend:n,attrs:{begin:l,size:h}});return l[i]+=d,f})}var mue={kernelName:nl,backendName:"webgl",kernelFunc:fue},j1="return sqrt(x);",gue=Ze({opSnippet:j1,packedOpSnippet:j1,cpuKernelImpl:VQ}),bue={kernelName:zi,backendName:"webgl",kernelFunc:gue},yue="return x * x;",vue=Ze({opSnippet:yue}),xue={kernelName:Qd,backendName:"webgl",kernelFunc:vue},q1="return (a - b) * (a - b);",wue=mn({opSnippet:q1,packedOpSnippet:q1}),Iue={kernelName:Ui,backendName:"webgl",kernelFunc:wue};function kue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");let a=n.readSync(s.dataId),o=T.fromUint8ToStringArray(a),i=UQ(o,"string",r);return n.makeTensorInfo(s.shape,"string",i)}var Sue={kernelName:ep,backendName:"webgl",kernelFunc:kue};function Cue({inputs:e,attrs:t,backend:n}){let{x:r}=e,s=Or+`
2021-09-11 17:11:38 +02:00
return x > 0.0 ? 1.0 : float(${t.alpha});
2024-01-17 22:44:24 +01:00
`,a=new is(r.shape,s);return n.runWebGLProgram(a,[r],r.dtype)}var Tue={kernelName:Na,backendName:"webgl",kernelFunc:Cue},Nue=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let r=n.length,s=ht(n.length),a=ht(n.length),o="";if(r===1)o="coords * strides + begin";else{let i=0;o=n.map((u,c)=>(i++,n.length===1?`coords * strides[${c}] + begin[${c}]`:`coords[${i-1}] * strides[${c}] + begin[${c}]`)).join(",")}this.userCode=`
${s} begin = ${s}(${e});
${s} strides = ${s}(${t});
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${a} coords = getOutputCoords();
setOutput(getX(${o}));
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function _ue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,end:o,strides:i,beginMask:u,endMask:c,ellipsisMask:l,newAxisMask:p,shrinkAxisMask:d}=r,{finalShapeSparse:h,finalShape:f,isIdentity:g,sliceDim0:m,isSimpleSlice:b,begin:y,end:v,strides:x}=Kt.sliceInfo(s.shape,a,o,i,u,c,l,p,d),k;if(g)k=pe({inputs:{x:s},backend:n,attrs:{shape:f}});else if(m||b){w.assert(s.shape.length>=1,()=>`Input must have rank at least 1, got: ${s.shape.length}`);let N=Kt.computeOutShape(y,v,x),E=_l({inputs:{x:s},backend:n,attrs:{begin:y,size:N}});k=pe({inputs:{x:E},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(E)}else if(n.shouldExecuteOnCPU([s])){let E=n.readSync(s.dataId),$=Me(s.shape,s.dtype,E),F=GQ(h,$,x,y);k=n.makeTensorInfo(f,s.dtype,F.values)}else{let E=new Nue(y,x,h);k=n.runWebGLProgram(E,[s],s.dtype)}let S=pe({inputs:{x:k},backend:n,attrs:{shape:f}});return n.disposeIntermediateTensorInfo(k),S}var Eue={kernelName:al,backendName:"webgl",kernelFunc:_ue};function Aue(e){let{inputs:t,backend:n,attrs:r}=e,{separator:s,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:c}=r,{data:l,dataSplits:p}=t,d=n.readSync(l.dataId),h=n.readSync(p.dataId),[f,g]=HQ(d,h,s,a,o,i,u,c);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(p.shape,"int32",g)]}var Due={kernelName:tp,backendName:"webgl",kernelFunc:Aue};function $ue(e){let{inputs:t,backend:n,attrs:r}=e,{skipEmpty:s}=r,{input:a,delimiter:o}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(o.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${o.shape}`);let i=n.readSync(a.dataId),u=n.readSync(o.dataId)[0],[c,l,p]=jQ(i,u,s),d=l.length;return[n.makeTensorInfo([d,2],"int32",c),n.makeTensorInfo([d],"string",l),n.makeTensorInfo([2],"int32",new Int32Array(p))]}var Fue={kernelName:np,backendName:"webgl",kernelFunc:$ue};function Rue(e){let{inputs:t,backend:n,attrs:r}=e,{numBuckets:s}=r,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(s<=0)throw new Error("Number of buckets must be at least 1");let o=n.readSync(a.dataId),i=qQ(o,s);return n.makeTensorInfo(a.shape,"int32",i)}var Pue={kernelName:rp,backendName:"webgl",kernelFunc:Rue},Oue="return tan(x);",Mue=Ze({opSnippet:Oue}),Lue={kernelName:Hi,backendName:"webgl",kernelFunc:Mue},Bue=`
2021-09-11 17:11:38 +02:00
float e2x = exp(-2.0 * abs(x));
return sign(x) * (1.0 - e2x) / (1.0 + e2x);
2024-01-17 22:44:24 +01:00
`,zue=Ze({opSnippet:Bue}),Wue={kernelName:ji,backendName:"webgl",kernelFunc:zue};function Vue(e){let{inputs:t,backend:n,attrs:r}=e,{tensor:s,indices:a,updates:o}=t,{}=r,{sliceRank:i,numUpdates:u,sliceSize:c,strides:l,outputSize:p}=T.calculateShapes(o,a,s.shape),d=[p/c,c];if(p===0)return n.makeTensorInfo(s.shape,a.dtype);let h=pe({inputs:{x:a},backend:n,attrs:{shape:[u,i]}}),f=pe({inputs:{x:o},backend:n,attrs:{shape:[u,c]}}),g=pe({inputs:{x:s},backend:n,attrs:{shape:d}}),m=new x0(u,i,h.shape.length,f.shape.length,l,d,!1,!0),b=n.runWebGLProgram(m,[f,h,g],g.dtype),y=pe({inputs:{x:b},backend:n,attrs:{shape:s.shape}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),n.disposeIntermediateTensorInfo(b),y}var Uue={kernelName:Zc,backendName:"webgl",kernelFunc:Vue},Gue=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 r=ht(this.rank),s=Hue(e);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2024-01-17 22:44:24 +01:00
${r} resRC = getOutputCoords();
setOutput(getA(${s}));
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
`}};function Hue(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"],r=[];for(let s=0;s<e.length;s++)r.push(`imod(${n[s]}, ${e[s]})`);return r.join()}function ED(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reps:a}=r;if(s.dtype==="string"||s.shape.length>5){let u=n.readSync(s.dataId),c=s.dtype==="string"?u.map(d=>w.decodeString(d)):u,l=Me(s.shape,s.dtype,c),p=XQ(l,a);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let o=new Gue(s.shape,a);return n.runWebGLProgram(o,[s],s.dtype)}var jue={kernelName:Ta,backendName:"webgl",kernelFunc:ED},que=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=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int elemIdx = coords[1];
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
// 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.
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
bool isFirstInPair = imod(elemIdx, 2 * inc) < inc;
int i = isFirstInPair ? elemIdx : elemIdx - inc;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
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;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
// 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));
}
}
2024-01-17 22:44:24 +01:00
`}},Kue=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=`
2021-09-11 17:11:38 +02:00
void main() {
// Takes max of indices (0, k), (1, k + 1), (2, k + 2) ...
ivec2 coords = getOutputCoords();
int batch = coords[0];
int elemIdx = coords[1];
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
// 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.
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
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));
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
float x0 = getX(batch, i0);
float x1 = i1 < n ? getX(batch, i1) : x0;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
setOutput(x0 >= x1 ? float(i0) : float(i1));
}
2024-01-17 22:44:24 +01:00
`}};function Xa(e,t){t!==null&&e.disposeIntermediateTensorInfo(t)}function K1(e){let t=1;for(;t<e;)t*=2;return t}function Xue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{k:a,sorted:o}=r,i=G().getNumber("TOPK_LAST_DIM_CPU_HANDOFF_SIZE_THRESHOLD"),u=G().getNumber("TOPK_K_CPU_HANDOFF_THRESHOLD"),c=s.shape,l=c[c.length-1];if(n.shouldExecuteOnCPU([s])||l<i||a>u){let F=n.readSync(s.dataId),[D,R]=YQ(F,c,s.dtype,a,o);return[n.makeTensorInfo(D.shape,D.dtype,D.values),n.makeTensorInfo(R.shape,R.dtype,R.values)]}if(a===0)return c[c.length-1]=0,[n.makeTensorInfo(c,s.dtype,[]),n.makeTensorInfo(c,"int32",[])];if(l===1)return[s,Lp({attrs:{shape:c,dtype:"int32",value:0},backend:n})];let p=n.texData.get(s.dataId),d=p!==null&&p.isPacked,h=d?n.unpackTensor(s):s,g=w.sizeFromShape(c)/l,m=pe({inputs:{x:h},attrs:{shape:[g,l]},backend:n});d&&Xa(n,h);let b=K1(a),y=K1(l),v=null,x=()=>v===null?[m,m]:[m,v],k=(F,D,R)=>{let C=x(),L=new que(R),H=[[l],[v===null?1:0],[Number.NEGATIVE_INFINITY],[F],[D]],K=v;v=n.runWebGLProgram(L,C,"int32",H),Xa(n,K)};for(let F=1;F<b;F*=2){let D=F*2;for(let R=F;R>=1;R/=2)k(D,R,[g,y])}for(let F=y;F>b;F/=2){let D=x(),R=new Kue([g,F/2]),L=[[l],[v===null?1:0],[b]],U=v;v=n.runWebGLProgram(R,D,"int32",L),Xa(n,U);let H=b/2,K=H*2;for(let q=H;q>=1;q/=2)k(K,q,v.shape)}let S=v;v=_l({inputs:{x:v},backend:n,attrs:{begin:0,size:[g,a]}}),Xa(n,S);let N=wD({inputs:{x:m,indices:v},backend:n,attrs:{axis:1,batchDims:1}});Xa(n,m);let E=c.slice(0,-1);E.push(a),S=v,v=pe({inputs:{x:v},attrs:{shape:E},backend:n}),Xa(n,S);let $=N;return N=pe({inputs:{x:N},attrs:{shape:E},backend:n}),Xa(n,$),[N,v]}var Yue={kernelName:ol,backendName:"webgl",kernelFunc:Xue},Zue=class{constructor(e,t,n,r,s,a){this.variableNames=["Image","Transforms"],this.outputShape=a;let o=n==="nearest"?1:2,i;switch(r){case"constant":i=1;break;case"reflect":i=2;break;case"wrap":i=3;break;case"nearest":i=4;break;default:i=1;break}this.userCode=`
2021-09-11 17:11:38 +02:00
float mapCoord(float outCoord, float len) {
float inCoord = outCoord;
2024-01-17 22:44:24 +01:00
if(${i} == 2) {
2021-09-11 17:11:38 +02:00
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz2 = 2.0 * len;
if (inCoord < sz2) {
inCoord = sz2 * float(int(float(-inCoord / sz2))) +
inCoord;
}
inCoord = inCoord < -len ? inCoord + sz2 : -inCoord - 1.0;
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz2 = 2.0 * len;
inCoord -= sz2 * float(int(float(inCoord / sz2)));
if (inCoord >= len) {
inCoord = sz2 - inCoord - 1.0;
}
}
}
return clamp(inCoord, 0.0, len - 1.0);
2024-01-17 22:44:24 +01:00
} else if (${i} == 3) {
2021-09-11 17:11:38 +02:00
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz = len - 1.0;
inCoord += len * (float(int(float(-inCoord / sz))) + 1.0);
}
} else if (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
float sz = len - 1.0;
inCoord -= len * float(int(float(inCoord / sz)));
}
}
return clamp(inCoord, 0.0, len - 1.0);
2024-01-17 22:44:24 +01:00
} else if (${i} == 4) {
2021-09-11 17:11:38 +02:00
return clamp(outCoord, 0.0, len - 1.0);
} else {
return outCoord;
}
}
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
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 {
2024-01-17 22:44:24 +01:00
outputValue = float(${s});
2021-09-11 17:11:38 +02:00
}
return outputValue;
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
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) {
2024-01-17 22:44:24 +01:00
outputValue = float(${s});
2021-09-11 17:11:38 +02:00
} 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}));
2021-03-31 13:01:22 +02:00
2024-01-17 22:44:24 +01:00
if (${o} == 1) {
2021-09-11 17:11:38 +02:00
int coordY = int(round(mapY));
int coordX = int(round(mapX));
outputValue = readWithFillValue(batch, coordY, coordX,
channel);
} else {
float yFloor = floor(mapY);
float xFloor = floor(mapX);
float yCeil = yFloor + 1.0;
float xCeil = xFloor + 1.0;
float valueYFloor = (xCeil - mapX) *
readWithFillValue(batch, int(yFloor), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yFloor), int(xCeil), channel);
float valueYCeil = (xCeil - mapX) *
readWithFillValue(batch, int(yCeil), int(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, int(yCeil), int(xCeil), channel);
outputValue = (yCeil - mapY) * valueYFloor +
(mapY - yFloor) * valueYCeil;
}
}
setOutput(outputValue);
}
2024-01-17 22:44:24 +01:00
`}};function Jue(e){let{inputs:t,backend:n,attrs:r}=e,{image:s,transforms:a}=t,{interpolation:o,fillMode:i,fillValue:u,outputShape:c}=r,[l,p,d,h]=s.shape,[f,g]=c!=null?c:[p,d],m=[l,f,g,h],b=new Zue(p,d,o,i,u,m);return n.runWebGLProgram(b,[s,a],"float32")}var Que={kernelName:il,backendName:"webgl",kernelFunc:Jue};function ece(e){let{inputs:t,attrs:n,backend:r}=e,{axis:s}=n,{x:a}=t;wl(a,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let o=r.readSync(a.dataId),{outputValues:i,outputShape:u,indices:c}=ZQ(o,s,a.shape,a.dtype);return[r.makeTensorInfo(u,a.dtype,i),r.makeTensorInfo([c.length],"int32",c)]}var tce={kernelName:sp,backendName:"webgl",kernelFunc:ece};function nce(e){let{inputs:t,backend:n,attrs:r}=e,{value:s}=t,{axis:a}=r;a<0&&(a+=s.shape.length);let o=s,i=o.shape.length,u=s.shape[a],c=new Array(i-1),l=0;for(let g=0;g<i;g++)g!==a&&(c[l++]=o.shape[g]);let p=[],d=new Array(i).fill(0),h=o.shape.slice();h[a]=1;let f=new Array(u);for(let g=0;g<f.length;g++){d[a]=g;let m=_l({inputs:{x:o},backend:n,attrs:{begin:d,size:h}}),b=pe({inputs:{x:m},backend:n,attrs:{shape:c}});f[g]=b,p.push(m)}return p.forEach(g=>n.disposeIntermediateTensorInfo(g)),f}var rce={kernelName:ul,backendName:"webgl",kernelFunc:nce},sce=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,r=e.batchSize,s=e.inSize,a=e.numSegments,o=a*Math.ceil(s/n);this.outputShape=[r,o];let i="0.0",u="sumValue",c=Math.floor(n/4)*4,l=n%4,p=`
2021-09-11 17:11:38 +02:00
sumValue += dot(values, segFilter);
2024-01-17 22:44:24 +01:00
`,d="";s%n>0&&(d=`
if (inIdx < 0 || inIdx >= ${s}) {
2021-09-11 17:11:38 +02:00
return initializationValue;
}
2024-01-17 22:44:24 +01:00
`);let h="";s%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${s}) {
2021-09-11 17:11:38 +02:00
return -1.0;
}
`),this.userCode=`
2024-01-17 22:44:24 +01:00
const float initializationValue = ${i};
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
float getValue(int batch, int inIdx) {
2024-01-17 22:44:24 +01:00
${d}
2021-09-11 17:11:38 +02:00
return getX(batch, inIdx);
2021-03-26 15:26:02 +01:00
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
float getSegmentIdAtIndex(int inIdx) {
${h}
return getSegmentIds(inIdx);
2021-03-31 13:01:22 +02:00
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = int(floor(float(outIdx) / float(
2024-01-17 22:44:24 +01:00
${a})) * float(${n}));
int currentSeg = int(mod(float(outIdx), float(${a})));
2021-03-09 23:32:33 +01:00
2021-09-11 17:11:38 +02:00
float sumValue = 0.0;
2021-03-09 23:32:33 +01:00
2024-01-17 22:44:24 +01:00
for (int i = 0; i < ${c}; i += 4) {
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
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
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${p}
2020-12-15 14:15:43 +01:00
}
2020-10-29 05:16:50 +01:00
2024-01-17 22:44:24 +01:00
int inIdx = inOffset + ${c};
if (${l===1}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
int inIdxSeg = int(getSegmentIdAtIndex(inIdx));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
0,
0,
0
);
2021-03-26 15:26:02 +01:00
2024-01-17 22:44:24 +01:00
${p}
} else if (${l===2}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
0,
0
);
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
${p}
} else if (${l===3}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
0
);
2020-10-29 05:16:50 +01:00
2024-01-17 22:44:24 +01:00
${p}
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:44:24 +01:00
setOutput(${u});
2021-12-01 21:37:52 +01:00
}
2024-01-17 22:44:24 +01:00
`}};function ace(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,segmentIds:a}=t,{numSegments:o}=r,i=s.shape.length,u=[],c=0,l=T.getAxesPermutation([c],i),p=s;l!=null&&(p=Tn({inputs:{x:s},backend:n,attrs:{perm:l}}),u.push(p),c=T.getInnerMostAxes(1,i)[0]);let d=T.segment_util.computeOutShape(p.shape,c,o),h=w.sizeFromShape([p.shape[c]]),f=pe({inputs:{x:p},backend:n,attrs:{shape:[-1,h]}});u.push(f);let g=Zf(s.dtype),m=(x,k,S,N,E)=>{let $=x.shape[0],F=x.shape[1],D=T.segment_util.segOpComputeOptimalWindowSize(F,E),R={windowSize:D,inSize:F,batchSize:$,numSegments:E},C=new sce(R,k),L=n.compileAndRun(C,[x,S],N);if(u.push(L),L.shape[1]===E)return L;let U=_D({backend:n,attrs:{start:0,stop:E,step:1,dtype:"float32"}}),H=ED({inputs:{x:U},backend:n,attrs:{reps:[F/D]}});return u.push(U),u.push(H),m(L,k,H,N,E)},b=m(f,"unsortedSegmentSum",a,g,o),y=pe({inputs:{x:b},backend:n,attrs:{shape:d}}),v=y;if(l!=null){u.push(y);let x=T.getUndoAxesPermutation(l);v=Tn({inputs:{x:v},backend:n,attrs:{perm:x}})}return u.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var oce={kernelName:ap,backendName:"webgl",kernelFunc:ace},ice=[Gee,jee,Xee,Jee,ete,rte,ate,ite,dte,hte,gte,vte,Ite,Tte,Ete,Dte,Fte,Mte,Bte,Wte,Hte,Jte,ene,sne,one,pne,fne,yne,Nee,wne,Tne,Ane,One,Bne,Wne,Une,Hne,Xne,Jne,tre,rre,are,ire,lre,pre,gre,yre,wre,Sre,Tre,Are,Rre,Lre,Wre,Gre,Hre,qre,Xre,Zre,Qre,tse,ase,use,dse,hse,gse,vse,kse,Nse,Tee,Ese,Sne,$se,Pse,Lse,Eee,Vse,jse,Kse,Jse,tae,aae,uae,pae,gae,vae,wae,Cae,Nae,Eae,Fae,Pae,Mae,Bae,Wae,Hae,Xae,Qae,ioe,$ee,doe,foe,boe,xoe,une,koe,Coe,Noe,Aoe,Roe,Dee,Ooe,Loe,zoe,Voe,Uoe,cne,roe,joe,Yoe,eie,Ree,sie,iie,die,fie,yie,xie,kie,Tie,Eie,$ie,Pie,Lie,Vie,Hie,Xie,Jie,Yte,aoe,tue,rue,aue,iue,cue,due,hue,mue,bue,xue,Iue,Sue,Tue,Eue,Due,Fue,Pue,soe,Wee,Lue,Wue,Uue,jue,Yue,Que,Vee,tce,rce,oce,Soe];for(let e of ice)op(e);var et;(function(e){e[e.float32=0]="float32",e[e.int32=1]="int32",e[e.bool=2]="bool",e[e.string=3]="string",e[e.complex64=4]="complex64"})(et||(et={}));var Od;(function(e){e[e.linear=0]="linear",e[e.relu=1]="relu",e[e.relu6=2]="relu6",e[e.prelu=3]="prelu",e[e.leakyrelu=4]="leakyrelu",e[e.sigmoid=5]="sigmoid",e[e.elu=6]="elu"})(Od||(Od={}));var AD;function uce(e){AD=e.wasm.cwrap(uo,null,["number","array","number","number","array","number","number","number","number","number","number","number","number"])}function cce(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a,bias:o,preluActivationWeights:i}=t;if(s.dtype!=="float32"||a.dtype!=="float32")throw new Error("_FusedMatMul for non non-float32 tensors not yet supported.");let{transposeA:u,transposeB:c,activation:l,leakyreluAlpha:p}=r,d=n.dataIdMap.get(s.dataId).id,h=n.dataIdMap.get(a.dataId).id,f=0;if(o!=null){let E=n.dataIdMap.get(o.dataId);if(E.shape.length!==1)throw new Error(`_FusedMatMul only supports rank-1 bias but got rank ${E.shape.length}.`);f=E.id}let g=i==null?0:n.dataIdMap.get(i.dataId).id,m=Od[l];if(m==null)throw new Error(`${l} activation not yet supported for FusedConv2D in the wasm backend.`);let b=u?s.shape[2]:s.shape[1],y=c?a.shape[1]:a.shape[2],v=dl.assertAndGetBroadcastShape(s.shape.slice(0,-2),a.shape.slice(0,-2)),x=n.makeOutput([...v,b,y],s.dtype),k=n.dataIdMap.get(x.dataId).id,S=new Uint8Array(new Int32Array(s.shape).buffer),N=new Uint8Array(new Int32Array(a.shape).buffer);return AD(d,S,s.shape.length,h,N,a.shape.length,u,c,m,f,g,p||0,k),x}var lce={kernelName:uo,backendName:"wasm",setupFunc:uce,kernelFunc:cce};function Xe(e,t){let n;function r(a){n=a.wasm.cwrap(e,null,["number","number","number"])}function s(a){let{backend:o,inputs:{x:i}}=a,u=o.dataIdMap.get(i.dataId).id,c=o.makeOutput(i.shape,t||i.dtype),l=o.dataIdMap.get(c.dataId).id;return w.sizeFromShape(c.shape)===0||n(u,et[i.dtype],l),c}return{kernelName:e,backendName:"wasm",setupFunc:r,kernelFunc:s}}var dce=Xe(cc),pce=Xe(Eo),hce=Xe(Ao);function Ut(e,t,n){let r;function s(o){r=o.wasm.cwrap(e,null,["number","array","number","number","array","number","number","number"])}function a(o){let{backend:i,inputs:u}=o,{a:c,b:l}=u,p=i.dataIdMap.get(c.dataId).id,d=i.dataIdMap.get(l.dataId).id,h=n!=null?n:c.dtype,f=T.assertAndGet
${r.shape}`);if(s.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=t.dataIdMap.get(r.dataId).id,i=t.dataIdMap.get(s.dataId).id,u=t.dataIdMap.get(a.dataId).id,c=r.shape[0],l=w.sizeFromShape(a.shape),p=t.makeOutput([c,l],r.dtype),d=t.dataIdMap.get(p.dataId).id,h=t.makeOutput([l],a.dtype),f=t.dataIdMap.get(h.dataId).id,g=t.makeOutput([3],"int32"),m=t.dataIdMap.get(g.dataId).id;K$(o,i,u,c,d,f,m);let b=t.readSync(g.dataId),y;switch(b[0]){case 0:{y=T.getSparseReshapeMultipleNegativeOneOutputDimErrorMessage(b[1],b[2]);break}case 1:{y=T.getSparseReshapeNegativeOutputDimErrorMessage(b[1],b[2]);break}case 2:y=T.getSparseReshapeEmptyTensorZeroOutputDimErrorMessage();break;case 3:{let v=Array.from(t.readSync(s.dataId)),x=Array.from(t.readSync(h.dataId));y=T.getSparseReshapeInputOutputMultipleErrorMessage(v,x);break}case 4:{let v=Array.from(t.readSync(s.dataId)),x=Array.from(t.readSync(h.dataId));y=T.getSparseReshapeInputOutputMismatchErrorMessage(v,x);break}default:y=""}if(t.disposeData(g.dataId),y)throw t.disposeData(p.dataId),t.disposeData(h.dataId),new Error(y);return[p,h]}var $fe={kernelName:rl,backendName:"wasm",setupFunc:Afe,kernelFunc:Dfe},X$;function Y$(e){X$=e.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function Z$(e,t){let{backend:n,inputs:r}=e,{data:s,indices:a,segmentIds:o}=r,i=a.shape[0],u=n.readSync(o.dataId,i-1,i)[0],l=i>0?u+1:0;if(l<0)throw new Error(T.getSparseSegmentReductionNegativeSegmentIdsErrorMessage());let p=s.shape.slice();p[0]=l;let d=n.dataIdMap.get(s.dataId).id,h=n.dataIdMap.get(a.dataId).id,f=n.dataIdMap.get(o.dataId).id,g=n.makeOutput(p,s.dtype),m=n.dataIdMap.get(g.dataId).id,b=n.makeOutput([4],"int32"),y=n.dataIdMap.get(b.dataId).id;X$(d,et[s.dtype],s.shape[0],h,f,m,y,t,0);let v=n.readSync(b.dataId),x;switch(v[0]){case 0:{x=T.getSparseSegmentReductionNegativeSegmentIdsErrorMessage();break}case 1:{x=T.getSparseSegmentReductionNonIncreasingSegmentIdsErrorMessage();break}case 2:x=T.getSparseSegmentReductionSegmentIdOutOfRangeErrorMessage(v[1],v[2]);break;case 3:x=T.getSparseSegmentReductionIndicesOutOfRangeErrorMessage(v[1],v[2],v[3]);break;default:x=""}if(n.disposeData(b.dataId),x)throw n.disposeData(g.dataId),new Error(x);return g}function Ffe(e){return Z$(e,!0)}var Rfe={kernelName:Zd,backendName:"wasm",setupFunc:Y$,kernelFunc:Ffe};function Pfe(e){return Z$(e,!1)}var Ofe={kernelName:Jd,backendName:"wasm",setupFunc:Y$,kernelFunc:Pfe},J$;function Mfe(e){J$=e.wasm.cwrap(sl,null,["number","number","number","number","number","number","number","number","array","number","number"])}function Lfe(e){let{backend:t,inputs:n,attrs:r}=e,{sparseIndices:s,sparseValues:a,defaultValue:o}=n,{outputShape:i}=r,u=t.makeOutput(i,o.dtype);if(w.sizeFromShape(i)===0)return u;let{sliceRank:c,numUpdates:l,sliceSize:p,strides:d,outputSize:h}=T.calculateShapes(a,s,i),f=t.dataIdMap.get(s.dataId).id,g=t.dataIdMap.get(a.dataId).id,m=t.dataIdMap.get(o.dataId).id,b=new Uint8Array(new Int32Array(d).buffer),y=t.dataIdMap.get(u.dataId).id;return J$(f,g,a.shape.length,m,et[o.dtype],c,l,p,b,h,y),u}var Bfe={kernelName:sl,backendName:"wasm",setupFunc:Mfe,kernelFunc:Lfe};function zfe(e){let{inputs:t,attrs:n,backend:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=n,i=w.parseAxisParam(o,s.shape)[0],u=T.prepareSplitSize(s,a,i),c=new Array(s.shape.length).fill(0),l=s.shape.slice();return u.map(p=>{let d=[...l];d[i]=p;let h=No({inputs:{x:s},attrs:{begin:c,size:d},backend:r});return c[i]+=p,h})}var Wfe={kernelName:nl,backendName:"wasm",kernelFunc:zfe},Vfe=Xe(zi),Ufe=Xe(Qd),Gfe=!0,Hfe=Ut(Ui,Gfe),Q$;function jfe(e){Q$=e.wasm.cwrap(Na,null,["number","number","number","number"])}function qfe(e){let{backend:t,inputs:n,attrs:r}=e,{alpha:s}=r,{x:a}=n,o=t.dataIdMap.get(a.dataId).id,i=t.makeOutput(a.shape,a.dtype),u=t.dataIdMap.get(i.dataId).id;return Q$(o,s,et[a.dtype],u),i}var Kfe={kernelName:Na,backendName:"wasm",setupFunc:jfe,kernelFunc:qfe},eF;function Xfe(e){eF=e.wasm.cwrap(al,null,["number","array","number","array","ar