2020-12-02 22:46:41 +01:00
/ *
Face - API
homepage : < https : //github.com/vladmandic/face-api>
author : < https : //github.com/vladmandic>'
* /
2021-03-09 23:32:33 +01:00
var FA = Object . create , nd = Object . defineProperty , _A = Object . getPrototypeOf , EA = Object . prototype . hasOwnProperty , $A = Object . getOwnPropertyNames , DA = Object . getOwnPropertyDescriptor ; var Uw = e => nd ( e , "__esModule" , { value : ! 0 } ) ; var MA = ( e , t ) => ( ) => ( t || ( t = { exports : { } } , e ( t . exports , t ) ) , t . exports ) , ad = ( e , t ) => { for ( var n in t ) nd ( e , n , { get : t [ n ] , enumerable : ! 0 } ) } , RA = ( e , t , n ) => { if ( t && typeof t == "object" || typeof t == "function" ) for ( let a of $A ( t ) ) ! EA . call ( e , a ) && a !== "default" && nd ( e , a , { get : ( ) => t [ a ] , enumerable : ! ( n = DA ( t , a ) ) || n . enumerable } ) ; return e } , PA = e => RA ( Uw ( nd ( e != null ? FA ( _A ( e ) ) : { } , "default" , e && e . _ _esModule && "default" in e ? { get : ( ) => e . default , enumerable : ! 0 } : { value : e , enumerable : ! 0 } ) ) , e ) ; var XS = MA ( ( Ire , qS ) => { Uw ( Ire ) ; ad ( Ire , { isNodejs : ( ) => Nre } ) ; function Nre ( ) { return typeof global == "object" && ! 0 && typeof qS != "undefined" && typeof process != "undefined" && ! ! process . version } } ) ; var Gw = { } ; ad ( Gw , { Abs : ( ) => So , Acos : ( ) => Co , Acosh : ( ) => Ao , AdadeltaOptimizer : ( ) => _h , AdagradOptimizer : ( ) => Eh , AdamOptimizer : ( ) => $h , AdamaxOptimizer : ( ) => Dh , Add : ( ) => Br , AddN : ( ) => Ns , All : ( ) => ud , Any : ( ) => cd , ArgMax : ( ) => Ts , ArgMin : ( ) => Ku , Asin : ( ) => Fo , Asinh : ( ) => _o , Atan : ( ) => Eo , Atan2 : ( ) => Do , Atanh : ( ) => $o , AvgPool : ( ) => Ss , AvgPool3D : ( ) => Yu , AvgPool3DGrad : ( ) => dd , AvgPoolGrad : ( ) => pd , BackendWasm : ( ) => VS , BatchMatMul : ( ) => Cs , BatchToSpaceND : ( ) => Ju , Bincount : ( ) => hd , BroadcastTo : ( ) => s0 , Callback : ( ) => WI , CallbackList : ( ) => Lk , Cast : ( ) => As , Ceil : ( ) => Fs , ClipByValue : ( ) => Vr , Complex : ( ) => md , ComplexAbs : ( ) => Qu , Concat : ( ) => Mo , Conv2D : ( ) => _s , Conv2DBackpropFilter : ( ) => fd , Conv2DBackpropInput : ( ) => Es , Conv3D : ( ) => Zu , Conv3DBackpropFilterV2 : ( ) => gd , Conv3DBackpropInputV2 : ( ) => yd , Cos : ( ) => $s , Cosh : ( ) => Ro , CropAndResize : ( ) => Po , Cumsum : ( ) => Ds , CustomCallback : ( ) => Wk , DataStorage : ( ) => sd , DenseBincount : ( ) => bd , DepthToSpace : ( ) => Oo , DepthwiseConv2dNative : ( ) => Ms , DepthwiseConv2dNativeBackpropFilter : ( ) => xd , DepthwiseConv2dNativeBackpropInput : ( ) => vd , Diag : ( ) => wd , Dilation2D : ( ) => ec , Dilation2DBackpropFilter : ( ) => Id , Dilation2DBackpropInput : ( ) => kd , ENV : ( ) => Ug , EarlyStopping : ( ) => VI , Elu : ( ) => Lo , EluGrad : ( ) => Nd , Environment : ( ) => a0 , Equal : ( ) => Wo , Erf : ( ) => zo , Exp : ( ) => Ps , ExpandDims : ( ) => Bo , Expm1 : ( ) => Vo , FFT : ( ) => Td , Fill : ( ) => tc , FlipLeftRight : ( ) => Uo , Floor : ( ) => Os , FloorDiv : ( ) => Ls , FromPixels : ( ) => Wd , FusedBatchNorm : ( ) => zs , FusedConv2D : ( ) => yi , FusedDepthwiseConv2D : ( ) => bi , GatherNd : ( ) => Ho , GatherV2 : ( ) => Go , GraphModel : ( ) => bN , Greater : ( ) => jo , GreaterEqual : ( ) => Ws , History : ( ) => zk , IFFT : ( ) => Sd , Identity : ( ) => Bs , Imag : ( ) => Cd , InputSpec : ( ) => Yt , IsFinite : ( ) => qo , IsInf : ( ) => Xo , IsNan : ( ) => Ko , KernelBackend : ( ) => ju , LRN : ( ) => rc , LRNGrad : ( ) => Fd , LayerVariable : ( ) => Dk , LayersModel : ( ) => wr , LeakyRelu : ( ) => Vs , Less : ( ) => Yo , LessEqual : ( ) => Jo , LinSpace : ( ) => Ad , Log : ( ) => Us , Log1p : ( ) => Qo , LogSoftmax : ( ) => i0 , LogicalAnd : ( ) => Zo , LogicalNot : ( ) => nc , LogicalOr : ( ) => ac , Max : ( ) => Gs , MaxPool : ( ) => js , MaxPool3D : ( ) => sc , MaxPool3DGrad : ( ) => Ed , MaxPoolGrad : ( ) => _d , MaxPoolWithArgmax : ( ) => $d , Maximum : ( ) => Hs , Mean : ( ) => qs , Min : ( ) => Xs , Minimum : ( ) => Ks , MirrorPad : ( ) => ic , Mod : ( ) => el , MomentumOptimizer : ( ) => Mh , Multinomial : ( ) => Dd , Multiply : ( ) => Ys , Neg : ( ) => tl , NonMaxSuppressionV3 : ( ) => al , NonMaxSuppressionV4 : ( ) => rl , NonMaxSuppressionV5 : ( ) => sl , NotEqual : ( ) => nl , OP _SCOPE _SUFFIX : ( ) => y0 , OneHot : ( ) => Js , OnesLike : ( ) => il , Optimizer : ( ) => br , Pack : ( ) => ol , PadV2 : ( ) => Qs , Pool : ( ) => BF , Pow : ( ) => Zs , Prelu : ( ) => ei , Prod : ( ) => ll , RMSPropOptimizer : ( ) => Rh , RNN : ( ) => er , Range : ( ) => oc , Rank : ( ) => Yg , Real : ( ) => Md , RealDiv : ( ) => Rs , Reciprocal : ( ) => ul , Reduction : ( ) => hn , Relu : ( ) => ti , Relu6 : ( ) => ai , Reshape : ( ) => cl , ResizeBilinear : ( ) => ni , ResizeBilinearGrad : ( ) => Pd , ResizeNearestNeighbor : ( ) => lc , ResizeNearestNeighborGrad : ( ) => Rd , Reverse : ( ) => ri , RotateWithOffset : ( ) => Nl , Round : ( ) => si , Rsqrt : ( ) => ii , SGDOptimizer : ( ) => Oc , ScatterNd : ( ) => pl , Select : ( ) => dl , Selu : ( ) => hl , Sequential : ( ) => Kl , Sigmoid : ( ) => li , Sign : ( ) => gl , Sin : ( ) => oi , Sinh : ( ) => fl , Slice : ( ) => ml , Softmax : ( ) => pi , Softplus : ( ) => yl , SpaceToBatchND : ( ) => uc , SparseToDense : ( ) => Od , SplitV : ( ) => bl , Sqrt : ( ) => ui , Square : ( ) => cc , SquaredDifference : ( ) => di , Step : ( ) => Gr , StridedSlice : ( ) => xl , Sub : ( ) => hi , Sum : ( ) => ci , SymbolicTensor : ( ) => Ea , Tan : ( ) => vl , Tanh : ( ) => mi , Tensor : ( ) => Fe , TensorBuffer : ( ) => Lt , Tile : ( ) => Ur , TopK : ( ) => wl , Transform : ( ) => Ld , Transpose : ( ) => fi , Unique : ( ) => zd , Unpack : ( ) => kl , UnsortedSegmentSum : ( ) => pc , Variable : ( ) => Hr , ZerosLike : ( ) => Il , _FusedMatMul : ( ) => gi , abs : ( ) => zt , acos : ( ) => wy , acosh : (
2021-01-12 16:01:34 +01:00
` )),l.join( `
2021-03-09 23:32:33 +01:00
` )}function ZF(e,t,n,a){let r=Ot(t),s=a[a.length-1],i=new Array(s).fill(0),o=t.length,l=n==="complex64"?bc(e):e;if(o>1)for(let u=0;u<r/s;u++){let c=u*s;for(let p=0;p<s;p++)i[p]=Math.max(i[p],yc(l[c+p],0,n).length)}return i}function yc(e,t,n){let a;return Array.isArray(e)?a= ` $ { parseFloat ( e [ 0 ] . toFixed ( Xg ) ) } + $ { parseFloat ( e [ 1 ] . toFixed ( Xg ) ) } j ` :zr(e)?a= ` '${e}' ` :n==="bool"?a=u0(e):a=parseFloat(e.toFixed(Xg)).toString(),Xu(a,t)}function u0(e){return e===0?"false":"true"}function Hd(e,t,n,a,r,s=!0){let i=n==="complex64"?2:1,o=t[0],l=t.length;if(l===0){if(n==="complex64"){let f=bc(e);return[yc(f[0],0,n)]}return n==="bool"?[u0(e[0])]:[e[0].toString()]}if(l===1){if(o>l0){let g=gc*i,y=Array.from(e.slice(0,g)),b=Array.from(e.slice((o-gc)*i,o*i));return n==="complex64"&&(y=bc(y),b=bc(b)),["["+y.map((x,v)=>yc(x,r[v],n)).join(", ")+", ..., "+b.map((x,v)=>yc(x,r[o-gc+v],n)).join(", ")+"]"]}let f=n==="complex64"?bc(e):Array.from(e);return["["+f.map((g,y)=>yc(g,r[y],n)).join(", ")+"]"]}let u=t.slice(1),c=a.slice(1),p=a[0]*i,d=[];if(o>l0){for(let f=0;f<gc;f++){let g=f*p,y=g+p;d.push(...Hd(e.slice(g,y),u,n,c,r,!1))}d.push("...");for(let f=o-gc;f<o;f++){let g=f*p,y=g+p;d.push(...Hd(e.slice(g,y),u,n,c,r,f===o-1))}}else for(let f=0;f<o;f++){let g=f*p,y=g+p;d.push(...Hd(e.slice(g,y),u,n,c,r,f===o-1))}let h=l===2?",":"";d[0]="["+d[0]+h;for(let f=1;f<d.length-1;f++)d[f]=" "+d[f]+h;let m= ` ,
2021-01-12 16:01:34 +01:00
` ;for(let f=2;f<l;f++)m+= `
2021-03-09 23:32:33 +01:00
` ;return d[d.length-1]=" "+d[d.length-1]+"]"+(s?"":m),d}function bc(e){let t=[];for(let n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}var Lt=class{constructor(e,t,n){if(this.dtype=t,this.shape=e.slice(),this.size=Ot(e),n!=null){let a=n.length; $ (a===this.size,()=> ` Length of values '${a}' does not match the size inferred by the shape '${this.size}' . ` )}if(t==="complex64")throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag).");this.values=n||Kw(t,this.size),this.strides=No(e)}set(e,...t){t.length===0&&(t=[0]), $ (t.length===this.rank,()=> ` The number of provided coordinates ( $ { t . length } ) must match the rank ( $ { this . rank } ) ` );let n=this.locToIndex(t);this.values[n]=e}get(...e){e.length===0&&(e=[0]);let t=0;for(let a of e){if(a<0||a>=this.shape[t]){let r= ` Requested out of range element at $ { e } . Buffer shape = $ { this . shape } ` ;throw new Error(r)}t++}let n=e[e.length-1];for(let a=0;a<e.length-1;++a)n+=this.strides[a]*e[a];return this.values[n]}locToIndex(e){if(this.rank===0)return 0;if(this.rank===1)return e[0];let t=e[e.length-1];for(let n=0;n<e.length-1;++n)t+=this.strides[n]*e[n];return t}indexToLoc(e){if(this.rank===0)return[];if(this.rank===1)return[e];let t=new Array(this.shape.length);for(let n=0;n<t.length-1;++n)t[n]=Math.floor(e/this.strides[n]),e-=t[n]*this.strides[n];return t[t.length-1]=e,t}get rank(){return this.shape.length}toTensor(){return Ba().makeTensor(this.values,this.shape,this.dtype)}},Ba=null,Sl=null,t_=null;function n_(e){Ba=e}function a_(e){Sl=e}function r_(e){t_=e}var Fe=class{constructor(e,t,n,a){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=Ot(e),this.strides=No(e),this.dataId=n,this.id=a,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){let e=await this.data();return Sl.buffer(this.shape,this.dtype,e)}bufferSync(){return Sl.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return To(this.shape,e)}arraySync(){return To(this.shape,this.dataSync())}async data(){this.throwIfDisposed();let e=Ba().read(this.dataId);if(this.dtype==="string"){let t=await e;try{return t.map(n=>Gd(n))}catch(n){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}}return e}dataSync(){this.throwIfDisposed();let e=Ba().readSync(this.dataId);if(this.dtype==="string")try{return e.map(t=>Gd(t))}catch(t){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}return e}async bytes(){this.throwIfDisposed();let e=await Ba().read(this.dataId);return this.dtype==="string"?e:new Uint8Array(e.buffer)}dispose(){this.isDisposed||(Ba().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(e=!1){return Sl.print(this,e)}clone(){return this.throwIfDisposed(),Sl.clone(this)}toString(e=!1){let t=this.dataSync();return e_(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Sl.cast(this,e)}variable(e=!0,t,n){return this.throwIfDisposed(),Ba().makeVariable(this,e,t,n)}};Object.defineProperty(Fe,Symbol.hasInstance,{value:e=>!!e&&e.data!=null&&e.dataSync!=null&&e.throwIfDisposed!=null});function Y(){return Hg("Tensor",()=>Fe)}Y();var Hr=class extends Fe{constructor(e,t,n,a){super(e.shape,e.dtype,e.dataId,a);this.trainable=t,this.name=n}assign(e){if(e.dtype!==this.dtype)throw new Error( ` dtype of the new value ( $ { e . dtype } ) and previous value ( $ { this . dtype } ) must match ` );if(!hr(e.shape,this.shape))throw new Error( ` shape of the new value ( $ { e . shape } ) and previous value ( $ { this . shape } ) must match ` );Ba().disposeTensor(this),this.dataId=e.dataId,Ba().incRef(this,null)}dispose(){Ba().disposeVariable(this),this.isDisposedInternal=!0}};Object.defineProperty(Hr,Symbol.hasInstance,{value:e=>e instanceof Fe&&e.assign!=null&&e.assign instanceof Function});var Ia={}
Manifest JSON has weights with names : $ { o . join ( ", " ) } . ` )}let l=r.reduce((h,m,f)=>(m&&h.push(f),h),[]),u=[];l.forEach(h=>{t[h].paths.forEach(m=>{let f=n+(n.endsWith("/")?"":"/")+m;u.push(f)})});let c=await e(u),p={},d=0;return l.forEach(h=>{let m=t[h].paths.length,f=0;for(let x=0;x<m;x++)f+=c[d+x].byteLength;let g=new ArrayBuffer(f),y=new Uint8Array(g),b=0;for(let x=0;x<m;x++){let v=new Uint8Array(c[d+x]);y.set(v,b),b+=v.byteLength}s[h].forEach(x=>{let v=g.slice(x.groupOffset,x.groupOffset+x.sizeBytes),k=b0(v,[x.manifestEntry]);for(let w in k)p[w]=k[w]}),d+=m}),p}}var aE="application/octet-stream",rE="application/json",py=class{constructor(e,t){if(this.DEFAULT_METHOD="POST",t==null&&(t={}),this.weightPathPrefix=t.weightPathPrefix,this.onProgress=t.onProgress,this.weightUrlConverter=t.weightUrlConverter,t.fetchFunc!=null?( $ (typeof t.fetchFunc=="function",()=>"Must pass a function that matches the signature of ` fetch ` (see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)"),this.fetch=t.fetchFunc):this.fetch=Z().platform.fetch, $ (e!=null&&e.length>0,()=>"URL path for http must not be null, undefined or empty."),Array.isArray(e)&& $ (e.length===2,()=> ` URL paths for http must have a length of 2 , ( actual length is $ { e . length } ) . ` ),this.path=e,t.requestInit!=null&&t.requestInit.body!=null)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=t.requestInit||{}}async save(e){if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");let t=Object.assign({method:this.DEFAULT_METHOD},this.requestInit);t.body=new FormData;let n=[{paths:["./model.weights.bin"],weights:e.weightSpecs}],a={modelTopology:e.modelTopology,format:e.format,generatedBy:e.generatedBy,convertedBy:e.convertedBy,weightsManifest:n};e.signature!=null&&(a.signature=e.signature),e.userDefinedMetadata!=null&&(a.userDefinedMetadata=e.userDefinedMetadata),e.modelInitializer!=null&&(a.modelInitializer=e.modelInitializer),t.body.append("model.json",new Blob([JSON.stringify(a)],{type:rE}),"model.json"),e.weightData!=null&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:aE}),"model.weights.bin");let r=await this.fetch(this.path,t);if(r.ok)return{modelArtifactsInfo:kc(e),responses:[r]};throw new Error( ` BrowserHTTPRequest . save ( ) failed due to HTTP response status $ { r . status } . ` )}async load(){let e=await this.fetch(this.path,this.requestInit);if(!e.ok)throw new Error( ` Request to $ { this . path } failed with status code $ { e . status } . Please verify this URL points to the model JSON of the model to load . ` );let t;try{t=await e.json()}catch(h){let m= ` Failed to parse model JSON of response from $ { this . path } . ` ;throw this.path.endsWith(".pb")?m+=" 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.":m+=" Please make sure the server is serving valid JSON for this request.",new Error(m)}let n=t.modelTopology,a=t.weightsManifest,r=t.generatedBy,s=t.convertedBy,i=t.format,o=t.signature,l=t.userDefinedMetadata;if(n==null&&a==null)throw new Error( ` The JSON from HTTP path $ { this . path } contains neither model topology or manifest for weights . ` );let u,c;a!=null&&([u,c]=await this.loadWeights(a));let p={modelTopology:n,weightSpecs:u,weightData:c,generatedBy:r,convertedBy:s,format:i};o!=null&&(p.signature=o),l!=null&&(p.userDefinedMetadata=l);let d=t.modelInitializer;return d&&(p.modelInitializer=d),p}async loadWeights(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,a]=sE(t),r=this.weightPathPrefix||n,s=[];for(let u of e)s.push(...u.weights);let i=[],o=[];for(let u of e)for(let c of u.paths)this.weightUrlConverter!=null?o.push(this.weightUrlConverter(c)):i.push(r+c+a);this.weightUrlConverter&&i.push(...await Promise.all(o));let l=await E0(i,{requestInit:this
2021-01-12 16:01:34 +01:00
Actual : $ { r } .
Expected : $ { s } . ` );for(let i=0;i<s.length;++i){let o=r[i],l=s[i];if(!n(o,l))throw new Error( ` Arrays differ : actual [ $ { i } ] = $ { o } , expected [ $ { i } ] = $ { l } .
Actual : $ { r } .
2021-03-09 23:32:33 +01:00
Expected : $ { s } . ` )}}function NE(e,t){e().then(()=>t.fail(),()=>t())}function TE(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return zr(e)||zr(e[0])||zr(t)||zr(t[0])?by(e,n,(a,r)=>a==r):by(e,t,(a,r)=>xy(a,r,0))}function SE(e,t,n){if(n==null&&(n=yy()),!xy(e,t,n))throw new Error( ` Numbers differ : actual === $ { e } , expected === $ { t } ` )}function xy(e,t,n){return!isFinite(e)&&!isFinite(t)?!0:!(isNaN(e)||isNaN(t)||Math.abs(e-t)>n)}function CE(e,t,n){for(let a=0;a<e.length;a++)if(e[a]<t||e[a]>n)throw new Error( ` Value out of range : $ { e [ a ] } low : $ { t } , high : $ { n } ` )}function AE(e,t){expect(new Float32Array(e)).toEqual(new Float32Array(t))}function J0(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?J0(n):e[t]=fc(n)}return e}var Q0="3.3.0";function _E(){Z().set("PROD",!0)}function EE(){Z().set("DEBUG",!0)}function $ E(){Z().set("DEPRECATION_WARNINGS_ENABLED",!1),console.warn("TensorFlow.js deprecation warnings have been disabled.")}function vy(e){Z().getBool("DEPRECATION_WARNINGS_ENABLED")&&console.warn(e+" You can disable deprecation warnings with tf.disableDeprecationWarnings().")}r_(vy);function DE(){P.disposeVariables()}function Ua(){return P}function Jd(){return P.memory()}function ME(e){return P.profile(e)}function M(e,t){return P.tidy(e,t)}function Ee(e){Kg(e).forEach(t=>t.dispose())}function qt(e){return P.keep(e)}function RE(e){return P.time(e)}function PE(e){return P.setBackend(e)}function OE(){return P.ready()}function LE(){return P.backendName}function zE(e){P.removeBackend(e)}function WE(e){return P.findBackend(e)}function BE(e){return P.findBackendFactory(e)}function Qd(e,t,n=1){return P.registerBackend(e,t,n)}function Z0(){return P.backend}function VE(e,t){Z().setPlatform(e,t)}function UE(e,t){let n=_(e,"a","add"),a=_(t,"b","add");[n,a]=Tt(n,a);let r={a:n,b:a};return P.runKernel(Br,r)}var J=O({add_:UE});function GE(e,t){let n=_(e,"a","floorDiv"),a=_(t,"b","floorDiv");[n,a]=Tt(n,a);let r={a:n,b:a};return P.runKernel(Ls,r)}var Zd=O({floorDiv_:GE});function HE(e,t){let n=_(e,"a","div"),a=_(t,"b","div");if([n,a]=Tt(n,a),n.dtype==="int32"&&a.dtype==="int32")return Zd(n,a);let r={a:n,b:a},s={};return P.runKernel(Rs,r,s)}var xe=O({div_:HE});function jE(e,t){let n=_(e,"a","mul"),a=_(t,"b","mul");[n,a]=Tt(n,a);let r={a:n,b:a};return P.runKernel(Ys,r)}var B=O({mul_:jE});function qE(e){let t=_(e,"x","abs");if(t.dtype==="complex64"){let n={x:t};return P.runKernel(Qu,n)}else{let n={x:t};return P.runKernel(So,n)}}var zt=O({abs_:qE});function XE(e){let t={x:_(e,"x","acos")};return P.runKernel(Co,t)}var wy=O({acos_:XE});function KE(e){let t={x:_(e,"x","acosh")};return P.runKernel(Ao,t)}var ky=O({acosh_:KE});function YE(e){ $ (Array.isArray(e),()=>"The argument passed to tf.addN() must be a list of tensors"), $ (e.length>=1,()=> ` Must pass at least one tensor to tf . addN ( ) , but got $ { e . length } ` );let t=e.map((r,s)=>_(r, ` tensors$ { s } ` ,"addN")),n=t[0];t.forEach(r=>{if(r.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),t.forEach(r=>{if(!hr(r.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});let a=t;return P.runKernel(Ns,a)}var e1=O({addN_:YE});function JE(e,t=null,n=!1){let a={x:_(e,"x","all","bool")},r={axis:t,keepDims:n};return P.runKernel(ud,a,r)}var eh=O({all_:JE});function QE(e,t=null,n=!1){let a={x:_(e,"x","any","bool")},r={axis:t,keepDims:n};return P.runKernel(cd,a,r)}var Ic=O({any_:QE});function ZE(e,t=0){let n={x:_(e,"x","argMax")},a={axis:t};return P.runKernel(Ts,n,a)}var Nc=O({argMax_:ZE});function e $ (e,t=0){let n={x:_(e,"x","argMin")},a={axis:t};return P.runKernel(Ku,n,a)}var Iy=O({argMin_:e $ });function t $ (e){let t={x:_(e,"x","asin")};return P.runKernel(Fo,t)}var Ny=O({asin_:t $ });function n $ (e){let t={x:_(e,"x","asinh")};return P.runKernel(_o,t)}var Ty=O({asinh_:n $ });function a $ (e){let t={x:_(e,"x","atan")};return P.runKernel(Eo,t)}var Sy=O({atan_:a $ });function r $ (e,t){let n=_(e,"a","atan2"),a=_(t,"b","atan2");[n,a]=Tt(n,a);let r={a:n,b:a};return P.runKernel(Do,r)}var Cy=O({atan2_:r $ });function s $ (e){let t={x:_(
with dtype $ { s . dtype } . ` )}),n.length===1)return Kr(n[0]);let a=n,r={axis:t};return P.runKernel(Mo,a,r)}var Je=O({concat_:f $ });function g $ (e){let t={x:_(e,"x","sigmoid")};return P.runKernel(li,t)}var ca=O({sigmoid_:g $ });function y $ (e,t,n){let a=_(e,"x","slice","string_or_numeric");if(a.rank===0)throw new Error("Slicing scalar is not possible");let r={x:a},s={begin:t,size:n};return P.runKernel(ml,r,s)}var Be=O({slice_:y $ });function b $ (e){let t={x:_(e,"x","tanh")};return P.runKernel(mi,t)}var Dl=O({tanh_:b $ });function x $ (e,t,n,a,r,s){let i=_(e,"forgetBias","basicLSTMCell"),o=_(t,"lstmKernel","basicLSTMCell"),l=_(n,"lstmBias","basicLSTMCell"),u=_(a,"data","basicLSTMCell"),c=_(r,"c","basicLSTMCell"),p=_(s,"h","basicLSTMCell"),d=Je([u,p],1),h=ze(d,o),m=J(h,l),f=m.shape[0],g=m.shape[1]/4,y=[f,g],b=Be(m,[0,0],y),x=Be(m,[0,g],y),v=Be(m,[0,g*2],y),k=Be(m,[0,g*3],y),w=J(B(ca(b),Dl(x)),B(c,ca(J(i,v)))),S=B(Dl(w),ca(k));return[w,S]}var v $ =O({basicLSTMCell_:x $ });function w $ (e,t,n){let a=_(e,"x","batchToSpaceND"),r=t.reduce((o,l)=>o*l); $ (a.rank>=1+t.length,()=> ` input rank is $ { a . rank } but should be > than blockShape . length $ { t . length } ` ), $ (n.length===t.length,()=> ` crops . length is $ { n . length } but should be equal to blockShape . length $ { t . length } ` ), $ (a.shape[0]%r==0,()=> ` input tensor batch is $ { a . shape [ 0 ] } but is not divisible by the product of the elements of blockShape $ { t . join ( " * " ) } === $ { r } ` );let s={x:a},i={blockShape:t,crops:n};return P.runKernel(Ju,s,i)}var Sc=O({batchToSpaceND_:w $ });function k $ (e){let t;return e.rank===0||e.rank===1?t=H(e,[1,1,1,e.size]):e.rank===2?t=H(e,[1,1,e.shape[0],e.shape[1]]):e.rank===3?t=H(e,[1,e.shape[0],e.shape[1],e.shape[2]]):t=e,t}function I $ (e,t,n,a,r,s){s==null&&(s=.001);let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),l=_(n,"variance","batchNorm"),u;r!=null&&(u=_(r,"scale","batchNorm"));let c;a!=null&&(c=_(a,"offset","batchNorm")), $ (o.rank===l.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."), $ (c==null||o.rank===c.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."), $ (u==null||o.rank===u.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let p={x:k $ (i),scale:u,offset:c,mean:o,variance:l},d={varianceEpsilon:s},h=P.runKernel(zs,p,d);return H(h,i.shape)}var fr=O({batchNorm_:I $ });function N $ (e,t,n,a,r,s){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),l=_(n,"variance","batchNorm"),u;r!=null&&(u=_(r,"scale","batchNorm"));let c;return a!=null&&(c=_(a,"offset","batchNorm")), $ (i.rank===2,()=> ` Error in batchNorm2D : x must be rank 2 but got rank $ { i . rank } . ` ), $ (o.rank===2||o.rank===1,()=> ` Error in batchNorm2D : mean must be rank 2 or rank 1 but got rank $ { o . rank } . ` ), $ (l.rank===2||l.rank===1,()=> ` Error in batchNorm2D : variance must be rank 2 or rank 1 but got rank $ { l . rank } . ` ),u!=null&& $ (u.rank===2||u.rank===1,()=> ` Error in batchNorm2D : scale must be rank 2 or rank 1 but got rank $ { u . 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 } . ` ),fr(i,o,l,c,u,s)}var r1=O({batchNorm2d_:N $ });function T $ (e,t,n,a,r,s){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),l=_(n,"variance","batchNorm"),u;r!=null&&(u=_(r,"scale","batchNorm"));let c;return a!=null&&(c=_(a,"offset","batchNorm")), $ (i.rank===3,()=> ` Error in batchNorm3D : x must be rank 3 but got rank $ { i . rank } . ` ), $ (o.rank===3||o.rank===1,()=> ` Error in batchNorm3D : mean must be rank 3 or rank 1 but got rank $ { o . rank } . ` ), $ (l.rank===3||l.rank===1,()=> ` Error in batchNorm3D : variance must be rank 3 or rank 1 but got rank $ { l . rank } . ` ),u!=null&& $ (u.rank===3||u.rank===1,()=> ` Error in batchNorm3D : scale must be rank 3 or rank 1 but got rank $ { u . 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 } . ` ),fr(i,o,l,c,u,s)}var s1=O({batchNorm3d_:T $ });function S $ (e,t,n,a,r,s){let i=_(e,"x","batchNorm"),o=_(t,"mean","batchNorm"),l=_(n,"variance","batchNorm"),u;r!=null&&(u=_(r,"scale","batchNorm"));let c;return a!=nul
2021-01-12 16:01:34 +01:00
$ { r } and $ { t } for depthToSpace with input shape
2021-03-09 23:32:33 +01:00
$ { a . shape } ` ), $ (s*t>=0,()=> ` Negative dimension size caused by overflow when multiplying
2021-01-12 16:01:34 +01:00
$ { s } and $ { t } for depthToSpace with input shape
2021-03-09 23:32:33 +01:00
$ { a . shape } ` ), $ (i%(t*t)==0,()=> ` Dimension size must be evenly divisible by $ { t * t } but is $ { i } for depthToSpace with input shape $ { a . shape } ` );let o={x:a},l={blockSize:t,dataFormat:n};return P.runKernel(Oo,o,l)}var Ry=O({depthToSpace_:q $ });function X $ (e,t,n,a,r="NHWC",s=[1,1],i){let o=_(e,"x","depthwiseConv2d"),l=_(t,"filter","depthwiseConv2d"),u=o,c=!1;o.rank===3&&(c=!0,u=H(o,[1,o.shape[0],o.shape[1],o.shape[2]])), $ (u.rank===4,()=> ` Error in depthwiseConv2d : input must be rank 4 , but got rank $ { u . rank } . ` ), $ (l.rank===4,()=> ` Error in depthwiseConv2d : filter must be rank 4 , but got rank $ { l . rank } . ` ), $ (u.shape[3]===l.shape[2],()=> ` Error in depthwiseConv2d : number of input channels ( $ { u . shape [ 3 ] } ) must match the inChannels dimension in filter $ { l . shape [ 2 ] } . ` ),i!=null&& $ (Ht(a),()=> ` Error in depthwiseConv2d : pad must be an integer when using , dimRoundingMode $ { i } but got pad $ { a } . ` );let p={x:u,filter:l},d={strides:n,pad:a,dataFormat:r,dilations:s,dimRoundingMode:i},h=P.runKernel(Ms,p,d);return c?H(h,[h.shape[1],h.shape[2],h.shape[3]]):h}var Qr=O({depthwiseConv2d_:X $ });function K $ (e){let t={x:_(e,"x","diag")};return P.runKernel(wd,t)}var Y $ =O({diag_:K $ });function J $ (e,t,n,a,r=[1,1],s="NHWC"){let i=_(e,"x","dilation2d"),o=_(t,"filter","dilation2d"); $ (i.rank===3||i.rank===4,()=> ` Error in dilation2d : input must be rank 3 or 4 , but got rank $ { i . rank } . ` ), $ (o.rank===3,()=> ` Error in dilation2d : filter must be rank 3 , but got rank $ { o . rank } . ` ), $ (s==="NHWC",()=> ` Error in dilation2d : Only NHWC is currently supported , but got dataFormat of $ { s } ` );let l=i,u=!1;i.rank===3&&(l=H(i,[1,i.shape[0],i.shape[1],i.shape[2]]),u=!0);let c={x:l,filter:o},p={strides:n,pad:a,dilations:r},d=P.runKernel(ec,c,p);return u?H(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var Py=O({dilation2d_:J $ });function Q $ (e,t){let n=e.length,a=[];for(let r=0;r<n;r++){let s=n-1-r,i=e[s]||1;(t[t.length-1-r]||1)>1&&i===1&&a.unshift(s)}return a}function Wt(e,t){let n=[];for(let a=0;a<t.length;a++){let r=e[e.length-a-1],s=t.length-a-1,i=t[s];(r==null||r===1&&i>1)&&n.unshift(s)}return n}function xt(e,t){let n=[],a=Math.max(e.length,t.length);for(let r=0;r<a;r++){let s=e[e.length-r-1];s==null&&(s=1);let i=t[t.length-r-1];if(i==null&&(i=1),s===1)n.unshift(i);else if(i===1)n.unshift(s);else if(s!==i){let o= ` Operands could not be broadcast together with shapes $ { e } and $ { t } . ` ;throw Error(o)}else n.unshift(s)}return n}function Z $ (e,t){let n=_(e,"a","equal"),a=_(t,"b","equal");[n,a]=Tt(n,a),xt(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(Wo,r)}var Zr=O({equal_:Z $ });function eD(e,t,n){let a=_(t,"a","where"),r=_(n,"b","where"),s=_(e,"condition","where","bool"),i=xt(a.shape,r.shape),o=Cc(a,i),l=Cc(r,i);s.rank===1&& $ (s.shape[0]===a.shape[0],()=>"The first dimension of ` a ` must match the size of ` condition ` ."),s.rank!==1&&ln(s.shape,l.shape,"Error in where: ");let u={condition:s,t:o,e:l};return P.runKernel(dl,u)}var Tn=O({where_:eD});function tD(e){let t={x:_(e,"x","zerosLike")};return P.runKernel(Il,t)}var Ge=O({zerosLike_:tD});function nD(e,t){let n=_(e,"a","div"),a=_(t,"b","div");[n,a]=Tt(n,a);let r=xe(n,a),s=Ge(r),i=Zr(a,s);return Tn(i,s,r)}var Oy=O({divNoNan_:nD});function aD(e,t){let n=_(e,"t1","dot"),a=_(t,"t2","dot"); $ ((n.rank===1||n.rank===2)&&(a.rank===1||a.rank===2),()=> ` Error in dot : inputs must all be rank 1 or 2 , but got ranks $ { n . rank } and $ { a . rank } . ` );let r=n.rank===1?n.size:n.shape[1],s=a.rank===1?a.size:a.shape[0];if( $ (r===s,()=> ` Error in dot : inner dimensions of inputs must match , but got $ { r } and $ { s } . ` ),n.rank===1&&a.rank===1){let i=H(n,[1,-1]),o=H(a,[-1,1]),l=ze(i,o);return H(l,[])}else if(n.rank===1&&a.rank===2){let i=H(n,[1,-1]),o=H(a,[a.shape[0],a.shape[1]]),l=ze(i,o);return H(l,[l.size])}else if(n.rank===2&&a.rank===1){let i=H(a,[-1,1]),o=ze(n,i);return H(o,[o.size])}else{let i=H(a,[a.shape[0],a.shape[1]]);return ze(n,i)}}var m1=O({dot_:aD});function rD(e){let t={x:_(e,"x","elu")};return P.runKernel(Lo,t)}var Ml=O({elu_:rD});function sD(e){let t=_(e,"x","erf"); $ (t.dtype==="int32"||t.dtype==="float32",()=>"Input dtype must be ` int32 ` or ` float32 ` ."),t.dtype=
rank $ { s . rank } . ` ), $ (Ht(t),()=> ` Error in localResponseNormalization : depthRadius must be an integer but got depthRadius $ { t } . ` );let i=s,o=!1;s.rank===3&&(o=!0,i=H(s,[1,s.shape[0],s.shape[1],s.shape[2]]));let l={x:i},u={depthRadius:t,bias:n,alpha:a,beta:r},c=P.runKernel(rc,l,u);return o?H(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var By=O({localResponseNormalization_:kD});function ID(e){let t={x:_(e,"x","log")};return P.runKernel(Us,t)}var Pn=O({log_:ID});function ND(e){let t={x:_(e,"x","log1p")};return P.runKernel(Qo,t)}var lh=O({log1p_:ND});function TD(e){return $ (Wr(e),()=>"The f passed in grad(f) must be a function"),(t,n)=>{let a=_(t,"x","tf.grad","string_or_numeric"),r=n!=null?_(n,"dy","tf.grad"):null;return P.tidy(()=>{let{value:s,grads:i}=P.gradients(()=>e(a),[a],r);return r!=null&&ln(s.shape,r.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),uh(i),i[0]})}}function SD(e){return $ (Wr(e),()=>"The f passed in grads(f) must be a function"),(t,n)=>{ $ (Array.isArray(t),()=>"The args passed in grads(f)(args) must be an array of ` Tensor ` s or ` TensorLike ` s");let a=wc(t,"args","tf.grads","string_or_numeric"),r=n!=null?_(n,"dy","tf.grads"):null;return P.tidy(()=>{let{value:s,grads:i}=P.gradients(()=>e(...a),a,r);return r!=null&&ln(s.shape,r.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),uh(i),i})}}function CD(e){return $ (Wr(e),()=>"The f passed in valueAndGrad(f) must be a function"),(t,n)=>{ $ (t instanceof Fe,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"), $ (n==null||n instanceof Fe,()=>"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");let{grads:a,value:r}=P.gradients(()=>e(t),[t],n);return uh(a),{grad:a[0],value:r}}}function AD(e){return $ (Wr(e),()=>"The f passed in valueAndGrads(f) must be a function"),(t,n)=>{ $ (Array.isArray(t)&&t.every(r=>r instanceof Fe),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"), $ (n==null||n instanceof Fe,()=>"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");let a=P.gradients(()=>e(...t),t,n);return n!=null&&ln(a.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),uh(a.grads),a}}function x1(e,t){ $ (Wr(e),()=>"The f passed in variableGrads(f) must be a function"), $ (t==null||Array.isArray(t)&&t.every(u=>u instanceof Hr),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let n=t!=null;if(!n){t=[];for(let u in P.registeredVariables)t.push(P.registeredVariables[u])}let a=n?t.filter(u=>!u.trainable):null,r=t.length;t=t.filter(u=>u.trainable), $ (t.length>0,()=> ` variableGrads ( ) expects at least one of the input variables to be trainable , but none of the $ { r } variables is trainable . ` );let s=!0,{value:i,grads:o}=P.gradients(e,t,null,s); $ (o.some(u=>u!=null),()=>"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."), $ (i.rank===0,()=> ` The f passed in variableGrads ( f ) must return a scalar , but it returned a rank - $ { i . rank } tensor ` );let l={};return t.forEach((u,c)=>{o[c]!=null&&(l[u.name]=o[c])}),a!=null&&a.forEach(u=>l[u.name]=null),{value:i,grads:l}}function ja(e){return P.customGrad(e)}function uh(e){if(e.filter(t=>t==null).length>0)throw new Error( ` Cannot compute gradient of y = f ( x ) with respect to x . Make sure that
the f you passed encloses all operations that lead from x to y . ` )}function FD(e){let t={x:_(e,"x","neg")};return P.runKernel(tl,t)}var St=O({neg_:FD});function _D(e){let t={x:_(e,"x","softplus")};return P.runKernel(yl,t)}var Pl=O({softplus_:_D});function ED(e){let t=_(e,"x","logSigmoid");return ja(n=>({value:St(Pl(St(n))),gradFunc:a=>B(a,ca(St(n)))}))(t)}var v1=O({logSigmoid_:ED});function $ D(e,t=null,n=!1){let a={x:_(e,"x","max")},r={reductionIndices:t,keepDims:n};return P.runKernel(Gs,a,r)}var Zn=O({max_: $ D});function DD(e,t){let n=_(e,"a","sub"),a=_(t,"b","sub");[n,a]=Tt(n,a);let r={a:n,b:a};return P.runKernel(hi,r)}var me=O({sub_:DD});function MD(e,t=null,n=!1){let a=_(e,"x","sum");a.dtype==="bool"&&(a=ue(a,"int32"));let r={x:a},s={axis:t,keepDims:n};return P.runKernel(ci,r,s)}var Se=O({sum_:MD});function RD(e,t=-1){let n=_(e,"logits","logSoftmax");if(t===-1&&(t=n.rank-1),t!==n.rank-1)throw Error( ` Log Softmax along a non - last dimension is not yet supported . Logits was rank $ { n . rank } and axis was $ { t } ` );return ja((a,r)=>{let s=!0,i=Zn(a,t,!0),o=me(a,i),l=me(ue(o,"float32"),Pn(Se(dn(o),t,s)));return r([l]),{value:l,gradFunc:(u,c)=>{let[p]=c,d=!0,h=dn(p);return me(u,B(Se(u,t,d),h))}}})(n)}var ch=O({logSoftmax_:RD});function Vy(e,t){for(let n=0;n<e.length;++n)if(e[e.length-n-1]!==t-1-n)return!1;return!0}function w1(e,t,n){let a=e.length+t.length,r=[],s=0,i=0;for(let o=0;o<a;o++)n.indexOf(o)===-1?r.push(e[s++]):r.push(t[i++]);return r}function k1(e,t){let n=[],a=e.length;for(let s=0;s<a;s++)t.indexOf(s)===-1&&n.push(e[s]);let r=t.map(s=>e[s]);return[n,r]}function Ci(e,t){let n=t.map(a=>1);return w1(e,n,t)}function PD(e,t,n){ $ (Vy(t,n),()=> ` $ { e } supports only inner - most axes for now . Got axes $ { t } and rank - $ { n } input . ` )}function I1(e,t){if(Vy(e,t))return null;let n=[];for(let a=0;a<t;++a)e.indexOf(a)===-1&&n.push(a);return e.forEach(a=>n.push(a)),n}function Uy(e){return e.map((t,n)=>[n,t]).sort((t,n)=>t[1]-n[1]).map(t=>t[0])}function OD(e,t){let n=[];for(let a=t-e;a<t;++a)n.push(a);return n}function LD(e,t=null,n=!1){let a=_(e,"x","logSumExp"),r=la(t,a.shape),s=Zn(a,r,!0),i=me(a,s),o=dn(i),l=Se(o,r),u=Pn(l),c=J(H(s,u.shape),u);if(n){let p=Ci(c.shape,r);return H(c,p)}return c}var Gy=O({logSumExp_:LD});function zD(e,t){let n=_(e,"a","logicalAnd","bool"),a=_(t,"b","logicalAnd","bool");xt(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(Zo,r)}var da=O({logicalAnd_:zD});function WD(e){let t={x:_(e,"x","logicalNot","bool")};return P.runKernel(nc,t)}var _c=O({logicalNot_:WD});function BD(e,t){let n=_(e,"a","logicalOr","bool"),a=_(t,"b","logicalOr","bool");xt(n.shape,a.shape);let r={a:n,b:a};return P.runKernel(ac,r)}var ph=O({logicalOr_:BD});function VD(e,t){let n=_(e,"a","logicalXor","bool"),a=_(t,"b","logicalXor","bool");return xt(n.shape,a.shape),da(ph(e,t),_c(da(e,t)))}var N1=O({logicalXor_:VD});function UD(e,t,n,a,r){let s=_(e,"x","maxPool"),i=1,o=s,l=!1;s.rank===3&&(l=!0,o=H(s,[1,s.shape[0],s.shape[1],s.shape[2]])), $ (o.rank===4,()=> ` Error in maxPool : input must be rank 4 but got rank $ { o . rank } . ` ), $ (Ga(n,i),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { n } and dilations '${i}' ` ),r!=null&& $ (Ht(a),()=> ` Error in maxPool : pad must be an integer when using , dimRoundingMode $ { r } but got pad $ { a } . ` );let u={x:o},c={filterSize:t,strides:n,pad:a,dimRoundingMode:r},p=P.runKernel(js,u,c);return l?H(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var $ t=O({maxPool_:UD});function GD(e,t=[1,1,1],n,a,r,s="NDHWC"){let i=_(e,"x","maxPool3d"),o=i,l=!1;i.rank===4&&(l=!0,o=H(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])), $ (o.rank===5,()=> ` Error in maxPool3d : x must be rank 5 but got rank $ { o . rank } . ` ), $ (s==="NDHWC",()=> ` Error in maxPool3d : Only NDHWC is currently supported , but got dataFormat of $ { s } ` ),r!=null&& $ (Ht(a),()=> ` Error in maxPool3d : pad must be an integer when using , dimRoundingMode $ { r } but got pad $ { a } . ` );let u={x:o},c={filterSize:t,strides:n,pad:a,dimRoundingMode:r,dataFormat:s},p=P.runKernel(sc,u,c);return l?H(p,[p.shape[1],p.shape[2],p.shape[3],p.shape[4]]):p}var Hy=O({maxPool3d_:GD});function HD(e,
$ { s } ) . ` );if(n<a)throw new Error( ` batchDims ( $ { a } ) must be less than or equal to axis ( $ { n } ) . ` );for(let p=0;p<a;++p)if(e.shape[p]!==t.shape[p])throw new Error( ` x . shape [ $ { p } ] : $ { e . shape [ p ] } should be equal to indices . shape [ $ { p } ] : $ { t . shape [ p ] } . ` );let i=e.shape[n],o=[],l=1,u=1,c=1;for(let p=0;p<a;++p)o.push(e.shape[p]),l*=e.shape[p];for(let p=a;p<n;p++)o.push(e.shape[p]),u*=e.shape[p];for(let p=a;p<r;p++)o.push(t.shape[p]);for(let p=n+1;p<s;p++)o.push(e.shape[p]),c*=e.shape[p];return{batchSize:l,sliceSize:c,outerSize:u,dimSize:i,outputShape:o}}function SO(e){try{return e.map(t=>Gd(t))}catch(t){throw new Error( ` Failed to decode encoded string bytes into utf - 8 , error : $ { t } ` )}}function CO(e){return e.map(t=>fc(t))}var Ya={};Oe(Ya,{nonMaxSuppressionV3Impl:()=>B1,nonMaxSuppressionV4Impl:()=>V1,nonMaxSuppressionV5Impl:()=>U1,whereImpl:()=>E1});var Q1={kernelName:So,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>B(e,Vl(ue(n,"float32"),-1))}}},EO={kernelName:Co,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let a=lt(ue(n,"float32")),r=an(me(ce(1),a));return St(xe(e,r))}}}}, $ O={kernelName:Ao,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let a=an(me(lt(ue(n,"float32")),1));return xe(e,a)}}}},DO={kernelName:Br,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,a]=t,r=xt(n.shape,a.shape);return{a:()=>{let s=e,i=Wt(n.shape,r);return i.length>0&&(s=Se(s,i)),H(s,n.shape)},b:()=>{let s=e,i=Wt(a.shape,r);return i.length>0&&(s=Se(s,i)),H(s,a.shape)}}}},MO={kernelName:Ns,saveAllInputs:!0,gradFunc:(e,t)=>{let n={};return t.forEach((a,r)=>{n[r]=()=>e.clone()}),n}},RO={kernelName:Ts,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>Ge(n)}}},PO={kernelName:Ku,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>Ge(n)}}},OO={kernelName:Fo,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>xe(e,an(me(ce(1),lt(ue(n,"float32")))))}}},LO={kernelName:_o,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let a=an(J(ce(1),lt(ue(n,"float32"))));return xe(e,a)}}}},zO={kernelName:Do,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,a]=t,r=xt(n.shape,a.shape);return{a:()=>{let s=J(lt(n),lt(a)),i=B(e,xe(a,s)),o=Wt(n.shape,r);return o.length>0&&(i=Se(i,o)),H(i,n.shape)},b:()=>{let s=J(lt(n),lt(a)),i=St(B(e,xe(n,s))),o=Wt(a.shape,r);return o.length>0&&(i=Se(i,o)),H(i,a.shape)}}}},WO={kernelName:Eo,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>xe(e,J(lt(ue(n,"float32")),1))}}},BO={kernelName: $ o,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>xe(e,me(ce(1),lt(ue(n,"float32"))))}}};function VO(e,t,n,a,r,s){let i=_(e,"dy","avgPool3dGrad"),o=_(t,"input","avgPool3dGrad"),l=i,u=o,c=!1;o.rank===4&&(c=!0,l=H(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]]),u=H(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]])), $ (l.rank===5,()=> ` Error in avgPool3dGrad : dy must be rank 5 but got rank $ { l . rank } . ` ), $ (u.rank===5,()=> ` Error in avgPool3dGrad : input must be rank 5 but got rank $ { u . rank } . ` ),s!=null&& $ (Ht(r),()=> ` Error in avgPool3dGrad : pad must be an integer when using , dimRoundingMode $ { s } but got pad $ { r } . ` );let p={dy:l,input:u},d={filterSize:n,strides:a,pad:r,dimRoundingMode:s},h=P.runKernel(dd,p,d);return c?H(h,[h.shape[1],h.shape[2],h.shape[3],h.shape[4]]):h}var UO=O({avgPool3dGrad_:VO}),GO={kernelName:Yu,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[a]=t,{filterSize:r,strides:s,pad:i,dimRoundingMode:o}=n;return{x:()=>UO(e,a,r,s,i,o)}}};function HO(e,t,n,a,r){let s=_(e,"dy","avgPoolGrad"),i=_(t,"input","avgPoolGrad"); $ (i.rank===s.rank,()=> ` Rank of input ( $ { i . rank } ) does not match rank of dy ( $ { s . rank } ) ` );let o=i,l=s,u=!1;i.rank===3&&(u=!0,o=H(i,[1,i.shape[0],i.shape[1],i.shape[2]]),l=H(s,[1,s.shape[0],s.shape[1],s.shape[2]])), $ (l.rank===4,()=> ` Error in avgPoolGrad : dy must be rank 4 but got rank $ { l . rank } . ` ), $ (o.rank===4,()=> ` Error in avgPoolGrad : input must be rank 4 but got rank $ { o . rank } . ` );let c={dy:l,input:o},p={filterSize:n,strides:a,pad:r},d=P.runKernel(pd,c,p);return u?H(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var jO=O({avgPoolGrad_:HO}),qO={kernelName:Ss,inputsToSave:["x"],gradFunc:(e,
2021-01-12 16:01:34 +01:00
1. The $ { a } is defined in Python , in which case it needs to be ported to TensorFlow . js or your JavaScript code .
2021-03-09 23:32:33 +01:00
2. The custom $ { a } is defined in JavaScript , but is not registered properly with tf . serialization . registerClass ( ) . ` );return i}else{let s=e;if(s.className==null||s.config==null)throw new V( ` $ { a } : Improper config format : $ { JSON . stringify ( s ) } .
'className' and 'config' must set . ` );let i=s.className,o,l;if(i in n?[o,l]=n[i]:i in ha?[o,l]=ha.className:i in t&&([o,l]=t[i]),o==null)throw new V( ` Unknown $ { a } : $ { i } . This may be due to one of the following reasons :
2021-01-12 16:01:34 +01:00
1. The $ { a } is defined in Python , in which case it needs to be ported to TensorFlow . js or your JavaScript code .
2021-03-09 23:32:33 +01:00
2. The custom $ { a } is defined in JavaScript , but is not registered properly with tf . serialization . registerClass ( ) . ` );if(l!=null){let u={};for(let h of Object.keys(ha))u[h]=ha[h];for(let h of Object.keys(n))u[h]=n[h];let c=s.config;c.customObjects=u;let p=Object.assign({},ha);for(let h of Object.keys(n))ha[h]=n[h];db(s.config);let d=l(o,s.config,n,r);return ha=Object.assign({},p),d}else{let u=Object.assign({},ha);for(let p of Object.keys(n))ha[p]=n[p];let c=new o(s.config);return ha=Object.assign({},u),c}}}function P3(e,t){return e<t?-1:e>t?1:0}function Oh(e,t){return-1*P3(e,t)}function as(e){if(e==null)return e;let t=[];for(let n of e)t.indexOf(n)===-1&&t.push(n);return t}function O3(e){if(e==null)throw new V( ` Invalid value in obj : $ { JSON . stringify ( e ) } ` );for(let t in e)if(e.hasOwnProperty(t))return!1;return!0}function Mi(e,t,n){if(n!=null&&e.indexOf(n)<0)throw new V( ` $ { n } is not a valid $ { t } . Valid values are $ { e } or null / undefined . ` )}function hb(e,t,n=0,a=Infinity){return Ja(n>=0),Ja(a>=n),Array.isArray(e)&&e.length>=n&&e.length<=a&&e.every(r=>typeof r===t)}function Kt(e,t){Array.isArray(e)?(I.assert(e.length>0,()=> ` $ { t } is unexpectedly an empty array . ` ),e.forEach((n,a)=>Kt(n, ` element $ { a + 1 } of $ { t } ` ))):I.assert(Number.isInteger(e)&&e>0,()=> ` Expected $ { t } to be a positive integer , but got $ { uk ( e ) } . ` )}function uk(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>uk(t)).join(",")+"]":typeof e=="string"? ` "${e}" ` : ` $ { e } ` }function L3(e,t){let n=I.now(),a;return(...r)=>{let s=I.now();return s-n<t||(n=s,a=e(...r)),a}}function ck(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}function mb(e,t){return M(()=>an(Se(B(e,e),t,!0)))}var zc=class extends re.Serializable{getConfig(){return{}}},fb=class extends zc{constructor(e){super();this.defaultMaxValue=2,this.defaultAxis=0,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return M(()=>{let t=mb(e,this.axis),n=Xt(t,0,this.maxValue);return B(e,xe(n,J(Bt(),t)))})}getConfig(){return{maxValue:this.maxValue,axis:this.axis}}};fb.className="MaxNorm";re.registerClass(fb);var gb=class extends zc{constructor(e){super();this.defaultAxis=0,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return M(()=>xe(e,J(Bt(),mb(e,this.axis))))}getConfig(){return{axis:this.axis}}};gb.className="UnitNorm";re.registerClass(gb);var yb=class extends zc{apply(e){return qe(e)}};yb.className="NonNeg";re.registerClass(yb);var bb=class extends zc{constructor(e){super();this.defaultMinValue=0,this.defaultMaxValue=1,this.defaultRate=1,this.defaultAxis=0,this.minValue=e.minValue!=null?e.minValue:this.defaultMinValue,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.rate=e.rate!=null?e.rate:this.defaultRate,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return M(()=>{let t=mb(e,this.axis),n=J(B(this.rate,Xt(t,this.minValue,this.maxValue)),B(1-this.rate,t));return B(e,xe(n,J(Bt(),t)))})}getConfig(){return{minValue:this.minValue,maxValue:this.maxValue,rate:this.rate,axis:this.axis}}};bb.className="MinMaxNorm";re.registerClass(bb);var pk={maxNorm:"MaxNorm",minMaxNorm:"MinMaxNorm",nonNeg:"NonNeg",unitNorm:"UnitNorm"};function Vt(e){return pb(e)}function dk(e,t={}){return Lc(e,re.SerializationMap.getMap().classNameMap,t,"constraint")}function Ut(e){if(e==null)return null;if(typeof e=="string"){let t={className:e in pk?pk[e]:e,config:{}};return dk(t)}else return e instanceof zc?e:dk(e)}function $ 3(e){return new fb(e)}function D3(e){return new gb(e)}function M3(){return new yb}function R3(e){return new bb(e)}var hk={};Oe(hk,{constant:()=>B3,glorotNormal:()=>X3,glorotUniform:()=>q3,heNormal:()=>K3,heUniform:()=>Y3,identity:()=>H3,leCunNormal:()=>J3,leCunUniform:()=>Q3,ones:()=>W3,orthogonal:()=>Z3,randomNormal:()=>U3,randomUniform:()=>V3,truncatedNormal:()=>G3,varianceScaling:()=>j3,zeros:()=>z3});var ez=["channelsFirst","channelsLast"],tz=["nearest","bilinear"],nz=["valid","same","causal"],az=["max","avg"],rz=["sum","mul","concat","ave"],Gl=new Map;function Mt(e){Mi(ez,"DataFormat",e)}function sz(
because the value dtype is $ { t . dtype } , but TensorArray dtype is $ { this . dtype } . ` );if(this.size()===0&&(this.elementShape==null||this.elementShape.length===0)&&(this.elementShape=t.shape),ga(this.elementShape,t.shape, ` TensorArray $ { this . name } : Could not write to TensorArray index $ { e } . ` ),n.read)throw new Error( ` TensorArray $ { this . name } : Could not write to TensorArray index $ { e } , because it has already been read . ` );if(n.written)throw new Error( ` TensorArray $ { this . name } : Could not write to TensorArray index $ { e } , because it has already been written . ` );n.tensor=t,qt(t),n.written=!0,this.tensors[e]=n}writeMany(e,t){if(e.length!==t.length)throw new Error( ` TensorArray $ { this . name } : could not write multiple tensors , because the index size : $ { e . length } is not the same as tensors size : $ { t . length } . ` );e.forEach((n,a)=>this.write(n,t[a]))}gather(e,t){if(!!t&&t!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but gather requested dtype $ { t } ` );if(e)e=e.slice(0,this.size());else{e=[];for(let a=0;a<this.size();a++)e.push(a)}if(e.length===0)return Yn([],[0].concat(this.elementShape));let n=this.readMany(e);return ga(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),Dt(n,0)}concat(e){if(!!e&&e!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but concat requested dtype $ { e } ` );if(this.size()===0)return Yn([],[0].concat(this.elementShape));let t=[];for(let a=0;a<this.size();a++)t.push(a);let n=this.readMany(t);return ga(this.elementShape,n[0].shape, ` TensorArray shape mismatch : tensor array shape ( $ { this . elementShape } ) vs first tensor shape ( $ { n [ 0 ] . shape } ) ` ),Je(n,0)}scatter(e,t){if(t.dtype!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but tensor has dtype $ { t . dtype } ` );if(e.length!==t.shape[0])throw new Error( ` Expected len ( indices ) == tensor . shape [ 0 ] , but saw : $ { e . length } vs . $ { t . shape [ 0 ] } ` );let n=Math.max(...e);if(!this.dynamicSize&&n>=this.maxSize)throw new Error( ` Max index must be < array size ( $ { n } vs . $ { this . maxSize } ) ` );this.writeMany(e,ct(t,0))}split(e,t){if(t.dtype!==this.dtype)throw new Error( ` TensorArray dtype is $ { this . dtype } but tensor has dtype $ { t . dtype } ` );let n=0,a=e.map(o=>(n+=o,n));if(n!==t.shape[0])throw new Error( ` Expected sum of lengths to be equal to
2020-10-12 20:28:19 +02:00
tensor . shape [ 0 ] , but sum of lengths is
2021-03-09 23:32:33 +01:00
$ { n } , and tensor 's shape is: ${t.shape}`);if(!this.dynamicSize&&e.length!==this.maxSize)throw new Error(`TensorArray' s size is not equal to the size of lengths ( $ { this . maxSize } vs . $ { e . length } ) , and the TensorArray is not marked as dynamically resizeable ` );let r=n===0?0:t.size/n,s=[];M(()=>{t=H(t,[1,n,r]);for(let o=0;o<e.length;++o){let l=o===0?0:a[o-1],u=[0,l,0],c=[1,e[o],r];s[o]=H(Be(t,u,c),this.elementShape)}return s});let i=[];for(let o=0;o<e.length;o++)i[o]=o;this.writeMany(i,s)}},ap=class{constructor(e,t,n,a=-1){this.tensors=e,this.elementShape=t,this.elementDtype=n,e!=null&&e.forEach(r=>{if(n!==r.dtype)throw new Error( ` Invalid data types ; op elements $ { n } , but list elements $ { r . dtype } ` );ga(t,r.shape,"TensorList shape mismatch: "),qt(r)}),this.idTensor=ce(0),this.maxNumElements=a,qt(this.idTensor)}get id(){return this.idTensor.id}copy(){return new ap([...this.tensors],this.elementShape,this.elementDtype)}clearAndClose(e){this.tensors.forEach(t=>{(e==null||!e.has(t.id))&&t.dispose()}),this.tensors.length=0,this.idTensor.dispose()}size(){return this.tensors.length}stack(e,t,n=-1){if(t!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { t } , but list elements $ { this . elementDtype } ` );if(n!==-1&&this.tensors.length!==n)throw new Error( ` Operation expected a list with $ { n } elements but got a list with $ { this . tensors . length } elements . ` );ga(e,this.elementShape,"TensorList shape mismatch: ");let a=np(this.elementShape,this.tensors,e);return M(()=>{let r=this.tensors.map(s=>H(s,a));return Dt(r,0)})}popBack(e,t){if(t!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { t } , but list elements $ { this . elementDtype } ` );if(this.size()===0)throw new Error("Trying to pop from an empty list.");let n=np(this.elementShape,this.tensors,e),a=this.tensors.pop();return ga(a.shape,e,"TensorList shape mismatch: "),H(a,n)}pushBack(e){if(e.dtype!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { e . dtype } , but list elements $ { this . elementDtype } ` );if(ga(e.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");qt(e),this.tensors.push(e)}resize(e){if(e<0)throw new Error( ` TensorListResize expects size to be non - negative . Got : $ { e } ` );if(this.maxNumElements!==-1&&e>this.maxNumElements)throw new Error( ` TensorListResize input size $ { e } is greater maxNumElement $ { this . maxNumElements } . ` );this.tensors.length=e}getItem(e,t,n){if(n!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { n } , but list elements $ { this . elementDtype } ` );if(e<0||e>this.tensors.length)throw new Error( ` Trying to access element $ { e } in a list with $ { this . tensors . length } elements . ` );if(this.tensors[e]==null)throw new Error( ` element at index $ { e } is null . ` );ga(this.tensors[e].shape,t,"TensorList shape mismatch: ");let a=np(this.elementShape,this.tensors,t);return H(this.tensors[e],a)}setItem(e,t){if(t.dtype!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { t . dtype } , but list elements $ { this . elementDtype } ` );if(e<0||this.maxNumElements!==-1&&e>=this.maxNumElements)throw new Error( ` Trying to set element $ { e } in a list with max $ { this . maxNumElements } elements . ` );ga(this.elementShape,t.shape,"TensorList shape mismatch: "),qt(t),this.tensors[e]=t}gather(e,t,n){if(t!==this.elementDtype)throw new Error( ` Invalid data types ; op elements $ { t } , but list elements $ { this . elementDtype } ` );ga(this.elementShape,n,"TensorList shape mismatch: "),e=e.slice(0,this.size());let a=np(this.elementShape,this.tensors,n);return e.length===0?Yn([],[0].concat(a)):M(()=>{let r=e.map(s=>H(this.tensors[s],a));return Dt(r,0)})}concat(e,t){if(!!e&&e!==this.elementDtype)throw new Error( ` TensorList dtype is $ { this . elementDtype } but concat requested dtype $ { e } ` );ga(this.elementShape,t,"TensorList shape mismatch: ");let n=np(this.elementShape,this.tensors,t);return this.size()===0?Yn([],[0].concat(n)):M(()=>{let a=this.tensors.map(r=>H(r,n));return Je(a,0)})}};function z4(e,t,n){let a=e.dtype;if(e.shape.length<1)throw new Error( ` Tensor m
2020-10-12 20:28:19 +02:00
tensor . shape [ 0 ] , but sum of lengths is
2021-03-09 23:32:33 +01:00
$ { a } , and tensor ' s shape is : $ { e . shape } ` );let s=e.shape.slice(1),i=iv(s,n),o=a===0?0:e.size/a,l=M(()=>{let c=[];e=H(e,[1,a,o]);for(let p=0;p<t.length;++p){let d=p===0?0:r[p-1],h=[0,d,0],m=[1,t[p],o];c[p]=H(Be(e,h,m),i)}return e.dispose(),c}),u=new ap([],n,e.dtype,t.length);for(let c=0;c<l.length;c++)u.setItem(c,l[c]);return u}var U4=async(e,t,n)=>{switch(e.op){case"If":case"StatelessIf":{let a=N("thenBranch",e,t,n),r=N("elseBranch",e,t,n),s=N("cond",e,t,n),i=N("args",e,t,n);return(await s.data())[0]?n.functionMap[a].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap):n.functionMap[r].executeFunctionAsync(i,n.tensorArrayMap,n.tensorListMap)}case"While":case"StatelessWhile":{let a=N("body",e,t,n),r=N("cond",e,t,n),s=N("args",e,t,n),i=await n.functionMap[r].executeFunctionAsync(s,n.tensorArrayMap,n.tensorListMap),o=s.map(c=>c.id),l=await i[0].data();i.forEach(c=>{!c.kept&&o.indexOf(c.id)===-1&&c.dispose()});let u=s;for(;l[0];){let c=u;u=await n.functionMap[a].executeFunctionAsync(u,n.tensorArrayMap,n.tensorListMap);let p=u.map(h=>h.id);c.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()});let d=await n.functionMap[r].executeFunctionAsync(u,n.tensorArrayMap,n.tensorListMap);l=await d[0].data(),d.forEach(h=>{!h.kept&&o.indexOf(h.id)===-1&&p.indexOf(h.id)===-1&&h.dispose()})}return u}case"LoopCond":{let a=N("pred",e,t,n);return[Ir(a)]}case"Switch":{let a=N("pred",e,t,n),r=N("data",e,t,n);return r.kept||(r=Ir(r)),(await a.data())[0]?[void 0,r]:[r,void 0]}case"Merge":{let a=e.inputNames.find(r=>Fn(r,t,n)!==void 0);if(a){let r=Fn(a,t,n);return[Ir(r)]}return}case"Enter":{let a=N("frameName",e,t,n),r=N("tensor",e,t,n);return n.enterFrame(a),[Ir(r)]}case"Exit":{let a=N("tensor",e,t,n);return n.exitFrame(),[Ir(a)]}case"NextIteration":{let a=N("tensor",e,t,n);return n.nextIteration(),[Ir(a)]}case"TensorArrayV3":{let a=N("size",e,t,n),r=N("dtype",e,t,n),s=N("elementShape",e,t,n),i=N("dynamicSize",e,t,n),o=N("clearAfterRead",e,t,n),l=N("identicalElementShapes",e,t,n),u=N("name",e,t,n),c=new L4(u,r,a,s,l,i,o);return n.addTensorArray(c),[c.idTensor,ce(1)]}case"TensorArrayWriteV3":{let a=N("tensorArrayId",e,t,n),r=N("index",e,t,n),s=N("tensor",e,t,n),i=n.getTensorArray(a.id);return i.write(r,s),[i.idTensor]}case"TensorArrayReadV3":{let a=N("tensorArrayId",e,t,n),r=N("index",e,t,n);return[n.getTensorArray(a.id).read(r)]}case"TensorArrayGatherV3":{let a=N("tensorArrayId",e,t,n),r=N("indices",e,t,n),s=N("dtype",e,t,n);return[n.getTensorArray(a.id).gather(r,s)]}case"TensorArrayScatterV3":{let a=N("tensorArrayId",e,t,n),r=N("indices",e,t,n),s=N("tensor",e,t,n),i=n.getTensorArray(a.id);return i.scatter(r,s),[i.idTensor]}case"TensorArrayConcatV3":{let a=N("tensorArrayId",e,t,n),r=n.getTensorArray(a.id),s=N("dtype",e,t,n);return[r.concat(s)]}case"TensorArraySplitV3":{let a=N("tensorArrayId",e,t,n),r=N("tensor",e,t,n),s=N("lengths",e,t,n),i=n.getTensorArray(a.id);return i.split(s,r),[i.idTensor]}case"TensorArraySizeV3":{let a=N("tensorArrayId",e,t,n),r=n.getTensorArray(a.id);return[ce(r.size(),"int32")]}case"TensorArrayCloseV3":{let a=N("tensorArrayId",e,t,n),r=n.getTensorArray(a.id);return r.clearAndClose(),[r.idTensor]}case"TensorListSetItem":{let a=N("tensorListId",e,t,n),r=N("index",e,t,n),s=N("tensor",e,t,n),i=n.getTensorList(a.id);return i.setItem(r,s),[i.idTensor]}case"TensorListGetItem":{let a=N("tensorListId",e,t,n),r=N("index",e,t,n),s=N("elementShape",e,t,n),i=N("elementDType",e,t,n);return[n.getTensorList(a.id).getItem(r,s,i)]}case"TensorListScatterV2":case"TensorListScatter":{let a=N("indices",e,t,n),r=N("tensor",e,t,n),s=N("elementShape",e,t,n),i=N("numElements",e,t,n),o=B4(r,a,s,i);return n.addTensorList(o),[o.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let a=N("elementShape",e,t,n),r=N("elementDType",e,t,n),s;e.op==="TensorListReserve"?s="numElements":s="maxNumElements";let i=N(s,e,t,n),o=W4(a,r,i);return n.addTensorList(o),[o.idTensor]}case"TensorListGather":{let a=N("tensorListId",e,t,n),r=N("indices",e,t,n),s=N("elementShape",e,t,n),i=N("elementDType",e,t,n);return[n.getTensor
$ { e } ` );let a;return this.size===Infinity||this.size==null?a=this.size:t?a=Math.ceil(this.size/e):a=Math.floor(this.size/e),Vn(async()=>(await n.iterator()).columnMajorBatch(e,t,GV),a)}concatenate(e){let t=this,n;return this.size===Infinity||e.size===Infinity?n=Infinity:this.size!=null&&e.size!=null?n=this.size+e.size:n=null,Vn(async()=>(await t.iterator()).concatenate(await e.iterator()),n)}filter(e){let t=this,n;return this.size===Infinity?n=Infinity:n=null,Vn(async()=>(await t.iterator()).filter(a=>M(()=>e(a))),n)}async forEachAsync(e){return(await this.iterator()).forEachAsync(e)}map(e){let t=this;return Vn(async()=>(await t.iterator()).map(n=>M(()=>e(n))),this.size)}mapAsync(e){let t=this;return Vn(async()=>(await t.iterator()).mapAsync(e),this.size)}prefetch(e){if(e==null)throw new RangeError(" ` Dataset . prefetch ( ) ` requires bufferSize to be specified.");let t=this;return Vn(async()=>(await t.iterator()).prefetch(e),this.size)}repeat(e){let t=this,n;return this.size!=null&&e>0?n=this.size*e:e===0?n=0:this.size!=null&&(e===void 0||e<0)?n=Infinity:n=null,Vn(async()=>{let a=cv(async()=>({value:await t.iterator(),done:!1}));return $ V(a.take(e))},n)}skip(e){let t=this,n;return this.size!=null&&e>=0&&this.size>=e?n=this.size-e:this.size!=null&&(this.size<e||e===void 0||e<0)?n=0:n=null,Vn(async()=>(await t.iterator()).skip(e),n)}shuffle(e,t,n=!0){if(e==null||e<0)throw this.size==null?new RangeError(" ` Dataset . shuffle ( ) ` requires bufferSize to be specified."):new RangeError( ` \ ` Dataset.shuffle() \` requires bufferSize to be specified. If your data fits in main memory (for regular JS objects), and/or GPU memory (for \` tf.Tensor \` s), consider setting bufferSize to the dataset size ( ${ this . size } elements) ` ) ; let a = this , r = kV . alea ( t || I . now ( ) . toString ( ) ) ; return Vn ( async ( ) => { let s = r . int32 ( ) ; return n && ( s += r . int32 ( ) ) , ( await a . iterator ( ) ) . shuffle ( e , s . toString ( ) ) } , this . size ) } take ( e ) { let t = this , n ; return this . size != null && this . size > e ? n = e : this . size != null && this . size <= e ? n = this . size : n = null , Vn ( async ( ) => ( await t . iterator ( ) ) . take ( e ) , n ) } async toArray ( ) { if ( this . size === Infinity ) throw new Error ( "Can not convert infinite data stream to array." ) ; return ( await this . iterator ( ) ) . toArray ( ) } async toArrayForTest ( ) { if ( this . size === Infinity ) throw new Error ( "Can not convert infinite data stream to array." ) ; return ( await this . iterator ( ) ) . toArrayForTest ( ) } } ; Jl . MAX _BUFFER _SIZE = 1e4 ; function Vn ( e , t = null ) { return new class extends Jl { constructor ( ) { super ( ... arguments ) ; this . size = t } async iterator ( ) { return e ( ) } } } function fV ( e ) { return Vn ( async ( ) => _N ( e ) , e . length ) } function gV ( e ) { if ( ! Ql ( e ) ) throw new Error ( "The argument to zip() must be an object or array." ) ; let t ; if ( Array . isArray ( e ) ) for ( let n = 0 ; n < e . length ; n ++ ) t = t == null ? e [ n ] . size : Math . min ( t , e [ n ] . size ) ; else if ( e instanceof Object ) for ( let n in e ) t = t == null ? e [ n ] . size : Math . min ( t , e [ n ] . size ) ; return Vn ( async ( ) => { let n = await AN ( e , a => { if ( a instanceof Jl ) return { value : a . iterator ( ) , recurse : ! 1 } ; if ( Ql ( a ) ) return { value : null , recurse : ! 0 } ; throw new Error ( "Leaves of the structure passed to zip() must be Datasets, not primitives." ) } ) ; return MV ( n , cs . SHORTEST ) } , t ) } function GV ( e ) { if ( e === null ) return null ; let t = e [ 0 ] ; return CV ( t ) ? { value : HV ( e ) , recurse : ! 1 } : { value : null , recurse : ! 0 } } function HV ( e ) { if ( e . length === 0 ) throw new Error ( "Can't make a batch of zero elements." ) ; return e [ 0 ] instanceof Fe ? Dt ( e ) : Yn ( e ) } var wN = class extends Jl { constructor ( e ) { super ( ) ; this . input = e } async iterator ( ) { return ( await this . input . iterator ( ) ) . decodeUTF8 ( ) . split ( `
` ).map(e=>(e.endsWith(" \r ")&&(e=e.slice(0,-1)),e))}},vm='"',rp=Symbol("out"),MN=Symbol("field"),wm=Symbol("quote"),dv=Symbol("quoteafterquote"),RN=Symbol("quoteinquote"),kN=class extends Jl{constructor(e,t){super();this.input=e,this.hasHeader=!0,this.fullColumnNames=null,this.columnNamesValidated=!1,this.columnConfigs=null,this.configuredColumnsOnly=!1,this.delimiter=",",this.delimWhitespace=!1,this.base=new wN(e),t||(t={}),this.hasHeader=t.hasHeader!==!1,this.fullColumnNames=t.columnNames,this.columnConfigs=t.columnConfigs,this.configuredColumnsOnly=t.configuredColumnsOnly,t.delimWhitespace?(I.assert(t.delimiter==null,()=>"Delimiter should not be provided when delimWhitespace is true."),this.delimWhitespace=!0,this.delimiter=" "):this.delimiter=t.delimiter?t.delimiter:","}async columnNames(){return this.columnNamesValidated||await this.setColumnNames(),this.configuredColumnsOnly?Object.keys(this.columnConfigs):this.fullColumnNames}async setColumnNames(){let e=await this.maybeReadHeaderLine();if(!this.fullColumnNames&&!e)throw new Error("Column names must be provided if there is no header line.");this.fullColumnNames&&e&&I.assert(e.length===this.fullColumnNames.length,()=>"The length of provided columnNames ("+this.fullColumnNames.length.toString()+") does not match the length of the header line read from file ("+e.length.toString()+")."),this.fullColumnNames||(this.fullColumnNames=e);let t=this.fullColumnNames.reduce((a,r)=>(a[r]=a[r]+1||1,a),{}),n=Object.keys(t).filter(a=>t[a]>1);if(I.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let a of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(a)===-1)throw new Error('The key "'+a+'" provided in columnConfigs does not match any of the column names ('+this.fullColumnNames.toString()+").")}this.columnNamesValidated=!0}async maybeReadHeaderLine(){if(this.hasHeader){let e=await(await this.base.iterator()).next();if(e.done)throw new Error("No data was found for CSV parsing.");let t=e.value;return this.parseRow(t,!1)}else return null}async iterator(){this.columnNamesValidated||await this.setColumnNames();let e=await this.base.iterator();return this.hasHeader&&(e=e.skip(1)),e.map(t=>this.makeDataElement(t))}makeDataElement(e){let t=this.parseRow(e),n={},a={};for(let r=0;r<this.fullColumnNames.length;r++){let s=this.fullColumnNames[r],i=this.columnConfigs?this.columnConfigs[s]:null;if(!(this.configuredColumnsOnly&&!i)){let o=t[r],l=null;if(o==="")if(i&&i.default!==void 0)l=i.default;else{if(i&&(i.required||i.isLabel))throw new Error( ` Required column $ { s } is empty in this line : $ { e } ` );l=void 0}else{let u=Number(o);if(isNaN(u))i&&i.dtype==="bool"?l=this.getBoolean(o):l=o;else if(!i||!i.dtype)l=u;else switch(i.dtype){case"float32":l=u;break;case"int32":l=Math.floor(u);break;case"bool":l=this.getBoolean(o);break;default:l=u}}i&&i.isLabel?a[s]=l:n[s]=l}}return Object.keys(a).length===0?n:{xs:n,ys:a}}getBoolean(e){return e==="1"||e.toLowerCase()==="true"?1:0}parseRow(e,t=!0){let n=[],a=0,r=e.length,s=rp;for(let i=0;i<r;i++)switch(s){case rp:switch(e.charAt(i)){case vm:a=i+1,s=wm;break;case this.delimiter:if(a=i+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),s=rp;break;default:s=MN,a=i;break}break;case MN:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(a,i)),s=rp,a=i+1;break;default:}break;case wm:switch(e.charAt(i)){case vm:s=dv;break;default:}break;case dv:switch(e.charAt(i)){case this.delimiter:n.push(e.substring(a,i-1)),s=rp,a=i+1;break;case vm:s=wm;break;default:s=RN;break}break;case RN:switch(e.charAt(i)){case vm:s=wm;break;default:}break;default:}if(s===dv?n.push(e.substring(a,r-1)):n.push(e.substring(a)),t&&n.length!==this.fullColumnNames.length)throw new Error( ` Invalid row in csv file . Should have $ { this . fullColumnNames . length } elements in a row , but got $ { n } ` );return n}},PN=class extends Jt{constructor(e){super();this.microphoneConfig=e,this.isClosed=!1,this.fftSize=e.fftSize||1024;let t=Math.log2(this.fftSize);if(this.fftSize<0||t<4||t>14||!Number.isInteger(t))throw new Error( ` Invalid fftSi
2020-10-12 20:28:19 +02:00
=== === === === === === === === === =
2020-11-25 14:30:53 +01:00
Hi there \ u { 1 F44B } . Looks like you are running TensorFlow . js in Node . js . To speed things up dramatically , install our node backend , which binds to TensorFlow C ++ , by running npm i @ tensorflow / tfjs - node , or npm i @ tensorflow / tfjs - node - gpu if you have CUDA . Then call require ( '@tensorflow/tfjs-node' ) ; ( - gpu suffix for CUDA ) at the start of your program . Visit https : //github.com/tensorflow/tfjs-node for more details.
2021-03-09 23:32:33 +01:00
=== === === === === === === === === = ` ));let a={id:this.nextDataId()};return this.data.set(a,{values:e,dtype:n,refCount:1}),a}makeTensorInfo(e,t,n){let a;if(t==="string"&&n!=null&&n.length>0&&I.isString(n[0])){let r=n.map(s=>I.encodeString(s));a=this.write(r,e,t)}else a=this.write(n,e,t);return{dataId:a,shape:e,dtype:t}}refCount(e){return this.data.has(e)?this.data.get(e).refCount:0}incRef(e){let t=this.data.get(e);t.refCount++}decRef(e){if(this.data.has(e)){let t=this.data.get(e);t.refCount--}}move(e,t,n,a,r){this.data.set(e,{values:t,dtype:a,refCount:r})}numDataIds(){return this.data.numDataIds()}async read(e){return this.readSync(e)}readSync(e){let{dtype:t,complexTensorInfos:n}=this.data.get(e);if(t==="complex64"){let a=this.readSync(n.real.dataId),r=this.readSync(n.imag.dataId);return E.mergeRealAndImagArrays(a,r)}return this.data.get(e).values}bufferSync(e){let t=this.readSync(e.dataId),n=t;if(e.dtype==="string")try{n=t.map(a=>I.decodeString(a))}catch(a){throw new Error("Failed to decode encoded string bytes into utf-8")}return Le(e.shape,e.dtype,n)}makeOutput(e,t,n){let a=this.write(e,t,n);return Ua().makeTensorFromDataId(a,t,n,this)}disposeData(e,t=!1){if(this.data.has(e)){if(this.data.get(e).refCount--,!t&&this.data.get(e).refCount>0)return!1;let{complexTensorInfos:n}=this.data.get(e);n!=null&&(this.disposeData(n.real.dataId,!0),this.disposeData(n.imag.dataId,!0)),this.data.delete(e)}return!0}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}async time(e){let t=I.now();return e(),{kernelMs:I.now()-t}}memory(){return{unreliable:!0,reasons:["The reported memory is an upper bound. Due to automatic garbage collection, the true allocated memory may be less."]}}where(e){ve([e],"where");let t=this.readSync(e.dataId);return ZV(e.shape,t)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}};hv.nextDataId=0;var VN={};Oe(VN,{addImpl:()=>GN,bincountImpl:()=>mv,bincountReduceImpl:()=>HN,ceilImpl:()=>jN,concatImpl:()=>fv,expImpl:()=>qN,expm1Impl:()=>XN,floorImpl:()=>KN,gatherV2Impl:()=>YN,greaterImpl:()=>JN,lessImpl:()=>QN,linSpaceImpl:()=>ZN,logImpl:()=>e2,maxImpl:()=>t2,maximumImpl:()=>n2,minimumImpl:()=>a2,multiplyImpl:()=>gv,negImpl:()=>r2,notEqualImpl:()=>s2,prodImpl:()=>i2,rangeImpl:()=>bv,rsqrtImpl:()=>o2,simpleAbsImpl:()=>UN,sliceImpl:()=>km,squaredDifferenceImpl:()=>l2,stridedSliceImpl:()=>u2,subImpl:()=>c2,tileImpl:()=>p2,topKImpl:()=>d2,transposeImpl:()=>yv,uniqueImpl:()=>h2});function UN(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var eU=e=>{let{x:t}=e.inputs,n=e.backend;ve(t,"abs");let a=new Float32Array(I.sizeFromShape(t.shape)),r=n.data.get(t.dataId).values;return a=UN(r),n.makeOutput(a,t.shape,"float32")},tU={kernelName:So,backendName:"cpu",kernelFunc:eU};function Rt(e){return(t,n,a,r,s)=>{let i=E.assertAndGetBroadcastShape(t,n),o=i.length,l=I.computeStrides(i),u=I.sizeFromShape(i),c=I.getTypedArrayFromDType(s,u),p=t.length,d=n.length,h=I.computeStrides(t),m=I.computeStrides(n),f=E.getBroadcastDims(t,i),g=E.getBroadcastDims(n,i);if(f.length+g.length===0)for(let y=0;y<c.length;++y)c[y]=e(a[y%a.length],r[y%r.length]);else for(let y=0;y<c.length;++y){let b=I.indexToLoc(y,o,l),x=b.slice(-p);f.forEach(S=>x[S]=0);let v=I.locToIndex(x,p,h),k=b.slice(-d);g.forEach(S=>k[S]=0);let w=I.locToIndex(k,d,m);c[y]=e(a[v],r[w])}return[c,i]}}function Un(e){let{inputs:t,backend:n}=e,{real:a,imag:r}=t,s=n.data.get(a.dataId).values,i=n.data.get(r.dataId).values,o=n.makeTensorInfo(a.shape,"complex64"),l=n.data.get(o.dataId);return l.complexTensorInfos={real:n.makeTensorInfo(a.shape,"float32",s),imag:n.makeTensorInfo(r.shape,"float32",i)},o}var nU={kernelName:md,backendName:"cpu",kernelFunc:Un};function Im(e,t,n="float32"){if(n==="complex64"){let r=Im(e,t,"float32"),s=Im(e,t,"float32");return Un({inputs:{real:r,imag:s},backend:e})}let a=I.makeZerosTypedArray(I.sizeFromShape(t),n);return e.makeTensorInfo(t,n,a)}function nr(e){let{inputs:t,backend:n}=e,{x:a}=t;return n.incRef(a.dataId),{dataId:a.dataId,shape:a.shape,dtype:a.dtype}}var aU={kernelName:Bs,backendName:"cp
` ),s=r.length.toString().length+2,i=r.map((p,d)=>I.rightPad((d+1).toString(),s)+p),o=0;for(let p=0;p<i.length;p++)o=Math.max(i[p].length,o);let l=i.slice(0,a-1),u=i.slice(a-1,a),c=i.slice(a);console.log(l.join( `
2021-01-12 16:01:34 +01:00
` )),console.log(t.split( `
2021-03-09 23:32:33 +01:00
` )[0]),console.log( ` % c $ { I . rightPad ( u [ 0 ] , o ) } ` ,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(c.join( `
` ))}function Zq(e){return Nr(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function e5(e,t){if(Ie(e,()=>e.linkProgram(t)),e.getProgramParameter(t,e.LINK_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Failed to link vertex and fragment shaders.")}function _v(e,t){if(Ie(e,()=>e.validateProgram(t)),e.getProgramParameter(t,e.VALIDATE_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Shader program validation failed.")}function t5(e,t){let n=Nr(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return Ie(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),Ie(e,()=>e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function n5(e,t){let n=Nr(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return Ie(e,()=>e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,n)),Ie(e,()=>e.bufferData(e.ELEMENT_ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function a5(e){return Nr(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function r5(e,t){let n=Z().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e<=0||t<=0){let a= ` [ $ { e } x$ { t } ] ` ;throw new Error("Requested texture size "+a+" is invalid.")}if(e>n||t>n){let a= ` [ $ { e } x$ { t } ] ` ,r= ` [ $ { n } x$ { n } ] ` ;throw new Error("Requested texture size "+a+" greater than WebGL maximum on this browser / GPU "+r+".")}}function s5(e){return Nr(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function R2(e,t,n,a,r,s,i){let o=e.getAttribLocation(t,n);return o===-1?!1:(Ie(e,()=>e.bindBuffer(e.ARRAY_BUFFER,a)),Ie(e,()=>e.vertexAttribPointer(o,r,e.FLOAT,!1,s,i)),Ie(e,()=>e.enableVertexAttribArray(o)),!0)}function o5(e,t,n){i5(e,n),Ie(e,()=>e.activeTexture(e.TEXTURE0+n)),Ie(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function l5(e,t,n){return Nr(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function u5(e,t,n){return e.getUniformLocation(t,n)}function c5(e,t,n,a){Ie(e,()=>o5(e,t,a)),Ie(e,()=>e.uniform1i(n,a))}function Ev(e,t,n){Ie(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,n)),Ie(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0))}function P2(e,t){Ie(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,t)),Ie(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0))}function Am(e){let t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+p5(e,t))}function p5(e,t){switch(t){case e.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case e.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return ` unknown error $ { t } ` }}function Nr(e,t,n){let a=Ie(e,()=>t());if(a==null)throw new Error(n);return a}function i5(e,t){let n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,a=t+e.TEXTURE0;if(a<e.TEXTURE0||a>n){let r= ` [ gl . TEXTURE0 , gl . TEXTURE$ { n } ] ` ;throw new Error( ` textureUnit must be in $ { r } . ` )}}function ru(e,t=2){return I.sizeFromShape(e.slice(0,e.length-t))}function su(e){if(e.length===0)throw Error("Cannot get rows and columns of an empty shape array.");return[e.length>1?e[e.length-2]:1,e[e.length-1]]}function $ v(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[ru(e),...su(e)]),t}function d5(e,t=!1){let n=Z().getNumber("WEBGL_MAX_TEXTURE_SIZE");t&&(n=n*2,e=e.map((r,s)=>s>=e.length-2?I.nearestLargerEven(e[s]):e[s]),e.length===1&&(e=[2,e[0]])),e.length!==2&&(e=I.squeezeShape(e).newShape);let a=I.sizeFromShape(e);if(e.length<=1&&a<=n)return[1,a];if(e.length===2&&e[0]<=n&&e[1]<=n)return e;if(e.length===3&&e[0]*e[1]<=n&&e[2]<=n)return[e[0]*e[1],e[2]];if(e.length===3&&e[0]<=n&&e[1]*e[2]<=n)return[e[0],e[1]*e[2]];if(e.length===4&&e[0]*e[1]*e[2]<=n&&e[3]<=n)return[e[0]*e[1]*e[2],e[3]];if(e.length===4&&e[0]<=n&&e[1]*e[2]*e[3]<=n)return[e[0],e[1]*e[2]*e[3]];if(t){let r=ru(e),s=2,i=2;return e.length&&([s,i]=su(e)),a=r*(s/2)*(i/2),I.sizeToSquarishShape(a).map(o=>o*2)}return I.sizeToSquarishShape(a)}function Fm(e){return e%2==0}function _m(e,t){if(e=e.slice(-2),t=t.slice(-2),I.array
2020-10-12 20:28:19 +02:00
bool isnan _custom ( float val ) {
return ( val > 0.0 || val < 0.0 ) ? false : val != 0.0 ;
}
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
bvec4 isnan _custom ( vec4 val ) {
return bvec4 ( isnan _custom ( val . x ) ,
isnan _custom ( val . y ) , isnan _custom ( val . z ) , isnan _custom ( val . w ) ) ;
}
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
# define isnan ( value ) isnan _custom ( value )
2021-03-09 23:32:33 +01:00
` ,l="",u= `
2020-10-12 20:28:19 +02:00
# define round ( value ) newRound ( value )
int newRound ( float value ) {
return int ( floor ( value + 0.5 ) ) ;
}
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
ivec4 newRound ( vec4 value ) {
return ivec4 ( floor ( value + vec4 ( 0.5 ) ) ) ;
}
2021-01-12 16:01:34 +01:00
` ):(e="",t="attribute",n="varying",a="varying",r="texture2D",s="gl_FragColor",i="",o= `
2020-10-12 20:28:19 +02:00
# define isnan ( value ) isnan _custom ( value )
bool isnan _custom ( float val ) {
return ( val > 0. || val < 1. || val == 0. ) ? false : true ;
}
bvec4 isnan _custom ( vec4 val ) {
return bvec4 ( isnan ( val . x ) , isnan ( val . y ) , isnan ( val . z ) , isnan ( val . w ) ) ;
}
2021-01-12 16:01:34 +01:00
` ,l= `
2020-10-12 20:28:19 +02:00
uniform float INFINITY ;
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
bool isinf ( float val ) {
return abs ( val ) == INFINITY ;
}
bvec4 isinf ( vec4 val ) {
return equal ( abs ( val ) , vec4 ( INFINITY ) ) ;
}
2021-03-09 23:32:33 +01:00
` ,u= `
2020-10-12 20:28:19 +02:00
int round ( float value ) {
return int ( floor ( value + 0.5 ) ) ;
}
ivec4 round ( vec4 value ) {
return ivec4 ( floor ( value + vec4 ( 0.5 ) ) ) ;
}
2021-03-09 23:32:33 +01:00
` ),{version:e,attribute:t,varyingVs:n,varyingFs:a,texture2D:r,output:s,defineOutput:i,defineSpecialNaN:o,defineSpecialInf:l,defineRound:u}}function Gi(e,t,n="index"){let a=I.computeStrides(t);return a.map((r,s)=>{let i= ` int $ { e [ s ] } = $ { n } / $ { r } ` ,o=s===a.length-1? ` int $ { e [ s + 1 ] } = $ { n } - $ { e [ s ] } * $ { r } ` : ` index -= $ { e [ s ] } * $ { r } ` ;return ` $ { i } ; $ { o } ; ` }).join("")}function Pv(e){let t=I.computeStrides(e).map(n=>n.toString());return `
2020-10-12 20:28:19 +02:00
int getFlatIndex ( ivec3 coords ) {
2021-01-12 16:01:34 +01:00
return coords . x * $ { t [ 0 ] } + coords . y * $ { t [ 1 ] } + coords . z ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }var L2= `
2020-10-12 20:28:19 +02:00
const float FLOAT _MAX = 1.70141184 e38 ;
const float FLOAT _MIN = 1.17549435 e - 38 ;
lowp vec4 encode _float ( highp float v ) {
if ( isnan ( v ) ) {
return vec4 ( 255 , 255 , 255 , 255 ) ;
}
highp float av = abs ( v ) ;
if ( av < FLOAT _MIN ) {
return vec4 ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
} else if ( v > FLOAT _MAX ) {
return vec4 ( 0.0 , 0.0 , 128.0 , 127.0 ) / 255.0 ;
} else if ( v < - FLOAT _MAX ) {
return vec4 ( 0.0 , 0.0 , 128.0 , 255.0 ) / 255.0 ;
}
highp vec4 c = vec4 ( 0 , 0 , 0 , 0 ) ;
highp float e = floor ( log2 ( av ) ) ;
highp float m = exp2 ( fract ( log2 ( av ) ) ) - 1.0 ;
c [ 2 ] = floor ( 128.0 * m ) ;
m -= c [ 2 ] / 128.0 ;
c [ 1 ] = floor ( 32768.0 * m ) ;
m -= c [ 1 ] / 32768.0 ;
c [ 0 ] = floor ( 8388608.0 * m ) ;
highp float ebias = e + 127.0 ;
c [ 3 ] = floor ( ebias / 2.0 ) ;
ebias -= c [ 3 ] * 2.0 ;
c [ 2 ] += floor ( ebias ) * 128.0 ;
c [ 3 ] += 128.0 * step ( 0.0 , - v ) ;
return c / 255.0 ;
}
2021-03-09 23:32:33 +01:00
` ,v5=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outPackingScheme=op.DENSE;let t=up(e),n=mn();this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
ivec3 outCoordsFromFlatIndex ( int index ) {
2021-02-25 14:12:39 +01:00
$ { Gi ( [ "r" , "c" , "d" ] , e ) }
2020-12-15 14:15:43 +01:00
return ivec3 ( r , c , d ) ;
}
void main ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = 4 * ( resTexRC . x * $ { t [ 1 ] } + resTexRC . y ) ;
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
for ( int i = 0 ; i < 4 ; i ++ ) {
int flatIndex = index + i ;
ivec3 rc = outCoordsFromFlatIndex ( flatIndex ) ;
result [ i ] = getA ( rc . x , rc . y , rc . z ) ;
}
2021-01-12 16:01:34 +01:00
$ { n . output } = result ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }},w5=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=op.DENSE;let t=up(e),n=mn();this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
ivec3 outCoordsFromFlatIndex ( int index ) {
2021-02-25 14:12:39 +01:00
$ { Gi ( [ "r" , "c" , "d" ] , e ) }
2020-12-15 14:15:43 +01:00
return ivec3 ( r , c , d ) ;
}
void main ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = 4 * ( resTexRC . x * $ { t [ 1 ] } + resTexRC . y ) ;
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
for ( int i = 0 ; i < 4 ; i ++ ) {
int flatIndex = index + i ;
ivec3 rc = outCoordsFromFlatIndex ( flatIndex ) ;
result [ i ] = getChannel ( getA ( rc . x , rc . y , rc . z ) , vec2 ( rc . y , rc . z ) ) ;
}
2021-01-12 16:01:34 +01:00
$ { n . output } = result ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }},k5=class{constructor(e){this.variableNames=["A"],this.outTexUsage=na.DOWNLOAD;let t=mn();this.outputShape=e,this.userCode= `
$ { L2 }
2020-12-15 14:15:43 +01:00
void main ( ) {
float x = getAAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { t . output } = encode _float ( x ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }},I5=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=na.DOWNLOAD;let t=mn();this.outputShape=e,this.userCode= `
$ { L2 }
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
float x = getChannel ( getAAtOutCoords ( ) , vec2 ( coords . y , coords . z ) ) ;
2021-01-12 16:01:34 +01:00
$ { t . output } = encode _float ( x ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }},N5=class{constructor(e,t,n=!1){this.variableNames=["A"];let a=mn(),[r,s]=t;this.outputShape=e;let i="result";n&&(i="floor(result * 255. + 0.5)"),this.userCode= `
$ { Pv ( e ) }
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
int flatIndex = getFlatIndex ( coords ) ;
int offset = imod ( flatIndex , 4 ) ;
flatIndex = idiv ( flatIndex , 4 , 1. ) ;
2021-01-12 16:01:34 +01:00
int r = flatIndex / $ { s } ;
int c = imod ( flatIndex , $ { s } ) ;
vec2 uv = ( vec2 ( c , r ) + halfCR ) / vec2 ( $ { s } . 0 , $ { r } . 0 ) ;
vec4 values = $ { a . texture2D } ( A , uv ) ;
2020-12-15 14:15:43 +01:00
float result ;
if ( offset == 0 ) {
result = values [ 0 ] ;
} else if ( offset == 1 ) {
result = values [ 1 ] ;
} else if ( offset == 2 ) {
result = values [ 2 ] ;
} else {
result = values [ 3 ] ;
}
2021-01-12 16:01:34 +01:00
$ { a . output } = vec4 ( $ { i } , 0. , 0. , 0. ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }},T5=class{constructor(e,t,n=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let a=mn(),[r,s]=t;this.outputShape=e;let i="",o="result";n&&(o="floor(result * 255. + 0.5)");for(let l=0;l<=1;l++)for(let u=0;u<=1;u++){let c=l*2+u;i+= `
2020-12-15 14:15:43 +01:00
localCoords = coords ;
2021-03-09 23:32:33 +01:00
if ( localCoords [ 2 ] + $ { u } < $ { e [ 2 ] } ) {
localCoords [ 2 ] += $ { u } ;
2021-01-12 16:01:34 +01:00
if ( localCoords [ 1 ] + $ { l } < $ { e [ 1 ] } ) {
localCoords [ 1 ] += $ { l } ;
2020-12-15 14:15:43 +01:00
flatIndex = getFlatIndex ( localCoords ) ;
offset = imod ( flatIndex , 4 ) ;
flatIndex = idiv ( flatIndex , 4 , 1. ) ;
2021-01-12 16:01:34 +01:00
r = flatIndex / $ { s } ;
c = imod ( flatIndex , $ { s } ) ;
uv = ( vec2 ( c , r ) + halfCR ) / vec2 ( $ { s } . 0 , $ { r } . 0 ) ;
values = $ { a . texture2D } ( A , uv ) ;
2020-12-15 14:15:43 +01:00
if ( offset == 0 ) {
2021-03-09 23:32:33 +01:00
result [ $ { c } ] = values [ 0 ] ;
2020-12-15 14:15:43 +01:00
} else if ( offset == 1 ) {
2021-03-09 23:32:33 +01:00
result [ $ { c } ] = values [ 1 ] ;
2020-12-15 14:15:43 +01:00
} else if ( offset == 2 ) {
2021-03-09 23:32:33 +01:00
result [ $ { c } ] = values [ 2 ] ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
result [ $ { c } ] = values [ 3 ] ;
2020-12-15 14:15:43 +01:00
}
}
}
` }this.userCode= `
2021-03-09 23:32:33 +01:00
$ { Pv ( e ) }
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
int flatIndex , r , c , offset ;
ivec3 localCoords ;
vec2 uv ;
vec4 values ;
2021-01-12 16:01:34 +01:00
$ { i }
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
$ { a . output } = $ { o } ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }};function S5(e){let t=mn(),n= ` $ { t . version }
2020-12-15 14:15:43 +01:00
precision highp float ;
2021-01-12 16:01:34 +01:00
$ { t . attribute } vec3 clipSpacePos ;
$ { t . attribute } vec2 uv ;
$ { t . varyingVs } vec2 resultUV ;
2020-12-15 14:15:43 +01:00
void main ( ) {
gl _Position = vec4 ( clipSpacePos , 1 ) ;
resultUV = uv ;
2021-03-09 23:32:33 +01:00
} ` ;return Kq(e,n)}function C5(e){let t=new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]);return t5(e,t)}function A5(e){let t=new Uint16Array([0,1,2,2,1,3]);return n5(e,t)}function pp(e,t,n,a,r,s){r5(t,n);let i=a5(e),o=e.TEXTURE_2D;return Ie(e,()=>e.bindTexture(o,i)),Ie(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),Ie(e,()=>e.texParameteri(o,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),Ie(e,()=>e.texParameteri(o,e.TEXTURE_MIN_FILTER,e.NEAREST)),Ie(e,()=>e.texParameteri(o,e.TEXTURE_MAG_FILTER,e.NEAREST)),Ie(e,()=>e.texImage2D(o,0,a,t,n,0,r,s,null)),Ie(e,()=>e.bindTexture(e.TEXTURE_2D,null)),i}function z2(e){return e.internalFormatFloat}function F5(e,t,n,a){let[r,s]=lp(t,n);return pp(e,r,s,z2(a),a.textureFormatFloat,e.FLOAT)}function W2(e){return e.internalFormatHalfFloat}function _5(e,t,n,a){let[r,s]=lp(t,n);return pp(e,r,s,W2(a),a.textureFormatFloat,a.textureTypeHalfFloat)}function B2(e){return e.downloadTextureFormat}function E5(e,t,n,a){let[r,s]=lp(t,n);return pp(e,r,s,B2(a),e.RGBA,e.UNSIGNED_BYTE)}function V2(e){return e.internalFormatPackedFloat}function $ 5(e,t,n,a){let[r,s]=au(t,n);return pp(e,r,s,V2(a),e.RGBA,e.FLOAT)}function U2(e){return e.internalFormatPackedHalfFloat}function D5(e,t,n,a){let[r,s]=au(t,n);return pp(e,r,s,U2(a),e.RGBA,a.textureTypeHalfFloat)}function M5(e,t,n){let a=0,r=3*4,s=3*4+2*4;return Ie(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),R2(e,t,"clipSpacePos",n,3,s,a)&&R2(e,t,"uv",n,2,s,r)}function R5(e,t,n,a,r,s){Ie(e,()=>e.bindTexture(e.TEXTURE_2D,t));let i,o,l;r instanceof Uint8Array?(i=new Uint8Array(n*a*4),o=e.UNSIGNED_BYTE,l=e.RGBA):(i=new Float32Array(n*a*4),o=e.FLOAT,l=s.internalFormatPackedFloat),i.set(r),Ie(e,()=>e.texImage2D(e.TEXTURE_2D,0,l,n,a,0,e.RGBA,o,i)),Ie(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function P5(e,t,n){Ie(e,()=>e.bindTexture(e.TEXTURE_2D,t)),n.data instanceof Uint8Array?Ie(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,n.width,n.height,0,e.RGBA,e.UNSIGNED_BYTE,n.data)):Ie(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)),Ie(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function O5(e,t,n,a){let r=e.createBuffer();Ie(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,r));let s=4*4*t*n;return Ie(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,s,e.STREAM_READ)),Ie(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,0)),Ie(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,null)),r}function L5(e,t,n){let a=e,r=new Float32Array(n);return a.bindBuffer(a.PIXEL_PACK_BUFFER,t),a.getBufferSubData(a.PIXEL_PACK_BUFFER,0,r),a.bindBuffer(a.PIXEL_PACK_BUFFER,null),r}function z5(e,t,n,a){let[r,s]=lp(t,n),i=4,o=new Uint8Array(Vq(t*n,i));return Ie(e,()=>e.readPixels(0,0,r,s,a.downloadTextureFormat,e.UNSIGNED_BYTE,o)),new Float32Array(o.buffer)}function W5(e,t,n,a,r,s,i,o){let l=e,u=new Float32Array(Uq(s,i));return l.bindBuffer(l.PIXEL_PACK_BUFFER,t),l.getBufferSubData(l.PIXEL_PACK_BUFFER,0,u),l.bindBuffer(l.PIXEL_PACK_BUFFER,null),u}function B5(e,t,n){let a=new Float32Array(t*n*4);return Ie(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,a)),a}var U5=class{constructor(e){this.outputTexture=null,this.program=null,this.disposed=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[];let t=Z().getNumber("WEBGL_VERSION");e!=null?(this.gl=e,zq(t,e)):this.gl=ar(t);let n="WEBGL_color_buffer_float",a="EXT_color_buffer_half_float";if(Z().getNumber("WEBGL_VERSION")===1){let r="OES_texture_float",s="OES_texture_half_float";if(this.textureFloatExtension=Cm(this.gl,r),ba(this.gl,s))this.textureHalfFloatExtension=Cm(this.gl,s);else if(Z().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support half float textures, yet the environment flag WEBGL_FORCE_F16_TEXTURES is set to true.");if(this.colorBufferFloatExtension=this.gl.getExtension(n),ba(this.gl,a))this.colorBufferHalfFloatExtension=Cm(this.gl,a);else if(Z().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support color renderable half floats, yet the environment flag WEBGL_FORCE_F16_TEXTURES is set to true.")}else if(n="EXT_color_buffer_float",ba(this.gl,n))this.colorBufferFloatExtension=this.gl.getExtension(n);else if(ba(this.gl,a))this.
` ),i=e.map(h=>G5(h,t,a)).join( `
` ),o=t.texShape,l=mn(),u=q5(l),c,p,d=Y5(l);return t.isPacked?(c=H5(t.logicalShape,o),p=K5(l)):(c=j5(t.logicalShape,o),p=X5(l)),a&&(d+=J5),[d,u,p,s,c,i,n].join( `
` )}function iu(e){let t=e.shapeInfo.logicalShape;switch(t.length){case 0:return Z5(e);case 1:return e8(e);case 2:return t8(e);case 3:return n8(e);case 4:return a8(e);case 5:return r8(e);case 6:return s8(e);default:throw new Error( ` $ { t . length } - D input sampling is not yet supported ` )}}function H2(e){switch(e.shapeInfo.logicalShape.length){case 0:return i8(e);case 1:return o8(e);case 2:return l8(e);case 3:return u8(e);default:return c8(e)}}function G5(e,t,n=!1){let a="";n?a+=H2(e):a+=iu(e);let r=e.shapeInfo.logicalShape,s=t.logicalShape;return r.length<=s.length&&(n?a+=p8(e,t):a+=d8(e,t)),a}function H5(e,t){switch(e.length){case 0:return j2();case 1:return h8(e,t);case 2:return g8(e,t);case 3:return m8(e,t);default:return f8(e,t)}}function j5(e,t){switch(e.length){case 0:return j2();case 1:return y8(e,t);case 2:return k8(e,t);case 3:return b8(e,t);case 4:return x8(e,t);case 5:return v8(e,t);case 6:return w8(e,t);default:throw new Error( ` $ { e . length } - D output sampling is not yet supported ` )}}function q5(e){return `
2020-10-12 20:28:19 +02:00
float sampleTexture ( sampler2D textureSampler , vec2 uv ) {
2021-01-12 16:01:34 +01:00
return $ { e . texture2D } ( textureSampler , uv ) . r ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function X5(e){return `
2020-10-12 20:28:19 +02:00
void setOutput ( float val ) {
2021-01-12 16:01:34 +01:00
$ { e . output } = vec4 ( val , 0 , 0 , 0 ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function K5(e){return `
2020-10-12 20:28:19 +02:00
void setOutput ( vec4 val ) {
2021-01-12 16:01:34 +01:00
$ { e . output } = val ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function Y5(e){return ` $ { e . version }
2020-10-12 20:28:19 +02:00
precision highp float ;
precision highp int ;
precision highp sampler2D ;
2021-01-12 16:01:34 +01:00
$ { e . varyingFs } vec2 resultUV ;
$ { e . defineOutput }
2020-10-12 20:28:19 +02:00
const vec2 halfCR = vec2 ( 0.5 , 0.5 ) ;
struct ivec5
{
int x ;
int y ;
int z ;
int w ;
int u ;
} ;
struct ivec6
{
int x ;
int y ;
int z ;
int w ;
int u ;
int v ;
} ;
uniform float NAN ;
2021-01-12 16:01:34 +01:00
$ { e . defineSpecialNaN }
$ { e . defineSpecialInf }
$ { e . defineRound }
2020-10-12 20:28:19 +02:00
int imod ( int x , int y ) {
return x - y * ( x / y ) ;
2020-08-29 20:56:29 +02:00
}
2020-10-12 20:28:19 +02:00
int idiv ( int a , int b , float sign ) {
int res = a / b ;
int mod = imod ( a , b ) ;
if ( sign < 0. && mod != 0 ) {
res -= 1 ;
}
return res ;
}
//Based on the work of Dave Hoskins
//https://www.shadertoy.com/view/4djSRW
# define HASHSCALE1 443.8975
float random ( float seed ) {
vec2 p = resultUV * seed ;
vec3 p3 = fract ( vec3 ( p . xyx ) * HASHSCALE1 ) ;
p3 += dot ( p3 , p3 . yzx + 19.19 ) ;
return fract ( ( p3 . x + p3 . y ) * p3 . z ) ;
}
2021-03-09 23:32:33 +01:00
$ { I8 }
$ { N8 }
$ { T8 }
` }var I8= `
2020-10-12 20:28:19 +02:00
vec2 uvFromFlat ( int texNumR , int texNumC , int index ) {
int texR = index / texNumC ;
int texC = index - texR * texNumC ;
return ( vec2 ( texC , texR ) + halfCR ) / vec2 ( texNumC , texNumR ) ;
}
vec2 packedUVfrom1D ( int texNumR , int texNumC , int index ) {
int texelIndex = index / 2 ;
int texR = texelIndex / texNumC ;
int texC = texelIndex - texR * texNumC ;
return ( vec2 ( texC , texR ) + halfCR ) / vec2 ( texNumC , texNumR ) ;
}
2021-03-09 23:32:33 +01:00
` ,N8= `
2020-10-12 20:28:19 +02:00
vec2 packedUVfrom2D ( int texelsInLogicalRow , int texNumR ,
int texNumC , int row , int col ) {
int texelIndex = ( row / 2 ) * texelsInLogicalRow + ( col / 2 ) ;
int texR = texelIndex / texNumC ;
int texC = texelIndex - texR * texNumC ;
return ( vec2 ( texC , texR ) + halfCR ) / vec2 ( texNumC , texNumR ) ;
}
2021-03-09 23:32:33 +01:00
` ,T8= `
2020-10-12 20:28:19 +02:00
vec2 packedUVfrom3D ( int texNumR , int texNumC ,
int texelsInBatch , int texelsInLogicalRow , int b ,
int row , int col ) {
int index = b * texelsInBatch + ( row / 2 ) * texelsInLogicalRow + ( col / 2 ) ;
int texR = index / texNumC ;
int texC = index - texR * texNumC ;
return ( vec2 ( texC , texR ) + halfCR ) / vec2 ( texNumC , texNumR ) ;
}
2021-03-09 23:32:33 +01:00
` ,J5= `
2020-10-12 20:28:19 +02:00
float getChannel ( vec4 frag , vec2 innerDims ) {
vec2 modCoord = mod ( innerDims , 2. ) ;
return modCoord . x == 0. ?
( modCoord . y == 0. ? frag . r : frag . g ) :
( modCoord . y == 0. ? frag . b : frag . a ) ;
}
float getChannel ( vec4 frag , int dim ) {
float modCoord = mod ( float ( dim ) , 2. ) ;
return modCoord == 0. ? frag . r : frag . g ;
}
2021-03-09 23:32:33 +01:00
` ;function j2(){return `
2020-10-12 20:28:19 +02:00
int getOutputCoords ( ) {
return 0 ;
}
2021-03-09 23:32:33 +01:00
` }function h8(e,t){let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return n[0]===1? `
2020-10-12 20:28:19 +02:00
int getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
return 2 * int ( resultUV . x * $ { n [ 1 ] } . 0 ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` :n[1]===1? `
2020-10-12 20:28:19 +02:00
int getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
return 2 * int ( resultUV . y * $ { n [ 0 ] } . 0 ) ;
2020-10-12 20:28:19 +02:00
}
` : `
int getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
return 2 * ( resTexRC . x * $ { n [ 1 ] } + resTexRC . y ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function y8(e,t){return t[0]===1? `
2020-10-12 20:28:19 +02:00
int getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
return int ( resultUV . x * $ { t [ 1 ] } . 0 ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` :t[1]===1? `
2020-10-12 20:28:19 +02:00
int getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
return int ( resultUV . y * $ { t [ 0 ] } . 0 ) ;
2020-10-12 20:28:19 +02:00
}
` : `
int getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
return resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function m8(e,t){let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],a=Math.ceil(e[2]/2),r=a*Math.ceil(e[1]/2);return `
2020-10-12 20:28:19 +02:00
ivec3 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
int index = resTexRC . x * $ { n [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int b = index / $ { r } ;
index -= b * $ { r } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int r = 2 * ( index / $ { a } ) ;
int c = imod ( index , $ { a } ) * 2 ;
2020-10-12 20:28:19 +02:00
return ivec3 ( b , r , c ) ;
}
2021-03-09 23:32:33 +01:00
` }function b8(e,t){let n=Gi(["r","c","d"],e);return `
2020-10-12 20:28:19 +02:00
ivec3 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
$ { n }
2020-10-12 20:28:19 +02:00
return ivec3 ( r , c , d ) ;
}
2021-03-09 23:32:33 +01:00
` }function f8(e,t){let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],a=Math.ceil(e[e.length-1]/2),r=a*Math.ceil(e[e.length-2]/2),s=r,i="",o="b, r, c";for(let l=2;l<e.length-1;l++)s*=e[e.length-l-1],i= `
2021-01-12 16:01:34 +01:00
int b$ { l } = index / $ { s } ;
index -= b$ { l } * $ { s } ;
` +i,o= ` b$ { l } , ` +o;return `
ivec$ { e . length } getOutputCoords ( ) {
2020-10-12 20:28:19 +02:00
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
int index = resTexRC . x * $ { n [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { i }
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int b = index / $ { r } ;
index -= b * $ { r } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int r = 2 * ( index / $ { a } ) ;
int c = imod ( index , $ { a } ) * 2 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
return ivec$ { e . length } ( $ { o } ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function x8(e,t){let n=Gi(["r","c","d","d2"],e);return `
2020-10-12 20:28:19 +02:00
ivec4 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
$ { n }
2020-10-12 20:28:19 +02:00
return ivec4 ( r , c , d , d2 ) ;
}
2021-03-09 23:32:33 +01:00
` }function v8(e,t){let n=Gi(["r","c","d","d2","d3"],e);return `
2020-10-12 20:28:19 +02:00
ivec5 getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
ivec2 resTexRC = ivec2 ( resultUV . yx * vec2 ( $ { t [ 0 ] } ,
$ { t [ 1 ] } ) ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { n }
2020-10-12 20:28:19 +02:00
ivec5 outShape = ivec5 ( r , c , d , d2 , d3 ) ;
return outShape ;
}
2021-03-09 23:32:33 +01:00
` }function w8(e,t){let n=Gi(["r","c","d","d2","d3","d4"],e);return `
2020-10-12 20:28:19 +02:00
ivec6 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { n }
2020-10-12 20:28:19 +02:00
ivec6 result = ivec6 ( r , c , d , d2 , d3 , d4 ) ;
return result ;
}
2021-03-09 23:32:33 +01:00
` }function g8(e,t){let n=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];if(I.arraysEqual(e,t))return `
2020-10-12 20:28:19 +02:00
ivec2 getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
return 2 * ivec2 ( resultUV . yx * vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` ;let a=Math.ceil(e[1]/2);return `
2020-10-12 20:28:19 +02:00
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { n [ 0 ] } , $ { n [ 1 ] } ) ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int index = resTexRC . x * $ { n [ 1 ] } + resTexRC . y ;
int r = 2 * ( index / $ { a } ) ;
int c = imod ( index , $ { a } ) * 2 ;
2020-10-12 20:28:19 +02:00
return ivec2 ( r , c ) ;
2020-08-29 20:56:29 +02:00
}
2021-03-09 23:32:33 +01:00
` }function k8(e,t){return I.arraysEqual(e,t)? `
2020-10-12 20:28:19 +02:00
ivec2 getOutputCoords ( ) {
2021-01-12 16:01:34 +01:00
return ivec2 ( resultUV . yx * vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` :e[1]===1? `
2020-10-12 20:28:19 +02:00
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
return ivec2 ( index , 0 ) ;
}
2021-01-12 16:01:34 +01:00
` :e[0]===1? `
2020-10-12 20:28:19 +02:00
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
2020-10-12 20:28:19 +02:00
return ivec2 ( 0 , index ) ;
}
` : `
ivec2 getOutputCoords ( ) {
ivec2 resTexRC = ivec2 ( resultUV . yx *
2021-01-12 16:01:34 +01:00
vec2 ( $ { t [ 0 ] } , $ { t [ 1 ] } ) ) ;
int index = resTexRC . x * $ { t [ 1 ] } + resTexRC . y ;
int r = index / $ { e [ 1 ] } ;
int c = index - r * $ { e [ 1 ] } ;
2020-10-12 20:28:19 +02:00
return ivec2 ( r , c ) ;
}
2021-03-09 23:32:33 +01:00
` }function Hi(e){return ` offset$ { e } ` }function i8(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),a=mn();return `
2021-01-12 16:01:34 +01:00
vec4 $ { n } ( ) {
return $ { a . texture2D } ( $ { t } , halfCR ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function Z5(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);if(e.shapeInfo.isUniform)return ` float $ { n } ( ) { return $ { t } ; } ` ;let[a,r]=e.shapeInfo.texShape;if(a===1&&r===1)return `
2021-01-12 16:01:34 +01:00
float $ { n } ( ) {
return sampleTexture ( $ { t } , halfCR ) ;
}
2021-02-25 14:12:39 +01:00
` ;let[s,i]=e.shapeInfo.texShape,o=Hi(t);return `
2021-01-12 16:01:34 +01:00
float $ { n } ( ) {
vec2 uv = uvFromFlat ( $ { s } , $ { i } , $ { o } ) ;
return sampleTexture ( $ { t } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function o8(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),a=e.shapeInfo.texShape,r=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],s=mn();return `
2021-01-12 16:01:34 +01:00
vec4 $ { n } ( int index ) {
2020-10-12 20:28:19 +02:00
vec2 uv = packedUVfrom1D (
2021-01-12 16:01:34 +01:00
$ { r [ 0 ] } , $ { r [ 1 ] } , index ) ;
return $ { s . texture2D } ( $ { t } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function e8(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);if(e.shapeInfo.isUniform)return `
2021-01-12 16:01:34 +01:00
float $ { n } ( int index ) {
2021-02-25 14:12:39 +01:00
$ { ou ( e ) }
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` ;let a=e.shapeInfo.texShape,r=a[0],s=a[1];if(s===1&&r===1)return `
float $ { n } ( int index ) {
return sampleTexture ( $ { t } , halfCR ) ;
2020-10-12 20:28:19 +02:00
}
2021-02-25 14:12:39 +01:00
` ;let i=Hi(t);return s===1? `
2021-01-12 16:01:34 +01:00
float $ { n } ( int index ) {
vec2 uv = vec2 ( 0.5 , ( float ( index + $ { i } ) + 0.5 ) / $ { r } . 0 ) ;
return sampleTexture ( $ { t } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` :r===1? `
float $ { n } ( int index ) {
vec2 uv = vec2 ( ( float ( index + $ { i } ) + 0.5 ) / $ { s } . 0 , 0.5 ) ;
return sampleTexture ( $ { t } , uv ) ;
2020-10-12 20:28:19 +02:00
}
` : `
2021-01-12 16:01:34 +01:00
float $ { n } ( int index ) {
vec2 uv = uvFromFlat ( $ { r } , $ { s } , index + $ { i } ) ;
return sampleTexture ( $ { t } , uv ) ;
2020-08-29 20:56:29 +02:00
}
2021-03-09 23:32:33 +01:00
` }function l8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape,s=r[0],i=r[1],o=mn();if(r!=null&&I.arraysEqual(t,r))return `
2021-01-12 16:01:34 +01:00
vec4 $ { a } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { i } . 0 , $ { s } . 0 ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
return $ { o . texture2D } ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;let l=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)],u=Math.ceil(t[1]/2);return `
2021-01-12 16:01:34 +01:00
vec4 $ { a } ( int row , int col ) {
2021-03-09 23:32:33 +01:00
vec2 uv = packedUVfrom2D ( $ { u } , $ { l [ 0 ] } , $ { l [ 1 ] } , row , col ) ;
2021-01-12 16:01:34 +01:00
return $ { o . texture2D } ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function t8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape;if(r!=null&&I.arraysEqual(t,r)){let p=r[0],d=r[1];return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col ) {
vec2 uv = ( vec2 ( col , row ) + halfCR ) / vec2 ( $ { d } . 0 , $ { p } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }let{newShape:s,keptDims:i}=I.squeezeShape(t),o=s;if(o.length<t.length){let p=lu(e,o),d=["row","col"];return `
2021-02-25 14:12:39 +01:00
$ { iu ( p ) }
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col ) {
2021-02-25 14:12:39 +01:00
return $ { a } ( $ { uu ( d , i ) } ) ;
2021-01-12 16:01:34 +01:00
}
` }if(e.shapeInfo.isUniform)return `
float $ { a } ( int row , int col ) {
int index = round ( dot ( vec2 ( row , col ) , vec2 ( $ { t [ 1 ] } , 1 ) ) ) ;
2021-02-25 14:12:39 +01:00
$ { ou ( e ) }
2021-01-12 16:01:34 +01:00
}
2021-03-09 23:32:33 +01:00
` ;let l=r[0],u=r[1],c=Hi(n);return u===1? `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col ) {
2021-03-09 23:32:33 +01:00
float index = dot ( vec3 ( row , col , $ { c } ) , vec3 ( $ { t [ 1 ] } , 1 , 1 ) ) ;
2021-01-12 16:01:34 +01:00
vec2 uv = vec2 ( 0.5 , ( index + 0.5 ) / $ { l } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` :l===1? `
float $ { a } ( int row , int col ) {
2021-03-09 23:32:33 +01:00
float index = dot ( vec3 ( row , col , $ { c } ) , vec3 ( $ { t [ 1 ] } , 1 , 1 ) ) ;
vec2 uv = vec2 ( ( index + 0.5 ) / $ { u } . 0 , 0.5 ) ;
2021-01-12 16:01:34 +01:00
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
` : `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col ) {
2020-10-12 20:28:19 +02:00
// Explicitly use integer operations as dot() only works on floats.
2021-03-09 23:32:33 +01:00
int index = row * $ { t [ 1 ] } + col + $ { c } ;
vec2 uv = uvFromFlat ( $ { l } , $ { u } , index ) ;
2021-01-12 16:01:34 +01:00
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function u8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=e.shapeInfo.texShape,s=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)];if(t[0]===1){let p=t.slice(1),d=[1,2],h=lu(e,p),m=["b","row","col"];return `
$ { H2 ( h ) }
2021-01-12 16:01:34 +01:00
vec4 $ { a } ( int b , int row , int col ) {
2021-02-25 14:12:39 +01:00
return $ { a } ( $ { uu ( m , d ) } ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }let i=s[0],o=s[1],l=Math.ceil(t[2]/2),u=l*Math.ceil(t[1]/2),c=mn();return `
2021-01-12 16:01:34 +01:00
vec4 $ { a } ( int b , int row , int col ) {
2020-10-12 20:28:19 +02:00
vec2 uv = packedUVfrom3D (
2021-03-09 23:32:33 +01:00
$ { i } , $ { o } , $ { u } , $ { l } , b , row , col ) ;
return $ { c . texture2D } ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function n8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=t[1]*t[2],s=t[2],{newShape:i,keptDims:o}=I.squeezeShape(t),l=i;if(l.length<t.length){let m=lu(e,l),f=["row","col","depth"];return `
2021-02-25 14:12:39 +01:00
$ { iu ( m ) }
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ) {
2021-02-25 14:12:39 +01:00
return $ { a } ( $ { uu ( f , o ) } ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` }if(e.shapeInfo.isUniform)return `
float $ { a } ( int row , int col , int depth ) {
2020-10-12 20:28:19 +02:00
int index = round ( dot ( vec3 ( row , col , depth ) ,
2021-01-12 16:01:34 +01:00
vec3 ( $ { r } , $ { s } , 1 ) ) ) ;
2021-02-25 14:12:39 +01:00
$ { ou ( e ) }
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;let u=e.shapeInfo.texShape,c=u[0],p=u[1],d=e.shapeInfo.flatOffset;if(p===r&&d==null)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ) {
2020-10-12 20:28:19 +02:00
float texR = float ( row ) ;
2021-01-12 16:01:34 +01:00
float texC = dot ( vec2 ( col , depth ) , vec2 ( $ { s } , 1 ) ) ;
2020-10-12 20:28:19 +02:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-03-09 23:32:33 +01:00
vec2 ( $ { p } . 0 , $ { c } . 0 ) ;
2021-01-12 16:01:34 +01:00
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` ;if(p===s&&d==null)return `
float $ { a } ( int row , int col , int depth ) {
float texR = dot ( vec2 ( row , col ) , vec2 ( $ { t [ 1 ] } , 1 ) ) ;
2020-10-12 20:28:19 +02:00
float texC = float ( depth ) ;
2021-03-09 23:32:33 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { p } . 0 , $ { c } . 0 ) ;
2021-01-12 16:01:34 +01:00
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-02-25 14:12:39 +01:00
` ;let h=Hi(n);return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ) {
2020-10-12 20:28:19 +02:00
// Explicitly use integer operations as dot() only works on floats.
2021-01-12 16:01:34 +01:00
int index = row * $ { r } + col * $ { s } + depth + $ { h } ;
2021-03-09 23:32:33 +01:00
vec2 uv = uvFromFlat ( $ { c } , $ { p } , index ) ;
2021-01-12 16:01:34 +01:00
return sampleTexture ( $ { n } , uv ) ;
}
2021-03-09 23:32:33 +01:00
` }function c8(e){let t=e.shapeInfo.logicalShape,n=t.length,a=e.name,r="get"+a.charAt(0).toUpperCase()+a.slice(1),s=e.shapeInfo.texShape,i=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)],o=i[0],l=i[1],u=Math.ceil(t[n-1]/2),c=u*Math.ceil(t[n-2]/2),p="int b, int row, int col",d= ` b * $ { c } + ( row / 2 ) * $ { u } + ( col / 2 ) ` ;for(let m=2;m<n-1;m++)p= ` int b$ { m } , ` +p,c*=t[n-m-1],d= ` b$ { m } * $ { c } + ` +d;let h=mn();return `
2021-01-12 16:01:34 +01:00
vec4 $ { r } ( $ { p } ) {
int index = $ { d } ;
int texR = index / $ { l } ;
int texC = index - texR * $ { l } ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { l } , $ { o } ) ;
return $ { h . texture2D } ( $ { a } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function a8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=t[3],s=t[2]*r,i=t[1]*s,{newShape:o,keptDims:l}=I.squeezeShape(t);if(o.length<t.length){let m=lu(e,o),f=["row","col","depth","depth2"];return `
2021-02-25 14:12:39 +01:00
$ { iu ( m ) }
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 ) {
2021-02-25 14:12:39 +01:00
return $ { a } ( $ { uu ( f , l ) } ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` }if(e.shapeInfo.isUniform)return `
float $ { a } ( int row , int col , int depth , int depth2 ) {
2020-10-12 20:28:19 +02:00
int index = round ( dot ( vec4 ( row , col , depth , depth2 ) ,
2021-01-12 16:01:34 +01:00
vec4 ( $ { i } , $ { s } , $ { r } , 1 ) ) ) ;
2021-02-25 14:12:39 +01:00
$ { ou ( e ) }
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;let u=e.shapeInfo.flatOffset,c=e.shapeInfo.texShape,p=c[0],d=c[1];if(d===i&&u==null)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 ) {
2020-10-12 20:28:19 +02:00
float texR = float ( row ) ;
float texC =
dot ( vec3 ( col , depth , depth2 ) ,
2021-01-12 16:01:34 +01:00
vec3 ( $ { s } , $ { r } , 1 ) ) ;
2020-10-12 20:28:19 +02:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { d } . 0 , $ { p } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;if(d===r&&u==null)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 ) {
2020-10-12 20:28:19 +02:00
float texR = dot ( vec3 ( row , col , depth ) ,
2021-01-12 16:01:34 +01:00
vec3 ( $ { t [ 1 ] * t [ 2 ] } , $ { t [ 2 ] } , 1 ) ) ;
2020-10-12 20:28:19 +02:00
float texC = float ( depth2 ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { d } . 0 , $ { p } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-02-25 14:12:39 +01:00
` ;let h=Hi(n);return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 ) {
2020-10-12 20:28:19 +02:00
// Explicitly use integer operations as dot() only works on floats.
2021-01-12 16:01:34 +01:00
int index = row * $ { i } + col * $ { s } +
depth * $ { r } + depth2 ;
vec2 uv = uvFromFlat ( $ { p } , $ { d } , index + $ { h } ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function r8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),r=t[4],s=t[3]*r,i=t[2]*s,o=t[1]*i,{newShape:l,keptDims:u}=I.squeezeShape(t);if(l.length<t.length){let f=lu(e,l),g=["row","col","depth","depth2","depth3"];return `
2021-02-25 14:12:39 +01:00
$ { iu ( f ) }
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 , int depth3 ) {
2021-03-09 23:32:33 +01:00
return $ { a } ( $ { uu ( g , u ) } ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` }if(e.shapeInfo.isUniform)return `
float $ { a } ( int row , int col , int depth , int depth2 , int depth3 ) {
2020-10-12 20:28:19 +02:00
float index = dot (
vec4 ( row , col , depth , depth2 ) ,
2021-01-12 16:01:34 +01:00
vec4 ( $ { o } , $ { i } , $ { s } , $ { r } ) ) +
2020-10-12 20:28:19 +02:00
depth3 ;
2021-02-25 14:12:39 +01:00
$ { ou ( e ) }
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;let c=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,d=p[0],h=p[1];if(h===o&&c==null)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 , int depth3 ) {
2020-10-12 20:28:19 +02:00
int texR = row ;
float texC = dot ( vec4 ( col , depth , depth2 , depth3 ) ,
2021-01-12 16:01:34 +01:00
vec4 ( $ { i } , $ { s } , $ { r } , 1 ) ) ;
2020-10-12 20:28:19 +02:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { h } . 0 , $ { d } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;if(h===r&&c==null)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 , int depth3 ) {
2020-10-12 20:28:19 +02:00
float texR = dot (
vec4 ( row , col , depth , depth2 ) ,
2021-01-12 16:01:34 +01:00
vec4 ( $ { t [ 1 ] * t [ 2 ] * t [ 3 ] } ,
$ { t [ 2 ] * t [ 3 ] } , $ { t [ 3 ] } , 1 ) ) ;
2020-10-12 20:28:19 +02:00
int texC = depth3 ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { h } . 0 , $ { d } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-02-25 14:12:39 +01:00
` ;let m=Hi(n);return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth , int depth2 , int depth3 ) {
2020-10-12 20:28:19 +02:00
// Explicitly use integer operations as dot() only works on floats.
2021-01-12 16:01:34 +01:00
int index = row * $ { o } + col * $ { i } + depth * $ { s } +
depth2 * $ { r } + depth3 + $ { m } ;
vec2 uv = uvFromFlat ( $ { d } , $ { h } , index ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function s8(e){let t=e.shapeInfo.logicalShape,n=e.name,a="get"+n.charAt(0).toUpperCase()+n.slice(1),{newShape:r,keptDims:s}=I.squeezeShape(t);if(r.length<t.length){let g=lu(e,r),y=["row","col","depth","depth2","depth3","depth4"];return `
2021-02-25 14:12:39 +01:00
$ { iu ( g ) }
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ,
2020-10-12 20:28:19 +02:00
int depth2 , int depth3 , int depth4 ) {
2021-02-25 14:12:39 +01:00
return $ { a } ( $ { uu ( y , s ) } ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }let i=t[5],o=t[4]*i,l=t[3]*o,u=t[2]*l,c=t[1]*u;if(e.shapeInfo.isUniform)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ,
2020-10-12 20:28:19 +02:00
int depth2 , int depth3 , int depth4 ) {
int index = round ( dot (
vec4 ( row , col , depth , depth2 ) ,
2021-03-09 23:32:33 +01:00
vec4 ( $ { c } , $ { u } , $ { l } , $ { o } ) ) +
2020-10-12 20:28:19 +02:00
dot (
vec2 ( depth3 , depth4 ) ,
2021-01-12 16:01:34 +01:00
vec2 ( $ { i } , 1 ) ) ) ;
2021-02-25 14:12:39 +01:00
$ { ou ( e ) }
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;let p=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,h=d[0],m=d[1];if(m===c&&p==null)return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ,
2020-10-12 20:28:19 +02:00
int depth2 , int depth3 , int depth4 ) {
int texR = row ;
float texC = dot ( vec4 ( col , depth , depth2 , depth3 ) ,
2021-03-09 23:32:33 +01:00
vec4 ( $ { u } , $ { l } , $ { o } , $ { i } ) ) +
2020-10-12 20:28:19 +02:00
float ( depth4 ) ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { m } . 0 , $ { h } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` ;if(m===i&&p==null)return `
float $ { a } ( int row , int col , int depth ,
2020-10-12 20:28:19 +02:00
int depth2 , int depth3 , int depth4 ) {
float texR = dot ( vec4 ( row , col , depth , depth2 ) ,
2021-01-12 16:01:34 +01:00
vec4 ( $ { t [ 1 ] * t [ 2 ] * t [ 3 ] * t [ 4 ] } ,
$ { t [ 2 ] * t [ 3 ] * t [ 4 ] } ,
$ { t [ 3 ] * t [ 4 ] } ,
$ { t [ 4 ] } ) ) + float ( depth3 ) ;
2020-10-12 20:28:19 +02:00
int texC = depth4 ;
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { m } . 0 , $ { h } . 0 ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-02-25 14:12:39 +01:00
` ;let f=Hi(n);return `
2021-01-12 16:01:34 +01:00
float $ { a } ( int row , int col , int depth ,
2020-10-12 20:28:19 +02:00
int depth2 , int depth3 , int depth4 ) {
// Explicitly use integer operations as dot() only works on floats.
2021-03-09 23:32:33 +01:00
int index = row * $ { c } + col * $ { u } + depth * $ { l } +
2021-01-12 16:01:34 +01:00
depth2 * $ { o } + depth3 * $ { i } + depth4 + $ { f } ;
vec2 uv = uvFromFlat ( $ { h } , $ { m } , index ) ;
return sampleTexture ( $ { n } , uv ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function ou(e){let t=e.name,n=I.sizeFromShape(e.shapeInfo.logicalShape);return n<2? ` return $ { t } ; ` : `
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { n } ; i ++ ) {
2020-10-12 20:28:19 +02:00
if ( i == index ) {
2021-01-12 16:01:34 +01:00
return $ { t } [ i ] ;
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
}
2021-03-09 23:32:33 +01:00
` }function p8(e,t){let n=e.name,a=n.charAt(0).toUpperCase()+n.slice(1),r="get"+a+"AtOutCoords",s=e.shapeInfo.logicalShape.length,i=t.logicalShape.length,o=G2(e.shapeInfo.logicalShape,t.logicalShape),l=ht(i),u=i-s,c,p=["x","y","z","w","u","v"];s===0?c="":i<2&&o.length>=1?c="coords = 0;":c=o.map(g=> ` coords . $ { p [ g + u ] } = 0 ; ` ).join( `
` );let d="";i<2&&s>0?d="coords":d=e.shapeInfo.logicalShape.map((g,y)=> ` coords . $ { p [ y + u ] } ` ).join(", ");let h="return outputValue;",m=I.sizeFromShape(e.shapeInfo.logicalShape)===1,f=I.sizeFromShape(t.logicalShape)===1;if(s===1&&!m&&!f)h= `
2020-10-12 20:28:19 +02:00
return vec4 ( outputValue . xy , outputValue . xy ) ;
2021-01-12 16:01:34 +01:00
` ;else if(m&&!f)i===1?h= `
2020-10-12 20:28:19 +02:00
return vec4 ( outputValue . x , outputValue . x , 0. , 0. ) ;
2021-01-12 16:01:34 +01:00
` :h= `
2020-10-12 20:28:19 +02:00
return vec4 ( outputValue . x ) ;
2021-01-12 16:01:34 +01:00
` ;else if(o.length){let g=s-2,y=s-1;o.indexOf(g)>-1&&o.indexOf(y)>-1?h="return vec4(outputValue.x);":o.indexOf(g)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":o.indexOf(y)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return `
vec4 $ { r } ( ) {
$ { l } coords = getOutputCoords ( ) ;
2021-03-09 23:32:33 +01:00
$ { c }
2021-01-12 16:01:34 +01:00
vec4 outputValue = get$ { a } ( $ { d } ) ;
$ { h }
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function d8(e,t){let n=e.name,a=n.charAt(0).toUpperCase()+n.slice(1),r="get"+a+"AtOutCoords",s=t.texShape,i=e.shapeInfo.texShape,o=e.shapeInfo.logicalShape.length,l=t.logicalShape.length;if(!e.shapeInfo.isUniform&&o===l&&e.shapeInfo.flatOffset==null&&I.arraysEqual(i,s))return `
2021-01-12 16:01:34 +01:00
float $ { r } ( ) {
return sampleTexture ( $ { n } , resultUV ) ;
}
2021-03-09 23:32:33 +01:00
` ;let u=ht(l),c=G2(e.shapeInfo.logicalShape,t.logicalShape),p=l-o,d,h=["x","y","z","w","u","v"];o===0?d="":l<2&&c.length>=1?d="coords = 0;":d=c.map(f=> ` coords . $ { h [ f + p ] } = 0 ; ` ).join( `
2021-01-12 16:01:34 +01:00
` );let m="";return l<2&&o>0?m="coords":m=e.shapeInfo.logicalShape.map((f,g)=> ` coords . $ { h [ g + p ] } ` ).join(", "), `
float $ { r } ( ) {
2021-03-09 23:32:33 +01:00
$ { u } coords = getOutputCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { d }
return get$ { a } ( $ { m } ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }function ht(e){if(e<=1)return"int";if(e===2)return"ivec2";if(e===3)return"ivec3";if(e===4)return"ivec4";if(e===5)return"ivec5";if(e===6)return"ivec6";throw Error( ` GPU for rank $ { e } is not yet supported ` )}function lu(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function uu(e,t){return t.map(n=>e[n]).join(", ")}function S8(e,t,n,a){let r=t.userCode,s=n.map((h,m)=>{let f={logicalShape:h.shape,texShape:h.isUniform?null:h.texData.texShape,isUniform:h.isUniform,isPacked:h.isUniform?!1:h.texData.isPacked,flatOffset:null};return h.texData!=null&&h.texData.slice!=null&&h.texData.slice.flatOffset>0&&(f.flatOffset=h.texData.slice.flatOffset),{name:t.variableNames[m],shapeInfo:f}}),i=s.map(h=>h.shapeInfo),o={logicalShape:a.shape,texShape:a.texData.texShape,isUniform:!1,isPacked:a.texData.isPacked,flatOffset:null},l=Q5(s,o,r,t.packedInputs),u=e.createProgram(l),c=null,p=e.getUniformLocation(u,"NAN",!1);Z().getNumber("WEBGL_VERSION")===1&&(c=e.getUniformLocation(u,"INFINITY",!1));let d={};for(let h=0;h<t.variableNames.length;h++){let m=t.variableNames[h],f=!1;d[m]=e.getUniformLocation(u,m,f),d[ ` offset$ { m } ` ]=e.getUniformLocation(u, ` offset$ { m } ` ,f)}return{program:t,source:l,webGLProgram:u,uniformLocations:d,inShapeInfos:i,outShapeInfo:o,infLoc:c,nanLoc:p}}function q2(e,t){if(e.length!==t.length)throw Error( ` Binary was compiled with $ { e . length } inputs , but was executed with $ { t . length } inputs ` );e.forEach((n,a)=>{let r=n.logicalShape,s=t[a],i=s.shape;if(!I.arraysEqual(r,i))throw Error( ` Binary was compiled with different shapes than the current args . Shapes $ { r } and $ { i } must match ` );if(n.isUniform&&s.isUniform)return;let o=n.texShape,l=s.isUniform?null:s.texData.texShape;if(!I.arraysEqual(o,l))throw Error( ` Binary was compiled with different texture shapes than the current args . Shape $ { o } and $ { l } must match ` )})}function C8(e,t,n,a,r){q2(t.inShapeInfos,n),q2([t.outShapeInfo],[a]);let s=a.texData.texture,i=a.texData.texShape;a.texData.isPacked?e.setOutputPackedMatrixTexture(s,i[0],i[1]):e.setOutputMatrixTexture(s,i[0],i[1]),e.setProgram(t.webGLProgram),Z().getNumber("WEBGL_VERSION")===1&&t.infLoc!==null&&e.gl.uniform1f(t.infLoc,Infinity),t.nanLoc!==null&&e.gl.uniform1f(t.nanLoc,NaN),n.forEach((o,l)=>{let u=t.program.variableNames[l],c=t.uniformLocations[u],p=t.uniformLocations[ ` offset$ { u } ` ];if(c!=null){if(o.isUniform){if(I.sizeFromShape(o.shape)<2)e.gl.uniform1f(c,o.uniformValues[0]);else{let d=o.uniformValues;d instanceof Float32Array||(d=new Float32Array(d)),e.gl.uniform1fv(c,d)}return}o.texData.slice!=null&&p!=null&&e.gl.uniform1i(p,o.texData.slice.flatOffset),e.setInputMatrixTexture(o.texData.texture,c,l)}}),r!=null&&r(e,t.webGLProgram),e.executeProgram()}function A8(e,t,n){let a="";t.concat(n).forEach(i=>{let o=i.texData!=null&&i.texData.slice!=null&&i.texData.slice.flatOffset>0,l=i.isUniform?"uniform":i.texData.texShape;a+= ` $ { i . shape } _$ { l } _$ { o } ` });let r=e.userCode,s=e.constructor.name;return s+="_"+a+"_"+r,s}var{addImpl:F8,bincountImpl:X2,bincountReduceImpl:_8,ceilImpl:E8,concatImpl: $ 8,expImpl:D8,expm1Impl:M8,floorImpl:R8,gatherV2Impl:P8,greaterImpl:O8,lessImpl:L8,linSpaceImpl:z8,logImpl:W8,maxImpl:B8,maximumImpl:V8,minimumImpl:U8,multiplyImpl:G8,negImpl:H8,prodImpl:j8,rangeImpl:q8,rsqrtImpl:X8,simpleAbsImpl:K2,sliceImpl:K8,stridedSliceImpl:Y8,subImpl:J8,tileImpl:Q8,topKImpl:Z8,transposeImpl:Ov,uniqueImpl:eX}=VN;function Y2(e,t){return["x","y","z","w","u","v"].slice(0,t).map(n=> ` $ { e } . $ { n } ` )}function fn(e,t){return t===1?[e]:Y2(e,t)}function tX(e,t){if(e===1)return"rc";let n="";for(let a=0;a<e;a++)n+=t[a],a<e-1&&(n+=",");return n}var sX=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outputShape=e;let t=e.length;if(t===0)this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
setOutput ( vec4 ( getA ( ) , 0. , 0. , 0. ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;else{let n=fn("rc",t),a=ht(t),r=nX(t,e,n),s=aX(t,e[e.length-1],e[e.length-2],n),i=rX(e,n);this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { a } rc = getOutputCoords ( ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { r } ) {
2020-12-15 14:15:43 +01:00
setOutput ( vec4 ( 0 ) ) ;
} else {
2021-01-12 16:01:34 +01:00
$ { s }
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
setOutput ( vec4 ( $ { i } ) ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }}};function iX(e,t){let n=[];for(let a=0;a<=1;a++)for(let r=0;r<=1;r++){let s= ` $ { a === 0 ? "r" : "rp1" } , $ { r === 0 ? "c" : "cp1" } ` ;for(let i=2;i<e;i++)s= ` $ { t [ t . length - 1 - i ] } , ` +s;n.push(s)}return n}function nX(e,t,n){if(e===1)return ` rc > $ { t [ 0 ] } ` ;let a="";for(let r=e-2;r<e;r++)a+= ` $ { n [ r ] } >= $ { t [ r ] } ` ,r<e-1&&(a+="||");return a}function aX(e,t,n,a){if(e===1)return"";let r=a.slice(-2);return `
2021-01-12 16:01:34 +01:00
int r = $ { r [ 0 ] } ;
int c = $ { r [ 1 ] } ;
2020-12-15 14:15:43 +01:00
int rp1 = r + 1 ;
int cp1 = c + 1 ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
bool cEdge = cp1 >= $ { t } ;
bool rEdge = rp1 >= $ { n } ;
2021-03-09 23:32:33 +01:00
` }function rX(e,t){let n=e.length,a=iX(n,t);return n===1? ` getA ( rc ) ,
2021-01-12 16:01:34 +01:00
rc + 1 >= $ { e [ 0 ] } ? 0. : getA ( rc + 1 ) ,
0 , 0 ` : ` getA ( $ { a [ 0 ] } ) ,
cEdge ? 0. : getA ( $ { a [ 1 ] } ) ,
rEdge ? 0. : getA ( $ { a [ 2 ] } ) ,
2021-03-09 23:32:33 +01:00
rEdge || cEdge ? 0. : getA ( $ { a [ 3 ] } ) ` }var J2=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e;let n="";for(let a=0;a<4;a++){let r="thisRC = rc;";a%2==1&&(r+="thisRC.z += 1;"),a>1&&(r+="thisRC.y += 1;"),n+= `
2021-01-12 16:01:34 +01:00
$ { r }
$ { a > 0 ? "if(thisRC.y < rows && thisRC.z < cols){" : "" }
2020-12-15 14:15:43 +01:00
int flatIndex = getFlatIndex ( thisRC ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
ivec3 inputRC = inputCoordsFromReshapedOutCoords ( flatIndex ) ;
vec2 inputRCInnerDims = vec2 ( float ( inputRC . y ) , float ( inputRC . z ) ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
result [ $ { a } ] =
2020-12-15 14:15:43 +01:00
getChannel ( getA ( inputRC . x , inputRC . y , inputRC . z ) , inputRCInnerDims ) ;
2021-01-12 16:01:34 +01:00
$ { a > 0 ? "}" : "" }
2020-12-15 14:15:43 +01:00
` }this.userCode= `
2021-03-09 23:32:33 +01:00
$ { oX ( t ) }
$ { Pv ( e ) }
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec3 rc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
ivec3 thisRC ;
2021-01-12 16:01:34 +01:00
int rows = $ { e [ 1 ] } ;
int cols = $ { e [ 2 ] } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
$ { n }
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }};function oX(e){return `
2020-12-15 14:15:43 +01:00
ivec3 inputCoordsFromReshapedOutCoords ( int index ) {
2021-02-25 14:12:39 +01:00
$ { Gi ( [ "r" , "c" , "d" ] , e ) }
2020-12-15 14:15:43 +01:00
return ivec3 ( r , c , d ) ;
}
2021-03-09 23:32:33 +01:00
` }var lX=class{constructor(e){this.gpgpu=e,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}acquireTexture(e,t,n){let a=Z2(t,n),r=eT(e,a,n);r in this.freeTextures||(this.freeTextures[r]=[]),r in this.usedTextures||(this.usedTextures[r]=[]);let s=Q2(e,a,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[r].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=s,this.log();let o=this.freeTextures[r].shift();return this.usedTextures[r].push(o),o}let i;return a===rn.PACKED_2X2_FLOAT32?i=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):a===rn.PACKED_2X2_FLOAT16?i=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):a===rn.UNPACKED_FLOAT32?i=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):a===rn.UNPACKED_FLOAT16?i=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):a===rn.PACKED_4X1_UNSIGNED_BYTE&&(i=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[r].push(i),this.numUsedTextures++,this._numBytesAllocated+=s,this.log(),i}releaseTexture(e,t,n,a){if(this.freeTextures==null)return;let r=Z2(n,a),s=eT(t,r,a);s in this.freeTextures||(this.freeTextures[s]=[]);let i=Q2(t,r,this.gpgpu.gl,this.gpgpu.textureConfig,a),o=Z().get("WEBGL_DELETE_TEXTURE_THRESHOLD");o!==-1&&this._numBytesAllocated>o?(this.gpgpu.deleteMatrixTexture(e),this._numBytesAllocated-=i):(this.freeTextures[s].push(e),this.numFreeTextures++,this._numBytesFree+=i),this.numUsedTextures--;let l=this.usedTextures[s],u=l.indexOf(e);if(u<0)throw new Error("Cannot release a texture that was never provided by this texture manager");l.splice(u,1),this.log()}log(){if(!this.logEnabled)return;let e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used", ` $ { this . numFreeTextures } / $ { this . numUsedTextures } ` , ` ( $ { e } ) ` );let t=this._numBytesFree/this._numBytesAllocated;console.log( ` Bytes allocated : $ { this . _numBytesAllocated } ` ),console.log( ` Bytes unused : $ { this . _numBytesFree } ( $ { Math . round ( 100 * t ) } % ) ` )}get numBytesAllocated(){return this._numBytesAllocated}get numBytesFree(){return this._numBytesFree}getNumUsedTextures(){return this.numUsedTextures}getNumFreeTextures(){return this.numFreeTextures}dispose(){if(this.freeTextures!=null){for(let e in this.freeTextures)this.freeTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t)});for(let e in this.usedTextures)this.usedTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0}}};function uX(e,t){let n=e;if(t===n.R32F)return 4;if(t===n.R16F)return 2;if(t===n.RGBA32F||t===e.RGBA)return 16;if(t===n.RGBA16F)return 8;throw new Error( ` Unknown internal format $ { t } ` )}function Q2(e,t,n,a,r){let s=cX(t,a),i;if(r){let[l,u]=au(e[0],e[1]);i=l*u}else{let[l,u]=lp(e[0],e[1]);i=l*u}let o=uX(n,s);return i*o}function cX(e,t){switch(e){case rn.PACKED_2X2_FLOAT32:return V2(t);case rn.PACKED_2X2_FLOAT16:return U2(t);case rn.UNPACKED_FLOAT32:return z2(t);case rn.UNPACKED_FLOAT16:return W2(t);case rn.PACKED_4X1_UNSIGNED_BYTE:return B2(t);default:throw new Error( ` Unknown physical texture type $ { e } ` )}}function pX(e){return Z().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?rn.PACKED_2X2_FLOAT32:rn.UNPACKED_FLOAT32:e?rn.PACKED_2X2_FLOAT16:rn.UNPACKED_FLOAT16}function Z2(e,t){if(e===na.UPLOAD)return rn.PACKED_2X2_FLOAT32;if(e===na.RENDER||e==null)return pX(t);if(e===na.DOWNLOAD||e===na.PIXELS)return rn.PACKED_4X1_UNSIGNED_BYTE;throw new Error( ` Unknown logical texture type $ { e } ` )}function eT(e,t,n){return ` $ { e [ 0 ] } _$ { e [ 1 ] } _$ { t } _$ { n } ` }var ds=class{constructor(e,t){this.variableNames=["A"],this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
float unaryOperation ( float x ) {
2021-01-12 16:01:34 +01:00
$ { t }
2020-10-12 20:28:19 +02:00
}
void main ( ) {
2020-12-15 14:15:43 +01:00
float x = getAAtOutCoords ( ) ;
float y = unaryOperation ( x ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( y ) ;
}
2021-03-09 23:32:33 +01:00
` }},Ra="if (isnan(x)) return x;",dX="return x;",tT="return abs(x);",hX="return (x >= 0.0) ? x : (exp(x) - 1.0);",mX=Ra+ `
2020-12-15 14:15:43 +01:00
return ( x < 0.0 ) ? 0.0 : x ;
2021-03-09 23:32:33 +01:00
` ,fX=Ra+ `
2020-12-15 14:15:43 +01:00
return ( x < 0.0 ) ? 0.0 : min ( 6.0 , x ) ;
2021-03-09 23:32:33 +01:00
` ,Em="return x;",gX="return x;",yX= `
2020-12-15 14:15:43 +01:00
vec4 result ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,bX= `
2020-12-15 14:15:43 +01:00
vec4 result = x * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
bvec4 isNaN = isnan ( x ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,xX= `
2020-12-15 14:15:43 +01:00
vec4 result = min ( x , vec4 ( 6. ) ) * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
bvec4 isNaN = isnan ( x ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
return result ;
2021-02-25 14:12:39 +01:00
` ,cu=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
vec4 unaryOperation ( vec4 x ) {
2021-01-12 16:01:34 +01:00
$ { t }
2020-12-15 14:15:43 +01:00
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
vec4 x = getAAtOutCoords ( ) ;
vec4 y = unaryOperation ( x ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( y ) ;
}
2021-03-09 23:32:33 +01:00
` }},vX=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=e;let t=e.length,n=fn("rc",t),a=ht(t),r=tX(t,n),s=n.slice(-2),i=t<=1?"rc": ` vec2 ( $ { s . join ( "," ) } ) ` ;this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { a } rc = getOutputCoords ( ) ;
vec4 packedInput = getA ( $ { r } ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
setOutput ( getChannel ( packedInput , $ { i } ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},wX=Ya.whereImpl,kX=1e-7,IX=1e-4,Lv={};function NX(e){return e in Lv||(Lv[e]={}),Lv[e]}var TX=128,SX=600;function CX(){return Z().global.screen==null?1024:Z().global.screen.height*Z().global.screen.width*window.devicePixelRatio*SX/1024/1024}var zv=class extends ju{constructor(e){super();if(this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.lastGlFlushTime=0,this.warnedAboutMemory=!1,this.warnedAboutCPUBackend=!1,this.pendingDeletes=0,this.disposed=!1,!Z().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");if(e==null){let t=ar(Z().getNumber("WEBGL_VERSION"));this.binaryCache=NX(Z().getNumber("WEBGL_VERSION")),this.gpgpu=new U5(t),this.canvas=t.canvas,this.gpgpuCreatedLocally=!0}else this.gpgpu=e,this.binaryCache={},this.gpgpuCreatedLocally=!1,this.canvas=e.gl.canvas;this.textureManager=new lX(this.gpgpu),this.numMBBeforeWarning=CX(),this.texData=new sd(this,Ua())}nextDataId(){return zv.nextDataId++}numDataIds(){return this.texData.numDataIds()+(this.cpuBackend?this.cpuBackend.numDataIds():0)-this.pendingDeletes}write(e,t,n){if((Z().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||Z().getBool("DEBUG"))&&this.checkNumericalProblems(e),n==="complex64"&&e!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");let a={id:this.nextDataId()};return this.texData.set(a,{shape:t,dtype:n,values:e,usage:na.UPLOAD,refCount:1}),a}refCount(e){return this.texData.has(e)?this.texData.get(e).refCount:0}incRef(e){let t=this.texData.get(e);t.refCount++}decRef(e){if(this.texData.has(e)){let t=this.texData.get(e);t.refCount--}}move(e,t,n,a,r){if(Z().getBool("DEBUG")&&this.checkNumericalProblems(t),a==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(e,{shape:n,dtype:a,values:t,usage:na.UPLOAD,refCount:r})}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}readSync(e){let t=this.texData.get(e),{values:n,dtype:a,complexTensorInfos:r,slice:s,shape:i,isPacked:o}=t;if(s!=null){let p;o?p=new cu(i,Em):p=new ds(i,Em);let d=this.runWebGLProgram(p,[{dataId:e,shape:i,dtype:a}],a),h=this.readSync(d.dataId);return this.disposeIntermediateTensorInfo(d),h}if(n!=null)return this.convertAndCacheOnCPU(e);if(a==="string")return n;let l=this.activeTimers!=null,u;l&&(u=I.now());let c;if(a==="complex64"){let p=this.readSync(r.real.dataId),d=this.readSync(r.imag.dataId);c=E.mergeRealAndImagArrays(p,d)}else c=this.getValuesFromTexture(e);return l&&(this.downloadWaitMs+=I.now()-u),this.convertAndCacheOnCPU(e,c)}async read(e){if(this.pendingRead.has(e)){let h=this.pendingRead.get(e);return new Promise(m=>h.push(m))}let t=this.texData.get(e),{values:n,shape:a,slice:r,dtype:s,complexTensorInfos:i,isPacked:o}=t;if(r!=null){let h;o?h=new cu(a,Em):h=new ds(a,Em);let m=this.runWebGLProgram(h,[{dataId:e,shape:a,dtype:s}],s),f=this.read(m.dataId);return this.disposeIntermediateTensorInfo(m),f}if(n!=null)return this.convertAndCacheOnCPU(e);if(!Z().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&Z().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,u;if(s!=="complex64"&&Z().get("WEBGL_BUFFER_SUPPORTED")){u=this.decode(e);let h=this.texData.get(u.dataId);l=this.gpgpu.createBufferFromTexture(h.texture,...up(a))}this.pendingRead.set(e,[]),s!=="complex64"&&await this.gpgpu.createAndWaitForFence();let c;if(s==="complex64"){let h=await Promise.all([this.read(i.real.dataId),this.read(i.imag.dataId)]),m=h[0],f=h[1];c=E.mergeRealAndImagArrays(m,f)}else if(l==null)c=this.getValuesFromTexture(e);else{let h=I.sizeFromShape(a);c=this.gpgpu.downloadFloat32MatrixFromBuffer(l,h)}u!=null&&this.disposeIntermediateTensorInfo(u);let p=this.convertAndCacheOnCPU(e,c),d=this.pendingRead.get(e);return this.pendingRead.delete(e),d.forEach(h=>h(p)),this.pendingDisposal.has(e)&&(this.pendingDisposal.delete(e),this.disposeData(e)&&Ua().removeDataId(e,this),this
2020-10-12 20:28:19 +02:00
if ( isnan ( a ) ) return a ;
if ( isnan ( b ) ) return b ;
2021-03-09 23:32:33 +01:00
` ,pu=class{constructor(e,t,n){this.variableNames=["A","B"],this.outputShape=E.assertAndGetBroadcastShape(t,n),this.userCode= `
2020-10-12 20:28:19 +02:00
float binaryOperation ( float a , float b ) {
2021-01-12 16:01:34 +01:00
$ { e }
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
float a = getAAtOutCoords ( ) ;
float b = getBAtOutCoords ( ) ;
setOutput ( binaryOperation ( a , b ) ) ;
}
2021-03-09 23:32:33 +01:00
` }}, $ m= `
2020-10-12 20:28:19 +02:00
result . r = isNaN . r > 0. ? NAN : result . r ;
result . g = isNaN . g > 0. ? NAN : result . g ;
result . b = isNaN . b > 0. ? NAN : result . b ;
result . a = isNaN . a > 0. ? NAN : result . a ;
2021-03-09 23:32:33 +01:00
` ,dp=class{constructor(e,t,n,a=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=E.assertAndGetBroadcastShape(t,n);let r=this.outputShape.length,s="";if(a)if(r===0||I.sizeFromShape(this.outputShape)===1)s= `
2020-10-12 20:28:19 +02:00
result . y = 0. ;
result . z = 0. ;
result . w = 0. ;
2021-01-12 16:01:34 +01:00
` ;else if(s= `
2021-03-09 23:32:33 +01:00
$ { ht ( r ) } coords = getOutputCoords ( ) ;
2021-01-12 16:01:34 +01:00
` ,r===1)s+= `
2020-10-12 20:28:19 +02:00
result . y = ( coords + 1 ) >= $ { this . outputShape [ 0 ] } ? 0. : result . y ;
result . z = 0. ;
result . w = 0. ;
2021-02-25 14:12:39 +01:00
` ;else{let i=fn("coords",r);s+= `
2020-10-12 20:28:19 +02:00
bool nextRowOutOfBounds =
2021-01-12 16:01:34 +01:00
( $ { i [ r - 2 ] } + 1 ) >= $ { this . outputShape [ r - 2 ] } ;
2020-10-12 20:28:19 +02:00
bool nextColOutOfBounds =
2021-01-12 16:01:34 +01:00
( $ { i [ r - 1 ] } + 1 ) >= $ { this . outputShape [ r - 1 ] } ;
2020-10-12 20:28:19 +02:00
result . y = nextColOutOfBounds ? 0. : result . y ;
result . z = nextRowOutOfBounds ? 0. : result . z ;
result . w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result . w ;
2021-01-03 17:05:09 +01:00
` }this.userCode= `
2020-10-12 20:28:19 +02:00
vec4 binaryOperation ( vec4 a , vec4 b ) {
2021-01-12 16:01:34 +01:00
$ { e }
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
vec4 a = getAAtOutCoords ( ) ;
vec4 b = getBAtOutCoords ( ) ;
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
vec4 result = binaryOperation ( a , b ) ;
2021-01-12 16:01:34 +01:00
$ { s }
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }};function Gn(e){let{inputs:t,backend:n}=e,{x:a}=t;return n.incRef(a.dataId),{dataId:a.dataId,shape:a.shape,dtype:a.dtype}}var _X={kernelName:Bs,backendName:"webgl",kernelFunc:Gn};function hs(e){let{inputs:t,backend:n}=e,{real:a,imag:r}=t,s=n.makeTensorInfo(a.shape,"complex64"),i=n.texData.get(s.dataId),o=Gn({inputs:{x:a},backend:n}),l=Gn({inputs:{x:r},backend:n});return i.complexTensorInfos={real:o,imag:l},s}var EX={kernelName:md,backendName:"webgl",kernelFunc:hs},aT="return (a < 0.) ? b * a : a;",rT= `
2020-12-15 14:15:43 +01:00
vec4 aLessThanZero = vec4 ( lessThan ( a , vec4 ( 0. ) ) ) ;
return ( aLessThanZero * ( b * a ) ) + ( ( vec4 ( 1.0 ) - aLessThanZero ) * a ) ;
2021-03-09 23:32:33 +01:00
` ;function $ X(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{alpha:s}=a,i=n.makeTensorInfo([],"float32",I.createScalarValue(s,"float32")),o=Z().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new dp(rT,r.shape,i.shape):new pu(aT,r.shape,i.shape),l=n.runWebGLProgram(o,[r,i],r.dtype);return n.disposeIntermediateTensorInfo(i),l}var DX={kernelName:Vs,backendName:"webgl",kernelFunc: $ X},sT="return (a < 0.) ? b * a : a;",iT= `
2020-12-15 14:15:43 +01:00
vec4 aLessThanZero = vec4 ( lessThan ( a , vec4 ( 0. ) ) ) ;
return ( aLessThanZero * ( b * a ) ) + ( ( vec4 ( 1.0 ) - aLessThanZero ) * a ) ;
2021-03-09 23:32:33 +01:00
` ;function MX(e){let{inputs:t,backend:n}=e,{x:a,alpha:r}=t,s=Z().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new dp(iT,a.shape,r.shape):new pu(sT,a.shape,r.shape);return n.runWebGLProgram(s,[a,r],a.dtype)}var RX={kernelName:ei,backendName:"webgl",kernelFunc:MX},oT="if (isnan(x)) return x;",PX= `
2020-12-15 14:15:43 +01:00
if ( isnan ( a ) ) return a ;
if ( isnan ( b ) ) return b ;
2021-03-09 23:32:33 +01:00
` ,OX= `
2020-12-15 14:15:43 +01:00
result . r = isNaN . r > 0. ? NAN : result . r ;
result . g = isNaN . g > 0. ? NAN : result . g ;
result . b = isNaN . b > 0. ? NAN : result . b ;
result . a = isNaN . a > 0. ? NAN : result . a ;
2021-03-09 23:32:33 +01:00
` ;function Xe({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:a}){return({inputs:r,backend:s})=>{let{x:i}=r,o=s,l=a||i.dtype;if(o.shouldExecuteOnCPU([i])&&n!=null){let p=o.texData.get(i.dataId),d=n(p.values,l);return o.makeTensorInfo(i.shape,l,d)}let u=Z().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,c;return u?c=new cu(i.shape,t):c=new ds(i.shape,e),o.runWebGLProgram(c,[i],l)}}function sn({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:a=!1,cpuKernelImpl:r,dtype:s}){return({inputs:i,backend:o})=>{let{a:l,b:u}=i,c=o;if(a&&l.dtype==="complex64"){let m=c.texData.get(l.dataId),f=c.texData.get(u.dataId),[g,y]=[[m.complexTensorInfos.real,f.complexTensorInfos.real],[m.complexTensorInfos.imag,f.complexTensorInfos.imag]].map(x=>{let[v,k]=x,w={dataId:v.dataId,dtype:v.dtype,shape:l.shape},S={dataId:k.dataId,dtype:k.dtype,shape:u.shape},A=new pu(e,l.shape,u.shape);return c.runWebGLProgram(A,[w,S],ua(v.dtype,k.dtype))}),b=hs({inputs:{real:g,imag:y},backend:c});return c.disposeIntermediateTensorInfo(g),c.disposeIntermediateTensorInfo(y),b}let p=s||ua(l.dtype,u.dtype);if(c.shouldExecuteOnCPU([l,u])&&r!=null){let m=c.texData.get(l.dataId),f=c.texData.get(u.dataId),[g,y]=r(l.shape,u.shape,m.values,f.values,p),b=c.makeTensorInfo(y,p),x=c.texData.get(b.dataId);return x.values=g,b}let d=Z().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,h;return d?h=new dp(t,l.shape,u.shape,n):h=new pu(e,l.shape,u.shape),c.runWebGLProgram(h,[l,u],p)}}function Dm(e,t=!1){if(e==="linear")return t?gX:dX;if(e==="relu")return t?bX:mX;if(e==="elu")return t?yX:hX;if(e==="relu6")return t?xX:fX;if(e==="prelu")return t?iT:sT;if(e==="leakyrelu")return t?rT:aT;throw new Error( ` Activation $ { e } has not been implemented for the WebGL backend . ` )}var lT=class{constructor(e,t,n,a=!1,r=!1,s=!1,i=null,o=!1,l=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n;let u=a?e[1]:e[2],c=Math.ceil(u/2),p=a?"i * 2, rc.y":"rc.y, i * 2",d=r?"rc.z, i * 2":"i * 2, rc.z",h=a?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],m=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],f="",g="";i&&(o?f= ` vec4 activation ( vec4 a ) {
2020-12-15 14:15:43 +01:00
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { i }
} ` :l?f= ` vec4 activation ( vec4 a ) {
2020-12-15 14:15:43 +01:00
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { i }
} ` :f= ` vec4 activation ( vec4 x ) {
$ { i }
2021-02-25 14:12:39 +01:00
} ` ,g="result = activation(result);");let y=s?"result += getBiasAtOutCoords();":"";s&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),l&&this.variableNames.push("leakyreluAlpha");let b="rc.x",x="rc.x";e[0]<t[0]?b= ` int ( min ( float ( rc . x ) , $ { e [ 0 ] - 1 } . ) ) ` :t[0]<e[0]&&(x= ` int ( min ( float ( rc . x ) , $ { t [ 0 ] - 1 } . ) ) ` ),this.userCode= `
2021-01-12 16:01:34 +01:00
$ { f }
2020-08-29 20:56:29 +02:00
2021-03-09 23:32:33 +01:00
const float sharedDimension = $ { c } . 0 ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
vec4 dot2x2ARowBCol ( ivec3 rc ) {
vec4 result = vec4 ( 0 ) ;
2021-03-09 23:32:33 +01:00
for ( int i = 0 ; i < $ { c } ; i ++ ) {
2021-01-12 16:01:34 +01:00
int batchA = $ { b } ;
2021-02-25 14:12:39 +01:00
int batchB = $ { x } ;
2021-01-12 16:01:34 +01:00
vec4 a = getMatrixA ( batchA , $ { p } ) ;
vec4 b = getMatrixB ( batchB , $ { d } ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
// These swizzled products need to be separately added.
// See: https://github.com/tensorflow/tfjs/issues/1735
2021-01-12 16:01:34 +01:00
result += ( $ { h [ 0 ] } * $ { m [ 0 ] } ) ;
result += ( $ { h [ 1 ] } * $ { m [ 1 ] } ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
return result ;
2020-10-12 20:28:19 +02:00
}
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec3 rc = getOutputCoords ( ) ;
vec4 result = dot2x2ARowBCol ( rc ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { y }
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { g }
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }},uT={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},cT=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=E.assertAndGetBroadcastShape(t,n),this.userCode= `
2020-12-15 14:15:43 +01:00
float binaryOpComplex (
float areal , float aimag , float breal , float bimag ) {
2021-01-12 16:01:34 +01:00
$ { e }
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
float areal = getARealAtOutCoords ( ) ;
float aimag = getAImagAtOutCoords ( ) ;
float breal = getBRealAtOutCoords ( ) ;
float bimag = getBImagAtOutCoords ( ) ;
setOutput ( binaryOpComplex ( areal , aimag , breal , bimag ) ) ;
}
2021-03-09 23:32:33 +01:00
` }},pT="return a * b;";function dT(e){let{inputs:t,backend:n}=e,{a,b:r}=t,s=E.upcastType(a.dtype,r.dtype);if(a.dtype==="complex64"){let o=n.texData.get(a.dataId),l=n.texData.get(r.dataId),u=new cT(uT.REAL,a.shape,r.shape),c=new cT(uT.IMAG,a.shape,r.shape),p=[{dataId:o.complexTensorInfos.real.dataId,dtype:o.complexTensorInfos.real.dtype,shape:a.shape},{dataId:o.complexTensorInfos.imag.dataId,dtype:o.complexTensorInfos.imag.dtype,shape:a.shape},{dataId:l.complexTensorInfos.real.dataId,dtype:l.complexTensorInfos.real.dtype,shape:r.shape},{dataId:l.complexTensorInfos.imag.dataId,dtype:l.complexTensorInfos.imag.dtype,shape:r.shape}],d=n.runWebGLProgram(u,p,"float32"),h=n.runWebGLProgram(c,p,"float32"),m=hs({inputs:{real:d,imag:h},backend:n});return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),m}if(n.shouldExecuteOnCPU([a,r])){let o=n.texData.get(a.dataId),l=n.texData.get(r.dataId),[u,c]=G8(a.shape,r.shape,o.values,l.values,s),p=n.makeTensorInfo(c,s),d=n.texData.get(p.dataId);return d.values=u,p}let i;return Z().getBool("WEBGL_PACK_BINARY_OPERATIONS")?i=new dp(pT,a.shape,r.shape):i=new pu(pT,a.shape,r.shape),n.runWebGLProgram(i,[a,r],s)}var LX={kernelName:Ys,backendName:"webgl",kernelFunc:dT};function zX(e,t,n){let a=[ru(e.shape),...su(e.shape)],r={dtype:e.dtype,shape:a,dataId:e.dataId},s=[ru(t),...su(t)],i=new J2(s,a),o=!0,l=n.runWebGLProgram(i,[r],e.dtype,null,o);return{dataId:l.dataId,shape:t,dtype:l.dtype}}function ye(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{shape:s}=a,i=n,o=I.sizeFromShape(r.shape),l=I.inferFromImplicitShape(s,o),u=I.sizeFromShape(l);I.assert(o===u,()=> ` The new shape ( $ { l } ) has $ { u } elements and the old shape ( $ { r . shape } ) has $ { o } elements . The new shape and old shape must have the same number of elements . ` );let c=i.texData.get(r.dataId);return c.isPacked&&!_m(r.shape,l)&&!(c.texture!==null&&_m(c.shape,l))?zX(r,l,i):(i.incRef(r.dataId),{dataId:r.dataId,shape:l,dtype:r.dtype})}var WX={kernelName:cl,backendName:"webgl",kernelFunc:ye},hT=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:a,inSize:r,outSize:s}=e;this.outputShape=[a,s];let i=Math.floor(n/4)*4,o=n%4,l="sumValue += dot(values, ones);";if(t!=null){let c=1/t;l= ` sumValue += dot ( values * $ { I . isInt ( c ) ? c . toPrecision ( 2 ) : c } , ones ) ; ` }let u="";r%n>0&&(u= `
2021-01-12 16:01:34 +01:00
if ( inIdx < 0 || inIdx >= $ { r } ) {
2020-12-15 14:15:43 +01:00
return 0.0 ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
` ),this.userCode= `
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
float getValue ( int batch , int inIdx ) {
2021-03-09 23:32:33 +01:00
$ { u }
2020-12-15 14:15:43 +01:00
return getX ( batch , inIdx ) ;
2020-10-12 20:28:19 +02:00
}
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec2 coords = getOutputCoords ( ) ;
2020-10-12 20:28:19 +02:00
int batch = coords [ 0 ] ;
2020-12-15 14:15:43 +01:00
int outIdx = coords [ 1 ] ;
2021-01-12 16:01:34 +01:00
int inOffset = outIdx * $ { n } ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float sumValue = 0.0 ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { i } ; i += 4 ) {
2020-12-15 14:15:43 +01:00
int inIdx = inOffset + i ;
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
getValue ( batch , inIdx + 3 )
) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { l }
2020-12-15 14:15:43 +01:00
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
int inIdx = inOffset + $ { i } ;
if ( $ { o === 1 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 ( getValue ( batch , inIdx ) , 0.0 , 0.0 , 0.0 ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { l }
} else if ( $ { o === 2 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) , 0.0 , 0.0 ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { l }
} else if ( $ { o === 3 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) , 0.0 ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { l }
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( sumValue ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},BX=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:a,inSize:r,outSize:s}=e;this.outputShape=[a,s];let i="0.0",o="";t==="prod"?i="1.0":t==="min"?(i="1.0 / 1e-20",o="min"):t==="max"&&(i="-1.0 / 1e-20",o="max");let l= ` $ { t } ( $ { t } ( $ { t } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;t==="sum"?l="sumValue":t==="prod"?l="prodValue":t==="all"?l="allValue":t==="any"&&(l="anyValue");let u=Math.floor(n/4)*4,c=n%4,p= `
2021-01-12 16:01:34 +01:00
if ( $ { t === "sum" } ) {
2020-12-15 14:15:43 +01:00
sumValue += dot ( values , ones ) ;
2021-01-12 16:01:34 +01:00
} else if ( $ { t === "prod" } ) {
2020-12-15 14:15:43 +01:00
vec2 tmp = vec2 ( values [ 0 ] , values [ 1 ] ) * vec2 ( values [ 2 ] , values [ 3 ] ) ;
prodValue *= tmp [ 0 ] * tmp [ 1 ] ;
} else {
2021-01-12 16:01:34 +01:00
minMaxValue = $ { o } ( values , minMaxValue ) ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
` ,d="vec4";t==="all"?(i="1.0",p= `
2020-12-15 14:15:43 +01:00
bool reducedAllValue = all ( values ) ;
float floatedReducedAllValue = float ( reducedAllValue ) ;
allValue = float ( allValue >= 1.0 && floatedReducedAllValue >= 1.0 ) ;
2021-01-12 16:01:34 +01:00
` ,d="bvec4"):t==="any"&&(i="0.0",p= `
2020-12-15 14:15:43 +01:00
bool reducedAnyValue = any ( values ) ;
float floatedReducedAnyValue = float ( reducedAnyValue ) ;
anyValue = float ( anyValue >= 1.0 || floatedReducedAnyValue >= 1.0 ) ;
2021-01-12 16:01:34 +01:00
` ,d="bvec4");let h="";r%n>0&&(h= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
2020-12-15 14:15:43 +01:00
return initializationValue ;
}
` ),this.userCode= `
2021-01-12 16:01:34 +01:00
const float initializationValue = $ { i } ;
2020-12-15 14:15:43 +01:00
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float getValue ( int batch , int inIdx ) {
2021-01-12 16:01:34 +01:00
$ { h }
2020-12-15 14:15:43 +01:00
return getX ( batch , inIdx ) ;
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
2021-01-12 16:01:34 +01:00
int inOffset = outIdx * $ { n } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
vec4 minMaxValue = vec4 ( $ { i } ) ;
2020-12-15 14:15:43 +01:00
float prodValue = 1.0 ;
float sumValue = 0.0 ;
float allValue = 1.0 ;
float anyValue = 0.0 ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
for ( int i = 0 ; i < $ { u } ; i += 4 ) {
2020-12-15 14:15:43 +01:00
int inIdx = inOffset + i ;
2021-01-12 16:01:34 +01:00
$ { d } values = $ { d } (
2020-12-15 14:15:43 +01:00
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
getValue ( batch , inIdx + 3 )
) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { p }
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
int inIdx = inOffset + $ { u } ;
if ( $ { c === 1 } ) {
2021-01-12 16:01:34 +01:00
$ { d } values = $ { d } (
2020-12-15 14:15:43 +01:00
getValue ( batch , inIdx ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { p }
2021-03-09 23:32:33 +01:00
} else if ( $ { c === 2 } ) {
2021-01-12 16:01:34 +01:00
$ { d } values = $ { d } (
2020-12-15 14:15:43 +01:00
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
initializationValue ,
initializationValue
) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { p }
2021-03-09 23:32:33 +01:00
} else if ( $ { c === 3 } ) {
2021-01-12 16:01:34 +01:00
$ { d } values = $ { d } (
2020-12-15 14:15:43 +01:00
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
initializationValue
) ;
2021-01-12 16:01:34 +01:00
$ { p }
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
setOutput ( $ { l } ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function VX(e){let t=[];for(;t.length===0||t[t.length-1].outSize!==1;){let n=t.length?t[t.length-1].outSize:e[1],a=E.computeOptimalWindowSize(n);t.push({inSize:n,windowSize:a,outSize:Math.ceil(n/a)})}return t}function ji(e,t,n,a){let r=VX(e.shape),s=e;for(let i=0;i<r.length;i++){let{inSize:o,windowSize:l,outSize:u}=r[i],c,p;n==="mean"?c=i===0?new hT({windowSize:l,inSize:o,batchSize:e.shape[0],outSize:u},o):new hT({windowSize:l,inSize:o,batchSize:e.shape[0],outSize:u}):c=new BX({windowSize:l,inSize:o,batchSize:e.shape[0],outSize:u},n),p=s,s=a.runWebGLProgram(c,[s],t),p.dataId!==e.dataId&&a.disposeIntermediateTensorInfo(p)}return s}var GX=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[t[s]];this.outputShape=n,this.rank=n.length;let a=ht(this.rank),r=UX(t);this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { a } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { r } ) ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }};function UX(e){let t=e.length;if(t>6)throw Error( ` Transpose for rank $ { t } is not yet supported ` );let n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],a=new Array(t);for(let r=0;r<e.length;r++)a[e[r]]=n[r];return a.join()}var HX=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0;let n=new Array(e.length);for(let u=0;u<n.length;u++)n[u]=e[t[u]];if(this.outputShape=n,this.rank=n.length,this.rank>6)throw Error( ` Packed transpose for rank $ { this . rank } is not yet supported . ` );let a=ht(this.rank),r=Y2("rc",this.rank),s=new Array(this.rank);for(let u=0;u<t.length;u++)s[t[u]]=r[u];let i= ` vec2 ( $ { s . slice ( - 2 ) . join ( ) } ) ` ,o= ` ++ $ { r [ this . rank - 1 ] } < $ { n [ this . rank - 1 ] } ` ,l= ` getChannel ( getA ( $ { s . join ( ) } ) , $ { i } ) ` ;this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { a } rc = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
2021-01-12 16:01:34 +01:00
result [ 0 ] = $ { l } ;
if ( $ { o } ) {
result [ 1 ] = $ { l } ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
-- $ { r [ this . rank - 1 ] } ;
if ( ++ $ { r [ this . rank - 2 ] } < $ { n [ this . rank - 2 ] } ) {
result [ 2 ] = $ { l } ;
if ( $ { o } ) {
result [ 3 ] = $ { l } ;
2020-12-15 14:15:43 +01:00
}
}
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }};function Mm(e,t,n){let a=Z().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new HX(e.shape,t):new GX(e.shape,t);return n.runWebGLProgram(a,[e],e.dtype)}function jX(e,t,n,a){let r=t,s=e.shape.length,i=I.parseAxisParam(r,e.shape),o=i,l=E.getAxesPermutation(o,s),u=l!=null,c=e;u&&(c=Mm(e,l,a),o=E.getInnerMostAxes(o.length,s)),E.assertAxesAreInnerMostDims("sum",o,s);let[p,d]=E.computeOutAndReduceShapes(c.shape,o),h=p;n&&(h=E.expandShapeToKeepDim(p,i));let m=I.sizeFromShape(d),f=I.sizeFromShape(e.shape)/m,g=ye({inputs:{x:c},attrs:{shape:[f,m]},backend:a}),y=jd(e.dtype),b=ji(g,y,"sum",a),x=ye({inputs:{x:b},attrs:{shape:h},backend:a});return a.disposeIntermediateTensorInfo(g),a.disposeIntermediateTensorInfo(b),u&&a.disposeIntermediateTensorInfo(c),x}function Wv(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a;return jX(r,s,i,n)}var qX={kernelName:ci,backendName:"webgl",kernelFunc:Wv};function _n(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{perm:s}=a,i=n,o=r.shape.length,l=new Array(o);for(let c=0;c<l.length;c++)l[c]=r.shape[s[c]];let u;if(i.shouldExecuteOnCPU([r])){let c=i.texData.get(r.dataId).values,p=Ov(c,r.shape,r.dtype,s,l);u=i.makeTensorInfo(l,r.dtype);let d=i.texData.get(u.dataId);d.values=p}else u=Mm(r,s,i);return u}var XX={kernelName:fi,backendName:"webgl",kernelFunc:_n},mT=1e3;function Rm({a:e,b:t,transposeA:n,transposeB:a,backend:r,bias:s=null,preluActivationWeights:i=null,leakyreluAlpha:o=0,activation:l=null}){let u=e.shape.length,c=t.shape.length,p=n?e.shape[u-2]:e.shape[u-1],d=a?t.shape[c-1]:t.shape[c-2],h=n?e.shape[u-1]:e.shape[u-2],m=a?t.shape[c-2]:t.shape[c-1],f=e.shape.slice(0,-2),g=t.shape.slice(0,-2),y=I.sizeFromShape(f),b=I.sizeFromShape(g),x=y===b||y===1||b===1;I.assert(u>=2&&c>=2&&x,()=> ` Error in matMul : the input batch dimensions must either be the same or at least one input batch dimension must be 1. Got input batch dimensions of ( $ { f } ) and ( $ { g } ) . ` );let v=(y>b?e.shape.slice(0,-2):t.shape.slice(0,-2)).concat([h,m]);I.assert(p===d,()=> ` Error in matMul : inner shapes ( $ { p } ) and ( $ { d } ) of Tensors with shapes $ { e . shape } and $ { t . shape } and transposeA = $ { n } and transposeB = $ { a } must match . ` );let k=n?[y,p,h]:[y,h,p],w=a?[b,m,d]:[b,d,m],S=ye({inputs:{x:e},backend:r,attrs:{shape:k}}),A=ye({inputs:{x:t},backend:r,attrs:{shape:w}}),F=[S,A],D=Math.max(y,b),R=n?S.shape[1]:S.shape[2],L=s!=null,W=i!=null,U=l==="leakyrelu",G=l!=null?Dm(l,!0):null,X=L||W||U||G!=null,j;if((h===1||m===1)&&R>mT&&X===!1){let Q=S,se=A;n&&(Q=_n({inputs:{x:S},backend:r,attrs:{perm:[0,2,1]}}),F.push(Q)),a&&(se=_n({inputs:{x:A},backend:r,attrs:{perm:[0,2,1]}}),F.push(se));let ne=m!==1,ie=m===1,ee=Q;ne&&(ee=ye({inputs:{x:Q},backend:r,attrs:{shape:[D,R,1]}}),F.push(ee));let de=m===1?2:1,oe=se;ie&&(oe=ye({inputs:{x:se},backend:r,attrs:{shape:[D,1,R]}}),F.push(oe));let ge=dT({inputs:{a:ee,b:oe},backend:r});j=Wv({inputs:{x:ge},backend:r,attrs:{axis:de,keepDims:!0}}),F.push(ge)}else{let Q=ua(e.dtype,t.dtype),se=new lT(k,w,[D,h,m],n,a,L,G,W,U),ne=[S,A];if(s!=null&&ne.push(s),W&&ne.push(i),U){let ie=r.makeTensorInfo([],"float32",I.createScalarValue(o,"float32"));ne.push(ie),F.push(ie)}j=r.runWebGLProgram(se,ne,Q)}let te=ye({inputs:{x:j},backend:r,attrs:{shape:v}});F.push(j);for(let Q of F)r.disposeIntermediateTensorInfo(Q);return te}function KX(e){let{inputs:t,backend:n,attrs:a}=e,{a:r,b:s,bias:i,preluActivationWeights:o}=t,{transposeA:l,transposeB:u,activation:c,leakyreluAlpha:p}=a;return Rm({a:r,b:s,transposeA:l,transposeB:u,backend:n,bias:i,preluActivationWeights:o,leakyreluAlpha:p,activation:c})}var YX={kernelName:gi,backendName:"webgl",kernelFunc:KX},fT="return abs(x);";function JX(e){let{inputs:t,backend:n}=e,{x:a}=t;if(n.shouldExecuteOnCPU([a])&&a.dtype!=="complex64"){let s=n.texData.get(a.dataId),i=K2(s.values);return n.makeTensorInfo(a.shape,a.dtype,i)}let r;return Z().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new cu(a.shape,fT):r=new ds(a.shape,fT),n.runWebGLProgram(r,[a],a.dtype)}var QX={kernelName:So,backendName:"webgl",kernelFunc:JX},ZX=Ra+ `
2020-12-15 14:15:43 +01:00
if ( abs ( x ) > 1. ) {
return NAN ;
}
return acos ( x ) ;
2021-03-09 23:32:33 +01:00
` ,eK=Xe({opSnippet:ZX}),tK={kernelName:Co,backendName:"webgl",kernelFunc:eK},nK=Ra+ `
2020-12-15 14:15:43 +01:00
if ( x < 1.0 ) return NAN ;
2021-03-09 23:32:33 +01:00
return log ( x + sqrt ( x * x - 1.0 ) ) ; ` ,aK=Xe({opSnippet:nK}),rK={kernelName:Ao,backendName:"webgl",kernelFunc:aK},gT="return a + b;",sK=sn({opSnippet:gT,packedOpSnippet:gT,supportsComplex:!0,cpuKernelImpl:F8}),iK={kernelName:Br,backendName:"webgl",kernelFunc:sK},oK=class{constructor(e,t){this.outputShape=[],this.outputShape=e,this.variableNames=t.map((r,s)=> ` T$ { s } ` );let n=[];this.variableNames.forEach(r=>{n.push( ` float v$ { r } = get$ { r } AtOutCoords ( ) ; ` )});let a=this.variableNames.map(r=> ` v$ { r } ` ).join(" + ");this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { n . join ( `
2020-12-15 14:15:43 +01:00
` )}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
float result = $ { a } ;
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }},lK=class{constructor(e,t){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.variableNames=t.map((r,s)=> ` T$ { s } ` );let n=[];this.variableNames.forEach(r=>{n.push( ` vec4 v$ { r } = get$ { r } AtOutCoords ( ) ; ` )});let a=this.variableNames.map(r=> ` v$ { r } ` ).join(" + ");this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { n . join ( `
2020-12-15 14:15:43 +01:00
` )}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
vec4 result = $ { a } ;
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }};function Pm(e){let{inputs:t,backend:n}=e,a=t;if(a.length===1)return Gn({inputs:{x:a[0]},backend:n});if(a.length>Z().get("WEBGL_MAX_TEXTURES_IN_SHADER")){let o=Math.floor(a.length/2),l=Pm({inputs:a.slice(0,o),backend:n}),u=Pm({inputs:a.slice(o),backend:n});return Pm({inputs:[l,u],backend:n})}let r=a.map(o=>o.dtype).reduce((o,l)=>ua(o,l)),s=a.map(o=>o.shape),i=Z().getBool("WEBGL_PACK")?new lK(a[0].shape,s):new oK(a[0].shape,s);return n.runWebGLProgram(i,a,r)}var uK={kernelName:Ns,backendName:"webgl",kernelFunc:Pm};function cK(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=I.parseAxisParam(s,r.shape),u=l,c=E.getAxesPermutation(u,o),p=r;c!=null&&(p=_n({inputs:{x:r},backend:n,attrs:{perm:c}}),u=E.getInnerMostAxes(u.length,o)),E.assertAxesAreInnerMostDims("all",u,o);let[d,h]=E.computeOutAndReduceShapes(p.shape,u),m=I.sizeFromShape(h),f=ye({inputs:{x:p},backend:n,attrs:{shape:[-1,m]}}),g=ji(f,f.dtype,"all",n),y;if(i){let b=E.expandShapeToKeepDim(d,l);y=ye({inputs:{x:g},backend:n,attrs:{shape:b}})}else y=ye({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),y}var pK={kernelName:ud,backendName:"webgl",kernelFunc:cK};function dK(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=I.parseAxisParam(s,r.shape),u=l,c=E.getAxesPermutation(u,o),p=r;c!=null&&(p=_n({inputs:{x:r},backend:n,attrs:{perm:c}}),u=E.getInnerMostAxes(u.length,o)),E.assertAxesAreInnerMostDims("any",u,o);let[d,h]=E.computeOutAndReduceShapes(p.shape,u),m=I.sizeFromShape(h),f=ye({inputs:{x:p},backend:n,attrs:{shape:[-1,m]}}),g=ji(f,f.dtype,"any",n),y;if(i){let b=E.expandShapeToKeepDim(d,l);y=ye({inputs:{x:g},backend:n,attrs:{shape:b}})}else y=ye({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),y}var hK={kernelName:cd,backendName:"webgl",kernelFunc:dK},mK=class{constructor(e,t,n){this.variableNames=["A"];let{windowSize:a,batchSize:r,outSize:s}=e;n||this.variableNames.push("bestIndicesA"),this.outputShape=[r,s];let i=t==="max"?">":"<",o=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
2021-01-12 16:01:34 +01:00
int inOffset = outIdx * $ { a } ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
int bestIndex = inOffset ;
float bestValue = getA ( batch , bestIndex ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { a } ; i ++ ) {
int inIdx = $ { o } ;
2020-12-15 14:15:43 +01:00
float candidate = getA ( batch , inIdx ) ;
2021-01-12 16:01:34 +01:00
if ( candidate $ { i } bestValue ) {
2020-12-15 14:15:43 +01:00
bestValue = candidate ;
bestIndex = inIdx ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
}
setOutput ( float ( bestIndex ) ) ;
}
2021-03-09 23:32:33 +01:00
` }},fK=class{constructor(e,t,n,a){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,I.assert(e.length>2,()=> ` Packed arg$ { n . charAt ( 0 ) . toUpperCase ( ) + n . slice ( 1 ) } supports only inputs with rank above 2. ` );let r=e[e.length-1],s=Math.ceil(r/t);this.outputShape=e.slice(0,-1),s>1&&this.outputShape.push(s),a||this.variableNames.push("bestIndicesA");let i=this.outputShape,o=i.length,l=ht(o),u=fn("coords",o),c,p;if(s===1){p=o+1;let S=ht(p);c= `
$ { S } sourceLocR = $ { S } ( $ { u . join ( ) } , 0 ) ;
++ $ { u [ o - 1 ] } ;
$ { S } sourceLocG = $ { S } ( $ { u . join ( ) } , 0 ) ;
++ $ { u [ o - 2 ] } ;
$ { S } sourceLocA = $ { S } ( $ { u . join ( ) } , 0 ) ;
-- $ { u [ o - 1 ] } ;
$ { S } sourceLocB = $ { S } ( $ { u . join ( ) } , 0 ) ;
-- $ { u [ o - 2 ] } ; ` }else p=o,c= `
2021-01-12 16:01:34 +01:00
$ { l } sourceLocR = coords ;
2021-03-09 23:32:33 +01:00
++ $ { u [ o - 1 ] } ;
2021-01-12 16:01:34 +01:00
$ { l } sourceLocG = coords ;
2021-03-09 23:32:33 +01:00
++ $ { u [ o - 2 ] } ;
2021-01-12 16:01:34 +01:00
$ { l } sourceLocA = coords ;
2021-03-09 23:32:33 +01:00
-- $ { u [ o - 1 ] } ;
2021-01-12 16:01:34 +01:00
$ { l } sourceLocB = coords ;
2021-03-09 23:32:33 +01:00
-- $ { u [ o - 2 ] } ; ` ;let d=["x","y","z","w","u","v"].slice(0,p),h="."+d[p-1],m=d.map(S=>"int "+S),f=fn("sourceLocR",p-1).concat("inIdx.r"),g=fn("sourceLocG",p-1).concat("inIdx.g"),y=fn("sourceLocB",p-1).concat("inIdx.b"),b=fn("sourceLocA",p-1).concat("inIdx.a"),x=n==="max"?"greaterThan":"lessThan",v=a?"": `
2021-01-12 16:01:34 +01:00
inIdx = round ( vec4 ( getBestIndicesAChannel ( $ { f . join ( ) } ) ,
getBestIndicesAChannel ( $ { g . join ( ) } ) ,
getBestIndicesAChannel ( $ { y . join ( ) } ) ,
2021-03-09 23:32:33 +01:00
getBestIndicesAChannel ( $ { b . join ( ) } ) ) ) ; ` ,k= ` vec4 (
2021-01-12 16:01:34 +01:00
getAChannel ( $ { f . join ( ) } ) ,
hasNextCol ? getAChannel ( $ { g . join ( ) } ) : 0. ,
hasNextRow ? getAChannel ( $ { y . join ( ) } ) : 0. ,
2021-03-09 23:32:33 +01:00
hasNextRow && hasNextCol ? getAChannel ( $ { b . join ( ) } ) : 0. ) ` ,w=a?"": `
2021-01-12 16:01:34 +01:00
float getBestIndicesAChannel ( $ { m . join ( ) } ) {
return getChannel ( getBestIndicesA ( $ { d . join ( ) } ) ,
vec2 ( $ { d . slice ( - 2 ) . join ( ) } ) ) ;
2020-12-15 14:15:43 +01:00
} ` ;this.userCode= `
2021-01-12 16:01:34 +01:00
float getAChannel ( $ { m . join ( ) } ) {
return getChannel ( getA ( $ { d . join ( ) } ) ,
vec2 ( $ { d . slice ( - 2 ) . join ( ) } ) ) ;
}
2021-03-09 23:32:33 +01:00
$ { w }
2021-01-12 16:01:34 +01:00
void main ( ) {
$ { l } coords = getOutputCoords ( ) ;
2021-03-09 23:32:33 +01:00
bool hasNextCol = $ { u [ o - 1 ] } < $ { i [ o - 1 ] - 1 } ;
bool hasNextRow = $ { u [ o - 2 ] } < $ { i [ o - 2 ] - 1 } ;
$ { c }
2021-01-12 16:01:34 +01:00
ivec4 srcIdx = ivec4 ( sourceLocR$ { h } , sourceLocG$ { h } ,
sourceLocB$ { h } , sourceLocA$ { h } ) * $ { t } ;
2020-12-15 14:15:43 +01:00
ivec4 inIdx = srcIdx ;
vec4 bestIndex = vec4 ( inIdx ) ;
2021-03-09 23:32:33 +01:00
vec4 bestValue = $ { k } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { t } ; i ++ ) {
2020-12-15 14:15:43 +01:00
inIdx = srcIdx ;
2021-02-25 14:12:39 +01:00
$ { v }
2021-03-09 23:32:33 +01:00
vec4 candidate = $ { k } ;
2020-12-15 14:15:43 +01:00
bvec4 nan = isnan ( candidate ) ;
bvec4 replace = bvec4 (
2021-02-25 14:12:39 +01:00
vec4 ( $ { x } ( candidate , bestValue ) ) * ( vec4 ( 1.0 ) - vec4 ( nan ) ) ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ++ ;
2020-08-29 20:56:29 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( bestIndex ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function yT(e,t,n,a=null){let r=t.shape[0],s=t.shape[1];a!=null&&(r=a.shape[0],s=a.shape[1]);let i=E.computeOptimalWindowSize(s),o={windowSize:i,inSize:s,batchSize:r,outSize:Math.ceil(s/i)},l=new mK(o,n,a==null),u=[t];a!=null&&u.push(a);let c=e.runWebGLProgram(l,u,"int32");if(c.shape[1]===1)return c;let p=yT(e,t,n,c);return e.disposeIntermediateTensorInfo(c),p}function bT(e,t,n,a=null){let r=a!=null?a.shape:t.shape,s=r[r.length-1],i=E.computeOptimalWindowSize(s),o=new fK(r,i,n,a==null),l=a==null?[t]:[t,a],u=e.runWebGLProgram(o,l,"int32");if(u.shape.length===t.shape.length){let c=bT(e,t,n,u);return e.disposeIntermediateTensorInfo(u),c}return u}function xT(e,t,n,a){let r=[n];if(E.assertAxesAreInnerMostDims("arg"+a.charAt(0).toUpperCase()+a.slice(1),r,t.shape.length),!Z().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let s=[],[i,o]=E.computeOutAndReduceShapes(t.shape,r),l=I.sizeFromShape(o),u=ye({inputs:{x:t},backend:e,attrs:{shape:[-1,l]}});s.push(u);let c=yT(e,u,a);s.push(c);let p=ye({inputs:{x:c},backend:e,attrs:{shape:i}});return s.forEach(d=>e.disposeIntermediateTensorInfo(d)),p}return bT(e,t,a)}function gK(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s}=a,i=I.parseAxisParam(s,r.shape),o=E.getAxesPermutation(i,r.shape.length),l=r,u=[];o!=null&&(l=_n({inputs:{x:r},backend:n,attrs:{perm:o}}),u.push(l),i=E.getInnerMostAxes(i.length,l.shape.length)),E.assertAxesAreInnerMostDims("argMax",[i[0]],l.shape.length);let c=xT(n,l,i[0],"max");return u.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var yK={kernelName:Ts,backendName:"webgl",kernelFunc:gK};function bK(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s}=a,i=I.parseAxisParam(s,r.shape),o=E.getAxesPermutation(i,r.shape.length),l=r,u=[];o!=null&&(l=_n({inputs:{x:r},backend:n,attrs:{perm:o}}),u.push(l),i=E.getInnerMostAxes(i.length,l.shape.length)),E.assertAxesAreInnerMostDims("argMin",[i[0]],l.shape.length);let c=xT(n,l,i[0],"min");return u.forEach(p=>n.disposeIntermediateTensorInfo(p)),c}var xK={kernelName:Ku,backendName:"webgl",kernelFunc:bK},vK=Ra+ `
2020-12-15 14:15:43 +01:00
if ( abs ( x ) > 1. ) {
return NAN ;
}
return asin ( x ) ;
2021-03-09 23:32:33 +01:00
` ,wK=Xe({opSnippet:vK}),kK={kernelName:Fo,backendName:"webgl",kernelFunc:wK},IK=Ra+"return log(x + sqrt(x * x + 1.0));",NK=Xe({opSnippet:IK}),TK={kernelName:_o,backendName:"webgl",kernelFunc:NK},SK=Ra+ `
2020-12-15 14:15:43 +01:00
return atan ( x ) ;
2021-03-09 23:32:33 +01:00
` ,CK=Xe({opSnippet:SK}),AK={kernelName:Eo,backendName:"webgl",kernelFunc:CK},FK=PX+ `
2020-12-15 14:15:43 +01:00
return atan ( a , b ) ;
2021-03-09 23:32:33 +01:00
` ,_K= `
2020-12-15 14:15:43 +01:00
vec4 result = atan ( a , b ) ;
vec4 isNaN = min ( vec4 ( isnan ( a ) ) + vec4 ( isnan ( b ) ) , vec4 ( 1.0 ) ) ;
2021-03-09 23:32:33 +01:00
` +OX+ `
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,EK=sn({opSnippet:FK,packedOpSnippet:_K}), $ K={kernelName:Do,backendName:"webgl",kernelFunc:EK},DK=Ra+ `
2020-12-15 14:15:43 +01:00
if ( ( x < - 1.0 ) || ( x > 1.0 ) ) return NAN ;
2021-03-09 23:32:33 +01:00
return ( log ( 1.0 + x ) - log ( 1.0 - x ) ) / 2.0 ; ` ,MK=Xe({opSnippet:DK}),RK={kernelName: $ o,backendName:"webgl",kernelFunc:MK},hp=class{constructor(e,t,n,a=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let s=e.filterWidth,i=e.strideHeight,o=e.strideWidth,l=e.dilationHeight,u=e.dilationWidth,c=e.effectiveFilterHeight,p=e.effectiveFilterWidth,d=e.padInfo.top,h=e.padInfo.left;this.outputShape=e.outShape;let m=t==="avg",f= ` ( ( batch * $ { e . inHeight } + xR ) * $ { e . inWidth } + xC ) * $ { e . inChannels } + d ` ,g= ` ( xR * $ { e . inWidth } + xC ) * $ { e . inChannels } + d ` ,y="0.0";if(m||(y="-1.0 / 1e-20"),n){let S=">=";this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec2 strides = ivec2 ( $ { i } , $ { o } ) ;
const ivec2 pads = ivec2 ( $ { d } , $ { h } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int d = coords [ 3 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
ivec2 xRCCorner = coords . yz * strides - pads ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
float minMaxValue = 0.0 ;
float minMaxValueFound = 0.0 ;
int minMaxPosition = 0 ;
float avgValue = 0.0 ;
2021-03-09 23:32:33 +01:00
for ( int wR = 0 ; wR < $ { c } ;
2021-01-12 16:01:34 +01:00
wR += $ { l } ) {
2020-12-15 14:15:43 +01:00
int xR = xRCorner + wR ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { p } ;
2021-03-09 23:32:33 +01:00
wC += $ { u } ) {
2020-12-15 14:15:43 +01:00
int xC = xCCorner + wC ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2020-12-15 14:15:43 +01:00
float value = getX ( batch , xR , xC , d ) ;
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix (
value , minMaxValue , minMaxValueFound ) ;
2021-02-25 14:12:39 +01:00
if ( value $ { S } currMinMaxValue ) {
2020-12-15 14:15:43 +01:00
minMaxValue = value ;
minMaxValueFound = 1.0 ;
2021-01-12 16:01:34 +01:00
minMaxPosition = $ { a ? r ? f : g : ` wR * ${ p } + wC ` } ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( float ( minMaxPosition ) ) ;
2020-08-29 20:56:29 +02:00
}
2021-03-09 23:32:33 +01:00
` ;return}let b="max",x= ` $ { t } ( $ { t } ( $ { t } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;t==="avg"&&(x="avgValue / count");let v=Math.floor(s/4)*4,k=s%4,w= `
2021-01-12 16:01:34 +01:00
if ( $ { m } ) {
2020-12-15 14:15:43 +01:00
avgValue += dot ( values , ones ) ;
} else {
2021-01-12 16:01:34 +01:00
minMaxValue = $ { b } ( values , minMaxValue ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
` ;this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec2 strides = ivec2 ( $ { i } , $ { o } ) ;
const ivec2 pads = ivec2 ( $ { d } , $ { h } ) ;
const float initializationValue = $ { y } ;
2020-12-15 14:15:43 +01:00
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
float count = 0.0 ;
float getValue ( int batch , int xR , int xC , int d ) {
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
return initializationValue ;
}
count += 1.0 ;
return getX ( batch , xR , xC , d ) ;
}
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
2020-12-15 14:15:43 +01:00
int d = coords [ 3 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
ivec2 xRCCorner = coords . yz * strides - pads ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
2021-01-12 16:01:34 +01:00
vec4 minMaxValue = vec4 ( $ { y } ) ;
2020-12-15 14:15:43 +01:00
float avgValue = 0.0 ;
count = 0.0 ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
for ( int wR = 0 ; wR < $ { c } ;
2021-01-12 16:01:34 +01:00
wR += $ { l } ) {
2020-12-15 14:15:43 +01:00
int xR = xRCorner + wR ;
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2021-02-25 14:12:39 +01:00
for ( int wC = 0 ; wC < $ { v } ; wC += 4 ) {
2021-03-09 23:32:33 +01:00
int xC = xCCorner + wC * $ { u } ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
2021-03-09 23:32:33 +01:00
getValue ( batch , xR , xC + $ { u } , d ) ,
getValue ( batch , xR , xC + 2 * $ { u } , d ) ,
getValue ( batch , xR , xC + 3 * $ { u } , d )
2020-12-15 14:15:43 +01:00
) ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
$ { w }
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-02-25 14:12:39 +01:00
int xC = xCCorner + $ { v } ;
2021-03-09 23:32:33 +01:00
if ( $ { k === 1 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
$ { w }
} else if ( $ { k === 2 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
2021-03-09 23:32:33 +01:00
getValue ( batch , xR , xC + $ { u } , d ) ,
2020-12-15 14:15:43 +01:00
initializationValue ,
initializationValue
) ;
2021-03-09 23:32:33 +01:00
$ { w }
} else if ( $ { k === 3 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xR , xC , d ) ,
2021-03-09 23:32:33 +01:00
getValue ( batch , xR , xC + $ { u } , d ) ,
getValue ( batch , xR , xC + 2 * $ { u } , d ) ,
2020-12-15 14:15:43 +01:00
initializationValue
) ;
2021-03-09 23:32:33 +01:00
$ { w }
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
}
2021-02-25 14:12:39 +01:00
setOutput ( $ { x } ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},Bv=class{constructor(e,t,n,a=!1,r=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let s=e.filterWidth,i=e.strideDepth,o=e.strideHeight,l=e.strideWidth,u=e.dilationDepth,c=e.dilationHeight,p=e.dilationWidth,d=e.effectiveFilterDepth,h=e.effectiveFilterHeight,m=e.effectiveFilterWidth,f=e.padInfo.front,g=e.padInfo.top,y=e.padInfo.left;this.outputShape=e.outShape;let b=t==="avg",x="0.0";if(b||(x="-1.0 / 1e-20"),n){let F=">=";this.userCode= `
2020-12-15 14:15:43 +01:00
const ivec3 strides =
2021-01-12 16:01:34 +01:00
ivec3 ( $ { i } , $ { o } , $ { l } ) ;
const ivec3 pads = ivec3 ( $ { f } , $ { g } , $ { y } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int wD = 0 ; wD < $ { d } ;
2021-03-09 23:32:33 +01:00
wD += $ { u } ) {
2020-12-15 14:15:43 +01:00
int xD = xDCorner + wD ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xD < 0 || xD >= $ { e . inDepth } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { h } ;
2021-03-09 23:32:33 +01:00
wR += $ { c } ) {
2020-12-15 14:15:43 +01:00
int xR = xRCorner + wR ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-12-15 14:15:43 +01:00
continue ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { m } ;
wC += $ { p } ) {
2020-12-15 14:15:43 +01:00
int xC = xCCorner + wC ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float value = getX ( batch , xD , xR , xC , ch ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ) ;
2021-03-09 23:32:33 +01:00
if ( value $ { F } currMinMaxValue ) {
2020-12-15 14:15:43 +01:00
minMaxValue = value ;
minMaxValueFound = 1.0 ;
2021-01-12 16:01:34 +01:00
minMaxPosition = $ { a ? r ? ` (((batch * ${ e . inDepth } + xD) * ${ e . inHeight } + xR) * ${ e . inWidth } + xC) * ${ e . inChannels } + ch ` : ` ((xD * ${ e . inHeight } + xR) * ${ e . inWidth } + xC) * ${ e . inChannels } + ch ` : ` wD * ${ h } * ${ m } +
wR * $ { m } + wC ` };
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
}
}
}
2020-12-15 14:15:43 +01:00
setOutput ( float ( minMaxPosition ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` ;return}let v="max",k= ` $ { t } ( $ { t } ( $ { t } ( minMaxValue [ 0 ] , minMaxValue [ 1 ] ) , minMaxValue [ 2 ] ) , minMaxValue [ 3 ] ) ` ;t==="avg"&&(k="avgValue / count");let w=Math.floor(s/4)*4,S=s%4,A= `
2021-01-12 16:01:34 +01:00
if ( $ { b } ) {
2020-12-15 14:15:43 +01:00
avgValue += dot ( values , ones ) ;
} else {
2021-02-25 14:12:39 +01:00
minMaxValue = $ { v } ( values , minMaxValue ) ;
2020-08-29 20:56:29 +02:00
}
2020-12-15 14:15:43 +01:00
` ;this.userCode= `
const ivec3 strides =
2021-01-12 16:01:34 +01:00
ivec3 ( $ { i } , $ { o } , $ { l } ) ;
const ivec3 pads = ivec3 ( $ { f } , $ { g } , $ { y } ) ;
2021-02-25 14:12:39 +01:00
const float initializationValue = $ { x } ;
2020-12-15 14:15:43 +01:00
const vec4 ones = vec4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
float count = 0.0 ;
float getValue ( int batch , int xD , int xR , int xC , int ch ) {
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
return initializationValue ;
}
count += 1.0 ;
return getX ( batch , xD , xR , xC , ch ) ;
}
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
2020-12-15 14:15:43 +01:00
int ch = coords . u ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).
// ? = to be determined
2021-02-25 14:12:39 +01:00
vec4 minMaxValue = vec4 ( $ { x } ) ;
2020-12-15 14:15:43 +01:00
float avgValue = 0.0 ;
count = 0.0 ;
2021-01-12 16:01:34 +01:00
for ( int wD = 0 ; wD < $ { d } ;
2021-03-09 23:32:33 +01:00
wD += $ { u } ) {
2020-12-15 14:15:43 +01:00
int xD = xDCorner + wD ;
2021-01-12 16:01:34 +01:00
if ( xD < 0 || xD >= $ { e . inDepth } ) {
2020-12-15 14:15:43 +01:00
continue ;
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { h } ;
2021-03-09 23:32:33 +01:00
wR += $ { c } ) {
2020-12-15 14:15:43 +01:00
int xR = xRCorner + wR ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2021-03-09 23:32:33 +01:00
for ( int wC = 0 ; wC < $ { w } ; wC += 4 ) {
2021-01-12 16:01:34 +01:00
int xC = xCCorner + wC * $ { p } ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
2021-01-12 16:01:34 +01:00
getValue ( batch , xD , xR , xC + $ { p } , ch ) ,
getValue ( batch , xD , xR , xC + 2 * $ { p } , ch ) ,
getValue ( batch , xD , xR , xC + 3 * $ { p } , ch )
2020-12-15 14:15:43 +01:00
) ;
2020-10-12 20:28:19 +02:00
2021-02-25 14:12:39 +01:00
$ { A }
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
int xC = xCCorner + $ { w } ;
2021-02-25 14:12:39 +01:00
if ( $ { S === 1 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
2020-10-12 20:28:19 +02:00
2021-02-25 14:12:39 +01:00
$ { A }
} else if ( $ { S === 2 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
2021-01-12 16:01:34 +01:00
getValue ( batch , xD , xR , xC + $ { p } , ch ) ,
2020-12-15 14:15:43 +01:00
initializationValue ,
initializationValue
) ;
2021-02-25 14:12:39 +01:00
$ { A }
} else if ( $ { S === 3 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , xD , xR , xC , ch ) ,
2021-01-12 16:01:34 +01:00
getValue ( batch , xD , xR , xC + $ { p } , ch ) ,
getValue ( batch , xD , xR , xC + 2 * $ { p } , ch ) ,
2020-12-15 14:15:43 +01:00
initializationValue
) ;
2021-02-25 14:12:39 +01:00
$ { A }
2020-10-12 20:28:19 +02:00
}
}
2021-03-09 23:32:33 +01:00
setOutput ( $ { k } ) ;
2020-10-12 20:28:19 +02:00
}
}
2021-03-09 23:32:33 +01:00
` }};function PK(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t;cp(r,"avgPool");let{filterSize:s,strides:i,pad:o,dimRoundingMode:l}=a,u=1;I.assert(E.eitherStridesOrDilationsAreOne(i,u),()=> ` Error in avgPool : Either strides or dilations must be 1. Got strides $ { i } and dilations '${u}' ` );let c=E.computePool2DInfo(r.shape,s,i,u,o,l);if(c.filterWidth===1&&c.filterHeight===1&&I.arraysEqual(c.inShape,c.outShape))return Gn({inputs:{x:r},backend:n});let p=new hp(c,"avg",!1);return n.runWebGLProgram(p,[r],"float32")}var OK={kernelName:Ss,backendName:"webgl",kernelFunc:PK};function LK(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{filterSize:s,strides:i,pad:o,dimRoundingMode:l,dataFormat:u}=a,c=[1,1,1],p=E.computePool3DInfo(r.shape,s,i,c,o,l,u),d=new Bv(p,"avg",!1);return n.runWebGLProgram(d,[r],"float32")}var zK={kernelName:Yu,backendName:"webgl",kernelFunc:LK},WK=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,a=e.strideHeight,r=e.strideWidth,s=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterHeight,l=e.effectiveFilterWidth,u=o-1-e.padInfo.top,c=l-1-e.padInfo.left,p=1/(t*n);this.userCode= `
const ivec2 pads = ivec2 ( $ { u } , $ { c } ) ;
2021-01-12 16:01:34 +01:00
const float avgMultiplier = float ( $ { p } ) ;
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
ivec2 dyRCCorner = coords . yz - pads ;
int dyRCorner = dyRCCorner . x ;
int dyCCorner = dyRCCorner . y ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
2020-10-12 20:28:19 +02:00
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { o } ;
wR += $ { s } ) {
float dyR = float ( dyRCorner + wR ) / $ { a } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2020-12-15 14:15:43 +01:00
int idyR = int ( dyR ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { l } ;
wC += $ { i } ) {
float dyC = float ( dyCCorner + wC ) / $ { r } . 0 ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyC ) > 0.0 ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2020-12-15 14:15:43 +01:00
int idyC = int ( dyC ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float dyValue = getDy ( b , idyR , idyC , d ) ;
dotProd += dyValue * avgMultiplier ;
2020-10-12 20:28:19 +02:00
}
}
2020-12-15 14:15:43 +01:00
setOutput ( dotProd ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},BK=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,a=e.filterWidth,r=e.strideDepth,s=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,l=e.dilationHeight,u=e.dilationWidth,c=e.effectiveFilterDepth,p=e.effectiveFilterHeight,d=e.effectiveFilterWidth,h=c-1-e.padInfo.front,m=p-1-e.padInfo.top,f=d-1-e.padInfo.left,g=1/(t*n*a);this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec3 pads = ivec3 ( $ { h } , $ { m } , $ { f } ) ;
const float avgMultiplier = float ( $ { g } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
ivec3 dyCorner = ivec3 ( coords . y , coords . z , coords . w ) - pads ;
int dyDCorner = dyCorner . x ;
int dyRCorner = dyCorner . y ;
int dyCCorner = dyCorner . z ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Convolve dy(?, ?, ?, d) with pos mask(:, :, :, ch) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
for ( int wD = 0 ; wD < $ { c } ;
2021-01-12 16:01:34 +01:00
wD += $ { o } ) {
float dyD = float ( dyDCorner + wD ) / $ { r } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyD < 0.0 || dyD >= $ { e . outDepth } . 0 || fract ( dyD ) > 0.0 ) {
2020-12-15 14:15:43 +01:00
continue ;
}
int idyD = int ( dyD ) ;
2020-12-02 22:46:41 +01:00
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { p } ;
wR += $ { l } ) {
float dyR = float ( dyRCorner + wR ) / $ { s } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyR ) > 0.0 ) {
continue ;
}
int idyR = int ( dyR ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { d } ;
2021-03-09 23:32:33 +01:00
wC += $ { u } ) {
2021-01-12 16:01:34 +01:00
float dyC = float ( dyCCorner + wC ) / $ { i } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float dyValue = getDy ( batch , idyD , idyR , idyC , ch ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
dotProd += dyValue * avgMultiplier ;
}
}
}
setOutput ( dotProd ) ;
}
2021-03-09 23:32:33 +01:00
` }};function VK(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s}=t,i=s,{filterSize:o,strides:l,pad:u,dimRoundingMode:c}=a,p=[1,1,1],d=E.computePool3DInfo(i.shape,o,l,p,u,c),h=new BK(d);return n.runWebGLProgram(h,[r],i.dtype)}var UK={kernelName:dd,backendName:"webgl",kernelFunc:VK};function GK(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s}=t,i=s;cp([r,s],"avgPoolGrad");let{filterSize:o,strides:l,pad:u}=a,c=E.computePool2DInfo(i.shape,o,l,1,u),p=new WK(c);return n.runWebGLProgram(p,[r],i.dtype)}var HK={kernelName:pd,backendName:"webgl",kernelFunc:GK};function jK(e){let{inputs:t,backend:n,attrs:a}=e,{a:r,b:s}=t,{transposeA:i,transposeB:o}=a;return Rm({a:r,b:s,transposeA:i,transposeB:o,backend:n})}var qK={kernelName:Cs,backendName:"webgl",kernelFunc:jK},XK=class{constructor(e,t,n,a,r,s){this.outputShape=[],this.variableNames=["x","mean","variance"],E.assertAndGetBroadcastShape(e,t),E.assertAndGetBroadcastShape(e,n);let i="0.0";a!=null&&(E.assertAndGetBroadcastShape(e,a),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="1.0";r!=null&&(E.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
float x = getXAtOutCoords ( ) ;
float mean = getMeanAtOutCoords ( ) ;
float variance = getVarianceAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
float offset = $ { i } ;
float scale = $ { o } ;
float inv = scale * inversesqrt ( variance + float ( $ { s } ) ) ;
2020-12-15 14:15:43 +01:00
setOutput ( dot ( vec3 ( x , - mean , offset ) , vec3 ( inv , inv , 1 ) ) ) ;
}
2021-03-09 23:32:33 +01:00
` }},KK=class{constructor(e,t,n,a,r,s){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],E.assertAndGetBroadcastShape(e,t),E.assertAndGetBroadcastShape(e,n);let i="vec4(0.0)";a!=null&&(E.assertAndGetBroadcastShape(e,a),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");let o="vec4(1.0)";r!=null&&(E.assertAndGetBroadcastShape(e,r),this.variableNames.push("scale"),o="getScaleAtOutCoords()"),this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
vec4 offset = $ { i } ;
vec4 scale = $ { o } ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
vec4 x = getXAtOutCoords ( ) ;
vec4 mean = getMeanAtOutCoords ( ) ;
vec4 variance = getVarianceAtOutCoords ( ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
vec4 inv = scale * inversesqrt ( variance + vec4 ( $ { s } ) ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( ( x - mean ) * inv + offset ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},YK=({inputs:e,backend:t,attrs:n})=>{let{x:a,mean:r,variance:s,offset:i,scale:o}=e;I.assert(r.shape.length===s.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),I.assert(i==null||r.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),I.assert(o==null||r.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:l}=n;l==null&&(l=.001);let u=[a,r,s],c=null;i!=null&&(c=i.shape,u.push(i));let p=null;o!=null&&(p=o.shape,u.push(o));let d=Z().getBool("WEBGL_PACK_NORMALIZATION")?new KK(a.shape,r.shape,s.shape,c,p,l):new XK(a.shape,r.shape,s.shape,c,p,l);return t.runWebGLProgram(d,u,u[0].dtype)},JK={kernelName:zs,backendName:"webgl",kernelFunc:YK},ZK=class{constructor(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;let t=ht(this.rank),n= ` uniform int start [ $ { this . rank } ] ; ` ,a=QK(this.rank),r,s=e.map((i,o)=> ` sourceLoc . $ { Vv [ o ] } = start [ $ { o } ] + coords . $ { Vv [ o ] } ; ` );r= `
2021-01-12 16:01:34 +01:00
$ { t } sourceLoc ;
$ { t } coords = getOutputCoords ( ) ;
$ { s . join ( `
2020-12-15 14:15:43 +01:00
` )}
` ,this.userCode= `
2021-01-12 16:01:34 +01:00
$ { n }
void main ( ) {
$ { r }
setOutput ( getSource ( $ { a } ) ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){if(e.length!==this.rank)throw Error( ` The rank ( $ { this . rank } ) of the program must match the length of start ( $ { e . length } ) ` );return(t,n)=>{this.startLoc==null&&(this.startLoc=t.getUniformLocationNoThrow(n,"start"),this.startLoc==null)||t.gl.uniform1iv(this.startLoc,e)}}},Vv=["x","y","z","w","u","v"];function QK(e){if(e===1)return"sourceLoc";if(e<=6)return Vv.slice(0,e).map(t=>"sourceLoc."+t).join(",");throw Error( ` Slicing for rank $ { e } is not yet supported ` )}var e7=class{constructor(e){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.rank=e.length;let t=ht(this.rank),n=fn("coords",this.rank),a=fn("sourceLoc",this.rank),r=this.rank===1?"sourceLoc": ` vec2 ( $ { a . slice ( - 2 ) . join ( ) } ) ` ,s= ` getChannel ( getSource ( $ { a . join ( ) } ) , $ { r } ) ` ,i= `
2021-01-12 16:01:34 +01:00
result . x = $ { s } ;
if ( ++ $ { n [ this . rank - 1 ] } < $ { e [ this . rank - 1 ] } ) {
++ $ { a [ this . rank - 1 ] } ;
result . y = $ { s } ;
-- $ { a [ this . rank - 1 ] } ;
}
` ,o=this.rank===1?"": `
-- $ { n [ this . rank - 1 ] } ;
if ( ++ $ { n [ this . rank - 2 ] } < $ { e [ this . rank - 2 ] } ) {
++ $ { a [ this . rank - 2 ] } ;
result . z = $ { s } ;
if ( ++ $ { n [ this . rank - 1 ] } < $ { e [ this . rank - 1 ] } ) {
++ $ { a [ this . rank - 1 ] } ;
result . w = $ { s } ;
}
}
` ,l=this.rank<=4? ` sourceLoc = coords +
2021-03-09 23:32:33 +01:00
$ { t } ( $ { e . map ( ( u , c ) => ` start[ ${ c } ] ` ) . join ( ) } ) ; ` :e.map((u,c)=> ` $ { a [ c ] } = $ { n [ c ] } + start [ $ { c } ] ; ` ).join( `
2020-12-15 14:15:43 +01:00
` );this.userCode= `
uniform int start [ $ { this . rank } ] ;
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { t } coords = getOutputCoords ( ) ;
$ { t } sourceLoc ;
$ { l }
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
2021-01-12 16:01:34 +01:00
$ { i }
$ { o }
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){if(e.length!==this.rank)throw Error( ` The rank ( $ { this . rank } ) of the program must match the length of start ( $ { e . length } ) ` );return(t,n)=>{this.startLoc==null&&(this.startLoc=t.getUniformLocationNoThrow(n,"start"),this.startLoc==null)||t.gl.uniform1iv(this.startLoc,e)}}};function t7(e,t,n,a){let r=a.texData.get(e.dataId),s=a.makeTensorInfo(n,e.dtype),i=a.texData.get(s.dataId);Object.assign(i,r),i.refCount=1,i.shape=n,i.dtype=e.dtype;let o=pn.computeFlatOffset(t,I.computeStrides(e.shape));r.slice&&(o+=r.slice.flatOffset),i.slice={flatOffset:o,origDataId:r.slice&&r.slice.origDataId||e.dataId};let l=a.dataRefCount.get(i.slice.origDataId)||1;return a.dataRefCount.set(i.slice.origDataId,l+1),s}function mp(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{begin:s,size:i}=a,[o,l]=pn.parseSliceParams(r,s,i);if(pn.assertParamsValid(r,o,l),I.sizeFromShape(l)===0)return n.makeTensorInfo(l,r.dtype,[]);if(n.shouldExecuteOnCPU([r])||r.dtype==="string"){let p=n.texData.get(r.dataId),d=K8(p.values,o,l,r.shape,r.dtype);return n.makeTensorInfo(l,r.dtype,d)}let{isPacked:u}=n.texData.get(r.dataId),c=pn.isSliceContinous(r.shape,o,l);if(u||!c){let p=Z().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new e7(l):new ZK(l),d=p.getCustomSetupFunc(o);return n.runWebGLProgram(p,[r],r.dtype,d)}return n.uploadToGPU(r.dataId),t7(r,o,l,n)}var n7={kernelName:ml,backendName:"webgl",kernelFunc:mp},a7=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{blockShape:s,crops:i}=a;I.assert(r.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let o=s.reduce((b,x)=>b*x),l=E.getReshaped(r.shape,s,o),u=E.getPermuted(l.length,s.length),c=E.getReshapedPermuted(r.shape,s,o),p=E.getSliceBeginCoords(i,s.length),d=E.getSliceSize(c,i,s.length),h=[],m=ye({inputs:{x:r},backend:n,attrs:{shape:l}}),f=_n({inputs:{x:m},backend:n,attrs:{perm:u}}),g=ye({inputs:{x:f},backend:n,attrs:{shape:c}}),y=mp({inputs:{x:g},backend:n,attrs:{begin:p,size:d}});return h.push(m),h.push(f),h.push(g),h.forEach(b=>n.disposeIntermediateTensorInfo(b)),y},r7={kernelName:Ju,backendName:"webgl",kernelFunc:a7};function s7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,weights:s}=t,{size:i}=a,o=n.readSync(r.dataId),l=n.readSync(s.dataId),u=X2(o,l,s.dtype,s.shape,i);return n.makeTensorInfo([i],s.dtype,u)}var i7={kernelName:hd,backendName:"webgl",kernelFunc:s7},o7="return float(a != b);",vT=sn({opSnippet:o7,dtype:"bool"}),l7={kernelName:nl,backendName:"webgl",kernelFunc:vT};function fp(e){let{inputs:t,backend:n}=e,{input:a}=t,r=n.texData.get(a.dataId);return Gn({inputs:{x:r.complexTensorInfos.real},backend:n})}var u7={kernelName:Md,backendName:"webgl",kernelFunc:fp},c7="return float(int(x));";function p7(e,t){let n=new ds(e.shape,c7),a=t.runWebGLProgram(n,[e],"int32");return{dataId:a.dataId,shape:a.shape,dtype:a.dtype}}function Uv(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{dtype:s}=a;if(s==="complex64"){if(r.dtype==="complex64")return Gn({inputs:{x:r},backend:n});let i=vt(r.shape),o=Uv({inputs:{x:r},backend:n,attrs:{dtype:"float32"}}),l=hs({inputs:{real:o,imag:i},backend:n});return i.dispose(),n.disposeIntermediateTensorInfo(o),l}if(r.dtype==="complex64"){let i=fp({inputs:{input:r},backend:n}),o=Uv({inputs:{x:i},backend:n,attrs:{dtype:s}});return n.disposeIntermediateTensorInfo(i),o}if(!I.hasEncodingLoss(r.dtype,s)){let i=Gn({inputs:{x:r},backend:n});return{dataId:i.dataId,shape:i.shape,dtype:s}}if(s==="int32")return p7(r,n);if(s==="bool"){let i=n.makeTensorInfo([],"bool",I.getTypedArrayFromDType("bool",1)),o=vT({inputs:{a:r,b:i},backend:n});return n.disposeIntermediateTensorInfo(i),o}throw new Error( ` Error in Cast : failed to cast $ { r . dtype } to $ { s } ` )}var d7={kernelName:As,backendName:"webgl",kernelFunc:Uv},wT="return ceil(x);",h7=Xe({opSnippet:wT,packedOpSnippet:wT,cpuKernelImpl:E8}),m7={kernelName:Fs,backendName:"webgl",kernelFunc:h7},f7=class{constructor(e){this.variableNames=["A"],this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
uniform float minVal ;
uniform float maxVal ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
float value = getAAtOutCoords ( ) ;
if ( isnan ( value ) ) {
setOutput ( value ) ;
2020-10-12 20:28:19 +02:00
return ;
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( clamp ( value , minVal , maxVal ) ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e,t){return(n,a)=>{this.minLoc==null&&(this.minLoc=n.getUniformLocationNoThrow(a,"minVal"),this.maxLoc=n.getUniformLocationNoThrow(a,"maxVal")),n.gl.uniform1f(this.minLoc,e),n.gl.uniform1f(this.maxLoc,t)}}},g7=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
uniform float minVal ;
uniform float maxVal ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
vec4 value = getAAtOutCoords ( ) ;
if ( any ( isnan ( value ) ) ) {
setOutput ( value ) ;
2020-10-12 20:28:19 +02:00
return ;
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( clamp ( value , vec4 ( minVal ) , vec4 ( maxVal ) ) ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e,t){return(n,a)=>{this.minLoc==null&&(this.minLoc=n.getUniformLocationNoThrow(a,"minVal"),this.maxLoc=n.getUniformLocationNoThrow(a,"maxVal")),n.gl.uniform1f(this.minLoc,e),n.gl.uniform1f(this.maxLoc,t)}}};function y7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{clipValueMin:s,clipValueMax:i}=a,o;Z().getBool("WEBGL_PACK_CLIP")?o=new g7(r.shape):o=new f7(r.shape);let l=o.getCustomSetupFunc(s,i);return n.runWebGLProgram(o,[r],r.dtype,l)}var b7={kernelName:Vr,backendName:"webgl",kernelFunc:y7},x7=class{constructor(e){this.variableNames=["real","imag"],this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
float re = abs ( getRealAtOutCoords ( ) ) ;
float im = abs ( getImagAtOutCoords ( ) ) ;
float mx = max ( re , im ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ) )
) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function kT(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function v7(e){let{inputs:t,backend:n}=e,{x:a}=t,r=n.texData.get(a.dataId),s=new x7(a.shape),i=[kT(a,r.complexTensorInfos.real),kT(a,r.complexTensorInfos.imag)];return n.runWebGLProgram(s,i,i[0].dtype)}var w7={kernelName:Qu,backendName:"webgl",kernelFunc:v7},k7=class{constructor(e){this.outputShape=[],this.outputShape=E.computeOutShape(e,1),this.variableNames=e.map((s,i)=> ` T$ { i } ` );let t=new Array(e.length-1);t[0]=e[0][1];for(let s=1;s<t.length;s++)t[s]=t[s-1]+e[s][1];let n=[ ` if ( yC < $ { t [ 0 ] } ) setOutput ( getT0 ( yR , yC ) ) ; ` ];for(let s=1;s<t.length;s++){let i=t[s-1];n.push( ` else if ( yC < $ { t [ s ] } ) setOutput ( getT$ { s } ( yR , yC - $ { i } ) ) ; ` )}let a=t.length,r=t[t.length-1];n.push( ` else setOutput ( getT$ { a } ( yR , yC - $ { r } ) ) ; ` ),this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec2 coords = getOutputCoords ( ) ;
int yR = coords . x ;
int yC = coords . y ;
2021-01-12 16:01:34 +01:00
$ { n . join ( `
2020-12-15 14:15:43 +01:00
` )}
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},I7=class{constructor(e,t){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=E.computeOutShape(e,t);let n=this.outputShape,a=n.length,r=ht(a),s=fn("coords",a),i=["x","y","z","w","u","v"].slice(0,a);this.variableNames=e.map((m,f)=> ` T$ { f } ` );let o=new Array(e.length-1);o[0]=e[0][t];for(let m=1;m<o.length;m++)o[m]=o[m-1]+e[m][t];let l=i[t],u=i.slice(-2),c=i.join(),p= ` if ( $ { l } < $ { o [ 0 ] } ) {
2020-12-15 14:15:43 +01:00
return getChannel (
2021-03-09 23:32:33 +01:00
getT0 ( $ { c } ) , vec2 ( $ { u . join ( ) } ) ) ;
2021-01-12 16:01:34 +01:00
} ` ;for(let m=1;m<o.length;m++){let f=o[m-1];p+= `
if ( $ { l } < $ { o [ m ] } && $ { l } >= $ { o [ m - 1 ] } ) {
2020-12-15 14:15:43 +01:00
return getChannel (
2021-03-09 23:32:33 +01:00
getT$ { m } ( $ { Om ( i , l , f ) } ) ,
vec2 ( $ { Om ( u , l , f ) } ) ) ;
2021-01-12 16:01:34 +01:00
} ` }let d=o.length,h=o[o.length-1];p+= `
2020-12-15 14:15:43 +01:00
return getChannel (
2021-03-09 23:32:33 +01:00
getT$ { d } ( $ { Om ( i , l , h ) } ) ,
vec2 ( $ { Om ( u , l , h ) } ) ) ; ` ,this.userCode= `
2021-01-12 16:01:34 +01:00
float getValue ( $ { i . map ( m => "int " + m ) } ) {
$ { p }
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { r } coords = getOutputCoords ( ) ;
vec4 result = vec4 ( getValue ( $ { s } ) , 0. , 0. , 0. ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { s [ a - 1 ] } = $ { s [ a - 1 ] } + 1 ;
if ( $ { s [ a - 1 ] } < $ { n [ a - 1 ] } ) {
result . g = getValue ( $ { s } ) ;
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { s [ a - 2 ] } = $ { s [ a - 2 ] } + 1 ;
if ( $ { s [ a - 2 ] } < $ { n [ a - 2 ] } ) {
result . a = getValue ( $ { s } ) ;
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { s [ a - 1 ] } = $ { s [ a - 1 ] } - 1 ;
if ( $ { s [ a - 2 ] } < $ { n [ a - 2 ] } &&
$ { s [ a - 1 ] } < $ { n [ a - 1 ] } ) {
result . b = getValue ( $ { s } ) ;
2020-12-15 14:15:43 +01:00
}
setOutput ( result ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function Om(e,t,n){let a=e.indexOf(t);return e.map((r,s)=>s===a? ` $ { r } - $ { n } ` :r).join()}function Lm(e){let{inputs:t,backend:n}=e,{input:a}=t,r=n.texData.get(a.dataId);return Gn({inputs:{x:r.complexTensorInfos.imag},backend:n})}var N7={kernelName:Cd,backendName:"webgl",kernelFunc:Lm};function du(e,t,n){let a=e[0].dtype;if(a==="complex64"){let u=e.map(m=>fp({inputs:{input:m},backend:n})),c=e.map(m=>Lm({inputs:{input:m},backend:n})),p=du(u,t,n),d=du(c,t,n),h=hs({inputs:{real:p,imag:d},backend:n});return u.forEach(m=>n.disposeIntermediateTensorInfo(m)),c.forEach(m=>n.disposeIntermediateTensorInfo(m)),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),h}if(a==="string"){let{tensors2D:u,outShape:c}=IT(e,t,n),p=u.map(g=>({vals:n.readSync(g.dataId),shape:g.shape})),d=u[0].shape[0]===1,h= $ 8(p,c,a,d),m=E.computeOutShape(e.map(g=>g.shape),t),f=n.makeTensorInfo(m,a,h);return u.forEach(g=>n.disposeIntermediateTensorInfo(g)),f}if(e.length>Z().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let u=Math.floor(e.length/2),c=du(e.slice(0,u),t,n),p=du(e.slice(u),t,n),d=du([c,p],t,n);return n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(p),d}if(Z().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&e[0].shape.length>1){let u=new I7(e.map(c=>c.shape),t);return n.runWebGLProgram(u,e,a)}let{tensors2D:r,outShape:s}=IT(e,t,n),i=new k7(r.map(u=>u.shape)),o=n.runWebGLProgram(i,r,a);r.forEach(u=>n.disposeIntermediateTensorInfo(u));let l=ye({inputs:{x:o},attrs:{shape:s},backend:n});return n.disposeIntermediateTensorInfo(o),l}function IT(e,t,n){let a=E.computeOutShape(e.map(r=>r.shape),t);return{tensors2D:e.map(r=>ye({inputs:{x:r},attrs:{shape:[-1,I.sizeFromShape(r.shape.slice(t))]},backend:n})),outShape:a}}function NT(e){let{inputs:t,backend:n,attrs:a}=e,{axis:r}=a,s=I.parseAxisParam(r,t[0].shape)[0],i=E.computeOutShape(t.map(u=>u.shape),s);if(I.sizeFromShape(i)===0)return n.makeTensorInfo(i,t[0].dtype,[]);let o=t.filter(u=>I.sizeFromShape(u.shape)>0);if(o.length===1)return Gn({inputs:{x:o[0]},backend:n});let l=o.map(u=>u.shape);return E.assertParamsConsistent(l,s),du(o,s,n)}var T7={kernelName:Mo,backendName:"webgl",kernelFunc:NT},TT=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let s=e.padInfo.top,i=e.padInfo.left,o=e.strideHeight,l=e.strideWidth,u=e.dilationHeight,c=e.dilationWidth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,m=e.inChannels%4,f=e.dataFormat==="channelsLast",g=f?1:2,y=f?2:3,b=f?3:1,x="",v="";n&&(a?x= ` float activation ( float a ) {
2020-12-15 14:15:43 +01:00
float b = getPreluActivationWeightsAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { n }
2021-02-25 14:12:39 +01:00
} ` :r?x= ` float activation ( float a ) {
2020-12-15 14:15:43 +01:00
float b = getLeakyreluAlphaAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { n }
2021-02-25 14:12:39 +01:00
} ` :x= `
2020-12-15 14:15:43 +01:00
float activation ( float x ) {
2021-01-12 16:01:34 +01:00
$ { n }
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` ,v="result = activation(result);");let k=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
2021-02-25 14:12:39 +01:00
$ { x }
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
const ivec2 strides = ivec2 ( $ { o } , $ { l } ) ;
const ivec2 pads = ivec2 ( $ { s } , $ { i } ) ;
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec4 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
2021-01-12 16:01:34 +01:00
int d2 = coords [ $ { b } ] ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
ivec2 xRCCorner =
2021-01-12 16:01:34 +01:00
ivec2 ( coords [ $ { g } ] , coords [ $ { y } ] ) * strides - pads ;
2020-12-15 14:15:43 +01:00
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ;
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { p } ; wR ++ ) {
2021-03-09 23:32:33 +01:00
int xR = xRCorner + wR * $ { u } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { d } ; wC ++ ) {
2021-03-09 23:32:33 +01:00
int xC = xCCorner + wC * $ { c } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int d1 = 0 ; d1 < $ { h } ; d1 += 4 ) {
2020-12-15 14:15:43 +01:00
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 )
) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { f } ) {
2020-12-15 14:15:43 +01:00
vec4 xValues = vec4 (
getX ( batch , xR , xC , d1 ) ,
getX ( batch , xR , xC , d1 + 1 ) ,
getX ( batch , xR , xC , d1 + 2 ) ,
getX ( batch , xR , xC , d1 + 3 )
) ;
dotProd += dot ( xValues , wValues ) ;
} else {
vec4 xValues = vec4 (
getX ( batch , d1 , xR , xC ) ,
getX ( batch , d1 + 1 , xR , xC ) ,
getX ( batch , d1 + 2 , xR , xC ) ,
getX ( batch , d1 + 3 , xR , xC )
) ;
dotProd += dot ( xValues , wValues ) ;
}
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { m === 1 } ) {
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { f } ) {
2020-12-15 14:15:43 +01:00
dotProd +=
2021-01-12 16:01:34 +01:00
getX ( batch , xR , xC , $ { h } ) *
getW ( wR , wC , $ { h } , d2 ) ;
2020-12-15 14:15:43 +01:00
} else {
dotProd +=
2021-01-12 16:01:34 +01:00
getX ( batch , $ { h } , xR , xC ) *
getW ( wR , wC , $ { h } , d2 ) ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
} else if ( $ { m === 2 } ) {
2020-12-15 14:15:43 +01:00
vec2 wValues = vec2 (
2021-01-12 16:01:34 +01:00
getW ( wR , wC , $ { h } , d2 ) ,
getW ( wR , wC , $ { h } + 1 , d2 )
2020-12-15 14:15:43 +01:00
) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { f } ) {
2020-12-15 14:15:43 +01:00
vec2 xValues = vec2 (
2021-01-12 16:01:34 +01:00
getX ( batch , xR , xC , $ { h } ) ,
getX ( batch , xR , xC , $ { h } + 1 )
2020-12-15 14:15:43 +01:00
) ;
dotProd += dot ( xValues , wValues ) ;
} else {
vec2 xValues = vec2 (
2021-01-12 16:01:34 +01:00
getX ( batch , $ { h } , xR , xC ) ,
getX ( batch , $ { h } + 1 , xR , xC )
2020-12-15 14:15:43 +01:00
) ;
dotProd += dot ( xValues , wValues ) ;
}
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
} else if ( $ { m === 3 } ) {
2020-12-15 14:15:43 +01:00
vec3 wValues = vec3 (
2021-01-12 16:01:34 +01:00
getW ( wR , wC , $ { h } , d2 ) ,
getW ( wR , wC , $ { h } + 1 , d2 ) ,
getW ( wR , wC , $ { h } + 2 , d2 )
2020-12-15 14:15:43 +01:00
) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { f } ) {
2020-12-15 14:15:43 +01:00
vec3 xValues = vec3 (
2021-01-12 16:01:34 +01:00
getX ( batch , xR , xC , $ { h } ) ,
getX ( batch , xR , xC , $ { h } + 1 ) ,
getX ( batch , xR , xC , $ { h } + 2 )
2020-12-15 14:15:43 +01:00
) ;
dotProd += dot ( xValues , wValues ) ;
2020-10-12 20:28:19 +02:00
} else {
2020-12-15 14:15:43 +01:00
vec3 xValues = vec3 (
2021-01-12 16:01:34 +01:00
getX ( batch , $ { h } , xR , xC ) ,
getX ( batch , $ { h } + 1 , xR , xC ) ,
getX ( batch , $ { h } + 2 , xR , xC )
2020-12-15 14:15:43 +01:00
) ;
dotProd += dot ( xValues , wValues ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
2020-10-12 20:28:19 +02:00
}
}
2020-12-15 14:15:43 +01:00
}
float result = dotProd ;
2021-03-09 23:32:33 +01:00
$ { k }
2021-02-25 14:12:39 +01:00
$ { v }
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }},S7=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let t=e.padInfo.front,n=e.padInfo.top,a=e.padInfo.left,r=e.strideDepth,s=e.strideHeight,i=e.strideWidth,o=e.dilationDepth,l=e.dilationHeight,u=e.dilationWidth,c=e.filterDepth,p=e.filterHeight,d=e.filterWidth,h=Math.floor(e.inChannels/4)*4,m=e.inChannels%4;this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec3 strides = ivec3 ( $ { r } , $ { s } , $ { i } ) ;
const ivec3 pads = ivec3 ( $ { t } , $ { n } , $ { a } ) ;
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int d2 = coords . u ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ;
2021-03-09 23:32:33 +01:00
for ( int wF = 0 ; wF < $ { c } ; wF ++ ) {
2021-01-12 16:01:34 +01:00
int xF = xFCorner + wF * $ { o } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( xF < 0 || xF >= $ { e . inDepth } ) {
2020-12-15 14:15:43 +01:00
continue ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { p } ; wR ++ ) {
int xR = xRCorner + wR * $ { l } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { d } ; wC ++ ) {
2021-03-09 23:32:33 +01:00
int xC = xCCorner + wC * $ { u } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int d1 = 0 ; d1 < $ { h } ; d1 += 4 ) {
2020-12-15 14:15:43 +01:00
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 ) ;
}
2021-01-12 16:01:34 +01:00
if ( $ { m === 1 } ) {
2020-12-15 14:15:43 +01:00
dotProd +=
2021-01-12 16:01:34 +01:00
getX ( batch , xF , xR , xC , $ { h } ) *
getW ( wF , wR , wC , $ { h } , d2 ) ;
} else if ( $ { m === 2 } ) {
2020-12-15 14:15:43 +01:00
vec2 xValues = vec2 (
2021-01-12 16:01:34 +01:00
getX ( batch , xF , xR , xC , $ { h } ) ,
getX ( batch , xF , xR , xC , $ { h } + 1 )
2020-12-15 14:15:43 +01:00
) ;
vec2 wValues = vec2 (
2021-01-12 16:01:34 +01:00
getW ( wF , wR , wC , $ { h } , d2 ) ,
getW ( wF , wR , wC , $ { h } + 1 , d2 )
2020-12-15 14:15:43 +01:00
) ;
dotProd += dot ( xValues , wValues ) ;
2021-01-12 16:01:34 +01:00
} else if ( $ { m === 3 } ) {
2020-12-15 14:15:43 +01:00
vec3 xValues = vec3 (
2021-01-12 16:01:34 +01:00
getX ( batch , xF , xR , xC , $ { h } ) ,
getX ( batch , xF , xR , xC , $ { h } + 1 ) ,
getX ( batch , xF , xR , xC , $ { h } + 2 )
2020-12-15 14:15:43 +01:00
) ;
vec3 wValues = vec3 (
2021-01-12 16:01:34 +01:00
getW ( wF , wR , wC , $ { h } , d2 ) ,
getW ( wF , wR , wC , $ { h } + 1 , d2 ) ,
getW ( wF , wR , wC , $ { h } + 2 , d2 )
2020-12-15 14:15:43 +01:00
) ;
dotProd += dot ( xValues , wValues ) ;
}
}
}
}
setOutput ( dotProd ) ;
}
2021-03-09 23:32:33 +01:00
` }},C7=class{constructor(e,t,n){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e;let{filterWidth:a,inChannels:r,strideWidth:s,strideHeight:i,padInfo:o,outWidth:l,dilationWidth:u,dilationHeight:c,dataFormat:p}=n,{left:d,top:h}=o,m=r*a,f=mn(),g=p==="channelsLast",y=g?0:1,b=g?1:2,x="";for(let v=0;v<=1;v++)for(let k=0;k<=1;k++)x+= `
blockIndex = rc . y + $ { k } ;
2021-02-25 14:12:39 +01:00
pos = rc . x + $ { v } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( blockIndex < $ { e [ 1 ] } && pos < $ { e [ 0 ] } ) {
offsetY = int ( blockIndex / ( $ { l } ) ) * $ { i } - $ { h } ;
2021-03-09 23:32:33 +01:00
d0 = offsetY + $ { c } * ( pos / $ { m } ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( d0 < $ { t [ y ] } && d0 >= 0 ) {
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
offsetX = int ( mod ( float ( blockIndex ) , $ { l } . ) * $ { s } . - $ { d } . ) ;
2021-03-09 23:32:33 +01:00
d1 = offsetX + $ { u } * ( int ( mod ( float ( pos ) , $ { m } . ) / $ { r } . ) ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( d1 < $ { t [ b ] } && d1 >= 0 ) {
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
ch = int ( mod ( float ( pos ) , $ { r } . ) ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( $ { g } ) {
2020-10-12 20:28:19 +02:00
innerDims = vec2 ( d1 , ch ) ;
2021-03-09 23:32:33 +01:00
result [ $ { v * 2 + k } ] = getChannel (
2020-10-12 20:28:19 +02:00
getA ( d0 , int ( innerDims . x ) ,
int ( innerDims . y ) ) , innerDims ) ;
} else {
innerDims = vec2 ( d0 , d1 ) ;
2021-03-09 23:32:33 +01:00
result [ $ { v * 2 + k } ] = getChannel (
2020-10-12 20:28:19 +02:00
getA ( ch , int ( innerDims . x ) ,
int ( innerDims . y ) ) , innerDims ) ;
}
}
}
}
` ;this.userCode= `
void main ( ) {
ivec2 rc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0 ) ;
int blockIndex , pos , offsetY , d0 , offsetX , d1 , ch ;
vec2 innerDims ;
2021-02-25 14:12:39 +01:00
$ { x }
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { f . output } = result ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function ST({x:e,filter:t,convInfo:n,backend:a,bias:r=null,preluActivationWeights:s=null,leakyreluAlpha:i=0,activation:o=null}){let l=e.shape,u=a.texData.get(e.dataId),c=n.inChannels,p=l[0]*l[1]*l[2],d=n.outChannels,h=n.dataFormat==="channelsLast",m=!1,f=!1,g,y=[],b=(p===1||d===1)&&c>mT,x=l[2]%2!=0&&!!u.isPacked;if(b||!Z().getBool("WEBGL_LAZILY_UNPACK")||!Z().getBool("WEBGL_PACK_BINARY_OPERATIONS")||!x){let v=h?l[0]*l[1]*l[2]:l[0]*l[2]*l[3],k=ye({inputs:{x:e},backend:a,attrs:{shape:[1,v,n.inChannels]}}),w=ye({inputs:{x:t},backend:a,attrs:{shape:[1,n.inChannels,n.outChannels]}}),S=Rm({a:k,b:w,transposeA:m,transposeB:f,backend:a,bias:r,activation:o,preluActivationWeights:s,leakyreluAlpha:i});g=ye({inputs:{x:S},backend:a,attrs:{shape:n.outShape}}),y.push(k),y.push(w),y.push(S)}else{let v=h?l[0]*l[1]*(l[2]+1):l[0]*l[2]*(l[3]+1),k={dataId:e.dataId,shape:[1,v,n.inChannels],dtype:e.dtype},w=u.shape;u.shape=u.shape.slice(),u.shape[u.shape.length-2]++,I.assert(_m(u.shape,k.shape),()=> ` packed reshape $ { u . shape } to $ { k . shape } isn ' t free ` );let S=ye({inputs:{x:t},backend:a,attrs:{shape:[1,n.inChannels,n.outChannels]}});y.push(S);let A=Rm({a:k,b:S,backend:a,transposeA:m,transposeB:f,bias:r,activation:o,preluActivationWeights:s,leakyreluAlpha:i}),F=a.texData.get(A.dataId);I.assert(F.isPacked,()=>"batchMatMul result is expected to be packed"),u.shape=w,F.shape=n.outShape,g=Gn({inputs:{x:A},backend:a}),g.shape=n.outShape,y.push(A)}for(let v of y)a.disposeIntermediateTensorInfo(v);return g}function CT({x:e,filter:t,convInfo:n,backend:a,bias:r=null,preluActivationWeights:s=null,leakyreluAlpha:i=0,activation:o=null}){let{filterWidth:l,filterHeight:u,inChannels:c,outWidth:p,outHeight:d,dataFormat:h}=n,m=h==="channelsLast",f=l*u*c,g=d*p,y=[f,g],b=!0,x=!1,v=[],k=ye({inputs:{x:e},backend:a,attrs:{shape:e.shape.slice(1)}}),w=ye({inputs:{x:t},backend:a,attrs:{shape:[1,f,I.sizeFromShape(t.shape)/f]}});v.push(k),v.push(w);let S=new C7(y,k.shape,n),A=a.runWebGLProgram(S,[k],"float32"),F=ye({inputs:{x:A},backend:a,attrs:{shape:[1,y[0],y[1]]}});v.push(A),v.push(F);let D=r!=null,R=s!=null,L=o==="leakyrelu",W=o?Dm(o,!0):null,U=new lT(F.shape,w.shape,[1,g,n.outChannels],b,x,D,W,R,L),G=[F,w];if(r&&G.push(r),R&&G.push(s),L){let Q=a.makeTensorInfo([],"float32",I.createScalarValue(i,"float32"));G.push(Q),v.push(Q)}let X=a.runWebGLProgram(U,G,"float32"),j=m?[1,d,p,n.outChannels]:[1,n.outChannels,d,p],te=ye({inputs:{x:X},backend:a,attrs:{shape:j}});v.push(X);for(let Q of v)a.disposeIntermediateTensorInfo(Q);return te}function A7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dataFormat:l,dilations:u,dimRoundingMode:c}=a,p=E.convertConv2DDataFormat(l),d=E.computeConv2DInfo(r.shape,s.shape,i,u,o,c,!1,p),h;if(d.filterHeight===1&&d.filterWidth===1&&d.dilationHeight===1&&d.dilationWidth===1&&d.strideHeight===1&&d.strideWidth===1&&(d.padInfo.type==="SAME"||d.padInfo.type==="VALID"))h=ST({x:r,filter:s,convInfo:d,backend:n});else if(Z().getBool("WEBGL_CONV_IM2COL")&&r.shape[0]===1)h=CT({x:r,filter:s,convInfo:d,backend:n});else{let f=new TT(d);h=n.runWebGLProgram(f,[r,s],"float32")}let m=ye({inputs:{x:h},backend:n,attrs:{shape:d.outShape}});return n.disposeIntermediateTensorInfo(h),m}var F7={kernelName:_s,backendName:"webgl",kernelFunc:A7},_7=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,a=e.padInfo.top,r=e.padInfo.left,s=e.dataFormat==="channelsLast";this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
int wR = coords . x ;
int wC = coords . y ;
int d1 = coords . z ;
int d2 = coords . w ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int b = 0 ; b < $ { e . batchSize } ; b ++ ) {
for ( int yR = 0 ; yR < $ { e . outHeight } ; yR ++ ) {
int xR = wR + yR * $ { t } - $ { a } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int yC = 0 ; yC < $ { e . outWidth } ; yC ++ ) {
int xC = wC + yC * $ { n } - $ { r } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
continue ;
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
if ( $ { s } ) {
2020-12-15 14:15:43 +01:00
float dyValue = getDy ( b , yR , yC , d2 ) ;
float xValue = getX ( b , xR , xC , d1 ) ;
dotProd += ( xValue * dyValue ) ;
} else {
float dyValue = getDy ( b , d2 , yR , yC ) ;
float xValue = getX ( b , d1 , xR , xC ) ;
dotProd += ( xValue * dyValue ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
2020-10-12 20:28:19 +02:00
}
}
}
2020-12-15 14:15:43 +01:00
setOutput ( dotProd ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},E7=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,a=e.strideHeight,r=e.strideWidth,s=e.dataFormat==="channelsLast",i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,l=s?1:2,u=s?2:3,c=s?3:1;this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec2 pads = ivec2 ( $ { i } , $ { o } ) ;
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
int batch = coords [ 0 ] ;
2021-03-09 23:32:33 +01:00
int d1 = coords [ $ { c } ] ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
ivec2 dyCorner = ivec2 ( coords [ $ { l } ] , coords [ $ { u } ] ) - pads ;
2020-12-15 14:15:43 +01:00
int dyRCorner = dyCorner . x ;
int dyCCorner = dyCorner . y ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
2020-10-12 20:28:19 +02:00
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0 ;
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { t } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / $ { a } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
2020-10-12 20:28:19 +02:00
continue ;
}
int idyR = int ( dyR ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
int wRPerm = $ { t } - 1 - wR ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { n } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { r } . 0 ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-10-12 20:28:19 +02:00
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
int wCPerm = $ { n } - 1 - wC ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int d2 = 0 ; d2 < $ { e . outChannels } ; d2 ++ ) {
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( $ { s } ) {
2020-12-15 14:15:43 +01:00
float xValue = getDy ( batch , idyR , idyC , d2 ) ;
float wValue = getW ( wRPerm , wCPerm , d1 , d2 ) ;
dotProd += xValue * wValue ;
} else {
float xValue = getDy ( batch , d2 , idyR , idyC ) ;
float wValue = getW ( wRPerm , wCPerm , d1 , d2 ) ;
dotProd += xValue * wValue ;
}
}
}
}
setOutput ( dotProd ) ;
}
2021-03-09 23:32:33 +01:00
` }}, $ 7=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideDepth,n=e.strideHeight,a=e.strideWidth,r=e.padInfo.front,s=e.padInfo.top,i=e.padInfo.left;this.userCode= `
2020-12-15 14:15:43 +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 ;
float dotProd = 0.0 ;
2021-01-12 16:01:34 +01:00
for ( int b = 0 ; b < $ { e . batchSize } ; b ++ ) {
for ( int yF = 0 ; yF < $ { e . outDepth } ; yF ++ ) {
int xF = wF + yF * $ { t } - $ { r } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( xF < 0 || xF >= $ { e . inDepth } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int yR = 0 ; yR < $ { e . outHeight } ; yR ++ ) {
int xR = wR + yR * $ { n } - $ { s } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int yC = 0 ; yC < $ { e . outWidth } ; yC ++ ) {
int xC = wC + yC * $ { a } - $ { i } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
float dyValue = getDy ( b , yF , yR , yC , d2 ) ;
float xValue = getX ( b , xF , xR , xC , d1 ) ;
dotProd += ( xValue * dyValue ) ;
}
}
}
}
setOutput ( dotProd ) ;
}
2021-03-09 23:32:33 +01:00
` }},D7=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,a=e.filterWidth,r=e.strideDepth,s=e.strideHeight,i=e.strideWidth,o=t-1-e.padInfo.front,l=n-1-e.padInfo.top,u=a-1-e.padInfo.left;this.userCode= `
const ivec3 pads = ivec3 ( $ { o } , $ { l } , $ { u } ) ;
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int d1 = coords . u ;
2020-10-12 20:28:19 +02:00
ivec3 dyCorner = ivec3 ( coords . y , coords . z , coords . w ) - pads ;
2020-12-15 14:15:43 +01:00
int dyFCorner = dyCorner . x ;
2020-10-12 20:28:19 +02:00
int dyRCorner = dyCorner . y ;
int dyCCorner = dyCorner . z ;
float dotProd = 0.0 ;
2021-01-12 16:01:34 +01:00
for ( int wF = 0 ; wF < $ { t } ; wF ++ ) {
float dyF = float ( dyFCorner + wF ) / $ { r } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyF < 0.0 || dyF >= $ { e . outDepth } . 0 || fract ( dyF ) > 0.0 ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2020-12-15 14:15:43 +01:00
int idyF = int ( dyF ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int wFPerm = $ { t } - 1 - wF ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { n } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / $ { s } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyR ) > 0.0 ) {
2020-10-12 20:28:19 +02:00
continue ;
}
int idyR = int ( dyR ) ;
2021-01-12 16:01:34 +01:00
int wRPerm = $ { n } - 1 - wR ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { a } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { i } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-10-12 20:28:19 +02:00
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2021-01-12 16:01:34 +01:00
int wCPerm = $ { a } - 1 - wC ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int d2 = 0 ; d2 < $ { e . outChannels } ; d2 ++ ) {
2020-12-15 14:15:43 +01:00
float xValue = getDy ( batch , idyF , idyR , idyC , d2 ) ;
float wValue = getW ( wFPerm , wRPerm , wCPerm , d1 , d2 ) ;
dotProd += xValue * wValue ;
}
2020-10-12 20:28:19 +02:00
}
}
}
setOutput ( dotProd ) ;
}
2021-03-09 23:32:33 +01:00
` }};function M7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,dy:s}=t,{strides:i,pad:o,dataFormat:l,dimRoundingMode:u,filterShape:c}=a,p=E.convertConv2DDataFormat(l),d=E.computeConv2DInfo(r.shape,c,i,1,o,u,!1,p),h=new _7(d);return n.runWebGLProgram(h,[r,s],"float32")}var R7={kernelName:fd,backendName:"webgl",kernelFunc:M7};function P7(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,filter:s}=t,{inputShape:i,strides:o,pad:l,dataFormat:u,dimRoundingMode:c}=a,p=E.convertConv2DDataFormat(u),d=E.computeConv2DInfo(i,s.shape,o,1,l,c,!1,p),h=new E7(d);return n.runWebGLProgram(h,[r,s],"float32")}var O7={kernelName:Es,backendName:"webgl",kernelFunc:P7};function L7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dilations:l}=a,u=E.computeConv3DInfo(r.shape,s.shape,i,l,o),c=new S7(u);return n.runWebGLProgram(c,[r,s],"float32")}var z7={kernelName:Zu,backendName:"webgl",kernelFunc:L7};function W7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,dy:s}=t,{strides:i,pad:o,filterShape:l}=a,u=E.computeConv3DInfo(r.shape,l,i,1,o),c=new $ 7(u);return n.runWebGLProgram(c,[r,s],"float32")}var B7={kernelName:gd,backendName:"webgl",kernelFunc:W7};function V7(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,filter:s}=t,{pad:i,strides:o,inputShape:l}=a,u=E.computeConv3DInfo(l,s.shape,o,1,i),c=new D7(u);return n.runWebGLProgram(c,[r,s],"float32")}var U7={kernelName:yd,backendName:"webgl",kernelFunc:V7},G7=oT+ `
2020-12-15 14:15:43 +01:00
return cos ( x ) ;
2021-03-09 23:32:33 +01:00
` ,H7=Xe({opSnippet:G7}),j7={kernelName: $ s,backendName:"webgl",kernelFunc:H7},q7= `
2020-12-15 14:15:43 +01:00
float e2x = exp ( - x ) ;
return ( e2x + 1.0 / e2x ) / 2.0 ;
2021-03-09 23:32:33 +01:00
` ,X7=Xe({opSnippet:q7}),K7={kernelName:Ro,backendName:"webgl",kernelFunc:X7},Y7=class{constructor(e,t,n,a,r){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[s,i,o,l]=e,[u]=t,[c,p]=n;this.outputShape=[u,c,p,l];let d=a==="bilinear"?1:0,[h,m]=[ ` $ { i - 1 } . 0 ` , ` $ { o - 1 } . 0 ` ],[f,g,y]=c>1?[ ` $ { ( i - 1 ) / ( c - 1 ) } ` ,"(y2-y1) * height_ratio", ` y1 * $ { h } + float ( y ) * ( height _scale ) ` ]:["0.0","0.0", ` 0.5 * ( y1 + y2 ) * $ { h } ` ],[b,x,v]=p>1?[ ` $ { ( o - 1 ) / ( p - 1 ) } ` ,"(x2-x1) * width_ratio", ` x1 * $ { m } + float ( x ) * ( width _scale ) ` ]:["0.0","0.0", ` 0.5 * ( x1 + x2 ) * $ { m } ` ];this.userCode= `
2021-01-12 16:01:34 +01:00
const float height _ratio = float ( $ { f } ) ;
const float width _ratio = float ( $ { b } ) ;
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int y = coords [ 1 ] ;
int x = coords [ 2 ] ;
int d = coords [ 3 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// get image in batch index
int bInd = round ( getBoxInd ( b ) ) ;
2021-01-12 16:01:34 +01:00
if ( bInd < 0 || bInd >= $ { s } ) {
2020-12-15 14:15:43 +01:00
return ;
2020-10-12 20:28:19 +02:00
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
float height _scale = $ { g } ;
2021-02-25 14:12:39 +01:00
float width _scale = $ { x } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
float in _y = $ { y } ;
if ( in _y < 0.0 || in _y > $ { h } ) {
setOutput ( float ( $ { r } ) ) ;
2020-12-15 14:15:43 +01:00
return ;
}
2021-02-25 14:12:39 +01:00
float in _x = $ { v } ;
2021-01-12 16:01:34 +01:00
if ( in _x < 0.0 || in _x > $ { m } ) {
setOutput ( float ( $ { r } ) ) ;
2020-12-15 14:15:43 +01:00
return ;
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
vec2 sourceFracIndexCR = vec2 ( in _x , in _y ) ;
2021-01-12 16:01:34 +01:00
if ( $ { d } == 1 ) {
2020-12-15 14:15:43 +01:00
// Compute the four integer indices.
ivec2 sourceFloorCR = ivec2 ( sourceFracIndexCR ) ;
ivec2 sourceCeilCR = ivec2 ( ceil ( sourceFracIndexCR ) ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
vec2 fracCR = sourceFracIndexCR - vec2 ( sourceFloorCR ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ) ;
2020-10-12 20:28:19 +02:00
}
}
2021-03-09 23:32:33 +01:00
` }},J7=e=>{let{inputs:t,backend:n,attrs:a}=e,{image:r,boxes:s,boxInd:i}=t,{cropSize:o,method:l,extrapolationValue:u}=a,c=new Y7(r.shape,s.shape,o,l,u);return n.runWebGLProgram(c,[r,s,i],"float32")},Q7={kernelName:Po,backendName:"webgl",kernelFunc:J7},_T=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=e;let a=e.length,r=t?"0.0": ` getX ( $ { AT ( a , "coords" ) } ) ` ,s=e[e.length-1],i="",o="";t?(i=n? ` end != $ { s - 1 } ` :"end != 0",o=n?"end + 1":"end - 1"):(i=n? ` end + pow2 < $ { s } ` :"end >= pow2",o=n?"end + pow2":"end - pow2"),this.userCode= `
2020-12-15 14:15:43 +01:00
uniform float index ;
2020-10-12 20:28:19 +02:00
void main ( ) {
2021-03-09 23:32:33 +01:00
$ { ht ( a ) } coords = getOutputCoords ( ) ;
int end = $ { FT ( a , "coords" ) } ;
2021-01-12 16:01:34 +01:00
float val = $ { r } ;
2020-12-15 14:15:43 +01:00
int pow2 = int ( pow ( 2.0 , index ) ) ;
2021-01-12 16:01:34 +01:00
if ( $ { i } ) {
int idx = $ { o } ;
2021-03-09 23:32:33 +01:00
$ { FT ( a , "coords" ) } = idx ;
val += getX ( $ { AT ( a , "coords" ) } ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( val ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){return(t,n)=>{this.index==null&&(this.index=t.getUniformLocation(n,"index")),t.gl.uniform1f(this.index,e)}}};function AT(e,t){if(e===1)return ` $ { t } ` ;if(e===2)return ` $ { t } . x , $ { t } . y ` ;if(e===3)return ` $ { t } . x , $ { t } . y , $ { t } . z ` ;if(e===4)return ` $ { t } . x , $ { t } . y , $ { t } . z , $ { t } . w ` ;throw Error( ` Cumulative sum for rank $ { e } is not yet supported ` )}function FT(e,t){if(e===1)return ` $ { t } ` ;if(e===2)return ` $ { t } . y ` ;if(e===3)return ` $ { t } . z ` ;if(e===4)return ` $ { t } . w ` ;throw Error( ` Cumulative sum for rank $ { e } is not yet supported ` )}function Z7(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,exclusive:i,reverse:o}=a,l=r.shape.length,u=E.getAxesPermutation([s],l),c=r;u!=null&&(c=_n({inputs:{x:r},backend:n,attrs:{perm:u}}));let p=E.getInnerMostAxes(1,l)[0];if(p!==l-1)throw new Error( ` WebGL cumsum shader expects an inner - most axis = $ { r . shape . length - 1 } but got axis = $ { s } ` );let d=c.shape[p],h=Gn({inputs:{x:c},backend:n});for(let m=0;m<=Math.ceil(Math.log2(d))-1;m++){let f=new _T(c.shape,!1,o),g=f.getCustomSetupFunc(m),y=h;h=n.runWebGLProgram(f,[h],h.dtype,g),n.disposeIntermediateTensorInfo(y)}if(i){let m=new _T(c.shape,i,o),f=h;h=n.runWebGLProgram(m,[h],h.dtype),n.disposeIntermediateTensorInfo(f)}if(u!=null){let m=E.getUndoAxesPermutation(u),f=_n({inputs:{x:h},backend:n,attrs:{perm:m}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(c),f}return h}var eY={kernelName:Ds,backendName:"webgl",kernelFunc:Z7};function tY(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,weights:s}=t,{size:i,binaryOutput:o}=a;if(r.shape.length===1){let l=n.readSync(r.dataId),u=n.readSync(s.dataId),c=X2(l,u,s.dtype,s.shape,i);return n.makeTensorInfo([i],s.dtype,c)}else if(r.shape.length===2){let l=n.bufferSync(r),u=n.bufferSync(s),c=_8(l,u,i,o);return n.makeTensorInfo(c.shape,s.dtype,c.values)}throw new Error( ` Error in denseBincount : input must be at most rank 2 , but got rank$ { r . shape . length } . ` )}var nY={kernelName:bd,backendName:"webgl",kernelFunc:tY},aY=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int h = $ { this . getHeightCoordString ( ) } ;
int w = $ { this . getWidthCoordString ( ) } ;
int d = $ { this . getDepthCoordString ( ) } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
int in _h = h / $ { t } ;
int offset _h = imod ( h , $ { t } ) ;
int in _w = w / $ { t } ;
int offset _w = imod ( w , $ { t } ) ;
int offset _d = ( offset _h * $ { t } + offset _w ) *
2020-12-15 14:15:43 +01:00
$ { this . getOutputDepthSize ( ) } ;
int in _d = d + offset _d ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float result = $ { this . getInputSamplingString ( ) } ;
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }getHeightCoordString(){return this.dataFormat==="NHWC"?"coords[1]":"coords[2]"}getWidthCoordString(){return this.dataFormat==="NHWC"?"coords[2]":"coords[3]"}getDepthCoordString(){return this.dataFormat==="NHWC"?"coords[3]":"coords[1]"}getOutputDepthSize(){return this.dataFormat==="NHWC"?this.outputShape[3]:this.outputShape[1]}getInputSamplingString(){return this.dataFormat==="NHWC"?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"}};function rY(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{blockSize:s,dataFormat:i}=a;I.assert(s>1,()=> ` blockSize should be > 1 for depthToSpace , but was : $ { s } ` );let o=r.shape[0],l=i==="NHWC"?r.shape[1]:r.shape[2],u=i==="NHWC"?r.shape[2]:r.shape[3],c=i==="NHWC"?r.shape[3]:r.shape[1],p=l*s,d=u*s,h=c/(s*s),m=i==="NHWC"?[o,p,d,h]:[o,h,p,d],f=new aY(m,s,i);return n.runWebGLProgram(f,[r],r.dtype)}var sY={kernelName:Oo,backendName:"webgl",kernelFunc:rY},ET=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let s=e.inHeight,i=e.inWidth,o=e.padInfo.top,l=e.padInfo.left,u=e.strideHeight,c=e.strideWidth,p=e.dilationHeight,d=e.dilationWidth,h=e.filterHeight,m=e.filterWidth,f=e.outChannels/e.inChannels,g="",y="";n&&(a?g= ` float activation ( float a ) {
2020-12-15 14:15:43 +01:00
float b = getPreluActivationWeightsAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { n }
} ` :r?g= ` float activation ( float a ) {
2020-12-15 14:15:43 +01:00
float b = getLeakyreluAlphaAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { n }
} ` :g= `
2020-12-15 14:15:43 +01:00
float activation ( float x ) {
2021-01-12 16:01:34 +01:00
$ { n }
2020-10-12 20:28:19 +02:00
}
2021-01-12 16:01:34 +01:00
` ,y="result = activation(result);");let b=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
$ { g }
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
const ivec2 strides = ivec2 ( $ { u } , $ { c } ) ;
2021-01-12 16:01:34 +01:00
const ivec2 pads = ivec2 ( $ { o } , $ { l } ) ;
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
int batch = coords . x ;
2020-10-12 20:28:19 +02:00
ivec2 xRCCorner = coords . yz * strides - pads ;
2020-12-15 14:15:43 +01:00
int d2 = coords . w ;
2021-01-12 16:01:34 +01:00
int d1 = d2 / $ { f } ;
int q = d2 - d1 * $ { f } ;
2020-12-15 14:15:43 +01:00
2020-10-12 20:28:19 +02:00
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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.
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { h } ; wR ++ ) {
int xR = xRCorner + wR * $ { p } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { s } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { m } ; wC ++ ) {
int xC = xCCorner + wC * $ { d } ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { i } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float xVal = getX ( batch , xR , xC , d1 ) ;
float wVal = getW ( wR , wC , d1 , q ) ;
dotProd += xVal * wVal ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float result = dotProd ;
2021-01-12 16:01:34 +01:00
$ { b }
$ { y }
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }}, $ T=class{constructor(e,t=!1,n=null,a=!1,r=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e.outShape;let s=e.inHeight,i=e.inWidth,o=e.padInfo.top,l=e.padInfo.left,u=e.strideHeight,c=e.strideWidth,p=e.dilationHeight,d=e.dilationWidth,h=e.filterHeight,m=e.filterWidth,f=m,g="int xR; int xC; int xCOffset;";for(let v=0;v<h;v++)for(let k=0;k<m;k++)g+= `
vec4 xTexelR$ { v } C$ { k * 2 } = vec4 ( 0. ) ;
vec4 wR$ { v } C$ { k } = vec4 ( 0. ) ;
vec4 xR$ { v } C$ { k } = vec4 ( 0. ) ; ` ;for(let v=0;v<h;v++)for(let k=0;k<f;k++){let w=k*2;if(g+= `
2021-02-25 14:12:39 +01:00
xR = xRCorner + $ { v * p } ;
2021-03-09 23:32:33 +01:00
xC = xCCorner + $ { w * d } ;
` ,c===1){if(w<m&&(l%2==1?g+= `
2020-12-15 14:15:43 +01:00
xCOffset = xC + 1 ;
2021-01-12 16:01:34 +01:00
if ( xR >= 0 && xR < $ { s } && xCOffset >= 0 && xCOffset < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = getX ( batch , xR , xCOffset , d1 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
2021-01-12 16:01:34 +01:00
if ( xCOffset + 1 >= $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } . zw = vec2 ( 0. ) ;
2020-12-15 14:15:43 +01:00
}
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = vec4 ( 0. ) ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
xCOffset = xC + 1 - 2 ;
2021-01-12 16:01:34 +01:00
if ( xR >= 0 && xR < $ { s } && xCOffset >= 0 && xCOffset < $ { i } ) {
2020-12-15 14:15:43 +01:00
vec4 previous = getX ( batch , xR , xCOffset , d1 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
2021-01-12 16:01:34 +01:00
if ( xCOffset + 1 >= $ { i } ) {
2020-12-15 14:15:43 +01:00
previous . zw = vec2 ( 0. ) ;
}
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w } = vec4 ( previous . zw , xTexelR$ { v } C$ { w } . xy ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w } = vec4 ( 0 , 0 , xTexelR$ { v } C$ { w } . xy ) ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
` :g+= `
if ( xR >= 0 && xR < $ { s } && xC >= 0 && xC < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = getX ( batch , xR , xC , d1 ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = vec4 ( 0. ) ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w } = xTexelR$ { v } C$ { w } ;
` ,w+1<m)){let S=l%2==0?I.nearestLargerEven(d):d;d%2==0&&l%2==1||d%2!=0&&l%2!=1?(g+= `
2021-02-25 14:12:39 +01:00
xCOffset = xC + $ { l % 2 } + $ { S } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xR >= 0 && xR < $ { s } &&
xCOffset >= 0 && xCOffset < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w + 2 } = getX ( batch , xR , xCOffset , d1 ) ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
` ,d>1&&(g+= `
2020-12-15 14:15:43 +01:00
xCOffset -= 2 ;
2021-01-12 16:01:34 +01:00
if ( xR >= 0 && xR < $ { s } &&
xCOffset >= 0 && xCOffset < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = getX ( batch , xR , xCOffset , d1 ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = vec4 ( 0. ) ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
` ),g+= `
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w + 1 } = vec4 (
xTexelR$ { v } C$ { w } . zw , xTexelR$ { v } C$ { w + 2 } . xy ) ;
2021-01-12 16:01:34 +01:00
` ):g+= `
2021-02-25 14:12:39 +01:00
xCOffset = xC + $ { S } ;
2021-01-12 16:01:34 +01:00
if ( xR >= 0 && xR < $ { s } &&
xCOffset >= 0 && xCOffset < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w + 2 } = getX ( batch , xR , xCOffset , d1 ) ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w + 1 } = xTexelR$ { v } C$ { w + 2 } ;
` }}else w<m&&(g+= `
2021-01-12 16:01:34 +01:00
if ( xR >= 0 && xR < $ { s } ) {
` ,l%2==1?(g+= `
2021-03-09 23:32:33 +01:00
xCOffset = xC + 1 - $ { c } ;
2021-01-12 16:01:34 +01:00
if ( xCOffset >= 0 && xCOffset < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = getX ( batch , xR , xCOffset , d1 ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = vec4 ( 0. ) ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xC + 1 >= 0 && xC + 1 < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w + 2 } = getX ( batch , xR , xC + 1 , d1 ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w + 2 } = vec4 ( 0. ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w } = vec4 (
xTexelR$ { v } C$ { w } . zw , xTexelR$ { v } C$ { w + 2 } . zw ) ;
` ,w+1<m&&(g+= `
2020-12-15 14:15:43 +01:00
vec4 final = vec4 ( 0. ) ;
2021-03-09 23:32:33 +01:00
xCOffset = xC + 1 + $ { c } ;
2021-01-12 16:01:34 +01:00
if ( xCOffset >= 0 && xCOffset < $ { i } ) {
2020-12-15 14:15:43 +01:00
final = getX ( batch , xR , xCOffset , d1 ) ;
}
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w + 1 } = vec4 ( xTexelR$ { v } C$ { w + 2 } . xy , final . xy ) ;
2021-01-12 16:01:34 +01:00
` )):(g+= `
if ( xC >= 0 && xC < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = getX ( batch , xR , xC , d1 ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w } = vec4 ( 0. ) ;
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
xCOffset = xC + $ { c } ;
2021-01-12 16:01:34 +01:00
if ( xCOffset >= 0 && xCOffset < $ { i } ) {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w + 2 } = getX ( batch , xR , xCOffset , d1 ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-03-09 23:32:33 +01:00
xTexelR$ { v } C$ { w + 2 } = vec4 ( 0. ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
xR$ { v } C$ { w } = vec4 (
xTexelR$ { v } C$ { w } . xy , xTexelR$ { v } C$ { w + 2 } . xy ) ;
` ,w+1<m&&(g+= `
xR$ { v } C$ { w + 1 } = vec4 (
xTexelR$ { v } C$ { w } . zw , xTexelR$ { v } C$ { w + 2 } . zw ) ;
` )),g+="}");w<m&&(g+= `
vec4 wTexelR$ { v } C$ { w } = getW ( $ { v } , $ { w } , d1 , q ) ;
wR$ { v } C$ { w } = vec4 ( wTexelR$ { v } C$ { w } . xz , wTexelR$ { v } C$ { w } . xz ) ;
` ,w+1<m&&(g+= `
vec4 wTexelR$ { v } C$ { w + 1 } = getW ( $ { v } , $ { w + 1 } , d1 , q ) ;
wR$ { v } C$ { w + 1 } =
vec4 ( wTexelR$ { v } C$ { w + 1 } . xz , wTexelR$ { v } C$ { w + 1 } . xz ) ; ` ))}for(let v=0;v<h;v++)for(let k=0;k<m;k++)g+= ` dotProd += xR$ { v } C$ { k } * wR$ { v } C$ { k } ; ` ;let y="",b="";n&&(a?y= ` vec4 activation ( vec4 a ) {
2020-12-15 14:15:43 +01:00
vec4 b = getPreluActivationWeightsAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { n }
} ` :r?y= ` vec4 activation ( vec4 a ) {
2020-12-15 14:15:43 +01:00
vec4 b = getLeakyreluAlphaAtOutCoords ( ) ;
2021-01-12 16:01:34 +01:00
$ { n }
} ` :y= ` vec4 activation ( vec4 x ) {
$ { n }
2021-02-25 14:12:39 +01:00
} ` ,b="result = activation(result);");let x=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),a&&this.variableNames.push("preluActivationWeights"),r&&this.variableNames.push("leakyreluAlpha"),this.userCode= `
2021-01-12 16:01:34 +01:00
$ { y }
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
const ivec2 strides = ivec2 ( $ { u } , $ { c } ) ;
2021-01-12 16:01:34 +01:00
const ivec2 pads = ivec2 ( $ { o } , $ { l } ) ;
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec4 coords = getOutputCoords ( ) ;
2020-10-12 20:28:19 +02:00
int batch = coords . x ;
2020-12-15 14:15:43 +01:00
ivec2 xRCCorner = coords . yz * strides - pads ;
int d2 = coords . w ;
int d1 = d2 ;
int q = 0 ;
int xRCorner = xRCCorner . x ;
int xCCorner = xRCCorner . y ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
vec4 dotProd = vec4 ( 0. ) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
$ { g }
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
vec4 result = dotProd ;
2021-02-25 14:12:39 +01:00
$ { x }
2021-01-12 16:01:34 +01:00
$ { b }
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }};function iY(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dilations:l,dimRoundingMode:u}=a,c=l;c==null&&(c=[1,1]),I.assert(E.eitherStridesOrDilationsAreOne(i,c),()=> ` Error in depthwiseConv2d : Either strides or dilations must be 1. Got strides $ { i } and dilations '${c}' ` );let p=E.computeConv2DInfo(r.shape,s.shape,i,c,o,u,!0),d;return Z().getBool("WEBGL_PACK_DEPTHWISECONV")&&p.strideWidth<=2&&p.outChannels/p.inChannels==1?d=new $ T(p):d=new ET(p),n.runWebGLProgram(d,[r,s],"float32")}var oY={kernelName:Ms,backendName:"webgl",kernelFunc:iY},lY=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,a=e.padInfo.top,r=e.padInfo.left,s=e.outChannels/e.inChannels;this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int wR = coords . x ;
int wC = coords . y ;
int d1 = coords . z ;
int dm = coords . w ;
2021-01-12 16:01:34 +01:00
int d2 = d1 * $ { s } + dm ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float dotProd = 0.0 ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
// TO DO: Vec4 over the batch size
2021-01-12 16:01:34 +01:00
for ( int b = 0 ; b < $ { e . batchSize } ; b ++ ) {
for ( int yR = 0 ; yR < $ { e . outHeight } ; yR ++ ) {
int xR = wR + yR * $ { t } - $ { a } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xR < 0 || xR >= $ { e . inHeight } ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2021-01-12 16:01:34 +01:00
for ( int yC = 0 ; yC < $ { e . outWidth } ; yC ++ ) {
int xC = wC + yC * $ { n } - $ { r } ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( xC < 0 || xC >= $ { e . inWidth } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float dyValue = getDy ( b , yR , yC , d2 ) ;
float xValue = getX ( b , xR , xC , d1 ) ;
dotProd += ( xValue * dyValue ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
}
}
setOutput ( dotProd ) ;
}
2021-03-09 23:32:33 +01:00
` }},uY=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,a=e.strideHeight,r=e.strideWidth,s=t-1-e.padInfo.top,i=n-1-e.padInfo.left,o=e.outChannels/e.inChannels;this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec2 pads = ivec2 ( $ { s } , $ { i } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float dotProd = 0.0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { t } ; wR ++ ) {
float dyR = float ( dyRCorner + wR ) / $ { a } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
2020-12-15 14:15:43 +01:00
continue ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
int idyR = int ( dyR ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int wRPerm = $ { t } - 1 - wR ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { n } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { r } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int wCPerm = $ { n } - 1 - wC ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// TO DO: Vec4 over the channelMul
2021-01-12 16:01:34 +01:00
for ( int dm = 0 ; dm < $ { o } ; dm ++ ) {
int d2 = d1 * $ { o } + dm ;
2020-12-15 14:15:43 +01:00
float xValue = getDy ( batch , idyR , idyC , d2 ) ;
float wValue = getW ( wRPerm , wCPerm , d1 , dm ) ;
dotProd += xValue * wValue ;
}
}
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( dotProd ) ;
2020-08-29 20:56:29 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function cY(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,dy:s}=t,{strides:i,dilations:o,pad:l,dimRoundingMode:u,filterShape:c}=a,p=E.computeConv2DInfo(r.shape,c,i,o,l,u,!0),d=new lY(p);return n.runWebGLProgram(d,[r,s],"float32")}var pY={kernelName:xd,backendName:"webgl",kernelFunc:cY};function dY(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,filter:s}=t,{strides:i,dilations:o,pad:l,dimRoundingMode:u,inputShape:c}=a,p=E.computeConv2DInfo(c,s.shape,i,o,l,u,!0),d=new uY(p);return n.runWebGLProgram(d,[r,s],"float32")}var hY={kernelName:vd,backendName:"webgl",kernelFunc:dY},mY=class{constructor(e){this.variableNames=["X"],this.outputShape=[e,e],this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec2 coords = getOutputCoords ( ) ;
float val = coords [ 0 ] == coords [ 1 ] ? getX ( coords [ 0 ] ) : 0.0 ;
setOutput ( val ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function fY(e){let{inputs:t,backend:n}=e,{x:a}=t,r=[...a.shape,...a.shape],s=I.sizeFromShape(a.shape),i=ye({inputs:{x:a},backend:n,attrs:{shape:[s]}}),o=new mY(s),l=n.runWebGLProgram(o,[i],i.dtype),u=ye({inputs:{x:l},backend:n,attrs:{shape:r}});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(l),u}var gY={kernelName:wd,backendName:"webgl",kernelFunc:fY},yY=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:a,strideHeight:r,strideWidth:s,filterHeight:i,filterWidth:o,dilationHeight:l,dilationWidth:u}=e,{top:c,left:p}=a;this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec2 strides = ivec2 ( $ { r } , $ { s } ) ;
2021-03-09 23:32:33 +01:00
const ivec2 pads = ivec2 ( $ { c } , $ { p } ) ;
2020-12-15 14:15:43 +01:00
const float neg _infinity = - 3.4 e38 ;
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
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 ;
2021-01-12 16:01:34 +01:00
for ( int h = 0 ; h < $ { i } ; h ++ ) {
int hIn = hBeg + h * $ { l } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( hIn >= 0 && hIn < $ { t } ) {
for ( int w = 0 ; w < $ { o } ; w ++ ) {
2021-03-09 23:32:33 +01:00
int wIn = wBeg + w * $ { u } ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( wIn >= 0 && wIn < $ { n } ) {
2020-12-15 14:15:43 +01:00
float xVal = getX ( batch , hIn , wIn , d1 ) ;
float wVal = getW ( h , w , d1 ) ;
float val = xVal + wVal ;
if ( val > curVal ) {
curVal = val ;
}
}
}
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float result = curVal ;
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }};function bY(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s}=t,{strides:i,pad:o,dilations:l}=a,u=E.computeDilation2DInfo(r.shape,s.shape,i,o,"NHWC",l),c,p=new yY(u);c=n.runWebGLProgram(p,[r,s],"float32");let d=ye({inputs:{x:c},backend:n,attrs:{shape:u.outShape}});return n.disposeIntermediateTensorInfo(c),d}var xY={kernelName:ec,backendName:"webgl",kernelFunc:bY},vY="return (x >= 0.0) ? x : (exp(x) - 1.0);",wY= `
2020-12-15 14:15:43 +01:00
vec4 result ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,kY=Xe({opSnippet:vY,packedOpSnippet:wY}),IY={kernelName:Lo,backendName:"webgl",kernelFunc:kY},NY="return (b >= 1.0) ? a : a * (b + 1.0);",TY= `
2020-12-15 14:15:43 +01:00
vec4 bGTEZero = vec4 ( greaterThanEqual ( b , vec4 ( 0. ) ) ) ;
return ( bGTEZero * a ) + ( ( vec4 ( 1.0 ) - bGTEZero ) * ( a * ( b + vec4 ( 1.0 ) ) ) ) ;
2021-03-09 23:32:33 +01:00
` ,SY=e=>{let{inputs:t,backend:n}=e,{dy:a,y:r}=t,s=Z().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new dp(TY,a.shape,r.shape):new pu(NY,a.shape,r.shape);return n.runWebGLProgram(s,[a,r],a.dtype)},CY={kernelName:Nd,backendName:"webgl",kernelFunc:SY},AY= `
2020-12-15 14:15:43 +01:00
return vec4 ( equal ( a , b ) ) ;
2021-03-09 23:32:33 +01:00
` ,FY="return float(a == b);",_Y=sn({opSnippet:FY,packedOpSnippet:AY,dtype:"bool"}),EY={kernelName:Wo,backendName:"webgl",kernelFunc:_Y}, $ Y= `
2020-12-15 14:15:43 +01:00
// Error function is calculated approximately with elementary function.
// See "Handbook of Mathematical Functions with Formulas,
// Graphs, and Mathematical Tables", Abramowitz and Stegun.
2021-03-09 23:32:33 +01:00
float p = $ { E . ERF _P } ;
float a1 = $ { E . ERF _A1 } ;
float a2 = $ { E . ERF _A2 } ;
float a3 = $ { E . ERF _A3 } ;
float a4 = $ { E . ERF _A4 } ;
float a5 = $ { E . ERF _A5 } ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ) ) ;
2021-03-09 23:32:33 +01:00
` ,DY=Xe({opSnippet: $ Y}),MY={kernelName:zo,backendName:"webgl",kernelFunc:DY},DT="return exp(x);",MT=Xe({opSnippet:DT,packedOpSnippet:DT,cpuKernelImpl:D8}),RY={kernelName:Ps,backendName:"webgl",kernelFunc:MT};function Gv(e){let{inputs:t,attrs:n,backend:a}=e,{dim:r}=n,{input:s}=t,i=s.shape.length,o=s.shape.slice(),l=r;return r<0&&(I.assert(-(i+1)<=r,()=> ` Axis must be in the interval [ $ { - ( i + 1 ) } , $ { i } ] ` ),l=i+r+1),o.splice(l,0,1),ye({inputs:{x:s},backend:a,attrs:{shape:o}})}var PY={kernelName:Bo,backendName:"webgl",kernelFunc:Gv},RT="return exp(x) - 1.0;",OY=Xe({opSnippet:RT,packedOpSnippet:RT,cpuKernelImpl:M8}),LY={kernelName:Vo,backendName:"webgl",kernelFunc:OY},PT=class{constructor(e,t,n){this.variableNames=["real","imag"];let a=t[1];this.outputShape=t;let r=n? ` 2.0 * $ { Math . PI } ` : ` - 2.0 * $ { Math . PI } ` ,s=n? ` $ { a } . 0 ` :"1.0",i;if(e==="real")i="return real * expR - imag * expI;";else if(e==="imag")i="return real * expI + imag * expR;";else throw new Error( ` FFT component must be either "real" or "imag" , got $ { e } . ` );this.userCode= `
2021-01-12 16:01:34 +01:00
const float exponentMultiplier = $ { r } ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float unaryOpComplex ( float real , float expR , float imag , float expI ) {
2021-01-12 16:01:34 +01:00
$ { i }
2020-12-15 14:15:43 +01:00
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float mulMatDFT ( int batch , int index ) {
2021-01-12 16:01:34 +01:00
float indexRatio = float ( index ) / float ( $ { a } ) ;
2020-12-15 14:15:43 +01:00
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float result = 0.0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { a } ; i ++ ) {
2020-12-15 14:15:43 +01:00
// x = (-2|2 * PI / N) * index * i;
float x = exponentMultiplierTimesIndexRatio * float ( i ) ;
float expR = cos ( x ) ;
float expI = sin ( x ) ;
float real = getReal ( batch , i ) ;
float imag = getImag ( batch , i ) ;
result +=
2021-01-12 16:01:34 +01:00
unaryOpComplex ( real , expR , imag , expI ) / $ { s } ;
2020-08-29 20:56:29 +02:00
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
return result ;
2020-10-12 20:28:19 +02:00
}
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec2 coords = getOutputCoords ( ) ;
setOutput ( mulMatDFT ( coords [ 0 ] , coords [ 1 ] ) ) ;
}
2021-03-09 23:32:33 +01:00
` }};function OT(e,t,n){let a=n.texData.get(e.dataId),r=I.sizeFromShape(e.shape),s=e.shape[e.shape.length-1],i=r/s,o=ye({inputs:{x:e},backend:n,attrs:{shape:[i,s]}}),l=o.shape,u=new PT("real",l,t),c=new PT("imag",l,t),p=[{dataId:a.complexTensorInfos.real.dataId,dtype:a.complexTensorInfos.real.dtype,shape:l},{dataId:a.complexTensorInfos.imag.dataId,dtype:a.complexTensorInfos.imag.dtype,shape:l}],d=n.runWebGLProgram(u,p,"float32"),h=n.runWebGLProgram(c,p,"float32"),m=hs({inputs:{real:d,imag:h},backend:n});n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h);let f=ye({inputs:{x:m},backend:n,attrs:{shape:e.shape}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(m),f}function zY(e){let{inputs:t,backend:n}=e,{input:a}=t;return OT(a,!1,n)}var WY={kernelName:Td,backendName:"webgl",kernelFunc:zY},BY=class{constructor(e,t){this.outputShape=[],this.variableNames=["x"],this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
uniform float value ;
void main ( ) {
// Input can be obtained from uniform value.
setOutput ( value ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){return(t,n)=>{this.valueLoc==null&&(this.valueLoc=t.getUniformLocationNoThrow(n,"value")),t.gl.uniform1f(this.valueLoc,e)}}};function Hv(e){let{backend:t,attrs:n}=e,{shape:a,value:r}=n,{dtype:s}=n;if(s=s||I.inferDtype(r),s==="string"){let i=I.getArrayFromDType(s,I.sizeFromShape(a));return i.fill(r),t.makeTensorInfo(a,s,i)}else{let i=new BY(a,r),o=i.getCustomSetupFunc(r);return t.runWebGLProgram(i,[],s,o)}}var VY={kernelName:tc,backendName:"webgl",kernelFunc:Hv},UY=class{constructor(e){this.variableNames=["Image"],this.outputShape=[];let t=e[2];this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int x = coords [ 2 ] ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
int coordX = $ { t } - x ;
2020-12-15 14:15:43 +01:00
float outputValue ;
2021-01-12 16:01:34 +01:00
if ( coordX >= 0 && coordX < $ { t } ) {
2020-12-15 14:15:43 +01:00
outputValue = getImage ( coords [ 0 ] , coords [ 1 ] , coordX , coords [ 3 ] ) ;
} else {
outputValue = getImage ( coords [ 0 ] , coords [ 1 ] , coords [ 2 ] , coords [ 3 ] ) ;
}
setOutput ( outputValue ) ;
}
2021-03-09 23:32:33 +01:00
` }},GY={kernelName:Uo,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,a=t,r=new UY(n.shape);return a.runWebGLProgram(r,[n],n.dtype)}},LT="return floor(x);",HY=Xe({opSnippet:LT,packedOpSnippet:LT,cpuKernelImpl:R8}),jY={kernelName:Os,backendName:"webgl",kernelFunc:HY},qY= `
2020-12-15 14:15:43 +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 ;
}
2021-03-09 23:32:33 +01:00
` ,XY= `
2020-12-15 14:15:43 +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 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ] ) ;
}
if ( cond [ 1 ] ) {
result [ 1 ] = idiv ( ia [ 1 ] , ib [ 1 ] , s [ 1 ] ) ;
}
if ( cond [ 2 ] ) {
result [ 2 ] = idiv ( ia [ 2 ] , ib [ 2 ] , s [ 2 ] ) ;
}
if ( cond [ 3 ] ) {
result [ 3 ] = idiv ( ia [ 3 ] , ib [ 3 ] , s [ 3 ] ) ;
}
return vec4 ( result ) ;
2021-03-09 23:32:33 +01:00
` ,KY=sn({opSnippet:qY,packedOpSnippet:XY,dtype:"int32"}),YY={kernelName:Ls,backendName:"webgl",kernelFunc:KY},JY=class{constructor(e){this.variableNames=["A"];let t=mn(),[n,a]=e;this.outputShape=e,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec3 coords = getOutputCoords ( ) ;
int texR = coords [ 0 ] ;
int texC = coords [ 1 ] ;
int depth = coords [ 2 ] ;
2021-01-12 16:01:34 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) / vec2 ( $ { a } . 0 , $ { n } . 0 ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
vec4 values = $ { t . texture2D } ( A , uv ) ;
2020-12-15 14:15:43 +01:00
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 ;
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
setOutput ( floor ( value * 255.0 + 0.5 ) ) ;
2020-08-29 20:56:29 +02:00
}
2021-03-09 23:32:33 +01:00
` }},QY=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=mn(),[n,a]=e;this.outputShape=e,this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec3 coords = getOutputCoords ( ) ;
int texR = coords [ 0 ] ;
int texC = coords [ 1 ] ;
int depth = coords [ 2 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
for ( int row = 0 ; row <= 1 ; row ++ ) {
for ( int col = 0 ; col <= 1 ; col ++ ) {
texC = coords [ 1 ] + row ;
depth = coords [ 2 ] + col ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
vec2 uv = ( vec2 ( texC , texR ) + halfCR ) /
2021-01-12 16:01:34 +01:00
vec2 ( $ { a } . 0 , $ { n } . 0 ) ;
vec4 values = $ { t . texture2D } ( A , uv ) ;
2020-12-15 14:15:43 +01:00
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 ;
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
result [ row * 2 + col ] = floor ( value * 255.0 + 0.5 ) ;
}
}
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
$ { t . output } = result ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }},e9={kernelName:Wd,backendName:"webgl",kernelFunc:ZY},hu;function ZY(e){let{inputs:t,backend:n,attrs:a}=e,{pixels:r}=t,{numChannels:s}=a,i=typeof HTMLVideoElement!="undefined"&&r instanceof HTMLVideoElement,o=typeof HTMLImageElement!="undefined"&&r instanceof HTMLImageElement,[l,u]=i?[r.videoWidth,r.videoHeight]:[r.width,r.height],c=[u,l],p=[u,l,s];(o||i)&&(hu==null&&(hu=document.createElement("canvas").getContext("2d")),hu.canvas.width=l,hu.canvas.height=u,hu.drawImage(r,0,0,l,u),r=hu.canvas);let d=n.makeTensorInfo(c,"int32");n.texData.get(d.dataId).usage=na.PIXELS,n.gpgpu.uploadPixelDataToTexture(n.getTexture(d.dataId),r);let h=Z().getBool("WEBGL_PACK")?new QY(p):new JY(p),m=n.runWebGLProgram(h,[d],"int32");return n.disposeData(d.dataId),m}function t9(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s,bias:i,preluActivationWeights:o}=t,{strides:l,pad:u,dataFormat:c,dilations:p,dimRoundingMode:d,activation:h,leakyreluAlpha:m}=a,f=E.convertConv2DDataFormat(c),g=E.computeConv2DInfo(r.shape,s.shape,l,p,u,d,!1,f),y,b=[];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"))y=ST({x:r,filter:s,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:m});else if(Z().getBool("WEBGL_CONV_IM2COL")&&r.shape[0]===1)y=CT({x:r,filter:s,convInfo:g,backend:n,bias:i,activation:h,preluActivationWeights:o,leakyreluAlpha:m});else{let v=i!=null,k=o!=null,w=h==="leakyrelu",S=h?Dm(h,!1):null,A=new TT(g,v,S,k,w),F=[r,s];if(i&&F.push(i),o&&F.push(o),w){let D=n.makeTensorInfo([],"float32",I.createScalarValue(m,"float32"));F.push(D),b.push(D)}y=n.runWebGLProgram(A,F,"float32")}let x=ye({inputs:{x:y},backend:n,attrs:{shape:g.outShape}});return b.push(y),b.forEach(v=>n.disposeIntermediateTensorInfo(v)),x}var n9={kernelName:yi,backendName:"webgl",kernelFunc:t9};function a9(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,filter:s,bias:i,preluActivationWeights:o}=t,{strides:l,pad:u,dilations:c,dimRoundingMode:p,activation:d,leakyreluAlpha:h}=a,m=[],f=c;f==null&&(f=[1,1]),I.assert(E.eitherStridesOrDilationsAreOne(l,f),()=> ` Error in depthwiseConv2d : Either strides or dilations must be 1. Got strides $ { l } and dilations '${f}' ` );let g=E.computeConv2DInfo(r.shape,s.shape,l,f,u,p,!0),y=Z().getBool("WEBGL_PACK_DEPTHWISECONV")&&g.strideWidth<=2&&g.outChannels/g.inChannels==1,b=d?Dm(d,y):null,x=[r,s],v=i!=null,k=o!=null,w=d==="leakyrelu";if(v&&x.push(i),k&&x.push(o),w){let F=n.makeTensorInfo([],"float32",I.createScalarValue(h,"float32"));x.push(F),m.push(F)}let S;y?S=new $ T(g,v,b,k,w):S=new ET(g,v,b,k,w);let A=n.runWebGLProgram(S,x,"float32");return m.forEach(F=>n.disposeIntermediateTensorInfo(F)),A}var r9={kernelName:bi,backendName:"webgl",kernelFunc:a9},s9=class{constructor(e,t,n){this.sliceDim=e,this.strides=t,this.variableNames=["x","indices"],this.outputShape=n;let a=ht(t.length),r=ht(n.length),s=this.sliceDim>1?"strides[j]":"strides";this.userCode= `
2021-01-12 16:01:34 +01:00
$ { a } strides = $ { a } ( $ { this . strides } ) ;
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { r } coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
int flattenIndex = 0 ;
for ( int j = 0 ; j < $ { this . sliceDim } ; j ++ ) {
int index = round ( getIndices ( coords [ 0 ] , j ) ) ;
2021-01-12 16:01:34 +01:00
flattenIndex += index * $ { s } ;
2020-12-15 14:15:43 +01:00
}
setOutput ( getX ( flattenIndex , coords [ 1 ] ) ) ;
}
2021-03-09 23:32:33 +01:00
` }};function i9(e){let{inputs:t,backend:n}=e,{params:a,indices:r}=t,s=r.shape,i=s[s.length-1],[o,l,u,c]=E.prepareAndValidate(a,r),p=ye({inputs:{x:r},backend:n,attrs:{shape:[l,i]}}),d=ye({inputs:{x:a},backend:n,attrs:{shape:[I.sizeFromShape(a.shape)/u,u]}}),h=new s9(i,c,[l,u]),m=n.runWebGLProgram(h,[d,p],d.dtype),f=ye({inputs:{x:m},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(m),f}var o9={kernelName:Ho,backendName:"webgl",kernelFunc:i9},u9=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=ht(this.rank),a=l9(e,2);this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { n } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { a } ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function l9(e,t){let n=["resRC.x","resRC.y","resRC.z","resRC.w"],a=[];for(let r=0;r<e.length;r++)r===2?a.push("int(getIndices(resRC.x, resRC.z))"):a.push( ` $ { n [ r ] } ` );return a.join()}function c9(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,indices:s}=t,{axis:i,batchDims:o}=a,l=I.parseAxisParam(i,r.shape)[0],u=E.segment_util.collectGatherOpShapeInfo(r,s,l,o),c=I.sizeFromShape(s.shape),p=[],d=ye({inputs:{x:r},backend:n,attrs:{shape:[u.batchSize,u.outerSize,u.dimSize,u.sliceSize]}}),h=ye({inputs:{x:s},backend:n,attrs:{shape:[u.batchSize,c/u.batchSize]}});p.push(d),p.push(h);let m=[u.batchSize,u.outerSize,c/u.batchSize,u.sliceSize];if(n.shouldExecuteOnCPU([r,s])||r.dtype==="string"){let b=n.bufferSync(h),x=n.bufferSync(d),v=P8(x,b,m);return p.forEach(k=>n.disposeIntermediateTensorInfo(k)),n.makeTensorInfo(u.outputShape,v.dtype,v.values)}let f=new u9(d.shape,m),g=n.runWebGLProgram(f,[d,h],d.dtype);p.push(g);let y=ye({inputs:{x:g},backend:n,attrs:{shape:u.outputShape}});return p.forEach(b=>n.disposeIntermediateTensorInfo(b)),y}var p9={kernelName:Go,backendName:"webgl",kernelFunc:c9},d9="return float(a > b);",h9= `
2020-12-15 14:15:43 +01:00
return vec4 ( greaterThan ( a , b ) ) ;
2021-03-09 23:32:33 +01:00
` ,m9=sn({opSnippet:d9,packedOpSnippet:h9,cpuKernelImpl:O8,dtype:"bool"}),f9={kernelName:jo,backendName:"webgl",kernelFunc:m9},g9="return float(a >= b);",y9= `
2020-12-15 14:15:43 +01:00
return vec4 ( greaterThanEqual ( a , b ) ) ;
2021-03-09 23:32:33 +01:00
` ,b9=sn({opSnippet:g9,packedOpSnippet:y9,dtype:"bool"}),x9={kernelName:Ws,backendName:"webgl",kernelFunc:b9};function v9(e){let{inputs:t,backend:n}=e,{input:a}=t;return OT(a,!0,n)}var w9={kernelName:Sd,backendName:"webgl",kernelFunc:v9},k9="return float(!isnan(x) && !isinf(x));",I9=Xe({opSnippet:k9,dtype:"bool"}),N9={kernelName:qo,backendName:"webgl",kernelFunc:I9},T9="return float(isinf(x));",S9=Xe({opSnippet:T9,dtype:"bool"}),C9={kernelName:Xo,backendName:"webgl",kernelFunc:S9},A9="return float(isnan(x));",F9=Xe({opSnippet:A9,dtype:"bool"}),_9={kernelName:Ko,backendName:"webgl",kernelFunc:F9},E9="return float(a < b);", $ 9= `
2020-12-15 14:15:43 +01:00
return vec4 ( lessThan ( a , b ) ) ;
2021-03-09 23:32:33 +01:00
` ,D9=sn({opSnippet:E9,packedOpSnippet: $ 9,cpuKernelImpl:L8,dtype:"bool"}),M9={kernelName:Yo,backendName:"webgl",kernelFunc:D9},R9="return float(a <= b);",P9= `
2020-12-15 14:15:43 +01:00
return vec4 ( lessThanEqual ( a , b ) ) ;
2021-03-09 23:32:33 +01:00
` ,O9=sn({opSnippet:R9,packedOpSnippet:P9,dtype:"bool"}),L9={kernelName:Jo,backendName:"webgl",kernelFunc:O9};function z9(e){let{backend:t,attrs:n}=e,{start:a,stop:r,num:s}=n,i=z8(a,r,s);return t.makeTensorInfo([i.length],"float32",i)}var W9={kernelName:Ad,backendName:"webgl",kernelFunc:z9},B9= ` if ( x < 0.0 ) return NAN ;
return log ( x ) ; ` ,V9= `
2020-12-15 14:15:43 +01:00
vec4 result = log ( x ) ;
vec4 isNaN = vec4 ( lessThan ( x , vec4 ( 0.0 ) ) ) ;
result . r = isNaN . r == 1.0 ? NAN : result . r ;
result . g = isNaN . g == 1.0 ? NAN : result . g ;
result . b = isNaN . b == 1.0 ? NAN : result . b ;
result . a = isNaN . a == 1.0 ? NAN : result . a ;
return result ;
2021-03-09 23:32:33 +01:00
` ,U9=Xe({opSnippet:B9,packedOpSnippet:V9,cpuKernelImpl:W8}),G9={kernelName:Us,backendName:"webgl",kernelFunc:U9},H9="return log(1.0 + x);",j9=Xe({opSnippet:H9}),q9={kernelName:Qo,backendName:"webgl",kernelFunc:j9},X9="return float(a >= 1.0 && b >= 1.0);",K9= `
2020-12-15 14:15:43 +01:00
return vec4 (
vec4 ( greaterThanEqual ( a , vec4 ( 1.0 ) ) ) *
vec4 ( greaterThanEqual ( b , vec4 ( 1.0 ) ) ) ) ;
2021-03-09 23:32:33 +01:00
` ,Y9=sn({opSnippet:X9,packedOpSnippet:K9,dtype:"bool"}),J9={kernelName:Zo,backendName:"webgl",kernelFunc:Y9},Q9="return float(!(x >= 1.0));",Z9=Xe({opSnippet:Q9}),eJ={kernelName:nc,backendName:"webgl",kernelFunc:Z9},tJ="return float(a >= 1.0 || b >= 1.0);",nJ= `
2020-12-15 14:15:43 +01:00
return min (
vec4 ( greaterThanEqual ( a , vec4 ( 1.0 ) ) ) +
vec4 ( greaterThanEqual ( b , vec4 ( 1.0 ) ) ) ,
vec4 ( 1.0 ) ) ;
2021-03-09 23:32:33 +01:00
` ,aJ=sn({opSnippet:tJ,packedOpSnippet:nJ,dtype:"bool"}),rJ={kernelName:ac,backendName:"webgl",kernelFunc:aJ},sJ=class{constructor(e,t,n,a,r){this.variableNames=["x"],this.outputShape=[];let s=t,i=e[3]-1;this.outputShape=e;let o,l= ` float ( $ { n } ) + float ( $ { a } ) * sum ` ;r===.5?o= ` inversesqrt ( $ { l } ) ` :r===1?o= ` 1.0 / ( $ { l } ) ` :o= ` exp ( log ( $ { l } ) * float ( - $ { r } ) ) ; ` ,this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2020-12-15 14:15:43 +01:00
int d = coords [ 3 ] ;
float x = getX ( b , r , c , d ) ;
float sum = 0.0 ;
2021-01-12 16:01:34 +01:00
for ( int j = - $ { s } ; j <= $ { s } ; j ++ ) {
2020-12-15 14:15:43 +01:00
int idx = d + j ;
2021-01-12 16:01:34 +01:00
if ( idx >= 0 && idx <= $ { i } ) {
2020-12-15 14:15:43 +01:00
float z = getX ( b , r , c , idx ) ;
sum += z * z ;
}
}
2021-01-12 16:01:34 +01:00
float val = x * $ { o } ;
2020-12-15 14:15:43 +01:00
setOutput ( val ) ;
}
2021-03-09 23:32:33 +01:00
` }},iJ=class{constructor(e,t,n,a,r){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let s=t,i=e[3]-1;this.outputShape=e;let o,l= ` float ( $ { n } ) + float ( $ { a } ) * sum ` ;r===.5?o= ` inversesqrt ( $ { l } ) ` :r===1?o= ` 1.0 / ( $ { l } ) ` :o= ` exp ( log ( $ { l } ) * float ( - $ { r } ) ) ; ` ,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords . x ;
int r = coords . y ;
int c = coords . z ;
int d = coords . w ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
bool hasNextCol = d < $ { this . outputShape [ 3 ] } ;
bool hasNextRow = c < $ { this . outputShape [ 2 ] } ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
vec4 sum = vec4 ( 0. ) ;
vec4 xFragAtOutputCoords = getX ( b , r , c , d ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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
) ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
int firstChannel = d - $ { s } ;
2020-12-15 14:15:43 +01:00
vec2 cache = vec2 ( 0. ) ;
if ( firstChannel >= 0 ) {
vec4 firstChannelFrag = getX ( b , r , c , firstChannel ) ;
cache . x = getChannel ( firstChannelFrag , vec2 ( c , firstChannel ) ) ;
if ( hasNextRow ) {
cache . y = getChannel ( firstChannelFrag , vec2 ( c + 1 , firstChannel ) ) ;
}
}
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
ivec2 depth = ivec2 ( d , d + 1 ) ;
2021-01-12 16:01:34 +01:00
for ( int j = - $ { s } ; j <= $ { s } ; j ++ ) {
2020-12-15 14:15:43 +01:00
ivec2 idx = depth + j ;
bvec2 aboveLowerBound = greaterThanEqual ( idx , ivec2 ( 0 ) ) ;
2021-01-12 16:01:34 +01:00
bvec2 belowUpperBound = lessThanEqual ( idx , ivec2 ( $ { i } ) ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
bool depthInRange = aboveLowerBound . x && belowUpperBound . x ;
bool depthPlusOneInRange = aboveLowerBound . y && belowUpperBound . y ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +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 ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
vec4 result = xAtOutputCoords * $ { o } ;
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }},oJ=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{depthRadius:s,bias:i,alpha:o,beta:l}=a,u=Z().getBool("WEBGL_PACK_NORMALIZATION")?new iJ(r.shape,s,i,o,l):new sJ(r.shape,s,i,o,l);return n.runWebGLProgram(u,[r],r.dtype)},lJ={kernelName:rc,backendName:"webgl",kernelFunc:oJ},uJ=class{constructor(e,t,n,a,r){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=a,this.beta=r,this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float result = 0.0 ;
for ( int d = 0 ; d < $ { this . depth } ; ++ d ) {
2021-01-12 16:01:34 +01:00
int depthBegin = int ( max ( 0.0 , float ( d - $ { t } ) ) ) ;
2020-12-15 14:15:43 +01:00
int depthEnd = int ( min ( float ( $ { this . depth } ) ,
2021-01-12 16:01:34 +01:00
float ( d + $ { t } + 1 ) ) ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
const int MIN _DEPTH _BEGIN = 0 ;
const int MAX _DEPTH _END = $ { this . depth } ;
float norm = 0.0 ;
for ( int k = MIN _DEPTH _BEGIN ; k < MAX _DEPTH _END ; ++ k ) {
if ( k < depthBegin ) {
2020-10-12 20:28:19 +02:00
continue ;
}
2020-12-15 14:15:43 +01:00
else if ( k >= depthBegin && k < depthEnd ) {
norm += getInputImage ( b , r , c , k ) * getInputImage ( b , r , c , k ) ;
}
else {
break ;
}
}
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
norm = float ( $ { a } ) * norm + float ( $ { n } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
for ( int k = MIN _DEPTH _BEGIN ; k < MAX _DEPTH _END ; ++ k ) {
if ( k < depthBegin ) {
continue ;
}
else if ( k >= depthBegin && k < depthEnd ) {
2021-01-12 16:01:34 +01:00
float dyi = - 2.0 * float ( $ { a } )
* float ( $ { r } )
2020-12-15 14:15:43 +01:00
* getInputImage ( b , r , c , k ) * getOutputImage ( b , r , c , d )
/ n o r m ;
if ( k == d ) {
2021-01-12 16:01:34 +01:00
dyi += pow ( norm , - 1.0 * $ { r } ) ;
2020-12-15 14:15:43 +01:00
}
if ( k == coords [ 3 ] ) {
dyi *= getDy ( b , r , c , d ) ;
result += dyi ;
}
}
else {
break ;
2020-10-12 20:28:19 +02:00
}
}
2020-08-29 20:56:29 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }},cJ=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r,y:s,dy:i}=t,{depthRadius:o,bias:l,alpha:u,beta:c}=a,p=new uJ(r.shape,o,l,u,c);return n.runWebGLProgram(p,[r,s,i],r.dtype)},pJ={kernelName:Fd,backendName:"webgl",kernelFunc:cJ};function dJ(e,t,n,a){let r=I.sizeFromShape(t),s=I.sizeFromShape(e.shape)/r,i=ye({inputs:{x:e},attrs:{shape:[s,r]},backend:a}),o=ji(i,e.dtype,"max",a),l=ye({inputs:{x:o},attrs:{shape:n},backend:a});return a.disposeIntermediateTensorInfo(i),a.disposeIntermediateTensorInfo(o),l}function zT(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{reductionIndices:s,keepDims:i}=a,o=r.shape.length,l=I.parseAxisParam(s,r.shape),u=l,c=E.getAxesPermutation(u,o),p=c!=null,d=n.shouldExecuteOnCPU([r]),h=r;if(p){if(d){let b=n.texData.get(h.dataId).values,x=new Array(o);for(let w=0;w<x.length;w++)x[w]=r.shape[c[w]];let v=Ov(b,r.shape,r.dtype,c,x);h=n.makeTensorInfo(x,r.dtype);let k=n.texData.get(h.dataId);k.values=v}else h=Mm(r,c,n);u=E.getInnerMostAxes(u.length,o)}E.assertAxesAreInnerMostDims("max",u,o);let[m,f]=E.computeOutAndReduceShapes(h.shape,u),g=m;i&&(g=E.expandShapeToKeepDim(m,l));let y;if(d){let b=n.texData.get(h.dataId).values,x=B8(b,I.sizeFromShape(f),g,r.dtype);y=n.makeTensorInfo(g,r.dtype);let v=n.texData.get(y.dataId);v.values=x}else y=dJ(h,f,g,n);return p&&n.disposeIntermediateTensorInfo(h),y}var hJ={kernelName:Gs,backendName:"webgl",kernelFunc:zT},mJ=nT+ `
2020-12-15 14:15:43 +01:00
return max ( a , b ) ;
2021-03-09 23:32:33 +01:00
` ,fJ= `
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( max ( a , b ) ) ;
vec4 isNaN = min ( vec4 ( isnan ( a ) ) + vec4 ( isnan ( b ) ) , vec4 ( 1.0 ) ) ;
2021-03-09 23:32:33 +01:00
` + $ m+ `
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,gJ=sn({opSnippet:mJ,packedOpSnippet:fJ,cpuKernelImpl:V8}),yJ={kernelName:Hs,backendName:"webgl",kernelFunc:gJ};function bJ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t;cp(r,"maxPool");let{filterSize:s,strides:i,pad:o,dimRoundingMode:l}=a,u=1;I.assert(E.eitherStridesOrDilationsAreOne(i,u),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { i } and dilations '${u}' ` );let c=E.computePool2DInfo(r.shape,s,i,u,o,l);if(c.filterWidth===1&&c.filterHeight===1&&I.arraysEqual(c.inShape,c.outShape))return Gn({inputs:{x:r},backend:n});let p=new hp(c,"max",!1);return n.runWebGLProgram(p,[r],r.dtype)}var xJ={kernelName:js,backendName:"webgl",kernelFunc:bJ};function vJ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{filterSize:s,strides:i,pad:o,dataFormat:l,dimRoundingMode:u}=a,c=[1,1,1],p=E.computePool3DInfo(r.shape,s,i,c,o,u,l),d=new Bv(p,"max",!1);return n.runWebGLProgram(d,[r],r.dtype)}var wJ={kernelName:sc,backendName:"webgl",kernelFunc:vJ},kJ=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideHeight,n=e.strideWidth,a=e.dilationHeight,r=e.effectiveFilterHeight,s=e.effectiveFilterWidth,i=r-1-e.padInfo.top,o=s-1-e.padInfo.left,l=r*s-1;this.userCode= `
2021-01-12 16:01:34 +01:00
const ivec2 pads = ivec2 ( $ { i } , $ { o } ) ;
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
2020-12-15 14:15:43 +01:00
ivec2 dyRCCorner = coords . yz - pads ;
int dyRCorner = dyRCCorner . x ;
int dyCCorner = dyRCCorner . y ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { r } ;
wR += $ { a } ) {
float dyR = float ( dyRCorner + wR ) / $ { t } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 || fract ( dyR ) > 0.0 ) {
2020-12-15 14:15:43 +01:00
continue ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
int idyR = int ( dyR ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int wC = 0 ; wC < $ { s } ; wC ++ ) {
float dyC = float ( dyCCorner + wC ) / $ { n } . 0 ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyC ) > 0.0 ) {
continue ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
int idyC = int ( dyC ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
float dyValue = getDy ( b , idyR , idyC , d ) ;
2021-01-12 16:01:34 +01:00
int maxPosValue = $ { l } - int ( getMaxPos ( b , idyR , idyC , d ) ) ;
2020-08-29 20:56:29 +02:00
2020-12-15 14:15:43 +01:00
// Get the current value, check it against the value from the
// position matrix.
2021-01-12 16:01:34 +01:00
int curPosValue = wR * $ { s } + wC ;
2020-12-15 14:15:43 +01:00
float mask = float ( maxPosValue == curPosValue ? 1.0 : 0.0 ) ;
dotProd += dyValue * mask ;
}
}
setOutput ( dotProd ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }},IJ=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideDepth,n=e.strideHeight,a=e.strideWidth,r=e.dilationDepth,s=e.dilationHeight,i=e.dilationWidth,o=e.effectiveFilterDepth,l=e.effectiveFilterHeight,u=e.effectiveFilterWidth,c=o-1-e.padInfo.front,p=l-1-e.padInfo.top,d=u-1-e.padInfo.left,h=o*l*u-1;this.userCode= `
const ivec3 pads = ivec3 ( $ { c } , $ { p } , $ { d } ) ;
2020-08-29 20:56:29 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec5 coords = getOutputCoords ( ) ;
int batch = coords . x ;
int ch = coords . u ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
ivec3 dyCorner = ivec3 ( coords . y , coords . z , coords . w ) - pads ;
int dyDCorner = dyCorner . x ;
int dyRCorner = dyCorner . y ;
int dyCCorner = dyCorner . z ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +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 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int wD = 0 ; wD < $ { o } ;
wD += $ { r } ) {
float dyD = float ( dyDCorner + wD ) / $ { t } . 0 ;
2020-08-29 20:56:29 +02:00
2021-01-12 16:01:34 +01:00
if ( dyD < 0.0 || dyD >= $ { e . outDepth } . 0 || fract ( dyD ) > 0.0 ) {
2020-12-15 14:15:43 +01:00
continue ;
}
int idyD = int ( dyD ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int wR = 0 ; wR < $ { l } ;
wR += $ { s } ) {
float dyR = float ( dyRCorner + wR ) / $ { n } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyR < 0.0 || dyR >= $ { e . outHeight } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyR ) > 0.0 ) {
continue ;
}
int idyR = int ( dyR ) ;
2020-10-12 20:28:19 +02:00
2021-03-09 23:32:33 +01:00
for ( int wC = 0 ; wC < $ { u } ;
2021-01-12 16:01:34 +01:00
wC += $ { i } ) {
float dyC = float ( dyCCorner + wC ) / $ { a } . 0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
if ( dyC < 0.0 || dyC >= $ { e . outWidth } . 0 ||
2020-12-15 14:15:43 +01:00
fract ( dyC ) > 0.0 ) {
continue ;
}
int idyC = int ( dyC ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float dyValue = getDy ( batch , idyD , idyR , idyC , ch ) ;
2021-01-12 16:01:34 +01:00
int maxPosValue = $ { h } -
2020-12-15 14:15:43 +01:00
int ( getMaxPos ( batch , idyD , idyR , idyC , ch ) ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Get the current value, check it against the value from the
// position matrix.
int curPosValue =
2021-03-09 23:32:33 +01:00
wD * $ { l } * $ { u } +
wR * $ { u } + wC ;
2020-12-15 14:15:43 +01:00
float mask = float ( maxPosValue == curPosValue ? 1.0 : 0.0 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
dotProd += dyValue * mask ;
}
}
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
setOutput ( dotProd ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function NJ(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s}=t,i=s,{filterSize:o,strides:l,pad:u,dimRoundingMode:c}=a,p=[1,1,1],d=E.computePool3DInfo(i.shape,o,l,p,u,c),h=new Bv(d,"max",!0),m=n.runWebGLProgram(h,[i],i.dtype),f=new IJ(d),g=n.runWebGLProgram(f,[r,m],i.dtype);return n.disposeIntermediateTensorInfo(m),g}var TJ={kernelName:Ed,backendName:"webgl",kernelFunc:NJ};function SJ(e){let{inputs:t,backend:n,attrs:a}=e,{dy:r,input:s,output:i}=t,o=s;cp([s,i],"maxPoolGrad");let{filterSize:l,strides:u,pad:c,dimRoundingMode:p}=a,d=E.computePool2DInfo(o.shape,l,u,1,c,p),h=!0,m=new hp(d,"max",h),f=n.runWebGLProgram(m,[o],o.dtype),g=new kJ(d),y=n.runWebGLProgram(g,[r,f],o.dtype);return n.disposeIntermediateTensorInfo(f),y}var CJ={kernelName:_d,backendName:"webgl",kernelFunc:SJ};function AJ(e,t,n,a){let r=new hp(n,"max",!1),s=a.runWebGLProgram(r,[e],"float32");r=new hp(n,"max",!0,!0,t);let i=a.runWebGLProgram(r,[e],"float32");return[s,i]}var FJ={kernelName: $ d,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:a}=e,{filterSize:r,strides:s,pad:i,includeBatchInIndex:o}=t,l=n;I.assert(a.shape.length===4,()=> ` Error in maxPool : input must be rank 4 but got rank $ { a . shape . length } . ` );let u=[1,1];I.assert(E.eitherStridesOrDilationsAreOne(s,u),()=> ` Error in maxPool : Either strides or dilations must be 1. Got strides $ { s } and dilations '${u}' ` );let c=E.computePool2DInfo(a.shape,r,s,u,i),[p,d]=AJ(a,o,c,l);return[p,d]}};function _J(e,t,n,a){let r=I.sizeFromShape(t),s=I.sizeFromShape(e.shape)/r,i=ye({inputs:{x:e},attrs:{shape:[s,r]},backend:a}),o=ji(i,"float32","mean",a),l=ye({inputs:{x:o},attrs:{shape:n},backend:a});return a.disposeIntermediateTensorInfo(i),a.disposeIntermediateTensorInfo(o),l}var EJ={kernelName:qs,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:a}=e,{keepDims:r,axis:s}=t,i=n,o=a.shape.length,l=I.parseAxisParam(s,a.shape),u=l,c=E.getAxesPermutation(u,o),p=c!=null,d=i.shouldExecuteOnCPU([a]),h=[],m=a;if(p){if(d){let x=i.texData.get(m.dataId).values,v=new Array(o);for(let S=0;S<v.length;S++)v[S]=a.shape[c[S]];let k=Ov(x,a.shape,a.dtype,c,v);m=i.makeTensorInfo(v,a.dtype);let w=i.texData.get(m.dataId);w.values=k}else m=Mm(a,c,i);h.push(m),u=E.getInnerMostAxes(u.length,o)}E.assertAxesAreInnerMostDims("sum",u,o);let[f,g]=E.computeOutAndReduceShapes(m.shape,u),y=f;r&&(y=E.expandShapeToKeepDim(f,l));let b=_J(m,g,y,i);for(let x of h)i.disposeIntermediateTensorInfo(x);return b}};function $ J(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=I.parseAxisParam(s,r.shape),u=l,c=E.getAxesPermutation(u,o),p=r;c!=null&&(p=_n({inputs:{x:r},backend:n,attrs:{perm:c}}),u=E.getInnerMostAxes(u.length,r.shape.length)),E.assertAxesAreInnerMostDims("min",u,o);let[d,h]=E.computeOutAndReduceShapes(p.shape,u),m=I.sizeFromShape(h),f=ye({inputs:{x:p},backend:n,attrs:{shape:[-1,m]}}),g=ji(f,f.dtype,"min",n),y;if(i){let b=E.expandShapeToKeepDim(d,l);y=ye({inputs:{x:g},backend:n,attrs:{shape:b}})}else y=ye({inputs:{x:g},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(p),y}var DJ={kernelName:Xs,backendName:"webgl",kernelFunc: $ J},MJ=nT+ `
2020-12-15 14:15:43 +01:00
return min ( a , b ) ;
2021-03-09 23:32:33 +01:00
` ,RJ= `
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( min ( a , b ) ) ;
vec4 isNaN = min ( vec4 ( isnan ( a ) ) + vec4 ( isnan ( b ) ) , vec4 ( 1.0 ) ) ;
2021-03-09 23:32:33 +01:00
` + $ m+ `
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,PJ=sn({opSnippet:MJ,packedOpSnippet:RJ,cpuKernelImpl:U8}),OJ={kernelName:Ks,backendName:"webgl",kernelFunc:PJ},LJ=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=t.map((u,c)=>u[0]+e[c]+u[1]);let a=e.length,r=ht(a),s=t.map(u=>u[0]).join(","),i=t.map((u,c)=>u[0]+e[c]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,a),l=n==="reflect"?0:1;if(a===1){this.userCode= `
2021-01-12 16:01:34 +01:00
int start = $ { s } ;
int end = $ { i } ;
2020-12-15 14:15:43 +01:00
void main ( ) {
int outC = getOutputCoords ( ) ;
if ( outC < start ) {
2021-01-12 16:01:34 +01:00
outC = start * 2 - outC - $ { l } ;
2020-12-15 14:15:43 +01:00
} else if ( outC >= end ) {
2021-01-12 16:01:34 +01:00
outC = ( end - 1 ) * 2 - outC + $ { l } ;
2020-12-15 14:15:43 +01:00
}
setOutput ( getX ( outC - start ) ) ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
` ;return}this.userCode= `
2021-01-12 16:01:34 +01:00
$ { r } start = $ { r } ( $ { s } ) ;
$ { r } end = $ { r } ( $ { i } ) ;
2020-12-15 14:15:43 +01:00
2020-12-02 22:46:41 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { r } outC = getOutputCoords ( ) ;
for ( int i = 0 ; i < $ { a } ; i ++ ) {
2020-12-15 14:15:43 +01:00
if ( outC [ i ] < start [ i ] ) {
2021-01-12 16:01:34 +01:00
outC [ i ] = start [ i ] * 2 - outC [ i ] - $ { l } ;
2020-12-15 14:15:43 +01:00
} else if ( outC [ i ] >= end [ i ] ) {
2021-01-12 16:01:34 +01:00
outC [ i ] = ( end [ i ] - 1 ) * 2 - outC [ i ] + $ { l } ;
2020-12-15 14:15:43 +01:00
}
2020-12-02 22:46:41 +01:00
}
2021-01-12 16:01:34 +01:00
$ { r } coords = outC - start ;
setOutput ( getX ( $ { o } ) ) ;
2020-12-02 22:46:41 +01:00
}
2021-03-09 23:32:33 +01:00
` }},zJ=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.map((h,m)=>h[0]+e[m]+h[1]);let a=e.length,r=ht(a),s=t.map(h=>h[0]).join(","),i=t.map((h,m)=>h[0]+e[m]).join(","),o=fn("rc",a),l=fn("source",a),u= ` $ { o [ a - 1 ] } < $ { this . outputShape [ a - 1 ] } ` ,c=a===1?"source": ` vec2 ( $ { l . slice ( - 2 ) . join ( ) } ) ` ,p=n==="reflect"?0:1,d="";if(a===1){let h= `
2021-01-12 16:01:34 +01:00
$ { r } source = rc ;
2020-12-15 14:15:43 +01:00
if ( source < start ) {
2021-01-12 16:01:34 +01:00
source = start * 2 - source - $ { p } ;
2020-12-15 14:15:43 +01:00
} else if ( source >= end ) {
2021-01-12 16:01:34 +01:00
source = ( end - 1 ) * 2 - source + $ { p } ;
2020-12-15 14:15:43 +01:00
}
source -= start ;
2021-01-12 16:01:34 +01:00
` ;d= `
$ { r } rc = outputLoc ;
$ { h }
2021-03-09 23:32:33 +01:00
result [ 0 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2021-01-12 16:01:34 +01:00
$ { o [ a - 1 ] } += 1 ;
2021-03-09 23:32:33 +01:00
if ( $ { u } ) {
2021-01-12 16:01:34 +01:00
$ { h }
2021-03-09 23:32:33 +01:00
result [ 1 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2021-01-12 16:01:34 +01:00
}
` }else{let h= `
$ { r } source = rc ;
$ { r } lt = $ { r } ( lessThan ( source , start ) ) ;
$ { r } gte = $ { r } ( greaterThanEqual ( source , end ) ) ;
$ { r } orig = 1 - ( lt + gte ) ;
2020-12-15 14:15:43 +01:00
source = orig * source +
2021-01-12 16:01:34 +01:00
lt * ( start * 2 - source - $ { p } ) +
gte * ( ( end - 1 ) * 2 - source + $ { p } ) ;
2020-12-15 14:15:43 +01:00
source -= start ;
2021-01-12 16:01:34 +01:00
` ;d= `
$ { r } rc = outputLoc ;
$ { h }
2021-03-09 23:32:33 +01:00
result [ 0 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2021-01-12 16:01:34 +01:00
$ { o [ a - 1 ] } += 1 ;
2021-03-09 23:32:33 +01:00
if ( $ { u } ) {
2021-01-12 16:01:34 +01:00
$ { h }
2021-03-09 23:32:33 +01:00
result [ 1 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2020-12-15 14:15:43 +01:00
}
rc = outputLoc ;
2021-01-12 16:01:34 +01:00
$ { o [ a - 2 ] } += 1 ;
if ( $ { o [ a - 2 ] } < $ { this . outputShape [ a - 2 ] } ) {
$ { h }
2021-03-09 23:32:33 +01:00
result [ 2 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2021-01-12 16:01:34 +01:00
$ { o [ a - 1 ] } += 1 ;
2021-03-09 23:32:33 +01:00
if ( $ { u } ) {
2021-01-12 16:01:34 +01:00
$ { h }
2021-03-09 23:32:33 +01:00
result [ 3 ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2020-12-15 14:15:43 +01:00
}
}
` }this.userCode= `
2021-01-12 16:01:34 +01:00
const $ { r } start = $ { r } ( $ { s } ) ;
const $ { r } end = $ { r } ( $ { i } ) ;
2020-12-15 14:15:43 +01:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { r } outputLoc = getOutputCoords ( ) ;
2020-10-12 20:28:19 +02:00
vec4 result = vec4 ( 0. ) ;
2021-01-12 16:01:34 +01:00
$ { d }
2020-10-12 20:28:19 +02:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }},WJ=({inputs:e,backend:t,attrs:n})=>{let{x:a}=e,{paddings:r,mode:s}=n,i=Z().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new zJ(a.shape,r,s):new LJ(a.shape,r,s);return t.runWebGLProgram(i,[a],a.dtype)},BJ={kernelName:ic,backendName:"webgl",kernelFunc:WJ},VJ= ` if ( b == 0.0 ) return NAN ;
return mod ( a , b ) ; ` ,UJ= `
2020-12-15 14:15:43 +01:00
vec4 result = mod ( a , b ) ;
vec4 isNaN = vec4 ( equal ( b , vec4 ( 0.0 ) ) ) ;
2021-03-09 23:32:33 +01:00
` + $ m+ `
2020-12-15 14:15:43 +01:00
return result ;
2021-03-09 23:32:33 +01:00
` ,GJ=sn({opSnippet:VJ,packedOpSnippet:UJ}),HJ={kernelName:el,backendName:"webgl",kernelFunc:GJ},jJ=class{constructor(e,t,n){this.variableNames=["probs"],this.outputShape=[e,n],this.userCode= `
2020-12-15 14:15:43 +01:00
uniform float seed ;
2020-09-08 18:26:51 +02:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float r = random ( seed ) ;
float cdf = 0.0 ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { t - 1 } ; i ++ ) {
2020-12-15 14:15:43 +01:00
cdf += getProbs ( batch , i ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
if ( r < cdf ) {
setOutput ( float ( i ) ) ;
return ;
}
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// If no other event happened, last event happened.
2021-01-12 16:01:34 +01:00
setOutput ( float ( $ { t - 1 } ) ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){return(t,n)=>{this.seedLoc==null&&(this.seedLoc=t.getUniformLocation(n,"seed")),t.gl.uniform1f(this.seedLoc,e)}}},qJ= `
2020-12-15 14:15:43 +01:00
if ( a == b ) {
return 1.0 ;
} ;
2021-03-09 23:32:33 +01:00
return a / b ; ` ,XJ= `
2020-12-15 14:15:43 +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. ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
if ( a . y == b . y ) {
result . y = 1. ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
if ( a . z == b . z ) {
result . z = 1. ;
}
if ( a . w == b . w ) {
result . w = 1. ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
2020-10-12 20:28:19 +02:00
return result ;
2021-03-09 23:32:33 +01:00
` ,WT=sn({opSnippet:qJ,packedOpSnippet:XJ,checkOutOfBounds:!0}),KJ={kernelName:Rs,backendName:"webgl",kernelFunc:WT},BT="return a - b;",VT=sn({opSnippet:BT,packedOpSnippet:BT,supportsComplex:!0,cpuKernelImpl:J8}),YJ={kernelName:hi,backendName:"webgl",kernelFunc:VT};function UT(e){let{inputs:t,backend:n,attrs:a}=e,{logits:r}=t,{dim:s}=a,i=I.parseAxisParam([s],r.shape),o=zT({inputs:{x:r},backend:n,attrs:{reductionIndices:i,keepDims:!1}}),l=E.expandShapeToKeepDim(o.shape,i),u=ye({inputs:{x:o},backend:n,attrs:{shape:l}}),c=VT({inputs:{a:r,b:u},backend:n}),p=MT({inputs:{x:c},backend:n}),d=Wv({inputs:{x:p},backend:n,attrs:{axis:i,keepDims:!1}}),h=ye({inputs:{x:d},backend:n,attrs:{shape:l}}),m=WT({inputs:{a:p,b:h},backend:n});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(u),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(h),m}var JJ={kernelName:pi,backendName:"webgl",kernelFunc:UT};function QJ(e){let{inputs:t,backend:n,attrs:a}=e,{logits:r}=t,{numSamples:s,seed:i,normalized:o}=a,l=o?r:UT({inputs:{logits:r},backend:n,attrs:{dim:r.shape.length-1}}),u=l.shape[0],c=l.shape[1],p=new jJ(u,c,s),d=p.getCustomSetupFunc(i),h=n.runWebGLProgram(p,[l],"int32",d);return o||n.disposeIntermediateTensorInfo(l),h}var ZJ={kernelName:Dd,backendName:"webgl",kernelFunc:QJ},GT="return -x;";function eQ(e){let{inputs:t,backend:n}=e,{x:a}=t;if(n.shouldExecuteOnCPU([a])){let s=n.texData.get(a.dataId),[i,o]=H8(s.values,a.shape,a.dtype);return n.makeTensorInfo(o,a.dtype,i)}let r;return Z().getBool("WEBGL_PACK_UNARY_OPERATIONS")?r=new cu(a.shape,GT):r=new ds(a.shape,GT),n.runWebGLProgram(r,[a],a.dtype)}var tQ={kernelName:tl,backendName:"webgl",kernelFunc:eQ},nQ=Ya.nonMaxSuppressionV3Impl;function aQ(e){E.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:a}=e,{boxes:r,scores:s}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:l}=a,u=n.readSync(r.dataId),c=n.readSync(s.dataId),{selectedIndices:p}=nQ(u,c,i,o,l);return n.makeTensorInfo([p.length],"int32",new Int32Array(p))}var rQ={kernelName:al,backendName:"webgl",kernelFunc:aQ},sQ=Ya.nonMaxSuppressionV4Impl;function iQ(e){E.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:a}=e,{boxes:r,scores:s}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:l,padToMaxOutputSize:u}=a,c=n.readSync(r.dataId),p=n.readSync(s.dataId),{selectedIndices:d,validOutputs:h}=sQ(c,p,i,o,l,u);return[n.makeTensorInfo([d.length],"int32",new Int32Array(d)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var oQ={kernelName:rl,backendName:"webgl",kernelFunc:iQ},lQ=Ya.nonMaxSuppressionV5Impl;function uQ(e){E.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:a}=e,{boxes:r,scores:s}=t,{maxOutputSize:i,iouThreshold:o,scoreThreshold:l,softNmsSigma:u}=a,c=n.readSync(r.dataId),p=n.readSync(s.dataId),d=i,h=o,m=l,f=u,{selectedIndices:g,selectedScores:y}=lQ(c,p,d,h,m,f);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([y.length],"float32",new Float32Array(y))]}var cQ={kernelName:sl,backendName:"webgl",kernelFunc:uQ},pQ=class{constructor(e,t,n,a){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int index = round ( getIndices ( coords . x ) ) ;
2021-01-12 16:01:34 +01:00
setOutput ( mix ( float ( $ { a } ) , float ( $ { n } ) ,
2020-12-15 14:15:43 +01:00
float ( index == coords . y ) ) ) ;
}
2021-03-09 23:32:33 +01:00
` }},dQ=e=>{let{inputs:t,backend:n,attrs:a}=e,{indices:r}=t,{depth:s,onValue:i,offValue:o}=a,l=I.sizeFromShape(r.shape),u=new pQ(l,s,i,o),c=ye({inputs:{x:r},backend:n,attrs:{shape:[l]}}),p=n.runWebGLProgram(u,[c],r.dtype);n.disposeIntermediateTensorInfo(c);let d=[...r.shape,s],h=ye({inputs:{x:p},backend:n,attrs:{shape:d}});return n.disposeIntermediateTensorInfo(p),h},hQ={kernelName:Js,backendName:"webgl",kernelFunc:dQ};function zm(e){let{inputs:t,backend:n}=e,{x:a}=t;if(a.dtype==="complex64"){let r=fp({inputs:{input:a},backend:n}),s=zm({inputs:{x:r},backend:n}),i=Lm({inputs:{input:a},backend:n}),o=zm({inputs:{x:i},backend:n}),l=hs({inputs:{real:s,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),l}else return Hv({attrs:{shape:a.shape,dtype:a.dtype,value:a.dtype==="string"?"":0},backend:n})}var mQ={kernelName:Il,backendName:"webgl",kernelFunc:zm};function HT(e){let{inputs:t,backend:n}=e,{x:a}=t;if(a.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(a.dtype==="complex64"){let r=fp({inputs:{input:a},backend:n}),s=HT({inputs:{x:r},backend:n}),i=Lm({inputs:{input:a},backend:n}),o=zm({inputs:{x:i},backend:n}),l=hs({inputs:{real:s,imag:o},backend:n});return n.disposeIntermediateTensorInfo(r),n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(o),l}else return Hv({attrs:{shape:a.shape,dtype:a.dtype,value:1},backend:n})}var fQ={kernelName:il,backendName:"webgl",kernelFunc:HT};function gQ(e){let{inputs:t,backend:n,attrs:a}=e,{axis:r}=a;if(t.length===1)return Gv({inputs:{input:t[0]},backend:n,attrs:{dim:r}});let s=t[0].shape,i=t[0].dtype;t.forEach(c=>{I.assertShapesMatch(s,c.shape,"All tensors passed to stack must have matching shapes"),I.assert(i===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let o=[],l=t.map(c=>{let p=Gv({inputs:{input:c},backend:n,attrs:{dim:r}});return o.push(p),p}),u=NT({inputs:l,backend:n,attrs:{axis:r}});return o.forEach(c=>n.disposeIntermediateTensorInfo(c)),u}var yQ={kernelName:ol,backendName:"webgl",kernelFunc:gQ},bQ=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=t.map((l,u)=>l[0]+e[u]+l[1]);let a=e.length,r=ht(a),s=t.map(l=>l[0]).join(","),i=t.map((l,u)=>l[0]+e[u]).join(","),o=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,a);if(a===1){this.userCode= `
2021-01-12 16:01:34 +01:00
int start = $ { s } ;
int end = $ { i } ;
2021-03-09 23:32:33 +01:00
uniform float value ;
2020-12-15 14:15:43 +01:00
void main ( ) {
int outC = getOutputCoords ( ) ;
if ( outC < start || outC >= end ) {
2021-03-09 23:32:33 +01:00
setOutput ( value ) ;
2020-12-15 14:15:43 +01:00
} else {
setOutput ( getX ( outC - start ) ) ;
}
}
` ;return}this.userCode= `
2021-01-12 16:01:34 +01:00
$ { r } start = $ { r } ( $ { s } ) ;
$ { r } end = $ { r } ( $ { i } ) ;
2021-03-09 23:32:33 +01:00
uniform float value ;
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { r } outC = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
if ( any ( lessThan ( outC , start ) ) || any ( greaterThanEqual ( outC , end ) ) ) {
2021-03-09 23:32:33 +01:00
setOutput ( value ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-01-12 16:01:34 +01:00
$ { r } coords = outC - start ;
setOutput ( getX ( $ { o } ) ) ;
2020-12-15 14:15:43 +01:00
}
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){return(t,n)=>{this.valueLoc==null&&(this.valueLoc=t.getUniformLocationNoThrow(n,"value")),t.gl.uniform1f(this.valueLoc,e)}}},xQ=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.map((m,f)=>m[0]+e[f]+m[1]);let a=e.length,r=ht(a),s=t.map(m=>m[0]).join(","),i=t.map((m,f)=>m[0]+e[f]).join(","),o=fn("rc",a),l=fn("source",a),u= ` $ { o [ a - 1 ] } < $ { this . outputShape [ a - 1 ] } ` ,c=a===1?"source": ` vec2 ( $ { l . slice ( - 2 ) . join ( ) } ) ` ,p=[ ` $ { r } rc = outputLoc ; ` , ` $ { o [ a - 1 ] } += 1 ;
if ( $ { u } ) {
2021-01-12 16:01:34 +01:00
` ,a===1?"": ` }
2020-12-15 14:15:43 +01:00
rc = outputLoc ;
2021-01-12 16:01:34 +01:00
$ { o [ a - 2 ] } += 1 ;
if ( $ { o [ a - 2 ] } < $ { this . outputShape [ a - 2 ] } ) { ` ,a===1?"": ` $ { o [ a - 1 ] } += 1 ;
2021-03-09 23:32:33 +01:00
if ( $ { u } ) { ` ],d=a===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="";for(let m=0,f=a===1?2:4;m<f;m++)h+= `
2021-01-12 16:01:34 +01:00
$ { p [ m ] }
if ( $ { d } ) {
2021-03-09 23:32:33 +01:00
result [ $ { m } ] = float ( value ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-01-12 16:01:34 +01:00
$ { r } source = rc - start ;
2021-03-09 23:32:33 +01:00
result [ $ { m } ] = getChannel ( getX ( $ { l . join ( ) } ) , $ { c } ) ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
` ;h+=a===1?"} ":"}}",this.userCode= `
const $ { r } start = $ { r } ( $ { s } ) ;
const $ { r } end = $ { r } ( $ { i } ) ;
2021-03-09 23:32:33 +01:00
uniform float value ;
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { r } outputLoc = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
2021-01-12 16:01:34 +01:00
$ { h }
2020-12-15 14:15:43 +01:00
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e){return(t,n)=>{this.valueLoc==null&&(this.valueLoc=t.getUniformLocationNoThrow(n,"value")),t.gl.uniform1f(this.valueLoc,e)}}},jT=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{paddings:s,constantValue:i}=a,o=Z().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new xQ(r.shape,s,i):new bQ(r.shape,s,i),l=o.getCustomSetupFunc(i);return n.runWebGLProgram(o,[r],r.dtype,l)},vQ={kernelName:Qs,backendName:"webgl",kernelFunc:jT},wQ= `
2020-12-15 14:15:43 +01:00
if ( a < 0.0 && floor ( b ) < b ) {
2020-10-12 20:28:19 +02:00
return NAN ;
}
2020-12-15 14:15:43 +01:00
if ( b == 0.0 ) {
return 1.0 ;
2020-10-12 20:28:19 +02:00
}
2020-12-15 14:15:43 +01:00
return ( round ( mod ( b , 2.0 ) ) != 1 ) ?
pow ( abs ( a ) , b ) : sign ( a ) * pow ( abs ( a ) , b ) ;
2021-03-09 23:32:33 +01:00
` ,kQ= `
2020-12-15 14:15:43 +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 ) ;
// Ensure that a^0 = 1, including 0^0 = 1 as this correspond to TF and JS
bvec4 isExpZero = equal ( b , vec4 ( 0.0 ) ) ;
result . r = isExpZero . r ? 1.0 : result . r ;
result . g = isExpZero . g ? 1.0 : result . g ;
result . b = isExpZero . b ? 1.0 : result . b ;
result . a = isExpZero . a ? 1.0 : result . a ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
vec4 isNaN = vec4 ( lessThan ( a , vec4 ( 0.0 ) ) ) * vec4 ( lessThan ( floor ( b ) , b ) ) ;
2021-03-09 23:32:33 +01:00
` + $ m+ `
2020-10-12 20:28:19 +02:00
return result ;
2021-03-09 23:32:33 +01:00
` ,IQ=sn({opSnippet:wQ,packedOpSnippet:kQ}),NQ={kernelName:Zs,backendName:"webgl",kernelFunc:IQ};function TQ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{axis:s,keepDims:i}=a,o=r.shape.length,l=[],u=I.parseAxisParam(s,r.shape),c=u,p=E.getAxesPermutation(c,o),d=r;p!=null&&(d=_n({inputs:{x:r},backend:n,attrs:{perm:p}}),c=E.getInnerMostAxes(c.length,o),l.push(d)),E.assertAxesAreInnerMostDims("prod",c,o);let h;if(n.shouldExecuteOnCPU([d])){let m=n.texData.get(d.dataId).values,{outVals:f,outShape:g,outDtype:y}=j8(d.shape,d.dtype,m,c);h=n.makeTensorInfo(g,y,f)}else{let[m,f]=E.computeOutAndReduceShapes(d.shape,c),g=I.sizeFromShape(f),y=ye({inputs:{x:d},backend:n,attrs:{shape:[-1,g]}}),b=jd(r.dtype),x=ji(y,b,"prod",n);h=ye({inputs:{x},backend:n,attrs:{shape:m}}),l.push(y),l.push(x)}if(i){l.push(h);let m=E.expandShapeToKeepDim(h.shape,u);h=ye({inputs:{x:h},backend:n,attrs:{shape:m}})}return l.forEach(m=>n.disposeIntermediateTensorInfo(m)),h}var SQ={kernelName:ll,backendName:"webgl",kernelFunc:TQ},qT=e=>{let{backend:t,attrs:n}=e,{start:a,stop:r,step:s,dtype:i}=n,o=q8(a,r,s,i);return t.makeTensorInfo([o.length],i,o)},CQ={kernelName:oc,backendName:"webgl",kernelFunc:qT},AQ="return 1.0 / x;",FQ=Xe({opSnippet:AQ}),_Q={kernelName:ul,backendName:"webgl",kernelFunc:FQ},EQ=Ra+ `
2020-12-15 14:15:43 +01:00
return ( x < 0.0 ) ? 0.0 : x ;
2021-03-09 23:32:33 +01:00
` , $ Q= `
2020-10-12 20:28:19 +02:00
vec4 result = x * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
bvec4 isNaN = isnan ( x ) ;
result . r = isNaN . r ? x . r : result . r ;
result . g = isNaN . g ? x . g : result . g ;
result . b = isNaN . b ? x . b : result . b ;
result . a = isNaN . a ? x . a : result . a ;
return result ;
2021-03-09 23:32:33 +01:00
` ,DQ=Xe({opSnippet:EQ,packedOpSnippet: $ Q}),MQ={kernelName:ti,backendName:"webgl",kernelFunc:DQ},RQ=Ra+ `
2020-12-15 14:15:43 +01:00
return ( x < 0.0 ) ? 0.0 : min ( 6.0 , x ) ;
2021-03-09 23:32:33 +01:00
` ,PQ= `
2020-10-12 20:28:19 +02:00
vec4 result = min ( x , vec4 ( 6. ) ) * vec4 ( greaterThanEqual ( x , vec4 ( 0.0 ) ) ) ;
bvec4 isNaN = isnan ( x ) ;
result . r = isNaN . r ? x . r : result . r ;
result . g = isNaN . g ? x . g : result . g ;
result . b = isNaN . b ? x . b : result . b ;
result . a = isNaN . a ? x . a : result . a ;
return result ;
2021-03-09 23:32:33 +01:00
` ,OQ=Xe({opSnippet:RQ,packedOpSnippet:PQ}),LQ={kernelName:ai,backendName:"webgl",kernelFunc:OQ},zQ=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],c=[a&&t>1?t-1:t,a&&n>1?n-1:n],p;r?p="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":p="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2020-12-15 14:15:43 +01:00
const vec2 effectiveInputOverOutputRatioRC = vec2 (
2021-03-09 23:32:33 +01:00
$ { u [ 0 ] / c [ 0 ] } ,
$ { u [ 1 ] / c [ 1 ] } ) ;
2021-01-12 16:01:34 +01:00
const vec2 inputShapeRC = vec2 ( $ { i } . 0 , $ { o } . 0 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
ivec2 yRC = coords . yz ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Fractional source index.
2021-01-12 16:01:34 +01:00
vec2 sourceFracIndexRC = $ { p } ;
2020-12-15 14:15:43 +01:00
// Compute the four integer indices.
ivec2 sourceFloorRC = ivec2 ( max ( sourceFracIndexRC , vec2 ( 0.0 ) ) ) ;
ivec2 sourceCeilRC = ivec2 (
min ( inputShapeRC - 1.0 , ceil ( sourceFracIndexRC ) ) ) ;
float topLeft = getA ( b , sourceFloorRC . x , sourceFloorRC . y , d ) ;
float bottomLeft = getA ( b , sourceCeilRC . x , sourceFloorRC . y , d ) ;
float topRight = getA ( b , sourceFloorRC . x , sourceCeilRC . y , d ) ;
float bottomRight = getA ( b , sourceCeilRC . x , sourceCeilRC . y , d ) ;
vec2 fracRC = sourceFracIndexRC - vec2 ( sourceFloorRC ) ;
float top = topLeft + ( topRight - topLeft ) * fracRC . y ;
float bottom = bottomLeft + ( bottomRight - bottomLeft ) * fracRC . y ;
float newValue = top + ( bottom - top ) * fracRC . x ;
setOutput ( newValue ) ;
}
2021-03-09 23:32:33 +01:00
` }},WQ=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],c=[a&&t>1?t-1:t,a&&n>1?n-1:n],p;r?p="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":p="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2020-12-15 14:15:43 +01:00
const vec3 effectiveInputOverOutputRatioRC = vec3 (
2021-03-09 23:32:33 +01:00
$ { u [ 0 ] / c [ 0 ] } ,
$ { u [ 1 ] / c [ 1 ] } ,
$ { u [ 1 ] / c [ 1 ] } ) ;
2021-01-12 16:01:34 +01:00
const vec3 inputShapeRC = vec3 ( $ { i } . 0 , $ { o } . 0 ,
$ { o } . 0 ) ;
2020-12-15 14:15:43 +01:00
float getAValue ( int b , int r , int c , int d ) {
return getChannel ( getA ( b , r , c , d ) , vec2 ( c , d ) ) ;
2020-10-12 20:28:19 +02:00
}
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
// Calculate values for next column in yRC.z.
ivec3 yRC = coords . yzz + ivec3 ( 0 , 0 , 1 ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
// Fractional source index.
2021-01-12 16:01:34 +01:00
vec3 sourceFracIndexRC = $ { p } ;
2020-12-15 14:15:43 +01:00
// Compute the four integer indices.
ivec3 sourceFloorRC = ivec3 ( max ( sourceFracIndexRC , vec3 ( 0.0 ) ) ) ;
ivec3 sourceCeilRC = ivec3 (
min ( inputShapeRC - 1.0 , ceil ( sourceFracIndexRC ) ) ) ;
// Should we calculate next column and row elements in 2x2 packed cell.
2021-01-12 16:01:34 +01:00
bool hasNextCol = d < $ { l - 1 } ;
bool hasNextRow = coords . z < $ { n - 1 } ;
2020-12-15 14:15:43 +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 ) ;
vec4 bottomLeft = vec4 (
getAValue ( b , sourceCeilRC . x , sourceFloorRC . y , d ) ,
hasNextCol ? getAValue ( b , sourceCeilRC . x , sourceFloorRC . y , d + 1 )
: 0.0 ,
hasNextRow ? getAValue ( b , sourceCeilRC . x , sourceFloorRC . z , d )
: 0.0 ,
( hasNextRow && hasNextCol ) ?
getAValue ( b , sourceCeilRC . x , sourceFloorRC . z , d + 1 ) : 0.0 ) ;
vec4 topRight = vec4 (
getAValue ( b , sourceFloorRC . x , sourceCeilRC . y , d ) ,
hasNextCol ? getAValue ( b , sourceFloorRC . x , sourceCeilRC . y , d + 1 )
: 0.0 ,
hasNextRow ? getAValue ( b , sourceFloorRC . x , sourceCeilRC . z , d )
: 0.0 ,
( hasNextRow && hasNextCol ) ?
getAValue ( b , sourceFloorRC . x , sourceCeilRC . z , d + 1 ) : 0.0 ) ;
vec4 bottomRight = vec4 (
getAValue ( b , sourceCeilRC . x , sourceCeilRC . y , d ) ,
hasNextCol ? getAValue ( b , sourceCeilRC . x , sourceCeilRC . y , d + 1 )
: 0.0 ,
hasNextRow ? getAValue ( b , sourceCeilRC . x , sourceCeilRC . z , d )
: 0.0 ,
( hasNextRow && hasNextCol ) ?
getAValue ( b , sourceCeilRC . x , sourceCeilRC . z , d + 1 ) : 0.0 ) ;
vec3 fracRC = sourceFracIndexRC - vec3 ( sourceFloorRC ) ;
vec4 top = mix ( topLeft , topRight , fracRC . yyzz ) ;
vec4 bottom = mix ( bottomLeft , bottomRight , fracRC . yyzz ) ;
vec4 newValue = mix ( top , bottom , fracRC . x ) ;
setOutput ( newValue ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function BQ(e){let{inputs:t,backend:n,attrs:a}=e,{images:r}=t,{alignCorners:s,halfPixelCenters:i,size:o}=a,[l,u]=o,c=Z().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new WQ(r.shape,l,u,s,i):new zQ(r.shape,l,u,s,i);return n.runWebGLProgram(c,[r],"float32")}var VQ={kernelName:ni,backendName:"webgl",kernelFunc:BQ},UQ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,a,r]=t,[,s,i]=e,o=[n&&s>1?a-1:a,n&&i>1?r-1:r],l=[n&&s>1?s-1:s,n&&i>1?i-1:i],u=o[0]/l[0],c=o[1]/l[1],p=1/u,d=1/c,h=Math.ceil(p)*2+2,m=Math.ceil(d)*2+2;this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float accumulator = 0.0 ;
2021-03-09 23:32:33 +01:00
const float heightScale = float ( $ { u } ) ;
const float widthScale = float ( $ { c } ) ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
const float invHeightScale = float ( $ { p } ) ;
const float invWidthScale = float ( $ { d } ) ;
2020-12-15 14:15:43 +01:00
2021-01-12 16:01:34 +01:00
const int winHeight = int ( $ { h } ) ;
const int winWidth = int ( $ { m } ) ;
2020-12-15 14:15:43 +01:00
// Compute bounds for where in dy we will look
float startRLerp = floor ( float ( r ) * invHeightScale ) ;
int startDyR = int ( startRLerp - float ( winHeight / 2 ) ) ;
float startCLerp = floor ( float ( c ) * invWidthScale ) ;
int startDyC = int ( startCLerp - float ( winWidth / 2 ) ) ;
// Loop over dy
for ( int dyROffset = 0 ; dyROffset < winHeight ; dyROffset ++ ) {
int dyR = dyROffset + startDyR ;
// Guard against the window exceeding the bounds of dy
2021-01-12 16:01:34 +01:00
if ( dyR < 0 || dyR >= $ { s } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
for ( int dyCOffset = 0 ; dyCOffset < winWidth ; dyCOffset ++ ) {
int dyC = dyCOffset + startDyC ;
// Guard against the window exceeding the bounds of dy
2021-01-12 16:01:34 +01:00
if ( dyC < 0 || dyC >= $ { i } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
float dxR = float ( dyR ) * heightScale ;
int topDxRIndex = int ( floor ( dxR ) ) ;
2021-01-12 16:01:34 +01:00
int bottomDxRIndex = int ( min ( ceil ( dxR ) , $ { a - 1 } . 0 ) ) ;
2020-12-15 14:15:43 +01:00
float dxRLerp = dxR - float ( topDxRIndex ) ;
float inverseDxRLerp = 1.0 - dxRLerp ;
float dxC = float ( dyC ) * widthScale ;
int leftDxCIndex = int ( floor ( dxC ) ) ;
2021-01-12 16:01:34 +01:00
int rightDxCIndex = int ( min ( ceil ( dxC ) , $ { r - 1 } . 0 ) ) ;
2020-12-15 14:15:43 +01:00
float dxCLerp = dxC - float ( leftDxCIndex ) ;
float inverseDxCLerp = 1.0 - dxCLerp ;
if ( r == topDxRIndex && c == leftDxCIndex ) {
// topLeft
accumulator +=
getDy ( b , dyR , dyC , d ) * inverseDxRLerp * inverseDxCLerp ;
}
if ( r == topDxRIndex && c == rightDxCIndex ) {
// topRight
accumulator += getDy ( b , dyR , dyC , d ) * inverseDxRLerp * dxCLerp ;
}
if ( r == bottomDxRIndex && c == leftDxCIndex ) {
// bottomLeft
accumulator += getDy ( b , dyR , dyC , d ) * dxRLerp * inverseDxCLerp ;
}
if ( r == bottomDxRIndex && c == rightDxCIndex ) {
// bottomRight
accumulator += getDy ( b , dyR , dyC , d ) * dxRLerp * dxCLerp ;
}
}
}
// End loop over dy
setOutput ( accumulator ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function GQ(e){let{inputs:t,backend:n,attrs:a}=e,{images:r,dy:s}=t,{alignCorners:i}=a,o=new UQ(s.shape,r.shape,i);return n.runWebGLProgram(o,[s],s.dtype)}var HQ={kernelName:Pd,backendName:"webgl",kernelFunc:GQ},jQ=class{constructor(e,t,n,a,r){this.variableNames=["A"],this.outputShape=[];let[s,i,o,l]=e;this.outputShape=[s,t,n,l];let u=[a&&t>1?i-1:i,a&&n>1?o-1:o],c=[a&&t>1?t-1:t,a&&n>1?n-1:n],p=a?"0.5":"0.0",d;r?d="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":d="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode= `
2020-12-15 14:15:43 +01:00
const vec2 effectiveInputOverOutputRatioRC = vec2 (
2021-03-09 23:32:33 +01:00
$ { u [ 0 ] / c [ 0 ] } ,
$ { u [ 1 ] / c [ 1 ] } ) ;
2021-01-12 16:01:34 +01:00
const vec2 inputShapeRC = vec2 ( $ { i } . 0 , $ { o } . 0 ) ;
2020-12-15 14:15:43 +01:00
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
ivec2 yRC = coords . yz ;
// Fractional source index.
2021-01-12 16:01:34 +01:00
vec2 sourceFracIndexRC = $ { d } ;
2020-12-15 14:15:43 +01:00
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestRC = ivec2 (
2021-01-12 16:01:34 +01:00
min ( inputShapeRC - 1.0 , floor ( sourceFracIndexRC + $ { p } ) ) ) ;
2020-12-15 14:15:43 +01:00
float newValue = getA ( b , sourceNearestRC . x , sourceNearestRC . y , d ) ;
setOutput ( newValue ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function qQ(e){let{inputs:t,backend:n,attrs:a}=e,{images:r}=t,{alignCorners:s,halfPixelCenters:i,size:o}=a,[l,u]=o,c=new jQ(r.shape,l,u,s,i);return n.runWebGLProgram(c,[r],r.dtype)}var XQ={kernelName:lc,backendName:"webgl",kernelFunc:qQ},KQ=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,a,r]=t,[,s,i]=e,o=[n&&s>1?a-1:a,n&&i>1?r-1:r],l=[n&&s>1?s-1:s,n&&i>1?i-1:i],u=o[0]/l[0],c=o[1]/l[1],p=1/u,d=1/c,h=Math.ceil(p)*2+2,m=Math.ceil(d)*2+2;this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2020-12-15 14:15:43 +01:00
ivec4 coords = getOutputCoords ( ) ;
int b = coords [ 0 ] ;
int d = coords [ 3 ] ;
int r = coords [ 1 ] ;
int c = coords [ 2 ] ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float accumulator = 0.0 ;
2020-10-11 18:41:17 +02:00
2021-03-09 23:32:33 +01:00
const float heightScale = float ( $ { u } ) ;
const float widthScale = float ( $ { c } ) ;
2020-10-12 20:28:19 +02:00
2021-01-12 16:01:34 +01:00
const float invHeightScale = float ( $ { p } ) ;
const float invWidthScale = float ( $ { d } ) ;
2020-10-29 05:16:50 +01:00
2021-01-12 16:01:34 +01:00
const int winHeight = int ( $ { h } ) ;
const int winWidth = int ( $ { m } ) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +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 ) ) ) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
float startCLerp = floor ( float ( c ) * invWidthScale ) ;
int startDyC = int ( floor ( startCLerp - float ( winWidth / 2 ) ) ) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
// Loop over dy
for ( int dyROffset = 0 ; dyROffset < winHeight ; dyROffset ++ ) {
int dyR = dyROffset + startDyR ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
// Guard against the window exceeding the bounds of dy
2021-01-12 16:01:34 +01:00
if ( dyR < 0 || dyR >= $ { s } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
for ( int dyCOffset = 0 ; dyCOffset < winWidth ; dyCOffset ++ ) {
int dyC = dyCOffset + startDyC ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
// Guard against the window exceeding the bounds of dy
2021-01-12 16:01:34 +01:00
if ( dyC < 0 || dyC >= $ { i } ) {
2020-12-15 14:15:43 +01:00
continue ;
}
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
float sourceFracRow =
2021-01-12 16:01:34 +01:00
float ( $ { o [ 0 ] } ) *
( float ( dyR ) / float ( $ { l [ 0 ] } ) ) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
float sourceFracCol =
2021-01-12 16:01:34 +01:00
float ( $ { o [ 1 ] } ) *
( float ( dyC ) / float ( $ { l [ 1 ] } ) ) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
int sourceNearestRow = int ( min (
2021-01-12 16:01:34 +01:00
float ( int ( $ { a } ) - 1 ) ,
$ { n } ? float ( round ( sourceFracRow ) ) :
2020-12-15 14:15:43 +01:00
float ( floor ( sourceFracRow ) ) ) ) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
int sourceNearestCol = int ( min (
2021-01-12 16:01:34 +01:00
float ( int ( $ { r } ) - 1 ) ,
$ { n } ? float ( round ( sourceFracCol ) ) :
2020-12-15 14:15:43 +01:00
float ( floor ( sourceFracCol ) ) ) ) ;
if ( r == sourceNearestRow && c == sourceNearestCol ) {
accumulator += getDy ( b , dyR , dyC , d ) ;
}
}
2020-10-29 05:16:50 +01:00
}
2020-12-15 14:15:43 +01:00
// End loop over dy
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +01:00
setOutput ( accumulator ) ;
2020-10-29 05:16:50 +01:00
}
2021-03-09 23:32:33 +01:00
` }};function YQ(e){let{inputs:t,backend:n,attrs:a}=e,{images:r,dy:s}=t,{alignCorners:i}=a,o=new KQ(s.shape,r.shape,i);return n.runWebGLProgram(o,[s],s.dtype)}var JQ={kernelName:Rd,backendName:"webgl",kernelFunc:YQ},QQ=class{constructor(e,t){this.variableNames=["x"];let n=e.length;if(n>4)throw new Error( ` WebGL backend : Reverse of rank - $ { n } tensor is not yet supported ` );if(this.outputShape=e,n===1){this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
int coord = getOutputCoords ( ) ;
2021-01-12 16:01:34 +01:00
setOutput ( getX ( $ { e [ 0 ] } - coord - 1 ) ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` ;return}let a=i=>t.indexOf(i)!==-1&&e[i]!==1? ` $ { e [ i ] } - coords [ $ { i } ] - 1 ` : ` coords [ $ { i } ] ` ,r=e.map((i,o)=>a(o)).join(","),s=ht(n);this.userCode= `
2020-10-29 05:16:50 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { s } coords = getOutputCoords ( ) ;
setOutput ( getX ( $ { r } ) ) ;
2020-10-29 05:16:50 +01:00
}
2021-03-09 23:32:33 +01:00
` }},ZQ=class{constructor(e,t){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0;let n=e.length;if(n>4)throw new Error( ` WebGL backend : Reverse of rank - $ { n } tensor is not yet supported ` );this.outputShape=e;let a=fn("rc",n),r= ` $ { a [ n - 1 ] } + 1 < $ { this . outputShape [ n - 1 ] } ` ,s= ` $ { a [ n - 2 ] } + 1 < $ { this . outputShape [ n - 2 ] } ` ,i=ht(n);n===1?this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
int rc = getOutputCoords ( ) ;
vec4 result = vec4 ( 0. ) ;
2021-01-12 16:01:34 +01:00
result . r = getChannel ( getX ( $ { e [ 0 ] } - rc - 1 ) ,
$ { e [ 0 ] } - rc - 1 ) ;
if ( $ { r } ) {
result . g = getChannel ( getX ( $ { e [ 0 ] } - ( rc + 1 ) - 1 ) ,
$ { e [ 0 ] } - ( rc + 1 ) - 1 ) ;
2020-12-15 14:15:43 +01:00
}
setOutput ( result ) ;
}
` :this.userCode= `
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { i } rc = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
vec4 result = vec4 ( 0. ) ;
2021-01-12 16:01:34 +01:00
result . r = $ { o ( a . slice ( ) ) } ;
if ( $ { r } ) {
result . g = $ { l ( a . slice ( ) ) } ;
2020-12-15 14:15:43 +01:00
}
2021-01-12 16:01:34 +01:00
if ( $ { s } ) {
2021-03-09 23:32:33 +01:00
result . b = $ { u ( a . slice ( ) ) } ;
2021-01-12 16:01:34 +01:00
if ( $ { r } ) {
2021-03-09 23:32:33 +01:00
result . a = $ { c ( a . slice ( ) ) } ;
2020-12-15 14:15:43 +01:00
}
}
setOutput ( result ) ;
}
2021-03-09 23:32:33 +01:00
` ;function o(h){return p(h)}function l(h){return h[n-1]="("+h[n-1]+" + 1)",p(h)}function u(h){return h[n-2]="("+h[n-2]+" + 1)",p(h)}function c(h){return h[n-1]="("+h[n-1]+" + 1)",h[n-2]="("+h[n-2]+" + 1)",p(h)}function p(h){let m=e.map((y,b)=>d(b,h)),f=m.join(","),g=m.slice(-2).join(",");return ` getChannel ( getX ( $ { f } ) , vec2 ( $ { g } ) ) ` }function d(h,m){return t.indexOf(h)!==-1&&e[h]!==1? ` $ { e [ h ] } - $ { m [ h ] } - 1 ` : ` $ { m [ h ] } ` }}};function eZ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{dims:s}=a,i=r.shape.length,o=I.parseAxisParam(s,r.shape);if(i===0)return Gn({inputs:{x:r},backend:n});let l=Z().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new ZQ(r.shape,o):new QQ(r.shape,o);return n.runWebGLProgram(l,[r],r.dtype)}var tZ={kernelName:ri,backendName:"webgl",kernelFunc:eZ},nZ=class{constructor(e,t){this.variableNames=["Image"],this.outputShape=[];let n=e[1],a=e[2];this.outputShape=e;let r="";typeof t=="number"?r= ` float outputValue = $ { t . toFixed ( 2 ) } ; ` :r= `
vec3 fill = vec3 ( $ { t . join ( "," ) } ) ;
2020-12-15 14:15:43 +01:00
float outputValue = fill [ coords [ 3 ] ] ; ` ,this.userCode= `
2021-03-09 23:32:33 +01:00
uniform vec4 params ;
2020-10-12 20:28:19 +02:00
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
int x = coords [ 2 ] ;
2020-12-15 14:15:43 +01:00
int y = coords [ 1 ] ;
2021-03-09 23:32:33 +01:00
float coordXFloat = ( float ( x ) - params [ 0 ] ) * params [ 3 ] -
( float ( y ) - params [ 1 ] ) * params [ 2 ] ;
float coordYFloat = ( float ( x ) - params [ 0 ] ) * params [ 2 ] +
( float ( y ) - params [ 1 ] ) * params [ 3 ] ;
int coordX = int ( round ( coordXFloat + params [ 0 ] ) ) ;
int coordY = int ( round ( coordYFloat + params [ 1 ] ) ) ;
$ { r }
if ( coordX >= 0 && coordX < $ { a } && coordY >= 0 && coordY < $ { n } ) {
2020-12-15 14:15:43 +01:00
outputValue = getImage ( coords [ 0 ] , coordY , coordX , coords [ 3 ] ) ;
2020-10-12 20:28:19 +02:00
}
setOutput ( outputValue ) ;
}
2021-03-09 23:32:33 +01:00
` }getCustomSetupFunc(e,t,n,a){return(r,s)=>{this.paramsLoc==null&&(this.paramsLoc=r.getUniformLocationNoThrow(s,"params")),r.gl.uniform4f(this.paramsLoc,e,t,n,a)}}},aZ={kernelName:Nl,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:a}=e,{radians:r,fillValue:s,center:i}=t,o=n,l=new nZ(a.shape,s),[u,c]=E.getImageCenter(i,a.shape[1],a.shape[2]),p=l.getCustomSetupFunc(u,c,Math.sin(r),Math.cos(r));return o.runWebGLProgram(l,[a],a.dtype,p)}},rZ= `
2020-12-15 14:15:43 +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 ;
}
}
2021-03-09 23:32:33 +01:00
` ,sZ=Xe({opSnippet:rZ}),iZ={kernelName:si,backendName:"webgl",kernelFunc:sZ},oZ="return inversesqrt(x);",lZ=Xe({opSnippet:oZ,cpuKernelImpl:X8}),uZ={kernelName:ii,backendName:"webgl",kernelFunc:lZ},XT=class{constructor(e,t,n,a,r,s,i=!0){this.variableNames=["updates","indices","defaultValue"],this.outputShape=s;let o=ht(r.length),l=ht(s.length),u="";n===1?u="i":n===2&&(u="i, j");let c= ` getIndices ( $ { u } ) ` ,p="";a===1?p="i":a===2&&(p="i, coords[1]");let d= ` getUpdates ( $ { p } ) ` ,h=t>1?"strides[j]":"strides";this.userCode= `
2021-01-12 16:01:34 +01:00
$ { o } strides = $ { o } ( $ { r } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { l } coords = getOutputCoords ( ) ;
2020-12-15 14:15:43 +01:00
float sum = 0.0 ;
bool found = false ;
2021-01-12 16:01:34 +01:00
for ( int i = 0 ; i < $ { e } ; i ++ ) {
2020-12-15 14:15:43 +01:00
int flattenedIndex = 0 ;
2021-01-12 16:01:34 +01:00
for ( int j = 0 ; j < $ { t } ; j ++ ) {
2021-03-09 23:32:33 +01:00
int index = round ( $ { c } ) ;
2021-01-12 16:01:34 +01:00
flattenedIndex += index * $ { h } ;
2020-12-15 14:15:43 +01:00
}
if ( flattenedIndex == coords [ 0 ] ) {
2021-01-12 16:01:34 +01:00
sum += $ { d } ;
2020-12-15 14:15:43 +01:00
found = true ;
}
}
setOutput ( mix ( getDefaultValue ( ) , sum , float ( found ) ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function cZ(e){let{inputs:t,backend:n,attrs:a}=e,{indices:r,updates:s}=t,{shape:i}=a,{sliceRank:o,numUpdates:l,sliceSize:u,strides:c,outputSize:p}=E.calculateShapes(s,r,i),d=[p/u,u];if(p===0)return n.makeTensorInfo(i,r.dtype);let h=ye({inputs:{x:r},backend:n,attrs:{shape:[l,o]}}),m=ye({inputs:{x:s},backend:n,attrs:{shape:[l,u]}}),f=n.makeTensorInfo([],"float32",new Float32Array([0])),g=new XT(l,o,h.shape.length,m.shape.length,c,d),y=n.runWebGLProgram(g,[m,h,f],m.dtype),b=ye({inputs:{x:y},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(y),n.disposeIntermediateTensorInfo(f),b}var pZ={kernelName:pl,backendName:"webgl",kernelFunc:cZ},dZ=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.outputShape=t;let a,r;if(n>4)throw Error( ` Where for rank $ { n } is not yet supported ` );if(n===1)r="resRC",a="resRC";else{let i=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],l=[];for(let u=0;u<t.length;u++)l.push( ` $ { i [ u ] } ` ),u<e&&o.push( ` $ { i [ u ] } ` );a=o.join(),r=l.join()}let s=ht(n);this.userCode= `
2020-10-12 20:28:19 +02:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { s } resRC = getOutputCoords ( ) ;
float cVal = getC ( $ { a } ) ;
2020-12-15 14:15:43 +01:00
if ( cVal >= 1.0 ) {
2021-01-12 16:01:34 +01:00
setOutput ( getA ( $ { r } ) ) ;
2020-12-15 14:15:43 +01:00
} else {
2021-01-12 16:01:34 +01:00
setOutput ( getB ( $ { r } ) ) ;
2020-12-15 14:15:43 +01:00
}
}
2021-03-09 23:32:33 +01:00
` }};function hZ(e){let{inputs:t,backend:n}=e,{condition:a,t:r,e:s}=t,i=new dZ(a.shape.length,r.shape,r.shape.length);return n.runWebGLProgram(i,[a,r,s],ua(r.dtype,s.dtype))}var mZ={kernelName:dl,backendName:"webgl",kernelFunc:hZ},fZ= `
2020-12-15 14:15:43 +01:00
// Stable and Attracting Fixed Point (0, 1) for Normalized Weights.
// see: https://arxiv.org/abs/1706.02515
2021-03-09 23:32:33 +01:00
float scaleAlpha = $ { E . SELU _SCALEALPHA } ;
float scale = $ { E . SELU _SCALE } ;
2020-12-15 14:15:43 +01:00
return ( x >= 0.0 ) ? scale * x : scaleAlpha * ( exp ( x ) - 1.0 ) ;
2021-03-09 23:32:33 +01:00
` ,gZ=Xe({opSnippet:fZ}),yZ={kernelName:hl,backendName:"webgl",kernelFunc:gZ},bZ="return 1.0 / (1.0 + exp(-1.0 * x));",xZ=Xe({opSnippet:bZ}),vZ={kernelName:li,backendName:"webgl",kernelFunc:xZ},wZ= `
2020-12-15 14:15:43 +01:00
if ( isnan ( x ) ) { return 0.0 ; }
return sign ( x ) ;
2021-03-09 23:32:33 +01:00
` ,kZ=Xe({opSnippet:wZ}),IZ={kernelName:gl,backendName:"webgl",kernelFunc:kZ},NZ=oT+ `
2020-12-15 14:15:43 +01:00
return sin ( x ) ;
2021-03-09 23:32:33 +01:00
` ,TZ=Xe({opSnippet:NZ}),SZ={kernelName:oi,backendName:"webgl",kernelFunc:TZ},CZ= `
2020-12-15 14:15:43 +01:00
float e2x = exp ( x ) ;
return ( e2x - 1.0 / e2x ) / 2.0 ;
2021-03-09 23:32:33 +01:00
` ,AZ=Xe({opSnippet:CZ}),FZ={kernelName:fl,backendName:"webgl",kernelFunc:AZ},_Z= `
2020-12-15 14:15:43 +01:00
float epsilon = 1.1920928955078125 e - 7 ;
float threshold = log ( epsilon ) + 2.0 ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
bool too _large = x > - threshold ;
bool too _small = x < threshold ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
float result ;
float exp _x = exp ( x ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
if ( too _large ) {
result = x ;
}
else if ( too _small ) {
result = exp _x ;
}
else {
result = log ( exp _x + 1.0 ) ;
}
return result ;
2021-03-09 23:32:33 +01:00
` ,EZ=Xe({opSnippet:_Z}), $ Z={kernelName:yl,backendName:"webgl",kernelFunc:EZ},DZ=e=>{let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{blockShape:s,paddings:i}=a;I.assert(r.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let o=s.reduce((y,b)=>y*b),l=[[0,0]];l.push(...i);for(let y=1+s.length;y<r.shape.length;++y)l.push([0,0]);let u=[],c=jT({inputs:{x:r},backend:n,attrs:{paddings:l,constantValue:0}}),p=E.getReshaped(c.shape,s,o,!1),d=E.getPermuted(p.length,s.length,!1),h=E.getReshapedPermuted(c.shape,s,o,!1),m=ye({inputs:{x:c},backend:n,attrs:{shape:p}}),f=_n({inputs:{x:m},backend:n,attrs:{perm:d}}),g=ye({inputs:{x:f},backend:n,attrs:{shape:h}});return u.push(c),u.push(m),u.push(f),u.forEach(y=>n.disposeIntermediateTensorInfo(y)),g},MZ={kernelName:uc,backendName:"webgl",kernelFunc:DZ};function RZ(e){let{inputs:t,backend:n,attrs:a}=e,{sparseIndices:r,sparseValues:s,defaultValue:i}=t,{outputShape:o}=a,{sliceRank:l,numUpdates:u,strides:c,outputSize:p}=E.calculateShapes(s,r,o),d=!1,h=new XT(u,l,r.shape.length,s.shape.length,c,[p,1],d),m=n.runWebGLProgram(h,[s,r,i],s.dtype),f=ye({inputs:{x:m},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(m),f}var PZ={kernelName:Od,backendName:"webgl",kernelFunc:RZ};function OZ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{numOrSizeSplits:s,axis:i}=a,o=I.parseAxisParam(i,r.shape)[0],l=E.prepareSplitSize(r,s,o),u=r.shape.length,c=new Array(u).fill(0),p=r.shape.slice();return l.map(d=>{let h=[...p];h[o]=d;let m=mp({inputs:{x:r},backend:n,attrs:{begin:c,size:h}});return c[o]+=d,m})}var LZ={kernelName:bl,backendName:"webgl",kernelFunc:OZ},zZ="return sqrt(x);",WZ=Xe({opSnippet:zZ}),BZ={kernelName:ui,backendName:"webgl",kernelFunc:WZ},VZ="return x * x;",UZ=Xe({opSnippet:VZ}),GZ={kernelName:cc,backendName:"webgl",kernelFunc:UZ},KT="return (a - b) * (a - b);",HZ=sn({opSnippet:KT,packedOpSnippet:KT}),jZ={kernelName:di,backendName:"webgl",kernelFunc:HZ};function qZ({inputs:e,attrs:t,backend:n}){let{x:a}=e,r=Ra+ `
2021-01-12 16:01:34 +01:00
return x > 0.0 ? 1.0 : float ( $ { t . alpha } ) ;
2021-03-09 23:32:33 +01:00
` ,s=new ds(a.shape,r);return n.runWebGLProgram(s,[a],a.dtype)}var XZ={kernelName:Gr,backendName:"webgl",kernelFunc:qZ},KZ=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let a=n.length,r=ht(n.length),s=ht(n.length),i="";if(a===1)i="coords * strides + begin";else{let o=0;i=n.map((l,u)=>(o++,n.length===1? ` coords * strides [ $ { u } ] + begin [ $ { u } ] ` : ` coords [ $ { o - 1 } ] * strides [ $ { u } ] + begin [ $ { u } ] ` )).join(",")}this.userCode= `
2021-01-12 16:01:34 +01:00
$ { r } begin = $ { r } ( $ { e } ) ;
$ { r } strides = $ { r } ( $ { t } ) ;
2020-10-12 20:28:19 +02:00
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { s } coords = getOutputCoords ( ) ;
setOutput ( getX ( $ { i } ) ) ;
2020-12-15 14:15:43 +01:00
}
2021-03-09 23:32:33 +01:00
` }};function YZ(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{begin:s,end:i,strides:o,beginMask:l,endMask:u,ellipsisMask:c,newAxisMask:p,shrinkAxisMask:d}=a,{nonStrided:h, $ begin:m, $ strides:f,size:g,newShape:y,outShape:b}=pn.sliceInfo(r.shape,s,i,o,l,u,c,p,d),x=ye({inputs:{x:r},backend:n,attrs:{shape:y}}),v;if(h){let w=mp({inputs:{x},backend:n,attrs:{begin:m,size:g}});v=ye({inputs:{x:w},backend:n,attrs:{shape:b}}),n.disposeIntermediateTensorInfo(w)}else if(b.some(w=>w===0))v=n.makeTensorInfo(b,r.dtype,[]);else if(n.shouldExecuteOnCPU([x])){let w=n.texData.get(x.dataId).values,S=Le(x.shape,x.dtype,w),A=Y8(b,S,f,m);v=n.makeTensorInfo(b,x.dtype,A.values)}else{let w=new KZ(m,f,b);v=n.runWebGLProgram(w,[x],x.dtype)}let k=ye({inputs:{x:v},backend:n,attrs:{shape:b}});return n.disposeIntermediateTensorInfo(x),n.disposeIntermediateTensorInfo(v),k}var JZ={kernelName:xl,backendName:"webgl",kernelFunc:YZ},QZ="return tan(x);",ZZ=Xe({opSnippet:QZ}),eee={kernelName:vl,backendName:"webgl",kernelFunc:ZZ},tee= `
2020-12-15 14:15:43 +01:00
float e2x = exp ( - 2.0 * abs ( x ) ) ;
return sign ( x ) * ( 1.0 - e2x ) / ( 1.0 + e2x ) ;
2021-03-09 23:32:33 +01:00
` ,nee=Xe({opSnippet:tee}),aee={kernelName:mi,backendName:"webgl",kernelFunc:nee},see=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let s=0;s<n.length;s++)n[s]=e[s]*t[s];this.outputShape=n,this.rank=n.length;let a=ht(this.rank),r=ree(e);this.userCode= `
2020-12-15 14:15:43 +01:00
void main ( ) {
2021-01-12 16:01:34 +01:00
$ { a } resRC = getOutputCoords ( ) ;
setOutput ( getA ( $ { r } ) ) ;
2020-10-12 20:28:19 +02:00
}
2021-03-09 23:32:33 +01:00
` }};function ree(e){let t=e.length;if(t>5)throw Error( ` Tile for rank $ { t } is not yet supported ` );if(t===1)return ` imod ( resRC , $ { e [ 0 ] } ) ` ;let n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],a=[];for(let r=0;r<e.length;r++)a.push( ` imod ( $ { n [ r ] } , $ { e [ r ] } ) ` );return a.join()}function YT(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{reps:s}=a;if(r.dtype==="string"){let o=n.readSync(r.dataId).map(c=>I.decodeString(c)),l=Le(r.shape,r.dtype,o),u=Q8(l,s);return n.makeTensorInfo(u.shape,u.dtype,u.values)}let i=new see(r.shape,s);return n.runWebGLProgram(i,[r],r.dtype)}var iee={kernelName:Ur,backendName:"webgl",kernelFunc:YT};function oee(e){let{inputs:t,backend:n,attrs:a}=e,{x:r}=t,{k:s,sorted:i}=a,o=n.readSync(r.dataId),[l,u]=Z8(o,r.shape,r.dtype,s,i);return[n.makeTensorInfo(l.shape,l.dtype,l.values),n.makeTensorInfo(u.shape,u.dtype,u.values)]}var lee={kernelName:wl,backendName:"webgl",kernelFunc:oee},uee=class{constructor(e,t,n,a,r,s){this.variableNames=["Image","Transforms"],this.outputShape=s;let i=n==="nearest"?1:2,o;switch(a){case"constant":o=1;break;case"reflect":o=2;break;case"wrap":o=3;break;case"nearest":o=4;break;default:o=1;break}this.userCode= `
float mapCoord ( float outCoord , float len ) {
float inCoord = outCoord ;
if ( $ { o } == 2 ) {
if ( inCoord < 0.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz2 = 2.0 * len ;
if ( inCoord < sz2 ) {
inCoord = sz2 * float ( int ( float ( - inCoord / sz2 ) ) ) +
inCoord ;
}
inCoord = inCoord < - len ? inCoord + sz2 : - inCoord - 1.0 ;
}
} else if ( inCoord > len - 1.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz2 = 2.0 * len ;
inCoord -= sz2 * float ( int ( float ( inCoord / sz2 ) ) ) ;
if ( inCoord >= len ) {
inCoord = sz2 - inCoord - 1.0 ;
}
}
}
return clamp ( inCoord , 0.0 , len - 1.0 ) ;
} else if ( $ { o } == 3 ) {
if ( inCoord < 0.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz = len - 1.0 ;
inCoord += len * ( float ( int ( float ( - inCoord / sz ) ) ) + 1.0 ) ;
}
} else if ( inCoord > len - 1.0 ) {
if ( len <= 1.0 ) {
inCoord = 0.0 ;
} else {
float sz = len - 1.0 ;
inCoord -= len * float ( int ( float ( inCoord / sz ) ) ) ;
}
}
return clamp ( inCoord , 0.0 , len - 1.0 ) ;
} else if ( $ { o } == 4 ) {
return clamp ( outCoord , 0.0 , len - 1.0 ) ;
} else {
return outCoord ;
}
}
float readWithFillValue ( int batch , int coordY , int coordX ,
int channel ) {
float outputValue ;
if ( 0 <= coordY && coordY < $ { e } && 0 <= coordX && coordX < $ { t } ) {
outputValue = getImage ( batch , coordY , coordX , channel ) ;
} else {
outputValue = float ( $ { r } ) ;
}
return outputValue ;
}
void main ( ) {
ivec4 coords = getOutputCoords ( ) ;
float outputValue ;
int batch = coords [ 0 ] ;
int x = coords [ 2 ] ;
int y = coords [ 1 ] ;
int channel = coords [ 3 ] ;
float xf = float ( x ) ;
float yf = float ( y ) ;
float a1 = getTransforms ( batch , 0 ) ;
float a2 = getTransforms ( batch , 1 ) ;
float a3 = getTransforms ( batch , 2 ) ;
float b1 = getTransforms ( batch , 3 ) ;
float b2 = getTransforms ( batch , 4 ) ;
float b3 = getTransforms ( batch , 5 ) ;
float c1 = getTransforms ( batch , 6 ) ;
float c2 = getTransforms ( batch , 7 ) ;
float projection = c1 * xf + c2 * yf + 1.0 ;
if ( projection == 0.0 ) {
outputValue = float ( $ { r } ) ;
} else {
float inX = ( a1 * xf + a2 * yf + a3 ) / projection ;
float inY = ( b1 * xf + b2 * yf + b3 ) / projection ;
float mapX = mapCoord ( inX , float ( $ { t } ) ) ;
float mapY = mapCoord ( inY , float ( $ { e } ) ) ;
if ( $ { i } == 1 ) {
int coordY = int ( round ( mapY ) ) ;
int coordX = int ( round ( mapX ) ) ;
outputValue = readWithFillValue ( batch , coordY , coordX ,
channel ) ;
} else {
float yFloor = floor ( mapY ) ;
float xFloor = floor ( mapX ) ;
float yCeil = yFloor + 1.0 ;
float xCeil = xFloor + 1.0 ;
float valueYFloor = ( xCeil - mapX ) *
readWithFillValue ( batch , int ( yFloor ) , int ( xFloor ) , channel ) +
( mapX - xFloor ) *
readWithFillValue ( batch , int ( yFloor ) , int ( xCeil ) , channel ) ;
float valueYCeil = ( xCeil - mapX ) *
readWithFillValue ( batch , int ( yCeil ) , int ( xFloor ) , channel ) +
( mapX - xFloor ) *
readWithFillValue ( batch , int ( yCeil ) , int ( xCeil ) , channel ) ;
outputValue = ( yCeil - mapY ) * valueYFloor +
( mapY - yFloor ) * valueYCeil ;
}
}
setOutput ( outputValue ) ;
}
` }};function cee(e){let{inputs:t,backend:n,attrs:a}=e,{image:r,transforms:s}=t,{interpolation:i,fillMode:o,fillValue:l,outputShape:u}=a,[c,p,d,h]=r.shape,[m,f]=u!=null?u:[p,d],g=[c,m,f,h],y=new uee(p,d,i,o,l,g);return n.runWebGLProgram(y,[r,s],"float32")}var pee={kernelName:Ld,backendName:"webgl",kernelFunc:cee};function dee(e){let{inputs:t,attrs:n,backend:a}=e,{axis:r}=n,{x:s}=t;cp(s,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let i=a.readSync(s.dataId),{outputValues:o,outputShape:l,indices:u}=eX(i,r,s.shape,s.dtype);return[a.makeTensorInfo(l,s.dtype,o),a.makeTensorInfo([u.length],"int32",u)]}var hee={kernelName:zd,backendName:"webgl",kernelFunc:dee};function mee(e){let{inputs:t,backend:n,attrs:a}=e,{value:r}=t,{axis:s}=a;s<0&&(s+=r.shape.length);let i=r,o=i.shape.length,l=r.shape[s],u=new Array(o-1),c=0;for(let f=0;f<o;f++)f!==s&&(u[c++]=i.shape[f]);let p=[],d=new Array(o).fill(0),h=i.shape.slice();h[s]=1;let m=new Array(l);for(let f=0;f<m.length;f++){d[s]=f;let g=mp({inputs:{x:i},backend:n,attrs:{begin:d,size:h}}),y=ye({inputs:{x:g},backend:n,attrs:{shape:u}});m[f]=y,p.push(g)}return p.forEach(f=>n.disposeIntermediateTensorInfo(f)),m}var fee={kernelName:kl,backendName:"webgl",kernelFunc:mee},gee=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,a=e.batchSize,r=e.inSize,s=e.numSegments,i=s*Math.ceil(r/n);this.outputShape=[a,i];let o="0.0",l="sumValue",u=Math.floor(n/4)*4,c=n%4,p= `
2020-12-15 14:15:43 +01:00
sumValue += dot ( values , segFilter ) ;
2021-01-12 16:01:34 +01:00
` ,d="";r%n>0&&(d= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
2020-12-15 14:15:43 +01:00
return initializationValue ;
}
2021-01-12 16:01:34 +01:00
` );let h="";r%n>0&&(h= `
if ( inIdx < 0 || inIdx >= $ { r } ) {
2020-12-15 14:15:43 +01:00
return - 1.0 ;
2020-10-29 05:16:50 +01:00
}
` ),this.userCode= `
2021-01-12 16:01:34 +01:00
const float initializationValue = $ { o } ;
2020-10-29 05:16:50 +01:00
float getValue ( int batch , int inIdx ) {
2021-01-12 16:01:34 +01:00
$ { d }
2020-10-29 05:16:50 +01:00
return getX ( batch , inIdx ) ;
}
2020-12-15 14:15:43 +01:00
float getSegmentIdAtIndex ( int inIdx ) {
2021-01-12 16:01:34 +01:00
$ { h }
2020-12-15 14:15:43 +01:00
return getSegmentIds ( inIdx ) ;
}
2020-10-29 05:16:50 +01:00
void main ( ) {
ivec2 coords = getOutputCoords ( ) ;
int batch = coords [ 0 ] ;
int outIdx = coords [ 1 ] ;
2020-12-15 14:15:43 +01:00
int inOffset = int ( floor ( float ( outIdx ) / float (
2021-01-12 16:01:34 +01:00
$ { s } ) ) * float ( $ { n } ) ) ;
int currentSeg = int ( mod ( float ( outIdx ) , float ( $ { s } ) ) ) ;
2020-10-29 05:16:50 +01:00
float sumValue = 0.0 ;
2021-03-09 23:32:33 +01:00
for ( int i = 0 ; i < $ { u } ; i += 4 ) {
2020-10-29 05:16:50 +01:00
int inIdx = inOffset + i ;
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
getValue ( batch , inIdx + 2 ) ,
getValue ( batch , inIdx + 3 )
) ;
2020-12-15 14:15:43 +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-01-12 16:01:34 +01:00
$ { p }
2020-10-29 05:16:50 +01:00
}
2021-03-09 23:32:33 +01:00
int inIdx = inOffset + $ { u } ;
if ( $ { c === 1 } ) {
2020-12-15 14:15:43 +01:00
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
initializationValue ,
initializationValue ,
initializationValue
) ;
int inIdxSeg = int ( getSegmentIdAtIndex ( inIdx ) ) ;
vec4 segFilter = vec4 (
int ( getSegmentIdAtIndex ( inIdx ) ) == currentSeg ? 1 : 0 ,
0 ,
0 ,
0
) ;
2020-10-29 05:16:50 +01:00
2021-01-12 16:01:34 +01:00
$ { p }
2021-03-09 23:32:33 +01:00
} else if ( $ { c === 2 } ) {
2020-10-29 05:16:50 +01:00
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
2020-12-15 14:15:43 +01:00
getValue ( batch , inIdx + 1 ) ,
initializationValue ,
initializationValue
) ;
vec4 segFilter = vec4 (
int ( getSegmentIdAtIndex ( inIdx ) ) == currentSeg ? 1 : 0 ,
int ( getSegmentIdAtIndex ( inIdx + 1 ) ) == currentSeg ? 1 : 0 ,
0 ,
0
) ;
2020-10-29 05:16:50 +01:00
2021-01-12 16:01:34 +01:00
$ { p }
2021-03-09 23:32:33 +01:00
} else if ( $ { c === 3 } ) {
2020-10-29 05:16:50 +01:00
vec4 values = vec4 (
getValue ( batch , inIdx ) ,
getValue ( batch , inIdx + 1 ) ,
2020-12-15 14:15:43 +01:00
getValue ( batch , inIdx + 2 ) ,
initializationValue
) ;
2020-10-29 05:16:50 +01:00
2020-12-15 14:15:43 +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
) ;
2020-10-29 05:16:50 +01:00
2021-01-12 16:01:34 +01:00
$ { p }
2020-10-29 05:16:50 +01:00
}
2021-01-12 16:01:34 +01:00
setOutput ( $ { l } ) ;
2020-10-29 05:16:50 +01:00
}
2021-03-09 23:32:33 +01:00
` }};function yee(e){let{inputs:t,backend:n,attrs:a}=e,{x:r,segmentIds:s}=t,{numSegments:i}=a,o=r.shape.length,l=[],u=0,c=E.getAxesPermutation([u],o),p=r;c!=null&&(p=_n({inputs:{x:r},backend:n,attrs:{perm:c}}),l.push(p),u=E.getInnerMostAxes(1,o)[0]);let d=E.segment_util.computeOutShape(p.shape,u,i),h=I.sizeFromShape([p.shape[u]]),m=ye({inputs:{x:p},backend:n,attrs:{shape:[-1,h]}});l.push(m);let f=jd(r.dtype),g=(v,k,w,S,A)=>{let F=v.shape[0],D=v.shape[1],R=E.segment_util.segOpComputeOptimalWindowSize(D,A),L={windowSize:R,inSize:D,batchSize:F,numSegments:A},W=new gee(L,k),U=n.compileAndRun(W,[v,w],S);if(l.push(U),U.shape[1]===A)return U;let G=qT({backend:n,attrs:{start:0,stop:A,step:1,dtype:"float32"}}),X=YT({inputs:{x:G},backend:n,attrs:{reps:[D/R]}});return l.push(G),l.push(X),g(U,k,X,S,A)},y=g(m,"unsortedSegmentSum",s,f,i),b=ye({inputs:{x:y},backend:n,attrs:{shape:d}}),x=b;if(c!=null){l.push(b);let v=E.getUndoAxesPermutation(c);x=_n({inputs:{x},backend:n,attrs:{perm:v}})}return l.forEach(v=>n.disposeIntermediateTensorInfo(v)),x}var bee={kernelName:pc,backendName:"webgl",kernelFunc:yee},xee=[lJ,pJ,YX,QX,tK,rK,iK,uK,pK,hK,yK,xK,kK,TK, $ K,AK,RK,zK,OK,UK,HK,qK,JK,r7,i7,d7,m7,b7,w7,EX,T7,R7,O7,F7,B7,U7,z7,j7,K7,Q7,eY,nY,sY,pY,hY,oY,gY,xY,IY,CY,EY,MY,RY,PY,LY,WY,VY,GY,jY,YY,e9,n9,r9,o9,p9,f9,x9,_X,w9,N7,N9,C9,_9,DX,M9,L9,W9,q9,G9,J9,eJ,rJ,hJ,wJ,xJ,TJ,CJ,FJ,yJ,EJ,DJ,OJ,BJ,HJ,ZJ,LX,tQ,rQ,oQ,cQ,l7,hQ,fQ,yQ,vQ,NQ,RX,SQ,CQ,u7,KJ,_Q,LQ,MQ,WX,VQ,HQ,XQ,JQ,tZ,aZ,iZ,uZ,pZ,mZ,yZ,vZ,IZ,SZ,FZ,n7,JJ, $ Z,MZ,PZ,LZ,BZ,GZ,jZ,XZ,JZ,YJ,qX,eee,aee,iee,lee,pee,XX,hee,fee,bee,mQ];for(let e of xee)hc(e);var vee="3.3.0",wee={"tfjs-core":Q0,"tfjs-backend-cpu":BU,"tfjs-backend-webgl":FX,"tfjs-data":TN,"tfjs-layers":im,"tfjs-converter":xN,tfjs:vee},Hn;(function(e){e[e.float32=0]="float32",e[e.int32=1]="int32",e[e.bool=2]="bool",e[e.string=3]="string",e[e.complex64=4]="complex64"})(Hn||(Hn={}));var gp;(function(e){e[e.linear=0]="linear",e[e.relu=1]="relu",e[e.relu6=2]="relu6",e[e.prelu=3]="prelu",e[e.leakyrelu=4]="leakyrelu"})(gp||(gp={}));var JT;function kee(e){JT=e.wasm.cwrap(gi,null,["number","array","number","number","array","number","number","number","number","number","number","number","number"])}function Iee(e){let{inputs:t,backend:n,attrs:a}=e,{a:r,b:s,bias:i,preluActivationWeights:o}=t;if(r.dtype!=="float32"||s.dtype!=="float32")throw new Error("_FusedMatMul for non non-float32 tensors not yet supported.");let{transposeA:l,transposeB:u,activation:c,leakyreluAlpha:p}=a,d=n.dataIdMap.get(r.dataId).id,h=n.dataIdMap.get(s.dataId).id,m=0;if(i!=null){let A=n.dataIdMap.get(i.dataId);if(A.shape.length!==1)throw new Error( ` _FusedMatMul only supports rank - 1 bias but got rank $ { A . shape . length } . ` );m=A.id}let f=o==null?0:n.dataIdMap.get(o.dataId).id,g=gp[c];if(g==null)throw new Error( ` $ { c } activation not yet supported for FusedConv2D in the wasm backend . ` );let y=l?r.shape[2]:r.shape[1],b=u?s.shape[1]:s.shape[2],x=r.shape[0],v=n.makeOutput([x,y,b],r.dtype),k=n.dataIdMap.get(v.dataId).id,w=new Uint8Array(new Int32Array(r.shape).buffer),S=new Uint8Array(new Int32Array(s.shape).buffer);return JT(d,w,r.shape.length,h,S,s.shape.length,l,u,g,m,f,p||0,k),v}var Nee={kernelName:gi,backendName:"wasm",setupFunc:kee,kernelFunc:Iee};function En(e){let t;function n(r){t=r.wasm.cwrap(e,null,["number","number"])}function a(r){let{backend:s,inputs:{x:i}}=r,o=s.dataIdMap.get(i.dataId).id,l=s.makeOutput(i.shape,i.dtype),u=s.dataIdMap.get(l.dataId).id;return I.sizeFromShape(l.shape)===0||t(o,u),l}return{kernelName:e,backendName:"wasm",setupFunc:n,kernelFunc:a}}var Tee=En(So);function gn(e,t,n){let a;function r(i){a=i.wasm.cwrap(e,null,["number","array","number","number","array","number","number","number"])}function s(i){let{backend:o,inputs:l}=i,{a:u,b:c}=l,p=o.dataIdMap.get(u.dataId).id,d=o.dataIdMap.get(c.dataId).id,h=n!=null?n:u.dtype,m=E.assertAndGetBroadcastShape(u.shape,c.shape),f=o.makeOutput(m,h);if(I.sizeFromShape(m)===0)return f;let g=new Uint8Array(new Int32Array(u.shape).buffer),y=new Uint8Array(new Int32Array(c.shape).buffer),b=o.dataIdMap.get(f.dataId).id,x=()=>a(
2020-10-12 20:28:19 +02:00
/ * *
* @ license
* Copyright 2017 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT - style
* license that can be found in the LICENSE file or at
* https : //opensource.org/licenses/MIT.
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2018 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
*
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2018 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2019 Google LLC
*
* Use of this source code is governed by an MIT - style
* license that can be found in the LICENSE file or at
* https : //opensource.org/licenses/MIT.
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2019 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
*
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2019 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2020 Google Inc . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2020 Google LLC
*
* Use of this source code is governed by an MIT - style
* license that can be found in the LICENSE file or at
* https : //opensource.org/licenses/MIT.
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2020 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
/ * *
* @ license
* Copyright 2020 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the License ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an AS IS BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
2021-01-10 16:35:51 +01:00
/ * *
* @ license
* Copyright 2021 Google LLC . All Rights Reserved .
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* === === === === === === === === === === === === === === === === === === === === === === === === === ==
* /
2020-10-12 20:28:19 +02:00
/** @license See the LICENSE file. */
2020-09-16 18:43:53 +02:00
//# sourceMappingURL=face-api.esm.js.map