2022-08-22 19:17:39 +02:00
/ *
2020-12-02 22:46:41 +01:00
Face - API
homepage : < https : //github.com/vladmandic/face-api>
author : < https : //github.com/vladmandic>'
2021-09-08 19:51:28 +02:00
* /
2020-12-02 22:46:41 +01:00
2024-01-17 22:56:53 +01:00
"use strict" ; var faceapi = ( ( ) => { var px = Object . defineProperty ; var NR = Object . getOwnPropertyDescriptor ; var TR = Object . getOwnPropertyNames ; var CR = Object . prototype . hasOwnProperty ; var ER = ( 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 ) px ( e , n , { get : t [ n ] , enumerable : ! 0 } ) } , _R = ( e , t , n , a ) => { if ( t && typeof t == "object" || typeof t == "function" ) for ( let r of TR ( t ) ) ! CR . call ( e , r ) && r !== n && px ( e , r , { get : ( ) => t [ r ] , enumerable : ! ( a = NR ( t , r ) ) || a . enumerable } ) ; return e } ; var AR = e => _R ( px ( { } , "__esModule" , { value : ! 0 } ) , e ) ; var Xge = { } ; $h ( Xge , { AgeGenderNet : ( ) => Kd , BoundingBox : ( ) => il , Box : ( ) => on , ComposableTask : ( ) => Sa , ComputeAllFaceDescriptorsTask : ( ) => Hr , ComputeFaceDescriptorsTaskBase : ( ) => Jd , ComputeSingleFaceDescriptorTask : ( ) => qr , DetectAllFaceLandmarksTask : ( ) => eh , DetectAllFacesTask : ( ) => Hp , DetectFaceLandmarksTaskBase : ( ) => Qd , DetectFacesTaskBase : ( ) => nh , DetectSingleFaceLandmarksTask : ( ) => th , DetectSingleFaceTask : ( ) => ah , Dimensions : ( ) => Un , FACE _EXPRESSION _LABELS : ( ) => Hk , FaceDetection : ( ) => Tt , FaceDetectionNet : ( ) => Jk , FaceExpressionNet : ( ) => jd , FaceExpressions : ( ) => Vr , FaceLandmark68Net : ( ) => bl , FaceLandmark68TinyNet : ( ) => Xd , FaceLandmarkNet : ( ) => Xk , FaceLandmarks : ( ) => sa , FaceLandmarks5 : ( ) => Fk , FaceLandmarks68 : ( ) => ul , FaceMatch : ( ) => Ap , FaceMatcher : ( ) => tI , FaceRecognitionNet : ( ) => yl , Gender : ( ) => vg , LabeledBox : ( ) => Fp , LabeledFaceDescriptors : ( ) => Ls , NetInput : ( ) => xr , NeuralNetwork : ( ) => un , ObjectDetection : ( ) => ol , Point : ( ) => Ue , PredictedBox : ( ) => $k , Rect : ( ) => ll , SsdMobilenetv1 : ( ) => zs , SsdMobilenetv1Options : ( ) => Ia , TinyFaceDetector : ( ) => kl , TinyFaceDetectorOptions : ( ) => Zd , TinyYolov2 : ( ) => vl , TinyYolov2Options : ( ) => Ja , allFaces : ( ) => qge , allFacesSsdMobilenetv1 : ( ) => GD , allFacesTinyYolov2 : ( ) => Hge , awaitMediaLoaded : ( ) => zk , bufferToImage : ( ) => Wk , computeFaceDescriptor : ( ) => Fge , createCanvas : ( ) => ml , createCanvasFromMedia : ( ) => Ud , createFaceDetectionNet : ( ) => Sge , createFaceRecognitionNet : ( ) => hge , createSsdMobilenetv1 : ( ) => _D , createTinyFaceDetector : ( ) => jge , createTinyYolov2 : ( ) => Cge , detectAllFaces : ( ) => Ag , detectFaceLandmarks : ( ) => VD , detectFaceLandmarksTiny : ( ) => Age , detectLandmarks : ( ) => Uge , detectSingleFace : ( ) => Gge , draw : ( ) => jk , env : ( ) => tt , euclideanDistance : ( ) => eI , extendWithAge : ( ) => Sg , extendWithFaceDescriptor : ( ) => Ig , extendWithFaceDetection : ( ) => pl , extendWithFaceExpressions : ( ) => gg , extendWithFaceLandmarks : ( ) => Wp , extendWithGender : ( ) => Ng , extractFaceTensors : ( ) => Rp , extractFaces : ( ) => Dp , fetchImage : ( ) => Qfe , fetchJson : ( ) => Uk , fetchNetWeights : ( ) => ege , fetchOrThrow : ( ) => Br , fetchVideo : ( ) => tge , getContext2dOrThrow : ( ) => Gn , getMediaDimensions : ( ) => hl , imageTensorToCanvas : ( ) => Bk , imageToSquare : ( ) => Vk , inverseSigmoid : ( ) => qfe , iou : ( ) => Ck , isMediaElement : ( ) => lg , isMediaLoaded : ( ) => Vd , isWithAge : ( ) => mge , isWithFaceDetection : ( ) => yr , isWithFaceExpressions : ( ) => qk , isWithFaceLandmarks : ( ) => gl , isWithGender : ( ) => fge , loadAgeGenderModel : ( ) => Wge , loadFaceDetectionModel : ( ) => Bge , loadFaceExpressionModel : ( ) => zge , loadFaceLandmarkModel : ( ) => Oge , loadFaceLandmarkTinyModel : ( ) => Pge , loadFaceRecognitionModel : ( ) => Lge , loadSsdMobilenetv1Model : ( ) => UD , loadTinyFaceDetectorModel : ( ) => Rge , loadTinyYolov2Model : ( ) => Mge , loadWeightMap : ( ) => Gk , locateFaces : ( ) => Vge , matchDimensions : ( ) => nge , minBbox : ( ) => Ek , nets : ( ) => nt , nonMaxSuppression : ( ) => _k , normalize : ( ) => Ya , padToSquare : ( ) => Ak , predictAgeAndGender : ( ) => Dge , recognizeFaceExpressions : ( ) => $ge , resizeResults : ( ) => HD , resolveInput : ( ) => cl , shuffleArray : ( ) => Hfe , sigmoid : ( ) => zd , ssdMobilenetv1 : ( ) => BD , tf : ( ) => Oe , tinyFaceDetector : ( ) => Ege , tinyYolov2 : ( ) => _ge , toNetInput : ( ) => vt , utils : ( ) => Tk , validateConfig : ( ) => Qk , version : ( ) => Kge } ) ; var Oe = { } ; $h ( Oe , { Abs : ( ) => uu , Acos : ( ) => Ci , Acosh : ( ) => Ei , AdadeltaOptimizer : ( ) => Yw , AdagradOptimizer : ( ) => Zw , AdamOptimizer : ( ) => Jw , AdamaxOptimizer : ( ) => Qw , Add : ( ) => Is , AddN : ( ) => _i , All : ( ) => pu , Any : ( ) => cu , ArgMax : ( ) => du , ArgMin : ( ) => hu , Asin : ( ) => Ai , Asinh : ( ) => Fi , Atan : ( ) => $i , Atan2 : ( ) => Ri , Atanh : ( ) => Di , AvgPool : ( ) => Mi , AvgPool3D : ( ) => mu , AvgPool3DGrad : ( ) => Wc , AvgPoolGrad : ( ) => zc , BackendWasm : ( ) => rD , BatchMatMul : ( ) => Oi , BatchToSpaceND : ( ) => fu , Bincount : ( ) => gu , BitwiseAn
` ),K=R=>U.writeSync(2,R+ `
` ));var Z=c.print||j,J=c.printErr||K;Object.assign(c,g),g=null,c.arguments&&(b=c.arguments),c.thisProgram&&(y=c.thisProgram),c.quit&&(x=c.quit);var ee=4,ae=Atomics.load,te=Atomics.store,se=Atomics.compareExchange,ie;c.wasmBinary&&(ie=c.wasmBinary);var xe=c.noExitRuntime||!0;typeof WebAssembly!="object"&&Gs("no native wasm support detected");var ue,ye,ke=!1,Se;function Le(R,q){R||Gs(q)}var Ge=typeof TextDecoder!="undefined"?new TextDecoder("utf8"):void 0;function mt(R,q,le){q>>>=0;for(var Ne=q+le,Me=q;R[Me]&&!(Me>=Ne);)++Me;if(Me-q>16&&R.buffer&&Ge)return Ge.decode(R.buffer instanceof SharedArrayBuffer?R.slice(q,Me):R.subarray(q,Me));for(var Fe="";q<Me;){var me=R[q++];if(!(me&128)){Fe+=String.fromCharCode(me);continue}var we=R[q++]&63;if((me&224)==192){Fe+=String.fromCharCode((me&31)<<6|we);continue}var Ct=R[q++]&63;if((me&240)==224?me=(me&15)<<12|we<<6|Ct:me=(me&7)<<18|we<<12|Ct<<6|R[q++]&63,me<65536)Fe+=String.fromCharCode(me);else{var pa=me-65536;Fe+=String.fromCharCode(55296|pa>>10,56320|pa&1023)}}return Fe}function st(R,q){return R>>>=0,R?mt(i(),R,q):""}function at(R,q,le,Ne){if(le>>>=0,!(Ne>0))return 0;for(var Me=le,Fe=le+Ne-1,me=0;me<R.length;++me){var we=R.charCodeAt(me);if(we>=55296&&we<=57343){var Ct=R.charCodeAt(++me);we=65536+((we&1023)<<10)|Ct&1023}if(we<=127){if(le>=Fe)break;q[le++>>>0]=we}else if(we<=2047){if(le+1>=Fe)break;q[le++>>>0]=192|we>>6,q[le++>>>0]=128|we&63}else if(we<=65535){if(le+2>=Fe)break;q[le++>>>0]=224|we>>12,q[le++>>>0]=128|we>>6&63,q[le++>>>0]=128|we&63}else{if(le+3>=Fe)break;q[le++>>>0]=240|we>>18,q[le++>>>0]=128|we>>12&63,q[le++>>>0]=128|we>>6&63,q[le++>>>0]=128|we&63}}return q[le>>>0]=0,le-Me}function rt(R,q,le){return at(R,i(),q,le)}var Re,gt,qn,Ot,oa,pn,An,la,Fn;C&&(Re=c.buffer);function ut(R){Re=R,c.HEAP8=gt=new Int8Array(R),c.HEAP16=Ot=new Int16Array(R),c.HEAP32=pn=new Int32Array(R),c.HEAPU8=qn=new Uint8Array(R),c.HEAPU16=oa=new Uint16Array(R),c.HEAPU32=An=new Uint32Array(R),c.HEAPF32=la=new Float32Array(R),c.HEAPF64=Fn=new Float64Array(R)}var $ n=c.INITIAL_MEMORY||16777216;if(C)ue=c.wasmMemory,Re=c.buffer;else if(c.wasmMemory)ue=c.wasmMemory;else if(ue=new WebAssembly.Memory({initial: $ n/65536,maximum:65536,shared:!0}),!(ue.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"),N&&J("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");ue&&(Re=ue.buffer), $ n=Re.byteLength,ut(Re);var jn,vr=[],Cl=[],Qa=[],qp=!1;function Na(){return xe}function jr(){if(c.preRun)for(typeof c.preRun=="function"&&(c.preRun=[c.preRun]);c.preRun.length;)Fg(c.preRun.shift());Kp(vr)}function Jt(){qp=!0,!C&&Kp(Cl)}function rh(){if(!C){if(c.postRun)for(typeof c.postRun=="function"&&(c.postRun=[c.postRun]);c.postRun.length;)nI(c.postRun.shift());Kp(Qa)}}function Fg(R){vr.unshift(R)}function $ g(R){Cl.unshift(R)}function nI(R){Qa.unshift(R)}var Kr=0,El=null,wr=null;function Dg(R){Kr++,c.monitorRunDependencies&&c.monitorRunDependencies(Kr)}function sh(R){if(Kr--,c.monitorRunDependencies&&c.monitorRunDependencies(Kr),Kr==0&&(El!==null&&(clearInterval(El),El=null),wr)){var q=wr;wr=null,q()}}function Gs(R){c.onAbort&&c.onAbort(R),R="Aborted("+R+")",J(R),ke=!0,Se=1,R+=". Build with -sASSERTIONS for more info.";var q=new WebAssembly.RuntimeError(R);throw m(q),q}var Rg="data:application/octet-stream;base64,";function ih(R){return R.startsWith(Rg)}function _l(R){return R.startsWith("file://")}var gn;gn="tfjs-backend-wasm-threaded-simd.wasm",ih(gn)||(gn=F(gn));function oh(R){try{if(R==gn&&ie)return new Uint8Array(ie);if(S)return S(R);throw"both async and sync fetching of the wasm failed"}catch(q){Gs(q)}}function Mg(){if(!ie&&(v||I)){if(typeof fetch=="function"&&!_l(gn))return fetch(gn,{credentials:"same-origin"}).then(function(R){if(!R.ok)throw"failed to load wasm binary file at '"+gn+"'";return R.arrayBuffer()}).catch(function(){return oh(
");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}};`}),QR=Vt((e,t)=>{var n=(()=>{var a=typeof document!=" undefined "&&document.currentScript?document.currentScript.src:void 0;return typeof __filename!=" undefined "&&(a=a||__filename),function(r){r=r||{};var s=typeof r!=" undefined "?r:{},i,o;s.ready=new Promise(function(Y,oe){i=Y,o=oe});var l;typeof process!=" undefined "&&process.listeners&&(l={uncaughtException:process.listeners(" uncaughtException "),unhandledRejection:process.listeners(" unhandledRejection ")});var u=Object.assign({},s),p=[],d=" . / this . program ",c=(Y,oe)=>{throw oe},h=typeof window==" object ",m=typeof importScripts==" function ",f=typeof process==" object "&&typeof process.versions==" object "&&typeof process.versions.node==" string ",g=" ";function b(Y){return s.locateFile?s.locateFile(Y,g):g+Y}var y,x,v,I;function N(Y){Y instanceof El||D(" exiting due to exception : "+Y)}if(f){var C=Av(),_=eN();m?g=_.dirname(g)+" / ":g=__dirname+" / ",y=(Y,oe)=>(Y=jr(Y)?new URL(Y):_.normalize(Y),C.readFileSync(Y,oe?void 0:" utf8 ")),v=Y=>{var oe=y(Y,!0);return oe.buffer||(oe=new Uint8Array(oe)),oe},x=(Y,oe,Ie)=>{Y=jr(Y)?new URL(Y):_.normalize(Y),C.readFile(Y,function(Ye,wt){Ye?Ie(Ye):oe(wt.buffer)})},process.argv.length>1&&(d=process.argv[1].replace(/\\/g," / ")),p=process.argv.slice(2),process.on(" uncaughtException ",function(Y){if(!(Y instanceof El))throw Y}),process.on(" unhandledRejection ",function(Y){throw Y}),c=(Y,oe)=>{if(qn())throw process.exitCode=Y,oe;N(oe),process.exit(Y)},s.inspect=function(){return" [ Emscripten Module object ] "}}else(h||m)&&(m?g=self.location.href:typeof document!=" undefined "&&document.currentScript&&(g=document.currentScript.src),a&&(g=a),g.indexOf(" blob : ")!==0?g=g.substr(0,g.replace(/[?#].*/," ").lastIndexOf(" / ")+1):g=" ",y=Y=>{var oe=new XMLHttpRequest;return oe.open(" GET ",Y,!1),oe.send(null),oe.responseText},m&&(v=Y=>{var oe=new XMLHttpRequest;return oe.open(" GET ",Y,!1),oe.responseType=" arraybuffer "
` )),l.join( `
` )}function UM(e,t,n,a){let r=ot(t),s=a[a.length-1],i=new Array(s).fill(0),o=t.length,l=n==="complex64"?ic(e):e;if(o>1)for(let u=0;u<r/s;u++){let p=u*s;for(let d=0;d<s;d++)i[d]=Math.max(i[d],sc(l[p+d],0,n).length)}return i}function sc(e,t,n){let a;return Array.isArray(e)?a= ` $ { parseFloat ( e [ 0 ] . toFixed ( dx ) ) } + $ { parseFloat ( e [ 1 ] . toFixed ( dx ) ) } j ` :ns(e)?a= ` '${e}' ` :n==="bool"?a=vN(e):a=parseFloat(e.toFixed(dx)).toString(),fc(a,t)}function vN(e){return e===0?"false":"true"}function jh(e,t,n,a,r,s=!0){let i=n==="complex64"?2:1,o=t[0],l=t.length;if(l===0){if(n==="complex64"){let f=ic(e);return[sc(f[0],0,n)]}return n==="bool"?[vN(e[0])]:[e[0].toString()]}if(l===1){if(o>bI){let f=ec*i,g=Array.from(e.slice(0,f)),b=Array.from(e.slice((o-ec)*i,o*i));return n==="complex64"&&(g=ic(g),b=ic(b)),["["+g.map((y,x)=>sc(y,r[x],n)).join(", ")+", ..., "+b.map((y,x)=>sc(y,r[o-ec+x],n)).join(", ")+"]"]}return["["+(n==="complex64"?ic(e):Array.from(e)).map((f,g)=>sc(f,r[g],n)).join(", ")+"]"]}let u=t.slice(1),p=a.slice(1),d=a[0]*i,c=[];if(o>bI){for(let f=0;f<ec;f++){let g=f*d,b=g+d;c.push(...jh(e.slice(g,b),u,n,p,r,!1))}c.push("...");for(let f=o-ec;f<o;f++){let g=f*d,b=g+d;c.push(...jh(e.slice(g,b),u,n,p,r,f===o-1))}}else for(let f=0;f<o;f++){let g=f*d,b=g+d;c.push(...jh(e.slice(g,b),u,n,p,r,f===o-1))}let h=l===2?",":"";c[0]="["+(o>0?c[0]+h:"");for(let f=1;f<c.length-1;f++)c[f]=" "+c[f]+h;let m= ` ,
` ;for(let f=2;f<l;f++)m+= `
` ;return c[c.length-1]=" "+c[c.length-1]+"]"+(s?"":m),c}function ic(e){let t=[];for(let n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}var Wt=class{constructor(e,t,n){if(this.dtype=t,this.shape=e.slice(),this.size=ot(e),n!=null){let a=n.length;A(a===this.size,()=> ` Length of values '${a}' 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||Fv(t,this.size),this.strides=lu(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 a of e){if(a<0||a>=this.shape[t]){let r= ` Requested out of range element at $ { e } . Buffer shape = $ { this . shape } ` ;throw new Error(r)}t++}let n=e[e.length-1];for(let a=0;a<e.length-1;++a)n+=this.strides[a]*e[a];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 za().makeTensor(this.values,this.shape,this.dtype)}},za=null,Pl=null,GM=null;function HM(e){za=e}function qM(e){Pl=e}function jM(e){GM=e}var Ce=class{constructor(e,t,n,a){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=ot(e),this.strides=lu(e),this.dataId=n,this.id=a,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){let e=await this.data();return Pl.buffer(this.shape,this.dtype,e)}bufferSync(){return Pl.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return Bl(this.shape,e,this.dtype==="complex64")}arraySync(){return Bl(this.shape,this.dataSync(),this.dtype==="complex64")}async data(){this.throwIfDisposed();let e=za().read(this.dataId);if(this.dtype==="string"){let t=await e;try{return t.map(n=>om(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(),za().readToGPU(this.dataId,e)}dataSync(){this.throwIfDisposed();let e=za().readSync(this.dataId);if(this.dtype==="string")try{return e.map(t=>om(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 za().read(this.dataId);return this.dtype==="string"?e:new Uint8Array(e.buffer)}dispose(){this.isDisposed||(this.kerasMask&&this.kerasMask.dispose(),za().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(e=!1){return Pl.print(this,e)}clone(){return this.throwIfDisposed(),Pl.clone(this)}toString(e=!1){let t=this.dataSync();return VM(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Pl.cast(this,e)}variable(e=!0,t,n){return this.throwIfDisposed(),za().makeVariable(this,e,t,n)}};Object.defineProperty(Ce,Symbol.hasInstance,{value:e=>!!e&&e.data!=null&&e.dataSync!=null&&e.throwIfDisposed!=null});function Q(){return Rv("Tensor",()=>Ce)}Q();var cs=class extends Ce{constructor(e,t,n,a){super(e.shape,e.dtype,e.dataId,a),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(! $ r(e.shape,this.shape))throw new Error( ` shape of the new value ( $ { e . shape } ) and previous value ( $ { this . shape } ) must match ` );za().disposeTensor(this),this.dataId=e.dataId,za().incRef(this,null)}dispose(){za().disposeVariabl
with dtype $ { s . dtype } . ` )}),n.length===1)return or(n[0]);let a=n,r={axis:t};return P.runKernel(yu,a,r)}var et=L({concat_:SP});function NP(e,t,n=!1,a=!1){let r=E(e,"a","matMul"),s=E(t,"b","matMul");[r,s]=At(r,s);let i={a:r,b:s},o={transposeA:n,transposeB:a};return P.runKernel(Oi,i,o)}var $ e=L({matMul_:NP});function TP(e){let t={x:E(e,"x","sigmoid","float32")};return P.runKernel(Po,t)}var ma=L({sigmoid_:TP});function CP(e,t,n){let a=E(e,"x","slice","string_or_numeric");if(a.rank===0)throw new Error("Slicing scalar is not possible");let r={x:a},s={begin:t,size:n};return P.runKernel(ep,r,s)}var Ve=L({slice_:CP});function EP(e){let t={x:E(e,"x","tanh","float32")};return P.runKernel(Ho,t)}var fi=L({tanh_:EP});function _P(e,t,n,a,r,s){let i=E(e,"forgetBias","basicLSTMCell"),o=E(t,"lstmKernel","basicLSTMCell"),l=E(n,"lstmBias","basicLSTMCell"),u=E(a,"data","basicLSTMCell"),p=E(r,"c","basicLSTMCell"),d=E(s,"h","basicLSTMCell"),c=et([u,d],1),h= $ e(c,o),m=X(h,l),f=m.shape[0],g=m.shape[1]/4,b=[f,g],y=Ve(m,[0,0],b),x=Ve(m,[0,g],b),v=Ve(m,[0,g*2],b),I=Ve(m,[0,g*3],b),N=X(z(ma(y),fi(x)),z(p,ma(X(i,v)))),C=z(fi(N),ma(I));return[N,C]}var qN=L({basicLSTMCell_:_P});function AP(e,t,n){let a=E(e,"x","batchToSpaceND"),r=t.reduce((o,l)=>o*l);A(a.rank>=1+t.length,()=> ` input rank is $ { a . 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(a.shape[0]%r===0,()=> ` input tensor batch is $ { a . shape [ 0 ] } but is not divisible by the product of the elements of blockShape $ { t . join ( " * " ) } === $ { r } ` );let s={x:a},i={blockShape:t,crops:n};return P.runKernel(fu,s,i)}var dd=L({batchToSpaceND_:AP});function FP(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 $ P(e,t,n,a,r,s){s==null&&(s=.001);let i=E(e,"x","batchNorm"),o=E(t,"mean","batchNorm"),l=E(n,"variance","batchNorm"),u;r!=null&&(u=E(r,"scale","batchNorm"));let p;a!=null&&(p=E(a,"offset","batchNorm")),A(o.rank===l.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),A(p==null||o.rank===p.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),A(u==null||o.rank===u.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let d={x:FP(i),scale:u,offset:p,mean:o,variance:l},c={varianceEpsilon:s},h=P.runKernel(eo,d,c);return W(h,i.shape)}var Es=L({batchNorm_: $ P});function DP(e,t,n,a,r,s){let i=E(e,"x","batchNorm"),o=E(t,"mean","batchNorm"),l=E(n,"variance","batchNorm"),u;r!=null&&(u=E(r,"scale","batchNorm"));let p;return a!=null&&(p=E(a,"offset","batchNorm")),A(i.rank===2,()=> ` Error in batchNorm2D : x must be rank 2 but got rank $ { i . rank } . ` ),A(o.rank===2||o.rank===1,()=> ` Error in batchNorm2D : mean must be rank 2 or rank 1 but got rank $ { o . rank } . ` ),A(l.rank===2||l.rank===1,()=> ` Error in batchNorm2D : variance must be rank 2 or rank 1 but got rank $ { l . rank } . ` ),u!=null&&A(u.rank===2||u.rank===1,()=> ` Error in batchNorm2D : scale must be rank 2 or rank 1 but got rank $ { u . rank } . ` ),p!=null&&A(p.rank===2||p.rank===1,()=> ` Error in batchNorm2D : offset must be rank 2 or rank 1 but got rank $ { p . rank } . ` ),Es(i,o,l,p,u,s)}var ew=L({batchNorm2d_:DP});function RP(e,t,n,a,r,s){let i=E(e,"x","batchNorm"),o=E(t,"mean","batchNorm"),l=E(n,"variance","batchNorm"),u;r!=null&&(u=E(r,"scale","batchNorm"));let p;return a!=null&&(p=E(a,"offset","batchNorm")),A(i.rank===3,()=> ` Error in batchNorm3D : x must be rank 3 but got rank $ { i . rank } . ` ),A(o.rank===3||o.rank===1,()=> ` Error in batchNorm3D : mean must be rank 3 or rank 1 but got rank $ { o . rank } . ` ),A(l.rank===3||l.rank===1,()=> ` Error in batchNorm3D : variance must be rank 3 or rank 1 but got rank $ { l . rank } . ` ),u!=null&&A(u.rank===3||u.rank===1,()=> ` Error in batchNorm3D : scale must be rank 3 or rank 1 but got rank $ { u . rank } . ` ),p!=null&&A(p.rank===3||p.rank===1,()=> ` Error in batchNorm3D : offset must be rank 3 or rank 1 but got rank $ { p . rank } . ` ),Es(i,o,l,
$ { r } and $ { t } for depthToSpace with input shape
$ { a . shape } ` ),A(s*t>=0,()=> ` Negative dimension size caused by overflow when multiplying
$ { s } and $ { t } for depthToSpace with input shape
$ { a . shape } ` ),A(i%(t*t)===0,()=> ` Dimension size must be evenly divisible by $ { t * t } but is $ { i } for depthToSpace with input shape $ { a . shape } ` );let o={x:a},l={blockSize:t,dataFormat:n};return P.runKernel(Iu,o,l)}var dw=L({depthToSpace_:r3});function s3(e,t,n,a,r="NHWC",s=[1,1],i){let o=E(e,"x","depthwiseConv2d","float32"),l=E(t,"filter","depthwiseConv2d","float32"),u=o,p=!1;o.rank===3&&(p=!0,u=W(o,[1,o.shape[0],o.shape[1],o.shape[2]])),A(u.rank===4,()=> ` Error in depthwiseConv2d : input must be rank 4 , but got rank $ { u . rank } . ` ),A(l.rank===4,()=> ` Error in depthwiseConv2d : filter must be rank 4 , but got rank $ { l . rank } . ` );let d=r==="NHWC"?u.shape[3]:u.shape[1];A(d===l.shape[2],()=> ` Error in depthwiseConv2d : number of input channels ( $ { d } ) must match the inChannels dimension in filter $ { l . shape [ 2 ] } . ` ),Nn("depthwiseConv2d",a,i);let c={x:u,filter:l},h={strides:n,pad:a,dataFormat:r,dilations:s,dimRoundingMode:i},m=P.runKernel(Hi,c,h);return p?W(m,[m.shape[1],m.shape[2],m.shape[3]]):m}var _s=L({depthwiseConv2d_:s3});function i3(e){let t={x:E(e,"x","diag")};return P.runKernel(Gc,t)}var YN=L({diag_:i3});function o3(e,t,n,a,r=[1,1],s="NHWC"){let i=E(e,"x","dilation2d"),o=E(t,"filter","dilation2d");A(i.rank===3||i.rank===4,()=> ` Error in dilation2d : input must be rank 3 or 4 , but got rank $ { i . rank } . ` ),A(o.rank===3,()=> ` Error in dilation2d : filter must be rank 3 , but got rank $ { o . rank } . ` ),A(s==="NHWC",()=> ` Error in dilation2d : Only NHWC is currently supported , but got dataFormat of $ { s } ` );let l=i,u=!1;i.rank===3&&(l=W(i,[1,i.shape[0],i.shape[1],i.shape[2]]),u=!0),A(l.shape[3]===o.shape[2],()=> ` Error in dilation2d : input and filter must have the same depth : $ { l . shape [ 3 ] } vs $ { o . shape [ 2 ] } ` );let p={x:l,filter:o},d={strides:n,pad:a,dilations:r},c=P.runKernel(qi,p,d);return u?W(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var hw=L({dilation2d_:o3}),cp={};_e(cp,{assertAndGetBroadcastShape:()=>ct,getBroadcastDims:()=>ZN,getReductionAxes:()=>Bt});function ZN(e,t){let n=e.length,a=[];for(let r=0;r<n;r++){let s=n-1-r,i=e[s]||1;(t[t.length-1-r]||1)>1&&i===1&&a.unshift(s)}return a}function Bt(e,t){let n=[];for(let a=0;a<t.length;a++){let r=e[e.length-a-1],s=t.length-a-1,i=t[s];(r==null||r===1&&i>1)&&n.unshift(s)}return n}function ct(e,t){let n=Math.max(e.length,t.length),a=new Array(n);for(let r=0;r<n;r++){let s=e[e.length-r-1];s==null&&(s=1);let i=t[t.length-r-1];if(i==null&&(i=1),s===1)a[n-r-1]=i;else if(i===1)a[n-r-1]=s;else if(s!==i){let o= ` Operands could not be broadcast together with shapes $ { e } and $ { t } . ` ;throw Error(o)}else a[n-r-1]=s}return a}function l3(e,t){let n=E(e,"a","equal","string_or_numeric"),a=E(t,"b","equal","string_or_numeric");[n,a]=At(n,a),ct(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(Nu,r)}var ea=L({equal_:l3});function u3(e,t,n){let a=E(t,"a","where"),r=E(n,"b","where"),s=E(e,"condition","where","bool"),i=ct(ct(s.shape,a.shape),r.shape),o=ri(s,i),l=ri(a,i),u=ri(r,i),p={condition:o,t:l,e:u};return P.runKernel(Qu,p)}var nn=L({where_:u3});function p3(e){let t={x:E(e,"x","zerosLike")};return P.runKernel(up,t)}var je=L({zerosLike_:p3});function c3(e,t){let n=E(e,"a","div"),a=E(t,"b","div");[n,a]=At(n,a);let r=he(n,a),s=je(r),i=ea(a,s);return nn(i,s,r)}var mw=L({divNoNan_:c3});function d3(e,t){let n=E(e,"t1","dot"),a=E(t,"t2","dot");A((n.rank===1||n.rank===2)&&(a.rank===1||a.rank===2),()=> ` Error in dot : inputs must all be rank 1 or 2 , but got ranks $ { n . rank } and $ { a . rank } . ` );let r=n.rank===1?n.size:n.shape[1],s=a.rank===1?a.size:a.shape[0];if(A(r===s,()=> ` Error in dot : inner dimensions of inputs must match , but got $ { r } and $ { s } . ` ),n.rank===1&&a.rank===1){let i=W(n,[1,-1]),o=W(a,[-1,1]),l= $ e(i,o);return W(l,[])}else if(n.rank===1&&a.rank===2){let i=W(n,[1,-1]),o=W(a,[a.shape[0],a.shape[1]]),l= $ e(i,o);return W(l,[l.size])}else if(n.rank===2&&a.rank===1){let i=W(a,[-1,1]),o= $ e(n,i);return W(o,[o.size])}else{let i=W(a,[a.shape[0],a.shape[1]]);return $ e(n,i)}}var fw=L({dot_:d3});function h3(e,...t){let n=t.map((r,s)=>E(r, ` tensors$ { s } ` ,"einsum")),a={equation:e};return P.runKernel(Wm,n,a)}var Js=L({einsum_:h3});function m3(e
rank $ { s . rank } . ` ),A(Hl(t),()=> ` Error in localResponseNormalization : depthRadius must be an integer but got depthRadius $ { t } . ` );let i=s,o=!1;s.rank===3&&(o=!0,i=W(s,[1,s.shape[0],s.shape[1],s.shape[2]]));let l={x:i},u={depthRadius:t,bias:n,alpha:a,beta:r},p=P.runKernel(uo,l,u);return o?W(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var Sw=L({localResponseNormalization_:U3});function G3(e){let t={x:E(e,"x","log","float32")};return P.runKernel(oo,t)}var ta=L({log_:G3});function H3(e){let t={x:E(e,"x","log1p")};return P.runKernel(lo,t)}var gd=L({log1p_:H3});function q3(e){return A(us(e),()=>"The f passed in grad(f) must be a function"),(t,n)=>{let a=E(t,"x","tf.grad","string_or_numeric"),r=n!=null?E(n,"dy","tf.grad"):null;return P.tidy(()=>{let{value:s,grads:i}=P.gradients(()=>e(a),[a],r);return r!=null&&Sn(s.shape,r.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),rf(i),i[0]})}}function j3(e){return A(us(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 a=Ic(t,"args","tf.grads","string_or_numeric"),r=n!=null?E(n,"dy","tf.grads"):null;return P.tidy(()=>{let{value:s,grads:i}=P.gradients(()=>e(...a),a,r);return r!=null&&Sn(s.shape,r.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),rf(i),i})}}function K3(e){return A(us(e),()=>"The f passed in valueAndGrad(f) must be a function"),(t,n)=>{A(t instanceof Ce,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"),A(n==null||n instanceof Ce,()=>"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");let{grads:a,value:r}=P.gradients(()=>e(t),[t],n);return rf(a),{grad:a[0],value:r}}}function X3(e){return A(us(e),()=>"The f passed in valueAndGrads(f) must be a function"),(t,n)=>{A(Array.isArray(t)&&t.every(r=>r instanceof Ce),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"),A(n==null||n instanceof Ce,()=>"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");let a=P.gradients(()=>e(...t),t,n);return n!=null&&Sn(a.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),rf(a.grads),a}}function rT(e,t){A(us(e),()=>"The f passed in variableGrads(f) must be a function"),A(t==null||Array.isArray(t)&&t.every(u=>u instanceof cs),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let n=t!=null;if(!n){t=[];for(let u in P.registeredVariables)t.push(P.registeredVariables[u])}let a=n?t.filter(u=>!u.trainable):null,r=t.length;t=t.filter(u=>u.trainable),A(t.length>0,()=> ` variableGrads ( ) expects at least one of the input variables to be trainable , but none of the $ { r } variables is trainable . ` );let s=!0,{value:i,grads:o}=P.gradients(e,t,null,s);A(o.some(u=>u!=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(i.rank===0,()=> ` The f passed in variableGrads ( f ) must return a scalar , but it returned a rank - $ { i . rank } tensor ` );let l={};return t.forEach((u,p)=>{o[p]!=null&&(l[u.name]=o[p])}),a!=null&&a.forEach(u=>l[u.name]=null),{value:i,grads:l}}function cr(e){return P.customGrad(e)}function rf(e){if(e.filter(t=>t==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 Y3(e){let t={x:E(e,"x","neg")};return P.runKernel(Wu,t)}var yt=L({neg_:Y3});function Z3(e){let t={x:E(e,"x","softplus")};return P.runKernel(Lo,t)}var qo=L({softplus_:Z3});function J3(e){let t=E(e,"x","logSigmoid");return cr(n=>({value:yt(qo(yt(n))),gradFunc:a=>z(a,ma(yt(n)))}))(t)}var Nw=L({logSigmoid_:J3});function Q3(e,t){let n=E(e,"a","sub"),a=E(t,"b","sub");[n,a]=At(n,a);let r={a:n,b:a};return P.runKernel(Uo,r)}var pe=L({sub_:Q3});function eL(e,t=-1){let n=E(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 cr((a,r)=>{let s=fa(a,t,!0),i=pe(a,s),o=pe(re(i,"float32"),ta(fe(mn(i),t,!0)));return r([o]),{value:o,gradFunc:(l,u)=>{let[p]=u,d=!0,c=mn(p);return pe(l,z(fe(l,t,d),c))}}})(n)}var sf=L({logSoftmax_:eL});function tL(e,t=null,n=!1){let a=E(e,"x","logSumExp"),r= $ a(t,a.shape),s=fa(a,r,!0),i=pe(a,s),o=mn(i),l=fe(o,r),u=ta(l),p=X(W(s,u.shape),u);if(n){let d=gi(p.shape,r);return W(p,d)}return p}var bd=L({logSumExp_:tL});function nL(e,t){let n=E(e,"a","logicalAnd","bool"),a=E(t,"b","logicalAnd","bool");ct(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(Ru,r)}var Fa=L({logicalAnd_:nL});function aL(e){let t={x:E(e,"x","logicalNot","bool")};return P.runKernel(Mu,t)}var yd=L({logicalNot_:aL});function rL(e,t){let n=E(e,"a","logicalOr","bool"),a=E(t,"b","logicalOr","bool");ct(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(Ou,r)}var of=L({logicalOr_:rL});function sL(e,t){let n=E(e,"a","logicalXor","bool"),a=E(t,"b","logicalXor","bool");return ct(n.shape,a.shape),Fa(of(e,t),yd(Fa(e,t)))}var Tw=L({logicalXor_:sL}),Rh=2147483648;function iL(e,t,n="left"){let a=E(e,"sortedSequence","searchSorted"),r=E(t,"values","searchSorted"),s=a.shape[a.shape.length-1],i=r.shape[r.shape.length-1],o=W(a,[-1,s]),l=W(r,[-1,i]);if(o.rank<2)throw new Error("Sorted input argument must be at least 2-dimensional");if(o.shape[0]!==l.shape[0])throw new Error("Leading dimension of 'sortedSequence' and 'values' must match.");if(ot(l.shape)>=Rh)throw new Error( ` values tensor size must less than $ { Rh } ` );if(o.shape[1]>=Rh)throw new Error( ` trailing dim _size must less than $ { Rh } for int32 output type , was $ { o . shape [ 1 ] } ` );let u={sortedSequence:o,values:l},p={side:n};return P.runKernel(Ju,u,p)}var lf=L({searchSorted_:iL});function sT(e,t){return lf(e,t,"left")}function oL(e,t,n,a,r){let s=E(e,"x","maxPool"),i=1,o=s,l=!1;s.rank===3&&(l=!0,o=W(s,[1,s.shape[0],s.shape[1],s.shape[2]])),A(o.rank===4,()=> ` Error in maxPool : input must be rank 4 but got rank $ { o . rank } . ` ),A(hr(n,i),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { n } and dilations '${i}' ` ),Nn("maxPool",a,r);let u={x:o},p={filterSize:t,strides:n,pad:a,dimRoundingMode:r},d=P.runKernel(ho,u,p);return l?W(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var Dt=L({maxPool_:oL});function lL(e,t=[1,1,1],n,a,r,s="NDHWC"){let i=E(e,"x","maxPool3d"),o=i,l=!1;i.rank===4&&(l=!0,o=W(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])),A(o.rank===5,()=> ` Error in maxPool3d : x must be rank 5 but got rank $ { o . rank } . ` ),A(s==="NDHWC",()=> ` Error in maxPool3d : Only NDHWC is currently supported , but got dataFormat of $ { s } ` ),Nn("maxPool3d",a,r);let u={x:o},p={filterSize:t,strides:n,pad:a,dimRoundingMode:r,dataFormat:s},d=P.runKernel(Lu,u,p);return l?W(d,[d.shape[1],d.shape[2],d.shape[3],d.shape[4]]):d}var Cw=L({maxPool3d_:lL});function uL(e,t,n,a,r=!1){let s={x:E(e,"x","maxPoolWithArgmax")},i={filterSize:t,strides:n,pad:a,includeBatchInIndex:r},o=P.runKernel(Kc,s,i);return{result:o[0],indexes:o[1]}}var iT=L({maxPoolWithArgmax_:uL});function pL(e,t){let n=E(e,"a","maximum"),a=E(t,"b","maximum");[n,a]=At(n,a),n.dtype==="bool"&&(n=re(n,"int32"),a=re(a,"int32")),ct(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(co,r)}var mr=L({maximum_:pL});function cL(e,t=null,n=!1){let a={x:E(e,"x","mean")},r={axis:t,keepDims:n};return P.runKernel(mo,a,r)}var Et=L({mean_:cL});function It(e,t="float32"){
Actual : $ { r } .
Expected : $ { s } . ` );for(let i=0;i<s.length;++i){let o=r[i],l=s[i];if(!n(o,l))throw new Error( ` Arrays differ : actual [ $ { i } ] = $ { o } , expected [ $ { i } ] = $ { l } .
Actual : $ { r } .
Expected : $ { s } . ` )}typeof expect!="undefined"&&expect().nothing()}function zL(e,t){e().then(()=>t.fail(),()=>t()),typeof expect!="undefined"&&expect().nothing()}function WL(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return ns(e)||ns(e[0])||ns(t)||ns(t[0])?Bx(e,n,(a,r)=>a==r):Bx(e,t,(a,r)=>Rw(a,r,0))}function BL(e,t,n){if(n==null&&(n=Dw()),!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 VL(e,t,n){for(let a=0;a<e.length;a++)if(e[a]<t||e[a]>n)throw new Error( ` Value out of range : $ { e [ a ] } low : $ { t } , high : $ { n } ` )}function UL(e,t){let n=new Float32Array(e),a=new Float32Array(t);if(n.length!==a.length)throw new Error( ` Expected ArrayBuffer to be of length $ { a . length } , but it was $ { n . length } ` );for(let r=0;r<a.length;r++)if(n[r]!==a[r])throw new Error( ` Expected ArrayBuffer value at $ { r } to be $ { a [ r ] } but got $ { n [ r ] } instead ` )}function wT(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?wT(n):e[t]=ld(n)}return e}function GL(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",a=>n(t)),t.load()})}async function HL(e){await e.play(),"requestVideoFrameCallback"in e&&await new Promise(t=>{e.requestVideoFrameCallback(t)})}var Mw=class{constructor(e,t,n,a,r){this.mean=e,this.stdDev=t,this.dtype=n,this.nextVal=NaN,this.truncated=a,this.truncated&&(this.upper=this.mean+this.stdDev*2,this.lower=this.mean-this.stdDev*2);let s=r||Math.random();this.random= $ w.alea(s.toString())}nextValue(){if(!isNaN(this.nextVal)){let a=this.nextVal;return this.nextVal=NaN,a}let e,t,n=!1;for(;!n;){let a,r,s;do a=2*this.random()-1,r=2*this.random()-1,s=a*a+r*r;while(s>=1||s===0);let i=Math.sqrt(-2*Math.log(s)/s);e=this.mean+this.stdDev*a*i,t=this.mean+this.stdDev*r*i,(!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}},qL=class{constructor(e,t,n,a){this.alpha=e,this.beta=1/t,this.dtype=n;let r=a||Math.random();this.randu= $ w.alea(r.toString()),this.randn=new Mw(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,a,r,s;for(;;){do a=this.randn.nextValue(),s=1+this.c*a;while(s<=0);if(s*=s*s,e=a*a,t=1-.331*e*e,n=.5*e+this.d*(1-s+Math.log(s)),r=this.randu(),r<t||Math.log(r)<n)break}return s=1/this.beta*this.d*s,this.alpha<1&&(s*=Math.pow(this.randu(),1/this.alpha)),this.convertValue(s)}convertValue(e){return this.dtype==="float32"?e:Math.round(e)}},jL=class{constructor(e=0,t=1,n,a){if(this.canReturnFloat=()=>this.dtype==null||this.dtype==="float32",this.min=e,this.range=t-e,this.dtype=n,a==null&&(a=Math.random()),typeof a=="number"&&(a=a.toString()),!this.canReturnFloat()&&this.range<=1)throw new Error( ` The difference between $ { e } - $ { t } <= 1 and dtype is not float ` );this.random= $ w.alea(a)}convertValue(e){return this.canReturnFloat()?e:Math.round(e)}nextValue(){return this.convertValue(this.min+this.range*this.random())}};function KL(e,t,n=1,a="float32",r){if(ra(e),n==null&&(n=1),a==null&&(a="float32"),a!=="float32"&&a!=="int32")throw new Error( ` Unsupported data type $ { a } ` );let s=new qL(t,n,a,r),i=Pe(e,a);for(let o=0;o<i.values.length;o++)i.values[o]=s.nextValue();return i.toTensor()}var kT=L({randomGamma_:KL});function XL(e,t=0,n=1,a,r){if(ra(e),a!=null&&a==="bool")throw new Error( ` Unsupported data type $ { a } ` );let s=new Mw(t,n,a,!1,r),i=Pe(e,a);for(let o=0;o<i.values.length;o++)i.values[o]=s.nextValue();return i.toTensor()}var uf=L({randomNormal_:XL});function YL(e,t,n){if(t!=null&&t==="bool")throw new Error( ` Unsupported data type $ { t } ` );return uf(e,0,1,t,n)}va
$ { r . shape } ` );if(s.rank!==1)throw new Error( ` Values should be Tensor1D but received shape $ { s . shape } ` );if(i.rank!==1)throw new Error( ` Dense shape should be Tensor1D but received shape $ { i . shape } ` );if(o.rank!==0)throw new Error( ` Default value should be a scalar but received shape $ { o . shape } ` );let l={indices:r,values:s,denseShape:i,defaultValue:o},u=P.runKernel(Yc,l);return{outputIndices:u[0],outputValues:u[1],emptyRowIndicator:u[2],reverseIndexMap:u[3]}}var bB=L({sparseFillEmptyRows_:gB});function yB(e,t,n){let a=E(e,"inputIndices","sparseReshape","int32"),r=E(t,"inputShape","sparseReshape","int32"),s=E(n,"newShape","sparseReshape","int32");if(a.rank!==2)throw new Error( ` Input indices should be Tensor2D but received shape
$ { a . shape } ` );if(r.rank!==1)throw new Error( ` Input shape should be Tensor1D but received shape $ { r . shape } ` );if(s.rank!==1)throw new Error( ` New shape should be Tensor1D but received shape $ { s . shape } ` );let i={inputIndices:a,inputShape:r,newShape:s},o=P.runKernel(ap,i);return{outputIndices:o[0],outputShape:o[1]}}var xB=L({sparseReshape_:yB});function vB(e,t,n){let a=E(e,"data","sparseSegmentMean"),r=E(t,"indices","sparseSegmentMean","int32"),s=E(n,"segmentIds","sparseSegmentMean","int32");if(a.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.rank!==1)throw new Error( ` Indices should be Tensor1D but received shape
$ { r . shape } ` );if(s.rank!==1)throw new Error( ` Segment ids should be Tensor1D but received shape
$ { s . shape } ` );let i={data:a,indices:r,segmentIds:s};return P.runKernel(Zc,i)}var wB=L({sparseSegmentMean_:vB});function kB(e,t,n){let a=E(e,"data","sparseSegmentSum"),r=E(t,"indices","sparseSegmentSum","int32"),s=E(n,"segmentIds","sparseSegmentSum","int32");if(a.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.rank!==1)throw new Error( ` Indices should be Tensor1D but received shape
$ { r . shape } ` );if(s.rank!==1)throw new Error( ` Segment ids should be Tensor1D but received shape
$ { s . shape } ` );let i={data:a,indices:r,segmentIds:s};return P.runKernel(Jc,i)}var IB=L({sparseSegmentSum_:kB});function SB(e,t,n,a,r,s,i,o){let l=E(e,"data","stringNGrams","string");if(l.dtype!=="string")throw new Error("Data must be of datatype string");if(l.shape.length!==1)throw new Error( ` Data must be a vector , saw : $ { l . shape } ` );let u=E(t,"dataSplits","stringNGrams");if(u.dtype!=="int32")throw new Error("Data splits must be of datatype int32");let p={separator:n,nGramWidths:a,leftPad:r,rightPad:s,padWidth:i,preserveShortSequences:o},d={data:l,dataSplits:u},c=P.runKernel(td,d,p);return{nGrams:c[0],nGramsSplits:c[1]}}var NB=L({stringNGrams_:SB});function TB(e,t,n=!0){let a=E(e,"input","stringSplit","string"),r=E(t,"delimiter","stringSplit","string");if(a.rank!==1)throw new Error( ` Input should be Tensor1D but received shape $ { a . shape } ` );if(r.rank!==0)throw new Error( ` Delimiter should be a scalar but received shape $ { r . shape } ` );let s={skipEmpty:n},i={input:a,delimiter:r},o=P.runKernel(nd,i,s);return{indices:o[0],values:o[1],shape:o[2]}}var CB=L({stringSplit_:TB});function EB(e,t){let n=E(e,"input","stringToHashBucketFast","string"),a={numBuckets:t};if(t<=0)throw new Error("Number of buckets must be at least 1");let r={input:n};return P.runKernel(ad,r,a)}var _B=L({stringToHashBucketFast_:EB});function AB(e,t,n,a=!0){let r=E(e,"input","staticRegexReplace","string"),s={pattern:t,rewrite:n,replaceGlobal:a};return P.runKernel(ed,{x:r},s)}var FB=L({staticRegexReplace_:AB}),ZT={fft:Id,ifft:eu,rfft:Sd,irfft:bf},JT={hammingWindow:sW,hannWindow:GT,frame:HT,stft:uW},Qn={flipLeftRight:hW,grayscaleToRGB:fW,resizeNearestNeighbor:YT,resizeBilinear:XT,rgbToGrayscale:bW,rotateWithOffset:xW,cropAndResize:cW,nonMaxSuppression:wW,nonMaxSuppressionAsync:_W,nonMaxSuppressionWithScore:FW,nonMaxSuppressionWithScoreAsync:DW,nonMaxSuppressionPadded:MW,nonMaxSuppressionPaddedAsync:PW,threshold:VW,transform:GW},Xw={bandPart:qW,gramSchmidt:KW,qr:YW},QT={absoluteDifference:QW,computeWeightedLoss:Mr,cosineDistance:tB,hingeLoss:aB,huberLoss:sB,logLoss:oB,meanSquaredError:uB,sigmoidCrossEntropy:dB,softmaxCrossEntropy:fB},e2={sparseFillEmptyRows:bB,sparseReshape:xB,sparseSegmentMean:wB,sparseSegmentSum:IB},t2={stringNGrams:NB,stringSplit:CB,stringToHashBucketFast:_B,staticRegexReplace:FB},ne={};_e(ne,{Serializable:()=>n2,SerializationMap:()=>a2,getRegisteredName:()=>DB,registerClass:()=>r2});var $ B=new Map,Vx=new Map,n2=class{getClassName(){return this.constructor.className}static fromConfig(e,t){return new e(t)}},a2=class zl{constructor(){this.classNameMap={}}static getMap(){return zl.instance==null&&(zl.instance=new zl),zl.instance}static register(t){zl.getMap().classNameMap[t.className]=[t,t.fromConfig]}};function r2(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 a=n,r=t+">"+a;return a2.register(e), $ B.set(r,e),Vx.set(e,r),e}function DB(e){return Vx.has(e)?Vx.get(e):e.className}var Or=class extends n2{minimize(e,t=!1,n){let{value:a,grads:r}=this.computeGradients(e,n);if(n!=null){let s=n.map(i=>({name:i.name,tensor:r[i.name]}));this.applyGradients(s)}else this.applyGradients(r);return Ee(r),t?a:(a.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(e,t){return rT(e,t)}dispose(){this.iterations_!=null&&Ee(this.iterations_)}async saveIterations(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:ve(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 : $ { o . join ( ", " ) } . ` )}let l=r.reduce((h,m,f)=>(m&&h.push(f),h),[]),u=[];l.forEach(h=>{t[h].paths.forEach(m=>{let f=n+(n.endsWith("/")?"":"/")+m;u.push(f)})});let p=await e(u),d={},c=0;return l.forEach(h=>{let m=t[h].paths.length,f=new Dr(p.slice(c,c+m));s[h].forEach(g=>{let b=f.slice(g.groupOffset,g.groupOffset+g.sizeBytes),y=AN(b,[g.manifestEntry]);for(let x in y)d[x]=y[x]}),c+=m}),d}}var HB="application/octet-stream",qB="application/json",n0=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}],a=DN(e,n);if(t.body.append("model.json",new Blob([JSON.stringify(a)],{type:qB}),"model.json"),e.weightData!=null){let s=Dr.join(e.weightData);t.body.append("model.weights.bin",new Blob([s],{type:HB}),"model.weights.bin")}let r=await this.fetch(this.path,t);if(r.ok)return{modelArtifactsInfo:pd(e),responses:[r]};throw new Error( ` BrowserHTTPRequest . save ( ) failed due to HTTP response status $ { r . 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(r){let s= ` Failed to parse model JSON of response from $ { this . path } . ` ;throw this.path.endsWith(".pb")?s+=" 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.":s+=" Please make sure the server is serving valid JSON for this request.",new Error(s)}let n=t.modelTopology,a=t.weightsManifest;if(n==null&&a==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 Vv(e,t=>this.loadWeights(t))}async loadStream(){let e=await this.loadModelJSON(),t=await this.getWeightUrls(e.weightsManifest),n=Ox(e.weightsManifest),a=()=>UB(t,this.loadOptions);return Object.assign(Object.assign({},e),{weightSpecs:n,getWeightStream:a})}async getWeightUrls(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,a]=jB(t),r=this.weightPathPrefix||n,s=[],i=[];for(let o of e)for(let l of o.paths)this.weightUrlConverter!=null?i.push(this.weightUrlConverter(l)):s.push(r+l+a);return this.weightUrlConverter&&s.push(...await Promise.all(i)),s}async loadWeights(e){let t=await this.getWeightUrls(e),n=Ox(e),a=await s2(t,this.loadOptions);return[n,a]}};n0.URL_SCHEME_REGEX=/^https?: \/ \/ /;function jB(e){let t=e.lastIndexOf("/"),n=e.lastIndexOf("?"),a=e.substring(0,t),r=n>t?e.substring(n):"";return[a+"/",r]}function Gx(e){return e.match(n0.URL_SCHEME_REGEX)!=null}var o2=(e,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let n=!0;if(Array.isArray(e)?n=e.every(a=>Gx(a)
indices . shape [ 0 ] = $ { e } ` }function aV(e,t){return ` indices ( $ { e } , 0 ) is invalid : $ { t } < 0 ` }function rV(e,t,n){return ` indices ( $ { e } , 0 ) is invalid : $ { t } >= $ { n } ` }function sV(e,t){return ` only one output dimension may be - 1 , not both $ { e } and $ { t } ` }function iV(e,t){return ` size $ { e } must be non - negative , not $ { t } ` }function oV(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function lV(e,t){let n=ot(e),a=ot(t);return ` Input to reshape is a SparseTensor with $ { n }
dense values , but the requested shape requires a multiple of $ { a } . inputShape = $ { e } outputShape = $ { t } ` }function uV(e,t){let n=ot(e),a=ot(t);return ` Input to reshape is a tensor with $ { n } dense values , but the requested shape has $ { a } . inputShape = $ { e } outputShape = $ { t } ` }function pV(){return"segment ids must be >= 0"}function cV(){return"segment ids are not increasing"}function dV(e,t){return ` Segment id $ { e } out of range [ 0 , $ { t } ) , possibly because segmentIds input is not sorted . ` }function hV(e,t,n){return ` Bad : indices [ $ { e } ] == $ { t } out of range [ 0 , $ { n } ) ` }var T2={};_e(T2,{collectGatherOpShapeInfo:()=>gV,computeOutShape:()=>fV,segOpComputeOptimalWindowSize:()=>mV});function mV(e,t){let n=!1,a;for(e<=i0?(a=e,n=!0):a=rm(e,Math.floor(Math.sqrt(e)));!n;)a>t||a===e?n=!0:a=rm(e,a+1);return a}function fV(e,t,n){let a=[],r=e.length;for(let s=0;s<r;s++)s!==t?a.push(e[s]):a.push(n);return a}function gV(e,t,n,a){let r=t.shape.length,s=e.shape.length;if(a!==0&&(a<-r||a>r))throw new Error( ` Expect batchDims in the range of [ - $ { r } , $ { r } ] , but got $ { a } ` );if(a<0&&(a+=r),a>s)throw new Error( ` batchDims ( $ { a } ) must be less than rank ( x ) (
$ { s } ) . ` );if(n<a)throw new Error( ` batchDims ( $ { a } ) must be less than or equal to axis ( $ { n } ) . ` );for(let d=0;d<a;++d)if(e.shape[d]!==t.shape[d])throw new Error( ` x . shape [ $ { d } ] : $ { e . shape [ d ] } should be equal to indices . shape [ $ { d } ] : $ { t . shape [ d ] } . ` );let i=e.shape[n],o=[],l=1,u=1,p=1;for(let d=0;d<a;++d)o.push(e.shape[d]),l*=e.shape[d];for(let d=a;d<n;d++)o.push(e.shape[d]),u*=e.shape[d];for(let d=a;d<r;d++)o.push(t.shape[d]);for(let d=n+1;d<s;d++)o.push(e.shape[d]),p*=e.shape[d];return{batchSize:l,sliceSize:p,outerSize:u,dimSize:i,outputShape:o}}function bV(e){try{return e.map(t=>om(t))}catch(t){throw new Error( ` Failed to decode encoded string bytes into utf - 8 , error : $ { t } ` )}}function yV(e){return e.map(t=>ld(t))}var fr={};_e(fr,{nonMaxSuppressionV3Impl:()=>qT,nonMaxSuppressionV4Impl:()=>jT,nonMaxSuppressionV5Impl:()=>KT,whereImpl:()=>MT});MB();var C2={kernelName:uu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>z(e,Ko(re(n,"float32"),-1))}}},xV={kernelName:Ci,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let a=pt(re(n,"float32")),r=hn(pe(ve(1),a));return yt(he(e,r))}}}},vV={kernelName:Ei,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let a=hn(pe(pt(re(n,"float32")),1));return he(e,a)}}}},wV={kernelName:Is,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,a]=t,r=ct(n.shape,a.shape);return{a:()=>{let s=e,i=Bt(n.shape,r);return i.length>0&&(s=fe(s,i)),W(s,n.shape)},b:()=>{let s=e,i=Bt(a.shape,r);return i.length>0&&(s=fe(s,i)),W(s,a.shape)}}}},kV={kernelName:_i,saveAllInputs:!0,gradFunc:(e,t)=>{let n={};return t.forEach((a,r)=>{n[r]=()=>e.clone()}),n}},IV={kernelName:du,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>je(n)}}},SV={kernelName:hu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>je(n)}}},NV={kernelName:Ai,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>he(e,hn(pe(ve(1),pt(re(n,"float32")))))}}},TV={kernelName:Fi,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let a=hn(X(ve(1),pt(re(n,"float32"))));return he(e,a)}}}},CV={kernelName:Ri,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,a]=t,r=ct(n.shape,a.shape);return{a:()=>{let s=X(pt(n),pt(a)),i=z(e,he(a,s)),o=Bt(n.shape,r);return o.length>0&&(i=fe(i,o)),W(i,n.shape)},b:()=>{let s=X(pt(n),pt(a)),i=yt(z(e,he(n,s))),o=Bt(a.shape,r);return o.length>0&&(i=fe(i,o)),W(i,a.shape)}}}},EV={kernelName: $ i,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>he(e,X(pt(re(n,"float32")),1))}}},_V={kernelName:Di,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>he(e,pe(ve(1),pt(re(n,"float32"))))}}};function AV(e,t,n,a,r,s){let i=E(e,"dy","avgPool3dGrad"),o=E(t,"input","avgPool3dGrad"),l=i,u=o,p=!1;o.rank===4&&(p=!0,l=W(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]]),u=W(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]])),A(l.rank===5,()=> ` Error in avgPool3dGrad : dy must be rank 5 but got rank $ { l . rank } . ` ),A(u.rank===5,()=> ` Error in avgPool3dGrad : input must be rank 5 but got rank $ { u . rank } . ` ),Nn("avgPool3dGrad",r,s);let d={dy:l,input:u},c={filterSize:n,strides:a,pad:r,dimRoundingMode:s},h=P.runKernel(Wc,d,c);return p?W(h,[h.shape[1],h.shape[2],h.shape[3],h.shape[4]]):h}var FV=L({avgPool3dGrad_:AV}), $ V={kernelName:mu,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[a]=t,{filterSize:r,strides:s,pad:i,dimRoundingMode:o}=n;return{x:()=>FV(e,a,r,s,i,o)}}};function DV(e,t,n,a,r){let s=E(e,"dy","avgPoolGrad"),i=E(t,"input","avgPoolGrad");A(i.rank===s.rank,()=> ` Rank of input ( $ { i . rank } ) does not match rank of dy ( $ { s . rank } ) ` );let o=i,l=s,u=!1;i.rank===3&&(u=!0,o=W(i,[1,i.shape[0],i.shape[1],i.shape[2]]),l=W(s,[1,s.shape[0],s.shape[1],s.shape[2]])),A(l.rank===4,()=> ` Error in avgPoolGrad : dy must be rank 4 but got rank $ { l . rank } . ` ),A(o.rank===4,()=> ` Error in avgPoolGrad : input must be rank 4 but got rank $ { o . rank } . ` );let p={dy:l,input:o},d={filterSize:n,strides:a,pad:r},c=P.runKernel(zc,p,d);return u?W(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var RV=L({avgPoolGrad_:DV}),MV={kernelName:Mi,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[a]=t,{filterSize:r,strides:s,pad:i}=n;return{x:()=>RV(e,a,r,s,i)}}},OV={kernelName
1. The $ { a } is defined in Python , in which case it needs to be ported to TensorFlow . js or your JavaScript code .
2. The custom $ { a } is defined in JavaScript , but is not registered properly with tf . serialization . registerClass ( ) . ` );return i}else{let s=e;if(s.className==null||s.config==null)throw new V( ` $ { a } : Improper config format : $ { JSON . stringify ( s ) } .
'className' and 'config' must set . ` );let i=s.className,o,l;if(i in n?[o,l]=n[i]:i in Ta?[o,l]=Ta.className:i in t&&([o,l]=t[i]),o==null)throw new V( ` Unknown $ { a } : $ { i } . This may be due to one of the following reasons :
1. The $ { a } is defined in Python , in which case it needs to be ported to TensorFlow . js or your JavaScript code .
2. The custom $ { a } is defined in JavaScript , but is not registered properly with tf . serialization . registerClass ( ) . ` );if(l!=null){let u={};for(let h of Object.keys(Ta))u[h]=Ta[h];for(let h of Object.keys(n))u[h]=n[h];let p=s.config;p.customObjects=u;let d=Object.assign({},Ta);for(let h of Object.keys(n))Ta[h]=n[h];qx(s.config);let c=l(o,s.config,n,r);return Ta=Object.assign({},d),c}else{let u=Object.assign({},Ta);for(let d of Object.keys(n))Ta[d]=n[d];let p=new o(s.config);return Ta=Object.assign({},u),p}}}function SG(e,t){return e<t?-1:e>t?1:0}function Mh(e,t){return-1*SG(e,t)}function os(e){if(e==null)return e;let t=[];for(let n of e)t.indexOf(n)===-1&&t.push(n);return t}function NG(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 Yo(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 l0(e,t,n=0,a=1/0){return ar(n>=0),ar(a>=n),Array.isArray(e)&&e.length>=n&&e.length<=a&&e.every(r=>typeof r===t)}function tn(e,t){Array.isArray(e)?(w.assert(e.length>0,()=> ` $ { t } is unexpectedly an empty array . ` ),e.forEach((n,a)=>tn(n, ` element $ { a + 1 } of $ { t } ` ))):w.assert(Number.isInteger(e)&&e>0,()=> ` Expected $ { t } to be a positive integer , but got $ { M2 ( e ) } . ` )}function M2(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>M2(t)).join(",")+"]":typeof e=="string"? ` "${e}" ` : ` $ { e } ` }function TG(e,t,n){let a=n!=null?n():w.now(),r;return(...s)=>{let i=n!=null?n():w.now();return i-a<t||(a=i,r=e(...s)),r}}function O2(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}var CG=0;function P2(){return CG++}var Oh={};function _f(e=""){return e in Oh||(Oh[e]=0),Oh[e]+=1,e+Oh[e].toString()}var EG=["channelsFirst","channelsLast"],_G=["nearest","bilinear"],AG=["valid","same","causal"],FG=["max","avg"], $ G=["sum","mul","concat","ave"],Ml=new Map;function Rt(e){Yo(EG,"DataFormat",e)}function DG(e){Yo(_G,"InterpolationFormat",e)}function wa(e){Yo(AG,"PaddingMode",e)}function L2(e){Yo(FG,"PoolMode",e)}var gc=[],LI="/";function si(e,t){gc.push(e);try{let n=t();return gc.pop(),n}catch(n){throw gc.pop(),n}}function RG(){return gc.length===0?"":gc.join(LI)+LI}function z2(e){if(!B2(e))throw new Error("Not a valid tensor name: '"+e+"'");return RG()+e}function W2(e){if(!B2(e))throw new Error("Not a valid tensor name: '"+e+"'");Ml.has(e)||Ml.set(e,0);let t=Ml.get(e);if(Ml.set(e,Ml.get(e)+1),t>0){let n= ` $ { e } _$ { t } ` ;return Ml.set(n,1),n}else return e}var MG=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9 \. _ \/ ]* $ /);function B2(e){return!!e.match(MG)}function OG(e){return e===parseInt(e.toString(),10)}function ls(e,t,n){t==null&&(t=0),n==null&&(n=e.length);let a=1;for(let r=t;r<n;++r)a*=e[r];return a}function nu(e){if(e.length===0)return Number.NaN;let t=Number.POSITIVE_INFINITY;for(let n=0;n<e.length;n++){let a=e[n];a<t&&(t=a)}return t}function gs(e){if(e.length===0)return Number.NaN;let t=Number.NEGATIVE_INFINITY;for(let n=0;n<e.length;n++){let a=e[n];a>t&&(t=a)}return t}function Ua(e,t){if(t<e)throw new V( ` end ( $ { t } ) < begin ( $ { e } ) is forbidden . ` );let n=[];for(let a=e;a<t;++a)n.push(a);return n}var bx;function jt(){return bx==null&&(bx=Wv().epsilon()),bx}function Ga(){return"channelsLast"}function lr(e,t){return re(e,t)}function Cd(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 PG(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=Cd(e,1);return jx(n,[1,t,1])})}function LG(e){let t=[ls(e.shape)];return W(e,t)}function zG(e){if(e.rank<=1)throw new V( ` batchFlatten requires a minimum rank of 2. Got rank : $ { e . rank } . ` );let t=[e.shape[0],ls(e.shape,1)];return W(e,t)}function ii(e,t,n){return O(()=>{switch(e.rank){case 1:return kd(e,t,n);case 2:return gf(e,[t,0],[n,e.shape[1]]);case 3:return jo(e,[t,0,0],[n,e.shape[1],e.shape[2]]);case 4:return Ql(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 a=fa(e),r=Xl(e),s=Tn(this.numTokens,a).bufferSync().get(0),i=Rr(r,0).bufferSync().get(0);if(!(s&&i))throw new V( ` Input values must be between 0 < values <= numTokens with numTokens = $ { this . numTokens } ` );return O6(e,this.outputMode,this.numTokens,n)})}};F1.className="CategoryEncoding";ne.registerClass(F1);var P6=["bilinear","nearest"],nS=new Set(P6), $ 1=class extends We{constructor(e){if(super(e),this.height=e.height,this.width=e.width,e.interpolation)if(nS.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=Je(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 Qn.resizeBilinear(e,n,!this.cropToAspectRatio);if(this.interpolation==="nearest")return Qn.resizeNearestNeighbor(e,n,!this.cropToAspectRatio);throw new Error( ` Interpolation is $ { this . interpolation } but only $ { [ ... nS ] } are supported ` )})}}; $ 1.className="Resizing";ne.registerClass( $ 1);var KC=class{constructor(e){this.seed=e}next(){if(this.seed!==void 0)return this.seed++}};KC.className="RandomSeed";var XC=class extends We{constructor(e){super(e),this.randomGenerator=new KC(e.seed)}getConfig(){let e={seed:this.randomGenerator.seed},t=super.getConfig();return Object.assign(e,t),e}};XC.className="BaseRandomLayer";var L6=["bilinear","nearest"],aS=new Set(L6),D1=class extends XC{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:56:53 +01:00
` );if(n)if(aS.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=Je(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 a=n.shape[n.shape.length-2];this.widthFactor=Fs([1],1+this.widthLower,1+this.widthUpper,"float32",this.randomGenerator.next());let r=this.widthFactor.dataSync()[0]*a;r=Math.round(r);let s=[this.imgHeight,r];switch(this.interpolation){case"bilinear":return Qn.resizeBilinear(e,s);case"nearest":return Qn.resizeNearestNeighbor(e,s);default:throw new Error( ` Interpolation is $ { this . interpolation }
but only $ { [ ... aS ] } are supported ` )}})}};D1.className="RandomWidth";ne.registerClass(D1);function z6(e){return new bp(e)}function W6(e){return new R0(e)}function B6(e){return new F0(e)}function V6(e){return new $ 0(e)}function U6(e){return new D0(e)}function G6(e){return new O0(e)}function H6(e){return new M0(e)}function q6(e){return new B0(e)}function j6(e){return new Uf(e)}function K6(e){return new L0(e)}function X6(e){return new Gf(e)}function Y6(e){return new z0(e)}function Z6(e){return new W0(e)}function J6(e){return new V0(e)}function Q6(e){return new U0(e)}function eq(e){return new G0(e)}function tq(e){return new J0(e)}function nq(e){return new Y0(e)}function aq(e){return new Xf(e)}function rq(e){return new X0(e)}function sq(e){return new Z0(e)}function iq(e){return new Q0(e)}function oq(e){return new e1(e)}function lq(e){return new t1(e)}function uq(e){return new a1(e)}function pq(e){return new r1(e)}function cq(e){return new i1(e)}function dq(e){return new u1(e)}function hq(e){return new o1(e)}function mq(e){return new l1(e)}function fq(e){return new s1(e)}function gq(e){return new p1(e)}function bq(e){return new m1(e)}function yq(e){return new f1(e)}function xq(e){return new g1(e)}function R1(e){return new y1(e)}function vq(e){return R1(e)}function wq(e){return R1(e)}function M1(e){return new v1(e)}function kq(e){return M1(e)}function Iq(e){return M1(e)}function O1(e){return new k1(e)}function Sq(e){return O1(e)}function Nq(e){return O1(e)}function Tq(e){return new I1(e)}function Cq(e){return new N1(e)}function YC(e){return new S1(e)}function ZC(e){return new T1(e)}function JC(e){return new b1(e)}function QC(e){return new x1(e)}function Eq(e){return new w1(e)}function _q(e){return new q0(e)}function Aq(e){return new qf(e)}function Fq(e){return new j0(e)}function $ q(e){return new Dd(e)}function Dq(e){return new H0(e)}function Rq(e){return new Hf(e)}function Mq(e){return new K0(e)}function Oq(e){return new Kf(e)}function Pq(e){return new Pr(e)}function Lq(e){return new jf(e)}function zq(e){return new E1(e)}function Wq(e){return new C1(e)}var Bq=YC,Vq=ZC,Uq=JC,Gq=QC;function Hq(e){return new c1(e)}function qq(e){return new d1(e)}function jq(e){return new h1(e)}function Kq(e){return new n1(e)}function Xq(e){return new _1(e)}function Yq(e){return new A1(e)}function Zq(e){return new $ 1(e)}function Jq(e){return new F1(e)}function Qq(e){return new D1(e)}var eE={};_e(eE,{MAPE:()=>pj,MSE:()=>hj,binaryAccuracy:()=>ej,binaryCrossentropy:()=>tj,categoricalAccuracy:()=>aj,categoricalCrossentropy:()=>rj,cosineProximity:()=>oj,mape:()=>cj,meanAbsoluteError:()=>lj,meanAbsolutePercentageError:()=>uj,meanSquaredError:()=>dj,mse:()=>mj,precision:()=>sj,recall:()=>ij,sparseCategoricalAccuracy:()=>nj});function ej(e,t){return N0(e,t)}function tj(e,t){return sC(e,t)}function nj(e,t){return iC(e,t)}function aj(e,t){return T0(e,t)}function rj(e,t){return C0(e,t)}function sj(e,t){return rC(e,t)}function ij(e,t){return LH(e,t)}function oj(e,t){return S0(e,t)}function lj(e,t){return zf(e,t)}function uj(e,t){return yp(e,t)}function pj(e,t){return yp(e,t)}function cj(e,t){return yp(e,t)}function dj(e,t){return Zo(e,t)}function hj(e,t){return Zo(e,t)}function mj(e,t){return Zo(e,t)}var tE={};_e(tE,{modelFromJSON:()=>m6});var nE={};_e(nE,{l1:()=>gj,l1l2:()=>fj,l2:()=>bj});function fj(e){return new Fd(e)}function gj(e){return w6(e)}function bj(e){return k6(e)}var aE=class extends au{constructor(){super(...arguments),this.model=null}setModel(e){if(!(e instanceof _r))throw new Error("model must be a LayersModel, not some other Container");this.model=e}};function zh(e,t){return e<t}function rS(e,t){return e>t}var rE=class extends aE{constructor(e){if(super(),e==null&&(e={}),e.restoreBestWeights)throw new ze("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),_a(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,a)=>this.write(n,t[a]))}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 a=0;a<this.size();a++)e.push(a)}if(e.length===0)return bn([],[0].concat(this.elementShape));let n=this.readMany(e);return _a(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),Ft(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 bn([],[0].concat(this.elementShape));let t=[];for(let a=0;a<this.size();a++)t.push(a);let n=this.readMany(t);return _a(this.elementShape,n[0].shape, ` TensorArray shape mismatch : tensor array shape ( $ { this . elementShape } ) vs first tensor shape ( $ { n [ 0 ] . shape } ) ` ),et(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,dt(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,a=e.map(o=>(n+=o,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:56:53 +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 r=n===0?0:t.size/n,s=[];O(()=>{t=W(t,[1,n,r]);for(let o=0;o<e.length;++o){let l=[0,o===0?0:a[o-1],0],u=[1,e[o],r];s[o]=W(Ve(t,l,u),this.elementShape)}return s});let i=[];for(let o=0;o<e.length;o++)i[o]=o;this.writeMany(i,s)}},Zf=class hv{get id(){return this.idTensor.id}constructor(t,n,a,r=-1){this.tensors=t,this.elementShape=n,this.elementDtype=a,t!=null&&t.forEach(s=>{if(a!==s.dtype)throw new Error( ` Invalid data types ; op elements $ { a } , but list elements $ { s . dtype } ` );_a(n,s.shape,"TensorList shape mismatch: "),Ht(s)}),this.idTensor=ve(0),this.maxNumElements=r,Ht(this.idTensor)}copy(){return new hv([...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,a=-1){if(n!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { n } , but list elements $ { this . elementDtype } ` );if(a!==-1&&this.tensors.length!==a)throw new Error( ` Operation expected a list with $ { a } elements but got a list with $ { this . tensors . length } elements . ` );_a(t,this.elementShape,"TensorList shape mismatch: ");let r=ac(this.elementShape,this.tensors,t);return O(()=>{let s=this.tensors.map(i=>W(i,r));return Ft(s,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 a=ac(this.elementShape,this.tensors,t),r=this.tensors.pop();return r.kept=!1,_a(r.shape,t,"TensorList shape mismatch: "),W(r,a)}pushBack(t){if(t.dtype!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { t . dtype } , but list elements $ { this . elementDtype } ` );if(_a(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 hv([],this.elementShape,this.elementDtype,this.maxNumElements);n.tensors.length=t;for(let a=0;a<Math.min(this.tensors.length,t);++a)n.tensors[a]=this.tensors[a];return n}getItem(t,n,a){if(a!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { a } , 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 . ` );_a(this.tensors[t].shape,n,"TensorList shape mismatch: ");let r=ac(this.elementShape,this.tensors,n);return W(this.tensors[t],r)}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 . ` );_a(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,a){if(n!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { n } , but list elements $ { this . elementDtype } ` );_a(this.elementShape,a,"TensorList shape mismatch: "),t=t.slice(0,this.size());let r=ac(this.elementShape,this.tensors,a);return t.length===0?bn([],[0].concat(r)):O(()=>{let s=t.map(i=>W(this.tensors[i],r));return Ft(s,0)})}concat(t,n){if(t&&t!==this.elementDtype)throw new Error( ` TensorList dtype is $ { this . elementDtype } but concat requested dtype $ { t } ` );_a(this.elementShape,n,"TensorList shape mismatch: ");let a=ac(th
2021-09-11 17:11:38 +02:00
tensor . shape [ 0 ] , but sum of lengths is
2024-01-17 22:56:53 +01:00
$ { a } , and tensor ' s shape is : $ { e . shape } ` );let s=e.shape.slice(1),i=dv(s,n),o=a===0?0:e.size/a,l=O(()=>{let p=[];e=W(e,[1,a,o]);for(let d=0;d<t.length;++d){let c=[0,d===0?0:r[d-1],0],h=[1,t[d],o];p[d]=W(Ve(e,c,h),i)}return e.dispose(),p}),u=new Zf([],n,e.dtype,t.length);for(let p=0;p<l.length;p++)u.setItem(p,l[p]);return u}var Zj=async(e,t,n)=>{switch(e.op){case"If":case"StatelessIf":{let a=k("thenBranch",e,t,n),r=k("elseBranch",e,t,n),s=k("cond",e,t,n),i=k("args",e,t,n);return(await s.data())[0]?n.functionMap[a].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap):n.functionMap[r].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap)}case"While":case"StatelessWhile":{let a=k("body",e,t,n),r=k("cond",e,t,n),s=k("args",e,t,n),i=await n.functionMap[r].executeFunctionAsync(s,n.tensorArrayMap,n.tensorListMap),o=s.map(p=>p.id),l=await i[0].data();i.forEach(p=>{!p.kept&&o.indexOf(p.id)===-1&&p.dispose()});let u=s;for(;l[0];){let p=u;u=await n.functionMap[a].executeFunctionAsync(u,n.tensorArrayMap,n.tensorListMap);let d=u.map(h=>h.id);p.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&d.indexOf(h.id)===-1&&h.dispose()});let c=await n.functionMap[r].executeFunctionAsync(u,n.tensorArrayMap,n.tensorListMap);l=await c[0].data(),c.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&d.indexOf(h.id)===-1&&h.dispose()})}return u}case"LoopCond":{let a=k("pred",e,t,n);return[Tr(a)]}case"Switch":{let a=k("pred",e,t,n),r=k("data",e,t,n);return r.kept||(r=Tr(r)),(await a.data())[0]?[void 0,r]:[r,void 0]}case"Merge":{let a=e.inputNames.find(r=>cn(r,t,n)!==void 0);if(a){let r=cn(a,t,n);return[Tr(r)]}return}case"Enter":{let a=k("frameName",e,t,n),r=k("tensor",e,t,n);return n.enterFrame(a),[Tr(r)]}case"Exit":{let a=k("tensor",e,t,n);return n.exitFrame(),[Tr(a)]}case"NextIteration":{let a=k("tensor",e,t,n);return n.nextIteration(),[Tr(a)]}case"TensorArrayV3":{let a=k("size",e,t,n),r=k("dtype",e,t,n),s=k("elementShape",e,t,n),i=k("dynamicSize",e,t,n),o=k("clearAfterRead",e,t,n),l=k("identicalElementShapes",e,t,n),u=k("name",e,t,n),p=new qj(u,r,a,s,l,i,o);return n.addTensorArray(p),[p.idTensor,ve(1)]}case"TensorArrayWriteV3":{let a=k("tensorArrayId",e,t,n),r=k("index",e,t,n),s=k("tensor",e,t,n),i=n.getTensorArray(a.id);return i.write(r,s),[i.idTensor]}case"TensorArrayReadV3":{let a=k("tensorArrayId",e,t,n),r=k("index",e,t,n);return[n.getTensorArray(a.id).read(r)]}case"TensorArrayGatherV3":{let a=k("tensorArrayId",e,t,n),r=k("indices",e,t,n),s=k("dtype",e,t,n);return[n.getTensorArray(a.id).gather(r,s)]}case"TensorArrayScatterV3":{let a=k("tensorArrayId",e,t,n),r=k("indices",e,t,n),s=k("tensor",e,t,n),i=n.getTensorArray(a.id);return i.scatter(r,s),[i.idTensor]}case"TensorArrayConcatV3":{let a=k("tensorArrayId",e,t,n),r=n.getTensorArray(a.id),s=k("dtype",e,t,n);return[r.concat(s)]}case"TensorArraySplitV3":{let a=k("tensorArrayId",e,t,n),r=k("tensor",e,t,n),s=k("lengths",e,t,n),i=n.getTensorArray(a.id);return i.split(s,r),[i.idTensor]}case"TensorArraySizeV3":{let a=k("tensorArrayId",e,t,n),r=n.getTensorArray(a.id);return[ve(r.size(),"int32")]}case"TensorArrayCloseV3":{let a=k("tensorArrayId",e,t,n),r=n.getTensorArray(a.id);return r.clearAndClose(),[r.idTensor]}case"TensorListSetItem":{let a=k("tensorListId",e,t,n),r=k("index",e,t,n),s=k("tensor",e,t,n),i=n.getTensorList(a.id);return i.setItem(r,s),[i.idTensor]}case"TensorListGetItem":{let a=k("tensorListId",e,t,n),r=k("index",e,t,n),s=k("elementShape",e,t,n),i=k("elementDType",e,t,n);return[n.getTensorList(a.id).getItem(r,s,i)]}case"TensorListScatterV2":case"TensorListScatter":{let a=k("indices",e,t,n),r=k("tensor",e,t,n),s=k("elementShape",e,t,n),i=k("numElements",e,t,n),o=Xj(r,a,s,i);return n.addTensorList(o),[o.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let a=k("elementShape",e,t,n),r=k("elementDType",e,t,n),s;e.op==="TensorListReserve"?s="numElements":s="maxNumElements";let i=k(s,e,t,n),o=e.op==="TensorListReserve"?-1:i,l=Kj(a,r,i,o);return n.addTensorList(l),[l.idTensor]}case"TensorListGather":{let a=k("tensorListId",e,t,n),r=k("indices",e,t,n),s=k("elementShape",e,t,n),i=k("element
$ { e } ` );let a;return this.size===1/0||this.size==null?a=this.size:t?a=Math.ceil(this.size/e):a=Math.floor(this.size/e),Yn(async()=>(await n.iterator()).columnMajorBatch(e,t,nK),a)}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,Yn(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,Yn(async()=>(await t.iterator()).filter(a=>O(()=>e(a))),n)}async forEachAsync(e){return(await this.iterator()).forEachAsync(e)}map(e){let t=this;return Yn(async()=>(await t.iterator()).map(n=>O(()=>e(n))),this.size)}mapAsync(e){let t=this;return Yn(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 Yn(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,Yn(async()=>{let a=W1(async()=>({value:await t.iterator(),done:!1}));return W5(a.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,Yn(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 a = this , r = $5 . alea ( t || w . now ( ) . toString ( ) ) ; return Yn ( async ( ) => { let s = r . int32 ( ) ; return n && ( s += r . int32 ( ) ) , ( await a . iterator ( ) ) . shuffle ( e , s . 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 , Yn ( 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 ( ) } } ; xp . MAX _BUFFER _SIZE = 1e4 ; function Yn ( e , t = null ) { return new class extends xp { constructor ( ) { super ( ... arguments ) , this . size = t } async iterator ( ) { return e ( ) } } } function eK ( e ) { return Yn ( async ( ) => ME ( e ) , e . length ) } function tK ( e ) { if ( ! ru ( 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 Yn ( async ( ) => { let n = await FE ( e , a => { if ( a instanceof xp ) return { value : a . iterator ( ) , recurse : ! 1 } ; if ( ru ( a ) ) return { value : null , recurse : ! 0 } ; throw new Error ( "Leaves of the structure passed to zip() must be Datasets, not primitives." ) } ) ; return B5 ( n , rs . SHORTEST ) } , t ) } function nK ( e ) { if ( e === null ) return null ; let t = e [ 0 ] ; return O5 ( t ) ? { value : aK ( e ) , recurse : ! 1 } : { value : null , recurse : ! 0 } } function aK ( e ) { if ( e . length === 0 ) throw new Error ( "Can't make a batch of zero elements." ) ; return e [ 0 ] instanceof Ce ? Ft ( e ) : bn ( e ) } var LE = class extends xp { constructor ( e ) { super ( ) , this . input = e } async iterator ( ) { return ( await this . input . iterator ( ) ) . decodeUTF8 ( ) . split ( `
` ).map(e=>(e.endsWith(" \r ")&&(e=e.slice(0,-1)),e))}},Bh='"',rc=Symbol("out"),gS=Symbol("field"),Vh=Symbol("quote"),Sx=Symbol("quoteafterquote"),bS=Symbol("quoteinquote"),zE=class extends xp{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((a,r)=>(a[r]=a[r]+1||1,a),{}),n=Object.keys(t).filter(a=>t[a]>1);if(w.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let a of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(a)===-1)throw new Error('The key "'+a+'" provided in columnConfigs does not match any of the column names ('+this.fullColumnNames.toString()+").")}this.columnNamesValidated=!0}async maybeReadHeaderLine(){if(this.hasHeader){let e=await(await this.base.iterator()).next();if(e.done)throw new Error("No data was found for CSV parsing.");let t=e.value;return this.parseRow(t,!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 LE(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={},a={};for(let r=0;r<this.fullColumnNames.length;r++){let s=this.fullColumnNames[r],i=this.columnConfigs?this.columnConfigs[s]:null;if(!(this.configuredColumnsOnly&&!i)){let o=t[r],l=null;if(o==="")if(i&&i.default!==void 0)l=i.default;else{if(i&&(i.required||i.isLabel))throw new Error( ` Required column $ { s } is empty in this line : $ { e } ` );l=void 0}else{let u=Number(o);if(isNaN(u))i&&i.dtype==="bool"?l=this.getBoolean(o):l=o;else if(!i||!i.dtype)l=u;else switch(i.dtype){case"float32":l=u;break;case"int32":l=Math.floor(u);break;case"bool":l=this.getBoolean(o);break;default:l=u}}i&&i.isLabel?a[s]=l:n[s]=l}}return Object.keys(a).length===0?n:{xs:n,ys:a}}getBoolean(e){return e==="1"||e.toLowerCase()==="true"?1:0}parseRow(e,t=!0){let n=[],a=0,r=e.length,s=rc;for(let i=0;i<r;i++)switch(s){case rc:switch(e.charAt(i)){case Bh:a=i+1,s=Vh;break;case this.delimiter:if(a=i+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),s=rc;break;default:s=gS,a=i;break}break;case gS:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(a,i)),s=rc,a=i+1;break;default:}break;case Vh:switch(e.charAt(i)){case Bh:s=Sx;break;default:}break;case Sx:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(a,i-1)),s=rc,a=i+1;break;case Bh:s=Vh;break;default:s=bS;break}break;case bS:switch(e.charAt(i)){case Bh:s=Vh;break;default:}break;default:}if(s===Sx?n.push(e.substring(a,r-1)):n.push(e.substring(a)),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}},rK=class WE extends rn{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:56:53 +01:00
=== === === === === === === === === = ` ));let r={id:this.nextDataId()};return this.data.set(r,{values:t,dtype:a,refCount:1}),r}makeTensorInfo(t,n,a){let r;if(n==="string"&&a!=null&&a.length>0&&w.isString(a[0])){let s=a.map(i=>w.encodeString(i));r=this.write(s,t,n)}else r=this.write(a,t,n);return{dataId:r,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,a,r,s){this.data.set(t,{values:n,dtype:r,refCount:s})}numDataIds(){return this.data.numDataIds()}async read(t){return this.readSync(t)}readSync(t){let{dtype:n,complexTensorInfos:a}=this.data.get(t);if(n==="complex64"){let r=this.readSync(a.real.dataId),s=this.readSync(a.imag.dataId);return T.mergeRealAndImagArrays(r,s)}return w.convertBackendValuesAndArrayBuffer(this.data.get(t).values,n)}bufferSync(t){let n=this.readSync(t.dataId);if(t.dtype==="string")try{let a=n.map(r=>w.decodeString(r));return Pe(t.shape,t.dtype,a)}catch(a){throw new Error("Failed to decode encoded string bytes into utf-8")}return Pe(t.shape,t.dtype,n)}makeOutput(t,n,a){return Ea().makeTensorFromTensorInfo(this.makeTensorInfo(n,a,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:a}=this.data.get(t);a!=null&&(this.disposeData(a.real.dataId,!0),this.disposeData(a.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){ge([t],"where");let n=this.readSync(t.dataId);return xK(t.shape,n)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}};V1.nextDataId=0;var U1={};_e(U1,{addImpl:()=>ZE,bincountImpl:()=>H1,bincountReduceImpl:()=>JE,bitwiseAndImpl:()=>QE,castImpl:()=>YE,ceilImpl:()=>e_,concatImpl:()=>q1,equalImpl:()=>t_,expImpl:()=>a_,expm1Impl:()=>s_,floorDivImpl:()=>o_,floorImpl:()=>i_,gatherNdImpl:()=>l_,gatherV2Impl:()=>u_,greaterEqualImpl:()=>c_,greaterImpl:()=>p_,lessEqualImpl:()=>h_,lessImpl:()=>d_,linSpaceImpl:()=>m_,logImpl:()=>f_,maxImpl:()=>g_,maximumImpl:()=>b_,minimumImpl:()=>y_,multiplyImpl:()=>j1,negImpl:()=>x_,notEqualImpl:()=>v_,prodImpl:()=>w_,raggedGatherImpl:()=>k_,raggedRangeImpl:()=>I_,raggedTensorToTensorImpl:()=>S_,rangeImpl:()=>X1,rsqrtImpl:()=>N_,scatterImpl:()=>ai,sigmoidImpl:()=>y8,simpleAbsImpl:()=>XE,sliceImpl:()=>Im,sparseFillEmptyRowsImpl:()=>C_,sparseReshapeImpl:()=>E_,sparseSegmentReductionImpl:()=>Y1,sqrtImpl:()=>w8,squaredDifferenceImpl:()=>__,staticRegexReplaceImpl:()=>A_,stridedSliceImpl:()=>F_,stringNGramsImpl:()=>Z1,stringSplitImpl:()=>J1,stringToHashBucketFastImpl:()=>Q1,subImpl:()=> $ _,tileImpl:()=>D_,topKImpl:()=>M_,transposeImpl:()=>K1,uniqueImpl:()=>tk});function XE(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var vK=e=>{let{x:t}=e.inputs,n=e.backend;ge(t,"abs");let a=new Float32Array(w.sizeFromShape(t.shape)),r=n.data.get(t.dataId).values;return a=XE(r),n.makeOutput(a,t.shape,t.dtype)},wK={kernelName:uu,backendName:"cpu",kernelFunc:vK};function Mt(e){return(t,n,a,r,s)=>{let i=T.assertAndGetBroadcastShape(t,n),o=i.length,l=w.computeStrides(i),u=w.sizeFromShape(i),p=w.getTypedArrayFromDType(s,u),d=t.length,c=n.length,h=w.computeStrides(t),m=w.computeStrides(n),f=T.getBroadcastDims(t,i),g=T.getBroadcastDims(n,i);if(f.length+g.length===0)for(let b=0;b<p.length;++b)p[b]=e(a[b%a.length],r[b%r.length]);else for(let b=0;b<p.length;++b){let y=w.indexToLoc(b,o,l),x=y.slice(-d);f.forEach(C=>x[C]=0);let v=w.locToIndex(x,d,h),I=y.slice(-c);g.forEach(C=>I[C]=0);let N=w.locToIndex(I,c,m);p[b]=e(a[v],r[N])}return[p,i]}}function Jn(e){let{inputs:t,backend:n}=e,{real:a,imag:r}=t,s=n.data.get(a.dataId).values,i=n.data.get(r.dataId).values,o=n.makeTensorInfo(a.shape,"complex64"),l=n.data.get(o.dataId);return l.
$ { s . shape } ` );if(a.shape.length!==2)throw new Error( ` Indices must be a matrix , saw :
$ { a . shape } ` );if(r.shape.length!==1)throw new Error( ` Values must be a vector , saw :
$ { r . shape } ` );if(i.shape.length!==0)throw new Error( ` Default value must be a scalar , saw :
$ { i . shape } ` );let o=n.data.get(a.dataId).values,l=n.data.get(r.dataId).values,u=n.data.get(s.dataId).values,p=n.data.get(i.dataId).values[0],[d,c,h,m,f]=C_(o,a.shape,a.dtype,l,r.dtype,u,p);return[n.makeTensorInfo(c,a.dtype,d),n.makeTensorInfo([c[0]],r.dtype,h),n.makeTensorInfo([m.length],"bool",new Uint8Array(m.map(g=>Number(g)))),n.makeTensorInfo([f.length],a.dtype,new Int32Array(f))]}var aJ={kernelName:Yc,backendName:"cpu",kernelFunc:nJ};function rJ(e){let{inputs:t,backend:n}=e,{inputIndices:a,inputShape:r,newShape:s}=t;if(a.shape.length!==2)throw new Error( ` Input indices should be a matrix but received shape
$ { a . shape } ` );if(r.shape.length!==1)throw new Error( ` Input shape should be a vector but received shape
$ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Target shape should be a vector but received shape $ { s . shape } ` );let i=Array.from(n.data.get(r.dataId).values),o=n.data.get(a.dataId).values,l=Array.from(n.data.get(s.dataId).values),[u,p,d]=E_(o,a.shape,a.dtype,i,l);return[n.makeTensorInfo(p,a.dtype,u),n.makeTensorInfo([d.length],s.dtype,new Int32Array(d))]}var sJ={kernelName:ap,backendName:"cpu",kernelFunc:rJ};function iJ(e){let{inputs:t,backend:n}=e,{data:a,indices:r,segmentIds:s}=t;if(a.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
$ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
$ { s . shape } ` );if(r.shape[0]!==s.shape[0])throw new Error("segmentIds and indices should have same size.");let i=n.data.get(a.dataId).values,o=n.data.get(r.dataId).values,l=n.data.get(s.dataId).values,[u,p]=Y1(i,a.shape,a.dtype,o,l,!0);return n.makeTensorInfo(p,a.dtype,u)}var oJ={kernelName:Zc,backendName:"cpu",kernelFunc:iJ};function lJ(e){let{inputs:t,backend:n}=e,{data:a,indices:r,segmentIds:s}=t;if(a.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
$ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
$ { s . shape } ` );if(r.shape[0]!==s.shape[0])throw new Error("segmentIds and indices should have same size.");let i=n.data.get(a.dataId).values,o=n.data.get(r.dataId).values,l=n.data.get(s.dataId).values,[u,p]=Y1(i,a.shape,a.dtype,o,l);return n.makeTensorInfo(p,a.dtype,u)}var uJ={kernelName:Jc,backendName:"cpu",kernelFunc:lJ};function pJ(e){let{inputs:t,backend:n,attrs:a}=e,{sparseIndices:r,sparseValues:s,defaultValue:i}=t,{outputShape:o}=a,{sliceRank:l,numUpdates:u,sliceSize:p,strides:d,outputSize:c}=T.calculateShapes(s,r,o),h=!1,m=n.bufferSync(r),f;switch(s.dtype){case"bool":{let g=n.bufferSync(s),b=!!n.data.get(i.dataId).values[0];f=ai(m,g,o,c,p,u,l,d,b,h);break}case"float32":{let g=n.bufferSync(s),b=n.data.get(i.dataId).values[0];f=ai(m,g,o,c,p,u,l,d,b,h);break}case"int32":{let g=n.bufferSync(s),b=n.data.get(i.dataId).values[0];f=ai(m,g,o,c,p,u,l,d,b,h);break}case"string":{let g=n.bufferSync(s),b=w.decodeString(n.data.get(i.dataId).values[0]);f=ai(m,g,o,c,p,u,l,d,b,h);break}default:throw new Error( ` Unsupported type $ { s . dtype } ` )}return n.makeTensorInfo(o,f.dtype,f.values)}var cJ={kernelName:rp,backendName:"cpu",kernelFunc:pJ};function dJ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{numOrSizeSplits:s,axis:i}=a,o=w.parseAxisParam(i,r.shape)[0],l=T.prepareSplitSize(r,s,o),u=new Array(r.shape.length).fill(0),p=r.shape.slice();return l.map(d=>{let c=[...p];c[o]=d;let h=wi({inputs:{x:r},backend:n,attrs:{begin:u,size:c}});return u[o]+=d,h})}var hJ={kernelName:np,backendName:"cpu",kernelFunc:dJ},mJ={kernelName:Qc,backendName:"cpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,a=t;ge(n,"square");let r=a.data.get(n.dataId).values,s=new Float32Array(r.length);for(let i=0;i<r.length;++i){let o=r[i];s[i]=o*o}return{dataId:a.write(s,n.shape,n.dtype),shape:n.shape,dtype:n.dtype}}},fJ=lt(Ts,(e,t)=>{let n=t;return isNaN(e)?NaN:e>0?1:n.alpha}),gJ={kernelName:Ts,backendName:"cpu",kernelFunc:fJ};function bJ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{begin:s,end:i,strides:o,beginMask:l,endMask:u,ellipsisMask:p,newAxisMask:d,shrinkAxisMask:c}=a;ge(r,"stridedSlice");let{finalShapeSparse:h,finalShape:m,isIdentity:f,sliceDim0:g,isSimpleSlice:b,begin:y,end:x,strides:v}=Kt.sliceInfo(r.shape,s,i,o,l,u,p,d,c),I;if(f)I=xt({inputs:{x:r},backend:n,attrs:{shape:m}});else if(g||b){w.assert(r.shape.length>=1,()=> ` Input must have rank at least 1 , got : $ { r . shape . length } ` );let N=Kt.computeOutShape(y,x,v),C=wi({inputs:{x:r},backend:n,attrs:{begin:y,size:N}});I=xt({inputs:{x:C},backend:n,attrs:{shape:m}}),n.disposeIntermediateTensorInfo(C)}else{let N=n.bufferSync(r),C=F_(h,N,v,y);I=n.makeTensorInfo(m,C.dtype,C.values)}return I}var yJ={kernelName:sp,backendName:"cpu",kernelFunc:bJ};function xJ(e){let{inputs:t,backend:n,attrs:a}=e,{separator:r,nGramWidths:s,leftPad:i,rightPad:o,padWidth:l,preserveShortSequences:u}=a,{data:p,dataSplits:d}=t,c=n.data.get(p.dataId).values,h=n.data.get(d.dataId).values,[m,f]=Z1(c,h,r,s,i,o,l,u);return[n.makeTensorInfo([m.length],"string",m),n.makeTensorInfo(d.shape,"int32",f)]}var vJ={kernelName:td,backendName:"cpu",kernelFunc:xJ};function wJ(e){let{inputs:t,backend:n,attrs:a}=e,{skipEmpty:r}=a,{input:s,delimiter:i}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");if(s.shape.length!==1)throw new Error( ` Input must be a vector , got shape : $ { s . shape } ` );if(i.shape.length!==0)throw new Error( ` Delimiter must be a scalar , got shape : $ { i . shape } ` );let o=n.data.get(s.dataId).values,l=n.data.get(i.dataId).values[0],[u,p,d]=J1(o,l,r),c=p.length;return[n.makeTensorInfo([c,2],"int32",u),n.makeTensorInfo([c],"string",p),n.makeTensorInfo([2],"int32",new Int32Array(d))]}var kJ={kernelName:nd,backendName:"cpu",kernelFunc:wJ};function IJ(e){let{inputs:t,backend:n,attrs:a}=e,{numBuckets:r}=a,{input:s}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");if(r<=0)throw new Error("Number of buckets must be at least 1");let i=n.data.get(s.dataId).values,o=Q1(i,r);return n.makeTensorInfo(s.shape,"int32",o)}var SJ={kernelName:ad,backendName:"cpu",kernelFunc:IJ},NJ=lt(Go,e=>Math.tan(e)),TJ={kernel
` ),s=r.length.toString().length+2,i=r.map((d,c)=>w.rightPad((c+1).toString(),s)+d),o=0;for(let d=0;d<i.length;d++)o=Math.max(i[d].length,o);let l=i.slice(0,a-1),u=i.slice(a-1,a),p=i.slice(a);console.log(l.join( `
2021-09-11 17:11:38 +02:00
` )),console.log(t.split( `
2024-01-17 22:56:53 +01:00
` )[0]),console.log( ` % c $ { w . rightPad ( u [ 0 ] , o ) } ` ,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(p.join( `
` ))}function rA(e){return Lr(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function sA(e,t){if(de(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 Yh(e,t){if(de(e,()=>e.validateProgram(t)),e.getProgramParameter(t,e.VALIDATE_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Shader program validation failed.")}function iA(e,t){let n=Lr(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return de(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),de(e,()=>e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function oA(e,t){let n=Lr(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return de(e,()=>e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,n)),de(e,()=>e.bufferData(e.ELEMENT_ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function r9(){return G().getNumber("WEBGL_VERSION")===2?1:4}function lA(e){return Lr(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function uA(e,t){let n=G().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e<=0||t<=0){let a= ` [ $ { e } x$ { t } ] ` ;throw new Error("Requested texture size "+a+" is invalid.")}if(e>n||t>n){let a= ` [ $ { e } x$ { t } ] ` ,r= ` [ $ { n } x$ { n } ] ` ;throw new Error("Requested texture size "+a+" greater than WebGL maximum on this browser / GPU "+r+".")}}function pA(e){return Lr(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function bv(e,t,n,a,r,s,i){let o=e.getAttribLocation(t,n);return o===-1?!1:(de(e,()=>e.bindBuffer(e.ARRAY_BUFFER,a)),de(e,()=>e.vertexAttribPointer(o,r,e.FLOAT,!1,s,i)),de(e,()=>e.enableVertexAttribArray(o)),!0)}function cA(e,t,n){gA(e,n),de(e,()=>e.activeTexture(e.TEXTURE0+n)),de(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function s9(e,t){gA(e,t),de(e,()=>e.activeTexture(e.TEXTURE0+t)),de(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function dA(e,t,n){return Lr(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function hA(e,t,n){return e.getUniformLocation(t,n)}function mA(e,t,n,a){de(e,()=>cA(e,t,a)),de(e,()=>e.uniform1i(n,a))}function i9(e){de(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,null)),de(e,()=>e.viewport(0,0,e.canvas.width,e.canvas.height)),de(e,()=>e.scissor(0,0,e.canvas.width,e.canvas.height))}function Zh(e,t,n){de(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,n)),de(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0))}function yv(e,t){de(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,t)),de(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0))}function cc(e){let t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+fA(e,t))}function fA(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 Lr(e,t,n){let a=de(e,()=>t());if(a==null)throw new Error(n);return a}function gA(e,t){let n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,a=t+e.TEXTURE0;if(a<e.TEXTURE0||a>n){let r= ` [ gl . TEXTURE0 , gl . TEXTURE$ { n } ] ` ;throw new Error( ` textureUnit must be in $ { r } . ` )}}function ki(e,t=2){return w.sizeFromShape(e.slice(0,e.length-t))}function Ii(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 dc(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[ki(e),...Ii(e)]),t}function bA(e,t=!1){let n=G().getNumber("WEBGL_MAX_TEXTURE_SIZE"),a=G().getNumber("WEBGL_MAX_SIZE_FOR_NARROW_TEXTURE");a===1/0&&G().getBool("WEBGL_AUTO_SQUARIFY_NARROW_TEXTURE_SHAPE")&&(a=n/2),t&&(n=n*2,a=a*2,e=e.map((o,l)=>l>=e.length-2?w.nearestLargerEven(e[l]):e[l]),e.length===1&&(e=[2,e[0]])),e.length!==2&&(e=w.squeezeShape(e).newShape);let r=w.sizeFromShape(e),s=null;e.length<=1&&r<=n?s=[1,r]: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 & 0x7fffffff u ) > 0x7f800000 u ;
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:56:53 +01:00
` :"",l="",u= `
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:56:53 +01:00
` ):(e="",t="attribute",n="varying",a="varying",r="texture2D",s="gl_FragColor",i="",o= `
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:56:53 +01:00
` ,l= `
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:56:53 +01:00
` ,u= `
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:56:53 +01:00
` ),{version:e,attribute:t,varyingVs:n,varyingFs:a,texture2D:r,output:s,defineOutput:i,defineSpecialNaN:o,defineSpecialInf:l,defineRound:u}}function Qo(e,t,n="index"){let a=w.computeStrides(t);return a.map((r,s)=>{let i= ` int $ { e [ s ] } = $ { n } / $ { r } ` ,o=s===a.length-1? ` int $ { e [ s + 1 ] } = $ { n } - $ { e [ s ] } * $ { r } ` : ` index -= $ { e [ s ] } * $ { r } ` ;return ` $ { i } ; $ { o } ; ` }).join("")}function Qf(e,t,n="index"){let a=w.computeStrides(t);return a.map((r,s)=>{let i= ` int $ { e [ s ] } = $ { n } / outShapeStrides [ $ { s } ] ` ,o=s===a.length-1? ` int $ { e [ s + 1 ] } = $ { n } - $ { e [ s ] } * outShapeStrides [ $ { s } ] ` : ` index -= $ { e [ s ] } * outShapeStrides [ $ { s } ] ` ;return ` $ { i } ; $ { o } ; ` }).join("")}function p9(e,t){let n=e.length,a=e.map(s=> ` $ { t } [ $ { s } ] ` ),r=new Array(n-1);r[n-2]=a[n-1];for(let s=n-3;s>=0;--s)r[s]= ` ( $ { r [ s + 1 ] } * $ { a [ s + 1 ] } ) ` ;return r}function c9(e,t,n="index"){let a=e.map((s,i)=>i),r=p9(a,t);return r.map((s,i)=>{let o= ` int $ { e [ i ] } = $ { n } / $ { r [ i ] } ` ,l=i===r.length-1? ` int $ { e [ i + 1 ] } = $ { n } - $ { e [ i ] } * $ { r [ i ] } ` : ` index -= $ { e [ i ] } * $ { r [ i ] } ` ;return ` $ { o } ; $ { l } ; ` }).join("")}function ok(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:56:53 +01:00
` }function lk(){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:56:53 +01:00
` }var SA= `
2021-09-11 17:11:38 +02:00
const float FLOAT _MAX = 1.70141184 e38 ;
const float FLOAT _MIN = 1.17549435 e - 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:56:53 +01:00
` ,{getBroadcastDims:NA}=T;function d9(e,t,n){let a=[];if(e.forEach(c=>{let h=w.sizeFromShape(c.shapeInfo.logicalShape);if(c.shapeInfo.isUniform?a.push( ` uniform float $ { c . name } $ { h > 1 ? ` [ ${ h } ] ` : "" } ; ` ):(a.push( ` uniform sampler2D $ { c . name } ; ` ),a.push( ` uniform int offset$ { c . name } ; ` )),n.enableShapeUniforms){let{uniformShape:m}=uk(n.packedInputs,c.shapeInfo.logicalShape,c.shapeInfo.texShape);switch(m.length){case 1:a.push( ` uniform int $ { c . name } Shape ; ` );break;case 2:a.push( ` uniform ivec2 $ { c . name } Shape ; ` );break;case 3:a.push( ` uniform ivec3 $ { c . name } Shape ; ` );break;case 4:a.push( ` uniform ivec4 $ { c . name } Shape ; ` );break;default:break}a.push( ` uniform ivec2 $ { c . name } TexShape ; ` )}}),n.enableShapeUniforms){switch(t.logicalShape.length){case 1:a.push("uniform int outShape;");break;case 2:a.push("uniform ivec2 outShape;"),a.push("uniform int outShapeStrides;");break;case 3:a.push("uniform ivec3 outShape;"),a.push("uniform ivec2 outShapeStrides;");break;case 4:a.push("uniform ivec4 outShape;"),a.push("uniform ivec3 outShapeStrides;");break;default:break}a.push("uniform ivec2 outTexShape;")}n.customUniforms&&n.customUniforms.forEach(c=>{a.push( ` uniform $ { c . type } $ { c . name } $ { c . arrayIndex ? ` [ ${ c . arrayIndex } ] ` : "" } ; ` )});let r=a.join( `
` ),s=e.map(c=>h9(c,t,n.packedInputs,n.enableShapeUniforms)).join( `
` ),i=t.texShape,o=Cn(),l=g9(o),u,p,d=x9(o);return t.isPacked?(u=m9(t.logicalShape,i,n.enableShapeUniforms),p=y9(o)):(u=f9(t.logicalShape,i,n.enableShapeUniforms),p=b9(o)),n.packedInputs&&(d+=I9),[d,l,p,r,u,s,n.userCode].join( `
` )}function kp(e,t=!1){let n=e.shapeInfo.logicalShape;switch(n.length){case 0:return M9(e,t);case 1:return P9(e,t);case 2:return z9(e,t);case 3:return B9(e,t);case 4:return U9(e,t);case 5:return G9(e);case 6:return H9(e);default:throw new Error( ` $ { n . length } - D input sampling is not yet supported ` )}}function TA(e,t){switch(e.shapeInfo.logicalShape.length){case 0:return R9(e);case 1:return O9(e,t);case 2:return L9(e,t);case 3:return W9(e,t);default:return V9(e,t)}}function h9(e,t,n=!1,a){let r="";n?r+=TA(e,a):r+=kp(e,a);let s=e.shapeInfo.logicalShape,i=t.logicalShape;return s.length<=i.length&&(n?r+=q9(e,t):r+=j9(e,t)),r}function m9(e,t,n){switch(e.length){case 0:return CA();case 1:return S9(e,t,n);case 2:return $ 9(e,t,n);case 3:return T9(e,t,n);default:return E9(e,t,n)}}function f9(e,t,n){switch(e.length){case 0:return CA();case 1:return N9(e,t,n);case 2:return D9(e,t,n);case 3:return C9(e,t,n);case 4:return _9(e,t,n);case 5:return A9(e,t);case 6:return F9(e,t);default:throw new Error( ` $ { e . length } - D output sampling is not yet supported ` )}}function g9(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:56:53 +01:00
` }function b9(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:56:53 +01:00
` }function y9(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:56:53 +01:00
` }function x9(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:56:53 +01:00
$ { v9 }
$ { w9 }
$ { k9 }
` }var v9= `
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:56:53 +01:00
` ,w9= `
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:56:53 +01:00
` ,k9= `
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:56:53 +01:00
` ,I9= `
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:56:53 +01:00
` ;function CA(){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:56:53 +01:00
` }function S9(e,t,n){let a=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return a[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:56:53 +01:00
return 2 * int ( resultUV . x * $ { a [ 1 ] } . 0 ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` :a[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:56:53 +01:00
return 2 * int ( resultUV . y * $ { a [ 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:56:53 +01:00
vec2 ( $ { a [ 0 ] } , $ { a [ 1 ] } ) ) ;
return 2 * ( resTexRC . x * $ { a [ 1 ] } + resTexRC . y ) ;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:56:53 +01:00
` }function N9(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:56:53 +01:00
` }function T9(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:56:53 +01:00
` ;let a=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],r=Math.ceil(e[2]/2),s=r*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:56:53 +01:00
vec2 ( $ { a [ 0 ] } , $ { a [ 1 ] } ) ) ;
int index = resTexRC . x * $ { a [ 1 ] } + resTexRC . y ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
int b = index / $ { s } ;
index -= b * $ { s } ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
int r = 2 * ( index / $ { r } ) ;
int c = imod ( index , $ { r } ) * 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:56:53 +01:00
` }function C9(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:56:53 +01:00
$ { Qf ( [ "r" , "c" , "d" ] , e ) }
2021-09-11 17:11:38 +02:00
return ivec3 ( r , c , d ) ;
}
2024-01-17 22:56:53 +01:00
` ;let a=Qo(["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:56:53 +01:00
$ { a }
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:56:53 +01:00
` }function E9(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:56:53 +01:00
` ;let a=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],r=Math.ceil(e[e.length-1]/2),s=r*Math.ceil(e[e.length-2]/2),i=s,o="",l="b, r, c";for(let u=2;u<e.length-1;u++)i*=e[e.length-u-1],o= `
int b$ { u } = index / $ { i } ;
index -= b$ { u } * $ { i } ;
` +o,l= ` b$ { u } , ` +l;return `
2021-09-11 17:11:38 +02:00
ivec$ { e . length } getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2024-01-17 22:56:53 +01:00
vec2 ( $ { a [ 0 ] } , $ { a [ 1 ] } ) ) ;
int index = resTexRC . x * $ { a [ 1 ] } + resTexRC . y ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
$ { o }
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
int b = index / $ { s } ;
index -= b * $ { s } ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
int r = 2 * ( index / $ { r } ) ;
int c = imod ( index , $ { r } ) * 2 ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
return ivec$ { e . length } ( $ { l } ) ;
2021-05-18 14:11:17 +02:00
}
2024-01-17 22:56:53 +01:00
` }function _9(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:56:53 +01:00
$ { Qf ( [ "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:56:53 +01:00
` ;let a=Qo(["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:56:53 +01:00
$ { a }
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:56:53 +01:00
` }function A9(e,t){let n=Qo(["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:56:53 +01:00
` }function F9(e,t){let n=Qo(["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:56:53 +01:00
` }function $ 9(e,t,n){let a=[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:56:53 +01:00
return 2 * ivec2 ( resultUV . yx * vec2 ( $ { a [ 0 ] } , $ { a [ 1 ] } ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let r=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:56:53 +01:00
vec2 ( $ { a [ 0 ] } , $ { a [ 1 ] } ) ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
int index = resTexRC . x * $ { a [ 1 ] } + resTexRC . y ;
int r = 2 * ( index / $ { r } ) ;
int c = imod ( index , $ { r } ) * 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:56:53 +01:00
` }function D9(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:56:53 +01:00
` }function el(e){return ` offset$ { e } ` }function R9(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),a=Cn();return `
2021-09-11 17:11:38 +02:00
vec4 $ { n } ( ) {
2024-01-17 22:56:53 +01:00
return $ { a . texture2D } ( $ { t } , halfCR ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }function M9(e,t){let n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return ` float $ { a } ( ) { return $ { n } ; } ` ;let[r,s]=e.shapeInfo.texShape;if(r===1&&s===1)return `
float $ { a } ( ) {
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , halfCR ) ;
}
2024-01-17 22:56:53 +01:00
` ;let i=el(n);if(t)return `
float $ { a } ( ) {
vec2 uv = uvFromFlat ( $ { n } TexShape [ 0 ] , $ { n } TexShape [ 1 ] , $ { i } ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let[o,l]=e.shapeInfo.texShape;return `
float $ { a } ( ) {
vec2 uv = uvFromFlat ( $ { o } , $ { l } , $ { i } ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }function O9(e,t){let n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape,s=Cn();if(t)return `
vec4 $ { a } ( 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:56:53 +01:00
return $ { s . texture2D } ( $ { n } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)];return `
vec4 $ { a } ( int index ) {
2021-09-11 17:11:38 +02:00
vec2 uv = packedUVfrom1D (
2024-01-17 22:56:53 +01:00
$ { i [ 0 ] } , $ { i [ 1 ] } , index ) ;
return $ { s . texture2D } ( $ { n } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }function P9(e,t){let n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return `
float $ { a } ( int index ) {
$ { Ip ( e ) }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let r=e.shapeInfo.texShape,s=r[0],i=r[1];if(i===1&&s===1)return `
float $ { a } ( int index ) {
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , halfCR ) ;
}
2024-01-17 22:56:53 +01:00
` ;let o=el(n);return i===1?t? `
float $ { a } ( int index ) {
vec2 uv = vec2 ( 0.5 , ( float ( index + $ { o } ) + 0.5 ) / float ( $ { n } TexShape [ 0 ] ) ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
` : `
2024-01-17 22:56:53 +01:00
float $ { a } ( int index ) {
vec2 uv = vec2 ( 0.5 , ( float ( index + $ { o } ) + 0.5 ) / $ { s } . 0 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` :s===1?t? `
float $ { a } ( int index ) {
vec2 uv = vec2 ( ( float ( index + $ { o } ) + 0.5 ) / float ( $ { n } TexShape [ 1 ] ) , 0.5 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
` : `
2024-01-17 22:56:53 +01:00
float $ { a } ( int index ) {
vec2 uv = vec2 ( ( float ( index + $ { o } ) + 0.5 ) / $ { i } . 0 , 0.5 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
` :t? `
2024-01-17 22:56:53 +01:00
float $ { a } ( int index ) {
vec2 uv = uvFromFlat ( $ { n } TexShape [ 0 ] , $ { n } TexShape [ 1 ] , index + $ { o } ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
` : `
2024-01-17 22:56:53 +01:00
float $ { a } ( int index ) {
vec2 uv = uvFromFlat ( $ { s } , $ { i } , index + $ { o } ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` }function L9(e,t){let n=e.shapeInfo.logicalShape,a=e.name,r="get"+a.charAt(0).toUpperCase()+a.slice(1),s=e.shapeInfo.texShape,i=s[0],o=s[1],l=Cn();if(s!=null&&w.arraysEqual(n,s))return t? `
vec4 $ { r } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { a } TexShape [ 1 ] , $ { a } TexShape [ 0 ] ) ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
return $ { l . texture2D } ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
vec4 $ { r } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { o } . 0 , $ { i } . 0 ) ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
return $ { l . texture2D } ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` ;if(t)return `
2024-01-17 22:56:53 +01:00
vec4 $ { r } ( int row , int col ) {
ivec2 packedTexShape = ivec2 ( ceil ( float ( $ { a } TexShape [ 0 ] ) / 2.0 ) , ceil ( float ( $ { a } TexShape [ 1 ] ) / 2.0 ) ) ;
int valuesPerRow = int ( ceil ( float ( $ { a } 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:56:53 +01:00
return $ { l . texture2D } ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let u=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)],p=Math.ceil(n[1]/2);return `
vec4 $ { r } ( int row , int col ) {
vec2 uv = packedUVfrom2D ( $ { p } , $ { u [ 0 ] } , $ { u [ 1 ] } , row , col ) ;
return $ { l . texture2D } ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }function z9(e,t){let n=e.shapeInfo.logicalShape,a=e.name,r="get"+a.charAt(0).toUpperCase()+a.slice(1),s=e.shapeInfo.texShape;if(s!=null&&w.arraysEqual(n,s)){if(t)return `
float $ { r } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { a } TexShape [ 1 ] , $ { a } TexShape [ 0 ] ) ;
return sampleTexture ( $ { a } , uv ) ;
}
` ;let c=s[0],h=s[1];return `
float $ { r } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { h } . 0 , $ { c } . 0 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }let{newShape:i,keptDims:o}=w.squeezeShape(n),l=i;if(l.length<n.length){let c=Sp(e,l),h=["row","col"];return `
$ { kp ( c , t ) }
float $ { r } ( int row , int col ) {
return $ { r } ( $ { Np ( h , o ) } ) ;
2021-09-11 17:11:38 +02:00
}
` }if(e.shapeInfo.isUniform)return `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
$ { Ip ( e ) }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let u=s[0],p=s[1],d=el(a);return p===1?t? `
float $ { r } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { d } ) , vec3 ( $ { a } Shape [ 1 ] , 1 , 1 ) ) ;
vec2 uv = vec2 ( 0.5 , ( index + 0.5 ) / float ( $ { a } TexShape [ 0 ] ) ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { d } ) , vec3 ( $ { n [ 1 ] } , 1 , 1 ) ) ;
vec2 uv = vec2 ( 0.5 , ( index + 0.5 ) / $ { u } . 0 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` :u===1?t? `
float $ { r } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { d } ) , vec3 ( $ { a } Shape [ 1 ] , 1 , 1 ) ) ;
vec2 uv = vec2 ( ( index + 0.5 ) / float ( $ { a } TexShape [ 1 ] ) , 0.5 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { d } ) , vec3 ( $ { n [ 1 ] } , 1 , 1 ) ) ;
vec2 uv = vec2 ( ( index + 0.5 ) / $ { p } . 0 , 0.5 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` :t? `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
int index = row * $ { a } Shape [ 1 ] + col + $ { d } ;
vec2 uv = uvFromFlat ( $ { a } TexShape [ 0 ] , $ { a } TexShape [ 1 ] , index ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
int index = row * $ { n [ 1 ] } + col + $ { d } ;
vec2 uv = uvFromFlat ( $ { u } , $ { p } , index ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }function W9(e,t){let n=e.shapeInfo.logicalShape,a=e.name,r="get"+a.charAt(0).toUpperCase()+a.slice(1),s=e.shapeInfo.texShape,i=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)];if(n[0]===1){let c=n.slice(1),h=[1,2],m=Sp(e,c),f=["b","row","col"];return `
$ { TA ( m , t ) }
vec4 $ { r } ( int b , int row , int col ) {
return $ { r } ( $ { Np ( f , h ) } ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }let o=Cn();if(t)return `
vec4 $ { r } ( int b , int row , int col ) {
ivec2 packedTexShape = ivec2 ( ceil ( float ( $ { a } TexShape [ 0 ] ) / 2.0 ) , ceil ( float ( $ { a } TexShape [ 1 ] ) / 2.0 ) ) ;
int valuesPerRow = int ( ceil ( float ( $ { a } Shape [ 2 ] ) / 2.0 ) ) ;
int texelsInBatch = valuesPerRow * int ( ceil ( float ( $ { a } 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:56:53 +01:00
return $ { o . texture2D } ( $ { a } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let l=i[0],u=i[1],p=Math.ceil(n[2]/2),d=p*Math.ceil(n[1]/2);return `
vec4 $ { r } ( int b , int row , int col ) {
2021-09-11 17:11:38 +02:00
vec2 uv = packedUVfrom3D (
2024-01-17 22:56:53 +01:00
$ { l } , $ { u } , $ { d } , $ { p } , b , row , col ) ;
return $ { o . texture2D } ( $ { a } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }function B9(e,t){let n=e.shapeInfo.logicalShape,a=e.name,r="get"+a.charAt(0).toUpperCase()+a.slice(1),s=n[1]*n[2],i=n[2],{newShape:o,keptDims:l}=w.squeezeShape(n),u=o;if(u.length<n.length){let f=Sp(e,u),g=["row","col","depth"];return `
$ { kp ( f , t ) }
float $ { r } ( int row , int col , int depth ) {
return $ { r } ( $ { Np ( g , l ) } ) ;
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:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
vec3 ( $ { s } , $ { i } , 1 ) ) ) ;
$ { Ip ( e ) }
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let p=e.shapeInfo.texShape,d=p[0],c=p[1],h=e.shapeInfo.flatOffset;if(c===s&&h==null)return t? `
float $ { r } ( int row , int col , int depth ) {
int stride1 = $ { a } 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:56:53 +01:00
vec2 ( $ { a } TexShape [ 1 ] , $ { a } TexShape [ 0 ] ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( int row , int col , int depth ) {
2021-09-11 17:11:38 +02:00
float texR = float ( row ) ;
2024-01-17 22:56:53 +01:00
float texC = dot ( vec2 ( col , depth ) , vec2 ( $ { i } , 1 ) ) ;
2021-09-11 17:11:38 +02:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2024-01-17 22:56:53 +01:00
vec2 ( $ { c } . 0 , $ { d } . 0 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ;if(c===i&&h==null)return t? `
float $ { r } ( int row , int col , int depth ) {
float texR = dot ( vec2 ( row , col ) , vec2 ( $ { a } Shape [ 1 ] , 1 ) ) ;
2021-09-11 17:11:38 +02:00
float texC = float ( depth ) ;
2024-01-17 22:56:53 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { a } TexShape [ 1 ] , $ { a } TexShape [ 0 ] ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { c } . 0 , $ { d } . 0 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let m=el(a);return t? `
float $ { r } ( 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:56:53 +01:00
int stride0 = $ { a } Shape [ 1 ] * $ { a } Shape [ 2 ] ;
int stride1 = $ { a } Shape [ 2 ] ;
int index = row * stride0 + col * stride1 + depth + $ { m } ;
vec2 uv = uvFromFlat ( $ { a } TexShape [ 0 ] , $ { a } TexShape [ 1 ] , index ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
int index = row * $ { s } + col * $ { i } + depth + $ { m } ;
vec2 uv = uvFromFlat ( $ { d } , $ { c } , index ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }function V9(e,t){let n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=Cn();if(t)return `
vec4 $ { a } ( 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:56:53 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( packedTexShape [ 1 ] , packedTexShape [ 0 ] ) ; return $ { r . texture2D } ( $ { n } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let s=e.shapeInfo.logicalShape,i=s.length,o=e.shapeInfo.texShape,l=[Math.ceil(o[0]/2),Math.ceil(o[1]/2)],u=l[0],p=l[1],d=Math.ceil(s[i-1]/2),c=d*Math.ceil(s[i-2]/2),h="int b, int row, int col",m= ` b * $ { c } + ( row / 2 ) * $ { d } + ( col / 2 ) ` ;for(let f=2;f<i-1;f++)h= ` int b$ { f } , ` +h,c*=s[i-f-1],m= ` b$ { f } * $ { c } + ` +m;return `
vec4 $ { a } ( $ { h } ) {
int index = $ { m } ;
int texR = index / $ { p } ;
int texC = index - texR * $ { p } ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { p } , $ { u } ) ;
return $ { r . texture2D } ( $ { n } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }function U9(e,t){let n=e.shapeInfo.logicalShape,a=e.name,r="get"+a.charAt(0).toUpperCase()+a.slice(1),s=n[3],i=n[2]*s,o=n[1]*i,{newShape:l,keptDims:u}=w.squeezeShape(n);if(l.length<n.length){let y=Sp(e,l),x=["row","col","depth","depth2"];return `
$ { kp ( y , t ) }
float $ { r } ( int row , int col , int depth , int depth2 ) {
return $ { r } ( $ { Np ( x , u ) } ) ;
2021-09-11 17:11:38 +02:00
}
` }if(e.shapeInfo.isUniform)return `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
vec4 ( $ { o } , $ { i } , $ { s } , 1 ) ) ) ;
$ { Ip ( e ) }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let p=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,c=d[0],h=d[1],m= ` int stride2 = $ { a } Shape [ 3 ] ; ` ,f= ` int stride1 = $ { a } Shape [ 2 ] * stride2 ; ` ,g= ` int stride0 = $ { a } Shape [ 1 ] * stride1 ; ` ;if(h===o&&p==null)return t? `
float $ { r } ( int row , int col , int depth , int depth2 ) {
$ { m }
2022-10-18 13:23:49 +02:00
$ { f }
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:56:53 +01:00
vec2 ( $ { a } TexShape [ 1 ] , $ { a } TexShape [ 0 ] ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
vec3 ( $ { i } , $ { s } , 1 ) ) ;
2021-09-11 17:11:38 +02:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2024-01-17 22:56:53 +01:00
vec2 ( $ { h } . 0 , $ { c } . 0 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;if(h===s&&p==null)return t? `
float $ { r } ( 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:56:53 +01:00
vec3 ( $ { a } Shape [ 1 ] * $ { a } Shape [ 2 ] , $ { a } 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:56:53 +01:00
vec2 ( $ { a } TexShape [ 1 ] , $ { a } TexShape [ 0 ] ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
vec2 ( $ { h } . 0 , $ { c } . 0 ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let b=el(a);return t? `
float $ { r } ( 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:56:53 +01:00
$ { m }
2022-10-18 13:23:49 +02:00
$ { f }
2021-09-11 17:11:38 +02:00
$ { g }
int index = row * stride0 + col * stride1 +
depth * stride2 + depth2 ;
2024-01-17 22:56:53 +01:00
vec2 uv = uvFromFlat ( $ { a } TexShape [ 0 ] , $ { a } TexShape [ 1 ] , index + $ { b } ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
` : `
2024-01-17 22:56:53 +01:00
float $ { r } ( 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:56:53 +01:00
int index = row * $ { o } + col * $ { i } +
depth * $ { s } + depth2 ;
vec2 uv = uvFromFlat ( $ { c } , $ { h } , index + $ { b } ) ;
return sampleTexture ( $ { a } , uv ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }function G9(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=t[4],s=t[3]*r,i=t[2]*s,o=t[1]*i,{newShape:l,keptDims:u}=w.squeezeShape(t);if(l.length<t.length){let f=Sp(e,l),g=["row","col","depth","depth2","depth3"];return `
$ { kp ( f ) }
float $ { a } ( int row , int col , int depth , int depth2 , int depth3 ) {
return $ { a } ( $ { Np ( g , u ) } ) ;
2021-09-11 17:11:38 +02:00
}
` }if(e.shapeInfo.isUniform)return `
2024-01-17 22:56:53 +01:00
float $ { a } ( 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:56:53 +01:00
vec4 ( $ { o } , $ { i } , $ { s } , $ { r } ) ) +
2021-09-11 17:11:38 +02:00
depth3 ;
2024-01-17 22:56:53 +01:00
$ { Ip ( e ) }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let p=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,c=d[0],h=d[1];if(h===o&&p==null)return `
float $ { a } ( 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:56:53 +01:00
vec4 ( $ { i } , $ { s } , $ { r } , 1 ) ) ;
2021-09-11 17:11:38 +02:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2024-01-17 22:56:53 +01:00
vec2 ( $ { h } . 0 , $ { c } . 0 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` ;if(h===r&&p==null)return `
float $ { a } ( 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:56:53 +01:00
vec2 ( $ { h } . 0 , $ { c } . 0 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` ;let m=el(n);return `
float $ { a } ( 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:56:53 +01:00
int index = row * $ { o } + col * $ { i } + depth * $ { s } +
depth2 * $ { r } + depth3 + $ { m } ;
vec2 uv = uvFromFlat ( $ { c } , $ { h } , index ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` }function H9(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),{newShape:r,keptDims:s}=w.squeezeShape(t);if(r.length<t.length){let g=Sp(e,r),b=["row","col","depth","depth2","depth3","depth4"];return `
$ { kp ( g ) }
float $ { a } ( int row , int col , int depth ,
2021-09-11 17:11:38 +02:00
int depth2 , int depth3 , int depth4 ) {
2024-01-17 22:56:53 +01:00
return $ { a } ( $ { Np ( b , s ) } ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }let i=t[5],o=t[4]*i,l=t[3]*o,u=t[2]*l,p=t[1]*u;if(e.shapeInfo.isUniform)return `
float $ { a } ( 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:56:53 +01:00
vec4 ( $ { p } , $ { u } , $ { l } , $ { o } ) ) +
2021-09-11 17:11:38 +02:00
dot (
vec2 ( depth3 , depth4 ) ,
2024-01-17 22:56:53 +01:00
vec2 ( $ { i } , 1 ) ) ) ;
$ { Ip ( e ) }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let d=e.shapeInfo.flatOffset,c=e.shapeInfo.texShape,h=c[0],m=c[1];if(m===p&&d==null)return `
float $ { a } ( 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:56:53 +01:00
vec4 ( $ { u } , $ { l } , $ { o } , $ { i } ) ) +
2021-09-11 17:11:38 +02:00
float ( depth4 ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2024-01-17 22:56:53 +01:00
vec2 ( $ { m } . 0 , $ { h } . 0 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` ;if(m===i&&d==null)return `
float $ { a } ( 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:56:53 +01:00
vec2 ( $ { m } . 0 , $ { h } . 0 ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` ;let f=el(n);return `
float $ { a } ( 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:56:53 +01:00
int index = row * $ { p } + col * $ { u } + depth * $ { l } +
depth2 * $ { o } + depth3 * $ { i } + depth4 + $ { f } ;
vec2 uv = uvFromFlat ( $ { h } , $ { m } , index ) ;
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , uv ) ;
}
2024-01-17 22:56:53 +01:00
` }function Ip(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:56:53 +01:00
` }function q9(e,t){let n=e.name,a=n.charAt(0).toUpperCase()+n.slice(1),r="get"+a+"AtOutCoords",s=e.shapeInfo.logicalShape.length,i=t.logicalShape.length,o=NA(e.shapeInfo.logicalShape,t.logicalShape),l=ht(i),u=i-s,p,d=["x","y","z","w","u","v"];s===0?p="":i<2&&o.length>=1?p="coords = 0;":p=o.map(g=> ` coords . $ { d [ g + u ] } = 0 ; ` ).join( `
` );let c="";i<2&&s>0?c="coords":c=e.shapeInfo.logicalShape.map((g,b)=> ` coords . $ { d [ b + u ] } ` ).join(", ");let h="return outputValue;",m=w.sizeFromShape(e.shapeInfo.logicalShape)===1,f=w.sizeFromShape(t.logicalShape)===1;if(s===1&&!m&&!f)h= `
2021-09-11 17:11:38 +02:00
return vec4 ( outputValue . xy , outputValue . xy ) ;
2024-01-17 22:56:53 +01:00
` ;else if(m&&!f)i===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:56:53 +01:00
` ;else if(o.length){let g=s-2,b=s-1;o.indexOf(g)>-1&&o.indexOf(b)>-1?h="return vec4(outputValue.x);":o.indexOf(g)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":o.indexOf(b)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return `
vec4 $ { r } ( ) {
$ { l } coords = getOutputCoords ( ) ;
$ { p }
vec4 outputValue = get$ { a } ( $ { c } ) ;
2021-09-11 17:11:38 +02:00
$ { h }
}
2024-01-17 22:56:53 +01:00
` }function j9(e,t){let n=e.name,a=n.charAt(0).toUpperCase()+n.slice(1),r="get"+a+"AtOutCoords",s=t.texShape,i=e.shapeInfo.texShape,o=e.shapeInfo.logicalShape.length,l=t.logicalShape.length;if(!e.shapeInfo.isUniform&&o===l&&e.shapeInfo.flatOffset==null&&w.arraysEqual(i,s))return `
float $ { r } ( ) {
2021-09-11 17:11:38 +02:00
return sampleTexture ( $ { n } , resultUV ) ;
}
2024-01-17 22:56:53 +01:00
` ;let u=ht(l),p=NA(e.shapeInfo.logicalShape,t.logicalShape),d=l-o,c,h=["x","y","z","w","u","v"];o===0?c="":l<2&&p.length>=1?c="coords = 0;":c=p.map(f=> ` coords . $ { h [ f + d ] } = 0 ; ` ).join( `
` );let m="";return l<2&&o>0?m="coords":m=e.shapeInfo.logicalShape.map((f,g)=> ` coords . $ { h [ g + d ] } ` ).join(", "), `
float $ { r } ( ) {
$ { u } coords = getOutputCoords ( ) ;
$ { c }
return get$ { a } ( $ { m } ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +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 uk(e,t,n){let{newShape:a,keptDims:r}=w.squeezeShape(t),s=t.length,i=e&&s===3&&t[0]===1,o=i?t.slice(1):a,l=!e&&s>1&&!w.arraysEqual(t,n)&&a.length<s||i;return{useSqueezeShape:l,uniformShape:l?o:t,keptDims:r}}function Sp(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function Np(e,t){return t.map(n=>e[n]).join(", ")}function K9(e,t,n,a){let r=n.map((p,d)=>{let c={logicalShape:p.shape,texShape:p.isUniform?null:p.texData.texShape,isUniform:p.isUniform,isPacked:p.isUniform?!1:p.texData.isPacked,flatOffset:null};return p.texData!=null&&p.texData.slice!=null&&p.texData.slice.flatOffset>0&&(c.flatOffset=p.texData.slice.flatOffset),{name:t.variableNames[d],shapeInfo:c}}),s=r.map(p=>p.shapeInfo),i={logicalShape:a.shape,texShape:a.texData.texShape,isUniform:!1,isPacked:a.texData.isPacked,flatOffset:null},o=d9(r,i,t),l=aA(e.gl,o),u=e.createProgram(l);return G().get("ENGINE_COMPILE_ONLY")?{program:t,fragmentShader:l,source:o,webGLProgram:u,inShapeInfos:s,outShapeInfo:i,variablesLocations:null,customUniformLocations:null,infLoc:null,nanLoc:null,outShapeLocation:null,outShapeStridesLocation:null,outTexShapeLocation:null}:(e.buildVao(u),Object.assign({program:t,fragmentShader:l,source:o,webGLProgram:u,inShapeInfos:s,outShapeInfo:i},EA(e,t,u)))}function EA(e,t,n){let a=[],r=[],s,i,o,l=null,u=null;u=e.getUniformLocation(n,"NAN",!1),G().getNumber("WEBGL_VERSION")===1&&(l=e.getUniformLocation(n,"INFINITY",!1));let p=!1;for(let d of t.variableNames){let c={name:d,uniform:e.getUniformLocation(n,d,p),offset:e.getUniformLocation(n, ` offset$ { d } ` ,p)};t.enableShapeUniforms&&(c.shape=e.getUniformLocation(n, ` $ { d } Shape ` ,p),c.texShape=e.getUniformLocation(n, ` $ { d } TexShape ` ,p)),a.push(c)}if(t.enableShapeUniforms&&(s=e.getUniformLocation(n,"outShape",p),o=e.getUniformLocation(n,"outShapeStrides",p),i=e.getUniformLocation(n,"outTexShape",p)),t.customUniforms)for(let d of t.customUniforms)r.push(e.getUniformLocation(n,d.name,p));return{variablesLocations:a,customUniformLocations:r,infLoc:l,nanLoc:u,outShapeLocation:s,outShapeStridesLocation:o,outTexShapeLocation:i}}function SS(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,a)=>{let r=n.logicalShape,s=t[a],i=s.shape;if(!w.arraysEqual(r,i))throw Error( ` Binary was compiled with different shapes than the current args . Shapes $ { r } and $ { i } must match ` );if(n.isUniform&&s.isUniform)return;let o=n.texShape,l=s.isUniform?null:s.texData.texShape;if(!w.arraysEqual(o,l))throw Error( ` Binary was compiled with different texture shapes than the current args . Shape $ { o } and $ { l } must match ` )})}function X9(e,t,n,a,r){t.program.enableShapeUniforms||(SS(t.inShapeInfos,n),SS([t.outShapeInfo],[a]));let s=a.texData.texture,i=a.texData.texShape;a.texData.isPacked?e.setOutputPackedMatrixTexture(s.texture,i[0],i[1]):e.setOutputMatrixTexture(s.texture,i[0],i[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 l=0;l<n.length;++l){let u=n[l],{uniform:p,offset:d,shape:c,texShape:h}=t.variablesLocations[l];if(c){let{uniformShape:m}=uk(t.program.packedInputs,u.shape,u.texData.texShape);switch(m.length){case 1:e.gl.uniform1iv(c,new Int32Array(m));break;case 2:e.gl.uniform2iv(c,new Int32Array(m));break;case 3:e.gl.uniform3iv(c,new Int32Array(m));break;case 4:e.gl.uniform4iv(c,new Int32Array(m));break;default:break}}if(h&&e.gl.uniform2i(h,u.texData.texShape[0],u.texData.texShape[1]),p!=null){if(u.isUniform){if(w.sizeFromShape(u.shape)<2)e.gl.uniform1f(p,u.uniformValues[0]);else{let m=u.uniformValues;m instanceof Float32Array||(m=new Float32Array(m)),e.gl.uniform1fv(p,m)}continue}u.texData.slice!=null&&d!=null&&e.gl.uniform1i(d,u.texData.slice.flatOffset)
2021-09-11 17:11:38 +02:00
ivec3 outCoordsFromFlatIndex ( int index ) {
2024-01-17 22:56:53 +01:00
$ { this . enableShapeUniforms ? Qf ( [ "r" , "c" , "d" ] , e ) : Qo ( [ "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:56:53 +01:00
` }},J9=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=Fc.DENSE,this.customUniforms=[{name:"texShape",type:"ivec2"}];let t=Cn();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:56:53 +01:00
$ { this . enableShapeUniforms ? Qf ( [ "r" , "c" , "d" ] , e ) : Qo ( [ "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:56:53 +01:00
` }},Q9=class{constructor(e){this.variableNames=["A"],this.outTexUsage=da.DOWNLOAD;let t=Cn();this.outputShape=e,this.userCode= `
$ { SA }
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:56:53 +01:00
` }},eQ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=da.DOWNLOAD;let t=Cn();this.outputShape=e,this.userCode= `
$ { SA }
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:56:53 +01:00
` }},tQ={R:0,G:1,B:2,A:3},NS=class{constructor(e,t=!1,n="RGBA"){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let a=Cn();this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let r="result";t&&(r="floor(result * 255. + 0.5)");let s="";for(let i=0;i<n.length;i++){let o=n[i];s+= `
if ( offset == $ { i } ) {
result = values [ $ { tQ [ o ] } ] ;
2022-10-14 14:01:39 +02:00
} ` }this.userCode= `
2024-01-17 22:56:53 +01:00
$ { this . enableShapeUniforms ? lk ( ) : ok ( 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:56:53 +01:00
vec4 values = $ { a . texture2D } ( A , uv ) ;
$ { s }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
$ { a . output } = vec4 ( $ { r } , 0. , 0. , 0. ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }},nQ=class{constructor(e,t=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=Cn();this.outputShape=e,this.enableShapeUniforms=xn(this.outputShape.length);let a="",r="result";t&&(r="floor(result * 255. + 0.5)");for(let s=0;s<=1;s++)for(let i=0;i<=1;i++){let o=s*2+i;a+= `
2021-09-11 17:11:38 +02:00
localCoords = coords ;
2024-01-17 22:56:53 +01:00
if ( localCoords [ 2 ] + $ { i } < $ { this . enableShapeUniforms ? "outShape[2]" : ` ${ e [ 2 ] } ` } ) {
localCoords [ 2 ] += $ { i } ;
if ( localCoords [ 1 ] + $ { s } < $ { this . enableShapeUniforms ? "outShape[1]" : ` ${ e [ 1 ] } ` } ) {
localCoords [ 1 ] += $ { s } ;
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:56:53 +01:00
result [ $ { o } ] = values [ 0 ] ;
2021-09-11 17:11:38 +02:00
} else if ( offset == 1 ) {
2024-01-17 22:56:53 +01:00
result [ $ { o } ] = values [ 1 ] ;
2021-09-11 17:11:38 +02:00
} else if ( offset == 2 ) {
2024-01-17 22:56:53 +01:00
result [ $ { o } ] = values [ 2 ] ;
2021-09-11 17:11:38 +02:00
} else {
2024-01-17 22:56:53 +01:00
result [ $ { o } ] = values [ 3 ] ;
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
` }this.userCode= `
2024-01-17 22:56:53 +01:00
$ { this . enableShapeUniforms ? lk ( ) : ok ( 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:56:53 +01:00
$ { a }
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { n . output } = $ { r } ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},_A={};_e(_A,{bindVertexProgramAttributeStreams:()=>LA,createBufferFromOutputTexture:()=>BA,createFloat16MatrixTexture:()=>RA,createFloat16PackedMatrixTexture:()=>PA,createFloat32MatrixTexture:()=>DA,createIndexBuffer:()=> $ A,createPackedMatrixTexture:()=>OA,createUnsignedBytesMatrixTexture:()=>MA,createVertexBuffer:()=>FA,createVertexShader:()=>AA,downloadByteEncodedFloatMatrixFromOutputTexture:()=>UA,downloadFloat32MatrixFromBuffer:()=>VA,downloadMatrixFromPackedOutputTexture:()=>HA,downloadPackedMatrixFromBuffer:()=>GA,getInternalFormatForFloat16MatrixTexture:()=>ck,getInternalFormatForFloat16PackedMatrixTexture:()=>mk,getInternalFormatForFloat32MatrixTexture:()=>pk,getInternalFormatForPackedMatrixTexture:()=>hk,getInternalFormatForUnsignedBytesMatrixTexture:()=>dk,uploadDenseMatrixToTexture:()=>zA,uploadPixelDataToTexture:()=>WA});function AA(e){let t=Cn(),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:56:53 +01:00
} ` ;return nA(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 iA(e,t)}function $ A(e){let t=new Uint16Array([0,1,2,2,1,3]);return oA(e,t)}function Od(e,t,n,a,r,s){uA(t,n);let i=lA(e),o=e.TEXTURE_2D;return de(e,()=>e.bindTexture(o,i)),de(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),de(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),de(e,()=>e.texParameteri(o,e.TEXTURE_MIN_FILTER,e.NEAREST)),de(e,()=>e.texParameteri(o,e.TEXTURE_MAG_FILTER,e.NEAREST)),G().getNumber("WEBGL_VERSION")===1?de(e,()=>e.texImage2D(o,0,a,t,n,0,r,s,null)):de(e,()=>e.texStorage2D(o,1,a,t,n)),de(e,()=>e.bindTexture(e.TEXTURE_2D,null)),{texture:i,texShape:[n,t]}}function pk(e){return e.internalFormatFloat}function DA(e,t,n,a){let[r,s]=Md(t,n);return Od(e,r,s,pk(a),a.textureFormatFloat,e.FLOAT)}function ck(e){return e.internalFormatHalfFloat}function RA(e,t,n,a){let[r,s]=Md(t,n);return Od(e,r,s,ck(a),a.textureFormatFloat,a.textureTypeHalfFloat)}function dk(e){return e.downloadTextureFormat}function MA(e,t,n,a){let[r,s]=Md(t,n);return Od(e,r,s,dk(a),e.RGBA,e.UNSIGNED_BYTE)}function hk(e){return e.internalFormatPackedFloat}function OA(e,t,n,a){let[r,s]=vp(t,n);return Od(e,r,s,hk(a),e.RGBA,e.FLOAT)}function mk(e){return e.internalFormatPackedHalfFloat}function PA(e,t,n,a){let[r,s]=vp(t,n);return Od(e,r,s,mk(a),e.RGBA,a.textureTypeHalfFloat)}function LA(e,t,n){return de(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),bv(e,t,"clipSpacePos",n,3,20,0)&&bv(e,t,"uv",n,2,20,12)}function zA(e,t,n,a,r,s){de(e,()=>e.bindTexture(e.TEXTURE_2D,t));let i,o,l;r instanceof Uint8Array?(i=new Uint8Array(n*a*4),o=e.UNSIGNED_BYTE,l=e.RGBA):(i=new Float32Array(n*a*4),o=e.FLOAT,l=s.internalFormatPackedFloat),i.set(r),G().getNumber("WEBGL_VERSION")===2?de(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,a,e.RGBA,o,i)):de(e,()=>e.texImage2D(e.TEXTURE_2D,0,l,n,a,0,e.RGBA,o,i)),de(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function WA(e,t,n){de(e,()=>e.bindTexture(e.TEXTURE_2D,t)),n.data instanceof Uint8Array?G().getNumber("WEBGL_VERSION")===2?de(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n.width,n.height,e.RGBA,e.UNSIGNED_BYTE,n.data)):de(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?de(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,e.RGBA,e.UNSIGNED_BYTE,n)):de(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)),de(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function BA(e,t,n,a){let r=e.createBuffer();de(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,r));let s=4*4*t*n;return de(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,s,e.STREAM_READ)),de(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,0)),de(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,null)),r}function VA(e,t,n){let a=e,r=new Float32Array(n);return a.bindBuffer(a.PIXEL_PACK_BUFFER,t),a.getBufferSubData(a.PIXEL_PACK_BUFFER,0,r),a.bindBuffer(a.PIXEL_PACK_BUFFER,null),r}function UA(e,t,n,a){let[r,s]=Md(t,n),i=4,o=new Uint8Array(JJ(t*n,i));return de(e,()=>e.readPixels(0,0,r,s,a.downloadTextureFormat,e.UNSIGNED_BYTE,o)),new Float32Array(o.buffer)}function GA(e,t,n,a,r,s,i,o){let l=e,u=new Float32Array(QJ(s,i));return l.bindBuffer(l.PIXEL_PACK_BUFFER,t),l.getBufferSubData(l.PIXEL_PACK_BUFFER,0,u),l.bindBuffer(l.PIXEL_PACK_BUFFER,null),u}function HA(e,t,n){let a=new Float32Array(t*n*4);return de(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,a)),a}var em=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,Q_(t,e)):this.gl=qa(t),e=this.gl,G().getNumber("WEBGL_VERSION")===2){let r=e;this.createVertexArray=()=>de(r,()=>r.createVertexArray()),this.bindVertexArray=s=>de(r,()=>r.bindVertexArray(s)),this.deleteVertexArray=s=>de(r,()=>r.deleteVertexArray(s)),this.getVertexArray=()=>de(r,()=>r.getParameter(r.VERTEX_ARRAY_BINDING))}else if(e!=null){let r=e.getExtension("OES_vertex_array_object");if(r==null)throw new Error("All WebGL1 implementations are expected to offer OES_vertex_array_object.");this.createVertexArray=()=>d
2021-09-11 17:11:38 +02:00
void main ( ) {
setOutput ( vec4 ( getA ( ) , 0. , 0. , 0. ) ) ;
}
2024-01-17 22:56:53 +01:00
` ;else{let t=kn("rc",this.rank),n=ht(this.rank),a=this.getOutOfBoundsCondition(t),r=this.getSetup(t),s=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:56:53 +01:00
if ( $ { a } ) {
2021-09-11 17:11:38 +02:00
setOutput ( vec4 ( 0 ) ) ;
} else {
2024-01-17 22:56:53 +01:00
$ { r }
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
setOutput ( vec4 ( $ { s } ) ) ;
2021-04-01 19:39:54 +02:00
}
}
2024-01-17 22:56:53 +01:00
` }}getSourceCoordsArr(e){let t=[];for(let n=0;n<=1;n++)for(let a=0;a<=1;a++){let r= ` $ { n === 0 ? "r" : "rp1" } , $ { a === 0 ? "c" : "cp1" } ` ;for(let s=2;s<this.rank;s++)r= ` $ { e [ e . length - 1 - s ] } , ` +r;t.push(r)}return t}getOutOfBoundsCondition(e){if(this.rank===1)return ` rc > $ { this . enableShapeUniforms ? "outShape" : this . outputShape [ 0 ] } ` ;let t="";for(let n=this.rank-2;n<this.rank;n++)t+= ` $ { e [ n ] } >= $ { this . enableShapeUniforms ? ` outShape[ ${ n } ] ` : this . outputShape [ n ] } ` ,n<this.rank-1&&(t+="||");return t}getSetup(e){if(this.rank===1)return"";let t=e.slice(-2),n=this.enableShapeUniforms? ` outShape [ $ { this . rank } - 1 ] ` :this.outputShape[this.rank-1],a=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:56:53 +01:00
bool rEdge = rp1 >= $ { a } ;
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:56:53 +01:00
rEdge || cEdge ? 0. : getA ( $ { t [ 3 ] } ) ` }},YA=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 a=0;a<4;a++){let r="thisRC = rc;";a%2===1&&(r+="thisRC.z += 1;"),a>1&&(r+="thisRC.y += 1;"),n+= `
$ { r }
$ { a > 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:56:53 +01:00
result [ $ { a } ] =
2021-09-11 17:11:38 +02:00
getChannel ( getA ( inputRC . x , inputRC . y , inputRC . z ) , inputRCInnerDims ) ;
2024-01-17 22:56:53 +01:00
$ { a > 0 ? "}" : "" }
2021-09-11 17:11:38 +02:00
` }this.userCode= `
2024-01-17 22:56:53 +01:00
$ { ZQ ( t , this . enableShapeUniforms ) }
$ { this . enableShapeUniforms ? lk ( ) : ok ( 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:56:53 +01:00
` }};function ZQ(e,t){return `
2021-09-11 17:11:38 +02:00
ivec3 inputCoordsFromReshapedOutCoords ( int index ) {
2024-01-17 22:56:53 +01:00
$ { t ? c9 ( [ "r" , "c" , "d" ] , "inputShape" ) : Qo ( [ "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:56:53 +01:00
` }var JQ=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 a=CS(t,n),r=ES(e,a,n);r in this.freeTextures||(this.freeTextures[r]=[]),r in this.usedTextures||(this.usedTextures[r]=[]);let s=TS(e,a,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[r].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=s,this.log();let o=this.freeTextures[r].pop();return this.usedTextures[r].push(o),o}let i;return a===dn.PACKED_2X2_FLOAT32?i=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):a===dn.PACKED_2X2_FLOAT16?i=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):a===dn.UNPACKED_FLOAT32?i=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):a===dn.UNPACKED_FLOAT16?i=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):a===dn.PACKED_4X1_UNSIGNED_BYTE&&(i=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[r].push(i),this.numUsedTextures++,this._numBytesAllocated+=s,this.log(),i}releaseTexture(e,t,n,a){if(this.freeTextures==null)return;let r=CS(n,a),s=ES(t,r,a);s in this.freeTextures||(this.freeTextures[s]=[]);let i=TS(t,r,this.gpgpu.gl,this.gpgpu.textureConfig,a),o=G().getNumber("WEBGL_DELETE_TEXTURE_THRESHOLD");o!==-1&&this._numBytesAllocated>o?(this.gpgpu.deleteMatrixTexture(e.texture),this._numBytesAllocated-=i):(this.freeTextures[s].push(e),this.numFreeTextures++,this._numBytesFree+=i),this.numUsedTextures--;let l=this.usedTextures[s],u=l&&l.indexOf(e);if(u==null||u<0)throw new Error("Cannot release a texture that was never provided by this texture manager");l[u]=l[l.length-1],l.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 QQ(e,t){let n=e;if(t===n.R32F)return 4;if(t===n.R16F)return 2;if(t===n.RGBA32F||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 TS(e,t,n,a,r){let s=eee(t,a),i;if(r){let[l,u]=vp(e[0],e[1]);i=l*u}else{let[l,u]=Md(e[0],e[1]);i=l*u}let o=QQ(n,s);return i*o}function eee(e,t){switch(e){case dn.PACKED_2X2_FLOAT32:return hk(t);case dn.PACKED_2X2_FLOAT16:return mk(t);case dn.UNPACKED_FLOAT32:return pk(t);case dn.UNPACKED_FLOAT16:return ck(t);case dn.PACKED_4X1_UNSIGNED_BYTE:return dk(t);default:throw new Error( ` Unknown physical texture type $ { e } ` )}}function tee(e){return G().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?dn.PACKED_2X2_FLOAT32:dn.UNPACKED_FLOAT32:e?dn.PACKED_2X2_FLOAT16:dn.UNPACKED_FLOAT16}function CS(e,t){if(e===da.UPLOAD)return dn.PACKED_2X2_FLOAT32;if(e===da.RENDER||e==null)return tee(t);if(e===da.DOWNLOAD||e===da.PIXELS)return dn.PACKED_4X1_UNSIGNED_BYTE;throw new Error( ` Unknown logical texture type $ { e } ` )}function ES(e,t,n){return ` $ { e [ 0 ] } _$ { e [ 1 ] } _$ { t } _$ { n } ` }var ir=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-15 22:59:35 +02:00
}
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-04-15 22:59:35 +02:00
2021-09-11 17:11:38 +02:00
setOutput ( y ) ;
}
2024-01-17 22:56:53 +01:00
` }},Ma="if (isnan(x)) return x;",nee="return x;",_S="return abs(x);",aee="return (x >= 0.0) ? x : (exp(x) - 1.0);",ree=Ma+ `
2021-09-11 17:11:38 +02:00
return ( x < 0.0 ) ? 0.0 : x ;
2024-01-17 22:56:53 +01:00
` ,see=Ma+ `
2021-09-11 17:11:38 +02:00
return ( x < 0.0 ) ? 0.0 : min ( 6.0 , x ) ;
2024-01-17 22:56:53 +01:00
` ,es="return x;",iee="return 1.0 / (1.0 + exp(-1.0 * x));",oee="return x;",lee= `
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:56:53 +01:00
` ,uee= `
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:56:53 +01:00
` ,pee= `
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:56:53 +01:00
` ,cee="return 1.0 / (1.0 + exp(-1.0 * x));",ss=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:56:53 +01:00
` }},dee=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=kn("rc",t),a=ht(t),r=XQ(t,n),s=n.slice(-2),i=t<=1?"rc": ` vec2 ( $ { s . join ( "," ) } ) ` ;this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { a } rc = getOutputCoords ( ) ;
vec4 packedInput = getA ( $ { r } ) ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
setOutput ( getChannel ( packedInput , $ { i } ) ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},hee=fr.whereImpl,mee=1e-7,fee=1e-4,Nx={};function gee(e){return e in Nx||(Nx[e]={}),Nx[e]}var bee=G().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),yee=600;function xee(){return G().global.screen==null?1024:G().global.screen.height*G().global.screen.width*window.devicePixelRatio*yee/1024/1024}var gk=class ZA extends Pc{nextDataId(){return ZA.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 em)n=t;else{let a=qa(G().getNumber("WEBGL_VERSION"),t);n=new em(a)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{let a=qa(G().getNumber("WEBGL_VERSION"));n=new em(a),this.binaryCache=gee(G().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=n,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new JQ(this.gpgpu),this.numMBBeforeWarning=xee(),this.texData=new $ m(this,Ea())}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}writeTexture(t,n,a,r,s,i){let o=this.makeTensorInfo(n,a),l=this.texData.get(o.dataId);l.isPacked=!1,l.texture={texture:t,texShape:[r,s]},l.texShape=[r,s];let u=dc(n),p=new NS(u,!1,i),d=this.runWebGLProgram(p,[o],a,[[r,s]]);return d.shape=n,l.texture=null,this.disposeIntermediateTensorInfo(o),d.dataId}write(t,n,a){if((G().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||G().getBool("DEBUG"))&&this.checkNumericalProblems(t),a==="complex64"&&t!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");let r={id:this.nextDataId()};return this.texData.set(r,{shape:n,dtype:a,values:t,usage:da.UPLOAD,refCount:1}),r}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,a,r,s){if(G().getBool("DEBUG")&&this.checkNumericalProblems(n),r==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(t,{shape:a,dtype:r,values:n,usage:da.UPLOAD,refCount:s})}disposeIntermediateTensorInfo(t){this.disposeData(t.dataId)}readSync(t){let n=this.texData.get(t),{values:a,dtype:r,complexTensorInfos:s,slice:i,shape:o,isPacked:l}=n;if(i!=null){let c;l?c=new ss(o,es):c=new ir(o,es);let h=this.runWebGLProgram(c,[{dataId:t,shape:o,dtype:r}],r),m=this.readSync(h.dataId);return this.disposeIntermediateTensorInfo(h),m}if(a!=null)return this.convertAndCacheOnCPU(t);if(r==="string")return a;let u=this.activeTimers!=null,p;u&&(p=w.now());let d;if(r==="complex64"){let c=this.readSync(s.real.dataId),h=this.readSync(s.imag.dataId);d=T.mergeRealAndImagArrays(c,h)}else d=this.getValuesFromTexture(t);return u&&(this.downloadWaitMs+=w.now()-p),this.convertAndCacheOnCPU(t,d)}async read(t){if(this.pendingRead.has(t)){let m=this.pendingRead.get(t);return new Promise(f=>m.push(f))}let n=this.texData.get(t),{values:a,shape:r,slice:s,dtype:i,complexTensorInfos:o,isPacked:l}=n;if(s!=null){let m;l?m=new ss(r,es):m=new ir(r,es);let f=this.runWebGLProgram(m,[{dataId:t,shape:r,dtype:i}],i),g=this.read(f.dataId);return this.disposeIntermediateTensorInfo(f),g}if(a!=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 u=null,p;if(i!=="complex64"&&G().get("WEBGL_BUFFER_SUPPORTED")){p=this.decode(t);let m=this.texData.get(p.dataId);u=this.gpgpu.createBufferFromTexture(m.texture.texture,...Gh(r))}this.pendingRead.set(t,[]),i!=="complex64"&&await this.gpgpu.createAndWaitForFence();let d;if(i==="complex64"){let m=await Promise.all([this.read(o.real.dataId),this.read(o.imag.dataId)]),f=m[0],g=m[1];d=T.mergeRealAndImagArrays(f,g)}else if(u==null)d=this.getVa
2021-09-11 17:11:38 +02:00
if ( isnan ( a ) ) return a ;
if ( isnan ( b ) ) return b ;
2024-01-17 22:56:53 +01:00
` ,Si=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:56:53 +01:00
` }},tl= `
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:56:53 +01:00
` ,Tp=class{constructor(e,t,n,a=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=T.assertAndGetBroadcastShape(t,n);let r=this.outputShape.length;this.enableShapeUniforms=xn(r);let s="";if(a)if(r===0||w.sizeFromShape(this.outputShape)===1)s= `
2021-09-11 17:11:38 +02:00
result . y = 0. ;
result . z = 0. ;
result . w = 0. ;
2024-01-17 22:56:53 +01:00
` ;else if(s= `
$ { ht ( r ) } coords = getOutputCoords ( ) ;
` ,r===1)this.enableShapeUniforms?s+= `
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:56:53 +01:00
` :s+= `
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:56:53 +01:00
` ;else{let i=kn("coords",r);this.enableShapeUniforms?s+= `
2021-09-11 17:11:38 +02:00
bool nextRowOutOfBounds =
2024-01-17 22:56:53 +01:00
( $ { i [ r - 2 ] } + 1 ) >= outShape [ $ { r } - 2 ] ;
2021-09-11 17:11:38 +02:00
bool nextColOutOfBounds =
2024-01-17 22:56:53 +01:00
( $ { i [ r - 1 ] } + 1 ) >= outShape [ $ { r } - 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:56:53 +01:00
` :s+= `
2021-09-11 17:11:38 +02:00
bool nextRowOutOfBounds =
2024-01-17 22:56:53 +01:00
( $ { i [ r - 2 ] } + 1 ) >= $ { this . outputShape [ r - 2 ] } ;
2021-09-11 17:11:38 +02:00
bool nextColOutOfBounds =
2024-01-17 22:56:53 +01:00
( $ { i [ r - 1 ] } + 1 ) >= $ { this . outputShape [ r - 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:56:53 +01:00
$ { s }
2021-04-01 19:39:54 +02:00
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` }};function aa(e){let{inputs:t,backend:n}=e,{x:a}=t;return n.incRef(a.dataId),{dataId:a.dataId,shape:a.shape,dtype:a.dtype}}var Iee={kernelName:no,backendName:"webgl",kernelFunc:aa};function Ms(e){let{inputs:t,backend:n}=e,{real:a,imag:r}=t,s=n.makeTensorInfo(a.shape,"complex64"),i=n.texData.get(s.dataId),o=aa({inputs:{x:a},backend:n}),l=aa({inputs:{x:r},backend:n});return i.complexTensorInfos={real:o,imag:l},s}var See={kernelName:Mm,backendName:"webgl",kernelFunc:Ms},QA="return (a < 0.) ? b * a : a;",eF= `
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:56:53 +01:00
` ;function Nee(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{alpha:s}=a,i=n.makeTensorInfo([],"float32",w.createScalarValue(s,"float32")),o=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Tp(eF,r.shape,i.shape):new Si(QA,r.shape,i.shape),l=n.runWebGLProgram(o,[r,i],"float32");return n.disposeIntermediateTensorInfo(i),l}var Tee={kernelName:io,backendName:"webgl",kernelFunc:Nee},tF="return (a < 0.) ? b * a : a;",nF= `
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:56:53 +01:00
` ;function Cee(e){let{inputs:t,backend:n}=e,{x:a,alpha:r}=t,s=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Tp(nF,a.shape,r.shape):new Si(tF,a.shape,r.shape);return n.runWebGLProgram(s,[a,r],"float32")}var Eee={kernelName:Io,backendName:"webgl",kernelFunc:Cee},Cp="if (isnan(x)) return x;";function Ze({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:a}){return({inputs:r,backend:s})=>{let{x:i}=r,o=s,l=a||i.dtype;if(o.shouldExecuteOnCPU([i])&&n!=null){let d=o.texData.get(i.dataId),c=n(d.values,l);return o.makeTensorInfo(i.shape,l,c)}let u=G().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,p;return u?p=new ss(i.shape,t):p=new ir(i.shape,e),o.runWebGLProgram(p,[i],l)}}function fn({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:a=!1,cpuKernelImpl:r,dtype:s}){return({inputs:i,backend:o})=>{let{a:l,b:u}=i,p=o;if(a&&l.dtype==="complex64"){let m=p.texData.get(l.dataId),f=p.texData.get(u.dataId),[g,b]=[[m.complexTensorInfos.real,f.complexTensorInfos.real],[m.complexTensorInfos.imag,f.complexTensorInfos.imag]].map(x=>{let[v,I]=x,N={dataId:v.dataId,dtype:v.dtype,shape:l.shape},C={dataId:I.dataId,dtype:I.dtype,shape:u.shape},_=new Si(e,l.shape,u.shape);return p.runWebGLProgram(_,[N,C],ga(v.dtype,I.dtype))}),y=Ms({inputs:{real:g,imag:b},backend:p});return p.disposeIntermediateTensorInfo(g),p.disposeIntermediateTensorInfo(b),y}let d=s||ga(l.dtype,u.dtype);if((l.dtype==="string"||u.dtype==="string"||p.shouldExecuteOnCPU([l,u]))&&r!=null){let m=p.texData.get(l.dataId).values,f=p.texData.get(u.dataId).values,g=l.dtype==="string"?T.fromUint8ToStringArray(m):m,b=l.dtype==="string"?T.fromUint8ToStringArray(f):f,[y,x]=r(l.shape,u.shape,g,b,d),v=p.makeTensorInfo(x,d),I=p.texData.get(v.dataId);return I.values=y,v}let c=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,h;return c?h=new Tp(t,l.shape,u.shape,n):h=new Si(e,l.shape,u.shape),p.runWebGLProgram(h,[l,u],d)}}function Dc(e,t=!1){if(e==="linear")return t?oee:nee;if(e==="relu")return t?uee:ree;if(e==="elu")return t?lee:aee;if(e==="relu6")return t?pee:see;if(e==="prelu")return t?nF:tF;if(e==="leakyrelu")return t?eF:QA;if(e==="sigmoid")return t?cee:iee;throw new Error( ` Activation $ { e } has not been implemented for the WebGL backend . ` )}var aF=class{constructor(e,t,n,a=!1,r=!1,s=!1,i=null,o=!1,l=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n,this.enableShapeUniforms=xn(this.outputShape.length);let u=a?e[1]:e[2],p=Math.ceil(u/2),d=a?"i * 2, rc.y":"rc.y, i * 2",c=r?"rc.z, i * 2":"i * 2, rc.z",h=a?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],m=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],f="",g="";i&&(o?f= ` vec4 activation ( vec4 a ) {
2021-09-11 17:11:38 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
2024-01-17 22:56:53 +01:00
$ { i }
} ` :l?f= ` vec4 activation ( vec4 a ) {
2021-09-11 17:11:38 +02:00
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
2024-01-17 22:56:53 +01:00
$ { i }
} ` :f= ` vec4 activation ( vec4 x ) {
$ { i }
} ` ,g="result = activation(result);");let b=s?"result += getBiasAtOutCoords();":"";s&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),l&&this.variableNames.push("leakyreluAlpha");let y="rc.x",x="rc.x";e[0]<t[0]?y= ` imod ( rc . x , $ { e [ 0 ] } ) ` :t[0]<e[0]&&(x= ` imod ( rc . x , $ { t [ 0 ] } ) ` ),this.userCode= `
$ { f }
2021-09-11 17:11:38 +02:00
// Don't use uniform for sharedDimensionPacked for performance.
2024-01-17 22:56:53 +01:00
const float sharedDimension = $ { p } . 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:56:53 +01:00
int batchB = $ { x } ;
for ( int i = 0 ; i < $ { p } ; i ++ ) {
vec4 a = getMatrixA ( batchA , $ { d } ) ;
vec4 b = getMatrixB ( batchB , $ { c } ) ;
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:56:53 +01:00
result += ( $ { h [ 0 ] } * $ { m [ 0 ] } ) ;
result += ( $ { h [ 1 ] } * $ { m [ 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:56:53 +01:00
$ { g }
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` }},AS={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},FS=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:56:53 +01:00
` }}, $ S="return a * b;";function yk(e){let{inputs:t,backend:n}=e,{a,b:r}=t,s=T.upcastType(a.dtype,r.dtype);if(a.dtype==="complex64"){let o=n.texData.get(a.dataId),l=n.texData.get(r.dataId),u=new FS(AS.REAL,a.shape,r.shape),p=new FS(AS.IMAG,a.shape,r.shape),d=[{dataId:o.complexTensorInfos.real.dataId,dtype:o.complexTensorInfos.real.dtype,shape:a.shape},{dataId:o.complexTensorInfos.imag.dataId,dtype:o.complexTensorInfos.imag.dtype,shape:a.shape},{dataId:l.complexTensorInfos.real.dataId,dtype:l.complexTensorInfos.real.dtype,shape:r.shape},{dataId:l.complexTensorInfos.imag.dataId,dtype:l.complexTensorInfos.imag.dtype,shape:r.shape}],c=n.runWebGLProgram(u,d,"float32"),h=n.runWebGLProgram(p,d,"float32"),m=Ms({inputs:{real:c,imag:h},backend:n});return n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(h),m}if(n.shouldExecuteOnCPU([a,r])){let o=n.texData.get(a.dataId),l=n.texData.get(r.dataId),[u,p]=NQ(a.shape,r.shape,o.values,l.values,s),d=n.makeTensorInfo(p,s),c=n.texData.get(d.dataId);return c.values=u,d}let i;return G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?i=new Tp( $ S,a.shape,r.shape):i=new Si( $ S,a.shape,r.shape),n.runWebGLProgram(i,[a,r],s)}var _ee={kernelName:xo,backendName:"webgl",kernelFunc:yk};function Aee(e,t,n){let a=[ki(e.shape),...Ii(e.shape)],r={dtype:e.dtype,shape:a,dataId:e.dataId},s=[ki(t),...Ii(t)],i=new YA(s,a),o=!0,l=[a],u=n.runWebGLProgram(i,[r],e.dtype,l,o);return{dataId:u.dataId,shape:t,dtype:u.dtype}}function ce(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{shape:s}=a,i=n,o=w.sizeFromShape(r.shape),l=w.inferFromImplicitShape(s,o),u=w.sizeFromShape(l);w.assert(o===u,()=> ` The new shape ( $ { l } ) has $ { u } elements and the old shape ( $ { r . shape } ) has $ { o } elements . The new shape and old shape must have the same number of elements . ` );let p=i.texData.get(r.dataId);return p.isPacked&&! $ c(r.shape,l)&&!(p.texture!==null&& $ c(p.shape,l))?Aee(r,l,i):(i.incRef(r.dataId),{dataId:r.dataId,shape:l,dtype:r.dtype})}var Fee={kernelName:ju,backendName:"webgl",kernelFunc:ce},DS=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:a,inSize:r,outSize:s}=e;this.outputShape=[a,s];let i=Math.floor(n/4)*4,o=n%4,l="sumValue += dot(values, ones);";if(t!=null){let p=1/t;l= ` sumValue += dot ( values * $ { w . isInt ( p ) ? p . toPrecision ( 2 ) : p } , ones ) ; ` }let u="";r%n>0&&(u= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
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:56:53 +01:00
$ { u }
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:56:53 +01:00
for ( int i = 0 ; i < $ { i } ; 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:56:53 +01:00
$ { l }
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
int inIdx = inOffset + $ { i } ;
if ( $ { o === 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:56:53 +01:00
$ { l }
} else if ( $ { o === 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:56:53 +01:00
$ { l }
} else if ( $ { o === 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:56:53 +01:00
$ { l }
2021-09-11 17:11:38 +02:00
}
setOutput ( sumValue ) ;
}
2024-01-17 22:56:53 +01:00
` }}, $ ee=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:a,inSize:r,outSize:s}=e;this.outputShape=[a,s];let i="0.0",o="";t==="prod"?i="1.0":t==="min"?(i="1.0 / 1e-20",o="min"):t==="max"&&(i="-1.0 / 1e-20",o="max");let l= ` $ { t } ( $ { t } ( $ { t } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;t==="sum"?l="sumValue":t==="prod"?l="prodValue":t==="all"?l="allValue":t==="any"&&(l="anyValue");let u=Math.floor(n/4)*4,p=n%4,d= `
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:56:53 +01:00
minMaxValue = $ { o } ( values , minMaxValue ) ;
2021-09-11 17:11:38 +02:00
if ( $ { t === "min" } || $ { t === "max" } ) {
2024-01-17 22:56:53 +01:00
minMaxValue = $ { o } ( 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:56:53 +01:00
` ,c="vec4";t==="all"?(i="1.0",d= `
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:56:53 +01:00
` ,c="bvec4"):t==="any"&&(i="0.0",d= `
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:56:53 +01:00
` ,c="bvec4");let h="";r%n>0&&(h= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
2021-09-11 17:11:38 +02:00
return initializationValue ;
}
` ),this.userCode= `
2024-01-17 22:56:53 +01:00
const float initializationValue = $ { i } ;
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:56:53 +01:00
vec4 minMaxValue = vec4 ( $ { i } ) ;
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:56:53 +01:00
for ( int i = 0 ; i < $ { u } ; i += 4 ) {
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + i ;
2024-01-17 22:56:53 +01:00
$ { c } values = $ { c } (
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:56:53 +01:00
$ { d }
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
int inIdx = inOffset + $ { u } ;
if ( $ { p === 1 } ) {
$ { c } values = $ { c } (
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:56:53 +01:00
$ { d }
} else if ( $ { p === 2 } ) {
$ { c } values = $ { c } (
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:56:53 +01:00
$ { d }
} else if ( $ { p === 3 } ) {
$ { c } values = $ { c } (
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:56:53 +01:00
$ { d }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
setOutput ( $ { l } ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function Dee(e){let t=[];for(;t.length===0||t[t.length-1].outSize!==1;){let n=t.length?t[t.length-1].outSize:e[1],a=T.computeOptimalWindowSize(n);t.push({inSize:n,windowSize:a,outSize:Math.ceil(n/a)})}return t}function nl(e,t,n,a){let r=Dee(e.shape),s=e;for(let i=0;i<r.length;i++){let{inSize:o,windowSize:l,outSize:u}=r[i],p,d;n==="mean"?p=i===0?new DS({windowSize:l,inSize:o,batchSize:e.shape[0],outSize:u},o):new DS({windowSize:l,inSize:o,batchSize:e.shape[0],outSize:u}):p=new $ ee({windowSize:l,inSize:o,batchSize:e.shape[0],outSize:u},n),d=s,s=a.runWebGLProgram(p,[s],t),d.dataId!==e.dataId&&a.disposeIntermediateTensorInfo(d)}return s}var Ree=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[t[s]];this.outputShape=n,this.rank=n.length;let a=ht(this.rank),r=Mee(t);this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { a } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { r } ) ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function Mee(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"],a=new Array(t);for(let r=0;r<e.length;r++)a[e[r]]=n[r];return a.join()}var Oee=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0;let n=new Array(e.length);for(let u=0;u<n.length;u++)n[u]=e[t[u]];if(this.outputShape=n,this.rank=n.length,this.rank>6)throw Error( ` Packed transpose for rank $ { this . rank } is not yet supported . ` );let a=ht(this.rank),r=XA("rc",this.rank),s=new Array(this.rank);for(let u=0;u<t.length;u++)s[t[u]]=r[u];let i= ` vec2 ( $ { s . slice ( - 2 ) . join ( ) } ) ` ,o= ` ++ $ { r [ this . rank - 1 ] } < $ { n [ this . rank - 1 ] } ` ,l= ` getChannel ( getA ( $ { s . join ( ) } ) , $ { i } ) ` ;this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { a } rc = getOutputCoords ( ) ;
2021-09-11 17:11:38 +02:00
vec4 result = vec4 ( 0. ) ;
2024-01-17 22:56:53 +01:00
result [ 0 ] = $ { l } ;
if ( $ { o } ) {
result [ 1 ] = $ { l } ;
}
-- $ { r [ this . rank - 1 ] } ;
if ( ++ $ { r [ this . rank - 2 ] } < $ { n [ this . rank - 2 ] } ) {
result [ 2 ] = $ { l } ;
if ( $ { o } ) {
result [ 3 ] = $ { l } ;
2021-04-15 22:59:35 +02:00
}
}
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
2021-04-15 22:59:35 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function eg(e,t,n){let a=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Oee(e.shape,t):new Ree(e.shape,t);return n.runWebGLProgram(a,[e],e.dtype)}function Pee(e,t,n,a){let r=t,s=e.shape.length,i=w.parseAxisParam(r,e.shape),o=i,l=T.getAxesPermutation(o,s),u=l!=null,p=e;u&&(p=eg(e,l,a),o=T.getInnerMostAxes(o.length,s)),T.assertAxesAreInnerMostDims("sum",o,s);let[d,c]=T.computeOutAndReduceShapes(p.shape,o),h=d;n&&(h=T.expandShapeToKeepDim(d,i));let m=w.sizeFromShape(c),f=w.sizeFromShape(e.shape)/m,g=ce({inputs:{x:p},attrs:{shape:[f,m]},backend:a}),b=Xm(e.dtype),y=nl(g,b,"sum",a),x=ce({inputs:{x:y},attrs:{shape:h},backend:a});return a.disposeIntermediateTensorInfo(g),a.disposeIntermediateTensorInfo(y),u&&a.disposeIntermediateTensorInfo(p),x}function tg(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a;return Pee(r,s,i,n)}var Lee={kernelName:Wo,backendName:"webgl",kernelFunc:tg};function In(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{perm:s}=a,i=n,o=r.shape.length,l=new Array(o);for(let p=0;p<l.length;p++)l[p]=r.shape[s[p]];let u;if(i.shouldExecuteOnCPU([r])){let p=i.texData.get(r.dataId).values,d=fk(p,r.shape,r.dtype,s,l);u=i.makeTensorInfo(l,r.dtype);let c=i.texData.get(u.dataId);c.values=d}else u=eg(r,s,i);return u}var zee={kernelName:Er,backendName:"webgl",kernelFunc:In},rF=1e3;function Cm({a:e,b:t,transposeA:n,transposeB:a,backend:r,bias:s=null,preluActivationWeights:i=null,leakyreluAlpha:o=0,activation:l=null}){let u=e.shape.length,p=t.shape.length,d=n?e.shape[u-2]:e.shape[u-1],c=a?t.shape[p-1]:t.shape[p-2],h=n?e.shape[u-1]:e.shape[u-2],m=a?t.shape[p-2]:t.shape[p-1],f=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(f),y=w.sizeFromShape(g),x=cp.assertAndGetBroadcastShape(e.shape.slice(0,-2),t.shape.slice(0,-2)).concat([h,m]);w.assert(d===c,()=> ` Error in matMul : inner shapes ( $ { d } ) and ( $ { c } ) of Tensors with shapes $ { e . shape } and $ { t . shape } and transposeA = $ { n } and transposeB = $ { a } must match . ` );let v=n?[b,d,h]:[b,h,d],I=a?[y,m,c]:[y,c,m],N=ce({inputs:{x:e},backend:r,attrs:{shape:v}}),C=ce({inputs:{x:t},backend:r,attrs:{shape:I}}),_=[N,C],F=Math.max(b,y),D=n?N.shape[1]:N.shape[2], $ =s!=null,S=i!=null,M=l==="leakyrelu",B=l!=null?Dc(l,!0):null,U= $ ||S||M||B!=null,H;if((h===1||m===1)&&D>rF&&U===!1){let K=N,Z=C;n&&(K=In({inputs:{x:N},backend:r,attrs:{perm:[0,2,1]}}),_.push(K)),a&&(Z=In({inputs:{x:C},backend:r,attrs:{perm:[0,2,1]}}),_.push(Z));let J=m!==1,ee=m===1,ae=K;J&&(ae=ce({inputs:{x:K},backend:r,attrs:{shape:[F,D,1]}}),_.push(ae));let te=m===1?2:1,se=Z;ee&&(se=ce({inputs:{x:Z},backend:r,attrs:{shape:[F,1,D]}}),_.push(se));let ie=yk({inputs:{a:ae,b:se},backend:r});H=tg({inputs:{x:ie},backend:r,attrs:{axis:te,keepDims:!0}}),_.push(ie)}else{let K=ga(e.dtype,t.dtype),Z=new aF(v,I,[F,h,m],n,a, $ ,B,S,M),J=[N,C];if(s!=null&&J.push(s),S&&J.push(i),M){let ee=r.makeTensorInfo([],"float32",w.createScalarValue(o,"float32"));J.push(ee),_.push(ee)}H=r.runWebGLProgram(Z,J,K)}let j=ce({inputs:{x:H},backend:r,attrs:{shape:x}});_.push(H);for(let K of _)r.disposeIntermediateTensorInfo(K);return j}function Wee(e){let{inputs:t,backend:n,attrs:a}=e,{a:r,b:s,bias:i,preluActivationWeights:o}=t,{transposeA:l,transposeB:u,activation:p,leakyreluAlpha:d}=a;return Cm({a:r,b:s,transposeA:l,transposeB:u,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:d,activation:p})}var Bee={kernelName:oi,backendName:"webgl",kernelFunc:Wee},RS="return abs(x);";function Vee(e){let{inputs:t,backend:n}=e,{x:a}=t;if(n.shouldExecuteOnCPU([a])&&a.dtype!=="complex64"){let s=n.texData.get(a.dataId),i=jA(s.values);return n.makeTensorInfo(a.shape,a.dtype,i)}let r;return G().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new ss(a.shape,RS):r=new ir(a.shape,RS),n.runWebGLProgram(r,[a],a.dtype)}var Uee={kernelName:uu,backendName:"webgl",kernelFunc:Vee},Gee=Ma+ `
2021-09-11 17:11:38 +02:00
if ( abs ( x ) > 1. ) {
return NAN ;
2021-04-15 22:59:35 +02:00
}
2021-09-11 17:11:38 +02:00
return acos ( x ) ;
2024-01-17 22:56:53 +01:00
` ,Hee=Ze({opSnippet:Gee}),qee={kernelName:Ci,backendName:"webgl",kernelFunc:Hee},jee=Ma+ `
2021-09-11 17:11:38 +02:00
if ( x < 1.0 ) return NAN ;
2024-01-17 22:56:53 +01:00
return log ( x + sqrt ( x * x - 1.0 ) ) ; ` ,Kee=Ze({opSnippet:jee}),Xee={kernelName:Ei,backendName:"webgl",kernelFunc:Kee},MS="return a + b;",Yee=fn({opSnippet:MS,packedOpSnippet:MS,supportsComplex:!0,cpuKernelImpl:rQ}),Zee={kernelName:Is,backendName:"webgl",kernelFunc:Yee},Jee=class{constructor(e,t){this.outputShape=[],this.outputShape=e,this.variableNames=t.map((r,s)=> ` T$ { s } ` );let n=[];this.variableNames.forEach(r=>{n.push( ` float v$ { r } = get$ { r } AtOutCoords ( ) ; ` )});let a=this.variableNames.map(r=> ` v$ { r } ` ).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:56:53 +01:00
float result = $ { a } ;
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},Qee=class{constructor(e,t){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.variableNames=t.map((r,s)=> ` T$ { s } ` );let n=[];this.variableNames.forEach(r=>{n.push( ` vec4 v$ { r } = get$ { r } AtOutCoords ( ) ; ` )});let a=this.variableNames.map(r=> ` v$ { r } ` ).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:56:53 +01:00
vec4 result = $ { a } ;
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function tm(e){let{inputs:t,backend:n}=e,a=t;if(a.length===1)return aa({inputs:{x:a[0]},backend:n});if(a.length>G().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let o=Math.floor(a.length/2),l=tm({inputs:a.slice(0,o),backend:n}),u=tm({inputs:a.slice(o),backend:n});return tm({inputs:[l,u],backend:n})}let r=a.map(o=>o.dtype).reduce((o,l)=>ga(o,l)),s=a.map(o=>o.shape),i=G().getBool("WEBGL_PACK")?new Qee(a[0].shape,s):new Jee(a[0].shape,s);return n.runWebGLProgram(i,a,r)}var ete={kernelName:_i,backendName:"webgl",kernelFunc:tm};function tte(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=w.parseAxisParam(s,r.shape),u=l,p=T.getAxesPermutation(u,o),d=r;p!=null&&(d=In({inputs:{x:r},backend:n,attrs:{perm:p}}),u=T.getInnerMostAxes(u.length,o)),T.assertAxesAreInnerMostDims("all",u,o);let[c,h]=T.computeOutAndReduceShapes(d.shape,u),m=w.sizeFromShape(h),f=ce({inputs:{x:d},backend:n,attrs:{shape:[-1,m]}}),g=nl(f,f.dtype,"all",n),b;if(i){let y=T.expandShapeToKeepDim(c,l);b=ce({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=ce({inputs:{x:g},backend:n,attrs:{shape:c}});return n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),p!=null&&n.disposeIntermediateTensorInfo(d),b}var nte={kernelName:pu,backendName:"webgl",kernelFunc:tte};function ate(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=w.parseAxisParam(s,r.shape),u=l,p=T.getAxesPermutation(u,o),d=r;p!=null&&(d=In({inputs:{x:r},backend:n,attrs:{perm:p}}),u=T.getInnerMostAxes(u.length,o)),T.assertAxesAreInnerMostDims("any",u,o);let[c,h]=T.computeOutAndReduceShapes(d.shape,u),m=w.sizeFromShape(h),f=ce({inputs:{x:d},backend:n,attrs:{shape:[-1,m]}}),g=nl(f,f.dtype,"any",n),b;if(i){let y=T.expandShapeToKeepDim(c,l);b=ce({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=ce({inputs:{x:g},backend:n,attrs:{shape:c}});return n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),p!=null&&n.disposeIntermediateTensorInfo(d),b}var rte={kernelName:cu,backendName:"webgl",kernelFunc:ate},ste=class{constructor(e,t,n){this.variableNames=["A"];let{windowSize:a,batchSize:r,outSize:s}=e;n||this.variableNames.push("bestIndicesA"),this.outputShape=[r,s];let i=t==="max"?">":"<",o=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:56:53 +01:00
int inOffset = outIdx * $ { a } ;
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:56:53 +01:00
for ( int i = 0 ; i < $ { a } ; i ++ ) {
int inIdx = $ { o } ;
2021-09-11 17:11:38 +02:00
float candidate = getA ( batch , inIdx ) ;
2024-01-17 22:56:53 +01:00
if ( candidate $ { i } 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:56:53 +01:00
` }},ite=class{constructor(e,t,n,a){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,w.assert(e.length>2,()=> ` Packed arg$ { n . charAt ( 0 ) . toUpperCase ( ) + n . slice ( 1 ) } supports only inputs with rank above 2. ` );let r=e[e.length-1],s=Math.ceil(r/t);this.outputShape=e.slice(0,-1),s>1&&this.outputShape.push(s),a||this.variableNames.push("bestIndicesA");let i=this.outputShape,o=i.length,l=ht(o),u=kn("coords",o),p,d;if(s===1){d=o+1;let C=ht(d);p= `
$ { C } sourceLocR = $ { C } ( $ { u . join ( ) } , 0 ) ;
++ $ { u [ o - 1 ] } ;
$ { C } sourceLocG = $ { C } ( $ { u . join ( ) } , 0 ) ;
++ $ { u [ o - 2 ] } ;
$ { C } sourceLocA = $ { C } ( $ { u . join ( ) } , 0 ) ;
-- $ { u [ o - 1 ] } ;
$ { C } sourceLocB = $ { C } ( $ { u . join ( ) } , 0 ) ;
-- $ { u [ o - 2 ] } ; ` }else d=o,p= `
$ { l } sourceLocR = coords ;
++ $ { u [ o - 1 ] } ;
$ { l } sourceLocG = coords ;
++ $ { u [ o - 2 ] } ;
$ { l } sourceLocA = coords ;
-- $ { u [ o - 1 ] } ;
$ { l } sourceLocB = coords ;
-- $ { u [ o - 2 ] } ; ` ;let c=["x","y","z","w","u","v"].slice(0,d),h="."+c[d-1],m=c.map(C=>"int "+C),f=kn("sourceLocR",d-1).concat("inIdx.r"),g=kn("sourceLocG",d-1).concat("inIdx.g"),b=kn("sourceLocB",d-1).concat("inIdx.b"),y=kn("sourceLocA",d-1).concat("inIdx.a"),x=n==="max"?"greaterThan":"lessThan",v=a?"": `
inIdx = round ( vec4 ( getBestIndicesAChannel ( $ { f . join ( ) } ) ,
getBestIndicesAChannel ( $ { g . join ( ) } ) ,
2023-03-21 13:00:18 +01:00
getBestIndicesAChannel ( $ { b . join ( ) } ) ,
2024-01-17 22:56:53 +01:00
getBestIndicesAChannel ( $ { y . join ( ) } ) ) ) ; ` ,I= ` vec4 (
getAChannel ( $ { f . join ( ) } ) ,
hasNextCol ? getAChannel ( $ { g . join ( ) } ) : 0. ,
2023-03-21 13:00:18 +01:00
hasNextRow ? getAChannel ( $ { b . join ( ) } ) : 0. ,
2024-01-17 22:56:53 +01:00
hasNextRow && hasNextCol ? getAChannel ( $ { y . join ( ) } ) : 0. ) ` ,N=a?"": `
float getBestIndicesAChannel ( $ { m . join ( ) } ) {
return getChannel ( getBestIndicesA ( $ { c . join ( ) } ) ,
vec2 ( $ { c . slice ( - 2 ) . join ( ) } ) ) ;
2021-09-11 17:11:38 +02:00
} ` ;this.userCode= `
2024-01-17 22:56:53 +01:00
float getAChannel ( $ { m . join ( ) } ) {
return getChannel ( getA ( $ { c . join ( ) } ) ,
vec2 ( $ { c . slice ( - 2 ) . join ( ) } ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
$ { N }
2021-04-01 19:39:54 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { l } coords = getOutputCoords ( ) ;
bool hasNextCol = $ { u [ o - 1 ] } < $ { i [ o - 1 ] - 1 } ;
bool hasNextRow = $ { u [ o - 2 ] } < $ { i [ o - 2 ] - 1 } ;
$ { p }
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:56:53 +01:00
vec4 bestValue = $ { I } ;
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:56:53 +01:00
$ { v }
vec4 candidate = $ { I } ;
2021-09-11 17:11:38 +02:00
bvec4 nan = isnan ( candidate ) ;
bvec4 replace = bvec4 (
2024-01-17 22:56:53 +01:00
vec4 ( $ { x } ( 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:56:53 +01:00
` }};function sF(e,t,n,a=null){let r=t.shape[0],s=t.shape[1];a!=null&&(r=a.shape[0],s=a.shape[1]);let i=T.computeOptimalWindowSize(s),o={windowSize:i,inSize:s,batchSize:r,outSize:Math.ceil(s/i)},l=new ste(o,n,a==null),u=[t];a!=null&&u.push(a);let p=e.runWebGLProgram(l,u,"int32");if(p.shape[1]===1)return p;let d=sF(e,t,n,p);return e.disposeIntermediateTensorInfo(p),d}function iF(e,t,n,a=null){let r=a!=null?a.shape:t.shape,s=r[r.length-1],i=T.computeOptimalWindowSize(s),o=new ite(r,i,n,a==null),l=a==null?[t]:[t,a],u=e.runWebGLProgram(o,l,"int32");if(u.shape.length===t.shape.length){let p=iF(e,t,n,u);return e.disposeIntermediateTensorInfo(u),p}return u}function oF(e,t,n,a){let r=[n];if(T.assertAxesAreInnerMostDims("arg"+a.charAt(0).toUpperCase()+a.slice(1),r,t.shape.length),!G().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let s=[],i=e.texData.get(t.dataId),o=i!==null&&i.isPacked,l=t;o&&(l=e.unpackTensor(t),s.push(l));let[u,p]=T.computeOutAndReduceShapes(l.shape,r),d=w.sizeFromShape(p),c=ce({inputs:{x:l},backend:e,attrs:{shape:[-1,d]}});s.push(c);let h=sF(e,c,a);s.push(h);let m=ce({inputs:{x:h},backend:e,attrs:{shape:u}});return s.forEach(f=>e.disposeIntermediateTensorInfo(f)),m}return iF(e,t,a)}function ote(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s}=a,i=w.parseAxisParam(s,r.shape),o=T.getAxesPermutation(i,r.shape.length),l=r,u=[];o!=null&&(l=In({inputs:{x:r},backend:n,attrs:{perm:o}}),u.push(l),i=T.getInnerMostAxes(i.length,l.shape.length)),T.assertAxesAreInnerMostDims("argMax",[i[0]],l.shape.length);let p=oF(n,l,i[0],"max");return u.forEach(d=>n.disposeIntermediateTensorInfo(d)),p}var lte={kernelName:du,backendName:"webgl",kernelFunc:ote};function ute(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s}=a,i=w.parseAxisParam(s,r.shape),o=T.getAxesPermutation(i,r.shape.length),l=r,u=[];o!=null&&(l=In({inputs:{x:r},backend:n,attrs:{perm:o}}),u.push(l),i=T.getInnerMostAxes(i.length,l.shape.length)),T.assertAxesAreInnerMostDims("argMin",[i[0]],l.shape.length);let p=oF(n,l,i[0],"min");return u.forEach(d=>n.disposeIntermediateTensorInfo(d)),p}var pte={kernelName:hu,backendName:"webgl",kernelFunc:ute},cte=Ma+ `
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:56:53 +01:00
` ,dte=Ze({opSnippet:cte}),hte={kernelName:Ai,backendName:"webgl",kernelFunc:dte},mte=Ma+"return log(x + sqrt(x * x + 1.0));",fte=Ze({opSnippet:mte}),gte={kernelName:Fi,backendName:"webgl",kernelFunc:fte},bte=Ma+ `
2021-09-11 17:11:38 +02:00
return atan ( x ) ;
2024-01-17 22:56:53 +01:00
` ,yte=Ze({opSnippet:bte}),xte={kernelName: $ i,backendName:"webgl",kernelFunc:yte},vte=bk+ `
2021-09-11 17:11:38 +02:00
return atan ( a , b ) ;
2024-01-17 22:56:53 +01:00
` ,wte= `
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:56:53 +01:00
` +tl+ `
2021-09-11 17:11:38 +02:00
return result ;
2024-01-17 22:56:53 +01:00
` ,kte=fn({opSnippet:vte,packedOpSnippet:wte}),Ite={kernelName:Ri,backendName:"webgl",kernelFunc:kte},Ste=Ma+ `
2021-09-11 17:11:38 +02:00
if ( ( x < - 1.0 ) || ( x > 1.0 ) ) return NAN ;
2024-01-17 22:56:53 +01:00
return ( log ( 1.0 + x ) - log ( 1.0 - x ) ) / 2.0 ; ` ,Nte=Ze({opSnippet:Ste}),Tte={kernelName:Di,backendName:"webgl",kernelFunc:Nte},Rc=class{constructor(e,t,n,a=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let s=e.filterWidth,i=e.strideHeight,o=e.strideWidth,l=e.dilationHeight,u=e.dilationWidth,p=e.effectiveFilterHeight,d=e.effectiveFilterWidth,c=e.padInfo.top,h=e.padInfo.left;this.outputShape=e.outShape;let m=t==="avg",f= ` ( ( batch * $ { e . inHeight } + xR ) * $ { e . inWidth } + xC ) * $ { e . inChannels } + d ` ,g= ` ( xR * $ { e . inWidth } + xC ) * $ { e . inChannels } + d ` ,b="0.0";if(m||(b="-1.0 / 1e-20"),n){let C=">=";this.userCode= `
const ivec2 strides = ivec2 ( $ { i } , $ { o } ) ;
const ivec2 pads = ivec2 ( $ { c } , $ { 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:56:53 +01:00
for ( int wR = 0 ; wR < $ { p } ;
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:56:53 +01:00
for ( int wC = 0 ; wC < $ { d } ;
wC += $ { u } ) {
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:56:53 +01:00
if ( value $ { C } currMinMaxValue ) {
2021-09-11 17:11:38 +02:00
minMaxValue = value ;
minMaxValueFound = 1.0 ;
2024-01-17 22:56:53 +01:00
minMaxPosition = $ { a ? r ? f : g : ` wR * ${ d } + 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:56:53 +01:00
` ;return}let y="max",x= ` $ { t } ( $ { t } ( $ { t } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;t==="avg"&&(x="avgValue / max(count, 1.0)");let v=Math.floor(s/4)*4,I=s%4,N= `
if ( $ { m } ) {
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:56:53 +01:00
const ivec2 strides = ivec2 ( $ { i } , $ { o } ) ;
const ivec2 pads = ivec2 ( $ { c } , $ { 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:56:53 +01:00
for ( int wR = 0 ; wR < $ { p } ;
wR += $ { l } ) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR ;
if ( xR < 0 || xR >= $ { e . inHeight } ) {
continue ;
}
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { v } ; wC += 4 ) {
int xC = xCCorner + wC * $ { u } ;
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
2024-01-17 22:56:53 +01:00
getValue ( batch , xR , xC + $ { u } , d ) ,
getValue ( batch , xR , xC + 2 * $ { u } , d ) ,
getValue ( batch , xR , xC + 3 * $ { u } , d )
2021-09-11 17:11:38 +02:00
) ;
2024-01-17 22:56:53 +01:00
$ { N }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
int xC = xCCorner + $ { v } ;
if ( $ { I === 1 } ) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2024-01-17 22:56:53 +01:00
$ { N }
} else if ( $ { I === 2 } ) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
2024-01-17 22:56:53 +01:00
getValue ( batch , xR , xC + $ { u } , d ) ,
2021-09-11 17:11:38 +02:00
initializationValue ,
initializationValue
) ;
2024-01-17 22:56:53 +01:00
$ { N }
} else if ( $ { I === 3 } ) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
2024-01-17 22:56:53 +01:00
getValue ( batch , xR , xC + $ { u } , d ) ,
getValue ( batch , xR , xC + 2 * $ { u } , d ) ,
2021-09-11 17:11:38 +02:00
initializationValue
) ;
2024-01-17 22:56:53 +01:00
$ { N }
2021-04-01 19:39:54 +02:00
}
}
2024-01-17 22:56:53 +01:00
setOutput ( $ { x } ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},xk=class{constructor(e,t,n,a=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let s=e.filterWidth,i=e.strideDepth,o=e.strideHeight,l=e.strideWidth,u=e.dilationDepth,p=e.dilationHeight,d=e.dilationWidth,c=e.effectiveFilterDepth,h=e.effectiveFilterHeight,m=e.effectiveFilterWidth,f=e.padInfo.front,g=e.padInfo.top,b=e.padInfo.left;this.outputShape=e.outShape;let y=t==="avg",x="0.0";if(y||(x="-1.0 / 1e-20"),n){let F=">=";this.userCode= `
2021-09-11 17:11:38 +02:00
const ivec3 strides =
2024-01-17 22:56:53 +01:00
ivec3 ( $ { i } , $ { o } , $ { l } ) ;
const ivec3 pads = ivec3 ( $ { f } , $ { g } , $ { 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:56:53 +01:00
for ( int wD = 0 ; wD < $ { c } ;
wD += $ { u } ) {
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:56:53 +01:00
wR += $ { p } ) {
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:56:53 +01:00
for ( int wC = 0 ; wC < $ { m } ;
wC += $ { d } ) {
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:56:53 +01:00
if ( value $ { F } currMinMaxValue ) {
2021-09-11 17:11:38 +02:00
minMaxValue = value ;
minMaxValueFound = 1.0 ;
2024-01-17 22:56:53 +01:00
minMaxPosition = $ { a ? r ? ` (((batch * ${ e . inDepth } + xD) * ${ e . inHeight } + xR) * ${ e . inWidth } + xC) * ${ e . inChannels } + ch ` : ` ((xD * ${ e . inHeight } + xR) * ${ e . inWidth } + xC) * ${ e . inChannels } + ch ` : ` wD * ${ h } * ${ m } +
wR * $ { m } + 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:56:53 +01:00
` ;return}let v="max",I= ` $ { t } ( $ { t } ( $ { t } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;t==="avg"&&(I="avgValue / max(count, 1.0)");let N=Math.floor(s/4)*4,C=s%4,_= `
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:56:53 +01:00
minMaxValue = $ { v } ( 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:56:53 +01:00
ivec3 ( $ { i } , $ { o } , $ { l } ) ;
const ivec3 pads = ivec3 ( $ { f } , $ { g } , $ { b } ) ;
const float initializationValue = $ { x } ;
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:56:53 +01:00
vec4 minMaxValue = vec4 ( $ { x } ) ;
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:56:53 +01:00
for ( int wD = 0 ; wD < $ { c } ;
wD += $ { u } ) {
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:56:53 +01:00
wR += $ { p } ) {
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:56:53 +01:00
for ( int wC = 0 ; wC < $ { N } ; wC += 4 ) {
int xC = xCCorner + wC * $ { d } ;
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
2024-01-17 22:56:53 +01:00
getValue ( batch , xD , xR , xC + $ { d } , ch ) ,
getValue ( batch , xD , xR , xC + 2 * $ { d } , ch ) ,
getValue ( batch , xD , xR , xC + 3 * $ { d } , ch )
2021-09-11 17:11:38 +02:00
) ;
2024-01-17 22:56:53 +01:00
$ { _ }
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
int xC = xCCorner + $ { N } ;
if ( $ { C === 1 } ) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2024-01-17 22:56:53 +01:00
$ { _ }
} else if ( $ { C === 2 } ) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
2024-01-17 22:56:53 +01:00
getValue ( batch , xD , xR , xC + $ { d } , ch ) ,
2021-09-11 17:11:38 +02:00
initializationValue ,
initializationValue
) ;
2024-01-17 22:56:53 +01:00
$ { _ }
} else if ( $ { C === 3 } ) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
2024-01-17 22:56:53 +01:00
getValue ( batch , xD , xR , xC + $ { d } , ch ) ,
getValue ( batch , xD , xR , xC + 2 * $ { d } , ch ) ,
2021-09-11 17:11:38 +02:00
initializationValue
) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { _ }
2021-04-01 19:39:54 +02:00
}
}
}
2024-01-17 22:56:53 +01:00
setOutput ( $ { I } ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function Cte(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t;wp(r,"avgPool");let{filterSize:s,strides:i,pad:o,dimRoundingMode:l}=a,u=1;w.assert(T.eitherStridesOrDilationsAreOne(i,u),()=> ` Error in avgPool : Either strides or dilations must be 1. Got strides $ { i } and dilations '${u}' ` );let p=T.computePool2DInfo(r.shape,s,i,u,o,l);if(p.filterWidth===1&&p.filterHeight===1&&w.arraysEqual(p.inShape,p.outShape))return aa({inputs:{x:r},backend:n});let d=new Rc(p,"avg",!1);return n.runWebGLProgram(d,[r],"float32")}var Ete={kernelName:Mi,backendName:"webgl",kernelFunc:Cte};function _te(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{filterSize:s,strides:i,pad:o,dimRoundingMode:l,dataFormat:u}=a,p=[1,1,1],d=T.computePool3DInfo(r.shape,s,i,p,o,l,u),c=new xk(d,"avg",!1);return n.runWebGLProgram(c,[r],"float32")}var Ate={kernelName:mu,backendName:"webgl",kernelFunc:_te},Fte=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,a=e.strideHeight,r=e.strideWidth,s=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterHeight,l=e.effectiveFilterWidth,u=o-1-e.padInfo.top,p=l-1-e.padInfo.left,d=1/(t*n);this.userCode= `
const ivec2 pads = ivec2 ( $ { u } , $ { p } ) ;
const float avgMultiplier = float ( $ { d } ) ;
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:56:53 +01:00
for ( int wR = 0 ; wR < $ { o } ;
wR += $ { s } ) {
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 || fract ( dyR ) > 0.0 ) {
2021-04-01 19:39:54 +02:00
continue ;
}
int idyR = int ( dyR ) ;
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { l } ;
wC += $ { i } ) {
float dyC = float ( dyCCorner + wC ) / $ { r } . 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:56:53 +01:00
` }}, $ te=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,a=e.filterWidth,r=e.strideDepth,s=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,l=e.dilationHeight,u=e.dilationWidth,p=e.effectiveFilterDepth,d=e.effectiveFilterHeight,c=e.effectiveFilterWidth,h=p-1-e.padInfo.front,m=d-1-e.padInfo.top,f=c-1-e.padInfo.left,g=1/(t*n*a);this.userCode= `
const ivec3 pads = ivec3 ( $ { h } , $ { m } , $ { f } ) ;
const float avgMultiplier = float ( $ { g } ) ;
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:56:53 +01:00
for ( int wD = 0 ; wD < $ { p } ;
wD += $ { o } ) {
float dyD = float ( dyDCorner + wD ) / $ { r } . 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:56:53 +01:00
for ( int wR = 0 ; wR < $ { d } ;
wR += $ { l } ) {
float dyR = float ( dyRCorner + wR ) / $ { s } . 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:56:53 +01:00
for ( int wC = 0 ; wC < $ { c } ;
wC += $ { u } ) {
float dyC = float ( dyCCorner + wC ) / $ { i } . 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:56:53 +01:00
` }};function Dte(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s}=t,i=s,{filterSize:o,strides:l,pad:u,dimRoundingMode:p}=a,d=[1,1,1],c=T.computePool3DInfo(i.shape,o,l,d,u,p),h=new $ te(c);return n.runWebGLProgram(h,[r],i.dtype)}var Rte={kernelName:Wc,backendName:"webgl",kernelFunc:Dte};function Mte(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s}=t,i=s;wp([r,s],"avgPoolGrad");let{filterSize:o,strides:l,pad:u}=a,p=T.computePool2DInfo(i.shape,o,l,1,u),d=new Fte(p);return n.runWebGLProgram(d,[r],i.dtype)}var Ote={kernelName:zc,backendName:"webgl",kernelFunc:Mte};function Pte(e){let{inputs:t,backend:n,attrs:a}=e,{a:r,b:s}=t,{transposeA:i,transposeB:o}=a;return Cm({a:r,b:s,transposeA:i,transposeB:o,backend:n})}var Lte={kernelName:Oi,backendName:"webgl",kernelFunc:Pte},zte=class{constructor(e,t,n,a,r,s){this.outputShape=[],this.variableNames=["x","mean","variance"],T.assertAndGetBroadcastShape(e,t),T.assertAndGetBroadcastShape(e,n);let i="0.0";a!=null&&(T.assertAndGetBroadcastShape(e,a),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="1.0";r!=null&&(T.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="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:56:53 +01:00
float offset = $ { i } ;
float scale = $ { o } ;
float inv = scale * inversesqrt ( variance + float ( $ { s } ) ) ;
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:56:53 +01:00
` }},Wte=class{constructor(e,t,n,a,r,s){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],T.assertAndGetBroadcastShape(e,t),T.assertAndGetBroadcastShape(e,n);let i="vec4(0.0)";a!=null&&(T.assertAndGetBroadcastShape(e,a),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="vec4(1.0)";r!=null&&(T.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode= `
2021-04-01 19:39:54 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
vec4 offset = $ { i } ;
vec4 scale = $ { o } ;
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:56:53 +01:00
vec4 inv = scale * inversesqrt ( variance + vec4 ( $ { s } ) ) ;
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:56:53 +01:00
` }},Bte=({inputs:e,backend:t,attrs:n})=>{let{x:a,mean:r,variance:s,offset:i,scale:o}=e;w.assert(r.shape.length===s.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),w.assert(i==null||r.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),w.assert(o==null||r.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:l}=n;l==null&&(l=.001);let u=[a,r,s],p=null;i!=null&&(p=i.shape,u.push(i));let d=null;o!=null&&(d=o.shape,u.push(o));let c=G().getBool("WEBGL_PACK_NORMALIZATION")?new Wte(a.shape,r.shape,s.shape,p,d,l):new zte(a.shape,r.shape,s.shape,p,d,l);return t.runWebGLProgram(c,u,u[0].dtype)},Vte={kernelName:eo,backendName:"webgl",kernelFunc:Bte},Ute=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=Gte(this.rank),a,r=e.map((s,i)=> ` sourceLoc . $ { wv [ i ] } = start [ $ { i } ] + coords . $ { wv [ i ] } ; ` );a= `
2021-09-11 17:11:38 +02:00
$ { t } sourceLoc ;
$ { t } coords = getOutputCoords ( ) ;
2024-01-17 22:56:53 +01:00
$ { r . 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:56:53 +01:00
$ { a }
2021-09-11 17:11:38 +02:00
setOutput ( getSource ( $ { n } ) ) ;
}
2024-01-17 22:56:53 +01:00
` }},wv=["x","y","z","w","u","v"];function Gte(e){if(e===1)return"sourceLoc";if(e<=6)return wv.slice(0,e).map(t=>"sourceLoc."+t).join(",");throw Error( ` Slicing for rank $ { e } is not yet supported ` )}var Hte=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=kn("coords",this.rank),a=kn("sourceLoc",this.rank),r=this.rank===1?"sourceLoc": ` vec2 ( $ { a . slice ( - 2 ) . join ( ) } ) ` ,s= ` getChannel ( getSource ( $ { a . join ( ) } ) , $ { r } ) ` ,i= `
result . x = $ { s } ;
2021-09-11 17:11:38 +02:00
if ( ++ $ { n [ this . rank - 1 ] } < $ { e [ this . rank - 1 ] } ) {
2024-01-17 22:56:53 +01:00
++ $ { a [ this . rank - 1 ] } ;
result . y = $ { s } ;
-- $ { a [ this . rank - 1 ] } ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ,o=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:56:53 +01:00
++ $ { a [ this . rank - 2 ] } ;
result . z = $ { s } ;
2021-09-11 17:11:38 +02:00
if ( ++ $ { n [ this . rank - 1 ] } < $ { e [ this . rank - 1 ] } ) {
2024-01-17 22:56:53 +01:00
++ $ { a [ this . rank - 1 ] } ;
result . w = $ { s } ;
2021-09-11 17:11:38 +02:00
}
}
2024-01-17 22:56:53 +01:00
` ,l=this.rank<=4? ` sourceLoc = coords +
$ { t } ( $ { e . map ( ( u , p ) => ` start[ ${ p } ] ` ) . join ( ) } ) ; ` :e.map((u,p)=> ` $ { a [ p ] } = $ { n [ p ] } + start [ $ { p } ] ; ` ).join( `
2021-09-11 17:11:38 +02:00
` );this.userCode= `
void main ( ) {
$ { t } coords = getOutputCoords ( ) ;
$ { t } sourceLoc ;
2024-01-17 22:56:53 +01:00
$ { l }
2021-09-11 17:11:38 +02:00
vec4 result = vec4 ( 0. ) ;
2024-01-17 22:44:24 +01:00
$ { i }
2024-01-17 22:56:53 +01:00
$ { o }
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` }};function qte(e,t,n,a){let r=a.texData.get(e.dataId),s=a.makeTensorInfo(n,e.dtype),i=a.texData.get(s.dataId);Object.assign(i,r),i.refCount=1,i.shape=n,i.dtype=e.dtype;let o=Kt.computeFlatOffset(t,w.computeStrides(e.shape));r.slice&&(o+=r.slice.flatOffset),i.slice={flatOffset:o,origDataId:r.slice&&r.slice.origDataId||e.dataId};let l=a.dataRefCount.get(i.slice.origDataId)||1;return a.dataRefCount.set(i.slice.origDataId,l+1),s}function Ep(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{begin:s,size:i}=a,[o,l]=Kt.parseSliceParams(r,s,i);if(Kt.assertParamsValid(r,o,l),w.sizeFromShape(l)===0)return n.makeTensorInfo(l,r.dtype,[]);if(n.shouldExecuteOnCPU([r])||r.dtype==="string"){let d=n.texData.get(r.dataId),c=OQ(d.values,o,l,r.shape,r.dtype);return n.makeTensorInfo(l,r.dtype,c)}let{isPacked:u}=n.texData.get(r.dataId),p=Kt.isSliceContinous(r.shape,o,l);if(u||!p){let d=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Hte(l):new Ute(l),c=[o];return n.runWebGLProgram(d,[r],r.dtype,c)}return n.uploadToGPU(r.dataId),qte(r,o,l,n)}var jte={kernelName:ep,backendName:"webgl",kernelFunc:Ep},Kte=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{blockShape:s,crops:i}=a;w.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let o=s.reduce((y,x)=>y*x),l=T.getReshaped(r.shape,s,o),u=T.getPermuted(l.length,s.length),p=T.getReshapedPermuted(r.shape,s,o),d=T.getSliceBeginCoords(i,s.length),c=T.getSliceSize(p,i,s.length),h=[],m=ce({inputs:{x:r},backend:n,attrs:{shape:l}}),f=In({inputs:{x:m},backend:n,attrs:{perm:u}}),g=ce({inputs:{x:f},backend:n,attrs:{shape:p}}),b=Ep({inputs:{x:g},backend:n,attrs:{begin:d,size:c}});return h.push(m),h.push(f),h.push(g),h.forEach(y=>n.disposeIntermediateTensorInfo(y)),b},Xte={kernelName:fu,backendName:"webgl",kernelFunc:Kte};function Yte(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,weights:s}=t,{size:i}=a,o=n.readSync(r.dataId),l=n.readSync(s.dataId),u=qA(o,l,s.dtype,s.shape,i);return n.makeTensorInfo([i],s.dtype,u)}var Zte={kernelName:gu,backendName:"webgl",kernelFunc:Yte},Jte= `
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:56:53 +01:00
` ,Qte= `
2023-06-12 18:02:21 +02:00
return float ( int ( a . r ) & int ( b . r ) ) ;
2024-01-17 22:56:53 +01:00
` ;function ene(e){let{inputs:t,backend:n}=e,{a,b:r}=t,s=G().getBool("WEBGL_PACK_BINARY_OPERATIONS"),i=G().getNumber("WEBGL_VERSION");if(n.shouldExecuteOnCPU([a,r])||i===1){let l=n.texData.get(a.dataId).values,u=n.texData.get(r.dataId).values,[p,d]=iQ(a.shape,r.shape,l,u,a.dtype),c=n.makeTensorInfo(d,a.dtype),h=n.texData.get(c.dataId);return h.values=p,c}let o;return s?o=new Tp(Jte,a.shape,r.shape,!1):o=new Si(Qte,a.shape,r.shape),n.runWebGLProgram(o,[a,r],a.dtype)}var tne={kernelName:bu,backendName:"webgl",kernelFunc:ene};function nne(e){let{inputs:t,backend:n}=e,{s0:a,s1:r}=t,s=n.readSync(a.dataId),i=n.readSync(r.dataId),o=T.assertAndGetBroadcastShape(Array.from(s),Array.from(i));return n.makeTensorInfo([o.length],"int32",Int32Array.from(o))}var ane={kernelName:Bc,backendName:"webgl",kernelFunc:nne},rne="return float(a != b);",lF=fn({opSnippet:rne,cpuKernelImpl:CQ,dtype:"bool"}),sne={kernelName:Bu,backendName:"webgl",kernelFunc:lF};function Pd(e){let{inputs:t,backend:n}=e,{input:a}=t,r=n.texData.get(a.dataId);return aa({inputs:{x:r.complexTensorInfos.real},backend:n})}var ine={kernelName:jm,backendName:"webgl",kernelFunc:Pd},one="return float(int(x));";function lne(e,t){let n=new ir(e.shape,one),a=t.runWebGLProgram(n,[e],"int32");return{dataId:a.dataId,shape:a.shape,dtype:a.dtype}}function kv(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{dtype:s}=a;if(s==="complex64"){if(r.dtype==="complex64")return aa({inputs:{x:r},backend:n});let i=It(r.shape),o=kv({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),l=Ms({inputs:{real:o,imag:i},backend:n});return i.dispose(),n.disposeIntermediateTensorInfo(o),l}if(r.dtype==="complex64"){let i=Pd({inputs:{input:r},backend:n}),o=kv({inputs:{x:i},backend:n,attrs:{dtype:s}});return n.disposeIntermediateTensorInfo(i),o}if(!w.hasEncodingLoss(r.dtype,s)){let i=aa({inputs:{x:r},backend:n});return{dataId:i.dataId,shape:i.shape,dtype:s}}if(n.shouldExecuteOnCPU([r])){let i=n.texData.get(r.dataId).values,[o,l,u]=oQ(i,r.shape,r.dtype,s);return n.makeTensorInfo(o,l,u)}if(s==="int32")return lne(r,n);if(s==="bool"){let i=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),o=lF({inputs:{a:r,b:i},backend:n});return n.disposeIntermediateTensorInfo(i),o}throw new Error( ` Error in Cast : failed to cast $ { r . dtype } to $ { s } ` )}var une={kernelName:Pi,backendName:"webgl",kernelFunc:kv},OS="return ceil(x);",pne=Ze({opSnippet:OS,packedOpSnippet:OS,cpuKernelImpl:lQ}),cne={kernelName:Li,backendName:"webgl",kernelFunc:pne},dne=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:56:53 +01:00
` }},hne=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:56:53 +01:00
` }};function mne(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{clipValueMin:s,clipValueMax:i}=a,o;G().getBool("WEBGL_PACK_CLIP")?o=new hne(r.shape):o=new dne(r.shape);let l=[[s],[i]];return n.runWebGLProgram(o,[r],r.dtype,l)}var fne={kernelName:Ss,backendName:"webgl",kernelFunc:mne},gne=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:56:53 +01:00
` }};function PS(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function bne(e){let{inputs:t,backend:n}=e,{x:a}=t,r=n.texData.get(a.dataId),s=new gne(a.shape),i=[PS(a,r.complexTensorInfos.real),PS(a,r.complexTensorInfos.imag)];return n.runWebGLProgram(s,i,i[0].dtype)}var yne={kernelName:Vc,backendName:"webgl",kernelFunc:bne},xne=class{constructor(e){this.outputShape=[],this.outputShape=T.computeOutShape(e,1),this.variableNames=e.map((s,i)=> ` T$ { i } ` );let t=new Array(e.length-1);t[0]=e[0][1];for(let s=1;s<t.length;s++)t[s]=t[s-1]+e[s][1];let n=[ ` if ( yC < $ { t [ 0 ] } ) setOutput ( getT0 ( yR , yC ) ) ; ` ];for(let s=1;s<t.length;s++){let i=t[s-1];n.push( ` else if ( yC < $ { t [ s ] } ) setOutput ( getT$ { s } ( yR , yC - $ { i } ) ) ; ` )}let a=t.length,r=t[t.length-1];n.push( ` else setOutput ( getT$ { a } ( yR , yC - $ { r } ) ) ; ` ),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:56:53 +01:00
` }},vne=class{constructor(e,t){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=T.computeOutShape(e,t);let n=this.outputShape,a=n.length,r=ht(a),s=kn("coords",a),i=["x","y","z","w","u","v"].slice(0,a);this.variableNames=e.map((m,f)=> ` T$ { f } ` );let o=new Array(e.length-1);o[0]=e[0][t];for(let m=1;m<o.length;m++)o[m]=o[m-1]+e[m][t];let l=i[t],u=i.slice(-2),p=i.join(),d= ` if ( $ { l } < $ { o [ 0 ] } ) {
2021-09-11 17:11:38 +02:00
return getChannel (
2024-01-17 22:56:53 +01:00
getT0 ( $ { p } ) , vec2 ( $ { u . join ( ) } ) ) ;
} ` ;for(let m=1;m<o.length;m++){let f=o[m-1];d+= `
if ( $ { l } < $ { o [ m ] } && $ { l } >= $ { o [ m - 1 ] } ) {
2021-09-11 17:11:38 +02:00
return getChannel (
2024-01-17 22:56:53 +01:00
getT$ { m } ( $ { qh ( i , l , f ) } ) ,
vec2 ( $ { qh ( u , l , f ) } ) ) ;
} ` }let c=o.length,h=o[o.length-1];d+= `
2021-09-11 17:11:38 +02:00
return getChannel (
2024-01-17 22:56:53 +01:00
getT$ { c } ( $ { qh ( i , l , h ) } ) ,
vec2 ( $ { qh ( u , l , h ) } ) ) ; ` ,this.userCode= `
float getValue ( $ { i . map ( m => "int " + m ) } ) {
$ { d }
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { r } coords = getOutputCoords ( ) ;
vec4 result = vec4 ( getValue ( $ { s } ) , 0. , 0. , 0. ) ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
$ { s [ a - 1 ] } = $ { s [ a - 1 ] } + 1 ;
if ( $ { s [ a - 1 ] } < $ { n [ a - 1 ] } ) {
result . g = getValue ( $ { s } ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
$ { s [ a - 2 ] } = $ { s [ a - 2 ] } + 1 ;
if ( $ { s [ a - 2 ] } < $ { n [ a - 2 ] } ) {
result . a = getValue ( $ { s } ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
$ { s [ a - 1 ] } = $ { s [ a - 1 ] } - 1 ;
if ( $ { s [ a - 2 ] } < $ { n [ a - 2 ] } &&
$ { s [ a - 1 ] } < $ { n [ a - 1 ] } ) {
result . b = getValue ( $ { s } ) ;
2021-09-11 17:11:38 +02:00
}
setOutput ( result ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function qh(e,t,n){let a=e.indexOf(t);return e.map((r,s)=>s===a? ` $ { r } - $ { n } ` :r).join()}function ng(e){let{inputs:t,backend:n}=e,{input:a}=t,r=n.texData.get(a.dataId);return aa({inputs:{x:r.complexTensorInfos.imag},backend:n})}var wne={kernelName:Um,backendName:"webgl",kernelFunc:ng};function hc(e,t,n){let a=e[0].dtype;if(a==="complex64"){let h=e.map(y=>Pd({inputs:{input:y},backend:n})),m=e.map(y=>ng({inputs:{input:y},backend:n})),f=hc(h,t,n),g=hc(m,t,n),b=Ms({inputs:{real:f,imag:g},backend:n});return h.forEach(y=>n.disposeIntermediateTensorInfo(y)),m.forEach(y=>n.disposeIntermediateTensorInfo(y)),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),b}let r=n.shouldExecuteOnCPU(e);if(a==="string"&&(r=!0),r){let h=e.map(v=>{let I=[-1,w.sizeFromShape(v.shape.slice(t))];return ce({inputs:{x:v},backend:n,attrs:{shape:I}})}),m=h.map(v=>({vals:n.readSync(v.dataId),shape:v.shape})),f=T.computeOutShape(h.map(v=>v.shape),1),g=h[0].shape[0]===1,b=uQ(m,f,a,g),y=T.computeOutShape(e.map(v=>v.shape),t),x=n.makeTensorInfo(y,a,b);return h.forEach(v=>n.disposeIntermediateTensorInfo(v)),x}let s=e.filter(h=>w.sizeFromShape(h.shape)>0),i=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&s[0].shape.length>1;if(s.length===1){let h=i?new ir(e[0].shape,es):new ss(e[0].shape,es);return n.runWebGLProgram(h,e,a)}let o=G().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER");if(s.length>o){let h=[];for(let f=0;f<s.length;f+=o){let g=s.slice(f,f+o);h.push(hc(g,t,n))}let m=hc(h,t,n);for(let f of h)n.disposeIntermediateTensorInfo(f);return m}if(i){let h=new vne(s.map(m=>m.shape),t);return n.runWebGLProgram(h,s,a)}let{tensors2D:l,outShape:u}=kne(s,t,n),p=new xne(l.map(h=>h.shape)),d=n.runWebGLProgram(p,l,a);l.forEach(h=>n.disposeIntermediateTensorInfo(h));let c=ce({inputs:{x:d},attrs:{shape:u},backend:n});return n.disposeIntermediateTensorInfo(d),c}function kne(e,t,n){let a=T.computeOutShape(e.map(r=>r.shape),t);return{tensors2D:e.map(r=>ce({inputs:{x:r},attrs:{shape:[-1,w.sizeFromShape(r.shape.slice(t))]},backend:n})),outShape:a}}function uF(e){let{inputs:t,backend:n,attrs:a}=e,{axis:r}=a,s=w.parseAxisParam(r,t[0].shape)[0],i=t.map(u=>u.shape);T.assertParamsConsistent(i,s);let o=T.computeOutShape(t.map(u=>u.shape),s);if(w.sizeFromShape(o)===0)return n.makeTensorInfo(o,t[0].dtype,[]);let l=t.filter(u=>w.sizeFromShape(u.shape)>0);return l.length===1?aa({inputs:{x:l[0]},backend:n}):hc(l,s,n)}var Ine={kernelName:yu,backendName:"webgl",kernelFunc:uF},pF=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let s=e.padInfo.top,i=e.padInfo.left,o=e.strideHeight,l=e.strideWidth,u=e.dilationHeight,p=e.dilationWidth,d=e.filterHeight,c=e.filterWidth,h=Math.floor(e.inChannels/4)*4,m=e.inChannels%4,f=e.dataFormat==="channelsLast",g=f?1:2,b=f?2:3,y=f?3:1,x="",v="";n&&(a?x= ` float activation ( float a ) {
2021-09-11 17:11:38 +02:00
float b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :r?x= ` float activation ( float a ) {
2021-09-11 17:11:38 +02:00
float b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :x= `
2021-09-11 17:11:38 +02:00
float activation ( float x ) {
$ { n }
}
2024-01-17 22:56:53 +01:00
` ,v="result = activation(result);");let I=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { x }
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
const ivec2 strides = ivec2 ( $ { o } , $ { l } ) ;
const ivec2 pads = ivec2 ( $ { s } , $ { i } ) ;
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:56:53 +01:00
ivec2 ( coords [ $ { g } ] , 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:56:53 +01:00
for ( int wR = 0 ; wR < $ { d } ; 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 } ) {
continue ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { c } ; wC ++ ) {
int xC = xCCorner + wC * $ { p } ;
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:56:53 +01:00
if ( $ { f } ) {
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:56:53 +01:00
if ( $ { m === 1 } ) {
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( $ { f } ) {
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:56:53 +01:00
} else if ( $ { m === 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:56:53 +01:00
if ( $ { f } ) {
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:56:53 +01:00
} else if ( $ { m === 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:56:53 +01:00
if ( $ { f } ) {
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:56:53 +01:00
$ { I }
$ { v }
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` }},Sne=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let t=e.padInfo.front,n=e.padInfo.top,a=e.padInfo.left,r=e.strideDepth,s=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,l=e.dilationHeight,u=e.dilationWidth,p=e.filterDepth,d=e.filterHeight,c=e.filterWidth,h=Math.floor(e.inChannels/4)*4,m=e.inChannels%4;this.userCode= `
const ivec3 strides = ivec3 ( $ { r } , $ { s } , $ { i } ) ;
const ivec3 pads = ivec3 ( $ { t } , $ { n } , $ { a } ) ;
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:56:53 +01:00
for ( int wF = 0 ; wF < $ { p } ; wF ++ ) {
int xF = xFCorner + wF * $ { o } ;
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:56:53 +01:00
for ( int wR = 0 ; wR < $ { d } ; wR ++ ) {
int xR = xRCorner + wR * $ { l } ;
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:56:53 +01:00
for ( int wC = 0 ; wC < $ { c } ; wC ++ ) {
int xC = xCCorner + wC * $ { u } ;
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:56:53 +01:00
if ( $ { m === 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:56:53 +01:00
} else if ( $ { m === 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:56:53 +01:00
} else if ( $ { m === 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:56:53 +01:00
` }},cF=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=xn(this.outputShape.length);let s=e.padInfo.left,i=e.strideWidth,o=e.dilationWidth,l=e.filterHeight,u=e.filterWidth,p=u,d= `
2022-08-24 14:21:15 +02:00
int xR ; int xC ; int xCOffset ;
2024-01-17 22:56:53 +01:00
vec4 wTexel ; vec4 previous ; vec4 final ; ` ;for(let f=0;f<u;f++)d+= `
vec4 xTexelC$ { f * 2 } ;
int xTexelC$ { f * 2 } Ready ;
vec4 xTexelC$ { f * 2 + 1 } ;
int xTexelC$ { f * 2 + 1 } Ready ;
vec4 xC$ { f } ; ` ;d+= `
for ( int r = 0 ; r < $ { l } ; r ++ ) {
2022-08-24 14:21:15 +02:00
for ( int d1 = 0 ; d1 < $ { e . inChannels } ; d1 += 2 ) {
2024-01-17 22:56:53 +01:00
` ;for(let f=0;f<u;f++)d+= `
xTexelC$ { f * 2 } = vec4 ( 0.0 ) ;
xTexelC$ { f * 2 } Ready = 0 ;
xTexelC$ { f * 2 + 1 } = vec4 ( 0.0 ) ;
xTexelC$ { f * 2 + 1 } Ready = 0 ;
xC$ { f } = vec4 ( 0.0 ) ; ` ;d+= `
2022-08-24 14:21:15 +02:00
xR = xRCorner + r * dilations [ 0 ] ;
if ( xR >= 0 && xR < inDims [ 0 ] ) {
2024-01-17 22:56:53 +01:00
` ;for(let f=0;f<(p+1)/2;f++){let g=f*2;if(d+= `
xC = xCCorner + $ { g * o } ;
` ,i===1){if(g<u&&(s%2===1?(d+= `
2022-08-24 14:21:15 +02:00
xCOffset = xC + 1 ;
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { g } Ready == 0 ) {
xTexelC$ { g } = 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:56:53 +01:00
xTexelC$ { g } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ,o===1&&g>0?d+= `
xC$ { g } = vec4 ( xTexelC$ { g - 2 } . zw , xTexelC$ { g } . xy ) ;
` :d+= `
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:56:53 +01:00
xC$ { g } = vec4 ( previous . zw , xTexelC$ { g } . xy ) ;
2022-08-24 14:21:15 +02:00
} else {
2024-01-17 22:56:53 +01:00
xC$ { g } = vec4 ( 0.0 , 0.0 , xTexelC$ { g } . xy ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ):d+= `
if ( xC >= 0 && xC < inDims [ 1 ] && xTexelC$ { g } Ready == 0 ) {
xTexelC$ { g } = getX ( batch , xR , xC , d1 ) ;
2022-08-24 14:21:15 +02:00
if ( xC + 1 >= inDims [ 1 ] ) {
2024-01-17 22:56:53 +01:00
xTexelC$ { g } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xC$ { g } = xTexelC$ { g } ;
` ,g+1<u)){let b=s%2===0?w.nearestLargerEven(o):o;o%2===0&&s%2===1||o%2!==0&&s%2!==1?(d+= `
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:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { g + 1 } Ready == 0 ) {
xTexelC$ { g + 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:56:53 +01:00
xTexelC$ { g + 1 } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g + 1 } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ,o>1?d+= `
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:56:53 +01:00
xC$ { g + 1 } = vec4 ( previous . zw , xTexelC$ { g + 1 } . xy ) ;
2022-08-24 14:21:15 +02:00
} else {
2024-01-17 22:56:53 +01:00
xC$ { g + 1 } = vec4 ( 0.0 , 0.0 , xTexelC$ { g + 1 } . xy ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` :d+= `
xC$ { g + 1 } = vec4 ( xTexelC$ { g } . zw , xTexelC$ { g + 1 } . xy ) ;
` ):b===1?d+= `
xC$ { g + 1 } = xTexelC$ { g } ;
` :d+= `
2023-03-21 13:00:18 +01:00
xCOffset = xC + $ { b } ;
2022-08-24 14:21:15 +02:00
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { g + 1 } Ready == 0 ) {
xTexelC$ { g + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
2022-08-24 14:21:15 +02:00
if ( xCOffset + 1 >= inDims [ 1 ] ) {
2024-01-17 22:56:53 +01:00
xTexelC$ { g + 1 } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g + 1 } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xC$ { g + 1 } = xTexelC$ { g + 1 } ;
` }}else g<u&&(s%2===1?(d+= `
2022-08-24 14:21:15 +02:00
xCOffset = xC + 1 - strides [ 1 ] ;
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { g } Ready == 0 ) {
xTexelC$ { g } = 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:56:53 +01:00
xTexelC$ { g } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
if ( xC + 1 >= 0 && xC + 1 < inDims [ 1 ] && xTexelC$ { g + 1 } Ready == 0 ) {
xTexelC$ { g + 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:56:53 +01:00
xTexelC$ { g + 1 } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g + 1 } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xC$ { g } = vec4 ( xTexelC$ { g } . zw , xTexelC$ { g + 1 } . zw ) ;
` ,g+1<u&&(d+= `
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:56:53 +01:00
xC$ { g + 1 } = vec4 ( xTexelC$ { g + 1 } . xy , final . xy ) ;
` )):(d+= `
if ( xC >= 0 && xC < inDims [ 1 ] && xTexelC$ { g } Ready == 0 ) {
xTexelC$ { g } = getX ( batch , xR , xC , d1 ) ;
2022-08-24 14:21:15 +02:00
if ( xC + 1 >= inDims [ 1 ] ) {
2024-01-17 22:56:53 +01:00
xTexelC$ { g } . zw = vec2 ( 0.0 ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
xCOffset = xC + strides [ 1 ] ;
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { g + 1 } Ready == 0 ) {
xTexelC$ { g + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
2022-08-24 14:21:15 +02:00
if ( xCOffset + 1 >= inDims [ 1 ] ) {
2024-01-17 22:56:53 +01:00
xTexelC$ { g + 1 } . zw = vec2 ( 0. ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xTexelC$ { g + 1 } Ready = 1 ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
xC$ { g } = vec4 (
xTexelC$ { g } . xy , xTexelC$ { g + 1 } . xy ) ;
` ,g+1<u&&(d+= `
xC$ { g + 1 } = vec4 ( xTexelC$ { g } . zw , xTexelC$ { g + 1 } . zw ) ;
` )));g<u&&(d+= `
wTexel = getW ( r , $ { g } , d1 , d2 ) ;
dotProd += xC$ { g } . xxzz * vec4 ( wTexel . xy , wTexel . xy ) ;
2022-08-24 14:21:15 +02:00
if ( d1 + 1 < $ { e . inChannels } ) {
2024-01-17 22:56:53 +01:00
dotProd += xC$ { g } . yyww * vec4 ( wTexel . zw , wTexel . zw ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ,g+1<u&&(d+= `
wTexel = getW ( r , $ { g + 1 } , d1 , d2 ) ;
dotProd += xC$ { g + 1 } . xxzz * vec4 ( wTexel . xy , wTexel . xy ) ;
2022-08-24 14:21:15 +02:00
if ( d1 + 1 < $ { e . inChannels } ) {
2024-01-17 22:56:53 +01:00
dotProd += xC$ { g + 1 } . yyww * vec4 ( wTexel . zw , wTexel . zw ) ;
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ))}d+= `
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ,d+= `
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ,d+= `
2022-08-24 14:21:15 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let c="",h="";n&&(a?c= ` vec4 activation ( vec4 a ) {
2022-08-24 14:21:15 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :r?c= ` vec4 activation ( vec4 a ) {
2022-08-24 14:21:15 +02:00
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :c= ` vec4 activation ( vec4 x ) {
2022-08-24 14:21:15 +02:00
$ { n }
2024-01-17 22:56:53 +01:00
} ` ,h="result = activation(result);");let m=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { c }
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:56:53 +01:00
$ { d }
2022-08-24 14:21:15 +02:00
vec4 result = dotProd - vec4 ( 0.000000000000001 ) ;
2024-01-17 22:56:53 +01:00
$ { m }
2022-08-24 14:21:15 +02:00
$ { h }
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` }},Nne=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,a=Cn(),r=n==="channelsLast",s=r?1:2,i=r?2:3,o=this.enableShapeUniforms?"if(blockIndex < outShape[2] && pos < outShape[1]) {": ` if ( blockIndex < $ { e [ 2 ] } && pos < $ { e [ 1 ] } ) { ` ,l="";for(let u=0;u<=1;u++)for(let p=0;p<=1;p++)l+= `
blockIndex = rc . z + $ { p } ;
pos = rc . y + $ { u } ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { o }
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:56:53 +01:00
if ( d0 < inputShape [ $ { s } ] && 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:56:53 +01:00
if ( d1 < inputShape [ $ { i } ] && 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:56:53 +01:00
if ( $ { r } ) {
2021-09-11 17:11:38 +02:00
innerDims = vec2 ( d1 , ch ) ;
2024-01-17 22:56:53 +01:00
result [ $ { u * 2 + p } ] = 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:56:53 +01:00
result [ $ { u * 2 + p } ] = 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:56:53 +01:00
$ { l }
2021-04-30 18:01:04 +02:00
2024-01-17 22:56:53 +01:00
$ { a . output } = result ;
2021-04-30 18:01:04 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function Em(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 dF({x:e,filter:t,convInfo:n,backend:a,bias:r=null,preluActivationWeights:s=null,leakyreluAlpha:i=0,activation:o=null}){let l=e.shape,u=a.texData.get(e.dataId),p=n.inChannels,d=l[0]*l[1]*l[2],c=n.outChannels,h=n.dataFormat==="channelsLast",m=!1,f=!1,g,b=[];if(s!=null){let y=Em(s.shape,h);y!=null&&(s=ce({inputs:{x:s},backend:a,attrs:{shape:y}}),b.push(s))}if(r!=null){let y=Em(r.shape,h);y!=null&&(r=ce({inputs:{x:r},backend:a,attrs:{shape:y}}),b.push(r))}if(!((d===1||c===1)&&p>rF)&&u.isPacked&&h&&u.texture!=null&&l[2]%2!==0&&w.arraysEqual(u.shape.slice(-3),l.slice(-3))){let y=l[0]*l[1]*(l[2]+1),x={dataId:e.dataId,shape:[1,y,n.inChannels],dtype:e.dtype},v=u.shape;u.shape=u.shape.slice(),u.shape[u.shape.length-2]++,w.assert( $ c(u.shape,x.shape),()=> ` packed reshape $ { u . shape } to $ { x . shape } isn ' t free ` );let I=ce({inputs:{x:t},backend:a,attrs:{shape:[1,n.inChannels,n.outChannels]}});b.push(I);let N=Cm({a:x,b:I,backend:a,transposeA:m,transposeB:f,bias:r,activation:o,preluActivationWeights:s,leakyreluAlpha:i}),C=a.texData.get(N.dataId);w.assert(C.isPacked,()=>"batchMatMul result is expected to be packed"),u.shape=v,C.shape=n.outShape,g=aa({inputs:{x:N},backend:a}),g.shape=n.outShape,b.push(N)}else{let y=n.outHeight*n.outWidth,x=ce({inputs:{x:e},backend:a,attrs:{shape:h?[n.batchSize,y,n.inChannels]:[n.batchSize,n.inChannels,y]}}),v=ce({inputs:{x:t},backend:a,attrs:{shape:[1,n.inChannels,n.outChannels]}}),I=Cm({a:h?x:v,b:h?v:x,transposeA:!h,transposeB:f,backend:a,bias:r,activation:o,preluActivationWeights:s,leakyreluAlpha:i});g=ce({inputs:{x:I},backend:a,attrs:{shape:n.outShape}}),b.push(x),b.push(v),b.push(I)}for(let y of b)a.disposeIntermediateTensorInfo(y);return g}function hF({x:e,filter:t,convInfo:n,backend:a,bias:r=null,preluActivationWeights:s=null,leakyreluAlpha:i=0,activation:o=null}){let{filterWidth:l,filterHeight:u,inChannels:p,outWidth:d,outHeight:c,dataFormat:h}=n,m=h==="channelsLast",f=l*u*p,g=c*d,b=[n.batchSize,f,g],y=!0,x=!1,v=[];if(s!=null){let K=Em(s.shape,m);K!=null&&(s=ce({inputs:{x:s},backend:a,attrs:{shape:K}}),v.push(s))}if(r!=null){let K=Em(r.shape,m);K!=null&&(r=ce({inputs:{x:r},backend:a,attrs:{shape:K}}),v.push(r))}let I=ce({inputs:{x:t},backend:a,attrs:{shape:[1,f,w.sizeFromShape(t.shape)/f]}});v.push(I);let N=new Nne(b,n),C=[e.shape,[n.padInfo.top,n.padInfo.left],[n.strideHeight,n.strideWidth],[n.dilationHeight,n.dilationWidth],[n.inChannels],[n.filterWidth*n.inChannels],[n.outWidth]],_=a.runWebGLProgram(N,[e],"float32",C),F=ce({inputs:{x:_},backend:a,attrs:{shape:b}});v.push(_),v.push(F);let D=r!=null, $ =s!=null,S=o==="leakyrelu",M=o?Dc(o,!0):null,B=new aF(m?F.shape:I.shape,m?I.shape:F.shape,m?[n.batchSize,g,n.outChannels]:[n.batchSize,n.outChannels,g],y,x,D,M, $ ,S),U=m?[F,I]:[I,F];if(r&&U.push(r), $ &&U.push(s),S){let K=a.makeTensorInfo([],"float32",w.createScalarValue(i,"float32"));U.push(K),v.push(K)}let H=a.runWebGLProgram(B,U,"float32"),j=ce({inputs:{x:H},backend:a,attrs:{shape:n.outShape}});v.push(H);for(let K of v)a.disposeIntermediateTensorInfo(K);return j}function Tne(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dataFormat:l,dilations:u,dimRoundingMode:p}=a,d=T.convertConv2DDataFormat(l),c=T.computeConv2DInfo(r.shape,s.shape,i,u,o,p,!1,d),h;if(c.filterHeight===1&&c.filterWidth===1&&c.dilationHeight===1&&c.dilationWidth===1&&c.strideHeight===1&&c.strideWidth===1&&(c.padInfo.type==="SAME"||c.padInfo.type==="VALID"))h=dF({x:r,filter:s,convInfo:c,backend:n});else if(c.strideWidth<=2&&d==="channelsLast"&&G().getBool("WEBGL_EXP_CONV")){let f=new cF(c),g=[[c.padInfo.top,c.padInfo.left],[c.strideHeight,c.strideWidth],[c.dilationHeight,c.dilationWidth],[c.inHeight,c.inWidth]];h=n.runWebGLProgram(f,[r,s],"float32",g)}else if(G().getBool("WEBGL_CONV_IM2COL"))h=hF({x:r,filter:s,convInfo:c,backend:n});else{let f=new pF(c);h=n.runWebGLProgram(f,[r,s],"float32")}let m=ce({inputs:{x:h},backend:n,attrs:{shape:c.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:56:53 +01:00
int xR = wR + yR * $ { t } - $ { a } ;
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:56:53 +01:00
int xC = wC + yC * $ { n } - $ { r } ;
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:56:53 +01:00
$ { s ? ` 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:56:53 +01:00
` }},_ne=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,a=e.strideHeight,r=e.strideWidth,s=e.dataFormat==="channelsLast",i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,l=s?1:2,u=s?2:3,p=s?3:1;this.userCode= `
const ivec2 pads = ivec2 ( $ { i } , $ { o } ) ;
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:56:53 +01:00
int d1 = coords [ $ { p } ] ;
2021-09-11 17:11:38 +02:00
2024-01-17 22:56:53 +01:00
ivec2 dyCorner = ivec2 ( coords [ $ { l } ] , coords [ $ { u } ] ) - 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:56:53 +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 ) {
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:56:53 +01:00
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 ) {
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:56:53 +01:00
if ( $ { s } ) {
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:56:53 +01:00
` }},Ane=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideDepth,n=e.strideHeight,a=e.strideWidth,r=e.padInfo.front,s=e.padInfo.top,i=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:56:53 +01:00
int xF = wF + yF * $ { t } - $ { r } ;
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:56:53 +01:00
int xR = wR + yR * $ { n } - $ { s } ;
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:56:53 +01:00
int xC = wC + yC * $ { a } - $ { i } ;
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:56:53 +01:00
` }},Fne=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,a=e.filterWidth,r=e.strideDepth,s=e.strideHeight,i=e.strideWidth,o=t-1-e.padInfo.front,l=n-1-e.padInfo.top,u=a-1-e.padInfo.left;this.userCode= `
const ivec3 pads = ivec3 ( $ { o } , $ { l } , $ { u } ) ;
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:56:53 +01:00
float dyF = float ( dyFCorner + wF ) / $ { r } . 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:56:53 +01:00
float dyR = float ( dyRCorner + wR ) / $ { s } . 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:56:53 +01:00
for ( int wC = 0 ; wC < $ { a } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { i } . 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:56:53 +01:00
int wCPerm = $ { a } - 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:56:53 +01:00
` }};function $ ne(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,dy:s}=t,{strides:i,pad:o,dataFormat:l,dimRoundingMode:u,filterShape:p}=a,d=T.convertConv2DDataFormat(l),c=T.computeConv2DInfo(r.shape,p,i,1,o,u,!1,d),h=new Ene(c);return n.runWebGLProgram(h,[r,s],"float32")}var Dne={kernelName:Om,backendName:"webgl",kernelFunc: $ ne},Rne=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,a=t-1-e.padInfo.top,r=n-1-e.padInfo.left;this.userCode= `
const ivec2 pads = ivec2 ( $ { a } , $ { r } ) ;
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:56:53 +01:00
` }};function Mne(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,filter:s}=t,{inputShape:i,strides:o,pad:l,dataFormat:u,dimRoundingMode:p}=a,d=T.convertConv2DDataFormat(u),c=T.computeConv2DInfo(i,s.shape,o,1,l,p,!1,d);if(G().getBool("WEBGL_PACK_CONV2DTRANSPOSE")&&d==="channelsLast"){let h=[[c.strideHeight,c.strideWidth]],m=new Rne(c);return n.runWebGLProgram(m,[r,s],"float32",h)}else{let h=new _ne(c);return n.runWebGLProgram(h,[r,s],"float32")}}var One={kernelName:Wi,backendName:"webgl",kernelFunc:Mne};function Pne(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dilations:l}=a,u=T.computeConv3DInfo(r.shape,s.shape,i,l,o),p=new Sne(u);return n.runWebGLProgram(p,[r,s],"float32")}var Lne={kernelName:Bi,backendName:"webgl",kernelFunc:Pne};function zne(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,dy:s}=t,{strides:i,pad:o,filterShape:l}=a,u=T.computeConv3DInfo(r.shape,l,i,1,o),p=new Ane(u);return n.runWebGLProgram(p,[r,s],"float32")}var Wne={kernelName:xu,backendName:"webgl",kernelFunc:zne};function Bne(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,filter:s}=t,{pad:i,strides:o,inputShape:l}=a,u=T.computeConv3DInfo(l,s.shape,o,1,i),p=new Fne(u);return n.runWebGLProgram(p,[r,s],"float32")}var Vne={kernelName:vu,backendName:"webgl",kernelFunc:Bne},Une=Cp+ `
2021-09-11 17:11:38 +02:00
return cos ( x ) ;
2024-01-17 22:56:53 +01:00
` ,Gne= `
2023-03-21 13:00:18 +01:00
vec4 result = cos ( x ) ;
bvec4 isNaN = isnan ( x ) ;
2024-01-17 22:56:53 +01:00
$ { tl }
2023-03-21 13:00:18 +01:00
return result ;
2024-01-17 22:56:53 +01:00
` ,Hne=Ze({opSnippet:Une,packedOpSnippet:Gne}),qne={kernelName:Vi,backendName:"webgl",kernelFunc:Hne},jne= `
2021-09-11 17:11:38 +02:00
float e2x = exp ( - x ) ;
return ( e2x + 1.0 / e2x ) / 2.0 ;
2024-01-17 22:56:53 +01:00
` ,Kne=Ze({opSnippet:jne}),Xne={kernelName:Ui,backendName:"webgl",kernelFunc:Kne},Yne=class{constructor(e,t,n,a,r){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[s,i,o,l]=e,[u]=t,[p,d]=n;this.outputShape=[u,p,d,l];let c=a==="bilinear"?1:0,[h,m]=[ ` $ { i - 1 } . 0 ` , ` $ { o - 1 } . 0 ` ],[f,g,b]=p>1?[ ` $ { ( i - 1 ) / ( p - 1 ) } ` ,"(y2-y1) * height_ratio", ` y1 * $ { h } + float ( y ) * ( height _scale ) ` ]:["0.0","0.0", ` 0.5 * ( y1 + y2 ) * $ { h } ` ],[y,x,v]=d>1?[ ` $ { ( o - 1 ) / ( d - 1 ) } ` ,"(x2-x1) * width_ratio", ` x1 * $ { m } + float ( x ) * ( width _scale ) ` ]:["0.0","0.0", ` 0.5 * ( x1 + x2 ) * $ { m } ` ];this.userCode= `
const float height _ratio = float ( $ { f } ) ;
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:56:53 +01:00
if ( bInd < 0 || bInd >= $ { s } ) {
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:56:53 +01:00
float height _scale = $ { g } ;
float width _scale = $ { x } ;
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:56:53 +01:00
setOutput ( float ( $ { r } ) ) ;
2021-09-11 17:11:38 +02:00
return ;
}
2024-01-17 22:56:53 +01:00
float in _x = $ { v } ;
if ( in _x < 0.0 || in _x > $ { m } ) {
setOutput ( float ( $ { r } ) ) ;
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:56:53 +01:00
if ( $ { c } == 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:56:53 +01:00
` }},Zne=e=>{let{inputs:t,backend:n,attrs:a}=e,{image:r,boxes:s,boxInd:i}=t,{cropSize:o,method:l,extrapolationValue:u}=a,p=new Yne(r.shape,s.shape,o,l,u);return n.runWebGLProgram(p,[r,s,i],"float32")},Jne={kernelName:ku,backendName:"webgl",kernelFunc:Zne},Mc;(function(e){e.Prod="*",e.Sum="+"})(Mc||(Mc={}));var LS=class{constructor(e,t,n,a){this.op=e,this.outputShape=t,this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}];let r=this.outputShape.length,s=this.op===Mc.Prod?"1.0":"0.0",i=n?s: ` getX ( $ { zS ( r , "coords" , this . op ) } ) ` ,o=this.outputShape[this.outputShape.length-1],l="",u="";n?(l=a? ` end != $ { o - 1 } ` :"end != 0",u=a?"end + 1":"end - 1"):(l=a? ` end + pow2 < $ { o } ` :"end >= pow2",u=a?"end + pow2":"end - pow2"),this.userCode= `
2021-04-01 19:39:54 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { ht ( r ) } coords = getOutputCoords ( ) ;
int end = $ { WS ( r , "coords" , this . op ) } ;
float val = $ { i } ;
2021-09-11 17:11:38 +02:00
int pow2 = int ( pow ( 2.0 , index ) ) ;
2024-01-17 22:56:53 +01:00
if ( $ { l } ) {
int idx = $ { u } ;
$ { WS ( r , "coords" , this . op ) } = idx ;
val $ { this . op } = getX ( $ { zS ( r , "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:56:53 +01:00
` }};function zS(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 WS(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 mF(e,t,n,a,r,s){let i=t.shape.length,o=T.getAxesPermutation([a],i),l=t;o!=null&&(l=In({inputs:{x:t},backend:n,attrs:{perm:o}}));let u=T.getInnerMostAxes(1,i)[0];if(u!==i-1)throw new Error( ` WebGL cumprod shader expects an inner - most axis = $ { t . shape . length - 1 } but got axis = $ { a } ` );let p=l.shape[u],d=aa({inputs:{x:l},backend:n});for(let c=0;c<=Math.ceil(Math.log2(p))-1;c++){let h=new LS(e,l.shape,!1,s),m=[[c]],f=d;d=n.runWebGLProgram(h,[d],d.dtype,m),n.disposeIntermediateTensorInfo(f)}if(r){let c=new LS(e,l.shape,r,s),h=d;d=n.runWebGLProgram(c,[d],d.dtype),n.disposeIntermediateTensorInfo(h)}if(o!=null){let c=T.getUndoAxesPermutation(o),h=In({inputs:{x:d},backend:n,attrs:{perm:c}});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(l),h}return d}function Qne(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,exclusive:i,reverse:o}=a;return mF(Mc.Prod,r,n,s,i,o)}var eae={kernelName:wu,backendName:"webgl",kernelFunc:Qne};function tae(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,exclusive:i,reverse:o}=a;return mF(Mc.Sum,r,n,s,i,o)}var nae={kernelName:Gi,backendName:"webgl",kernelFunc:tae};function aae(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,weights:s}=t,{size:i,binaryOutput:o}=a;if(r.shape.length===1){let l=n.readSync(r.dataId),u=n.readSync(s.dataId),p=qA(l,u,s.dtype,s.shape,i);return n.makeTensorInfo([i],s.dtype,p)}else if(r.shape.length===2){let l=n.bufferSync(r),u=n.bufferSync(s),p=sQ(l,u,i,o);return n.makeTensorInfo(p.shape,s.dtype,p.values)}throw new Error( ` Error in denseBincount : input must be at most rank 2 , but got rank$ { r . shape . length } . ` )}var rae={kernelName:Uc,backendName:"webgl",kernelFunc:aae},sae=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:56:53 +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 iae(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{blockSize:s,dataFormat:i}=a,o=r.shape[0],l=i==="NHWC"?r.shape[1]:r.shape[2],u=i==="NHWC"?r.shape[2]:r.shape[3],p=i==="NHWC"?r.shape[3]:r.shape[1],d=l*s,c=u*s,h=p/(s*s),m=i==="NHWC"?[o,d,c,h]:[o,h,d,c],f=new sae(m,s,i);return n.runWebGLProgram(f,[r],r.dtype)}var oae={kernelName:Iu,backendName:"webgl",kernelFunc:iae},fF=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=xn(this.outputShape.length);let s=e.filterHeight,i=e.filterWidth,o=e.outChannels/e.inChannels,l="",u="";n&&(a?l= ` float activation ( float a ) {
2021-09-11 17:11:38 +02:00
float b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :r?l= ` float activation ( float a ) {
2021-09-11 17:11:38 +02:00
float b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :l= `
2021-09-11 17:11:38 +02:00
float activation ( float x ) {
$ { n }
}
2024-01-17 22:56:53 +01:00
` ,u="result = activation(result);");let p=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { l }
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:56:53 +01:00
int d1 = d2 / $ { o } ;
int q = d2 - d1 * $ { o } ;
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:56:53 +01:00
for ( int wR = 0 ; wR < $ { s } ; 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:56:53 +01:00
for ( int wC = 0 ; wC < $ { i } ; 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:56:53 +01:00
$ { p }
$ { u }
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` }},gF=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=xn(this.outputShape.length);let s=e.outChannels/e.inChannels,i=e.padInfo.left,o=e.strideWidth,l=e.dilationWidth,u=e.filterHeight,p=e.filterWidth,d=p,c= `
2021-09-11 17:11:38 +02:00
int xR ; int xC ; int xCOffset ;
2024-01-17 22:56:53 +01:00
vec4 wTexel ; vec4 previous ; vec4 final ; ` ;for(let g=0;g<p;g++)c+= `
vec4 xTexelC$ { g * 2 } ;
int xTexelC$ { g * 2 } Ready ;
vec4 xTexelC$ { g * 2 + 1 } ;
int xTexelC$ { g * 2 + 1 } Ready ;
vec4 xC$ { g } ; ` ;c+= `
for ( int r = 0 ; r < $ { u } ; r ++ ) {
` ;for(let g=0;g<p;g++)c+= `
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 ) ; ` ;c+= `
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:56:53 +01:00
` ;for(let g=0;g<(d+1)/2;g++){let b=g*2;if(c+= `
xC = xCCorner + $ { b * l } ;
` ,o===1){if(b<p&&(i%2===1?(c+= `
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:56:53 +01:00
` ,l===1&&b>0?c+= `
2023-03-21 13:00:18 +01:00
xC$ { b } = vec4 ( xTexelC$ { b - 2 } . zw , xTexelC$ { b } . xy ) ;
2024-01-17 22:56:53 +01:00
` :c+= `
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:56:53 +01:00
` ):c+= `
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:56:53 +01:00
` ,b+1<p)){let y=i%2===0?w.nearestLargerEven(l):l;l%2===0&&i%2===1||l%2!==0&&i%2!==1?(c+= `
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:56:53 +01:00
` ,l>1?c+= `
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:56:53 +01:00
` :c+= `
2023-03-21 13:00:18 +01:00
xC$ { b + 1 } = vec4 ( xTexelC$ { b } . zw , xTexelC$ { b + 1 } . xy ) ;
2024-01-17 22:56:53 +01:00
` ):y===1?c+= `
2023-03-21 13:00:18 +01:00
xC$ { b + 1 } = xTexelC$ { b } ;
2024-01-17 22:56:53 +01:00
` :c+= `
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:56:53 +01:00
` }}else b<p&&(i%2===1?(c+= `
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:56:53 +01:00
` ,b+1<p&&(c+= `
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:56:53 +01:00
` )):(c+= `
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:56:53 +01:00
` ,b+1<p&&(c+= `
2023-03-21 13:00:18 +01:00
xC$ { b + 1 } = vec4 ( xTexelC$ { b } . zw , xTexelC$ { b + 1 } . zw ) ;
2024-01-17 22:56:53 +01:00
` )));b<p&&(c+= `
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:56:53 +01:00
` ,b+1<p&&(c+= `
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:56:53 +01:00
` ))}c+= `
2021-10-22 15:06:43 +02:00
}
2024-01-17 22:56:53 +01:00
` ,c+= `
2021-10-22 15:06:43 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let h="",m="";n&&(a?h= ` vec4 activation ( vec4 a ) {
2021-09-11 17:11:38 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
2024-01-17 22:56:53 +01:00
} ` :r?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:56:53 +01:00
} ` ,m="result = activation(result);");let f=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&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:56:53 +01:00
int d1 = d2 / $ { s } ;
int q = d2 - d1 * $ { s } ;
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:56:53 +01:00
$ { c }
2021-09-11 17:11:38 +02:00
vec4 result = dotProd - vec4 ( 0.000000000000001 ) ;
2022-10-18 13:10:36 +02:00
$ { f }
2024-01-17 22:56:53 +01:00
$ { m }
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function lae(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dilations:l,dimRoundingMode:u}=a,p=l;p==null&&(p=[1,1]),w.assert(T.eitherStridesOrDilationsAreOne(i,p),()=> ` Error in depthwiseConv2d : Either strides or dilations must be 1. Got strides $ { i } and dilations '${p}' ` );let d=T.computeConv2DInfo(r.shape,s.shape,i,p,o,u,!0),c;G().getBool("WEBGL_PACK_DEPTHWISECONV")&&d.strideWidth<=2&&d.outChannels/d.inChannels===1?c=new gF(d):c=new fF(d);let h=[[d.padInfo.top,d.padInfo.left],[d.strideHeight,d.strideWidth],[d.dilationHeight,d.dilationWidth],[d.inHeight,d.inWidth]];return n.runWebGLProgram(c,[r,s],"float32",h)}var uae={kernelName:Hi,backendName:"webgl",kernelFunc:lae},pae=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,a=e.padInfo.top,r=e.padInfo.left,s=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:56:53 +01:00
int d2 = d1 * $ { s } + 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:56:53 +01:00
int xR = wR + yR * $ { t } - $ { a } ;
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:56:53 +01:00
int xC = wC + yC * $ { n } - $ { r } ;
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:56:53 +01:00
` }},cae=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,a=e.strideHeight,r=e.strideWidth,s=t-1-e.padInfo.top,i=n-1-e.padInfo.left,o=e.outChannels/e.inChannels;this.userCode= `
const ivec2 pads = ivec2 ( $ { s } , $ { i } ) ;
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:56:53 +01:00
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 || 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:56:53 +01:00
float dyC = float ( dyCCorner + wC ) / $ { r } . 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:56:53 +01:00
for ( int dm = 0 ; dm < $ { o } ; dm ++ ) {
int d2 = d1 * $ { o } + 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:56:53 +01:00
` }};function dae(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,dy:s}=t,{strides:i,dilations:o,pad:l,dimRoundingMode:u,filterShape:p}=a,d=T.computeConv2DInfo(r.shape,p,i,o,l,u,!0),c=new pae(d);return n.runWebGLProgram(c,[r,s],"float32")}var hae={kernelName:Pm,backendName:"webgl",kernelFunc:dae};function mae(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,filter:s}=t,{strides:i,dilations:o,pad:l,dimRoundingMode:u,inputShape:p}=a,d=T.computeConv2DInfo(p,s.shape,i,o,l,u,!0),c=new cae(d);return n.runWebGLProgram(c,[r,s],"float32")}var fae={kernelName:Lm,backendName:"webgl",kernelFunc:mae},gae=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:56:53 +01:00
` }};function bae(e){let{inputs:t,backend:n}=e,{x:a}=t,r=[...a.shape,...a.shape],s=w.sizeFromShape(a.shape),i=ce({inputs:{x:a},backend:n,attrs:{shape:[s]}}),o=new gae(s),l=n.runWebGLProgram(o,[i],i.dtype),u=ce({inputs:{x:l},backend:n,attrs:{shape:r}});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(l),u}var yae={kernelName:Gc,backendName:"webgl",kernelFunc:bae},xae=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:a,strideHeight:r,strideWidth:s,filterHeight:i,filterWidth:o,dilationHeight:l,dilationWidth:u}=e,{top:p,left:d}=a;this.userCode= `
const ivec2 strides = ivec2 ( $ { r } , $ { s } ) ;
const ivec2 pads = ivec2 ( $ { p } , $ { d } ) ;
2021-09-11 17:11:38 +02:00
const float neg _infinity = - 3.4 e38 ;
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:56:53 +01:00
for ( int h = 0 ; h < $ { i } ; h ++ ) {
int hIn = hBeg + h * $ { l } ;
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:56:53 +01:00
for ( int w = 0 ; w < $ { o } ; w ++ ) {
int wIn = wBeg + w * $ { u } ;
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:56:53 +01:00
` }};function vae(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dilations:l}=a,u=T.computeDilation2DInfo(r.shape,s.shape,i,o,"NHWC",l),p,d=new xae(u);p=n.runWebGLProgram(d,[r,s],"float32");let c=ce({inputs:{x:p},backend:n,attrs:{shape:u.outShape}});return n.disposeIntermediateTensorInfo(p),c}var wae={kernelName:qi,backendName:"webgl",kernelFunc:vae};function kae(e){let{inputs:t,backend:n,attrs:a}=e,{equation:r}=a,s=t,{allDims:i,summedDims:o,idDims:l}=T.decodeEinsumEquation(r,s.length);T.checkEinsumDimSizes(i.length,l,s);let{path:u,steps:p}=T.getEinsumComputePath(o,l),d=p.length,c=null,h=i.length,m=[];for(let f=0;f<d;++f){for(let g of p[f]){let{permutationIndices:b,expandDims:y}=T.getEinsumPermutation(h,l[g]),x;T.isIdentityPermutation(b)?x=s[g]:(x=In({inputs:{x:s[g]},backend:n,attrs:{perm:b}}),m.push(x));let v=x.shape.slice();for(let I=0;I<y.length;++I)v.splice(y[I],0,1);w.arraysEqual(x.shape,v)||(x=ce({inputs:{x},backend:n,attrs:{shape:v}}),m.push(x)),c===null?c=x:(c=yk({inputs:{a:x,b:c},backend:n}),m.push(c))}f<d-1&&(u[f]>=0&&(c=tg({inputs:{x:c},backend:n,attrs:{axis:u[f]-(i.length-h),keepDims:!1}}),m.push(c)),h--)}for(let f of m)f!==c&&n.disposeIntermediateTensorInfo(f);return c}var Iae={kernelName:Wm,backendName:"webgl",kernelFunc:kae},Sae="return (x >= 0.0) ? x : (exp(x) - 1.0);",Nae= `
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:56:53 +01:00
` ,Tae=Ze({opSnippet:Sae,packedOpSnippet:Nae}),Cae={kernelName:Ki,backendName:"webgl",kernelFunc:Tae},Eae="return (b >= 0.0) ? a : a * (b + 1.0);",_ae= `
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:56:53 +01:00
` ,Aae=e=>{let{inputs:t,backend:n}=e,{dy:a,y:r}=t,s=G().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Tp(_ae,a.shape,r.shape):new Si(Eae,a.shape,r.shape);return n.runWebGLProgram(s,[a,r],a.dtype)},Fae={kernelName:Su,backendName:"webgl",kernelFunc:Aae}, $ ae= `
2021-09-11 17:11:38 +02:00
return vec4 ( equal ( a , b ) ) ;
2024-01-17 22:56:53 +01:00
` ,Dae="return float(a == b);",Rae=fn({opSnippet:Dae,packedOpSnippet: $ ae,dtype:"bool",cpuKernelImpl:pQ}),Mae={kernelName:Nu,backendName:"webgl",kernelFunc:Rae},Oae= `
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:56:53 +01:00
` ,Pae=Ze({opSnippet:Oae}),Lae={kernelName:Xi,backendName:"webgl",kernelFunc:Pae},zae=Cp+ `
2022-01-14 16:04:13 +01:00
return exp ( x ) ;
2024-01-17 22:56:53 +01:00
` ,Wae= `
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:56:53 +01:00
` ,bF=Ze({opSnippet:zae,packedOpSnippet:Wae,cpuKernelImpl:cQ,dtype:"float32"}),Bae={kernelName:Yi,backendName:"webgl",kernelFunc:bF};function Iv(e){let{inputs:t,attrs:n,backend:a}=e,{dim:r}=n,{input:s}=t,i=s.shape.length,o=s.shape.slice(),l=r;return r<0&&(w.assert(-(i+1)<=r,()=> ` Axis must be in the interval [ $ { - ( i + 1 ) } , $ { i } ] ` ),l=i+r+1),o.splice(l,0,1),ce({inputs:{x:s},backend:a,attrs:{shape:o}})}var Vae={kernelName:Tu,backendName:"webgl",kernelFunc:Iv},BS="return exp(x) - 1.0;",Uae=Ze({opSnippet:BS,packedOpSnippet:BS,cpuKernelImpl:dQ}),Gae={kernelName:Zi,backendName:"webgl",kernelFunc:Uae},VS=class{constructor(e,t,n){this.variableNames=["real","imag"];let a=t[1];this.outputShape=t;let r=n? ` 2.0 * $ { Math . PI } ` : ` - 2.0 * $ { Math . PI } ` ,s=n? ` $ { a } . 0 ` :"1.0",i;if(e==="real")i="return real * expR - imag * expI;";else if(e==="imag")i="return real * expI + imag * expR;";else throw new Error( ` FFT component must be either "real" or "imag" , got $ { e } . ` );this.userCode= `
const float exponentMultiplier = $ { r } ;
2021-09-11 17:11:38 +02:00
float unaryOpComplex ( float real , float expR , float imag , float expI ) {
2024-01-17 22:56:53 +01:00
$ { i }
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:56:53 +01:00
float indexRatio = float ( index ) / float ( $ { a } ) ;
2021-09-11 17:11:38 +02:00
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio ;
float result = 0.0 ;
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { a } ; 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:56:53 +01:00
unaryOpComplex ( real , expR , imag , expI ) / $ { s } ;
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:56:53 +01:00
` }};function yF(e,t,n){let a=n.texData.get(e.dataId),r=w.sizeFromShape(e.shape),s=e.shape[e.shape.length-1],i=r/s,o=ce({inputs:{x:e},backend:n,attrs:{shape:[i,s]}}),l=o.shape,u=new VS("real",l,t),p=new VS("imag",l,t),d=[{dataId:a.complexTensorInfos.real.dataId,dtype:a.complexTensorInfos.real.dtype,shape:l},{dataId:a.complexTensorInfos.imag.dataId,dtype:a.complexTensorInfos.imag.dtype,shape:l}],c=n.runWebGLProgram(u,d,"float32"),h=n.runWebGLProgram(p,d,"float32"),m=Ms({inputs:{real:c,imag:h},backend:n});n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(h);let f=ce({inputs:{x:m},backend:n,attrs:{shape:e.shape}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(m),f}function Hae(e){let{inputs:t,backend:n}=e,{input:a}=t;return yF(a,!1,n)}var qae={kernelName:Bm,backendName:"webgl",kernelFunc:Hae},jae=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:56:53 +01:00
` }};function Ld(e){let{backend:t,attrs:n}=e,{shape:a,value:r}=n,{dtype:s}=n;if(s=s||w.inferDtype(r),s==="string"){let i=w.getArrayFromDType(s,w.sizeFromShape(a));return i.fill(r),t.makeTensorInfo(a,s,i)}else{let i=new jae(a,r),o=[[r]];return t.runWebGLProgram(i,[],s,o)}}var Kae={kernelName:Hc,backendName:"webgl",kernelFunc:Ld},Xae=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:56:53 +01:00
` }},Yae={kernelName:Cu,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,a=t,r=new Xae(n.shape);return a.runWebGLProgram(r,[n],n.dtype)}},US="return floor(x);",Zae=Ze({opSnippet:US,packedOpSnippet:US,cpuKernelImpl:hQ}),Jae={kernelName:Ji,backendName:"webgl",kernelFunc:Zae},Qae= `
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:56:53 +01:00
` ,ere= `
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:56:53 +01:00
` ,tre=fn({opSnippet:Qae,packedOpSnippet:ere,dtype:"int32"}),nre={kernelName:Qi,backendName:"webgl",kernelFunc:tre},are=class{constructor(e){this.variableNames=["A"];let t=Cn(),[n,a]=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:56:53 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { a } . 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:56:53 +01:00
` }},rre=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=Cn(),[n,a]=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:56:53 +01:00
vec2 ( $ { a } . 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:56:53 +01:00
` }},sre={kernelName:sm,backendName:"webgl",kernelFunc:ire},Ol,Tx=G().getBool("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU");function ire(e){let{inputs:t,backend:n,attrs:a}=e,{pixels:r}=t,{numChannels:s}=a,i=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,o=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,[l,u]=i?[r.videoWidth,r.videoHeight]:[r.width,r.height],p=[u,l],d=[u,l,s];if(o||i){let f=G().getBool("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU");(Ol==null||f!==Tx)&&(Tx=f,Ol=document.createElement("canvas").getContext("2d",{willReadFrequently:Tx})),Ol.canvas.width=l,Ol.canvas.height=u,Ol.drawImage(r,0,0,l,u),r=Ol.canvas}let c=n.makeTensorInfo(p,"int32");n.texData.get(c.dataId).usage=da.PIXELS,n.gpgpu.uploadPixelDataToTexture(n.getTexture(c.dataId),r);let h=G().getBool("WEBGL_PACK")?new rre(d):new are(d),m=n.runWebGLProgram(h,[c],"int32");return n.disposeData(c.dataId),m}function ore(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s,bias:i,preluActivationWeights:o}=t,{strides:l,pad:u,dataFormat:p,dilations:d,dimRoundingMode:c,activation:h,leakyreluAlpha:m}=a,f=T.convertConv2DDataFormat(p),g=T.computeConv2DInfo(r.shape,s.shape,l,d,u,c,!1,f),b,y=[],x=i!=null,v=o!=null,I=h==="leakyrelu",N=()=>{let _=[r,s],F=(D, $ )=>{if( $ ==="NCHW"&&D.shape.length===1&&D.shape[0]!==1){let S=ce({inputs:{x:D},backend:n,attrs:{shape:[D.shape[0],1,1]}});return y.push(S),S}return D};if(x&&_.push(F(i,p)),v&&_.push(F(o,p)),I){let D=n.makeTensorInfo([],"float32",w.createScalarValue(m,"float32"));_.push(D),y.push(D)}return _};if(g.filterHeight===1&&g.filterWidth===1&&g.dilationHeight===1&&g.dilationWidth===1&&g.strideHeight===1&&g.strideWidth===1&&(g.padInfo.type==="SAME"||g.padInfo.type==="VALID"))b=dF({x:r,filter:s,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:m});else if(g.strideWidth<=2&&f==="channelsLast"&&G().getBool("WEBGL_EXP_CONV")){let _=h?Dc(h,!0):null,F=new cF(g,x,_,v,I),D=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]], $ =N();b=n.runWebGLProgram(F, $ ,"float32",D)}else if(G().getBool("WEBGL_CONV_IM2COL"))b=hF({x:r,filter:s,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:m});else{let _=h?Dc(h,!1):null,F=new pF(g,x,_,v,I),D=N();b=n.runWebGLProgram(F,D,"float32")}let C=ce({inputs:{x:b},backend:n,attrs:{shape:g.outShape}});return y.push(b),y.forEach(_=>n.disposeIntermediateTensorInfo(_)),C}var lre={kernelName:li,backendName:"webgl",kernelFunc:ore};function ure(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s,bias:i,preluActivationWeights:o}=t,{strides:l,pad:u,dilations:p,dimRoundingMode:d,activation:c,leakyreluAlpha:h}=a,m=[],f=p;f==null&&(f=[1,1]),w.assert(T.eitherStridesOrDilationsAreOne(l,f),()=> ` Error in depthwiseConv2d : Either strides or dilations must be 1. Got strides $ { l } and dilations '${f}' ` );let g=T.computeConv2DInfo(r.shape,s.shape,l,f,u,d,!0),b=G().getBool("WEBGL_PACK_DEPTHWISECONV")&&g.strideWidth<=2&&g.outChannels/g.inChannels===1,y=c?Dc(c,b):null,x=[r,s],v=i!=null,I=o!=null,N=c==="leakyrelu";if(v&&x.push(i),I&&x.push(o),N){let D=n.makeTensorInfo([],"float32",w.createScalarValue(h,"float32"));x.push(D),m.push(D)}let C;b?C=new gF(g,v,y,I,N):C=new fF(g,v,y,I,N);let _=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]],F=n.runWebGLProgram(C,x,"float32",_);return m.forEach(D=>n.disposeIntermediateTensorInfo(D)),F}var pre={kernelName:ui,backendName:"webgl",kernelFunc:ure},cre=class{constructor(e,t,n,a){this.sliceDim=e,this.strides=t,this.paramsShape=a,this.variableNames=["x","indices"],this.outputShape=n;let r=ht(n.length),s= `
int index ; ` ;for(let i=0;i<this.sliceDim;i++)s+= `
index = round ( getIndices ( coords [ 0 ] , $ { i } ) ) ;
2022-10-09 19:40:33 +02:00
out _of _bounds = out _of _bounds || index < 0 ;
2024-01-17 22:56:53 +01:00
out _of _bounds = out _of _bounds || index >= $ { this . paramsShape [ i ] } ;
flattenIndex += index * $ { this . strides [ i ] } ; ` ;this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { r } 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:56:53 +01:00
$ { s }
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:56:53 +01:00
` }};function dre(e){let{inputs:t,backend:n}=e,{params:a,indices:r}=t,s=r.shape,i=s[s.length-1],o=w.sizeFromShape(a.shape),[l,u,p,d]=T.prepareAndValidate(a,r),c=ce({inputs:{x:r},backend:n,attrs:{shape:[u,i]}}),h=ce({inputs:{x:a},backend:n,attrs:{shape:[w.sizeFromShape(a.shape)/p,p]}});if(n.shouldExecuteOnCPU([a,r])||a.dtype==="string"){let b=n.readSync(r.dataId),y=n.bufferSync(a),x=mQ(b,y,a.dtype,u,i,p,d,a.shape,o);return n.makeTensorInfo(l,a.dtype,x.values)}let m=new cre(i,d,[u,p],a.shape),f=n.runWebGLProgram(m,[h,c],h.dtype),g=ce({inputs:{x:f},backend:n,attrs:{shape:l}});return n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),g}var hre={kernelName:_u,backendName:"webgl",kernelFunc:dre},mre=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=ht(this.rank),a=fre(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:56:53 +01:00
setOutput ( inBounds * getA ( $ { a } ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function fre(e,t){let n=["resRC.x","resRC.y","resRC.z","resRC.w"],a=[];for(let r=0;r<e.length;r++)r===2?a.push("index"):a.push( ` $ { n [ r ] } ` );return a.join()}function xF(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,indices:s}=t,{axis:i,batchDims:o}=a,l=w.parseAxisParam(i,r.shape)[0];if(G().get("DEBUG")){let y=n.readSync(s.dataId),x=r.shape[l];for(let v=0;v<y.length;++v){let I=y[v];w.assert(I<=x-1&&I>=0,()=> ` GatherV2 : the index value $ { I } is not in [ 0 , $ { x - 1 } ] ` )}}let u=T.segment_util.collectGatherOpShapeInfo(r,s,l,o),p=w.sizeFromShape(s.shape),d=[],c=ce({inputs:{x:r},backend:n,attrs:{shape:[u.batchSize,u.outerSize,u.dimSize,u.sliceSize]}}),h=ce({inputs:{x:s},backend:n,attrs:{shape:[u.batchSize,p/u.batchSize]}});d.push(c),d.push(h);let m=[u.batchSize,u.outerSize,p/u.batchSize,u.sliceSize];if(n.shouldExecuteOnCPU([r,s])||r.dtype==="string"){let y=n.bufferSync(h),x=n.bufferSync(c),v=fQ(x,y,m);return d.forEach(I=>n.disposeIntermediateTensorInfo(I)),n.makeTensorInfo(u.outputShape,v.dtype,v.values)}let f=new mre(c.shape,m),g=n.runWebGLProgram(f,[c,h],c.dtype);d.push(g);let b=ce({inputs:{x:g},backend:n,attrs:{shape:u.outputShape}});return d.forEach(y=>n.disposeIntermediateTensorInfo(y)),b}var gre={kernelName:Eu,backendName:"webgl",kernelFunc:xF},bre="return float(a > b);",yre= `
2021-09-11 17:11:38 +02:00
return vec4 ( greaterThan ( a , b ) ) ;
2024-01-17 22:56:53 +01:00
` ,xre=fn({opSnippet:bre,packedOpSnippet:yre,cpuKernelImpl:gQ,dtype:"bool"}),vre={kernelName:Au,backendName:"webgl",kernelFunc:xre},wre="return float(a >= b);",kre= `
2021-09-11 17:11:38 +02:00
return vec4 ( greaterThanEqual ( a , b ) ) ;
2024-01-17 22:56:53 +01:00
` ,Ire=fn({opSnippet:wre,packedOpSnippet:kre,dtype:"bool",cpuKernelImpl:bQ}),Sre={kernelName:to,backendName:"webgl",kernelFunc:Ire};function Nre(e){let{inputs:t,backend:n}=e,{input:a}=t;return yF(a,!0,n)}var Tre={kernelName:Vm,backendName:"webgl",kernelFunc:Nre},Cre="return float(!isnan(x) && !isinf(x));",Ere=Ze({opSnippet:Cre,dtype:"bool"}),_re={kernelName:ao,backendName:"webgl",kernelFunc:Ere},Are="return float(isinf(x));",Fre=Ze({opSnippet:Are,dtype:"bool"}), $ re={kernelName:ro,backendName:"webgl",kernelFunc:Fre},Dre="return float(isnan(x));",Rre=Ze({opSnippet:Dre,dtype:"bool"}),Mre={kernelName:so,backendName:"webgl",kernelFunc:Rre},Ore="return float(a < b);",Pre= `
2021-09-11 17:11:38 +02:00
return vec4 ( lessThan ( a , b ) ) ;
2024-01-17 22:56:53 +01:00
` ,Lre=fn({opSnippet:Ore,packedOpSnippet:Pre,cpuKernelImpl:yQ,dtype:"bool"}),zre={kernelName:Fu,backendName:"webgl",kernelFunc:Lre},Wre="return float(a <= b);",Bre= `
2021-09-11 17:11:38 +02:00
return vec4 ( lessThanEqual ( a , b ) ) ;
2024-01-17 22:56:53 +01:00
` ,Vre=fn({opSnippet:Wre,packedOpSnippet:Bre,cpuKernelImpl:xQ,dtype:"bool"}),Ure={kernelName: $ u,backendName:"webgl",kernelFunc:Vre};function Gre(e){let{backend:t,attrs:n}=e,{start:a,stop:r,num:s}=n,i=vQ(a,r,s);return t.makeTensorInfo([i.length],"float32",i)}var Hre={kernelName:Du,backendName:"webgl",kernelFunc:Gre},qre=Cp+ `
2022-01-14 16:04:13 +01:00
return x < 0.0 ? 0. / 0. : log ( x ) ;
2024-01-17 22:56:53 +01:00
` ,jre= `
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:56:53 +01:00
` ,Kre=Ze({opSnippet:qre,packedOpSnippet:jre,cpuKernelImpl:wQ}),Xre={kernelName:oo,backendName:"webgl",kernelFunc:Kre},Yre=Cp+ `
2022-01-14 16:04:13 +01:00
return log ( 1.0 + x ) ;
2024-01-17 22:56:53 +01:00
` ,Zre=Ze({opSnippet:Yre}),Jre={kernelName:lo,backendName:"webgl",kernelFunc:Zre},Qre="return float(a >= 1.0 && b >= 1.0);",ese= `
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:56:53 +01:00
` ,tse=fn({opSnippet:Qre,packedOpSnippet:ese,dtype:"bool"}),nse={kernelName:Ru,backendName:"webgl",kernelFunc:tse},ase="return float(!(x >= 1.0));",rse=Ze({opSnippet:ase}),sse={kernelName:Mu,backendName:"webgl",kernelFunc:rse},ise="return float(a >= 1.0 || b >= 1.0);",ose= `
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:56:53 +01:00
` ,lse=fn({opSnippet:ise,packedOpSnippet:ose,dtype:"bool"}),use={kernelName:Ou,backendName:"webgl",kernelFunc:lse},pse=class{constructor(e,t,n,a,r){this.variableNames=["x"],this.outputShape=[];let s=t,i=e[3]-1;this.outputShape=e;let o,l= ` float ( $ { n } ) + float ( $ { a } ) * sum ` ;r===.5?o= ` inversesqrt ( $ { l } ) ` :r===1?o= ` 1.0 / ( $ { l } ) ` :o= ` exp ( log ( $ { l } ) * float ( - $ { r } ) ) ; ` ,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:56:53 +01:00
for ( int j = - $ { s } ; j <= $ { s } ; j ++ ) {
2021-09-11 17:11:38 +02:00
int idx = d + j ;
2024-01-17 22:56:53 +01:00
if ( idx >= 0 && idx <= $ { i } ) {
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:56:53 +01:00
float val = x * $ { o } ;
2021-09-11 17:11:38 +02:00
setOutput ( val ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},cse=class{constructor(e,t,n,a,r){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let s=t,i=e[3]-1;this.outputShape=e;let o,l= ` float ( $ { n } ) + float ( $ { a } ) * sum ` ;r===.5?o= ` inversesqrt ( $ { l } ) ` :r===1?o= ` 1.0 / ( $ { l } ) ` :o= ` exp ( log ( $ { l } ) * float ( - $ { r } ) ) ; ` ,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:56:53 +01:00
int firstChannel = d - $ { s } ;
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:56:53 +01:00
for ( int j = - $ { s } ; j <= $ { s } ; j ++ ) {
2021-09-11 17:11:38 +02:00
ivec2 idx = depth + j ;
bvec2 aboveLowerBound = greaterThanEqual ( idx , ivec2 ( 0 ) ) ;
2024-01-17 22:56:53 +01:00
bvec2 belowUpperBound = lessThanEqual ( idx , ivec2 ( $ { i } ) ) ;
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:56:53 +01:00
vec4 result = xAtOutputCoords * $ { o } ;
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},dse=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{depthRadius:s,bias:i,alpha:o,beta:l}=a,u=G().getBool("WEBGL_PACK_NORMALIZATION")?new cse(r.shape,s,i,o,l):new pse(r.shape,s,i,o,l);return n.runWebGLProgram(u,[r],r.dtype)},hse={kernelName:uo,backendName:"webgl",kernelFunc:dse},mse=class{constructor(e,t,n,a,r){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=a,this.beta=r,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:56:53 +01:00
norm = float ( $ { a } ) * 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:56:53 +01:00
float dyi = - 2.0 * float ( $ { a } )
* float ( $ { r } )
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
/ n o r m ;
if ( k == d ) {
2024-01-17 22:56:53 +01:00
dyi += pow ( norm , - 1.0 * $ { r } ) ;
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:56:53 +01:00
` }},fse=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r,y:s,dy:i}=t,{depthRadius:o,bias:l,alpha:u,beta:p}=a,d=new mse(r.shape,o,l,u,p);return n.runWebGLProgram(d,[r,s,i],r.dtype)},gse={kernelName:Pu,backendName:"webgl",kernelFunc:fse};function bse(e,t,n,a){let r=w.sizeFromShape(t),s=w.sizeFromShape(e.shape)/r,i=ce({inputs:{x:e},attrs:{shape:[s,r]},backend:a}),o=nl(i,e.dtype,"max",a),l=ce({inputs:{x:o},attrs:{shape:n},backend:a});return a.disposeIntermediateTensorInfo(i),a.disposeIntermediateTensorInfo(o),l}function vF(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{reductionIndices:s,keepDims:i}=a,o=r.shape.length,l=w.parseAxisParam(s,r.shape),u=l,p=T.getAxesPermutation(u,o),d=p!=null,c=n.shouldExecuteOnCPU([r]),h=r;if(d){if(c){let y=n.texData.get(h.dataId).values,x=new Array(o);for(let N=0;N<x.length;N++)x[N]=r.shape[p[N]];let v=fk(y,r.shape,r.dtype,p,x);h=n.makeTensorInfo(x,r.dtype);let I=n.texData.get(h.dataId);I.values=v}else h=eg(r,p,n);u=T.getInnerMostAxes(u.length,o)}T.assertAxesAreInnerMostDims("max",u,o);let[m,f]=T.computeOutAndReduceShapes(h.shape,u),g=m;i&&(g=T.expandShapeToKeepDim(m,l));let b;if(c){let y=n.texData.get(h.dataId).values,x=kQ(y,w.sizeFromShape(f),g,r.dtype);b=n.makeTensorInfo(g,r.dtype);let v=n.texData.get(b.dataId);v.values=x}else b=bse(h,f,g,n);return d&&n.disposeIntermediateTensorInfo(h),b}var yse={kernelName:po,backendName:"webgl",kernelFunc:vF},xse=bk+ `
2021-09-11 17:11:38 +02:00
return max ( a , b ) ;
2024-01-17 22:56:53 +01:00
` ,vse= `
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:56:53 +01:00
` +tl+ `
2021-09-11 17:11:38 +02:00
return result ;
2024-01-17 22:56:53 +01:00
` ,wse=fn({opSnippet:xse,packedOpSnippet:vse,cpuKernelImpl:IQ}),kse={kernelName:co,backendName:"webgl",kernelFunc:wse};function Ise(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t;wp(r,"maxPool");let{filterSize:s,strides:i,pad:o,dimRoundingMode:l}=a,u=1;w.assert(T.eitherStridesOrDilationsAreOne(i,u),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { i } and dilations '${u}' ` );let p=T.computePool2DInfo(r.shape,s,i,u,o,l);if(p.filterWidth===1&&p.filterHeight===1&&w.arraysEqual(p.inShape,p.outShape))return aa({inputs:{x:r},backend:n});let d=new Rc(p,"max",!1);return n.runWebGLProgram(d,[r],r.dtype)}var Sse={kernelName:ho,backendName:"webgl",kernelFunc:Ise};function Nse(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{filterSize:s,strides:i,pad:o,dataFormat:l,dimRoundingMode:u}=a,p=[1,1,1],d=T.computePool3DInfo(r.shape,s,i,p,o,u,l),c=new xk(d,"max",!1);return n.runWebGLProgram(c,[r],r.dtype)}var Tse={kernelName:Lu,backendName:"webgl",kernelFunc:Nse},Cse=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideHeight,n=e.strideWidth,a=e.dilationHeight,r=e.effectiveFilterHeight,s=e.effectiveFilterWidth,i=r-1-e.padInfo.top,o=s-1-e.padInfo.left,l=r*s-1;this.userCode= `
const ivec2 pads = ivec2 ( $ { i } , $ { o } ) ;
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:56:53 +01:00
for ( int wR = 0 ; wR < $ { r } ;
wR += $ { a } ) {
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:56:53 +01:00
for ( int wC = 0 ; wC < $ { s } ; 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:56:53 +01:00
int maxPosValue = $ { l } - 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:56:53 +01:00
int curPosValue = wR * $ { s } + 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:56:53 +01:00
` }},Ese=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideDepth,n=e.strideHeight,a=e.strideWidth,r=e.dilationDepth,s=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterDepth,l=e.effectiveFilterHeight,u=e.effectiveFilterWidth,p=o-1-e.padInfo.front,d=l-1-e.padInfo.top,c=u-1-e.padInfo.left,h=o*l*u-1;this.userCode= `
const ivec3 pads = ivec3 ( $ { p } , $ { d } , $ { c } ) ;
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:56:53 +01:00
for ( int wD = 0 ; wD < $ { o } ;
wD += $ { r } ) {
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:56:53 +01:00
for ( int wR = 0 ; wR < $ { l } ;
wR += $ { s } ) {
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:56:53 +01:00
for ( int wC = 0 ; wC < $ { u } ;
wC += $ { i } ) {
float dyC = float ( dyCCorner + wC ) / $ { a } . 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:56:53 +01:00
wD * $ { l } * $ { u } +
wR * $ { u } + 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:56:53 +01:00
` }};function _se(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s}=t,i=s,{filterSize:o,strides:l,pad:u,dimRoundingMode:p}=a,d=[1,1,1],c=T.computePool3DInfo(i.shape,o,l,d,u,p),h=new xk(c,"max",!0),m=n.runWebGLProgram(h,[i],i.dtype),f=new Ese(c),g=n.runWebGLProgram(f,[r,m],i.dtype);return n.disposeIntermediateTensorInfo(m),g}var Ase={kernelName:jc,backendName:"webgl",kernelFunc:_se};function Fse(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s,output:i}=t,o=s;wp([s,i],"maxPoolGrad");let{filterSize:l,strides:u,pad:p,dimRoundingMode:d}=a,c=T.computePool2DInfo(o.shape,l,u,1,p,d),h=!0,m=new Rc(c,"max",h),f=n.runWebGLProgram(m,[o],o.dtype),g=new Cse(c),b=n.runWebGLProgram(g,[r,f],o.dtype);return n.disposeIntermediateTensorInfo(f),b}var $ se={kernelName:qc,backendName:"webgl",kernelFunc:Fse};function Dse(e,t,n,a){let r=new Rc(n,"max",!1),s=a.runWebGLProgram(r,[e],"float32");r=new Rc(n,"max",!0,!0,t);let i=a.runWebGLProgram(r,[e],"float32");return[s,i]}var Rse={kernelName:Kc,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:a}=e,{filterSize:r,strides:s,pad:i,includeBatchInIndex:o}=t,l=n;w.assert(a.shape.length===4,()=> ` Error in maxPool : input must be rank 4 but got rank $ { a . shape . length } . ` );let u=[1,1];w.assert(T.eitherStridesOrDilationsAreOne(s,u),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { s } and dilations '${u}' ` );let p=T.computePool2DInfo(a.shape,r,s,u,i),[d,c]=Dse(a,o,p,l);return[d,c]}};function Mse(e,t,n,a){let r=w.sizeFromShape(t),s=w.sizeFromShape(e.shape)/r,i=ce({inputs:{x:e},attrs:{shape:[s,r]},backend:a}),o=nl(i,"float32","mean",a),l=ce({inputs:{x:o},attrs:{shape:n},backend:a});return a.disposeIntermediateTensorInfo(i),a.disposeIntermediateTensorInfo(o),l}var Ose={kernelName:mo,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:a}=e,{keepDims:r,axis:s}=t,i=n,o=a.shape.length,l=w.parseAxisParam(s,a.shape),u=l,p=T.getAxesPermutation(u,o),d=p!=null,c=i.shouldExecuteOnCPU([a]),h=[],m=a;if(d){if(c){let x=i.texData.get(m.dataId).values,v=new Array(o);for(let C=0;C<v.length;C++)v[C]=a.shape[p[C]];let I=fk(x,a.shape,a.dtype,p,v);m=i.makeTensorInfo(v,a.dtype);let N=i.texData.get(m.dataId);N.values=I}else m=eg(a,p,i);h.push(m),u=T.getInnerMostAxes(u.length,o)}T.assertAxesAreInnerMostDims("sum",u,o);let[f,g]=T.computeOutAndReduceShapes(m.shape,u),b=f;r&&(b=T.expandShapeToKeepDim(f,l));let y=Mse(m,g,b,i);for(let x of h)i.disposeIntermediateTensorInfo(x);return y}};function Pse(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=w.parseAxisParam(s,r.shape),u=l,p=T.getAxesPermutation(u,o),d=r;p!=null&&(d=In({inputs:{x:r},backend:n,attrs:{perm:p}}),u=T.getInnerMostAxes(u.length,r.shape.length)),T.assertAxesAreInnerMostDims("min",u,o);let[c,h]=T.computeOutAndReduceShapes(d.shape,u),m=w.sizeFromShape(h),f=ce({inputs:{x:d},backend:n,attrs:{shape:[-1,m]}}),g=nl(f,f.dtype,"min",n),b;if(i){let y=T.expandShapeToKeepDim(c,l);b=ce({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=ce({inputs:{x:g},backend:n,attrs:{shape:c}});return n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),p!=null&&n.disposeIntermediateTensorInfo(d),b}var Lse={kernelName:fo,backendName:"webgl",kernelFunc:Pse},zse=bk+ `
2021-09-11 17:11:38 +02:00
return min ( a , b ) ;
2024-01-17 22:56:53 +01:00
` ,Wse= `
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:56:53 +01:00
` +tl+ `
2021-09-11 17:11:38 +02:00
return result ;
2024-01-17 22:56:53 +01:00
` ,Bse=fn({opSnippet:zse,packedOpSnippet:Wse,cpuKernelImpl:SQ}),Vse={kernelName:go,backendName:"webgl",kernelFunc:Bse},Use=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=t.map((u,p)=>u[0]+e[p]+u[1]);let a=e.length,r=ht(a),s=t.map(u=>u[0]).join(","),i=t.map((u,p)=>u[0]+e[p]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,a),l=n==="reflect"?0:1;if(a===1){this.userCode= `
int start = $ { s } ;
int end = $ { i } ;
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:56:53 +01:00
outC = start * 2 - outC - $ { l } ;
2021-09-11 17:11:38 +02:00
} else if ( outC >= end ) {
2024-01-17 22:56:53 +01:00
outC = ( end - 1 ) * 2 - outC + $ { l } ;
2021-09-11 17:11:38 +02:00
}
setOutput ( getX ( outC - start ) ) ;
}
` ;return}this.userCode= `
2024-01-17 22:56:53 +01:00
$ { r } start = $ { r } ( $ { s } ) ;
$ { r } end = $ { r } ( $ { i } ) ;
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { r } outC = getOutputCoords ( ) ;
for ( int i = 0 ; i < $ { a } ; i ++ ) {
2021-09-11 17:11:38 +02:00
if ( outC [ i ] < start [ i ] ) {
2024-01-17 22:56:53 +01:00
outC [ i ] = start [ i ] * 2 - outC [ i ] - $ { l } ;
2021-09-11 17:11:38 +02:00
} else if ( outC [ i ] >= end [ i ] ) {
2024-01-17 22:56:53 +01:00
outC [ i ] = ( end [ i ] - 1 ) * 2 - outC [ i ] + $ { l } ;
2021-09-11 17:11:38 +02:00
}
}
2024-01-17 22:56:53 +01:00
$ { r } coords = outC - start ;
setOutput ( getX ( $ { o } ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }},Gse=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.map((h,m)=>h[0]+e[m]+h[1]);let a=e.length,r=ht(a),s=t.map(h=>h[0]).join(","),i=t.map((h,m)=>h[0]+e[m]).join(","),o=kn("rc",a),l=kn("source",a),u= ` $ { o [ a - 1 ] } < $ { this . outputShape [ a - 1 ] } ` ,p=a===1?"source": ` vec2 ( $ { l . slice ( - 2 ) . join ( ) } ) ` ,d=n==="reflect"?0:1,c="";if(a===1){let h= `
$ { r } source = rc ;
2021-09-11 17:11:38 +02:00
if ( source < start ) {
2024-01-17 22:56:53 +01:00
source = start * 2 - source - $ { d } ;
2021-09-11 17:11:38 +02:00
} else if ( source >= end ) {
2024-01-17 22:56:53 +01:00
source = ( end - 1 ) * 2 - source + $ { d } ;
2021-09-11 17:11:38 +02:00
}
source -= start ;
2024-01-17 22:56:53 +01:00
` ;c= `
$ { r } rc = outputLoc ;
2021-09-11 17:11:38 +02:00
$ { h }
2024-01-17 22:56:53 +01:00
result [ 0 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
$ { o [ a - 1 ] } += 1 ;
if ( $ { u } ) {
2021-09-11 17:11:38 +02:00
$ { h }
2024-01-17 22:56:53 +01:00
result [ 1 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
2021-09-11 17:11:38 +02:00
}
` }else{let h= `
2024-01-17 22:56:53 +01:00
$ { r } source = rc ;
$ { r } lt = $ { r } ( lessThan ( source , start ) ) ;
$ { r } gte = $ { r } ( greaterThanEqual ( source , end ) ) ;
$ { r } orig = 1 - ( lt + gte ) ;
2021-09-11 17:11:38 +02:00
source = orig * source +
2024-01-17 22:56:53 +01:00
lt * ( start * 2 - source - $ { d } ) +
gte * ( ( end - 1 ) * 2 - source + $ { d } ) ;
2021-09-11 17:11:38 +02:00
source -= start ;
2024-01-17 22:56:53 +01:00
` ;c= `
$ { r } rc = outputLoc ;
2021-09-11 17:11:38 +02:00
$ { h }
2024-01-17 22:56:53 +01:00
result [ 0 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
$ { o [ a - 1 ] } += 1 ;
if ( $ { u } ) {
2021-09-11 17:11:38 +02:00
$ { h }
2024-01-17 22:56:53 +01:00
result [ 1 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
2021-09-11 17:11:38 +02:00
}
rc = outputLoc ;
2024-01-17 22:56:53 +01:00
$ { o [ a - 2 ] } += 1 ;
if ( $ { o [ a - 2 ] } < $ { this . outputShape [ a - 2 ] } ) {
2021-09-11 17:11:38 +02:00
$ { h }
2024-01-17 22:56:53 +01:00
result [ 2 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
$ { o [ a - 1 ] } += 1 ;
if ( $ { u } ) {
2021-09-11 17:11:38 +02:00
$ { h }
2024-01-17 22:56:53 +01:00
result [ 3 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
2021-09-11 17:11:38 +02:00
}
}
` }this.userCode= `
2024-01-17 22:56:53 +01:00
const $ { r } start = $ { r } ( $ { s } ) ;
const $ { r } end = $ { r } ( $ { i } ) ;
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { r } outputLoc = getOutputCoords ( ) ;
2021-09-11 17:11:38 +02:00
vec4 result = vec4 ( 0. ) ;
2024-01-17 22:56:53 +01:00
$ { c }
2021-09-11 17:11:38 +02:00
setOutput ( result ) ;
2020-12-15 14:15:43 +01:00
}
2024-01-17 22:56:53 +01:00
` }},Hse=({inputs:e,backend:t,attrs:n})=>{let{x:a}=e,{paddings:r,mode:s}=n,i=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Gse(a.shape,r,s):new Use(a.shape,r,s);return t.runWebGLProgram(i,[a],a.dtype)},qse={kernelName:bo,backendName:"webgl",kernelFunc:Hse},jse= ` if ( b == 0.0 ) return NAN ;
return mod ( a , b ) ; ` ,Kse= `
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:56:53 +01:00
` +tl+ `
2021-09-11 17:11:38 +02:00
return result ;
2024-01-17 22:56:53 +01:00
` ,Xse=fn({opSnippet:jse,packedOpSnippet:Kse}),Yse={kernelName:yo,backendName:"webgl",kernelFunc:Xse},Zse=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:56:53 +01:00
` }},Jse= `
2021-09-11 17:11:38 +02:00
if ( a == b ) {
return 1.0 ;
} ;
2024-01-17 22:56:53 +01:00
return a / b ; ` ,Qse= `
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:56:53 +01:00
` ,wF=fn({opSnippet:Jse,packedOpSnippet:Qse,checkOutOfBounds:!0}),eie={kernelName:ji,backendName:"webgl",kernelFunc:wF},GS="return a - b;",kF=fn({opSnippet:GS,packedOpSnippet:GS,supportsComplex:!0,cpuKernelImpl:HQ}),tie={kernelName:Uo,backendName:"webgl",kernelFunc:kF};function IF(e){let{inputs:t,backend:n,attrs:a}=e,{logits:r}=t,{dim:s}=a,i=w.parseAxisParam([s],r.shape),o=vF({inputs:{x:r},backend:n,attrs:{reductionIndices:i,keepDims:!1}}),l=T.expandShapeToKeepDim(o.shape,i),u=ce({inputs:{x:o},backend:n,attrs:{shape:l}}),p=kF({inputs:{a:r,b:u},backend:n}),d=bF({inputs:{x:p},backend:n}),c=tg({inputs:{x:d},backend:n,attrs:{axis:i,keepDims:!1}}),h=ce({inputs:{x:c},backend:n,attrs:{shape:l}}),m=wF({inputs:{a:d,b:h},backend:n});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(u),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(h),m}var nie={kernelName:Bo,backendName:"webgl",kernelFunc:IF};function aie(e){let{inputs:t,backend:n,attrs:a}=e,{logits:r}=t,{numSamples:s,seed:i,normalized:o}=a,l=o?r:IF({inputs:{logits:r},backend:n,attrs:{dim:r.shape.length-1}}),u=l.shape[0],p=l.shape[1],d=new Zse(u,p,s),c=[[i]],h=n.runWebGLProgram(d,[l],"int32",c);return o||n.disposeIntermediateTensorInfo(l),h}var rie={kernelName:zu,backendName:"webgl",kernelFunc:aie},sie=Ma+ `
2022-01-14 16:04:13 +01:00
return - x ;
2024-01-17 22:56:53 +01:00
` ,iie= `
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:56:53 +01:00
` ;function oie(e){let{inputs:t,backend:n}=e,{x:a}=t;if(n.shouldExecuteOnCPU([a])){let s=n.texData.get(a.dataId),[i,o]=TQ(s.values,a.shape,a.dtype);return n.makeTensorInfo(o,a.dtype,i)}let r;return G().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new ss(a.shape,iie):r=new ir(a.shape,sie),n.runWebGLProgram(r,[a],a.dtype)}var lie={kernelName:Wu,backendName:"webgl",kernelFunc:oie},uie=fr.nonMaxSuppressionV3Impl;function pie(e){T.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:a}=e,{boxes:r,scores:s}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:l}=a,u=n.readSync(r.dataId),p=n.readSync(s.dataId),{selectedIndices:d}=uie(u,p,i,o,l);return n.makeTensorInfo([d.length],"int32",new Int32Array(d))}var cie={kernelName:Vu,backendName:"webgl",kernelFunc:pie},die=fr.nonMaxSuppressionV4Impl;function hie(e){T.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:a}=e,{boxes:r,scores:s}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:l,padToMaxOutputSize:u}=a,p=n.readSync(r.dataId),d=n.readSync(s.dataId),{selectedIndices:c,validOutputs:h}=die(p,d,i,o,l,u);return[n.makeTensorInfo([c.length],"int32",new Int32Array(c)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var mie={kernelName:Uu,backendName:"webgl",kernelFunc:hie},fie=fr.nonMaxSuppressionV5Impl;function gie(e){T.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:a}=e,{boxes:r,scores:s}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:l,softNmsSigma:u}=a,p=n.readSync(r.dataId),d=n.readSync(s.dataId),c=i,h=o,m=l,f=u,{selectedIndices:g,selectedScores:b}=fie(p,d,c,h,m,f);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var bie={kernelName:Gu,backendName:"webgl",kernelFunc:gie},yie=class{constructor(e,t,n,a){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:56:53 +01:00
setOutput ( mix ( float ( $ { a } ) , 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:56:53 +01:00
` }},xie=e=>{let{inputs:t,backend:n,attrs:a}=e,{indices:r}=t,{dtype:s,depth:i,onValue:o,offValue:l}=a,u=w.sizeFromShape(r.shape),p=new yie(u,i,o,l),d=ce({inputs:{x:r},backend:n,attrs:{shape:[u]}}),c=n.runWebGLProgram(p,[d],s);n.disposeIntermediateTensorInfo(d);let h=[...r.shape,i],m=ce({inputs:{x:c},backend:n,attrs:{shape:h}});return n.disposeIntermediateTensorInfo(c),m},vie={kernelName:vo,backendName:"webgl",kernelFunc:xie};function _m(e){let{inputs:t,backend:n}=e,{x:a}=t;if(a.dtype==="complex64"){let r=Pd({inputs:{input:a},backend:n}),s=_m({inputs:{x:r},backend:n}),i=ng({inputs:{input:a},backend:n}),o=_m({inputs:{x:i},backend:n}),l=Ms({inputs:{real:s,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),l}else return Ld({attrs:{shape:a.shape,dtype:a.dtype,value:a.dtype==="string"?"":0},backend:n})}var wie={kernelName:up,backendName:"webgl",kernelFunc:_m};function SF(e){let{inputs:t,backend:n}=e,{x:a}=t;if(a.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(a.dtype==="complex64"){let r=Pd({inputs:{input:a},backend:n}),s=SF({inputs:{x:r},backend:n}),i=ng({inputs:{input:a},backend:n}),o=_m({inputs:{x:i},backend:n}),l=Ms({inputs:{real:s,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),l}else return Ld({attrs:{shape:a.shape,dtype:a.dtype,value:1},backend:n})}var kie={kernelName:Hu,backendName:"webgl",kernelFunc:SF};function Iie(e){let{inputs:t,backend:n,attrs:a}=e,{axis:r}=a;if(t.length===1)return Iv({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let s=t[0].shape,i=t[0].dtype;t.forEach(p=>{w.assertShapesMatch(s,p.shape,"All tensors passed to stack must have matching shapes"),w.assert(i===p.dtype,()=>"All tensors passed to stack must have matching dtypes")});let o=[],l=t.map(p=>{let d=Iv({inputs:{input:p},backend:n,attrs:{dim:r}});return o.push(d),d}),u=uF({inputs:l,backend:n,attrs:{axis:r}});return o.forEach(p=>n.disposeIntermediateTensorInfo(p)),u}var Sie={kernelName:qu,backendName:"webgl",kernelFunc:Iie},Nie=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((l,u)=>l[0]+e[u]+l[1]);let a=e.length,r=ht(a),s=t.map(l=>l[0]).join(","),i=t.map((l,u)=>l[0]+e[u]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,a);if(a===1){this.userCode= `
int start = $ { s } ;
int end = $ { i } ;
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:56:53 +01:00
$ { r } start = $ { r } ( $ { s } ) ;
$ { r } end = $ { r } ( $ { i } ) ;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { r } 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:56:53 +01:00
$ { r } coords = outC - start ;
setOutput ( getX ( $ { o } ) ) ;
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }},Tie=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((m,f)=>m[0]+e[f]+m[1]);let a=e.length,r=ht(a),s=t.map(m=>m[0]).join(","),i=t.map((m,f)=>m[0]+e[f]).join(","),o=kn("rc",a),l=kn("source",a),u= ` $ { o [ a - 1 ] } < $ { this . outputShape [ a - 1 ] } ` ,p=a===1?"source": ` vec2 ( $ { l . slice ( - 2 ) . join ( ) } ) ` ,d=[ ` $ { r } rc = outputLoc ; ` , ` $ { o [ a - 1 ] } += 1 ;
if ( $ { u } ) {
` ,a===1?"": ` }
2021-09-11 17:11:38 +02:00
rc = outputLoc ;
2024-01-17 22:56:53 +01:00
$ { o [ a - 2 ] } += 1 ;
if ( $ { o [ a - 2 ] } < $ { this . outputShape [ a - 2 ] } ) { ` ,a===1?"": ` $ { o [ a - 1 ] } += 1 ;
if ( $ { u } ) { ` ],c=a===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="";for(let m=0,f=a===1?2:4;m<f;m++)h+= `
$ { d [ m ] }
if ( $ { c } ) {
result [ $ { m } ] = float ( value ) ;
2021-09-11 17:11:38 +02:00
} else {
2024-01-17 22:56:53 +01:00
$ { r } source = rc - start ;
result [ $ { m } ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` ;h+=a===1?"} ":"}}",this.userCode= `
const $ { r } start = $ { r } ( $ { s } ) ;
const $ { r } end = $ { r } ( $ { i } ) ;
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { r } 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:56:53 +01:00
` }},NF=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{paddings:s,constantValue:i}=a;if(w.sizeFromShape(r.shape)===0){let u=s.map((p,d)=>p[0]+r.shape[d]+p[1]);return Ld({backend:n,attrs:{shape:u,value:i,dtype:r.dtype}})}let o=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Tie(r.shape,s,i):new Nie(r.shape,s,i),l=[[i]];return n.runWebGLProgram(o,[r],r.dtype,l)},Cie={kernelName:wo,backendName:"webgl",kernelFunc:NF},Eie= `
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:56:53 +01:00
` ,_ie= `
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:56:53 +01:00
` +tl+ `
2021-09-11 17:11:38 +02:00
return result ;
2024-01-17 22:56:53 +01:00
` ,Aie=fn({opSnippet:Eie,packedOpSnippet:_ie}),Fie={kernelName:ko,backendName:"webgl",kernelFunc:Aie};function $ ie(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=[],u=w.parseAxisParam(s,r.shape),p=u,d=T.getAxesPermutation(p,o),c=r;d!=null&&(c=In({inputs:{x:r},backend:n,attrs:{perm:d}}),p=T.getInnerMostAxes(p.length,o),l.push(c)),T.assertAxesAreInnerMostDims("prod",p,o);let h;if(n.shouldExecuteOnCPU([c])){let m=n.texData.get(c.dataId).values,{outVals:f,outShape:g,outDtype:b}=EQ(c.shape,c.dtype,m,p);h=n.makeTensorInfo(g,b,f)}else{let[m,f]=T.computeOutAndReduceShapes(c.shape,p),g=w.sizeFromShape(f),b=ce({inputs:{x:c},backend:n,attrs:{shape:[-1,g]}}),y=Xm(r.dtype),x=nl(b,y,"prod",n);h=ce({inputs:{x},backend:n,attrs:{shape:m}}),l.push(b),l.push(x)}if(i){l.push(h);let m=T.expandShapeToKeepDim(h.shape,u);h=ce({inputs:{x:h},backend:n,attrs:{shape:m}})}return l.forEach(m=>n.disposeIntermediateTensorInfo(m)),h}var Die={kernelName:So,backendName:"webgl",kernelFunc: $ ie};function Rie(e){let{inputs:t,backend:n,attrs:a}=e,{paramsNestedSplits:r,paramsDenseValues:s,indices:i}=t,{outputRaggedRank:o}=a,l=r.map(b=>n.readSync(b.dataId)),u=r.map(b=>b.shape),p=n.readSync(s.dataId),d=n.readSync(i.dataId),[c,h,m]=_Q(l,u,p,s.shape,s.dtype,d,i.shape,o),f=c.map(b=>n.makeTensorInfo([b.length],"int32",b)),g=n.makeTensorInfo(m,s.dtype,h);return f.concat([g])}var Mie={kernelName:Gm,backendName:"webgl",kernelFunc:Rie};function Oie(e){let{inputs:t,backend:n}=e,{starts:a,limits:r,deltas:s}=t,i=n.readSync(a.dataId),o=n.readSync(r.dataId),l=n.readSync(s.dataId),[u,p]=AQ(i,a.shape,a.dtype,o,r.shape,l,s.shape),d=n.makeTensorInfo([u.length],"int32",u),c=n.makeTensorInfo([p.length],a.dtype,p);return[d,c]}var Pie={kernelName:Hm,backendName:"webgl",kernelFunc:Oie};function Lie(e){let{inputs:t,backend:n,attrs:a}=e,{shape:r,values:s,defaultValue:i,rowPartitionTensors:o}=t,{rowPartitionTypes:l}=a,u=n.readSync(r.dataId),p=n.readSync(s.dataId),d=n.readSync(i.dataId),c=o.map(g=>n.readSync(g.dataId)),h=o.map(g=>g.shape),[m,f]=FQ(u,r.shape,p,s.shape,s.dtype,d,i.shape,c,h,l);return n.makeTensorInfo(m,s.dtype,f)}var zie={kernelName:qm,backendName:"webgl",kernelFunc:Lie},TF=e=>{let{backend:t,attrs:n}=e,{start:a,stop:r,step:s,dtype:i}=n,o= $ Q(a,r,s,i);return t.makeTensorInfo([o.length],i,o)},Wie={kernelName:Xc,backendName:"webgl",kernelFunc:TF},Bie="return 1.0 / x;",Vie=Ze({opSnippet:Bie}),Uie={kernelName:No,backendName:"webgl",kernelFunc:Vie},Gie=Ma+ `
2021-09-11 17:11:38 +02:00
return ( x < 0.0 ) ? 0.0 : x ;
2024-01-17 22:56:53 +01:00
` ,Hie= `
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:56:53 +01:00
` ,qie=Ze({opSnippet:Gie,packedOpSnippet:Hie}),jie={kernelName:To,backendName:"webgl",kernelFunc:qie},Kie=Ma+ `
2021-09-11 17:11:38 +02:00
return ( x < 0.0 ) ? 0.0 : min ( 6.0 , x ) ;
2024-01-17 22:56:53 +01:00
` ,Xie= `
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:56:53 +01:00
` ,Yie=Ze({opSnippet:Kie,packedOpSnippet:Xie}),Zie={kernelName:_o,backendName:"webgl",kernelFunc:Yie},Jie=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],p=[a&&t>1?t-1:t,a&&n>1?n-1:n],d;r?d="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":d="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2021-09-11 17:11:38 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2 (
2024-01-17 22:56:53 +01:00
$ { u [ 0 ] / p [ 0 ] } ,
$ { u [ 1 ] / p [ 1 ] } ) ;
const vec2 inputShapeRC = vec2 ( $ { i } . 0 , $ { o } . 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:56:53 +01:00
vec2 sourceFracIndexRC = $ { d } ;
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:56:53 +01:00
` }},Qie=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],p=[a&&t>1?t-1:t,a&&n>1?n-1:n],d;r?d="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":d="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2021-09-11 17:11:38 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3 (
2024-01-17 22:56:53 +01:00
$ { u [ 0 ] / p [ 0 ] } ,
$ { u [ 1 ] / p [ 1 ] } ,
$ { u [ 1 ] / p [ 1 ] } ) ;
const vec3 inputShapeRC = vec3 ( $ { i } . 0 , $ { o } . 0 ,
$ { o } . 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:56:53 +01:00
vec3 sourceFracIndexRC = $ { d } ;
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:56:53 +01:00
bool hasNextCol = d < $ { l - 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:56:53 +01:00
` }};function eoe(e){let{inputs:t,backend:n,attrs:a}=e,{images:r}=t,{alignCorners:s,halfPixelCenters:i,size:o}=a,[l,u]=o,p=G().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new Qie(r.shape,l,u,s,i):new Jie(r.shape,l,u,s,i);return n.runWebGLProgram(p,[r],"float32")}var toe={kernelName:Eo,backendName:"webgl",kernelFunc:eoe},noe=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,a,r]=t,[,s,i]=e,o=[n&&s>1?a-1:a,n&&i>1?r-1:r],l=[n&&s>1?s-1:s,n&&i>1?i-1:i],u=o[0]/l[0],p=o[1]/l[1],d=1/u,c=1/p,h=Math.ceil(d)*2+2,m=Math.ceil(c)*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:56:53 +01:00
const float heightScale = float ( $ { u } ) ;
const float widthScale = float ( $ { p } ) ;
2020-10-11 18:41:17 +02:00
2024-01-17 22:56:53 +01:00
const float invHeightScale = float ( $ { d } ) ;
const float invWidthScale = float ( $ { c } ) ;
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:56:53 +01:00
const int winWidth = int ( $ { m } ) ;
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-05-27 22:04:17 +02:00
2021-09-11 17:11:38 +02:00
float startCLerp = floor ( float ( c ) * invWidthScale ) ;
int startDyC = int ( startCLerp - float ( winWidth / 2 ) ) ;
2021-05-27 22:04:17 +02:00
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:56:53 +01:00
if ( dyR < 0 || dyR >= $ { s } ) {
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:56:53 +01:00
if ( dyC < 0 || dyC >= $ { i } ) {
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:56:53 +01:00
int bottomDxRIndex = int ( min ( ceil ( dxR ) , $ { a - 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:56:53 +01:00
int rightDxCIndex = int ( min ( ceil ( dxC ) , $ { r - 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:56:53 +01:00
` }};function aoe(e){let{inputs:t,backend:n,attrs:a}=e,{images:r,dy:s}=t,{alignCorners:i}=a,o=new noe(s.shape,r.shape,i);return n.runWebGLProgram(o,[s],s.dtype)}var roe={kernelName:Xu,backendName:"webgl",kernelFunc:aoe},soe=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],p=[a&&t>1?t-1:t,a&&n>1?n-1:n],d=a?"0.5":"0.0",c;r?c="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":c="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2021-09-11 17:11:38 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2 (
2024-01-17 22:56:53 +01:00
$ { u [ 0 ] / p [ 0 ] } ,
$ { u [ 1 ] / p [ 1 ] } ) ;
const vec2 inputShapeRC = vec2 ( $ { i } . 0 , $ { o } . 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:56:53 +01:00
vec2 sourceFracIndexRC = $ { c } ;
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:56:53 +01:00
min ( inputShapeRC - 1.0 , floor ( sourceFracIndexRC + $ { d } ) ) ) ;
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:56:53 +01:00
` }},ioe=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],p=[a&&t>1?t-1:t,a&&n>1?n-1:n],d=a?"0.5":"0.0",c;r?c="max((vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC, vec3(0.0))":c="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2021-09-11 17:11:38 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3 (
2024-01-17 22:56:53 +01:00
$ { u [ 0 ] / p [ 0 ] } ,
$ { u [ 1 ] / p [ 1 ] } ,
$ { u [ 1 ] / p [ 1 ] } ) ;
const vec3 inputShapeRC = vec3 ( $ { i } . 0 , $ { o } . 0 ,
$ { o } . 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:56:53 +01:00
vec3 sourceFracIndexRC = $ { c } ;
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:56:53 +01:00
min ( inputShapeRC - 1.0 , floor ( sourceFracIndexRC + $ { d } ) ) ) ;
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:56:53 +01:00
bool hasNextCol = d < $ { l - 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:56:53 +01:00
` }};function ooe(e){let{inputs:t,backend:n,attrs:a}=e,{images:r}=t,{alignCorners:s,halfPixelCenters:i,size:o}=a,[l,u]=o,p=G().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new ioe(r.shape,l,u,s,i):new soe(r.shape,l,u,s,i);return n.runWebGLProgram(p,[r],r.dtype)}var loe={kernelName:Co,backendName:"webgl",kernelFunc:ooe},uoe=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,a,r]=t,[,s,i]=e,o=[n&&s>1?a-1:a,n&&i>1?r-1:r],l=[n&&s>1?s-1:s,n&&i>1?i-1:i],u=o[0]/l[0],p=o[1]/l[1],d=1/u,c=1/p,h=Math.ceil(d)*2+2,m=Math.ceil(c)*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:56:53 +01:00
const float heightScale = float ( $ { u } ) ;
const float widthScale = float ( $ { p } ) ;
2020-12-15 14:15:43 +01:00
2024-01-17 22:56:53 +01:00
const float invHeightScale = float ( $ { d } ) ;
const float invWidthScale = float ( $ { c } ) ;
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:56:53 +01:00
const int winWidth = int ( $ { m } ) ;
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:56:53 +01:00
if ( dyR < 0 || dyR >= $ { s } ) {
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:56:53 +01:00
if ( dyC < 0 || dyC >= $ { i } ) {
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:56:53 +01:00
float ( $ { o [ 0 ] } ) *
( float ( dyR ) / float ( $ { l [ 0 ] } ) ) ;
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
float sourceFracCol =
2024-01-17 22:56:53 +01:00
float ( $ { o [ 1 ] } ) *
( float ( dyC ) / float ( $ { l [ 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:56:53 +01:00
float ( int ( $ { a } ) - 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:56:53 +01:00
float ( int ( $ { r } ) - 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:56:53 +01:00
` }};function poe(e){let{inputs:t,backend:n,attrs:a}=e,{images:r,dy:s}=t,{alignCorners:i}=a,o=new uoe(s.shape,r.shape,i);return n.runWebGLProgram(o,[s],s.dtype)}var coe={kernelName:Ku,backendName:"webgl",kernelFunc:poe},doe=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:56:53 +01:00
` ;return}let a=i=>t.indexOf(i)!==-1&&e[i]!==1? ` $ { e [ i ] } - coords [ $ { i } ] - 1 ` : ` coords [ $ { i } ] ` ,r=e.map((i,o)=>a(o)).join(","),s=ht(n);this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { s } coords = getOutputCoords ( ) ;
setOutput ( getX ( $ { r } ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }},hoe=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 a=kn("rc",n),r= ` $ { a [ n - 1 ] } + 1 < $ { this . outputShape [ n - 1 ] } ` ,s= ` $ { a [ n - 2 ] } + 1 < $ { this . outputShape [ n - 2 ] } ` ,i=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:56:53 +01:00
if ( $ { r } ) {
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:56:53 +01:00
$ { i } rc = getOutputCoords ( ) ;
2021-09-11 17:11:38 +02:00
vec4 result = vec4 ( 0. ) ;
2024-01-17 22:56:53 +01:00
result . r = $ { o ( a . slice ( ) ) } ;
if ( $ { r } ) {
result . g = $ { l ( a . slice ( ) ) } ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
if ( $ { s } ) {
result . b = $ { u ( a . slice ( ) ) } ;
if ( $ { r } ) {
result . a = $ { p ( a . slice ( ) ) } ;
2021-09-11 17:11:38 +02:00
}
}
setOutput ( result ) ;
}
2024-01-17 22:56:53 +01:00
` ;function o(h){return d(h)}function l(h){return h[n-1]="("+h[n-1]+" + 1)",d(h)}function u(h){return h[n-2]="("+h[n-2]+" + 1)",d(h)}function p(h){return h[n-1]="("+h[n-1]+" + 1)",h[n-2]="("+h[n-2]+" + 1)",d(h)}function d(h){let m=e.map((b,y)=>c(y,h)),f=m.join(","),g=m.slice(-2).join(",");return ` getChannel ( getX ( $ { f } ) , vec2 ( $ { g } ) ) ` }function c(h,m){return t.indexOf(h)!==-1&&e[h]!==1? ` $ { e [ h ] } - $ { m [ h ] } - 1 ` : ` $ { m [ h ] } ` }}};function moe(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{dims:s}=a,i=r.shape.length,o=w.parseAxisParam(s,r.shape);if(i===0)return aa({inputs:{x:r},backend:n});let l=G().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new hoe(r.shape,o):new doe(r.shape,o);return n.runWebGLProgram(l,[r],r.dtype)}var foe={kernelName:Ao,backendName:"webgl",kernelFunc:moe},goe=class{constructor(e,t){this.variableNames=["Image"],this.outputShape=[],this.customUniforms=[{name:"params",type:"vec4"}];let n=e[1],a=e[2];this.outputShape=e;let r="";typeof t=="number"?r= ` float outputValue = $ { t . toFixed ( 2 ) } ; ` :r= `
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:56:53 +01:00
$ { r }
if ( coordX >= 0 && coordX < $ { a } && 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:56:53 +01:00
` }},boe={kernelName:pp,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:a}=e,{radians:r,fillValue:s,center:i}=t,o=n,l=new goe(a.shape,s),[u,p]=T.getImageCenter(i,a.shape[1],a.shape[2]),d=[[u,p,Math.sin(r),Math.cos(r)]];return o.runWebGLProgram(l,[a],a.dtype,d)}},yoe= `
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:56:53 +01:00
` ,xoe=Ze({opSnippet:yoe}),voe={kernelName:Fo,backendName:"webgl",kernelFunc:xoe},woe="return inversesqrt(x);",koe=Ze({opSnippet:woe,cpuKernelImpl:DQ}),Ioe={kernelName: $ o,backendName:"webgl",kernelFunc:koe},vk=class{constructor(e,t,n,a,r,s,i=!0,o=!1){this.variableNames=["updates","indices","defaultValue"],this.outputShape=s;let l=ht(r.length),u=ht(s.length),p="";n===1?p="i":n===2&&(p="i, j");let d= ` getIndices ( $ { p } ) ` ,c="";a===1?c="i":a===2&&(c="i, coords[1]");let h= ` getUpdates ( $ { c } ) ` ,m="";o&&(m="coords[0], coords[1]");let f= ` getDefaultValue ( $ { m } ) ` ,g=t>1?"strides[j]":"strides";this.userCode= `
$ { l } strides = $ { l } ( $ { r } ) ;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { u } 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:56:53 +01:00
int index = round ( $ { d } ) ;
flattenedIndex += index * $ { g } ;
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:56:53 +01:00
setOutput ( mix ( $ { f } , sum , float ( found ) ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }},Soe=class{constructor(e,t,n,a,r,s,i=!0,o=!1){this.variableNames=["updates","indices","defaultValue"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=s;let l=ht(r.length),u=ht(s.length),p="";n===1?p="i":n===2&&(p="i, j");let d= ` getIndices ( $ { p } ) ` ,c="";a===1?c="i":a===2&&(c="i, coords[1]");let h= ` getUpdates ( $ { c } ) ` ,m="";o&&(m="coords[0], coords[1]");let f= ` getDefaultValue ( $ { m } ) ` ,g=t>1?"strides[j]":"strides",b=t>1?"strides[j + 1]":"strides";this.userCode= `
$ { l } strides = $ { l } ( $ { r } ) ;
2023-03-21 13:00:18 +01:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { u } 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:56:53 +01:00
ivec4 index = round ( $ { d } ) ;
flattenedIndex += index . xz * $ { g } ;
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:56:53 +01:00
setOutput ( mix ( $ { f } , sum , found ) ) ;
2023-03-21 13:00:18 +01:00
}
2024-01-17 22:56:53 +01:00
` }};function Noe(e){let{inputs:t,backend:n,attrs:a}=e,{indices:r,updates:s}=t,{shape:i}=a,{sliceRank:o,numUpdates:l,sliceSize:u,strides:p,outputSize:d}=T.calculateShapes(s,r,i),c=[d/u,u];if(d===0)return n.makeTensorInfo(i,r.dtype);let h=ce({inputs:{x:r},backend:n,attrs:{shape:[l,o]}}),m=ce({inputs:{x:s},backend:n,attrs:{shape:[l,u]}}),f=n.makeTensorInfo([],"float32",new Float32Array([0])),g;G().getBool("WEBGL_PACK")?g=new Soe(l,o,h.shape.length,m.shape.length,p,c):g=new vk(l,o,h.shape.length,m.shape.length,p,c);let b=n.runWebGLProgram(g,[m,h,f],m.dtype),y=ce({inputs:{x:b},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(b),n.disposeIntermediateTensorInfo(f),y}var Toe={kernelName:Yu,backendName:"webgl",kernelFunc:Noe},Coe=class{constructor(e,t,n,a){this.variableNames=["sortedSequence","values"],this.customUniforms=[{name:"numInputs",type:"int"}],this.outputShape=[e,n];let r="while (left < right) {",s= ` for ( int i = 0 ; i < $ { Math . ceil ( Math . log2 ( t + 1 ) ) } ; ++ i ) { if ( left >= right ) break ; ` ,i=G().getNumber("WEBGL_VERSION")===2?r:s,o=a==="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:56:53 +01:00
$ { i }
2022-05-18 14:36:05 +02:00
mid = ( left + right ) / 2 ;
2024-01-17 22:56:53 +01:00
if ( getSortedSequence ( batch , mid ) $ { o } 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:56:53 +01:00
` }};function Eoe(e){let{inputs:t,backend:n,attrs:a}=e,{sortedSequence:r,values:s}=t,{side:i}=a,o=new Coe(r.shape[0],r.shape[1],s.shape[1],i),l=[[r.shape[1]]];return n.runWebGLProgram(o,[r,s],"int32",l)}var _oe={kernelName:Ju,backendName:"webgl",kernelFunc:Eoe},Aoe=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.outputShape=t;let a,r;if(n>4)throw Error( ` Where for rank $ { n } is not yet supported ` );if(n===1)r="resRC",a="resRC";else{let i=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],l=[];for(let u=0;u<t.length;u++)l.push( ` $ { i [ u ] } ` ),u<e&&o.push( ` $ { i [ u ] } ` );a=o.join(),r=l.join()}let s=ht(n);this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { s } resRC = getOutputCoords ( ) ;
float cVal = getC ( $ { a } ) ;
2021-09-11 17:11:38 +02:00
if ( cVal >= 1.0 ) {
2024-01-17 22:56:53 +01:00
setOutput ( getA ( $ { r } ) ) ;
2021-09-11 17:11:38 +02:00
} else {
2024-01-17 22:56:53 +01:00
setOutput ( getB ( $ { r } ) ) ;
2021-09-11 17:11:38 +02:00
}
2021-03-26 15:26:02 +01:00
}
2024-01-17 22:56:53 +01:00
` }};function Foe(e){let{inputs:t,backend:n}=e,{condition:a,t:r,e:s}=t,i=new Aoe(a.shape.length,r.shape,r.shape.length);return n.runWebGLProgram(i,[a,r,s],ga(r.dtype,s.dtype))}var $ oe={kernelName:Qu,backendName:"webgl",kernelFunc:Foe},Doe= `
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:56:53 +01:00
` ,Roe=Ze({opSnippet:Doe}),Moe={kernelName:Do,backendName:"webgl",kernelFunc:Roe},Ooe=Cp+ `
2022-01-14 16:04:13 +01:00
return 1.0 / ( 1.0 + exp ( - 1.0 * x ) ) ;
2024-01-17 22:56:53 +01:00
` ,Poe= `
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:56:53 +01:00
` ,Loe=Ze({opSnippet:Ooe,packedOpSnippet:Poe,cpuKernelImpl:MQ}),zoe={kernelName:Po,backendName:"webgl",kernelFunc:Loe},Woe= `
2021-09-11 17:11:38 +02:00
if ( isnan ( x ) ) { return 0.0 ; }
return sign ( x ) ;
2024-01-17 22:56:53 +01:00
` ,Boe=Ze({opSnippet:Woe}),Voe={kernelName:Oo,backendName:"webgl",kernelFunc:Boe},Uoe=Cp+ `
2021-09-11 17:11:38 +02:00
return sin ( x ) ;
2024-01-17 22:56:53 +01:00
` ,Goe= `
2023-03-21 13:00:18 +01:00
vec4 result = sin ( x ) ;
bvec4 isNaN = isnan ( x ) ;
2024-01-17 22:56:53 +01:00
$ { tl }
2023-03-21 13:00:18 +01:00
return result ;
2024-01-17 22:56:53 +01:00
` ,Hoe=Ze({opSnippet:Uoe,packedOpSnippet:Goe}),qoe={kernelName:Ro,backendName:"webgl",kernelFunc:Hoe},joe= `
2021-09-11 17:11:38 +02:00
float e2x = exp ( x ) ;
return ( e2x - 1.0 / e2x ) / 2.0 ;
2024-01-17 22:56:53 +01:00
` ,Koe=Ze({opSnippet:joe}),Xoe={kernelName:Mo,backendName:"webgl",kernelFunc:Koe},Yoe= `
2021-09-11 17:11:38 +02:00
float epsilon = 1.1920928955078125 e - 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:56:53 +01:00
` ,Zoe=Ze({opSnippet:Yoe}),Joe={kernelName:Lo,backendName:"webgl",kernelFunc:Zoe},Qoe=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{blockShape:s,paddings:i}=a;w.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let o=s.reduce((b,y)=>b*y),l=[[0,0]];l.push(...i);for(let b=1+s.length;b<r.shape.length;++b)l.push([0,0]);let u=[],p=NF({inputs:{x:r},backend:n,attrs:{paddings:l,constantValue:0}}),d=T.getReshaped(p.shape,s,o,!1),c=T.getPermuted(d.length,s.length,!1),h=T.getReshapedPermuted(p.shape,s,o,!1),m=ce({inputs:{x:p},backend:n,attrs:{shape:d}}),f=In({inputs:{x:m},backend:n,attrs:{perm:c}}),g=ce({inputs:{x:f},backend:n,attrs:{shape:h}});return u.push(p),u.push(m),u.push(f),u.forEach(b=>n.disposeIntermediateTensorInfo(b)),g},ele={kernelName:tp,backendName:"webgl",kernelFunc:Qoe};function tle(e){let{inputs:t,backend:n}=e,{indices:a,values:r,denseShape:s,defaultValue:i}=t;if(s.shape.length!==1)throw new Error( ` Dense shape must be a vector , saw :
$ { s . shape } ` );if(a.shape.length!==2)throw new Error( ` Indices must be a matrix , saw :
$ { a . shape } ` );if(r.shape.length!==1)throw new Error( ` Values must be a vector , saw :
$ { r . shape } ` );if(i.shape.length!==0)throw new Error( ` Default value must be a scalar , saw :
$ { i . shape } ` );let o=n.readSync(a.dataId),l=n.readSync(r.dataId),u=n.readSync(s.dataId),p=n.readSync(i.dataId)[0],[d,c,h,m,f]=PQ(o,a.shape,a.dtype,l,r.dtype,u,p);return[n.makeTensorInfo(c,a.dtype,d),n.makeTensorInfo([c[0]],r.dtype,h),n.makeTensorInfo([m.length],"bool",new Uint8Array(m.map(g=>Number(g)))),n.makeTensorInfo([f.length],a.dtype,new Int32Array(f))]}var nle={kernelName:Yc,backendName:"webgl",kernelFunc:tle};function ale(e){let{inputs:t,backend:n}=e,{inputIndices:a,inputShape:r,newShape:s}=t;if(a.shape.length!==2)throw new Error( ` Input indices should be a matrix but received shape $ { a . shape } ` );if(r.shape.length!==1)throw new Error( ` Input shape should be a vector but received shape $ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Target shape should be a vector but received shape $ { s . shape } ` );let i=Array.from(n.readSync(r.dataId)),o=n.readSync(a.dataId),l=Array.from(n.readSync(s.dataId)),[u,p,d]=LQ(o,a.shape,a.dtype,i,l);return[n.makeTensorInfo(p,a.dtype,u),n.makeTensorInfo([d.length],s.dtype,new Int32Array(d))]}var rle={kernelName:ap,backendName:"webgl",kernelFunc:ale};function sle(e){let{inputs:t,backend:n}=e,{data:a,indices:r,segmentIds:s}=t;if(a.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
$ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
$ { s . shape } ` );let i=n.readSync(a.dataId),o=n.readSync(r.dataId),l=n.readSync(s.dataId),[u,p]=KA(i,a.shape,a.dtype,o,l,!0);return n.makeTensorInfo(p,a.dtype,u)}var ile={kernelName:Zc,backendName:"webgl",kernelFunc:sle};function ole(e){let{inputs:t,backend:n}=e,{data:a,indices:r,segmentIds:s}=t;if(a.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(r.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
$ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
$ { s . shape } ` );let i=n.readSync(a.dataId),o=n.readSync(r.dataId),l=n.readSync(s.dataId),[u,p]=KA(i,a.shape,a.dtype,o,l);return n.makeTensorInfo(p,a.dtype,u)}var lle={kernelName:Jc,backendName:"webgl",kernelFunc:ole};function ule(e){let{inputs:t,backend:n,attrs:a}=e,{sparseIndices:r,sparseValues:s,defaultValue:i}=t,{outputShape:o}=a,{sliceRank:l,numUpdates:u,sliceSize:p,strides:d,outputSize:c}=T.calculateShapes(s,r,o),h=!1;if(s.dtype==="string"){let b=n.bufferSync(r),y=n.bufferSync(s),x=w.decodeString(n.readSync(i.dataId)[0]),v=RQ(b,y,o,c,p,u,l,d,x,h);return n.makeTensorInfo(o,v.dtype,v.values)}let m=new vk(u,l,r.shape.length,s.shape.length,d,[c,1],h),f=n.runWebGLProgram(m,[s,r,i],s.dtype),g=ce({inputs:{x:f},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(f),g}var ple={kernelName:rp,backendName:"webgl",kernelFunc:ule};function cle(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{numOrSizeSplits:s,axis:i}=a,o=w.parseAxisParam(i,r.shape)[0],l=T.prepareSplitSize(r,s,o),u=r.shape.length,p=new Array(u).fill(0),d=r.shape.slice();return l.map(c=>{let h=[...d];h[o]=c;let m=Ep({inputs:{x:r},backend:n,attrs:{begin:p,size:h}});return p[o]+=c,m})}var dle={kernelName:np,backendName:"webgl",kernelFunc:cle},HS="return sqrt(x);",hle=Ze({opSnippet:HS,packedOpSnippet:HS,cpuKernelImpl:zQ}),mle={kernelName:zo,backendName:"webgl",kernelFunc:hle},fle="return x * x;",gle=Ze({opSnippet:fle}),ble={kernelName:Qc,backendName:"webgl",kernelFunc:gle},qS="return (a - b) * (a - b);",yle=fn({opSnippet:qS,packedOpSnippet:qS}),xle={kernelName:Vo,backendName:"webgl",kernelFunc:yle};function vle(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t;if(r.dtype!=="string")throw new Error("Input must be of datatype string");let s=n.readSync(r.dataId),i=T.fromUint8ToStringArray(s),o=WQ(i,"string",a);return n.makeTensorInfo(r.shape,"string",o)}var wle={kernelName:ed,backendName:"webgl",kernelFunc:vle};function kle({inputs:e,attrs:t,backend:n}){let{x:a}=e,r=Ma+ `
2021-09-11 17:11:38 +02:00
return x > 0.0 ? 1.0 : float ( $ { t . alpha } ) ;
2024-01-17 22:56:53 +01:00
` ,s=new ir(a.shape,r);return n.runWebGLProgram(s,[a],a.dtype)}var Ile={kernelName:Ts,backendName:"webgl",kernelFunc:kle},Sle=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let a=n.length,r=ht(n.length),s=ht(n.length),i="";if(a===1)i="coords * strides + begin";else{let o=0;i=n.map((l,u)=>(o++,n.length===1? ` coords * strides [ $ { u } ] + begin [ $ { u } ] ` : ` coords [ $ { o - 1 } ] * strides [ $ { u } ] + begin [ $ { u } ] ` )).join(",")}this.userCode= `
$ { r } begin = $ { r } ( $ { e } ) ;
$ { r } strides = $ { r } ( $ { t } ) ;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { s } coords = getOutputCoords ( ) ;
setOutput ( getX ( $ { i } ) ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function Nle(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{begin:s,end:i,strides:o,beginMask:l,endMask:u,ellipsisMask:p,newAxisMask:d,shrinkAxisMask:c}=a,{finalShapeSparse:h,finalShape:m,isIdentity:f,sliceDim0:g,isSimpleSlice:b,begin:y,end:x,strides:v}=Kt.sliceInfo(r.shape,s,i,o,l,u,p,d,c),I;if(f)I=ce({inputs:{x:r},backend:n,attrs:{shape:m}});else if(g||b){w.assert(r.shape.length>=1,()=> ` Input must have rank at least 1 , got : $ { r . shape . length } ` );let C=Kt.computeOutShape(y,x,v),_=Ep({inputs:{x:r},backend:n,attrs:{begin:y,size:C}});I=ce({inputs:{x:_},backend:n,attrs:{shape:m}}),n.disposeIntermediateTensorInfo(_)}else if(n.shouldExecuteOnCPU([r])){let C=n.readSync(r.dataId),_=Pe(r.shape,r.dtype,C),F=BQ(h,_,v,y);I=n.makeTensorInfo(m,r.dtype,F.values)}else{let C=new Sle(y,v,h);I=n.runWebGLProgram(C,[r],r.dtype)}let N=ce({inputs:{x:I},backend:n,attrs:{shape:m}});return n.disposeIntermediateTensorInfo(I),N}var Tle={kernelName:sp,backendName:"webgl",kernelFunc:Nle};function Cle(e){let{inputs:t,backend:n,attrs:a}=e,{separator:r,nGramWidths:s,leftPad:i,rightPad:o,padWidth:l,preserveShortSequences:u}=a,{data:p,dataSplits:d}=t,c=n.readSync(p.dataId),h=n.readSync(d.dataId),[m,f]=VQ(c,h,r,s,i,o,l,u);return[n.makeTensorInfo([m.length],"string",m),n.makeTensorInfo(d.shape,"int32",f)]}var Ele={kernelName:td,backendName:"webgl",kernelFunc:Cle};function _le(e){let{inputs:t,backend:n,attrs:a}=e,{skipEmpty:r}=a,{input:s,delimiter:i}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");if(s.shape.length!==1)throw new Error( ` Input must be a vector , got shape : $ { s . shape } ` );if(i.shape.length!==0)throw new Error( ` Delimiter must be a scalar , got shape : $ { i . shape } ` );let o=n.readSync(s.dataId),l=n.readSync(i.dataId)[0],[u,p,d]=UQ(o,l,r),c=p.length;return[n.makeTensorInfo([c,2],"int32",u),n.makeTensorInfo([c],"string",p),n.makeTensorInfo([2],"int32",new Int32Array(d))]}var Ale={kernelName:nd,backendName:"webgl",kernelFunc:_le};function Fle(e){let{inputs:t,backend:n,attrs:a}=e,{numBuckets:r}=a,{input:s}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");if(r<=0)throw new Error("Number of buckets must be at least 1");let i=n.readSync(s.dataId),o=GQ(i,r);return n.makeTensorInfo(s.shape,"int32",o)}var $ le={kernelName:ad,backendName:"webgl",kernelFunc:Fle},Dle="return tan(x);",Rle=Ze({opSnippet:Dle}),Mle={kernelName:Go,backendName:"webgl",kernelFunc:Rle},Ole= `
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:56:53 +01:00
` ,Ple=Ze({opSnippet:Ole}),Lle={kernelName:Ho,backendName:"webgl",kernelFunc:Ple};function zle(e){let{inputs:t,backend:n,attrs:a}=e,{tensor:r,indices:s,updates:i}=t,{}=a,{sliceRank:o,numUpdates:l,sliceSize:u,strides:p,outputSize:d}=T.calculateShapes(i,s,r.shape),c=[d/u,u];if(d===0)return n.makeTensorInfo(r.shape,s.dtype);let h=ce({inputs:{x:s},backend:n,attrs:{shape:[l,o]}}),m=ce({inputs:{x:i},backend:n,attrs:{shape:[l,u]}}),f=ce({inputs:{x:r},backend:n,attrs:{shape:c}}),g=new vk(l,o,h.shape.length,m.shape.length,p,c,!1,!0),b=n.runWebGLProgram(g,[m,h,f],f.dtype),y=ce({inputs:{x:b},backend:n,attrs:{shape:r.shape}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(b),y}var Wle={kernelName:Zu,backendName:"webgl",kernelFunc:zle},Ble=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[s]*t[s];this.outputShape=n,this.rank=n.length;let a=ht(this.rank),r=Vle(e);this.userCode= `
2021-09-11 17:11:38 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { a } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { r } ) ) ;
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
` }};function Vle(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"],a=[];for(let r=0;r<e.length;r++)a.push( ` imod ( $ { n [ r ] } , $ { e [ r ] } ) ` );return a.join()}function CF(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{reps:s}=a;if(r.dtype==="string"||r.shape.length>5){let o=n.readSync(r.dataId),l=r.dtype==="string"?o.map(d=>w.decodeString(d)):o,u=Pe(r.shape,r.dtype,l),p=qQ(u,s);return n.makeTensorInfo(p.shape,p.dtype,p.values)}let i=new Ble(r.shape,s);return n.runWebGLProgram(i,[r],r.dtype)}var Ule={kernelName:Ns,backendName:"webgl",kernelFunc:CF},Gle=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:56:53 +01:00
` }},Hle=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:56:53 +01:00
` }};function Ks(e,t){t!==null&&e.disposeIntermediateTensorInfo(t)}function jS(e){let t=1;for(;t<e;)t*=2;return t}function qle(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{k:s,sorted:i}=a,o=G().getNumber("TOPK_LAST_DIM_CPU_HANDOFF_SIZE_THRESHOLD"),l=G().getNumber("TOPK_K_CPU_HANDOFF_THRESHOLD"),u=r.shape,p=u[u.length-1];if(n.shouldExecuteOnCPU([r])||p<o||s>l){let F=n.readSync(r.dataId),[D, $ ]=jQ(F,u,r.dtype,s,i);return[n.makeTensorInfo(D.shape,D.dtype,D.values),n.makeTensorInfo( $ .shape, $ .dtype, $ .values)]}if(s===0)return u[u.length-1]=0,[n.makeTensorInfo(u,r.dtype,[]),n.makeTensorInfo(u,"int32",[])];if(p===1)return[r,Ld({attrs:{shape:u,dtype:"int32",value:0},backend:n})];let d=n.texData.get(r.dataId),c=d!==null&&d.isPacked,h=c?n.unpackTensor(r):r,m=w.sizeFromShape(u)/p,f=ce({inputs:{x:h},attrs:{shape:[m,p]},backend:n});c&&Ks(n,h);let g=jS(s),b=jS(p),y=null,x=()=>y===null?[f,f]:[f,y],v=(F,D, $ )=>{let S=x(),M=new Gle( $ ),B=[[p],[y===null?1:0],[Number.NEGATIVE_INFINITY],[F],[D]],U=y;y=n.runWebGLProgram(M,S,"int32",B),Ks(n,U)};for(let F=1;F<g;F*=2){let D=F*2;for(let $ =F; $ >=1; $ /=2)v(D, $ ,[m,b])}for(let F=b;F>g;F/=2){let D=x(), $ =new Hle([m,F/2]),S=[[p],[y===null?1:0],[g]],M=y;y=n.runWebGLProgram( $ ,D,"int32",S),Ks(n,M);let B=g/2,U=B*2;for(let H=B;H>=1;H/=2)v(U,H,y.shape)}let I=y;y=Ep({inputs:{x:y},backend:n,attrs:{begin:0,size:[m,s]}}),Ks(n,I);let N=xF({inputs:{x:f,indices:y},backend:n,attrs:{axis:1,batchDims:1}});Ks(n,f);let C=u.slice(0,-1);C.push(s),I=y,y=ce({inputs:{x:y},attrs:{shape:C},backend:n}),Ks(n,I);let _=N;return N=ce({inputs:{x:N},attrs:{shape:C},backend:n}),Ks(n,_),[N,y]}var jle={kernelName:ip,backendName:"webgl",kernelFunc:qle},Kle=class{constructor(e,t,n,a,r,s){this.variableNames=["Image","Transforms"],this.outputShape=s;let i=n==="nearest"?1:2,o;switch(a){case"constant":o=1;break;case"reflect":o=2;break;case"wrap":o=3;break;case"nearest":o=4;break;default:o=1;break}this.userCode= `
2021-09-11 17:11:38 +02:00
float mapCoord ( float outCoord , float len ) {
float inCoord = outCoord ;
2024-01-17 22:56:53 +01:00
if ( $ { o } == 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:56:53 +01:00
} else if ( $ { o } == 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:56:53 +01:00
} else if ( $ { o } == 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:56:53 +01:00
outputValue = float ( $ { r } ) ;
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:56:53 +01:00
outputValue = float ( $ { r } ) ;
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:56:53 +01:00
if ( $ { i } == 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:56:53 +01:00
` }};function Xle(e){let{inputs:t,backend:n,attrs:a}=e,{image:r,transforms:s}=t,{interpolation:i,fillMode:o,fillValue:l,outputShape:u}=a,[p,d,c,h]=r.shape,[m,f]=u!=null?u:[d,c],g=[p,m,f,h],b=new Kle(d,c,i,o,l,g);return n.runWebGLProgram(b,[r,s],"float32")}var Yle={kernelName:op,backendName:"webgl",kernelFunc:Xle};function Zle(e){let{inputs:t,attrs:n,backend:a}=e,{axis:r}=n,{x:s}=t;wp(s,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let i=a.readSync(s.dataId),{outputValues:o,outputShape:l,indices:u}=KQ(i,r,s.shape,s.dtype);return[a.makeTensorInfo(l,s.dtype,o),a.makeTensorInfo([u.length],"int32",u)]}var Jle={kernelName:rd,backendName:"webgl",kernelFunc:Zle};function Qle(e){let{inputs:t,backend:n,attrs:a}=e,{value:r}=t,{axis:s}=a;s<0&&(s+=r.shape.length);let i=r,o=i.shape.length,l=r.shape[s],u=new Array(o-1),p=0;for(let f=0;f<o;f++)f!==s&&(u[p++]=i.shape[f]);let d=[],c=new Array(o).fill(0),h=i.shape.slice();h[s]=1;let m=new Array(l);for(let f=0;f<m.length;f++){c[s]=f;let g=Ep({inputs:{x:i},backend:n,attrs:{begin:c,size:h}}),b=ce({inputs:{x:g},backend:n,attrs:{shape:u}});m[f]=b,d.push(g)}return d.forEach(f=>n.disposeIntermediateTensorInfo(f)),m}var eue={kernelName:lp,backendName:"webgl",kernelFunc:Qle},tue=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,a=e.batchSize,r=e.inSize,s=e.numSegments,i=s*Math.ceil(r/n);this.outputShape=[a,i];let o="0.0",l="sumValue",u=Math.floor(n/4)*4,p=n%4,d= `
2021-09-11 17:11:38 +02:00
sumValue += dot ( values , segFilter ) ;
2024-01-17 22:56:53 +01:00
` ,c="";r%n>0&&(c= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
2021-09-11 17:11:38 +02:00
return initializationValue ;
}
2024-01-17 22:56:53 +01:00
` );let h="";r%n>0&&(h= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
2021-09-11 17:11:38 +02:00
return - 1.0 ;
}
` ),this.userCode= `
2024-01-17 22:56:53 +01:00
const float initializationValue = $ { o } ;
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:56:53 +01:00
$ { c }
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:56:53 +01:00
$ { s } ) ) * float ( $ { n } ) ) ;
int currentSeg = int ( mod ( float ( outIdx ) , float ( $ { s } ) ) ) ;
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:56:53 +01:00
for ( int i = 0 ; i < $ { u } ; 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:56:53 +01:00
$ { d }
2020-12-15 14:15:43 +01:00
}
2020-10-29 05:16:50 +01:00
2024-01-17 22:56:53 +01:00
int inIdx = inOffset + $ { u } ;
if ( $ { p === 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:56:53 +01:00
$ { d }
} else if ( $ { p === 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:56:53 +01:00
$ { d }
} else if ( $ { p === 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:56:53 +01:00
$ { d }
2021-09-11 17:11:38 +02:00
}
2024-01-17 22:56:53 +01:00
setOutput ( $ { l } ) ;
2021-12-01 21:37:52 +01:00
}
2024-01-17 22:56:53 +01:00
` }};function nue(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,segmentIds:s}=t,{numSegments:i}=a,o=r.shape.length,l=[],u=0,p=T.getAxesPermutation([u],o),d=r;p!=null&&(d=In({inputs:{x:r},backend:n,attrs:{perm:p}}),l.push(d),u=T.getInnerMostAxes(1,o)[0]);let c=T.segment_util.computeOutShape(d.shape,u,i),h=w.sizeFromShape([d.shape[u]]),m=ce({inputs:{x:d},backend:n,attrs:{shape:[-1,h]}});l.push(m);let f=Xm(r.dtype),g=(v,I,N,C,_)=>{let F=v.shape[0],D=v.shape[1], $ =T.segment_util.segOpComputeOptimalWindowSize(D,_),S={windowSize: $ ,inSize:D,batchSize:F,numSegments:_},M=new tue(S,I),B=n.compileAndRun(M,[v,N],C);if(l.push(B),B.shape[1]===_)return B;let U=TF({backend:n,attrs:{start:0,stop:_,step:1,dtype:"float32"}}),H=CF({inputs:{x:U},backend:n,attrs:{reps:[D/ $ ]}});return l.push(U),l.push(H),g(B,I,H,C,_)},b=g(m,"unsortedSegmentSum",s,f,i),y=ce({inputs:{x:b},backend:n,attrs:{shape:c}}),x=y;if(p!=null){l.push(y);let v=T.getUndoAxesPermutation(p);x=In({inputs:{x},backend:n,attrs:{perm:v}})}return l.forEach(v=>n.disposeIntermediateTensorInfo(v)),x}var aue={kernelName:sd,backendName:"webgl",kernelFunc:nue},rue=[Bee,Uee,qee,Xee,Zee,ete,nte,rte,lte,pte,hte,gte,xte,Ite,Tte,Ete,Ate,Rte,Ote,Lte,Vte,Xte,Zte,tne,ane,une,cne,fne,See,yne,Ine,Cne,Dne,One,Lne,Wne,Vne,qne,Xne,Jne,eae,nae,rae,oae,uae,hae,fae,yae,wae,Iae,Cae,Fae,Mae,Lae,Bae,Vae,Gae,qae,Kae,Yae,Jae,nre,sre,lre,pre,hre,gre,vre,Sre,Iee,Tre,wne,_re, $ re,Mre,Tee,zre,Ure,Hre,Xre,Jre,nse,sse,use,hse,gse,yse,kse,Sse,Tse,Ase, $ se,Rse,Ose,Lse,Vse,qse,Yse,rie,_ee,lie,cie,mie,bie,sne,vie,kie,Sie,Cie,Fie,Eee,Die,Mie,Pie,zie,Wie,ine,eie,Uie,jie,Zie,Fee,toe,roe,loe,coe,foe,boe,voe,Ioe,Toe,_oe, $ oe,Moe,zoe,Voe,qoe,Xoe,jte,nie,Joe,ele,nle,rle,ile,lle,ple,dle,mle,ble,xle,wle,Ile,Tle,Ele,Ale, $ le,tie,Lee,Mle,Lle,Wle,Ule,jle,Yle,zee,Jle,eue,aue,wie];for(let e of rue)id(e);var Qe;(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"})(Qe||(Qe={}));var Oc;(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"})(Oc||(Oc={}));var EF;function sue(e){EF=e.wasm.cwrap(oi,null,["number","array","number","number","array","number","number","number","number","number","number","number","number"])}function iue(e){let{inputs:t,backend:n,attrs:a}=e,{a:r,b:s,bias:i,preluActivationWeights:o}=t;if(r.dtype!=="float32"||s.dtype!=="float32")throw new Error("_FusedMatMul for non non-float32 tensors not yet supported.");let{transposeA:l,transposeB:u,activation:p,leakyreluAlpha:d}=a,c=n.dataIdMap.get(r.dataId).id,h=n.dataIdMap.get(s.dataId).id,m=0;if(i!=null){let _=n.dataIdMap.get(i.dataId);if(_.shape.length!==1)throw new Error( ` _FusedMatMul only supports rank - 1 bias but got rank $ { _ . shape . length } . ` );m=_.id}let f=o==null?0:n.dataIdMap.get(o.dataId).id,g=Oc[p];if(g==null)throw new Error( ` $ { p } activation not yet supported for FusedConv2D in the wasm backend . ` );let b=l?r.shape[2]:r.shape[1],y=u?s.shape[1]:s.shape[2],x=cp.assertAndGetBroadcastShape(r.shape.slice(0,-2),s.shape.slice(0,-2)),v=n.makeOutput([...x,b,y],r.dtype),I=n.dataIdMap.get(v.dataId).id,N=new Uint8Array(new Int32Array(r.shape).buffer),C=new Uint8Array(new Int32Array(s.shape).buffer);return EF(c,N,r.shape.length,h,C,s.shape.length,l,u,g,m,f,d||0,I),v}var oue={kernelName:oi,backendName:"wasm",setupFunc:sue,kernelFunc:iue};function Xe(e,t){let n;function a(s){n=s.wasm.cwrap(e,null,["number","number","number"])}function r(s){let{backend:i,inputs:{x:o}}=s,l=i.dataIdMap.get(o.dataId).id,u=i.makeOutput(o.shape,t||o.dtype),p=i.dataIdMap.get(u.dataId).id;return w.sizeFromShape(u.shape)===0||n(l,Qe[o.dtype],p),u}return{kernelName:e,backendName:"wasm",setupFunc:a,kernelFunc:r}}var lue=Xe(uu),uue=Xe(Ci),pue=Xe(Ei);function Ut(e,t,n){let a;function r(i){a=i.wasm.cwrap(e,null,["number","array","number","number","array","number","number","number"])}function s(i){let{backend:o,inputs:l}=i,{a:u,b:p}=l,d=o.dataIdMap.get(u.dataId).id,c=o.dataIdMap.get(p.dataId).id,h=n!=null?n:u.dtype,m=T.assertAndGetBr
$ { a . shape } ` );if(r.shape.length!==1)throw new Error( ` Input shape should be a vector but received shape
$ { r . shape } ` );if(s.shape.length!==1)throw new Error( ` Target shape should be a vector but received shape $ { s . shape } ` );let i=t.dataIdMap.get(a.dataId).id,o=t.dataIdMap.get(r.dataId).id,l=t.dataIdMap.get(s.dataId).id,u=a.shape[0],p=w.sizeFromShape(s.shape),d=t.makeOutput([u,p],a.dtype),c=t.dataIdMap.get(d.dataId).id,h=t.makeOutput([p],s.dtype),m=t.dataIdMap.get(h.dataId).id,f=t.makeOutput([3],"int32"),g=t.dataIdMap.get(f.dataId).id;q $ (i,o,l,u,c,m,g);let b=t.readSync(f.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 x=Array.from(t.readSync(r.dataId)),v=Array.from(t.readSync(h.dataId));y=T.getSparseReshapeInputOutputMultipleErrorMessage(x,v);break}case 4:{let x=Array.from(t.readSync(r.dataId)),v=Array.from(t.readSync(h.dataId));y=T.getSparseReshapeInputOutputMismatchErrorMessage(x,v);break}default:y=""}if(t.disposeData(f.dataId),y)throw t.disposeData(d.dataId),t.disposeData(h.dataId),new Error(y);return[d,h]}var _me={kernelName:ap,backendName:"wasm",setupFunc:Cme,kernelFunc:Eme},j $ ;function K $ (e){j $ =e.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function X $ (e,t){let{backend:n,inputs:a}=e,{data:r,indices:s,segmentIds:i}=a,o=s.shape[0],l=n.readSync(i.dataId,o-1,o)[0],u=o>0?l+1:0;if(u<0)throw new Error(T.getSparseSegmentReductionNegativeSegmentIdsErrorMessage());let p=r.shape.slice();p[0]=u;let d=n.dataIdMap.get(r.dataId).id,c=n.dataIdMap.get(s.dataId).id,h=n.dataIdMap.get(i.dataId).id,m=n.makeOutput(p,r.dtype),f=n.dataIdMap.get(m.dataId).id,g=n.makeOutput([4],"int32"),b=n.dataIdMap.get(g.dataId).id;j $ (d,Qe[r.dtype],r.shape[0],c,h,f,b,t,0);let y=n.readSync(g.dataId),x;switch(y[0]){case 0:{x=T.getSparseSegmentReductionNegativeSegmentIdsErrorMessage();break}case 1:{x=T.getSparseSegmentReductionNonIncreasingSegmentIdsErrorMessage();break}case 2:x=T.getSparseSegmentReductionSegmentIdOutOfRangeErrorMessage(y[1],y[2]);break;case 3:x=T.getSparseSegmentReductionIndicesOutOfRangeErrorMessage(y[1],y[2],y[3]);break;default:x=""}if(n.disposeData(g.dataId),x)throw n.disposeData(m.dataId),new Error(x);return m}function Ame(e){return X $ (e,!0)}var Fme={kernelName:Zc,backendName:"wasm",setupFunc:K $ ,kernelFunc:Ame};function $ me(e){return X $ (e,!1)}var Dme={kernelName:Jc,backendName:"wasm",setupFunc:K $ ,kernelFunc: $ me},Y $ ;function Rme(e){Y $ =e.wasm.cwrap(rp,null,["number","number","number","number","number","number","number","number","array","number","number"])}function Mme(e){let{backend:t,inputs:n,attrs:a}=e,{sparseIndices:r,sparseValues:s,defaultValue:i}=n,{outputShape:o}=a,l=t.makeOutput(o,i.dtype);if(w.sizeFromShape(o)===0)return l;let{sliceRank:u,numUpdates:p,sliceSize:d,strides:c,outputSize:h}=T.calculateShapes(s,r,o),m=t.dataIdMap.get(r.dataId).id,f=t.dataIdMap.get(s.dataId).id,g=t.dataIdMap.get(i.dataId).id,b=new Uint8Array(new Int32Array(c).buffer),y=t.dataIdMap.get(l.dataId).id;return Y $ (m,f,s.shape.length,g,Qe[i.dtype],u,p,d,b,h,y),l}var Ome={kernelName:rp,backendName:"wasm",setupFunc:Rme,kernelFunc:Mme};function Pme(e){let{inputs:t,attrs:n,backend:a}=e,{x:r}=t,{numOrSizeSplits:s,axis:i}=n,o=w.parseAxisParam(i,r.shape)[0],l=T.prepareSplitSize(r,s,o),u=new Array(r.shape.length).fill(0),p=r.shape.slice();return l.map(d=>{let c=[...p];c[o]=d;let h=Ni({inputs:{x:r},attrs:{begin:u,size:c},backend:a});return u[o]+=d,h})}var Lme={kernelName:np,backendName:"wasm",kernelFunc:Pme},zme=Xe(zo),Wme=Xe(Qc),Bme=!0,Vme=Ut(Vo,Bme),Z $ ;function Ume(e){Z $ =e.wasm.cwrap(Ts,null,["number","number","number","number"])}function Gme(e){let{backend:t,inputs:n,attrs:a}=e,{alpha:r}=a,{x:s}=n,i=t.dataIdMap.get(s.dataId).id,o=t.makeOutput(s.shape,s.dtype),l=t.dataIdMap.get(o.dataId).id;return Z $ (i,r,Qe[s.dtype],l),o}var Hme={kernelName:Ts,backendName:"wasm",setupFunc:Ume,kernelFunc:Gme},J $ ;function qme(e){J $ =e.wasm.cwrap(sp,null,["number","array","number","array","ar