@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
17 lines • 255 kB
JavaScript
/**
* @license
* Copyright 2024 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).tf=e.tf||{})}(this,(function(e){"use strict";function t(e,t){return t.forEach((function(t){t&&"string"!=typeof t&&!Array.isArray(t)&&Object.keys(t).forEach((function(n){if("default"!==n&&!(n in e)){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}}))})),e}class n{refCount(e){return r("refCount")}incRef(e){return r("incRef")}timerAvailable(){return!0}time(e){return r("time")}read(e){return r("read")}readSync(e){return r("readSync")}readToGPU(e,t){return r("readToGPU")}numDataIds(){return r("numDataIds")}disposeData(e,t){return r("disposeData")}write(e,t,n){return r("write")}move(e,t,n,s,o){return r("move")}createTensorFromGPUData(e,t,n){return r("createTensorFromGPUData")}memory(){return r("memory")}floatPrecision(){return r("floatPrecision")}epsilon(){return 32===this.floatPrecision()?1e-7:1e-4}dispose(){return r("dispose")}}function r(e){throw new Error(`'${e}' not yet implemented or not found in the registry. This kernel may not be supported by the tfjs backend you have chosen`)}function s(e){let t=e.length,n=0;for(;t>0;)n=Math.random()*t|0,t--,a(e,t,n)}function o(e,t,n){return Math.max(e,Math.min(t,n))}function a(e,t,n){const r=e[t];e[t]=e[n],e[n]=r}function i(e,t){if(!e)throw new Error("string"==typeof t?t:t())}function l(e,t,n=""){i(d(e,t),(()=>n+` Shapes ${e} and ${t} must match`))}function u(e){i(null!=e,(()=>"The input to the tensor constructor must be a non-null value."))}function c(e){if(0===e.length)return 1;let t=e[0];for(let n=1;n<e.length;n++)t*=e[n];return t}function h(e,t){if(e===t)return!0;if(null==e||null==t)return!1;if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(null!==e[n]&&null!==t[n]&&e[n]!==t[n])return!1;return!0}function d(e,t){if(e===t)return!0;if(null==e||null==t)return!1;if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function p(e){return e%1==0}function f(e,t){return t<=e.length?e:e+" ".repeat(t-e.length)}function g(e,t){const n=t.length;return i((e=null==e?t.map(((e,t)=>t)):[].concat(e)).every((e=>e>=-n&&e<n)),(()=>`All values in axis param must be in range [-${n}, ${n}) but got axis ${e}`)),i(e.every((e=>p(e))),(()=>`All values in axis param must be integers but got axis ${e}`)),e.map((e=>e<0?n+e:e))}function m(e,t){const n=[],r=[],s=null!=t&&Array.isArray(t)&&0===t.length,o=null==t||s?null:g(t,e).sort();let a=0;for(let t=0;t<e.length;++t){if(null!=o){if(o[a]===t&&1!==e[t])throw new Error(`Can't squeeze axis ${t} since its dim '${e[t]}' is not 1`);(null==o[a]||o[a]>t)&&1===e[t]&&(n.push(e[t]),r.push(t)),o[a]<=t&&a++}1!==e[t]&&(n.push(e[t]),r.push(t))}return{newShape:n,keptDims:r}}function b(e,t){return y(e,t)}function y(e,t){let n=null;if(null==e||"float32"===e)n=new Float32Array(t);else if("int32"===e)n=new Int32Array(t);else if("bool"===e)n=new Uint8Array(t);else{if("string"!==e)throw new Error(`Unknown data type ${e}`);n=new Array(t)}return n}function w(e,t){for(let n=0;n<e.length;n++){const r=e[n];if(isNaN(r)||!isFinite(r))throw Error(`A tensor of type ${t} being uploaded contains ${r}.`)}}function k(e){return"bool"===e||"complex64"===e||"float32"===e||"int32"===e||"string"===e}function v(e){if("float32"===e||"int32"===e)return 4;if("complex64"===e)return 8;if("bool"===e)return 1;throw new Error(`Unknown dtype ${e}`)}function x(e){if(null==e)return 0;let t=0;return e.forEach((e=>t+=e.length)),t}function E(e){return"string"==typeof e||e instanceof String}function S(e){return"boolean"==typeof e}function $(e){return"number"==typeof e}function A(e){return Array.isArray(e)?A(e[0]):e instanceof Float32Array?"float32":e instanceof Int32Array||e instanceof Uint8Array||e instanceof Uint8ClampedArray?"int32":$(e)?"float32":E(e)?"string":S(e)?"bool":"float32"}function _(e){return!!(e&&e.constructor&&e.call&&e.apply)}function I(e,t){for(let n=t;n<e;++n)if(e%n==0)return n;return e}function N(e){const t=e.length;if(t<2)return[];const n=new Array(t-1);n[t-2]=e[t-1];for(let r=t-3;r>=0;--r)n[r]=n[r+1]*e[r+1];return n}function M(e,t,n,r=!1){const s=new Array;if(1===t.length){const o=t[0]*(r?2:1);for(let t=0;t<o;t++)s[t]=n[e+t]}else{const o=t[0],a=t.slice(1),i=a.reduce(((e,t)=>e*t))*(r?2:1);for(let t=0;t<o;t++)s[t]=M(e+t*i,a,n,r)}return s}function T(e,t,n=!1){if(0===e.length)return t[0];const r=e.reduce(((e,t)=>e*t))*(n?2:1);if(0===r)return[];if(r!==t.length)throw new Error(`[${e}] does not match the input size ${t.length}${n?" for a complex tensor":""}.`);return M(0,e,t,n)}function D(e,t){const n=R(e,t);for(let e=0;e<n.length;e++)n[e]=1;return n}function R(e,t){if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return new Int32Array(e);if("bool"===t)return new Uint8Array(e);throw new Error(`Unknown data type ${t}`)}function B(e){e.forEach((t=>{i(Number.isInteger(t)&&t>=0,(()=>`Tensor must have a shape comprised of positive integers but got shape [${e}].`))}))}function F(e){return e&&e.then&&"function"==typeof e.then}const C="tfjsflags";class P{constructor(e){this.global=e,this.flags={},this.flagRegistry={},this.urlFlags={},this.getQueryParams=O,this.populateURLFlags()}setPlatform(e,t){null!=this.platform&&(L().getBool("IS_TEST")||L().getBool("PROD")||console.warn(`Platform ${this.platformName} has already been set. Overwriting the platform with ${e}.`)),this.platformName=e,this.platform=t}registerFlag(e,t,n){if(this.flagRegistry[e]={evaluationFn:t,setHook:n},null!=this.urlFlags[e]){const t=this.urlFlags[e];L().getBool("IS_TEST")||L().getBool("PROD")||console.warn(`Setting feature override from URL ${e}: ${t}.`),this.set(e,t)}}async getAsync(e){return e in this.flags||(this.flags[e]=await this.evaluateFlag(e)),this.flags[e]}get(e){if(e in this.flags)return this.flags[e];const t=this.evaluateFlag(e);if(F(t))throw new Error(`Flag ${e} cannot be synchronously evaluated. Please use getAsync() instead.`);return this.flags[e]=t,this.flags[e]}getNumber(e){return this.get(e)}getBool(e){return this.get(e)}getString(e){return this.get(e)}getFlags(){return this.flags}get features(){return this.flags}set(e,t){if(null==this.flagRegistry[e])throw new Error(`Cannot set flag ${e} as it has not been registered.`);this.flags[e]=t,null!=this.flagRegistry[e].setHook&&this.flagRegistry[e].setHook(t)}evaluateFlag(e){if(null==this.flagRegistry[e])throw new Error(`Cannot evaluate flag '${e}': no evaluation function found.`);return this.flagRegistry[e].evaluationFn()}setFlags(e){this.flags=Object.assign({},e)}reset(){this.flags={},this.urlFlags={},this.populateURLFlags()}populateURLFlags(){if("undefined"==typeof this.global||"undefined"==typeof this.global.location||"undefined"==typeof this.global.location.search)return;const e=this.getQueryParams(this.global.location.search);if(C in e){e.tfjsflags.split(",").forEach((e=>{const[t,n]=e.split(":");this.urlFlags[t]=function(e,t){const n=t.toLowerCase();return"true"===n||"false"===n?"true"===n:""+ +n===n?+n:t}(0,n)}))}}}function O(e){const t={};return e.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,((e,...n)=>(function(e,t,n){e[decodeURIComponent(t)]=decodeURIComponent(n||"")}(t,n[0],n[1]),n.join("=")))),t}function L(){return e.ENV}let z;function U(){if(null==z){let e;if("undefined"!=typeof window)e=window;else if("undefined"!=typeof global)e=global;else if("undefined"!=typeof process)e=process;else{if("undefined"==typeof self)throw new Error("Could not find a global object");e=self}z=e}return z}function W(e,t){const n=function(){const e=U();return null==e._tfGlobals&&(e._tfGlobals=new Map),e._tfGlobals}();if(n.has(e))return n.get(e);{const r=t();return n.set(e,r),n.get(e)}}e.ENV=null;const G="Acos",q="Acosh",K="Add",V="AddN",H="ArgMax",j="ArgMin",J="Asin",Z="Asinh",Y="Atan",X="Atanh",Q="Atan2",ee="AvgPool",te="AvgPool3D",ne="BatchMatMul",re="BatchToSpaceND",se="Bincount",oe="BitwiseAnd",ae="BroadcastArgs",ie="Cast",le="Ceil",ue="ClipByValue",ce="Complex",he="ComplexAbs",de="Concat",pe="Conv2D",fe="Conv2DBackpropFilter",ge="Conv2DBackpropInput",me="Conv3D",be="Conv3DBackpropInputV2",ye="Cosh",we="Cumprod",ke="Cumsum",ve="CropAndResize",xe="DenseBincount",Ee="DepthToSpace",Se="DepthwiseConv2dNative",$e="DepthwiseConv2dNativeBackpropFilter",Ae="DepthwiseConv2dNativeBackpropInput",_e="Diag",Ie="Dilation2D",Ne="Draw",Me="RealDiv",Te="Einsum",De="Equal",Re="ExpandDims",Be="Expm1",Fe="Fill",Ce="FlipLeftRight",Pe="Floor",Oe="FloorDiv",Le="FusedBatchNorm",ze="GatherV2",Ue="GatherNd",We="Greater",Ge="GreaterEqual",qe="Identity",Ke="IFFT",Ve="Imag",He="IsFinite",je="IsInf",Je="IsNan",Ze="LeakyRelu",Ye="Less",Xe="LessEqual",Qe="LinSpace",et="Log1p",tt="LogicalAnd",nt="LogicalNot",rt="LogicalOr",st="Maximum",ot="MaxPool",at="MaxPool3D",it="MaxPoolWithArgmax",lt="Mean",ut="Minimum",ct="MirrorPad",ht="Multinomial",dt="Multiply",pt="NotEqual",ft="NonMaxSuppressionV3",gt="NonMaxSuppressionV4",mt="NonMaxSuppressionV5",bt="OnesLike",yt="OneHot",wt="Pack",kt="PadV2",vt="Prelu",xt="Prod",Et="RaggedGather",St="RaggedRange",$t="RaggedTensorToTensor",At="Range",_t="Real",It="Reciprocal",Nt="Relu",Mt="Reshape",Tt="ResizeNearestNeighbor",Dt="ResizeBilinear",Rt="Relu6",Bt="Reverse",Ft="Round",Ct="Rsqrt",Pt="ScatterNd",Ot="TensorScatterUpdate",Lt="SearchSorted",zt="Select",Ut="Selu",Wt="Slice",Gt="Sinh",qt="Sign",Kt="Sigmoid",Vt="Softplus",Ht="Sqrt",jt="SpaceToBatchND",Jt="SplitV",Zt="Softmax",Yt="SparseFillEmptyRows",Xt="SparseReshape",Qt="SparseSegmentMean",en="SparseSegmentSum",tn="SparseToDense",nn="SquaredDifference",rn="StaticRegexReplace",sn="StridedSlice",on="StringNGrams",an="StringSplit",ln="StringToHashBucketFast",un="Tanh",cn="Tile",hn="TopK",dn="Transform",pn="Transpose",fn="Unique",gn="Unpack",mn="UnsortedSegmentSum",bn="ZerosLike",yn="Step",wn="FromPixels",kn="RotateWithOffset",vn="_FusedMatMul",xn="FusedConv2D",En="FusedDepthwiseConv2D";function Sn(...e){L().getBool("IS_TEST")||L().getBool("PROD")||console.warn(...e)}const $n=W("kernelRegistry",(()=>new Map)),An=W("gradRegistry",(()=>new Map));function _n(e,t){const n=Tn(e,t);return $n.get(n)}function In(e){return An.get(e)}function Nn(e){const t=$n.entries(),n=[];for(;;){const{done:r,value:s}=t.next();if(r)break;const[o,a]=s,[i]=o.split("_");i===e&&n.push(a)}return n}function Mn(e){const{kernelName:t,backendName:n}=e,r=Tn(t,n);$n.has(r)&&Sn(`The kernel '${t}' for backend '${n}' is already registered`),$n.set(r,e)}function Tn(e,t){return`${t}_${e}`}function Dn(e){return e instanceof Float32Array||e instanceof Int32Array||e instanceof Uint8Array||e instanceof Uint8ClampedArray}var Rn="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function Bn(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var Fn=Pn,Cn=null;try{Cn=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch(e){}function Pn(e,t,n){this.low=0|e,this.high=0|t,this.unsigned=!!n}function On(e){return!0===(e&&e.__isLong__)}Pn.prototype.__isLong__,Object.defineProperty(Pn.prototype,"__isLong__",{value:!0}),Pn.isLong=On;var Ln={},zn={};function Un(e,t){var n,r,s;return t?(s=0<=(e>>>=0)&&e<256)&&(r=zn[e])?r:(n=Gn(e,(0|e)<0?-1:0,!0),s&&(zn[e]=n),n):(s=-128<=(e|=0)&&e<128)&&(r=Ln[e])?r:(n=Gn(e,e<0?-1:0,!1),s&&(Ln[e]=n),n)}function Wn(e,t){if(isNaN(e))return t?Xn:Yn;if(t){if(e<0)return Xn;if(e>=jn)return rr}else{if(e<=-Jn)return sr;if(e+1>=Jn)return nr}return e<0?Wn(-e,t).neg():Gn(e%Hn|0,e/Hn|0,t)}function Gn(e,t,n){return new Pn(e,t,n)}Pn.fromInt=Un,Pn.fromNumber=Wn,Pn.fromBits=Gn;var qn=Math.pow;function Kn(e,t,n){if(0===e.length)throw Error("empty string");if("NaN"===e||"Infinity"===e||"+Infinity"===e||"-Infinity"===e)return Yn;if("number"==typeof t?(n=t,t=!1):t=!!t,(n=n||10)<2||36<n)throw RangeError("radix");var r;if((r=e.indexOf("-"))>0)throw Error("interior hyphen");if(0===r)return Kn(e.substring(1),t,n).neg();for(var s=Wn(qn(n,8)),o=Yn,a=0;a<e.length;a+=8){var i=Math.min(8,e.length-a),l=parseInt(e.substring(a,a+i),n);if(i<8){var u=Wn(qn(n,i));o=o.mul(u).add(Wn(l))}else o=(o=o.mul(s)).add(Wn(l))}return o.unsigned=t,o}function Vn(e,t){return"number"==typeof e?Wn(e,t):"string"==typeof e?Kn(e,t):Gn(e.low,e.high,"boolean"==typeof t?t:e.unsigned)}Pn.fromString=Kn,Pn.fromValue=Vn;var Hn=4294967296,jn=Hn*Hn,Jn=jn/2,Zn=Un(1<<24),Yn=Un(0);Pn.ZERO=Yn;var Xn=Un(0,!0);Pn.UZERO=Xn;var Qn=Un(1);Pn.ONE=Qn;var er=Un(1,!0);Pn.UONE=er;var tr=Un(-1);Pn.NEG_ONE=tr;var nr=Gn(-1,2147483647,!1);Pn.MAX_VALUE=nr;var rr=Gn(-1,-1,!0);Pn.MAX_UNSIGNED_VALUE=rr;var sr=Gn(0,-2147483648,!1);Pn.MIN_VALUE=sr;var or=Pn.prototype;or.toInt=function(){return this.unsigned?this.low>>>0:this.low},or.toNumber=function(){return this.unsigned?(this.high>>>0)*Hn+(this.low>>>0):this.high*Hn+(this.low>>>0)},or.toString=function(e){if((e=e||10)<2||36<e)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative()){if(this.eq(sr)){var t=Wn(e),n=this.div(t),r=n.mul(t).sub(this);return n.toString(e)+r.toInt().toString(e)}return"-"+this.neg().toString(e)}for(var s=Wn(qn(e,6),this.unsigned),o=this,a="";;){var i=o.div(s),l=(o.sub(i.mul(s)).toInt()>>>0).toString(e);if((o=i).isZero())return l+a;for(;l.length<6;)l="0"+l;a=""+l+a}},or.getHighBits=function(){return this.high},or.getHighBitsUnsigned=function(){return this.high>>>0},or.getLowBits=function(){return this.low},or.getLowBitsUnsigned=function(){return this.low>>>0},or.getNumBitsAbs=function(){if(this.isNegative())return this.eq(sr)?64:this.neg().getNumBitsAbs();for(var e=0!=this.high?this.high:this.low,t=31;t>0&&0==(e&1<<t);t--);return 0!=this.high?t+33:t+1},or.isZero=function(){return 0===this.high&&0===this.low},or.eqz=or.isZero,or.isNegative=function(){return!this.unsigned&&this.high<0},or.isPositive=function(){return this.unsigned||this.high>=0},or.isOdd=function(){return 1==(1&this.low)},or.isEven=function(){return 0==(1&this.low)},or.equals=function(e){return On(e)||(e=Vn(e)),(this.unsigned===e.unsigned||this.high>>>31!=1||e.high>>>31!=1)&&(this.high===e.high&&this.low===e.low)},or.eq=or.equals,or.notEquals=function(e){return!this.eq(e)},or.neq=or.notEquals,or.ne=or.notEquals,or.lessThan=function(e){return this.comp(e)<0},or.lt=or.lessThan,or.lessThanOrEqual=function(e){return this.comp(e)<=0},or.lte=or.lessThanOrEqual,or.le=or.lessThanOrEqual,or.greaterThan=function(e){return this.comp(e)>0},or.gt=or.greaterThan,or.greaterThanOrEqual=function(e){return this.comp(e)>=0},or.gte=or.greaterThanOrEqual,or.ge=or.greaterThanOrEqual,or.compare=function(e){if(On(e)||(e=Vn(e)),this.eq(e))return 0;var t=this.isNegative(),n=e.isNegative();return t&&!n?-1:!t&&n?1:this.unsigned?e.high>>>0>this.high>>>0||e.high===this.high&&e.low>>>0>this.low>>>0?-1:1:this.sub(e).isNegative()?-1:1},or.comp=or.compare,or.negate=function(){return!this.unsigned&&this.eq(sr)?sr:this.not().add(Qn)},or.neg=or.negate,or.add=function(e){On(e)||(e=Vn(e));var t=this.high>>>16,n=65535&this.high,r=this.low>>>16,s=65535&this.low,o=e.high>>>16,a=65535&e.high,i=e.low>>>16,l=0,u=0,c=0,h=0;return c+=(h+=s+(65535&e.low))>>>16,u+=(c+=r+i)>>>16,l+=(u+=n+a)>>>16,l+=t+o,Gn((c&=65535)<<16|(h&=65535),(l&=65535)<<16|(u&=65535),this.unsigned)},or.subtract=function(e){return On(e)||(e=Vn(e)),this.add(e.neg())},or.sub=or.subtract,or.multiply=function(e){if(this.isZero())return Yn;if(On(e)||(e=Vn(e)),Cn)return Gn(Cn.mul(this.low,this.high,e.low,e.high),Cn.get_high(),this.unsigned);if(e.isZero())return Yn;if(this.eq(sr))return e.isOdd()?sr:Yn;if(e.eq(sr))return this.isOdd()?sr:Yn;if(this.isNegative())return e.isNegative()?this.neg().mul(e.neg()):this.neg().mul(e).neg();if(e.isNegative())return this.mul(e.neg()).neg();if(this.lt(Zn)&&e.lt(Zn))return Wn(this.toNumber()*e.toNumber(),this.unsigned);var t=this.high>>>16,n=65535&this.high,r=this.low>>>16,s=65535&this.low,o=e.high>>>16,a=65535&e.high,i=e.low>>>16,l=65535&e.low,u=0,c=0,h=0,d=0;return h+=(d+=s*l)>>>16,c+=(h+=r*l)>>>16,h&=65535,c+=(h+=s*i)>>>16,u+=(c+=n*l)>>>16,c&=65535,u+=(c+=r*i)>>>16,c&=65535,u+=(c+=s*a)>>>16,u+=t*l+n*i+r*a+s*o,Gn((h&=65535)<<16|(d&=65535),(u&=65535)<<16|(c&=65535),this.unsigned)},or.mul=or.multiply,or.divide=function(e){if(On(e)||(e=Vn(e)),e.isZero())throw Error("division by zero");var t,n,r;if(Cn)return this.unsigned||-2147483648!==this.high||-1!==e.low||-1!==e.high?Gn((this.unsigned?Cn.div_u:Cn.div_s)(this.low,this.high,e.low,e.high),Cn.get_high(),this.unsigned):this;if(this.isZero())return this.unsigned?Xn:Yn;if(this.unsigned){if(e.unsigned||(e=e.toUnsigned()),e.gt(this))return Xn;if(e.gt(this.shru(1)))return er;r=Xn}else{if(this.eq(sr))return e.eq(Qn)||e.eq(tr)?sr:e.eq(sr)?Qn:(t=this.shr(1).div(e).shl(1)).eq(Yn)?e.isNegative()?Qn:tr:(n=this.sub(e.mul(t)),r=t.add(n.div(e)));if(e.eq(sr))return this.unsigned?Xn:Yn;if(this.isNegative())return e.isNegative()?this.neg().div(e.neg()):this.neg().div(e).neg();if(e.isNegative())return this.div(e.neg()).neg();r=Yn}for(n=this;n.gte(e);){t=Math.max(1,Math.floor(n.toNumber()/e.toNumber()));for(var s=Math.ceil(Math.log(t)/Math.LN2),o=s<=48?1:qn(2,s-48),a=Wn(t),i=a.mul(e);i.isNegative()||i.gt(n);)i=(a=Wn(t-=o,this.unsigned)).mul(e);a.isZero()&&(a=Qn),r=r.add(a),n=n.sub(i)}return r},or.div=or.divide,or.modulo=function(e){return On(e)||(e=Vn(e)),Cn?Gn((this.unsigned?Cn.rem_u:Cn.rem_s)(this.low,this.high,e.low,e.high),Cn.get_high(),this.unsigned):this.sub(this.div(e).mul(e))},or.mod=or.modulo,or.rem=or.modulo,or.not=function(){return Gn(~this.low,~this.high,this.unsigned)},or.and=function(e){return On(e)||(e=Vn(e)),Gn(this.low&e.low,this.high&e.high,this.unsigned)},or.or=function(e){return On(e)||(e=Vn(e)),Gn(this.low|e.low,this.high|e.high,this.unsigned)},or.xor=function(e){return On(e)||(e=Vn(e)),Gn(this.low^e.low,this.high^e.high,this.unsigned)},or.shiftLeft=function(e){return On(e)&&(e=e.toInt()),0==(e&=63)?this:e<32?Gn(this.low<<e,this.high<<e|this.low>>>32-e,this.unsigned):Gn(0,this.low<<e-32,this.unsigned)},or.shl=or.shiftLeft,or.shiftRight=function(e){return On(e)&&(e=e.toInt()),0==(e&=63)?this:e<32?Gn(this.low>>>e|this.high<<32-e,this.high>>e,this.unsigned):Gn(this.high>>e-32,this.high>=0?0:-1,this.unsigned)},or.shr=or.shiftRight,or.shiftRightUnsigned=function(e){if(On(e)&&(e=e.toInt()),0===(e&=63))return this;var t=this.high;return e<32?Gn(this.low>>>e|t<<32-e,t>>>e,this.unsigned):Gn(32===e?t:t>>>e-32,0,this.unsigned)},or.shru=or.shiftRightUnsigned,or.shr_u=or.shiftRightUnsigned,or.toSigned=function(){return this.unsigned?Gn(this.low,this.high,!1):this},or.toUnsigned=function(){return this.unsigned?this:Gn(this.low,this.high,!0)},or.toBytes=function(e){return e?this.toBytesLE():this.toBytesBE()},or.toBytesLE=function(){var e=this.high,t=this.low;return[255&t,t>>>8&255,t>>>16&255,t>>>24,255&e,e>>>8&255,e>>>16&255,e>>>24]},or.toBytesBE=function(){var e=this.high,t=this.low;return[e>>>24,e>>>16&255,e>>>8&255,255&e,t>>>24,t>>>16&255,t>>>8&255,255&t]},Pn.fromBytes=function(e,t,n){return n?Pn.fromBytesLE(e,t):Pn.fromBytesBE(e,t)},Pn.fromBytesLE=function(e,t){return new Pn(e[0]|e[1]<<8|e[2]<<16|e[3]<<24,e[4]|e[5]<<8|e[6]<<16|e[7]<<24,t)},Pn.fromBytesBE=function(e,t){return new Pn(e[4]<<24|e[5]<<16|e[6]<<8|e[7],e[0]<<24|e[1]<<16|e[2]<<8|e[3],t)};var ar=Bn(Fn);const ir=ar||t({__proto__:null,default:ar},[Fn]);function lr(e){return ir.fromString(e,!0,16)}const ur=lr("c3a5c85c97cb3127"),cr=lr("b492b66fbe98f273"),hr=lr("9ae16a3b2f90404f");function dr(e){return e.xor(e.shru(47))}function pr(e,t,n){const r=e.slice(t,t+n);return ir.fromBytes(Array.from(r),!0,!0)}function fr(e,t){return pr(e,t,8)}function gr(e,t){return pr(e,t,4)}function mr(e,t){return 0===t?e:e.shru(t).or(e.shl(64-t))}function br(e,t,n=lr("9ddfea08eb382d69")){let r=e.xor(t).mul(n);r=r.xor(r.shru(47));let s=t.xor(r).mul(n);return s=s.xor(s.shru(47)),s=s.mul(n),s}function yr(e,t,n,r){return function(e,t,n,r,s,o){s=s.add(e),o=mr(o.add(s).add(r),21);const a=s;return s=(s=s.add(t)).add(n),o=o.add(mr(s,44)),[s.add(r),o.add(a)]}(fr(e,t),fr(e,t+8),fr(e,t+16),fr(e,t+24),n,r)}function wr(e,t){if("string"===t)throw new Error("Cannot convert a string[] to a TypedArray");if(Array.isArray(e)&&(e=Sr(e)),L().getBool("DEBUG")&&w(e,t),function(e,t){return e instanceof Float32Array&&"float32"===t||e instanceof Int32Array&&"int32"===t||e instanceof Uint8Array&&"bool"===t}(e,t))return e;if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return new Int32Array(e);if("bool"===t){const t=new Uint8Array(e.length);for(let n=0;n<t.length;++n)0!==Math.round(e[n])&&(t[n]=1);return t}throw new Error(`Unknown data type ${t}`)}function kr(){return L().platform.now()}function vr(e,t="utf-8"){return t=t||"utf-8",L().platform.encode(e,t)}function xr(e,t="utf-8"){return t=t||"utf-8",L().platform.decode(e,t)}function Er(e){return null!=L().platform.isTypedArray?L().platform.isTypedArray(e):Dn(e)}function Sr(e,t=[],n=!1){if(null==t&&(t=[]),"boolean"==typeof e||"number"==typeof e||"string"==typeof e||F(e)||null==e||Er(e)&&n)t.push(e);else if(Array.isArray(e)||Er(e))for(let r=0;r<e.length;++r)Sr(e[r],t,n);else{let r=-1;for(const t of Object.keys(e))/^([1-9]+[0-9]*|0)$/.test(t)&&(r=Math.max(r,Number(t)));for(let s=0;s<=r;s++)Sr(e[s],t,n)}return t}var $r={__proto__:null,arraysEqual:d,arraysEqualWithNull:h,assert:i,assertNonNegativeIntegerDimensions:B,assertNonNull:u,assertShapesMatch:l,bytesFromStringArray:x,bytesPerElement:v,checkConversionForErrors:w,clamp:o,computeStrides:N,convertBackendValuesAndArrayBuffer:function(e,t){if(Array.isArray(e))return e;if("float32"===t)return e instanceof Float32Array?e:new Float32Array(e);if("int32"===t)return e instanceof Int32Array?e:new Int32Array(e);if("bool"===t||"string"===t)return Uint8Array.from(new Int32Array(e));throw new Error(`Unknown dtype ${t}`)},createScalarValue:function(e,t){return"string"===t?vr(e):wr([e],t)},createShuffledIndices:function(e){const t=new Uint32Array(e);for(let n=0;n<e;++n)t[n]=n;return s(t),t},decodeString:xr,distSquared:function(e,t){let n=0;for(let r=0;r<e.length;r++){const s=Number(e[r])-Number(t[r]);n+=s*s}return n},encodeString:vr,fetch:function(e,t){return L().platform.fetch(e,t)},fingerPrint64:function(e,t=e.length){const n=ir.fromNumber(81,!0);if(t<=32)return t<=16?function(e,t=e.length){if(t>=8){const n=hr.add(2*t),r=fr(e,0).add(hr),s=fr(e,t-8);return br(mr(s,37).mul(n).add(r),mr(r,25).add(s).mul(n),n)}if(t>=4){const n=hr.add(2*t);return br(gr(e,0).shl(3).add(t),gr(e,t-4),n)}if(t>0){const n=e[0]+(e[t>>1]<<8),r=t+(e[t-1]<<2);return dr(hr.mul(n).xor(ur.mul(r))).mul(hr)}return hr}(e,t):function(e,t=e.length){const n=hr.add(2*t),r=fr(e,0).mul(cr),s=fr(e,8),o=fr(e,t-8).mul(n),a=fr(e,t-16).mul(hr);return br(mr(r.add(s),43).add(mr(o,30)).add(a),r.add(mr(s.add(hr),18)).add(o),n)}(e,t);if(t<=64)return function(e,t=e.length){const n=hr.add(2*t),r=fr(e,0).mul(hr),s=fr(e,8),o=fr(e,t-8).mul(n),a=fr(e,t-16).mul(hr),i=mr(r.add(s),43).add(mr(o,30)).add(a),l=br(i,r.add(mr(s.add(hr),18)).add(o),n),u=fr(e,16).mul(n),c=fr(e,24),h=i.add(fr(e,t-32)).mul(n),d=l.add(fr(e,t-24)).mul(n);return br(mr(u.add(c),43).add(mr(h,30)).add(d),u.add(mr(c.add(r),18)).add(h),n)}(e,t);let r=n,s=n.mul(cr).add(113),o=dr(s.mul(hr).add(113)).mul(hr),a=[ir.UZERO,ir.UZERO],i=[ir.UZERO,ir.UZERO];r=r.mul(hr).add(fr(e,0));let l=0;const u=64*(t-1>>6),c=u+(t-1&63)-63;do{r=mr(r.add(s).add(a[0]).add(fr(e,l+8)),37).mul(cr),s=mr(s.add(a[1]).add(fr(e,l+48)),42).mul(cr),r=r.xor(i[1]),s=s.add(a[0]).add(fr(e,l+40)),o=mr(o.add(i[0]),33).mul(cr),a=yr(e,l,a[1].mul(cr),r.add(i[0])),i=yr(e,l+32,o.add(i[1]),s.add(fr(e,l+16))),[o,r]=[r,o],l+=64}while(l!==u);const h=cr.add(o.and(255).shl(1));return l=c,i[0]=i[0].add(t-1&63),a[0]=a[0].add(i[0]),i[0]=i[0].add(a[0]),r=mr(r.add(s).add(a[0]).add(fr(e,l+8)),37).mul(h),s=mr(s.add(a[1]).add(fr(e,l+48)),42).mul(h),r=r.xor(i[1].mul(9)),s=s.add(a[0].mul(9).add(fr(e,l+40))),o=mr(o.add(i[0]),33).mul(h),a=yr(e,l,a[1].mul(h),r.add(i[0])),i=yr(e,l+32,o.add(i[1]),s.add(fr(e,l+16))),[o,r]=[r,o],br(br(a[0],i[0],h).add(dr(s).mul(ur)).add(o),br(a[1],i[1],h).add(r),h)},flatten:Sr,getArrayFromDType:y,getTypedArrayFromDType:b,hasEncodingLoss:function(e,t){return"complex64"!==t&&(("float32"!==t||"complex64"===e)&&(("int32"!==t||"float32"===e||"complex64"===e)&&("bool"!==t||"bool"!==e)))},hexToLong:lr,indexToLoc:function(e,t,n){if(0===t)return[];if(1===t)return[e];const r=new Array(t);for(let t=0;t<r.length-1;++t)r[t]=Math.floor(e/n[t]),e-=r[t]*n[t];return r[r.length-1]=e,r},inferDtype:A,inferFromImplicitShape:function(e,t){let n=1,r=-1;for(let t=0;t<e.length;++t)if(e[t]>=0)n*=e[t];else if(-1===e[t]){if(-1!==r)throw Error(`Shapes can only have 1 implicit size. Found -1 at dim ${r} and dim ${t}`);r=t}else if(e[t]<0)throw Error(`Shapes can not be < 0. Found ${e[t]} at dim ${t}`);if(-1===r){if(t>0&&t!==n)throw Error(`Size(${t}) must match the product of shape ${e}`);return e}if(0===n)throw Error(`Cannot infer the missing size in [${e}] when there are 0 elements`);if(t%n!=0)throw Error(`The implicit shape can't be a fractional number. Got ${t} / ${n}`);const s=e.slice();return s[r]=t/n,s},isBoolean:S,isFunction:_,isInt:p,isNumber:$,isPromise:F,isScalarShape:function(e){return 0===e.length},isString:E,isTypedArray:Er,isValidDtype:k,locToIndex:function(e,t,n){if(0===t)return 0;if(1===t)return e[0];let r=e[e.length-1];for(let t=0;t<e.length-1;++t)r+=n[t]*e[t];return r},makeOnesTypedArray:D,makeZerosNestedTypedArray:function(e,t){const n=e.reduce(((e,t)=>e*t),1);if(null==t||"float32"===t)return T(e,new Float32Array(n));if("int32"===t)return T(e,new Int32Array(n));if("bool"===t)return T(e,new Uint8Array(n));throw new Error(`Unknown data type ${t}`)},makeZerosTypedArray:R,nearestDivisor:I,nearestLargerEven:function(e){return e%2==0?e:e+1},now:kr,parseAxisParam:g,randUniform:function(e,t){const n=Math.random();return t*n+(1-n)*e},repeatedTry:function(e,t=(e=>0),n,r){return new Promise(((s,o)=>{let a=0;const i=()=>{if(e())return void s();a++;const l=t(a);null!=n&&a>=n?o():null!=r?r(i,l):setTimeout(i,l)};i()}))},rightPad:f,shuffle:s,shuffleCombo:function(e,t){if(e.length!==t.length)throw new Error(`Array sizes must match to be shuffled together First array length was ${e.length}Second array length was ${t.length}`);let n=e.length,r=0;for(;n>0;)r=Math.random()*n|0,n--,a(e,n,r),a(t,n,r)},sizeFromShape:c,sizeToSquarishShape:function(e){const t=Math.ceil(Math.sqrt(e));return[t,Math.ceil(e/t)]},squeezeShape:m,sum:function(e){let t=0;for(let n=0;n<e.length;n++)t+=e[n];return t},swap:a,tanh:function(e){if(null!=Math.tanh)return Math.tanh(e);if(e===1/0)return 1;if(e===-1/0)return-1;{const t=Math.exp(2*e);return(t-1)/(t+1)}},toNestedArray:T,toTypedArray:wr};class Ar{constructor(e,t){this.backendTimer=e,this.logger=t,null==t&&(this.logger=new Ir)}profileKernel(e,t,n){let r;const s=()=>{r=n()};let o;const a=kr();if(this.backendTimer.timerAvailable())o=this.backendTimer.time(s);else{s();for(const e of r)e.dataSync();o=Promise.resolve({kernelMs:kr()-a})}if(L().getBool("CHECK_COMPUTATION_FOR_ERRORS"))for(let t=0;t<r.length;t++){const n=r[t];n.data().then((t=>{_r(t,n.dtype,e)}))}return{kernelName:e,outputs:r,inputs:t,timeMs:o.then((e=>e.kernelMs)),extraInfo:o.then((e=>null!=e.getExtraProfileInfo?e.getExtraProfileInfo():""))}}logKernelProfile(e){const{kernelName:t,outputs:n,timeMs:r,inputs:s,extraInfo:o}=e;n.forEach((e=>{Promise.all([e.data(),r,o]).then((n=>{this.logger.logKernelProfile(t,e,n[0],n[1],s,n[2])}))}))}}function _r(e,t,n){if("float32"!==t)return!1;for(let t=0;t<e.length;t++){const r=e[t];if(isNaN(r)||!isFinite(r))return console.warn(`Found ${r} in the result of '${n}'`),!0}return!1}class Ir{logKernelProfile(e,t,n,r,s,o){const a="number"==typeof r?f(`${r}ms`,9):r.error,i=f(e,25),l=t.rank,u=t.size,c=f(t.shape.toString(),14);let h="";for(const e in s){const n=s[e];if(null!=n){const r=n.shape||t.shape,s=r.length;h+=`${e}: ${s}D ${s>0?r:""} `}}console.log(`%c${i}\t%c${a}\t%c${l}D ${c}\t%c${u}\t%c${h}\t%c${o}`,"font-weight:bold","color:red","color:blue","color: orange","color: green","color: steelblue")}}function Nr(e,t,n,r){const s=N(t),o=function(e,t,n,r){const s=c(t),o=r[r.length-1],a=new Array(o).fill(0),i=t.length,l="complex64"===n?Rr(e):e;if(i>1)for(let e=0;e<s/o;e++){const t=e*o;for(let e=0;e<o;e++)a[e]=Math.max(a[e],Mr(l[t+e],0,n).length)}return a}(e,t,n,s),a=t.length,i=Dr(e,t,n,s,o),l=["Tensor"];return r&&(l.push(` dtype: ${n}`),l.push(` rank: ${a}`),l.push(` shape: [${t}]`),l.push(" values:")),l.push(i.map((e=>" "+e)).join("\n")),l.join("\n")}function Mr(e,t,n){let r;return r=Array.isArray(e)?`${parseFloat(e[0].toFixed(7))} + ${parseFloat(e[1].toFixed(7))}j`:E(e)?`'${e}'`:"bool"===n?Tr(e):parseFloat(e.toFixed(7)).toString(),f(r,t)}function Tr(e){return 0===e?"false":"true"}function Dr(e,t,n,r,s,o=!0){const a="complex64"===n?2:1,i=t[0],l=t.length;if(0===l){if("complex64"===n){return[Mr(Rr(e)[0],0,n)]}return"bool"===n?[Tr(e[0])]:[e[0].toString()]}if(1===l){if(i>20){const t=3*a;let r=Array.from(e.slice(0,t)),o=Array.from(e.slice((i-3)*a,i*a));return"complex64"===n&&(r=Rr(r),o=Rr(o)),["["+r.map(((e,t)=>Mr(e,s[t],n))).join(", ")+", ..., "+o.map(((e,t)=>Mr(e,s[i-3+t],n))).join(", ")+"]"]}return["["+("complex64"===n?Rr(e):Array.from(e)).map(((e,t)=>Mr(e,s[t],n))).join(", ")+"]"]}const u=t.slice(1),c=r.slice(1),h=r[0]*a,d=[];if(i>20){for(let t=0;t<3;t++){const r=t*h,o=r+h;d.push(...Dr(e.slice(r,o),u,n,c,s,!1))}d.push("...");for(let t=i-3;t<i;t++){const r=t*h,o=r+h;d.push(...Dr(e.slice(r,o),u,n,c,s,t===i-1))}}else for(let t=0;t<i;t++){const r=t*h,o=r+h;d.push(...Dr(e.slice(r,o),u,n,c,s,t===i-1))}const p=2===l?",":"";d[0]="["+(i>0?d[0]+p:"");for(let e=1;e<d.length-1;e++)d[e]=" "+d[e]+p;let f=",\n";for(let e=2;e<l;e++)f+="\n";return d[d.length-1]=" "+d[d.length-1]+"]"+(o?"":f),d}function Rr(e){const t=[];for(let n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}class Br{constructor(e,t,n){if(this.dtype=t,this.shape=e.slice(),this.size=c(e),null!=n){const e=n.length;i(e===this.size,(()=>`Length of values '${e}' does not match the size inferred by the shape '${this.size}'.`))}if("complex64"===t)throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag).");this.values=n||y(t,this.size),this.strides=N(e)}set(e,...t){0===t.length&&(t=[0]),i(t.length===this.rank,(()=>`The number of provided coordinates (${t.length}) must match the rank (${this.rank})`));const n=this.locToIndex(t);this.values[n]=e}get(...e){0===e.length&&(e=[0]);let t=0;for(const n of e){if(n<0||n>=this.shape[t]){const t=`Requested out of range element at ${e}. Buffer shape=${this.shape}`;throw new Error(t)}t++}let n=e[e.length-1];for(let t=0;t<e.length-1;++t)n+=this.strides[t]*e[t];return this.values[n]}locToIndex(e){if(0===this.rank)return 0;if(1===this.rank)return e[0];let t=e[e.length-1];for(let n=0;n<e.length-1;++n)t+=this.strides[n]*e[n];return t}indexToLoc(e){if(0===this.rank)return[];if(1===this.rank)return[e];const t=new Array(this.shape.length);for(let n=0;n<t.length-1;++n)t[n]=Math.floor(e/this.strides[n]),e-=t[n]*this.strides[n];return t[t.length-1]=e,t}get rank(){return this.shape.length}toTensor(){return Fr().makeTensor(this.values,this.shape,this.dtype)}}let Fr=null,Cr=null;class Pr{constructor(e,t,n,r){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=c(e),this.strides=N(e),this.dataId=n,this.id=r,this.rankType=this.rank<5?this.rank.toString():"higher"}get rank(){return this.shape.length}async buffer(){const e=await this.data();return Cr.buffer(this.shape,this.dtype,e)}bufferSync(){return Cr.buffer(this.shape,this.dtype,this.dataSync())}async array(){const e=await this.data();return T(this.shape,e,"complex64"===this.dtype)}arraySync(){return T(this.shape,this.dataSync(),"complex64"===this.dtype)}async data(){this.throwIfDisposed();const e=Fr().read(this.dataId);if("string"===this.dtype){const t=await e;try{return t.map((e=>xr(e)))}catch(e){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}}return e}dataToGPU(e){return this.throwIfDisposed(),Fr().readToGPU(this.dataId,e)}dataSync(){this.throwIfDisposed();const e=Fr().readSync(this.dataId);if("string"===this.dtype)try{return e.map((e=>xr(e)))}catch(e){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}return e}async bytes(){this.throwIfDisposed();const e=await Fr().read(this.dataId);return"string"===this.dtype?e:new Uint8Array(e.buffer)}dispose(){this.isDisposed||(this.kerasMask&&this.kerasMask.dispose(),Fr().disposeTensor(this),this.isDisposedInternal=!0)}get isDisposed(){return this.isDisposedInternal}throwIfDisposed(){if(this.isDisposed)throw new Error("Tensor is disposed.")}print(e=!1){return Cr.print(this,e)}clone(){return this.throwIfDisposed(),Cr.clone(this)}toString(e=!1){return Nr(this.dataSync(),this.shape,this.dtype,e)}cast(e){return this.throwIfDisposed(),Cr.cast(this,e)}variable(e=!0,t,n){return this.throwIfDisposed(),Fr().makeVariable(this,e,t,n)}}function Or(){return W("Tensor",(()=>Pr))}Object.defineProperty(Pr,Symbol.hasInstance,{value:e=>!!e&&null!=e.data&&null!=e.dataSync&&null!=e.throwIfDisposed}),Or();class Lr extends Pr{constructor(e,t,n,r){super(e.shape,e.dtype,e.dataId,r),this.trainable=t,this.name=n}assign(e){if(e.dtype!==this.dtype)throw new Error(`dtype of the new value (${e.dtype}) and previous value (${this.dtype}) must match`);if(!d(e.shape,this.shape))throw new Error(`shape of the new value (${e.shape}) and previous value (${this.shape}) must match`);Fr().disposeTensor(this),this.dataId=e.dataId,Fr().incRef(this,null)}dispose(){Fr().disposeVariable(this),this.isDisposedInternal=!0}}var zr,Ur,Wr,Gr,qr;Object.defineProperty(Lr,Symbol.hasInstance,{value:e=>e instanceof Pr&&null!=e.assign&&e.assign instanceof Function}),e.Rank=void 0,(zr=e.Rank||(e.Rank={})).R0="R0",zr.R1="R1",zr.R2="R2",zr.R3="R3",zr.R4="R4",zr.R5="R5",zr.R6="R6",function(e){e.float32="float32",e.int32="int32",e.bool="int32",e.complex64="complex64"}(Ur||(Ur={})),function(e){e.float32="float32",e.int32="int32",e.bool="bool",e.complex64="complex64"}(Wr||(Wr={})),function(e){e.float32="float32",e.int32="float32",e.bool="float32",e.complex64="complex64"}(Gr||(Gr={})),function(e){e.float32="complex64",e.int32="complex64",e.bool="complex64",e.complex64="complex64"}(qr||(qr={}));const Kr={float32:Gr,int32:Ur,bool:Wr,complex64:qr};function Vr(e,t){if("string"===e||"string"===t){if("string"===e&&"string"===t)return"string";throw new Error(`Can not upcast ${e} with ${t}`)}return Kr[e][t]}function Hr(e){return null!=e&&"object"==typeof e&&"texture"in e&&e.texture instanceof WebGLTexture}function jr(e){return"undefined"!=typeof GPUBuffer&&null!=e&&"object"==typeof e&&"buffer"in e&&e.buffer instanceof GPUBuffer}function Jr(e,t){if(e.dtype===t.dtype)return[e,t];const n=Vr(e.dtype,t.dtype);return[e.cast(n),t.cast(n)]}function Zr(e,t){i(e.dtype===t.dtype,(()=>`The dtypes of the first(${e.dtype}) and second(${t.dtype}) input must match`))}function Yr(e){const t=[];return Xr(e,t,new Set),t}function Xr(e,t,n){if(null==e)return;if(e instanceof Pr)return void t.push(e);if(r=e,!Array.isArray(r)&&"object"!=typeof r)return;var r;const s=e;for(const e in s){const r=s[e];n.has(r)||(n.add(r),Xr(r,t,n))}}var Qr={__proto__:null,assertTypesMatch:Zr,getTensorsInContainer:Yr,isTensorInList:function(e,t){return t.some((t=>t.id===e.id))},makeTypesMatch:Jr};function es(e){return null!=e.kernelName}class ts{constructor(){this.registeredVariables={},this.nextTapeNodeId=0,this.numBytes=0,this.numTensors=0,this.numStringTensors=0,this.numDataBuffers=0,this.gradientDepth=0,this.kernelDepth=0,this.scopeStack=[],this.numDataMovesStack=[],this.nextScopeId=0,this.tensorInfo=new WeakMap,this.profiling=!1,this.activeProfile={newBytes:0,newTensors:0,peakBytes:0,kernels:[],result:null,get kernelNames(){return Array.from(new Set(this.kernels.map((e=>e.name))))}}}dispose(){for(const e in this.registeredVariables)this.registeredVariables[e].dispose()}}class ns{constructor(e){this.ENV=e,this.registry={},this.registryFactory={},this.pendingBackendInitId=0,this.state=new ts}async ready(){if(null!=this.pendingBackendInit)return this.pendingBackendInit.then((()=>{}));if(null!=this.backendInstance)return;const e=this.getSortedBackends();for(let t=0;t<e.length;t++){const n=e[t];if(await this.initializeBackend(n).success)return void await this.setBackend(n)}throw new Error("Could not initialize any backends, all backend initializations failed.")}get backend(){if(null!=this.pendingBackendInit)throw new Error(`Backend '${this.backendName}' has not yet been initialized. Make sure to await tf.ready() or await tf.setBackend() before calling other methods`);if(null==this.backendInstance){const{name:e,asyncInit:t}=this.initializeBackendsAndReturnBest();if(t)throw new Error(`The highest priority backend '${e}' has not yet been initialized. Make sure to await tf.ready() or await tf.setBackend() before calling other methods`);this.setBackend(e)}return this.backendInstance}backendNames(){return Object.keys(this.registryFactory)}findBackend(e){if(!(e in this.registry)){if(!(e in this.registryFactory))return null;{const{asyncInit:t}=this.initializeBackend(e);if(t)return null}}return this.registry[e]}findBackendFactory(e){return e in this.registryFactory?this.registryFactory[e].factory:null}registerBackend(e,t,n=1){return e in this.registryFactory?(Sn(`${e} backend was already registered. Reusing existing backend factory.`),!1):(this.registryFactory[e]={factory:t,priority:n},!0)}async setBackend(e){if(null==this.registryFactory[e])throw new Error(`Backend name '${e}' not found in registry`);if(this.backendName=e,null==this.registry[e]){this.backendInstance=null;const{success:t,asyncInit:n}=this.initializeBackend(e);if(!(n?await t:t))return!1}return this.backendInstance=this.registry[e],this.setupRegisteredKernels(),this.profiler=new Ar(this.backendInstance),!0}setupRegisteredKernels(){Nn(this.backendName).forEach((e=>{null!=e.setupFunc&&e.setupFunc(this.backendInstance)}))}disposeRegisteredKernels(e){Nn(e).forEach((t=>{null!=t.disposeFunc&&t.disposeFunc(this.registry[e])}))}initializeBackend(e){const t=this.registryFactory[e];if(null==t)throw new Error(`Cannot initialize backend ${e}, no registration found.`);try{const r=t.factory();if(!r||r instanceof n||"function"!=typeof r.then)return this.registry[e]=r,{success:!0,asyncInit:!1};{const t=++this.pendingBackendInitId,n=r.then((n=>!(t<this.pendingBackendInitId)&&(this.registry[e]=n,this.pendingBackendInit=null,!0))).catch((n=>(t<this.pendingBackendInitId||(this.pendingBackendInit=null,Sn(`Initialization of backend ${e} failed`),Sn(n.stack||n.message)),!1)));return this.pendingBackendInit=n,{success:n,asyncInit:!0}}}catch(t){return Sn(`Initialization of backend ${e} failed`),Sn(t.stack||t.message),{success:!1,asyncInit:!1}}}removeBackend(e){if(!(e in this.registryFactory))throw new Error(`${e} backend not found in registry`);this.backendName===e&&null!=this.pendingBackendInit&&this.pendingBackendInitId++,e in this.registry&&(this.disposeRegisteredKernels(e),this.registry[e].dispose(),delete this.registry[e]),delete this.registryFactory[e],this.backendName===e&&(this.pendingBackendInit=null,this.backendName=null,this.backendInstance=null)}getSortedBackends(){if(0===Object.keys(this.registryFactory).length)throw new Error("No backend found in registry.");return Object.keys(this.registryFactory).sort(((e,t)=>this.registryFactory[t].priority-this.registryFactory[e].priority))}initializeBackendsAndReturnBest(){const e=this.getSortedBackends();for(let t=0;t<e.length;t++){const n=e[t],{success:r,asyncInit:s}=this.initializeBackend(n);if(s||r)return{name:n,asyncInit:s}}throw new Error("Could not initialize any backends, all backend initializations failed.")}moveData(e,t){const n=this.state.tensorInfo.get(t),r=n.backend,s=this.readSync(t),o=r.refCount(t);r.disposeData(t,!0),n.backend=e,e.move(t,s,n.shape,n.dtype,o),this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack[this.state.numDataMovesStack.length-1]++}tidy(e,t){let n,r=null;if(null==t){if("function"!=typeof e)throw new Error("Please provide a function to tidy()");t=e}else{if("string"!=typeof e&&!(e instanceof String))throw new Error("When calling with two arguments, the first argument to tidy() must be a string");if("function"!=typeof t)throw new Error("When calling with two arguments, the 2nd argument to tidy() must be a function");r=e}return this.scopedRun((()=>this.startScope(r)),(()=>this.endScope(n)),(()=>(n=t(),n instanceof Promise&&console.error("Cannot return a Promise inside of tidy."),n)))}scopedRun(e,t,n){e();try{const e=n();return t(),e}catch(e){throw t(),e}}nextTensorId(){return ns.nextTensorId++}nextVariableId(){return ns.nextVariableId++}clone(e){const t=ss.runKernel(qe,{x:e}),n={x:e};return this.addTapeNode(this.state.activeScope.name,n,[t],(e=>({x:()=>{const t={x:e},n={dtype:"float32"};return ss.runKernel(ie,t,n)}})),[],{}),t}runKernel(e,t,n){null==this.backendName&&this.backend;if(!(null!=_n(e,this.backendName)))throw new Error(`Kernel '${e}' not registered for backend '${this.backendName}'`);return this.runKernelFunc({kernelName:e,inputs:t,attrs:n})}shouldCheckForMemLeaks(){return this.ENV.getBool("IS_TEST")}checkKernelForMemLeak(e,t,n){const r=this.backend.numDataIds();let s=0;n.forEach((e=>{s+="complex64"===e.dtype?3:1}));const o=this.state.numDataMovesStack[this.state.numDataMovesStack.length-1],a=r-t-s-o;if(a>0)throw new Error(`Backend '${this.backendName}' has an internal memory leak (${a} data ids) after running '${e}'`)}runKernelFunc(e){let t,n=[];const r=this.isTapeOn(),s=this.state.numBytes,o=this.state.numTensors;let a,l;this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack.push(0),null==this.backendName&&this.backend;const u=es(e)?e.kernelName:null!=this.state.activeScope?this.state.activeScope.name:"";if(es(e)){const{kernelName:t,inputs:s,attrs:o}=e;null==this.backendName&&this.backend;const u=_n(t,this.backendName);i(null!=u,(()=>`Cannot find registered kernel '${t}' for backend '${this.backendName}'`)),a=()=>{const e=this.backend.numDataIds();l=u.kernelFunc({inputs:s,attrs:o,backend:this.backend});const a=Array.isArray(l)?l:[l];this.shouldCheckForMemLeaks()&&this.checkKernelForMemLeak(t,e,a);const i=a.map((e=>null!=e.rank?e:this.makeTensorFromTensorInfo(e)));if(r){const e=this.getTensorsForGradient(t,s,i);n=this.saveTensorsForBackwardMode(e)}return i}}else{const{forwardFunc:t}=e,s=e=>{r&&(n=e.map((e=>this.keep(this.clone(e)))))};a=()=>{const e=this.backend.numDataIds();l=this.tidy((()=>t(this.backend,s)));const n=Array.isArray(l)?l:[l];return this.shouldCheckForMemLeaks()&&this.checkKernelForMemLeak(u,e,n),n}}const{inputs:c,attrs:h}=e,d=es(e)?null:e.backwardsFunc;let p;return this.scopedRun((()=>this.state.kernelDepth++),(()=>this.state.kernelDepth--),(()=>{this.ENV.getBool("DEBUG")||this.state.profiling?(p=this.profiler.profileKernel(u,c,(()=>a())),this.ENV.getBool("DEBUG")&&this.profiler.logKernelProfile(p),t=p.outputs):t=a()})),r&&this.addTapeNode(u,c,t,d,n,h),this.state.profiling&&this.state.activeProfile.kernels.push({name:u,bytesAdded:this.state.numBytes-s,totalBytesSnapshot:this.state.numBytes,tensorsAdded:this.state.numTensors-o,totalTensorsSnapshot:this.state.numTensors,inputShapes:Object.keys(c).map((e=>null!=c[e]?c[e].shape:null)),outputShapes:t.map((e=>e.shape)),kernelTimeMs:p.timeMs,extraInfo:p.extraInfo}),Array.isArray(l)?t:t[0]}saveTensorsForBackwardMode(e){const t=e.map((e=>this.keep(this.clone(e))));return t}getTensorsForGradient(e,t,n){const r=In(e);if(null!=r){const e=r.inputsToSave||[],s=r.outputsToSave||[];let o;r.saveAllInputs?(i(Array.isArray(t),(()=>"saveAllInputs is true, expected inputs to be an array.")),o=Object.keys(t).map((e=>t[e]))):o=e.map((e=>t[e]));const a=n.filter(((e,t)=>s[t]));return o.concat(a)}return[]}makeTensor(e,t,n,r){if(null==e)throw new Error("Values passed to engine.makeTensor() are null");n=n||"float32",r=r||this.backend;let s=e;"string"===n&&E(e[0])&&(s=e.map((e=>vr(e))));const o=r.write(s,t,n),a=new Pr(t,n,o,this.nextTensorId());if(this.trackTensor(a,r),"string"===n){const e=this.state.tensorInfo.get(o),t=x(s);this.state.numBytes+=t-e.bytes,e.bytes=t}return a}makeTensorFromDataId(e,t,n,r){const s={dataId:e,shape:t,dtype:n=n||"float32"};return this.makeTensorFromTensorInfo(s,r)}makeTensorFromTensorInfo(e,t){const{dataId:n,shape:r,dtype:s}=e,o=new Pr(r,s,n,this.nextTensorId());return this.trackTensor(o,t),o}makeVariable(e,t=!0,n,r){n=n||this.nextVariableId().toString(),null!=r&&r!==e.dtype&&(e=e.cast(r));const s=new Lr(e,t,n,this.nextTensorId());if(null!=this.state.registeredVariables[s.name])throw new Error(`Variable with name ${s.name} was already registered`);return this.state.registeredVariables[s.name]=s,this.incRef(s,this.backend),s}trackTensor(e,t){this.state.numTensors++,"string"===e.dtype&&this.state.numStringTensors++;let n=0;"complex64"!==e.dtype&&"string"!==e.dtype&&(n=e.size*v(e.dtype)),this.state.numBytes+=n,this.state.tensorInfo.has(e.dataId)||(this.state.numDataBuffers++,this.state.tensorInfo.set(e.dataId,{backend:t||this.backend,dtype:e.dtype,shape:e.shape,bytes:n})),e instanceof Lr||this.track(e)}incRef(e,t){this.trackTensor(e,t),this.backend.incRef(e.dataId)}removeDataId(e,t){this.state.tensorInfo.has(e)&&this.state.tensorInfo.get(e).backend===t&&(this.state.tensorInfo.delete(e),this.state.numDataBuffers--)}disposeTensor(e){if(!this.state.tensorInfo.has(e.dataId))return;const t=this.state.tensorInfo.get(e.dataId);if(this.state.numTensors--,"string"===e.dtype&&(this.state.numStringTensors--,this.state.numBytes-=t.bytes),"complex64"!==e.dtype&&"string"!==e.dtype){const t=e.size*v(e.dtype);this.state.numBytes-=t}t.backend.disposeData(e.dataId)&&this.removeDataId(e.dataId,t.backend)}disposeVariables(){for(const e in this.state.registeredVariables){const t=this.state.registeredVariables[e];this.disposeVariable(t)}}disposeVariable(e){this.disposeTensor(e),null!=this.state.registeredVariables[e.name]&&delete this.state.registeredVariables[e.name]}memory(){const e=this.backend.memory();return e.numTensors=this.state.numTensors,e.numDataBuffers=this.state.numDataBuffers,e.numBytes=this.state.numBytes,this.state.numStringTensors>0&&(e.unreliable=!0,null==e.reasons&&(e.reasons=[]),e.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)")),e}async profile(e){this.state.profiling=!0;const t=this.state.numBytes,n=this.state.numTensors;this.state.activeProfile.kernels=[],this.state.activeProfile.result=await e(),this.state.profiling=!1,this.state.activeProfile.peakBytes=Math.max(...this.state.activeProfile.kernels.map((e=>e.totalBytesSnapshot))),this.state.activeProfile.newBytes=this.state.numBytes-t,this.state.activeProfile.newTensors=this.state.numTensors-n;for(const e of this.state.activeProfile.kernels)e.kernelTimeMs=await e.kernelTimeMs,e.extraInfo=await e.extraInfo;return this.state.activeProfile}isTapeOn(){return this.state.gradientDepth>0&&0===this.state.kernelDepth}addTapeNode(e,t,n,r,s,o){const a={id:this.state.nextTapeNodeId++,kernelName:e,inputs:t,outputs:n,saved:s},i=In(e);null!=i&&(r=i.gradFunc),null!=r&&(a.gradient=e=>(e=e.map(((e,t)=>{if(null==e){const e=n[t],r=R(e.size,e.dtype);return this.makeTensor(r,e.shape,e.dtype)}return e})),r(e.length>1?e:e[0],s,o))),this.state.activeTape.push(a)}keep(e){return e.kept=!0,e}startTape(){0===this.state.gradientDepth&&(this.state.activeTape=[]),this.state.gradientDepth++}endTape(){this.state.gradientDepth--}startScope(e){const t={track:[],name:"unnamed scope",id:this.state.nextScopeId++};e&&(t.name=e),this.state.scopeStack.push(t),this.state.activeScope=t}endScope(e){const t=Yr(e),n=new Set(t.map((e=>e.id)));for(let e=0;e<this.state.activeScope.track.length;e++){const t=this.state.activeScope.track[e];t.kept||n.has(t.id)||t.dispose()}const r=this.state.scopeStack.pop();this.state.activeScope=0===this.state.scopeStack.length?null:this.state.scopeStack[this.state.scopeStack.length-1],t.forEach((e=>{e.kept||e.scopeId!==r.id||this.track(e)}))}gradients(e,t,n,r=!1){if(i(t.length>0,(()=>"gradients() received an empty list of xs.")),null!=n&&"float32"!==n.dtype)throw new Error(`dy must have 'float32' dtype, but