diff --git a/README.md b/README.md index d3ed4936..df98850e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/justadudewhohacks/face-api.js.svg?branch=master)](https://travis-ci.org/justadudewhohacks/face-api.js) [![Slack](https://slack.bri.im/badge.svg)](https://slack.bri.im) -**JavaScript face recognition API for the browser and nodejs implemented on top of tensorflow.js core ([tensorflow/tfjs-core](https://github.com/tensorflow/tfjs-core))** +**JavaScript face recognition API for the browser and nodejs implemented on top of tensorflow.js core ([tensorflow/tfjs-core](https://github.com/tensorflow/tfjs))** ![faceapi](https://user-images.githubusercontent.com/31125521/57224752-ad3dc080-700a-11e9-85b9-1357b9f9bca4.gif) @@ -16,6 +16,7 @@ * **[Realtime Webcam Face Detection And Emotion Recognition - Video](https://youtu.be/CVClHLwv-4I)** * **[Easy Face Recognition Tutorial With JavaScript - Video](https://youtu.be/AZ4PdALMqx0)** * **[Using face-api.js with Vue.js and Electron](https://medium.com/@andreas.schallwig/do-not-laugh-a-simple-ai-powered-game-3e22ad0f8166)** +* **[Add Masks to People - Gant Laborde on Learn with Jason](https://www.learnwithjason.dev/fun-with-machine-learning-pt-2)** ## Table of Contents diff --git a/dist/face-api.js b/dist/face-api.js index dfdc420a..a4373418 100644 --- a/dist/face-api.js +++ b/dist/face-api.js @@ -6,7 +6,7 @@ /** * @license - * Copyright 2019 Google LLC. All Rights Reserved. + * 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 @@ -20,285 +20,291 @@ * limitations under the License. * ============================================================================= */ - var t=function(e,n){return (t=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]);})(e,n)};function e(e,n){function r(){this.constructor=e;}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r);}function n(t,e,n,r){return new(n||(n=Promise))(function(o,a){function i(t){try{s(r.next(t));}catch(t){a(t);}}function u(t){try{s(r.throw(t));}catch(t){a(t);}}function s(t){t.done?o(t.value):new n(function(e){e(t.value);}).then(i,u);}s((r=r.apply(t,e||[])).next());})}function r(t,e){var n,r,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(a){return function(u){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,r=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===a[0]||2===a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]0;)r=Math.random()*e|0,n=t[--e],t[e]=t[r],t[r]=n;}function d(t,e,n){return Math.max(t,Math.min(e,n))}function v(t){return t%2==0?t:t+1}function m(t){for(var e=0,n=0;n=n?o():setTimeout(i,u);}};i();})}function A(t,e){for(var n=1,r=-1,o=0;o=0)n*=t[o];else if(-1===t[o]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+o);r=o;}else if(t[o]<0)throw Error("Shapes can not be < 0. Found "+t[o]+" at dim "+o);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 D(t,e){var n=e.length;return g((t=null==t?e.map(function(t,e){return e}):[].concat(t)).every(function(t){return t>=-n&&tu)&&1===t[u]&&(n.push(t[u]),r.push(u)),a[i]<=u&&i++;}1!==t[u]&&(n.push(t[u]),r.push(u));}return {newShape:n,keptDims:r}}function N(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 F(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 _(t,e){for(var n=0;n=0;--r)n[r]=n[r+1]*t[r+1];return n}function K(t,e,n){if("string"===e)throw new Error("Cannot convert a string[] to a TypedArray");if(Array.isArray(t)&&(t=b(t)),n&&_(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),o=0;o=0,function(){return "Tensor must have a shape comprised of positive integers but got shape ["+t+"]."});});}function J(t,e){return void 0===e&&(e="utf-8"),e=e||"utf-8",a().platform.encode(t,e)}function Z(t,e){return void 0===e&&(e="utf-8"),e=e||"utf-8",a().platform.decode(t,e)}var tt=Object.freeze({shuffle:p,clamp:d,nearestLargerEven:v,sum:m,randUniform:function(t,e){var n=Math.random();return e*n+(1-n)*t},distSquared:function(t,e){for(var n=0,r=0;r0?p:"")+" ";}console.log("%c"+u+"\t%c"+i+"\t%c"+s+"D "+l+"\t%c"+c+"\t%c"+h+"\t%c"+a,"font-weight:bold","color:red","color:blue","color: orange","color: green","color: steelblue");},t}();var rt=20,ot=3,at=7;function it(t,e,n,r){var o=q(e),a=function(t,e,n,r){var o=w(e),a=r[r.length-1],i=new Array(a).fill(0),u=e.length,s="complex64"===n?ct(t):t;if(u>1)for(var c=0;crt){var h=ot*u,f=Array.from(e.slice(0,h)),p=Array.from(e.slice((s-ot)*u,s*u));return "complex64"===r&&(f=ct(f),p=ct(p)),["["+f.map(function(t,e){return ut(t,a[e],r)}).join(", ")+", ..., "+p.map(function(t,e){return ut(t,a[s-ot+e],r)}).join(", ")+"]"]}var d="complex64"===r?ct(e):Array.from(e);return ["["+d.map(function(t,e){return ut(t,a[e],r)}).join(", ")+"]"]}var v=n.slice(1);var m=o.slice(1);var g=o[0]*u;var y=[];if(s>rt){for(var x=0;x=this.shape[n]){var i="Requested out of range element at "+t+". Buffer shape="+this.shape;throw new Error(i)}n++;}for(var u=t[t.length-1],s=0;s0)throw new Error("Backend '"+this.backendName+"' has an internal memory leak ("+i+" data ids) after running '"+t+"'")},t.prototype.runKernelFunc=function(t,e,n,r,o,a,i){var u,c=this;void 0===a&&(a=[]),void 0===i&&(i=[]);var l,h=[],f=this.isTapeOn(),p=null!=this.state.activeScope?this.state.activeScope.name:"",d=function(t){f&&(h=t.map(function(t){return c.keep(c.clone(t))}));},v=this.state.numBytes,m=this.state.numTensors;this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack.push(0);var g,y=s(r,this.backendName);return l=null!=y?function(){var t=c.backend.numDataIds();g=y.kernelFunc({inputs:e,attrs:o,backend:c.backend});var n=Array.isArray(g)?g:[g];c.shouldCheckForMemLeaks()&&c.checkKernelForMemLeak(p,t,n);var r=n.map(function(t){var e=t.dataId,n=t.shape,r=t.dtype;return c.makeTensorFromDataId(e,n,r)}),u=r.filter(function(t,e){return i[e]});return d(a.slice().concat(u)),r}:function(){var e=c.backend.numDataIds();g=c.tidy(function(){return t(c.backend,d)});var n=Array.isArray(g)?g:[g];return c.shouldCheckForMemLeaks()&&c.checkKernelForMemLeak(p,e,n),n},this.scopedRun(function(){return c.state.kernelDepth++},function(){return c.state.kernelDepth--},function(){u=c.ENV.getBool("DEBUG")?c.profiler.profileKernel(p,e,function(){return l()}):l();}),f&&this.addTapeNode(p,e,u,n,h),this.state.profiling&&this.state.activeProfile.kernels.push({name:p,bytesAdded:this.state.numBytes-v,totalBytesSnapshot:this.state.numBytes,tensorsAdded:this.state.numTensors-m,totalTensorsSnapshot:this.state.numTensors,inputShapes:Object.keys(e).map(function(t){return e[t].shape}),outputShapes:u.map(function(t){return t.shape})}),Array.isArray(g)?u:u[0]},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 o=t;"string"===n&&W(t[0])&&(o=t.map(function(t){return J(t)}));var a=r.write(o,e,n),i=new dt(e,n,a,this.nextTensorId());if(this.incRef(i,r),"string"===n){var u=this.state.tensorInfo.get(a),s=L(o);this.state.numBytes+=s-u.bytes,u.bytes=s;}return i},t.prototype.makeTensorFromDataId=function(t,e,n,r){var o=new dt(e,n=n||"float32",t,this.nextTensorId());return this.incRef(o,r),o},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 o=new bt(t,e,n,this.nextTensorId());if(null!=this.state.registeredVariables[o.name])throw new Error("Variable with name "+o.name+" was already registered");return this.state.registeredVariables[o.name]=o,this.incRef(o,this.backend),o},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*P(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 bt||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 n(this,void 0,void 0,function(){var e,n;return r(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,o){var a=this,i={id:this.state.nextTapeNodeId++,name:t,inputs:e,outputs:n,saved:o};null!=r&&(i.gradient=function(t){return t=t.map(function(t,e){if(null==t){var r=n[e],o=$(r.size,r.dtype);return a.makeTensor(o,r.shape,r.dtype)}return t}),r(t.length>1?t:t[0],o)}),this.state.activeTape.push(i);},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=kt(t),r=new Set(n.map(function(t){return t.id})),o=0;o0,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 o.startTape()},function(){return o.endTape()},function(){return o.tidy("forward",t)});g(a instanceof dt,function(){return "The result y returned by f() must be a tensor."});var i=function(t,e,n){for(var r={},o={},a=0;a=0;a--)for(i=(d=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,u={};u[a.id]=null==n?(t=a.shape,r=X(w(t),"float32"),Nt.makeTensor(r,t,"float32")):n,function(t,e,n){for(var r=function(r){var o=e[r],a=[];if(o.outputs.forEach(function(e){var n=t[e.id];null!=n?a.push(n):a.push(null);}),null==o.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+o.name+".");var i=o.gradient(a),u=function(e){if(!(e in i))throw new Error("Cannot backprop through input "+e+". Available gradients found: "+Object.keys(i)+".");var r=n(function(){return i[e]()});if("float32"!==r.dtype)throw new Error("Error in gradient for op "+o.name+". The gradient of input "+e+" must have 'float32' dtype, but has '"+r.dtype+"'");var a=o.inputs[e];if(!C(r.shape,a.shape))throw new Error("Error in gradient for op "+o.name+". 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 u=t[a.id];t[a.id]=u.add(r),u.dispose();}};for(var s in o.inputs)u(s);},o=e.length-1;o>=0;o--)r(o);}(u,i,function(t){return o.tidy(t)});var s=e.map(function(t){return u[t.id]});return 0===o.state.gradientDepth&&(o.state.activeTape.forEach(function(t){for(var e=0,n=t.saved;en||e>n){r="["+t+"x"+e+"]";throw new Error("Requested texture size "+r+" greater than WebGL maximum on this browser / GPU "+("["+n+"x"+n+"]")+".")}}function ce(t,e){return xe(t,e,function(){return t.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function le(t,e,n,r,o,a,i,u){var s=t.getAttribLocation(n,r);return -1!==s&&(qt(t,e,function(){return t.bindBuffer(t.ARRAY_BUFFER,o)}),qt(t,e,function(){return t.vertexAttribPointer(s,a,t.FLOAT,!1,i,u)}),qt(t,e,function(){return t.enableVertexAttribArray(s)}),!0)}function he(t,e,n,r){be(t,r),qt(t,e,function(){return t.activeTexture(t.TEXTURE0+r)}),qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,n)});}function fe(t,e,n,r){return xe(t,e,function(){return t.getUniformLocation(n,r)},'uniform "'+r+'" not present in program.')}function pe(t,e,n){return t.getUniformLocation(e,n)}function de(t,e,n,r,o,a){qt(t,e,function(){return he(t,e,r,a)}),qt(t,e,function(){return t.uniform1i(o,a)});}function ve(t,e,n,r){qt(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,r)}),qt(t,e,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,n,0)});}function me(t,e,n){qt(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,n)}),qt(t,e,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,null,0)});}function ge(t){var e=t.checkFramebufferStatus(t.FRAMEBUFFER);if(e!==t.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+ye(t,e))}function ye(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 xe(t,e,n,r){var o=qt(t,e,function(){return n()});if(null==o)throw new Error(r);return o}function be(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 we(t,e){return void 0===e&&(e=2),w(t.slice(0,t.length-e))}function Ce(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 Ee(t){var e=[1,1,1];return 0===t.length||1===t.length&&1===t[0]||(e=[we(t)].concat(Ce(t))),e}function Re(t,e){var n;void 0===e&&(e=!1);var r=a().getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e&&(r*=2,1===(t=t.map(function(e,n){return n>=t.length-2?v(t[n]):t[n]})).length&&(t=[2,t[0]])),2!==t.length){var o=T(t);t=o.newShape;}var i=w(t);if(t.length<=1&&i<=r)return [1,i];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 u=we(t),s=2,c=2;return t.length&&(s=(n=Ce(t))[0],c=n[1]),I(i=u*(s/2)*(c/2)).map(function(t){return 2*t})}return I(i)}function Ie(t){return t%2==0}function ke(t,e){if(C(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(Ie(n)&&Ie(r)&&(1===t[0]||1===e[0]))return !0}return t[1]===e[1]&&Ie(t[0])&&Ie(e[0])}function Se(t){if(null==Zt){var e=Ut(t);Zt=e.getParameter(e.MAX_TEXTURE_SIZE);}return Zt}function Ae(t){if(null==te){var e=Ut(t);te=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS);}return Math.min(16,te)}function De(t){if(0===t)return 0;var e=Ut(t);return Te(e,"EXT_disjoint_timer_query_webgl2")&&2===t?2:Te(e,"EXT_disjoint_timer_query")?1:0}function Te(t,e){return null!=t.getExtension(e)}function Ne(t){try{if(null!=Ut(t))return !0}catch(t){return !1}return !1}function Fe(t){if(0===t)return !1;var e=Ut(t);if(1===t){if(!Te(e,"OES_texture_float"))return !1}else if(!Te(e,"EXT_color_buffer_float"))return !1;return Oe(e)}function _e(t){if(0===t)return !1;var e=Ut(t);if(1!==t){if(Te(e,"EXT_color_buffer_float"))return Oe(e);if(Te(e,"EXT_color_buffer_half_float")){var n=e.getExtension("EXT_color_buffer_half_float");return function(t,e){var n=Ht(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 o=t.createFramebuffer();t.bindFramebuffer(t.FRAMEBUFFER,o),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(o),a}(e,n)}return !1}return !!Te(e,"OES_texture_float")&&(!!Te(e,"WEBGL_color_buffer_float")&&Oe(e))}function Oe(t){var e=Ht(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 o=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),o}function Me(t){return 2===t&&null!=Ut(t).fenceSync}var Be=Object.freeze({callAndCheck:qt,canBeRepresented:Xt,getWebGLErrorMessage:$t,getExtensionOrThrow:Yt,createVertexShader:Qt,createFragmentShader:Jt,createProgram:ne,linkProgram:re,validateProgram:oe,createStaticVertexBuffer:ae,createStaticIndexBuffer:ie,getNumChannels:function(){return 2===a().getNumber("WEBGL_VERSION")?1:4},createTexture:ue,validateTextureSize:se,createFramebuffer:ce,bindVertexBufferToProgramAttribute:le,bindTextureUnit:he,unbindTextureUnit:function(t,e,n){be(t,n),qt(t,e,function(){return t.activeTexture(t.TEXTURE0+n)}),qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,null)});},getProgramUniformLocationOrThrow:fe,getProgramUniformLocation:pe,bindTextureToProgramUniformSampler:de,bindCanvasToFramebuffer:function(t,e){qt(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,null)}),qt(t,e,function(){return t.viewport(0,0,t.canvas.width,t.canvas.height)}),qt(t,e,function(){return t.scissor(0,0,t.canvas.width,t.canvas.height)});},bindColorTextureToFramebuffer:ve,unbindColorTextureFromFramebuffer:me,validateFramebuffer:ge,getFramebufferErrorMessage:ye,getBatchDim:we,getRowsCols:Ce,getShapeAs3D:Ee,getTextureShapeFromLogicalShape:Re,isReshapeFree:ke,getWebGLMaxTextureSize:Se,resetMaxTextureSize:function(){Zt=null;},resetMaxTexturesInShader:function(){te=null;},getMaxTexturesInShader:Ae,getWebGLDisjointQueryTimerVersion:De,hasExtension:Te,isWebGLVersionEnabled:Ne,isCapableOfRenderingToFloatTexture:Fe,isDownloadFloatTextureEnabled:_e,isWebGLFenceEnabled:Me}),Pe=a();function Le(){a().set("PROD",!0);}function We(){a().set("DEBUG",!0);}function Ue(){a().set("DEPRECATION_WARNINGS_ENABLED",!1),console.warn("TensorFlow.js deprecation warnings have been disabled.");}function Ve(t){a().getBool("DEPRECATION_WARNINGS_ENABLED")&&console.warn(t+" You can disable deprecation warnings with tf.disableDeprecationWarnings().");}function ze(){Nt.disposeVariables();}function Ge(){return Nt}function He(){return Nt.memory()}function qe(t){return Nt.profile(t)}function Ke(t,e){return Nt.tidy(t,e)}function je(t){kt(t).forEach(function(t){return t.dispose()});}function Xe(t){return Nt.keep(t)}function $e(t){return Nt.time(t)}function Ye(t){return Nt.setBackend(t)}function Qe(){return Nt.ready()}function Je(){return Nt.backendName}function Ze(t){Nt.removeBackend(t);}function tn(t){return Nt.findBackend(t)}function en(t){return Nt.findBackendFactory(t)}function nn(t,e,n){return void 0===n&&(n=1),Nt.registerBackend(t,e,n)}function rn(){return Nt.backend}function on(t,e){a().setPlatform(t,e);}function an(){for(var t=[],e=0;e0,function(){return "Element arr["+r.join("][")+"] should be a primitive, but is an array of "+e.length+" elements"});g(e.length===n[0],function(){return "Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+e.length+" elements"});var o=n.slice(1);for(var a=0;a=0&&(o=r),sn(r,o,e,n),null==t||!B(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t&&"string"!=typeof t){var i=null==t?"null":t.constructor.name;throw new Error("Argument '"+e+"' passed to '"+n+"' must be a Tensor or TensorLike, but got '"+i+"'")}var u=un(t,o);B(t)||Array.isArray(t)||(t=[t]);var s="string"!==o?K(t,o,a().getBool("DEBUG")):b(t,[],!0);return Nt.makeTensor(s,u,o)}function ln(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 cn(t,e+"["+r+"]",n)},r)}function hn(t,e){for(var n=0;n=0&&e0}),Pe.registerFlag("WEBGL_VERSION",function(){return Ne(2)?2:Ne(1)?1:0}),Pe.registerFlag("WEBGL_BUFFER_SUPPORTED",function(){return 2===Pe.get("WEBGL_VERSION")}),Pe.registerFlag("WEBGL_CPU_FORWARD",function(){return !0}),Pe.registerFlag("WEBGL_FORCE_F16_TEXTURES",function(){return !1}),Pe.registerFlag("WEBGL_PACK",function(){return Pe.getBool("HAS_WEBGL")}),Pe.registerFlag("WEBGL_PACK_NORMALIZATION",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_PACK_CLIP",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_PACK_DEPTHWISECONV",function(){return !1}),Pe.registerFlag("WEBGL_PACK_BINARY_OPERATIONS",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_PACK_UNARY_OPERATIONS",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_PACK_ARRAY_OPERATIONS",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_PACK_IMAGE_OPERATIONS",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_PACK_REDUCE",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_LAZILY_UNPACK",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_CONV_IM2COL",function(){return Pe.getBool("WEBGL_PACK")}),Pe.registerFlag("WEBGL_MAX_TEXTURE_SIZE",function(){return Se(Pe.getNumber("WEBGL_VERSION"))}),Pe.registerFlag("WEBGL_MAX_TEXTURES_IN_SHADER",function(){return Ae(Pe.getNumber("WEBGL_VERSION"))}),Pe.registerFlag("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",function(){var t=Pe.getNumber("WEBGL_VERSION");return 0===t?0:De(t)}),Pe.registerFlag("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",function(){return Pe.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;}),Pe.registerFlag("WEBGL_RENDER_FLOAT32_CAPABLE",function(){return Fe(Pe.getNumber("WEBGL_VERSION"))}),Pe.registerFlag("WEBGL_RENDER_FLOAT32_ENABLED",function(){return !Pe.getBool("WEBGL_FORCE_F16_TEXTURES")&&Pe.getBool("WEBGL_RENDER_FLOAT32_CAPABLE")}),Pe.registerFlag("WEBGL_DOWNLOAD_FLOAT_ENABLED",function(){return _e(Pe.getNumber("WEBGL_VERSION"))}),Pe.registerFlag("WEBGL_FENCE_API_ENABLED",function(){return Me(Pe.getNumber("WEBGL_VERSION"))}),Pe.registerFlag("WEBGL_SIZE_UPLOAD_UNIFORM",function(){return Pe.getBool("WEBGL_RENDER_FLOAT32_ENABLED")?4:0}),pt=Ve;var Cn=wn({complex_:function(t,e){var n=cn(t,"real","complex"),r=cn(e,"imag","complex");return y(n.shape,r.shape,"real and imag shapes, "+n.shape+" and "+r.shape+", must match in call to tf.complex()."),Nt.runKernelFunc(function(t){return t.complex(n,r)},{$real:n,$imag:r})}}),En=wn({real_:function(t){var e=cn(t,"input","real");return Nt.runKernelFunc(function(t){return t.real(e)},{$input:e})}}),Rn=wn({imag_:function(t){var e=cn(t,"input","imag");return Nt.runKernelFunc(function(t){return t.imag(e)},{$input:e})}});function In(t,e,n){return kn(t,e,un(t,n),n)}function kn(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(!B(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){Q(e);var o=w(e),i=w(n);g(o===i,function(){return "Based on the provided shape, ["+e+"], the tensor should have "+o+" values but has "+i});for(var u=0;u1)return Bn([0],r);var o=$(Math.abs(Math.ceil((e-t)/n)),r);e=1,function(){return "Pass at least one tensor to concat"});var n=ln(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=D(e,n[0].shape)[0];var r=bn(n.map(function(t){return t.shape}),e);if(0===w(r))return In([],r);if(1===(n=n.filter(function(t){return t.size>0})).length)return n[0];var o=n.map(function(t){return t.shape});xn(o,e);var a=n,i={axis:e};return Nt.runKernelFunc(function(t){return t.concat(n,e)},a,function(t){var n=o.map(function(t){return t[e]});return jn(t,n,e).map(function(t){return function(){return t}})},"Concat",i)}}),Gn=wn({concat1d_:function(t){return zn(t,0)}}),Hn=wn({concat2d_:function(t,e){return zn(t,e)}}),qn=wn({concat3d_:function(t,e){return zn(t,e)}}),Kn=wn({concat4d_:function(t,e){return zn(t,e)}}),jn=wn({split_:function(t,e,n){void 0===n&&(n=0);var r,o=cn(t,"x","split");return n=D(n,o.shape)[0],"number"==typeof e?(g(o.shape[n]%e==0,function(){return "Number of splits must evenly divide the axis."}),r=new Array(e).fill(o.shape[n]/e)):(g(o.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),Nt.runKernelFunc(function(t){return t.split(o,r,n)},{$x:o},function(t){return {$x:function(){return zn(t,n)}}})}});function Xn(t,e){return t(e={exports:{}},e.exports),e.exports}var $n=Xn(function(t){!function(t,e,n){function r(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;}function o(t,e){return e.c=t.c,e.s0=t.s0,e.s1=t.s1,e.s2=t.s2,e}function a(t,e){var n=new r(t),a=e&&e.state,i=n.next;return i.int32=function(){return 4294967296*n.next()|0},i.double=function(){return i()+1.1102230246251565e-16*(2097152*i()|0)},i.quick=i,a&&("object"==typeof a&&o(a,n),i.state=function(){return o(n,{})}),i}e&&e.exports?e.exports=a:n&&n.amd?n(function(){return a}):this.alea=a;}(0,t,!1);}),Yn=Xn(function(t){!function(t,e,n){function r(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 i.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===t);return t},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&o(a,n),i.state=function(){return o(n,{})}),i}e&&e.exports?e.exports=a:n&&n.amd?n(function(){return a}):this.xor128=a;}(0,t,!1);}),Qn=Xn(function(t){!function(t,e,n){function r(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();}function o(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 a(t,e){var n=new r(t),a=e&&e.state,i=function(){return (n.next()>>>0)/4294967296};return i.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===t);return t},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&o(a,n),i.state=function(){return o(n,{})}),i}e&&e.exports?e.exports=a:n&&n.amd?n(function(){return a}):this.xorwow=a;}(0,t,!1);}),Jn=Xn(function(t){!function(t,e,n){function r(t){var e=this;e.next=function(){var t,n,r=e.x,o=e.i;return t=r[o],n=(t^=t>>>7)^t<<24,n^=(t=r[o+1&7])^t>>>10,n^=(t=r[o+3&7])^t>>>3,n^=(t=r[o+4&7])^t<<7,t=r[o+7&7],n^=(t^=t<<13)^t<<9,r[o]=n,e.i=o+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);}function o(t,e){return e.x=t.x.slice(),e.i=t.i,e}function a(t,e){null==t&&(t=+new Date);var n=new r(t),a=e&&e.state,i=function(){return (n.next()>>>0)/4294967296};return i.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===t);return t},i.int32=n.next,i.quick=i,a&&(a.x&&o(a,n),i.state=function(){return o(n,{})}),i}e&&e.exports?e.exports=a:n&&n.amd?n(function(){return a}):this.xorshift7=a;}(0,t,!1);}),Zn=Xn(function(t){!function(t,e,n){function r(t){var e=this;e.next=function(){var t,n,r=e.w,o=e.X,a=e.i;return e.w=r=r+1640531527|0,n=o[a+34&127],t=o[a=a+1&127],n^=n<<13,t^=t<<17,n^=n>>>15,t^=t>>>12,n=o[a]=n^t,e.i=a,n+(r^r>>>16)|0},function(t,e){var n,r,o,a,i,u=[],s=128;for(e===(0|e)?(r=e,e=null):(e+="\0",r=0,s=Math.max(s,e.length)),o=0,a=-32;a>>15,r^=r<<4,r^=r>>>13,a>=0&&(i=i+1640531527|0,o=0==(n=u[127&a]^=r+i)?o+1:0);for(o>=128&&(u[127&(e&&e.length||0)]=-1),o=127,a=512;a>0;--a)r=u[o+34&127],n=u[o=o+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,u[o]=r^n;t.w=i,t.X=u,t.i=o;}(e,t);}function o(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function a(t,e){null==t&&(t=+new Date);var n=new r(t),a=e&&e.state,i=function(){return (n.next()>>>0)/4294967296};return i.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===t);return t},i.int32=n.next,i.quick=i,a&&(a.X&&o(a,n),i.state=function(){return o(n,{})}),i}e&&e.exports?e.exports=a:n&&n.amd?n(function(){return a}):this.xor4096=a;}(0,t,!1);}),tr=Xn(function(t){!function(t,e,n){function r(t){var e=this,n="";e.next=function(){var t=e.b,n=e.c,r=e.d,o=e.a;return t=t<<25^t>>>7^n,n=n-r|0,r=r<<24^r>>>8^o,o=o-t|0,e.b=t=t<<20^t>>>12^n,e.c=n=n-r|0,e.d=r<<16^n>>>16^o,e.a=o-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 i.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21);}while(0===t);return t},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&o(a,n),i.state=function(){return o(n,{})}),i}e&&e.exports?e.exports=a:n&&n.amd?n(function(){return a}):this.tychei=a;}(0,t,!1);}),er=Xn(function(t){!function(e,n){var r,o=this,a=256,i=6,u="random",s=n.pow(a,i),c=n.pow(2,52),l=2*c,h=a-1;function f(t,h,f){var g=[],y=v(function t(e,n){var r,o=[],a=typeof e;if(n&&"object"==a)for(r in e)try{o.push(t(e[r],n-1));}catch(t){}return o.length?o:"string"==a?e:e+"\0"}((h=1==h?{entropy:!0}:h||{}).entropy?[t,m(e)]:null==t?function(){try{var t;return r&&(t=r.randomBytes)?t=t(a):(t=new Uint8Array(a),(o.crypto||o.msCrypto).getRandomValues(t)),m(t)}catch(t){var n=o.navigator,i=n&&n.plugins;return [+new Date,o,i,o.screen,m(e)]}}():t,3),g),x=new p(g),b=function(){for(var t=x.g(i),e=s,n=0;t=l;)t/=2,e/=2,n>>>=1;return (t+n)/e};return b.int32=function(){return 0|x.g(4)},b.quick=function(){return x.g(4)/4294967296},b.double=b,v(m(x.S),e),(h.pass||f||function(t,e,r,o){return o&&(o.S&&d(o,x),t.state=function(){return d(x,{})}),r?(n[u]=t,e):t})(b,y,"global"in h?h.global:this==n,h.state)}function p(t){var e,n=t.length,r=this,o=0,i=r.i=r.j=0,u=r.S=[];for(n||(t=[n++]);o=1||0===i);var u=Math.sqrt(-2*Math.log(i)/i);e=this.mean+this.stdDev*o*u,n=this.mean+this.stdDev*a*u,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}(),or=function(){function t(t,e,n,r){this.alpha=t,this.beta=1/e,this.dtype=n;var o=r||Math.random();this.randu=nr(o.toString()),this.randn=new rr(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,o,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)),(o=this.randu())=1+e.length,function(){return "input rank is "+r.rank+" but should be > than blockShape.length "+e.length}),g(n.length===e.length,function(){return "crops.length is "+n.length+" but should be equal to blockShape.length "+e.length}),g(r.shape[0]%o==0,function(){return "input tensor batch is "+r.shape[0]+" but is not divisible by the product of the elements of blockShape "+e.join(" * ")+" === "+o}),Nt.runKernelFunc(function(t){return t.batchToSpaceND(r,e,n)},{$x:r},function(t){return {$x:function(){return t.spaceToBatchND(e,n)}}})}}),cr=wn({broadcastTo_:function(t,e){var n=cn(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 o=n.shape.slice();o.length=0;i--)if(n.shape[i]===e[i])a[i]=1;else if(1!==n.shape[i])throw new Error("broadcastTo(): ["+r+"] cannot be broadcast to ["+e+"].");var u=a.map(function(t,e){return t>1?e:-1}).filter(function(t){return t>=0});return 0===u.length?n.clone():Nt.runKernelFunc(function(t){return t.tile(n,a)},{input:n},function(t){return {input:function(){return t.sum(u,!0)}}})}}),lr=wn({cast_:function(t,e){var n=cn(t,"x","cast");if(!O(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 Nt.runKernelFunc(function(t){return t.cast(n,e)},{x:n},function(t){return {x:function(){return t.clone()}}},"Cast",r)}}),hr=wn({clone_:function(t){var e=cn(t,"x","clone",null);return Nt.runKernelFunc(function(){return Nt.makeTensorFromDataId(e.dataId,e.shape,e.dtype)},{$x:e},function(t){return {$x:function(){return t.toFloat()}}})}}),fr=wn({cumsum_:function(t,e,n,r){void 0===e&&(e=0),void 0===n&&(n=!1),void 0===r&&(r=!1);var o=cn(t,"x","cumsum"),a=mn([e|=0],o.rank),i=o;null!=a&&(i=o.transpose(a));var u=yn(1,o.rank)[0],s=Nt.runKernelFunc(function(t){return t.cumsum(i,u,n,r)},{permutedX:i},function(t){return {permutedX:function(){return t.cumsum(e,n,!r)}}});return null!=a&&(s=s.transpose(a)),s}}),pr=wn({depthToSpace_:function(t,e,n){void 0===n&&(n="NHWC");var r=cn(t,"x","depthToSpace"),o="NHWC"===n?r.shape[1]:r.shape[2],a="NHWC"===n?r.shape[2]:r.shape[3],i="NHWC"===n?r.shape[3]:r.shape[1];return g(o*e>=0,function(){return "Negative dimension size caused by overflow when multiplying\n "+o+" and "+e+" for depthToSpace with input shape\n "+r.shape}),g(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}),g(i%(e*e)==0,function(){return "Dimension size must be evenly divisible by "+e*e+" but is "+i+" for depthToSpace with input shape "+r.shape}),Nt.runKernelFunc(function(t){return t.depthToSpace(r,e,n)},{$x:r})}}),dr=wn({expandDims_:function(t,e){void 0===e&&(e=0);var n=cn(t,"x","expandDims",null);g(e<=n.rank,function(){return "Axis must be <= rank of the tensor"});var r=n.shape.slice();return e<0&&(g(-(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),Sr(n,r)}}),vr=wn({eye_:function(t,e,n,r){void 0===r&&(r="float32"),null==e&&(e=t);for(var o=ir([t,e],r),a=t<=e?t:e,i=0;i2)throw new Error("Rank of probabilities must be 1 or 2, but is "+i);n=n||Math.random();var u=1===i?o.as2D(1,-1):o,s=Nt.runKernelFunc(function(t){return t.multinomial(u,r,e,n)},{logits2D:u});return 1===i?s.as1D():s}}),gr=wn({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 o=cn(t,"indices","oneHot","int32"),a=o.shape.concat([e]);return o=o.flatten(),Nt.runKernelFunc(function(t){return t.oneHot(o,e,n,r)},{$indices:o},function(t){return {$indices:function(){return Bn(o.shape,"float32")}}}).reshape(a)}}),yr=wn({pad_:function(t,e,n){void 0===n&&(n=0);var r=cn(t,"x","pad");if(0===r.rank)throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");var o={paddings:e,constantValue:n};return Nt.runKernelFunc(function(t){return t.pad(r,e,n)},{x:r},function(t){var n=e.map(function(t){return t[0]});return {x:function(){return t.slice(n,r.shape)}}},"PadV2",o)}}),xr=wn({pad1d_:function(t,e,n){return void 0===n&&(n=0),g(2===e.length,function(){return "Invalid number of paddings. Must be length of 2."}),yr(t,[e],n)}}),br=wn({pad2d_:function(t,e,n){return void 0===n&&(n=0),g(2===e.length&&2===e[0].length&&2===e[1].length,function(){return "Invalid number of paddings. Must be length of 2 each."}),yr(t,e,n)}}),wr=wn({pad3d_:function(t,e,n){return void 0===n&&(n=0),g(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."}),yr(t,e,n)}}),Cr=wn({pad4d_:function(t,e,n){return void 0===n&&(n=0),g(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."}),yr(t,e,n)}}),Er=wn({rand_:function(t,e,n){var r=w(t),o=null;if(null==n||"float32"===n)o=new Float32Array(r);else if("int32"===n)o=new Int32Array(r);else{if("bool"!==n)throw new Error("Unknown data type "+n);o=new Uint8Array(r);}for(var a=0;a=1+e.length,function(){return "input rank "+r.rank+" should be > than [blockShape] "+e.length}),g(n.length===e.length,function(){return "paddings.shape[0] "+n.length+" must be equal to [blockShape] "+e.length}),g(r.shape.reduce(function(t,r,o){return o>0&&o<=e.length?t&&(r+n[o-1][0]+n[o-1][1])%e[o-1]==0:t},!0),function(){return "input spatial dimensions "+r.shape.slice(1)+" with paddings "+n.toString()+" must be divisible by blockShapes "+e.toString()}),Nt.runKernelFunc(function(t){return t.spaceToBatchND(r,e,n)},{$x:r},function(t){return {$x:function(){return t.batchToSpaceND(e,n)}}})}}),Dr=wn({squeeze_:function(t,e){var n=cn(t,"x","squeeze");return Sr(n,T(n.shape,e).newShape)}}),Tr=wn({stack_:function(t,e){void 0===e&&(e=0);var n=ln(t,"tensors","stack");if(g(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,o=n[0].shape,a=n[0].dtype;g(e<=r,function(){return "Axis must be <= rank of the tensor"}),n.forEach(function(t){y(o,t.shape,"All tensors passed to stack must have matching shapes");}),n.forEach(function(t){g(a===t.dtype,function(){return "All tensors passed to stack must have matching dtypes"});});var i=n.map(function(t){return t.expandDims(e)});return zn(i,e)}}),Nr=wn({tile_:function(t,e){var n=cn(t,"x","tile",null);return g(n.rank===e.length,function(){return "Error in transpose: rank of input "+n.rank+" must match length of reps "+e+"."}),Nt.runKernelFunc(function(t,r){var o=t.tile(n,e);return r([n]),o},{$x:n},function(t,n){var r=n[0];return {$x:function(){var n=Vn(r);if(1===r.rank)for(var o=0;o=-n.shape.length&&e=2*e+1||o%2==1?i.push(o):a.push(o);r.push.apply(r,a),r.push(0),r.push.apply(r,i);}return r}function Pr(t,e,n,r){void 0===r&&(r=!0);var o=[];r?o.push(t[0]/n):o.push(t[0]*n);for(var a=1;at.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],o=1,a=0;a1?e.shape[e.rank-1]:1,o=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: "+o+".";if(n.rank1?e.shape[e.rank-1]:1,o=n.length,a=1,i=r;i0;)1&t&&e.push(n),t/=2,n++;return e}function jr(t,e,n){for(var r=[],o=0;o0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var u=r[o];return a<0&&(a+=u),a=d(0,a,u-1)}function $r(t,e,n,r,o){var a=e[o],i=n[o]||1;(t&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var u=r[o];return a<0&&(a+=u),a=i>0?d(0,a,u):d(-1,a,u-1)}function Yr(t,e,n){for(var r=n.length,o=0;o1){r=o;break}for(o=r+1;o0||n[o]!==t[o])return !1;return !0}function Qr(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 i=Nt.gradients(t,e,null,!0),u=i.value,s=i.grads;g(s.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()."}),g(0===u.rank,function(){return "The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+u.rank+" tensor"});var c={};return e.forEach(function(t,e){null!=s[e]&&(c[t.name]=s[e]);}),null!=o&&o.forEach(function(t){return c[t.name]=null}),{value:u,grads:c}}function oo(t){return Nt.customGrad(t)}function ao(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 io=wn({softmax_:function(t,e){void 0===e&&(e=-1);var n=cn(t,"logits","softmax");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 oo(function(t,n){var r=t.logSumExp([e],!0),o=t.toFloat().sub(r).exp();return n([o]),{value:o,gradFunc:function(t,n){var r=n[0],o=t.mul(r);return o.sub(o.sum([e],!0).mul(r))}}})(n)}}),uo=wn({logSoftmax_:function(t,e){void 0===e&&(e=-1);var n=cn(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 oo(function(t,n){var r=t.max(e,!0),o=t.sub(r),a=o.toFloat().sub(o.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)}}),so=function(){function t(t,e){this.backend=t,this.dataMover=e,this.data=new WeakMap,this.dataIdsCount=0;}return t.prototype.get=function(t){return this.data.has(t)||this.dataMover.moveData(this.backend,t),this.data.get(t)},t.prototype.set=function(t,e){this.dataIdsCount++,this.data.set(t,e);},t.prototype.has=function(t){return this.data.has(t)},t.prototype.delete=function(t){return this.dataIdsCount--,this.data.delete(t)},t.prototype.numDataIds=function(){return this.dataIdsCount},t}(),co=function(){function t(){}return t.prototype.time=function(t){return lo("time")},t.prototype.read=function(t){return lo("read")},t.prototype.readSync=function(t){return lo("readSync")},t.prototype.numDataIds=function(){return lo("numDataIds")},t.prototype.disposeData=function(t){return lo("disposeData")},t.prototype.write=function(t,e,n){return lo("write")},t.prototype.move=function(t,e,n,r){return lo("move")},t.prototype.memory=function(){return lo("memory")},t.prototype.floatPrecision=function(){return lo("floatPrecision")},t.prototype.epsilon=function(){return 32===this.floatPrecision()?1e-7:1e-4},t.prototype.batchMatMul=function(t,e,n,r){return lo("batchMatMul")},t.prototype.fusedBatchMatMul=function(t){t.a,t.b,t.transposeA,t.transposeB,t.bias,t.activation,t.preluActivationWeights;return lo("fusedBatchMatMul")},t.prototype.slice=function(t,e,n){return lo("slice")},t.prototype.stridedSlice=function(t,e,n,r){return lo("stridedSlice")},t.prototype.unstack=function(t,e){return lo("unstack")},t.prototype.reverse=function(t,e){return lo("reverse")},t.prototype.concat=function(t,e){return lo("concat")},t.prototype.neg=function(t){return lo("neg")},t.prototype.add=function(t,e){return lo("add")},t.prototype.addN=function(t){return lo("addN")},t.prototype.subtract=function(t,e){return lo("subtract")},t.prototype.multiply=function(t,e){return lo("multiply")},t.prototype.realDivide=function(t,e){return lo("realDivide")},t.prototype.floorDiv=function(t,e){return lo("floorDiv")},t.prototype.sum=function(t,e){return lo("sum")},t.prototype.prod=function(t,e){return lo("prod")},t.prototype.unsortedSegmentSum=function(t,e,n){return lo("unsortedSegmentSum")},t.prototype.argMin=function(t,e){return lo("argMin")},t.prototype.argMax=function(t,e){return lo("argMax")},t.prototype.equal=function(t,e){return lo("equal")},t.prototype.notEqual=function(t,e){return lo("notEqual")},t.prototype.less=function(t,e){return lo("less")},t.prototype.lessEqual=function(t,e){return lo("lessEqual")},t.prototype.greater=function(t,e){return lo("greater")},t.prototype.greaterEqual=function(t,e){return lo("greaterEqual")},t.prototype.logicalNot=function(t){return lo("logicalNot")},t.prototype.logicalAnd=function(t,e){return lo("logicalAnd")},t.prototype.logicalOr=function(t,e){return lo("logicalOr")},t.prototype.where=function(t){return lo("where")},t.prototype.select=function(t,e,n){return lo("select")},t.prototype.topk=function(t,e,n){return lo("topk")},t.prototype.min=function(t,e){return lo("min")},t.prototype.minimum=function(t,e){return lo("minimum")},t.prototype.mod=function(t,e){return lo("mod")},t.prototype.max=function(t,e){return lo("max")},t.prototype.maximum=function(t,e){return lo("maximum")},t.prototype.all=function(t,e){return lo("all")},t.prototype.any=function(t,e){return lo("any")},t.prototype.squaredDifference=function(t,e){return lo("squaredDifference")},t.prototype.ceil=function(t){return lo("ceil")},t.prototype.floor=function(t){return lo("floor")},t.prototype.round=function(t){return lo("round")},t.prototype.sign=function(t){return lo("sign")},t.prototype.isNaN=function(t){return lo("isNaN")},t.prototype.isInf=function(t){return lo("isInf")},t.prototype.isFinite=function(t){return lo("isFinite")},t.prototype.pow=function(t,e){return lo("pow")},t.prototype.exp=function(t){return lo("exp")},t.prototype.expm1=function(t){return lo("expm1")},t.prototype.log=function(t){return lo("log")},t.prototype.log1p=function(t){return lo("log1p")},t.prototype.sqrt=function(t){return lo("sqrt")},t.prototype.rsqrt=function(t){return lo("rsqrt")},t.prototype.square=function(t){return lo("square")},t.prototype.reciprocal=function(t){return lo("reciprocal")},t.prototype.relu=function(t){return lo("relu")},t.prototype.relu6=function(t){return lo("relu6")},t.prototype.prelu=function(t,e){return lo("prelu")},t.prototype.elu=function(t){return lo("elu")},t.prototype.eluDer=function(t,e){return lo("eluDer")},t.prototype.selu=function(t){return lo("selu")},t.prototype.int=function(t){return lo("int")},t.prototype.clip=function(t,e,n){return lo("clip")},t.prototype.abs=function(t){return lo("abs")},t.prototype.complexAbs=function(t){return lo("complexAbs")},t.prototype.sigmoid=function(t){return lo("sigmoid")},t.prototype.softplus=function(t){return lo("softplus")},t.prototype.sin=function(t){return lo("sin")},t.prototype.cos=function(t){return lo("cos")},t.prototype.tan=function(t){return lo("tan")},t.prototype.asin=function(t){return lo("asin")},t.prototype.acos=function(t){return lo("acos")},t.prototype.atan=function(t){return lo("atan")},t.prototype.atan2=function(t,e){return lo("atan2")},t.prototype.sinh=function(t){return lo("sinh")},t.prototype.cosh=function(t){return lo("cosh")},t.prototype.tanh=function(t){return lo("tanh")},t.prototype.asinh=function(t){return lo("asinh")},t.prototype.acosh=function(t){return lo("acosh")},t.prototype.atanh=function(t){return lo("atanh")},t.prototype.erf=function(t){return lo("erf")},t.prototype.step=function(t,e){return lo("step")},t.prototype.fusedConv2d=function(t){t.input,t.filter,t.convInfo,t.bias,t.activation,t.preluActivationWeights;return lo("fusedConv2d")},t.prototype.conv2d=function(t,e,n){return lo("conv2d")},t.prototype.conv2dDerInput=function(t,e,n){return lo("conv2dDerInput")},t.prototype.conv2dDerFilter=function(t,e,n){return lo("conv2dDerFilter")},t.prototype.fusedDepthwiseConv2D=function(t){t.input,t.filter,t.convInfo,t.bias,t.activation,t.preluActivationWeights;return lo("fusedDepthwiseConv2D")},t.prototype.depthwiseConv2D=function(t,e,n){return lo("depthwiseConv2D")},t.prototype.depthwiseConv2DDerInput=function(t,e,n){return lo("depthwiseConv2DDerInput")},t.prototype.depthwiseConv2DDerFilter=function(t,e,n){return lo("depthwiseConv2DDerFilter")},t.prototype.conv3d=function(t,e,n){return lo("conv3d")},t.prototype.conv3dDerInput=function(t,e,n){return lo("conv3dDerInput")},t.prototype.conv3dDerFilter=function(t,e,n){return lo("conv3dDerFilter")},t.prototype.maxPool=function(t,e){return lo("maxPool")},t.prototype.maxPoolBackprop=function(t,e,n,r){return lo("maxPoolBackprop")},t.prototype.avgPool=function(t,e){return lo("avgPool")},t.prototype.avgPoolBackprop=function(t,e,n){return lo("avgPoolBackprop")},t.prototype.avgPool3d=function(t,e){return lo("avgPool3d")},t.prototype.avgPool3dBackprop=function(t,e,n){return lo("avgPool3dBackprop")},t.prototype.maxPool3d=function(t,e){return lo("maxPool3d")},t.prototype.maxPool3dBackprop=function(t,e,n,r){return lo("maxPool3dBackprop")},t.prototype.reshape=function(t,e){return lo("reshape")},t.prototype.cast=function(t,e){return lo("cast")},t.prototype.tile=function(t,e){return lo("tile")},t.prototype.pad=function(t,e,n){return lo("pad")},t.prototype.transpose=function(t,e){return lo("transpose")},t.prototype.gather=function(t,e,n){return lo("gather")},t.prototype.gatherND=function(t,e){return lo("gatherND")},t.prototype.scatterND=function(t,e,n){return lo("scatterND")},t.prototype.batchToSpaceND=function(t,e,n){return lo("batchToSpaceND")},t.prototype.spaceToBatchND=function(t,e,n){return lo("spaceToBatchND")},t.prototype.resizeBilinear=function(t,e,n,r){return lo("resizeBilinear")},t.prototype.resizeBilinearBackprop=function(t,e,n){return lo("resizeBilinearBackprop")},t.prototype.resizeNearestNeighbor=function(t,e,n,r){return lo("resizeNearestNeighbor")},t.prototype.resizeNearestNeighborBackprop=function(t,e,n){return lo("resizeNearestNeighborBackprop")},t.prototype.batchNormalization=function(t,e,n,r,o,a){return lo("batchNormalization")},t.prototype.localResponseNormalization4D=function(t,e,n,r,o){return lo("localResponseNormalization4D")},t.prototype.LRNGrad=function(t,e,n,r,o,a,i){return lo("LRNGrad")},t.prototype.multinomial=function(t,e,n,r){return lo("multinomial")},t.prototype.oneHot=function(t,e,n,r){return lo("oneHot")},t.prototype.cumsum=function(t,e,n,r){return lo("cumsum")},t.prototype.nonMaxSuppression=function(t,e,n,r,o){return lo("nonMaxSuppression")},t.prototype.fft=function(t){return lo("fft")},t.prototype.ifft=function(t){return lo("ifft")},t.prototype.complex=function(t,e){return lo("complex")},t.prototype.real=function(t){return lo("real")},t.prototype.imag=function(t){return lo("imag")},t.prototype.cropAndResize=function(t,e,n,r,o,a){return lo("cropAndResize")},t.prototype.depthToSpace=function(t,e,n){return lo("depthToSpace")},t.prototype.split=function(t,e,n){return lo("split")},t.prototype.sparseToDense=function(t,e,n,r){return lo("sparseToDense")},t.prototype.diag=function(t){return lo("diag")},t.prototype.fill=function(t,e,n){return lo("fill")},t.prototype.onesLike=function(t){return lo("onesLike")},t.prototype.zerosLike=function(t){return lo("zerosLike")},t.prototype.linspace=function(t,e,n){return lo("linspace")},t.prototype.dispose=function(){return lo("dispose")},t}();function lo(t){throw new Error("'"+t+"' not yet implemented or not found in the registry. Did you forget to import the kernel?")}function ho(t,e){for(var n=t.length,r=[],o=0;o1&&1===i&&r.unshift(a);}return r}function fo(t,e){for(var n=[],r=0;r1)&&n.unshift(a);}return n}function po(t,e){for(var n=[],r=Math.max(t.length,e.length),o=0;oo}).sort(function(t,e){return e.score-t.score}),i=[],u=0;u=0;--f){if(Bo(t,l,i[f])>=r){h=!0;break}}if(!h&&(i.push(l),i.length>=n))break}return An(i,"int32")}function Bo(t,e,n){var r=t.subarray(4*e,4*e+4),o=t.subarray(4*n,4*n+4),a=Math.min(r[0],r[2]),i=Math.min(r[1],r[3]),u=Math.max(r[0],r[2]),s=Math.max(r[1],r[3]),c=Math.min(o[0],o[2]),l=Math.min(o[1],o[3]),h=Math.max(o[0],o[2]),f=Math.max(o[1],o[3]),p=(u-a)*(s-i),d=(h-c)*(f-l);if(p<=0||d<=0)return 0;var v=Math.max(a,c),m=Math.max(i,l),g=Math.min(u,h),y=Math.min(s,f),x=Math.max(g-v,0)*Math.max(y-m,0);return x/(p+d-x)}function Po(t,e,n){var r=new Array(t.rank).fill(0),o=t.shape.slice();return e.map(function(e){o[n]=e;var a=t.slice(r,o);return r[n]+=e,a})}function Lo(t,e){for(var n=new Array(t.rank),r=0;r":"<",s=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 = "+s+";\n float candidate = getA(batch, inIdx);\n if (candidate "+u+" bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n ";}}();function Ho(t,e){return ["x","y","z","w","u","v"].slice(0,e).map(function(e){return t+"."+e})}function qo(t,e){return 1===e?[t]:Ho(t,e)}function Ko(){var t,e,n,r,o,i,u,s,c,l;return 2===a().getNumber("WEBGL_VERSION")?(t="#version 300 es",e="in",n="out",r="in",o="texture",i="outputColor",u="out vec4 outputColor;",s="\n bool isnan_custom(float val) {\n return (val > 0.0 || val < 0.0) ? false : val != 0.0;\n }\n\n bvec4 isnan_custom(vec4 val) {\n return bvec4(isnan_custom(val.x),\n isnan_custom(val.y), isnan_custom(val.z), isnan_custom(val.w));\n }\n\n #define isnan(value) isnan_custom(value)\n ",c="",l="\n #define round(value) newRound(value)\n int newRound(float value) {\n return int(floor(value + 0.5));\n }\n\n ivec4 newRound(vec4 value) {\n return ivec4(floor(value + vec4(0.5)));\n }\n "):(t="",e="attribute",n="varying",r="varying",o="texture2D",i="gl_FragColor",u="",s="\n #define isnan(value) isnan_custom(value)\n bool isnan_custom(float val) {\n return (val > 0. || val < 1. || val == 0.) ? false : true;\n }\n bvec4 isnan_custom(vec4 val) {\n return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));\n }\n ",c="\n uniform float INFINITY;\n\n bool isinf(float val) {\n return abs(val) == INFINITY;\n }\n bvec4 isinf(vec4 val) {\n return equal(abs(val), vec4(INFINITY));\n }\n ",l="\n int round(float value) {\n return int(floor(value + 0.5));\n }\n\n ivec4 round(vec4 value) {\n return ivec4(floor(value + vec4(0.5)));\n }\n "),{version:t,attribute:e,varyingVs:n,varyingFs:r,texture2D:o,output:i,defineOutput:u,defineSpecialNaN:s,defineSpecialInf:c,defineRound:l}}function jo(t,e,n){void 0===n&&(n="index");var r=q(e);return r.map(function(e,o){return "int "+t[o]+" = "+n+" / "+e+"; "+(o===r.length-1?"int "+t[o+1]+" = "+n+" - "+t[o]+" * "+e:"index -= "+t[o]+" * "+e)+";"}).join("")}function Xo(t){var e=q(t).map(function(t){return t.toString()});return "\n int getFlatIndex(ivec3 coords) {\n return coords.x * "+e[0]+" + coords.y * "+e[1]+" + coords.z;\n }\n"}var $o="\n const float FLOAT_MAX = 1.70141184e38;\n const float FLOAT_MIN = 1.17549435e-38;\n\n lowp vec4 encode_float(highp float v) {\n if (isnan(v)) {\n return vec4(255, 255, 255, 255);\n }\n\n highp float av = abs(v);\n\n if(av < FLOAT_MIN) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n } else if(v > FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;\n } else if(v < -FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;\n }\n\n highp vec4 c = vec4(0,0,0,0);\n\n highp float e = floor(log2(av));\n highp float m = exp2(fract(log2(av))) - 1.0;\n\n c[2] = floor(128.0 * m);\n m -= c[2] / 128.0;\n c[1] = floor(32768.0 * m);\n m -= c[1] / 32768.0;\n c[0] = floor(8388608.0 * m);\n\n highp float ebias = e + 127.0;\n c[3] = floor(ebias / 2.0);\n ebias -= c[3] * 2.0;\n c[2] += floor(ebias) * 128.0;\n\n c[3] += 128.0 * step(0.0, -v);\n\n return c / 255.0;\n }\n";function Yo(t,e,n,r){var o=[];t.forEach(function(t){var e=w(t.shapeInfo.logicalShape);t.shapeInfo.isUniform?o.push("uniform float "+t.name+(e>1?"["+e+"]":"")+";"):(o.push("uniform sampler2D "+t.name+";"),o.push("uniform int offset"+t.name+";"));});var a,i,u=o.join("\n"),s=t.map(function(t){return function(t,e,n){void 0===n&&(n=!1);var r="";r+=n?Jo(t):Qo(t);var o=t.shapeInfo.logicalShape,a=e.logicalShape;o.length<=a.length&&(r+=n?function(t,e){var n,r=t.name,o=r.charAt(0).toUpperCase()+r.slice(1),a="get"+o+"AtOutCoords",i=t.shapeInfo.logicalShape.length,u=e.logicalShape.length,s=ho(t.shapeInfo.logicalShape,e.logicalShape),c=aa(u),l=u-i,h=["x","y","z","w","u","v"];n=0===i?"":u<2&&s.length>=1?"coords = 0;":s.map(function(t){return "coords."+h[t+l]+" = 0;"}).join("\n");var f="";f=u<2&&i>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return "coords."+h[e+l]}).join(", ");var p="return outputValue;",d=1===w(t.shapeInfo.logicalShape),v=1===w(e.logicalShape);if(1!==i||d||v){if(d&&!v)p=1===u?"\n return vec4(outputValue.x, outputValue.x, 0., 0.);\n ":"\n return vec4(outputValue.x);\n ";else if(s.length){var m=i-2,g=i-1;s.indexOf(m)>-1&&s.indexOf(g)>-1?p="return vec4(outputValue.x);":s.indexOf(m)>-1?p="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":s.indexOf(g)>-1&&(p="return vec4(outputValue.xx, outputValue.zz);");}}else p="\n return vec4(outputValue.xy, outputValue.xy);\n ";return "\n vec4 "+a+"() {\n "+c+" coords = getOutputCoords();\n "+n+"\n vec4 outputValue = get"+o+"("+f+");\n "+p+"\n }\n "}(t,e):function(t,e){var n=t.name,r=n.charAt(0).toUpperCase()+n.slice(1),o="get"+r+"AtOutCoords",a=e.texShape,i=t.shapeInfo.texShape,u=t.shapeInfo.logicalShape.length,s=e.logicalShape.length;if(!t.shapeInfo.isUniform&&u===s&&null==t.shapeInfo.flatOffset&&C(i,a))return "\n float "+o+"() {\n return sampleTexture("+n+", resultUV);\n }\n ";var c,l=aa(s),h=ho(t.shapeInfo.logicalShape,e.logicalShape),f=s-u,p=["x","y","z","w","u","v"];c=0===u?"":s<2&&h.length>=1?"coords = 0;":h.map(function(t){return "coords."+p[t+f]+" = 0;"}).join("\n");var d="";d=s<2&&u>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return "coords."+p[e+f]}).join(", ");return "\n float "+o+"() {\n "+l+" coords = getOutputCoords();\n "+c+"\n return get"+r+"("+d+");\n }\n "}(t,e));return r}(t,e,r)}).join("\n"),c=e.texShape,l=Ko(),h=function(t){return "\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return "+t.texture2D+"(textureSampler, uv).r;\n }\n "}(l),f=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 "+Zo+"\n "+ta+"\n "+ea+"\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)];if(1===n[0])return "\n int getOutputCoords() {\n return 2 * int(resultUV.x * "+n[1]+".0);\n }\n ";if(1===n[1])return "\n int getOutputCoords() {\n return 2 * int(resultUV.y * "+n[0]+".0);\n }\n ";return "\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(C(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,o=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)],a=Math.ceil(n[2]/2),i=a*Math.ceil(n[1]/2),"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+o[0]+", "+o[1]+"));\n int index = resTexRC.x * "+o[1]+" + resTexRC.y;\n\n int b = index / "+i+";\n index -= b * "+i+";\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),o=r*Math.ceil(t[t.length-2]/2),a=o,i="",u="b, r, c",s=2;s2,function(){return "Packed arg"+(n.charAt(0).toUpperCase()+n.slice(1))+" supports only inputs with rank above 2."});var o=t[t.length-1],a=Math.ceil(o/e);this.outputShape=t.slice(0,-1),a>1&&this.outputShape.push(a),r||this.variableNames.push("bestIndicesA");var i,u,s=this.outputShape,c=s.length,l=aa(c),h=qo("coords",c);if(1===a){var f=aa(u=c+1);i="\n "+f+" sourceLocR = "+f+"("+h.join()+", 0);\n ++"+h[c-1]+";\n "+f+" sourceLocG = "+f+"("+h.join()+", 0);\n ++"+h[c-2]+";\n "+f+" sourceLocA = "+f+"("+h.join()+", 0);\n --"+h[c-1]+";\n "+f+" sourceLocB = "+f+"("+h.join()+", 0);\n --"+h[c-2]+";";}else u=c,i="\n "+l+" sourceLocR = coords;\n ++"+h[c-1]+";\n "+l+" sourceLocG = coords;\n ++"+h[c-2]+";\n "+l+" sourceLocA = coords;\n --"+h[c-1]+";\n "+l+" sourceLocB = coords;\n --"+h[c-2]+";";var p=["x","y","z","w","u","v"].slice(0,u),d="."+p[u-1],v=p.map(function(t){return "int "+t}),m=qo("sourceLocR",u-1).concat("inIdx.r"),y=qo("sourceLocG",u-1).concat("inIdx.g"),x=qo("sourceLocB",u-1).concat("inIdx.b"),b=qo("sourceLocA",u-1).concat("inIdx.a"),w="max"===n?"greaterThan":"lessThan",C=r?"":"\n inIdx = round(vec4(getBestIndicesAChannel("+m.join()+"),\n getBestIndicesAChannel("+y.join()+"),\n getBestIndicesAChannel("+x.join()+"),\n getBestIndicesAChannel("+b.join()+")));",E="vec4(\n getAChannel("+m.join()+"),\n hasNextCol ? getAChannel("+y.join()+") : 0.,\n hasNextRow ? getAChannel("+x.join()+") : 0.,\n hasNextRow && hasNextCol ? getAChannel("+b.join()+") : 0.)",R=r?"":"\n float getBestIndicesAChannel("+v.join()+") {\n return getChannel(getBestIndicesA("+p.join()+"),\n vec2("+p.slice(-2).join()+"));\n }";this.userCode="\n float getAChannel("+v.join()+") {\n return getChannel(getA("+p.join()+"),\n vec2("+p.slice(-2).join()+"));\n }\n "+R+"\n void main() {\n "+l+" coords = getOutputCoords();\n bool hasNextCol = "+h[c-1]+" < "+(s[c-1]-1)+";\n bool hasNextRow = "+h[c-2]+" < "+(s[c-2]-1)+";\n "+i+"\n ivec4 srcIdx = ivec4(sourceLocR"+d+", sourceLocG"+d+",\n sourceLocB"+d+", sourceLocA"+d+") * "+e+";\n ivec4 inIdx = srcIdx;\n vec4 bestIndex = vec4(inIdx);\n vec4 bestValue = "+E+";\n\n for (int i = 0; i < "+e+"; i++) {\n inIdx = srcIdx;\n "+C+"\n vec4 candidate = "+E+";\n bvec4 nan = isnan(candidate);\n bvec4 replace = bvec4(\n vec4("+w+"(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 ";}}(),ca=function(){return function(t){this.variableNames=["dy"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,o=t.strideWidth,a=t.dilationHeight,i=t.dilationWidth,u=t.effectiveFilterHeight,s=t.effectiveFilterWidth,c=u-1-t.padInfo.top,l=s-1-t.padInfo.left,h=1/(e*n);this.userCode="\n const ivec2 pads = ivec2("+c+", "+l+");\n const float avgMultiplier = float("+h+");\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 < "+u+";\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 < "+s+";\n wC+= "+i+") {\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(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n ";}}(),la=function(){return function(t){this.variableNames=["dy"],this.outputShape=t.inShape;var e=t.filterDepth,n=t.filterHeight,r=t.filterWidth,o=t.strideDepth,a=t.strideHeight,i=t.strideWidth,u=t.dilationDepth,s=t.dilationHeight,c=t.dilationWidth,l=t.effectiveFilterDepth,h=t.effectiveFilterHeight,f=t.effectiveFilterWidth,p=l-1-t.padInfo.front,d=h-1-t.padInfo.top,v=f-1-t.padInfo.left,m=1/(e*n*r);this.userCode="\n const ivec3 pads = ivec3("+p+", "+d+", "+v+");\n const float avgMultiplier = float("+m+");\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(?, ?, ?, d) with pos mask(:, :, :, ch) 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 < "+l+";\n wD += "+u+") {\n float dyD = float(dyDCorner + wD) / "+o+".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 < "+h+";\n wR += "+s+") {\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 < "+f+";\n wC += "+c+") {\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(batch, idyD, idyR, idyC, ch);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n }\n setOutput(dotProd);\n }\n ";}}(),ha=function(){return function(t,e,n,r,o,a){this.outputShape=[],this.variableNames=["x","mean","variance"],po(t,e),po(t,n);var i="0.0";null!=r&&(po(t,r),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");var u="1.0";null!=o&&(po(t,o),this.variableNames.push("scale"),u="getScaleAtOutCoords()"),this.outputShape=t,this.userCode="\n void main() {\n float x = getXAtOutCoords();\n float mean = getMeanAtOutCoords();\n float variance = getVarianceAtOutCoords();\n float offset = "+i+";\n float scale = "+u+";\n float inv = scale * inversesqrt(variance + float("+a+"));\n setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));\n }\n ";}}(),fa=function(){return function(t,e,n,r,o,a){this.packedInputs=!0,this.packedOutput=!0,this.variableNames=["x","mean","variance"],po(t,e),po(t,n);var i="vec4(0.0)";null!=r&&(po(t,r),this.variableNames.push("offset"),i="getOffsetAtOutCoords()");var u="vec4(1.0)";null!=o&&(po(t,o),this.variableNames.push("scale"),u="getScaleAtOutCoords()"),this.outputShape=t,this.userCode="\n void main() {\n vec4 offset = "+i+";\n vec4 scale = "+u+";\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 ";}}(),pa="return areal * breal - aimag * bimag;",da="return areal * bimag + aimag * breal;",va=function(){return function(t,e,n){this.variableNames=["AReal","AImag","BReal","BImag"],this.outputShape=po(e,n),this.userCode="\n float binaryOpComplex(\n float areal, float aimag, float breal, float bimag) {\n "+t+"\n }\n\n void main() {\n float areal = getARealAtOutCoords();\n float aimag = getAImagAtOutCoords();\n float breal = getBRealAtOutCoords();\n float bimag = getBImagAtOutCoords();\n setOutput(binaryOpComplex(areal, aimag, breal, bimag));\n }\n ";}}(),ma="return a + b;",ga="return a - b;",ya="return a * b;",xa="return (a < 0.) ? b * a : a;",ba=function(){return function(t,e,n){this.variableNames=["A","B"],this.outputShape=po(e,n),this.userCode="\n float binaryOperation(float a, float b) {\n "+t+"\n }\n\n void main() {\n float a = getAAtOutCoords();\n float b = getBAtOutCoords();\n setOutput(binaryOperation(a, b));\n }\n ";}}(),wa="\n vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));\n return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);\n",Ca=function(){return function(t,e,n,r){void 0===r&&(r=!1),this.variableNames=["A","B"],this.supportsBroadcasting=!0,this.packedInputs=!0,this.packedOutput=!0,this.outputShape=po(e,n);var o=this.outputShape.length,a="";if(r)if(0===o||1===w(this.outputShape))a="\n result.y = 0.;\n result.z = 0.;\n result.w = 0.;\n ";else if(a="\n "+aa(o)+" coords = getOutputCoords();\n ",1===o)a+="\n result.y = (coords + 1) >= "+this.outputShape[0]+" ? 0. : result.y;\n result.z = 0.;\n result.w = 0.;\n ";else{var i=qo("coords",o);a+="\n bool nextRowOutOfBounds =\n ("+i[o-2]+" + 1) >= "+this.outputShape[o-2]+";\n bool nextColOutOfBounds =\n ("+i[o-1]+" + 1) >= "+this.outputShape[o-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 ";}}(),Ea=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,o){null==n.minLoc&&(n.minLoc=r.getUniformLocationNoThrow(o,"minVal"),n.maxLoc=r.getUniformLocationNoThrow(o,"maxVal")),r.gl.uniform1f(n.minLoc,t),r.gl.uniform1f(n.maxLoc,e);}},t}(),Ra=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,o){null==n.minLoc&&(n.minLoc=r.getUniformLocationNoThrow(o,"minVal"),n.maxLoc=r.getUniformLocationNoThrow(o,"maxVal")),r.gl.uniform1f(n.minLoc,t),r.gl.uniform1f(n.maxLoc,e);}},t}(),Ia=function(){return 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 ";}}(),ka=function(){return function(t){this.outputShape=[],this.outputShape=bn(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;n= "+u[s-1]+") {\n return getChannel(\n getT"+s+"("+Aa(i,c,p)+"),\n vec2("+Aa(l,c,p)+"));\n }";}var d=u.length,v=u[u.length-1];f+="\n return getChannel(\n getT"+d+"("+Aa(i,c,v)+"),\n vec2("+Aa(l,c,v)+"));",this.userCode="\n float getValue("+i.map(function(t){return "int "+t})+") {\n "+f+"\n }\n\n void main() {\n "+o+" 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 ";}}();function Aa(t,e,n){var r=t.indexOf(e);return t.map(function(t,e){return e===r?t+" - "+n:t}).join()}var Da=function(){return function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,n=t.strideWidth,r=t.padInfo.top,o=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+" - "+o+";\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 ";}}(),Ta=function(){return function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,o=t.strideWidth,a="channelsLast"===t.dataFormat,i=e-1-t.padInfo.top,u=n-1-t.padInfo.left,s=a?1:2,c=a?2:3,l=a?3:1;this.userCode="\n const ivec2 pads = ivec2("+i+", "+u+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords["+l+"];\n\n ivec2 dyCorner = ivec2(coords["+s+"], 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) / "+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 = "+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 ";}}(),Na=function(){return function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideDepth,n=t.strideHeight,r=t.strideWidth,o=t.padInfo.front,a=t.padInfo.top,i=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+" - "+o+";\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+" - "+i+";\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 ";}}(),Fa=function(){return function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterDepth,n=t.filterHeight,r=t.filterWidth,o=t.strideDepth,a=t.strideHeight,i=t.strideWidth,u=e-1-t.padInfo.front,s=n-1-t.padInfo.top,c=r-1-t.padInfo.left;this.userCode="\n const ivec3 pads = ivec3("+u+", "+s+", "+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) / "+o+".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) / "+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 = "+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 ";}}(),_a=function(){return function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,n=t.strideWidth,r=t.padInfo.top,o=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+" - "+o+";\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 ";}}(),Oa=function(){return function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,o=t.strideWidth,a=e-1-t.padInfo.top,i=n-1-t.padInfo.left,u=t.outChannels/t.inChannels;this.userCode="\n const ivec2 pads = ivec2("+a+", "+i+");\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) / "+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 = "+n+" - 1 - wC;\n\n // TO DO: Vec4 over the channelMul\n for (int dm = 0; dm < "+u+"; dm++) {\n int d2 = d1 * "+u+" + 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 ";}}(),Ma=function(){return 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 o=t.padInfo.top,a=t.padInfo.left,i=t.strideHeight,u=t.strideWidth,s=t.dilationHeight,c=t.dilationWidth,l=t.filterHeight,h=t.filterWidth,f=4*Math.floor(t.inChannels/4),p=t.inChannels%4,d="channelsLast"===t.dataFormat,v=d?1:2,m=d?2:3,g=d?3:1,y="",x="";n&&(y=r?"float activation(float a) {\n float b = getPreluActivationWeightsAtOutCoords();\n "+n+"\n }":"\n float activation(float x) {\n "+n+"\n }\n ",x="result = activation(result);");var b=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.userCode="\n "+y+"\n\n const ivec2 strides = ivec2("+i+", "+u+");\n const ivec2 pads = ivec2("+o+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords["+g+"];\n\n ivec2 xRCCorner =\n ivec2(coords["+v+"], coords["+m+"]) * 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 * "+s+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+h+"; 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 < "+f+"; 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 ("+d+") {\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===p)+") {\n\n if ("+d+") {\n dotProd +=\n getX(batch, xR, xC, "+f+") *\n getW(wR, wC, "+f+", d2);\n } else {\n dotProd +=\n getX(batch, "+f+", xR, xC) *\n getW(wR, wC, "+f+", d2);\n }\n\n } else if ("+(2===p)+") {\n vec2 wValues = vec2(\n getW(wR, wC, "+f+", d2),\n getW(wR, wC, "+f+" + 1, d2)\n );\n\n if ("+d+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+f+"),\n getX(batch, xR, xC, "+f+" + 1)\n );\n dotProd += dot(xValues, wValues);\n } else {\n vec2 xValues = vec2(\n getX(batch, "+f+", xR, xC),\n getX(batch, "+f+" + 1, xR, xC)\n );\n dotProd += dot(xValues, wValues);\n }\n\n } else if ("+(3===p)+") {\n vec3 wValues = vec3(\n getW(wR, wC, "+f+", d2),\n getW(wR, wC, "+f+" + 1, d2),\n getW(wR, wC, "+f+" + 2, d2)\n );\n\n if ("+d+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+f+"),\n getX(batch, xR, xC, "+f+" + 1),\n getX(batch, xR, xC, "+f+" + 2)\n );\n dotProd += dot(xValues, wValues);\n } else {\n vec3 xValues = vec3(\n getX(batch, "+f+", xR, xC),\n getX(batch, "+f+" + 1, xR, xC),\n getX(batch, "+f+" + 2, xR, xC)\n );\n dotProd += dot(xValues, wValues);\n }\n\n }\n }\n }\n\n float result = dotProd;\n "+b+"\n "+x+"\n setOutput(result);\n }\n ";}}(),Ba=function(){return function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.padInfo.front,n=t.padInfo.top,r=t.padInfo.left,o=t.strideDepth,a=t.strideHeight,i=t.strideWidth,u=t.dilationDepth,s=t.dilationHeight,c=t.dilationWidth,l=t.filterDepth,h=t.filterHeight,f=t.filterWidth,p=4*Math.floor(t.inChannels/4),d=t.inChannels%4;this.userCode="\n const ivec3 strides = ivec3("+o+", "+a+", "+i+");\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 * "+u+";\n\n if (xF < 0 || xF >= "+t.inDepth+") {\n continue;\n }\n\n for (int wR = 0; wR < "+h+"; wR++) {\n int xR = xRCorner + wR * "+s+";\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 < "+p+"; 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===d)+") {\n dotProd +=\n getX(batch, xF, xR, xC, "+p+") *\n getW(wF, wR, wC, "+p+", d2);\n } else if ("+(2===d)+") {\n vec2 xValues = vec2(\n getX(batch, xF, xR, xC, "+p+"),\n getX(batch, xF, xR, xC, "+p+" + 1)\n );\n vec2 wValues = vec2(\n getW(wF, wR, wC, "+p+", d2),\n getW(wF, wR, wC, "+p+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===d)+") {\n vec3 xValues = vec3(\n getX(batch, xF, xR, xC, "+p+"),\n getX(batch, xF, xR, xC, "+p+" + 1),\n getX(batch, xF, xR, xC, "+p+" + 2)\n );\n vec3 wValues = vec3(\n getW(wF, wR, wC, "+p+", d2),\n getW(wF, wR, wC, "+p+" + 1, d2),\n getW(wF, wR, wC, "+p+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n }\n setOutput(dotProd);\n }\n ";}}(),Pa=function(){return 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 o=t.inHeight,a=t.inWidth,i=t.padInfo.top,u=t.padInfo.left,s=t.strideHeight,c=t.strideWidth,l=t.dilationHeight,h=t.dilationWidth,f=t.filterHeight,p=t.filterWidth,d=t.outChannels/t.inChannels,v="",m="";n&&(v=r?"float activation(float a) {\n float b = getPreluActivationWeightsAtOutCoords();\n "+n+"\n }":"\n float activation(float x) {\n "+n+"\n }\n ",m="result = activation(result);");var g=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.userCode="\n "+v+"\n\n const ivec2 strides = ivec2("+s+", "+c+");\n const ivec2 pads = ivec2("+i+", "+u+");\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 / "+d+";\n int q = d2 - d1 * "+d+";\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 < "+f+"; wR++) {\n int xR = xRCorner + wR * "+l+";\n\n if (xR < 0 || xR >= "+o+") {\n continue;\n }\n\n for (int wC = 0; wC < "+p+"; wC++) {\n int xC = xCCorner + wC * "+h+";\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 "+g+"\n "+m+"\n setOutput(result);\n }\n ";}}(),La=function(){return 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 o=t.inHeight,a=t.inWidth,i=t.padInfo.top,u=t.padInfo.left,s=t.strideHeight,c=t.strideWidth,l=t.dilationHeight,h=t.dilationWidth,f=t.filterHeight,p=t.filterWidth,d=p,m="int xR; int xC; int xCOffset;",g=0;g= 0 && xR < "+o+" && xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+y+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+y+" = vec4(0.);\n }\n\n xCOffset = xC + 1 - 2;\n if(xR >= 0 && xR < "+o+" && xCOffset >= 0 && xCOffset < "+a+") {\n vec4 previous = getX(batch, xR, xCOffset, d1);\n xR"+g+"C"+y+" = vec4(previous.zw, xTexelR"+g+"C"+y+".xy);\n } else {\n xR"+g+"C"+y+" = vec4(0, 0, xTexelR"+g+"C"+y+".xy);\n }\n ":"\n if(xR >= 0 && xR < "+o+" && xC >= 0 && xC < "+a+") {\n xTexelR"+g+"C"+y+" = getX(batch, xR, xC, d1);\n } else {\n xTexelR"+g+"C"+y+" = vec4(0.);\n }\n\n xR"+g+"C"+y+" = xTexelR"+g+"C"+y+";\n ",y+1= 0 && xR < "+o+" &&\n xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+(y+2)+" = getX(batch, xR, xCOffset, d1);\n }\n ",h>1&&(m+="\n xCOffset -= 2;\n if(xR >= 0 && xR < "+o+" &&\n xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+y+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+y+" = vec4(0.);\n }\n "),m+="\n xR"+g+"C"+(y+1)+" = vec4(\n xTexelR"+g+"C"+y+".zw, xTexelR"+g+"C"+(y+2)+".xy);\n "):m+="\n xCOffset = xC + "+b+";\n\n if(xR >= 0 && xR < "+o+" &&\n xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+(y+2)+" = getX(batch, xR, xCOffset, d1);\n }\n\n xR"+g+"C"+(y+1)+" = xTexelR"+g+"C"+(y+2)+";\n ";}}else y= 0 && xR < "+o+") {\n ",u%2==1?(m+="\n xCOffset = xC + 1 - "+c+";\n if(xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+y+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+y+" = vec4(0.);\n }\n\n if(xC + 1 >= 0 && xC + 1 < "+a+") {\n xTexelR"+g+"C"+(y+2)+" = getX(batch, xR, xC + 1, d1);\n } else {\n xTexelR"+g+"C"+(y+2)+" = vec4(0.);\n }\n\n xR"+g+"C"+y+" = vec4(\n xTexelR"+g+"C"+y+".zw, xTexelR"+g+"C"+(y+2)+".zw);\n ",y+1= 0 && xCOffset < "+a+") {\n final = getX(batch, xR, xCOffset, d1);\n }\n xR"+g+"C"+(y+1)+" = vec4(xTexelR"+g+"C"+(y+2)+".xy, final.xy);\n ")):(m+="\n if(xC >= 0 && xC < "+a+") {\n xTexelR"+g+"C"+y+" = getX(batch, xR, xC, d1);\n } else {\n xTexelR"+g+"C"+y+" = vec4(0.);\n }\n\n xCOffset = xC + "+c+";\n if(xCOffset >= 0 && xCOffset < "+a+") {\n xTexelR"+g+"C"+(y+2)+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+g+"C"+(y+2)+" = vec4(0.);\n }\n\n xR"+g+"C"+y+" = vec4(\n xTexelR"+g+"C"+y+".xy, xTexelR"+g+"C"+(y+2)+".xy);\n ",y+11?[""+(i-1)/(l-1),"(y2-y1) * height_ratio","y1*"+d+" + float(y)*(height_scale)"]:["0.0","0.0","0.5 * (y1+y2) * "+d],g=m[0],y=m[1],x=m[2],b=h>1?[""+(u-1)/(h-1),"(x2-x1) * width_ratio","x1*"+v+" + float(x)*(width_scale)"]:["0.0","0.0","0.5 * (x1+x2) * "+v],w=b[0],C=b[1],E=b[2];this.userCode="\n const float height_ratio = float("+g+");\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 = "+C+";\n\n float in_y = "+x+";\n if( in_y < 0.0 || in_y > "+d+" ) {\n setOutput(float("+o+"));\n return;\n }\n float in_x = "+E+";\n if( in_x < 0.0 || in_x > "+v+" ) {\n setOutput(float("+o+"));\n return;\n }\n\n vec2 sourceFracIndexCR = vec2(in_x,in_y);\n if("+f+" == 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 ";}}(),Ua=function(){return function(t,e,n){this.variableNames=["x"],this.outputShape=t;var r=t.length,o=t[t.length-1],a=n?"<":">";this.userCode="\n int getIndex(int i) {\n "+(n?"return "+o+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+aa(r)+" coords = getOutputCoords();\n int end = "+Va(r,"coords")+";\n float val = 0.0;\n for (int i = "+o+" - 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 "+Va(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 ";}}();function Va(t,e){if(1===t)return ""+e;if(2===t)return e+".y";if(3===t)return e+".z";if(4===t)return e+".w";throw Error("Cumulative sum for rank "+t+" is not yet supported")}var za=function(){return function(t){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outPackingScheme=Ot.DENSE;var e=zt(t),n=Ko();this.outputShape=t,this.userCode="\n ivec3 outCoordsFromFlatIndex(int index) {\n "+jo(["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 ";}}(),Ga=function(){return function(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=Ot.DENSE;var e=zt(t),n=Ko();this.outputShape=t,this.userCode="\n ivec3 outCoordsFromFlatIndex(int index) {\n "+jo(["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 ";}}(),Ha=function(){function t(t,e,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=t,this.blockSize=e,this.dataFormat=n,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int h = "+this.getHeightCoordString()+";\n int w = "+this.getWidthCoordString()+";\n int d = "+this.getDepthCoordString()+";\n\n int in_h = h / "+e+";\n int offset_h = imod(h, "+e+");\n int in_w = w / "+e+";\n int offset_w = imod(w, "+e+");\n int offset_d = (offset_h * "+e+" + offset_w) *\n "+this.getOutputDepthSize()+";\n int in_d = d + offset_d;\n\n float result = "+this.getInputSamplingString()+";\n setOutput(result);\n }\n ";}return t.prototype.getHeightCoordString=function(){return "NHWC"===this.dataFormat?"coords[1]":"coords[2]"},t.prototype.getWidthCoordString=function(){return "NHWC"===this.dataFormat?"coords[2]":"coords[3]"},t.prototype.getDepthCoordString=function(){return "NHWC"===this.dataFormat?"coords[3]":"coords[1]"},t.prototype.getOutputDepthSize=function(){return "NHWC"===this.dataFormat?this.outputShape[3]:this.outputShape[1]},t.prototype.getInputSamplingString=function(){return "NHWC"===this.dataFormat?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"},t}(),qa=function(){return 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 ";}}(),Ka=function(){return function(t){this.variableNames=["A"],this.outTexUsage=Mt.DOWNLOAD;var e=Ko();this.outputShape=t,this.userCode="\n "+$o+"\n\n void main() {\n float x = getAAtOutCoords();\n "+e.output+" = encode_float(x);\n }\n ";}}(),ja=function(){return function(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=Mt.DOWNLOAD;var e=Ko();this.outputShape=t,this.userCode="\n "+$o+"\n\n void main() {\n ivec3 coords = getOutputCoords();\n float x = getChannel(getAAtOutCoords(), vec2(coords.y, coords.z));\n "+e.output+" = encode_float(x);\n }\n ";}}(),Xa=function(){return function(t,e,n){void 0===n&&(n=!1),this.variableNames=["A"];var r=Ko(),o=e[0],a=e[1];this.outputShape=t;var i="result";n&&(i="floor(result * 255. + 0.5)"),this.userCode="\n "+Xo(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, "+o+".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("+i+", 0., 0., 0.);\n }\n ";}}(),$a=function(){return function(t,e,n){void 0===n&&(n=!1),this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;var r=Ko(),o=e[0],a=e[1];this.outputShape=t;var i="",u="result";n&&(u="floor(result * 255. + 0.5)");for(var s=0;s<=1;s++)for(var c=0;c<=1;c++){var l=2*s+c;i+="\n localCoords = coords;\n if(localCoords[2] + "+c+" < "+t[2]+") {\n localCoords[2] += "+c+";\n if(localCoords[1] + "+s+" < "+t[1]+") {\n localCoords[1] += "+s+";\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, "+o+".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 "+Xo(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 "+i+"\n\n "+r.output+" = "+u+";\n }\n ";}}(),Ya="return real * expR - imag * expI;",Qa="return real * expI + imag * expR;",Ja=function(){return function(t,e,n){this.variableNames=["real","imag"];var r=e[1];this.outputShape=e;var o=n?"2.0 * "+Math.PI:"-2.0 * "+Math.PI,a=n?r+".0":"1.0";this.userCode="\n const float exponentMultiplier = "+o+";\n\n float unaryOpComplex(float real, float expR, float imag, float expI) {\n "+t+"\n }\n\n float mulMatDFT(int batch, int index) {\n float indexRatio = float(index) / float("+r+");\n float exponentMultiplierTimesIndexRatio =\n exponentMultiplier * indexRatio;\n\n float result = 0.0;\n\n for (int i = 0; i < "+r+"; i++) {\n // x = (-2|2 * PI / N) * index * i;\n float x = exponentMultiplierTimesIndexRatio * float(i);\n float expR = cos(x);\n float expI = sin(x);\n float real = getReal(batch, i);\n float imag = getImag(batch, i);\n\n result +=\n unaryOpComplex(real, expR, imag, expI) / "+a+";\n }\n\n return result;\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n setOutput(mulMatDFT(coords[0], coords[1]));\n }\n ";}}(),Za=function(){function t(t,e){this.outputShape=[],this.variableNames=["x"],this.outputShape=t,this.userCode="\n uniform float value;\n void main() {\n // Input can be obtained from uniform value.\n setOutput(value);\n }\n ";}return t.prototype.getCustomSetupFunc=function(t){var e=this;return function(n,r){null==e.valueLoc&&(e.valueLoc=n.getUniformLocationNoThrow(r,"value")),n.gl.uniform1f(e.valueLoc,t);}},t}(),ti=function(){return function(t,e,n){this.variableNames=["A","indices"];var r=t.slice();r[n]=e,this.outputShape=r,this.rank=r.length;var o=aa(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"],o=[],a=0;a1?"strides[j]":"strides";this.userCode="\n "+r+" strides = "+r+"("+this.strides+");\n void main() {\n "+o+" 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 ";}}();function ni(t,e){var n=Ko();return Qt(t,e,n.version+"\n precision highp float;\n "+n.attribute+" vec3 clipSpacePos;\n "+n.attribute+" vec2 uv;\n "+n.varyingVs+" vec2 resultUV;\n\n void main() {\n gl_Position = vec4(clipSpacePos, 1);\n resultUV = uv;\n }")}function ri(t,e){return ae(t,e,new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]))}function oi(t,e){return ie(t,e,new Uint16Array([0,1,2,2,1,3]))}function ai(t,e,n,r,o,a,i){se(n,r);var u=ue(t,e),s=t.TEXTURE_2D;return qt(t,e,function(){return t.bindTexture(s,u)}),qt(t,e,function(){return t.texParameteri(s,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE)}),qt(t,e,function(){return t.texParameteri(s,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE)}),qt(t,e,function(){return t.texParameteri(s,t.TEXTURE_MIN_FILTER,t.NEAREST)}),qt(t,e,function(){return t.texParameteri(s,t.TEXTURE_MAG_FILTER,t.NEAREST)}),qt(t,e,function(){return t.texImage2D(s,0,o,n,r,0,a,i,null)}),qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,null)}),u}function ii(t,e,n,r,o){var a=Vt(n,r);return ai(t,e,a[0],a[1],o.internalFormatFloat,o.textureFormatFloat,t.FLOAT)}function ui(t,e,n,r,o){var a=Vt(n,r);return ai(t,e,a[0],a[1],o.internalFormatHalfFloat,o.textureFormatFloat,o.textureTypeHalfFloat)}function si(t,e,n,r,o){var a=Vt(n,r);return ai(t,e,a[0],a[1],t.RGBA,t.RGBA,t.UNSIGNED_BYTE)}function ci(t,e,n,r,o){var a=Gt(n,r);return ai(t,e,a[0],a[1],o.internalFormatPackedFloat,t.RGBA,t.FLOAT)}function li(t,e,n,r,o){var a=Gt(n,r);return ai(t,e,a[0],a[1],o.internalFormatPackedHalfFloat,t.RGBA,o.textureTypeHalfFloat)}function hi(t,e,n,r){return qt(t,e,function(){return t.bindBuffer(t.ARRAY_BUFFER,r)}),le(t,e,n,"clipSpacePos",r,3,20,0)&&le(t,e,n,"uv",r,2,20,12)}function fi(t,e,n,r,o,a,i){var u,s,c;qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,n)}),a instanceof Uint8Array?(u=new Uint8Array(r*o*4),s=t.UNSIGNED_BYTE,c=t.RGBA):(u=new Float32Array(r*o*4),s=t.FLOAT,c=i.internalFormatPackedFloat),u.set(a),qt(t,e,function(){return t.texImage2D(t.TEXTURE_2D,0,c,r,o,0,t.RGBA,s,u)}),qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,null)});}function pi(t,e,n,r){qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,n)}),r.data instanceof Uint8Array?qt(t,e,function(){return t.texImage2D(t.TEXTURE_2D,0,t.RGBA,r.width,r.height,0,t.RGBA,t.UNSIGNED_BYTE,r.data)}):qt(t,e,function(){return t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,r)}),qt(t,e,function(){return t.bindTexture(t.TEXTURE_2D,null)});}function di(t,e,n,r,o){var a=t.createBuffer();qt(t,e,function(){return t.bindBuffer(t.PIXEL_PACK_BUFFER,a)});var i=16*n*r;return qt(t,e,function(){return t.bufferData(t.PIXEL_PACK_BUFFER,i,t.STREAM_READ)}),qt(t,e,function(){return t.readPixels(0,0,r,n,t.RGBA,t.FLOAT,0)}),qt(t,e,function(){return t.bindBuffer(t.PIXEL_PACK_BUFFER,null)}),a}function vi(t,e,n){var r=t,o=new Float32Array(n);return r.bindBuffer(r.PIXEL_PACK_BUFFER,e),r.getBufferSubData(r.PIXEL_PACK_BUFFER,0,o),r.bindBuffer(r.PIXEL_PACK_BUFFER,null),o}function mi(t,e,n,r,o){var a=Vt(n,r),i=a[0],u=a[1],s=new Uint8Array(n*r*4);return qt(t,e,function(){return t.readPixels(0,0,i,u,o.downloadTextureFormat,t.UNSIGNED_BYTE,s)}),new Float32Array(s.buffer)}function gi(t,e,n,r,o,a,i,u){var s=t,c=new Float32Array(function(t,e){var n=Gt(t,e);return n[0]*n[1]*4}(a,i));return s.bindBuffer(s.PIXEL_PACK_BUFFER,e),s.getBufferSubData(s.PIXEL_PACK_BUFFER,0,c),s.bindBuffer(s.PIXEL_PACK_BUFFER,null),c}function yi(t,e,n,r){var o=new Float32Array(n*r*4);return qt(t,e,function(){return t.readPixels(0,0,r,n,t.RGBA,t.FLOAT,o)}),o}var xi=Object.freeze({createVertexShader:ni,createVertexBuffer:ri,createIndexBuffer:oi,createFloat32MatrixTexture:ii,createFloat16MatrixTexture:ui,createUnsignedBytesMatrixTexture:si,createPackedMatrixTexture:ci,createFloat16PackedMatrixTexture:li,bindVertexProgramAttributeStreams:hi,uploadDenseMatrixToTexture:fi,uploadPixelDataToTexture:pi,createBufferFromOutputTexture:di,downloadFloat32MatrixFromBuffer:vi,downloadByteEncodedFloatMatrixFromOutputTexture:mi,downloadPackedMatrixFromBuffer:gi,downloadMatrixFromPackedOutputTexture:yi}),bi=function(){function t(t){this.outputTexture=null,this.program=null,this.disposed=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[];var e=a().getNumber("WEBGL_VERSION");null!=t?(this.gl=t,Wt(e,t)):this.gl=Ut(e);var n="WEBGL_color_buffer_float";if(1===a().getNumber("WEBGL_VERSION")){if(this.textureFloatExtension=Yt(this.gl,this.debug,"OES_texture_float"),Te(this.gl,"OES_texture_half_float"))this.textureHalfFloatExtension=Yt(this.gl,this.debug,"OES_texture_half_float");else if(a().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support half float textures, yet the environment flag WEBGL_FORCE_F16_TEXTURES is set to true.");if(this.colorBufferFloatExtension=this.gl.getExtension(n),Te(this.gl,"EXT_color_buffer_half_float"))this.colorBufferHalfFloatExtension=Yt(this.gl,this.debug,"EXT_color_buffer_half_float");else if(a().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support color renderable half floats, yet the environment flag WEBGL_FORCE_F16_TEXTURES is set to true.")}else if(n="EXT_color_buffer_float",Te(this.gl,n))this.colorBufferFloatExtension=this.gl.getExtension(n);else{if(!Te(this.gl,"EXT_color_buffer_half_float"))throw new Error("GL context does not support color renderable floats");this.colorBufferHalfFloatExtension=this.gl.getExtension("EXT_color_buffer_half_float");}this.vertexBuffer=ri(this.gl,this.debug),this.indexBuffer=oi(this.gl,this.debug),this.framebuffer=ce(this.gl,this.debug),this.textureConfig=Ht(this.gl,this.textureHalfFloatExtension);}return Object.defineProperty(t.prototype,"debug",{get:function(){return a().getBool("DEBUG")},enumerable:!0,configurable:!0}),t.prototype.dispose=function(){var t=this;if(!this.disposed){null!=this.program&&console.warn("Disposing a GPGPUContext that still has a bound WebGLProgram. This is probably a resource leak, delete the program with GPGPUContext.deleteProgram before disposing."),null!=this.outputTexture&&console.warn("Disposing a GPGPUContext that still has a bound output matrix texture. This is probably a resource leak, delete the output matrix texture with GPGPUContext.deleteMatrixTexture before disposing.");var e=this.gl;qt(e,this.debug,function(){return e.finish()}),qt(e,this.debug,function(){return e.bindFramebuffer(e.FRAMEBUFFER,null)}),qt(e,this.debug,function(){return e.deleteFramebuffer(t.framebuffer)}),qt(e,this.debug,function(){return e.bindBuffer(e.ARRAY_BUFFER,null)}),qt(e,this.debug,function(){return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null)}),qt(e,this.debug,function(){return e.deleteBuffer(t.indexBuffer)}),this.disposed=!0;}},t.prototype.createFloat32MatrixTexture=function(t,e){return this.throwIfDisposed(),ii(this.gl,this.debug,t,e,this.textureConfig)},t.prototype.createFloat16MatrixTexture=function(t,e){return this.throwIfDisposed(),ui(this.gl,this.debug,t,e,this.textureConfig)},t.prototype.createUnsignedBytesMatrixTexture=function(t,e){return this.throwIfDisposed(),si(this.gl,this.debug,t,e,this.textureConfig)},t.prototype.uploadPixelDataToTexture=function(t,e){this.throwIfDisposed(),pi(this.gl,this.debug,t,e);},t.prototype.uploadDenseMatrixToTexture=function(t,e,n,r){this.throwIfDisposed(),fi(this.gl,this.debug,t,e,n,r,this.textureConfig);},t.prototype.createFloat16PackedMatrixTexture=function(t,e){return this.throwIfDisposed(),li(this.gl,this.debug,t,e,this.textureConfig)},t.prototype.createPackedMatrixTexture=function(t,e){return this.throwIfDisposed(),ci(this.gl,this.debug,t,e,this.textureConfig)},t.prototype.deleteMatrixTexture=function(t){var e=this;this.throwIfDisposed(),this.outputTexture===t&&(me(this.gl,this.debug,this.framebuffer),this.outputTexture=null),qt(this.gl,this.debug,function(){return e.gl.deleteTexture(t)});},t.prototype.downloadByteEncodedFloatMatrixFromOutputTexture=function(t,e,n){var r=this;return this.downloadMatrixDriver(t,function(){return mi(r.gl,r.debug,e,n,r.textureConfig)})},t.prototype.downloadPackedMatrixFromBuffer=function(t,e,n,r,o,a){return gi(this.gl,t,0,0,0,o,a,this.textureConfig)},t.prototype.downloadFloat32MatrixFromBuffer=function(t,e){return vi(this.gl,t,e)},t.prototype.createBufferFromTexture=function(t,e,n){this.bindTextureToFrameBuffer(t);var r=di(this.gl,this.debug,e,n,this.textureConfig);return this.unbindTextureToFrameBuffer(),r},t.prototype.createAndWaitForFence=function(){var t=this.createFence(this.gl);return this.pollFence(t)},t.prototype.createFence=function(t){var e,n,r=this;if(a().getBool("WEBGL_FENCE_API_ENABLED")){var o=t,i=o.fenceSync(o.SYNC_GPU_COMMANDS_COMPLETE,0);t.flush(),n=function(){var t=o.clientWaitSync(i,0,0);return t===o.ALREADY_SIGNALED||t===o.CONDITION_SATISFIED},e=i;}else a().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(e=this.beginQuery(),this.endQuery(),n=function(){return r.isQueryAvailable(e,a().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 yi(r.gl,r.debug,e,n)})},t.prototype.createProgram=function(t){this.throwIfDisposed();var e=this.gl,n=Jt(e,this.debug,t),r=ni(e,this.debug),o=ne(e,this.debug);return qt(e,this.debug,function(){return e.attachShader(o,r)}),qt(e,this.debug,function(){return e.attachShader(o,n)}),re(e,this.debug,o),this.debug&&oe(e,this.debug,o),this.vertexAttrsAreBound||(this.setProgram(o),this.vertexAttrsAreBound=hi(e,this.debug,this.program,this.vertexBuffer)),o},t.prototype.deleteProgram=function(t){var e=this;this.throwIfDisposed(),t===this.program&&(this.program=null),null!=t&&qt(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&&oe(this.gl,this.debug,this.program),qt(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?fe(this.gl,this.debug,t,e):pe(this.gl,t,e)},t.prototype.getAttributeLocation=function(t,e){var n=this;return this.throwIfDisposed(),qt(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(),de(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=Gt(e,n),o=r[0],a=r[1];this.setOutputMatrixTextureDriver(t,o,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&&oe(this.gl,this.debug,this.program),ge(this.gl);},t.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var t=this.gl;this.debug&&this.debugValidate(),qt(t,this.debug,function(){return t.drawElements(t.TRIANGLES,6,t.UNSIGNED_SHORT,0)});},t.prototype.blockUntilAllProgramsCompleted=function(){var t=this;this.throwIfDisposed(),qt(this.gl,this.debug,function(){return t.gl.finish()});},t.prototype.getQueryTimerExtension=function(){return null==this.disjointQueryTimerExtension&&(this.disjointQueryTimerExtension=Yt(this.gl,this.debug,2===a().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===a().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(),o=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,o),o},t.prototype.endQuery=function(){if(2!==a().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 n(this,void 0,void 0,function(){var e=this;return r(this,function(n){switch(n.label){case 0:return [4,S(function(){return e.disposed||e.isQueryAvailable(t,a().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))})];case 1:return n.sent(),[2,this.getQueryTime(t,a().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(),o=n.getQueryParameter(t,n.QUERY_RESULT_AVAILABLE);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),o&&!this.disjoint}o=(r=this.getQueryTimerExtensionWebGL1()).getQueryObjectEXT(t,r.QUERY_RESULT_AVAILABLE_EXT);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),o&&!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||S(function(){return n.pollItems(),0===n.itemsToPoll.length});},t.prototype.bindTextureToFrameBuffer=function(t){this.throwIfDisposed(),ve(this.gl,this.debug,t,this.framebuffer),this.debug&&ge(this.gl);},t.prototype.unbindTextureToFrameBuffer=function(){null!=this.outputTexture?(ve(this.gl,this.debug,this.outputTexture,this.framebuffer),this.debug&&ge(this.gl)):me(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;ve(r,this.debug,t,this.framebuffer),this.debug&&ge(r),this.outputTexture=t,qt(r,this.debug,function(){return r.viewport(0,0,e,n)}),qt(r,this.debug,function(){return r.scissor(0,0,e,n)});},t.prototype.setOutputMatrixWriteRegionDriver=function(t,e,n,r){var o=this;this.throwIfDisposed(),qt(this.gl,this.debug,function(){return o.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 wi(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,o=e[n],a=o.shape;if(!C(r,a))throw Error("Binary was compiled with different shapes than the current args. Shapes "+r+" and "+a+" must match");if(!t.isUniform||!o.isUniform){var i=t.texShape,u=o.isUniform?null:o.texData.texShape;if(!C(i,u))throw Error("Binary was compiled with different texture shapes than the current args. Shape "+i+" and "+u+" must match")}});}var Ci=function(){return function(t,e,n){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t;for(var r=n.filterWidth,o=n.inChannels,a=n.strideWidth,i=n.strideHeight,u=n.padInfo,s=n.outWidth,c=n.dilationWidth,l=n.dilationHeight,h=n.dataFormat,f=u.left,p=u.top,d=o*r,v=Ko(),m="channelsLast"===h,g=m?0:1,y=m?1:2,x="",b=0;b<=1;b++)for(var w=0;w<=1;w++)x+="\n blockIndex = rc.y + "+w+";\n pos = rc.x + "+b+";\n\n if(blockIndex < "+t[1]+" && pos < "+t[0]+") {\n offsetY = int(blockIndex / ("+s+")) * "+i+" - "+p+";\n d0 = offsetY + "+l+" * (pos / "+d+");\n\n if(d0 < "+e[g]+" && d0 >= 0) {\n\n offsetX = int(mod(float(blockIndex), "+s+".) * "+a+". - "+f+".);\n d1 = offsetX + "+c+" * (int(mod(float(pos), "+d+".) / "+o+".));\n\n if(d1 < "+e[y]+" && d1 >= 0) {\n\n ch = int(mod(float(pos), "+o+".));\n\n if ("+m+") {\n innerDims = vec2(d1, ch);\n result["+(2*b+w)+"] = getChannel(\n getA(d0, int(innerDims.x),\n int(innerDims.y)), innerDims);\n } else {\n innerDims = vec2(d0, d1);\n result["+(2*b+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 "+x+"\n\n "+v.output+" = result;\n }\n ";}}(),Ei=function(){return function(t,e,n,r,o){this.variableNames=["x"],this.outputShape=[];var a,i=e,u=t[3]-1;this.outputShape=t;var s="float("+n+") + float("+r+") * sum";a=.5===o?"inversesqrt("+s+")":1===o?"1.0/("+s+")":"exp(log("+s+") * float(-"+o+"));",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 = -"+i+"; j <= "+i+"; j++) {\n int idx = d + j;\n if (idx >= 0 && idx <= "+u+") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * "+a+";\n setOutput(val);\n }\n ";}}(),Ri=function(){return function(t,e,n,r,o){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=o,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("+o+")\n * getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)\n / norm;\n if (k == d) {\n dyi += pow(norm, -1.0 * "+o+");\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 ";}}(),Ii=function(){return function(t,e,n,r,o){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;var a,i=e,u=t[3]-1;this.outputShape=t;var s="float("+n+") + float("+r+") * sum";a=.5===o?"inversesqrt("+s+")":1===o?"1.0/("+s+")":"exp(log("+s+") * float(-"+o+"));",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 - "+i+";\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 = - "+i+"; j <= "+i+"; j++) {\n ivec2 idx = depth + j;\n bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));\n bvec2 belowUpperBound = lessThanEqual(idx, ivec2("+u+"));\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 ";}}(),ki=function(){return function(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideHeight,n=t.strideWidth,r=t.dilationHeight,o=t.effectiveFilterHeight,a=t.effectiveFilterWidth,i=o-1-t.padInfo.top,u=a-1-t.padInfo.left,s=o*a-1;this.userCode="\n const ivec2 pads = ivec2("+i+", "+u+");\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 < "+o+";\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 = "+s+" - 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 ";}}(),Si=function(){return function(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideDepth,n=t.strideHeight,r=t.strideWidth,o=t.dilationDepth,a=t.dilationHeight,i=t.dilationWidth,u=t.effectiveFilterDepth,s=t.effectiveFilterHeight,c=t.effectiveFilterWidth,l=u-1-t.padInfo.front,h=s-1-t.padInfo.top,f=c-1-t.padInfo.left,p=u*s*c-1;this.userCode="\n const ivec3 pads = ivec3("+l+", "+h+", "+f+");\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 < "+u+";\n wD += "+o+") {\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 < "+s+";\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 += "+i+") {\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 = "+p+" -\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 * "+s+" * "+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 ";}}(),Ai=function(){return function(t,e,n,r,o,a,i){void 0===n&&(n=!1),void 0===r&&(r=!1),void 0===o&&(o=!1),void 0===a&&(a=null),void 0===i&&(i=!1),this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e;var u=n?t[1]:t[2],s=Math.ceil(u/2),c=n?"i * 2, rc.y":"rc.y, i * 2",l=r?"rc.z, i * 2":"i * 2, rc.z",h=n?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],f=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],p="",d="";a&&(p=i?"vec4 activation(vec4 a) {\n vec4 b = getPreluActivationWeightsAtOutCoords();\n "+a+"\n }":"vec4 activation(vec4 x) {\n "+a+"\n }",d="result = activation(result);");var v=o?"result += getBiasAtOutCoords();":"";o&&this.variableNames.push("bias"),i&&this.variableNames.push("preluActivationWeights"),this.userCode="\n "+p+"\n\n const float sharedDimension = "+s+".0;\n\n vec4 dot2x2ARowBCol(ivec3 rc) {\n vec4 result = vec4(0);\n for (int i = 0; i < "+s+"; 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 += ("+h[0]+" * "+f[0]+");\n result += ("+h[1]+" * "+f[1]+");\n }\n return result;\n }\n\n void main() {\n ivec3 rc = getOutputCoords();\n vec4 result = dot2x2ARowBCol(rc);\n\n "+v+"\n\n "+d+"\n\n setOutput(result);\n }\n ";}}(),Di=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}(),Ti=function(){return 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 ";}}(),Ni=function(){return function(t){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outputShape=t;var e=t.length;if(0===e)this.userCode="\n void main() {\n setOutput(vec4(getA(), 0., 0., 0.));\n }\n ";else{var n=qo("rc",e),r=aa(e),o=function(t,e,n){if(1===t)return "rc > "+e[0];for(var r="",o=t-2;o= "+e[o],o= "+e+";\n bool rEdge = rp1 >= "+n+";\n "}(e,t[t.length-1],t[t.length-2],n),i=function(t,e){var n=t.length,r=function(t,e){for(var n=[],r=0;r<=1;r++)for(var o=0;o<=1;o++){for(var a=(0===r?"r":"rp1")+", "+(0===o?"c":"cp1"),i=2;i= "+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("+o+") {\n setOutput(vec4(0));\n } else {\n "+a+"\n\n setOutput(vec4("+i+"));\n }\n }\n ";}}}();var Fi=function(){return function(t,e,n){this.variableNames=["x"],this.outputShape=e.map(function(e,n){return e[0]+t[n]+e[1]});var r=t.length,o=aa(r),a=e.map(function(t){return t[0]}).join(","),i=e.map(function(e,n){return e[0]+t[n]}).join(","),u=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r);this.userCode=1!==r?"\n "+o+" start = "+o+"("+a+");\n "+o+" end = "+o+"("+i+");\n\n void main() {\n "+o+" outC = getOutputCoords();\n if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {\n setOutput(float("+n+"));\n } else {\n "+o+" coords = outC - start;\n setOutput(getX("+u+"));\n }\n }\n ":"\n int start = "+a+";\n int end = "+i+";\n\n void main() {\n int outC = getOutputCoords();\n if (outC < start || outC >= end) {\n setOutput(float("+n+"));\n } else {\n setOutput(getX(outC - start));\n }\n }\n ";}}(),_i=function(){return function(t,e,n){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e.map(function(e,n){return e[0]+t[n]+e[1]});for(var r=t.length,o=aa(r),a=e.map(function(t){return t[0]}).join(","),i=e.map(function(e,n){return e[0]+t[n]}).join(","),u=qo("rc",r),s=qo("source",r),c=u[r-1]+" < "+this.outputShape[r-1],l=1===r?"source":"vec2("+s.slice(-2).join()+")",h=[o+" rc = outputLoc;",u[r-1]+" += 1;\n if("+c+") {\n ",1===r?"":"}\n rc = outputLoc;\n "+u[r-2]+" += 1;\n if("+u[r-2]+" < "+this.outputShape[r-2]+") {",1===r?"":" "+u[r-1]+" += 1;\n if("+c+") {"],f=1===r?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",p="",d=0,v=1===r?2:4;d= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+c+";\n wC += "+u+") {\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 = wR * "+c+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var d=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===e&&(d="avgValue / count");var v=4*Math.floor(r/4),m=r%4,g="\n if ("+f+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+o+", "+a+");\n const ivec2 pads = ivec2("+l+", "+h+");\n const float initializationValue = "+p+";\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("+p+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+s+";\n wR += "+i+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+v+"; wC += 4) {\n int xC = xCCorner + wC * "+u+";\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+u+", d),\n getValue(batch, xR, xC + 2 * "+u+", d),\n getValue(batch, xR, xC + 3 * "+u+", d)\n );\n\n "+g+"\n }\n\n int xC = xCCorner + "+v+";\n if ("+(1===m)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+g+"\n } else if ("+(2===m)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+u+", d),\n initializationValue,\n initializationValue\n );\n\n "+g+"\n } else if ("+(3===m)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+u+", d),\n getValue(batch, xR, xC + 2 * "+u+", d),\n initializationValue\n );\n\n "+g+"\n }\n }\n setOutput("+d+");\n }\n ";}}}(),Mi=function(){return function(t,e,n){if(this.variableNames=["x"],"avg"===e&&n)throw new Error("Cannot compute positions for average pool.");var r=t.filterWidth,o=t.strideDepth,a=t.strideHeight,i=t.strideWidth,u=t.dilationDepth,s=t.dilationHeight,c=t.dilationWidth,l=t.effectiveFilterDepth,h=t.effectiveFilterHeight,f=t.effectiveFilterWidth,p=t.padInfo.front,d=t.padInfo.top,v=t.padInfo.left;this.outputShape=t.outShape;var m="avg"===e,g="0.0";if(m||(g="-1.0 / 1e-20"),n)this.userCode="\n const ivec3 strides =\n ivec3("+o+", "+a+", "+i+");\n const ivec3 pads = ivec3("+p+", "+d+", "+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 < "+l+";\n wD += "+u+") {\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 += "+s+") {\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, 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 =\n wD * "+h+" * "+f+" +\n wR * "+f+" + wC;;\n }\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 x=4*Math.floor(r/4),b=r%4,w="\n if ("+m+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec3 strides =\n ivec3("+o+", "+a+", "+i+");\n const ivec3 pads = ivec3("+p+", "+d+", "+v+");\n const float initializationValue = "+g+";\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("+g+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wD = 0; wD < "+l+";\n wD += "+u+") {\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 += "+s+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+x+"; wC += 4) {\n int xC = xCCorner + wC * "+c+";\n\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n getValue(batch, xD, xR, xC + "+c+", ch),\n getValue(batch, xD, xR, xC + 2 * "+c+", ch),\n getValue(batch, xD, xR, xC + 3 * "+c+", ch)\n );\n\n "+w+"\n }\n\n int xC = xCCorner + "+x+";\n if ("+(1===b)+") {\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+w+"\n } else if ("+(2===b)+") {\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n getValue(batch, xD, xR, xC + "+c+", ch),\n initializationValue,\n initializationValue\n );\n\n "+w+"\n } else if ("+(3===b)+") {\n vec4 values = vec4(\n getValue(batch, xD, xR, xC, ch),\n getValue(batch, xD, xR, xC + "+c+", ch),\n getValue(batch, xD, xR, xC + 2 * "+c+", ch),\n initializationValue\n );\n\n "+w+"\n }\n }\n setOutput("+y+");\n }\n }\n ";}}}(),Bi=function(){return function(t,e){this.variableNames=["x"];var n=t.windowSize,r=t.batchSize,o=t.inSize,a=Math.ceil(o/n);this.outputShape=[r,a];var i="0.0",u="";"prod"===e?i="1.0":"min"===e?(i="1.0 / 1e-20",u="min"):"max"===e&&(i="-1.0 / 1e-20",u="max");var s=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"sum"===e?s="sumValue":"prod"===e?s="prodValue":"all"===e?s="allValue":"any"===e&&(s="anyValue");var c=4*Math.floor(n/4),l=n%4,h="\n if ("+("sum"===e)+") {\n sumValue += dot(values, ones);\n } else if ("+("prod"===e)+") {\n vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);\n prodValue *= tmp[0] * tmp[1];\n } else {\n minMaxValue = "+u+"(values, minMaxValue);\n }\n ",f="vec4";"all"===e?(i="1.0",h="\n bool reducedAllValue = all(values);\n float floatedReducedAllValue = float(reducedAllValue);\n allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);\n ",f="bvec4"):"any"===e&&(i="0.0",h="\n bool reducedAnyValue = any(values);\n float floatedReducedAnyValue = float(reducedAnyValue);\n anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);\n ",f="bvec4");var p="";o%n>0&&(p="\n if (inIdx < 0 || inIdx >= "+o+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+i+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float getValue(int batch, int inIdx) {\n "+p+"\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("+i+");\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 "+f+" values = "+f+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+h+"\n }\n\n int inIdx = inOffset + "+c+";\n if ("+(1===l)+") {\n "+f+" values = "+f+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+h+"\n } else if ("+(2===l)+") {\n "+f+" values = "+f+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+h+"\n } else if ("+(3===l)+") {\n "+f+" values = "+f+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+h+"\n }\n setOutput("+s+");\n }\n ";}}(),Pi=function(){return function(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t;for(var n="",r=0;r<4;r++){var o="thisRC = rc;";r%2==1&&(o+="thisRC.z += 1;"),r>1&&(o+="thisRC.y += 1;"),n+="\n "+o+"\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 "+jo(["r","c","d"],e)+"\n return ivec3(r, c, d);\n }\n \n "+Xo(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 ";}}();var Li=function(){return function(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,o=r[1],a=r[2],i=t.shape,u=i[1],s=i[2],c=[n&&u>1?o-1:o,n&&s>1?a-1:a],l=[n&&u>1?u-1:u,n&&s>1?s-1:s],h=c[0]/l[0],f=c[1]/l[1],p=1/h,d=1/f,v=2*Math.ceil(p)+2,m=2*Math.ceil(d)+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("+h+");\n const float widthScale = float("+f+");\n\n const float invHeightScale = float("+p+");\n const float invWidthScale = float("+d+");\n\n const int winHeight = int("+v+");\n const int winWidth = int("+m+");\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 >= "+u+") {\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 >= "+s+") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), "+(o-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 ";}}(),Wi=function(){return function(t,e,n,r){this.variableNames=["A"],this.outputShape=[];var o=t[0],a=t[1],i=t[2],u=t[3];this.outputShape=[o,e,n,u];var s=[r&&e>1?a-1:a,r&&n>1?i-1:i],c=[r&&e>1?e-1:e,r&&n>1?n-1:n];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+s[0]/c[0]+",\n "+s[1]/c[1]+");\n const vec2 inputShapeRC = vec2("+a+".0, "+i+".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 ";}}(),Ui=function(){return function(t,e,n,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];var o=t[0],a=t[1],i=t[2],u=t[3];this.outputShape=[o,e,n,u];var s=[r&&e>1?a-1:a,r&&n>1?i-1:i],c=[r&&e>1?e-1:e,r&&n>1?n-1:n];this.userCode="\n const vec3 effectiveInputOverOutputRatioRC = vec3(\n "+s[0]/c[0]+",\n "+s[1]/c[1]+",\n "+s[1]/c[1]+");\n const vec3 inputShapeRC = vec3("+a+".0, "+i+".0,\n "+i+".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 < "+(u-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 ";}}(),Vi=function(){return function(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,o=r[1],a=r[2],i=t.shape,u=i[1],s=i[2],c=[n&&u>1?o-1:o,n&&s>1?a-1:a],l=[n&&u>1?u-1:u,n&&s>1?s-1:s],h=c[0]/l[0],f=c[1]/l[1],p=1/h,d=1/f,v=2*Math.ceil(p)+2,m=2*Math.ceil(d)+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("+h+");\n const float widthScale = float("+f+");\n\n const float invHeightScale = float("+p+");\n const float invWidthScale = float("+d+");\n\n const int winHeight = int("+v+");\n const int winWidth = int("+m+");\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 >= "+u+") {\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 >= "+s+") {\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("+o+") - 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 ";}}(),zi=function(){return function(t,e,n,r){this.variableNames=["A"],this.outputShape=[];var o=t[0],a=t[1],i=t[2],u=t[3];this.outputShape=[o,e,n,u];var s=[r&&e>1?a-1:a,r&&n>1?i-1:i],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 "+s[0]/c[0]+",\n "+s[1]/c[1]+");\n const vec2 inputShapeRC = vec2("+a+".0, "+i+".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 ";}}(),Gi=function(){return 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(","),o=aa(n);this.userCode="\n void main() {\n "+o+" 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 ";}}(),Hi=function(){return function(t,e){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0;var n=t.length;if(n>4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");this.outputShape=t;var r=qo("rc",n),o=r[n-1]+" + 1 < "+this.outputShape[n-1],a=r[n-2]+" + 1 < "+this.outputShape[n-2],i=aa(n);function u(n){var r=t.map(function(r,o){return function(n,r){return -1!==e.indexOf(n)&&1!==t[n]?t[n]+" - "+r[n]+" - 1":""+r[n]}(o,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("+o+"){\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 "+i+" rc = getOutputCoords();\n vec4 result = vec4(0.);\n result.r = "+function(t){return u(t)}(r.slice())+";\n if("+o+"){\n result.g = "+function(t){return t[n-1]="("+t[n-1]+" + 1)",u(t)}(r.slice())+";\n }\n if("+a+") {\n result.b = "+function(t){return t[n-2]="("+t[n-2]+" + 1)",u(t)}(r.slice())+";\n if("+o+") {\n result.a = "+function(t){return t[n-1]="("+t[n-1]+" + 1)",t[n-2]="("+t[n-2]+" + 1)",u(t)}(r.slice())+";\n }\n }\n setOutput(result);\n }\n ";}}(),qi=function(){return function(t,e,n,r,o,a,i){this.variableNames=["updates","indices","defaultValue"],this.outputShape=a;var u=aa(o.length),s=aa(a.length),c="";1===n?c="i":2===n&&(c="i, j");var l="getIndices("+c+")",h="";1===r?h="i":2===r&&(h="i, coords[1]");var f="getUpdates("+h+")",p=e>1?"strides[j]":"strides";this.userCode="\n "+u+" strides = "+u+"("+o+");\n\n void main() {\n "+s+" 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 * "+p+";\n }\n if (flattenedIndex == coords[0]) {\n sum += "+f+";\n found = true;\n }\n }\n setOutput(mix(getDefaultValue(), sum, float(found)));\n }\n ";}}(),Ki=function(){return function(t,e){this.variableNames=["x","segmentIds"];var n=t.windowSize,r=t.batchSize,o=t.inSize,a=t.numSegments,i=a*Math.ceil(o/n);this.outputShape=[r,i];var u=4*Math.floor(n/4),s=n%4,c="\n sumValue += dot(values, segFilter);\n ",l="";o%n>0&&(l="\n if (inIdx < 0 || inIdx >= "+o+") {\n return initializationValue;\n }\n ");var h="";o%n>0&&(h="\n if (inIdx < 0 || inIdx >= "+o+") {\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 "+h+"\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 < "+u+"; 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 + "+u+";\n if ("+(1===s)+") {\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===s)+") {\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===s)+") {\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 ";}}(),ji=function(){return function(t,e,n){var r,o;if(this.variableNames=["c","a","b"],this.outputShape=e,n>4)throw Error("Where for rank "+n+" is not yet supported");if(1===n)o="resRC",r="resRC";else{for(var a=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],u=[],s=0;s= 1.0) {\n setOutput(getA("+o+"));\n } else {\n setOutput(getB("+o+"));\n }\n }\n ";}}(),Xi=function(){function t(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;var e,n=aa(this.rank),r="uniform int start["+this.rank+"];",o=function(t){if(1===t)return "sourceLoc";if(t<=6)return $i.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."+$i[e]+" = start["+e+"] + coords."+$i[e]+";"}).join("\n")+"\n ",this.userCode="\n "+r+"\n void main() {\n "+e+"\n setOutput(getSource("+o+"));\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}(),$i=["x","y","z","w","u","v"];var Yi=function(){function t(t){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.rank=t.length;var e=aa(this.rank),n=qo("coords",this.rank),r=qo("sourceLoc",this.rank),o=1===this.rank?"sourceLoc":"vec2("+r.slice(-2).join()+")",a="getChannel(getSource("+r.join()+"), "+o+")",i="\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 ",u=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 ",s=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 "+s+"\n vec4 result = vec4(0.);\n "+i+"\n "+u+"\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}(),Qi=function(){return function(t,e,n){this.variableNames=["x"],this.outputShape=n;var r=n.length,o=aa(n.length),a=aa(n.length),i="";if(1===r)i="coords * strides + begin";else{var u=0;i=n.map(function(t,e){return u++,1===n.length?"coords * strides["+e+"] + begin["+e+"]":"coords["+(u-1)+"] * strides["+e+"] + begin["+e+"]"}).join(",");}this.userCode="\n "+o+" begin = "+o+"("+t+");\n "+o+" strides = "+o+"("+e+");\n\n void main() {\n "+a+" coords = getOutputCoords();\n setOutput(getX("+i+"));\n }\n ";}}(),Ji=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,o=Zi(e,n),a=tu(t,o,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 i=this.freeTextures[a].shift();return this.usedTextures[a].push(i),i}return this.numUsedTextures++,this.log(),o===Bt.PACKED_2X2_FLOAT32?r=this.gpgpu.createPackedMatrixTexture(t[0],t[1]):o===Bt.PACKED_2X2_FLOAT16?r=this.gpgpu.createFloat16PackedMatrixTexture(t[0],t[1]):o===Bt.UNPACKED_FLOAT32?r=this.gpgpu.createFloat32MatrixTexture(t[0],t[1]):o===Bt.UNPACKED_FLOAT16?r=this.gpgpu.createFloat16MatrixTexture(t[0],t[1]):o===Bt.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 o=tu(e,Zi(n,r),r);o in this.freeTextures||(this.freeTextures[o]=[]),this.freeTextures[o].push(t),this.numFreeTextures++,this.numUsedTextures--;var a=this.usedTextures[o],i=a.indexOf(t);if(i<0)throw new Error("Cannot release a texture that was never provided by this texture manager");a.splice(i,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 Zi(t,e){if(t===Mt.UPLOAD)return Bt.PACKED_2X2_FLOAT32;if(t===Mt.RENDER||null==t)return function(t){return a().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?t?Bt.PACKED_2X2_FLOAT32:Bt.UNPACKED_FLOAT32:t?Bt.PACKED_2X2_FLOAT16:Bt.UNPACKED_FLOAT16}(e);if(t===Mt.DOWNLOAD||t===Mt.PIXELS)return Bt.PACKED_4X1_UNSIGNED_BYTE;throw new Error("Unknown logical texture type "+t)}function tu(t,e,n){return t[0]+"_"+t[1]+"_"+e+"_"+n}var eu=function(){return 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=[],o=0;o6)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),o=0;o6)throw Error("Packed transpose for rank "+this.rank+" is not yet supported.");var o=aa(this.rank),a=Ho("rc",this.rank),i=new Array(this.rank);for(r=0;r= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n";var du="return -x;",vu="return ceil(x);",mu="return floor(x);",gu="return exp(x);",yu="return exp(x) - 1.0;",xu=uu+"\n return sin(x);\n",bu=uu+"\n return cos(x);\n",wu=uu+"\n if (abs(x) > 1.) {\n return NAN;\n }\n return asin(x);\n",Cu=uu+"\n if (abs(x) > 1.) {\n return NAN;\n }\n return acos(x);\n",Eu=uu+"\n return atan(x);\n",Ru=uu+"return log(x + sqrt(x * x + 1.0));",Iu=uu+"\n if (x < 1.0) return NAN;\n return log(x + sqrt(x * x - 1.0));",ku=uu+"\n if ((x < -1.0) || (x > 1.0)) return NAN;\n return (log(1.0 + x) - log(1.0 - x)) / 2.0;",Su="return x;",Au="return x;",Du="\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",Tu="\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",Nu="\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",Fu=function(){return 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 ";}}(),_u=function(){return function(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outputShape=t;var e=t.length,n=qo("rc",e),r=aa(e),o=function(t,e){if(1===t)return "rc";for(var n="",r=0;r0?this.gpgpu.beginQuery():{startMs:Y(),endMs:null}},o.prototype.endTimer=function(t){return a().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),t):(t.endMs=Y(),t)},o.prototype.getQueryTime=function(t){return n(this,void 0,void 0,function(){var e;return r(this,function(n){return a().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.waitForQueryAndGetTime(t)]:[2,(e=t).endMs-e.startMs]})})},o.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);}}},o.prototype.releaseGPUData=function(t){var e=this.texData.get(t),n=e.texture,r=e.dtype,o=e.texShape,a=e.usage,i=e.isPacked,u=e.slice,s=u&&u.origDataId||t,c=this.dataRefCount.get(s);c>1?this.dataRefCount.set(s,c-1):(this.dataRefCount.delete(s),null!=n&&(this.numBytesInGPU-=this.computeBytes(o,r),this.textureManager.releaseTexture(n,o,a,i)));var l=this.texData.get(t);l.texture=null,l.texShape=null,l.isPacked=!1,l.slice=null;},o.prototype.getTexture=function(t){return this.uploadToGPU(t),this.texData.get(t).texture},o.prototype.getDataInfo=function(t){return this.texData.get(t)},o.prototype.getCPUBackend=function(){return a().getBool("WEBGL_CPU_FORWARD")?(null==this.cpuBackend&&(this.cpuBackend=Nt.findBackend("cpu")),this.cpuBackend):null},o.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&&t.sizea().getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){var o=Math.floor(t.length/2),i=this.concat(t.slice(0,o),e),u=this.concat(t.slice(o),e);return this.concat([i,u],e)}if(a().getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&t[0].rank>1){var s=new Sa(t.map(function(t){return t.shape}),e);return this.compileAndRun(s,t)}var c=bn(t.map(function(t){return t.shape}),e),l=t.map(function(t){return t.as2D(-1,w(t.shape.slice(e)))}),h=new ka(l.map(function(t){return t.shape}));return this.compileAndRun(h,l).reshape(c)},o.prototype.neg=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.neg(t);if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,du,t.dtype);var e=new iu(t.shape,du);return this.compileAndRun(e,[t])},o.prototype.batchMatMul=function(t,e,n,r){var o=n?t.shape[2]:t.shape[1],a=r?e.shape[1]:e.shape[2],i=n?t.shape[1]:t.shape[2],u=t.shape[0];if((1===o||1===a)&&i>1e3){n&&(t=t.transpose([0,2,1])),r&&(e=e.transpose([0,2,1]));var s=1===a?t:t.as3D(u,i,1),c=1===a?2:1,l=1===a?e.as3D(u,1,i):e;return this.multiply(s,l).sum(c,!0)}var h=Ct(t.dtype,e.dtype),f=new Ai(t.shape,[u,o,a],n,r);return this.compileAndRun(f,[t,e],h)},o.prototype.fusedBatchMatMul=function(t){var e=t.a,n=t.b,r=t.transposeA,o=t.transposeB,a=t.bias,i=t.activation,u=t.preluActivationWeights,s=r?e.shape[2]:e.shape[1],c=o?n.shape[1]:n.shape[2],l=e.shape[0],h=Ct(e.dtype,n.dtype),f=null!=a,p=null!=u,d=i?Mu(i,!0):null,v=new Ai(e.shape,[l,s,c],r,o,f,d,p),m=[e,n];return a&&m.push(a),u&&m.push(u),this.compileAndRun(v,m,h)},o.prototype.multiply=function(t,e){if("complex64"===t.dtype){var n=this.texData.get(t.dataId),r=this.texData.get(e.dataId),o=new va(pa,t.shape,e.shape),i=new va(da,t.shape,e.shape),u=[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(o,u),c=this.compileAndRun(i,u),l=this.complex(s,c);return s.dispose(),c.dispose(),l}if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.multiply(t,e);if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,ya,t.dtype);var h=new ba(ya,t.shape,e.shape);return this.compileAndRun(h,[t,e],t.dtype)},o.prototype.batchNormalization=function(t,e,n,r,o,i){var u=[t,e,n],s=null;null!=i&&(s=i.shape,u.push(i));var c=null;if(null!=o&&(c=o.shape,u.push(o)),a().getBool("WEBGL_PACK_NORMALIZATION")){var l=new fa(t.shape,e.shape,n.shape,s,c,r);return this.compileAndRun(l,u)}var h=new ha(t.shape,e.shape,n.shape,s,c,r);return this.compileAndRun(h,u)},o.prototype.localResponseNormalization4D=function(t,e,n,r,o){var i=a().getBool("WEBGL_PACK_NORMALIZATION")?new Ii(t.shape,e,n,r,o):new Ei(t.shape,e,n,r,o);return this.compileAndRun(i,[t])},o.prototype.LRNGrad=function(t,e,n,r,o,a,i){var u=new Ri(e.shape,r,o,a,i);return this.compileAndRun(u,[e,n,t])},o.prototype.tile=function(t,e){if("string"===t.dtype){var n=this.readSync(t.dataId).map(function(t){return Z(t)});return Lo(ir(t.shape,t.dtype,n),e)}var r=new eu(t.shape,e);return this.compileAndRun(r,[t])},o.prototype.pad=function(t,e,n){var r=a().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new _i(t.shape,e,n):new Fi(t.shape,e,n);return this.compileAndRun(r,[t])},o.prototype.transpose=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.transpose(t,e);var n=a().getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new ru(t.shape,e):new nu(t.shape,e);return this.compileAndRun(n,[t])},o.prototype.gather=function(t,e,n){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.gather(t,e,n);var r=new ti(t.shape,e.size,n);return this.compileAndRun(r,[t,e])},o.prototype.batchToSpaceND=function(t,e,n){g(t.rank<=4,function(){return "batchToSpaceND for rank > 4 with a WebGL backend not implemented yet"});var r=e.reduce(function(t,e){return t*e}),o=Mr(t.shape,e,r),a=Br(o.length,e.length),i=Pr(t.shape,e,r),u=Lr(n,e.length),s=Wr(i,n,e.length);return t.reshape(o).transpose(a).reshape(i).slice(u,s)},o.prototype.spaceToBatchND=function(t,e,n){g(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}),o=[[0,0]];o.push.apply(o,n);for(var a=1+e.length;ae||n===t?r=!0:n=H(t,n+1);return n}(i,o),s=new Ki({windowSize:u,inSize:i,batchSize:a,numSegments:o},e),c=this.compileAndRun(s,[t,n],r);return c.shape[1]===o?c:(n=Wn(0,o).tile([i/u]),this.segOpCompute(c,e,n,r,o))},o.prototype.argMinMaxReduce=function(t,e,n){var r=[e];if(vn("arg"+n.charAt(0).toUpperCase()+n.slice(1),r,t.rank),!a().getBool("WEBGL_PACK_REDUCE")||t.rank<=2){var o=pn(t.shape,r),i=o[0],u=w(o[1]),s=t.as2D(-1,u);return this.argReduce(s,n).reshape(i)}return this.argReducePacked(t,n)},o.prototype.argMin=function(t,e){return this.argMinMaxReduce(t,e,"min")},o.prototype.argMax=function(t,e){return this.argMinMaxReduce(t,e,"max")},o.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 o=new Ua(t.shape,n,r);return this.compileAndRun(o,[t])},o.prototype.equal=function(t,e){if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(equal(a, b));\n","bool");var n=new ba("return float(a == b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.notEqual=function(t,e){if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(notEqual(a, b));\n","bool");var n=new ba("return float(a != b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.less=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.less(t,e);if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(lessThan(a, b));\n","bool");var n=new ba("return float(a < b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.lessEqual=function(t,e){if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(lessThanEqual(a, b));\n","bool");var n=new ba("return float(a <= b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.greater=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.greater(t,e);if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(greaterThan(a, b));\n","bool");var n=new ba("return float(a > b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.greaterEqual=function(t,e){if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(greaterThanEqual(a, b));\n","bool");var n=new ba("return float(a >= b);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.logicalNot=function(t){var e=new iu(t.shape,"return float(!(x >= 1.0));");return this.compileAndRun(e,[t])},o.prototype.logicalAnd=function(t,e){if(a().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 ba("return float(a >= 1.0 && b >= 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.logicalOr=function(t,e){if(a().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 ba("return float(a >= 1.0 || b >= 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e],"bool")},o.prototype.select=function(t,e,n){var r=new ji(t.rank,e.shape,e.rank);return this.compileAndRun(r,[t,e,n],Ct(e.dtype,n.dtype))},o.prototype.where=function(t){an("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");var e=t.dataSync();return Uo(t.shape,e)},o.prototype.topk=function(t,e,n){return Wo(t.dataSync(),t.shape,t.dtype,e)},o.prototype.min=function(t,e){vn("min",e,t.rank);var n=pn(t.shape,e),r=n[0],o=w(n[1]),a=t.as2D(-1,o);return this.reduce(a,"min",a.dtype).reshape(r)},o.prototype.minimum=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.minimum(t,e);var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("\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 ba("\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])},o.prototype.mod=function(t,e){var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("\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 ba("if (b == 0.0) return NAN;\n return mod(a, b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},o.prototype.max=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.max(t,e);vn("max",e,t.rank);var n=pn(t.shape,e),r=n[0],o=w(n[1]),a=t.as2D(-1,o);return this.reduce(a,"max",a.dtype).reshape(r)},o.prototype.maximum=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.maximum(t,e);var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("\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 ba("\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])},o.prototype.all=function(t,e){vn("all",e,t.rank);var n=pn(t.shape,e),r=n[0],o=w(n[1]),a=t.as2D(-1,o);return this.reduce(a,"all",a.dtype).reshape(r)},o.prototype.any=function(t,e){vn("any",e,t.rank);var n=pn(t.shape,e),r=n[0],o=w(n[1]),a=t.as2D(-1,o);return this.reduce(a,"any",a.dtype).reshape(r)},o.prototype.squaredDifference=function(t,e){var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("return (a - b) * (a - b);",t.shape,e.shape):new ba("return (a - b) * (a - b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},o.prototype.realDivide=function(t,e){if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS")){return this.packedBinaryOp(t,e,"\n // vec4 one = vec4(equal(a, b));\n // return one + (vec4(1.0) - one) * a / b;\n vec4 result = a / b;\n if(a.x == b.x) {\n result.x = 1.;\n }\n if(a.y == b.y) {\n result.y = 1.;\n }\n if(a.z == b.z) {\n result.z = 1.;\n }\n if(a.w == b.w) {\n result.w = 1.;\n }\n\n return result;\n","float32",!0)}var n=new ba("\nif (a == b) {\n return 1.0;\n};\nreturn a / b;",t.shape,e.shape);return this.compileAndRun(n,[t,e],"float32")},o.prototype.floorDiv=function(t,e){if(a().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 ba("\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")},o.prototype.add=function(t,e){if("complex64"===t.dtype&&"complex64"===e.dtype)return this.complexSeparableBinaryOp(t,e,ma);if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.add(t,e);var n=Ct(t.dtype,e.dtype);if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,ma,n);var r=new ba(ma,t.shape,e.shape);return this.compileAndRun(r,[t,e],n)},o.prototype.packedUnaryOp=function(t,e,n){var r=new Fu(t.shape,e);return this.compileAndRun(r,[t],n)},o.prototype.packedBinaryOp=function(t,e,n,r,o){void 0===o&&(o=!1);var a=new Ca(n,t.shape,e.shape,o);return this.compileAndRun(a,[t,e],r)},o.prototype.complexSeparableBinaryOp=function(t,e,n){var r=this,o=this.texData.get(t.dataId),a=this.texData.get(e.dataId),i=[[o.complexTensors.real,a.complexTensors.real],[o.complexTensors.imag,a.complexTensors.imag]].map(function(o){var a=o[0],i=o[1],u=r.makeComplexComponentTensorInfo(t,a),s=r.makeComplexComponentTensorInfo(e,i),c=new ba(n,t.shape,e.shape);return r.compileAndRun(c,[u,s],Ct(a.dtype,i.dtype))}),u=i[0],s=i[1],c=this.complex(u,s);return u.dispose(),s.dispose(),c},o.prototype.makeComplexComponentTensorInfo=function(t,e){return {dataId:e.dataId,dtype:e.dtype,shape:t.shape}},o.prototype.addN=function(t){if(1===t.length)return t[0];if(t.length>a().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 o=t.map(function(t){return t.dtype}).reduce(function(t,e){return Ct(t,e)}),i=t.map(function(t){return t.shape}),u=a().getBool("WEBGL_PACK")?new zo(t[0].shape,i):new Vo(t[0].shape,i);return this.compileAndRun(u,t,o)},o.prototype.subtract=function(t,e){if("complex64"===t.dtype&&"complex64"===e.dtype)return this.complexSeparableBinaryOp(t,e,ga);if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.subtract(t,e);var n=Ct(t.dtype,e.dtype);if(a().getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,ga,t.dtype);var r=new ba(ga,t.shape,e.shape);return this.compileAndRun(r,[t,e],n)},o.prototype.pow=function(t,e){var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("\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 ba("\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=Ct(t.dtype,e.dtype);return this.compileAndRun(n,[t,e],r)},o.prototype.ceil=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.ceil(t);if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,vu,t.dtype);var e=new iu(t.shape,vu);return this.compileAndRun(e,[t])},o.prototype.floor=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.floor(t);if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,mu,t.dtype);var e=new iu(t.shape,mu);return this.compileAndRun(e,[t])},o.prototype.sign=function(t){var e=new iu(t.shape,"\n if (isnan(x)) { return 0.0; }\n return sign(x);\n");return this.compileAndRun(e,[t])},o.prototype.isNaN=function(t){var e=new iu(t.shape,"return float(isnan(x));");return this.compileAndRun(e,[t],"bool")},o.prototype.isInf=function(t){var e=new iu(t.shape,"return float(isinf(x));");return this.compileAndRun(e,[t],"bool")},o.prototype.isFinite=function(t){var e=new iu(t.shape,"return float(!isnan(x) && !isinf(x));");return this.compileAndRun(e,[t],"bool")},o.prototype.round=function(t){var e=new iu(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])},o.prototype.exp=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.exp(t);if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,gu,t.dtype);var e=new iu(t.shape,gu);return this.compileAndRun(e,[t])},o.prototype.expm1=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.expm1(t);if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,yu,t.dtype);var e=new iu(t.shape,yu);return this.compileAndRun(e,[t])},o.prototype.log=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.log(t);if(a().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 iu(t.shape,"if (x < 0.0) return NAN;\n return log(x);");return this.compileAndRun(e,[t])},o.prototype.log1p=function(t){var e=new iu(t.shape,"return log(1.0 + x);");return this.compileAndRun(e,[t])},o.prototype.sqrt=function(t){var e=new iu(t.shape,"return sqrt(x);");return this.compileAndRun(e,[t])},o.prototype.rsqrt=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.rsqrt(t);var e=new iu(t.shape,"return inversesqrt(x);");return this.compileAndRun(e,[t])},o.prototype.reciprocal=function(t){var e=new iu(t.shape,"return 1.0 / x;");return this.compileAndRun(e,[t])},o.prototype.relu=function(t){var e;return e=a().getBool("WEBGL_PACK")?new Fu(t.shape,Du):new iu(t.shape,lu),this.compileAndRun(e,[t])},o.prototype.relu6=function(t){var e;return e=a().getBool("WEBGL_PACK")?new Fu(t.shape,Tu):new iu(t.shape,hu),this.compileAndRun(e,[t])},o.prototype.prelu=function(t,e){var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca(wa,t.shape,e.shape):new ba(xa,t.shape,e.shape);return this.compileAndRun(n,[t,e])},o.prototype.elu=function(t){if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,Nu,t.dtype);var e=new iu(t.shape,fu);return this.compileAndRun(e,[t])},o.prototype.eluDer=function(t,e){var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("\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 ba("return (b >= 1.0) ? a : a * (b + 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},o.prototype.selu=function(t){var e=new iu(t.shape,pu);return this.compileAndRun(e,[t])},o.prototype.int=function(t){var e=new iu(t.shape,"return float(int(x));");return this.compileAndRun(e,[t],"int32")},o.prototype.clip=function(t,e,n){var r,o=(r=a().getBool("WEBGL_PACK_CLIP")?new Ra(t.shape):new Ea(t.shape)).getCustomSetupFunc(e,n);return this.compileAndRun(r,[t],null,o)},o.prototype.abs=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.abs(t);if(a().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(t,cu,t.dtype);var e=new iu(t.shape,cu);return this.compileAndRun(e,[t])},o.prototype.complexAbs=function(t){var e=this.texData.get(t.dataId),n=new Ia(t.shape),r=[this.makeComplexComponentTensorInfo(t,e.complexTensors.real),this.makeComplexComponentTensorInfo(t,e.complexTensors.imag)];return this.compileAndRun(n,r)},o.prototype.sigmoid=function(t){var e=new iu(t.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(e,[t])},o.prototype.softplus=function(t){var e=new iu(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])},o.prototype.sin=function(t){var e=new iu(t.shape,xu);return this.compileAndRun(e,[t])},o.prototype.cos=function(t){var e=new iu(t.shape,bu);return this.compileAndRun(e,[t])},o.prototype.tan=function(t){var e=new iu(t.shape,"return tan(x);");return this.compileAndRun(e,[t])},o.prototype.asin=function(t){var e=new iu(t.shape,wu);return this.compileAndRun(e,[t])},o.prototype.acos=function(t){var e=new iu(t.shape,Cu);return this.compileAndRun(e,[t])},o.prototype.atan=function(t){var e=new iu(t.shape,Eu);return this.compileAndRun(e,[t])},o.prototype.atan2=function(t,e){var n=a().getBool("WEBGL_PACK_BINARY_OPERATIONS")?new Ca("\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 ba("\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])},o.prototype.sinh=function(t){var e=new iu(t.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},o.prototype.cosh=function(t){var e=new iu(t.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},o.prototype.tanh=function(t){var e=new iu(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])},o.prototype.asinh=function(t){var e=new iu(t.shape,Ru);return this.compileAndRun(e,[t])},o.prototype.acosh=function(t){var e=new iu(t.shape,Iu);return this.compileAndRun(e,[t])},o.prototype.atanh=function(t){var e=new iu(t.shape,ku);return this.compileAndRun(e,[t])},o.prototype.erf=function(t){var e=new iu(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])},o.prototype.step=function(t,e){var n=new iu(t.shape,function(t){return void 0===t&&(t=0),uu+"\n return x > 0.0 ? 1.0 : float("+t+");\n "}(e));return this.compileAndRun(n,[t])},o.prototype.conv2dByMatMul=function(t,e,n,r,o,i){var u=t.shape,s=this.texData.get(t.dataId),c=n.inChannels,l=u[0]*u[1]*u[2],h=n.outChannels,f="channelsLast"===n.dataFormat,p=(1===l||1===h)&&c>1e3,d=u[2]%2!=0&&!!s.isPacked;if(p||!a().getBool("WEBGL_LAZILY_UNPACK")||!a().getBool("WEBGL_PACK_BINARY_OPERATIONS")||!d){var v=f?u[0]*u[1]*u[2]:u[0]*u[2]*u[3],m=this.reshape(t,[1,v,n.inChannels]),y=this.reshape(e,[1,n.inChannels,n.outChannels]);return this.reshape(this.fusedBatchMatMul({a:m,b:y,transposeA:!1,transposeB:!1,bias:r,activation:o,preluActivationWeights:i}),n.outShape)}var x=f?u[0]*u[1]*(u[2]+1):u[0]*u[2]*(u[3]+1),b={dataId:t.dataId,shape:[1,x,n.inChannels],dtype:t.dtype},w=s.shape;s.shape=s.shape.slice(),s.shape[s.shape.length-2]++,g(ke(s.shape,b.shape),function(){return "packed reshape "+s.shape+" to "+b.shape+" isn't free"});var C=this.reshape(e,[1,n.inChannels,n.outChannels]),E=this.fusedBatchMatMul({a:b,b:C,transposeA:!1,transposeB:!1,bias:r,activation:o,preluActivationWeights:i}),R=this.texData.get(E.dataId);return g(R.isPacked,function(){return "batchMatMul result is expected to be packed"}),s.shape=w,R.shape=n.outShape,Nt.makeTensorFromDataId(E.dataId,n.outShape,E.dtype)},o.prototype.conv2dWithIm2Row=function(t,e,n,r,o,a){var i=n.filterWidth,u=n.filterHeight,s=n.inChannels,c=n.outWidth,l=n.outHeight,h="channelsLast"===n.dataFormat,f=i*u*s,p=l*c,d=[f,p],v=t.squeeze([0]),m=e.reshape([1,f,-1]),g=new Ci(d,v.shape,n),y=this.compileAndRun(g,[v]).reshape([1,d[0],d[1]]),x=null!=r,b=null!=a,w=o?Mu(o,!0):null,C=new Ai(y.shape,[1,p,n.outChannels],!0,!1,x,w,b),E=[y,m];r&&E.push(r),b&&E.push(a);var R=this.compileAndRun(C,E);return h?R.reshape([1,l,c,n.outChannels]):R.reshape([1,n.outChannels,l,c])},o.prototype.fusedConv2d=function(t){var e=t.input,n=t.filter,r=t.convInfo,o=t.bias,i=t.activation,u=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,o,i,u);if(a().getBool("WEBGL_CONV_IM2COL")&&1===e.shape[0])return this.conv2dWithIm2Row(e,n,r,o,i,u);var s=null!=o,c=null!=u,l=i?Mu(i,!1):null,h=new Ma(r,s,l,c),f=[e,n];return o&&f.push(o),u&&f.push(u),this.compileAndRun(h,f)},o.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(a().getBool("WEBGL_CONV_IM2COL")&&1===t.shape[0])return this.conv2dWithIm2Row(t,e,n);var r=new Ma(n);return this.compileAndRun(r,[t,e])},o.prototype.conv2dDerInput=function(t,e,n){var r=new Ta(n);return this.compileAndRun(r,[t,e])},o.prototype.conv2dDerFilter=function(t,e,n){var r=new Da(n);return this.compileAndRun(r,[t,e])},o.prototype.fusedDepthwiseConv2D=function(t){var e,n=t.input,r=t.filter,o=t.convInfo,i=t.bias,u=t.activation,s=t.preluActivationWeights,c=a().getBool("WEBGL_PACK_DEPTHWISECONV")&&o.strideWidth<=2&&o.outChannels/o.inChannels==1,l=u?Mu(u,c):null,h=[n,r],f=null!=i,p=null!=s;return f&&h.push(i),p&&h.push(s),c?(e=new La(o,f,l,p),this.compileAndRun(e,h)):(e=new Pa(o,f,l,p),this.compileAndRun(e,h))},o.prototype.depthwiseConv2D=function(t,e,n){var r;return a().getBool("WEBGL_PACK_DEPTHWISECONV")&&n.strideWidth<=2&&n.outChannels/n.inChannels==1?(r=new La(n),this.compileAndRun(r,[t,e])):(r=new Pa(n),this.compileAndRun(r,[t,e]))},o.prototype.depthwiseConv2DDerInput=function(t,e,n){var r=new Oa(n);return this.compileAndRun(r,[t,e])},o.prototype.depthwiseConv2DDerFilter=function(t,e,n){var r=new _a(n);return this.compileAndRun(r,[t,e])},o.prototype.conv3d=function(t,e,n){var r=new Ba(n);return this.compileAndRun(r,[t,e])},o.prototype.conv3dDerInput=function(t,e,n){var r=new Fa(n);return this.compileAndRun(r,[t,e])},o.prototype.conv3dDerFilter=function(t,e,n){var r=new Na(n);return this.compileAndRun(r,[t,e])},o.prototype.maxPool=function(t,e){var n=new Oi(e,"max",!1);return this.compileAndRun(n,[t])},o.prototype.avgPool=function(t,e){var n=new Oi(e,"avg",!1);return this.compileAndRun(n,[t],"float32")},o.prototype.maxPoolBackprop=function(t,e,n,r){var o=new Oi(r,"max",!0),a=this.compileAndRun(o,[e]),i=new ki(r),u=this.compileAndRun(i,[t,a],e.dtype);return a.dispose(),u},o.prototype.avgPoolBackprop=function(t,e,n){var r=new ca(n);return this.compileAndRun(r,[t],e.dtype)},o.prototype.cast=function(t,e){return So(t,e,this)},o.prototype.unstack=function(t,e){for(var n=t.shape[e],r=new Array(t.rank-1),o=0,a=0;a1,function(){return "blockSize should be > 1 for depthToSpace, but was: "+e});var r=t.shape[0],o="NHWC"===n?t.shape[1]:t.shape[2],a="NHWC"===n?t.shape[2]:t.shape[3],i="NHWC"===n?t.shape[3]:t.shape[1],u=o*e,s=a*e,c=i/(e*e),l=new Ha("NHWC"===n?[r,u,s,c]:[r,c,u,s],e,n);return this.compileAndRun(l,[t])},o.prototype.split=function(t,e,n){return Po(t,e,n)},o.prototype.scatterND=function(t,e,n){var r=Hr(0,t,n),o=r.sliceRank,a=r.numUpdates,i=r.sliceSize,u=r.strides,s=r.outputSize,c=[s/i,i],l=t.reshape([a,o]),h=e.reshape([a,i]);if(0===s)return Ao(In([]),n);var f=Sn(0),p=new qi(a,o,l.rank,h.rank,u,c);return this.compileAndRun(p,[h,l,f]).reshape(n)},o.prototype.sparseToDense=function(t,e,n,r){var o=Hr(0,t,n),a=o.sliceRank,i=o.numUpdates,u=o.strides,s=o.outputSize,c=new qi(i,a,t.rank,e.rank,u,[s,1],!1);return this.compileAndRun(c,[e,t,r]).reshape(n)},o.prototype.fft=function(t){return this.fftImpl(t,!1)},o.prototype.ifft=function(t){return this.fftImpl(t,!0)},o.prototype.fftImpl=function(t,e){var n=this.texData.get(t.dataId),r=new Ja(Ya,t.shape,e),o=new Ja(Qa,t.shape,e),a=[this.makeComplexComponentTensorInfo(t,n.complexTensors.real),this.makeComplexComponentTensorInfo(t,n.complexTensors.imag)],i=this.compileAndRun(r,a),u=this.compileAndRun(o,a),s=this.complex(i,u).as2D(t.shape[0],t.shape[1]);return i.dispose(),u.dispose(),s},o.prototype.gatherND=function(t,e){var n=e.shape,r=n[n.length-1],o=Ur(t,e),a=o[0],i=o[1],u=o[2],s=o[3],c=e.reshape([i,r]),l=t.reshape([t.size/u,u]),h=new ei(r,s,[i,u]);return this.compileAndRun(h,[l,c]).reshape(a)},o.prototype.fill=function(t,e,n){if("string"===(n=n||z(e))){var r=F(n,w(t));return r.fill(e),Nt.makeTensor(r,t,n,this)}var o=new Za(t,e),a=o.getCustomSetupFunc(e);return this.compileAndRun(o,[],n,a)},o.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)},o.prototype.zerosLike=function(t){return this.fill(t.shape,"string"===t.dtype?"":0,t.dtype)},o.prototype.linspace=function(t,e,n){return Do(t,e,n)},o.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}},o.prototype.makeOutput=function(t,e){var n=this.makeTensorInfo(t,e).dataId;return Nt.makeTensorFromDataId(n,t,e,this)},o.prototype.unpackTensor=function(t){var e=new _u(t.shape);return this.runWebGLProgram(e,[t],t.dtype)},o.prototype.packTensor=function(t){var e=new Ni(t.shape);return this.runWebGLProgram(e,[t],t.dtype,null,!0)},o.prototype.packedReshape=function(t,e){var n=[we(t.shape)].concat(Ce(t.shape)),r={dtype:t.dtype,shape:n,dataId:t.dataId},o=[we(e)].concat(Ce(e)),a=new Pi(o,n),i=this.runWebGLProgram(a,[r],t.dtype,null,!0);return {dataId:i.dataId,shape:e,dtype:i.dtype}},o.prototype.decode=function(t){var e,n=this.texData.get(t),r=n.isPacked,o=n.shape,a=n.dtype,i=Ee(o);e=r?new Ga(i):new za(i);return {dtype:a,shape:o,dataId:this.runWebGLProgram(e,[{shape:i,dtype:a,dataId:t}],a,null,!0).dataId}},o.prototype.runWebGLProgram=function(t,e,n,r,o){var i=this;void 0===o&&(o=!1);var u=this.makeTensorInfo(t.outputShape,n),s=this.texData.get(u.dataId);if(t.packedOutput&&(s.isPacked=!0),t.outPackingScheme===Ot.DENSE){var c=zt(t.outputShape);s.texShape=c.map(function(t){return 2*t});}if(null!=t.outTexUsage&&(s.usage=t.outTexUsage),0===w(u.shape))return s.values=N(u.dtype,0),u;var l=[],h=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=i.texData.get(e.dataId);if(null==n.texture){if(!t.packedInputs&&w(e.shape)<=a().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?i.unpackTensor(e):i.packTensor(e),l.push(e),n=i.texData.get(e.dataId);else if(n.isPacked&&!ke(n.shape,e.shape)){var r=e,o=e.shape;e.shape=n.shape,e=i.packedReshape(e,o),l.push(e),n=i.texData.get(e.dataId),r.shape=o;}return i.uploadToGPU(e.dataId),{shape:e.shape,texData:n,isUniform:!1}});this.uploadToGPU(u.dataId);var f,p={shape:u.shape,texData:s,isUniform:!1},d=function(t,e,n){var r="";e.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 o=t.userCode,a=t.constructor.name;return a+="_"+r+"_"+o}(t,h,p),v=this.getAndSaveBinary(d,function(){return function(t,e,n,r){var o=e.userCode,i=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}}),u=i.map(function(t){return t.shapeInfo}),s={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1,isPacked:r.texData.isPacked,flatOffset:null},c=Yo(i,s,o,e.packedInputs),l=t.createProgram(c),h=null,f=t.getUniformLocation(l,"NAN",!1);1===a().getNumber("WEBGL_VERSION")&&(h=t.getUniformLocation(l,"INFINITY",!1));for(var p={},d=0;d0)return 32}return 16})),this.floatPrecisionValue},o.prototype.epsilon=function(){return 32===this.floatPrecision()?1e-7:1e-4},o.prototype.uploadToGPU=function(t){var e,n=this.texData.get(t),r=n.shape,o=n.dtype,a=n.values,i=n.texture,u=n.usage,s=n.isPacked;if(null==i){var c,l=null!=this.activeTimers;l&&(c=Y());var h=n.texShape;if(null==h&&(h=Re(r,s),n.texShape=h),null!=a){var f=Ee(r),p=void 0,d=h[1],v=h[0],m=a instanceof Uint8Array;s?(d=(e=Gt(h[0],h[1]))[0],v=e[1],p=new $a(f,[v,d],m)):p=new Xa(f,[v,d],m);var g=this.makeTensorInfo([v,d],o);this.texData.get(g.dataId).usage=m?Mt.PIXELS:Mt.UPLOAD,this.gpgpu.uploadDenseMatrixToTexture(this.getTexture(g.dataId),d,v,a);var y=this.runWebGLProgram(p,[g],o,null,!0),x=this.texData.get(y.dataId);n.texture=x.texture,n.texShape=x.texShape,n.isPacked=x.isPacked,n.usage=x.usage,this.disposeData(g.dataId),this.texData.delete(y.dataId),n.values=null,l&&(this.uploadWaitMs+=Y()-c);}else{var b=this.acquireTexture(h,u,o,s);n.texture=b;}}},o.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 o=(this.numBytesInGPU/1024/1024).toFixed(2);this.warnedAboutMemory=!0,console.warn("High memory usage in GPU: "+o+" MB, most likely due to a memory leak");}return this.textureManager.acquireTexture(t,e,r)},o.prototype.computeBytes=function(t,e){return t[0]*t[1]*P(e)},o}(co);Ft()&&Nt.registerBackend("webgl",function(){return new Pu},2);var Lu=wn({square_:function(t){var e=cn(t,"x","square"),n=[e];return Nt.runKernelFunc(function(t,n){return n([e]),t.square(e)},{x:e},function(t,e){var n=e[0];return {x:function(){return t.mul(n.toFloat().mul(2))}}},"Square",{},n,[])}});var Wu=wn({abs_:function(t){var e=cn(t,"x","abs");return "complex64"===e.dtype?Nt.runKernelFunc(function(t){return t.complexAbs(e)},{$x:e}):Nt.runKernelFunc(function(t,n){var r=t.abs(e);return n([e]),r},{x:e},function(t,e){var n=e[0];return {x:function(){return t.mul(n.toFloat().step(-1))}}},"Abs")}}),Uu=wn({acos_:function(t){var e=cn(t,"x","acos");return Nt.runKernelFunc(function(t,n){var r=t.acos(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.divStrict(Sn(1).sub(n.toFloat().square()).sqrt()).neg()}}})}}),Vu=wn({acosh_:function(t){var e=cn(t,"x","acosh");return Nt.runKernelFunc(function(t,n){var r=t.acosh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.divStrict(n.toFloat().square().sub(1).sqrt())}}})}}),zu=wn({asin_:function(t){var e=cn(t,"x","asin");return Nt.runKernelFunc(function(t,n){var r=t.asin(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.divStrict(Sn(1).sub(n.toFloat().square()).sqrt())}}})}}),Gu=wn({asinh_:function(t){var e=cn(t,"x","asinh");return Nt.runKernelFunc(function(t,n){var r=t.asinh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.divStrict(Sn(1).add(n.toFloat().square()).sqrt())}}})}}),Hu=wn({atan_:function(t){var e=cn(t,"x","atan");return Nt.runKernelFunc(function(t,n){var r=t.atan(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.toFloat().square().add(1))}}})}}),qu=wn({atanh_:function(t){var e=cn(t,"x","atanh");return Nt.runKernelFunc(function(t,n){var r=t.atanh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(Sn(1).sub(n.toFloat().square()))}}})}}),Ku=wn({ceil_:function(t){var e=cn(t,"x","ceil");return Nt.runKernelFunc(function(t){return t.ceil(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),ju=wn({clipByValue_:function(t,e,n){var r=cn(t,"x","clipByValue");g(e<=n,function(){return "Error in clip: min ("+e+") must be less than or equal to max ("+n+")."});var o=[r],a={min:e,max:n};return Nt.runKernelFunc(function(t,o){var a=t.clip(r,e,n);return o([r]),a},{x:r},function(t,r){var o=r[0];return {x:function(){return t.where(o.greaterEqual(e).logicalAnd(o.lessEqual(n)),Vn(t))}}},"ClipByValue",a,o)}}),Xu=wn({cos_:function(t){var e=cn(t,"x","cos");return Nt.runKernelFunc(function(t,n){var r=t.cos(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return n.toFloat().sin().neg().mul(t)}}})}}),$u=wn({cosh_:function(t){var e=cn(t,"x","cosh");return Nt.runKernelFunc(function(t,n){var r=t.cosh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return n.toFloat().sinh().mulStrict(t)}}})}}),Yu=wn({erf_:function(t){var e=cn(t,"x","erf");return g("int32"===e.dtype||"float32"===e.dtype,function(){return "Input dtype must be `int32` or `float32`."}),"int32"===e.dtype&&(e=e.toFloat()),Nt.runKernelFunc(function(t,n){var r=t.erf(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.mul(n.square().neg().exp().mul(2/Math.sqrt(Math.PI)))}}})}}),Qu=wn({exp_:function(t){var e=cn(t,"x","exp");return Nt.runKernelFunc(function(t,n){var r=t.exp(e);return n([r]),r},{$x:e},function(t,e){return {$x:function(){return t.mulStrict(e[0])}}})}}),Ju=wn({expm1_:function(t){var e=cn(t,"x","expm1");return Nt.runKernelFunc(function(t,n){var r=t.expm1(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.mul(n.exp())}}})}}),Zu=wn({floor_:function(t){var e=cn(t,"x","floor");return Nt.runKernelFunc(function(t){return t.floor(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),ts=wn({log_:function(t){var e=cn(t,"x","log");return Nt.runKernelFunc(function(t,n){var r=t.log(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.toFloat())}}})}}),es=wn({log1p_:function(t){var e=cn(t,"x","log1p");return Nt.runKernelFunc(function(t,n){var r=t.log1p(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.add(1))}}})}}),ns=wn({logSigmoid_:function(t){var e=cn(t,"x","logSigmoid");return Nt.runKernelFunc(function(t,n){var r=t.softplus(e.neg()).neg();return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.mul(n.neg().sigmoid())}}})}}),rs=wn({neg_:function(t){var e=cn(t,"x","neg");return Nt.runKernelFunc(function(t){return t.neg(e)},{$x:e},function(t){return {$x:function(){return t.neg()}}})}}),os=wn({reciprocal_:function(t){var e=cn(t,"x","reciprocal");return Nt.runKernelFunc(function(t,n){var r=t.reciprocal(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.square().neg())}}})}}),as=wn({round_:function(t){var e=cn(t,"x","round");return Nt.runKernelFunc(function(t){return t.round(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),is=wn({rsqrt_:function(t){var e=cn(t,"x","rsqrt");return Nt.runKernelFunc(function(t,n){var r=t.rsqrt(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.pow(1.5).mul(2)).neg()}}})}}),us=wn({sigmoid_:function(t){var e=cn(t,"x","sigmoid");return Nt.runKernelFunc(function(t,n){var r=t.sigmoid(e);return n([r]),r},{x:e},function(t,e){var n=e[0];return {x:function(){return t.mul(n.mul(Sn(1).sub(n)))}}},"Sigmoid")}}),ss=wn({sign_:function(t){var e=cn(t,"x","sign");return Nt.runKernelFunc(function(t){return t.sign(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),cs=wn({isNaN_:function(t){var e=cn(t,"x","isNaN");return Nt.runKernelFunc(function(t){return t.isNaN(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),ls=wn({isInf_:function(t){var e=cn(t,"x","isInf");return Nt.runKernelFunc(function(t){return t.isInf(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),hs=wn({isFinite_:function(t){var e=cn(t,"x","isFinite");return Nt.runKernelFunc(function(t){return t.isFinite(e)},{$x:e},function(t){return {$x:function(){return Vn(t)}}})}}),fs=wn({sin_:function(t){var e=cn(t,"x","sin");return Nt.runKernelFunc(function(t,n){var r=t.sin(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return n.toFloat().cos().mul(t)}}})}}),ps=wn({sinh_:function(t){var e=cn(t,"x","sinh");return Nt.runKernelFunc(function(t,n){var r=t.sinh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return n.toFloat().cosh().mulStrict(t)}}})}}),ds=wn({softplus_:function(t){var e=cn(t,"x","softplus");return Nt.runKernelFunc(function(t,n){var r=t.softplus(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.mul(n.sigmoid())}}})}}),vs=wn({sqrt_:function(t){var e=cn(t,"x","sqrt");return Nt.runKernelFunc(function(t,n){var r=t.sqrt(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.toFloat().sqrt().mul(2))}}})}}),ms=wn({step_:function(t,e){void 0===e&&(e=0);var n=cn(t,"x","step");return Nt.runKernelFunc(function(t){return t.step(n,e)},{$x:n},function(t){return {$x:function(){return Vn(t)}}})}}),gs=wn({tan_:function(t){var e=cn(t,"x","tan");return Nt.runKernelFunc(function(t,n){var r=t.tan(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return t.div(n.cos().square())}}})}}),ys=wn({tanh_:function(t){var e=cn(t,"x","tanh");return Nt.runKernelFunc(function(t,n){var r=t.tanh(e);return n([r]),r},{$x:e},function(t,e){var n=e[0];return {$x:function(){return Sn(1).sub(n.square()).mulStrict(t)}}})}});function xs(t,e,n,r,o,a){var i,u,s=cn(t,"x","batchNorm"),c=cn(e,"mean","batchNorm"),l=cn(n,"variance","batchNorm");return null!=o&&(i=cn(o,"scale","batchNorm")),null!=r&&(u=cn(r,"offset","batchNorm")),g(2===s.rank,function(){return "Error in batchNorm3D: x must be rank 3 but got rank "+s.rank+"."}),g(2===c.rank||1===c.rank,function(){return "Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank "+c.rank+"."}),g(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!=i&&g(2===i.rank||1===i.rank,function(){return "Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank "+i.rank+"."}),null!=u&&g(2===u.rank||1===u.rank,function(){return "Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank "+u.rank+"."}),Cs(s,c,l,u,i,a)}function bs(t,e,n,r,o,a){var i,u,s=cn(t,"x","batchNorm"),c=cn(e,"mean","batchNorm"),l=cn(n,"variance","batchNorm");return null!=o&&(i=cn(o,"scale","batchNorm")),null!=r&&(u=cn(r,"offset","batchNorm")),g(3===s.rank,function(){return "Error in batchNorm3D: x must be rank 3 but got rank "+s.rank+"."}),g(3===c.rank||1===c.rank,function(){return "Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank "+c.rank+"."}),g(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!=i&&g(3===i.rank||1===i.rank,function(){return "Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank "+i.rank+"."}),null!=u&&g(3===u.rank||1===u.rank,function(){return "Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank "+u.rank+"."}),Cs(s,c,l,u,i,a)}function ws(t,e,n,r,o,a){var i,u,s=cn(t,"x","batchNorm"),c=cn(e,"mean","batchNorm"),l=cn(n,"variance","batchNorm");return null!=o&&(i=cn(o,"scale","batchNorm")),null!=r&&(u=cn(r,"offset","batchNorm")),g(4===s.rank,function(){return "Error in batchNorm4D: x must be rank 4 but got rank "+s.rank+"."}),g(4===c.rank||1===c.rank,function(){return "Error in batchNorm4D: mean must be rank 4 or rank 1 but got rank "+c.rank+"."}),g(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!=i&&g(4===i.rank||1===i.rank,function(){return "Error in batchNorm4D: scale must be rank 4 or rank 1 but got rank "+i.rank+"."}),null!=u&&g(4===u.rank||1===u.rank,function(){return "Error in batchNorm4D: offset must be rank 4 or rank 1 but got rank "+u.rank+"."}),Cs(s,c,l,u,i,a)}function Cs(t,e,n,r,o,a){null==a&&(a=.001);var i,u,s,c=cn(t,"x","batchNorm"),l=cn(e,"mean","batchNorm"),h=cn(n,"variance","batchNorm");null!=o&&(i=cn(o,"scale","batchNorm")),null!=r&&(u=cn(r,"offset","batchNorm")),g(l.rank===h.rank,function(){return "Batch normalization gradient requires mean and variance to have equal ranks."}),g(null==u||l.rank===u.rank,function(){return "Batch normalization gradient requires mean and offset to have equal ranks."}),g(null==i||l.rank===i.rank,function(){return "Batch normalization gradient requires mean and scale to have equal ranks."}),s=0===c.rank||1===c.rank?c.as4D(1,1,1,c.size):2===c.rank?c.as4D(1,1,c.shape[0],c.shape[1]):3===c.rank?c.as4D(1,c.shape[0],c.shape[1],c.shape[2]):c;var f=[c,l,h,i];return Nt.runKernelFunc(function(t,e){var n=t.batchNormalization(s,Es(l),Es(h),a,Es(i),Es(u));return e([c,l,h,i]),n},{x:c,mean:l,variance:h,scale:i,offset:u},function(t,e){var n=e,r=n[0],o=n[1],i=n[2],u=n[3],c=null==u?Sn(1):u,l=fo(o.shape,s.shape),h=[];if(1===o.rank){for(var f=0;f0&&(e=e.sum(n)),e.reshape(r.shape)},b:function(){var e=t,n=fo(o.shape,a);return n.length>0&&(e=e.sum(n)),e.reshape(o.shape)}}},"Add")}}),Us=wn({addN_:function(t){g(Array.isArray(t),function(){return "The argument passed to tf.addN() must be a list of tensors"}),g(t.length>=1,function(){return "Must pass at least one tensor to tf.addN(), but got "+t.length});var e=t.map(function(t,e){return cn(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(!C(t.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});var r=e;return Nt.runKernelFunc(function(t){return t.addN(e)},r,function(t){var n={};return e.forEach(function(e,r){n[r]=function(){return t.clone()};}),n},"AddN")}}),Vs=wn({addStrict_:function(t,e){var n=cn(t,"a","addStrict"),r=cn(e,"b","addStrict");return y(n.shape,r.shape,"Error in addStrict: "),n.add(r)}}),zs=wn({atan2_:function(t,e){var n,r=cn(t,"a","atan2"),o=cn(e,"b","atan2");n=Rt(r,o),r=n[0],o=n[1];var a=po(r.shape,o.shape);return Nt.runKernelFunc(function(t,e){var n=t.atan2(r,o);return e([r,o]),n},{$a:r,$b:o},function(t,e){var n=e[0],r=e[1];return {$a:function(){var e=Ws(n.square(),r.square()),o=t.mul(r.div(e)),i=fo(n.shape,a);return i.length>0&&(o=o.sum(i)),o.reshape(n.shape)},$b:function(){var e=Ws(n.square(),r.square()),o=rs(t.mul(n.div(e))),i=fo(r.shape,a);return i.length>0&&(o=o.sum(i)),o.reshape(r.shape)}}})}}),Gs=wn({div_:function(t,e){var n,r=cn(t,"a","div"),o=cn(e,"b","div");if(n=Rt(r,o),r=n[0],o=n[1],"int32"===r.dtype&&"int32"===o.dtype)return Ks(r,o);var a=po(r.shape,o.shape);return Nt.runKernelFunc(function(t,e){var n=t.realDivide(r,o);return e([r,o]),n},{a:r,b:o},function(t,e){var n=e[0],r=e[1];return {a:function(){var e=t.div(r.toFloat()),o=fo(n.shape,a);return o.length>0?e.sum(o).reshape(n.shape):e},b:function(){var e=t.mul(n.toFloat()),o=fo(r.shape,a);o.length>0&&(e=e.sum(o).reshape(r.shape));var i=r.square();return e.div(i.toFloat()).neg()}}},"Div")}}),Hs=wn({divNoNan_:function(t,e){var n,r=cn(t,"a","div"),o=cn(e,"b","div");r=(n=Rt(r,o))[0],o=n[1];var a=Gs(r,o),i=Vn(a),u=o.equal(i);return Ps(u,i,a)}}),qs=wn({divStrict_:function(t,e){var n=cn(t,"a","div"),r=cn(e,"b","div");return y(n.shape,r.shape,"Error in divideStrict: "),n.div(r)}}),Ks=wn({floorDiv_:function(t,e){var n,r=cn(t,"a","floorDiv"),o=cn(e,"b","floorDiv");n=Rt(r,o),r=n[0],o=n[1];var a=po(r.shape,o.shape);return Nt.runKernelFunc(function(t,e){var n=t.floorDiv(r,o);return e([r,o]),n},{a:r,b:o},function(t,e){var n=e[0],r=e[1];return {a:function(){var e=t.div(r.toFloat()),o=fo(n.shape,a);return o.length>0?e.sum(o).reshape(n.shape):e},b:function(){var e=t.mul(n.toFloat()),o=fo(r.shape,a);o.length>0&&(e=e.sum(o).reshape(r.shape));var i=r.square();return e.div(i.toFloat()).neg()}}},"FloorDiv")}}),js=wn({maximum_:function(t,e){var n,r=cn(t,"a","maximum"),o=cn(e,"b","maximum");return n=Rt(r,o),r=n[0],o=n[1],"bool"===r.dtype&&(r=r.toInt(),o=o.toInt()),po(r.shape,o.shape),Nt.runKernelFunc(function(t,e){var n=t.maximum(r,o);return e([r,o]),n},{$a:r,$b:o},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())}}})}}),Xs=wn({maximumStrict_:function(t,e){var n=cn(t,"a","maximumStrict"),r=cn(e,"b","maximumStrict");return y(n.shape,r.shape,"Error in maximumStrict: "),n.maximum(r)}}),$s=wn({minimum_:function(t,e){var n,r=cn(t,"a","minimum"),o=cn(e,"b","minimum");return n=Rt(r,o),r=n[0],o=n[1],"bool"===r.dtype&&(r=r.toInt(),o=o.toInt()),po(r.shape,o.shape),Nt.runKernelFunc(function(t,e){var n=t.minimum(r,o);return e([r,o]),n},{$a:r,$b:o},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())}}})}}),Ys=wn({minimumStrict_:function(t,e){var n=cn(t,"a","minimumStrict"),r=cn(e,"b","minimumStrict");return y(n.shape,r.shape,"Error in minimumStrict: "),n.minimum(r)}}),Qs=wn({mod_:function(t,e){var n,r=cn(t,"a","mod"),o=cn(e,"b","mod");n=Rt(r,o),r=n[0],o=n[1];var a=po(r.shape,o.shape);return Nt.runKernelFunc(function(t,e){var n=t.mod(r,o);return e([r,o]),n},{$a:r,$b:o},function(t,e){var n=e[0],r=e[1];return {$a:function(){var e=fo(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()),o=fo(r.shape,a);return o.length>0?e.sum(o).reshape(r.shape):e}}})}}),Js=wn({modStrict_:function(t,e){var n=cn(t,"a","modStrict"),r=cn(e,"b","modStrict");return y(n.shape,r.shape,"Error in modStrict: "),n.mod(r)}}),Zs=wn({mul_:function(t,e){var n,r=cn(t,"a","mul"),o=cn(e,"b","mul");n=Rt(r,o),r=n[0],o=n[1];var a=po(r.shape,o.shape);return Nt.runKernelFunc(function(t,e){var n=t.multiply(r,o);return e([r,o]),n},{a:r,b:o},function(t,e){var n=e[0],r=e[1];return {a:function(){var e=t.mul(r.toFloat()),o=fo(n.shape,a);return o.length>0?e.sum(o).reshape(n.shape):e},b:function(){var e=t.mul(n.toFloat()),o=fo(r.shape,a);return o.length>0?e.sum(o).reshape(r.shape):e}}},"Mul")}}),tc=wn({mulStrict_:function(t,e){var n=cn(t,"a","mul"),r=cn(e,"b","mul");return y(n.shape,r.shape,"Error in multiplyStrict: "),n.mul(r)}}),ec=wn({pow_:function(t,e){var n=cn(t,"base","pow"),r=cn(e,"exp","pow"),o=po(n.shape,r.shape);return t=n.cast(Ct(n.dtype,r.dtype)),e=r.cast(Ct(n.dtype,r.dtype)),Nt.runKernelFunc(function(t,e){var o=t.pow(n,r);return e([n,r,o]),o},{$base:n,$exp:r},function(t,e){var n=e[0],r=e[1],a=e[2];return {$base:function(){var e=r.toFloat(),a=t.mul(e.mul(n.pow(e.sub(Sn(1))))),i=fo(n.shape,o);return i.length>0&&(a=a.sum(i)),a.reshape(n.shape)},$exp:function(){var e=n.greater(0),i=n.log().where(e,Vn(n)),u=t.mul(a.mul(i)),s=fo(r.shape,o);return s.length>0&&(u=u.sum(s)),u.reshape(r.shape)}}})}}),nc=wn({powStrict_:function(t,e){return y(t.shape,e.shape,"Error in powStrict: "),t.pow(e)}}),rc=wn({squaredDifference_:function(t,e){var n,r=cn(t,"a","squaredDifference"),o=cn(e,"b","squaredDifference");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t,e){var n=t.squaredDifference(r,o);return e([r,o]),n},{$a:r,$b:o},function(t,e){var n=e[0],r=e[1],o=Sn(2);return {$a:function(){return t.mul(n.sub(r).mul(o))},$b:function(){return t.mul(r.sub(n).mul(o))}}})}}),oc=wn({squaredDifferenceStrict_:function(t,e){var n=cn(t,"a","squaredDifferenceStrict"),r=cn(e,"b","squaredDifferenceStrict");return y(n.shape,r.shape,"Error in squaredDifferenceStrict: "),n.squaredDifference(r)}}),ac=wn({sub_:function(t,e){var n,r=cn(t,"a","sub"),o=cn(e,"b","sub");n=Rt(r,o),r=n[0],o=n[1];var a=po(r.shape,o.shape);return Nt.runKernelFunc(function(t){return t.subtract(r,o)},{a:r,b:o},function(t){return {a:function(){var e=t,n=fo(r.shape,a);return n.length>0&&(e=e.sum(n)),e.reshape(r.shape)},b:function(){var e=t,n=fo(o.shape,a);return n.length>0&&(e=e.sum(n)),e.neg().reshape(o.shape)}}},"Sub")}}),ic=wn({subStrict_:function(t,e){var n=cn(t,"a","subStrict"),r=cn(e,"b","subStrict");return y(n.shape,r.shape,"Error in subStrict: "),n.sub(r)}});var uc=wn({equal_:function(t,e){var n,r=cn(t,"a","equal"),o=cn(e,"b","equal");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t){return t.equal(r,o)},{$a:r,$b:o})}}),sc=wn({equalStrict_:function(t,e){var n=cn(t,"a","equalStrict"),r=cn(e,"b","equalStrict");return y(n.shape,r.shape,"Error in equalStrict: "),n.equal(r)}}),cc=wn({greater_:function(t,e){var n,r=cn(t,"a","greater"),o=cn(e,"b","greater");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t){return t.greater(r,o)},{$a:r,$b:o})}}),lc=wn({greaterEqual_:function(t,e){var n,r=cn(t,"a","greaterEqual"),o=cn(e,"b","greaterEqual");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t,e){var n=t.greaterEqual(r,o);return e([r,o]),n},{$a:r,$b:o},function(t,e){var n=e[0],r=e[1];return {$a:function(){return Vn(n)},$b:function(){return Vn(r)}}})}}),hc=wn({greaterEqualStrict_:function(t,e){var n=cn(t,"a","greaterEqualStrict"),r=cn(e,"b","greaterEqualStrict");return y(n.shape,r.shape,"Error in greaterEqualStrict: "),n.greaterEqual(r)}}),fc=wn({greaterStrict_:function(t,e){var n=cn(t,"a","greaterStrict"),r=cn(e,"b","greaterStrict");return y(n.shape,r.shape,"Error in greaterStrict: "),n.greater(r)}}),pc=wn({less_:function(t,e){var n,r=cn(t,"a","less"),o=cn(e,"b","less");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t){return t.less(r,o)},{$a:r,$b:o})}}),dc=wn({lessEqual_:function(t,e){var n,r=cn(t,"a","lessEqual"),o=cn(e,"b","lessEqual");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t){return t.lessEqual(r,o)},{$a:r,$b:o})}}),vc=wn({lessEqualStrict_:function(t,e){var n=cn(t,"a","lessEqualStrict"),r=cn(e,"b","lessEqualStrict");return y(n.shape,r.shape,"Error in lessEqualStrict: "),n.lessEqual(r)}}),mc=wn({lessStrict_:function(t,e){var n=cn(t,"a","lessStrict"),r=cn(e,"b","lessStrict");return y(n.shape,r.shape,"Error in lessStrict: "),n.less(r)}}),gc=wn({notEqual_:function(t,e){var n,r=cn(t,"a","notEqual"),o=cn(e,"b","notEqual");return n=Rt(r,o),r=n[0],o=n[1],po(r.shape,o.shape),Nt.runKernelFunc(function(t){return t.notEqual(r,o)},{$a:r,$b:o})}}),yc=wn({notEqualStrict_:function(t,e){var n=cn(t,"a","notEqualStrict"),r=cn(e,"b","notEqualStrict");return y(n.shape,r.shape,"Error in notEqualStrict: "),n.notEqual(r)}});function xc(t,e){for(var n=[],r=t;r0,function(){return "mask cannot be scalar"}),y(s.slice(i,i+u),a.shape,"mask's shape must match the first K dimensions of tensor's shape,"),c=1,l=i;l=2&&i.rank>=2&&a.rank===i.rank,function(){return "Error in matMul: inputs must have the same rank of at least 2, got ranks "+a.rank+" and "+i.rank+"."}),g(C(h,f),function(){return "Error in matMul: outer dimensions ("+h+") and ("+f+") of Tensors with shapes "+a.shape+" and "+i.shape+" must match."}),g(u===s,function(){return "Error in matMul: inner shapes ("+u+") and ("+s+") of Tensors with shapes "+a.shape+" and "+i.shape+" and transposeA="+n+" and transposeB="+r+" must match."});var v=a.shape.slice(0,-2).concat([c,l]),m=n?a.as3D(p,u,c):a.as3D(p,c,u),y=r?i.as3D(d,l,s):i.as3D(d,s,l),x={transposeA:n,transposeB:r};return Nt.runKernelFunc(function(t,e){var o=t.batchMatMul(m,y,n,r);return e([m,y]),o},{a:m,b:y},function(t,e){var o=e,a=o[0],i=o[1];return n||r?!n&&r?{a:function(){return t.matMul(i,!1,!1)},b:function(){return t.matMul(a,!0,!1)}}:n&&!r?{a:function(){return i.matMul(t,!1,!0)},b:function(){return a.matMul(t,!1,!1)}}:{a:function(){return i.matMul(t,!0,!0)},b:function(){return t.matMul(a,!0,!0)}}:{a:function(){return t.matMul(i,!1,!0)},b:function(){return a.matMul(t,!0,!1)}}},"BatchMatMul",x).reshape(v)}}),Wc=wn({dot_:function(t,e){var n=cn(t,"t1","dot"),r=cn(e,"t2","dot");g(!(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 o=1===n.rank?n.size:n.shape[1],a=1===r.rank?r.size:r.shape[0];return g(o===a,function(){return "Error in dot: inner dimensions of inputs must match, but got "+o+" 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]))}}),Uc=wn({outerProduct_:function(t,e){var n=cn(t,"v1","outerProduct"),r=cn(e,"v2","outerProduct");return g(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))}});var Vc=wn({reverse_:function(t,e){var n=cn(t,"x","reverse");if(0===n.rank)return n.clone();var r=D(e,n.shape);return Nt.runKernelFunc(function(t){return t.reverse(n,r)},{$x:n},function(t){return {$x:function(){return t.reverse(r)}}}).reshapeAs(n)}}),zc=wn({reverse1d_:function(t){var e=cn(t,"x","reverse");return g(1===e.rank,function(){return "Error in reverse1D: x must be rank 1 but got rank "+e.rank+"."}),Vc(e,0)}}),Gc=wn({reverse2d_:function(t,e){var n=cn(t,"x","reverse");return g(2===n.rank,function(){return "Error in reverse2D: x must be rank 2 but got rank "+n.rank+"."}),Vc(n,e)}}),Hc=wn({reverse3d_:function(t,e){var n=cn(t,"x","reverse");return g(3===n.rank,function(){return "Error in reverse3D: x must be rank 3 but got rank "+n.rank+"."}),Vc(n,e)}}),qc=wn({reverse4d_:function(t,e){var n=cn(t,"x","reverse");return g(4===n.rank,function(){return "Error in reverse4D: x must be rank 4 but got rank "+n.rank+"."}),Vc(n,e)}});function Kc(t,e,n,r,o,a){var i=cn(t,"x","maxPool"),u=i,s=!1;3===i.rank&&(s=!0,u=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),null==r&&(r=[1,1]),g(4===u.rank,function(){return "Error in maxPool: input must be rank 4 but got rank "+u.rank+"."}),g(Io(n,r),function(){return "Error in maxPool: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+r+"'"}),null!=a&&g(E(o),function(){return "Error in maxPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+o+"."});var c=vo(u.shape,e,n,r,o,a);if(1===c.filterWidth&&1===c.filterHeight&&C(c.inShape,c.outShape))return i.clone();var l=[u],h=Nt.runKernelFunc(function(t,e){var n=t.maxPool(u,c);return e([u,n]),n},{x:u},function(t,a){var i=a[0],u=a[1];return {x:function(){return function(t,e,n,r,o,a,i,u){var s=cn(t,"dy","maxPoolBackprop"),c=cn(e,"input","maxPoolBackprop"),l=cn(n,"output","maxPoolBackprop");g(c.rank===s.rank,function(){return "Rank of input ("+c.rank+") does not match rank of dy ("+s.rank+")"}),null==a&&(a=[1,1]),g(Io(o,a),function(){return "Error in maxPoolBackProp: Either strides or dilations must be 1. Got strides "+o+" and dilations '"+a+"'"}),g(4===s.rank,function(){return "Error in maxPoolBackprop: dy must be rank 4 but got rank "+s.rank+"."}),g(4===c.rank,function(){return "Error in maxPoolBackprop: input must be rank 4 but got rank "+c.rank+"."}),null!=u&&g(E(i),function(){return "Error in maxPoolBackprop: pad must be an integer when using, dimRoundingMode "+u+" but got pad "+i+"."});var h=vo(c.shape,r,o,a,i,u);return Nt.runKernelFunc(function(t){return t.maxPoolBackprop(s,c,l,h)},{$dy:s,$input:c})}(t,i,u,e,n,r,o)}}},"MaxPool",c,l);return s?h.as3D(h.shape[1],h.shape[2],h.shape[3]):h}function jc(t,e,n,r,o,a){var i=cn(t,"x","avgPool","float32");null==r&&(r=[1,1]),g(Io(n,r),function(){return "Error in avgPool: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+r+"'"});var u=i,s=!1;3===i.rank&&(s=!0,u=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),g(4===u.rank,function(){return "Error in avgPool: x must be rank 4 but got rank "+u.rank+"."}),null!=a&&g(E(o),function(){return "Error in avgPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+o+"."});var c=vo(u.shape,e,n,r,o,a);if(1===c.filterWidth&&1===c.filterHeight&&C(c.inShape,c.outShape))return i.clone();var l=Nt.runKernelFunc(function(t){return t.avgPool(u,c)},{x:u},function(t){return {x:function(){return function(t,e,n,r,o,a){var i=cn(t,"dy","avgPoolBackprop"),u=cn(e,"input","avgPoolBackprop");g(u.rank===i.rank,function(){return "Rank of input ("+u.rank+") does not match rank of dy ("+i.rank+")"}),null==o&&(o=[1,1]),g(Io(r,o),function(){return "Error in avgPoolBackprop: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+o+"'"});var s=u,c=i,l=!1;3===u.rank&&(l=!0,s=u.as4D(1,u.shape[0],u.shape[1],u.shape[2]),c=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),g(4===c.rank,function(){return "Error in avgPoolBackprop: dy must be rank 4 but got rank "+c.rank+"."}),g(4===s.rank,function(){return "Error in avgPoolBackprop: input must be rank 4 but got rank "+s.rank+"."});var h=vo(s.shape,n,r,o,a),f=Nt.runKernelFunc(function(t){return t.avgPoolBackprop(c,s,h)},{dy4D:c,input4D:s});return l?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f}(t,u,e,n,r,o)}}},"AvgPool",c);return l=l.cast(i.dtype),s?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}var Xc=wn({maxPool_:function(t,e,n,r,o){return Kc(t,e,n,1,r,o)}}),$c=wn({avgPool_:function(t,e,n,r,o){return jc(t,e,n,1,r,o)}}),Yc=wn({pool_:function(t,e,n,r,o,a){null==o&&(o=[1,1]),null==a&&(a=1),0===r&&(r="valid");var i=cn(t,"x","maxPool"),u=i,s=!1;3===i.rank&&(s=!0,u=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),g(Io(a,o),function(){return "Error in pool: Either strides or dilations must be 1. Got strides "+a+" and dilations '"+o+"'"});var c,l=vo(u.shape,e,a,o,r),h=[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)}),o=n.map(function(t,e){return t-r[e]});return n.map(function(t,e){return [r[e],o[e]]})}([l.filterHeight,l.filterWidth],h):[[0,0],[0,0]];var f=1===h[0]&&1===h[1],p=function(t,e,n){var r=n.map(function(t){return t[0]}),o=n.map(function(t){return t[1]}),a=t.concat(r,o),i=e.map(function(t,e){return (t-a[e]%t)%t}),u=o.map(function(t,e){return t+i[e]}),s=e.map(function(t,e){return [r[e],u[e]]}),c=e.map(function(t,e){return [0,i[e]]});return [s,c]}([l.inHeight,l.inWidth],h,c),d=p[0],v=p[1],m=f?r:"valid",y=f?u:Ar(u,h,d),x=("avg"===n?function(){return jc(y,e,a,1,m)}:function(){return Kc(y,e,a,1,m)})(),b=f?x:sr(x,h,v);return s?b.as3D(b.shape[1],b.shape[2],b.shape[3]):b}}),Qc=wn({maxPool3d_:function(t,e,n,r,o,a,i){void 0===a&&(a="NDHWC");var u=cn(t,"x","maxPool3d"),s=u,c=!1;4===u.rank&&(c=!0,s=u.as5D(1,u.shape[0],u.shape[1],u.shape[2],u.shape[3])),null==i&&(i=[1,1,1]),g(5===s.rank,function(){return "Error in maxPool3d: x must be rank 5 but got rank "+s.rank+"."}),g("NDHWC"===a,function(){return "Error in maxPool3d: Only NDHWC is currently supported, but got dataFormat of "+a}),g(Io(n,i),function(){return "Error in maxPool3d: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+i+"'"}),null!=o&&g(E(r),function(){return "Error in maxPool3d: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+r+"."});var l=mo(s.shape,e,n,i,r,o,a),h=Nt.runKernelFunc(function(t,e){var n=t.maxPool3d(s,l);return e([s,n]),n},{x:s},function(t,a){var u=a[0],s=a[1];return {x:function(){return function(t,e,n,r,o,a,i,u){var s=cn(t,"dy","maxPool3dBackprop"),c=cn(e,"input","maxPool3dBackprop"),l=cn(n,"output","maxPool3dBackprop"),h=s,f=c,p=l,d=!1;4===c.rank&&(d=!0,h=s.as5D(1,s.shape[0],s.shape[1],s.shape[2],s.shape[3]),f=c.as5D(1,c.shape[0],c.shape[1],c.shape[2],c.shape[3]),p=l.as5D(1,l.shape[0],l.shape[1],l.shape[2],l.shape[3])),g(5===h.rank,function(){return "Error in maxPool3dBackprop: dy must be rank 5 but got rank "+h.rank+"."}),g(5===f.rank,function(){return "Error in maxPool3dBackprop: input must be rank 5 but got rank "+f.rank+"."}),g(5===p.rank,function(){return "Error in maxPool3dBackprop: output must be rank 5 but got rank "+p.rank+"."}),null==a&&(a=[1,1,1]),g(Io(o,a),function(){return "Error in maxPool3dBackprop: Either strides or dilations must be 1. Got strides "+o+" and dilations '"+a+"'"}),null!=u&&g(E(i),function(){return "Error in maxPool3dBackprop: pad must be an integer when using, dimRoundingMode "+u+" but got pad "+i+"."});var v=mo(f.shape,r,o,a,i,u),m=Nt.runKernelFunc(function(t){return t.maxPool3dBackprop(h,f,p,v)},{dy5D:h,input5D:f});return d?m.as4D(m.shape[1],m.shape[2],m.shape[3],m.shape[4]):m}(t,u,s,e,n,i,r,o)}}});return c?h.as4D(h.shape[1],h.shape[2],h.shape[3],h.shape[4]):h}}),Jc=wn({avgPool3d_:function(t,e,n,r,o,a,i){void 0===a&&(a="NDHWC");var u=cn(t,"x","avgPool3d","float32"),s=u,c=!1;4===u.rank&&(c=!0,s=u.as5D(1,u.shape[0],u.shape[1],u.shape[2],u.shape[3])),null==i&&(i=[1,1,1]),g(5===s.rank,function(){return "Error in avgPool3d: x must be rank 5 but got rank "+s.rank+"."}),g("NDHWC"===a,function(){return "Error in avgPool3d: Only NDHWC is currently supported, but got dataFormat of "+a}),g(Io(n,i),function(){return "Error in avgPool3d: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+i+"'"}),null!=o&&g(E(r),function(){return "Error in avgPool3d: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+r+"."});var l=mo(s.shape,e,n,i,r,o,a),h=Nt.runKernelFunc(function(t){return t.avgPool3d(s,l)},{x:s},function(t){return {x:function(){return function(t,e,n,r,o,a,i){var u=cn(t,"dy","avgPool3dBackprop"),s=cn(e,"input","avgPool3dBackprop"),c=u,l=s,h=!1;4===s.rank&&(h=!0,c=u.as5D(1,u.shape[0],u.shape[1],u.shape[2],u.shape[3]),l=s.as5D(1,s.shape[0],s.shape[1],s.shape[2],s.shape[3])),g(5===c.rank,function(){return "Error in avgPool3dBackprop: dy must be rank 5 but got rank "+c.rank+"."}),g(5===l.rank,function(){return "Error in avgPool3dBackprop: input must be rank 5 but got rank "+l.rank+"."}),null==o&&(o=[1,1,1]),g(Io(r,o),function(){return "Error in avgPool3dBackprop: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+o+"'"}),null!=i&&g(E(a),function(){return "Error in maxPool3dBackprop: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+a+"."});var f=mo(l.shape,n,r,o,a,i),p=Nt.runKernelFunc(function(t){return t.avgPool3dBackprop(c,l,f)},{dy5D:c,input5D:l});return h?p.as4D(p.shape[1],p.shape[2],p.shape[3],p.shape[4]):p}(t,s,e,n,i,r,o)}}});return h=h.cast(s.dtype),c?h.as4D(h.shape[1],h.shape[2],h.shape[3],h.shape[4]):h}});var Zc=wn({slice_:function(t,e,n){var r,o,a=cn(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:(g(-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])}),qr(a,r,o);var i=a.shape,u={begin:r,size:o};return Nt.runKernelFunc(function(t){return t.slice(a,r,o)},{x:a},function(t){for(var e=[],n=0;n0&&(e=e.sum(a)),e.reshape(r.shape)}}},"Prelu")}}),xl=wn({relu_:function(t){var e=cn(t,"x","relu");return "bool"===e.dtype?e.toInt():Nt.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")}}),bl=wn({relu6_:function(t){var e=cn(t,"x","relu6");return "bool"===e.dtype?e.toInt():Nt.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")}}),wl=wn({selu_:function(t){var e=cn(t,"x","selu");return Nt.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(Sn(0)),r=Sn(ou),o=Sn(au),a=t.mul(o),i=t.mul(r).mul(n.toFloat().exp());return Ps(e,a,i)}}})}});var Cl=wn({transpose_:function(t,e){var n=cn(t,"x","transpose");if(null==e&&(e=n.shape.map(function(t,e){return e}).reverse()),g(n.rank===e.length,function(){return "Error in transpose: rank of input "+n.rank+" must match length of perm "+e+"."}),e.forEach(function(t){g(t>=0&&to)throw new Error("'k' passed to topk() must be <= the last dimension ("+o+") but got "+e);var a=Nt.runKernelFunc(function(t){return t.topk(r,e,n)},{$x:r});return {values:a[0],indices:a[1]}}});var Tl=wn({scatterND_:function(t,e,n){var r=cn(t,"indices","scatterND","int32"),o=cn(e,"updates","scatterND");return Gr(o,r,n),Nt.runKernelFunc(function(t){return t.scatterND(r,o,n)},{$indices:r,$updates:o})}});var Nl=wn({fft_:function(t){g("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 Nt.runKernelFunc(function(t){return t.fft(r)},{input:t}).reshape(t.shape)}}),Fl=wn({ifft_:function(t){g("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 Nt.runKernelFunc(function(t){return t.ifft(r)},{input:t}).reshape(t.shape)}}),_l=wn({rfft_:function(t,e){g("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],o=t.size/r;if(null!=e&&er){var u=t.shape.map(function(t){return t});u[t.shape.length-1]=e-r,n=t.concat(Bn(u),t.shape.length-1),r=e;}else n=t;var s=n.zerosLike(),c=Cn(n,s).as2D(o,r),l=Nl(c),h=Math.floor(r/2)+1,f=En(l),p=Rn(l),d=f.split([h,r-h],f.shape.length-1),v=p.split([h,r-h],p.shape.length-1),m=n.shape.slice();return m[n.shape.length-1]=h,Cn(d[0],v[0]).reshape(m)}}),Ol=wn({irfft_:function(t){var e=t.shape[t.shape.length-1],n=t.size/e;if(e<=2){var r=t.as2D(n,e),o=Fl(r);return En(o)}var a=[n,2*(e-1)],i=En(t).as2D(n,e),u=Rn(t).as2D(n,e),s=i.slice([0,1],[n,e-2]).reverse(1),c=u.slice([0,1],[n,e-2]).reverse(1).mul(Sn(-1)),l=i.concat(s,1),h=u.concat(c,1);return r=Cn(l,h).as2D(a[0],a[1]),o=Fl(r),En(o)}}),Ml=Object.freeze({fft:Nl,ifft:Fl,rfft:_l,irfft:Ol});var Bl=wn({sparseToDense_:function(t,e,n,r){void 0===r&&(r=0);var o=cn(t,"sparseIndices","sparseToDense","int32"),a=cn(e,"sparseValues","sparseToDense"),i=cn(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 o=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 i=e.size;if(0!==e.rank&&(1!==e.rank||i!==o))throw new Error("sparseValues has incorrect shape "+e.shape+", should be [] or ["+o+"]");if(e.dtype!==r.dtype)throw new Error("sparseValues.dtype must match defaultValues.dtype")}(o,a,n,i),Nt.runKernelFunc(function(t){return t.sparseToDense(o,a,n,i)},{$sparseIndices:o,$sparseValues:a,$defaultValue:i})}});var Pl=wn({gatherND_:function(t,e){var n=cn(e,"indices","gatherND","int32"),r=cn(t,"x","gatherND");return Nt.runKernelFunc(function(t){return t.gatherND(r,n)},{$x:r,$indices:n})}});var Ll=wn({diag_:function(t){var e=cn(t,"x","diag").flatten(),n=t.shape.concat(t.shape);return Nt.runKernelFunc(function(t){return t.diag(e)},{$x:e}).reshape(n)}});var Wl=wn({dropout_:function(t,e,n,r){var o=cn(t,"x","dropout");if(g("float32"===o.dtype,function(){return "x has to be a floating point tensor since it's going to be scaled, but got a "+o.dtype+" tensor instead."}),g(e>=0&&e<1,function(){return "rate must be a float in the range [0, 1), but got "+e+"."}),0===e)return t instanceof dt?o.clone():o;var a=function(t,e){if(null==e)return t.shape.slice();if(C(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 "+n.rank}),g(n.rank-1===a.rank,function(){return "predictions rank should be 1 larger than targets rank, but got predictions rank "+n.rank+" and targets rank "+a.rank}),y(n.shape.slice(0,n.shape.length-1),a.shape,"predictions's shape should be align with the targets' shape, except the last dimension."),i=n.shape[n.shape.length-1],g(o>0&&o<=i,function(){return "'k' passed to inTopK() must be > 0 && <= the predictions last dimension ("+i+"), but got "+o}),[4,n.data()];case 1:return u=r.sent(),[4,a.data()];case 2:for(s=r.sent(),c=[u.length/i,i],h=c[1],f=N("bool",l=c[0]),p=0;p1?u.div(Sn(i)):u}if(n===Kl.SUM_BY_NONZERO_WEIGHTS){if(null==o)return a.sum().div(Sn(r.size));var s=o.mul(Mn(r.shape)).notEqual(Sn(0)).sum().toFloat();return a.sum().div(s)}throw Error("Unknown reduction: "+n)}}),Yl=wn({cosineDistance_:function(t,e,n,r,o){void 0===o&&(o=Kl.SUM_BY_NONZERO_WEIGHTS);var a=cn(t,"labels","cosineDistance"),i=cn(e,"predictions","cosineDistance"),u=null;null!=r&&(u=cn(r,"weights","cosineDistance")),y(a.shape,i.shape,"Error in cosineDistance: ");var s=Sn(1).sub(a.mul(i).sum(n,!0));return $l(s,u,o)}}),Ql=wn({hingeLoss_:function(t,e,n,r){void 0===r&&(r=Kl.SUM_BY_NONZERO_WEIGHTS);var o=cn(t,"labels","hingeLoss"),a=cn(e,"predictions","hingeLoss"),i=null;null!=n&&(i=cn(n,"weights","hingeLoss")),y(o.shape,a.shape,"Error in hingeLoss: ");var u=Sn(1);o=Sn(2).mul(o).sub(u);var s=u.sub(o.mul(a)).relu();return $l(s,i,r)}}),Jl=wn({huberLoss_:function(t,e,n,r,o){void 0===r&&(r=1),void 0===o&&(o=Kl.SUM_BY_NONZERO_WEIGHTS);var a=cn(t,"labels","huberLoss"),i=cn(e,"predictions","huberLoss"),u=null;null!=n&&(u=cn(n,"weights","huberLoss")),y(a.shape,i.shape,"Error in huberLoss: ");var s=Sn(r),c=i.sub(a).abs(),l=$s(c,s),h=c.sub(l),f=Sn(.5).mul(l.square()).add(s.mul(h));return $l(f,u,o)}}),Zl=wn({logLoss_:function(t,e,n,r,o){void 0===r&&(r=1e-7),void 0===o&&(o=Kl.SUM_BY_NONZERO_WEIGHTS);var a=cn(t,"labels","logLoss"),i=cn(e,"predictions","logLoss"),u=null;null!=n&&(u=cn(n,"weights","logLoss")),y(a.shape,i.shape,"Error in logLoss: ");var s=Sn(1),c=Sn(r),l=a.mul(i.add(c).log()).neg().sub(s.sub(a).mul(s.sub(i).add(c).log()));return $l(l,u,o)}}),th=wn({meanSquaredError_:function(t,e,n,r){void 0===r&&(r=Kl.SUM_BY_NONZERO_WEIGHTS);var o=cn(t,"labels","meanSquaredError"),a=cn(e,"predictions","meanSquaredError"),i=null;null!=n&&(i=cn(n,"weights","meanSquaredError")),y(o.shape,a.shape,"Error in meanSquaredError: ");var u=o.squaredDifference(a);return $l(u,i,r)}}),eh=wn({sigmoidCrossEntropy_:function(t,e,n,r,o){void 0===r&&(r=0),void 0===o&&(o=Kl.SUM_BY_NONZERO_WEIGHTS);var a=cn(t,"multiClassLabels","sigmoidCrossEntropy"),i=cn(e,"logits","sigmoidCrossEntropy"),u=null;if(null!=n&&(u=cn(n,"weights","sigmoidCrossEntropy")),y(a.shape,i.shape,"Error in sigmoidCrossEntropy: "),r>0){var s=Sn(r),c=Sn(1),l=Sn(.5);a=a.mul(c.sub(s)).add(l.mul(s));}var h=function(t,e){var n=cn(t,"labels","sigmoidCrossEntropyWithLogits"),r=cn(e,"logits","sigmoidCrossEntropyWithLogits");y(n.shape,r.shape,"Error in sigmoidCrossEntropyWithLogits: ");var o=r.relu(),a=r.mul(n),i=r.abs().neg().exp().log1p();return o.sub(a).add(i)}(a,i);return $l(h,u,o)}}),nh=wn({softmaxCrossEntropy_:function(t,e,n,r,o){void 0===r&&(r=0),void 0===o&&(o=Kl.SUM_BY_NONZERO_WEIGHTS);var a=cn(t,"onehotLabels","softmaxCrossEntropy"),i=cn(e,"logits","softmaxCrossEntropy"),u=null;if(null!=n&&(u=cn(n,"weights","softmaxCrossEntropy")),y(a.shape,i.shape,"Error in softmaxCrossEntropy: "),r>0){var s=Sn(r),c=Sn(1),l=Sn(a.shape[1]);a=a.mul(c.sub(s)).add(s.div(l));}var h=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 oo(function(t,e,r){var o=e.logSumExp([n],!0),a=e.toFloat().sub(o);return r([t,a]),{value:a.mul(t).neg().sum([n]),gradFunc:function(t,e){var r=e[0],o=e[1],a=dn(t.shape,[n]);return [t.reshape(a).mul(r.toFloat().sub(o.exp())),t.reshape(a).mul(o.exp().sub(r.toFloat()))]}}})(t,e)}(a,i);return $l(h,u,o)}}),rh=Object.freeze({get Reduction(){return Kl},absoluteDifference:Xl,computeWeightedLoss:$l,cosineDistance:Yl,hingeLoss:Ql,huberLoss:Jl,logLoss:Zl,meanSquaredError:th,sigmoidCrossEntropy:eh,softmaxCrossEntropy:nh});function oh(t,e){return void 0===e&&(e=!1),Nt.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],o=vr(n),a=t.clone(),i=Dn([[1]],[1,1]),u=i.clone(),s=n>=r?r:n,c=function(t){var e,s=a,c=u,l=o;e=Nt.tidy(function(){var e=a.slice([t,t],[n-t,1]),s=e.norm(),c=a.slice([t,t],[1,1]),l=Dn([[-1]]).where(c.greater(0),Dn([[1]])),h=c.sub(l.mul(s)),f=e.div(h);u=1===f.shape[0]?i.clone():i.concat(f.slice([1,0],[f.shape[0]-1,f.shape[1]]),0);var p=l.matMul(h).div(s).neg(),d=a.slice([t,0],[n-t,r]),v=p.mul(u);if(0===t)a=d.sub(v.matMul(u.transpose().matMul(d)));else{var m=d.sub(v.matMul(u.transpose().matMul(d)));a=a.slice([0,0],[t,r]).concat(m,0);}var g=o.slice([0,t],[n,o.shape[1]-t]);if(0===t)o=g.sub(g.matMul(u).matMul(v.transpose()));else{var y=g.sub(g.matMul(u).matMul(v.transpose()));o=o.slice([0,0],[n,t]).concat(y,1);}return [u,a,o]}),u=e[0],a=e[1],o=e[2],je([s,c,l]);},l=0;lr&&(o=o.slice([0,0],[n,r]),a=a.slice([0,0],[r,r])),[o,a]})}var ah=wn({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=cn(t,"a","bandPart");if(r.rank<2)throw new Error("bandPart(): Rank must be at least 2, got "+r.rank+".");var o=r.shape,a=r.shape.slice(-2),i=a[0],u=a[1];if(!(e<=i))throw new Error("bandPart(): numLower ("+e+") must not be greater than the number of rows ("+i+").");if(!(n<=u))throw new Error("bandPart(): numUpper ("+n+") must not be greater than the number of columns ("+u+").");e<0&&(e=i),n<0&&(n=u);var s=Wn(0,i,1,"int32").reshape([-1,1]),c=Wn(0,u,1,"int32"),l=ac(s,c),h=_s(l.lessEqual(Sn(+e,"int32")),l.greaterEqual(Sn(-n,"int32"))),f=Bn([i,u],r.dtype);return Tr(_r(r.reshape([-1,i,u])).map(function(t){return Ps(h,t,f)})).reshape(o)}}),ih=wn({gramSchmidt_:function(t){var e;if(Array.isArray(t)){e=!1,g(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){g(t[e].shape[0]===n,function(){return "Gram-Schmidt: Non-unique lengths found in the input vectors: ("+t[e].shape[0]+" vs. "+n+")"});},o=1;o0)for(var n=0;n= 2, but got rank "+t.rank);if(2===t.rank)return oh(t,e);var n=t.shape.slice(0,t.shape.length-2).reduce(function(t,e){return t*e}),r=_r(t.reshape([n,t.shape[t.shape.length-2],t.shape[t.shape.length-1]]),0),o=[],a=[];return r.forEach(function(t){var n=oh(t,e),r=n[0],i=n[1];o.push(r),a.push(i);}),[Tr(o,0).reshape(t.shape),Tr(a,0).reshape(t.shape)]}}),sh=Object.freeze({bandPart:ah,gramSchmidt:ih,qr:uh});function ch(t,e,n,r,o){null==r&&(r=.5),null==o&&(o=Number.NEGATIVE_INFINITY);var a=t.shape[0];return n=Math.min(n,a),g(0<=r&&r<=1,function(){return "iouThreshold must be in [0, 1], but was '"+r+"'"}),g(2===t.rank,function(){return "boxes must be a 2D tensor, but was of rank '"+t.rank+"'"}),g(4===t.shape[1],function(){return "boxes must have 4 columns, but 2nd dimension was "+t.shape[1]}),g(1===e.rank,function(){return "scores must be a 1D tensor"}),g(e.shape[0]===a,function(){return "scores has incompatible shape with boxes. Expected "+a+", but was "+e.shape[0]}),{maxOutputSize:n,iouThreshold:r,scoreThreshold:o}}var lh=wn({resizeBilinear_:function(t,e,n){void 0===n&&(n=!1);var r=cn(t,"images","resizeBilinear");g(3===r.rank||4===r.rank,function(){return "Error in resizeBilinear: x must be rank 3 or 4, but got rank "+r.rank+"."}),g(2===e.length,function(){return "Error in resizeBilinear: new shape must 2D, but got shape "+e+"."});var o=r,a=!1;3===r.rank&&(a=!0,o=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var i=e[0],u=e[1],s=Nt.runKernelFunc(function(t,e){return e([o]),t.resizeBilinear(o,i,u,n)},{x:o},function(t,e){return {x:function(){return Nt.runKernelFunc(function(r){return r.resizeBilinearBackprop(t,e[0],n)},{})}}},"ResizeBilinear",{alignCorners:n,newHeight:i,newWidth:u});return a?s.as3D(s.shape[1],s.shape[2],s.shape[3]):s}}),hh=wn({resizeNearestNeighbor_:function(t,e,n){void 0===n&&(n=!1);var r=cn(t,"images","resizeNearestNeighbor");g(3===r.rank||4===r.rank,function(){return "Error in resizeNearestNeighbor: x must be rank 3 or 4, but got rank "+r.rank+"."}),g(2===e.length,function(){return "Error in resizeNearestNeighbor: new shape must 2D, but got shape "+e+"."}),g("float32"===r.dtype||"int32"===r.dtype,function(){return "`images` must have `int32` or `float32` as dtype"});var o=r,a=!1;3===r.rank&&(a=!0,o=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var i=e[0],u=e[1],s=Nt.runKernelFunc(function(t,e){return e([o]),t.resizeNearestNeighbor(o,i,u,n)},{batchImages:o},function(t,e){return {batchImages:function(){return Nt.runKernelFunc(function(r){return r.resizeNearestNeighborBackprop(t,e[0],n)},{})}}});return a?s.as3D(s.shape[1],s.shape[2],s.shape[3]):s}}),fh=wn({nonMaxSuppression_:function(t,e,n,r,o){void 0===r&&(r=.5),void 0===o&&(o=Number.NEGATIVE_INFINITY);var a=cn(t,"boxes","nonMaxSuppression"),i=cn(e,"scores","nonMaxSuppression"),u=ch(a,i,n,r,o);n=u.maxOutputSize,r=u.iouThreshold,o=u.scoreThreshold;var s={maxOutputSize:n,iouThreshold:r,scoreThreshold:o};return Nt.runKernelFunc(function(t){return t.nonMaxSuppression(a,i,n,r,o)},{boxes:a,scores:i},null,"NonMaxSuppressionV3",s)}}),ph=function(t,e,o,a,i){return void 0===a&&(a=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY),n(this,void 0,void 0,function(){var n,u,s,c,l,h,f;return r(this,function(r){switch(r.label){case 0:return n=cn(t,"boxes","nonMaxSuppressionAsync"),u=cn(e,"scores","nonMaxSuppressionAsync"),s=ch(n,u,o,a,i),o=s.maxOutputSize,a=s.iouThreshold,i=s.scoreThreshold,[4,Promise.all([n.data(),u.data()])];case 1:return c=r.sent(),l=c[0],h=c[1],f=Mo(l,h,o,a,i),n!==t&&n.dispose(),u!==e&&u.dispose(),[2,f]}})})},dh=wn({cropAndResize_:function(t,e,n,r,o,a){var i=cn(t,"image","cropAndResize"),u=cn(e,"boxes","cropAndResize","float32"),s=cn(n,"boxInd","cropAndResize","int32");o=o||"bilinear",a=a||0;var c=u.shape[0];return g(4===i.rank,function(){return "Error in cropAndResize: image must be rank 4,but got rank "+i.rank+"."}),g(2===u.rank&&4===u.shape[1],function(){return "Error in cropAndResize: boxes must be have size ["+c+",4] but had shape "+u.shape+"."}),g(1===s.rank&&s.shape[0]===c,function(){return "Error in cropAndResize: boxInd must be have size ["+c+"] but had shape "+u.shape+"."}),g(2===r.length,function(){return "Error in cropAndResize: cropSize must be of length 2, but got length "+r.length+"."}),g(r[0]>=1&&r[1]>=1,function(){return "cropSize must be atleast [1,1], but was "+r}),g("bilinear"===o||"nearest"===o,function(){return "method must be bilinear or nearest, but was "+o}),Nt.runKernelFunc(function(t,e){return t.cropAndResize(i,u,s,r,o,a)},{images:i,boxes:u,boxInd:s},null,"CropAndResize",{method:o,extrapolationValue:a,cropSize:r})}}),vh=Object.freeze({resizeBilinear:lh,resizeNearestNeighbor:hh,nonMaxSuppression:fh,nonMaxSuppressionAsync:ph,cropAndResize:dh}),mh=function(t,e){return !(t>0)||"linear"===e},gh=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.")},yh=function(t,e){var n=e,r=fo(t.shape,e.shape);return r.length>0&&(n=n.sum(r)),n.reshape(t.shape)},xh=function(t,e,n){if("linear"===e)return t;if("relu"===e)return xl(t);if("elu"===e)return ml(t);if("relu6"===e)return bl(t);if("prelu"===e)return yl(t,n);throw new Error("Unknown fused activation "+e+".")};var bh=wn({fusedMatMul_:function(t){var e,n=t.a,r=t.b,o=t.transposeA,a=void 0!==o&&o,i=t.transposeB,u=void 0!==i&&i,s=t.bias,c=t.activation,l=void 0===c?"linear":c,h=t.preluActivationWeights;if(!1===mh(Nt.state.gradientDepth,l)){var f=Lc(n,r,a,u);return null!=s&&(f=Ws(f,s)),xh(f,l,h)}var p=cn(n,"a","fused matMul"),d=cn(r,"b","fused matMul");e=Rt(p,d),p=e[0],d=e[1];var v=a?p.shape[p.rank-2]:p.shape[p.rank-1],m=u?d.shape[d.rank-1]:d.shape[d.rank-2],y=a?p.shape[p.rank-1]:p.shape[p.rank-2],x=u?d.shape[d.rank-2]:d.shape[d.rank-1],b=p.shape.slice(0,-2),E=d.shape.slice(0,-2),R=w(b),I=w(E);g(p.rank>=2&&d.rank>=2&&p.rank===d.rank,function(){return "Error in fused matMul: inputs must have the same rank of at least 2, got ranks "+p.rank+" and "+d.rank+"."}),g(C(b,E),function(){return "Error in fused matMul: outer dimensions ("+b+") and ("+E+") of Tensors with shapes "+p.shape+" and "+d.shape+" must match."}),g(v===m,function(){return "Error in fused matMul: inner shapes ("+v+") and ("+m+") of Tensors with shapes "+p.shape+" and "+d.shape+" and transposeA="+a+" and transposeB="+u+" must match."});var k,S,A=p.shape.slice(0,-2).concat([y,x]),D=a?p.as3D(R,v,y):p.as3D(R,y,v),T=u?d.as3D(I,x,m):d.as3D(I,m,x);null!=s&&po(A,(k=Rt(k=cn(s,"bias","fused matMul"),p)[0]).shape),null!=h&&(S=cn(h,"prelu weights","fused matMul"));var N={$a:D,$b:T};return null!=s&&(N.$bias=k),null!=h&&(N.$preluActivationWeights=S),Nt.runKernelFunc(function(t,e){var n=t.fusedBatchMatMul({a:D,b:T,transposeA:a,transposeB:u,bias:k,activation:l,preluActivationWeights:S});return e([D,T,n]),n},N,function(t,e){var n=e[0],r=e[1],o=e[2],i=gh(t,o,l),c={};return null!=s&&(c={$bias:function(){return yh(k,i)}}),a||u?!a&&u?Object.assign({$a:function(){return i.matMul(r,!1,!1)},$b:function(){return i.matMul(n,!0,!1)}},c):a&&!u?Object.assign({$a:function(){return r.matMul(i,!1,!0)},$b:function(){return n.matMul(i,!1,!1)}},c):Object.assign({$a:function(){return r.matMul(i,!0,!0)},$b:function(){return i.matMul(n,!0,!0)}},c):Object.assign({$a:function(){return i.matMul(r,!1,!0)},$b:function(){return n.matMul(i,!0,!1)}},c)}).reshape(A)}}),wh=wn({fusedConv2d_:function(t){var e=t.x,n=t.filter,r=t.strides,o=t.pad,a=t.dataFormat,i=void 0===a?"NHWC":a,u=t.dilations,s=void 0===u?[1,1]:u,c=t.dimRoundingMode,l=t.bias,h=t.activation,f=void 0===h?"linear":h,p=t.preluActivationWeights;if(f=f||"linear",!1===mh(Nt.state.gradientDepth,f)){var d=Ac(e,n,r,o,i,s,c);return null!=l&&(d=Ws(d,l)),xh(d,f,p)}var v=cn(e,"x","conv2d"),m=cn(n,"filter","conv2d"),y=v,x=!1;3===v.rank&&(x=!0,y=v.as4D(1,v.shape[0],v.shape[1],v.shape[2])),g(4===y.rank,function(){return "Error in fused conv2d: input must be rank 4, but got rank "+y.rank+"."}),g(4===m.rank,function(){return "Error in fused conv2d: filter must be rank 4, but got rank "+m.rank+"."}),null!=c&&g(E(o),function(){return "Error in fused conv2d: pad must be an integer when using, dimRoundingMode "+c+" but got pad "+o+"."}),g(y.shape[3]===m.shape[2],function(){return "Error in conv2d: depth of input ("+y.shape[3]+") must match input depth for filter "+m.shape[2]+"."}),g(Io(r,s),function(){return "Error in conv2D: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+s+"'"}),g("NHWC"===i,function(){return "Error in conv2d: got dataFormat of "+i+" but only NHWC is currently supported."});var b,w,C=go(y.shape,m.shape,r,s,o,c);null!=l&&(b=Rt(b=cn(l,"bias","fused conv2d"),v)[0],po(C.outShape,b.shape)),null!=p&&(w=cn(p,"prelu weights","fused conv2d"));var R={x:y,filter:m};null!=l&&(R.bias=b),null!=p&&(R.preluActivationWeights=w);var I=[m,y],k=Nt.runKernelFunc(function(t,e){var n=t.fusedConv2d({input:y,filter:m,convInfo:C,bias:b,activation:f,preluActivationWeights:w});return e([m,y,n]),n},R,function(t,e){var n=e,a=n[0],i=n[1],u=n[2],c=gh(t,u,f);g(Ro(s),function(){return "Error in gradient of fused conv2D: dilation rates greater than 1 are not yet supported in gradients. Got dilations '"+s+"'"});var h={};return null!=l&&(h={bias:function(){return yh(b,c)}}),Object.assign({x:function(){return Nc(i.shape,c,a,r,o)},filter:function(){return Tc(i,c,a.shape,r,o)}},h)},"FusedConv2D",{convInfo:C,activation:f},I,[!0]);return x?k.as3D(k.shape[1],k.shape[2],k.shape[3]):k}}),Ch=wn({fusedDepthwiseConv2d_:function(t){var e=t.x,n=t.filter,r=t.strides,o=t.pad,a=t.dataFormat,i=void 0===a?"NHWC":a,u=t.dilations,s=void 0===u?[1,1]:u,c=t.dimRoundingMode,l=t.bias,h=t.activation,f=void 0===h?"linear":h,p=t.preluActivationWeights;if(!1===mh(Nt.state.gradientDepth,f)){var d=Fc(e,n,r,o,i,s,c);return null!=l&&(d=Ws(d,l)),xh(d,f,p)}var v=cn(e,"x","depthwiseConv2d"),m=cn(n,"filter","depthwiseConv2d"),y=v,x=!1;3===v.rank&&(x=!0,y=v.as4D(1,v.shape[0],v.shape[1],v.shape[2])),g(4===y.rank,function(){return "Error in fused depthwiseConv2d: input must be rank 4, but got rank "+y.rank+"."}),g(4===m.rank,function(){return "Error in fused depthwiseConv2d: filter must be rank 4, but got rank "+m.rank+"."}),g(y.shape[3]===m.shape[2],function(){return "Error in fused depthwiseConv2d: number of input channels ("+y.shape[3]+") must match the inChannels dimension in filter "+m.shape[2]+"."}),null==s&&(s=[1,1]),g(Io(r,s),function(){return "Error in fused depthwiseConv2d: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+s+"'"}),null!=c&&g(E(o),function(){return "Error in fused depthwiseConv2d: pad must be an integer when using dimRoundingMode "+c+" but got pad "+o+"."});var b,w,C=go(y.shape,m.shape,r,s,o,c,!0);null!=l&&(b=Rt(b=cn(l,"bias","fused conv2d"),v)[0],po(C.outShape,b.shape)),null!=p&&(w=cn(p,"prelu weights","fused depthwiseConv2d"));var R={x:y,filter:m};null!=l&&(R.bias=b),null!=p&&(R.preluActivationWeights=w);var I=[m,y],k=Nt.runKernelFunc(function(t,e){var n=t.fusedDepthwiseConv2D({input:y,filter:m,convInfo:C,bias:b,activation:f,preluActivationWeights:w});return e([m,y,n]),n},R,function(t,e){g(Ro(s),function(){return "Error in gradient of fused depthwiseConv2d: dilation rates greater than 1 are not yet supported. Got dilations '"+s+"'"});var n=e[0],r=e[1],o=e[2],a=gh(t,o,f),i={};return null!=l&&(i={bias:function(){return yh(b,a)}}),Object.assign({x:function(){return _c(r.shape,a,n,C)},filter:function(){return Oc(r,a,n.shape,C)}},i)},"FusedDepthwiseConv2D",{convInfo:C,activation:f},I,[!0]);return x?k.as3D(k.shape[1],k.shape[2],k.shape[3]):k}}),Eh=Object.freeze({matMul:bh,conv2d:wh,depthwiseConv2d:Ch}),Rh=Object.freeze({image:vh,linalg:sh,losses:rh,spectral:Ml,fused:Eh,signal:ql,square:Lu,conv1d:Sc,conv2d:Ac,conv3d:Dc,depthwiseConv2d:Fc,separableConv2d:Mc,conv2dTranspose:Bc,conv3dTranspose:Pc,op:wn,batchNormalization2d:Is,batchNormalization3d:ks,batchNormalization4d:Ss,batchNormalization:As,batchNorm:Ds,batchNorm2d:Ts,batchNorm3d:Ns,batchNorm4d:Fs,booleanMaskAsync:Ec,complex:Cn,real:En,imag:Rn,concat:zn,concat1d:Gn,concat2d:Hn,concat3d:qn,concat4d:Kn,split:jn,matMul:Lc,dot:Wc,outerProduct:Uc,reverse:Vc,reverse1d:zc,reverse2d:Gc,reverse3d:Hc,reverse4d:qc,maxPool:Xc,avgPool:$c,pool:Yc,maxPool3d:Qc,avgPool3d:Jc,slice:Zc,slice1d:tl,slice2d:el,slice3d:nl,slice4d:rl,abs:Wu,acos:Uu,acosh:Vu,asin:zu,asinh:Gu,atan:Hu,atanh:qu,ceil:Ku,clipByValue:ju,cos:Xu,cosh:$u,erf:Yu,exp:Qu,expm1:Ju,floor:Zu,log:ts,log1p:es,logSigmoid:ns,neg:rs,reciprocal:os,round:as,rsqrt:is,sigmoid:us,sign:ss,isNaN:cs,isInf:ls,isFinite:hs,sin:fs,sinh:ps,softplus:ds,sqrt:vs,step:ms,tan:gs,tanh:ys,all:al,any:il,argMax:ul,argMin:sl,logSumExp:cl,max:ll,mean:hl,min:fl,moments:pl,sum:dl,prod:vl,equal:uc,equalStrict:sc,greater:cc,greaterEqual:lc,greaterEqualStrict:hc,greaterStrict:fc,less:pc,lessEqual:dc,lessEqualStrict:vc,lessStrict:mc,notEqual:gc,notEqualStrict:yc,add:Ws,addN:Us,addStrict:Vs,atan2:zs,div:Gs,divNoNan:Hs,divStrict:qs,floorDiv:Ks,maximum:js,maximumStrict:Xs,minimum:$s,minimumStrict:Ys,mod:Qs,modStrict:Js,mul:Zs,mulStrict:tc,pow:ec,powStrict:nc,squaredDifference:rc,squaredDifferenceStrict:oc,sub:ac,subStrict:ic,elu:ml,leakyRelu:gl,prelu:yl,relu:xl,relu6:bl,selu:wl,logicalAnd:_s,logicalNot:Os,logicalOr:Ms,logicalXor:Bs,where:Ps,whereAsync:Ls,buffer:ir,print:ur,batchToSpaceND:sr,broadcastTo:cr,cast:lr,clone:hr,cumsum:fr,depthToSpace:pr,expandDims:dr,eye:vr,multinomial:mr,oneHot:gr,pad:yr,pad1d:xr,pad2d:br,pad3d:wr,pad4d:Cr,rand:Er,randomNormal:Rr,randomGamma:Ir,randomUniform:kr,reshape:Sr,spaceToBatchND:Ar,squeeze:Dr,stack:Tr,tile:Nr,truncatedNormal:Fr,unstack:_r,setdiff1dAsync:Or,fill:Pn,linspace:Ln,ones:Mn,range:Wn,scalar:Sn,tensor:In,tensor1d:An,tensor2d:Dn,tensor3d:Tn,tensor4d:Nn,tensor5d:Fn,tensor6d:_n,variable:On,zeros:Bn,onesLike:Un,zerosLike:Vn,transpose:Cl,softmax:io,logSoftmax:uo,localResponseNormalization:El,norm:Rl,gather:wc,unsortedSegmentSum:Cc,basicLSTMCell:Il,multiRNNCell:kl,movingAverage:Sl,stridedSlice:Al,topk:Dl,scatterND:Tl,fft:Nl,ifft:Fl,rfft:_l,irfft:Ol,sparseToDense:Bl,gatherND:Pl,diag:Ll,dropout:Wl,hannWindow:Vl,hammingWindow:zl,frame:Gl,stft:Hl,inTopKAsync:jl});function Ih(t,e){Array.isArray(t)||(t=[t]),t.forEach(function(t){null!=t&&g("complex64"!==t.dtype,function(){return e+" does not support complex64 tensors."});});}function kh(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 Sh=function(t){function o(){var e=t.call(this)||this;return e.blockSize=48,e.firstUse=!0,e.data=new so(e,Nt),e}return e(o,t),o.prototype.write=function(t,e,n){this.firstUse&&(this.firstUse=!1,a().get("IS_NODE")&&an("\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============================\n"));var r={};return this.data.set(r,{values:t,dtype:n}),r},o.prototype.move=function(t,e,n,r){this.data.set(t,{values:e,dtype:r});},o.prototype.numDataIds=function(){return this.data.numDataIds()},o.prototype.read=function(t){return n(this,void 0,void 0,function(){return r(this,function(e){return [2,this.readSync(t)]})})},o.prototype.readSync=function(t){var e=this.data.get(t),n=e.dtype,r=e.complexTensors;return "complex64"===n?No(this.readSync(r.real.dataId),this.readSync(r.imag.dataId)):this.data.get(t).values},o.prototype.bufferSync=function(t){var e=this.readSync(t.dataId),n=e;if("string"===t.dtype)try{n=e.map(function(t){return Z(t)});}catch(t){throw new Error("Failed to decode encoded string bytes into utf-8")}return ir(t.shape,t.dtype,n)},o.prototype.makeOutput=function(t,e,n){var r=this.write(t,e,n);return Nt.makeTensorFromDataId(r,e,n,this)},o.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);}},o.prototype.time=function(t){return n(this,void 0,void 0,function(){var e;return r(this,function(n){return e=Y(),t(),[2,{kernelMs:Y()-e}]})})},o.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."]}},o.prototype.complex=function(t,e){var n=this.makeOutput(null,t.shape,"complex64");return this.data.get(n.dataId).complexTensors={real:Nt.keep(t.clone()),imag:Nt.keep(e.clone())},n},o.prototype.real=function(t){return this.data.get(t.dataId).complexTensors.real.clone()},o.prototype.imag=function(t){return this.data.get(t.dataId).complexTensors.imag.clone()},o.prototype.slice=function(t,e,n){if(Ih(t,"slice"),Yr(t.shape,e,n)){var r=Qr(e,t.strides),o=w(n);return In(this.readSync(t.dataId).subarray(r,r+o),n,t.dtype)}for(var a=ir(n,t.dtype),i=this.bufferSync(t),u=0;uf&&(f=v,p=d);}s[l]=p;}return i},o.prototype.cumsum=function(t,e,n,r){if(Ih(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 o=Ct(t.dtype,"int32"),a=Bn(t.shape,o),i=this.readSync(a.dataId),u=this.readSync(t.dataId),s=t.shape[t.rank-1],c=r?function(t,e){return t+s-e-1}:function(t,e){return t+e},l=0;le?1:0})},o.prototype.greaterEqual=function(t,e){return Ih([t,e],"greaterEqual"),this.broadcastedBinaryOp(t,e,"bool",function(t,e){return t>=e?1:0})},o.prototype.logicalNot=function(t){Ih(t,"logicalNot");for(var e=this.readSync(t.dataId),n=new Uint8Array(e.length),r=0;r1||1===e.rank?1:w(e.shape.slice(1)),l=0;l=0&&e>=0?n:(n+e)%e})},o.prototype.max=function(t,e){Ih(t,"max"),vn("max",e,t.rank);for(var n=pn(t.shape,e),r=n[0],o=n[1],a=Bn(r,t.dtype),i=w(o),u=this.readSync(a.dataId),s=this.readSync(t.dataId),c=0;ch&&(h=p);}u[c]=h;}return a},o.prototype.maximum=function(t,e){return Ih([t,e],"maximum"),this.broadcastedBinaryOp(t,e,t.dtype,function(t,e){return Math.max(t,e)})},o.prototype.all=function(t,e){Ih(t,"all"),vn("all",e,t.rank);for(var n=pn(t.shape,e),r=n[0],o=n[1],a=Bn(r,t.dtype),i=w(o),u=this.readSync(a.dataId),s=this.readSync(t.dataId),c=0;c0?n[r]=1:n[r]=0;return this.makeOutput(n,t.shape,"float32")},o.prototype.isNaN=function(t){Ih(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]=o%2==0?o:o+1;}return this.makeOutput(n,t.shape,"float32")},o.prototype.exp=function(t){Ih(t,"exp");for(var e=this.readSync(t.dataId),n=new Float32Array(e.length),r=0;r=0?o:Math.exp(o)-1;}return this.makeOutput(e,t.shape,"float32")},o.prototype.eluDer=function(t,e){Ih([t,e],"eluDer");for(var n=new Float32Array(e.size),r=this.readSync(e.dataId),o=this.readSync(t.dataId),a=0;a=1?o[a]:o[a]*(i+1);}return this.makeOutput(n,e.shape,"float32")},o.prototype.selu=function(t){Ih(t,"selu");for(var e=ou,n=au,r=new Float32Array(t.size),o=this.readSync(t.dataId),a=0;a=0?n*i:e*(Math.exp(i)-1);}return this.makeOutput(r,t.shape,"float32")},o.prototype.clip=function(t,e,n){Ih(t,"clip");for(var r=new Float32Array(t.size),o=this.readSync(t.dataId),a=0;an?n:i-e,i=r[o]0?1:e;}return this.makeOutput(n,t.shape,"float32")},o.prototype.fusedConv2d=function(t){var e=t.input,n=t.filter,r=t.convInfo,o=t.bias,a=t.activation,i=t.preluActivationWeights,u=this.conv2d(e,n,r);return o&&(u=this.add(u,o)),a&&(u=kh(this,u,a,i)),u},o.prototype.conv2d=function(t,e,n){Ih([t,e],"conv2d");for(var r=n.filterHeight,o=n.filterWidth,a=n.dilationHeight,i=n.dilationWidth,u=n.padInfo.left,s=n.padInfo.top,c="channelsLast"===n.dataFormat,l=ir(n.outShape,t.dtype),h=t.strides[0],f=c?t.strides[1]:t.strides[2],p=c?t.strides[2]:1,d=c?1:t.strides[1],v=l.strides[0],m=c?l.strides[1]:l.strides[2],g=c?l.strides[2]:1,y=c?1:l.strides[1],x=this.readSync(t.dataId),b=this.readSync(e.dataId),w=l.values,C=0;C=n.inHeight))for(var T=A*e.strides[0],N=E+D*f,F=0;F=n.inWidth))for(var P=N+B*p,L=T+M*e.strides[1],W=0;W=n.inDepth))for(var R=C*e.strides[0],I=g+E*t.strides[1],k=0;k=n.inHeight))for(var N=R+D*e.strides[1],F=I+T*t.strides[2],_=0;_=n.inWidth))for(var L=N+B*e.strides[2],W=F+P*n.inChannels,U=L,V=0;V=n.inHeight))for(var C=b*e.strides[0],E=v+w*t.strides[1],R=0;R=n.inWidth))for(var D=C+S*e.strides[1],T=E+A*n.inChannels,N=I,F=D,_=0;_N?N=P:"avg"===n&&(F+=P,_++);}if(isNaN(N))break}d[k+S*g+w]="avg"===n?F/_:N;}return p.toTensor()},o.prototype.maxPool=function(t,e){return this.pool(t,e,"max")},o.prototype.maxPoolPositions=function(t,e){for(var n=ir(e.outShape,"int32"),r=e.strideHeight,o=e.strideWidth,a=e.dilationHeight,i=e.dilationWidth,u=e.effectiveFilterHeight,s=e.effectiveFilterWidth,c=e.padInfo.top,l=e.padInfo.left,h=this.bufferSync(t),f=0;fC&&(C=A,E=I*s+S);}n.set(E,f,d,y,p);}}return n.toTensor()},o.prototype.maxPoolBackprop=function(t,e,n,r){Ih([e,n],"maxPoolBackprop");for(var o=this.maxPoolPositions(e,r),a=r.strideHeight,i=r.strideWidth,u=r.dilationHeight,s=r.dilationWidth,c=r.effectiveFilterHeight,l=r.effectiveFilterWidth,h=l-1-r.padInfo.left,f=c-1-r.padInfo.top,p=ir(e.shape,"float32"),d=this.bufferSync(o),v=this.bufferSync(t),m=0;m=r.outHeight||Math.floor(R)!==R))for(var I=0;I=r.outWidth||Math.floor(k)!==k)){var S=c*l-1-d.get(m,R,k,g)===E*l+I?1:0;if(0!==S)C+=v.get(m,R,k,g)*S;}}}p.set(C,m,y,x,g);}return p.toTensor()},o.prototype.avgPoolBackprop=function(t,e,n){Ih([t,e],"avgPoolBackprop");for(var r=n.strideHeight,o=n.strideWidth,a=n.filterHeight,i=n.filterWidth,u=n.dilationHeight,s=n.dilationWidth,c=n.effectiveFilterHeight,l=n.effectiveFilterWidth,h=l-1-n.padInfo.left,f=c-1-n.padInfo.top,p=ir(e.shape,"float32"),d=1/(a*i),v=this.bufferSync(t),m=0;m=n.outHeight||Math.floor(R)!==R))for(var I=0;I=n.outWidth||Math.floor(k)!==k))C+=v.get(m,R,k,g);}}p.set(C*d,m,y,x,g);}return p.toTensor()},o.prototype.pool3d=function(t,e,n){Ih(t,"pool3d");for(var r=e.strideDepth,o=e.strideHeight,a=e.strideWidth,i=e.dilationDepth,u=e.dilationHeight,s=e.dilationWidth,c=e.effectiveFilterDepth,l=e.effectiveFilterHeight,h=e.effectiveFilterWidth,f=e.padInfo.front,p=e.padInfo.top,d=e.padInfo.left,v="max"===n?Number.NEGATIVE_INFINITY:Number.POSITIVE_INFINITY,m=this.readSync(t.dataId),g=ir(e.outShape,t.dtype),y=g.values,x=e.outShape[1]*e.outShape[2]*e.outShape[3]*e.outShape[4],b=e.outShape[2]*e.outShape[3]*e.outShape[4],w=e.outShape[3]*e.outShape[4],C=e.outShape[4],E=0;Ez?z=Y:"avg"===n&&(G+=Y,H++),isNaN(z))break}if(isNaN(z))break}if(isNaN(z))break}y[V+k]="avg"===n?G/H:z;}}}return g.toTensor()},o.prototype.avgPool3d=function(t,e){return Ih(t,"avgPool3d"),this.pool3d(t,e,"avg").toFloat()},o.prototype.avgPool3dBackprop=function(t,e,n){Ih([t,e],"avgPool3dBackprop");for(var r=n.strideDepth,o=n.strideHeight,a=n.strideWidth,i=n.filterDepth,u=n.filterHeight,s=n.filterWidth,c=n.dilationDepth,l=n.dilationHeight,h=n.dilationWidth,f=n.effectiveFilterDepth,p=n.effectiveFilterHeight,d=n.effectiveFilterWidth,v=f-1-n.padInfo.front,m=d-1-n.padInfo.left,g=p-1-n.padInfo.top,y=ir(e.shape,"float32"),x=1/(i*u*s),b=this.bufferSync(t),w=0;w=n.outDepth||Math.floor(N)!==N))for(var F=0;F=n.outHeight||Math.floor(_)!==_))for(var O=0;O=n.outWidth||Math.floor(M)!==M))D+=b.get(w,N,_,M,C);}}}y.set(D*x,w,E,R,I,C);}return y.toTensor()},o.prototype.maxPool3d=function(t,e){return Ih(t,"maxPool3d"),this.pool3d(t,e,"max").toFloat()},o.prototype.maxPool3dPositions=function(t,e){for(var n=ir(e.outShape,"int32"),r=e.strideDepth,o=e.strideHeight,a=e.strideWidth,i=e.dilationDepth,u=e.dilationHeight,s=e.dilationWidth,c=e.effectiveFilterDepth,l=e.effectiveFilterHeight,h=e.effectiveFilterWidth,f=e.padInfo.front,p=e.padInfo.top,d=e.padInfo.left,v=this.bufferSync(t),m=0;m=T&&(T=L,N=_*l*h+M*l+P);}n.set(N,m,y,C,k,g);}}}return n.toTensor()},o.prototype.maxPool3dBackprop=function(t,e,n,r){Ih([e,n],"maxPool3dBackprop");for(var o=this.maxPool3dPositions(e,r),a=r.strideDepth,i=r.strideHeight,u=r.strideWidth,s=r.dilationDepth,c=r.dilationHeight,l=r.dilationWidth,h=r.effectiveFilterDepth,f=r.effectiveFilterHeight,p=r.effectiveFilterWidth,d=h-1-r.padInfo.front,v=p-1-r.padInfo.left,m=f-1-r.padInfo.top,g=ir(e.shape,"float32"),y=this.bufferSync(o),x=this.bufferSync(t),b=0;b=r.outDepth||Math.floor(T)!==T))for(var N=0;N=r.outHeight||Math.floor(F)!==F))for(var _=0;_=r.outWidth||Math.floor(O)!==O)){var M=h*f*p-1-y.get(b,T,F,O,w)===D*f*p+N*p+_?1:0;if(0!==M)A+=x.get(b,T,F,O,w)*M;}}}}g.set(A,b,C,E,R,w);}return g.toTensor()},o.prototype.cast=function(t,e){return So(t,e,this)},o.prototype.reshape=function(t,e){return Ao(t,e)},o.prototype.avgPool=function(t,e){return Ih(t,"avgPool"),this.pool(t,e,"avg").toFloat()},o.prototype.resizeBilinear=function(t,e,n,r){Ih(t,"resizeBilinear");for(var o=t.shape,a=o[0],i=o[1],u=o[2],s=o[3],c=this.readSync(t.dataId),l=new Float32Array(w([a,e,n,s])),h=[r&&e>1?i-1:i,r&&n>1?u-1:u],f=[r&&e>1?e-1:e,r&&n>1?n-1:n],p=0,d=h[0]/f[0],v=h[1]/f[1],m=0;m1?a-1:a,n&&l>1?i-1:i],p=[n&&c>1?c-1:c,n&&l>1?l-1:l],d=f[0]/p[0],v=f[1]/p[1],m=this.readSync(t.dataId),g=0,y=0;y1?i-1:i,r&&n>1?u-1:u],f=[r&&e>1?e-1:e,r&&n>1?n-1:n],p=h[0]/f[0],d=h[1]/f[1],v=0,m=0;m1?a-1:a,n&&l>1?i-1:i],d=[n&&c>1?c-1:c,n&&l>1?l-1:l],v=p[0]/d[0],m=p[1]/d[1],g=1/v,y=1/m,x=2*Math.ceil(g)+2,b=2*Math.ceil(y)+2,w=0;w=c)){var M=C+O*t.strides[1],B=O*v;if(E===Math.min(a-1,n?Math.round(B):Math.floor(B)))for(var P=0;P=l)){var W=M+L*t.strides[2],U=L*m;S===Math.min(i-1,n?Math.round(U):Math.floor(U))&&(F+=f[W+N]);}}}}h[A+N]=F;}return Nn(h,e.shape,e.dtype)},o.prototype.batchNormalization=function(t,e,n,r,o,a){Ih([t,e,n,o,a],"batchNorm");for(var i=this.readSync(t.dataId),u=this.readSync(e.dataId),s=this.readSync(n.dataId),c=o?this.readSync(o.dataId):new Float32Array([1]),l=a?this.readSync(a.dataId):new Float32Array([0]),h=new Float32Array(i.length),f=l.length,p=c.length,d=s.length,v=u.length,m=0,g=0,y=0,x=0,b=0;b=f&&(m=0),g>=v&&(g=0),y>=p&&(y=0),x>=d&&(x=0);return Nn(h,t.shape)},o.prototype.localResponseNormalization4D=function(t,e,n,r,o){Ih(t,"localResponseNormalization4D");var a=t.shape[3],i=a-1,u=this.readSync(t.dataId),s=t.size,c=new Float32Array(s);function l(t){for(var n=t%a,r=t-n+Math.max(0,n-e),o=t-n+Math.min(n+e,i),s=0;r<=o;r++){var c=u[r];s+=c*c;}return s}for(var h=0;h=0&&a[i]1,function(){return "blockSize should be > 1 for depthToSpace, but was: "+e});for(var r=t.shape[0],o=t.shape[1],a=t.shape[2],i=t.shape[3],u=o*e,s=a*e,c=i/(e*e),l=this.readSync(t.dataId),h=new Float32Array(r*u*s*c),f=0,p=0;p=u))for(var S=f>1?(R-C)*(s-1)/(f-1):0,A=p>1?(I-E)*(c-1)/(p-1):0,D=0;D1?C*(s-1)+D*S:.5*(C+R)*(s-1);if(T<0||T>s-1)for(var N=0;N1?E*(c-1)+N*A:.5*(E+I)*(c-1))<0||q>c-1)for(F=0;F1?E*(c-1)+N*A:.5*(E+I)*(c-1))<0||q>c-1)for(F=0;F=t.size/u)throw new Error("Invalid indices: "+p+" does not index into "+t.shape);for(var g=0;g=r/o)throw new Error("Invalid indices: "+v+" does not index into "+n);for(var x=0;x0,function(){return "scheme must not be an empty string."});var r=t.getInstance();g(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 jh(t){if(-1===t.indexOf(qh))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+Kh.getSchemes().join(","));return {scheme:t.split(qh)[0],path:t.split(qh)[1]}}function Xh(t,e,o){return void 0===o&&(o=!1),n(this,void 0,void 0,function(){var n,a,i,u,s,c,l,h,f;return r(this,function(r){switch(r.label){case 0:return g(t!==e,function(){return "Old path and new path are the same: '"+t+"'"}),g((n=Hh.getLoadHandlers(t)).length>0,function(){return "Copying failed because no load handler is found for source URL "+t+"."}),g(n.length<2,function(){return "Copying failed because more than one ("+n.length+") load handlers for source URL "+t+"."}),a=n[0],g((i=Hh.getSaveHandlers(e)).length>0,function(){return "Copying failed because no save handler is found for destination URL "+e+"."}),g(i.length<2,function(){return "Copying failed because more than one ("+n.length+") save handlers for destination URL "+e+"."}),u=i[0],s=jh(t).scheme,c=jh(t).path,l=s===jh(t).scheme,[4,a.load()];case 1:return h=r.sent(),o&&l?[4,Kh.getManager(s).removeModel(c)]:[3,3];case 2:r.sent(),r.label=3;case 3:return [4,u.save(h)];case 4:return f=r.sent(),!o||l?[3,6]:[4,Kh.getManager(s).removeModel(c)];case 5:r.sent(),r.label=6;case 6:return [2,f.modelArtifactsInfo]}})})}var $h="models_store",Yh="model_info_store";function Qh(){if(!a().getBool("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var t=window,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 Jh(t){var e=t.result;e.createObjectStore($h,{keyPath:"modelPath"}),e.createObjectStore(Yh,{keyPath:"modelPath"});}var Zh=function(){function t(t){if(this.indexedDB=Qh(),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 n(this,void 0,void 0,function(){return r(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 n(this,void 0,void 0,function(){return r(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 o=n.indexedDB.open("tensorflowjs",1);o.onupgradeneeded=function(){return Jh(o)},o.onsuccess=function(){var a=o.result;if(null==e){var i=a.transaction($h,"readonly"),u=i.objectStore($h).get(n.modelPath);u.onsuccess=function(){if(null==u.result)return a.close(),r(new Error("Cannot find model with path '"+n.modelPath+"' in IndexedDB."));t(u.result.modelArtifacts);},u.onerror=function(t){return a.close(),r(u.error)},i.oncomplete=function(){return a.close()};}else{var s,c=Gh(e),l=a.transaction(Yh,"readwrite"),h=l.objectStore(Yh),f=h.put({modelPath:n.modelPath,modelArtifactsInfo:c});f.onsuccess=function(){var o=(s=a.transaction($h,"readwrite")).objectStore($h).put({modelPath:n.modelPath,modelArtifacts:e,modelArtifactsInfo:c});o.onsuccess=function(){return t({modelArtifactsInfo:c})},o.onerror=function(t){var e=(h=l.objectStore(Yh)).delete(n.modelPath);e.onsuccess=function(){return a.close(),r(o.error)},e.onerror=function(t){return a.close(),r(o.error)};};},f.onerror=function(t){return a.close(),r(f.error)},l.oncomplete=function(){null==s?a.close():s.oncomplete=function(){return a.close()};};}},o.onerror=function(t){return r(o.error)};})},t.URL_SCHEME="indexeddb://",t}(),tf=function(t){return a().getBool("IS_BROWSER")&&!Array.isArray(t)&&t.startsWith(Zh.URL_SCHEME)?(e=t.slice(Zh.URL_SCHEME.length),new Zh(e)):null;var e;};Hh.registerSaveRouter(tf),Hh.registerLoadRouter(tf);var ef=function(){function t(){this.indexedDB=Qh();}return t.prototype.listModels=function(){return n(this,void 0,void 0,function(){var t=this;return r(this,function(e){return [2,new Promise(function(e,n){var r=t.indexedDB.open("tensorflowjs",1);r.onupgradeneeded=function(){return Jh(r)},r.onsuccess=function(){var t=r.result,o=t.transaction(Yh,"readonly"),a=o.objectStore(Yh).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){g(t>=0&&t<=1,function(){return "Progress fraction must be in range [0, 1], but got startFraction "+t}),g(e>=0&&e<=1,function(){return "Progress fraction must be in range [0, 1], but got endFraction "+e}),g(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 o=0;return Promise.all(t.map(function(a){return a.then(function(a){var i=n+ ++o/t.length*(r-n);return e(i),a}),a}))}function Cf(t,e){return n(this,void 0,void 0,function(){var n,o,i,u,s,c,l,h,f;return r(this,function(r){switch(r.label){case 0:return null==e&&(e={}),n=null==e.fetchFunc?a().platform.fetch:e.fetchFunc,o=t.map(function(t){return n(t,e.requestInit,{isBinary:!0})}),i=0,u=.5,null!=e.onProgress?[3,2]:[4,Promise.all(o)];case 1:return s=r.sent(),[3,4];case 2:return [4,wf(o,e.onProgress,i,u)];case 3:s=r.sent(),r.label=4;case 4:return c=s.map(function(t){return t.arrayBuffer()}),l=.5,h=1,null!=e.onProgress?[3,6]:[4,Promise.all(c)];case 5:return f=r.sent(),[3,8];case 6:return [4,wf(c,e.onProgress,l,h)];case 7:f=r.sent(),r.label=8;case 8:return [2,f]}})})}function Ef(t){var e=this;return function(o,a,i){return void 0===a&&(a=""),n(e,void 0,void 0,function(){var e,n,u,s,c,l,h,f,p,d;return r(this,function(r){switch(r.label){case 0:if(e=o.map(function(){return !1}),n={},u=null!=i?i.map(function(){return !1}):[],s=[],o.forEach(function(t,r){var o=0;t.weights.forEach(function(t){var a="quantization"in t?t.quantization.dtype:t.dtype,c=Mh[a]*w(t.shape),l=function(){e[r]=!0,null==n[r]&&(n[r]=[]),n[r].push({manifestEntry:t,groupOffset:o,sizeBytes:c});};null!=i?i.forEach(function(e,n){e===t.name&&(l(),u[n]=!0);}):l(),s.push(t.name),o+=c;});}),!u.every(function(t){return t}))throw c=i.filter(function(t,e){return !u[e]}),new Error("Could not find weights in manifest with names: "+c.join(", ")+". \nManifest JSON has weights with names: "+s.join(", ")+".");return l=e.reduce(function(t,e,n){return e&&t.push(n),t},[]),h=[],l.forEach(function(t){o[t].paths.forEach(function(t){var e=a+(a.endsWith("/")?"":"/")+t;h.push(e);});}),[4,t(h)];case 1:return f=r.sent(),p={},d=0,l.forEach(function(t){for(var e=o[t].paths.length,r=0,a=0;a0,function(){return "URL path for http must not be null, undefined or empty."}),Array.isArray(t)&&g(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 n(this,void 0,void 0,function(){var e,n,o,a;return r(this,function(r){switch(r.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}],o={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(o)],{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((a=r.sent()).ok)return [2,{modelArtifactsInfo:Gh(t),responses:[a]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+a.status+".")}})})},t.prototype.load=function(){return n(this,void 0,void 0,function(){var t,e,n,o,a,i,u,s,c,l,h,f;return r(this,function(r){switch(r.label){case 0:return [4,this.fetch(this.path,this.requestInit)];case 1:if(!(t=r.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.");r.label=2;case 2:return r.trys.push([2,4,,5]),[4,t.json()];case 3:return e=r.sent(),[3,5];case 4:throw r.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(o=e.modelTopology,a=e.weightsManifest,i=e.generatedBy,u=e.convertedBy,s=e.format,c=e.userDefinedMetadata,null==o&&null==a)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");return null==a?[3,7]:[4,this.loadWeights(a)];case 6:f=r.sent(),l=f[0],h=f[1],r.label=7;case 7:return [2,{modelTopology:o,weightSpecs:l,weightData:h,userDefinedMetadata:c,generatedBy:i,convertedBy:u,format:s}]}})})},t.prototype.loadWeights=function(t){return n(this,void 0,void 0,function(){var e,n,o,a,i,u,s,c,l,h,f;return r(this,function(r){switch(r.label){case 0:for(e=Array.isArray(this.path)?this.path[1]:this.path,n=function(t){var e=t.lastIndexOf("/"),n=t.lastIndexOf("?"),r=t.substring(0,e),o=n>e?t.substring(n):"";return [r+"/",o]}(e),o=n[0],a=n[1],i=this.weightPathPrefix||o,u=[],s=0,c=t;s0&&Number.isInteger(n),function(){return "If provided, numClasses must be a positive integer, but got "+n}),g(1===r.rank,function(){return "Expected the rank of labels to be 1, but got "+r.rank}),g(1===o.rank,function(){return "Expected the rank of predictions to be 1, but got "+o.rank}),g(r.shape[0]===o.shape[0],function(){return "Mismatch in the number of examples: "+r.shape[0]+" vs. "+o.shape[0]+". Labels and predictions should have the same number of elements."}),g(n>0&&Number.isInteger(n),function(){return "numClasses is required to be a positive integer, but got "+n});var a=gr(r.asType("int32"),n),i=gr(o.asType("int32"),n);return a.transpose().matMul(i).asType("int32")}}),_f=Object.freeze({confusionMatrix:Ff});var Of=wn({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,o=!1,a=!1,i=!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)o=!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);i=!0;}if(o&&o&&t.readyState<2)throw new Error("The video element has not loaded data yet. Please wait for `loadeddata` event on the