!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ml5=e():t.ml5=e()}(window,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="/",n(n.s=693)}([function(t,e,n){"use strict";n.r(e),function(t,r,i,a){n.d(e,"AdadeltaOptimizer",function(){return Nd}),n.d(e,"AdagradOptimizer",function(){return Td}),n.d(e,"AdamOptimizer",function(){return Id}),n.d(e,"AdamaxOptimizer",function(){return Dd}),n.d(e,"Add",function(){return _r}),n.d(e,"AddN",function(){return Er}),n.d(e,"BroadcastTo",function(){return Ir}),n.d(e,"DataStorage",function(){return da}),n.d(e,"Div",function(){return Or}),n.d(e,"ENV",function(){return d}),n.d(e,"Environment",function(){return l}),n.d(e,"FromPixels",function(){return jr}),n.d(e,"FusedBatchNorm",function(){return Sr}),n.d(e,"Identity",function(){return Mr}),n.d(e,"KernelBackend",function(){return ha}),n.d(e,"MaxPoolWithArgmax",function(){return Pr}),n.d(e,"MomentumOptimizer",function(){return Rd}),n.d(e,"NonMaxSuppressionV5",function(){return Tr}),n.d(e,"OneHot",function(){return Dr}),n.d(e,"Optimizer",function(){return Ad}),n.d(e,"PadV2",function(){return Fr}),n.d(e,"RMSPropOptimizer",function(){return Fd}),n.d(e,"Rank",function(){return St}),n.d(e,"Reduction",function(){return pl}),n.d(e,"SGDOptimizer",function(){return Md}),n.d(e,"Square",function(){return Ar}),n.d(e,"SquaredDifference",function(){return Cr}),n.d(e,"Tensor",function(){return Ot}),n.d(e,"TensorBuffer",function(){return wt}),n.d(e,"Tile",function(){return Rr}),n.d(e,"Transpose",function(){return Nr}),n.d(e,"Variable",function(){return It}),n.d(e,"abs",function(){return Wr}),n.d(e,"acos",function(){return Vr}),n.d(e,"acosh",function(){return qr}),n.d(e,"add",function(){return Lr}),n.d(e,"addN",function(){return Ls}),n.d(e,"addStrict",function(){return ki}),n.d(e,"all",function(){return Mc}),n.d(e,"any",function(){return Rc}),n.d(e,"argMax",function(){return Fc}),n.d(e,"argMin",function(){return jc}),n.d(e,"asin",function(){return Hr}),n.d(e,"asinh",function(){return Gr}),n.d(e,"atan",function(){return $r}),n.d(e,"atan2",function(){return _i}),n.d(e,"atanh",function(){return Kr}),n.d(e,"avgPool",function(){return kc}),n.d(e,"avgPool3d",function(){return Oc}),n.d(e,"backend",function(){return mn}),n.d(e,"backend_util",function(){return Ta}),n.d(e,"basicLSTMCell",function(){return Zc}),n.d(e,"batchNorm",function(){return qs}),n.d(e,"batchNorm2d",function(){return $s}),n.d(e,"batchNorm3d",function(){return Ys}),n.d(e,"batchNorm4d",function(){return Qs}),n.d(e,"batchNormalization",function(){return Vs}),n.d(e,"batchNormalization2d",function(){return Gs}),n.d(e,"batchNormalization3d",function(){return Xs}),n.d(e,"batchNormalization4d",function(){return Zs}),n.d(e,"batchToSpaceND",function(){return sr}),n.d(e,"booleanMaskAsync",function(){return Yu}),n.d(e,"broadcastTo",function(){return tu}),n.d(e,"browser",function(){return wd}),n.d(e,"buffer",function(){return ar}),n.d(e,"cast",function(){return ur}),n.d(e,"ceil",function(){return Xr}),n.d(e,"clipByValue",function(){return Yr}),n.d(e,"clone",function(){return eu}),n.d(e,"complex",function(){return Mn}),n.d(e,"concat",function(){return Qn}),n.d(e,"concat1d",function(){return tr}),n.d(e,"concat2d",function(){return er}),n.d(e,"concat3d",function(){return nr}),n.d(e,"concat4d",function(){return rr}),n.d(e,"conv1d",function(){return tc}),n.d(e,"conv2d",function(){return ec}),n.d(e,"conv2dTranspose",function(){return cc}),n.d(e,"conv3d",function(){return nc}),n.d(e,"conv3dTranspose",function(){return lc}),n.d(e,"cos",function(){return Jr}),n.d(e,"cosh",function(){return Zr}),n.d(e,"cumsum",function(){return cr}),n.d(e,"customGrad",function(){return sa}),n.d(e,"deprecationWarn",function(){return Ze}),n.d(e,"depthToSpace",function(){return lr}),n.d(e,"depthwiseConv2d",function(){return ac}),n.d(e,"diag",function(){return fl}),n.d(e,"disableDeprecationWarnings",function(){return Je}),n.d(e,"dispose",function(){return an}),n.d(e,"disposeVariables",function(){return Qe}),n.d(e,"div",function(){return zi}),n.d(e,"divNoNan",function(){return uu}),n.d(e,"divStrict",function(){return Ei}),n.d(e,"dot",function(){return dc}),n.d(e,"dropout",function(){return dl}),n.d(e,"elu",function(){return qc}),n.d(e,"enableDebugMode",function(){return Ye}),n.d(e,"enableProdMode",function(){return Xe}),n.d(e,"engine",function(){return tn}),n.d(e,"env",function(){return f}),n.d(e,"equal",function(){return Ru}),n.d(e,"equalStrict",function(){return Fu}),n.d(e,"erf",function(){return Qr}),n.d(e,"exp",function(){return ti}),n.d(e,"expandDims",function(){return fr}),n.d(e,"expm1",function(){return ei}),n.d(e,"eye",function(){return lu}),n.d(e,"fft",function(){return il}),n.d(e,"fill",function(){return Kn}),n.d(e,"findBackend",function(){return dn}),n.d(e,"findBackendFactory",function(){return hn}),n.d(e,"floor",function(){return ni}),n.d(e,"floorDiv",function(){return Oi}),n.d(e,"frame",function(){return vl}),n.d(e,"fused",function(){return Yl}),n.d(e,"gather",function(){return Ku}),n.d(e,"gatherND",function(){return ll}),n.d(e,"gather_util",function(){return Ui}),n.d(e,"getBackend",function(){return ln}),n.d(e,"getGradient",function(){return g}),n.d(e,"getKernel",function(){return m}),n.d(e,"getKernelsForBackend",function(){return v}),n.d(e,"grad",function(){return na}),n.d(e,"grads",function(){return ra}),n.d(e,"greater",function(){return ju}),n.d(e,"greaterEqual",function(){return Pu}),n.d(e,"greaterEqualStrict",function(){return Lu}),n.d(e,"greaterStrict",function(){return zu}),n.d(e,"hammingWindow",function(){return gl}),n.d(e,"hannWindow",function(){return ml}),n.d(e,"ifft",function(){return al}),n.d(e,"imag",function(){return Fn}),n.d(e,"image",function(){return Wl}),n.d(e,"inTopKAsync",function(){return xl}),n.d(e,"io",function(){return vd}),n.d(e,"irfft",function(){return sl}),n.d(e,"isFinite",function(){return pi}),n.d(e,"isInf",function(){return hi}),n.d(e,"isNaN",function(){return di}),n.d(e,"keep",function(){return on}),n.d(e,"leakyRelu",function(){return Hc}),n.d(e,"less",function(){return Bu}),n.d(e,"lessEqual",function(){return Uu}),n.d(e,"lessEqualStrict",function(){return Wu}),n.d(e,"lessStrict",function(){return Vu}),n.d(e,"linalg",function(){return Fl}),n.d(e,"linspace",function(){return Xn}),n.d(e,"localResponseNormalization",function(){return Yc}),n.d(e,"log",function(){return ri}),n.d(e,"log1p",function(){return ii}),n.d(e,"logSigmoid",function(){return ai}),n.d(e,"logSoftmax",function(){return la}),n.d(e,"logSumExp",function(){return Pc}),n.d(e,"logicalAnd",function(){return nu}),n.d(e,"logicalNot",function(){return ru}),n.d(e,"logicalOr",function(){return iu}),n.d(e,"logicalXor",function(){return au}),n.d(e,"losses",function(){return Tl}),n.d(e,"matMul",function(){return fc}),n.d(e,"math",function(){return bd}),n.d(e,"max",function(){return Lc}),n.d(e,"maxPool",function(){return wc}),n.d(e,"maxPool3d",function(){return Ec}),n.d(e,"maxPoolWithArgmax",function(){return Sc}),n.d(e,"maximum",function(){return Si}),n.d(e,"maximumStrict",function(){return Ci}),n.d(e,"mean",function(){return zc}),n.d(e,"memory",function(){return en}),n.d(e,"min",function(){return Bc}),n.d(e,"minimum",function(){return Ai}),n.d(e,"minimumStrict",function(){return Ni}),n.d(e,"mod",function(){return Ti}),n.d(e,"modStrict",function(){return Ii}),n.d(e,"moments",function(){return Uc}),n.d(e,"movingAverage",function(){return tl}),n.d(e,"mul",function(){return Di}),n.d(e,"mulStrict",function(){return Mi}),n.d(e,"multiRNNCell",function(){return Qc}),n.d(e,"multinomial",function(){return fu}),n.d(e,"neg",function(){return oi}),n.d(e,"nextFrame",function(){return zd}),n.d(e,"norm",function(){return Jc}),n.d(e,"notEqual",function(){return qu}),n.d(e,"notEqualStrict",function(){return Hu}),n.d(e,"oneHot",function(){return du}),n.d(e,"ones",function(){return Gn}),n.d(e,"onesLike",function(){return Jn}),n.d(e,"op",function(){return Dn}),n.d(e,"outerProduct",function(){return hc}),n.d(e,"pad",function(){return hu}),n.d(e,"pad1d",function(){return pu}),n.d(e,"pad2d",function(){return mu}),n.d(e,"pad3d",function(){return gu}),n.d(e,"pad4d",function(){return vu}),n.d(e,"pool",function(){return _c}),n.d(e,"pow",function(){return Ri}),n.d(e,"powStrict",function(){return Fi}),n.d(e,"prelu",function(){return Gc}),n.d(e,"print",function(){return or}),n.d(e,"prod",function(){return Vc}),n.d(e,"profile",function(){return nn}),n.d(e,"rand",function(){return yu}),n.d(e,"randomGamma",function(){return Au}),n.d(e,"randomNormal",function(){return Nu}),n.d(e,"randomUniform",function(){return Tu}),n.d(e,"range",function(){return Yn}),n.d(e,"ready",function(){return cn}),n.d(e,"real",function(){return Rn}),n.d(e,"reciprocal",function(){return si}),n.d(e,"registerBackend",function(){return pn}),n.d(e,"registerGradient",function(){return b}),n.d(e,"registerKernel",function(){return y}),n.d(e,"relu",function(){return $c}),n.d(e,"relu6",function(){return Kc}),n.d(e,"removeBackend",function(){return fn}),n.d(e,"reshape",function(){return dr}),n.d(e,"reverse",function(){return pc}),n.d(e,"reverse1d",function(){return mc}),n.d(e,"reverse2d",function(){return gc}),n.d(e,"reverse3d",function(){return vc}),n.d(e,"reverse4d",function(){return yc}),n.d(e,"rfft",function(){return ol}),n.d(e,"round",function(){return ui}),n.d(e,"rsqrt",function(){return ci}),n.d(e,"scalar",function(){return Ln}),n.d(e,"scatterND",function(){return rl}),n.d(e,"scatter_util",function(){return $i}),n.d(e,"selu",function(){return Xc}),n.d(e,"separableConv2d",function(){return uc}),n.d(e,"serialization",function(){return Od}),n.d(e,"setBackend",function(){return un}),n.d(e,"setPlatform",function(){return gn}),n.d(e,"setdiff1dAsync",function(){return vr}),n.d(e,"sigmoid",function(){return li}),n.d(e,"sign",function(){return fi}),n.d(e,"signal",function(){return bl}),n.d(e,"sin",function(){return mi}),n.d(e,"sinh",function(){return gi}),n.d(e,"slice",function(){return Cc}),n.d(e,"slice1d",function(){return Ac}),n.d(e,"slice2d",function(){return Nc}),n.d(e,"slice3d",function(){return Tc}),n.d(e,"slice4d",function(){return Ic}),n.d(e,"slice_util",function(){return ea}),n.d(e,"softmax",function(){return ca}),n.d(e,"softplus",function(){return vi}),n.d(e,"spaceToBatchND",function(){return hr}),n.d(e,"sparseToDense",function(){return cl}),n.d(e,"spectral",function(){return ul}),n.d(e,"split",function(){return ir}),n.d(e,"sqrt",function(){return yi}),n.d(e,"square",function(){return Iu}),n.d(e,"squaredDifference",function(){return Du}),n.d(e,"squaredDifferenceStrict",function(){return ji}),n.d(e,"squeeze",function(){return pr}),n.d(e,"stack",function(){return mr}),n.d(e,"step",function(){return bi}),n.d(e,"stft",function(){return yl}),n.d(e,"stridedSlice",function(){return el}),n.d(e,"sub",function(){return Pi}),n.d(e,"subStrict",function(){return Li}),n.d(e,"sum",function(){return Wc}),n.d(e,"sumOutType",function(){return Rt}),n.d(e,"tan",function(){return xi}),n.d(e,"tanh",function(){return wi}),n.d(e,"tensor",function(){return jn}),n.d(e,"tensor1d",function(){return zn}),n.d(e,"tensor2d",function(){return Bn}),n.d(e,"tensor3d",function(){return Un}),n.d(e,"tensor4d",function(){return Wn}),n.d(e,"tensor5d",function(){return Vn}),n.d(e,"tensor6d",function(){return qn}),n.d(e,"tensor_util",function(){return zt}),n.d(e,"test_util",function(){return Eu}),n.d(e,"tidy",function(){return rn}),n.d(e,"tile",function(){return cu}),n.d(e,"time",function(){return sn}),n.d(e,"topk",function(){return nl}),n.d(e,"train",function(){return Pd}),n.d(e,"transpose",function(){return fa}),n.d(e,"truncatedNormal",function(){return Mu}),n.d(e,"unregisterGradient",function(){return w}),n.d(e,"unregisterKernel",function(){return x}),n.d(e,"unsortedSegmentSum",function(){return Xu}),n.d(e,"unstack",function(){return gr}),n.d(e,"util",function(){return dt}),n.d(e,"valueAndGrad",function(){return ia}),n.d(e,"valueAndGrads",function(){return aa}),n.d(e,"variable",function(){return Hn}),n.d(e,"variableGrads",function(){return oa}),n.d(e,"version_core",function(){return Sd}),n.d(e,"webgl",function(){return Cd}),n.d(e,"where",function(){return ou}),n.d(e,"whereAsync",function(){return su}),n.d(e,"zeros",function(){return $n}),n.d(e,"zerosLike",function(){return Zn}); /** * @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. * ============================================================================= */ var o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function s(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function u(t,e,n,r){return new(n||(n=Promise))(function(i,a){function o(t){try{u(r.next(t))}catch(t){a(t)}}function s(t){try{u(r.throw(t))}catch(t){a(t)}}function u(t){t.done?i(t.value):new n(function(e){e(t.value)}).then(o,s)}u((r=r.apply(t,e||[])).next())})}function c(t,e){var n,r,i,a,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(a){return function(s){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,r=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(i=(i=o.trys).length>0&&i[i.length-1])&&(6===a[0]||2===a[0])){o=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]0;)r=Math.random()*e|0,n=t[--e],t[e]=t[r],t[r]=n}function E(t,e,n){return Math.max(t,Math.min(e,n))}function O(t){return t%2==0?t:t+1}function S(t){for(var e=0,n=0;n=n?i():setTimeout(o,s)}};o()})}function L(t,e){for(var n=1,r=-1,i=0;i=0)n*=t[i];else if(-1===t[i]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+i);r=i}else if(t[i]<0)throw Error("Shapes can not be < 0. Found "+t[i]+" at dim "+i);if(-1===r){if(e>0&&e!==n)throw Error("Size("+e+") must match the product of shape "+t);return t}if(0===n)throw Error("Cannot infer the missing size in ["+t+"] when there are 0 elements");if(e%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+e+" / "+n);var a=t.slice();return a[r]=e/n,a}function z(t,e){var n=e.length;return C((t=null==t?e.map(function(t,e){return e}):[].concat(t)).every(function(t){return t>=-n&&ts)&&1===t[s]&&(n.push(t[s]),r.push(s)),a[o]<=s&&o++}1!==t[s]&&(n.push(t[s]),r.push(s))}return{newShape:n,keptDims:r}}function U(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else{if("bool"!==t)throw new Error("Unknown data type "+t);n=new Uint8Array(e)}return n}function W(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else if("bool"===t)n=new Uint8Array(e);else{if("string"!==t)throw new Error("Unknown data type "+t);n=new Array(e)}return n}function V(t,e){for(var n=0;n=0;--r)n[r]=n[r+1]*t[r+1];return n}function nt(t,e,n){if("string"===e)throw new Error("Cannot convert a string[] to a TypedArray");if(Array.isArray(t)&&(t=T(t)),n&&V(t,e),function(t,e){return t instanceof Float32Array&&"float32"===e||t instanceof Int32Array&&"int32"===e||t instanceof Uint8Array&&"bool"===e}(t,e))return t;if(null==e||"float32"===e||"complex64"===e)return new Float32Array(t);if("int32"===e)return new Int32Array(t);if("bool"===e){for(var r=new Uint8Array(t.length),i=0;i=0,function(){return"Tensor must have a shape comprised of positive integers but got shape ["+t+"]."})})}function ut(t,e){return void 0===e&&(e="utf-8"),e=e||"utf-8",f().platform.encode(t,e)}function ct(t,e){return void 0===e&&(e="utf-8"),e=e||"utf-8",f().platform.decode(t,e)}function lt(t,e,n){if(0===e)return 0;if(1===e)return t[0];for(var r=t[t.length-1],i=0;i0?h:"")+" "}console.log("%c"+s+"\t%c"+o+"\t%c"+u+"D "+l+"\t%c"+c+"\t%c"+f+"\t%c"+a,"font-weight:bold","color:red","color:blue","color: orange","color: green","color: steelblue")},t}(),mt=20,gt=3,vt=7;function yt(t,e,n){return j(Array.isArray(t)?parseFloat(t[0].toFixed(vt))+" + "+parseFloat(t[1].toFixed(vt))+"j":X(t)?"'"+t+"'":"bool"===n?bt(t):parseFloat(t.toFixed(vt)).toString(),e)}function bt(t){return 0===t?"false":"true"}function xt(t){for(var e=[],n=0;n=this.shape[n]){var o="Requested out of range element at "+t+". Buffer shape="+this.shape;throw new Error(o)}n++}for(var s=t[t.length-1],u=0;u1)for(var c=0;cmt){var l=gt*s,f=Array.from(e.slice(0,l)),d=Array.from(e.slice((u-gt)*s,u*s));return"complex64"===r&&(f=xt(f),d=xt(d)),["["+f.map(function(t,e){return yt(t,a[e],r)}).join(", ")+", ..., "+d.map(function(t,e){return yt(t,a[u-gt+e],r)}).join(", ")+"]"]}return["["+("complex64"===r?xt(e):Array.from(e)).map(function(t,e){return yt(t,a[e],r)}).join(", ")+"]"]}var h=n.slice(1),p=i.slice(1),m=i[0]*s,g=[];if(u>mt){for(var v=0;v0)throw new Error("Backend '"+this.backendName+"' has an internal memory leak ("+o+" data ids) after running '"+t+"'")},t.prototype.runKernelFunc=function(t,e,n,r,i,a,o){var s,u=this,c=[],l=this.isTapeOn();null==r&&(r=null!=this.state.activeScope?this.state.activeScope.name:"");var f,d=this.state.numBytes,h=this.state.numTensors;this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack.push(0);var p,g=m(r,this.backendName);if(null!=g)f=function(){var t=u.backend.numDataIds();p=g.kernelFunc({inputs:e,attrs:i,backend:u.backend});var n=Array.isArray(p)?p:[p];u.shouldCheckForMemLeaks()&&u.checkKernelForMemLeak(r,t,n);var s=n.map(function(t){var e=t.dataId,n=t.shape,r=t.dtype;return u.makeTensorFromDataId(e,n,r)});if(l){var f=u.getTensorsForGradient(r,e,s);if(null==f){null==o&&(o=[]);var d=s.filter(function(t,e){return o[e]});f=(a||[]).slice().concat(d)}c=u.saveTensorsForBackwardMode(f)}return s};else{var v=function(t){l&&(c=t.map(function(t){return u.keep(u.clone(t))}))};f=function(){var e=u.backend.numDataIds();p=u.tidy(function(){return t(u.backend,v)});var n=Array.isArray(p)?p:[p];return u.shouldCheckForMemLeaks()&&u.checkKernelForMemLeak(r,e,n),n}}return this.scopedRun(function(){return u.state.kernelDepth++},function(){return u.state.kernelDepth--},function(){s=u.ENV.getBool("DEBUG")?u.profiler.profileKernel(r,e,function(){return f()}):f()}),l&&this.addTapeNode(r,e,s,n,c,i),this.state.profiling&&this.state.activeProfile.kernels.push({name:r,bytesAdded:this.state.numBytes-d,totalBytesSnapshot:this.state.numBytes,tensorsAdded:this.state.numTensors-h,totalTensorsSnapshot:this.state.numTensors,inputShapes:Object.keys(e).map(function(t){return e[t].shape}),outputShapes:s.map(function(t){return t.shape})}),Array.isArray(p)?s:s[0]},t.prototype.saveTensorsForBackwardMode=function(t){var e=this;return t.map(function(t){return e.keep(e.clone(t))})},t.prototype.getTensorsForGradient=function(t,e,n){var r=g(t);if(null!=r){var i=r.inputsToSave||[],a=r.outputsToSave||[],o=void 0;r.saveAllInputs?(C(Array.isArray(e),function(){return"saveAllInputs is true, expected inputs to be an array."}),o=Object.keys(e).map(function(t){return e[t]})):o=i.map(function(t){return e[t]});var s=n.filter(function(t,e){return a[e]});return o.concat(s)}return null},t.prototype.makeTensor=function(t,e,n,r){if(null==t)throw new Error("Values passed to engine.makeTensor() are null");n=n||"float32",r=r||this.backend;var i=t;"string"===n&&X(t[0])&&(i=t.map(function(t){return ut(t)}));var a=r.write(i,e,n),o=new Ot(e,n,a,this.nextTensorId());if(this.incRef(o,r),"string"===n){var s=this.state.tensorInfo.get(a),u=K(i);this.state.numBytes+=u-s.bytes,s.bytes=u}return o},t.prototype.makeTensorFromDataId=function(t,e,n,r){var i=new Ot(e,n=n||"float32",t,this.nextTensorId());return this.incRef(i,r),i},t.prototype.makeVariable=function(t,e,n,r){void 0===e&&(e=!0),n=n||this.nextVariableId().toString(),null!=r&&r!==t.dtype&&(t=t.asType(r));var i=new It(t,e,n,this.nextTensorId());if(null!=this.state.registeredVariables[i.name])throw new Error("Variable with name "+i.name+" was already registered");return this.state.registeredVariables[i.name]=i,this.incRef(i,this.backend),i},t.prototype.incRef=function(t,e){var n=this.state.tensorInfo.has(t.dataId)?this.state.tensorInfo.get(t.dataId).refCount:0;if(this.state.numTensors++,"string"===t.dtype&&this.state.numStringTensors++,0===n){this.state.numDataBuffers++;var r=0;"complex64"!==t.dtype&&"string"!==t.dtype&&(r=t.size*$(t.dtype)),this.state.tensorInfo.set(t.dataId,{backend:e||this.backend,dtype:t.dtype,shape:t.shape,bytes:r,refCount:0}),this.state.numBytes+=r}this.state.tensorInfo.get(t.dataId).refCount++,t instanceof It||this.track(t)},t.prototype.disposeTensor=function(t){if(this.state.tensorInfo.has(t.dataId)){this.state.numTensors--,"string"===t.dtype&&this.state.numStringTensors--;var e=this.state.tensorInfo.get(t.dataId);e.refCount<=1?("complex64"!==t.dtype&&(this.state.numBytes-=e.bytes),this.state.numDataBuffers--,e.backend.disposeData(t.dataId),this.state.tensorInfo.delete(t.dataId)):this.state.tensorInfo.get(t.dataId).refCount--}},t.prototype.disposeVariables=function(){for(var t in this.state.registeredVariables){var e=this.state.registeredVariables[t];this.disposeVariable(e)}},t.prototype.disposeVariable=function(t){this.disposeTensor(t),null!=this.state.registeredVariables[t.name]&&delete this.state.registeredVariables[t.name]},t.prototype.memory=function(){var t=this.backend.memory();return t.numTensors=this.state.numTensors,t.numDataBuffers=this.state.numDataBuffers,t.numBytes=this.state.numBytes,this.state.numStringTensors>0&&(t.unreliable=!0,null==t.reasons&&(t.reasons=[]),t.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)")),t},t.prototype.profile=function(t){return u(this,void 0,void 0,function(){var e,n;return c(this,function(r){return this.state.profiling=!0,e=this.state.numBytes,n=this.state.numTensors,this.state.activeProfile.kernels=[],this.state.activeProfile.result=t(),this.state.profiling=!1,this.state.activeProfile.peakBytes=Math.max.apply(Math,this.state.activeProfile.kernels.map(function(t){return t.totalBytesSnapshot})),this.state.activeProfile.newBytes=this.state.numBytes-e,this.state.activeProfile.newTensors=this.state.numTensors-n,[2,this.state.activeProfile]})})},t.prototype.isTapeOn=function(){return this.state.gradientDepth>0&&0===this.state.kernelDepth},t.prototype.addTapeNode=function(t,e,n,r,i,a){var o=this,s={id:this.state.nextTapeNodeId++,kernelName:t,inputs:e,outputs:n,saved:i},u=g(t);null!=u&&(r=u.gradFunc),null!=r&&(s.gradient=function(t){return t=t.map(function(t,e){if(null==t){var r=n[e],i=at(r.size,r.dtype);return o.makeTensor(i,r.shape,r.dtype)}return t}),r(t.length>1?t:t[0],i,a)}),this.state.activeTape.push(s)},t.prototype.keep=function(t){return t.kept=!0,t},t.prototype.startTape=function(){0===this.state.gradientDepth&&(this.state.activeTape=[]),this.state.gradientDepth++},t.prototype.endTape=function(){this.state.gradientDepth--},t.prototype.startScope=function(t){var e={track:[],name:"unnamed scope",id:this.state.nextScopeId++};t&&(e.name=t),this.state.scopeStack.push(e),this.state.activeScope=e},t.prototype.endScope=function(t){for(var e=this,n=Pt(t),r=new Set(n.map(function(t){return t.id})),i=0;i0,function(){return"gradients() received an empty list of xs."}),null!=n&&"float32"!==n.dtype)throw new Error("dy must have 'float32' dtype, but has '"+n.dtype+"'");var a=this.scopedRun(function(){return i.startTape()},function(){return i.endTape()},function(){return i.tidy("forward",t)});C(a instanceof Ot,function(){return"The result y returned by f() must be a tensor."});var o=function(t,e,n){for(var r={},i={},a=0;a=0;a--)for(o=(p=t[a]).inputs,l=0;l0)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.");return this.tidy("backward",function(){var t,r,s={};s[a.id]=null==n?(r=it(I(t=a.shape),"float32"),Wt.makeTensor(r,t,"float32")):n,function(t,e,n){for(var r=function(r){var i=e[r],a=[];if(i.outputs.forEach(function(e){var n=t[e.id];null!=n?a.push(n):a.push(null)}),null==i.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+i.kernelName+".");var o=i.gradient(a),s=function(e){if(!(e in o))throw new Error("Cannot backprop through input "+e+". Available gradients found: "+Object.keys(o)+".");var r=n(function(){return o[e]()});if("float32"!==r.dtype)throw new Error("Error in gradient for op "+i.kernelName+". The gradient of input "+e+" must have 'float32' dtype, but has '"+r.dtype+"'");var a=i.inputs[e];if(!D(r.shape,a.shape))throw new Error("Error in gradient for op "+i.kernelName+". The gradient of input '"+e+"' has shape '"+r.shape+"', which does not match the shape of the input '"+a.shape+"'");if(null==t[a.id])t[a.id]=r;else{var s=t[a.id];t[a.id]=s.add(r),s.dispose()}};for(var u in i.inputs)s(u)},i=e.length-1;i>=0;i--)r(i)}(s,o,function(t){return i.tidy(t)});var u=e.map(function(t){return s[t.id]});return 0===i.state.gradientDepth&&(i.state.activeTape.forEach(function(t){for(var e=0,n=t.saved;en||e>n)throw r="["+t+"x"+e+"]",new Error("Requested texture size "+r+" greater than WebGL maximum on this browser / GPU ["+n+"x"+n+"].")}function xe(t,e){return Te(t,e,function(){return t.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function we(t,e,n,r,i,a,o,s){var u=t.getAttribLocation(n,r);return-1!==u&&(ne(t,e,function(){return t.bindBuffer(t.ARRAY_BUFFER,i)}),ne(t,e,function(){return t.vertexAttribPointer(u,a,t.FLOAT,!1,o,s)}),ne(t,e,function(){return t.enableVertexAttribArray(u)}),!0)}function ke(t,e,n,r){Ie(t,r),ne(t,e,function(){return t.activeTexture(t.TEXTURE0+r)}),ne(t,e,function(){return t.bindTexture(t.TEXTURE_2D,n)})}function _e(t,e,n,r){return Te(t,e,function(){return t.getUniformLocation(n,r)},'uniform "'+r+'" not present in program.')}function Ee(t,e,n){return t.getUniformLocation(e,n)}function Oe(t,e,n,r,i,a){ne(t,e,function(){return ke(t,e,r,a)}),ne(t,e,function(){return t.uniform1i(i,a)})}function Se(t,e,n,r){ne(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,r)}),ne(t,e,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,n,0)})}function Ce(t,e,n){ne(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,n)}),ne(t,e,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,null,0)})}function Ae(t){var e=t.checkFramebufferStatus(t.FRAMEBUFFER);if(e!==t.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+Ne(t,e))}function Ne(t,e){switch(e){case t.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case t.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case t.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case t.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return"unknown error "+e}}function Te(t,e,n,r){var i=ne(t,e,function(){return n()});if(null==i)throw new Error(r);return i}function Ie(t,e){var n=t.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=e+t.TEXTURE0;if(rn)throw new Error("textureUnit must be in [gl.TEXTURE0, gl.TEXTURE"+n+"].")}function De(t,e){return void 0===e&&(e=2),I(t.slice(0,t.length-e))}function Me(t){if(0===t.length)throw Error("Cannot get rows and columns of an empty shape array.");return[t.length>1?t[t.length-2]:1,t[t.length-1]]}function Re(t){var e=[1,1,1];return 0===t.length||1===t.length&&1===t[0]||(e=[De(t)].concat(Me(t))),e}function Fe(t,e){var n;void 0===e&&(e=!1);var r=f().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e&&(r*=2,1===(t=t.map(function(e,n){return n>=t.length-2?O(t[n]):t[n]})).length&&(t=[2,t[0]])),2!==t.length){var i=B(t);t=i.newShape}var a=I(t);if(t.length<=1&&a<=r)return[1,a];if(2===t.length&&t[0]<=r&&t[1]<=r)return t;if(3===t.length&&t[0]*t[1]<=r&&t[2]<=r)return[t[0]*t[1],t[2]];if(3===t.length&&t[0]<=r&&t[1]*t[2]<=r)return[t[0],t[1]*t[2]];if(4===t.length&&t[0]*t[1]*t[2]<=r&&t[3]<=r)return[t[0]*t[1]*t[2],t[3]];if(4===t.length&&t[0]<=r&&t[1]*t[2]*t[3]<=r)return[t[0],t[1]*t[2]*t[3]];if(e){var o=De(t),s=2,u=2;return t.length&&(s=(n=Me(t))[0],u=n[1]),F(a=o*(s/2)*(u/2)).map(function(t){return 2*t})}return F(a)}function je(t){return t%2==0}function Pe(t,e){if(D(t=t.slice(-2),e=e.slice(-2)))return!0;if(!t.length||!e.length)return!0;if(0===t[0]||0===t[1]||0===e[0]||0===e[1])return!0;if(t.length!==e.length){var n=t.slice(-1)[0],r=e.slice(-1)[0];if(n===r)return!0;if(je(n)&&je(r)&&(1===t[0]||1===e[0]))return!0}return t[1]===e[1]&&je(t[0])&&je(e[0])}function Le(t){if(null==le){var e=Jt(t);le=e.getParameter(e.MAX_TEXTURE_SIZE)}return le}function ze(t){if(null==fe){var e=Jt(t);fe=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS)}return Math.min(16,fe)}function Be(t){if(0===t)return 0;var e=Jt(t);return Ue(e,"EXT_disjoint_timer_query_webgl2")&&2===t?2:Ue(e,"EXT_disjoint_timer_query")?1:0}function Ue(t,e){return null!=t.getExtension(e)}function We(t){try{if(null!=Jt(t))return!0}catch(t){return!1}return!1}function Ve(t){if(0===t)return!1;var e=Jt(t);if(1===t){if(!Ue(e,"OES_texture_float"))return!1}else if(!Ue(e,"EXT_color_buffer_float"))return!1;return He(e)}function qe(t){if(0===t)return!1;var e=Jt(t);return 1!==t?Ue(e,"EXT_color_buffer_float")?He(e):!!Ue(e,"EXT_color_buffer_half_float")&&function(t,e){var n=ee(t,e),r=t.createTexture();t.bindTexture(t.TEXTURE_2D,r),t.texImage2D(t.TEXTURE_2D,0,n.internalFormatHalfFloat,1,1,0,n.textureFormatFloat,n.textureTypeHalfFloat,null);var i=t.createFramebuffer();t.bindFramebuffer(t.FRAMEBUFFER,i),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,r,0);var a=t.checkFramebufferStatus(t.FRAMEBUFFER)===t.FRAMEBUFFER_COMPLETE;return t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,null),t.deleteTexture(r),t.deleteFramebuffer(i),a}(e,e.getExtension("EXT_color_buffer_half_float")):!!Ue(e,"OES_texture_float")&&!!Ue(e,"WEBGL_color_buffer_float")&&He(e)}function He(t){var e=ee(t),n=t.createTexture();t.bindTexture(t.TEXTURE_2D,n),t.texImage2D(t.TEXTURE_2D,0,e.internalFormatFloat,1,1,0,e.textureFormatFloat,e.textureTypeFloat,null);var r=t.createFramebuffer();t.bindFramebuffer(t.FRAMEBUFFER,r),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,n,0);var i=t.checkFramebufferStatus(t.FRAMEBUFFER)===t.FRAMEBUFFER_COMPLETE;return t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,null),t.deleteTexture(n),t.deleteFramebuffer(r),i}function Ge(t){return 2===t&&null!=Jt(t).fenceSync}var $e=Object.freeze({callAndCheck:ne,canBeRepresented:ae,getWebGLErrorMessage:oe,getExtensionOrThrow:se,createVertexShader:ue,createFragmentShader:ce,createProgram:he,linkProgram:pe,validateProgram:me,createStaticVertexBuffer:ge,createStaticIndexBuffer:ve,getNumChannels:function(){return 2===f().getNumber("WEBGL_VERSION")?1:4},createTexture:ye,validateTextureSize:be,createFramebuffer:xe,bindVertexBufferToProgramAttribute:we,bindTextureUnit:ke,unbindTextureUnit:function(t,e,n){Ie(t,n),ne(t,e,function(){return t.activeTexture(t.TEXTURE0+n)}),ne(t,e,function(){return t.bindTexture(t.TEXTURE_2D,null)})},getProgramUniformLocationOrThrow:_e,getProgramUniformLocation:Ee,bindTextureToProgramUniformSampler:Oe,bindCanvasToFramebuffer:function(t,e){ne(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,null)}),ne(t,e,function(){return t.viewport(0,0,t.canvas.width,t.canvas.height)}),ne(t,e,function(){return t.scissor(0,0,t.canvas.width,t.canvas.height)})},bindColorTextureToFramebuffer:Se,unbindColorTextureFromFramebuffer:Ce,validateFramebuffer:Ae,getFramebufferErrorMessage:Ne,getBatchDim:De,getRowsCols:Me,getShapeAs3D:Re,getTextureShapeFromLogicalShape:Fe,isReshapeFree:Pe,getWebGLMaxTextureSize:Le,resetMaxTextureSize:function(){le=null},resetMaxTexturesInShader:function(){fe=null},getMaxTexturesInShader:ze,getWebGLDisjointQueryTimerVersion:Be,hasExtension:Ue,isWebGLVersionEnabled:We,isCapableOfRenderingToFloatTexture:Ve,isDownloadFloatTextureEnabled:qe,isWebGLFenceEnabled:Ge}),Ke=f();function Xe(){f().set("PROD",!0)}function Ye(){f().set("DEBUG",!0)}function Je(){f().set("DEPRECATION_WARNINGS_ENABLED",!1),console.warn("TensorFlow.js deprecation warnings have been disabled.")}function Ze(t){f().getBool("DEPRECATION_WARNINGS_ENABLED")&&console.warn(t+" You can disable deprecation warnings with tf.disableDeprecationWarnings().")}function Qe(){Wt.disposeVariables()}function tn(){return Wt}function en(){return Wt.memory()}function nn(t){return Wt.profile(t)}function rn(t,e){return Wt.tidy(t,e)}function an(t){Pt(t).forEach(function(t){return t.dispose()})}function on(t){return Wt.keep(t)}function sn(t){return Wt.time(t)}function un(t){return Wt.setBackend(t)}function cn(){return Wt.ready()}function ln(){return Wt.backendName}function fn(t){Wt.removeBackend(t)}function dn(t){return Wt.findBackend(t)}function hn(t){return Wt.findBackendFactory(t)}function pn(t,e,n){return void 0===n&&(n=1),Wt.registerBackend(t,e,n)}function mn(){return Wt.backend}function gn(t,e){f().setPlatform(t,e)}function vn(){for(var t=[],e=0;e0,function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+e.length+" elements"}),C(e.length===n[0],function(){return"Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+e.length+" elements"});for(var i=n.slice(1),a=0;a=0&&(i=r),bn(r,i,e,n),null==t||!G(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t&&"string"!=typeof t){var a=null==t?"null":t.constructor.name;throw new Error("Argument '"+e+"' passed to '"+n+"' must be a Tensor or TensorLike, but got '"+a+"'")}var o=yn(t,i);G(t)||Array.isArray(t)||(t=[t]);var s="string"!==i?nt(t,i,f().getBool("DEBUG")):T(t,[],!0);return Wt.makeTensor(s,o,i)}function wn(t,e,n,r){if(void 0===r&&(r="numeric"),!Array.isArray(t))throw new Error("Argument "+e+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return t.map(function(t,r){return xn(t,e+"["+r+"]",n)},r)}function kn(t,e){for(var n=0;n=0&&e0}),Ke.registerFlag("WEBGL_VERSION",function(){return We(2)?2:We(1)?1:0}),Ke.registerFlag("WEBGL_BUFFER_SUPPORTED",function(){return 2===Ke.get("WEBGL_VERSION")}),Ke.registerFlag("WEBGL_CPU_FORWARD",function(){return!0}),Ke.registerFlag("WEBGL_FORCE_F16_TEXTURES",function(){return!1}),Ke.registerFlag("WEBGL_PACK",function(){return Ke.getBool("HAS_WEBGL")}),Ke.registerFlag("WEBGL_PACK_NORMALIZATION",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_PACK_CLIP",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_PACK_DEPTHWISECONV",function(){return!1}),Ke.registerFlag("WEBGL_PACK_BINARY_OPERATIONS",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_PACK_UNARY_OPERATIONS",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_PACK_ARRAY_OPERATIONS",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_PACK_IMAGE_OPERATIONS",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_PACK_REDUCE",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_LAZILY_UNPACK",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_CONV_IM2COL",function(){return Ke.getBool("WEBGL_PACK")}),Ke.registerFlag("WEBGL_MAX_TEXTURE_SIZE",function(){return Le(Ke.getNumber("WEBGL_VERSION"))}),Ke.registerFlag("WEBGL_MAX_TEXTURES_IN_SHADER",function(){return ze(Ke.getNumber("WEBGL_VERSION"))}),Ke.registerFlag("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",function(){var t=Ke.getNumber("WEBGL_VERSION");return 0===t?0:Be(t)}),Ke.registerFlag("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",function(){return Ke.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&(t=navigator.userAgent||navigator.vendor||window.opera,!(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(t)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(t.substr(0,4))));var t}),Ke.registerFlag("WEBGL_RENDER_FLOAT32_CAPABLE",function(){return Ve(Ke.getNumber("WEBGL_VERSION"))}),Ke.registerFlag("WEBGL_RENDER_FLOAT32_ENABLED",function(){return!Ke.getBool("WEBGL_FORCE_F16_TEXTURES")&&Ke.getBool("WEBGL_RENDER_FLOAT32_CAPABLE")}),Ke.registerFlag("WEBGL_DOWNLOAD_FLOAT_ENABLED",function(){return qe(Ke.getNumber("WEBGL_VERSION"))}),Ke.registerFlag("WEBGL_FENCE_API_ENABLED",function(){return Ge(Ke.getNumber("WEBGL_VERSION"))}),Ke.registerFlag("WEBGL_SIZE_UPLOAD_UNIFORM",function(){return Ke.getBool("WEBGL_RENDER_FLOAT32_ENABLED")?4:0}),Et=Ze;var Mn=Dn({complex_:function(t,e){var n=xn(t,"real","complex"),r=xn(e,"imag","complex");return A(n.shape,r.shape,"real and imag shapes, "+n.shape+" and "+r.shape+", must match in call to tf.complex()."),Wt.runKernelFunc(function(t){return t.complex(n,r)},{$real:n,$imag:r})}}),Rn=Dn({real_:function(t){var e=xn(t,"input","real");return Wt.runKernelFunc(function(t){return t.real(e)},{$input:e})}}),Fn=Dn({imag_:function(t){var e=xn(t,"input","imag");return Wt.runKernelFunc(function(t){return t.imag(e)},{$input:e})}});function jn(t,e,n){return Pn(t,e,yn(t,n),n)}function Pn(t,e,n,r){if(null==r&&(r=Z(t)),"complex64"===r)throw new Error("Cannot construct a complex64 tensor directly. Please use tf.complex(real, imag).");if(!G(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t&&"string"!=typeof t)throw new Error("values passed to tensor(values) must be a number/boolean/string or an array of numbers/booleans/strings, or a TypedArray");if(null!=e){st(e);var i=I(e),a=I(n);C(i===a,function(){return"Based on the provided shape, ["+e+"], the tensor should have "+i+" values but has "+a});for(var o=0;o1)return $n([0],r);var i=at(Math.abs(Math.ceil((e-t)/n)),r);e=1,function(){return"Pass at least one tensor to concat"});var n=wn(t,"tensors","concat");"complex64"===n[0].dtype&&n.forEach(function(t){if("complex64"!==t.dtype)throw new Error("Cannot concatenate complex64 tensors with a tensor\n with dtype "+t.dtype+". ")}),e=z(e,n[0].shape)[0];var r=In(n.map(function(t){return t.shape}),e);if(0===I(r))return jn([],r);if(1===(n=n.filter(function(t){return t.size>0})).length)return n[0];var i=n.map(function(t){return t.shape});Tn(i,e);var a=n,o={axis:e};return Wt.runKernelFunc(function(t){return t.concat(n,e)},a,function(t){var n=i.map(function(t){return t[e]});return ir(t,n,e).map(function(t){return function(){return t}})},"Concat",o)}}),tr=Dn({concat1d_:function(t){return Qn(t,0)}}),er=Dn({concat2d_:function(t,e){return Qn(t,e)}}),nr=Dn({concat3d_:function(t,e){return Qn(t,e)}}),rr=Dn({concat4d_:function(t,e){return Qn(t,e)}}),ir=Dn({split_:function(t,e,n){void 0===n&&(n=0);var r,i=xn(t,"x","split");return n=z(n,i.shape)[0],"number"==typeof e?(C(i.shape[n]%e==0,function(){return"Number of splits must evenly divide the axis."}),r=new Array(e).fill(i.shape[n]/e)):(C(i.shape[n]===e.reduce(function(t,e){return t+e}),function(){return"The sum of sizes must match the size of the axis dimension."}),r=e),Wt.runKernelFunc(function(t){return t.split(i,r,n)},{$x:i},function(t){return{$x:function(){return Qn(t,n)}}})}});function ar(t,e,n){return void 0===e&&(e="float32"),e=e||"float32",st(t),new wt(t,e,n)}function or(t,e){void 0===e&&(e=!1),console.log(t.toString(e))}var sr=Dn({batchToSpaceND_:function(t,e,n){var r=xn(t,"x","batchToSpaceND"),i=e.reduce(function(t,e){return t*e});return C(r.rank>=1+e.length,function(){return"input rank is "+r.rank+" but should be > than blockShape.length "+e.length}),C(n.length===e.length,function(){return"crops.length is "+n.length+" but should be equal to blockShape.length "+e.length}),C(r.shape[0]%i==0,function(){return"input tensor batch is "+r.shape[0]+" but is not divisible by the product of the elements of blockShape "+e.join(" * ")+" === "+i}),Wt.runKernelFunc(function(t){return t.batchToSpaceND(r,e,n)},{$x:r},function(t){return{$x:function(){return t.spaceToBatchND(e,n)}}})}}),ur=Dn({cast_:function(t,e){var n=xn(t,"x","cast");if(!q(e))throw new Error("Failed to cast to unknown dtype "+e);if("string"===e&&"string"!==n.dtype||"string"!==e&&"string"===n.dtype)throw new Error("Only strings can be casted to strings");var r={dtype:e};return Wt.runKernelFunc(function(t){return t.cast(n,e)},{x:n},function(t){return{x:function(){return t.clone()}}},"Cast",r)}}),cr=Dn({cumsum_:function(t,e,n,r){void 0===e&&(e=0),void 0===n&&(n=!1),void 0===r&&(r=!1);var i=xn(t,"x","cumsum"),a=Cn([e|=0],i.rank),o=i;null!=a&&(o=i.transpose(a));var s=Nn(1,i.rank)[0],u=Wt.runKernelFunc(function(t){return t.cumsum(o,s,n,r)},{permutedX:o},function(t){return{permutedX:function(){return t.cumsum(e,n,!r)}}});return null!=a&&(u=u.transpose(a)),u}}),lr=Dn({depthToSpace_:function(t,e,n){void 0===n&&(n="NHWC");var r=xn(t,"x","depthToSpace"),i="NHWC"===n?r.shape[1]:r.shape[2],a="NHWC"===n?r.shape[2]:r.shape[3],o="NHWC"===n?r.shape[3]:r.shape[1];return C(i*e>=0,function(){return"Negative dimension size caused by overflow when multiplying\n "+i+" and "+e+" for depthToSpace with input shape\n "+r.shape}),C(a*e>=0,function(){return"Negative dimension size caused by overflow when multiplying\n "+a+" and "+e+" for depthToSpace with input shape\n "+r.shape}),C(o%(e*e)==0,function(){return"Dimension size must be evenly divisible by "+e*e+" but is "+o+" for depthToSpace with input shape "+r.shape}),Wt.runKernelFunc(function(t){return t.depthToSpace(r,e,n)},{$x:r})}}),fr=Dn({expandDims_:function(t,e){void 0===e&&(e=0);var n=xn(t,"x","expandDims",null);C(e<=n.rank,function(){return"Axis must be <= rank of the tensor"});var r=n.shape.slice();return e<0&&(C(-(n.rank+1)<=e,function(){return"Axis must be in the interval ["+-(n.rank+1)+", "+n.rank+"]"}),e=n.rank+e+1),r.splice(e,0,1),dr(n,r)}}),dr=Dn({reshape_:function(t,e){var n=xn(t,"x","reshape",null);e=L(e,n.size),C(n.size===I(e),function(){return"new shape and old shape must have the same number of elements."});var r={shape:e};return Wt.runKernelFunc(function(t){return t.reshape(n,e)},{x:n},function(t){return{x:function(){return t.reshape(n.shape)}}},"Reshape",r)}}),hr=Dn({spaceToBatchND_:function(t,e,n){var r=xn(t,"x","spaceToBatchND");return C(r.rank>=1+e.length,function(){return"input rank "+r.rank+" should be > than [blockShape] "+e.length}),C(n.length===e.length,function(){return"paddings.shape[0] "+n.length+" must be equal to [blockShape] "+e.length}),C(r.shape.reduce(function(t,r,i){return i>0&&i<=e.length?t&&(r+n[i-1][0]+n[i-1][1])%e[i-1]==0:t},!0),function(){return"input spatial dimensions "+r.shape.slice(1)+" with paddings "+n.toString()+" must be divisible by blockShapes "+e.toString()}),Wt.runKernelFunc(function(t){return t.spaceToBatchND(r,e,n)},{$x:r},function(t){return{$x:function(){return t.batchToSpaceND(e,n)}}})}}),pr=Dn({squeeze_:function(t,e){var n=xn(t,"x","squeeze");return dr(n,B(n.shape,e).newShape)}}),mr=Dn({stack_:function(t,e){void 0===e&&(e=0);var n=wn(t,"tensors","stack");if(C(n.length>=1,function(){return"Pass at least one tensor to tf.stack"}),1===n.length)return n[0].expandDims(e);var r=n[0].rank,i=n[0].shape,a=n[0].dtype;C(e<=r,function(){return"Axis must be <= rank of the tensor"}),n.forEach(function(t){A(i,t.shape,"All tensors passed to stack must have matching shapes")}),n.forEach(function(t){C(a===t.dtype,function(){return"All tensors passed to stack must have matching dtypes"})});var o=n.map(function(t){return t.expandDims(e)});return Qn(o,e)}}),gr=Dn({unstack_:function(t,e){void 0===e&&(e=0),e=e||0;var n=xn(t,"x","unstack");C(e>=-n.shape.length&&e=2*e+1||i%2==1?o.push(i):a.push(i);r.push.apply(r,a),r.push(0),r.push.apply(r,o)}return r}function xr(t,e,n,r){void 0===r&&(r=!0);var i=[];r?i.push(t[0]/n):i.push(t[0]*n);for(var a=1;a1&&1===o&&r.unshift(a)}return r}function Br(t,e){for(var n=[],r=0;r1)&&n.unshift(a)}return n}function Ur(t,e){for(var n=[],r=Math.max(t.length,e.length),i=0;i0&&(i=i.sum(o)),i.reshape(n.shape)},$b:function(){var e=Lr(n.square(),r.square()),i=oi(t.mul(n.div(e))),o=Br(r.shape,a);return o.length>0&&(i=i.sum(o)),i.reshape(r.shape)}}})}}),Ei=Dn({divStrict_:function(t,e){var n=xn(t,"a","div"),r=xn(e,"b","div");return A(n.shape,r.shape,"Error in divideStrict: "),n.div(r)}}),Oi=Dn({floorDiv_:function(t,e){var n,r=xn(t,"a","floorDiv"),i=xn(e,"b","floorDiv");n=Ft(r,i),r=n[0],i=n[1];var a=Ur(r.shape,i.shape);return Wt.runKernelFunc(function(t,e){var n=t.floorDiv(r,i);return e([r,i]),n},{a:r,b:i},function(t,e){var n=e[0],r=e[1];return{a:function(){var e=t.div(r.toFloat()),i=Br(n.shape,a);return i.length>0?e.sum(i).reshape(n.shape):e},b:function(){var e=t.mul(n.toFloat()),i=Br(r.shape,a);i.length>0&&(e=e.sum(i).reshape(r.shape));var o=r.square();return e.div(o.toFloat()).neg()}}},"FloorDiv")}}),Si=Dn({maximum_:function(t,e){var n,r=xn(t,"a","maximum"),i=xn(e,"b","maximum");return n=Ft(r,i),r=n[0],i=n[1],"bool"===r.dtype&&(r=r.toInt(),i=i.toInt()),Ur(r.shape,i.shape),Wt.runKernelFunc(function(t,e){var n=t.maximum(r,i);return e([r,i]),n},{a:r,b:i},function(t,e){var n=e[0],r=e[1];return{a:function(){return t.mul(n.greaterEqual(r).toFloat())},b:function(){return t.mul(n.less(r).toFloat())}}},"Maximum")}}),Ci=Dn({maximumStrict_:function(t,e){var n=xn(t,"a","maximumStrict"),r=xn(e,"b","maximumStrict");return A(n.shape,r.shape,"Error in maximumStrict: "),n.maximum(r)}}),Ai=Dn({minimum_:function(t,e){var n,r=xn(t,"a","minimum"),i=xn(e,"b","minimum");return n=Ft(r,i),r=n[0],i=n[1],"bool"===r.dtype&&(r=r.toInt(),i=i.toInt()),Ur(r.shape,i.shape),Wt.runKernelFunc(function(t,e){var n=t.minimum(r,i);return e([r,i]),n},{a:r,b:i},function(t,e){var n=e[0],r=e[1];return{a:function(){return t.mul(n.lessEqual(r).toFloat())},b:function(){return t.mul(n.greater(r).toFloat())}}},"Minimum")}}),Ni=Dn({minimumStrict_:function(t,e){var n=xn(t,"a","minimumStrict"),r=xn(e,"b","minimumStrict");return A(n.shape,r.shape,"Error in minimumStrict: "),n.minimum(r)}}),Ti=Dn({mod_:function(t,e){var n,r=xn(t,"a","mod"),i=xn(e,"b","mod");n=Ft(r,i),r=n[0],i=n[1];var a=Ur(r.shape,i.shape);return Wt.runKernelFunc(function(t,e){var n=t.mod(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){var e=Br(n.shape,a);return e.length>0?t.sum(e).reshape(n.shape):t},$b:function(){var e=t.mul(n.div(r).floor().neg()),i=Br(r.shape,a);return i.length>0?e.sum(i).reshape(r.shape):e}}})}}),Ii=Dn({modStrict_:function(t,e){var n=xn(t,"a","modStrict"),r=xn(e,"b","modStrict");return A(n.shape,r.shape,"Error in modStrict: "),n.mod(r)}}),Di=Dn({mul_:function(t,e){var n,r=xn(t,"a","mul"),i=xn(e,"b","mul");n=Ft(r,i),r=n[0],i=n[1];var a=Ur(r.shape,i.shape);return Wt.runKernelFunc(function(t,e){var n=t.multiply(r,i);return e([r,i]),n},{a:r,b:i},function(t,e){var n=e[0],r=e[1];return{a:function(){var e=t.mul(r.toFloat()),i=Br(n.shape,a);return i.length>0?e.sum(i).reshape(n.shape):e},b:function(){var e=t.mul(n.toFloat()),i=Br(r.shape,a);return i.length>0?e.sum(i).reshape(r.shape):e}}},"Mul")}}),Mi=Dn({mulStrict_:function(t,e){var n=xn(t,"a","mul"),r=xn(e,"b","mul");return A(n.shape,r.shape,"Error in multiplyStrict: "),n.mul(r)}}),Ri=Dn({pow_:function(t,e){var n,r=xn(t,"base","pow"),i=xn(e,"exp","pow");n=Ft(r,i),r=n[0],i=n[1];var a=Ur(r.shape,i.shape),o=[r,i];return Wt.runKernelFunc(function(t,e){var n=t.pow(r,i);return e([r,i,n]),n},{a:r,b:i},function(t,e){var n=e[0],r=e[1],i=e[2];return{a:function(){var e=r.toFloat(),i=t.mul(e.mul(n.pow(e.sub(Ln(1))))),o=Br(n.shape,a);return o.length>0&&(i=i.sum(o)),i.reshape(n.shape)},b:function(){var e=n.greater(0),o=n.log().where(e,Zn(n)),s=t.mul(i.mul(o)),u=Br(r.shape,a);return u.length>0&&(s=s.sum(u)),s.reshape(r.shape)}}},"Pow",{},o,[!0])}}),Fi=Dn({powStrict_:function(t,e){return A(t.shape,e.shape,"Error in powStrict: "),t.pow(e)}}),ji=Dn({squaredDifferenceStrict_:function(t,e){var n=xn(t,"a","squaredDifferenceStrict"),r=xn(e,"b","squaredDifferenceStrict");return A(n.shape,r.shape,"Error in squaredDifferenceStrict: "),n.squaredDifference(r)}}),Pi=Dn({sub_:function(t,e){var n,r=xn(t,"a","sub"),i=xn(e,"b","sub");n=Ft(r,i),r=n[0],i=n[1];var a=Ur(r.shape,i.shape);return Wt.runKernelFunc(function(t){return t.subtract(r,i)},{a:r,b:i},function(t){return{a:function(){var e=t,n=Br(r.shape,a);return n.length>0&&(e=e.sum(n)),e.reshape(r.shape)},b:function(){var e=t,n=Br(i.shape,a);return n.length>0&&(e=e.sum(n)),e.neg().reshape(i.shape)}}},"Sub")}}),Li=Dn({subStrict_:function(t,e){var n=xn(t,"a","subStrict"),r=xn(e,"b","subStrict");return A(n.shape,r.shape,"Error in subStrict: "),n.sub(r)}}),zi=Dn({div_:function(t,e){var n,r=xn(t,"a","div"),i=xn(e,"b","div");if(n=Ft(r,i),r=n[0],i=n[1],"int32"===r.dtype&&"int32"===i.dtype)return Oi(r,i);var a={a:r,b:i};return Wt.runKernelFunc(function(t,e){var n=t.realDivide(r,i);return e([r,i]),n},a,null,Or,{})}});function Bi(t,e){if(t.rank<1)throw new Error("tf.gatherND() expects the input to be rank 1 or higher, but the rank was "+t.rank+".");if(e.rank<1)throw new Error("tf.gatherND() expects the indices to be rank 1 or higher, but the rank was "+e.rank+".");if("int32"!==e.dtype)throw new Error("tf.gatherND() expects the indices to be int32 type, but the dtype was "+e.dtype+".");if(e.shape[e.rank-1]>t.rank)throw new Error("index innermost dimension length must be <= tensor rank; saw: "+e.shape[e.rank-1]+" vs. "+t.rank);if(0===t.size)throw new Error("Requested more than 0 entries, but input is empty. Input shape: "+t.shape+".");for(var n=e.shape,r=n[n.length-1],i=1,a=0;a1?e.shape[e.rank-1]:1,i=e.rank>1?e.rank-1:1,a="Must have updates.shape = indices.shape[:batchDim] + shape[sliceDim:], got updates.shape: "+n.shape+", indices.shape: "+e.shape+", shape: "+t+", sliceDim: "+r+", and batchDim: "+i+".";if(n.rank1?e.shape[r-1]:1,a=n.length,o=1,s=i;s0;)1&t&&e.push(n),t/=2,n++;return e}function Yi(t,e,n){for(var r=[],i=0;i0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var s=r[i];return a<0&&(a+=s),E(0,a,s-1)}function Zi(t,e,n,r,i){var a=e[i],o=n[i]||1;(t&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var s=r[i];return a<0&&(a+=s),o>0?E(0,a,s):E(-1,a,s-1)}function Qi(t,e,n){for(var r=n.length,i=0;i1){r=i;break}for(i=r+1;i0||n[i]!==t[i])return!1;return!0}function ta(t,e){for(var n=t.length>0?t[t.length-1]:1,r=0;r0,function(){return"variableGrads() expects at least one of the input variables to be trainable, but none of the "+a+" variables is trainable."});var o=Wt.gradients(t,e,null,!0),s=o.value,u=o.grads;C(u.some(function(t){return null!=t}),function(){return"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()."}),C(0===s.rank,function(){return"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+s.rank+" tensor"});var c={};return e.forEach(function(t,e){null!=u[e]&&(c[t.name]=u[e])}),null!=i&&i.forEach(function(t){return c[t.name]=null}),{value:s,grads:c}}function sa(t){return Wt.customGrad(t)}function ua(t){if(t.filter(function(t){return null==t}).length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that\n the f you passed encloses all operations that lead from x to y.")}var ca=Dn({softmax_:function(t,e){void 0===e&&(e=-1);var n=xn(t,"logits","softmax","float32");if(-1===e&&(e=n.rank-1),e!==n.rank-1)throw Error("Softmax along a non-last dimension is not yet supported. Logits was rank "+n.rank+" and dim was "+e);return Wt.runKernelFunc(function(t,r){var i=t.softmax(n,e);return r([i]),i},{logits:n},function(t,n){var r=n[0],i=t.mul(r);return{logits:function(){return i.sub(i.sum([e],!0).mul(r))}}},"Softmax",{dim:e},[],[!0])}}),la=Dn({logSoftmax_:function(t,e){void 0===e&&(e=-1);var n=xn(t,"logits","logSoftmax");if(-1===e&&(e=n.rank-1),e!==n.rank-1)throw Error("Log Softmax along a non-last dimension is not yet supported. Logits was rank "+n.rank+" and axis was "+e);return sa(function(t,n){var r=t.max(e,!0),i=t.sub(r),a=i.toFloat().sub(i.exp().sum(e,!0).log());return n([a]),{value:a,gradFunc:function(t,n){var r=n[0].exp();return t.sub(t.sum(e,!0).mul(r))}}})(n)}}),fa=Dn({transpose_:function(t,e){var n=xn(t,"x","transpose");if(null==e&&(e=n.shape.map(function(t,e){return e}).reverse()),C(n.rank===e.length,function(){return"Error in transpose: rank of input "+n.rank+" must match length of perm "+e+"."}),e.forEach(function(t){C(t>=0&&t>>1)]);s>0?r=a+1:(i=a,o=!s)}return o?r:-r-1}(t,e,n||ja)}(t,e,n),i=r<0?-(r+1):r;t.splice(i,0,e)}function ja(t,e){return t>e?1:ti}).sort(Wa),c=a>0?-.5/a:0,l=[],f=[];l.length0;){var d=u.pop(),h=d.score,p=d.boxIndex,m=d.suppressBeginIndex;if(h=m;--v){var y=Ba(t,p,l[v]);if(y>=r){g=!0;break}if(d.score=d.score*Ua(r,c,y),d.score<=i)break}d.suppressBeginIndex=l.length,g||(d.score===h?(l.push(p),f.push(d.score)):d.score>i&&Fa(u,d,Wa))}var b=l.length;return s&&(l.fill(0,b),f.fill(0,b)),{selectedIndices:zn(l,"int32"),selectedScores:zn(f,"float32"),numValidOutputs:Ln(b,"int32")}}function Ba(t,e,n){var r=t.subarray(4*e,4*e+4),i=t.subarray(4*n,4*n+4),a=Math.min(r[0],r[2]),o=Math.min(r[1],r[3]),s=Math.max(r[0],r[2]),u=Math.max(r[1],r[3]),c=Math.min(i[0],i[2]),l=Math.min(i[1],i[3]),f=Math.max(i[0],i[2]),d=Math.max(i[1],i[3]),h=(s-a)*(u-o),p=(f-c)*(d-l);if(h<=0||p<=0)return 0;var m=Math.max(a,c),g=Math.max(o,l),v=Math.min(s,f),y=Math.min(u,d),b=Math.max(v-m,0)*Math.max(y-g,0);return b/(h+p-b)}function Ua(t,e,n){var r=Math.exp(e*n*n);return n<=t?r:0}function Wa(t,e){return t.score-e.score||t.score===e.score&&e.boxIndex-t.boxIndex}function Va(t,e,n){var r=new Array(t.rank).fill(0),i=t.shape.slice();return e.map(function(e){i[n]=e;var a=t.slice(r,i);return r[n]+=e,a})}function qa(t,e){for(var n=new Array(t.rank),r=0;r1?"["+e+"]":"")+";"):(i.push("uniform sampler2D "+t.name+";"),i.push("uniform int offset"+t.name+";"))});var a,o,s=i.join("\n"),u=t.map(function(t){return function(t,e,n){void 0===n&&(n=!1);var r="";r+=n?function t(e){var n,r,i;switch(e.shapeInfo.logicalShape.length){case 0:return n=e.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),i=Xa(),"\n vec4 "+r+"() {\n return "+i.texture2D+"("+n+", halfCR);\n }\n ";case 1:return function(t){var e=t.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1),r=t.shapeInfo.texShape,i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)],a=Xa();return"\n vec4 "+n+"(int index) {\n vec2 uv = packedUVfrom1D(\n "+i[0]+", "+i[1]+", index);\n return "+a.texture2D+"("+e+", uv);\n }\n "}(e);case 2:return function(t){var e=t.shapeInfo.logicalShape,n=t.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),i=t.shapeInfo.texShape,a=i[0],o=i[1],s=Xa();if(null!=i&&D(e,i))return"\n vec4 "+r+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+o+".0, "+a+".0);\n\n return "+s.texture2D+"("+n+", uv);\n }\n ";var u=[Math.ceil(i[0]/2),Math.ceil(i[1]/2)],c=Math.ceil(e[1]/2);return"\n vec4 "+r+"(int row, int col) {\n vec2 uv = packedUVfrom2D("+c+", "+u[0]+", "+u[1]+", row, col);\n return "+s.texture2D+"("+n+", uv);\n }\n "}(e);case 3:return function(e){var n=e.shapeInfo.logicalShape,r=e.name,i="get"+r.charAt(0).toUpperCase()+r.slice(1),a=e.shapeInfo.texShape,o=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)];if(1===n[0]){var s=n.slice(1),u=so(e,s);return"\n "+t(u)+"\n vec4 "+i+"(int b, int row, int col) {\n return "+i+"("+uo(["b","row","col"],[1,2])+");\n }\n "}var c=o[0],l=o[1],f=Math.ceil(n[2]/2),d=f*Math.ceil(n[1]/2),h=Xa();return"\n vec4 "+i+"(int b, int row, int col) {\n vec2 uv = packedUVfrom3D(\n "+c+", "+l+", "+d+", "+f+", b, row, col);\n return "+h.texture2D+"("+r+", uv);\n }\n "}(e);default:return function(t){for(var e=t.shapeInfo.logicalShape,n=e.length,r=t.name,i="get"+r.charAt(0).toUpperCase()+r.slice(1),a=t.shapeInfo.texShape,o=[Math.ceil(a[0]/2),Math.ceil(a[1]/2)],s=o[0],u=o[1],c=Math.ceil(e[n-1]/2),l=c*Math.ceil(e[n-2]/2),f="int b, int row, int col",d="b * "+l+" + (row / 2) * "+c+" + (col / 2)",h=2;h=1?"coords = 0;":u.map(function(t){return"coords."+f[t+l]+" = 0;"}).join("\n");var d;d=s<2&&o>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return"coords."+f[e+l]}).join(", ");var h="return outputValue;",p=1===I(t.shapeInfo.logicalShape),m=1===I(e.logicalShape);if(1!==o||p||m){if(p&&!m)h=1===s?"\n return vec4(outputValue.x, outputValue.x, 0., 0.);\n ":"\n return vec4(outputValue.x);\n ";else if(u.length){var g=o-2,v=o-1;u.indexOf(g)>-1&&u.indexOf(v)>-1?h="return vec4(outputValue.x);":u.indexOf(g)>-1?h="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":u.indexOf(v)>-1&&(h="return vec4(outputValue.xx, outputValue.zz);")}}else h="\n return vec4(outputValue.xy, outputValue.xy);\n ";return"\n vec4 "+a+"() {\n "+c+" coords = getOutputCoords();\n "+n+"\n vec4 outputValue = get"+i+"("+d+");\n "+h+"\n }\n "}(t,e):function(t,e){var n=t.name,r=n.charAt(0).toUpperCase()+n.slice(1),i="get"+r+"AtOutCoords",a=e.texShape,o=t.shapeInfo.texShape,s=t.shapeInfo.logicalShape.length,u=e.logicalShape.length;if(!t.shapeInfo.isUniform&&s===u&&null==t.shapeInfo.flatOffset&&D(o,a))return"\n float "+i+"() {\n return sampleTexture("+n+", resultUV);\n }\n ";var c=oo(u),l=zr(t.shapeInfo.logicalShape,e.logicalShape),f=u-s,d=["x","y","z","w","u","v"];return"\n float "+i+"() {\n "+c+" coords = getOutputCoords();\n "+(0===s?"":u<2&&l.length>=1?"coords = 0;":l.map(function(t){return"coords."+d[t+f]+" = 0;"}).join("\n"))+"\n return get"+r+"("+(u<2&&s>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return"coords."+d[e+f]}).join(", "))+");\n }\n "}(t,e)),r}(t,e,r)}).join("\n"),c=e.texShape,l=Xa(),f="\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return "+l.texture2D+"(textureSampler, uv).r;\n }\n ",d=function(t){return t.version+"\n precision highp float;\n precision highp int;\n precision highp sampler2D;\n "+t.varyingFs+" vec2 resultUV;\n "+t.defineOutput+"\n const vec2 halfCR = vec2(0.5, 0.5);\n\n struct ivec5\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n };\n\n struct ivec6\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n int v;\n };\n\n uniform float NAN;\n "+t.defineSpecialNaN+"\n "+t.defineSpecialInf+"\n "+t.defineRound+"\n\n int imod(int x, int y) {\n return x - y * (x / y);\n }\n\n int idiv(int a, int b, float sign) {\n int res = a / b;\n int mod = imod(a, b);\n if (sign < 0. && mod != 0) {\n res -= 1;\n }\n return res;\n }\n\n //Based on the work of Dave Hoskins\n //https://www.shadertoy.com/view/4djSRW\n #define HASHSCALE1 443.8975\n float random(float seed){\n vec2 p = resultUV * seed;\n vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);\n p3 += dot(p3, p3.yzx + 19.19);\n return fract((p3.x + p3.y) * p3.z);\n }\n\n "+to+"\n "+eo+"\n "+no+"\n "}(l);return e.isPacked?(a=function(t,e){switch(t.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 1:return function(t,e){var n=[Math.ceil(e[0]/2),Math.ceil(e[1]/2)];return 1===n[0]?"\n int getOutputCoords() {\n return 2 * int(resultUV.x * "+n[1]+".0);\n }\n ":1===n[1]?"\n int getOutputCoords() {\n return 2 * int(resultUV.y * "+n[0]+".0);\n }\n ":"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n return 2 * (resTexRC.x * "+n[1]+" + resTexRC.y);\n }\n "}(0,e);case 2:return function(t,e){var n=[Math.ceil(e[0]/2),Math.ceil(e[1]/2)];if(D(t,e))return"\n ivec2 getOutputCoords() {\n return 2 * ivec2(resultUV.yx * vec2("+n[0]+", "+n[1]+"));\n }\n ";var r=Math.ceil(t[1]/2);return"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n int r = 2 * (index / "+r+");\n int c = imod(index, "+r+") * 2;\n\n return ivec2(r, c);\n }\n "}(t,e);case 3:return n=t,r=e,i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)],o=(a=Math.ceil(n[2]/2))*Math.ceil(n[1]/2),"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+i[0]+", "+i[1]+"));\n int index = resTexRC.x * "+i[1]+" + resTexRC.y;\n\n int b = index / "+o+";\n index -= b * "+o+";\n\n int r = 2 * (index / "+a+");\n int c = imod(index, "+a+") * 2;\n\n return ivec3(b, r, c);\n }\n ";default:return function(t,e){for(var n=[Math.ceil(e[0]/2),Math.ceil(e[1]/2)],r=Math.ceil(t[t.length-1]/2),i=r*Math.ceil(t[t.length-2]/2),a=i,o="",s="b, r, c",u=2;u= "+this.outputShape[0]+" ? 0. : result.y;\n result.z = 0.;\n result.w = 0.;\n ";else{var o=Ka("coords",i);a+="\n bool nextRowOutOfBounds =\n ("+o[i-2]+" + 1) >= "+this.outputShape[i-2]+";\n bool nextColOutOfBounds =\n ("+o[i-1]+" + 1) >= "+this.outputShape[i-1]+";\n result.y = nextColOutOfBounds ? 0. : result.y;\n result.z = nextRowOutOfBounds ? 0. : result.z;\n result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;\n "}this.userCode="\n vec4 binaryOperation(vec4 a, vec4 b) {\n "+t+"\n }\n\n void main() {\n vec4 a = getAAtOutCoords();\n vec4 b = getBAtOutCoords();\n\n vec4 result = binaryOperation(a, b);\n "+a+"\n\n setOutput(result);\n }\n "},yo=function(){function t(t){this.variableNames=["A"],this.outputShape=t,this.userCode="\n uniform float minVal;\n uniform float maxVal;\n\n void main() {\n float value = getAAtOutCoords();\n if (isnan(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, minVal, maxVal));\n }\n "}return t.prototype.getCustomSetupFunc=function(t,e){var n=this;return function(r,i){null==n.minLoc&&(n.minLoc=r.getUniformLocationNoThrow(i,"minVal"),n.maxLoc=r.getUniformLocationNoThrow(i,"maxVal")),r.gl.uniform1f(n.minLoc,t),r.gl.uniform1f(n.maxLoc,e)}},t}(),bo=function(){function t(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.userCode="\n uniform float minVal;\n uniform float maxVal;\n\n void main() {\n vec4 value = getAAtOutCoords();\n\n if (any(isnan(value))) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, vec4(minVal), vec4(maxVal)));\n }\n "}return t.prototype.getCustomSetupFunc=function(t,e){var n=this;return function(r,i){null==n.minLoc&&(n.minLoc=r.getUniformLocationNoThrow(i,"minVal"),n.maxLoc=r.getUniformLocationNoThrow(i,"maxVal")),r.gl.uniform1f(n.minLoc,t),r.gl.uniform1f(n.maxLoc,e)}},t}();function xo(t,e,n){var r=t.indexOf(e);return t.map(function(t,e){return e===r?t+" - "+n:t}).join()}var wo=function(t,e,n,r){void 0===e&&(e=!1),void 0===n&&(n=null),void 0===r&&(r=!1),this.variableNames=["x","W"],this.outputShape=t.outShape;var i=t.padInfo.top,a=t.padInfo.left,o=t.strideHeight,s=t.strideWidth,u=t.dilationHeight,c=t.dilationWidth,l=t.filterHeight,f=t.filterWidth,d=4*Math.floor(t.inChannels/4),h=t.inChannels%4,p="channelsLast"===t.dataFormat,m=p?1:2,g=p?2:3,v=p?3:1,y="",b="";n&&(y=r?"float activation(float a) {\n float b = getPreluActivationWeightsAtOutCoords();\n "+n+"\n }":"\n float activation(float x) {\n "+n+"\n }\n ",b="result = activation(result);");var x=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.userCode="\n "+y+"\n\n const ivec2 strides = ivec2("+o+", "+s+");\n const ivec2 pads = ivec2("+i+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords["+v+"];\n\n ivec2 xRCCorner =\n ivec2(coords["+m+"], coords["+g+"]) * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+l+"; wR++) {\n int xR = xRCorner + wR * "+u+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+f+"; wC++) {\n int xC = xCCorner + wC * "+c+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+d+"; d1 += 4) {\n vec4 wValues = vec4(\n getW(wR, wC, d1, d2),\n getW(wR, wC, d1 + 1, d2),\n getW(wR, wC, d1 + 2, d2),\n getW(wR, wC, d1 + 3, d2)\n );\n\n if ("+p+") {\n vec4 xValues = vec4(\n getX(batch, xR, xC, d1),\n getX(batch, xR, xC, d1 + 1),\n getX(batch, xR, xC, d1 + 2),\n getX(batch, xR, xC, d1 + 3)\n );\n dotProd += dot(xValues, wValues);\n } else {\n vec4 xValues = vec4(\n getX(batch, d1, xR, xC),\n getX(batch, d1 + 1, xR, xC),\n getX(batch, d1 + 2, xR, xC),\n getX(batch, d1 + 3, xR, xC)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n\n if ("+(1===h)+") {\n\n if ("+p+") {\n dotProd +=\n getX(batch, xR, xC, "+d+") *\n getW(wR, wC, "+d+", d2);\n } else {\n dotProd +=\n getX(batch, "+d+", xR, xC) *\n getW(wR, wC, "+d+", d2);\n }\n\n } else if ("+(2===h)+") {\n vec2 wValues = vec2(\n getW(wR, wC, "+d+", d2),\n getW(wR, wC, "+d+" + 1, d2)\n );\n\n if ("+p+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+d+"),\n getX(batch, xR, xC, "+d+" + 1)\n );\n dotProd += dot(xValues, wValues);\n } else {\n vec2 xValues = vec2(\n getX(batch, "+d+", xR, xC),\n getX(batch, "+d+" + 1, xR, xC)\n );\n dotProd += dot(xValues, wValues);\n }\n\n } else if ("+(3===h)+") {\n vec3 wValues = vec3(\n getW(wR, wC, "+d+", d2),\n getW(wR, wC, "+d+" + 1, d2),\n getW(wR, wC, "+d+" + 2, d2)\n );\n\n if ("+p+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+d+"),\n getX(batch, xR, xC, "+d+" + 1),\n getX(batch, xR, xC, "+d+" + 2)\n );\n dotProd += dot(xValues, wValues);\n } else {\n vec3 xValues = vec3(\n getX(batch, "+d+", xR, xC),\n getX(batch, "+d+" + 1, xR, xC),\n getX(batch, "+d+" + 2, xR, xC)\n );\n dotProd += dot(xValues, wValues);\n }\n\n }\n }\n }\n\n float result = dotProd;\n "+x+"\n "+b+"\n setOutput(result);\n }\n "},ko=function(t,e,n,r){void 0===e&&(e=!1),void 0===n&&(n=null),void 0===r&&(r=!1),this.variableNames=["x","W"],this.outputShape=t.outShape;var i=t.inHeight,a=t.inWidth,o=t.padInfo.top,s=t.padInfo.left,u=t.strideHeight,c=t.strideWidth,l=t.dilationHeight,f=t.dilationWidth,d=t.filterHeight,h=t.filterWidth,p=t.outChannels/t.inChannels,m="",g="";n&&(m=r?"float activation(float a) {\n float b = getPreluActivationWeightsAtOutCoords();\n "+n+"\n }":"\n float activation(float x) {\n "+n+"\n }\n ",g="result = activation(result);");var v=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.userCode="\n "+m+"\n\n const ivec2 strides = ivec2("+u+", "+c+");\n const ivec2 pads = ivec2("+o+", "+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords.x;\n ivec2 xRCCorner = coords.yz * strides - pads;\n int d2 = coords.w;\n int d1 = d2 / "+p+";\n int q = d2 - d1 * "+p+";\n\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n // TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < "+d+"; wR++) {\n int xR = xRCorner + wR * "+l+";\n\n if (xR < 0 || xR >= "+i+") {\n continue;\n }\n\n for (int wC = 0; wC < "+h+"; wC++) {\n int xC = xCCorner + wC * "+f+";\n\n if (xC < 0 || xC >= "+a+") {\n continue;\n }\n\n float xVal = getX(batch, xR, xC, d1);\n float wVal = getW(wR, wC, d1, q);\n dotProd += xVal * wVal;\n }\n }\n\n float result = dotProd;\n "+v+"\n "+g+"\n setOutput(result);\n }\n "},_o=function(t,e,n,r){void 0===e&&(e=!1),void 0===n&&(n=null),void 0===r&&(r=!1),this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.outShape;for(var i=t.inHeight,a=t.inWidth,o=t.padInfo.top,s=t.padInfo.left,u=t.strideHeight,c=t.strideWidth,l=t.dilationHeight,f=t.dilationWidth,d=t.filterHeight,h=t.filterWidth,p=h,m="int xR; int xC; int xCOffset;",g=0;g= 0 && xR < "+i+" && xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+v+" = getX(batch, xR, xCOffset, d1);\n\n // Need to manually clear unused channels in case\n // we're reading from recycled texture.\n if(xCOffset + 1 >= "+a+") {\n xTexelR"+g+"C"+v+".zw = vec2(0.);\n }\n } else {\n xTexelR"+g+"C"+v+" = vec4(0.);\n }\n\n xCOffset = xC + 1 - 2;\n if(xR >= 0 && xR < "+i+" && xCOffset >= 0 && xCOffset < "+a+") {\n vec4 previous = getX(batch, xR, xCOffset, d1);\n\n // Need to manually clear unused channels in case\n // we're reading from recycled texture.\n if(xCOffset + 1 >= "+a+") {\n previous.zw = vec2(0.);\n }\n\n xR"+g+"C"+v+" = vec4(previous.zw, xTexelR"+g+"C"+v+".xy);\n } else {\n xR"+g+"C"+v+" = vec4(0, 0, xTexelR"+g+"C"+v+".xy);\n }\n ":"\n if(xR >= 0 && xR < "+i+" && xC >= 0 && xC < "+a+") {\n xTexelR"+g+"C"+v+" = getX(batch, xR, xC, d1);\n } else {\n xTexelR"+g+"C"+v+" = vec4(0.);\n }\n\n xR"+g+"C"+v+" = xTexelR"+g+"C"+v+";\n ",v+1= 0 && xR < "+i+" &&\n xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+(v+2)+" = getX(batch, xR, xCOffset, d1);\n }\n ",f>1&&(m+="\n xCOffset -= 2;\n if(xR >= 0 && xR < "+i+" &&\n xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+v+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+v+" = vec4(0.);\n }\n "),m+="\n xR"+g+"C"+(v+1)+" = vec4(\n xTexelR"+g+"C"+v+".zw, xTexelR"+g+"C"+(v+2)+".xy);\n "):m+="\n xCOffset = xC + "+b+";\n\n if(xR >= 0 && xR < "+i+" &&\n xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+(v+2)+" = getX(batch, xR, xCOffset, d1);\n }\n\n xR"+g+"C"+(v+1)+" = xTexelR"+g+"C"+(v+2)+";\n "}}else v= 0 && xR < "+i+") {\n ",s%2==1?(m+="\n xCOffset = xC + 1 - "+c+";\n if(xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+v+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+v+" = vec4(0.);\n }\n\n if(xC + 1 >= 0 && xC + 1 < "+a+") {\n xTexelR"+g+"C"+(v+2)+" = getX(batch, xR, xC + 1, d1);\n } else {\n xTexelR"+g+"C"+(v+2)+" = vec4(0.);\n }\n\n xR"+g+"C"+v+" = vec4(\n xTexelR"+g+"C"+v+".zw, xTexelR"+g+"C"+(v+2)+".zw);\n ",v+1= 0 && xCOffset < "+a+") {\n final = getX(batch, xR, xCOffset, d1);\n }\n xR"+g+"C"+(v+1)+" = vec4(xTexelR"+g+"C"+(v+2)+".xy, final.xy);\n ")):(m+="\n if(xC >= 0 && xC < "+a+") {\n xTexelR"+g+"C"+v+" = getX(batch, xR, xC, d1);\n } else {\n xTexelR"+g+"C"+v+" = vec4(0.);\n }\n\n xCOffset = xC + "+c+";\n if(xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+(v+2)+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+(v+2)+" = vec4(0.);\n }\n\n xR"+g+"C"+v+" = vec4(\n xTexelR"+g+"C"+v+".xy, xTexelR"+g+"C"+(v+2)+".xy);\n ",v+10?(e=this.beginQuery(),this.endQuery(),n=function(){return r.isQueryAvailable(e,f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))}):n=function(){return!0};return{query:e,isFencePassed:n}},t.prototype.downloadMatrixFromPackedTexture=function(t,e,n){var r=this;return this.downloadMatrixDriver(t,function(){return qo(r.gl,r.debug,e,n)})},t.prototype.createProgram=function(t){this.throwIfDisposed();var e=this.gl,n=ce(e,this.debug,t),r=Ao(e,this.debug),i=he(e,this.debug);return ne(e,this.debug,function(){return e.attachShader(i,r)}),ne(e,this.debug,function(){return e.attachShader(i,n)}),pe(e,this.debug,i),this.debug&&me(e,this.debug,i),this.vertexAttrsAreBound||(this.setProgram(i),this.vertexAttrsAreBound=Po(e,this.debug,this.program,this.vertexBuffer)),i},t.prototype.deleteProgram=function(t){var e=this;this.throwIfDisposed(),t===this.program&&(this.program=null),null!=t&&ne(this.gl,this.debug,function(){return e.gl.deleteProgram(t)})},t.prototype.setProgram=function(t){var e=this;this.throwIfDisposed(),this.program=t,null!=this.program&&this.debug&&me(this.gl,this.debug,this.program),ne(this.gl,this.debug,function(){return e.gl.useProgram(t)})},t.prototype.getUniformLocation=function(t,e,n){return void 0===n&&(n=!0),this.throwIfDisposed(),n?_e(this.gl,this.debug,t,e):Ee(this.gl,t,e)},t.prototype.getAttributeLocation=function(t,e){var n=this;return this.throwIfDisposed(),ne(this.gl,this.debug,function(){return n.gl.getAttribLocation(t,e)})},t.prototype.getUniformLocationNoThrow=function(t,e){return this.throwIfDisposed(),this.gl.getUniformLocation(t,e)},t.prototype.setInputMatrixTexture=function(t,e,n){this.throwIfDisposed(),this.throwIfNoProgram(),Oe(this.gl,this.debug,this.program,t,e,n)},t.prototype.setOutputMatrixTexture=function(t,e,n){this.setOutputMatrixTextureDriver(t,n,e)},t.prototype.setOutputPackedMatrixTexture=function(t,e,n){this.throwIfDisposed();var r=te(e,n),i=r[0],a=r[1];this.setOutputMatrixTextureDriver(t,i,a)},t.prototype.setOutputMatrixWriteRegion=function(t,e,n,r){this.setOutputMatrixWriteRegionDriver(n,t,r,e)},t.prototype.setOutputPackedMatrixWriteRegion=function(t,e,n,r){throw new Error("setOutputPackedMatrixWriteRegion not implemented.")},t.prototype.debugValidate=function(){null!=this.program&&me(this.gl,this.debug,this.program),Ae(this.gl)},t.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var t=this.gl;this.debug&&this.debugValidate(),ne(t,this.debug,function(){return t.drawElements(t.TRIANGLES,6,t.UNSIGNED_SHORT,0)})},t.prototype.blockUntilAllProgramsCompleted=function(){var t=this;this.throwIfDisposed(),ne(this.gl,this.debug,function(){return t.gl.finish()})},t.prototype.getQueryTimerExtension=function(){return null==this.disjointQueryTimerExtension&&(this.disjointQueryTimerExtension=se(this.gl,this.debug,2===f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")?"EXT_disjoint_timer_query_webgl2":"EXT_disjoint_timer_query")),this.disjointQueryTimerExtension},t.prototype.getQueryTimerExtensionWebGL2=function(){return this.getQueryTimerExtension()},t.prototype.getQueryTimerExtensionWebGL1=function(){return this.getQueryTimerExtension()},t.prototype.beginQuery=function(){if(2===f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var t=this.gl,e=this.getQueryTimerExtensionWebGL2(),n=t.createQuery();return t.beginQuery(e.TIME_ELAPSED_EXT,n),n}var r=this.getQueryTimerExtensionWebGL1(),i=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,i),i},t.prototype.endQuery=function(){if(2!==f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var t=this.getQueryTimerExtensionWebGL1();t.endQueryEXT(t.TIME_ELAPSED_EXT)}else{var e=this.gl,n=this.getQueryTimerExtensionWebGL2();e.endQuery(n.TIME_ELAPSED_EXT)}},t.prototype.waitForQueryAndGetTime=function(t){return u(this,void 0,void 0,function(){var e=this;return c(this,function(n){switch(n.label){case 0:return[4,P(function(){return e.disposed||e.isQueryAvailable(t,f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))})];case 1:return n.sent(),[2,this.getQueryTime(t,f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))]}})})},t.prototype.getQueryTime=function(t,e){if(0===e)return null;if(2===e){var n=this.gl;return n.getQueryParameter(t,n.QUERY_RESULT)/1e6}var r=this.getQueryTimerExtensionWebGL1();return r.getQueryObjectEXT(t,r.QUERY_RESULT_EXT)/1e6},t.prototype.isQueryAvailable=function(t,e){if(0===e)return!0;if(2===e){var n=this.gl,r=this.getQueryTimerExtensionWebGL2(),i=n.getQueryParameter(t,n.QUERY_RESULT_AVAILABLE);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),i&&!this.disjoint}return i=(r=this.getQueryTimerExtensionWebGL1()).getQueryObjectEXT(t,r.QUERY_RESULT_AVAILABLE_EXT),null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),i&&!this.disjoint},t.prototype.pollFence=function(t){var e=this;return new Promise(function(n){e.addItemToPoll(function(){return t.isFencePassed()},function(){return n()})})},t.prototype.pollItems=function(){for(var t=function(t){for(var e=0;e1||P(function(){return n.pollItems(),0===n.itemsToPoll.length})},t.prototype.bindTextureToFrameBuffer=function(t){this.throwIfDisposed(),Se(this.gl,this.debug,t,this.framebuffer),this.debug&&Ae(this.gl)},t.prototype.unbindTextureToFrameBuffer=function(){null!=this.outputTexture?(Se(this.gl,this.debug,this.outputTexture,this.framebuffer),this.debug&&Ae(this.gl)):Ce(this.gl,this.debug,this.framebuffer)},t.prototype.downloadMatrixDriver=function(t,e){this.bindTextureToFrameBuffer(t);var n=e();return this.unbindTextureToFrameBuffer(),n},t.prototype.setOutputMatrixTextureDriver=function(t,e,n){this.throwIfDisposed();var r=this.gl;Se(r,this.debug,t,this.framebuffer),this.debug&&Ae(r),this.outputTexture=t,ne(r,this.debug,function(){return r.viewport(0,0,e,n)}),ne(r,this.debug,function(){return r.scissor(0,0,e,n)})},t.prototype.setOutputMatrixWriteRegionDriver=function(t,e,n,r){var i=this;this.throwIfDisposed(),ne(this.gl,this.debug,function(){return i.gl.scissor(t,e,n,r)})},t.prototype.throwIfDisposed=function(){if(this.disposed)throw new Error("Attempted to use disposed GPGPUContext.")},t.prototype.throwIfNoProgram=function(){if(null==this.program)throw new Error("No GPU program is currently set.")},t}();function $o(t,e){if(t.length!==e.length)throw Error("Binary was compiled with "+t.length+" inputs, but was executed with "+e.length+" inputs");t.forEach(function(t,n){var r=t.logicalShape,i=e[n],a=i.shape;if(!D(r,a))throw Error("Binary was compiled with different shapes than the current args. Shapes "+r+" and "+a+" must match");if(!t.isUniform||!i.isUniform){var o=t.texShape,s=i.isUniform?null:i.texData.texShape;if(!D(o,s))throw Error("Binary was compiled with different texture shapes than the current args. Shape "+o+" and "+s+" must match")}})}var Ko=function(t,e,n,r,i,a,o){void 0===n&&(n=!1),void 0===r&&(r=!1),void 0===i&&(i=!1),void 0===a&&(a=null),void 0===o&&(o=!1),this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e;var s=n?t[1]:t[2],u=Math.ceil(s/2),c=n?"i * 2, rc.y":"rc.y, i * 2",l=r?"rc.z, i * 2":"i * 2, rc.z",f=n?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],d=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],h="",p="";a&&(h=o?"vec4 activation(vec4 a) {\n vec4 b = getPreluActivationWeightsAtOutCoords();\n "+a+"\n }":"vec4 activation(vec4 x) {\n "+a+"\n }",p="result = activation(result);");var m=i?"result += getBiasAtOutCoords();":"";i&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),this.userCode="\n "+h+"\n\n const float sharedDimension = "+u+".0;\n\n vec4 dot2x2ARowBCol(ivec3 rc) {\n vec4 result = vec4(0);\n for (int i = 0; i < "+u+"; i++) {\n vec4 a = getMatrixA(rc.x, "+c+");\n vec4 b = getMatrixB(rc.x, "+l+");\n\n // These swizzled products need to be separately added.\n // See: https://github.com/tensorflow/tfjs/issues/1735\n result += ("+f[0]+" * "+d[0]+");\n result += ("+f[1]+" * "+d[1]+");\n }\n return result;\n }\n\n void main() {\n ivec3 rc = getOutputCoords();\n vec4 result = dot2x2ARowBCol(rc);\n\n "+m+"\n\n "+p+"\n\n setOutput(result);\n }\n "},Xo=function(){function t(t,e,n){this.variableNames=["probs"],this.outputShape=[t,n],this.userCode="\n uniform float seed;\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n\n float r = random(seed);\n float cdf = 0.0;\n\n for (int i = 0; i < "+(e-1)+"; i++) {\n cdf += getProbs(batch, i);\n\n if (r < cdf) {\n setOutput(float(i));\n return;\n }\n }\n\n // If no other event happened, last event happened.\n setOutput(float("+(e-1)+"));\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;return function(n,r){null==e.seedLoc&&(e.seedLoc=n.getUniformLocation(r,"seed")),n.gl.uniform1f(e.seedLoc,t)}},t}(),Yo=function(t,e,n,r,i){if(void 0===r&&(r=!1),void 0===i&&(i=!1),this.variableNames=["x"],"avg"===e&&n)throw new Error("Cannot compute positions for average pool.");var a=t.filterWidth,o=t.strideHeight,s=t.strideWidth,u=t.dilationHeight,c=t.dilationWidth,l=t.effectiveFilterHeight,f=t.effectiveFilterWidth,d=t.padInfo.top,h=t.padInfo.left;this.outputShape=t.outShape;var p="avg"===e,m="((batch * "+t.inHeight+" + xR) * "+t.inWidth+" + xC) * "+t.inChannels+" + d",g="(xR * "+t.inWidth+" + xC) * "+t.inChannels+" + d",v="0.0";if(p||(v="-1.0 / 1e-20"),n)this.userCode="\n const ivec2 strides = ivec2("+o+", "+s+");\n const ivec2 pads = ivec2("+d+", "+h+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+l+";\n wR += "+u+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+f+";\n wC += "+c+") {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float value = getX(batch, xR, xC, d);\n\n // If a min / max value has already been found, use it. If not,\n // use the current value.\n float currMinMaxValue = mix(\n value, minMaxValue, minMaxValueFound);\n if (value >= currMinMaxValue) {\n minMaxValue = value;\n minMaxValueFound = 1.0;\n minMaxPosition = "+(r?i?m:g:"wR * "+f+" + wC")+";\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var y=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===e&&(y="avgValue / count");var b=4*Math.floor(a/4),x=a%4,w="\n if ("+p+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+o+", "+s+");\n const ivec2 pads = ivec2("+d+", "+h+");\n const float initializationValue = "+v+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float count = 0.0;\n\n float getValue(int batch, int xR, int xC, int d) {\n if (xC < 0 || xC >= "+t.inWidth+") {\n return initializationValue;\n }\n count += 1.0;\n return getX(batch, xR, xC, d);\n }\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n vec4 minMaxValue = vec4("+v+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+l+";\n wR += "+u+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+b+"; wC += 4) {\n int xC = xCCorner + wC * "+c+";\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+c+", d),\n getValue(batch, xR, xC + 2 * "+c+", d),\n getValue(batch, xR, xC + 3 * "+c+", d)\n );\n\n "+w+"\n }\n\n int xC = xCCorner + "+b+";\n if ("+(1===x)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+w+"\n } else if ("+(2===x)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+c+", d),\n initializationValue,\n initializationValue\n );\n\n "+w+"\n } else if ("+(3===x)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+c+", d),\n getValue(batch, xR, xC + 2 * "+c+", d),\n initializationValue\n );\n\n "+w+"\n }\n }\n setOutput("+y+");\n }\n "}},Jo=function(t,e,n,r,i){if(void 0===r&&(r=!1),void 0===i&&(i=!1),this.variableNames=["x"],"avg"===e&&n)throw new Error("Cannot compute positions for average pool.");var a=t.filterWidth,o=t.strideDepth,s=t.strideHeight,u=t.strideWidth,c=t.dilationDepth,l=t.dilationHeight,f=t.dilationWidth,d=t.effectiveFilterDepth,h=t.effectiveFilterHeight,p=t.effectiveFilterWidth,m=t.padInfo.front,g=t.padInfo.top,v=t.padInfo.left;this.outputShape=t.outShape;var y="avg"===e,b="0.0";if(y||(b="-1.0 / 1e-20"),n)this.userCode="\n const ivec3 strides =\n ivec3("+o+", "+s+", "+u+");\n const ivec3 pads = ivec3("+m+", "+g+", "+v+");\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int ch = coords.u;\n\n ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;\n int xDCorner = xCorner.x;\n int xRCorner = xCorner.y;\n int xCCorner = xCorner.z;\n\n // max/min x(?, ?, ?, ch) to get y(yD, yR, yC, ch).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n\n for (int wD = 0; wD < "+d+";\n wD += "+c+") {\n int xD = xDCorner + wD;\n\n if (xD < 0 || xD >= "+t.inDepth+") {\n continue;\n }\n\n for (int wR = 0; wR < "+h+";\n wR += "+l+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+p+";\n wC += "+f+") {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float value = getX(batch, xD, xR, xC, ch);\n\n // If a min / max value has already been found, use it. If not,\n // use the current value.\n float currMinMaxValue = mix(\n value, minMaxValue, minMaxValueFound);\n if (value >= currMinMaxValue) {\n minMaxValue = value;\n minMaxValueFound = 1.0;\n minMaxPosition = "+(r?i?"(((batch * "+t.inDepth+" + xD) * "+t.inHeight+" + xR) * "+t.inWidth+" + xC) * "+t.inChannels+" + ch":"((xD * "+t.inHeight+" + xR) * "+t.inWidth+" + xC) * "+t.inChannels+" + ch":"wD * "+h+" * "+p+" +\n wR * "+p+" + wC")+";\n }\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var x=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===e&&(x="avgValue / count");var w=4*Math.floor(a/4),k=a%4,_="\n if ("+y+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec3 strides =\n ivec3("+o+", "+s+", "+u+");\n const ivec3 pads = ivec3("+m+", "+g+", "+v+");\n const float initializationValue = "+b+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float count = 0.0;\n\n float getValue(int batch, int xD, int xR, int xC, int ch) {\n if (xC < 0 || xC >= "+t.inWidth+") {\n return initializationValue;\n }\n count += 1.0;\n return getX(batch, xD, xR, xC, ch);\n }\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int ch = coords.u;\n\n ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;\n int xDCorner = xCorner.x;\n int xRCorner = xCorner.y;\n int xCCorner = xCorner.z;\n\n // max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).\n // ? = to be determined\n vec4 minMaxValue = vec4("+b+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wD = 0; wD < "+d+";\n wD += "+c+") {\n int xD = xDCorner + wD;\n\n if (xD < 0 || xD >= "+t.inDepth+") {\n continue;\n }\n\n for (int wR = 0; wR < "+h+";\n wR += "+l+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+w+"; wC += 4) {\n int xC = xCCorner + wC * "+f+";\n\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n getValue(batch, xD, xR, xC + "+f+", ch),\n getValue(batch, xD, xR, xC + 2 * "+f+", ch),\n getValue(batch, xD, xR, xC + 3 * "+f+", ch)\n );\n\n "+_+"\n }\n\n int xC = xCCorner + "+w+";\n if ("+(1===k)+") {\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+_+"\n } else if ("+(2===k)+") {\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n getValue(batch, xD, xR, xC + "+f+", ch),\n initializationValue,\n initializationValue\n );\n\n "+_+"\n } else if ("+(3===k)+") {\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n getValue(batch, xD, xR, xC + "+f+", ch),\n getValue(batch, xD, xR, xC + 2 * "+f+", ch),\n initializationValue\n );\n\n "+_+"\n }\n }\n setOutput("+x+");\n }\n }\n "}},Zo=function(t,e,n,r,i,a,o){void 0===o&&(o=!0),this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;var s=oo(i.length),u=oo(a.length),c="";1===n?c="i":2===n&&(c="i, j");var l="getIndices("+c+")",f="";1===r?f="i":2===r&&(f="i, coords[1]");var d="getUpdates("+f+")",h=e>1?"strides[j]":"strides";this.userCode="\n "+s+" strides = "+s+"("+i+");\n\n void main() {\n "+u+" coords = getOutputCoords();\n float sum = 0.0;\n bool found = false;\n for (int i = 0; i < "+t+"; i++) {\n int flattenedIndex = 0;\n for (int j = 0; j < "+e+"; j++) {\n int index = round("+l+");\n flattenedIndex += index * "+h+";\n }\n if (flattenedIndex == coords[0]) {\n sum += "+d+";\n found = true;\n }\n }\n setOutput(mix(getDefaultValue(), sum, float(found)));\n }\n "},Qo=function(){function t(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;var e,n=oo(this.rank),r="uniform int start["+this.rank+"];",i=function(t){if(1===t)return"sourceLoc";if(t<=6)return ts.slice(0,t).map(function(t){return"sourceLoc."+t}).join(",");throw Error("Slicing for rank "+t+" is not yet supported")}(this.rank);e="\n "+n+" sourceLoc;\n "+n+" coords = getOutputCoords();\n "+t.map(function(t,e){return"sourceLoc."+ts[e]+" = start["+e+"] + coords."+ts[e]+";"}).join("\n")+"\n ",this.userCode="\n "+r+"\n void main() {\n "+e+"\n setOutput(getSource("+i+"));\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;if(t.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+t.length+")");return function(n,r){null==e.startLoc&&(e.startLoc=n.getUniformLocationNoThrow(r,"start"),null==e.startLoc)||n.gl.uniform1iv(e.startLoc,t)}},t}(),ts=["x","y","z","w","u","v"],es=function(){function t(t){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.rank=t.length;var e=oo(this.rank),n=Ka("coords",this.rank),r=Ka("sourceLoc",this.rank),i=1===this.rank?"sourceLoc":"vec2("+r.slice(-2).join()+")",a="getChannel(getSource("+r.join()+"), "+i+")",o="\n result.x = "+a+";\n if (++"+n[this.rank-1]+" < "+t[this.rank-1]+") {\n ++"+r[this.rank-1]+";\n result.y = "+a+";\n --"+r[this.rank-1]+";\n }\n ",s=1===this.rank?"":"\n --"+n[this.rank-1]+";\n if (++"+n[this.rank-2]+" < "+t[this.rank-2]+") {\n ++"+r[this.rank-2]+";\n result.z = "+a+";\n if (++"+n[this.rank-1]+" < "+t[this.rank-1]+") {\n ++"+r[this.rank-1]+";\n result.w = "+a+";\n }\n }\n ",u=this.rank<=4?"sourceLoc = coords +\n "+e+"("+t.map(function(t,e){return"start["+e+"]"}).join()+");":t.map(function(t,e){return r[e]+" = "+n[e]+" + start["+e+"];"}).join("\n");this.userCode="\n uniform int start["+this.rank+"];\n void main() {\n "+e+" coords = getOutputCoords();\n "+e+" sourceLoc;\n "+u+"\n vec4 result = vec4(0.);\n "+o+"\n "+s+"\n setOutput(result);\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;if(t.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+t.length+")");return function(n,r){null==e.startLoc&&(e.startLoc=n.getUniformLocationNoThrow(r,"start"),null==e.startLoc)||n.gl.uniform1iv(e.startLoc,t)}},t}(),ns=function(){function t(t){this.gpgpu=t,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}return t.prototype.acquireTexture=function(t,e,n){var r,i=rs(e,n),a=is(t,i,n);if(a in this.freeTextures||(this.freeTextures[a]=[]),a in this.usedTextures||(this.usedTextures[a]=[]),this.freeTextures[a].length>0){this.numFreeTextures--,this.numUsedTextures++,this.log();var o=this.freeTextures[a].shift();return this.usedTextures[a].push(o),o}return this.numUsedTextures++,this.log(),i===$t.PACKED_2X2_FLOAT32?r=this.gpgpu.createPackedMatrixTexture(t[0],t[1]):i===$t.PACKED_2X2_FLOAT16?r=this.gpgpu.createFloat16PackedMatrixTexture(t[0],t[1]):i===$t.UNPACKED_FLOAT32?r=this.gpgpu.createFloat32MatrixTexture(t[0],t[1]):i===$t.UNPACKED_FLOAT16?r=this.gpgpu.createFloat16MatrixTexture(t[0],t[1]):i===$t.PACKED_4X1_UNSIGNED_BYTE&&(r=this.gpgpu.createUnsignedBytesMatrixTexture(t[0],t[1])),this.usedTextures[a].push(r),r},t.prototype.releaseTexture=function(t,e,n,r){if(null!=this.freeTextures){var i=is(e,rs(n,r),r);i in this.freeTextures||(this.freeTextures[i]=[]),this.freeTextures[i].push(t),this.numFreeTextures++,this.numUsedTextures--;var a=this.usedTextures[i],o=a.indexOf(t);if(o<0)throw new Error("Cannot release a texture that was never provided by this texture manager");a.splice(o,1),this.log()}},t.prototype.log=function(){if(this.logEnabled){var t=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",this.numFreeTextures+" / "+this.numUsedTextures,"("+t+")")}},t.prototype.getNumUsedTextures=function(){return this.numUsedTextures},t.prototype.getNumFreeTextures=function(){return this.numFreeTextures},t.prototype.dispose=function(){var t=this;if(null!=this.freeTextures){for(var e in this.freeTextures)this.freeTextures[e].forEach(function(e){t.gpgpu.deleteMatrixTexture(e)});for(var e in this.usedTextures)this.usedTextures[e].forEach(function(e){t.gpgpu.deleteMatrixTexture(e)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0}},t}();function rs(t,e){if(t===Gt.UPLOAD)return $t.PACKED_2X2_FLOAT32;if(t===Gt.RENDER||null==t)return function(t){return f().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?t?$t.PACKED_2X2_FLOAT32:$t.UNPACKED_FLOAT32:t?$t.PACKED_2X2_FLOAT16:$t.UNPACKED_FLOAT16}(e);if(t===Gt.DOWNLOAD||t===Gt.PIXELS)return $t.PACKED_4X1_UNSIGNED_BYTE;throw new Error("Unknown logical texture type "+t)}function is(t,e,n){return t[0]+"_"+t[1]+"_"+e+"_"+n}var as=1.7580993408473768,os=1.0507009873554805,ss=function(t,e){this.variableNames=["A"],this.outputShape=t,this.userCode="\n float unaryOperation(float x) {\n "+e+"\n }\n\n void main() {\n float x = getAAtOutCoords();\n float y = unaryOperation(x);\n\n setOutput(y);\n }\n "},us="if (isnan(x)) return x;",cs="return x;",ls="return abs(x);",fs=us+"\n return (x < 0.0) ? 0.0 : x;\n",ds=us+"\n return (x < 0.0) ? 0.0 : min(6.0, x);\n",hs="return (x >= 0.0) ? x : (exp(x) - 1.0);",ps="return -x;",ms="return ceil(x);",gs="return floor(x);",vs="return exp(x);",ys="return exp(x) - 1.0;",bs="return x;",xs="return x;",ws="\n vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n",ks="\n vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n",_s="\n vec4 result;\n\n result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);\n result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);\n result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);\n result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);\n\n return result;\n",Es=function(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.userCode="\n vec4 unaryOperation(vec4 x) {\n "+e+"\n }\n\n void main() {\n vec4 x = getAAtOutCoords();\n vec4 y = unaryOperation(x);\n\n setOutput(y);\n }\n "},Os={};function Ss(t,e){if(void 0===e&&(e=!1),"linear"===t)return e?xs:cs;if("relu"===t)return e?ws:fs;if("elu"===t)return e?_s:hs;if("relu6"===t)return e?ks:ds;if("prelu"===t)return e?go:po;throw new Error("Activation "+t+" has not been implemented for the WebGL backend.")}var Cs=600,As=function(t){function e(e){var n,r=t.call(this)||this;if(r.pendingRead=new WeakMap,r.pendingDisposal=new WeakSet,r.dataRefCount=new WeakMap,r.numBytesInGPU=0,r.uploadWaitMs=0,r.downloadWaitMs=0,r.warnedAboutMemory=!1,r.pendingDeletes=0,r.disposed=!1,!f().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");if(null==e){var i=Jt(f().getNumber("WEBGL_VERSION"));r.binaryCache=(n=f().getNumber("WEBGL_VERSION"))in Os?Os[n]:(Os[n]={},Os[n]),r.gpgpu=new Go(i),r.canvas=i.canvas,r.gpgpuCreatedLocally=!0}else r.gpgpu=e,r.binaryCache={},r.gpgpuCreatedLocally=!1,r.canvas=e.gl.canvas;return r.textureManager=new ns(r.gpgpu),r.numMBBeforeWarning=null==f().global.screen?1024:f().global.screen.height*f().global.screen.width*window.devicePixelRatio*Cs/1024/1024,r.texData=new da(r,Wt),r}return s(e,t),e.prototype.numDataIds=function(){return this.texData.numDataIds()+(this.cpuBackend?this.cpuBackend.numDataIds():0)-this.pendingDeletes},e.prototype.write=function(t,e,n){if(f().getBool("DEBUG")&&this.checkNumericalProblems(t),"complex64"===n&&null!=t)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");var r={};return this.texData.set(r,{shape:e,dtype:n,values:t,usage:Gt.UPLOAD}),r},e.prototype.move=function(t,e,n,r){if(f().getBool("DEBUG")&&this.checkNumericalProblems(e),"complex64"===r)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(t,{shape:n,dtype:r,values:e,usage:Gt.UPLOAD})},e.prototype.readSync=function(t){var e=this.texData.get(t),n=e.values,r=e.dtype,i=e.complexTensors,a=e.slice,o=e.shape,s=e.isPacked;if(null!=a){var u;u=s?new Es(o,bs):new ss(o,bs);var c=this.runWebGLProgram(u,[{dataId:t,shape:o,dtype:r}],r),l=this.readSync(c.dataId);return this.disposeData(c.dataId),l}if(null!=n)return this.convertAndCacheOnCPU(t);if("string"===r)return n;var f,d,h=null!=this.activeTimers;return h&&(f=ot()),d="complex64"===r?Ia(i.real.dataSync(),i.imag.dataSync()):this.getValuesFromTexture(t),h&&(this.downloadWaitMs+=ot()-f),this.convertAndCacheOnCPU(t,d)},e.prototype.read=function(t){return u(this,void 0,void 0,function(){var e,n,r,i,a,o,s,u,l,d,h,p,m,g,v,y,b,x,w,k,_,E;return c(this,function(c){switch(c.label){case 0:if(this.pendingRead.has(t))return e=this.pendingRead.get(t),[2,new Promise(function(t){return e.push(t)})];if(n=this.texData.get(t),r=n.values,i=n.shape,a=n.slice,o=n.dtype,s=n.complexTensors,u=n.isPacked,null!=a)return l=void 0,l=u?new Es(i,bs):new ss(i,bs),d=this.runWebGLProgram(l,[{dataId:t,shape:i,dtype:o}],o),h=this.read(d.dataId),this.disposeData(d.dataId),[2,h];if(null!=r)return[2,this.convertAndCacheOnCPU(t)];if(!f().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&2===f().getNumber("WEBGL_VERSION"))throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");return p=null,"complex64"!==o&&f().get("WEBGL_BUFFER_SUPPORTED")&&(m=this.decode(t),g=this.texData.get(m.dataId),p=(E=this.gpgpu).createBufferFromTexture.apply(E,[g.texture].concat(Qt(i)))),this.pendingRead.set(t,[]),"complex64"===o?[3,2]:[4,this.gpgpu.createAndWaitForFence()];case 1:c.sent(),c.label=2;case 2:return"complex64"!==o?[3,4]:[4,Promise.all([s.real.data(),s.imag.data()])];case 3:return y=c.sent(),b=y[0],x=y[1],v=Ia(b,x),[3,5];case 4:null==p?v=this.getValuesFromTexture(t):(w=I(i),v=this.gpgpu.downloadFloat32MatrixFromBuffer(p,w)),c.label=5;case 5:return null!=m&&this.disposeData(m.dataId),k=this.convertAndCacheOnCPU(t,v),_=this.pendingRead.get(t),this.pendingRead.delete(t),_.forEach(function(t){return t(k)}),this.pendingDisposal.has(t)&&(this.pendingDisposal.delete(t),this.disposeData(t),this.pendingDeletes--),[2,k]}})})},e.prototype.checkNumericalProblems=function(t){if(null!=t)for(var e=0;e0?[4,Promise.all(i)]:[3,2];case 1:return s=u.sent(),o.kernelMs=S(s),o.getExtraProfileInfo=function(){return s.map(function(t,e){return{name:a[e],ms:t}}).map(function(t){return t.name+": "+t.ms}).join(", ")},[3,3];case 2:o.kernelMs={error:"WebGL query timers are not supported in this environment."},u.label=3;case 3:return this.uploadWaitMs=0,this.downloadWaitMs=0,[2,o]}})})},e.prototype.memory=function(){return{unreliable:!1,numBytesInGPU:this.numBytesInGPU}},e.prototype.startTimer=function(){return f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?this.gpgpu.beginQuery():{startMs:ot(),endMs:null}},e.prototype.endTimer=function(t){return f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?(this.gpgpu.endQuery(),t):(t.endMs=ot(),t)},e.prototype.getQueryTime=function(t){return u(this,void 0,void 0,function(){var e;return c(this,function(n){return f().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?[2,this.gpgpu.waitForQueryAndGetTime(t)]:[2,(e=t).endMs-e.startMs]})})},e.prototype.disposeData=function(t){if(!this.pendingDisposal.has(t)){if(this.pendingRead.has(t))return this.pendingDisposal.add(t),void this.pendingDeletes++;if(this.texData.has(t)){this.releaseGPUData(t);var e=this.texData.get(t).complexTensors;null!=e&&(e.real.dispose(),e.imag.dispose()),this.texData.delete(t)}}},e.prototype.releaseGPUData=function(t){var e=this.texData.get(t),n=e.texture,r=e.dtype,i=e.texShape,a=e.usage,o=e.isPacked,s=e.slice,u=s&&s.origDataId||t,c=this.dataRefCount.get(u);c>1?this.dataRefCount.set(u,c-1):(this.dataRefCount.delete(u),null!=n&&(this.numBytesInGPU-=this.computeBytes(i,r),this.textureManager.releaseTexture(n,i,a,o)));var l=this.texData.get(t);l.texture=null,l.texShape=null,l.isPacked=!1,l.slice=null},e.prototype.getTexture=function(t){return this.uploadToGPU(t),this.texData.get(t).texture},e.prototype.getDataInfo=function(t){return this.texData.get(t)},e.prototype.getCPUBackend=function(){return f().getBool("WEBGL_CPU_FORWARD")?(null==this.cpuBackend&&(this.cpuBackend=Wt.findBackend("cpu")),this.cpuBackend):null},e.prototype.shouldExecuteOnCPU=function(t,e){var n=this;return void 0===e&&(e=128),null!=this.getCPUBackend()&&t.every(function(t){return null==n.texData.get(t.dataId).texture&&I(t.shape)4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");this.outputShape=t;var r=Ka("rc",n),i=r[n-1]+" + 1 < "+this.outputShape[n-1],a=r[n-2]+" + 1 < "+this.outputShape[n-2],o=oo(n);function s(n){var r=t.map(function(r,i){return function(n,r){return-1!==e.indexOf(n)&&1!==t[n]?t[n]+" - "+r[n]+" - 1":""+r[n]}(i,n)});return"getChannel(getX("+r.join(",")+"), vec2("+r.slice(-2).join(",")+"))"}this.userCode=1===n?"\n void main(){\n int rc = getOutputCoords();\n vec4 result = vec4(0.);\n result.r = getChannel(getX("+t[0]+" - rc - 1),\n "+t[0]+" - rc - 1);\n if("+i+"){\n result.g = getChannel(getX("+t[0]+" - (rc + 1) - 1),\n "+t[0]+" - (rc + 1) - 1);\n }\n setOutput(result);\n }\n ":"\n void main() {\n "+o+" rc = getOutputCoords();\n vec4 result = vec4(0.);\n result.r = "+s(r.slice())+";\n if("+i+"){\n result.g = "+function(t){return t[n-1]="("+t[n-1]+" + 1)",s(t)}(r.slice())+";\n }\n if("+a+") {\n result.b = "+function(t){return t[n-2]="("+t[n-2]+" + 1)",s(t)}(r.slice())+";\n if("+i+") {\n result.a = "+function(t){return t[n-1]="("+t[n-1]+" + 1)",t[n-2]="("+t[n-2]+" + 1)",s(t)}(r.slice())+";\n }\n }\n setOutput(result);\n }\n "}(t.shape,e):new function(t,e){this.variableNames=["x"];var n=t.length;if(n>4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");if(this.outputShape=t,1!==n){var r=t.map(function(n,r){return function(n){return-1!==e.indexOf(n)&&1!==t[n]?t[n]+" - coords["+n+"] - 1":"coords["+n+"]"}(r)}).join(","),i=oo(n);this.userCode="\n void main() {\n "+i+" coords = getOutputCoords();\n setOutput(getX("+r+"));\n }\n "}else this.userCode="\n void main() {\n int coord = getOutputCoords();\n setOutput(getX("+t[0]+" - coord - 1));\n }\n "}(t.shape,e);return this.compileAndRun(n,[t])},e.prototype.concat=function(t,e){if("complex64"===t[0].dtype){var n=t.map(function(t){return Rn(t)}),r=t.map(function(t){return Fn(t)});return Mn(this.concat(n,e),this.concat(r,e))}if(this.shouldExecuteOnCPU(t))return this.cpuBackend.concat(t,e);if(1===t.length)return t[0];if(t.length>f().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){var i=Math.floor(t.length/2),a=this.concat(t.slice(0,i),e),o=this.concat(t.slice(i),e);return this.concat([a,o],e)}if(f().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&t[0].rank>1){var s=new function(t,e){this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[],this.outputShape=In(t,e);var n=this.outputShape,r=n.length,i=oo(r),a=Ka("coords",r),o=["x","y","z","w","u","v"].slice(0,r);this.variableNames=t.map(function(t,e){return"T"+e});var s=new Array(t.length-1);s[0]=t[0][e];for(var u=1;u= "+s[u-1]+") {\n return getChannel(\n getT"+u+"("+xo(o,c,h)+"),\n vec2("+xo(l,c,h)+"));\n }"}var p=s.length,m=s[s.length-1];d+="\n return getChannel(\n getT"+p+"("+xo(o,c,m)+"),\n vec2("+xo(l,c,m)+"));",this.userCode="\n float getValue("+o.map(function(t){return"int "+t})+") {\n "+d+"\n }\n\n void main() {\n "+i+" coords = getOutputCoords();\n vec4 result = vec4(getValue("+a+"), 0., 0., 0.);\n\n "+a[r-1]+" = "+a[r-1]+" + 1;\n if ("+a[r-1]+" < "+n[r-1]+") {\n result.g = getValue("+a+");\n }\n\n "+a[r-2]+" = "+a[r-2]+" + 1;\n if ("+a[r-2]+" < "+n[r-2]+") {\n result.a = getValue("+a+");\n }\n\n "+a[r-1]+" = "+a[r-1]+" - 1;\n if ("+a[r-2]+" < "+n[r-2]+" &&\n "+a[r-1]+" < "+n[r-1]+") {\n result.b = getValue("+a+");\n }\n setOutput(result);\n }\n "}(t.map(function(t){return t.shape}),e);return this.compileAndRun(s,t)}var u=In(t.map(function(t){return t.shape}),e),c=t.map(function(t){return t.as2D(-1,I(t.shape.slice(e)))}),l=new function(t){this.outputShape=[],this.outputShape=In(t,1),this.variableNames=t.map(function(t,e){return"T"+e});var e=new Array(t.length-1);e[0]=t[0][1];for(var n=1;n1e3){n&&(t=fa(t,[0,2,1])),r&&(e=fa(e,[0,2,1]));var u=1===a?t:t.as3D(s,o,1),c=1===a?2:1,l=1===a?e.as3D(s,1,o):e;return this.multiply(u,l).sum(c,!0)}var f=Mt(t.dtype,e.dtype),d=new Ko(t.shape,[s,i,a],n,r);return this.compileAndRun(d,[t,e],f)},e.prototype.fusedBatchMatMul=function(t){var e=t.a,n=t.b,r=t.transposeA,i=t.transposeB,a=t.bias,o=t.activation,s=t.preluActivationWeights,u=r?e.shape[2]:e.shape[1],c=i?n.shape[1]:n.shape[2],l=e.shape[0],f=Mt(e.dtype,n.dtype),d=null!=a,h=null!=s,p=o?Ss(o,!0):null,m=new Ko(e.shape,[l,u,c],r,i,d,p,h),g=[e,n];return a&&g.push(a),s&&g.push(s),this.compileAndRun(m,g,f)},e.prototype.multiply=function(t,e){if("complex64"===t.dtype){var n=this.texData.get(t.dataId),r=this.texData.get(e.dataId),i=new co("return areal * breal - aimag * bimag;",t.shape,e.shape),a=new co("return areal * bimag + aimag * breal;",t.shape,e.shape),o=[this.makeComplexComponentTensorInfo(t,n.complexTensors.real),this.makeComplexComponentTensorInfo(t,n.complexTensors.imag),this.makeComplexComponentTensorInfo(e,r.complexTensors.real),this.makeComplexComponentTensorInfo(e,r.complexTensors.imag)],s=this.compileAndRun(i,o),u=this.compileAndRun(a,o),c=this.complex(s,u);return s.dispose(),u.dispose(),c}if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.multiply(t,e);if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,ho,t.dtype);var l=new mo(ho,t.shape,e.shape);return this.compileAndRun(l,[t,e],t.dtype)},e.prototype.batchNormalization=function(t,e,n,r,i,a){var o=[t,e,n],s=null;null!=a&&(s=a.shape,o.push(a));var u=null;if(null!=i&&(u=i.shape,o.push(i)),f().getBool("WEBGL_PACK_NORMALIZATION")){var c=new function(t,e,n,r,i,a){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],Ur(t,e),Ur(t,n);var o="vec4(0.0)";null!=r&&(Ur(t,r),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");var s="vec4(1.0)";null!=i&&(Ur(t,i),this.variableNames.push("scale"),s="getScaleAtOutCoords()"),this.outputShape=t,this.userCode="\n void main() {\n vec4 offset = "+o+";\n vec4 scale = "+s+";\n\n vec4 x = getXAtOutCoords();\n vec4 mean = getMeanAtOutCoords();\n vec4 variance = getVarianceAtOutCoords();\n\n vec4 inv = scale * inversesqrt(variance + vec4("+a+"));\n\n setOutput((x - mean) * inv + offset);\n }\n "}(t.shape,e.shape,n.shape,s,u,r);return this.compileAndRun(c,o)}var l=new function(t,e,n,r,i,a){this.outputShape=[],this.variableNames=["x","mean","variance"],Ur(t,e),Ur(t,n);var o="0.0";null!=r&&(Ur(t,r),this.variableNames.push("offset"),o="getOffsetAtOutCoords()");var s="1.0";null!=i&&(Ur(t,i),this.variableNames.push("scale"),s="getScaleAtOutCoords()"),this.outputShape=t,this.userCode="\n void main() {\n float x = getXAtOutCoords();\n float mean = getMeanAtOutCoords();\n float variance = getVarianceAtOutCoords();\n float offset = "+o+";\n float scale = "+s+";\n float inv = scale * inversesqrt(variance + float("+a+"));\n setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));\n }\n "}(t.shape,e.shape,n.shape,s,u,r);return this.compileAndRun(l,o)},e.prototype.localResponseNormalization4D=function(t,e,n,r,i){var a=f().getBool("WEBGL_PACK_NORMALIZATION")?new function(t,e,n,r,i){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;var a,o=e,s=t[3]-1;this.outputShape=t;var u="float("+n+") + float("+r+") * sum";a=.5===i?"inversesqrt("+u+")":1===i?"1.0/("+u+")":"exp(log("+u+") * float(-"+i+"));",this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords.x;\n int r = coords.y;\n int c = coords.z;\n int d = coords.w;\n\n bool hasNextCol = d < "+this.outputShape[3]+";\n bool hasNextRow = c < "+this.outputShape[2]+";\n\n vec4 sum = vec4(0.);\n vec4 xFragAtOutputCoords = getX(b, r, c, d);\n\n vec4 xAtOutputCoords = vec4(\n getChannel(xFragAtOutputCoords, vec2(c, d)),\n hasNextCol ?\n getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,\n hasNextRow ?\n getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,\n (hasNextRow && hasNextCol) ?\n getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0\n );\n\n int firstChannel = d - "+o+";\n vec2 cache = vec2(0.);\n if(firstChannel >= 0){\n vec4 firstChannelFrag = getX(b, r, c, firstChannel);\n cache.x = getChannel(firstChannelFrag, vec2(c, firstChannel));\n if(hasNextRow){\n cache.y = getChannel(firstChannelFrag, vec2(c + 1, firstChannel));\n }\n }\n\n ivec2 depth = ivec2(d, d + 1);\n for (int j = - "+o+"; j <= "+o+"; j++) {\n ivec2 idx = depth + j;\n bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));\n bvec2 belowUpperBound = lessThanEqual(idx, ivec2("+s+"));\n\n bool depthInRange = aboveLowerBound.x && belowUpperBound.x;\n bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;\n\n if(depthInRange || depthPlusOneInRange){\n vec4 z = vec4(0.);\n vec4 xFragAtCurrentDepth;\n z.xz = cache.xy;\n if(depthPlusOneInRange && hasNextCol){\n xFragAtCurrentDepth = idx.y != d ?\n getX(b, r, c, idx.y) : xFragAtOutputCoords;\n z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));\n if(hasNextRow){\n z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));\n }\n }\n cache.xy = z.yw;\n sum += z * z;\n }\n }\n vec4 result = xAtOutputCoords * "+a+";\n setOutput(result);\n }\n "}(t.shape,e,n,r,i):new function(t,e,n,r,i){this.variableNames=["x"],this.outputShape=[];var a,o=e,s=t[3]-1;this.outputShape=t;var u="float("+n+") + float("+r+") * sum";a=.5===i?"inversesqrt("+u+")":1===i?"1.0/("+u+")":"exp(log("+u+") * float(-"+i+"));",this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int r = coords[1];\n int c = coords[2];\n int d = coords[3];\n float x = getX(b, r, c, d);\n float sum = 0.0;\n for (int j = -"+o+"; j <= "+o+"; j++) {\n int idx = d + j;\n if (idx >= 0 && idx <= "+s+") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * "+a+";\n setOutput(val);\n }\n "}(t.shape,e,n,r,i);return this.compileAndRun(a,[t])},e.prototype.LRNGrad=function(t,e,n,r,i,a,o){var s=new function(t,e,n,r,i){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=t,this.depth=t[3],this.depthRadius=e,this.bias=n,this.alpha=r,this.beta=i,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int r = coords[1];\n int c = coords[2];\n\n float result = 0.0;\n for (int d = 0; d < "+this.depth+"; ++d) {\n int depthBegin = int(max(0.0, float(d - "+e+")));\n int depthEnd = int(min(float("+this.depth+"),\n float(d + "+e+" + 1)));\n\n const int MIN_DEPTH_BEGIN = 0;\n const int MAX_DEPTH_END = "+this.depth+";\n\n float norm = 0.0;\n for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd) {\n norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);\n }\n else {\n break;\n }\n }\n\n norm = float("+r+") * norm + float("+n+");\n\n for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd){\n float dyi = -2.0 * float("+r+")\n * float("+i+")\n * getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)\n / norm;\n if (k == d) {\n dyi += pow(norm, -1.0 * "+i+");\n }\n if (k == coords[3]) {\n dyi *= getDy(b, r, c, d);\n result += dyi;\n }\n }\n else {\n break;\n }\n }\n }\n setOutput(result);\n }\n "}(e.shape,r,i,a,o);return this.compileAndRun(s,[e,n,t])},e.prototype.tile=function(t,e){if("string"===t.dtype){var n=this.readSync(t.dataId).map(function(t){return ct(t)});return qa(ar(t.shape,t.dtype,n),e)}var r=new function(t,e){this.variableNames=["A"];for(var n=new Array(t.length),r=0;r5)throw Error("Tile for rank "+e+" is not yet supported");if(1===e)return"imod(resRC, "+t[0]+")";for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[],i=0;i= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",h="",p=0,m=1===r?2:4;p= end) {\n setOutput(float("+n+"));\n } else {\n setOutput(getX(outC - start));\n }\n }\n "}(t.shape,e,n);return this.compileAndRun(r,[t])},e.prototype.gather=function(t,e,n){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.gather(t,e,n);var r=new function(t,e,n){this.variableNames=["A","indices"];var r=t.slice();r[n]=e,this.outputShape=r,this.rank=r.length;var i=oo(this.rank),a=function(t,e){var n=t.length;if(n>4)throw Error("Gather for rank "+n+" is not yet supported");if(1===n)return"int(getIndices(resRC))";for(var r=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],a=0;a 4 with a WebGL backend not implemented yet"});var r=e.reduce(function(t,e){return t*e}),i=yr(t.shape,e,r),a=br(i.length,e.length),o=xr(t.shape,e,r),s=wr(n,e.length),u=kr(o,n,e.length);return fa(t.reshape(i),a).reshape(o).slice(s,u)},e.prototype.spaceToBatchND=function(t,e,n){C(t.rank<=4,function(){return"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet"});var r=e.reduce(function(t,e){return t*e}),i=[[0,0]];i.push.apply(i,n);for(var a=1+e.length;a= 1.0 && floatedReducedAllValue >= 1.0);\n ",d="bvec4"):"any"===e&&(o="0.0",f="\n bool reducedAnyValue = any(values);\n float floatedReducedAnyValue = float(reducedAnyValue);\n anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);\n ",d="bvec4");var h="";i%n>0&&(h="\n if (inIdx < 0 || inIdx >= "+i+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+o+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float getValue(int batch, int inIdx) {\n "+h+"\n return getX(batch, inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+n+";\n\n vec4 minMaxValue = vec4("+o+");\n float prodValue = 1.0;\n float sumValue = 0.0;\n float allValue = 1.0;\n float anyValue = 0.0;\n\n for (int i = 0; i < "+c+"; i += 4) {\n int inIdx = inOffset + i;\n "+d+" values = "+d+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+f+"\n }\n\n int inIdx = inOffset + "+c+";\n if ("+(1===l)+") {\n "+d+" values = "+d+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(2===l)+") {\n "+d+" values = "+d+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(3===l)+") {\n "+d+" values = "+d+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+f+"\n }\n setOutput("+u+");\n }\n "}({windowSize:Vi(i),inSize:i,batchSize:r},e),o=this.compileAndRun(a,[t],n);return 1===o.shape[1]?o:this.reduce(o,e,n)},e.prototype.argReduce=function(t,e,n){void 0===n&&(n=null);var r=t.shape[0],i=t.shape[1];null!=n&&(r=n.shape[0],i=n.shape[1]);var a=new function(t,e,n){this.variableNames=["A"];var r=t.windowSize,i=t.batchSize,a=t.inSize,o=Math.ceil(a/r);n||this.variableNames.push("bestIndicesA"),this.outputShape=[i,o];var s="max"===e?">":"<",u=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+r+";\n\n int bestIndex = inOffset;\n float bestValue = getA(batch, bestIndex);\n\n for (int i = 0; i < "+r+"; i++) {\n int inIdx = "+u+";\n float candidate = getA(batch, inIdx);\n if (candidate "+s+" bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n "}({windowSize:Vi(i),inSize:i,batchSize:r},e,null==n),o=[t];null!=n&&o.push(n);var s=this.compileAndRun(a,o,"int32");return 1===s.shape[1]?s:this.argReduce(t,e,s)},e.prototype.argReducePacked=function(t,e,n){void 0===n&&(n=null);var r=null!=n?n.shape:t.shape,i=new function(t,e,n,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,C(t.length>2,function(){return"Packed arg"+(n.charAt(0).toUpperCase()+n.slice(1))+" supports only inputs with rank above 2."});var i=t[t.length-1],a=Math.ceil(i/e);this.outputShape=t.slice(0,-1),a>1&&this.outputShape.push(a),r||this.variableNames.push("bestIndicesA");var o,s,u=this.outputShape,c=u.length,l=oo(c),f=Ka("coords",c);if(1===a){var d=oo(s=c+1);o="\n "+d+" sourceLocR = "+d+"("+f.join()+", 0);\n ++"+f[c-1]+";\n "+d+" sourceLocG = "+d+"("+f.join()+", 0);\n ++"+f[c-2]+";\n "+d+" sourceLocA = "+d+"("+f.join()+", 0);\n --"+f[c-1]+";\n "+d+" sourceLocB = "+d+"("+f.join()+", 0);\n --"+f[c-2]+";"}else s=c,o="\n "+l+" sourceLocR = coords;\n ++"+f[c-1]+";\n "+l+" sourceLocG = coords;\n ++"+f[c-2]+";\n "+l+" sourceLocA = coords;\n --"+f[c-1]+";\n "+l+" sourceLocB = coords;\n --"+f[c-2]+";";var h=["x","y","z","w","u","v"].slice(0,s),p="."+h[s-1],m=h.map(function(t){return"int "+t}),g=Ka("sourceLocR",s-1).concat("inIdx.r"),v=Ka("sourceLocG",s-1).concat("inIdx.g"),y=Ka("sourceLocB",s-1).concat("inIdx.b"),b=Ka("sourceLocA",s-1).concat("inIdx.a"),x="max"===n?"greaterThan":"lessThan",w=r?"":"\n inIdx = round(vec4(getBestIndicesAChannel("+g.join()+"),\n getBestIndicesAChannel("+v.join()+"),\n getBestIndicesAChannel("+y.join()+"),\n getBestIndicesAChannel("+b.join()+")));",k="vec4(\n getAChannel("+g.join()+"),\n hasNextCol ? getAChannel("+v.join()+") : 0.,\n hasNextRow ? getAChannel("+y.join()+") : 0.,\n hasNextRow && hasNextCol ? getAChannel("+b.join()+") : 0.)",_=r?"":"\n float getBestIndicesAChannel("+m.join()+") {\n return getChannel(getBestIndicesA("+h.join()+"),\n vec2("+h.slice(-2).join()+"));\n }";this.userCode="\n float getAChannel("+m.join()+") {\n return getChannel(getA("+h.join()+"),\n vec2("+h.slice(-2).join()+"));\n }\n "+_+"\n void main() {\n "+l+" coords = getOutputCoords();\n bool hasNextCol = "+f[c-1]+" < "+(u[c-1]-1)+";\n bool hasNextRow = "+f[c-2]+" < "+(u[c-2]-1)+";\n "+o+"\n ivec4 srcIdx = ivec4(sourceLocR"+p+", sourceLocG"+p+",\n sourceLocB"+p+", sourceLocA"+p+") * "+e+";\n ivec4 inIdx = srcIdx;\n vec4 bestIndex = vec4(inIdx);\n vec4 bestValue = "+k+";\n\n for (int i = 0; i < "+e+"; i++) {\n inIdx = srcIdx;\n "+w+"\n vec4 candidate = "+k+";\n bvec4 nan = isnan(candidate);\n bvec4 replace = bvec4(\n vec4("+x+"(candidate, bestValue)) * (vec4(1.0) - vec4(nan)));\n\n bestValue = vec4(replace.x ? candidate.x : bestValue.x,\n replace.y ? candidate.y : bestValue.y,\n replace.z ? candidate.z : bestValue.z,\n replace.w ? candidate.w : bestValue.w);\n bestIndex = mix(bestIndex, vec4(inIdx), vec4(replace));\n srcIdx++;\n }\n setOutput(bestIndex);\n }\n "}(r,Vi(r[r.length-1]),e,null==n),a=null==n?[t]:[t,n],o=this.compileAndRun(i,a,"int32");return o.rank===t.rank?this.argReducePacked(t,e,o):o},e.prototype.sum=function(t,e){Sn("sum",e,t.rank);var n=En(t.shape,e),r=n[0],i=I(n[1]),a=t.as2D(-1,i),o=Rt(t.dtype);return this.reduce(a,"sum",o).reshape(r)},e.prototype.prod=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.prod(t,e);var n=En(t.shape,e),r=n[0],i=I(n[1]),a=t.as2D(-1,i),o=Rt(t.dtype);return this.reduce(a,"prod",o).reshape(r)},e.prototype.unsortedSegmentSum=function(t,e,n){var r=0,i=Cn([r],t.rank),a=t;null!=i&&(a=fa(t,i),r=Nn(1,t.rank)[0]);var o=function(t,e,n){for(var r=[],i=t.length,a=0;ae||n===t?r=!0:n=tt(t,n+1);return n}(o,i),u=new function(t,e){this.variableNames=["x","segmentIds"];var n=t.windowSize,r=t.batchSize,i=t.inSize,a=t.numSegments,o=a*Math.ceil(i/n);this.outputShape=[r,o];var s=4*Math.floor(n/4),u=n%4,c="\n sumValue += dot(values, segFilter);\n ",l="";i%n>0&&(l="\n if (inIdx < 0 || inIdx >= "+i+") {\n return initializationValue;\n }\n ");var f="";i%n>0&&(f="\n if (inIdx < 0 || inIdx >= "+i+") {\n return -1.0;\n }\n "),this.userCode="\n const float initializationValue = 0.0;\n\n float getValue(int batch, int inIdx) {\n "+l+"\n return getX(batch, inIdx);\n }\n\n float getSegmentIdAtIndex(int inIdx) {\n "+f+"\n return getSegmentIds(inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = int(floor(float(outIdx) / float(\n "+a+")) * float("+n+"));\n int currentSeg = int(mod(float(outIdx), float("+a+")));\n\n float sumValue = 0.0;\n\n for (int i = 0; i < "+s+"; i += 4) {\n int inIdx = inOffset + i;\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0\n );\n\n "+c+"\n }\n\n int inIdx = inOffset + "+s+";\n if ("+(1===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n int inIdxSeg = int(getSegmentIdAtIndex(inIdx));\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n 0,\n 0,\n 0\n );\n\n "+c+"\n } else if ("+(2===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n 0,\n 0\n );\n\n "+c+"\n } else if ("+(3===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n 0\n );\n\n "+c+"\n }\n setOutput(sumValue);\n }\n "}({windowSize:s,inSize:o,batchSize:a,numSegments:i},e),c=this.compileAndRun(u,[t,n],r);return c.shape[1]===i?c:(n=Yn(0,i).tile([o/s]),this.segOpCompute(c,e,n,r,i))},e.prototype.argMinMaxReduce=function(t,e,n){var r=[e];if(Sn("arg"+n.charAt(0).toUpperCase()+n.slice(1),r,t.rank),!f().getBool("WEBGL_PACK_REDUCE")||t.rank<=2){var i=En(t.shape,r),a=i[0],o=I(i[1]),s=t.as2D(-1,o);return this.argReduce(s,n).reshape(a)}return this.argReducePacked(t,n)},e.prototype.argMin=function(t,e){return this.argMinMaxReduce(t,e,"min")},e.prototype.argMax=function(t,e){return this.argMinMaxReduce(t,e,"max")},e.prototype.cumsum=function(t,e,n,r){if(e!==t.rank-1)throw new Error("WebGL cumsum shader expects an inner-most axis="+(t.rank-1)+" but got axis="+e);var i=new function(t,e,n){this.variableNames=["x"],this.outputShape=t;var r=t.length,i=t[t.length-1],a=n?"<":">";this.userCode="\n int getIndex(int i) {\n "+(n?"return "+i+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+oo(r)+" coords = getOutputCoords();\n int end = "+Eo(r,"coords")+";\n float val = 0.0;\n for (int i = "+i+" - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx "+a+" end) {\n continue;\n }\n if (idx == end && "+e+") {\n continue;\n }\n "+Eo(r,"coords")+" = idx;\n val += getX("+function(t,e){if(1===t)return""+e;if(2===t)return e+".x, "+e+".y";if(3===t)return e+".x, "+e+".y, "+e+".z";if(4===t)return e+".x, "+e+".y, "+e+".z, "+e+".w";throw Error("Cumulative sum for rank "+t+" is not yet supported")}(r,"coords")+");\n }\n setOutput(val);\n }\n "}(t.shape,n,r);return this.compileAndRun(i,[t])},e.prototype.equal=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(equal(a, b));\n","bool");var n=new mo("return float(a == b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.notEqual=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(notEqual(a, b));\n","bool");var n=new mo("return float(a != b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.less=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.less(t,e);if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(lessThan(a, b));\n","bool");var n=new mo("return float(a < b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.lessEqual=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(lessThanEqual(a, b));\n","bool");var n=new mo("return float(a <= b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.greater=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.greater(t,e);if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(greaterThan(a, b));\n","bool");var n=new mo("return float(a > b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.greaterEqual=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(greaterThanEqual(a, b));\n","bool");var n=new mo("return float(a >= b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.logicalNot=function(t){var e=new ss(t.shape,"return float(!(x >= 1.0));");return this.compileAndRun(e,[t])},e.prototype.logicalAnd=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(\n vec4(greaterThanEqual(a, vec4(1.0))) *\n vec4(greaterThanEqual(b, vec4(1.0))));\n","bool");var n=new mo("return float(a >= 1.0 && b >= 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.logicalOr=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return min(\n vec4(greaterThanEqual(a, vec4(1.0))) +\n vec4(greaterThanEqual(b, vec4(1.0))),\n vec4(1.0));\n","bool");var n=new mo("return float(a >= 1.0 || b >= 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},e.prototype.select=function(t,e,n){var r=new function(t,e,n){var r,i;if(this.variableNames=["c","a","b"],this.outputShape=e,n>4)throw Error("Where for rank "+n+" is not yet supported");if(1===n)i="resRC",r="resRC";else{for(var a=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],s=[],u=0;u= 1.0) {\n setOutput(getA("+i+"));\n } else {\n setOutput(getB("+i+"));\n }\n }\n "}(t.rank,e.shape,e.rank);return this.compileAndRun(r,[t,e,n],Mt(e.dtype,n.dtype))},e.prototype.where=function(t){vn("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");var e=t.dataSync();return Ga(t.shape,e)},e.prototype.topk=function(t,e,n){return Ha(t.dataSync(),t.shape,t.dtype,e)},e.prototype.min=function(t,e){Sn("min",e,t.rank);var n=En(t.shape,e),r=n[0],i=I(n[1]),a=t.as2D(-1,i);return this.reduce(a,"min",a.dtype).reshape(r)},e.prototype.minimum=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.minimum(t,e);var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo("\n vec4 result = vec4(min(a, b));\n vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new mo("\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n\n return min(a, b);\n",t.shape,e.shape);return this.compileAndRun(n,[t,e])},e.prototype.mod=function(t,e){var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo("\n vec4 result = mod(a, b);\n vec4 isNaN = vec4(equal(b, vec4(0.0)));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new mo("if (b == 0.0) return NAN;\n return mod(a, b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},e.prototype.max=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.max(t,e);Sn("max",e,t.rank);var n=En(t.shape,e),r=n[0],i=I(n[1]),a=t.as2D(-1,i);return this.reduce(a,"max",a.dtype).reshape(r)},e.prototype.maximum=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.maximum(t,e);var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo("\n vec4 result = vec4(max(a, b));\n vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new mo("\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n\n return max(a, b);\n",t.shape,e.shape);return this.compileAndRun(n,[t,e])},e.prototype.all=function(t,e){Sn("all",e,t.rank);var n=En(t.shape,e),r=n[0],i=I(n[1]),a=t.as2D(-1,i);return this.reduce(a,"all",a.dtype).reshape(r)},e.prototype.any=function(t,e){Sn("any",e,t.rank);var n=En(t.shape,e),r=n[0],i=I(n[1]),a=t.as2D(-1,i);return this.reduce(a,"any",a.dtype).reshape(r)},e.prototype.floorDiv=function(t,e){if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n ivec4 ia = round(a);\n ivec4 ib = round(b);\n bvec4 cond = notEqual(ib, ivec4(0));\n ivec4 result = ivec4(0);\n vec4 s = sign(a) * sign(b);\n\n // Windows (D3D) wants guaranteed non-zero int division at compile-time.\n if (cond[0]) {\n result[0] = idiv(ia[0], ib[0], s[0]);\n }\n if (cond[1]) {\n result[1] = idiv(ia[1], ib[1], s[1]);\n }\n if (cond[2]) {\n result[2] = idiv(ia[2], ib[2], s[2]);\n }\n if (cond[3]) {\n result[3] = idiv(ia[3], ib[3], s[3]);\n }\n return vec4(result);\n","int32");var n=new mo("\n float s = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n if (ib != 0) {\n // Windows (D3D) wants guaranteed non-zero int division at compile-time.\n return float(idiv(ia, ib, s));\n } else {\n return NAN;\n }\n",t.shape,e.shape);return this.compileAndRun(n,[t,e],"int32")},e.prototype.add=function(t,e){if("complex64"===t.dtype&&"complex64"===e.dtype)return this.complexSeparableBinaryOp(t,e,lo);if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.add(t,e);var n=Mt(t.dtype,e.dtype);if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,lo,n);var r=new mo(lo,t.shape,e.shape);return this.compileAndRun(r,[t,e],n)},e.prototype.packedUnaryOp=function(t,e,n){var r=new Es(t.shape,e);return this.compileAndRun(r,[t],n)},e.prototype.packedBinaryOp=function(t,e,n,r,i){void 0===i&&(i=!1);var a=new vo(n,t.shape,e.shape,i);return this.compileAndRun(a,[t,e],r)},e.prototype.complexSeparableBinaryOp=function(t,e,n){var r=this,i=this.texData.get(t.dataId),a=this.texData.get(e.dataId),o=[[i.complexTensors.real,a.complexTensors.real],[i.complexTensors.imag,a.complexTensors.imag]].map(function(i){var a=i[0],o=i[1],s=r.makeComplexComponentTensorInfo(t,a),u=r.makeComplexComponentTensorInfo(e,o),c=new mo(n,t.shape,e.shape);return r.compileAndRun(c,[s,u],Mt(a.dtype,o.dtype))}),s=o[0],u=o[1],c=this.complex(s,u);return s.dispose(),u.dispose(),c},e.prototype.makeComplexComponentTensorInfo=function(t,e){return{dataId:e.dataId,dtype:e.dtype,shape:t.shape}},e.prototype.addN=function(t){if(1===t.length)return t[0];if(t.length>f().get("WEBGL_MAX_TEXTURES_IN_SHADER")){var e=Math.floor(t.length/2),n=this.addN(t.slice(0,e)),r=this.addN(t.slice(e));return this.addN([n,r])}var i=t.map(function(t){return t.dtype}).reduce(function(t,e){return Mt(t,e)}),a=t.map(function(t){return t.shape}),o=f().getBool("WEBGL_PACK")?new function(t,e){this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.variableNames=e.map(function(t,e){return"T"+e});var n=[];this.variableNames.forEach(function(t){n.push("vec4 v"+t+" = get"+t+"AtOutCoords();")});var r=this.variableNames.map(function(t){return"v"+t}).join(" + ");this.userCode="\n void main() {\n "+n.join("\n ")+"\n\n vec4 result = "+r+";\n setOutput(result);\n }\n "}(t[0].shape,a):new function(t,e){this.outputShape=[],this.outputShape=t,this.variableNames=e.map(function(t,e){return"T"+e});var n=[];this.variableNames.forEach(function(t){n.push("float v"+t+" = get"+t+"AtOutCoords();")});var r=this.variableNames.map(function(t){return"v"+t}).join(" + ");this.userCode="\n void main() {\n "+n.join("\n ")+"\n\n float result = "+r+";\n setOutput(result);\n }\n "}(t[0].shape,a);return this.compileAndRun(o,t,i)},e.prototype.subtract=function(t,e){if("complex64"===t.dtype&&"complex64"===e.dtype)return this.complexSeparableBinaryOp(t,e,fo);if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.subtract(t,e);var n=Mt(t.dtype,e.dtype);if(f().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,fo,t.dtype);var r=new mo(fo,t.shape,e.shape);return this.compileAndRun(r,[t,e],n)},e.prototype.pow=function(t,e){var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo("\n // isModRound1 has 1 for components with round(mod(b, 2.0)) == 1, 0 otherwise.\n vec4 isModRound1 = vec4(equal(round(mod(b, 2.0)), ivec4(1)));\n vec4 multiplier = sign(a) * isModRound1 + (vec4(1.0) - isModRound1);\n vec4 result = multiplier * pow(abs(a), b);\n\n // Ensure that a^0 = 1, including 0^0 = 1 as this correspond to TF and JS\n bvec4 isExpZero = equal(b, vec4(0.0));\n result.r = isExpZero.r ? 1.0 : result.r;\n result.g = isExpZero.g ? 1.0 : result.g;\n result.b = isExpZero.b ? 1.0 : result.b;\n result.a = isExpZero.a ? 1.0 : result.a;\n\n vec4 isNaN = vec4(lessThan(a, vec4(0.0))) * vec4(lessThan(floor(b), b));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new mo("\nif(a < 0.0 && floor(b) < b){\n return NAN;\n}\nif (b == 0.0) {\n return 1.0;\n}\nreturn (round(mod(b, 2.0)) != 1) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n",t.shape,e.shape),r=Mt(t.dtype,e.dtype);return this.compileAndRun(n,[t,e],r)},e.prototype.ceil=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.ceil(t);if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,ms,t.dtype);var e=new ss(t.shape,ms);return this.compileAndRun(e,[t])},e.prototype.floor=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.floor(t);if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,gs,t.dtype);var e=new ss(t.shape,gs);return this.compileAndRun(e,[t])},e.prototype.sign=function(t){var e=new ss(t.shape,"\n if (isnan(x)) { return 0.0; }\n return sign(x);\n");return this.compileAndRun(e,[t])},e.prototype.isNaN=function(t){var e=new ss(t.shape,"return float(isnan(x));");return this.compileAndRun(e,[t],"bool")},e.prototype.isInf=function(t){var e=new ss(t.shape,"return float(isinf(x));");return this.compileAndRun(e,[t],"bool")},e.prototype.isFinite=function(t){var e=new ss(t.shape,"return float(!isnan(x) && !isinf(x));");return this.compileAndRun(e,[t],"bool")},e.prototype.round=function(t){var e=new ss(t.shape,"\n // OpenGL ES does not support round function.\n // The algorithm is based on banker's rounding.\n float base = floor(x);\n if ((x - base) < 0.5) {\n return floor(x);\n } else if ((x - base) > 0.5) {\n return ceil(x);\n } else {\n if (mod(base, 2.0) == 0.0) {\n return base;\n } else {\n return base + 1.0;\n }\n }\n");return this.compileAndRun(e,[t])},e.prototype.exp=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.exp(t);if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,vs,t.dtype);var e=new ss(t.shape,vs);return this.compileAndRun(e,[t])},e.prototype.expm1=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.expm1(t);if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,ys,t.dtype);var e=new ss(t.shape,ys);return this.compileAndRun(e,[t])},e.prototype.softmax=function(t,e){var n=z([e],t.shape),r=this.max(t,n),i=On(r.shape,n),a=this.subtract(t,r.reshape(i)),o=this.exp(a),s=this.sum(o,n).reshape(i);return zi(o,s)},e.prototype.log=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.log(t);if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,"\n vec4 result = log(x);\n vec4 isNaN = vec4(lessThan(x, vec4(0.0)));\n result.r = isNaN.r == 1.0 ? NAN : result.r;\n result.g = isNaN.g == 1.0 ? NAN : result.g;\n result.b = isNaN.b == 1.0 ? NAN : result.b;\n result.a = isNaN.a == 1.0 ? NAN : result.a;\n\n return result;\n",t.dtype);var e=new ss(t.shape,"if (x < 0.0) return NAN;\n return log(x);");return this.compileAndRun(e,[t])},e.prototype.log1p=function(t){var e=new ss(t.shape,"return log(1.0 + x);");return this.compileAndRun(e,[t])},e.prototype.sqrt=function(t){var e=new ss(t.shape,"return sqrt(x);");return this.compileAndRun(e,[t])},e.prototype.rsqrt=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.rsqrt(t);var e=new ss(t.shape,"return inversesqrt(x);");return this.compileAndRun(e,[t])},e.prototype.reciprocal=function(t){var e=new ss(t.shape,"return 1.0 / x;");return this.compileAndRun(e,[t])},e.prototype.relu=function(t){var e;return e=f().getBool("WEBGL_PACK")?new Es(t.shape,ws):new ss(t.shape,fs),this.compileAndRun(e,[t])},e.prototype.relu6=function(t){var e;return e=f().getBool("WEBGL_PACK")?new Es(t.shape,ks):new ss(t.shape,ds),this.compileAndRun(e,[t])},e.prototype.prelu=function(t,e){var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo(go,t.shape,e.shape):new mo(po,t.shape,e.shape);return this.compileAndRun(n,[t,e])},e.prototype.elu=function(t){if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,_s,t.dtype);var e=new ss(t.shape,hs);return this.compileAndRun(e,[t])},e.prototype.eluDer=function(t,e){var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo("\n vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));\n return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));\n",t.shape,e.shape):new mo("return (b >= 1.0) ? a : a * (b + 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},e.prototype.selu=function(t){var e=new ss(t.shape,"\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = 1.7580993408473768;\n float scale = 1.0507009873554805;\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n");return this.compileAndRun(e,[t])},e.prototype.int=function(t){var e=new ss(t.shape,"return float(int(x));");return this.compileAndRun(e,[t],"int32")},e.prototype.clip=function(t,e,n){var r,i=(r=f().getBool("WEBGL_PACK_CLIP")?new bo(t.shape):new yo(t.shape)).getCustomSetupFunc(e,n);return this.compileAndRun(r,[t],null,i)},e.prototype.abs=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.abs(t);if(f().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,ls,t.dtype);var e=new ss(t.shape,ls);return this.compileAndRun(e,[t])},e.prototype.complexAbs=function(t){var e=this.texData.get(t.dataId),n=new function(t){this.variableNames=["real","imag"],this.outputShape=t,this.userCode="\n void main() {\n float re = abs(getRealAtOutCoords());\n float im = abs(getImagAtOutCoords());\n float mx = max(re, im);\n\n // sadly the length function in glsl is not underflow-safe\n // (at least not on Intel GPUs). So the safe solution is\n // to ensure underflow-safety in all cases.\n setOutput(\n mx == 0.0 ? 0.0 : mx * length(vec2(1, min(re, im)/mx))\n );\n }\n "}(t.shape),r=[this.makeComplexComponentTensorInfo(t,e.complexTensors.real),this.makeComplexComponentTensorInfo(t,e.complexTensors.imag)];return this.compileAndRun(n,r)},e.prototype.sigmoid=function(t){var e=new ss(t.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(e,[t])},e.prototype.softplus=function(t){var e=new ss(t.shape,"\n float epsilon = 1.1920928955078125e-7;\n float threshold = log(epsilon) + 2.0;\n\n bool too_large = x > -threshold;\n bool too_small = x < threshold;\n\n float result;\n float exp_x = exp(x);\n\n if (too_large){\n result = x;\n }\n else if (too_small){\n result = exp_x;\n }\n else{\n result = log(exp_x + 1.0);\n }\n return result;\n");return this.compileAndRun(e,[t])},e.prototype.sin=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n return sin(x);\n");return this.compileAndRun(e,[t])},e.prototype.cos=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n return cos(x);\n");return this.compileAndRun(e,[t])},e.prototype.tan=function(t){var e=new ss(t.shape,"return tan(x);");return this.compileAndRun(e,[t])},e.prototype.asin=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n if (abs(x) > 1.) {\n return NAN;\n }\n return asin(x);\n");return this.compileAndRun(e,[t])},e.prototype.acos=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n if (abs(x) > 1.) {\n return NAN;\n }\n return acos(x);\n");return this.compileAndRun(e,[t])},e.prototype.atan=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n return atan(x);\n");return this.compileAndRun(e,[t])},e.prototype.atan2=function(t,e){var n=f().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new vo("\n vec4 result = atan(a, b);\n vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new mo("\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n\n return atan(a, b);\n",t.shape,e.shape);return this.compileAndRun(n,[t,e])},e.prototype.sinh=function(t){var e=new ss(t.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},e.prototype.cosh=function(t){var e=new ss(t.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},e.prototype.tanh=function(t){var e=new ss(t.shape,"\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n");return this.compileAndRun(e,[t])},e.prototype.asinh=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;return log(x + sqrt(x * x + 1.0));");return this.compileAndRun(e,[t])},e.prototype.acosh=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n if (x < 1.0) return NAN;\n return log(x + sqrt(x * x - 1.0));");return this.compileAndRun(e,[t])},e.prototype.atanh=function(t){var e=new ss(t.shape,"if (isnan(x)) return x;\n if ((x < -1.0) || (x > 1.0)) return NAN;\n return (log(1.0 + x) - log(1.0 - x)) / 2.0;");return this.compileAndRun(e,[t])},e.prototype.erf=function(t){var e=new ss(t.shape,'\n // Error function is calculated approximately with elementary function.\n // See "Handbook of Mathematical Functions with Formulas,\n // Graphs, and Mathematical Tables", Abramowitz and Stegun.\n float p = 0.3275911;\n float a1 = 0.254829592;\n float a2 = -0.284496736;\n float a3 = 1.421413741;\n float a4 = -1.453152027;\n float a5 = 1.061405429;\n\n float sign = sign(x);\n x = abs(x);\n float t = 1.0 / (1.0 + p * x);\n return sign * (1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x));\n');return this.compileAndRun(e,[t])},e.prototype.step=function(t,e){var n=new ss(t.shape,function(t){return void 0===t&&(t=0),us+"\n return x > 0.0 ? 1.0 : float("+t+");\n "}(e));return this.compileAndRun(n,[t])},e.prototype.conv2dByMatMul=function(t,e,n,r,i,a){var o=t.shape,s=this.texData.get(t.dataId),u=n.inChannels,c=o[0]*o[1]*o[2],l=n.outChannels,d="channelsLast"===n.dataFormat,h=(1===c||1===l)&&u>1e3,p=o[2]%2!=0&&!!s.isPacked;if(h||!f().getBool("WEBGL_LAZILY_UNPACK")||!f().getBool("WEBGL_PACK_BINARY_OPERATIONS")||!p){var m=d?o[0]*o[1]*o[2]:o[0]*o[2]*o[3],g=this.reshape(t,[1,m,n.inChannels]),v=this.reshape(e,[1,n.inChannels,n.outChannels]);return this.reshape(this.fusedBatchMatMul({a:g,b:v,transposeA:!1,transposeB:!1,bias:r,activation:i,preluActivationWeights:a}),n.outShape)}var y=d?o[0]*o[1]*(o[2]+1):o[0]*o[2]*(o[3]+1),b={dataId:t.dataId,shape:[1,y,n.inChannels],dtype:t.dtype},x=s.shape;s.shape=s.shape.slice(),s.shape[s.shape.length-2]++,C(Pe(s.shape,b.shape),function(){return"packed reshape "+s.shape+" to "+b.shape+" isn't free"});var w=this.reshape(e,[1,n.inChannels,n.outChannels]),k=this.fusedBatchMatMul({a:b,b:w,transposeA:!1,transposeB:!1,bias:r,activation:i,preluActivationWeights:a}),_=this.texData.get(k.dataId);return C(_.isPacked,function(){return"batchMatMul result is expected to be packed"}),s.shape=x,_.shape=n.outShape,Wt.makeTensorFromDataId(k.dataId,n.outShape,k.dtype)},e.prototype.conv2dWithIm2Row=function(t,e,n,r,i,a){var o=n.filterWidth,s=n.filterHeight,u=n.inChannels,c=n.outWidth,l=n.outHeight,f="channelsLast"===n.dataFormat,d=o*s*u,h=l*c,p=[d,h],m=t.squeeze([0]),g=e.reshape([1,d,-1]),v=new function(t,e,n){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t;for(var r=n.filterWidth,i=n.inChannels,a=n.strideWidth,o=n.strideHeight,s=n.padInfo,u=n.outWidth,c=n.dilationWidth,l=n.dilationHeight,f=n.dataFormat,d=s.left,h=s.top,p=i*r,m=Xa(),g="channelsLast"===f,v=g?0:1,y=g?1:2,b="",x=0;x<=1;x++)for(var w=0;w<=1;w++)b+="\n blockIndex = rc.y + "+w+";\n pos = rc.x + "+x+";\n\n if(blockIndex < "+t[1]+" && pos < "+t[0]+") {\n offsetY = int(blockIndex / ("+u+")) * "+o+" - "+h+";\n d0 = offsetY + "+l+" * (pos / "+p+");\n\n if(d0 < "+e[v]+" && d0 >= 0) {\n\n offsetX = int(mod(float(blockIndex), "+u+".) * "+a+". - "+d+".);\n d1 = offsetX + "+c+" * (int(mod(float(pos), "+p+".) / "+i+".));\n\n if(d1 < "+e[y]+" && d1 >= 0) {\n\n ch = int(mod(float(pos), "+i+".));\n\n if ("+g+") {\n innerDims = vec2(d1, ch);\n result["+(2*x+w)+"] = getChannel(\n getA(d0, int(innerDims.x),\n int(innerDims.y)), innerDims);\n } else {\n innerDims = vec2(d0, d1);\n result["+(2*x+w)+"] = getChannel(\n getA(ch, int(innerDims.x),\n int(innerDims.y)), innerDims);\n }\n }\n }\n }\n ";this.userCode="\n void main() {\n ivec2 rc = getOutputCoords();\n\n vec4 result = vec4(0);\n\n int blockIndex, pos, offsetY, d0, offsetX, d1, ch;\n vec2 innerDims;\n\n "+b+"\n\n "+m.output+" = result;\n }\n "}(p,m.shape,n),y=this.compileAndRun(v,[m]).reshape([1,p[0],p[1]]),b=null!=r,x=null!=a,w=i?Ss(i,!0):null,k=new Ko(y.shape,[1,h,n.outChannels],!0,!1,b,w,x),_=[y,g];r&&_.push(r),x&&_.push(a);var E=this.compileAndRun(k,_);return f?E.reshape([1,l,c,n.outChannels]):E.reshape([1,n.outChannels,l,c])},e.prototype.fusedConv2d=function(t){var e=t.input,n=t.filter,r=t.convInfo,i=t.bias,a=t.activation,o=t.preluActivationWeights;if(1===r.filterHeight&&1===r.filterWidth&&1===r.dilationHeight&&1===r.dilationWidth&&1===r.strideHeight&&1===r.strideWidth&&("SAME"===r.padInfo.type||"VALID"===r.padInfo.type))return this.conv2dByMatMul(e,n,r,i,a,o);if(f().getBool("WEBGL_CONV_IM2COL")&&1===e.shape[0])return this.conv2dWithIm2Row(e,n,r,i,a,o);var s=null!=i,u=null!=o,c=a?Ss(a,!1):null,l=new wo(r,s,c,u),d=[e,n];return i&&d.push(i),o&&d.push(o),this.compileAndRun(l,d)},e.prototype.conv2d=function(t,e,n){if(1===n.filterHeight&&1===n.filterWidth&&1===n.dilationHeight&&1===n.dilationWidth&&1===n.strideHeight&&1===n.strideWidth&&("SAME"===n.padInfo.type||"VALID"===n.padInfo.type))return this.conv2dByMatMul(t,e,n);if(f().getBool("WEBGL_CONV_IM2COL")&&1===t.shape[0])return this.conv2dWithIm2Row(t,e,n);var r=new wo(n);return this.compileAndRun(r,[t,e])},e.prototype.conv2dDerInput=function(t,e,n){var r=new function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,i=t.strideWidth,a="channelsLast"===t.dataFormat,o=e-1-t.padInfo.top,s=n-1-t.padInfo.left,u=a?1:2,c=a?2:3,l=a?3:1;this.userCode="\n const ivec2 pads = ivec2("+o+", "+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords["+l+"];\n\n ivec2 dyCorner = ivec2(coords["+u+"], coords["+c+"]) - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+e+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+e+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+i+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+t.outChannels+"; d2++) {\n\n if ("+a+") {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n } else {\n float xValue = getDy(batch, d2, idyR, idyC);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.conv2dDerFilter=function(t,e,n){var r=new function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,n=t.strideWidth,r=t.padInfo.top,i=t.padInfo.left,a="channelsLast"===t.dataFormat;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int d2 = coords.w;\n\n // Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n\n for (int b = 0; b < "+t.batchSize+"; b++) {\n for (int yR = 0; yR < "+t.outHeight+"; yR++) {\n int xR = wR + yR * "+e+" - "+r+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+t.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+i+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n if ("+a+") {\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n } else {\n float dyValue = getDy(b, d2, yR, yC);\n float xValue = getX(b, d1, xR, xC);\n dotProd += (xValue * dyValue);\n }\n\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.fusedDepthwiseConv2D=function(t){var e,n=t.input,r=t.filter,i=t.convInfo,a=t.bias,o=t.activation,s=t.preluActivationWeights,u=f().getBool("WEBGL_PACK_DEPTHWISECONV")&&i.strideWidth<=2&&i.outChannels/i.inChannels==1,c=o?Ss(o,u):null,l=[n,r],d=null!=a,h=null!=s;return d&&l.push(a),h&&l.push(s),u?(e=new _o(i,d,c,h),this.compileAndRun(e,l)):(e=new ko(i,d,c,h),this.compileAndRun(e,l))},e.prototype.depthwiseConv2D=function(t,e,n){var r;return f().getBool("WEBGL_PACK_DEPTHWISECONV")&&n.strideWidth<=2&&n.outChannels/n.inChannels==1?(r=new _o(n),this.compileAndRun(r,[t,e])):(r=new ko(n),this.compileAndRun(r,[t,e]))},e.prototype.depthwiseConv2DDerInput=function(t,e,n){var r=new function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,i=t.strideWidth,a=e-1-t.padInfo.top,o=n-1-t.padInfo.left,s=t.outChannels/t.inChannels;this.userCode="\n const ivec2 pads = ivec2("+a+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n float dotProd = 0.0;\n\n for (int wR = 0; wR < "+e+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+e+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+i+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n // TO DO: Vec4 over the channelMul\n for (int dm = 0; dm < "+s+"; dm++) {\n int d2 = d1 * "+s+" + dm;\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, dm);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.depthwiseConv2DDerFilter=function(t,e,n){var r=new function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,n=t.strideWidth,r=t.padInfo.top,i=t.padInfo.left,a=t.outChannels/t.inChannels;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int dm = coords.w;\n int d2 = d1 * "+a+" + dm;\n\n float dotProd = 0.0;\n\n // TO DO: Vec4 over the batch size\n for (int b = 0; b < "+t.batchSize+"; b++) {\n for (int yR = 0; yR < "+t.outHeight+"; yR++) {\n int xR = wR + yR * "+e+" - "+r+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+t.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+i+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.conv3d=function(t,e,n){var r=new function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.padInfo.front,n=t.padInfo.top,r=t.padInfo.left,i=t.strideDepth,a=t.strideHeight,o=t.strideWidth,s=t.dilationDepth,u=t.dilationHeight,c=t.dilationWidth,l=t.filterDepth,f=t.filterHeight,d=t.filterWidth,h=4*Math.floor(t.inChannels/4),p=t.inChannels%4;this.userCode="\n const ivec3 strides = ivec3("+i+", "+a+", "+o+");\n const ivec3 pads = ivec3("+e+", "+n+", "+r+");\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int d2 = coords.u;\n\n ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;\n int xFCorner = xFRCCorner.x;\n int xRCorner = xFRCCorner.y;\n int xCCorner = xFRCCorner.z;\n\n // Convolve x(?, ?, ?, d1) with w(:, :, :, d1, d2) to get\n // y(yF, yR, yC, d2). ? = to be determined. : = across all\n // values in that axis.\n float dotProd = 0.0;\n for (int wF = 0; wF < "+l+"; wF++) {\n int xF = xFCorner + wF * "+s+";\n\n if (xF < 0 || xF >= "+t.inDepth+") {\n continue;\n }\n\n for (int wR = 0; wR < "+f+"; wR++) {\n int xR = xRCorner + wR * "+u+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+d+"; wC++) {\n int xC = xCCorner + wC * "+c+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+h+"; d1 += 4) {\n vec4 xValues = vec4(\n getX(batch, xF, xR, xC, d1),\n getX(batch, xF, xR, xC, d1 + 1),\n getX(batch, xF, xR, xC, d1 + 2),\n getX(batch, xF, xR, xC, d1 + 3)\n );\n vec4 wValues = vec4(\n getW(wF, wR, wC, d1, d2),\n getW(wF, wR, wC, d1 + 1, d2),\n getW(wF, wR, wC, d1 + 2, d2),\n getW(wF, wR, wC, d1 + 3, d2)\n );\n\n dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===p)+") {\n dotProd +=\n getX(batch, xF, xR, xC, "+h+") *\n getW(wF, wR, wC, "+h+", d2);\n } else if ("+(2===p)+") {\n vec2 xValues = vec2(\n getX(batch, xF, xR, xC, "+h+"),\n getX(batch, xF, xR, xC, "+h+" + 1)\n );\n vec2 wValues = vec2(\n getW(wF, wR, wC, "+h+", d2),\n getW(wF, wR, wC, "+h+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===p)+") {\n vec3 xValues = vec3(\n getX(batch, xF, xR, xC, "+h+"),\n getX(batch, xF, xR, xC, "+h+" + 1),\n getX(batch, xF, xR, xC, "+h+" + 2)\n );\n vec3 wValues = vec3(\n getW(wF, wR, wC, "+h+", d2),\n getW(wF, wR, wC, "+h+" + 1, d2),\n getW(wF, wR, wC, "+h+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.conv3dDerInput=function(t,e,n){var r=new function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterDepth,n=t.filterHeight,r=t.filterWidth,i=t.strideDepth,a=t.strideHeight,o=t.strideWidth,s=e-1-t.padInfo.front,u=n-1-t.padInfo.top,c=r-1-t.padInfo.left;this.userCode="\n const ivec3 pads = ivec3("+s+", "+u+", "+c+");\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int d1 = coords.u;\n\n\n ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;\n int dyFCorner = dyCorner.x;\n int dyRCorner = dyCorner.y;\n int dyCCorner = dyCorner.z;\n\n float dotProd = 0.0;\n for (int wF = 0; wF < "+e+"; wF++) {\n float dyF = float(dyFCorner + wF) / "+i+".0;\n\n if (dyF < 0.0 || dyF >= "+t.outDepth+".0 || fract(dyF) > 0.0) {\n continue;\n }\n int idyF = int(dyF);\n\n int wFPerm = "+e+" - 1 - wF;\n\n for (int wR = 0; wR < "+n+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+a+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 ||\n fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+n+" - 1 - wR;\n\n for (int wC = 0; wC < "+r+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+o+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+r+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+t.outChannels+"; d2++) {\n float xValue = getDy(batch, idyF, idyR, idyC, d2);\n float wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.conv3dDerFilter=function(t,e,n){var r=new function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideDepth,n=t.strideHeight,r=t.strideWidth,i=t.padInfo.front,a=t.padInfo.top,o=t.padInfo.left;this.userCode="\n void main() {\n ivec5 coords = getOutputCoords();\n int wF = coords.x;\n int wR = coords.y;\n int wC = coords.z;\n int d1 = coords.w;\n int d2 = coords.u;\n\n float dotProd = 0.0;\n\n for (int b = 0; b < "+t.batchSize+"; b++) {\n for (int yF = 0; yF < "+t.outDepth+"; yF++) {\n int xF = wF + yF * "+e+" - "+i+";\n\n if (xF < 0 || xF >= "+t.inDepth+") {\n continue;\n }\n\n for (int yR = 0; yR < "+t.outHeight+"; yR++) {\n int xR = wR + yR * "+n+" - "+a+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+t.outWidth+"; yC++) {\n int xC = wC + yC * "+r+" - "+o+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yF, yR, yC, d2);\n float xValue = getX(b, xF, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},e.prototype.maxPool=function(t,e){var n=new Yo(e,"max",!1);return this.compileAndRun(n,[t])},e.prototype.avgPool=function(t,e){var n=new Yo(e,"avg",!1);return this.compileAndRun(n,[t],"float32")},e.prototype.maxPoolBackprop=function(t,e,n,r){var i=new Yo(r,"max",!0),a=this.compileAndRun(i,[e]),o=new function(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideHeight,n=t.strideWidth,r=t.dilationHeight,i=t.effectiveFilterHeight,a=t.effectiveFilterWidth,o=i-1-t.padInfo.top,s=a-1-t.padInfo.left,u=i*a-1;this.userCode="\n const ivec2 pads = ivec2("+o+", "+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+i+";\n wR += "+r+") {\n float dyR = float(dyRCorner + wR) / "+e+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+a+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+n+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n int maxPosValue = "+u+" - int(getMaxPos(b, idyR, idyC, d));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue = wR * "+a+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n "}(r),s=this.compileAndRun(o,[t,a],e.dtype);return a.dispose(),s},e.prototype.avgPoolBackprop=function(t,e,n){var r=new function(t){this.variableNames=["dy"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,i=t.strideWidth,a=t.dilationHeight,o=t.dilationWidth,s=t.effectiveFilterHeight,u=t.effectiveFilterWidth,c=s-1-t.padInfo.top,l=u-1-t.padInfo.left,f=1/(e*n);this.userCode="\n const ivec2 pads = ivec2("+c+", "+l+");\n const float avgMultiplier = float("+f+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+s+";\n wR += "+a+") {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+u+";\n wC+= "+o+") {\n float dyC = float(dyCCorner + wC) / "+i+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t],e.dtype)},e.prototype.cast=function(t,e){return Ca(t,e,this)},e.prototype.unstack=function(t,e){for(var n=t.shape[e],r=new Array(t.rank-1),i=0,a=0;a= "+t.outDepth+".0 || fract(dyD) > 0.0) {\n continue;\n }\n int idyD = int(dyD);\n\n for (int wR = 0; wR < "+f+";\n wR += "+u+") {\n float dyR = float(dyRCorner + wR) / "+a+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 ||\n fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+d+";\n wC += "+c+") {\n float dyC = float(dyCCorner + wC) / "+o+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(batch, idyD, idyR, idyC, ch);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t],e.dtype)},e.prototype.maxPool3d=function(t,e){var n=new Jo(e,"max",!1);return this.compileAndRun(n,[t],"float32")},e.prototype.maxPool3dBackprop=function(t,e,n,r){var i=new Jo(r,"max",!0),a=this.compileAndRun(i,[e]),o=new function(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideDepth,n=t.strideHeight,r=t.strideWidth,i=t.dilationDepth,a=t.dilationHeight,o=t.dilationWidth,s=t.effectiveFilterDepth,u=t.effectiveFilterHeight,c=t.effectiveFilterWidth,l=s-1-t.padInfo.front,f=u-1-t.padInfo.top,d=c-1-t.padInfo.left,h=s*u*c-1;this.userCode="\n const ivec3 pads = ivec3("+l+", "+f+", "+d+");\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int ch = coords.u;\n\n ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;\n int dyDCorner = dyCorner.x;\n int dyRCorner = dyCorner.y;\n int dyCCorner = dyCorner.z;\n\n // Convolve dy(?, ?, ?, ch) with pos mask(:, :, :, d) to get\n // dx(xD, xR, xC, ch).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n\n for (int wD = 0; wD < "+s+";\n wD += "+i+") {\n float dyD = float(dyDCorner + wD) / "+e+".0;\n\n if (dyD < 0.0 || dyD >= "+t.outDepth+".0 || fract(dyD) > 0.0) {\n continue;\n }\n int idyD = int(dyD);\n\n for (int wR = 0; wR < "+u+";\n wR += "+a+") {\n float dyR = float(dyRCorner + wR) / "+n+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 ||\n fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+c+";\n wC += "+o+") {\n float dyC = float(dyCCorner + wC) / "+r+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(batch, idyD, idyR, idyC, ch);\n int maxPosValue = "+h+" -\n int(getMaxPos(batch, idyD, idyR, idyC, ch));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue =\n wD * "+u+" * "+c+" +\n wR * "+c+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(r),s=this.compileAndRun(o,[t,a],e.dtype);return a.dispose(),s},e.prototype.reshape=function(t,e){var n=this.texData.get(t.dataId);if(n.isPacked&&!Pe(t.shape,e)&&(null===n.texture||!Pe(n.shape,e))){var r=this.packedReshape(t,e);return Wt.makeTensorFromDataId(r.dataId,r.shape,r.dtype)}return Aa(t,e)},e.prototype.resizeBilinear=function(t,e,n,r){var i=f().getBool("WEBGL_PACK_IMAGE_OPERATIONS")?new function(t,e,n,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];var i=t[0],a=t[1],o=t[2],s=t[3];this.outputShape=[i,e,n,s];var u=[r&&e>1?a-1:a,r&&n>1?o-1:o],c=[r&&e>1?e-1:e,r&&n>1?n-1:n];this.userCode="\n const vec3 effectiveInputOverOutputRatioRC = vec3(\n "+u[0]/c[0]+",\n "+u[1]/c[1]+",\n "+u[1]/c[1]+");\n const vec3 inputShapeRC = vec3("+a+".0, "+o+".0,\n "+o+".0);\n\n float getAValue(int b, int r, int c, int d) {\n return getChannel(getA(b, r, c, d), vec2(c, d));\n }\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n // Calculate values for next column in yRC.z.\n ivec3 yRC = coords.yzz + ivec3(0, 0, 1);\n\n // Fractional source index.\n vec3 sourceFracIndexRC = vec3(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the four integer indices.\n ivec3 sourceFloorRC = ivec3(sourceFracIndexRC);\n ivec3 sourceCeilRC = ivec3(\n min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));\n\n // Should we calculate next column and row elements in 2x2 packed cell.\n bool hasNextCol = d < "+(s-1)+";\n bool hasNextRow = coords.z < "+(n-1)+";\n\n // In parallel, construct four corners for all four components in\n // packed 2x2 cell.\n vec4 topLeft = vec4(\n getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d),\n hasNextCol ? getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d + 1) : 0.0);\n\n vec4 bottomLeft = vec4(\n getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d),\n hasNextCol ? getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d + 1) : 0.0);\n\n vec4 topRight = vec4(\n getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d),\n hasNextCol ? getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d + 1) : 0.0);\n\n vec4 bottomRight = vec4(\n getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d),\n hasNextCol ? getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d + 1) : 0.0);\n\n vec3 fracRC = sourceFracIndexRC - vec3(sourceFloorRC);\n\n vec4 top = mix(topLeft, topRight, fracRC.yyzz);\n vec4 bottom = mix(bottomLeft, bottomRight, fracRC.yyzz);\n vec4 newValue = mix(top, bottom, fracRC.x);\n\n setOutput(newValue);\n }\n "}(t.shape,e,n,r):new function(t,e,n,r){this.variableNames=["A"],this.outputShape=[];var i=t[0],a=t[1],o=t[2],s=t[3];this.outputShape=[i,e,n,s];var u=[r&&e>1?a-1:a,r&&n>1?o-1:o],c=[r&&e>1?e-1:e,r&&n>1?n-1:n];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/c[0]+",\n "+u[1]/c[1]+");\n const vec2 inputShapeRC = vec2("+a+".0, "+o+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the four integer indices.\n ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);\n ivec2 sourceCeilRC = ivec2(\n min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));\n\n float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);\n float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);\n float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);\n float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);\n\n vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);\n\n float top = topLeft + (topRight - topLeft) * fracRC.y;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;\n float newValue = top + (bottom - top) * fracRC.x;\n\n setOutput(newValue);\n }\n "}(t.shape,e,n,r);return this.compileAndRun(i,[t],"float32")},e.prototype.resizeBilinearBackprop=function(t,e,n){var r=new function(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,i=r[1],a=r[2],o=t.shape,s=o[1],u=o[2],c=[n&&s>1?i-1:i,n&&u>1?a-1:a],l=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=c[0]/l[0],d=c[1]/l[1],h=1/f,p=1/d,m=2*Math.ceil(h)+2,g=2*Math.ceil(p)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+d+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+p+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(startRLerp - float(winHeight / 2));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(startCLerp - float(winWidth / 2));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), "+(i-1)+".0));\n float dxRLerp = dxR - float(topDxRIndex);\n float inverseDxRLerp = 1.0 - dxRLerp;\n\n float dxC = float(dyC) * widthScale;\n int leftDxCIndex = int(floor(dxC));\n int rightDxCIndex = int(min(ceil(dxC), "+(a-1)+".0));\n float dxCLerp = dxC - float(leftDxCIndex);\n float inverseDxCLerp = 1.0 - dxCLerp;\n\n if (r == topDxRIndex && c == leftDxCIndex) {\n // topLeft\n accumulator +=\n getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;\n }\n\n if (r == topDxRIndex && c == rightDxCIndex) {\n // topRight\n accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;\n }\n\n if (r == bottomDxRIndex && c == leftDxCIndex) {\n // bottomLeft\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;\n }\n\n if (r == bottomDxRIndex && c == rightDxCIndex) {\n // bottomRight\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(t,e,n);return this.compileAndRun(r,[t])},e.prototype.resizeNearestNeighbor=function(t,e,n,r){var i=new function(t,e,n,r){this.variableNames=["A"],this.outputShape=[];var i=t[0],a=t[1],o=t[2],s=t[3];this.outputShape=[i,e,n,s];var u=[r&&e>1?a-1:a,r&&n>1?o-1:o],c=[r&&e>1?e-1:e,r&&n>1?n-1:n],l=r?"0.5":"0.0";this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/c[0]+",\n "+u[1]/c[1]+");\n const vec2 inputShapeRC = vec2("+a+".0, "+o+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestRC = ivec2(\n min(inputShapeRC - 1.0, floor(sourceFracIndexRC + "+l+")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n "}(t.shape,e,n,r);return this.compileAndRun(i,[t])},e.prototype.resizeNearestNeighborBackprop=function(t,e,n){var r=new function(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,i=r[1],a=r[2],o=t.shape,s=o[1],u=o[2],c=[n&&s>1?i-1:i,n&&u>1?a-1:a],l=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=c[0]/l[0],d=c[1]/l[1],h=1/f,p=1/d,m=2*Math.ceil(h)+2,g=2*Math.ceil(p)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+d+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+p+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(floor(startRLerp - float(winHeight / 2)));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(floor(startCLerp - float(winWidth / 2)));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float sourceFracRow =\n float("+c[0]+") *\n (float(dyR) / float("+l[0]+"));\n\n float sourceFracCol =\n float("+c[1]+") *\n (float(dyC) / float("+l[1]+"));\n\n int sourceNearestRow = int(min(\n float(int("+i+") - 1),\n "+n+" ? float(round(sourceFracRow)) :\n float(floor(sourceFracRow))));\n\n int sourceNearestCol = int(min(\n float(int("+a+") - 1),\n "+n+" ? float(round(sourceFracCol)) :\n float(floor(sourceFracCol))));\n\n if (r == sourceNearestRow && c == sourceNearestCol) {\n accumulator += getDy(b, dyR, dyC, d);\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(t,e,n);return this.compileAndRun(r,[t])},e.prototype.multinomial=function(t,e,n,r){var i=e?t:ca(t),a=i.shape[0],o=i.shape[1],s=new Xo(a,o,n),u=s.getCustomSetupFunc(r);return this.compileAndRun(s,[i],"int32",u)},e.prototype.oneHot=function(t,e,n,r){var i=new function(t,e,n,r){this.variableNames=["indices"],this.outputShape=[t,e],this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int index = round(getIndices(coords.x));\n setOutput(mix(float("+r+"), float("+n+"),\n float(index == coords.y)));\n }\n "}(t.size,e,n,r);return this.compileAndRun(i,[t])},e.prototype.diag=function(t){var e=new function(t){this.variableNames=["X"],this.outputShape=[t,t],this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n float val = coords[0] == coords[1] ? getX(coords[0]) : 0.0;\n setOutput(val);\n }\n "}(t.size);return this.compileAndRun(e,[t])},e.prototype.nonMaxSuppression=function(t,e,n,r,i){return vn("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead"),Pa(t.dataSync(),e.dataSync(),n,r,i)},e.prototype.cropAndResize=function(t,e,n,r,i,a){var o=new function(t,e,n,r,i){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];var a=t[0],o=t[1],s=t[2],u=t[3],c=e[0],l=n[0],f=n[1];this.outputShape=[c,l,f,u];var d="bilinear"===r?1:0,h=[o-1+".0",s-1+".0"],p=h[0],m=h[1],g=l>1?[""+(o-1)/(l-1),"(y2-y1) * height_ratio","y1*"+p+" + float(y)*(height_scale)"]:["0.0","0.0","0.5 * (y1+y2) * "+p],v=g[0],y=g[1],b=g[2],x=f>1?[""+(s-1)/(f-1),"(x2-x1) * width_ratio","x1*"+m+" + float(x)*(width_scale)"]:["0.0","0.0","0.5 * (x1+x2) * "+m],w=x[0],k=x[1],_=x[2];this.userCode="\n const float height_ratio = float("+v+");\n const float width_ratio = float("+w+");\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int y = coords[1];\n int x = coords[2];\n int d = coords[3];\n\n // get box vals\n float y1 = getBoxes(b,0);\n float x1 = getBoxes(b,1);\n float y2 = getBoxes(b,2);\n float x2 = getBoxes(b,3);\n\n // get image in batch index\n int bInd = round(getBoxInd(b));\n if(bInd < 0 || bInd >= "+a+") {\n return;\n }\n\n float height_scale = "+y+";\n float width_scale = "+k+";\n\n float in_y = "+b+";\n if( in_y < 0.0 || in_y > "+p+" ) {\n setOutput(float("+i+"));\n return;\n }\n float in_x = "+_+";\n if( in_x < 0.0 || in_x > "+m+" ) {\n setOutput(float("+i+"));\n return;\n }\n\n vec2 sourceFracIndexCR = vec2(in_x,in_y);\n if("+d+" == 1) {\n // Compute the four integer indices.\n ivec2 sourceFloorCR = ivec2(sourceFracIndexCR);\n ivec2 sourceCeilCR = ivec2(ceil(sourceFracIndexCR));\n\n float topLeft = getImage(b, sourceFloorCR.y, sourceFloorCR.x, d);\n float bottomLeft = getImage(b, sourceCeilCR.y, sourceFloorCR.x, d);\n float topRight = getImage(b, sourceFloorCR.y, sourceCeilCR.x, d);\n float bottomRight = getImage(b, sourceCeilCR.y, sourceCeilCR.x, d);\n\n vec2 fracCR = sourceFracIndexCR - vec2(sourceFloorCR);\n\n float top = topLeft + (topRight - topLeft) * fracCR.x;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;\n float newValue = top + (bottom - top) * fracCR.y;\n setOutput(newValue);\n } else {\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestCR = ivec2(floor(\n sourceFracIndexCR + vec2(0.5,0.5)));\n float newValue = getImage(b, sourceNearestCR.y, sourceNearestCR.x, d);\n setOutput(newValue);\n }\n }\n "}(t.shape,e.shape,r,i,a);return this.compileAndRun(o,[t,e,n],"float32")},e.prototype.depthToSpace=function(t,e,n){C(e>1,function(){return"blockSize should be > 1 for depthToSpace, but was: "+e});var r=t.shape[0],i="NHWC"===n?t.shape[1]:t.shape[2],a="NHWC"===n?t.shape[2]:t.shape[3],o="NHWC"===n?t.shape[3]:t.shape[1],s=i*e,u=a*e,c=o/(e*e),l=new Oo("NHWC"===n?[r,s,u,c]:[r,c,s,u],e,n);return this.compileAndRun(l,[t])},e.prototype.split=function(t,e,n){return Va(t,e,n)},e.prototype.scatterND=function(t,e,n){var r=Gi(0,t,n),i=r.sliceRank,a=r.numUpdates,o=r.sliceSize,s=r.strides,u=r.outputSize,c=[u/o,o],l=t.reshape([a,i]),f=e.reshape([a,o]);if(0===u)return Aa(jn([]),n);var d=Ln(0),h=new Zo(a,i,l.rank,f.rank,s,c);return this.compileAndRun(h,[f,l,d]).reshape(n)},e.prototype.sparseToDense=function(t,e,n,r){var i=Gi(0,t,n),a=i.sliceRank,o=i.numUpdates,s=i.strides,u=i.outputSize,c=new Zo(o,a,t.rank,e.rank,s,[u,1],!1);return this.compileAndRun(c,[e,t,r]).reshape(n)},e.prototype.fft=function(t){return this.fftImpl(t,!1)},e.prototype.ifft=function(t){return this.fftImpl(t,!0)},e.prototype.fftImpl=function(t,e){var n=this.texData.get(t.dataId),r=new So("return real * expR - imag * expI;",t.shape,e),i=new So("return real * expI + imag * expR;",t.shape,e),a=[this.makeComplexComponentTensorInfo(t,n.complexTensors.real),this.makeComplexComponentTensorInfo(t,n.complexTensors.imag)],o=this.compileAndRun(r,a),s=this.compileAndRun(i,a),u=this.complex(o,s).as2D(t.shape[0],t.shape[1]);return o.dispose(),s.dispose(),u},e.prototype.gatherND=function(t,e){var n=e.shape,r=n[n.length-1],i=Bi(t,e),a=i[0],o=i[1],s=i[2],u=i[3],c=e.reshape([o,r]),l=t.reshape([t.size/s,s]),f=new function(t,e,n){this.sliceDim=t,this.strides=e,this.variableNames=["x","indices"],this.outputShape=n;var r=oo(e.length),i=oo(n.length),a=this.sliceDim>1?"strides[j]":"strides";this.userCode="\n "+r+" strides = "+r+"("+this.strides+");\n void main() {\n "+i+" coords = getOutputCoords();\n int flattenIndex = 0;\n for (int j = 0; j < "+this.sliceDim+"; j++) {\n int index = round(getIndices(coords[0], j));\n flattenIndex += index * "+a+";\n }\n setOutput(getX(flattenIndex, coords[1]));\n }\n "}(r,u,[o,s]);return this.compileAndRun(f,[l,c]).reshape(a)},e.prototype.fill=function(t,e,n){if("string"===(n=n||Z(e))){var r=W(n,I(t));return r.fill(e),Wt.makeTensor(r,t,n,this)}var i=new Co(t,e),a=i.getCustomSetupFunc(e);return this.compileAndRun(i,[],n,a)},e.prototype.onesLike=function(t){if("string"===t.dtype)throw new Error("onesLike is not supported under string dtype");return this.fill(t.shape,1,t.dtype)},e.prototype.zerosLike=function(t){return this.fill(t.shape,"string"===t.dtype?"":0,t.dtype)},e.prototype.linspace=function(t,e,n){return Na(t,e,n)},e.prototype.makeTensorInfo=function(t,e){var n=this.write(null,t,e);return this.texData.get(n).usage=null,{dataId:n,shape:t,dtype:e}},e.prototype.makeOutput=function(t,e){var n=this.makeTensorInfo(t,e).dataId;return Wt.makeTensorFromDataId(n,t,e,this)},e.prototype.unpackTensor=function(t){var e=new function(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=t;var e=t.length,n=Ka("rc",e),r=oo(e),i=function(t,e){if(1===t)return"rc";for(var n="",r=0;r "+e[0];for(var r="",i=t-2;i= "+e[i],i= "+e+";\n bool rEdge = rp1 >= "+n+";\n "}(e,t[t.length-1],t[t.length-2],n),o=function(t,e){var n=t.length,r=function(t,e){for(var n=[],r=0;r<=1;r++)for(var i=0;i<=1;i++){for(var a=(0===r?"r":"rp1")+", "+(0===i?"c":"cp1"),o=2;o= "+t[0]+" ? 0. : getA(rc + 1),\n 0, 0":"getA("+r[0]+"),\n cEdge ? 0. : getA("+r[1]+"),\n rEdge ? 0. : getA("+r[2]+"),\n rEdge || cEdge ? 0. : getA("+r[3]+")"}(t,n);this.userCode="\n void main() {\n "+r+" rc = getOutputCoords();\n\n if("+i+") {\n setOutput(vec4(0));\n } else {\n "+a+"\n\n setOutput(vec4("+o+"));\n }\n }\n "}}(t.shape);return this.runWebGLProgram(e,[t],t.dtype,null,!0)},e.prototype.packedReshape=function(t,e){var n=[De(t.shape)].concat(Me(t.shape)),r={dtype:t.dtype,shape:n,dataId:t.dataId},i=new function(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t;for(var n="",r=0;r<4;r++){var i="thisRC = rc;";r%2==1&&(i+="thisRC.z += 1;"),r>1&&(i+="thisRC.y += 1;"),n+="\n "+i+"\n "+(r>0?"if(thisRC.y < rows && thisRC.z < cols){":"")+"\n int flatIndex = getFlatIndex(thisRC);\n\n ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);\n vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));\n\n result["+r+"] =\n getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);\n "+(r>0?"}":"")+"\n "}this.userCode="\n \n ivec3 inputCoordsFromReshapedOutCoords(int index) {\n "+Ya(["r","c","d"],e)+"\n return ivec3(r, c, d);\n }\n \n "+Ja(t)+"\n\n void main() {\n ivec3 rc = getOutputCoords();\n\n vec4 result = vec4(0.);\n\n ivec3 thisRC;\n int rows = "+t[1]+";\n int cols = "+t[2]+";\n\n "+n+"\n\n setOutput(result);\n }\n "}([De(e)].concat(Me(e)),n),a=this.runWebGLProgram(i,[r],t.dtype,null,!0);return{dataId:a.dataId,shape:e,dtype:a.dtype}},e.prototype.decode=function(t){var e,n=this.texData.get(t),r=n.isPacked,i=n.shape,a=n.dtype,o=Re(i);return e=r?new function(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=Ht.DENSE;var e=Qt(t),n=Xa();this.outputShape=t,this.userCode="\n ivec3 outCoordsFromFlatIndex(int index) {\n "+Ya(["r","c","d"],t)+"\n return ivec3(r, c, d);\n }\n\n void main() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n int index = 4 * (resTexRC.x * "+e[1]+" + resTexRC.y);\n\n vec4 result = vec4(0.);\n\n for (int i=0; i<4; i++) {\n int flatIndex = index + i;\n ivec3 rc = outCoordsFromFlatIndex(flatIndex);\n result[i] = getChannel(getA(rc.x, rc.y, rc.z), vec2(rc.y, rc.z));\n }\n\n "+n.output+" = result;\n }\n "}(o):new function(t){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outPackingScheme=Ht.DENSE;var e=Qt(t),n=Xa();this.outputShape=t,this.userCode="\n ivec3 outCoordsFromFlatIndex(int index) {\n "+Ya(["r","c","d"],t)+"\n return ivec3(r, c, d);\n }\n\n void main() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+e[0]+", "+e[1]+"));\n int index = 4 * (resTexRC.x * "+e[1]+" + resTexRC.y);\n\n vec4 result = vec4(0.);\n\n for (int i=0; i<4; i++) {\n int flatIndex = index + i;\n ivec3 rc = outCoordsFromFlatIndex(flatIndex);\n result[i] = getA(rc.x, rc.y, rc.z);\n }\n\n "+n.output+" = result;\n }\n "}(o),{dtype:a,shape:i,dataId:this.runWebGLProgram(e,[{shape:o,dtype:a,dataId:t}],a,null,!0).dataId}},e.prototype.runWebGLProgram=function(t,e,n,r,i){var a=this;void 0===i&&(i=!1);var o=this.makeTensorInfo(t.outputShape,n),s=this.texData.get(o.dataId);if(t.packedOutput&&(s.isPacked=!0),t.outPackingScheme===Ht.DENSE){var u=Qt(t.outputShape);s.texShape=u.map(function(t){return 2*t})}if(null!=t.outTexUsage&&(s.usage=t.outTexUsage),0===I(o.shape))return s.values=U(o.dtype,0),o;var c=[],l=e.map(function(e){if("complex64"===e.dtype)throw new Error("GPGPUProgram does not support complex64 input. For complex64 dtypes, please separate the program into real and imaginary parts.");var n=a.texData.get(e.dataId);if(null==n.texture){if(!t.packedInputs&&I(e.shape)<=f().getNumber("WEBGL_SIZE_UPLOAD_UNIFORM"))return{shape:e.shape,texData:null,isUniform:!0,uniformValues:n.values};t.packedInputs&&(n.isPacked=!0,n.shape=e.shape)}else if(!!n.isPacked!=!!t.packedInputs)e=n.isPacked?a.unpackTensor(e):a.packTensor(e),c.push(e),n=a.texData.get(e.dataId);else if(n.isPacked&&!Pe(n.shape,e.shape)){var r=e,i=e.shape;e.shape=n.shape,e=a.packedReshape(e,i),c.push(e),n=a.texData.get(e.dataId),r.shape=i}return a.uploadToGPU(e.dataId),{shape:e.shape,texData:n,isUniform:!1}});this.uploadToGPU(o.dataId);var d,h={shape:o.shape,texData:s,isUniform:!1},p=function(t,e,n){var r="";l.concat(n).forEach(function(t){var e=null!=t.texData&&null!=t.texData.slice&&t.texData.slice.flatOffset>0,n=t.isUniform?"uniform":t.texData.texShape;r+=t.shape+"_"+n+"_"+e});var i=t.userCode;return t.constructor.name+"_"+r+"_"+i}(t,0,h),m=this.getAndSaveBinary(p,function(){return function(t,e,n,r){var i=e.userCode,a=n.map(function(t,n){var r={logicalShape:t.shape,texShape:t.isUniform?null:t.texData.texShape,isUniform:t.isUniform,isPacked:!t.isUniform&&t.texData.isPacked,flatOffset:null};return null!=t.texData&&null!=t.texData.slice&&t.texData.slice.flatOffset>0&&(r.flatOffset=t.texData.slice.flatOffset),{name:e.variableNames[n],shapeInfo:r}}),o=a.map(function(t){return t.shapeInfo}),s={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1,isPacked:r.texData.isPacked,flatOffset:null},u=Qa(a,s,i,e.packedInputs),c=t.createProgram(u),l=null,d=t.getUniformLocation(c,"NAN",!1);1===f().getNumber("WEBGL_VERSION")&&(l=t.getUniformLocation(c,"INFINITY",!1));for(var h={},p=0;p0)return 32}return 16})),this.floatPrecisionValue},e.prototype.epsilon=function(){return 32===this.floatPrecision()?1e-7:1e-4},e.prototype.uploadToGPU=function(t){var e,n=this.texData.get(t),r=n.shape,i=n.dtype,a=n.values,o=n.texture,s=n.usage,u=n.isPacked;if(null==o){var c,l=null!=this.activeTimers;l&&(c=ot());var f=n.texShape;if(null==f&&(f=Fe(r,u),n.texShape=f),null!=a){var d=Re(r),h=void 0,p=f[1],m=f[0],g=a instanceof Uint8Array;u?(p=(e=te(f[0],f[1]))[0],h=new function(t,e,n){void 0===n&&(n=!1),this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;var r=Xa(),i=e[0],a=e[1];this.outputShape=t;var o="",s="result";n&&(s="floor(result * 255. + 0.5)");for(var u=0;u<=1;u++)for(var c=0;c<=1;c++){var l=2*u+c;o+="\n localCoords = coords;\n if(localCoords[2] + "+c+" < "+t[2]+") {\n localCoords[2] += "+c+";\n if(localCoords[1] + "+u+" < "+t[1]+") {\n localCoords[1] += "+u+";\n\n flatIndex = getFlatIndex(localCoords);\n offset = imod(flatIndex, 4);\n\n flatIndex = idiv(flatIndex, 4, 1.);\n\n r = flatIndex / "+a+";\n c = imod(flatIndex, "+a+");\n uv = (vec2(c, r) + halfCR) / vec2("+a+".0, "+i+".0);\n values = "+r.texture2D+"(A, uv);\n\n if(offset == 0) {\n result["+l+"] = values[0];\n } else if(offset == 1) {\n result["+l+"] = values[1];\n } else if(offset == 2) {\n result["+l+"] = values[2];\n } else {\n result["+l+"] = values[3];\n }\n }\n }\n "}this.userCode="\n "+Ja(t)+"\n\n void main() {\n ivec3 coords = getOutputCoords();\n\n vec4 result = vec4(0.);\n int flatIndex, r, c, offset;\n ivec3 localCoords;\n vec2 uv;\n vec4 values;\n\n "+o+"\n\n "+r.output+" = "+s+";\n }\n "}(d,[m=e[1],p],g)):h=new function(t,e,n){void 0===n&&(n=!1),this.variableNames=["A"];var r=Xa(),i=e[0],a=e[1];this.outputShape=t;var o="result";n&&(o="floor(result * 255. + 0.5)"),this.userCode="\n "+Ja(t)+"\n\n void main() {\n ivec3 coords = getOutputCoords();\n\n int flatIndex = getFlatIndex(coords);\n int offset = imod(flatIndex, 4);\n\n flatIndex = idiv(flatIndex, 4, 1.);\n \n int r = flatIndex / "+a+";\n int c = imod(flatIndex, "+a+");\n vec2 uv = (vec2(c, r) + halfCR) / vec2("+a+".0, "+i+".0);\n vec4 values = "+r.texture2D+"(A, uv);\n\n float result;\n\n if(offset == 0) {\n result = values[0];\n } else if(offset == 1) {\n result = values[1];\n } else if(offset == 2) {\n result = values[2];\n } else {\n result = values[3];\n }\n\n "+r.output+" = vec4("+o+", 0., 0., 0.);\n }\n "}(d,[m,p],g);var v=this.makeTensorInfo([m,p],i);this.texData.get(v.dataId).usage=g?Gt.PIXELS:Gt.UPLOAD,this.gpgpu.uploadDenseMatrixToTexture(this.getTexture(v.dataId),p,m,a);var y=this.runWebGLProgram(h,[v],i,null,!0),b=this.texData.get(y.dataId);n.texture=b.texture,n.texShape=b.texShape,n.isPacked=b.isPacked,n.usage=b.usage,this.disposeData(v.dataId),this.texData.delete(y.dataId),n.values=null,l&&(this.uploadWaitMs+=ot()-c)}else{var x=this.acquireTexture(f,s,i,u);n.texture=x}}},e.prototype.convertAndCacheOnCPU=function(t,e){var n=this.texData.get(t),r=n.dtype;return this.releaseGPUData(t),null!=e&&(n.values=function(t,e){if("float32"===e||"complex64"===e)return t;if("int32"===e||"bool"===e){for(var n="int32"===e?new Int32Array(t.length):new Uint8Array(t.length),r=0;r1024*this.numMBBeforeWarning*1024){var i=(this.numBytesInGPU/1024/1024).toFixed(2);this.warnedAboutMemory=!0,console.warn("High memory usage in GPU: "+i+" MB, most likely due to a memory leak")}return this.textureManager.acquireTexture(t,e,r)},e.prototype.computeBytes=function(t,e){return t[0]*t[1]*$(e)},e}(ha);function Ns(t,e){return t(e={exports:{}},e.exports),e.exports}Vt()&&Wt.registerBackend("webgl",function(){return new As},2),"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:void 0!==t||"undefined"!=typeof self&&self;var Ts=Ns(function(t){!function(t,e,n){function r(t,e){return e.c=t.c,e.s0=t.s0,e.s1=t.s1,e.s2=t.s2,e}function i(t,e){var n=new function(t){var e,n=this,r=(e=4022871197,function(t){t=t.toString();for(var n=0;n>>0,e=(r*=e)>>>0,e+=4294967296*(r-=e)}return 2.3283064365386963e-10*(e>>>0)});n.next=function(){var t=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=t-(n.c=0|t)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(t),n.s0<0&&(n.s0+=1),n.s1-=r(t),n.s1<0&&(n.s1+=1),n.s2-=r(t),n.s2<0&&(n.s2+=1),r=null}(t),i=e&&e.state,a=n.next;return a.int32=function(){return 4294967296*n.next()|0},a.double=function(){return a()+1.1102230246251565e-16*(2097152*a()|0)},a.quick=a,i&&("object"==typeof i&&r(i,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=i:this.alea=i}(0,t)}),Is=Ns(function(t){!function(t,e,n){function r(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e}function i(t,e){var n=new function(t){var e=this,n="";e.x=0,e.y=0,e.z=0,e.w=0,e.next=function(){var t=e.x^e.x<<11;return e.x=e.y,e.y=e.z,e.z=e.w,e.w^=e.w>>>19^t^t>>>8},t===(0|t)?e.x=t:n+=t;for(var r=0;r>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,i&&("object"==typeof i&&r(i,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=i:this.xor128=i}(0,t)}),Ds=Ns(function(t){!function(t,e,n){function r(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e.v=t.v,e.d=t.d,e}function i(t,e){var n=new function(t){var e=this,n="";e.next=function(){var t=e.x^e.x>>>2;return e.x=e.y,e.y=e.z,e.z=e.w,e.w=e.v,(e.d=e.d+362437|0)+(e.v=e.v^e.v<<4^t^t<<1)|0},e.x=0,e.y=0,e.z=0,e.w=0,e.v=0,t===(0|t)?e.x=t:n+=t;for(var r=0;r>>4),e.next()}(t),i=e&&e.state,a=function(){return(n.next()>>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,i&&("object"==typeof i&&r(i,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=i:this.xorwow=i}(0,t)}),Ms=Ns(function(t){!function(t,e,n){function r(t,e){return e.x=t.x.slice(),e.i=t.i,e}function i(t,e){null==t&&(t=+new Date);var n=new function(t){var e=this;e.next=function(){var t,n,r=e.x,i=e.i;return t=r[i],n=(t^=t>>>7)^t<<24,n^=(t=r[i+1&7])^t>>>10,n^=(t=r[i+3&7])^t>>>3,n^=(t=r[i+4&7])^t<<7,t=r[i+7&7],n^=(t^=t<<13)^t<<9,r[i]=n,e.i=i+1&7,n},function(t,e){var n,r=[];if(e===(0|e))r[0]=e;else for(e=""+e,n=0;n0;--n)t.next()}(e,t)}(t),i=e&&e.state,a=function(){return(n.next()>>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,i&&(i.x&&r(i,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=i:this.xorshift7=i}(0,t)}),Rs=Ns(function(t){!function(t,e,n){function r(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function i(t,e){null==t&&(t=+new Date);var n=new function(t){var e=this;e.next=function(){var t,n,r=e.w,i=e.X,a=e.i;return e.w=r=r+1640531527|0,n=i[a+34&127],t=i[a=a+1&127],n^=n<<13,t^=t<<17,n^=n>>>15,t^=t>>>12,n=i[a]=n^t,e.i=a,n+(r^r>>>16)|0},function(t,e){var n,r,i,a,o,s=[],u=128;for(e===(0|e)?(r=e,e=null):(e+="\0",r=0,u=Math.max(u,e.length)),i=0,a=-32;a>>15,r^=r<<4,r^=r>>>13,a>=0&&(o=o+1640531527|0,i=0==(n=s[127&a]^=r+o)?i+1:0);for(i>=128&&(s[127&(e&&e.length||0)]=-1),i=127,a=512;a>0;--a)r=s[i+34&127],n=s[i=i+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,s[i]=r^n;t.w=o,t.X=s,t.i=i}(e,t)}(t),i=e&&e.state,a=function(){return(n.next()>>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,i&&(i.X&&r(i,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=i:this.xor4096=i}(0,t)}),Fs=Ns(function(t){!function(t,e,n){function r(t,e){return e.a=t.a,e.b=t.b,e.c=t.c,e.d=t.d,e}function i(t,e){var n=new function(t){var e=this,n="";e.next=function(){var t=e.b,n=e.c,r=e.d,i=e.a;return t=t<<25^t>>>7^n,n=n-r|0,r=r<<24^r>>>8^i,i=i-t|0,e.b=t=t<<20^t>>>12^n,e.c=n=n-r|0,e.d=r<<16^n>>>16^i,e.a=i-t|0},e.a=0,e.b=0,e.c=-1640531527,e.d=1367130551,t===Math.floor(t)?(e.a=t/4294967296|0,e.b=0|t):n+=t;for(var r=0;r>>0)/4294967296};return a.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},a.int32=n.next,a.quick=a,i&&("object"==typeof i&&r(i,n),a.state=function(){return r(n,{})}),a}e&&e.exports?e.exports=i:this.tychei=i}(0,t)}),js=Ns(function(t){!function(e,r){var i,a=this,o=256,s=6,u="random",c=r.pow(o,s),l=r.pow(2,52),f=2*l,d=o-1;function h(t,n,d){var h=[],y=g(function t(e,n){var r,i=[],a=typeof e;if(n&&"object"==a)for(r in e)try{i.push(t(e[r],n-1))}catch(t){}return i.length?i:"string"==a?e:e+"\0"}((n=1==n?{entropy:!0}:n||{}).entropy?[t,v(e)]:null==t?function(){try{var t;return i&&(t=i.randomBytes)?t=t(o):(t=new Uint8Array(o),(a.crypto||a.msCrypto).getRandomValues(t)),v(t)}catch(t){var n=a.navigator,r=n&&n.plugins;return[+new Date,a,r,a.screen,v(e)]}}():t,3),h),b=new p(h),x=function(){for(var t=b.g(s),e=c,n=0;t=f;)t/=2,e/=2,n>>>=1;return(t+n)/e};return x.int32=function(){return 0|b.g(4)},x.quick=function(){return b.g(4)/4294967296},x.double=x,g(v(b.S),e),(n.pass||d||function(t,e,n,i){return i&&(i.S&&m(i,b),t.state=function(){return m(b,{})}),n?(r[u]=t,e):t})(x,y,"global"in n?n.global:this==r,n.state)}function p(t){var e,n=t.length,r=this,i=0,a=r.i=r.j=0,s=r.S=[];for(n||(t=[n++]);i=1,function(){return"Must pass at least one tensor to tf.addN(), but got "+t.length});var e=t.map(function(t,e){return xn(t,"tensors"+e,"addN")}),n=e[0];e.forEach(function(t){if(t.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),e.forEach(function(t){if(!D(t.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});var r=e;return Wt.runKernelFunc(function(t,n){return t.addN(e)},r,null,"AddN")}});function zs(){Ze("tf.batchNormalization() is going away. Use tf.batchNorm() instead, and note the positional argument change of scale, offset, and varianceEpsilon")}function Bs(t){return 0===t.rank||1===t.rank?t.as4D(1,1,1,t.size):2===t.rank?t.as4D(1,1,t.shape[0],t.shape[1]):3===t.rank?t.as4D(1,t.shape[0],t.shape[1],t.shape[2]):t}function Us(t,e,n,r,i,a){null==a&&(a=.001);var o,s,u=xn(t,"x","batchNorm"),c=xn(e,"mean","batchNorm"),l=xn(n,"variance","batchNorm");null!=i&&(o=xn(i,"scale","batchNorm")),null!=r&&(s=xn(r,"offset","batchNorm")),C(c.rank===l.rank,function(){return"Batch normalization gradient requires mean and variance to have equal ranks."}),C(null==s||c.rank===s.rank,function(){return"Batch normalization gradient requires mean and offset to have equal ranks."}),C(null==o||c.rank===o.rank,function(){return"Batch normalization gradient requires mean and scale to have equal ranks."});var f={x:u,scale:o,offset:s,mean:c,variance:l},d={varianceEpsilon:a};return Wt.runKernelFunc(function(t,e){var n=Bs(u),r=t.batchNormalization(n,Ws(c),Ws(l),a,Ws(o),Ws(s));return e([u,c,l,o]),r},f,null,"FusedBatchNorm",d).reshape(u.shape)}function Ws(t){return null==t?null:0===t.rank?t.as1D():1===t.rank?t:2===t.rank?t.as4D(1,1,t.shape[0],t.shape[1]):3===t.rank?t.as4D(1,t.shape[0],t.shape[1],t.shape[2]):t}var Vs=Dn({batchNormalization_:function(t,e,n,r,i,a){return void 0===r&&(r=.001),zs(),Us(t,e,n,a,i,r)}}),qs=Dn({batchNorm_:Us});function Hs(t,e,n,r,i,a){var o,s,u=xn(t,"x","batchNorm"),c=xn(e,"mean","batchNorm"),l=xn(n,"variance","batchNorm");return null!=i&&(o=xn(i,"scale","batchNorm")),null!=r&&(s=xn(r,"offset","batchNorm")),C(2===u.rank,function(){return"Error in batchNorm3D: x must be rank 3 but got rank "+u.rank+"."}),C(2===c.rank||1===c.rank,function(){return"Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank "+c.rank+"."}),C(2===l.rank||1===l.rank,function(){return"Error in batchNorm2D: variance must be rank 2 or rank 1 but got rank "+l.rank+"."}),null!=o&&C(2===o.rank||1===o.rank,function(){return"Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank "+o.rank+"."}),null!=s&&C(2===s.rank||1===s.rank,function(){return"Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank "+s.rank+"."}),qs(u,c,l,s,o,a)}var Gs=Dn({batchNormalization2d_:function(t,e,n,r,i,a){return void 0===r&&(r=.001),zs(),Hs(t,e,n,a,i,r)}}),$s=Dn({batchNorm2d_:Hs});function Ks(t,e,n,r,i,a){var o,s,u=xn(t,"x","batchNorm"),c=xn(e,"mean","batchNorm"),l=xn(n,"variance","batchNorm");return null!=i&&(o=xn(i,"scale","batchNorm")),null!=r&&(s=xn(r,"offset","batchNorm")),C(3===u.rank,function(){return"Error in batchNorm3D: x must be rank 3 but got rank "+u.rank+"."}),C(3===c.rank||1===c.rank,function(){return"Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank "+c.rank+"."}),C(3===l.rank||1===l.rank,function(){return"Error in batchNorm3D: variance must be rank 3 or rank 1 but got rank "+l.rank+"."}),null!=o&&C(3===o.rank||1===o.rank,function(){return"Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank "+o.rank+"."}),null!=s&&C(3===s.rank||1===s.rank,function(){return"Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank "+s.rank+"."}),qs(u,c,l,s,o,a)}var Xs=Dn({batchNormalization3d_:function(t,e,n,r,i,a){return void 0===r&&(r=.001),zs(),Ks(t,e,n,a,i,r)}}),Ys=Dn({batchNorm3d_:Ks});function Js(t,e,n,r,i,a){var o,s,u=xn(t,"x","batchNorm"),c=xn(e,"mean","batchNorm"),l=xn(n,"variance","batchNorm");return null!=i&&(o=xn(i,"scale","batchNorm")),null!=r&&(s=xn(r,"offset","batchNorm")),C(4===u.rank,function(){return"Error in batchNorm4D: x must be rank 4 but got rank "+u.rank+"."}),C(4===c.rank||1===c.rank,function(){return"Error in batchNorm4D: mean must be rank 4 or rank 1 but got rank "+c.rank+"."}),C(4===l.rank||1===l.rank,function(){return"Error in batchNorm4D: variance must be rank 4 or rank 1 but got rank "+l.rank+"."}),null!=o&&C(4===o.rank||1===o.rank,function(){return"Error in batchNorm4D: scale must be rank 4 or rank 1 but got rank "+o.rank+"."}),null!=s&&C(4===s.rank||1===s.rank,function(){return"Error in batchNorm4D: offset must be rank 4 or rank 1 but got rank "+s.rank+"."}),qs(u,c,l,s,o,a)}var Zs=Dn({batchNormalization4d_:function(t,e,n,r,i,a){return void 0===r&&(r=.001),zs(),Js(t,e,n,a,i,r)}}),Qs=Dn({batchNorm4d_:Js}),tu=Dn({broadcastTo_:function(t,e){var n=xn(t,"broadcastTo","x"),r=n.shape;if(e.some(function(t){return!(t>0)||t%1!=0}))throw new Error("broadcastTo(): Invalid broadcast shape ["+e+"].");if(e.lengthn.rank){for(var i=n.shape.slice();i.length=0;s--)if(a[s]===e[s])o[s]=1;else if(1!==n.shape[s])throw new Error("broadcastTo(): ["+r+"] cannot be broadcast to ["+e+"].");var u=o.map(function(t,e){return t>1?e:-1}).filter(function(t){return t>=0});if(0===u.length)return n.clone();var c={x:n},l={shape:e,inputShape:a};return Wt.runKernelFunc(function(t){return t.tile(n,o)},c,function(t){return{x:function(){return t.sum(u,!0)}}},Ir,l)}}),eu=Dn({clone_:function(t){var e=xn(t,"x","clone",null);return Wt.runKernelFunc(function(){return Wt.makeTensorFromDataId(e.dataId,e.shape,e.dtype)},{x:e},null,Mr)}}),nu=Dn({logicalAnd_:function(t,e){var n=xn(t,"a","logicalAnd","bool"),r=xn(e,"b","logicalAnd","bool");return Ur(n.shape,r.shape),Wt.runKernelFunc(function(t){return t.logicalAnd(n,r)},{a:n,b:r},null,"LogicalAnd")}}),ru=Dn({logicalNot_:function(t){var e=xn(t,"x","logicalNot","bool");return Wt.runKernelFunc(function(t){return t.logicalNot(e)},{$x:e})}}),iu=Dn({logicalOr_:function(t,e){var n=xn(t,"a","logicalOr","bool"),r=xn(e,"b","logicalOr","bool");return Ur(n.shape,r.shape),Wt.runKernelFunc(function(t){return t.logicalOr(n,r)},{$a:n,$b:r})}}),au=Dn({logicalXor_:function(t,e){var n=xn(t,"a","logicalXor","bool"),r=xn(e,"b","logicalXor","bool");return Ur(n.shape,r.shape),iu(t,e).logicalAnd(nu(t,e).logicalNot())}}),ou=Dn({where_:function(t,e,n){var r=xn(e,"a","where"),i=xn(n,"b","where"),a=xn(t,"condition","where","bool");return A(r.shape,i.shape,"Error in where: "),1===a.rank?C(a.shape[0]===r.shape[0],function(){return"The first dimension of `a` must match the size of `condition`."}):A(a.shape,i.shape,"Error in where: "),Wt.runKernelFunc(function(t,e){var n=t.select(a,r,i);return e([a]),n},{$condition:a,$a:r,$b:i},function(t,e){var n=e[0];return{$condition:function(){return Zn(n).toFloat()},$a:function(){return t.mul(n.cast(t.dtype))},$b:function(){return t.mul(n.logicalNot().cast(t.dtype))}}})}}),su=function(t){return u(this,void 0,void 0,function(){var e,n,r;return c(this,function(i){switch(i.label){case 0:return[4,(e=xn(t,"condition","whereAsync","bool")).data()];case 1:return n=i.sent(),r=Ga(e.shape,n),t!==e&&e.dispose(),[2,r]}})})},uu=Dn({divNoNan_:function(t,e){var n,r=xn(t,"a","div"),i=xn(e,"b","div");r=(n=Ft(r,i))[0],i=n[1];var a=zi(r,i),o=Zn(a),s=i.equal(o);return ou(s,o,a)}}),cu=Dn({tile_:function(t,e){var n=xn(t,"x","tile",null);C(n.rank===e.length,function(){return"Error in transpose: rank of input "+n.rank+" must match length of reps "+e+"."});var r=[n],i={x:n},a={reps:e};return Wt.runKernelFunc(function(t,r){var i=t.tile(n,e);return r([n]),i},i,null,Rr,a,r)}}),lu=Dn({eye_:function(t,e,n,r){void 0===r&&(r="float32"),null==e&&(e=t);for(var i=ar([t,e],r),a=t<=e?t:e,o=0;o2)throw new Error("Rank of probabilities must be 1 or 2, but is "+o);n=n||Math.random();var s=1===o?i.as2D(1,-1):i,u=Wt.runKernelFunc(function(t){return t.multinomial(s,r,e,n)},{logits2D:s});return 1===o?u.as1D():u}}),du=Dn({oneHot_:function(t,e,n,r){if(void 0===n&&(n=1),void 0===r&&(r=0),e<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+e);var i=xn(t,"indices","oneHot","int32"),a=i.shape.concat([e]),o={indices:i=i.flatten()},s={depth:e,onValue:n,offValue:r};return Wt.runKernelFunc(function(t,a){return a([i]),t.oneHot(i,e,n,r)},o,null,Dr,s).reshape(a)}}),hu=Dn({pad_:function(t,e,n){void 0===n&&(n=0);var r=xn(t,"x","pad");if(0===r.rank)throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");var i={paddings:e,constantValue:n},a={x:r};return Wt.runKernelFunc(function(t,i){return i([r]),t.pad(r,e,n)},a,null,Fr,i)}}),pu=Dn({pad1d_:function(t,e,n){return void 0===n&&(n=0),C(2===e.length,function(){return"Invalid number of paddings. Must be length of 2."}),hu(t,[e],n)}}),mu=Dn({pad2d_:function(t,e,n){return void 0===n&&(n=0),C(2===e.length&&2===e[0].length&&2===e[1].length,function(){return"Invalid number of paddings. Must be length of 2 each."}),hu(t,e,n)}}),gu=Dn({pad3d_:function(t,e,n){return void 0===n&&(n=0),C(3===e.length&&2===e[0].length&&2===e[1].length&&2===e[2].length,function(){return"Invalid number of paddings. Must be length of 2 each."}),hu(t,e,n)}}),vu=Dn({pad4d_:function(t,e,n){return void 0===n&&(n=0),C(4===e.length&&2===e[0].length&&2===e[1].length&&2===e[2].length&&2===e[3].length,function(){return"Invalid number of paddings. Must be length of 2 each."}),hu(t,e,n)}}),yu=Dn({rand_:function(t,e,n){var r=I(t),i=null;if(null==n||"float32"===n)i=new Float32Array(r);else if("int32"===n)i=new Int32Array(r);else{if("bool"!==n)throw new Error("Unknown data type "+n);i=new Uint8Array(r)}for(var a=0;an)}var Eu=Object.freeze({TEST_EPSILON_FLOAT16:xu,expectArraysClose:function(t,e,n){return null==n&&(n=wu()),ku(t,e,function(t,e){return _u(t,e,n)})},testEpsilon:wu,expectPromiseToFail:function(t,e){t().then(function(){return e.fail()},function(){return e()})},expectArraysEqual:function(t,e){var n="string"==typeof e||"number"==typeof e||"boolean"==typeof e?[e]:e;return X(t)||X(t[0])||X(e)||X(e[0])?ku(t,n,function(t,e){return t==e}):ku(t,e,function(t,e){return _u(t,e,0)})},expectNumbersClose:function(t,e,n){if(null==n&&(n=wu()),!_u(t,e,n))throw new Error("Numbers differ: actual === "+t+", expected === "+e)},expectValuesInRange:function(t,e,n){for(var r=0;rn)throw new Error("Value out of range:"+t[r]+" low: "+e+", high: "+n)},expectArrayBuffersEqual:function(t,e){expect(new Float32Array(t)).toEqual(new Float32Array(e))}}),Ou=function(){function t(t,e,n,r,i){this.mean=t,this.stdDev=e,this.dtype=n,this.nextVal=NaN,this.truncated=r,this.truncated&&(this.upper=this.mean+2*this.stdDev,this.lower=this.mean-2*this.stdDev);var a=i||Math.random();this.random=Ps(a.toString())}return t.prototype.nextValue=function(){if(!isNaN(this.nextVal)){var t=this.nextVal;return this.nextVal=NaN,t}for(var e,n,r=!1;!r;){var i=void 0,a=void 0,o=void 0;do{o=(i=2*this.random()-1)*i+(a=2*this.random()-1)*a}while(o>=1||0===o);var s=Math.sqrt(-2*Math.log(o)/o);e=this.mean+this.stdDev*i*s,n=this.mean+this.stdDev*a*s,this.truncated&&!this.isValidTruncated(e)||(r=!0)}return this.truncated&&!this.isValidTruncated(n)||(this.nextVal=this.convertValue(n)),this.convertValue(e)},t.prototype.convertValue=function(t){return null==this.dtype||"float32"===this.dtype?t:Math.round(t)},t.prototype.isValidTruncated=function(t){return t<=this.upper&&t>=this.lower},t}(),Su=function(){function t(t,e,n,r){this.alpha=t,this.beta=1/e,this.dtype=n;var i=r||Math.random();this.randu=Ps(i.toString()),this.randn=new Ou(0,1,n,!1,this.randu()),this.d=t<1?t+2/3:t-1/3,this.c=1/Math.sqrt(9*this.d)}return t.prototype.nextValue=function(){for(var t,e,n,r,i,a;;){do{r=this.randn.nextValue(),a=1+this.c*r}while(a<=0);if(a*=a*a,e=1-.331*(t=r*r)*t,n=.5*t+this.d*(1-a+Math.log(a)),(i=this.randu())0,function(){return"mask cannot be scalar"}),A(s.slice(a,a+o),i.shape,"mask's shape must match the first K dimensions of tensor's shape,"),u=1,l=a;l=2&&o.rank>=2&&a.rank===o.rank,function(){return"Error in matMul: inputs must have the same rank of at least 2, got ranks "+a.rank+" and "+o.rank+"."}),C(D(f,d),function(){return"Error in matMul: outer dimensions ("+f+") and ("+d+") of Tensors with shapes "+a.shape+" and "+o.shape+" must match."}),C(s===u,function(){return"Error in matMul: inner shapes ("+s+") and ("+u+") of Tensors with shapes "+a.shape+" and "+o.shape+" and transposeA="+n+" and transposeB="+r+" must match."});var m=a.shape.slice(0,-2).concat([c,l]),g=n?a.as3D(h,s,c):a.as3D(h,c,s),v=r?o.as3D(p,l,u):o.as3D(p,u,l),y={transposeA:n,transposeB:r};return Wt.runKernelFunc(function(t,e){var i=t.batchMatMul(g,v,n,r);return e([g,v]),i},{a:g,b:v},function(t,e){var i=e,a=i[0],o=i[1];return n||r?!n&&r?{a:function(){return t.matMul(o,!1,!1)},b:function(){return t.matMul(a,!0,!1)}}:n&&!r?{a:function(){return o.matMul(t,!1,!0)},b:function(){return a.matMul(t,!1,!1)}}:{a:function(){return o.matMul(t,!0,!0)},b:function(){return t.matMul(a,!0,!0)}}:{a:function(){return t.matMul(o,!1,!0)},b:function(){return a.matMul(t,!0,!1)}}},"BatchMatMul",y).reshape(m)}}),dc=Dn({dot_:function(t,e){var n=xn(t,"t1","dot"),r=xn(e,"t2","dot");C(!(1!==n.rank&&2!==n.rank||1!==r.rank&&2!==r.rank),function(){return"Error in dot: inputs must all be rank 1 or 2, but got ranks "+n.rank+" and "+r.rank+"."});var i=1===n.rank?n.size:n.shape[1],a=1===r.rank?r.size:r.shape[0];return C(i===a,function(){return"Error in dot: inner dimensions of inputs must match, but got "+i+" and "+a+"."}),1===n.rank&&1===r.rank?n.as2D(1,-1).matMul(r.as2D(-1,1)).asScalar():1===n.rank&&2===r.rank?n.as2D(1,-1).matMul(r.as2D(r.shape[0],r.shape[1])).as1D():2===n.rank&&1===r.rank?n.matMul(r.as2D(-1,1)).as1D():n.matMul(r.as2D(r.shape[0],r.shape[1]))}}),hc=Dn({outerProduct_:function(t,e){var n=xn(t,"v1","outerProduct"),r=xn(e,"v2","outerProduct");return C(1===n.rank&&1===r.rank,function(){return"Error in outerProduct: inputs must be rank 1, but got ranks "+n.rank+" and "+r.rank+"."}),n.as2D(-1,1).matMul(r.as2D(1,-1))}}),pc=Dn({reverse_:function(t,e){var n=xn(t,"x","reverse");if(0===n.rank)return n.clone();var r=z(e,n.shape);return Wt.runKernelFunc(function(t){return t.reverse(n,r)},{$x:n},function(t){return{$x:function(){return t.reverse(r)}}}).reshapeAs(n)}}),mc=Dn({reverse1d_:function(t){var e=xn(t,"x","reverse");return C(1===e.rank,function(){return"Error in reverse1D: x must be rank 1 but got rank "+e.rank+"."}),pc(e,0)}}),gc=Dn({reverse2d_:function(t,e){var n=xn(t,"x","reverse");return C(2===n.rank,function(){return"Error in reverse2D: x must be rank 2 but got rank "+n.rank+"."}),pc(n,e)}}),vc=Dn({reverse3d_:function(t,e){var n=xn(t,"x","reverse");return C(3===n.rank,function(){return"Error in reverse3D: x must be rank 3 but got rank "+n.rank+"."}),pc(n,e)}}),yc=Dn({reverse4d_:function(t,e){var n=xn(t,"x","reverse");return C(4===n.rank,function(){return"Error in reverse4D: x must be rank 4 but got rank "+n.rank+"."}),pc(n,e)}});function bc(t,e,n,r,i,a){var o=xn(t,"x","maxPool"),s=o,u=!1;3===o.rank&&(u=!0,s=o.as4D(1,o.shape[0],o.shape[1],o.shape[2])),null==r&&(r=[1,1]),C(4===s.rank,function(){return"Error in maxPool: input must be rank 4 but got rank "+s.rank+"."}),C(Oa(n,r),function(){return"Error in maxPool: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+r+"'"}),null!=a&&C(M(i),function(){return"Error in maxPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+i+"."});var c=ma(s.shape,e,n,r,i,a);if(1===c.filterWidth&&1===c.filterHeight&&D(c.inShape,c.outShape))return o.clone();var l=[s],f=Wt.runKernelFunc(function(t,e){var n=t.maxPool(s,c);return e([s,n]),n},{x:s},function(t,a){var o=a[0],s=a[1];return{x:function(){return function(t,e,n,r,i,a,o,s){var u=xn(t,"dy","maxPoolBackprop"),c=xn(e,"input","maxPoolBackprop"),l=xn(n,"output","maxPoolBackprop");C(c.rank===u.rank,function(){return"Rank of input ("+c.rank+") does not match rank of dy ("+u.rank+")"}),null==a&&(a=[1,1]),C(Oa(i,a),function(){return"Error in maxPoolBackProp: Either strides or dilations must be 1. Got strides "+i+" and dilations '"+a+"'"}),C(4===u.rank,function(){return"Error in maxPoolBackprop: dy must be rank 4 but got rank "+u.rank+"."}),C(4===c.rank,function(){return"Error in maxPoolBackprop: input must be rank 4 but got rank "+c.rank+"."});var f=ma(c.shape,r,i,a,o,s);return Wt.runKernelFunc(function(t){return t.maxPoolBackprop(u,c,l,f)},{$dy:u,$input:c})}(t,o,s,e,n,r,i)}}},"MaxPool",c,l);return u?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f}function xc(t,e,n,r,i,a){var o=xn(t,"x","avgPool","float32");null==r&&(r=[1,1]),C(Oa(n,r),function(){return"Error in avgPool: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+r+"'"});var s=o,u=!1;3===o.rank&&(u=!0,s=o.as4D(1,o.shape[0],o.shape[1],o.shape[2])),C(4===s.rank,function(){return"Error in avgPool: x must be rank 4 but got rank "+s.rank+"."}),null!=a&&C(M(i),function(){return"Error in avgPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+i+"."});var c=ma(s.shape,e,n,r,i,a);if(1===c.filterWidth&&1===c.filterHeight&&D(c.inShape,c.outShape))return o.clone();var l=Wt.runKernelFunc(function(t){return t.avgPool(s,c)},{x:s},function(t){return{x:function(){return function(t,e,n,r,i,a){var o=xn(t,"dy","avgPoolBackprop"),s=xn(e,"input","avgPoolBackprop");C(s.rank===o.rank,function(){return"Rank of input ("+s.rank+") does not match rank of dy ("+o.rank+")"}),null==i&&(i=[1,1]),C(Oa(r,i),function(){return"Error in avgPoolBackprop: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+i+"'"});var u=s,c=o,l=!1;3===s.rank&&(l=!0,u=s.as4D(1,s.shape[0],s.shape[1],s.shape[2]),c=o.as4D(1,o.shape[0],o.shape[1],o.shape[2])),C(4===c.rank,function(){return"Error in avgPoolBackprop: dy must be rank 4 but got rank "+c.rank+"."}),C(4===u.rank,function(){return"Error in avgPoolBackprop: input must be rank 4 but got rank "+u.rank+"."});var f=ma(u.shape,n,r,i,a),d=Wt.runKernelFunc(function(t){return t.avgPoolBackprop(c,u,f)},{dy4D:c,input4D:u});return l?d.as3D(d.shape[1],d.shape[2],d.shape[3]):d}(t,s,e,n,r,i)}}},"AvgPool",c);return l=l.cast(o.dtype),u?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}var wc=Dn({maxPool_:function(t,e,n,r,i){return bc(t,e,n,1,r,i)}}),kc=Dn({avgPool_:function(t,e,n,r,i){return xc(t,e,n,1,r,i)}}),_c=Dn({pool_:function(t,e,n,r,i,a){null==i&&(i=[1,1]),null==a&&(a=1),0===r&&(r="valid");var o=xn(t,"x","maxPool"),s=o,u=!1;3===o.rank&&(u=!0,s=o.as4D(1,o.shape[0],o.shape[1],o.shape[2])),C(Oa(a,i),function(){return"Error in pool: Either strides or dilations must be 1. Got strides "+a+" and dilations '"+i+"'"});var c,l=ma(s.shape,e,a,i,r),f=[l.dilationHeight,l.dilationWidth];c="same"===r?function(t,e){var n=t.map(function(t,n){return t+(t-1)*(e[n]-1)}).map(function(t){return t-1}),r=n.map(function(t){return Math.floor(t/2)}),i=n.map(function(t,e){return t-r[e]});return n.map(function(t,e){return[r[e],i[e]]})}([l.filterHeight,l.filterWidth],f):[[0,0],[0,0]];var d=1===f[0]&&1===f[1],h=function(t,e,n){var r=n.map(function(t){return t[0]}),i=n.map(function(t){return t[1]}),a=t.concat(r,i),o=e.map(function(t,e){return(t-a[e]%t)%t}),s=i.map(function(t,e){return t+o[e]});return[e.map(function(t,e){return[r[e],s[e]]}),e.map(function(t,e){return[0,o[e]]})]}([l.inHeight,l.inWidth],f,c),p=h[0],m=h[1],g=d?r:"valid",v=d?s:hr(s,f,p),y=("avg"===n?function(){return xc(v,e,a,1,g)}:function(){return bc(v,e,a,1,g)})(),b=d?y:sr(y,f,m);return u?b.as3D(b.shape[1],b.shape[2],b.shape[3]):b}}),Ec=Dn({maxPool3d_:function(t,e,n,r,i,a,o){void 0===a&&(a="NDHWC");var s=xn(t,"x","maxPool3d"),u=s,c=!1;4===s.rank&&(c=!0,u=s.as5D(1,s.shape[0],s.shape[1],s.shape[2],s.shape[3])),null==o&&(o=[1,1,1]),C(5===u.rank,function(){return"Error in maxPool3d: x must be rank 5 but got rank "+u.rank+"."}),C("NDHWC"===a,function(){return"Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of "+a}),C(Oa(n,o),function(){return"Error in maxPool3d: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+o+"'"}),null!=i&&C(M(r),function(){return"Error in maxPool3d: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+r+"."});var l=ga(u.shape,e,n,o,r,i,a),f=Wt.runKernelFunc(function(t,e){var n=t.maxPool3d(u,l);return e([u,n]),n},{x:u},function(t,a){var s=a[0],u=a[1];return{x:function(){return function(t,e,n,r,i,a,o,s){var u=xn(t,"dy","maxPool3dBackprop"),c=xn(e,"input","maxPool3dBackprop"),l=xn(n,"output","maxPool3dBackprop"),f=u,d=c,h=l,p=!1;4===c.rank&&(p=!0,f=u.as5D(1,u.shape[0],u.shape[1],u.shape[2],u.shape[3]),d=c.as5D(1,c.shape[0],c.shape[1],c.shape[2],c.shape[3]),h=l.as5D(1,l.shape[0],l.shape[1],l.shape[2],l.shape[3])),C(5===f.rank,function(){return"Error in maxPool3dBackprop: dy must be rank 5 but got rank "+f.rank+"."}),C(5===d.rank,function(){return"Error in maxPool3dBackprop: input must be rank 5 but got rank "+d.rank+"."}),C(5===h.rank,function(){return"Error in maxPool3dBackprop: output must be rank 5 but got rank "+h.rank+"."}),null==a&&(a=[1,1,1]),C(Oa(i,a),function(){return"Error in maxPool3dBackprop: Either strides or dilations must be 1. Got strides "+i+" and dilations '"+a+"'"}),null!=s&&C(M(o),function(){return"Error in maxPool3dBackprop: pad must be an integer when using, dimRoundingMode "+s+" but got pad "+o+"."});var m=ga(d.shape,r,i,a,o,s),g=Wt.runKernelFunc(function(t){return t.maxPool3dBackprop(f,d,h,m)},{dy5D:f,input5D:d});return p?g.as4D(g.shape[1],g.shape[2],g.shape[3],g.shape[4]):g}(t,s,u,e,n,o,r,i)}}});return c?f.as4D(f.shape[1],f.shape[2],f.shape[3],f.shape[4]):f}}),Oc=Dn({avgPool3d_:function(t,e,n,r,i,a,o){void 0===a&&(a="NDHWC");var s=xn(t,"x","avgPool3d","float32"),u=s,c=!1;4===s.rank&&(c=!0,u=s.as5D(1,s.shape[0],s.shape[1],s.shape[2],s.shape[3])),null==o&&(o=[1,1,1]),C(5===u.rank,function(){return"Error in avgPool3d: x must be rank 5 but got rank "+u.rank+"."}),C("NDHWC"===a,function(){return"Error in avgPool3d: Only NDHWC is currently supported, but got dataFormat of "+a}),C(Oa(n,o),function(){return"Error in avgPool3d: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+o+"'"}),null!=i&&C(M(r),function(){return"Error in avgPool3d: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+r+"."});var l=ga(u.shape,e,n,o,r,i,a),f=Wt.runKernelFunc(function(t){return t.avgPool3d(u,l)},{x:u},function(t){return{x:function(){return function(t,e,n,r,i,a,o){var s=xn(t,"dy","avgPool3dBackprop"),u=xn(e,"input","avgPool3dBackprop"),c=s,l=u,f=!1;4===u.rank&&(f=!0,c=s.as5D(1,s.shape[0],s.shape[1],s.shape[2],s.shape[3]),l=u.as5D(1,u.shape[0],u.shape[1],u.shape[2],u.shape[3])),C(5===c.rank,function(){return"Error in avgPool3dBackprop: dy must be rank 5 but got rank "+c.rank+"."}),C(5===l.rank,function(){return"Error in avgPool3dBackprop: input must be rank 5 but got rank "+l.rank+"."}),null==i&&(i=[1,1,1]),C(Oa(r,i),function(){return"Error in avgPool3dBackprop: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+i+"'"}),null!=o&&C(M(a),function(){return"Error in maxPool3dBackprop: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+a+"."});var d=ga(l.shape,n,r,i,a,o),h=Wt.runKernelFunc(function(t){return t.avgPool3dBackprop(c,l,d)},{dy5D:c,input5D:l});return f?h.as4D(h.shape[1],h.shape[2],h.shape[3],h.shape[4]):h}(t,u,e,n,o,r,i)}}});return f=f.cast(u.dtype),c?f.as4D(f.shape[1],f.shape[2],f.shape[3],f.shape[4]):f}}),Sc=Dn({maxPoolWithArgmax_:function(t,e,n,r,i){void 0===i&&(i=!1);var a=xn(t,"x","maxPoolWithArgmax"),o={filterSize:e,strides:n,pad:r,includeBatchInIndex:i},s=Wt.runKernel("MaxPoolWithArgmax",{x:a},o);return{result:s[0],indexes:s[1]}}}),Cc=Dn({slice_:function(t,e,n){var r,i,a=xn(t,"x","slice");if(0===a.rank)throw new Error("Slicing scalar is not possible");(r="number"==typeof e?[e].concat(new Array(a.rank-1).fill(0)):e.length=0?t:(C(-1===t,function(){return"Negative size values should be exactly -1 but got "+t+" for the slice() size at index "+e+"."}),a.shape[e]-r[e])}),Ki(a,r,i);var o=a.shape,s={begin:r,size:i};return Wt.runKernelFunc(function(t){return t.slice(a,r,i)},{x:a},function(t){for(var e=[],n=0;n0&&(e=e.sum(a)),e.reshape(r.shape)}}},"Prelu")}}),$c=Dn({relu_:function(t){var e=xn(t,"x","relu");return"bool"===e.dtype?e.toInt():Wt.runKernelFunc(function(t,n){var r=t.relu(e);return n([e]),r},{x:e},function(t,e){var n=e[0];return{x:function(){return t.mulStrict(n.step().toFloat())}}},"Relu")}}),Kc=Dn({relu6_:function(t){var e=xn(t,"x","relu6");return"bool"===e.dtype?e.toInt():Wt.runKernelFunc(function(t,n){var r=t.relu6(e);return n([e]),r},{x:e},function(t,e){var n=e[0],r=n.lessEqual(6).mul(n.step());return{x:function(){return t.mulStrict(r.toFloat())}}},"Relu6")}}),Xc=Dn({selu_:function(t){var e=xn(t,"x","selu");return Wt.runKernelFunc(function(t,n){var r=t.selu(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){var e=n.greater(Ln(0)),r=Ln(as),i=Ln(os),a=t.mul(i),o=t.mul(r).mul(n.toFloat().exp());return ou(e,a,o)}}})}}),Yc=Dn({localResponseNormalization_:function(t,e,n,r,i){void 0===e&&(e=5),void 0===n&&(n=1),void 0===r&&(r=1),void 0===i&&(i=.5);var a=xn(t,"x","localResponseNormalization");C(4===a.rank||3===a.rank,function(){return"Error in localResponseNormalization: x must be rank 3 or 4 but got\n rank "+a.rank+"."}),C(M(e),function(){return"Error in localResponseNormalization: depthRadius must be an integer but got depthRadius "+e+"."});var o=a,s=!1;3===a.rank&&(s=!0,o=a.as4D(1,a.shape[0],a.shape[1],a.shape[2]));var u=Wt.runKernelFunc(function(t,a){var s=t.localResponseNormalization4D(o,e,n,r,i);return a([o,s]),s},{x4D:o},function(t,a){var o=a[0],s=a[1];return{x4D:function(){return Wt.runKernelFunc(function(a){return a.LRNGrad(t,o,s,e,n,r,i)},{})}}});return s?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Jc=Dn({norm_:function(t,e,n,r){void 0===e&&(e="euclidean"),void 0===n&&(n=null),void 0===r&&(r=!1);var i=function t(e,n,r){if(void 0===r&&(r=null),0===e.rank)return e.abs();if(1!==e.rank&&null===r)return t(e.reshape([-1]),n,r);if(1===e.rank||"number"==typeof r||Array.isArray(r)&&1===r.length){if(1===n)return e.abs().sum(r);if(n===1/0)return e.abs().max(r);if(n===-1/0)return e.abs().min(r);if("euclidean"===n||2===n)return e.abs().pow(Ln(2,"int32")).sum(r).sqrt();throw new Error("Error in norm: invalid ord value: "+n)}if(Array.isArray(r)&&2===r.length){if(1===n)return e.abs().sum(r[0]).max(r[1]-1);if(n===1/0)return e.abs().sum(r[1]).max(r[0]);if(n===-1/0)return e.abs().sum(r[1]).min(r[0]);if("fro"===n||"euclidean"===n)return e.square().sum(r).sqrt();throw new Error("Error in norm: invalid ord value: "+n)}throw new Error("Error in norm: invalid axis: "+r)}(t=xn(t,"x","norm"),e,n),a=i.shape;if(r){var o=z(n,t.shape);a=On(i.shape,o)}return i.reshape(a)}}),Zc=Dn({basicLSTMCell_:function(t,e,n,r,i,a){var o=xn(t,"forgetBias","basicLSTMCell"),s=xn(e,"lstmKernel","basicLSTMCell"),u=xn(n,"lstmBias","basicLSTMCell"),c=xn(r,"data","basicLSTMCell"),l=xn(i,"c","basicLSTMCell"),f=xn(a,"h","basicLSTMCell"),d=c.concat(f,1).matMul(s).add(u),h=d.shape[0],p=d.shape[1]/4,m=[h,p],g=d.slice([0,0],m),v=d.slice([0,p],m),y=d.slice([0,2*p],m),b=d.slice([0,3*p],m),x=g.sigmoid().mulStrict(v.tanh()).addStrict(l.mulStrict(o.add(y).sigmoid()));return[x,x.tanh().mulStrict(b.sigmoid())]}}),Qc=Dn({multiRNNCell_:function(t,e,n,r){for(var i=xn(e,"data","multiRNNCell"),a=wn(n,"c","multiRNNCell"),o=wn(r,"h","multiRNNCell"),s=i,u=[],c=0;ci)throw new Error("'k' passed to topk() must be <= the last dimension ("+i+") but got "+e);var a=Wt.runKernelFunc(function(t){return t.topk(r,e,n)},{$x:r});return{values:a[0],indices:a[1]}}}),rl=Dn({scatterND_:function(t,e,n){var r=xn(t,"indices","scatterND","int32"),i=xn(e,"updates","scatterND");return Hi(i,r,n),Wt.runKernelFunc(function(t){return t.scatterND(r,i,n)},{indices:r,updates:i},null,"ScatterNd",{shape:n})}}),il=Dn({fft_:function(t){C("complex64"===t.dtype,function(){return"The dtype for tf.spectral.fft() must be complex64 but got "+t.dtype+"."});var e=t.shape[t.shape.length-1],n=t.size/e,r=t.as2D(n,e);return Wt.runKernelFunc(function(t){return t.fft(r)},{input:t}).reshape(t.shape)}}),al=Dn({ifft_:function(t){C("complex64"===t.dtype,function(){return"The dtype for tf.spectral.ifft() must be complex64 but got "+t.dtype+"."});var e=t.shape[t.shape.length-1],n=t.size/e,r=t.as2D(n,e);return Wt.runKernelFunc(function(t){return t.ifft(r)},{input:t}).reshape(t.shape)}}),ol=Dn({rfft_:function(t,e){C("float32"===t.dtype,function(){return"The dtype for rfft() must be real value but got "+t.dtype});var n,r=t.shape[t.shape.length-1],i=t.size/r;if(null!=e&&er){var s=t.shape.map(function(t){return t});s[t.shape.length-1]=e-r,n=t.concat($n(s),t.shape.length-1),r=e}else n=t;var u=n.zerosLike(),c=Mn(n,u).as2D(i,r),l=il(c),f=Math.floor(r/2)+1,d=Rn(l),h=Fn(l),p=d.split([f,r-f],d.shape.length-1),m=h.split([f,r-f],h.shape.length-1),g=n.shape.slice();return g[n.shape.length-1]=f,Mn(p[0],m[0]).reshape(g)}}),sl=Dn({irfft_:function(t){var e=t.shape[t.shape.length-1],n=t.size/e;if(e<=2){var r=t.as2D(n,e),i=al(r);return Rn(i)}var a=[n,2*(e-1)],o=Rn(t).as2D(n,e),s=Fn(t).as2D(n,e),u=o.slice([0,1],[n,e-2]).reverse(1),c=s.slice([0,1],[n,e-2]).reverse(1).mul(Ln(-1)),l=o.concat(u,1),f=s.concat(c,1);return r=Mn(l,f).as2D(a[0],a[1]),i=al(r),Rn(i)}}),ul=Object.freeze({fft:il,ifft:al,rfft:ol,irfft:sl}),cl=Dn({sparseToDense_:function(t,e,n,r){void 0===r&&(r=0);var i=xn(t,"sparseIndices","sparseToDense","int32"),a=xn(e,"sparseValues","sparseToDense"),o=xn(r,"defaultValue","sparseToDense",a.dtype);return function(t,e,n,r){if("int32"!==t.dtype)throw new Error("tf.sparseToDense() expects the indices to be int32 type, but the dtype was "+t.dtype+".");if(t.rank>2)throw new Error("sparseIndices should be a scalar, vector, or matrix, but got shape "+t.shape+".");var i=t.rank>0?t.shape[0]:1,a=t.rank>1?t.shape[1]:1;if(n.length!==a)throw new Error("outputShape has incorrect number of elements:, "+n.length+", should be: "+a+".");var o=e.size;if(0!==e.rank&&(1!==e.rank||o!==i))throw new Error("sparseValues has incorrect shape "+e.shape+", should be [] or ["+i+"]");if(e.dtype!==r.dtype)throw new Error("sparseValues.dtype must match defaultValues.dtype")}(i,a,n,o),Wt.runKernelFunc(function(t){return t.sparseToDense(i,a,n,o)},{$sparseIndices:i,$sparseValues:a,$defaultValue:o})}}),ll=Dn({gatherND_:function(t,e){var n=xn(e,"indices","gatherND","int32"),r=xn(t,"x","gatherND");return Wt.runKernelFunc(function(t){return t.gatherND(r,n)},{x:r,indices:n},null,"GatherNd")}}),fl=Dn({diag_:function(t){var e=xn(t,"x","diag").flatten(),n=t.shape.concat(t.shape);return Wt.runKernelFunc(function(t){return t.diag(e)},{$x:e}).reshape(n)}}),dl=Dn({dropout_:function(t,e,n,r){var i=xn(t,"x","dropout");if(C("float32"===i.dtype,function(){return"x has to be a floating point tensor since it's going to be scaled, but got a "+i.dtype+" tensor instead."}),C(e>=0&&e<1,function(){return"rate must be a float in the range [0, 1), but got "+e+"."}),0===e)return t instanceof Ot?i.clone():i;var a=function(t,e){if(null==e)return t.shape.slice();if(D(t.shape,e))return e;if(t.shape.length===e.length){for(var n=[],r=0;r1,function(){return"inTopK() expects the predictions to be of rank 2 or higher, but got "+r.rank}),C(r.rank-1===i.rank,function(){return"predictions rank should be 1 larger than targets rank, but got predictions rank "+r.rank+" and targets rank "+i.rank}),A(r.shape.slice(0,r.shape.length-1),i.shape,"predictions's shape should be align with the targets' shape, except the last dimension."),a=r.shape[r.shape.length-1],C(n>0&&n<=a,function(){return"'k' passed to inTopK() must be > 0 && <= the predictions last dimension ("+a+"), but got "+n}),[4,r.data()];case 1:return o=c.sent(),[4,i.data()];case 2:for(s=c.sent(),u=[o.length/a,a],f=u[1],d=U("bool",l=u[0]),h=0;h1?s.div(Ln(o)):s}if(n===pl.SUM_BY_NONZERO_WEIGHTS){if(null==i)return a.sum().div(Ln(r.size));var u=i.mul(Gn(r.shape)).notEqual(Ln(0)).sum().toFloat();return a.sum().div(u)}throw Error("Unknown reduction: "+n)}}),_l=Dn({cosineDistance_:function(t,e,n,r,i){void 0===i&&(i=pl.SUM_BY_NONZERO_WEIGHTS);var a=xn(t,"labels","cosineDistance"),o=xn(e,"predictions","cosineDistance"),s=null;null!=r&&(s=xn(r,"weights","cosineDistance")),A(a.shape,o.shape,"Error in cosineDistance: ");var u=Ln(1).sub(a.mul(o).sum(n,!0));return kl(u,s,i)}}),El=Dn({hingeLoss_:function(t,e,n,r){void 0===r&&(r=pl.SUM_BY_NONZERO_WEIGHTS);var i=xn(t,"labels","hingeLoss"),a=xn(e,"predictions","hingeLoss"),o=null;null!=n&&(o=xn(n,"weights","hingeLoss")),A(i.shape,a.shape,"Error in hingeLoss: ");var s=Ln(1);i=Ln(2).mul(i).sub(s);var u=s.sub(i.mul(a)).relu();return kl(u,o,r)}}),Ol=Dn({huberLoss_:function(t,e,n,r,i){void 0===r&&(r=1),void 0===i&&(i=pl.SUM_BY_NONZERO_WEIGHTS);var a=xn(t,"labels","huberLoss"),o=xn(e,"predictions","huberLoss"),s=null;null!=n&&(s=xn(n,"weights","huberLoss")),A(a.shape,o.shape,"Error in huberLoss: ");var u=Ln(r),c=o.sub(a).abs(),l=Ai(c,u),f=c.sub(l),d=Ln(.5).mul(l.square()).add(u.mul(f));return kl(d,s,i)}}),Sl=Dn({logLoss_:function(t,e,n,r,i){void 0===r&&(r=1e-7),void 0===i&&(i=pl.SUM_BY_NONZERO_WEIGHTS);var a=xn(t,"labels","logLoss"),o=xn(e,"predictions","logLoss"),s=null;null!=n&&(s=xn(n,"weights","logLoss")),A(a.shape,o.shape,"Error in logLoss: ");var u=Ln(1),c=Ln(r),l=a.mul(o.add(c).log()).neg().sub(u.sub(a).mul(u.sub(o).add(c).log()));return kl(l,s,i)}}),Cl=Dn({meanSquaredError_:function(t,e,n,r){void 0===r&&(r=pl.SUM_BY_NONZERO_WEIGHTS);var i=xn(t,"labels","meanSquaredError"),a=xn(e,"predictions","meanSquaredError"),o=null;null!=n&&(o=xn(n,"weights","meanSquaredError")),A(i.shape,a.shape,"Error in meanSquaredError: ");var s=i.squaredDifference(a);return kl(s,o,r)}}),Al=Dn({sigmoidCrossEntropy_:function(t,e,n,r,i){void 0===r&&(r=0),void 0===i&&(i=pl.SUM_BY_NONZERO_WEIGHTS);var a=xn(t,"multiClassLabels","sigmoidCrossEntropy"),o=xn(e,"logits","sigmoidCrossEntropy"),s=null;if(null!=n&&(s=xn(n,"weights","sigmoidCrossEntropy")),A(a.shape,o.shape,"Error in sigmoidCrossEntropy: "),r>0){var u=Ln(r),c=Ln(1),l=Ln(.5);a=a.mul(c.sub(u)).add(l.mul(u))}var f=function(t,e){var n=xn(t,"labels","sigmoidCrossEntropyWithLogits"),r=xn(e,"logits","sigmoidCrossEntropyWithLogits");A(n.shape,r.shape,"Error in sigmoidCrossEntropyWithLogits: ");var i=r.relu(),a=r.mul(n),o=r.abs().neg().exp().log1p();return i.sub(a).add(o)}(a,o);return kl(f,s,i)}}),Nl=Dn({softmaxCrossEntropy_:function(t,e,n,r,i){void 0===r&&(r=0),void 0===i&&(i=pl.SUM_BY_NONZERO_WEIGHTS);var a=xn(t,"onehotLabels","softmaxCrossEntropy"),o=xn(e,"logits","softmaxCrossEntropy"),s=null;if(null!=n&&(s=xn(n,"weights","softmaxCrossEntropy")),A(a.shape,o.shape,"Error in softmaxCrossEntropy: "),r>0){var u=Ln(r),c=Ln(1),l=Ln(a.shape[1]);a=a.mul(c.sub(u)).add(u.div(l))}var f=function(t,e,n){if(void 0===n&&(n=-1),-1===n&&(n=e.rank-1),n!==e.rank-1)throw Error("Softmax cross entropy along a non-last dimension is not yet supported. Labels / logits was rank "+e.rank+" and dim was "+n);return sa(function(t,e,r){var i=e.logSumExp([n],!0),a=e.toFloat().sub(i);return r([t,a]),{value:a.mul(t).neg().sum([n]),gradFunc:function(t,e){var r=e[0],i=e[1],a=On(t.shape,[n]);return[t.reshape(a).mul(r.toFloat().sub(i.exp())),t.reshape(a).mul(i.exp().sub(r.toFloat()))]}}})(t,e)}(a,o);return kl(f,s,i)}}),Tl=Object.freeze({get Reduction(){return pl},absoluteDifference:wl,computeWeightedLoss:kl,cosineDistance:_l,hingeLoss:El,huberLoss:Ol,logLoss:Sl,meanSquaredError:Cl,sigmoidCrossEntropy:Al,softmaxCrossEntropy:Nl});function Il(t,e){return void 0===e&&(e=!1),Wt.tidy(function(){if(2!==t.shape.length)throw new Error("qr2d() requires a 2D Tensor, but got a "+t.shape.length+"D Tensor.");for(var n=t.shape[0],r=t.shape[1],i=lu(n),a=t.clone(),o=Bn([[1]],[1,1]),s=o.clone(),u=n>=r?r:n,c=function(t){var e,u=a,c=s,l=i;e=Wt.tidy(function(){var e=a.slice([t,t],[n-t,1]),u=e.norm(),c=a.slice([t,t],[1,1]),l=Bn([[-1]]).where(c.greater(0),Bn([[1]])),f=c.sub(l.mul(u)),d=e.div(f);s=1===d.shape[0]?o.clone():o.concat(d.slice([1,0],[d.shape[0]-1,d.shape[1]]),0);var h=l.matMul(f).div(u).neg(),p=a.slice([t,0],[n-t,r]),m=h.mul(s),g=s.transpose();if(0===t)a=p.sub(m.matMul(g.matMul(p)));else{var v=p.sub(m.matMul(g.matMul(p)));a=a.slice([0,0],[t,r]).concat(v,0)}var y=m.transpose(),b=i.slice([0,t],[n,i.shape[1]-t]);if(0===t)i=b.sub(b.matMul(s).matMul(y));else{var x=b.sub(b.matMul(s).matMul(y));i=i.slice([0,0],[n,t]).concat(x,1)}return[s,a,i]}),s=e[0],a=e[1],i=e[2],an([u,c,l])},l=0;lr&&(i=i.slice([0,0],[n,r]),a=a.slice([0,0],[r,r])),[i,a]})}var Dl=Dn({bandPart_:function(t,e,n){if(e%1!=0)throw new Error("bandPart(): numLower must be an integer, got "+e+".");if(n%1!=0)throw new Error("bandPart(): numUpper must be an integer, got "+n+".");var r=xn(t,"a","bandPart");if(r.rank<2)throw new Error("bandPart(): Rank must be at least 2, got "+r.rank+".");var i=r.shape,a=r.shape.slice(-2),o=a[0],s=a[1];if(!(e<=o))throw new Error("bandPart(): numLower ("+e+") must not be greater than the number of rows ("+o+").");if(!(n<=s))throw new Error("bandPart(): numUpper ("+n+") must not be greater than the number of columns ("+s+").");e<0&&(e=o),n<0&&(n=s);var u=Yn(0,o,1,"int32").reshape([-1,1]),c=Yn(0,s,1,"int32"),l=Pi(u,c),f=nu(l.lessEqual(Ln(+e,"int32")),l.greaterEqual(Ln(-n,"int32"))),d=$n([o,s],r.dtype);return mr(gr(r.reshape([-1,o,s])).map(function(t){return ou(f,t,d)})).reshape(i)}}),Ml=Dn({gramSchmidt_:function(t){var e;if(Array.isArray(t)){e=!1,C(null!=t&&t.length>0,function(){return"Gram-Schmidt process: input must not be null, undefined, or empty"});for(var n=t[0].shape[0],r=function(e){C(t[e].shape[0]===n,function(){return"Gram-Schmidt: Non-unique lengths found in the input vectors: ("+t[e].shape[0]+" vs. "+n+")"})},i=1;i0)for(var n=0;n= 2, but got rank "+t.rank);if(2===t.rank)return Il(t,e);var n=t.shape.slice(0,t.shape.length-2).reduce(function(t,e){return t*e}),r=[],i=[];return gr(t.reshape([n,t.shape[t.shape.length-2],t.shape[t.shape.length-1]]),0).forEach(function(t){var n=Il(t,e),a=n[0],o=n[1];r.push(a),i.push(o)}),[mr(r,0).reshape(t.shape),mr(i,0).reshape(t.shape)]}}),Fl=Object.freeze({bandPart:Dl,gramSchmidt:Ml,qr:Rl});function jl(t,e,n,r,i,a){null==r&&(r=.5),null==i&&(i=Number.NEGATIVE_INFINITY),null==a&&(a=0);var o=t.shape[0];return n=Math.min(n,o),C(0<=r&&r<=1,function(){return"iouThreshold must be in [0, 1], but was '"+r+"'"}),C(2===t.rank,function(){return"boxes must be a 2D tensor, but was of rank '"+t.rank+"'"}),C(4===t.shape[1],function(){return"boxes must have 4 columns, but 2nd dimension was "+t.shape[1]}),C(1===e.rank,function(){return"scores must be a 1D tensor"}),C(e.shape[0]===o,function(){return"scores has incompatible shape with boxes. Expected "+o+", but was "+e.shape[0]}),C(0<=a&&a<=1,function(){return"softNmsSigma must be in [0, 1], but was '"+a+"'"}),{maxOutputSize:n,iouThreshold:r,scoreThreshold:i,softNmsSigma:a}}var Pl=Dn({resizeBilinear_:function(t,e,n){void 0===n&&(n=!1);var r=xn(t,"images","resizeBilinear");C(3===r.rank||4===r.rank,function(){return"Error in resizeBilinear: x must be rank 3 or 4, but got rank "+r.rank+"."}),C(2===e.length,function(){return"Error in resizeBilinear: new shape must 2D, but got shape "+e+"."});var i=r,a=!1;3===r.rank&&(a=!0,i=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var o=e[0],s=e[1],u=Wt.runKernelFunc(function(t,e){return e([i]),t.resizeBilinear(i,o,s,n)},{x:i},function(t,e){return{x:function(){return Wt.runKernelFunc(function(r){return r.resizeBilinearBackprop(t,e[0],n)},{})}}},"ResizeBilinear",{alignCorners:n,newHeight:o,newWidth:s});return a?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Ll=Dn({resizeNearestNeighbor_:function(t,e,n){void 0===n&&(n=!1);var r=xn(t,"images","resizeNearestNeighbor");C(3===r.rank||4===r.rank,function(){return"Error in resizeNearestNeighbor: x must be rank 3 or 4, but got rank "+r.rank+"."}),C(2===e.length,function(){return"Error in resizeNearestNeighbor: new shape must 2D, but got shape "+e+"."}),C("float32"===r.dtype||"int32"===r.dtype,function(){return"`images` must have `int32` or `float32` as dtype"});var i=r,a=!1;3===r.rank&&(a=!0,i=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var o=e[0],s=e[1],u=Wt.runKernelFunc(function(t,e){return e([i]),t.resizeNearestNeighbor(i,o,s,n)},{batchImages:i},function(t,e){return{batchImages:function(){return Wt.runKernelFunc(function(r){return r.resizeNearestNeighborBackprop(t,e[0],n)},{})}}});return a?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),zl=Dn({nonMaxSuppression_:function(t,e,n,r,i){void 0===r&&(r=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY);var a=xn(t,"boxes","nonMaxSuppression"),o=xn(e,"scores","nonMaxSuppression"),s=jl(a,o,n,r,i);n=s.maxOutputSize,r=s.iouThreshold,i=s.scoreThreshold;var u={maxOutputSize:n,iouThreshold:r,scoreThreshold:i};return Wt.runKernelFunc(function(t){return t.nonMaxSuppression(a,o,n,r,i)},{boxes:a,scores:o},null,"NonMaxSuppressionV3",u)}}),Bl=Dn({nonMaxSuppressionWithScore_:function(t,e,n,r,i,a){void 0===r&&(r=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY),void 0===a&&(a=0);var o=xn(t,"boxes","nonMaxSuppression"),s=xn(e,"scores","nonMaxSuppression"),u=jl(o,s,n,r,i,a),c={maxOutputSize:n=u.maxOutputSize,iouThreshold:r=u.iouThreshold,scoreThreshold:i=u.scoreThreshold,softNmsSigma:a=u.softNmsSigma},l=Wt.runKernel("NonMaxSuppressionV5",{boxes:o,scores:s},c);return{selectedIndices:l[0],selectedScores:l[1]}}}),Ul=Dn({cropAndResize_:function(t,e,n,r,i,a){var o=xn(t,"image","cropAndResize"),s=xn(e,"boxes","cropAndResize","float32"),u=xn(n,"boxInd","cropAndResize","int32");i=i||"bilinear",a=a||0;var c=s.shape[0];return C(4===o.rank,function(){return"Error in cropAndResize: image must be rank 4,but got rank "+o.rank+"."}),C(2===s.rank&&4===s.shape[1],function(){return"Error in cropAndResize: boxes must be have size ["+c+",4] but had shape "+s.shape+"."}),C(1===u.rank&&u.shape[0]===c,function(){return"Error in cropAndResize: boxInd must be have size ["+c+"] but had shape "+s.shape+"."}),C(2===r.length,function(){return"Error in cropAndResize: cropSize must be of length 2, but got length "+r.length+"."}),C(r[0]>=1&&r[1]>=1,function(){return"cropSize must be atleast [1,1], but was "+r}),C("bilinear"===i||"nearest"===i,function(){return"method must be bilinear or nearest, but was "+i}),Wt.runKernelFunc(function(t,e){return t.cropAndResize(o,s,u,r,i,a)},{images:o,boxes:s,boxInd:u},null,"CropAndResize",{method:i,extrapolationValue:a,cropSize:r})}}),Wl=Object.freeze({resizeBilinear:Pl,resizeNearestNeighbor:Ll,nonMaxSuppression:zl,nonMaxSuppressionAsync:function(t,e,n,r,i){return void 0===r&&(r=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY),u(this,void 0,void 0,function(){var a,o,s,u,l,f,d;return c(this,function(c){switch(c.label){case 0:return a=xn(t,"boxes","nonMaxSuppressionAsync"),o=xn(e,"scores","nonMaxSuppressionAsync"),s=jl(a,o,n,r,i),n=s.maxOutputSize,r=s.iouThreshold,i=s.scoreThreshold,[4,Promise.all([a.data(),o.data()])];case 1:return u=c.sent(),l=u[0],f=u[1],d=Pa(l,f,n,r,i),a!==t&&a.dispose(),o!==e&&o.dispose(),[2,d]}})})},nonMaxSuppressionWithScore:Bl,nonMaxSuppressionWithScoreAsync:function(t,e,n,r,i,a){return void 0===r&&(r=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY),void 0===a&&(a=0),u(this,void 0,void 0,function(){var o,s,u,l,f,d,h;return c(this,function(c){switch(c.label){case 0:return o=xn(t,"boxes","nonMaxSuppressionAsync"),s=xn(e,"scores","nonMaxSuppressionAsync"),u=jl(o,s,n,r,i,a),n=u.maxOutputSize,r=u.iouThreshold,i=u.scoreThreshold,a=u.softNmsSigma,[4,Promise.all([o.data(),s.data()])];case 1:return l=c.sent(),f=l[0],d=l[1],h=La(f,d,n,r,i,a),o!==t&&o.dispose(),s!==e&&s.dispose(),[2,h]}})})},cropAndResize:Ul}),Vl=function(t,e){return!(t>0)||"linear"===e},ql=function(t,e,n){if(null==n||"linear"===n)return t;if("relu"===n)return t.mul(e.step());throw new Error("Gradient for activation "+n+" has not been implemented yet.")},Hl=function(t,e){var n=e,r=Br(t.shape,e.shape);return r.length>0&&(n=n.sum(r)),n.reshape(t.shape)},Gl=function(t,e,n){if("linear"===e)return t;if("relu"===e)return $c(t);if("elu"===e)return qc(t);if("relu6"===e)return Kc(t);if("prelu"===e)return Gc(t,n);throw new Error("Unknown fused activation "+e+".")},$l=Dn({fusedMatMul_:function(t){var e,n=t.a,r=t.b,i=t.transposeA,a=void 0!==i&&i,o=t.transposeB,s=void 0!==o&&o,u=t.bias,c=t.activation,l=void 0===c?"linear":c,f=t.preluActivationWeights;if(!1===Vl(Wt.state.gradientDepth,l)){var d=fc(n,r,a,s);return null!=u&&(d=Lr(d,u)),Gl(d,l,f)}var h=xn(n,"a","fused matMul"),p=xn(r,"b","fused matMul");e=Ft(h,p),h=e[0],p=e[1];var m=a?h.shape[h.rank-2]:h.shape[h.rank-1],g=s?p.shape[p.rank-1]:p.shape[p.rank-2],v=a?h.shape[h.rank-1]:h.shape[h.rank-2],y=s?p.shape[p.rank-2]:p.shape[p.rank-1],b=h.shape.slice(0,-2),x=p.shape.slice(0,-2),w=I(b),k=I(x);C(h.rank>=2&&p.rank>=2&&h.rank===p.rank,function(){return"Error in fused matMul: inputs must have the same rank of at least 2, got ranks "+h.rank+" and "+p.rank+"."}),C(D(b,x),function(){return"Error in fused matMul: outer dimensions ("+b+") and ("+x+") of Tensors with shapes "+h.shape+" and "+p.shape+" must match."}),C(m===g,function(){return"Error in fused matMul: inner shapes ("+m+") and ("+g+") of Tensors with shapes "+h.shape+" and "+p.shape+" and transposeA="+a+" and transposeB="+s+" must match."});var _,E,O=h.shape.slice(0,-2).concat([v,y]),S=a?h.as3D(w,m,v):h.as3D(w,v,m),A=s?p.as3D(k,y,g):p.as3D(k,g,y);null!=u&&Ur(O,(_=Ft(_=xn(u,"bias","fused matMul"),h)[0]).shape),null!=f&&(E=xn(f,"prelu weights","fused matMul"));var N={a:S,b:A};null!=u&&(N.bias=_),null!=f&&(N.preluActivationWeights=E);var T=[S,A];return Wt.runKernelFunc(function(t,e){var n=t.fusedBatchMatMul({a:S,b:A,transposeA:a,transposeB:s,bias:_,activation:l,preluActivationWeights:E});return e([S,A,n]),n},N,function(t,e){var n=e[0],r=e[1],i=e[2],o=ql(t,i,l),c={};return null!=u&&(c={bias:function(){return Hl(_,o)}}),a||s?!a&&s?Object.assign({a:function(){return o.matMul(r,!1,!1)},b:function(){return o.matMul(n,!0,!1)}},c):a&&!s?Object.assign({a:function(){return r.matMul(o,!1,!0)},b:function(){return n.matMul(o,!1,!1)}},c):Object.assign({a:function(){return r.matMul(o,!0,!0)},b:function(){return o.matMul(n,!0,!0)}},c):Object.assign({a:function(){return o.matMul(r,!1,!0)},b:function(){return n.matMul(o,!0,!1)}},c)},"_FusedMatMul",{transposeA:a,transposeB:s,activation:l},T,[!0]).reshape(O)}}),Kl=Dn({fusedConv2d_:function(t){var e=t.x,n=t.filter,r=t.strides,i=t.pad,a=t.dataFormat,o=void 0===a?"NHWC":a,s=t.dilations,u=void 0===s?[1,1]:s,c=t.dimRoundingMode,l=t.bias,f=t.activation,d=void 0===f?"linear":f,h=t.preluActivationWeights;if(d=d||"linear",!1===Vl(Wt.state.gradientDepth,d)){var p=ec(e,n,r,i,o,u,c);return null!=l&&(p=Lr(p,l)),Gl(p,d,h)}var m=xn(e,"x","conv2d"),g=xn(n,"filter","conv2d"),v=m,y=!1;3===m.rank&&(y=!0,v=m.as4D(1,m.shape[0],m.shape[1],m.shape[2])),C(4===v.rank,function(){return"Error in fused conv2d: input must be rank 4, but got rank "+v.rank+"."}),C(4===g.rank,function(){return"Error in fused conv2d: filter must be rank 4, but got rank "+g.rank+"."}),null!=c&&C(M(i),function(){return"Error in fused conv2d: pad must be an integer when using, dimRoundingMode "+c+" but got pad "+i+"."}),C(v.shape[3]===g.shape[2],function(){return"Error in conv2d: depth of input ("+v.shape[3]+") must match input depth for filter "+g.shape[2]+"."}),C(Oa(r,u),function(){return"Error in conv2D: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+u+"'"}),C("NHWC"===o,function(){return"Error in conv2d: got dataFormat of "+o+" but only NHWC is currently supported."});var b,x,w=va(v.shape,g.shape,r,u,i,c);null!=l&&(b=Ft(b=xn(l,"bias","fused conv2d"),m)[0],Ur(w.outShape,b.shape)),null!=h&&(x=xn(h,"prelu weights","fused conv2d"));var k={x:v,filter:g};null!=l&&(k.bias=b),null!=h&&(k.preluActivationWeights=x);var _=[g,v],E=Wt.runKernelFunc(function(t,e){var n=t.fusedConv2d({input:v,filter:g,convInfo:w,bias:b,activation:d,preluActivationWeights:x});return e([g,v,n]),n},k,function(t,e){var n=e,a=n[0],o=n[1],s=n[2],c=ql(t,s,d);C(Ea(u),function(){return"Error in gradient of fused conv2D: dilation rates greater than 1 are not yet supported in gradients. Got dilations '"+u+"'"});var f={};return null!=l&&(f={bias:function(){return Hl(b,c)}}),Object.assign({x:function(){return ic(o.shape,c,a,r,i)},filter:function(){return rc(o,c,a.shape,r,i)}},f)},"FusedConv2D",{convInfo:w,activation:d},_,[!0]);return y?E.as3D(E.shape[1],E.shape[2],E.shape[3]):E}}),Xl=Dn({fusedDepthwiseConv2d_:function(t){var e=t.x,n=t.filter,r=t.strides,i=t.pad,a=t.dataFormat,o=void 0===a?"NHWC":a,s=t.dilations,u=void 0===s?[1,1]:s,c=t.dimRoundingMode,l=t.bias,f=t.activation,d=void 0===f?"linear":f,h=t.preluActivationWeights;if(!1===Vl(Wt.state.gradientDepth,d)){var p=ac(e,n,r,i,o,u,c);return null!=l&&(p=Lr(p,l)),Gl(p,d,h)}var m=xn(e,"x","depthwiseConv2d"),g=xn(n,"filter","depthwiseConv2d"),v=m,y=!1;3===m.rank&&(y=!0,v=m.as4D(1,m.shape[0],m.shape[1],m.shape[2])),C(4===v.rank,function(){return"Error in fused depthwiseConv2d: input must be rank 4, but got rank "+v.rank+"."}),C(4===g.rank,function(){return"Error in fused depthwiseConv2d: filter must be rank 4, but got rank "+g.rank+"."}),C(v.shape[3]===g.shape[2],function(){return"Error in fused depthwiseConv2d: number of input channels ("+v.shape[3]+") must match the inChannels dimension in filter "+g.shape[2]+"."}),null==u&&(u=[1,1]),C(Oa(r,u),function(){return"Error in fused depthwiseConv2d: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+u+"'"}),null!=c&&C(M(i),function(){return"Error in fused depthwiseConv2d: pad must be an integer when using dimRoundingMode "+c+" but got pad "+i+"."});var b,x,w=va(v.shape,g.shape,r,u,i,c,!0);null!=l&&(b=Ft(b=xn(l,"bias","fused conv2d"),m)[0],Ur(w.outShape,b.shape)),null!=h&&(x=xn(h,"prelu weights","fused depthwiseConv2d"));var k={x:v,filter:g};null!=l&&(k.bias=b),null!=h&&(k.preluActivationWeights=x);var _=[g,v],E=Wt.runKernelFunc(function(t,e){var n=t.fusedDepthwiseConv2D({input:v,filter:g,convInfo:w,bias:b,activation:d,preluActivationWeights:x});return e([g,v,n]),n},k,function(t,e){C(Ea(u),function(){return"Error in gradient of fused depthwiseConv2d: dilation rates greater than 1 are not yet supported. Got dilations '"+u+"'"});var n=e[0],r=e[1],i=e[2],a=ql(t,i,d),o={};return null!=l&&(o={bias:function(){return Hl(b,a)}}),Object.assign({x:function(){return oc(r.shape,a,n,w)},filter:function(){return sc(r,a,n.shape,w)}},o)},"FusedDepthwiseConv2D",{convInfo:w,activation:d},_,[!0]);return y?E.as3D(E.shape[1],E.shape[2],E.shape[3]):E}}),Yl=Object.freeze({matMul:$l,conv2d:Kl,depthwiseConv2d:Xl}),Jl=Object.freeze({image:Wl,linalg:Fl,losses:Tl,spectral:ul,fused:Yl,signal:bl,add:Lr,addN:Ls,batchNorm:qs,batchNormalization:Vs,batchNorm2d:$s,batchNormalization2d:Gs,batchNorm3d:Ys,batchNormalization3d:Xs,batchNorm4d:Qs,batchNormalization4d:Zs,broadcastTo:tu,clone:eu,div:zi,divNoNan:uu,eye:lu,multinomial:fu,oneHot:du,pad:hu,pad1d:pu,pad2d:mu,pad3d:gu,pad4d:vu,rand:yu,randomGamma:Au,randomNormal:Nu,randomUniform:Tu,square:Iu,squaredDifference:Du,tile:cu,truncatedNormal:Mu,conv1d:tc,conv2d:ec,conv3d:nc,depthwiseConv2d:ac,separableConv2d:uc,conv2dTranspose:cc,conv3dTranspose:lc,op:Dn,booleanMaskAsync:Yu,complex:Mn,real:Rn,imag:Fn,concat:Qn,concat1d:tr,concat2d:er,concat3d:nr,concat4d:rr,split:ir,matMul:fc,dot:dc,outerProduct:hc,reverse:pc,reverse1d:mc,reverse2d:gc,reverse3d:vc,reverse4d:yc,maxPool:wc,avgPool:kc,pool:_c,maxPool3d:Ec,avgPool3d:Oc,maxPoolWithArgmax:Sc,slice:Cc,slice1d:Ac,slice2d:Nc,slice3d:Tc,slice4d:Ic,abs:Wr,acos:Vr,acosh:qr,asin:Hr,asinh:Gr,atan:$r,atanh:Kr,ceil:Xr,clipByValue:Yr,cos:Jr,cosh:Zr,erf:Qr,exp:ti,expm1:ei,floor:ni,log:ri,log1p:ii,logSigmoid:ai,neg:oi,reciprocal:si,round:ui,rsqrt:ci,sigmoid:li,sign:fi,isNaN:di,isInf:hi,isFinite:pi,sin:mi,sinh:gi,softplus:vi,sqrt:yi,step:bi,tan:xi,tanh:wi,all:Mc,any:Rc,argMax:Fc,argMin:jc,logSumExp:Pc,max:Lc,mean:zc,min:Bc,moments:Uc,sum:Wc,prod:Vc,equal:Ru,equalStrict:Fu,greater:ju,greaterEqual:Pu,greaterEqualStrict:Lu,greaterStrict:zu,less:Bu,lessEqual:Uu,lessEqualStrict:Wu,lessStrict:Vu,notEqual:qu,notEqualStrict:Hu,addStrict:ki,atan2:_i,divStrict:Ei,floorDiv:Oi,maximum:Si,maximumStrict:Ci,minimum:Ai,minimumStrict:Ni,mod:Ti,modStrict:Ii,mul:Di,mulStrict:Mi,pow:Ri,powStrict:Fi,squaredDifferenceStrict:ji,sub:Pi,subStrict:Li,elu:qc,leakyRelu:Hc,prelu:Gc,relu:$c,relu6:Kc,selu:Xc,logicalAnd:nu,logicalNot:ru,logicalOr:iu,logicalXor:au,where:ou,whereAsync:su,buffer:ar,print:or,batchToSpaceND:sr,cast:ur,cumsum:cr,depthToSpace:lr,expandDims:fr,reshape:dr,spaceToBatchND:hr,squeeze:pr,stack:mr,unstack:gr,setdiff1dAsync:vr,fill:Kn,linspace:Xn,ones:Gn,range:Yn,scalar:Ln,tensor:jn,tensor1d:zn,tensor2d:Bn,tensor3d:Un,tensor4d:Wn,tensor5d:Vn,tensor6d:qn,variable:Hn,zeros:$n,onesLike:Jn,zerosLike:Zn,transpose:fa,softmax:ca,logSoftmax:la,localResponseNormalization:Yc,norm:Jc,gather:Ku,unsortedSegmentSum:Xu,basicLSTMCell:Zc,multiRNNCell:Qc,movingAverage:tl,stridedSlice:el,topk:nl,scatterND:rl,fft:il,ifft:al,rfft:ol,irfft:sl,sparseToDense:cl,gatherND:ll,diag:fl,dropout:dl,hannWindow:ml,hammingWindow:gl,frame:vl,stft:yl,inTopKAsync:xl});function Zl(t,e){Array.isArray(t)||(t=[t]),t.forEach(function(t){null!=t&&C("complex64"!==t.dtype,function(){return e+" does not support complex64 tensors."})})}function Ql(t,e,n,r,i,a){for(var o=i.strideHeight,s=i.strideWidth,u=i.dilationHeight,c=i.dilationWidth,l=i.effectiveFilterHeight,f=i.effectiveFilterWidth,d=i.padInfo.top,h=i.padInfo.left,p="max"===a?Number.NEGATIVE_INFINITY:Number.POSITIVE_INFINITY,m=ar(i.outShape,n),g=m.values,v=i.outShape[1]*i.outShape[2]*i.outShape[3],y=i.outShape[2]*i.outShape[3],b=i.outShape[3],x=0;xM?M=z:"avg"===a&&(R+=z,F++)}if(isNaN(M))break}g[A+N*b+_]="avg"===a?R/F:M}return m}function tf(t,e,n,r,i,a){void 0===i&&(i=!1),void 0===a&&(a=!1);for(var o=ar(r.outShape,"int32"),s=r.strideHeight,u=r.strideWidth,c=r.dilationHeight,l=r.dilationWidth,f=r.effectiveFilterHeight,d=r.effectiveFilterWidth,h=r.padInfo.top,p=r.padInfo.left,m=ar(e,n,t),g=0;gS&&(S=D,C=i?a?((g*r.inHeight+A)*r.inWidth+T)*r.inChannels+v:(A*r.inWidth+T)*r.inChannels+v:N*d+I)}o.set(C,g,y,k,v)}}return o}function ef(t,e,n,r){if("linear"===n)return t.linear(e);if("relu"===n)return t.relu(e);if("elu"===n)return t.elu(e);if("relu6"===n)return t.relu6(e);if("prelu"===n)return t.prelu(e,r);throw new Error("Activation "+n+" has not been implemented for the CPU backend.")}var nf=function(t){function e(){var e=t.call(this)||this;return e.blockSize=48,e.firstUse=!0,e.data=new da(e,Wt),e}return s(e,t),e.prototype.write=function(t,e,n){this.firstUse&&(this.firstUse=!1,f().get("IS_NODE")&&vn("\n============================\nHi there 👋. 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.\n============================"));var r={};return this.data.set(r,{values:t,dtype:n}),r},e.prototype.move=function(t,e,n,r){this.data.set(t,{values:e,dtype:r})},e.prototype.numDataIds=function(){return this.data.numDataIds()},e.prototype.read=function(t){return u(this,void 0,void 0,function(){return c(this,function(e){return[2,this.readSync(t)]})})},e.prototype.readSync=function(t){var e=this.data.get(t),n=e.dtype,r=e.complexTensors;return"complex64"===n?Ia(this.readSync(r.real.dataId),this.readSync(r.imag.dataId)):this.data.get(t).values},e.prototype.bufferSync=function(t){var e=this.readSync(t.dataId),n=e;if("string"===t.dtype)try{n=e.map(function(t){return ct(t)})}catch(t){throw new Error("Failed to decode encoded string bytes into utf-8")}return ar(t.shape,t.dtype,n)},e.prototype.makeOutput=function(t,e,n){var r=this.write(t,e,n);return Wt.makeTensorFromDataId(r,e,n,this)},e.prototype.disposeData=function(t){if(this.data.has(t)){var e=this.data.get(t).complexTensors;null!=e&&(e.real.dispose(),e.imag.dispose()),this.data.delete(t)}},e.prototype.time=function(t){return u(this,void 0,void 0,function(){var e;return c(this,function(n){return e=ot(),t(),[2,{kernelMs:ot()-e}]})})},e.prototype.memory=function(){return{unreliable:!0,reasons:["The reported memory is an upper bound. Due to automatic garbage collection, the true allocated memory may be less."]}},e.prototype.complex=function(t,e){var n=this.makeOutput(null,t.shape,"complex64");return this.data.get(n.dataId).complexTensors={real:Wt.keep(t.clone()),imag:Wt.keep(e.clone())},n},e.prototype.real=function(t){return this.data.get(t.dataId).complexTensors.real.clone()},e.prototype.imag=function(t){return this.data.get(t.dataId).complexTensors.imag.clone()},e.prototype.slice=function(t,e,n){if(Zl(t,"slice"),Qi(t.shape,e,n)){var r=ta(e,t.strides),i=I(n);return jn(this.readSync(t.dataId).subarray(r,r+i),n,t.dtype)}for(var a=ar(n,t.dtype),o=this.bufferSync(t),s=0;sd&&(d=m,h=p)}u[l]=h}return o},e.prototype.cumsum=function(t,e,n,r){if(Zl(t,"cumsum"),e!==t.rank-1)throw new Error("backend.cumsum in CPU expects an inner-most axis="+(t.rank-1)+" but got axis="+e);for(var i=Mt(t.dtype,"int32"),a=$n(t.shape,i),o=this.readSync(a.dataId),s=this.readSync(t.dataId),u=t.shape[t.rank-1],c=r?function(t,e){return t+u-e-1}:function(t,e){return t+e},l=0;le?1:0})},e.prototype.greaterEqual=function(t,e){return Zl([t,e],"greaterEqual"),this.broadcastedBinaryOp(t,e,"bool",function(t,e){return t>=e?1:0})},e.prototype.logicalNot=function(t){Zl(t,"logicalNot");for(var e=this.readSync(t.dataId),n=new Uint8Array(e.length),r=0;r1||1===e.rank?1:I(e.shape.slice(1)),l=0;l=0&&e>=0?n:(n+e)%e})},e.prototype.max=function(t,e){Zl(t,"max"),Sn("max",e,t.rank);for(var n=En(t.shape,e),r=n[0],i=n[1],a=$n(r,t.dtype),o=I(i),s=this.readSync(a.dataId),u=this.readSync(t.dataId),c=0;cf&&(f=h)}s[c]=f}return a},e.prototype.maximum=function(t,e){return Zl([t,e],"maximum"),this.broadcastedBinaryOp(t,e,t.dtype,function(t,e){return Math.max(t,e)})},e.prototype.all=function(t,e){Zl(t,"all"),Sn("all",e,t.rank);for(var n=En(t.shape,e),r=n[0],i=n[1],a=$n(r,t.dtype),o=I(i),s=this.readSync(a.dataId),u=this.readSync(t.dataId),c=0;c0?n[r]=1:n[r]=0;return this.makeOutput(n,t.shape,"float32")},e.prototype.isNaN=function(t){Zl(t,"x");for(var e=this.readSync(t.dataId),n=new Uint8Array(e.length),r=0;r.5?n[r]=Math.ceil(e[r]):n[r]=i%2==0?i:i+1}return this.makeOutput(n,t.shape,"float32")},e.prototype.exp=function(t){Zl(t,"exp");for(var e=this.readSync(t.dataId),n=new Float32Array(e.length),r=0;r=0?i:Math.exp(i)-1}return this.makeOutput(e,t.shape,"float32")},e.prototype.eluDer=function(t,e){Zl([t,e],"eluDer");for(var n=new Float32Array(e.size),r=this.readSync(e.dataId),i=this.readSync(t.dataId),a=0;a=1?i[a]:i[a]*(o+1)}return this.makeOutput(n,e.shape,"float32")},e.prototype.selu=function(t){Zl(t,"selu");for(var e=new Float32Array(t.size),n=this.readSync(t.dataId),r=0;r=0?1.0507009873554805*i:1.7580993408473768*(Math.exp(i)-1)}return this.makeOutput(e,t.shape,"float32")},e.prototype.clip=function(t,e,n){Zl(t,"clip");for(var r=new Float32Array(t.size),i=this.readSync(t.dataId),a=0;an?n:o-e,s=r[i]0?1:e}return this.makeOutput(n,t.shape,"float32")},e.prototype.fusedConv2d=function(t){var e=t.input,n=t.filter,r=t.convInfo,i=t.bias,a=t.activation,o=t.preluActivationWeights,s=this.conv2d(e,n,r);return i&&(s=this.add(s,i)),a&&(s=ef(this,s,a,o)),s},e.prototype.conv2d=function(t,e,n){Zl([t,e],"conv2d");for(var r=n.filterHeight,i=n.filterWidth,a=n.dilationHeight,o=n.dilationWidth,s=n.padInfo.left,u=n.padInfo.top,c="channelsLast"===n.dataFormat,l=ar(n.outShape,t.dtype),f=t.strides[0],d=c?t.strides[1]:t.strides[2],h=c?t.strides[2]:1,p=c?1:t.strides[1],m=l.strides[0],g=c?l.strides[1]:l.strides[2],v=c?l.strides[2]:1,y=c?1:l.strides[1],b=this.readSync(t.dataId),x=this.readSync(e.dataId),w=l.values,k=0;k=n.inHeight))for(var T=A*e.strides[0],I=_+N*d,D=0;D=n.inWidth))for(var P=I+j*h,L=T+F*e.strides[1],z=0;z=n.inDepth))for(var E=k*e.strides[0],O=v+_*t.strides[1],S=0;S=n.inHeight))for(var I=E+N*e.strides[1],D=O+T*t.strides[2],M=0;M=n.inWidth))for(var L=I+j*e.strides[2],z=D+P*n.inChannels,B=L,U=0;U=n.inHeight))for(var k=x*e.strides[0],_=m+w*t.strides[1],E=0;E=n.inWidth))for(var N=k+C*e.strides[1],T=_+A*n.inChannels,I=O,D=N,M=0;M=r.outHeight||Math.floor(E)!==E))for(var O=0;O=r.outWidth||Math.floor(S)!==S)){var C=l*f-1-a.get(g,E,S,v)===_*f+O?1:0;0!==C&&(k+=m.get(g,E,S,v)*C)}}}p.set(k,g,y,b,v)}return p.toTensor()},e.prototype.avgPoolBackprop=function(t,e,n){Zl([t,e],"avgPoolBackprop");for(var r=n.strideHeight,i=n.strideWidth,a=n.filterHeight,o=n.filterWidth,s=n.dilationHeight,u=n.dilationWidth,c=n.effectiveFilterHeight,l=n.effectiveFilterWidth,f=l-1-n.padInfo.left,d=c-1-n.padInfo.top,h=ar(e.shape,"float32"),p=1/(a*o),m=this.bufferSync(t),g=0;g=n.outHeight||Math.floor(E)!==E))for(var O=0;O=n.outWidth||Math.floor(S)!==S||(k+=m.get(g,E,S,v))}}h.set(k*p,g,y,b,v)}return h.toTensor()},e.prototype.pool3d=function(t,e,n){Zl(t,"pool3d");for(var r=e.strideDepth,i=e.strideHeight,a=e.strideWidth,o=e.dilationDepth,s=e.dilationHeight,u=e.dilationWidth,c=e.effectiveFilterDepth,l=e.effectiveFilterHeight,f=e.effectiveFilterWidth,d=e.padInfo.front,h=e.padInfo.top,p=e.padInfo.left,m="max"===n?Number.NEGATIVE_INFINITY:Number.POSITIVE_INFINITY,g=this.readSync(t.dataId),v=ar(e.outShape,t.dtype),y=v.values,b=e.outShape[1]*e.outShape[2]*e.outShape[3]*e.outShape[4],x=e.outShape[2]*e.outShape[3]*e.outShape[4],w=e.outShape[3]*e.outShape[4],k=e.outShape[4],_=0;_W?W=Y:"avg"===n&&(V+=Y,q++),isNaN(W))break}if(isNaN(W))break}if(isNaN(W))break}y[U+S]="avg"===n?V/q:W}}}return v.toTensor()},e.prototype.avgPool3d=function(t,e){return Zl(t,"avgPool3d"),this.pool3d(t,e,"avg").toFloat()},e.prototype.avgPool3dBackprop=function(t,e,n){Zl([t,e],"avgPool3dBackprop");for(var r=n.strideDepth,i=n.strideHeight,a=n.strideWidth,o=n.filterDepth,s=n.filterHeight,u=n.filterWidth,c=n.dilationDepth,l=n.dilationHeight,f=n.dilationWidth,d=n.effectiveFilterDepth,h=n.effectiveFilterHeight,p=n.effectiveFilterWidth,m=d-1-n.padInfo.front,g=p-1-n.padInfo.left,v=h-1-n.padInfo.top,y=ar(e.shape,"float32"),b=1/(o*s*u),x=this.bufferSync(t),w=0;w=n.outDepth||Math.floor(I)!==I))for(var D=0;D=n.outHeight||Math.floor(M)!==M))for(var R=0;R=n.outWidth||Math.floor(F)!==F||(N+=x.get(w,I,M,F,k))}}}y.set(N*b,w,_,E,O,k)}return y.toTensor()},e.prototype.maxPool3d=function(t,e){return Zl(t,"maxPool3d"),this.pool3d(t,e,"max").toFloat()},e.prototype.maxPool3dPositions=function(t,e){for(var n=ar(e.outShape,"int32"),r=e.strideDepth,i=e.strideHeight,a=e.strideWidth,o=e.dilationDepth,s=e.dilationHeight,u=e.dilationWidth,c=e.effectiveFilterDepth,l=e.effectiveFilterHeight,f=e.effectiveFilterWidth,d=e.padInfo.front,h=e.padInfo.top,p=e.padInfo.left,m=this.bufferSync(t),g=0;g=T&&(T=L,I=M*l*f+F*l+P)}n.set(I,g,y,k,S,v)}}}return n.toTensor()},e.prototype.maxPool3dBackprop=function(t,e,n,r){Zl([e,n],"maxPool3dBackprop");for(var i=this.maxPool3dPositions(e,r),a=r.strideDepth,o=r.strideHeight,s=r.strideWidth,u=r.dilationDepth,c=r.dilationHeight,l=r.dilationWidth,f=r.effectiveFilterDepth,d=r.effectiveFilterHeight,h=r.effectiveFilterWidth,p=f-1-r.padInfo.front,m=h-1-r.padInfo.left,g=d-1-r.padInfo.top,v=ar(e.shape,"float32"),y=this.bufferSync(i),b=this.bufferSync(t),x=0;x=r.outDepth||Math.floor(T)!==T))for(var I=0;I=r.outHeight||Math.floor(D)!==D))for(var M=0;M=r.outWidth||Math.floor(R)!==R)){var F=f*d*h-1-y.get(x,T,D,R,w)===N*d*h+I*h+M?1:0;0!==F&&(A+=b.get(x,T,D,R,w)*F)}}}}v.set(A,x,k,_,E,w)}return v.toTensor()},e.prototype.cast=function(t,e){return Ca(t,e,this)},e.prototype.reshape=function(t,e){return Aa(t,e)},e.prototype.avgPool=function(t,e){return Zl(t,"avgPool"),Zl(t,"maxPool"),Ql(this.readSync(t.dataId),t.shape,t.dtype,t.strides,e,"avg").toTensor().toFloat()},e.prototype.resizeBilinear=function(t,e,n,r){Zl(t,"resizeBilinear");for(var i=t.shape,a=i[0],o=i[1],s=i[2],u=i[3],c=this.readSync(t.dataId),l=new Float32Array(I([a,e,n,u])),f=[r&&e>1?o-1:o,r&&n>1?s-1:s],d=[r&&e>1?e-1:e,r&&n>1?n-1:n],h=0,p=f[0]/d[0],m=f[1]/d[1],g=0;g1?a-1:a,n&&l>1?o-1:o],h=[n&&c>1?c-1:c,n&&l>1?l-1:l],p=d[0]/h[0],m=d[1]/h[1],g=this.readSync(t.dataId),v=0,y=0;y1?o-1:o,r&&n>1?s-1:s],d=[r&&e>1?e-1:e,r&&n>1?n-1:n],h=f[0]/d[0],p=f[1]/d[1],m=0,g=0;g1?a-1:a,n&&l>1?o-1:o],p=[n&&c>1?c-1:c,n&&l>1?l-1:l],m=h[0]/p[0],g=h[1]/p[1],v=1/m,y=1/g,b=2*Math.ceil(v)+2,x=2*Math.ceil(y)+2,w=0;w=c)){var F=k+R*t.strides[1],j=R*m;if(_===Math.min(a-1,n?Math.round(j):Math.floor(j)))for(var P=0;P=l)){var z=F+L*t.strides[2],B=L*g;C===Math.min(o-1,n?Math.round(B):Math.floor(B))&&(D+=d[z+I])}}}}f[A+I]=D}return Wn(f,e.shape,e.dtype)},e.prototype.batchNormalization=function(t,e,n,r,i,a){Zl([t,e,n,i,a],"batchNorm");for(var o=this.readSync(t.dataId),s=this.readSync(e.dataId),u=this.readSync(n.dataId),c=i?this.readSync(i.dataId):new Float32Array([1]),l=a?this.readSync(a.dataId):new Float32Array([0]),f=new Float32Array(o.length),d=l.length,h=c.length,p=u.length,m=s.length,g=0,v=0,y=0,b=0,x=0;x=d&&(g=0),v>=m&&(v=0),y>=h&&(y=0),b>=p&&(b=0);return Wn(f,t.shape)},e.prototype.localResponseNormalization4D=function(t,e,n,r,i){Zl(t,"localResponseNormalization4D");var a=t.shape[3],o=a-1,s=this.readSync(t.dataId),u=t.size,c=new Float32Array(u);function l(t){for(var n=t%a,r=t-n+Math.max(0,n-e),i=t-n+Math.min(n+e,o),u=0;r<=i;r++){var c=s[r];u+=c*c}return u}for(var f=0;f=0&&a[o]1,function(){return"blockSize should be > 1 for depthToSpace, but was: "+e});for(var r=t.shape[0],i=t.shape[1],a=t.shape[2],o=t.shape[3],s=i*e,u=a*e,c=o/(e*e),l=this.readSync(t.dataId),f=new Float32Array(r*s*u*c),d=0,h=0;h=s))for(var C=d>1?(E-k)*(u-1)/(d-1):0,A=h>1?(O-_)*(c-1)/(h-1):0,N=0;N1?k*(u-1)+N*C:.5*(k+E)*(u-1);if(T<0||T>u-1)for(var I=0;I1?_*(c-1)+I*A:.5*(_+O)*(c-1))<0||H>c-1)for(D=0;D1?_*(c-1)+I*A:.5*(_+O)*(c-1))<0||H>c-1)for(D=0;D=t.size/s)throw new Error("Invalid indices: "+h+" does not index into "+t.shape);for(var v=0;v=r/i)throw new Error("Invalid indices: "+m+" does not index into "+n);for(var b=0;b6)throw Error("Packed transpose for rank "+this.rank+" is not yet supported.");var i=oo(this.rank),a=$a("rc",this.rank),o=new Array(this.rank);for(r=0;r6)throw Error("Transpose for rank "+e+" is not yet supported");for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(e),i=0;i0&&(e=e.sum(r)),e.reshape(n.shape)},b:function(){var e=t,n=Br(r.shape,i);return n.length>0&&(e=e.sum(n)),e.reshape(r.shape)}}}},{kernelName:"AddN",saveAllInputs:!0,gradFunc:function(t,e){var n={};return e.forEach(function(e,r){n[r]=function(){return t.clone()}}),n}},{kernelName:Ir,gradFunc:function(t,e,n){for(var r=n,i=r.inputShape,a=r.shape,o=Array.from(a),s=i.length-1;s>=0;s--)if(i[s]===a[s])o[s]=1;else if(1!==i[s])throw new Error("broadcastTo(): ["+i+"] cannot be broadcast to ["+a+"].");var u=[];for(s=0;s1&&u.push(s);return{x:function(){return t.sum(u,!0)}}}},{kernelName:Or,inputsToSave:["a","b"],gradFunc:function(t,e){var n=e[0],r=e[1],i=Ur(n.shape,r.shape);return{a:function(){var e=zi(t,r.toFloat()),a=Br(n.shape,i);return a.length>0?Wc(e,a).reshape(n.shape):e},b:function(){var e=t.mul(n.toFloat()),a=Br(r.shape,i);a.length>0&&(e=Wc(e,a).reshape(r.shape));var o=Iu(r);return oi(zi(e,o.toFloat()))}}}},{kernelName:"FusedBatchNorm",inputsToSave:["x","mean","variance","scale"],gradFunc:function(t,e,n){var r=n.varianceEpsilon,i=e[0],a=e[1],o=e[2],s=e[3],u=Bs(i),c=null==s?Ln(1):s,l=Br(a.shape,u.shape),f=[];if(1===a.rank){for(var d=0;d0,function(){return"scheme must not be an empty string."});var r=t.getInstance();C(null==r.managers[e],function(){return"A model store manager is already registered for scheme '"+e+"'."}),r.managers[e]=n},t.getManager=function(t){var e=this.getInstance().managers[t];if(null==e)throw new Error("Cannot find model manager for scheme '"+t+"'");return e},t.getSchemes=function(){return Object.keys(this.getInstance().managers)},t}();function Rf(t){if(-1===t.indexOf(Df))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+Mf.getSchemes().join(","));return{scheme:t.split(Df)[0],path:t.split(Df)[1]}}function Ff(t,e,n){return void 0===n&&(n=!1),u(this,void 0,void 0,function(){var r,i,a,o,s,u,l,f,d;return c(this,function(c){switch(c.label){case 0:return C(t!==e,function(){return"Old path and new path are the same: '"+t+"'"}),C((r=If.getLoadHandlers(t)).length>0,function(){return"Copying failed because no load handler is found for source URL "+t+"."}),C(r.length<2,function(){return"Copying failed because more than one ("+r.length+") load handlers for source URL "+t+"."}),i=r[0],C((a=If.getSaveHandlers(e)).length>0,function(){return"Copying failed because no save handler is found for destination URL "+e+"."}),C(a.length<2,function(){return"Copying failed because more than one ("+r.length+") save handlers for destination URL "+e+"."}),o=a[0],s=Rf(t).scheme,u=Rf(t).path,l=s===Rf(t).scheme,[4,i.load()];case 1:return f=c.sent(),n&&l?[4,Mf.getManager(s).removeModel(u)]:[3,3];case 2:c.sent(),c.label=3;case 3:return[4,o.save(f)];case 4:return d=c.sent(),!n||l?[3,6]:[4,Mf.getManager(s).removeModel(u)];case 5:c.sent(),c.label=6;case 6:return[2,d.modelArtifactsInfo]}})})}var jf="models_store",Pf="model_info_store";function Lf(){if(!f().getBool("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var t=window||self,e=t.indexedDB||t.mozIndexedDB||t.webkitIndexedDB||t.msIndexedDB||t.shimIndexedDB;if(null==e)throw new Error("The current browser does not appear to support IndexedDB.");return e}function zf(t){var e=t.result;e.createObjectStore(jf,{keyPath:"modelPath"}),e.createObjectStore(Pf,{keyPath:"modelPath"})}var Bf=function(){function t(t){if(this.indexedDB=Lf(),null==t||!t)throw new Error("For IndexedDB, modelPath must not be null, undefined or empty.");this.modelPath=t}return t.prototype.save=function(t){return u(this,void 0,void 0,function(){return c(this,function(e){if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");return[2,this.databaseAction(this.modelPath,t)]})})},t.prototype.load=function(){return u(this,void 0,void 0,function(){return c(this,function(t){return[2,this.databaseAction(this.modelPath)]})})},t.prototype.databaseAction=function(t,e){var n=this;return new Promise(function(t,r){var i=n.indexedDB.open("tensorflowjs",1);i.onupgradeneeded=function(){return zf(i)},i.onsuccess=function(){var a=i.result;if(null==e){var o=a.transaction(jf,"readonly"),s=o.objectStore(jf).get(n.modelPath);s.onsuccess=function(){if(null==s.result)return a.close(),r(new Error("Cannot find model with path '"+n.modelPath+"' in IndexedDB."));t(s.result.modelArtifacts)},s.onerror=function(t){return a.close(),r(s.error)},o.oncomplete=function(){return a.close()}}else{var u,c=Tf(e),l=a.transaction(Pf,"readwrite"),f=l.objectStore(Pf),d=f.put({modelPath:n.modelPath,modelArtifactsInfo:c});d.onsuccess=function(){var i=(u=a.transaction(jf,"readwrite")).objectStore(jf).put({modelPath:n.modelPath,modelArtifacts:e,modelArtifactsInfo:c});i.onsuccess=function(){return t({modelArtifactsInfo:c})},i.onerror=function(t){var e=(f=l.objectStore(Pf)).delete(n.modelPath);e.onsuccess=function(){return a.close(),r(i.error)},e.onerror=function(t){return a.close(),r(i.error)}}},d.onerror=function(t){return a.close(),r(d.error)},l.oncomplete=function(){null==u?a.close():u.oncomplete=function(){return a.close()}}}},i.onerror=function(t){return r(i.error)}})},t.URL_SCHEME="indexeddb://",t}(),Uf=function(t){return f().getBool("IS_BROWSER")&&!Array.isArray(t)&&t.startsWith(Bf.URL_SCHEME)?(e=t.slice(Bf.URL_SCHEME.length),new Bf(e)):null;var e};If.registerSaveRouter(Uf),If.registerLoadRouter(Uf);var Wf=function(){function t(){this.indexedDB=Lf()}return t.prototype.listModels=function(){return u(this,void 0,void 0,function(){var t=this;return c(this,function(e){return[2,new Promise(function(e,n){var r=t.indexedDB.open("tensorflowjs",1);r.onupgradeneeded=function(){return zf(r)},r.onsuccess=function(){var t=r.result,i=t.transaction(Pf,"readonly"),a=i.objectStore(Pf).getAll();a.onsuccess=function(){for(var t={},n=0,r=a.result;n0,function(){return"promises must be a none empty array"})}(t),function(t,e){C(t>=0&&t<=1,function(){return"Progress fraction must be in range [0, 1], but got startFraction "+t}),C(e>=0&&e<=1,function(){return"Progress fraction must be in range [0, 1], but got endFraction "+e}),C(e>=t,function(){return"startFraction must be no more than endFraction, but got startFraction "+t+" and endFraction "+e})}(n=null==n?0:n,r=null==r?1:r);var i=0;return Promise.all(t.map(function(a){return a.then(function(a){var o=n+ ++i/t.length*(r-n);return e(o),a}),a}))}function ud(t,e){return u(this,void 0,void 0,function(){var n,r,i,a,o,s,u,l,d;return c(this,function(c){switch(c.label){case 0:return null==e&&(e={}),n=null==e.fetchFunc?f().platform.fetch:e.fetchFunc,r=t.map(function(t){return n(t,e.requestInit,{isBinary:!0})}),i=0,a=.5,null!=e.onProgress?[3,2]:[4,Promise.all(r)];case 1:return o=c.sent(),[3,4];case 2:return[4,sd(r,e.onProgress,i,a)];case 3:o=c.sent(),c.label=4;case 4:return s=o.map(function(t){return t.arrayBuffer()}),u=.5,l=1,null!=e.onProgress?[3,6]:[4,Promise.all(s)];case 5:return d=c.sent(),[3,8];case 6:return[4,sd(s,e.onProgress,u,l)];case 7:d=c.sent(),c.label=8;case 8:return[2,d]}})})}function cd(t){var e=this;return function(n,r,i){return void 0===r&&(r=""),u(e,void 0,void 0,function(){var e,a,o,s,u,l,f,d,h,p;return c(this,function(c){switch(c.label){case 0:if(e=n.map(function(){return!1}),a={},o=null!=i?i.map(function(){return!1}):[],s=[],n.forEach(function(t,n){var r=0;t.weights.forEach(function(t){var u="quantization"in t?t.quantization.dtype:t.dtype,c=_f[u]*I(t.shape),l=function(){e[n]=!0,null==a[n]&&(a[n]=[]),a[n].push({manifestEntry:t,groupOffset:r,sizeBytes:c})};null!=i?i.forEach(function(e,n){e===t.name&&(l(),o[n]=!0)}):l(),s.push(t.name),r+=c})}),!o.every(function(t){return t}))throw u=i.filter(function(t,e){return!o[e]}),new Error("Could not find weights in manifest with names: "+u.join(", ")+". \nManifest JSON has weights with names: "+s.join(", ")+".");return l=e.reduce(function(t,e,n){return e&&t.push(n),t},[]),f=[],l.forEach(function(t){n[t].paths.forEach(function(t){var e=r+(r.endsWith("/")?"":"/")+t;f.push(e)})}),[4,t(f)];case 1:return d=c.sent(),h={},p=0,l.forEach(function(t){for(var e=n[t].paths.length,r=0,i=0;i0,function(){return"URL path for http must not be null, undefined or empty."}),Array.isArray(t)&&C(2===t.length,function(){return"URL paths for http must have a length of 2, (actual length is "+t.length+")."}),this.path=t,null!=e.requestInit&&null!=e.requestInit.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=e.requestInit||{}}return t.prototype.save=function(t){return u(this,void 0,void 0,function(){var e,n,r,i;return c(this,function(a){switch(a.label){case 0:if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(e=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:t.weightSpecs}],r={modelTopology:t.modelTopology,format:t.format,generatedBy:t.generatedBy,convertedBy:t.convertedBy,userDefinedMetadata:t.userDefinedMetadata,weightsManifest:n},e.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=t.weightData&&e.body.append("model.weights.bin",new Blob([t.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,this.fetch(this.path,e)];case 1:if((i=a.sent()).ok)return[2,{modelArtifactsInfo:Tf(t),responses:[i]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+i.status+".")}})})},t.prototype.load=function(){return u(this,void 0,void 0,function(){var t,e,n,r,i,a,o,s,u,l,f,d;return c(this,function(c){switch(c.label){case 0:return[4,this.fetch(this.path,this.requestInit)];case 1:if(!(t=c.sent()).ok)throw new Error("Request to "+this.path+" failed with status code "+t.status+". Please verify this URL points to the model JSON of the model to load.");c.label=2;case 2:return c.trys.push([2,4,,5]),[4,t.json()];case 3:return e=c.sent(),[3,5];case 4:throw c.sent(),n="Failed to parse model JSON of response from "+this.path+".",this.path.endsWith(".pb")?n+=" 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.":n+=" Please make sure the server is serving valid JSON for this request.",new Error(n);case 5:if(r=e.modelTopology,i=e.weightsManifest,a=e.generatedBy,o=e.convertedBy,s=e.format,u=e.userDefinedMetadata,null==r&&null==i)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");return null==i?[3,7]:[4,this.loadWeights(i)];case 6:d=c.sent(),l=d[0],f=d[1],c.label=7;case 7:return[2,{modelTopology:r,weightSpecs:l,weightData:f,userDefinedMetadata:u,generatedBy:a,convertedBy:o,format:s}]}})})},t.prototype.loadWeights=function(t){return u(this,void 0,void 0,function(){var e,n,r,i,a,o,s,u,l,f,d;return c(this,function(c){switch(c.label){case 0:for(e=Array.isArray(this.path)?this.path[1]:this.path,n=function(t){var e=t.lastIndexOf("/"),n=t.lastIndexOf("?");return[t.substring(0,e)+"/",n>e?t.substring(n):""]}(e),r=n[0],i=n[1],a=this.weightPathPrefix||r,o=[],s=0,u=t;s0&&Number.isInteger(n),function(){return"If provided, numClasses must be a positive integer, but got "+n}),C(1===r.rank,function(){return"Expected the rank of labels to be 1, but got "+r.rank}),C(1===i.rank,function(){return"Expected the rank of predictions to be 1, but got "+i.rank}),C(r.shape[0]===i.shape[0],function(){return"Mismatch in the number of examples: "+r.shape[0]+" vs. "+i.shape[0]+". Labels and predictions should have the same number of elements."}),C(n>0&&Number.isInteger(n),function(){return"numClasses is required to be a positive integer, but got "+n});var a=du(r.asType("int32"),n),o=du(i.asType("int32"),n);return a.transpose().matMul(o).asType("int32")}}),bd=Object.freeze({confusionMatrix:yd}),xd=Dn({fromPixels_:function(t,e){if(void 0===e&&(e=3),e>4)throw new Error("Cannot construct Tensor with more than 4 channels from pixels.");if(null==t)throw new Error("pixels passed to tf.browser.fromPixels() can not be null");var n=!1,r=!1,i=!1,a=!1,o=!1;if(t.data instanceof Uint8Array)n=!0;else if("undefined"!=typeof ImageData&&t instanceof ImageData)r=!0;else if("undefined"!=typeof HTMLVideoElement&&t instanceof HTMLVideoElement)i=!0;else if("undefined"!=typeof HTMLImageElement&&t instanceof HTMLImageElement)a=!0;else{if(null==t.getContext)throw new Error("pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, but was "+t.constructor.name);o=!0}if(i&&i&&t.readyState<2)throw new Error("The video element has not loaded data yet. Please wait for `loadeddata` event on the