2021-09-08 19:51:28 +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-09-10 17:30:23 +02:00
var zq = Object . create ; var f0 = Object . defineProperty ; var Bq = Object . getOwnPropertyDescriptor ; var Vq = Object . getOwnPropertyNames ; var Gq = Object . getPrototypeOf , Wq = Object . prototype . hasOwnProperty ; var br = ( r , t ) => ( ) => ( t || r ( ( t = { exports : { } } ) . exports , t ) , t . exports ) , Kt = ( r , t ) => { for ( var e in t ) f0 ( r , e , { get : t [ e ] , enumerable : ! 0 } ) } , Uq = ( r , t , e , n ) => { if ( t && typeof t == "object" || typeof t == "function" ) for ( let o of Vq ( t ) ) ! Wq . call ( r , o ) && o !== e && f0 ( r , o , { get : ( ) => t [ o ] , enumerable : ! ( n = Bq ( t , o ) ) || n . enumerable } ) ; return r } ; var Gl = ( r , t , e ) => ( e = r != null ? zq ( Gq ( r ) ) : { } , Uq ( t || ! r || ! r . _ _esModule ? f0 ( e , "default" , { value : r , enumerable : ! 0 } ) : e , r ) ) ; var W _ = br ( ( gft , G _ ) => { G _ . exports = He ; var yo = null ; try { yo = new WebAssembly . Instance ( new WebAssembly . Module ( new Uint8Array ( [ 0 , 97 , 115 , 109 , 1 , 0 , 0 , 0 , 1 , 13 , 2 , 96 , 0 , 1 , 127 , 96 , 4 , 127 , 127 , 127 , 127 , 1 , 127 , 3 , 7 , 6 , 0 , 1 , 1 , 1 , 1 , 1 , 6 , 6 , 1 , 127 , 1 , 65 , 0 , 11 , 7 , 50 , 6 , 3 , 109 , 117 , 108 , 0 , 1 , 5 , 100 , 105 , 118 , 95 , 115 , 0 , 2 , 5 , 100 , 105 , 118 , 95 , 117 , 0 , 3 , 5 , 114 , 101 , 109 , 95 , 115 , 0 , 4 , 5 , 114 , 101 , 109 , 95 , 117 , 0 , 5 , 8 , 103 , 101 , 116 , 95 , 104 , 105 , 103 , 104 , 0 , 0 , 10 , 191 , 1 , 6 , 4 , 0 , 35 , 0 , 11 , 36 , 1 , 1 , 126 , 32 , 0 , 173 , 32 , 1 , 173 , 66 , 32 , 134 , 132 , 32 , 2 , 173 , 32 , 3 , 173 , 66 , 32 , 134 , 132 , 126 , 34 , 4 , 66 , 32 , 135 , 167 , 36 , 0 , 32 , 4 , 167 , 11 , 36 , 1 , 1 , 126 , 32 , 0 , 173 , 32 , 1 , 173 , 66 , 32 , 134 , 132 , 32 , 2 , 173 , 32 , 3 , 173 , 66 , 32 , 134 , 132 , 127 , 34 , 4 , 66 , 32 , 135 , 167 , 36 , 0 , 32 , 4 , 167 , 11 , 36 , 1 , 1 , 126 , 32 , 0 , 173 , 32 , 1 , 173 , 66 , 32 , 134 , 132 , 32 , 2 , 173 , 32 , 3 , 173 , 66 , 32 , 134 , 132 , 128 , 34 , 4 , 66 , 32 , 135 , 167 , 36 , 0 , 32 , 4 , 167 , 11 , 36 , 1 , 1 , 126 , 32 , 0 , 173 , 32 , 1 , 173 , 66 , 32 , 134 , 132 , 32 , 2 , 173 , 32 , 3 , 173 , 66 , 32 , 134 , 132 , 129 , 34 , 4 , 66 , 32 , 135 , 167 , 36 , 0 , 32 , 4 , 167 , 11 , 36 , 1 , 1 , 126 , 32 , 0 , 173 , 32 , 1 , 173 , 66 , 32 , 134 , 132 , 32 , 2 , 173 , 32 , 3 , 173 , 66 , 32 , 134 , 132 , 130 , 34 , 4 , 66 , 32 , 135 , 167 , 36 , 0 , 32 , 4 , 167 , 11 ] ) ) , { } ) . exports } catch ( r ) { } function He ( r , t , e ) { this . low = r | 0 , this . high = t | 0 , this . unsigned = ! ! e } He . prototype . _ _isLong _ _ ; Object . defineProperty ( He . prototype , "__isLong__" , { value : ! 0 } ) ; function zn ( r ) { return ( r && r . _ _isLong _ _ ) === ! 0 } He . isLong = zn ; var R _ = { } , F _ = { } ; function oc ( r , t ) { var e , n , o ; return t ? ( r >>>= 0 , ( o = 0 <= r && r < 256 ) && ( n = F _ [ r ] , n ) ? n : ( e = qe ( r , ( r | 0 ) < 0 ? - 1 : 0 , ! 0 ) , o && ( F _ [ r ] = e ) , e ) ) : ( r |= 0 , ( o = - 128 <= r && r < 128 ) && ( n = R _ [ r ] , n ) ? n : ( e = qe ( r , r < 0 ? - 1 : 0 , ! 1 ) , o && ( R _ [ r ] = e ) , e ) ) } He . fromInt = oc ; function bo ( r , t ) { if ( isNaN ( r ) ) return t ? nc : wo ; if ( t ) { if ( r < 0 ) return nc ; if ( r >= L _ ) return V _ } else { if ( r <= - M _ ) return Ln ; if ( r + 1 >= M _ ) return B _ } return r < 0 ? bo ( - r , t ) . neg ( ) : qe ( r % Hp | 0 , r / Hp | 0 , t ) } He . fromNumber = bo ; function qe ( r , t , e ) { return new He ( r , t , e ) } He . fromBits = qe ; var Jg = Math . pow ; function k0 ( r , t , e ) { if ( r . length === 0 ) throw Error ( "empty string" ) ; if ( r === "NaN" || r === "Infinity" || r === "+Infinity" || r === "-Infinity" ) return wo ; if ( typeof t == "number" ? ( e = t , t = ! 1 ) : t = ! ! t , e = e || 10 , e < 2 || 36 < e ) throw RangeError ( "radix" ) ; var n ; if ( ( n = r . indexOf ( "-" ) ) > 0 ) throw Error ( "interior hyphen" ) ; if ( n === 0 ) return k0 ( r . substring ( 1 ) , t , e ) . neg ( ) ; for ( var o = bo ( Jg ( e , 8 ) ) , s = wo , i = 0 ; i < r . length ; i += 8 ) { var a = Math . min ( 8 , r . length - i ) , u = parseInt ( r . substring ( i , i + a ) , e ) ; if ( a < 8 ) { var l = bo ( Jg ( e , a ) ) ; s = s . mul ( l ) . add ( bo ( u ) ) } else s = s . mul ( o ) , s = s . add ( bo ( u ) ) } return s . unsigned = t , s } He . fromString = k0 ; function oi ( r , t ) { return typeof r == "number" ? bo ( r , t ) : typeof r == "string" ? k0 ( r , t ) : qe ( r . low , r . high , typeof t == "boolean" ? t : r . unsigned ) } He . fromValue = oi ; var O _ = 65536 , fK = 1 << 24 , Hp = O _ * O _ , L _ = Hp * Hp , M _ = L _ / 2 , P _ = oc ( fK ) , wo = oc ( 0 ) ; He . ZERO = wo ; var nc = oc ( 0 , ! 0 ) ; He . UZERO = nc ; var Up = oc ( 1 ) ; He . ONE = Up ; var z _ = oc ( 1 , ! 0 ) ; He . UONE = z _ ; var N0 = oc ( - 1 ) ; He . NEG _ONE = N0 ; var B _ = qe ( - 1 , 2147483647 , ! 1 ) ; He . MAX _VALUE = B _ ; var V _ = qe ( - 1 , - 1 , ! 0 ) ; He . MAX _UNSIGNED _VALUE = V _ ; var Ln = qe ( 0 , - 2147483648 , ! 1 ) ; He . MIN _VALUE = Ln ; var xt = He . prototype ; xt . toInt = function ( ) { return this . unsigned ? this . low >>> 0 : this . low } ; xt . toNumber = function ( ) { return this . unsigned ? ( this . high >>> 0 ) * Hp + ( this . low >>> 0 ) : this . high * Hp + ( this . low >>> 0 ) } ; xt . toString = function ( t ) { if ( t = t || 10 , t < 2 || 36 < t ) throw RangeError ( "radix" ) ; if ( this . isZero ( ) ) return "0" ; if ( this . isNegative ( ) ) if ( this . eq ( Ln ) ) { var e = bo ( t ) , n = this . div ( e ) , o = n . mul ( e ) . sub ( this ) ; return n . toString ( t ) + o . toInt ( ) . toString ( t ) } else return "-" + this . neg ( ) . toString ( t ) ; for ( var s = bo ( Jg ( t , 6 ) , this . unsigned ) , i = this , a = "" ; ; ) { var u = i . div ( s ) , l = i . sub ( u . mul ( s ) ) . toInt ( ) >>> 0 , c = l . toString ( t ) ; if ( i = u , i . isZero ( ) ) return c + a ; for ( ; c . length < 6 ; ) c = "0" + c ; a = "" + c + a } } ; xt . getHighBits = function ( ) { return this . high } ; xt . getHighBitsUnsigned = function ( ) { return this . high >>> 0 } ; xt . getLowBits = function ( ) { return this . low } ; xt . getLowBi
2024-01-17 22:56:53 +01:00
` ),H=P=>G.writeSync(2,P+ `
2024-09-10 17:30:23 +02:00
` ));var j=l.print||q,Y=l.printErr||H;Object.assign(l,f),f=null,l.arguments&&(d=l.arguments),l.thisProgram&&(h=l.thisProgram),l.quit&&(g=l.quit);var Z=4,et=Atomics.load,nt=Atomics.store,st=Atomics.compareExchange,lt;l.wasmBinary&&(lt=l.wasmBinary);var ot=l.noExitRuntime||!0;typeof WebAssembly!="object"&&Xu("no native wasm support detected");var it,ft,gt=!1,Ct;function Rt(P,U){P||Xu(U)}var Dt=typeof TextDecoder!="undefined"?new TextDecoder("utf8"):void 0;function Ht(P,U,dt){U>>>=0;for(var Pt=U+dt,Jt=U;P[Jt]&&!(Jt>=Pt);)++Jt;if(Jt-U>16&&P.buffer&&Dt)return Dt.decode(P.buffer instanceof SharedArrayBuffer?P.slice(U,Jt):P.subarray(U,Jt));for(var Zt="";U<Jt;){var bt=P[U++];if(!(bt&128)){Zt+=String.fromCharCode(bt);continue}var $ t=P[U++]&63;if((bt&224)==192){Zt+=String.fromCharCode((bt&31)<<6| $ t);continue}var or=P[U++]&63;if((bt&240)==224?bt=(bt&15)<<12| $ t<<6|or:bt=(bt&7)<<18| $ t<<12|or<<6|P[U++]&63,bt<65536)Zt+=String.fromCharCode(bt);else{var eo=bt-65536;Zt+=String.fromCharCode(55296|eo>>10,56320|eo&1023)}}return Zt}function qt(P,U){return P>>>=0,P?Ht(n(),P,U):""}function pe(P,U,dt,Pt){if(dt>>>=0,!(Pt>0))return 0;for(var Jt=dt,Zt=dt+Pt-1,bt=0;bt<P.length;++bt){var $ t=P.charCodeAt(bt);if( $ t>=55296&& $ t<=57343){var or=P.charCodeAt(++bt); $ t=65536+(( $ t&1023)<<10)|or&1023}if( $ t<=127){if(dt>=Zt)break;U[dt++>>>0]= $ t}else if( $ t<=2047){if(dt+1>=Zt)break;U[dt++>>>0]=192| $ t>>6,U[dt++>>>0]=128| $ t&63}else if( $ t<=65535){if(dt+2>=Zt)break;U[dt++>>>0]=224| $ t>>12,U[dt++>>>0]=128| $ t>>6&63,U[dt++>>>0]=128| $ t&63}else{if(dt+3>=Zt)break;U[dt++>>>0]=240| $ t>>18,U[dt++>>>0]=128| $ t>>12&63,U[dt++>>>0]=128| $ t>>6&63,U[dt++>>>0]=128| $ t&63}}return U[dt>>>0]=0,dt-Jt}function ye(P,U,dt){return pe(P,n(),U,dt)}var re,be,de,Ee,Ae,On,lr,Zn,Br;I&&(re=l.buffer);function Xe(P){re=P,l.HEAP8=be=new Int8Array(P),l.HEAP16=Ee=new Int16Array(P),l.HEAP32=On=new Int32Array(P),l.HEAPU8=de=new Uint8Array(P),l.HEAPU16=Ae=new Uint16Array(P),l.HEAPU32=lr=new Uint32Array(P),l.HEAPF32=Zn=new Float32Array(P),l.HEAPF64=Br=new Float64Array(P)}var Vr=l.INITIAL_MEMORY||16777216;if(I)it=l.wasmMemory,re=l.buffer;else if(l.wasmMemory)it=l.wasmMemory;else if(it=new WebAssembly.Memory({initial:Vr/65536,maximum:65536,shared:!0}),!(it.buffer instanceof SharedArrayBuffer))throw Y("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"),w&&Y("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and/or recent version)"),Error("bad memory");it&&(re=it.buffer),Vr=re.byteLength,Xe(re);var Gr,Jn=[],Qn=[],Qr=[],Ca=!1;function zo(){return ot}function Ti(){if(l.preRun)for(typeof l.preRun=="function"&&(l.preRun=[l.preRun]);l.preRun.length;)Vd(l.preRun.shift());Ud(Jn)}function Er(){Ca=!0,!I&&Ud(Qn)}function va(){if(!I){if(l.postRun)for(typeof l.postRun=="function"&&(l.postRun=[l.postRun]);l.postRun.length;)c_(l.postRun.shift());Ud(Qr)}}function Vd(P){Jn.unshift(P)}function Gd(P){Qn.unshift(P)}function c_(P){Qr.unshift(P)}var zl=0,bp=null,Sa=null;function MC(P){zl++,l.monitorRunDependencies&&l.monitorRunDependencies(zl)}function Ig(P){if(zl--,l.monitorRunDependencies&&l.monitorRunDependencies(zl),zl==0&&(bp!==null&&(clearInterval(bp),bp=null),Sa)){var U=Sa;Sa=null,U()}}function Xu(P){l.onAbort&&l.onAbort(P),P="Aborted("+P+")",Y(P),gt=!0,Ct=1,P+=". Build with -sASSERTIONS for more info.";var U=new WebAssembly.RuntimeError(P);throw p(U),U}var PC="data:application/octet-stream;base64,";function Cg(P){return P.startsWith(PC)}function wp(P){return P.startsWith("file://")}var tn;tn="tfjs-backend-wasm-threaded-simd.wasm",Cg(tn)||(tn=E(tn));function vg(P){try{if(P==tn&<)return new Uint8Array(lt);if(F)return F(P);throw"both async and sync fetching of the wasm failed"}catch(U){Xu(U)}}function LC(){if(!lt&&(x||b)){if(typeof fetch=="function"&&!wp(tn))return fetch(tn,{credentials:"same-origin"}).then(function(P){if(!P.ok)throw"failed to load wasm binary file at '"+tn+"'";return P.arrayBuffer()}).catch(function(){return vg(t
");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}};`});var QH=br((FC,s_)=>{var o_=(()=>{var r=typeof document!=" undefined "&&document.currentScript?document.currentScript.src:void 0;return typeof __filename!=" undefined "&&(r=r||__filename),function(t){t=t||{};var e=typeof t!=" undefined "?t:{},n,o;e.ready=new Promise(function(X,ct){n=X,o=ct});var s;typeof process!=" undefined "&&process.listeners&&(s={uncaughtException:process.listeners(" uncaughtException "),unhandledRejection:process.listeners(" unhandledRejection ")});var i=Object.assign({},e),a=[],u=" . / this . program ",l=(X,ct)=>{throw ct},c=typeof window==" object ",p=typeof importScripts==" function ",m=typeof process==" object "&&typeof process.versions==" object "&&typeof process.versions.node==" string ",f=" ";function d(X){return e.locateFile?e.locateFile(X,f):f+X}var h,g,x,b;function w(X){if(X instanceof bp)return;A(" exiting due to exception : "+X)}if(m){var I=hw(),N=e_();p?f=N.dirname(f)+" / ":f=__dirname+" / ",h=(X,ct)=>(X=Ti(X)?new URL(X):N.normalize(X),I.readFileSync(X,ct?void 0:" utf8 ")),x=X=>{var ct=h(X,!0);return ct.buffer||(ct=new Uint8Array(ct)),ct},g=(X,ct,Ft)=>{X=Ti(X)?new URL(X):N.normalize(X),I.readFile(X,function(me,Ye){me?Ft(me):ct(Ye.buffer)})},process.argv.length>1&&(u=process.argv[1].replace(/\\/g," / ")),a=process.argv.slice(2),process.on(" uncaughtException ",function(X){if(!(X instanceof bp))throw X}),process.on(" unhandledRejection ",function(X){throw X}),l=(X,ct)=>{if(de())throw process.exitCode=X,ct;w(ct),process.exit(X)},e.inspect=function(){return" [ Emscripten Module object ] "}}else(c||p)&&(p?f=self.location.href:typeof document!=" undefined "&&document.currentScript&&(f=document.currentScript.src),r&&(f=r),f.indexOf(" blob : ")!==0?f=f.substr(0,f.replace(/[?#].*/," ").lastIndexOf(" / ")+1):f=" ",h=X=>{var ct=new XMLHttpRequest;return ct.open(" GET ",X,!1),ct.send(null),ct.responseText},p&&(x=X=>{var ct=new XMLHttpRequest;return ct.open(" GET " , X , ! 1 ) , ct . responseTy
2024-01-17 22:56:53 +01:00
` )),u.join( `
2024-09-10 17:30:23 +02:00
` )}function vK(r,t,e,n){let o=jt(t),s=n[n.length-1],i=new Array(s).fill(0),a=t.length,u=e==="complex64"?sh(r):r;if(a>1)for(let l=0;l<o/s;l++){let c=l*s;for(let p=0;p<s;p++)i[p]=Math.max(i[p],oh(u[c+p],0,e).length)}return i}function oh(r,t,e){let n;return Array.isArray(r)?n= ` $ { parseFloat ( r [ 0 ] . toFixed ( A0 ) ) } + $ { parseFloat ( r [ 1 ] . toFixed ( A0 ) ) } j ` :Vo(r)?n= ` '${r}' ` :e==="bool"?n=Z_(r):n=parseFloat(r.toFixed(A0)).toString(),Qu(n,t)}function Z_(r){return r===0?"false":"true"}function ex(r,t,e,n,o,s=!0){let i=e==="complex64"?2:1,a=t[0],u=t.length;if(u===0){if(e==="complex64"){let h=sh(r);return[oh(h[0],0,e)]}return e==="bool"?[Z_(r[0])]:[r[0].toString()]}if(u===1){if(a>X_){let g=nh*i,x=Array.from(r.slice(0,g)),b=Array.from(r.slice((a-nh)*i,a*i));return e==="complex64"&&(x=sh(x),b=sh(b)),["["+x.map((w,I)=>oh(w,o[I],e)).join(", ")+", ..., "+b.map((w,I)=>oh(w,o[a-nh+I],e)).join(", ")+"]"]}return["["+(e==="complex64"?sh(r):Array.from(r)).map((g,x)=>oh(g,o[x],e)).join(", ")+"]"]}let l=t.slice(1),c=n.slice(1),p=n[0]*i,m=[];if(a>X_){for(let h=0;h<nh;h++){let g=h*p,x=g+p;m.push(...ex(r.slice(g,x),l,e,c,o,!1))}m.push("...");for(let h=a-nh;h<a;h++){let g=h*p,x=g+p;m.push(...ex(r.slice(g,x),l,e,c,o,h===a-1))}}else for(let h=0;h<a;h++){let g=h*p,x=g+p;m.push(...ex(r.slice(g,x),l,e,c,o,h===a-1))}let f=u===2?",":"";m[0]="["+(a>0?m[0]+f:"");for(let h=1;h<m.length-1;h++)m[h]=" "+m[h]+f;let d= ` ,
2024-01-17 22:56:53 +01:00
` ;for(let h=2;h<u;h++)d+= `
2024-09-10 17:30:23 +02:00
` ;return m[m.length-1]=" "+m[m.length-1]+"]"+(s?"":d),m}function sh(r){let t=[];for(let e=0;e<r.length;e+=2)t.push([r[e],r[e+1]]);return t}var le=class{constructor(t,e,n){if(this.dtype=e,this.shape=t.slice(),this.size=jt(t),n!=null){let o=n.length;_(o===this.size,()=> ` Length of values '${o}' does not match the size inferred by the shape '${this.size}' . ` )}if(e==="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||Xg(e,this.size),this.strides=Ei(t)}set(t,...e){e.length===0&&(e=[0]),_(e.length===this.rank,()=> ` The number of provided coordinates ( $ { e . length } ) must match the rank ( $ { this . rank } ) ` );let n=this.locToIndex(e);this.values[n]=t}get(...t){t.length===0&&(t=[0]);let e=0;for(let o of t){if(o<0||o>=this.shape[e]){let s= ` Requested out of range element at $ { t } . Buffer shape = $ { this . shape } ` ;throw new Error(s)}e++}let n=t[t.length-1];for(let o=0;o<t.length-1;++o)n+=this.strides[o]*t[o];return this.values[n]}locToIndex(t){if(this.rank===0)return 0;if(this.rank===1)return t[0];let e=t[t.length-1];for(let n=0;n<t.length-1;++n)e+=this.strides[n]*t[n];return e}indexToLoc(t){if(this.rank===0)return[];if(this.rank===1)return[t];let e=new Array(this.shape.length);for(let n=0;n<e.length-1;++n)e[n]=Math.floor(t/this.strides[n]),t-=e[n]*this.strides[n];return e[e.length-1]=t,e}get rank(){return this.shape.length}toTensor(){return ii().makeTensor(this.values,this.shape,this.dtype)}},ii=null,jp=null,SK=null;function J_(r){ii=r}function Q_(r){jp=r}function tE(r){SK=r}var Lt=class{constructor(t,e,n,o){this.kept=!1,this.isDisposedInternal=!1,this.shape=t.slice(),this.dtype=e||"float32",this.size=jt(t),this.strides=Ei(t),this.dataId=n,this.id=o,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){let t=await this.data();return jp.buffer(this.shape,this.dtype,t)}bufferSync(){return jp.buffer(this.shape,this.dtype,this.dataSync())}async array(){let t=await this.data();return Ju(this.shape,t,this.dtype==="complex64")}arraySync(){return Ju(this.shape,this.dataSync(),this.dtype==="complex64")}async data(){this.throwIfDisposed();let t=ii().read(this.dataId);if(this.dtype==="string"){let e=await t;try{return e.map(n=>Kp(n))}catch(n){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}}return t}dataToGPU(t){return this.throwIfDisposed(),ii().readToGPU(this.dataId,t)}dataSync(){this.throwIfDisposed();let t=ii().readSync(this.dataId);if(this.dtype==="string")try{return t.map(e=>Kp(e))}catch(e){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}return t}async bytes(){this.throwIfDisposed();let t=await ii().read(this.dataId);return this.dtype==="string"?t:new Uint8Array(t.buffer)}dispose(){this.isDisposed||(this.kerasMask&&this.kerasMask.dispose(),ii().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(t=!1){return jp.print(this,t)}clone(){return this.throwIfDisposed(),jp.clone(this)}toString(t=!1){let e=this.dataSync();return Y_(e,this.shape,this.dtype,t)}cast(t){return this.throwIfDisposed(),jp.cast(this,t)}variable(t=!0,e,n){return this.throwIfDisposed(),ii().makeVariable(this,t,e,n)}};Object.defineProperty(Lt,Symbol.hasInstance,{value:r=>!!r&&r.data!=null&&r.dataSync!=null&&r.throwIfDisposed!=null});function O(){return Jd("Tensor",()=>Lt)}O();var ml=class extends Lt{constructor(t,e,n,o){super(t.shape,t.dtype,t.dataId,o),this.trainable=e,this.name=n}assign(t){if(t.dtype!==this.dtype)throw new Error( ` dtype of the new value ( $ { t . dtype } ) and previous value ( $ { this . dtype } ) must match ` );if(!sn(t.shape,this.shape))throw new Error( ` shape of the new value ( $ { t . shape } ) and previous value ( $ { this . shape } ) must match ` );ii().disposeTensor(this),this.dataId=t.dataId,ii().incRef(this,null)}dispose(){ii().disposeVariabl
with dtype $ { s . dtype } . ` )}),e.length===1)return un(e[0]);let n=e,o={axis:t};return T.runKernel(Oi,n,o)}var ie=k({concat_:_j});function Ej(r,t,e=!1,n=!1){let o=C(r,"a","matMul"),s=C(t,"b","matMul");[o,s]=Xt(o,s);let i={a:o,b:s},a={transposeA:e,transposeB:n};return T.runKernel(Zo,i,a)}var Bt=k({matMul_:Ej});function Aj(r){let e={x:C(r,"x","sigmoid","float32")};return T.runKernel(Xs,e)}var en=k({sigmoid_:Aj});function Dj(r,t,e){let n=C(r,"x","slice","string_or_numeric");if(n.rank===0)throw new Error("Slicing scalar is not possible");let o={x:n},s={begin:t,size:e};return T.runKernel(Ui,o,s)}var Ot=k({slice_:Dj});function $ j(r){let e={x:C(r,"x","tanh","float32")};return T.runKernel(ni,e)}var li=k({tanh_: $ j});function Rj(r,t,e,n,o,s){let i=C(r,"forgetBias","basicLSTMCell"),a=C(t,"lstmKernel","basicLSTMCell"),u=C(e,"lstmBias","basicLSTMCell"),l=C(n,"data","basicLSTMCell"),c=C(o,"c","basicLSTMCell"),p=C(s,"h","basicLSTMCell"),m=ie([l,p],1),f=Bt(m,a),d=K(f,u),h=d.shape[0],g=d.shape[1]/4,x=[h,g],b=Ot(d,[0,0],x),w=Ot(d,[0,g],x),I=Ot(d,[0,g*2],x),N=Ot(d,[0,g*3],x),E=K( $ (en(b),li(w)), $ (c,en(K(i,I)))),A= $ (li(E),en(N));return[E,A]}var $ E=k({basicLSTMCell_:Rj});function Fj(r,t,e){let n=C(r,"x","batchToSpaceND"),o=t.reduce((a,u)=>a*u);_(n.rank>=1+t.length,()=> ` input rank is $ { n . rank } but should be > than blockShape . length $ { t . length } ` ),_(e.length===t.length,()=> ` crops . length is $ { e . length } but should be equal to blockShape . length $ { t . length } ` ),_(n.shape[0]%o===0,()=> ` input tensor batch is $ { n . shape [ 0 ] } but is not divisible by the product of the elements of blockShape $ { t . join ( " * " ) } === $ { o } ` );let s={x:n},i={blockShape:t,crops:e};return T.runKernel(Fi,s,i)}var yu=k({batchToSpaceND_:Fj});function RE(r){let t;return r.rank===0||r.rank===1?t=R(r,[1,1,1,r.size]):r.rank===2?t=R(r,[1,1,r.shape[0],r.shape[1]]):r.rank===3?t=R(r,[1,r.shape[0],r.shape[1],r.shape[2]]):t=r,t}function Oj(r,t,e,n,o,s){s==null&&(s=.001);let i=C(r,"x","batchNorm"),a=C(t,"mean","batchNorm"),u=C(e,"variance","batchNorm"),l;o!=null&&(l=C(o,"scale","batchNorm"));let c;n!=null&&(c=C(n,"offset","batchNorm")),_(a.rank===u.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),_(c==null||a.rank===c.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),_(l==null||a.rank===l.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let m={x:RE(i),scale:l,offset:c,mean:a,variance:u},f={varianceEpsilon:s},d=T.runKernel(ds,m,f);return R(d,i.shape)}var oa=k({batchNorm_:Oj});function Mj(r,t,e,n,o,s){let i=C(r,"x","batchNorm"),a=C(t,"mean","batchNorm"),u=C(e,"variance","batchNorm"),l;o!=null&&(l=C(o,"scale","batchNorm"));let c;return n!=null&&(c=C(n,"offset","batchNorm")),_(i.rank===2,()=> ` Error in batchNorm2D : x must be rank 2 but got rank $ { i . rank } . ` ),_(a.rank===2||a.rank===1,()=> ` Error in batchNorm2D : mean must be rank 2 or rank 1 but got rank $ { a . rank } . ` ),_(u.rank===2||u.rank===1,()=> ` Error in batchNorm2D : variance must be rank 2 or rank 1 but got rank $ { u . rank } . ` ),l!=null&&_(l.rank===2||l.rank===1,()=> ` Error in batchNorm2D : scale must be rank 2 or rank 1 but got rank $ { l . rank } . ` ),c!=null&&_(c.rank===2||c.rank===1,()=> ` Error in batchNorm2D : offset must be rank 2 or rank 1 but got rank $ { c . rank } . ` ),oa(i,a,u,c,l,s)}var Cx=k({batchNorm2d_:Mj});function Pj(r,t,e,n,o,s){let i=C(r,"x","batchNorm"),a=C(t,"mean","batchNorm"),u=C(e,"variance","batchNorm"),l;o!=null&&(l=C(o,"scale","batchNorm"));let c;return n!=null&&(c=C(n,"offset","batchNorm")),_(i.rank===3,()=> ` Error in batchNorm3D : x must be rank 3 but got rank $ { i . rank } . ` ),_(a.rank===3||a.rank===1,()=> ` Error in batchNorm3D : mean must be rank 3 or rank 1 but got rank $ { a . rank } . ` ),_(u.rank===3||u.rank===1,()=> ` Error in batchNorm3D : variance must be rank 3 or rank 1 but got rank $ { u . rank } . ` ),l!=null&&_(l.rank===3||l.rank===1,()=> ` Error in batchNorm3D : scale must be rank 3 or rank 1 but got rank $ { l . rank } . ` ),c!=null&&_(c.rank===3||c.rank===1,()=> ` Error in batchNorm3D : offset must be rank 3 or rank 1 but got rank $ { c . rank } . ` ),oa(i,a,u,
2024-01-17 22:56:53 +01:00
$ { o } and $ { t } for depthToSpace with input shape
$ { n . shape } ` ),_(s*t>=0,()=> ` Negative dimension size caused by overflow when multiplying
$ { s } and $ { t } for depthToSpace with input shape
2024-09-10 17:30:23 +02:00
$ { n . shape } ` ),_(i%(t*t)===0,()=> ` Dimension size must be evenly divisible by $ { t * t } but is $ { i } for depthToSpace with input shape $ { n . shape } ` );let a={x:n},u={blockSize:t,dataFormat:e};return T.runKernel(Pa,a,u)}var Fx=k({depthToSpace_:a6});function l6(r,t,e,n,o="NHWC",s=[1,1],i){let a=C(r,"x","depthwiseConv2d","float32"),u=C(t,"filter","depthwiseConv2d","float32"),l=a,c=!1;a.rank===3&&(c=!0,l=R(a,[1,a.shape[0],a.shape[1],a.shape[2]])),_(l.rank===4,()=> ` Error in depthwiseConv2d : input must be rank 4 , but got rank $ { l . rank } . ` ),_(u.rank===4,()=> ` Error in depthwiseConv2d : filter must be rank 4 , but got rank $ { u . rank } . ` );let p=o==="NHWC"?l.shape[3]:l.shape[1];_(p===u.shape[2],()=> ` Error in depthwiseConv2d : number of input channels ( $ { p } ) must match the inChannels dimension in filter $ { u . shape [ 2 ] } . ` ),Se("depthwiseConv2d",n,i);let m={x:l,filter:u},f={strides:e,pad:n,dataFormat:o,dilations:s,dimRoundingMode:i},d=T.runKernel(ss,m,f);return c?R(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var ia=k({depthwiseConv2d_:l6});function u6(r){let e={x:C(r,"x","diag")};return T.runKernel(Xl,e)}var ME=k({diag_:u6});function c6(r,t,e,n,o=[1,1],s="NHWC"){let i=C(r,"x","dilation2d"),a=C(t,"filter","dilation2d");_(i.rank===3||i.rank===4,()=> ` Error in dilation2d : input must be rank 3 or 4 , but got rank $ { i . rank } . ` ),_(a.rank===3,()=> ` Error in dilation2d : filter must be rank 3 , but got rank $ { a . rank } . ` ),_(s==="NHWC",()=> ` Error in dilation2d : Only NHWC is currently supported , but got dataFormat of $ { s } ` );let u=i,l=!1;i.rank===3&&(u=R(i,[1,i.shape[0],i.shape[1],i.shape[2]]),l=!0),_(u.shape[3]===a.shape[2],()=> ` Error in dilation2d : input and filter must have the same depth : $ { u . shape [ 3 ] } vs $ { a . shape [ 2 ] } ` );let c={x:u,filter:a},p={strides:e,pad:n,dilations:o},m=T.runKernel(is,c,p);return l?R(m,[m.shape[1],m.shape[2],m.shape[3]]):m}var Ox=k({dilation2d_:c6});var Hr={};Kt(Hr,{assertAndGetBroadcastShape:()=>Mt,getBroadcastDims:()=>PE,getReductionAxes:()=>we});function PE(r,t){let e=r.length,n=[];for(let o=0;o<e;o++){let s=e-1-o,i=r[s]||1;(t[t.length-1-o]||1)>1&&i===1&&n.unshift(s)}return n}function we(r,t){let e=[];for(let n=0;n<t.length;n++){let o=r[r.length-n-1],s=t.length-n-1,i=t[s];(o==null||o===1&&i>1)&&e.unshift(s)}return e}function Mt(r,t){let e=Math.max(r.length,t.length),n=new Array(e);for(let o=0;o<e;o++){let s=r[r.length-o-1];s==null&&(s=1);let i=t[t.length-o-1];if(i==null&&(i=1),s===1)n[e-o-1]=i;else if(i===1)n[e-o-1]=s;else if(s!==i){let a= ` Operands could not be broadcast together with shapes $ { r } and $ { t } . ` ;throw Error(a)}else n[e-o-1]=s}return n}function p6(r,t){let e=C(r,"a","equal","string_or_numeric"),n=C(t,"b","equal","string_or_numeric");[e,n]=Xt(e,n),Mt(e.shape,n.shape);let o={a:e,b:n};return T.runKernel(za,o)}var $ r=k({equal_:p6});function m6(r,t,e){let n=C(t,"a","where"),o=C(e,"b","where"),s=C(r,"condition","where","bool"),i=Mt(Mt(s.shape,n.shape),o.shape),a=sa(s,i),u=sa(n,i),l=sa(o,i),c={condition:a,t:u,e:l};return T.runKernel(Wi,c)}var Ie=k({where_:m6});function f6(r){let e={x:C(r,"x","zerosLike")};return T.runKernel(ji,e)}var vt=k({zerosLike_:f6});function d6(r,t){let e=C(r,"a","div"),n=C(t,"b","div");[e,n]=Xt(e,n);let o=ut(e,n),s=vt(o),i= $ r(n,s);return Ie(i,s,o)}var Mx=k({divNoNan_:d6});function h6(r,t){let e=C(r,"t1","dot"),n=C(t,"t2","dot");_((e.rank===1||e.rank===2)&&(n.rank===1||n.rank===2),()=> ` Error in dot : inputs must all be rank 1 or 2 , but got ranks $ { e . rank } and $ { n . rank } . ` );let o=e.rank===1?e.size:e.shape[1],s=n.rank===1?n.size:n.shape[0];if(_(o===s,()=> ` Error in dot : inner dimensions of inputs must match , but got $ { o } and $ { s } . ` ),e.rank===1&&n.rank===1){let i=R(e,[1,-1]),a=R(n,[-1,1]),u=Bt(i,a);return R(u,[])}else if(e.rank===1&&n.rank===2){let i=R(e,[1,-1]),a=R(n,[n.shape[0],n.shape[1]]),u=Bt(i,a);return R(u,[u.size])}else if(e.rank===2&&n.rank===1){let i=R(n,[-1,1]),a=Bt(e,i);return R(a,[a.size])}else{let i=R(n,[n.shape[0],n.shape[1]]);return Bt(e,i)}}var Px=k({dot_:h6});function g6(r,...t){let e=t.map((o,s)=>C(o, ` tensors$ { s } ` ,"einsum")),n={equation:r};return T.runKernel(Fp,e,n)}var wu=k({einsum_:g6});function
rank $ { s . rank } . ` ),_(_a(t),()=> ` Error in localResponseNormalization : depthRadius must be an integer but got depthRadius $ { t } . ` );let i=s,a=!1;s.rank===3&&(a=!0,i=R(s,[1,s.shape[0],s.shape[1],s.shape[2]]));let u={x:i},l={depthRadius:t,bias:e,alpha:n,beta:o},c=T.runKernel(Cs,u,l);return a?R(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var Wx=k({localResponseNormalization_:q6});function K6(r){let e={x:C(r,"x","log","float32")};return T.runKernel(ws,e)}var Nr=k({log_:K6});function j6(r){let e={x:C(r,"x","log1p")};return T.runKernel(Is,e)}var vu=k({log1p_:j6});function X6(r){return _(_i(r),()=>"The f passed in grad(f) must be a function"),(t,e)=>{let n=C(t,"x","tf.grad","string_or_numeric"),o=e!=null?C(e,"dy","tf.grad"):null;return T.tidy(()=>{let{value:s,grads:i}=T.gradients(()=>r(n),[n],o);return o!=null&& $ e(s.shape,o.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Hx(i),i[0]})}}function Y6(r){return _(_i(r),()=>"The f passed in grads(f) must be a function"),(t,e)=>{_(Array.isArray(t),()=>"The args passed in grads(f)(args) must be an array of ` Tensor ` s or ` TensorLike ` s");let n=fl(t,"args","tf.grads","string_or_numeric"),o=e!=null?C(e,"dy","tf.grads"):null;return T.tidy(()=>{let{value:s,grads:i}=T.gradients(()=>r(...n),n,o);return o!=null&& $ e(s.shape,o.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Hx(i),i})}}function Z6(r){return _(_i(r),()=>"The f passed in valueAndGrad(f) must be a function"),(t,e)=>{_(t instanceof Lt,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"),_(e==null||e instanceof Lt,()=>"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");let{grads:n,value:o}=T.gradients(()=>r(t),[t],e);return Hx(n),{grad:n[0],value:o}}}function J6(r){return _(_i(r),()=>"The f passed in valueAndGrads(f) must be a function"),(t,e)=>{_(Array.isArray(t)&&t.every(o=>o instanceof Lt),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"),_(e==null||e instanceof Lt,()=>"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");let n=T.gradients(()=>r(...t),t,e);return e!=null&& $ e(n.value.shape,e.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Hx(n.grads),n}}function Ux(r,t){_(_i(r),()=>"The f passed in variableGrads(f) must be a function"),_(t==null||Array.isArray(t)&&t.every(l=>l instanceof ml),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let e=t!=null;if(!e){t=[];for(let l in T.registeredVariables)t.push(T.registeredVariables[l])}let n=e?t.filter(l=>!l.trainable):null,o=t.length;t=t.filter(l=>l.trainable),_(t.length>0,()=> ` variableGrads ( ) expects at least one of the input variables to be trainable , but none of the $ { o } variables is trainable . ` );let s=!0,{value:i,grads:a}=T.gradients(r,t,null,s);_(a.some(l=>l!=null),()=>"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),_(i.rank===0,()=> ` The f passed in variableGrads ( f ) must return a scalar , but it returned a rank - $ { i . rank } tensor ` );let u={};return t.forEach((l,c)=>{a[c]!=null&&(u[l.name]=a[c])}),n!=null&&n.forEach(l=>u[l.name]=null),{value:i,grads:u}}function pn(r){return T.customGrad(r)}function Hx(r){if(r.filter(e=>e==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 Q6(r){let e={x:C(r,"x","neg")};return T.runKernel(zi,e)}var Ut=k({neg_:Q6});function tX(r){let e={x:C(r,"x","softplus")};return T.runKernel(Ys,e)}var ui=k({softplus_:tX});function eX(r){let t=C(r,"x","logSigmoid");return pn(n=>({value:Ut(ui(Ut(n))),gradFunc:i=> $ (i,en(Ut(n)))}))(t)}var qx=k({logSigmoid_:eX});function rX(r,t){let e=C(r,"a","sub"),n=C(t,"b","sub");[e,n]=Xt(e,n);let o={a:e,b:n};return T.runKernel(ei,o)}var at=k({sub_:rX});function nX(r,t=-1){let e=C(r,"logits","logSoftmax");if(t===-1&&(t=e.rank-1),t!==e.rank-1)throw Error( ` Log Softmax along a non - last dimension is not yet supported . Logits was rank $ { e . rank } and axis was $ { t } ` );return pn((o,s)=>{let a=Sr(o,t,!0),u=at(o,a),l=at(J(u,"float32"),Nr(mt(Ke(u),t,!0)));return s([l]),{value:l,gradFunc:(p,m)=>{let[f]=m,d=!0,h=Ke(f);return at(p, $ (mt(p,t,d),h))}}})(e)}var lm=k({logSoftmax_:nX});function oX(r,t=null,e=!1){let n=C(r,"x","logSumExp"),o=fr(t,n.shape),s=Sr(n,o,!0),i=at(n,s),a=Ke(i),u=mt(a,o),l=Nr(u),c=K(R(s,l.shape),l);if(e){let p=vo(c.shape,o);return R(c,p)}return c}var Su=k({logSumExp_:oX});function sX(r,t){let e=C(r,"a","logicalAnd","bool"),n=C(t,"b","logicalAnd","bool");Mt(e.shape,n.shape);let o={a:e,b:n};return T.runKernel(qa,o)}var Fr=k({logicalAnd_:sX});function iX(r){let e={x:C(r,"x","logicalNot","bool")};return T.runKernel(Ka,e)}var Nu=k({logicalNot_:iX});function aX(r,t){let e=C(r,"a","logicalOr","bool"),n=C(t,"b","logicalOr","bool");Mt(e.shape,n.shape);let o={a:e,b:n};return T.runKernel(ja,o)}var um=k({logicalOr_:aX});function lX(r,t){let e=C(r,"a","logicalXor","bool"),n=C(t,"b","logicalXor","bool");return Mt(e.shape,n.shape),Fr(um(r,t),Nu(Fr(r,t)))}var Kx=k({logicalXor_:lX});var jx=2147483648;function uX(r,t,e="left"){let n=C(r,"sortedSequence","searchSorted"),o=C(t,"values","searchSorted"),s=n.shape[n.shape.length-1],i=o.shape[o.shape.length-1],a=R(n,[-1,s]),u=R(o,[-1,i]);if(a.rank<2)throw new Error("Sorted input argument must be at least 2-dimensional");if(a.shape[0]!==u.shape[0])throw new Error("Leading dimension of 'sortedSequence' and 'values' must match.");if(jt(u.shape)>=jx)throw new Error( ` values tensor size must less than $ { jx } ` );if(a.shape[1]>=jx)throw new Error( ` trailing dim _size must less than $ { jx } for int32 output type , was $ { a . shape [ 1 ] } ` );let l={sortedSequence:a,values:u},c={side:e};return T.runKernel(sl,l,c)}var dh=k({searchSorted_:uX});function GE(r,t){return dh(r,t,"left")}function cX(r,t,e,n,o){let s=C(r,"x","maxPool"),i=1,a=s,u=!1;s.rank===3&&(u=!0,a=R(s,[1,s.shape[0],s.shape[1],s.shape[2]])),_(a.rank===4,()=> ` Error in maxPool : input must be rank 4 but got rank $ { a . rank } . ` ),_(Dr(e,i),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { e } and dilations '${i}' ` ),Se("maxPool",n,o);let l={x:a},c={filterSize:t,strides:e,pad:n,dimRoundingMode:o},p=T.runKernel(Ns,l,c);return u?R(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var ku=k({maxPool_:cX});function pX(r,t=[1,1,1],e,n,o,s="NDHWC"){let i=C(r,"x","maxPool3d"),a=i,u=!1;i.rank===4&&(u=!0,a=R(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])),_(a.rank===5,()=> ` Error in maxPool3d : x must be rank 5 but got rank $ { a . rank } . ` ),_(s==="NDHWC",()=> ` Error in maxPool3d : Only NDHWC is currently supported , but got dataFormat of $ { s } ` ),Se("maxPool3d",n,o);let l={x:a},c={filterSize:t,strides:e,pad:n,dimRoundingMode:o,dataFormat:s},p=T.runKernel(Li,l,c);return u?R(p,[p.shape[1],p.shape[2],p.shape[3],p.shape[4]]):p}var Xx=k({maxPool3d_:pX});function mX(r,t,e,n,o=!1){let i={x:C(r,"x","maxPoolWithArgmax")},a={filterSize:t,strides:e,pad:n,includeBatchInIndex:o},u=T.runKernel(eu,i,a);return{result:u[0],indexes:u[1]}}var WE=k({maxPoolWithArgmax_:mX});function fX(r,t){let e=C(r,"a","maximum"),n=C(t,"b","maximum");[e,n]=Xt(e,n),e.dtype==="bool"&&(e=J(e,"int32"),n=J(n,"int32")),Mt(e.shape,n.shape);let o={a:e,b:n};return T.runKernel(Ss,o)}var kn=k({maximum_:fX});function dX(r,t=null,e=!1){let o={x:C(r,"x","mean")},s={axis:t,keepDims:e};return T.runKernel(ks,o,s)}var Ne=k({mean_:dX});function ke(r,t="float32")
2024-01-17 22:56:53 +01:00
Actual : $ { o } .
Expected : $ { s } . ` );for(let i=0;i<s.length;++i){let a=o[i],u=s[i];if(!e(a,u))throw new Error( ` Arrays differ : actual [ $ { i } ] = $ { a } , expected [ $ { i } ] = $ { u } .
Actual : $ { o } .
2024-09-10 17:30:23 +02:00
Expected : $ { s } . ` )}typeof expect!="undefined"&&expect().nothing()}function jX(r,t){r().then(()=>t.fail(),()=>t()),typeof expect!="undefined"&&expect().nothing()}function XX(r,t){let e=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return Vo(r)||Vo(r[0])||Vo(t)||Vo(t[0])?pN(r,e,(n,o)=>n==o):pN(r,t,(n,o)=>mN(n,o,0))}function bA(r,t,e){if(e==null&&(e=ey()),!mN(r,t,e))throw new Error( ` Numbers differ : actual === $ { r } , expected === $ { t } ` );typeof expect!="undefined"&&expect().nothing()}function mN(r,t,e){return!isFinite(r)&&!isFinite(t)?!0:!(isNaN(r)||isNaN(t)||Math.abs(r-t)>e)}function YX(r,t,e){for(let n=0;n<r.length;n++)if(r[n]<t||r[n]>e)throw new Error( ` Value out of range : $ { r [ n ] } low : $ { t } , high : $ { e } ` )}function ZX(r,t){let e=new Float32Array(r),n=new Float32Array(t);if(e.length!==n.length)throw new Error( ` Expected ArrayBuffer to be of length $ { n . length } , but it was $ { e . length } ` );for(let o=0;o<n.length;o++)if(e[o]!==n[o])throw new Error( ` Expected ArrayBuffer value at $ { o } to be $ { n [ o ] } but got $ { e [ o ] } instead ` )}function wA(r){for(let t=0;t<r.length;t++){let e=r[t];Array.isArray(e)?wA(e):r[t]=fu(e)}return r}function JX(r){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(r),new Promise(e=>{t.addEventListener("loadeddata",n=>e(t)),t.load()})}async function QX(r){await r.play(),"requestVideoFrameCallback"in r&&await new Promise(t=>{r.requestVideoFrameCallback(t)})}var gc=class{constructor(t,e,n,o,s){this.mean=t,this.stdDev=e,this.dtype=n,this.nextVal=NaN,this.truncated=o,this.truncated&&(this.upper=this.mean+this.stdDev*2,this.lower=this.mean-this.stdDev*2);let i=s||Math.random();this.random=oy.alea(i.toString())}nextValue(){if(!isNaN(this.nextVal)){let o=this.nextVal;return this.nextVal=NaN,o}let t,e,n=!1;for(;!n;){let o,s,i;do o=2*this.random()-1,s=2*this.random()-1,i=o*o+s*s;while(i>=1||i===0);let a=Math.sqrt(-2*Math.log(i)/i);t=this.mean+this.stdDev*o*a,e=this.mean+this.stdDev*s*a,(!this.truncated||this.isValidTruncated(t))&&(n=!0)}return(!this.truncated||this.isValidTruncated(e))&&(this.nextVal=this.convertValue(e)),this.convertValue(t)}convertValue(t){return this.dtype==null||this.dtype==="float32"?t:Math.round(t)}isValidTruncated(t){return t<=this.upper&&t>=this.lower}},ry=class{constructor(t,e,n,o){this.alpha=t,this.beta=1/e,this.dtype=n;let s=o||Math.random();this.randu=oy.alea(s.toString()),this.randn=new gc(0,1,n,!1,this.randu()),t<1?this.d=t+2/3:this.d=t-1/3,this.c=1/Math.sqrt(9*this.d)}nextValue(){let t,e,n,o,s,i;for(;;){do o=this.randn.nextValue(),i=1+this.c*o;while(i<=0);if(i*=i*i,t=o*o,e=1-.331*t*t,n=.5*t+this.d*(1-i+Math.log(i)),s=this.randu(),s<e||Math.log(s)<n)break}return i=1/this.beta*this.d*i,this.alpha<1&&(i*=Math.pow(this.randu(),1/this.alpha)),this.convertValue(i)}convertValue(t){return this.dtype==="float32"?t:Math.round(t)}},ny=class{constructor(t=0,e=1,n,o){if(this.canReturnFloat=()=>this.dtype==null||this.dtype==="float32",this.min=t,this.range=e-t,this.dtype=n,o==null&&(o=Math.random()),typeof o=="number"&&(o=o.toString()),!this.canReturnFloat()&&this.range<=1)throw new Error( ` The difference between $ { t } - $ { e } <= 1 and dtype is not float ` );this.random=oy.alea(o)}convertValue(t){return this.canReturnFloat()?t:Math.round(t)}nextValue(){return this.convertValue(this.min+this.range*this.random())}};function t5(r,t,e=1,n="float32",o){if(Le(r),e==null&&(e=1),n==null&&(n="float32"),n!=="float32"&&n!=="int32")throw new Error( ` Unsupported data type $ { n } ` );let s=new ry(t,e,n,o),i=wt(r,n);for(let a=0;a<i.values.length;a++)i.values[a]=s.nextValue();return i.toTensor()}var CA=k({randomGamma_:t5});function e5(r,t=0,e=1,n,o){if(Le(r),n!=null&&n==="bool")throw new Error( ` Unsupported data type $ { n } ` );let s=new gc(t,e,n,!1,o),i=wt(r,n);for(let a=0;a<i.values.length;a++)i.values[a]=s.nextValue();return i.toTensor()}var xc=k({randomNormal_:e5});function r5(r,t,e){if(t!=null&&t==="bool")throw new Error( ` Unsupported data type $ { t } ` );return xc(r,0,1,t,e)}va
$ { o . 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(a.rank!==0)throw new Error( ` Default value should be a scalar but received shape $ { a . shape } ` );let u={indices:o,values:s,denseShape:i,defaultValue:a},l=T.runKernel(nu,u);return{outputIndices:l[0],outputValues:l[1],emptyRowIndicator:l[2],reverseIndexMap:l[3]}}var g2=k({sparseFillEmptyRows_:Z8});function J8(r,t,e){let n=C(r,"inputIndices","sparseReshape","int32"),o=C(t,"inputShape","sparseReshape","int32"),s=C(e,"newShape","sparseReshape","int32");if(n.rank!==2)throw new Error( ` Input indices should be Tensor2D but received shape
$ { n . shape } ` );if(o.rank!==1)throw new Error( ` Input shape should be Tensor1D but received shape $ { o . shape } ` );if(s.rank!==1)throw new Error( ` New shape should be Tensor1D but received shape $ { s . shape } ` );let i={inputIndices:n,inputShape:o,newShape:s},a=T.runKernel(il,i);return{outputIndices:a[0],outputShape:a[1]}}var x2=k({sparseReshape_:J8});function Q8(r,t,e){let n=C(r,"data","sparseSegmentMean"),o=C(t,"indices","sparseSegmentMean","int32"),s=C(e,"segmentIds","sparseSegmentMean","int32");if(n.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(o.rank!==1)throw new Error( ` Indices should be Tensor1D but received shape
2024-01-17 22:56:53 +01:00
$ { o . shape } ` );if(s.rank!==1)throw new Error( ` Segment ids should be Tensor1D but received shape
2024-09-10 17:30:23 +02:00
$ { s . shape } ` );let i={data:n,indices:o,segmentIds:s};return T.runKernel(ou,i)}var y2=k({sparseSegmentMean_:Q8});function tY(r,t,e){let n=C(r,"data","sparseSegmentSum"),o=C(t,"indices","sparseSegmentSum","int32"),s=C(e,"segmentIds","sparseSegmentSum","int32");if(n.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(o.rank!==1)throw new Error( ` Indices should be Tensor1D but received shape
2024-01-17 22:56:53 +01:00
$ { o . shape } ` );if(s.rank!==1)throw new Error( ` Segment ids should be Tensor1D but received shape
2024-09-10 17:30:23 +02:00
$ { s . shape } ` );let i={data:n,indices:o,segmentIds:s};return T.runKernel(su,i)}var b2=k({sparseSegmentSum_:tY});function eY(r,t,e,n,o,s,i,a){let u=C(r,"data","stringNGrams","string");if(u.dtype!=="string")throw new Error("Data must be of datatype string");if(u.shape.length!==1)throw new Error( ` Data must be a vector , saw : $ { u . shape } ` );let l=C(t,"dataSplits","stringNGrams");if(l.dtype!=="int32")throw new Error("Data splits must be of datatype int32");let c={separator:e,nGramWidths:n,leftPad:o,rightPad:s,padWidth:i,preserveShortSequences:a},p={data:u,dataSplits:l},m=T.runKernel(au,p,c);return{nGrams:m[0],nGramsSplits:m[1]}}var w2=k({stringNGrams_:eY});function rY(r,t,e=!0){let n=C(r,"input","stringSplit","string"),o=C(t,"delimiter","stringSplit","string");if(n.rank!==1)throw new Error( ` Input should be Tensor1D but received shape $ { n . shape } ` );if(o.rank!==0)throw new Error( ` Delimiter should be a scalar but received shape $ { o . shape } ` );let s={skipEmpty:e},i={input:n,delimiter:o},a=T.runKernel(lu,i,s);return{indices:a[0],values:a[1],shape:a[2]}}var I2=k({stringSplit_:rY});function nY(r,t){let e=C(r,"input","stringToHashBucketFast","string"),n={numBuckets:t};if(t<=0)throw new Error("Number of buckets must be at least 1");let o={input:e};return T.runKernel(uu,o,n)}var C2=k({stringToHashBucketFast_:nY});function oY(r,t,e,n=!0){let o=C(r,"input","staticRegexReplace","string"),s={pattern:t,rewrite:e,replaceGlobal:n};return T.runKernel(ec,{x:o},s)}var v2=k({staticRegexReplace_:oY});var sY={fft:Au,ifft:wl,rfft:Du,irfft:bm},iY={hammingWindow:VA,hannWindow:xy,frame:yy,stft:GA},fn={flipLeftRight:UA,grayscaleToRGB:HA,resizeNearestNeighbor:vy,resizeBilinear:Cy,rgbToGrayscale:qA,rotateWithOffset:KA,cropAndResize:WA,nonMaxSuppression:jA,nonMaxSuppressionAsync:ZA,nonMaxSuppressionWithScore:JA,nonMaxSuppressionWithScoreAsync:QA,nonMaxSuppressionPadded:t2,nonMaxSuppressionPaddedAsync:e2,threshold:r2,transform:n2},xN={bandPart:o2,gramSchmidt:s2,qr:a2},aY={absoluteDifference:l2,computeWeightedLoss:Kr,cosineDistance:u2,hingeLoss:c2,huberLoss:p2,logLoss:m2,meanSquaredError:f2,sigmoidCrossEntropy:d2,softmaxCrossEntropy:h2},lY={sparseFillEmptyRows:g2,sparseReshape:x2,sparseSegmentMean:y2,sparseSegmentSum:b2},uY={stringNGrams:w2,stringSplit:I2,stringToHashBucketFast:C2,staticRegexReplace:v2};var Q={};Kt(Q,{Serializable:()=>yh,SerializationMap:()=>Sy,getRegisteredName:()=>pY,registerClass:()=>bN});var cY=new Map,yN=new Map,yh=class{getClassName(){return this.constructor.className}static fromConfig(t,e){return new t(e)}},Sy=class r{constructor(){this.classNameMap={}}static getMap(){return r.instance==null&&(r.instance=new r),r.instance}static register(t){r.getMap().classNameMap[t.className]=[t,t.fromConfig]}};function bN(r,t,e){_(r.className!=null,()=>"Class being registered does not have the static className property defined."),_(typeof r.className=="string",()=>"className is required to be a string, but got type "+typeof r.className),_(r.className.length>0,()=>"Class being registered has an empty-string as its className, which is disallowed."),typeof t=="undefined"&&(t="Custom"),typeof e=="undefined"&&(e=r.className);let n=e,o=t+">"+n;return Sy.register(r),cY.set(o,r),yN.set(r,o),r}function pY(r){return yN.has(r)?yN.get(r):r.className}var jr=class extends yh{minimize(t,e=!1,n){let{value:o,grads:s}=this.computeGradients(t,n);if(n!=null){let i=n.map(a=>({name:a.name,tensor:s[a.name]}));this.applyGradients(i)}else this.applyGradients(s);return Tt(s),e?o:(o.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(t,e){return Ux(t,e)}dispose(){this.iterations_!=null&&Tt(this.iterations_)}async saveIterations(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:pt(this.iterations_,"int32")}}async getWeights(){throw new Error("getWeights() is not implemented for this optimizer yet.")}async setWeights(t){throw new Error( ` setWeights ( ) is not implemented for this optimizer class $ { this . getClassName ( ) } ` )}async extractI
Manifest JSON has weights with names : $ { a . join ( ", " ) } . ` )}let u=o.reduce((f,d,h)=>(d&&f.push(h),f),[]),l=[];u.forEach(f=>{t[f].paths.forEach(d=>{let h=e+(e.endsWith("/")?"":"/")+d;l.push(h)})});let c=await r(l),p={},m=0;return u.forEach(f=>{let d=t[f].paths.length,h=new Ur(c.slice(m,m+d));s[f].forEach(x=>{let b=h.slice(x.groupOffset,x.groupOffset+x.sizeBytes),w=ix(b,[x.manifestEntry]);for(let I in w)p[I]=w[I]}),m+=d}),p}}var yY="application/octet-stream",bY="application/json",bh=class{constructor(t,e){if(this.DEFAULT_METHOD="POST",e==null&&(e={}),this.weightPathPrefix=e.weightPathPrefix,this.weightUrlConverter=e.weightUrlConverter,e.fetchFunc!=null?(_(typeof e.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=e.fetchFunc):this.fetch=L().platform.fetch,_(t!=null&&t.length>0,()=>"URL path for http must not be null, undefined or empty."),Array.isArray(t)&&_(t.length===2,()=> ` URL paths for http must have a length of 2 , ( actual length is $ { t . length } ) . ` ),this.path=t,e.requestInit!=null&&e.requestInit.body!=null)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=e.requestInit||{},this.loadOptions=e}async save(t){if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");let e=Object.assign({method:this.DEFAULT_METHOD},this.requestInit);e.body=new FormData;let n=[{paths:["./model.weights.bin"],weights:t.weightSpecs}],o=lx(t,n);if(e.body.append("model.json",new Blob([JSON.stringify(o)],{type:bY}),"model.json"),t.weightData!=null){let i=Ur.join(t.weightData);e.body.append("model.weights.bin",new Blob([i],{type:yY}),"model.weights.bin")}let s=await this.fetch(this.path,e);if(s.ok)return{modelArtifactsInfo:Qi(t),responses:[s]};throw new Error( ` BrowserHTTPRequest . save ( ) failed due to HTTP response status $ { s . status } . ` )}async loadModelJSON(){let t=await this.fetch(this.path,this.requestInit);if(!t.ok)throw new Error( ` Request to $ { this . path } failed with status code $ { t . status } . Please verify this URL points to the model JSON of the model to load . ` );let e;try{e=await t.json()}catch(s){let i= ` Failed to parse model JSON of response from $ { this . path } . ` ;throw this.path.endsWith(".pb")?i+=" 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.":i+=" Please make sure the server is serving valid JSON for this request.",new Error(i)}let n=e.modelTopology,o=e.weightsManifest;if(n==null&&o==null)throw new Error( ` The JSON from HTTP path $ { this . path } contains neither model topology or manifest for weights . ` );return e}async load(){if(this.loadOptions.streamWeights)return this.loadStream();let t=await this.loadModelJSON();return Yp(t,e=>this.loadWeights(e))}async loadStream(){let t=await this.loadModelJSON(),e=await this.getWeightUrls(t.weightsManifest),n=uh(t.weightsManifest),o=()=>T2(e,this.loadOptions);return Object.assign(Object.assign({},t),{weightSpecs:n,getWeightStream:o})}async getWeightUrls(t){let e=Array.isArray(this.path)?this.path[1]:this.path,[n,o]=wY(e),s=this.weightPathPrefix||n,i=[],a=[];for(let u of t)for(let l of u.paths)this.weightUrlConverter!=null?a.push(this.weightUrlConverter(l)):i.push(s+l+o);return this.weightUrlConverter&&i.push(...await Promise.all(a)),i}async loadWeights(t){let e=await this.getWeightUrls(t),n=uh(t),o=await CN(e,this.loadOptions);return[n,o]}};bh.URL_SCHEME_REGEX=/^https?: \/ \/ /;function wY(r){let t=r.lastIndexOf("/"),e=r.lastIndexOf("?"),n=r.substring(0,t),o=e>t?r.substring(e):"";return[n+"/",o]}function Ny(r){return r.match(bh.URL_SCHEME_REGEX)!=null}var E2=(r,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let e=!0;if(Array.isArray(r)?e=r.every(n=>Ny(n)
indices . shape [ 0 ] = $ { r } ` }function vZ(r,t){return ` indices ( $ { r } , 0 ) is invalid : $ { t } < 0 ` }function SZ(r,t,e){return ` indices ( $ { r } , 0 ) is invalid : $ { t } >= $ { e } ` }function NZ(r,t){return ` only one output dimension may be - 1 , not both $ { r } and $ { t } ` }function kZ(r,t){return ` size $ { r } must be non - negative , not $ { t } ` }function TZ(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function _Z(r,t){let e=jt(r),n=jt(t);return ` Input to reshape is a SparseTensor with $ { e }
dense values , but the requested shape requires a multiple of $ { n } . inputShape = $ { r } outputShape = $ { t } ` }function EZ(r,t){let e=jt(r),n=jt(t);return ` Input to reshape is a tensor with $ { e } dense values , but the requested shape has $ { n } . inputShape = $ { r } outputShape = $ { t } ` }function AZ(){return"segment ids must be >= 0"}function DZ(){return"segment ids are not increasing"}function $ Z(r,t){return ` Segment id $ { r } out of range [ 0 , $ { t } ) , possibly because segmentIds input is not sorted . ` }function RZ(r,t,e){return ` Bad : indices [ $ { r } ] == $ { t } out of range [ 0 , $ { e } ) ` }var DN={};Kt(DN,{collectGatherOpShapeInfo:()=>MZ,computeOutShape:()=>OZ,segOpComputeOptimalWindowSize:()=>FZ});function FZ(r,t){let e=!1,n;for(r<=Ay?(n=r,e=!0):n=_p(r,Math.floor(Math.sqrt(r)));!e;)n>t||n===r?e=!0:n=_p(r,n+1);return n}function OZ(r,t,e){let n=[],o=r.length;for(let s=0;s<o;s++)s!==t?n.push(r[s]):n.push(e);return n}function MZ(r,t,e,n){let o=t.shape.length,s=r.shape.length;if(n!==0&&(n<-o||n>o))throw new Error( ` Expect batchDims in the range of [ - $ { o } , $ { o } ] , but got $ { n } ` );if(n<0&&(n+=o),n>s)throw new Error( ` batchDims ( $ { n } ) must be less than rank ( x ) (
$ { s } ) . ` );if(e<n)throw new Error( ` batchDims ( $ { n } ) must be less than or equal to axis ( $ { e } ) . ` );for(let p=0;p<n;++p)if(r.shape[p]!==t.shape[p])throw new Error( ` x . shape [ $ { p } ] : $ { r . shape [ p ] } should be equal to indices . shape [ $ { p } ] : $ { t . shape [ p ] } . ` );let i=r.shape[e],a=[],u=1,l=1,c=1;for(let p=0;p<n;++p)a.push(r.shape[p]),u*=r.shape[p];for(let p=n;p<e;p++)a.push(r.shape[p]),l*=r.shape[p];for(let p=n;p<o;p++)a.push(t.shape[p]);for(let p=e+1;p<s;p++)a.push(r.shape[p]),c*=r.shape[p];return{batchSize:u,sliceSize:c,outerSize:l,dimSize:i,outputShape:a}}function PZ(r){try{return r.map(t=>Kp(t))}catch(t){throw new Error( ` Failed to decode encoded string bytes into utf - 8 , error : $ { t } ` )}}function LZ(r){return r.map(t=>fu(t))}var Xr={};Kt(Xr,{nonMaxSuppressionV3Impl:()=>by,nonMaxSuppressionV4Impl:()=>wy,nonMaxSuppressionV5Impl:()=>Iy,whereImpl:()=>fy});S2();var Dy={kernelName:Ai,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=> $ (r,So(J(e,"float32"),-1))}}};var J2={kernelName:Go,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>{let n=Wt(J(e,"float32")),o=ge(at(pt(1),n));return Ut(ut(r,o))}}}};var Q2={kernelName:Wo,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>{let n=ge(at(Wt(J(e,"float32")),1));return ut(r,n)}}}};var tD={kernelName:no,inputsToSave:["a","b"],gradFunc:(r,t)=>{let[e,n]=t,o=Mt(e.shape,n.shape);return{a:()=>{let a=r,u=we(e.shape,o);return u.length>0&&(a=mt(a,u)),R(a,e.shape)},b:()=>{let a=r,u=we(n.shape,o);return u.length>0&&(a=mt(a,u)),R(a,n.shape)}}}};var eD={kernelName:Uo,saveAllInputs:!0,gradFunc:(r,t)=>{let e={};return t.forEach((n,o)=>{e[o]=()=>r.clone()}),e}};var rD={kernelName:Di,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>vt(e)}}};var nD={kernelName: $ i,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>vt(e)}}};var oD={kernelName:Ho,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>ut(r,ge(at(pt(1),Wt(J(e,"float32")))))}}};var sD={kernelName:qo,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>{let n=ge(K(pt(1),Wt(J(e,"float32"))));return ut(r,n)}}}};var iD={kernelName:Xo,inputsToSave:["a","b"],gradFunc:(r,t)=>{let[e,n]=t,o=Mt(e.shape,n.shape);return{a:()=>{let a=K(Wt(e),Wt(n)),u= $ (r,ut(n,a)),l=we(e.shape,o);return l.length>0&&(u=mt(u,l)),R(u,e.shape)},b:()=>{let a=K(Wt(e),Wt(n)),u=Ut( $ (r,ut(e,a))),l=we(n.shape,o);return l.length>0&&(u=mt(u,l)),R(u,n.shape)}}}};var aD={kernelName:Ko,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>ut(r,K(Wt(J(e,"float32")),1))}}};var lD={kernelName:jo,inputsToSave:["x"],gradFunc:(r,t)=>{let[e]=t;return{x:()=>ut(r,at(pt(1),Wt(J(e,"float32"))))}}};function zZ(r,t,e,n,o,s){let i=C(r,"dy","avgPool3dGrad"),a=C(t,"input","avgPool3dGrad"),u=i,l=a,c=!1;a.rank===4&&(c=!0,u=R(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]]),l=R(a,[1,a.shape[0],a.shape[1],a.shape[2],a.shape[3]])),_(u.rank===5,()=> ` Error in avgPool3dGrad : dy must be rank 5 but got rank $ { u . rank } . ` ),_(l.rank===5,()=> ` Error in avgPool3dGrad : input must be rank 5 but got rank $ { l . rank } . ` ),Se("avgPool3dGrad",o,s);let p={dy:u,input:l},m={filterSize:e,strides:n,pad:o,dimRoundingMode:s},f=T.runKernel(Hl,p,m);return c?R(f,[f.shape[1],f.shape[2],f.shape[3],f.shape[4]]):f}var uD=k({avgPool3dGrad_:zZ});var cD={kernelName:Ri,inputsToSave:["x"],gradFunc:(r,t,e)=>{let[n]=t,{filterSize:o,strides:s,pad:i,dimRoundingMode:a}=e;return{x:()=>uD(r,n,o,s,i,a)}}};function BZ(r,t,e,n,o){let s=C(r,"dy","avgPoolGrad"),i=C(t,"input","avgPoolGrad");_(i.rank===s.rank,()=> ` Rank of input ( $ { i . rank } ) does not match rank of dy ( $ { s . rank } ) ` );let a=i,u=s,l=!1;i.rank===3&&(l=!0,a=R(i,[1,i.shape[0],i.shape[1],i.shape[2]]),u=R(s,[1,s.shape[0],s.shape[1],s.shape[2]])),_(u.rank===4,()=> ` Error in avgPoolGrad : dy must be rank 4 but got rank $ { u . rank } . ` ),_(a.rank===4,()=> ` Error in avgPoolGrad : input must be rank 4 but got rank $ { a . rank } . ` );let c={dy:u,input:a},p={filterSize:e,strides:n,pad:o},m=T.runKernel(Ul,c,p);return l?R(m,[m.shape[1],m.shape[2],m.shape[3]]):m}var pD=k({avgPoolGrad_:BZ});var mD={kernelName:Yo,inputsToSave:["x"],gradFunc:(r,t,e)=>{let[n]=t,{filterSize:o,strides:s,pad:i}=e
2024-01-17 22:56:53 +01:00
1. The $ { n } is defined in Python , in which case it needs to be ported to TensorFlow . js or your JavaScript code .
2. The custom $ { n } is defined in JavaScript , but is not registered properly with tf . serialization . registerClass ( ) . ` );return i}else{let s=r;if(s.className==null||s.config==null)throw new z( ` $ { n } : Improper config format : $ { JSON . stringify ( s ) } .
'className' and 'config' must set . ` );let i=s.className,a,u;if(i in e?[a,u]=e[i]:i in ko?[a,u]=ko.className:i in t&&([a,u]=t[i]),a==null)throw new z( ` Unknown $ { n } : $ { i } . This may be due to one of the following reasons :
1. The $ { n } is defined in Python , in which case it needs to be ported to TensorFlow . js or your JavaScript code .
2024-09-10 17:30:23 +02:00
2. The custom $ { n } is defined in JavaScript , but is not registered properly with tf . serialization . registerClass ( ) . ` );if(u!=null){let l={};for(let f of Object.keys(ko))l[f]=ko[f];for(let f of Object.keys(e))l[f]=e[f];let c=s.config;c.customObjects=l;let p=Object.assign({},ko);for(let f of Object.keys(e))ko[f]=e[f];MN(s.config);let m=u(a,s.config,e,o);return ko=Object.assign({},p),m}else{let l=Object.assign({},ko);for(let p of Object.keys(e))ko[p]=e[p];let c=new a(s.config);return ko=Object.assign({},l),c}}}function XZ(r,t){return r<t?-1:r>t?1:0}function Sh(r,t){return-1*XZ(r,t)}function Eo(r){if(r==null)return r;let t=[];for(let e of r)t.indexOf(e)===-1&&t.push(e);return t}function j $ (r){if(r==null)throw new z( ` Invalid value in obj : $ { JSON . stringify ( r ) } ` );for(let t in r)if(r.hasOwnProperty(t))return!1;return!0}function da(r,t,e){if(e!=null&&r.indexOf(e)<0)throw new z( ` $ { e } is not a valid $ { t } . Valid values are $ { r } or null / undefined . ` )}function Fy(r,t,e=0,n=1/0){return co(e>=0),co(n>=e),Array.isArray(r)&&r.length>=e&&r.length<=n&&r.every(o=>typeof o===t)}function tr(r,t){Array.isArray(r)?(y.assert(r.length>0,()=> ` $ { t } is unexpectedly an empty array . ` ),r.forEach((e,n)=>tr(e, ` element $ { n + 1 } of $ { t } ` ))):y.assert(Number.isInteger(r)&&r>0,()=> ` Expected $ { t } to be a positive integer , but got $ { X$ ( r ) } . ` )}function X $ (r){return r===null?"null":Array.isArray(r)?"["+r.map(t=>X $ (t)).join(",")+"]":typeof r=="string"? ` "${r}" ` : ` $ { r } ` }function Y $ (r,t,e){let n=e!=null?e():y.now(),o;return(...i)=>{let a=e!=null?e():y.now();return a-n<t||(n=a,o=r(...i)),o}}function Oy(r){return r==="relu"?"relu":r==="linear"?"linear":r==="elu"?"elu":null}var YZ=0;function Py(){return YZ++}var My={};function Fu(r=""){return r in My||(My[r]=0),My[r]+=1,r+My[r].toString()}var Z $ =["channelsFirst","channelsLast"],J $ =["nearest","bilinear"],Q $ =["valid","same","causal"],tR=["max","avg"],eR=["sum","mul","concat","ave"];var Tm=new Map;function Me(r){da(Z $ ,"DataFormat",r)}function nR(r){da(J $ ,"InterpolationFormat",r)}function hn(r){da(Q $ ,"PaddingMode",r)}function LN(r){da(tR,"PoolMode",r)}var Nh=[],rR="/";function fi(r,t){Nh.push(r);try{let e=t();return Nh.pop(),e}catch(e){throw Nh.pop(),e}}function ZZ(){return Nh.length===0?"":Nh.join(rR)+rR}function Ly(r){if(!oR(r))throw new Error("Not a valid tensor name: '"+r+"'");return ZZ()+r}function zy(r){if(!oR(r))throw new Error("Not a valid tensor name: '"+r+"'");Tm.has(r)||Tm.set(r,0);let t=Tm.get(r);if(Tm.set(r,Tm.get(r)+1),t>0){let e= ` $ { r } _$ { t } ` ;return Tm.set(e,1),e}else return r}var JZ=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9 \. _ \/ ]* $ /);function oR(r){return!!r.match(JZ)}function sR(r){return r===parseInt(r.toString(),10)}function Ao(r,t,e){t==null&&(t=0),e==null&&(e=r.length);let n=1;for(let o=t;o<e;++o)n*=r[o];return n}function Dc(r){if(r.length===0)return Number.NaN;let t=Number.POSITIVE_INFINITY;for(let e=0;e<r.length;e++){let n=r[e];n<t&&(t=n)}return t}function di(r){if(r.length===0)return Number.NaN;let t=Number.NEGATIVE_INFINITY;for(let e=0;e<r.length;e++){let n=r[e];n>t&&(t=n)}return t}function gn(r,t){if(t<r)throw new z( ` end ( $ { t } ) < begin ( $ { r } ) is forbidden . ` );let e=[];for(let n=r;n<t;++n)e.push(n);return e}var zN;function cr(){return zN==null&&(zN=sx().epsilon()),zN}function xn(){return"channelsLast"}function rn(r,t){return J(r,t)}function Sl(r,t=-1){let e=r.shape.slice();return t<0&&(t=e.length+t+1),e.splice(t,0,1),R(r,e)}function iR(r,t){return B(()=>{if(r.shape.length!==2)throw new z( ` repeat ( ) expects a rank - 2 tensor , but received a rank - $ { r . shape . length } tensor . ` );let e=Sl(r,1);return Vy(e,[1,t,1])})}function aR(r){let t=[Ao(r.shape)];return R(r,t)}function lR(r){if(r.rank<=1)throw new z( ` batchFlatten requires a minimum rank of 2. Got rank : $ { r . rank } . ` );let t=[r.shape[0],Ao(r.shape,1)];return R(r,t)}function vl(r,t,e){return B(()=>{switch(r.rank){case 1:return xm(r,t,e);case 2:return gh(r,[t,0],[e,r.shape[1]]);case 3:return ym(r,[t,0,0],[e,r.shape[1],r.shape[2]]);case 4:return yc(r,[t,0,0,0],[e,r.shape[1],r.shape[2],r.shape[3]]);case 5:return Ot(r,[t,0,0,0,0],[e,r.shape[1],r.shape[2],r.shape[3],r.shape[4]]);
Received countWeights = $ { e . countWeights } ` );n=St(e.countWeights)}let o=Sr(t),s=gl(t),i=Re(this.numTokens,o).bufferSync().get(0),a=cn(s,0).bufferSync().get(0);if(!(i&&a))throw new z( ` Input values must be between 0 < values <= numTokens with numTokens = $ { this . numTokens } ` );return ZR(t,this.outputMode,this.numTokens,n)})}};Jf.className="CategoryEncoding";Q.registerClass(Jf);var FJ=["bilinear","nearest"],JR=new Set(FJ),Qf=class extends Et{constructor(t){if(super(t),this.height=t.height,this.width=t.width,t.interpolation)if(JR.has(t.interpolation))this.interpolation=t.interpolation;else throw new z( ` Invalid interpolation parameter : $ { t . interpolation } is not implemented ` );else this.interpolation="bilinear";this.cropToAspectRatio=!!t.cropToAspectRatio}computeOutputShape(t){t=Gt(t);let e=t[2];return[this.height,this.width,e]}getConfig(){let t={height:this.height,width:this.width,interpolation:this.interpolation,cropToAspectRatio:this.cropToAspectRatio},e=super.getConfig();return Object.assign(t,e),t}call(t,e){return B(()=>{let n=[this.height,this.width];if(this.interpolation==="bilinear")return fn.resizeBilinear(t,n,!this.cropToAspectRatio);if(this.interpolation==="nearest")return fn.resizeNearestNeighbor(t,n,!this.cropToAspectRatio);throw new Error( ` Interpolation is $ { this . interpolation } but only $ { [ ... JR ] } are supported ` )})}};Qf.className="Resizing";Q.registerClass(Qf);var Bh=class{constructor(t){this.seed=t}next(){if(this.seed!==void 0)return this.seed++}};Bh.className="RandomSeed";var Vh=class extends Et{constructor(t){super(t),this.randomGenerator=new Bh(t.seed)}getConfig(){let t={seed:this.randomGenerator.seed},e=super.getConfig();return Object.assign(t,e),t}};Vh.className="BaseRandomLayer";var OJ=["bilinear","nearest"],QR=new Set(OJ),td=class extends Vh{constructor(t){super(t);let{factor:e,interpolation:n="bilinear"}=t;if(this.factor=e,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 z( ` Invalid factor : $ { this . factor } . Must be positive number or tuple of 2 numbers ` );if(this.widthLower<-1||this.widthUpper<-1)throw new z( ` factor must have values larger than - 1. Got : $ { this . factor } ` );if(this.widthUpper<this.widthLower)throw new z( ` factor cannot have upper bound less than lower bound .
2024-01-17 22:56:53 +01:00
Got upper bound : $ { this . widthUpper } .
Got lower bound : $ { this . widthLower }
2024-09-10 17:30:23 +02:00
` );if(n)if(QR.has(n))this.interpolation=n;else throw new z( ` Invalid interpolation parameter : $ { n } is not implemented ` )}getConfig(){let t={factor:this.factor,interpolation:this.interpolation},e=super.getConfig();return Object.assign(t,e),t}computeOutputShape(t){t=Gt(t);let e=t[2];return[this.imgHeight,-1,e]}call(t,e){return B(()=>{let n=St(t);this.imgHeight=n.shape[n.shape.length-3];let o=n.shape[n.shape.length-2];this.widthFactor=Gn([1],1+this.widthLower,1+this.widthUpper,"float32",this.randomGenerator.next());let s=this.widthFactor.dataSync()[0]*o;s=Math.round(s);let i=[this.imgHeight,s];switch(this.interpolation){case"bilinear":return fn.resizeBilinear(t,i);case"nearest":return fn.resizeNearestNeighbor(t,i);default:throw new Error( ` Interpolation is $ { this . interpolation }
but only $ { [ ... QR ] } are supported ` )}})}};td.className="RandomWidth";Q.registerClass(td);function MJ(r){return new hi(r)}function PJ(r){return new tf(r)}function LJ(r){return new Zm(r)}function zJ(r){return new Jm(r)}function BJ(r){return new Qm(r)}function VJ(r){return new rf(r)}function GJ(r){return new ef(r)}function WJ(r){return new lf(r)}function UJ(r){return new Uc(r)}function HJ(r){return new of(r)}function qJ(r){return new Hc(r)}function KJ(r){return new sf(r)}function jJ(r){return new af(r)}function XJ(r){return new uf(r)}function YJ(r){return new cf(r)}function ZJ(r){return new pf(r)}function JJ(r){return new bf(r)}function QJ(r){return new xf(r)}function t9(r){return new Yc(r)}function e9(r){return new gf(r)}function r9(r){return new yf(r)}function n9(r){return new wf(r)}function o9(r){return new If(r)}function s9(r){return new Cf(r)}function i9(r){return new Sf(r)}function a9(r){return new Nf(r)}function l9(r){return new Tf(r)}function u9(r){return new Af(r)}function c9(r){return new _f(r)}function p9(r){return new Ef(r)}function m9(r){return new kf(r)}function f9(r){return new Df(r)}function d9(r){return new Of(r)}function h9(r){return new Mf(r)}function g9(r){return new Pf(r)}function ok(r){return new zf(r)}function x9(r){return ok(r)}function y9(r){return ok(r)}function sk(r){return new Vf(r)}function b9(r){return sk(r)}function w9(r){return sk(r)}function ik(r){return new Wf(r)}function I9(r){return ik(r)}function C9(r){return ik(r)}function v9(r){return new Uf(r)}function S9(r){return new qf(r)}function tF(r){return new Hf(r)}function eF(r){return new Kf(r)}function rF(r){return new Lf(r)}function nF(r){return new Bf(r)}function N9(r){return new Gf(r)}function k9(r){return new ff(r)}function T9(r){return new Kc(r)}function _9(r){return new df(r)}function E9(r){return new _l(r)}function A9(r){return new mf(r)}function D9(r){return new qc(r)}function $ 9(r){return new hf(r)}function R9(r){return new Xc(r)}function F9(r){return new po(r)}function O9(r){return new jc(r)}function M9(r){return new Xf(r)}function P9(r){return new jf(r)}var L9=tF,z9=eF,B9=rF,V9=nF;function G9(r){return new $ f(r)}function W9(r){return new Rf(r)}function U9(r){return new Ff(r)}function H9(r){return new vf(r)}function q9(r){return new Yf(r)}function K9(r){return new Zf(r)}function j9(r){return new Qf(r)}function X9(r){return new Jf(r)}function Y9(r){return new td(r)}var sF={};Kt(sF,{MAPE:()=>aQ,MSE:()=>cQ,binaryAccuracy:()=>Z9,binaryCrossentropy:()=>J9,categoricalAccuracy:()=>tQ,categoricalCrossentropy:()=>eQ,cosineProximity:()=>oQ,mape:()=>lQ,meanAbsoluteError:()=>sQ,meanAbsolutePercentageError:()=>iQ,meanSquaredError:()=>uQ,mse:()=>pQ,precision:()=>rQ,r2Score:()=>mQ,recall:()=>nQ,sparseCategoricalAccuracy:()=>Q9});function Z9(r,t){return $ h(r,t)}function J9(r,t){return rb(r,t)}function Q9(r,t){return nb(r,t)}function tQ(r,t){return Rh(r,t)}function eQ(r,t){return Fh(r,t)}function rQ(r,t){return qN(r,t)}function nQ(r,t){return TR(r,t)}function oQ(r,t){return Dh(r,t)}function sQ(r,t){return Hm(r,t)}function iQ(r,t){return Pu(r,t)}function aQ(r,t){return Pu(r,t)}function lQ(r,t){return Pu(r,t)}function uQ(r,t){return xa(r,t)}function cQ(r,t){return xa(r,t)}function pQ(r,t){return xa(r,t)}function mQ(r,t){return _R(r,t)}var iF={};Kt(iF,{modelFromJSON:()=>WR});var aF={};Kt(aF,{l1:()=>dQ,l1l2:()=>fQ,l2:()=>hQ});function fQ(r){return new Lu(r)}function dQ(r){return KR(r)}function hQ(r){return jR(r)}var zb=class extends kl{constructor(){super(...arguments),this.model=null}setModel(t){if(!(t instanceof Un))throw new Error("model must be a LayersModel, not some other Container");this.model=t}};function Lb(r,t){return r<t}function lF(r,t){return r>t}var Bb=class extends zb{constructor(t){if(super(),t==null&&(t={}),t.restoreBestWeights)throw new _t("restoreBestWeights = True is not implemented in EarlyStopping yet.");this.monitor=t.monitor||"val_loss",this.minDelta=Math.abs(t.minDelta||0),this.patience=t.patience||0,this.verbose=t.verbose||0,this.mode=t.mode||"auto",this.baseline=t.baseline,["auto","min","max"].indexOf(this.mode)===
2024-01-17 22:56:53 +01:00
because the value dtype is $ { e . dtype } , but TensorArray dtype is $ { this . dtype } . ` );if(this.size()===0&&(this.elementShape==null||this.elementShape.length===0)&&(this.elementShape=e.shape),Hn(this.elementShape,e.shape, ` TensorArray $ { this . name } : Could not write to TensorArray index $ { t } . ` ),n.read)throw new Error( ` TensorArray $ { this . name } : Could not write to TensorArray index $ { t } , because it has already been read . ` );if(n.written)throw new Error( ` TensorArray $ { this . name } : Could not write to TensorArray index $ { t } , because it has already been written . ` );n.tensor=e,De(e),n.written=!0,this.tensors[t]=n}writeMany(t,e){if(t.length!==e.length)throw new Error( ` TensorArray $ { this . name } : could not write multiple tensors , because the index size : $ { t . length } is not the same as tensors size : $ { e . length } . ` );t.forEach((n,o)=>this.write(n,e[o]))}gather(t,e){if(e&&e!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but gather requested dtype $ { e } ` );if(t)t=t.slice(0,this.size());else{t=[];for(let o=0;o<this.size();o++)t.push(o)}if(t.length===0)return ir([],[0].concat(this.elementShape));let n=this.readMany(t);return Hn(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),Fe(n,0)}concat(t){if(t&&t!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but concat requested dtype $ { t } ` );if(this.size()===0)return ir([],[0].concat(this.elementShape));let e=[];for(let o=0;o<this.size();o++)e.push(o);let n=this.readMany(e);return Hn(this.elementShape,n[0].shape, ` TensorArray shape mismatch : tensor array shape ( $ { this . elementShape } ) vs first tensor shape ( $ { n [ 0 ] . shape } ) ` ),ie(n,0)}scatter(t,e){if(e.dtype!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but tensor has dtype $ { e . dtype } ` );if(t.length!==e.shape[0])throw new Error( ` Expected len ( indices ) == tensor . shape [ 0 ] , but saw : $ { t . length } vs . $ { e . shape [ 0 ] } ` );let n=Math.max(...t);if(!this.dynamicSize&&n>=this.maxSize)throw new Error( ` Max index must be < array size ( $ { n } vs . $ { this . maxSize } ) ` );this.writeMany(t,gr(e,0))}split(t,e){if(e.dtype!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but tensor has dtype $ { e . dtype } ` );let n=0,o=t.map(u=>(n+=u,n));if(n!==e.shape[0])throw new Error( ` Expected sum of lengths to be equal to
tensor . shape [ 0 ] , but sum of lengths is
2024-09-10 17:30:23 +02:00
$ { n } , and tensor 's shape is: ${e.shape}`);if(!this.dynamicSize&&t.length!==this.maxSize)throw new Error(`TensorArray' s size is not equal to the size of lengths ( $ { this . maxSize } vs . $ { t . length } ) , and the TensorArray is not marked as dynamically resizeable ` );let s=n===0?0:e.size/n,i=[];B(()=>{e=R(e,[1,n,s]);for(let u=0;u<t.length;++u){let c=[0,u===0?0:o[u-1],0],p=[1,t[u],s];i[u]=R(Ot(e,c,p),this.elementShape)}return i});let a=[];for(let u=0;u<t.length;u++)a[u]=u;this.writeMany(a,i)}};var rd=class r{get id(){return this.idTensor.id}constructor(t,e,n,o=-1){this.tensors=t,this.elementShape=e,this.elementDtype=n,t!=null&&t.forEach(s=>{if(n!==s.dtype)throw new Error( ` Invalid data types ; op elements $ { n } , but list elements $ { s . dtype } ` );Hn(e,s.shape,"TensorList shape mismatch: "),De(s)}),this.idTensor=pt(0),this.maxNumElements=o,De(this.idTensor)}copy(){return new r([...this.tensors],this.elementShape,this.elementDtype)}clearAndClose(t){this.tensors.forEach(e=>{(t==null||!t.has(e.id))&&e.dispose()}),this.tensors.length=0,this.idTensor.dispose()}size(){return this.tensors.length}stack(t,e,n=-1){if(e!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { e } , but list elements $ { this . elementDtype } ` );if(n!==-1&&this.tensors.length!==n)throw new Error( ` Operation expected a list with $ { n } elements but got a list with $ { this . tensors . length } elements . ` );Hn(t,this.elementShape,"TensorList shape mismatch: ");let o=ed(this.elementShape,this.tensors,t);return B(()=>{let s=this.tensors.map(i=>R(i,o));return Fe(s,0)})}popBack(t,e){if(e!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { e } , but list elements $ { this . elementDtype } ` );if(this.size()===0)throw new Error("Trying to pop from an empty list.");let n=ed(this.elementShape,this.tensors,t),o=this.tensors.pop();return o.kept=!1,Hn(o.shape,t,"TensorList shape mismatch: "),R(o,n)}pushBack(t){if(t.dtype!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { t . dtype } , but list elements $ { this . elementDtype } ` );if(Hn(t.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");De(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 e=new r([],this.elementShape,this.elementDtype,this.maxNumElements);e.tensors.length=t;for(let n=0;n<Math.min(this.tensors.length,t);++n)e.tensors[n]=this.tensors[n];return e}getItem(t,e,n){if(n!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { n } , 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 . ` );Hn(this.tensors[t].shape,e,"TensorList shape mismatch: ");let o=ed(this.elementShape,this.tensors,e);return R(this.tensors[t],o)}setItem(t,e){if(e.dtype!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { e . 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 . ` );Hn(this.elementShape,e.shape,"TensorList shape mismatch: "),De(e),this.tensors[t]!=null&&(this.tensors[t].kept=!1),this.tensors[t]=e}gather(t,e,n){if(e!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { e } , but list elements $ { this . elementDtype } ` );Hn(this.elementShape,n,"TensorList shape mismatch: "),t=t.slice(0,this.size());let o=ed(this.elementShape,this.tensors,n);return t.length===0?ir([],[0].concat(o)):B(()=>{let s=t.map(i=>R(this.tensors[i],o));return Fe(s,0)})}concat(t,e){if(t&&t!==this.elementDtype)throw new Error( ` TensorList dtype is $ { this . elementDtype } but concat requested dtype $ { t } ` );Hn(this.elementShape,e,"TensorList shape mismatch: ");let n=ed(t
2024-01-17 22:56:53 +01:00
tensor . shape [ 0 ] , but sum of lengths is
2024-09-10 17:30:23 +02:00
$ { n } , and tensor ' s shape is : $ { r . shape } ` );let s=r.shape.slice(1),i=tw(s,e),a=n===0?0:r.size/n,u=B(()=>{let c=[];r=R(r,[1,n,a]);for(let p=0;p<t.length;++p){let f=[0,p===0?0:o[p-1],0],d=[1,t[p],a];c[p]=R(Ot(r,f,d),i)}return r.dispose(),c}),l=new rd([],e,r.dtype,t.length);for(let c=0;c<u.length;c++)l.setItem(c,u[c]);return l}var wF=async(r,t,e)=>{switch(r.op){case"If":case"StatelessIf":{let n=v("thenBranch",r,t,e),o=v("elseBranch",r,t,e),s=v("cond",r,t,e),i=v("args",r,t,e);return(await s.data())[0]?e.functionMap[n].executeFunctionAsync(i,e.tensorArrayMap,e.tensorListMap):e.functionMap[o].executeFunctionAsync(i,e.tensorArrayMap,e.tensorListMap)}case"While":case"StatelessWhile":{let n=v("body",r,t,e),o=v("cond",r,t,e),s=v("args",r,t,e),i=await e.functionMap[o].executeFunctionAsync(s,e.tensorArrayMap,e.tensorListMap),a=s.map(c=>c.id),u=await i[0].data();i.forEach(c=>{!c.kept&&a.indexOf(c.id)===-1&&c.dispose()});let l=s;for(;u[0];){let c=l;l=await e.functionMap[n].executeFunctionAsync(l,e.tensorArrayMap,e.tensorListMap);let p=l.map(f=>f.id);c.forEach(f=>{!f.kept&&a.indexOf(f.id)===-1&&p.indexOf(f.id)===-1&&f.dispose()});let m=await e.functionMap[o].executeFunctionAsync(l,e.tensorArrayMap,e.tensorListMap);u=await m[0].data(),m.forEach(f=>{!f.kept&&a.indexOf(f.id)===-1&&p.indexOf(f.id)===-1&&f.dispose()})}return l}case"LoopCond":{let n=v("pred",r,t,e);return[wi(n)]}case"Switch":{let n=v("pred",r,t,e),o=v("data",r,t,e);return o.kept||(o=wi(o)),(await n.data())[0]?[void 0,o]:[o,void 0]}case"Merge":{let n=r.inputNames.find(o=>pr(o,t,e)!==void 0);if(n){let o=pr(n,t,e);return[wi(o)]}return}case"Enter":{let n=v("frameName",r,t,e),o=v("tensor",r,t,e);return e.enterFrame(n),[wi(o)]}case"Exit":{let n=v("tensor",r,t,e);return e.exitFrame(),[wi(n)]}case"NextIteration":{let n=v("tensor",r,t,e);return e.nextIteration(),[wi(n)]}case"TensorArrayV3":{let n=v("size",r,t,e),o=v("dtype",r,t,e),s=v("elementShape",r,t,e),i=v("dynamicSize",r,t,e),a=v("clearAfterRead",r,t,e),u=v("identicalElementShapes",r,t,e),l=v("name",r,t,e),c=new ew(l,o,n,s,u,i,a);return e.addTensorArray(c),[c.idTensor,pt(1)]}case"TensorArrayWriteV3":{let n=v("tensorArrayId",r,t,e),o=v("index",r,t,e),s=v("tensor",r,t,e),i=e.getTensorArray(n.id);return i.write(o,s),[i.idTensor]}case"TensorArrayReadV3":{let n=v("tensorArrayId",r,t,e),o=v("index",r,t,e);return[e.getTensorArray(n.id).read(o)]}case"TensorArrayGatherV3":{let n=v("tensorArrayId",r,t,e),o=v("indices",r,t,e),s=v("dtype",r,t,e);return[e.getTensorArray(n.id).gather(o,s)]}case"TensorArrayScatterV3":{let n=v("tensorArrayId",r,t,e),o=v("indices",r,t,e),s=v("tensor",r,t,e),i=e.getTensorArray(n.id);return i.scatter(o,s),[i.idTensor]}case"TensorArrayConcatV3":{let n=v("tensorArrayId",r,t,e),o=e.getTensorArray(n.id),s=v("dtype",r,t,e);return[o.concat(s)]}case"TensorArraySplitV3":{let n=v("tensorArrayId",r,t,e),o=v("tensor",r,t,e),s=v("lengths",r,t,e),i=e.getTensorArray(n.id);return i.split(s,o),[i.idTensor]}case"TensorArraySizeV3":{let n=v("tensorArrayId",r,t,e),o=e.getTensorArray(n.id);return[pt(o.size(),"int32")]}case"TensorArrayCloseV3":{let n=v("tensorArrayId",r,t,e),o=e.getTensorArray(n.id);return o.clearAndClose(),[o.idTensor]}case"TensorListSetItem":{let n=v("tensorListId",r,t,e),o=v("index",r,t,e),s=v("tensor",r,t,e),i=e.getTensorList(n.id);return i.setItem(o,s),[i.idTensor]}case"TensorListGetItem":{let n=v("tensorListId",r,t,e),o=v("index",r,t,e),s=v("elementShape",r,t,e),i=v("elementDType",r,t,e);return[e.getTensorList(n.id).getItem(o,s,i)]}case"TensorListScatterV2":case"TensorListScatter":{let n=v("indices",r,t,e),o=v("tensor",r,t,e),s=v("elementShape",r,t,e),i=v("numElements",r,t,e),a=yF(o,n,s,i);return e.addTensorList(a),[a.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let n=v("elementShape",r,t,e),o=v("elementDType",r,t,e),s;r.op==="TensorListReserve"?s="numElements":s="maxNumElements";let i=v(s,r,t,e),a=r.op==="TensorListReserve"?-1:i,u=xF(n,o,i,a);return e.addTensorList(u),[u.idTensor]}case"TensorListGather":{let n=v("tensorListId",r,t,e),o=v("indices",r,t,e),s=v("elementShape",r,t,e),i=v("element
$ { t } ` );let o;return this.size===1/0||this.size==null?o=this.size:e?o=Math.ceil(this.size/t):o=Math.floor(this.size/t),En(async()=>(await n.iterator()).columnMajorBatch(t,e,Ctt),o)}concatenate(t){let e=this,n;return this.size===1/0||t.size===1/0?n=1/0:this.size!=null&&t.size!=null?n=this.size+t.size:n=null,En(async()=>(await e.iterator()).concatenate(await t.iterator()),n)}filter(t){let e=this,n;return this.size===1/0?n=1/0:n=null,En(async()=>(await e.iterator()).filter(o=>B(()=>t(o))),n)}async forEachAsync(t){return(await this.iterator()).forEachAsync(t)}map(t){let e=this;return En(async()=>(await e.iterator()).map(n=>B(()=>t(n))),this.size)}mapAsync(t){let e=this;return En(async()=>(await e.iterator()).mapAsync(t),this.size)}prefetch(t){if(t==null)throw new RangeError(" ` Dataset . prefetch ( ) ` requires bufferSize to be specified.");let e=this;return En(async()=>(await e.iterator()).prefetch(t),this.size)}repeat(t){let e=this,n;return this.size!=null&&t>0?n=this.size*t:t===0?n=0:this.size!=null&&(t===void 0||t<0)?n=1/0:n=null,En(async()=>{let o=jh(async()=>({value:await e.iterator(),done:!1}));return jF(o.take(t))},n)}skip(t){let e=this,n;return this.size!=null&&t>=0&&this.size>=t?n=this.size-t:this.size!=null&&(this.size<t||t===void 0||t<0)?n=0:n=null,En(async()=>(await e.iterator()).skip(t),n)}shuffle(t,e,n=!0){if(t==null||t<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 o = this , s = YF . alea ( e || y . now ( ) . toString ( ) ) ; return En ( async ( ) => { let i = s . int32 ( ) ; return n && ( i += s . int32 ( ) ) , ( await o . iterator ( ) ) . shuffle ( t , i . toString ( ) ) } , this . size ) } take ( t ) { let e = this , n ; return this . size != null && this . size > t ? n = t : this . size != null && this . size <= t ? n = this . size : n = null , En ( async ( ) => ( await e . iterator ( ) ) . take ( t ) , 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 ( ) } } ; Ii . MAX _BUFFER _SIZE = 1e4 ; function En ( r , t = null ) { return new class extends Ii { constructor ( ) { super ( ... arguments ) , this . size = t } async iterator ( ) { return r ( ) } } } function ZF ( r ) { return En ( async ( ) => qk ( r ) , r . length ) } function JF ( r ) { if ( ! Vu ( r ) ) throw new Error ( "The argument to zip() must be an object or array." ) ; let t ; if ( Array . isArray ( r ) ) for ( let e = 0 ; e < r . length ; e ++ ) t = t == null ? r [ e ] . size : Math . min ( t , r [ e ] . size ) ; else if ( r instanceof Object ) for ( let e in r ) t = t == null ? r [ e ] . size : Math . min ( t , r [ e ] . size ) ; return En ( async ( ) => { let e = await sw ( r , n => { if ( n instanceof Ii ) return { value : n . iterator ( ) , recurse : ! 1 } ; if ( Vu ( n ) ) return { value : null , recurse : ! 0 } ; throw new Error ( "Leaves of the structure passed to zip() must be Datasets, not primitives." ) } ) ; return XF ( e , Dl . SHORTEST ) } , t ) } function Ctt ( r ) { if ( r === null ) return null ; let t = r [ 0 ] ; return HF ( t ) ? { value : vtt ( r ) , recurse : ! 1 } : { value : null , recurse : ! 0 } } function vtt ( r ) { if ( r . length === 0 ) throw new Error ( "Can't make a batch of zero elements." ) ; return r [ 0 ] instanceof Lt ? Fe ( r ) : ir ( r ) } var sd = class extends Ii { constructor ( t ) { super ( ) , this . input = t } async iterator ( ) { return ( await this . input . iterator ( ) ) . decodeUTF8 ( ) . split ( `
` ).map(o=>(o.endsWith(" \r ")&&(o=o.slice(0,-1)),o))}};var uw='"',Xh=Symbol("out"),QF=Symbol("field"),cw=Symbol("quote"),Kk=Symbol("quoteafterquote"),tO=Symbol("quoteinquote"),id=class extends Ii{async columnNames(){return this.columnNamesValidated||await this.setColumnNames(),this.configuredColumnsOnly?Object.keys(this.columnConfigs):this.fullColumnNames}async setColumnNames(){let t=await this.maybeReadHeaderLine();if(!this.fullColumnNames&&!t)throw new Error("Column names must be provided if there is no header line.");this.fullColumnNames&&t&&y.assert(t.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 ("+t.length.toString()+")."),this.fullColumnNames||(this.fullColumnNames=t);let e=this.fullColumnNames.reduce((o,s)=>(o[s]=o[s]+1||1,o),{}),n=Object.keys(e).filter(o=>e[o]>1);if(y.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let o of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(o)===-1)throw new Error('The key "'+o+'" 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 n=e.value;return this.parseRow(n,!1)}else return null}constructor(t,e){super(),this.input=t,this.hasHeader=!0,this.fullColumnNames=null,this.columnNamesValidated=!1,this.columnConfigs=null,this.configuredColumnsOnly=!1,this.delimiter=",",this.delimWhitespace=!1,this.base=new sd(t),e||(e={}),this.hasHeader=e.hasHeader!==!1,this.fullColumnNames=e.columnNames,this.columnConfigs=e.columnConfigs,this.configuredColumnsOnly=e.configuredColumnsOnly,e.delimWhitespace?(y.assert(e.delimiter==null,()=>"Delimiter should not be provided when delimWhitespace is true."),this.delimWhitespace=!0,this.delimiter=" "):this.delimiter=e.delimiter?e.delimiter:","}async iterator(){this.columnNamesValidated||await this.setColumnNames();let t=await this.base.iterator();return this.hasHeader&&(t=t.skip(1)),t.map(e=>this.makeDataElement(e))}makeDataElement(t){let e=this.parseRow(t),n={},o={};for(let s=0;s<this.fullColumnNames.length;s++){let i=this.fullColumnNames[s],a=this.columnConfigs?this.columnConfigs[i]:null;if(!(this.configuredColumnsOnly&&!a)){let u=e[s],l=null;if(u==="")if(a&&a.default!==void 0)l=a.default;else{if(a&&(a.required||a.isLabel))throw new Error( ` Required column $ { i } is empty in this line : $ { t } ` );l=void 0}else{let c=Number(u);if(isNaN(c))a&&a.dtype==="bool"?l=this.getBoolean(u):l=u;else if(!a||!a.dtype)l=c;else switch(a.dtype){case"float32":l=c;break;case"int32":l=Math.floor(c);break;case"bool":l=this.getBoolean(u);break;default:l=c}}a&&a.isLabel?o[i]=l:n[i]=l}}return Object.keys(o).length===0?n:{xs:n,ys:o}}getBoolean(t){return t==="1"||t.toLowerCase()==="true"?1:0}parseRow(t,e=!0){let n=[],o=0,s=t.length,i=Xh;for(let a=0;a<s;a++)switch(i){case Xh:switch(t.charAt(a)){case uw:o=a+1,i=cw;break;case this.delimiter:if(o=a+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),i=Xh;break;default:i=QF,o=a;break}break;case QF:switch(t.charAt(a)){case this.delimiter:n.push(t.substring(o,a)),i=Xh,o=a+1;break;default:}break;case cw:switch(t.charAt(a)){case uw:i=Kk;break;default:}break;case Kk:switch(t.charAt(a)){case this.delimiter:n.push(t.substring(o,a-1)),i=Xh,o=a+1;break;case uw:i=cw;break;default:i=tO;break}break;case tO:switch(t.charAt(a)){case uw:i=cw;break;default:}break;default:}if(i===Kk?n.push(t.substring(o,s-1)):n.push(t.substring(o)),e&&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}};var pw=class r extends er{constructor(t){super(),this.microphoneConfig=t,this.isClosed=!1,this.fftSize=t.fftSize||1024;let e=Math.log2(this.fftSize);if(this.fftSize<0||e<4||e>14||!Number.isInteger(e))throw new Error( ` Inv
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
=== === === === === === === === === = ` ));let o={id:this.nextDataId()};return this.data.set(o,{values:t,dtype:n,refCount:1}),o}makeTensorInfo(t,e,n){let o;if(e==="string"&&n!=null&&n.length>0&&y.isString(n[0])){let s=n.map(i=>y.encodeString(i));o=this.write(s,t,e)}else o=this.write(n,t,e);return{dataId:o,shape:t,dtype:e}}refCount(t){return this.data.has(t)?this.data.get(t).refCount:0}incRef(t){let e=this.data.get(t);e.refCount++}decRef(t){if(this.data.has(t)){let e=this.data.get(t);e.refCount--}}move(t,e,n,o,s){this.data.set(t,{values:e,dtype:o,refCount:s})}numDataIds(){return this.data.numDataIds()}async read(t){return this.readSync(t)}readSync(t){let{dtype:e,complexTensorInfos:n}=this.data.get(t);if(e==="complex64"){let o=this.readSync(n.real.dataId),s=this.readSync(n.imag.dataId);return S.mergeRealAndImagArrays(o,s)}return y.convertBackendValuesAndArrayBuffer(this.data.get(t).values,e)}bufferSync(t){let e=this.readSync(t.dataId);if(t.dtype==="string")try{let n=e.map(o=>y.decodeString(o));return wt(t.shape,t.dtype,n)}catch(n){throw new Error("Failed to decode encoded string bytes into utf-8")}return wt(t.shape,t.dtype,e)}makeOutput(t,e,n){return Bn().makeTensorFromTensorInfo(this.makeTensorInfo(e,n,t),this)}disposeData(t,e=!1){if(this.data.has(t)){if(this.data.get(t).refCount--,!e&&this.data.get(t).refCount>0)return!1;let{complexTensorInfos:n}=this.data.get(t);n!=null&&(this.disposeData(n.real.dataId,!0),this.disposeData(n.imag.dataId,!0)),this.data.delete(t)}return!0}disposeIntermediateTensorInfo(t){this.disposeData(t.dataId)}async time(t){let e=y.now();return t(),{kernelMs:y.now()-e}}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){tt([t],"where");let e=this.readSync(t.dataId);return Ntt(t.shape,e)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}};pd.nextDataId=0;var Ew={};Kt(Ew,{addImpl:()=>eT,bincountImpl:()=>dd,bincountReduceImpl:()=>gw,bitwiseAndImpl:()=>rT,castImpl:()=>tT,ceilImpl:()=>nT,concatImpl:()=>Jc,equalImpl:()=>oT,expImpl:()=>iT,expm1Impl:()=>lT,floorDivImpl:()=>cT,floorImpl:()=>uT,gatherNdImpl:()=>xw,gatherV2Impl:()=>yw,greaterEqualImpl:()=>mT,greaterImpl:()=>pT,lessEqualImpl:()=>dT,lessImpl:()=>fT,linSpaceImpl:()=>bw,logImpl:()=>hT,maxImpl:()=>ww,maximumImpl:()=>gT,minimumImpl:()=>xT,multiplyImpl:()=>Zh,negImpl:()=>yT,notEqualImpl:()=>bT,prodImpl:()=>wT,raggedGatherImpl:()=>Iw,raggedRangeImpl:()=>Cw,raggedTensorToTensorImpl:()=>vw,rangeImpl:()=>tp,rsqrtImpl:()=>CT,scatterImpl:()=>Ci,sigmoidImpl:()=>LO,simpleAbsImpl:()=>Qk,sliceImpl:()=>ep,sparseFillEmptyRowsImpl:()=>Sw,sparseReshapeImpl:()=>Nw,sparseSegmentReductionImpl:()=>gd,sqrtImpl:()=>VO,squaredDifferenceImpl:()=>ST,staticRegexReplaceImpl:()=>NT,stridedSliceImpl:()=>kw,stringNGramsImpl:()=>rp,stringSplitImpl:()=>np,stringToHashBucketFastImpl:()=>op,subImpl:()=>TT,tileImpl:()=>Tw,topKImpl:()=>_w,transposeImpl:()=>hd,uniqueImpl:()=>sp});function Qk(r){let t=new Float32Array(r.length);for(let e=0;e<r.length;++e)t[e]=Math.abs(r[e]);return t}var ktt=r=>{let{x:t}=r.inputs,e=r.backend;tt(t,"abs");let n=new Float32Array(y.sizeFromShape(t.shape)),o=e.data.get(t.dataId).values;return n=Qk(o),e.makeOutput(n,t.shape,t.dtype)},lO={kernelName:Ai,backendName:"cpu",kernelFunc:ktt};function Qt(r){return(t,e,n,o,s)=>{let i=S.assertAndGetBroadcastShape(t,e),a=i.length,u=y.computeStrides(i),l=y.sizeFromShape(i),c=y.getTypedArrayFromDType(s,l),p=t.length,m=e.length,f=y.computeStrides(t),d=y.computeStrides(e),h=S.getBroadcastDims(t,i),g=S.getBroadcastDims(e,i);if(h.length+g.length===0)for(let x=0;x<c.length;++x)c[x]=r(n[x%n.length],o[x%o.length]);else for(let x=0;x<c.length;++x){let b=y.indexToLoc(x,a,u),w=b.slice(-p);h.forEach(A=>w[A]=0);let I=y.locToIndex(w,p,f),N=b.slice(-m);g.forEach(A=>N[A]=0);let E=y.locToIndex(N,m,d);c[x]=r(n[I],o[E])}return[c,i]}}function Ir(r){let{inputs:t,backend:e}=r,{real:n,imag:o}=t,s=e.data.get(n.dataId).values,i=e.data.get(o.dataId).values,a=e.makeTensorInfo(n.shape,"complex64"),u=e.data.get(a.dataId);return
2024-01-17 22:56:53 +01:00
$ { s . shape } ` );if(n.shape.length!==2)throw new Error( ` Indices must be a matrix , saw :
$ { n . shape } ` );if(o.shape.length!==1)throw new Error( ` Values must be a vector , saw :
$ { o . shape } ` );if(i.shape.length!==0)throw new Error( ` Default value must be a scalar , saw :
2024-09-10 17:30:23 +02:00
$ { i . shape } ` );let a=e.data.get(n.dataId).values,u=e.data.get(o.dataId).values,l=e.data.get(s.dataId).values,c=e.data.get(i.dataId).values[0],[p,m,f,d,h]=Sw(a,n.shape,n.dtype,u,o.dtype,l,c);return[e.makeTensorInfo(m,n.dtype,p),e.makeTensorInfo([m[0]],o.dtype,f),e.makeTensorInfo([d.length],"bool",new Uint8Array(d.map(g=>Number(g)))),e.makeTensorInfo([h.length],n.dtype,new Int32Array(h))]}var uL={kernelName:nu,backendName:"cpu",kernelFunc:dnt};function hnt(r){let{inputs:t,backend:e}=r,{inputIndices:n,inputShape:o,newShape:s}=t;if(n.shape.length!==2)throw new Error( ` Input indices should be a matrix but received shape
2024-01-17 22:56:53 +01:00
$ { n . shape } ` );if(o.shape.length!==1)throw new Error( ` Input shape should be a vector but received shape
2024-09-10 17:30:23 +02:00
$ { o . shape } ` );if(s.shape.length!==1)throw new Error( ` Target shape should be a vector but received shape $ { s . shape } ` );let i=Array.from(e.data.get(o.dataId).values),a=e.data.get(n.dataId).values,u=Array.from(e.data.get(s.dataId).values),[l,c,p]=Nw(a,n.shape,n.dtype,i,u);return[e.makeTensorInfo(c,n.dtype,l),e.makeTensorInfo([p.length],s.dtype,new Int32Array(p))]}var cL={kernelName:il,backendName:"cpu",kernelFunc:hnt};function gnt(r){let{inputs:t,backend:e}=r,{data:n,indices:o,segmentIds:s}=t;if(n.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(o.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
2024-01-17 22:56:53 +01:00
$ { o . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
2024-09-10 17:30:23 +02:00
$ { s . shape } ` );if(o.shape[0]!==s.shape[0])throw new Error("segmentIds and indices should have same size.");let i=e.data.get(n.dataId).values,a=e.data.get(o.dataId).values,u=e.data.get(s.dataId).values,[l,c]=gd(i,n.shape,n.dtype,a,u,!0);return e.makeTensorInfo(c,n.dtype,l)}var pL={kernelName:ou,backendName:"cpu",kernelFunc:gnt};function xnt(r){let{inputs:t,backend:e}=r,{data:n,indices:o,segmentIds:s}=t;if(n.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(o.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
2024-01-17 22:56:53 +01:00
$ { o . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
2024-09-10 17:30:23 +02:00
$ { s . shape } ` );if(o.shape[0]!==s.shape[0])throw new Error("segmentIds and indices should have same size.");let i=e.data.get(n.dataId).values,a=e.data.get(o.dataId).values,u=e.data.get(s.dataId).values,[l,c]=gd(i,n.shape,n.dtype,a,u);return e.makeTensorInfo(c,n.dtype,l)}var mL={kernelName:su,backendName:"cpu",kernelFunc:xnt};function ynt(r){let{inputs:t,backend:e,attrs:n}=r,{sparseIndices:o,sparseValues:s,defaultValue:i}=t,{outputShape:a}=n,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:m}=S.calculateShapes(s,o,a),f=!1,d=e.bufferSync(o),h;switch(s.dtype){case"bool":{let g=e.bufferSync(s),x=!!e.data.get(i.dataId).values[0];h=Ci(d,g,a,m,c,l,u,p,x,f);break}case"float32":{let g=e.bufferSync(s),x=e.data.get(i.dataId).values[0];h=Ci(d,g,a,m,c,l,u,p,x,f);break}case"int32":{let g=e.bufferSync(s),x=e.data.get(i.dataId).values[0];h=Ci(d,g,a,m,c,l,u,p,x,f);break}case"string":{let g=e.bufferSync(s),x=y.decodeString(e.data.get(i.dataId).values[0]);h=Ci(d,g,a,m,c,l,u,p,x,f);break}default:throw new Error( ` Unsupported type $ { s . dtype } ` )}return e.makeTensorInfo(a,h.dtype,h.values)}var fL={kernelName:al,backendName:"cpu",kernelFunc:ynt};function bnt(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{numOrSizeSplits:s,axis:i}=n,a=y.parseAxisParam(i,o.shape)[0],u=S.prepareSplitSize(o,s,a),l=new Array(o.shape.length).fill(0),c=o.shape.slice();return u.map(p=>{let m=[...c];m[a]=p;let f=Mo({inputs:{x:o},backend:e,attrs:{begin:l,size:m}});return l[a]+=p,f})}var dL={kernelName:qi,backendName:"cpu",kernelFunc:bnt};var hL={kernelName:iu,backendName:"cpu",kernelFunc:({inputs:r,backend:t})=>{let{x:e}=r,n=t;tt(e,"square");let o=n.data.get(e.dataId).values,s=new Float32Array(o.length);for(let a=0;a<o.length;++a){let u=o[a];s[a]=u*u}return{dataId:n.write(s,e.shape,e.dtype),shape:e.shape,dtype:e.dtype}}};var wnt=At(xo,(r,t)=>{let e=t;return isNaN(r)?NaN:r>0?1:e.alpha}),gL={kernelName:xo,backendName:"cpu",kernelFunc:wnt};function Int(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{begin:s,end:i,strides:a,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:m}=n;tt(o,"stridedSlice");let{finalShapeSparse:f,finalShape:d,isIdentity:h,sliceDim0:g,isSimpleSlice:x,begin:b,end:w,strides:I}=Be.sliceInfo(o.shape,s,i,a,u,l,c,p,m),N;if(h)N=te({inputs:{x:o},backend:e,attrs:{shape:d}});else if(g||x){y.assert(o.shape.length>=1,()=> ` Input must have rank at least 1 , got : $ { o . shape . length } ` );let E=Be.computeOutShape(b,w,I),A=Mo({inputs:{x:o},backend:e,attrs:{begin:b,size:E}});N=te({inputs:{x:A},backend:e,attrs:{shape:d}}),e.disposeIntermediateTensorInfo(A)}else{let E=e.bufferSync(o),A=kw(f,E,I,b);N=e.makeTensorInfo(d,A.dtype,A.values)}return N}var xL={kernelName:ll,backendName:"cpu",kernelFunc:Int};function Cnt(r){let{inputs:t,backend:e,attrs:n}=r,{separator:o,nGramWidths:s,leftPad:i,rightPad:a,padWidth:u,preserveShortSequences:l}=n,{data:c,dataSplits:p}=t,m=e.data.get(c.dataId).values,f=e.data.get(p.dataId).values,[d,h]=rp(m,f,o,s,i,a,u,l);return[e.makeTensorInfo([d.length],"string",d),e.makeTensorInfo(p.shape,"int32",h)]}var yL={kernelName:au,backendName:"cpu",kernelFunc:Cnt};function vnt(r){let{inputs:t,backend:e,attrs:n}=r,{skipEmpty:o}=n,{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 a=e.data.get(s.dataId).values,u=e.data.get(i.dataId).values[0],[l,c,p]=np(a,u,o),m=c.length;return[e.makeTensorInfo([m,2],"int32",l),e.makeTensorInfo([m],"string",c),e.makeTensorInfo([2],"int32",new Int32Array(p))]}var bL={kernelName:lu,backendName:"cpu",kernelFunc:vnt};function Snt(r){let{inputs:t,backend:e,attrs:n}=r,{numBuckets:o}=n,{input:s}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");if(o<=0)throw new Error("Number of buckets must be at least 1");let i=e.data.get(s.dataId).values,a=op(i,o);return e.makeTensorInfo(s.shape,"int32",a)}var wL={kernelName:uu,backendName:"cpu",kernelFunc:Snt};var Nnt=At(r
2024-01-17 22:56:53 +01:00
` ),s=o.length.toString().length+2,i=o.map((p,m)=>y.rightPad((m+1).toString(),s)+p),a=0;for(let p=0;p<i.length;p++)a=Math.max(i[p].length,a);let u=i.slice(0,n-1),l=i.slice(n-1,n),c=i.slice(n);console.log(u.join( `
` )),console.log(t.split( `
` )[0]),console.log( ` % c $ { y . rightPad ( l [ 0 ] , a ) } ` ,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(c.join( `
2024-09-10 17:30:23 +02:00
` ))}function qT(r){return Rl(r,()=>r.createProgram(),"Unable to create WebGLProgram.")}function KT(r,t){if(ht(r,()=>r.linkProgram(t)),!L().get("ENGINE_COMPILE_ONLY")&&r.getProgramParameter(t,r.LINK_STATUS)===!1)throw console.log(r.getProgramInfoLog(t)),new Error("Failed to link vertex and fragment shaders.")}function ag(r,t){if(ht(r,()=>r.validateProgram(t)),r.getProgramParameter(t,r.VALIDATE_STATUS)===!1)throw console.log(r.getProgramInfoLog(t)),new Error("Shader program validation failed.")}function jT(r,t){let e=Rl(r,()=>r.createBuffer(),"Unable to create WebGLBuffer");return ht(r,()=>r.bindBuffer(r.ARRAY_BUFFER,e)),ht(r,()=>r.bufferData(r.ARRAY_BUFFER,t,r.STATIC_DRAW)),e}function XT(r,t){let e=Rl(r,()=>r.createBuffer(),"Unable to create WebGLBuffer");return ht(r,()=>r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,e)),ht(r,()=>r.bufferData(r.ELEMENT_ARRAY_BUFFER,t,r.STATIC_DRAW)),e}function Knt(){return L().getNumber("WEBGL_VERSION")===2?1:4}function YT(r){return Rl(r,()=>r.createTexture(),"Unable to create WebGLTexture.")}function ZT(r,t){let e=L().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(r<=0||t<=0){let n= ` [ $ { r } x$ { t } ] ` ;throw new Error("Requested texture size "+n+" is invalid.")}if(r>e||t>e){let n= ` [ $ { r } x$ { t } ] ` ,o= ` [ $ { e } x$ { e } ] ` ;throw new Error("Requested texture size "+n+" greater than WebGL maximum on this browser / GPU "+o+".")}}function JT(r){return Rl(r,()=>r.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function zw(r,t,e,n,o,s,i){let a=r.getAttribLocation(t,e);return a===-1?!1:(ht(r,()=>r.bindBuffer(r.ARRAY_BUFFER,n)),ht(r,()=>r.vertexAttribPointer(a,o,r.FLOAT,!1,s,i)),ht(r,()=>r.enableVertexAttribArray(a)),!0)}function FL(r,t,e){ML(r,e),ht(r,()=>r.activeTexture(r.TEXTURE0+e)),ht(r,()=>r.bindTexture(r.TEXTURE_2D,t))}function jnt(r,t){ML(r,t),ht(r,()=>r.activeTexture(r.TEXTURE0+t)),ht(r,()=>r.bindTexture(r.TEXTURE_2D,null))}function QT(r,t,e){return Rl(r,()=>r.getUniformLocation(t,e),'uniform "'+e+'" not present in program.')}function t1(r,t,e){return r.getUniformLocation(t,e)}function e1(r,t,e,n){ht(r,()=>FL(r,t,n)),ht(r,()=>r.uniform1i(e,n))}function Xnt(r){ht(r,()=>r.bindFramebuffer(r.FRAMEBUFFER,null)),ht(r,()=>r.viewport(0,0,r.canvas.width,r.canvas.height)),ht(r,()=>r.scissor(0,0,r.canvas.width,r.canvas.height))}function lg(r,t,e){ht(r,()=>r.bindFramebuffer(r.FRAMEBUFFER,e)),ht(r,()=>r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,t,0))}function Bw(r,t){ht(r,()=>r.bindFramebuffer(r.FRAMEBUFFER,t)),ht(r,()=>r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,null,0))}function wd(r){let t=r.checkFramebufferStatus(r.FRAMEBUFFER);if(t!==r.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+OL(r,t))}function OL(r,t){switch(t){case r.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case r.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case r.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case r.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return ` unknown error $ { t } ` }}function Rl(r,t,e){let n=ht(r,()=>t());if(n==null)throw new Error(e);return n}function ML(r,t){let e=r.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,n=t+r.TEXTURE0;if(n<r.TEXTURE0||n>e){let o= ` [ gl . TEXTURE0 , gl . TEXTURE$ { e } ] ` ;throw new Error( ` textureUnit must be in $ { o } . ` )}}function Fl(r,t=2){return y.sizeFromShape(r.slice(0,r.length-t))}function Ol(r){if(r.length===0)throw Error("Cannot get rows and columns of an empty shape array.");return[r.length>1?r[r.length-2]:1,r[r.length-1]]}function Id(r){let t=[1,1,1];return r.length===0||r.length===1&&r[0]===1||(t=[Fl(r),...Ol(r)]),t}function r1(r,t=!1){let e=L().getNumber("WEBGL_MAX_TEXTURE_SIZE"),n=L().getNumber("WEBGL_MAX_SIZE_FOR_NARROW_TEXTURE");n===1/0&&L().getBool("WEBGL_AUTO_SQUARIFY_NARROW_TEXTURE_SHAPE")&&(n=e/2),t&&(e=e*2,n=n*2,r=r.map((a,u)=>u>=r.length-2?y.nearestLargerEven(r[u]):r[u]),r.length===1&&(r=[2,r[0]])),r.length!==2&&(r=y.squeezeShape(r).newShape);let o=y.sizeFromShape(r),s=null;r.length<=1&&o<=e?s=[1,o]:r.length===2&&r[0]<=e&&r
2024-01-17 22:56:53 +01:00
bool isnan _custom ( float val ) {
uint floatToUint = floatBitsToUint ( val ) ;
return ( floatToUint & 0x7fffffff u ) > 0x7f800000 u ;
2024-01-17 22:44:24 +01:00
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
bvec4 isnan _custom ( vec4 val ) {
return bvec4 ( isnan _custom ( val . x ) ,
isnan _custom ( val . y ) , isnan _custom ( val . z ) , isnan _custom ( val . w ) ) ;
2024-01-17 22:44:24 +01:00
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
# define isnan ( value ) isnan _custom ( value )
` :"",u="",l= `
# define round ( value ) newRound ( value )
int newRound ( float value ) {
return int ( floor ( value + 0.5 ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
ivec4 newRound ( vec4 value ) {
return ivec4 ( floor ( value + vec4 ( 0.5 ) ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` ):(r="",t="attribute",e="varying",n="varying",o="texture2D",s="gl_FragColor",i="",a= `
# define isnan ( value ) isnan _custom ( value )
bool isnan _custom ( float val ) {
return ( val > 0. || val < 1. || val == 0. ) ? false : true ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
bvec4 isnan _custom ( vec4 val ) {
return bvec4 ( isnan ( val . x ) , isnan ( val . y ) , isnan ( val . z ) , isnan ( val . w ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` ,u= `
uniform float INFINITY ;
bool isinf ( float val ) {
return abs ( val ) == INFINITY ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
bvec4 isinf ( vec4 val ) {
return equal ( abs ( val ) , vec4 ( INFINITY ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` ,l= `
int round ( float value ) {
return int ( floor ( value + 0.5 ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
ivec4 round ( vec4 value ) {
return ivec4 ( floor ( value + vec4 ( 0.5 ) ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` ),{version:r,attribute:t,varyingVs:e,varyingFs:n,texture2D:o,output:s,defineOutput:i,defineSpecialNaN:a,defineSpecialInf:u,defineRound:l}}function Si(r,t,e="index"){let n=y.computeStrides(t);return n.map((o,s)=>{let i= ` int $ { r [ s ] } = $ { e } / $ { o } ` ,a=s===n.length-1? ` int $ { r [ s + 1 ] } = $ { e } - $ { r [ s ] } * $ { o } ` : ` index -= $ { r [ s ] } * $ { o } ` ;return ` $ { i } ; $ { a } ; ` }).join("")}function up(r,t,e="index"){let n=y.computeStrides(t);return n.map((o,s)=>{let i= ` int $ { r [ s ] } = $ { e } / outShapeStrides [ $ { s } ] ` ,a=s===n.length-1? ` int $ { r [ s + 1 ] } = $ { e } - $ { r [ s ] } * outShapeStrides [ $ { s } ] ` : ` index -= $ { r [ s ] } * outShapeStrides [ $ { s } ] ` ;return ` $ { i } ; $ { a } ; ` }).join("")}function Qnt(r,t){let e=r.length,n=r.map(s=> ` $ { t } [ $ { s } ] ` ),o=new Array(e-1);o[e-2]=n[e-1];for(let s=e-3;s>=0;--s)o[s]= ` ( $ { o [ s + 1 ] } * $ { n [ s + 1 ] } ) ` ;return o}function PL(r,t,e="index"){let n=r.map((s,i)=>i),o=Qnt(n,t);return o.map((s,i)=>{let a= ` int $ { r [ i ] } = $ { e } / $ { o [ i ] } ` ,u=i===o.length-1? ` int $ { r [ i + 1 ] } = $ { e } - $ { r [ i ] } * $ { o [ i ] } ` : ` index -= $ { r [ i ] } * $ { o [ i ] } ` ;return ` $ { a } ; $ { u } ; ` }).join("")}function vd(r){let t=y.computeStrides(r).map(e=>e.toString());return `
2024-01-17 22:56:53 +01:00
int getFlatIndex ( ivec3 coords ) {
return coords . x * $ { t [ 0 ] } + coords . y * $ { t [ 1 ] } + coords . z ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }function Sd(){return `
2024-01-17 22:56:53 +01:00
int getFlatIndex ( ivec3 coords ) {
return coords . x * outShapeStrides [ 0 ] + coords . y * outShapeStrides [ 1 ] + coords . z ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }var Gw= `
2024-01-17 22:56:53 +01:00
const float FLOAT _MAX = 1.70141184 e38 ;
const float FLOAT _MIN = 1.17549435 e - 38 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
lowp vec4 encode _float ( highp float v ) {
if ( isnan ( v ) ) {
return vec4 ( 255 , 255 , 255 , 255 ) ;
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
highp float av = abs ( v ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
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 ;
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
highp vec4 c = vec4 ( 0 , 0 , 0 , 0 ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
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 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
c [ 3 ] += 128.0 * step ( 0.0 , - v ) ;
return c / 255.0 ;
2022-10-18 13:10:36 +02:00
}
2024-09-10 17:30:23 +02:00
` ;var{getBroadcastDims:LL}=S;function zL(r,t,e){let n=[];if(r.forEach(f=>{let d=y.sizeFromShape(f.shapeInfo.logicalShape);if(f.shapeInfo.isUniform?n.push( ` uniform float $ { f . name } $ { d > 1 ? ` [ ${ d } ] ` : "" } ; ` ):(n.push( ` uniform sampler2D $ { f . name } ; ` ),n.push( ` uniform int offset$ { f . name } ; ` )),e.enableShapeUniforms){let{uniformShape:h}=Ww(e.packedInputs,f.shapeInfo.logicalShape,f.shapeInfo.texShape);switch(h.length){case 1:n.push( ` uniform int $ { f . name } Shape ; ` );break;case 2:n.push( ` uniform ivec2 $ { f . name } Shape ; ` );break;case 3:n.push( ` uniform ivec3 $ { f . name } Shape ; ` );break;case 4:n.push( ` uniform ivec4 $ { f . name } Shape ; ` );break;default:break}n.push( ` uniform ivec2 $ { f . name } TexShape ; ` )}}),e.enableShapeUniforms){switch(t.logicalShape.length){case 1:n.push("uniform int outShape;");break;case 2:n.push("uniform ivec2 outShape;"),n.push("uniform int outShapeStrides;");break;case 3:n.push("uniform ivec3 outShape;"),n.push("uniform ivec2 outShapeStrides;");break;case 4:n.push("uniform ivec4 outShape;"),n.push("uniform ivec3 outShapeStrides;");break;default:break}n.push("uniform ivec2 outTexShape;")}e.customUniforms&&e.customUniforms.forEach(f=>{n.push( ` uniform $ { f . type } $ { f . name } $ { f . arrayIndex ? ` [ ${ f . arrayIndex } ] ` : "" } ; ` )});let o=n.join( `
` ),s=r.map(f=>tot(f,t,e.packedInputs,e.enableShapeUniforms)).join( `
` ),i=t.texShape,a=Ue(),u=not(a),l,c,p=iot(a);return t.isPacked?(l=eot(t.logicalShape,i,e.enableShapeUniforms),c=sot(a)):(l=rot(t.logicalShape,i,e.enableShapeUniforms),c=oot(a)),e.packedInputs&&(p+=cot),[p,u,c,o,l,s,e.userCode].join( `
` )}function kd(r,t=!1){let e=r.shapeInfo.logicalShape;switch(e.length){case 0:return Cot(r,t);case 1:return Sot(r,t);case 2:return kot(r,t);case 3:return _ot(r,t);case 4:return Aot(r,t);case 5:return Dot(r);case 6:return $ ot(r);default:throw new Error( ` $ { e . length } - D input sampling is not yet supported ` )}}function BL(r,t){switch(r.shapeInfo.logicalShape.length){case 0:return Iot(r);case 1:return vot(r,t);case 2:return Not(r,t);case 3:return Tot(r,t);default:return Eot(r,t)}}function tot(r,t,e=!1,n){let o="";e?o+=BL(r,n):o+=kd(r,n);let s=r.shapeInfo.logicalShape,i=t.logicalShape;return s.length<=i.length&&(e?o+=Rot(r,t):o+=Fot(r,t)),o}function eot(r,t,e){switch(r.length){case 0:return VL();case 1:return pot(r,t,e);case 2:return bot(r,t,e);case 3:return fot(r,t,e);default:return hot(r,t,e)}}function rot(r,t,e){switch(r.length){case 0:return VL();case 1:return mot(r,t,e);case 2:return wot(r,t,e);case 3:return dot(r,t,e);case 4:return got(r,t,e);case 5:return xot(r,t);case 6:return yot(r,t);default:throw new Error( ` $ { r . length } - D output sampling is not yet supported ` )}}function not(r){return `
2024-01-17 22:56:53 +01:00
float sampleTexture ( sampler2D textureSampler , vec2 uv ) {
return $ { r . texture2D } ( textureSampler , uv ) . r ;
}
2024-09-10 17:30:23 +02:00
` }function oot(r){return `
2024-01-17 22:56:53 +01:00
void setOutput ( float val ) {
$ { r . output } = vec4 ( val , 0 , 0 , 0 ) ;
}
2024-09-10 17:30:23 +02:00
` }function sot(r){return `
2024-01-17 22:56:53 +01:00
void setOutput ( vec4 val ) {
$ { r . output } = val ;
}
2024-09-10 17:30:23 +02:00
` }function iot(r){return ` $ { r . version }
2024-01-17 22:56:53 +01:00
precision highp float ;
precision highp int ;
precision highp sampler2D ;
$ { r . varyingFs } vec2 resultUV ;
$ { r . defineOutput }
const vec2 halfCR = vec2 ( 0.5 , 0.5 ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
struct ivec5
{
int x ;
int y ;
int z ;
int w ;
int u ;
} ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
struct ivec6
{
int x ;
int y ;
int z ;
int w ;
int u ;
int v ;
} ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
uniform float NAN ;
$ { r . defineSpecialNaN }
$ { r . defineSpecialInf }
$ { r . defineRound }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int imod ( int x , int y ) {
return x - y * ( x / y ) ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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 ;
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
return res ;
2024-01-17 22:44:24 +01:00
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01: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 ) ;
}
2022-10-18 13:10:36 +02:00
2024-09-10 17:30:23 +02:00
$ { aot }
$ { lot }
$ { uot }
` }var aot= `
2024-01-17 22:56:53 +01: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 ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
vec2 packedUVfrom1D ( int texNumR , int texNumC , int index ) {
int texelIndex = index / 2 ;
int texR = texelIndex / texNumC ;
int texC = texelIndex - texR * texNumC ;
return ( vec2 ( texC , texR ) + halfCR ) / vec2 ( texNumC , texNumR ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` ,lot= `
2024-01-17 22:56:53 +01:00
vec2 packedUVfrom2D ( int texelsInLogicalRow , int texNumR ,
int texNumC , int row , int col ) {
int texelIndex = ( row / 2 ) * texelsInLogicalRow + ( col / 2 ) ;
int texR = texelIndex / texNumC ;
int texC = texelIndex - texR * texNumC ;
return ( vec2 ( texC , texR ) + halfCR ) / vec2 ( texNumC , texNumR ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` ,uot= `
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
` ,cot= `
2024-01-17 22:56:53 +01: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 ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
float getChannel ( vec4 frag , int dim ) {
float modCoord = mod ( float ( dim ) , 2. ) ;
return modCoord == 0. ? frag . r : frag . g ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` ;function VL(){return `
2024-01-17 22:56:53 +01:00
int getOutputCoords ( ) {
return 0 ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }function pot(r,t,e){let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return n[0]===1?e? `
2024-01-17 22:56:53 +01:00
int getOutputCoords ( ) {
return 2 * int ( resultUV . x * ceil ( float ( outTexShape [ 1 ] ) / 2.0 ) ) ;
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
` : `
int getOutputCoords ( ) {
return 2 * int ( resultUV . x * $ { n [ 1 ] } . 0 ) ;
}
` :n[1]===1?e? `
int getOutputCoords ( ) {
return 2 * int ( resultUV . y * ceil ( float ( outTexShape [ 0 ] ) / 2.0 ) ) ;
}
` : `
int getOutputCoords ( ) {
return 2 * int ( resultUV . y * $ { n [ 0 ] } . 0 ) ;
}
` :e? `
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 ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` : `
int getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
return 2 * ( resTexRC . x * $ { n [ 1 ] } + resTexRC . y ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }function mot(r,t,e){return t[0]===1?e? `
2024-01-17 22:56:53 +01:00
int getOutputCoords ( ) {
return int ( resultUV . x * float ( outTexShape [ 1 ] ) ) ;
}
` : `
int getOutputCoords ( ) {
return int ( resultUV . x * $ { t [ 1 ] } . 0 ) ;
}
` :t[1]===1?e? `
int getOutputCoords ( ) {
return int ( resultUV . y * float ( outTexShape [ 0 ] ) ) ;
}
` : `
int getOutputCoords ( ) {
return int ( resultUV . y * $ { t [ 0 ] } . 0 ) ;
}
` :e? `
int getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
return resTexRC . x * outTexShape [ 1 ] + resTexRC . y ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` : `
int getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
return resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }function fot(r,t,e){if(e)return `
2024-01-17 22:56:53 +01: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 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int b = index / texelsInBatch ;
index -= b * texelsInBatch ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int r = 2 * ( index / texelsInLogicalRow ) ;
int c = imod ( index , texelsInLogicalRow ) * 2 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
return ivec3 ( b , r , c ) ;
}
` ;let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],o=Math.ceil(r[2]/2),s=o*Math.ceil(r[1]/2);return `
ivec3 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
int index = resTexRC . x * $ { n [ 1 ] } + resTexRC . y ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int b = index / $ { s } ;
index -= b * $ { s } ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int r = 2 * ( index / $ { o } ) ;
int c = imod ( index , $ { o } ) * 2 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
return ivec3 ( b , r , c ) ;
}
2024-09-10 17:30:23 +02:00
` }function dot(r,t,e){if(e)return `
2024-01-17 22:56:53 +01:00
ivec3 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
int index = resTexRC . x * outTexShape [ 1 ] + resTexRC . y ;
$ { up ( [ "r" , "c" , "d" ] , r ) }
return ivec3 ( r , c , d ) ;
}
2024-09-10 17:30:23 +02:00
` ;let n=Si(["r","c","d"],r);return `
2024-01-17 22:56:53 +01:00
ivec3 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
$ { n }
return ivec3 ( r , c , d ) ;
}
2024-09-10 17:30:23 +02:00
` }function hot(r,t,e){if(e)return `
2024-01-17 22:56:53 +01: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 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int texelsInLogicalRow = int ( ceil ( float ( outShape [ 3 ] ) / 2.0 ) ) ;
int texelsInBatch = texelsInLogicalRow * int ( ceil ( float ( outShape [ 2 ] ) / 2.0 ) ) ;
int texelsInBatchN = texelsInBatch * outShape [ 1 ] ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int b2 = index / texelsInBatchN ;
index -= b2 * texelsInBatchN ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int b = index / texelsInBatch ;
index -= b * texelsInBatch ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
int r = 2 * ( index / texelsInLogicalRow ) ;
int c = imod ( index , texelsInLogicalRow ) * 2 ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
return ivec4 ( b2 , b , r , c ) ;
}
` ;let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],o=Math.ceil(r[r.length-1]/2),s=o*Math.ceil(r[r.length-2]/2),i=s,a="",u="b, r, c";for(let l=2;l<r.length-1;l++)i*=r[r.length-l-1],a= `
int b$ { l } = index / $ { i } ;
index -= b$ { l } * $ { i } ;
` +a,u= ` b$ { l } , ` +u;return `
ivec$ { r . length } getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
int index = resTexRC . x * $ { n [ 1 ] } + resTexRC . y ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { a }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int b = index / $ { s } ;
index -= b * $ { s } ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int r = 2 * ( index / $ { o } ) ;
int c = imod ( index , $ { o } ) * 2 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
return ivec$ { r . length } ( $ { u } ) ;
}
2024-09-10 17:30:23 +02:00
` }function got(r,t,e){if(e)return `
2024-01-17 22:56:53 +01:00
ivec4 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
int index = resTexRC . x * outTexShape [ 1 ] + resTexRC . y ;
$ { up ( [ "r" , "c" , "d" , "d2" ] , r ) }
return ivec4 ( r , c , d , d2 ) ;
}
2024-09-10 17:30:23 +02:00
` ;let n=Si(["r","c","d","d2"],r);return `
2024-01-17 22:56:53 +01:00
ivec4 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
$ { n }
return ivec4 ( r , c , d , d2 ) ;
}
2024-09-10 17:30:23 +02:00
` }function xot(r,t){let e=Si(["r","c","d","d2","d3"],r);return `
2024-01-17 22:56:53 +01:00
ivec5 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx * vec2 ( $ { t [ 0 ] } ,
$ { t [ 1 ] } ) ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { e }
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
ivec5 outShape = ivec5 ( r , c , d , d2 , d3 ) ;
return outShape ;
}
2024-09-10 17:30:23 +02:00
` }function yot(r,t){let e=Si(["r","c","d","d2","d3","d4"],r);return `
2024-01-17 22:56:53 +01:00
ivec6 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { e }
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
ivec6 result = ivec6 ( r , c , d , d2 , d3 , d4 ) ;
return result ;
}
2024-09-10 17:30:23 +02:00
` }function bot(r,t,e){let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];if(y.arraysEqual(r,t))return e? `
2024-01-17 22:56:53 +01: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 ( ) {
return 2 * ivec2 ( resultUV . yx * vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
}
` ;let o=Math.ceil(r[1]/2);return e? `
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 ] ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
int index = resTexRC . x * packedTexShape [ 1 ] + resTexRC . y ;
int r = 2 * ( index / texelsInLogicalRow ) ;
int c = imod ( index , texelsInLogicalRow ) * 2 ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
return ivec2 ( r , c ) ;
}
` : `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
int index = resTexRC . x * $ { n [ 1 ] } + resTexRC . y ;
int r = 2 * ( index / $ { o } ) ;
int c = imod ( index , $ { o } ) * 2 ;
return ivec2 ( r , c ) ;
}
2024-09-10 17:30:23 +02:00
` }function wot(r,t,e){return y.arraysEqual(r,t)?e? `
2024-01-17 22:56:53 +01:00
ivec2 getOutputCoords ( ) {
return ivec2 ( resultUV . yx * vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` : `
ivec2 getOutputCoords ( ) {
return ivec2 ( resultUV . yx * vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
}
` :r[1]===1?e? `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
int index = resTexRC . x * outTexShape [ 1 ] + resTexRC . y ;
return ivec2 ( index , 0 ) ;
}
` : `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
return ivec2 ( index , 0 ) ;
}
` :r[0]===1?e? `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
int index = resTexRC . x * outTexShape [ 1 ] + resTexRC . y ;
return ivec2 ( 0 , index ) ;
}
` : `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
return ivec2 ( 0 , index ) ;
}
` :e? `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( outTexShape [ 0 ] , outTexShape [ 1 ] ) ) ;
int index = resTexRC . x * outTexShape [ 1 ] + resTexRC . y ;
int r = index / outShape [ 1 ] ;
int c = index - r * outShape [ 1 ] ;
return ivec2 ( r , c ) ;
}
` : `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
int r = index / $ { r [ 1 ] } ;
int c = index - r * $ { r [ 1 ] } ;
return ivec2 ( r , c ) ;
}
2024-09-10 17:30:23 +02:00
` }function cp(r){return ` offset$ { r } ` }function Iot(r){let t=r.name,e="get"+t.charAt(0).toUpperCase()+t.slice(1),n=Ue();return `
2024-01-17 22:56:53 +01:00
vec4 $ { e } ( ) {
return $ { n . texture2D } ( $ { t } , halfCR ) ;
}
2024-09-10 17:30:23 +02:00
` }function Cot(r,t){let e=r.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1);if(r.shapeInfo.isUniform)return ` float $ { n } ( ) { return $ { e } ; } ` ;let[o,s]=r.shapeInfo.texShape;if(o===1&&s===1)return `
2024-01-17 22:56:53 +01:00
float $ { n } ( ) {
return sampleTexture ( $ { e } , halfCR ) ;
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let i=cp(e);if(t)return `
float $ { n } ( ) {
vec2 uv = uvFromFlat ( $ { e } TexShape [ 0 ] , $ { e } TexShape [ 1 ] , $ { i } ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` ;let[a,u]=r.shapeInfo.texShape;return `
float $ { n } ( ) {
vec2 uv = uvFromFlat ( $ { a } , $ { u } , $ { i } ) ;
return sampleTexture ( $ { e } , uv ) ;
2022-10-18 13:23:49 +02:00
}
2024-09-10 17:30:23 +02:00
` }function vot(r,t){let e=r.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1),o=r.shapeInfo.texShape,s=Ue();if(t)return `
2024-01-17 22:56:53 +01:00
vec4 $ { n } ( int index ) {
ivec2 packedTexShape = ivec2 ( ceil ( float ( $ { e } TexShape [ 0 ] ) / 2.0 ) , ceil ( float ( $ { e } TexShape [ 1 ] ) / 2.0 ) ) ;
vec2 uv = packedUVfrom1D (
packedTexShape [ 0 ] , packedTexShape [ 1 ] , index ) ;
return $ { s . texture2D } ( $ { e } , uv ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` ;let i=[Math.ceil(o[0]/2),Math.ceil(o[1]/2)];return `
vec4 $ { n } ( int index ) {
vec2 uv = packedUVfrom1D (
$ { i [ 0 ] } , $ { i [ 1 ] } , index ) ;
return $ { s . texture2D } ( $ { e } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function Sot(r,t){let e=r.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1);if(r.shapeInfo.isUniform)return `
2024-01-17 22:56:53 +01:00
float $ { n } ( int index ) {
2024-09-10 17:30:23 +02:00
$ { Td ( r ) }
2024-01-17 22:56:53 +01:00
}
` ;let o=r.shapeInfo.texShape,s=o[0],i=o[1];if(i===1&&s===1)return `
float $ { n } ( int index ) {
return sampleTexture ( $ { e } , halfCR ) ;
}
` ;let a=cp(e);return i===1?t? `
float $ { n } ( int index ) {
vec2 uv = vec2 ( 0.5 , ( float ( index + $ { a } ) + 0.5 ) / float ( $ { e } TexShape [ 0 ] ) ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` : `
float $ { n } ( int index ) {
vec2 uv = vec2 ( 0.5 , ( float ( index + $ { a } ) + 0.5 ) / $ { s } . 0 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` :s===1?t? `
float $ { n } ( int index ) {
vec2 uv = vec2 ( ( float ( index + $ { a } ) + 0.5 ) / float ( $ { e } TexShape [ 1 ] ) , 0.5 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` : `
float $ { n } ( int index ) {
vec2 uv = vec2 ( ( float ( index + $ { a } ) + 0.5 ) / $ { i } . 0 , 0.5 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` :t? `
float $ { n } ( int index ) {
vec2 uv = uvFromFlat ( $ { e } TexShape [ 0 ] , $ { e } TexShape [ 1 ] , index + $ { a } ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` : `
float $ { n } ( int index ) {
vec2 uv = uvFromFlat ( $ { s } , $ { i } , index + $ { a } ) ;
return sampleTexture ( $ { e } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function Not(r,t){let e=r.shapeInfo.logicalShape,n=r.name,o="get"+n.charAt(0).toUpperCase()+n.slice(1),s=r.shapeInfo.texShape,i=s[0],a=s[1],u=Ue();if(s!=null&&y.arraysEqual(e,s))return t? `
2024-01-17 22:56:53 +01:00
vec4 $ { o } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { n } TexShape [ 1 ] , $ { n } TexShape [ 0 ] ) ;
return $ { u . texture2D } ( $ { n } , uv ) ;
}
` : `
vec4 $ { o } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { a } . 0 , $ { i } . 0 ) ;
return $ { u . texture2D } ( $ { n } , uv ) ;
}
` ;if(t)return `
vec4 $ { o } ( int row , int col ) {
ivec2 packedTexShape = ivec2 ( ceil ( float ( $ { n } TexShape [ 0 ] ) / 2.0 ) , ceil ( float ( $ { n } TexShape [ 1 ] ) / 2.0 ) ) ;
int valuesPerRow = int ( ceil ( float ( $ { n } Shape [ 1 ] ) / 2.0 ) ) ;
vec2 uv = packedUVfrom2D ( valuesPerRow , packedTexShape [ 0 ] , packedTexShape [ 1 ] , row , col ) ;
return $ { u . texture2D } ( $ { n } , uv ) ;
}
` ;let l=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)],c=Math.ceil(e[1]/2);return `
vec4 $ { o } ( int row , int col ) {
vec2 uv = packedUVfrom2D ( $ { c } , $ { l [ 0 ] } , $ { l [ 1 ] } , row , col ) ;
return $ { u . texture2D } ( $ { n } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function kot(r,t){let e=r.shapeInfo.logicalShape,n=r.name,o="get"+n.charAt(0).toUpperCase()+n.slice(1),s=r.shapeInfo.texShape;if(s!=null&&y.arraysEqual(e,s)){if(t)return `
2024-01-17 22:56:53 +01:00
float $ { o } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { n } TexShape [ 1 ] , $ { n } TexShape [ 0 ] ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` ;let m=s[0],f=s[1];return `
float $ { o } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { f } . 0 , $ { m } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }let{newShape:i,keptDims:a}=y.squeezeShape(e),u=i;if(u.length<e.length){let m=_d(r,u),f=["row","col"];return `
$ { kd ( m , t ) }
2024-01-17 22:56:53 +01:00
float $ { o } ( int row , int col ) {
2024-09-10 17:30:23 +02:00
return $ { o } ( $ { Ed ( f , a ) } ) ;
2024-01-17 22:56:53 +01:00
}
` }if(r.shapeInfo.isUniform)return `
float $ { o } ( int row , int col ) {
int index = round ( dot ( vec2 ( row , col ) , vec2 ( $ { e [ 1 ] } , 1 ) ) ) ;
2024-09-10 17:30:23 +02:00
$ { Td ( r ) }
2024-01-17 22:56:53 +01:00
}
` ;let l=s[0],c=s[1],p=cp(n);return c===1?t? `
float $ { o } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { p } ) , vec3 ( $ { n } Shape [ 1 ] , 1 , 1 ) ) ;
vec2 uv = vec2 ( 0.5 , ( index + 0.5 ) / float ( $ { n } TexShape [ 0 ] ) ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` : `
float $ { o } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { p } ) , vec3 ( $ { e [ 1 ] } , 1 , 1 ) ) ;
vec2 uv = vec2 ( 0.5 , ( index + 0.5 ) / $ { l } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` :l===1?t? `
float $ { o } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { p } ) , vec3 ( $ { n } Shape [ 1 ] , 1 , 1 ) ) ;
vec2 uv = vec2 ( ( index + 0.5 ) / float ( $ { n } TexShape [ 1 ] ) , 0.5 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` : `
float $ { o } ( int row , int col ) {
float index = dot ( vec3 ( row , col , $ { p } ) , vec3 ( $ { e [ 1 ] } , 1 , 1 ) ) ;
vec2 uv = vec2 ( ( index + 0.5 ) / $ { c } . 0 , 0.5 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` :t? `
float $ { o } ( int row , int col ) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * $ { n } Shape [ 1 ] + col + $ { p } ;
vec2 uv = uvFromFlat ( $ { n } TexShape [ 0 ] , $ { n } TexShape [ 1 ] , index ) ;
return sampleTexture ( $ { n } , uv ) ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` : `
float $ { o } ( int row , int col ) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * $ { e [ 1 ] } + col + $ { p } ;
vec2 uv = uvFromFlat ( $ { l } , $ { c } , index ) ;
return sampleTexture ( $ { n } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function Tot(r,t){let e=r.shapeInfo.logicalShape,n=r.name,o="get"+n.charAt(0).toUpperCase()+n.slice(1),s=r.shapeInfo.texShape,i=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)];if(e[0]===1){let m=e.slice(1),f=[1,2],d=_d(r,m),h=["b","row","col"];return `
$ { BL ( d , t ) }
2024-01-17 22:56:53 +01:00
vec4 $ { o } ( int b , int row , int col ) {
2024-09-10 17:30:23 +02:00
return $ { o } ( $ { Ed ( h , f ) } ) ;
2024-01-17 22:56:53 +01:00
}
` }let a=Ue();if(t)return `
vec4 $ { o } ( int b , int row , int col ) {
ivec2 packedTexShape = ivec2 ( ceil ( float ( $ { n } TexShape [ 0 ] ) / 2.0 ) , ceil ( float ( $ { n } TexShape [ 1 ] ) / 2.0 ) ) ;
int valuesPerRow = int ( ceil ( float ( $ { n } Shape [ 2 ] ) / 2.0 ) ) ;
int texelsInBatch = valuesPerRow * int ( ceil ( float ( $ { n } Shape [ 1 ] ) / 2.0 ) ) ;
vec2 uv = packedUVfrom3D (
packedTexShape [ 0 ] , packedTexShape [ 1 ] , texelsInBatch , valuesPerRow , b , row , col ) ;
return $ { a . texture2D } ( $ { n } , uv ) ;
}
` ;let u=i[0],l=i[1],c=Math.ceil(e[2]/2),p=c*Math.ceil(e[1]/2);return `
vec4 $ { o } ( int b , int row , int col ) {
vec2 uv = packedUVfrom3D (
$ { u } , $ { l } , $ { p } , $ { c } , b , row , col ) ;
return $ { a . texture2D } ( $ { n } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function _ot(r,t){let e=r.shapeInfo.logicalShape,n=r.name,o="get"+n.charAt(0).toUpperCase()+n.slice(1),s=e[1]*e[2],i=e[2],{newShape:a,keptDims:u}=y.squeezeShape(e),l=a;if(l.length<e.length){let h=_d(r,l),g=["row","col","depth"];return `
$ { kd ( h , t ) }
2024-01-17 22:56:53 +01:00
float $ { o } ( int row , int col , int depth ) {
2024-09-10 17:30:23 +02:00
return $ { o } ( $ { Ed ( g , u ) } ) ;
2024-01-17 22:56:53 +01:00
}
` }if(r.shapeInfo.isUniform)return `
float $ { o } ( int row , int col , int depth ) {
int index = round ( dot ( vec3 ( row , col , depth ) ,
vec3 ( $ { s } , $ { i } , 1 ) ) ) ;
2024-09-10 17:30:23 +02:00
$ { Td ( r ) }
2024-01-17 22:56:53 +01:00
}
` ;let c=r.shapeInfo.texShape,p=c[0],m=c[1],f=r.shapeInfo.flatOffset;if(m===s&&f==null)return t? `
float $ { o } ( int row , int col , int depth ) {
int stride1 = $ { n } Shape [ 2 ] ;
float texR = float ( row ) ;
float texC = dot ( vec2 ( col , depth ) , vec2 ( stride1 , 1 ) ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { n } TexShape [ 1 ] , $ { n } TexShape [ 0 ] ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` : `
float $ { o } ( int row , int col , int depth ) {
float texR = float ( row ) ;
float texC = dot ( vec2 ( col , depth ) , vec2 ( $ { i } , 1 ) ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { m } . 0 , $ { p } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` ;if(m===i&&f==null)return t? `
float $ { o } ( int row , int col , int depth ) {
float texR = dot ( vec2 ( row , col ) , vec2 ( $ { n } Shape [ 1 ] , 1 ) ) ;
float texC = float ( depth ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { n } TexShape [ 1 ] , $ { n } TexShape [ 0 ] ) ;
return sampleTexture ( $ { n } , uv ) ;
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
` : `
float $ { o } ( int row , int col , int depth ) {
float texR = dot ( vec2 ( row , col ) , vec2 ( $ { e [ 1 ] } , 1 ) ) ;
float texC = float ( depth ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { m } . 0 , $ { p } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
` ;let d=cp(n);return t? `
float $ { o } ( int row , int col , int depth ) {
// Explicitly use integer operations as dot() only works on floats.
int stride0 = $ { n } Shape [ 1 ] * $ { n } Shape [ 2 ] ;
int stride1 = $ { n } Shape [ 2 ] ;
int index = row * stride0 + col * stride1 + depth + $ { d } ;
vec2 uv = uvFromFlat ( $ { n } TexShape [ 0 ] , $ { n } TexShape [ 1 ] , index ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` : `
float $ { o } ( int row , int col , int depth ) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * $ { s } + col * $ { i } + depth + $ { d } ;
vec2 uv = uvFromFlat ( $ { p } , $ { m } , index ) ;
return sampleTexture ( $ { n } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function Eot(r,t){let e=r.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1),o=Ue();if(t)return `
2024-01-17 22:56:53 +01:00
vec4 $ { n } ( int b2 , int b , int row , int col ) {
int valuesPerRow = int ( ceil ( float ( $ { e } Shape [ 3 ] ) / 2.0 ) ) ;
int texelsInBatch = valuesPerRow * int ( ceil ( float ( $ { e } Shape [ 2 ] ) / 2.0 ) ) ;
int index = b * texelsInBatch + ( row / 2 ) * valuesPerRow + ( col / 2 ) ;
texelsInBatch *= $ { e } Shape [ 1 ] ;
index = b2 * texelsInBatch + index ;
ivec2 packedTexShape = ivec2 ( ceil ( float ( $ { e } TexShape [ 0 ] ) / 2.0 ) , ceil ( float ( $ { e } TexShape [ 1 ] ) / 2.0 ) ) ;
int texR = index / packedTexShape [ 1 ] ;
int texC = index - texR * packedTexShape [ 1 ] ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( packedTexShape [ 1 ] , packedTexShape [ 0 ] ) ; return $ { o . texture2D } ( $ { e } , uv ) ;
}
` ;let s=r.shapeInfo.logicalShape,i=s.length,a=r.shapeInfo.texShape,u=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],l=u[0],c=u[1],p=Math.ceil(s[i-1]/2),m=p*Math.ceil(s[i-2]/2),f="int b, int row, int col",d= ` b * $ { m } + ( row / 2 ) * $ { p } + ( col / 2 ) ` ;for(let h=2;h<i-1;h++)f= ` int b$ { h } , ` +f,m*=s[i-h-1],d= ` b$ { h } * $ { m } + ` +d;return `
vec4 $ { n } ( $ { f } ) {
int index = $ { d } ;
int texR = index / $ { c } ;
int texC = index - texR * $ { c } ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { c } , $ { l } ) ;
return $ { o . texture2D } ( $ { e } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function Aot(r,t){let e=r.shapeInfo.logicalShape,n=r.name,o="get"+n.charAt(0).toUpperCase()+n.slice(1),s=e[3],i=e[2]*s,a=e[1]*i,{newShape:u,keptDims:l}=y.squeezeShape(e);if(u.length<e.length){let b=_d(r,u),w=["row","col","depth","depth2"];return `
$ { kd ( b , t ) }
2024-01-17 22:56:53 +01:00
float $ { o } ( int row , int col , int depth , int depth2 ) {
2024-09-10 17:30:23 +02:00
return $ { o } ( $ { Ed ( w , l ) } ) ;
2024-01-17 22:56:53 +01:00
}
` }if(r.shapeInfo.isUniform)return `
float $ { o } ( int row , int col , int depth , int depth2 ) {
int index = round ( dot ( vec4 ( row , col , depth , depth2 ) ,
vec4 ( $ { a } , $ { i } , $ { s } , 1 ) ) ) ;
2024-09-10 17:30:23 +02:00
$ { Td ( r ) }
2024-01-17 22:56:53 +01:00
}
` ;let c=r.shapeInfo.flatOffset,p=r.shapeInfo.texShape,m=p[0],f=p[1],d= ` int stride2 = $ { n } Shape [ 3 ] ; ` ,h= ` int stride1 = $ { n } Shape [ 2 ] * stride2 ; ` ,g= ` int stride0 = $ { n } Shape [ 1 ] * stride1 ; ` ;if(f===a&&c==null)return t? `
float $ { o } ( int row , int col , int depth , int depth2 ) {
$ { d }
$ { h }
float texR = float ( row ) ;
float texC =
dot ( vec3 ( col , depth , depth2 ) ,
vec3 ( stride1 , stride2 , 1 ) ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { n } TexShape [ 1 ] , $ { n } TexShape [ 0 ] ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` : `
float $ { o } ( int row , int col , int depth , int depth2 ) {
float texR = float ( row ) ;
float texC =
dot ( vec3 ( col , depth , depth2 ) ,
vec3 ( $ { i } , $ { s } , 1 ) ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { f } . 0 , $ { m } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` ;if(f===s&&c==null)return t? `
float $ { o } ( int row , int col , int depth , int depth2 ) {
float texR = dot ( vec3 ( row , col , depth ) ,
vec3 ( $ { n } Shape [ 1 ] * $ { n } Shape [ 2 ] , $ { n } Shape [ 2 ] , 1 ) ) ;
float texC = float ( depth2 ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { n } TexShape [ 1 ] , $ { n } TexShape [ 0 ] ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` : `
float $ { o } ( int row , int col , int depth , int depth2 ) {
float texR = dot ( vec3 ( row , col , depth ) ,
vec3 ( $ { e [ 1 ] * e [ 2 ] } , $ { e [ 2 ] } , 1 ) ) ;
float texC = float ( depth2 ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { f } . 0 , $ { m } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
}
` ;let x=cp(n);return t? `
float $ { o } ( int row , int col , int depth , int depth2 ) {
// Explicitly use integer operations as dot() only works on floats.
$ { d }
$ { h }
$ { g }
int index = row * stride0 + col * stride1 +
depth * stride2 + depth2 ;
vec2 uv = uvFromFlat ( $ { n } TexShape [ 0 ] , $ { n } TexShape [ 1 ] , index + $ { x } ) ;
return sampleTexture ( $ { n } , uv ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` : `
float $ { o } ( int row , int col , int depth , int depth2 ) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * $ { a } + col * $ { i } +
depth * $ { s } + depth2 ;
vec2 uv = uvFromFlat ( $ { m } , $ { f } , index + $ { x } ) ;
return sampleTexture ( $ { n } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function Dot(r){let t=r.shapeInfo.logicalShape,e=r.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1),o=t[4],s=t[3]*o,i=t[2]*s,a=t[1]*i,{newShape:u,keptDims:l}=y.squeezeShape(t);if(u.length<t.length){let h=_d(r,u),g=["row","col","depth","depth2","depth3"];return `
$ { kd ( h ) }
2024-01-17 22:56:53 +01:00
float $ { n } ( int row , int col , int depth , int depth2 , int depth3 ) {
2024-09-10 17:30:23 +02:00
return $ { n } ( $ { Ed ( g , l ) } ) ;
2024-01-17 22:56:53 +01:00
}
` }if(r.shapeInfo.isUniform)return `
float $ { n } ( int row , int col , int depth , int depth2 , int depth3 ) {
float index = dot (
vec4 ( row , col , depth , depth2 ) ,
vec4 ( $ { a } , $ { i } , $ { s } , $ { o } ) ) +
depth3 ;
2024-09-10 17:30:23 +02:00
$ { Td ( r ) }
2024-01-17 22:56:53 +01:00
}
` ;let c=r.shapeInfo.flatOffset,p=r.shapeInfo.texShape,m=p[0],f=p[1];if(f===a&&c==null)return `
float $ { n } ( int row , int col , int depth , int depth2 , int depth3 ) {
int texR = row ;
float texC = dot ( vec4 ( col , depth , depth2 , depth3 ) ,
vec4 ( $ { i } , $ { s } , $ { o } , 1 ) ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { f } . 0 , $ { m } . 0 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` ;if(f===o&&c==null)return `
float $ { n } ( int row , int col , int depth , int depth2 , int depth3 ) {
float texR = dot (
vec4 ( row , col , depth , depth2 ) ,
vec4 ( $ { t [ 1 ] * t [ 2 ] * t [ 3 ] } ,
$ { t [ 2 ] * t [ 3 ] } , $ { t [ 3 ] } , 1 ) ) ;
int texC = depth3 ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { f } . 0 , $ { m } . 0 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` ;let d=cp(e);return `
float $ { n } ( int row , int col , int depth , int depth2 , int depth3 ) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * $ { a } + col * $ { i } + depth * $ { s } +
depth2 * $ { o } + depth3 + $ { d } ;
vec2 uv = uvFromFlat ( $ { m } , $ { f } , index ) ;
return sampleTexture ( $ { e } , uv ) ;
}
2024-09-10 17:30:23 +02:00
` }function $ ot(r){let t=r.shapeInfo.logicalShape,e=r.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1),{newShape:o,keptDims:s}=y.squeezeShape(t);if(o.length<t.length){let g=_d(r,o),x=["row","col","depth","depth2","depth3","depth4"];return `
$ { kd ( g ) }
2024-01-17 22:56:53 +01:00
float $ { n } ( int row , int col , int depth ,
int depth2 , int depth3 , int depth4 ) {
2024-09-10 17:30:23 +02:00
return $ { n } ( $ { Ed ( x , s ) } ) ;
2024-01-17 22:56:53 +01:00
}
` }let i=t[5],a=t[4]*i,u=t[3]*a,l=t[2]*u,c=t[1]*l;if(r.shapeInfo.isUniform)return `
float $ { n } ( int row , int col , int depth ,
int depth2 , int depth3 , int depth4 ) {
int index = round ( dot (
vec4 ( row , col , depth , depth2 ) ,
vec4 ( $ { c } , $ { l } , $ { u } , $ { a } ) ) +
dot (
vec2 ( depth3 , depth4 ) ,
vec2 ( $ { i } , 1 ) ) ) ;
2024-09-10 17:30:23 +02:00
$ { Td ( r ) }
2024-01-17 22:56:53 +01:00
}
` ;let p=r.shapeInfo.flatOffset,m=r.shapeInfo.texShape,f=m[0],d=m[1];if(d===c&&p==null)return `
float $ { n } ( int row , int col , int depth ,
int depth2 , int depth3 , int depth4 ) {
int texR = row ;
float texC = dot ( vec4 ( col , depth , depth2 , depth3 ) ,
vec4 ( $ { l } , $ { u } , $ { a } , $ { i } ) ) +
float ( depth4 ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { d } . 0 , $ { f } . 0 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` ;if(d===i&&p==null)return `
float $ { n } ( int row , int col , int depth ,
int depth2 , int depth3 , int depth4 ) {
float texR = dot ( vec4 ( row , col , depth , depth2 ) ,
vec4 ( $ { t [ 1 ] * t [ 2 ] * t [ 3 ] * t [ 4 ] } ,
$ { t [ 2 ] * t [ 3 ] * t [ 4 ] } ,
$ { t [ 3 ] * t [ 4 ] } ,
$ { t [ 4 ] } ) ) + float ( depth3 ) ;
int texC = depth4 ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { d } . 0 , $ { f } . 0 ) ;
return sampleTexture ( $ { e } , uv ) ;
}
` ;let h=cp(e);return `
float $ { n } ( int row , int col , int depth ,
int depth2 , int depth3 , int depth4 ) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * $ { c } + col * $ { l } + depth * $ { u } +
depth2 * $ { a } + depth3 * $ { i } + depth4 + $ { h } ;
vec2 uv = uvFromFlat ( $ { f } , $ { d } , index ) ;
return sampleTexture ( $ { e } , uv ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }function Td(r){let t=r.name,e=y.sizeFromShape(r.shapeInfo.logicalShape);return e<2? ` return $ { t } ; ` : `
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { e } ; i ++ ) {
if ( i == index ) {
return $ { t } [ i ] ;
}
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }function Rot(r,t){let e=r.name,n=e.charAt(0).toUpperCase()+e.slice(1),o="get"+n+"AtOutCoords",s=r.shapeInfo.logicalShape.length,i=t.logicalShape.length,a=LL(r.shapeInfo.logicalShape,t.logicalShape),u=zt(i),l=i-s,c,p=["x","y","z","w","u","v"];s===0?c="":i<2&&a.length>=1?c="coords = 0;":c=a.map(b=> ` coords . $ { p [ b + l ] } = 0 ; ` ).join( `
2024-01-17 22:56:53 +01:00
` );let m="";i<2&&s>0?m="coords":m=r.shapeInfo.logicalShape.map((b,w)=> ` coords . $ { p [ w + l ] } ` ).join(", ");let f="return outputValue;",h=y.sizeFromShape(r.shapeInfo.logicalShape)===1,x=y.sizeFromShape(t.logicalShape)===1;if(s===1&&!h&&!x)f= `
return vec4 ( outputValue . xy , outputValue . xy ) ;
` ;else if(h&&!x)i===1?f= `
return vec4 ( outputValue . x , outputValue . x , 0. , 0. ) ;
` :f= `
return vec4 ( outputValue . x ) ;
` ;else if(a.length){let b=s-2,w=s-1;a.indexOf(b)>-1&&a.indexOf(w)>-1?f="return vec4(outputValue.x);":a.indexOf(b)>-1?f="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":a.indexOf(w)>-1&&(f="return vec4(outputValue.xx, outputValue.zz);")}return `
vec4 $ { o } ( ) {
$ { u } coords = getOutputCoords ( ) ;
$ { c }
vec4 outputValue = get$ { n } ( $ { m } ) ;
$ { f }
}
2024-09-10 17:30:23 +02:00
` }function Fot(r,t){let e=r.name,n=e.charAt(0).toUpperCase()+e.slice(1),o="get"+n+"AtOutCoords",s=t.texShape,i=r.shapeInfo.texShape,a=r.shapeInfo.logicalShape.length,u=t.logicalShape.length;if(!r.shapeInfo.isUniform&&a===u&&r.shapeInfo.flatOffset==null&&y.arraysEqual(i,s))return `
2024-01-17 22:56:53 +01:00
float $ { o } ( ) {
return sampleTexture ( $ { e } , resultUV ) ;
}
2024-09-10 17:30:23 +02:00
` ;let l=zt(u),c=LL(r.shapeInfo.logicalShape,t.logicalShape),p=u-a,m,f=["x","y","z","w","u","v"];a===0?m="":u<2&&c.length>=1?m="coords = 0;":m=c.map(h=> ` coords . $ { f [ h + p ] } = 0 ; ` ).join( `
2024-01-17 22:56:53 +01:00
` );let d="";return u<2&&a>0?d="coords":d=r.shapeInfo.logicalShape.map((h,g)=> ` coords . $ { f [ g + p ] } ` ).join(", "), `
float $ { o } ( ) {
$ { l } coords = getOutputCoords ( ) ;
$ { m }
return get$ { n } ( $ { d } ) ;
}
2024-09-10 17:30:23 +02:00
` }function zt(r){if(r<=1)return"int";if(r===2)return"ivec2";if(r===3)return"ivec3";if(r===4)return"ivec4";if(r===5)return"ivec5";if(r===6)return"ivec6";throw Error( ` GPU for rank $ { r } is not yet supported ` )}function Ww(r,t,e){let{newShape:n,keptDims:o}=y.squeezeShape(t),s=t.length,i=r&&s===3&&t[0]===1,a=i?t.slice(1):n,u=!r&&s>1&&!y.arraysEqual(t,e)&&n.length<s||i;return{useSqueezeShape:u,uniformShape:u?a:t,keptDims:o}}function _d(r,t){let e=JSON.parse(JSON.stringify(r));return e.shapeInfo.logicalShape=t,e}function Ed(r,t){return t.map(e=>r[e]).join(", ")}function WL(r,t,e,n){let o=e.map((c,p)=>{let m={logicalShape:c.shape,texShape:c.isUniform?null:c.texData.texShape,isUniform:c.isUniform,isPacked:c.isUniform?!1:c.texData.isPacked,flatOffset:null};return c.texData!=null&&c.texData.slice!=null&&c.texData.slice.flatOffset>0&&(m.flatOffset=c.texData.slice.flatOffset),{name:t.variableNames[p],shapeInfo:m}}),s=o.map(c=>c.shapeInfo),i={logicalShape:n.shape,texShape:n.texData.texShape,isUniform:!1,isPacked:n.texData.isPacked,flatOffset:null},a=zL(o,i,t),u=HT(r.gl,a),l=r.createProgram(u);return L().get("ENGINE_COMPILE_ONLY")?{program:t,fragmentShader:u,source:a,webGLProgram:l,inShapeInfos:s,outShapeInfo:i,variablesLocations:null,customUniformLocations:null,infLoc:null,nanLoc:null,outShapeLocation:null,outShapeStridesLocation:null,outTexShapeLocation:null}:(r.buildVao(l),Object.assign({program:t,fragmentShader:u,source:a,webGLProgram:l,inShapeInfos:s,outShapeInfo:i},u1(r,t,l)))}function u1(r,t,e){let n=[],o=[],s,i,a,u=null,l=null;l=r.getUniformLocation(e,"NAN",!1),L().getNumber("WEBGL_VERSION")===1&&(u=r.getUniformLocation(e,"INFINITY",!1));let c=!1;for(let p of t.variableNames){let m={name:p,uniform:r.getUniformLocation(e,p,c),offset:r.getUniformLocation(e, ` offset$ { p } ` ,c)};t.enableShapeUniforms&&(m.shape=r.getUniformLocation(e, ` $ { p } Shape ` ,c),m.texShape=r.getUniformLocation(e, ` $ { p } TexShape ` ,c)),n.push(m)}if(t.enableShapeUniforms&&(s=r.getUniformLocation(e,"outShape",c),a=r.getUniformLocation(e,"outShapeStrides",c),i=r.getUniformLocation(e,"outTexShape",c)),t.customUniforms)for(let p of t.customUniforms)o.push(r.getUniformLocation(e,p.name,c));return{variablesLocations:n,customUniformLocations:o,infLoc:u,nanLoc:l,outShapeLocation:s,outShapeStridesLocation:a,outTexShapeLocation:i}}function GL(r,t){if(r.length!==t.length)throw Error( ` Binary was compiled with $ { r . length } inputs , but was executed with $ { t . length } inputs ` );r.forEach((e,n)=>{let o=e.logicalShape,s=t[n],i=s.shape;if(!y.arraysEqual(o,i))throw Error( ` Binary was compiled with different shapes than the current args . Shapes $ { o } and $ { i } must match ` );if(e.isUniform&&s.isUniform)return;let a=e.texShape,u=s.isUniform?null:s.texData.texShape;if(!y.arraysEqual(a,u))throw Error( ` Binary was compiled with different texture shapes than the current args . Shape $ { a } and $ { u } must match ` )})}function UL(r,t,e,n,o){t.program.enableShapeUniforms||(GL(t.inShapeInfos,e),GL([t.outShapeInfo],[n]));let s=n.texData.texture,i=n.texData.texShape;n.texData.isPacked?r.setOutputPackedMatrixTexture(s.texture,i[0],i[1]):r.setOutputMatrixTexture(s.texture,i[0],i[1]),r.setProgram(t.webGLProgram),r.bindVertexArray(t.webGLProgram.vao),L().getNumber("WEBGL_VERSION")===1&&t.infLoc!==null&&r.gl.uniform1f(t.infLoc,1/0),t.nanLoc!==null&&r.gl.uniform1f(t.nanLoc,NaN);for(let u=0;u<e.length;++u){let l=e[u],{uniform:c,offset:p,shape:m,texShape:f}=t.variablesLocations[u];if(m){let{uniformShape:d}=Ww(t.program.packedInputs,l.shape,l.texData.texShape);switch(d.length){case 1:r.gl.uniform1iv(m,new Int32Array(d));break;case 2:r.gl.uniform2iv(m,new Int32Array(d));break;case 3:r.gl.uniform3iv(m,new Int32Array(d));break;case 4:r.gl.uniform4iv(m,new Int32Array(d));break;default:break}}if(f&&r.gl.uniform2i(f,l.texData.texShape[0],l.texData.texShape[1]),c!=null){if(l.isUniform){if(y.sizeFromShape(l.shape)<2)r.gl.uniform1f(c,l.uniformValues[0]);else{let d=l.uniformValues;d instanceof Float32Array||(d=new Float32Array(d)),r.gl.uniform1fv(c,d)}continue}l.texData.slice!=null&&p!=null&&r.gl.uniform1i(p,l.texData.slice.flatOffset)
2024-01-17 22:56:53 +01:00
ivec3 outCoordsFromFlatIndex ( int index ) {
2024-09-10 17:30:23 +02:00
$ { this . enableShapeUniforms ? up ( [ "r" , "c" , "d" ] , t ) : Si ( [ "r" , "c" , "d" ] , t ) }
2024-01-17 22:56:53 +01:00
return ivec3 ( r , c , d ) ;
}
2022-03-07 19:17:31 +01:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec2 resTexRC = ivec2 ( resultUV . yx * vec2 ( texShape [ 0 ] , texShape [ 1 ] ) ) ;
int index = 4 * ( resTexRC . x * texShape [ 1 ] + resTexRC . y ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0. ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01: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 ) ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { e . output } = result ;
}
2024-09-10 17:30:23 +02:00
` }};var Hw=class{constructor(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=Wu.DENSE,this.customUniforms=[{name:"texShape",type:"ivec2"}];let e=Ue();this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length),this.userCode= `
2024-01-17 22:56:53 +01:00
ivec3 outCoordsFromFlatIndex ( int index ) {
2024-09-10 17:30:23 +02:00
$ { this . enableShapeUniforms ? up ( [ "r" , "c" , "d" ] , t ) : Si ( [ "r" , "c" , "d" ] , t ) }
2024-01-17 22:56:53 +01:00
return ivec3 ( r , c , d ) ;
2022-10-18 13:23:49 +02:00
}
2022-03-07 19:17:31 +01:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec2 resTexRC = ivec2 ( resultUV . yx * vec2 ( texShape [ 0 ] , texShape [ 1 ] ) ) ;
int index = 4 * ( resTexRC . x * texShape [ 1 ] + resTexRC . y ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0. ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ) ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { e . output } = result ;
}
2024-09-10 17:30:23 +02:00
` }};var qw=class{constructor(t){this.variableNames=["A"],this.outTexUsage=Jr.DOWNLOAD;let e=Ue();this.outputShape=t,this.userCode= `
$ { Gw }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
float x = getAAtOutCoords ( ) ;
$ { e . output } = encode _float ( x ) ;
}
2024-09-10 17:30:23 +02:00
` }};var Kw=class{constructor(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=Jr.DOWNLOAD;let e=Ue();this.outputShape=t,this.userCode= `
$ { Gw }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
float x = getChannel ( getAAtOutCoords ( ) , vec2 ( coords . y , coords . z ) ) ;
$ { e . output } = encode _float ( x ) ;
}
2024-09-10 17:30:23 +02:00
` }};var Pot={R:0,G:1,B:2,A:3},ug=class{constructor(t,e=!1,n="RGBA"){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let o=Ue();this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length);let s="result";e&&(s="floor(result * 255. + 0.5)");let i="";for(let a=0;a<n.length;a++){let u=n[a];i+= `
2024-01-17 22:56:53 +01:00
if ( offset == $ { a } ) {
2024-09-10 17:30:23 +02:00
result = values [ $ { Pot [ u ] } ] ;
2024-01-17 22:56:53 +01:00
} ` }this.userCode= `
2024-09-10 17:30:23 +02:00
$ { this . enableShapeUniforms ? Sd ( ) : vd ( t ) }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
int flatIndex = getFlatIndex ( coords ) ;
float result = 0. ;
int offset = imod ( flatIndex , $ { n . length } ) ;
flatIndex = idiv ( flatIndex , $ { n . length } , 1. ) ;
int r = flatIndex / texShape [ 1 ] ;
if ( r < texShape [ 0 ] ) {
int c = imod ( flatIndex , texShape [ 1 ] ) ;
vec2 uv = ( vec2 ( c , r ) + halfCR ) / vec2 ( texShape [ 1 ] , texShape [ 0 ] ) ;
vec4 values = $ { o . texture2D } ( A , uv ) ;
$ { i }
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
$ { o . output } = vec4 ( $ { s } , 0. , 0. , 0. ) ;
2022-03-07 19:17:31 +01:00
}
2024-09-10 17:30:23 +02:00
` }};var jw=class{constructor(t,e=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=Ue();this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length);let o="",s="result";e&&(s="floor(result * 255. + 0.5)");for(let i=0;i<=1;i++)for(let a=0;a<=1;a++){let u=i*2+a;o+= `
2024-01-17 22:56:53 +01:00
localCoords = coords ;
if ( localCoords [ 2 ] + $ { a } < $ { this . enableShapeUniforms ? "outShape[2]" : ` ${ t [ 2 ] } ` } ) {
localCoords [ 2 ] += $ { a } ;
if ( localCoords [ 1 ] + $ { i } < $ { this . enableShapeUniforms ? "outShape[1]" : ` ${ t [ 1 ] } ` } ) {
localCoords [ 1 ] += $ { i } ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
flatIndex = getFlatIndex ( localCoords ) ;
offset = imod ( flatIndex , 4 ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
flatIndex = idiv ( flatIndex , 4 , 1. ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01: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 ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
if ( offset == 0 ) {
result [ $ { u } ] = values [ 0 ] ;
} else if ( offset == 1 ) {
result [ $ { u } ] = values [ 1 ] ;
} else if ( offset == 2 ) {
result [ $ { u } ] = values [ 2 ] ;
} else {
result [ $ { u } ] = values [ 3 ] ;
}
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
}
` }this.userCode= `
2024-09-10 17:30:23 +02:00
$ { this . enableShapeUniforms ? Sd ( ) : vd ( t ) }
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0. ) ;
int flatIndex , r , c , offset ;
ivec3 localCoords ;
vec2 uv ;
vec4 values ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
$ { o }
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { n . output } = $ { s } ;
}
2024-09-10 17:30:23 +02:00
` }};var k1={};Kt(k1,{bindVertexProgramAttributeStreams:()=>y1,createBufferFromOutputTexture:()=>I1,createFloat16MatrixTexture:()=>d1,createFloat16PackedMatrixTexture:()=>x1,createFloat32MatrixTexture:()=>f1,createIndexBuffer:()=>m1,createPackedMatrixTexture:()=>g1,createUnsignedBytesMatrixTexture:()=>h1,createVertexBuffer:()=>p1,createVertexShader:()=>c1,downloadByteEncodedFloatMatrixFromOutputTexture:()=>v1,downloadFloat32MatrixFromBuffer:()=>C1,downloadMatrixFromPackedOutputTexture:()=>N1,downloadPackedMatrixFromBuffer:()=>S1,getInternalFormatForFloat16MatrixTexture:()=>Yw,getInternalFormatForFloat16PackedMatrixTexture:()=>Qw,getInternalFormatForFloat32MatrixTexture:()=>Xw,getInternalFormatForPackedMatrixTexture:()=>Jw,getInternalFormatForUnsignedBytesMatrixTexture:()=>Zw,uploadDenseMatrixToTexture:()=>b1,uploadPixelDataToTexture:()=>w1});function c1(r){let t=Ue(),e= ` $ { t . version }
2024-01-17 22:56:53 +01:00
precision highp float ;
$ { t . attribute } vec3 clipSpacePos ;
$ { t . attribute } vec2 uv ;
$ { t . varyingVs } vec2 resultUV ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
gl _Position = vec4 ( clipSpacePos , 1 ) ;
resultUV = uv ;
2024-09-10 17:30:23 +02:00
} ` ;return UT(r,e)}function p1(r){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 jT(r,t)}function m1(r){let t=new Uint16Array([0,1,2,2,1,3]);return XT(r,t)}function cg(r,t,e,n,o,s){ZT(t,e);let i=YT(r),a=r.TEXTURE_2D;return ht(r,()=>r.bindTexture(a,i)),ht(r,()=>r.texParameteri(a,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE)),ht(r,()=>r.texParameteri(a,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE)),ht(r,()=>r.texParameteri(a,r.TEXTURE_MIN_FILTER,r.NEAREST)),ht(r,()=>r.texParameteri(a,r.TEXTURE_MAG_FILTER,r.NEAREST)),L().getNumber("WEBGL_VERSION")===1?ht(r,()=>r.texImage2D(a,0,n,t,e,0,o,s,null)):ht(r,()=>r.texStorage2D(a,1,n,t,e)),ht(r,()=>r.bindTexture(r.TEXTURE_2D,null)),{texture:i,texShape:[e,t]}}function Xw(r){return r.internalFormatFloat}function f1(r,t,e,n){let[o,s]=lp(t,e);return cg(r,o,s,Xw(n),n.textureFormatFloat,r.FLOAT)}function Yw(r){return r.internalFormatHalfFloat}function d1(r,t,e,n){let[o,s]=lp(t,e);return cg(r,o,s,Yw(n),n.textureFormatFloat,n.textureTypeHalfFloat)}function Zw(r){return r.downloadTextureFormat}function h1(r,t,e,n){let[o,s]=lp(t,e);return cg(r,o,s,Zw(n),r.RGBA,r.UNSIGNED_BYTE)}function Jw(r){return r.internalFormatPackedFloat}function g1(r,t,e,n){let[o,s]=wa(t,e);return cg(r,o,s,Jw(n),r.RGBA,r.FLOAT)}function Qw(r){return r.internalFormatPackedHalfFloat}function x1(r,t,e,n){let[o,s]=wa(t,e);return cg(r,o,s,Qw(n),r.RGBA,n.textureTypeHalfFloat)}function y1(r,t,e){return ht(r,()=>r.bindBuffer(r.ARRAY_BUFFER,e)),zw(r,t,"clipSpacePos",e,3,20,0)&&zw(r,t,"uv",e,2,20,12)}function b1(r,t,e,n,o,s){ht(r,()=>r.bindTexture(r.TEXTURE_2D,t));let i,a,u;o instanceof Uint8Array?(i=new Uint8Array(e*n*4),a=r.UNSIGNED_BYTE,u=r.RGBA):(i=new Float32Array(e*n*4),a=r.FLOAT,u=s.internalFormatPackedFloat),i.set(o),L().getNumber("WEBGL_VERSION")===2?ht(r,()=>r.texSubImage2D(r.TEXTURE_2D,0,0,0,e,n,r.RGBA,a,i)):ht(r,()=>r.texImage2D(r.TEXTURE_2D,0,u,e,n,0,r.RGBA,a,i)),ht(r,()=>r.bindTexture(r.TEXTURE_2D,null))}function w1(r,t,e){ht(r,()=>r.bindTexture(r.TEXTURE_2D,t)),e.data instanceof Uint8Array?L().getNumber("WEBGL_VERSION")===2?ht(r,()=>r.texSubImage2D(r.TEXTURE_2D,0,0,0,e.width,e.height,r.RGBA,r.UNSIGNED_BYTE,e.data)):ht(r,()=>r.texImage2D(r.TEXTURE_2D,0,r.RGBA,e.width,e.height,0,r.RGBA,r.UNSIGNED_BYTE,e.data)):L().getNumber("WEBGL_VERSION")===2?ht(r,()=>r.texSubImage2D(r.TEXTURE_2D,0,0,0,r.RGBA,r.UNSIGNED_BYTE,e)):ht(r,()=>r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,e)),ht(r,()=>r.bindTexture(r.TEXTURE_2D,null))}function I1(r,t,e,n){let o=r.createBuffer();ht(r,()=>r.bindBuffer(r.PIXEL_PACK_BUFFER,o));let a=4*4*t*e;return ht(r,()=>r.bufferData(r.PIXEL_PACK_BUFFER,a,r.STREAM_READ)),ht(r,()=>r.readPixels(0,0,e,t,r.RGBA,r.FLOAT,0)),ht(r,()=>r.bindBuffer(r.PIXEL_PACK_BUFFER,null)),o}function C1(r,t,e){let n=r,o=new Float32Array(e);return n.bindBuffer(n.PIXEL_PACK_BUFFER,t),n.getBufferSubData(n.PIXEL_PACK_BUFFER,0,o),n.bindBuffer(n.PIXEL_PACK_BUFFER,null),o}function v1(r,t,e,n){let[o,s]=lp(t,e),i=4,a=new Uint8Array(DL(t*e,i));return ht(r,()=>r.readPixels(0,0,o,s,n.downloadTextureFormat,r.UNSIGNED_BYTE,a)),new Float32Array(a.buffer)}function S1(r,t,e,n,o,s,i,a){let u=r,l=new Float32Array( $ L(s,i));return u.bindBuffer(u.PIXEL_PACK_BUFFER,t),u.getBufferSubData(u.PIXEL_PACK_BUFFER,0,l),u.bindBuffer(u.PIXEL_PACK_BUFFER,null),l}function N1(r,t,e){let n=new Float32Array(t*e*4);return ht(r,()=>r.readPixels(0,0,e,t,r.RGBA,r.FLOAT,n)),n}var pp=class{constructor(t){this.outputTexture=null,this.program=null,this.disposed=!1,this.itemsToPoll=[];let e=L().getNumber("WEBGL_VERSION");if(t!=null?(this.gl=t,BT(e,t)):this.gl=qn(e),t=this.gl,L().getNumber("WEBGL_VERSION")===2){let s=t;this.createVertexArray=()=>ht(s,()=>s.createVertexArray()),this.bindVertexArray=i=>ht(s,()=>s.bindVertexArray(i)),this.deleteVertexArray=i=>ht(s,()=>s.deleteVertexArray(i)),this.getVertexArray=()=>ht(s,()=>s.getParameter(s.VERTEX_ARRAY_BINDING))}else if(t!=null){let s=t.getExtension("OES_vertex_array_object");if(s==null)throw new Error("All WebGL1 implementations are expected to offer OES_vertex_array_object.");this.createVertexArray=()=>h
2024-01-17 22:56:53 +01:00
void main ( ) {
setOutput ( vec4 ( getA ( ) , 0. , 0. , 0. ) ) ;
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
` ;else{let e=rr("rc",this.rank),n=zt(this.rank),o=this.getOutOfBoundsCondition(e),s=this.getSetup(e),i=this.getOutput(e);this.userCode= `
void main ( ) {
$ { n } rc = getOutputCoords ( ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
if ( $ { o } ) {
setOutput ( vec4 ( 0 ) ) ;
} else {
$ { s }
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( vec4 ( $ { i } ) ) ;
}
}
` }}getSourceCoordsArr(t){let e=[];for(let n=0;n<=1;n++)for(let o=0;o<=1;o++){let s= ` $ { n === 0 ? "r" : "rp1" } , $ { o === 0 ? "c" : "cp1" } ` ;for(let i=2;i<this.rank;i++)s= ` $ { t [ t . length - 1 - i ] } , ` +s;e.push(s)}return e}getOutOfBoundsCondition(t){if(this.rank===1)return ` rc > $ { this . enableShapeUniforms ? "outShape" : this . outputShape [ 0 ] } ` ;let e="";for(let n=this.rank-2;n<this.rank;n++)e+= ` $ { t [ n ] } >= $ { this . enableShapeUniforms ? ` outShape[ ${ n } ] ` : this . outputShape [ n ] } ` ,n<this.rank-1&&(e+="||");return e}getSetup(t){if(this.rank===1)return"";let e=t.slice(-2),n=this.enableShapeUniforms? ` outShape [ $ { this . rank } - 1 ] ` :this.outputShape[this.rank-1],o=this.enableShapeUniforms? ` outShape [ $ { this . rank } - 2 ] ` :this.outputShape[this.rank-2];return `
int r = $ { e [ 0 ] } ;
int c = $ { e [ 1 ] } ;
int rp1 = r + 1 ;
int cp1 = c + 1 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
bool cEdge = cp1 >= $ { n } ;
bool rEdge = rp1 >= $ { o } ;
` }getOutput(t){let e=this.getSourceCoordsArr(t);return this.rank===1? ` getA ( rc ) , ( rc + 1 >= $ { this . enableShapeUniforms ? "outShape" : this . outputShape [ 0 ] } ? 0. : getA ( rc + 1 ) ) , 0 , 0 ` : ` getA ( $ { e [ 0 ] } ) ,
cEdge ? 0. : getA ( $ { e [ 1 ] } ) ,
rEdge ? 0. : getA ( $ { e [ 2 ] } ) ,
2024-09-10 17:30:23 +02:00
rEdge || cEdge ? 0. : getA ( $ { e [ 3 ] } ) ` }};var Ad=class{constructor(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec3"}],this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length);let n="";for(let o=0;o<4;o++){let s="thisRC = rc;";o%2===1&&(s+="thisRC.z += 1;"),o>1&&(s+="thisRC.y += 1;"),n+= `
2024-01-17 22:56:53 +01:00
$ { s }
$ { o > 0 ? "if(thisRC.y < rows && thisRC.z < cols){" : "" }
int flatIndex = getFlatIndex ( thisRC ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
ivec3 inputRC = inputCoordsFromReshapedOutCoords ( flatIndex ) ;
vec2 inputRCInnerDims = vec2 ( float ( inputRC . y ) , float ( inputRC . z ) ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
result [ $ { o } ] =
getChannel ( getA ( inputRC . x , inputRC . y , inputRC . z ) , inputRCInnerDims ) ;
$ { o > 0 ? "}" : "" }
` }this.userCode= `
2024-09-10 17:30:23 +02:00
$ { zot ( e , this . enableShapeUniforms ) }
$ { this . enableShapeUniforms ? Sd ( ) : vd ( t ) }
2021-04-01 19:39:54 +02:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec3 rc = getOutputCoords ( ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0. ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
ivec3 thisRC ;
int rows = $ { this . enableShapeUniforms ? "outShape[1]" : t [ 1 ] } ;
int cols = $ { this . enableShapeUniforms ? "outShape[2]" : t [ 2 ] } ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
$ { n }
2024-01-17 22:44:24 +01:00
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function zot(r,t){return `
2024-01-17 22:56:53 +01:00
ivec3 inputCoordsFromReshapedOutCoords ( int index ) {
2024-09-10 17:30:23 +02:00
$ { t ? PL ( [ "r" , "c" , "d" ] , "inputShape" ) : Si ( [ "r" , "c" , "d" ] , r ) }
2024-01-17 22:56:53 +01:00
return ivec3 ( r , c , d ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }var oI=class{constructor(t){this.gpgpu=t,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0,this.freeTextures={},this.usedTextures={},this.logEnabled=!1}acquireTexture(t,e,n){let o=zz(e,n),s=Bz(t,o,n);s in this.freeTextures||(this.freeTextures[s]=[]),s in this.usedTextures||(this.usedTextures[s]=[]);let i=Lz(t,o,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[s].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=i,this.log();let u=this.freeTextures[s].pop();return this.usedTextures[s].push(u),u}let a;return o===Lr.PACKED_2X2_FLOAT32?a=this.gpgpu.createPackedMatrixTexture(t[0],t[1]):o===Lr.PACKED_2X2_FLOAT16?a=this.gpgpu.createFloat16PackedMatrixTexture(t[0],t[1]):o===Lr.UNPACKED_FLOAT32?a=this.gpgpu.createFloat32MatrixTexture(t[0],t[1]):o===Lr.UNPACKED_FLOAT16?a=this.gpgpu.createFloat16MatrixTexture(t[0],t[1]):o===Lr.PACKED_4X1_UNSIGNED_BYTE&&(a=this.gpgpu.createUnsignedBytesMatrixTexture(t[0],t[1])),this.usedTextures[s].push(a),this.numUsedTextures++,this._numBytesAllocated+=i,this.log(),a}releaseTexture(t,e,n,o){if(this.freeTextures==null)return;let s=zz(n,o),i=Bz(e,s,o);i in this.freeTextures||(this.freeTextures[i]=[]);let a=Lz(e,s,this.gpgpu.gl,this.gpgpu.textureConfig,o),u=L().getNumber("WEBGL_DELETE_TEXTURE_THRESHOLD");u!==-1&&this._numBytesAllocated>u?(this.gpgpu.deleteMatrixTexture(t.texture),this._numBytesAllocated-=a):(this.freeTextures[i].push(t),this.numFreeTextures++,this._numBytesFree+=a),this.numUsedTextures--;let l=this.usedTextures[i],c=l&&l.indexOf(t);if(c==null||c<0)throw new Error("Cannot release a texture that was never provided by this texture manager");l[c]=l[l.length-1],l.pop(),this.log()}log(){if(!this.logEnabled)return;let t=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used", ` $ { this . numFreeTextures } / $ { this . numUsedTextures } ` , ` ( $ { t } ) ` );let e=this._numBytesFree/this._numBytesAllocated;console.log( ` Bytes allocated : $ { this . _numBytesAllocated } ` ),console.log( ` Bytes unused : $ { this . _numBytesFree } ( $ { Math . round ( 100 * e ) } % ) ` )}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 t in this.freeTextures)this.freeTextures[t].forEach(e=>{this.gpgpu.deleteMatrixTexture(e.texture)});for(let t in this.usedTextures)this.usedTextures[t].forEach(e=>{this.gpgpu.deleteMatrixTexture(e.texture)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0}}};function Bot(r,t){let e=r;if(t===e.R32F)return 4;if(t===e.R16F)return 2;if(t===e.RGBA32F)return 16;if(t===r.RGBA)return 16;if(t===e.RGBA16F)return 8;if(t===e.RGBA8)return 4;throw new Error( ` Unknown internal format $ { t } ` )}function Lz(r,t,e,n,o){let s=Vot(t,n),i;if(o){let[u,l]=wa(r[0],r[1]);i=u*l}else{let[u,l]=lp(r[0],r[1]);i=u*l}let a=Bot(e,s);return i*a}function Vot(r,t){switch(r){case Lr.PACKED_2X2_FLOAT32:return Jw(t);case Lr.PACKED_2X2_FLOAT16:return Qw(t);case Lr.UNPACKED_FLOAT32:return Xw(t);case Lr.UNPACKED_FLOAT16:return Yw(t);case Lr.PACKED_4X1_UNSIGNED_BYTE:return Zw(t);default:throw new Error( ` Unknown physical texture type $ { r } ` )}}function Got(r){return L().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?r?Lr.PACKED_2X2_FLOAT32:Lr.UNPACKED_FLOAT32:r?Lr.PACKED_2X2_FLOAT16:Lr.UNPACKED_FLOAT16}function zz(r,t){if(r===Jr.UPLOAD)return Lr.PACKED_2X2_FLOAT32;if(r===Jr.RENDER||r==null)return Got(t);if(r===Jr.DOWNLOAD||r===Jr.PIXELS)return Lr.PACKED_4X1_UNSIGNED_BYTE;throw new Error( ` Unknown logical texture type $ { r } ` )}function Bz(r,t,e){return ` $ { r [ 0 ] } _$ { r [ 1 ] } _$ { t } _$ { e } ` }var zr=class{constructor(t,e){this.variableNames=["A"],this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length),this.userCode= `
2024-01-17 22:56:53 +01:00
float unaryOperation ( float x ) {
$ { e }
2024-01-17 22:44:24 +01:00
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
float x = getAAtOutCoords ( ) ;
float y = unaryOperation ( x ) ;
setOutput ( y ) ;
}
2024-09-10 17:30:23 +02:00
` }},xr="if (isnan(x)) return x;",Vz="return x;",_1="return abs(x);";var Gz="return (x >= 0.0) ? x : (exp(x) - 1.0);",Wz=xr+ `
2024-01-17 22:56:53 +01:00
return ( x < 0.0 ) ? 0.0 : x ;
2024-09-10 17:30:23 +02:00
` ,Uz=xr+ `
2024-01-17 22:56:53 +01:00
return ( x < 0.0 ) ? 0.0 : min ( 6.0 , x ) ;
2024-09-10 17:30:23 +02:00
` ,Ia="return x;",Hz="return 1.0 / (1.0 + exp(-1.0 * x));";var Kz="return x;",jz= `
2024-01-17 22:44:24 +01:00
vec4 result ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01: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
2024-01-17 22:44:24 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,Xz= `
2024-01-17 22:56:53 +01:00
vec4 result = x * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
bvec4 isNaN = isnan ( x ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01: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 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,Yz= `
2024-01-17 22:56:53 +01:00
vec4 result = min ( x , vec4 ( 6. ) ) * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
2024-01-17 22:44:24 +01:00
bvec4 isNaN = isnan ( x ) ;
2024-01-17 22:56:53 +01:00
2024-01-17 22:44:24 +01: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 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:44:24 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,Zz="return 1.0 / (1.0 + exp(-1.0 * x));",Dn=class{constructor(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length),this.userCode= `
2024-01-17 22:56:53 +01:00
vec4 unaryOperation ( vec4 x ) {
$ { e }
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
vec4 x = getAAtOutCoords ( ) ;
vec4 y = unaryOperation ( x ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( y ) ;
}
2024-09-10 17:30:23 +02:00
` }};var sI=class{constructor(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=t,this.enableShapeUniforms=he(this.outputShape.length);let e=t.length,n=rr("rc",e),o=zt(e),s=Pz(e,n),i=n.slice(-2),a=e<=1?"rc": ` vec2 ( $ { i . join ( "," ) } ) ` ;this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { o } rc = getOutputCoords ( ) ;
vec4 packedInput = getA ( $ { s } ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( getChannel ( packedInput , $ { a } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var Uot=Xr.whereImpl,Hot=1e-7,qot=1e-4,iI={};function Kot(r){return r in iI||(iI[r]={}),iI[r]}var jot=L().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),Xot=600;function Yot(){return L().global.screen==null?1024:L().global.screen.height*L().global.screen.width*window.devicePixelRatio*Xot/1024/1024}var Dd=class r extends Bo{nextDataId(){return r.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,!L().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");let e;if(t!=null){if(t instanceof pp)e=t;else{let n=qn(L().getNumber("WEBGL_VERSION"),t);e=new pp(n)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{let n=qn(L().getNumber("WEBGL_VERSION"));e=new pp(n),this.binaryCache=Kot(L().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=e,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new oI(this.gpgpu),this.numMBBeforeWarning=Yot(),this.texData=new Ta(this,Bn())}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}writeTexture(t,e,n,o,s,i){let a=this.makeTensorInfo(e,n),u=this.texData.get(a.dataId);u.isPacked=!1,u.texture={texture:t,texShape:[o,s]},u.texShape=[o,s];let l=Id(e),c=new ug(l,!1,i),p=this.runWebGLProgram(c,[a],n,[[o,s]]);return p.shape=e,u.texture=null,this.disposeIntermediateTensorInfo(a),p.dataId}write(t,e,n){if((L().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||L().getBool("DEBUG"))&&this.checkNumericalProblems(t),n==="complex64"&&t!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");let o={id:this.nextDataId()};return this.texData.set(o,{shape:e,dtype:n,values:t,usage:Jr.UPLOAD,refCount:1}),o}refCount(t){return this.texData.has(t)?this.texData.get(t).refCount:0}incRef(t){let e=this.texData.get(t);e.refCount++}decRef(t){if(this.texData.has(t)){let e=this.texData.get(t);e.refCount--}}move(t,e,n,o,s){if(L().getBool("DEBUG")&&this.checkNumericalProblems(e),o==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(t,{shape:n,dtype:o,values:e,usage:Jr.UPLOAD,refCount:s})}disposeIntermediateTensorInfo(t){this.disposeData(t.dataId)}readSync(t){let e=this.texData.get(t),{values:n,dtype:o,complexTensorInfos:s,slice:i,shape:a,isPacked:u}=e;if(i!=null){let m;u?m=new Dn(a,Ia):m=new zr(a,Ia);let f=this.runWebGLProgram(m,[{dataId:t,shape:a,dtype:o}],o),d=this.readSync(f.dataId);return this.disposeIntermediateTensorInfo(f),d}if(n!=null)return this.convertAndCacheOnCPU(t);if(o==="string")return n;let l=this.activeTimers!=null,c;l&&(c=y.now());let p;if(o==="complex64"){let m=this.readSync(s.real.dataId),f=this.readSync(s.imag.dataId);p=S.mergeRealAndImagArrays(m,f)}else p=this.getValuesFromTexture(t);return l&&(this.downloadWaitMs+=y.now()-c),this.convertAndCacheOnCPU(t,p)}async read(t){if(this.pendingRead.has(t)){let d=this.pendingRead.get(t);return new Promise(h=>d.push(h))}let e=this.texData.get(t),{values:n,shape:o,slice:s,dtype:i,complexTensorInfos:a,isPacked:u}=e;if(s!=null){let d;u?d=new Dn(o,Ia):d=new zr(o,Ia);let h=this.runWebGLProgram(d,[{dataId:t,shape:o,dtype:i}],i),g=this.read(h.dataId);return this.disposeIntermediateTensorInfo(h),g}if(n!=null)return this.convertAndCacheOnCPU(t);if(L().getBool("DEBUG")&&!L().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&L().getNumber("WEBGL_VERSION")===2)throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");let l=null,c;if(i!=="complex64"&&L().get("WEBGL_BUFFER_SUPPORTED")){c=this.decode(t);let d=this.texData.get(c.dataId);l=this.gpgpu.createBufferFromTexture(d.texture.texture,...sg(o))}this.pendingRead.set(t,[]),i!=="complex64"&&await this.gpgpu.createAndWaitForFence();let p;if(i==="complex64"){let d=await Promise.all([this.read(a.real.dataId),this.read(a.imag.dataId)]),h=d[0],g=d[1];p=S.mergeRealAndImagArrays(h,g)}else if(l==null)p=this.get
2024-01-17 22:56:53 +01:00
if ( isnan ( a ) ) return a ;
if ( isnan ( b ) ) return b ;
` ;var $ n=class{constructor(t,e,n){this.variableNames=["A","B"],this.outputShape=S.assertAndGetBroadcastShape(e,n),this.enableShapeUniforms=he(this.outputShape.length),this.userCode= `
float binaryOperation ( float a , float b ) {
$ { t }
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
float a = getAAtOutCoords ( ) ;
float b = getBAtOutCoords ( ) ;
setOutput ( binaryOperation ( a , b ) ) ;
}
` }};var Xn= `
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 ;
` ;var jn=class{constructor(t,e,n,o=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=S.assertAndGetBroadcastShape(e,n);let s=this.outputShape.length;this.enableShapeUniforms=he(s);let i="";if(o)if(s===0||y.sizeFromShape(this.outputShape)===1)i= `
result . y = 0. ;
result . z = 0. ;
result . w = 0. ;
` ;else if(i= `
$ { zt ( s ) } coords = getOutputCoords ( ) ;
` ,s===1)this.enableShapeUniforms?i+= `
result . y = ( coords + 1 ) >= outShape ? 0. : result . y ;
result . z = 0. ;
result . w = 0. ;
` :i+= `
result . y = ( coords + 1 ) >= $ { this . outputShape [ 0 ] } ? 0. : result . y ;
result . z = 0. ;
result . w = 0. ;
` ;else{let u=rr("coords",s);this.enableShapeUniforms?i+= `
bool nextRowOutOfBounds =
( $ { u [ s - 2 ] } + 1 ) >= outShape [ $ { s } - 2 ] ;
bool nextColOutOfBounds =
( $ { u [ s - 1 ] } + 1 ) >= outShape [ $ { s } - 1 ] ;
result . y = nextColOutOfBounds ? 0. : result . y ;
result . z = nextRowOutOfBounds ? 0. : result . z ;
result . w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result . w ;
` :i+= `
bool nextRowOutOfBounds =
( $ { u [ s - 2 ] } + 1 ) >= $ { this . outputShape [ s - 2 ] } ;
bool nextColOutOfBounds =
( $ { u [ s - 1 ] } + 1 ) >= $ { this . outputShape [ s - 1 ] } ;
result . y = nextColOutOfBounds ? 0. : result . y ;
result . z = nextRowOutOfBounds ? 0. : result . z ;
result . w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result . w ;
` }this.userCode= `
vec4 binaryOperation ( vec4 a , vec4 b ) {
$ { t }
2024-01-17 22:44:24 +01:00
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
vec4 a = getAAtOutCoords ( ) ;
vec4 b = getBAtOutCoords ( ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
vec4 result = binaryOperation ( a , b ) ;
$ { i }
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function nr(r){let{inputs:t,backend:e}=r,{x:n}=t;return e.incRef(n.dataId),{dataId:n.dataId,shape:n.shape,dtype:n.dtype}}var t3={kernelName:go,backendName:"webgl",kernelFunc:nr};function Rn(r){let{inputs:t,backend:e}=r,{real:n,imag:o}=t,s=e.makeTensorInfo(n.shape,"complex64"),i=e.texData.get(s.dataId),a=nr({inputs:{x:n},backend:e}),u=nr({inputs:{x:o},backend:e});return i.complexTensorInfos={real:a,imag:u},s}var e3={kernelName:Ap,backendName:"webgl",kernelFunc:Rn};var E1="return (a < 0.) ? b * a : a;",A1= `
2024-01-17 22:56:53 +01:00
vec4 aLessThanZero = vec4 ( lessThan ( a , vec4 ( 0. ) ) ) ;
return ( aLessThanZero * ( b * a ) ) + ( ( vec4 ( 1.0 ) - aLessThanZero ) * a ) ;
2024-09-10 17:30:23 +02:00
` ;function Jot(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{alpha:s}=n,i=e.makeTensorInfo([],"float32",y.createScalarValue(s,"float32")),a=L().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new jn(A1,o.shape,i.shape):new $ n(E1,o.shape,i.shape),u=e.runWebGLProgram(a,[o,i],"float32");return e.disposeIntermediateTensorInfo(i),u}var r3={kernelName:bs,backendName:"webgl",kernelFunc:Jot};var D1="return (a < 0.) ? b * a : a;", $ 1= `
2024-01-17 22:56:53 +01:00
vec4 aLessThanZero = vec4 ( lessThan ( a , vec4 ( 0. ) ) ) ;
return ( aLessThanZero * ( b * a ) ) + ( ( vec4 ( 1.0 ) - aLessThanZero ) * a ) ;
2024-09-10 17:30:23 +02:00
` ;function Qot(r){let{inputs:t,backend:e}=r,{x:n,alpha:o}=t,s=L().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new jn( $ 1,n.shape,o.shape):new $ n(D1,n.shape,o.shape);return e.runWebGLProgram(s,[n,o],"float32")}var n3={kernelName:Os,backendName:"webgl",kernelFunc:Qot};var Po="if (isnan(x)) return x;";function It({opSnippet:r,packedOpSnippet:t,cpuKernelImpl:e,dtype:n}){return({inputs:o,backend:s})=>{let{x:i}=o,a=s,u=n||i.dtype;if(a.shouldExecuteOnCPU([i])&&e!=null){let p=a.texData.get(i.dataId),m=e(p.values,u);return a.makeTensorInfo(i.shape,u,m)}let l=L().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,c;return l?c=new Dn(i.shape,t):c=new zr(i.shape,r),a.runWebGLProgram(c,[i],u)}}function ce({opSnippet:r,packedOpSnippet:t,checkOutOfBounds:e=!1,supportsComplex:n=!1,cpuKernelImpl:o,dtype:s}){return({inputs:i,backend:a})=>{let{a:u,b:l}=i,c=a;if(n&&u.dtype==="complex64"){let d=c.texData.get(u.dataId),h=c.texData.get(l.dataId),[g,x]=[[d.complexTensorInfos.real,h.complexTensorInfos.real],[d.complexTensorInfos.imag,h.complexTensorInfos.imag]].map(w=>{let[I,N]=w,E={dataId:I.dataId,dtype:I.dtype,shape:u.shape},A={dataId:N.dataId,dtype:N.dtype,shape:l.shape},D=new $ n(r,u.shape,l.shape);return c.runWebGLProgram(D,[E,A],ur(I.dtype,N.dtype))}),b=Rn({inputs:{real:g,imag:x},backend:c});return c.disposeIntermediateTensorInfo(g),c.disposeIntermediateTensorInfo(x),b}let p=s||ur(u.dtype,l.dtype);if((u.dtype==="string"||l.dtype==="string"||c.shouldExecuteOnCPU([u,l]))&&o!=null){let d=c.texData.get(u.dataId).values,h=c.texData.get(l.dataId).values,g=u.dtype==="string"?S.fromUint8ToStringArray(d):d,x=u.dtype==="string"?S.fromUint8ToStringArray(h):h,[b,w]=o(u.shape,l.shape,g,x,p),I=c.makeTensorInfo(w,p),N=c.texData.get(I.dataId);return N.values=b,I}let m=L().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,f;return m?f=new jn(t,u.shape,l.shape,e):f=new $ n(r,u.shape,l.shape),c.runWebGLProgram(f,[u,l],p)}}function Ml(r,t=!1){if(r==="linear")return t?Kz:Vz;if(r==="relu")return t?Xz:Wz;if(r==="elu")return t?jz:Gz;if(r==="relu6")return t?Yz:Uz;if(r==="prelu")return t? $ 1:D1;if(r==="leakyrelu")return t?A1:E1;if(r==="sigmoid")return t?Zz:Hz;throw new Error( ` Activation $ { r } has not been implemented for the WebGL backend . ` )}var Rd=class{constructor(t,e,n,o=!1,s=!1,i=!1,a=null,u=!1,l=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n,this.enableShapeUniforms=he(this.outputShape.length);let c=o?t[1]:t[2],p=Math.ceil(c/2),m=o?"i * 2, rc.y":"rc.y, i * 2",f=s?"rc.z, i * 2":"i * 2, rc.z",d=o?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],h=s?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],g="",x="";a&&(u?g= ` vec4 activation ( vec4 a ) {
2024-01-17 22:56:53 +01:00
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { a }
} ` :l?g= ` vec4 activation ( vec4 a ) {
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { a }
} ` :g= ` vec4 activation ( vec4 x ) {
$ { a }
} ` ,x="result = activation(result);");let b=i?"result += getBiasAtOutCoords();":"";i&&this.variableNames.push("bias"),u&&this.variableNames.push("preluActivationWeights"),l&&this.variableNames.push("leakyreluAlpha");let w="rc.x",I="rc.x";t[0]<e[0]?w= ` imod ( rc . x , $ { t [ 0 ] } ) ` :e[0]<t[0]&&(I= ` imod ( rc . x , $ { e [ 0 ] } ) ` ),this.userCode= `
$ { g }
// Don't use uniform for sharedDimensionPacked for performance.
const float sharedDimension = $ { p } . 0 ;
vec4 dot2x2ARowBCol ( ivec3 rc ) {
vec4 result = vec4 ( 0 ) ;
int batchA = $ { w } ;
int batchB = $ { I } ;
for ( int i = 0 ; i < $ { p } ; i ++ ) {
vec4 a = getMatrixA ( batchA , $ { m } ) ;
vec4 b = getMatrixB ( batchB , $ { f } ) ;
// These swizzled products need to be separately added.
// See: https://github.com/tensorflow/tfjs/issues/1735
result += ( $ { d [ 0 ] } * $ { h [ 0 ] } ) ;
result += ( $ { d [ 1 ] } * $ { h [ 1 ] } ) ;
}
return result ;
}
void main ( ) {
ivec3 rc = getOutputCoords ( ) ;
vec4 result = dot2x2ARowBCol ( rc ) ;
$ { b }
$ { x }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var R1={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},pg=class{constructor(t,e,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=S.assertAndGetBroadcastShape(e,n),this.userCode= `
2024-01-17 22:56:53 +01:00
float binaryOpComplex (
float areal , float aimag , float breal , float bimag ) {
$ { t }
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
float areal = getARealAtOutCoords ( ) ;
float aimag = getAImagAtOutCoords ( ) ;
float breal = getBRealAtOutCoords ( ) ;
float bimag = getBImagAtOutCoords ( ) ;
setOutput ( binaryOpComplex ( areal , aimag , breal , bimag ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var o3="return a * b;";function mg(r){let{inputs:t,backend:e}=r,{a:n,b:o}=t,s=S.upcastType(n.dtype,o.dtype);if(n.dtype==="complex64"){let a=e.texData.get(n.dataId),u=e.texData.get(o.dataId),l=new pg(R1.REAL,n.shape,o.shape),c=new pg(R1.IMAG,n.shape,o.shape),p=[{dataId:a.complexTensorInfos.real.dataId,dtype:a.complexTensorInfos.real.dtype,shape:n.shape},{dataId:a.complexTensorInfos.imag.dataId,dtype:a.complexTensorInfos.imag.dtype,shape:n.shape},{dataId:u.complexTensorInfos.real.dataId,dtype:u.complexTensorInfos.real.dtype,shape:o.shape},{dataId:u.complexTensorInfos.imag.dataId,dtype:u.complexTensorInfos.imag.dtype,shape:o.shape}],m=e.runWebGLProgram(l,p,"float32"),f=e.runWebGLProgram(c,p,"float32"),d=Rn({inputs:{real:m,imag:f},backend:e});return e.disposeIntermediateTensorInfo(m),e.disposeIntermediateTensorInfo(f),d}if(e.shouldExecuteOnCPU([n,o])){let a=e.texData.get(n.dataId),u=e.texData.get(o.dataId),[l,c]=fz(n.shape,o.shape,a.values,u.values,s),p=e.makeTensorInfo(c,s),m=e.texData.get(p.dataId);return m.values=l,p}let i;return L().getBool("WEBGL_PACK_BINARY_OPERATIONS")?i=new jn(o3,n.shape,o.shape):i=new $ n(o3,n.shape,o.shape),e.runWebGLProgram(i,[n,o],s)}var s3={kernelName:Ds,backendName:"webgl",kernelFunc:mg};function i3(r,t,e){let n=[Fl(r.shape),...Ol(r.shape)],o={dtype:r.dtype,shape:n,dataId:r.dataId},s=[Fl(t),...Ol(t)],i=new Ad(s,n),a=!0,u=[n],l=e.runWebGLProgram(i,[o],r.dtype,u,a);return{dataId:l.dataId,shape:t,dtype:l.dtype}}function rt(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{shape:s}=n,i=e,a=y.sizeFromShape(o.shape),u=y.inferFromImplicitShape(s,a),l=y.sizeFromShape(u);y.assert(a===l,()=> ` The new shape ( $ { u } ) has $ { l } elements and the old shape ( $ { o . shape } ) has $ { a } elements . The new shape and old shape must have the same number of elements . ` );let c=i.texData.get(o.dataId);return c.isPacked&&!Uu(o.shape,u)&&!(c.texture!==null&&Uu(c.shape,u))?i3(o,u,i):(i.incRef(o.dataId),{dataId:o.dataId,shape:u,dtype:o.dtype})}var a3={kernelName:Gi,backendName:"webgl",kernelFunc:rt};var fg=class{constructor(t,e){this.variableNames=["x"];let{windowSize:n,batchSize:o,inSize:s,outSize:i}=t;this.outputShape=[o,i];let a=Math.floor(n/4)*4,u=n%4,l="sumValue += dot(values, ones);";if(e!=null){let p=1/e;l= ` sumValue += dot ( values * $ { y . isInt ( p ) ? p . toPrecision ( 2 ) : p } , ones ) ; ` }let c="";s%n>0&&(c= `
2024-01-17 22:56:53 +01:00
if ( inIdx < 0 || inIdx >= $ { s } ) {
return 0.0 ;
2021-04-01 19:39:54 +02:00
}
2024-01-17 22:56:53 +01:00
` ),this.userCode= `
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float getValue ( int batch , int inIdx ) {
$ { c }
return getX ( batch , inIdx ) ;
2022-10-18 13:23:49 +02:00
}
2021-04-01 19:39:54 +02:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:44:24 +01:00
ivec2 coords = getOutputCoords ( ) ;
2024-01-17 22:56:53 +01:00
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
int inOffset = outIdx * $ { n } ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01: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 < $ { a } ; i += 4 ) {
int inIdx = inOffset + i ;
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
getValue ( batch , inIdx + 3 )
) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
$ { l }
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
int inIdx = inOffset + $ { a } ;
if ( $ { u === 1 } ) {
vec4 values = vec4 ( getValue ( batch , inIdx ) , 0.0 , 0.0 , 0.0 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { l }
} else if ( $ { u === 2 } ) {
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) , 0.0 , 0.0 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { l }
} else if ( $ { u === 3 } ) {
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) , 0.0 ) ;
$ { l }
}
setOutput ( sumValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};var aI=class{constructor(t,e){this.variableNames=["x"];let{windowSize:n,batchSize:o,inSize:s,outSize:i}=t;this.outputShape=[o,i];let a="0.0",u="";e==="prod"?a="1.0":e==="min"?(a="1.0 / 1e-20",u="min"):e==="max"&&(a="-1.0 / 1e-20",u="max");let l= ` $ { e } ( $ { e } ( $ { e } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;e==="sum"?l="sumValue":e==="prod"?l="prodValue":e==="all"?l="allValue":e==="any"&&(l="anyValue");let c=Math.floor(n/4)*4,p=n%4,m= `
2024-01-17 22:56:53 +01:00
if ( $ { e === "sum" } ) {
sumValue += dot ( values , ones ) ;
} else if ( $ { e === "prod" } ) {
vec2 tmp = vec2 ( values [ 0 ] , values [ 1 ] ) * vec2 ( values [ 2 ] , values [ 3 ] ) ;
prodValue *= tmp [ 0 ] * tmp [ 1 ] ;
} else {
minMaxValue = $ { u } ( values , minMaxValue ) ;
if ( $ { e === "min" } || $ { e === "max" } ) {
minMaxValue = $ { u } ( values , minMaxValue ) ;
bvec4 isNaN = isnan ( values ) ;
if ( isNaN . r || isNaN . g || isNaN . b || isNaN . a ) {
minMaxValue = vec4 ( NAN ) ;
2024-01-17 22:44:24 +01:00
}
}
2024-01-17 22:56:53 +01:00
}
` ,f="vec4";e==="all"?(a="1.0",m= `
bool reducedAllValue = all ( values ) ;
float floatedReducedAllValue = float ( reducedAllValue ) ;
allValue = float ( allValue >= 1.0 && floatedReducedAllValue >= 1.0 ) ;
` ,f="bvec4"):e==="any"&&(a="0.0",m= `
bool reducedAnyValue = any ( values ) ;
float floatedReducedAnyValue = float ( reducedAnyValue ) ;
anyValue = float ( anyValue >= 1.0 || floatedReducedAnyValue >= 1.0 ) ;
` ,f="bvec4");let d="";s%n>0&&(d= `
if ( inIdx < 0 || inIdx >= $ { s } ) {
return initializationValue ;
}
` ),this.userCode= `
const float initializationValue = $ { a } ;
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
float getValue ( int batch , int inIdx ) {
$ { d }
return getX ( batch , inIdx ) ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
int inOffset = outIdx * $ { n } ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
vec4 minMaxValue = vec4 ( $ { a } ) ;
float prodValue = 1.0 ;
float sumValue = 0.0 ;
float allValue = 1.0 ;
float anyValue = 0.0 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { c } ; i += 4 ) {
int inIdx = inOffset + i ;
$ { f } values = $ { f } (
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
$ { m }
2022-10-18 13:23:49 +02:00
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
int inIdx = inOffset + $ { c } ;
if ( $ { p === 1 } ) {
$ { f } values = $ { f } (
getValue ( batch , inIdx ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
$ { m }
} else if ( $ { p === 2 } ) {
$ { f } values = $ { f } (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
initializationValue ,
initializationValue
) ;
$ { m }
} else if ( $ { p === 3 } ) {
$ { f } values = $ { f } (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
initializationValue
) ;
$ { m }
}
setOutput ( $ { l } ) ;
}
2024-09-10 17:30:23 +02:00
` }};function est(r){let t=[];for(;t.length===0||t[t.length-1].outSize!==1;){let e=t.length?t[t.length-1].outSize:r[1],n=S.computeOptimalWindowSize(e);t.push({inSize:e,windowSize:n,outSize:Math.ceil(e/n)})}return t}function Yn(r,t,e,n){let o=est(r.shape),s=r;for(let i=0;i<o.length;i++){let{inSize:a,windowSize:u,outSize:l}=o[i],c,p;e==="mean"?c=i===0?new fg({windowSize:u,inSize:a,batchSize:r.shape[0],outSize:l},a):new fg({windowSize:u,inSize:a,batchSize:r.shape[0],outSize:l}):c=new aI({windowSize:u,inSize:a,batchSize:r.shape[0],outSize:l},e),p=s,s=n.runWebGLProgram(c,[s],t),p.dataId!==r.dataId&&n.disposeIntermediateTensorInfo(p)}return s}var lI=class{constructor(t,e){this.variableNames=["A"];let n=new Array(t.length);for(let i=0;i<n.length;i++)n[i]=t[e[i]];this.outputShape=n,this.rank=n.length;let o=zt(this.rank),s=rst(e);this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { o } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { s } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function rst(r){let t=r.length;if(t>6)throw Error( ` Transpose for rank $ { t } is not yet supported ` );let e=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],n=new Array(t);for(let o=0;o<r.length;o++)n[r[o]]=e[o];return n.join()}var uI=class{constructor(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0;let n=new Array(t.length);for(let c=0;c<n.length;c++)n[c]=t[e[c]];if(this.outputShape=n,this.rank=n.length,this.rank>6)throw Error( ` Packed transpose for rank $ { this . rank } is not yet supported . ` );let o=zt(this.rank),s=T1("rc",this.rank),i=new Array(this.rank);for(let c=0;c<e.length;c++)i[e[c]]=s[c];let a= ` vec2 ( $ { i . slice ( - 2 ) . join ( ) } ) ` ,u= ` ++ $ { s [ this . rank - 1 ] } < $ { n [ this . rank - 1 ] } ` ,l= ` getChannel ( getA ( $ { i . join ( ) } ) , $ { a } ) ` ;this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { o } rc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
result [ 0 ] = $ { l } ;
if ( $ { u } ) {
result [ 1 ] = $ { l } ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
-- $ { s [ this . rank - 1 ] } ;
if ( ++ $ { s [ this . rank - 2 ] } < $ { n [ this . rank - 2 ] } ) {
result [ 2 ] = $ { l } ;
if ( $ { u } ) {
result [ 3 ] = $ { l } ;
}
}
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Hu(r,t,e){let n=L().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new uI(r.shape,t):new lI(r.shape,t);return e.runWebGLProgram(n,[r],r.dtype)}function l3(r,t,e,n){let o=t,s=r.shape.length,i=y.parseAxisParam(o,r.shape),a=i,u=S.getAxesPermutation(a,s),l=u!=null,c=r;l&&(c=Hu(r,u,n),a=S.getInnerMostAxes(a.length,s)),S.assertAxesAreInnerMostDims("sum",a,s);let[p,m]=S.computeOutAndReduceShapes(c.shape,a),f=p;e&&(f=S.expandShapeToKeepDim(p,i));let d=y.sizeFromShape(m),g=y.sizeFromShape(r.shape)/d,x=rt({inputs:{x:c},attrs:{shape:[g,d]},backend:n}),b=lc(r.dtype),w=Yn(x,b,"sum",n),I=rt({inputs:{x:w},attrs:{shape:f},backend:n});return n.disposeIntermediateTensorInfo(x),n.disposeIntermediateTensorInfo(w),l&&n.disposeIntermediateTensorInfo(c),I}function fp(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,keepDims:i}=n;return l3(o,s,i,e)}var u3={kernelName:Js,backendName:"webgl",kernelFunc:fp};function Pe(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{perm:s}=n,i=e,a=o.shape.length,u=new Array(a);for(let c=0;c<u.length;c++)u[c]=o.shape[s[c]];let l;if(i.shouldExecuteOnCPU([o])){let p=i.texData.get(o.dataId).values,m=mp(p,o.shape,o.dtype,s,u);l=i.makeTensorInfo(u,o.dtype);let f=i.texData.get(l.dataId);f.values=m}else l=Hu(o,s,i);return l}var c3={kernelName:so,backendName:"webgl",kernelFunc:Pe};var F1=1e3;function dp({a:r,b:t,transposeA:e,transposeB:n,backend:o,bias:s=null,preluActivationWeights:i=null,leakyreluAlpha:a=0,activation:u=null}){let l=r.shape.length,c=t.shape.length,p=e?r.shape[l-2]:r.shape[l-1],m=n?t.shape[c-1]:t.shape[c-2],f=e?r.shape[l-1]:r.shape[l-2],d=n?t.shape[c-2]:t.shape[c-1],h=r.shape.slice(0,-2),g=t.shape.slice(0,-2),x=y.sizeFromShape(h),b=y.sizeFromShape(g),I=Hr.assertAndGetBroadcastShape(r.shape.slice(0,-2),t.shape.slice(0,-2)).concat([f,d]);y.assert(p===m,()=> ` Error in matMul : inner shapes ( $ { p } ) and ( $ { m } ) of Tensors with shapes $ { r . shape } and $ { t . shape } and transposeA = $ { e } and transposeB = $ { n } must match . ` );let N=e?[x,p,f]:[x,f,p],E=n?[b,d,m]:[b,m,d],A=rt({inputs:{x:r},backend:o,attrs:{shape:N}}),D=rt({inputs:{x:t},backend:o,attrs:{shape:E}}),F=[A,D],M=Math.max(x,b),V=e?A.shape[1]:A.shape[2],G=s!=null,W=i!=null,q=u==="leakyrelu",H=u!=null?Ml(u,!0):null,j=G||W||q||H!=null,Y;if((f===1||d===1)&&V>F1&&j===!1){let et=A,nt=D;e&&(et=Pe({inputs:{x:A},backend:o,attrs:{perm:[0,2,1]}}),F.push(et)),n&&(nt=Pe({inputs:{x:D},backend:o,attrs:{perm:[0,2,1]}}),F.push(nt));let st=d!==1,lt=d===1,ot=et;st&&(ot=rt({inputs:{x:et},backend:o,attrs:{shape:[M,V,1]}}),F.push(ot));let it=d===1?2:1,ft=nt;lt&&(ft=rt({inputs:{x:nt},backend:o,attrs:{shape:[M,1,V]}}),F.push(ft));let gt=mg({inputs:{a:ot,b:ft},backend:o});Y=fp({inputs:{x:gt},backend:o,attrs:{axis:it,keepDims:!0}}),F.push(gt)}else{let et=ur(r.dtype,t.dtype),nt=new Rd(N,E,[M,f,d],e,n,G,H,W,q),st=[A,D];if(s!=null&&st.push(s),W&&st.push(i),q){let lt=o.makeTensorInfo([],"float32",y.createScalarValue(a,"float32"));st.push(lt),F.push(lt)}Y=o.runWebGLProgram(nt,st,et)}let Z=rt({inputs:{x:Y},backend:o,attrs:{shape:I}});F.push(Y);for(let et of F)o.disposeIntermediateTensorInfo(et);return Z}function nst(r){let{inputs:t,backend:e,attrs:n}=r,{a:o,b:s,bias:i,preluActivationWeights:a}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=n;return dp({a:o,b:s,transposeA:u,transposeB:l,backend:e,bias:i,preluActivationWeights:a,leakyreluAlpha:p,activation:c})}var p3={kernelName:Xi,backendName:"webgl",kernelFunc:nst};var m3="return abs(x);";function ost(r){let{inputs:t,backend:e}=r,{x:n}=t;if(e.shouldExecuteOnCPU([n])&&n.dtype!=="complex64"){let s=e.texData.get(n.dataId),i=eI(s.values);return e.makeTensorInfo(n.shape,n.dtype,i)}let o;return L().getBool("WEBGL_PACK_UNARY_OPERATIONS")?o=new Dn(n.shape,m3):o=new zr(n.shape,m3),e.runWebGLProgram(o,[n],n.dtype)}var f3={kernelName:Ai,backendName:"webgl",kernelFunc:ost};var sst=xr+ `
2024-01-17 22:56:53 +01:00
if ( abs ( x ) > 1. ) {
return NAN ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
return acos ( x ) ;
2024-09-10 17:30:23 +02:00
` ,ist=It({opSnippet:sst}),d3={kernelName:Go,backendName:"webgl",kernelFunc:ist};var ast=xr+ `
2024-01-17 22:56:53 +01:00
if ( x < 1.0 ) return NAN ;
2024-09-10 17:30:23 +02:00
return log ( x + sqrt ( x * x - 1.0 ) ) ; ` ,lst=It({opSnippet:ast}),h3={kernelName:Wo,backendName:"webgl",kernelFunc:lst};var g3="return a + b;",ust=ce({opSnippet:g3,packedOpSnippet:g3,supportsComplex:!0,cpuKernelImpl:qL}),x3={kernelName:no,backendName:"webgl",kernelFunc:ust};var cI=class{constructor(t,e){this.outputShape=[],this.outputShape=t,this.variableNames=e.map((s,i)=> ` T$ { i } ` );let n=[];this.variableNames.forEach(s=>{n.push( ` float v$ { s } = get$ { s } AtOutCoords ( ) ; ` )});let o=this.variableNames.map(s=> ` v$ { s } ` ).join(" + ");this.userCode= `
2024-01-17 22:44:24 +01:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { n . join ( `
` )}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float result = $ { o } ;
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var pI=class{constructor(t,e){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.variableNames=e.map((s,i)=> ` T$ { i } ` );let n=[];this.variableNames.forEach(s=>{n.push( ` vec4 v$ { s } = get$ { s } AtOutCoords ( ) ; ` )});let o=this.variableNames.map(s=> ` v$ { s } ` ).join(" + ");this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { n . join ( `
` )}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
vec4 result = $ { o } ;
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function mI(r){let{inputs:t,backend:e}=r,n=t;if(n.length===1)return nr({inputs:{x:n[0]},backend:e});if(n.length>L().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let u=Math.floor(n.length/2),l=mI({inputs:n.slice(0,u),backend:e}),c=mI({inputs:n.slice(u),backend:e});return mI({inputs:[l,c],backend:e})}let o=n.map(u=>u.dtype).reduce((u,l)=>ur(u,l)),s=n.map(u=>u.shape),a=L().getBool("WEBGL_PACK")?new pI(n[0].shape,s):new cI(n[0].shape,s);return e.runWebGLProgram(a,n,o)}var y3={kernelName:Uo,backendName:"webgl",kernelFunc:mI};function cst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,keepDims:i}=n,a=o.shape.length,u=y.parseAxisParam(s,o.shape),l=u,c=S.getAxesPermutation(l,a),p=o;c!=null&&(p=Pe({inputs:{x:o},backend:e,attrs:{perm:c}}),l=S.getInnerMostAxes(l.length,a)),S.assertAxesAreInnerMostDims("all",l,a);let[m,f]=S.computeOutAndReduceShapes(p.shape,l),d=y.sizeFromShape(f),h=rt({inputs:{x:p},backend:e,attrs:{shape:[-1,d]}}),g=Yn(h,h.dtype,"all",e),x;if(i){let b=S.expandShapeToKeepDim(m,u);x=rt({inputs:{x:g},backend:e,attrs:{shape:b}})}else x=rt({inputs:{x:g},backend:e,attrs:{shape:m}});return e.disposeIntermediateTensorInfo(h),e.disposeIntermediateTensorInfo(g),c!=null&&e.disposeIntermediateTensorInfo(p),x}var b3={kernelName:Ea,backendName:"webgl",kernelFunc:cst};function pst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,keepDims:i}=n,a=o.shape.length,u=y.parseAxisParam(s,o.shape),l=u,c=S.getAxesPermutation(l,a),p=o;c!=null&&(p=Pe({inputs:{x:o},backend:e,attrs:{perm:c}}),l=S.getInnerMostAxes(l.length,a)),S.assertAxesAreInnerMostDims("any",l,a);let[m,f]=S.computeOutAndReduceShapes(p.shape,l),d=y.sizeFromShape(f),h=rt({inputs:{x:p},backend:e,attrs:{shape:[-1,d]}}),g=Yn(h,h.dtype,"any",e),x;if(i){let b=S.expandShapeToKeepDim(m,u);x=rt({inputs:{x:g},backend:e,attrs:{shape:b}})}else x=rt({inputs:{x:g},backend:e,attrs:{shape:m}});return e.disposeIntermediateTensorInfo(h),e.disposeIntermediateTensorInfo(g),c!=null&&e.disposeIntermediateTensorInfo(p),x}var w3={kernelName:Aa,backendName:"webgl",kernelFunc:pst};var fI=class{constructor(t,e,n){this.variableNames=["A"];let{windowSize:o,batchSize:s,outSize:i}=t;n||this.variableNames.push("bestIndicesA"),this.outputShape=[s,i];let a=e==="max"?">":"<",u=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
int inOffset = outIdx * $ { o } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
int bestIndex = inOffset ;
float bestValue = getA ( batch , bestIndex ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { o } ; i ++ ) {
int inIdx = $ { u } ;
float candidate = getA ( batch , inIdx ) ;
if ( candidate $ { a } bestValue ) {
bestValue = candidate ;
bestIndex = inIdx ;
2022-03-07 19:17:31 +01:00
}
}
2024-01-17 22:56:53 +01:00
setOutput ( float ( bestIndex ) ) ;
2022-03-07 19:17:31 +01:00
}
2024-09-10 17:30:23 +02:00
` }};var dI=class{constructor(t,e,n,o){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,y.assert(t.length>2,()=> ` Packed arg$ { n . charAt ( 0 ) . toUpperCase ( ) + n . slice ( 1 ) } supports only inputs with rank above 2. ` );let s=t[t.length-1],i=Math.ceil(s/e);this.outputShape=t.slice(0,-1),i>1&&this.outputShape.push(i),o||this.variableNames.push("bestIndicesA");let a=this.outputShape,u=a.length,l=zt(u),c=rr("coords",u),p,m;if(i===1){m=u+1;let D=zt(m);p= `
2024-01-17 22:56:53 +01:00
$ { D } sourceLocR = $ { D } ( $ { c . join ( ) } , 0 ) ;
++ $ { c [ u - 1 ] } ;
$ { D } sourceLocG = $ { D } ( $ { c . join ( ) } , 0 ) ;
++ $ { c [ u - 2 ] } ;
$ { D } sourceLocA = $ { D } ( $ { c . join ( ) } , 0 ) ;
-- $ { c [ u - 1 ] } ;
$ { D } sourceLocB = $ { D } ( $ { c . join ( ) } , 0 ) ;
-- $ { c [ u - 2 ] } ; ` }else m=u,p= `
$ { l } sourceLocR = coords ;
++ $ { c [ u - 1 ] } ;
$ { l } sourceLocG = coords ;
++ $ { c [ u - 2 ] } ;
$ { l } sourceLocA = coords ;
-- $ { c [ u - 1 ] } ;
$ { l } sourceLocB = coords ;
-- $ { c [ u - 2 ] } ; ` ;let f=["x","y","z","w","u","v"].slice(0,m),d="."+f[m-1],h=f.map(D=>"int "+D),g=rr("sourceLocR",m-1).concat("inIdx.r"),x=rr("sourceLocG",m-1).concat("inIdx.g"),b=rr("sourceLocB",m-1).concat("inIdx.b"),w=rr("sourceLocA",m-1).concat("inIdx.a"),I=n==="max"?"greaterThan":"lessThan",N=o?"": `
inIdx = round ( vec4 ( getBestIndicesAChannel ( $ { g . join ( ) } ) ,
getBestIndicesAChannel ( $ { x . join ( ) } ) ,
getBestIndicesAChannel ( $ { b . join ( ) } ) ,
getBestIndicesAChannel ( $ { w . join ( ) } ) ) ) ; ` ,E= ` vec4 (
getAChannel ( $ { g . join ( ) } ) ,
hasNextCol ? getAChannel ( $ { x . join ( ) } ) : 0. ,
hasNextRow ? getAChannel ( $ { b . join ( ) } ) : 0. ,
hasNextRow && hasNextCol ? getAChannel ( $ { w . join ( ) } ) : 0. ) ` ,A=o?"": `
float getBestIndicesAChannel ( $ { h . join ( ) } ) {
return getChannel ( getBestIndicesA ( $ { f . join ( ) } ) ,
vec2 ( $ { f . slice ( - 2 ) . join ( ) } ) ) ;
} ` ;this.userCode= `
float getAChannel ( $ { h . join ( ) } ) {
return getChannel ( getA ( $ { f . join ( ) } ) ,
vec2 ( $ { f . slice ( - 2 ) . join ( ) } ) ) ;
}
$ { A }
void main ( ) {
$ { l } coords = getOutputCoords ( ) ;
bool hasNextCol = $ { c [ u - 1 ] } < $ { a [ u - 1 ] - 1 } ;
bool hasNextRow = $ { c [ u - 2 ] } < $ { a [ u - 2 ] - 1 } ;
$ { p }
ivec4 srcIdx = ivec4 ( sourceLocR$ { d } , sourceLocG$ { d } ,
sourceLocB$ { d } , sourceLocA$ { d } ) * $ { e } ;
ivec4 inIdx = srcIdx ;
vec4 bestIndex = vec4 ( inIdx ) ;
vec4 bestValue = $ { E } ;
2022-01-14 16:04:13 +01:00
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { e } ; i ++ ) {
inIdx = srcIdx ;
$ { N }
vec4 candidate = $ { E } ;
bvec4 nan = isnan ( candidate ) ;
bvec4 replace = bvec4 (
vec4 ( $ { I } ( candidate , bestValue ) ) * ( vec4 ( 1.0 ) - vec4 ( nan ) ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ++ ;
}
setOutput ( bestIndex ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};function I3(r,t,e,n=null){let o=t.shape[0],s=t.shape[1];n!=null&&(o=n.shape[0],s=n.shape[1]);let i=S.computeOptimalWindowSize(s),a={windowSize:i,inSize:s,batchSize:o,outSize:Math.ceil(s/i)},u=new fI(a,e,n==null),l=[t];n!=null&&l.push(n);let c=r.runWebGLProgram(u,l,"int32");if(c.shape[1]===1)return c;let p=I3(r,t,e,c);return r.disposeIntermediateTensorInfo(c),p}function C3(r,t,e,n=null){let o=n!=null?n.shape:t.shape,s=o[o.length-1],i=S.computeOptimalWindowSize(s),a=new dI(o,i,e,n==null),u=n==null?[t]:[t,n],l=r.runWebGLProgram(a,u,"int32");if(l.shape.length===t.shape.length){let c=C3(r,t,e,l);return r.disposeIntermediateTensorInfo(l),c}return l}function hI(r,t,e,n){let o=[e];if(S.assertAxesAreInnerMostDims("arg"+n.charAt(0).toUpperCase()+n.slice(1),o,t.shape.length),!L().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let s=[],i=r.texData.get(t.dataId),a=i!==null&&i.isPacked,u=t;a&&(u=r.unpackTensor(t),s.push(u));let[l,c]=S.computeOutAndReduceShapes(u.shape,o),p=y.sizeFromShape(c),m=rt({inputs:{x:u},backend:r,attrs:{shape:[-1,p]}});s.push(m);let f=I3(r,m,n);s.push(f);let d=rt({inputs:{x:f},backend:r,attrs:{shape:l}});return s.forEach(h=>r.disposeIntermediateTensorInfo(h)),d}return C3(r,t,n)}function mst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s}=n,i=y.parseAxisParam(s,o.shape),a=S.getAxesPermutation(i,o.shape.length),u=o,l=[];a!=null&&(u=Pe({inputs:{x:o},backend:e,attrs:{perm:a}}),l.push(u),i=S.getInnerMostAxes(i.length,u.shape.length)),S.assertAxesAreInnerMostDims("argMax",[i[0]],u.shape.length);let c=hI(e,u,i[0],"max");return l.forEach(p=>e.disposeIntermediateTensorInfo(p)),c}var v3={kernelName:Di,backendName:"webgl",kernelFunc:mst};function fst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s}=n,i=y.parseAxisParam(s,o.shape),a=S.getAxesPermutation(i,o.shape.length),u=o,l=[];a!=null&&(u=Pe({inputs:{x:o},backend:e,attrs:{perm:a}}),l.push(u),i=S.getInnerMostAxes(i.length,u.shape.length)),S.assertAxesAreInnerMostDims("argMin",[i[0]],u.shape.length);let c=hI(e,u,i[0],"min");return l.forEach(p=>e.disposeIntermediateTensorInfo(p)),c}var S3={kernelName: $ i,backendName:"webgl",kernelFunc:fst};var dst=xr+ `
2024-01-17 22:56:53 +01:00
if ( abs ( x ) > 1. ) {
return NAN ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
return asin ( x ) ;
2024-09-10 17:30:23 +02:00
` ,hst=It({opSnippet:dst}),N3={kernelName:Ho,backendName:"webgl",kernelFunc:hst};var gst=xr+"return log(x + sqrt(x * x + 1.0));",xst=It({opSnippet:gst}),k3={kernelName:qo,backendName:"webgl",kernelFunc:xst};var yst=xr+ `
2024-01-17 22:56:53 +01:00
return atan ( x ) ;
2024-09-10 17:30:23 +02:00
` ,bst=It({opSnippet:yst}),T3={kernelName:Ko,backendName:"webgl",kernelFunc:bst};var wst= $ d+ `
2024-01-17 22:56:53 +01:00
return atan ( a , b ) ;
2024-09-10 17:30:23 +02:00
` ,Ist= `
2024-01-17 22:56:53 +01:00
vec4 result = atan ( a , b ) ;
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 ) ;
` +Xn+ `
2024-01-17 22:44:24 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,Cst=ce({opSnippet:wst,packedOpSnippet:Ist}),_3={kernelName:Xo,backendName:"webgl",kernelFunc:Cst};var vst=xr+ `
2024-01-17 22:56:53 +01:00
if ( ( x < - 1.0 ) || ( x > 1.0 ) ) return NAN ;
2024-09-10 17:30:23 +02:00
return ( log ( 1.0 + x ) - log ( 1.0 - x ) ) / 2.0 ; ` ,Sst=It({opSnippet:vst}),E3={kernelName:jo,backendName:"webgl",kernelFunc:Sst};var Ni=class{constructor(t,e,n,o=!1,s=!1){if(this.variableNames=["x"],e==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let i=t.filterWidth,a=t.strideHeight,u=t.strideWidth,l=t.dilationHeight,c=t.dilationWidth,p=t.effectiveFilterHeight,m=t.effectiveFilterWidth,f=t.padInfo.top,d=t.padInfo.left;this.outputShape=t.outShape;let h=e==="avg",g= ` ( ( batch * $ { t . inHeight } + xR ) * $ { t . inWidth } + xC ) * $ { t . inChannels } + d ` ,x= ` ( xR * $ { t . inWidth } + xC ) * $ { t . inChannels } + d ` ,b="0.0";if(h||(b="-1.0 / 1e-20"),n){let D=">=";this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 strides = ivec2 ( $ { a } , $ { u } ) ;
const ivec2 pads = ivec2 ( $ { f } , $ { d } ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int d = coords [ 3 ] ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
ivec2 xRCCorner = coords . yz * strides - pads ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { p } ;
wR += $ { l } ) {
int xR = xRCorner + wR ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { m } ;
wC += $ { c } ) {
int xC = xCCorner + wC ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float value = getX ( batch , xR , xC , d ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix (
value , minMaxValue , minMaxValueFound ) ;
if ( value $ { D } currMinMaxValue ) {
minMaxValue = value ;
minMaxValueFound = 1.0 ;
minMaxPosition = $ { o ? s ? g : x : ` wR * ${ m } + wC ` } ;
}
}
}
setOutput ( float ( minMaxPosition ) ) ;
}
` ;return}let w="max",I= ` $ { e } ( $ { e } ( $ { e } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;e==="avg"&&(I="avgValue / max(count, 1.0)");let N=Math.floor(i/4)*4,E=i%4,A= `
if ( $ { h } ) {
avgValue += dot ( values , ones ) ;
} else {
minMaxValue = $ { w } ( values , minMaxValue ) ;
}
` ;this.userCode= `
const ivec2 strides = ivec2 ( $ { a } , $ { u } ) ;
const ivec2 pads = ivec2 ( $ { f } , $ { d } ) ;
const float initializationValue = $ { b } ;
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float count = 0.0 ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
float getValue ( int batch , int xR , int xC , int d ) {
if ( xC < 0 || xC >= $ { t . inWidth } ) {
return initializationValue ;
}
count += 1.0 ;
return getX ( batch , xR , xC , d ) ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int d = coords [ 3 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
ivec2 xRCCorner = coords . yz * strides - pads ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
vec4 minMaxValue = vec4 ( $ { b } ) ;
float avgValue = 0.0 ;
count = 0.0 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { p } ;
wR += $ { l } ) {
int xR = xRCorner + wR ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { N } ; wC += 4 ) {
int xC = xCCorner + wC * $ { c } ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
getValue ( batch , xR , xC + $ { c } , d ) ,
getValue ( batch , xR , xC + 2 * $ { c } , d ) ,
getValue ( batch , xR , xC + 3 * $ { c } , d )
) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
$ { A }
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
int xC = xCCorner + $ { N } ;
if ( $ { E === 1 } ) {
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
$ { A }
} else if ( $ { E === 2 } ) {
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
getValue ( batch , xR , xC + $ { c } , d ) ,
initializationValue ,
initializationValue
) ;
$ { A }
} else if ( $ { E === 3 } ) {
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
getValue ( batch , xR , xC + $ { c } , d ) ,
getValue ( batch , xR , xC + 2 * $ { c } , d ) ,
initializationValue
) ;
$ { A }
2024-01-17 22:44:24 +01:00
}
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
setOutput ( $ { I } ) ;
2022-03-07 19:17:31 +01:00
}
2024-01-17 22:56:53 +01:00
` }},qu=class{constructor(t,e,n,o=!1,s=!1){if(this.variableNames=["x"],e==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let i=t.filterWidth,a=t.strideDepth,u=t.strideHeight,l=t.strideWidth,c=t.dilationDepth,p=t.dilationHeight,m=t.dilationWidth,f=t.effectiveFilterDepth,d=t.effectiveFilterHeight,h=t.effectiveFilterWidth,g=t.padInfo.front,x=t.padInfo.top,b=t.padInfo.left;this.outputShape=t.outShape;let w=e==="avg",I="0.0";if(w||(I="-1.0 / 1e-20"),n){let M=">=";this.userCode= `
const ivec3 strides =
ivec3 ( $ { a } , $ { u } , $ { l } ) ;
const ivec3 pads = ivec3 ( $ { g } , $ { x } , $ { b } ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
ivec3 xCorner = ivec3 ( coords . y , coords . z , coords . w ) * strides - pads ;
int xDCorner = xCorner . x ;
int xRCorner = xCorner . y ;
int xCCorner = xCorner . z ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01: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 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int wD = 0 ; wD < $ { f } ;
wD += $ { c } ) {
int xD = xDCorner + wD ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
if ( xD < 0 || xD >= $ { t . inDepth } ) {
continue ;
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { d } ;
wR += $ { p } ) {
int xR = xRCorner + wR ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { h } ;
wC += $ { m } ) {
int xC = xCCorner + wC ;
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
}
float value = getX ( batch , xD , xR , xC , ch ) ;
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix (
value , minMaxValue , minMaxValueFound ) ;
if ( value $ { M } currMinMaxValue ) {
minMaxValue = value ;
minMaxValueFound = 1.0 ;
minMaxPosition = $ { o ? s ? ` (((batch * ${ t . inDepth } + xD) * ${ t . inHeight } + xR) * ${ t . inWidth } + xC) * ${ t . inChannels } + ch ` : ` ((xD * ${ t . inHeight } + xR) * ${ t . inWidth } + xC) * ${ t . inChannels } + ch ` : ` wD * ${ d } * ${ h } +
wR * $ { h } + wC ` };
}
2022-10-18 13:23:49 +02:00
}
}
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
setOutput ( float ( minMaxPosition ) ) ;
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
` ;return}let N="max",E= ` $ { e } ( $ { e } ( $ { e } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;e==="avg"&&(E="avgValue / max(count, 1.0)");let A=Math.floor(i/4)*4,D=i%4,F= `
if ( $ { w } ) {
avgValue += dot ( values , ones ) ;
} else {
minMaxValue = $ { N } ( values , minMaxValue ) ;
}
` ;this.userCode= `
const ivec3 strides =
ivec3 ( $ { a } , $ { u } , $ { l } ) ;
const ivec3 pads = ivec3 ( $ { g } , $ { x } , $ { b } ) ;
const float initializationValue = $ { I } ;
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 >= $ { t . inWidth } ) {
return initializationValue ;
}
count += 1.0 ;
return getX ( batch , xD , xR , xC , ch ) ;
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:44:24 +01:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
ivec3 xCorner = ivec3 ( coords . y , coords . z , coords . w ) * strides - pads ;
int xDCorner = xCorner . x ;
int xRCorner = xCorner . y ;
int xCCorner = xCorner . z ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).
// ? = to be determined
vec4 minMaxValue = vec4 ( $ { I } ) ;
float avgValue = 0.0 ;
count = 0.0 ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
for ( int wD = 0 ; wD < $ { f } ;
wD += $ { c } ) {
int xD = xDCorner + wD ;
if ( xD < 0 || xD >= $ { t . inDepth } ) {
continue ;
2022-10-18 13:23:49 +02:00
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { d } ;
wR += $ { p } ) {
int xR = xRCorner + wR ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
2022-10-18 13:23:49 +02:00
continue ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { A } ; wC += 4 ) {
int xC = xCCorner + wC * $ { m } ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
getValue ( batch , xD , xR , xC + $ { m } , ch ) ,
getValue ( batch , xD , xR , xC + 2 * $ { m } , ch ) ,
getValue ( batch , xD , xR , xC + 3 * $ { m } , ch )
) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { F }
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
int xC = xCCorner + $ { A } ;
if ( $ { D === 1 } ) {
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { F }
} else if ( $ { D === 2 } ) {
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
getValue ( batch , xD , xR , xC + $ { m } , ch ) ,
initializationValue ,
initializationValue
) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { F }
} else if ( $ { D === 3 } ) {
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
getValue ( batch , xD , xR , xC + $ { m } , ch ) ,
getValue ( batch , xD , xR , xC + 2 * $ { m } , ch ) ,
initializationValue
) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
$ { F }
}
}
}
setOutput ( $ { E } ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Nst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t;vi(o,"avgPool");let{filterSize:s,strides:i,pad:a,dimRoundingMode:u}=n,l=1;y.assert(S.eitherStridesOrDilationsAreOne(i,l),()=> ` Error in avgPool : Either strides or dilations must be 1. Got strides $ { i } and dilations '${l}' ` );let c=S.computePool2DInfo(o.shape,s,i,l,a,u);if(c.filterWidth===1&&c.filterHeight===1&&y.arraysEqual(c.inShape,c.outShape))return nr({inputs:{x:o},backend:e});let p=new Ni(c,"avg",!1);return e.runWebGLProgram(p,[o],"float32")}var A3={kernelName:Yo,backendName:"webgl",kernelFunc:Nst};function kst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{filterSize:s,strides:i,pad:a,dimRoundingMode:u,dataFormat:l}=n,c=[1,1,1],p=S.computePool3DInfo(o.shape,s,i,c,a,u,l),m=new qu(p,"avg",!1);return e.runWebGLProgram(m,[o],"float32")}var D3={kernelName:Ri,backendName:"webgl",kernelFunc:kst};var gI=class{constructor(t){this.variableNames=["dy"],this.outputShape=t.inShape;let e=t.filterHeight,n=t.filterWidth,o=t.strideHeight,s=t.strideWidth,i=t.dilationHeight,a=t.dilationWidth,u=t.effectiveFilterHeight,l=t.effectiveFilterWidth,c=u-1-t.padInfo.top,p=l-1-t.padInfo.left,m=1/(e*n);this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 pads = ivec2 ( $ { c } , $ { p } ) ;
const float avgMultiplier = float ( $ { m } ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:44:24 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:44:24 +01:00
ivec2 dyRCCorner = coords . yz - pads ;
int dyRCorner = dyRCCorner . x ;
int dyCCorner = dyRCCorner . y ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:44:24 +01:00
// 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 < $ { u } ;
wR += $ { i } ) {
float dyR = float ( dyRCorner + wR ) / $ { o } . 0 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
2024-01-17 22:44:24 +01:00
continue ;
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:44:24 +01:00
int idyR = int ( dyR ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { l } ;
wC += $ { a } ) {
float dyC = float ( dyCCorner + wC ) / $ { s } . 0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
2024-01-17 22:44:24 +01:00
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:44:24 +01:00
float dyValue = getDy ( b , idyR , idyC , d ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
dotProd += dyValue * avgMultiplier ;
2024-01-17 22:44:24 +01:00
}
}
setOutput ( dotProd ) ;
2022-10-18 13:10:36 +02:00
}
2024-09-10 17:30:23 +02:00
` }},xI=class{constructor(t){this.variableNames=["dy"],this.outputShape=t.inShape;let e=t.filterDepth,n=t.filterHeight,o=t.filterWidth,s=t.strideDepth,i=t.strideHeight,a=t.strideWidth,u=t.dilationDepth,l=t.dilationHeight,c=t.dilationWidth,p=t.effectiveFilterDepth,m=t.effectiveFilterHeight,f=t.effectiveFilterWidth,d=p-1-t.padInfo.front,h=m-1-t.padInfo.top,g=f-1-t.padInfo.left,x=1/(e*n*o);this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec3 pads = ivec3 ( $ { d } , $ { h } , $ { g } ) ;
const float avgMultiplier = float ( $ { x } ) ;
2024-01-17 22:44:24 +01:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:44:24 +01:00
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
ivec3 dyCorner = ivec3 ( coords . y , coords . z , coords . w ) - pads ;
int dyDCorner = dyCorner . x ;
int dyRCorner = dyCorner . y ;
int dyCCorner = dyCorner . z ;
2024-01-17 22:56:53 +01:00
// Convolve dy(?, ?, ?, d) with pos mask(:, :, :, ch) to get
2024-01-17 22:44:24 +01:00
// dx(xD, xR, xC, ch).
2022-10-18 13:23:49 +02:00
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
for ( int wD = 0 ; wD < $ { p } ;
wD += $ { u } ) {
float dyD = float ( dyDCorner + wD ) / $ { s } . 0 ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( dyD < 0.0 || dyD >= $ { t . outDepth } . 0 || fract ( dyD ) > 0.0 ) {
2024-01-17 22:44:24 +01:00
continue ;
}
int idyD = int ( dyD ) ;
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { m } ;
wR += $ { l } ) {
float dyR = float ( dyRCorner + wR ) / $ { i } . 0 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 ||
2024-01-17 22:44:24 +01:00
fract ( dyR ) > 0.0 ) {
2022-10-18 13:23:49 +02:00
continue ;
}
2024-01-17 22:44:24 +01:00
int idyR = int ( dyR ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { f } ;
wC += $ { c } ) {
float dyC = float ( dyCCorner + wC ) / $ { a } . 0 ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
2024-01-17 22:44:24 +01:00
fract ( dyC ) > 0.0 ) {
2022-10-18 13:23:49 +02:00
continue ;
}
2024-01-17 22:44:24 +01:00
int idyC = int ( dyC ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:44:24 +01:00
float dyValue = getDy ( batch , idyD , idyR , idyC , ch ) ;
2024-01-17 22:56:53 +01:00
dotProd += dyValue * avgMultiplier ;
2022-10-18 13:23:49 +02:00
}
}
}
setOutput ( dotProd ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Tst(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,input:s}=t,i=s,{filterSize:a,strides:u,pad:l,dimRoundingMode:c}=n,p=[1,1,1],m=S.computePool3DInfo(i.shape,a,u,p,l,c),f=new xI(m);return e.runWebGLProgram(f,[o],i.dtype)}var $ 3={kernelName:Hl,backendName:"webgl",kernelFunc:Tst};function _st(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,input:s}=t,i=s;vi([o,s],"avgPoolGrad");let{filterSize:a,strides:u,pad:l}=n,c=S.computePool2DInfo(i.shape,a,u,1,l),p=new gI(c);return e.runWebGLProgram(p,[o],i.dtype)}var R3={kernelName:Ul,backendName:"webgl",kernelFunc:_st};function Est(r){let{inputs:t,backend:e,attrs:n}=r,{a:o,b:s}=t,{transposeA:i,transposeB:a}=n;return dp({a:o,b:s,transposeA:i,transposeB:a,backend:e})}var F3={kernelName:Zo,backendName:"webgl",kernelFunc:Est};var yI=class{constructor(t,e,n,o,s,i){this.outputShape=[],this.variableNames=["x","mean","variance"],S.assertAndGetBroadcastShape(t,e),S.assertAndGetBroadcastShape(t,n);let a="0.0";o!=null&&(S.assertAndGetBroadcastShape(t,o),this.variableNames.push("offset"),a="getOffsetAtOutCoords()");let u="1.0";s!=null&&(S.assertAndGetBroadcastShape(t,s),this.variableNames.push("scale"),u="getScaleAtOutCoords()"),this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
float x = getXAtOutCoords ( ) ;
float mean = getMeanAtOutCoords ( ) ;
float variance = getVarianceAtOutCoords ( ) ;
float offset = $ { a } ;
float scale = $ { u } ;
float inv = scale * inversesqrt ( variance + float ( $ { i } ) ) ;
setOutput ( dot ( vec3 ( x , - mean , offset ) , vec3 ( inv , inv , 1 ) ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var bI=class{constructor(t,e,n,o,s,i){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],S.assertAndGetBroadcastShape(t,e),S.assertAndGetBroadcastShape(t,n);let a="vec4(0.0)";o!=null&&(S.assertAndGetBroadcastShape(t,o),this.variableNames.push("offset"),a="getOffsetAtOutCoords()");let u="vec4(1.0)";s!=null&&(S.assertAndGetBroadcastShape(t,s),this.variableNames.push("scale"),u="getScaleAtOutCoords()"),this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
vec4 offset = $ { a } ;
vec4 scale = $ { u } ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec4 x = getXAtOutCoords ( ) ;
vec4 mean = getMeanAtOutCoords ( ) ;
vec4 variance = getVarianceAtOutCoords ( ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec4 inv = scale * inversesqrt ( variance + vec4 ( $ { i } ) ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( ( x - mean ) * inv + offset ) ;
}
2024-09-10 17:30:23 +02:00
` }};var Ast=({inputs:r,backend:t,attrs:e})=>{let{x:n,mean:o,variance:s,offset:i,scale:a}=r;y.assert(o.shape.length===s.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),y.assert(i==null||o.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),y.assert(a==null||o.shape.length===a.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:u}=e;u==null&&(u=.001);let l=[n,o,s],c=null;i!=null&&(c=i.shape,l.push(i));let p=null;a!=null&&(p=a.shape,l.push(a));let m=L().getBool("WEBGL_PACK_NORMALIZATION")?new bI(n.shape,o.shape,s.shape,c,p,u):new yI(n.shape,o.shape,s.shape,c,p,u);return t.runWebGLProgram(m,l,l[0].dtype)},O3={kernelName:ds,backendName:"webgl",kernelFunc:Ast};var wI=class{constructor(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;let e=zt(this.rank);this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let n=Dst(this.rank),o,s=t.map((i,a)=> ` sourceLoc . $ { O1 [ a ] } = start [ $ { a } ] + coords . $ { O1 [ a ] } ; ` );o= `
2024-01-17 22:56:53 +01:00
$ { e } sourceLoc ;
$ { e } coords = getOutputCoords ( ) ;
$ { s . join ( `
` )}
` ,this.userCode= `
void main ( ) {
$ { o }
setOutput ( getSource ( $ { n } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }},O1=["x","y","z","w","u","v"];function Dst(r){if(r===1)return"sourceLoc";if(r<=6)return O1.slice(0,r).map(t=>"sourceLoc."+t).join(",");throw Error( ` Slicing for rank $ { r } is not yet supported ` )}var II=class{constructor(t){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.rank=t.length,this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let e=zt(this.rank),n=rr("coords",this.rank),o=rr("sourceLoc",this.rank),s=this.rank===1?"sourceLoc": ` vec2 ( $ { o . slice ( - 2 ) . join ( ) } ) ` ,i= ` getChannel ( getSource ( $ { o . join ( ) } ) , $ { s } ) ` ,a= `
2024-01-17 22:56:53 +01:00
result . x = $ { i } ;
if ( ++ $ { n [ this . rank - 1 ] } < $ { t [ this . rank - 1 ] } ) {
++ $ { o [ this . rank - 1 ] } ;
result . y = $ { i } ;
-- $ { o [ this . rank - 1 ] } ;
}
` ,u=this.rank===1?"": `
-- $ { n [ this . rank - 1 ] } ;
if ( ++ $ { n [ this . rank - 2 ] } < $ { t [ this . rank - 2 ] } ) {
++ $ { o [ this . rank - 2 ] } ;
result . z = $ { i } ;
if ( ++ $ { n [ this . rank - 1 ] } < $ { t [ this . rank - 1 ] } ) {
++ $ { o [ this . rank - 1 ] } ;
result . w = $ { i } ;
}
}
` ,l=this.rank<=4? ` sourceLoc = coords +
$ { e } ( $ { t . map ( ( c , p ) => ` start[ ${ p } ] ` ) . join ( ) } ) ; ` :t.map((c,p)=> ` $ { o [ p ] } = $ { n [ p ] } + start [ $ { p } ] ; ` ).join( `
` );this.userCode= `
void main ( ) {
$ { e } coords = getOutputCoords ( ) ;
$ { e } sourceLoc ;
$ { l }
vec4 result = vec4 ( 0. ) ;
$ { a }
$ { u }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function $ st(r,t,e,n){let o=n.texData.get(r.dataId),s=n.makeTensorInfo(e,r.dtype),i=n.texData.get(s.dataId);Object.assign(i,o),i.refCount=1,i.shape=e,i.dtype=r.dtype;let a=Be.computeFlatOffset(t,y.computeStrides(r.shape));o.slice&&(a+=o.slice.flatOffset),i.slice={flatOffset:a,origDataId:o.slice&&o.slice.origDataId||r.dataId};let u=n.dataRefCount.get(i.slice.origDataId)||1;return n.dataRefCount.set(i.slice.origDataId,u+1),s}function ki(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{begin:s,size:i}=n,[a,u]=Be.parseSliceParams(o,s,i);if(Be.assertParamsValid(o,a,u),y.sizeFromShape(u)===0)return e.makeTensorInfo(u,o.dtype,[]);if(e.shouldExecuteOnCPU([o])||o.dtype==="string"){let p=e.texData.get(o.dataId),m=Sz(p.values,a,u,o.shape,o.dtype);return e.makeTensorInfo(u,o.dtype,m)}let{isPacked:l}=e.texData.get(o.dataId),c=Be.isSliceContinous(o.shape,a,u);if(l||!c){let p=L().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new II(u):new wI(u),m=[a];return e.runWebGLProgram(p,[o],o.dtype,m)}return e.uploadToGPU(o.dataId), $ st(o,a,u,e)}var M3={kernelName:Ui,backendName:"webgl",kernelFunc:ki};var Rst=r=>{let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{blockShape:s,crops:i}=n;y.assert(o.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let a=s.reduce((b,w)=>b*w),u=S.getReshaped(o.shape,s,a),l=S.getPermuted(u.length,s.length),c=S.getReshapedPermuted(o.shape,s,a),p=S.getSliceBeginCoords(i,s.length),m=S.getSliceSize(c,i,s.length),f=[],d=rt({inputs:{x:o},backend:e,attrs:{shape:u}}),h=Pe({inputs:{x:d},backend:e,attrs:{perm:l}}),g=rt({inputs:{x:h},backend:e,attrs:{shape:c}}),x=ki({inputs:{x:g},backend:e,attrs:{begin:p,size:m}});return f.push(d),f.push(h),f.push(g),f.forEach(b=>e.disposeIntermediateTensorInfo(b)),x},P3={kernelName:Fi,backendName:"webgl",kernelFunc:Rst};function Fst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,weights:s}=t,{size:i}=n,a=e.readSync(o.dataId),u=e.readSync(s.dataId),l=tI(a,u,s.dtype,s.shape,i);return e.makeTensorInfo([i],s.dtype,l)}var L3={kernelName:Da,backendName:"webgl",kernelFunc:Fst};var Ost= `
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
` ,Mst= `
2024-01-17 22:56:53 +01:00
return float ( int ( a . r ) & int ( b . r ) ) ;
2024-09-10 17:30:23 +02:00
` ;function Pst(r){let{inputs:t,backend:e}=r,{a:n,b:o}=t,s=L().getBool("WEBGL_PACK_BINARY_OPERATIONS"),i=L().getNumber("WEBGL_VERSION");if(e.shouldExecuteOnCPU([n,o])||i===1){let u=e.texData.get(n.dataId).values,l=e.texData.get(o.dataId).values,[c,p]=jL(n.shape,o.shape,u,l,n.dtype),m=e.makeTensorInfo(p,n.dtype),f=e.texData.get(m.dataId);return f.values=c,m}let a;return s?a=new jn(Ost,n.shape,o.shape,!1):a=new $ n(Mst,n.shape,o.shape),e.runWebGLProgram(a,[n,o],n.dtype)}var z3={kernelName: $ a,backendName:"webgl",kernelFunc:Pst};function Lst(r){let{inputs:t,backend:e}=r,{s0:n,s1:o}=t,s=e.readSync(n.dataId),i=e.readSync(o.dataId),a=S.assertAndGetBroadcastShape(Array.from(s),Array.from(i));return e.makeTensorInfo([a.length],"int32",Int32Array.from(a))}var B3={kernelName:ql,backendName:"webgl",kernelFunc:Lst};var zst="return float(a != b);",M1=ce({opSnippet:zst,cpuKernelImpl:hz,dtype:"bool"}),V3={kernelName:Za,backendName:"webgl",kernelFunc:M1};function Pl(r){let{inputs:t,backend:e}=r,{input:n}=t,o=e.texData.get(n.dataId);return nr({inputs:{x:o.complexTensorInfos.real},backend:e})}var G3={kernelName:Vp,backendName:"webgl",kernelFunc:Pl};var Bst="return float(int(x));";function W3(r,t){let e=new zr(r.shape,Bst),n=t.runWebGLProgram(e,[r],"int32");return{dataId:n.dataId,shape:n.shape,dtype:n.dtype}}function P1(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{dtype:s}=n;if(s==="complex64"){if(o.dtype==="complex64")return nr({inputs:{x:o},backend:e});let i=ke(o.shape),a=P1({inputs:{x:o},backend:e,attrs:{dtype:"float32"}}),u=Rn({inputs:{real:a,imag:i},backend:e});return i.dispose(),e.disposeIntermediateTensorInfo(a),u}if(o.dtype==="complex64"){let i=Pl({inputs:{input:o},backend:e}),a=P1({inputs:{x:i},backend:e,attrs:{dtype:s}});return e.disposeIntermediateTensorInfo(i),a}if(!y.hasEncodingLoss(o.dtype,s)){let i=nr({inputs:{x:o},backend:e});return{dataId:i.dataId,shape:i.shape,dtype:s}}if(e.shouldExecuteOnCPU([o])){let i=e.texData.get(o.dataId).values,[a,u,l]=XL(i,o.shape,o.dtype,s);return e.makeTensorInfo(a,u,l)}if(s==="int32")return W3(o,e);if(s==="bool"){let i=e.makeTensorInfo([],"bool",y.getTypedArrayFromDType("bool",1)),u=M1({inputs:{a:o,b:i},backend:e});return e.disposeIntermediateTensorInfo(i),u}throw new Error( ` Error in Cast : failed to cast $ { o . dtype } to $ { s } ` )}var U3={kernelName:fo,backendName:"webgl",kernelFunc:P1};var H3="return ceil(x);",Vst=It({opSnippet:H3,packedOpSnippet:H3,cpuKernelImpl:YL}),q3={kernelName:Jo,backendName:"webgl",kernelFunc:Vst};var CI=class{constructor(t){this.variableNames=["A"],this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=t,this.userCode= `
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
float value = getAAtOutCoords ( ) ;
if ( isnan ( value ) ) {
setOutput ( value ) ;
return ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
setOutput ( clamp ( value , minVal , maxVal ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};var vI=class{constructor(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
vec4 value = getAAtOutCoords ( ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( any ( isnan ( value ) ) ) {
setOutput ( value ) ;
return ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( clamp ( value , vec4 ( minVal ) , vec4 ( maxVal ) ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Gst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{clipValueMin:s,clipValueMax:i}=n,a;L().getBool("WEBGL_PACK_CLIP")?a=new vI(o.shape):a=new CI(o.shape);let u=[[s],[i]];return e.runWebGLProgram(a,[o],o.dtype,u)}var K3={kernelName:ho,backendName:"webgl",kernelFunc:Gst};var SI=class{constructor(t){this.variableNames=["real","imag"],this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
float re = abs ( getRealAtOutCoords ( ) ) ;
float im = abs ( getImagAtOutCoords ( ) ) ;
float mx = max ( re , im ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
` }};function j3(r,t){return{dataId:t.dataId,dtype:t.dtype,shape:r.shape}}function Wst(r){let{inputs:t,backend:e}=r,{x:n}=t,o=e.texData.get(n.dataId),s=new SI(n.shape),i=[j3(n,o.complexTensorInfos.real),j3(n,o.complexTensorInfos.imag)];return e.runWebGLProgram(s,i,i[0].dtype)}var X3={kernelName:Kl,backendName:"webgl",kernelFunc:Wst};var NI=class{constructor(t){this.outputShape=[],this.outputShape=S.computeOutShape(t,1),this.variableNames=t.map((i,a)=> ` T$ { a } ` );let e=new Array(t.length-1);e[0]=t[0][1];for(let i=1;i<e.length;i++)e[i]=e[i-1]+t[i][1];let n=[ ` if ( yC < $ { e [ 0 ] } ) setOutput ( getT0 ( yR , yC ) ) ; ` ];for(let i=1;i<e.length;i++){let a=e[i-1];n.push( ` else if ( yC < $ { e [ i ] } ) setOutput ( getT$ { i } ( yR , yC - $ { a } ) ) ; ` )}let o=e.length,s=e[e.length-1];n.push( ` else setOutput ( getT$ { o } ( yR , yC - $ { s } ) ) ; ` ),this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int yR = coords . x ;
int yC = coords . y ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { n . join ( `
` )}
}
2024-09-10 17:30:23 +02:00
` }};var TI=class{constructor(t,e){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=S.computeOutShape(t,e);let n=this.outputShape,o=n.length,s=zt(o),i=rr("coords",o),a=["x","y","z","w","u","v"].slice(0,o);this.variableNames=t.map((h,g)=> ` T$ { g } ` );let u=new Array(t.length-1);u[0]=t[0][e];for(let h=1;h<u.length;h++)u[h]=u[h-1]+t[h][e];let l=a[e],c=a.slice(-2),p=a.join(),m= ` if ( $ { l } < $ { u [ 0 ] } ) {
2024-01-17 22:56:53 +01:00
return getChannel (
getT0 ( $ { p } ) , vec2 ( $ { c . join ( ) } ) ) ;
} ` ;for(let h=1;h<u.length;h++){let g=u[h-1];m+= `
if ( $ { l } < $ { u [ h ] } && $ { l } >= $ { u [ h - 1 ] } ) {
return getChannel (
2024-09-10 17:30:23 +02:00
getT$ { h } ( $ { kI ( a , l , g ) } ) ,
vec2 ( $ { kI ( c , l , g ) } ) ) ;
2024-01-17 22:56:53 +01:00
} ` }let f=u.length,d=u[u.length-1];m+= `
return getChannel (
2024-09-10 17:30:23 +02:00
getT$ { f } ( $ { kI ( a , l , d ) } ) ,
vec2 ( $ { kI ( c , l , d ) } ) ) ; ` ,this.userCode= `
2024-01-17 22:56:53 +01:00
float getValue ( $ { a . map ( h => "int " + h ) } ) {
$ { m }
}
2021-04-01 19:39:54 +02:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
$ { s } coords = getOutputCoords ( ) ;
vec4 result = vec4 ( getValue ( $ { i } ) , 0. , 0. , 0. ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { i [ o - 1 ] } = $ { i [ o - 1 ] } + 1 ;
if ( $ { i [ o - 1 ] } < $ { n [ o - 1 ] } ) {
result . g = getValue ( $ { i } ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
$ { i [ o - 2 ] } = $ { i [ o - 2 ] } + 1 ;
if ( $ { i [ o - 2 ] } < $ { n [ o - 2 ] } ) {
result . a = getValue ( $ { i } ) ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { i [ o - 1 ] } = $ { i [ o - 1 ] } - 1 ;
if ( $ { i [ o - 2 ] } < $ { n [ o - 2 ] } &&
$ { i [ o - 1 ] } < $ { n [ o - 1 ] } ) {
result . b = getValue ( $ { i } ) ;
}
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function kI(r,t,e){let n=r.indexOf(t);return r.map((s,i)=>i===n? ` $ { s } - $ { e } ` :s).join()}function hp(r){let{inputs:t,backend:e}=r,{input:n}=t,o=e.texData.get(n.dataId);return nr({inputs:{x:o.complexTensorInfos.imag},backend:e})}var Y3={kernelName:Pp,backendName:"webgl",kernelFunc:hp};function Fd(r,t,e){let n=r[0].dtype;if(n==="complex64"){let f=r.map(b=>Pl({inputs:{input:b},backend:e})),d=r.map(b=>hp({inputs:{input:b},backend:e})),h=Fd(f,t,e),g=Fd(d,t,e),x=Rn({inputs:{real:h,imag:g},backend:e});return f.forEach(b=>e.disposeIntermediateTensorInfo(b)),d.forEach(b=>e.disposeIntermediateTensorInfo(b)),e.disposeIntermediateTensorInfo(h),e.disposeIntermediateTensorInfo(g),x}let o=e.shouldExecuteOnCPU(r);if(n==="string"&&(o=!0),o){let f=r.map(I=>{let E=[-1,y.sizeFromShape(I.shape.slice(t))];return rt({inputs:{x:I},backend:e,attrs:{shape:E}})}),d=f.map(I=>({vals:e.readSync(I.dataId),shape:I.shape})),h=S.computeOutShape(f.map(I=>I.shape),1),g=f[0].shape[0]===1,x=ZL(d,h,n,g),b=S.computeOutShape(r.map(I=>I.shape),t),w=e.makeTensorInfo(b,n,x);return f.forEach(I=>e.disposeIntermediateTensorInfo(I)),w}let s=r.filter(f=>y.sizeFromShape(f.shape)>0),i=L().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&s[0].shape.length>1;if(s.length===1){let f=i?new zr(r[0].shape,Ia):new Dn(r[0].shape,Ia);return e.runWebGLProgram(f,r,n)}let a=L().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER");if(s.length>a){let f=[];for(let h=0;h<s.length;h+=a){let g=s.slice(h,h+a);f.push(Fd(g,t,e))}let d=Fd(f,t,e);for(let h of f)e.disposeIntermediateTensorInfo(h);return d}if(i){let f=new TI(s.map(d=>d.shape),t);return e.runWebGLProgram(f,s,n)}let{tensors2D:u,outShape:l}=Ust(s,t,e),c=new NI(u.map(f=>f.shape)),p=e.runWebGLProgram(c,u,n);u.forEach(f=>e.disposeIntermediateTensorInfo(f));let m=rt({inputs:{x:p},attrs:{shape:l},backend:e});return e.disposeIntermediateTensorInfo(p),m}function Ust(r,t,e){let n=S.computeOutShape(r.map(s=>s.shape),t);return{tensors2D:r.map(s=>rt({inputs:{x:s},attrs:{shape:[-1,y.sizeFromShape(s.shape.slice(t))]},backend:e})),outShape:n}}function L1(r){let{inputs:t,backend:e,attrs:n}=r,{axis:o}=n,s=y.parseAxisParam(o,t[0].shape)[0],i=t.map(l=>l.shape);S.assertParamsConsistent(i,s);let a=S.computeOutShape(t.map(l=>l.shape),s);if(y.sizeFromShape(a)===0)return e.makeTensorInfo(a,t[0].dtype,[]);let u=t.filter(l=>y.sizeFromShape(l.shape)>0);return u.length===1?nr({inputs:{x:u[0]},backend:e}):Fd(u,s,e)}var Z3={kernelName:Oi,backendName:"webgl",kernelFunc:L1};var Od=class{constructor(t,e=!1,n=null,o=!1,s=!1){this.variableNames=["x","W"],this.outputShape=t.outShape;let i=t.padInfo.top,a=t.padInfo.left,u=t.strideHeight,l=t.strideWidth,c=t.dilationHeight,p=t.dilationWidth,m=t.filterHeight,f=t.filterWidth,d=Math.floor(t.inChannels/4)*4,h=t.inChannels%4,g=t.dataFormat==="channelsLast",x=g?1:2,b=g?2:3,w=g?3:1,I="",N="";n&&(o?I= ` float activation ( float a ) {
2024-01-17 22:56:53 +01:00
float b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
} ` :s?I= ` float activation ( float a ) {
float b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
} ` :I= `
float activation ( float x ) {
$ { n }
}
` ,N="result = activation(result);");let E=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { I }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
const ivec2 strides = ivec2 ( $ { u } , $ { l } ) ;
const ivec2 pads = ivec2 ( $ { i } , $ { a } ) ;
2024-01-17 22:44:24 +01:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec4 coords = getOutputCoords ( ) ;
2022-10-18 13:23:49 +02:00
int batch = coords [ 0 ] ;
2024-01-17 22:56:53 +01:00
int d2 = coords [ $ { w } ] ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
ivec2 xRCCorner =
ivec2 ( coords [ $ { x } ] , coords [ $ { b } ] ) * strides - pads ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01: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 ;
for ( int wR = 0 ; wR < $ { m } ; wR ++ ) {
int xR = xRCorner + wR * $ { c } ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { f } ; wC ++ ) {
int xC = xCCorner + wC * $ { p } ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int d1 = 0 ; d1 < $ { d } ; 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 ( $ { g } ) {
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-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( $ { h === 1 } ) {
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( $ { g } ) {
dotProd +=
getX ( batch , xR , xC , $ { d } ) *
getW ( wR , wC , $ { d } , d2 ) ;
} else {
dotProd +=
getX ( batch , $ { d } , xR , xC ) *
getW ( wR , wC , $ { d } , d2 ) ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
} else if ( $ { h === 2 } ) {
vec2 wValues = vec2 (
getW ( wR , wC , $ { d } , d2 ) ,
getW ( wR , wC , $ { d } + 1 , d2 )
) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( $ { g } ) {
vec2 xValues = vec2 (
getX ( batch , xR , xC , $ { d } ) ,
getX ( batch , xR , xC , $ { d } + 1 )
) ;
dotProd += dot ( xValues , wValues ) ;
} else {
vec2 xValues = vec2 (
getX ( batch , $ { d } , xR , xC ) ,
getX ( batch , $ { d } + 1 , xR , xC )
) ;
dotProd += dot ( xValues , wValues ) ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
} else if ( $ { h === 3 } ) {
vec3 wValues = vec3 (
getW ( wR , wC , $ { d } , d2 ) ,
getW ( wR , wC , $ { d } + 1 , d2 ) ,
getW ( wR , wC , $ { d } + 2 , d2 )
) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( $ { g } ) {
vec3 xValues = vec3 (
getX ( batch , xR , xC , $ { d } ) ,
getX ( batch , xR , xC , $ { d } + 1 ) ,
getX ( batch , xR , xC , $ { d } + 2 )
) ;
dotProd += dot ( xValues , wValues ) ;
} else {
vec3 xValues = vec3 (
getX ( batch , $ { d } , xR , xC ) ,
getX ( batch , $ { d } + 1 , xR , xC ) ,
getX ( batch , $ { d } + 2 , xR , xC )
) ;
dotProd += dot ( xValues , wValues ) ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
}
}
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float result = dotProd ;
$ { E }
$ { N }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }},_I=class{constructor(t){this.variableNames=["x","W"],this.outputShape=t.outShape;let e=t.padInfo.front,n=t.padInfo.top,o=t.padInfo.left,s=t.strideDepth,i=t.strideHeight,a=t.strideWidth,u=t.dilationDepth,l=t.dilationHeight,c=t.dilationWidth,p=t.filterDepth,m=t.filterHeight,f=t.filterWidth,d=Math.floor(t.inChannels/4)*4,h=t.inChannels%4;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec3 strides = ivec3 ( $ { s } , $ { i } , $ { a } ) ;
const ivec3 pads = ivec3 ( $ { e } , $ { n } , $ { o } ) ;
2021-04-30 18:01:04 +02:00
2024-01-17 22:44:24 +01:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int d2 = coords . u ;
2021-04-30 18:01:04 +02:00
2024-01-17 22:56:53 +01: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-30 18:01:04 +02:00
2024-01-17 22:56:53 +01: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 ;
for ( int wF = 0 ; wF < $ { p } ; wF ++ ) {
int xF = xFCorner + wF * $ { u } ;
2021-04-30 18:01:04 +02:00
2024-01-17 22:56:53 +01:00
if ( xF < 0 || xF >= $ { t . inDepth } ) {
continue ;
}
2021-04-30 18:01:04 +02:00
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { m } ; wR ++ ) {
int xR = xRCorner + wR * $ { l } ;
2021-04-30 18:01:04 +02:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { f } ; wC ++ ) {
int xC = xCCorner + wC * $ { c } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
}
for ( int d1 = 0 ; d1 < $ { d } ; d1 += 4 ) {
vec4 xValues = vec4 (
getX ( batch , xF , xR , xC , d1 ) ,
getX ( batch , xF , xR , xC , d1 + 1 ) ,
getX ( batch , xF , xR , xC , d1 + 2 ) ,
getX ( batch , xF , xR , xC , d1 + 3 )
) ;
vec4 wValues = vec4 (
getW ( wF , wR , wC , d1 , d2 ) ,
getW ( wF , wR , wC , d1 + 1 , d2 ) ,
getW ( wF , wR , wC , d1 + 2 , d2 ) ,
getW ( wF , wR , wC , d1 + 3 , d2 )
) ;
dotProd += dot ( xValues , wValues ) ;
}
if ( $ { h === 1 } ) {
dotProd +=
getX ( batch , xF , xR , xC , $ { d } ) *
getW ( wF , wR , wC , $ { d } , d2 ) ;
} else if ( $ { h === 2 } ) {
vec2 xValues = vec2 (
getX ( batch , xF , xR , xC , $ { d } ) ,
getX ( batch , xF , xR , xC , $ { d } + 1 )
) ;
vec2 wValues = vec2 (
getW ( wF , wR , wC , $ { d } , d2 ) ,
getW ( wF , wR , wC , $ { d } + 1 , d2 )
) ;
dotProd += dot ( xValues , wValues ) ;
} else if ( $ { h === 3 } ) {
vec3 xValues = vec3 (
getX ( batch , xF , xR , xC , $ { d } ) ,
getX ( batch , xF , xR , xC , $ { d } + 1 ) ,
getX ( batch , xF , xR , xC , $ { d } + 2 )
) ;
vec3 wValues = vec3 (
getW ( wF , wR , wC , $ { d } , d2 ) ,
getW ( wF , wR , wC , $ { d } + 1 , d2 ) ,
getW ( wF , wR , wC , $ { d } + 2 , d2 )
) ;
dotProd += dot ( xValues , wValues ) ;
}
}
}
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
setOutput ( dotProd ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};var Md=class{constructor(t,e=!1,n=null,o=!1,s=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=t.outShape,this.enableShapeUniforms=he(this.outputShape.length);let i=t.padInfo.left,a=t.strideWidth,u=t.dilationWidth,l=t.filterHeight,c=t.filterWidth,p=c,m= `
2024-01-17 22:56:53 +01:00
int xR ; int xC ; int xCOffset ;
vec4 wTexel ; vec4 previous ; vec4 final ; ` ;for(let g=0;g<c;g++)m+= `
vec4 xTexelC$ { g * 2 } ;
int xTexelC$ { g * 2 } Ready ;
vec4 xTexelC$ { g * 2 + 1 } ;
int xTexelC$ { g * 2 + 1 } Ready ;
vec4 xC$ { g } ; ` ;m+= `
for ( int r = 0 ; r < $ { l } ; r ++ ) {
for ( int d1 = 0 ; d1 < $ { t . inChannels } ; d1 += 2 ) {
` ;for(let g=0;g<c;g++)m+= `
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 ) ; ` ;m+= `
xR = xRCorner + r * dilations [ 0 ] ;
if ( xR >= 0 && xR < inDims [ 0 ] ) {
` ;for(let g=0;g<(p+1)/2;g++){let x=g*2;if(m+= `
xC = xCCorner + $ { x * u } ;
` ,a===1){if(x<c&&(i%2===1?(m+= `
xCOffset = xC + 1 ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { x } Ready == 0 ) {
xTexelC$ { x } = getX ( batch , xR , xCOffset , d1 ) ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { x } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x } Ready = 1 ;
}
` ,u===1&&x>0?m+= `
xC$ { x } = vec4 ( xTexelC$ { x - 2 } . zw , xTexelC$ { x } . xy ) ;
` :m+= `
xCOffset = xC + 1 - 2 ;
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] ) {
previous = getX ( batch , xR , xCOffset , d1 ) ;
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
previous . zw = vec2 ( 0.0 ) ;
}
xC$ { x } = vec4 ( previous . zw , xTexelC$ { x } . xy ) ;
} else {
xC$ { x } = vec4 ( 0.0 , 0.0 , xTexelC$ { x } . xy ) ;
}
` ):m+= `
if ( xC >= 0 && xC < inDims [ 1 ] && xTexelC$ { x } Ready == 0 ) {
xTexelC$ { x } = getX ( batch , xR , xC , d1 ) ;
if ( xC + 1 >= inDims [ 1 ] ) {
xTexelC$ { x } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x } Ready = 1 ;
}
xC$ { x } = xTexelC$ { x } ;
` ,x+1<c)){let b=i%2===0?y.nearestLargerEven(u):u;u%2===0&&i%2===1||u%2!==0&&i%2!==1?(m+= `
xCOffset = xC + imod ( pads [ 1 ] , 2 ) + $ { b } ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { x + 1 } Ready == 0 ) {
xTexelC$ { x + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { x + 1 } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x + 1 } Ready = 1 ;
}
` ,u>1?m+= `
xCOffset -= 2 ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] ) {
previous = getX ( batch , xR , xCOffset , d1 ) ;
xC$ { x + 1 } = vec4 ( previous . zw , xTexelC$ { x + 1 } . xy ) ;
} else {
xC$ { x + 1 } = vec4 ( 0.0 , 0.0 , xTexelC$ { x + 1 } . xy ) ;
}
` :m+= `
xC$ { x + 1 } = vec4 ( xTexelC$ { x } . zw , xTexelC$ { x + 1 } . xy ) ;
` ):b===1?m+= `
xC$ { x + 1 } = xTexelC$ { x } ;
` :m+= `
xCOffset = xC + $ { b } ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { x + 1 } Ready == 0 ) {
xTexelC$ { x + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { x + 1 } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x + 1 } Ready = 1 ;
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
xC$ { x + 1 } = xTexelC$ { x + 1 } ;
` }}else x<c&&(i%2===1?(m+= `
xCOffset = xC + 1 - strides [ 1 ] ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { x } Ready == 0 ) {
xTexelC$ { x } = getX ( batch , xR , xCOffset , d1 ) ;
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { x } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x } Ready = 1 ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
if ( xC + 1 >= 0 && xC + 1 < inDims [ 1 ] && xTexelC$ { x + 1 } Ready == 0 ) {
xTexelC$ { x + 1 } = getX ( batch , xR , xC + 1 , d1 ) ;
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xC + 2 >= inDims [ 1 ] ) {
xTexelC$ { x + 1 } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x + 1 } Ready = 1 ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
xC$ { x } = vec4 ( xTexelC$ { x } . zw , xTexelC$ { x + 1 } . zw ) ;
` ,x+1<c&&(m+= `
final = vec4 ( 0.0 ) ;
xCOffset = xC + 1 + strides [ 1 ] ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] ) {
final = getX ( batch , xR , xCOffset , d1 ) ;
}
xC$ { x + 1 } = vec4 ( xTexelC$ { x + 1 } . xy , final . xy ) ;
` )):(m+= `
if ( xC >= 0 && xC < inDims [ 1 ] && xTexelC$ { x } Ready == 0 ) {
xTexelC$ { x } = getX ( batch , xR , xC , d1 ) ;
if ( xC + 1 >= inDims [ 1 ] ) {
xTexelC$ { x } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { x } Ready = 1 ;
}
2021-04-01 19:39:54 +02:00
2024-01-17 22:56:53 +01:00
xCOffset = xC + strides [ 1 ] ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { x + 1 } Ready == 0 ) {
xTexelC$ { x + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { x + 1 } . zw = vec2 ( 0. ) ;
}
xTexelC$ { x + 1 } Ready = 1 ;
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
xC$ { x } = vec4 (
xTexelC$ { x } . xy , xTexelC$ { x + 1 } . xy ) ;
` ,x+1<c&&(m+= `
xC$ { x + 1 } = vec4 ( xTexelC$ { x } . zw , xTexelC$ { x + 1 } . zw ) ;
` )));x<c&&(m+= `
wTexel = getW ( r , $ { x } , d1 , d2 ) ;
dotProd += xC$ { x } . xxzz * vec4 ( wTexel . xy , wTexel . xy ) ;
if ( d1 + 1 < $ { t . inChannels } ) {
dotProd += xC$ { x } . yyww * vec4 ( wTexel . zw , wTexel . zw ) ;
}
` ,x+1<c&&(m+= `
wTexel = getW ( r , $ { x + 1 } , d1 , d2 ) ;
dotProd += xC$ { x + 1 } . xxzz * vec4 ( wTexel . xy , wTexel . xy ) ;
if ( d1 + 1 < $ { t . inChannels } ) {
dotProd += xC$ { x + 1 } . yyww * vec4 ( wTexel . zw , wTexel . zw ) ;
}
` ))}m+= `
}
` ,m+= `
}
` ,m+= `
}
` ;let f="",d="";n&&(o?f= ` vec4 activation ( vec4 a ) {
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
} ` :s?f= ` vec4 activation ( vec4 a ) {
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
} ` :f= ` vec4 activation ( vec4 x ) {
$ { n }
} ` ,d="result = activation(result);");let h=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { f }
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01: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 ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
//intialize dotProd with a small epsilon seems to reduce GPU accuracy loss.
vec4 dotProd = vec4 ( 0.000000000000001 ) ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
$ { m }
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
vec4 result = dotProd - vec4 ( 0.000000000000001 ) ;
$ { h }
$ { d }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var EI=class{constructor(t,e){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=t,this.enableShapeUniforms=he(this.outputShape.length);let{dataFormat:n}=e,o=Ue(),s=n==="channelsLast",i=s?1:2,a=s?2:3,u=this.enableShapeUniforms?"if(blockIndex < outShape[2] && pos < outShape[1]) {": ` if ( blockIndex < $ { t [ 2 ] } && pos < $ { t [ 1 ] } ) { ` ,l="";for(let c=0;c<=1;c++)for(let p=0;p<=1;p++)l+= `
2024-01-17 22:56:53 +01:00
blockIndex = rc . z + $ { p } ;
pos = rc . y + $ { c } ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
$ { u }
offsetY = int ( blockIndex / outWidth ) * stride [ 0 ] - pad [ 0 ] ;
d0 = offsetY + dilation [ 0 ] * ( pos / itemsPerBlockRow ) ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
if ( d0 < inputShape [ $ { i } ] && d0 >= 0 ) {
// Use custom imod instead mod. On Intel GPU, mod may generate
// unexpected value.
// https://github.com/tensorflow/tfjs/issues/5447
offsetX = imod ( blockIndex , outWidth ) * stride [ 1 ] - pad [ 1 ] ;
d1 = offsetX + dilation [ 1 ] * ( imod ( pos , itemsPerBlockRow ) /
inChannels ) ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
if ( d1 < inputShape [ $ { a } ] && d1 >= 0 ) {
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
ch = imod ( pos , inChannels ) ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
if ( $ { s } ) {
innerDims = vec2 ( d1 , ch ) ;
result [ $ { c * 2 + p } ] = getChannel (
getA ( rc . x , d0 , int ( innerDims . x ) ,
int ( innerDims . y ) ) , innerDims ) ;
} else {
innerDims = vec2 ( d0 , d1 ) ;
result [ $ { c * 2 + p } ] = getChannel (
getA ( rc . x , ch , int ( innerDims . x ) ,
int ( innerDims . y ) ) , innerDims ) ;
}
}
}
}
` ;this.userCode= `
void main ( ) {
ivec3 rc = getOutputCoords ( ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0 ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int blockIndex , pos , offsetY , d0 , offsetX , d1 , ch ;
vec2 innerDims ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { l }
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { o . output } = result ;
}
2024-09-10 17:30:23 +02:00
` }};function AI(r,t){let e=r.length;return e>=3?t?[...r.slice(0,-3),r[e-3]*r[e-2],r[e-1]]:[...r.slice(0,-3),r[e-3],r[e-2]*r[e-1]]:!t&&e===1&&r[0]>1?[r[0],1]:null}function DI({x:r,filter:t,convInfo:e,backend:n,bias:o=null,preluActivationWeights:s=null,leakyreluAlpha:i=0,activation:a=null}){let u=r.shape,l=n.texData.get(r.dataId),c=e.inChannels,p=u[0]*u[1]*u[2],m=e.outChannels,f=e.dataFormat==="channelsLast",d=!1,h=!1,g,x=[];if(s!=null){let I=AI(s.shape,f);I!=null&&(s=rt({inputs:{x:s},backend:n,attrs:{shape:I}}),x.push(s))}if(o!=null){let I=AI(o.shape,f);I!=null&&(o=rt({inputs:{x:o},backend:n,attrs:{shape:I}}),x.push(o))}if(!((p===1||m===1)&&c>F1)&&l.isPacked&&f&&l.texture!=null&&u[2]%2!==0&&y.arraysEqual(l.shape.slice(-3),u.slice(-3))){let I=u[0]*u[1]*(u[2]+1),N={dataId:r.dataId,shape:[1,I,e.inChannels],dtype:r.dtype},E=l.shape;l.shape=l.shape.slice(),l.shape[l.shape.length-2]++,y.assert(Uu(l.shape,N.shape),()=> ` packed reshape $ { l . shape } to $ { N . shape } isn ' t free ` );let A=rt({inputs:{x:t},backend:n,attrs:{shape:[1,e.inChannels,e.outChannels]}});x.push(A);let D=dp({a:N,b:A,backend:n,transposeA:d,transposeB:h,bias:o,activation:a,preluActivationWeights:s,leakyreluAlpha:i}),F=n.texData.get(D.dataId);y.assert(F.isPacked,()=>"batchMatMul result is expected to be packed"),l.shape=E,F.shape=e.outShape,g=nr({inputs:{x:D},backend:n}),g.shape=e.outShape,x.push(D)}else{let I=e.outHeight*e.outWidth,N=rt({inputs:{x:r},backend:n,attrs:{shape:f?[e.batchSize,I,e.inChannels]:[e.batchSize,e.inChannels,I]}}),E=rt({inputs:{x:t},backend:n,attrs:{shape:[1,e.inChannels,e.outChannels]}}),A=dp({a:f?N:E,b:f?E:N,transposeA:!f,transposeB:h,backend:n,bias:o,activation:a,preluActivationWeights:s,leakyreluAlpha:i});g=rt({inputs:{x:A},backend:n,attrs:{shape:e.outShape}}),x.push(N),x.push(E),x.push(A)}for(let I of x)n.disposeIntermediateTensorInfo(I);return g}function $ I({x:r,filter:t,convInfo:e,backend:n,bias:o=null,preluActivationWeights:s=null,leakyreluAlpha:i=0,activation:a=null}){let{filterWidth:u,filterHeight:l,inChannels:c,outWidth:p,outHeight:m,dataFormat:f}=e,d=f==="channelsLast",h=u*l*c,g=m*p,x=[e.batchSize,h,g],b=!0,w=!1,I=[];if(s!=null){let Z=AI(s.shape,d);Z!=null&&(s=rt({inputs:{x:s},backend:n,attrs:{shape:Z}}),I.push(s))}if(o!=null){let Z=AI(o.shape,d);Z!=null&&(o=rt({inputs:{x:o},backend:n,attrs:{shape:Z}}),I.push(o))}let N=rt({inputs:{x:t},backend:n,attrs:{shape:[1,h,y.sizeFromShape(t.shape)/h]}});I.push(N);let E=new EI(x,e),A=[r.shape,[e.padInfo.top,e.padInfo.left],[e.strideHeight,e.strideWidth],[e.dilationHeight,e.dilationWidth],[e.inChannels],[e.filterWidth*e.inChannels],[e.outWidth]],D=n.runWebGLProgram(E,[r],"float32",A),F=rt({inputs:{x:D},backend:n,attrs:{shape:x}});I.push(D),I.push(F);let M=o!=null,V=s!=null,G=a==="leakyrelu",W=a?Ml(a,!0):null,q=new Rd(d?F.shape:N.shape,d?N.shape:F.shape,d?[e.batchSize,g,e.outChannels]:[e.batchSize,e.outChannels,g],b,w,M,W,V,G),H=d?[F,N]:[N,F];if(o&&H.push(o),V&&H.push(s),G){let Z=n.makeTensorInfo([],"float32",y.createScalarValue(i,"float32"));H.push(Z),I.push(Z)}let j=n.runWebGLProgram(q,H,"float32"),Y=rt({inputs:{x:j},backend:n,attrs:{shape:e.outShape}});I.push(j);for(let Z of I)n.disposeIntermediateTensorInfo(Z);return Y}function Hst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,filter:s}=t,{strides:i,pad:a,dataFormat:u,dilations:l,dimRoundingMode:c}=n,p=S.convertConv2DDataFormat(u),m=S.computeConv2DInfo(o.shape,s.shape,i,l,a,c,!1,p),f;if(m.filterHeight===1&&m.filterWidth===1&&m.dilationHeight===1&&m.dilationWidth===1&&m.strideHeight===1&&m.strideWidth===1&&(m.padInfo.type==="SAME"||m.padInfo.type==="VALID"))f=DI({x:o,filter:s,convInfo:m,backend:e});else if(m.strideWidth<=2&&p==="channelsLast"&&L().getBool("WEBGL_EXP_CONV")){let h=new Md(m),g=[[m.padInfo.top,m.padInfo.left],[m.strideHeight,m.strideWidth],[m.dilationHeight,m.dilationWidth],[m.inHeight,m.inWidth]];f=e.runWebGLProgram(h,[o,s],"float32",g)}else if(L().getBool("WEBGL_CONV_IM2COL"))f= $ I({x:o,filter:s,convInfo:m,backend:e});else{let h=new Od(m);f=e.runWebGLProgram(h,[o,s],"float32")}let d=rt({inputs:{x:f},backend:e,attrs:{shape:m.outShap
2022-03-07 19:17:31 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2024-01-17 22:56:53 +01:00
int wR = coords . x ;
int wC = coords . y ;
int d1 = coords . z ;
int d2 = coords . w ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01: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
2024-01-17 22:56:53 +01:00
for ( int b = 0 ; b < $ { t . batchSize } ; b ++ ) {
for ( int yR = 0 ; yR < $ { t . outHeight } ; yR ++ ) {
int xR = wR + yR * $ { e } - $ { o } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int yC = 0 ; yC < $ { t . outWidth } ; yC ++ ) {
int xC = wC + yC * $ { n } - $ { s } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
}
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
$ { i ? ` float dyValue = getDy(b, yR, yC, d2);
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 ) ; ` }
}
}
}
setOutput ( dotProd ) ;
2022-10-18 13:23:49 +02:00
}
2024-09-10 17:30:23 +02:00
` }},FI=class{constructor(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;let e=t.filterHeight,n=t.filterWidth,o=t.strideHeight,s=t.strideWidth,i=t.dataFormat==="channelsLast",a=e-1-t.padInfo.top,u=n-1-t.padInfo.left,l=i?1:2,c=i?2:3,p=i?3:1;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 pads = ivec2 ( $ { a } , $ { u } ) ;
2024-01-17 22:44:24 +01:00
2022-10-18 13:23:49 +02:00
void main ( ) {
2024-01-17 22:44:24 +01:00
ivec4 coords = getOutputCoords ( ) ;
2024-01-17 22:56:53 +01:00
int batch = coords [ 0 ] ;
int d1 = coords [ $ { p } ] ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
ivec2 dyCorner = ivec2 ( coords [ $ { l } ] , coords [ $ { c } ] ) - pads ;
int dyRCorner = dyCorner . x ;
int dyCCorner = dyCorner . y ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// 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 < $ { e } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / $ { o } . 0 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
continue ;
}
int idyR = int ( dyR ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
int wRPerm = $ { e } - 1 - wR ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { n } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { s } . 0 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
int wCPerm = $ { n } - 1 - wC ;
for ( int d2 = 0 ; d2 < $ { t . outChannels } ; d2 ++ ) {
if ( $ { i } ) {
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 ;
}
}
}
}
setOutput ( dotProd ) ;
}
2024-09-10 17:30:23 +02:00
` }},OI=class{constructor(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;let e=t.strideDepth,n=t.strideHeight,o=t.strideWidth,s=t.padInfo.front,i=t.padInfo.top,a=t.padInfo.left;this.userCode= `
2024-01-17 22:56:53 +01: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 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
float dotProd = 0.0 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int b = 0 ; b < $ { t . batchSize } ; b ++ ) {
for ( int yF = 0 ; yF < $ { t . outDepth } ; yF ++ ) {
int xF = wF + yF * $ { e } - $ { s } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xF < 0 || xF >= $ { t . inDepth } ) {
continue ;
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
for ( int yR = 0 ; yR < $ { t . outHeight } ; yR ++ ) {
int xR = wR + yR * $ { n } - $ { i } ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
for ( int yC = 0 ; yC < $ { t . outWidth } ; yC ++ ) {
int xC = wC + yC * $ { o } - $ { a } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float dyValue = getDy ( b , yF , yR , yC , d2 ) ;
float xValue = getX ( b , xF , xR , xC , d1 ) ;
dotProd += ( xValue * dyValue ) ;
}
}
}
}
setOutput ( dotProd ) ;
}
2024-09-10 17:30:23 +02:00
` }},MI=class{constructor(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;let e=t.filterDepth,n=t.filterHeight,o=t.filterWidth,s=t.strideDepth,i=t.strideHeight,a=t.strideWidth,u=e-1-t.padInfo.front,l=n-1-t.padInfo.top,c=o-1-t.padInfo.left;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec3 pads = ivec3 ( $ { u } , $ { l } , $ { c } ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int d1 = coords . u ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
ivec3 dyCorner = ivec3 ( coords . y , coords . z , coords . w ) - pads ;
int dyFCorner = dyCorner . x ;
int dyRCorner = dyCorner . y ;
int dyCCorner = dyCorner . z ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dotProd = 0.0 ;
for ( int wF = 0 ; wF < $ { e } ; wF ++ ) {
float dyF = float ( dyFCorner + wF ) / $ { s } . 0 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( dyF < 0.0 || dyF >= $ { t . outDepth } . 0 || fract ( dyF ) > 0.0 ) {
2024-01-17 22:44:24 +01:00
continue ;
}
2024-01-17 22:56:53 +01:00
int idyF = int ( dyF ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int wFPerm = $ { e } - 1 - wF ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { n } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / $ { i } . 0 ;
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 ||
fract ( dyR ) > 0.0 ) {
2024-01-17 22:44:24 +01:00
continue ;
}
2024-01-17 22:56:53 +01:00
int idyR = int ( dyR ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int wRPerm = $ { n } - 1 - wR ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { o } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { a } . 0 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
int wCPerm = $ { o } - 1 - wC ;
2021-03-31 13:01:22 +02:00
2024-01-17 22:56:53 +01:00
for ( int d2 = 0 ; d2 < $ { t . outChannels } ; d2 ++ ) {
float xValue = getDy ( batch , idyF , idyR , idyC , d2 ) ;
float wValue = getW ( wFPerm , wRPerm , wCPerm , d1 , d2 ) ;
dotProd += xValue * wValue ;
}
2024-01-17 22:44:24 +01:00
}
}
}
2024-01-17 22:56:53 +01:00
setOutput ( dotProd ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};function qst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,dy:s}=t,{strides:i,pad:a,dataFormat:u,dimRoundingMode:l,filterShape:c}=n,p=S.convertConv2DDataFormat(u),m=S.computeConv2DInfo(o.shape,c,i,1,a,l,!1,p),f=new RI(m);return e.runWebGLProgram(f,[o,s],"float32")}var Q3={kernelName:Dp,backendName:"webgl",kernelFunc:qst};var PI=class{constructor(t){this.variableNames=["dy","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"strides",type:"vec2"}],this.outputShape=t.inShape,this.enableShapeUniforms=he(this.outputShape.length);let e=t.filterHeight,n=t.filterWidth,o=e-1-t.padInfo.top,s=n-1-t.padInfo.left;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 pads = ivec2 ( $ { o } , $ { s } ) ;
2022-03-07 19:17:31 +01:00
2022-10-18 13:23:49 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2024-01-17 22:56:53 +01:00
int batch = coords [ 0 ] ;
int d1 = coords [ 3 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
ivec2 dyCorner = ivec2 ( coords [ 1 ] , coords [ 2 ] ) - pads ;
int dyRCorner = dyCorner . x ;
int dyCCorner = dyCorner . y ;
2022-10-09 19:40:33 +02:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0. ) ;
for ( int wR = 0 ; wR < $ { e } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / strides [ 0 ] ;
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
continue ;
}
int idyR = int ( dyR ) ;
int wRPerm = $ { e } - 1 - wR ;
2022-10-09 19:40:33 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { n } ; wC ++ ) {
int wCPerm = $ { n } - 1 - wC ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dyC = float ( dyCCorner + wC ) / strides [ 1 ] ;
bool idyCVal = ( dyC >= 0.0 ) && ( dyC < $ { t . outWidth } . 0 )
&& ( fract ( dyC ) == 0.0 ) ;
int idyC = int ( dyC ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dyC2 = float ( dyCCorner + wC + 1 ) / strides [ 1 ] ;
bool idyCVal2 = ( dyC2 >= 0.0 ) && ( dyC2 < $ { t . outWidth } . 0 )
&& ( fract ( dyC2 ) == 0.0 ) ;
int idyC2 = int ( dyC2 ) ;
if ( idyCVal && idyCVal2 ) {
for ( int d2 = 0 ; d2 < $ { t . 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 ) ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
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 < $ { t . 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 < $ { t . 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-09-10 17:30:23 +02:00
` }};function Kst(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,filter:s}=t,{inputShape:i,strides:a,pad:u,dataFormat:l,dimRoundingMode:c}=n,p=S.convertConv2DDataFormat(l),m=S.computeConv2DInfo(i,s.shape,a,1,u,c,!1,p);if(L().getBool("WEBGL_PACK_CONV2DTRANSPOSE")&&p==="channelsLast"){let f=[[m.strideHeight,m.strideWidth]],d=new PI(m);return e.runWebGLProgram(d,[o,s],"float32",f)}else{let f=new FI(m);return e.runWebGLProgram(f,[o,s],"float32")}}var tB={kernelName:ts,backendName:"webgl",kernelFunc:Kst};function jst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,filter:s}=t,{strides:i,pad:a,dilations:u}=n,l=S.computeConv3DInfo(o.shape,s.shape,i,u,a),c=new _I(l);return e.runWebGLProgram(c,[o,s],"float32")}var eB={kernelName:es,backendName:"webgl",kernelFunc:jst};function Xst(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,dy:s}=t,{strides:i,pad:a,filterShape:u}=n,l=S.computeConv3DInfo(o.shape,u,i,1,a),c=new OI(l);return e.runWebGLProgram(c,[o,s],"float32")}var rB={kernelName:Ra,backendName:"webgl",kernelFunc:Xst};function Yst(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,filter:s}=t,{pad:i,strides:a,inputShape:u}=n,l=S.computeConv3DInfo(u,s.shape,a,1,i),c=new MI(l);return e.runWebGLProgram(c,[o,s],"float32")}var nB={kernelName:Fa,backendName:"webgl",kernelFunc:Yst};var Zst=Po+ `
2024-01-17 22:56:53 +01:00
return cos ( x ) ;
2024-09-10 17:30:23 +02:00
` ,Jst= `
2024-01-17 22:56:53 +01:00
vec4 result = cos ( x ) ;
bvec4 isNaN = isnan ( x ) ;
$ { Xn }
return result ;
2024-09-10 17:30:23 +02:00
` ,Qst=It({opSnippet:Zst,packedOpSnippet:Jst}),oB={kernelName:rs,backendName:"webgl",kernelFunc:Qst};var tit= `
2024-01-17 22:56:53 +01:00
float e2x = exp ( - x ) ;
return ( e2x + 1.0 / e2x ) / 2.0 ;
2024-09-10 17:30:23 +02:00
` ,eit=It({opSnippet:tit}),sB={kernelName:ns,backendName:"webgl",kernelFunc:eit};var LI=class{constructor(t,e,n,o,s){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[i,a,u,l]=t,[c]=e,[p,m]=n;this.outputShape=[c,p,m,l];let f=o==="bilinear"?1:0,[d,h]=[ ` $ { a - 1 } . 0 ` , ` $ { u - 1 } . 0 ` ],[g,x,b]=p>1?[ ` $ { ( a - 1 ) / ( p - 1 ) } ` ,"(y2-y1) * height_ratio", ` y1 * $ { d } + float ( y ) * ( height _scale ) ` ]:["0.0","0.0", ` 0.5 * ( y1 + y2 ) * $ { d } ` ],[w,I,N]=m>1?[ ` $ { ( u - 1 ) / ( m - 1 ) } ` ,"(x2-x1) * width_ratio", ` x1 * $ { h } + float ( x ) * ( width _scale ) ` ]:["0.0","0.0", ` 0.5 * ( x1 + x2 ) * $ { h } ` ];this.userCode= `
2024-01-17 22:56:53 +01:00
const float height _ratio = float ( $ { g } ) ;
const float width _ratio = float ( $ { w } ) ;
2021-03-31 13:01:22 +02:00
void main ( ) {
2022-03-07 19:17:31 +01:00
ivec4 coords = getOutputCoords ( ) ;
2024-01-17 22:44:24 +01:00
int b = coords [ 0 ] ;
2024-01-17 22:56:53 +01:00
int y = coords [ 1 ] ;
int x = coords [ 2 ] ;
2024-01-17 22:44:24 +01:00
int d = coords [ 3 ] ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// get box vals
float y1 = getBoxes ( b , 0 ) ;
float x1 = getBoxes ( b , 1 ) ;
float y2 = getBoxes ( b , 2 ) ;
float x2 = getBoxes ( b , 3 ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// get image in batch index
int bInd = round ( getBoxInd ( b ) ) ;
if ( bInd < 0 || bInd >= $ { i } ) {
return ;
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float height _scale = $ { x } ;
float width _scale = $ { I } ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float in _y = $ { b } ;
if ( in _y < 0.0 || in _y > $ { d } ) {
setOutput ( float ( $ { s } ) ) ;
return ;
}
float in _x = $ { N } ;
if ( in _x < 0.0 || in _x > $ { h } ) {
setOutput ( float ( $ { s } ) ) ;
return ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec2 sourceFracIndexCR = vec2 ( in _x , in _y ) ;
if ( $ { f } == 1 ) {
// Compute the four integer indices.
ivec2 sourceFloorCR = ivec2 ( sourceFracIndexCR ) ;
ivec2 sourceCeilCR = ivec2 ( ceil ( sourceFracIndexCR ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01: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 ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
vec2 fracCR = sourceFracIndexCR - vec2 ( sourceFloorCR ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
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 ) ;
}
}
2024-09-10 17:30:23 +02:00
` }};var rit=r=>{let{inputs:t,backend:e,attrs:n}=r,{image:o,boxes:s,boxInd:i}=t,{cropSize:a,method:u,extrapolationValue:l}=n,c=new LI(o.shape,s.shape,a,u,l);return e.runWebGLProgram(c,[o,s,i],"float32")},iB={kernelName:Ma,backendName:"webgl",kernelFunc:rit};var gp;(function(r){r.Prod="*",r.Sum="+"})(gp||(gp={}));var dg=class{constructor(t,e,n,o){this.op=t,this.outputShape=e,this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}];let s=this.outputShape.length,i=this.op===gp.Prod?"1.0":"0.0",a=n?i: ` getX ( $ { aB ( s , "coords" , this . op ) } ) ` ,u=this.outputShape[this.outputShape.length-1],l="",c="";n?(l=o? ` end != $ { u - 1 } ` :"end != 0",c=o?"end + 1":"end - 1"):(l=o? ` end + pow2 < $ { u } ` :"end >= pow2",c=o?"end + pow2":"end - pow2"),this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { zt ( s ) } coords = getOutputCoords ( ) ;
2024-09-10 17:30:23 +02:00
int end = $ { lB ( s , "coords" , this . op ) } ;
2024-01-17 22:56:53 +01:00
float val = $ { a } ;
int pow2 = int ( pow ( 2.0 , index ) ) ;
if ( $ { l } ) {
int idx = $ { c } ;
2024-09-10 17:30:23 +02:00
$ { lB ( s , "coords" , this . op ) } = idx ;
val $ { this . op } = getX ( $ { aB ( s , "coords" , this . op ) } ) ;
2024-01-17 22:56:53 +01:00
}
setOutput ( val ) ;
}
2024-09-10 17:30:23 +02:00
` }};function aB(r,t,e){if(r===1)return ` $ { t } ` ;if(r===2)return ` $ { t } . x , $ { t } . y ` ;if(r===3)return ` $ { t } . x , $ { t } . y , $ { t } . z ` ;if(r===4)return ` $ { t } . x , $ { t } . y , $ { t } . z , $ { t } . w ` ;throw new Error( ` Cumulative $ { e } for rank $ { r } is not yet supported ` )}function lB(r,t,e){if(r===1)return ` $ { t } ` ;if(r===2)return ` $ { t } . y ` ;if(r===3)return ` $ { t } . z ` ;if(r===4)return ` $ { t } . w ` ;throw new Error( ` Cumulative $ { e } for rank $ { r } is not yet supported ` )}function zI(r,t,e,n,o,s){let i=t.shape.length,a=S.getAxesPermutation([n],i),u=t;a!=null&&(u=Pe({inputs:{x:t},backend:e,attrs:{perm:a}}));let l=S.getInnerMostAxes(1,i)[0];if(l!==i-1)throw new Error( ` WebGL cumprod shader expects an inner - most axis = $ { t . shape . length - 1 } but got axis = $ { n } ` );let c=u.shape[l],p=nr({inputs:{x:u},backend:e});for(let m=0;m<=Math.ceil(Math.log2(c))-1;m++){let f=new dg(r,u.shape,!1,s),d=[[m]],h=p;p=e.runWebGLProgram(f,[p],p.dtype,d),e.disposeIntermediateTensorInfo(h)}if(o){let m=new dg(r,u.shape,o,s),f=p;p=e.runWebGLProgram(m,[p],p.dtype),e.disposeIntermediateTensorInfo(f)}if(a!=null){let m=S.getUndoAxesPermutation(a),f=Pe({inputs:{x:p},backend:e,attrs:{perm:m}});return e.disposeIntermediateTensorInfo(p),e.disposeIntermediateTensorInfo(u),f}return p}function nit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,exclusive:i,reverse:a}=n;return zI(gp.Prod,o,e,s,i,a)}var uB={kernelName:Oa,backendName:"webgl",kernelFunc:nit};function oit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,exclusive:i,reverse:a}=n;return zI(gp.Sum,o,e,s,i,a)}var cB={kernelName:os,backendName:"webgl",kernelFunc:oit};function sit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,weights:s}=t,{size:i,binaryOutput:a}=n;if(o.shape.length===1){let u=e.readSync(o.dataId),l=e.readSync(s.dataId),c=tI(u,l,s.dtype,s.shape,i);return e.makeTensorInfo([i],s.dtype,c)}else if(o.shape.length===2){let u=e.bufferSync(o),l=e.bufferSync(s),c=KL(u,l,i,a);return e.makeTensorInfo(c.shape,s.dtype,c.values)}throw new Error( ` Error in denseBincount : input must be at most rank 2 , but got rank$ { o . shape . length } . ` )}var pB={kernelName:jl,backendName:"webgl",kernelFunc:sit};var BI=class{constructor(t,e,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=t,this.blockSize=e,this.dataFormat=n,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int h = $ { this . getHeightCoordString ( ) } ;
int w = $ { this . getWidthCoordString ( ) } ;
int d = $ { this . getDepthCoordString ( ) } ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int in _h = h / $ { e } ;
int offset _h = imod ( h , $ { e } ) ;
int in _w = w / $ { e } ;
int offset _w = imod ( w , $ { e } ) ;
int offset _d = ( offset _h * $ { e } + offset _w ) *
$ { this . getOutputDepthSize ( ) } ;
int in _d = d + offset _d ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float result = $ { this . getInputSamplingString ( ) } ;
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }getHeightCoordString(){return this.dataFormat==="NHWC"?"coords[1]":"coords[2]"}getWidthCoordString(){return this.dataFormat==="NHWC"?"coords[2]":"coords[3]"}getDepthCoordString(){return this.dataFormat==="NHWC"?"coords[3]":"coords[1]"}getOutputDepthSize(){return this.dataFormat==="NHWC"?this.outputShape[3]:this.outputShape[1]}getInputSamplingString(){return this.dataFormat==="NHWC"?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"}};function iit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{blockSize:s,dataFormat:i}=n,a=o.shape[0],u=i==="NHWC"?o.shape[1]:o.shape[2],l=i==="NHWC"?o.shape[2]:o.shape[3],c=i==="NHWC"?o.shape[3]:o.shape[1],p=u*s,m=l*s,f=c/(s*s),d=i==="NHWC"?[a,p,m,f]:[a,f,p,m],h=new BI(d,s,i);return e.runWebGLProgram(h,[o],o.dtype)}var mB={kernelName:Pa,backendName:"webgl",kernelFunc:iit};var Pd=class{constructor(t,e=!1,n=null,o=!1,s=!1){this.variableNames=["x","W"],this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=t.outShape,this.enableShapeUniforms=he(this.outputShape.length);let i=t.filterHeight,a=t.filterWidth,u=t.outChannels/t.inChannels,l="",c="";n&&(o?l= ` float activation ( float a ) {
2024-01-17 22:56:53 +01:00
float b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
} ` :s?l= ` float activation ( float a ) {
float b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
} ` :l= `
float activation ( float x ) {
$ { n }
}
` ,c="result = activation(result);");let p=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { l }
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords . x ;
ivec2 xRCCorner = coords . yz * strides - pads ;
int d2 = coords . w ;
int d1 = d2 / $ { u } ;
int q = d2 - d1 * $ { u } ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
// TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.
for ( int wR = 0 ; wR < $ { i } ; wR ++ ) {
int xR = xRCorner + wR * dilations [ 0 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( xR < 0 || xR >= inDims [ 0 ] ) {
2024-01-17 22:44:24 +01:00
continue ;
2022-10-18 13:23:49 +02:00
}
2022-01-14 16:04:13 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { a } ; wC ++ ) {
int xC = xCCorner + wC * dilations [ 1 ] ;
2022-01-14 16:04:13 +01:00
2024-01-17 22:56:53 +01:00
if ( xC < 0 || xC >= inDims [ 1 ] ) {
2024-01-17 22:44:24 +01:00
continue ;
}
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float xVal = getX ( batch , xR , xC , d1 ) ;
float wVal = getW ( wR , wC , d1 , q ) ;
dotProd += xVal * wVal ;
}
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
float result = dotProd ;
$ { p }
$ { c }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var Ld=class{constructor(t,e=!1,n=null,o=!1,s=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=t.outShape,this.enableShapeUniforms=he(this.outputShape.length);let i=t.outChannels/t.inChannels,a=t.padInfo.left,u=t.strideWidth,l=t.dilationWidth,c=t.filterHeight,p=t.filterWidth,m=p,f= `
2024-01-17 22:56:53 +01:00
int xR ; int xC ; int xCOffset ;
vec4 wTexel ; vec4 previous ; vec4 final ; ` ;for(let x=0;x<p;x++)f+= `
vec4 xTexelC$ { x * 2 } ;
int xTexelC$ { x * 2 } Ready ;
vec4 xTexelC$ { x * 2 + 1 } ;
int xTexelC$ { x * 2 + 1 } Ready ;
vec4 xC$ { x } ; ` ;f+= `
for ( int r = 0 ; r < $ { c } ; r ++ ) {
` ;for(let x=0;x<p;x++)f+= `
xTexelC$ { x * 2 } = vec4 ( 0.0 ) ;
xTexelC$ { x * 2 } Ready = 0 ;
xTexelC$ { x * 2 + 1 } = vec4 ( 0.0 ) ;
xTexelC$ { x * 2 + 1 } Ready = 0 ;
xC$ { x } = vec4 ( 0.0 ) ; ` ;f+= `
xR = xRCorner + r * dilations [ 0 ] ;
if ( xR >= 0 && xR < inDims [ 0 ] ) {
` ;for(let x=0;x<(m+1)/2;x++){let b=x*2;if(f+= `
xC = xCCorner + $ { b * l } ;
` ,u===1){if(b<p&&(a%2===1?(f+= `
xCOffset = xC + 1 ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { b } Ready == 0 ) {
xTexelC$ { b } = getX ( batch , xR , xCOffset , d1 ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { b } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b } Ready = 1 ;
}
` ,l===1&&b>0?f+= `
xC$ { b } = vec4 ( xTexelC$ { b - 2 } . zw , xTexelC$ { b } . xy ) ;
` :f+= `
xCOffset = xC + 1 - 2 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] ) {
previous = getX ( batch , xR , xCOffset , d1 ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
xC$ { b } = vec4 ( previous . zw , xTexelC$ { b } . xy ) ;
} else {
xC$ { b } = vec4 ( 0.0 , 0.0 , xTexelC$ { b } . xy ) ;
}
` ):f+= `
if ( xC >= 0 && xC < inDims [ 1 ] && xTexelC$ { b } Ready == 0 ) {
xTexelC$ { b } = getX ( batch , xR , xC , d1 ) ;
if ( xC + 1 >= inDims [ 1 ] ) {
xTexelC$ { b } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b } Ready = 1 ;
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
xC$ { b } = xTexelC$ { b } ;
` ,b+1<p)){let w=a%2===0?y.nearestLargerEven(l):l;l%2===0&&a%2===1||l%2!==0&&a%2!==1?(f+= `
xCOffset = xC + imod ( pads [ 1 ] , 2 ) + $ { w } ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { b + 1 } Ready == 0 ) {
xTexelC$ { b + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { b + 1 } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b + 1 } Ready = 1 ;
}
` ,l>1?f+= `
xCOffset -= 2 ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] ) {
previous = getX ( batch , xR , xCOffset , d1 ) ;
xC$ { b + 1 } = vec4 ( previous . zw , xTexelC$ { b + 1 } . xy ) ;
} else {
xC$ { b + 1 } = vec4 ( 0.0 , 0.0 , xTexelC$ { b + 1 } . xy ) ;
}
` :f+= `
xC$ { b + 1 } = vec4 ( xTexelC$ { b } . zw , xTexelC$ { b + 1 } . xy ) ;
` ):w===1?f+= `
xC$ { b + 1 } = xTexelC$ { b } ;
` :f+= `
xCOffset = xC + $ { w } ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { b + 1 } Ready == 0 ) {
xTexelC$ { b + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { b + 1 } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b + 1 } Ready = 1 ;
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
xC$ { b + 1 } = xTexelC$ { b + 1 } ;
` }}else b<p&&(a%2===1?(f+= `
xCOffset = xC + 1 - strides [ 1 ] ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { b } Ready == 0 ) {
xTexelC$ { b } = getX ( batch , xR , xCOffset , d1 ) ;
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { b } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b } Ready = 1 ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( xC + 1 >= 0 && xC + 1 < inDims [ 1 ] && xTexelC$ { b + 1 } Ready == 0 ) {
xTexelC$ { b + 1 } = getX ( batch , xR , xC + 1 , d1 ) ;
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if ( xC + 2 >= inDims [ 1 ] ) {
xTexelC$ { b + 1 } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b + 1 } Ready = 1 ;
}
2021-03-31 13:01:22 +02:00
2024-01-17 22:56:53 +01:00
xC$ { b } = vec4 ( xTexelC$ { b } . zw , xTexelC$ { b + 1 } . zw ) ;
` ,b+1<p&&(f+= `
final = vec4 ( 0.0 ) ;
xCOffset = xC + 1 + strides [ 1 ] ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] ) {
final = getX ( batch , xR , xCOffset , d1 ) ;
}
xC$ { b + 1 } = vec4 ( xTexelC$ { b + 1 } . xy , final . xy ) ;
` )):(f+= `
if ( xC >= 0 && xC < inDims [ 1 ] && xTexelC$ { b } Ready == 0 ) {
xTexelC$ { b } = getX ( batch , xR , xC , d1 ) ;
if ( xC + 1 >= inDims [ 1 ] ) {
xTexelC$ { b } . zw = vec2 ( 0.0 ) ;
}
xTexelC$ { b } Ready = 1 ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
xCOffset = xC + strides [ 1 ] ;
if ( xCOffset >= 0 && xCOffset < inDims [ 1 ] && xTexelC$ { b + 1 } Ready == 0 ) {
xTexelC$ { b + 1 } = getX ( batch , xR , xCOffset , d1 ) ;
if ( xCOffset + 1 >= inDims [ 1 ] ) {
xTexelC$ { b + 1 } . zw = vec2 ( 0. ) ;
}
xTexelC$ { b + 1 } Ready = 1 ;
}
2021-03-31 13:01:22 +02:00
2024-01-17 22:56:53 +01:00
xC$ { b } = vec4 (
xTexelC$ { b } . xy , xTexelC$ { b + 1 } . xy ) ;
` ,b+1<p&&(f+= `
xC$ { b + 1 } = vec4 ( xTexelC$ { b } . zw , xTexelC$ { b + 1 } . zw ) ;
` )));b<p&&(f+= `
wTexel = getW ( r , $ { b } , d1 , q ) ;
dotProd += xC$ { b } * vec4 ( wTexel . xz , wTexel . xz ) ;
` ,b+1<p&&(f+= `
wTexel = getW ( r , $ { b + 1 } , d1 , q ) ;
dotProd += xC$ { b + 1 } * vec4 ( wTexel . xz , wTexel . xz ) ;
` ))}f+= `
}
` ,f+= `
}
` ;let d="",h="";n&&(o?d= ` vec4 activation ( vec4 a ) {
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
$ { n }
} ` :s?d= ` vec4 activation ( vec4 a ) {
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
$ { n }
} ` :d= ` vec4 activation ( vec4 x ) {
$ { n }
} ` ,h="result = activation(result);");let g=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { d }
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords . x ;
ivec2 xRCCorner = coords . yz * strides - pads ;
int d2 = coords . w ;
int d1 = d2 / $ { i } ;
int q = d2 - d1 * $ { i } ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
//intialize dotProd with a small epsilon seems to reduce GPU accuracy loss.
vec4 dotProd = vec4 ( 0.000000000000001 ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
$ { f }
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
vec4 result = dotProd - vec4 ( 0.000000000000001 ) ;
$ { g }
$ { h }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};function ait(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,filter:s}=t,{strides:i,pad:a,dilations:u,dimRoundingMode:l}=n,c=u;c==null&&(c=[1,1]),y.assert(S.eitherStridesOrDilationsAreOne(i,c),()=> ` Error in depthwiseConv2d : Either strides or dilations must be 1. Got strides $ { i } and dilations '${c}' ` );let p=S.computeConv2DInfo(o.shape,s.shape,i,c,a,l,!0),m;L().getBool("WEBGL_PACK_DEPTHWISECONV")&&p.strideWidth<=2&&p.outChannels/p.inChannels===1?m=new Ld(p):m=new Pd(p);let f=[[p.padInfo.top,p.padInfo.left],[p.strideHeight,p.strideWidth],[p.dilationHeight,p.dilationWidth],[p.inHeight,p.inWidth]];return e.runWebGLProgram(m,[o,s],"float32",f)}var fB={kernelName:ss,backendName:"webgl",kernelFunc:ait};var VI=class{constructor(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;let e=t.strideHeight,n=t.strideWidth,o=t.padInfo.top,s=t.padInfo.left,i=t.outChannels/t.inChannels;this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int wR = coords . x ;
int wC = coords . y ;
int d1 = coords . z ;
int dm = coords . w ;
int d2 = d1 * $ { i } + dm ;
float dotProd = 0.0 ;
// TO DO: Vec4 over the batch size
for ( int b = 0 ; b < $ { t . batchSize } ; b ++ ) {
for ( int yR = 0 ; yR < $ { t . outHeight } ; yR ++ ) {
int xR = wR + yR * $ { e } - $ { o } ;
if ( xR < 0 || xR >= $ { t . inHeight } ) {
continue ;
}
for ( int yC = 0 ; yC < $ { t . outWidth } ; yC ++ ) {
int xC = wC + yC * $ { n } - $ { s } ;
if ( xC < 0 || xC >= $ { t . inWidth } ) {
continue ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
float dyValue = getDy ( b , yR , yC , d2 ) ;
float xValue = getX ( b , xR , xC , d1 ) ;
dotProd += ( xValue * dyValue ) ;
2022-03-07 19:17:31 +01:00
}
}
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
setOutput ( dotProd ) ;
}
2024-09-10 17:30:23 +02:00
` }},GI=class{constructor(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;let e=t.filterHeight,n=t.filterWidth,o=t.strideHeight,s=t.strideWidth,i=e-1-t.padInfo.top,a=n-1-t.padInfo.left,u=t.outChannels/t.inChannels;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 pads = ivec2 ( $ { i } , $ { a } ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int d1 = coords [ 3 ] ;
ivec2 dyCorner = coords . yz - pads ;
int dyRCorner = dyCorner . x ;
int dyCCorner = dyCorner . y ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dotProd = 0.0 ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
for ( int wR = 0 ; wR < $ { e } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / $ { o } . 0 ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
continue ;
}
int idyR = int ( dyR ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int wRPerm = $ { e } - 1 - wR ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { n } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { s } . 0 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
int wCPerm = $ { n } - 1 - wC ;
// TO DO: Vec4 over the channelMul
for ( int dm = 0 ; dm < $ { u } ; dm ++ ) {
int d2 = d1 * $ { u } + dm ;
float xValue = getDy ( batch , idyR , idyC , d2 ) ;
float wValue = getW ( wRPerm , wCPerm , d1 , dm ) ;
dotProd += xValue * wValue ;
}
}
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
setOutput ( dotProd ) ;
2022-03-07 19:17:31 +01:00
}
2024-09-10 17:30:23 +02:00
` }};function lit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,dy:s}=t,{strides:i,dilations:a,pad:u,dimRoundingMode:l,filterShape:c}=n,p=S.computeConv2DInfo(o.shape,c,i,a,u,l,!0),m=new VI(p);return e.runWebGLProgram(m,[o,s],"float32")}var dB={kernelName: $ p,backendName:"webgl",kernelFunc:lit};function uit(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,filter:s}=t,{strides:i,dilations:a,pad:u,dimRoundingMode:l,inputShape:c}=n,p=S.computeConv2DInfo(c,s.shape,i,a,u,l,!0),m=new GI(p);return e.runWebGLProgram(m,[o,s],"float32")}var hB={kernelName:Rp,backendName:"webgl",kernelFunc:uit};var WI=class{constructor(t){this.variableNames=["X"],this.outputShape=[t,t],this.userCode= `
2022-10-18 13:10:36 +02:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec2 coords = getOutputCoords ( ) ;
float val = coords [ 0 ] == coords [ 1 ] ? getX ( coords [ 0 ] ) : 0.0 ;
setOutput ( val ) ;
}
2024-09-10 17:30:23 +02:00
` }};function cit(r){let{inputs:t,backend:e}=r,{x:n}=t,o=[...n.shape,...n.shape],s=y.sizeFromShape(n.shape),i=rt({inputs:{x:n},backend:e,attrs:{shape:[s]}}),a=new WI(s),u=e.runWebGLProgram(a,[i],i.dtype),l=rt({inputs:{x:u},backend:e,attrs:{shape:o}});return e.disposeIntermediateTensorInfo(i),e.disposeIntermediateTensorInfo(u),l}var gB={kernelName:Xl,backendName:"webgl",kernelFunc:cit};var UI=class{constructor(t){this.variableNames=["x","W"],this.outputShape=t.outShape;let{inHeight:e,inWidth:n,padInfo:o,strideHeight:s,strideWidth:i,filterHeight:a,filterWidth:u,dilationHeight:l,dilationWidth:c}=t,{top:p,left:m}=o;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 strides = ivec2 ( $ { s } , $ { i } ) ;
const ivec2 pads = ivec2 ( $ { p } , $ { m } ) ;
const float neg _infinity = - 3.4 e38 ;
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int d1 = coords . w ;
ivec2 outTopLeftCorner =
coords . yz * strides - pads ;
int hBeg = outTopLeftCorner . x ;
int wBeg = outTopLeftCorner . y ;
float curVal = neg _infinity ;
for ( int h = 0 ; h < $ { a } ; h ++ ) {
int hIn = hBeg + h * $ { l } ;
if ( hIn >= 0 && hIn < $ { e } ) {
for ( int w = 0 ; w < $ { u } ; w ++ ) {
int wIn = wBeg + w * $ { c } ;
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 ;
}
}
}
}
2022-10-18 13:10:36 +02:00
}
2024-01-17 22:56:53 +01:00
float result = curVal ;
setOutput ( result ) ;
2022-03-07 19:17:31 +01:00
}
2024-09-10 17:30:23 +02:00
` }};function pit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,filter:s}=t,{strides:i,pad:a,dilations:u}=n,l=S.computeDilation2DInfo(o.shape,s.shape,i,a,"NHWC",u),c,p=new UI(l);c=e.runWebGLProgram(p,[o,s],"float32");let m=rt({inputs:{x:c},backend:e,attrs:{shape:l.outShape}});return e.disposeIntermediateTensorInfo(c),m}var xB={kernelName:is,backendName:"webgl",kernelFunc:pit};function mit(r){let{inputs:t,backend:e,attrs:n}=r,{equation:o}=n,s=t,{allDims:i,summedDims:a,idDims:u}=S.decodeEinsumEquation(o,s.length);S.checkEinsumDimSizes(i.length,u,s);let{path:l,steps:c}=S.getEinsumComputePath(a,u),p=c.length,m=null,f=i.length,d=[];for(let h=0;h<p;++h){for(let g of c[h]){let{permutationIndices:x,expandDims:b}=S.getEinsumPermutation(f,u[g]),w;S.isIdentityPermutation(x)?w=s[g]:(w=Pe({inputs:{x:s[g]},backend:e,attrs:{perm:x}}),d.push(w));let I=w.shape.slice();for(let N=0;N<b.length;++N)I.splice(b[N],0,1);y.arraysEqual(w.shape,I)||(w=rt({inputs:{x:w},backend:e,attrs:{shape:I}}),d.push(w)),m===null?m=w:(m=mg({inputs:{a:w,b:m},backend:e}),d.push(m))}h<p-1&&(l[h]>=0&&(m=fp({inputs:{x:m},backend:e,attrs:{axis:l[h]-(i.length-f),keepDims:!1}}),d.push(m)),f--)}for(let h of d)h!==m&&e.disposeIntermediateTensorInfo(h);return m}var yB={kernelName:Fp,backendName:"webgl",kernelFunc:mit};var fit="return (x >= 0.0) ? x : (exp(x) - 1.0);",dit= `
2024-01-17 22:56:53 +01:00
vec4 result ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01: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 ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,hit=It({opSnippet:fit,packedOpSnippet:dit}),bB={kernelName:ls,backendName:"webgl",kernelFunc:hit};var git="return (b >= 0.0) ? a : a * (b + 1.0);",xit= `
2024-01-17 22:56:53 +01:00
vec4 bGTEZero = vec4 ( greaterThanEqual ( b , vec4 ( 0. ) ) ) ;
return ( bGTEZero * a ) + ( ( vec4 ( 1.0 ) - bGTEZero ) * ( a * ( b + vec4 ( 1.0 ) ) ) ) ;
2024-09-10 17:30:23 +02:00
` ,yit=r=>{let{inputs:t,backend:e}=r,{dy:n,y:o}=t,s=L().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new jn(xit,n.shape,o.shape):new $ n(git,n.shape,o.shape);return e.runWebGLProgram(s,[n,o],n.dtype)},wB={kernelName:La,backendName:"webgl",kernelFunc:yit};var bit= `
2024-01-17 22:56:53 +01:00
return vec4 ( equal ( a , b ) ) ;
2024-09-10 17:30:23 +02:00
` ,wit="return float(a == b);",Iit=ce({opSnippet:wit,packedOpSnippet:bit,dtype:"bool",cpuKernelImpl:JL}),IB={kernelName:za,backendName:"webgl",kernelFunc:Iit};var Cit= `
2024-01-17 22:56:53 +01:00
// Error function is calculated approximately with elementary function.
// See "Handbook of Mathematical Functions with Formulas,
// Graphs, and Mathematical Tables", Abramowitz and Stegun.
float p = $ { S . ERF _P } ;
float a1 = $ { S . ERF _A1 } ;
float a2 = $ { S . ERF _A2 } ;
float a3 = $ { S . ERF _A3 } ;
float a4 = $ { S . ERF _A4 } ;
float a5 = $ { S . ERF _A5 } ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
` ,vit=It({opSnippet:Cit}),CB={kernelName:us,backendName:"webgl",kernelFunc:vit};var Sit=Po+ `
2024-01-17 22:56:53 +01:00
return exp ( x ) ;
2024-09-10 17:30:23 +02:00
` ,Nit= `
2024-01-17 22:56:53 +01:00
vec4 result = exp ( x ) ;
2024-01-17 22:44:24 +01:00
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 ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:44:24 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,z1=It({opSnippet:Sit,packedOpSnippet:Nit,cpuKernelImpl:QL,dtype:"float32"}),vB={kernelName:cs,backendName:"webgl",kernelFunc:z1};function HI(r){let{inputs:t,attrs:e,backend:n}=r,{dim:o}=e,{input:s}=t,i=s.shape.length,a=s.shape.slice(),u=o;return o<0&&(y.assert(-(i+1)<=o,()=> ` Axis must be in the interval [ $ { - ( i + 1 ) } , $ { i } ] ` ),u=i+o+1),a.splice(u,0,1),rt({inputs:{x:s},backend:n,attrs:{shape:a}})}var SB={kernelName:Mi,backendName:"webgl",kernelFunc:HI};var NB="return exp(x) - 1.0;",kit=It({opSnippet:NB,packedOpSnippet:NB,cpuKernelImpl:tz}),kB={kernelName:ps,backendName:"webgl",kernelFunc:kit};var hg=class{constructor(t,e,n){this.variableNames=["real","imag"];let o=e[1];this.outputShape=e;let s=n? ` 2.0 * $ { Math . PI } ` : ` - 2.0 * $ { Math . PI } ` ,i=n? ` $ { o } . 0 ` :"1.0",a;if(t==="real")a="return real * expR - imag * expI;";else if(t==="imag")a="return real * expI + imag * expR;";else throw new Error( ` FFT component must be either "real" or "imag" , got $ { t } . ` );this.userCode= `
2024-01-17 22:56:53 +01:00
const float exponentMultiplier = $ { s } ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float unaryOpComplex ( float real , float expR , float imag , float expI ) {
$ { a }
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float mulMatDFT ( int batch , int index ) {
float indexRatio = float ( index ) / float ( $ { o } ) ;
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float result = 0.0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { o } ; i ++ ) {
// x = (-2|2 * PI / N) * index * i;
float x = exponentMultiplierTimesIndexRatio * float ( i ) ;
float expR = cos ( x ) ;
float expI = sin ( x ) ;
float real = getReal ( batch , i ) ;
float imag = getImag ( batch , i ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
result +=
unaryOpComplex ( real , expR , imag , expI ) / $ { i } ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
return result ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
setOutput ( mulMatDFT ( coords [ 0 ] , coords [ 1 ] ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function qI(r,t,e){let n=e.texData.get(r.dataId),o=y.sizeFromShape(r.shape),s=r.shape[r.shape.length-1],i=o/s,a=rt({inputs:{x:r},backend:e,attrs:{shape:[i,s]}}),u=a.shape,l=new hg("real",u,t),c=new hg("imag",u,t),p=[{dataId:n.complexTensorInfos.real.dataId,dtype:n.complexTensorInfos.real.dtype,shape:u},{dataId:n.complexTensorInfos.imag.dataId,dtype:n.complexTensorInfos.imag.dtype,shape:u}],m=e.runWebGLProgram(l,p,"float32"),f=e.runWebGLProgram(c,p,"float32"),d=Rn({inputs:{real:m,imag:f},backend:e});e.disposeIntermediateTensorInfo(m),e.disposeIntermediateTensorInfo(f);let h=rt({inputs:{x:d},backend:e,attrs:{shape:r.shape}});return e.disposeIntermediateTensorInfo(a),e.disposeIntermediateTensorInfo(d),h}function Tit(r){let{inputs:t,backend:e}=r,{input:n}=t;return qI(n,!1,e)}var TB={kernelName:Op,backendName:"webgl",kernelFunc:Tit};var KI=class{constructor(t,e){this.outputShape=[],this.customUniforms=[{name:"value",type:"float"}],this.variableNames=["x"],this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
// Input can be obtained from uniform value.
setOutput ( value ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Ll(r){let{backend:t,attrs:e}=r,{shape:n,value:o}=e,{dtype:s}=e;if(s=s||y.inferDtype(o),s==="string"){let i=y.getArrayFromDType(s,y.sizeFromShape(n));return i.fill(o),t.makeTensorInfo(n,s,i)}else{let i=new KI(n,o),a=[[o]];return t.runWebGLProgram(i,[],s,a)}}var _B={kernelName:Jl,backendName:"webgl",kernelFunc:Ll};var jI=class{constructor(t){this.variableNames=["Image"],this.outputShape=[];let e=t[2];this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int x = coords [ 2 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
int coordX = $ { e } - x - 1 ;
float outputValue ;
if ( coordX >= 0 && coordX < $ { e } ) {
outputValue = getImage ( coords [ 0 ] , coords [ 1 ] , coordX , coords [ 3 ] ) ;
} else {
outputValue = getImage ( coords [ 0 ] , coords [ 1 ] , coords [ 2 ] , coords [ 3 ] ) ;
}
setOutput ( outputValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};var EB={kernelName:Ba,backendName:"webgl",kernelFunc:({inputs:r,backend:t})=>{let{image:e}=r,n=t,o=new jI(e.shape);return n.runWebGLProgram(o,[e],e.dtype)}};var AB="return floor(x);",_it=It({opSnippet:AB,packedOpSnippet:AB,cpuKernelImpl:ez}),DB={kernelName:ms,backendName:"webgl",kernelFunc:_it};var Eit= `
2024-01-17 22:56:53 +01: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 ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` ,Ait= `
2024-01-17 22:56:53 +01: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 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
if ( cond [ 0 ] ) {
result [ 0 ] = idiv ( ia [ 0 ] , ib [ 0 ] , s [ 0 ] ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( cond [ 1 ] ) {
result [ 1 ] = idiv ( ia [ 1 ] , ib [ 1 ] , s [ 1 ] ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( cond [ 2 ] ) {
result [ 2 ] = idiv ( ia [ 2 ] , ib [ 2 ] , s [ 2 ] ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( cond [ 3 ] ) {
result [ 3 ] = idiv ( ia [ 3 ] , ib [ 3 ] , s [ 3 ] ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
return vec4 ( result ) ;
2024-09-10 17:30:23 +02:00
` ,Dit=ce({opSnippet:Eit,packedOpSnippet:Ait,dtype:"int32"}), $ B={kernelName:fs,backendName:"webgl",kernelFunc:Dit};var XI=class{constructor(t){this.variableNames=["A"];let e=Ue(),[n,o]=t;this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
int texR = coords [ 0 ] ;
int texC = coords [ 1 ] ;
int depth = coords [ 2 ] ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { o } . 0 , $ { n } . 0 ) ;
vec4 values = $ { e . 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 ;
}
setOutput ( floor ( value * 255.0 + 0.5 ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var YI=class{constructor(t){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let e=Ue(),[n,o]=t;this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
int texR = coords [ 0 ] ;
int texC = coords [ 1 ] ;
int depth = coords [ 2 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( 0. ) ;
2020-12-15 14:15:43 +01:00
2024-01-17 22:56:53 +01:00
for ( int row = 0 ; row <= 1 ; row ++ ) {
for ( int col = 0 ; col <= 1 ; col ++ ) {
texC = coords [ 1 ] + row ;
depth = coords [ 2 ] + col ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
vec2 ( $ { o } . 0 , $ { n } . 0 ) ;
vec4 values = $ { e . 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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
result [ row * 2 + col ] = floor ( value * 255.0 + 0.5 ) ;
}
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { e . output } = result ;
}
2024-09-10 17:30:23 +02:00
` }};var RB={kernelName:th,backendName:"webgl",kernelFunc: $ it},zd,B1=L().getBool("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU");function $ it(r){let{inputs:t,backend:e,attrs:n}=r,{pixels:o}=t,{numChannels:s}=n,i=typeof HTMLVideoElement!="undefined"&&o instanceof HTMLVideoElement,a=typeof HTMLImageElement!="undefined"&&o instanceof HTMLImageElement,[u,l]=i?[o.videoWidth,o.videoHeight]:[o.width,o.height],c=[l,u],p=[l,u,s];if(a||i){let h=L().getBool("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU");(zd==null||h!==B1)&&(B1=h,zd=document.createElement("canvas").getContext("2d",{willReadFrequently:B1})),zd.canvas.width=u,zd.canvas.height=l,zd.drawImage(o,0,0,u,l),o=zd.canvas}let m=e.makeTensorInfo(c,"int32");e.texData.get(m.dataId).usage=Jr.PIXELS,e.gpgpu.uploadPixelDataToTexture(e.getTexture(m.dataId),o);let f=L().getBool("WEBGL_PACK")?new YI(p):new XI(p),d=e.runWebGLProgram(f,[m],"int32");return e.disposeData(m.dataId),d}function Rit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,filter:s,bias:i,preluActivationWeights:a}=t,{strides:u,pad:l,dataFormat:c,dilations:p,dimRoundingMode:m,activation:f,leakyreluAlpha:d}=n,h=S.convertConv2DDataFormat(c),g=S.computeConv2DInfo(o.shape,s.shape,u,p,l,m,!1,h),x,b=[],w=i!=null,I=a!=null,N=f==="leakyrelu",E=()=>{let D=[o,s],F=(M,V)=>{if(V==="NCHW"&&M.shape.length===1&&M.shape[0]!==1){let G=rt({inputs:{x:M},backend:e,attrs:{shape:[M.shape[0],1,1]}});return b.push(G),G}return M};if(w&&D.push(F(i,c)),I&&D.push(F(a,c)),N){let M=e.makeTensorInfo([],"float32",y.createScalarValue(d,"float32"));D.push(M),b.push(M)}return D};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"))x=DI({x:o,filter:s,convInfo:g,backend:e,bias:i,activation:f,preluActivationWeights:a,leakyreluAlpha:d});else if(g.strideWidth<=2&&h==="channelsLast"&&L().getBool("WEBGL_EXP_CONV")){let D=f?Ml(f,!0):null,F=new Md(g,w,D,I,N),M=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]],V=E();x=e.runWebGLProgram(F,V,"float32",M)}else if(L().getBool("WEBGL_CONV_IM2COL"))x= $ I({x:o,filter:s,convInfo:g,backend:e,bias:i,activation:f,preluActivationWeights:a,leakyreluAlpha:d});else{let D=f?Ml(f,!1):null,F=new Od(g,w,D,I,N),M=E();x=e.runWebGLProgram(F,M,"float32")}let A=rt({inputs:{x},backend:e,attrs:{shape:g.outShape}});return b.push(x),b.forEach(D=>e.disposeIntermediateTensorInfo(D)),A}var FB={kernelName:Yi,backendName:"webgl",kernelFunc:Rit};function Fit(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,filter:s,bias:i,preluActivationWeights:a}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:p,activation:m,leakyreluAlpha:f}=n,d=[],h=c;h==null&&(h=[1,1]),y.assert(S.eitherStridesOrDilationsAreOne(u,h),()=> ` Error in depthwiseConv2d : Either strides or dilations must be 1. Got strides $ { u } and dilations '${h}' ` );let g=S.computeConv2DInfo(o.shape,s.shape,u,h,l,p,!0),x=L().getBool("WEBGL_PACK_DEPTHWISECONV")&&g.strideWidth<=2&&g.outChannels/g.inChannels===1,b=m?Ml(m,x):null,w=[o,s],I=i!=null,N=a!=null,E=m==="leakyrelu";if(I&&w.push(i),N&&w.push(a),E){let M=e.makeTensorInfo([],"float32",y.createScalarValue(f,"float32"));w.push(M),d.push(M)}let A;x?A=new Ld(g,I,b,N,E):A=new Pd(g,I,b,N,E);let D=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]],F=e.runWebGLProgram(A,w,"float32",D);return d.forEach(M=>e.disposeIntermediateTensorInfo(M)),F}var OB={kernelName:Zi,backendName:"webgl",kernelFunc:Fit};var ZI=class{constructor(t,e,n,o){this.sliceDim=t,this.strides=e,this.paramsShape=o,this.variableNames=["x","indices"],this.outputShape=n;let s=zt(n.length),i= `
2024-01-17 22:56:53 +01:00
int index ; ` ;for(let a=0;a<this.sliceDim;a++)i+= `
index = round ( getIndices ( coords [ 0 ] , $ { a } ) ) ;
out _of _bounds = out _of _bounds || index < 0 ;
out _of _bounds = out _of _bounds || index >= $ { this . paramsShape [ a ] } ;
flattenIndex += index * $ { this . strides [ a ] } ; ` ;this.userCode= `
void main ( ) {
$ { s } coords = getOutputCoords ( ) ;
int flattenIndex = 0 ;
bool out _of _bounds = false ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { i }
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( out _of _bounds ? 0.0 : getX ( flattenIndex , coords [ 1 ] ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Oit(r){let{inputs:t,backend:e}=r,{params:n,indices:o}=t,s=o.shape,i=s[s.length-1],a=y.sizeFromShape(n.shape),[u,l,c,p]=S.prepareAndValidate(n,o),m=rt({inputs:{x:o},backend:e,attrs:{shape:[l,i]}}),f=rt({inputs:{x:n},backend:e,attrs:{shape:[y.sizeFromShape(n.shape)/c,c]}});if(e.shouldExecuteOnCPU([n,o])||n.dtype==="string"){let x=e.readSync(o.dataId),b=e.bufferSync(n),w=rz(x,b,n.dtype,l,i,c,p,n.shape,a);return e.makeTensorInfo(u,n.dtype,w.values)}let d=new ZI(i,p,[l,c],n.shape),h=e.runWebGLProgram(d,[f,m],f.dtype),g=rt({inputs:{x:h},backend:e,attrs:{shape:u}});return e.disposeIntermediateTensorInfo(m),e.disposeIntermediateTensorInfo(f),e.disposeIntermediateTensorInfo(h),g}var MB={kernelName:Va,backendName:"webgl",kernelFunc:Oit};var JI=class{constructor(t,e){this.variableNames=["A","indices"],this.outputShape=e,this.rank=e.length;let n=zt(this.rank),o=Mit(t,2);this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { n } resRC = getOutputCoords ( ) ;
int index = int ( getIndices ( resRC . x , resRC . z ) ) ;
float inBounds = ( index >= 0 ) && ( index < $ { t [ 2 ] } ) ? 1.0 : 0.0 ;
setOutput ( inBounds * getA ( $ { o } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Mit(r,t){let e=["resRC.x","resRC.y","resRC.z","resRC.w"],n=[];for(let o=0;o<r.length;o++)o===2?n.push("index"):n.push( ` $ { e [ o ] } ` );return n.join()}function V1(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,indices:s}=t,{axis:i,batchDims:a}=n,u=y.parseAxisParam(i,o.shape)[0];if(L().get("DEBUG")){let b=e.readSync(s.dataId),w=o.shape[u];for(let I=0;I<b.length;++I){let N=b[I];y.assert(N<=w-1&&N>=0,()=> ` GatherV2 : the index value $ { N } is not in [ 0 , $ { w - 1 } ] ` )}}let l=S.segment_util.collectGatherOpShapeInfo(o,s,u,a),c=y.sizeFromShape(s.shape),p=[],m=rt({inputs:{x:o},backend:e,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),f=rt({inputs:{x:s},backend:e,attrs:{shape:[l.batchSize,c/l.batchSize]}});p.push(m),p.push(f);let d=[l.batchSize,l.outerSize,c/l.batchSize,l.sliceSize];if(e.shouldExecuteOnCPU([o,s])||o.dtype==="string"){let b=e.bufferSync(f),w=e.bufferSync(m),I=nz(w,b,d);return p.forEach(N=>e.disposeIntermediateTensorInfo(N)),e.makeTensorInfo(l.outputShape,I.dtype,I.values)}let h=new JI(m.shape,d),g=e.runWebGLProgram(h,[m,f],m.dtype);p.push(g);let x=rt({inputs:{x:g},backend:e,attrs:{shape:l.outputShape}});return p.forEach(b=>e.disposeIntermediateTensorInfo(b)),x}var PB={kernelName:Pi,backendName:"webgl",kernelFunc:V1};var Pit="return float(a > b);",Lit= `
2024-01-17 22:56:53 +01:00
return vec4 ( greaterThan ( a , b ) ) ;
2024-09-10 17:30:23 +02:00
` ,zit=ce({opSnippet:Pit,packedOpSnippet:Lit,cpuKernelImpl:oz,dtype:"bool"}),LB={kernelName:Ga,backendName:"webgl",kernelFunc:zit};var Bit="return float(a >= b);",Vit= `
2024-01-17 22:56:53 +01:00
return vec4 ( greaterThanEqual ( a , b ) ) ;
2024-09-10 17:30:23 +02:00
` ,Git=ce({opSnippet:Bit,packedOpSnippet:Vit,dtype:"bool",cpuKernelImpl:sz}),zB={kernelName:hs,backendName:"webgl",kernelFunc:Git};function Wit(r){let{inputs:t,backend:e}=r,{input:n}=t;return qI(n,!0,e)}var BB={kernelName:Mp,backendName:"webgl",kernelFunc:Wit};var Uit="return float(!isnan(x) && !isinf(x));",Hit=It({opSnippet:Uit,dtype:"bool"}),VB={kernelName:gs,backendName:"webgl",kernelFunc:Hit};var qit="return float(isinf(x));",Kit=It({opSnippet:qit,dtype:"bool"}),GB={kernelName:xs,backendName:"webgl",kernelFunc:Kit};var jit="return float(isnan(x));",Xit=It({opSnippet:jit,dtype:"bool"}),WB={kernelName:ys,backendName:"webgl",kernelFunc:Xit};var Yit="return float(a < b);",Zit= `
2024-01-17 22:56:53 +01:00
return vec4 ( lessThan ( a , b ) ) ;
2024-09-10 17:30:23 +02:00
` ,Jit=ce({opSnippet:Yit,packedOpSnippet:Zit,cpuKernelImpl:iz,dtype:"bool"}),UB={kernelName:Wa,backendName:"webgl",kernelFunc:Jit};var Qit="return float(a <= b);",tat= `
2024-01-17 22:56:53 +01:00
return vec4 ( lessThanEqual ( a , b ) ) ;
2024-09-10 17:30:23 +02:00
` ,eat=ce({opSnippet:Qit,packedOpSnippet:tat,cpuKernelImpl:az,dtype:"bool"}),HB={kernelName:Ua,backendName:"webgl",kernelFunc:eat};function rat(r){let{backend:t,attrs:e}=r,{start:n,stop:o,num:s}=e,i=lz(n,o,s);return t.makeTensorInfo([i.length],"float32",i)}var qB={kernelName:Ha,backendName:"webgl",kernelFunc:rat};var nat=Po+ `
2024-01-17 22:56:53 +01:00
return x < 0.0 ? 0. / 0. : log ( x ) ;
2024-09-10 17:30:23 +02:00
` ,oat= `
2024-01-17 22:56:53 +01:00
vec4 result = log ( x ) ;
bvec4 isNaN = isnan ( x ) ;
result . r = isNaN . r ? x . r : ( x . r < 0.0 ? 0. / 0. : result . r ) ;
result . g = isNaN . g ? x . g : ( x . g < 0.0 ? 0. / 0. : result . g ) ;
result . b = isNaN . b ? x . b : ( x . b < 0.0 ? 0. / 0. : result . b ) ;
result . a = isNaN . a ? x . a : ( x . a < 0.0 ? 0. / 0. : result . a ) ;
return result ;
2024-09-10 17:30:23 +02:00
` ,sat=It({opSnippet:nat,packedOpSnippet:oat,cpuKernelImpl:uz}),KB={kernelName:ws,backendName:"webgl",kernelFunc:sat};var iat=Po+ `
2024-01-17 22:56:53 +01:00
return log ( 1.0 + x ) ;
2024-09-10 17:30:23 +02:00
` ,aat=It({opSnippet:iat}),jB={kernelName:Is,backendName:"webgl",kernelFunc:aat};var lat="return float(a >= 1.0 && b >= 1.0);",uat= `
2024-01-17 22:56:53 +01:00
return vec4 (
vec4 ( greaterThanEqual ( a , vec4 ( 1.0 ) ) ) *
vec4 ( greaterThanEqual ( b , vec4 ( 1.0 ) ) ) ) ;
2024-09-10 17:30:23 +02:00
` ,cat=ce({opSnippet:lat,packedOpSnippet:uat,dtype:"bool"}),XB={kernelName:qa,backendName:"webgl",kernelFunc:cat};var pat="return float(!(x >= 1.0));",mat=It({opSnippet:pat}),YB={kernelName:Ka,backendName:"webgl",kernelFunc:mat};var fat="return float(a >= 1.0 || b >= 1.0);",dat= `
2024-01-17 22:56:53 +01:00
return min (
vec4 ( greaterThanEqual ( a , vec4 ( 1.0 ) ) ) +
vec4 ( greaterThanEqual ( b , vec4 ( 1.0 ) ) ) ,
vec4 ( 1.0 ) ) ;
2024-09-10 17:30:23 +02:00
` ,hat=ce({opSnippet:fat,packedOpSnippet:dat,dtype:"bool"}),ZB={kernelName:ja,backendName:"webgl",kernelFunc:hat};var QI=class{constructor(t,e,n,o,s){this.variableNames=["x"],this.outputShape=[];let i=e,a=t[3]-1;this.outputShape=t;let u,l= ` float ( $ { n } ) + float ( $ { o } ) * sum ` ;s===.5?u= ` inversesqrt ( $ { l } ) ` :s===1?u= ` 1.0 / ( $ { l } ) ` :u= ` exp ( log ( $ { l } ) * float ( - $ { s } ) ) ; ` ,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
int d = coords [ 3 ] ;
float x = getX ( b , r , c , d ) ;
float sum = 0.0 ;
for ( int j = - $ { i } ; j <= $ { i } ; j ++ ) {
int idx = d + j ;
if ( idx >= 0 && idx <= $ { a } ) {
float z = getX ( b , r , c , idx ) ;
sum += z * z ;
}
}
float val = x * $ { u } ;
setOutput ( val ) ;
}
2024-09-10 17:30:23 +02:00
` }};var tC=class{constructor(t,e,n,o,s){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let i=e,a=t[3]-1;this.outputShape=t;let u,l= ` float ( $ { n } ) + float ( $ { o } ) * sum ` ;s===.5?u= ` inversesqrt ( $ { l } ) ` :s===1?u= ` 1.0 / ( $ { l } ) ` :u= ` exp ( log ( $ { l } ) * float ( - $ { s } ) ) ; ` ,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords . x ;
int r = coords . y ;
int c = coords . z ;
int d = coords . w ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
bool hasNextCol = d < $ { this . outputShape [ 3 ] } ;
bool hasNextRow = c < $ { this . outputShape [ 2 ] } ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec4 sum = vec4 ( 0. ) ;
vec4 xFragAtOutputCoords = getX ( b , r , c , d ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
vec4 xAtOutputCoords = vec4 (
getChannel ( xFragAtOutputCoords , vec2 ( c , d ) ) ,
hasNextCol ?
getChannel ( xFragAtOutputCoords , vec2 ( c , d + 1 ) ) : 0.0 ,
hasNextRow ?
getChannel ( xFragAtOutputCoords , vec2 ( c + 1 , d ) ) : 0.0 ,
( hasNextRow && hasNextCol ) ?
getChannel ( xFragAtOutputCoords , vec2 ( c + 1 , d + 1 ) ) : 0.0
) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
int firstChannel = d - $ { i } ;
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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
ivec2 depth = ivec2 ( d , d + 1 ) ;
for ( int j = - $ { i } ; j <= $ { i } ; j ++ ) {
ivec2 idx = depth + j ;
bvec2 aboveLowerBound = greaterThanEqual ( idx , ivec2 ( 0 ) ) ;
bvec2 belowUpperBound = lessThanEqual ( idx , ivec2 ( $ { a } ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
bool depthInRange = aboveLowerBound . x && belowUpperBound . x ;
bool depthPlusOneInRange = aboveLowerBound . y && belowUpperBound . y ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( depthInRange || depthPlusOneInRange ) {
vec4 z = vec4 ( 0. ) ;
vec4 xFragAtCurrentDepth ;
z . xz = cache . xy ;
if ( depthPlusOneInRange && hasNextCol ) {
xFragAtCurrentDepth = idx . y != d ?
getX ( b , r , c , idx . y ) : xFragAtOutputCoords ;
z . y = getChannel ( xFragAtCurrentDepth , vec2 ( c , idx . y ) ) ;
if ( hasNextRow ) {
z . w = getChannel ( xFragAtCurrentDepth , vec2 ( c + 1 , idx . y ) ) ;
}
}
cache . xy = z . yw ;
sum += z * z ;
}
}
vec4 result = xAtOutputCoords * $ { u } ;
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var gat=r=>{let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{depthRadius:s,bias:i,alpha:a,beta:u}=n,l=L().getBool("WEBGL_PACK_NORMALIZATION")?new tC(o.shape,s,i,a,u):new QI(o.shape,s,i,a,u);return e.runWebGLProgram(l,[o],o.dtype)},JB={kernelName:Cs,backendName:"webgl",kernelFunc:gat};var eC=class{constructor(t,e,n,o,s){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=t,this.depth=t[3],this.depthRadius=e,this.bias=n,this.alpha=o,this.beta=s,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
float result = 0.0 ;
for ( int d = 0 ; d < $ { this . depth } ; ++ d ) {
int depthBegin = int ( max ( 0.0 , float ( d - $ { e } ) ) ) ;
int depthEnd = int ( min ( float ( $ { this . depth } ) ,
float ( d + $ { e } + 1 ) ) ) ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
const int MIN _DEPTH _BEGIN = 0 ;
const int MAX _DEPTH _END = $ { this . depth } ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float norm = 0.0 ;
for ( int k = MIN _DEPTH _BEGIN ; k < MAX _DEPTH _END ; ++ k ) {
if ( k < depthBegin ) {
continue ;
}
else if ( k >= depthBegin && k < depthEnd ) {
norm += getInputImage ( b , r , c , k ) * getInputImage ( b , r , c , k ) ;
}
else {
break ;
}
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
norm = float ( $ { o } ) * norm + float ( $ { n } ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int k = MIN _DEPTH _BEGIN ; k < MAX _DEPTH _END ; ++ k ) {
if ( k < depthBegin ) {
continue ;
}
else if ( k >= depthBegin && k < depthEnd ) {
float dyi = - 2.0 * float ( $ { o } )
* float ( $ { s } )
* getInputImage ( b , r , c , k ) * getOutputImage ( b , r , c , d )
/ n o r m ;
if ( k == d ) {
dyi += pow ( norm , - 1.0 * $ { s } ) ;
}
if ( k == coords [ 3 ] ) {
dyi *= getDy ( b , r , c , d ) ;
result += dyi ;
}
}
else {
break ;
}
}
}
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var xat=r=>{let{inputs:t,backend:e,attrs:n}=r,{x:o,y:s,dy:i}=t,{depthRadius:a,bias:u,alpha:l,beta:c}=n,p=new eC(o.shape,a,u,l,c);return e.runWebGLProgram(p,[o,s,i],o.dtype)},QB={kernelName:Xa,backendName:"webgl",kernelFunc:xat};function tV(r,t,e,n){let o=y.sizeFromShape(t),i=y.sizeFromShape(r.shape)/o,a=rt({inputs:{x:r},attrs:{shape:[i,o]},backend:n}),u=Yn(a,r.dtype,"max",n),l=rt({inputs:{x:u},attrs:{shape:e},backend:n});return n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(u),l}function G1(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{reductionIndices:s,keepDims:i}=n,a=o.shape.length,u=y.parseAxisParam(s,o.shape),l=u,c=S.getAxesPermutation(l,a),p=c!=null,m=e.shouldExecuteOnCPU([o]),f=o;if(p){if(m){let w=e.texData.get(f.dataId).values,I=new Array(a);for(let A=0;A<I.length;A++)I[A]=o.shape[c[A]];let N=mp(w,o.shape,o.dtype,c,I);f=e.makeTensorInfo(I,o.dtype);let E=e.texData.get(f.dataId);E.values=N}else f=Hu(o,c,e);l=S.getInnerMostAxes(l.length,a)}S.assertAxesAreInnerMostDims("max",l,a);let[d,h]=S.computeOutAndReduceShapes(f.shape,l),g=d;i&&(g=S.expandShapeToKeepDim(d,u));let x;if(m){let w=e.texData.get(f.dataId).values,I=cz(w,y.sizeFromShape(h),g,o.dtype);x=e.makeTensorInfo(g,o.dtype);let N=e.texData.get(x.dataId);N.values=I}else x=tV(f,h,g,e);return p&&e.disposeIntermediateTensorInfo(f),x}var eV={kernelName:vs,backendName:"webgl",kernelFunc:G1};var yat= $ d+ `
2024-01-17 22:56:53 +01:00
return max ( a , b ) ;
2024-09-10 17:30:23 +02:00
` ,bat= `
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( max ( a , b ) ) ;
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 ) ;
` +Xn+ `
return result ;
2024-09-10 17:30:23 +02:00
` ,wat=ce({opSnippet:yat,packedOpSnippet:bat,cpuKernelImpl:pz}),rV={kernelName:Ss,backendName:"webgl",kernelFunc:wat};function Iat(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t;vi(o,"maxPool");let{filterSize:s,strides:i,pad:a,dimRoundingMode:u}=n,l=1;y.assert(S.eitherStridesOrDilationsAreOne(i,l),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { i } and dilations '${l}' ` );let c=S.computePool2DInfo(o.shape,s,i,l,a,u);if(c.filterWidth===1&&c.filterHeight===1&&y.arraysEqual(c.inShape,c.outShape))return nr({inputs:{x:o},backend:e});let p=new Ni(c,"max",!1);return e.runWebGLProgram(p,[o],o.dtype)}var nV={kernelName:Ns,backendName:"webgl",kernelFunc:Iat};function Cat(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{filterSize:s,strides:i,pad:a,dataFormat:u,dimRoundingMode:l}=n,c=[1,1,1],p=S.computePool3DInfo(o.shape,s,i,c,a,l,u),m=new qu(p,"max",!1);return e.runWebGLProgram(m,[o],o.dtype)}var oV={kernelName:Li,backendName:"webgl",kernelFunc:Cat};var rC=class{constructor(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;let e=t.strideHeight,n=t.strideWidth,o=t.dilationHeight,s=t.effectiveFilterHeight,i=t.effectiveFilterWidth,a=s-1-t.padInfo.top,u=i-1-t.padInfo.left,l=s*i-1;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec2 pads = ivec2 ( $ { a } , $ { u } ) ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:44:24 +01:00
void main ( ) {
2024-01-17 22:56:53 +01:00
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
ivec2 dyRCCorner = coords . yz - pads ;
int dyRCorner = dyRCCorner . x ;
int dyCCorner = dyRCCorner . y ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
for ( int wR = 0 ; wR < $ { s } ;
wR += $ { o } ) {
float dyR = float ( dyRCorner + wR ) / $ { e } . 0 ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
continue ;
}
int idyR = int ( dyR ) ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { i } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { n } . 0 ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float dyValue = getDy ( b , idyR , idyC , d ) ;
int maxPosValue = $ { l } - int ( getMaxPos ( b , idyR , idyC , d ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// Get the current value, check it against the value from the
// position matrix.
int curPosValue = wR * $ { i } + wC ;
float mask = float ( maxPosValue == curPosValue ? 1.0 : 0.0 ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
dotProd += dyValue * mask ;
}
}
setOutput ( dotProd ) ;
}
2024-09-10 17:30:23 +02:00
` }},nC=class{constructor(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;let e=t.strideDepth,n=t.strideHeight,o=t.strideWidth,s=t.dilationDepth,i=t.dilationHeight,a=t.dilationWidth,u=t.effectiveFilterDepth,l=t.effectiveFilterHeight,c=t.effectiveFilterWidth,p=u-1-t.padInfo.front,m=l-1-t.padInfo.top,f=c-1-t.padInfo.left,d=u*l*c-1;this.userCode= `
2024-01-17 22:56:53 +01:00
const ivec3 pads = ivec3 ( $ { p } , $ { m } , $ { f } ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
ivec3 dyCorner = ivec3 ( coords . y , coords . z , coords . w ) - pads ;
int dyDCorner = dyCorner . x ;
int dyRCorner = dyCorner . y ;
int dyCCorner = dyCorner . z ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// Convolve dy(?, ?, ?, ch) with pos mask(:, :, :, d) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int wD = 0 ; wD < $ { u } ;
wD += $ { s } ) {
float dyD = float ( dyDCorner + wD ) / $ { e } . 0 ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
if ( dyD < 0.0 || dyD >= $ { t . outDepth } . 0 || fract ( dyD ) > 0.0 ) {
continue ;
}
int idyD = int ( dyD ) ;
for ( int wR = 0 ; wR < $ { l } ;
wR += $ { i } ) {
float dyR = float ( dyRCorner + wR ) / $ { n } . 0 ;
if ( dyR < 0.0 || dyR >= $ { t . outHeight } . 0 ||
fract ( dyR ) > 0.0 ) {
continue ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
int idyR = int ( dyR ) ;
2021-07-29 15:05:49 +02:00
2024-01-17 22:56:53 +01:00
for ( int wC = 0 ; wC < $ { c } ;
wC += $ { a } ) {
float dyC = float ( dyCCorner + wC ) / $ { o } . 0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( dyC < 0.0 || dyC >= $ { t . outWidth } . 0 ||
fract ( dyC ) > 0.0 ) {
continue ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
int idyC = int ( dyC ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dyValue = getDy ( batch , idyD , idyR , idyC , ch ) ;
int maxPosValue = $ { d } -
int ( getMaxPos ( batch , idyD , idyR , idyC , ch ) ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
// Get the current value, check it against the value from the
// position matrix.
int curPosValue =
wD * $ { l } * $ { c } +
wR * $ { c } + wC ;
float mask = float ( maxPosValue == curPosValue ? 1.0 : 0.0 ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
dotProd += dyValue * mask ;
}
}
}
setOutput ( dotProd ) ;
}
2024-09-10 17:30:23 +02:00
` }};function vat(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,input:s}=t,i=s,{filterSize:a,strides:u,pad:l,dimRoundingMode:c}=n,p=[1,1,1],m=S.computePool3DInfo(i.shape,a,u,p,l,c),f=new qu(m,"max",!0),d=e.runWebGLProgram(f,[i],i.dtype),h=new nC(m),g=e.runWebGLProgram(h,[o,d],i.dtype);return e.disposeIntermediateTensorInfo(d),g}var sV={kernelName:tu,backendName:"webgl",kernelFunc:vat};function Sat(r){let{inputs:t,backend:e,attrs:n}=r,{dy:o,input:s,output:i}=t,a=s;vi([s,i],"maxPoolGrad");let{filterSize:u,strides:l,pad:c,dimRoundingMode:p}=n,m=S.computePool2DInfo(a.shape,u,l,1,c,p),f=!0,d=new Ni(m,"max",f),h=e.runWebGLProgram(d,[a],a.dtype),g=new rC(m),x=e.runWebGLProgram(g,[o,h],a.dtype);return e.disposeIntermediateTensorInfo(h),x}var iV={kernelName:Ql,backendName:"webgl",kernelFunc:Sat};function aV(r,t,e,n){let o=new Ni(e,"max",!1),s=n.runWebGLProgram(o,[r],"float32");o=new Ni(e,"max",!0,!0,t);let i=n.runWebGLProgram(o,[r],"float32");return[s,i]}var lV={kernelName:eu,backendName:"webgl",kernelFunc:({inputs:r,attrs:t,backend:e})=>{let{x:n}=r,{filterSize:o,strides:s,pad:i,includeBatchInIndex:a}=t,u=e;y.assert(n.shape.length===4,()=> ` Error in maxPool : input must be rank 4 but got rank $ { n . shape . length } . ` );let l=[1,1];y.assert(S.eitherStridesOrDilationsAreOne(s,l),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { s } and dilations '${l}' ` );let c=S.computePool2DInfo(n.shape,o,s,l,i),[p,m]=aV(n,a,c,u);return[p,m]}};function uV(r,t,e,n){let o=y.sizeFromShape(t),i=y.sizeFromShape(r.shape)/o,a=rt({inputs:{x:r},attrs:{shape:[i,o]},backend:n}),u=Yn(a,"float32","mean",n),l=rt({inputs:{x:u},attrs:{shape:e},backend:n});return n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(u),l}var cV={kernelName:ks,backendName:"webgl",kernelFunc:({inputs:r,attrs:t,backend:e})=>{let{x:n}=r,{keepDims:o,axis:s}=t,i=e,a=n.shape.length,u=y.parseAxisParam(s,n.shape),l=u,c=S.getAxesPermutation(l,a),p=c!=null,m=i.shouldExecuteOnCPU([n]),f=[],d=n;if(p){if(m){let I=i.texData.get(d.dataId).values,N=new Array(a);for(let D=0;D<N.length;D++)N[D]=n.shape[c[D]];let E=mp(I,n.shape,n.dtype,c,N);d=i.makeTensorInfo(N,n.dtype);let A=i.texData.get(d.dataId);A.values=E}else d=Hu(n,c,i);f.push(d),l=S.getInnerMostAxes(l.length,a)}S.assertAxesAreInnerMostDims("sum",l,a);let[h,g]=S.computeOutAndReduceShapes(d.shape,l),x=h;o&&(x=S.expandShapeToKeepDim(h,u));let b=uV(d,g,x,i);for(let w of f)i.disposeIntermediateTensorInfo(w);return b}};function Nat(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,keepDims:i}=n,a=o.shape.length,u=y.parseAxisParam(s,o.shape),l=u,c=S.getAxesPermutation(l,a),p=o;c!=null&&(p=Pe({inputs:{x:o},backend:e,attrs:{perm:c}}),l=S.getInnerMostAxes(l.length,o.shape.length)),S.assertAxesAreInnerMostDims("min",l,a);let[m,f]=S.computeOutAndReduceShapes(p.shape,l),d=y.sizeFromShape(f),h=rt({inputs:{x:p},backend:e,attrs:{shape:[-1,d]}}),g=Yn(h,h.dtype,"min",e),x;if(i){let b=S.expandShapeToKeepDim(m,u);x=rt({inputs:{x:g},backend:e,attrs:{shape:b}})}else x=rt({inputs:{x:g},backend:e,attrs:{shape:m}});return e.disposeIntermediateTensorInfo(h),e.disposeIntermediateTensorInfo(g),c!=null&&e.disposeIntermediateTensorInfo(p),x}var pV={kernelName:Ts,backendName:"webgl",kernelFunc:Nat};var kat= $ d+ `
2024-01-17 22:56:53 +01:00
return min ( a , b ) ;
2024-09-10 17:30:23 +02:00
` ,Tat= `
2024-01-17 22:56:53 +01:00
vec4 result = vec4 ( min ( a , b ) ) ;
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 ) ;
` +Xn+ `
return result ;
2024-09-10 17:30:23 +02:00
` ,_at=ce({opSnippet:kat,packedOpSnippet:Tat,cpuKernelImpl:mz}),mV={kernelName:_s,backendName:"webgl",kernelFunc:_at};var oC=class{constructor(t,e,n){this.variableNames=["x"],this.outputShape=e.map((c,p)=>c[0]+t[p]+c[1]);let o=t.length,s=zt(o),i=e.map(c=>c[0]).join(","),a=e.map((c,p)=>c[0]+t[p]).join(","),u=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,o),l=n==="reflect"?0:1;if(o===1){this.userCode= `
2024-01-17 22:56:53 +01:00
int start = $ { i } ;
int end = $ { a } ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
int outC = getOutputCoords ( ) ;
if ( outC < start ) {
outC = start * 2 - outC - $ { l } ;
} else if ( outC >= end ) {
outC = ( end - 1 ) * 2 - outC + $ { l } ;
}
setOutput ( getX ( outC - start ) ) ;
}
` ;return}this.userCode= `
$ { s } start = $ { s } ( $ { i } ) ;
$ { s } end = $ { s } ( $ { a } ) ;
void main ( ) {
$ { s } outC = getOutputCoords ( ) ;
for ( int i = 0 ; i < $ { o } ; i ++ ) {
if ( outC [ i ] < start [ i ] ) {
outC [ i ] = start [ i ] * 2 - outC [ i ] - $ { l } ;
} else if ( outC [ i ] >= end [ i ] ) {
outC [ i ] = ( end [ i ] - 1 ) * 2 - outC [ i ] + $ { l } ;
}
}
$ { s } coords = outC - start ;
setOutput ( getX ( $ { u } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var sC=class{constructor(t,e,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e.map((d,h)=>d[0]+t[h]+d[1]);let o=t.length,s=zt(o),i=e.map(d=>d[0]).join(","),a=e.map((d,h)=>d[0]+t[h]).join(","),u=rr("rc",o),l=rr("source",o),c= ` $ { u [ o - 1 ] } < $ { this . outputShape [ o - 1 ] } ` ,p=o===1?"source": ` vec2 ( $ { l . slice ( - 2 ) . join ( ) } ) ` ,m=n==="reflect"?0:1,f="";if(o===1){let d= `
2024-01-17 22:56:53 +01:00
$ { s } source = rc ;
if ( source < start ) {
source = start * 2 - source - $ { m } ;
} else if ( source >= end ) {
source = ( end - 1 ) * 2 - source + $ { m } ;
}
source -= start ;
` ;f= `
$ { s } rc = outputLoc ;
$ { d }
result [ 0 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
$ { u [ o - 1 ] } += 1 ;
if ( $ { c } ) {
$ { d }
result [ 1 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
}
` }else{let d= `
$ { s } source = rc ;
$ { s } lt = $ { s } ( lessThan ( source , start ) ) ;
$ { s } gte = $ { s } ( greaterThanEqual ( source , end ) ) ;
$ { s } orig = 1 - ( lt + gte ) ;
source = orig * source +
lt * ( start * 2 - source - $ { m } ) +
gte * ( ( end - 1 ) * 2 - source + $ { m } ) ;
source -= start ;
` ;f= `
$ { s } rc = outputLoc ;
$ { d }
result [ 0 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
$ { u [ o - 1 ] } += 1 ;
if ( $ { c } ) {
$ { d }
result [ 1 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
rc = outputLoc ;
$ { u [ o - 2 ] } += 1 ;
if ( $ { u [ o - 2 ] } < $ { this . outputShape [ o - 2 ] } ) {
$ { d }
result [ 2 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
$ { u [ o - 1 ] } += 1 ;
if ( $ { c } ) {
$ { d }
result [ 3 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
}
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
` }this.userCode= `
const $ { s } start = $ { s } ( $ { i } ) ;
const $ { s } end = $ { s } ( $ { a } ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { s } outputLoc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
$ { f }
setOutput ( result ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};var Eat=({inputs:r,backend:t,attrs:e})=>{let{x:n}=r,{paddings:o,mode:s}=e,i=L().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new sC(n.shape,o,s):new oC(n.shape,o,s);return t.runWebGLProgram(i,[n],n.dtype)},fV={kernelName:Es,backendName:"webgl",kernelFunc:Eat};var Aat= ` if ( b == 0.0 ) return NAN ;
return mod ( a , b ) ; ` ,Dat= `
2024-01-17 22:56:53 +01:00
vec4 result = mod ( a , b ) ;
bvec4 isNaN = equal ( b , vec4 ( 0.0 ) ) ;
` +Xn+ `
return result ;
2024-09-10 17:30:23 +02:00
` , $ at=ce({opSnippet:Aat,packedOpSnippet:Dat}),dV={kernelName:As,backendName:"webgl",kernelFunc: $ at};var iC=class{constructor(t,e,n){this.variableNames=["probs"],this.customUniforms=[{name:"seed",type:"float"}],this.outputShape=[t,n],this.userCode= `
2022-10-18 13:23:49 +02:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
2024-01-17 22:44:24 +01:00
int batch = coords [ 0 ] ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
float r = random ( seed ) ;
float cdf = 0.0 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { e - 1 } ; i ++ ) {
cdf += getProbs ( batch , i ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
if ( r < cdf ) {
setOutput ( float ( i ) ) ;
return ;
}
2022-10-18 13:23:49 +02:00
}
2024-01-17 22:56:53 +01:00
// If no other event happened, last event happened.
setOutput ( float ( $ { e - 1 } ) ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};var Rat= `
2024-01-17 22:56:53 +01:00
if ( a == b ) {
return 1.0 ;
2024-01-17 22:44:24 +01:00
} ;
2024-09-10 17:30:23 +02:00
return a / b ; ` ,Fat= `
2024-01-17 22:56:53 +01: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. ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( a . y == b . y ) {
result . y = 1. ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( a . z == b . z ) {
result . z = 1. ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( a . w == b . w ) {
result . w = 1. ;
2024-01-17 22:44:24 +01:00
}
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,W1=ce({opSnippet:Rat,packedOpSnippet:Fat,checkOutOfBounds:!0}),hV={kernelName:as,backendName:"webgl",kernelFunc:W1};var gV="return a - b;",U1=ce({opSnippet:gV,packedOpSnippet:gV,supportsComplex:!0,cpuKernelImpl:Rz}),xV={kernelName:ei,backendName:"webgl",kernelFunc:U1};function H1(r){let{inputs:t,backend:e,attrs:n}=r,{logits:o}=t,{dim:s}=n,i=y.parseAxisParam([s],o.shape),a=G1({inputs:{x:o},backend:e,attrs:{reductionIndices:i,keepDims:!1}}),u=S.expandShapeToKeepDim(a.shape,i),l=rt({inputs:{x:a},backend:e,attrs:{shape:u}}),c=U1({inputs:{a:o,b:l},backend:e}),p=z1({inputs:{x:c},backend:e}),m=fp({inputs:{x:p},backend:e,attrs:{axis:i,keepDims:!1}}),f=rt({inputs:{x:m},backend:e,attrs:{shape:u}}),d=W1({inputs:{a:p,b:f},backend:e});return e.disposeIntermediateTensorInfo(a),e.disposeIntermediateTensorInfo(l),e.disposeIntermediateTensorInfo(c),e.disposeIntermediateTensorInfo(p),e.disposeIntermediateTensorInfo(m),e.disposeIntermediateTensorInfo(f),d}var yV={kernelName:Qs,backendName:"webgl",kernelFunc:H1};function Oat(r){let{inputs:t,backend:e,attrs:n}=r,{logits:o}=t,{numSamples:s,seed:i,normalized:a}=n,u=a?o:H1({inputs:{logits:o},backend:e,attrs:{dim:o.shape.length-1}}),l=u.shape[0],c=u.shape[1],p=new iC(l,c,s),m=[[i]],f=e.runWebGLProgram(p,[u],"int32",m);return a||e.disposeIntermediateTensorInfo(u),f}var bV={kernelName:Ya,backendName:"webgl",kernelFunc:Oat};var Mat=xr+ `
2024-01-17 22:56:53 +01:00
return - x ;
2024-09-10 17:30:23 +02:00
` ,Pat= `
2024-01-17 22:56:53 +01:00
vec4 result = - x ;
bvec4 isNaN = isnan ( x ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01: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 ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ;function Lat(r){let{inputs:t,backend:e}=r,{x:n}=t;if(e.shouldExecuteOnCPU([n])){let s=e.texData.get(n.dataId),[i,a]=dz(s.values,n.shape,n.dtype);return e.makeTensorInfo(a,n.dtype,i)}let o;return L().getBool("WEBGL_PACK_UNARY_OPERATIONS")?o=new Dn(n.shape,Pat):o=new zr(n.shape,Mat),e.runWebGLProgram(o,[n],n.dtype)}var wV={kernelName:zi,backendName:"webgl",kernelFunc:Lat};var zat=Xr.nonMaxSuppressionV3Impl;function Bat(r){S.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:e,attrs:n}=r,{boxes:o,scores:s}=t,{maxOutputSize:i,iouThreshold:a,scoreThreshold:u}=n,l=e.readSync(o.dataId),c=e.readSync(s.dataId),{selectedIndices:p}=zat(l,c,i,a,u);return e.makeTensorInfo([p.length],"int32",new Int32Array(p))}var IV={kernelName:Ja,backendName:"webgl",kernelFunc:Bat};var Vat=Xr.nonMaxSuppressionV4Impl;function Gat(r){S.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:e,attrs:n}=r,{boxes:o,scores:s}=t,{maxOutputSize:i,iouThreshold:a,scoreThreshold:u,padToMaxOutputSize:l}=n,c=e.readSync(o.dataId),p=e.readSync(s.dataId),{selectedIndices:m,validOutputs:f}=Vat(c,p,i,a,u,l);return[e.makeTensorInfo([m.length],"int32",new Int32Array(m)),e.makeTensorInfo([],"int32",new Int32Array([f]))]}var CV={kernelName:Qa,backendName:"webgl",kernelFunc:Gat};var Wat=Xr.nonMaxSuppressionV5Impl;function Uat(r){S.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:e,attrs:n}=r,{boxes:o,scores:s}=t,{maxOutputSize:i,iouThreshold:a,scoreThreshold:u,softNmsSigma:l}=n,c=e.readSync(o.dataId),p=e.readSync(s.dataId),m=i,f=a,d=u,h=l,{selectedIndices:g,selectedScores:x}=Wat(c,p,m,f,d,h);return[e.makeTensorInfo([g.length],"int32",new Int32Array(g)),e.makeTensorInfo([x.length],"float32",new Float32Array(x))]}var vV={kernelName:tl,backendName:"webgl",kernelFunc:Uat};var aC=class{constructor(t,e,n,o){this.variableNames=["indices"],this.outputShape=[t,e],this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int index = round ( getIndices ( coords . x ) ) ;
setOutput ( mix ( float ( $ { o } ) , float ( $ { n } ) ,
float ( index == coords . y ) ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var Hat=r=>{let{inputs:t,backend:e,attrs:n}=r,{indices:o}=t,{dtype:s,depth:i,onValue:a,offValue:u}=n,l=y.sizeFromShape(o.shape),c=new aC(l,i,a,u),p=rt({inputs:{x:o},backend:e,attrs:{shape:[l]}}),m=e.runWebGLProgram(c,[p],s);e.disposeIntermediateTensorInfo(p);let f=[...o.shape,i],d=rt({inputs:{x:m},backend:e,attrs:{shape:f}});return e.disposeIntermediateTensorInfo(m),d},SV={kernelName: $ s,backendName:"webgl",kernelFunc:Hat};function gg(r){let{inputs:t,backend:e}=r,{x:n}=t;if(n.dtype==="complex64"){let o=Pl({inputs:{input:n},backend:e}),s=gg({inputs:{x:o},backend:e}),i=hp({inputs:{input:n},backend:e}),a=gg({inputs:{x:i},backend:e}),u=Rn({inputs:{real:s,imag:a},backend:e});return e.disposeIntermediateTensorInfo(o),e.disposeIntermediateTensorInfo(s),e.disposeIntermediateTensorInfo(i),e.disposeIntermediateTensorInfo(a),u}else return Ll({attrs:{shape:n.shape,dtype:n.dtype,value:n.dtype==="string"?"":0},backend:e})}var NV={kernelName:ji,backendName:"webgl",kernelFunc:gg};function kV(r){let{inputs:t,backend:e}=r,{x:n}=t;if(n.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(n.dtype==="complex64"){let o=Pl({inputs:{input:n},backend:e}),s=kV({inputs:{x:o},backend:e}),i=hp({inputs:{input:n},backend:e}),a=gg({inputs:{x:i},backend:e}),u=Rn({inputs:{real:s,imag:a},backend:e});return e.disposeIntermediateTensorInfo(o),e.disposeIntermediateTensorInfo(s),e.disposeIntermediateTensorInfo(i),e.disposeIntermediateTensorInfo(a),u}else return Ll({attrs:{shape:n.shape,dtype:n.dtype,value:1},backend:e})}var TV={kernelName:Bi,backendName:"webgl",kernelFunc:kV};function qat(r){let{inputs:t,backend:e,attrs:n}=r,{axis:o}=n;if(t.length===1)return HI({inputs:{input:t[0]},backend:e,attrs:{dim:o}});let s=t[0].shape,i=t[0].dtype;t.forEach(c=>{y.assertShapesMatch(s,c.shape,"All tensors passed to stack must have matching shapes"),y.assert(i===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let a=[],u=t.map(c=>{let p=HI({inputs:{input:c},backend:e,attrs:{dim:o}});return a.push(p),p}),l=L1({inputs:u,backend:e,attrs:{axis:o}});return a.forEach(c=>e.disposeIntermediateTensorInfo(c)),l}var _V={kernelName:Vi,backendName:"webgl",kernelFunc:qat};var lC=class{constructor(t,e,n){this.variableNames=["x"],this.customUniforms=[{name:"value",type:"float"}],this.outputShape=e.map((l,c)=>l[0]+t[c]+l[1]);let o=t.length,s=zt(o),i=e.map(l=>l[0]).join(","),a=e.map((l,c)=>l[0]+t[c]).join(","),u=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,o);if(o===1){this.userCode= `
2024-01-17 22:56:53 +01:00
int start = $ { i } ;
int end = $ { a } ;
2020-12-02 22:46:41 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
int outC = getOutputCoords ( ) ;
if ( outC < start || outC >= end ) {
setOutput ( value ) ;
} else {
setOutput ( getX ( outC - start ) ) ;
}
}
` ;return}this.userCode= `
$ { s } start = $ { s } ( $ { i } ) ;
$ { s } end = $ { s } ( $ { a } ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { s } outC = getOutputCoords ( ) ;
if ( any ( lessThan ( outC , start ) ) || any ( greaterThanEqual ( outC , end ) ) ) {
setOutput ( value ) ;
} else {
$ { s } coords = outC - start ;
setOutput ( getX ( $ { u } ) ) ;
}
2021-04-01 19:39:54 +02:00
}
2024-09-10 17:30:23 +02:00
` }};var uC=class{constructor(t,e,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"value",type:"float"}],this.outputShape=e.map((h,g)=>h[0]+t[g]+h[1]);let o=t.length,s=zt(o),i=e.map(h=>h[0]).join(","),a=e.map((h,g)=>h[0]+t[g]).join(","),u=rr("rc",o),l=rr("source",o),c= ` $ { u [ o - 1 ] } < $ { this . outputShape [ o - 1 ] } ` ,p=o===1?"source": ` vec2 ( $ { l . slice ( - 2 ) . join ( ) } ) ` ,m=[ ` $ { s } rc = outputLoc ; ` , ` $ { u [ o - 1 ] } += 1 ;
2024-01-17 22:56:53 +01:00
if ( $ { c } ) {
` ,o===1?"": ` }
rc = outputLoc ;
$ { u [ o - 2 ] } += 1 ;
if ( $ { u [ o - 2 ] } < $ { this . outputShape [ o - 2 ] } ) { ` ,o===1?"": ` $ { u [ o - 1 ] } += 1 ;
if ( $ { c } ) { ` ],f=o===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",d="";for(let h=0,g=o===1?2:4;h<g;h++)d+= `
$ { m [ h ] }
if ( $ { f } ) {
result [ $ { h } ] = float ( value ) ;
} else {
$ { s } source = rc - start ;
result [ $ { h } ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { p } ) ;
}
` ;d+=o===1?"} ":"}}",this.userCode= `
const $ { s } start = $ { s } ( $ { i } ) ;
const $ { s } end = $ { s } ( $ { a } ) ;
2020-12-15 14:15:43 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { s } outputLoc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
$ { d }
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` }};var q1=r=>{let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{paddings:s,constantValue:i}=n;if(y.sizeFromShape(o.shape)===0){let l=s.map((c,p)=>c[0]+o.shape[p]+c[1]);return Ll({backend:e,attrs:{shape:l,value:i,dtype:o.dtype}})}let a=L().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new uC(o.shape,s,i):new lC(o.shape,s,i),u=[[i]];return e.runWebGLProgram(a,[o],o.dtype,u)},EV={kernelName:Rs,backendName:"webgl",kernelFunc:q1};var Kat= `
2024-01-17 22:56:53 +01:00
if ( a < 0.0 && floor ( b ) < b ) {
return NAN ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
if ( b == 0.0 ) {
return 1.0 ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
return ( round ( mod ( b , 2.0 ) ) != 1 ) ?
pow ( abs ( a ) , b ) : sign ( a ) * pow ( abs ( a ) , b ) ;
2024-09-10 17:30:23 +02:00
` ,jat= `
2024-01-17 22:56:53 +01: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 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ;
` +Xn+ `
return result ;
2024-09-10 17:30:23 +02:00
` ,Xat=ce({opSnippet:Kat,packedOpSnippet:jat}),AV={kernelName:Fs,backendName:"webgl",kernelFunc:Xat};function Yat(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{axis:s,keepDims:i}=n,a=o.shape.length,u=[],l=y.parseAxisParam(s,o.shape),c=l,p=S.getAxesPermutation(c,a),m=o;p!=null&&(m=Pe({inputs:{x:o},backend:e,attrs:{perm:p}}),c=S.getInnerMostAxes(c.length,a),u.push(m)),S.assertAxesAreInnerMostDims("prod",c,a);let f;if(e.shouldExecuteOnCPU([m])){let d=e.texData.get(m.dataId).values,{outVals:h,outShape:g,outDtype:x}=gz(m.shape,m.dtype,d,c);f=e.makeTensorInfo(g,x,h)}else{let[d,h]=S.computeOutAndReduceShapes(m.shape,c),g=y.sizeFromShape(h),x=rt({inputs:{x:m},backend:e,attrs:{shape:[-1,g]}}),b=lc(o.dtype),w=Yn(x,b,"prod",e);f=rt({inputs:{x:w},backend:e,attrs:{shape:d}}),u.push(x),u.push(w)}if(i){u.push(f);let d=S.expandShapeToKeepDim(f.shape,l);f=rt({inputs:{x:f},backend:e,attrs:{shape:d}})}return u.forEach(d=>e.disposeIntermediateTensorInfo(d)),f}var DV={kernelName:Ms,backendName:"webgl",kernelFunc:Yat};function Zat(r){let{inputs:t,backend:e,attrs:n}=r,{paramsNestedSplits:o,paramsDenseValues:s,indices:i}=t,{outputRaggedRank:a}=n,u=o.map(x=>e.readSync(x.dataId)),l=o.map(x=>x.shape),c=e.readSync(s.dataId),p=e.readSync(i.dataId),[m,f,d]=xz(u,l,c,s.shape,s.dtype,p,i.shape,a),h=m.map(x=>e.makeTensorInfo([x.length],"int32",x)),g=e.makeTensorInfo(d,s.dtype,f);return h.concat([g])}var $ V={kernelName:Lp,backendName:"webgl",kernelFunc:Zat};function Jat(r){let{inputs:t,backend:e}=r,{starts:n,limits:o,deltas:s}=t,i=e.readSync(n.dataId),a=e.readSync(o.dataId),u=e.readSync(s.dataId),[l,c]=yz(i,n.shape,n.dtype,a,o.shape,u,s.shape),p=e.makeTensorInfo([l.length],"int32",l),m=e.makeTensorInfo([c.length],n.dtype,c);return[p,m]}var RV={kernelName:zp,backendName:"webgl",kernelFunc:Jat};function Qat(r){let{inputs:t,backend:e,attrs:n}=r,{shape:o,values:s,defaultValue:i,rowPartitionTensors:a}=t,{rowPartitionTypes:u}=n,l=e.readSync(o.dataId),c=e.readSync(s.dataId),p=e.readSync(i.dataId),m=a.map(g=>e.readSync(g.dataId)),f=a.map(g=>g.shape),[d,h]=bz(l,o.shape,c,s.shape,s.dtype,p,i.shape,m,f,u);return e.makeTensorInfo(d,s.dtype,h)}var FV={kernelName:Bp,backendName:"webgl",kernelFunc:Qat};var K1=r=>{let{backend:t,attrs:e}=r,{start:n,stop:o,step:s,dtype:i}=e,a=wz(n,o,s,i);return t.makeTensorInfo([a.length],i,a)},OV={kernelName:ru,backendName:"webgl",kernelFunc:K1};var tlt="return 1.0 / x;",elt=It({opSnippet:tlt}),MV={kernelName:Ps,backendName:"webgl",kernelFunc:elt};var rlt=xr+ `
2024-01-17 22:56:53 +01:00
return ( x < 0.0 ) ? 0.0 : x ;
2024-09-10 17:30:23 +02:00
` ,nlt= `
2024-01-17 22:56:53 +01:00
vec4 result = x * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
bvec4 isNaN = isnan ( x ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,olt=It({opSnippet:rlt,packedOpSnippet:nlt}),PV={kernelName:Ls,backendName:"webgl",kernelFunc:olt};var slt=xr+ `
2024-01-17 22:56:53 +01:00
return ( x < 0.0 ) ? 0.0 : min ( 6.0 , x ) ;
2024-09-10 17:30:23 +02:00
` ,ilt= `
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
` ,alt=It({opSnippet:slt,packedOpSnippet:ilt}),LV={kernelName:Vs,backendName:"webgl",kernelFunc:alt};var cC=class{constructor(t,e,n,o,s){this.variableNames=["A"],this.outputShape=[];let[i,a,u,l]=t;this.outputShape=[i,e,n,l];let c=[o&&e>1?a-1:a,o&&n>1?u-1:u],p=[o&&e>1?e-1:e,o&&n>1?n-1:n],m;s?m="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":m="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2024-01-17 22:56:53 +01:00
const vec2 effectiveInputOverOutputRatioRC = vec2 (
$ { c [ 0 ] / p [ 0 ] } ,
$ { c [ 1 ] / p [ 1 ] } ) ;
const vec2 inputShapeRC = vec2 ( $ { a } . 0 , $ { u } . 0 ) ;
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
ivec2 yRC = coords . yz ;
// Fractional source index.
vec2 sourceFracIndexRC = $ { m } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Compute the four integer indices.
ivec2 sourceFloorRC = ivec2 ( max ( sourceFracIndexRC , vec2 ( 0.0 ) ) ) ;
ivec2 sourceCeilRC = ivec2 (
min ( inputShapeRC - 1.0 , ceil ( sourceFracIndexRC ) ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec2 fracRC = sourceFracIndexRC - vec2 ( sourceFloorRC ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float top = topLeft + ( topRight - topLeft ) * fracRC . y ;
float bottom = bottomLeft + ( bottomRight - bottomLeft ) * fracRC . y ;
float newValue = top + ( bottom - top ) * fracRC . x ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( newValue ) ;
2022-10-18 13:10:36 +02:00
}
2024-09-10 17:30:23 +02:00
` }};var pC=class{constructor(t,e,n,o,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[i,a,u,l]=t;this.outputShape=[i,e,n,l];let c=[o&&e>1?a-1:a,o&&n>1?u-1:u],p=[o&&e>1?e-1:e,o&&n>1?n-1:n],m;s?m="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":m="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2024-01-17 22:56:53 +01:00
const vec3 effectiveInputOverOutputRatioRC = vec3 (
$ { c [ 0 ] / p [ 0 ] } ,
$ { c [ 1 ] / p [ 1 ] } ,
$ { c [ 1 ] / p [ 1 ] } ) ;
const vec3 inputShapeRC = vec3 ( $ { a } . 0 , $ { u } . 0 ,
$ { u } . 0 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float getAValue ( int b , int r , int c , int d ) {
return getChannel ( getA ( b , r , c , d ) , vec2 ( c , d ) ) ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Fractional source index.
vec3 sourceFracIndexRC = $ { m } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Compute the four integer indices.
ivec3 sourceFloorRC = ivec3 ( max ( sourceFracIndexRC , vec3 ( 0.0 ) ) ) ;
ivec3 sourceCeilRC = ivec3 (
min ( inputShapeRC - 1.0 , ceil ( sourceFracIndexRC ) ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Should we calculate next column and row elements in 2x2 packed cell.
bool hasNextCol = d < $ { l - 1 } ;
bool hasNextRow = coords . z < $ { n - 1 } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01: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 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec3 fracRC = sourceFracIndexRC - vec3 ( sourceFloorRC ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec4 top = mix ( topLeft , topRight , fracRC . yyzz ) ;
vec4 bottom = mix ( bottomLeft , bottomRight , fracRC . yyzz ) ;
vec4 newValue = mix ( top , bottom , fracRC . x ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( newValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};function llt(r){let{inputs:t,backend:e,attrs:n}=r,{images:o}=t,{alignCorners:s,halfPixelCenters:i,size:a}=n,[u,l]=a,c=L().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new pC(o.shape,u,l,s,i):new cC(o.shape,u,l,s,i);return e.runWebGLProgram(c,[o],"float32")}var zV={kernelName:Bs,backendName:"webgl",kernelFunc:llt};var mC=class{constructor(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e;let[,o,s]=e,[,i,a]=t,u=[n&&i>1?o-1:o,n&&a>1?s-1:s],l=[n&&i>1?i-1:i,n&&a>1?a-1:a],c=u[0]/l[0],p=u[1]/l[1],m=1/c,f=1/p,d=Math.ceil(m)*2+2,h=Math.ceil(f)*2+2;this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float accumulator = 0.0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
const float heightScale = float ( $ { c } ) ;
const float widthScale = float ( $ { p } ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
const float invHeightScale = float ( $ { m } ) ;
const float invWidthScale = float ( $ { f } ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
const int winHeight = int ( $ { d } ) ;
const int winWidth = int ( $ { h } ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Compute bounds for where in dy we will look
float startRLerp = floor ( float ( r ) * invHeightScale ) ;
int startDyR = int ( startRLerp - float ( winHeight / 2 ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float startCLerp = floor ( float ( c ) * invWidthScale ) ;
int startDyC = int ( startCLerp - float ( winWidth / 2 ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Loop over dy
for ( int dyROffset = 0 ; dyROffset < winHeight ; dyROffset ++ ) {
int dyR = dyROffset + startDyR ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Guard against the window exceeding the bounds of dy
if ( dyR < 0 || dyR >= $ { i } ) {
continue ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int dyCOffset = 0 ; dyCOffset < winWidth ; dyCOffset ++ ) {
int dyC = dyCOffset + startDyC ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Guard against the window exceeding the bounds of dy
if ( dyC < 0 || dyC >= $ { a } ) {
continue ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dxR = float ( dyR ) * heightScale ;
int topDxRIndex = int ( floor ( dxR ) ) ;
int bottomDxRIndex = int ( min ( ceil ( dxR ) , $ { o - 1 } . 0 ) ) ;
float dxRLerp = dxR - float ( topDxRIndex ) ;
float inverseDxRLerp = 1.0 - dxRLerp ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float dxC = float ( dyC ) * widthScale ;
int leftDxCIndex = int ( floor ( dxC ) ) ;
int rightDxCIndex = int ( min ( ceil ( dxC ) , $ { s - 1 } . 0 ) ) ;
float dxCLerp = dxC - float ( leftDxCIndex ) ;
float inverseDxCLerp = 1.0 - dxCLerp ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( r == topDxRIndex && c == leftDxCIndex ) {
// topLeft
accumulator +=
getDy ( b , dyR , dyC , d ) * inverseDxRLerp * inverseDxCLerp ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( r == topDxRIndex && c == rightDxCIndex ) {
// topRight
accumulator += getDy ( b , dyR , dyC , d ) * inverseDxRLerp * dxCLerp ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( r == bottomDxRIndex && c == leftDxCIndex ) {
// bottomLeft
accumulator += getDy ( b , dyR , dyC , d ) * dxRLerp * inverseDxCLerp ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( r == bottomDxRIndex && c == rightDxCIndex ) {
// bottomRight
accumulator += getDy ( b , dyR , dyC , d ) * dxRLerp * dxCLerp ;
}
}
}
// End loop over dy
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( accumulator ) ;
}
2024-09-10 17:30:23 +02:00
` }};function ult(r){let{inputs:t,backend:e,attrs:n}=r,{images:o,dy:s}=t,{alignCorners:i}=n,a=new mC(s.shape,o.shape,i);return e.runWebGLProgram(a,[s],s.dtype)}var BV={kernelName:rl,backendName:"webgl",kernelFunc:ult};var fC=class{constructor(t,e,n,o,s){this.variableNames=["A"],this.outputShape=[];let[i,a,u,l]=t;this.outputShape=[i,e,n,l];let c=[o&&e>1?a-1:a,o&&n>1?u-1:u],p=[o&&e>1?e-1:e,o&&n>1?n-1:n],m=o?"0.5":"0.0",f;s?f="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":f="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2024-01-17 22:56:53 +01:00
const vec2 effectiveInputOverOutputRatioRC = vec2 (
$ { c [ 0 ] / p [ 0 ] } ,
$ { c [ 1 ] / p [ 1 ] } ) ;
const vec2 inputShapeRC = vec2 ( $ { a } . 0 , $ { u } . 0 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
ivec2 yRC = coords . yz ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Fractional source index.
vec2 sourceFracIndexRC = $ { f } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestRC = ivec2 (
min ( inputShapeRC - 1.0 , floor ( sourceFracIndexRC + $ { m } ) ) ) ;
float newValue = getA ( b , sourceNearestRC . x , sourceNearestRC . y , d ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( newValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};var dC=class{constructor(t,e,n,o,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[i,a,u,l]=t;this.outputShape=[i,e,n,l];let c=[o&&e>1?a-1:a,o&&n>1?u-1:u],p=[o&&e>1?e-1:e,o&&n>1?n-1:n],m=o?"0.5":"0.0",f;s?f="max((vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC, vec3(0.0))":f="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2024-01-17 22:56:53 +01:00
const vec3 effectiveInputOverOutputRatioRC = vec3 (
$ { c [ 0 ] / p [ 0 ] } ,
$ { c [ 1 ] / p [ 1 ] } ,
$ { c [ 1 ] / p [ 1 ] } ) ;
const vec3 inputShapeRC = vec3 ( $ { a } . 0 , $ { u } . 0 ,
$ { u } . 0 ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float getAValue ( int b , int r , int c , int d ) {
return getChannel ( getA ( b , r , c , d ) , vec2 ( c , d ) ) ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
// Fractional source index.
vec3 sourceFracIndexRC = $ { f } ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
// Compute the coordinators of nearest neighbor point.
ivec3 sourceNearestRC = ivec3 (
min ( inputShapeRC - 1.0 , floor ( sourceFracIndexRC + $ { m } ) ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
// Should we calculate next column and row elements in 2x2 packed cell.
bool hasNextCol = d < $ { l - 1 } ;
bool hasNextRow = coords . z < $ { n - 1 } ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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 ) ;
2022-05-18 14:36:05 +02:00
2024-01-17 22:56:53 +01:00
setOutput ( newValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};function clt(r){let{inputs:t,backend:e,attrs:n}=r,{images:o}=t,{alignCorners:s,halfPixelCenters:i,size:a}=n,[u,l]=a,c=L().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new dC(o.shape,u,l,s,i):new fC(o.shape,u,l,s,i);return e.runWebGLProgram(c,[o],o.dtype)}var VV={kernelName:zs,backendName:"webgl",kernelFunc:clt};var hC=class{constructor(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e;let[,o,s]=e,[,i,a]=t,u=[n&&i>1?o-1:o,n&&a>1?s-1:s],l=[n&&i>1?i-1:i,n&&a>1?a-1:a],c=u[0]/l[0],p=u[1]/l[1],m=1/c,f=1/p,d=Math.ceil(m)*2+2,h=Math.ceil(f)*2+2;this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2022-05-18 14:36:05 +02:00
2024-01-17 22:56:53 +01:00
float accumulator = 0.0 ;
2022-05-18 14:36:05 +02:00
2024-01-17 22:56:53 +01:00
const float heightScale = float ( $ { c } ) ;
const float widthScale = float ( $ { p } ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
const float invHeightScale = float ( $ { m } ) ;
const float invWidthScale = float ( $ { f } ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
const int winHeight = int ( $ { d } ) ;
const int winWidth = int ( $ { h } ) ;
2022-10-18 13:23:49 +02:00
2024-01-17 22:56:53 +01:00
// Compute bounds for where in dy we will look
float startRLerp = floor ( float ( r ) * invHeightScale ) ;
int startDyR = int ( floor ( startRLerp - float ( winHeight / 2 ) ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float startCLerp = floor ( float ( c ) * invWidthScale ) ;
int startDyC = int ( floor ( startCLerp - float ( winWidth / 2 ) ) ) ;
2023-03-21 13:00:18 +01:00
2024-01-17 22:56:53 +01:00
// Loop over dy
for ( int dyROffset = 0 ; dyROffset < winHeight ; dyROffset ++ ) {
int dyR = dyROffset + startDyR ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
// Guard against the window exceeding the bounds of dy
if ( dyR < 0 || dyR >= $ { i } ) {
continue ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
for ( int dyCOffset = 0 ; dyCOffset < winWidth ; dyCOffset ++ ) {
int dyC = dyCOffset + startDyC ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
// Guard against the window exceeding the bounds of dy
if ( dyC < 0 || dyC >= $ { a } ) {
continue ;
}
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
float sourceFracRow =
float ( $ { u [ 0 ] } ) *
( float ( dyR ) / float ( $ { l [ 0 ] } ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
float sourceFracCol =
float ( $ { u [ 1 ] } ) *
( float ( dyC ) / float ( $ { l [ 1 ] } ) ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
int sourceNearestRow = int ( min (
float ( int ( $ { o } ) - 1 ) ,
$ { n } ? float ( round ( sourceFracRow ) ) :
float ( floor ( sourceFracRow ) ) ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
int sourceNearestCol = int ( min (
float ( int ( $ { s } ) - 1 ) ,
$ { n } ? float ( round ( sourceFracCol ) ) :
float ( floor ( sourceFracCol ) ) ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( r == sourceNearestRow && c == sourceNearestCol ) {
accumulator += getDy ( b , dyR , dyC , d ) ;
}
}
}
// End loop over dy
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( accumulator ) ;
}
2024-09-10 17:30:23 +02:00
` }};function plt(r){let{inputs:t,backend:e,attrs:n}=r,{images:o,dy:s}=t,{alignCorners:i}=n,a=new hC(s.shape,o.shape,i);return e.runWebGLProgram(a,[s],s.dtype)}var GV={kernelName:el,backendName:"webgl",kernelFunc:plt};var gC=class{constructor(t,e){this.variableNames=["x"];let n=t.length;if(n>4)throw new Error( ` WebGL backend : Reverse of rank - $ { n } tensor is not yet supported ` );if(this.outputShape=t,n===1){this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
int coord = getOutputCoords ( ) ;
setOutput ( getX ( $ { t [ 0 ] } - coord - 1 ) ) ;
}
` ;return}let o=a=>e.indexOf(a)!==-1&&t[a]!==1? ` $ { t [ a ] } - coords [ $ { a } ] - 1 ` : ` coords [ $ { a } ] ` ,s=t.map((a,u)=>o(u)).join(","),i=zt(n);this.userCode= `
void main ( ) {
$ { i } coords = getOutputCoords ( ) ;
setOutput ( getX ( $ { s } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var xC=class{constructor(t,e){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0;let n=t.length;if(n>4)throw new Error( ` WebGL backend : Reverse of rank - $ { n } tensor is not yet supported ` );this.outputShape=t;let o=rr("rc",n),s= ` $ { o [ n - 1 ] } + 1 < $ { this . outputShape [ n - 1 ] } ` ,i= ` $ { o [ n - 2 ] } + 1 < $ { this . outputShape [ n - 2 ] } ` ,a=zt(n);n===1?this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
int rc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
result . r = getChannel ( getX ( $ { t [ 0 ] } - rc - 1 ) ,
$ { t [ 0 ] } - rc - 1 ) ;
if ( $ { s } ) {
result . g = getChannel ( getX ( $ { t [ 0 ] } - ( rc + 1 ) - 1 ) ,
$ { t [ 0 ] } - ( rc + 1 ) - 1 ) ;
}
setOutput ( result ) ;
}
` :this.userCode= `
void main ( ) {
$ { a } rc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
result . r = $ { u ( o . slice ( ) ) } ;
if ( $ { s } ) {
result . g = $ { l ( o . slice ( ) ) } ;
}
if ( $ { i } ) {
result . b = $ { c ( o . slice ( ) ) } ;
if ( $ { s } ) {
result . a = $ { p ( o . slice ( ) ) } ;
}
}
setOutput ( result ) ;
}
2024-09-10 17:30:23 +02:00
` ;function u(d){return m(d)}function l(d){return d[n-1]="("+d[n-1]+" + 1)",m(d)}function c(d){return d[n-2]="("+d[n-2]+" + 1)",m(d)}function p(d){return d[n-1]="("+d[n-1]+" + 1)",d[n-2]="("+d[n-2]+" + 1)",m(d)}function m(d){let h=t.map((b,w)=>f(w,d)),g=h.join(","),x=h.slice(-2).join(",");return ` getChannel ( getX ( $ { g } ) , vec2 ( $ { x } ) ) ` }function f(d,h){return e.indexOf(d)!==-1&&t[d]!==1? ` $ { t [ d ] } - $ { h [ d ] } - 1 ` : ` $ { h [ d ] } ` }}};function mlt(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{dims:s}=n,i=o.shape.length,a=y.parseAxisParam(s,o.shape);if(i===0)return nr({inputs:{x:o},backend:e});let u=L().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new xC(o.shape,a):new gC(o.shape,a);return e.runWebGLProgram(u,[o],o.dtype)}var WV={kernelName:Gs,backendName:"webgl",kernelFunc:mlt};var yC=class{constructor(t,e){this.variableNames=["Image"],this.outputShape=[],this.customUniforms=[{name:"params",type:"vec4"}];let n=t[1],o=t[2];this.outputShape=t;let s="";typeof e=="number"?s= ` float outputValue = $ { e . toFixed ( 2 ) } ; ` :s= `
2024-01-17 22:56:53 +01:00
vec3 fill = vec3 ( $ { e . 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 ] ) ) ;
$ { s }
if ( coordX >= 0 && coordX < $ { o } && coordY >= 0 && coordY < $ { n } ) {
outputValue = getImage ( coords [ 0 ] , coordY , coordX , coords [ 3 ] ) ;
}
setOutput ( outputValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};var UV={kernelName:pl,backendName:"webgl",kernelFunc:({inputs:r,attrs:t,backend:e})=>{let{image:n}=r,{radians:o,fillValue:s,center:i}=t,a=e,u=new yC(n.shape,s),[l,c]=S.getImageCenter(i,n.shape[1],n.shape[2]),p=[[l,c,Math.sin(o),Math.cos(o)]];return a.runWebGLProgram(u,[n],n.dtype,p)}};var flt= `
2024-01-17 22:56:53 +01: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 ;
2024-01-17 22:44:24 +01:00
}
}
2024-09-10 17:30:23 +02:00
` ,dlt=It({opSnippet:flt}),HV={kernelName:Ws,backendName:"webgl",kernelFunc:dlt};var hlt="return inversesqrt(x);",glt=It({opSnippet:hlt,cpuKernelImpl:Iz}),qV={kernelName:Us,backendName:"webgl",kernelFunc:glt};var Ku=class{constructor(t,e,n,o,s,i,a=!0,u=!1){this.variableNames=["updates","indices","defaultValue"],this.outputShape=i;let l=zt(s.length),c=zt(i.length),p="";n===1?p="i":n===2&&(p="i, j");let m= ` getIndices ( $ { p } ) ` ,f="";o===1?f="i":o===2&&(f="i, coords[1]");let d= ` getUpdates ( $ { f } ) ` ,h="";u&&(h="coords[0], coords[1]");let g= ` getDefaultValue ( $ { h } ) ` ,x=e>1?"strides[j]":"strides";this.userCode= `
2024-01-17 22:56:53 +01:00
$ { l } strides = $ { l } ( $ { s } ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { c } coords = getOutputCoords ( ) ;
float sum = 0.0 ;
bool found = false ;
for ( int i = 0 ; i < $ { t } ; i ++ ) {
int flattenedIndex = 0 ;
for ( int j = 0 ; j < $ { e } ; j ++ ) {
int index = round ( $ { m } ) ;
flattenedIndex += index * $ { x } ;
}
if ( flattenedIndex == coords [ 0 ] ) {
sum += $ { d } ;
found = true ;
}
}
setOutput ( mix ( $ { g } , sum , float ( found ) ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};var bC=class{constructor(t,e,n,o,s,i,a=!0,u=!1){this.variableNames=["updates","indices","defaultValue"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=i;let l=zt(s.length),c=zt(i.length),p="";n===1?p="i":n===2&&(p="i, j");let m= ` getIndices ( $ { p } ) ` ,f="";o===1?f="i":o===2&&(f="i, coords[1]");let d= ` getUpdates ( $ { f } ) ` ,h="";u&&(h="coords[0], coords[1]");let g= ` getDefaultValue ( $ { h } ) ` ,x=e>1?"strides[j]":"strides",b=e>1?"strides[j + 1]":"strides";this.userCode= `
2024-01-17 22:56:53 +01:00
$ { l } strides = $ { l } ( $ { s } ) ;
void main ( ) {
$ { c } coords = getOutputCoords ( ) ;
vec4 sum = vec4 ( 0. ) ;
vec4 found = vec4 ( 0. ) ;
for ( int i = 0 ; i < $ { t } ; i += 2 ) {
ivec2 flattenedIndex = ivec2 ( 0 ) ;
for ( int j = 0 ; j < $ { e } ; j += 2 ) {
ivec4 index = round ( $ { m } ) ;
flattenedIndex += index . xz * $ { x } ;
if ( j + 1 < $ { e } ) {
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 = $ { d } ;
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. ) ;
}
}
}
setOutput ( mix ( $ { g } , sum , found ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function xlt(r){let{inputs:t,backend:e,attrs:n}=r,{indices:o,updates:s}=t,{shape:i}=n,{sliceRank:a,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=S.calculateShapes(s,o,i),m=[p/l,l];if(p===0)return e.makeTensorInfo(i,o.dtype);let f=rt({inputs:{x:o},backend:e,attrs:{shape:[u,a]}}),d=rt({inputs:{x:s},backend:e,attrs:{shape:[u,l]}}),h=e.makeTensorInfo([],"float32",new Float32Array([0])),g;L().getBool("WEBGL_PACK")?g=new bC(u,a,f.shape.length,d.shape.length,c,m):g=new Ku(u,a,f.shape.length,d.shape.length,c,m);let x=e.runWebGLProgram(g,[d,f,h],d.dtype),b=rt({inputs:{x},backend:e,attrs:{shape:i}});return e.disposeIntermediateTensorInfo(f),e.disposeIntermediateTensorInfo(d),e.disposeIntermediateTensorInfo(x),e.disposeIntermediateTensorInfo(h),b}var KV={kernelName:nl,backendName:"webgl",kernelFunc:xlt};var wC=class{constructor(t,e,n,o){this.variableNames=["sortedSequence","values"],this.customUniforms=[{name:"numInputs",type:"int"}],this.outputShape=[t,n];let s="while (left < right) {",i= ` for ( int i = 0 ; i < $ { Math . ceil ( Math . log2 ( e + 1 ) ) } ; ++ i ) { if ( left >= right ) break ; ` ,a=L().getNumber("WEBGL_VERSION")===2?s:i,u=o==="left"?"<":"<=";this.userCode= `
2024-01-17 22:56:53 +01:00
int findBound ( int batch , float value ) {
int left = 0 ;
int right = numInputs ;
int mid ;
$ { a }
mid = ( left + right ) / 2 ;
if ( getSortedSequence ( batch , mid ) $ { u } value ) {
left = mid + 1 ;
} else {
right = mid ;
}
}
return right ;
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int valueIndex = coords [ 1 ] ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float value = getValues ( batch , valueIndex ) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( float ( findBound ( batch , value ) ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function ylt(r){let{inputs:t,backend:e,attrs:n}=r,{sortedSequence:o,values:s}=t,{side:i}=n,a=new wC(o.shape[0],o.shape[1],s.shape[1],i),u=[[o.shape[1]]];return e.runWebGLProgram(a,[o,s],"int32",u)}var jV={kernelName:sl,backendName:"webgl",kernelFunc:ylt};var IC=class{constructor(t,e,n){this.variableNames=["c","a","b"],this.outputShape=e;let o,s;if(n>4)throw Error( ` Where for rank $ { n } is not yet supported ` );if(n===1)s="resRC",o="resRC";else{let a=["resRC.x","resRC.y","resRC.z","resRC.w"],u=[],l=[];for(let c=0;c<e.length;c++)l.push( ` $ { a [ c ] } ` ),c<t&&u.push( ` $ { a [ c ] } ` );o=u.join(),s=l.join()}let i=zt(n);this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { i } resRC = getOutputCoords ( ) ;
float cVal = getC ( $ { o } ) ;
if ( cVal >= 1.0 ) {
setOutput ( getA ( $ { s } ) ) ;
} else {
setOutput ( getB ( $ { s } ) ) ;
}
}
2024-09-10 17:30:23 +02:00
` }};function blt(r){let{inputs:t,backend:e}=r,{condition:n,t:o,e:s}=t,i=new IC(n.shape.length,o.shape,o.shape.length);return e.runWebGLProgram(i,[n,o,s],ur(o.dtype,s.dtype))}var XV={kernelName:Wi,backendName:"webgl",kernelFunc:blt};var wlt= `
2024-01-17 22:56:53 +01:00
// Stable and Attracting Fixed Point (0, 1) for Normalized Weights.
// see: https://arxiv.org/abs/1706.02515
float scaleAlpha = $ { S . SELU _SCALEALPHA } ;
float scale = $ { S . SELU _SCALE } ;
return ( x >= 0.0 ) ? scale * x : scaleAlpha * ( exp ( x ) - 1.0 ) ;
2024-09-10 17:30:23 +02:00
` ,Ilt=It({opSnippet:wlt}),YV={kernelName:Hs,backendName:"webgl",kernelFunc:Ilt};var Clt=Po+ `
2024-01-17 22:56:53 +01:00
return 1.0 / ( 1.0 + exp ( - 1.0 * x ) ) ;
2024-09-10 17:30:23 +02:00
` ,vlt= `
2024-01-17 22:56:53 +01:00
vec4 result = 1.0 / ( 1.0 + exp ( - 1.0 * x ) ) ;
bvec4 isNaN = isnan ( x ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,Slt=It({opSnippet:Clt,packedOpSnippet:vlt,cpuKernelImpl:vz}),ZV={kernelName:Xs,backendName:"webgl",kernelFunc:Slt};var Nlt= `
2024-01-17 22:56:53 +01:00
if ( isnan ( x ) ) { return 0.0 ; }
return sign ( x ) ;
2024-09-10 17:30:23 +02:00
` ,klt=It({opSnippet:Nlt}),JV={kernelName:js,backendName:"webgl",kernelFunc:klt};var Tlt=Po+ `
2024-01-17 22:56:53 +01:00
return sin ( x ) ;
2024-09-10 17:30:23 +02:00
` ,_lt= `
2024-01-17 22:56:53 +01:00
vec4 result = sin ( x ) ;
bvec4 isNaN = isnan ( x ) ;
$ { Xn }
return result ;
2024-09-10 17:30:23 +02:00
` ,Elt=It({opSnippet:Tlt,packedOpSnippet:_lt}),QV={kernelName:qs,backendName:"webgl",kernelFunc:Elt};var Alt= `
2024-01-17 22:56:53 +01:00
float e2x = exp ( x ) ;
return ( e2x - 1.0 / e2x ) / 2.0 ;
2024-09-10 17:30:23 +02:00
` ,Dlt=It({opSnippet:Alt}),tG={kernelName:Ks,backendName:"webgl",kernelFunc:Dlt};var $ lt= `
2024-01-17 22:56:53 +01:00
float epsilon = 1.1920928955078125 e - 7 ;
float threshold = log ( epsilon ) + 2.0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
bool too _large = x > - threshold ;
bool too _small = x < threshold ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float result ;
float exp _x = exp ( x ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
if ( too _large ) {
result = x ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
else if ( too _small ) {
result = exp _x ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
else {
result = log ( exp _x + 1.0 ) ;
2024-01-17 22:44:24 +01:00
}
2024-01-17 22:56:53 +01:00
return result ;
2024-09-10 17:30:23 +02:00
` ,Rlt=It({opSnippet: $ lt}),eG={kernelName:Ys,backendName:"webgl",kernelFunc:Rlt};var Flt=r=>{let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{blockShape:s,paddings:i}=n;y.assert(o.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let a=s.reduce((x,b)=>x*b),u=[[0,0]];u.push(...i);for(let x=1+s.length;x<o.shape.length;++x)u.push([0,0]);let l=[],c=q1({inputs:{x:o},backend:e,attrs:{paddings:u,constantValue:0}}),p=S.getReshaped(c.shape,s,a,!1),m=S.getPermuted(p.length,s.length,!1),f=S.getReshapedPermuted(c.shape,s,a,!1),d=rt({inputs:{x:c},backend:e,attrs:{shape:p}}),h=Pe({inputs:{x:d},backend:e,attrs:{perm:m}}),g=rt({inputs:{x:h},backend:e,attrs:{shape:f}});return l.push(c),l.push(d),l.push(h),l.forEach(x=>e.disposeIntermediateTensorInfo(x)),g},rG={kernelName:Hi,backendName:"webgl",kernelFunc:Flt};function Olt(r){let{inputs:t,backend:e}=r,{indices:n,values:o,denseShape:s,defaultValue:i}=t;if(s.shape.length!==1)throw new Error( ` Dense shape must be a vector , saw :
2024-01-17 22:56:53 +01:00
$ { s . shape } ` );if(n.shape.length!==2)throw new Error( ` Indices must be a matrix , saw :
$ { n . shape } ` );if(o.shape.length!==1)throw new Error( ` Values must be a vector , saw :
$ { o . shape } ` );if(i.shape.length!==0)throw new Error( ` Default value must be a scalar , saw :
2024-09-10 17:30:23 +02:00
$ { i . shape } ` );let a=e.readSync(n.dataId),u=e.readSync(o.dataId),l=e.readSync(s.dataId),c=e.readSync(i.dataId)[0],[p,m,f,d,h]=Nz(a,n.shape,n.dtype,u,o.dtype,l,c);return[e.makeTensorInfo(m,n.dtype,p),e.makeTensorInfo([m[0]],o.dtype,f),e.makeTensorInfo([d.length],"bool",new Uint8Array(d.map(g=>Number(g)))),e.makeTensorInfo([h.length],n.dtype,new Int32Array(h))]}var nG={kernelName:nu,backendName:"webgl",kernelFunc:Olt};function Mlt(r){let{inputs:t,backend:e}=r,{inputIndices:n,inputShape:o,newShape:s}=t;if(n.shape.length!==2)throw new Error( ` Input indices should be a matrix but received shape $ { n . shape } ` );if(o.shape.length!==1)throw new Error( ` Input shape should be a vector but received shape $ { o . shape } ` );if(s.shape.length!==1)throw new Error( ` Target shape should be a vector but received shape $ { s . shape } ` );let i=Array.from(e.readSync(o.dataId)),a=e.readSync(n.dataId),u=Array.from(e.readSync(s.dataId)),[l,c,p]=kz(a,n.shape,n.dtype,i,u);return[e.makeTensorInfo(c,n.dtype,l),e.makeTensorInfo([p.length],s.dtype,new Int32Array(p))]}var oG={kernelName:il,backendName:"webgl",kernelFunc:Mlt};function Plt(r){let{inputs:t,backend:e}=r,{data:n,indices:o,segmentIds:s}=t;if(n.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(o.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
2024-01-17 22:56:53 +01:00
$ { o . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
2024-09-10 17:30:23 +02:00
$ { s . shape } ` );let i=e.readSync(n.dataId),a=e.readSync(o.dataId),u=e.readSync(s.dataId),[l,c]=rI(i,n.shape,n.dtype,a,u,!0);return e.makeTensorInfo(c,n.dtype,l)}var sG={kernelName:ou,backendName:"webgl",kernelFunc:Plt};function Llt(r){let{inputs:t,backend:e}=r,{data:n,indices:o,segmentIds:s}=t;if(n.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(o.shape.length!==1)throw new Error( ` Indices should be a vector but received shape
2024-01-17 22:56:53 +01:00
$ { o . shape } ` );if(s.shape.length!==1)throw new Error( ` Segment ids should be a vector but received shape
2024-09-10 17:30:23 +02:00
$ { s . shape } ` );let i=e.readSync(n.dataId),a=e.readSync(o.dataId),u=e.readSync(s.dataId),[l,c]=rI(i,n.shape,n.dtype,a,u);return e.makeTensorInfo(c,n.dtype,l)}var iG={kernelName:su,backendName:"webgl",kernelFunc:Llt};function zlt(r){let{inputs:t,backend:e,attrs:n}=r,{sparseIndices:o,sparseValues:s,defaultValue:i}=t,{outputShape:a}=n,{sliceRank:u,numUpdates:l,sliceSize:c,strides:p,outputSize:m}=S.calculateShapes(s,o,a),f=!1;if(s.dtype==="string"){let x=e.bufferSync(o),b=e.bufferSync(s),w=y.decodeString(e.readSync(i.dataId)[0]),I=Cz(x,b,a,m,c,l,u,p,w,f);return e.makeTensorInfo(a,I.dtype,I.values)}let d=new Ku(l,u,o.shape.length,s.shape.length,p,[m,1],f),h=e.runWebGLProgram(d,[s,o,i],s.dtype),g=rt({inputs:{x:h},backend:e,attrs:{shape:a}});return e.disposeIntermediateTensorInfo(h),g}var aG={kernelName:al,backendName:"webgl",kernelFunc:zlt};function Blt(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{numOrSizeSplits:s,axis:i}=n,a=y.parseAxisParam(i,o.shape)[0],u=S.prepareSplitSize(o,s,a),l=o.shape.length,c=new Array(l).fill(0),p=o.shape.slice();return u.map(m=>{let f=[...p];f[a]=m;let d=ki({inputs:{x:o},backend:e,attrs:{begin:c,size:f}});return c[a]+=m,d})}var lG={kernelName:qi,backendName:"webgl",kernelFunc:Blt};var uG="return sqrt(x);",Vlt=It({opSnippet:uG,packedOpSnippet:uG,cpuKernelImpl:Tz}),cG={kernelName:Zs,backendName:"webgl",kernelFunc:Vlt};var Glt="return x * x;",Wlt=It({opSnippet:Glt}),pG={kernelName:iu,backendName:"webgl",kernelFunc:Wlt};var mG="return (a - b) * (a - b);",Ult=ce({opSnippet:mG,packedOpSnippet:mG}),fG={kernelName:ti,backendName:"webgl",kernelFunc:Ult};function Hlt(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t;if(o.dtype!=="string")throw new Error("Input must be of datatype string");let s=e.readSync(o.dataId),i=S.fromUint8ToStringArray(s),a=_z(i,"string",n);return e.makeTensorInfo(o.shape,"string",a)}var dG={kernelName:ec,backendName:"webgl",kernelFunc:Hlt};function qlt({inputs:r,attrs:t,backend:e}){let{x:n}=r,o=xr+ `
2024-01-17 22:56:53 +01:00
return x > 0.0 ? 1.0 : float ( $ { t . alpha } ) ;
2024-09-10 17:30:23 +02:00
` ,s=new zr(n.shape,o);return e.runWebGLProgram(s,[n],n.dtype)}var hG={kernelName:xo,backendName:"webgl",kernelFunc:qlt};var CC=class{constructor(t,e,n){this.variableNames=["x"],this.outputShape=n;let o=n.length,s=zt(n.length),i=zt(n.length),a="";if(o===1)a="coords * strides + begin";else{let u=0;a=n.map((l,c)=>(u++,n.length===1? ` coords * strides [ $ { c } ] + begin [ $ { c } ] ` : ` coords [ $ { u - 1 } ] * strides [ $ { c } ] + begin [ $ { c } ] ` )).join(",")}this.userCode= `
2024-01-17 22:56:53 +01:00
$ { s } begin = $ { s } ( $ { t } ) ;
$ { s } strides = $ { s } ( $ { e } ) ;
2022-10-18 13:10:36 +02:00
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { i } coords = getOutputCoords ( ) ;
setOutput ( getX ( $ { a } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function Klt(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{begin:s,end:i,strides:a,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:m}=n,{finalShapeSparse:f,finalShape:d,isIdentity:h,sliceDim0:g,isSimpleSlice:x,begin:b,end:w,strides:I}=Be.sliceInfo(o.shape,s,i,a,u,l,c,p,m),N;if(h)N=rt({inputs:{x:o},backend:e,attrs:{shape:d}});else if(g||x){y.assert(o.shape.length>=1,()=> ` Input must have rank at least 1 , got : $ { o . shape . length } ` );let A=Be.computeOutShape(b,w,I),D=ki({inputs:{x:o},backend:e,attrs:{begin:b,size:A}});N=rt({inputs:{x:D},backend:e,attrs:{shape:d}}),e.disposeIntermediateTensorInfo(D)}else if(e.shouldExecuteOnCPU([o])){let D=e.readSync(o.dataId),F=wt(o.shape,o.dtype,D),M=Ez(f,F,I,b);N=e.makeTensorInfo(d,o.dtype,M.values)}else{let D=new CC(b,I,f);N=e.runWebGLProgram(D,[o],o.dtype)}let E=rt({inputs:{x:N},backend:e,attrs:{shape:d}});return e.disposeIntermediateTensorInfo(N),E}var gG={kernelName:ll,backendName:"webgl",kernelFunc:Klt};function jlt(r){let{inputs:t,backend:e,attrs:n}=r,{separator:o,nGramWidths:s,leftPad:i,rightPad:a,padWidth:u,preserveShortSequences:l}=n,{data:c,dataSplits:p}=t,m=e.readSync(c.dataId),f=e.readSync(p.dataId),[d,h]=Az(m,f,o,s,i,a,u,l);return[e.makeTensorInfo([d.length],"string",d),e.makeTensorInfo(p.shape,"int32",h)]}var xG={kernelName:au,backendName:"webgl",kernelFunc:jlt};function Xlt(r){let{inputs:t,backend:e,attrs:n}=r,{skipEmpty:o}=n,{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 a=e.readSync(s.dataId),u=e.readSync(i.dataId)[0],[l,c,p]=Dz(a,u,o),m=c.length;return[e.makeTensorInfo([m,2],"int32",l),e.makeTensorInfo([m],"string",c),e.makeTensorInfo([2],"int32",new Int32Array(p))]}var yG={kernelName:lu,backendName:"webgl",kernelFunc:Xlt};function Ylt(r){let{inputs:t,backend:e,attrs:n}=r,{numBuckets:o}=n,{input:s}=t;if(s.dtype!=="string")throw new Error("Input must be of datatype string");if(o<=0)throw new Error("Number of buckets must be at least 1");let i=e.readSync(s.dataId),a= $ z(i,o);return e.makeTensorInfo(s.shape,"int32",a)}var bG={kernelName:uu,backendName:"webgl",kernelFunc:Ylt};var Zlt="return tan(x);",Jlt=It({opSnippet:Zlt}),wG={kernelName:ri,backendName:"webgl",kernelFunc:Jlt};var Qlt= `
2024-01-17 22:56:53 +01:00
float e2x = exp ( - 2.0 * abs ( x ) ) ;
return sign ( x ) * ( 1.0 - e2x ) / ( 1.0 + e2x ) ;
2024-09-10 17:30:23 +02:00
` ,tut=It({opSnippet:Qlt}),IG={kernelName:ni,backendName:"webgl",kernelFunc:tut};function eut(r){let{inputs:t,backend:e,attrs:n}=r,{tensor:o,indices:s,updates:i}=t,{}=n,{sliceRank:a,numUpdates:u,sliceSize:l,strides:c,outputSize:p}=S.calculateShapes(i,s,o.shape),m=[p/l,l];if(p===0)return e.makeTensorInfo(o.shape,s.dtype);let f=rt({inputs:{x:s},backend:e,attrs:{shape:[u,a]}}),d=rt({inputs:{x:i},backend:e,attrs:{shape:[u,l]}}),h=rt({inputs:{x:o},backend:e,attrs:{shape:m}}),g=new Ku(u,a,f.shape.length,d.shape.length,c,m,!1,!0),x=e.runWebGLProgram(g,[d,f,h],h.dtype),b=rt({inputs:{x},backend:e,attrs:{shape:o.shape}});return e.disposeIntermediateTensorInfo(f),e.disposeIntermediateTensorInfo(d),e.disposeIntermediateTensorInfo(h),e.disposeIntermediateTensorInfo(x),b}var CG={kernelName:ol,backendName:"webgl",kernelFunc:eut};var vC=class{constructor(t,e){this.variableNames=["A"];let n=new Array(t.length);for(let i=0;i<n.length;i++)n[i]=t[i]*e[i];this.outputShape=n,this.rank=n.length;let o=zt(this.rank),s=rut(t);this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
$ { o } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { s } ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function rut(r){let t=r.length;if(t>5)throw Error( ` Tile for rank $ { t } is not yet supported ` );if(t===1)return ` imod ( resRC , $ { r [ 0 ] } ) ` ;let e=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],n=[];for(let o=0;o<r.length;o++)n.push( ` imod ( $ { e [ o ] } , $ { r [ o ] } ) ` );return n.join()}function j1(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{reps:s}=n;if(o.dtype==="string"||o.shape.length>5){let u=e.readSync(o.dataId),l=o.dtype==="string"?u.map(m=>y.decodeString(m)):u,c=wt(o.shape,o.dtype,l),p=Fz(c,s);return e.makeTensorInfo(p.shape,p.dtype,p.values)}let i=new vC(o.shape,s);return e.runWebGLProgram(i,[o],o.dtype)}var vG={kernelName:oo,backendName:"webgl",kernelFunc:j1};var SC=class{constructor(t){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=t,this.userCode= `
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int elemIdx = coords [ 1 ] ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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.
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
bool isFirstInPair = imod ( elemIdx , 2 * inc ) < inc ;
int i = isFirstInPair ? elemIdx : elemIdx - inc ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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 ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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-09-10 17:30:23 +02:00
` }},NC=class{constructor(t){this.variableNames=["x","indices"],this.customUniforms=[{name:"n",type:"int"},{name:"firstPass",type:"int"},{name:"k",type:"int"}],this.outputShape=t,this.userCode= `
2024-01-17 22:56:53 +01: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 ] ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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.
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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 ) ) ;
2022-03-07 19:17:31 +01:00
2024-01-17 22:56:53 +01:00
float x0 = getX ( batch , i0 ) ;
float x1 = i1 < n ? getX ( batch , i1 ) : x0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
setOutput ( x0 >= x1 ? float ( i0 ) : float ( i1 ) ) ;
}
2024-09-10 17:30:23 +02:00
` }};function xp(r,t){t!==null&&r.disposeIntermediateTensorInfo(t)}function SG(r){let t=1;for(;t<r;)t*=2;return t}function nut(r){let{inputs:t,backend:e,attrs:n}=r,{x:o}=t,{k:s,sorted:i}=n,a=L().getNumber("TOPK_LAST_DIM_CPU_HANDOFF_SIZE_THRESHOLD"),u=L().getNumber("TOPK_K_CPU_HANDOFF_THRESHOLD"),l=o.shape,c=l[l.length-1];if(e.shouldExecuteOnCPU([o])||c<a||s>u){let M=e.readSync(o.dataId),[V,G]=Oz(M,l,o.dtype,s,i);return[e.makeTensorInfo(V.shape,V.dtype,V.values),e.makeTensorInfo(G.shape,G.dtype,G.values)]}if(s===0)return l[l.length-1]=0,[e.makeTensorInfo(l,o.dtype,[]),e.makeTensorInfo(l,"int32",[])];if(c===1)return[o,Ll({attrs:{shape:l,dtype:"int32",value:0},backend:e})];let p=e.texData.get(o.dataId),m=p!==null&&p.isPacked,f=m?e.unpackTensor(o):o,h=y.sizeFromShape(l)/c,g=rt({inputs:{x:f},attrs:{shape:[h,c]},backend:e});m&&xp(e,f);let x=SG(s),b=SG(c),w=null,I=()=>w===null?[g,g]:[g,w],N=(M,V,G)=>{let W=I(),q=new SC(G),j=[[c],[w===null?1:0],[Number.NEGATIVE_INFINITY],[M],[V]],Y=w;w=e.runWebGLProgram(q,W,"int32",j),xp(e,Y)};for(let M=1;M<x;M*=2){let V=M*2;for(let G=M;G>=1;G/=2)N(V,G,[h,b])}for(let M=b;M>x;M/=2){let V=I(),G=new NC([h,M/2]),q=[[c],[w===null?1:0],[x]],H=w;w=e.runWebGLProgram(G,V,"int32",q),xp(e,H);let j=x/2,Y=j*2;for(let Z=j;Z>=1;Z/=2)N(Y,Z,w.shape)}let E=w;w=ki({inputs:{x:w},backend:e,attrs:{begin:0,size:[h,s]}}),xp(e,E);let A=V1({inputs:{x:g,indices:w},backend:e,attrs:{axis:1,batchDims:1}});xp(e,g);let D=l.slice(0,-1);D.push(s),E=w,w=rt({inputs:{x:w},attrs:{shape:D},backend:e}),xp(e,E);let F=A;return A=rt({inputs:{x:A},attrs:{shape:D},backend:e}),xp(e,F),[A,w]}var NG={kernelName:ul,backendName:"webgl",kernelFunc:nut};var kC=class{constructor(t,e,n,o,s,i){this.variableNames=["Image","Transforms"],this.outputShape=i;let a=n==="nearest"?1:2,u;switch(o){case"constant":u=1;break;case"reflect":u=2;break;case"wrap":u=3;break;case"nearest":u=4;break;default:u=1;break}this.userCode= `
2024-01-17 22:56:53 +01:00
float mapCoord ( float outCoord , float len ) {
float inCoord = outCoord ;
if ( $ { u } == 2 ) {
if ( inCoord < 0.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz2 = 2.0 * len ;
if ( inCoord < sz2 ) {
inCoord = sz2 * float ( int ( float ( - inCoord / sz2 ) ) ) +
inCoord ;
}
inCoord = inCoord < - len ? inCoord + sz2 : - inCoord - 1.0 ;
}
} else if ( inCoord > len - 1.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz2 = 2.0 * len ;
inCoord -= sz2 * float ( int ( float ( inCoord / sz2 ) ) ) ;
if ( inCoord >= len ) {
inCoord = sz2 - inCoord - 1.0 ;
}
}
}
return clamp ( inCoord , 0.0 , len - 1.0 ) ;
} else if ( $ { u } == 3 ) {
if ( inCoord < 0.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz = len - 1.0 ;
inCoord += len * ( float ( int ( float ( - inCoord / sz ) ) ) + 1.0 ) ;
}
} else if ( inCoord > len - 1.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz = len - 1.0 ;
inCoord -= len * float ( int ( float ( inCoord / sz ) ) ) ;
}
}
return clamp ( inCoord , 0.0 , len - 1.0 ) ;
} else if ( $ { u } == 4 ) {
return clamp ( outCoord , 0.0 , len - 1.0 ) ;
} else {
return outCoord ;
}
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float readWithFillValue ( int batch , int coordY , int coordX ,
int channel ) {
float outputValue ;
if ( 0 <= coordY && coordY < $ { t } && 0 <= coordX && coordX < $ { e } ) {
outputValue = getImage ( batch , coordY , coordX , channel ) ;
} else {
outputValue = float ( $ { s } ) ;
}
return outputValue ;
}
2021-12-09 20:22:22 +01:00
2024-01-17 22:56:53 +01: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 ) {
outputValue = float ( $ { s } ) ;
} else {
float inX = ( a1 * xf + a2 * yf + a3 ) / projection ;
float inY = ( b1 * xf + b2 * yf + b3 ) / projection ;
float mapX = mapCoord ( inX , float ( $ { e } ) ) ;
float mapY = mapCoord ( inY , float ( $ { t } ) ) ;
2021-12-09 20:22:22 +01:00
2024-01-17 22:56:53 +01:00
if ( $ { a } == 1 ) {
int coordY = int ( round ( mapY ) ) ;
int coordX = int ( round ( mapX ) ) ;
outputValue = readWithFillValue ( batch , coordY , coordX ,
channel ) ;
} else {
float yFloor = floor ( mapY ) ;
float xFloor = floor ( mapX ) ;
float yCeil = yFloor + 1.0 ;
float xCeil = xFloor + 1.0 ;
float valueYFloor = ( xCeil - mapX ) *
readWithFillValue ( batch , int ( yFloor ) , int ( xFloor ) , channel ) +
( mapX - xFloor ) *
readWithFillValue ( batch , int ( yFloor ) , int ( xCeil ) , channel ) ;
float valueYCeil = ( xCeil - mapX ) *
readWithFillValue ( batch , int ( yCeil ) , int ( xFloor ) , channel ) +
( mapX - xFloor ) *
readWithFillValue ( batch , int ( yCeil ) , int ( xCeil ) , channel ) ;
outputValue = ( yCeil - mapY ) * valueYFloor +
( mapY - yFloor ) * valueYCeil ;
}
}
setOutput ( outputValue ) ;
}
2024-09-10 17:30:23 +02:00
` }};function out(r){let{inputs:t,backend:e,attrs:n}=r,{image:o,transforms:s}=t,{interpolation:i,fillMode:a,fillValue:u,outputShape:l}=n,[c,p,m,f]=o.shape,[d,h]=l!=null?l:[p,m],g=[c,d,h,f],x=new kC(p,m,i,a,u,g);return e.runWebGLProgram(x,[o,s],"float32")}var kG={kernelName:cl,backendName:"webgl",kernelFunc:out};function sut(r){let{inputs:t,attrs:e,backend:n}=r,{axis:o}=e,{x:s}=t;vi(s,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let i=n.readSync(s.dataId),{outputValues:a,outputShape:u,indices:l}=Mz(i,o,s.shape,s.dtype);return[n.makeTensorInfo(u,s.dtype,a),n.makeTensorInfo([l.length],"int32",l)]}var TG={kernelName:cu,backendName:"webgl",kernelFunc:sut};function iut(r){let{inputs:t,backend:e,attrs:n}=r,{value:o}=t,{axis:s}=n;s<0&&(s+=o.shape.length);let i=o,a=i.shape.length,u=o.shape[s],l=new Array(a-1),c=0;for(let h=0;h<a;h++)h!==s&&(l[c++]=i.shape[h]);let p=[],m=new Array(a).fill(0),f=i.shape.slice();f[s]=1;let d=new Array(u);for(let h=0;h<d.length;h++){m[s]=h;let g=ki({inputs:{x:i},backend:e,attrs:{begin:m,size:f}}),x=rt({inputs:{x:g},backend:e,attrs:{shape:l}});d[h]=x,p.push(g)}return p.forEach(h=>e.disposeIntermediateTensorInfo(h)),d}var _G={kernelName:Ki,backendName:"webgl",kernelFunc:iut};var TC=class{constructor(t,e){this.variableNames=["x","segmentIds"];let n=t.windowSize,o=t.batchSize,s=t.inSize,i=t.numSegments,a=i*Math.ceil(s/n);this.outputShape=[o,a];let u="0.0",l="sumValue",c=Math.floor(n/4)*4,p=n%4,m= `
2024-01-17 22:56:53 +01:00
sumValue += dot ( values , segFilter ) ;
` ,f="";s%n>0&&(f= `
if ( inIdx < 0 || inIdx >= $ { s } ) {
return initializationValue ;
}
` );let d="";s%n>0&&(d= `
if ( inIdx < 0 || inIdx >= $ { s } ) {
return - 1.0 ;
}
` ),this.userCode= `
const float initializationValue = $ { u } ;
2021-12-09 20:22:22 +01:00
2024-01-17 22:56:53 +01:00
float getValue ( int batch , int inIdx ) {
$ { f }
return getX ( batch , inIdx ) ;
}
2021-12-09 20:22:22 +01:00
2024-01-17 22:56:53 +01:00
float getSegmentIdAtIndex ( int inIdx ) {
$ { d }
return getSegmentIds ( inIdx ) ;
}
2021-12-09 20:22:22 +01:00
2024-01-17 22:56:53 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
int inOffset = int ( floor ( float ( outIdx ) / float (
$ { i } ) ) * float ( $ { n } ) ) ;
int currentSeg = int ( mod ( float ( outIdx ) , float ( $ { i } ) ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
float sumValue = 0.0 ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
for ( int i = 0 ; i < $ { c } ; i += 4 ) {
int inIdx = inOffset + i ;
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
getValue ( batch , inIdx + 3 )
) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01: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-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { m }
}
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
int inIdx = inOffset + $ { c } ;
if ( $ { p === 1 } ) {
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
int inIdxSeg = int ( getSegmentIdAtIndex ( inIdx ) ) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec4 segFilter = vec4 (
int ( getSegmentIdAtIndex ( inIdx ) ) == currentSeg ? 1 : 0 ,
0 ,
0 ,
0
) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { m }
} else if ( $ { p === 2 } ) {
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
initializationValue ,
initializationValue
) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
vec4 segFilter = vec4 (
int ( getSegmentIdAtIndex ( inIdx ) ) == currentSeg ? 1 : 0 ,
int ( getSegmentIdAtIndex ( inIdx + 1 ) ) == currentSeg ? 1 : 0 ,
0 ,
0
) ;
2021-12-06 12:34:50 +01:00
2024-01-17 22:56:53 +01:00
$ { m }
} else if ( $ { p === 3 } ) {
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
initializationValue
) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01: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
) ;
2024-01-17 22:44:24 +01:00
2024-01-17 22:56:53 +01:00
$ { m }
}
setOutput ( $ { l } ) ;
2024-01-17 22:44:24 +01:00
}
2024-09-10 17:30:23 +02:00
` }};function aut(r){let{inputs:t,backend:e,attrs:n}=r,{x:o,segmentIds:s}=t,{numSegments:i}=n,a=o.shape.length,u=[],l=0,c=S.getAxesPermutation([l],a),p=o;c!=null&&(p=Pe({inputs:{x:o},backend:e,attrs:{perm:c}}),u.push(p),l=S.getInnerMostAxes(1,a)[0]);let m=S.segment_util.computeOutShape(p.shape,l,i),f=y.sizeFromShape([p.shape[l]]),d=rt({inputs:{x:p},backend:e,attrs:{shape:[-1,f]}});u.push(d);let h=lc(o.dtype),g=(I,N,E,A,D)=>{let F=I.shape[0],M=I.shape[1],V=S.segment_util.segOpComputeOptimalWindowSize(M,D),G={windowSize:V,inSize:M,batchSize:F,numSegments:D},W=new TC(G,N),q=e.compileAndRun(W,[I,E],A);if(u.push(q),q.shape[1]===D)return q;let H=K1({backend:e,attrs:{start:0,stop:D,step:1,dtype:"float32"}}),j=j1({inputs:{x:H},backend:e,attrs:{reps:[M/V]}});return u.push(H),u.push(j),g(q,N,j,A,D)},x=g(d,"unsortedSegmentSum",s,h,i),b=rt({inputs:{x},backend:e,attrs:{shape:m}}),w=b;if(c!=null){u.push(b);let I=S.getUndoAxesPermutation(c);w=Pe({inputs:{x:w},backend:e,attrs:{perm:I}})}return u.forEach(I=>e.disposeIntermediateTensorInfo(I)),w}var EG={kernelName:pu,backendName:"webgl",kernelFunc:aut};var lut=[p3,f3,d3,h3,x3,y3,b3,w3,v3,S3,N3,k3,T3,_3,E3,A3,D3, $ 3,R3,F3,O3,P3,L3,z3,B3,U3,q3,K3,e3,X3,Z3,J3,Q3,tB,eB,rB,nB,oB,sB,iB,uB,cB,pB,mB,fB,dB,hB,gB,xB,yB,bB,wB,IB,CB,vB,SB,kB,TB,_B,EB,DB, $ B,RB,FB,OB,MB,PB,LB,zB,t3,BB,Y3,VB,GB,WB,r3,UB,HB,qB,KB,jB,XB,YB,ZB,JB,QB,eV,rV,nV,oV,sV,iV,lV,cV,pV,mV,fV,dV,bV,s3,wV,IV,CV,vV,V3,SV,TV,_V,EV,AV,n3,DV, $ V,RV,FV,OV,G3,hV,MV,PV,LV,a3,zV,BV,VV,GV,WV,UV,HV,qV,KV,jV,XV,YV,ZV,JV,QV,tG,M3,yV,eG,rG,nG,oG,sG,iG,aG,lG,cG,pG,fG,dG,hG,gG,xG,yG,bG,xV,u3,wG,IG,CG,vG,NG,kG,c3,TG,_G,EG,NV];for(let r of lut)rc(r);var Nt;(function(r){r[r.float32=0]="float32",r[r.int32=1]="int32",r[r.bool=2]="bool",r[r.string=3]="string",r[r.complex64=4]="complex64"})(Nt||(Nt={}));var ju;(function(r){r[r.linear=0]="linear",r[r.relu=1]="relu",r[r.relu6=2]="relu6",r[r.prelu=3]="prelu",r[r.leakyrelu=4]="leakyrelu",r[r.sigmoid=5]="sigmoid",r[r.elu=6]="elu"})(ju||(ju={}));var AG;function uut(r){AG=r.wasm.cwrap(Xi,null,["number","array","number","number","array","number","number","number","number","number","number","number","number"])}function cut(r){let{inputs:t,backend:e,attrs:n}=r,{a:o,b:s,bias:i,preluActivationWeights:a}=t;if(o.dtype!=="float32"||s.dtype!=="float32")throw new Error("_FusedMatMul for non non-float32 tensors not yet supported.");let{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:p}=n,m=e.dataIdMap.get(o.dataId).id,f=e.dataIdMap.get(s.dataId).id,d=0;if(i!=null){let D=e.dataIdMap.get(i.dataId);if(D.shape.length!==1)throw new Error( ` _FusedMatMul only supports rank - 1 bias but got rank $ { D . shape . length } . ` );d=D.id}let h=a==null?0:e.dataIdMap.get(a.dataId).id,g=ju[c];if(g==null)throw new Error( ` $ { c } activation not yet supported for FusedConv2D in the wasm backend . ` );let x=u?o.shape[2]:o.shape[1],b=l?s.shape[1]:s.shape[2],w=Hr.assertAndGetBroadcastShape(o.shape.slice(0,-2),s.shape.slice(0,-2)),I=e.makeOutput([...w,x,b],o.dtype),N=e.dataIdMap.get(I.dataId).id,E=new Uint8Array(new Int32Array(o.shape).buffer),A=new Uint8Array(new Int32Array(s.shape).buffer);return AG(m,E,o.shape.length,f,A,s.shape.length,u,l,g,d,h,p||0,N),I}var DG={kernelName:Xi,backendName:"wasm",setupFunc:uut,kernelFunc:cut};function yt(r,t){let e;function n(s){e=s.wasm.cwrap(r,null,["number","number","number"])}function o(s){let{backend:i,inputs:{x:a}}=s,u=i.dataIdMap.get(a.dataId).id,l=i.makeOutput(a.shape,t||a.dtype),c=i.dataIdMap.get(l.dataId).id;return y.sizeFromShape(l.shape)===0||e(u,Nt[a.dtype],c),l}return{kernelName:r,backendName:"wasm",setupFunc:n,kernelFunc:o}}var $ G=yt(Ai);var RG=yt(Go);var FG=yt(Wo);function ee(r,t,e){let n;function o(i){n=i.wasm.cwrap(r,null,["number","array","number","number","array","number","number","number"])}function s(i){let{backend:a,inputs:u}=i,{a:l,b:c}=u,p=a.dataIdMap.get(l.dataId).id,m=a.dataIdMap.get(c.dataId).id,f=e!=null?e:l.dtype,d=S.assertAndGetBroadcastShape(l.shape,c.shape),h=a.makeOutput(d,f);if(y.sizeFromShape(d)===0)return h;let g=new Uint8Array(new Int32Array(l.shape).buffer),x=new Uint8Array(new Int32
2024-01-17 22:56:53 +01:00
$ { n . shape } ` );if(o.shape.length!==1)throw new Error( ` Input shape should be a vector but received shape
2024-09-10 17:30:23 +02:00
$ { o . 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(n.dataId).id,a=t.dataIdMap.get(o.dataId).id,u=t.dataIdMap.get(s.dataId).id,l=n.shape[0],c=y.sizeFromShape(s.shape),p=t.makeOutput([l,c],n.dtype),m=t.dataIdMap.get(p.dataId).id,f=t.makeOutput([c],s.dtype),d=t.dataIdMap.get(f.dataId).id,h=t.makeOutput([3],"int32"),g=t.dataIdMap.get(h.dataId).id;fH(i,a,u,l,m,d,g);let x=t.readSync(h.dataId),b;switch(x[0]){case 0:{b=S.getSparseReshapeMultipleNegativeOneOutputDimErrorMessage(x[1],x[2]);break}case 1:{b=S.getSparseReshapeNegativeOutputDimErrorMessage(x[1],x[2]);break}case 2:b=S.getSparseReshapeEmptyTensorZeroOutputDimErrorMessage();break;case 3:{let w=Array.from(t.readSync(o.dataId)),I=Array.from(t.readSync(f.dataId));b=S.getSparseReshapeInputOutputMultipleErrorMessage(w,I);break}case 4:{let w=Array.from(t.readSync(o.dataId)),I=Array.from(t.readSync(f.dataId));b=S.getSparseReshapeInputOutputMismatchErrorMessage(w,I);break}default:b=""}if(t.disposeData(h.dataId),b)throw t.disposeData(p.dataId),t.disposeData(f.dataId),new Error(b);return[p,f]}var dH={kernelName:il,backendName:"wasm",setupFunc:amt,kernelFunc:lmt};var hH;function DC(r){hH=r.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function $ C(r,t){let{backend:e,inputs:n}=r,{data:o,indices:s,segmentIds:i}=n,a=s.shape[0],u=e.readSync(i.dataId,a-1,a)[0],c=a>0?u+1:0;if(c<0)throw new Error(S.getSparseSegmentReductionNegativeSegmentIdsErrorMessage());let p=o.shape.slice();p[0]=c;let m=e.dataIdMap.get(o.dataId).id,f=e.dataIdMap.get(s.dataId).id,d=e.dataIdMap.get(i.dataId).id,h=e.makeOutput(p,o.dtype),g=e.dataIdMap.get(h.dataId).id,x=e.makeOutput([4],"int32"),b=e.dataIdMap.get(x.dataId).id;hH(m,Nt[o.dtype],o.shape[0],f,d,g,b,t,0);let w=e.readSync(x.dataId),I;switch(w[0]){case 0:{I=S.getSparseSegmentReductionNegativeSegmentIdsErrorMessage();break}case 1:{I=S.getSparseSegmentReductionNonIncreasingSegmentIdsErrorMessage();break}case 2:I=S.getSparseSegmentReductionSegmentIdOutOfRangeErrorMessage(w[1],w[2]);break;case 3:I=S.getSparseSegmentReductionIndicesOutOfRangeErrorMessage(w[1],w[2],w[3]);break;default:I=""}if(e.disposeData(x.dataId),I)throw e.disposeData(h.dataId),new Error(I);return h}function umt(r){return $ C(r,!0)}var gH={kernelName:ou,backendName:"wasm",setupFunc:DC,kernelFunc:umt};function cmt(r){return $ C(r,!1)}var xH={kernelName:su,backendName:"wasm",setupFunc:DC,kernelFunc:cmt};var yH;function pmt(r){yH=r.wasm.cwrap(al,null,["number","number","number","number","number","number","number","number","array","number","number"])}function mmt(r){let{backend:t,inputs:e,attrs:n}=r,{sparseIndices:o,sparseValues:s,defaultValue:i}=e,{outputShape:a}=n,u=t.makeOutput(a,i.dtype);if(y.sizeFromShape(a)===0)return u;let{sliceRank:l,numUpdates:c,sliceSize:p,strides:m,outputSize:f}=S.calculateShapes(s,o,a),d=t.dataIdMap.get(o.dataId).id,h=t.dataIdMap.get(s.dataId).id,g=t.dataIdMap.get(i.dataId).id,x=new Uint8Array(new Int32Array(m).buffer),b=t.dataIdMap.get(u.dataId).id;return yH(d,h,s.shape.length,g,Nt[i.dtype],l,c,p,x,f,b),u}var bH={kernelName:al,backendName:"wasm",setupFunc:pmt,kernelFunc:mmt};function fmt(r){let{inputs:t,attrs:e,backend:n}=r,{x:o}=t,{numOrSizeSplits:s,axis:i}=e,a=y.parseAxisParam(i,o.shape)[0],u=S.prepareSplitSize(o,s,a),l=new Array(o.shape.length).fill(0),c=o.shape.slice();return u.map(p=>{let m=[...c];m[a]=p;let f=Lo({inputs:{x:o},attrs:{begin:l,size:m},backend:n});return l[a]+=p,f})}var wH={kernelName:qi,backendName:"wasm",kernelFunc:fmt};var IH=yt(Zs);var CH=yt(iu);var dmt=!0,vH=ee(ti,dmt);var SH;function hmt(r){SH=r.wasm.cwrap(xo,null,["number","number","number","number"])}function gmt(r){let{backend:t,inputs:e,attrs:n}=r,{alpha:o}=n,{x:s}=e,i=t.dataIdMap.get(s.dataId).id,a=t.makeOutput(s.shape,s.dtype),u=t.dataIdMap.get(a.dataId).id;return SH(i,o,Nt[s.dtype],u),a}var NH={kernelName:xo,backendName:"wasm",setupFunc:hmt,kernelFunc:gmt};var kH;function xmt(r){kH=r.wasm.cwrap(ll,null,["number","array","