face-api/dist/face-api.js

7209 lines
1.4 MiB
JavaScript
Raw Normal View History

2021-09-08 19:51:28 +02:00
/*
Face-API
homepage: <https://github.com/vladmandic/face-api>
author: <https://github.com/vladmandic>'
2021-09-08 19:51:28 +02:00
*/
2021-12-01 21:37:52 +01:00
var faceapi=(()=>{var AI=Object.defineProperty;var GF=e=>AI(e,"__esModule",{value:!0});var Xb=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(t,n)=>(typeof require!="undefined"?require:t)[n]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var ih=(e,t)=>{GF(e);for(var n in t)AI(e,n,{get:t[n],enumerable:!0})};var abe={};ih(abe,{AgeGenderNet:()=>ig,BoundingBox:()=>ll,Box:()=>dt,ComposableTask:()=>Wr,ComputeAllFaceDescriptorsTask:()=>Do,ComputeFaceDescriptorsTaskBase:()=>mg,ComputeSingleFaceDescriptorTask:()=>Ro,DetectAllFaceLandmarksTask:()=>bg,DetectAllFacesTask:()=>Dp,DetectFaceLandmarksTaskBase:()=>gg,DetectFacesTaskBase:()=>vg,DetectSingleFaceLandmarksTask:()=>yg,DetectSingleFaceTask:()=>xg,Dimensions:()=>On,FACE_EXPRESSION_LABELS:()=>nI,FaceDetection:()=>St,FaceDetectionNet:()=>$$,FaceExpressionNet:()=>ag,FaceExpressions:()=>Fo,FaceLandmark68Net:()=>vl,FaceLandmark68TinyNet:()=>ug,FaceLandmarkNet:()=>v$,FaceLandmarks:()=>Sr,FaceLandmarks5:()=>s$,FaceLandmarks68:()=>pl,FaceMatch:()=>kp,FaceMatcher:()=>kg,FaceRecognitionNet:()=>xl,Gender:()=>Ys,LabeledBox:()=>Ip,LabeledFaceDescriptors:()=>qs,NetInput:()=>Ks,NeuralNetwork:()=>gn,ObjectDetection:()=>Ao,Point:()=>Pe,PredictedBox:()=>a$,Rect:()=>dl,SsdMobilenetv1:()=>wu,SsdMobilenetv1Options:()=>zr,TinyFaceDetector:()=>Cl,TinyFaceDetectorOptions:()=>fg,TinyYolov2:()=>Il,TinyYolov2Options:()=>Is,allFaces:()=>tbe,allFacesSsdMobilenetv1:()=>j$,allFacesTinyYolov2:()=>ebe,awaitMediaLoaded:()=>Yk,bufferToImage:()=>Qk,computeFaceDescriptor:()=>zge,createCanvas:()=>bu,createCanvasFromMedia:()=>Tp,createFaceDetectionNet:()=>Dge,createFaceRecognitionNet:()=>kge,createSsdMobilenetv1:()=>A$,createTinyFaceDetector:()=>nbe,createTinyYolov2:()=>Oge,detectAllFaces:()=>wg,detectFaceLandmarks:()=>G$,detectFaceLandmarksTiny:()=>Bge,detectLandmarks:()=>Zge,detectSingleFace:()=>Jge,draw:()=>oI,env:()=>rt,euclideanDistance:()=>gI,extendWithAge:()=>dg,extendWithFaceDescriptor:()=>lg,extendWithFaceDetection:()=>fu,extendWithFaceExpressions:()=>og,extendWithFaceLandmarks:()=>yl,extendWithGender:()=>pg,extractFaceTensors:()=>fl,extractFaces:()=>hl,fetchImage:()=>cge,fetchJson:()=>eI,fetchNetWeights:()=>lge,fetchOrThrow:()=>Xs,fetchVideo:()=>dge,getContext2dOrThrow:()=>Xn,getMediaDimensions:()=>gu,imageTensorToCanvas:()=>Zk,imageToSquare:()=>Jk,inverseSigmoid:()=>nge,iou:()=>Bk,isMediaElement:()=>Ym,isMediaLoaded:()=>Cp,isWithAge:()=>Ige,isWithFaceDetection:()=>ws,isWithFaceExpressions:()=>rI,isWithFaceLandmarks:()=>vu,isWithGender:()=>Sge,loadAgeGenderModel:()=>Xge,loadFaceDetectionModel:()=>Yge,loadFaceExpressionModel:()=>Kge,loadFaceLandmarkModel:()=>Hge,loadFaceLandmarkTinyModel:()=>jge,loadFaceRecognitionModel:()=>qge,loadSsdMobilenetv1Model:()=>H$,loadTinyFaceDetectorModel:()=>Uge,loadTinyYolov2Model:()=>Gge,loadWeightMap:()=>tI,locateFaces:()=>Qge,matchDimensions:()=>pge,minBbox:()=>zk,nets:()=>st,nonMaxSuppression:()=>Wk,normalize:()=>rs,padToSquare:()=>Vk,predictAgeAndGender:()=>Vge,recognizeFaceExpressions:()=>Wge,resizeResults:()=>q$,resolveInput:()=>mu,shuffleArray:()=>tge,sigmoid:()=>wp,ssdMobilenetv1:()=>U$,tf:()=>Rk,tinyFaceDetector:()=>Mge,tinyYolov2:()=>Lge,toNetInput:()=>xt,utils:()=>Lk,validateConfig:()=>pI,version:()=>sbe});var Rk={};ih(Rk,{Abs:()=>Vo,Acos:()=>Vu,Acosh:()=>Uu,AdadeltaOptimizer:()=>kf,AdagradOptimizer:()=>If,AdamOptimizer:()=>Sf,AdamaxOptimizer:()=>Cf,Add:()=>_s,AddN:()=>la,All:()=>Gu,Any:()=>Hu,ArgMax:()=>da,ArgMin:()=>ju,Asin:()=>qu,Asinh:()=>Ku,Atan:()=>Xu,Atan2:()=>Qu,Atanh:()=>Yu,AvgPool:()=>pa,AvgPool3D:()=>ql,AvgPool3DGrad:()=>gh,AvgPoolGrad:()=>mh,BackendWasm:()=>t$,BatchMatMul:()=>ha,BatchToSpaceND:()=>Uo,Bincount:()=>bh,BroadcastArgs:()=>yh,BroadcastTo:()=>jI,Callback:()=>rC,CallbackList:()=>H0,Cast:()=>fa,Ceil:()=>ma,ClipByValue:()=>Es,Complex:()=>Kl,ComplexAbs:()=>Xl,Concat:()=>Go,Conv2D:()=>ga,Conv2DBackpropFilter:()=>vh,Conv2DBackpropInput:()=>ba,Conv3D:()=>Yl,Conv3DBackpropFilterV2:()=>xh,Conv3DBackpropInputV2:()=>
`)),u.join(`
`)}function eR(e,t,n,r){let s=wt(t),a=r[r.length-1],o=new Array(a).fill(0),i=t.length,u=n==="complex64"?bd(e):e;if(i>1)for(let l=0;l<s/a;l++){let c=l*a;for(let d=0;d<a;d++)o[d]=Math.max(o[d],gd(u[c+d],0,n).length)}return o}function gd(e,t,n){let r;return Array.isArray(e)?r=`${parseFloat(e[0].toFixed(ay))} + ${parseFloat(e[1].toFixed(ay))}j`:ua(e)?r=`'${e}'`:n==="bool"?r=e1(e):r=parseFloat(e.toFixed(ay)).toString(),jl(r,t)}function e1(e){return e===0?"false":"true"}function qh(e,t,n,r,s,a=!0){let o=n==="complex64"?2:1,i=t[0],u=t.length;if(u===0){if(n==="complex64"){let m=bd(e);return[gd(m[0],0,n)]}return n==="bool"?[e1(e[0])]:[e[0].toString()]}if(u===1){if(i>JI){let g=md*o,b=Array.from(e.slice(0,g)),y=Array.from(e.slice((i-md)*o,i*o));return n==="complex64"&&(b=bd(b),y=bd(y)),["["+b.map((v,x)=>gd(v,s[x],n)).join(", ")+", ..., "+y.map((v,x)=>gd(v,s[i-md+x],n)).join(", ")+"]"]}let m=n==="complex64"?bd(e):Array.from(e);return["["+m.map((g,b)=>gd(g,s[b],n)).join(", ")+"]"]}let l=t.slice(1),c=r.slice(1),d=r[0]*o,p=[];if(i>JI){for(let m=0;m<md;m++){let g=m*d,b=g+d;p.push(...qh(e.slice(g,b),l,n,c,s,!1))}p.push("...");for(let m=i-md;m<i;m++){let g=m*d,b=g+d;p.push(...qh(e.slice(g,b),l,n,c,s,m===i-1))}}else for(let m=0;m<i;m++){let g=m*d,b=g+d;p.push(...qh(e.slice(g,b),l,n,c,s,m===i-1))}let h=u===2?",":"";p[0]="["+p[0]+h;for(let m=1;m<p.length-1;m++)p[m]=" "+p[m]+h;let f=`,
`;for(let m=2;m<u;m++)f+=`
`;return p[p.length-1]=" "+p[p.length-1]+"]"+(a?"":f),p}function bd(e){let t=[];for(let n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}var Kt=class{constructor(e,t,n){if(this.dtype=t,this.shape=e.slice(),this.size=wt(e),n!=null){let r=n.length;P(r===this.size,()=>`Length of values '${r}' does not match the size inferred by the shape '${this.size}'.`)}if(t==="complex64")throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag).");this.values=n||OI(t,this.size),this.strides=Bu(e)}set(e,...t){t.length===0&&(t=[0]),P(t.length===this.rank,()=>`The number of provided coordinates (${t.length}) must match the rank (${this.rank})`);let n=this.locToIndex(t);this.values[n]=e}get(...e){e.length===0&&(e=[0]);let t=0;for(let r of e){if(r<0||r>=this.shape[t]){let s=`Requested out of range element at ${e}. Buffer shape=${this.shape}`;throw new Error(s)}t++}let n=e[e.length-1];for(let r=0;r<e.length-1;++r)n+=this.strides[r]*e[r];return this.values[n]}locToIndex(e){if(this.rank===0)return 0;if(this.rank===1)return e[0];let t=e[e.length-1];for(let n=0;n<e.length-1;++n)t+=this.strides[n]*e[n];return t}indexToLoc(e){if(this.rank===0)return[];if(this.rank===1)return[e];let t=new Array(this.shape.length);for(let n=0;n<t.length-1;++n)t[n]=Math.floor(e/this.strides[n]),e-=t[n]*this.strides[n];return t[t.length-1]=e,t}get rank(){return this.shape.length}toTensor(){return as().makeTensor(this.values,this.shape,this.dtype)}},as=null,bc=null,tR=null;function nR(e){as=e}function rR(e){bc=e}function sR(e){tR=e}var Ae=class{constructor(e,t,n,r){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=wt(e),this.strides=Bu(e),this.dataId=n,this.id=r,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){let e=await this.data();return bc.buffer(this.shape,this.dtype,e)}bufferSync(){return bc.buffer(this.shape,this.dtype,this.dataSync())}async array(){let e=await this.data();return zu(this.shape,e,this.dtype==="complex64")}arraySync(){return zu(this.shape,this.dataSync(),this.dtype==="complex64")}async data(){this.throwIfDisposed();let e=as().read(this.dataId);if(this.dtype==="string"){let t=await e;try{return t.map(n=>jh(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=as().readSync(this.dataId);if(this.dtype==="string")try{return e.map(t=>jh(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 as().read(this.dataId);return this.dtype==="string"?e:new Uint8Array(e.buffer)}dispose(){this.isDisposed||(as().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(e=!1){return bc.print(this,e)}clone(){return this.throwIfDisposed(),bc.clone(this)}toString(e=!1){let t=this.dataSync();return JD(t,this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),bc.cast(this,e)}variable(e=!0,t,n){return this.throwIfDisposed(),as().makeVariable(this,e,t,n)}};Object.defineProperty(Ae,Symbol.hasInstance,{value:e=>!!e&&e.data!=null&&e.dataSync!=null&&e.throwIfDisposed!=null});function aR(){return ty("Tensor",()=>Ae)}aR();var oo=class extends Ae{constructor(e,t,n,r){super(e.shape,e.dtype,e.dataId,r);this.trainable=t,this.name=n}assign(e){if(e.dtype!==this.dtype)throw new Error(`dtype of the new value (${e.dtype}) and previous value (${this.dtype}) must match`);if(!ia(e.shape,this.shape))throw new Error(`shape of the new value (${e.shape}) and previous value (${this.shape}) must match`);as().disposeTensor(this),this.dataId=e.dataId,as().incRef(this,null)}dispose(){as().disposeVariable(this),this.isDisposedInternal=!0}};Object.defineProperty(oo,Symbol.hasInstance,{value:e=>e instanceof Ae&&e.as
Manifest JSON has weights with names: ${i.join(", ")}.`)}let u=s.reduce((h,f,m)=>(f&&h.push(m),h),[]),l=[];u.forEach(h=>{t[h].paths.forEach(f=>{let m=n+(n.endsWith("/")?"":"/")+f;l.push(m)})});let c=await e(l),d={},p=0;return u.forEach(h=>{let f=t[h].paths.length,m=0;for(let x=0;x<f;x++)m+=c[p+x].byteLength;let g=new ArrayBuffer(m),b=new Uint8Array(g),y=0;for(let x=0;x<f;x++){let k=new Uint8Array(c[p+x]);b.set(k,y),y+=k.byteLength}a[h].forEach(x=>{let k=g.slice(x.groupOffset,x.groupOffset+x.sizeBytes),T=c1(k,[x.manifestEntry]);for(let C in T)d[C]=T[C]}),p+=f}),d}}var n3="application/octet-stream",r3="application/json",Cy=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?(P(typeof t.fetchFunc=="function",()=>"Must pass a function that matches the signature of `fetch` (see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)"),this.fetch=t.fetchFunc):this.fetch=X().platform.fetch,P(e!=null&&e.length>0,()=>"URL path for http must not be null, undefined or empty."),Array.isArray(e)&&P(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}],r=p1(e,n);t.body.append("model.json",new Blob([JSON.stringify(r)],{type:r3}),"model.json"),e.weightData!=null&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:n3}),"model.weights.bin");let s=await this.fetch(this.path,t);if(s.ok)return{modelArtifactsInfo:xd(e),responses:[s]};throw new Error(`BrowserHTTPRequest.save() failed due to HTTP response status ${s.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(s){let a=`Failed to parse model JSON of response from ${this.path}.`;throw this.path.endsWith(".pb")?a+=" Your path contains a .pb file extension. Support for .pb models have been removed in TensorFlow.js 1.0 in favor of .json models. You can re-convert your Python TensorFlow model using the TensorFlow.js 1.0 conversion scripts or you can convert your.pb models with the 'pb2json'NPM script in the tensorflow/tfjs-converter repository.":a+=" Please make sure the server is serving valid JSON for this request.",new Error(a)}let n=t.modelTopology,r=t.weightsManifest;if(n==null&&r==null)throw new Error(`The JSON from HTTP path ${this.path} contains neither model topology or manifest for weights.`);return vy(t,s=>this.loadWeights(s))}async loadWeights(e){let t=Array.isArray(this.path)?this.path[1]:this.path,[n,r]=s3(t),s=this.weightPathPrefix||n,a=[];for(let l of e)a.push(...l.weights);let o=[],i=[];for(let l of e)for(let c of l.paths)this.weightUrlConverter!=null?i.push(this.weightUrlConverter(c)):o.push(s+c+r);this.weightUrlConverter&&o.push(...await Promise.all(i));let u=await I1(o,{requestInit:this.requestInit,fetchFunc:this.fetch,onProgress:this.onProgress});return[a,yy(u)]}};Cy.URL_SCHEME_REGEX=/^https?:\/\//;function s3(e){let t=e.lastIndexOf("/"),n=e.lastIndexOf("?"),r=e.substring(0,t),s=n>t?e.substring(n):"";return[r+"/",s]}function Ty(e){return e.match(Cy.URL_SCHEME_REGEX)!=null}var C1=(e,t)=>{if(typeof fetch=="undefined"&&(t==null||t.fetchFunc==null))return null;{let n=!0;if(Array.isArray(e)?n=e.every(r=>Ty(r)):n=Ty(e),n)return Ny(e,t)}return null};Pt.registerSaveRouter(C1);Pt.registerLoadRouter(C1);function Ny(e,t){return new Cy(e,t)}function a3(e,t){return Ny(e,t)}var _y=class{
2021-09-11 17:11:38 +02:00
Actual: ${s}.
2021-12-01 21:37:52 +01:00
Expected: ${a}.`);for(let o=0;o<a.length;++o){let i=s[o],u=a[o];if(!n(i,u))throw new Error(`Arrays differ: actual[${o}] = ${i}, expected[${o}] = ${u}.
2021-09-11 17:11:38 +02:00
Actual: ${s}.
2021-12-01 21:37:52 +01:00
Expected: ${a}.`)}}function F3(e,t){e().then(()=>t.fail(),()=>t())}function D3(e,t){let n=typeof t=="string"||typeof t=="number"||typeof t=="boolean"?[t]:t;return ua(e)||ua(e[0])||ua(t)||ua(t[0])?Py(e,n,(r,s)=>r==s):Py(e,t,(r,s)=>Oy(r,s,0))}function R3(e,t,n){if(n==null&&(n=Ry()),!Oy(e,t,n))throw new Error(`Numbers differ: actual === ${e}, expected === ${t}`)}function Oy(e,t,n){return!isFinite(e)&&!isFinite(t)?!0:!(isNaN(e)||isNaN(t)||Math.abs(e-t)>n)}function P3(e,t,n){for(let r=0;r<e.length;r++)if(e[r]<t||e[r]>n)throw new Error(`Value out of range:${e[r]} low: ${t}, high: ${n}`)}function O3(e,t){expect(new Float32Array(e)).toEqual(new Float32Array(t))}function G1(e){for(let t=0;t<e.length;t++){let n=e[t];Array.isArray(n)?G1(n):e[t]=fd(n)}return e}var M3="0.0.0";function L3(){X().set("PROD",!0)}function B3(){X().set("DEBUG",!0)}function z3(){X().set("DEPRECATION_WARNINGS_ENABLED",!1),console.warn("TensorFlow.js deprecation warnings have been disabled.")}function My(e){X().getBool("DEPRECATION_WARNINGS_ENABLED")&&console.warn(e+" You can disable deprecation warnings with tf.disableDeprecationWarnings().")}sR(My);function W3(){z.disposeVariables()}function is(){return z}function Zh(){return z.memory()}function V3(e){return z.profile(e)}function M(e,t){return z.tidy(e,t)}function Fe(e){dy(e).forEach(n=>n.dispose())}function nn(e){return z.keep(e)}function U3(e){return z.time(e)}function G3(e){return z.setBackend(e)}function H3(){return z.ready()}function j3(){return z.backendName}function q3(e){z.removeBackend(e)}function K3(e){return z.findBackend(e)}function X3(e){return z.findBackendFactory(e)}function kd(e,t,n=1){return z.registerBackend(e,t,n)}function H1(){return z.backend}function Y3(e,t){X().setPlatform(e,t)}function Q3(e,t){let n=$(e,"a","add"),r=$(t,"b","add");[n,r]=At(n,r);let s={a:n,b:r};return z.runKernel(_s,s)}var Z=W({add_:Q3});function Z3(e,t){let n=$(e,"a","floorDiv"),r=$(t,"b","floorDiv");[n,r]=At(n,r);let s={a:n,b:r};return z.runKernel(Ca,s)}var Ly=W({floorDiv_:Z3});function J3(e,t){let n=$(e,"a","div"),r=$(t,"b","div");if([n,r]=At(n,r),n.dtype==="int32"&&r.dtype==="int32")return Ly(n,r);let s={a:n,b:r},a={};return z.runKernel(wa,s,a)}var me=W({div_:J3});function eP(e,t){let n=$(e,"a","mul"),r=$(t,"b","mul");[n,r]=At(n,r);let s={a:n,b:r};return z.runKernel(Ma,s)}var V=W({mul_:eP});function tP(e){let t=$(e,"x","abs");if(t.dtype==="complex64"){let n={x:t};return z.runKernel(Xl,n)}else{let n={x:t};return z.runKernel(Vo,n)}}var Xt=W({abs_:tP});function nP(e){let n={x:$(e,"x","acos")};return z.runKernel(Vu,n)}var j1=W({acos_:nP});function rP(e){let n={x:$(e,"x","acosh")};return z.runKernel(Uu,n)}var q1=W({acosh_:rP});function sP(e){P(Array.isArray(e),()=>"The argument passed to tf.addN() must be a list of tensors"),P(e.length>=1,()=>`Must pass at least one tensor to tf.addN(), but got ${e.length}`);let t=e.map((s,a)=>$(s,`tensors${a}`,"addN")),n=t[0];t.forEach(s=>{if(s.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),t.forEach(s=>{if(!ia(s.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});let r=t;return z.runKernel(la,r)}var K1=W({addN_:sP});function aP(e,t=null,n=!1){let s={x:$(e,"x","all","bool")},a={axis:t,keepDims:n};return z.runKernel(Gu,s,a)}var By=W({all_:aP});function oP(e,t=null,n=!1){let s={x:$(e,"x","any","bool")},a={axis:t,keepDims:n};return z.runKernel(Hu,s,a)}var Jh=W({any_:oP});function iP(e,t=0){let r={x:$(e,"x","argMax")},s={axis:t};return z.runKernel(da,r,s)}var wc=W({argMax_:iP});function uP(e,t=0){let r={x:$(e,"x","argMin")},s={axis:t};return z.runKernel(ju,r,s)}var X1=W({argMin_:uP});function cP(e){let n={x:$(e,"x","asin")};return z.runKernel(qu,n)}var Y1=W({asin_:cP});function lP(e){let n={x:$(e,"x","asinh")};return z.runKernel(Ku,n)}var Q1=W({asinh_:lP});function dP(e){let n={x:$(e,"x","atan")};return z.runKernel(Xu,n)}var Z1=W({atan_:dP});function pP(e,t){let n=$(e,"a","atan2"),r=$(t,"b","atan2");[n,r]=At(n,r);let s={a:n,b:r};return z.runKernel(Qu,s)}var J1=W({atan2_:pP});function hP(e){let n={x:$(
with dtype ${a.dtype}. `)}),n.length===1)return Fs(n[0]);let r=n,s={axis:t};return z.runKernel(Go,r,s)}var ot=W({concat_:IP});function SP(e){let n={x:$(e,"x","sigmoid","float32")};return z.runKernel(ja,n)}var $r=W({sigmoid_:SP});function CP(e,t,n){let r=$(e,"x","slice","string_or_numeric");if(r.rank===0)throw new Error("Slicing scalar is not possible");let s={x:r},a={begin:t,size:n};return z.runKernel(yi,s,a)}var Ve=W({slice_:CP});function TP(e){let n={x:$(e,"x","tanh","float32")};return z.runKernel(Za,n)}var Ic=W({tanh_:TP});function NP(e,t,n,r,s,a){let o=$(e,"forgetBias","basicLSTMCell"),i=$(t,"lstmKernel","basicLSTMCell"),u=$(n,"lstmBias","basicLSTMCell"),l=$(r,"data","basicLSTMCell"),c=$(s,"c","basicLSTMCell"),d=$(a,"h","basicLSTMCell"),p=ot([l,d],1),h=Re(p,i),f=Z(h,u),m=f.shape[0],g=f.shape[1]/4,b=[m,g],y=Ve(f,[0,0],b),v=Ve(f,[0,g],b),x=Ve(f,[0,g*2],b),k=Ve(f,[0,g*3],b),T=Z(V($r(y),Ic(v)),V(c,$r(Z(o,x)))),C=V(Ic(T),$r(k));return[T,C]}var _P=W({basicLSTMCell_:NP});function EP(e,t,n){let r=$(e,"x","batchToSpaceND"),s=t.reduce((i,u)=>i*u);P(r.rank>=1+t.length,()=>`input rank is ${r.rank} but should be > than blockShape.length ${t.length}`),P(n.length===t.length,()=>`crops.length is ${n.length} but should be equal to blockShape.length ${t.length}`),P(r.shape[0]%s==0,()=>`input tensor batch is ${r.shape[0]} but is not divisible by the product of the elements of blockShape ${t.join(" * ")} === ${s}`);let a={x:r},o={blockShape:t,crops:n};return z.runKernel(Uo,a,o)}var tf=W({batchToSpaceND_:EP});function AP(e){let t;return e.rank===0||e.rank===1?t=G(e,[1,1,1,e.size]):e.rank===2?t=G(e,[1,1,e.shape[0],e.shape[1]]):e.rank===3?t=G(e,[1,e.shape[0],e.shape[1],e.shape[2]]):t=e,t}function $P(e,t,n,r,s,a){a==null&&(a=.001);let o=$(e,"x","batchNorm"),i=$(t,"mean","batchNorm"),u=$(n,"variance","batchNorm"),l;s!=null&&(l=$(s,"scale","batchNorm"));let c;r!=null&&(c=$(r,"offset","batchNorm")),P(i.rank===u.rank,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),P(c==null||i.rank===c.rank,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),P(l==null||i.rank===l.rank,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let p={x:AP(o),scale:l,offset:c,mean:i,variance:u},h={varianceEpsilon:a},f=z.runKernel(Ta,p,h);return G(f,o.shape)}var ho=W({batchNorm_:$P});function FP(e,t,n,r,s,a){let o=$(e,"x","batchNorm"),i=$(t,"mean","batchNorm"),u=$(n,"variance","batchNorm"),l;s!=null&&(l=$(s,"scale","batchNorm"));let c;return r!=null&&(c=$(r,"offset","batchNorm")),P(o.rank===2,()=>`Error in batchNorm2D: x must be rank 2 but got rank ${o.rank}.`),P(i.rank===2||i.rank===1,()=>`Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank ${i.rank}.`),P(u.rank===2||u.rank===1,()=>`Error in batchNorm2D: variance must be rank 2 or rank 1 but got rank ${u.rank}.`),l!=null&&P(l.rank===2||l.rank===1,()=>`Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank ${l.rank}.`),c!=null&&P(c.rank===2||c.rank===1,()=>`Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank ${c.rank}.`),ho(o,i,u,c,l,a)}var sS=W({batchNorm2d_:FP});function DP(e,t,n,r,s,a){let o=$(e,"x","batchNorm"),i=$(t,"mean","batchNorm"),u=$(n,"variance","batchNorm"),l;s!=null&&(l=$(s,"scale","batchNorm"));let c;return r!=null&&(c=$(r,"offset","batchNorm")),P(o.rank===3,()=>`Error in batchNorm3D: x must be rank 3 but got rank ${o.rank}.`),P(i.rank===3||i.rank===1,()=>`Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank ${i.rank}.`),P(u.rank===3||u.rank===1,()=>`Error in batchNorm3D: variance must be rank 3 or rank 1 but got rank ${u.rank}.`),l!=null&&P(l.rank===3||l.rank===1,()=>`Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank ${l.rank}.`),c!=null&&P(c.rank===3||c.rank===1,()=>`Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank ${c.rank}.`),ho(o,i,u,c,l,a)}var aS=W({batchNorm3d_:DP});function RP(e,t,n,r,s,a){let o=$(e,"x","batchNorm"),i=$(t,"mean","batchNorm"),u=$(n,"variance","batchNorm"),l;s!=null&&(l=$(s,"scale","batchNorm"))
2021-09-11 17:11:38 +02:00
${s} and ${t} for depthToSpace with input shape
2021-12-01 21:37:52 +01:00
${r.shape}`),P(a*t>=0,()=>`Negative dimension size caused by overflow when multiplying
2021-09-11 17:11:38 +02:00
${a} and ${t} for depthToSpace with input shape
2021-12-01 21:37:52 +01:00
${r.shape}`),P(o%(t*t)==0,()=>`Dimension size must be evenly divisible by ${t*t} but is ${o} for depthToSpace with input shape ${r.shape}`);let i={x:r},u={blockSize:t,dataFormat:n};return z.runKernel(qo,i,u)}var gS=W({depthToSpace_:tO});function nO(e,t,n,r,s="NHWC",a=[1,1],o){let i=$(e,"x","depthwiseConv2d","float32"),u=$(t,"filter","depthwiseConv2d","float32"),l=i,c=!1;i.rank===3&&(c=!0,l=G(i,[1,i.shape[0],i.shape[1],i.shape[2]])),P(l.rank===4,()=>`Error in depthwiseConv2d: input must be rank 4, but got rank ${l.rank}.`),P(u.rank===4,()=>`Error in depthwiseConv2d: filter must be rank 4, but got rank ${u.rank}.`),P(l.shape[3]===u.shape[2],()=>`Error in depthwiseConv2d: number of input channels (${l.shape[3]}) must match the inChannels dimension in filter ${u.shape[2]}.`),Sn("depthwiseConv2d",r,o);let d={x:l,filter:u},p={strides:n,pad:r,dataFormat:s,dilations:a,dimRoundingMode:o},h=z.runKernel(xa,d,p);return c?G(h,[h.shape[1],h.shape[2],h.shape[3]]):h}var Bi=W({depthwiseConv2d_:nO});function rO(e){let n={x:$(e,"x","diag")};return z.runKernel(Ch,n)}var sO=W({diag_:rO});function aO(e,t,n,r,s=[1,1],a="NHWC"){let o=$(e,"x","dilation2d"),i=$(t,"filter","dilation2d");P(o.rank===3||o.rank===4,()=>`Error in dilation2d: input must be rank 3 or 4, but got rank ${o.rank}.`),P(i.rank===3,()=>`Error in dilation2d: filter must be rank 3, but got rank ${i.rank}.`),P(a==="NHWC",()=>`Error in dilation2d: Only NHWC is currently supported, but got dataFormat of ${a}`);let u=o,l=!1;o.rank===3&&(u=G(o,[1,o.shape[0],o.shape[1],o.shape[2]]),l=!0);let c={x:u,filter:i},d={strides:n,pad:r,dilations:s},p=z.runKernel(Ql,c,d);return l?G(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var bS=W({dilation2d_:aO});function oO(e,t){let n=$(e,"a","equal","string_or_numeric"),r=$(t,"b","equal","string_or_numeric");[n,r]=At(n,r),ht(n.shape,r.shape);let s={a:n,b:r};return z.runKernel(Ko,s)}var mr=W({equal_:oO});function iO(e,t,n){let r=$(t,"a","where"),s=$(n,"b","where"),a=$(e,"condition","where","bool"),o=ht(ht(a.shape,r.shape),s.shape),i=Sd(a,o),u=Sd(r,o),l=Sd(s,o),c={condition:i,t:u,e:l};return z.runKernel(bi,c)}var Cn=W({where_:iO});function uO(e){let n={x:$(e,"x","zerosLike")};return z.runKernel(Ni,n)}var Xe=W({zerosLike_:uO});function cO(e,t){let n=$(e,"a","div"),r=$(t,"b","div");[n,r]=At(n,r);let s=me(n,r),a=Xe(s),o=mr(r,a);return Cn(o,a,s)}var yS=W({divNoNan_:cO});function lO(e,t){let n=$(e,"t1","dot"),r=$(t,"t2","dot");P((n.rank===1||n.rank===2)&&(r.rank===1||r.rank===2),()=>`Error in dot: inputs must all be rank 1 or 2, but got ranks ${n.rank} and ${r.rank}.`);let s=n.rank===1?n.size:n.shape[1],a=r.rank===1?r.size:r.shape[0];if(P(s===a,()=>`Error in dot: inner dimensions of inputs must match, but got ${s} and ${a}.`),n.rank===1&&r.rank===1){let o=G(n,[1,-1]),i=G(r,[-1,1]),u=Re(o,i);return G(u,[])}else if(n.rank===1&&r.rank===2){let o=G(n,[1,-1]),i=G(r,[r.shape[0],r.shape[1]]),u=Re(o,i);return G(u,[u.size])}else if(n.rank===2&&r.rank===1){let o=G(r,[-1,1]),i=Re(n,o);return G(i,[i.size])}else{let o=G(r,[r.shape[0],r.shape[1]]);return Re(n,o)}}var dO=W({dot_:lO});function pO(e,...t){let n=t.map((s,a)=>$(s,`tensors${a}`,"einsum")),r={equation:e};return z.runKernel(Zl,n,r)}var vS=W({einsum_:pO});function hO(e){let n={x:$(e,"x","elu","float32")};return z.runKernel(ka,n)}var Cd=W({elu_:hO});function fO(e){let t=$(e,"x","erf");P(t.dtype==="int32"||t.dtype==="float32",()=>"Input dtype must be `int32` or `float32`."),t.dtype==="int32"&&(t=ue(t,"float32"));let n={x:t};return z.runKernel(Zu,n)}var xS=W({erf_:fO});function mO(e){let n={x:$(e,"x","exp")};return z.runKernel(Ia,n)}var Tn=W({exp_:mO});function gO(e,t=0){let n=$(e,"x","expandDims","string_or_numeric");P(t<=n.rank,()=>"Axis must be <= rank of the tensor");let r={input:n},s={dim:t};return z.runKernel(Xo,r,s)}var Nn=W({expandDims_:gO});function bO(e){let n={x:$(e,"x","expm1")};return z.runKernel(Yo,n)}var wS=W({expm1_:bO});function yO(e,t){let n=$(e,"x","tile","string_or_numeric");P(n.rank===t.length,()=>`Error in transpose: rank of input ${n.rank} must match length of reps ${t}.`);let r={x:n},s={reps:
rank ${a.rank}.`),P(Lu(t),()=>`Error in localResponseNormalization: depthRadius must be an integer but got depthRadius ${t}.`);let o=a,i=!1;a.rank===3&&(i=!0,o=G(a,[1,a.shape[0],a.shape[1],a.shape[2]]));let u={x:o},l={depthRadius:t,bias:n,alpha:r,beta:s},c=z.runKernel(td,u,l);return i?G(c,[c.shape[1],c.shape[2],c.shape[3]]):c}var SS=W({localResponseNormalization_:DO});function RO(e){let n={x:$(e,"x","log","float32")};return z.runKernel(Ea,n)}var gr=W({log_:RO});function PO(e){let n={x:$(e,"x","log1p")};return z.runKernel(rc,n)}var af=W({log1p_:PO});function OO(e){return P(ca(e),()=>"The f passed in grad(f) must be a function"),(t,n)=>{let r=$(t,"x","tf.grad","string_or_numeric"),s=n!=null?$(n,"dy","tf.grad"):null;return z.tidy(()=>{let{value:a,grads:o}=z.gradients(()=>e(r),[r],s);return s!=null&&wn(a.shape,s.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),of(o),o[0]})}}function MO(e){return P(ca(e),()=>"The f passed in grads(f) must be a function"),(t,n)=>{P(Array.isArray(t),()=>"The args passed in grads(f)(args) must be an array of `Tensor`s or `TensorLike`s");let r=vd(t,"args","tf.grads","string_or_numeric"),s=n!=null?$(n,"dy","tf.grads"):null;return z.tidy(()=>{let{value:a,grads:o}=z.gradients(()=>e(...r),r,s);return s!=null&&wn(a.shape,s.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),of(o),o})}}function LO(e){return P(ca(e),()=>"The f passed in valueAndGrad(f) must be a function"),(t,n)=>{P(t instanceof Ae,()=>"The x passed in valueAndGrad(f)(x) must be a tensor"),P(n==null||n instanceof Ae,()=>"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");let{grads:r,value:s}=z.gradients(()=>e(t),[t],n);return of(r),{grad:r[0],value:s}}}function BO(e){return P(ca(e),()=>"The f passed in valueAndGrads(f) must be a function"),(t,n)=>{P(Array.isArray(t)&&t.every(s=>s instanceof Ae),()=>"The args passed in valueAndGrads(f)(args) must be array of tensors"),P(n==null||n instanceof Ae,()=>"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");let r=z.gradients(()=>e(...t),t,n);return n!=null&&wn(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),of(r.grads),r}}function CS(e,t){P(ca(e),()=>"The f passed in variableGrads(f) must be a function"),P(t==null||Array.isArray(t)&&t.every(l=>l instanceof oo),()=>"The varList passed in variableGrads(f, varList) must be an array of variables");let n=t!=null;if(!n){t=[];for(let l in z.registeredVariables)t.push(z.registeredVariables[l])}let r=n?t.filter(l=>!l.trainable):null,s=t.length;t=t.filter(l=>l.trainable),P(t.length>0,()=>`variableGrads() expects at least one of the input variables to be trainable, but none of the ${s} variables is trainable.`);let a=!0,{value:o,grads:i}=z.gradients(e,t,null,a);P(i.some(l=>l!=null),()=>"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),P(o.rank===0,()=>`The f passed in variableGrads(f) must return a scalar, but it returned a rank-${o.rank} tensor`);let u={};return t.forEach((l,c)=>{i[c]!=null&&(u[l.name]=i[c])}),r!=null&&r.forEach(l=>u[l.name]=null),{value:o,grads:u}}function cs(e){return z.customGrad(e)}function of(e){if(e.filter(n=>n==null).length>0)throw new Error(`Cannot compute gradient of y=f(x) with respect to x. Make sure that
the f you passed encloses all operations that lead from x to y.`)}function zO(e){let n={x:$(e,"x","neg")};return z.runKernel(ai,n)}var Ft=W({neg_:zO});function WO(e){let n={x:$(e,"x","softplus")};return z.runKernel(pc,n)}var Cc=W({softplus_:WO});function VO(e){let t=$(e,"x","logSigmoid");return cs(r=>({value:Ft(Cc(Ft(r))),gradFunc:o=>V(o,$r(Ft(r)))}))(t)}var UO=W({logSigmoid_:VO});function GO(e,t=null,n=!1){let s={x:$(e,"x","max")},a={reductionIndices:t,keepDims:n};return z.runKernel(Aa,s,a)}var Gr=W({max_:GO});function HO(e,t){let n=$(e,"a","sub"),r=$(t,"b","sub");[n,r]=At(n,r);let s={a:n,b:r};return z.runKernel(Qa,s)}var he=W({sub_:HO});function jO(e,t=null,n=!1){let r=$(e,"x","sum");r.dtype==="bool"&&(r=ue(r,"int32"));let s={x:r},a={axis:t,keepDims:n};return z.runKernel(Ka,s,a)}var ve=W({sum_:jO});function qO(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 cs((s,a)=>{let o=!0,i=Gr(s,t,!0),u=he(s,i),l=he(ue(u,"float32"),gr(ve(Tn(u),t,o)));return a([l]),{value:l,gradFunc:(d,p)=>{let[h]=p,f=!0,m=Tn(h);return he(d,V(ve(d,t,f),m))}}})(n)}var Zy=W({logSoftmax_:qO});function Jy(e,t){for(let n=0;n<e.length;++n)if(e[e.length-n-1]!==t-1-n)return!1;return!0}function TS(e,t,n){let r=e.length+t.length,s=[],a=0,o=0;for(let i=0;i<r;i++)n.indexOf(i)===-1?s.push(e[a++]):s.push(t[o++]);return s}function NS(e,t){let n=[],r=e.length;for(let a=0;a<r;a++)t.indexOf(a)===-1&&n.push(e[a]);let s=t.map(a=>e[a]);return[n,s]}function Vi(e,t){let n=t.map(r=>1);return TS(e,n,t)}function KO(e,t,n){P(Jy(t,n),()=>`${e} supports only inner-most axes for now. Got axes ${t} and rank-${n} input.`)}function _S(e,t){if(Jy(e,t))return null;let n=[];for(let r=0;r<t;++r)e.indexOf(r)===-1&&n.push(r);return e.forEach(r=>n.push(r)),n}function ev(e){return e.map((t,n)=>[n,t]).sort((t,n)=>t[1]-n[1]).map(t=>t[0])}function XO(e,t){let n=[];for(let r=t-e;r<t;++r)n.push(r);return n}function YO(e,t=null,n=!1){let r=$(e,"x","logSumExp"),s=Er(t,r.shape),a=Gr(r,s,!0),o=he(r,a),i=Tn(o),u=ve(i,s),l=gr(u),c=Z(G(a,l.shape),l);if(n){let d=Vi(c.shape,s);return G(c,d)}return c}var ES=W({logSumExp_:YO});function QO(e,t){let n=$(e,"a","logicalAnd","bool"),r=$(t,"b","logicalAnd","bool");ht(n.shape,r.shape);let s={a:n,b:r};return z.runKernel(si,s)}var Hr=W({logicalAnd_:QO});function ZO(e){let n={x:$(e,"x","logicalNot","bool")};return z.runKernel(sc,n)}var uf=W({logicalNot_:ZO});function JO(e,t){let n=$(e,"a","logicalOr","bool"),r=$(t,"b","logicalOr","bool");ht(n.shape,r.shape);let s={a:n,b:r};return z.runKernel(ed,s)}var tv=W({logicalOr_:JO});function eM(e,t){let n=$(e,"a","logicalXor","bool"),r=$(t,"b","logicalXor","bool");return ht(n.shape,r.shape),Hr(tv(e,t),uf(Hr(e,t)))}var tM=W({logicalXor_:eM});function nM(e,t,n,r,s){let a=$(e,"x","maxPool"),o=1,i=a,u=!1;a.rank===3&&(u=!0,i=G(a,[1,a.shape[0],a.shape[1],a.shape[2]])),P(i.rank===4,()=>`Error in maxPool: input must be rank 4 but got rank ${i.rank}.`),P(us(n,o),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${n} and dilations '${o}'`),Sn("maxPool",r,s);let l={x:i},c={filterSize:t,strides:n,pad:r,dimRoundingMode:s},d=z.runKernel(Fa,l,c);return u?G(d,[d.shape[1],d.shape[2],d.shape[3]]):d}var Vt=W({maxPool_:nM});function rM(e,t=[1,1,1],n,r,s,a="NDHWC"){let o=$(e,"x","maxPool3d"),i=o,u=!1;o.rank===4&&(u=!0,i=G(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]])),P(i.rank===5,()=>`Error in maxPool3d: x must be rank 5 but got rank ${i.rank}.`),P(a==="NDHWC",()=>`Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of ${a}`),Sn("maxPool3d",r,s);let l={x:i},c={filterSize:t,strides:n,pad:r,dimRoundingMode:s,dataFormat:a},d=z.runKernel(nd,l,c);return u?G(d,[d.shape[1],d.shape[2],d.shape[3],d.shape[4]]):d}var nv=W({maxPool3d_:rM});function sM(e,t,n,r,s=!1){let o={x:$(e,"x","maxPoolWithArgmax")},i={filterSize:t,strides:n,pad:r,includeBatchInIndex:s},u=z.runKernel(Ph,o,i);return{result:u[0],indexes:u[1]}}var AS=W({maxPoolWithArgmax_:sM});f
${s.shape}`);if(a.rank!==1)throw new Error(`Values should be Tensor1D but received shape ${a.shape}`);if(o.rank!==1)throw new Error(`Dense shape should be Tensor1D but received shape ${o.shape}`);if(i.rank!==0)throw new Error(`Default value should be a scalar but received shape ${i.shape}`);let u={indices:s,values:a,denseShape:o,defaultValue:i},l=z.runKernel(sd,u);return{outputIndices:l[0],outputValues:l[1],emptyRowIndicator:l[2],reverseIndexMap:l[3]}}var vz=W({sparseFillEmptyRows_:yz});function xz(e,t,n){let r=$(e,"inputIndices","sparseReshape","int32"),s=$(t,"inputShape","sparseReshape","int32"),a=$(n,"newShape","sparseReshape","int32");if(r.rank!==2)throw new Error(`Input indices should be Tensor2D but received shape
${r.shape}`);if(s.rank!==1)throw new Error(`Input shape should be Tensor1D but received shape ${s.shape}`);if(a.rank!==1)throw new Error(`New shape should be Tensor1D but received shape ${a.shape}`);let o={inputIndices:r,inputShape:s,newShape:a},i=z.runKernel(hc,o);return{outputIndices:i[0],outputShape:i[1]}}var wz=W({sparseReshape_:xz});function kz(e,t,n){let r=$(e,"data","sparseSegmentMean"),s=$(t,"indices","sparseSegmentMean","int32"),a=$(n,"segmentIds","sparseSegmentMean","int32");if(r.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.rank!==1)throw new Error(`Indices should be Tensor1D but received shape
2021-09-11 17:11:38 +02:00
${s.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
2021-12-01 21:37:52 +01:00
${a.shape}`);let o={data:r,indices:s,segmentIds:a};return z.runKernel(ad,o)}var Iz=W({sparseSegmentMean_:kz});function Sz(e,t,n){let r=$(e,"data","sparseSegmentSum"),s=$(t,"indices","sparseSegmentSum","int32"),a=$(n,"segmentIds","sparseSegmentSum","int32");if(r.rank<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.rank!==1)throw new Error(`Indices should be Tensor1D but received shape
2021-09-11 17:11:38 +02:00
${s.shape}`);if(a.rank!==1)throw new Error(`Segment ids should be Tensor1D but received shape
2021-12-01 21:37:52 +01:00
${a.shape}`);let o={data:r,indices:s,segmentIds:a};return z.runKernel(od,o)}var Cz=W({sparseSegmentSum_:Sz});function Tz(e,t,n,r,s,a,o,i){let u=$(e,"data","stringNGrams","string");if(u.dtype!=="string")throw new Error("Data must be of datatype string");if(u.shape.length!==1)throw new Error(`Data must be a vector, saw: ${u.shape}`);let l=$(t,"dataSplits","stringNGrams");if(l.dtype!=="int32")throw new Error("Data splits must be of datatype int32");let c={separator:n,nGramWidths:r,leftPad:s,rightPad:a,padWidth:o,preserveShortSequences:i},d={data:u,dataSplits:l},p=z.runKernel(ud,d,c);return{nGrams:p[0],nGramsSplits:p[1]}}var Nz=W({stringNGrams_:Tz});function _z(e,t,n=!0){let r=$(e,"input","stringSplit","string"),s=$(t,"delimiter","stringSplit","string");if(r.rank!==1)throw new Error(`Input should be Tensor1D but received shape ${r.shape}`);if(s.rank!==0)throw new Error(`Delimiter should be a scalar but received shape ${s.shape}`);let a={skipEmpty:n},o={input:r,delimiter:s},i=z.runKernel(Bh,o,a);return{indices:i[0],values:i[1],shape:i[2]}}var Ez=W({stringSplit_:_z});function Az(e,t){let n=$(e,"input","stringToHashBucketFast","string"),r={numBuckets:t};if(t<=0)throw new Error("Number of buckets must be at least 1");let s={input:n};return z.runKernel(zh,s,r)}var $z=W({stringToHashBucketFast_:Az}),Fz={fft:ff,ifft:Ad,rfft:mf,irfft:hv},Dz={hammingWindow:iB,hannWindow:QS,frame:ZS,stft:dB},ar={flipLeftRight:mB,grayscaleToRGB:bB,resizeNearestNeighbor:WB,resizeBilinear:BB,rotateWithOffset:vB,cropAndResize:hB,nonMaxSuppression:wB,nonMaxSuppressionAsync:EB,nonMaxSuppressionWithScore:$B,nonMaxSuppressionWithScoreAsync:DB,nonMaxSuppressionPadded:PB,nonMaxSuppressionPaddedAsync:MB,threshold:GB,transform:jB},s0={bandPart:KB,gramSchmidt:YB,qr:ZB},Rz={absoluteDifference:tz,computeWeightedLoss:Ps,cosineDistance:rz,hingeLoss:az,huberLoss:iz,logLoss:cz,meanSquaredError:dz,sigmoidCrossEntropy:fz,softmaxCrossEntropy:bz},Fd={sparseFillEmptyRows:vz,sparseReshape:wz,sparseSegmentMean:Iz,sparseSegmentSum:Cz},wf={stringNGrams:Nz,stringSplit:Ez,stringToHashBucketFast:$z},Os=class extends W1{minimize(e,t=!1,n){let{value:r,grads:s}=this.computeGradients(e,n);if(n!=null){let a=n.map(o=>({name:o.name,tensor:s[o.name]}));this.applyGradients(a)}else this.applyGradients(s);return Fe(s),t?r:(r.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(e,t){return CS(e,t)}dispose(){this.iterations_!=null&&Fe(this.iterations_)}async saveIterations(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:ke(this.iterations_,"int32")}}async getWeights(){throw new Error("getWeights() is not implemented for this optimizer yet.")}async setWeights(e){throw new Error(`setWeights() is not implemented for this optimizer class ${this.getClassName()}`)}async extractIterations(e){return this.iterations_=(await e[0].tensor.data())[0],e.slice(1)}};Object.defineProperty(Os,Symbol.hasInstance,{value:e=>e.minimize!=null&&e.computeGradients!=null&&e.applyGradients!=null});var kf=class extends Os{constructor(e,t,n=null){super();this.learningRate=e,this.rho=t,this.epsilon=n,this.accumulatedGrads=[],this.accumulatedUpdates=[],n==null&&(this.epsilon=z.backend.epsilon())}applyGradients(e){(Array.isArray(e)?e.map(n=>n.name):Object.keys(e)).forEach((n,r)=>{let s=z.registeredVariables[n],a=!1;this.accumulatedGrads[r]==null&&(this.accumulatedGrads[r]={originalName:`${n}/accum_grad`,variable:M(()=>Xe(s).variable(a))}),this.accumulatedUpdates[r]==null&&(this.accumulatedUpdates[r]={originalName:`${n}/accum_var`,variable:M(()=>Xe(s).variable(a))});let o=Array.isArray(e)?e[r].tensor:e[n];if(o==null)return;let i=this.accumulatedGrads[r].variable,u=this.accumulatedUpdates[r].variable;M(()=>{let l=Z(V(i,this.rho),V(ft(o),1-this.rho)),c=V(me(vn(Z(u,this.epsilon)),vn(Z(i,this.epsilon))),o),d=Z(V(u,this.rho),V(ft(c),1-this.rho));i.assign(l),u.assign(d);let p=Z(V(c,-this.learningRate),s);s.assign(p)})}),this.incrementIterations()}dispose(){this.accumulatedUpda
indices.shape[0] = ${e}`}function fW(e,t){return`indices(${e}, 0) is invalid: ${t} < 0`}function mW(e,t,n){return`indices(${e}, 0) is invalid: ${t} >= ${n}`}function gW(e,t){return`only one output dimension may be -1, not both ${e} and ${t}`}function bW(e,t){return`size ${e} must be non-negative, not ${t}`}function yW(){return"reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero"}function vW(e,t){let n=wt(e),r=wt(t);return`Input to reshape is a SparseTensor with ${n}
dense values, but the requested shape requires a multiple of ${r}. inputShape=${e} outputShape= ${t}`}function xW(e,t){let n=wt(e),r=wt(t);return`Input to reshape is a tensor with ${n} dense values, but the requested shape has ${r}. inputShape=${e} outputShape=${t}`}function wW(){return"segment ids must be >= 0"}function kW(){return"segment ids are not increasing"}function IW(e,t){return`Segment id ${e} out of range [0, ${t}), possibly because segmentIds input is not sorted.`}function SW(e,t,n){return`Bad: indices[${e}] == ${t} out of range [0, ${n})`}var l0={};Ee(l0,{collectGatherOpShapeInfo:()=>NW,computeOutShape:()=>TW,segOpComputeOptimalWindowSize:()=>CW});function CW(e,t){let n=!1,r;for(e<=kv?(r=e,n=!0):r=hh(e,Math.floor(Math.sqrt(e)));!n;)r>t||r===e?n=!0:r=hh(e,r+1);return r}function TW(e,t,n){let r=[],s=e.length;for(let a=0;a<s;a++)a!==t?r.push(e[a]):r.push(n);return r}function NW(e,t,n,r){let s=t.shape.length,a=e.shape.length;if(r!==0&&(r<-s||r>s))throw new Error(`Expect batchDims in the range of [-${s}, ${s}], but got ${r}`);if(r<0&&(r+=s),r>a)throw new Error(`batchDims (${r}) must be less than rank(x) (
${a}).`);if(n<r)throw new Error(`batchDims (${r}) must be less than or equal to axis (${n}).`);for(let d=0;d<r;++d)if(e.shape[d]!==t.shape[d])throw new Error(`x.shape[${d}]: ${e.shape[d]} should be equal to indices.shape[${d}]: ${t.shape[d]}.`);let o=e.shape[n],i=[],u=1,l=1,c=1;for(let d=0;d<r;++d)i.push(e.shape[d]),u*=e.shape[d];for(let d=r;d<n;d++)i.push(e.shape[d]),l*=e.shape[d];for(let d=r;d<s;d++)i.push(t.shape[d]);for(let d=n+1;d<a;d++)i.push(e.shape[d]),c*=e.shape[d];return{batchSize:u,sliceSize:c,outerSize:l,dimSize:o,outputShape:i}}function _W(e){try{return e.map(t=>jh(t))}catch(t){throw new Error(`Failed to decode encoded string bytes into utf-8, error: ${t}`)}}function EW(e){return e.map(t=>fd(t))}var Dr={};Ee(Dr,{nonMaxSuppressionV3Impl:()=>JS,nonMaxSuppressionV4Impl:()=>e0,nonMaxSuppressionV5Impl:()=>t0,whereImpl:()=>US});var d0={kernelName:Vo,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>V(e,$d(ue(n,"float32"),-1))}}},AW={kernelName:Vu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let r=ft(ue(n,"float32")),s=vn(he(ke(1),r));return Ft(me(e,s))}}}},$W={kernelName:Uu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let r=vn(he(ft(ue(n,"float32")),1));return me(e,r)}}}},FW={kernelName:_s,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,r]=t,s=ht(n.shape,r.shape);return{a:()=>{let i=e,u=zt(n.shape,s);return u.length>0&&(i=ve(i,u)),G(i,n.shape)},b:()=>{let i=e,u=zt(r.shape,s);return u.length>0&&(i=ve(i,u)),G(i,r.shape)}}}},DW={kernelName:la,saveAllInputs:!0,gradFunc:(e,t)=>{let n={};return t.forEach((r,s)=>{n[s]=()=>e.clone()}),n}},RW={kernelName:da,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>Xe(n)}}},PW={kernelName:ju,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>Xe(n)}}},OW={kernelName:qu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>me(e,vn(he(ke(1),ft(ue(n,"float32")))))}}},MW={kernelName:Ku,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>{let r=vn(Z(ke(1),ft(ue(n,"float32"))));return me(e,r)}}}},LW={kernelName:Qu,inputsToSave:["a","b"],gradFunc:(e,t)=>{let[n,r]=t,s=ht(n.shape,r.shape);return{a:()=>{let i=Z(ft(n),ft(r)),u=V(e,me(r,i)),l=zt(n.shape,s);return l.length>0&&(u=ve(u,l)),G(u,n.shape)},b:()=>{let i=Z(ft(n),ft(r)),u=Ft(V(e,me(n,i))),l=zt(r.shape,s);return l.length>0&&(u=ve(u,l)),G(u,r.shape)}}}},BW={kernelName:Xu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>me(e,Z(ft(ue(n,"float32")),1))}}},zW={kernelName:Yu,inputsToSave:["x"],gradFunc:(e,t)=>{let[n]=t;return{x:()=>me(e,he(ke(1),ft(ue(n,"float32"))))}}};function WW(e,t,n,r,s,a){let o=$(e,"dy","avgPool3dGrad"),i=$(t,"input","avgPool3dGrad"),u=o,l=i,c=!1;i.rank===4&&(c=!0,u=G(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]]),l=G(i,[1,i.shape[0],i.shape[1],i.shape[2],i.shape[3]])),P(u.rank===5,()=>`Error in avgPool3dGrad: dy must be rank 5 but got rank ${u.rank}.`),P(l.rank===5,()=>`Error in avgPool3dGrad: input must be rank 5 but got rank ${l.rank}.`),Sn("avgPool3dGrad",s,a);let d={dy:u,input:l},p={filterSize:n,strides:r,pad:s,dimRoundingMode:a},h=z.runKernel(gh,d,p);return c?G(h,[h.shape[1],h.shape[2],h.shape[3],h.shape[4]]):h}var VW=W({avgPool3dGrad_:WW}),UW={kernelName:ql,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[r]=t,{filterSize:s,strides:a,pad:o,dimRoundingMode:i}=n;return{x:()=>VW(e,r,s,a,o,i)}}};function GW(e,t,n,r,s){let a=$(e,"dy","avgPoolGrad"),o=$(t,"input","avgPoolGrad");P(o.rank===a.rank,()=>`Rank of input (${o.rank}) does not match rank of dy (${a.rank})`);let i=o,u=a,l=!1;o.rank===3&&(l=!0,i=G(o,[1,o.shape[0],o.shape[1],o.shape[2]]),u=G(a,[1,a.shape[0],a.shape[1],a.shape[2]])),P(u.rank===4,()=>`Error in avgPoolGrad: dy must be rank 4 but got rank ${u.rank}.`),P(i.rank===4,()=>`Error in avgPoolGrad: input must be rank 4 but got rank ${i.rank}.`);let c={dy:u,input:i},d={filterSize:n,strides:r,pad:s},p=z.runKernel(mh,c,d);return l?G(p,[p.shape[1],p.shape[2],p.shape[3]]):p}var HW=W({avgPoolGrad_:GW}),jW={kernelName:pa,inputsToSave:["x"],gradFunc:(e,t,n)=>{let[r]=t,{filterSize:s,strides:a,pad:o}=n;return{x:()=>HW(e,r,s,a,o)}}},qW={kernelName:ha,i
2021-09-11 17:11:38 +02:00
1. The ${r} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom ${r} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);return o}else{let a=e;if(a.className==null||a.config==null)throw new H(`${r}: Improper config format: ${JSON.stringify(a)}.
2021-12-01 21:37:52 +01:00
'className' and 'config' must set.`);let o=a.className,i,u;if(o in n?[i,u]=n[o]:o in Rr?[i,u]=Rr.className:o in t&&([i,u]=t[o]),i==null)throw new H(`Unknown ${r}: ${o}. This may be due to one of the following reasons:
2021-09-11 17:11:38 +02:00
1. The ${r} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2021-12-01 21:37:52 +01:00
2. The custom ${r} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);if(u!=null){let l={};for(let h of Object.keys(Rr))l[h]=Rr[h];for(let h of Object.keys(n))l[h]=n[h];let c=a.config;c.customObjects=l;let d={...Rr};for(let h of Object.keys(n))Rr[h]=n[h];Tv(a.config);let p=u(i,a.config,n,s);return Rr={...d},p}else{let l={...Rr};for(let d of Object.keys(n))Rr[d]=n[d];let c=new i(a.config);return Rr={...l},c}}}function $V(e,t){return e<t?-1:e>t?1:0}function _f(e,t){return-1*$V(e,t)}function bo(e){if(e==null)return e;let t=[];for(let n of e)t.indexOf(n)===-1&&t.push(n);return t}function FV(e){if(e==null)throw new H(`Invalid value in obj: ${JSON.stringify(e)}`);for(let t in e)if(e.hasOwnProperty(t))return!1;return!0}function ji(e,t,n){if(n!=null&&e.indexOf(n)<0)throw new H(`${n} is not a valid ${t}. Valid values are ${e} or null/undefined.`)}function Nv(e,t,n=0,r=1/0){return ds(n>=0),ds(r>=n),Array.isArray(e)&&e.length>=n&&e.length<=r&&e.every(s=>typeof s===t)}function rn(e,t){Array.isArray(e)?(w.assert(e.length>0,()=>`${t} is unexpectedly an empty array.`),e.forEach((n,r)=>rn(n,`element ${r+1} of ${t}`))):w.assert(Number.isInteger(e)&&e>0,()=>`Expected ${t} to be a positive integer, but got ${k0(e)}.`)}function k0(e){return e===null?"null":Array.isArray(e)?"["+e.map(t=>k0(t)).join(",")+"]":typeof e=="string"?`"${e}"`:`${e}`}function DV(e,t,n){let r=n!=null?n():w.now(),s;return(...o)=>{let i=n!=null?n():w.now();return i-r<t||(r=i,s=e(...o)),s}}function I0(e){return e==="relu"?"relu":e==="linear"?"linear":e==="elu"?"elu":null}function _v(e,t){return M(()=>vn(ve(V(e,e),t,!0)))}var Pd=class extends oe.Serializable{getConfig(){return{}}},Ev=class extends Pd{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=_v(e,this.axis),n=dn(t,0,this.maxValue);return V(e,me(n,Z(Yt(),t)))})}getConfig(){return{maxValue:this.maxValue,axis:this.axis}}};Ev.className="MaxNorm";oe.registerClass(Ev);var Av=class extends Pd{constructor(e){super();this.defaultAxis=0,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return M(()=>me(e,Z(Yt(),_v(e,this.axis))))}getConfig(){return{axis:this.axis}}};Av.className="UnitNorm";oe.registerClass(Av);var $v=class extends Pd{apply(e){return Qe(e)}};$v.className="NonNeg";oe.registerClass($v);var Fv=class extends Pd{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=_v(e,this.axis),n=Z(V(this.rate,dn(t,this.minValue,this.maxValue)),V(1-this.rate,t));return V(e,me(n,Z(Yt(),t)))})}getConfig(){return{minValue:this.minValue,maxValue:this.maxValue,rate:this.rate,axis:this.axis}}};Fv.className="MinMaxNorm";oe.registerClass(Fv);var S0={maxNorm:"MaxNorm",minMaxNorm:"MinMaxNorm",nonNeg:"NonNeg",unitNorm:"UnitNorm"};function Qt(e){return Cv(e)}function C0(e,t={}){return Rd(e,oe.SerializationMap.getMap().classNameMap,t,"constraint")}function Zt(e){if(e==null)return null;if(typeof e=="string"){let n={className:e in S0?S0[e]:e,config:{}};return C0(n)}else return e instanceof Pd?e:C0(e)}function RV(e){return new Ev(e)}function PV(e){return new Av(e)}function OV(){return new $v}function MV(e){return new Fv(e)}var T0={};Ee(T0,{constant:()=>oU,glorotNormal:()=>hU,glorotUniform:()=>pU,heNormal:()=>fU,heUniform:()=>mU,identity:()=>lU,leCunNormal:()=>gU,leCunUniform:()=>bU,ones:()=>aU,orthogonal:()=>yU,randomNormal:()=>uU,randomUniform:()=>iU,truncatedNormal:()=>cU,varianceScaling:()=>dU,zeros:()=>sU});var LV=["channelsFirst","channelsLast"],BV=["nearest","bilinear"],zV=["valid","same","causal"],WV=["max","avg"],VV=["sum","mul","concat","ave"],Fc=new Map;function Bt(e){ji(LV,"DataFormat",e)}function UV(e){ji(BV,"InterpolationFormat",
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),Mr(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,nn(t),n.written=!0,this.tensors[e]=n}writeMany(e,t){if(e.length!==t.length)throw new Error(`TensorArray ${this.name}: could not write multiple tensors,because the index size: ${e.length} is not the same as tensors size: ${t.length}.`);e.forEach((n,r)=>this.write(n,t[r]))}gather(e,t){if(!!t&&t!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but gather requested dtype ${t}`);if(e)e=e.slice(0,this.size());else{e=[];for(let r=0;r<this.size();r++)e.push(r)}if(e.length===0)return er([],[0].concat(this.elementShape));let n=this.readMany(e);return Mr(this.elementShape,n[0].shape,"TensorArray shape mismatch: "),Ut(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 er([],[0].concat(this.elementShape));let t=[];for(let r=0;r<this.size();r++)t.push(r);let n=this.readMany(t);return Mr(this.elementShape,n[0].shape,`TensorArray shape mismatch: tensor array shape (${this.elementShape}) vs first tensor shape (${n[0].shape})`),ot(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,vt(t,0))}split(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);let n=0,r=e.map(i=>(n+=i,n));if(n!==t.shape[0])throw new Error(`Expected sum of lengths to be equal to
2021-09-11 17:11:38 +02:00
tensor.shape[0], but sum of lengths is
2021-12-01 21:37:52 +01:00
${n}, and tensor's shape is: ${t.shape}`);if(!this.dynamicSize&&e.length!==this.maxSize)throw new Error(`TensorArray's size is not equal to the size of lengths (${this.maxSize} vs. ${e.length}), and the TensorArray is not marked as dynamically resizeable`);let s=n===0?0:t.size/n,a=[];M(()=>{t=G(t,[1,n,s]);for(let i=0;i<e.length;++i){let u=i===0?0:r[i-1],l=[0,u,0],c=[1,e[i],s];a[i]=G(Ve(t,l,c),this.elementShape)}return a});let o=[];for(let i=0;i<e.length;i++)o[i]=i;this.writeMany(o,a)}},Qd=class{constructor(e,t,n,r=-1){this.tensors=e,this.elementShape=t,this.elementDtype=n,e!=null&&e.forEach(s=>{if(n!==s.dtype)throw new Error(`Invalid data types; op elements ${n}, but list elements ${s.dtype}`);Mr(t,s.shape,"TensorList shape mismatch: "),nn(s)}),this.idTensor=ke(0),this.maxNumElements=r,nn(this.idTensor)}get id(){return this.idTensor.id}copy(){return new Qd([...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.`);Mr(e,this.elementShape,"TensorList shape mismatch: ");let r=Yd(this.elementShape,this.tensors,e);return M(()=>{let s=this.tensors.map(a=>G(a,r));return Ut(s,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=Yd(this.elementShape,this.tensors,e),r=this.tensors.pop();return Mr(r.shape,e,"TensorList shape mismatch: "),G(r,n)}pushBack(e){if(e.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${e.dtype}, but list elements ${this.elementDtype}`);if(Mr(e.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");nn(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.`);Mr(this.tensors[e].shape,t,"TensorList shape mismatch: ");let r=Yd(this.elementShape,this.tensors,t);return G(this.tensors[e],r)}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.`);Mr(this.elementShape,t.shape,"TensorList shape mismatch: "),nn(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}`);Mr(this.elementShape,n,"TensorList shape mismatch: "),e=e.slice(0,this.size());let r=Yd(this.elementShape,this.tensors,n);return e.length===0?er([],[0].concat(r)):M(()=>{let s=e.map(a=>G(this.tensors[a],r));return Ut(s,0)})}concat(e,t){if(!!e&&e!==this.elementDtype)throw new Error(`TensorList dtype is ${this.elementDtype} but concat requested dtype ${e}`);Mr(this.elementShape,t,"TensorList shape mismatch: ");let n=Yd(this.elementShape,this.tensors,t);return this.size()===0?er([],[0].concat(n)):M(()=>{let r=this.tensors.map(s=>G(s,n));return ot(r,0)})}};function W6(e,t,n){let r=e.dtype;if(e.shape.length<1)throw new Error(`Tensor m
2021-09-11 17:11:38 +02:00
tensor.shape[0], but sum of lengths is
2021-12-01 21:37:52 +01:00
${r}, and tensor's shape is: ${e.shape}`);let a=e.shape.slice(1),o=Cw(a,n),i=r===0?0:e.size/r,u=M(()=>{let c=[];e=G(e,[1,r,i]);for(let d=0;d<t.length;++d){let p=d===0?0:s[d-1],h=[0,p,0],f=[1,t[d],i];c[d]=G(Ve(e,h,f),o)}return e.dispose(),c}),l=new Qd([],n,e.dtype,t.length);for(let c=0;c<u.length;c++)l.setItem(c,u[c]);return l}var H6=async(e,t,n)=>{switch(e.op){case"If":case"StatelessIf":{let r=I("thenBranch",e,t,n),s=I("elseBranch",e,t,n),a=I("cond",e,t,n),o=I("args",e,t,n);return(await a.data())[0]?n.functionMap[r].executeFunctionAsync(o,n.tensorArrayMap,n.tensorListMap):n.functionMap[s].executeFunctionAsync(o,n.tensorArrayMap,n.tensorListMap)}case"While":case"StatelessWhile":{let r=I("body",e,t,n),s=I("cond",e,t,n),a=I("args",e,t,n),o=await n.functionMap[s].executeFunctionAsync(a,n.tensorArrayMap,n.tensorListMap),i=a.map(c=>c.id),u=await o[0].data();o.forEach(c=>{!c.kept&&i.indexOf(c.id)===-1&&c.dispose()});let l=a;for(;u[0];){let c=l;l=await n.functionMap[r].executeFunctionAsync(l,n.tensorArrayMap,n.tensorListMap);let d=l.map(h=>h.id);c.forEach(h=>{!h.kept&&i.indexOf(h.id)===-1&&d.indexOf(h.id)===-1&&h.dispose()});let p=await n.functionMap[s].executeFunctionAsync(l,n.tensorArrayMap,n.tensorListMap);u=await p[0].data(),p.forEach(h=>{!h.kept&&i.indexOf(h.id)===-1&&d.indexOf(h.id)===-1&&h.dispose()})}return l}case"LoopCond":{let r=I("pred",e,t,n);return[Ws(r)]}case"Switch":{let r=I("pred",e,t,n),s=I("data",e,t,n);return s.kept||(s=Ws(s)),(await r.data())[0]?[void 0,s]:[s,void 0]}case"Merge":{let r=e.inputNames.find(s=>An(s,t,n)!==void 0);if(r){let s=An(r,t,n);return[Ws(s)]}return}case"Enter":{let r=I("frameName",e,t,n),s=I("tensor",e,t,n);return n.enterFrame(r),[Ws(s)]}case"Exit":{let r=I("tensor",e,t,n);return n.exitFrame(),[Ws(r)]}case"NextIteration":{let r=I("tensor",e,t,n);return n.nextIteration(),[Ws(r)]}case"TensorArrayV3":{let r=I("size",e,t,n),s=I("dtype",e,t,n),a=I("elementShape",e,t,n),o=I("dynamicSize",e,t,n),i=I("clearAfterRead",e,t,n),u=I("identicalElementShapes",e,t,n),l=I("name",e,t,n),c=new z6(l,s,r,a,u,o,i);return n.addTensorArray(c),[c.idTensor,ke(1)]}case"TensorArrayWriteV3":{let r=I("tensorArrayId",e,t,n),s=I("index",e,t,n),a=I("tensor",e,t,n),o=n.getTensorArray(r.id);return o.write(s,a),[o.idTensor]}case"TensorArrayReadV3":{let r=I("tensorArrayId",e,t,n),s=I("index",e,t,n);return[n.getTensorArray(r.id).read(s)]}case"TensorArrayGatherV3":{let r=I("tensorArrayId",e,t,n),s=I("indices",e,t,n),a=I("dtype",e,t,n);return[n.getTensorArray(r.id).gather(s,a)]}case"TensorArrayScatterV3":{let r=I("tensorArrayId",e,t,n),s=I("indices",e,t,n),a=I("tensor",e,t,n),o=n.getTensorArray(r.id);return o.scatter(s,a),[o.idTensor]}case"TensorArrayConcatV3":{let r=I("tensorArrayId",e,t,n),s=n.getTensorArray(r.id),a=I("dtype",e,t,n);return[s.concat(a)]}case"TensorArraySplitV3":{let r=I("tensorArrayId",e,t,n),s=I("tensor",e,t,n),a=I("lengths",e,t,n),o=n.getTensorArray(r.id);return o.split(a,s),[o.idTensor]}case"TensorArraySizeV3":{let r=I("tensorArrayId",e,t,n),s=n.getTensorArray(r.id);return[ke(s.size(),"int32")]}case"TensorArrayCloseV3":{let r=I("tensorArrayId",e,t,n),s=n.getTensorArray(r.id);return s.clearAndClose(),[s.idTensor]}case"TensorListSetItem":{let r=I("tensorListId",e,t,n),s=I("index",e,t,n),a=I("tensor",e,t,n),o=n.getTensorList(r.id);return o.setItem(s,a),[o.idTensor]}case"TensorListGetItem":{let r=I("tensorListId",e,t,n),s=I("index",e,t,n),a=I("elementShape",e,t,n),o=I("elementDType",e,t,n);return[n.getTensorList(r.id).getItem(s,a,o)]}case"TensorListScatterV2":case"TensorListScatter":{let r=I("indices",e,t,n),s=I("tensor",e,t,n),a=I("elementShape",e,t,n),o=I("numElements",e,t,n),i=U6(s,r,a,o);return n.addTensorList(i),[i.idTensor]}case"TensorListReserve":case"EmptyTensorList":{let r=I("elementShape",e,t,n),s=I("elementDType",e,t,n),a;e.op==="TensorListReserve"?a="numElements":a="maxNumElements";let o=I(a,e,t,n),i=V6(r,s,o);return n.addTensorList(i),[i.idTensor]}case"TensorListGather":{let r=I("tensorListId",e,t,n),s=I("indices",e,t,n),a=I("elementShape",e,t,n),o=I("elementDType",e,t,n);return[n.getTensor
${e}`);let r;return this.size===1/0||this.size==null?r=this.size:t?r=Math.ceil(this.size/e):r=Math.floor(this.size/e),ir(async()=>(await n.iterator()).columnMajorBatch(e,t,G5),r)}concatenate(e){let t=this,n;return this.size===1/0||e.size===1/0?n=1/0:this.size!=null&&e.size!=null?n=this.size+e.size:n=null,ir(async()=>(await t.iterator()).concatenate(await e.iterator()),n)}filter(e){let t=this,n;return this.size===1/0?n=1/0:n=null,ir(async()=>(await t.iterator()).filter(r=>M(()=>e(r))),n)}async forEachAsync(e){return(await this.iterator()).forEachAsync(e)}map(e){let t=this;return ir(async()=>(await t.iterator()).map(n=>M(()=>e(n))),this.size)}mapAsync(e){let t=this;return ir(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 ir(async()=>(await t.iterator()).prefetch(e),this.size)}repeat(e){let t=this,n;return this.size!=null&&e>0?n=this.size*e:e===0?n=0:this.size!=null&&(e===void 0||e<0)?n=1/0:n=null,ir(async()=>{let r=_w(async()=>({value:await t.iterator(),done:!1}));return _5(r.take(e))},n)}skip(e){let t=this,n;return this.size!=null&&e>=0&&this.size>=e?n=this.size-e:this.size!=null&&(this.size<e||e===void 0||e<0)?n=0:n=null,ir(async()=>(await t.iterator()).skip(e),n)}shuffle(e,t,n=!0){if(e==null||e<0)throw this.size==null?new RangeError("`Dataset.shuffle()` requires bufferSize to be specified."):new RangeError(`\`Dataset.shuffle()\` requires bufferSize to be specified. If your data fits in main memory (for regular JS objects), and/or GPU memory (for \`tf.Tensor\`s), consider setting bufferSize to the dataset size (${this.size} elements)`);let r=this,s=x5.alea(t||w.now().toString());return ir(async()=>{let a=s.int32();return n&&(a+=s.int32()),(await r.iterator()).shuffle(e,a.toString())},this.size)}take(e){let t=this,n;return this.size!=null&&this.size>e?n=e:this.size!=null&&this.size<=e?n=this.size:n=null,ir(async()=>(await t.iterator()).take(e),n)}async toArray(){if(this.size===1/0)throw new Error("Can not convert infinite data stream to array.");return(await this.iterator()).toArray()}async toArrayForTest(){if(this.size===1/0)throw new Error("Can not convert infinite data stream to array.");return(await this.iterator()).toArrayForTest()}};Bc.MAX_BUFFER_SIZE=1e4;function ir(e,t=null){return new class extends Bc{constructor(){super(...arguments);this.size=t}async iterator(){return e()}}}function V5(e){return ir(async()=>HC(e),e.length)}function U5(e){if(!Lc(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 ir(async()=>{let n=await WC(e,r=>{if(r instanceof Bc)return{value:r.iterator(),recurse:!1};if(Lc(r))return{value:null,recurse:!0};throw new Error("Leaves of the structure passed to zip() must be Datasets, not primitives.")});return E5(n,dm.SHORTEST)},t)}function G5(e){if(e===null)return null;let t=e[0];return S5(t)?{value:H5(e),recurse:!1}:{value:null,recurse:!0}}function H5(e){if(e.length===0)throw new Error("Can't make a batch of zero elements.");return e[0]instanceof Ae?Ut(e):er(e)}var XC=class extends Bc{constructor(e){super();this.input=e}async iterator(){return(await this.input.iterator()).decodeUTF8().split(`
`).map(r=>(r.endsWith("\r")&&(r=r.slice(0,-1)),r))}},pm='"',Zd=Symbol("out"),YC=Symbol("field"),hm=Symbol("quote"),Aw=Symbol("quoteafterquote"),QC=Symbol("quoteinquote"),ZC=class extends Bc{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 XC(e),t||(t={}),this.hasHeader=t.hasHeader!==!1,this.fullColumnNames=t.columnNames,this.columnConfigs=t.columnConfigs,this.configuredColumnsOnly=t.configuredColumnsOnly,t.delimWhitespace?(w.assert(t.delimiter==null,()=>"Delimiter should not be provided when delimWhitespace is true."),this.delimWhitespace=!0,this.delimiter=" "):this.delimiter=t.delimiter?t.delimiter:","}async columnNames(){return this.columnNamesValidated||await this.setColumnNames(),this.configuredColumnsOnly?Object.keys(this.columnConfigs):this.fullColumnNames}async setColumnNames(){let e=await this.maybeReadHeaderLine();if(!this.fullColumnNames&&!e)throw new Error("Column names must be provided if there is no header line.");this.fullColumnNames&&e&&w.assert(e.length===this.fullColumnNames.length,()=>"The length of provided columnNames ("+this.fullColumnNames.length.toString()+") does not match the length of the header line read from file ("+e.length.toString()+")."),this.fullColumnNames||(this.fullColumnNames=e);let t=this.fullColumnNames.reduce((r,s)=>(r[s]=r[s]+1||1,r),{}),n=Object.keys(t).filter(r=>t[r]>1);if(w.assert(n.length===0,()=>"Duplicate column names found: "+n.toString()),this.columnConfigs){for(let r of Object.keys(this.columnConfigs))if(this.fullColumnNames.indexOf(r)===-1)throw new Error('The key "'+r+'" provided in columnConfigs does not match any of the column names ('+this.fullColumnNames.toString()+").")}this.columnNamesValidated=!0}async maybeReadHeaderLine(){if(this.hasHeader){let t=await(await this.base.iterator()).next();if(t.done)throw new Error("No data was found for CSV parsing.");let n=t.value;return this.parseRow(n,!1)}else return null}async iterator(){this.columnNamesValidated||await this.setColumnNames();let e=await this.base.iterator();return this.hasHeader&&(e=e.skip(1)),e.map(t=>this.makeDataElement(t))}makeDataElement(e){let t=this.parseRow(e),n={},r={};for(let s=0;s<this.fullColumnNames.length;s++){let a=this.fullColumnNames[s],o=this.columnConfigs?this.columnConfigs[a]:null;if(!(this.configuredColumnsOnly&&!o)){let i=t[s],u=null;if(i==="")if(o&&o.default!==void 0)u=o.default;else{if(o&&(o.required||o.isLabel))throw new Error(`Required column ${a} is empty in this line: ${e}`);u=void 0}else{let l=Number(i);if(isNaN(l))o&&o.dtype==="bool"?u=this.getBoolean(i):u=i;else if(!o||!o.dtype)u=l;else switch(o.dtype){case"float32":u=l;break;case"int32":u=Math.floor(l);break;case"bool":u=this.getBoolean(i);break;default:u=l}}o&&o.isLabel?r[a]=u:n[a]=u}}return Object.keys(r).length===0?n:{xs:n,ys:r}}getBoolean(e){return e==="1"||e.toLowerCase()==="true"?1:0}parseRow(e,t=!0){let n=[],r=0,s=e.length,a=Zd;for(let o=0;o<s;o++)switch(a){case Zd:switch(e.charAt(o)){case pm:r=o+1,a=hm;break;case this.delimiter:if(r=o+1,this.delimiter===" "&&this.delimWhitespace)break;n.push(""),a=Zd;break;default:a=YC,r=o;break}break;case YC:switch(e.charAt(o)){case this.delimiter:n.push(e.substring(r,o)),a=Zd,r=o+1;break;default:}break;case hm:switch(e.charAt(o)){case pm:a=Aw;break;default:}break;case Aw:switch(e.charAt(o)){case this.delimiter:n.push(e.substring(r,o-1)),a=Zd,r=o+1;break;case pm:a=hm;break;default:a=QC;break}break;case QC:switch(e.charAt(o)){case pm:a=hm;break;default:}break;default:}if(a===Aw?n.push(e.substring(r,s-1)):n.push(e.substring(r)),t&&n.length!==this.fullColumnNames.length)throw new Error(`Invalid row in csv file. Should have ${this.fullColumnNames.length} elements in a row, but got ${n}`);return n}},JC=class extends sn{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
2021-09-11 17:11:38 +02:00
============================
Hi there \u{1F44B}. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.
2021-12-01 21:37:52 +01:00
============================`));let r={id:this.nextDataId()};return this.data.set(r,{values:e,dtype:n,refCount:1}),r}makeTensorInfo(e,t,n){let r;if(t==="string"&&n!=null&&n.length>0&&w.isString(n[0])){let s=n.map(a=>w.encodeString(a));r=this.write(s,e,t)}else r=this.write(n,e,t);return{dataId:r,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,r,s){this.data.set(e,{values:t,dtype:r,refCount:s})}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 r=this.readSync(n.real.dataId),s=this.readSync(n.imag.dataId);return N.mergeRealAndImagArrays(r,s)}return this.data.get(e).values}bufferSync(e){let t=this.readSync(e.dataId),n=t;if(e.dtype==="string")try{n=t.map(r=>w.decodeString(r))}catch(r){throw new Error("Failed to decode encoded string bytes into utf-8")}return $e(e.shape,e.dtype,n)}makeOutput(e,t,n){let r=this.write(e,t,n);return is().makeTensorFromDataId(r,t,n,this)}disposeData(e,t=!1){if(this.data.has(e)){if(this.data.get(e).refCount--,!t&&this.data.get(e).refCount>0)return!1;let{complexTensorInfos:n}=this.data.get(e);n!=null&&(this.disposeData(n.real.dataId,!0),this.disposeData(n.imag.dataId,!0)),this.data.delete(e)}return!0}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}async time(e){let t=w.now();return e(),{kernelMs:w.now()-t}}memory(){return{unreliable:!0,reasons:["The reported memory is an upper bound. Due to automatic garbage collection, the true allocated memory may be less."]}}where(e){we([e],"where");let t=this.readSync(e.dataId);return aj(e.shape,t)}dispose(){}floatPrecision(){return 32}epsilon(){return super.epsilon()}},$w=iT;$w.nextDataId=0;var fm={};Ee(fm,{addImpl:()=>cT,bincountImpl:()=>Dw,bincountReduceImpl:()=>lT,ceilImpl:()=>dT,concatImpl:()=>Rw,equalImpl:()=>pT,expImpl:()=>fT,expm1Impl:()=>gT,floorImpl:()=>bT,gatherNdImpl:()=>yT,gatherV2Impl:()=>vT,greaterEqualImpl:()=>wT,greaterImpl:()=>xT,lessEqualImpl:()=>IT,lessImpl:()=>kT,linSpaceImpl:()=>ST,logImpl:()=>CT,maxImpl:()=>TT,maximumImpl:()=>NT,minimumImpl:()=>_T,multiplyImpl:()=>Pw,negImpl:()=>ET,notEqualImpl:()=>AT,prodImpl:()=>$T,rangeImpl:()=>Mw,rsqrtImpl:()=>FT,sigmoidImpl:()=>qj,simpleAbsImpl:()=>uT,sliceImpl:()=>bm,sparseFillEmptyRowsImpl:()=>RT,sparseReshapeImpl:()=>PT,sparseSegmentReductionImpl:()=>Lw,sqrtImpl:()=>Yj,squaredDifferenceImpl:()=>OT,stridedSliceImpl:()=>MT,stringNGramsImpl:()=>LT,stringSplitImpl:()=>BT,stringToHashBucketFastImpl:()=>zT,subImpl:()=>WT,tileImpl:()=>VT,topKImpl:()=>GT,transposeImpl:()=>Ow,uniqueImpl:()=>HT});function uT(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;++n)t[n]=Math.abs(e[n]);return t}var oj=e=>{let{x:t}=e.inputs,n=e.backend;we(t,"abs");let r=new Float32Array(w.sizeFromShape(t.shape)),s=n.data.get(t.dataId).values;return r=uT(s),n.makeOutput(r,t.shape,t.dtype)},ij={kernelName:Vo,backendName:"cpu",kernelFunc:oj};function Ht(e){return(t,n,r,s,a)=>{let o=N.assertAndGetBroadcastShape(t,n),i=o.length,u=w.computeStrides(o),l=w.sizeFromShape(o),c=w.getTypedArrayFromDType(a,l),d=t.length,p=n.length,h=w.computeStrides(t),f=w.computeStrides(n),m=N.getBroadcastDims(t,o),g=N.getBroadcastDims(n,o);if(m.length+g.length===0)for(let b=0;b<c.length;++b)c[b]=e(r[b%r.length],s[b%s.length]);else for(let b=0;b<c.length;++b){let y=w.indexToLoc(b,i,u),v=y.slice(-d);m.forEach(C=>v[C]=0);let x=w.locToIndex(v,d,h),k=y.slice(-p);g.forEach(C=>k[C]=0);let T=w.locToIndex(k,p,f);c[b]=e(r[x],s[T])}return[c,o]}}function ur(e){let{inputs:t,backend:n}=e,{real:r,imag:s}=t,a=n.data.get(r.dataId).values,o=n.data.get(s.dataId).values,i=n.makeTensorInfo(r.shape,"complex64"),u=n.data.get(i.dataId);return u.complexTensorInfos={real:n.makeTensorInfo(r.shape,"float32",a),imag:n.makeTensorInfo(s.shape,"float32",o)},i}var uj={kernelName:Kl,backendName:"cpu",kernelFunc:ur};function mm(e,t,n="float32"){if(n==="complex64"){let s=mm(e,t,"float32"),a=mm(e,t,"flo
2021-09-11 17:11:38 +02:00
${a.shape}`);if(r.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${r.shape}`);if(s.shape.length!==1)throw new Error(`Values must be a vector, saw:
${s.shape}`);if(o.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
2021-12-01 21:37:52 +01:00
${o.shape}`);let i=n.data.get(r.dataId).values,u=n.data.get(s.dataId).values,l=n.data.get(a.dataId).values,c=n.data.get(o.dataId).values[0],[d,p,h,f,m]=RT(i,r.shape,r.dtype,u,s.dtype,l,c);return[n.makeTensorInfo(p,r.dtype,d),n.makeTensorInfo([p[0]],s.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(g=>Number(g)))),n.makeTensorInfo([m.length],r.dtype,new Int32Array(m))]}var y7={kernelName:sd,backendName:"cpu",kernelFunc:b7};function v7(e){let{inputs:t,backend:n}=e,{inputIndices:r,inputShape:s,newShape:a}=t;if(r.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape
2021-09-11 17:11:38 +02:00
${r.shape}`);if(s.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
2021-12-01 21:37:52 +01:00
${s.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=Array.from(n.data.get(s.dataId).values),i=n.data.get(r.dataId).values,u=Array.from(n.data.get(a.dataId).values),[l,c,d]=PT(i,r.shape,r.dtype,o,u);return[n.makeTensorInfo(c,r.dtype,l),n.makeTensorInfo([d.length],a.dtype,new Int32Array(d))]}var x7={kernelName:hc,backendName:"cpu",kernelFunc:v7};function w7(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2021-09-11 17:11:38 +02:00
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2021-12-01 21:37:52 +01:00
${a.shape}`);if(s.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let o=n.data.get(r.dataId).values,i=n.data.get(s.dataId).values,u=n.data.get(a.dataId).values,[l,c]=Lw(o,r.shape,r.dtype,i,u,!0);return n.makeTensorInfo(c,r.dtype,l)}var k7={kernelName:ad,backendName:"cpu",kernelFunc:w7};function I7(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2021-09-11 17:11:38 +02:00
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2021-12-01 21:37:52 +01:00
${a.shape}`);if(s.shape[0]!==a.shape[0])throw new Error("segmentIds and indices should have same size.");let o=n.data.get(r.dataId).values,i=n.data.get(s.dataId).values,u=n.data.get(a.dataId).values,[l,c]=Lw(o,r.shape,r.dtype,i,u);return n.makeTensorInfo(c,r.dtype,l)}var S7={kernelName:od,backendName:"cpu",kernelFunc:I7};function C7(e){let{inputs:t,backend:n,attrs:r}=e,{sparseIndices:s,sparseValues:a,defaultValue:o}=t,{outputShape:i}=r,{sliceRank:u,numUpdates:l,sliceSize:c,strides:d,outputSize:p}=N.calculateShapes(a,s,i),h=!1,f=n.bufferSync(s),m=n.bufferSync(a),g=n.data.get(o.dataId).values[0],b=uN(f,m,i,p,c,l,u,d,g,h);return n.makeTensorInfo(i,b.dtype,b.values)}var T7={kernelName:id,backendName:"cpu",kernelFunc:C7};function N7(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=r,i=w.parseAxisParam(o,s.shape)[0],u=N.prepareSplitSize(s,a,i),l=new Array(s.shape.length).fill(0),c=s.shape.slice();return u.map(d=>{let p=[...c];p[i]=d;let h=eu({inputs:{x:s},backend:n,attrs:{begin:l,size:p}});return l[i]+=d,h})}var _7={kernelName:wi,backendName:"cpu",kernelFunc:N7},E7={kernelName:fc,backendName:"cpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,r=t;we(n,"square");let s=r.data.get(n.dataId).values,a=new Float32Array(s.length);for(let i=0;i<s.length;++i){let u=s[i];a[i]=u*u}return{dataId:r.write(a,n.shape,n.dtype),shape:n.shape,dtype:n.dtype}}},A7=ct(eo,(e,t)=>{let n=t;return isNaN(e)?NaN:e>0?1:n.alpha}),$7={kernelName:eo,backendName:"cpu",kernelFunc:A7};function F7(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,end:o,strides:i,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:d,shrinkAxisMask:p}=r;we(s,"stridedSlice");let{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=$t.sliceInfo(s.shape,a,o,i,u,l,c,d,p),k;if(m)k=Et({inputs:{x:s},backend:n,attrs:{shape:f}});else if(g||b){w.assert(s.shape.length>=1,()=>`Input must have rank at least 1, got: ${s.shape.length}`);let T=$t.computeOutShape(y,v,x),C=eu({inputs:{x:s},backend:n,attrs:{begin:y,size:T}});k=Et({inputs:{x:C},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(C)}else{let T=n.bufferSync(s),C=MT(h,T,x,y);k=n.makeTensorInfo(f,C.dtype,C.values)}return k}var D7={kernelName:ki,backendName:"cpu",kernelFunc:F7};function R7(e){let{inputs:t,backend:n,attrs:r}=e,{separator:s,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:l}=r,{data:c,dataSplits:d}=t,p=n.data.get(c.dataId).values,h=n.data.get(d.dataId).values,[f,m]=LT(p,h,s,a,o,i,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(d.shape,"int32",m)]}var P7={kernelName:ud,backendName:"cpu",kernelFunc:R7};function O7(e){let{inputs:t,backend:n,attrs:r}=e,{skipEmpty:s}=r,{input:a,delimiter:o}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(o.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${o.shape}`);let i=n.data.get(a.dataId).values,u=n.data.get(o.dataId).values[0],[l,c,d]=BT(i,u,s),p=c.length;return[n.makeTensorInfo([p,2],"int32",l),n.makeTensorInfo([p],"string",c),n.makeTensorInfo([2],"int32",new Int32Array(d))]}var M7={kernelName:Bh,backendName:"cpu",kernelFunc:O7};function L7(e){let{inputs:t,backend:n,attrs:r}=e,{numBuckets:s}=r,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(s<=0)throw new Error("Number of buckets must be at least 1");let o=n.data.get(a.dataId).values,i=zT(o,s);return n.makeTensorInfo(a.shape,"int32",i)}var B7={kernelName:zh,backendName:"cpu",kernelFunc:L7},z7=ct(Ii,e=>Math.tan(e)),W7={kernelName:Ii,backendName:"cpu",kernelFunc:z7},V7=ct(Za,e=>Math.tanh(e)),U7={kernelName:Za,backendName:"cpu",kernelFunc:V7};function G7(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reps:a}=r;we(s,"tile");let o=VT(n.bufferSync(s),a);return n.makeTensorInfo(o.shape,o.dtype,o.values)}var H7={kernelName:As,backendName:"cpu",kernelFunc:G7};function j7(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{k:a,sorted:o}=r;we(s,"topk
`),a=s.length.toString().length+2,o=s.map((d,p)=>w.rightPad((p+1).toString(),a)+d),i=0;for(let d=0;d<o.length;d++)i=Math.max(o[d].length,i);let u=o.slice(0,r-1),l=o.slice(r-1,r),c=o.slice(r);console.log(u.join(`
2021-09-11 17:11:38 +02:00
`)),console.log(t.split(`
2021-12-01 21:37:52 +01:00
`)[0]),console.log(`%c ${w.rightPad(l[0],i)}`,"border:1px solid red; background-color:#e3d2d2; color:#a61717"),console.log(c.join(`
`))}function bN(e){return Vs(e,()=>e.createProgram(),"Unable to create WebGLProgram.")}function yN(e,t){if(ge(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 wm(e,t){if(ge(e,()=>e.validateProgram(t)),e.getProgramParameter(t,e.VALIDATE_STATUS)===!1)throw console.log(e.getProgramInfoLog(t)),new Error("Shader program validation failed.")}function vN(e,t){let n=Vs(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return ge(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),ge(e,()=>e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function xN(e,t){let n=Vs(e,()=>e.createBuffer(),"Unable to create WebGLBuffer");return ge(e,()=>e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,n)),ge(e,()=>e.bufferData(e.ELEMENT_ARRAY_BUFFER,t,e.STATIC_DRAW)),n}function yY(){return X().getNumber("WEBGL_VERSION")===2?1:4}function wN(e){return Vs(e,()=>e.createTexture(),"Unable to create WebGLTexture.")}function kN(e,t){let n=X().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e<=0||t<=0){let r=`[${e}x${t}]`;throw new Error("Requested texture size "+r+" is invalid.")}if(e>n||t>n){let r=`[${e}x${t}]`,s=`[${n}x${n}]`;throw new Error("Requested texture size "+r+" greater than WebGL maximum on this browser / GPU "+s+".")}}function IN(e){return Vs(e,()=>e.createFramebuffer(),"Unable to create WebGLFramebuffer.")}function Kw(e,t,n,r,s,a,o){let i=e.getAttribLocation(t,n);return i===-1?!1:(ge(e,()=>e.bindBuffer(e.ARRAY_BUFFER,r)),ge(e,()=>e.vertexAttribPointer(i,s,e.FLOAT,!1,a,o)),ge(e,()=>e.enableVertexAttribArray(i)),!0)}function SN(e,t,n){EN(e,n),ge(e,()=>e.activeTexture(e.TEXTURE0+n)),ge(e,()=>e.bindTexture(e.TEXTURE_2D,t))}function vY(e,t){EN(e,t),ge(e,()=>e.activeTexture(e.TEXTURE0+t)),ge(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function CN(e,t,n){return Vs(e,()=>e.getUniformLocation(t,n),'uniform "'+n+'" not present in program.')}function TN(e,t,n){return e.getUniformLocation(t,n)}function NN(e,t,n,r){ge(e,()=>SN(e,t,r)),ge(e,()=>e.uniform1i(n,r))}function xY(e){ge(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,null)),ge(e,()=>e.viewport(0,0,e.canvas.width,e.canvas.height)),ge(e,()=>e.scissor(0,0,e.canvas.width,e.canvas.height))}function km(e,t,n){ge(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,n)),ge(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0))}function Xw(e,t){ge(e,()=>e.bindFramebuffer(e.FRAMEBUFFER,t)),ge(e,()=>e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0))}function op(e){let t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+_N(e,t))}function _N(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 Vs(e,t,n){let r=ge(e,()=>t());if(r==null)throw new Error(n);return r}function EN(e,t){let n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=t+e.TEXTURE0;if(r<e.TEXTURE0||r>n){let s=`[gl.TEXTURE0, gl.TEXTURE${n}]`;throw new Error(`textureUnit must be in ${s}.`)}}function nu(e,t=2){return w.sizeFromShape(e.slice(0,e.length-t))}function ru(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 Im(e){let t=[1,1,1];return e.length===0||e.length===1&&e[0]===1||(t=[nu(e),...ru(e)]),t}function AN(e,t=!1){let n=X().getNumber("WEBGL_MAX_TEXTURE_SIZE");t&&(n=n*2,e=e.map((s,a)=>a>=e.length-2?w.nearestLargerEven(e[a]):e[a]),e.length===1&&(e=[2,e[0]])),e.length!==2&&(e=w.squeezeShape(e).newShape);let r=w.sizeFromShape(e);if(e.length<=1&&r<=n)return[1,r];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]
2021-09-11 17:11:38 +02:00
bool isnan_custom(float val) {
return (val > 0.0 || val < 0.0) ? false : val != 0.0;
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
bvec4 isnan_custom(vec4 val) {
return bvec4(isnan_custom(val.x),
isnan_custom(val.y), isnan_custom(val.z), isnan_custom(val.w));
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
#define isnan(value) isnan_custom(value)
2021-12-01 21:37:52 +01:00
`,u="",l=`
2021-09-11 17:11:38 +02:00
#define round(value) newRound(value)
int newRound(float value) {
return int(floor(value + 0.5));
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
ivec4 newRound(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
`):(e="",t="attribute",n="varying",r="varying",s="texture2D",a="gl_FragColor",o="",i=`
#define isnan(value) isnan_custom(value)
bool isnan_custom(float val) {
return (val > 0. || val < 1. || val == 0.) ? false : true;
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
bvec4 isnan_custom(vec4 val) {
return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));
2021-06-04 14:54:48 +02:00
}
2021-12-01 21:37:52 +01:00
`,u=`
2021-09-11 17:11:38 +02:00
uniform float INFINITY;
bool isinf(float val) {
return abs(val) == INFINITY;
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
bvec4 isinf(vec4 val) {
return equal(abs(val), vec4(INFINITY));
2021-06-04 14:54:48 +02:00
}
2021-10-22 15:06:43 +02:00
`,l=`
2021-09-11 17:11:38 +02:00
int round(float value) {
return int(floor(value + 0.5));
}
ivec4 round(vec4 value) {
return ivec4(floor(value + vec4(0.5)));
}
2021-12-01 21:37:52 +01:00
`),{version:e,attribute:t,varyingVs:n,varyingFs:r,texture2D:s,output:a,defineOutput:o,defineSpecialNaN:i,defineSpecialInf:u,defineRound:l}}function su(e,t,n="index"){let r=w.computeStrides(t);return r.map((s,a)=>{let o=`int ${e[a]} = ${n} / ${s}`,i=a===r.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * ${s}`:`index -= ${e[a]} * ${s}`;return`${o}; ${i};`}).join("")}function Nm(e,t,n="index"){let r=w.computeStrides(t);return r.map((s,a)=>{let o=`int ${e[a]} = ${n} / outShapeStrides[${a}]`,i=a===r.length-1?`int ${e[a+1]} = ${n} - ${e[a]} * outShapeStrides[${a}]`:`index -= ${e[a]} * outShapeStrides[${a}]`;return`${o}; ${i};`}).join("")}function SY(e,t){let n=e.length,r=e.map(a=>`${t}[${a}]`),s=new Array(n-1);s[n-2]=r[n-1];for(let a=n-3;a>=0;--a)s[a]=`(${s[a+1]} * ${r[a+1]})`;return s}function CY(e,t,n="index"){let r=e.map((a,o)=>o),s=SY(r,t);return s.map((a,o)=>{let i=`int ${e[o]} = ${n} / ${s[o]}`,u=o===s.length-1?`int ${e[o+1]} = ${n} - ${e[o]} * ${s[o]}`:`index -= ${e[o]} * ${s[o]}`;return`${i}; ${u};`}).join("")}function Zw(e){let t=w.computeStrides(e).map(n=>n.toString());return`
2021-09-11 17:11:38 +02:00
int getFlatIndex(ivec3 coords) {
return coords.x * ${t[0]} + coords.y * ${t[1]} + coords.z;
}
2021-12-01 21:37:52 +01:00
`}function Jw(){return`
2021-09-11 17:11:38 +02:00
int getFlatIndex(ivec3 coords) {
return coords.x * outShapeStrides[0] + coords.y * outShapeStrides[1] + coords.z;
}
2021-12-01 21:37:52 +01:00
`}var MN=`
2021-09-11 17:11:38 +02:00
const float FLOAT_MAX = 1.70141184e38;
const float FLOAT_MIN = 1.17549435e-38;
lowp vec4 encode_float(highp float v) {
if (isnan(v)) {
return vec4(255, 255, 255, 255);
2021-06-04 14:54:48 +02:00
}
2021-09-11 17:11:38 +02:00
highp float av = abs(v);
if(av < FLOAT_MIN) {
return vec4(0.0, 0.0, 0.0, 0.0);
} else if(v > FLOAT_MAX) {
return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;
} else if(v < -FLOAT_MAX) {
return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
highp vec4 c = vec4(0,0,0,0);
highp float e = floor(log2(av));
highp float m = exp2(fract(log2(av))) - 1.0;
c[2] = floor(128.0 * m);
m -= c[2] / 128.0;
c[1] = floor(32768.0 * m);
m -= c[1] / 32768.0;
c[0] = floor(8388608.0 * m);
highp float ebias = e + 127.0;
c[3] = floor(ebias / 2.0);
ebias -= c[3] * 2.0;
c[2] += floor(ebias) * 128.0;
c[3] += 128.0 * step(0.0, -v);
return c / 255.0;
}
2021-12-01 21:37:52 +01:00
`,{getBroadcastDims:LN}=N;function TY(e,t,n){let r=[];if(e.forEach(h=>{let f=w.sizeFromShape(h.shapeInfo.logicalShape);if(h.shapeInfo.isUniform?r.push(`uniform float ${h.name}${f>1?`[${f}]`:""};`):(r.push(`uniform sampler2D ${h.name};`),r.push(`uniform int offset${h.name};`)),n.enableShapeUniforms){let{uniformShape:m}=ek(n.packedInputs,h.shapeInfo.logicalShape,h.shapeInfo.texShape);switch(m.length){case 1:r.push(`uniform int ${h.name}Shape;`);break;case 2:r.push(`uniform ivec2 ${h.name}Shape;`);break;case 3:r.push(`uniform ivec3 ${h.name}Shape;`);break;case 4:r.push(`uniform ivec4 ${h.name}Shape;`);break;default:break}r.push(`uniform ivec2 ${h.name}TexShape;`)}}),n.enableShapeUniforms){switch(t.logicalShape.length){case 1:r.push("uniform int outShape;");break;case 2:r.push("uniform ivec2 outShape;"),r.push("uniform int outShapeStrides;");break;case 3:r.push("uniform ivec3 outShape;"),r.push("uniform ivec2 outShapeStrides;");break;case 4:r.push("uniform ivec4 outShape;"),r.push("uniform ivec3 outShapeStrides;");break;default:break}r.push("uniform ivec2 outTexShape;")}n.customUniforms&&n.customUniforms.forEach(h=>{r.push(`uniform ${h.type} ${h.name}${h.arrayIndex?`[${h.arrayIndex}]`:""};`)});let s=r.join(`
`),a=e.map(h=>NY(h,t,n.packedInputs,n.enableShapeUniforms)).join(`
`),o=t.texShape,i=$n(),u=AY(i),l,c,d=DY(i);return t.isPacked?(l=_Y(t.logicalShape,o,n.enableShapeUniforms),c=FY(i)):(l=EY(t.logicalShape,o,n.enableShapeUniforms),c=$Y(i)),n.packedInputs&&(d+=MY),[d,u,c,s,l,a,n.userCode].join(`
`)}function Hc(e,t=!1){let n=e.shapeInfo.logicalShape;switch(n.length){case 0:return XY(e,t);case 1:return QY(e,t);case 2:return JY(e,t);case 3:return t9(e,t);case 4:return r9(e,t);case 5:return s9(e);case 6:return a9(e);default:throw new Error(`${n.length}-D input sampling is not yet supported`)}}function BN(e,t){switch(e.shapeInfo.logicalShape.length){case 0:return KY(e);case 1:return YY(e,t);case 2:return ZY(e,t);case 3:return e9(e,t);default:return n9(e,t)}}function NY(e,t,n=!1,r){let s="";n?s+=BN(e,r):s+=Hc(e,r);let a=e.shapeInfo.logicalShape,o=t.logicalShape;return a.length<=o.length&&(n?s+=o9(e,t):s+=i9(e,t)),s}function _Y(e,t,n){switch(e.length){case 0:return zN();case 1:return LY(e,t,n);case 2:return jY(e,t,n);case 3:return zY(e,t,n);default:return VY(e,t,n)}}function EY(e,t,n){switch(e.length){case 0:return zN();case 1:return BY(e,t,n);case 2:return qY(e,t,n);case 3:return WY(e,t,n);case 4:return UY(e,t,n);case 5:return GY(e,t);case 6:return HY(e,t);default:throw new Error(`${e.length}-D output sampling is not yet supported`)}}function AY(e){return`
2021-09-11 17:11:38 +02:00
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${e.texture2D}(textureSampler, uv).r;
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function $Y(e){return`
2021-09-11 17:11:38 +02:00
void setOutput(float val) {
${e.output} = vec4(val, 0, 0, 0);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function FY(e){return`
2021-09-11 17:11:38 +02:00
void setOutput(vec4 val) {
${e.output} = val;
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function DY(e){return`${e.version}
2021-09-11 17:11:38 +02:00
precision highp float;
precision highp int;
precision highp sampler2D;
${e.varyingFs} vec2 resultUV;
${e.defineOutput}
const vec2 halfCR = vec2(0.5, 0.5);
struct ivec5
{
int x;
int y;
int z;
int w;
int u;
};
struct ivec6
{
int x;
int y;
int z;
int w;
int u;
int v;
};
uniform float NAN;
${e.defineSpecialNaN}
${e.defineSpecialInf}
${e.defineRound}
int imod(int x, int y) {
return x - y * (x / y);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
int idiv(int a, int b, float sign) {
int res = a / b;
int mod = imod(a, b);
if (sign < 0. && mod != 0) {
res -= 1;
}
return res;
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
//Based on the work of Dave Hoskins
//https://www.shadertoy.com/view/4djSRW
#define HASHSCALE1 443.8975
float random(float seed){
vec2 p = resultUV * seed;
vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);
p3 += dot(p3, p3.yzx + 19.19);
return fract((p3.x + p3.y) * p3.z);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
2021-12-01 21:37:52 +01:00
${RY}
${PY}
${OY}
`}var RY=`
2021-09-11 17:11:38 +02:00
vec2 uvFromFlat(int texNumR, int texNumC, int index) {
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 packedUVfrom1D(int texNumR, int texNumC, int index) {
int texelIndex = index / 2;
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2021-12-01 21:37:52 +01:00
`,PY=`
2021-09-11 17:11:38 +02:00
vec2 packedUVfrom2D(int texelsInLogicalRow, int texNumR,
int texNumC, int row, int col) {
int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2);
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2021-12-01 21:37:52 +01:00
`,OY=`
2021-09-11 17:11:38 +02:00
vec2 packedUVfrom3D(int texNumR, int texNumC,
int texelsInBatch, int texelsInLogicalRow, int b,
int row, int col) {
int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2);
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
2021-12-01 21:37:52 +01:00
`,MY=`
2021-09-11 17:11:38 +02:00
float getChannel(vec4 frag, vec2 innerDims) {
vec2 modCoord = mod(innerDims, 2.);
return modCoord.x == 0. ?
(modCoord.y == 0. ? frag.r : frag.g) :
(modCoord.y == 0. ? frag.b : frag.a);
}
float getChannel(vec4 frag, int dim) {
float modCoord = mod(float(dim), 2.);
return modCoord == 0. ? frag.r : frag.g;
}
2021-12-01 21:37:52 +01:00
`;function zN(){return`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return 0;
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function LY(e,t,n){let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];return r[0]===1?n?`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return 2 * int(resultUV.x * ceil(float(outTexShape[1]) / 2.0));
}
`:`
int getOutputCoords() {
return 2 * int(resultUV.x * ${r[1]}.0);
}
`:r[1]===1?n?`
int getOutputCoords() {
return 2 * int(resultUV.y * ceil(float(outTexShape[0]) / 2.0));
}
`:`
int getOutputCoords() {
return 2 * int(resultUV.y * ${r[0]}.0);
}
`:n?`
int getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
return 2 * (resTexRC.x * packedTexShape[1] + resTexRC.y);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${r[0]}, ${r[1]}));
return 2 * (resTexRC.x * ${r[1]} + resTexRC.y);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function BY(e,t,n){return t[0]===1?n?`
2021-09-11 17:11:38 +02:00
int getOutputCoords() {
return int(resultUV.x * float(outTexShape[1]));
}
`:`
int getOutputCoords() {
return int(resultUV.x * ${t[1]}.0);
}
`:t[1]===1?n?`
int getOutputCoords() {
return int(resultUV.y * float(outTexShape[0]));
}
`:`
int getOutputCoords() {
return int(resultUV.y * ${t[0]}.0);
}
`:n?`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
return resTexRC.x * outTexShape[1] + resTexRC.y;
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
return resTexRC.x * ${t[1]} + resTexRC.y;
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function zY(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[2]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec3(b, r, c);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`;let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],s=Math.ceil(e[2]/2),a=s*Math.ceil(e[1]/2);return`
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${r[0]}, ${r[1]}));
int index = resTexRC.x * ${r[1]} + resTexRC.y;
int b = index / ${a};
index -= b * ${a};
int r = 2 * (index / ${s});
int c = imod(index, ${s}) * 2;
return ivec3(b, r, c);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function WY(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
2021-12-01 21:37:52 +01:00
${Nm(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
}
2021-12-01 21:37:52 +01:00
`;let r=su(["r","c","d"],e);return`
2021-09-11 17:11:38 +02:00
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${r}
return ivec3(r, c, d);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function VY(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec4 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int texelsInLogicalRow = int(ceil(float(outShape[3]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[2]) / 2.0));
int texelsInBatchN = texelsInBatch * outShape[1];
int b2 = index / texelsInBatchN;
index -= b2 * texelsInBatchN;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec4(b2, b, r, c);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`;let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)],s=Math.ceil(e[e.length-1]/2),a=s*Math.ceil(e[e.length-2]/2),o=a,i="",u="b, r, c";for(let l=2;l<e.length-1;l++)o*=e[e.length-l-1],i=`
2021-10-22 15:06:43 +02:00
int b${l} = index / ${o};
index -= b${l} * ${o};
2021-12-01 21:37:52 +01:00
`+i,u=`b${l}, `+u;return`
2021-09-11 17:11:38 +02:00
ivec${e.length} getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${r[0]}, ${r[1]}));
int index = resTexRC.x * ${r[1]} + resTexRC.y;
${i}
int b = index / ${a};
index -= b * ${a};
int r = 2 * (index / ${s});
int c = imod(index, ${s}) * 2;
2021-12-01 21:37:52 +01:00
return ivec${e.length}(${u});
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function UY(e,t,n){if(n)return`
2021-09-11 17:11:38 +02:00
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
2021-12-01 21:37:52 +01:00
${Nm(["r","c","d","d2"],e)}
2021-09-11 17:11:38 +02:00
return ivec4(r, c, d, d2);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`;let r=su(["r","c","d","d2"],e);return`
2021-09-11 17:11:38 +02:00
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${r}
return ivec4(r, c, d, d2);
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function GY(e,t){let n=su(["r","c","d","d2","d3"],e);return`
2021-09-11 17:11:38 +02:00
ivec5 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(${t[0]},
${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${n}
ivec5 outShape = ivec5(r, c, d, d2, d3);
return outShape;
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function HY(e,t){let n=su(["r","c","d","d2","d3","d4"],e);return`
2021-09-11 17:11:38 +02:00
ivec6 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
${n}
ivec6 result = ivec6(r, c, d, d2, d3, d4);
return result;
2021-05-18 14:11:17 +02:00
}
2021-12-01 21:37:52 +01:00
`}function jY(e,t,n){let r=[Math.ceil(t[0]/2),Math.ceil(t[1]/2)];if(w.arraysEqual(e,t))return n?`
2021-09-11 17:11:38 +02:00
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
return 2 * ivec2(resultUV.yx * vec2(packedTexShape[0], packedTexShape[1]));
}
`:`
ivec2 getOutputCoords() {
return 2 * ivec2(resultUV.yx * vec2(${r[0]}, ${r[1]}));
}
`;let s=Math.ceil(e[1]/2);return n?`
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec2(r, c);
2021-05-18 14:11:17 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${r[0]}, ${r[1]}));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int index = resTexRC.x * ${r[1]} + resTexRC.y;
int r = 2 * (index / ${s});
int c = imod(index, ${s}) * 2;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return ivec2(r, c);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}function qY(e,t,n){return w.arraysEqual(e,t)?n?`
2021-09-11 17:11:38 +02:00
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(outTexShape[0], outTexShape[1]));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(${t[0]}, ${t[1]}));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:e[1]===1?n?`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
return ivec2(index, 0);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
return ivec2(index, 0);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:e[0]===1?n?`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
return ivec2(0, index);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
return ivec2(0, index);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:n?`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
int r = index / outShape[1];
int c = index - r * outShape[1];
return ivec2(r, c);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.x * ${t[1]} + resTexRC.y;
int r = index / ${e[1]};
int c = index - r * ${e[1]};
return ivec2(r, c);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}function au(e){return`offset${e}`}function KY(e){let t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1),r=$n();return`
2021-09-11 17:11:38 +02:00
vec4 ${n}() {
return ${r.texture2D}(${t}, halfCR);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}function XY(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`float ${r}() {return ${n};}`;let[s,a]=e.shapeInfo.texShape;if(s===1&&a===1)return`
2021-09-11 17:11:38 +02:00
float ${r}() {
return sampleTexture(${n}, halfCR);
}
2021-12-01 21:37:52 +01:00
`;let o=au(n);if(t)return`
2021-09-11 17:11:38 +02:00
float ${r}() {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], ${o});
return sampleTexture(${n}, uv);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`;let[i,u]=e.shapeInfo.texShape;return`
2021-09-11 17:11:38 +02:00
float ${r}() {
2021-12-01 21:37:52 +01:00
vec2 uv = uvFromFlat(${i}, ${u}, ${o});
2021-09-11 17:11:38 +02:00
return sampleTexture(${n}, uv);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}function YY(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),s=e.shapeInfo.texShape,a=$n();if(t)return`
2021-09-11 17:11:38 +02:00
vec4 ${r}(int index) {
ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
vec2 uv = packedUVfrom1D(
packedTexShape[0], packedTexShape[1], index);
return ${a.texture2D}(${n}, uv);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`;let o=[Math.ceil(s[0]/2),Math.ceil(s[1]/2)];return`
vec4 ${r}(int index) {
vec2 uv = packedUVfrom1D(
${o[0]}, ${o[1]}, index);
return ${a.texture2D}(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`}function QY(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1);if(e.shapeInfo.isUniform)return`
2021-09-11 17:11:38 +02:00
float ${r}(int index) {
2021-12-01 21:37:52 +01:00
${jc(e)}
2021-09-11 17:11:38 +02:00
}
`;let s=e.shapeInfo.texShape,a=s[0],o=s[1];if(o===1&&a===1)return`
float ${r}(int index) {
return sampleTexture(${n}, halfCR);
}
2021-12-01 21:37:52 +01:00
`;let i=au(n);return o===1?t?`
2021-09-11 17:11:38 +02:00
float ${r}(int index) {
vec2 uv = vec2(0.5, (float(index + ${i}) + 0.5) / float(${n}TexShape[0]));
return sampleTexture(${n}, uv);
}
`:`
float ${r}(int index) {
vec2 uv = vec2(0.5, (float(index + ${i}) + 0.5) / ${a}.0);
return sampleTexture(${n}, uv);
}
`:a===1?t?`
float ${r}(int index) {
vec2 uv = vec2((float(index + ${i}) + 0.5) / float(${n}TexShape[1]), 0.5);
return sampleTexture(${n}, uv);
}
`:`
float ${r}(int index) {
vec2 uv = vec2((float(index + ${i}) + 0.5) / ${o}.0, 0.5);
return sampleTexture(${n}, uv);
}
`:t?`
float ${r}(int index) {
vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], index + ${i});
return sampleTexture(${n}, uv);
}
`:`
float ${r}(int index) {
vec2 uv = uvFromFlat(${a}, ${o}, index + ${i});
return sampleTexture(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`}function ZY(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape,o=a[0],i=a[1],u=$n();if(a!=null&&w.arraysEqual(n,a))return t?`
2021-09-11 17:11:38 +02:00
vec4 ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
2021-12-01 21:37:52 +01:00
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`:`
vec4 ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${i}.0, ${o}.0);
2021-12-01 21:37:52 +01:00
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
`;if(t)return`
vec4 ${s}(int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${r}TexShape[0]) / 2.0), ceil(float(${r}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${r}Shape[1]) / 2.0));
vec2 uv = packedUVfrom2D(valuesPerRow, packedTexShape[0], packedTexShape[1], row, col);
2021-12-01 21:37:52 +01:00
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`;let l=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],c=Math.ceil(n[1]/2);return`
2021-09-11 17:11:38 +02:00
vec4 ${s}(int row, int col) {
2021-12-01 21:37:52 +01:00
vec2 uv = packedUVfrom2D(${c}, ${l[0]}, ${l[1]}, row, col);
return ${u.texture2D}(${r}, uv);
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`}function JY(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape;if(a!=null&&w.arraysEqual(n,a)){if(t)return`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
}
`;let p=a[0],h=a[1];return`
float ${s}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${h}.0, ${p}.0);
return sampleTexture(${r}, uv);
}
2021-12-01 21:37:52 +01:00
`}let{newShape:o,keptDims:i}=w.squeezeShape(n),u=o;if(u.length<n.length){let p=qc(e,u),h=["row","col"];return`
${Hc(p,t)}
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col) {
2021-12-01 21:37:52 +01:00
return ${s}(${Kc(h,i)});
2021-09-11 17:11:38 +02:00
}
`}if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col) {
int index = round(dot(vec2(row, col), vec2(${n[1]}, 1)));
2021-12-01 21:37:52 +01:00
${jc(e)}
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`;let l=a[0],c=a[1],d=au(r);return c===1?t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${d}), vec3(${r}Shape[1], 1, 1));
vec2 uv = vec2(0.5, (index + 0.5) / float(${r}TexShape[0]));
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${d}), vec3(${n[1]}, 1, 1));
2021-10-22 15:06:43 +02:00
vec2 uv = vec2(0.5, (index + 0.5) / ${l}.0);
2021-09-11 17:11:38 +02:00
return sampleTexture(${r}, uv);
}
2021-10-22 15:06:43 +02:00
`:l===1?t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${d}), vec3(${r}Shape[1], 1, 1));
vec2 uv = vec2((index + 0.5) / float(${r}TexShape[1]), 0.5);
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col) {
float index = dot(vec3(row, col, ${d}), vec3(${n[1]}, 1, 1));
2021-12-01 21:37:52 +01:00
vec2 uv = vec2((index + 0.5) / ${c}.0, 0.5);
2021-09-11 17:11:38 +02:00
return sampleTexture(${r}, uv);
}
`:t?`
float ${s}(int row, int col) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${r}Shape[1] + col + ${d};
vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index);
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${n[1]} + col + ${d};
2021-12-01 21:37:52 +01:00
vec2 uv = uvFromFlat(${l}, ${c}, index);
2021-09-11 17:11:38 +02:00
return sampleTexture(${r}, uv);
}
2021-12-01 21:37:52 +01:00
`}function e9(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape,o=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)];if(n[0]===1){let p=n.slice(1),h=[1,2],f=qc(e,p),m=["b","row","col"];return`
${BN(f,t)}
2021-09-11 17:11:38 +02:00
vec4 ${s}(int b, int row, int col) {
2021-12-01 21:37:52 +01:00
return ${s}(${Kc(m,h)});
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`}let i=$n();if(t)return`
2021-09-11 17:11:38 +02:00
vec4 ${s}(int b, int row, int col) {
ivec2 packedTexShape = ivec2(ceil(float(${r}TexShape[0]) / 2.0), ceil(float(${r}TexShape[1]) / 2.0));
int valuesPerRow = int(ceil(float(${r}Shape[2]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${r}Shape[1]) / 2.0));
vec2 uv = packedUVfrom3D(
packedTexShape[0], packedTexShape[1], texelsInBatch, valuesPerRow, b, row, col);
return ${i.texture2D}(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`;let u=o[0],l=o[1],c=Math.ceil(n[2]/2),d=c*Math.ceil(n[1]/2);return`
2021-09-11 17:11:38 +02:00
vec4 ${s}(int b, int row, int col) {
vec2 uv = packedUVfrom3D(
2021-12-01 21:37:52 +01:00
${u}, ${l}, ${d}, ${c}, b, row, col);
2021-09-11 17:11:38 +02:00
return ${i.texture2D}(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}function t9(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=n[1]*n[2],o=n[2],{newShape:i,keptDims:u}=w.squeezeShape(n),l=i;if(l.length<n.length){let m=qc(e,l),g=["row","col","depth"];return`
${Hc(m,t)}
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth) {
2021-12-01 21:37:52 +01:00
return ${s}(${Kc(g,u)});
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`}if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col, int depth) {
int index = round(dot(vec3(row, col, depth),
vec3(${a}, ${o}, 1)));
2021-12-01 21:37:52 +01:00
${jc(e)}
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`;let c=e.shapeInfo.texShape,d=c[0],p=c[1],h=e.shapeInfo.flatOffset;if(p===a&&h==null)return t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth) {
int stride1 = ${r}Shape[2];
float texR = float(row);
float texC = dot(vec2(col, depth), vec2(stride1, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col, int depth) {
float texR = float(row);
float texC = dot(vec2(col, depth), vec2(${o}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${p}.0, ${d}.0);
return sampleTexture(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`;if(p===o&&h==null)return t?`
float ${s}(int row, int col, int depth) {
float texR = dot(vec2(row, col), vec2(${r}Shape[1], 1));
float texC = float(depth);
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`:`
float ${s}(int row, int col, int depth) {
float texR = dot(vec2(row, col), vec2(${n[1]}, 1));
float texC = float(depth);
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${p}.0, ${d}.0);
return sampleTexture(${r}, uv);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`;let f=au(r);return t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int stride0 = ${r}Shape[1] * ${r}Shape[2];
int stride1 = ${r}Shape[2];
int index = row * ${a} + col * ${o} + depth + ${f};
vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index);
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col, int depth) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${a} + col * ${o} + depth + ${f};
vec2 uv = uvFromFlat(${d}, ${p}, index);
return sampleTexture(${r}, uv);
}
2021-12-01 21:37:52 +01:00
`}function n9(e,t){let n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),s=$n();if(t)return`
2021-09-11 17:11:38 +02:00
vec4 ${r}(int b2, int b, int row, int col) {
int valuesPerRow = int(ceil(float(${n}Shape[3]) / 2.0));
int texelsInBatch = valuesPerRow * int(ceil(float(${n}Shape[2]) / 2.0));
int index = b * texelsInBatch + (row / 2) * valuesPerRow + (col / 2);
texelsInBatch *= ${n}Shape[1];
index = b2 * texelsInBatch + index;
ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
int texR = index / packedTexShape[1];
int texC = index - texR * packedTexShape[1];
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(packedTexShape[1], packedTexShape[0]); return ${s.texture2D}(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`;let a=e.shapeInfo.logicalShape,o=a.length,i=e.shapeInfo.texShape,u=[Math.ceil(i[0]/2),Math.ceil(i[1]/2)],l=u[0],c=u[1],d=Math.ceil(a[o-1]/2),p=d*Math.ceil(a[o-2]/2),h="int b, int row, int col",f=`b * ${p} + (row / 2) * ${d} + (col / 2)`;for(let m=2;m<o-1;m++)h=`int b${m}, `+h,p*=a[o-m-1],f=`b${m} * ${p} + `+f;return`
2021-09-11 17:11:38 +02:00
vec4 ${r}(${h}) {
int index = ${f};
2021-12-01 21:37:52 +01:00
int texR = index / ${c};
int texC = index - texR * ${c};
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${c}, ${l});
2021-09-11 17:11:38 +02:00
return ${s.texture2D}(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`}function r9(e,t){let n=e.shapeInfo.logicalShape,r=e.name,s="get"+r.charAt(0).toUpperCase()+r.slice(1),a=n[3],o=n[2]*a,i=n[1]*o,{newShape:u,keptDims:l}=w.squeezeShape(n);if(u.length<n.length){let y=qc(e,u),v=["row","col","depth","depth2"];return`
${Hc(y,t)}
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth, int depth2) {
2021-12-01 21:37:52 +01:00
return ${s}(${Kc(v,l)});
2021-09-11 17:11:38 +02:00
}
`}if(e.shapeInfo.isUniform)return`
float ${s}(int row, int col, int depth, int depth2) {
int index = round(dot(vec4(row, col, depth, depth2),
vec4(${i}, ${o}, ${a}, 1)));
2021-12-01 21:37:52 +01:00
${jc(e)}
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`;let c=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,p=d[0],h=d[1],f=`int stride2 = ${r}Shape[3];`,m=`int stride1 = ${r}Shape[2] * stride2;`,g=`int stride0 = ${r}Shape[1] * stride1;`;if(h===i&&c==null)return t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth, int depth2) {
${f}
${m}
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(stride1, stride2, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col, int depth, int depth2) {
float texR = float(row);
float texC =
dot(vec3(col, depth, depth2),
vec3(${o}, ${a}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${p}.0);
return sampleTexture(${r}, uv);
}
2021-12-01 21:37:52 +01:00
`;if(h===a&&c==null)return t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth, int depth2) {
float texR = dot(vec3(row, col, depth),
vec3(${r}Shape[1] * ${r}Shape[2], ${r}Shape[2], 1));
float texC = float(depth2);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${r}TexShape[1], ${r}TexShape[0]);
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col, int depth, int depth2) {
float texR = dot(vec3(row, col, depth),
vec3(${n[1]*n[2]}, ${n[2]}, 1));
float texC = float(depth2);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${p}.0);
return sampleTexture(${r}, uv);
}
2021-12-01 21:37:52 +01:00
`;let b=au(r);return t?`
2021-09-11 17:11:38 +02:00
float ${s}(int row, int col, int depth, int depth2) {
// Explicitly use integer operations as dot() only works on floats.
${f}
${m}
${g}
int index = row * stride0 + col * stride1 +
depth * stride2 + depth2;
vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index + ${b});
return sampleTexture(${r}, uv);
}
`:`
float ${s}(int row, int col, int depth, int depth2) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${i} + col * ${o} +
depth * ${a} + depth2;
vec2 uv = uvFromFlat(${p}, ${h}, index + ${b});
return sampleTexture(${r}, uv);
}
2021-12-01 21:37:52 +01:00
`}function s9(e){let t=e.shapeInfo.logicalShape,n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),s=t[4],a=t[3]*s,o=t[2]*a,i=t[1]*o,{newShape:u,keptDims:l}=w.squeezeShape(t);if(u.length<t.length){let m=qc(e,u),g=["row","col","depth","depth2","depth3"];return`
${Hc(m)}
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth, int depth2, int depth3) {
2021-12-01 21:37:52 +01:00
return ${r}(${Kc(g,l)});
2021-09-11 17:11:38 +02:00
}
`}if(e.shapeInfo.isUniform)return`
float ${r}(int row, int col, int depth, int depth2, int depth3) {
float index = dot(
vec4(row, col, depth, depth2),
vec4(${i}, ${o}, ${a}, ${s})) +
depth3;
2021-12-01 21:37:52 +01:00
${jc(e)}
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`;let c=e.shapeInfo.flatOffset,d=e.shapeInfo.texShape,p=d[0],h=d[1];if(h===i&&c==null)return`
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth, int depth2, int depth3) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
vec4(${o}, ${a}, ${s}, 1));
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${p}.0);
return sampleTexture(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`;if(h===s&&c==null)return`
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth, int depth2, int depth3) {
float texR = dot(
vec4(row, col, depth, depth2),
vec4(${t[1]*t[2]*t[3]},
${t[2]*t[3]}, ${t[3]}, 1));
int texC = depth3;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${h}.0, ${p}.0);
return sampleTexture(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`;let f=au(n);return`
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth, int depth2, int depth3) {
// Explicitly use integer operations as dot() only works on floats.
int index = row * ${i} + col * ${o} + depth * ${a} +
depth2 * ${s} + depth3 + ${f};
vec2 uv = uvFromFlat(${p}, ${h}, index);
return sampleTexture(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`}function a9(e){let t=e.shapeInfo.logicalShape,n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),{newShape:s,keptDims:a}=w.squeezeShape(t);if(s.length<t.length){let g=qc(e,s),b=["row","col","depth","depth2","depth3","depth4"];return`
${Hc(g)}
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
2021-12-01 21:37:52 +01:00
return ${r}(${Kc(b,a)});
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`}let o=t[5],i=t[4]*o,u=t[3]*i,l=t[2]*u,c=t[1]*l;if(e.shapeInfo.isUniform)return`
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int index = round(dot(
vec4(row, col, depth, depth2),
2021-12-01 21:37:52 +01:00
vec4(${c}, ${l}, ${u}, ${i})) +
2021-09-11 17:11:38 +02:00
dot(
vec2(depth3, depth4),
vec2(${o}, 1)));
2021-12-01 21:37:52 +01:00
${jc(e)}
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`;let d=e.shapeInfo.flatOffset,p=e.shapeInfo.texShape,h=p[0],f=p[1];if(f===c&&d==null)return`
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int texR = row;
float texC = dot(vec4(col, depth, depth2, depth3),
2021-12-01 21:37:52 +01:00
vec4(${l}, ${u}, ${i}, ${o})) +
2021-09-11 17:11:38 +02:00
float(depth4);
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
`;if(f===o&&d==null)return`
float ${r}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
float texR = dot(vec4(row, col, depth, depth2),
vec4(${t[1]*t[2]*t[3]*t[4]},
${t[2]*t[3]*t[4]},
${t[3]*t[4]},
${t[4]})) + float(depth3);
int texC = depth4;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${f}.0, ${h}.0);
return sampleTexture(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`;let m=au(n);return`
2021-09-11 17:11:38 +02:00
float ${r}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
// Explicitly use integer operations as dot() only works on floats.
2021-12-01 21:37:52 +01:00
int index = row * ${c} + col * ${l} + depth * ${u} +
2021-09-11 17:11:38 +02:00
depth2 * ${i} + depth3 * ${o} + depth4 + ${m};
vec2 uv = uvFromFlat(${h}, ${f}, index);
return sampleTexture(${n}, uv);
}
2021-12-01 21:37:52 +01:00
`}function jc(e){let t=e.name,n=w.sizeFromShape(e.shapeInfo.logicalShape);return n<2?`return ${t};`:`
2021-09-11 17:11:38 +02:00
for (int i = 0; i < ${n}; i++) {
if (i == index) {
return ${t}[i];
}
}
2021-12-01 21:37:52 +01:00
`}function o9(e,t){let n=e.name,r=n.charAt(0).toUpperCase()+n.slice(1),s="get"+r+"AtOutCoords",a=e.shapeInfo.logicalShape.length,o=t.logicalShape.length,i=LN(e.shapeInfo.logicalShape,t.logicalShape),u=gt(o),l=o-a,c,d=["x","y","z","w","u","v"];a===0?c="":o<2&&i.length>=1?c="coords = 0;":c=i.map(y=>`coords.${d[y+l]} = 0;`).join(`
`);let p="";o<2&&a>0?p="coords":p=e.shapeInfo.logicalShape.map((y,v)=>`coords.${d[v+l]}`).join(", ");let h="return outputValue;",m=w.sizeFromShape(e.shapeInfo.logicalShape)===1,b=w.sizeFromShape(t.logicalShape)===1;if(a===1&&!m&&!b)h=`
2021-09-11 17:11:38 +02:00
return vec4(outputValue.xy, outputValue.xy);
`;else if(m&&!b)o===1?h=`
return vec4(outputValue.x, outputValue.x, 0., 0.);
`:h=`
return vec4(outputValue.x);
2021-12-01 21:37:52 +01:00
`;else if(i.length){let y=a-2,v=a-1;i.indexOf(y)>-1&&i.indexOf(v)>-1?h="return vec4(outputValue.x);":i.indexOf(y)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":i.indexOf(v)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}return`
2021-09-11 17:11:38 +02:00
vec4 ${s}() {
2021-12-01 21:37:52 +01:00
${u} coords = getOutputCoords();
${c}
2021-09-11 17:11:38 +02:00
vec4 outputValue = get${r}(${p});
${h}
}
2021-12-01 21:37:52 +01:00
`}function i9(e,t){let n=e.name,r=n.charAt(0).toUpperCase()+n.slice(1),s="get"+r+"AtOutCoords",a=t.texShape,o=e.shapeInfo.texShape,i=e.shapeInfo.logicalShape.length,u=t.logicalShape.length;if(!e.shapeInfo.isUniform&&i===u&&e.shapeInfo.flatOffset==null&&w.arraysEqual(o,a))return`
2021-09-11 17:11:38 +02:00
float ${s}() {
return sampleTexture(${n}, resultUV);
}
2021-12-01 21:37:52 +01:00
`;let l=gt(u),c=LN(e.shapeInfo.logicalShape,t.logicalShape),d=u-i,p,h=["x","y","z","w","u","v"];i===0?p="":u<2&&c.length>=1?p="coords = 0;":p=c.map(m=>`coords.${h[m+d]} = 0;`).join(`
`);let f="";return u<2&&i>0?f="coords":f=e.shapeInfo.logicalShape.map((m,g)=>`coords.${h[g+d]}`).join(", "),`
2021-09-11 17:11:38 +02:00
float ${s}() {
2021-10-22 15:06:43 +02:00
${l} coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
${p}
return get${r}(${f});
}
2021-12-01 21:37:52 +01:00
`}function gt(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 ek(e,t,n){let{newShape:r,keptDims:s}=w.squeezeShape(t),a=t.length,o=e&&a===3&&t[0]===1,i=o?t.slice(1):r,u=!e&&a>1&&!w.arraysEqual(t,n)&&r.length<a||o;return{useSqueezeShape:u,uniformShape:u?i:t,keptDims:s}}function qc(e,t){let n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function Kc(e,t){return t.map(n=>e[n]).join(", ")}function u9(e,t,n,r){let s=n.map((x,k)=>{let T={logicalShape:x.shape,texShape:x.isUniform?null:x.texData.texShape,isUniform:x.isUniform,isPacked:x.isUniform?!1:x.texData.isPacked,flatOffset:null};return x.texData!=null&&x.texData.slice!=null&&x.texData.slice.flatOffset>0&&(T.flatOffset=x.texData.slice.flatOffset),{name:t.variableNames[k],shapeInfo:T}}),a=s.map(x=>x.shapeInfo),o={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1,isPacked:r.texData.isPacked,flatOffset:null},i=TY(s,o,t),u=gN(e.gl,i),l=e.createProgram(u),c=null,d=e.getUniformLocation(l,"NAN",!1);X().getNumber("WEBGL_VERSION")===1&&(c=e.getUniformLocation(l,"INFINITY",!1));let p=!1,h={},f={},m={};for(let x=0;x<t.variableNames.length;x++){let k=t.variableNames[x];h[k]=e.getUniformLocation(l,k,p),h[`offset${k}`]=e.getUniformLocation(l,`offset${k}`,p),t.enableShapeUniforms&&(f[`${k}Shape`]=e.getUniformLocation(l,`${k}Shape`,p),m[`${k}TexShape`]=e.getUniformLocation(l,`${k}TexShape`,p))}let g,b,y;t.enableShapeUniforms&&(g=e.getUniformLocation(l,"outShape",p),y=e.getUniformLocation(l,"outShapeStrides",p),b=e.getUniformLocation(l,"outTexShape",p));let v=[];return t.customUniforms&&t.customUniforms.forEach((x,k)=>{v[k]=e.getUniformLocation(l,x.name,p)}),{program:t,fragmentShader:u,source:i,webGLProgram:l,uniformLocations:h,customUniformLocations:v,inShapeInfos:a,outShapeInfo:o,infLoc:c,nanLoc:d,inShapesLocations:f,inTexShapesLocations:m,outShapeLocation:g,outShapeStridesLocation:y,outTexShapeLocation:b}}function WN(e,t){if(e.length!==t.length)throw Error(`Binary was compiled with ${e.length} inputs, but was executed with ${t.length} inputs`);e.forEach((n,r)=>{let s=n.logicalShape,a=t[r],o=a.shape;if(!w.arraysEqual(s,o))throw Error(`Binary was compiled with different shapes than the current args. Shapes ${s} and ${o} must match`);if(n.isUniform&&a.isUniform)return;let i=n.texShape,u=a.isUniform?null:a.texData.texShape;if(!w.arraysEqual(i,u))throw Error(`Binary was compiled with different texture shapes than the current args. Shape ${i} and ${u} must match`)})}function c9(e,t,n,r,s){t.program.enableShapeUniforms||(WN(t.inShapeInfos,n),WN([t.outShapeInfo],[r]));let a=r.texData.texture,o=r.texData.texShape;r.texData.isPacked?e.setOutputPackedMatrixTexture(a,o[0],o[1]):e.setOutputMatrixTexture(a,o[0],o[1]),e.setProgram(t.webGLProgram),X().getNumber("WEBGL_VERSION")===1&&t.infLoc!==null&&e.gl.uniform1f(t.infLoc,1/0),t.nanLoc!==null&&e.gl.uniform1f(t.nanLoc,NaN),n.forEach((u,l)=>{let c=t.program.variableNames[l],d=t.uniformLocations[c],p=t.uniformLocations[`offset${c}`],h=t.inShapesLocations[`${c}Shape`],f=t.inTexShapesLocations[`${c}TexShape`];if(h){let{uniformShape:m}=ek(t.program.packedInputs,u.shape,u.texData.texShape);switch(m.length){case 1:e.gl.uniform1iv(h,new Int32Array(m));break;case 2:e.gl.uniform2iv(h,new Int32Array(m));break;case 3:e.gl.uniform3iv(h,new Int32Array(m));break;case 4:e.gl.uniform4iv(h,new Int32Array(m));break;default:break}}if(f&&e.gl.uniform2i(f,u.texData.texShape[0],u.texData.texShape[1]),d!=null){if(u.isUniform){if(w.sizeFromShape(u.shape)<2)e.gl.uniform1f(d,u.uniformValues[0]);else{let m=u.uniformValues;m instanceof Float32Array||(m=new Float32Array(m)),e.gl.uniform1fv(d,m)}return}u.texData.slice!=null&&p!=null&&e.gl.uniform1i(p,u.texData.slice.flatOffset),e.setInputMatrixTexture(u.texData.texture,d,l)}});let i=t.outShapeLocation;if(i)switch(r.shape.length){case 1:e.gl.uniform1iv(i,new Int32Array(r.shape));break;case 2:e.gl.uniform2iv(i,new Int32Array(r.shape));break;case 3:e.gl
2021-09-11 17:11:38 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2021-12-01 21:37:52 +01:00
${this.enableShapeUniforms?Nm(["r","c","d"],e):su(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i=0; i<4; i++) {
int flatIndex = index + i;
ivec3 rc = outCoordsFromFlatIndex(flatIndex);
result[i] = getA(rc.x, rc.y, rc.z);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${t.output} = result;
}
2021-12-01 21:37:52 +01:00
`}},p9=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=rp.DENSE,this.customUniforms=[{name:"texShape",type:"ivec2"}];let t=$n();this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
ivec3 outCoordsFromFlatIndex(int index) {
2021-12-01 21:37:52 +01:00
${this.enableShapeUniforms?Nm(["r","c","d"],e):su(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(texShape[0], texShape[1]));
int index = 4 * (resTexRC.x * texShape[1] + resTexRC.y);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i=0; i<4; i++) {
int flatIndex = index + i;
ivec3 rc = outCoordsFromFlatIndex(flatIndex);
result[i] = getChannel(getA(rc.x, rc.y, rc.z), vec2(rc.y, rc.z));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
${t.output} = result;
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},h9=class{constructor(e){this.variableNames=["A"],this.outTexUsage=wr.DOWNLOAD;let t=$n();this.outputShape=e,this.userCode=`
${MN}
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
float x = getAAtOutCoords();
${t.output} = encode_float(x);
}
2021-12-01 21:37:52 +01:00
`}},f9=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=wr.DOWNLOAD;let t=$n();this.outputShape=e,this.userCode=`
${MN}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
float x = getChannel(getAAtOutCoords(), vec2(coords.y, coords.z));
${t.output} = encode_float(x);
}
2021-12-01 21:37:52 +01:00
`}},m9=class{constructor(e,t=!1){this.variableNames=["A"],this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=$n();this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length);let r="result";t&&(r="floor(result * 255. + 0.5)"),this.userCode=`
${this.enableShapeUniforms?Jw():Zw(e)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int flatIndex = getFlatIndex(coords);
int offset = imod(flatIndex, 4);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
flatIndex = idiv(flatIndex, 4, 1.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int r = flatIndex / texShape[1];
int c = imod(flatIndex, texShape[1]);
vec2 uv = (vec2(c, r) + halfCR) / vec2(texShape[1], texShape[0]);
vec4 values = ${n.texture2D}(A, uv);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float result;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
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-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${n.output} = vec4(${r}, 0., 0., 0.);
}
2021-12-01 21:37:52 +01:00
`}},g9=class{constructor(e,t=!1){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.customUniforms=[{name:"texShape",type:"ivec2"}];let n=$n();this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length);let r="",s="result";t&&(s="floor(result * 255. + 0.5)");for(let a=0;a<=1;a++)for(let o=0;o<=1;o++){let i=a*2+o;r+=`
2021-09-11 17:11:38 +02:00
localCoords = coords;
if(localCoords[2] + ${o} < ${this.enableShapeUniforms?"outShape[2]":`${e[2]}`}) {
localCoords[2] += ${o};
if (localCoords[1] + ${a} < ${this.enableShapeUniforms?"outShape[1]":`${e[1]}`}) {
localCoords[1] += ${a};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
flatIndex = getFlatIndex(localCoords);
offset = imod(flatIndex, 4);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
flatIndex = idiv(flatIndex, 4, 1.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int r = flatIndex / texShape[1];
int c = imod(flatIndex, texShape[1]);
vec2 uv = (vec2(c, r) + halfCR) / vec2(texShape[1], texShape[0]);
values = ${n.texture2D}(A, uv);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (offset == 0) {
result[${i}] = values[0];
} else if (offset == 1) {
result[${i}] = values[1];
} else if (offset == 2) {
result[${i}] = values[2];
} else {
result[${i}] = values[3];
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
`}this.userCode=`
2021-12-01 21:37:52 +01:00
${this.enableShapeUniforms?Jw():Zw(e)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
int flatIndex, r, c, offset;
ivec3 localCoords;
vec2 uv;
vec4 values;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${r}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${n.output} = ${s};
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},VN={};Ee(VN,{bindVertexProgramAttributeStreams:()=>QN,createBufferFromOutputTexture:()=>e_,createFloat16MatrixTexture:()=>qN,createFloat16PackedMatrixTexture:()=>YN,createFloat32MatrixTexture:()=>jN,createIndexBuffer:()=>HN,createPackedMatrixTexture:()=>XN,createUnsignedBytesMatrixTexture:()=>KN,createVertexBuffer:()=>GN,createVertexShader:()=>UN,downloadByteEncodedFloatMatrixFromOutputTexture:()=>n_,downloadFloat32MatrixFromBuffer:()=>t_,downloadMatrixFromPackedOutputTexture:()=>s_,downloadPackedMatrixFromBuffer:()=>r_,getInternalFormatForFloat16MatrixTexture:()=>nk,getInternalFormatForFloat16PackedMatrixTexture:()=>ak,getInternalFormatForFloat32MatrixTexture:()=>tk,getInternalFormatForPackedMatrixTexture:()=>sk,getInternalFormatForUnsignedBytesMatrixTexture:()=>rk,uploadDenseMatrixToTexture:()=>ZN,uploadPixelDataToTexture:()=>JN});function UN(e){let t=$n(),n=`${t.version}
2021-09-11 17:11:38 +02:00
precision highp float;
${t.attribute} vec3 clipSpacePos;
${t.attribute} vec2 uv;
${t.varyingVs} vec2 resultUV;
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
gl_Position = vec4(clipSpacePos, 1);
resultUV = uv;
2021-12-01 21:37:52 +01:00
}`;return mN(e,n)}function GN(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 vN(e,t)}function HN(e){let t=new Uint16Array([0,1,2,2,1,3]);return xN(e,t)}function up(e,t,n,r,s,a){kN(t,n);let o=wN(e),i=e.TEXTURE_2D;return ge(e,()=>e.bindTexture(i,o)),ge(e,()=>e.texParameteri(i,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)),ge(e,()=>e.texParameteri(i,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)),ge(e,()=>e.texParameteri(i,e.TEXTURE_MIN_FILTER,e.NEAREST)),ge(e,()=>e.texParameteri(i,e.TEXTURE_MAG_FILTER,e.NEAREST)),X().getNumber("WEBGL_VERSION")===1?ge(e,()=>e.texImage2D(i,0,r,t,n,0,s,a,null)):ge(e,()=>e.texStorage2D(i,1,r,t,n)),ge(e,()=>e.bindTexture(e.TEXTURE_2D,null)),o}function tk(e){return e.internalFormatFloat}function jN(e,t,n,r){let[s,a]=sp(t,n);return up(e,s,a,tk(r),r.textureFormatFloat,e.FLOAT)}function nk(e){return e.internalFormatHalfFloat}function qN(e,t,n,r){let[s,a]=sp(t,n);return up(e,s,a,nk(r),r.textureFormatFloat,r.textureTypeHalfFloat)}function rk(e){return e.downloadTextureFormat}function KN(e,t,n,r){let[s,a]=sp(t,n);return up(e,s,a,rk(r),e.RGBA,e.UNSIGNED_BYTE)}function sk(e){return e.internalFormatPackedFloat}function XN(e,t,n,r){let[s,a]=Uc(t,n);return up(e,s,a,sk(r),e.RGBA,e.FLOAT)}function ak(e){return e.internalFormatPackedHalfFloat}function YN(e,t,n,r){let[s,a]=Uc(t,n);return up(e,s,a,ak(r),e.RGBA,r.textureTypeHalfFloat)}function QN(e,t,n){let r=0,s=3*4,a=3*4+2*4;return ge(e,()=>e.bindBuffer(e.ARRAY_BUFFER,n)),Kw(e,t,"clipSpacePos",n,3,a,r)&&Kw(e,t,"uv",n,2,a,s)}function ZN(e,t,n,r,s,a){ge(e,()=>e.bindTexture(e.TEXTURE_2D,t));let o,i,u;s instanceof Uint8Array?(o=new Uint8Array(n*r*4),i=e.UNSIGNED_BYTE,u=e.RGBA):(o=new Float32Array(n*r*4),i=e.FLOAT,u=a.internalFormatPackedFloat),o.set(s),X().getNumber("WEBGL_VERSION")===2?ge(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,e.RGBA,i,o)):ge(e,()=>e.texImage2D(e.TEXTURE_2D,0,u,n,r,0,e.RGBA,i,o)),ge(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function JN(e,t,n){ge(e,()=>e.bindTexture(e.TEXTURE_2D,t)),n.data instanceof Uint8Array?X().getNumber("WEBGL_VERSION")===2?(ge(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,n.width,n.height,e.RGBA,e.UNSIGNED_BYTE,n.data)),e.flush()):ge(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,n.width,n.height,0,e.RGBA,e.UNSIGNED_BYTE,n.data)):X().getNumber("WEBGL_VERSION")===2?(ge(e,()=>e.texSubImage2D(e.TEXTURE_2D,0,0,0,e.RGBA,e.UNSIGNED_BYTE,n)),e.flush()):ge(e,()=>e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)),ge(e,()=>e.bindTexture(e.TEXTURE_2D,null))}function e_(e,t,n,r){let s=e.createBuffer();ge(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,s));let i=4*4*t*n;return ge(e,()=>e.bufferData(e.PIXEL_PACK_BUFFER,i,e.STREAM_READ)),ge(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,0)),ge(e,()=>e.bindBuffer(e.PIXEL_PACK_BUFFER,null)),s}function t_(e,t,n){let r=e,s=new Float32Array(n);return r.bindBuffer(r.PIXEL_PACK_BUFFER,t),r.getBufferSubData(r.PIXEL_PACK_BUFFER,0,s),r.bindBuffer(r.PIXEL_PACK_BUFFER,null),s}function n_(e,t,n,r){let[s,a]=sp(t,n),o=4,i=new Uint8Array(dY(t*n,o));return ge(e,()=>e.readPixels(0,0,s,a,r.downloadTextureFormat,e.UNSIGNED_BYTE,i)),new Float32Array(i.buffer)}function r_(e,t,n,r,s,a,o,i){let u=e,l=new Float32Array(pY(a,o));return u.bindBuffer(u.PIXEL_PACK_BUFFER,t),u.getBufferSubData(u.PIXEL_PACK_BUFFER,0,l),u.bindBuffer(u.PIXEL_PACK_BUFFER,null),l}function s_(e,t,n){let r=new Float32Array(t*n*4);return ge(e,()=>e.readPixels(0,0,n,t,e.RGBA,e.FLOAT,r)),r}var a_=class{constructor(e){this.outputTexture=null,this.program=null,this.disposed=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[];let t=X().getNumber("WEBGL_VERSION");e!=null?(this.gl=e,pN(t,e)):this.gl=ys(t);let n="WEBGL_color_buffer_float",r="EXT_color_buffer_half_float";if(X().getNumber("WEBGL_VERSION")===1){let s="OES_texture_float",a="OES_texture_half_float";if(this.textureFloatExtension=ap(this.gl,s),kr(this.gl,a))this.textureHalfFloatExtension=ap(this.gl,a);else if(X().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.colorB
2021-09-11 17:11:38 +02:00
void main() {
setOutput(vec4(getA(), 0., 0., 0.));
}
2021-12-01 21:37:52 +01:00
`;else{let t=Fn("rc",this.rank),n=gt(this.rank),r=this.getOutOfBoundsCondition(t),s=this.getSetup(t),a=this.getOutput(t);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
2021-12-01 21:37:52 +01:00
${n} rc = getOutputCoords();
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
if(${r}) {
2021-09-11 17:11:38 +02:00
setOutput(vec4(0));
} else {
2021-12-01 21:37:52 +01:00
${s}
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
setOutput(vec4(${a}));
2021-04-01 19:39:54 +02:00
}
}
2021-12-01 21:37:52 +01:00
`}}getSourceCoordsArr(e){let t=[];for(let n=0;n<=1;n++)for(let r=0;r<=1;r++){let s=`${n===0?"r":"rp1"}, ${r===0?"c":"cp1"}`;for(let a=2;a<this.rank;a++)s=`${e[e.length-1-a]},`+s;t.push(s)}return t}getOutOfBoundsCondition(e){if(this.rank===1)return`rc > ${this.enableShapeUniforms?"outShape":this.outputShape[0]}`;let t="";for(let n=this.rank-2;n<this.rank;n++)t+=`${e[n]} >= ${this.enableShapeUniforms?`outShape[${n}]`:this.outputShape[n]}`,n<this.rank-1&&(t+="||");return t}getSetup(e){if(this.rank===1)return"";let t=e.slice(-2),n=this.enableShapeUniforms?`outShape[${this.rank} - 1]`:this.outputShape[this.rank-1],r=this.enableShapeUniforms?`outShape[${this.rank} - 2]`:this.outputShape[this.rank-2];return`
int r = ${t[0]};
int c = ${t[1]};
int rp1 = r + 1;
int cp1 = c + 1;
bool cEdge = cp1 >= ${n};
bool rEdge = rp1 >= ${r};
`}getOutput(e){let t=this.getSourceCoordsArr(e);return this.rank===1?`getA(rc),
rc + 1 >= ${this.enableShapeUniforms?"outShape":this.outputShape[0]} ? 0. : getA(rc + 1),
0, 0`:`getA(${t[0]}),
cEdge ? 0. : getA(${t[1]}),
rEdge ? 0. : getA(${t[2]}),
rEdge || cEdge ? 0. : getA(${t[3]})`}},l_=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec3"}],this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length);let n="";for(let r=0;r<4;r++){let s="thisRC = rc;";r%2==1&&(s+="thisRC.z += 1;"),r>1&&(s+="thisRC.y += 1;"),n+=`
2021-09-11 17:11:38 +02:00
${s}
${r>0?"if(thisRC.y < rows && thisRC.z < cols){":""}
int flatIndex = getFlatIndex(thisRC);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);
vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
result[${r}] =
getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);
${r>0?"}":""}
`}this.userCode=`
2021-12-01 21:37:52 +01:00
${sQ(t,this.enableShapeUniforms)}
${this.enableShapeUniforms?Jw():Zw(e)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 rc = getOutputCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 thisRC;
int rows = ${this.enableShapeUniforms?"outShape[1]":e[1]};
int cols = ${this.enableShapeUniforms?"outShape[2]":e[2]};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${n}
2021-04-01 19:39:54 +02:00
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}};function sQ(e,t){return`
2021-09-11 17:11:38 +02:00
ivec3 inputCoordsFromReshapedOutCoords(int index) {
2021-12-01 21:37:52 +01:00
${t?CY(["r","c","d"],"inputShape"):su(["r","c","d"],e)}
2021-09-11 17:11:38 +02:00
return ivec3(r, c, d);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}var aQ=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 r=p_(t,n),s=h_(e,r,n);s in this.freeTextures||(this.freeTextures[s]=[]),s in this.usedTextures||(this.usedTextures[s]=[]);let a=d_(e,r,this.gpgpu.gl,this.gpgpu.textureConfig,n);if(this.freeTextures[s].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=a,this.log();let i=this.freeTextures[s].shift();return this.usedTextures[s].push(i),i}let o;return r===pn.PACKED_2X2_FLOAT32?o=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):r===pn.PACKED_2X2_FLOAT16?o=this.gpgpu.createFloat16PackedMatrixTexture(e[0],e[1]):r===pn.UNPACKED_FLOAT32?o=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):r===pn.UNPACKED_FLOAT16?o=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):r===pn.PACKED_4X1_UNSIGNED_BYTE&&(o=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[s].push(o),this.numUsedTextures++,this._numBytesAllocated+=a,this.log(),o}releaseTexture(e,t,n,r){if(this.freeTextures==null)return;let s=p_(n,r),a=h_(t,s,r);a in this.freeTextures||(this.freeTextures[a]=[]);let o=d_(t,s,this.gpgpu.gl,this.gpgpu.textureConfig,r),i=X().get("WEBGL_DELETE_TEXTURE_THRESHOLD");i!==-1&&this._numBytesAllocated>i?(this.gpgpu.deleteMatrixTexture(e),this._numBytesAllocated-=o):(this.freeTextures[a].push(e),this.numFreeTextures++,this._numBytesFree+=o),this.numUsedTextures--;let u=this.usedTextures[a],l=u.indexOf(e);if(l<0)throw new Error("Cannot release a texture that was never provided by this texture manager");u.splice(l,1),this.log()}log(){if(!this.logEnabled)return;let e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",`${this.numFreeTextures} / ${this.numUsedTextures}`,`(${e})`);let t=this._numBytesFree/this._numBytesAllocated;console.log(`Bytes allocated: ${this._numBytesAllocated}`),console.log(`Bytes unused: ${this._numBytesFree} (${Math.round(100*t)}%)`)}get numBytesAllocated(){return this._numBytesAllocated}get numBytesFree(){return this._numBytesFree}getNumUsedTextures(){return this.numUsedTextures}getNumFreeTextures(){return this.numFreeTextures}dispose(){if(this.freeTextures!=null){for(let e in this.freeTextures)this.freeTextures[e].forEach(t=>{this.gpgpu.deleteMatrixTexture(t)});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 oQ(e,t){let n=e;if(t===n.R32F)return 4;if(t===n.R16F)return 2;if(t===n.RGBA32F)return 16;if(t===e.RGBA)return 16;if(t===n.RGBA16F)return 8;if(t===n.RGBA8)return 4;throw new Error(`Unknown internal format ${t}`)}function d_(e,t,n,r,s){let a=iQ(t,r),o;if(s){let[u,l]=Uc(e[0],e[1]);o=u*l}else{let[u,l]=sp(e[0],e[1]);o=u*l}let i=oQ(n,a);return o*i}function iQ(e,t){switch(e){case pn.PACKED_2X2_FLOAT32:return sk(t);case pn.PACKED_2X2_FLOAT16:return ak(t);case pn.UNPACKED_FLOAT32:return tk(t);case pn.UNPACKED_FLOAT16:return nk(t);case pn.PACKED_4X1_UNSIGNED_BYTE:return rk(t);default:throw new Error(`Unknown physical texture type ${e}`)}}function uQ(e){return X().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?e?pn.PACKED_2X2_FLOAT32:pn.UNPACKED_FLOAT32:e?pn.PACKED_2X2_FLOAT16:pn.UNPACKED_FLOAT16}function p_(e,t){if(e===wr.UPLOAD)return pn.PACKED_2X2_FLOAT32;if(e===wr.RENDER||e==null)return uQ(t);if(e===wr.DOWNLOAD||e===wr.PIXELS)return pn.PACKED_4X1_UNSIGNED_BYTE;throw new Error(`Unknown logical texture type ${e}`)}function h_(e,t,n){return`${e[0]}_${e[1]}_${t}_${n}`}var To=class{constructor(e,t){this.variableNames=["A"],this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
float unaryOperation(float x) {
${t}
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
float x = getAAtOutCoords();
float y = unaryOperation(x);
2021-09-11 17:11:38 +02:00
setOutput(y);
}
2021-12-01 21:37:52 +01:00
`}},ts="if (isnan(x)) return x;",cQ="return x;",f_="return abs(x);",lQ="return (x >= 0.0) ? x : (exp(x) - 1.0);",dQ=ts+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : x;
2021-12-01 21:37:52 +01:00
`,pQ=ts+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2021-12-01 21:37:52 +01:00
`,_m="return x;",hQ="return 1.0 / (1.0 + exp(-1.0 * x));",fQ="return x;",mQ=`
2021-09-11 17:11:38 +02:00
vec4 result;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,gQ=`
2021-09-11 17:11:38 +02:00
vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,bQ=`
2021-09-11 17:11:38 +02:00
vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,yQ="return 1.0 / (1.0 + exp(-1.0 * x));",Xc=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
vec4 unaryOperation(vec4 x) {
${t}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
vec4 x = getAAtOutCoords();
vec4 y = unaryOperation(x);
setOutput(y);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},vQ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length);let t=e.length,n=Fn("rc",t),r=gt(t),s=nQ(t,n),a=n.slice(-2),o=t<=1?"rc":`vec2(${a.join(",")})`;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${r} rc = getOutputCoords();
vec4 packedInput = getA(${s});
setOutput(getChannel(packedInput, ${o}));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},xQ=Dr.whereImpl,wQ=1e-7,kQ=1e-4,Em={};function IQ(e){return e in Em||(Em[e]={}),Em[e]}var SQ=X().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),CQ=600;function TQ(){return X().global.screen==null?1024:X().global.screen.height*X().global.screen.width*window.devicePixelRatio*CQ/1024/1024}var m_=class extends Mu{constructor(e){super();if(this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.lastGlFlushTime=0,this.warnedAboutMemory=!1,this.pendingDeletes=0,this.disposed=!1,!X().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");if(e==null){let t=ys(X().getNumber("WEBGL_VERSION"));this.binaryCache=IQ(X().getNumber("WEBGL_VERSION")),this.gpgpu=new a_(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 aQ(this.gpgpu),this.numMBBeforeWarning=TQ(),this.texData=new Gl(this,is())}nextDataId(){return m_.nextDataId++}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}write(e,t,n){if((X().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||X().getBool("DEBUG"))&&this.checkNumericalProblems(e),n==="complex64"&&e!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");let r={id:this.nextDataId()};return this.texData.set(r,{shape:t,dtype:n,values:e,usage:wr.UPLOAD,refCount:1}),r}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,r,s){if(X().getBool("DEBUG")&&this.checkNumericalProblems(t),r==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(e,{shape:n,dtype:r,values:t,usage:wr.UPLOAD,refCount:s})}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}readSync(e){let t=this.texData.get(e),{values:n,dtype:r,complexTensorInfos:s,slice:a,shape:o,isPacked:i}=t;if(a!=null){let d;i?d=new Xc(o,_m):d=new To(o,_m);let p=this.runWebGLProgram(d,[{dataId:e,shape:o,dtype:r}],r),h=this.readSync(p.dataId);return this.disposeIntermediateTensorInfo(p),h}if(n!=null)return this.convertAndCacheOnCPU(e);if(r==="string")return n;let u=this.activeTimers!=null,l;u&&(l=w.now());let c;if(r==="complex64"){let d=this.readSync(s.real.dataId),p=this.readSync(s.imag.dataId);c=N.mergeRealAndImagArrays(d,p)}else c=this.getValuesFromTexture(e);return u&&(this.downloadWaitMs+=w.now()-l),this.convertAndCacheOnCPU(e,c)}async read(e){if(this.pendingRead.has(e)){let h=this.pendingRead.get(e);return new Promise(f=>h.push(f))}let t=this.texData.get(e),{values:n,shape:r,slice:s,dtype:a,complexTensorInfos:o,isPacked:i}=t;if(s!=null){let h;i?h=new Xc(r,_m):h=new To(r,_m);let f=this.runWebGLProgram(h,[{dataId:e,shape:r,dtype:a}],a),m=this.read(f.dataId);return this.disposeIntermediateTensorInfo(f),m}if(n!=null)return this.convertAndCacheOnCPU(e);if(X().getBool("DEBUG")&&!X().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&X().getNumber("WEBGL_VERSION")===2)throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");let u=null,l;if(a!=="complex64"&&X().get("WEBGL_BUFFER_SUPPORTED")){l=this.decode(e);let h=this.texData.get(l.dataId);u=this.gpgpu.createBufferFromTexture(h.texture,...xm(r))}this.pendingRead.set(e,[]),a!=="complex64"&&await this.gpgpu.createAndWaitForFence();let c;if(a==="complex64"){let h=await Promise.all([this.read(o.real.dataId),this.read(o.imag.dataId)]),f=h[0],m=h[1];c=N.mergeRealAndImagArrays(f,m)}else if(u==null)c=this.getValuesFromTexture(e);else{let h=w.sizeFromShape(r);c=this.gpgpu.downloadFloat32MatrixFromBuffer(u,h)}if(l!=null&&this.disposeIntermediateTensorInfo(l),u!=null){let h=this.gpgpu.gl;ge(h,()=>h.deleteBuffer(u))}let d=this.convertAndCacheOnCPU(e,c),p=this.pendingRead.get(e);return this.pendingRead.delete(e),p.forEach(h=>h(d)),this.pendingDisposal.has(e)&&(this.pendingDisposal.delete(e),this.dis
2021-09-11 17:11:38 +02:00
if (isnan(a)) return a;
if (isnan(b)) return b;
2021-12-01 21:37:52 +01:00
`,Yc=class{constructor(e,t,n){this.variableNames=["A","B"],this.outputShape=N.assertAndGetBroadcastShape(t,n),this.enableShapeUniforms=qn(this.outputShape.length),this.userCode=`
2021-09-11 17:11:38 +02:00
float binaryOperation(float a, float b) {
${e}
2021-04-01 19:39:54 +02:00
}
void main() {
2021-09-11 17:11:38 +02:00
float a = getAAtOutCoords();
float b = getBAtOutCoords();
setOutput(binaryOperation(a, b));
}
2021-12-01 21:37:52 +01:00
`}},Am=`
2021-09-11 17:11:38 +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-12-01 21:37:52 +01:00
`,cp=class{constructor(e,t,n,r=!1){this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=N.assertAndGetBroadcastShape(t,n);let s=this.outputShape.length;this.enableShapeUniforms=qn(s);let a="";if(r)if(s===0||w.sizeFromShape(this.outputShape)===1)a=`
2021-09-11 17:11:38 +02:00
result.y = 0.;
result.z = 0.;
result.w = 0.;
`;else if(a=`
2021-12-01 21:37:52 +01:00
${gt(s)} coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
`,s===1)this.enableShapeUniforms?a+=`
result.y = (coords + 1) >= outShape ? 0. : result.y;
result.z = 0.;
result.w = 0.;
`:a+=`
result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
result.z = 0.;
result.w = 0.;
2021-12-01 21:37:52 +01:00
`;else{let i=Fn("coords",s);this.enableShapeUniforms?a+=`
2021-09-11 17:11:38 +02:00
bool nextRowOutOfBounds =
(${i[s-2]} + 1) >= outShape[${s} - 2];
bool nextColOutOfBounds =
(${i[s-1]} + 1) >= outShape[${s} - 1];
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`:a+=`
bool nextRowOutOfBounds =
(${i[s-2]} + 1) >= ${this.outputShape[s-2]};
bool nextColOutOfBounds =
(${i[s-1]} + 1) >= ${this.outputShape[s-1]};
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`}this.userCode=`
vec4 binaryOperation(vec4 a, vec4 b) {
${e}
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
vec4 a = getAAtOutCoords();
vec4 b = getBAtOutCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = binaryOperation(a, b);
${a}
2021-04-01 19:39:54 +02:00
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}};function cr(e){let{inputs:t,backend:n}=e,{x:r}=t;return n.incRef(r.dataId),{dataId:r.dataId,shape:r.shape,dtype:r.dtype}}var AQ={kernelName:_a,backendName:"webgl",kernelFunc:cr};function No(e){let{inputs:t,backend:n}=e,{real:r,imag:s}=t,a=n.makeTensorInfo(r.shape,"complex64"),o=n.texData.get(a.dataId),i=cr({inputs:{x:r},backend:n}),u=cr({inputs:{x:s},backend:n});return o.complexTensorInfos={real:i,imag:u},a}var $Q={kernelName:Kl,backendName:"webgl",kernelFunc:No},y_="return (a < 0.) ? b * a : a;",v_=`
2021-09-11 17:11:38 +02:00
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
2021-12-01 21:37:52 +01:00
`;function FQ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{alpha:a}=r,o=n.makeTensorInfo([],"float32",w.createScalarValue(a,"float32")),i=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new cp(v_,s.shape,o.shape):new Yc(y_,s.shape,o.shape),u=n.runWebGLProgram(i,[s,o],"float32");return n.disposeIntermediateTensorInfo(o),u}var DQ={kernelName:ti,backendName:"webgl",kernelFunc:FQ},x_="return (a < 0.) ? b * a : a;",w_=`
2021-09-11 17:11:38 +02:00
vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
2021-12-01 21:37:52 +01:00
`;function RQ(e){let{inputs:t,backend:n}=e,{x:r,alpha:s}=t,a=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new cp(w_,r.shape,s.shape):new Yc(x_,r.shape,s.shape);return n.runWebGLProgram(a,[r,s],"float32")}var PQ={kernelName:za,backendName:"webgl",kernelFunc:RQ},k_="if (isnan(x)) return x;",OQ=`
2021-09-11 17:11:38 +02:00
if (isnan(a)) return a;
if (isnan(b)) return b;
2021-12-01 21:37:52 +01:00
`,MQ=`
2021-09-11 17:11:38 +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-12-01 21:37:52 +01:00
`;function Ze({opSnippet:e,packedOpSnippet:t,cpuKernelImpl:n,dtype:r}){return({inputs:s,backend:a})=>{let{x:o}=s,i=a,u=r||o.dtype;if(i.shouldExecuteOnCPU([o])&&n!=null){let d=i.texData.get(o.dataId),p=n(d.values,u);return i.makeTensorInfo(o.shape,u,p)}let l=X().getBool("WEBGL_PACK_UNARY_OPERATIONS")&&t!=null,c;return l?c=new Xc(o.shape,t):c=new To(o.shape,e),i.runWebGLProgram(c,[o],u)}}function hn({opSnippet:e,packedOpSnippet:t,checkOutOfBounds:n=!1,supportsComplex:r=!1,cpuKernelImpl:s,dtype:a}){return({inputs:o,backend:i})=>{let{a:u,b:l}=o,c=i;if(r&&u.dtype==="complex64"){let f=c.texData.get(u.dataId),m=c.texData.get(l.dataId),[g,b]=[[f.complexTensorInfos.real,m.complexTensorInfos.real],[f.complexTensorInfos.imag,m.complexTensorInfos.imag]].map(v=>{let[x,k]=v,T={dataId:x.dataId,dtype:x.dtype,shape:u.shape},C={dataId:k.dataId,dtype:k.dtype,shape:l.shape},E=new Yc(e,u.shape,l.shape);return c.runWebGLProgram(E,[T,C],In(x.dtype,k.dtype))}),y=No({inputs:{real:g,imag:b},backend:c});return c.disposeIntermediateTensorInfo(g),c.disposeIntermediateTensorInfo(b),y}let d=a||In(u.dtype,l.dtype);if((u.dtype==="string"||l.dtype==="string"||c.shouldExecuteOnCPU([u,l]))&&s!=null){let f=c.texData.get(u.dataId).values,m=c.texData.get(l.dataId).values,g=u.dtype==="string"?N.fromUint8ToStringArray(f):f,b=u.dtype==="string"?N.fromUint8ToStringArray(m):m,[y,v]=s(u.shape,l.shape,g,b,d),x=c.makeTensorInfo(v,d),k=c.texData.get(x.dataId);return k.values=y,x}let p=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")&&t!=null,h;return p?h=new cp(t,u.shape,l.shape,n):h=new Yc(e,u.shape,l.shape),c.runWebGLProgram(h,[u,l],d)}}function $m(e,t=!1){if(e==="linear")return t?fQ:cQ;if(e==="relu")return t?gQ:dQ;if(e==="elu")return t?mQ:lQ;if(e==="relu6")return t?bQ:pQ;if(e==="prelu")return t?w_:x_;if(e==="leakyrelu")return t?v_:y_;if(e==="sigmoid")return t?yQ:hQ;throw new Error(`Activation ${e} has not been implemented for the WebGL backend.`)}var I_=class{constructor(e,t,n,r=!1,s=!1,a=!1,o=null,i=!1,u=!1){this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=n,this.enableShapeUniforms=qn(this.outputShape.length);let l=r?e[1]:e[2],c=Math.ceil(l/2),d=r?"i * 2, rc.y":"rc.y, i * 2",p=s?"rc.z, i * 2":"i * 2, rc.z",h=r?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],f=s?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],m="",g="";o&&(i?m=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
${o}
2021-12-01 21:37:52 +01:00
}`:u?m=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getLeakyreluAlphaAtOutCoords();
${o}
}`:m=`vec4 activation(vec4 x) {
${o}
2021-12-01 21:37:52 +01:00
}`,g="result = activation(result);");let b=a?"result += getBiasAtOutCoords();":"";a&&this.variableNames.push("bias"),i&&this.variableNames.push("preluActivationWeights"),u&&this.variableNames.push("leakyreluAlpha");let y="rc.x",v="rc.x";e[0]<t[0]?y=`int(min(float(rc.x), ${e[0]-1}.))`:t[0]<e[0]&&(v=`int(min(float(rc.x), ${t[0]-1}.))`),this.userCode=`
2021-09-11 17:11:38 +02:00
${m}
// Don't use uniform for sharedDimensionPacked for performance.
2021-12-01 21:37:52 +01:00
const float sharedDimension = ${c}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 dot2x2ARowBCol(ivec3 rc) {
vec4 result = vec4(0);
2021-12-01 21:37:52 +01:00
for (int i = 0; i < ${c}; i++) {
int batchA = ${y};
int batchB = ${v};
2021-09-11 17:11:38 +02:00
vec4 a = getMatrixA(batchA, ${d});
vec4 b = getMatrixB(batchB, ${p});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// These swizzled products need to be separately added.
// See: https://github.com/tensorflow/tfjs/issues/1735
result += (${h[0]} * ${f[0]});
result += (${h[1]} * ${f[1]});
}
return result;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec3 rc = getOutputCoords();
vec4 result = dot2x2ARowBCol(rc);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${b}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${g}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}},S_={REAL:"return areal * breal - aimag * bimag;",IMAG:"return areal * bimag + aimag * breal;"},C_=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=N.assertAndGetBroadcastShape(t,n),this.userCode=`
2021-09-11 17:11:38 +02:00
float binaryOpComplex(
float areal, float aimag, float breal, float bimag) {
${e}
}
void main() {
float areal = getARealAtOutCoords();
float aimag = getAImagAtOutCoords();
float breal = getBRealAtOutCoords();
float bimag = getBImagAtOutCoords();
setOutput(binaryOpComplex(areal, aimag, breal, bimag));
}
2021-12-01 21:37:52 +01:00
`}},T_="return a * b;";function uk(e){let{inputs:t,backend:n}=e,{a:r,b:s}=t,a=N.upcastType(r.dtype,s.dtype);if(r.dtype==="complex64"){let i=n.texData.get(r.dataId),u=n.texData.get(s.dataId),l=new C_(S_.REAL,r.shape,s.shape),c=new C_(S_.IMAG,r.shape,s.shape),d=[{dataId:i.complexTensorInfos.real.dataId,dtype:i.complexTensorInfos.real.dtype,shape:r.shape},{dataId:i.complexTensorInfos.imag.dataId,dtype:i.complexTensorInfos.imag.dtype,shape:r.shape},{dataId:u.complexTensorInfos.real.dataId,dtype:u.complexTensorInfos.real.dtype,shape:s.shape},{dataId:u.complexTensorInfos.imag.dataId,dtype:u.complexTensorInfos.imag.dtype,shape:s.shape}],p=n.runWebGLProgram(l,d,"float32"),h=n.runWebGLProgram(c,d,"float32"),f=No({inputs:{real:p,imag:h},backend:n});return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(h),f}if(n.shouldExecuteOnCPU([r,s])){let i=n.texData.get(r.dataId),u=n.texData.get(s.dataId),[l,c]=M9(r.shape,s.shape,i.values,u.values,a),d=n.makeTensorInfo(c,a),p=n.texData.get(d.dataId);return p.values=l,d}let o;return X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?o=new cp(T_,r.shape,s.shape):o=new Yc(T_,r.shape,s.shape),n.runWebGLProgram(o,[r,s],a)}var LQ={kernelName:Ma,backendName:"webgl",kernelFunc:uk};function BQ(e,t,n){let r=[nu(e.shape),...ru(e.shape)],s={dtype:e.dtype,shape:r,dataId:e.dataId},a=[nu(t),...ru(t)],o=new l_(a,r),i=!0,u=[r],l=n.runWebGLProgram(o,[s],e.dtype,u,i);return{dataId:l.dataId,shape:t,dtype:l.dtype}}function fe(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{shape:a}=r,o=n,i=w.sizeFromShape(s.shape),u=w.inferFromImplicitShape(a,i),l=w.sizeFromShape(u);w.assert(i===l,()=>`The new shape (${u}) has ${l} elements and the old shape (${s.shape}) has ${i} elements. The new shape and old shape must have the same number of elements.`);let c=o.texData.get(s.dataId);return c.isPacked&&!ip(s.shape,u)&&!(c.texture!==null&&ip(c.shape,u))?BQ(s,u,o):(o.incRef(s.dataId),{dataId:s.dataId,shape:u,dtype:s.dtype})}var zQ={kernelName:hi,backendName:"webgl",kernelFunc:fe},N_=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:r,inSize:s,outSize:a}=e;this.outputShape=[r,a];let o=Math.floor(n/4)*4,i=n%4,u="sumValue += dot(values, ones);";if(t!=null){let c=1/t;u=`sumValue += dot(values * ${w.isInt(c)?c.toPrecision(2):c}, ones);`}let l="";s%n>0&&(l=`
2021-09-11 17:11:38 +02:00
if (inIdx < 0 || inIdx >= ${s}) {
return 0.0;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`),this.userCode=`
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
2021-10-22 15:06:43 +02:00
${l}
2021-09-11 17:11:38 +02:00
return getX(batch, inIdx);
2021-04-01 19:39:54 +02:00
}
void main() {
2021-09-11 17:11:38 +02:00
ivec2 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
int batch = coords[0];
2021-09-11 17:11:38 +02:00
int outIdx = coords[1];
int inOffset = outIdx * ${n};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float sumValue = 0.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i = 0; i < ${o}; i += 4) {
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
${u}
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + ${o};
if (${i===1}) {
vec4 values = vec4(getValue(batch, inIdx), 0.0, 0.0, 0.0);
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
${u}
2021-09-11 17:11:38 +02:00
} else if (${i===2}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1), 0.0, 0.0);
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
${u}
2021-09-11 17:11:38 +02:00
} else if (${i===3}) {
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2), 0.0);
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
${u}
2021-09-11 17:11:38 +02:00
}
setOutput(sumValue);
}
2021-12-01 21:37:52 +01:00
`}},WQ=class{constructor(e,t){this.variableNames=["x"];let{windowSize:n,batchSize:r,inSize:s,outSize:a}=e;this.outputShape=[r,a];let o="0.0",i="";t==="prod"?o="1.0":t==="min"?(o="1.0 / 1e-20",i="min"):t==="max"&&(o="-1.0 / 1e-20",i="max");let u=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="sum"?u="sumValue":t==="prod"?u="prodValue":t==="all"?u="allValue":t==="any"&&(u="anyValue");let l=Math.floor(n/4)*4,c=n%4,d=`
2021-09-11 17:11:38 +02:00
if (${t==="sum"}) {
sumValue += dot(values, ones);
} else if (${t==="prod"}) {
vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);
prodValue *= tmp[0] * tmp[1];
} else {
minMaxValue = ${i}(values, minMaxValue);
if (${t==="min"} || ${t==="max"}) {
minMaxValue = ${i}(values, minMaxValue);
bvec4 isNaN = isnan(values);
if (isNaN.r || isNaN.g || isNaN.b || isNaN.a) {
minMaxValue = vec4(NAN);
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
`,p="vec4";t==="all"?(o="1.0",d=`
bool reducedAllValue = all(values);
float floatedReducedAllValue = float(reducedAllValue);
allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
`,p="bvec4"):t==="any"&&(o="0.0",d=`
bool reducedAnyValue = any(values);
float floatedReducedAnyValue = float(reducedAnyValue);
anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);
`,p="bvec4");let h="";s%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${s}) {
return initializationValue;
}
`),this.userCode=`
const float initializationValue = ${o};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float getValue(int batch, int inIdx) {
${h}
return getX(batch, inIdx);
2021-04-01 19:39:54 +02:00
}
void main() {
2021-09-11 17:11:38 +02:00
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${n};
vec4 minMaxValue = vec4(${o});
float prodValue = 1.0;
float sumValue = 0.0;
float allValue = 1.0;
float anyValue = 0.0;
2021-10-22 15:06:43 +02:00
for (int i = 0; i < ${l}; i += 4) {
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + i;
${p} values = ${p}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${d}
}
2021-04-01 19:39:54 +02:00
2021-10-22 15:06:43 +02:00
int inIdx = inOffset + ${l};
2021-12-01 21:37:52 +01:00
if (${c===1}) {
2021-09-11 17:11:38 +02:00
${p} values = ${p}(
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${d}
2021-12-01 21:37:52 +01:00
} else if (${c===2}) {
2021-09-11 17:11:38 +02:00
${p} values = ${p}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${d}
2021-12-01 21:37:52 +01:00
} else if (${c===3}) {
2021-09-11 17:11:38 +02:00
${p} values = ${p}(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${d}
}
2021-12-01 21:37:52 +01:00
setOutput(${u});
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function VQ(e){let t=[];for(;t.length===0||t[t.length-1].outSize!==1;){let n=t.length?t[t.length-1].outSize:e[1],r=N.computeOptimalWindowSize(n);t.push({inSize:n,windowSize:r,outSize:Math.ceil(n/r)})}return t}function ou(e,t,n,r){let s=VQ(e.shape),a=e;for(let o=0;o<s.length;o++){let{inSize:i,windowSize:u,outSize:l}=s[o],c,d;n==="mean"?c=o===0?new N_({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:l},i):new N_({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:l}):c=new WQ({windowSize:u,inSize:i,batchSize:e.shape[0],outSize:l},n),d=a,a=r.runWebGLProgram(c,[a],t),d.dataId!==e.dataId&&r.disposeIntermediateTensorInfo(d)}return a}var UQ=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let a=0;a<n.length;a++)n[a]=e[t[a]];this.outputShape=n,this.rank=n.length;let r=gt(this.rank),s=GQ(t);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${r} resRC = getOutputCoords();
setOutput(getA(${s}));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function GQ(e){let t=e.length;if(t>6)throw Error(`Transpose for rank ${t} is not yet supported`);let n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(t);for(let s=0;s<e.length;s++)r[e[s]]=n[s];return r.join()}var HQ=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0;let n=new Array(e.length);for(let l=0;l<n.length;l++)n[l]=e[t[l]];if(this.outputShape=n,this.rank=n.length,this.rank>6)throw Error(`Packed transpose for rank ${this.rank} is not yet supported.`);let r=gt(this.rank),s=c_("rc",this.rank),a=new Array(this.rank);for(let l=0;l<t.length;l++)a[t[l]]=s[l];let o=`vec2(${a.slice(-2).join()})`,i=`++${s[this.rank-1]} < ${n[this.rank-1]}`,u=`getChannel(getA(${a.join()}), ${o})`;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${r} rc = getOutputCoords();
vec4 result = vec4(0.);
2021-12-01 21:37:52 +01:00
result[0] = ${u};
2021-09-11 17:11:38 +02:00
if(${i}) {
2021-12-01 21:37:52 +01:00
result[1] = ${u};
}
2021-09-11 17:11:38 +02:00
--${s[this.rank-1]};
if(++${s[this.rank-2]} < ${n[this.rank-2]}) {
2021-12-01 21:37:52 +01:00
result[2] = ${u};
2021-09-11 17:11:38 +02:00
if(${i}) {
2021-12-01 21:37:52 +01:00
result[3] = ${u};
}
}
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}};function Fm(e,t,n){let r=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new HQ(e.shape,t):new UQ(e.shape,t);return n.runWebGLProgram(r,[e],e.dtype)}function jQ(e,t,n,r){let s=t,a=e.shape.length,o=w.parseAxisParam(s,e.shape),i=o,u=N.getAxesPermutation(i,a),l=u!=null,c=e;l&&(c=Fm(e,u,r),i=N.getInnerMostAxes(i.length,a)),N.assertAxesAreInnerMostDims("sum",i,a);let[d,p]=N.computeOutAndReduceShapes(c.shape,i),h=d;n&&(h=N.expandShapeToKeepDim(d,o));let f=w.sizeFromShape(p),g=w.sizeFromShape(e.shape)/f,b=fe({inputs:{x:c},attrs:{shape:[g,f]},backend:r}),y=yd(e.dtype),v=ou(b,y,"sum",r),x=fe({inputs:{x:v},attrs:{shape:h},backend:r});return r.disposeIntermediateTensorInfo(b),r.disposeIntermediateTensorInfo(v),l&&r.disposeIntermediateTensorInfo(c),x}function Dm(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r;return jQ(s,a,o,n)}var qQ={kernelName:Ka,backendName:"webgl",kernelFunc:Dm};function Dn(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{perm:a}=r,o=n,i=s.shape.length,u=new Array(i);for(let c=0;c<u.length;c++)u[c]=s.shape[a[c]];let l;if(o.shouldExecuteOnCPU([s])){let d=o.texData.get(s.dataId).values,p=ok(d,s.shape,s.dtype,a,u);l=o.makeTensorInfo(u,s.dtype);let h=o.texData.get(l.dataId);h.values=p}else l=Fm(s,a,o);return l}var KQ={kernelName:Ja,backendName:"webgl",kernelFunc:Dn},__=1e3;function Rm({a:e,b:t,transposeA:n,transposeB:r,backend:s,bias:a=null,preluActivationWeights:o=null,leakyreluAlpha:i=0,activation:u=null}){let l=e.shape.length,c=t.shape.length,d=n?e.shape[l-2]:e.shape[l-1],p=r?t.shape[c-1]:t.shape[c-2],h=n?e.shape[l-1]:e.shape[l-2],f=r?t.shape[c-2]:t.shape[c-1],m=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(m),y=w.sizeFromShape(g),x=Ri.assertAndGetBroadcastShape(e.shape.slice(0,-2),t.shape.slice(0,-2)).concat([h,f]);w.assert(d===p,()=>`Error in matMul: inner shapes (${d}) and (${p}) of Tensors with shapes ${e.shape} and ${t.shape} and transposeA=${n} and transposeB=${r} must match.`);let k=n?[b,d,h]:[b,h,d],T=r?[y,f,p]:[y,p,f],C=fe({inputs:{x:e},backend:s,attrs:{shape:k}}),E=fe({inputs:{x:t},backend:s,attrs:{shape:T}}),F=[C,E],O=Math.max(b,y),D=n?C.shape[1]:C.shape[2],R=a!=null,_=o!=null,L=u==="leakyrelu",U=u!=null?$m(u,!0):null,j=R||_||L||U!=null,K;if((h===1||f===1)&&D>__&&j===!1){let Q=C,ee=E;n&&(Q=Dn({inputs:{x:C},backend:s,attrs:{perm:[0,2,1]}}),F.push(Q)),r&&(ee=Dn({inputs:{x:E},backend:s,attrs:{perm:[0,2,1]}}),F.push(ee));let re=f!==1,se=f===1,ne=Q;re&&(ne=fe({inputs:{x:Q},backend:s,attrs:{shape:[O,D,1]}}),F.push(ne));let ie=f===1?2:1,te=ee;se&&(te=fe({inputs:{x:ee},backend:s,attrs:{shape:[O,1,D]}}),F.push(te));let pe=uk({inputs:{a:ne,b:te},backend:s});K=Dm({inputs:{x:pe},backend:s,attrs:{axis:ie,keepDims:!0}}),F.push(pe)}else{let Q=In(e.dtype,t.dtype),ee=new I_(k,T,[O,h,f],n,r,R,U,_,L),re=[C,E];if(a!=null&&re.push(a),_&&re.push(o),L){let se=s.makeTensorInfo([],"float32",w.createScalarValue(i,"float32"));re.push(se),F.push(se)}K=s.runWebGLProgram(ee,re,Q)}let q=fe({inputs:{x:K},backend:s,attrs:{shape:x}});F.push(K);for(let Q of F)s.disposeIntermediateTensorInfo(Q);return q}function XQ(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a,bias:o,preluActivationWeights:i}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:d}=r;return Rm({a:s,b:a,transposeA:u,transposeB:l,backend:n,bias:o,preluActivationWeights:i,leakyreluAlpha:d,activation:c})}var YQ={kernelName:to,backendName:"webgl",kernelFunc:XQ},E_="return abs(x);";function QQ(e){let{inputs:t,backend:n}=e,{x:r}=t;if(n.shouldExecuteOnCPU([r])&&r.dtype!=="complex64"){let a=n.texData.get(r.dataId),o=i_(a.values);return n.makeTensorInfo(r.shape,r.dtype,o)}let s;return X().getBool("WEBGL_PACK_UNARY_OPERATIONS")?s=new Xc(r.shape,E_):s=new To(r.shape,E_),n.runWebGLProgram(s,[r],r.dtype)}var ZQ={kernelName:Vo,backendName:"webgl",kernelFunc:QQ},JQ=ts+`
2021-09-11 17:11:38 +02:00
if (abs(x) > 1.) {
return NAN;
}
2021-09-11 17:11:38 +02:00
return acos(x);
2021-12-01 21:37:52 +01:00
`,eZ=Ze({opSnippet:JQ}),tZ={kernelName:Vu,backendName:"webgl",kernelFunc:eZ},nZ=ts+`
2021-09-11 17:11:38 +02:00
if (x < 1.0) return NAN;
2021-12-01 21:37:52 +01:00
return log(x + sqrt(x * x - 1.0));`,rZ=Ze({opSnippet:nZ}),sZ={kernelName:Uu,backendName:"webgl",kernelFunc:rZ},A_="return a + b;",aZ=hn({opSnippet:A_,packedOpSnippet:A_,supportsComplex:!0,cpuKernelImpl:y9}),oZ={kernelName:_s,backendName:"webgl",kernelFunc:aZ},iZ=class{constructor(e,t){this.outputShape=[],this.outputShape=e,this.variableNames=t.map((s,a)=>`T${a}`);let n=[];this.variableNames.forEach(s=>{n.push(`float v${s} = get${s}AtOutCoords();`)});let r=this.variableNames.map(s=>`v${s}`).join(" + ");this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${n.join(`
`)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float result = ${r};
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},uZ=class{constructor(e,t){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.variableNames=t.map((s,a)=>`T${a}`);let n=[];this.variableNames.forEach(s=>{n.push(`vec4 v${s} = get${s}AtOutCoords();`)});let r=this.variableNames.map(s=>`v${s}`).join(" + ");this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${n.join(`
`)}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = ${r};
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function Pm(e){let{inputs:t,backend:n}=e,r=t;if(r.length===1)return cr({inputs:{x:r[0]},backend:n});if(r.length>X().get("WEBGL_MAX_TEXTURES_IN_SHADER")){let u=Math.floor(r.length/2),l=Pm({inputs:r.slice(0,u),backend:n}),c=Pm({inputs:r.slice(u),backend:n});return Pm({inputs:[l,c],backend:n})}let s=r.map(u=>u.dtype).reduce((u,l)=>In(u,l)),a=r.map(u=>u.shape),i=X().getBool("WEBGL_PACK")?new uZ(r[0].shape,a):new iZ(r[0].shape,a);return n.runWebGLProgram(i,r,s)}var cZ={kernelName:la,backendName:"webgl",kernelFunc:Pm};function lZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=N.getAxesPermutation(l,i),d=s;c!=null&&(d=Dn({inputs:{x:s},backend:n,attrs:{perm:c}}),l=N.getInnerMostAxes(l.length,i)),N.assertAxesAreInnerMostDims("all",l,i);let[p,h]=N.computeOutAndReduceShapes(d.shape,l),f=w.sizeFromShape(h),m=fe({inputs:{x:d},backend:n,attrs:{shape:[-1,f]}}),g=ou(m,m.dtype,"all",n),b;if(o){let y=N.expandShapeToKeepDim(p,u);b=fe({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=fe({inputs:{x:g},backend:n,attrs:{shape:p}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(d),b}var dZ={kernelName:Gu,backendName:"webgl",kernelFunc:lZ};function pZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=N.getAxesPermutation(l,i),d=s;c!=null&&(d=Dn({inputs:{x:s},backend:n,attrs:{perm:c}}),l=N.getInnerMostAxes(l.length,i)),N.assertAxesAreInnerMostDims("any",l,i);let[p,h]=N.computeOutAndReduceShapes(d.shape,l),f=w.sizeFromShape(h),m=fe({inputs:{x:d},backend:n,attrs:{shape:[-1,f]}}),g=ou(m,m.dtype,"any",n),b;if(o){let y=N.expandShapeToKeepDim(p,u);b=fe({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=fe({inputs:{x:g},backend:n,attrs:{shape:p}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(d),b}var hZ={kernelName:Hu,backendName:"webgl",kernelFunc:pZ},fZ=class{constructor(e,t,n){this.variableNames=["A"];let{windowSize:r,batchSize:s,outSize:a}=e;n||this.variableNames.push("bestIndicesA"),this.outputShape=[s,a];let o=t==="max"?">":"<",i=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = outIdx * ${r};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int bestIndex = inOffset;
float bestValue = getA(batch, bestIndex);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i = 0; i < ${r}; i++) {
int inIdx = ${i};
float candidate = getA(batch, inIdx);
if (candidate ${o} bestValue) {
bestValue = candidate;
bestIndex = inIdx;
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(float(bestIndex));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},mZ=class{constructor(e,t,n,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,w.assert(e.length>2,()=>`Packed arg${n.charAt(0).toUpperCase()+n.slice(1)} supports only inputs with rank above 2.`);let s=e[e.length-1],a=Math.ceil(s/t);this.outputShape=e.slice(0,-1),a>1&&this.outputShape.push(a),r||this.variableNames.push("bestIndicesA");let o=this.outputShape,i=o.length,u=gt(i),l=Fn("coords",i),c,d;if(a===1){d=i+1;let C=gt(d);c=`
${C} sourceLocR = ${C}(${l.join()}, 0);
2021-10-22 15:06:43 +02:00
++${l[i-1]};
2021-12-01 21:37:52 +01:00
${C} sourceLocG = ${C}(${l.join()}, 0);
2021-10-22 15:06:43 +02:00
++${l[i-2]};
2021-12-01 21:37:52 +01:00
${C} sourceLocA = ${C}(${l.join()}, 0);
2021-10-22 15:06:43 +02:00
--${l[i-1]};
2021-12-01 21:37:52 +01:00
${C} sourceLocB = ${C}(${l.join()}, 0);
--${l[i-2]};`}else d=i,c=`
${u} sourceLocR = coords;
2021-10-22 15:06:43 +02:00
++${l[i-1]};
2021-12-01 21:37:52 +01:00
${u} sourceLocG = coords;
2021-10-22 15:06:43 +02:00
++${l[i-2]};
2021-12-01 21:37:52 +01:00
${u} sourceLocA = coords;
2021-10-22 15:06:43 +02:00
--${l[i-1]};
2021-12-01 21:37:52 +01:00
${u} sourceLocB = coords;
--${l[i-2]};`;let p=["x","y","z","w","u","v"].slice(0,d),h="."+p[d-1],f=p.map(C=>"int "+C),m=Fn("sourceLocR",d-1).concat("inIdx.r"),g=Fn("sourceLocG",d-1).concat("inIdx.g"),b=Fn("sourceLocB",d-1).concat("inIdx.b"),y=Fn("sourceLocA",d-1).concat("inIdx.a"),v=n==="max"?"greaterThan":"lessThan",x=r?"":`
2021-09-11 17:11:38 +02:00
inIdx = round(vec4(getBestIndicesAChannel(${m.join()}),
getBestIndicesAChannel(${g.join()}),
getBestIndicesAChannel(${b.join()}),
2021-12-01 21:37:52 +01:00
getBestIndicesAChannel(${y.join()})));`,k=`vec4(
2021-09-11 17:11:38 +02:00
getAChannel(${m.join()}),
hasNextCol ? getAChannel(${g.join()}) : 0.,
hasNextRow ? getAChannel(${b.join()}) : 0.,
2021-12-01 21:37:52 +01:00
hasNextRow && hasNextCol ? getAChannel(${y.join()}) : 0.)`,T=r?"":`
2021-09-11 17:11:38 +02:00
float getBestIndicesAChannel(${f.join()}) {
return getChannel(getBestIndicesA(${p.join()}),
vec2(${p.slice(-2).join()}));
}`;this.userCode=`
float getAChannel(${f.join()}) {
return getChannel(getA(${p.join()}),
vec2(${p.slice(-2).join()}));
}
2021-12-01 21:37:52 +01:00
${T}
2021-04-01 19:39:54 +02:00
void main() {
2021-12-01 21:37:52 +01:00
${u} coords = getOutputCoords();
2021-10-22 15:06:43 +02:00
bool hasNextCol = ${l[i-1]} < ${o[i-1]-1};
bool hasNextRow = ${l[i-2]} < ${o[i-2]-1};
2021-12-01 21:37:52 +01:00
${c}
2021-09-11 17:11:38 +02:00
ivec4 srcIdx = ivec4(sourceLocR${h}, sourceLocG${h},
sourceLocB${h}, sourceLocA${h}) * ${t};
ivec4 inIdx = srcIdx;
vec4 bestIndex = vec4(inIdx);
vec4 bestValue = ${k};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int i = 0; i < ${t}; i++) {
inIdx = srcIdx;
${x}
vec4 candidate = ${k};
bvec4 nan = isnan(candidate);
bvec4 replace = bvec4(
2021-12-01 21:37:52 +01:00
vec4(${v}(candidate, bestValue)) * (vec4(1.0) - vec4(nan)));
2021-09-11 17:11:38 +02:00
bestValue = vec4(replace.x ? candidate.x : bestValue.x,
replace.y ? candidate.y : bestValue.y,
replace.z ? candidate.z : bestValue.z,
replace.w ? candidate.w : bestValue.w);
bestIndex = mix(bestIndex, vec4(inIdx), vec4(replace));
srcIdx++;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(bestIndex);
}
2021-12-01 21:37:52 +01:00
`}};function $_(e,t,n,r=null){let s=t.shape[0],a=t.shape[1];r!=null&&(s=r.shape[0],a=r.shape[1]);let o=N.computeOptimalWindowSize(a),i={windowSize:o,inSize:a,batchSize:s,outSize:Math.ceil(a/o)},u=new fZ(i,n,r==null),l=[t];r!=null&&l.push(r);let c=e.runWebGLProgram(u,l,"int32");if(c.shape[1]===1)return c;let d=$_(e,t,n,c);return e.disposeIntermediateTensorInfo(c),d}function F_(e,t,n,r=null){let s=r!=null?r.shape:t.shape,a=s[s.length-1],o=N.computeOptimalWindowSize(a),i=new mZ(s,o,n,r==null),u=r==null?[t]:[t,r],l=e.runWebGLProgram(i,u,"int32");if(l.shape.length===t.shape.length){let c=F_(e,t,n,l);return e.disposeIntermediateTensorInfo(l),c}return l}function D_(e,t,n,r){let s=[n];if(N.assertAxesAreInnerMostDims("arg"+r.charAt(0).toUpperCase()+r.slice(1),s,t.shape.length),!X().getBool("WEBGL_PACK_REDUCE")||t.shape.length<=2){let a=[],o=e.texData.get(t.dataId),i=o!==null&&o.isPacked,u=t;i&&(u=e.unpackTensor(t),a.push(u));let[l,c]=N.computeOutAndReduceShapes(u.shape,s),d=w.sizeFromShape(c),p=fe({inputs:{x:u},backend:e,attrs:{shape:[-1,d]}});a.push(p);let h=$_(e,p,r);a.push(h);let f=fe({inputs:{x:h},backend:e,attrs:{shape:l}});return a.forEach(m=>e.disposeIntermediateTensorInfo(m)),f}return F_(e,t,r)}function gZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a}=r,o=w.parseAxisParam(a,s.shape),i=N.getAxesPermutation(o,s.shape.length),u=s,l=[];i!=null&&(u=Dn({inputs:{x:s},backend:n,attrs:{perm:i}}),l.push(u),o=N.getInnerMostAxes(o.length,u.shape.length)),N.assertAxesAreInnerMostDims("argMax",[o[0]],u.shape.length);let c=D_(n,u,o[0],"max");return l.forEach(d=>n.disposeIntermediateTensorInfo(d)),c}var bZ={kernelName:da,backendName:"webgl",kernelFunc:gZ};function yZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a}=r,o=w.parseAxisParam(a,s.shape),i=N.getAxesPermutation(o,s.shape.length),u=s,l=[];i!=null&&(u=Dn({inputs:{x:s},backend:n,attrs:{perm:i}}),l.push(u),o=N.getInnerMostAxes(o.length,u.shape.length)),N.assertAxesAreInnerMostDims("argMin",[o[0]],u.shape.length);let c=D_(n,u,o[0],"min");return l.forEach(d=>n.disposeIntermediateTensorInfo(d)),c}var vZ={kernelName:ju,backendName:"webgl",kernelFunc:yZ},xZ=ts+`
2021-09-11 17:11:38 +02:00
if (abs(x) > 1.) {
2021-04-01 19:39:54 +02:00
return NAN;
}
2021-09-11 17:11:38 +02:00
return asin(x);
2021-12-01 21:37:52 +01:00
`,wZ=Ze({opSnippet:xZ}),kZ={kernelName:qu,backendName:"webgl",kernelFunc:wZ},IZ=ts+"return log(x + sqrt(x * x + 1.0));",SZ=Ze({opSnippet:IZ}),CZ={kernelName:Ku,backendName:"webgl",kernelFunc:SZ},TZ=ts+`
2021-09-11 17:11:38 +02:00
return atan(x);
2021-12-01 21:37:52 +01:00
`,NZ=Ze({opSnippet:TZ}),_Z={kernelName:Xu,backendName:"webgl",kernelFunc:NZ},EZ=OQ+`
2021-09-11 17:11:38 +02:00
return atan(a, b);
2021-12-01 21:37:52 +01:00
`,AZ=`
2021-09-11 17:11:38 +02:00
vec4 result = atan(a, b);
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
2021-12-01 21:37:52 +01:00
`+MQ+`
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,$Z=hn({opSnippet:EZ,packedOpSnippet:AZ}),FZ={kernelName:Qu,backendName:"webgl",kernelFunc:$Z},DZ=ts+`
2021-09-11 17:11:38 +02:00
if ((x < -1.0) || (x > 1.0)) return NAN;
2021-12-01 21:37:52 +01:00
return (log(1.0 + x) - log(1.0 - x)) / 2.0;`,RZ=Ze({opSnippet:DZ}),PZ={kernelName:Yu,backendName:"webgl",kernelFunc:RZ},lp=class{constructor(e,t,n,r=!1,s=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,o=e.strideHeight,i=e.strideWidth,u=e.dilationHeight,l=e.dilationWidth,c=e.effectiveFilterHeight,d=e.effectiveFilterWidth,p=e.padInfo.top,h=e.padInfo.left;this.outputShape=e.outShape;let f=t==="avg",m=`((batch * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + d`,g=`(xR * ${e.inWidth} + xC) * ${e.inChannels} + d`,b="0.0";if(f||(b="-1.0 / 1e-20"),n){let C=">=";this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec2 strides = ivec2(${o}, ${i});
const ivec2 pads = ivec2(${p}, ${h});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
int d = coords[3];
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec2 xRCCorner = coords.yz * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
float minMaxValue = 0.0;
float minMaxValueFound = 0.0;
int minMaxPosition = 0;
float avgValue = 0.0;
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
for (int wR = 0; wR < ${c};
wR += ${u}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${d};
2021-10-22 15:06:43 +02:00
wC += ${l}) {
2021-09-11 17:11:38 +02:00
int xC = xCCorner + wC;
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float value = getX(batch, xR, xC, d);
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix(
value, minMaxValue, minMaxValueFound);
2021-12-01 21:37:52 +01:00
if (value ${C} currMinMaxValue) {
2021-09-11 17:11:38 +02:00
minMaxValue = value;
minMaxValueFound = 1.0;
minMaxPosition = ${r?s?m:g:`wR * ${d} + wC`};
}
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(float(minMaxPosition));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`;return}let y="max",v=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(v="avgValue / count");let x=Math.floor(a/4)*4,k=a%4,T=`
2021-09-11 17:11:38 +02:00
if (${f}) {
avgValue += dot(values, ones);
2021-04-01 19:39:54 +02:00
} else {
2021-12-01 21:37:52 +01:00
minMaxValue = ${y}(values, minMaxValue);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`;this.userCode=`
const ivec2 strides = ivec2(${o}, ${i});
const ivec2 pads = ivec2(${p}, ${h});
const float initializationValue = ${b};
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float count = 0.0;
float getValue(int batch, int xR, int xC, int d) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
}
count += 1.0;
return getX(batch, xR, xC, d);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int batch = coords[0];
2021-04-01 19:39:54 +02:00
int d = coords[3];
2021-09-11 17:11:38 +02:00
ivec2 xRCCorner = coords.yz * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// max/min x(?, ?, d) to get y(yR, yC, d).
// ? = to be determined
vec4 minMaxValue = vec4(${b});
float avgValue = 0.0;
count = 0.0;
2021-12-01 21:37:52 +01:00
for (int wR = 0; wR < ${c};
wR += ${u}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
for (int wC = 0; wC < ${x}; wC += 4) {
2021-10-22 15:06:43 +02:00
int xC = xCCorner + wC * ${l};
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xR, xC, d),
2021-10-22 15:06:43 +02:00
getValue(batch, xR, xC + ${l}, d),
getValue(batch, xR, xC + 2 * ${l}, d),
getValue(batch, xR, xC + 3 * ${l}, d)
2021-09-11 17:11:38 +02:00
);
2021-12-01 21:37:52 +01:00
${T}
2021-09-11 17:11:38 +02:00
}
int xC = xCCorner + ${x};
if (${k===1}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
initializationValue,
initializationValue,
initializationValue
);
2021-12-01 21:37:52 +01:00
${T}
2021-09-11 17:11:38 +02:00
} else if (${k===2}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
2021-10-22 15:06:43 +02:00
getValue(batch, xR, xC + ${l}, d),
2021-09-11 17:11:38 +02:00
initializationValue,
initializationValue
);
2021-12-01 21:37:52 +01:00
${T}
2021-09-11 17:11:38 +02:00
} else if (${k===3}) {
vec4 values = vec4(
getValue(batch, xR, xC, d),
2021-10-22 15:06:43 +02:00
getValue(batch, xR, xC + ${l}, d),
getValue(batch, xR, xC + 2 * ${l}, d),
2021-09-11 17:11:38 +02:00
initializationValue
);
2021-12-01 21:37:52 +01:00
${T}
2021-04-01 19:39:54 +02:00
}
}
2021-12-01 21:37:52 +01:00
setOutput(${v});
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},ck=class{constructor(e,t,n,r=!1,s=!1){if(this.variableNames=["x"],t==="avg"&&n)throw new Error("Cannot compute positions for average pool.");let a=e.filterWidth,o=e.strideDepth,i=e.strideHeight,u=e.strideWidth,l=e.dilationDepth,c=e.dilationHeight,d=e.dilationWidth,p=e.effectiveFilterDepth,h=e.effectiveFilterHeight,f=e.effectiveFilterWidth,m=e.padInfo.front,g=e.padInfo.top,b=e.padInfo.left;this.outputShape=e.outShape;let y=t==="avg",v="0.0";if(y||(v="-1.0 / 1e-20"),n){let F=">=";this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec3 strides =
2021-12-01 21:37:52 +01:00
ivec3(${o}, ${i}, ${u});
2021-09-11 17:11:38 +02:00
const ivec3 pads = ivec3(${m}, ${g}, ${b});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// max/min x(?, ?, ?, ch) to get y(yD, yR, yC, ch).
// ? = to be determined
float minMaxValue = 0.0;
float minMaxValueFound = 0.0;
int minMaxPosition = 0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wD = 0; wD < ${p};
2021-10-22 15:06:43 +02:00
wD += ${l}) {
2021-09-11 17:11:38 +02:00
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${h};
2021-12-01 21:37:52 +01:00
wR += ${c}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${f};
wC += ${d}) {
int xC = xCCorner + wC;
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float value = getX(batch, xD, xR, xC, ch);
// If a min / max value has already been found, use it. If not,
// use the current value.
float currMinMaxValue = mix(
value, minMaxValue, minMaxValueFound);
2021-10-28 19:58:21 +02:00
if (value ${F} currMinMaxValue) {
2021-09-11 17:11:38 +02:00
minMaxValue = value;
minMaxValueFound = 1.0;
minMaxPosition = ${r?s?`(((batch * ${e.inDepth} + xD) * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + ch`:`((xD * ${e.inHeight} + xR) * ${e.inWidth} + xC) * ${e.inChannels} + ch`:`wD * ${h} * ${f} +
wR * ${f} + wC`};
}
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
setOutput(float(minMaxPosition));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`;return}let x="max",k=`${t}(${t}(${t}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;t==="avg"&&(k="avgValue / count");let T=Math.floor(a/4)*4,C=a%4,E=`
if (${y}) {
2021-09-11 17:11:38 +02:00
avgValue += dot(values, ones);
} else {
minMaxValue = ${x}(values, minMaxValue);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
`;this.userCode=`
const ivec3 strides =
2021-12-01 21:37:52 +01:00
ivec3(${o}, ${i}, ${u});
2021-09-11 17:11:38 +02:00
const ivec3 pads = ivec3(${m}, ${g}, ${b});
2021-12-01 21:37:52 +01:00
const float initializationValue = ${v};
2021-09-11 17:11:38 +02:00
const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
float count = 0.0;
float getValue(int batch, int xD, int xR, int xC, int ch) {
if (xC < 0 || xC >= ${e.inWidth}) {
return initializationValue;
}
count += 1.0;
return getX(batch, xD, xR, xC, ch);
}
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xDCorner = xCorner.x;
int xRCorner = xCorner.y;
int xCCorner = xCorner.z;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).
// ? = to be determined
2021-12-01 21:37:52 +01:00
vec4 minMaxValue = vec4(${v});
2021-09-11 17:11:38 +02:00
float avgValue = 0.0;
count = 0.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wD = 0; wD < ${p};
2021-10-22 15:06:43 +02:00
wD += ${l}) {
2021-09-11 17:11:38 +02:00
int xD = xDCorner + wD;
if (xD < 0 || xD >= ${e.inDepth}) {
continue;
}
for (int wR = 0; wR < ${h};
2021-12-01 21:37:52 +01:00
wR += ${c}) {
2021-09-11 17:11:38 +02:00
int xR = xRCorner + wR;
if (xR < 0 || xR >= ${e.inHeight}) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
2021-12-01 21:37:52 +01:00
for (int wC = 0; wC < ${T}; wC += 4) {
2021-09-11 17:11:38 +02:00
int xC = xCCorner + wC * ${d};
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${d}, ch),
getValue(batch, xD, xR, xC + 2 * ${d}, ch),
getValue(batch, xD, xR, xC + 3 * ${d}, ch)
);
2021-12-01 21:37:52 +01:00
${E}
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
int xC = xCCorner + ${T};
if (${C===1}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
initializationValue,
initializationValue,
initializationValue
);
2021-12-01 21:37:52 +01:00
${E}
} else if (${C===2}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${d}, ch),
initializationValue,
initializationValue
);
2021-12-01 21:37:52 +01:00
${E}
} else if (${C===3}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, xD, xR, xC, ch),
getValue(batch, xD, xR, xC + ${d}, ch),
getValue(batch, xD, xR, xC + 2 * ${d}, ch),
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-12-01 21:37:52 +01:00
${E}
2021-04-01 19:39:54 +02:00
}
}
2021-09-11 17:11:38 +02:00
setOutput(${k});
2021-04-01 19:39:54 +02:00
}
}
2021-12-01 21:37:52 +01:00
`}};function OZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t;Gc(s,"avgPool");let{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=r,l=1;w.assert(N.eitherStridesOrDilationsAreOne(o,l),()=>`Error in avgPool: Either strides or dilations must be 1. Got strides ${o} and dilations '${l}'`);let c=N.computePool2DInfo(s.shape,a,o,l,i,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return cr({inputs:{x:s},backend:n});let d=new lp(c,"avg",!1);return n.runWebGLProgram(d,[s],"float32")}var MZ={kernelName:pa,backendName:"webgl",kernelFunc:OZ};function LZ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u,dataFormat:l}=r,c=[1,1,1],d=N.computePool3DInfo(s.shape,a,o,c,i,u,l),p=new ck(d,"avg",!1);return n.runWebGLProgram(p,[s],"float32")}var BZ={kernelName:ql,backendName:"webgl",kernelFunc:LZ},zZ=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,s=e.strideWidth,a=e.dilationHeight,o=e.dilationWidth,i=e.effectiveFilterHeight,u=e.effectiveFilterWidth,l=i-1-e.padInfo.top,c=u-1-e.padInfo.left,d=1/(t*n);this.userCode=`
const ivec2 pads = ivec2(${l}, ${c});
2021-09-11 17:11:38 +02:00
const float avgMultiplier = float(${d});
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${i};
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${r}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
int idyR = int(dyR);
2021-12-01 21:37:52 +01:00
for (int wC = 0; wC < ${u};
2021-09-11 17:11:38 +02:00
wC+= ${o}) {
float dyC = float(dyCCorner + wC) / ${s}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
2021-04-01 19:39:54 +02:00
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
2021-09-11 17:11:38 +02:00
dotProd += dyValue * avgMultiplier;
2021-04-01 19:39:54 +02:00
}
}
setOutput(dotProd);
}
2021-12-01 21:37:52 +01:00
`}},WZ=class{constructor(e){this.variableNames=["dy"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,r=e.filterWidth,s=e.strideDepth,a=e.strideHeight,o=e.strideWidth,i=e.dilationDepth,u=e.dilationHeight,l=e.dilationWidth,c=e.effectiveFilterDepth,d=e.effectiveFilterHeight,p=e.effectiveFilterWidth,h=c-1-e.padInfo.front,f=d-1-e.padInfo.top,m=p-1-e.padInfo.left,g=1/(t*n*r);this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec3 pads = ivec3(${h}, ${f}, ${m});
const float avgMultiplier = float(${g});
2021-04-01 19:39:54 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyDCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
2021-09-11 17:11:38 +02:00
// Convolve dy(?, ?, ?, d) with pos mask(:, :, :, ch) to get
2021-04-01 19:39:54 +02:00
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
2021-12-01 21:37:52 +01:00
for (int wD = 0; wD < ${c};
2021-09-11 17:11:38 +02:00
wD += ${i}) {
float dyD = float(dyDCorner + wD) / ${s}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
int idyD = int(dyD);
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${d};
2021-12-01 21:37:52 +01:00
wR += ${u}) {
2021-09-11 17:11:38 +02:00
float dyR = float(dyRCorner + wR) / ${a}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
2021-04-01 19:39:54 +02:00
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${p};
2021-10-22 15:06:43 +02:00
wC += ${l}) {
2021-09-11 17:11:38 +02:00
float dyC = float(dyCCorner + wC) / ${o}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
2021-04-01 19:39:54 +02:00
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
2021-09-11 17:11:38 +02:00
dotProd += dyValue * avgMultiplier;
2021-04-01 19:39:54 +02:00
}
}
}
setOutput(dotProd);
}
2021-12-01 21:37:52 +01:00
`}};function VZ(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a}=t,o=a,{filterSize:i,strides:u,pad:l,dimRoundingMode:c}=r,d=[1,1,1],p=N.computePool3DInfo(o.shape,i,u,d,l,c),h=new WZ(p);return n.runWebGLProgram(h,[s],o.dtype)}var UZ={kernelName:gh,backendName:"webgl",kernelFunc:VZ};function GZ(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a}=t,o=a;Gc([s,a],"avgPoolGrad");let{filterSize:i,strides:u,pad:l}=r,c=N.computePool2DInfo(o.shape,i,u,1,l),d=new zZ(c);return n.runWebGLProgram(d,[s],o.dtype)}var HZ={kernelName:mh,backendName:"webgl",kernelFunc:GZ};function jZ(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a}=t,{transposeA:o,transposeB:i}=r;return Rm({a:s,b:a,transposeA:o,transposeB:i,backend:n})}var qZ={kernelName:ha,backendName:"webgl",kernelFunc:jZ},KZ=class{constructor(e,t,n,r,s,a){this.outputShape=[],this.variableNames=["x","mean","variance"],N.assertAndGetBroadcastShape(e,t),N.assertAndGetBroadcastShape(e,n);let o="0.0";r!=null&&(N.assertAndGetBroadcastShape(e,r),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");let i="1.0";s!=null&&(N.assertAndGetBroadcastShape(e,s),this.variableNames.push("scale"),i="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
float x = getXAtOutCoords();
float mean = getMeanAtOutCoords();
float variance = getVarianceAtOutCoords();
float offset = ${o};
float scale = ${i};
float inv = scale * inversesqrt(variance + float(${a}));
setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},XZ=class{constructor(e,t,n,r,s,a){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],N.assertAndGetBroadcastShape(e,t),N.assertAndGetBroadcastShape(e,n);let o="vec4(0.0)";r!=null&&(N.assertAndGetBroadcastShape(e,r),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");let i="vec4(1.0)";s!=null&&(N.assertAndGetBroadcastShape(e,s),this.variableNames.push("scale"),i="getScaleAtOutCoords()"),this.outputShape=e,this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
vec4 offset = ${o};
vec4 scale = ${i};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 x = getXAtOutCoords();
vec4 mean = getMeanAtOutCoords();
vec4 variance = getVarianceAtOutCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 inv = scale * inversesqrt(variance + vec4(${a}));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput((x - mean) * inv + offset);
}
2021-12-01 21:37:52 +01:00
`}},YZ=({inputs:e,backend:t,attrs:n})=>{let{x:r,mean:s,variance:a,offset:o,scale:i}=e;w.assert(s.shape.length===a.shape.length,()=>"Batch normalization gradient requires mean and variance to have equal ranks."),w.assert(o==null||s.shape.length===o.shape.length,()=>"Batch normalization gradient requires mean and offset to have equal ranks."),w.assert(i==null||s.shape.length===i.shape.length,()=>"Batch normalization gradient requires mean and scale to have equal ranks.");let{varianceEpsilon:u}=n;u==null&&(u=.001);let l=[r,s,a],c=null;o!=null&&(c=o.shape,l.push(o));let d=null;i!=null&&(d=i.shape,l.push(i));let p=X().getBool("WEBGL_PACK_NORMALIZATION")?new XZ(r.shape,s.shape,a.shape,c,d,u):new KZ(r.shape,s.shape,a.shape,c,d,u);return t.runWebGLProgram(p,l,l[0].dtype)},QZ={kernelName:Ta,backendName:"webgl",kernelFunc:YZ},ZZ=class{constructor(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;let t=gt(this.rank);this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let n=JZ(this.rank),r,s=e.map((a,o)=>`sourceLoc.${lk[o]} = start[${o}] + coords.${lk[o]};`);r=`
2021-09-11 17:11:38 +02:00
${t} sourceLoc;
${t} coords = getOutputCoords();
${s.join(`
`)}
`,this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
${r}
setOutput(getSource(${n}));
}
2021-12-01 21:37:52 +01:00
`}},lk=["x","y","z","w","u","v"];function JZ(e){if(e===1)return"sourceLoc";if(e<=6)return lk.slice(0,e).map(t=>"sourceLoc."+t).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}var eJ=class{constructor(e){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e,this.rank=e.length,this.customUniforms=[{name:"start",arrayIndex:this.rank,type:"int"}];let t=gt(this.rank),n=Fn("coords",this.rank),r=Fn("sourceLoc",this.rank),s=this.rank===1?"sourceLoc":`vec2(${r.slice(-2).join()})`,a=`getChannel(getSource(${r.join()}), ${s})`,o=`
2021-09-11 17:11:38 +02:00
result.x = ${a};
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
++${r[this.rank-1]};
result.y = ${a};
--${r[this.rank-1]};
}
`,i=this.rank===1?"":`
--${n[this.rank-1]};
if (++${n[this.rank-2]} < ${e[this.rank-2]}) {
++${r[this.rank-2]};
result.z = ${a};
if (++${n[this.rank-1]} < ${e[this.rank-1]}) {
++${r[this.rank-1]};
result.w = ${a};
}
}
2021-12-01 21:37:52 +01:00
`,u=this.rank<=4?`sourceLoc = coords +
${t}(${e.map((l,c)=>`start[${c}]`).join()});`:e.map((l,c)=>`${r[c]} = ${n[c]} + start[${c}];`).join(`
2021-09-11 17:11:38 +02:00
`);this.userCode=`
void main() {
${t} coords = getOutputCoords();
${t} sourceLoc;
2021-12-01 21:37:52 +01:00
${u}
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0.);
${o}
${i}
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}};function tJ(e,t,n,r){let s=r.texData.get(e.dataId),a=r.makeTensorInfo(n,e.dtype),o=r.texData.get(a.dataId);Object.assign(o,s),o.refCount=1,o.shape=n,o.dtype=e.dtype;let i=$t.computeFlatOffset(t,w.computeStrides(e.shape));s.slice&&(i+=s.slice.flatOffset),o.slice={flatOffset:i,origDataId:s.slice&&s.slice.origDataId||e.dataId};let u=r.dataRefCount.get(o.slice.origDataId)||1;return r.dataRefCount.set(o.slice.origDataId,u+1),a}function Qc(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,size:o}=r,[i,u]=$t.parseSliceParams(s,a,o);if($t.assertParamsValid(s,i,u),w.sizeFromShape(u)===0)return n.makeTensorInfo(u,s.dtype,[]);if(n.shouldExecuteOnCPU([s])||s.dtype==="string"){let d=n.texData.get(s.dataId),p=G9(d.values,i,u,s.shape,s.dtype);return n.makeTensorInfo(u,s.dtype,p)}let{isPacked:l}=n.texData.get(s.dataId),c=$t.isSliceContinous(s.shape,i,u);if(l||!c){let d=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new eJ(u):new ZZ(u),p=[i];return n.runWebGLProgram(d,[s],s.dtype,p)}return n.uploadToGPU(s.dataId),tJ(s,i,u,n)}var nJ={kernelName:yi,backendName:"webgl",kernelFunc:Qc},rJ=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockShape:a,crops:o}=r;w.assert(s.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGL backend not implemented yet");let i=a.reduce((y,v)=>y*v),u=N.getReshaped(s.shape,a,i),l=N.getPermuted(u.length,a.length),c=N.getReshapedPermuted(s.shape,a,i),d=N.getSliceBeginCoords(o,a.length),p=N.getSliceSize(c,o,a.length),h=[],f=fe({inputs:{x:s},backend:n,attrs:{shape:u}}),m=Dn({inputs:{x:f},backend:n,attrs:{perm:l}}),g=fe({inputs:{x:m},backend:n,attrs:{shape:c}}),b=Qc({inputs:{x:g},backend:n,attrs:{begin:d,size:p}});return h.push(f),h.push(m),h.push(g),h.forEach(y=>n.disposeIntermediateTensorInfo(y)),b},sJ={kernelName:Uo,backendName:"webgl",kernelFunc:rJ};function aJ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,weights:a}=t,{size:o}=r,i=n.readSync(s.dataId),u=n.readSync(a.dataId),l=o_(i,u,a.dtype,a.shape,o);return n.makeTensorInfo([o],a.dtype,l)}var oJ={kernelName:bh,backendName:"webgl",kernelFunc:aJ};function iJ(e){let{inputs:t,backend:n}=e,{s0:r,s1:s}=t,a=n.readSync(r.dataId),o=n.readSync(s.dataId),i=N.assertAndGetBroadcastShape(Array.from(a),Array.from(o));return n.makeTensorInfo([i.length],"int32",Int32Array.from(i))}var uJ={kernelName:yh,backendName:"webgl",kernelFunc:iJ},cJ="return float(a != b);",R_=hn({opSnippet:cJ,cpuKernelImpl:B9,dtype:"bool"}),lJ={kernelName:oi,backendName:"webgl",kernelFunc:R_};function dp(e){let{inputs:t,backend:n}=e,{input:r}=t,s=n.texData.get(r.dataId);return cr({inputs:{x:s.complexTensorInfos.real},backend:n})}var dJ={kernelName:rd,backendName:"webgl",kernelFunc:dp},pJ="return float(int(x));";function hJ(e,t){let n=new To(e.shape,pJ),r=t.runWebGLProgram(n,[e],"int32");return{dataId:r.dataId,shape:r.shape,dtype:r.dtype}}function dk(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{dtype:a}=r;if(a==="complex64"){if(s.dtype==="complex64")return cr({inputs:{x:s},backend:n});let o=kt(s.shape),i=dk({inputs:{x:s},backend:n,attrs:{dtype:"float32"}}),u=No({inputs:{real:i,imag:o},backend:n});return o.dispose(),n.disposeIntermediateTensorInfo(i),u}if(s.dtype==="complex64"){let o=dp({inputs:{input:s},backend:n}),i=dk({inputs:{x:o},backend:n,attrs:{dtype:a}});return n.disposeIntermediateTensorInfo(o),i}if(!w.hasEncodingLoss(s.dtype,a)){let o=cr({inputs:{x:s},backend:n});return{dataId:o.dataId,shape:o.shape,dtype:a}}if(a==="int32")return hJ(s,n);if(a==="bool"){let o=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=R_({inputs:{a:s,b:o},backend:n});return n.disposeIntermediateTensorInfo(o),u}throw new Error(`Error in Cast: failed to cast ${s.dtype} to ${a}`)}var fJ={kernelName:fa,backendName:"webgl",kernelFunc:dk},P_="return ceil(x);",mJ=Ze({opSnippet:P_,packedOpSnippet:P_,cpuKernelImpl:x9}),gJ={kernelName:ma,backendName:"webgl",kernelFunc:mJ},bJ=class{constructor(e){this.variableNames=["A"],this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=e,this.userCode=`
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
float value = getAAtOutCoords();
if (isnan(value)) {
setOutput(value);
return;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput(clamp(value, minVal, maxVal));
}
2021-12-01 21:37:52 +01:00
`}},yJ=class{constructor(e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"minVal",type:"float"},{name:"maxVal",type:"float"}],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
vec4 value = getAAtOutCoords();
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (any(isnan(value))) {
setOutput(value);
return;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
setOutput(clamp(value, vec4(minVal), vec4(maxVal)));
}
2021-12-01 21:37:52 +01:00
`}};function vJ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{clipValueMin:a,clipValueMax:o}=r,i;X().getBool("WEBGL_PACK_CLIP")?i=new yJ(s.shape):i=new bJ(s.shape);let u=[[a],[o]];return n.runWebGLProgram(i,[s],s.dtype,u)}var xJ={kernelName:Es,backendName:"webgl",kernelFunc:vJ},wJ=class{constructor(e){this.variableNames=["real","imag"],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
float re = abs(getRealAtOutCoords());
float im = abs(getImagAtOutCoords());
float mx = max(re, im);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// sadly the length function in glsl is not underflow-safe
// (at least not on Intel GPUs). So the safe solution is
// to ensure underflow-safety in all cases.
setOutput(
mx == 0.0 ? 0.0 : mx * length(vec2(1, min(re, im)/mx))
);
}
2021-12-01 21:37:52 +01:00
`}};function O_(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}}function kJ(e){let{inputs:t,backend:n}=e,{x:r}=t,s=n.texData.get(r.dataId),a=new wJ(r.shape),o=[O_(r,s.complexTensorInfos.real),O_(r,s.complexTensorInfos.imag)];return n.runWebGLProgram(a,o,o[0].dtype)}var IJ={kernelName:Xl,backendName:"webgl",kernelFunc:kJ},SJ=class{constructor(e){this.outputShape=[],this.outputShape=N.computeOutShape(e,1),this.variableNames=e.map((a,o)=>`T${o}`);let t=new Array(e.length-1);t[0]=e[0][1];for(let a=1;a<t.length;a++)t[a]=t[a-1]+e[a][1];let n=[`if (yC < ${t[0]}) setOutput(getT0(yR, yC));`];for(let a=1;a<t.length;a++){let o=t[a-1];n.push(`else if (yC < ${t[a]}) setOutput(getT${a}(yR, yC-${o}));`)}let r=t.length,s=t[t.length-1];n.push(`else setOutput(getT${r}(yR, yC-${s}));`),this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int yR = coords.x;
int yC = coords.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${n.join(`
`)}
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},CJ=class{constructor(e,t){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=N.computeOutShape(e,t);let n=this.outputShape,r=n.length,s=gt(r),a=Fn("coords",r),o=["x","y","z","w","u","v"].slice(0,r);this.variableNames=e.map((f,m)=>`T${m}`);let i=new Array(e.length-1);i[0]=e[0][t];for(let f=1;f<i.length;f++)i[f]=i[f-1]+e[f][t];let u=o[t],l=o.slice(-2),c=o.join(),d=`if (${u} < ${i[0]}) {
2021-09-11 17:11:38 +02:00
return getChannel(
2021-12-01 21:37:52 +01:00
getT0(${c}), vec2(${l.join()}));
2021-09-11 17:11:38 +02:00
}`;for(let f=1;f<i.length;f++){let m=i[f-1];d+=`
2021-12-01 21:37:52 +01:00
if (${u} < ${i[f]} && ${u} >= ${i[f-1]}) {
2021-09-11 17:11:38 +02:00
return getChannel(
2021-12-01 21:37:52 +01:00
getT${f}(${Om(o,u,m)}),
vec2(${Om(l,u,m)}));
2021-09-11 17:11:38 +02:00
}`}let p=i.length,h=i[i.length-1];d+=`
return getChannel(
2021-12-01 21:37:52 +01:00
getT${p}(${Om(o,u,h)}),
vec2(${Om(l,u,h)}));`,this.userCode=`
2021-09-11 17:11:38 +02:00
float getValue(${o.map(f=>"int "+f)}) {
${d}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
${s} coords = getOutputCoords();
vec4 result = vec4(getValue(${a}), 0., 0., 0.);
${a[r-1]} = ${a[r-1]} + 1;
if (${a[r-1]} < ${n[r-1]}) {
result.g = getValue(${a});
}
${a[r-2]} = ${a[r-2]} + 1;
if (${a[r-2]} < ${n[r-2]}) {
result.a = getValue(${a});
}
${a[r-1]} = ${a[r-1]} - 1;
if (${a[r-2]} < ${n[r-2]} &&
${a[r-1]} < ${n[r-1]}) {
result.b = getValue(${a});
}
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function Om(e,t,n){let r=e.indexOf(t);return e.map((a,o)=>o===r?`${a} - ${n}`:a).join()}function Mm(e){let{inputs:t,backend:n}=e,{input:r}=t,s=n.texData.get(r.dataId);return cr({inputs:{x:s.complexTensorInfos.imag},backend:n})}var TJ={kernelName:Jl,backendName:"webgl",kernelFunc:Mm};function Zc(e,t,n){let r=e[0].dtype;if(r==="complex64"){let c=e.map(m=>dp({inputs:{input:m},backend:n})),d=e.map(m=>Mm({inputs:{input:m},backend:n})),p=Zc(c,t,n),h=Zc(d,t,n),f=No({inputs:{real:p,imag:h},backend:n});return c.forEach(m=>n.disposeIntermediateTensorInfo(m)),d.forEach(m=>n.disposeIntermediateTensorInfo(m)),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(h),f}let s=n.shouldExecuteOnCPU(e);if(r==="string"&&(s=!0),s){let c=e.map(b=>{let y=w.sizeFromShape(b.shape.slice(t));return fe({inputs:{x:b},backend:n,attrs:{shape:[-1,y]}})}),d=c.map(b=>({vals:n.readSync(b.dataId),shape:b.shape})),p=N.computeOutShape(c.map(b=>b.shape),1),h=c[0].shape[0]===1,f=w9(d,p,r,h),m=N.computeOutShape(e.map(b=>b.shape),t),g=n.makeTensorInfo(m,r,f);return c.forEach(b=>n.disposeIntermediateTensorInfo(b)),g}if(e.length>X().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){let c=Math.floor(e.length/2),d=Zc(e.slice(0,c),t,n),p=Zc(e.slice(c),t,n),h=Zc([d,p],t,n);return n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(p),h}if(X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&e[0].shape.length>1){let c=new CJ(e.map(d=>d.shape),t);return n.runWebGLProgram(c,e,r)}let{tensors2D:a,outShape:o}=NJ(e,t,n),i=new SJ(a.map(c=>c.shape)),u=n.runWebGLProgram(i,a,r);a.forEach(c=>n.disposeIntermediateTensorInfo(c));let l=fe({inputs:{x:u},attrs:{shape:o},backend:n});return n.disposeIntermediateTensorInfo(u),l}function NJ(e,t,n){let r=N.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>fe({inputs:{x:a},attrs:{shape:[-1,w.sizeFromShape(a.shape.slice(t))]},backend:n})),outShape:r}}function M_(e){let{inputs:t,backend:n,attrs:r}=e,{axis:s}=r,a=w.parseAxisParam(s,t[0].shape)[0],o=N.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(o)===0)return n.makeTensorInfo(o,t[0].dtype,[]);let i=t.filter(l=>w.sizeFromShape(l.shape)>0);if(i.length===1)return cr({inputs:{x:i[0]},backend:n});let u=i.map(l=>l.shape);return N.assertParamsConsistent(u,a),Zc(i,a,n)}var _J={kernelName:Go,backendName:"webgl",kernelFunc:M_},L_=class{constructor(e,t=!1,n=null,r=!1,s=!1){this.variableNames=["x","W"],this.outputShape=e.outShape;let a=e.padInfo.top,o=e.padInfo.left,i=e.strideHeight,u=e.strideWidth,l=e.dilationHeight,c=e.dilationWidth,d=e.filterHeight,p=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4,m=e.dataFormat==="channelsLast",g=m?1:2,b=m?2:3,y=m?3:1,v="",x="";n&&(r?v=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getPreluActivationWeightsAtOutCoords();
${n}
2021-12-01 21:37:52 +01:00
}`:s?v=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getLeakyreluAlphaAtOutCoords();
${n}
2021-12-01 21:37:52 +01:00
}`:v=`
2021-09-11 17:11:38 +02:00
float activation(float x) {
${n}
}
`,x="result = activation(result);");let k=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
2021-12-01 21:37:52 +01:00
${v}
2021-09-11 17:11:38 +02:00
2021-12-01 21:37:52 +01:00
const ivec2 strides = ivec2(${i}, ${u});
2021-09-11 17:11:38 +02:00
const ivec2 pads = ivec2(${a}, ${o});
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int batch = coords[0];
2021-12-01 21:37:52 +01:00
int d2 = coords[${y}];
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec2 xRCCorner =
ivec2(coords[${g}], coords[${b}]) * strides - pads;
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${d}; wR++) {
2021-10-22 15:06:43 +02:00
int xR = xRCorner + wR * ${l};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${p}; wC++) {
2021-12-01 21:37:52 +01:00
int xC = xCCorner + wC * ${c};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int d1 = 0; d1 < ${h}; d1 += 4) {
vec4 wValues = vec4(
getW(wR, wC, d1, d2),
getW(wR, wC, d1 + 1, d2),
getW(wR, wC, d1 + 2, d2),
getW(wR, wC, d1 + 3, d2)
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${m}) {
vec4 xValues = vec4(
getX(batch, xR, xC, d1),
getX(batch, xR, xC, d1 + 1),
getX(batch, xR, xC, d1 + 2),
getX(batch, xR, xC, d1 + 3)
);
dotProd += dot(xValues, wValues);
} else {
vec4 xValues = vec4(
getX(batch, d1, xR, xC),
getX(batch, d1 + 1, xR, xC),
getX(batch, d1 + 2, xR, xC),
getX(batch, d1 + 3, xR, xC)
);
dotProd += dot(xValues, wValues);
}
}
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
if (${f===1}) {
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${m}) {
dotProd +=
getX(batch, xR, xC, ${h}) *
getW(wR, wC, ${h}, d2);
} else {
dotProd +=
getX(batch, ${h}, xR, xC) *
getW(wR, wC, ${h}, d2);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
} else if (${f===2}) {
vec2 wValues = vec2(
getW(wR, wC, ${h}, d2),
getW(wR, wC, ${h} + 1, d2)
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${m}) {
vec2 xValues = vec2(
getX(batch, xR, xC, ${h}),
getX(batch, xR, xC, ${h} + 1)
);
dotProd += dot(xValues, wValues);
} else {
vec2 xValues = vec2(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC)
);
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
} else if (${f===3}) {
vec3 wValues = vec3(
getW(wR, wC, ${h}, d2),
getW(wR, wC, ${h} + 1, d2),
getW(wR, wC, ${h} + 2, d2)
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${m}) {
vec3 xValues = vec3(
getX(batch, xR, xC, ${h}),
getX(batch, xR, xC, ${h} + 1),
getX(batch, xR, xC, ${h} + 2)
);
dotProd += dot(xValues, wValues);
} else {
vec3 xValues = vec3(
getX(batch, ${h}, xR, xC),
getX(batch, ${h} + 1, xR, xC),
getX(batch, ${h} + 2, xR, xC)
);
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
}
}
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float result = dotProd;
${k}
${x}
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}},EJ=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let t=e.padInfo.front,n=e.padInfo.top,r=e.padInfo.left,s=e.strideDepth,a=e.strideHeight,o=e.strideWidth,i=e.dilationDepth,u=e.dilationHeight,l=e.dilationWidth,c=e.filterDepth,d=e.filterHeight,p=e.filterWidth,h=Math.floor(e.inChannels/4)*4,f=e.inChannels%4;this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec3 strides = ivec3(${s}, ${a}, ${o});
const ivec3 pads = ivec3(${t}, ${n}, ${r});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d2 = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
int xFCorner = xFRCCorner.x;
int xRCorner = xFRCCorner.y;
int xCCorner = xFRCCorner.z;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Convolve x(?, ?, ?, d1) with w(:, :, :, d1, d2) to get
// y(yF, yR, yC, d2). ? = to be determined. : = across all
// values in that axis.
float dotProd = 0.0;
2021-12-01 21:37:52 +01:00
for (int wF = 0; wF < ${c}; wF++) {
2021-09-11 17:11:38 +02:00
int xF = xFCorner + wF * ${i};
if (xF < 0 || xF >= ${e.inDepth}) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${d}; wR++) {
2021-12-01 21:37:52 +01:00
int xR = xRCorner + wR * ${u};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${p}; wC++) {
2021-10-22 15:06:43 +02:00
int xC = xCCorner + wC * ${l};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int d1 = 0; d1 < ${h}; d1 += 4) {
vec4 xValues = vec4(
getX(batch, xF, xR, xC, d1),
getX(batch, xF, xR, xC, d1 + 1),
getX(batch, xF, xR, xC, d1 + 2),
getX(batch, xF, xR, xC, d1 + 3)
);
vec4 wValues = vec4(
getW(wF, wR, wC, d1, d2),
getW(wF, wR, wC, d1 + 1, d2),
getW(wF, wR, wC, d1 + 2, d2),
getW(wF, wR, wC, d1 + 3, d2)
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${f===1}) {
dotProd +=
getX(batch, xF, xR, xC, ${h}) *
getW(wF, wR, wC, ${h}, d2);
} else if (${f===2}) {
vec2 xValues = vec2(
getX(batch, xF, xR, xC, ${h}),
getX(batch, xF, xR, xC, ${h} + 1)
);
vec2 wValues = vec2(
getW(wF, wR, wC, ${h}, d2),
getW(wF, wR, wC, ${h} + 1, d2)
);
dotProd += dot(xValues, wValues);
} else if (${f===3}) {
vec3 xValues = vec3(
getX(batch, xF, xR, xC, ${h}),
getX(batch, xF, xR, xC, ${h} + 1),
getX(batch, xF, xR, xC, ${h} + 2)
);
vec3 wValues = vec3(
getW(wF, wR, wC, ${h}, d2),
getW(wF, wR, wC, ${h} + 1, d2),
getW(wF, wR, wC, ${h} + 2, d2)
);
dotProd += dot(xValues, wValues);
}
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},AJ=class{constructor(e,t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"inputShape",type:"ivec3"},{name:"pad",type:"ivec2"},{name:"stride",type:"ivec2"},{name:"dilation",type:"ivec2"},{name:"inChannels",type:"int"},{name:"itemsPerBlockRow",type:"int"},{name:"outWidth",type:"int"}],this.outputShape=e,this.enableShapeUniforms=qn(this.outputShape.length);let{dataFormat:n}=t,r=$n(),s=n==="channelsLast",a=s?0:1,o=s?1:2,i=this.enableShapeUniforms?"if(blockIndex < outShape[1] && pos < outShape[0]) {":`if(blockIndex < ${e[1]} && pos < ${e[0]}) {`,u="";for(let l=0;l<=1;l++)for(let c=0;c<=1;c++)u+=`
blockIndex = rc.y + ${c};
2021-10-22 15:06:43 +02:00
pos = rc.x + ${l};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${i}
offsetY = int(blockIndex / outWidth) * stride[0] - pad[0];
d0 = offsetY + dilation[0] * (pos / itemsPerBlockRow);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if(d0 < inputShape[${a}] && d0 >= 0) {
// Use custom imod instead mod. On Intel GPU, mod may generate
// unexpected value.
// https://github.com/tensorflow/tfjs/issues/5447
offsetX = imod(blockIndex, outWidth) * stride[1] - pad[1];
d1 = offsetX + dilation[1] * (imod(pos, itemsPerBlockRow) /
inChannels);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if(d1 < inputShape[${o}] && d1 >= 0) {
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
ch = imod(pos, inChannels);
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
if (${s}) {
innerDims = vec2(d1, ch);
2021-12-01 21:37:52 +01:00
result[${l*2+c}] = getChannel(
2021-09-11 17:11:38 +02:00
getA(d0, int(innerDims.x),
int(innerDims.y)), innerDims);
} else {
innerDims = vec2(d0, d1);
2021-12-01 21:37:52 +01:00
result[${l*2+c}] = getChannel(
2021-09-11 17:11:38 +02:00
getA(ch, int(innerDims.x),
int(innerDims.y)), innerDims);
}
}
}
}
`;this.userCode=`
2021-04-30 18:01:04 +02:00
void main() {
2021-09-11 17:11:38 +02:00
ivec2 rc = getOutputCoords();
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
vec4 result = vec4(0);
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
int blockIndex, pos, offsetY, d0, offsetX, d1, ch;
vec2 innerDims;
2021-04-30 18:01:04 +02:00
2021-12-01 21:37:52 +01:00
${u}
2021-04-30 18:01:04 +02:00
2021-09-11 17:11:38 +02:00
${r.output} = result;
2021-04-30 18:01:04 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function B_({x:e,filter:t,convInfo:n,backend:r,bias:s=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let u=e.shape,l=r.texData.get(e.dataId),c=n.inChannels,d=u[0]*u[1]*u[2],p=n.outChannels,h=n.dataFormat==="channelsLast",f=!1,m=!1,g,b=[];if(!((d===1||p===1)&&c>__)&&l.isPacked&&h&&l.texture!=null&&u[2]%2!=0&&w.arraysEqual(l.shape.slice(-3),u.slice(-3))){let x=u[0]*u[1]*(u[2]+1),k={dataId:e.dataId,shape:[1,x,n.inChannels],dtype:e.dtype},T=l.shape;l.shape=l.shape.slice(),l.shape[l.shape.length-2]++,w.assert(ip(l.shape,k.shape),()=>`packed reshape ${l.shape} to ${k.shape} isn't free`);let C=fe({inputs:{x:t},backend:r,attrs:{shape:[1,n.inChannels,n.outChannels]}});b.push(C);let E=Rm({a:k,b:C,backend:r,transposeA:f,transposeB:m,bias:s,activation:i,preluActivationWeights:a,leakyreluAlpha:o}),F=r.texData.get(E.dataId);w.assert(F.isPacked,()=>"batchMatMul result is expected to be packed"),l.shape=T,F.shape=n.outShape,g=cr({inputs:{x:E},backend:r}),g.shape=n.outShape,b.push(E)}else{let x=h?u[0]*u[1]*u[2]:u[0]*u[2]*u[3],k=fe({inputs:{x:e},backend:r,attrs:{shape:[1,x,n.inChannels]}}),T=fe({inputs:{x:t},backend:r,attrs:{shape:[1,n.inChannels,n.outChannels]}}),C=Rm({a:k,b:T,transposeA:f,transposeB:m,backend:r,bias:s,activation:i,preluActivationWeights:a,leakyreluAlpha:o});g=fe({inputs:{x:C},backend:r,attrs:{shape:n.outShape}}),b.push(k),b.push(T),b.push(C)}for(let x of b)r.disposeIntermediateTensorInfo(x);return g}function z_({x:e,filter:t,convInfo:n,backend:r,bias:s=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let{filterWidth:u,filterHeight:l,inChannels:c,outWidth:d,outHeight:p,dataFormat:h}=n,f=h==="channelsLast",m=u*l*c,g=p*d,b=[m,g],y=!0,v=!1,x=[],k=fe({inputs:{x:e},backend:r,attrs:{shape:e.shape.slice(1)}}),T=fe({inputs:{x:t},backend:r,attrs:{shape:[1,m,w.sizeFromShape(t.shape)/m]}});x.push(k),x.push(T);let C=new AJ(b,n),E=[k.shape,[n.padInfo.top,n.padInfo.left],[n.strideHeight,n.strideWidth],[n.dilationHeight,n.dilationWidth],[n.inChannels],[n.filterWidth*n.inChannels],[n.outWidth]],F=r.runWebGLProgram(C,[k],"float32",E),O=fe({inputs:{x:F},backend:r,attrs:{shape:[1,b[0],b[1]]}});x.push(F),x.push(O);let D=s!=null,R=a!=null,_=i==="leakyrelu",L=i?$m(i,!0):null,U=new I_(O.shape,T.shape,[1,g,n.outChannels],y,v,D,L,R,_),j=[O,T];if(s&&j.push(s),R&&j.push(a),_){let ee=r.makeTensorInfo([],"float32",w.createScalarValue(o,"float32"));j.push(ee),x.push(ee)}let K=r.runWebGLProgram(U,j,"float32"),q=f?[1,p,d,n.outChannels]:[1,n.outChannels,p,d],Q=fe({inputs:{x:K},backend:r,attrs:{shape:q}});x.push(K);for(let ee of x)r.disposeIntermediateTensorInfo(ee);return Q}function $J(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dataFormat:u,dilations:l,dimRoundingMode:c}=r,d=N.convertConv2DDataFormat(u),p=N.computeConv2DInfo(s.shape,a.shape,o,l,i,c,!1,d),h;if(p.filterHeight===1&&p.filterWidth===1&&p.dilationHeight===1&&p.dilationWidth===1&&p.strideHeight===1&&p.strideWidth===1&&(p.padInfo.type==="SAME"||p.padInfo.type==="VALID"))h=B_({x:s,filter:a,convInfo:p,backend:n});else if(X().getBool("WEBGL_CONV_IM2COL")&&s.shape[0]===1)h=z_({x:s,filter:a,convInfo:p,backend:n});else{let m=new L_(p);h=n.runWebGLProgram(m,[s,a],"float32")}let f=fe({inputs:{x:h},backend:n,attrs:{shape:p.outShape}});return n.disposeIntermediateTensorInfo(h),f}var FJ={kernelName:ga,backendName:"webgl",kernelFunc:$J},DJ=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,s=e.padInfo.left,a=e.dataFormat==="channelsLast";this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
ivec4 coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
int wR = coords.x;
int wC = coords.y;
int d1 = coords.z;
int d2 = coords.w;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${t} - ${r};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${n} - ${s};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${a}) {
float dyValue = getDy(b, yR, yC, d2);
float xValue = getX(b, xR, xC, d1);
dotProd += (xValue * dyValue);
} else {
float dyValue = getDy(b, d2, yR, yC);
float xValue = getX(b, d1, xR, xC);
dotProd += (xValue * dyValue);
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
}
}
}
setOutput(dotProd);
}
2021-12-01 21:37:52 +01:00
`}},RJ=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,s=e.strideWidth,a=e.dataFormat==="channelsLast",o=t-1-e.padInfo.top,i=n-1-e.padInfo.left,u=a?1:2,l=a?2:3,c=a?3:1;this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec2 pads = ivec2(${o}, ${i});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords[0];
2021-12-01 21:37:52 +01:00
int d1 = coords[${c}];
2021-09-11 17:11:38 +02:00
2021-12-01 21:37:52 +01:00
ivec2 dyCorner = ivec2(coords[${u}], coords[${l}]) - pads;
2021-09-11 17:11:38 +02:00
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / ${r}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
int idyR = int(dyR);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wRPerm = ${t} - 1 - wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${n}; wC++) {
float dyC = float(dyCCorner + wC) / ${s}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
2021-04-01 19:39:54 +02:00
continue;
}
2021-09-11 17:11:38 +02:00
int idyC = int(dyC);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wCPerm = ${n} - 1 - wC;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int d2 = 0; d2 < ${e.outChannels}; d2++) {
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (${a}) {
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
} else {
float xValue = getDy(batch, d2, idyR, idyC);
float wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
}
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},PJ=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideDepth,n=e.strideHeight,r=e.strideWidth,s=e.padInfo.front,a=e.padInfo.top,o=e.padInfo.left;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int wF = coords.x;
int wR = coords.y;
int wC = coords.z;
int d1 = coords.w;
int d2 = coords.u;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float dotProd = 0.0;
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yF = 0; yF < ${e.outDepth}; yF++) {
int xF = wF + yF * ${t} - ${s};
if (xF < 0 || xF >= ${e.inDepth}) {
continue;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${n} - ${a};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${r} - ${o};
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float dyValue = getDy(b, yF, yR, yC, d2);
float xValue = getX(b, xF, xR, xC, d1);
dotProd += (xValue * dyValue);
}
2021-04-01 19:39:54 +02:00
}
}
}
2021-09-11 17:11:38 +02:00
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},OJ=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterDepth,n=e.filterHeight,r=e.filterWidth,s=e.strideDepth,a=e.strideHeight,o=e.strideWidth,i=t-1-e.padInfo.front,u=n-1-e.padInfo.top,l=r-1-e.padInfo.left;this.userCode=`
const ivec3 pads = ivec3(${i}, ${u}, ${l});
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyFCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
float dotProd = 0.0;
for (int wF = 0; wF < ${t}; wF++) {
float dyF = float(dyFCorner + wF) / ${s}.0;
if (dyF < 0.0 || dyF >= ${e.outDepth}.0 || fract(dyF) > 0.0) {
continue;
}
int idyF = int(dyF);
int wFPerm = ${t} - 1 - wF;
for (int wR = 0; wR < ${n}; wR++) {
float dyR = float(dyRCorner + wR) / ${a}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
int wRPerm = ${n} - 1 - wR;
for (int wC = 0; wC < ${r}; wC++) {
float dyC = float(dyCCorner + wC) / ${o}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
int wCPerm = ${r} - 1 - wC;
for (int d2 = 0; d2 < ${e.outChannels}; d2++) {
float xValue = getDy(batch, idyF, idyR, idyC, d2);
float wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
}
}
2021-04-01 19:39:54 +02:00
}
}
2021-09-11 17:11:38 +02:00
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function MJ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,dy:a}=t,{strides:o,pad:i,dataFormat:u,dimRoundingMode:l,filterShape:c}=r,d=N.convertConv2DDataFormat(u),p=N.computeConv2DInfo(s.shape,c,o,1,i,l,!1,d),h=new DJ(p);return n.runWebGLProgram(h,[s,a],"float32")}var LJ={kernelName:vh,backendName:"webgl",kernelFunc:MJ};function BJ(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{inputShape:o,strides:i,pad:u,dataFormat:l,dimRoundingMode:c}=r,d=N.convertConv2DDataFormat(l),p=N.computeConv2DInfo(o,a.shape,i,1,u,c,!1,d),h=new RJ(p);return n.runWebGLProgram(h,[s,a],"float32")}var zJ={kernelName:ba,backendName:"webgl",kernelFunc:BJ};function WJ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u}=r,l=N.computeConv3DInfo(s.shape,a.shape,o,u,i),c=new EJ(l);return n.runWebGLProgram(c,[s,a],"float32")}var VJ={kernelName:Yl,backendName:"webgl",kernelFunc:WJ};function UJ(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,dy:a}=t,{strides:o,pad:i,filterShape:u}=r,l=N.computeConv3DInfo(s.shape,u,o,1,i),c=new PJ(l);return n.runWebGLProgram(c,[s,a],"float32")}var GJ={kernelName:xh,backendName:"webgl",kernelFunc:UJ};function HJ(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{pad:o,strides:i,inputShape:u}=r,l=N.computeConv3DInfo(u,a.shape,i,1,o),c=new OJ(l);return n.runWebGLProgram(c,[s,a],"float32")}var jJ={kernelName:wh,backendName:"webgl",kernelFunc:HJ},qJ=k_+`
2021-09-11 17:11:38 +02:00
return cos(x);
2021-12-01 21:37:52 +01:00
`,KJ=Ze({opSnippet:qJ}),XJ={kernelName:ya,backendName:"webgl",kernelFunc:KJ},YJ=`
2021-09-11 17:11:38 +02:00
float e2x = exp(-x);
return (e2x + 1.0 / e2x) / 2.0;
2021-12-01 21:37:52 +01:00
`,QJ=Ze({opSnippet:YJ}),ZJ={kernelName:va,backendName:"webgl",kernelFunc:QJ},JJ=class{constructor(e,t,n,r,s){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];let[a,o,i,u]=e,[l]=t,[c,d]=n;this.outputShape=[l,c,d,u];let p=r==="bilinear"?1:0,[h,f]=[`${o-1}.0`,`${i-1}.0`],[m,g,b]=c>1?[`${(o-1)/(c-1)}`,"(y2-y1) * height_ratio",`y1*${h} + float(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${h}`],[y,v,x]=d>1?[`${(i-1)/(d-1)}`,"(x2-x1) * width_ratio",`x1*${f} + float(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${f}`];this.userCode=`
2021-09-11 17:11:38 +02:00
const float height_ratio = float(${m});
2021-12-01 21:37:52 +01:00
const float width_ratio = float(${y});
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
ivec4 coords = getOutputCoords();
int b = coords[0];
int y = coords[1];
int x = coords[2];
int d = coords[3];
// get box vals
float y1 = getBoxes(b,0);
float x1 = getBoxes(b,1);
float y2 = getBoxes(b,2);
float x2 = getBoxes(b,3);
// get image in batch index
int bInd = round(getBoxInd(b));
if(bInd < 0 || bInd >= ${a}) {
return;
2021-04-01 19:39:54 +02:00
}
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
float height_scale = ${g};
2021-12-01 21:37:52 +01:00
float width_scale = ${v};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float in_y = ${b};
if( in_y < 0.0 || in_y > ${h} ) {
setOutput(float(${s}));
return;
}
float in_x = ${x};
if( in_x < 0.0 || in_x > ${f} ) {
setOutput(float(${s}));
return;
}
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
vec2 sourceFracIndexCR = vec2(in_x,in_y);
if(${p} == 1) {
// Compute the four integer indices.
ivec2 sourceFloorCR = ivec2(sourceFracIndexCR);
ivec2 sourceCeilCR = ivec2(ceil(sourceFracIndexCR));
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
float topLeft = getImage(b, sourceFloorCR.y, sourceFloorCR.x, d);
float bottomLeft = getImage(b, sourceCeilCR.y, sourceFloorCR.x, d);
float topRight = getImage(b, sourceFloorCR.y, sourceCeilCR.x, d);
float bottomRight = getImage(b, sourceCeilCR.y, sourceCeilCR.x, d);
vec2 fracCR = sourceFracIndexCR - vec2(sourceFloorCR);
float top = topLeft + (topRight - topLeft) * fracCR.x;
float bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;
float newValue = top + (bottom - top) * fracCR.y;
setOutput(newValue);
} else {
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestCR = ivec2(floor(
sourceFracIndexCR + vec2(0.5,0.5)));
float newValue = getImage(b, sourceNearestCR.y, sourceNearestCR.x, d);
setOutput(newValue);
}
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},eee=e=>{let{inputs:t,backend:n,attrs:r}=e,{image:s,boxes:a,boxInd:o}=t,{cropSize:i,method:u,extrapolationValue:l}=r,c=new JJ(s.shape,a.shape,i,u,l);return n.runWebGLProgram(c,[s,a,o],"float32")},tee={kernelName:jo,backendName:"webgl",kernelFunc:eee},W_=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"index",type:"float"}],this.outputShape=e;let r=e.length,s=t?"0.0":`getX(${V_(r,"coords")})`,a=e[e.length-1],o="",i="";t?(o=n?`end != ${a-1}`:"end != 0",i=n?"end + 1":"end - 1"):(o=n?`end + pow2 < ${a}`:"end >= pow2",i=n?"end + pow2":"end - pow2"),this.userCode=`
2021-04-01 19:39:54 +02:00
void main() {
2021-12-01 21:37:52 +01:00
${gt(r)} coords = getOutputCoords();
int end = ${U_(r,"coords")};
2021-09-11 17:11:38 +02:00
float val = ${s};
int pow2 = int(pow(2.0, index));
if (${o}) {
int idx = ${i};
2021-12-01 21:37:52 +01:00
${U_(r,"coords")} = idx;
val += getX(${V_(r,"coords")});
2021-09-11 17:11:38 +02:00
}
setOutput(val);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function V_(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 U_(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 nee(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,exclusive:o,reverse:i}=r,u=s.shape.length,l=N.getAxesPermutation([a],u),c=s;l!=null&&(c=Dn({inputs:{x:s},backend:n,attrs:{perm:l}}));let d=N.getInnerMostAxes(1,u)[0];if(d!==u-1)throw new Error(`WebGL cumsum shader expects an inner-most axis=${s.shape.length-1} but got axis=${a}`);let p=c.shape[d],h=cr({inputs:{x:c},backend:n});for(let f=0;f<=Math.ceil(Math.log2(p))-1;f++){let m=new W_(c.shape,!1,i),g=[[f]],b=h;h=n.runWebGLProgram(m,[h],h.dtype,g),n.disposeIntermediateTensorInfo(b)}if(o){let f=new W_(c.shape,o,i),m=h;h=n.runWebGLProgram(f,[h],h.dtype),n.disposeIntermediateTensorInfo(m)}if(l!=null){let f=N.getUndoAxesPermutation(l),m=Dn({inputs:{x:h},backend:n,attrs:{perm:f}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(c),m}return h}var ree={kernelName:Ho,backendName:"webgl",kernelFunc:nee};function see(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,weights:a}=t,{size:o,binaryOutput:i}=r;if(s.shape.length===1){let u=n.readSync(s.dataId),l=n.readSync(a.dataId),c=o_(u,l,a.dtype,a.shape,o);return n.makeTensorInfo([o],a.dtype,c)}else if(s.shape.length===2){let u=n.bufferSync(s),l=n.bufferSync(a),c=v9(u,l,o,i);return n.makeTensorInfo(c.shape,a.dtype,c.values)}throw new Error(`Error in denseBincount: input must be at most rank 2, but got rank${s.shape.length}.`)}var aee={kernelName:kh,backendName:"webgl",kernelFunc:see},oee=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int h = ${this.getHeightCoordString()};
int w = ${this.getWidthCoordString()};
int d = ${this.getDepthCoordString()};
int in_h = h / ${t};
int offset_h = imod(h, ${t});
int in_w = w / ${t};
int offset_w = imod(w, ${t});
int offset_d = (offset_h * ${t} + offset_w) *
${this.getOutputDepthSize()};
int in_d = d + offset_d;
float result = ${this.getInputSamplingString()};
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +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 iee(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockSize:a,dataFormat:o}=r,i=s.shape[0],u=o==="NHWC"?s.shape[1]:s.shape[2],l=o==="NHWC"?s.shape[2]:s.shape[3],c=o==="NHWC"?s.shape[3]:s.shape[1],d=u*a,p=l*a,h=c/(a*a),f=o==="NHWC"?[i,d,p,h]:[i,h,d,p],m=new oee(f,a,o);return n.runWebGLProgram(m,[s],s.dtype)}var uee={kernelName:qo,backendName:"webgl",kernelFunc:iee},G_=class{constructor(e,t=!1,n=null,r=!1,s=!1){this.variableNames=["x","W"],this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=qn(this.outputShape.length);let a=e.filterHeight,o=e.filterWidth,i=e.outChannels/e.inChannels,u="",l="";n&&(r?u=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getPreluActivationWeightsAtOutCoords();
${n}
2021-12-01 21:37:52 +01:00
}`:s?u=`float activation(float a) {
2021-09-11 17:11:38 +02:00
float b = getLeakyreluAlphaAtOutCoords();
${n}
2021-12-01 21:37:52 +01:00
}`:u=`
2021-09-11 17:11:38 +02:00
float activation(float x) {
${n}
}
2021-12-01 21:37:52 +01:00
`,l="result = activation(result);");let c=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${u}
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
int d1 = d2 / ${i};
int q = d2 - d1 * ${i};
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
// TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.
for (int wR = 0; wR < ${a}; wR++) {
int xR = xRCorner + wR * dilations[0];
if (xR < 0 || xR >= inDims[0]) {
continue;
}
for (int wC = 0; wC < ${o}; wC++) {
int xC = xCCorner + wC * dilations[1];
if (xC < 0 || xC >= inDims[1]) {
continue;
}
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
float xVal = getX(batch, xR, xC, d1);
float wVal = getW(wR, wC, d1, q);
dotProd += xVal * wVal;
}
}
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
float result = dotProd;
2021-12-01 21:37:52 +01:00
${c}
2021-10-22 15:06:43 +02:00
${l}
2021-09-11 17:11:38 +02:00
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`}},H_=class{constructor(e,t=!1,n=null,r=!1,s=!1){this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"pads",type:"ivec2"},{name:"strides",type:"ivec2"},{name:"dilations",type:"ivec2"},{name:"inDims",type:"ivec2"}],this.outputShape=e.outShape,this.enableShapeUniforms=qn(this.outputShape.length);let a=e.outChannels/e.inChannels,o=e.padInfo.left,i=e.strideWidth,u=e.dilationWidth,l=e.filterHeight,c=e.filterWidth,d=c,p=`
2021-09-11 17:11:38 +02:00
int xR; int xC; int xCOffset;
2021-12-01 21:37:52 +01:00
vec4 wTexel; vec4 previous; vec4 final;`;for(let g=0;g<c;g++)p+=`
2021-09-11 17:11:38 +02:00
vec4 xTexelC${g*2};
int xTexelC${g*2}Ready;
vec4 xTexelC${g*2+1};
int xTexelC${g*2+1}Ready;
2021-10-22 15:06:43 +02:00
vec4 xC${g};`;p+=`
for (int r = 0; r < ${l}; r++) {
2021-12-01 21:37:52 +01:00
`;for(let g=0;g<c;g++)p+=`
2021-10-22 15:06:43 +02:00
xTexelC${g*2} = vec4(0.0);
xTexelC${g*2}Ready = 0;
xTexelC${g*2+1} = vec4(0.0);
xTexelC${g*2+1}Ready = 0;
xC${g} = vec4(0.0);`;p+=`
xR = xRCorner + r * dilations[0];
2021-09-11 17:11:38 +02:00
if (xR >=0 && xR < inDims[0]) {
2021-10-22 15:06:43 +02:00
`;for(let g=0;g<(d+1)/2;g++){let b=g*2;if(p+=`
2021-12-01 21:37:52 +01:00
xC = xCCorner + ${b*u};
`,i===1){if(b<c&&(o%2==1?(p+=`
2021-09-11 17:11:38 +02:00
xCOffset = xC + 1;
2021-10-22 15:06:43 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b}.zw = vec2(0.0);
2021-09-11 17:11:38 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
`,u===1&&b>0?p+=`
2021-10-22 15:06:43 +02:00
xC${b} = vec4(xTexelC${b-2}.zw, xTexelC${b}.xy);
2021-09-11 17:11:38 +02:00
`:p+=`
xCOffset = xC + 1 - 2;
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1]) {
previous = getX(batch, xR, xCOffset, d1);
2021-07-29 15:05:49 +02:00
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
previous.zw = vec2(0.0);
}
2021-07-29 15:05:49 +02:00
2021-10-22 15:06:43 +02:00
xC${b} = vec4(previous.zw, xTexelC${b}.xy);
2021-04-01 19:39:54 +02:00
} else {
2021-10-22 15:06:43 +02:00
xC${b} = vec4(0.0, 0.0, xTexelC${b}.xy);
2021-09-11 17:11:38 +02:00
}
`):p+=`
2021-10-22 15:06:43 +02:00
if (xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
2021-09-11 17:11:38 +02:00
if (xC + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b}.zw = vec2(0.0);
2021-09-11 17:11:38 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2021-10-22 15:06:43 +02:00
xC${b} = xTexelC${b};
2021-12-01 21:37:52 +01:00
`,b+1<c)){let y=o%2==0?w.nearestLargerEven(u):u;u%2==0&&o%2==1||u%2!=0&&o%2!=1?(p+=`
xCOffset = xC + imod(pads[1], 2) + ${y};
2021-09-11 17:11:38 +02:00
2021-10-22 15:06:43 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b+1}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b+1}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`,u>1&&(p+=`
2021-09-11 17:11:38 +02:00
xCOffset -= 2;
2021-10-22 15:06:43 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
xTexelC${b}Ready = 1;
2021-09-11 17:11:38 +02:00
}
`),p+=`
2021-10-22 15:06:43 +02:00
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.xy);
2021-12-01 21:37:52 +01:00
`):y===1?p+=`
2021-10-22 15:06:43 +02:00
xC${b+1} = xTexelC${b};
2021-09-11 17:11:38 +02:00
`:p+=`
2021-12-01 21:37:52 +01:00
xCOffset = xC + ${y};
2021-09-11 17:11:38 +02:00
2021-10-22 15:06:43 +02:00
if (xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
if (xCOffset + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b+1}.zw = vec2(0.0);
2021-09-11 17:11:38 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b+1}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
2021-10-22 15:06:43 +02:00
xC${b+1} = xTexelC${b+1};
2021-12-01 21:37:52 +01:00
`}}else b<c&&(o%2==1?(p+=`
2021-09-11 17:11:38 +02:00
xCOffset = xC + 1 - strides[1];
2021-10-22 15:06:43 +02:00
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xCOffset + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
2021-10-22 15:06:43 +02:00
if(xC + 1 >= 0 && xC + 1 < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xC + 1, d1);
2021-09-11 17:11:38 +02:00
// Need to manually clear unused channels in case
// we're reading from recycled texture.
if (xC + 2 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b+1}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b+1}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2021-10-22 15:06:43 +02:00
xC${b} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
2021-12-01 21:37:52 +01:00
`,b+1<c&&(p+=`
2021-09-11 17:11:38 +02:00
final = vec4(0.0);
xCOffset = xC + 1 + strides[1];
if(xCOffset >= 0 && xCOffset < inDims[1]) {
final = getX(batch, xR, xCOffset, d1);
}
2021-10-22 15:06:43 +02:00
xC${b+1} = vec4(xTexelC${b+1}.xy, final.xy);
2021-09-11 17:11:38 +02:00
`)):(p+=`
2021-10-22 15:06:43 +02:00
if(xC >= 0 && xC < inDims[1] && xTexelC${b}Ready == 0) {
xTexelC${b} = getX(batch, xR, xC, d1);
2021-09-11 17:11:38 +02:00
if (xC + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b}.zw = vec2(0.0);
2021-04-01 19:39:54 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b}Ready = 1;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
xCOffset = xC + strides[1];
2021-10-22 15:06:43 +02:00
if(xCOffset >= 0 && xCOffset < inDims[1] && xTexelC${b+1}Ready == 0) {
xTexelC${b+1} = getX(batch, xR, xCOffset, d1);
2021-09-11 17:11:38 +02:00
if (xCOffset + 1 >= inDims[1]) {
2021-10-22 15:06:43 +02:00
xTexelC${b+1}.zw = vec2(0.);
2021-09-11 17:11:38 +02:00
}
2021-10-22 15:06:43 +02:00
xTexelC${b+1}Ready = 1;
2021-09-11 17:11:38 +02:00
}
2021-04-01 19:39:54 +02:00
2021-10-22 15:06:43 +02:00
xC${b} = vec4(
xTexelC${b}.xy, xTexelC${b+1}.xy);
2021-12-01 21:37:52 +01:00
`,b+1<c&&(p+=`
2021-10-22 15:06:43 +02:00
xC${b+1} = vec4(xTexelC${b}.zw, xTexelC${b+1}.zw);
2021-12-01 21:37:52 +01:00
`)));b<c&&(p+=`
2021-10-22 15:06:43 +02:00
wTexel = getW(r, ${b}, d1, q);
dotProd += xC${b} * vec4(wTexel.xz, wTexel.xz);
2021-12-01 21:37:52 +01:00
`,b+1<c&&(p+=`
2021-10-22 15:06:43 +02:00
wTexel = getW(r, ${b+1}, d1, q);
dotProd += xC${b+1} * vec4(wTexel.xz, wTexel.xz);
2021-09-11 17:11:38 +02:00
`))}p+=`
2021-10-22 15:06:43 +02:00
}
`,p+=`
}
`;let h="",f="";n&&(r?h=`vec4 activation(vec4 a) {
2021-09-11 17:11:38 +02:00
vec4 b = getPreluActivationWeightsAtOutCoords();
${n}
}`:s?h=`vec4 activation(vec4 a) {
vec4 b = getLeakyreluAlphaAtOutCoords();
${n}
}`:h=`vec4 activation(vec4 x) {
${n}
}`,f="result = activation(result);");let m=t?"result += getBiasAtOutCoords();":"";t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),s&&this.variableNames.push("leakyreluAlpha"),this.userCode=`
${h}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
ivec2 xRCCorner = coords.yz * strides - pads;
int d2 = coords.w;
int d1 = d2 / ${a};
int q = d2 - d1 * ${a};
int xRCorner = xRCCorner.x;
int xCCorner = xRCCorner.y;
//intialize dotProd with a small epsilon seems to reduce GPU accuracy loss.
vec4 dotProd = vec4(0.000000000000001);
${p}
vec4 result = dotProd - vec4(0.000000000000001);
${m}
${f}
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function cee(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u,dimRoundingMode:l}=r,c=u;c==null&&(c=[1,1]),w.assert(N.eitherStridesOrDilationsAreOne(o,c),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${o} and dilations '${c}'`);let d=N.computeConv2DInfo(s.shape,a.shape,o,c,i,l,!0),p;X().getBool("WEBGL_PACK_DEPTHWISECONV")&&d.strideWidth<=2&&d.outChannels/d.inChannels==1?p=new H_(d):p=new G_(d);let h=[[d.padInfo.top,d.padInfo.left],[d.strideHeight,d.strideWidth],[d.dilationHeight,d.dilationWidth],[d.inHeight,d.inWidth]];return n.runWebGLProgram(p,[s,a],"float32",h)}var lee={kernelName:xa,backendName:"webgl",kernelFunc:cee},dee=class{constructor(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;let t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,s=e.padInfo.left,a=e.outChannels/e.inChannels;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int wR = coords.x;
int wC = coords.y;
int d1 = coords.z;
int dm = coords.w;
int d2 = d1 * ${a} + dm;
float dotProd = 0.0;
// TO DO: Vec4 over the batch size
for (int b = 0; b < ${e.batchSize}; b++) {
for (int yR = 0; yR < ${e.outHeight}; yR++) {
int xR = wR + yR * ${t} - ${r};
if (xR < 0 || xR >= ${e.inHeight}) {
continue;
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int yC = 0; yC < ${e.outWidth}; yC++) {
int xC = wC + yC * ${n} - ${s};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (xC < 0 || xC >= ${e.inWidth}) {
continue;
}
float dyValue = getDy(b, yR, yC, d2);
float xValue = getX(b, xR, xC, d1);
dotProd += (xValue * dyValue);
}
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},pee=class{constructor(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;let t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,s=e.strideWidth,a=t-1-e.padInfo.top,o=n-1-e.padInfo.left,i=e.outChannels/e.inChannels;this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec2 pads = ivec2(${a}, ${o});
2021-04-01 19:39:54 +02:00
void main() {
2021-09-11 17:11:38 +02:00
ivec4 coords = getOutputCoords();
2021-04-01 19:39:54 +02:00
int batch = coords[0];
2021-09-11 17:11:38 +02:00
int d1 = coords[3];
ivec2 dyCorner = coords.yz - pads;
int dyRCorner = dyCorner.x;
int dyCCorner = dyCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float dotProd = 0.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wR = 0; wR < ${t}; wR++) {
float dyR = float(dyRCorner + wR) / ${r}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wRPerm = ${t} - 1 - wR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
for (int wC = 0; wC < ${n}; wC++) {
float dyC = float(dyCCorner + wC) / ${s}.0;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
int wCPerm = ${n} - 1 - wC;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// TO DO: Vec4 over the channelMul
for (int dm = 0; dm < ${i}; dm++) {
int d2 = d1 * ${i} + dm;
float xValue = getDy(batch, idyR, idyC, d2);
float wValue = getW(wRPerm, wCPerm, d1, dm);
dotProd += xValue * wValue;
}
}
}
setOutput(dotProd);
}
2021-12-01 21:37:52 +01:00
`}};function hee(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,dy:a}=t,{strides:o,dilations:i,pad:u,dimRoundingMode:l,filterShape:c}=r,d=N.computeConv2DInfo(s.shape,c,o,i,u,l,!0),p=new dee(d);return n.runWebGLProgram(p,[s,a],"float32")}var fee={kernelName:Ih,backendName:"webgl",kernelFunc:hee};function mee(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{strides:o,dilations:i,pad:u,dimRoundingMode:l,inputShape:c}=r,d=N.computeConv2DInfo(c,a.shape,o,i,u,l,!0),p=new pee(d);return n.runWebGLProgram(p,[s,a],"float32")}var gee={kernelName:Sh,backendName:"webgl",kernelFunc:mee},bee=class{constructor(e){this.variableNames=["X"],this.outputShape=[e,e],this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
float val = coords[0] == coords[1] ? getX(coords[0]) : 0.0;
setOutput(val);
}
2021-12-01 21:37:52 +01:00
`}};function yee(e){let{inputs:t,backend:n}=e,{x:r}=t,s=[...r.shape,...r.shape],a=w.sizeFromShape(r.shape),o=fe({inputs:{x:r},backend:n,attrs:{shape:[a]}}),i=new bee(a),u=n.runWebGLProgram(i,[o],o.dtype),l=fe({inputs:{x:u},backend:n,attrs:{shape:s}});return n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(u),l}var vee={kernelName:Ch,backendName:"webgl",kernelFunc:yee},xee=class{constructor(e){this.variableNames=["x","W"],this.outputShape=e.outShape;let{inHeight:t,inWidth:n,padInfo:r,strideHeight:s,strideWidth:a,filterHeight:o,filterWidth:i,dilationHeight:u,dilationWidth:l}=e,{top:c,left:d}=r;this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec2 strides = ivec2(${s}, ${a});
2021-12-01 21:37:52 +01:00
const ivec2 pads = ivec2(${c}, ${d});
2021-09-11 17:11:38 +02:00
const float neg_infinity = -3.4e38;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
int d1 = coords.w;
ivec2 outTopLeftCorner =
coords.yz * strides - pads;
int hBeg = outTopLeftCorner.x;
int wBeg = outTopLeftCorner.y;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
float curVal = neg_infinity;
for (int h = 0; h < ${o}; h++) {
2021-12-01 21:37:52 +01:00
int hIn = hBeg + h * ${u};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
if (hIn >= 0 && hIn < ${t}) {
for (int w = 0; w < ${i}; w++) {
2021-10-22 15:06:43 +02:00
int wIn = wBeg + w * ${l};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (wIn >= 0 && wIn < ${n}) {
float xVal = getX(batch, hIn, wIn, d1);
float wVal = getW(h, w, d1);
float val = xVal + wVal;
if (val > curVal) {
curVal = val;
}
}
}
}
2021-07-29 15:05:49 +02:00
}
2021-09-11 17:11:38 +02:00
float result = curVal;
setOutput(result);
2021-07-29 15:05:49 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function wee(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u}=r,l=N.computeDilation2DInfo(s.shape,a.shape,o,i,"NHWC",u),c,d=new xee(l);c=n.runWebGLProgram(d,[s,a],"float32");let p=fe({inputs:{x:c},backend:n,attrs:{shape:l.outShape}});return n.disposeIntermediateTensorInfo(c),p}var kee={kernelName:Ql,backendName:"webgl",kernelFunc:wee};function Iee(e){let{inputs:t,backend:n,attrs:r}=e,{equation:s}=r,a=t,{allDims:o,summedDims:i,idDims:u}=N.decodeEinsumEquation(s,a.length);N.checkEinsumDimSizes(o.length,u,a);let{path:l,steps:c}=N.getEinsumComputePath(i,u),d=c.length,p=null,h=o.length,f=[];for(let m=0;m<d;++m){for(let g of c[m]){let{permutationIndices:b,expandDims:y}=N.getEinsumPermutation(h,u[g]),v;N.isIdentityPermutation(b)?v=a[g]:(v=Dn({inputs:{x:a[g]},backend:n,attrs:{perm:b}}),f.push(v));let x=v.shape.slice();for(let k=0;k<y.length;++k)x.splice(y[k],0,1);w.arraysEqual(v.shape,x)||(v=fe({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),p===null?p=v:(p=uk({inputs:{a:v,b:p},backend:n}),f.push(p))}m<d-1&&(l[m]>=0&&(p=Dm({inputs:{x:p},backend:n,attrs:{axis:l[m]-(o.length-h),keepDims:!1}}),f.push(p)),h--)}for(let m of f)m!==p&&n.disposeIntermediateTensorInfo(m);return p}var See={kernelName:Zl,backendName:"webgl",kernelFunc:Iee},Cee="return (x >= 0.0) ? x : (exp(x) - 1.0);",Tee=`
2021-09-11 17:11:38 +02:00
vec4 result;
result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);
return result;
2021-12-01 21:37:52 +01:00
`,Nee=Ze({opSnippet:Cee,packedOpSnippet:Tee}),_ee={kernelName:ka,backendName:"webgl",kernelFunc:Nee},Eee="return (b >= 1.0) ? a : a * (b + 1.0);",Aee=`
2021-09-11 17:11:38 +02:00
vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));
return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));
2021-12-01 21:37:52 +01:00
`,$ee=e=>{let{inputs:t,backend:n}=e,{dy:r,y:s}=t,a=X().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new cp(Aee,r.shape,s.shape):new Yc(Eee,r.shape,s.shape);return n.runWebGLProgram(a,[r,s],r.dtype)},Fee={kernelName:_h,backendName:"webgl",kernelFunc:$ee},Dee=`
2021-09-11 17:11:38 +02:00
return vec4(equal(a, b));
2021-12-01 21:37:52 +01:00
`,Ree="return float(a == b);",Pee=hn({opSnippet:Ree,packedOpSnippet:Dee,dtype:"bool",cpuKernelImpl:k9}),Oee={kernelName:Ko,backendName:"webgl",kernelFunc:Pee},Mee=`
2021-09-11 17:11:38 +02:00
// Error function is calculated approximately with elementary function.
// See "Handbook of Mathematical Functions with Formulas,
// Graphs, and Mathematical Tables", Abramowitz and Stegun.
2021-12-01 21:37:52 +01:00
float p = ${N.ERF_P};
float a1 = ${N.ERF_A1};
float a2 = ${N.ERF_A2};
float a3 = ${N.ERF_A3};
float a4 = ${N.ERF_A4};
float a5 = ${N.ERF_A5};
2021-09-11 17:11:38 +02:00
float sign = sign(x);
x = abs(x);
float t = 1.0 / (1.0 + p * x);
return sign * (1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x));
2021-12-01 21:37:52 +01:00
`,Lee=Ze({opSnippet:Mee}),Bee={kernelName:Zu,backendName:"webgl",kernelFunc:Lee},j_="return exp(x);",q_=Ze({opSnippet:j_,packedOpSnippet:j_,cpuKernelImpl:I9,dtype:"float32"}),zee={kernelName:Ia,backendName:"webgl",kernelFunc:q_};function pk(e){let{inputs:t,attrs:n,backend:r}=e,{dim:s}=n,{input:a}=t,o=a.shape.length,i=a.shape.slice(),u=s;return s<0&&(w.assert(-(o+1)<=s,()=>`Axis must be in the interval [${-(o+1)}, ${o}]`),u=o+s+1),i.splice(u,0,1),fe({inputs:{x:a},backend:r,attrs:{shape:i}})}var Wee={kernelName:Xo,backendName:"webgl",kernelFunc:pk},K_="return exp(x) - 1.0;",Vee=Ze({opSnippet:K_,packedOpSnippet:K_,cpuKernelImpl:S9}),Uee={kernelName:Yo,backendName:"webgl",kernelFunc:Vee},X_=class{constructor(e,t,n){this.variableNames=["real","imag"];let r=t[1];this.outputShape=t;let s=n?`2.0 * ${Math.PI}`:`-2.0 * ${Math.PI}`,a=n?`${r}.0`:"1.0",o;if(e==="real")o="return real * expR - imag * expI;";else if(e==="imag")o="return real * expI + imag * expR;";else throw new Error(`FFT component must be either "real" or "imag", got ${e}.`);this.userCode=`
2021-09-11 17:11:38 +02:00
const float exponentMultiplier = ${s};
float unaryOpComplex(float real, float expR, float imag, float expI) {
${o}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
float mulMatDFT(int batch, int index) {
float indexRatio = float(index) / float(${r});
float exponentMultiplierTimesIndexRatio =
exponentMultiplier * indexRatio;
float result = 0.0;
for (int i = 0; i < ${r}; i++) {
// x = (-2|2 * PI / N) * index * i;
float x = exponentMultiplierTimesIndexRatio * float(i);
float expR = cos(x);
float expI = sin(x);
float real = getReal(batch, i);
float imag = getImag(batch, i);
result +=
unaryOpComplex(real, expR, imag, expI) / ${a};
}
return result;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
setOutput(mulMatDFT(coords[0], coords[1]));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function Y_(e,t,n){let r=n.texData.get(e.dataId),s=w.sizeFromShape(e.shape),a=e.shape[e.shape.length-1],o=s/a,i=fe({inputs:{x:e},backend:n,attrs:{shape:[o,a]}}),u=i.shape,l=new X_("real",u,t),c=new X_("imag",u,t),d=[{dataId:r.complexTensorInfos.real.dataId,dtype:r.complexTensorInfos.real.dtype,shape:u},{dataId:r.complexTensorInfos.imag.dataId,dtype:r.complexTensorInfos.imag.dtype,shape:u}],p=n.runWebGLProgram(l,d,"float32"),h=n.runWebGLProgram(c,d,"float32"),f=No({inputs:{real:p,imag:h},backend:n});n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(h);let m=fe({inputs:{x:f},backend:n,attrs:{shape:e.shape}});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(f),m}function Gee(e){let{inputs:t,backend:n}=e,{input:r}=t;return Y_(r,!1,n)}var Hee={kernelName:Eh,backendName:"webgl",kernelFunc:Gee},jee=class{constructor(e,t){this.outputShape=[],this.customUniforms=[{name:"value",type:"float"}],this.variableNames=["x"],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
// Input can be obtained from uniform value.
setOutput(value);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function pp(e){let{backend:t,attrs:n}=e,{shape:r,value:s}=n,{dtype:a}=n;if(a=a||w.inferDtype(s),a==="string"){let o=w.getArrayFromDType(a,w.sizeFromShape(r));return o.fill(s),t.makeTensorInfo(r,a,o)}else{let o=new jee(r,s),i=[[s]];return t.runWebGLProgram(o,[],a,i)}}var qee={kernelName:Ju,backendName:"webgl",kernelFunc:pp},Kee=class{constructor(e){this.variableNames=["Image"],this.outputShape=[];let t=e[2];this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int x = coords[2];
int coordX = ${t} - x - 1;
float outputValue;
if(coordX >= 0 && coordX < ${t}) {
outputValue = getImage(coords[0], coords[1], coordX, coords[3]);
} else {
outputValue = getImage(coords[0], coords[1], coords[2], coords[3]);
}
setOutput(outputValue);
}
2021-12-01 21:37:52 +01:00
`}},Xee={kernelName:Qo,backendName:"webgl",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,r=t,s=new Kee(n.shape);return r.runWebGLProgram(s,[n],n.dtype)}},Q_="return floor(x);",Yee=Ze({opSnippet:Q_,packedOpSnippet:Q_,cpuKernelImpl:C9}),Qee={kernelName:Sa,backendName:"webgl",kernelFunc:Yee},Zee=`
2021-09-11 17:11:38 +02:00
float s = sign(a) * sign(b);
int ia = round(a);
int ib = round(b);
if (ib != 0) {
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
return float(idiv(ia, ib, s));
} else {
return NAN;
2021-04-30 18:01:04 +02:00
}
2021-12-01 21:37:52 +01:00
`,Jee=`
2021-09-11 17:11:38 +02:00
ivec4 ia = round(a);
ivec4 ib = round(b);
bvec4 cond = notEqual(ib, ivec4(0));
ivec4 result = ivec4(0);
vec4 s = sign(a) * sign(b);
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
if (cond[0]) {
result[0] = idiv(ia[0], ib[0], s[0]);
2021-04-30 18:01:04 +02:00
}
2021-09-11 17:11:38 +02:00
if (cond[1]) {
result[1] = idiv(ia[1], ib[1], s[1]);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if (cond[2]) {
result[2] = idiv(ia[2], ib[2], s[2]);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if (cond[3]) {
result[3] = idiv(ia[3], ib[3], s[3]);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
return vec4(result);
2021-12-01 21:37:52 +01:00
`,ete=hn({opSnippet:Zee,packedOpSnippet:Jee,dtype:"int32"}),tte={kernelName:Ca,backendName:"webgl",kernelFunc:ete},nte=class{constructor(e){this.variableNames=["A"];let t=$n(),[n,r]=e;this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
int texR = coords[0];
int texC = coords[1];
int depth = coords[2];
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${r}.0, ${n}.0);
vec4 values = ${t.texture2D}(A, uv);
float value;
if (depth == 0) {
value = values.r;
} else if (depth == 1) {
value = values.g;
} else if (depth == 2) {
value = values.b;
} else if (depth == 3) {
value = values.a;
2021-03-31 13:01:22 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(floor(value * 255.0 + 0.5));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},rte=class{constructor(e){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;let t=$n(),[n,r]=e;this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec3 coords = getOutputCoords();
int texR = coords[0];
int texC = coords[1];
int depth = coords[2];
vec4 result = vec4(0.);
for(int row=0; row<=1; row++) {
for(int col=0; col<=1; col++) {
texC = coords[1] + row;
depth = coords[2] + col;
vec2 uv = (vec2(texC, texR) + halfCR) /
vec2(${r}.0, ${n}.0);
vec4 values = ${t.texture2D}(A, uv);
float value;
if (depth == 0) {
value = values.r;
} else if (depth == 1) {
value = values.g;
} else if (depth == 2) {
value = values.b;
} else if (depth == 3) {
value = values.a;
}
result[row * 2 + col] = floor(value * 255.0 + 0.5);
}
2021-03-31 13:01:22 +02:00
}
2021-09-11 17:11:38 +02:00
${t.output} = result;
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},ste={kernelName:ld,backendName:"webgl",kernelFunc:ate},Jc;function ate(e){let{inputs:t,backend:n,attrs:r}=e,{pixels:s}=t,{numChannels:a}=r,o=typeof HTMLVideoElement!="undefined"&&s instanceof HTMLVideoElement,i=typeof HTMLImageElement!="undefined"&&s instanceof HTMLImageElement,[u,l]=o?[s.videoWidth,s.videoHeight]:[s.width,s.height],c=[l,u],d=[l,u,a];(i||o)&&(Jc==null&&(Jc=document.createElement("canvas").getContext("2d")),Jc.canvas.width=u,Jc.canvas.height=l,Jc.drawImage(s,0,0,u,l),s=Jc.canvas);let p=n.makeTensorInfo(c,"int32");n.texData.get(p.dataId).usage=wr.PIXELS,n.gpgpu.uploadPixelDataToTexture(n.getTexture(p.dataId),s);let h=X().getBool("WEBGL_PACK")?new rte(d):new nte(d),f=n.runWebGLProgram(h,[p],"int32");return n.disposeData(p.dataId),f}function ote(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a,bias:o,preluActivationWeights:i}=t,{strides:u,pad:l,dataFormat:c,dilations:d,dimRoundingMode:p,activation:h,leakyreluAlpha:f}=r,m=N.convertConv2DDataFormat(c),g=N.computeConv2DInfo(s.shape,a.shape,u,d,l,p,!1,m),b,y=[];if(g.filterHeight===1&&g.filterWidth===1&&g.dilationHeight===1&&g.dilationWidth===1&&g.strideHeight===1&&g.strideWidth===1&&(g.padInfo.type==="SAME"||g.padInfo.type==="VALID"))b=B_({x:s,filter:a,convInfo:g,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});else if(X().getBool("WEBGL_CONV_IM2COL")&&s.shape[0]===1)b=z_({x:s,filter:a,convInfo:g,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});else{let x=o!=null,k=i!=null,T=h==="leakyrelu",C=h?$m(h,!1):null,E=new L_(g,x,C,k,T),F=[s,a];if(o&&F.push(o),i&&F.push(i),T){let O=n.makeTensorInfo([],"float32",w.createScalarValue(f,"float32"));F.push(O),y.push(O)}b=n.runWebGLProgram(E,F,"float32")}let v=fe({inputs:{x:b},backend:n,attrs:{shape:g.outShape}});return y.push(b),y.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var ite={kernelName:no,backendName:"webgl",kernelFunc:ote};function ute(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a,bias:o,preluActivationWeights:i}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:d,activation:p,leakyreluAlpha:h}=r,f=[],m=c;m==null&&(m=[1,1]),w.assert(N.eitherStridesOrDilationsAreOne(u,m),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${m}'`);let g=N.computeConv2DInfo(s.shape,a.shape,u,m,l,d,!0),b=X().getBool("WEBGL_PACK_DEPTHWISECONV")&&g.strideWidth<=2&&g.outChannels/g.inChannels==1,y=p?$m(p,b):null,v=[s,a],x=o!=null,k=i!=null,T=p==="leakyrelu";if(x&&v.push(o),k&&v.push(i),T){let O=n.makeTensorInfo([],"float32",w.createScalarValue(h,"float32"));v.push(O),f.push(O)}let C;b?C=new H_(g,x,y,k,T):C=new G_(g,x,y,k,T);let E=[[g.padInfo.top,g.padInfo.left],[g.strideHeight,g.strideWidth],[g.dilationHeight,g.dilationWidth],[g.inHeight,g.inWidth]],F=n.runWebGLProgram(C,v,"float32",E);return f.forEach(O=>n.disposeIntermediateTensorInfo(O)),F}var cte={kernelName:ro,backendName:"webgl",kernelFunc:ute},lte=class{constructor(e,t,n){this.sliceDim=e,this.strides=t,this.variableNames=["x","indices"],this.outputShape=n;let r=gt(t.length),s=gt(n.length),a=this.sliceDim>1?"strides[j]":"strides";this.userCode=`
2021-09-11 17:11:38 +02:00
${r} strides = ${r}(${this.strides});
void main() {
${s} coords = getOutputCoords();
int flattenIndex = 0;
for (int j = 0; j < ${this.sliceDim}; j++) {
int index = round(getIndices(coords[0], j));
flattenIndex += index * ${a};
}
setOutput(getX(flattenIndex, coords[1]));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function dte(e){let{inputs:t,backend:n}=e,{params:r,indices:s}=t,a=s.shape,o=a[a.length-1],i=w.sizeFromShape(r.shape),[u,l,c,d]=N.prepareAndValidate(r,s),p=fe({inputs:{x:s},backend:n,attrs:{shape:[l,o]}}),h=fe({inputs:{x:r},backend:n,attrs:{shape:[w.sizeFromShape(r.shape)/c,c]}});if(n.shouldExecuteOnCPU([r,s])||r.dtype==="string"){let b=n.readSync(s.dataId),y=n.bufferSync(r),v=T9(b,y,r.dtype,l,o,c,d,r.shape,i);return n.makeTensorInfo(u,r.dtype,v.values)}let f=new lte(o,d,[l,c]),m=n.runWebGLProgram(f,[h,p],h.dtype),g=fe({inputs:{x:m},backend:n,attrs:{shape:u}});return n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(m),g}var pte={kernelName:Jo,backendName:"webgl",kernelFunc:dte},hte=class{constructor(e,t){this.variableNames=["A","indices"],this.outputShape=t,this.rank=t.length;let n=gt(this.rank),r=fte(e,2);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${n} resRC = getOutputCoords();
setOutput(getA(${r}));
}
2021-12-01 21:37:52 +01:00
`}};function fte(e,t){let n=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[];for(let s=0;s<e.length;s++)s===2?r.push("int(getIndices(resRC.x, resRC.z))"):r.push(`${n[s]}`);return r.join()}function Z_(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,indices:a}=t,{axis:o,batchDims:i}=r,u=w.parseAxisParam(o,s.shape)[0],l=n.readSync(a.dataId),c=s.shape[u];for(let x=0;x<l.length;++x){let k=l[x];w.assert(k<=c-1&&k>=0,()=>`GatherV2: the index value ${k} is not in [0, ${c-1}]`)}let d=N.segment_util.collectGatherOpShapeInfo(s,a,u,i),p=w.sizeFromShape(a.shape),h=[],f=fe({inputs:{x:s},backend:n,attrs:{shape:[d.batchSize,d.outerSize,d.dimSize,d.sliceSize]}}),m=fe({inputs:{x:a},backend:n,attrs:{shape:[d.batchSize,p/d.batchSize]}});h.push(f),h.push(m);let g=[d.batchSize,d.outerSize,p/d.batchSize,d.sliceSize];if(n.shouldExecuteOnCPU([s,a])||s.dtype==="string"){let x=n.bufferSync(m),k=n.bufferSync(f),T=N9(k,x,g);return h.forEach(C=>n.disposeIntermediateTensorInfo(C)),n.makeTensorInfo(d.outputShape,T.dtype,T.values)}let b=new hte(f.shape,g),y=n.runWebGLProgram(b,[f,m],f.dtype);h.push(y);let v=fe({inputs:{x:y},backend:n,attrs:{shape:d.outputShape}});return h.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var mte={kernelName:Zo,backendName:"webgl",kernelFunc:Z_},gte="return float(a > b);",bte=`
2021-09-11 17:11:38 +02:00
return vec4(greaterThan(a, b));
2021-12-01 21:37:52 +01:00
`,yte=hn({opSnippet:gte,packedOpSnippet:bte,cpuKernelImpl:_9,dtype:"bool"}),vte={kernelName:ei,backendName:"webgl",kernelFunc:yte},xte="return float(a >= b);",wte=`
2021-09-11 17:11:38 +02:00
return vec4(greaterThanEqual(a, b));
2021-12-01 21:37:52 +01:00
`,kte=hn({opSnippet:xte,packedOpSnippet:wte,dtype:"bool",cpuKernelImpl:E9}),Ite={kernelName:Na,backendName:"webgl",kernelFunc:kte};function Ste(e){let{inputs:t,backend:n}=e,{input:r}=t;return Y_(r,!0,n)}var Cte={kernelName:Ah,backendName:"webgl",kernelFunc:Ste},Tte="return float(!isnan(x) && !isinf(x));",Nte=Ze({opSnippet:Tte,dtype:"bool"}),_te={kernelName:ec,backendName:"webgl",kernelFunc:Nte},Ete="return float(isinf(x));",Ate=Ze({opSnippet:Ete,dtype:"bool"}),$te={kernelName:tc,backendName:"webgl",kernelFunc:Ate},Fte="return float(isnan(x));",Dte=Ze({opSnippet:Fte,dtype:"bool"}),Rte={kernelName:nc,backendName:"webgl",kernelFunc:Dte},Pte="return float(a < b);",Ote=`
2021-09-11 17:11:38 +02:00
return vec4(lessThan(a, b));
2021-12-01 21:37:52 +01:00
`,Mte=hn({opSnippet:Pte,packedOpSnippet:Ote,cpuKernelImpl:A9,dtype:"bool"}),Lte={kernelName:ni,backendName:"webgl",kernelFunc:Mte},Bte="return float(a <= b);",zte=`
2021-09-11 17:11:38 +02:00
return vec4(lessThanEqual(a, b));
2021-12-01 21:37:52 +01:00
`,Wte=hn({opSnippet:Bte,packedOpSnippet:zte,cpuKernelImpl:$9,dtype:"bool"}),Vte={kernelName:ri,backendName:"webgl",kernelFunc:Wte};function Ute(e){let{backend:t,attrs:n}=e,{start:r,stop:s,num:a}=n,o=F9(r,s,a);return t.makeTensorInfo([o.length],"float32",o)}var Gte={kernelName:$h,backendName:"webgl",kernelFunc:Ute},Hte=`if (x < 0.0) return NAN;
return log(x);`,jte=`
2021-09-11 17:11:38 +02: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-12-01 21:37:52 +01:00
`,qte=Ze({opSnippet:Hte,packedOpSnippet:jte,cpuKernelImpl:D9}),Kte={kernelName:Ea,backendName:"webgl",kernelFunc:qte},Xte="return log(1.0 + x);",Yte=Ze({opSnippet:Xte}),Qte={kernelName:rc,backendName:"webgl",kernelFunc:Yte},Zte="return float(a >= 1.0 && b >= 1.0);",Jte=`
2021-09-11 17:11:38 +02:00
return vec4(
vec4(greaterThanEqual(a, vec4(1.0))) *
vec4(greaterThanEqual(b, vec4(1.0))));
2021-12-01 21:37:52 +01:00
`,ene=hn({opSnippet:Zte,packedOpSnippet:Jte,dtype:"bool"}),tne={kernelName:si,backendName:"webgl",kernelFunc:ene},nne="return float(!(x >= 1.0));",rne=Ze({opSnippet:nne}),sne={kernelName:sc,backendName:"webgl",kernelFunc:rne},ane="return float(a >= 1.0 || b >= 1.0);",one=`
2021-09-11 17:11:38 +02:00
return min(
vec4(greaterThanEqual(a, vec4(1.0))) +
vec4(greaterThanEqual(b, vec4(1.0))),
vec4(1.0));
2021-12-01 21:37:52 +01:00
`,ine=hn({opSnippet:ane,packedOpSnippet:one,dtype:"bool"}),une={kernelName:ed,backendName:"webgl",kernelFunc:ine},cne=class{constructor(e,t,n,r,s){this.variableNames=["x"],this.outputShape=[];let a=t,o=e[3]-1;this.outputShape=e;let i,u=`float(${n}) + float(${r}) * sum`;s===.5?i=`inversesqrt(${u})`:s===1?i=`1.0/(${u})`:i=`exp(log(${u}) * float(-${s}));`,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
int d = coords[3];
float x = getX(b, r, c, d);
float sum = 0.0;
for (int j = -${a}; j <= ${a}; j++) {
int idx = d + j;
if (idx >= 0 && idx <= ${o}) {
float z = getX(b, r, c, idx);
sum += z * z;
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
float val = x * ${i};
setOutput(val);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},lne=class{constructor(e,t,n,r,s){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;let a=t,o=e[3]-1;this.outputShape=e;let i,u=`float(${n}) + float(${r}) * sum`;s===.5?i=`inversesqrt(${u})`:s===1?i=`1.0/(${u})`:i=`exp(log(${u}) * float(-${s}));`,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords.x;
int r = coords.y;
int c = coords.z;
int d = coords.w;
bool hasNextCol = d < ${this.outputShape[3]};
bool hasNextRow = c < ${this.outputShape[2]};
vec4 sum = vec4(0.);
vec4 xFragAtOutputCoords = getX(b, r, c, d);
vec4 xAtOutputCoords = vec4(
getChannel(xFragAtOutputCoords, vec2(c, d)),
hasNextCol ?
getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,
hasNextRow ?
getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,
(hasNextRow && hasNextCol) ?
getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0
);
int firstChannel = d - ${a};
vec2 cache = vec2(0.);
if(firstChannel >= 0){
vec4 firstChannelFrag = getX(b, r, c, firstChannel);
cache.x = getChannel(firstChannelFrag, vec2(c, firstChannel));
if(hasNextRow){
cache.y = getChannel(firstChannelFrag, vec2(c + 1, firstChannel));
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
ivec2 depth = ivec2(d, d + 1);
for (int j = - ${a}; j <= ${a}; j++) {
ivec2 idx = depth + j;
bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));
bvec2 belowUpperBound = lessThanEqual(idx, ivec2(${o}));
bool depthInRange = aboveLowerBound.x && belowUpperBound.x;
bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;
if(depthInRange || depthPlusOneInRange){
vec4 z = vec4(0.);
vec4 xFragAtCurrentDepth;
z.xz = cache.xy;
if(depthPlusOneInRange && hasNextCol){
xFragAtCurrentDepth = idx.y != d ?
getX(b, r, c, idx.y) : xFragAtOutputCoords;
z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));
if(hasNextRow){
z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));
}
}
cache.xy = z.yw;
sum += z * z;
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
vec4 result = xAtOutputCoords * ${i};
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},dne=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{depthRadius:a,bias:o,alpha:i,beta:u}=r,l=X().getBool("WEBGL_PACK_NORMALIZATION")?new lne(s.shape,a,o,i,u):new cne(s.shape,a,o,i,u);return n.runWebGLProgram(l,[s],s.dtype)},pne={kernelName:td,backendName:"webgl",kernelFunc:dne},hne=class{constructor(e,t,n,r,s){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=r,this.beta=s,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int r = coords[1];
int c = coords[2];
float result = 0.0;
for (int d = 0; d < ${this.depth}; ++d) {
int depthBegin = int(max(0.0, float(d - ${t})));
int depthEnd = int(min(float(${this.depth}),
float(d + ${t} + 1)));
const int MIN_DEPTH_BEGIN = 0;
const int MAX_DEPTH_END = ${this.depth};
float norm = 0.0;
for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {
if (k < depthBegin){
continue;
}
else if (k >= depthBegin && k < depthEnd) {
norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);
}
else {
break;
}
}
norm = float(${r}) * norm + float(${n});
for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){
if (k < depthBegin){
continue;
}
else if (k >= depthBegin && k < depthEnd){
float dyi = -2.0 * float(${r})
* float(${s})
* getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)
/ norm;
if (k == d) {
dyi += pow(norm, -1.0 * ${s});
}
if (k == coords[3]) {
dyi *= getDy(b, r, c, d);
result += dyi;
}
}
else {
break;
}
}
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
setOutput(result);
2021-03-26 15:26:02 +01:00
}
2021-12-01 21:37:52 +01:00
`}},fne=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s,y:a,dy:o}=t,{depthRadius:i,bias:u,alpha:l,beta:c}=r,d=new hne(s.shape,i,u,l,c);return n.runWebGLProgram(d,[s,a,o],s.dtype)},mne={kernelName:Fh,backendName:"webgl",kernelFunc:fne};function gne(e,t,n,r){let s=w.sizeFromShape(t),o=w.sizeFromShape(e.shape)/s,i=fe({inputs:{x:e},attrs:{shape:[o,s]},backend:r}),u=ou(i,e.dtype,"max",r),l=fe({inputs:{x:u},attrs:{shape:n},backend:r});return r.disposeIntermediateTensorInfo(i),r.disposeIntermediateTensorInfo(u),l}function J_(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reductionIndices:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=N.getAxesPermutation(l,i),d=c!=null,p=n.shouldExecuteOnCPU([s]),h=s;if(d){if(p){let v=n.texData.get(h.dataId).values,x=new Array(i);for(let C=0;C<x.length;C++)x[C]=s.shape[c[C]];let k=ok(v,s.shape,s.dtype,c,x);h=n.makeTensorInfo(x,s.dtype);let T=n.texData.get(h.dataId);T.values=k}else h=Fm(s,c,n);l=N.getInnerMostAxes(l.length,i)}N.assertAxesAreInnerMostDims("max",l,i);let[f,m]=N.computeOutAndReduceShapes(h.shape,l),g=f;o&&(g=N.expandShapeToKeepDim(f,u));let b;if(p){let v=n.texData.get(h.dataId).values,x=R9(v,w.sizeFromShape(m),g,s.dtype);b=n.makeTensorInfo(g,s.dtype);let k=n.texData.get(b.dataId);k.values=x}else b=gne(h,m,g,n);return d&&n.disposeIntermediateTensorInfo(h),b}var bne={kernelName:Aa,backendName:"webgl",kernelFunc:J_},yne=b_+`
2021-09-11 17:11:38 +02:00
return max(a, b);
2021-12-01 21:37:52 +01:00
`,vne=`
2021-09-11 17:11:38 +02:00
vec4 result = vec4(max(a, b));
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
2021-12-01 21:37:52 +01:00
`+Am+`
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,xne=hn({opSnippet:yne,packedOpSnippet:vne,cpuKernelImpl:P9}),wne={kernelName:$a,backendName:"webgl",kernelFunc:xne};function kne(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t;Gc(s,"maxPool");let{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=r,l=1;w.assert(N.eitherStridesOrDilationsAreOne(o,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${o} and dilations '${l}'`);let c=N.computePool2DInfo(s.shape,a,o,l,i,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return cr({inputs:{x:s},backend:n});let d=new lp(c,"max",!1);return n.runWebGLProgram(d,[s],s.dtype)}var Ine={kernelName:Fa,backendName:"webgl",kernelFunc:kne};function Sne(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{filterSize:a,strides:o,pad:i,dataFormat:u,dimRoundingMode:l}=r,c=[1,1,1],d=N.computePool3DInfo(s.shape,a,o,c,i,l,u),p=new ck(d,"max",!1);return n.runWebGLProgram(p,[s],s.dtype)}var Cne={kernelName:nd,backendName:"webgl",kernelFunc:Sne},Tne=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideHeight,n=e.strideWidth,r=e.dilationHeight,s=e.effectiveFilterHeight,a=e.effectiveFilterWidth,o=s-1-e.padInfo.top,i=a-1-e.padInfo.left,u=s*a-1;this.userCode=`
2021-09-11 17:11:38 +02:00
const ivec2 pads = ivec2(${o}, ${i});
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 dyRCCorner = coords.yz - pads;
int dyRCorner = dyRCCorner.x;
int dyCCorner = dyRCCorner.y;
// Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wR = 0; wR < ${s};
wR += ${r}) {
float dyR = float(dyRCorner + wR) / ${t}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 || fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
for (int wC = 0; wC < ${a}; wC++) {
float dyC = float(dyCCorner + wC) / ${n}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(b, idyR, idyC, d);
2021-12-01 21:37:52 +01:00
int maxPosValue = ${u} - int(getMaxPos(b, idyR, idyC, d));
2021-09-11 17:11:38 +02:00
// Get the current value, check it against the value from the
// position matrix.
int curPosValue = wR * ${a} + wC;
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
dotProd += dyValue * mask;
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},Nne=class{constructor(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;let t=e.strideDepth,n=e.strideHeight,r=e.strideWidth,s=e.dilationDepth,a=e.dilationHeight,o=e.dilationWidth,i=e.effectiveFilterDepth,u=e.effectiveFilterHeight,l=e.effectiveFilterWidth,c=i-1-e.padInfo.front,d=u-1-e.padInfo.top,p=l-1-e.padInfo.left,h=i*u*l-1;this.userCode=`
const ivec3 pads = ivec3(${c}, ${d}, ${p});
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec5 coords = getOutputCoords();
int batch = coords.x;
int ch = coords.u;
ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
int dyDCorner = dyCorner.x;
int dyRCorner = dyCorner.y;
int dyCCorner = dyCorner.z;
// Convolve dy(?, ?, ?, ch) with pos mask(:, :, :, d) to get
// dx(xD, xR, xC, ch).
// ? = to be determined. : = across all values in that axis.
float dotProd = 0.0;
for (int wD = 0; wD < ${i};
wD += ${s}) {
float dyD = float(dyDCorner + wD) / ${t}.0;
if (dyD < 0.0 || dyD >= ${e.outDepth}.0 || fract(dyD) > 0.0) {
continue;
}
int idyD = int(dyD);
2021-12-01 21:37:52 +01:00
for (int wR = 0; wR < ${u};
2021-09-11 17:11:38 +02:00
wR += ${a}) {
float dyR = float(dyRCorner + wR) / ${n}.0;
if (dyR < 0.0 || dyR >= ${e.outHeight}.0 ||
fract(dyR) > 0.0) {
continue;
}
int idyR = int(dyR);
2021-10-22 15:06:43 +02:00
for (int wC = 0; wC < ${l};
2021-09-11 17:11:38 +02:00
wC += ${o}) {
float dyC = float(dyCCorner + wC) / ${r}.0;
if (dyC < 0.0 || dyC >= ${e.outWidth}.0 ||
fract(dyC) > 0.0) {
continue;
}
int idyC = int(dyC);
float dyValue = getDy(batch, idyD, idyR, idyC, ch);
int maxPosValue = ${h} -
int(getMaxPos(batch, idyD, idyR, idyC, ch));
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// Get the current value, check it against the value from the
// position matrix.
int curPosValue =
2021-12-01 21:37:52 +01:00
wD * ${u} * ${l} +
2021-10-22 15:06:43 +02:00
wR * ${l} + wC;
2021-09-11 17:11:38 +02:00
float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
dotProd += dyValue * mask;
}
}
}
setOutput(dotProd);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function _ne(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a}=t,o=a,{filterSize:i,strides:u,pad:l,dimRoundingMode:c}=r,d=[1,1,1],p=N.computePool3DInfo(o.shape,i,u,d,l,c),h=new ck(p,"max",!0),f=n.runWebGLProgram(h,[o],o.dtype),m=new Nne(p),g=n.runWebGLProgram(m,[s,f],o.dtype);return n.disposeIntermediateTensorInfo(f),g}var Ene={kernelName:Rh,backendName:"webgl",kernelFunc:_ne};function Ane(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,input:a,output:o}=t,i=a;Gc([a,o],"maxPoolGrad");let{filterSize:u,strides:l,pad:c,dimRoundingMode:d}=r,p=N.computePool2DInfo(i.shape,u,l,1,c,d),h=!0,f=new lp(p,"max",h),m=n.runWebGLProgram(f,[i],i.dtype),g=new Tne(p),b=n.runWebGLProgram(g,[s,m],i.dtype);return n.disposeIntermediateTensorInfo(m),b}var $ne={kernelName:Dh,backendName:"webgl",kernelFunc:Ane};function Fne(e,t,n,r){let s=new lp(n,"max",!1),a=r.runWebGLProgram(s,[e],"float32");s=new lp(n,"max",!0,!0,t);let o=r.runWebGLProgram(s,[e],"float32");return[a,o]}var Dne={kernelName:Ph,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:r}=e,{filterSize:s,strides:a,pad:o,includeBatchInIndex:i}=t,u=n;w.assert(r.shape.length===4,()=>`Error in maxPool: input must be rank 4 but got rank ${r.shape.length}.`);let l=[1,1];w.assert(N.eitherStridesOrDilationsAreOne(a,l),()=>`Error in maxPool: Either strides or dilations must be 1. Got strides ${a} and dilations '${l}'`);let c=N.computePool2DInfo(r.shape,s,a,l,o),[d,p]=Fne(r,i,c,u);return[d,p]}};function Rne(e,t,n,r){let s=w.sizeFromShape(t),o=w.sizeFromShape(e.shape)/s,i=fe({inputs:{x:e},attrs:{shape:[o,s]},backend:r}),u=ou(i,"float32","mean",r),l=fe({inputs:{x:u},attrs:{shape:n},backend:r});return r.disposeIntermediateTensorInfo(i),r.disposeIntermediateTensorInfo(u),l}var Pne={kernelName:Da,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:r}=e,{keepDims:s,axis:a}=t,o=n,i=r.shape.length,u=w.parseAxisParam(a,r.shape),l=u,c=N.getAxesPermutation(l,i),d=c!=null,p=o.shouldExecuteOnCPU([r]),h=[],f=r;if(d){if(p){let x=o.texData.get(f.dataId).values,k=new Array(i);for(let E=0;E<k.length;E++)k[E]=r.shape[c[E]];let T=ok(x,r.shape,r.dtype,c,k);f=o.makeTensorInfo(k,r.dtype);let C=o.texData.get(f.dataId);C.values=T}else f=Fm(r,c,o);h.push(f),l=N.getInnerMostAxes(l.length,i)}N.assertAxesAreInnerMostDims("sum",l,i);let[m,g]=N.computeOutAndReduceShapes(f.shape,l),b=m;s&&(b=N.expandShapeToKeepDim(m,u));let y=Rne(f,g,b,o);for(let v of h)o.disposeIntermediateTensorInfo(v);return y}};function One(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=w.parseAxisParam(a,s.shape),l=u,c=N.getAxesPermutation(l,i),d=s;c!=null&&(d=Dn({inputs:{x:s},backend:n,attrs:{perm:c}}),l=N.getInnerMostAxes(l.length,s.shape.length)),N.assertAxesAreInnerMostDims("min",l,i);let[p,h]=N.computeOutAndReduceShapes(d.shape,l),f=w.sizeFromShape(h),m=fe({inputs:{x:d},backend:n,attrs:{shape:[-1,f]}}),g=ou(m,m.dtype,"min",n),b;if(o){let y=N.expandShapeToKeepDim(p,u);b=fe({inputs:{x:g},backend:n,attrs:{shape:y}})}else b=fe({inputs:{x:g},backend:n,attrs:{shape:p}});return n.disposeIntermediateTensorInfo(m),n.disposeIntermediateTensorInfo(g),c!=null&&n.disposeIntermediateTensorInfo(d),b}var Mne={kernelName:Ra,backendName:"webgl",kernelFunc:One},Lne=b_+`
2021-09-11 17:11:38 +02:00
return min(a, b);
2021-12-01 21:37:52 +01:00
`,Bne=`
2021-09-11 17:11:38 +02:00
vec4 result = vec4(min(a, b));
vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));
2021-12-01 21:37:52 +01:00
`+Am+`
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,zne=hn({opSnippet:Lne,packedOpSnippet:Bne,cpuKernelImpl:O9}),Wne={kernelName:Pa,backendName:"webgl",kernelFunc:zne},Vne=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=t.map((l,c)=>l[0]+e[c]+l[1]);let r=e.length,s=gt(r),a=t.map(l=>l[0]).join(","),o=t.map((l,c)=>l[0]+e[c]).join(","),i=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r),u=n==="reflect"?0:1;if(r===1){this.userCode=`
2021-09-11 17:11:38 +02:00
int start = ${a};
int end = ${o};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
int outC = getOutputCoords();
if (outC < start) {
2021-12-01 21:37:52 +01:00
outC = start * 2 - outC - ${u};
2021-09-11 17:11:38 +02:00
} else if(outC >= end) {
2021-12-01 21:37:52 +01:00
outC = (end - 1) * 2 - outC + ${u};
2021-09-11 17:11:38 +02:00
}
setOutput(getX(outC - start));
}
`;return}this.userCode=`
${s} start = ${s}(${a});
${s} end = ${s}(${o});
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
void main() {
${s} outC = getOutputCoords();
for (int i = 0; i < ${r}; i++) {
if (outC[i] < start[i]) {
2021-12-01 21:37:52 +01:00
outC[i] = start[i] * 2 - outC[i] - ${u};
2021-09-11 17:11:38 +02:00
} else if(outC[i] >= end[i]) {
2021-12-01 21:37:52 +01:00
outC[i] = (end[i] - 1) * 2 - outC[i] + ${u};
2021-09-11 17:11:38 +02:00
}
}
${s} coords = outC - start;
setOutput(getX(${i}));
}
2021-12-01 21:37:52 +01:00
`}},Une=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.map((h,f)=>h[0]+e[f]+h[1]);let r=e.length,s=gt(r),a=t.map(h=>h[0]).join(","),o=t.map((h,f)=>h[0]+e[f]).join(","),i=Fn("rc",r),u=Fn("source",r),l=`${i[r-1]} < ${this.outputShape[r-1]}`,c=r===1?"source":`vec2(${u.slice(-2).join()})`,d=n==="reflect"?0:1,p="";if(r===1){let h=`
2021-09-11 17:11:38 +02:00
${s} source = rc;
if (source < start) {
source = start * 2 - source - ${d};
} else if (source >= end) {
source = (end - 1) * 2 - source + ${d};
}
source -= start;
`;p=`
${s} rc = outputLoc;
${h}
2021-12-01 21:37:52 +01:00
result[0] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
${i[r-1]} += 1;
2021-10-22 15:06:43 +02:00
if(${l}) {
2021-09-11 17:11:38 +02:00
${h}
2021-12-01 21:37:52 +01:00
result[1] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
}
`}else{let h=`
${s} source = rc;
${s} lt = ${s}(lessThan(source, start));
${s} gte = ${s}(greaterThanEqual(source, end));
${s} orig = 1 - (lt + gte);
source = orig * source +
lt * (start * 2 - source - ${d}) +
gte * ((end - 1) * 2 - source + ${d});
source -= start;
`;p=`
${s} rc = outputLoc;
${h}
2021-12-01 21:37:52 +01:00
result[0] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
${i[r-1]} += 1;
2021-10-22 15:06:43 +02:00
if(${l}) {
2021-09-11 17:11:38 +02:00
${h}
2021-12-01 21:37:52 +01:00
result[1] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
}
rc = outputLoc;
${i[r-2]} += 1;
if(${i[r-2]} < ${this.outputShape[r-2]}) {
${h}
2021-12-01 21:37:52 +01:00
result[2] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
${i[r-1]} += 1;
2021-10-22 15:06:43 +02:00
if(${l}) {
2021-09-11 17:11:38 +02:00
${h}
2021-12-01 21:37:52 +01:00
result[3] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
}
}
`}this.userCode=`
const ${s} start = ${s}(${a});
const ${s} end = ${s}(${o});
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
void main() {
${s} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${p}
setOutput(result);
2020-12-15 14:15:43 +01:00
}
2021-12-01 21:37:52 +01:00
`}},Gne=({inputs:e,backend:t,attrs:n})=>{let{x:r}=e,{paddings:s,mode:a}=n,o=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Une(r.shape,s,a):new Vne(r.shape,s,a);return t.runWebGLProgram(o,[r],r.dtype)},Hne={kernelName:Oa,backendName:"webgl",kernelFunc:Gne},jne=`if (b == 0.0) return NAN;
return mod(a, b);`,qne=`
2021-09-11 17:11:38 +02:00
vec4 result = mod(a, b);
vec4 isNaN = vec4(equal(b, vec4(0.0)));
2021-12-01 21:37:52 +01:00
`+Am+`
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,Kne=hn({opSnippet:jne,packedOpSnippet:qne}),Xne={kernelName:ac,backendName:"webgl",kernelFunc:Kne},Yne=class{constructor(e,t,n){this.variableNames=["probs"],this.customUniforms=[{name:"seed",type:"float"}],this.outputShape=[e,n],this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
float r = random(seed);
float cdf = 0.0;
for (int i = 0; i < ${t-1}; i++) {
cdf += getProbs(batch, i);
if (r < cdf) {
setOutput(float(i));
return;
}
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// If no other event happened, last event happened.
setOutput(float(${t-1}));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},Qne=`
2021-09-11 17:11:38 +02:00
if (a == b) {
return 1.0;
};
2021-12-01 21:37:52 +01:00
return a / b;`,Zne=`
2021-09-11 17:11:38 +02:00
// vec4 one = vec4(equal(a, b));
// return one + (vec4(1.0) - one) * a / b;
vec4 result = a / b;
if(a.x == b.x) {
result.x = 1.;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if(a.y == b.y) {
result.y = 1.;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if(a.z == b.z) {
result.z = 1.;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if(a.w == b.w) {
result.w = 1.;
2021-04-01 19:39:54 +02:00
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,eE=hn({opSnippet:Qne,packedOpSnippet:Zne,checkOutOfBounds:!0}),Jne={kernelName:wa,backendName:"webgl",kernelFunc:eE},tE="return a - b;",nE=hn({opSnippet:tE,packedOpSnippet:tE,supportsComplex:!0,cpuKernelImpl:Z9}),ere={kernelName:Qa,backendName:"webgl",kernelFunc:nE};function rE(e){let{inputs:t,backend:n,attrs:r}=e,{logits:s}=t,{dim:a}=r,o=w.parseAxisParam([a],s.shape),i=J_({inputs:{x:s},backend:n,attrs:{reductionIndices:o,keepDims:!1}}),u=N.expandShapeToKeepDim(i.shape,o),l=fe({inputs:{x:i},backend:n,attrs:{shape:u}}),c=nE({inputs:{a:s,b:l},backend:n}),d=q_({inputs:{x:c},backend:n}),p=Dm({inputs:{x:d},backend:n,attrs:{axis:o,keepDims:!1}}),h=fe({inputs:{x:p},backend:n,attrs:{shape:u}}),f=eE({inputs:{a:d,b:h},backend:n});return n.disposeIntermediateTensorInfo(i),n.disposeIntermediateTensorInfo(l),n.disposeIntermediateTensorInfo(c),n.disposeIntermediateTensorInfo(d),n.disposeIntermediateTensorInfo(p),n.disposeIntermediateTensorInfo(h),f}var tre={kernelName:Xa,backendName:"webgl",kernelFunc:rE};function nre(e){let{inputs:t,backend:n,attrs:r}=e,{logits:s}=t,{numSamples:a,seed:o,normalized:i}=r,u=i?s:rE({inputs:{logits:s},backend:n,attrs:{dim:s.shape.length-1}}),l=u.shape[0],c=u.shape[1],d=new Yne(l,c,a),p=[[o]],h=n.runWebGLProgram(d,[u],"int32",p);return i||n.disposeIntermediateTensorInfo(u),h}var rre={kernelName:Oh,backendName:"webgl",kernelFunc:nre},sE="return -x;";function sre(e){let{inputs:t,backend:n}=e,{x:r}=t;if(n.shouldExecuteOnCPU([r])){let a=n.texData.get(r.dataId),[o,i]=L9(a.values,r.shape,r.dtype);return n.makeTensorInfo(i,r.dtype,o)}let s;return X().getBool("WEBGL_PACK_UNARY_OPERATIONS")?s=new Xc(r.shape,sE):s=new To(r.shape,sE),n.runWebGLProgram(s,[r],r.dtype)}var are={kernelName:ai,backendName:"webgl",kernelFunc:sre},ore=Dr.nonMaxSuppressionV3Impl;function ire(e){N.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u}=r,l=n.readSync(s.dataId),c=n.readSync(a.dataId),{selectedIndices:d}=ore(l,c,o,i,u);return n.makeTensorInfo([d.length],"int32",new Int32Array(d))}var ure={kernelName:ii,backendName:"webgl",kernelFunc:ire},cre=Dr.nonMaxSuppressionV4Impl;function lre(e){N.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u,padToMaxOutputSize:l}=r,c=n.readSync(s.dataId),d=n.readSync(a.dataId),{selectedIndices:p,validOutputs:h}=cre(c,d,o,i,u,l);return[n.makeTensorInfo([p.length],"int32",new Int32Array(p)),n.makeTensorInfo([],"int32",new Int32Array([h]))]}var dre={kernelName:oc,backendName:"webgl",kernelFunc:lre},pre=Dr.nonMaxSuppressionV5Impl;function hre(e){N.warn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u,softNmsSigma:l}=r,c=n.readSync(s.dataId),d=n.readSync(a.dataId),p=o,h=i,f=u,m=l,{selectedIndices:g,selectedScores:b}=pre(c,d,p,h,f,m);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var fre={kernelName:ui,backendName:"webgl",kernelFunc:hre},mre=class{constructor(e,t,n,r){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int index = round(getIndices(coords.x));
setOutput(mix(float(${r}), float(${n}),
float(index == coords.y)));
2021-03-26 15:26:02 +01:00
}
2021-12-01 21:37:52 +01:00
`}},gre=e=>{let{inputs:t,backend:n,attrs:r}=e,{indices:s}=t,{depth:a,onValue:o,offValue:i}=r,u=w.sizeFromShape(s.shape),l=new mre(u,a,o,i),c=fe({inputs:{x:s},backend:n,attrs:{shape:[u]}}),d=n.runWebGLProgram(l,[c],s.dtype);n.disposeIntermediateTensorInfo(c);let p=[...s.shape,a],h=fe({inputs:{x:d},backend:n,attrs:{shape:p}});return n.disposeIntermediateTensorInfo(d),h},bre={kernelName:li,backendName:"webgl",kernelFunc:gre};function Lm(e){let{inputs:t,backend:n}=e,{x:r}=t;if(r.dtype==="complex64"){let s=dp({inputs:{input:r},backend:n}),a=Lm({inputs:{x:s},backend:n}),o=Mm({inputs:{input:r},backend:n}),i=Lm({inputs:{x:o},backend:n}),u=No({inputs:{real:a,imag:i},backend:n});return n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(i),u}else return pp({attrs:{shape:r.shape,dtype:r.dtype,value:r.dtype==="string"?"":0},backend:n})}var yre={kernelName:Ni,backendName:"webgl",kernelFunc:Lm};function aE(e){let{inputs:t,backend:n}=e,{x:r}=t;if(r.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(r.dtype==="complex64"){let s=dp({inputs:{input:r},backend:n}),a=aE({inputs:{x:s},backend:n}),o=Mm({inputs:{input:r},backend:n}),i=Lm({inputs:{x:o},backend:n}),u=No({inputs:{real:a,imag:i},backend:n});return n.disposeIntermediateTensorInfo(s),n.disposeIntermediateTensorInfo(a),n.disposeIntermediateTensorInfo(o),n.disposeIntermediateTensorInfo(i),u}else return pp({attrs:{shape:r.shape,dtype:r.dtype,value:1},backend:n})}var vre={kernelName:ci,backendName:"webgl",kernelFunc:aE};function xre(e){let{inputs:t,backend:n,attrs:r}=e,{axis:s}=r;if(t.length===1)return pk({inputs:{input:t[0]},backend:n,attrs:{dim:s}});let a=t[0].shape,o=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(o===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let i=[],u=t.map(c=>{let d=pk({inputs:{input:c},backend:n,attrs:{dim:s}});return i.push(d),d}),l=M_({inputs:u,backend:n,attrs:{axis:s}});return i.forEach(c=>n.disposeIntermediateTensorInfo(c)),l}var wre={kernelName:di,backendName:"webgl",kernelFunc:xre},kre=class{constructor(e,t,n){this.variableNames=["x"],this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((u,l)=>u[0]+e[l]+u[1]);let r=e.length,s=gt(r),a=t.map(u=>u[0]).join(","),o=t.map((u,l)=>u[0]+e[l]).join(","),i=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r);if(r===1){this.userCode=`
2021-09-11 17:11:38 +02:00
int start = ${a};
int end = ${o};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
int outC = getOutputCoords();
if (outC < start || outC >= end) {
setOutput(value);
} else {
setOutput(getX(outC - start));
}
}
`;return}this.userCode=`
${s} start = ${s}(${a});
${s} end = ${s}(${o});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
void main() {
${s} outC = getOutputCoords();
if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {
setOutput(value);
} else {
${s} coords = outC - start;
setOutput(getX(${i}));
}
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},Ire=class{constructor(e,t,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.customUniforms=[{name:"value",type:"float"}],this.outputShape=t.map((f,m)=>f[0]+e[m]+f[1]);let r=e.length,s=gt(r),a=t.map(f=>f[0]).join(","),o=t.map((f,m)=>f[0]+e[m]).join(","),i=Fn("rc",r),u=Fn("source",r),l=`${i[r-1]} < ${this.outputShape[r-1]}`,c=r===1?"source":`vec2(${u.slice(-2).join()})`,d=[`${s} rc = outputLoc;`,`${i[r-1]} += 1;
2021-10-22 15:06:43 +02:00
if(${l}) {
2021-09-11 17:11:38 +02:00
`,r===1?"":`}
rc = outputLoc;
${i[r-2]} += 1;
if(${i[r-2]} < ${this.outputShape[r-2]}) {`,r===1?"":` ${i[r-1]} += 1;
2021-10-22 15:06:43 +02:00
if(${l}) {`],p=r===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="";for(let f=0,m=r===1?2:4;f<m;f++)h+=`
2021-09-11 17:11:38 +02:00
${d[f]}
if (${p}) {
result[${f}] = float(value);
} else {
${s} source = rc - start;
2021-12-01 21:37:52 +01:00
result[${f}] = getChannel(getX(${u.join()}), ${c});
2021-09-11 17:11:38 +02:00
}
`;h+=r===1?"} ":"}}",this.userCode=`
const ${s} start = ${s}(${a});
const ${s} end = ${s}(${o});
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
${s} outputLoc = getOutputCoords();
vec4 result = vec4(0.);
${h}
setOutput(result);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},oE=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{paddings:a,constantValue:o}=r;if(w.sizeFromShape(s.shape)===0){let l=a.map((c,d)=>c[0]+s.shape[d]+c[1]);return pp({backend:n,attrs:{shape:l,value:o,dtype:s.dtype}})}let i=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new Ire(s.shape,a,o):new kre(s.shape,a,o),u=[[o]];return n.runWebGLProgram(i,[s],s.dtype,u)},Sre={kernelName:La,backendName:"webgl",kernelFunc:oE},Cre=`
2021-09-11 17:11:38 +02:00
if(a < 0.0 && floor(b) < b){
return NAN;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
if (b == 0.0) {
return 1.0;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
return (round(mod(b, 2.0)) != 1) ?
pow(abs(a), b) : sign(a) * pow(abs(a), b);
2021-12-01 21:37:52 +01:00
`,Tre=`
2021-09-11 17:11:38 +02:00
// isModRound1 has 1 for components with round(mod(b, 2.0)) == 1, 0 otherwise.
vec4 isModRound1 = vec4(equal(round(mod(b, 2.0)), ivec4(1)));
vec4 multiplier = sign(a) * isModRound1 + (vec4(1.0) - isModRound1);
vec4 result = multiplier * pow(abs(a), b);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// Ensure that a^0 = 1, including 0^0 = 1 as this correspond to TF and JS
bvec4 isExpZero = equal(b, vec4(0.0));
result.r = isExpZero.r ? 1.0 : result.r;
result.g = isExpZero.g ? 1.0 : result.g;
result.b = isExpZero.b ? 1.0 : result.b;
result.a = isExpZero.a ? 1.0 : result.a;
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 isNaN = vec4(lessThan(a, vec4(0.0))) * vec4(lessThan(floor(b), b));
2021-12-01 21:37:52 +01:00
`+Am+`
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,Nre=hn({opSnippet:Cre,packedOpSnippet:Tre}),_re={kernelName:Ba,backendName:"webgl",kernelFunc:Nre};function Ere(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r,i=s.shape.length,u=[],l=w.parseAxisParam(a,s.shape),c=l,d=N.getAxesPermutation(c,i),p=s;d!=null&&(p=Dn({inputs:{x:s},backend:n,attrs:{perm:d}}),c=N.getInnerMostAxes(c.length,i),u.push(p)),N.assertAxesAreInnerMostDims("prod",c,i);let h;if(n.shouldExecuteOnCPU([p])){let f=n.texData.get(p.dataId).values,{outVals:m,outShape:g,outDtype:b}=z9(p.shape,p.dtype,f,c);h=n.makeTensorInfo(g,b,m)}else{let[f,m]=N.computeOutAndReduceShapes(p.shape,c),g=w.sizeFromShape(m),b=fe({inputs:{x:p},backend:n,attrs:{shape:[-1,g]}}),y=yd(s.dtype),v=ou(b,y,"prod",n);h=fe({inputs:{x:v},backend:n,attrs:{shape:f}}),u.push(b),u.push(v)}if(o){u.push(h);let f=N.expandShapeToKeepDim(h.shape,l);h=fe({inputs:{x:h},backend:n,attrs:{shape:f}})}return u.forEach(f=>n.disposeIntermediateTensorInfo(f)),h}var Are={kernelName:pi,backendName:"webgl",kernelFunc:Ere},iE=e=>{let{backend:t,attrs:n}=e,{start:r,stop:s,step:a,dtype:o}=n,i=W9(r,s,a,o);return t.makeTensorInfo([i.length],o,i)},$re={kernelName:ic,backendName:"webgl",kernelFunc:iE},Fre="return 1.0 / x;",Dre=Ze({opSnippet:Fre}),Rre={kernelName:uc,backendName:"webgl",kernelFunc:Dre},Pre=ts+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : x;
2021-12-01 21:37:52 +01:00
`,Ore=`
2021-09-11 17:11:38 +02:00
vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2021-12-01 21:37:52 +01:00
`,Mre=Ze({opSnippet:Pre,packedOpSnippet:Ore}),Lre={kernelName:Wa,backendName:"webgl",kernelFunc:Mre},Bre=ts+`
2021-09-11 17:11:38 +02:00
return (x < 0.0) ? 0.0 : min(6.0, x);
2021-12-01 21:37:52 +01:00
`,zre=`
2021-09-11 17:11:38 +02:00
vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
bvec4 isNaN = isnan(x);
result.r = isNaN.r ? x.r : result.r;
result.g = isNaN.g ? x.g : result.g;
result.b = isNaN.b ? x.b : result.b;
result.a = isNaN.a ? x.a : result.a;
return result;
2021-12-01 21:37:52 +01:00
`,Wre=Ze({opSnippet:Bre,packedOpSnippet:zre}),Vre={kernelName:Ua,backendName:"webgl",kernelFunc:Wre},Ure=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[r&&t>1?o-1:o,r&&n>1?i-1:i],c=[r&&t>1?t-1:t,r&&n>1?n-1:n],d;s?d="(vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC - vec2(0.5)":d="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2(
2021-12-01 21:37:52 +01:00
${l[0]/c[0]},
${l[1]/c[1]});
2021-09-11 17:11:38 +02:00
const vec2 inputShapeRC = vec2(${o}.0, ${i}.0);
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
vec2 sourceFracIndexRC = ${d};
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
// Compute the four integer indices.
ivec2 sourceFloorRC = ivec2(max(sourceFracIndexRC, vec2(0.0)));
ivec2 sourceCeilRC = ivec2(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);
float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);
float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);
float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
float top = topLeft + (topRight - topLeft) * fracRC.y;
float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
float newValue = top + (bottom - top) * fracRC.x;
setOutput(newValue);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},Gre=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[r&&t>1?o-1:o,r&&n>1?i-1:i],c=[r&&t>1?t-1:t,r&&n>1?n-1:n],d;s?d="(vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC - vec3(0.5)":d="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3(
2021-12-01 21:37:52 +01:00
${l[0]/c[0]},
${l[1]/c[1]},
${l[1]/c[1]});
2021-09-11 17:11:38 +02:00
const vec3 inputShapeRC = vec3(${o}.0, ${i}.0,
${i}.0);
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
// Calculate values for next column in yRC.z.
ivec3 yRC = coords.yzz + ivec3(0, 0, 1);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
vec3 sourceFracIndexRC = ${d};
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Compute the four integer indices.
ivec3 sourceFloorRC = ivec3(max(sourceFracIndexRC, vec3(0.0)));
ivec3 sourceCeilRC = ivec3(
min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Should we calculate next column and row elements in 2x2 packed cell.
2021-12-01 21:37:52 +01:00
bool hasNextCol = d < ${u-1};
2021-09-11 17:11:38 +02:00
bool hasNextRow = coords.z < ${n-1};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
// In parallel, construct four corners for all four components in
// packed 2x2 cell.
vec4 topLeft = vec4(
getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d),
hasNextCol ? getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d + 1) : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 bottomLeft = vec4(
getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d),
hasNextCol ? getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d + 1) : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 topRight = vec4(
getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d),
hasNextCol ? getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d + 1) : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 bottomRight = vec4(
getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d),
hasNextCol ? getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d + 1) : 0.0);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec3 fracRC = sourceFracIndexRC - vec3(sourceFloorRC);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 top = mix(topLeft, topRight, fracRC.yyzz);
vec4 bottom = mix(bottomLeft, bottomRight, fracRC.yyzz);
vec4 newValue = mix(top, bottom, fracRC.x);
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
setOutput(newValue);
}
2021-12-01 21:37:52 +01:00
`}};function Hre(e){let{inputs:t,backend:n,attrs:r}=e,{images:s}=t,{alignCorners:a,halfPixelCenters:o,size:i}=r,[u,l]=i,c=X().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new Gre(s.shape,u,l,a,o):new Ure(s.shape,u,l,a,o);return n.runWebGLProgram(c,[s],"float32")}var jre={kernelName:Va,backendName:"webgl",kernelFunc:Hre},qre=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,r,s]=t,[,a,o]=e,i=[n&&a>1?r-1:r,n&&o>1?s-1:s],u=[n&&a>1?a-1:a,n&&o>1?o-1:o],l=i[0]/u[0],c=i[1]/u[1],d=1/l,p=1/c,h=Math.ceil(d)*2+2,f=Math.ceil(p)*2+2;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
float accumulator = 0.0;
2020-10-11 18:41:17 +02:00
2021-10-22 15:06:43 +02:00
const float heightScale = float(${l});
2021-12-01 21:37:52 +01:00
const float widthScale = float(${c});
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
const float invHeightScale = float(${d});
const float invWidthScale = float(${p});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
const int winHeight = int(${h});
const int winWidth = int(${f});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(startRLerp - float(winHeight / 2));
2021-09-11 17:11:38 +02:00
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(startCLerp - float(winWidth / 2));
2021-09-11 17:11:38 +02:00
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
if (dyR < 0 || dyR >= ${a}) {
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
if (dyC < 0 || dyC >= ${o}) {
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float dxR = float(dyR) * heightScale;
int topDxRIndex = int(floor(dxR));
int bottomDxRIndex = int(min(ceil(dxR), ${r-1}.0));
float dxRLerp = dxR - float(topDxRIndex);
float inverseDxRLerp = 1.0 - dxRLerp;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float dxC = float(dyC) * widthScale;
int leftDxCIndex = int(floor(dxC));
int rightDxCIndex = int(min(ceil(dxC), ${s-1}.0));
float dxCLerp = dxC - float(leftDxCIndex);
float inverseDxCLerp = 1.0 - dxCLerp;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
if (r == topDxRIndex && c == leftDxCIndex) {
// topLeft
accumulator +=
getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;
}
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
if (r == topDxRIndex && c == rightDxCIndex) {
// topRight
accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
if (r == bottomDxRIndex && c == leftDxCIndex) {
// bottomLeft
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
if (r == bottomDxRIndex && c == rightDxCIndex) {
// bottomRight
accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;
}
}
}
// End loop over dy
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
setOutput(accumulator);
2021-03-31 13:01:22 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function Kre(e){let{inputs:t,backend:n,attrs:r}=e,{images:s,dy:a}=t,{alignCorners:o}=r,i=new qre(a.shape,s.shape,o);return n.runWebGLProgram(i,[a],a.dtype)}var Xre={kernelName:Lh,backendName:"webgl",kernelFunc:Kre},Yre=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[r&&t>1?o-1:o,r&&n>1?i-1:i],c=[r&&t>1?t-1:t,r&&n>1?n-1:n],d=r?"0.5":"0.0",p;s?p="max((vec2(yRC) + vec2(0.5)) * effectiveInputOverOutputRatioRC, vec2(0.0))":p="vec2(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec2 effectiveInputOverOutputRatioRC = vec2(
2021-12-01 21:37:52 +01:00
${l[0]/c[0]},
${l[1]/c[1]});
2021-09-11 17:11:38 +02:00
const vec2 inputShapeRC = vec2(${o}.0, ${i}.0);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
ivec2 yRC = coords.yz;
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
vec2 sourceFracIndexRC = ${p};
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Compute the coordinators of nearest neighbor point.
ivec2 sourceNearestRC = ivec2(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${d})));
float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);
setOutput(newValue);
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}},Qre=class{constructor(e,t,n,r,s){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];let[a,o,i,u]=e;this.outputShape=[a,t,n,u];let l=[r&&t>1?o-1:o,r&&n>1?i-1:i],c=[r&&t>1?t-1:t,r&&n>1?n-1:n],d=r?"0.5":"0.0",p;s?p="max((vec3(yRC) + vec3(0.5)) * effectiveInputOverOutputRatioRC, vec3(0.0))":p="vec3(yRC) * effectiveInputOverOutputRatioRC",this.userCode=`
2021-09-11 17:11:38 +02:00
const vec3 effectiveInputOverOutputRatioRC = vec3(
2021-12-01 21:37:52 +01:00
${l[0]/c[0]},
${l[1]/c[1]},
${l[1]/c[1]});
2021-09-11 17:11:38 +02:00
const vec3 inputShapeRC = vec3(${o}.0, ${i}.0,
${i}.0);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float getAValue(int b, int r, int c, int d) {
return getChannel(getA(b, r, c, d), vec2(c, d));
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
// Calculate values for next column in yRC.z.
ivec3 yRC = coords.yzz + ivec3(0, 0, 1);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Fractional source index.
vec3 sourceFracIndexRC = ${p};
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Compute the coordinators of nearest neighbor point.
ivec3 sourceNearestRC = ivec3(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + ${d})));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Should we calculate next column and row elements in 2x2 packed cell.
2021-12-01 21:37:52 +01:00
bool hasNextCol = d < ${u-1};
2021-09-11 17:11:38 +02:00
bool hasNextRow = coords.z < ${n-1};
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
vec4 newValue = vec4(
getAValue(b, sourceNearestRC.x, sourceNearestRC.y, d),
hasNextCol ? getAValue(b, sourceNearestRC.x, sourceNearestRC.y, d + 1)
: 0.0,
hasNextRow ? getAValue(b, sourceNearestRC.x, sourceNearestRC.z, d)
: 0.0,
(hasNextRow && hasNextCol) ?
getAValue(b, sourceNearestRC.x, sourceNearestRC.z, d + 1) : 0.0);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
setOutput(newValue);
}
2021-12-01 21:37:52 +01:00
`}};function Zre(e){let{inputs:t,backend:n,attrs:r}=e,{images:s}=t,{alignCorners:a,halfPixelCenters:o,size:i}=r,[u,l]=i,c=X().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new Qre(s.shape,u,l,a,o):new Yre(s.shape,u,l,a,o);return n.runWebGLProgram(c,[s],s.dtype)}var Jre={kernelName:cc,backendName:"webgl",kernelFunc:Zre},ese=class{constructor(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t;let[,r,s]=t,[,a,o]=e,i=[n&&a>1?r-1:r,n&&o>1?s-1:s],u=[n&&a>1?a-1:a,n&&o>1?o-1:o],l=i[0]/u[0],c=i[1]/u[1],d=1/l,p=1/c,h=Math.ceil(d)*2+2,f=Math.ceil(p)*2+2;this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
int b = coords[0];
int d = coords[3];
int r = coords[1];
int c = coords[2];
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float accumulator = 0.0;
2020-12-15 14:15:43 +01:00
2021-10-22 15:06:43 +02:00
const float heightScale = float(${l});
2021-12-01 21:37:52 +01:00
const float widthScale = float(${c});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
const float invHeightScale = float(${d});
const float invWidthScale = float(${p});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
const int winHeight = int(${h});
const int winWidth = int(${f});
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Compute bounds for where in dy we will look
float startRLerp = floor(float(r) * invHeightScale);
int startDyR = int(floor(startRLerp - float(winHeight / 2)));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float startCLerp = floor(float(c) * invWidthScale);
int startDyC = int(floor(startCLerp - float(winWidth / 2)));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Loop over dy
for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
int dyR = dyROffset + startDyR;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
if (dyR < 0 || dyR >= ${a}) {
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
int dyC = dyCOffset + startDyC;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
// Guard against the window exceeding the bounds of dy
if (dyC < 0 || dyC >= ${o}) {
continue;
}
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
float sourceFracRow =
float(${i[0]}) *
2021-12-01 21:37:52 +01:00
(float(dyR) / float(${u[0]}));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
float sourceFracCol =
float(${i[1]}) *
2021-12-01 21:37:52 +01:00
(float(dyC) / float(${u[1]}));
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
int sourceNearestRow = int(min(
float(int(${r}) - 1),
${n} ? float(round(sourceFracRow)) :
float(floor(sourceFracRow))));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
int sourceNearestCol = int(min(
float(int(${s}) - 1),
${n} ? float(round(sourceFracCol)) :
float(floor(sourceFracCol))));
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
if (r == sourceNearestRow && c == sourceNearestCol) {
accumulator += getDy(b, dyR, dyC, d);
}
}
}
// End loop over dy
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
setOutput(accumulator);
}
2021-12-01 21:37:52 +01:00
`}};function tse(e){let{inputs:t,backend:n,attrs:r}=e,{images:s,dy:a}=t,{alignCorners:o}=r,i=new ese(a.shape,s.shape,o);return n.runWebGLProgram(i,[a],a.dtype)}var nse={kernelName:Mh,backendName:"webgl",kernelFunc:tse},rse=class{constructor(e,t){this.variableNames=["x"];let n=e.length;if(n>4)throw new Error(`WebGL backend: Reverse of rank-${n} tensor is not yet supported`);if(this.outputShape=e,n===1){this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
int coord = getOutputCoords();
setOutput(getX(${e[0]} - coord - 1));
}
2021-12-01 21:37:52 +01:00
`;return}let r=o=>t.indexOf(o)!==-1&&e[o]!==1?`${e[o]} - coords[${o}] - 1`:`coords[${o}]`,s=e.map((o,i)=>r(i)).join(","),a=gt(n);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${s}));
}
2021-12-01 21:37:52 +01:00
`}},sse=class{constructor(e,t){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0;let n=e.length;if(n>4)throw new Error(`WebGL backend: Reverse of rank-${n} tensor is not yet supported`);this.outputShape=e;let r=Fn("rc",n),s=`${r[n-1]} + 1 < ${this.outputShape[n-1]}`,a=`${r[n-2]} + 1 < ${this.outputShape[n-2]}`,o=gt(n);n===1?this.userCode=`
2021-09-11 17:11:38 +02:00
void main(){
int rc = getOutputCoords();
vec4 result = vec4(0.);
result.r = getChannel(getX(${e[0]} - rc - 1),
${e[0]} - rc - 1);
if(${s}){
result.g = getChannel(getX(${e[0]} - (rc + 1) - 1),
${e[0]} - (rc + 1) - 1);
}
setOutput(result);
}
`:this.userCode=`
void main() {
${o} rc = getOutputCoords();
vec4 result = vec4(0.);
result.r = ${i(r.slice())};
if(${s}){
2021-12-01 21:37:52 +01:00
result.g = ${u(r.slice())};
2021-09-11 17:11:38 +02:00
}
if(${a}) {
2021-10-22 15:06:43 +02:00
result.b = ${l(r.slice())};
2021-09-11 17:11:38 +02:00
if(${s}) {
2021-12-01 21:37:52 +01:00
result.a = ${c(r.slice())};
2021-09-11 17:11:38 +02:00
}
}
setOutput(result);
}
2021-12-01 21:37:52 +01:00
`;function i(h){return d(h)}function u(h){return h[n-1]="("+h[n-1]+" + 1)",d(h)}function l(h){return h[n-2]="("+h[n-2]+" + 1)",d(h)}function c(h){return h[n-1]="("+h[n-1]+" + 1)",h[n-2]="("+h[n-2]+" + 1)",d(h)}function d(h){let f=e.map((b,y)=>p(y,h)),m=f.join(","),g=f.slice(-2).join(",");return`getChannel(getX(${m}), vec2(${g}))`}function p(h,f){return t.indexOf(h)!==-1&&e[h]!==1?`${e[h]} - ${f[h]} - 1`:`${f[h]}`}}};function ase(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{dims:a}=r,o=s.shape.length,i=w.parseAxisParam(a,s.shape);if(o===0)return cr({inputs:{x:s},backend:n});let u=X().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new sse(s.shape,i):new rse(s.shape,i);return n.runWebGLProgram(u,[s],s.dtype)}var ose={kernelName:fi,backendName:"webgl",kernelFunc:ase},ise=class{constructor(e,t){this.variableNames=["Image"],this.outputShape=[],this.customUniforms=[{name:"params",type:"vec4"}];let n=e[1],r=e[2];this.outputShape=e;let s="";typeof t=="number"?s=`float outputValue = ${t.toFixed(2)};`:s=`
2021-09-11 17:11:38 +02:00
vec3 fill = vec3(${t.join(",")});
float outputValue = fill[coords[3]];`,this.userCode=`
void main() {
ivec4 coords = getOutputCoords();
int x = coords[2];
int y = coords[1];
float coordXFloat = (float(x) - params[0]) * params[3] -
(float(y) - params[1]) * params[2];
float coordYFloat = (float(x) - params[0]) * params[2] +
(float(y) - params[1]) * params[3];
int coordX = int(round(coordXFloat + params[0]));
int coordY = int(round(coordYFloat + params[1]));
${s}
if(coordX >= 0 && coordX < ${r} && coordY >= 0 && coordY < ${n}) {
outputValue = getImage(coords[0], coordY, coordX, coords[3]);
}
setOutput(outputValue);
}
2021-12-01 21:37:52 +01:00
`}},use={kernelName:_i,backendName:"webgl",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:r}=e,{radians:s,fillValue:a,center:o}=t,i=n,u=new ise(r.shape,a),[l,c]=N.getImageCenter(o,r.shape[1],r.shape[2]),d=[[l,c,Math.sin(s),Math.cos(s)]];return i.runWebGLProgram(u,[r],r.dtype,d)}},cse=`
2021-09-11 17:11:38 +02:00
// OpenGL ES does not support round function.
// The algorithm is based on banker's rounding.
float base = floor(x);
if ((x - base) < 0.5) {
return floor(x);
} else if ((x - base) > 0.5) {
return ceil(x);
} else {
if (mod(base, 2.0) == 0.0) {
return base;
} else {
return base + 1.0;
2021-04-01 19:39:54 +02:00
}
}
2021-12-01 21:37:52 +01:00
`,lse=Ze({opSnippet:cse}),dse={kernelName:mi,backendName:"webgl",kernelFunc:lse},pse="return inversesqrt(x);",hse=Ze({opSnippet:pse,cpuKernelImpl:V9}),fse={kernelName:Ga,backendName:"webgl",kernelFunc:hse},uE=class{constructor(e,t,n,r,s,a,o=!0){this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;let i=gt(s.length),u=gt(a.length),l="";n===1?l="i":n===2&&(l="i, j");let c=`getIndices(${l})`,d="";r===1?d="i":r===2&&(d="i, coords[1]");let p=`getUpdates(${d})`,h=t>1?"strides[j]":"strides";this.userCode=`
2021-09-11 17:11:38 +02:00
${i} strides = ${i}(${s});
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
2021-12-01 21:37:52 +01:00
${u} coords = getOutputCoords();
2021-09-11 17:11:38 +02:00
float sum = 0.0;
bool found = false;
for (int i = 0; i < ${e}; i++) {
int flattenedIndex = 0;
for (int j = 0; j < ${t}; j++) {
2021-12-01 21:37:52 +01:00
int index = round(${c});
2021-09-11 17:11:38 +02:00
flattenedIndex += index * ${h};
}
if (flattenedIndex == coords[0]) {
sum += ${p};
found = true;
}
}
setOutput(mix(getDefaultValue(), sum, float(found)));
}
2021-12-01 21:37:52 +01:00
`}};function mse(e){let{inputs:t,backend:n,attrs:r}=e,{indices:s,updates:a}=t,{shape:o}=r,{sliceRank:i,numUpdates:u,sliceSize:l,strides:c,outputSize:d}=N.calculateShapes(a,s,o),p=[d/l,l];if(d===0)return n.makeTensorInfo(o,s.dtype);let h=fe({inputs:{x:s},backend:n,attrs:{shape:[u,i]}}),f=fe({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=n.makeTensorInfo([],"float32",new Float32Array([0])),g=new uE(u,i,h.shape.length,f.shape.length,c,p),b=n.runWebGLProgram(g,[f,h,m],f.dtype),y=fe({inputs:{x:b},backend:n,attrs:{shape:o}});return n.disposeIntermediateTensorInfo(h),n.disposeIntermediateTensorInfo(f),n.disposeIntermediateTensorInfo(b),n.disposeIntermediateTensorInfo(m),y}var gse={kernelName:gi,backendName:"webgl",kernelFunc:mse},bse=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.outputShape=t;let r,s;if(n>4)throw Error(`Where for rank ${n} is not yet supported`);if(n===1)s="resRC",r="resRC";else{let o=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],u=[];for(let l=0;l<t.length;l++)u.push(`${o[l]}`),l<e&&i.push(`${o[l]}`);r=i.join(),s=u.join()}let a=gt(n);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${a} resRC = getOutputCoords();
float cVal = getC(${r});
if (cVal >= 1.0) {
setOutput(getA(${s}));
} else {
setOutput(getB(${s}));
}
2021-03-26 15:26:02 +01:00
}
2021-12-01 21:37:52 +01:00
`}};function yse(e){let{inputs:t,backend:n}=e,{condition:r,t:s,e:a}=t,o=new bse(r.shape.length,s.shape,s.shape.length);return n.runWebGLProgram(o,[r,s,a],In(s.dtype,a.dtype))}var vse={kernelName:bi,backendName:"webgl",kernelFunc:yse},xse=`
2021-09-11 17:11:38 +02:00
// Stable and Attracting Fixed Point (0, 1) for Normalized Weights.
// see: https://arxiv.org/abs/1706.02515
2021-12-01 21:37:52 +01:00
float scaleAlpha = ${N.SELU_SCALEALPHA};
float scale = ${N.SELU_SCALE};
2021-09-11 17:11:38 +02:00
return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);
2021-12-01 21:37:52 +01:00
`,wse=Ze({opSnippet:xse}),kse={kernelName:lc,backendName:"webgl",kernelFunc:wse},cE="return 1.0 / (1.0 + exp(-1.0 * x));",Ise=Ze({opSnippet:cE,packedOpSnippet:cE,cpuKernelImpl:U9}),Sse={kernelName:ja,backendName:"webgl",kernelFunc:Ise},Cse=`
2021-09-11 17:11:38 +02:00
if (isnan(x)) { return 0.0; }
return sign(x);
2021-12-01 21:37:52 +01:00
`,Tse=Ze({opSnippet:Cse}),Nse={kernelName:dc,backendName:"webgl",kernelFunc:Tse},_se=k_+`
2021-09-11 17:11:38 +02:00
return sin(x);
2021-12-01 21:37:52 +01:00
`,Ese=Ze({opSnippet:_se}),Ase={kernelName:Ha,backendName:"webgl",kernelFunc:Ese},$se=`
2021-09-11 17:11:38 +02:00
float e2x = exp(x);
return (e2x - 1.0 / e2x) / 2.0;
2021-12-01 21:37:52 +01:00
`,Fse=Ze({opSnippet:$se}),Dse={kernelName:vi,backendName:"webgl",kernelFunc:Fse},Rse=`
2021-09-11 17:11:38 +02:00
float epsilon = 1.1920928955078125e-7;
float threshold = log(epsilon) + 2.0;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
bool too_large = x > -threshold;
bool too_small = x < threshold;
float result;
float exp_x = exp(x);
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
if (too_large){
result = x;
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
else if (too_small){
result = exp_x;
}
else{
result = log(exp_x + 1.0);
2021-04-01 19:39:54 +02:00
}
2021-09-11 17:11:38 +02:00
return result;
2021-12-01 21:37:52 +01:00
`,Pse=Ze({opSnippet:Rse}),Ose={kernelName:pc,backendName:"webgl",kernelFunc:Pse},Mse=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockShape:a,paddings:o}=r;w.assert(s.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");let i=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...o);for(let b=1+a.length;b<s.shape.length;++b)u.push([0,0]);let l=[],c=oE({inputs:{x:s},backend:n,attrs:{paddings:u,constantValue:0}}),d=N.getReshaped(c.shape,a,i,!1),p=N.getPermuted(d.length,a.length,!1),h=N.getReshapedPermuted(c.shape,a,i,!1),f=fe({inputs:{x:c},backend:n,attrs:{shape:d}}),m=Dn({inputs:{x:f},backend:n,attrs:{perm:p}}),g=fe({inputs:{x:m},backend:n,attrs:{shape:h}});return l.push(c),l.push(f),l.push(m),l.forEach(b=>n.disposeIntermediateTensorInfo(b)),g},Lse={kernelName:xi,backendName:"webgl",kernelFunc:Mse};function Bse(e){let{inputs:t,backend:n}=e,{indices:r,values:s,denseShape:a,defaultValue:o}=t;if(a.shape.length!==1)throw new Error(`Dense shape must be a vector, saw:
2021-09-11 17:11:38 +02:00
${a.shape}`);if(r.shape.length!==2)throw new Error(`Indices must be a matrix, saw:
${r.shape}`);if(s.shape.length!==1)throw new Error(`Values must be a vector, saw:
${s.shape}`);if(o.shape.length!==0)throw new Error(`Default value must be a scalar, saw:
2021-12-01 21:37:52 +01:00
${o.shape}`);let i=n.readSync(r.dataId),u=n.readSync(s.dataId),l=n.readSync(a.dataId),c=n.readSync(o.dataId)[0],[d,p,h,f,m]=H9(i,r.shape,r.dtype,u,s.dtype,l,c);return[n.makeTensorInfo(p,r.dtype,d),n.makeTensorInfo([p[0]],s.dtype,h),n.makeTensorInfo([f.length],"bool",new Uint8Array(f.map(g=>Number(g)))),n.makeTensorInfo([m.length],r.dtype,new Int32Array(m))]}var zse={kernelName:sd,backendName:"webgl",kernelFunc:Bse};function Wse(e){let{inputs:t,backend:n}=e,{inputIndices:r,inputShape:s,newShape:a}=t;if(r.shape.length!==2)throw new Error(`Input indices should be a matrix but received shape ${r.shape}`);if(s.shape.length!==1)throw new Error(`Input shape should be a vector but received shape ${s.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=Array.from(n.readSync(s.dataId)),i=n.readSync(r.dataId),u=Array.from(n.readSync(a.dataId)),[l,c,d]=j9(i,r.shape,r.dtype,o,u);return[n.makeTensorInfo(c,r.dtype,l),n.makeTensorInfo([d.length],a.dtype,new Int32Array(d))]}var Vse={kernelName:hc,backendName:"webgl",kernelFunc:Wse};function Use(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2021-09-11 17:11:38 +02:00
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2021-12-01 21:37:52 +01:00
${a.shape}`);let o=n.readSync(r.dataId),i=n.readSync(s.dataId),u=n.readSync(a.dataId),[l,c]=u_(o,r.shape,r.dtype,i,u,!0);return n.makeTensorInfo(c,r.dtype,l)}var Gse={kernelName:ad,backendName:"webgl",kernelFunc:Use};function Hse(e){let{inputs:t,backend:n}=e,{data:r,indices:s,segmentIds:a}=t;if(r.shape.length<1)throw new Error("Data should be at least 1 dimensional but received scalar");if(s.shape.length!==1)throw new Error(`Indices should be a vector but received shape
2021-09-11 17:11:38 +02:00
${s.shape}`);if(a.shape.length!==1)throw new Error(`Segment ids should be a vector but received shape
2021-12-01 21:37:52 +01:00
${a.shape}`);let o=n.readSync(r.dataId),i=n.readSync(s.dataId),u=n.readSync(a.dataId),[l,c]=u_(o,r.shape,r.dtype,i,u);return n.makeTensorInfo(c,r.dtype,l)}var jse={kernelName:od,backendName:"webgl",kernelFunc:Hse};function qse(e){let{inputs:t,backend:n,attrs:r}=e,{sparseIndices:s,sparseValues:a,defaultValue:o}=t,{outputShape:i}=r,{sliceRank:u,numUpdates:l,strides:c,outputSize:d}=N.calculateShapes(a,s,i),p=!1,h=new uE(l,u,s.shape.length,a.shape.length,c,[d,1],p),f=n.runWebGLProgram(h,[a,s,o],a.dtype),m=fe({inputs:{x:f},backend:n,attrs:{shape:i}});return n.disposeIntermediateTensorInfo(f),m}var Kse={kernelName:id,backendName:"webgl",kernelFunc:qse};function Xse(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=r,i=w.parseAxisParam(o,s.shape)[0],u=N.prepareSplitSize(s,a,i),l=s.shape.length,c=new Array(l).fill(0),d=s.shape.slice();return u.map(p=>{let h=[...d];h[i]=p;let f=Qc({inputs:{x:s},backend:n,attrs:{begin:c,size:h}});return c[i]+=p,f})}var Yse={kernelName:wi,backendName:"webgl",kernelFunc:Xse},lE="return sqrt(x);",Qse=Ze({opSnippet:lE,packedOpSnippet:lE,cpuKernelImpl:q9}),Zse={kernelName:qa,backendName:"webgl",kernelFunc:Qse},Jse="return x * x;",eae=Ze({opSnippet:Jse}),tae={kernelName:fc,backendName:"webgl",kernelFunc:eae},dE="return (a - b) * (a - b);",nae=hn({opSnippet:dE,packedOpSnippet:dE}),rae={kernelName:Ya,backendName:"webgl",kernelFunc:nae};function sae({inputs:e,attrs:t,backend:n}){let{x:r}=e,s=ts+`
2021-09-11 17:11:38 +02:00
return x > 0.0 ? 1.0 : float(${t.alpha});
2021-12-01 21:37:52 +01:00
`,a=new To(r.shape,s);return n.runWebGLProgram(a,[r],r.dtype)}var aae={kernelName:eo,backendName:"webgl",kernelFunc:sae},oae=class{constructor(e,t,n){this.variableNames=["x"],this.outputShape=n;let r=n.length,s=gt(n.length),a=gt(n.length),o="";if(r===1)o="coords * strides + begin";else{let i=0;o=n.map((u,l)=>(i++,n.length===1?`coords * strides[${l}] + begin[${l}]`:`coords[${i-1}] * strides[${l}] + begin[${l}]`)).join(",")}this.userCode=`
2021-09-11 17:11:38 +02:00
${s} begin = ${s}(${e});
${s} strides = ${s}(${t});
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
void main() {
${a} coords = getOutputCoords();
setOutput(getX(${o}));
2021-04-01 19:39:54 +02:00
}
2021-12-01 21:37:52 +01:00
`}};function iae(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,end:o,strides:i,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:d,shrinkAxisMask:p}=r,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=$t.sliceInfo(s.shape,a,o,i,u,l,c,d,p),k;if(m)k=fe({inputs:{x:s},backend:n,attrs:{shape:f}});else if(g||b){w.assert(s.shape.length>=1,()=>`Input must have rank at least 1, got: ${s.shape.length}`);let C=$t.computeOutShape(y,v,x),E=Qc({inputs:{x:s},backend:n,attrs:{begin:y,size:C}});k=fe({inputs:{x:E},backend:n,attrs:{shape:f}}),n.disposeIntermediateTensorInfo(E)}else if(n.shouldExecuteOnCPU([s])){let E=n.readSync(s.dataId),F=$e(s.shape,s.dtype,E),O=K9(h,F,x,y);k=n.makeTensorInfo(f,s.dtype,O.values)}else{let E=new oae(y,x,h);k=n.runWebGLProgram(E,[s],s.dtype)}let T=fe({inputs:{x:k},backend:n,attrs:{shape:f}});return n.disposeIntermediateTensorInfo(k),T}var uae={kernelName:ki,backendName:"webgl",kernelFunc:iae};function cae(e){let{inputs:t,backend:n,attrs:r}=e,{separator:s,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:l}=r,{data:c,dataSplits:d}=t,p=n.readSync(c.dataId),h=n.readSync(d.dataId),[f,m]=X9(p,h,s,a,o,i,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(d.shape,"int32",m)]}var lae={kernelName:ud,backendName:"webgl",kernelFunc:cae};function dae(e){let{inputs:t,backend:n,attrs:r}=e,{skipEmpty:s}=r,{input:a,delimiter:o}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(a.shape.length!==1)throw new Error(`Input must be a vector, got shape: ${a.shape}`);if(o.shape.length!==0)throw new Error(`Delimiter must be a scalar, got shape: ${o.shape}`);let i=n.readSync(a.dataId),u=n.readSync(o.dataId)[0],[l,c,d]=Y9(i,u,s),p=c.length;return[n.makeTensorInfo([p,2],"int32",l),n.makeTensorInfo([p],"string",c),n.makeTensorInfo([2],"int32",new Int32Array(d))]}var pae={kernelName:Bh,backendName:"webgl",kernelFunc:dae};function hae(e){let{inputs:t,backend:n,attrs:r}=e,{numBuckets:s}=r,{input:a}=t;if(a.dtype!=="string")throw new Error("Input must be of datatype string");if(s<=0)throw new Error("Number of buckets must be at least 1");let o=n.readSync(a.dataId),i=Q9(o,s);return n.makeTensorInfo(a.shape,"int32",i)}var fae={kernelName:zh,backendName:"webgl",kernelFunc:hae},mae="return tan(x);",gae=Ze({opSnippet:mae}),bae={kernelName:Ii,backendName:"webgl",kernelFunc:gae},yae=`
2021-09-11 17:11:38 +02:00
float e2x = exp(-2.0 * abs(x));
return sign(x) * (1.0 - e2x) / (1.0 + e2x);
2021-12-01 21:37:52 +01:00
`,vae=Ze({opSnippet:yae}),xae={kernelName:Za,backendName:"webgl",kernelFunc:vae},wae=class{constructor(e,t){this.variableNames=["A"];let n=new Array(e.length);for(let a=0;a<n.length;a++)n[a]=e[a]*t[a];this.outputShape=n,this.rank=n.length;let r=gt(this.rank),s=kae(e);this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
${r} resRC = getOutputCoords();
setOutput(getA(${s}));
}
2021-12-01 21:37:52 +01:00
`}};function kae(e){let t=e.length;if(t>5)throw Error(`Tile for rank ${t} is not yet supported`);if(t===1)return`imod(resRC, ${e[0]})`;let n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[];for(let s=0;s<e.length;s++)r.push(`imod(${n[s]}, ${e[s]})`);return r.join()}function pE(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reps:a}=r;if(s.dtype==="string"||s.shape.length>5){let u=n.readSync(s.dataId),l=s.dtype==="string"?u.map(p=>w.decodeString(p)):u,c=$e(s.shape,s.dtype,l),d=J9(c,a);return n.makeTensorInfo(d.shape,d.dtype,d.values)}let o=new wae(s.shape,a);return n.runWebGLProgram(o,[s],s.dtype)}var Iae={kernelName:As,backendName:"webgl",kernelFunc:pE},Sae=class{constructor(e){this.variableNames=["x","indices"],this.customUniforms=[{name:"n",type:"int"},{name:"firstPass",type:"int"},{name:"negativeInf",type:"float"},{name:"dir",type:"int"},{name:"inc",type:"int"}],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int elemIdx = coords[1];
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
// We compare elements pair-wise within a group of size 2 * inc.
// The comparing rule for each group alternates between ascending
// and descending. Within each group, we compare each pair at
// positions i and i+inc. To decide whether an element at position i
// is x0 or x1, we mod it by 2 * inc, if the result is smaller than
// inc, it is in the first half of the group, we denote it as x0,
// otherwise we denote it as x1.
// For example, as shown in the Bitonic top K paper referenced above,
// Figure5(a) shows that element[1] is in the
// second half of the group when group size is 2, but it is in the
// first half of the group when group size is 4.
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
bool isFirstInPair = imod(elemIdx, 2 * inc) < inc;
int i = isFirstInPair ? elemIdx : elemIdx - inc;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
int i0 = firstPass == 1 ? i : int(getIndices(batch, i));
int i1 = firstPass == 1 ? i + inc : int(getIndices(batch, i + inc));
float x0 = i0 < n ? getX(batch, i0) : negativeInf;
float x1 = i1 < n ? getX(batch, i1) : negativeInf;
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
// Denotes which direction indices are in (ascending or descending).
bool reverse = imod(elemIdx, 2 * dir) >= dir;
bool isGreater = x0 > x1 || (x0 == x1 && i1 > i0);
if (reverse == isGreater) { // Elements in opposite order of direction
int iTemp = i0;
i0 = i1;
i1 = iTemp;
}
if (isFirstInPair) {
setOutput(float(i0));
} else {
setOutput(float(i1));
}
}
2021-12-01 21:37:52 +01:00
`}},Cae=class{constructor(e){this.variableNames=["x","indices"],this.customUniforms=[{name:"n",type:"int"},{name:"firstPass",type:"int"},{name:"k",type:"int"}],this.outputShape=e,this.userCode=`
2021-09-11 17:11:38 +02:00
void main() {
// Takes max of indices (0, k), (1, k + 1), (2, k + 2) ...
ivec2 coords = getOutputCoords();
int batch = coords[0];
int elemIdx = coords[1];
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
// The output size is half of the previous size.
// If the previous sequence is | | | | _ _ _ _ | | | | _ _ _ _ (k=4),
// we only need to output the indices at positions |, the indices at
// positions _ can be thrown away, see Figure5(b) After Phase 2
// (Merge phase) in the Bitonic Top K paper referenced above.
// For example, the paper shows we only need to output the orange bars.
// The output sequence should look like this | | | | | | | |.
// Because the sequence is halved, to map the output index back
// to the previous sequence to find the corresponding value,
// we need to double the index. When we double the index,
// we basically interpolate a position, so 2i looks like
// | _ | _ | _ | _ | _ | _ | _. We move the | to the first k position
// of each 2k positions by - elemIdx % k. E.g. for output at
// index 4,5,6,7, we want to get the corresponding element at
// original index 8,9,10,11, for output at index 8,9,10,11,
// we want to get the corresponding element at original index
// 16,17,18,19, so on and so forth.
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
int i = elemIdx < k ? elemIdx : (elemIdx * 2 - imod(elemIdx, k));
int i0 = firstPass == 1 ? i : int(getIndices(batch, i));
int i1 = firstPass == 1 ? i + k : int(getIndices(batch, i + k));
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
float x0 = getX(batch, i0);
float x1 = i1 < n ? getX(batch, i1) : x0;
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
setOutput(x0 >= x1 ? float(i0) : float(i1));
}
2021-12-01 21:37:52 +01:00
`}};function iu(e,t){t!==null&&e.disposeIntermediateTensorInfo(t)}function hE(e){let t=1;for(;t<e;)t*=2;return t}function Tae(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{k:a,sorted:o}=r,i=X().getNumber("TOPK_LAST_DIM_CPU_HANDOFF_SIZE_THRESHOLD"),u=X().getNumber("TOPK_K_CPU_HANDOFF_THRESHOLD"),l=s.shape,c=l[l.length-1];if(n.shouldExecuteOnCPU([s])||c<i||a>u){let O=n.readSync(s.dataId),[D,R]=eQ(O,l,s.dtype,a,o);return[n.makeTensorInfo(D.shape,D.dtype,D.values),n.makeTensorInfo(R.shape,R.dtype,R.values)]}if(a===0)return l[l.length-1]=0,[n.makeTensorInfo(l,s.dtype,[]),n.makeTensorInfo(l,"int32",[])];if(c===1)return[s,pp({attrs:{shape:l,dtype:"int32",value:0},backend:n})];let d=n.texData.get(s.dataId),p=d!==null&&d.isPacked,h=p?n.unpackTensor(s):s,m=w.sizeFromShape(l)/c,g=fe({inputs:{x:h},attrs:{shape:[m,c]},backend:n});p&&iu(n,h);let b=hE(a),y=hE(c),v=null,x=()=>v===null?[g,g]:[g,v],k=(O,D,R)=>{let _=x(),L=new Sae(R),j=[[c],[v===null?1:0],[Number.NEGATIVE_INFINITY],[O],[D]],K=v;v=n.runWebGLProgram(L,_,"int32",j),iu(n,K)};for(let O=1;O<b;O*=2){let D=O*2;for(let R=O;R>=1;R/=2)k(D,R,[m,y])}for(let O=y;O>b;O/=2){let D=x(),R=new Cae([m,O/2]),L=[[c],[v===null?1:0],[b]],U=v;v=n.runWebGLProgram(R,D,"int32",L),iu(n,U);let j=b/2,K=j*2;for(let q=j;q>=1;q/=2)k(K,q,v.shape)}let T=v;v=Qc({inputs:{x:v},backend:n,attrs:{begin:0,size:[m,a]}}),iu(n,T);let C=Z_({inputs:{x:g,indices:v},backend:n,attrs:{axis:1,batchDims:1}});iu(n,g);let E=l.slice(0,-1);E.push(a),T=v,v=fe({inputs:{x:v},attrs:{shape:E},backend:n}),iu(n,T);let F=C;return C=fe({inputs:{x:C},attrs:{shape:E},backend:n}),iu(n,F),[C,v]}var Nae={kernelName:Si,backendName:"webgl",kernelFunc:Tae},_ae=class{constructor(e,t,n,r,s,a){this.variableNames=["Image","Transforms"],this.outputShape=a;let o=n==="nearest"?1:2,i;switch(r){case"constant":i=1;break;case"reflect":i=2;break;case"wrap":i=3;break;case"nearest":i=4;break;default:i=1;break}this.userCode=`
2021-09-11 17:11:38 +02:00
float mapCoord(float outCoord, float len) {
float inCoord = outCoord;
if(${i} == 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 (${i} == 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 (${i} == 4) {
return clamp(outCoord, 0.0, len - 1.0);
} else {
return outCoord;
}
}
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
float readWithFillValue(int batch, int coordY, int coordX,
int channel) {
float outputValue;
if (0 <= coordY && coordY < ${e} && 0 <= coordX && coordX < ${t}) {
outputValue = getImage(batch, coordY, coordX, channel);
} else {
outputValue = float(${s});
}
return outputValue;
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec4 coords = getOutputCoords();
float outputValue;
int batch = coords[0];
int x = coords[2];
int y = coords[1];
int channel = coords[3];
float xf = float(x);
float yf = float(y);
float a1 = getTransforms(batch, 0);
float a2 = getTransforms(batch, 1);
float a3 = getTransforms(batch, 2);
float b1 = getTransforms(batch, 3);
float b2 = getTransforms(batch, 4);
float b3 = getTransforms(batch, 5);
float c1 = getTransforms(batch, 6);
float c2 = getTransforms(batch, 7);
float projection = c1 * xf + c2 * yf + 1.0;
if (projection == 0.0) {
outputValue = float(${s});
} else {
float inX = (a1 * xf + a2 * yf + a3) / projection;
float inY = (b1 * xf + b2 * yf + b3) / projection;
float mapX = mapCoord(inX, float(${t}));
float mapY = mapCoord(inY, float(${e}));
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
if (${o} == 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);
}
2021-12-01 21:37:52 +01:00
`}};function Eae(e){let{inputs:t,backend:n,attrs:r}=e,{image:s,transforms:a}=t,{interpolation:o,fillMode:i,fillValue:u,outputShape:l}=r,[c,d,p,h]=s.shape,[f,m]=l!=null?l:[d,p],g=[c,f,m,h],b=new _ae(d,p,o,i,u,g);return n.runWebGLProgram(b,[s,a],"float32")}var Aae={kernelName:Ci,backendName:"webgl",kernelFunc:Eae};function $ae(e){let{inputs:t,attrs:n,backend:r}=e,{axis:s}=n,{x:a}=t;Gc(a,"unique"),console.warn("WARNING: ","UI might be locked temporarily as data is being downloaded");let o=r.readSync(a.dataId),{outputValues:i,outputShape:u,indices:l}=tQ(o,s,a.shape,a.dtype);return[r.makeTensorInfo(u,a.dtype,i),r.makeTensorInfo([l.length],"int32",l)]}var Fae={kernelName:Wh,backendName:"webgl",kernelFunc:$ae};function Dae(e){let{inputs:t,backend:n,attrs:r}=e,{value:s}=t,{axis:a}=r;a<0&&(a+=s.shape.length);let o=s,i=o.shape.length,u=s.shape[a],l=new Array(i-1),c=0;for(let m=0;m<i;m++)m!==a&&(l[c++]=o.shape[m]);let d=[],p=new Array(i).fill(0),h=o.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){p[a]=m;let g=Qc({inputs:{x:o},backend:n,attrs:{begin:p,size:h}}),b=fe({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,d.push(g)}return d.forEach(m=>n.disposeIntermediateTensorInfo(m)),f}var Rae={kernelName:Ti,backendName:"webgl",kernelFunc:Dae},Pae=class{constructor(e,t){this.variableNames=["x","segmentIds"];let n=e.windowSize,r=e.batchSize,s=e.inSize,a=e.numSegments,o=a*Math.ceil(s/n);this.outputShape=[r,o];let i="0.0",u="sumValue",l=Math.floor(n/4)*4,c=n%4,d=`
2021-09-11 17:11:38 +02:00
sumValue += dot(values, segFilter);
`,p="";s%n>0&&(p=`
if (inIdx < 0 || inIdx >= ${s}) {
return initializationValue;
}
`);let h="";s%n>0&&(h=`
if (inIdx < 0 || inIdx >= ${s}) {
return -1.0;
}
`),this.userCode=`
const float initializationValue = ${i};
2021-03-31 13:01:22 +02:00
2021-09-11 17:11:38 +02:00
float getValue(int batch, int inIdx) {
${p}
return getX(batch, inIdx);
2021-03-26 15:26:02 +01:00
}
2020-10-11 18:41:17 +02:00
2021-09-11 17:11:38 +02:00
float getSegmentIdAtIndex(int inIdx) {
${h}
return getSegmentIds(inIdx);
2021-03-31 13:01:22 +02:00
}
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
void main() {
ivec2 coords = getOutputCoords();
int batch = coords[0];
int outIdx = coords[1];
int inOffset = int(floor(float(outIdx) / float(
${a})) * float(${n}));
int currentSeg = int(mod(float(outIdx), float(${a})));
2021-03-09 23:32:33 +01:00
2021-09-11 17:11:38 +02:00
float sumValue = 0.0;
2021-03-09 23:32:33 +01:00
2021-10-22 15:06:43 +02:00
for (int i = 0; i < ${l}; i += 4) {
2021-09-11 17:11:38 +02:00
int inIdx = inOffset + i;
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
getValue(batch, inIdx + 3)
);
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${d}
2020-12-15 14:15:43 +01:00
}
2020-10-29 05:16:50 +01:00
2021-10-22 15:06:43 +02:00
int inIdx = inOffset + ${l};
2021-12-01 21:37:52 +01:00
if (${c===1}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
initializationValue,
initializationValue,
initializationValue
);
2020-12-15 14:15:43 +01:00
2021-09-11 17:11:38 +02:00
int inIdxSeg = int(getSegmentIdAtIndex(inIdx));
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
0,
0,
0
);
2021-03-26 15:26:02 +01:00
2021-09-11 17:11:38 +02:00
${d}
2021-12-01 21:37:52 +01:00
} else if (${c===2}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
initializationValue,
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
0,
0
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
${d}
2021-12-01 21:37:52 +01:00
} else if (${c===3}) {
2021-09-11 17:11:38 +02:00
vec4 values = vec4(
getValue(batch, inIdx),
getValue(batch, inIdx + 1),
getValue(batch, inIdx + 2),
initializationValue
);
2021-04-01 19:39:54 +02:00
2021-09-11 17:11:38 +02:00
vec4 segFilter = vec4(
int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
0
);
2020-10-29 05:16:50 +01:00
2021-09-11 17:11:38 +02:00
${d}
}
2021-12-01 21:37:52 +01:00
setOutput(${u});
}
`}};function Oae(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,segmentIds:a}=t,{numSegments:o}=r,i=s.shape.length,u=[],l=0,c=N.getAxesPermutation([l],i),d=s;c!=null&&(d=Dn({inputs:{x:s},backend:n,attrs:{perm:c}}),u.push(d),l=N.getInnerMostAxes(1,i)[0]);let p=N.segment_util.computeOutShape(d.shape,l,o),h=w.sizeFromShape([d.shape[l]]),f=fe({inputs:{x:d},backend:n,attrs:{shape:[-1,h]}});u.push(f);let m=yd(s.dtype),g=(x,k,T,C,E)=>{let F=x.shape[0],O=x.shape[1],D=N.segment_util.segOpComputeOptimalWindowSize(O,E),R={windowSize:D,inSize:O,batchSize:F,numSegments:E},_=new Pae(R,k),L=n.compileAndRun(_,[x,T],C);if(u.push(L),L.shape[1]===E)return L;let U=iE({backend:n,attrs:{start:0,stop:E,step:1,dtype:"float32"}}),j=pE({inputs:{x:U},backend:n,attrs:{reps:[O/D]}});return u.push(U),u.push(j),g(L,k,j,C,E)},b=g(f,"unsortedSegmentSum",a,m,o),y=fe({inputs:{x:b},backend:n,attrs:{shape:p}}),v=y;if(c!=null){u.push(y);let x=N.getUndoAxesPermutation(c);v=Dn({inputs:{x:v},backend:n,attrs:{perm:x}})}return u.forEach(x=>n.disposeIntermediateTensorInfo(x)),v}var Mae={kernelName:cd,backendName:"webgl",kernelFunc:Oae},Lae=[pne,mne,YQ,ZQ,tZ,sZ,oZ,cZ,dZ,hZ,bZ,vZ,kZ,CZ,FZ,_Z,PZ,BZ,MZ,UZ,HZ,qZ,QZ,sJ,oJ,uJ,fJ,gJ,xJ,IJ,$Q,_J,LJ,zJ,FJ,GJ,jJ,VJ,XJ,ZJ,tee,ree,aee,uee,fee,gee,lee,vee,kee,See,_ee,Fee,Oee,Bee,zee,Wee,Uee,Hee,qee,Xee,Qee,tte,ste,ite,cte,pte,mte,vte,Ite,AQ,Cte,TJ,_te,$te,Rte,DQ,Lte,Vte,Gte,Qte,Kte,tne,sne,une,bne,Cne,Ine,Ene,$ne,Dne,wne,Pne,Mne,Wne,Hne,Xne,rre,LQ,are,ure,dre,fre,lJ,bre,vre,wre,Sre,_re,PQ,Are,$re,dJ,Jne,Rre,Vre,Lre,zQ,jre,Xre,Jre,nse,ose,use,dse,fse,gse,vse,kse,Sse,Nse,Ase,Dse,nJ,tre,Ose,Lse,zse,Vse,Gse,jse,Kse,Yse,Zse,tae,rae,aae,uae,lae,pae,fae,ere,qQ,bae,xae,Iae,Nae,Aae,KQ,Fae,Rae,Mae,yre];for(let e of Lae)gc(e);var vs=X();vs.registerFlag("WEBGPU_DEFERRED_SUBMIT_BATCH_SIZE",()=>15);vs.registerFlag("WEBGPU_CPU_FORWARD",()=>!0);vs.registerFlag("WEBGPU_MATMUL_WORK_PER_THREAD",()=>4);vs.registerFlag("WEBGPU_USE_NAIVE_CONV2D",()=>!1);vs.registerFlag("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE",()=>!1);vs.registerFlag("WEBGPU_CONV_SEPARATE_IM2COL_SHADER",()=>!1);vs.registerFlag("WEBGPU_USE_LOW_POWER_GPU",()=>!1);vs.registerFlag("WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD",()=>1e3);vs.registerFlag("WEBGPU_USE_PROFILE_TOOL",()=>!1);vs.registerFlag("WEBGPU_USE_IMPORT",()=>!1);function Bae(e,t){if(Math.max(...e)>3)throw new Error("Cannot symbolically compute strides for rank > 4 tensor.");let n=e.length,r=e.map(a=>`${t}[${a}]`),s=new Array(n-1);s[n-2]=r[n-1];for(let a=n-3;a>=0;--a)s[a]=`(${s[a+1]} * ${r[a+1]})`;return s}function on(e){if(e<=1)return"i32";if(e===2)return"vec2<i32>";if(e===3)return"vec3<i32>";if(e===4)return"vec4<i32>";throw Error(`GPU for rank ${e} is not yet supported`)}function Bm(e,t){return e==="float32"?t?"vec4<f32>":"f32":e==="int32"||e==="bool"?t?"vec4<i32>":"i32":e}function zm(){return`
[[stage(compute), workgroup_size(workGroupSizeX, workGroupSizeY, workGroupSizeZ)]]
`}function hk(){return`
${zm()}
fn main([[builtin(local_invocation_id)]] localId : vec3<u32>,
[[builtin(global_invocation_id)]] globalId : vec3<u32>,
[[builtin(num_workgroups)]] numWorkgroups: vec3<u32>)
`}function el(){return`
${zm()}
fn main([[builtin(local_invocation_id)]] localId : vec3<u32>,
[[builtin(global_invocation_id)]] globalId : vec3<u32>)
`}function Ue(){return`
${hk()} {
let index = getGlobalIndex(globalId, localId, numWorkgroups);
`}function zae(e,t,n,r=!1){let s=`
let workGroupSizeX = ${n.workGroupSize[0]}u;
let workGroupSizeY = ${n.workGroupSize[1]}u;
let workGroupSizeZ = ${n.workGroupSize[2]}u;`;if(r===!0){let h=gE(t.shape),f=`
[[block]] struct Matrix0 {
numbers: array<${Bm(t.dtype,n.isVec4)}>;
};
[[block]] struct Uniform {
size : i32;
numChannels : i32;
outShapeStrides : vec2<i32>;
dispatchSize : vec3<u32>;
};
[[group(0), binding(0)]] var<storage, write> result : Matrix0;
[[group(0), binding(2)]] var<uniform> uniforms: Uniform;
`;return[fE,f,s,mE,h,n.getUserCode()].join(`
`)}let a=[],o="[[block]] struct Uniforms { NAN : f32; ";n.variableNames.forEach((h,f)=>{o+=`${h.charAt(0).toLowerCase()+h.slice(1)}Shape : ${on(e[f].shape.length)}; `}),o+=`outShape : ${on(t.shape.length)} ; `;let i=t.shape.length-1;o+=`
outShapeStrides: ${on(i)}; `,n.size&&(o+="size : i32; "),n.uniforms&&(o+=n.uniforms),o+="};",a.push(o),n.atomic?a.push(`
[[block]] struct Matrix0 {
numbers: array<atomic<i32>>;
};
[[group(0), binding(0)]] var<storage, read_write> result : Matrix0;
`):a.push(`
[[block]] struct Matrix0 {
numbers: array<${Bm(t.dtype,n.isVec4)}>;
};
[[group(0), binding(0)]] var<storage, write> result : Matrix0;
`),n.variableNames.forEach((h,f)=>{a.push(`
[[block]] struct Matrix${1+f} {
numbers: array<${Bm(e[f].dtype,n.isVec4)}>;
};
[[group(0), binding(${1+f})]] var<storage, read> ${h} : Matrix${1+f};
`)}),o!==""&&a.push(`
[[group(0), binding(${1+n.variableNames.length})]] var<uniform> uniforms : Uniforms;
`),a.push(s);let[u,l]=jae(t.shape,n.dispatchLayout),c=gE(t.shape),d=[fE,a.join(`
`),mE,c,u,Wae(t.shape.length)];if(n.atomic||d.push(Vae(t.shape,t.dtype,n.isVec4)),l===t.shape.length){let h=e.map(f=>Uae(f,t.shape,n.isVec4,n.dispatchLayout.x.length===t.shape.length)).join(`
`);d.push(h)}return d.push(n.getUserCode()),d.join(`
`)}var fE=`
fn idiv(a: i32, b: i32, sign: f32) -> i32 {
var res: i32 = a / b;
let mod: i32 = a % b;
if (sign < 0. && mod != 0) {
res = res - 1;
}
return res;
}
fn isNanCustom(val : f32) -> bool {
if (val > 0.0) {
return false;
}
if (val < 0.0) {
return false;
}
if (val == 0.0) {
return false;
}
return true;
}
fn isNanCustomVec4F32(val : vec4<f32>) -> vec4<f32> {
var res = vec4<f32> (0.0);
for (var i = 0u; i < 4u; i = i + 1u) {
if (isNanCustom(val[i])) {
res[i] = 1.0;
} else {
res[i] = 0.0;
2021-09-11 17:11:38 +02:00
}
2021-12-01 21:37:52 +01:00
}
return res;
}
// Checks whether coordinates lie within the bounds of the shape.
fn coordsInBounds4D(coord : vec4<i32>, shape : vec4<i32>) -> bool {
return all(coord >= vec4<i32>(0)) &&
all(coord < shape);
}
fn coordsInBounds3D(coord : vec3<i32>, shape : vec3<i32>) -> bool {
return all(coord >= vec3<i32>(0)) &&
all(coord < shape);
}
fn coordsInBounds2D(coord : vec2<i32>, shape : vec2<i32>) -> bool {
return all(coord >= vec2<i32>(0)) &&
all(coord < shape);
}
`,mE=`
fn getFlatIndex1D(coord : i32, shape : i32) -> i32 {
return coord;
}
fn getFlatIndex2D(coords : vec2<i32>, shape : vec2<i32>) -> i32 {
return dot(coords, vec2<i32>(shape.y, 1));
}
fn getFlatIndex3D(coords : vec3<i32>, shape : vec3<i32>) -> i32 {
return dot(coords, vec3<i32>(shape.y * shape.z, shape.z, 1));
}
fn getFlatIndex4D(coords : vec4<i32>, shape : vec4<i32>) -> i32 {
return dot(coords, vec4<i32>(
shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1));
}
// Only used when the y/z dimension of workgroup size is 1.
fn getGlobalIndex(globalId : vec3<u32>, localId : vec3<u32>, numWorkgroups: vec3<u32>) -> i32 {
if (numWorkgroups.y == 1u && numWorkgroups.z == 1u) {
return i32(globalId.x);
}
let localInvocationIndex = localId.z * workGroupSizeX * workGroupSizeY +
localId.y * workGroupSizeX + localId.x;
let workGroupID = (globalId - localId)/vec3<u32>(
workGroupSizeX, workGroupSizeY, workGroupSizeZ);
return i32((workGroupID.z * numWorkgroups.x * numWorkgroups.y +
workGroupID.y * numWorkgroups.x + workGroupID.x) *
(workGroupSizeX * workGroupSizeY * workGroupSizeZ) +
localInvocationIndex);
}
`;function Wae(e){let t="";switch(e){case 0:case 1:t+=`
fn getOutputFlatIndex(coords : i32) -> i32 {
return coords;
}
`;break;case 2:t+=`
fn getOutputFlatIndex(coords : vec2<i32>) -> i32 {
return dot(coords, vec2<i32>(uniforms.outShapeStrides, 1));
}
`;break;case 3:t+=`
fn getOutputFlatIndex(coords : vec3<i32>) -> i32 {
return dot(coords, vec3<i32>(uniforms.outShapeStrides.x, uniforms.outShapeStrides.y, 1));
}
`;break;case 4:t+=`
fn getOutputFlatIndex(coords : vec4<i32>) -> i32 {
return dot(coords, vec4<i32>(
uniforms.outShapeStrides.x, uniforms.outShapeStrides.y, uniforms.outShapeStrides.z, 1));
}
`;break;default:w.assert(!1,()=>`Unsupported ${e}D shape`);break}return t}function Vae(e,t,n){let r=e.length,s=Bm(t,n),a;if(n?a=`fn setOutputFlat(flatIndex : i32, value : vec4<f32>) {
result.numbers[flatIndex] = ${s}(value);
}
fn setOutputFlatI32(flatIndex : i32, value : vec4<i32>) {
result.numbers[flatIndex] = ${s}(value);
}`:a=`fn setOutputFlat(flatIndex : i32, value : f32) {
result.numbers[flatIndex] = ${s}(value);
}
fn setOutputFlatI32(flatIndex : i32, value : i32) {
result.numbers[flatIndex] = ${s}(value);
}`,r>=2){let o=["d0","d1","d2","d3"].slice(0,r),i=on(r);n?a+=`
fn setOutput(${o.map(u=>`${u} : i32`).join(", ")}, value : vec4<f32>) {
let flatIndex = getOutputFlatIndex(${i}(${o.join(", ")}));
setOutputFlat(flatIndex / 4, value);
}
fn setOutputI32(${o.map(u=>`${u} : i32`).join(", ")}, value : vec4<i32>) {
let flatIndex = getOutputFlatIndex(${i}(${o.join(", ")}));
setOutputFlatI32(flatIndex / 4, value);
}
`:a+=`
fn setOutput(${o.map(u=>`${u} : i32`).join(", ")}, value : f32) {
let flatIndex = getOutputFlatIndex(${i}(${o.join(", ")}));
setOutputFlat(flatIndex, value);
}
fn setOutputI32(${o.map(u=>`${u} : i32`).join(", ")}, value : i32) {
let flatIndex = getOutputFlatIndex(${i}(${o.join(", ")}));
setOutputFlatI32(flatIndex, value);
}
`}return a}function Uae(e,t,n,r){let s=Gae(e,n);return e.shape.length<=t.length&&(s+=Hae(e,t,n,r)),s}function Gae(e,t){let n=e.name,r=e.shape.length,s=on(r),a="get"+n.charAt(0).toUpperCase()+n.slice(1),o=["d0","d1","d2","d3"].slice(0,r),i=o.map(c=>`${c} : i32`).join(", ");if(r<1)return t?`
fn ${a}() -> vec4<f32> {
return vec4<f32>(${n}.numbers[0]);
}
`:`
fn ${a}() ->f32 {
return f32(${n}.numbers[0]);
}
`;let u=`uniforms.${n.charAt(0).toLowerCase()+n.slice(1)}Shape`,l=`${r}D`;return r===0&&(l="1D"),t?`
fn ${a}(${i}) -> vec4<f32> {
return vec4<f32>(${n}.numbers[getFlatIndex${l}(${s}(${o.join(",")}),
${u}) / 4]);
}
`:`
fn ${a}(${i}) -> f32 {
return f32(${n}.numbers[getFlatIndex${l}(${s}(${o.join(",")}),
${u})]);
}
`}function Hae(e,t,n,r){let s=e.name,a=s.charAt(0).toUpperCase()+s.slice(1),o="get"+a+"AtOutCoords",i=e.shape.length,u=t.length,l=on(u);if(w.arraysEqual(e.shape,t)&&r)return n?`
fn ${o}ByGlobalIndex(globalIndex : i32) -> vec4<f32> {
return vec4<f32>(${s}.numbers[globalIndex]);
}
fn ${o}ByCoords(coords : ${l}) -> vec4<f32> {
return vec4<f32>(${s}.numbers[${u>1?"getOutputFlatIndex(coords)":"coords"} / 4]);
}
`:`
fn ${o}ByGlobalIndex(globalIndex : i32) -> f32 {
return f32(${s}.numbers[globalIndex]);
}
fn ${o}ByCoords(coords : ${l}) -> f32 {
return f32(${s}.numbers[${u>1?"getOutputFlatIndex(coords)":"coords"}]);
}
`;let c=N.getBroadcastDims(e.shape,t),d=u-i,p="";if(i===0)return n?`
fn ${o}ByGlobalIndex(globalIndex : i32) -> vec4<f32> {
return get${a}();
}
fn ${o}ByCoords(coords : ${l}) -> vec4<f32> {
return get${a}();
}
`:`
fn ${o}ByGlobalIndex(globalIndex : i32) -> f32{
return get${a}();
}
fn ${o}ByCoords(coords : ${l}) -> f32{
return get${a}();
}
`;u<2&&c.length>=1?p="coords = 0;":p=c.map(g=>`coords[${g+d}] = 0;`).join(`
`);let h="";if(u<2&&i>0)h="coords";else if(u>1){let g=on(i),b=e.shape.map((y,v)=>`coords[${v+d}]`).join(", ");h=`${g}(${b})`}else h="coords";let f=`uniforms.${s.charAt(0).toLowerCase()+s.slice(1)}Shape`,m=`${i}D`;return n?`
fn ${o}ByGlobalIndex(globalIndex : i32) -> vec4<f32> {
var coords = getCoordsFromFlatIndex(globalIndex);
${p}
return ${s}.numbers[getFlatIndex${m}(${h}, ${f}) / 4];
}
fn ${o}ByCoords(coordsIn : ${l}) -> vec4<f32> {
var coords = coordsIn;
${p}
return ${s}.numbers[getFlatIndex${m}(${h}, ${f}) / 4];
}
`:`
fn ${o}ByGlobalIndex(globalIndex : i32) -> f32 {
var coords = getCoordsFromFlatIndex(globalIndex);
${p}
return f32(${s}.numbers[getFlatIndex${m}(${h}, ${f})]);
}
fn ${o}ByCoords(coordsIn : ${l}) -> f32 {
var coords = coordsIn;
${p}
return f32(${s}.numbers[getFlatIndex${m}(${h}, ${f})]);
}
`}function jae(e,t){let{x:n,y:r=[],z:s=[]}=t,a=e.length;if(n.length===a)return[`fn getOutputCoordsWithFlatDispatchLayout(globalId : vec3<u32>, localId : vec3<u32>, numWorkgroups: vec3<u32>) -> ${on(a)}{
let globalIndex = getGlobalIndex(globalId, localId, numWorkgroups);
return getCoordsFromFlatIndex(globalIndex);
}
`,a];let o="",i=[n,r,s],u=0;for(let p=0;p<i.length;p++){let h=i[p];if(h.length!==0)if(u+=h.length,h.length===1)o+=`let d${h[0]} = i32(globalId[${p}]);`;else{let f=Bae(h,"uniforms.outShape");o+=`var index${p} = i32(globalId[${p}]);`;for(let m=0;m<f.length;m++)o+=`let d${h[m]} = index${p} / ${f[m]};`,m===f.length-1?o+=`let d${h[m+1]} = index${p} - d${h[m]} * ${f[m]};`:o+=`index${p} = index${p} - d${h[m]} * ${f[m]};`}}let l=[];for(let p=0;p<u;p++)l.push(`d${p}`);let c=on(u),d=`fn getOutputCoordsWithNonFlatDispatchLayout(globalId : vec3<u32>) -> ${c} {
${o}
`;return l.length===0?d+=`return ${c}(0); }`:d+=`return ${c}(${l.join(",")}); }`,[d,u]}function gE(e){let t=e.length;if(t<=1)return"fn getCoordsFromFlatIndex(index : i32) -> i32 { return index; }";let n=w.computeStrides(e),r=on(t),s=[];for(let o=0;o<t;o++)s.push(`d${o}`);if(n.length===1)return` fn getCoordsFromFlatIndex(index : i32) -> vec2<i32> {
let d0 = index / uniforms.outShapeStrides; let d1 = index - d0 * uniforms.outShapeStrides;
return vec2<i32>(d0, d1);
}`;let a="var index2 = index;"+n.map((o,i)=>{let u=`let ${s[i]} = index2 / uniforms.outShapeStrides[${i}]`,l=i===n.length-1?`let ${s[i+1]} = index2 - ${s[i]} * uniforms.outShapeStrides[${i}]`:`index2 = index2 - ${s[i]} * uniforms.outShapeStrides[${i}]`;return`${u}; ${l};`}).join("");return`
fn getCoordsFromFlatIndex(index : i32) -> ${r} {
${a}
return ${r}(${s.join(",")});
}
`}var bE={};Ee(bE,{ArrayBufferToTypedArray:()=>yE,GPUBytesPerElement:()=>bk,computeDispatch:()=>_e,computeWorkGroupSizeForConv2d:()=>fk,computeWorkGroupSizeForMatMul:()=>mk,computeWorkPerThreadForConv2d:()=>gk,flatDispatchLayout:()=>ze,isWebGPUSupported:()=>yk,tilesFitEvenlyIntoShape:()=>Us});var tl=65535,uu=e=>{let t=1;for(let n=0;n<e.length;n++)t*=e[n];return t};function Us(e,t){if(e.length!==t.length)throw new Error(`Cannot compute whether rank ${e.length} tiles fit evenly into rank ${t.length} shape - ranks must match.`);return t.every((n,r)=>n%e[r]==0)}function _e(e,t,n=[1,1,1],r=[1,1,1]){let[s,a,o]=[Math.ceil(uu(e.x.map(u=>t[u]))/(n[0]*r[0])),e.y?Math.ceil(uu(e.y.map(u=>t[u]))/(n[1]*r[1])):1,e.z?Math.ceil(uu(e.z.map(u=>t[u]))/(n[2]*r[2])):1];if(s<=tl&&a<=tl&&o<=tl)return[s,a,o];w.assert(s>tl&&e.y===void 0&&e.z===void 0,()=>"Dispatch size exceeds WebGPU limits in Y or Z dimension.");let i=Math.ceil(Math.sqrt(s));return i>tl?(i=Math.ceil(Math.cbrt(s)),w.assert(i<=tl,()=>"Total dispatch size exceeds WebGPU maximum."),[i,i,i]):[i,i,1]}function fk(e,t){let n=uu(e.x.map(s=>t[s])),r=uu(e.y.map(s=>t[s]));return n<=4?[4,16,1]:r<=4?[16,4,1]:[16,16,1]}function mk(e,t,n){return e===1?[32,1,1]:n===1?[1,32,1]:[8,8,1]}function gk(e,t){let n=uu(e.x.map(s=>t[s])),r=uu(e.y.map(s=>t[s]));return n<=4?[1,2,1]:r<=4?[2,1,1]:[2,2,1]}function ze(e){return{x:e.map((t,n)=>n)}}function bk(e){if(e==="float32"||e==="int32"||e==="bool"||e==="string")return 4;if(e==="complex64")return 8;throw new Error(`Unknown dtype ${e}`)}function yE(e,t){if(t==="float32")return new Float32Array(e);if(t==="int32")return new Int32Array(e);if(t==="bool"||t==="string"){let n=new Int32Array(e),r=new ArrayBuffer(n.length),s=new Uint8Array(r);for(let a=0;a<n.length;a++)s[a]=n[a];return s}else throw new Error(`Unknown dtype ${t}`)}function yk(){return!!navigator.gpu}var Mt;(function(e){e[e.MUL=0]="MUL",e[e.ADD=1]="ADD",e[e.SUB=2]="SUB",e[e.DIV=3]="DIV",e[e.EQUAL=4]="EQUAL",e[e.GREATER=5]="GREATER",e[e.GREATER_EQUAL=6]="GREATER_EQUAL",e[e.LESS=7]="LESS",e[e.LESS_EQUAL=8]="LESS_EQUAL",e[e.LOGICAL_AND=9]="LOGICAL_AND",e[e.NOT_EQUAL=10]="NOT_EQUAL",e[e.SQUARED_DIFFERENCE=11]="SQUARED_DIFFERENCE",e[e.INT_DIV=12]="INT_DIV",e[e.POW=13]="POW",e[e.PRELU=14]="PRELU",e[e.MAX=15]="MAX",e[e.MIN=16]="MIN",e[e.COMPLEX_MULTIPLY_REAL=17]="COMPLEX_MULTIPLY_REAL",e[e.COMPLEX_MULTIPLY_IMAG=18]="COMPLEX_MULTIPLY_IMAG"})(Mt||(Mt={}));var qae="return a + b;",Kae="return areal * breal - aimag * bimag;",Xae="return areal * bimag + aimag * breal;",Yae="return a / b;",Qae="return a * b;",Zae="return (a - b) * (a - b);",Jae="return a - b;",eoe="return f32(a == b);",toe="return vec4<f32>(a == b);",noe="return f32(a > b);",roe="return vec4<f32>(a > b);",soe="return f32(a >= b);",aoe="return vec4<f32>(a >= b);",ooe="return f32(a < b);",ioe="return vec4<f32>(a < b);",uoe="return f32(a <= b);",coe="return vec4<f32>(a <= b);",loe="return f32(f32(a) >= 1.0 && f32(b) >= 1.0);",doe=`return (vec4<f32>(a >= vec4<f32>(1.0)) *
vec4<f32>(b >= vec4<f32>(1.0)));`,poe=`
if (isNanCustom(a)) { return a; }
if (isNanCustom(b)) { return b; }
`,vE=`
if (isNaN.r > 0.) {
resultTemp.r = uniforms.NAN;
}
if (isNaN.g > 0.) {
resultTemp.g = uniforms.NAN;
}
if (isNaN.b > 0.) {
resultTemp.b = uniforms.NAN;
}
if (isNaN.a > 0.) {
resultTemp.a = uniforms.NAN;
}
`,hoe=`
let s = sign(a) * sign(b);
let ia = i32(round(a));
let ib = i32(round(b));
return f32(idiv(ia, ib, s));
`,foe=`
let ia = vec4<i32>(round(a));
let ib = vec4<i32>(round(b));
let cond = ib != vec4<i32>(0);
var resultTemp = vec4<i32>(0);
let s = sign(a) * sign(b);
// Windows (D3D) wants guaranteed non-zero int division at compile-time.
if (cond[0]) {
resultTemp[0] = idiv(ia[0], ib[0], s[0]);
}
if (cond[1]) {
resultTemp[1] = idiv(ia[1], ib[1], s[1]);
}
if (cond[2]) {
resultTemp[2] = idiv(ia[2], ib[2], s[2]);
}
if (cond[3]) {
resultTemp[3] = idiv(ia[3], ib[3], s[3]);
}
return vec4<f32>(resultTemp);
`,moe="return f32(a != b);",goe="return vec4<f32>(a != b);",boe=`
if(a < 0.0 && floor(b) < b) {
return uniforms.NAN;
}
if (b == 0.0) {
return 1.0;
}
if (round(abs(b) % 2.0) != 1.0) {
return pow(abs(a), b);
}
return sign(a) * pow(abs(a), b);
`,yoe=`
let isModRound1Bool = vec4<i32>(round(abs(b) % vec4<f32>(2.0))) == vec4<i32>(1);
let isModRound1 = vec4<f32>(isModRound1Bool);
let multiplier = sign(a) * isModRound1 + (vec4<f32>(1.0) - isModRound1);
var resultTemp = multiplier * pow(abs(a), b);
// Ensure that a^0 = 1, including 0^0 = 1 as this correspond to TF and JS
let isExpZero = b == vec4<f32>(0.0);
if (isExpZero.r) {
resultTemp.r = 1.0;
}
if (isExpZero.g) {
resultTemp.g = 1.0;
}
if (isExpZero.b) {
resultTemp.b = 1.0;
}
if (isExpZero.a) {
resultTemp.a = 1.0;
}
let isNaN = vec4<f32>(a < vec4<f32>(0.0)) * vec4<f32>(floor(b) < b);
${vE}
return resultTemp;
`,voe="if (a < 0.0) { return b * a; } return a;",xoe=`
let aLessThanZero = vec4<f32>(a < vec4<f32>(0.0));
return (aLessThanZero * (b * a)) + ((vec4<f32>(1.0) - aLessThanZero) * a);
`;function xE(e,t){let n=t?vE:poe;return t?`
var resultTemp = vec4<f32>(${e}(a, b));
let isNaN = min(vec4<f32>(isNanCustomVec4F32(a)) + vec4<f32>(isNanCustomVec4F32(b)), vec4<f32>(1.0));
`+n+`
return resultTemp;
`:n+`
return ${e}(a, b);
`}function hp(e,t){switch(e){case 0:return Qae;case 1:return qae;case 2:return Jae;case 3:return Yae;case 4:return t?toe:eoe;case 5:return t?roe:noe;case 6:return t?aoe:soe;case 7:return t?ioe:ooe;case 8:return t?coe:uoe;case 9:return t?doe:loe;case 10:return t?goe:moe;case 11:return Zae;case 12:return t?foe:hoe;case 14:return t?xoe:voe;case 15:return xE("max",t);case 16:return xE("min",t);case 13:return t?yoe:boe;case 17:return Kae;case 18:return Xae;default:throw new Error(`BinaryType ${e} is not implemented!`)}}var bt;(function(e){e[e.ABS=0]="ABS",e[e.CEIL=1]="CEIL",e[e.COS=2]="COS",e[e.COSH=3]="COSH",e[e.ELU=4]="ELU",e[e.EXP=5]="EXP",e[e.EXPM1=6]="EXPM1",e[e.FLOOR=7]="FLOOR",e[e.LINEAR=8]="LINEAR",e[e.LOG=9]="LOG",e[e.LOGICAL_NOT=10]="LOGICAL_NOT",e[e.NEG=11]="NEG",e[e.PRELU=12]="PRELU",e[e.RELU=13]="RELU",e[e.RELU6=14]="RELU6",e[e.RSQRT=15]="RSQRT",e[e.SIN=16]="SIN",e[e.SINH=17]="SINH",e[e.SIGMOID=18]="SIGMOID",e[e.SQRT=19]="SQRT",e[e.SQUARE=20]="SQUARE",e[e.TANH=21]="TANH",e[e.TO_INT=22]="TO_INT"})(bt||(bt={}));var woe="return abs(a);",koe="return ceil(a);",Ioe="return cos(a);",Soe=`
let e2x = exp(-a);
return (e2x + 1.0 / e2x) / 2.0;
`,Coe="return exp(a) - 1.0;",Toe="if (a >= 0.0) { return a; } return (exp(a) - 1.0);",Noe=`
var resFloat = exp(a) - vec4<f32>(1.0);
if (a.r >= 0.0) {
resFloat.r = a.r;
}
if (a.g >= 0.0) {
resFloat.g = a.g;
}
if (a.b >= 0.0) {
resFloat.b = a.b;
}
if (a.a >= 0.0) {
resFloat.a = a.a;
}
return resFloat;
`,_oe="return exp(a);",Eoe="return floor(a);",Aoe="return a;",$oe=`if (a < 0.0) { return 1.0/0.0; }
return log(a);`,Foe="return f32(!(a >= 1.0));",Doe="return -a;",Roe="return (a < 0.0) ? b * a : a;",Poe="return max(a, 0.0);",Ooe="return clamp(a, 0.0, 6.0);",Moe="return clamp(a, vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(6.0, 6.0, 6.0, 6.0));",Loe=`
var resFloat = a * vec4<f32>(a >= vec4<f32>(0.0));
let isNaN = isNan(a);
if (isNaN.r) {
resFloat.r = a.r;
}
if (isNaN.g) {
resFloat.g = a.g;
}
if (isNaN.b) {
resFloat.b = a.b;
}
if (isNaN.a) {
resFloat.a = a.a;
}
return resFloat;
`,Boe="return 1.0/sqrt(a);",zoe="return 1.0 / (1.0 + exp(-1.0 * a));",Woe="return sin(a);",Voe=`
let e2x = exp(a);
return (e2x - 1.0 / e2x) / 2.0;
`,Uoe="return sqrt(a);",Goe="return a * a;",Hoe=`
let e2x = exp(-2.0 * abs(a));
return sign(a) * (1.0 - e2x) / (1.0 + e2x);
`,joe="return f32(i32((a)));";function nl(e,t){switch(e){case 0:return woe;case 2:return Ioe;case 3:return Soe;case 1:return koe;case 4:return t?Noe:Toe;case 5:return _oe;case 6:return Coe;case 7:return Eoe;case 8:return Aoe;case 9:return $oe;case 10:return Foe;case 11:return Doe;case 12:return Roe;case 13:return t?Loe:Poe;case 14:return t?Moe:Ooe;case 15:return Boe;case 18:return zoe;case 16:return Woe;case 17:return Voe;case 19:return Uoe;case 20:return Goe;case 21:return Hoe;case 22:return joe;default:throw new Error(`BinaryType ${e} is not implemented!`)}}function Gs(e,t=!1){if(e===null)return null;if(e==="linear")return nl(bt.LINEAR);if(e==="relu")return nl(bt.RELU,t);if(e==="elu")return nl(bt.ELU,t);if(e==="relu6")return nl(bt.RELU6,t);if(e==="prelu")return hp(Mt.PRELU,t);if(e==="sigmoid")return nl(bt.SIGMOID);throw new Error(`Activation ${e} has not been implemented for the WebGPU backend.`)}function wE(e,t){let n={RowPerThread:e[1],ColPerThread:e[0],TileAOuter:t[1]*e[1],TileBOuter:t[0]*e[0],TileInner:t[0]*e[0]};return`
var<workgroup> mm_Asub : array<array<vec4<f32>, ${n.TileInner/n.ColPerThread}>, ${n.TileAOuter}>;
var<workgroup> mm_Bsub : array<array<vec4<f32>, ${n.TileBOuter/n.ColPerThread}>, ${n.TileInner}>;
let RowPerThread = ${n.RowPerThread};
let ColPerThread = ${n.ColPerThread}; // only support ColPerThread = 4
let TileAOuter = ${n.TileAOuter};
let TileBOuter = ${n.TileBOuter};
let TileInner = ${n.TileInner};
${el()} {
let tileRow = i32(localId.y) * RowPerThread;
let tileCol = i32(localId.x);
let globalRow = i32(globalId.y) * RowPerThread;
let globalCol = i32(globalId.x);
let numTiles = (uniforms.dimInner - 1) / TileInner + 1;
var acc: array<vec4<f32>, ${n.RowPerThread}>;
var ACached : vec4<f32>;
var BCached : array<vec4<f32>, 4>;
// Loop over shared dimension.
var globalColA = tileCol;
let RowPerThreadB = TileInner / ${t[1]};
let tileRowB = i32(localId.y) * RowPerThreadB;
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
let inputRow = tileRow + innerRow;
let inputCol = tileCol;
mm_Asub[inputRow][inputCol] = mm_readA(globalRow + innerRow, globalColA, globalId);
}
globalColA = globalColA + TileInner / ColPerThread;
// Load one tile of B into local memory.
for (var innerRow = 0; innerRow < RowPerThreadB; innerRow = innerRow + 1) {
let inputRow = tileRowB + innerRow;
let inputCol = tileCol;
mm_Bsub[inputRow][inputCol] = mm_readB(t * TileInner + inputRow, globalCol, globalId);
}
workgroupBarrier();
// Compute acc values for a single thread.
for (var k = 0; k < TileInner / ColPerThread; k = k + 1) {
BCached[0] = mm_Bsub[k * ColPerThread][tileCol];
BCached[1] = mm_Bsub[k * ColPerThread + 1][tileCol];
BCached[2] = mm_Bsub[k * ColPerThread + 2][tileCol];
BCached[3] = mm_Bsub[k * ColPerThread + 3][tileCol];
for (var i = 0; i < RowPerThread; i = i + 1) {
ACached = mm_Asub[tileRow + i][k];
acc[i] = BCached[0] * ACached.x + acc[i];
acc[i] = BCached[1] * ACached.y + acc[i];
acc[i] = BCached[2] * ACached.z + acc[i];
acc[i] = BCached[3] * ACached.w + acc[i];
}
}
workgroupBarrier();
}
for (var innerRow = 0; innerRow < RowPerThread; innerRow = innerRow + 1) {
mm_write(globalRow + innerRow,
globalCol,
acc[innerRow], globalId);
}
}`}function qoe(e){return`
var<workgroup> mm_Asub : array<vec4<f32>, ${e[0]}>;
let tileSize = ${e[0]*4};
${el()} {
let tileCol = i32(localId.x);
let globalCol = i32(globalId.x);
let globalRow = i32(globalId.y);
let numTiles = (uniforms.dimInner - 1) / tileSize + 1;
// Without this initialization strange values show up in acc.
var acc = vec4<f32>(0.0);
// Loop over shared dimension.
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
let colA = t * tileSize / 4 + tileCol;
mm_Asub[tileCol] = mm_readA(globalRow, colA, globalId);
workgroupBarrier();
// Compute acc values for a single thread.
for (var k = 0; k < tileSize / 4; k = k + 1) {
let rowB = t * tileSize + k * 4;
let BCached0 = mm_readB(rowB, globalCol, globalId);
let BCached1 = mm_readB(rowB + 1, globalCol, globalId);
let BCached2 = mm_readB(rowB + 2, globalCol, globalId);
let BCached3 = mm_readB(rowB + 3, globalCol, globalId);
let ACached = mm_Asub[k];
acc = acc + BCached0 * ACached.x;
acc = acc + BCached1 * ACached.y;
acc = acc + BCached2 * ACached.z;
acc = acc + BCached3 * ACached.w;
}
workgroupBarrier();
}
if (globalRow < uniforms.dimAOuter && globalCol < uniforms.dimBOuter) {
mm_write(globalRow, globalCol, acc, globalId);
}
}
`}var Koe=class{constructor(e,t,n,r=null,s=null,a=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32; dimBOuter : i32; dimInner : i32;",this.workGroupSize=[16,16,1],this.isVec4=!0,this.vecSize=4,this.outputShape=t,this.workGroupSize=mk(t[1],e[2],t[2]),this.dispatchLayout={x:[2],y:[1],z:[0]},t[1]===1&&(n=1),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.vecSize,n,1]);let o=r!=null,i=a!=null;o&&this.variableNames.push("bias"),i&&this.variableNames.push("preluActivationWeights"),this.workPerThread=n,this.aShape=e,this.addBias=o,this.activation=s,this.hasPreluActivationWeights=i,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`matMulPackedVec4_${n}_${this.activation}_${this.fitA}_${this.fitB}_${this.outputShape[1]>1}`}getShapeFit(){let e=this.aShape[2],t=this.outputShape[2],n=[this.outputShape[0],e,t],r=this.workGroupSize[1]*this.workPerThread,s=this.workGroupSize[0]*this.vecSize,a=s,o=[r,a],i=[a,s];return[Us(o,this.aShape.slice(1)),Us(i,n.slice(1))]}getUserCode(){let e=this.fitA?"return A.numbers[batch * batchASize + row * uniforms.dimInner / 4 + col]":`if (coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A.numbers[batch * batchASize + row * uniforms.dimInner / 4 + col];
}
return vec4<f32>(0.0)`,t=this.fitB?"return B.numbers[batch * batchBSize + row * uniforms.dimBOuter / 4 + col]":`if(coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B.numbers[batch * batchBSize + row * uniforms.dimBOuter / 4 + col];
}
return vec4<f32>(0.0)`,n="",r="";if(this.activation){let o=Gs(this.activation,this.isVec4);this.hasPreluActivationWeights?n=`fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${o}
}`:n=`
fn activation(a : vec4<f32>, outCoord : vec3<i32>) -> vec4<f32> {
${o}
}`,r="value = activation(value, outCoord);"}let s=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2] / ${this.vecSize};
let batch = i32(globalId.z);
${e};
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2] / ${this.vecSize};
let batch = i32(globalId.z);
${t};
}
fn mm_write(row : i32, col : i32, valueIn : vec4<f32>, globalId : vec3<u32>) {
if (row < uniforms.aShape[1] && col * 4 < uniforms.bShape[2])
{
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col * 4);
${s}
${r}
setOutput(outCoord[0], outCoord[1], outCoord[2], value);
}
}
${this.outputShape[1]>1?wE([this.vecSize,this.workPerThread,1],this.workGroupSize):qoe(this.workGroupSize)}
`}};function vk(e,t){let n=t[1]*e[1],r=t[0]*e[0],s=n>r?n:r;return`
var<workgroup> mm_Asub : array<array<f32, ${s}>, ${n}>;
var<workgroup> mm_Bsub : array<array<f32, ${r}>, ${s}>;
${el()} {
let tileRow = i32(localId.y) * ${e[1]};
let tileCol = i32(localId.x) * ${e[0]};
let globalRow = i32(globalId.y) * ${e[1]};
let globalCol = i32(globalId.x) * ${e[0]};
let numTiles = (uniforms.dimInner - 1) / ${s} + 1;
var acc : array<array<f32, ${e[0]}>, ${e[1]}>;
var ACached : f32;
var BCached : array<f32, ${e[0]}>;
// Without this initialization strange values show up in acc.
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
acc[innerRow][innerCol] = 0.0;
}
}
let ColPerThreadA = ${s} / ${t[0]};
let tileColA = i32(localId.x) * ColPerThreadA;
let RowPerThreadB = ${s} / ${t[1]};
let tileRowB = i32(localId.y) * RowPerThreadB;
// Loop over shared dimension.
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ColPerThreadA; innerCol = innerCol + 1) {
let inputRow = tileRow + innerRow;
let inputCol = tileColA + innerCol;
mm_Asub[inputRow][inputCol] = mm_readA(
globalRow + innerRow,
t * ${s} + inputCol, globalId);
}
}
// Load one tile of B into local memory.
for (var innerRow = 0; innerRow < RowPerThreadB; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
let inputRow = tileRowB + innerRow;
let inputCol = tileCol + innerCol;
mm_Bsub[inputRow][inputCol] = mm_readB(
t * ${s} + inputRow,
globalCol + innerCol, globalId);
}
}
workgroupBarrier();
// Compute acc values for a single thread.
for (var k = 0; k < ${s}; k = k + 1) {
for (var inner = 0; inner < ${e[0]}; inner = inner + 1) {
BCached[inner] = mm_Bsub[k][tileCol + inner];
}
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
ACached = mm_Asub[tileRow + innerRow][k];
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol];
}
}
}
workgroupBarrier();
}
for (var innerRow = 0; innerRow < ${e[1]}; innerRow = innerRow + 1) {
for (var innerCol = 0; innerCol < ${e[0]}; innerCol = innerCol + 1) {
if ((globalCol + innerCol) < uniforms.dimBOuter &&
(globalRow + innerRow) < uniforms.dimAOuter) {
mm_write(globalRow + innerRow,
globalCol + innerCol,
acc[innerRow][innerCol], globalId);
}
}
}
}
`}function Xoe(e){return`
let TileSize = ${e[0]*4};
var<workgroup> mm_Asub : array<vec4<f32>, ${e[0]}>;
${el()} {
let tileCol = i32(localId.x);
let globalCol = i32(globalId.x);
let globalRow = i32(globalId.y);
let numTiles = (uniforms.dimInner - 1) / TileSize + 1;
// Without this initialization strange values show up in acc.
var acc = 0.0;
// Loop over shared dimension.
for (var t = 0; t < numTiles; t = t + 1) {
// Load one tile of A into local memory.
let colA = t * TileSize + tileCol * 4;
mm_Asub[tileCol] = vec4<f32>(mm_readA(globalRow, colA, globalId),
mm_readA(globalRow, colA + 1, globalId),
mm_readA(globalRow, colA + 2, globalId),
mm_readA(globalRow, colA + 3, globalId));
workgroupBarrier();
// Compute acc values for a single thread.
for (var k = 0; k < TileSize / 4; k = k + 1) {
let rowB = t * TileSize + k * 4;
let BCached = vec4<f32>(mm_readB(rowB, globalCol, globalId),
mm_readB(rowB + 1, globalCol, globalId),
mm_readB(rowB + 2, globalCol, globalId),
mm_readB(rowB + 3, globalCol, globalId));
let ACached = mm_Asub[k];
acc = acc + dot(ACached, BCached);
}
workgroupBarrier();
}
if (globalRow < uniforms.dimAOuter && globalCol < uniforms.dimBOuter) {
mm_write(globalRow, globalCol, acc, globalId);
}
}
`}var kE=class{constructor(e,t,n,r=!1,s=!1,a=null,o=null,i=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32; dimBOuter : i32; dimInner : i32;",this.workGroupSize=[16,16,1],this.outputShape=t,this.dispatchLayout={x:[2],y:[1],z:[0]};let u=r?e[1]:e[2];this.workGroupSize=mk(t[1],u,t[2]),(t[1]===1||t[2]===1)&&(n=1),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[n,n,1]),w.arraysEqual(this.dispatch,[1,1,1])&&(n=1,this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[n,n,1]));let l=a!=null,c=i!=null;l&&this.variableNames.push("bias"),c&&this.variableNames.push("preluActivationWeights"),this.workPerThread=n,this.aShape=e,this.transposeA=r,this.transposeB=s,this.addBias=l,this.activation=o,this.hasPreluActivationWeights=c;let d=this.outputShape[2],p=this.transposeB?[this.outputShape[0],d,u]:[this.outputShape[0],u,d];[this.fitA,this.fitB]=this.getShapeFit(p),this.shaderKey=`matMulPacked_${this.workPerThread}_${r}_${s}_${this.activation}_${this.fitA}_${this.fitB}_${this.outputShape[1]>1}`}getShapeFit(e){let t=this.workGroupSize[1]*this.workPerThread,n=this.workGroupSize[0]*this.workPerThread,r=t>n?t:n;this.outputShape[1]===1&&(r*=4),w.assert(r%this.workGroupSize[0]==0&&r%this.workGroupSize[1]==0,()=>"tileInner must be multiple of workgroupsize.x and workgroupsize.y");let s=[t,r],a=[r,n];return[Us(s,this.aShape.slice(1)),Us(a,e.slice(1))]}getUserCode(){let e;this.transposeA===!1?e=this.fitA?"return A.numbers[batch * batchASize + row * uniforms.dimInner + col];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A.numbers[batch * batchASize + row * uniforms.dimInner + col];
}
return 0.0;`:e=this.fitA?"return A.numbers[batch * batchASize + col * uniforms.dimAOuter + row];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A.numbers[batch* batchASize + col * uniforms.dimAOuter + row];
}
return 0.0;`;let t;this.transposeB===!1?t=this.fitB?"return B.numbers[batch * batchBSize + row * uniforms.dimBOuter + col];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B.numbers[batch * batchBSize + row * uniforms.dimBOuter + col];
}
return 0.0;`:t=this.fitB?"return B.numbers[batch * batchBSize + col * uniforms.dimInner + row];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B.numbers[batch * batchBSize + col * uniforms.dimInner + row];
}
return 0.0;`;let n="",r="";if(this.activation){let o=Gs(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${o}
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${o}
}
`,r="value = activation(value, outCoord);"}let s=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
let batch = i32(globalId.z);
${e}
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batch = i32(globalId.z);
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
${t}
}
fn mm_write(row : i32, col : i32, valueIn : f32, globalId : vec3<u32>) {
var value = valueIn;
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col);
${s}
${r}
setOutput(batch, row, col, value);
}
${this.outputShape[1]>1?vk([this.workPerThread,this.workPerThread,1],this.workGroupSize):Xoe(this.workGroupSize)}
`}};function Yoe(){return`
var<workgroup> sumValues : array<f32, workGroupSizeX>;
${el()} {
let coords = getOutputCoordsWithNonFlatDispatchLayout(globalId);
let batch = coords[0];
let row = coords[1];
let col = coords[2];
var sum = 0.0;
let Length = uniforms.dimInner;
for (var k = i32(localId.x); k < Length; k = k + i32(workGroupSizeX)) {
let dataA = mm_readA(batch, row, k);
let dataB = mm_readB(batch, k, col);
sum = sum + dataA * dataB;
}
sumValues[localId.x] = sum;
workgroupBarrier();
for(var currentSize = workGroupSizeX / 2u; currentSize > 1u;
currentSize = currentSize / 2u) {
if (localId.x < currentSize)
{
sumValues[localId.x] = sumValues[localId.x] + sumValues[localId.x + currentSize];
}
workgroupBarrier();
}
if (localId.x == 0u) {
sum = sumValues[0] + sumValues[1];
mm_write(batch, row, col, sum);
}
}
`}var Qoe=class{constructor(e,t=!1,n=!1,r=null,s=null,a=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32; dimBOuter : i32; dimInner : i32;",this.workGroupSize=[256,1,1],this.outputShape=e,this.dispatchLayout={x:[],y:[1,2],z:[0]},this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize);let o=r!=null,i=a!=null;o&&this.variableNames.push("bias"),i&&this.variableNames.push("preluActivationWeights"),this.transposeA=t,this.transposeB=n,this.addBias=o,this.activation=s,this.hasPreluActivationWeights=i,this.shaderKey=`matMulReduce_${this.activation}_${t}_${n}`}getUserCode(){let e;this.transposeA===!1?e="return A.numbers[batch * batchASize + row * uniforms.dimInner + col];":e="return A.numbers[batch * batchASize + col * uniforms.dimAOuter + row];";let t;this.transposeB===!1?t="return B.numbers[batch * batchBSize + row * uniforms.dimBOuter + col];":t="return B.numbers[batch * batchBSize + col * uniforms.dimInner + row];";let n="",r="";if(this.activation){let o=Gs(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${o}
}`:n=`
fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${o}
}
`,r="value = activation(value, outCoord);"}let s=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${n}
fn mm_readA(batch: i32, row : i32, col : i32) -> f32 {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
${e}
}
fn mm_readB(batch: i32, row : i32, col : i32) -> f32 {
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
${t}
}
fn mm_write(batch: i32, row : i32, col : i32, valueIn : f32) {
var value = valueIn;
let outCoord = vec3<i32>(batch, row, col);
${s}
${r}
setOutput(batch, row, col, value);
}
${Yoe()}
`}};function Zoe(e){let t=e[1]/2,n=e[0],r=t>n?t:n;return`
var<workgroup> mm_Asub1 : array<array<f32, ${r}>, ${t}>;
var<workgroup> mm_Bsub1 : array<array<f32, ${n}>, ${r}>;
var<workgroup> mm_Asub2 : array<array<f32, ${r}>, ${t}>;
var<workgroup> mm_Bsub2 : array<array<f32, ${n}>, ${r}>;
// If the output size is small for matrix multiplication, avoid to use vec4
// and handle some elements per thread to optimally utilize the ALU.
// Introduces two shared memory buffers, some logical threads could handle
// arithmetic operations and others handle IO operations between barrier api,
// makes ALUs and load/store units work simultaneously, could improves
// the performance.
${el()} {
let tileRow = i32(localId.y);
let tileCol = i32(localId.x);
let globalRow = i32(globalId.y);
let globalCol = i32(globalId.x);
// uniforms.dimInner should be greater than 0.
let numTiles = (uniforms.dimInner - 1) / ${r} + 1;
var acc = 0.0;
var globalColA = tileCol;
var globalRowB = tileRow;
for (var t = 0; t < numTiles; t = t + 1) {
if (t == 0) {
if (tileRow < ${t}) {
// Load one tile of A and B into local memory.
// globalRow is always greater than or equal tileRow.
mm_Asub1[tileRow][tileCol] =
mm_readA((globalRow - tileRow) / 2 + tileRow, globalColA, globalId);
globalColA = globalColA + ${r};
mm_Bsub1[tileRow][tileCol] = mm_readB(globalRowB, globalCol, globalId);
globalRowB = globalRowB + ${r};
}
} else {
if (tileRow < ${t}) {
// Load one tile of A and B into local memory.
// globalRow is always greater than or equal tileRow.
mm_Asub1[tileRow][tileCol] =
mm_readA((globalRow - tileRow) / 2 + tileRow, globalColA, globalId);
globalColA = globalColA + ${r};
mm_Bsub1[tileRow][tileCol] = mm_readB(globalRowB, globalCol, globalId);
globalRowB = globalRowB + ${r};
} else {
// Compute acc values for a single thread.
for (var k = 0; k < ${r}; k = k + 1) {
let subRow = tileRow - ${t};
if (subRow < 0) {
continue;
}
acc = acc + mm_Asub2[subRow][k] * mm_Bsub2[k][tileCol];
}
}
}
workgroupBarrier();
if (t != 0) {
t = t + 1;
}
if (t < numTiles) {
if (tileRow < ${t}) {
// Load one tile of A and B into local memory.
// globalRow is always greater than or equal tileRow.
mm_Asub2[tileRow][tileCol] =
mm_readA((globalRow - tileRow) / 2 + tileRow, globalColA, globalId);
globalColA = globalColA + ${r};
mm_Bsub2[tileRow][tileCol] = mm_readB(globalRowB, globalCol, globalId);
globalRowB = globalRowB + ${r};
} else {
// Compute acc values for a single thread.
for (var k = 0; k < ${r}; k = k + 1) {
let subRow = tileRow - ${t};
if (subRow < 0) {
continue;
}
acc = acc + mm_Asub1[subRow][k] * mm_Bsub1[k][tileCol];
}
}
}
workgroupBarrier();
}
let writeCol = (globalRow - tileRow) / 2 + tileRow - ${t};
if (tileRow >= ${t} && writeCol >= 0) {
mm_write(writeCol, globalCol, acc, globalId);
}
}
`}var Joe=class{constructor(e,t,n,r=null,s=null,a=null){this.variableNames=["A","B"],this.uniforms="dimAOuter : i32; dimBOuter : i32; dimInner : i32;",this.workGroupSize=[8,16,1],w.assert(e[1]<=16||t[2]<=16,()=>"This program can be only used when A width or B Height are small"),this.outputShape=n,this.dispatchLayout={x:[2],y:[1],z:[0]},this.dispatch=[Math.ceil(n[2]/this.workGroupSize[0]),Math.ceil(n[1]*2/this.workGroupSize[1]),n[0]];let o=r!=null;o&&this.variableNames.push("bias");let i=a!=null;i&&this.variableNames.push("preluActivationWeights"),this.addBias=o,this.activation=s,this.hasPreluActivationWeights=i,this.shaderKey=`matMulSmallOutputSize_${this.activation}`}getUserCode(){let e=`if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimInner))) {
return A.numbers[batch * batchASize + row * uniforms.dimInner + col];
}
return 0.0;`,t=`if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return B.numbers[batch * batchBSize + row * uniforms.dimBOuter + col];
}
return 0.0;`,n="",r="";if(this.activation){let o=Gs(this.activation,!1);this.hasPreluActivationWeights?n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${o}
}`:n=`fn activation(a : f32, outCoord : vec3<i32>) -> f32 {
${o}
}`,r="value = activation(value, outCoord);"}let s=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${n}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batchASize = uniforms.aShape[1] * uniforms.aShape[2];
let batch = i32(globalId.z);
${e}
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let batch = i32(globalId.z);
let batchBSize = uniforms.bShape[1] * uniforms.bShape[2];
${t}
}
fn mm_write(row : i32, col : i32, valueIn : f32, globalId : vec3<u32>) {
if (coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimAOuter, uniforms.dimBOuter))) {
let batch = i32(globalId.z);
let outCoord = vec3<i32>(batch, row, col);
var value = valueIn;
${s}
${r}
setOutput(batch, row, col, value);
}
}
${Zoe(this.workGroupSize)}
`}};function We(e){let{inputs:t,attrs:n}=e,{x:r}=t,{shape:s}=n,a=w.sizeFromShape(r.shape),o=w.inferFromImplicitShape(s,a),i=w.sizeFromShape(o);return w.assert(a===i,()=>`The new shape (${o}) has ${i} elements and the old shape (${r.shape}) has ${a} elements. The new shape and old shape must have the same number of elements.`),e.backend.incRef(r.dataId),{dataId:r.dataId,shape:o,dtype:r.dtype}}var eie={kernelName:hi,backendName:"webgpu",kernelFunc:We};function xk({a:e,b:t,transposeA:n,transposeB:r,backend:s,bias:a=null,preluActivationWeights:o=null,leakyreluAlpha:i=0,activation:u=null}){let l=e.shape.length,c=t.shape.length,d=n?e.shape[l-2]:e.shape[l-1],p=r?t.shape[c-1]:t.shape[c-2],h=n?e.shape[l-1]:e.shape[l-2],f=r?t.shape[c-2]:t.shape[c-1],m=e.shape.slice(0,-2),g=t.shape.slice(0,-2),b=w.sizeFromShape(m),y=w.sizeFromShape(g),x=Ri.assertAndGetBroadcastShape(e.shape.slice(0,-2),t.shape.slice(0,-2)).concat([h,f]);w.assert(d===p,()=>`Error in matMul: inner shapes (${d}) and (${p}) of Tensors with shapes ${e.shape} and ${t.shape} and transposeA=${n} and transposeB=${r} must match.`);let k=n?[b,d,h]:[b,h,d],T=r?[y,f,p]:[y,p,f],C=We({inputs:{x:e},backend:s,attrs:{shape:k}}),E=We({inputs:{x:t},backend:s,attrs:{shape:T}}),F=[C,E],O=Math.max(b,y),D=d%4==0&&f%4==0&&!n&&!r&&f>=32,R;h*f<=32?R=new Qoe([O,h,f],n,r,a,u,o):!n&&!r&&(h<=16&&(f<=512||p>=2*f)||f<=16&&(h<=512||d>=2*h))?R=new Joe(k,T,[O,h,f],a,u,o):D?R=new Koe(k,[O,h,f],X().get("WEBGPU_MATMUL_WORK_PER_THREAD"),a,u,o):R=new kE(k,[O,h,f],X().get("WEBGPU_MATMUL_WORK_PER_THREAD"),n,r,a,u,o);let _=[C,E];a&&_.push(a),o&&_.push(o);let L=[{type:"int32",data:[h]},{type:"int32",data:[f]},{type:"int32",data:[d]}],U=s.runWebGPUProgram(R,_,e.dtype,L),j=We({inputs:{x:U},backend:s,attrs:{shape:x}});F.push(U);for(let K of F)s.disposeData(K.dataId);return j}function tie(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a,bias:o,preluActivationWeights:i}=t,{transposeA:u,transposeB:l,activation:c,leakyreluAlpha:d}=r;return xk({a:s,b:a,transposeA:u,transposeB:l,backend:n,bias:o,preluActivationWeights:i,leakyreluAlpha:d,activation:c})}var nie={kernelName:to,backendName:"webgpu",kernelFunc:tie},IE=class{constructor(e,t,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.workGroupSize=[128,1,1],this.size=!0,this.outputShape=N.assertAndGetBroadcastShape(t,n),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`binaryOpComplex_${e}`,this.op=e}getUserCode(){return`
fn binaryOpComplex(
areal : f32, aimag : f32, breal : f32, bimag : f32) -> f32 {
${hp(this.op,!1)}
}
${Ue()}
if(index < uniforms.size) {
let areal = getARealAtOutCoordsByGlobalIndex(index);
let aimag = getAImagAtOutCoordsByGlobalIndex(index);
let breal = getBRealAtOutCoordsByGlobalIndex(index);
let bimag = getBImagAtOutCoordsByGlobalIndex(index);
setOutputFlat(index, binaryOpComplex(areal, aimag, breal, bimag));
}
}
`}},rie=class{constructor(e,t,n,r){this.variableNames=["A","B"],this.size=!0;let s=256;this.workGroupSize=[s,1,1],this.outputShape=N.assertAndGetBroadcastShape(t,n),this.dispatchLayout=ze(this.outputShape),this.lastDimensionSize=r?n[0]:t[0],this.lastDimensionSize<256?this.workPerThread=1:this.lastDimensionSize<512?this.workPerThread=2:this.workPerThread=4,this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.useSharedMemoryWithB=r,this.op=e,this.shaderKey=`binaryShared_${e}_${this.lastDimensionSize}_${this.useSharedMemoryWithB}`}getUserCode(){let e=this.lastDimensionSize>1?`coords[${this.outputShape.length-1}]`:"0",t=this.useSharedMemoryWithB?`let a = getAAtOutCoordsByCoords(coords);
let b = sharedBuf[${e}];`:`let a = sharedBuf[${e}];
let b = getBAtOutCoordsByCoords(coords);`;return`
fn binaryOperation(a : f32, b : f32) -> f32 {
${hp(this.op,!1)}
}
var<workgroup> sharedBuf : array<f32, ${this.lastDimensionSize}>;
${Ue()}
// Fill in the shared memory buffer. Here we need a loop to make sure
// that all data in A|B are uploaded when |sharedMemorySize| is larger
// than work group size.
for(var localIndex = i32(localId.x); localIndex < ${this.lastDimensionSize}; localIndex = localIndex + ${this.workGroupSize[0]}) {
sharedBuf[localIndex] = f32(${this.useSharedMemoryWithB?"B":"A"}.numbers[localIndex]);
}
workgroupBarrier();
for(var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if(flatIndex < uniforms.size) {
let coords = getCoordsFromFlatIndex(flatIndex);
${t}
setOutputFlat(flatIndex, binaryOperation(a, b));
}
}
}
`}},sie=class{constructor(e,t,n){this.variableNames=["A","B"],this.workPerThread=4,this.isVec4=!0,this.size=!0;let r=128;this.workGroupSize=[r,1,1],this.outputShape=N.assertAndGetBroadcastShape(t,n),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.op=e,this.shaderKey=`binaryVec4_${e}`}getUserCode(){return`
fn binaryOperation(a : vec4<f32>, b : vec4<f32>) -> vec4<f32> {
${hp(this.op,this.isVec4)}
}
${Ue()}
if (index < uniforms.size) {
let a = getAAtOutCoordsByGlobalIndex(index);
let b = getBAtOutCoordsByGlobalIndex(index);
setOutputFlat(index, binaryOperation(a, b));
}
}
`}},SE=class{constructor(e,t,n){this.variableNames=["A","B"],this.size=!0;let r=128;this.workGroupSize=[r,1,1],this.outputShape=N.assertAndGetBroadcastShape(t,n),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`binary_${e}`,this.op=e}getUserCode(){return`
fn binaryOperation(a : f32, b : f32) -> f32 {
${hp(this.op,!1)}
}
${Ue()}
if (index < uniforms.size) {
let a = getAAtOutCoordsByGlobalIndex(index);
let b = getBAtOutCoordsByGlobalIndex(index);
setOutputFlat(index, binaryOperation(a, b));
}
}
`}};function CE(e,t,n){if(w.arraysEqual(t,n)&&w.sizeFromShape(t)%4==0)return new sie(e,t,n);let s=t.length===1&&n.length>1&&t[0]<1024,a=n.length===1&&t.length>1&&n[0]<1024;return s||a?new rie(e,t,n,a):new SE(e,t,n)}function Lr(e){let{inputs:t}=e,{x:n}=t;return e.backend.incRef(n.dataId),{dataId:n.dataId,shape:n.shape,dtype:n.dtype}}var aie={kernelName:_a,backendName:"webgpu",kernelFunc:Lr};function rl(e){let{inputs:t,backend:n}=e,{real:r,imag:s}=t,a=n.makeTensorInfo(r.shape,"complex64"),o=n.tensorMap.get(a.dataId),i=Lr({inputs:{x:r},backend:n}),u=Lr({inputs:{x:s},backend:n});return o.complexTensorInfos={real:i,imag:u},a}var oie={kernelName:Kl,backendName:"webgpu",kernelFunc:rl},Wm=class{constructor(e,t){this.variableNames=["A"],this.size=!0;let n=128;this.workGroupSize=[n,1,1],this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.op=t,this.shaderKey=`unary_${t}`}getUserCode(){return`
fn unaryOperation(a : f32) -> f32 {
${nl(this.op,!1)}
}
${Ue()}
if (index < uniforms.size) {
let a = getAAtOutCoordsByGlobalIndex(index);
setOutputFlat(index, unaryOperation(a));
}
}
`}};function fn({opType:e,cpuKernelImpl:t,dtype:n}){return({inputs:r,backend:s})=>{let{x:a}=r,o=s,i=n||a.dtype;if(o.shouldExecuteOnCPU([a])&&t!=null){let l=o.tensorMap.get(a.dataId),c=t(l.values,i);return o.makeTensorInfo(a.shape,i,c)}let u=new Wm(a.shape,e);return o.runWebGPUProgram(u,[a],i)}}function Rn({opSnippet:e,cpuKernelImpl:t,supportsComplex:n=!1,dtype:r}){return({inputs:s,backend:a})=>{let{a:o,b:i}=s,u=a;if(n&&o.dtype==="complex64"){let d=u.tensorMap.get(o.dataId),p=u.tensorMap.get(i.dataId),h,f;if(e!==Mt.MUL)[h,f]=[[d.complexTensorInfos.real,p.complexTensorInfos.real],[d.complexTensorInfos.imag,p.complexTensorInfos.imag]].map(g=>{let[b,y]=g,v={dataId:b.dataId,dtype:b.dtype,shape:o.shape},x={dataId:y.dataId,dtype:y.dtype,shape:i.shape},k=CE(e,o.shape,i.shape);return u.runWebGPUProgram(k,[v,x],In(b.dtype,y.dtype))});else{let g=new IE(Mt.COMPLEX_MULTIPLY_REAL,o.shape,i.shape),b=new IE(Mt.COMPLEX_MULTIPLY_IMAG,o.shape,i.shape),y=[{dataId:d.complexTensorInfos.real.dataId,dtype:d.complexTensorInfos.real.dtype,shape:o.shape},{dataId:d.complexTensorInfos.imag.dataId,dtype:d.complexTensorInfos.imag.dtype,shape:o.shape},{dataId:p.complexTensorInfos.real.dataId,dtype:p.complexTensorInfos.real.dtype,shape:i.shape},{dataId:p.complexTensorInfos.imag.dataId,dtype:p.complexTensorInfos.imag.dtype,shape:i.shape}];h=u.runWebGPUProgram(g,y,"float32"),f=u.runWebGPUProgram(b,y,"float32")}let m=rl({inputs:{real:h,imag:f},backend:u});return u.disposeData(h.dataId),u.disposeData(f.dataId),m}let l=r||In(o.dtype,i.dtype);if((o.dtype==="string"||i.dtype==="string"||u.shouldExecuteOnCPU([o,i]))&&t!=null){let d=u.tensorMap.get(o.dataId).values,p=u.tensorMap.get(i.dataId).values,h=o.dtype==="string"?N.fromUint8ToStringArray(d):d,f=o.dtype==="string"?N.fromUint8ToStringArray(p):p,[m,g]=t(o.shape,i.shape,h,f,l);return u.makeTensorInfo(g,l,m)}let c=CE(e,o.shape,i.shape);return u.runWebGPUProgram(c,[o,i],l)}}var{addImpl:iie,ceilImpl:uie,concatImpl:cie,equalImpl:lie,expImpl:die,expm1Impl:pie,floorImpl:hie,gatherNdImpl:fie,gatherV2Impl:mie,greaterEqualImpl:gie,greaterImpl:bie,lessEqualImpl:yie,lessImpl:vie,logImpl:xie,maxImpl:wie,maximumImpl:kie,minimumImpl:Iie,multiplyImpl:Sie,negImpl:Cie,notEqualImpl:Tie,prodImpl:Nie,rangeImpl:_ie,rsqrtImpl:Eie,simpleAbsImpl:Aie,sliceImpl:$ie,stridedSliceImpl:Fie,stringNGramsImpl:Die,subImpl:Rie,tileImpl:Pie,topKImpl:Oie,transposeImpl:Mie,uniqueImpl:ibe}=fm,Lie=fn({opType:bt.ABS,cpuKernelImpl:Aie}),Bie={kernelName:Vo,backendName:"webgpu",kernelFunc:Lie},zie=Rn({opSnippet:Mt.ADD,cpuKernelImpl:iie,supportsComplex:!0}),Wie={kernelName:_s,backendName:"webgpu",kernelFunc:zie},Vie=class{constructor(e){this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e[0],this.variableNames=e.map((t,n)=>`T${n}`),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.shaderKey="addN"}getUserCode(){let e=[];this.variableNames.forEach(r=>{e.push(`let v${r} = get${r}AtOutCoordsByCoords(coords);`)});let t=this.variableNames.map(r=>`v${r}`).join(" + ");return`
${Ue()}
for (var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if (flatIndex < uniforms.size) {
let coords = getCoordsFromFlatIndex(flatIndex);
${e.join(`
`)}
setOutputFlat(flatIndex, ${t});
}
}
}
`}};function Uie(e){let{inputs:t,backend:n}=e,r=t;if(r.length===1)return Lr({inputs:{x:r[0]},backend:n});let s=r.map(i=>i.dtype).reduce((i,u)=>In(i,u)),a=r.map(i=>i.shape),o=new Vie(a);return n.runWebGPUProgram(o,r,s)}var Gie={kernelName:la,backendName:"webgpu",kernelFunc:Uie},TE=class{constructor(e,t,n){this.workGroupSize=[64,1,1],this.variableNames=["x"],this.uniforms="axis : i32; infinityValue : f32;",this.size=!0;let r=[t];N.assertAxesAreInnerMostDims("arg"+n.charAt(0).toUpperCase()+n.slice(1),r,e.length),this.op=n==="min"?"<":">";let[s]=N.computeOutAndReduceShapes(e,r);this.outputShape=s.length===0?[1]:s,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,[1,1,1]),this.inputShape=e,this.shaderKey=`argMinMax${this.op}`}getUserCode(){let e=`
var<workgroup> xBestIndices : array<i32, ${this.workGroupSize[0]}>;
var<workgroup> xBestValues : array<f32, ${this.workGroupSize[0]}>;
`,t=(s,a)=>this.outputShape.length===1?s:`${s}[${a}]`,n=s=>this.inputShape.length===1?"uniforms.xShape":`uniforms.xShape[${s}]`;return`
fn DIV_CEIL(a : u32, b : u32) -> u32 {
return ((a - 1u) / b + 1u);
}
${e}
// In order to get a flattened index into the input tensor, we need to
// add back the index along the reduced dimension to |outputCoords|.
// This function outputs the offset to the first value along
// |axis| and the stride to get the next value of the input along |axis|.
fn getInputCoordInfo(outputIndex : i32) -> vec2<i32>{
let outputCoords = getCoordsFromFlatIndex(outputIndex);
var i = ${this.outputShape.length-1};
var stride = 1;
var inputStride = 1;
var offset = 0;
for (var r = 1; r <= ${this.inputShape.length}; r = r + 1) {
let length = ${n(`${this.inputShape.length} - r`)};
if (${this.inputShape.length} - r == uniforms.axis) {
inputStride = stride;
} else {
offset = offset + ${t("outputCoords","i")} * stride;
i = i - 1;
}
stride = stride * length;
}
return vec2<i32>(offset, inputStride);
}
fn getInputIndex(coordInfo : vec2<i32>, index : i32) -> i32{
return coordInfo[0] + coordInfo[1] * index;
}
${Ue()}
let outputIndex = index / i32(workGroupSizeX);
let coordInfo = getInputCoordInfo(outputIndex);
let Length = ${n("uniforms.axis")};
var bestIndex = i32(localId.x);
var bestValue = uniforms.infinityValue;
for (var k = i32(localId.x); k < Length && outputIndex < uniforms.size;
k = k + i32(workGroupSizeX)) {
let candidate = f32(x.numbers[getInputIndex(coordInfo, k)]);
if (!isNanCustom(candidate) && candidate ${this.op} bestValue) {
bestValue = candidate;
bestIndex = k;
}
}
xBestValues[localId.x] = bestValue;
xBestIndices[localId.x] = bestIndex;
workgroupBarrier();
var reduceSize = min(u32(Length), workGroupSizeX);
for (var currentSize = reduceSize / 2u; reduceSize > 1u;
currentSize = reduceSize / 2u) {
let interval = DIV_CEIL(reduceSize, 2u);
if (localId.x < currentSize) {
let candidate = xBestValues[localId.x + interval];
if (candidate ${this.op} bestValue) {
bestValue = candidate;
xBestValues[localId.x] = bestValue;
xBestIndices[localId.x] = xBestIndices[localId.x + interval];
}
}
reduceSize = interval;
workgroupBarrier();
}
if (localId.x == 0u && outputIndex < uniforms.size) {
setOutputFlatI32(outputIndex, xBestIndices[localId.x]);
}
}
`}},Hie=class{constructor(e,t){this.variableNames=["A"],this.workGroupSize=[16,16,1];let n=new Array(e.length);for(let r=0;r<n.length;r++)n[r]=e[t[r]];this.outputShape=n,this.dispatchLayout={x:[0],y:[1]},this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[1,1,1]),this.shaderKey="transposeShared"}getUserCode(){return`
let TILE_DIM = ${this.workGroupSize[0]};
var<workgroup> tile : array<array<f32, ${this.workGroupSize[0]+1}>, ${this.workGroupSize[0]}>;
${zm()}
fn main([[builtin(local_invocation_id)]] localId : vec3<u32>,
[[builtin(workgroup_id)]] workgroupId : vec3<u32>) {
var x = i32(workgroupId.x) * TILE_DIM + i32(localId.x);
var y = i32(workgroupId.y) * TILE_DIM + i32(localId.y);
let width = uniforms.outShape[0];
let height = uniforms.outShape[1];
if (x < width && y < height) {
tile[localId.y][localId.x] =
A.numbers[y * width + x];
}
workgroupBarrier();
x = i32(workgroupId.y) * TILE_DIM + i32(localId.x);
y = i32(workgroupId.x) * TILE_DIM + i32(localId.y);
if (x < height && y < width) {
setOutputFlat((y * height + x), tile[localId.x]
[localId.y]);
}
}
`}},jie=class{constructor(e,t){this.variableNames=["A"],this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0;let n=new Array(e.length);for(let r=0;r<n.length;r++)n[r]=e[t[r]];this.outputShape=n,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.newDim=t,this.shaderKey=`transpose_${t}`}getUserCode(){let e=on(this.outputShape.length),t=qie(this.newDim);return`
${Ue()}
for(var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if(flatIndex < uniforms.size) {
let resRC = getCoordsFromFlatIndex(flatIndex);
setOutputFlat(flatIndex, A.numbers[getFlatIndex${this.outputShape.length}D(
${e}(${t}), uniforms.aShape)]);
}
}
}
`}};function qie(e){let t=e.length;if(t>4)throw Error(`Transpose for rank ${t} is not yet supported`);let n=new Array(t);for(let r=0;r<e.length;r++)n[e[r]]=`resRC[${r}]`;return n.join()}function cu(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{perm:a}=r,o=n,i=s.shape.length,u=new Array(i);for(let c=0;c<u.length;c++)u[c]=s.shape[a[c]];if(n.shouldExecuteOnCPU([s])){let d=o.tensorMap.get(s.dataId).values,p=Mie(d,s.shape,s.dtype,a,u);return n.makeTensorInfo(u,s.dtype,p)}if(s.shape.length===2&&w.arraysEqual(a,[1,0])){let c=new Hie(s.shape,a);return o.runWebGPUProgram(c,[s],s.dtype)}let l=new jie(s.shape,a);return o.runWebGPUProgram(l,[s],s.dtype)}var Kie={kernelName:Ja,backendName:"webgpu",kernelFunc:cu};function Xie(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a}=r,o=w.parseAxisParam(a,s.shape),i=N.getAxesPermutation(o,s.shape.length),u=s,l=[];i!=null&&(u=cu({inputs:{x:s},backend:n,attrs:{perm:i}}),l.push(u),o=N.getInnerMostAxes(o.length,u.shape.length)),N.assertAxesAreInnerMostDims("argMax",[o[0]],u.shape.length);let c=new TE(u.shape,o[0],"max"),d=[{type:"int32",data:[o[0]]},{type:"float32",data:[Number.NEGATIVE_INFINITY]}],p=n.runWebGPUProgram(c,[u],"int32",d);return l.forEach(h=>n.disposeData(h.dataId)),p}var Yie={kernelName:da,backendName:"webgpu",kernelFunc:Xie};function Qie(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a}=r,o=w.parseAxisParam(a,s.shape),i=N.getAxesPermutation(o,s.shape.length),u=s,l=[];i!=null&&(u=cu({inputs:{x:s},backend:n,attrs:{perm:i}}),l.push(u),o=N.getInnerMostAxes(o.length,u.shape.length)),N.assertAxesAreInnerMostDims("argMin",[o[0]],u.shape.length);let c=new TE(u.shape,o[0],"min"),d=[{type:"int32",data:[o[0]]},{type:"float32",data:[Number.POSITIVE_INFINITY]}],p=n.runWebGPUProgram(c,[u],"int32",d);return l.forEach(h=>n.disposeData(h.dataId)),p}var Zie={kernelName:ju,backendName:"webgpu",kernelFunc:Qie},NE=class{constructor(e,t){this.variableNames=["x"],this.uniforms="stride : vec2<i32>; pad : vec2<i32>; dilation : vec2<i32>; convDims : vec2<i32>; filterDims : vec2<i32>;",this.workGroupSize=[128,1,1],this.size=!0,this.outputShape=e.outShape,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`pool2D_${t}`,this.poolType=t}getUserCode(){let e="resultValue = max(value, resultValue);";this.poolType==="avg"&&(e="resultValue = resultValue + value; count = count + 1.0;");let t="resultValue";return this.poolType==="avg"&&(t="resultValue / count"),`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let batch = coords[0];
let xRCCorner = vec2<i32>(coords.yz) * uniforms.stride - uniforms.pad;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
var resultValue = ${this.poolType==="avg"?"0.0":"-1.0 / pow(10.0, -20.0)"};
var count = 0.0;
for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + uniforms.dilation.x) {
let xR = xRCorner + wR;
if (xR < 0 || xR >= uniforms.convDims.x) {
continue;
}
for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + uniforms.dilation.y) {
let xC = xCCorner + wC;
if (xC < 0 || xC >= uniforms.convDims.y) {
continue;
}
let value = getX(batch, xR, xC, coords[3]);
${e}
}
}
setOutputFlat(index, ${t});
}
}
`}},_E=class{constructor(e){this.variableNames=["x"],this.uniforms="stride : vec2<i32>;",this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e.outShape,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="poolWithFilterSizeEqualsOne"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let batch = coords[0];
let d = coords[3];
let xRCCorner = coords.yz * uniforms.stride;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
let value = getX(batch, xRCorner, xCCorner, d);
setOutputFlat(index, value);
}
}
`}};function Jie(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=r,l=1,c=N.computePool2DInfo(s.shape,a,o,l,i,u);if(c.filterWidth===1&&c.filterHeight===1&&w.arraysEqual(c.inShape,c.outShape))return Lr({inputs:{x:s},backend:n});let d,p=[{type:"int32",data:[c.strideHeight,c.strideWidth]}];return c.filterHeight===1&&c.filterWidth===1?d=new _E(c):(d=new NE(c,"avg"),p.push({type:"int32",data:[c.padInfo.top,c.padInfo.left]},{type:"int32",data:[c.dilationHeight,c.dilationWidth]},{type:"int32",data:[c.inHeight,c.inWidth]},{type:"int32",data:[c.effectiveFilterHeight,c.effectiveFilterWidth]})),n.runWebGPUProgram(d,[s],s.dtype,p)}var eue={kernelName:pa,backendName:"webgpu",kernelFunc:Jie};function tue(e){let{inputs:t,backend:n,attrs:r}=e,{a:s,b:a}=t,{transposeA:o,transposeB:i}=r;return xk({a:s,b:a,transposeA:o,transposeB:i,backend:n})}var nue={kernelName:ha,backendName:"webgpu",kernelFunc:tue},rue=class{constructor(e,t){this.variableNames=["source"],this.workPerThread=1,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.rank=t.length,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.start=e,this.uniforms=`start : ${on(e.length)}; `,this.shaderKey="slice"}getUserCode(){let e=on(this.rank),t=sue(this.rank),n;return this.start.length===1?n=this.outputShape.map((s,a)=>"sourceLoc = uniforms.start + coords;"):n=this.outputShape.map((s,a)=>`sourceLoc.${wk[a]} = uniforms.start[${a}] + coords.${wk[a]};`),`
${Ue()}
if (index < uniforms.size) {
var sourceLoc : ${e};
let coords = getCoordsFromFlatIndex(index);
${n.join(`
`)}
setOutputFlat(index, getSource(${t}));
}
}
`}},wk=["x","y","z","w","u","v"];function sue(e){if(e===1)return"sourceLoc";if(e<=6)return wk.slice(0,e).map(t=>`sourceLoc.${t}`).join(",");throw Error(`Slicing for rank ${e} is not yet supported`)}function sl(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,size:o}=r,[i,u]=$t.parseSliceParams(s,a,o);if($t.assertParamsValid(s,i,u),n.shouldExecuteOnCPU([s])||s.dtype==="string"){let d=n.tensorMap.get(s.dataId),p=$ie(d.values,i,u,s.shape,s.dtype);return n.makeTensorInfo(u,s.dtype,p)}if(w.sizeFromShape(u)===0)return n.makeTensorInfo(u,s.dtype,[]);let l=new rue(i,u),c=[{type:"int32",data:i}];return n.runWebGPUProgram(l,[s],s.dtype,c)}var aue={kernelName:yi,backendName:"webgpu",kernelFunc:sl},oue=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockShape:a,crops:o}=r;w.assert(s.shape.length<=4,()=>"batchToSpaceND for rank > 4 with a WebGPU backend not implemented yet");let i=a.reduce((y,v)=>y*v),u=N.getReshaped(s.shape,a,i),l=N.getPermuted(u.length,a.length),c=N.getReshapedPermuted(s.shape,a,i),d=N.getSliceBeginCoords(o,a.length),p=N.getSliceSize(c,o,a.length),h=[],f=We({inputs:{x:s},backend:n,attrs:{shape:u}}),m=cu({inputs:{x:f},backend:n,attrs:{perm:l}}),g=We({inputs:{x:m},backend:n,attrs:{shape:c}}),b=sl({inputs:{x:g},backend:n,attrs:{begin:d,size:p}});return h.push(f),h.push(m),h.push(g),h.forEach(y=>n.disposeData(y.dataId)),b},iue={kernelName:Uo,backendName:"webgpu",kernelFunc:oue},EE=Rn({opSnippet:Mt.NOT_EQUAL,dtype:"bool",cpuKernelImpl:Tie}),uue={kernelName:oi,backendName:"webgpu",kernelFunc:EE};function fp(e){let{inputs:t,backend:n}=e,{input:r}=t,s=n.tensorMap.get(r.dataId);return Lr({inputs:{x:s.complexTensorInfos.real},backend:n})}var cue={kernelName:rd,backendName:"webgpu",kernelFunc:fp};function lue(e,t){let n=new Wm(e.shape,bt.TO_INT),r=t.runWebGPUProgram(n,[e],"int32");return{dataId:r.dataId,shape:r.shape,dtype:r.dtype}}function kk(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{dtype:a}=r;if(a==="complex64"){if(s.dtype==="complex64")return Lr({inputs:{x:s},backend:n});let o=kt(s.shape),i=kk({inputs:{x:s},backend:n,attrs:{dtype:"float32"}}),u=rl({inputs:{real:i,imag:o},backend:n});return o.dispose(),n.disposeData(i.dataId),u}if(s.dtype==="complex64"){let o=fp({inputs:{input:s},backend:n}),i=kk({inputs:{x:o},backend:n,attrs:{dtype:a}});return n.disposeData(o.dataId),i}if(!w.hasEncodingLoss(s.dtype,a)){let o=Lr({inputs:{x:s},backend:n});return{dataId:o.dataId,shape:o.shape,dtype:a}}if(a==="int32")return lue(s,n);if(a==="bool"){let o=n.makeTensorInfo([],"bool",w.getTypedArrayFromDType("bool",1)),u=EE({inputs:{a:s,b:o},backend:n});return n.disposeData(o.dataId),u}throw new Error(`Error in Cast: failed to cast ${s.dtype} to ${a}`)}var due={kernelName:fa,backendName:"webgpu",kernelFunc:kk},pue=fn({opType:bt.CEIL,cpuKernelImpl:uie}),hue={kernelName:ma,backendName:"webgpu",kernelFunc:pue},fue=class{constructor(e){this.variableNames=["A"],this.uniforms="minVal : f32; maxVal : f32;",this.workPerThread=4,this.workGroupSize=[64,1,1],this.isVec4=!0,this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.shaderKey="clipVec4"}getUserCode(){return`
${Ue()}
if(index < uniforms.size) {
let value = getAAtOutCoordsByGlobalIndex(index);
var clampedValue : vec4<f32>;
for (var i = 0; i < 4; i = i + 1) {
if (isNanCustom(value[i])) {
clampedValue[i] = value[i];
} else {
clampedValue[i] = clamp(value[i], uniforms.minVal, uniforms.maxVal);
}
}
setOutputFlat(index, clampedValue);
}
}
`}},mue=class{constructor(e){this.variableNames=["A"],this.uniforms="minVal : f32; maxVal : f32;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="clip"}getUserCode(){return`
${Ue()}
if(index < uniforms.size) {
let value = getAAtOutCoordsByGlobalIndex(index);
if (isNanCustom(value)) {
setOutputFlat(index, value);
return;
}
setOutputFlat(index, clamp(value, uniforms.minVal, uniforms.maxVal));
}
}
`}};function gue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{clipValueMin:a,clipValueMax:o}=r,i,u=[{type:"float32",data:[a]},{type:"float32",data:[o]}];return w.sizeFromShape(s.shape)%4==0?i=new fue(s.shape):i=new mue(s.shape),n.runWebGPUProgram(i,[s],s.dtype,u)}var bue={kernelName:Es,backendName:"webgpu",kernelFunc:gue},yue=class{constructor(e){this.uniforms="",this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=N.computeOutShape(e,1),this.variableNames=e.map((t,n)=>`T${n}`),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.offsetLength=e.length-1;for(let t=0;t<this.offsetLength;t++)this.uniforms+=`offset${t} : i32;`;this.shaderKey="concat"}getUserCode(){let e=[];if(this.offsetLength>0){e.push("if (yC < uniforms.offset0){ setOutput(coords.x, coords.y, getT0(yR, yC)); }");for(let s=1;s<this.offsetLength;s++)e.push(`elseif (yC < uniforms.offset${[s]}){ setOutput(coords.x, coords.y, getT${s}(yR, yC - uniforms.offset${s-1})); }`);let n=this.offsetLength,r=this.offsetLength-1;e.push(`else { setOutput(coords.x, coords.y, getT${n}(yR, yC - uniforms.offset${r})); }`)}else e.push("setOutput(coords.x, coords.y, getT0(yR, yC));");return`
${Ue()}
for(var i = 0; i < ${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
if(flatIndex < uniforms.size) {
let coords = getCoordsFromFlatIndex(flatIndex);
let yR = coords.x;
let yC = coords.y;
${e.join(`
`)}
}
}
}
`}};function Vm(e){let{inputs:t,backend:n}=e,{input:r}=t,s=n.tensorMap.get(r.dataId);return Lr({inputs:{x:s.complexTensorInfos.imag},backend:n})}var vue={kernelName:Jl,backendName:"webgpu",kernelFunc:Vm};function Ik(e,t,n){let r=e[0].dtype;if(r==="complex64"){let h=e.map(y=>fp({inputs:{input:y},backend:n})),f=e.map(y=>Vm({inputs:{input:y},backend:n})),m=Ik(h,t,n),g=Ik(f,t,n),b=rl({inputs:{real:m,imag:g},backend:n});return h.forEach(y=>n.disposeData(y.dataId)),f.forEach(y=>n.disposeData(y.dataId)),n.disposeData(m.dataId),n.disposeData(g.dataId),b}let s=n.shouldExecuteOnCPU(e);if(r==="string"&&(s=!0),s){let h=e.map(x=>{let k=w.sizeFromShape(x.shape.slice(t));return We({inputs:{x},backend:n,attrs:{shape:[-1,k]}})}),f=h.map(x=>({vals:n.readSync(x.dataId),shape:x.shape})),m=N.computeOutShape(h.map(x=>x.shape),1),g=h[0].shape[0]===1,b=cie(f,m,r,g),y=N.computeOutShape(e.map(x=>x.shape),t),v=n.makeTensorInfo(y,r,b);return h.forEach(x=>n.disposeData(x.dataId)),v}let{tensors2D:a,outShape:o}=xue(e,t,n),i=a.map(h=>h.shape),u=new yue(i),l=[],c=new Array(i.length-1);if(c.length>0){c[0]=i[0][1],l.push({type:"int32",data:[c[0]]});for(let h=1;h<c.length;h++)c[h]=c[h-1]+i[h][1],l.push({type:"int32",data:[c[h]]})}let d=n.runWebGPUProgram(u,a,a[0].dtype,l);a.forEach(h=>n.disposeData(h.dataId));let p=We({inputs:{x:d},backend:n,attrs:{shape:o}});return n.disposeData(d.dataId),p}function xue(e,t,n){let r=N.computeOutShape(e.map(a=>a.shape),t);return{tensors2D:e.map(a=>We({inputs:{x:a},backend:n,attrs:{shape:[w.sizeFromShape(a.shape.slice(0,t)),w.sizeFromShape(a.shape.slice(t))]}})),outShape:r}}function AE(e){let{inputs:t,backend:n,attrs:r}=e,{axis:s}=r,a=w.parseAxisParam(s,t[0].shape)[0],o=N.computeOutShape(t.map(l=>l.shape),a);if(w.sizeFromShape(o)===0)return n.makeTensorInfo(o,t[0].dtype,[]);let i=t.filter(l=>w.sizeFromShape(l.shape)>0);if(i.length===1)return Lr({inputs:{x:i[0]},backend:n});let u=i.map(l=>l.shape);return N.assertParamsConsistent(u,a),Ik(i,a,n)}var wue={kernelName:Go,backendName:"webgpu",kernelFunc:AE},kue=class{constructor(e,t){this.variableNames=["A"],this.uniforms=`pad : vec2<i32>; stride : vec2<i32>; dilation : vec2<i32>; outWidth : i32; itemsPerBlockRow : i32;
inChannels : i32;`,this.workPerThread=4,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]),this.isChannelsLast=t,this.shaderKey=`im2col_${this.isChannelsLast}`}getUserCode(){let e=this.isChannelsLast?0:1,t=this.isChannelsLast?1:2;return`
${Ue()}
for(var i = 0; i<${this.workPerThread}; i = i + 1) {
let flatIndex = index * ${this.workPerThread} + i;
let rc = getCoordsFromFlatIndex(flatIndex);
if(flatIndex < uniforms.size) {
let blockIndex = rc[0];
let pos = rc[1];
let offsetY = blockIndex / uniforms.outWidth * uniforms.stride[1] - uniforms.pad[1];
let d0 = offsetY + uniforms.dilation[1] * pos / uniforms.itemsPerBlockRow;
var value = 0.0;
if(d0 < uniforms.aShape[${e}] && d0 >= 0) {
let offsetX = (blockIndex % uniforms.outWidth) * uniforms.stride[0] -
uniforms.pad[0];
let d1 = offsetX + uniforms.dilation[0] * ((pos %
uniforms.itemsPerBlockRow) / uniforms.inChannels);
let ch = pos % uniforms.inChannels;
if(d1 < uniforms.aShape[${t}] && d1 >= 0) {
value = getA(d0, d1, ch);
}
}
setOutputFlat(flatIndex, value);
}
}
}
`}};function $E({x:e,filter:t,convInfo:n,backend:r,bias:s=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let u=e.shape,l=n.dataFormat==="channelsLast",c=!1,d=!1,p=l?u[0]*u[1]*u[2]:u[0]*u[2]*u[3],h=We({inputs:{x:e},backend:r,attrs:{shape:[1,p,n.inChannels]}}),f=We({inputs:{x:t},backend:r,attrs:{shape:[1,n.inChannels,n.outChannels]}}),m=xk({a:h,b:f,transposeA:c,transposeB:d,backend:r,bias:s,activation:i,preluActivationWeights:a,leakyreluAlpha:o}),g=We({inputs:{x:m},backend:r,attrs:{shape:n.outShape}});return r.disposeData(h.dataId),r.disposeData(f.dataId),r.disposeData(m.dataId),g}function Iue({x:e,filter:t,convInfo:n,backend:r,bias:s=null,preluActivationWeights:a=null,leakyreluAlpha:o=0,activation:i=null}){let{filterWidth:u,filterHeight:l,inChannels:c,strideWidth:d,strideHeight:p,padInfo:h,outWidth:f,outHeight:m,dilationWidth:g,dilationHeight:b,dataFormat:y}=n,v=y==="channelsLast",x=u*l*c,k=m*f,T=[k,x],C=!1,E=!1,F=[],O=We({inputs:{x:e},backend:r,attrs:{shape:e.shape.slice(1)}}),D=We({inputs:{x:t},backend:r,attrs:{shape:[1,x,-1]}});F.push(O),F.push(D);let R=new kue(T,v),_=[{type:"int32",data:[h.left,h.top]},{type:"int32",data:[d,p]},{type:"int32",data:[g,b]},{type:"int32",data:[f]},{type:"int32",data:[c*u]},{type:"int32",data:[c]}],L=r.runWebGPUProgram(R,[O],O.dtype,_),U=We({inputs:{x:L},backend:r,attrs:{shape:[1,T[0],T[1]]}});F.push(L),F.push(U);let j=[1,T[0],T[1]],K=new kE(j,[1,k,n.outChannels],X().get("WEBGPU_MATMUL_WORK_PER_THREAD"),C,E),q=j[1],Q=j[2],ee=n.outChannels,re=[{type:"int32",data:[q]},{type:"int32",data:[ee]},{type:"int32",data:[Q]}],se=r.runWebGPUProgram(K,[U,D],U.dtype,re),ne=v?[1,m,f,n.outChannels]:[1,n.outChannels,m,f],ie=We({inputs:{x:se},backend:r,attrs:{shape:ne}});F.push(se);for(let te of F)r.disposeData(te.dataId);return ie}var FE=class{constructor(e,t=!1,n=null,r=!1,s=!1){this.variableNames=["x","W"],this.uniforms=`filterDims : vec2<i32>; pad : vec2<i32>; stride : vec2<i32>; dilation : vec2<i32>;
dimAOuter : i32; dimBOuter : i32; dimInner : i32;`,this.isVec4=!0,this.outputShape=e.outShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.workGroupSize=[8,8,1];let a=[4,4,1];this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,a),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivationWeights=r,this.hasLeakyreluAlpha=s,this.addBias&&this.variableNames.push("bias"),this.hasPreluActivationWeights&&this.variableNames.push("preluActivationWeights"),this.hasLeakyreluAlpha&&this.variableNames.push("leakyreluAlpha"),[this.fitA,this.fitB]=this.getShapeFit(a),this.shaderKey=`conv2DMMVec4_${this.activation}_${this.fitA}_${this.fitB}`}getShapeFit(e){let t=this.workGroupSize[1]*e[1],n=this.workGroupSize[0]*e[0],r=n,s=[t,r],a=[r,n],o=this.outputShape[1]*this.outputShape[2],i=this.outputShape[3],u=this.convInfo.filterHeight*this.convInfo.filterWidth*this.convInfo.inChannels;return[Us(s,[o,u]),Us(a,[u,i])]}getSampleAWithRemainder(e){return`let flatIndex${e} = getFlatIndex4D(coord, uniforms.xShape);
let divBy4Remainder${e} = flatIndex${e} % 4;
let divBy4Index${e} = flatIndex${e} / 4;
let curData${e} = x.numbers[divBy4Index${e}];
if (divBy4Remainder${e} == 0) {
temp = curData${e};
} else {
// TODO: This could end up being a redundant load with another one in
// the same shader invocation. Perhaps there's an opportunity for
// optimization
let nextData${e} = x.numbers[divBy4Index${e} + 1];
if (divBy4Remainder${e} == 1) {
temp = vec4<f32>(curData${e}.yzw, nextData${e}.x);
} elseif (divBy4Remainder${e} == 2) {
temp = vec4<f32>(curData${e}.zw, nextData${e}.xy);
} elseif (divBy4Remainder${e} == 3) {
temp = vec4<f32>(curData${e}.w, nextData${e}.xyz);
}
}
`}getUserCode(){let t=wE([4,4,1],this.workGroupSize),s=`let outRow = r / uniforms.outShape[2];
let outCol = r % uniforms.outShape[2];
let WRow = c / (uniforms.filterDims[1] * uniforms.xShape[3]);
let WCol = c / uniforms.xShape[3] % uniforms.filterDims[1];
let inChCoord = c % uniforms.xShape[3];
var coord = vec4<i32>(
batch,
outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0],
outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1],
inChCoord);
var resData = vec4<f32>(0.0);
${this.convInfo.inChannels%4===0?`// The bounds checking is always needed since we use it to pad zero for
// the 'same' padding type.
if (coordsInBounds4D(coord, uniforms.xShape)) {
resData = x.numbers[getFlatIndex4D(coord, uniforms.xShape) / 4];
} else {
resData = vec4<f32>(0.0); }`:`var temp = vec4<f32>(0.0);
${this.getSampleAWithRemainder(1)}
resData = temp;
if (WCol == (uniforms.filterDims[1] - 1)) {
coord = vec4<i32>(
coord.x, coord.y + 1, coord.z + 1 - uniforms.filterDims[1], 0);
${this.getSampleAWithRemainder(2)}
if (inChCoord == 0) {
resData = vec4<f32>(resData.xyz, temp.x);
} elseif (inChCoord == 1) {
resData = vec4<f32>(resData.xy, temp.xy);
} else {
resData = vec4<f32>(resData.x, temp.xyz);
}
}
`}
return resData;`,a=this.fitA?`${s}`:`if (r < uniforms.dimAOuter && c < uniforms.dimInner) {
${s}
}
return vec4<f32>(0.0);
`,o=this.fitB?"return W.numbers[row * uniforms.dimBOuter / 4 + col];":`if(coordsInBounds2D(vec2<i32>(row, col * 4), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return W.numbers[row * uniforms.dimBOuter / 4 + col];
}
return vec4<f32>(0.0);
`,i="",u="";if(this.activation){let d=Gs(this.activation,this.isVec4);if(this.hasPreluActivationWeights)i=`fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${d}
}`;else{if(this.hasLeakyreluAlpha)throw i=`fn activation(a: vec4<f32>) -> vec4<f32> {
let b = getLeakyreluAlphaAtOutCoords();
${d}
}`,new Error("Leakyrelu is not supported.");i=`
fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
${d}
}`}u="value = activation(value, outCoord);"}let l=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${i}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
let r = row;
let c = col * 4;
var batch = i32(globalId.z);
${a}
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> vec4<f32> {
${o}
}
fn mm_write(row : i32, col : i32, valueInput : vec4<f32>, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
if (row < uniforms.dimAOuter && col * 4 < uniforms.dimBOuter)
{
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col * 4);
${l}
${u}
setOutput(outCoord[0], outCoord[1], outCoord[2], outCoord[3],
value);
}
}
${t}
`}},DE=class{constructor(e,t=!1,n=null,r=!1){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>; pad : vec2<i32>; stride : vec2<i32>; dilation : vec2<i32>; dimAOuter : i32; dimBOuter : i32; dimInner : i32;",this.outputShape=e.outShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.workGroupSize=fk(this.dispatchLayout,this.outputShape),this.elementsPerThread=gk(this.dispatchLayout,this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivationWeights=r,[this.fitA,this.fitB]=this.getShapeFit(),this.shaderKey=`conv2DMM_${this.elementsPerThread}_${this.activation}_${this.fitA}_${this.fitB}`}getShapeFit(){let e=this.workGroupSize[1]*this.elementsPerThread[1],t=this.workGroupSize[0]*this.elementsPerThread[0],n=e>t?e:t;w.assert(n%this.workGroupSize[0]==0&&n%this.workGroupSize[1]==0,()=>"tileInner must be multiple of workgroupsize.x and workgroupsize.y");let r=[e,n],s=[n,t],a=this.outputShape[1]*this.outputShape[2],o=this.outputShape[3],i=this.convInfo.filterHeight*this.convInfo.filterWidth*this.convInfo.inChannels;return[Us(r,[a,i]),Us(s,[i,o])]}getUserCode(){let e=vk(this.elementsPerThread,this.workGroupSize),t=`
let outRow = row / uniforms.outShape[2];
let outCol = row % uniforms.outShape[2];
let WRow = col / (uniforms.filterDims[1] * uniforms.xShape[3]);
let WCol = col / uniforms.xShape[3] % uniforms.filterDims[1];
let coord = vec4<i32>(
batch,
outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0],
outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1],
col % uniforms.xShape[3]);
// The bounds checking is always needed since we use it to pad zero for the
// 'same' padding type.
if(coordsInBounds4D(coord, uniforms.xShape)) {
return x.numbers[getFlatIndex4D(coord, uniforms.xShape)];
}
return 0.0;`,n=this.fitA?`${t}`:`if (row < uniforms.dimAOuter && col < uniforms.dimInner) {
${t}
}
return 0.0;
`,r=this.fitB?"return W.numbers[row * uniforms.dimBOuter + col];":`if(coordsInBounds2D(vec2<i32>(row, col), vec2<i32>(uniforms.dimInner, uniforms.dimBOuter))) {
return W.numbers[row * uniforms.dimBOuter + col];
}
return 0.0;
`,s="",a="";if(this.activation){let u=Gs(this.activation,!1);this.hasPreluActivationWeights?s=`fn activation(a: f32, outCoord : vec4<i32>) -> f32 {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${u}
}`:s=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
${u}
}
`,a="value = activation(value, outCoord);"}let o=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${s}
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
var batch = i32(globalId.z);
${n}
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
${r}
}
fn mm_write(row : i32, col : i32, valueInput : f32, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col);
${o}
${a}
result.numbers[getFlatIndex4D(outCoord, uniforms.outShape)] = value;
}
${e}
`}},RE=class{constructor(e,t=!1,n=null,r=!1){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>; pad : vec2<i32>; stride : vec2<i32>; dilation : vec2<i32>;",this.workGroupSize=[128,1,1],this.outputShape=e.outShape,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivationWeights=r,this.shaderKey=`conv2DNaive_${this.activation}`}getUserCode(){let e="",t="";if(this.activation){let s=Gs(this.activation);this.hasPreluActivationWeights?e=`fn activation(a : f32, outCoord : vec4<i32>) -> f32{
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${s}
}`:e=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32{
${s}
}
`,t="value = activation(value, outCoord);"}let n=this.addBias?"value = value + getBiasAtOutCoordsByCoords(outCoord);":"";return`
${e}
fn readInp(batch : i32, row : i32, col : i32, chan : i32) -> f32 {
let coord = vec4<i32>(batch, row, col, chan);
if(coordsInBounds4D(coord, uniforms.xShape)) {
return getX(batch, row, col, chan);
}
return 0.0;
}
fn readFilt(row : i32, col : i32, xChannel : i32, outChannel : i32) -> f32{
let coord = vec4<i32>(row, col, xChannel, outChannel);
if(coordsInBounds4D(coord, uniforms.wShape)) {
return getW(row, col, xChannel, outChannel);
}
return 0.0;
}
fn writeResult(batch : i32, row : i32, col : i32, chan : i32, value : f32) {
let coord = vec4<i32>(batch, row, col, chan);
if (coordsInBounds4D(coord, uniforms.outShape)) {
${n}
${t}
setOutput(batch, row, col, chan, value);
}
}
${hk()} {
let coords = getOutputCoordsWithFlatDispatchLayout(globalId, localId, numWorkgroups);
let batch = coords[0];
let outChannel = coords[3];
var acc = 0.0;
for (var row = 0; row < uniforms.filterDims[0]; row = row + 1) {
for (var col = 0; col < uniforms.filterDims[1]; col = col + 1) {
for (var xChannel = 0; xChannel < uniforms.xShape[3]; xChannel = xChannel + 1) {
let coordRow = coords[1] * uniforms.stride[0] + uniforms.dilation[0] * row - uniforms.pad[0];
let coordCol = coords[2] * uniforms.stride[1] + uniforms.dilation[1] * col - uniforms.pad[1];
let v = readInp(batch, coordRow, coordCol, xChannel);
let f = readFilt(row, col, xChannel, outChannel);
acc = acc + v * f;
}
}
}
writeResult(batch, coords[1], coords[2], outChannel, acc);
}
`}};function Sue(e){let{inputs:t,attrs:n,backend:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dataFormat:u,dilations:l,dimRoundingMode:c}=n,d=N.convertConv2DDataFormat(u),p=N.computeConv2DInfo(s.shape,a.shape,o,l,i,c,!1,d);if(p.filterHeight===1&&p.filterWidth===1&&p.dilationHeight===1&&p.dilationWidth===1&&p.strideHeight===1&&p.strideWidth===1&&(p.padInfo.type==="SAME"||p.padInfo.type==="VALID"))return $E({x:s,filter:a,convInfo:p,backend:r});if(X().getBool("WEBGPU_CONV_SEPARATE_IM2COL_SHADER")&&s.shape[0]===1)return Iue({x:s,filter:a,convInfo:p,backend:r});let h,f=[p.padInfo.top,p.padInfo.left],m=[{type:"int32",data:[p.filterHeight,p.filterWidth]},{type:"int32",data:[...f]},{type:"int32",data:[p.strideHeight,p.strideWidth]},{type:"int32",data:[p.dilationHeight,p.dilationWidth]}],g=X().getBool("WEBGPU_USE_NAIVE_CONV2D");if(g?h=new RE(p):(p.inChannels%4==0||p.inChannels===3&&p.padInfo.type==="VALID")&&p.outChannels%4==0&&p.outChannels>=64?h=new FE(p):h=new DE(p),!g){let b=p.outShape[1]*p.outShape[2],y=p.outShape[3],v=p.filterHeight*p.filterWidth*p.inShape[3];m.push({type:"int32",data:[b]},{type:"int32",data:[y]},{type:"int32",data:[v]})}return r.runWebGPUProgram(h,[s,a],s.dtype,m)}var Cue={kernelName:ga,backendName:"webgpu",kernelFunc:Sue},Tue=class{constructor(e){this.variableNames=["x","W"],this.uniforms="filterDims : vec2<i32>; pads : vec2<i32>; stride : vec2<i32>; outBackprop : vec4<i32>; dimAOuter : i32; dimBOuter : i32; dimInner : i32;",this.outputShape=e.inShape,w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),this.dispatchLayout={x:[3],y:[1,2],z:[0]},this.workGroupSize=fk(this.dispatchLayout,this.outputShape),this.elementsPerThread=gk(this.dispatchLayout,this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,this.elementsPerThread),this.shaderKey=`conv2DDerInputMM_${this.elementsPerThread}`}getUserCode(){return`
fn mm_readA(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
var batch = i32(globalId.z);
if (row < uniforms.dimAOuter && col < uniforms.dimInner) {
let outRow = row / uniforms.outShape[2];
let outCol = row % uniforms.outShape[2];
let WRow = col / (uniforms.filterDims[1] * uniforms.outBackprop[3]);
let WCol = col / uniforms.outBackprop[3] % uniforms.filterDims[1];
let xR = f32(outRow - uniforms.pads[0] + WRow) / f32(uniforms.stride[0]);
let xC = f32(outCol - uniforms.pads[1] + WCol) / f32(uniforms.stride[1]);
if (xR < 0.0 || xR >= f32(uniforms.outBackprop[1]) || fract(xR) > 0.0) {
return 0.0;
}
if (xC < 0.0 || xC >= f32(uniforms.outBackprop[2]) || fract(xC) > 0.0) {
return 0.0;
}
let coord = vec4<i32>(
batch,
i32(xR),
i32(xC),
col % uniforms.outBackprop[3]);
return x.numbers[getFlatIndex4D(coord, uniforms.xShape)];
}
return 0.0;
}
fn mm_readB(row : i32, col : i32, globalId : vec3<u32>) -> f32 {
let coordX = uniforms.filterDims.x - 1 -
row / (uniforms.filterDims[1] * uniforms.outBackprop[3]);
let coordY = uniforms.filterDims.y - 1 -
(row / uniforms.outBackprop[3]) % uniforms.filterDims[1];
if (row < uniforms.dimInner && col < uniforms.dimBOuter &&
coordX >= 0 && coordY >= 0) {
let coord = vec4<i32>(coordX, coordY, col,
row % uniforms.outBackprop[3]);
return W.numbers[getFlatIndex4D(coord, uniforms.wShape)];
}
return 0.0;
}
fn mm_write(row : i32, col : i32, valueInput : f32, globalId : vec3<u32>) {
var batch = i32(globalId.z);
var value = valueInput;
let outCoord = vec4<i32>(
batch,
row / uniforms.outShape[2],
row % uniforms.outShape[2],
col);
result.numbers[getFlatIndex4D(outCoord, uniforms.outShape)] = value;
}
${vk(this.elementsPerThread,this.workGroupSize)}
`}},Nue=class{constructor(e){this.variableNames=["dy","W"],this.uniforms="filterDims : vec2<i32>; pads : vec2<i32>; stride : vec2<i32>; outBackprop : vec4<i32>;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e.inShape,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.isChannelsLast=e.dataFormat==="channelsLast",this.shaderKey=`conv2DDerInput_${this.isChannelsLast}`}getUserCode(){let e=this.isChannelsLast?1:2,t=this.isChannelsLast?2:3,n=this.isChannelsLast?3:1;return`
${Ue()} {
if(index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let batch = coords[0];
let d1 = coords[${n}];
let dyCorner = vec2<i32>(coords[${e}]), coords[${t}]) - uniforms.pads;
let dyRCorner = dyCorner.x;
let dyCCorner = dyCorner.y;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
var dotProd = 0.0;
for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + 1) {
let dyR = (f32(dyRCorner) + f32(wR)) / f32(uniforms.stride.x);
let wRPerm = uniforms.filterDims.x - 1 - wR;
if (dyR < 0.0 || dyR >= f32(uniforms.outBackprop[1]) || fract(dyR) > 0.0 ||
wRPerm < 0) {
continue;
}
let idyR = dyR;
for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + 1) {
let dyC = (f32(dyCCorner) + f32(wC)) / f32(uniforms.stride.y);
let wCPerm = uniforms.filterDims.y - 1 - wC;
if (dyC < 0.0 || dyC >= f32(uniforms.outBackprop[2]) ||
fract(dyC) > 0.0 || wCPerm < 0) {
continue;
}
let idyC = dyC;
for (var d2 = 0; d2 < uniforms.outBackprop[3]; d2 = d2 + 1) {
if (${this.isChannelsLast}) {
let xValue = getDy(batch, idyR, idyC, d2);
let wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd = dotProd + xValue * wValue;
} else {
let xValue = getDy(batch, d2, idyR, idyC);
let wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd = dotProd + xValue * wValue;
}
}
}
}
setOutputFlat(index, dotProd);
}
}
`}};function _ue(e){let{inputs:t,backend:n,attrs:r}=e,{dy:s,filter:a}=t,{inputShape:o,strides:i,pad:u,dataFormat:l,dimRoundingMode:c}=r,d=N.convertConv2DDataFormat(l),p=N.computeConv2DInfo(o,a.shape,i,1,u,c,!1,d),h=[{type:"int32",data:[p.filterHeight,p.filterWidth]},{type:"int32",data:[p.filterHeight-1-p.padInfo.top,p.filterWidth-1-p.padInfo.left]},{type:"int32",data:[p.strideHeight,p.strideWidth]},{type:"int32",data:[p.batchSize,p.outHeight,p.outWidth,p.outChannels]}],f;if(X().getBool("WEBGPU_USE_NAIVE_CONV2D_TRANSPOSE"))f=new Nue(p);else{f=new Tue(p);let m=p.inShape[1]*p.inShape[2],g=p.inShape[3],b=p.filterHeight*p.filterWidth*p.outChannels;h.push({type:"uint32",data:[m]},{type:"uint32",data:[g]},{type:"uint32",data:[b]})}return n.runWebGPUProgram(f,[s,a],"float32",h)}var Eue={kernelName:ba,backendName:"webgpu",kernelFunc:_ue},Aue=fn({opType:bt.COS}),$ue={kernelName:ya,backendName:"webgpu",kernelFunc:Aue},Fue=fn({opType:bt.COSH}),Due={kernelName:va,backendName:"webgpu",kernelFunc:Fue},Rue=class{constructor(e,t,n,r){this.variableNames=["Image","Boxes","BoxInd"],this.uniforms="extrapolationValue : f32;",this.workGroupSize=[64,1,1],this.size=!0;let[s]=t;this.outputShape=[s,n[0],n[1],e],this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.methodId=r==="bilinear"?1:0,this.cropHeightBiggerThan1=this.outputShape[1]>1,this.cropWidthBiggerThan1=this.outputShape[2]>1,this.shaderKey=`cropAndResize_${this.methodId}_${this.cropHeightBiggerThan1}_${this.cropWidthBiggerThan1}`}getUserCode(){let[e,t]=["f32(uniforms.imageShape[1] - 1)","f32(uniforms.imageShape[2] - 1)"],[n,r,s]=this.cropHeightBiggerThan1?[`(${e} / f32(uniforms.outShape[1] - 1))`,"(y2-y1) * height_ratio",`y1*${e} + f32(y)*(height_scale)`]:["0.0","0.0",`0.5 * (y1+y2) * ${e}`],[a,o,i]=this.cropWidthBiggerThan1?[`(${t} / f32(uniforms.outShape[2] - 1))`,"(x2-x1) * width_ratio",`x1*${t} + f32(x)*(width_scale)`]:["0.0","0.0",`0.5 * (x1+x2) * ${t}`];return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let height_ratio = f32(${n});
let width_ratio = f32(${a});
let b = coords[0];
let y = coords[1];
let x = coords[2];
let d = coords[3];
// get box vals
let y1 = getBoxes(b, 0);
let x1 = getBoxes(b, 1);
let y2 = getBoxes(b, 2);
let x2 = getBoxes(b, 3);
// get image in batch index
let bInd = i32(round(getBoxInd(b)));
if(bInd < 0 || bInd >= uniforms.outShape[0]) {
return;
}
let height_scale = ${r};
let width_scale = ${o};
let in_y = ${s};
if( in_y < 0.0 || in_y > ${e} ) {
setOutputFlat(index, uniforms.extrapolationValue);
return;
}
let in_x = ${i};
if( in_x < 0.0 || in_x > ${t} ) {
setOutputFlat(index, uniforms.extrapolationValue);
return;
}
let sourceFracIndexCR = vec2<f32>(in_x,in_y);
if(${this.methodId} == 1) {
// Compute the four integer indices.
let sourceFloorCR = vec2<i32>(sourceFracIndexCR);
let sourceCeilCR = vec2<i32>(ceil(sourceFracIndexCR));
let topLeft = getImage(bInd, sourceFloorCR.y, sourceFloorCR.x, d);
let bottomLeft = getImage(bInd, sourceCeilCR.y, sourceFloorCR.x, d);
let topRight = getImage(bInd, sourceFloorCR.y, sourceCeilCR.x, d);
let bottomRight = getImage(bInd, sourceCeilCR.y, sourceCeilCR.x, d);
let fracCR = sourceFracIndexCR - vec2<f32>(sourceFloorCR);
let top = topLeft + (topRight - topLeft) * fracCR.x;
let bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;
let newValue = top + (bottom - top) * fracCR.y;
setOutputFlat(index, newValue);
} else {
// Compute the coordinators of nearest neighbor point.
let sourceNearestCR = vec2<i32>(floor(
sourceFracIndexCR + vec2<f32>(0.5,0.5)));
let newValue = getImage(
bInd, sourceNearestCR.y, sourceNearestCR.x, d);
setOutputFlat(index, newValue);
}
}
}
`}},Pue=e=>{let{inputs:t,backend:n,attrs:r}=e,{image:s,boxes:a,boxInd:o}=t,{cropSize:i,method:u,extrapolationValue:l}=r,c=new Rue(s.shape[3],a.shape,i,u),d=[{type:"float32",data:[l]}];return n.runWebGPUProgram(c,[s,a,o],"float32",d)},Oue={kernelName:jo,backendName:"webgpu",kernelFunc:Pue},Mue=class{constructor(e,t){this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.uniforms="blockSize : i32;",this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`depthToSpace_${t}`,this.dataFormat=t}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let b = coords[0];
let h = ${this.getHeightCoordString()};
let w = ${this.getWidthCoordString()};
let d = ${this.getDepthCoordString()};
let in_h = h / uniforms.blockSize;
let offset_h = h % uniforms.blockSize;
let in_w = w / uniforms.blockSize;
let offset_w = w % uniforms.blockSize;
let offset_d = (offset_h * uniforms.blockSize + offset_w) *
${this.getOutputDepthSize()};
let in_d = d + offset_d;
let rlt = ${this.getInputSamplingString()};
setOutputFlat(index, rlt);
}
}`}getHeightCoordString(){return this.dataFormat==="NHWC"?"coords[1]":"coords[2]"}getWidthCoordString(){return this.dataFormat==="NHWC"?"coords[2]":"coords[3]"}getDepthCoordString(){return this.dataFormat==="NHWC"?"coords[3]":"coords[1]"}getOutputDepthSize(){return this.dataFormat==="NHWC"?"uniforms.outShape[3]":"uniforms.outShape[1]"}getInputSamplingString(){return this.dataFormat==="NHWC"?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"}};function Lue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockSize:a,dataFormat:o}=r,i=s.shape[0],u=o==="NHWC"?s.shape[1]:s.shape[2],l=o==="NHWC"?s.shape[2]:s.shape[3],c=o==="NHWC"?s.shape[3]:s.shape[1],d=u*a,p=l*a,h=c/(a*a),f=o==="NHWC"?[i,d,p,h]:[i,h,d,p],m=[{type:"int32",data:[a]}],g=new Mue(f,o);return n.runWebGPUProgram(g,[s],s.dtype,m)}var Bue={kernelName:qo,backendName:"webgpu",kernelFunc:Lue},PE=class{constructor(e,t=!1,n=null,r=!1){this.variableNames=["x","W"],this.uniforms="pad : vec2<i32>; stride : vec2<i32>; dilation : vec2<i32>; inDims : vec2<i32>;",this.workGroupSize=[4,4,4],this.isVec4=!0,this.outputShape=e.outShape,this.dispatchLayout={x:[0,1],y:[2],z:[3]},this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[1,4,4]),w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivation=r,this.shaderKey=`depthwise3x3_${n}`}getUserCode(){let e="",t="";if(this.activation){let s=Gs(this.activation,this.isVec4);this.hasPreluActivation?e=`fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${s}
}`:e=`
fn activation(a : vec4<f32>, outCoord : vec4<i32>) -> vec4<f32> {
${s}
}
`,t="dotProd[i] = activation(dotProd[i], coords);"}let n=this.addBias?"dotProd[i] = dotProd[i] + getBiasAtOutCoordsByCoords(coords);":"";return`
${e}
${zm()}
fn main([[builtin(global_invocation_id)]] globalId: vec3<u32>) {
let batch = 0;
let r = i32(globalId.x);
let c = i32(globalId.y) * 4;
let d2 = i32(globalId.z) * 4;
let xRCCorner = vec2<i32>(r, c) * uniforms.stride - uniforms.pad;
let d1 = d2;
let q = 0;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
var wVals : array<vec4<f32>, 9>;
wVals[0] = getW(0, 0, d1, q);
wVals[1] = getW(0, 1, d1, q);
wVals[2] = getW(0, 2, d1, q);
wVals[3] = getW(1, 0, d1, q);
wVals[4] = getW(1, 1, d1, q);
wVals[5] = getW(1, 2, d1, q);
wVals[6] = getW(2, 0, d1, q);
wVals[7] = getW(2, 1, d1, q);
wVals[8] = getW(2, 2, d1, q);
var xVals : array<array<vec4<f32>, 6>, 3>;
for (var wR = 0; wR < 3; wR = wR + 1) {
let xR = xRCorner + wR * uniforms.dilation[0];
for (var wC = 0; wC < 6; wC = wC + 1) {
let xC = xCCorner + wC * uniforms.dilation[1];
if (xR < 0 || xR >= uniforms.inDims[0] || xC < 0 || xC >= uniforms.inDims[1]) {
xVals[wR][wC] = vec4<f32>(0.0);
} else {
xVals[wR][wC] = getX(batch, xR, xC, d1);
}
}
}
var dotProd : array<vec4<f32>, 4>;
dotProd[0] = vec4<f32>(0.0);
dotProd[1] = vec4<f32>(0.0);
dotProd[2] = vec4<f32>(0.0);
dotProd[3] = vec4<f32>(0.0);
for (var wR = 0; wR < 3; wR = wR + 1) {
for (var wC = 0; wC < 3; wC = wC + 1) {
let indexW = wR * 3 + wC;
dotProd[0] = dotProd[0] + xVals[wR][0 + wC] * wVals[indexW];
dotProd[1] = dotProd[1] + xVals[wR][1 + wC] * wVals[indexW];
dotProd[2] = dotProd[2] + xVals[wR][2 + wC] * wVals[indexW];
dotProd[3] = dotProd[3] + xVals[wR][3 + wC] * wVals[indexW];
}
}
for (var i = 0; i < 4; i = i + 1) {
let coords = vec4<i32>(batch, r, c + i, d2);
if (coordsInBounds4D(coords, uniforms.outShape)) {
${n}
${t}
setOutput(coords[0], coords[1], coords[2], coords[3], dotProd[i]);
}
}
}
`}},OE=class{constructor(e,t=!1,n=null,r=!1){this.variableNames=["x","W"],this.uniforms=`pad : vec2<i32>; stride : vec2<i32>; dilation : vec2<i32>;
inDims : vec2<i32>; filterHeight : i32; filterWidth : i32;
channelMul : i32;`,this.workGroupSize=[256,1,1],this.outputShape=e.outShape,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),w.assert(e.dataFormat==="channelsLast",()=>"TODO: NCHW is unimplemented"),t&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.convInfo=e,this.addBias=t,this.activation=n,this.hasPreluActivation=r,this.shaderKey=`depthwise_${this.activation}`}getUserCode(){let e="",t="";if(this.activation){let s=Gs(this.activation,!1);this.hasPreluActivation?e=`fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
let b = getPreluActivationWeightsAtOutCoordsByCoords(outCoord);
${s}
}`:e=`
fn activation(a : f32, outCoord : vec4<i32>) -> f32 {
${s}
}
`,t="dotProd = activation(dotProd, coords);"}let n=this.addBias?"dotProd = dotProd + getBiasAtOutCoordsByCoords(coords);":"";return`
${e}
fn writeResult(batch : i32, row : i32, col : i32, chan : i32,
value : f32) {
let coord = vec4<i32>(batch, row, col, chan);
if (coordsInBounds4D(coord, uniforms.outShape)) {
setOutput(batch, row, col, chan, value);
}
}
${hk()} {
let coords = getOutputCoordsWithFlatDispatchLayout(globalId,
localId, numWorkgroups);
let batch = coords[0];
let xRCCorner = vec2<i32>(coords.yz) * uniforms.stride - uniforms.pad;
let d2 = coords[3];
let d1 = d2 / uniforms.channelMul;
let q = d2 - d1 * uniforms.channelMul;
let inputRowStart = xRCCorner.x;
let inputColStart = xRCCorner.y;
let inputRowEnd = inputRowStart + uniforms.filterHeight *
uniforms.dilation[0];
let inputColEnd = inputColStart + uniforms.filterWidth *
uniforms.dilation[1];
// Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
// ? = to be determined. : = across all values in that axis.
var dotProd = 0.0;
// Extract if checking out of for loop for performance.
if (inputRowStart >= 0 && inputColStart >= 0 &&
inputRowEnd < uniforms.inDims[0] &&
inputColEnd < uniforms.inDims[1]) {
// Here using a constant value |this.convInfo.filterHeight| instead
// of uniform value is in order to loop unrolling.
for (var wR = 0; wR < uniforms.filterHeight; wR = wR + 1) {
let xR = inputRowStart + wR * uniforms.dilation[0];
for (var wC = 0; wC < uniforms.filterWidth; wC = wC + 1) {
let xC = inputColStart + wC * uniforms.dilation[1];
let xVal = getX(batch, xR, xC, d1);
let wVal = getW(wR, wC, d1, q);
dotProd = dotProd + xVal * wVal;
}
}
} else {
for (var wR = 0; wR < uniforms.filterHeight; wR = wR + 1) {
let xR = inputRowStart + wR * uniforms.dilation[0];
if (xR < 0 || xR >= uniforms.inDims[0]) {
continue;
}
for (var wC = 0; wC < uniforms.filterWidth; wC = wC + 1) {
let xC = inputColStart + wC * uniforms.dilation[1];
if (xC < 0 || xC >= uniforms.inDims[1]) {
continue;
}
let xVal = getX(batch, xR, xC, d1);
let wVal = getW(wR, wC, d1, q);
dotProd = dotProd + xVal * wVal;
}
}
}
${n}
${t}
writeResult(batch, coords[1], coords[2], d2, dotProd);
}
`}};function zue(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a}=t,{strides:o,pad:i,dilations:u,dimRoundingMode:l}=r,c=u;c==null&&(c=[1,1]);let d=N.computeConv2DInfo(s.shape,a.shape,o,c,i,l,!0),p=[{type:"int32",data:[d.padInfo.top,d.padInfo.left]},{type:"int32",data:[d.strideHeight,d.strideWidth]},{type:"int32",data:[d.dilationHeight,d.dilationWidth]},{type:"int32",data:[d.inHeight,d.inWidth]}],h;return d.batchSize===1&&d.inHeight===d.outHeight&&d.inWidth===d.outWidth&&d.strideHeight===1&&d.strideWidth===1&&d.filterHeight===d.filterWidth&&d.inChannels===d.outChannels&&d.filterHeight===3&&d.inChannels%4==0?h=new PE(d):(h=new OE(d),p.push({type:"int32",data:[d.filterHeight]},{type:"int32",data:[d.filterWidth]},{type:"int32",data:[d.outChannels/d.inChannels]})),n.runWebGPUProgram(h,[s,a],s.dtype,p)}var Wue={kernelName:xa,backendName:"webgpu",kernelFunc:zue},ME=Rn({opSnippet:Mt.MUL,cpuKernelImpl:Sie,supportsComplex:!0}),Vue={kernelName:Ma,backendName:"webgpu",kernelFunc:ME},Uue=class{constructor(e,t){this.workGroupSize=[64,1,1],this.variableNames=["x"],this.uniforms="reduceSize : i32;",this.size=!0,this.inputShape=[e.batchSize,e.inSize];let[n]=N.computeOutAndReduceShapes(this.inputShape,[1]);this.outputShape=n.length===0?[1]:n,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,[1,1,1]),this.reduceType=t,this.shaderKey=`reduce_${t}`}getUserCode(){let e="",t="0.0";this.reduceType==="min"||this.reduceType==="max"?(e=`
if (isNanCustom(candidate)) {
bestValue = uniforms.NAN;
} elseif (!isNanCustom(bestValue) && candidate ${this.reduceType==="min"?"<":">"} bestValue)
{ bestValue = candidate; }`,t="f32(x.numbers[offset])"):this.reduceType==="sum"||this.reduceType==="mean"?e=" bestValue = bestValue + candidate; ":this.reduceType==="prod"&&(e=" bestValue = bestValue * candidate; ",t="1.0");let n=this.reduceType==="mean"?"setOutputFlat(outputIndex, bestValue / f32(uniforms.reduceSize));":"setOutputFlat(outputIndex, bestValue);";return`
fn DIV_CEIL(a : u32, b : u32) -> u32 {
return ((a - 1u) / b + 1u);
}
${`
var<workgroup> xBestValues : array<f32, ${this.workGroupSize[0]}>;
`}
fn getOffset(outputIndex : i32) -> i32 {
let outputCoords = getCoordsFromFlatIndex(outputIndex);
let offset = ${this.outputShape.length===1?"outputCoords":"outputCoords[0]"} * uniforms.reduceSize;
return offset;
}
${Ue()}
let outputIndex = index / i32(workGroupSizeX);
let offset = getOffset(outputIndex);
var bestValue = ${t};
let Length = uniforms.reduceSize;
let WorkPerThread = DIV_CEIL(u32(Length), workGroupSizeX);
for (var k = i32(localId.x); k < Length && outputIndex < uniforms.size;
k = k + i32(workGroupSizeX)) {
let candidate = f32(x.numbers[offset + k]);
${e}
}
xBestValues[localId.x] = bestValue;
workgroupBarrier();
var reduceSize = min(u32(Length), workGroupSizeX);
for (var currentSize = reduceSize / 2u; reduceSize > 1u;
currentSize = reduceSize / 2u) {
let interval = DIV_CEIL(reduceSize, 2u);
if (localId.x < currentSize) {
let candidate = xBestValues[localId.x + interval];
${e}
xBestValues[localId.x] = bestValue;
}
reduceSize = interval;
workgroupBarrier();
}
if (localId.x == 0u && outputIndex < uniforms.size) {
${n}
}
}
`}};function mp(e,t,n,r,s){let a=e.shape.length,o=[],i=w.parseAxisParam(t,e.shape),u=i,l=N.getAxesPermutation(u,a),c=e;l!=null&&(c=cu({inputs:{x:e},attrs:{perm:l},backend:s}),u=N.getInnerMostAxes(u.length,a),o.push(c)),N.assertAxesAreInnerMostDims(r,u,a);let[d,p]=N.computeOutAndReduceShapes(c.shape,u),h=d;n&&(h=N.expandShapeToKeepDim(d,i));let f;if((r==="max"||r==="prod")&&s.shouldExecuteOnCPU([c])){let m=s.tensorMap.get(c.dataId).values;switch(r){case"max":let g=wie(m,w.sizeFromShape(p),h,e.dtype);f=s.makeTensorInfo(h,e.dtype,g);break;case"prod":let{outVals:b,outShape:y,outDtype:v}=Nie(c.shape,c.dtype,m,u);f=s.makeTensorInfo(y,v,b);break;default:throw new Error(`${r} CPU implementation is not yet supported.`)}}else{let m=w.sizeFromShape(p),b=w.sizeFromShape(c.shape)/m,y={windowSize:m,inSize:m,batchSize:b,outSize:1},v=r==="mean"?"float32":yd(e.dtype),x=[{type:"int32",data:[m]}],k=new Uue(y,r),T=s.runWebGPUProgram(k,[c],v,x);o.push(T),f=We({inputs:{x:T},attrs:{shape:h},backend:s})}return o.forEach(m=>s.disposeData(m.dataId)),f}function Sk(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r;return mp(s,a,o,"sum",n)}var Gue={kernelName:Ka,backendName:"webgpu",kernelFunc:Sk};function Hue(e){let{inputs:t,backend:n,attrs:r}=e,{equation:s}=r,a=t,{allDims:o,summedDims:i,idDims:u}=N.decodeEinsumEquation(s,a.length);N.checkEinsumDimSizes(o.length,u,a);let{path:l,steps:c}=N.getEinsumComputePath(i,u),d=c.length,p=null,h=o.length,f=[];for(let m=0;m<d;++m){for(let g of c[m]){let{permutationIndices:b,expandDims:y}=N.getEinsumPermutation(h,u[g]),v;N.isIdentityPermutation(b)?v=a[g]:(v=cu({inputs:{x:a[g]},backend:n,attrs:{perm:b}}),f.push(v));let x=v.shape.slice();for(let k=0;k<y.length;++k)x.splice(y[k],0,1);w.arraysEqual(v.shape,x)||(v=We({inputs:{x:v},backend:n,attrs:{shape:x}}),f.push(v)),p===null?p=v:(p=ME({inputs:{a:v,b:p},backend:n}),f.push(p))}m<d-1&&(l[m]>=0&&(p=Sk({inputs:{x:p},backend:n,attrs:{axis:l[m]-(o.length-h),keepDims:!1}}),f.push(p)),h--)}for(let m of f)m!==p&&n.disposeData(m.dataId);return p}var jue={kernelName:Zl,backendName:"webgpu",kernelFunc:Hue},que=fn({opType:bt.ELU}),Kue={kernelName:ka,backendName:"webgpu",kernelFunc:que},Xue=Rn({opSnippet:Mt.EQUAL,dtype:"bool",cpuKernelImpl:lie}),Yue={kernelName:Ko,backendName:"webgpu",kernelFunc:Xue},LE=fn({opType:bt.EXP,cpuKernelImpl:die,dtype:"float32"}),Que={kernelName:Ia,backendName:"webgpu",kernelFunc:LE};function Ck(e){let{inputs:t,attrs:n,backend:r}=e,{dim:s}=n,{input:a}=t,o=a.shape.length,i=a.shape.slice(),u=s;return s<0&&(w.assert(-(o+1)<=s,()=>`Axis must be in the interval [${-(o+1)}, ${o}]`),u=o+s+1),i.splice(u,0,1),We({inputs:{x:a},backend:r,attrs:{shape:i}})}var Zue={kernelName:Xo,backendName:"webgpu",kernelFunc:Ck},Jue=fn({opType:bt.EXPM1,cpuKernelImpl:pie}),ece={kernelName:Yo,backendName:"webgpu",kernelFunc:Jue},tce=class{constructor(e){this.variableNames=[],this.outputShape=[],this.uniforms="value : f32;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="fill"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
setOutputFlat(index, uniforms.value);
}
}
`}};function al(e){let{backend:t,attrs:n}=e,{shape:r,value:s}=n,{dtype:a}=n;if(a=a||w.inferDtype(s),a==="string"){let o=w.getArrayFromDType(a,w.sizeFromShape(r));return o.fill(s),t.makeTensorInfo(r,a,o)}else{let o=new tce(r),i=[{type:"float32",data:[s]}];return t.runWebGPUProgram(o,[],a,i)}}var nce={kernelName:Ju,backendName:"webgpu",kernelFunc:al},rce=class{constructor(e){this.outputShape=[],this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="flipLeftRight"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let coordX = uniforms.xShape[2] - coords[2] - 1;
let outputValue = getX(coords[0], coords[1], coordX, coords[3]);
setOutputFlat(index, outputValue);
}
}
`}},sce={kernelName:Qo,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{image:n}=e,r=t,s=new rce(n.shape);return r.runWebGPUProgram(s,[n],n.dtype)}},ace=fn({opType:bt.FLOOR,cpuKernelImpl:hie}),oce={kernelName:Sa,backendName:"webgpu",kernelFunc:ace},ice=Rn({opSnippet:Mt.INT_DIV,dtype:"int32"}),uce={kernelName:Ca,backendName:"webgpu",kernelFunc:ice},cce=(e,t,n,r,s)=>{let a=[r,...n];return s&&a.push(s),e.createBindGroup({layout:t,entries:a.map((o,i)=>({binding:i,resource:o}))})},BE=(e,t,n,r,s,a=!1)=>{let o={dtype:s.dtype,shape:s.shape},i=zae(r,o,t,a),u=e.createShaderModule({code:i});return e.createComputePipeline({layout:n,compute:{module:u,entryPoint:"main"}})};function zE(e,t,n,r="",s=""){return e.shaderKey+"_"+(e.workGroupSize?e.workGroupSize.join(","):"")+t.map(o=>o.length).join(",")+n.join(",")+e.variableNames.join(",")+r+s}function WE(e){let{externalImage:t,backend:n,attrs:r,outShape:s,useImport:a}=e,{numChannels:o}=r,i=w.sizeFromShape(s),u=w.computeStrides(s),l=n.makeTensorInfo(s,"int32"),c=n.getFromPixelsProgram(a?"import":"copyExternal");c.updateOutputShape(s);let d=[l.shape],p=[l.dtype,a?"import":"copyExternal"],h=zE(c,d,p),f=c.getLayout(n.device),m=n.getAndSavePipeline(h,()=>BE(n.device,c,f.pipelineLayout,[],l,!0));c.setPipeline(m),a||n.queue.copyExternalImageToTexture({source:t,origin:{x:0,y:0}},{texture:c.makeInputTexture(n.device,s[1],s[0])},[s[1],s[0]]);let g=n.tensorMap.get(l.dataId);g.bufferInfo.buffer=n.acquireBuffer(g.bufferInfo.byteSize);let b=[i,o,...u,...c.dispatch];c.setUniform(n.device,b);let y;if(a){let v={source:t};y=n.device.importExternalTexture(v)}else y=c.inputTexture.createView();return n.runFromPixelsProgram(c,g.bufferInfo.buffer,f,y,l.dataId),l}var lce={kernelName:ld,backendName:"webgpu",kernelFunc:dce},ol;function dce(e){let{inputs:t,backend:n,attrs:r}=e,{pixels:s}=t,{numChannels:a}=r;if(s==null)throw new Error("pixels passed to tf.browser.fromPixels() can not be null");let o=typeof HTMLVideoElement!="undefined"&&s instanceof HTMLVideoElement,i=typeof HTMLImageElement!="undefined"&&s instanceof HTMLImageElement,u=typeof HTMLCanvasElement!="undefined"&&s instanceof HTMLCanvasElement||typeof OffscreenCanvas!="undefined"&&s instanceof OffscreenCanvas,l=typeof ImageBitmap!="undefined"&&s instanceof ImageBitmap,[c,d]=o?[s.videoWidth,s.videoHeight]:[s.width,s.height],p=[d,c,a];if(X().getBool("WEBGPU_USE_IMPORT")&&o)return WE({externalImage:s,backend:n,attrs:r,outShape:p,useImport:!0});if((o||i)&&(ol==null&&(ol=document.createElement("canvas").getContext("2d")),ol.canvas.width=c,ol.canvas.height=d,ol.drawImage(s,0,0,c,d),s=ol.canvas),l||u||o||i)return WE({externalImage:s,backend:n,attrs:r,outShape:p,useImport:!1});let h=s.data,f=h;if(a!=null&&a!==4){f=new Uint8Array(s.width*s.height*a);let b=h.length,y=0;for(let v=0;v<b;v++)v%4<a&&(f[y++]=h[v])}let m=n.makeTensorInfo(p,"int32"),g=n.tensorMap.get(m.dataId);return g.values=new Int32Array(f),n.maybeReleaseBuffer(m.dataId),n.uploadToGPU(m.dataId),m}var pce=class{constructor(e,t,n,r,s){this.uniforms="varianceEpsilon : f32;",this.workGroupSize=[128,1,1],this.size=!0,this.variableNames=["x","mean","variance"],N.assertAndGetBroadcastShape(e,t),N.assertAndGetBroadcastShape(e,n),this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),r!=null&&(N.assertAndGetBroadcastShape(e,r),this.variableNames.push("offset")),s!=null&&(N.assertAndGetBroadcastShape(e,s),this.variableNames.push("scale")),this.offsetShape=r,this.scaleShape=s,this.shaderKey="batchNorm"}getUserCode(){let e="0.0";this.offsetShape!=null&&(e="getOffsetAtOutCoordsByGlobalIndex(index)");let t="1.0";return this.scaleShape!=null&&(t="getScaleAtOutCoordsByGlobalIndex(index)"),`
${Ue()}
if (index < uniforms.size)
{
let xValue = getXAtOutCoordsByGlobalIndex(index);
let meanValue = getMeanAtOutCoordsByGlobalIndex(index);
let varianValue = getVarianceAtOutCoordsByGlobalIndex(index);
let offsetValue = ${e};
let scaleValue = ${t};
let inv = scaleValue * inverseSqrt(varianValue + f32(uniforms.varianceEpsilon));
setOutputFlat(index,dot(vec3<f32>(xValue, -meanValue, offsetValue), vec3<f32>(inv, inv, 1.0)));
}
}
`}},hce={kernelName:Ta,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:r,scale:s,offset:a,mean:o,variance:i}=e,{varianceEpsilon:u}=t,l=n,c=[r,o,i],d=null;a!=null&&(d=a.shape,c.push(a));let p=null;s!=null&&(p=s.shape,c.push(s));let h=new pce(r.shape,o.shape,i.shape,d,p),f=[{type:"float32",data:[u]}];return l.runWebGPUProgram(h,c,r.dtype,f)}};function fce(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a,bias:o,preluActivationWeights:i}=t,{strides:u,pad:l,dataFormat:c,dilations:d,dimRoundingMode:p,activation:h,leakyreluAlpha:f}=r,m=N.convertConv2DDataFormat(c),g=N.computeConv2DInfo(s.shape,a.shape,u,d,l,p,!1,m),b=o!=null,y=i!=null,v;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"))return $E({x:s,filter:a,convInfo:g,backend:n,bias:o,activation:h,preluActivationWeights:i,leakyreluAlpha:f});let x=X().getBool("WEBGPU_USE_NAIVE_CONV2D"),k=g.inChannels%4==0&&g.outChannels%4==0,T=[g.padInfo.top,g.padInfo.left],C=[{type:"int32",data:[g.filterHeight,g.filterWidth]},{type:"int32",data:[...T]},{type:"int32",data:[g.strideHeight,g.strideWidth]},{type:"int32",data:[g.dilationHeight,g.dilationWidth]}];if(x)v=new RE(g,b,h,y);else{k?v=new FE(g,b,h,y):v=new DE(g,b,h,y);let F=g.outShape[1]*g.outShape[2],O=g.outShape[3],D=g.filterHeight*g.filterWidth*g.inShape[3];C.push({type:"int32",data:[F]},{type:"int32",data:[O]},{type:"int32",data:[D]})}let E=[s,a];return b&&E.push(o),y&&E.push(i),n.runWebGPUProgram(v,E,s.dtype,C)}var mce={kernelName:no,backendName:"webgpu",kernelFunc:fce};function gce(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,filter:a,bias:o,preluActivationWeights:i}=t,{strides:u,pad:l,dilations:c,dimRoundingMode:d,activation:p}=r,h=c;h==null&&(h=[1,1]),w.assert(N.eitherStridesOrDilationsAreOne(u,h),()=>`Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides ${u} and dilations '${h}'`);let f=N.computeConv2DInfo(s.shape,a.shape,u,h,l,d,!0),m=[s,a],g=o!=null,b=i!=null;g&&m.push(o),b&&m.push(i);let y=[{type:"int32",data:[f.padInfo.top,f.padInfo.left]},{type:"int32",data:[f.strideHeight,f.strideWidth]},{type:"int32",data:[f.dilationHeight,f.dilationWidth]},{type:"int32",data:[f.inHeight,f.inWidth]}],v;return f.batchSize===1&&f.inHeight===f.outHeight&&f.inWidth===f.outWidth&&f.strideHeight===1&&f.strideWidth===1&&f.filterHeight===f.filterWidth&&f.inChannels===f.outChannels&&f.filterHeight===3&&f.inChannels%4==0?v=new PE(f,g,p,b):(v=new OE(f,g,p,b),y.push({type:"int32",data:[f.filterHeight]},{type:"int32",data:[f.filterWidth]},{type:"int32",data:[f.outChannels/f.inChannels]})),n.runWebGPUProgram(v,m,"float32",y)}var bce={kernelName:ro,backendName:"webgpu",kernelFunc:gce},yce=class{constructor(e,t){this.variableNames=["A","indices"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey=`gathernd_${e}`,this.sliceDim=e,this.uniforms=`sliceDim : i32; strides : ${on(e)};`}getUserCode(){let e;return this.sliceDim>1?e="uniforms.strides[j]":e="uniforms.strides",`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
var flattenIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexTemp = i32(round(getIndices(coords[0], j)));
let strideNum = ${e};
flattenIndex = flattenIndex + indexTemp * strideNum;
}
setOutputFlat(index, getA(flattenIndex, coords[1]));
}
}
`}};function vce(e){let{inputs:t,backend:n}=e,{params:r,indices:s}=t,a=s.shape,o=a[a.length-1],i=w.sizeFromShape(r.shape),[u,l,c,d]=N.prepareAndValidate(r,s),p=We({inputs:{x:s},backend:n,attrs:{shape:[l,o]}}),h=We({inputs:{x:r},backend:n,attrs:{shape:[w.sizeFromShape(r.shape)/c,c]}});if(n.shouldExecuteOnCPU([r,s])||r.dtype==="string"){let y=n.readSync(s.dataId),v=n.bufferSync(r),x=fie(y,v,r.dtype,l,o,c,d,r.shape,i);return n.makeTensorInfo(u,r.dtype,x.values)}let f=new yce(o,[l,c]),m=[{type:"int32",data:[o]},{type:"int32",data:d}],g=n.runWebGPUProgram(f,[h,p],h.dtype,m),b=We({inputs:{x:g},backend:n,attrs:{shape:u}});return n.disposeData(p.dataId),n.disposeData(h.dataId),n.disposeData(g.dataId),b}var xce={kernelName:Jo,backendName:"webgpu",kernelFunc:vce},wce=class{constructor(e,t){this.variableNames=["A","indices"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e.slice(),this.aShape=e,this.outputShape=t,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="gather"}getUserCode(){let e=kce(this.aShape,"i32");return`
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromFlatIndex(index);
setOutputFlat(index, getA(${e}));
}
}
`}};function kce(e,t="int"){let n=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[];for(let s=0;s<e.length;s++)s===2?r.push(`${t}(getIndices(resRC.x, resRC.z))`):r.push(`${n[s]}`);return r.join()}function VE(e){let{inputs:t,backend:n,attrs:r}=e,{x:s,indices:a}=t,{axis:o,batchDims:i}=r,u=w.parseAxisParam(o,s.shape)[0],l=N.segment_util.collectGatherOpShapeInfo(s,a,u,i),c=w.sizeFromShape(a.shape),d=[],p=We({inputs:{x:s},backend:n,attrs:{shape:[l.batchSize,l.outerSize,l.dimSize,l.sliceSize]}}),h=We({inputs:{x:a},backend:n,attrs:{shape:[l.batchSize,c/l.batchSize]}});d.push(p),d.push(h);let f=[l.batchSize,l.outerSize,c/l.batchSize,l.sliceSize];if(n.shouldExecuteOnCPU([s,a])){let v=n.tensorMap.get(h.dataId).values,x=$e(h.shape,h.dtype,v),T=n.tensorMap.get(p.dataId).values,C=$e(p.shape,p.dtype,T),E=mie(C,x,f);return d.forEach(F=>n.disposeData(F.dataId)),n.makeTensorInfo(l.outputShape,E.dtype,E.values)}let m=new wce(p.shape,f),g=n.runWebGPUProgram(m,[p,h],p.dtype);d.push(g);let b=We({inputs:{x:g},backend:n,attrs:{shape:l.outputShape}});return d.forEach(y=>n.disposeData(y.dataId)),b}var Ice={kernelName:Zo,backendName:"webgpu",kernelFunc:VE},Sce=Rn({opSnippet:Mt.GREATER,cpuKernelImpl:bie,dtype:"bool"}),Cce={kernelName:ei,backendName:"webgpu",kernelFunc:Sce},Tce=Rn({opSnippet:Mt.GREATER_EQUAL,dtype:"bool",cpuKernelImpl:gie}),Nce={kernelName:Na,backendName:"webgpu",kernelFunc:Tce},_ce=Rn({opSnippet:Mt.LESS,dtype:"bool",cpuKernelImpl:vie}),Ece={kernelName:ni,backendName:"webgpu",kernelFunc:_ce},Ace=Rn({opSnippet:Mt.LESS_EQUAL,dtype:"bool",cpuKernelImpl:yie}),$ce={kernelName:ri,backendName:"webgpu",kernelFunc:Ace},Fce=fn({opType:bt.LOG,cpuKernelImpl:xie}),Dce={kernelName:Ea,backendName:"webgpu",kernelFunc:Fce},Rce=Rn({opSnippet:Mt.LOGICAL_AND,dtype:"bool"}),Pce={kernelName:si,backendName:"webgpu",kernelFunc:Rce},Oce=fn({opType:bt.LOGICAL_NOT}),Mce={kernelName:sc,backendName:"webgpu",kernelFunc:Oce};function UE(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reductionIndices:a,keepDims:o}=r;return mp(s,a,o,"max",n)}var Lce={kernelName:Aa,backendName:"webgpu",kernelFunc:UE},Bce=Rn({opSnippet:Mt.MAX,cpuKernelImpl:kie}),zce={kernelName:$a,backendName:"webgpu",kernelFunc:Bce};function Wce(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{filterSize:a,strides:o,pad:i,dimRoundingMode:u}=r,l=1,c=N.computePool2DInfo(s.shape,a,o,l,i,u),d,p=[];if(c.filterHeight===1&&c.filterWidth===1){if(w.arraysEqual(c.inShape,c.outShape))return Lr({inputs:{x:s},backend:n});d=new _E(c),p.push({type:"int32",data:[c.strideHeight,c.strideWidth]})}else d=new NE(c,"max"),p.push({type:"int32",data:[c.strideHeight,c.strideWidth]},{type:"int32",data:[c.padInfo.top,c.padInfo.left]},{type:"int32",data:[c.dilationHeight,c.dilationWidth]},{type:"int32",data:[c.inHeight,c.inWidth]},{type:"int32",data:[c.effectiveFilterHeight,c.effectiveFilterWidth]});return n.runWebGPUProgram(d,[s],s.dtype,p)}var Vce={kernelName:Fa,backendName:"webgpu",kernelFunc:Wce};function Uce(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{keepDims:a,axis:o}=r;return mp(s,o,a,"mean",n)}var Gce={kernelName:Da,backendName:"webgpu",kernelFunc:Uce};function Hce(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r;return mp(s,a,o,"min",n)}var jce={kernelName:Ra,backendName:"webgpu",kernelFunc:Hce},qce=Rn({opSnippet:Mt.MIN,cpuKernelImpl:Iie}),Kce={kernelName:Pa,backendName:"webgpu",kernelFunc:qce},Xce=class{constructor(e,t,n){this.uniforms="",this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t.map((r,s)=>r[0]+e[s]+r[1]),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.xShape=e,t.map((r,s)=>{this.uniforms+=` pad${s} : vec2<i32>;`}),this.offset=n==="reflect"?0:1,this.shaderKey=`mirrorPad_${n}`}getUserCode(){let e=this.xShape.length,t=this.xShape.map((u,l)=>`uniforms.pad${l}[0]`).join(","),n=this.xShape.map((u,l)=>`uniforms.pad${l}[0] + uniforms.xShape${e>1?`[${l}]`:""}`).join(","),r=e===1?"start":"start[i]",s=e===1?"end":"end[i]",a=e===1?"outC":"outC[i]",o=on(e),i=e>1?["coords[0]","coords[1]","coords[
${Ue()}
if (index < uniforms.size) {
let start = ${o}(${t});
let end = ${o}(${n});
var outC = getCoordsFromFlatIndex(index);
for (var i = 0; i < ${e}; i = i + 1) {
if (${a} < ${r}) {
${a} = ${r} * 2 - ${a} - ${this.offset};
} elseif(${a} >= ${s}) {
${a} = (${s} - 1) * 2 - ${a} + ${this.offset};
}
}
let coords = outC - start;
setOutputFlat(index, getX(${i}));
}
}
`}},Yce={kernelName:Oa,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{x:r}=e,{paddings:s,mode:a}=t,o=n,i=s.map(c=>({type:"int32",data:[c[0],c[1]]})),u=new Xce(r.shape,s,a);return o.runWebGPUProgram(u,[r],r.dtype,i)}};function Qce(e){let{inputs:t,backend:n}=e,{x:r}=t;if(n.shouldExecuteOnCPU([r])){let a=n.tensorMap.get(r.dataId),[o,i]=Cie(a.values,r.shape,r.dtype);return n.makeTensorInfo(i,r.dtype,o)}let s=new Wm(r.shape,bt.NEG);return n.runWebGPUProgram(s,[r],r.dtype)}var Zce={kernelName:ai,backendName:"webgpu",kernelFunc:Qce};function Jce(e){console.warn("tf.nonMaxSuppression() in webgpu locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u}=r,l=n.readSync(s.dataId),c=n.readSync(a.dataId),{selectedIndices:d}=Dr.nonMaxSuppressionV3Impl(l,c,o,i,u);return n.makeTensorInfo([d.length],"int32",new Int32Array(d))}var ele={kernelName:ii,backendName:"webgpu",kernelFunc:Jce};function tle(e){console.warn("tf.nonMaxSuppression() in webgpu locks the UI thread. Call tf.nonMaxSuppressionAsync() instead");let{inputs:t,backend:n,attrs:r}=e,{boxes:s,scores:a}=t,{maxOutputSize:o,iouThreshold:i,scoreThreshold:u,softNmsSigma:l}=r,c=n.readSync(s.dataId),d=n.readSync(a.dataId),p=o,h=i,f=u,m=l,{selectedIndices:g,selectedScores:b}=Dr.nonMaxSuppressionV5Impl(c,d,p,h,f,m);return[n.makeTensorInfo([g.length],"int32",new Int32Array(g)),n.makeTensorInfo([b.length],"float32",new Float32Array(b))]}var nle={kernelName:ui,backendName:"webgpu",kernelFunc:tle};function Um(e){let{inputs:t,backend:n}=e,{x:r}=t;if(r.dtype==="complex64"){let s=fp({inputs:{input:r},backend:n}),a=Um({inputs:{x:s},backend:n}),o=Vm({inputs:{input:r},backend:n}),i=Um({inputs:{x:o},backend:n}),u=rl({inputs:{real:a,imag:i},backend:n});return n.disposeData(s.dataId),n.disposeData(a.dataId),n.disposeData(o.dataId),n.disposeData(i.dataId),u}else return al({attrs:{shape:r.shape,dtype:r.dtype,value:r.dtype==="string"?"":0},backend:n})}var rle={kernelName:Ni,backendName:"webgpu",kernelFunc:Um};function GE(e){let{inputs:t,backend:n}=e,{x:r}=t;if(r.dtype==="string")throw new Error("onesLike is not supported under string dtype");if(r.dtype==="complex64"){let s=fp({inputs:{input:r},backend:n}),a=GE({inputs:{x:s},backend:n}),o=Vm({inputs:{input:r},backend:n}),i=Um({inputs:{x:o},backend:n}),u=rl({inputs:{real:a,imag:i},backend:n});return n.disposeData(s.dataId),n.disposeData(a.dataId),n.disposeData(o.dataId),n.disposeData(i.dataId),u}else return al({attrs:{shape:r.shape,dtype:r.dtype,value:1},backend:n})}var sle={kernelName:ci,backendName:"webgpu",kernelFunc:GE};function ale(e){let{inputs:t,backend:n,attrs:r}=e,{axis:s}=r;if(t.length===1)return Ck({inputs:{input:t[0]},backend:n,attrs:{dim:s}});let a=t[0].shape,o=t[0].dtype;t.forEach(c=>{w.assertShapesMatch(a,c.shape,"All tensors passed to stack must have matching shapes"),w.assert(o===c.dtype,()=>"All tensors passed to stack must have matching dtypes")});let i=[],u=t.map(c=>{let d=Ck({inputs:{input:c},backend:n,attrs:{dim:s}});return i.push(d),d}),l=AE({inputs:u,backend:n,attrs:{axis:s}});return i.forEach(c=>n.disposeData(c.dataId)),l}var ole={kernelName:di,backendName:"webgpu",kernelFunc:ale},ile=class{constructor(e,t){this.variableNames=["x"],this.uniforms="constantValue : f32;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t.map((n,r)=>n[0]+e[r]+n[1]),this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),t.map((n,r)=>{this.uniforms+=` pad${r} : vec2<i32>;`}),this.xShape=e,this.shaderKey="pad"}getUserCode(){let e=this.xShape.length,t=on(e),n=this.xShape.map((c,d)=>`uniforms.pad${d}[0]`).join(","),r=this.xShape.map((c,d)=>`uniforms.pad${d}[0] + uniforms.xShape${e>1?`[${d}]`:""}`).join(","),s=e>1?`${t}(${n})`:`${n}`,a=e>1?`${t}(${r})`:`${r}`,o=e>1?"any(outC < start)":"outC < start",i=e>1?"any(outC >= end)":"outC >= end",u=e>1?["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,e):"coords";return`
${Ue()}
if (index < uniforms.size) {
let start = ${s};
let end = ${a};
let outC = getCoordsFromFlatIndex(index);
if (${o} || ${i}) {
setOutputFlat(index, uniforms.constantValue);
} else {
let coords = outC - start;
setOutputFlat(index, getX(${u}));
}
}
}
`}},HE=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{paddings:a,constantValue:o}=r;if(a.every(l=>w.arraysEqual(l,[0,0])))return Lr({inputs:{x:s},backend:n});if(w.sizeFromShape(s.shape)===0){let l=a.map((c,d)=>c[0]+s.shape[d]+c[1]);return al({backend:n,attrs:{shape:l,value:o,dtype:s.dtype}})}let i=[{type:"float32",data:[o]}];a.map(l=>i.push({type:"int32",data:[l[0],l[1]]}));let u=new ile(s.shape,a);return n.runWebGPUProgram(u,[s],s.dtype,i)},ule={kernelName:La,backendName:"webgpu",kernelFunc:HE},cle=Rn({opSnippet:Mt.POW}),lle={kernelName:Ba,backendName:"webgpu",kernelFunc:cle};function dle(e){let{inputs:t,backend:n}=e,{x:r,alpha:s}=t,a=new SE(Mt.PRELU,r.shape,s.shape);return n.runWebGPUProgram(a,[r,s],"float32")}var ple={kernelName:za,backendName:"webgpu",kernelFunc:dle};function hle(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{axis:a,keepDims:o}=r;return mp(s,a,o,"prod",n)}var fle={kernelName:pi,backendName:"webgpu",kernelFunc:hle},mle=e=>{let{backend:t,attrs:n}=e,{start:r,stop:s,step:a,dtype:o}=n,i=_ie(r,s,a,o);return t.makeTensorInfo([i.length],o,i)},gle={kernelName:ic,backendName:"webgpu",kernelFunc:mle},jE=Rn({opSnippet:Mt.DIV}),ble={kernelName:wa,backendName:"webgpu",kernelFunc:jE},yle=fn({opType:bt.RELU}),vle={kernelName:Wa,backendName:"webgpu",kernelFunc:yle},xle=fn({opType:bt.RELU6}),wle={kernelName:Ua,backendName:"webgpu",kernelFunc:xle},kle=class{constructor(e,t,n){this.variableNames=["x"],this.uniforms="adjustHeightWidth : vec2<f32>; halfPixelCenters : f32;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=[e[0],t,n,e[3]],this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="resizeBilinear"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let b = coords[0];
let d = coords[3];
let rc = coords.yz;
let effectiveInSize = vec2<f32>(
f32(uniforms.xShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.xShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveOutSize = vec2<f32>(
f32(uniforms.outShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.outShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveInputOverOutputRatioRC =
effectiveInSize / effectiveOutSize;
// Fractional source index
let sourceFracIndexRC =
(vec2<f32>(rc) + vec2<f32>(uniforms.halfPixelCenters)) *
effectiveInputOverOutputRatioRC - vec2<f32>(uniforms.halfPixelCenters);
// Compute the four integer indices.
let sourceFloorRC = vec2<i32>(sourceFracIndexRC);
let sourceCeilRC = vec2<i32>(
min(vec2<f32>(uniforms.xShape.yz) - vec2<f32>(1.0), ceil(sourceFracIndexRC)));
let topLeft = getX(b, sourceFloorRC.x, sourceFloorRC.y, d);
let bottomLeft = getX(b, sourceCeilRC.x, sourceFloorRC.y, d);
let topRight = getX(b, sourceFloorRC.x, sourceCeilRC.y, d);
let bottomRight = getX(b, sourceCeilRC.x, sourceCeilRC.y, d);
let fracRC = sourceFracIndexRC - vec2<f32>(sourceFloorRC);
let top = topLeft + (topRight - topLeft) * fracRC.y;
let bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
let newValue = top + (bottom - top) * fracRC.x;
setOutputFlat(index, newValue);
}
}
`}};function Ile(e){let{inputs:t,backend:n,attrs:r}=e,{images:s}=t,{alignCorners:a,size:o,halfPixelCenters:i}=r,[u,l]=o,c=a&&u>1?1:0,d=a&&l>1?1:0,h=[{type:"float32",data:[c,d]},{type:"float32",data:[i?.5:0]}],f=new kle(s.shape,u,l);return n.runWebGPUProgram(f,[s],"float32",h)}var Sle={kernelName:Va,backendName:"webgpu",kernelFunc:Ile},Cle=class{constructor(e,t,n,r){this.variableNames=["x"],this.uniforms="adjustHeightWidth : vec2<f32>; roundBase : f32;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=[e[0],t,n,e[3]],this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.halfPixelCenters=r,this.shaderKey=`resizeNearest_${r}`}getUserCode(){let e;return this.halfPixelCenters?e="max((vec2<f32>(rc) + vec2<f32>(0.5)) * effectiveInputOverOutputRatioRC, vec2<f32>(0.0))":e="vec2<f32>(rc) * effectiveInputOverOutputRatioRC",`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let b = coords[0];
let d = coords[3];
let rc = coords.yz;
let effectiveInSize = vec2<f32>(
f32(uniforms.xShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.xShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveOutSize = vec2<f32>(
f32(uniforms.outShape.y) - uniforms.adjustHeightWidth[0],
f32(uniforms.outShape.z) - uniforms.adjustHeightWidth[1]);
let effectiveInputOverOutputRatioRC =
effectiveInSize / effectiveOutSize;
// Fractional source index
let sourceFracIndexRC = ${e};
// Compute the coordinators of nearest neighbor point.
let inputShapeRC = vec2<f32>(f32(uniforms.xShape.y), f32(uniforms.xShape.z));
let sourceNearestRC = vec2<i32>(
min(inputShapeRC - 1.0, floor(sourceFracIndexRC + uniforms.roundBase)));
let newValue = getX(b, sourceNearestRC.x, sourceNearestRC.y, d);
setOutputFlat(index, newValue);
}
}
`}};function Tle(e){let{inputs:t,backend:n,attrs:r}=e,{images:s}=t,{alignCorners:a,halfPixelCenters:o,size:i}=r,[u,l]=i,c=a&&u>1?1:0,d=a&&l>1?1:0,h=[{type:"float32",data:[c,d]},{type:"float32",data:[a?.5:0]}],f=new Cle(s.shape,u,l,o);return n.runWebGPUProgram(f,[s],s.dtype,h)}var Nle={kernelName:cc,backendName:"webgpu",kernelFunc:Tle},_le=class{constructor(e,t){this.outputShape=[],this.variableNames=["x"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms=`centerX : f32; centerY : f32; sinRadians : f32;
cosRadians : f32;`,this.shaderKey="rotate",this.outputShape=e,typeof t=="number"?(this.uniforms+=" fillValue : f32;",this.fillSnippet="var outputValue = uniforms.fillValue;",this.shaderKey+="_float"):(this.uniforms+=" fillValue : vec3<f32>;",this.fillSnippet="var outputValue = uniforms.fillValue[coords[3]];",this.shaderKey+="_vec3")}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
let coordXFloat = (f32(coords[2]) - uniforms.centerX) *
uniforms.cosRadians - (f32(coords[1]) - uniforms.centerY) *
uniforms.sinRadians;
let coordYFloat = (f32(coords[2]) - uniforms.centerX) *
uniforms.sinRadians + (f32(coords[1]) - uniforms.centerY) *
uniforms.cosRadians;
let coordX = i32(round(coordXFloat + uniforms.centerX));
let coordY = i32(round(coordYFloat + uniforms.centerY));
${this.fillSnippet}
if(coordX >= 0 && coordX < uniforms.xShape[2] && coordY >= 0 &&
coordY < uniforms.xShape[1]) {
outputValue = getX(coords[0], coordY, coordX, coords[3]);
}
setOutputFlat(index, outputValue);
}
}
`}},Ele={kernelName:_i,backendName:"webgpu",kernelFunc:({inputs:e,attrs:t,backend:n})=>{let{image:r}=e,{radians:s,fillValue:a,center:o}=t,i=n,u=new _le(r.shape,a),[l,c]=N.getImageCenter(o,r.shape[1],r.shape[2]),d=[{type:"float32",data:[l]},{type:"float32",data:[c]},{type:"float32",data:[Math.sin(s)]},{type:"float32",data:[Math.cos(s)]}];return typeof a=="number"?d.push({type:"float32",data:[Number.parseFloat(a.toFixed(2))]}):d.push({type:"float32",data:a}),i.runWebGPUProgram(u,[r],r.dtype,d)}},Ale=fn({opType:bt.RSQRT,cpuKernelImpl:Eie}),$le={kernelName:Ga,backendName:"webgpu",kernelFunc:Ale},Fle=class{constructor(e,t,n,r,s,a,o){this.variableNames=["updates","indices"],this.workGroupSize=[64,1,1],this.atomic=!0,this.outputShape=a,this.type=o,this.dispatchLayout=ze(e),this.dispatch=_e(this.dispatchLayout,e,this.workGroupSize),this.sliceDimGreaterThanOne=t>1,this.shaderKey=`scatter_${n}_${r}_${this.sliceDimGreaterThanOne}_${o}`;let i=on(s.length);this.uniforms=`sliceDim : i32; strides: ${i}; size: i32;`,this.updatesRank=r,this.indicesRank=n}getUserCode(){let e="";this.indicesRank===1?e="coords[0]":this.indicesRank===2&&(e="coords[0], j");let t=`getIndices(${e})`,n=this.sliceDimGreaterThanOne?"uniforms.strides[j]":"uniforms.strides",r="",s="",a="";this.updatesRank===1?(r="coords[0]",s="flattenedIndex",a=`
fn getUpdatesCoordsFromFlatIndex(index : i32) -> i32 {
return index;
}
`):this.updatesRank===2&&(r="coords[0], coords[1]",s="vec2<i32>(flattenedIndex, coords[1])",a=`
fn getUpdatesCoordsFromFlatIndex(index : i32) -> vec2<i32> {
let d0 = index / uniforms.updatesShape[1];
let d1 = index - d0 * uniforms.updatesShape[1];
return vec2<i32>(d0, d1);
}
`);let o=`getUpdates(${r})`,i=this.type==="int32"?"atomicAdd(&(result.numbers[flatIndex]), i32(updateValue));":`
var assumed = atomicLoad(&(result.numbers[flatIndex]));
var success = 0;
for (; success == 0;) {
let new = bitcast<f32>(assumed) + updateValue;
let newI32 = bitcast<i32>(new);
let resValue = atomicCompareExchangeWeak(&(result.numbers[flatIndex]), assumed, newI32);
assumed = resValue[0];
success = resValue[1];
}
`;return`
${a}
${Ue()}
if (index < uniforms.size) {
let coords = getUpdatesCoordsFromFlatIndex(index);
var flattenedIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexInside = i32(round(${t}));
flattenedIndex = flattenedIndex + indexInside * ${n};
}
let updateValue = ${o};
let flatIndex = getOutputFlatIndex(${s});
${i}
}
}`}};function Dle(e){let{inputs:t,backend:n,attrs:r}=e,{indices:s,updates:a}=t,{shape:o}=r,{sliceRank:i,numUpdates:u,sliceSize:l,strides:c,outputSize:d}=N.calculateShapes(a,s,o),p=[d/l,l];if(d===0)return n.makeTensorInfo(o,s.dtype);let h=We({inputs:{x:s},backend:n,attrs:{shape:[u,i]}}),f=We({inputs:{x:a},backend:n,attrs:{shape:[u,l]}}),m=f.dtype,g=al({backend:n,attrs:{shape:p,value:0,dtype:m}}),b=w.sizeFromShape(f.shape),y=[{type:"int32",data:[i]},{type:"int32",data:c},{type:"int32",data:[b]}],v=new Fle(f.shape,i,h.shape.length,f.shape.length,c,p,m),x=n.runWebGPUProgram(v,[f,h],m,y,g),k=We({inputs:{x},backend:n,attrs:{shape:o}});return n.disposeData(h.dataId),n.disposeData(f.dataId),n.disposeData(x.dataId),k}var Rle={kernelName:gi,backendName:"webgpu",kernelFunc:Dle},Ple=class{constructor(e,t,n){this.variableNames=["c","a","b"],this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=t,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.cRank=e,this.rank=n,this.shaderKey="select"}getUserCode(){let e,t;if(this.rank>4)throw Error(`Where for rank ${this.rank} is not yet supported`);if(this.rank===1)t="resRC",e="resRC";else{let r=["resRC.x","resRC.y","resRC.z","resRC.w"],s=[],a=[];for(let o=0;o<this.outputShape.length;o++)a.push(`${r[o]}`),o<this.cRank&&s.push(`${r[o]}`);e=s.join(),t=a.join()}return`
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromFlatIndex(index);
let cVal = getC(${e});
if (cVal >= 1.0) {
setOutputFlat(index, getA(${t}));
} else {
setOutputFlat(index, getB(${t}));
}
}
}
`}};function Ole(e){let{inputs:t,backend:n}=e,{condition:r,t:s,e:a}=t,o=new Ple(r.shape.length,s.shape,s.shape.length);return n.runWebGPUProgram(o,[r,s,a],In(s.dtype,a.dtype))}var Mle={kernelName:bi,backendName:"webgpu",kernelFunc:Ole},Lle=fn({opType:bt.SIGMOID}),Ble={kernelName:ja,backendName:"webgpu",kernelFunc:Lle},zle=fn({opType:bt.SIN}),Wle={kernelName:Ha,backendName:"webgpu",kernelFunc:zle},Vle=fn({opType:bt.SINH}),Ule={kernelName:vi,backendName:"webgpu",kernelFunc:Vle},qE=Rn({opSnippet:Mt.SUB,cpuKernelImpl:Rie,supportsComplex:!0}),Gle={kernelName:Qa,backendName:"webgpu",kernelFunc:qE};function Hle(e){let{inputs:t,backend:n,attrs:r}=e,{logits:s}=t,{dim:a}=r,o=w.parseAxisParam([a],s.shape),i=UE({inputs:{x:s},backend:n,attrs:{reductionIndices:o,keepDims:!1}}),u=N.expandShapeToKeepDim(i.shape,o),l=We({inputs:{x:i},backend:n,attrs:{shape:u}}),c=qE({inputs:{a:s,b:l},backend:n}),d=LE({inputs:{x:c},backend:n}),p=Sk({inputs:{x:d},backend:n,attrs:{axis:o,keepDims:!1}}),h=We({inputs:{x:p},backend:n,attrs:{shape:u}}),f=jE({inputs:{a:d,b:h},backend:n});return n.disposeData(i.dataId),n.disposeData(l.dataId),n.disposeData(c.dataId),n.disposeData(d.dataId),n.disposeData(p.dataId),n.disposeData(h.dataId),f}var jle={kernelName:Xa,backendName:"webgpu",kernelFunc:Hle},qle=e=>{let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{blockShape:a,paddings:o}=r;w.assert(s.shape.length<=4,()=>"spaceToBatchND for rank > 4 with a WebGPU backend not implemented yet");let i=a.reduce((b,y)=>b*y),u=[[0,0]];u.push(...o);for(let b=1+a.length;b<s.shape.length;++b)u.push([0,0]);let l=[],c=HE({inputs:{x:s},backend:n,attrs:{paddings:u,constantValue:0}}),d=N.getReshaped(c.shape,a,i,!1),p=N.getPermuted(d.length,a.length,!1),h=N.getReshapedPermuted(c.shape,a,i,!1),f=We({inputs:{x:c},backend:n,attrs:{shape:d}}),m=cu({inputs:{x:f},backend:n,attrs:{perm:p}}),g=We({inputs:{x:m},backend:n,attrs:{shape:h}});return l.push(c),l.push(f),l.push(m),l.forEach(b=>n.disposeData(b.dataId)),g},Kle={kernelName:xi,backendName:"webgpu",kernelFunc:qle},Xle=class{constructor(e,t,n,r,s,a,o=!0){this.variableNames=["updates","indices","defaultValue"],this.workGroupSize=[64,1,1],this.workPerThread=4,this.size=!0,this.outputShape=a,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]);let i=t>1;this.shaderKey=`scatter_${n}_${r}_${i}`;let u=on(s.length);this.uniforms=`updateSize : i32; sliceDim : i32; strides: ${u};`;let l="";n===1?l="i":n===2&&(l="i, j"),this.indicesSnippet=`getIndices(${l})`;let c="";r===1?c="i":r===2&&(c="i, coords[1]"),this.updatesSnippet=`getUpdates(${c})`,this.strideString=i?"uniforms.strides[j]":"uniforms.strides"}getUserCode(){return`
${Ue()}
let globalIndex = index * ${this.workPerThread};
if (globalIndex < uniforms.size) {
var sum = vec4<f32>(0.0);
var found = vec4<bool>(false);
for (var i = 0; i < uniforms.updateSize; i = i + 1) {
var flattenedIndex = 0;
for (var j = 0; j < uniforms.sliceDim; j = j + 1) {
let indexInside = i32(round(${this.indicesSnippet}));
flattenedIndex = flattenedIndex + indexInside * ${this.strideString};
}
for (var innerIndex = 0; innerIndex < ${this.workPerThread}; innerIndex = innerIndex + 1) {
let curIndex = globalIndex + innerIndex;
let coords = getCoordsFromFlatIndex(curIndex);
if (flattenedIndex == coords[0]) {
sum[innerIndex] = sum[innerIndex] + ${this.updatesSnippet};
found[innerIndex] = true;
}
}
}
for (var innerIndex = 0; innerIndex < ${this.workPerThread}; innerIndex = innerIndex + 1) {
let curIndex = globalIndex + innerIndex;
if (curIndex < uniforms.size)
{
setOutputFlat(curIndex, mix(getDefaultValue(), sum[innerIndex], f32(found[innerIndex])));
}
}
}
}`}};function Yle(e){let{inputs:t,backend:n,attrs:r}=e,{sparseIndices:s,sparseValues:a,defaultValue:o}=t,{outputShape:i}=r,{sliceRank:u,numUpdates:l,strides:c,outputSize:d}=N.calculateShapes(a,s,i),p=!1,h=[{type:"int32",data:[l]},{type:"int32",data:[u]},{type:"int32",data:c}],f=new Xle(l,u,s.shape.length,a.shape.length,c,[d,1],p),m=n.runWebGPUProgram(f,[a,s,o],a.dtype,h),g=We({inputs:{x:m},backend:n,attrs:{shape:i}});return n.disposeData(m.dataId),g}var Qle={kernelName:id,backendName:"webgpu",kernelFunc:Yle};function Zle(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=r,i=w.parseAxisParam(o,s.shape)[0],u=N.prepareSplitSize(s,a,i),l=s.shape.length,c=new Array(l).fill(0),d=s.shape.slice();return u.map(p=>{let h=[...d];h[i]=p;let f=sl({inputs:{x:s},backend:n,attrs:{begin:c,size:h}});return c[i]+=p,f})}var Jle={kernelName:wi,backendName:"webgpu",kernelFunc:Zle},ede=fn({opType:bt.SQRT}),tde={kernelName:qa,backendName:"webgpu",kernelFunc:ede},nde={kernelName:fc,backendName:"webgpu",kernelFunc:({inputs:e,backend:t})=>{let{x:n}=e,r=t,s=new Wm(n.shape,bt.SQUARE);return r.runWebGPUProgram(s,[n],n.dtype)}},rde=Rn({opSnippet:Mt.SQUARED_DIFFERENCE}),sde={kernelName:Ya,backendName:"webgpu",kernelFunc:rde},ade=class{constructor(e){this.variableNames=["x"],this.workPerThread=1,this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]);let t=on(this.outputShape.length);this.uniforms=`begin : ${t}; strides : ${t}; `,this.shaderKey="stridedSlice"}getUserCode(){let e=this.outputShape.length,t="";if(e===1)t="coords * uniforms.strides + uniforms.begin";else{let r=0;t=this.outputShape.map((s,a)=>(r++,this.outputShape.length===1?`coords * uniforms.strides[${a}] + uniforms.begin[${a}]`:`coords[${r-1}] * uniforms.strides[${a}] + uniforms.begin[${a}]`)).join(",")}return`
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
setOutputFlat(index, getX(${t}));
}
}
`}};function ode(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{begin:a,end:o,strides:i,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:d,shrinkAxisMask:p}=r,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=$t.sliceInfo(s.shape,a,o,i,u,l,c,d,p),k;if(m)k=We({inputs:{x:s},backend:n,attrs:{shape:f}});else if(g||b){w.assert(s.shape.length>=1,()=>`Input must have rank at least 1, got: ${s.shape.length}`);let T=$t.computeOutShape(y,v,x),C=sl({inputs:{x:s},backend:n,attrs:{begin:y,size:T}});k=We({inputs:{x:C},backend:n,attrs:{shape:f}}),n.disposeData(C.dataId)}else if(n.shouldExecuteOnCPU([s])){let C=n.readSync(s.dataId),E=$e(s.shape,s.dtype,C),F=Fie(h,E,x,y);k=n.makeTensorInfo(f,s.dtype,F.values)}else{let C=new ade(h),E=[{type:"int32",data:y},{type:"int32",data:x}],F=n.runWebGPUProgram(C,[s],s.dtype,E);k=We({inputs:{x:F},backend:n,attrs:{shape:f}}),n.disposeData(F.dataId)}return k}var ide={kernelName:ki,backendName:"webgpu",kernelFunc:ode};function ude(e){let{inputs:t,backend:n,attrs:r}=e,{separator:s,nGramWidths:a,leftPad:o,rightPad:i,padWidth:u,preserveShortSequences:l}=r,{data:c,dataSplits:d}=t,p=n.readSync(c.dataId),h=n.readSync(d.dataId),[f,m]=Die(p,h,s,a,o,i,u,l);return[n.makeTensorInfo([f.length],"string",f),n.makeTensorInfo(d.shape,"int32",m)]}var cde={kernelName:ud,backendName:"webgpu",kernelFunc:ude},lde=fn({opType:bt.TANH}),dde={kernelName:Za,backendName:"webgpu",kernelFunc:lde},pde=class{constructor(e,t){this.variableNames=["A"],this.workGroupSize=[64,1,1],this.size=!0;let n=new Array(e.length);for(let r=0;r<n.length;r++)n[r]=e[r]*t[r];this.outputShape=n,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.rank=this.outputShape.length,this.shaderKey="tile"}getUserCode(){let e=hde(this.rank,"uniforms.");return`
${Ue()}
if (index < uniforms.size) {
let resRC = getCoordsFromFlatIndex(index);
setOutputFlat(index, getA(${e}));
}
}
`}};function hde(e,t=""){if(e>=5)throw Error(`Tile for rank ${e} is not yet supported`);if(e===1)return`(resRC % ${t}aShape)`;let n=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[];for(let s=0;s<e;s++)r.push(`(${n[s]} % ${t}aShape[${s}])`);return r.join()}function fde(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{reps:a}=r;if(n.shouldExecuteOnCPU([s])||s.dtype==="string"||s.shape.length>=5){let u=n.readSync(s.dataId),l=s.dtype==="string"?u.map(p=>w.decodeString(p)):u,c=$e(s.shape,s.dtype,l),d=Pie(c,a);return n.makeTensorInfo(d.shape,d.dtype,d.values)}let o=new pde(s.shape,a);return n.runWebGPUProgram(o,[s],s.dtype)}var mde={kernelName:As,backendName:"webgpu",kernelFunc:fde},gde=class{constructor(e){this.variableNames=["x","indices"],this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms=`inputSize : i32; firstPass : i32; negativeInf : f32;
dir : i32; inc : i32;`,this.shaderKey="swap"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let outC = getCoordsFromFlatIndex(index);
let batch = outC[0];
let elemIdx = outC[1];
// We compare elements pair-wise within a group of size 2 * inc.
// The comparing rule for each group alternates between ascending
// and descending. Within each group, we compare each pair at
// positions i and i+inc. To decide whether an element at position i
// is x0 or x1, we mod it by 2 * inc, if the result is smaller than
// inc, it is in the first half of the group, we denote it as x0,
// otherwise we denote it as x1.
// For example, as shown in the Bitonic top K paper referenced
// above, Figure5(a) shows that element[1] is in the second half of
// the group when group size is 2, but it is in the first half of
// the group when group size is 4.
let isFirstInPair = elemIdx % (2 * uniforms.inc) < uniforms.inc;
var i = 0;
if (isFirstInPair) {
i = elemIdx;
} else {
i = elemIdx - uniforms.inc;
}
var i0 = 0;
if (uniforms.firstPass == 1) {
i0 = i;
} else {
i0 = i32(getIndices(batch, i));
}
var i1 = 0;
if (uniforms.firstPass == 1) {
i1 = i + uniforms.inc;
} else {
i1 = i32(getIndices(batch, i + uniforms.inc));
}
var x0 = f32(0.0);
var x1 = f32(0.0);
if (i0 < uniforms.inputSize) {
x0 = getX(batch, i0);
} else {
x0 = uniforms.negativeInf;
}
if (i1 < uniforms.inputSize) {
x1 = getX(batch, i1);
} else {
x1 = uniforms.negativeInf;
}
let reverse = elemIdx % (2 * uniforms.dir) >= uniforms.dir;
let isGreater = x0 > x1 || (x0 == x1 && i1 > i0);
if (reverse == isGreater) {
// Elements in opposite order of direction
let iTemp = i0;
i0 = i1;
i1 = iTemp;
}
if (isFirstInPair) {
setOutputFlat(index, f32(i0));
} else {
setOutputFlat(index, f32(i1));
}
}
}
`}},bde=class{constructor(e){this.variableNames=["x","indices"],this.workGroupSize=[256,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.uniforms="inputSize : i32; firstPass : i32; k : i32;",this.shaderKey="merge"}getUserCode(){return`
${Ue()}
if (index < uniforms.size) {
let outC = getCoordsFromFlatIndex(index);
let batch = outC[0];
let elemIdx = outC[1];
// The output size is half of the previous size.
// If the previous sequence is | | | | _ _ _ _ | | | | _ _ _ _
// (k=4), we only need to output the indices at positions |, the
// indices at positions _ can be thrown away, see Figure5(b) After
// Phase 2 (Merge phase) in the Bitonic Top K paper referenced
// above.
// For example, the paper shows we only need to output the orange
// bars. The output sequence should look like this | | | | | | | |.
// Because the sequence is halved, to map the output index back to
// the previous sequence to find the corresponding value, we need
// to double the index. When we double the index, we basically
// interpolate a position, so 2i looks like
// | _ | _ | _ | _ | _ | _ | _. We move the | to the first k
// position of each 2k positions by - elemIdx % k. E.g. for output
// at index 4,5,6,7, we want to get the corresponding element at
// original index 8,9,10,11, for output at index 8,9,10,11,
// we want to get the corresponding element at original index
// 16,17,18,19, so on and so forth.
var i = 0;
if (elemIdx < uniforms.k) {
i = elemIdx;
} else {
i = elemIdx * 2 - elemIdx % uniforms.k;
}
var i0 = 0;
if (uniforms.firstPass == 1) {
i0 = i;
} else {
i0 = i32(getIndices(batch, i));
}
var i1 = 0;
if (uniforms.firstPass == 1) {
i1 = i + uniforms.k;
} else {
i1 = i32(getIndices(batch, i + uniforms.k));
}
let x0 = getX(batch, i0);
var x1 = f32(0.0);
if (i1 < uniforms.inputSize) {
x1 = getX(batch, i1);
} else {
x1 = x0;
}
if (x0 >= x1) {
setOutputFlat(index, f32(i0));
} else {
setOutputFlat(index, f32(i1));
}
}
}
`}};function il(e,t){t!==null&&e.disposeData(t.dataId)}function KE(e){let t=1;for(;t<e;)t*=2;return t}function yde(e){let{inputs:t,backend:n,attrs:r}=e,{x:s}=t,{k:a,sorted:o}=r,i=s.shape,u=i[i.length-1];if(n.shouldExecuteOnCPU([s])){let k=n.readSync(s.dataId),[T,C]=Oie(k,i,s.dtype,a,o);return[n.makeTensorInfo(T.shape,T.dtype,T.values),n.makeTensorInfo(C.shape,C.dtype,C.values)]}if(a===0)return i[i.length-1]=0,[n.makeTensorInfo(i,s.dtype,[]),n.makeTensorInfo(i,"int32",[])];if(u===1)return[s,al({attrs:{shape:i,dtype:"int32",value:0},backend:n})];let c=w.sizeFromShape(i)/u,d=We({inputs:{x:s},attrs:{shape:[c,u]},backend:n}),p=KE(a),h=KE(u),f=null,m=()=>f===null?[d,d]:[d,f],g=(k,T,C)=>{let E=m(),F=new gde(C),D=[{type:"int32",data:[u]},{type:"int32",data:[f===null?1:0]},{type:"float32",data:[Number.NEGATIVE_INFINITY]},{type:"int32",data:[k]},{type:"int32",data:[T]}],R=f;f=n.runWebGPUProgram(F,E,"int32",D),il(n,R)};for(let k=1;k<p;k*=2){let T=k*2;for(let C=k;C>=1;C/=2)g(T,C,[c,h])}for(let k=h;k>p;k/=2){let T=m(),C=new bde([c,k/2]),F=[{type:"int32",data:[u]},{type:"int32",data:[f===null?1:0]},{type:"int32",data:[p]}],O=f;f=n.runWebGPUProgram(C,T,"int32",F),il(n,O);let D=p/2,R=D*2;for(let _=D;_>=1;_/=2)g(R,_,f.shape)}let b=f;f=sl({inputs:{x:f},backend:n,attrs:{begin:0,size:[c,a]}}),il(n,b);let y=VE({inputs:{x:d,indices:f},backend:n,attrs:{axis:1,batchDims:1}});il(n,d);let v=i.slice(0,-1);v.push(a),b=f,f=We({inputs:{x:f},attrs:{shape:v},backend:n}),il(n,b);let x=y;return y=We({inputs:{x:y},attrs:{shape:v},backend:n}),il(n,x),[y,f]}var vde={kernelName:Si,backendName:"webgpu",kernelFunc:yde},xde=class{constructor(e){this.variableNames=["Image","Transforms"],this.uniforms="interpolationModeId : i32; fillModeId : i32; fillValue : f32;",this.workGroupSize=[64,1,1],this.size=!0,this.outputShape=e,this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize),this.shaderKey="transform"}getUserCode(){return`
fn mapCoord(outCoord : f32, len : f32) -> f32{
var inCoord = outCoord;
if(uniforms.fillModeId == 2) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz2 = 2.0 * len;
if (inCoord < sz2) {
inCoord = sz2 * f32(i32(f32(-inCoord / sz2))) +
inCoord;
}
if (inCoord < -len) {
inCoord = inCoord + sz2;
} else {
inCoord = -inCoord - 1.0;
}
}
} elseif (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz2 = 2.0 * len;
inCoord = inCoord - sz2 * f32(i32(f32(inCoord / sz2)));
if (inCoord >= len) {
inCoord = sz2 - inCoord - 1.0;
}
}
}
return clamp(inCoord, 0.0, len - 1.0);
} elseif (uniforms.fillModeId == 3) {
if (inCoord < 0.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz = len - 1.0;
inCoord = inCoord + len * (f32(i32(f32(-inCoord / sz))) + 1.0);
}
} elseif (inCoord > len - 1.0) {
if (len <= 1.0) {
inCoord = 0.0;
} else {
let sz = len - 1.0;
inCoord = inCoord - len * f32(i32(f32(inCoord / sz)));
}
}
return clamp(inCoord, 0.0, len - 1.0);
} elseif (uniforms.fillModeId == 4) {
return clamp(outCoord, 0.0, len - 1.0);
}
return outCoord;
}
fn readWithFillValue(batch : i32, coordY : i32, coordX : i32,
channel : i32) -> f32 {
var outputValue : f32;
if (0 <= coordY && coordY < uniforms.imageShape[1] && 0 <= coordX && coordX < uniforms.imageShape[2]) {
outputValue = getImage(batch, coordY, coordX, channel);
} else {
outputValue = uniforms.fillValue;
}
return outputValue;
}
${Ue()}
if (index < uniforms.size) {
let coords = getCoordsFromFlatIndex(index);
var outputValue : f32;
let batch = coords[0];
let x = coords[2];
let y = coords[1];
let channel = coords[3];
let xf = f32(x);
let yf = f32(y);
let a1 = getTransforms(batch, 0);
let a2 = getTransforms(batch, 1);
let a3 = getTransforms(batch, 2);
let b1 = getTransforms(batch, 3);
let b2 = getTransforms(batch, 4);
let b3 = getTransforms(batch, 5);
let c1 = getTransforms(batch, 6);
let c2 = getTransforms(batch, 7);
let projection = c1 * xf + c2 * yf + 1.0;
if (projection == 0.0) {
outputValue = uniforms.fillValue;
} else {
let inX = (a1 * xf + a2 * yf + a3) / projection;
let inY = (b1 * xf + b2 * yf + b3) / projection;
let mapX = mapCoord(inX, f32(uniforms.imageShape[2]));
let mapY = mapCoord(inY, f32(uniforms.imageShape[1]));
if (uniforms.interpolationModeId == 1) {
let coordY = i32(round(mapY));
let coordX = i32(round(mapX));
outputValue = readWithFillValue(batch, coordY, coordX,
channel);
} else {
let yFloor = floor(mapY);
let xFloor = floor(mapX);
let yCeil = yFloor + 1.0;
let xCeil = xFloor + 1.0;
let valueYFloor = (xCeil - mapX) *
readWithFillValue(batch, i32(yFloor), i32(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, i32(yFloor), i32(xCeil), channel);
let valueYCeil = (xCeil - mapX) *
readWithFillValue(batch, i32(yCeil), i32(xFloor), channel) +
(mapX - xFloor) *
readWithFillValue(batch, i32(yCeil), i32(xCeil), channel);
outputValue = (yCeil - mapY) * valueYFloor +
(mapY - yFloor) * valueYCeil;
}
}
setOutputFlat(index, outputValue);
}
}
`}};function wde(e){let{inputs:t,backend:n,attrs:r}=e,{image:s,transforms:a}=t,{interpolation:o,fillMode:i,fillValue:u,outputShape:l}=r,[c,d,p,h]=s.shape,[f,m]=l!=null?l:[d,p],g=[c,f,m,h],b=new xde(g),y=o==="nearest"?1:2,v;switch(i){case"constant":v=1;break;case"reflect":v=2;break;case"wrap":v=3;break;case"nearest":v=4;break;default:v=1;break}let x=[{type:"int32",data:[y]},{type:"int32",data:[v]},{type:"float32",data:[u]}];return n.runWebGPUProgram(b,[s,a],"float32",x)}var kde={kernelName:Ci,backendName:"webgpu",kernelFunc:wde};function Ide(e){let{inputs:t,backend:n,attrs:r}=e,{value:s}=t,{axis:a}=r;a<0&&(a+=s.shape.length);let o=s,i=o.shape.length,u=s.shape[a],l=new Array(i-1),c=0;for(let m=0;m<i;m++)m!==a&&(l[c++]=o.shape[m]);let d=[],p=new Array(i).fill(0),h=o.shape.slice();h[a]=1;let f=new Array(u);for(let m=0;m<f.length;m++){p[a]=m;let g=sl({inputs:{x:o},backend:n,attrs:{begin:p,size:h}}),b=We({inputs:{x:g},backend:n,attrs:{shape:l}});f[m]=b,d.push(g)}return d.forEach(m=>n.disposeData(m.dataId)),f}var Sde={kernelName:Ti,backendName:"webgpu",kernelFunc:Ide},Cde=[nie,Bie,Wie,Gie,Yie,Zie,eue,nue,iue,due,hue,bue,oie,wue,Cue,Eue,$ue,Due,Oue,Bue,Wue,jue,Kue,Yue,Zue,Que,ece,nce,sce,lce,oce,uce,hce,mce,bce,xce,Ice,Cce,Nce,aie,vue,Ece,$ce,Dce,Pce,Mce,Lce,zce,Vce,Gce,jce,Kce,Yce,Vue,Zce,ele,nle,uue,sle,ole,ule,ple,fle,lle,gle,cue,ble,vle,wle,eie,Sle,Nle,Ele,$le,Rle,Mle,Ble,Wle,Ule,aue,ide,cde,jle,Kle,Jle,Qle,tde,nde,sde,Gle,Gue,dde,mde,vde,kde,Kie,Sde,rle];for(let e of Cde)gc(e);var Tde=class{constructor(e){this.device=e,this.numUsedBuffers=0,this.numFreeBuffers=0,this.freeBuffers=new Map,this.usedBuffers=new Map,this.numBytesUsed=0,this.numBytesAllocated=0}acquireBuffer(e,t){let n=XE(e,t);if(this.freeBuffers.has(n)||this.freeBuffers.set(n,[]),this.usedBuffers.has(n)||this.usedBuffers.set(n,[]),this.numBytesUsed+=e,this.numUsedBuffers++,this.freeBuffers.get(n).length>0){this.numFreeBuffers--;let s=this.freeBuffers.get(n).shift();return this.usedBuffers.get(n).push(s),s}this.numBytesAllocated+=e;let r=this.device.createBuffer({size:e,usage:t});return this.usedBuffers.get(n).push(r),r}releaseBuffer(e,t,n){if(this.freeBuffers==null)return;let r=XE(t,n);this.freeBuffers.has(r)||this.freeBuffers.set(r,[]),this.freeBuffers.get(r).push(e),this.numFreeBuffers++,this.numUsedBuffers--;let s=this.usedBuffers.get(r),a=s.indexOf(e);if(a<0)throw new Error("Cannot release a buffer that was never provided by this buffer manager");s.splice(a,1),this.numBytesUsed-=t}getNumUsedBuffers(){return this.numUsedBuffers}getNumFreeBuffers(){return this.numFreeBuffers}reset(){this.freeBuffers=new Map,this.usedBuffers=new Map,this.numUsedBuffers=0,this.numFreeBuffers=0,this.numBytesUsed=0,this.numBytesAllocated=0}dispose(){this.freeBuffers==null&&this.usedBuffers==null||(this.freeBuffers.forEach((e,t)=>{e.forEach(n=>{n.destroy()})}),this.usedBuffers.forEach((e,t)=>{e.forEach(n=>{n.destroy()})}),this.freeBuffers=null,this.usedBuffers=null,this.numUsedBuffers=0,this.numFreeBuffers=0,this.numBytesUsed=0,this.numBytesAllocated=0)}};function XE(e,t){return`${e}_${t}`}var YE=class{constructor(){this.outputShape=[0],this.variableNames=[],this.workGroupSize=[256,1,1],this.lastUniformData=[],this.inputTexture=null,this.layout=null,this.lastPixelSize={width:0,height:0},this.disposed=!1,this.shaderKey="fromPixels",this.useImport=!1}updateOutputShape(e){w.arraysEqual(this.outputShape,e)||(this.outputShape=e,this.workPerThread=e[2],this.dispatchLayout=ze(this.outputShape),this.dispatch=_e(this.dispatchLayout,this.outputShape,this.workGroupSize,[this.workPerThread,1,1]))}makeFromPixelsSource(){let e=this.useImport?"textureLoad(src, vec2<i32>(coords.yx));":"textureLoad(src, vec2<i32>(coords.yx), 0)";return`
[[binding(1), group(0)]] var src: ${this.useImport?"texture_external":"texture_2d<f32>"};
${Ue()}
let flatIndexBase = index * uniforms.numChannels;
for (var i = 0; i < uniforms.numChannels; i = i + 1) {
let flatIndex = flatIndexBase + i;
if (flatIndex < uniforms.size) {
let coords = getCoordsFromFlatIndex(flatIndexBase);
let values = ${e};
result.numbers[flatIndex] = i32(floor(255.0 * values[i]));
}
}
}
`}getUserCode(){return this.makeFromPixelsSource()}setPipeline(e){this.pipeline=e}setUniform(e,t){if(!this.uniform){let n=e.createBuffer({size:t.length*4,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST});this.uniform=n}!t||t.length===this.lastUniformData.length&&t.every((n,r)=>n===this.lastUniformData[r])||(e.queue.writeBuffer(this.uniform,0,new Uint32Array(t)),this.lastUniformData=t)}makeInputTexture(e,t,n){return(!this.inputTexture||this.lastPixelSize.width!==t||this.lastPixelSize.height!==n)&&(this.inputTexture&&this.inputTexture.destroy(),this.inputTexture=e.createTexture({size:[t,n],format:"rgba8unorm",usage:GPUTextureUsage.COPY_DST|GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.TEXTURE_BINDING}),this.lastPixelSize.width=t,this.lastPixelSize.height=n),this.inputTexture}dispose(){this.disposed||(this.uniform&&this.uniform.destroy(),this.inputTexture&&this.inputTexture.destroy(),this.disposed=!0)}getLayout(e){return this.layout===null&&(this.layout=this.createTextureLayout(e)),this.layout}createTextureLayout(e){let t=[];t.push({binding:0,visibility:GPUShaderStage.COMPUTE,buffer:{type:"storage"}}),t.push({binding:1,visibility:GPUShaderStage.COMPUTE,texture:{}}),t.push({binding:2,visibility:GPUShaderStage.COMPUTE,buffer:{}});let n=e.createBindGroupLayout({entries:t}),r=e.createPipelineLayout({bindGroupLayouts:[n]});return{bindGroupLayout:n,pipelineLayout:r}}},Nde=class extends YE{constructor(){super(...arguments);this.layout=null,this.useImport=!0}getUserCode(){return this.makeFromPixelsSource()}getLayout(e){return this.layout===null&&(this.layout=this.createTextureImportLayout(e)),this.layout}createTextureImportLayout(e){let t=[];t.push({binding:0,visibility:GPUShaderStage.COMPUTE,buffer:{type:"storage"}}),t.push({binding:1,visibility:GPUShaderStage.COMPUTE,externalTexture:{}}),t.push({binding:2,visibility:GPUShaderStage.COMPUTE,buffer:{}});let n=e.createBindGroupLayout({entries:t}),r=e.createPipelineLayout({bindGroupLayouts:[n]});return{bindGroupLayout:n,pipelineLayout:r}}},_de=X().getNumber("WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD"),QE=class extends Mu{constructor(e,t=!1){super();if(this.commandQueueOwnedIds=new WeakSet,this.tensorDisposalQueue=[],this.uniformDisposalQueue=[],this.disposed=!1,this.uploadWaitMs=0,this.downloadWaitMs=0,this.dispatchNumberInEncoder=0,!yk())throw new Error("WebGPU is not supported on this device");this.layoutCache={},this.pipelineCache={},this.device=e,this.queue=e.queue,this.currentCommandEncoder=null,this.currentComputePass=null,this.supportTimeQuery=t,this.bufferManager=new Tde(this.device),this.tensorMap=new Gl(this,is()),this.supportTimeQuery&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:2})),X().getBool("WEBGPU_USE_PROFILE_TOOL")&&(this.dummyCanvas=document.createElement("canvas"),this.dummyCanvas.width=1,this.dummyCanvas.height=1,this.dummyContext=this.dummyCanvas.getContext("webgpu"),this.dummyContext.configure({device:e,format:"bgra8unorm"}),document.body.appendChild(this.dummyCanvas))}nextDataId(){return QE.nextDataId++}floatPrecision(){return 32}defaultGpuBufferUsage(){return GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST}flushDisposalQueue(){this.tensorDisposalQueue.forEach(e=>{this.maybeReleaseBuffer(e),this.tensorMap.delete(e)}),this.uniformDisposalQueue.forEach(e=>this.bufferManager.releaseBuffer(e.buffer,e.byteSize,e.usage)),this.tensorDisposalQueue=[],this.uniformDisposalQueue=[]}disposeData(e,t=!1){if(this.tensorMap.has(e)){let n=this.tensorMap.get(e);if(n.refCount--,!t&&n.refCount>0)return!1;if(this.commandQueueOwnedIds.has(e))return this.tensorDisposalQueue.push(e),!1;this.maybeReleaseBuffer(e);let{complexTensorInfos:r}=this.tensorMap.get(e);r!=null&&(this.disposeData(r.real.dataId,!0),this.disposeData(r.imag.dataId,!0)),this.tensorMap.delete(e)}return!0}memory(){return{numBytesInGPU:this.bufferManager.numBytesUsed,numBytesAllocatedInGPU:this.bufferManager.numBytesAllocated,unreliable:!1}}getBufferManager(){return this.bufferManager}acquireBuffer(e,t=this.defaultGpuBufferUsage()){return this.bufferManager.acquireBuffer
${r.shape}`);if(s.shape.length!==1)throw new Error(`Input shape should be a vector but received shape
${s.shape}`);if(a.shape.length!==1)throw new Error(`Target shape should be a vector but received shape ${a.shape}`);let o=t.dataIdMap.get(r.dataId).id,i=t.dataIdMap.get(s.dataId).id,u=t.dataIdMap.get(a.dataId).id,l=r.shape[0],c=w.sizeFromShape(a.shape),d=t.makeOutput([l,c],r.dtype),p=t.dataIdMap.get(d.dataId).id,h=t.makeOutput([c],a.dtype),f=t.dataIdMap.get(h.dataId).id,m=t.makeOutput([3],"int32"),g=t.dataIdMap.get(m.dataId).id;GA(o,i,u,l,p,f,g);let b=t.readSync(m.dataId),y;switch(b[0]){case 0:{y=N.getSparseReshapeMultipleNegativeOneOutputDimErrorMessage(b[1],b[2]);break}case 1:{y=N.getSparseReshapeNegativeOutputDimErrorMessage(b[1],b[2]);break}case 2:y=N.getSparseReshapeEmptyTensorZeroOutputDimErrorMessage();break;case 3:{let v=Array.from(t.readSync(s.dataId)),x=Array.from(t.readSync(h.dataId));y=N.getSparseReshapeInputOutputMultipleErrorMessage(v,x);break}case 4:{let v=Array.from(t.readSync(s.dataId)),x=Array.from(t.readSync(h.dataId));y=N.getSparseReshapeInputOutputMismatchErrorMessage(v,x);break}default:y=""}if(t.disposeData(m.dataId),y)throw t.disposeData(d.dataId),t.disposeData(h.dataId),new Error(y);return[d,h]}var rme={kernelName:hc,backendName:"wasm",setupFunc:tme,kernelFunc:nme},HA;function jA(e){HA=e.wasm.cwrap("SparseSegmentReduction",null,["number","number","number","number","number","number","number","number","number"])}function qA(e,t){let{backend:n,inputs:r}=e,{data:s,indices:a,segmentIds:o}=r,i=a.shape[0],u=n.readSync(o.dataId,i-1,i)[0],c=i>0?u+1:0;if(c<0)throw new Error(N.getSparseSegmentReductionNegativeSegmentIdsErrorMessage());let d=s.shape.slice();d[0]=c;let p=n.dataIdMap.get(s.dataId).id,h=n.dataIdMap.get(a.dataId).id,f=n.dataIdMap.get(o.dataId).id,m=n.makeOutput(d,s.dtype),g=n.dataIdMap.get(m.dataId).id,b=n.makeOutput([4],"int32"),y=n.dataIdMap.get(b.dataId).id;HA(p,Lt[s.dtype],s.shape[0],h,f,g,y,t,0);let v=n.readSync(b.dataId),x;switch(v[0]){case 0:{x=N.getSparseSegmentReductionNegativeSegmentIdsErrorMessage();break}case 1:{x=N.getSparseSegmentReductionNonIncreasingSegmentIdsErrorMessage();break}case 2:x=N.getSparseSegmentReductionSegmentIdOutOfRangeErrorMessage(v[1],v[2]);break;case 3:x=N.getSparseSegmentReductionIndicesOutOfRangeErrorMessage(v[1],v[2],v[3]);break;default:x=""}if(n.disposeData(b.dataId),x)throw n.disposeData(m.dataId),new Error(x);return m}function sme(e){return qA(e,!0)}var ame={kernelName:ad,backendName:"wasm",setupFunc:jA,kernelFunc:sme};function ome(e){return qA(e,!1)}var ime={kernelName:od,backendName:"wasm",setupFunc:jA,kernelFunc:ome};function ume(e){let{inputs:t,attrs:n,backend:r}=e,{x:s}=t,{numOrSizeSplits:a,axis:o}=n,i=w.parseAxisParam(o,s.shape)[0],u=N.prepareSplitSize(s,a,i),l=new Array(s.shape.length).fill(0),c=s.shape.slice();return u.map(d=>{let p=[...c];p[i]=d;let h=lu({inputs:{x:s},attrs:{begin:l,size:p},backend:r});return l[i]+=d,h})}var cme={kernelName:wi,backendName:"wasm",kernelFunc:ume},lme=mn(qa),dme=mn(fc),pme=!0,hme=Pn(Ya,pme),KA;function fme(e){KA=e.wasm.cwrap(eo,null,["number","number","number","number"])}function mme(e){let{backend:t,inputs:n,attrs:r}=e,{alpha:s}=r,{x:a}=n,o=t.dataIdMap.get(a.dataId).id,i=t.makeOutput(a.shape,a.dtype),u=t.dataIdMap.get(i.dataId).id;return KA(o,s,Lt[a.dtype],u),i}var gme={kernelName:eo,backendName:"wasm",setupFunc:fme,kernelFunc:mme},XA;function bme(e){XA=e.wasm.cwrap(ki,null,["number","array","number","array","array","array","array","array","number","number"])}function yme(e){let{backend:t,inputs:n,attrs:r}=e,{x:s}=n,{begin:a,end:o,strides:i,beginMask:u,endMask:l,ellipsisMask:c,newAxisMask:d,shrinkAxisMask:p}=r,{finalShapeSparse:h,finalShape:f,isIdentity:m,sliceDim0:g,isSimpleSlice:b,begin:y,end:v,strides:x}=$t.sliceInfo(s.shape,a,o,i,u,l,c,d,p),k;if(m)k=Kn({inputs:{x:s},backend:t,attrs:{shape:f}});else if(g||b){w.assert(s.shape.length>=1,()=>`Input must have rank at least 1, got: ${s.shape.length}`);let T=$t.computeOutShape(y,v,x),C=lu({inputs:{x:s},backend:t,attrs:{begin:y,size:T}});k=Kn({inputs:{x:C},backend:t,attrs:{shape:f}}),t.disposeData(C.dataId)}else{let T=t.makeOutput(h,"float32"),C=t.dataIdMap.get(
/**
* @license
* Copyright 2017 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use backend file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
2021-04-30 18:01:04 +02:00
* 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-08-31 18:21:57 +02:00
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
2021-04-26 20:45:49 +02:00
/** @license See the LICENSE file. */